magicsys.nim 5.4 KB

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