ast.nim 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  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, hashes, options, ropes, idents, idgen
  12. type
  13. TCallingConvention* = enum
  14. ccDefault, # proc has no explicit calling convention
  15. ccStdCall, # procedure is stdcall
  16. ccCDecl, # cdecl
  17. ccSafeCall, # safecall
  18. ccSysCall, # system call
  19. ccInline, # proc should be inlined
  20. ccNoInline, # proc should not be inlined
  21. ccFastCall, # fastcall (pass parameters in registers)
  22. ccClosure, # proc has a closure
  23. ccNoConvention # needed for generating proper C procs sometimes
  24. const
  25. CallingConvToStr*: array[TCallingConvention, string] = ["", "stdcall",
  26. "cdecl", "safecall", "syscall", "inline", "noinline", "fastcall",
  27. "closure", "noconv"]
  28. type
  29. TNodeKind* = enum # order is extremely important, because ranges are used
  30. # to check whether a node belongs to a certain class
  31. nkNone, # unknown node kind: indicates an error
  32. # Expressions:
  33. # Atoms:
  34. nkEmpty, # the node is empty
  35. nkIdent, # node is an identifier
  36. nkSym, # node is a symbol
  37. nkType, # node is used for its typ field
  38. nkCharLit, # a character literal ''
  39. nkIntLit, # an integer literal
  40. nkInt8Lit,
  41. nkInt16Lit,
  42. nkInt32Lit,
  43. nkInt64Lit,
  44. nkUIntLit, # an unsigned integer literal
  45. nkUInt8Lit,
  46. nkUInt16Lit,
  47. nkUInt32Lit,
  48. nkUInt64Lit,
  49. nkFloatLit, # a floating point literal
  50. nkFloat32Lit,
  51. nkFloat64Lit,
  52. nkFloat128Lit,
  53. nkStrLit, # a string literal ""
  54. nkRStrLit, # a raw string literal r""
  55. nkTripleStrLit, # a triple string literal """
  56. nkNilLit, # the nil literal
  57. # end of atoms
  58. nkComesFrom, # "comes from" template/macro information for
  59. # better stack trace generation
  60. nkDotCall, # used to temporarily flag a nkCall node;
  61. # this is used
  62. # for transforming ``s.len`` to ``len(s)``
  63. nkCommand, # a call like ``p 2, 4`` without parenthesis
  64. nkCall, # a call like p(x, y) or an operation like +(a, b)
  65. nkCallStrLit, # a call with a string literal
  66. # x"abc" has two sons: nkIdent, nkRStrLit
  67. # x"""abc""" has two sons: nkIdent, nkTripleStrLit
  68. nkInfix, # a call like (a + b)
  69. nkPrefix, # a call like !a
  70. nkPostfix, # something like a! (also used for visibility)
  71. nkHiddenCallConv, # an implicit type conversion via a type converter
  72. nkExprEqExpr, # a named parameter with equals: ''expr = expr''
  73. nkExprColonExpr, # a named parameter with colon: ''expr: expr''
  74. nkIdentDefs, # a definition like `a, b: typeDesc = expr`
  75. # either typeDesc or expr may be nil; used in
  76. # formal parameters, var statements, etc.
  77. nkVarTuple, # a ``var (a, b) = expr`` construct
  78. nkPar, # syntactic (); may be a tuple constructor
  79. nkObjConstr, # object constructor: T(a: 1, b: 2)
  80. nkCurly, # syntactic {}
  81. nkCurlyExpr, # an expression like a{i}
  82. nkBracket, # syntactic []
  83. nkBracketExpr, # an expression like a[i..j, k]
  84. nkPragmaExpr, # an expression like a{.pragmas.}
  85. nkRange, # an expression like i..j
  86. nkDotExpr, # a.b
  87. nkCheckedFieldExpr, # a.b, but b is a field that needs to be checked
  88. nkDerefExpr, # a^
  89. nkIfExpr, # if as an expression
  90. nkElifExpr,
  91. nkElseExpr,
  92. nkLambda, # lambda expression
  93. nkDo, # lambda block appering as trailing proc param
  94. nkAccQuoted, # `a` as a node
  95. nkTableConstr, # a table constructor {expr: expr}
  96. nkBind, # ``bind expr`` node
  97. nkClosedSymChoice, # symbol choice node; a list of nkSyms (closed)
  98. nkOpenSymChoice, # symbol choice node; a list of nkSyms (open)
  99. nkHiddenStdConv, # an implicit standard type conversion
  100. nkHiddenSubConv, # an implicit type conversion from a subtype
  101. # to a supertype
  102. nkConv, # a type conversion
  103. nkCast, # a type cast
  104. nkStaticExpr, # a static expr
  105. nkAddr, # a addr expression
  106. nkHiddenAddr, # implicit address operator
  107. nkHiddenDeref, # implicit ^ operator
  108. nkObjDownConv, # down conversion between object types
  109. nkObjUpConv, # up conversion between object types
  110. nkChckRangeF, # range check for floats
  111. nkChckRange64, # range check for 64 bit ints
  112. nkChckRange, # range check for ints
  113. nkStringToCString, # string to cstring
  114. nkCStringToString, # cstring to string
  115. # end of expressions
  116. nkAsgn, # a = b
  117. nkFastAsgn, # internal node for a fast ``a = b``
  118. # (no string copy)
  119. nkGenericParams, # generic parameters
  120. nkFormalParams, # formal parameters
  121. nkOfInherit, # inherited from symbol
  122. nkImportAs, # a 'as' b in an import statement
  123. nkProcDef, # a proc
  124. nkMethodDef, # a method
  125. nkConverterDef, # a converter
  126. nkMacroDef, # a macro
  127. nkTemplateDef, # a template
  128. nkIteratorDef, # an iterator
  129. nkOfBranch, # used inside case statements
  130. # for (cond, action)-pairs
  131. nkElifBranch, # used in if statements
  132. nkExceptBranch, # an except section
  133. nkElse, # an else part
  134. nkAsmStmt, # an assembler block
  135. nkPragma, # a pragma statement
  136. nkPragmaBlock, # a pragma with a block
  137. nkIfStmt, # an if statement
  138. nkWhenStmt, # a when expression or statement
  139. nkForStmt, # a for statement
  140. nkParForStmt, # a parallel for statement
  141. nkWhileStmt, # a while statement
  142. nkCaseStmt, # a case statement
  143. nkTypeSection, # a type section (consists of type definitions)
  144. nkVarSection, # a var section
  145. nkLetSection, # a let section
  146. nkConstSection, # a const section
  147. nkConstDef, # a const definition
  148. nkTypeDef, # a type definition
  149. nkYieldStmt, # the yield statement as a tree
  150. nkDefer, # the 'defer' statement
  151. nkTryStmt, # a try statement
  152. nkFinally, # a finally section
  153. nkRaiseStmt, # a raise statement
  154. nkReturnStmt, # a return statement
  155. nkBreakStmt, # a break statement
  156. nkContinueStmt, # a continue statement
  157. nkBlockStmt, # a block statement
  158. nkStaticStmt, # a static statement
  159. nkDiscardStmt, # a discard statement
  160. nkStmtList, # a list of statements
  161. nkImportStmt, # an import statement
  162. nkImportExceptStmt, # an import x except a statement
  163. nkExportStmt, # an export statement
  164. nkExportExceptStmt, # an 'export except' statement
  165. nkFromStmt, # a from * import statement
  166. nkIncludeStmt, # an include statement
  167. nkBindStmt, # a bind statement
  168. nkMixinStmt, # a mixin statement
  169. nkUsingStmt, # an using statement
  170. nkCommentStmt, # a comment statement
  171. nkStmtListExpr, # a statement list followed by an expr; this is used
  172. # to allow powerful multi-line templates
  173. nkBlockExpr, # a statement block ending in an expr; this is used
  174. # to allowe powerful multi-line templates that open a
  175. # temporary scope
  176. nkStmtListType, # a statement list ending in a type; for macros
  177. nkBlockType, # a statement block ending in a type; for macros
  178. # types as syntactic trees:
  179. nkWith, # distinct with `foo`
  180. nkWithout, # distinct without `foo`
  181. nkTypeOfExpr, # type(1+2)
  182. nkObjectTy, # object body
  183. nkTupleTy, # tuple body
  184. nkTupleClassTy, # tuple type class
  185. nkTypeClassTy, # user-defined type class
  186. nkStaticTy, # ``static[T]``
  187. nkRecList, # list of object parts
  188. nkRecCase, # case section of object
  189. nkRecWhen, # when section of object
  190. nkRefTy, # ``ref T``
  191. nkPtrTy, # ``ptr T``
  192. nkVarTy, # ``var T``
  193. nkConstTy, # ``const T``
  194. nkMutableTy, # ``mutable T``
  195. nkDistinctTy, # distinct type
  196. nkProcTy, # proc type
  197. nkIteratorTy, # iterator type
  198. nkSharedTy, # 'shared T'
  199. # we use 'nkPostFix' for the 'not nil' addition
  200. nkEnumTy, # enum body
  201. nkEnumFieldDef, # `ident = expr` in an enumeration
  202. nkArgList, # argument list
  203. nkPattern, # a special pattern; used for matching
  204. nkHiddenTryStmt, # token used for interpretation
  205. nkClosure, # (prc, env)-pair (internally used for code gen)
  206. nkGotoState, # used for the state machine (for iterators)
  207. nkState, # give a label to a code section (for iterators)
  208. nkBreakState, # special break statement for easier code generation
  209. nkFuncDef, # a func
  210. nkTupleConstr # a tuple constructor
  211. TNodeKinds* = set[TNodeKind]
  212. type
  213. TSymFlag* = enum # already 36 flags!
  214. sfUsed, # read access of sym (for warnings) or simply used
  215. sfExported, # symbol is exported from module
  216. sfFromGeneric, # symbol is instantiation of a generic; this is needed
  217. # for symbol file generation; such symbols should always
  218. # be written into the ROD file
  219. sfGlobal, # symbol is at global scope
  220. sfForward, # symbol is forward declared
  221. sfImportc, # symbol is external; imported
  222. sfExportc, # symbol is exported (under a specified name)
  223. sfVolatile, # variable is volatile
  224. sfRegister, # variable should be placed in a register
  225. sfPure, # object is "pure" that means it has no type-information
  226. # enum is "pure", its values need qualified access
  227. # variable is "pure"; it's an explicit "global"
  228. sfNoSideEffect, # proc has no side effects
  229. sfSideEffect, # proc may have side effects; cannot prove it has none
  230. sfMainModule, # module is the main module
  231. sfSystemModule, # module is the system module
  232. sfNoReturn, # proc never returns (an exit proc)
  233. sfAddrTaken, # the variable's address is taken (ex- or implicitly);
  234. # *OR*: a proc is indirectly called (used as first class)
  235. sfCompilerProc, # proc is a compiler proc, that is a C proc that is
  236. # needed for the code generator
  237. sfProcvar, # proc can be passed to a proc var
  238. sfDiscriminant, # field is a discriminant in a record/object
  239. sfDeprecated, # symbol is deprecated
  240. sfExplain, # provide more diagnostics when this symbol is used
  241. sfError, # usage of symbol should trigger a compile-time error
  242. sfShadowed, # a symbol that was shadowed in some inner scope
  243. sfThread, # proc will run as a thread
  244. # variable is a thread variable
  245. sfCompileTime, # proc can be evaluated at compile time
  246. sfConstructor, # proc is a C++ constructor
  247. sfDispatcher, # copied method symbol is the dispatcher
  248. # deprecated and unused, except for the con
  249. sfBorrow, # proc is borrowed
  250. sfInfixCall, # symbol needs infix call syntax in target language;
  251. # for interfacing with C++, JS
  252. sfNamedParamCall, # symbol needs named parameter call syntax in target
  253. # language; for interfacing with Objective C
  254. sfDiscardable, # returned value may be discarded implicitly
  255. sfOverriden, # proc is overriden
  256. sfCallsite # A flag for template symbols to tell the
  257. # compiler it should use line information from
  258. # the calling side of the macro, not from the
  259. # implementation.
  260. sfGenSym # symbol is 'gensym'ed; do not add to symbol table
  261. sfNonReloadable # symbol will be left as-is when hot code reloading is on -
  262. # meaning that it won't be renamed and/or changed in any way
  263. sfGeneratedOp # proc is a generated '='; do not inject destructors in it
  264. # variable is generated closure environment; requires early
  265. # destruction for --newruntime.
  266. TSymFlags* = set[TSymFlag]
  267. const
  268. sfNoInit* = sfMainModule # don't generate code to init the variable
  269. sfCursor* = sfDispatcher
  270. # local variable has been computed to be a "cursor".
  271. # see cursors.nim for details about what that means.
  272. sfAllUntyped* = sfVolatile # macro or template is immediately expanded \
  273. # in a generic context
  274. sfDirty* = sfPure
  275. # template is not hygienic (old styled template)
  276. # module, compiled from a dirty-buffer
  277. sfAnon* = sfDiscardable
  278. # symbol name that was generated by the compiler
  279. # the compiler will avoid printing such names
  280. # in user messages.
  281. sfHoisted* = sfForward
  282. # an expression was hoised to an anonymous variable.
  283. # the flag is applied to the var/let symbol
  284. sfNoForward* = sfRegister
  285. # forward declarations are not required (per module)
  286. sfReorder* = sfForward
  287. # reordering pass is enabled
  288. sfCompileToCpp* = sfInfixCall # compile the module as C++ code
  289. sfCompileToObjc* = sfNamedParamCall # compile the module as Objective-C code
  290. sfExperimental* = sfOverriden # module uses the .experimental switch
  291. sfGoto* = sfOverriden # var is used for 'goto' code generation
  292. sfWrittenTo* = sfBorrow # param is assigned to
  293. sfEscapes* = sfProcvar # param escapes
  294. sfBase* = sfDiscriminant
  295. sfIsSelf* = sfOverriden # param is 'self'
  296. sfCustomPragma* = sfRegister # symbol is custom pragma template
  297. const
  298. # getting ready for the future expr/stmt merge
  299. nkWhen* = nkWhenStmt
  300. nkWhenExpr* = nkWhenStmt
  301. nkEffectList* = nkArgList
  302. # hacks ahead: an nkEffectList is a node with 4 children:
  303. exceptionEffects* = 0 # exceptions at position 0
  304. usesEffects* = 1 # read effects at position 1
  305. writeEffects* = 2 # write effects at position 2
  306. tagEffects* = 3 # user defined tag ('gc', 'time' etc.)
  307. pragmasEffects* = 4 # not an effect, but a slot for pragmas in proc type
  308. effectListLen* = 5 # list of effects list
  309. type
  310. TTypeKind* = enum # order is important!
  311. # Don't forget to change hti.nim if you make a change here
  312. # XXX put this into an include file to avoid this issue!
  313. # several types are no longer used (guess which), but a
  314. # spot in the sequence is kept for backwards compatibility
  315. # (apparently something with bootstrapping)
  316. # if you need to add a type, they can apparently be reused
  317. tyNone, tyBool, tyChar,
  318. tyEmpty, tyAlias, tyNil, tyUntyped, tyTyped, tyTypeDesc,
  319. tyGenericInvocation, # ``T[a, b]`` for types to invoke
  320. tyGenericBody, # ``T[a, b, body]`` last parameter is the body
  321. tyGenericInst, # ``T[a, b, realInstance]`` instantiated generic type
  322. # realInstance will be a concrete type like tyObject
  323. # unless this is an instance of a generic alias type.
  324. # then realInstance will be the tyGenericInst of the
  325. # completely (recursively) resolved alias.
  326. tyGenericParam, # ``a`` in the above patterns
  327. tyDistinct,
  328. tyEnum,
  329. tyOrdinal, # integer types (including enums and boolean)
  330. tyArray,
  331. tyObject,
  332. tyTuple,
  333. tySet,
  334. tyRange,
  335. tyPtr, tyRef,
  336. tyVar,
  337. tySequence,
  338. tyProc,
  339. tyPointer, tyOpenArray,
  340. tyString, tyCString, tyForward,
  341. tyInt, tyInt8, tyInt16, tyInt32, tyInt64, # signed integers
  342. tyFloat, tyFloat32, tyFloat64, tyFloat128,
  343. tyUInt, tyUInt8, tyUInt16, tyUInt32, tyUInt64,
  344. tyOwned, tySink, tyLent,
  345. tyVarargs,
  346. tyUncheckedArray
  347. # An array with boundaries [0,+∞]
  348. tyProxy # used as errornous type (for idetools)
  349. tyBuiltInTypeClass
  350. # Type such as the catch-all object, tuple, seq, etc
  351. tyUserTypeClass
  352. # the body of a user-defined type class
  353. tyUserTypeClassInst
  354. # Instance of a parametric user-defined type class.
  355. # Structured similarly to tyGenericInst.
  356. # tyGenericInst represents concrete types, while
  357. # this is still a "generic param" that will bind types
  358. # and resolves them during sigmatch and instantiation.
  359. tyCompositeTypeClass
  360. # Type such as seq[Number]
  361. # The notes for tyUserTypeClassInst apply here as well
  362. # sons[0]: the original expression used by the user.
  363. # sons[1]: fully expanded and instantiated meta type
  364. # (potentially following aliases)
  365. tyInferred
  366. # In the initial state `base` stores a type class constraining
  367. # the types that can be inferred. After a candidate type is
  368. # selected, it's stored in `lastSon`. Between `base` and `lastSon`
  369. # there may be 0, 2 or more types that were also considered as
  370. # possible candidates in the inference process (i.e. lastSon will
  371. # be updated to store a type best conforming to all candidates)
  372. tyAnd, tyOr, tyNot
  373. # boolean type classes such as `string|int`,`not seq`,
  374. # `Sortable and Enumable`, etc
  375. tyAnything
  376. # a type class matching any type
  377. tyStatic
  378. # a value known at compile type (the underlying type is .base)
  379. tyFromExpr
  380. # This is a type representing an expression that depends
  381. # on generic parameters (the expression is stored in t.n)
  382. # It will be converted to a real type only during generic
  383. # instantiation and prior to this it has the potential to
  384. # be any type.
  385. tyOpt
  386. # Builtin optional type
  387. tyVoid
  388. # now different from tyEmpty, hurray!
  389. static:
  390. # remind us when TTypeKind stops to fit in a single 64-bit word
  391. assert TTypeKind.high.ord <= 63
  392. const
  393. tyPureObject* = tyTuple
  394. GcTypeKinds* = {tyRef, tySequence, tyString}
  395. tyError* = tyProxy # as an errornous node should match everything
  396. tyUnknown* = tyFromExpr
  397. tyUnknownTypes* = {tyError, tyFromExpr}
  398. tyTypeClasses* = {tyBuiltInTypeClass, tyCompositeTypeClass,
  399. tyUserTypeClass, tyUserTypeClassInst,
  400. tyAnd, tyOr, tyNot, tyAnything}
  401. tyMetaTypes* = {tyGenericParam, tyTypeDesc, tyUntyped} + tyTypeClasses
  402. tyUserTypeClasses* = {tyUserTypeClass, tyUserTypeClassInst}
  403. type
  404. TTypeKinds* = set[TTypeKind]
  405. TNodeFlag* = enum
  406. nfNone,
  407. nfBase2, # nfBase10 is default, so not needed
  408. nfBase8,
  409. nfBase16,
  410. nfAllConst, # used to mark complex expressions constant; easy to get rid of
  411. # but unfortunately it has measurable impact for compilation
  412. # efficiency
  413. nfTransf, # node has been transformed
  414. nfNoRewrite # node should not be transformed anymore
  415. nfSem # node has been checked for semantics
  416. nfLL # node has gone through lambda lifting
  417. nfDotField # the call can use a dot operator
  418. nfDotSetter # the call can use a setter dot operarator
  419. nfExplicitCall # x.y() was used instead of x.y
  420. nfExprCall # this is an attempt to call a regular expression
  421. nfIsRef # this node is a 'ref' node; used for the VM
  422. nfPreventCg # this node should be ignored by the codegen
  423. nfBlockArg # this a stmtlist appearing in a call (e.g. a do block)
  424. nfFromTemplate # a top-level node returned from a template
  425. nfDefaultParam # an automatically inserter default parameter
  426. nfDefaultRefsParam # a default param value references another parameter
  427. # the flag is applied to proc default values and to calls
  428. nfExecuteOnReload # A top-level statement that will be executed during reloads
  429. TNodeFlags* = set[TNodeFlag]
  430. TTypeFlag* = enum # keep below 32 for efficiency reasons (now: ~38)
  431. tfVarargs, # procedure has C styled varargs
  432. # tyArray type represeting a varargs list
  433. tfNoSideEffect, # procedure type does not allow side effects
  434. tfFinal, # is the object final?
  435. tfInheritable, # is the object inheritable?
  436. tfHasOwned, # type contains an 'owned' type and must be moved
  437. tfEnumHasHoles, # enum cannot be mapped into a range
  438. tfShallow, # type can be shallow copied on assignment
  439. tfThread, # proc type is marked as ``thread``; alias for ``gcsafe``
  440. tfFromGeneric, # type is an instantiation of a generic; this is needed
  441. # because for instantiations of objects, structural
  442. # type equality has to be used
  443. tfUnresolved, # marks unresolved typedesc/static params: e.g.
  444. # proc foo(T: typedesc, list: seq[T]): var T
  445. # proc foo(L: static[int]): array[L, int]
  446. # can be attached to ranges to indicate that the range
  447. # can be attached to generic procs with free standing
  448. # type parameters: e.g. proc foo[T]()
  449. # depends on unresolved static params.
  450. tfResolved # marks a user type class, after it has been bound to a
  451. # concrete type (lastSon becomes the concrete type)
  452. tfRetType, # marks return types in proc (used to detect type classes
  453. # used as return types for return type inference)
  454. tfCapturesEnv, # whether proc really captures some environment
  455. tfByCopy, # pass object/tuple by copy (C backend)
  456. tfByRef, # pass object/tuple by reference (C backend)
  457. tfIterator, # type is really an iterator, not a tyProc
  458. tfPartial, # type is declared as 'partial'
  459. tfNotNil, # type cannot be 'nil'
  460. tfNeedsInit, # type constains a "not nil" constraint somewhere or some
  461. # other type so that it requires initialization
  462. tfVarIsPtr, # 'var' type is translated like 'ptr' even in C++ mode
  463. tfHasMeta, # type contains "wildcard" sub-types such as generic params
  464. # or other type classes
  465. tfHasGCedMem, # type contains GC'ed memory
  466. tfPacked
  467. tfHasStatic
  468. tfGenericTypeParam
  469. tfImplicitTypeParam
  470. tfInferrableStatic
  471. tfConceptMatchedTypeSym
  472. tfExplicit # for typedescs, marks types explicitly prefixed with the
  473. # `type` operator (e.g. type int)
  474. tfWildcard # consider a proc like foo[T, I](x: Type[T, I])
  475. # T and I here can bind to both typedesc and static types
  476. # before this is determined, we'll consider them to be a
  477. # wildcard type.
  478. tfHasAsgn # type has overloaded assignment operator
  479. tfBorrowDot # distinct type borrows '.'
  480. tfTriggersCompileTime # uses the NimNode type which make the proc
  481. # implicitly '.compiletime'
  482. tfRefsAnonObj # used for 'ref object' and 'ptr object'
  483. tfCovariant # covariant generic param mimicing a ptr type
  484. tfWeakCovariant # covariant generic param mimicing a seq/array type
  485. tfContravariant # contravariant generic param
  486. tfCheckedForDestructor # type was checked for having a destructor.
  487. # If it has one, t.destructor is not nil.
  488. TTypeFlags* = set[TTypeFlag]
  489. TSymKind* = enum # the different symbols (start with the prefix sk);
  490. # order is important for the documentation generator!
  491. skUnknown, # unknown symbol: used for parsing assembler blocks
  492. # and first phase symbol lookup in generics
  493. skConditional, # symbol for the preprocessor (may become obsolete)
  494. skDynLib, # symbol represents a dynamic library; this is used
  495. # internally; it does not exist in Nim code
  496. skParam, # a parameter
  497. skGenericParam, # a generic parameter; eq in ``proc x[eq=`==`]()``
  498. skTemp, # a temporary variable (introduced by compiler)
  499. skModule, # module identifier
  500. skType, # a type
  501. skVar, # a variable
  502. skLet, # a 'let' symbol
  503. skConst, # a constant
  504. skResult, # special 'result' variable
  505. skProc, # a proc
  506. skFunc, # a func
  507. skMethod, # a method
  508. skIterator, # an iterator
  509. skConverter, # a type converter
  510. skMacro, # a macro
  511. skTemplate, # a template; currently also misused for user-defined
  512. # pragmas
  513. skField, # a field in a record or object
  514. skEnumField, # an identifier in an enum
  515. skForVar, # a for loop variable
  516. skLabel, # a label (for block statement)
  517. skStub, # symbol is a stub and not yet loaded from the ROD
  518. # file (it is loaded on demand, which may
  519. # mean: never)
  520. skPackage, # symbol is a package (used for canonicalization)
  521. skAlias # an alias (needs to be resolved immediately)
  522. TSymKinds* = set[TSymKind]
  523. const
  524. routineKinds* = {skProc, skFunc, skMethod, skIterator,
  525. skConverter, skMacro, skTemplate}
  526. tfIncompleteStruct* = tfVarargs
  527. tfUnion* = tfNoSideEffect
  528. tfGcSafe* = tfThread
  529. tfObjHasKids* = tfEnumHasHoles
  530. tfReturnsNew* = tfInheritable
  531. skError* = skUnknown
  532. # type flags that are essential for type equality:
  533. eqTypeFlags* = {tfIterator, tfNotNil, tfVarIsPtr}
  534. type
  535. TMagic* = enum # symbols that require compiler magic:
  536. mNone,
  537. mDefined, mDefinedInScope, mCompiles, mArrGet, mArrPut, mAsgn,
  538. mLow, mHigh, mSizeOf, mAlignOf, mOffsetOf, mTypeTrait,
  539. mIs, mOf, mAddr, mType, mTypeOf,
  540. mRoof, mPlugin, mEcho, mShallowCopy, mSlurp, mStaticExec, mStatic,
  541. mParseExprToAst, mParseStmtToAst, mExpandToAst, mQuoteAst,
  542. mUnaryLt, mInc, mDec, mOrd,
  543. mNew, mNewFinalize, mNewSeq, mNewSeqOfCap,
  544. mLengthOpenArray, mLengthStr, mLengthArray, mLengthSeq,
  545. mXLenStr, mXLenSeq,
  546. mIncl, mExcl, mCard, mChr,
  547. mGCref, mGCunref,
  548. mAddI, mSubI, mMulI, mDivI, mModI,
  549. mSucc, mPred,
  550. mAddF64, mSubF64, mMulF64, mDivF64,
  551. mShrI, mShlI, mAshrI, mBitandI, mBitorI, mBitxorI,
  552. mMinI, mMaxI,
  553. mMinF64, mMaxF64,
  554. mAddU, mSubU, mMulU, mDivU, mModU,
  555. mEqI, mLeI, mLtI,
  556. mEqF64, mLeF64, mLtF64,
  557. mLeU, mLtU,
  558. mLeU64, mLtU64,
  559. mEqEnum, mLeEnum, mLtEnum,
  560. mEqCh, mLeCh, mLtCh,
  561. mEqB, mLeB, mLtB,
  562. mEqRef, mEqUntracedRef, mLePtr, mLtPtr,
  563. mXor, mEqCString, mEqProc,
  564. mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot,
  565. mUnaryPlusI, mBitnotI,
  566. mUnaryPlusF64, mUnaryMinusF64, mAbsF64,
  567. mToFloat, mToBiggestFloat,
  568. mToInt, mToBiggestInt,
  569. mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr,
  570. mStrToStr, mEnumToStr,
  571. mAnd, mOr,
  572. mEqStr, mLeStr, mLtStr,
  573. mEqSet, mLeSet, mLtSet, mMulSet, mPlusSet, mMinusSet, mSymDiffSet,
  574. mConStrStr, mSlice,
  575. mDotDot, # this one is only necessary to give nice compile time warnings
  576. mFields, mFieldPairs, mOmpParFor,
  577. mAppendStrCh, mAppendStrStr, mAppendSeqElem,
  578. mInRange, mInSet, mRepr, mExit,
  579. mSetLengthStr, mSetLengthSeq,
  580. mIsPartOf, mAstToStr, mParallel,
  581. mSwap, mIsNil, mArrToSeq, mCopyStr, mCopyStrLast,
  582. mNewString, mNewStringOfCap, mParseBiggestFloat,
  583. mMove, mWasMoved, mDestroy,
  584. mDefault, mUnown, mAccessEnv, mReset,
  585. mArray, mOpenArray, mRange, mSet, mSeq, mOpt, mVarargs,
  586. mRef, mPtr, mVar, mDistinct, mVoid, mTuple,
  587. mOrdinal,
  588. mInt, mInt8, mInt16, mInt32, mInt64,
  589. mUInt, mUInt8, mUInt16, mUInt32, mUInt64,
  590. mFloat, mFloat32, mFloat64, mFloat128,
  591. mBool, mChar, mString, mCstring,
  592. mPointer, mEmptySet, mIntSetBaseType, mNil, mExpr, mStmt, mTypeDesc,
  593. mVoidType, mPNimrodNode, mShared, mGuarded, mLock, mSpawn, mDeepCopy,
  594. mIsMainModule, mCompileDate, mCompileTime, mProcCall,
  595. mCpuEndian, mHostOS, mHostCPU, mBuildOS, mBuildCPU, mAppType,
  596. mCompileOption, mCompileOptionArg,
  597. mNLen, mNChild, mNSetChild, mNAdd, mNAddMultiple, mNDel,
  598. mNKind, mNSymKind,
  599. mNccValue, mNccInc, mNcsAdd, mNcsIncl, mNcsLen, mNcsAt,
  600. mNctPut, mNctLen, mNctGet, mNctHasNext, mNctNext,
  601. mNIntVal, mNFloatVal, mNSymbol, mNIdent, mNGetType, mNStrVal, mNSetIntVal,
  602. mNSetFloatVal, mNSetSymbol, mNSetIdent, mNSetType, mNSetStrVal, mNLineInfo,
  603. mNNewNimNode, mNCopyNimNode, mNCopyNimTree, mStrToIdent, mNSigHash, mNSizeOf,
  604. mNBindSym, mLocals, mNCallSite,
  605. mEqIdent, mEqNimrodNode, mSameNodeType, mGetImpl, mNGenSym,
  606. mNHint, mNWarning, mNError,
  607. mInstantiationInfo, mGetTypeInfo,
  608. mNimvm, mIntDefine, mStrDefine, mBoolDefine, mRunnableExamples,
  609. mException, mBuiltinType, mSymOwner, mUncheckedArray, mGetImplTransf,
  610. mSymIsInstantiationOf
  611. # things that we can evaluate safely at compile time, even if not asked for it:
  612. const
  613. ctfeWhitelist* = {mNone, mUnaryLt, mSucc,
  614. mPred, mInc, mDec, mOrd, mLengthOpenArray,
  615. mLengthStr, mLengthArray, mLengthSeq, mXLenStr, mXLenSeq,
  616. mArrGet, mArrPut, mAsgn, mDestroy,
  617. mIncl, mExcl, mCard, mChr,
  618. mAddI, mSubI, mMulI, mDivI, mModI,
  619. mAddF64, mSubF64, mMulF64, mDivF64,
  620. mShrI, mShlI, mBitandI, mBitorI, mBitxorI,
  621. mMinI, mMaxI,
  622. mMinF64, mMaxF64,
  623. mAddU, mSubU, mMulU, mDivU, mModU,
  624. mEqI, mLeI, mLtI,
  625. mEqF64, mLeF64, mLtF64,
  626. mLeU, mLtU,
  627. mLeU64, mLtU64,
  628. mEqEnum, mLeEnum, mLtEnum,
  629. mEqCh, mLeCh, mLtCh,
  630. mEqB, mLeB, mLtB,
  631. mEqRef, mEqProc, mEqUntracedRef, mLePtr, mLtPtr, mEqCString, mXor,
  632. mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot, mUnaryPlusI, mBitnotI,
  633. mUnaryPlusF64, mUnaryMinusF64, mAbsF64,
  634. mToFloat, mToBiggestFloat,
  635. mToInt, mToBiggestInt,
  636. mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr,
  637. mStrToStr, mEnumToStr,
  638. mAnd, mOr,
  639. mEqStr, mLeStr, mLtStr,
  640. mEqSet, mLeSet, mLtSet, mMulSet, mPlusSet, mMinusSet, mSymDiffSet,
  641. mConStrStr, mAppendStrCh, mAppendStrStr, mAppendSeqElem,
  642. mInRange, mInSet, mRepr,
  643. mCopyStr, mCopyStrLast}
  644. type
  645. PNode* = ref TNode
  646. TNodeSeq* = seq[PNode]
  647. PType* = ref TType
  648. PSym* = ref TSym
  649. TNode*{.final, acyclic.} = object # on a 32bit machine, this takes 32 bytes
  650. when defined(useNodeIds):
  651. id*: int
  652. typ*: PType
  653. info*: TLineInfo
  654. flags*: TNodeFlags
  655. case kind*: TNodeKind
  656. of nkCharLit..nkUInt64Lit:
  657. intVal*: BiggestInt
  658. of nkFloatLit..nkFloat128Lit:
  659. floatVal*: BiggestFloat
  660. of nkStrLit..nkTripleStrLit:
  661. strVal*: string
  662. of nkSym:
  663. sym*: PSym
  664. of nkIdent:
  665. ident*: PIdent
  666. else:
  667. sons*: TNodeSeq
  668. comment*: string
  669. TStrTable* = object # a table[PIdent] of PSym
  670. counter*: int
  671. data*: seq[PSym]
  672. # -------------- backend information -------------------------------
  673. TLocKind* = enum
  674. locNone, # no location
  675. locTemp, # temporary location
  676. locLocalVar, # location is a local variable
  677. locGlobalVar, # location is a global variable
  678. locParam, # location is a parameter
  679. locField, # location is a record field
  680. locExpr, # "location" is really an expression
  681. locProc, # location is a proc (an address of a procedure)
  682. locData, # location is a constant
  683. locCall, # location is a call expression
  684. locOther # location is something other
  685. TLocFlag* = enum
  686. lfIndirect, # backend introduced a pointer
  687. lfFullExternalName, # only used when 'conf.cmd == cmdPretty': Indicates
  688. # that the symbol has been imported via 'importc: "fullname"' and
  689. # no format string.
  690. lfNoDeepCopy, # no need for a deep copy
  691. lfNoDecl, # do not declare it in C
  692. lfDynamicLib, # link symbol to dynamic library
  693. lfExportLib, # export symbol for dynamic library generation
  694. lfHeader, # include header file for symbol
  695. lfImportCompilerProc, # ``importc`` of a compilerproc
  696. lfSingleUse # no location yet and will only be used once
  697. lfEnforceDeref # a copyMem is required to dereference if this a
  698. # ptr array due to C array limitations.
  699. # See #1181, #6422, #11171
  700. lfPrepareForMutation # string location is about to be mutated (V2)
  701. TStorageLoc* = enum
  702. OnUnknown, # location is unknown (stack, heap or static)
  703. OnStatic, # in a static section
  704. OnStack, # location is on hardware stack
  705. OnHeap # location is on heap or global
  706. # (reference counting needed)
  707. TLocFlags* = set[TLocFlag]
  708. TLoc* = object
  709. k*: TLocKind # kind of location
  710. storage*: TStorageLoc
  711. flags*: TLocFlags # location's flags
  712. lode*: PNode # Node where the location came from; can be faked
  713. r*: Rope # rope value of location (code generators)
  714. # ---------------- end of backend information ------------------------------
  715. TLibKind* = enum
  716. libHeader, libDynamic
  717. TLib* = object # also misused for headers!
  718. kind*: TLibKind
  719. generated*: bool # needed for the backends:
  720. isOverriden*: bool
  721. name*: Rope
  722. path*: PNode # can be a string literal!
  723. CompilesId* = int ## id that is used for the caching logic within
  724. ## ``system.compiles``. See the seminst module.
  725. TInstantiation* = object
  726. sym*: PSym
  727. concreteTypes*: seq[PType]
  728. compilesId*: CompilesId
  729. PInstantiation* = ref TInstantiation
  730. TScope* = object
  731. depthLevel*: int
  732. symbols*: TStrTable
  733. parent*: PScope
  734. PScope* = ref TScope
  735. PLib* = ref TLib
  736. TSym* {.acyclic.} = object of TIdObj
  737. # proc and type instantiations are cached in the generic symbol
  738. case kind*: TSymKind
  739. of skType, skGenericParam:
  740. typeInstCache*: seq[PType]
  741. of routineKinds:
  742. procInstCache*: seq[PInstantiation]
  743. gcUnsafetyReason*: PSym # for better error messages wrt gcsafe
  744. transformedBody*: PNode # cached body after transf pass
  745. of skModule, skPackage:
  746. # modules keep track of the generic symbols they use from other modules.
  747. # this is because in incremental compilation, when a module is about to
  748. # be replaced with a newer version, we must decrement the usage count
  749. # of all previously used generics.
  750. # For 'import as' we copy the module symbol but shallowCopy the 'tab'
  751. # and set the 'usedGenerics' to ... XXX gah! Better set module.name
  752. # instead? But this doesn't work either. --> We need an skModuleAlias?
  753. # No need, just leave it as skModule but set the owner accordingly and
  754. # check for the owner when touching 'usedGenerics'.
  755. usedGenerics*: seq[PInstantiation]
  756. tab*: TStrTable # interface table for modules
  757. of skLet, skVar, skField, skForVar:
  758. guard*: PSym
  759. bitsize*: int
  760. else: nil
  761. magic*: TMagic
  762. typ*: PType
  763. name*: PIdent
  764. info*: TLineInfo
  765. owner*: PSym
  766. flags*: TSymFlags
  767. ast*: PNode # syntax tree of proc, iterator, etc.:
  768. # the whole proc including header; this is used
  769. # for easy generation of proper error messages
  770. # for variant record fields the discriminant
  771. # expression
  772. # for modules, it's a placeholder for compiler
  773. # generated code that will be appended to the
  774. # module after the sem pass (see appendToModule)
  775. options*: TOptions
  776. position*: int # used for many different things:
  777. # for enum fields its position;
  778. # for fields its offset
  779. # for parameters its position
  780. # for a conditional:
  781. # 1 iff the symbol is defined, else 0
  782. # (or not in symbol table)
  783. # for modules, an unique index corresponding
  784. # to the module's fileIdx
  785. # for variables a slot index for the evaluator
  786. # for routines a superop-ID
  787. offset*: int # offset of record field
  788. loc*: TLoc
  789. annex*: PLib # additional fields (seldom used, so we use a
  790. # reference to another object to save space)
  791. constraint*: PNode # additional constraints like 'lit|result'; also
  792. # misused for the codegenDecl pragma in the hope
  793. # it won't cause problems
  794. # for skModule the string literal to output for
  795. # deprecated modules.
  796. when defined(nimsuggest):
  797. allUsages*: seq[TLineInfo]
  798. TTypeSeq* = seq[PType]
  799. TLockLevel* = distinct int16
  800. TTypeAttachedOp* = enum ## as usual, order is important here
  801. attachedDestructor,
  802. attachedAsgn,
  803. attachedSink,
  804. attachedDeepCopy
  805. TType* {.acyclic.} = object of TIdObj # \
  806. # types are identical iff they have the
  807. # same id; there may be multiple copies of a type
  808. # in memory!
  809. kind*: TTypeKind # kind of type
  810. callConv*: TCallingConvention # for procs
  811. flags*: TTypeFlags # flags of the type
  812. sons*: TTypeSeq # base types, etc.
  813. n*: PNode # node for types:
  814. # for range types a nkRange node
  815. # for record types a nkRecord node
  816. # for enum types a list of symbols
  817. # for tyInt it can be the int literal
  818. # for procs and tyGenericBody, it's the
  819. # formal param list
  820. # for concepts, the concept body
  821. # else: unused
  822. owner*: PSym # the 'owner' of the type
  823. sym*: PSym # types have the sym associated with them
  824. # it is used for converting types to strings
  825. attachedOps*: array[TTypeAttachedOp, PSym] # destructors, etc.
  826. methods*: seq[(int,PSym)] # attached methods
  827. size*: BiggestInt # the size of the type in bytes
  828. # -1 means that the size is unkwown
  829. align*: int16 # the type's alignment requirements
  830. lockLevel*: TLockLevel # lock level as required for deadlock checking
  831. loc*: TLoc
  832. typeInst*: PType # for generic instantiations the tyGenericInst that led to this
  833. # type.
  834. uniqueId*: int # due to a design mistake, we need to keep the real ID here as it
  835. # required by the --incremental:on mode.
  836. TPair* = object
  837. key*, val*: RootRef
  838. TPairSeq* = seq[TPair]
  839. TIdPair* = object
  840. key*: PIdObj
  841. val*: RootRef
  842. TIdPairSeq* = seq[TIdPair]
  843. TIdTable* = object # the same as table[PIdent] of PObject
  844. counter*: int
  845. data*: TIdPairSeq
  846. TIdNodePair* = object
  847. key*: PIdObj
  848. val*: PNode
  849. TIdNodePairSeq* = seq[TIdNodePair]
  850. TIdNodeTable* = object # the same as table[PIdObj] of PNode
  851. counter*: int
  852. data*: TIdNodePairSeq
  853. TNodePair* = object
  854. h*: Hash # because it is expensive to compute!
  855. key*: PNode
  856. val*: int
  857. TNodePairSeq* = seq[TNodePair]
  858. TNodeTable* = object # the same as table[PNode] of int;
  859. # nodes are compared by structure!
  860. counter*: int
  861. data*: TNodePairSeq
  862. TObjectSeq* = seq[RootRef]
  863. TObjectSet* = object
  864. counter*: int
  865. data*: TObjectSeq
  866. TImplication* = enum
  867. impUnknown, impNo, impYes
  868. # BUGFIX: a module is overloadable so that a proc can have the
  869. # same name as an imported module. This is necessary because of
  870. # the poor naming choices in the standard library.
  871. const
  872. OverloadableSyms* = {skProc, skFunc, skMethod, skIterator,
  873. skConverter, skModule, skTemplate, skMacro}
  874. GenericTypes*: TTypeKinds = {tyGenericInvocation, tyGenericBody,
  875. tyGenericParam}
  876. StructuralEquivTypes*: TTypeKinds = {tyNil, tyTuple, tyArray,
  877. tySet, tyRange, tyPtr, tyRef, tyVar, tyLent, tySequence, tyProc, tyOpenArray,
  878. tyVarargs}
  879. ConcreteTypes*: TTypeKinds = { # types of the expr that may occur in::
  880. # var x = expr
  881. tyBool, tyChar, tyEnum, tyArray, tyObject,
  882. tySet, tyTuple, tyRange, tyPtr, tyRef, tyVar, tyLent, tySequence, tyProc,
  883. tyPointer,
  884. tyOpenArray, tyString, tyCString, tyInt..tyInt64, tyFloat..tyFloat128,
  885. tyUInt..tyUInt64}
  886. IntegralTypes* = {tyBool, tyChar, tyEnum, tyInt..tyInt64,
  887. tyFloat..tyFloat128, tyUInt..tyUInt64}
  888. ConstantDataTypes*: TTypeKinds = {tyArray, tySet,
  889. tyTuple, tySequence}
  890. NilableTypes*: TTypeKinds = {tyPointer, tyCString, tyRef, tyPtr,
  891. tyProc, tyError}
  892. ExportableSymKinds* = {skVar, skConst, skProc, skFunc, skMethod, skType,
  893. skIterator,
  894. skMacro, skTemplate, skConverter, skEnumField, skLet, skStub, skAlias}
  895. PersistentNodeFlags*: TNodeFlags = {nfBase2, nfBase8, nfBase16,
  896. nfDotSetter, nfDotField,
  897. nfIsRef, nfPreventCg, nfLL,
  898. nfFromTemplate, nfDefaultRefsParam,
  899. nfExecuteOnReload}
  900. namePos* = 0
  901. patternPos* = 1 # empty except for term rewriting macros
  902. genericParamsPos* = 2
  903. paramsPos* = 3
  904. pragmasPos* = 4
  905. miscPos* = 5 # used for undocumented and hacky stuff
  906. bodyPos* = 6 # position of body; use rodread.getBody() instead!
  907. resultPos* = 7
  908. dispatcherPos* = 8
  909. nkCallKinds* = {nkCall, nkInfix, nkPrefix, nkPostfix,
  910. nkCommand, nkCallStrLit, nkHiddenCallConv}
  911. nkIdentKinds* = {nkIdent, nkSym, nkAccQuoted, nkOpenSymChoice,
  912. nkClosedSymChoice}
  913. nkPragmaCallKinds* = {nkExprColonExpr, nkCall, nkCallStrLit}
  914. nkLiterals* = {nkCharLit..nkTripleStrLit}
  915. nkFloatLiterals* = {nkFloatLit..nkFloat128Lit}
  916. nkLambdaKinds* = {nkLambda, nkDo}
  917. declarativeDefs* = {nkProcDef, nkFuncDef, nkMethodDef, nkIteratorDef, nkConverterDef}
  918. procDefs* = nkLambdaKinds + declarativeDefs
  919. nkSymChoices* = {nkClosedSymChoice, nkOpenSymChoice}
  920. nkStrKinds* = {nkStrLit..nkTripleStrLit}
  921. skLocalVars* = {skVar, skLet, skForVar, skParam, skResult}
  922. skProcKinds* = {skProc, skFunc, skTemplate, skMacro, skIterator,
  923. skMethod, skConverter}
  924. var ggDebug* {.deprecated.}: bool ## convenience switch for trying out things
  925. #var
  926. # gMainPackageId*: int
  927. proc isCallExpr*(n: PNode): bool =
  928. result = n.kind in nkCallKinds
  929. proc discardSons*(father: PNode)
  930. proc len*(n: PNode): int {.inline.} =
  931. when defined(nimNoNilSeqs):
  932. result = len(n.sons)
  933. else:
  934. if isNil(n.sons): result = 0
  935. else: result = len(n.sons)
  936. proc safeLen*(n: PNode): int {.inline.} =
  937. ## works even for leaves.
  938. if n.kind in {nkNone..nkNilLit}: result = 0
  939. else: result = len(n)
  940. proc safeArrLen*(n: PNode): int {.inline.} =
  941. ## works for array-like objects (strings passed as openArray in VM).
  942. if n.kind in {nkStrLit..nkTripleStrLit}:result = len(n.strVal)
  943. elif n.kind in {nkNone..nkFloat128Lit}: result = 0
  944. else: result = len(n)
  945. proc add*(father, son: PNode) =
  946. assert son != nil
  947. when not defined(nimNoNilSeqs):
  948. if isNil(father.sons): father.sons = @[]
  949. add(father.sons, son)
  950. type Indexable = PNode | PType
  951. template `[]`*(n: Indexable, i: int): Indexable = n.sons[i]
  952. template `[]=`*(n: Indexable, i: int; x: Indexable) = n.sons[i] = x
  953. template `[]`*(n: Indexable, i: BackwardsIndex): Indexable = n[n.len - i.int]
  954. template `[]=`*(n: Indexable, i: BackwardsIndex; x: Indexable) = n[n.len - i.int] = x
  955. when defined(useNodeIds):
  956. const nodeIdToDebug* = -1 # 299750 # 300761 #300863 # 300879
  957. var gNodeId: int
  958. proc newNode*(kind: TNodeKind): PNode =
  959. new(result)
  960. result.kind = kind
  961. #result.info = UnknownLineInfo() inlined:
  962. result.info.fileIndex = InvalidFileIdx
  963. result.info.col = int16(-1)
  964. result.info.line = uint16(0)
  965. when defined(useNodeIds):
  966. result.id = gNodeId
  967. if result.id == nodeIdToDebug:
  968. echo "KIND ", result.kind
  969. writeStackTrace()
  970. inc gNodeId
  971. proc newTree*(kind: TNodeKind; children: varargs[PNode]): PNode =
  972. result = newNode(kind)
  973. if children.len > 0:
  974. result.info = children[0].info
  975. result.sons = @children
  976. template previouslyInferred*(t: PType): PType =
  977. if t.sons.len > 1: t.lastSon else: nil
  978. proc newSym*(symKind: TSymKind, name: PIdent, owner: PSym,
  979. info: TLineInfo; options: TOptions = {}): PSym =
  980. # generates a symbol and initializes the hash field too
  981. new(result)
  982. result.name = name
  983. result.kind = symKind
  984. result.flags = {}
  985. result.info = info
  986. result.options = options
  987. result.owner = owner
  988. result.offset = -1
  989. result.id = getID()
  990. when debugIds:
  991. registerId(result)
  992. proc astdef*(s: PSym): PNode =
  993. # get only the definition (initializer) portion of the ast
  994. if s.ast != nil and s.ast.kind == nkIdentDefs:
  995. s.ast[2]
  996. else:
  997. s.ast
  998. proc isMetaType*(t: PType): bool =
  999. return t.kind in tyMetaTypes or
  1000. (t.kind == tyStatic and t.n == nil) or
  1001. tfHasMeta in t.flags
  1002. proc isUnresolvedStatic*(t: PType): bool =
  1003. return t.kind == tyStatic and t.n == nil
  1004. proc linkTo*(t: PType, s: PSym): PType {.discardable.} =
  1005. t.sym = s
  1006. s.typ = t
  1007. result = t
  1008. proc linkTo*(s: PSym, t: PType): PSym {.discardable.} =
  1009. t.sym = s
  1010. s.typ = t
  1011. result = s
  1012. template fileIdx*(c: PSym): FileIndex =
  1013. # XXX: this should be used only on module symbols
  1014. c.position.FileIndex
  1015. template filename*(c: PSym): string =
  1016. # XXX: this should be used only on module symbols
  1017. c.position.FileIndex.toFilename
  1018. proc appendToModule*(m: PSym, n: PNode) =
  1019. ## The compiler will use this internally to add nodes that will be
  1020. ## appended to the module after the sem pass
  1021. if m.ast == nil:
  1022. m.ast = newNode(nkStmtList)
  1023. m.ast.sons = @[n]
  1024. else:
  1025. assert m.ast.kind == nkStmtList
  1026. m.ast.sons.add(n)
  1027. const # for all kind of hash tables:
  1028. GrowthFactor* = 2 # must be power of 2, > 0
  1029. StartSize* = 8 # must be power of 2, > 0
  1030. proc copyStrTable*(dest: var TStrTable, src: TStrTable) =
  1031. dest.counter = src.counter
  1032. setLen(dest.data, len(src.data))
  1033. for i in 0 .. high(src.data): dest.data[i] = src.data[i]
  1034. proc copyIdTable*(dest: var TIdTable, src: TIdTable) =
  1035. dest.counter = src.counter
  1036. newSeq(dest.data, len(src.data))
  1037. for i in 0 .. high(src.data): dest.data[i] = src.data[i]
  1038. proc copyObjectSet*(dest: var TObjectSet, src: TObjectSet) =
  1039. dest.counter = src.counter
  1040. setLen(dest.data, len(src.data))
  1041. for i in 0 .. high(src.data): dest.data[i] = src.data[i]
  1042. proc discardSons*(father: PNode) =
  1043. when defined(nimNoNilSeqs):
  1044. father.sons = @[]
  1045. else:
  1046. father.sons = nil
  1047. proc withInfo*(n: PNode, info: TLineInfo): PNode =
  1048. n.info = info
  1049. return n
  1050. proc newIdentNode*(ident: PIdent, info: TLineInfo): PNode =
  1051. result = newNode(nkIdent)
  1052. result.ident = ident
  1053. result.info = info
  1054. proc newSymNode*(sym: PSym): PNode =
  1055. result = newNode(nkSym)
  1056. result.sym = sym
  1057. result.typ = sym.typ
  1058. result.info = sym.info
  1059. proc newSymNode*(sym: PSym, info: TLineInfo): PNode =
  1060. result = newNode(nkSym)
  1061. result.sym = sym
  1062. result.typ = sym.typ
  1063. result.info = info
  1064. proc newNodeI*(kind: TNodeKind, info: TLineInfo): PNode =
  1065. new(result)
  1066. result.kind = kind
  1067. result.info = info
  1068. when defined(useNodeIds):
  1069. result.id = gNodeId
  1070. if result.id == nodeIdToDebug:
  1071. echo "KIND ", result.kind
  1072. writeStackTrace()
  1073. inc gNodeId
  1074. proc newNodeI*(kind: TNodeKind, info: TLineInfo, children: int): PNode =
  1075. new(result)
  1076. result.kind = kind
  1077. result.info = info
  1078. if children > 0:
  1079. newSeq(result.sons, children)
  1080. when defined(useNodeIds):
  1081. result.id = gNodeId
  1082. if result.id == nodeIdToDebug:
  1083. echo "KIND ", result.kind
  1084. writeStackTrace()
  1085. inc gNodeId
  1086. proc newNode*(kind: TNodeKind, info: TLineInfo, sons: TNodeSeq = @[],
  1087. typ: PType = nil): PNode =
  1088. new(result)
  1089. result.kind = kind
  1090. result.info = info
  1091. result.typ = typ
  1092. # XXX use shallowCopy here for ownership transfer:
  1093. result.sons = sons
  1094. when defined(useNodeIds):
  1095. result.id = gNodeId
  1096. if result.id == nodeIdToDebug:
  1097. echo "KIND ", result.kind
  1098. writeStackTrace()
  1099. inc gNodeId
  1100. proc newNodeIT*(kind: TNodeKind, info: TLineInfo, typ: PType): PNode =
  1101. result = newNode(kind)
  1102. result.info = info
  1103. result.typ = typ
  1104. proc newIntNode*(kind: TNodeKind, intVal: BiggestInt): PNode =
  1105. result = newNode(kind)
  1106. result.intVal = intVal
  1107. proc newIntTypeNode*(kind: TNodeKind, intVal: BiggestInt, typ: PType): PNode =
  1108. result = newIntNode(kind, intVal)
  1109. result.typ = typ
  1110. proc newFloatNode*(kind: TNodeKind, floatVal: BiggestFloat): PNode =
  1111. result = newNode(kind)
  1112. result.floatVal = floatVal
  1113. proc newStrNode*(kind: TNodeKind, strVal: string): PNode =
  1114. result = newNode(kind)
  1115. result.strVal = strVal
  1116. proc newStrNode*(strVal: string; info: TLineInfo): PNode =
  1117. result = newNodeI(nkStrLit, info)
  1118. result.strVal = strVal
  1119. proc addSon*(father, son: PNode) =
  1120. assert son != nil
  1121. when not defined(nimNoNilSeqs):
  1122. if isNil(father.sons): father.sons = @[]
  1123. add(father.sons, son)
  1124. proc newProcNode*(kind: TNodeKind, info: TLineInfo, body: PNode,
  1125. params,
  1126. name, pattern, genericParams,
  1127. pragmas, exceptions: PNode): PNode =
  1128. result = newNodeI(kind, info)
  1129. result.sons = @[name, pattern, genericParams, params,
  1130. pragmas, exceptions, body]
  1131. const
  1132. UnspecifiedLockLevel* = TLockLevel(-1'i16)
  1133. MaxLockLevel* = 1000'i16
  1134. UnknownLockLevel* = TLockLevel(1001'i16)
  1135. AttachedOpToStr*: array[TTypeAttachedOp, string] = ["=destroy", "=", "=sink", "=deepcopy"]
  1136. proc `$`*(x: TLockLevel): string =
  1137. if x.ord == UnspecifiedLockLevel.ord: result = "<unspecified>"
  1138. elif x.ord == UnknownLockLevel.ord: result = "<unknown>"
  1139. else: result = $int16(x)
  1140. proc `$`*(s: PSym): string =
  1141. if s != nil:
  1142. result = s.name.s & "@" & $s.id
  1143. else:
  1144. result = "<nil>"
  1145. proc newType*(kind: TTypeKind, owner: PSym): PType =
  1146. new(result)
  1147. result.kind = kind
  1148. result.owner = owner
  1149. result.size = -1
  1150. result.align = -1 # default alignment
  1151. result.id = getID()
  1152. result.uniqueId = result.id
  1153. result.lockLevel = UnspecifiedLockLevel
  1154. when debugIds:
  1155. registerId(result)
  1156. when false:
  1157. if result.id == 76426:
  1158. echo "KNID ", kind
  1159. writeStackTrace()
  1160. proc mergeLoc(a: var TLoc, b: TLoc) =
  1161. if a.k == low(a.k): a.k = b.k
  1162. if a.storage == low(a.storage): a.storage = b.storage
  1163. a.flags = a.flags + b.flags
  1164. if a.lode == nil: a.lode = b.lode
  1165. if a.r == nil: a.r = b.r
  1166. proc newSons*(father: PNode, length: int) =
  1167. when defined(nimNoNilSeqs):
  1168. setLen(father.sons, length)
  1169. else:
  1170. if isNil(father.sons):
  1171. newSeq(father.sons, length)
  1172. else:
  1173. setLen(father.sons, length)
  1174. proc newSons*(father: PType, length: int) =
  1175. when defined(nimNoNilSeqs):
  1176. setLen(father.sons, length)
  1177. else:
  1178. if isNil(father.sons):
  1179. newSeq(father.sons, length)
  1180. else:
  1181. setLen(father.sons, length)
  1182. proc sonsLen*(n: PType): int = n.sons.len
  1183. proc len*(n: PType): int = n.sons.len
  1184. proc sonsLen*(n: PNode): int = n.sons.len
  1185. proc lastSon*(n: PNode): PNode = n.sons[^1]
  1186. proc lastSon*(n: PType): PType = n.sons[^1]
  1187. proc assignType*(dest, src: PType) =
  1188. dest.kind = src.kind
  1189. dest.flags = src.flags
  1190. dest.callConv = src.callConv
  1191. dest.n = src.n
  1192. dest.size = src.size
  1193. dest.align = src.align
  1194. dest.attachedOps = src.attachedOps
  1195. dest.lockLevel = src.lockLevel
  1196. # this fixes 'type TLock = TSysLock':
  1197. if src.sym != nil:
  1198. if dest.sym != nil:
  1199. dest.sym.flags = dest.sym.flags + (src.sym.flags-{sfExported})
  1200. if dest.sym.annex == nil: dest.sym.annex = src.sym.annex
  1201. mergeLoc(dest.sym.loc, src.sym.loc)
  1202. else:
  1203. dest.sym = src.sym
  1204. newSons(dest, sonsLen(src))
  1205. for i in 0 ..< sonsLen(src): dest.sons[i] = src.sons[i]
  1206. proc copyType*(t: PType, owner: PSym, keepId: bool): PType =
  1207. result = newType(t.kind, owner)
  1208. assignType(result, t)
  1209. if keepId:
  1210. result.id = t.id
  1211. else:
  1212. when debugIds: registerId(result)
  1213. result.sym = t.sym # backend-info should not be copied
  1214. proc exactReplica*(t: PType): PType = copyType(t, t.owner, true)
  1215. proc copySym*(s: PSym): PSym =
  1216. result = newSym(s.kind, s.name, s.owner, s.info, s.options)
  1217. #result.ast = nil # BUGFIX; was: s.ast which made problems
  1218. result.typ = s.typ
  1219. when debugIds: registerId(result)
  1220. result.flags = s.flags
  1221. result.magic = s.magic
  1222. if s.kind == skModule:
  1223. copyStrTable(result.tab, s.tab)
  1224. result.options = s.options
  1225. result.position = s.position
  1226. result.loc = s.loc
  1227. result.annex = s.annex # BUGFIX
  1228. if result.kind in {skVar, skLet, skField}:
  1229. result.guard = s.guard
  1230. proc createModuleAlias*(s: PSym, newIdent: PIdent, info: TLineInfo;
  1231. options: TOptions): PSym =
  1232. result = newSym(s.kind, newIdent, s.owner, info, options)
  1233. # keep ID!
  1234. result.ast = s.ast
  1235. result.id = s.id
  1236. result.flags = s.flags
  1237. system.shallowCopy(result.tab, s.tab)
  1238. result.options = s.options
  1239. result.position = s.position
  1240. result.loc = s.loc
  1241. result.annex = s.annex
  1242. # XXX once usedGenerics is used, ensure module aliases keep working!
  1243. assert s.usedGenerics.len == 0
  1244. proc initStrTable*(x: var TStrTable) =
  1245. x.counter = 0
  1246. newSeq(x.data, StartSize)
  1247. proc newStrTable*: TStrTable =
  1248. initStrTable(result)
  1249. proc initIdTable*(x: var TIdTable) =
  1250. x.counter = 0
  1251. newSeq(x.data, StartSize)
  1252. proc newIdTable*: TIdTable =
  1253. initIdTable(result)
  1254. proc resetIdTable*(x: var TIdTable) =
  1255. x.counter = 0
  1256. # clear and set to old initial size:
  1257. setLen(x.data, 0)
  1258. setLen(x.data, StartSize)
  1259. proc initObjectSet*(x: var TObjectSet) =
  1260. x.counter = 0
  1261. newSeq(x.data, StartSize)
  1262. proc initIdNodeTable*(x: var TIdNodeTable) =
  1263. x.counter = 0
  1264. newSeq(x.data, StartSize)
  1265. proc initNodeTable*(x: var TNodeTable) =
  1266. x.counter = 0
  1267. newSeq(x.data, StartSize)
  1268. proc skipTypes*(t: PType, kinds: TTypeKinds): PType =
  1269. ## Used throughout the compiler code to test whether a type tree contains or
  1270. ## doesn't contain a specific type/types - it is often the case that only the
  1271. ## last child nodes of a type tree need to be searched. This is a really hot
  1272. ## path within the compiler!
  1273. result = t
  1274. while result.kind in kinds: result = lastSon(result)
  1275. proc skipTypes*(t: PType, kinds: TTypeKinds; maxIters: int): PType =
  1276. result = t
  1277. var i = maxIters
  1278. while result.kind in kinds:
  1279. result = lastSon(result)
  1280. dec i
  1281. if i == 0: return nil
  1282. proc skipTypesOrNil*(t: PType, kinds: TTypeKinds): PType =
  1283. ## same as skipTypes but handles 'nil'
  1284. result = t
  1285. while result != nil and result.kind in kinds:
  1286. if result.len == 0: return nil
  1287. result = lastSon(result)
  1288. proc isGCedMem*(t: PType): bool {.inline.} =
  1289. result = t.kind in {tyString, tyRef, tySequence} or
  1290. t.kind == tyProc and t.callConv == ccClosure
  1291. proc propagateToOwner*(owner, elem: PType) =
  1292. const HaveTheirOwnEmpty = {tySequence, tyOpt, tySet, tyPtr, tyRef, tyProc}
  1293. owner.flags = owner.flags + (elem.flags * {tfHasMeta, tfTriggersCompileTime})
  1294. if tfNotNil in elem.flags:
  1295. if owner.kind in {tyGenericInst, tyGenericBody, tyGenericInvocation}:
  1296. owner.flags.incl tfNotNil
  1297. elif owner.kind notin HaveTheirOwnEmpty:
  1298. owner.flags.incl tfNeedsInit
  1299. if tfNeedsInit in elem.flags:
  1300. if owner.kind in HaveTheirOwnEmpty: discard
  1301. else: owner.flags.incl tfNeedsInit
  1302. if elem.isMetaType:
  1303. owner.flags.incl tfHasMeta
  1304. if tfHasAsgn in elem.flags:
  1305. let o2 = owner.skipTypes({tyGenericInst, tyAlias, tySink})
  1306. if o2.kind in {tyTuple, tyObject, tyArray,
  1307. tySequence, tyOpt, tySet, tyDistinct}:
  1308. o2.flags.incl tfHasAsgn
  1309. owner.flags.incl tfHasAsgn
  1310. if tfHasOwned in elem.flags:
  1311. let o2 = owner.skipTypes({tyGenericInst, tyAlias, tySink})
  1312. if o2.kind in {tyTuple, tyObject, tyArray,
  1313. tySequence, tyOpt, tySet, tyDistinct}:
  1314. o2.flags.incl tfHasOwned
  1315. owner.flags.incl tfHasOwned
  1316. if owner.kind notin {tyProc, tyGenericInst, tyGenericBody,
  1317. tyGenericInvocation, tyPtr}:
  1318. let elemB = elem.skipTypes({tyGenericInst, tyAlias, tySink})
  1319. if elemB.isGCedMem or tfHasGCedMem in elemB.flags:
  1320. # for simplicity, we propagate this flag even to generics. We then
  1321. # ensure this doesn't bite us in sempass2.
  1322. owner.flags.incl tfHasGCedMem
  1323. proc rawAddSon*(father, son: PType) =
  1324. when not defined(nimNoNilSeqs):
  1325. if isNil(father.sons): father.sons = @[]
  1326. add(father.sons, son)
  1327. if not son.isNil: propagateToOwner(father, son)
  1328. proc rawAddSonNoPropagationOfTypeFlags*(father, son: PType) =
  1329. when not defined(nimNoNilSeqs):
  1330. if isNil(father.sons): father.sons = @[]
  1331. add(father.sons, son)
  1332. proc addSonNilAllowed*(father, son: PNode) =
  1333. when not defined(nimNoNilSeqs):
  1334. if isNil(father.sons): father.sons = @[]
  1335. add(father.sons, son)
  1336. proc delSon*(father: PNode, idx: int) =
  1337. when defined(nimNoNilSeqs):
  1338. if father.len == 0: return
  1339. else:
  1340. if isNil(father.sons): return
  1341. var length = sonsLen(father)
  1342. for i in idx .. length - 2: father.sons[i] = father.sons[i + 1]
  1343. setLen(father.sons, length - 1)
  1344. proc copyNode*(src: PNode): PNode =
  1345. # does not copy its sons!
  1346. if src == nil:
  1347. return nil
  1348. result = newNode(src.kind)
  1349. result.info = src.info
  1350. result.typ = src.typ
  1351. result.flags = src.flags * PersistentNodeFlags
  1352. result.comment = src.comment
  1353. when defined(useNodeIds):
  1354. if result.id == nodeIdToDebug:
  1355. echo "COMES FROM ", src.id
  1356. case src.kind
  1357. of nkCharLit..nkUInt64Lit: result.intVal = src.intVal
  1358. of nkFloatLiterals: result.floatVal = src.floatVal
  1359. of nkSym: result.sym = src.sym
  1360. of nkIdent: result.ident = src.ident
  1361. of nkStrLit..nkTripleStrLit: result.strVal = src.strVal
  1362. else: discard
  1363. proc shallowCopy*(src: PNode): PNode =
  1364. # does not copy its sons, but provides space for them:
  1365. if src == nil: return nil
  1366. result = newNode(src.kind)
  1367. result.info = src.info
  1368. result.typ = src.typ
  1369. result.flags = src.flags * PersistentNodeFlags
  1370. result.comment = src.comment
  1371. when defined(useNodeIds):
  1372. if result.id == nodeIdToDebug:
  1373. echo "COMES FROM ", src.id
  1374. case src.kind
  1375. of nkCharLit..nkUInt64Lit: result.intVal = src.intVal
  1376. of nkFloatLiterals: result.floatVal = src.floatVal
  1377. of nkSym: result.sym = src.sym
  1378. of nkIdent: result.ident = src.ident
  1379. of nkStrLit..nkTripleStrLit: result.strVal = src.strVal
  1380. else: newSeq(result.sons, sonsLen(src))
  1381. proc copyTree*(src: PNode): PNode =
  1382. # copy a whole syntax tree; performs deep copying
  1383. if src == nil:
  1384. return nil
  1385. result = newNode(src.kind)
  1386. result.info = src.info
  1387. result.typ = src.typ
  1388. result.flags = src.flags * PersistentNodeFlags
  1389. result.comment = src.comment
  1390. when defined(useNodeIds):
  1391. if result.id == nodeIdToDebug:
  1392. echo "COMES FROM ", src.id
  1393. case src.kind
  1394. of nkCharLit..nkUInt64Lit: result.intVal = src.intVal
  1395. of nkFloatLiterals: result.floatVal = src.floatVal
  1396. of nkSym: result.sym = src.sym
  1397. of nkIdent: result.ident = src.ident
  1398. of nkStrLit..nkTripleStrLit: result.strVal = src.strVal
  1399. else:
  1400. newSeq(result.sons, sonsLen(src))
  1401. for i in 0 ..< sonsLen(src):
  1402. result.sons[i] = copyTree(src.sons[i])
  1403. proc hasSonWith*(n: PNode, kind: TNodeKind): bool =
  1404. for i in 0 ..< sonsLen(n):
  1405. if n.sons[i].kind == kind:
  1406. return true
  1407. result = false
  1408. proc hasNilSon*(n: PNode): bool =
  1409. for i in 0 ..< safeLen(n):
  1410. if n.sons[i] == nil:
  1411. return true
  1412. elif hasNilSon(n.sons[i]):
  1413. return true
  1414. result = false
  1415. proc containsNode*(n: PNode, kinds: TNodeKinds): bool =
  1416. if n == nil: return
  1417. case n.kind
  1418. of nkEmpty..nkNilLit: result = n.kind in kinds
  1419. else:
  1420. for i in 0 ..< sonsLen(n):
  1421. if n.kind in kinds or containsNode(n.sons[i], kinds): return true
  1422. proc hasSubnodeWith*(n: PNode, kind: TNodeKind): bool =
  1423. case n.kind
  1424. of nkEmpty..nkNilLit: result = n.kind == kind
  1425. else:
  1426. for i in 0 ..< sonsLen(n):
  1427. if (n.sons[i].kind == kind) or hasSubnodeWith(n.sons[i], kind):
  1428. return true
  1429. result = false
  1430. proc getInt*(a: PNode): BiggestInt =
  1431. case a.kind
  1432. of nkCharLit..nkUInt64Lit: result = a.intVal
  1433. else:
  1434. raiseRecoverableError("cannot extract number from invalid AST node")
  1435. #internalError(a.info, "getInt")
  1436. #doAssert false, "getInt"
  1437. #result = 0
  1438. proc getFloat*(a: PNode): BiggestFloat =
  1439. case a.kind
  1440. of nkFloatLiterals: result = a.floatVal
  1441. else:
  1442. raiseRecoverableError("cannot extract number from invalid AST node")
  1443. #doAssert false, "getFloat"
  1444. #internalError(a.info, "getFloat")
  1445. #result = 0.0
  1446. proc getStr*(a: PNode): string =
  1447. case a.kind
  1448. of nkStrLit..nkTripleStrLit: result = a.strVal
  1449. of nkNilLit:
  1450. # let's hope this fixes more problems than it creates:
  1451. when defined(nimNoNilSeqs):
  1452. result = ""
  1453. else:
  1454. result = nil
  1455. else:
  1456. raiseRecoverableError("cannot extract string from invalid AST node")
  1457. #doAssert false, "getStr"
  1458. #internalError(a.info, "getStr")
  1459. #result = ""
  1460. proc getStrOrChar*(a: PNode): string =
  1461. case a.kind
  1462. of nkStrLit..nkTripleStrLit: result = a.strVal
  1463. of nkCharLit..nkUInt64Lit: result = $chr(int(a.intVal))
  1464. else:
  1465. raiseRecoverableError("cannot extract string from invalid AST node")
  1466. #doAssert false, "getStrOrChar"
  1467. #internalError(a.info, "getStrOrChar")
  1468. #result = ""
  1469. proc isGenericRoutine*(s: PSym): bool =
  1470. case s.kind
  1471. of skProcKinds:
  1472. result = sfFromGeneric in s.flags or
  1473. (s.ast != nil and s.ast[genericParamsPos].kind != nkEmpty)
  1474. else: discard
  1475. proc skipGenericOwner*(s: PSym): PSym =
  1476. ## Generic instantiations are owned by their originating generic
  1477. ## symbol. This proc skips such owners and goes straight to the owner
  1478. ## of the generic itself (the module or the enclosing proc).
  1479. result = if s.kind in skProcKinds and sfFromGeneric in s.flags:
  1480. s.owner.owner
  1481. else:
  1482. s.owner
  1483. proc originatingModule*(s: PSym): PSym =
  1484. result = s.owner
  1485. while result.kind != skModule: result = result.owner
  1486. proc isRoutine*(s: PSym): bool {.inline.} =
  1487. result = s.kind in skProcKinds
  1488. proc isCompileTimeProc*(s: PSym): bool {.inline.} =
  1489. result = s.kind == skMacro or
  1490. s.kind == skProc and sfCompileTime in s.flags
  1491. proc isRunnableExamples*(n: PNode): bool =
  1492. # Templates and generics don't perform symbol lookups.
  1493. result = n.kind == nkSym and n.sym.magic == mRunnableExamples or
  1494. n.kind == nkIdent and n.ident.s == "runnableExamples"
  1495. proc requiredParams*(s: PSym): int =
  1496. # Returns the number of required params (without default values)
  1497. # XXX: Perhaps we can store this in the `offset` field of the
  1498. # symbol instead?
  1499. for i in 1 ..< s.typ.len:
  1500. if s.typ.n[i].sym.ast != nil:
  1501. return i - 1
  1502. return s.typ.len - 1
  1503. proc hasPattern*(s: PSym): bool {.inline.} =
  1504. result = isRoutine(s) and s.ast.sons[patternPos].kind != nkEmpty
  1505. iterator items*(n: PNode): PNode =
  1506. for i in 0..<n.safeLen: yield n.sons[i]
  1507. iterator pairs*(n: PNode): tuple[i: int, n: PNode] =
  1508. for i in 0..<n.safeLen: yield (i, n.sons[i])
  1509. proc isAtom*(n: PNode): bool {.inline.} =
  1510. result = n.kind >= nkNone and n.kind <= nkNilLit
  1511. proc isEmptyType*(t: PType): bool {.inline.} =
  1512. ## 'void' and 'stmt' types are often equivalent to 'nil' these days:
  1513. result = t == nil or t.kind in {tyVoid, tyTyped}
  1514. proc makeStmtList*(n: PNode): PNode =
  1515. if n.kind == nkStmtList:
  1516. result = n
  1517. else:
  1518. result = newNodeI(nkStmtList, n.info)
  1519. result.add n
  1520. proc skipStmtList*(n: PNode): PNode =
  1521. if n.kind in {nkStmtList, nkStmtListExpr}:
  1522. for i in 0 .. n.len-2:
  1523. if n[i].kind notin {nkEmpty, nkCommentStmt}: return n
  1524. result = n.lastSon
  1525. else:
  1526. result = n
  1527. proc toVar*(typ: PType): PType =
  1528. ## If ``typ`` is not a tyVar then it is converted into a `var <typ>` and
  1529. ## returned. Otherwise ``typ`` is simply returned as-is.
  1530. result = typ
  1531. if typ.kind != tyVar:
  1532. result = newType(tyVar, typ.owner)
  1533. rawAddSon(result, typ)
  1534. proc toRef*(typ: PType): PType =
  1535. ## If ``typ`` is a tyObject then it is converted into a `ref <typ>` and
  1536. ## returned. Otherwise ``typ`` is simply returned as-is.
  1537. if typ.skipTypes({tyAlias, tyGenericInst}).kind == tyObject:
  1538. result = newType(tyRef, typ.owner)
  1539. rawAddSon(result, typ)
  1540. proc toObject*(typ: PType): PType =
  1541. ## If ``typ`` is a tyRef then its immediate son is returned (which in many
  1542. ## cases should be a ``tyObject``).
  1543. ## Otherwise ``typ`` is simply returned as-is.
  1544. let t = typ.skipTypes({tyAlias, tyGenericInst})
  1545. if t.kind == tyRef: t.lastSon
  1546. else: typ
  1547. proc isImportedException*(t: PType; conf: ConfigRef): bool =
  1548. assert t != nil
  1549. if optNoCppExceptions in conf.globalOptions:
  1550. return false
  1551. let base = t.skipTypes({tyAlias, tyPtr, tyDistinct, tyGenericInst})
  1552. if base.sym != nil and {sfCompileToCpp, sfImportc} * base.sym.flags != {}:
  1553. result = true
  1554. proc isInfixAs*(n: PNode): bool =
  1555. return n.kind == nkInfix and n[0].kind == nkIdent and n[0].ident.s == "as"
  1556. proc findUnresolvedStatic*(n: PNode): PNode =
  1557. if n.kind == nkSym and n.typ.kind == tyStatic and n.typ.n == nil:
  1558. return n
  1559. for son in n:
  1560. let n = son.findUnresolvedStatic
  1561. if n != nil: return n
  1562. return nil
  1563. when false:
  1564. proc containsNil*(n: PNode): bool =
  1565. # only for debugging
  1566. if n.isNil: return true
  1567. for i in 0 ..< n.safeLen:
  1568. if n[i].containsNil: return true
  1569. template hasDestructor*(t: PType): bool = {tfHasAsgn, tfHasOwned} * t.flags != {}
  1570. template incompleteType*(t: PType): bool =
  1571. t.sym != nil and {sfForward, sfNoForward} * t.sym.flags == {sfForward}
  1572. template typeCompleted*(s: PSym) =
  1573. incl s.flags, sfNoForward
  1574. template getBody*(s: PSym): PNode = s.ast[bodyPos]
  1575. template detailedInfo*(sym: PSym): string =
  1576. sym.name.s
  1577. proc isInlineIterator*(s: PSym): bool {.inline.} =
  1578. s.kind == skIterator and s.typ.callConv != ccClosure
  1579. proc isClosureIterator*(s: PSym): bool {.inline.} =
  1580. s.kind == skIterator and s.typ.callConv == ccClosure
  1581. proc isSinkParam*(s: PSym): bool {.inline.} =
  1582. s.kind == skParam and (s.typ.kind == tySink or tfHasOwned in s.typ.flags)
  1583. proc isSinkType*(t: PType): bool {.inline.} =
  1584. t.kind == tySink or tfHasOwned in t.flags
  1585. proc newProcType*(info: TLineInfo; owner: PSym): PType =
  1586. result = newType(tyProc, owner)
  1587. result.n = newNodeI(nkFormalParams, info)
  1588. rawAddSon(result, nil) # return type
  1589. # result.n[0] used to be `nkType`, but now it's `nkEffectList` because
  1590. # the effects are now stored in there too ... this is a bit hacky, but as
  1591. # usual we desperately try to save memory:
  1592. addSon(result.n, newNodeI(nkEffectList, info))
  1593. proc addParam*(procType: PType; param: PSym) =
  1594. param.position = procType.len-1
  1595. addSon(procType.n, newSymNode(param))
  1596. rawAddSon(procType, param.typ)
  1597. template destructor*(t: PType): PSym = t.attachedOps[attachedDestructor]
  1598. template assignment*(t: PType): PSym = t.attachedOps[attachedAsgn]
  1599. template asink*(t: PType): PSym = t.attachedOps[attachedSink]