ast.nim 73 KB

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