tmacrotypes.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. discard """
  2. nimout: '''intProc; ntyProc; proc[int, int, float]; proc (a: int; b: float): int
  3. void; ntyVoid; void; void
  4. int; ntyInt; int; int
  5. proc (); ntyProc; proc[void]; proc ()
  6. voidProc; ntyProc; proc[void]; proc ()
  7. listing fields for ObjType
  8. a: string
  9. b: int
  10. listing fields for ObjRef
  11. skipping ref type
  12. a: string
  13. b: int
  14. listing fields for RefType
  15. skipping ref type
  16. a: int
  17. b: float
  18. listing fields for typeof(a)
  19. skipping ref type
  20. a: string
  21. b: int
  22. listing fields for typeof(b)
  23. skipping ref type
  24. a: string
  25. b: int
  26. listing fields for typeof(c)
  27. skipping ref type
  28. a: int
  29. b: float
  30. listing fields for typeof(x)
  31. a: string
  32. b: int
  33. listing fields for typeof(x)
  34. a: int
  35. b: float
  36. typeDesc[range[1 .. 5]]; ntyTypeDesc; typeDesc[range[1, 5]]; typeDesc[range[1 .. 5]]
  37. typeDesc[range]; ntyTypeDesc; typeDesc[range[T]]; typeDesc[range]'''
  38. """
  39. import macros, typetraits
  40. macro checkType(ex: typed): untyped =
  41. echo ex.getTypeInst.repr, "; ", ex.typeKind, "; ", ex.getType.repr, "; ", ex.getTypeImpl.repr
  42. macro checkProcType(fn: typed): untyped =
  43. let fn_sym = if fn.kind == nnkProcDef: fn[0] else: fn
  44. echo fn_sym, "; ", fn_sym.typeKind, "; ", fn_sym.getType.repr, "; ", fn_sym.getTypeImpl.repr
  45. proc voidProc = echo "hello"
  46. proc intProc(a: int, b: float): int {.checkProcType.} = 10
  47. checkType(voidProc())
  48. checkType(intProc(10, 20.0))
  49. checkType(voidProc)
  50. checkProcType(voidProc)
  51. macro listFields(T: typed) =
  52. echo "listing fields for ", repr(T)
  53. let inputExprType = getType(T)
  54. var objType = inputExprType[1]
  55. if objType.kind == nnkBracketExpr and objType.len > 1:
  56. if ((objType[0].kind == nnkRefTy) or
  57. (objType[0].kind == nnkSym and eqIdent(objType[0], "ref"))):
  58. echo "skipping ref type"
  59. objType = objType[1]
  60. let typeAst = objType.getImpl
  61. var objectDef = typeAst[2]
  62. if objectDef.kind == nnkRefTy:
  63. objectDef = objectDef[0]
  64. let recList = objectDef[2]
  65. for rec in recList:
  66. echo $rec[0], ": ", $rec[1]
  67. type
  68. ObjType* = object of RootObj
  69. a: string
  70. b: int
  71. ObjRef = ref ObjType
  72. RefType* = ref object of RootObj
  73. a: int
  74. b: float
  75. listFields ObjType
  76. listFields ObjRef
  77. listFields RefType
  78. let
  79. a = new ObjType
  80. b = new ObjRef
  81. c = new RefType
  82. listFields typeOf(a)
  83. listFields typeOf(b)
  84. listFields typeOf(c)
  85. proc genericProc(x: object) =
  86. listFields typeOf(x)
  87. genericProc a[]
  88. genericProc b[]
  89. genericProc c[]
  90. # bug #10548
  91. block:
  92. var c {.compileTime.} = 0
  93. macro meshImpl(arg: typed): untyped =
  94. inc c
  95. result = arg
  96. type
  97. Blub = int32
  98. Mesh = meshImpl(Club)
  99. Club = Blub
  100. static: doAssert(c == 1)
  101. # bug #10702
  102. type
  103. VectorElementType = SomeNumber | bool
  104. Vec*[N : static[int], T: VectorElementType] = object
  105. arr*: array[N, T]
  106. type
  107. Vec4*[T: VectorElementType] = Vec[4,T]
  108. Vec3*[T: VectorElementType] = Vec[3,T]
  109. Vec2*[T: VectorElementType] = Vec[2,T]
  110. template vecGen(U:untyped,V:typed):typed=
  111. ## ``U`` suffix
  112. ## ``V`` valType
  113. ##
  114. type
  115. `Vec2 U`* {.inject.} = Vec2[V]
  116. `Vec3 U`* {.inject.} = Vec3[V]
  117. `Vec4 U`* {.inject.} = Vec4[V]
  118. vecGen(f, float32)
  119. macro foobar(arg: typed): untyped =
  120. let typ = arg.getTypeInst
  121. doAssert typ.getImpl[^1].kind == nnkCall
  122. var x: Vec2f
  123. foobar(x)
  124. checkType(range[1..5])
  125. checkType(range)