condsyms.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This module handles the conditional symbols.
  10. import
  11. strtabs, platform, strutils, idents
  12. # We need to use a StringTableRef here as defined symbols are always guaranteed
  13. # to be style insensitive. Otherwise hell would break lose.
  14. var gSymbols: StringTableRef
  15. const
  16. catNone = "false"
  17. proc defineSymbol*(symbol: string, value: string = "true") =
  18. gSymbols[symbol] = value
  19. proc undefSymbol*(symbol: string) =
  20. gSymbols[symbol] = catNone
  21. proc isDefined*(symbol: string): bool =
  22. if gSymbols.hasKey(symbol):
  23. result = gSymbols[symbol] != catNone
  24. elif cmpIgnoreStyle(symbol, CPU[targetCPU].name) == 0:
  25. result = true
  26. elif cmpIgnoreStyle(symbol, platform.OS[targetOS].name) == 0:
  27. result = true
  28. else:
  29. case symbol.normalize
  30. of "x86": result = targetCPU == cpuI386
  31. of "itanium": result = targetCPU == cpuIa64
  32. of "x8664": result = targetCPU == cpuAmd64
  33. of "posix", "unix":
  34. result = targetOS in {osLinux, osMorphos, osSkyos, osIrix, osPalmos,
  35. osQnx, osAtari, osAix,
  36. osHaiku, osVxWorks, osSolaris, osNetbsd,
  37. osFreebsd, osOpenbsd, osDragonfly, osMacosx,
  38. osAndroid}
  39. of "linux":
  40. result = targetOS in {osLinux, osAndroid}
  41. of "bsd":
  42. result = targetOS in {osNetbsd, osFreebsd, osOpenbsd, osDragonfly}
  43. of "emulatedthreadvars":
  44. result = platform.OS[targetOS].props.contains(ospLacksThreadVars)
  45. of "msdos": result = targetOS == osDos
  46. of "mswindows", "win32": result = targetOS == osWindows
  47. of "macintosh": result = targetOS in {osMacos, osMacosx}
  48. of "sunos": result = targetOS == osSolaris
  49. of "littleendian": result = CPU[targetCPU].endian == platform.littleEndian
  50. of "bigendian": result = CPU[targetCPU].endian == platform.bigEndian
  51. of "cpu8": result = CPU[targetCPU].bit == 8
  52. of "cpu16": result = CPU[targetCPU].bit == 16
  53. of "cpu32": result = CPU[targetCPU].bit == 32
  54. of "cpu64": result = CPU[targetCPU].bit == 64
  55. of "nimrawsetjmp":
  56. result = targetOS in {osSolaris, osNetbsd, osFreebsd, osOpenbsd,
  57. osDragonfly, osMacosx}
  58. else: discard
  59. proc isDefined*(symbol: PIdent): bool = isDefined(symbol.s)
  60. proc lookupSymbol*(symbol: string): string =
  61. result = if isDefined(symbol): gSymbols[symbol] else: nil
  62. proc lookupSymbol*(symbol: PIdent): string = lookupSymbol(symbol.s)
  63. iterator definedSymbolNames*: string =
  64. for key, val in pairs(gSymbols):
  65. if val != catNone: yield key
  66. proc countDefinedSymbols*(): int =
  67. result = 0
  68. for key, val in pairs(gSymbols):
  69. if val != catNone: inc(result)
  70. proc initDefines*() =
  71. gSymbols = newStringTable(modeStyleInsensitive)
  72. defineSymbol("nimrod") # 'nimrod' is always defined
  73. # for bootstrapping purposes and old code:
  74. defineSymbol("nimhygiene")
  75. defineSymbol("niminheritable")
  76. defineSymbol("nimmixin")
  77. defineSymbol("nimeffects")
  78. defineSymbol("nimbabel")
  79. defineSymbol("nimcomputedgoto")
  80. defineSymbol("nimunion")
  81. defineSymbol("nimnewshared")
  82. defineSymbol("nimrequiresnimframe")
  83. defineSymbol("nimparsebiggestfloatmagic")
  84. defineSymbol("nimalias")
  85. defineSymbol("nimlocks")
  86. defineSymbol("nimnode")
  87. defineSymbol("nimnomagic64")
  88. defineSymbol("nimvarargstyped")
  89. defineSymbol("nimtypedescfixed")
  90. defineSymbol("nimKnowsNimvm")
  91. defineSymbol("nimArrIdx")
  92. defineSymbol("nimImmediateDeprecated")
  93. defineSymbol("nimNewShiftOps")
  94. defineSymbol("nimDistros")
  95. defineSymbol("nimHasCppDefine")
  96. defineSymbol("nimGenericInOutFlags")
  97. when false: defineSymbol("nimHasOpt")
  98. defineSymbol("nimNoArrayToCstringConversion")