ast.nim 73 KB

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