magicsys.nim 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Built-in types and compilerprocs are registered here.
  10. import
  11. ast, astalgo, msgs, platform, idents,
  12. modulegraphs, lineinfos
  13. export createMagic
  14. proc nilOrSysInt*(g: ModuleGraph): PType = g.sysTypes[tyInt]
  15. proc newSysType(g: ModuleGraph; kind: TTypeKind, size: int): PType =
  16. result = newType(kind, nextTypeId(g.idgen), g.systemModule)
  17. result.size = size
  18. result.align = size.int16
  19. proc getSysSym*(g: ModuleGraph; info: TLineInfo; name: string): PSym =
  20. result = systemModuleSym(g, getIdent(g.cache, name))
  21. if result == nil:
  22. localError(g.config, info, "system module needs: " & name)
  23. result = newSym(skError, getIdent(g.cache, name), nextSymId(g.idgen), g.systemModule, g.systemModule.info, {})
  24. result.typ = newType(tyError, nextTypeId(g.idgen), g.systemModule)
  25. if result.kind == skAlias: result = result.owner
  26. proc getSysMagic*(g: ModuleGraph; info: TLineInfo; name: string, m: TMagic): PSym =
  27. let id = getIdent(g.cache, name)
  28. for r in systemModuleSyms(g, id):
  29. if r.magic == m:
  30. # prefer the tyInt variant:
  31. if r.typ[0] != nil and r.typ[0].kind == tyInt: return r
  32. result = r
  33. if result != nil: return result
  34. localError(g.config, info, "system module needs: " & name)
  35. result = newSym(skError, id, nextSymId(g.idgen), g.systemModule, g.systemModule.info, {})
  36. result.typ = newType(tyError, nextTypeId(g.idgen), g.systemModule)
  37. proc sysTypeFromName*(g: ModuleGraph; info: TLineInfo; name: string): PType =
  38. result = getSysSym(g, info, name).typ
  39. proc getSysType*(g: ModuleGraph; info: TLineInfo; kind: TTypeKind): PType =
  40. template sysTypeFromName(s: string): untyped = sysTypeFromName(g, info, s)
  41. result = g.sysTypes[kind]
  42. if result == nil:
  43. case kind
  44. of tyVoid: result = sysTypeFromName("void")
  45. of tyInt: result = sysTypeFromName("int")
  46. of tyInt8: result = sysTypeFromName("int8")
  47. of tyInt16: result = sysTypeFromName("int16")
  48. of tyInt32: result = sysTypeFromName("int32")
  49. of tyInt64: result = sysTypeFromName("int64")
  50. of tyUInt: result = sysTypeFromName("uint")
  51. of tyUInt8: result = sysTypeFromName("uint8")
  52. of tyUInt16: result = sysTypeFromName("uint16")
  53. of tyUInt32: result = sysTypeFromName("uint32")
  54. of tyUInt64: result = sysTypeFromName("uint64")
  55. of tyFloat: result = sysTypeFromName("float")
  56. of tyFloat32: result = sysTypeFromName("float32")
  57. of tyFloat64: result = sysTypeFromName("float64")
  58. of tyFloat128: result = sysTypeFromName("float128")
  59. of tyBool: result = sysTypeFromName("bool")
  60. of tyChar: result = sysTypeFromName("char")
  61. of tyString: result = sysTypeFromName("string")
  62. of tyCstring: result = sysTypeFromName("cstring")
  63. of tyPointer: result = sysTypeFromName("pointer")
  64. of tyNil: result = newSysType(g, tyNil, g.config.target.ptrSize)
  65. else: internalError(g.config, "request for typekind: " & $kind)
  66. g.sysTypes[kind] = result
  67. if result.kind != kind:
  68. if kind == tyFloat64 and result.kind == tyFloat: discard # because of aliasing
  69. else:
  70. internalError(g.config, "wanted: " & $kind & " got: " & $result.kind)
  71. if result == nil: internalError(g.config, "type not found: " & $kind)
  72. proc resetSysTypes*(g: ModuleGraph) =
  73. g.systemModule = nil
  74. initStrTable(g.compilerprocs)
  75. initStrTable(g.exposed)
  76. for i in low(g.sysTypes)..high(g.sysTypes):
  77. g.sysTypes[i] = nil
  78. proc getFloatLitType*(g: ModuleGraph; literal: PNode): PType =
  79. # for now we do not cache these:
  80. result = newSysType(g, tyFloat, size=8)
  81. result.n = literal
  82. proc skipIntLit*(t: PType; id: IdGenerator): PType {.inline.} =
  83. if t.n != nil and t.kind in {tyInt, tyFloat}:
  84. result = copyType(t, nextTypeId(id), t.owner)
  85. result.n = nil
  86. else:
  87. result = t
  88. proc addSonSkipIntLit*(father, son: PType; id: IdGenerator) =
  89. let s = son.skipIntLit(id)
  90. father.sons.add(s)
  91. propagateToOwner(father, s)
  92. proc getCompilerProc*(g: ModuleGraph; name: string): PSym =
  93. let ident = getIdent(g.cache, name)
  94. result = strTableGet(g.compilerprocs, ident)
  95. if result == nil:
  96. result = loadCompilerProc(g, name)
  97. proc registerCompilerProc*(g: ModuleGraph; s: PSym) =
  98. strTableAdd(g.compilerprocs, s)
  99. proc registerNimScriptSymbol*(g: ModuleGraph; s: PSym) =
  100. # Nimscript symbols must be al unique:
  101. let conflict = strTableGet(g.exposed, s.name)
  102. if conflict == nil:
  103. strTableAdd(g.exposed, s)
  104. else:
  105. localError(g.config, s.info,
  106. "symbol conflicts with other .exportNims symbol at: " & g.config$conflict.info)
  107. proc getNimScriptSymbol*(g: ModuleGraph; name: string): PSym =
  108. strTableGet(g.exposed, getIdent(g.cache, name))
  109. proc resetNimScriptSymbols*(g: ModuleGraph) = initStrTable(g.exposed)
  110. proc getMagicEqSymForType*(g: ModuleGraph; t: PType; info: TLineInfo): PSym =
  111. case t.kind
  112. of tyInt, tyInt8, tyInt16, tyInt32, tyInt64,
  113. tyUInt, tyUInt8, tyUInt16, tyUInt32, tyUInt64:
  114. result = getSysMagic(g, info, "==", mEqI)
  115. of tyEnum:
  116. result = getSysMagic(g, info, "==", mEqEnum)
  117. of tyBool:
  118. result = getSysMagic(g, info, "==", mEqB)
  119. of tyRef, tyPtr, tyPointer:
  120. result = getSysMagic(g, info, "==", mEqRef)
  121. of tyString:
  122. result = getSysMagic(g, info, "==", mEqStr)
  123. of tyChar:
  124. result = getSysMagic(g, info, "==", mEqCh)
  125. of tySet:
  126. result = getSysMagic(g, info, "==", mEqSet)
  127. of tyProc:
  128. result = getSysMagic(g, info, "==", mEqProc)
  129. else:
  130. globalError(g.config, info,
  131. "can't find magic equals operator for type kind " & $t.kind)