ast.nim 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  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. # abstract syntax tree + symbol table
  10. import
  11. lineinfos, options, ropes, idents, int128, wordrecg
  12. import std/[tables, hashes]
  13. from std/strutils import toLowerAscii
  14. when defined(nimPreviewSlimSystem):
  15. import std/assertions
  16. export int128
  17. import nodekinds
  18. export nodekinds
  19. type
  20. TCallingConvention* = enum
  21. ccNimCall = "nimcall" # nimcall, also the default
  22. ccStdCall = "stdcall" # procedure is stdcall
  23. ccCDecl = "cdecl" # cdecl
  24. ccSafeCall = "safecall" # safecall
  25. ccSysCall = "syscall" # system call
  26. ccInline = "inline" # proc should be inlined
  27. ccNoInline = "noinline" # proc should not be inlined
  28. ccFastCall = "fastcall" # fastcall (pass parameters in registers)
  29. ccThisCall = "thiscall" # thiscall (parameters are pushed right-to-left)
  30. ccClosure = "closure" # proc has a closure
  31. ccNoConvention = "noconv" # needed for generating proper C procs sometimes
  32. ccMember = "member" # proc is a (cpp) member
  33. TNodeKinds* = set[TNodeKind]
  34. type
  35. TSymFlag* = enum # 51 flags!
  36. sfUsed, # read access of sym (for warnings) or simply used
  37. sfExported, # symbol is exported from module
  38. sfFromGeneric, # symbol is instantiation of a generic; this is needed
  39. # for symbol file generation; such symbols should always
  40. # be written into the ROD file
  41. sfGlobal, # symbol is at global scope
  42. sfForward, # symbol is forward declared
  43. sfWasForwarded, # symbol had a forward declaration
  44. # (implies it's too dangerous to patch its type signature)
  45. sfImportc, # symbol is external; imported
  46. sfExportc, # symbol is exported (under a specified name)
  47. sfMangleCpp, # mangle as cpp (combines with `sfExportc`)
  48. sfVolatile, # variable is volatile
  49. sfRegister, # variable should be placed in a register
  50. sfPure, # object is "pure" that means it has no type-information
  51. # enum is "pure", its values need qualified access
  52. # variable is "pure"; it's an explicit "global"
  53. sfNoSideEffect, # proc has no side effects
  54. sfSideEffect, # proc may have side effects; cannot prove it has none
  55. sfMainModule, # module is the main module
  56. sfSystemModule, # module is the system module
  57. sfNoReturn, # proc never returns (an exit proc)
  58. sfAddrTaken, # the variable's address is taken (ex- or implicitly);
  59. # *OR*: a proc is indirectly called (used as first class)
  60. sfCompilerProc, # proc is a compiler proc, that is a C proc that is
  61. # needed for the code generator
  62. sfEscapes # param escapes
  63. # currently unimplemented
  64. sfDiscriminant, # field is a discriminant in a record/object
  65. sfRequiresInit, # field must be initialized during construction
  66. sfDeprecated, # symbol is deprecated
  67. sfExplain, # provide more diagnostics when this symbol is used
  68. sfError, # usage of symbol should trigger a compile-time error
  69. sfShadowed, # a symbol that was shadowed in some inner scope
  70. sfThread, # proc will run as a thread
  71. # variable is a thread variable
  72. sfCppNonPod, # tells compiler to treat such types as non-pod's, so that
  73. # `thread_local` is used instead of `__thread` for
  74. # {.threadvar.} + `--threads`. Only makes sense for importcpp types.
  75. # This has a performance impact so isn't set by default.
  76. sfCompileTime, # proc can be evaluated at compile time
  77. sfConstructor, # proc is a C++ constructor
  78. sfDispatcher, # copied method symbol is the dispatcher
  79. # deprecated and unused, except for the con
  80. sfBorrow, # proc is borrowed
  81. sfInfixCall, # symbol needs infix call syntax in target language;
  82. # for interfacing with C++, JS
  83. sfNamedParamCall, # symbol needs named parameter call syntax in target
  84. # language; for interfacing with Objective C
  85. sfDiscardable, # returned value may be discarded implicitly
  86. sfOverridden, # proc is overridden
  87. sfCallsite # A flag for template symbols to tell the
  88. # compiler it should use line information from
  89. # the calling side of the macro, not from the
  90. # implementation.
  91. sfGenSym # symbol is 'gensym'ed; do not add to symbol table
  92. sfNonReloadable # symbol will be left as-is when hot code reloading is on -
  93. # meaning that it won't be renamed and/or changed in any way
  94. sfGeneratedOp # proc is a generated '='; do not inject destructors in it
  95. # variable is generated closure environment; requires early
  96. # destruction for --newruntime.
  97. sfTemplateParam # symbol is a template parameter
  98. sfCursor # variable/field is a cursor, see RFC 177 for details
  99. sfInjectDestructors # whether the proc needs the 'injectdestructors' transformation
  100. sfNeverRaises # proc can never raise an exception, not even OverflowDefect
  101. # or out-of-memory
  102. sfSystemRaisesDefect # proc in the system can raise defects
  103. sfUsedInFinallyOrExcept # symbol is used inside an 'except' or 'finally'
  104. sfSingleUsedTemp # For temporaries that we know will only be used once
  105. sfNoalias # 'noalias' annotation, means C's 'restrict'
  106. # for templates and macros, means cannot be called
  107. # as a lone symbol (cannot use alias syntax)
  108. sfEffectsDelayed # an 'effectsDelayed' parameter
  109. sfGeneratedType # A anonymous generic type that is generated by the compiler for
  110. # objects that do not have generic parameters in case one of the
  111. # object fields has one.
  112. #
  113. # This is disallowed but can cause the typechecking to go into
  114. # an infinite loop, this flag is used as a sentinel to stop it.
  115. sfVirtual # proc is a C++ virtual function
  116. sfByCopy # param is marked as pass bycopy
  117. sfMember # proc is a C++ member of a type
  118. sfCodegenDecl # type, proc, global or proc param is marked as codegenDecl
  119. TSymFlags* = set[TSymFlag]
  120. const
  121. sfNoInit* = sfMainModule # don't generate code to init the variable
  122. sfAllUntyped* = sfVolatile # macro or template is immediately expanded \
  123. # in a generic context
  124. sfDirty* = sfPure
  125. # template is not hygienic (old styled template)
  126. # module, compiled from a dirty-buffer
  127. sfAnon* = sfDiscardable
  128. # symbol name that was generated by the compiler
  129. # the compiler will avoid printing such names
  130. # in user messages.
  131. sfNoForward* = sfRegister
  132. # forward declarations are not required (per module)
  133. sfReorder* = sfForward
  134. # reordering pass is enabled
  135. sfCompileToCpp* = sfInfixCall # compile the module as C++ code
  136. sfCompileToObjc* = sfNamedParamCall # compile the module as Objective-C code
  137. sfExperimental* = sfOverridden # module uses the .experimental switch
  138. sfGoto* = sfOverridden # var is used for 'goto' code generation
  139. sfWrittenTo* = sfBorrow # param is assigned to
  140. # currently unimplemented
  141. sfBase* = sfDiscriminant
  142. sfCustomPragma* = sfRegister # symbol is custom pragma template
  143. sfTemplateRedefinition* = sfExportc # symbol is a redefinition of an earlier template
  144. sfCppMember* = { sfVirtual, sfMember, sfConstructor } # proc is a C++ member, meaning it will be attached to the type definition
  145. const
  146. # getting ready for the future expr/stmt merge
  147. nkWhen* = nkWhenStmt
  148. nkWhenExpr* = nkWhenStmt
  149. nkEffectList* = nkArgList
  150. # hacks ahead: an nkEffectList is a node with 4 children:
  151. exceptionEffects* = 0 # exceptions at position 0
  152. requiresEffects* = 1 # 'requires' annotation
  153. ensuresEffects* = 2 # 'ensures' annotation
  154. tagEffects* = 3 # user defined tag ('gc', 'time' etc.)
  155. pragmasEffects* = 4 # not an effect, but a slot for pragmas in proc type
  156. forbiddenEffects* = 5 # list of illegal effects
  157. effectListLen* = 6 # list of effects list
  158. nkLastBlockStmts* = {nkRaiseStmt, nkReturnStmt, nkBreakStmt, nkContinueStmt}
  159. # these must be last statements in a block
  160. type
  161. TTypeKind* = enum # order is important!
  162. # Don't forget to change hti.nim if you make a change here
  163. # XXX put this into an include file to avoid this issue!
  164. # several types are no longer used (guess which), but a
  165. # spot in the sequence is kept for backwards compatibility
  166. # (apparently something with bootstrapping)
  167. # if you need to add a type, they can apparently be reused
  168. tyNone, tyBool, tyChar,
  169. tyEmpty, tyAlias, tyNil, tyUntyped, tyTyped, tyTypeDesc,
  170. tyGenericInvocation, # ``T[a, b]`` for types to invoke
  171. tyGenericBody, # ``T[a, b, body]`` last parameter is the body
  172. tyGenericInst, # ``T[a, b, realInstance]`` instantiated generic type
  173. # realInstance will be a concrete type like tyObject
  174. # unless this is an instance of a generic alias type.
  175. # then realInstance will be the tyGenericInst of the
  176. # completely (recursively) resolved alias.
  177. tyGenericParam, # ``a`` in the above patterns
  178. tyDistinct,
  179. tyEnum,
  180. tyOrdinal, # integer types (including enums and boolean)
  181. tyArray,
  182. tyObject,
  183. tyTuple,
  184. tySet,
  185. tyRange,
  186. tyPtr, tyRef,
  187. tyVar,
  188. tySequence,
  189. tyProc,
  190. tyPointer, tyOpenArray,
  191. tyString, tyCstring, tyForward,
  192. tyInt, tyInt8, tyInt16, tyInt32, tyInt64, # signed integers
  193. tyFloat, tyFloat32, tyFloat64, tyFloat128,
  194. tyUInt, tyUInt8, tyUInt16, tyUInt32, tyUInt64,
  195. tyOwned, tySink, tyLent,
  196. tyVarargs,
  197. tyUncheckedArray
  198. # An array with boundaries [0,+∞]
  199. tyProxy # used as errornous type (for idetools)
  200. tyBuiltInTypeClass
  201. # Type such as the catch-all object, tuple, seq, etc
  202. tyUserTypeClass
  203. # the body of a user-defined type class
  204. tyUserTypeClassInst
  205. # Instance of a parametric user-defined type class.
  206. # Structured similarly to tyGenericInst.
  207. # tyGenericInst represents concrete types, while
  208. # this is still a "generic param" that will bind types
  209. # and resolves them during sigmatch and instantiation.
  210. tyCompositeTypeClass
  211. # Type such as seq[Number]
  212. # The notes for tyUserTypeClassInst apply here as well
  213. # sons[0]: the original expression used by the user.
  214. # sons[1]: fully expanded and instantiated meta type
  215. # (potentially following aliases)
  216. tyInferred
  217. # In the initial state `base` stores a type class constraining
  218. # the types that can be inferred. After a candidate type is
  219. # selected, it's stored in `last`. Between `base` and `last`
  220. # there may be 0, 2 or more types that were also considered as
  221. # possible candidates in the inference process (i.e. last will
  222. # be updated to store a type best conforming to all candidates)
  223. tyAnd, tyOr, tyNot
  224. # boolean type classes such as `string|int`,`not seq`,
  225. # `Sortable and Enumable`, etc
  226. tyAnything
  227. # a type class matching any type
  228. tyStatic
  229. # a value known at compile type (the underlying type is .base)
  230. tyFromExpr
  231. # This is a type representing an expression that depends
  232. # on generic parameters (the expression is stored in t.n)
  233. # It will be converted to a real type only during generic
  234. # instantiation and prior to this it has the potential to
  235. # be any type.
  236. tyConcept
  237. # new style concept.
  238. tyVoid
  239. # now different from tyEmpty, hurray!
  240. tyIterable
  241. static:
  242. # remind us when TTypeKind stops to fit in a single 64-bit word
  243. # assert TTypeKind.high.ord <= 63
  244. discard
  245. const
  246. tyPureObject* = tyTuple
  247. GcTypeKinds* = {tyRef, tySequence, tyString}
  248. tyError* = tyProxy # as an errornous node should match everything
  249. tyUnknown* = tyFromExpr
  250. tyUnknownTypes* = {tyError, tyFromExpr}
  251. tyTypeClasses* = {tyBuiltInTypeClass, tyCompositeTypeClass,
  252. tyUserTypeClass, tyUserTypeClassInst,
  253. tyAnd, tyOr, tyNot, tyAnything}
  254. tyMetaTypes* = {tyGenericParam, tyTypeDesc, tyUntyped} + tyTypeClasses
  255. tyUserTypeClasses* = {tyUserTypeClass, tyUserTypeClassInst}
  256. # consider renaming as `tyAbstractVarRange`
  257. abstractVarRange* = {tyGenericInst, tyRange, tyVar, tyDistinct, tyOrdinal,
  258. tyTypeDesc, tyAlias, tyInferred, tySink, tyOwned}
  259. abstractInst* = {tyGenericInst, tyDistinct, tyOrdinal, tyTypeDesc, tyAlias,
  260. tyInferred, tySink, tyOwned} # xxx what about tyStatic?
  261. type
  262. TTypeKinds* = set[TTypeKind]
  263. TNodeFlag* = enum
  264. nfNone,
  265. nfBase2, # nfBase10 is default, so not needed
  266. nfBase8,
  267. nfBase16,
  268. nfAllConst, # used to mark complex expressions constant; easy to get rid of
  269. # but unfortunately it has measurable impact for compilation
  270. # efficiency
  271. nfTransf, # node has been transformed
  272. nfNoRewrite # node should not be transformed anymore
  273. nfSem # node has been checked for semantics
  274. nfLL # node has gone through lambda lifting
  275. nfDotField # the call can use a dot operator
  276. nfDotSetter # the call can use a setter dot operarator
  277. nfExplicitCall # x.y() was used instead of x.y
  278. nfExprCall # this is an attempt to call a regular expression
  279. nfIsRef # this node is a 'ref' node; used for the VM
  280. nfIsPtr # this node is a 'ptr' node; used for the VM
  281. nfPreventCg # this node should be ignored by the codegen
  282. nfBlockArg # this a stmtlist appearing in a call (e.g. a do block)
  283. nfFromTemplate # a top-level node returned from a template
  284. nfDefaultParam # an automatically inserter default parameter
  285. nfDefaultRefsParam # a default param value references another parameter
  286. # the flag is applied to proc default values and to calls
  287. nfExecuteOnReload # A top-level statement that will be executed during reloads
  288. nfLastRead # this node is a last read
  289. nfFirstWrite # this node is a first write
  290. nfHasComment # node has a comment
  291. nfSkipFieldChecking # node skips field visable checking
  292. nfOpenSym # node is a captured sym but can be overriden by local symbols
  293. TNodeFlags* = set[TNodeFlag]
  294. TTypeFlag* = enum # keep below 32 for efficiency reasons (now: 47)
  295. tfVarargs, # procedure has C styled varargs
  296. # tyArray type represeting a varargs list
  297. tfNoSideEffect, # procedure type does not allow side effects
  298. tfFinal, # is the object final?
  299. tfInheritable, # is the object inheritable?
  300. tfHasOwned, # type contains an 'owned' type and must be moved
  301. tfEnumHasHoles, # enum cannot be mapped into a range
  302. tfShallow, # type can be shallow copied on assignment
  303. tfThread, # proc type is marked as ``thread``; alias for ``gcsafe``
  304. tfFromGeneric, # type is an instantiation of a generic; this is needed
  305. # because for instantiations of objects, structural
  306. # type equality has to be used
  307. tfUnresolved, # marks unresolved typedesc/static params: e.g.
  308. # proc foo(T: typedesc, list: seq[T]): var T
  309. # proc foo(L: static[int]): array[L, int]
  310. # can be attached to ranges to indicate that the range
  311. # can be attached to generic procs with free standing
  312. # type parameters: e.g. proc foo[T]()
  313. # depends on unresolved static params.
  314. tfResolved # marks a user type class, after it has been bound to a
  315. # concrete type (lastSon becomes the concrete type)
  316. tfRetType, # marks return types in proc (used to detect type classes
  317. # used as return types for return type inference)
  318. tfCapturesEnv, # whether proc really captures some environment
  319. tfByCopy, # pass object/tuple by copy (C backend)
  320. tfByRef, # pass object/tuple by reference (C backend)
  321. tfIterator, # type is really an iterator, not a tyProc
  322. tfPartial, # type is declared as 'partial'
  323. tfNotNil, # type cannot be 'nil'
  324. tfRequiresInit, # type contains a "not nil" constraint somewhere or
  325. # a `requiresInit` field, so the default zero init
  326. # is not appropriate
  327. tfNeedsFullInit, # object type marked with {.requiresInit.}
  328. # all fields must be initialized
  329. tfVarIsPtr, # 'var' type is translated like 'ptr' even in C++ mode
  330. tfHasMeta, # type contains "wildcard" sub-types such as generic params
  331. # or other type classes
  332. tfHasGCedMem, # type contains GC'ed memory
  333. tfPacked
  334. tfHasStatic
  335. tfGenericTypeParam
  336. tfImplicitTypeParam
  337. tfInferrableStatic
  338. tfConceptMatchedTypeSym
  339. tfExplicit # for typedescs, marks types explicitly prefixed with the
  340. # `type` operator (e.g. type int)
  341. tfWildcard # consider a proc like foo[T, I](x: Type[T, I])
  342. # T and I here can bind to both typedesc and static types
  343. # before this is determined, we'll consider them to be a
  344. # wildcard type.
  345. tfHasAsgn # type has overloaded assignment operator
  346. tfBorrowDot # distinct type borrows '.'
  347. tfTriggersCompileTime # uses the NimNode type which make the proc
  348. # implicitly '.compiletime'
  349. tfRefsAnonObj # used for 'ref object' and 'ptr object'
  350. tfCovariant # covariant generic param mimicking a ptr type
  351. tfWeakCovariant # covariant generic param mimicking a seq/array type
  352. tfContravariant # contravariant generic param
  353. tfCheckedForDestructor # type was checked for having a destructor.
  354. # If it has one, t.destructor is not nil.
  355. tfAcyclic # object type was annotated as .acyclic
  356. tfIncompleteStruct # treat this type as if it had sizeof(pointer)
  357. tfCompleteStruct
  358. # (for importc types); type is fully specified, allowing to compute
  359. # sizeof, alignof, offsetof at CT
  360. tfExplicitCallConv
  361. tfIsConstructor
  362. tfEffectSystemWorkaround
  363. tfIsOutParam
  364. tfSendable
  365. tfImplicitStatic
  366. TTypeFlags* = set[TTypeFlag]
  367. TSymKind* = enum # the different symbols (start with the prefix sk);
  368. # order is important for the documentation generator!
  369. skUnknown, # unknown symbol: used for parsing assembler blocks
  370. # and first phase symbol lookup in generics
  371. skConditional, # symbol for the preprocessor (may become obsolete)
  372. skDynLib, # symbol represents a dynamic library; this is used
  373. # internally; it does not exist in Nim code
  374. skParam, # a parameter
  375. skGenericParam, # a generic parameter; eq in ``proc x[eq=`==`]()``
  376. skTemp, # a temporary variable (introduced by compiler)
  377. skModule, # module identifier
  378. skType, # a type
  379. skVar, # a variable
  380. skLet, # a 'let' symbol
  381. skConst, # a constant
  382. skResult, # special 'result' variable
  383. skProc, # a proc
  384. skFunc, # a func
  385. skMethod, # a method
  386. skIterator, # an iterator
  387. skConverter, # a type converter
  388. skMacro, # a macro
  389. skTemplate, # a template; currently also misused for user-defined
  390. # pragmas
  391. skField, # a field in a record or object
  392. skEnumField, # an identifier in an enum
  393. skForVar, # a for loop variable
  394. skLabel, # a label (for block statement)
  395. skStub, # symbol is a stub and not yet loaded from the ROD
  396. # file (it is loaded on demand, which may
  397. # mean: never)
  398. skPackage, # symbol is a package (used for canonicalization)
  399. TSymKinds* = set[TSymKind]
  400. const
  401. routineKinds* = {skProc, skFunc, skMethod, skIterator,
  402. skConverter, skMacro, skTemplate}
  403. ExportableSymKinds* = {skVar, skLet, skConst, skType, skEnumField, skStub} + routineKinds
  404. tfUnion* = tfNoSideEffect
  405. tfGcSafe* = tfThread
  406. tfObjHasKids* = tfEnumHasHoles
  407. tfReturnsNew* = tfInheritable
  408. skError* = skUnknown
  409. var
  410. eqTypeFlags* = {tfIterator, tfNotNil, tfVarIsPtr, tfGcSafe, tfNoSideEffect, tfIsOutParam}
  411. ## type flags that are essential for type equality.
  412. ## This is now a variable because for emulation of version:1.0 we
  413. ## might exclude {tfGcSafe, tfNoSideEffect}.
  414. type
  415. TMagic* = enum # symbols that require compiler magic:
  416. mNone,
  417. mDefined, mDeclared, mDeclaredInScope, mCompiles, mArrGet, mArrPut, mAsgn,
  418. mLow, mHigh, mSizeOf, mAlignOf, mOffsetOf, mTypeTrait,
  419. mIs, mOf, mAddr, mType, mTypeOf,
  420. mPlugin, mEcho, mShallowCopy, mSlurp, mStaticExec, mStatic,
  421. mParseExprToAst, mParseStmtToAst, mExpandToAst, mQuoteAst,
  422. mInc, mDec, mOrd,
  423. mNew, mNewFinalize, mNewSeq, mNewSeqOfCap,
  424. mLengthOpenArray, mLengthStr, mLengthArray, mLengthSeq,
  425. mIncl, mExcl, mCard, mChr,
  426. mGCref, mGCunref,
  427. mAddI, mSubI, mMulI, mDivI, mModI,
  428. mSucc, mPred,
  429. mAddF64, mSubF64, mMulF64, mDivF64,
  430. mShrI, mShlI, mAshrI, mBitandI, mBitorI, mBitxorI,
  431. mMinI, mMaxI,
  432. mAddU, mSubU, mMulU, mDivU, mModU,
  433. mEqI, mLeI, mLtI,
  434. mEqF64, mLeF64, mLtF64,
  435. mLeU, mLtU,
  436. mEqEnum, mLeEnum, mLtEnum,
  437. mEqCh, mLeCh, mLtCh,
  438. mEqB, mLeB, mLtB,
  439. mEqRef, mLePtr, mLtPtr,
  440. mXor, mEqCString, mEqProc,
  441. mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot,
  442. mUnaryPlusI, mBitnotI,
  443. mUnaryPlusF64, mUnaryMinusF64,
  444. mCharToStr, mBoolToStr,
  445. mCStrToStr,
  446. mStrToStr, mEnumToStr,
  447. mAnd, mOr,
  448. mImplies, mIff, mExists, mForall, mOld,
  449. mEqStr, mLeStr, mLtStr,
  450. mEqSet, mLeSet, mLtSet, mMulSet, mPlusSet, mMinusSet,
  451. mConStrStr, mSlice,
  452. mDotDot, # this one is only necessary to give nice compile time warnings
  453. mFields, mFieldPairs, mOmpParFor,
  454. mAppendStrCh, mAppendStrStr, mAppendSeqElem,
  455. mInSet, mRepr, mExit,
  456. mSetLengthStr, mSetLengthSeq,
  457. mIsPartOf, mAstToStr, mParallel,
  458. mSwap, mIsNil, mArrToSeq, mOpenArrayToSeq,
  459. mNewString, mNewStringOfCap, mParseBiggestFloat,
  460. mMove, mEnsureMove, mWasMoved, mDup, mDestroy, mTrace,
  461. mDefault, mUnown, mFinished, mIsolate, mAccessEnv, mAccessTypeField,
  462. mArray, mOpenArray, mRange, mSet, mSeq, mVarargs,
  463. mRef, mPtr, mVar, mDistinct, mVoid, mTuple,
  464. mOrdinal, mIterableType,
  465. mInt, mInt8, mInt16, mInt32, mInt64,
  466. mUInt, mUInt8, mUInt16, mUInt32, mUInt64,
  467. mFloat, mFloat32, mFloat64, mFloat128,
  468. mBool, mChar, mString, mCstring,
  469. mPointer, mNil, mExpr, mStmt, mTypeDesc,
  470. mVoidType, mPNimrodNode, mSpawn, mDeepCopy,
  471. mIsMainModule, mCompileDate, mCompileTime, mProcCall,
  472. mCpuEndian, mHostOS, mHostCPU, mBuildOS, mBuildCPU, mAppType,
  473. mCompileOption, mCompileOptionArg,
  474. mNLen, mNChild, mNSetChild, mNAdd, mNAddMultiple, mNDel,
  475. mNKind, mNSymKind,
  476. mNccValue, mNccInc, mNcsAdd, mNcsIncl, mNcsLen, mNcsAt,
  477. mNctPut, mNctLen, mNctGet, mNctHasNext, mNctNext,
  478. mNIntVal, mNFloatVal, mNSymbol, mNIdent, mNGetType, mNStrVal, mNSetIntVal,
  479. mNSetFloatVal, mNSetSymbol, mNSetIdent, mNSetStrVal, mNLineInfo,
  480. mNNewNimNode, mNCopyNimNode, mNCopyNimTree, mStrToIdent, mNSigHash, mNSizeOf,
  481. mNBindSym, mNCallSite,
  482. mEqIdent, mEqNimrodNode, mSameNodeType, mGetImpl, mNGenSym,
  483. mNHint, mNWarning, mNError,
  484. mInstantiationInfo, mGetTypeInfo, mGetTypeInfoV2,
  485. mNimvm, mIntDefine, mStrDefine, mBoolDefine, mGenericDefine, mRunnableExamples,
  486. mException, mBuiltinType, mSymOwner, mUncheckedArray, mGetImplTransf,
  487. mSymIsInstantiationOf, mNodeId, mPrivateAccess, mZeroDefault
  488. const
  489. # things that we can evaluate safely at compile time, even if not asked for it:
  490. ctfeWhitelist* = {mNone, mSucc,
  491. mPred, mInc, mDec, mOrd, mLengthOpenArray,
  492. mLengthStr, mLengthArray, mLengthSeq,
  493. mArrGet, mArrPut, mAsgn, mDestroy,
  494. mIncl, mExcl, mCard, mChr,
  495. mAddI, mSubI, mMulI, mDivI, mModI,
  496. mAddF64, mSubF64, mMulF64, mDivF64,
  497. mShrI, mShlI, mBitandI, mBitorI, mBitxorI,
  498. mMinI, mMaxI,
  499. mAddU, mSubU, mMulU, mDivU, mModU,
  500. mEqI, mLeI, mLtI,
  501. mEqF64, mLeF64, mLtF64,
  502. mLeU, mLtU,
  503. mEqEnum, mLeEnum, mLtEnum,
  504. mEqCh, mLeCh, mLtCh,
  505. mEqB, mLeB, mLtB,
  506. mEqRef, mEqProc, mLePtr, mLtPtr, mEqCString, mXor,
  507. mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot, mUnaryPlusI, mBitnotI,
  508. mUnaryPlusF64, mUnaryMinusF64,
  509. mCharToStr, mBoolToStr,
  510. mCStrToStr,
  511. mStrToStr, mEnumToStr,
  512. mAnd, mOr,
  513. mEqStr, mLeStr, mLtStr,
  514. mEqSet, mLeSet, mLtSet, mMulSet, mPlusSet, mMinusSet,
  515. mConStrStr, mAppendStrCh, mAppendStrStr, mAppendSeqElem,
  516. mInSet, mRepr, mOpenArrayToSeq}
  517. generatedMagics* = {mNone, mIsolate, mFinished, mOpenArrayToSeq}
  518. ## magics that are generated as normal procs in the backend
  519. type
  520. ItemId* = object
  521. module*: int32
  522. item*: int32
  523. proc `$`*(x: ItemId): string =
  524. "(module: " & $x.module & ", item: " & $x.item & ")"
  525. proc `==`*(a, b: ItemId): bool {.inline.} =
  526. a.item == b.item and a.module == b.module
  527. proc hash*(x: ItemId): Hash =
  528. var h: Hash = hash(x.module)
  529. h = h !& hash(x.item)
  530. result = !$h
  531. type
  532. PNode* = ref TNode
  533. TNodeSeq* = seq[PNode]
  534. PType* = ref TType
  535. PSym* = ref TSym
  536. TNode*{.final, acyclic.} = object # on a 32bit machine, this takes 32 bytes
  537. when defined(useNodeIds):
  538. id*: int
  539. typ*: PType
  540. info*: TLineInfo
  541. flags*: TNodeFlags
  542. case kind*: TNodeKind
  543. of nkCharLit..nkUInt64Lit:
  544. intVal*: BiggestInt
  545. of nkFloatLit..nkFloat128Lit:
  546. floatVal*: BiggestFloat
  547. of nkStrLit..nkTripleStrLit:
  548. strVal*: string
  549. of nkSym:
  550. sym*: PSym
  551. of nkIdent:
  552. ident*: PIdent
  553. else:
  554. sons*: TNodeSeq
  555. when defined(nimsuggest):
  556. endInfo*: TLineInfo
  557. TStrTable* = object # a table[PIdent] of PSym
  558. counter*: int
  559. data*: seq[PSym]
  560. # -------------- backend information -------------------------------
  561. TLocKind* = enum
  562. locNone, # no location
  563. locTemp, # temporary location
  564. locLocalVar, # location is a local variable
  565. locGlobalVar, # location is a global variable
  566. locParam, # location is a parameter
  567. locField, # location is a record field
  568. locExpr, # "location" is really an expression
  569. locProc, # location is a proc (an address of a procedure)
  570. locData, # location is a constant
  571. locCall, # location is a call expression
  572. locOther # location is something other
  573. TLocFlag* = enum
  574. lfIndirect, # backend introduced a pointer
  575. lfNoDeepCopy, # no need for a deep copy
  576. lfNoDecl, # do not declare it in C
  577. lfDynamicLib, # link symbol to dynamic library
  578. lfExportLib, # export symbol for dynamic library generation
  579. lfHeader, # include header file for symbol
  580. lfImportCompilerProc, # ``importc`` of a compilerproc
  581. lfSingleUse # no location yet and will only be used once
  582. lfEnforceDeref # a copyMem is required to dereference if this a
  583. # ptr array due to C array limitations.
  584. # See #1181, #6422, #11171
  585. lfPrepareForMutation # string location is about to be mutated (V2)
  586. TStorageLoc* = enum
  587. OnUnknown, # location is unknown (stack, heap or static)
  588. OnStatic, # in a static section
  589. OnStack, # location is on hardware stack
  590. OnHeap # location is on heap or global
  591. # (reference counting needed)
  592. TLocFlags* = set[TLocFlag]
  593. TLoc* = object
  594. k*: TLocKind # kind of location
  595. storage*: TStorageLoc
  596. flags*: TLocFlags # location's flags
  597. lode*: PNode # Node where the location came from; can be faked
  598. r*: Rope # rope value of location (code generators)
  599. # ---------------- end of backend information ------------------------------
  600. TLibKind* = enum
  601. libHeader, libDynamic
  602. TLib* = object # also misused for headers!
  603. # keep in sync with PackedLib
  604. kind*: TLibKind
  605. generated*: bool # needed for the backends:
  606. isOverridden*: bool
  607. name*: Rope
  608. path*: PNode # can be a string literal!
  609. CompilesId* = int ## id that is used for the caching logic within
  610. ## ``system.compiles``. See the seminst module.
  611. TInstantiation* = object
  612. sym*: PSym
  613. concreteTypes*: seq[PType]
  614. compilesId*: CompilesId
  615. PInstantiation* = ref TInstantiation
  616. TScope* {.acyclic.} = object
  617. depthLevel*: int
  618. symbols*: TStrTable
  619. parent*: PScope
  620. allowPrivateAccess*: seq[PSym] # # enable access to private fields
  621. PScope* = ref TScope
  622. PLib* = ref TLib
  623. TSym* {.acyclic.} = object # Keep in sync with PackedSym
  624. itemId*: ItemId
  625. # proc and type instantiations are cached in the generic symbol
  626. case kind*: TSymKind
  627. of routineKinds:
  628. #procInstCache*: seq[PInstantiation]
  629. gcUnsafetyReason*: PSym # for better error messages regarding gcsafe
  630. transformedBody*: PNode # cached body after transf pass
  631. of skLet, skVar, skField, skForVar:
  632. guard*: PSym
  633. bitsize*: int
  634. alignment*: int # for alignment
  635. else: nil
  636. magic*: TMagic
  637. typ*: PType
  638. name*: PIdent
  639. info*: TLineInfo
  640. when defined(nimsuggest):
  641. endInfo*: TLineInfo
  642. hasUserSpecifiedType*: bool # used for determining whether to display inlay type hints
  643. owner*: PSym
  644. flags*: TSymFlags
  645. ast*: PNode # syntax tree of proc, iterator, etc.:
  646. # the whole proc including header; this is used
  647. # for easy generation of proper error messages
  648. # for variant record fields the discriminant
  649. # expression
  650. # for modules, it's a placeholder for compiler
  651. # generated code that will be appended to the
  652. # module after the sem pass (see appendToModule)
  653. options*: TOptions
  654. position*: int # used for many different things:
  655. # for enum fields its position;
  656. # for fields its offset
  657. # for parameters its position (starting with 0)
  658. # for a conditional:
  659. # 1 iff the symbol is defined, else 0
  660. # (or not in symbol table)
  661. # for modules, an unique index corresponding
  662. # to the module's fileIdx
  663. # for variables a slot index for the evaluator
  664. offset*: int32 # offset of record field
  665. disamb*: int32 # disambiguation number; the basic idea is that
  666. # `<procname>__<module>_<disamb>` is unique
  667. loc*: TLoc
  668. annex*: PLib # additional fields (seldom used, so we use a
  669. # reference to another object to save space)
  670. when hasFFI:
  671. cname*: string # resolved C declaration name in importc decl, e.g.:
  672. # proc fun() {.importc: "$1aux".} => cname = funaux
  673. constraint*: PNode # additional constraints like 'lit|result'; also
  674. # misused for the codegenDecl and virtual pragmas in the hope
  675. # it won't cause problems
  676. # for skModule the string literal to output for
  677. # deprecated modules.
  678. instantiatedFrom*: PSym # for instances, the generic symbol where it came from.
  679. when defined(nimsuggest):
  680. allUsages*: seq[TLineInfo]
  681. TTypeSeq* = seq[PType]
  682. TTypeAttachedOp* = enum ## as usual, order is important here
  683. attachedWasMoved,
  684. attachedDestructor,
  685. attachedAsgn,
  686. attachedDup,
  687. attachedSink,
  688. attachedTrace,
  689. attachedDeepCopy
  690. TType* {.acyclic.} = object # \
  691. # types are identical iff they have the
  692. # same id; there may be multiple copies of a type
  693. # in memory!
  694. # Keep in sync with PackedType
  695. itemId*: ItemId
  696. kind*: TTypeKind # kind of type
  697. callConv*: TCallingConvention # for procs
  698. flags*: TTypeFlags # flags of the type
  699. sons: TTypeSeq # base types, etc.
  700. n*: PNode # node for types:
  701. # for range types a nkRange node
  702. # for record types a nkRecord node
  703. # for enum types a list of symbols
  704. # if kind == tyInt: it is an 'int literal(x)' type
  705. # for procs and tyGenericBody, it's the
  706. # formal param list
  707. # for concepts, the concept body
  708. # else: unused
  709. owner*: PSym # the 'owner' of the type
  710. sym*: PSym # types have the sym associated with them
  711. # it is used for converting types to strings
  712. size*: BiggestInt # the size of the type in bytes
  713. # -1 means that the size is unkwown
  714. align*: int16 # the type's alignment requirements
  715. paddingAtEnd*: int16 #
  716. loc*: TLoc
  717. typeInst*: PType # for generic instantiations the tyGenericInst that led to this
  718. # type.
  719. uniqueId*: ItemId # due to a design mistake, we need to keep the real ID here as it
  720. # is required by the --incremental:on mode.
  721. TPair* = object
  722. key*, val*: RootRef
  723. TPairSeq* = seq[TPair]
  724. TNodePair* = object
  725. h*: Hash # because it is expensive to compute!
  726. key*: PNode
  727. val*: int
  728. TNodePairSeq* = seq[TNodePair]
  729. TNodeTable* = object # the same as table[PNode] of int;
  730. # nodes are compared by structure!
  731. counter*: int
  732. data*: TNodePairSeq
  733. TObjectSeq* = seq[RootRef]
  734. TObjectSet* = object
  735. counter*: int
  736. data*: TObjectSeq
  737. TImplication* = enum
  738. impUnknown, impNo, impYes
  739. template nodeId(n: PNode): int = cast[int](n)
  740. type Gconfig = object
  741. # we put comments in a side channel to avoid increasing `sizeof(TNode)`, which
  742. # reduces memory usage given that `PNode` is the most allocated type by far.
  743. comments: Table[int, string] # nodeId => comment
  744. useIc*: bool
  745. var gconfig {.threadvar.}: Gconfig
  746. proc setUseIc*(useIc: bool) = gconfig.useIc = useIc
  747. proc comment*(n: PNode): string =
  748. if nfHasComment in n.flags and not gconfig.useIc:
  749. # IC doesn't track comments, see `packed_ast`, so this could fail
  750. result = gconfig.comments[n.nodeId]
  751. else:
  752. result = ""
  753. proc `comment=`*(n: PNode, a: string) =
  754. let id = n.nodeId
  755. if a.len > 0:
  756. # if needed, we could periodically cleanup gconfig.comments when its size increases,
  757. # to ensure only live nodes (and with nfHasComment) have an entry in gconfig.comments;
  758. # for compiling compiler, the waste is very small:
  759. # num calls to newNodeImpl: 14984160 (num of PNode allocations)
  760. # size of gconfig.comments: 33585
  761. # num of nodes with comments that were deleted and hence wasted: 3081
  762. n.flags.incl nfHasComment
  763. gconfig.comments[id] = a
  764. elif nfHasComment in n.flags:
  765. n.flags.excl nfHasComment
  766. gconfig.comments.del(id)
  767. # BUGFIX: a module is overloadable so that a proc can have the
  768. # same name as an imported module. This is necessary because of
  769. # the poor naming choices in the standard library.
  770. const
  771. OverloadableSyms* = {skProc, skFunc, skMethod, skIterator,
  772. skConverter, skModule, skTemplate, skMacro, skEnumField}
  773. GenericTypes*: TTypeKinds = {tyGenericInvocation, tyGenericBody,
  774. tyGenericParam}
  775. StructuralEquivTypes*: TTypeKinds = {tyNil, tyTuple, tyArray,
  776. tySet, tyRange, tyPtr, tyRef, tyVar, tyLent, tySequence, tyProc, tyOpenArray,
  777. tyVarargs}
  778. ConcreteTypes*: TTypeKinds = { # types of the expr that may occur in::
  779. # var x = expr
  780. tyBool, tyChar, tyEnum, tyArray, tyObject,
  781. tySet, tyTuple, tyRange, tyPtr, tyRef, tyVar, tyLent, tySequence, tyProc,
  782. tyPointer,
  783. tyOpenArray, tyString, tyCstring, tyInt..tyInt64, tyFloat..tyFloat128,
  784. tyUInt..tyUInt64}
  785. IntegralTypes* = {tyBool, tyChar, tyEnum, tyInt..tyInt64,
  786. tyFloat..tyFloat128, tyUInt..tyUInt64} # weird name because it contains tyFloat
  787. ConstantDataTypes*: TTypeKinds = {tyArray, tySet,
  788. tyTuple, tySequence}
  789. NilableTypes*: TTypeKinds = {tyPointer, tyCstring, tyRef, tyPtr,
  790. tyProc, tyError} # TODO
  791. PtrLikeKinds*: TTypeKinds = {tyPointer, tyPtr} # for VM
  792. PersistentNodeFlags*: TNodeFlags = {nfBase2, nfBase8, nfBase16,
  793. nfDotSetter, nfDotField,
  794. nfIsRef, nfIsPtr, nfPreventCg, nfLL,
  795. nfFromTemplate, nfDefaultRefsParam,
  796. nfExecuteOnReload, nfLastRead,
  797. nfFirstWrite, nfSkipFieldChecking,
  798. nfOpenSym}
  799. namePos* = 0
  800. patternPos* = 1 # empty except for term rewriting macros
  801. genericParamsPos* = 2
  802. paramsPos* = 3
  803. pragmasPos* = 4
  804. miscPos* = 5 # used for undocumented and hacky stuff
  805. bodyPos* = 6 # position of body; use rodread.getBody() instead!
  806. resultPos* = 7
  807. dispatcherPos* = 8
  808. nfAllFieldsSet* = nfBase2
  809. nkIdentKinds* = {nkIdent, nkSym, nkAccQuoted, nkOpenSymChoice,
  810. nkClosedSymChoice}
  811. nkPragmaCallKinds* = {nkExprColonExpr, nkCall, nkCallStrLit}
  812. nkLiterals* = {nkCharLit..nkTripleStrLit}
  813. nkFloatLiterals* = {nkFloatLit..nkFloat128Lit}
  814. nkLambdaKinds* = {nkLambda, nkDo}
  815. declarativeDefs* = {nkProcDef, nkFuncDef, nkMethodDef, nkIteratorDef, nkConverterDef}
  816. routineDefs* = declarativeDefs + {nkMacroDef, nkTemplateDef}
  817. procDefs* = nkLambdaKinds + declarativeDefs
  818. callableDefs* = nkLambdaKinds + routineDefs
  819. nkSymChoices* = {nkClosedSymChoice, nkOpenSymChoice}
  820. nkStrKinds* = {nkStrLit..nkTripleStrLit}
  821. skLocalVars* = {skVar, skLet, skForVar, skParam, skResult}
  822. skProcKinds* = {skProc, skFunc, skTemplate, skMacro, skIterator,
  823. skMethod, skConverter}
  824. defaultSize = -1
  825. defaultAlignment = -1
  826. defaultOffset* = -1
  827. proc getPIdent*(a: PNode): PIdent {.inline.} =
  828. ## Returns underlying `PIdent` for `{nkSym, nkIdent}`, or `nil`.
  829. case a.kind
  830. of nkSym: a.sym.name
  831. of nkIdent: a.ident
  832. of nkOpenSymChoice, nkClosedSymChoice: a.sons[0].sym.name
  833. else: nil
  834. const
  835. moduleShift = when defined(cpu32): 20 else: 24
  836. template id*(a: PType | PSym): int =
  837. let x = a
  838. (x.itemId.module.int shl moduleShift) + x.itemId.item.int
  839. type
  840. IdGenerator* = ref object # unfortunately, we really need the 'shared mutable' aspect here.
  841. module*: int32
  842. symId*: int32
  843. typeId*: int32
  844. sealed*: bool
  845. disambTable*: CountTable[PIdent]
  846. const
  847. PackageModuleId* = -3'i32
  848. proc idGeneratorFromModule*(m: PSym): IdGenerator =
  849. assert m.kind == skModule
  850. result = IdGenerator(module: m.itemId.module, symId: m.itemId.item, typeId: 0, disambTable: initCountTable[PIdent]())
  851. proc idGeneratorForPackage*(nextIdWillBe: int32): IdGenerator =
  852. result = IdGenerator(module: PackageModuleId, symId: nextIdWillBe - 1'i32, typeId: 0, disambTable: initCountTable[PIdent]())
  853. proc nextSymId(x: IdGenerator): ItemId {.inline.} =
  854. assert(not x.sealed)
  855. inc x.symId
  856. result = ItemId(module: x.module, item: x.symId)
  857. proc nextTypeId*(x: IdGenerator): ItemId {.inline.} =
  858. assert(not x.sealed)
  859. inc x.typeId
  860. result = ItemId(module: x.module, item: x.typeId)
  861. when false:
  862. proc nextId*(x: IdGenerator): ItemId {.inline.} =
  863. inc x.item
  864. result = x[]
  865. when false:
  866. proc storeBack*(dest: var IdGenerator; src: IdGenerator) {.inline.} =
  867. assert dest.ItemId.module == src.ItemId.module
  868. if dest.ItemId.item > src.ItemId.item:
  869. echo dest.ItemId.item, " ", src.ItemId.item, " ", src.ItemId.module
  870. assert dest.ItemId.item <= src.ItemId.item
  871. dest = src
  872. var ggDebug* {.deprecated.}: bool ## convenience switch for trying out things
  873. proc isCallExpr*(n: PNode): bool =
  874. result = n.kind in nkCallKinds
  875. proc discardSons*(father: PNode)
  876. proc len*(n: PNode): int {.inline.} =
  877. result = n.sons.len
  878. proc safeLen*(n: PNode): int {.inline.} =
  879. ## works even for leaves.
  880. if n.kind in {nkNone..nkNilLit}: result = 0
  881. else: result = n.len
  882. proc safeArrLen*(n: PNode): int {.inline.} =
  883. ## works for array-like objects (strings passed as openArray in VM).
  884. if n.kind in {nkStrLit..nkTripleStrLit}: result = n.strVal.len
  885. elif n.kind in {nkNone..nkFloat128Lit}: result = 0
  886. else: result = n.len
  887. proc add*(father, son: PNode) =
  888. assert son != nil
  889. father.sons.add(son)
  890. proc addAllowNil*(father, son: PNode) {.inline.} =
  891. father.sons.add(son)
  892. template `[]`*(n: PNode, i: int): PNode = n.sons[i]
  893. template `[]=`*(n: PNode, i: int; x: PNode) = n.sons[i] = x
  894. template `[]`*(n: PNode, i: BackwardsIndex): PNode = n[n.len - i.int]
  895. template `[]=`*(n: PNode, i: BackwardsIndex; x: PNode) = n[n.len - i.int] = x
  896. proc add*(father, son: PType) =
  897. assert son != nil
  898. father.sons.add(son)
  899. proc addAllowNil*(father, son: PType) {.inline.} =
  900. father.sons.add(son)
  901. template `[]`*(n: PType, i: int): PType = n.sons[i]
  902. template `[]=`*(n: PType, i: int; x: PType) = n.sons[i] = x
  903. template `[]`*(n: PType, i: BackwardsIndex): PType = n[n.len - i.int]
  904. template `[]=`*(n: PType, i: BackwardsIndex; x: PType) = n[n.len - i.int] = x
  905. proc getDeclPragma*(n: PNode): PNode =
  906. ## return the `nkPragma` node for declaration `n`, or `nil` if no pragma was found.
  907. ## Currently only supports routineDefs + {nkTypeDef}.
  908. case n.kind
  909. of routineDefs:
  910. if n[pragmasPos].kind != nkEmpty: result = n[pragmasPos]
  911. else: result = nil
  912. of nkTypeDef:
  913. #[
  914. type F3*{.deprecated: "x3".} = int
  915. TypeSection
  916. TypeDef
  917. PragmaExpr
  918. Postfix
  919. Ident "*"
  920. Ident "F3"
  921. Pragma
  922. ExprColonExpr
  923. Ident "deprecated"
  924. StrLit "x3"
  925. Empty
  926. Ident "int"
  927. ]#
  928. if n[0].kind == nkPragmaExpr:
  929. result = n[0][1]
  930. else:
  931. result = nil
  932. else:
  933. # support as needed for `nkIdentDefs` etc.
  934. result = nil
  935. if result != nil:
  936. assert result.kind == nkPragma, $(result.kind, n.kind)
  937. proc extractPragma*(s: PSym): PNode =
  938. ## gets the pragma node of routine/type/var/let/const symbol `s`
  939. if s.kind in routineKinds:
  940. result = s.ast[pragmasPos]
  941. elif s.kind in {skType, skVar, skLet, skConst}:
  942. if s.ast != nil and s.ast.len > 0:
  943. if s.ast[0].kind == nkPragmaExpr and s.ast[0].len > 1:
  944. # s.ast = nkTypedef / nkPragmaExpr / [nkSym, nkPragma]
  945. result = s.ast[0][1]
  946. else:
  947. result = nil
  948. else:
  949. result = nil
  950. else:
  951. result = nil
  952. assert result == nil or result.kind == nkPragma
  953. proc skipPragmaExpr*(n: PNode): PNode =
  954. ## if pragma expr, give the node the pragmas are applied to,
  955. ## otherwise give node itself
  956. if n.kind == nkPragmaExpr:
  957. result = n[0]
  958. else:
  959. result = n
  960. proc setInfoRecursive*(n: PNode, info: TLineInfo) =
  961. ## set line info recursively
  962. if n != nil:
  963. for i in 0..<n.safeLen: setInfoRecursive(n[i], info)
  964. n.info = info
  965. when defined(useNodeIds):
  966. const nodeIdToDebug* = -1 # 2322968
  967. var gNodeId: int
  968. template newNodeImpl(info2) =
  969. result = PNode(kind: kind, info: info2)
  970. when false:
  971. # this would add overhead, so we skip it; it results in a small amount of leaked entries
  972. # for old PNode that gets re-allocated at the same address as a PNode that
  973. # has `nfHasComment` set (and an entry in that table). Only `nfHasComment`
  974. # should be used to test whether a PNode has a comment; gconfig.comments
  975. # can contain extra entries for deleted PNode's with comments.
  976. gconfig.comments.del(cast[int](result))
  977. template setIdMaybe() =
  978. when defined(useNodeIds):
  979. result.id = gNodeId
  980. if result.id == nodeIdToDebug:
  981. echo "KIND ", result.kind
  982. writeStackTrace()
  983. inc gNodeId
  984. proc newNode*(kind: TNodeKind): PNode =
  985. ## new node with unknown line info, no type, and no children
  986. newNodeImpl(unknownLineInfo)
  987. setIdMaybe()
  988. proc newNodeI*(kind: TNodeKind, info: TLineInfo): PNode =
  989. ## new node with line info, no type, and no children
  990. newNodeImpl(info)
  991. setIdMaybe()
  992. proc newNodeI*(kind: TNodeKind, info: TLineInfo, children: int): PNode =
  993. ## new node with line info, type, and children
  994. newNodeImpl(info)
  995. if children > 0:
  996. newSeq(result.sons, children)
  997. setIdMaybe()
  998. proc newNodeIT*(kind: TNodeKind, info: TLineInfo, typ: PType): PNode =
  999. ## new node with line info, type, and no children
  1000. result = newNode(kind)
  1001. result.info = info
  1002. result.typ = typ
  1003. proc newNode*(kind: TNodeKind, info: TLineInfo): PNode =
  1004. ## new node with line info, no type, and no children
  1005. newNodeImpl(info)
  1006. setIdMaybe()
  1007. proc newAtom*(ident: PIdent, info: TLineInfo): PNode =
  1008. result = newNode(nkIdent, info)
  1009. result.ident = ident
  1010. proc newAtom*(kind: TNodeKind, intVal: BiggestInt, info: TLineInfo): PNode =
  1011. result = newNode(kind, info)
  1012. result.intVal = intVal
  1013. proc newAtom*(kind: TNodeKind, floatVal: BiggestFloat, info: TLineInfo): PNode =
  1014. result = newNode(kind, info)
  1015. result.floatVal = floatVal
  1016. proc newAtom*(kind: TNodeKind; strVal: sink string; info: TLineInfo): PNode =
  1017. result = newNode(kind, info)
  1018. result.strVal = strVal
  1019. proc newTree*(kind: TNodeKind; info: TLineInfo; children: varargs[PNode]): PNode =
  1020. result = newNodeI(kind, info)
  1021. if children.len > 0:
  1022. result.info = children[0].info
  1023. result.sons = @children
  1024. proc newTree*(kind: TNodeKind; children: varargs[PNode]): PNode =
  1025. result = newNode(kind)
  1026. if children.len > 0:
  1027. result.info = children[0].info
  1028. result.sons = @children
  1029. proc newTreeI*(kind: TNodeKind; info: TLineInfo; children: varargs[PNode]): PNode =
  1030. result = newNodeI(kind, info)
  1031. if children.len > 0:
  1032. result.info = children[0].info
  1033. result.sons = @children
  1034. proc newTreeIT*(kind: TNodeKind; info: TLineInfo; typ: PType; children: varargs[PNode]): PNode =
  1035. result = newNodeIT(kind, info, typ)
  1036. if children.len > 0:
  1037. result.info = children[0].info
  1038. result.sons = @children
  1039. template previouslyInferred*(t: PType): PType =
  1040. if t.sons.len > 1: t.last else: nil
  1041. when false:
  1042. import tables, strutils
  1043. var x: CountTable[string]
  1044. addQuitProc proc () {.noconv.} =
  1045. for k, v in pairs(x):
  1046. echo k
  1047. echo v
  1048. proc newSym*(symKind: TSymKind, name: PIdent, idgen: IdGenerator; owner: PSym,
  1049. info: TLineInfo; options: TOptions = {}): PSym =
  1050. # generates a symbol and initializes the hash field too
  1051. assert not name.isNil
  1052. let id = nextSymId idgen
  1053. result = PSym(name: name, kind: symKind, flags: {}, info: info, itemId: id,
  1054. options: options, owner: owner, offset: defaultOffset,
  1055. disamb: getOrDefault(idgen.disambTable, name).int32)
  1056. idgen.disambTable.inc name
  1057. when false:
  1058. if id.module == 48 and id.item == 39:
  1059. writeStackTrace()
  1060. echo "kind ", symKind, " ", name.s
  1061. if owner != nil: echo owner.name.s
  1062. proc astdef*(s: PSym): PNode =
  1063. # get only the definition (initializer) portion of the ast
  1064. if s.ast != nil and s.ast.kind in {nkIdentDefs, nkConstDef}:
  1065. s.ast[2]
  1066. else:
  1067. s.ast
  1068. proc isMetaType*(t: PType): bool =
  1069. return t.kind in tyMetaTypes or
  1070. (t.kind == tyStatic and t.n == nil) or
  1071. tfHasMeta in t.flags
  1072. proc isUnresolvedStatic*(t: PType): bool =
  1073. return t.kind == tyStatic and t.n == nil
  1074. proc linkTo*(t: PType, s: PSym): PType {.discardable.} =
  1075. t.sym = s
  1076. s.typ = t
  1077. result = t
  1078. proc linkTo*(s: PSym, t: PType): PSym {.discardable.} =
  1079. t.sym = s
  1080. s.typ = t
  1081. result = s
  1082. template fileIdx*(c: PSym): FileIndex =
  1083. # XXX: this should be used only on module symbols
  1084. c.position.FileIndex
  1085. template filename*(c: PSym): string =
  1086. # XXX: this should be used only on module symbols
  1087. c.position.FileIndex.toFilename
  1088. proc appendToModule*(m: PSym, n: PNode) =
  1089. ## The compiler will use this internally to add nodes that will be
  1090. ## appended to the module after the sem pass
  1091. if m.ast == nil:
  1092. m.ast = newNode(nkStmtList)
  1093. m.ast.sons = @[n]
  1094. else:
  1095. assert m.ast.kind == nkStmtList
  1096. m.ast.sons.add(n)
  1097. const # for all kind of hash tables:
  1098. GrowthFactor* = 2 # must be power of 2, > 0
  1099. StartSize* = 8 # must be power of 2, > 0
  1100. proc copyStrTable*(dest: var TStrTable, src: TStrTable) =
  1101. dest.counter = src.counter
  1102. setLen(dest.data, src.data.len)
  1103. for i in 0..high(src.data): dest.data[i] = src.data[i]
  1104. proc copyObjectSet*(dest: var TObjectSet, src: TObjectSet) =
  1105. dest.counter = src.counter
  1106. setLen(dest.data, src.data.len)
  1107. for i in 0..high(src.data): dest.data[i] = src.data[i]
  1108. proc discardSons*(father: PNode) =
  1109. father.sons = @[]
  1110. proc withInfo*(n: PNode, info: TLineInfo): PNode =
  1111. n.info = info
  1112. return n
  1113. proc newIdentNode*(ident: PIdent, info: TLineInfo): PNode =
  1114. result = newNode(nkIdent)
  1115. result.ident = ident
  1116. result.info = info
  1117. proc newSymNode*(sym: PSym): PNode =
  1118. result = newNode(nkSym)
  1119. result.sym = sym
  1120. result.typ = sym.typ
  1121. result.info = sym.info
  1122. proc newSymNode*(sym: PSym, info: TLineInfo): PNode =
  1123. result = newNode(nkSym)
  1124. result.sym = sym
  1125. result.typ = sym.typ
  1126. result.info = info
  1127. proc newIntNode*(kind: TNodeKind, intVal: BiggestInt): PNode =
  1128. result = newNode(kind)
  1129. result.intVal = intVal
  1130. proc newIntNode*(kind: TNodeKind, intVal: Int128): PNode =
  1131. result = newNode(kind)
  1132. result.intVal = castToInt64(intVal)
  1133. proc lastSon*(n: PNode): PNode {.inline.} = n.sons[^1]
  1134. template setLastSon*(n: PNode, s: PNode) = n.sons[^1] = s
  1135. template firstSon*(n: PNode): PNode = n.sons[0]
  1136. template secondSon*(n: PNode): PNode = n.sons[1]
  1137. template hasSon*(n: PNode): bool = n.len > 0
  1138. template has2Sons*(n: PNode): bool = n.len > 1
  1139. proc replaceFirstSon*(n, newson: PNode) {.inline.} =
  1140. n.sons[0] = newson
  1141. proc replaceSon*(n: PNode; i: int; newson: PNode) {.inline.} =
  1142. n.sons[i] = newson
  1143. proc last*(n: PType): PType {.inline.} = n.sons[^1]
  1144. proc elementType*(n: PType): PType {.inline.} = n.sons[^1]
  1145. proc skipModifier*(n: PType): PType {.inline.} = n.sons[^1]
  1146. proc indexType*(n: PType): PType {.inline.} = n.sons[0]
  1147. proc baseClass*(n: PType): PType {.inline.} = n.sons[0]
  1148. proc base*(t: PType): PType {.inline.} =
  1149. result = t.sons[0]
  1150. proc returnType*(n: PType): PType {.inline.} = n.sons[0]
  1151. proc setReturnType*(n, r: PType) {.inline.} = n.sons[0] = r
  1152. proc setIndexType*(n, idx: PType) {.inline.} = n.sons[0] = idx
  1153. proc firstParamType*(n: PType): PType {.inline.} = n.sons[1]
  1154. proc firstGenericParam*(n: PType): PType {.inline.} = n.sons[1]
  1155. proc typeBodyImpl*(n: PType): PType {.inline.} = n.sons[^1]
  1156. proc genericHead*(n: PType): PType {.inline.} = n.sons[0]
  1157. proc skipTypes*(t: PType, kinds: TTypeKinds): PType =
  1158. ## Used throughout the compiler code to test whether a type tree contains or
  1159. ## doesn't contain a specific type/types - it is often the case that only the
  1160. ## last child nodes of a type tree need to be searched. This is a really hot
  1161. ## path within the compiler!
  1162. result = t
  1163. while result.kind in kinds: result = last(result)
  1164. proc newIntTypeNode*(intVal: BiggestInt, typ: PType): PNode =
  1165. let kind = skipTypes(typ, abstractVarRange).kind
  1166. case kind
  1167. of tyInt: result = newNode(nkIntLit)
  1168. of tyInt8: result = newNode(nkInt8Lit)
  1169. of tyInt16: result = newNode(nkInt16Lit)
  1170. of tyInt32: result = newNode(nkInt32Lit)
  1171. of tyInt64: result = newNode(nkInt64Lit)
  1172. of tyChar: result = newNode(nkCharLit)
  1173. of tyUInt: result = newNode(nkUIntLit)
  1174. of tyUInt8: result = newNode(nkUInt8Lit)
  1175. of tyUInt16: result = newNode(nkUInt16Lit)
  1176. of tyUInt32: result = newNode(nkUInt32Lit)
  1177. of tyUInt64: result = newNode(nkUInt64Lit)
  1178. of tyBool, tyEnum:
  1179. # XXX: does this really need to be the kind nkIntLit?
  1180. result = newNode(nkIntLit)
  1181. of tyStatic: # that's a pre-existing bug, will fix in another PR
  1182. result = newNode(nkIntLit)
  1183. else: raiseAssert $kind
  1184. result.intVal = intVal
  1185. result.typ = typ
  1186. proc newIntTypeNode*(intVal: Int128, typ: PType): PNode =
  1187. # XXX: introduce range check
  1188. newIntTypeNode(castToInt64(intVal), typ)
  1189. proc newFloatNode*(kind: TNodeKind, floatVal: BiggestFloat): PNode =
  1190. result = newNode(kind)
  1191. result.floatVal = floatVal
  1192. proc newStrNode*(kind: TNodeKind, strVal: string): PNode =
  1193. result = newNode(kind)
  1194. result.strVal = strVal
  1195. proc newStrNode*(strVal: string; info: TLineInfo): PNode =
  1196. result = newNodeI(nkStrLit, info)
  1197. result.strVal = strVal
  1198. proc newProcNode*(kind: TNodeKind, info: TLineInfo, body: PNode,
  1199. params,
  1200. name, pattern, genericParams,
  1201. pragmas, exceptions: PNode): PNode =
  1202. result = newNodeI(kind, info)
  1203. result.sons = @[name, pattern, genericParams, params,
  1204. pragmas, exceptions, body]
  1205. const
  1206. AttachedOpToStr*: array[TTypeAttachedOp, string] = [
  1207. "=wasMoved", "=destroy", "=copy", "=dup", "=sink", "=trace", "=deepcopy"]
  1208. proc `$`*(s: PSym): string =
  1209. if s != nil:
  1210. result = s.name.s & "@" & $s.id
  1211. else:
  1212. result = "<nil>"
  1213. when false:
  1214. iterator items*(t: PType): PType =
  1215. for i in 0..<t.sons.len: yield t.sons[i]
  1216. iterator pairs*(n: PType): tuple[i: int, n: PType] =
  1217. for i in 0..<n.sons.len: yield (i, n.sons[i])
  1218. when true:
  1219. proc len*(n: PType): int {.inline.} =
  1220. result = n.sons.len
  1221. proc sameTupleLengths*(a, b: PType): bool {.inline.} =
  1222. result = a.sons.len == b.sons.len
  1223. iterator tupleTypePairs*(a, b: PType): (int, PType, PType) =
  1224. for i in 0 ..< a.sons.len:
  1225. yield (i, a.sons[i], b.sons[i])
  1226. iterator underspecifiedPairs*(a, b: PType; start = 0; without = 0): (PType, PType) =
  1227. # XXX Figure out with what typekinds this is called.
  1228. for i in start ..< min(a.sons.len, b.sons.len) + without:
  1229. yield (a.sons[i], b.sons[i])
  1230. proc signatureLen*(t: PType): int {.inline.} =
  1231. result = t.sons.len
  1232. proc paramsLen*(t: PType): int {.inline.} =
  1233. result = t.sons.len - 1
  1234. proc genericParamsLen*(t: PType): int {.inline.} =
  1235. assert t.kind == tyGenericInst
  1236. result = t.sons.len - 2 # without 'head' and 'body'
  1237. proc genericInvocationParamsLen*(t: PType): int {.inline.} =
  1238. assert t.kind == tyGenericInvocation
  1239. result = t.sons.len - 1 # without 'head'
  1240. proc kidsLen*(t: PType): int {.inline.} =
  1241. result = t.sons.len
  1242. proc genericParamHasConstraints*(t: PType): bool {.inline.} = t.sons.len > 0
  1243. proc hasElementType*(t: PType): bool {.inline.} = t.sons.len > 0
  1244. proc isEmptyTupleType*(t: PType): bool {.inline.} = t.sons.len == 0
  1245. proc isSingletonTupleType*(t: PType): bool {.inline.} = t.sons.len == 1
  1246. proc genericConstraint*(t: PType): PType {.inline.} = t.sons[0]
  1247. iterator genericInstParams*(t: PType): (bool, PType) =
  1248. for i in 1..<t.sons.len-1:
  1249. yield (i!=1, t.sons[i])
  1250. iterator genericInstParamPairs*(a, b: PType): (int, PType, PType) =
  1251. for i in 1..<min(a.sons.len, b.sons.len)-1:
  1252. yield (i-1, a.sons[i], b.sons[i])
  1253. iterator genericInvocationParams*(t: PType): (bool, PType) =
  1254. for i in 1..<t.sons.len:
  1255. yield (i!=1, t.sons[i])
  1256. iterator genericInvocationAndBodyElements*(a, b: PType): (PType, PType) =
  1257. for i in 1..<a.sons.len:
  1258. yield (a.sons[i], b.sons[i-1])
  1259. iterator genericInvocationParamPairs*(a, b: PType): (bool, PType, PType) =
  1260. for i in 1..<a.sons.len:
  1261. if i >= b.sons.len:
  1262. yield (false, nil, nil)
  1263. else:
  1264. yield (true, a.sons[i], b.sons[i])
  1265. iterator genericBodyParams*(t: PType): (int, PType) =
  1266. for i in 0..<t.sons.len-1:
  1267. yield (i, t.sons[i])
  1268. iterator userTypeClassInstParams*(t: PType): (bool, PType) =
  1269. for i in 1..<t.sons.len-1:
  1270. yield (i!=1, t.sons[i])
  1271. iterator ikids*(t: PType): (int, PType) =
  1272. for i in 0..<t.sons.len: yield (i, t.sons[i])
  1273. const
  1274. FirstParamAt* = 1
  1275. FirstGenericParamAt* = 1
  1276. iterator paramTypes*(t: PType): (int, PType) =
  1277. for i in FirstParamAt..<t.sons.len: yield (i, t.sons[i])
  1278. iterator paramTypePairs*(a, b: PType): (PType, PType) =
  1279. for i in FirstParamAt..<a.sons.len: yield (a.sons[i], b.sons[i])
  1280. template paramTypeToNodeIndex*(x: int): int = x
  1281. iterator kids*(t: PType): PType =
  1282. for i in 0..<t.sons.len: yield t.sons[i]
  1283. iterator signature*(t: PType): PType =
  1284. # yields return type + parameter types
  1285. for i in 0..<t.sons.len: yield t.sons[i]
  1286. proc newType*(kind: TTypeKind; idgen: IdGenerator; owner: PSym; son: sink PType = nil): PType =
  1287. let id = nextTypeId idgen
  1288. result = PType(kind: kind, owner: owner, size: defaultSize,
  1289. align: defaultAlignment, itemId: id,
  1290. uniqueId: id, sons: @[])
  1291. if son != nil: result.sons.add son
  1292. when false:
  1293. if result.itemId.module == 55 and result.itemId.item == 2:
  1294. echo "KNID ", kind
  1295. writeStackTrace()
  1296. proc setSons*(dest: PType; sons: sink seq[PType]) {.inline.} = dest.sons = sons
  1297. proc setSon*(dest: PType; son: sink PType) {.inline.} = dest.sons = @[son]
  1298. proc mergeLoc(a: var TLoc, b: TLoc) =
  1299. if a.k == low(typeof(a.k)): a.k = b.k
  1300. if a.storage == low(typeof(a.storage)): a.storage = b.storage
  1301. a.flags.incl b.flags
  1302. if a.lode == nil: a.lode = b.lode
  1303. if a.r == "": a.r = b.r
  1304. proc newSons*(father: PNode, length: int) =
  1305. setLen(father.sons, length)
  1306. proc newSons*(father: PType, length: int) =
  1307. setLen(father.sons, length)
  1308. proc truncateInferredTypeCandidates*(t: PType) {.inline.} =
  1309. assert t.kind == tyInferred
  1310. if t.sons.len > 1:
  1311. setLen(t.sons, 1)
  1312. proc assignType*(dest, src: PType) =
  1313. dest.kind = src.kind
  1314. dest.flags = src.flags
  1315. dest.callConv = src.callConv
  1316. dest.n = src.n
  1317. dest.size = src.size
  1318. dest.align = src.align
  1319. # this fixes 'type TLock = TSysLock':
  1320. if src.sym != nil:
  1321. if dest.sym != nil:
  1322. dest.sym.flags.incl src.sym.flags-{sfUsed, sfExported}
  1323. if dest.sym.annex == nil: dest.sym.annex = src.sym.annex
  1324. mergeLoc(dest.sym.loc, src.sym.loc)
  1325. else:
  1326. dest.sym = src.sym
  1327. newSons(dest, src.sons.len)
  1328. for i in 0..<src.sons.len: dest[i] = src[i]
  1329. proc copyType*(t: PType, idgen: IdGenerator, owner: PSym): PType =
  1330. result = newType(t.kind, idgen, owner)
  1331. assignType(result, t)
  1332. result.sym = t.sym # backend-info should not be copied
  1333. proc exactReplica*(t: PType): PType =
  1334. result = PType(kind: t.kind, owner: t.owner, size: defaultSize,
  1335. align: defaultAlignment, itemId: t.itemId,
  1336. uniqueId: t.uniqueId)
  1337. assignType(result, t)
  1338. result.sym = t.sym # backend-info should not be copied
  1339. proc copySym*(s: PSym; idgen: IdGenerator): PSym =
  1340. result = newSym(s.kind, s.name, idgen, s.owner, s.info, s.options)
  1341. #result.ast = nil # BUGFIX; was: s.ast which made problems
  1342. result.typ = s.typ
  1343. result.flags = s.flags
  1344. result.magic = s.magic
  1345. result.options = s.options
  1346. result.position = s.position
  1347. result.loc = s.loc
  1348. result.annex = s.annex # BUGFIX
  1349. result.constraint = s.constraint
  1350. if result.kind in {skVar, skLet, skField}:
  1351. result.guard = s.guard
  1352. result.bitsize = s.bitsize
  1353. result.alignment = s.alignment
  1354. proc createModuleAlias*(s: PSym, idgen: IdGenerator, newIdent: PIdent, info: TLineInfo;
  1355. options: TOptions): PSym =
  1356. result = newSym(s.kind, newIdent, idgen, s.owner, info, options)
  1357. # keep ID!
  1358. result.ast = s.ast
  1359. #result.id = s.id # XXX figure out what to do with the ID.
  1360. result.flags = s.flags
  1361. result.options = s.options
  1362. result.position = s.position
  1363. result.loc = s.loc
  1364. result.annex = s.annex
  1365. proc initStrTable*(): TStrTable =
  1366. result = TStrTable(counter: 0)
  1367. newSeq(result.data, StartSize)
  1368. proc initObjectSet*(): TObjectSet =
  1369. result = TObjectSet(counter: 0)
  1370. newSeq(result.data, StartSize)
  1371. proc initNodeTable*(): TNodeTable =
  1372. result = TNodeTable(counter: 0)
  1373. newSeq(result.data, StartSize)
  1374. proc skipTypes*(t: PType, kinds: TTypeKinds; maxIters: int): PType =
  1375. result = t
  1376. var i = maxIters
  1377. while result.kind in kinds:
  1378. result = last(result)
  1379. dec i
  1380. if i == 0: return nil
  1381. proc skipTypesOrNil*(t: PType, kinds: TTypeKinds): PType =
  1382. ## same as skipTypes but handles 'nil'
  1383. result = t
  1384. while result != nil and result.kind in kinds:
  1385. if result.sons.len == 0: return nil
  1386. result = last(result)
  1387. proc isGCedMem*(t: PType): bool {.inline.} =
  1388. result = t.kind in {tyString, tyRef, tySequence} or
  1389. t.kind == tyProc and t.callConv == ccClosure
  1390. proc propagateToOwner*(owner, elem: PType; propagateHasAsgn = true) =
  1391. owner.flags.incl elem.flags * {tfHasMeta, tfTriggersCompileTime}
  1392. if tfNotNil in elem.flags:
  1393. if owner.kind in {tyGenericInst, tyGenericBody, tyGenericInvocation}:
  1394. owner.flags.incl tfNotNil
  1395. if elem.isMetaType:
  1396. owner.flags.incl tfHasMeta
  1397. let mask = elem.flags * {tfHasAsgn, tfHasOwned}
  1398. if mask != {} and propagateHasAsgn:
  1399. let o2 = owner.skipTypes({tyGenericInst, tyAlias, tySink})
  1400. if o2.kind in {tyTuple, tyObject, tyArray,
  1401. tySequence, tySet, tyDistinct}:
  1402. o2.flags.incl mask
  1403. owner.flags.incl mask
  1404. if owner.kind notin {tyProc, tyGenericInst, tyGenericBody,
  1405. tyGenericInvocation, tyPtr}:
  1406. let elemB = elem.skipTypes({tyGenericInst, tyAlias, tySink})
  1407. if elemB.isGCedMem or tfHasGCedMem in elemB.flags:
  1408. # for simplicity, we propagate this flag even to generics. We then
  1409. # ensure this doesn't bite us in sempass2.
  1410. owner.flags.incl tfHasGCedMem
  1411. proc rawAddSon*(father, son: PType; propagateHasAsgn = true) =
  1412. father.sons.add(son)
  1413. if not son.isNil: propagateToOwner(father, son, propagateHasAsgn)
  1414. proc addSonNilAllowed*(father, son: PNode) =
  1415. father.sons.add(son)
  1416. proc delSon*(father: PNode, idx: int) =
  1417. if father.len == 0: return
  1418. for i in idx..<father.len - 1: father[i] = father[i + 1]
  1419. father.sons.setLen(father.len - 1)
  1420. proc copyNode*(src: PNode): PNode =
  1421. # does not copy its sons!
  1422. if src == nil:
  1423. return nil
  1424. result = newNode(src.kind)
  1425. result.info = src.info
  1426. result.typ = src.typ
  1427. result.flags = src.flags * PersistentNodeFlags
  1428. result.comment = src.comment
  1429. when defined(useNodeIds):
  1430. if result.id == nodeIdToDebug:
  1431. echo "COMES FROM ", src.id
  1432. case src.kind
  1433. of nkCharLit..nkUInt64Lit: result.intVal = src.intVal
  1434. of nkFloatLiterals: result.floatVal = src.floatVal
  1435. of nkSym: result.sym = src.sym
  1436. of nkIdent: result.ident = src.ident
  1437. of nkStrLit..nkTripleStrLit: result.strVal = src.strVal
  1438. else: discard
  1439. when defined(nimsuggest):
  1440. result.endInfo = src.endInfo
  1441. template transitionNodeKindCommon(k: TNodeKind) =
  1442. let obj {.inject.} = n[]
  1443. n[] = TNode(kind: k, typ: obj.typ, info: obj.info, flags: obj.flags)
  1444. # n.comment = obj.comment # shouldn't be needed, the address doesnt' change
  1445. when defined(useNodeIds):
  1446. n.id = obj.id
  1447. proc transitionSonsKind*(n: PNode, kind: range[nkComesFrom..nkTupleConstr]) =
  1448. transitionNodeKindCommon(kind)
  1449. n.sons = obj.sons
  1450. proc transitionIntKind*(n: PNode, kind: range[nkCharLit..nkUInt64Lit]) =
  1451. transitionNodeKindCommon(kind)
  1452. n.intVal = obj.intVal
  1453. proc transitionIntToFloatKind*(n: PNode, kind: range[nkFloatLit..nkFloat128Lit]) =
  1454. transitionNodeKindCommon(kind)
  1455. n.floatVal = BiggestFloat(obj.intVal)
  1456. proc transitionNoneToSym*(n: PNode) =
  1457. transitionNodeKindCommon(nkSym)
  1458. template transitionSymKindCommon*(k: TSymKind) =
  1459. let obj {.inject.} = s[]
  1460. s[] = TSym(kind: k, itemId: obj.itemId, magic: obj.magic, typ: obj.typ, name: obj.name,
  1461. info: obj.info, owner: obj.owner, flags: obj.flags, ast: obj.ast,
  1462. options: obj.options, position: obj.position, offset: obj.offset,
  1463. loc: obj.loc, annex: obj.annex, constraint: obj.constraint)
  1464. when hasFFI:
  1465. s.cname = obj.cname
  1466. when defined(nimsuggest):
  1467. s.allUsages = obj.allUsages
  1468. proc transitionGenericParamToType*(s: PSym) =
  1469. transitionSymKindCommon(skType)
  1470. proc transitionRoutineSymKind*(s: PSym, kind: range[skProc..skTemplate]) =
  1471. transitionSymKindCommon(kind)
  1472. s.gcUnsafetyReason = obj.gcUnsafetyReason
  1473. s.transformedBody = obj.transformedBody
  1474. proc transitionToLet*(s: PSym) =
  1475. transitionSymKindCommon(skLet)
  1476. s.guard = obj.guard
  1477. s.bitsize = obj.bitsize
  1478. s.alignment = obj.alignment
  1479. template copyNodeImpl(dst, src, processSonsStmt) =
  1480. if src == nil: return
  1481. dst = newNode(src.kind)
  1482. dst.info = src.info
  1483. when defined(nimsuggest):
  1484. result.endInfo = src.endInfo
  1485. dst.typ = src.typ
  1486. dst.flags = src.flags * PersistentNodeFlags
  1487. dst.comment = src.comment
  1488. when defined(useNodeIds):
  1489. if dst.id == nodeIdToDebug:
  1490. echo "COMES FROM ", src.id
  1491. case src.kind
  1492. of nkCharLit..nkUInt64Lit: dst.intVal = src.intVal
  1493. of nkFloatLiterals: dst.floatVal = src.floatVal
  1494. of nkSym: dst.sym = src.sym
  1495. of nkIdent: dst.ident = src.ident
  1496. of nkStrLit..nkTripleStrLit: dst.strVal = src.strVal
  1497. else: processSonsStmt
  1498. proc shallowCopy*(src: PNode): PNode =
  1499. # does not copy its sons, but provides space for them:
  1500. copyNodeImpl(result, src):
  1501. newSeq(result.sons, src.len)
  1502. proc copyTree*(src: PNode): PNode =
  1503. # copy a whole syntax tree; performs deep copying
  1504. copyNodeImpl(result, src):
  1505. newSeq(result.sons, src.len)
  1506. for i in 0..<src.len:
  1507. result[i] = copyTree(src[i])
  1508. proc copyTreeWithoutNode*(src, skippedNode: PNode): PNode =
  1509. copyNodeImpl(result, src):
  1510. result.sons = newSeqOfCap[PNode](src.len)
  1511. for n in src.sons:
  1512. if n != skippedNode:
  1513. result.sons.add copyTreeWithoutNode(n, skippedNode)
  1514. proc hasSonWith*(n: PNode, kind: TNodeKind): bool =
  1515. for i in 0..<n.len:
  1516. if n[i].kind == kind:
  1517. return true
  1518. result = false
  1519. proc hasNilSon*(n: PNode): bool =
  1520. for i in 0..<n.safeLen:
  1521. if n[i] == nil:
  1522. return true
  1523. elif hasNilSon(n[i]):
  1524. return true
  1525. result = false
  1526. proc containsNode*(n: PNode, kinds: TNodeKinds): bool =
  1527. result = false
  1528. if n == nil: return
  1529. case n.kind
  1530. of nkEmpty..nkNilLit: result = n.kind in kinds
  1531. else:
  1532. for i in 0..<n.len:
  1533. if n.kind in kinds or containsNode(n[i], kinds): return true
  1534. proc hasSubnodeWith*(n: PNode, kind: TNodeKind): bool =
  1535. case n.kind
  1536. of nkEmpty..nkNilLit, nkFormalParams: result = n.kind == kind
  1537. else:
  1538. for i in 0..<n.len:
  1539. if (n[i].kind == kind) or hasSubnodeWith(n[i], kind):
  1540. return true
  1541. result = false
  1542. proc getInt*(a: PNode): Int128 =
  1543. case a.kind
  1544. of nkCharLit, nkUIntLit..nkUInt64Lit:
  1545. result = toInt128(cast[uint64](a.intVal))
  1546. of nkInt8Lit..nkInt64Lit:
  1547. result = toInt128(a.intVal)
  1548. of nkIntLit:
  1549. # XXX: enable this assert
  1550. # assert a.typ.kind notin {tyChar, tyUint..tyUInt64}
  1551. result = toInt128(a.intVal)
  1552. else:
  1553. raiseRecoverableError("cannot extract number from invalid AST node")
  1554. proc getInt64*(a: PNode): int64 {.deprecated: "use getInt".} =
  1555. case a.kind
  1556. of nkCharLit, nkUIntLit..nkUInt64Lit, nkIntLit..nkInt64Lit:
  1557. result = a.intVal
  1558. else:
  1559. raiseRecoverableError("cannot extract number from invalid AST node")
  1560. proc getFloat*(a: PNode): BiggestFloat =
  1561. case a.kind
  1562. of nkFloatLiterals: result = a.floatVal
  1563. of nkCharLit, nkUIntLit..nkUInt64Lit, nkIntLit..nkInt64Lit:
  1564. result = BiggestFloat a.intVal
  1565. else:
  1566. raiseRecoverableError("cannot extract number from invalid AST node")
  1567. #doAssert false, "getFloat"
  1568. #internalError(a.info, "getFloat")
  1569. #result = 0.0
  1570. proc getStr*(a: PNode): string =
  1571. case a.kind
  1572. of nkStrLit..nkTripleStrLit: result = a.strVal
  1573. of nkNilLit:
  1574. # let's hope this fixes more problems than it creates:
  1575. result = ""
  1576. else:
  1577. raiseRecoverableError("cannot extract string from invalid AST node")
  1578. #doAssert false, "getStr"
  1579. #internalError(a.info, "getStr")
  1580. #result = ""
  1581. proc getStrOrChar*(a: PNode): string =
  1582. case a.kind
  1583. of nkStrLit..nkTripleStrLit: result = a.strVal
  1584. of nkCharLit..nkUInt64Lit: result = $chr(int(a.intVal))
  1585. else:
  1586. raiseRecoverableError("cannot extract string from invalid AST node")
  1587. #doAssert false, "getStrOrChar"
  1588. #internalError(a.info, "getStrOrChar")
  1589. #result = ""
  1590. proc isGenericParams*(n: PNode): bool {.inline.} =
  1591. ## used to judge whether a node is generic params.
  1592. n != nil and n.kind == nkGenericParams
  1593. proc isGenericRoutine*(n: PNode): bool {.inline.} =
  1594. n != nil and n.kind in callableDefs and n[genericParamsPos].isGenericParams
  1595. proc isGenericRoutineStrict*(s: PSym): bool {.inline.} =
  1596. ## determines if this symbol represents a generic routine
  1597. ## the unusual name is so it doesn't collide and eventually replaces
  1598. ## `isGenericRoutine`
  1599. s.kind in skProcKinds and s.ast.isGenericRoutine
  1600. proc isGenericRoutine*(s: PSym): bool {.inline.} =
  1601. ## determines if this symbol represents a generic routine or an instance of
  1602. ## one. This should be renamed accordingly and `isGenericRoutineStrict`
  1603. ## should take this name instead.
  1604. ##
  1605. ## Warning/XXX: Unfortunately, it considers a proc kind symbol flagged with
  1606. ## sfFromGeneric as a generic routine. Instead this should likely not be the
  1607. ## case and the concepts should be teased apart:
  1608. ## - generic definition
  1609. ## - generic instance
  1610. ## - either generic definition or instance
  1611. s.kind in skProcKinds and (sfFromGeneric in s.flags or
  1612. s.ast.isGenericRoutine)
  1613. proc skipGenericOwner*(s: PSym): PSym =
  1614. ## Generic instantiations are owned by their originating generic
  1615. ## symbol. This proc skips such owners and goes straight to the owner
  1616. ## of the generic itself (the module or the enclosing proc).
  1617. result = if s.kind == skModule:
  1618. s
  1619. elif s.kind in skProcKinds and sfFromGeneric in s.flags and s.owner.kind != skModule:
  1620. s.owner.owner
  1621. else:
  1622. s.owner
  1623. proc originatingModule*(s: PSym): PSym =
  1624. result = s
  1625. while result.kind != skModule: result = result.owner
  1626. proc isRoutine*(s: PSym): bool {.inline.} =
  1627. result = s.kind in skProcKinds
  1628. proc isCompileTimeProc*(s: PSym): bool {.inline.} =
  1629. result = s.kind == skMacro or
  1630. s.kind in {skProc, skFunc} and sfCompileTime in s.flags
  1631. proc hasPattern*(s: PSym): bool {.inline.} =
  1632. result = isRoutine(s) and s.ast[patternPos].kind != nkEmpty
  1633. iterator items*(n: PNode): PNode =
  1634. for i in 0..<n.safeLen: yield n[i]
  1635. iterator pairs*(n: PNode): tuple[i: int, n: PNode] =
  1636. for i in 0..<n.safeLen: yield (i, n[i])
  1637. proc isAtom*(n: PNode): bool {.inline.} =
  1638. result = n.kind >= nkNone and n.kind <= nkNilLit
  1639. proc isEmptyType*(t: PType): bool {.inline.} =
  1640. ## 'void' and 'typed' types are often equivalent to 'nil' these days:
  1641. result = t == nil or t.kind in {tyVoid, tyTyped}
  1642. proc makeStmtList*(n: PNode): PNode =
  1643. if n.kind == nkStmtList:
  1644. result = n
  1645. else:
  1646. result = newNodeI(nkStmtList, n.info)
  1647. result.add n
  1648. proc skipStmtList*(n: PNode): PNode =
  1649. if n.kind in {nkStmtList, nkStmtListExpr}:
  1650. for i in 0..<n.len-1:
  1651. if n[i].kind notin {nkEmpty, nkCommentStmt}: return n
  1652. result = n.lastSon
  1653. else:
  1654. result = n
  1655. proc toVar*(typ: PType; kind: TTypeKind; idgen: IdGenerator): PType =
  1656. ## If ``typ`` is not a tyVar then it is converted into a `var <typ>` and
  1657. ## returned. Otherwise ``typ`` is simply returned as-is.
  1658. result = typ
  1659. if typ.kind != kind:
  1660. result = newType(kind, idgen, typ.owner, typ)
  1661. proc toRef*(typ: PType; idgen: IdGenerator): PType =
  1662. ## If ``typ`` is a tyObject then it is converted into a `ref <typ>` and
  1663. ## returned. Otherwise ``typ`` is simply returned as-is.
  1664. result = typ
  1665. if typ.skipTypes({tyAlias, tyGenericInst}).kind == tyObject:
  1666. result = newType(tyRef, idgen, typ.owner, typ)
  1667. proc toObject*(typ: PType): PType =
  1668. ## If ``typ`` is a tyRef then its immediate son is returned (which in many
  1669. ## cases should be a ``tyObject``).
  1670. ## Otherwise ``typ`` is simply returned as-is.
  1671. let t = typ.skipTypes({tyAlias, tyGenericInst})
  1672. if t.kind == tyRef: t.elementType
  1673. else: typ
  1674. proc toObjectFromRefPtrGeneric*(typ: PType): PType =
  1675. #[
  1676. See also `toObject`.
  1677. Finds the underlying `object`, even in cases like these:
  1678. type
  1679. B[T] = object f0: int
  1680. A1[T] = ref B[T]
  1681. A2[T] = ref object f1: int
  1682. A3 = ref object f2: int
  1683. A4 = object f3: int
  1684. ]#
  1685. result = typ
  1686. while true:
  1687. case result.kind
  1688. of tyGenericBody: result = result.last
  1689. of tyRef, tyPtr, tyGenericInst, tyGenericInvocation, tyAlias: result = result[0]
  1690. # automatic dereferencing is deep, refs #18298.
  1691. else: break
  1692. # result does not have to be object type
  1693. proc isImportedException*(t: PType; conf: ConfigRef): bool =
  1694. assert t != nil
  1695. if conf.exc != excCpp:
  1696. return false
  1697. let base = t.skipTypes({tyAlias, tyPtr, tyDistinct, tyGenericInst})
  1698. result = base.sym != nil and {sfCompileToCpp, sfImportc} * base.sym.flags != {}
  1699. proc isInfixAs*(n: PNode): bool =
  1700. return n.kind == nkInfix and n[0].kind == nkIdent and n[0].ident.id == ord(wAs)
  1701. proc skipColon*(n: PNode): PNode =
  1702. result = n
  1703. if n.kind == nkExprColonExpr:
  1704. result = n[1]
  1705. proc findUnresolvedStatic*(n: PNode): PNode =
  1706. if n.kind == nkSym and n.typ != nil and n.typ.kind == tyStatic and n.typ.n == nil:
  1707. return n
  1708. if n.typ != nil and n.typ.kind == tyTypeDesc:
  1709. let t = skipTypes(n.typ, {tyTypeDesc})
  1710. if t.kind == tyGenericParam and not t.genericParamHasConstraints:
  1711. return n
  1712. for son in n:
  1713. let n = son.findUnresolvedStatic
  1714. if n != nil: return n
  1715. return nil
  1716. when false:
  1717. proc containsNil*(n: PNode): bool =
  1718. # only for debugging
  1719. if n.isNil: return true
  1720. for i in 0..<n.safeLen:
  1721. if n[i].containsNil: return true
  1722. template hasDestructor*(t: PType): bool = {tfHasAsgn, tfHasOwned} * t.flags != {}
  1723. template incompleteType*(t: PType): bool =
  1724. t.sym != nil and {sfForward, sfNoForward} * t.sym.flags == {sfForward}
  1725. template typeCompleted*(s: PSym) =
  1726. incl s.flags, sfNoForward
  1727. template detailedInfo*(sym: PSym): string =
  1728. sym.name.s
  1729. proc isInlineIterator*(typ: PType): bool {.inline.} =
  1730. typ.kind == tyProc and tfIterator in typ.flags and typ.callConv != ccClosure
  1731. proc isIterator*(typ: PType): bool {.inline.} =
  1732. typ.kind == tyProc and tfIterator in typ.flags
  1733. proc isClosureIterator*(typ: PType): bool {.inline.} =
  1734. typ.kind == tyProc and tfIterator in typ.flags and typ.callConv == ccClosure
  1735. proc isClosure*(typ: PType): bool {.inline.} =
  1736. typ.kind == tyProc and typ.callConv == ccClosure
  1737. proc isNimcall*(s: PSym): bool {.inline.} =
  1738. s.typ.callConv == ccNimCall
  1739. proc isExplicitCallConv*(s: PSym): bool {.inline.} =
  1740. tfExplicitCallConv in s.typ.flags
  1741. proc isSinkParam*(s: PSym): bool {.inline.} =
  1742. s.kind == skParam and (s.typ.kind == tySink or tfHasOwned in s.typ.flags)
  1743. proc isSinkType*(t: PType): bool {.inline.} =
  1744. t.kind == tySink or tfHasOwned in t.flags
  1745. proc newProcType*(info: TLineInfo; idgen: IdGenerator; owner: PSym): PType =
  1746. result = newType(tyProc, idgen, owner)
  1747. result.n = newNodeI(nkFormalParams, info)
  1748. rawAddSon(result, nil) # return type
  1749. # result.n[0] used to be `nkType`, but now it's `nkEffectList` because
  1750. # the effects are now stored in there too ... this is a bit hacky, but as
  1751. # usual we desperately try to save memory:
  1752. result.n.add newNodeI(nkEffectList, info)
  1753. proc addParam*(procType: PType; param: PSym) =
  1754. param.position = procType.sons.len-1
  1755. procType.n.add newSymNode(param)
  1756. rawAddSon(procType, param.typ)
  1757. const magicsThatCanRaise = {
  1758. mNone, mSlurp, mStaticExec, mParseExprToAst, mParseStmtToAst, mEcho}
  1759. proc canRaiseConservative*(fn: PNode): bool =
  1760. if fn.kind == nkSym and fn.sym.magic notin magicsThatCanRaise:
  1761. result = false
  1762. else:
  1763. result = true
  1764. proc canRaise*(fn: PNode): bool =
  1765. if fn.kind == nkSym and (fn.sym.magic notin magicsThatCanRaise or
  1766. {sfImportc, sfInfixCall} * fn.sym.flags == {sfImportc} or
  1767. sfGeneratedOp in fn.sym.flags):
  1768. result = false
  1769. elif fn.kind == nkSym and fn.sym.magic == mEcho:
  1770. result = true
  1771. else:
  1772. # TODO check for n having sons? or just return false for now if not
  1773. if fn.typ != nil and fn.typ.n != nil and fn.typ.n[0].kind == nkSym:
  1774. result = false
  1775. else:
  1776. result = fn.typ != nil and fn.typ.n != nil and ((fn.typ.n[0].len < effectListLen) or
  1777. (fn.typ.n[0][exceptionEffects] != nil and
  1778. fn.typ.n[0][exceptionEffects].safeLen > 0))
  1779. proc toHumanStrImpl[T](kind: T, num: static int): string =
  1780. result = $kind
  1781. result = result[num..^1]
  1782. result[0] = result[0].toLowerAscii
  1783. proc toHumanStr*(kind: TSymKind): string =
  1784. ## strips leading `sk`
  1785. result = toHumanStrImpl(kind, 2)
  1786. proc toHumanStr*(kind: TTypeKind): string =
  1787. ## strips leading `tk`
  1788. result = toHumanStrImpl(kind, 2)
  1789. proc skipHiddenAddr*(n: PNode): PNode {.inline.} =
  1790. (if n.kind == nkHiddenAddr: n[0] else: n)
  1791. proc isNewStyleConcept*(n: PNode): bool {.inline.} =
  1792. assert n.kind == nkTypeClassTy
  1793. result = n[0].kind == nkEmpty
  1794. proc isOutParam*(t: PType): bool {.inline.} = tfIsOutParam in t.flags
  1795. const
  1796. nodesToIgnoreSet* = {nkNone..pred(nkSym), succ(nkSym)..nkNilLit,
  1797. nkTypeSection, nkProcDef, nkConverterDef,
  1798. nkMethodDef, nkIteratorDef, nkMacroDef, nkTemplateDef, nkLambda, nkDo,
  1799. nkFuncDef, nkConstSection, nkConstDef, nkIncludeStmt, nkImportStmt,
  1800. nkExportStmt, nkPragma, nkCommentStmt, nkBreakState,
  1801. nkTypeOfExpr, nkMixinStmt, nkBindStmt}
  1802. proc isTrue*(n: PNode): bool =
  1803. n.kind == nkSym and n.sym.kind == skEnumField and n.sym.position != 0 or
  1804. n.kind == nkIntLit and n.intVal != 0
  1805. type
  1806. TypeMapping* = Table[ItemId, PType]
  1807. SymMapping* = Table[ItemId, PSym]
  1808. template idTableGet*(tab: typed; key: PSym | PType): untyped = tab.getOrDefault(key.itemId)
  1809. template idTablePut*(tab: typed; key, val: PSym | PType) = tab[key.itemId] = val
  1810. template initSymMapping*(): Table[ItemId, PSym] = initTable[ItemId, PSym]()
  1811. template initTypeMapping*(): Table[ItemId, PType] = initTable[ItemId, PType]()
  1812. template resetIdTable*(tab: Table[ItemId, PSym]) = tab.clear()
  1813. template resetIdTable*(tab: Table[ItemId, PType]) = tab.clear()