diff --git a/compiler/ast.nim b/compiler/ast.nim index d80589c087d36..92b34cf087913 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -692,30 +692,36 @@ type PScope* = ref TScope + ItemState* = enum + Complete # completely in memory + Partial # partially in memory + Sealed # complete in memory, already written to NIF file, so further mutations are not allowed + PLib* = ref TLib - TSym* {.acyclic.} = object # Keep in sync with PackedSym + TSym* {.acyclic.} = object # Keep in sync with ast2nif.nim itemId*: ItemId # proc and type instantiations are cached in the generic symbol - case kind*: TSymKind + state*: ItemState + case kindImpl*: TSymKind # Note: kept as 'kind' for case statement, but accessor checks state of routineKinds: #procInstCache*: seq[PInstantiation] - gcUnsafetyReason*: PSym # for better error messages regarding gcsafe - transformedBody*: PNode # cached body after transf pass + gcUnsafetyReasonImpl*: PSym # for better error messages regarding gcsafe + transformedBodyImpl*: PNode # cached body after transf pass of skLet, skVar, skField, skForVar: - guard*: PSym - bitsize*: int - alignment*: int # for alignment + guardImpl*: PSym + bitsizeImpl*: int + alignmentImpl*: int # for alignment else: nil - magic*: TMagic - typ*: PType + magicImpl*: TMagic + typImpl*: PType name*: PIdent - info*: TLineInfo + infoImpl*: TLineInfo when defined(nimsuggest): - endInfo*: TLineInfo - hasUserSpecifiedType*: bool # used for determining whether to display inlay type hints - ownerField: PSym - flags*: TSymFlags - ast*: PNode # syntax tree of proc, iterator, etc.: + endInfoImpl*: TLineInfo + hasUserSpecifiedTypeImpl*: bool # used for determining whether to display inlay type hints + ownerFieldImpl: PSym + flagsImpl*: TSymFlags + astImpl*: PNode # syntax tree of proc, iterator, etc.: # the whole proc including header; this is used # for easy generation of proper error messages # for variant record fields the discriminant @@ -723,8 +729,8 @@ type # for modules, it's a placeholder for compiler # generated code that will be appended to the # module after the sem pass (see appendToModule) - options*: TOptions - position*: int # used for many different things: + optionsImpl*: TOptions + positionImpl*: int # used for many different things: # for enum fields its position; # for fields its offset # for parameters its position (starting with 0) @@ -734,23 +740,23 @@ type # for modules, an unique index corresponding # to the module's fileIdx # for variables a slot index for the evaluator - offset*: int32 # offset of record field + offsetImpl*: int32 # offset of record field disamb*: int32 # disambiguation number; the basic idea is that # `___` is unique - loc*: TLoc - annex*: PLib # additional fields (seldom used, so we use a + locImpl*: TLoc + annexImpl*: PLib # additional fields (seldom used, so we use a # reference to another object to save space) when hasFFI: - cname*: string # resolved C declaration name in importc decl, e.g.: + cnameImpl*: string # resolved C declaration name in importc decl, e.g.: # proc fun() {.importc: "$1aux".} => cname = funaux - constraint*: PNode # additional constraints like 'lit|result'; also + constraintImpl*: PNode # additional constraints like 'lit|result'; also # misused for the codegenDecl and virtual pragmas in the hope # it won't cause problems # for skModule the string literal to output for # deprecated modules. - instantiatedFrom*: PSym # for instances, the generic symbol where it came from. + instantiatedFromImpl*: PSym # for instances, the generic symbol where it came from. when defined(nimsuggest): - allUsages*: seq[TLineInfo] + allUsagesImpl*: seq[TLineInfo] TTypeSeq* = seq[PType] @@ -770,6 +776,7 @@ type # Keep in sync with PackedType itemId*: ItemId kind*: TTypeKind # kind of type + state*: ItemState callConv*: TCallingConvention # for procs flags*: TTypeFlags # flags of the type sons: TTypeSeq # base types, etc. @@ -786,7 +793,7 @@ type sym*: PSym # types have the sym associated with them # it is used for converting types to strings size*: BiggestInt # the size of the type in bytes - # -1 means that the size is unkwown + # -1 means that the size is unknown align*: int16 # the type's alignment requirements paddingAtEnd*: int16 # loc*: TLoc @@ -834,11 +841,258 @@ template nodeId(n: PNode): int = cast[int](n) template typ*(n: PNode): PType = n.typField +proc loadSym*(s: PSym) {.inline.} = + ## Loads a symbol from NIF file if it's in Partial state. + ## This is a forward declaration - implementation should be provided elsewhere. + discard + +proc ensureMutable*(s: PSym) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + proc owner*(s: PSym|PType): PSym {.inline.} = - result = s.ownerField + when s is PSym: + if s.state == Partial: loadSym(s) + result = s.ownerFieldImpl + else: + result = s.ownerField proc setOwner*(s: PSym|PType, owner: PSym) {.inline.} = - s.ownerField = owner + when s is PSym: + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.ownerFieldImpl = owner + else: + s.ownerField = owner + +# Accessor procs for TSym fields +# Note: kind is kept as a direct field for case statement compatibility +# but we still provide an accessor that checks state +proc kind*(s: PSym): TSymKind {.inline.} = + if s.state == Partial: loadSym(s) + result = s.kindImpl + +proc `kind=`*(s: PSym, val: TSymKind) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.kindImpl = val + +proc gcUnsafetyReason*(s: PSym): PSym {.inline.} = + if s.state == Partial: loadSym(s) + result = s.gcUnsafetyReasonImpl + +proc `gcUnsafetyReason=`*(s: PSym, val: PSym) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.gcUnsafetyReasonImpl = val + +proc transformedBody*(s: PSym): PNode {.inline.} = + if s.state == Partial: loadSym(s) + result = s.transformedBodyImpl + +proc `transformedBody=`*(s: PSym, val: PNode) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.transformedBodyImpl = val + +proc guard*(s: PSym): PSym {.inline.} = + if s.state == Partial: loadSym(s) + result = s.guardImpl + +proc `guard=`*(s: PSym, val: PSym) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.guardImpl = val + +proc bitsize*(s: PSym): int {.inline.} = + if s.state == Partial: loadSym(s) + result = s.bitsizeImpl + +proc `bitsize=`*(s: PSym, val: int) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.bitsizeImpl = val + +proc alignment*(s: PSym): int {.inline.} = + if s.state == Partial: loadSym(s) + result = s.alignmentImpl + +proc `alignment=`*(s: PSym, val: int) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.alignmentImpl = val + +proc magic*(s: PSym): TMagic {.inline.} = + if s.state == Partial: loadSym(s) + result = s.magicImpl + +proc `magic=`*(s: PSym, val: TMagic) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.magicImpl = val + +proc typ*(s: PSym): PType {.inline.} = + if s.state == Partial: loadSym(s) + result = s.typImpl + +proc `typ=`*(s: PSym, val: PType) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.typImpl = val + +proc info*(s: PSym): TLineInfo {.inline.} = + if s.state == Partial: loadSym(s) + result = s.infoImpl + +proc `info=`*(s: PSym, val: TLineInfo) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.infoImpl = val + +when defined(nimsuggest): + proc endInfo*(s: PSym): TLineInfo {.inline.} = + if s.state == Partial: loadSym(s) + result = s.endInfoImpl + + proc `endInfo=`*(s: PSym, val: TLineInfo) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.endInfoImpl = val + + proc hasUserSpecifiedType*(s: PSym): bool {.inline.} = + if s.state == Partial: loadSym(s) + result = s.hasUserSpecifiedTypeImpl + + proc `hasUserSpecifiedType=`*(s: PSym, val: bool) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.hasUserSpecifiedTypeImpl = val + +proc flags*(s: PSym): TSymFlags {.inline.} = + if s.state == Partial: loadSym(s) + result = s.flagsImpl + +proc `flags=`*(s: PSym, val: TSymFlags) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.flagsImpl = val + +proc ast*(s: PSym): PNode {.inline.} = + if s.state == Partial: loadSym(s) + result = s.astImpl + +proc `ast=`*(s: PSym, val: PNode) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.astImpl = val + +proc options*(s: PSym): TOptions {.inline.} = + if s.state == Partial: loadSym(s) + result = s.optionsImpl + +proc `options=`*(s: PSym, val: TOptions) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.optionsImpl = val + +proc position*(s: PSym): int {.inline.} = + if s.state == Partial: loadSym(s) + result = s.positionImpl + +proc `position=`*(s: PSym, val: int) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.positionImpl = val + +proc offset*(s: PSym): int32 {.inline.} = + if s.state == Partial: loadSym(s) + result = s.offsetImpl + +proc `offset=`*(s: PSym, val: int32) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.offsetImpl = val + +proc loc*(s: PSym): TLoc {.inline.} = + if s.state == Partial: loadSym(s) + result = s.locImpl + +proc `loc=`*(s: PSym, val: TLoc) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.locImpl = val + +proc annex*(s: PSym): PLib {.inline.} = + if s.state == Partial: loadSym(s) + result = s.annexImpl + +proc `annex=`*(s: PSym, val: PLib) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.annexImpl = val + +when hasFFI: + proc cname*(s: PSym): string {.inline.} = + if s.state == Partial: loadSym(s) + result = s.cnameImpl + + proc `cname=`*(s: PSym, val: string) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.cnameImpl = val + +proc constraint*(s: PSym): PNode {.inline.} = + if s.state == Partial: loadSym(s) + result = s.constraintImpl + +proc `constraint=`*(s: PSym, val: PNode) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.constraintImpl = val + +proc instantiatedFrom*(s: PSym): PSym {.inline.} = + if s.state == Partial: loadSym(s) + result = s.instantiatedFromImpl + +proc `instantiatedFrom=`*(s: PSym, val: PSym) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.instantiatedFromImpl = val + +proc setSnippet*(s: PSym; val: sink string) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.locImpl.snippet = val + +proc incl*(s: PSym; flag: TSymFlag) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.flagsImpl.incl(flag) + +proc incl*(s: PSym; flags: set[TSymFlag]) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.flagsImpl.incl(flags) + +proc incl*(s: PSym; flag: TLocFlag) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.locImpl.flags.incl(flag) + +proc excl*(s: PSym; flag: TSymFlag) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.flagsImpl.excl(flag) + +when defined(nimsuggest): + proc allUsages*(s: PSym): seq[TLineInfo] {.inline.} = + if s.state == Partial: loadSym(s) + result = s.allUsagesImpl + + proc `allUsages=`*(s: PSym, val: seq[TLineInfo]) {.inline.} = + assert s.state != Sealed + if s.state == Partial: loadSym(s) + s.allUsagesImpl = val type Gconfig = object # we put comments in a side channel to avoid increasing `sizeof(TNode)`, which @@ -1085,15 +1339,17 @@ proc getDeclPragma*(n: PNode): PNode = proc extractPragma*(s: PSym): PNode = ## gets the pragma node of routine/type/var/let/const symbol `s` if s.kind in routineKinds: # bug #24167 - if s.ast[pragmasPos] != nil and s.ast[pragmasPos].kind != nkEmpty: - result = s.ast[pragmasPos] + let astVal = s.ast + if astVal != nil and astVal[pragmasPos] != nil and astVal[pragmasPos].kind != nkEmpty: + result = astVal[pragmasPos] else: result = nil elif s.kind in {skType, skVar, skLet, skConst}: - if s.ast != nil and s.ast.len > 0: - if s.ast[0].kind == nkPragmaExpr and s.ast[0].len > 1: + let astVal = s.ast + if astVal != nil and astVal.len > 0: + if astVal[0].kind == nkPragmaExpr and astVal[0].len > 1: # s.ast = nkTypedef / nkPragmaExpr / [nkSym, nkPragma] - result = s.ast[0][1] + result = astVal[0][1] else: result = nil else: @@ -1120,7 +1376,7 @@ when defined(useNodeIds): const nodeIdToDebug* = -1 # 2322968 var gNodeId: int -template newNodeImpl(info2) = +template newNodeImpl(info2) {.dirty.} = result = PNode(kind: kind, info: info2) when false: # this would add overhead, so we skip it; it results in a small amount of leaked entries @@ -1223,8 +1479,8 @@ proc newSym*(symKind: TSymKind, name: PIdent, idgen: IdGenerator; owner: PSym, # generates a symbol and initializes the hash field too assert not name.isNil let id = nextSymId idgen - result = PSym(name: name, kind: symKind, flags: {}, info: info, itemId: id, - options: options, ownerField: owner, offset: defaultOffset, + result = PSym(name: name, kindImpl: symKind, flagsImpl: {}, infoImpl: info, itemId: id, + optionsImpl: options, ownerFieldImpl: owner, offsetImpl: defaultOffset, disamb: getOrDefault(idgen.disambTable, name).int32) idgen.disambTable.inc name when false: @@ -1235,10 +1491,11 @@ proc newSym*(symKind: TSymKind, name: PIdent, idgen: IdGenerator; owner: PSym, proc astdef*(s: PSym): PNode = # get only the definition (initializer) portion of the ast - if s.ast != nil and s.ast.kind in {nkIdentDefs, nkConstDef}: - s.ast[2] + let astVal = s.ast + if astVal != nil and astVal.kind in {nkIdentDefs, nkConstDef}: + astVal[2] else: - s.ast + astVal proc isMetaType*(t: PType): bool = return t.kind in tyMetaTypes or @@ -1250,31 +1507,33 @@ proc isUnresolvedStatic*(t: PType): bool = proc linkTo*(t: PType, s: PSym): PType {.discardable.} = t.sym = s - s.typ = t + s.typImpl = t result = t proc linkTo*(s: PSym, t: PType): PSym {.discardable.} = t.sym = s - s.typ = t + s.typImpl = t result = s template fileIdx*(c: PSym): FileIndex = # XXX: this should be used only on module symbols - c.position.FileIndex + c.position().FileIndex template filename*(c: PSym): string = # XXX: this should be used only on module symbols - c.position.FileIndex.toFilename + c.position().FileIndex.toFilename proc appendToModule*(m: PSym, n: PNode) = ## The compiler will use this internally to add nodes that will be ## appended to the module after the sem pass - if m.ast == nil: - m.ast = newNode(nkStmtList) - m.ast.sons = @[n] + var astVal = m.ast + if astVal == nil: + astVal = newNode(nkStmtList) + astVal.sons = @[n] + m.astImpl = astVal else: - assert m.ast.kind == nkStmtList - m.ast.sons.add(n) + assert astVal.kind == nkStmtList + astVal.sons.add(n) const # for all kind of hash tables: GrowthFactor* = 2 # must be power of 2, > 0 @@ -1576,9 +1835,11 @@ proc assignType*(dest, src: PType) = # this fixes 'type TLock = TSysLock': if src.sym != nil: if dest.sym != nil: - dest.sym.flags.incl src.sym.flags-{sfUsed, sfExported} - if dest.sym.annex == nil: dest.sym.annex = src.sym.annex - mergeLoc(dest.sym.loc, src.sym.loc) + var destFlags = dest.sym.flags + var srcFlags = src.sym.flags + dest.sym.flagsImpl = destFlags + (srcFlags - {sfUsed, sfExported}) + if dest.sym.annex == nil: dest.sym.annexImpl = src.sym.annex + mergeLoc(dest.sym.locImpl, src.sym.loc) else: dest.sym = src.sym newSons(dest, src.sons.len) @@ -1598,31 +1859,31 @@ proc exactReplica*(t: PType): PType = proc copySym*(s: PSym; idgen: IdGenerator): PSym = result = newSym(s.kind, s.name, idgen, s.owner, s.info, s.options) - #result.ast = nil # BUGFIX; was: s.ast which made problems - result.typ = s.typ - result.flags = s.flags - result.magic = s.magic - result.options = s.options - result.position = s.position - result.loc = s.loc - result.annex = s.annex # BUGFIX - result.constraint = s.constraint + #result.astImpl = nil # BUGFIX; was: s.ast which made problems + result.typImpl = s.typ + result.flagsImpl = s.flags + result.magicImpl = s.magic + result.optionsImpl = s.options + result.positionImpl = s.position + result.locImpl = s.loc + result.annexImpl = s.annex # BUGFIX + result.constraintImpl = s.constraint if result.kind in {skVar, skLet, skField}: - result.guard = s.guard - result.bitsize = s.bitsize - result.alignment = s.alignment + result.guardImpl = s.guard + result.bitsizeImpl = s.bitsize + result.alignmentImpl = s.alignment proc createModuleAlias*(s: PSym, idgen: IdGenerator, newIdent: PIdent, info: TLineInfo; options: TOptions): PSym = result = newSym(s.kind, newIdent, idgen, s.owner, info, options) # keep ID! - result.ast = s.ast + result.astImpl = s.ast #result.id = s.id # XXX figure out what to do with the ID. - result.flags = s.flags - result.options = s.options - result.position = s.position - result.loc = s.loc - result.annex = s.annex + result.flagsImpl = s.flags + result.optionsImpl = s.options + result.positionImpl = s.position + result.locImpl = s.loc + result.annexImpl = s.annex proc initStrTable*(): TStrTable = result = TStrTable(counter: 0) @@ -1748,28 +2009,28 @@ proc transitionNoneToSym*(n: PNode) = template transitionSymKindCommon*(k: TSymKind) = let obj {.inject.} = s[] - s[] = TSym(kind: k, itemId: obj.itemId, magic: obj.magic, typ: obj.typ, name: obj.name, - info: obj.info, ownerField: obj.ownerField, flags: obj.flags, ast: obj.ast, - options: obj.options, position: obj.position, offset: obj.offset, - loc: obj.loc, annex: obj.annex, constraint: obj.constraint) + s[] = TSym(kindImpl: k, itemId: obj.itemId, magicImpl: obj.magicImpl, typImpl: obj.typImpl, name: obj.name, + infoImpl: obj.infoImpl, ownerFieldImpl: obj.ownerFieldImpl, flagsImpl: obj.flagsImpl, astImpl: obj.astImpl, + optionsImpl: obj.optionsImpl, positionImpl: obj.positionImpl, offsetImpl: obj.offsetImpl, + locImpl: obj.locImpl, annexImpl: obj.annexImpl, constraintImpl: obj.constraintImpl) when hasFFI: - s.cname = obj.cname + s.cnameImpl = obj.cnameImpl when defined(nimsuggest): - s.allUsages = obj.allUsages + s.allUsagesImpl = obj.allUsagesImpl proc transitionGenericParamToType*(s: PSym) = transitionSymKindCommon(skType) proc transitionRoutineSymKind*(s: PSym, kind: range[skProc..skTemplate]) = transitionSymKindCommon(kind) - s.gcUnsafetyReason = obj.gcUnsafetyReason - s.transformedBody = obj.transformedBody + s.gcUnsafetyReasonImpl = obj.gcUnsafetyReasonImpl + s.transformedBodyImpl = obj.transformedBodyImpl proc transitionToLet*(s: PSym) = transitionSymKindCommon(skLet) - s.guard = obj.guard - s.bitsize = obj.bitsize - s.alignment = obj.alignment + s.guardImpl = obj.guardImpl + s.bitsizeImpl = obj.bitsizeImpl + s.alignmentImpl = obj.alignmentImpl template copyNodeImpl(dst, src, processSonsStmt) = if src == nil: return @@ -2061,7 +2322,7 @@ template incompleteType*(t: PType): bool = t.sym != nil and {sfForward, sfNoForward} * t.sym.flags == {sfForward} template typeCompleted*(s: PSym) = - incl s.flags, sfNoForward + incl s, sfNoForward template detailedInfo*(sym: PSym): string = sym.name.s diff --git a/compiler/ast2nif.nim b/compiler/ast2nif.nim new file mode 100644 index 0000000000000..e49f59ab200fb --- /dev/null +++ b/compiler/ast2nif.nim @@ -0,0 +1,895 @@ +# +# +# The Nim Compiler +# (c) Copyright 2025 Andreas Rumpf +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## AST to NIF bridge. + +import std / [assertions, tables, sets] +from std / strutils import startsWith +import ast, idents, msgs, options +import lineinfos as astli +import pathutils +import "../dist/nimony/src/lib" / [bitabs, nifstreams, nifcursors, lineinfos, + nifindexes, nifreader] +import "../dist/nimony/src/gear2" / modnames + +import icnif / [enum2nif] + +# ---------------- Line info handling ----------------------------------------- + +type + LineInfoWriter = object + fileK: FileIndex # remember the current pair, even faster than the hash table + fileV: FileId + tab: Table[FileIndex, FileId] + revTab: Table[FileId, FileIndex] # reverse mapping for oldLineInfo + man: LineInfoManager + config: ConfigRef + +proc get(w: var LineInfoWriter; key: FileIndex): FileId = + if w.fileK == key: + result = w.fileV + else: + if key in w.tab: + result = w.tab[key] + w.fileK = key + w.fileV = result + else: + result = pool.files.getOrIncl(msgs.toFullPath(w.config, key)) + w.fileK = key + w.fileV = result + w.tab[key] = result + w.revTab[result] = key + +proc nifLineInfo(w: var LineInfoWriter; info: TLineInfo): PackedLineInfo = + if info == unknownLineInfo: + result = NoLineInfo + else: + let fid = get(w, info.fileIndex) + result = pack(w.man, fid, info.line.int32, info.col) + +proc oldLineInfo(w: var LineInfoWriter; info: PackedLineInfo): TLineInfo = + if info == NoLineInfo: + result = unknownLineInfo + else: + var x = unpack(w.man, info) + var fileIdx: FileIndex + if w.fileV == x.file: + fileIdx = w.fileK + elif x.file in w.revTab: + fileIdx = w.revTab[x.file] + else: + # Need to look up FileId -> FileIndex via the file path + let filePath = pool.files[x.file] + fileIdx = msgs.fileInfoIdx(w.config, AbsoluteFile filePath) + w.revTab[x.file] = fileIdx + result = TLineInfo(line: x.line.uint16, col: x.col.int16, fileIndex: fileIdx) + + +# -------------- Module name handling -------------------------------------------- + +proc modname(moduleToNifSuffix: var Table[FileIndex, string]; module: int; conf: ConfigRef): string = + let idx = module.FileIndex + # copied from ../nifgen.nim + result = moduleToNifSuffix.getOrDefault(idx) + if result.len == 0: + let fp = toFullPath(conf, idx) + result = moduleSuffix(fp, cast[seq[string]](conf.searchPaths)) + moduleToNifSuffix[idx] = result + #echo result, " -> ", fp + +proc modname(moduleToNifSuffix: var Table[FileIndex, string]; module: PSym; conf: ConfigRef): string = + assert module.kind == skModule + result = modname(moduleToNifSuffix, module.position, conf) + + + +# ------------- Writer --------------------------------------------------------------- + +#[ + +Strategy: + +We produce NIF from the PNode structure as the single source of truth. NIF nodes can +however, refer to PSym and PType, these get NIF names. If the PSym/PType belongs to +the module that we are currently writing, we emit these fields as an inner NIF +structure via the special tags `sd` and `td`. In fact it is only these tags +that get the NIF `SymbolDef` kinds so that the lazy loading mechanism cannot +be confused. + +We could also emit non-local symbols and types later as the index structure +will tell us the precise offsets anyway. + +]# + +let + sdefTag = registerTag("sd") + tdefTag = registerTag("td") + tuseTag = registerTag("t") + hiddenTypeTag = registerTag("ht") + +type + Writer = object + deps: TokenBuf # include&import deps + infos: LineInfoWriter + currentModule: int32 + decodedFileIndices: HashSet[FileIndex] + moduleToNifSuffix: Table[FileIndex, string] + locals: HashSet[ItemId] # track proc-local symbols + inProc: int + +proc toNifSymName(w: var Writer; sym: PSym): string = + ## Generate NIF name for a symbol: local names are `ident.disamb`, + ## global names are `ident.disamb.moduleSuffix` + result = sym.name.s + result.add '.' + result.addInt sym.disamb + if sym.itemId notin w.locals: + # Global symbol: ident.disamb.moduleSuffix + let module = sym.itemId.module + result.add '.' + result.add modname(w.moduleToNifSuffix, module, w.infos.config) + +type + ParsedSymName* = object + name*: string + module*: string + count*: int + +proc parseSymName*(s: string): ParsedSymName = + var i = s.len - 2 + while i > 0: + if s[i] == '.': + if s[i+1] in {'0'..'9'}: + var count = ord(s[i+1]) - ord('0') + var j = i+2 + while j < s.len and s[j] in {'0'..'9'}: + count = count * 10 + ord(s[j]) - ord('0') + inc j + return ParsedSymName(name: substr(s, 0, i-1), module: "", count: count) + else: + let mend = s.high + var b = i-1 + while b > 0 and s[b] != '.': dec b + var j = b+1 + var count = 0 + while j < s.len and s[j] in {'0'..'9'}: + count = count * 10 + ord(s[j]) - ord('0') + inc j + + return ParsedSymName(name: substr(s, 0, b-1), module: substr(s, i+1, mend), count: count) + dec i + return ParsedSymName(name: s, module: "") + +template buildTree(dest: var TokenBuf; tag: TagId; body: untyped) = + dest.addParLe tag + body + dest.addParRi + +template buildTree(dest: var TokenBuf; tag: string; body: untyped) = + buildTree dest, pool.tags.getOrIncl(tag), body + +proc writeFlags[E](dest: var TokenBuf; flags: set[E]) = + var flagsAsIdent = "" + genFlags(flags, flagsAsIdent) + if flagsAsIdent.len > 0: + dest.addIdent flagsAsIdent + else: + dest.addDotToken + +proc trLineInfo(w: var Writer; info: TLineInfo): PackedLineInfo {.inline.} = + result = nifLineInfo(w.infos, info) + +proc writeNode(w: var Writer; dest: var TokenBuf; n: PNode) +proc writeType(w: var Writer; dest: var TokenBuf; typ: PType) +proc writeSym(w: var Writer; dest: var TokenBuf; sym: PSym) + +proc typeToNifSym(w: var Writer; typ: PType): string = + result = "`t" + result.addInt ord(typ.kind) + result.add '.' + result.addInt typ.uniqueId.item + result.add '.' + result.add modname(w.moduleToNifSuffix, typ.uniqueId.module, w.infos.config) + +proc writeLoc(w: var Writer; dest: var TokenBuf; loc: TLoc) = + dest.addIdent toNifTag(loc.k) + dest.addIdent toNifTag(loc.storage) + writeFlags(dest, loc.flags) # TLocFlags + dest.addStrLit loc.snippet + +proc writeTypeDef(w: var Writer; dest: var TokenBuf; typ: PType) = + dest.buildTree tdefTag: + dest.addSymDef pool.syms.getOrIncl(w.typeToNifSym(typ)), NoLineInfo + + #dest.addIdent toNifTag(typ.kind) + writeFlags(dest, typ.flags) + dest.addIdent toNifTag(typ.callConv) + dest.addIntLit typ.size + dest.addIntLit typ.align + dest.addIntLit typ.paddingAtEnd + dest.addIntLit typ.itemId.item # nonUniqueId + + writeType(w, dest, typ.typeInst) + writeNode(w, dest, typ.n) + writeSym(w, dest, typ.owner) + writeSym(w, dest, typ.sym) + + # Write TLoc structure + writeLoc w, dest, typ.loc + # we store the type's elements here at the end so that + # it is not ambiguous and saves space: + for ch in typ.kids: + writeType(w, dest, ch) + + +proc writeType(w: var Writer; dest: var TokenBuf; typ: PType) = + if typ == nil: + dest.addDotToken() + elif typ.itemId.module == w.currentModule and typ.state == Complete: + typ.state = Sealed + writeTypeDef(w, dest, typ) + else: + dest.buildTree tuseTag: + dest.addSymUse pool.syms.getOrIncl(w.typeToNifSym(typ)), NoLineInfo + +proc writeBool(dest: var TokenBuf; b: bool) = + dest.buildTree (if b: "true" else: "false"): + discard + +proc writeLib(w: var Writer; dest: var TokenBuf; lib: PLib) = + if lib == nil: + dest.addDotToken() + else: + dest.buildTree toNifTag(lib.kind): + dest.writeBool lib.generated + dest.writeBool lib.isOverridden + dest.addStrLit lib.name + writeNode w, dest, lib.path + +proc writeSymDef(w: var Writer; dest: var TokenBuf; sym: PSym) = + dest.addParLe sdefTag, trLineInfo(w, sym.info) + dest.addSymDef pool.syms.getOrIncl(w.toNifSymName(sym)), NoLineInfo + if sym.magic == mNone: + dest.addDotToken + else: + dest.addIdent toNifTag(sym.magic) + writeFlags(dest, sym.flags) + writeFlags(dest, sym.options) + dest.addIntLit sym.offset + # field `disamb` made part of the name, so do not store it here + dest.buildTree sym.kind.toNifTag: + case sym.kind + of skLet, skVar, skField, skForVar: + writeSym(w, dest, sym.guard) + dest.addIntLit sym.bitsize + dest.addIntLit sym.alignment + else: + discard + if sym.kind == skModule: + dest.addDotToken() # position will be set by the loader! + else: + dest.addIntLit sym.position + writeType(w, dest, sym.typ) + writeSym(w, dest, sym.owner) + # We do not store `sym.ast` here but instead set it in the deserializer + #writeNode(w, sym.ast) + writeLoc w, dest, sym.loc + writeNode(w, dest, sym.constraint) + writeSym(w, dest, sym.instantiatedFrom) + dest.addParRi + +proc writeSym(w: var Writer; dest: var TokenBuf; sym: PSym) = + if sym == nil: + dest.addDotToken() + elif sym.itemId.module == w.currentModule and sym.state == Complete: + sym.state = Sealed + writeSymDef(w, dest, sym) + else: + # NIF has direct support for symbol references so we don't need to use a tag here, + # unlike what we do for types! + dest.addSymUse pool.syms.getOrIncl(w.toNifSymName(sym)), NoLineInfo + +proc writeSymNode(w: var Writer; dest: var TokenBuf; n: PNode; sym: PSym) = + if sym == nil: + dest.addDotToken() + elif sym.itemId.module == w.currentModule and sym.state == Complete: + sym.state = Sealed + if n.typ != n.sym.typ: + dest.buildTree hiddenTypeTag, trLineInfo(w, n.info): + writeSymDef(w, dest, sym) + else: + writeSymDef(w, dest, sym) + else: + # NIF has direct support for symbol references so we don't need to use a tag here, + # unlike what we do for types! + let info = trLineInfo(w, n.info) + if n.typ != n.sym.typ: + dest.buildTree hiddenTypeTag, info: + dest.addSymUse pool.syms.getOrIncl(w.toNifSymName(sym)), info + else: + dest.addSymUse pool.syms.getOrIncl(w.toNifSymName(sym)), info + +proc writeNodeFlags(dest: var TokenBuf; flags: set[TNodeFlag]) {.inline.} = + writeFlags(dest, flags) + +template withNode(w: var Writer; dest: var TokenBuf; n: PNode; body: untyped) = + dest.addParLe pool.tags.getOrIncl(toNifTag(n.kind)), trLineInfo(w, n.info) + writeNodeFlags(dest, n.flags) + writeType(w, dest, n.typ) + body + dest.addParRi + +proc addLocalSym(w: var Writer; n: PNode) = + ## Add symbol from a node to locals set if it's a symbol node + if n != nil and n.kind == nkSym and n.sym != nil and w.inProc > 0: + w.locals.incl(n.sym.itemId) + +proc addLocalSyms(w: var Writer; n: PNode) = + if n.kind in {nkIdentDefs, nkVarTuple}: + # nkIdentDefs: [ident1, ident2, ..., type, default] + # All children except the last two are identifiers + for i in 0 ..< max(0, n.len - 2): + addLocalSyms(w, n[i]) + elif n.kind == nkSym: + addLocalSym(w, n) + +proc trInclude(w: var Writer; n: PNode) = + w.deps.addParLe pool.tags.getOrIncl(toNifTag(n.kind)), trLineInfo(w, n.info) + for child in n: + assert child.kind == nkStrLit + w.deps.addStrLit child.strVal + w.deps.addParRi + +proc trImport(w: var Writer; n: PNode) = + w.deps.addParLe pool.tags.getOrIncl(toNifTag(n.kind)), trLineInfo(w, n.info) + for child in n: + assert child.kind == nkSym + let s = child.sym + assert s.kind == skModule + let fp = toFullPath(w.infos.config, s.position.FileIndex) + w.deps.addStrLit fp + w.deps.addParRi + +proc writeNode(w: var Writer; dest: var TokenBuf; n: PNode) = + if n == nil: + dest.addDotToken + else: + case n.kind: + of nkEmpty: + let info = trLineInfo(w, n.info) + dest.addParLe pool.tags.getOrIncl(toNifTag(nkEmpty)), info + writeNodeFlags(dest, n.flags) + dest.addParRi + of nkIdent: + # nkIdent uses flags and typ when it is a generic parameter + w.withNode dest, n: + dest.addIdent n.ident.s + of nkSym: + writeSymNode(w, dest, n, n.sym) + of nkCharLit: + w.withNode dest, n: + dest.add charToken(n.intVal.char, NoLineInfo) + of nkIntLit .. nkInt64Lit: + w.withNode dest, n: + dest.addIntLit n.intVal + of nkUIntLit .. nkUInt64Lit: + w.withNode dest, n: + dest.addUIntLit cast[BiggestUInt](n.intVal) + of nkFloatLit .. nkFloat128Lit: + w.withNode dest, n: + dest.add floatToken(pool.floats.getOrIncl(n.floatVal), NoLineInfo) + of nkStrLit .. nkTripleStrLit: + w.withNode dest, n: + dest.addStrLit n.strVal + of nkNilLit: + w.withNode dest, n: + discard + of nkLetSection, nkVarSection, nkConstSection, nkGenericParams: + # Track local variables declared in let/var sections + w.withNode dest, n: + for child in n: + addLocalSyms w, child + # Process the child node + writeNode(w, dest, child) + of nkForStmt, nkTypeDef: + # Track for loop variable (first child is the loop variable) + w.withNode dest, n: + if n.len > 0: + addLocalSyms(w, n[0]) + for i in 0 ..< n.len: + writeNode(w, dest, n[i]) + of nkFormalParams: + # Track parameters (first child is return type, rest are parameters) + w.withNode dest, n: + for i in 0 ..< n.len: + if i > 0: # Skip return type + addLocalSyms(w, n[i]) + writeNode(w, dest, n[i]) + of nkProcDef, nkFuncDef, nkMethodDef, nkIteratorDef, nkConverterDef, nkLambda, nkDo, nkMacroDef: + inc w.inProc + # Entering a proc/function body - parameters are local + var ast = n + if n[namePos].kind == nkSym: + ast = n[namePos].sym.ast + w.withNode dest, ast: + # Process body and other parts + for i in 0 ..< ast.len: + writeNode(w, dest, ast[i]) + dec w.inProc + of nkImportStmt: + # this has been transformed for us, see `importer.nim` to contain a list of module syms: + trImport w, n + of nkIncludeStmt: + trInclude w, n + else: + w.withNode dest, n: + for i in 0 ..< n.len: + writeNode(w, dest, n[i]) + +proc writeToplevelNode(w: var Writer; outer, inner: var TokenBuf; n: PNode) = + case n.kind + of nkStmtList, nkStmtListExpr: + for son in n: writeToplevelNode(w, outer, inner, son) + of nkProcDef, nkFuncDef, nkMethodDef, nkIteratorDef, nkConverterDef, nkLambda, nkDo, nkMacroDef: + # Delegate to `w.topLevel`! + writeNode w, inner, n + of nkConstSection, nkTypeSection, nkTypeDef: + writeNode w, inner, n + else: + writeNode w, outer, n + +proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode) = + var w = Writer(infos: LineInfoWriter(config: config), currentModule: thisModule) + var outer = createTokenBuf(300) + var inner = createTokenBuf(300) + + let rootInfo = trLineInfo(w, n.info) + outer.addParLe pool.tags.getOrIncl(toNifTag(nkStmtList)), rootInfo + inner.addParLe pool.tags.getOrIncl(toNifTag(nkStmtList)), rootInfo + + w.writeToplevelNode outer, inner, n + + outer.addParRi() + inner.addParRi() + + let m = modname(w.moduleToNifSuffix, w.currentModule, w.infos.config) + let d = toGeneratedFile(config, AbsoluteFile(m), ".nif").string + + var dest = createTokenBuf(600) + dest.addParLe pool.tags.getOrIncl(toNifTag(nkStmtList)), rootInfo + dest.add w.deps + dest.add outer + dest.add inner + dest.addParRi() + + writeFileAndIndex d, dest + + +# --------------------------- Loader (lazy!) ----------------------------------------------- + +proc nodeKind(n: Cursor): TNodeKind {.inline.} = + assert n.kind == ParLe + parse(TNodeKind, pool.tags[n.tagId]) + +proc expect(n: Cursor; k: set[NifKind]) = + if n.kind notin k: + when defined(debug): + writeStackTrace() + quit "[NIF decoder] expected: " & $k & " but got: " & $n.kind & toString n + +proc expect(n: Cursor; k: NifKind) {.inline.} = + expect n, {k} + +proc incExpect(n: var Cursor; k: set[NifKind]) = + inc n + expect n, k + +proc incExpect(n: var Cursor; k: NifKind) {.inline.} = + incExpect n, {k} + +proc skipParRi(n: var Cursor) = + expect n, {ParRi} + inc n + +proc firstSon*(n: Cursor): Cursor {.inline.} = + result = n + inc result + +proc expectTag(n: Cursor; tagId: TagId) = + if n.kind == ParLe and n.tagId == tagId: + discard + else: + when defined(debug): + writeStackTrace() + if n.kind != ParLe: + quit "[NIF decoder] expected: ParLe but got: " & $n.kind & toString n + else: + quit "[NIF decoder] expected: " & pool.tags[tagId] & " but got: " & pool.tags[n.tagId] & toString n + +proc incExpectTag(n: var Cursor; tagId: TagId) = + inc n + expectTag(n, tagId) + +proc loadBool(n: var Cursor): bool = + if n.kind == ParLe: + result = pool.tags[n.tagId] == "true" + inc n + skipParRi n + else: + raiseAssert "(true)/(false) expected" + +type + NifModule = object + stream: nifstreams.Stream + symCounter: int32 + index: NifIndex + + DecodeContext* = object + infos: LineInfoWriter + moduleIds: Table[string, int32] + types: Table[ItemId, (PType, NifIndexEntry)] + syms: Table[ItemId, (PSym, NifIndexEntry)] + mods: seq[NifModule] + cache: IdentCache + moduleToNifSuffix: Table[FileIndex, string] + +proc createDecodeContext*(config: ConfigRef; cache: IdentCache): DecodeContext = + ## Supposed to be a global variable + result = DecodeContext(infos: LineInfoWriter(config: config), cache: cache) + +proc idToIdx(x: int32): int {.inline.} = + assert x <= -2'i32 + result = -(x+2) + +proc cursorFromIndexEntry(c: var DecodeContext; module: int32; entry: NifIndexEntry; + buf: var TokenBuf): Cursor = + let m = idToIdx(module) + let s = addr c.mods[m].stream + s.r.jumpTo entry.offset + var buf = createTokenBuf(30) + nifcursors.parse(s[], buf, entry.info) + result = cursorAt(buf, 0) + +proc moduleId(c: var DecodeContext; suffix: string): int32 = + # We don't know the "real" FileIndex due to our mapping to a short "Module suffix" + # This is not a problem, we use negative `ItemId.module` values here and then + # there is no interference with in-memory-modules. Modulegraphs.nim already uses -1 + # so we start at -2 here. + result = c.moduleIds.getOrDefault(suffix) + if result == 0: + result = -int32(c.moduleIds.len + 2) # negative index! + let modFile = (getNimcacheDir(c.infos.config) / RelativeFile(suffix & ".nif")).string + let idxFile = (getNimcacheDir(c.infos.config) / RelativeFile(suffix & ".idx.nif")).string + c.moduleIds[suffix] = result + c.mods.add NifModule(stream: nifstreams.open(modFile), index: readIndex(idxFile)) + assert c.mods.len-1 == idToIdx(result) + +proc getOffset(c: var DecodeContext; module: int32; nifName: string): NifIndexEntry = + assert module < 0'i32 + let index = idToIdx(module) + let ii = addr c.mods[index].index + result = ii.public.getOrDefault(nifName) + if result.offset == 0: + result = ii.private.getOrDefault(nifName) + if result.offset == 0: + raiseAssert "symbol has no offset: " & nifName + +proc loadNode(c: var DecodeContext; n: var Cursor): PNode + +proc loadTypeStub(c: var DecodeContext; t: SymId): PType = + let name = pool.syms[t] + assert name.startsWith("`t") + var i = len("`t") + var k = 0 + while i < name.len and name[i] in {'0'..'9'}: + k = k * 10 + name[i].ord - ord('0') + inc i + if i < name.len and name[i] == '.': inc i + var itemId = 0'i32 + while i < name.len and name[i] in {'0'..'9'}: + itemId = itemId * 10'i32 + int32(name[i].ord - ord('0')) + inc i + if i < name.len and name[i] == '.': inc i + let suffix = name.substr(i) + let id = ItemId(module: moduleId(c, suffix), item: itemId) + result = c.types.getOrDefault(id)[0] + if result == nil: + let offs = c.getOffset(id.module, name) + result = PType(itemId: id, uniqueId: id, kind: TTypeKind(k), state: Partial) + c.types[id] = (result, offs) + +proc loadTypeStub(c: var DecodeContext; n: var Cursor): PType = + if n.kind == DotToken: + result = nil + inc n + elif n.kind == Symbol: + let s = n.symId + result = loadTypeStub(c, s) + inc n + elif n.kind == ParLe and n.tagId == tdefTag: + let s = n.firstSon.symId + skip n + result = loadTypeStub(c, s) + else: + raiseAssert "type expected but got " & $n.kind + +proc loadSymStub(c: var DecodeContext; t: SymId): PSym = + let symAsStr = pool.syms[t] + let sn = parseSymName(symAsStr) + let module = moduleId(c, sn.module) + let val = addr c.mods[idToIdx(module)].symCounter + inc val[] + + let id = ItemId(module: module, item: val[]) + result = c.syms.getOrDefault(id)[0] + if result == nil: + let offs = c.getOffset(module, symAsStr) + result = PSym(itemId: id, kindImpl: skStub, name: c.cache.getIdent(sn.name), disamb: sn.count.int32, state: Partial) + c.syms[id] = (result, offs) + +proc loadSymStub(c: var DecodeContext; n: var Cursor): PSym = + if n.kind == DotToken: + result = nil + inc n + elif n.kind == Symbol: + let s = n.symId + result = loadSymStub(c, s) + inc n + elif n.kind == ParLe and n.tagId == sdefTag: + let s = n.firstSon.symId + skip n + result = loadSymStub(c, s) + else: + raiseAssert "sym expected but got " & $n.kind + +proc isStub*(t: PType): bool {.inline.} = t.state == Partial +proc isStub*(s: PSym): bool {.inline.} = s.state == Partial + +proc loadAtom[T](t: typedesc[set[T]]; n: var Cursor): set[T] = + if n.kind == DotToken: + result = {} + inc n + else: + expect n, Ident + result = parse(T, pool.strings[n.litId]) + inc n + +proc loadAtom[T: enum](t: typedesc[T]; n: var Cursor): T = + if n.kind == DotToken: + result = default(T) + inc n + else: + expect n, Ident + result = parse(T, pool.strings[n.litId]) + inc n + +proc loadAtom(t: typedesc[string]; n: var Cursor): string = + expect n, StringLit + result = pool.strings[n.litId] + inc n + +proc loadAtom[T: int16|int32|int64](t: typedesc[T]; n: var Cursor): T = + expect n, IntLit + result = pool.integers[n.intId].T + inc n + +template loadField(field) = + field = loadAtom(typeof(field), n) + +proc loadLoc(c: var DecodeContext; n: var Cursor; loc: var TLoc) = + loadField loc.k + loadField loc.storage + loadField loc.flags + loadField loc.snippet + +proc loadType*(c: var DecodeContext; t: PType) = + if t.state != Partial: return + t.state = Sealed + var buf = createTokenBuf(30) + var n = cursorFromIndexEntry(c, t.itemId.module, c.types[t.itemId][1], buf) + + expect n, ParLe + if n.tagId != tdefTag: + raiseAssert "(td) expected" + inc n + expect n, SymbolDef + # ignore the type's name, we have already used it to create this PType's itemId! + inc n + #loadField t.kind + loadField t.flags + loadField t.callConv + loadField t.size + loadField t.align + loadField t.paddingAtEnd + loadField t.itemId.item + + t.typeInst = loadTypeStub(c, n) + t.n = loadNode(c, n) + t.setOwner loadSymStub(c, n) + t.sym = loadSymStub(c, n) + loadLoc c, n, t.loc + + var kids: seq[PType] = @[] + while n.kind != ParRi: + kids.add loadTypeStub(c, n) + + t.setSons kids + + skipParRi n + +proc loadAnnex(c: var DecodeContext; n: var Cursor): PLib = + if n.kind == DotToken: + result = nil + inc n + elif n.kind == ParLe: + result = PLib(kind: parse(TLibKind, pool.tags[n.tagId])) + inc n + result.generated = loadBool(n) + result.isOverridden = loadBool(n) + expect n, StringLit + result.name = pool.strings[n.litId] + inc n + result.path = loadNode(c, n) + skipParRi n + else: + raiseAssert "`lib/annex` information expected" + +proc loadSym*(c: var DecodeContext; s: PSym) = + if s.state != Partial: return + s.state = Sealed + var buf = createTokenBuf(30) + var n = cursorFromIndexEntry(c, s.itemId.module, c.syms[s.itemId][1], buf) + + expect n, ParLe + if n.tagId != sdefTag: + raiseAssert "(sd) expected" + inc n + expect n, SymbolDef + # ignore the symbol's name, we have already used it to create this PSym instance! + inc n + loadField s.magic + loadField s.flags + loadField s.options + loadField s.offset + + expect n, ParLe + s.kind = parse(TSymKind, pool.tags[n.tagId]) + inc n + + case s.kind + of skLet, skVar, skField, skForVar: + s.guard = loadSymStub(c, n) + loadField s.bitsize + loadField s.alignment + else: + discard + skipParRi n + + if s.kind == skModule: + expect n, DotToken + inc n + else: + loadField s.position + s.typ = loadTypeStub(c, n) + s.setOwner loadSymStub(c, n) + # We do not store `sym.ast` here but instead set it in the deserializer + #writeNode(w, sym.ast) + loadLoc c, n, s.locImpl + s.constraint = loadNode(c, n) + s.instantiatedFrom = loadSymStub(c, n) + skipParRi n + + +template withNode(c: var DecodeContext; n: var Cursor; result: PNode; kind: TNodeKind; body: untyped) = + let info = c.infos.oldLineInfo(n.info) + let flags = loadAtom(TNodeFlags, n) + result = newNodeI(kind, info) + result.flags = flags + result.typ = c.loadTypeStub n + body + skipParRi n + +proc loadNode(c: var DecodeContext; n: var Cursor): PNode = + result = nil + case n.kind: + of DotToken: + result = nil + inc n + of ParLe: + let kind = n.nodeKind + case kind: + of nkEmpty: + result = newNodeI(nkEmpty, c.infos.oldLineInfo(n.info)) + result.flags = loadAtom(TNodeFlags, n) + skipParRi n + of nkIdent: + let info = c.infos.oldLineInfo(n.info) + let flags = loadAtom(TNodeFlags, n) + let typ = c.loadTypeStub n + expect n, Ident + result = newIdentNode(c.cache.getIdent(pool.strings[n.litId]), info) + inc n + result.flags = flags + result.typ = typ + skipParRi n + of nkSym: + c.withNode n, result, kind: + result.sym = c.loadSymStub n + of nkCharLit: + c.withNode n, result, kind: + expect n, CharLit + result.intVal = n.charLit.int + inc n + of nkIntLit .. nkInt64Lit: + c.withNode n, result, kind: + expect n, IntLit + result.intVal = pool.integers[n.intId] + inc n + of nkUIntLit .. nkUInt64Lit: + c.withNode n, result, kind: + expect n, UIntLit + result.intVal = cast[BiggestInt](pool.uintegers[n.uintId]) + inc n + of nkFloatLit .. nkFloat128Lit: + c.withNode n, result, kind: + if n.kind == FloatLit: + result.floatVal = pool.floats[n.floatId] + inc n + elif n.kind == ParLe: + case pool.tags[n.tagId] + of "inf": + result.floatVal = Inf + of "nan": + result.floatVal = NaN + of "neginf": + result.floatVal = NegInf + else: + raiseAssert "expected float literal but got " & pool.tags[n.tagId] + inc n + skipParRi n + else: + raiseAssert "expected float literal but got " & $n.kind + of nkStrLit .. nkTripleStrLit: + c.withNode n, result, kind: + expect n, StringLit + result.strVal = pool.strings[n.litId] + inc n + of nkNilLit: + c.withNode n, result, kind: + discard + of nkNone: + raiseAssert "Unknown tag " & pool.tags[n.tagId] + else: + c.withNode n, result, kind: + while n.kind != ParRi: + result.addAllowNil c.loadNode n + else: + raiseAssert "Not yet implemented " & $n.kind + + +proc loadNifModule*(c: var DecodeContext; f: FileIndex): PNode = + let moduleSuffix = modname(c.moduleToNifSuffix, f.int, c.infos.config) + let modFile = toGeneratedFile(c.infos.config, AbsoluteFile(moduleSuffix), ".nif").string + + var buf = createTokenBuf(300) + var s = nifstreams.open(modFile) + # XXX We can optimize this here and only load the top level entries! + try: + nifcursors.parse(s, buf, NoLineInfo) + finally: + nifstreams.close(s) + var n = cursorAt(buf, 0) + result = loadNode(c, n) + +when isMainModule: + import std / syncio + let obj = parseSymName("a.123.sys") + echo obj.name, " ", obj.module, " ", obj.count + let objb = parseSymName("abcdef.0121") + echo objb.name, " ", objb.module, " ", objb.count diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index d3e215ea56097..bb73a9d96fe64 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -3359,8 +3359,9 @@ proc genConstSetup(p: BProc; sym: PSym): bool = useHeader(m, sym) if sym.loc.k == locNone: fillBackendName(p.module, sym) - fillLoc(sym.loc, locData, sym.astdef, OnStatic) - if m.hcrOn: incl(sym.loc.flags, lfIndirect) + ensureMutable sym + fillLoc(sym.locImpl, locData, sym.astdef, OnStatic) + if m.hcrOn: incl(sym, lfIndirect) result = lfNoDecl notin sym.loc.flags proc genConstHeader(m, q: BModule; p: BProc, sym: PSym) = diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 3aedca9a965d7..15fb55c3464b3 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -126,9 +126,10 @@ proc genVarTuple(p: BProc, n: PNode) = let vn = n[i] let v = vn.sym if sfCompileTime in v.flags: continue + ensureMutable v if sfGlobal in v.flags: assignGlobalVar(p, vn, "") - genObjectInit(p, cpsInit, v.typ, v.loc, constructObj) + genObjectInit(p, cpsInit, v.typ, v.locImpl, constructObj) registerTraverseProc(p, v) else: assignLocalVar(p, vn) @@ -142,9 +143,9 @@ proc genVarTuple(p: BProc, n: PNode) = if t.n[i].kind != nkSym: internalError(p.config, n.info, "genVarTuple") mangleRecFieldName(p.module, t.n[i].sym) field.snippet = dotField(rtup, fieldName) - putLocIntoDest(p, v.loc, field) + putLocIntoDest(p, v.locImpl, field) if forHcr or isGlobalInBlock: - hcrGlobals.add((loc: v.loc, tp: CNil)) + hcrGlobals.add((loc: v.locImpl, tp: CNil)) if forHcr: # end the block where the tuple gets initialized @@ -460,7 +461,8 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) = if value.kind != nkEmpty and valueAsRope.len == 0: genLineDir(targetProc, vn) if not isCppCtorCall: - loadInto(targetProc, vn, value, v.loc) + ensureMutable v + loadInto(targetProc, vn, value, v.locImpl) if forHcr: endBlockWith(targetProc): finishBranch(p.s(cpsStmts), hcrInit) @@ -736,7 +738,8 @@ proc genBlock(p: BProc, n: PNode, d: var TLoc) = # named block? assert(n[0].kind == nkSym) var sym = n[0].sym - sym.loc.k = locOther + ensureMutable sym + sym.locImpl.k = locOther sym.position = p.breakIdx+1 expr(p, n[1], d) endSimpleBlock(p, scope) @@ -1250,7 +1253,8 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) = initElifBranch(p.s(cpsStmts), ifStmt, orExpr) if exvar != nil: fillLocalName(p, exvar.sym) - fillLoc(exvar.sym.loc, locTemp, exvar, OnStack) + ensureMutable exvar.sym + fillLoc(exvar.sym.locImpl, locTemp, exvar, OnStack) linefmt(p, cpsStmts, "$1 $2 = T$3_;$n", [getTypeDesc(p.module, exvar.sym.typ), rdLoc(exvar.sym.loc), rope(etmp+1)]) # we handled the error: @@ -1298,7 +1302,8 @@ proc genTryCpp(p: BProc, t: PNode, d: var TLoc) = if isImportedException(typeNode.typ, p.config): let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:` fillLocalName(p, exvar.sym) - fillLoc(exvar.sym.loc, locTemp, exvar, OnStack) + ensureMutable exvar.sym + fillLoc(exvar.sym.locImpl, locTemp, exvar, OnStack) startBlockWith(p): lineCg(p, cpsStmts, "catch ($1& $2) {$n", [getTypeDesc(p.module, typeNode.typ), rdLoc(exvar.sym.loc)]) genExceptBranchBody(t[i][^1]) # exception handler body will duplicated for every type @@ -1389,7 +1394,8 @@ proc genTryCppOld(p: BProc, t: PNode, d: var TLoc) = if t[i][j].isInfixAs(): let exvar = t[i][j][2] # ex1 in `except ExceptType as ex1:` fillLocalName(p, exvar.sym) - fillLoc(exvar.sym.loc, locTemp, exvar, OnUnknown) + ensureMutable exvar.sym + fillLoc(exvar.sym.locImpl, locTemp, exvar, OnUnknown) startBlockWith(p): lineCg(p, cpsStmts, "catch ($1& $2) {$n", [getTypeDesc(p.module, t[i][j][1].typ), rdLoc(exvar.sym.loc)]) else: diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim index cdfa46cdd2d9d..bee0d656a50ea 100644 --- a/compiler/ccgtypes.nim +++ b/compiler/ccgtypes.nim @@ -84,7 +84,8 @@ proc fillBackendName(m: BModule; s: PSym) = if m.hcrOn: result.add '_' result.add(idOrSig(s, m.module.name.s.mangle, m.sigConflicts, m.config)) - s.loc.snippet = result + ensureMutable s + s.locImpl.snippet = result proc fillParamName(m: BModule; s: PSym) = if s.loc.snippet == "": @@ -107,7 +108,8 @@ proc fillParamName(m: BModule; s: PSym) = # and a function called in main or proxy uses `socket` as a parameter name. # That would lead to either needing to reload `proxy` or to overwrite the # executable file for the main module, which is running (or both!) -> error. - s.loc.snippet = res.rope + ensureMutable s + s.locImpl.snippet = res.rope proc fillLocalName(p: BProc; s: PSym) = assert s.kind in skLocalVars+{skTemp} @@ -122,7 +124,8 @@ proc fillLocalName(p: BProc; s: PSym) = elif s.kind != skResult: result.add "_" & rope(counter+1) p.sigConflicts.inc(key) - s.loc.snippet = result + ensureMutable s + s.locImpl.snippet = result proc scopeMangledParam(p: BProc; param: PSym) = ## parameter generation only takes BModule, not a BProc, so we have to @@ -300,12 +303,13 @@ proc addAbiCheck(m: BModule; t: PType, name: Rope) = proc fillResult(conf: ConfigRef; param: PNode, proctype: PType) = - fillLoc(param.sym.loc, locParam, param, "Result", + ensureMutable param.sym + fillLoc(param.sym.locImpl, locParam, param, "Result", OnStack) let t = param.sym.typ if mapReturnType(conf, t) != ctArray and isInvalidReturnType(conf, proctype): - incl(param.sym.loc.flags, lfIndirect) - param.sym.loc.storage = OnUnknown + incl(param.sym.locImpl.flags, lfIndirect) + param.sym.locImpl.storage = OnUnknown proc typeNameOrLiteral(m: BModule; t: PType, literal: string): Rope = if t.sym != nil and sfImportc in t.sym.flags and t.sym.magic == mNone: @@ -524,14 +528,15 @@ proc genMemberProcParams(m: BModule; prc: PSym, superCall, rettype, name, params var types, names, args: seq[string] = @[] if not isCtor: var this = t.n[1].sym + ensureMutable this fillParamName(m, this) - fillLoc(this.loc, locParam, t.n[1], + fillLoc(this.locImpl, locParam, t.n[1], this.paramStorageLoc) if this.typ.kind == tyPtr: - this.loc.snippet = "this" + this.locImpl.snippet = "this" else: - this.loc.snippet = "(*this)" - names.add this.loc.snippet + this.locImpl.snippet = "(*this)" + names.add this.locImpl.snippet types.add getTypeDescWeak(m, this.typ, check, dkParam) let firstParam = if isCtor: 1 else: 2 @@ -545,13 +550,14 @@ proc genMemberProcParams(m: BModule; prc: PSym, superCall, rettype, name, params else: descKind = dkRefParam var typ, name: string + ensureMutable param fillParamName(m, param) - fillLoc(param.loc, locParam, t.n[i], + fillLoc(param.locImpl, locParam, t.n[i], param.paramStorageLoc) if ccgIntroducedPtr(m.config, param, t.returnType) and descKind == dkParam: typ = getTypeDescWeak(m, param.typ, check, descKind) & "*" - incl(param.loc.flags, lfIndirect) - param.loc.storage = OnUnknown + incl(param.locImpl.flags, lfIndirect) + param.locImpl.storage = OnUnknown elif weakDep: typ = getTypeDescWeak(m, param.typ, check, descKind) else: @@ -559,7 +565,7 @@ proc genMemberProcParams(m: BModule; prc: PSym, superCall, rettype, name, params if sfNoalias in param.flags: typ.add("NIM_NOALIAS ") - name = param.loc.snippet + name = param.locImpl.snippet types.add typ names.add name if sfCodegenDecl notin param.flags: @@ -601,14 +607,15 @@ proc genProcParams(m: BModule; t: PType, rettype: var Rope, params: var Builder, else: descKind = dkRefParam if isCompileTimeOnly(param.typ): continue + ensureMutable param fillParamName(m, param) - fillLoc(param.loc, locParam, t.n[i], + fillLoc(param.locImpl, locParam, t.n[i], param.paramStorageLoc) var typ: Rope if ccgIntroducedPtr(m.config, param, t.returnType) and descKind == dkParam: typ = ptrType(getTypeDescWeak(m, param.typ, check, descKind)) - incl(param.loc.flags, lfIndirect) - param.loc.storage = OnUnknown + incl(param.locImpl.flags, lfIndirect) + param.locImpl.storage = OnUnknown elif weakDep: typ = (getTypeDescWeak(m, param.typ, check, descKind)) else: @@ -620,9 +627,9 @@ proc genProcParams(m: BModule; t: PType, rettype: var Rope, params: var Builder, var j = 0 while arr.kind in {tyOpenArray, tyVarargs}: # this fixes the 'sort' bug: - if param.typ.kind in {tyVar, tyLent}: param.loc.storage = OnUnknown + if param.typ.kind in {tyVar, tyLent}: param.locImpl.storage = OnUnknown # need to pass hidden parameter: - params.addParam(paramBuilder, name = param.loc.snippet & "Len_" & $j, typ = NimInt) + params.addParam(paramBuilder, name = param.locImpl.snippet & "Len_" & $j, typ = NimInt) inc(j) arr = arr[0].skipTypes({tySink}) if t.returnType != nil and isInvalidReturnType(m.config, t): @@ -707,7 +714,8 @@ proc genRecordFieldsAux(m: BModule; n: PNode, if field.typ.kind == tyVoid: return #assert(field.ast == nil) let sname = mangleRecFieldName(m, field) - fillLoc(field.loc, locField, n, unionPrefix & sname, OnUnknown) + ensureMutable field + fillLoc(field.locImpl, locField, n, unionPrefix & sname, OnUnknown) # for importcpp'ed objects, we only need to set field.loc, but don't # have to recurse via 'getTypeDescAux'. And not doing so prevents problems # with heavily templatized C++ code: @@ -1155,7 +1163,8 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Builder; asPtr: bool let isCtor = sfConstructor in prc.flags var check = initIntSet() fillBackendName(m, prc) - fillLoc(prc.loc, locProc, prc.ast[namePos], OnUnknown) + ensureMutable prc + fillLoc(prc.locImpl, locProc, prc.ast[namePos], OnUnknown) var memberOp = "#." #only virtual var typ: PType if isCtor: @@ -1187,7 +1196,7 @@ proc genMemberProcHeader(m: BModule; prc: PSym; result: var Builder; asPtr: bool superCall = "" else: if not isCtor: - prc.loc.snippet = "$1$2(@)" % [memberOp, name] + prc.locImpl.snippet = "$1$2(@)" % [memberOp, name] elif superCall != "": superCall = " : " & superCall @@ -1202,14 +1211,15 @@ proc genProcHeader(m: BModule; prc: PSym; result: var Builder; visibility: var D # using static is needed for inline procs var check = initIntSet() fillBackendName(m, prc) - fillLoc(prc.loc, locProc, prc.ast[namePos], OnUnknown) + ensureMutable prc + fillLoc(prc.locImpl, locProc, prc.ast[namePos], OnUnknown) var rettype: Snippet = "" var desc = newBuilder("") genProcParams(m, prc.typ, rettype, desc, check, true, false) let params = extract(desc) # handle the 2 options for hotcodereloading codegen - function pointer # (instead of forward declaration) or header for function body with "_actual" postfix - var name = prc.loc.snippet + var name = prc.locImpl.snippet if not asPtr and isReloadable(m, prc): name.add("_actual") # careful here! don't access ``prc.ast`` as that could reload large parts of @@ -1645,8 +1655,8 @@ proc generateRttiDestructor(g: ModuleGraph; typ: PType; owner: PSym; kind: TType n[bodyPos] = body result.ast = n - incl result.flags, sfFromGeneric - incl result.flags, sfGeneratedOp + incl result.flagsImpl, sfFromGeneric + incl result.flagsImpl, sfGeneratedOp proc genHook(m: BModule; t: PType; info: TLineInfo; op: TTypeAttachedOp; result: var Builder) = let theProc = getAttachedOp(m.g.graph, t, op) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 508e003a55db0..518613c1bdfd5 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -600,7 +600,8 @@ proc initLocalVar(p: BProc, v: PSym, immediateAsgn: bool) = # ``var v = X()`` gets transformed into ``X(&v)``. # Nowadays the logic in ccgcalls deals with this case however. if not immediateAsgn: - constructLoc(p, v.loc) + ensureMutable v + constructLoc(p, v.locImpl) proc getTemp(p: BProc, t: PType, needsInit=false): TLoc = inc(p.labels) @@ -646,8 +647,9 @@ proc localVarDecl(res: var Builder, p: BProc; n: PNode, let s = n.sym if s.loc.k == locNone: fillLocalName(p, s) - fillLoc(s.loc, locLocalVar, n, OnStack) - if s.kind == skLet: incl(s.loc.flags, lfNoDeepCopy) + ensureMutable s + fillLoc(s.locImpl, locLocalVar, n, OnStack) + if s.kind == skLet: incl(s, lfNoDeepCopy) genCLineDir(res, p, n.info, p.config) @@ -707,15 +709,17 @@ proc assignGlobalVar(p: BProc, n: PNode; value: Rope) = let s = n.sym if s.loc.k == locNone: fillBackendName(p.module, s) - fillLoc(s.loc, locGlobalVar, n, OnHeap) - if treatGlobalDifferentlyForHCR(p.module, s): incl(s.loc.flags, lfIndirect) + ensureMutable s + fillLoc(s.locImpl, locGlobalVar, n, OnHeap) + if treatGlobalDifferentlyForHCR(p.module, s): incl(s, lfIndirect) if lfDynamicLib in s.loc.flags: var q = findPendingModule(p.module, s) if q != nil and not containsOrIncl(q.declaredThings, s.id): varInDynamicLib(q, s) else: - s.loc.snippet = mangleDynLibProc(s) + ensureMutable s + s.locImpl.snippet = mangleDynLibProc(s) if value != "": internalError(p.config, n.info, ".dynlib variables cannot have a value") return @@ -755,12 +759,14 @@ proc assignGlobalVar(p: BProc, n: PNode; value: Rope) = genGlobalVarDecl(p.module.s[cfsVars], p, n, td, initializer = initializer) if p.withinLoop > 0 and value == "": # fixes tests/run/tzeroarray: - resetLoc(p, s.loc) + ensureMutable s + resetLoc(p, s.locImpl) proc callGlobalVarCppCtor(p: BProc; v: PSym; vn, value: PNode; didGenTemp: var bool) = let s = vn.sym fillBackendName(p.module, s) - fillLoc(s.loc, locGlobalVar, vn, OnHeap) + ensureMutable s + fillLoc(s.locImpl, locGlobalVar, vn, OnHeap) let td = getTypeDesc(p.module, vn.sym.typ, dkVar) var val = genCppParamsForCtor(p, value, didGenTemp) if didGenTemp: return # generated in the caller @@ -779,7 +785,8 @@ proc fillProcLoc(m: BModule; n: PNode) = let sym = n.sym if sym.loc.k == locNone: fillBackendName(m, sym) - fillLoc(sym.loc, locProc, n, OnStack) + ensureMutable sym + fillLoc(sym.locImpl, locProc, n, OnStack) proc getLabel(p: BProc): TLabel = inc(p.labels) @@ -948,7 +955,8 @@ proc symInDynamicLib(m: BModule, sym: PSym) = var extname = sym.loc.snippet if not isCall: loadDynamicLib(m, lib) var tmp = mangleDynLibProc(sym) - sym.loc.snippet = tmp # from now on we only need the internal name + ensureMutable sym + sym.locImpl.snippet = tmp # from now on we only need the internal name sym.typ.sym = nil # generate a new name inc(m.labels, 2) if isCall: @@ -990,9 +998,10 @@ proc varInDynamicLib(m: BModule, sym: PSym) = var lib = sym.annex var extname = sym.loc.snippet loadDynamicLib(m, lib) - incl(sym.loc.flags, lfIndirect) + incl(sym, lfIndirect) var tmp = mangleDynLibProc(sym) - sym.loc.snippet = tmp # from now on we only need the internal name + ensureMutable sym + sym.locImpl.snippet = tmp # from now on we only need the internal name inc(m.labels, 2) let t = ptrType(getTypeDesc(m, sym.typ, dkVar)) # cgsym has side effects, do it first: @@ -1005,7 +1014,8 @@ proc varInDynamicLib(m: BModule, sym: PSym) = m.s[cfsVars].addVar(name = sym.loc.snippet, typ = t) proc symInDynamicLibPartial(m: BModule, sym: PSym) = - sym.loc.snippet = mangleDynLibProc(sym) + ensureMutable sym + sym.locImpl.snippet = mangleDynLibProc(sym) sym.typ.sym = nil # generate a new name proc cgsymImpl(m: BModule; sym: PSym) {.inline.} = @@ -1300,7 +1310,7 @@ proc genProcAux*(m: BModule, prc: PSym) = let resNode = prc.ast[resultPos] let res = resNode.sym # get result symbol if not isInvalidReturnType(m.config, prc.typ) and sfConstructor notin prc.flags: - if sfNoInit in prc.flags: incl(res.flags, sfNoInit) + if sfNoInit in prc.flags: incl(res, sfNoInit) if sfNoInit in prc.flags and p.module.compileToCpp and (let val = easyResultAsgn(procBody); val != nil): var a: TLoc = initLocExprSingleUse(p, val) let ra = rdLoc(a) @@ -1321,9 +1331,11 @@ proc genProcAux*(m: BModule, prc: PSym) = returnBuilder.addReturn(rres) returnStmt = extract(returnBuilder) elif sfConstructor in prc.flags: - resNode.sym.loc.flags.incl lfIndirect - fillLoc(resNode.sym.loc, locParam, resNode, "this", OnHeap) - prc.loc.snippet = getTypeDesc(m, resNode.sym.loc.t, dkVar) + resNode.sym.incl lfIndirect + ensureMutable resNode.sym + fillLoc(resNode.sym.locImpl, locParam, resNode, "this", OnHeap) + ensureMutable prc + prc.locImpl.snippet = getTypeDesc(m, resNode.sym.locImpl.t, dkVar) else: fillResult(p.config, resNode, prc.typ) assignParam(p, res, prc.typ.returnType) @@ -1336,10 +1348,12 @@ proc genProcAux*(m: BModule, prc: PSym) = if sfNoInit in prc.flags: discard elif allPathsAsgnResult(p, procBody) == InitSkippable: discard else: - resetLoc(p, res.loc) + ensureMutable res + resetLoc(p, res.locImpl) if skipTypes(res.typ, abstractInst).kind == tyArray: #incl(res.loc.flags, lfIndirect) - res.loc.storage = OnUnknown + ensureMutable res + res.locImpl.storage = OnUnknown for i in 1.. resultPos: disp.ast[resultPos].sym = copySym(s.ast[resultPos].sym, idgen) diff --git a/compiler/closureiters.nim b/compiler/closureiters.nim index 8b61106abc33f..6c9bb560809a9 100644 --- a/compiler/closureiters.nim +++ b/compiler/closureiters.nim @@ -198,7 +198,7 @@ proc newStateAssgn(ctx: var Ctx, toValue: PNode): PNode = proc newEnvVar(ctx: var Ctx, name: string, typ: PType): PSym = result = newSym(skVar, getIdent(ctx.g.cache, name), ctx.idgen, ctx.fn, ctx.fn.info) result.typ = typ - result.flags.incl sfNoInit + result.flagsImpl.incl sfNoInit assert(not typ.isNil, "Env var needs a type") let envParam = getEnvParam(ctx.fn) diff --git a/compiler/commands.nim b/compiler/commands.nim index e206a37300e44..415fe6b352f7b 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -771,6 +771,8 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; conf.globalOptions.incl optItaniumMangle else: localError(conf, info, "expected nim|cpp but found " & arg) + of "compress": + conf.globalOptions.incl optCompress of "g": # alias for --debugger:native conf.globalOptions.incl optCDebug conf.options.incl optLineDir diff --git a/compiler/enumtostr.nim b/compiler/enumtostr.nim index 2223be2ffb575..0227e3023e658 100644 --- a/compiler/enumtostr.nim +++ b/compiler/enumtostr.nim @@ -47,8 +47,7 @@ proc genEnumToStrProc*(t: PType; info: TLineInfo; g: ModuleGraph; idgen: IdGener n[bodyPos] = body n[resultPos] = newSymNode(res) result.ast = n - incl result.flags, sfFromGeneric - incl result.flags, sfNeverRaises + incl result.flagsImpl, {sfFromGeneric, sfNeverRaises} proc searchObjCaseImpl(obj: PNode; field: PSym): PNode = case obj.kind @@ -110,5 +109,4 @@ proc genCaseObjDiscMapping*(t: PType; field: PSym; info: TLineInfo; g: ModuleGra n[bodyPos] = body n[resultPos] = newSymNode(res) result.ast = n - incl result.flags, sfFromGeneric - incl result.flags, sfNeverRaises + incl result.flagsImpl, {sfFromGeneric, sfNeverRaises} diff --git a/compiler/ic/ic.nim b/compiler/ic/ic.nim index ecc6069e75434..340193641c0a8 100644 --- a/compiler/ic/ic.nim +++ b/compiler/ic/ic.nim @@ -899,11 +899,11 @@ proc moduleIndex*(c: var PackedDecoder; g: var PackedModuleGraph; thisModule: in proc symHeaderFromPacked(c: var PackedDecoder; g: var PackedModuleGraph; s: PackedSym; si, item: int32): PSym = result = PSym(itemId: ItemId(module: si, item: item), - kind: s.kind, magic: s.magic, flags: s.flags, - info: translateLineInfo(c, g, si, s.info), - options: s.options, - position: if s.kind in {skForVar, skVar, skLet, skTemp}: 0 else: s.position, - offset: if s.kind in routineKinds: defaultOffset else: s.offset, + kindImpl: s.kind, magicImpl: s.magic, flagsImpl: s.flags, + infoImpl: translateLineInfo(c, g, si, s.info), + optionsImpl: s.options, + positionImpl: if s.kind in {skForVar, skVar, skLet, skTemp}: 0 else: s.position, + offsetImpl: if s.kind in routineKinds: defaultOffset else: s.offset, disamb: s.disamb, name: getIdent(c.cache, g[si].fromDisk.strings[s.name]) ) @@ -945,8 +945,8 @@ proc symBodyFromPacked(c: var PackedDecoder; g: var PackedModuleGraph; setOwner(result, loadSym(c, g, si, s.owner)) let externalName = g[si].fromDisk.strings[s.externalName] if externalName != "": - result.loc.snippet = externalName - result.loc.flags = s.locFlags + result.locImpl.snippet = externalName + result.locImpl.flags = s.locFlags result.instantiatedFrom = loadSym(c, g, si, s.instantiatedFrom) proc needsRecompile(g: var PackedModuleGraph; conf: ConfigRef; cache: IdentCache; @@ -1058,12 +1058,12 @@ proc setupLookupTables(g: var PackedModuleGraph; conf: ConfigRef; cache: IdentCa let filename = AbsoluteFile toFullPath(conf, fileIdx) # We cannot call ``newSym`` here, because we have to circumvent the ID # mechanism, which we do in order to assign each module a persistent ID. - m.module = PSym(kind: skModule, itemId: ItemId(module: int32(fileIdx), item: 0'i32), + m.module = PSym(kindImpl: skModule, itemId: ItemId(module: int32(fileIdx), item: 0'i32), name: getIdent(cache, splitFile(filename).name), - info: newLineInfo(fileIdx, 1, 1), - position: int(fileIdx)) + infoImpl: newLineInfo(fileIdx, 1, 1), + positionImpl: int(fileIdx)) setOwner(m.module, getPackage(conf, cache, fileIdx)) - m.module.flags = m.fromDisk.moduleFlags + m.module.flagsImpl = m.fromDisk.moduleFlags proc loadToReplayNodes(g: var PackedModuleGraph; conf: ConfigRef; cache: IdentCache; fileIdx: FileIndex; m: var LoadedModule) = diff --git a/compiler/icnif/enum2nif.nim b/compiler/icnif/enum2nif.nim new file mode 100644 index 0000000000000..b8626fe56d4f4 --- /dev/null +++ b/compiler/icnif/enum2nif.nim @@ -0,0 +1,1859 @@ +# Generated by tools/enumgen.nim. DO NOT EDIT! + +import ".." / [ast, options] + +proc toNifTag*(s: TNodeKind): string = + case s + of nkNone: "none" + of nkEmpty: "empty" + of nkIdent: "ident" + of nkSym: "sym" + of nkType: "onlytype" + of nkCharLit: "charlit" + of nkIntLit: "intlit" + of nkInt8Lit: "int8lit" + of nkInt16Lit: "int16lit" + of nkInt32Lit: "int32lit" + of nkInt64Lit: "int64lit" + of nkUIntLit: "uintlit" + of nkUInt8Lit: "uint8lit" + of nkUInt16Lit: "uint16lit" + of nkUInt32Lit: "uint32lit" + of nkUInt64Lit: "uint64lit" + of nkFloatLit: "floatlit" + of nkFloat32Lit: "float32lit" + of nkFloat64Lit: "float64lit" + of nkFloat128Lit: "float128lit" + of nkStrLit: "strlit" + of nkRStrLit: "rstrlit" + of nkTripleStrLit: "triplestrlit" + of nkNilLit: "nil" + of nkComesFrom: "comesfrom" + of nkDotCall: "dotcall" + of nkCommand: "cmd" + of nkCall: "call" + of nkCallStrLit: "callstrlit" + of nkInfix: "infix" + of nkPrefix: "prefix" + of nkPostfix: "postfix" + of nkHiddenCallConv: "hcallconv" + of nkExprEqExpr: "vv" + of nkExprColonExpr: "kv" + of nkIdentDefs: "identdefs" + of nkVarTuple: "vartuple" + of nkPar: "par" + of nkObjConstr: "objconstr" + of nkCurly: "curly" + of nkCurlyExpr: "curlyx" + of nkBracket: "bracket" + of nkBracketExpr: "at" + of nkPragmaExpr: "pragmax" + of nkRange: "range" + of nkDotExpr: "dot" + of nkCheckedFieldExpr: "checkedfieldx" + of nkDerefExpr: "deref" + of nkIfExpr: "ifx" + of nkElifExpr: "elifx" + of nkElseExpr: "elsex" + of nkLambda: "lambda" + of nkDo: "do" + of nkAccQuoted: "accquoted" + of nkTableConstr: "tableconstr" + of nkBind: "bind" + of nkClosedSymChoice: "closedsymchoice" + of nkOpenSymChoice: "opensymchoice" + of nkHiddenStdConv: "hstdconv" + of nkHiddenSubConv: "hsubconv" + of nkConv: "conv" + of nkCast: "cast" + of nkStaticExpr: "staticx" + of nkAddr: "addr" + of nkHiddenAddr: "haddr" + of nkHiddenDeref: "hderef" + of nkObjDownConv: "objdownconv" + of nkObjUpConv: "objupconv" + of nkChckRangeF: "chckrangef" + of nkChckRange64: "chckrange64" + of nkChckRange: "chckrange" + of nkStringToCString: "stringtocstring" + of nkCStringToString: "cstringtostring" + of nkAsgn: "asgn" + of nkFastAsgn: "fastasgn" + of nkGenericParams: "genericparams" + of nkFormalParams: "formalparams" + of nkOfInherit: "ofinherit" + of nkImportAs: "importas" + of nkProcDef: "proc" + of nkMethodDef: "method" + of nkConverterDef: "converter" + of nkMacroDef: "macro" + of nkTemplateDef: "template" + of nkIteratorDef: "iterator" + of nkOfBranch: "of" + of nkElifBranch: "elif" + of nkExceptBranch: "except" + of nkElse: "else" + of nkAsmStmt: "asm" + of nkPragma: "pragma" + of nkPragmaBlock: "pragmablock" + of nkIfStmt: "if" + of nkWhenStmt: "when" + of nkForStmt: "for" + of nkParForStmt: "parfor" + of nkWhileStmt: "while" + of nkCaseStmt: "case" + of nkTypeSection: "type" + of nkVarSection: "var" + of nkLetSection: "let" + of nkConstSection: "const" + of nkConstDef: "const0" + of nkTypeDef: "type0" + of nkYieldStmt: "yield" + of nkDefer: "defer" + of nkTryStmt: "try" + of nkFinally: "finally" + of nkRaiseStmt: "raise" + of nkReturnStmt: "ret" + of nkBreakStmt: "brk" + of nkContinueStmt: "continue" + of nkBlockStmt: "block" + of nkStaticStmt: "static" + of nkDiscardStmt: "discard" + of nkStmtList: "stmts" + of nkImportStmt: "import" + of nkImportExceptStmt: "importexcept" + of nkExportStmt: "export" + of nkExportExceptStmt: "exportexcept" + of nkFromStmt: "from" + of nkIncludeStmt: "include" + of nkBindStmt: "bind0" + of nkMixinStmt: "mixin" + of nkUsingStmt: "using" + of nkCommentStmt: "comment" + of nkStmtListExpr: "expr" + of nkBlockExpr: "blockx" + of nkStmtListType: "stmtlisttype" + of nkBlockType: "blocktype" + of nkWith: "with" + of nkWithout: "without" + of nkTypeOfExpr: "typeofx" + of nkObjectTy: "objectty" + of nkTupleTy: "tuplety" + of nkTupleClassTy: "tupleclassty" + of nkTypeClassTy: "typeclassty" + of nkStaticTy: "staticty" + of nkRecList: "reclist" + of nkRecCase: "reccase" + of nkRecWhen: "recwhen" + of nkRefTy: "refty" + of nkPtrTy: "ptrty" + of nkVarTy: "varty" + of nkConstTy: "constty" + of nkOutTy: "outty" + of nkDistinctTy: "distinctty" + of nkProcTy: "procty" + of nkIteratorTy: "iteratorty" + of nkSinkAsgn: "sinkasgn" + of nkEnumTy: "enumty" + of nkEnumFieldDef: "efld" + of nkArgList: "arglist" + of nkPattern: "pattern" + of nkHiddenTryStmt: "htrystmt" + of nkClosure: "closure" + of nkGotoState: "gotostate" + of nkState: "state" + of nkBreakState: "breakstate" + of nkFuncDef: "func" + of nkTupleConstr: "tupleconstr" + of nkError: "err" + of nkModuleRef: "moduleref" + of nkReplayAction: "replayaction" + of nkNilRodNode: "nilrodnode" + of nkOpenSym: "opensym" + + +proc parse*(t: typedesc[TNodeKind]; s: string): TNodeKind = + case s + of "none": nkNone + of "empty": nkEmpty + of "ident": nkIdent + of "sym": nkSym + of "onlytype": nkType + of "charlit": nkCharLit + of "intlit": nkIntLit + of "int8lit": nkInt8Lit + of "int16lit": nkInt16Lit + of "int32lit": nkInt32Lit + of "int64lit": nkInt64Lit + of "uintlit": nkUIntLit + of "uint8lit": nkUInt8Lit + of "uint16lit": nkUInt16Lit + of "uint32lit": nkUInt32Lit + of "uint64lit": nkUInt64Lit + of "floatlit": nkFloatLit + of "float32lit": nkFloat32Lit + of "float64lit": nkFloat64Lit + of "float128lit": nkFloat128Lit + of "strlit": nkStrLit + of "rstrlit": nkRStrLit + of "triplestrlit": nkTripleStrLit + of "nil": nkNilLit + of "comesfrom": nkComesFrom + of "dotcall": nkDotCall + of "cmd": nkCommand + of "call": nkCall + of "callstrlit": nkCallStrLit + of "infix": nkInfix + of "prefix": nkPrefix + of "postfix": nkPostfix + of "hcallconv": nkHiddenCallConv + of "vv": nkExprEqExpr + of "kv": nkExprColonExpr + of "identdefs": nkIdentDefs + of "vartuple": nkVarTuple + of "par": nkPar + of "objconstr": nkObjConstr + of "curly": nkCurly + of "curlyx": nkCurlyExpr + of "bracket": nkBracket + of "at": nkBracketExpr + of "pragmax": nkPragmaExpr + of "range": nkRange + of "dot": nkDotExpr + of "checkedfieldx": nkCheckedFieldExpr + of "deref": nkDerefExpr + of "ifx": nkIfExpr + of "elifx": nkElifExpr + of "elsex": nkElseExpr + of "lambda": nkLambda + of "do": nkDo + of "accquoted": nkAccQuoted + of "tableconstr": nkTableConstr + of "bind": nkBind + of "closedsymchoice": nkClosedSymChoice + of "opensymchoice": nkOpenSymChoice + of "hstdconv": nkHiddenStdConv + of "hsubconv": nkHiddenSubConv + of "conv": nkConv + of "cast": nkCast + of "staticx": nkStaticExpr + of "addr": nkAddr + of "haddr": nkHiddenAddr + of "hderef": nkHiddenDeref + of "objdownconv": nkObjDownConv + of "objupconv": nkObjUpConv + of "chckrangef": nkChckRangeF + of "chckrange64": nkChckRange64 + of "chckrange": nkChckRange + of "stringtocstring": nkStringToCString + of "cstringtostring": nkCStringToString + of "asgn": nkAsgn + of "fastasgn": nkFastAsgn + of "genericparams": nkGenericParams + of "formalparams": nkFormalParams + of "ofinherit": nkOfInherit + of "importas": nkImportAs + of "proc": nkProcDef + of "method": nkMethodDef + of "converter": nkConverterDef + of "macro": nkMacroDef + of "template": nkTemplateDef + of "iterator": nkIteratorDef + of "of": nkOfBranch + of "elif": nkElifBranch + of "except": nkExceptBranch + of "else": nkElse + of "asm": nkAsmStmt + of "pragma": nkPragma + of "pragmablock": nkPragmaBlock + of "if": nkIfStmt + of "when": nkWhenStmt + of "for": nkForStmt + of "parfor": nkParForStmt + of "while": nkWhileStmt + of "case": nkCaseStmt + of "type": nkTypeSection + of "var": nkVarSection + of "let": nkLetSection + of "const": nkConstSection + of "const0": nkConstDef + of "type0": nkTypeDef + of "yield": nkYieldStmt + of "defer": nkDefer + of "try": nkTryStmt + of "finally": nkFinally + of "raise": nkRaiseStmt + of "ret": nkReturnStmt + of "brk": nkBreakStmt + of "continue": nkContinueStmt + of "block": nkBlockStmt + of "static": nkStaticStmt + of "discard": nkDiscardStmt + of "stmts": nkStmtList + of "import": nkImportStmt + of "importexcept": nkImportExceptStmt + of "export": nkExportStmt + of "exportexcept": nkExportExceptStmt + of "from": nkFromStmt + of "include": nkIncludeStmt + of "bind0": nkBindStmt + of "mixin": nkMixinStmt + of "using": nkUsingStmt + of "comment": nkCommentStmt + of "expr": nkStmtListExpr + of "blockx": nkBlockExpr + of "stmtlisttype": nkStmtListType + of "blocktype": nkBlockType + of "with": nkWith + of "without": nkWithout + of "typeofx": nkTypeOfExpr + of "objectty": nkObjectTy + of "tuplety": nkTupleTy + of "tupleclassty": nkTupleClassTy + of "typeclassty": nkTypeClassTy + of "staticty": nkStaticTy + of "reclist": nkRecList + of "reccase": nkRecCase + of "recwhen": nkRecWhen + of "refty": nkRefTy + of "ptrty": nkPtrTy + of "varty": nkVarTy + of "constty": nkConstTy + of "outty": nkOutTy + of "distinctty": nkDistinctTy + of "procty": nkProcTy + of "iteratorty": nkIteratorTy + of "sinkasgn": nkSinkAsgn + of "enumty": nkEnumTy + of "efld": nkEnumFieldDef + of "arglist": nkArgList + of "pattern": nkPattern + of "htrystmt": nkHiddenTryStmt + of "closure": nkClosure + of "gotostate": nkGotoState + of "state": nkState + of "breakstate": nkBreakState + of "func": nkFuncDef + of "tupleconstr": nkTupleConstr + of "err": nkError + of "moduleref": nkModuleRef + of "replayaction": nkReplayAction + of "nilrodnode": nkNilRodNode + of "opensym": nkOpenSym + else: nkNone + + +proc toNifTag*(s: TSymKind): string = + case s + of skUnknown: "unknown" + of skConditional: "conditional" + of skDynLib: "dynlib" + of skParam: "param" + of skGenericParam: "genericparam" + of skTemp: "temp" + of skModule: "module" + of skType: "type" + of skVar: "var" + of skLet: "let" + of skConst: "const" + of skResult: "result" + of skProc: "proc" + of skFunc: "func" + of skMethod: "method" + of skIterator: "iterator" + of skConverter: "converter" + of skMacro: "macro" + of skTemplate: "template" + of skField: "field" + of skEnumField: "enumfield" + of skForVar: "forvar" + of skLabel: "label" + of skStub: "stub" + of skPackage: "package" + + +proc parse*(t: typedesc[TSymKind]; s: string): TSymKind = + case s + of "unknown": skUnknown + of "conditional": skConditional + of "dynlib": skDynLib + of "param": skParam + of "genericparam": skGenericParam + of "temp": skTemp + of "module": skModule + of "type": skType + of "var": skVar + of "let": skLet + of "const": skConst + of "result": skResult + of "proc": skProc + of "func": skFunc + of "method": skMethod + of "iterator": skIterator + of "converter": skConverter + of "macro": skMacro + of "template": skTemplate + of "field": skField + of "enumfield": skEnumField + of "forvar": skForVar + of "label": skLabel + of "stub": skStub + of "package": skPackage + else: skUnknown + + +proc toNifTag*(s: TTypeKind): string = + case s + of tyNone: "none" + of tyBool: "bool" + of tyChar: "char" + of tyEmpty: "empty" + of tyAlias: "alias" + of tyNil: "nil" + of tyUntyped: "untyped" + of tyTyped: "typed" + of tyTypeDesc: "typedesc" + of tyGenericInvocation: "ginvoke" + of tyGenericBody: "gbody" + of tyGenericInst: "ginst" + of tyGenericParam: "gparam" + of tyDistinct: "distinct" + of tyEnum: "enum" + of tyOrdinal: "ordinal" + of tyArray: "array" + of tyObject: "object" + of tyTuple: "tuple" + of tySet: "set" + of tyRange: "range" + of tyPtr: "ptr" + of tyRef: "ref" + of tyVar: "mut" + of tySequence: "seq" + of tyProc: "proctype" + of tyPointer: "pointer" + of tyOpenArray: "openarray" + of tyString: "string" + of tyCstring: "cstring" + of tyForward: "forward" + of tyInt: "int" + of tyInt8: "int8" + of tyInt16: "int16" + of tyInt32: "int32" + of tyInt64: "int64" + of tyFloat: "float" + of tyFloat32: "float32" + of tyFloat64: "float64" + of tyFloat128: "float128" + of tyUInt: "uint" + of tyUInt8: "uint8" + of tyUInt16: "uint16" + of tyUInt32: "uint32" + of tyUInt64: "uint64" + of tyOwned: "owned" + of tySink: "sink" + of tyLent: "lent" + of tyVarargs: "varargs" + of tyUncheckedArray: "uarray" + of tyError: "error" + of tyBuiltInTypeClass: "bconcept" + of tyUserTypeClass: "uconcept" + of tyUserTypeClassInst: "uconceptinst" + of tyCompositeTypeClass: "cconcept" + of tyInferred: "inferred" + of tyAnd: "and" + of tyOr: "or" + of tyNot: "not" + of tyAnything: "anything" + of tyStatic: "static" + of tyFromExpr: "fromx" + of tyConcept: "concept" + of tyVoid: "void" + of tyIterable: "iterable" + + +proc parse*(t: typedesc[TTypeKind]; s: string): TTypeKind = + case s + of "none": tyNone + of "bool": tyBool + of "char": tyChar + of "empty": tyEmpty + of "alias": tyAlias + of "nil": tyNil + of "untyped": tyUntyped + of "typed": tyTyped + of "typedesc": tyTypeDesc + of "ginvoke": tyGenericInvocation + of "gbody": tyGenericBody + of "ginst": tyGenericInst + of "gparam": tyGenericParam + of "distinct": tyDistinct + of "enum": tyEnum + of "ordinal": tyOrdinal + of "array": tyArray + of "object": tyObject + of "tuple": tyTuple + of "set": tySet + of "range": tyRange + of "ptr": tyPtr + of "ref": tyRef + of "mut": tyVar + of "seq": tySequence + of "proctype": tyProc + of "pointer": tyPointer + of "openarray": tyOpenArray + of "string": tyString + of "cstring": tyCstring + of "forward": tyForward + of "int": tyInt + of "int8": tyInt8 + of "int16": tyInt16 + of "int32": tyInt32 + of "int64": tyInt64 + of "float": tyFloat + of "float32": tyFloat32 + of "float64": tyFloat64 + of "float128": tyFloat128 + of "uint": tyUInt + of "uint8": tyUInt8 + of "uint16": tyUInt16 + of "uint32": tyUInt32 + of "uint64": tyUInt64 + of "owned": tyOwned + of "sink": tySink + of "lent": tyLent + of "varargs": tyVarargs + of "uarray": tyUncheckedArray + of "error": tyError + of "bconcept": tyBuiltInTypeClass + of "uconcept": tyUserTypeClass + of "uconceptinst": tyUserTypeClassInst + of "cconcept": tyCompositeTypeClass + of "inferred": tyInferred + of "and": tyAnd + of "or": tyOr + of "not": tyNot + of "anything": tyAnything + of "static": tyStatic + of "fromx": tyFromExpr + of "concept": tyConcept + of "void": tyVoid + of "iterable": tyIterable + else: tyNone + + +proc toNifTag*(s: TLocKind): string = + case s + of locNone: "none" + of locTemp: "temp" + of locLocalVar: "localvar" + of locGlobalVar: "globalvar" + of locParam: "param" + of locField: "field" + of locExpr: "expr" + of locProc: "proc" + of locData: "data" + of locCall: "call" + of locOther: "other" + + +proc parse*(t: typedesc[TLocKind]; s: string): TLocKind = + case s + of "none": locNone + of "temp": locTemp + of "localvar": locLocalVar + of "globalvar": locGlobalVar + of "param": locParam + of "field": locField + of "expr": locExpr + of "proc": locProc + of "data": locData + of "call": locCall + of "other": locOther + else: locNone + + +proc toNifTag*(s: TCallingConvention): string = + case s + of ccNimCall: "nimcall" + of ccStdCall: "stdcall" + of ccCDecl: "cdecl" + of ccSafeCall: "safecall" + of ccSysCall: "syscall" + of ccInline: "inline" + of ccNoInline: "noinline" + of ccFastCall: "fastcall" + of ccThisCall: "thiscall" + of ccClosure: "closure" + of ccNoConvention: "noconv" + of ccMember: "member" + + +proc parse*(t: typedesc[TCallingConvention]; s: string): TCallingConvention = + case s + of "nimcall": ccNimCall + of "stdcall": ccStdCall + of "cdecl": ccCDecl + of "safecall": ccSafeCall + of "syscall": ccSysCall + of "inline": ccInline + of "noinline": ccNoInline + of "fastcall": ccFastCall + of "thiscall": ccThisCall + of "closure": ccClosure + of "noconv": ccNoConvention + of "member": ccMember + else: ccNimCall + + +proc toNifTag*(s: TMagic): string = + case s + of mNone: "nonem" + of mDefined: "defined" + of mDeclared: "declared" + of mDeclaredInScope: "declaredinscope" + of mCompiles: "compiles" + of mArrGet: "arrget" + of mArrPut: "arrput" + of mAsgn: "asgnm" + of mLow: "low" + of mHigh: "high" + of mSizeOf: "sizeof" + of mAlignOf: "alignof" + of mOffsetOf: "offsetof" + of mTypeTrait: "typetrait" + of mIs: "is" + of mOf: "ofm" + of mAddr: "addrm" + of mType: "typem" + of mTypeOf: "typeof" + of mPlugin: "plugin" + of mEcho: "echo" + of mShallowCopy: "shallowcopy" + of mSlurp: "slurp" + of mStaticExec: "staticexec" + of mStatic: "staticm" + of mParseExprToAst: "parseexprtoast" + of mParseStmtToAst: "parsestmttoast" + of mExpandToAst: "expandtoast" + of mQuoteAst: "quoteast" + of mInc: "inc" + of mDec: "dec" + of mOrd: "ord" + of mNew: "new" + of mNewFinalize: "newfinalize" + of mNewSeq: "newseq" + of mNewSeqOfCap: "newseqofcap" + of mLengthOpenArray: "lenopenarray" + of mLengthStr: "lenstr" + of mLengthArray: "lenarray" + of mLengthSeq: "lenseq" + of mIncl: "incl" + of mExcl: "excl" + of mCard: "card" + of mChr: "chr" + of mGCref: "gcref" + of mGCunref: "gcunref" + of mAddI: "add" + of mSubI: "sub" + of mMulI: "mul" + of mDivI: "div" + of mModI: "mod" + of mSucc: "succ" + of mPred: "pred" + of mAddF64: "addf64" + of mSubF64: "subf64" + of mMulF64: "mulf64" + of mDivF64: "divf64" + of mShrI: "shr" + of mShlI: "shl" + of mAshrI: "ashr" + of mBitandI: "bitand" + of mBitorI: "bitor" + of mBitxorI: "bitxor" + of mMinI: "min" + of mMaxI: "max" + of mAddU: "addu" + of mSubU: "subu" + of mMulU: "mulu" + of mDivU: "divu" + of mModU: "modu" + of mEqI: "eq" + of mLeI: "le" + of mLtI: "lt" + of mEqF64: "eqf64" + of mLeF64: "lef64" + of mLtF64: "ltf64" + of mLeU: "leu" + of mLtU: "ltu" + of mEqEnum: "eqenum" + of mLeEnum: "leenum" + of mLtEnum: "ltenum" + of mEqCh: "eqch" + of mLeCh: "lech" + of mLtCh: "ltch" + of mEqB: "eqb" + of mLeB: "leb" + of mLtB: "ltb" + of mEqRef: "eqref" + of mLePtr: "leptr" + of mLtPtr: "ltptr" + of mXor: "xor" + of mEqCString: "eqcstring" + of mEqProc: "eqproc" + of mUnaryMinusI: "unaryminus" + of mUnaryMinusI64: "unaryminusi64" + of mAbsI: "abs" + of mNot: "not" + of mUnaryPlusI: "unaryplus" + of mBitnotI: "bitnot" + of mUnaryPlusF64: "unaryplusf64" + of mUnaryMinusF64: "unaryminusf64" + of mCharToStr: "chartostr" + of mBoolToStr: "booltostr" + of mCStrToStr: "cstrtostr" + of mStrToStr: "strtostr" + of mEnumToStr: "enumtostr" + of mAnd: "and" + of mOr: "or" + of mImplies: "implies" + of mIff: "iff" + of mExists: "exists" + of mForall: "forall" + of mOld: "old" + of mEqStr: "eqstr" + of mLeStr: "lestr" + of mLtStr: "ltstr" + of mEqSet: "eqset" + of mLeSet: "leset" + of mLtSet: "ltset" + of mMulSet: "mulset" + of mPlusSet: "plusset" + of mMinusSet: "minusset" + of mXorSet: "xorset" + of mConStrStr: "constrstr" + of mSlice: "slice" + of mDotDot: "dotdot" + of mFields: "fields" + of mFieldPairs: "fieldpairs" + of mOmpParFor: "ompparfor" + of mAppendStrCh: "addstrch" + of mAppendStrStr: "addstrstr" + of mAppendSeqElem: "addseqelem" + of mInSet: "contains" + of mRepr: "repr" + of mExit: "exit" + of mSetLengthStr: "setlenstr" + of mSetLengthSeq: "setlenseq" + of mSetLengthSeqUninit: "setlensequninit" + of mIsPartOf: "ispartof" + of mAstToStr: "asttostr" + of mParallel: "parallel" + of mSwap: "swap" + of mIsNil: "isnil" + of mArrToSeq: "arrtoseq" + of mOpenArrayToSeq: "openarraytoseq" + of mNewString: "newstring" + of mNewStringOfCap: "newstringofcap" + of mParseBiggestFloat: "parsebiggestfloat" + of mMove: "move" + of mEnsureMove: "ensuremove" + of mWasMoved: "wasmoved" + of mDup: "dup" + of mDestroy: "destroy" + of mTrace: "trace" + of mDefault: "default" + of mUnown: "unown" + of mFinished: "finished" + of mIsolate: "isolate" + of mAccessEnv: "accessenv" + of mAccessTypeField: "accesstypefield" + of mArray: "array" + of mOpenArray: "openarray" + of mRange: "rangem" + of mSet: "set" + of mSeq: "seq" + of mVarargs: "varargs" + of mRef: "ref" + of mPtr: "ptr" + of mVar: "varm" + of mDistinct: "distinct" + of mVoid: "void" + of mTuple: "tuple" + of mOrdinal: "ordinal" + of mIterableType: "iterabletype" + of mInt: "int" + of mInt8: "int8" + of mInt16: "int16" + of mInt32: "int32" + of mInt64: "int64" + of mUInt: "uint" + of mUInt8: "uint8" + of mUInt16: "uint16" + of mUInt32: "uint32" + of mUInt64: "uint64" + of mFloat: "float" + of mFloat32: "float32" + of mFloat64: "float64" + of mFloat128: "float128" + of mBool: "bool" + of mChar: "char" + of mString: "string" + of mCstring: "cstring" + of mPointer: "pointer" + of mNil: "nilm" + of mExpr: "exprm" + of mStmt: "stmtm" + of mTypeDesc: "typedesc" + of mVoidType: "voidtype" + of mPNimrodNode: "nimnode" + of mSpawn: "spawn" + of mDeepCopy: "deepcopy" + of mIsMainModule: "ismainmodule" + of mCompileDate: "compiledate" + of mCompileTime: "compiletime" + of mProcCall: "proccall" + of mCpuEndian: "cpuendian" + of mHostOS: "hostos" + of mHostCPU: "hostcpu" + of mBuildOS: "buildos" + of mBuildCPU: "buildcpu" + of mAppType: "apptype" + of mCompileOption: "compileoption" + of mCompileOptionArg: "compileoptionarg" + of mNLen: "nlen" + of mNChild: "nchild" + of mNSetChild: "nsetchild" + of mNAdd: "nadd" + of mNAddMultiple: "naddmultiple" + of mNDel: "ndel" + of mNKind: "nkind" + of mNSymKind: "nsymkind" + of mNccValue: "nccvalue" + of mNccInc: "nccinc" + of mNcsAdd: "ncsadd" + of mNcsIncl: "ncsincl" + of mNcsLen: "ncslen" + of mNcsAt: "ncsat" + of mNctPut: "nctput" + of mNctLen: "nctlen" + of mNctGet: "nctget" + of mNctHasNext: "ncthasnext" + of mNctNext: "nctnext" + of mNIntVal: "nintval" + of mNFloatVal: "nfloatval" + of mNSymbol: "nsymbol" + of mNIdent: "nident" + of mNGetType: "ngettype" + of mNStrVal: "nstrval" + of mNSetIntVal: "nsetintval" + of mNSetFloatVal: "nsetfloatval" + of mNSetSymbol: "nsetsymbol" + of mNSetIdent: "nsetident" + of mNSetStrVal: "nsetstrval" + of mNLineInfo: "nlineinfo" + of mNNewNimNode: "nnewnimnode" + of mNCopyNimNode: "ncopynimnode" + of mNCopyNimTree: "ncopynimtree" + of mStrToIdent: "strtoident" + of mNSigHash: "nsighash" + of mNSizeOf: "nsizeof" + of mNBindSym: "nbindsym" + of mNCallSite: "ncallsite" + of mEqIdent: "eqident" + of mEqNimrodNode: "eqnimnode" + of mSameNodeType: "samenodetype" + of mGetImpl: "getimpl" + of mNGenSym: "ngensym" + of mNHint: "nhint" + of mNWarning: "nwarning" + of mNError: "nerror" + of mInstantiationInfo: "instantiationinfo" + of mGetTypeInfo: "gettypeinfo" + of mGetTypeInfoV2: "gettypeinfov2" + of mNimvm: "nimvm" + of mIntDefine: "intdefine" + of mStrDefine: "strdefine" + of mBoolDefine: "booldefine" + of mGenericDefine: "genericdefine" + of mRunnableExamples: "runnableexamples" + of mException: "exception" + of mBuiltinType: "builtintype" + of mSymOwner: "symowner" + of mUncheckedArray: "uncheckedarray" + of mGetImplTransf: "getimpltransf" + of mSymIsInstantiationOf: "symisinstantiationof" + of mNodeId: "nodeid" + of mPrivateAccess: "privateaccess" + of mZeroDefault: "zerodefault" + + +proc parse*(t: typedesc[TMagic]; s: string): TMagic = + case s + of "nonem": mNone + of "defined": mDefined + of "declared": mDeclared + of "declaredinscope": mDeclaredInScope + of "compiles": mCompiles + of "arrget": mArrGet + of "arrput": mArrPut + of "asgnm": mAsgn + of "low": mLow + of "high": mHigh + of "sizeof": mSizeOf + of "alignof": mAlignOf + of "offsetof": mOffsetOf + of "typetrait": mTypeTrait + of "is": mIs + of "ofm": mOf + of "addrm": mAddr + of "typem": mType + of "typeof": mTypeOf + of "plugin": mPlugin + of "echo": mEcho + of "shallowcopy": mShallowCopy + of "slurp": mSlurp + of "staticexec": mStaticExec + of "staticm": mStatic + of "parseexprtoast": mParseExprToAst + of "parsestmttoast": mParseStmtToAst + of "expandtoast": mExpandToAst + of "quoteast": mQuoteAst + of "inc": mInc + of "dec": mDec + of "ord": mOrd + of "new": mNew + of "newfinalize": mNewFinalize + of "newseq": mNewSeq + of "newseqofcap": mNewSeqOfCap + of "lenopenarray": mLengthOpenArray + of "lenstr": mLengthStr + of "lenarray": mLengthArray + of "lenseq": mLengthSeq + of "incl": mIncl + of "excl": mExcl + of "card": mCard + of "chr": mChr + of "gcref": mGCref + of "gcunref": mGCunref + of "add": mAddI + of "sub": mSubI + of "mul": mMulI + of "div": mDivI + of "mod": mModI + of "succ": mSucc + of "pred": mPred + of "addf64": mAddF64 + of "subf64": mSubF64 + of "mulf64": mMulF64 + of "divf64": mDivF64 + of "shr": mShrI + of "shl": mShlI + of "ashr": mAshrI + of "bitand": mBitandI + of "bitor": mBitorI + of "bitxor": mBitxorI + of "min": mMinI + of "max": mMaxI + of "addu": mAddU + of "subu": mSubU + of "mulu": mMulU + of "divu": mDivU + of "modu": mModU + of "eq": mEqI + of "le": mLeI + of "lt": mLtI + of "eqf64": mEqF64 + of "lef64": mLeF64 + of "ltf64": mLtF64 + of "leu": mLeU + of "ltu": mLtU + of "eqenum": mEqEnum + of "leenum": mLeEnum + of "ltenum": mLtEnum + of "eqch": mEqCh + of "lech": mLeCh + of "ltch": mLtCh + of "eqb": mEqB + of "leb": mLeB + of "ltb": mLtB + of "eqref": mEqRef + of "leptr": mLePtr + of "ltptr": mLtPtr + of "xor": mXor + of "eqcstring": mEqCString + of "eqproc": mEqProc + of "unaryminus": mUnaryMinusI + of "unaryminusi64": mUnaryMinusI64 + of "abs": mAbsI + of "not": mNot + of "unaryplus": mUnaryPlusI + of "bitnot": mBitnotI + of "unaryplusf64": mUnaryPlusF64 + of "unaryminusf64": mUnaryMinusF64 + of "chartostr": mCharToStr + of "booltostr": mBoolToStr + of "cstrtostr": mCStrToStr + of "strtostr": mStrToStr + of "enumtostr": mEnumToStr + of "and": mAnd + of "or": mOr + of "implies": mImplies + of "iff": mIff + of "exists": mExists + of "forall": mForall + of "old": mOld + of "eqstr": mEqStr + of "lestr": mLeStr + of "ltstr": mLtStr + of "eqset": mEqSet + of "leset": mLeSet + of "ltset": mLtSet + of "mulset": mMulSet + of "plusset": mPlusSet + of "minusset": mMinusSet + of "xorset": mXorSet + of "constrstr": mConStrStr + of "slice": mSlice + of "dotdot": mDotDot + of "fields": mFields + of "fieldpairs": mFieldPairs + of "ompparfor": mOmpParFor + of "addstrch": mAppendStrCh + of "addstrstr": mAppendStrStr + of "addseqelem": mAppendSeqElem + of "contains": mInSet + of "repr": mRepr + of "exit": mExit + of "setlenstr": mSetLengthStr + of "setlenseq": mSetLengthSeq + of "setlensequninit": mSetLengthSeqUninit + of "ispartof": mIsPartOf + of "asttostr": mAstToStr + of "parallel": mParallel + of "swap": mSwap + of "isnil": mIsNil + of "arrtoseq": mArrToSeq + of "openarraytoseq": mOpenArrayToSeq + of "newstring": mNewString + of "newstringofcap": mNewStringOfCap + of "parsebiggestfloat": mParseBiggestFloat + of "move": mMove + of "ensuremove": mEnsureMove + of "wasmoved": mWasMoved + of "dup": mDup + of "destroy": mDestroy + of "trace": mTrace + of "default": mDefault + of "unown": mUnown + of "finished": mFinished + of "isolate": mIsolate + of "accessenv": mAccessEnv + of "accesstypefield": mAccessTypeField + of "array": mArray + of "openarray": mOpenArray + of "rangem": mRange + of "set": mSet + of "seq": mSeq + of "varargs": mVarargs + of "ref": mRef + of "ptr": mPtr + of "varm": mVar + of "distinct": mDistinct + of "void": mVoid + of "tuple": mTuple + of "ordinal": mOrdinal + of "iterabletype": mIterableType + of "int": mInt + of "int8": mInt8 + of "int16": mInt16 + of "int32": mInt32 + of "int64": mInt64 + of "uint": mUInt + of "uint8": mUInt8 + of "uint16": mUInt16 + of "uint32": mUInt32 + of "uint64": mUInt64 + of "float": mFloat + of "float32": mFloat32 + of "float64": mFloat64 + of "float128": mFloat128 + of "bool": mBool + of "char": mChar + of "string": mString + of "cstring": mCstring + of "pointer": mPointer + of "nilm": mNil + of "exprm": mExpr + of "stmtm": mStmt + of "typedesc": mTypeDesc + of "voidtype": mVoidType + of "nimnode": mPNimrodNode + of "spawn": mSpawn + of "deepcopy": mDeepCopy + of "ismainmodule": mIsMainModule + of "compiledate": mCompileDate + of "compiletime": mCompileTime + of "proccall": mProcCall + of "cpuendian": mCpuEndian + of "hostos": mHostOS + of "hostcpu": mHostCPU + of "buildos": mBuildOS + of "buildcpu": mBuildCPU + of "apptype": mAppType + of "compileoption": mCompileOption + of "compileoptionarg": mCompileOptionArg + of "nlen": mNLen + of "nchild": mNChild + of "nsetchild": mNSetChild + of "nadd": mNAdd + of "naddmultiple": mNAddMultiple + of "ndel": mNDel + of "nkind": mNKind + of "nsymkind": mNSymKind + of "nccvalue": mNccValue + of "nccinc": mNccInc + of "ncsadd": mNcsAdd + of "ncsincl": mNcsIncl + of "ncslen": mNcsLen + of "ncsat": mNcsAt + of "nctput": mNctPut + of "nctlen": mNctLen + of "nctget": mNctGet + of "ncthasnext": mNctHasNext + of "nctnext": mNctNext + of "nintval": mNIntVal + of "nfloatval": mNFloatVal + of "nsymbol": mNSymbol + of "nident": mNIdent + of "ngettype": mNGetType + of "nstrval": mNStrVal + of "nsetintval": mNSetIntVal + of "nsetfloatval": mNSetFloatVal + of "nsetsymbol": mNSetSymbol + of "nsetident": mNSetIdent + of "nsetstrval": mNSetStrVal + of "nlineinfo": mNLineInfo + of "nnewnimnode": mNNewNimNode + of "ncopynimnode": mNCopyNimNode + of "ncopynimtree": mNCopyNimTree + of "strtoident": mStrToIdent + of "nsighash": mNSigHash + of "nsizeof": mNSizeOf + of "nbindsym": mNBindSym + of "ncallsite": mNCallSite + of "eqident": mEqIdent + of "eqnimnode": mEqNimrodNode + of "samenodetype": mSameNodeType + of "getimpl": mGetImpl + of "ngensym": mNGenSym + of "nhint": mNHint + of "nwarning": mNWarning + of "nerror": mNError + of "instantiationinfo": mInstantiationInfo + of "gettypeinfo": mGetTypeInfo + of "gettypeinfov2": mGetTypeInfoV2 + of "nimvm": mNimvm + of "intdefine": mIntDefine + of "strdefine": mStrDefine + of "booldefine": mBoolDefine + of "genericdefine": mGenericDefine + of "runnableexamples": mRunnableExamples + of "exception": mException + of "builtintype": mBuiltinType + of "symowner": mSymOwner + of "uncheckedarray": mUncheckedArray + of "getimpltransf": mGetImplTransf + of "symisinstantiationof": mSymIsInstantiationOf + of "nodeid": mNodeId + of "privateaccess": mPrivateAccess + of "zerodefault": mZeroDefault + else: mNone + + +proc toNifTag*(s: TStorageLoc): string = + case s + of OnUnknown: "unknown" + of OnStatic: "static" + of OnStack: "stack" + of OnHeap: "heap" + + +proc parse*(t: typedesc[TStorageLoc]; s: string): TStorageLoc = + case s + of "unknown": OnUnknown + of "static": OnStatic + of "stack": OnStack + of "heap": OnHeap + else: OnUnknown + + +proc toNifTag*(s: TLibKind): string = + case s + of libHeader: "bheader" + of libDynamic: "bdynamic" + + +proc parse*(t: typedesc[TLibKind]; s: string): TLibKind = + case s + of "bheader": libHeader + of "bdynamic": libDynamic + else: libHeader + + +proc genFlags*(s: set[TSymFlag]; dest: var string) = + for e in s: + case e + of sfUsed: dest.add "u" + of sfExported: dest.add "e" + of sfFromGeneric: dest.add "f" + of sfGlobal: dest.add "g" + of sfForward: dest.add "f0" + of sfWasForwarded: dest.add "w" + of sfImportc: dest.add "i" + of sfExportc: dest.add "e0" + of sfMangleCpp: dest.add "m" + of sfVolatile: dest.add "v" + of sfRegister: dest.add "r" + of sfPure: dest.add "p" + of sfNoSideEffect: dest.add "n" + of sfSideEffect: dest.add "s" + of sfMainModule: dest.add "m0" + of sfSystemModule: dest.add "s0" + of sfNoReturn: dest.add "n0" + of sfAddrTaken: dest.add "a" + of sfCompilerProc: dest.add "c" + of sfEscapes: dest.add "e1" + of sfDiscriminant: dest.add "d" + of sfRequiresInit: dest.add "r0" + of sfDeprecated: dest.add "d0" + of sfExplain: dest.add "e2" + of sfError: dest.add "e3" + of sfShadowed: dest.add "s1" + of sfThread: dest.add "t" + of sfCppNonPod: dest.add "c0" + of sfCompileTime: dest.add "c1" + of sfConstructor: dest.add "c2" + of sfDispatcher: dest.add "d1" + of sfBorrow: dest.add "b" + of sfInfixCall: dest.add "i0" + of sfNamedParamCall: dest.add "n1" + of sfDiscardable: dest.add "d2" + of sfOverridden: dest.add "o" + of sfCallsite: dest.add "c3" + of sfGenSym: dest.add "g0" + of sfNonReloadable: dest.add "n2" + of sfGeneratedOp: dest.add "g1" + of sfTemplateParam: dest.add "t0" + of sfCursor: dest.add "c4" + of sfInjectDestructors: dest.add "i1" + of sfNeverRaises: dest.add "n3" + of sfSystemRaisesDefect: dest.add "s2" + of sfUsedInFinallyOrExcept: dest.add "u0" + of sfSingleUsedTemp: dest.add "s3" + of sfNoalias: dest.add "n4" + of sfEffectsDelayed: dest.add "e4" + of sfGeneratedType: dest.add "g2" + of sfVirtual: dest.add "v0" + of sfByCopy: dest.add "b0" + of sfMember: dest.add "m1" + of sfCodegenDecl: dest.add "c5" + of sfWasGenSym: dest.add "w0" + of sfForceLift: dest.add "l" + of sfDirty: dest.add "d3" + of sfCustomPragma: dest.add "c6" + of sfBase: dest.add "b1" + of sfGoto: dest.add "g3" + of sfAnon: dest.add "a0" + of sfAllUntyped: dest.add "a1" + of sfTemplateRedefinition: dest.add "t1" + + +proc parse*(t: typedesc[TSymFlag]; s: string): set[TSymFlag] = + result = {} + var i = 0 + while i < s.len: + case s[i] + of 'a': + if i+1 < s.len and s[i+1] == '0': + result.incl sfAnon + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfAllUntyped + inc i + else: result.incl sfAddrTaken + of 'b': + if i+1 < s.len and s[i+1] == '0': + result.incl sfByCopy + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfBase + inc i + else: result.incl sfBorrow + of 'c': + if i+1 < s.len and s[i+1] == '0': + result.incl sfCppNonPod + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfCompileTime + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl sfConstructor + inc i + elif i+1 < s.len and s[i+1] == '3': + result.incl sfCallsite + inc i + elif i+1 < s.len and s[i+1] == '4': + result.incl sfCursor + inc i + elif i+1 < s.len and s[i+1] == '5': + result.incl sfCodegenDecl + inc i + elif i+1 < s.len and s[i+1] == '6': + result.incl sfCustomPragma + inc i + else: result.incl sfCompilerProc + of 'd': + if i+1 < s.len and s[i+1] == '0': + result.incl sfDeprecated + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfDispatcher + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl sfDiscardable + inc i + elif i+1 < s.len and s[i+1] == '3': + result.incl sfDirty + inc i + else: result.incl sfDiscriminant + of 'e': + if i+1 < s.len and s[i+1] == '0': + result.incl sfExportc + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfEscapes + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl sfExplain + inc i + elif i+1 < s.len and s[i+1] == '3': + result.incl sfError + inc i + elif i+1 < s.len and s[i+1] == '4': + result.incl sfEffectsDelayed + inc i + else: result.incl sfExported + of 'f': + if i+1 < s.len and s[i+1] == '0': + result.incl sfForward + inc i + else: result.incl sfFromGeneric + of 'g': + if i+1 < s.len and s[i+1] == '0': + result.incl sfGenSym + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfGeneratedOp + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl sfGeneratedType + inc i + elif i+1 < s.len and s[i+1] == '3': + result.incl sfGoto + inc i + else: result.incl sfGlobal + of 'i': + if i+1 < s.len and s[i+1] == '0': + result.incl sfInfixCall + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfInjectDestructors + inc i + else: result.incl sfImportc + of 'l': result.incl sfForceLift + of 'm': + if i+1 < s.len and s[i+1] == '0': + result.incl sfMainModule + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfMember + inc i + else: result.incl sfMangleCpp + of 'n': + if i+1 < s.len and s[i+1] == '0': + result.incl sfNoReturn + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfNamedParamCall + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl sfNonReloadable + inc i + elif i+1 < s.len and s[i+1] == '3': + result.incl sfNeverRaises + inc i + elif i+1 < s.len and s[i+1] == '4': + result.incl sfNoalias + inc i + else: result.incl sfNoSideEffect + of 'o': result.incl sfOverridden + of 'p': result.incl sfPure + of 'r': + if i+1 < s.len and s[i+1] == '0': + result.incl sfRequiresInit + inc i + else: result.incl sfRegister + of 's': + if i+1 < s.len and s[i+1] == '0': + result.incl sfSystemModule + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfShadowed + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl sfSystemRaisesDefect + inc i + elif i+1 < s.len and s[i+1] == '3': + result.incl sfSingleUsedTemp + inc i + else: result.incl sfSideEffect + of 't': + if i+1 < s.len and s[i+1] == '0': + result.incl sfTemplateParam + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl sfTemplateRedefinition + inc i + else: result.incl sfThread + of 'u': + if i+1 < s.len and s[i+1] == '0': + result.incl sfUsedInFinallyOrExcept + inc i + else: result.incl sfUsed + of 'v': + if i+1 < s.len and s[i+1] == '0': + result.incl sfVirtual + inc i + else: result.incl sfVolatile + of 'w': + if i+1 < s.len and s[i+1] == '0': + result.incl sfWasGenSym + inc i + else: result.incl sfWasForwarded + else: discard + inc i + +proc genFlags*(s: set[TNodeFlag]; dest: var string) = + for e in s: + case e + of nfNone: dest.add "n" + of nfBase2: dest.add "b" + of nfBase8: dest.add "b0" + of nfBase16: dest.add "b1" + of nfAllConst: dest.add "a" + of nfTransf: dest.add "t" + of nfNoRewrite: dest.add "r" + of nfSem: dest.add "s" + of nfLL: dest.add "l" + of nfDotField: dest.add "d" + of nfDotSetter: dest.add "d0" + of nfExplicitCall: dest.add "e" + of nfExprCall: dest.add "c" + of nfIsRef: dest.add "i" + of nfIsPtr: dest.add "p" + of nfPreventCg: dest.add "p0" + of nfBlockArg: dest.add "b2" + of nfFromTemplate: dest.add "f" + of nfDefaultParam: dest.add "d1" + of nfDefaultRefsParam: dest.add "d2" + of nfExecuteOnReload: dest.add "o" + of nfLastRead: dest.add "l0" + of nfFirstWrite: dest.add "w" + of nfHasComment: dest.add "h" + of nfSkipFieldChecking: dest.add "s0" + of nfDisabledOpenSym: dest.add "d3" + + +proc parse*(t: typedesc[TNodeFlag]; s: string): set[TNodeFlag] = + result = {} + var i = 0 + while i < s.len: + case s[i] + of 'a': result.incl nfAllConst + of 'b': + if i+1 < s.len and s[i+1] == '0': + result.incl nfBase8 + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl nfBase16 + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl nfBlockArg + inc i + else: result.incl nfBase2 + of 'c': result.incl nfExprCall + of 'd': + if i+1 < s.len and s[i+1] == '0': + result.incl nfDotSetter + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl nfDefaultParam + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl nfDefaultRefsParam + inc i + elif i+1 < s.len and s[i+1] == '3': + result.incl nfDisabledOpenSym + inc i + else: result.incl nfDotField + of 'e': result.incl nfExplicitCall + of 'f': result.incl nfFromTemplate + of 'h': result.incl nfHasComment + of 'i': result.incl nfIsRef + of 'l': + if i+1 < s.len and s[i+1] == '0': + result.incl nfLastRead + inc i + else: result.incl nfLL + of 'n': result.incl nfNone + of 'o': result.incl nfExecuteOnReload + of 'p': + if i+1 < s.len and s[i+1] == '0': + result.incl nfPreventCg + inc i + else: result.incl nfIsPtr + of 'r': result.incl nfNoRewrite + of 's': + if i+1 < s.len and s[i+1] == '0': + result.incl nfSkipFieldChecking + inc i + else: result.incl nfSem + of 't': result.incl nfTransf + of 'w': result.incl nfFirstWrite + else: discard + inc i + +proc genFlags*(s: set[TTypeFlag]; dest: var string) = + for e in s: + case e + of tfVarargs: dest.add "v" + of tfNoSideEffect: dest.add "n" + of tfFinal: dest.add "f" + of tfInheritable: dest.add "i" + of tfHasOwned: dest.add "h" + of tfEnumHasHoles: dest.add "e" + of tfShallow: dest.add "s" + of tfThread: dest.add "t" + of tfFromGeneric: dest.add "g" + of tfUnresolved: dest.add "u" + of tfResolved: dest.add "r" + of tfRetType: dest.add "r0" + of tfCapturesEnv: dest.add "c" + of tfByCopy: dest.add "b" + of tfByRef: dest.add "b0" + of tfIterator: dest.add "i0" + of tfPartial: dest.add "p" + of tfNotNil: dest.add "n0" + of tfRequiresInit: dest.add "r1" + of tfNeedsFullInit: dest.add "n1" + of tfVarIsPtr: dest.add "v0" + of tfHasMeta: dest.add "m" + of tfHasGCedMem: dest.add "h0" + of tfPacked: dest.add "p0" + of tfHasStatic: dest.add "h1" + of tfGenericTypeParam: dest.add "g0" + of tfImplicitTypeParam: dest.add "i1" + of tfInferrableStatic: dest.add "i2" + of tfConceptMatchedTypeSym: dest.add "c0" + of tfExplicit: dest.add "e0" + of tfWildcard: dest.add "w" + of tfHasAsgn: dest.add "a" + of tfBorrowDot: dest.add "d" + of tfTriggersCompileTime: dest.add "t0" + of tfRefsAnonObj: dest.add "o" + of tfCovariant: dest.add "c1" + of tfWeakCovariant: dest.add "w0" + of tfContravariant: dest.add "c2" + of tfCheckedForDestructor: dest.add "c3" + of tfAcyclic: dest.add "a0" + of tfIncompleteStruct: dest.add "i3" + of tfCompleteStruct: dest.add "c4" + of tfExplicitCallConv: dest.add "e1" + of tfIsConstructor: dest.add "i4" + of tfEffectSystemWorkaround: dest.add "e2" + of tfIsOutParam: dest.add "i5" + of tfSendable: dest.add "s0" + of tfImplicitStatic: dest.add "i6" + + +proc parse*(t: typedesc[TTypeFlag]; s: string): set[TTypeFlag] = + result = {} + var i = 0 + while i < s.len: + case s[i] + of 'a': + if i+1 < s.len and s[i+1] == '0': + result.incl tfAcyclic + inc i + else: result.incl tfHasAsgn + of 'b': + if i+1 < s.len and s[i+1] == '0': + result.incl tfByRef + inc i + else: result.incl tfByCopy + of 'c': + if i+1 < s.len and s[i+1] == '0': + result.incl tfConceptMatchedTypeSym + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl tfCovariant + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl tfContravariant + inc i + elif i+1 < s.len and s[i+1] == '3': + result.incl tfCheckedForDestructor + inc i + elif i+1 < s.len and s[i+1] == '4': + result.incl tfCompleteStruct + inc i + else: result.incl tfCapturesEnv + of 'd': result.incl tfBorrowDot + of 'e': + if i+1 < s.len and s[i+1] == '0': + result.incl tfExplicit + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl tfExplicitCallConv + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl tfEffectSystemWorkaround + inc i + else: result.incl tfEnumHasHoles + of 'f': result.incl tfFinal + of 'g': + if i+1 < s.len and s[i+1] == '0': + result.incl tfGenericTypeParam + inc i + else: result.incl tfFromGeneric + of 'h': + if i+1 < s.len and s[i+1] == '0': + result.incl tfHasGCedMem + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl tfHasStatic + inc i + else: result.incl tfHasOwned + of 'i': + if i+1 < s.len and s[i+1] == '0': + result.incl tfIterator + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl tfImplicitTypeParam + inc i + elif i+1 < s.len and s[i+1] == '2': + result.incl tfInferrableStatic + inc i + elif i+1 < s.len and s[i+1] == '3': + result.incl tfIncompleteStruct + inc i + elif i+1 < s.len and s[i+1] == '4': + result.incl tfIsConstructor + inc i + elif i+1 < s.len and s[i+1] == '5': + result.incl tfIsOutParam + inc i + elif i+1 < s.len and s[i+1] == '6': + result.incl tfImplicitStatic + inc i + else: result.incl tfInheritable + of 'm': result.incl tfHasMeta + of 'n': + if i+1 < s.len and s[i+1] == '0': + result.incl tfNotNil + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl tfNeedsFullInit + inc i + else: result.incl tfNoSideEffect + of 'o': result.incl tfRefsAnonObj + of 'p': + if i+1 < s.len and s[i+1] == '0': + result.incl tfPacked + inc i + else: result.incl tfPartial + of 'r': + if i+1 < s.len and s[i+1] == '0': + result.incl tfRetType + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl tfRequiresInit + inc i + else: result.incl tfResolved + of 's': + if i+1 < s.len and s[i+1] == '0': + result.incl tfSendable + inc i + else: result.incl tfShallow + of 't': + if i+1 < s.len and s[i+1] == '0': + result.incl tfTriggersCompileTime + inc i + else: result.incl tfThread + of 'u': result.incl tfUnresolved + of 'v': + if i+1 < s.len and s[i+1] == '0': + result.incl tfVarIsPtr + inc i + else: result.incl tfVarargs + of 'w': + if i+1 < s.len and s[i+1] == '0': + result.incl tfWeakCovariant + inc i + else: result.incl tfWildcard + else: discard + inc i + +proc genFlags*(s: set[TLocFlag]; dest: var string) = + for e in s: + case e + of lfIndirect: dest.add "i" + of lfNoDeepCopy: dest.add "n" + of lfNoDecl: dest.add "d" + of lfDynamicLib: dest.add "l" + of lfExportLib: dest.add "e" + of lfHeader: dest.add "h" + of lfImportCompilerProc: dest.add "c" + of lfSingleUse: dest.add "s" + of lfEnforceDeref: dest.add "e0" + of lfPrepareForMutation: dest.add "p" + + +proc parse*(t: typedesc[TLocFlag]; s: string): set[TLocFlag] = + result = {} + var i = 0 + while i < s.len: + case s[i] + of 'c': result.incl lfImportCompilerProc + of 'd': result.incl lfNoDecl + of 'e': + if i+1 < s.len and s[i+1] == '0': + result.incl lfEnforceDeref + inc i + else: result.incl lfExportLib + of 'h': result.incl lfHeader + of 'i': result.incl lfIndirect + of 'l': result.incl lfDynamicLib + of 'n': result.incl lfNoDeepCopy + of 'p': result.incl lfPrepareForMutation + of 's': result.incl lfSingleUse + else: discard + inc i + +proc genFlags*(s: set[TOption]; dest: var string) = + for e in s: + case e + of optNone: dest.add "n" + of optObjCheck: dest.add "o" + of optFieldCheck: dest.add "f" + of optRangeCheck: dest.add "r" + of optBoundsCheck: dest.add "b" + of optOverflowCheck: dest.add "c" + of optRefCheck: dest.add "r0" + of optNaNCheck: dest.add "n0" + of optInfCheck: dest.add "i" + of optStaticBoundsCheck: dest.add "s" + of optStyleCheck: dest.add "s0" + of optAssert: dest.add "a" + of optLineDir: dest.add "l" + of optWarns: dest.add "w" + of optHints: dest.add "h" + of optOptimizeSpeed: dest.add "o0" + of optOptimizeSize: dest.add "o1" + of optStackTrace: dest.add "t" + of optStackTraceMsgs: dest.add "m" + of optLineTrace: dest.add "l0" + of optByRef: dest.add "b0" + of optProfiler: dest.add "p" + of optImplicitStatic: dest.add "i0" + of optTrMacros: dest.add "t0" + of optMemTracker: dest.add "m0" + of optSinkInference: dest.add "s1" + of optCursorInference: dest.add "c0" + of optImportHidden: dest.add "i1" + of optQuirky: dest.add "q" + + +proc parse*(t: typedesc[TOption]; s: string): set[TOption] = + result = {} + var i = 0 + while i < s.len: + case s[i] + of 'a': result.incl optAssert + of 'b': + if i+1 < s.len and s[i+1] == '0': + result.incl optByRef + inc i + else: result.incl optBoundsCheck + of 'c': + if i+1 < s.len and s[i+1] == '0': + result.incl optCursorInference + inc i + else: result.incl optOverflowCheck + of 'f': result.incl optFieldCheck + of 'h': result.incl optHints + of 'i': + if i+1 < s.len and s[i+1] == '0': + result.incl optImplicitStatic + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl optImportHidden + inc i + else: result.incl optInfCheck + of 'l': + if i+1 < s.len and s[i+1] == '0': + result.incl optLineTrace + inc i + else: result.incl optLineDir + of 'm': + if i+1 < s.len and s[i+1] == '0': + result.incl optMemTracker + inc i + else: result.incl optStackTraceMsgs + of 'n': + if i+1 < s.len and s[i+1] == '0': + result.incl optNaNCheck + inc i + else: result.incl optNone + of 'o': + if i+1 < s.len and s[i+1] == '0': + result.incl optOptimizeSpeed + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl optOptimizeSize + inc i + else: result.incl optObjCheck + of 'p': result.incl optProfiler + of 'q': result.incl optQuirky + of 'r': + if i+1 < s.len and s[i+1] == '0': + result.incl optRefCheck + inc i + else: result.incl optRangeCheck + of 's': + if i+1 < s.len and s[i+1] == '0': + result.incl optStyleCheck + inc i + elif i+1 < s.len and s[i+1] == '1': + result.incl optSinkInference + inc i + else: result.incl optStaticBoundsCheck + of 't': + if i+1 < s.len and s[i+1] == '0': + result.incl optTrMacros + inc i + else: result.incl optStackTrace + of 'w': result.incl optWarns + else: discard + inc i + diff --git a/compiler/icnif/icniftags.nim b/compiler/icnif/icniftags.nim new file mode 100644 index 0000000000000..f3c1fe3785f26 --- /dev/null +++ b/compiler/icnif/icniftags.nim @@ -0,0 +1,10 @@ +import "../../dist/nimony/src/lib" / [nifstreams] + +let + symIdTag* = registerTag("symId") + symTag* = registerTag("s") + typeIdTag* = registerTag("typeId") + typeTag* = registerTag("t") + sonsTag* = registerTag("sons") + + modIdTag* = registerTag("modId") diff --git a/compiler/icnif/nifbasics.nim b/compiler/icnif/nifbasics.nim new file mode 100644 index 0000000000000..83f91a2b27bda --- /dev/null +++ b/compiler/icnif/nifbasics.nim @@ -0,0 +1,19 @@ +import std/[assertions, tables] +import ".." / [ast, lineinfos, msgs, options] +import "../../dist/nimony/src/gear2" / modnames + +proc modname(moduleToNifSuffix: var Table[FileIndex, string]; module: PSym; conf: ConfigRef): string = + assert module.kind == skModule + let idx: FileIndex = module.position.FileIndex + # copied from ../nifgen.nim + result = moduleToNifSuffix.getOrDefault(idx) + if result.len == 0: + let fp = toFullPath(conf, idx) + result = moduleSuffix(fp, cast[seq[string]](conf.searchPaths)) + moduleToNifSuffix[idx] = result + #echo result, " -> ", fp + +proc toNifSym*(sym: PSym; moduleToNifSuffix: var Table[FileIndex, string]; conf: ConfigRef): string = + let module = sym.originatingModule + + result = sym.name.s & '.' & $sym.disamb & '.' & modname(moduleToNifSuffix, module, conf) diff --git a/compiler/icnif/nifdecoder.nim b/compiler/icnif/nifdecoder.nim new file mode 100644 index 0000000000000..37f9e888eceef --- /dev/null +++ b/compiler/icnif/nifdecoder.nim @@ -0,0 +1,405 @@ +import std / [assertions, tables] +import "../../dist/nimony/src/lib" / [bitabs, nifreader, nifstreams, nifcursors, lineinfos] +import ".." / [ast, idents, lineinfos, options, modules, modulegraphs, msgs, pathutils] +import enum2nif, icniftags, nifbasics + +type + NifProgram* = ref object + nifSymIdToPSym: Table[SymId, PSym] + moduleToNifSuffix: Table[FileIndex, string] # FileIndex (PSym.position) -> module suffix + + DecodeContext = object + graph: ModuleGraph + symbols: Table[ItemId, PSym] + types: Table[ItemId, PType] + modules: Table[int, FileIndex] # maps module id in NIF to FileIndex of the module + prog: NifProgram + +proc nodeKind(n: Cursor): TNodeKind {.inline.} = + assert n.kind == ParLe + pool.tags[n.tagId].parseNodeKind() + +proc expect(n: Cursor; k: set[NifKind]) = + if n.kind notin k: + when defined(debug): + writeStackTrace() + quit "[NIF decoder] expected: " & $k & " but got: " & $n.kind & toString n + +proc expect(n: Cursor; k: NifKind) {.inline.} = + expect n, {k} + +proc incExpect(n: var Cursor; k: set[NifKind]) = + inc n + expect n, k + +proc incExpect(n: var Cursor; k: NifKind) {.inline.} = + incExpect n, {k} + +proc skipParRi(n: var Cursor) = + expect n, {ParRi} + inc n + +proc expectTag(n: Cursor; tagId: TagId) = + if n.kind == ParLe and n.tagId == tagId: + discard + else: + when defined(debug): + writeStackTrace() + if n.kind != ParLe: + quit "[NIF decoder] expected: ParLe but got: " & $n.kind & toString n + else: + quit "[NIF decoder] expected: " & pool.tags[tagId] & " but got: " & pool.tags[n.tagId] & toString n + +proc incExpectTag(n: var Cursor; tagId: TagId) = + inc n + expectTag(n, tagId) + +when false: + proc expectTag(n: Cursor; tag: string) = + let id = pool.tags.getKeyId(tag) + if id == TagId(0): + when defined(debug): + writeStackTrace() + quit "[NIF decoder] expected: " & tag & " but doesn't exist" & toString n + else: + expectTag(n, id) + +proc fromNifLineInfo(c: var DecodeContext; n: Cursor): TLineInfo = + if n.info == NoLineInfo: + unknownLineInfo + else: + let info = pool.man.unpack(n.info) + c.graph.config.newLineInfo(pool.files[info.file].AbsoluteFile, info.line, info.col) + +proc fromNifModuleId(c: var DecodeContext; n: var Cursor): (FileIndex, int32) = + expect n, {ParLe, IntLit} + if n.kind == ParLe: + expectTag n, modIdTag + incExpect n, IntLit + let id = pool.integers[n.intId] + incExpect n, StringLit + let path = pool.strings[n.litId].AbsoluteFile + result = (fileInfoIdx(c.graph.config, path), id.int32) + assert id notin c.modules + c.modules[id] = result[0] + inc n + skipParRi n + elif n.kind == IntLit: + let id = pool.integers[n.intId] + result = (c.modules[id], id.int32) + inc n + +proc fromNifSymbol(c: var DecodeContext; n: var Cursor): PSym +proc fromNifType(c: var DecodeContext; n: var Cursor): PType +proc fromNif(c: var DecodeContext; n: var Cursor): PNode + +proc fromNifSymDef(c: var DecodeContext; n: var Cursor): PSym = + expectTag n, symIdTag + let info = c.fromNifLineInfo n + inc n + let (itemIdModule, nifModId) = c.fromNifModuleId(n) + expect n, IntLit + let itemId = pool.integers[n.intId].int32 + incExpect n, Ident + let ident = c.graph.cache.getIdent(pool.strings[n.litId]) + incExpect n, {Ident, DotToken} + let magic = if n.kind == Ident: pool.strings[n.litId].parseMagic else: mNone + incExpect n, {Ident, DotToken} + let flags = if n.kind == Ident: pool.strings[n.litId].parseSymFlags else: {} + incExpect n, {Ident, DotToken} + let options = if n.kind == Ident: pool.strings[n.litId].parseOptions else: {} + incExpect n, IntLit + let offset = pool.integers[n.intId].int32 + incExpect n, IntLit + let disamb = pool.integers[n.intId].int32 + incExpect n, ParLe + let kind = parseSymKind(pool.tags[n.tagId]) + inc n + + result = PSym(itemId: ItemId(module: itemIdModule.int32, item: itemId), + kind: kind, + magic: magic, + name: ident, + info: info, + flags: flags, + options: options, + offset: offset, + disamb: disamb) + + # PNode, PSym or PType type fields in PSym can have cycles. + # Add PSym to `c.symbols` before parsing these fields so that + # they can refer this PSym. + let nifItemId = ItemId(module: itemIdModule.int32, item: itemId) + assert nifItemId notin c.symbols + c.symbols[nifItemId] = result + + case kind + of skLet, skVar, skField, skForVar: + result.guard = c.fromNifSymbol n + expect n, IntLit + result.bitsize = pool.integers[n.intId] + incExpect n, IntLit + result.alignment = pool.integers[n.intId] + inc n + else: + discard + skipParRi n + result.position = if kind == skModule: + c.fromNifModuleId(n)[0].int + else: + expect n, IntLit + let p = pool.integers[n.intId] + inc n + p + + result.typ = c.fromNifType n + result.setOwner(c.fromNifSymbol n) + result.ast = c.fromNif n + + expect n, Ident + result.loc.k = pool.strings[n.litId].parseLocKind() + incExpect n, StringLit + result.loc.snippet.add pool.strings[n.litId] + inc n + result.constraint = c.fromNif n + result.instantiatedFrom = c.fromNifSymbol n + skipParRi n + + if sfExported in flags or kind == skModule: + let nifSym = toNifSym(result, c.prog.moduleToNifSuffix, c.graph.config) + let symId = pool.syms.getOrIncl(nifSym) + c.prog.nifSymIdToPSym[symId] = result + +proc fromNifTypeDef(c: var DecodeContext; n: var Cursor): PType = + expectTag n, typeIdTag + inc n + let (itemIdModule, nifModId) = c.fromNifModuleId(n) + expect n, IntLit + let itemId = pool.integers[n.intId].int32 + incExpect n, Ident + let kind = parseTypeKind(pool.strings[n.litId]) + incExpect n, {Ident, DotToken} + let flags = if n.kind == Ident: pool.strings[n.litId].parseTypeFlags else: {} + inc n + + result = PType(itemId: ItemId(module: itemIdModule.int32, item: itemId), + kind: kind, + flags: flags) + let nifItemId = ItemId(module: nifModId, item: itemId) + assert nifItemId notin c.types + c.types[nifItemId] = result + + expect n, {DotToken, ParLe} + if n.kind == DotToken: + inc n + else: + expectTag n, sonsTag + inc n + while n.kind != ParRi: + result.addAllowNil c.fromNifType n + inc n + result.n = c.fromNif n + result.setOwner c.fromNifSymbol n + result.sym = c.fromNifSymbol n + skipParRi n + +proc fromNifNodeFlags(n: var Cursor): set[TNodeFlag] = + if n.kind == DotToken: + result = {} + inc n + elif n.kind == Ident: + result = parseNodeFlags(pool.strings[n.litId]) + inc n + else: + assert false, "expected Node flag but got " & $n.kind + +proc fromNifSymbol(c: var DecodeContext; n: var Cursor): PSym = + if n.kind == DotToken: + result = nil + inc n + elif n.kind == Symbol: + if n.symId notin c.prog.nifSymIdToPSym: + # TODO: Support import statement and remove this branch + #echo pool.syms[n.symId], " is not found" + result = nil + else: + result = c.prog.nifSymIdToPSym[n.symId] + inc n + else: + expect n, ParLe + if n.tagId == symIdTag: + result = c.fromNifSymDef n + elif n.tagId == symTag: + incExpect n, IntLit + let nifModId = c.modules[pool.integers[n.intId].int32] + incExpect n, IntLit + let item = pool.integers[n.intId].int32 + let nifItemId = ItemId(module: nifModId.int32, item: item) + result = c.symbols[nifItemId] + inc n + skipParRi n + else: + assert false, "expected symbol tag but got " & pool.tags[n.tagId] + +proc fromNifType(c: var DecodeContext; n: var Cursor): PType = + if n.kind == DotToken: + result = nil + inc n + else: + expect n, ParLe + if n.tagId == typeIdTag: + result = c.fromNifTypeDef n + elif n.tagId == typeTag: + incExpect n, IntLit + let nifModId = pool.integers[n.intId].int32 + incExpect n, IntLit + let item = pool.integers[n.intId].int32 + let nifItemId = ItemId(module: nifModId, item: item) + result = c.types[nifItemId] + inc n + skipParRi n + else: + assert false, "expected type tag but got " & pool.tags[n.tagId] + +template withNode(c: var DecodeContext; n: var Cursor; result: PNode; kind: TNodeKind; body: untyped) = + let info = c.fromNifLineInfo(n) + incExpect n, {DotToken, Ident} + let flags = fromNifNodeFlags n + result = newNodeI(kind, info) + result.flags = flags + result.typ = c.fromNifType n + body + skipParRi n + +proc fromNif(c: var DecodeContext; n: var Cursor): PNode = + result = nil + case n.kind: + of DotToken: + result = nil + inc n + of ParLe: + let kind = n.nodeKind + case kind: + of nkEmpty: + result = newNodeI(nkEmpty, c.fromNifLineInfo(n)) + incExpect n, {Ident, DotToken} + let flags = fromNifNodeFlags n + result.flags = flags + skipParRi n + of nkIdent: + let info = c.fromNifLineInfo(n) + incExpect n, {DotToken, Ident} + let flags = fromNifNodeFlags n + let typ = c.fromNifType n + expect n, Ident + result = newIdentNode(c.graph.cache.getIdent(pool.strings[n.litId]), info) + inc n + result.flags = flags + result.typ = typ + skipParRi n + of nkSym: + c.withNode n, result, kind: + result.sym = c.fromNifSymbol n + of nkCharLit: + c.withNode n, result, kind: + expect n, CharLit + result.intVal = n.charLit.int + inc n + of nkIntLit .. nkInt64Lit: + c.withNode n, result, kind: + expect n, IntLit + result.intVal = pool.integers[n.intId] + inc n + of nkUIntLit .. nkUInt64Lit: + c.withNode n, result, kind: + expect n, UIntLit + result.intVal = cast[BiggestInt](pool.uintegers[n.uintId]) + inc n + of nkFloatLit .. nkFloat128Lit: + c.withNode n, result, kind: + if n.kind == FloatLit: + result.floatVal = pool.floats[n.floatId] + inc n + elif n.kind == ParLe: + case pool.tags[n.tagId] + of "inf": + result.floatVal = Inf + of "nan": + result.floatVal = NaN + of "neginf": + result.floatVal = NegInf + else: + assert false, "expected float literal but got " & pool.tags[n.tagId] + inc n + skipParRi n + else: + assert false, "expected float literal but got " & $n.kind + of nkStrLit .. nkTripleStrLit: + c.withNode n, result, kind: + expect n, StringLit + result.strVal = pool.strings[n.litId] + inc n + of nkNilLit: + c.withNode n, result, kind: + discard + of nkNone: + assert false, "Unknown tag " & pool.tags[n.tagId] + else: + c.withNode n, result, kind: + while n.kind != ParRi: + result.addAllowNil c.fromNif n + else: + assert false, "Not yet implemented " & $n.kind + +proc loadNif(stream: var Stream; graph: ModuleGraph; prog: NifProgram): PNode = + discard processDirectives(stream.r) + + var buf = fromStream(stream) + var n = beginRead(buf) + var c = DecodeContext(graph: graph, prog: prog) + + var n2 = n + var nested = 0 + while true: + if n2.info != NoLineInfo: + break + elif n2.kind == EofToken: + break + elif n2.kind == ParLe: + inc nested + elif n2.kind == ParRi: + dec nested + if nested == 0: break + inc n2 + assert n2.info != NoLineInfo + let info = pool.man.unpack(n2.info) + let fileIdx = c.graph.config.fileInfoIdx(pool.files[info.file].AbsoluteFile) + var currentModule = graph.newModule(fileIdx) + if currentModule.itemId.module == 0'i32: + currentModule.flags = {sfMainModule, sfSystemModule} + + c.symbols[currentModule.itemId] = currentModule + let nifSym = toNifSym(currentModule, c.prog.moduleToNifSuffix, c.graph.config) + let symId = pool.syms.getOrIncl(nifSym) + c.prog.nifSymIdToPSym[symId] = currentModule + + result = fromNif(c, n) + + endRead(buf) + +proc loadNifFile*(infile: AbsoluteFile; graph: ModuleGraph; prog: NifProgram): PNode = + var stream = nifstreams.open(infile.string) + result = loadNif(stream, graph, prog) + stream.close + +proc loadNifFromBuffer*(strbuf: sink string; graph: ModuleGraph; prog: NifProgram): PNode = + var stream = nifstreams.openFromBuffer(strbuf) + result = loadNif(stream, graph, prog) + +when isMainModule: + import std/cmdline + + if paramCount() > 0: + var graph = newModuleGraph(newIdentCache(), newConfigRef()) + var node = loadNifFile(paramStr(1).toAbsolute(toAbsoluteDir(".")), graph) + debug(node) diff --git a/compiler/icnif/nifencoder.nim b/compiler/icnif/nifencoder.nim new file mode 100644 index 0000000000000..e35ea4ab8abfd --- /dev/null +++ b/compiler/icnif/nifencoder.nim @@ -0,0 +1,218 @@ +import std / [assertions, sets, tables] +import ".." / [ast, idents, lineinfos, msgs, options] +import "../../dist/nimony/src/lib" / [bitabs, nifstreams, nifcursors, lineinfos] +import enum2nif, icniftags, nifbasics + +type + EncodeContext = object + conf: ConfigRef + currentModule: PSym + decodedSyms: HashSet[ItemId] + decodedTypes: HashSet[ItemId] + decodedFileIndices: HashSet[FileIndex] + dest: TokenBuf + moduleToNifSuffix: Table[FileIndex, string] # FileIndex (PSym.position) -> module suffix + +proc initEncodeContext(conf: ConfigRef; currentModule: PSym): EncodeContext = + result = EncodeContext(conf: conf, + currentModule: currentModule, + dest: createTokenBuf()) + result.decodedSyms.incl(currentModule.itemId) + +template buildTree(dest: var TokenBuf; tag: TagId; body: untyped) = + dest.addParLe tag + body + dest.addParRi + +template buildTree(dest: var TokenBuf; tag: string; body: untyped) = + buildTree dest, pool.tags.getOrIncl(tag), body + +proc writeFlags[E](dest: var TokenBuf; flags: set[E]) = + var flagsAsIdent = "" + genFlags(flags, flagsAsIdent) + if flagsAsIdent.len > 0: + dest.addIdent flagsAsIdent + else: + dest.addDotToken + +proc toNif(c: var EncodeContext; info: TLineInfo): PackedLineInfo = + if info == unknownLineInfo: + NoLineInfo + else: + let fileId = pool.files.getOrIncl(c.conf.toFullPath(info.fileIndex)) + pack(pool.man, fileId, info.line.int32, info.col) + +proc toNifModuleId(c: var EncodeContext; moduleId: int) = + # `ItemId.module` in PType and PSym (and `PSym.position` when it is skModule) are module's FileIndex + # but it cannot be directly encoded as the uniqueness of it can broke + # if any import/include statements are changed. + if not c.decodedFileIndices.containsOrIncl(moduleId.FileIndex): + c.dest.buildTree modIdTag: + c.dest.addIntLit moduleId + let path = toFullPath(c.conf, moduleId.FileIndex) + c.dest.addStrLit path + else: + c.dest.addIntLit moduleId + +proc toNif(c: var EncodeContext; sym: PSym) +proc toNif(c: var EncodeContext; typ: PType) +proc toNif(c: var EncodeContext; n: PNode) + +proc toNifDef(c: var EncodeContext; sym: PSym) = + c.dest.addParLe symIdTag, c.toNif sym.info + c.toNifModuleId sym.itemId.module + c.dest.addIntLit sym.itemId.item + c.dest.addIdent sym.name.s + if sym.magic == mNone: + c.dest.addDotToken + else: + c.dest.addIdent toNifTag(sym.magic) + c.dest.writeFlags sym.flags + c.dest.writeFlags sym.options + c.dest.addIntLit sym.offset + c.dest.addIntLit sym.disamb + c.dest.buildTree sym.kind.toNifTag: + case sym.kind + of skLet, skVar, skField, skForVar: + c.toNif sym.guard + c.dest.addIntLit sym.bitsize + c.dest.addIntLit sym.alignment + else: + discard + if sym.kind == skModule: + c.toNifModuleId sym.position + else: + c.dest.addIntLit sym.position + c.toNif sym.typ + c.toNif sym.owner + c.toNif sym.ast # drastically increase output NIF size! -- Yeah, no shit Sherlock. + c.dest.addIdent toNifTag(sym.loc.k) + c.dest.addStrLit sym.loc.snippet + c.toNif sym.constraint + c.toNif sym.instantiatedFrom + c.dest.addParRi + +proc toNifDef(c: var EncodeContext; typ: PType) = + c.dest.buildTree typeIdTag: + c.toNifModuleId typ.itemId.module + c.dest.addIntLit typ.itemId.item + c.dest.addIdent toNifTag(typ.kind) + c.dest.writeFlags typ.flags + # following PType or PSym type field can have cycles but this proc should not called recursively + # as c.decodedTypes prevents it. + if typ.len == 0: + c.dest.addDotToken + else: + c.dest.buildTree sonsTag: + for ch in typ.kids: + c.toNif ch + + c.toNif typ.n + c.toNif typ.owner + c.toNif typ.sym + +proc toNif(c: var EncodeContext; sym: PSym) = + if sym == nil: + c.dest.addDotToken() + elif sym.owner != nil and sym.originatingModule != c.currentModule: + let nifSym = toNifSym(sym, c.moduleToNifSuffix, c.conf) + c.dest.addSymUse(pool.syms.getOrIncl(nifSym), NoLineInfo) + else: + if not c.decodedSyms.containsOrIncl(sym.itemId): + c.toNifDef sym + else: + c.dest.buildTree symTag: + c.dest.addIntLit sym.itemId.module + c.dest.addIntLit sym.itemId.item + +proc toNif(c: var EncodeContext; typ: PType) = + if typ == nil: + c.dest.addDotToken() + else: + if not c.decodedTypes.containsOrIncl(typ.itemId): + c.toNifDef typ + else: + c.dest.buildTree typeTag: + c.dest.addIntLit typ.itemId.module + c.dest.addIntLit typ.itemId.item + +proc writeNodeFlags(dest: var TokenBuf; flags: set[TNodeFlag]) {.inline.} = + writeFlags dest, flags + +template withNode(c: var EncodeContext; n: PNode; body: untyped) = + c.dest.addParLe pool.tags.getOrIncl(toNifTag(n.kind)), c.toNif n.info + writeNodeFlags(c.dest, n.flags) + c.toNif n.typ + body + c.dest.addParRi + +proc toNif(c: var EncodeContext; n: PNode) = + if n == nil: + c.dest.addDotToken + else: + case n.kind: + of nkEmpty: + let info = c.toNif n.info + c.dest.addParLe pool.tags.getOrIncl(toNifTag(nkEmpty)), info + c.dest.writeNodeFlags(n.flags) + c.dest.addParRi + of nkIdent: + # nkIdent uses flags and typ when it is a generic parameter + c.withNode n: + c.dest.addIdent n.ident.s + of nkSym: + when false: + echo "nkSym: ", n.sym.name.s + if n.sym.kind == skModule: + echo "position = ", n.sym.position + debug(n.sym) + var o = n.sym.owner + for i in 0 .. 20: + if o == nil: + break + echo "owner ", i, ":" + if o.kind == skModule: + echo "position = ", o.position + debug(o) + o = o.owner + # PNode.typ and PNode.sym.typ are different in `int` nkSym Node in following statement: + # type TestInt = int + c.withNode n: + c.toNif n.sym + of nkCharLit: + c.withNode n: + c.dest.add charToken(n.intVal.char, NoLineInfo) + of nkIntLit .. nkInt64Lit: + c.withNode n: + c.dest.addIntLit n.intVal + of nkUIntLit .. nkUInt64Lit: + c.withNode n: + c.dest.addUIntLit cast[BiggestUInt](n.intVal) + of nkFloatLit .. nkFloat128Lit: + c.withNode n: + c.dest.add floatToken(pool.floats.getOrIncl(n.floatVal), NoLineInfo) + of nkStrLit .. nkTripleStrLit: + c.withNode n: + c.dest.addStrLit n.strVal + of nkNilLit: + c.withNode n: + discard + else: + #assert n.kind in {nkArgList, nkBracket, nkRecList, nkPragma, nkType} or n.len > 0, $n.kind + c.withNode(n): + for i in 0 ..< n.len: + c.toNif n[i] + +proc saveNif(c: var EncodeContext; n: PNode): string = + toNif c, n + + result = "(.nif24)\n" & toString(c.dest) + +proc saveNifFile*(n: PNode; conf: ConfigRef; module: PSym) = + let outfile = module.name.s & ".nif" + var c = initEncodeContext(conf, module) + writeFile outfile, saveNif(c, n) + +proc saveNifToBuffer*(n: PNode; conf: ConfigRef; module: PSym): string = + var c = initEncodeContext(conf, module) + result = saveNif(c, n) diff --git a/compiler/importer.nim b/compiler/importer.nim index 23814ae50f5dd..8ff3bcfdb3e05 100644 --- a/compiler/importer.nim +++ b/compiler/importer.nim @@ -245,7 +245,8 @@ proc importModuleAs(c: PContext; n: PNode, realModule: PSym, importHidden, track # avoids modifying `realModule`, see D20201209T194412 for `import {.all.}` result = createModuleAliasImpl(realModule.name) if importHidden: - result.options.incl optImportHidden + ensureMutable result + result.optionsImpl.incl optImportHidden let moduleIdent = if n.kind in {nkInfix, nkImportAs}: n[^1] else: n result.info = moduleIdent.info if trackUnusedImport: diff --git a/compiler/injectdestructors.nim b/compiler/injectdestructors.nim index 1f2cff7e5f393..223783a3f9161 100644 --- a/compiler/injectdestructors.nim +++ b/compiler/injectdestructors.nim @@ -1155,7 +1155,7 @@ proc ownsData(c: var Con; s: var Scope; orig: PNode; flags: set[MoveOrCopyFlag]) if n.kind in nkCallKinds and n.typ != nil and hasDestructor(c, n.typ): result = newNodeIT(nkStmtListExpr, orig.info, orig.typ) let tmp = c.getTemp(s, n.typ, n.info) - tmp.sym.flags.incl sfSingleUsedTemp + tmp.sym.flagsImpl.incl sfSingleUsedTemp result.add newTree(nkFastAsgn, tmp, copyTree(n)) s.final.add c.genDestroy(tmp) n[] = tmp[] @@ -1330,7 +1330,7 @@ proc addSinkCopy(c: var Con; s: var Scope; sinkParams: seq[PSym]; n: PNode): PNo for param in sinkParams: if param.id in mutatedSet: let newSym = newSym(skTemp, getIdent(c.graph.cache, "sinkCopy"), c.idgen, param.owner, n.info) - newSym.flags.incl sfFromGeneric + newSym.flagsImpl.incl sfFromGeneric newSym.typ = param.typ.elementType mapping[param.id] = newSym let v = newNodeI(nkVarSection, n.info) diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index fd8ef583d00a3..acd49110abc0e 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -277,7 +277,8 @@ proc mangleName(m: BModule, s: PSym): Rope = else: result.add("_") result.add(rope(s.id)) - s.loc.snippet = result + ensureMutable s + s.locImpl.snippet = result proc escapeJSString(s: string): string = result = newStringOfCap(s.len + s.len shr 2) @@ -1002,7 +1003,8 @@ proc genTry(p: PProc, n: PNode, r: var TCompRes) = # If some branch requires a local alias introduce it here. This is needed # since JS cannot do ``catch x as y``. if excAlias != nil: - excAlias.sym.loc.snippet = mangleName(p.module, excAlias.sym) + ensureMutable excAlias.sym + excAlias.sym.locImpl.snippet = mangleName(p.module, excAlias.sym) lineF(p, "var $1 = lastJSError;$n", excAlias.sym.loc.snippet) gen(p, n[i][^1], a) moveInto(p, a, r) @@ -1135,7 +1137,8 @@ proc genBlock(p: PProc, n: PNode, r: var TCompRes) = # named block? if (n[0].kind != nkSym): internalError(p.config, n.info, "genBlock") var sym = n[0].sym - sym.loc.k = locOther + ensureMutable sym + sym.locImpl.k = locOther sym.position = idx+1 let labl = p.unique lineF(p, "Label$1: {$n", [labl.rope]) @@ -1234,7 +1237,8 @@ proc generateHeader(p: PProc, prc: PSym): Rope = # to keep it simple let env = prc.ast[paramsPos].lastSon assert env.kind == nkSym, "env is missing" - env.sym.loc.snippet = "this" + ensureMutable env.sym + env.sym.locImpl.snippet = "this" for i in 1.. 0 @@ -2577,7 +2591,9 @@ proc genObjConstr(p: PProc, n: PNode, r: var TCompRes) = let val = it[1] gen(p, val, a) var f = it[0].sym - if f.loc.snippet == "": f.loc.snippet = mangleName(p.module, f) + if f.loc.snippet == "": + ensureMutable f + f.locImpl.snippet = mangleName(p.module, f) fieldIDs.incl(lookupFieldAgain(n.typ.skipTypes({tyDistinct}), f).id) let typ = val.typ.skipTypes(abstractInst) diff --git a/compiler/lambdalifting.nim b/compiler/lambdalifting.nim index e9195644e13a3..0fca8b980f70c 100644 --- a/compiler/lambdalifting.nim +++ b/compiler/lambdalifting.nim @@ -161,7 +161,7 @@ proc getClosureIterResult*(g: ModuleGraph; iter: PSym; idgen: IdGenerator): PSym # XXX a bit hacky: result = newSym(skResult, getIdent(g.cache, ":result"), idgen, iter, iter.info, {}) result.typ = iter.typ.returnType - incl(result.flags, sfUsed) + incl(result.flagsImpl, sfUsed) iter.ast.add newSymNode(result) proc addHiddenParam(routine: PSym, param: PSym) = @@ -228,7 +228,7 @@ proc makeClosure*(g: ModuleGraph; idgen: IdGenerator; prc: PSym; env: PNode; inf #if isClosureIterator(result.typ): createTypeBoundOps(g, nil, result.typ, info, idgen) if tfHasAsgn in result.typ.flags or optSeqDestructors in g.config.globalOptions: - prc.flags.incl sfInjectDestructors + prc.incl sfInjectDestructors template liftingHarmful(conf: ConfigRef; owner: PSym): bool = ## lambda lifting can be harmful for JS-like code generators. @@ -240,7 +240,7 @@ proc createTypeBoundOpsLL(g: ModuleGraph; refType: PType; info: TLineInfo; idgen createTypeBoundOps(g, nil, refType.elementType, info, idgen) createTypeBoundOps(g, nil, refType, info, idgen) if tfHasAsgn in refType.flags or optSeqDestructors in g.config.globalOptions: - owner.flags.incl sfInjectDestructors + owner.incl sfInjectDestructors proc genCreateEnv(env: PNode): PNode = var c = newNodeIT(nkObjConstr, env.info, env.typ) @@ -414,7 +414,7 @@ proc addClosureParam(c: var DetectionPass; fn: PSym; info: TLineInfo) = let t = c.getEnvTypeForOwner(owner, info) if cp == nil: cp = newSym(skParam, getIdent(c.graph.cache, paramName), c.idgen, fn, fn.info) - incl(cp.flags, sfFromGeneric) + incl(cp.flagsImpl, sfFromGeneric) cp.typ = t addHiddenParam(fn, cp) elif cp.typ != t and fn.kind != skIterator: @@ -624,7 +624,7 @@ proc rawClosureCreation(owner: PSym; if owner.kind != skMacro: createTypeBoundOps(d.graph, nil, fieldAccess.typ, env.info, d.idgen) if tfHasAsgn in fieldAccess.typ.flags or optSeqDestructors in d.graph.config.globalOptions: - owner.flags.incl sfInjectDestructors + owner.incl sfInjectDestructors let upField = lookupInRecord(env.typ.skipTypes({tyOwned, tyRef, tyPtr}).n, getIdent(d.graph.cache, upName)) if upField != nil: @@ -666,7 +666,7 @@ proc closureCreationForIter(owner: PSym, iter: PNode; result = newNodeIT(nkStmtListExpr, iter.info, iter.sym.typ) let iterOwner = iter.sym.skipGenericOwner var v = newSym(skVar, getIdent(d.graph.cache, envName), d.idgen, iterOwner, iter.info) - incl(v.flags, sfShadowed) + incl(v.flagsImpl, sfShadowed) v.typ = asOwnedRef(d, getHiddenParam(d.graph, iter.sym).typ) var vnode: PNode if iterOwner.isIterator: diff --git a/compiler/liftdestructors.nim b/compiler/liftdestructors.nim index 5d8fbc179dd82..b4b2fd28da8b2 100644 --- a/compiler/liftdestructors.nim +++ b/compiler/liftdestructors.nim @@ -284,7 +284,7 @@ proc fillBodyObjT(c: var TLiftCtx; t: PType, body, x, y: PNode) = body.add genIf(c, cond, newTreeI(nkReturnStmt, c.info, newNodeI(nkEmpty, c.info))) var temp = newSym(skTemp, getIdent(c.g.cache, lowerings.genPrefix), c.idgen, c.fn, c.info) temp.typ = x.typ - incl(temp.flags, sfFromGeneric) + incl(temp, sfFromGeneric) var v = newNodeI(nkVarSection, c.info) let blob = newSymNode(temp) v.addVar(blob, x) @@ -393,7 +393,8 @@ proc considerAsgnOrSink(c: var TLiftCtx; t: PType; body, x, y: PNode; if op != nil and op != c.fn and (sfOverridden in op.flags or destructorOverridden): if sfError in op.flags: - incl c.fn.flags, sfError + ensureMutable c.fn + incl c.fn.flagsImpl, sfError #else: # markUsed(c.g.config, c.info, op, c.g.usageSym) onUse(c.info, op) @@ -419,7 +420,8 @@ proc considerAsgnOrSink(c: var TLiftCtx; t: PType; body, x, y: PNode; if op == nil: op = produceSym(c.g, c.c, t, c.kind, c.info, c.idgen) if sfError in op.flags: - incl c.fn.flags, sfError + ensureMutable c.fn + incl c.fn.flagsImpl, sfError #else: # markUsed(c.g.config, c.info, op, c.g.usageSym) onUse(c.info, op) @@ -535,7 +537,7 @@ proc considerUserDefinedOp(c: var TLiftCtx; t: PType; body, x, y: PNode): bool = proc declareCounter(c: var TLiftCtx; body: PNode; first: BiggestInt): PNode = var temp = newSym(skTemp, getIdent(c.g.cache, lowerings.genPrefix), c.idgen, c.fn, c.info) temp.typ = getSysType(c.g, body.info, tyInt) - incl(temp.flags, sfFromGeneric) + incl(temp.flagsImpl, sfFromGeneric) var v = newNodeI(nkVarSection, c.info) result = newSymNode(temp) @@ -545,7 +547,7 @@ proc declareCounter(c: var TLiftCtx; body: PNode; first: BiggestInt): PNode = proc declareTempOf(c: var TLiftCtx; body: PNode; value: PNode): PNode = var temp = newSym(skTemp, getIdent(c.g.cache, lowerings.genPrefix), c.idgen, c.fn, c.info) temp.typ = value.typ - incl(temp.flags, sfFromGeneric) + incl(temp.flagsImpl, sfFromGeneric) var v = newNodeI(nkVarSection, c.info) result = newSymNode(temp) @@ -1120,8 +1122,7 @@ proc symDupPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttache n[bodyPos] = newNodeI(nkStmtList, info) n[resultPos] = newSymNode(res) result.ast = n - incl result.flags, sfFromGeneric - incl result.flags, sfGeneratedOp + incl result.flagsImpl, {sfFromGeneric, sfGeneratedOp} proc symPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttachedOp; info: TLineInfo; idgen: IdGenerator; isDiscriminant = false): PSym = @@ -1163,10 +1164,10 @@ proc symPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttachedOp n[paramsPos] = result.typ.n n[bodyPos] = newNodeI(nkStmtList, info) result.ast = n - incl result.flags, sfFromGeneric - incl result.flags, sfGeneratedOp + incl result.flagsImpl, sfFromGeneric + incl result.flagsImpl, sfGeneratedOp if kind == attachedWasMoved: - incl result.flags, sfNoSideEffect + incl result.flagsImpl, sfNoSideEffect incl result.typ.flags, tfNoSideEffect proc genTypeFieldCopy(c: var TLiftCtx; t: PType; body, x, y: PNode) = @@ -1200,7 +1201,8 @@ proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp; if kind == attachedSink and destructorOverridden(g, typ): ## compiler can use a combination of `=destroy` and memCopy for sink op - dest.flags.incl sfCursor + ensureMutable dest + dest.flagsImpl.incl sfCursor let op = getAttachedOp(g, typ, attachedDestructor) result.ast[bodyPos].add newOpCall(a, op, if op.typ.firstParamType.kind == tyVar: d[0] else: d) result.ast[bodyPos].add newAsgnStmt(d, src) @@ -1222,13 +1224,15 @@ proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp; genTypeFieldCopy(a, typ, result.ast[bodyPos], d, src) if not a.canRaise: - incl result.flags, sfNeverRaises + ensureMutable result + incl result.flagsImpl, sfNeverRaises result.ast[pragmasPos] = newNodeI(nkPragma, info) result.ast[pragmasPos].add newTree(nkExprColonExpr, newIdentNode(g.cache.getIdent("raises"), info), newNodeI(nkBracket, info)) if kind == attachedDestructor: - incl result.options, optQuirky + ensureMutable result + incl result.optionsImpl, optQuirky completePartialOp(g, idgen.module, typ, kind, result) @@ -1253,7 +1257,9 @@ proc produceDestructorForDiscriminator*(g: ModuleGraph; typ: PType; field: PSym, result.ast[bodyPos].add v let placeHolder = newNodeIT(nkSym, info, getSysType(g, info, tyPointer)) fillBody(a, typ, result.ast[bodyPos], d, placeHolder) - if not a.canRaise: incl result.flags, sfNeverRaises + if not a.canRaise: + ensureMutable result + incl result.flagsImpl, sfNeverRaises template liftTypeBoundOps*(c: PContext; typ: PType; info: TLineInfo) = diff --git a/compiler/lookups.nim b/compiler/lookups.nim index acaad9d9b49f5..bbc5b4df40443 100644 --- a/compiler/lookups.nim +++ b/compiler/lookups.nim @@ -311,7 +311,7 @@ proc errorSym*(c: PContext, ident: PIdent, info: TLineInfo): PSym = ## creates an error symbol to avoid cascading errors (for IDE support) result = newSym(skError, ident, c.idgen, getCurrOwner(c), info, {}) result.typ = errorType(c) - incl(result.flags, sfDiscardable) + incl(result.flagsImpl, sfDiscardable) # pretend it's from the top level scope to prevent cascading errors: if c.config.cmd != cmdInteractive and c.compilesContextId == 0: c.moduleScope.addSym(result) diff --git a/compiler/lowerings.nim b/compiler/lowerings.nim index a55d2776d8596..95359c6a665a7 100644 --- a/compiler/lowerings.nim +++ b/compiler/lowerings.nim @@ -82,7 +82,7 @@ proc lowerTupleUnpacking*(g: ModuleGraph; n: PNode; idgen: IdGenerator; owner: P var temp = newSym(skTemp, getIdent(g.cache, genPrefix), idgen, owner, value.info, g.config.options) temp.typ = skipTypes(value.typ, abstractInst) - incl(temp.flags, sfFromGeneric) + incl(temp.flagsImpl, sfFromGeneric) tempAsNode = newSymNode(temp) var v = newNodeI(nkVarSection, value.info) @@ -103,7 +103,7 @@ proc evalOnce*(g: ModuleGraph; value: PNode; idgen: IdGenerator; owner: PSym): P var temp = newSym(skTemp, getIdent(g.cache, genPrefix), idgen, owner, value.info, g.config.options) temp.typ = skipTypes(value.typ, abstractInst) - incl(temp.flags, sfFromGeneric) + incl(temp.flagsImpl, sfFromGeneric) var v = newNodeI(nkLetSection, value.info) let tempAsNode = newSymNode(temp) @@ -127,8 +127,8 @@ proc lowerSwap*(g: ModuleGraph; n: PNode; idgen: IdGenerator; owner: PSym): PNod # note: cannot use 'skTemp' here cause we really need the copy for the VM :-( var temp = newSym(skVar, getIdent(g.cache, genPrefix), idgen, owner, n.info, owner.options) temp.typ = n[1].typ - incl(temp.flags, sfFromGeneric) - incl(temp.flags, sfGenSym) + incl(temp.flagsImpl, sfFromGeneric) + incl(temp.flagsImpl, sfGenSym) var v = newNodeI(nkVarSection, n.info) let tempAsNode = newSymNode(temp) @@ -153,7 +153,7 @@ proc createObj*(g: ModuleGraph; idgen: IdGenerator; owner: PSym, info: TLineInfo result.n = newNodeI(nkRecList, info) let s = newSym(skType, getIdent(g.cache, "Env_" & toFilename(g.config, info) & "_" & $owner.name.s), idgen, owner, info, owner.options) - incl s.flags, sfAnon + incl s.flagsImpl, sfAnon s.typ = result result.sym = s diff --git a/compiler/main.nim b/compiler/main.nim index 08b57722c4e6a..377c85b6e1921 100644 --- a/compiler/main.nim +++ b/compiler/main.nim @@ -200,7 +200,7 @@ proc commandInteractive(graph: ModuleGraph) = discard graph.compilePipelineModule(fileInfoIdx(graph.config, graph.config.projectFull), {}) else: var m = graph.makeStdinModule() - incl(m.flags, sfMainModule) + incl(m, sfMainModule) var idgen = IdGenerator(module: m.itemId.module, symId: m.itemId.item, typeId: 0) let s = llStreamOpenStdIn(onPrompt = proc() = flushDot(graph.config)) discard processPipelineModule(graph, m, idgen, s) diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index 51b9e5e4eb992..cb6772dda1448 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -681,13 +681,13 @@ proc markDirty*(g: ModuleGraph; fileIdx: FileIndex) = if m != nil: g.suggestSymbols.del(fileIdx) g.suggestErrors.del(fileIdx) - incl m.flags, sfDirty + incl m.flagsImpl, sfDirty proc unmarkAllDirty*(g: ModuleGraph) = for i in 0i32..= 0: - result.flags.incl sfWasGenSym + result.flagsImpl.incl sfWasGenSym #if kind in {skForVar, skLet, skVar} and result.owner.kind == skModule: # incl(result.flags, sfGlobal) when defined(nimsuggest): diff --git a/compiler/semdata.nim b/compiler/semdata.nim index e3be90014ee95..7e0bdf448b972 100644 --- a/compiler/semdata.nim +++ b/compiler/semdata.nim @@ -568,7 +568,7 @@ proc makeTypeDesc*(c: PContext, typ: PType): PType = proc symFromType*(c: PContext; t: PType, info: TLineInfo): PSym = if t.sym != nil: return t.sym result = newSym(skType, getIdent(c.cache, "AnonType"), c.idgen, t.owner, info) - result.flags.incl sfAnon + result.flagsImpl.incl sfAnon result.typ = t proc symNodeFromType*(c: PContext, t: PType, info: TLineInfo): PNode = @@ -577,7 +577,7 @@ proc symNodeFromType*(c: PContext, t: PType, info: TLineInfo): PNode = proc markIndirect*(c: PContext, s: PSym) {.inline.} = if s.kind in {skProc, skFunc, skConverter, skMethod, skIterator}: - incl(s.flags, sfAddrTaken) + incl(s.flagsImpl, sfAddrTaken) # XXX add to 'c' for global analysis proc illFormedAst*(n: PNode; conf: ConfigRef) = @@ -685,7 +685,7 @@ proc analyseIfAddressTaken(c: PContext, n: PNode, isOutParam: bool): PNode = # n.sym.typ can be nil in 'check' mode ... if n.sym.typ != nil and skipTypes(n.sym.typ, abstractInst-{tyTypeDesc}).kind notin {tyVar, tyLent}: - incl(n.sym.flags, sfAddrTaken) + incl(n.sym.flagsImpl, sfAddrTaken) result = newHiddenAddrTaken(c, n, isOutParam) of nkDotExpr: checkSonsLen(n, 2, c.config) @@ -693,12 +693,12 @@ proc analyseIfAddressTaken(c: PContext, n: PNode, isOutParam: bool): PNode = internalError(c.config, n.info, "analyseIfAddressTaken") return if skipTypes(n[1].sym.typ, abstractInst-{tyTypeDesc}).kind notin {tyVar, tyLent}: - incl(n[1].sym.flags, sfAddrTaken) + incl(n[1].sym.flagsImpl, sfAddrTaken) result = newHiddenAddrTaken(c, n, isOutParam) of nkBracketExpr: checkMinSonsLen(n, 1, c.config) if skipTypes(n[0].typ, abstractInst-{tyTypeDesc}).kind notin {tyVar, tyLent}: - if n[0].kind == nkSym: incl(n[0].sym.flags, sfAddrTaken) + if n[0].kind == nkSym: incl(n[0].sym.flagsImpl, sfAddrTaken) result = newHiddenAddrTaken(c, n, isOutParam) else: result = newHiddenAddrTaken(c, n, isOutParam) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index c1b49a19e9e90..4ec7beebebec4 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1920,7 +1920,7 @@ proc makeTupleAssignments(c: PContext; n: PNode): PNode = let temp = newSym(skTemp, getIdent(c.cache, "tmpTupleAsgn"), c.idgen, getCurrOwner(c), n.info) temp.typ = value.typ - temp.flags.incl(sfGenSym) + temp.flagsImpl.incl(sfGenSym) var v = newNodeI(nkLetSection, value.info) let tempNode = newSymNode(temp) #newIdentNode(getIdent(genPrefix & $temp.id), value.info) var vpart = newNodeI(nkIdentDefs, v.info, 3) @@ -1937,7 +1937,7 @@ proc makeTupleAssignments(c: PContext; n: PNode): PNode = # generate `let _ = temp[i]` which should generate a destructor let utemp = newSym(skLet, lhs[i].ident, c.idgen, getCurrOwner(c), lhs[i].info) utemp.typ = value.typ[i] - temp.flags.incl(sfGenSym) + temp.flagsImpl.incl(sfGenSym) var uv = newNodeI(nkLetSection, lhs[i].info) let utempNode = newSymNode(utemp) var uvpart = newNodeI(nkIdentDefs, v.info, 3) @@ -2376,7 +2376,7 @@ proc semQuoteAst(c: PContext, n: PNode): PNode = processQuotations(c, quotedBlock, op, quotes, ids) let dummyTemplateSym = newAnonSym(c, skTemplate, n.info) - incl(dummyTemplateSym.flags, sfTemplateRedefinition) + incl(dummyTemplateSym.flagsImpl, sfTemplateRedefinition) var dummyTemplate = newProcNode( nkTemplateDef, quotedBlock.info, body = quotedBlock, params = c.graph.emptyNode, @@ -2505,8 +2505,9 @@ proc instantiateCreateFlowVarCall(c: PContext; t: PType; # since it's an instantiation, we unmark it as a compilerproc. Otherwise # codegen would fail: if sfCompilerProc in result.flags: - result.flags.excl {sfCompilerProc, sfExportc, sfImportc} - result.loc.snippet = "" + ensureMutable result + result.flagsImpl.excl {sfCompilerProc, sfExportc, sfImportc} + result.locImpl.snippet = "" proc setMs(n: PNode, s: PSym): PNode = result = n @@ -3216,7 +3217,7 @@ proc enumFieldSymChoice(c: PContext, n: PNode, s: PSym; flags: TExprFlags): PNod a = initOverloadIter(o, c, n) while a != nil: if a.kind == skEnumField: - incl(a.flags, sfUsed) + incl(a.flagsImpl, sfUsed) markOwnerModuleAsUsed(c, a) result.add newSymNode(a, info) onUse(info, a) diff --git a/compiler/semgnrc.nim b/compiler/semgnrc.nim index 9268498040ccf..92deca323151a 100644 --- a/compiler/semgnrc.nim +++ b/compiler/semgnrc.nim @@ -50,13 +50,13 @@ proc semGenericStmtScope(c: PContext, n: PNode, result = semGenericStmt(c, n, flags, ctx) closeScope(c) -template isMixedIn(sym): bool = +template isMixedIn(sym): bool {.dirty.} = let s = sym s.name.id in ctx.toMixin or (withinConcept in flags and s.magic == mNone and s.kind in OverloadableSyms) -template canOpenSym(s): bool = +template canOpenSym(s): bool {.dirty.} = {withinMixin, withinConcept} * flags == {withinMixin} and s.id notin ctx.toBind proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym, @@ -65,7 +65,7 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym, fromDotExpr=false): PNode = result = nil semIdeForTemplateOrGenericCheck(c.config, n, ctx.cursorInBody) - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) template maybeDotChoice(c: PContext, n: PNode, s: PSym, fromDotExpr: bool) = if fromDotExpr: result = symChoice(c, n, s, scForceOpen) @@ -274,7 +274,7 @@ proc semGenericStmt(c: PContext, n: PNode, result = lookup(c, n, flags, ctx) if result != nil and result.kind == nkSym: assert result.sym != nil - incl result.sym.flags, sfUsed + incl result.sym.flagsImpl, sfUsed markOwnerModuleAsUsed(c, result.sym) of nkDotExpr: #let luf = if withinMixin notin flags: {checkUndeclared} else: {} @@ -318,7 +318,7 @@ proc semGenericStmt(c: PContext, n: PNode, var first = int ord(withinConcept in flags) var mixinContext = false if s != nil: - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) mixinContext = s.magic in {mDefined, mDeclared, mDeclaredInScope, mCompiles, mAstToStr} let whichChoice = if s.id in ctx.toBind: scClosed elif s.isMixedIn: scForceOpen diff --git a/compiler/seminst.nim b/compiler/seminst.nim index 7db6469d929b9..628d010bcd0a8 100644 --- a/compiler/seminst.nim +++ b/compiler/seminst.nim @@ -24,7 +24,7 @@ proc addObjFieldsToLocalScope(c: PContext; n: PNode) = let f = n.sym if f.kind == skField and fieldVisible(c, f): c.currentScope.symbols.strTableIncl(f, onConflictKeepOld=true) - incl(f.flags, sfUsed) + incl(f.flagsImpl, sfUsed) # it is not an error to shadow fields via parameters else: discard @@ -42,7 +42,7 @@ iterator instantiateGenericParamList(c: PContext, n: PNode, pt: LayeredIdTable): if q.typ.kind in {tyTypeDesc, tyGenericParam, tyStatic, tyConcept}+tyTypeClasses: let symKind = if q.typ.kind == tyStatic: skConst else: skType var s = newSym(symKind, q.name, c.idgen, getCurrOwner(c), q.info) - s.flags.incl {sfUsed, sfFromGeneric} + s.flagsImpl.incl {sfUsed, sfFromGeneric} var t = lookup(pt, q.typ) if t == nil: if tfRetType in q.typ.flags: @@ -149,7 +149,7 @@ proc instantiateBody(c: PContext, n, params: PNode, result, orig: PSym) = nil b = semProcBody(c, b, resultType) result.ast[bodyPos] = hloBody(c, b) - excl(result.flags, sfForward) + excl(result, sfForward) trackProc(c, result, result.ast[bodyPos]) dec c.inGenericInst @@ -208,7 +208,7 @@ proc instGenericContainer(c: PContext, info: TLineInfo, header: PType, # this scope was not created by the user, # unused params shouldn't be reported. - param.flags.incl sfUsed + param.flagsImpl.incl sfUsed addDecl(c, param) result = replaceTypeVarsT(cl, header) @@ -337,7 +337,7 @@ proc instantiateOnlyProcType(c: PContext, pt: LayeredIdTable, prc: PSym, info: T # examples are in texplicitgenerics # might be buggy, see rest of generateInstance if problems occur let fakeSym = copySym(prc, c.idgen) - incl(fakeSym.flags, sfFromGeneric) + incl(fakeSym.flagsImpl, sfFromGeneric) fakeSym.instantiatedFrom = prc openScope(c) for s in instantiateGenericParamList(c, prc.ast[genericParamsPos], pt): @@ -393,7 +393,7 @@ proc generateInstance(c: PContext, fn: PSym, pt: LayeredIdTable, let oldScope = c.currentScope while not isTopLevel(c): c.currentScope = c.currentScope.parent result = copySym(fn, c.idgen) - incl(result.flags, sfFromGeneric) + incl(result, sfFromGeneric) result.instantiatedFrom = fn if sfGlobal in result.flags and c.config.symbolFiles != disabledSf: let passc = getLocalPassC(c, producer) @@ -438,7 +438,7 @@ proc generateInstance(c: PContext, fn: PSym, pt: LayeredIdTable, inc i #echo "INSTAN ", fn.name.s, " ", typeToString(result.typ), " ", entry.concreteTypes.len if tfTriggersCompileTime in result.typ.flags: - incl(result.flags, sfCompileTime) + incl(result, sfCompileTime) n[genericParamsPos] = c.graph.emptyNode var oldPrc = genericCacheGet(c.graph, fn, entry[], c.compilesContextId) if oldPrc == nil: diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim index 0ad6117813a68..7ec913a874d90 100644 --- a/compiler/semmagic.nim +++ b/compiler/semmagic.nim @@ -34,7 +34,7 @@ proc semAddr(c: PContext; n: PNode): PNode = result = newNodeI(nkAddr, n.info) let x = semExprWithType(c, n) if x.kind == nkSym: - x.sym.flags.incl(sfAddrTaken) + x.sym.flagsImpl.incl(sfAddrTaken) if isAssignable(c, x) notin {arLValue, arLocalLValue, arAddressableConst, arLentValue}: localError(c.config, n.info, errExprHasNoAddress) result.add x @@ -471,7 +471,7 @@ proc turnFinalizerIntoDestructor(c: PContext; orig: PSym; info: TLineInfo): PSym result = copySym(orig, c.idgen) result.info = info - result.flags.incl sfFromGeneric + result.incl sfFromGeneric setOwner(result, orig) let origParamType = orig.typ.firstParamType let newParamType = makeVarType(result, origParamType.skipTypes(abstractPtrs), c.idgen) @@ -551,7 +551,7 @@ proc semNewFinalize(c: PContext; n: PNode): PNode = let wrapperSym = newSym(skProc, getIdent(c.graph.cache, fin.name.s & "FinalizerWrapper"), c.idgen, fin.owner, fin.info) let selfSymNode = newSymNode(copySym(fin.ast[paramsPos][1][0].sym, c.idgen)) selfSymNode.typ() = fin.typ.firstParamType - wrapperSym.flags.incl sfUsed + wrapperSym.flagsImpl.incl sfUsed let wrapper = c.semExpr(c, newProcNode(nkProcDef, fin.info, body = newTree(nkCall, newSymNode(fin), selfSymNode), params = nkFormalParams.newTree(c.graph.emptyNode, diff --git a/compiler/semparallel.nim b/compiler/semparallel.nim index b0071979bcfcd..78d59dfb29a4f 100644 --- a/compiler/semparallel.nim +++ b/compiler/semparallel.nim @@ -491,7 +491,7 @@ proc liftParallel*(g: ModuleGraph; idgen: IdGenerator; owner: PSym; n: PNode): P var varSection = newNodeI(nkVarSection, n.info) var temp = newSym(skTemp, getIdent(g.cache, "barrier"), idgen, owner, n.info) temp.typ = magicsys.getCompilerProc(g, "Barrier").typ - incl(temp.flags, sfFromGeneric) + incl(temp.flagsImpl, sfFromGeneric) let tempNode = newSymNode(temp) varSection.addVar tempNode diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index aa3e865ffdae0..1b4b15ac3e013 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -141,7 +141,7 @@ proc createTypeBoundOps(tracked: PEffects, typ: PType; info: TLineInfo; explicit if tracked.config.selectedGC == gcRefc or optSeqDestructors in tracked.config.globalOptions or tfHasAsgn in typ.flags: - tracked.owner.flags.incl sfInjectDestructors + tracked.owner.incl sfInjectDestructors proc isLocalSym(a: PEffects, s: PSym): bool = s.typ != nil and (s.kind in {skLet, skVar, skResult} or (s.kind == skParam and isOutParam(s.typ))) and @@ -206,7 +206,7 @@ proc guardDotAccess(a: PEffects; n: PNode) = proc makeVolatile(a: PEffects; s: PSym) {.inline.} = if a.inTryStmt > 0 and a.config.exc == excSetjmp: - incl(s.flags, sfVolatile) + incl(s, sfVolatile) proc varDecl(a: PEffects; n: PNode) {.inline.} = if n.kind == nkSym: @@ -373,7 +373,7 @@ proc useVarNoInitCheck(a: PEffects; n: PNode; s: PSym) = proc useVar(a: PEffects, n: PNode) = let s = n.sym if a.inExceptOrFinallyStmt > 0: - incl s.flags, sfUsedInFinallyOrExcept + incl s, sfUsedInFinallyOrExcept if isLocalSym(a, s): if sfNoInit in s.flags: # If the variable is explicitly marked as .noinit. do not emit any error @@ -1243,7 +1243,7 @@ proc track(tracked: PEffects, n: PNode) = of nkSym: useVar(tracked, n) if n.sym.typ != nil and tfHasAsgn in n.sym.typ.flags: - tracked.owner.flags.incl sfInjectDestructors + tracked.owner.incl sfInjectDestructors # bug #15038: ensure consistency if n.typ == nil or (not hasDestructor(n.typ) and sameType(n.typ, n.sym.typ)): n.typ() = n.sym.typ of nkHiddenAddr, nkAddr: @@ -1682,7 +1682,7 @@ proc trackProc*(c: PContext; s: PSym, body: PNode) = t.scopes[res.id] = t.currentBlock if sfNoInit in s.flags: # marks result "noinit" - incl res.flags, sfNoInit + incl res, sfNoInit track(t, body) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index ae4c744ead7b2..9e5c54320e659 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -79,7 +79,7 @@ proc semBreakOrContinue(c: PContext, n: PNode): PNode = if s.kind == skLabel and s.owner.id == c.p.owner.id: var x = newSymNode(s) x.info = n.info - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) n[0] = x suggestSym(c.graph, x.info, s, c.graph.usageSym) onUse(x.info, s) @@ -483,13 +483,13 @@ proc identWithin(n: PNode, s: PIdent): bool = proc semIdentDef(c: PContext, n: PNode, kind: TSymKind, reportToNimsuggest = true): PSym = if isTopLevel(c): result = semIdentWithPragma(c, kind, n, {sfExported}, fromTopLevel = true) - incl(result.flags, sfGlobal) + incl(result, sfGlobal) #if kind in {skVar, skLet}: # echo "global variable here ", n.info, " ", result.name.s else: result = semIdentWithPragma(c, kind, n, {}) if result.owner.kind == skModule: - incl(result.flags, sfGlobal) + incl(result, sfGlobal) result.options = c.config.options if reportToNimsuggest: @@ -520,7 +520,7 @@ proc addToVarSection(c: PContext; result: var PNode; orig, identDefs: PNode) = proc isDiscardUnderscore(v: PSym): bool = if v.name.id == ord(wUnderscore): - v.flags.incl(sfGenSym) + v.incl(sfGenSym) result = true else: result = false @@ -779,7 +779,7 @@ proc makeVarTupleSection(c: PContext, n, a, def: PNode, typ: PType, symkind: TSy # use same symkind for compatibility with original section let temp = newSym(symkind, getIdent(c.cache, "tmpTuple"), c.idgen, getCurrOwner(c), n.info) temp.typ = typ - temp.flags.incl(sfGenSym) + temp.flagsImpl.incl(sfGenSym) lastDef = newNodeI(defkind, a.info) newSons(lastDef, 3) lastDef[0] = newSymNode(temp) @@ -937,11 +937,11 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode = else: if v.owner == nil: setOwner(v, c.p.owner) when oKeepVariableNames: - if c.inUnrolledContext > 0: v.flags.incl(sfShadowed) + if c.inUnrolledContext > 0: v.incl(sfShadowed) else: let shadowed = findShadowedVar(c, v) if shadowed != nil: - shadowed.flags.incl(sfShadowed) + shadowed.incl(sfShadowed) if shadowed.kind == skResult and sfGenSym notin v.flags: message(c.config, a.info, warnResultShadowed) if def.kind != nkEmpty: @@ -1113,7 +1113,7 @@ proc semForVars(c: PContext, n: PNode; flags: TExprFlags): PNode = for i in 0.. resultPos and n[resultPos] != nil: @@ -2158,8 +2158,8 @@ proc bindDupHook(c: PContext; s: PSym; n: PNode; op: TTypeAttachedOp) = localError(c.config, n.info, errGenerated, "signature for '=dup' must be proc[T: object](x: T): T") - incl(s.flags, sfUsed) - incl(s.flags, sfOverridden) + incl(s.flagsImpl, sfUsed) + incl(s, sfOverridden) proc bindTypeHook(c: PContext; s: PSym; n: PNode; op: TTypeAttachedOp) = let t = s.typ @@ -2216,8 +2216,8 @@ proc bindTypeHook(c: PContext; s: PSym; n: PNode; op: TTypeAttachedOp) = else: localError(c.config, n.info, errGenerated, "signature for '" & s.name.s & "' must be proc[T: object](x: var T)") - incl(s.flags, sfUsed) - incl(s.flags, sfOverridden) + incl(s.flagsImpl, sfUsed) + incl(s, sfOverridden) proc semOverride(c: PContext, s: PSym, n: PNode) = let name = s.name.s.normalize @@ -2257,12 +2257,12 @@ proc semOverride(c: PContext, s: PSym, n: PNode) = else: localError(c.config, n.info, errGenerated, "signature for 'deepCopy' must be proc[T: ptr|ref](x: T): T") - incl(s.flags, sfUsed) - incl(s.flags, sfOverridden) + incl(s.flagsImpl, sfUsed) + incl(s, sfOverridden) of "=", "=copy", "=sink": if s.magic == mAsgn: return - incl(s.flags, sfUsed) - incl(s.flags, sfOverridden) + incl(s.flagsImpl, sfUsed) + incl(s, sfOverridden) if name == "=": message(c.config, n.info, warnDeprecated, "Overriding `=` hook is deprecated; Override `=copy` hook instead") let t = s.typ @@ -2428,8 +2428,8 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind, case n[namePos].kind of nkEmpty: s = newSym(kind, c.cache.idAnon, c.idgen, c.getCurrOwner, n.info) - s.flags.incl sfUsed - s.flags.incl sfGenSym + s.flagsImpl.incl sfUsed + s.incl sfGenSym n[namePos] = newSymNode(s) of nkSym: s = n[namePos].sym @@ -2455,7 +2455,7 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind, #s.scope = c.currentScope if s.kind in {skMacro, skTemplate}: # push noalias flag at first to prevent unwanted recursive calls: - incl(s.flags, sfNoalias) + incl(s, sfNoalias) # before compiling the proc params & body, set as current the scope # where the proc was declared @@ -2493,13 +2493,13 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind, n[genericParamsPos] = n[miscPos][1] n[miscPos] = c.graph.emptyNode - if tfTriggersCompileTime in s.typ.flags: incl(s.flags, sfCompileTime) + if tfTriggersCompileTime in s.typ.flags: incl(s, sfCompileTime) if n[patternPos].kind != nkEmpty: n[patternPos] = semPattern(c, n[patternPos], s) if s.kind == skIterator: s.typ.flags.incl(tfIterator) elif s.kind == skFunc: - incl(s.flags, sfNoSideEffect) + incl(s, sfNoSideEffect) incl(s.typ.flags, tfNoSideEffect) var (proto, comesFromShadowScope) = @@ -2573,8 +2573,8 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind, if sfForward notin proto.flags and proto.magic == mNone: wrongRedefinition(c, n.info, proto.name.s, proto.info) if not comesFromShadowScope: - excl(proto.flags, sfForward) - incl(proto.flags, sfWasForwarded) + excl(proto, sfForward) + incl(proto, sfWasForwarded) suggestSym(c.graph, s.info, proto, c.graph.usageSym) closeScope(c) # close scope with wrong parameter symbols openScope(c) # open scope for old (correct) parameter symbols @@ -2676,8 +2676,8 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind, if s.kind in {skProc, skFunc} and s.typ.returnType != nil and s.typ.returnType.kind == tyAnything: localError(c.config, n[paramsPos][0].info, "return type 'auto' cannot be used in forward declarations") - incl(s.flags, sfForward) - incl(s.flags, sfWasForwarded) + incl(s, sfForward) + incl(s, sfWasForwarded) elif sfBorrow in s.flags: semBorrow(c, n, s) sideEffectsCheck(c, s) @@ -2794,14 +2794,14 @@ proc semMacroDef(c: PContext, n: PNode): PNode = if param.typ.kind != tyUntyped: allUntyped = false # no default value, parameters required in call if param.ast == nil: nullary = false - if allUntyped: incl(s.flags, sfAllUntyped) + if allUntyped: incl(s, sfAllUntyped) if nullary and n[genericParamsPos].kind == nkEmpty: # macro can be called with alias syntax, remove pushed noalias flag - excl(s.flags, sfNoalias) + excl(s, sfNoalias) if n[bodyPos].kind == nkEmpty: localError(c.config, n.info, errImplOfXexpected % s.name.s) -proc incMod(c: PContext, n: PNode, it: PNode, includeStmtResult: PNode) = +proc incMod(c: PContext, n: PNode, it: PNode, includeStmtResult, resolvedIncStmt: PNode) = var f = checkModuleName(c.config, it) if f != InvalidFileIdx: addIncludeFileDep(c, f) @@ -2809,12 +2809,22 @@ proc incMod(c: PContext, n: PNode, it: PNode, includeStmtResult: PNode) = if containsOrIncl(c.includedFiles, f.int): localError(c.config, n.info, errRecursiveDependencyX % toMsgFilename(c.config, f)) else: + if resolvedIncStmt != nil: + resolvedIncStmt.add newStrNode(toFullPath(c.config, f), it.info) includeStmtResult.add semStmt(c, c.graph.includeFileCallback(c.graph, c.module, f), {}) excl(c.includedFiles, f.int) proc evalInclude(c: PContext, n: PNode): PNode = result = newNodeI(nkStmtList, n.info) - result.add n + var resolvedIncStmt: PNode = nil + if optCompress in c.config.globalOptions: + # New resolve the include filenames to string literals that contain absolute paths, + # nicer for IC: + resolvedIncStmt = newNodeI(nkIncludeStmt, n.info) + result.add resolvedIncStmt + else: + # Legacy: Keep `include` statement as is: + result.add n template checkAs(it: PNode) = if it.kind == nkInfix and it.len == 3: let op = it[0].getPIdent @@ -2832,9 +2842,9 @@ proc evalInclude(c: PContext, n: PNode): PNode = for x in it[lastPos]: checkAs(x) imp[lastPos] = x - incMod(c, n, imp, result) + incMod(c, n, imp, result, resolvedIncStmt) else: - incMod(c, n, it, result) + incMod(c, n, it, result, resolvedIncStmt) proc recursiveSetFlag(n: PNode, flag: TNodeFlag) = if n != nil: diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim index c424b801f558b..33761da700114 100644 --- a/compiler/semtempl.nim +++ b/compiler/semtempl.nim @@ -68,7 +68,7 @@ proc symChoice(c: PContext, n: PNode, s: PSym, r: TSymChoiceRule; if not isField or sfGenSym notin s.flags: result = newSymNode(s, info) # possibly not final field sym - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) markOwnerModuleAsUsed(c, s) onUse(info, s) else: @@ -85,7 +85,7 @@ proc symChoice(c: PContext, n: PNode, s: PSym, r: TSymChoiceRule; a = initOverloadIter(o, c, n) while a != nil: if a.kind != skModule and (not isField or sfGenSym notin a.flags): - incl(a.flags, sfUsed) + incl(a.flagsImpl, sfUsed) markOwnerModuleAsUsed(c, a) result.add newSymNode(a, info) onUse(info, a) @@ -180,8 +180,7 @@ proc semTemplBodyScope(c: var TemplCtx, n: PNode): PNode = proc newGenSym(kind: TSymKind, n: PNode, c: var TemplCtx): PSym = result = newSym(kind, considerQuotedIdent(c.c, n), c.c.idgen, c.owner, n.info) - incl(result.flags, sfGenSym) - incl(result.flags, sfShadowed) + incl(result.flagsImpl, {sfGenSym, sfShadowed}) proc addLocalDecl(c: var TemplCtx, n: var PNode, k: TSymKind) = # locals default to 'gensym', fields default to 'inject': @@ -218,10 +217,10 @@ proc addLocalDecl(c: var TemplCtx, n: var PNode, k: TSymKind) = onDef(n.info, local) replaceIdentBySym(c.c, n, newSymNode(local, n.info)) if k == skParam and c.inTemplateHeader > 0: - local.flags.incl sfTemplateParam + local.incl sfTemplateParam proc semTemplSymbol(c: var TemplCtx, n: PNode, s: PSym; isField, isAmbiguous: bool): PNode = - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) # bug #12885; ideally sem'checking is performed again afterwards marking # the symbol as used properly, but the nfSem mechanism currently prevents # that from happening, so we mark the module as used here already: @@ -298,7 +297,7 @@ proc semRoutineInTemplName(c: var TemplCtx, n: PNode, explicitInject: bool): PNo if s != nil: if s.owner == c.owner and (s.kind == skParam or (sfGenSym in s.flags and not explicitInject)): - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) result = newSymNode(s, n.info) onUse(n.info, s) else: @@ -384,7 +383,7 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode = let s = qualifiedLookUp(c.c, n, {}) if s != nil: if s.owner == c.owner and s.kind == skParam and sfTemplateParam in s.flags: - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) result = newSymNode(s, n.info) onUse(n.info, s) elif contains(c.toBind, s.id): @@ -394,7 +393,7 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode = elif s.owner == c.owner and sfGenSym in s.flags and c.noGenSym == 0: # template tmp[T](x: var seq[T]) = # var yz: T - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) result = newSymNode(s, n.info) onUse(n.info, s) else: @@ -608,7 +607,7 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode = # do not symchoice a quoted template parameter (bug #2390): if s.owner == c.owner and s.kind == skParam and n.kind == nkAccQuoted and n.len == 1: - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) onUse(n.info, s) return newSymNode(s, n.info) elif contains(c.toBind, s.id): @@ -688,7 +687,7 @@ proc semTemplateDef(c: PContext, n: PNode): PNode = var s: PSym if isTopLevel(c): s = semIdentVis(c, skTemplate, n[namePos], {sfExported}) - incl(s.flags, sfGlobal) + incl(s, sfGlobal) else: s = semIdentVis(c, skTemplate, n[namePos], {}) assert s.kind == skTemplate @@ -701,7 +700,7 @@ proc semTemplateDef(c: PContext, n: PNode): PNode = # check parameter list: #s.scope = c.currentScope # push noalias flag at first to prevent unwanted recursive calls: - incl(s.flags, sfNoalias) + incl(s, sfNoalias) pushOwner(c, s) openScope(c) n[namePos] = newSymNode(s) @@ -724,8 +723,8 @@ proc semTemplateDef(c: PContext, n: PNode): PNode = for i in 1.. depthf: a.skipGenericAlias else: a let rootf = if skipBoth or depthf > deptha: f.skipGenericAlias else: f - + if f.isConcept: result = enterConceptMatch(c, rootf, roota, flags) elif a.kind == tyGenericInst: @@ -2316,7 +2316,7 @@ proc userConvMatch(c: PContext, m: var TCandidate, f, a: PType, let fdest = typeRel(m, f, dest) if fdest in {isEqual, isGeneric} and not (dest.kind == tyLent and f.kind in {tyVar}): # can't fully mark used yet, may not be used in final call - incl(c.converters[i].flags, sfUsed) + incl(c.converters[i].flagsImpl, sfUsed) markOwnerModuleAsUsed(c, c.converters[i]) var s = newSymNode(c.converters[i]) s.typ() = c.converters[i].typ diff --git a/compiler/sinkparameter_inference.nim b/compiler/sinkparameter_inference.nim index 09d54ec7906c8..1e025d1ac97a9 100644 --- a/compiler/sinkparameter_inference.nim +++ b/compiler/sinkparameter_inference.nim @@ -45,7 +45,8 @@ proc checkForSink*(config: ConfigRef; idgen: IdGenerator; owner: PSym; arg: PNod #echo config $ arg.info, " turned into a sink parameter ", arg.sym.name.s elif sfWasForwarded notin arg.sym.flags: # we only report every potential 'sink' parameter only once: - incl arg.sym.flags, sfWasForwarded + ensureMutable arg.sym + incl arg.sym.flagsImpl, sfWasForwarded message(config, arg.info, hintPerformance, "could not turn '$1' to a sink parameter" % [arg.sym.name.s]) #echo config $ arg.info, " candidate for a sink parameter here" diff --git a/compiler/spawn.nim b/compiler/spawn.nim index 99b3b55332634..7702e26221a20 100644 --- a/compiler/spawn.nim +++ b/compiler/spawn.nim @@ -58,7 +58,7 @@ proc addLocalVar(g: ModuleGraph; varSection, varInit: PNode; idgen: IdGenerator; result = newSym(skTemp, getIdent(g.cache, genPrefix), idgen, owner, varSection.info, owner.options) result.typ = typ - incl(result.flags, sfFromGeneric) + incl(result.flagsImpl, sfFromGeneric) var vpart = newNodeI(nkIdentDefs, varSection.info, 3) vpart[0] = newSymNode(result) @@ -358,7 +358,7 @@ proc wrapProcForSpawn*(g: ModuleGraph; idgen: IdGenerator; owner: PSym; spawnExp threadParam = newSym(skParam, getIdent(g.cache, "thread"), idgen, wrapperProc, n.info, g.config.options) argsParam = newSym(skParam, getIdent(g.cache, "args"), idgen, wrapperProc, n.info, g.config.options) - wrapperProc.flags.incl sfInjectDestructors + wrapperProc.incl sfInjectDestructors block: let ptrType = getSysType(g, n.info, tyPointer) threadParam.typ = ptrType @@ -372,7 +372,7 @@ proc wrapProcForSpawn*(g: ModuleGraph; idgen: IdGenerator; owner: PSym; spawnExp var scratchObj = newSym(skVar, getIdent(g.cache, "scratch"), idgen, owner, n.info, g.config.options) block: scratchObj.typ = objType - incl(scratchObj.flags, sfFromGeneric) + incl(scratchObj.flagsImpl, sfFromGeneric) var varSectionB = newNodeI(nkVarSection, n.info) varSectionB.addVar(scratchObj.newSymNode) result.add varSectionB diff --git a/compiler/suggest.nim b/compiler/suggest.nim index 3953936eb6dd0..5c3265dba232e 100644 --- a/compiler/suggest.nim +++ b/compiler/suggest.nim @@ -43,7 +43,7 @@ when defined(nimsuggest): const sep = '\t' -type +type ImportContext = object isMultiImport: bool # True if we're in a [...] context baseDir: string # e.g., "folder/" in "import folder/[..." @@ -590,7 +590,7 @@ when defined(nimsuggest): let infoAsInt = info.infoToInt for infoB in s.allUsages: if infoB.infoToInt == infoAsInt: return - s.allUsages.add(info) + s.allUsagesImpl.add(info) proc findUsages(g: ModuleGraph; info: TLineInfo; s: PSym; usageSym: var PSym) = if g.config.suggestVersion == 1: @@ -707,9 +707,9 @@ proc markOwnerModuleAsUsed(c: PContext; s: PSym) = proc markUsed(c: PContext; info: TLineInfo; s: PSym; checkStyle = true; isGenericInstance = false) = if not isGenericInstance: let conf = c.config - incl(s.flags, sfUsed) + incl(s.flagsImpl, sfUsed) if s.kind == skEnumField and s.owner != nil: - incl(s.owner.flags, sfUsed) + incl(s.owner.flagsImpl, sfUsed) if sfDeprecated in s.owner.flags: warnAboutDeprecated(conf, info, s) if {sfDeprecated, sfError} * s.flags != {}: @@ -788,7 +788,7 @@ proc extractImportContextFromAst(n: PNode, cursorCol: int): ImportContext = proc findModuleFile(c: PContext, partialPath: string): seq[string] = result = @[] let currentModuleDir = parentDir(toFullPath(c.config, FileIndex(c.module.position))) - + proc tryAddModule(path, baseName: string) = if fileExists(path & ".nim"): result.add(baseName) @@ -800,7 +800,7 @@ proc findModuleFile(c: PContext, partialPath: string): seq[string] = let (_, name, ext) = splitFile(path) if kind == pcFile: if ext == ".nim" and name.startsWith(file): - result.add(name) + result.add(name) proc collectImportModulesFromDir(dir: string, result: var seq[string]) = for kind, path in walkDir(dir): @@ -809,10 +809,10 @@ proc findModuleFile(c: PContext, partialPath: string): seq[string] = if kind == pcFile: if ext == ".nim" and name.startsWith(partialPath): result.add(name) - else: + else: if name.startsWith(partialPath): result.add(name) - + if '/' in partialPath: let parts = partialPath.split('/') let dir = parts[0] @@ -839,13 +839,13 @@ proc suggestModuleNames(c: PContext, n: PNode) = column: n.info.col.int, doc: "", quality: 100, - contextFits: true, + contextFits: true, prefix: if partialPath.len > 0: prefixMatch(path, partialPath) else: PrefixMatch.None, symkind: byte skModule ) suggestions.add(suggest) - + let importCtx = extractImportContextFromAst(n, c.config.m.trackPos.col) var searchPath = "" if importCtx.baseDir.len > 0: @@ -901,7 +901,7 @@ proc suggestExprNoCheck*(c: PContext, n: PNode) = if outputs.len > 0 and c.config.ideCmd in {ideSug, ideCon, ideDef}: produceOutput(outputs, c.config) suggestQuit() - + proc suggestExpr*(c: PContext, n: PNode) = if exactEquals(c.config.m.trackPos, n.info): suggestExprNoCheck(c, n) diff --git a/compiler/transf.nim b/compiler/transf.nim index 5d80bf9328b34..43605f505b7df 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -95,7 +95,7 @@ proc getCurrOwner(c: PTransf): PSym = proc newTemp(c: PTransf, typ: PType, info: TLineInfo): PNode = let r = newSym(skTemp, getIdent(c.graph.cache, genPrefix), c.idgen, getCurrOwner(c), info) r.typ = typ #skipTypes(typ, {tyGenericInst, tyAlias, tySink}) - incl(r.flags, sfFromGeneric) + incl(r.flagsImpl, sfFromGeneric) let owner = getCurrOwner(c) result = newSymNode(r) @@ -181,7 +181,7 @@ proc transformSym(c: PTransf, n: PNode): PNode = proc freshVar(c: PTransf; v: PSym): PNode = let owner = getCurrOwner(c) var newVar = copySym(v, c.idgen) - incl(newVar.flags, sfFromGeneric) + incl(newVar.flagsImpl, sfFromGeneric) setOwner(newVar, owner) result = newSymNode(newVar) @@ -795,7 +795,7 @@ proc transformFor(c: PTransf, n: PNode): PNode = addVar(v, copyTree(n[i][j])) # declare new vars else: if n[i].kind == nkSym and isSimpleIteratorVar(c, iter, call, n[i].sym.owner): - incl n[i].sym.flags, sfCursor + incl n[i].sym, sfCursor addVar(v, copyTree(n[i])) # declare new vars stmtList.add(v) diff --git a/compiler/varpartitions.nim b/compiler/varpartitions.nim index 1711fea46adb4..ddba3d4bcbca2 100644 --- a/compiler/varpartitions.nim +++ b/compiler/varpartitions.nim @@ -1014,6 +1014,6 @@ proc computeCursors*(s: PSym; n: PNode; g: ModuleGraph) = if par.s[rid].con.kind == isRootOf and dangerousMutation(par.graphs[par.s[rid].con.graphIndex], par.s[i]): discard "cannot cursor into a graph that is mutated" else: - v.sym.flags.incl sfCursor + v.sym.flagsImpl.incl sfCursor when false: echo "this is now a cursor ", v.sym, " ", par.s[rid].flags, " ", g.config $ v.sym.info diff --git a/compiler/vm.nim b/compiler/vm.nim index 258c2333453d8..33ba068650761 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -2214,7 +2214,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = if k < 0 or k > ord(high(TSymKind)): internalError(c.config, c.debug[pc], "request to create symbol of invalid kind") var sym = newSym(k.TSymKind, getIdent(c.cache, name), c.idgen, c.module.owner, c.debug[pc]) - incl(sym.flags, sfGenSym) + incl(sym.flagsImpl, sfGenSym) regs[ra].node = newSymNode(sym) regs[ra].node.flags.incl nfIsRef of opcNccValue: diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index aa848bca87c2a..450694d6cf90a 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -1790,7 +1790,7 @@ proc genRdVar(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags) = # see tests/t99bott for an example that triggers it: cannotEval(c, n) -template needsRegLoad(): untyped = +template needsRegLoad(): untyped {.dirty.} = {gfNode, gfNodeAddr} * flags == {} and fitsRegister(n.typ.skipTypes({tyVar, tyLent, tyStatic})) diff --git a/compiler/vmprofiler.nim b/compiler/vmprofiler.nim index 3f0db84bddb5c..38d9f1532fb99 100644 --- a/compiler/vmprofiler.nim +++ b/compiler/vmprofiler.nim @@ -1,5 +1,5 @@ -import options, vmdef, lineinfos, msgs +import ast, options, vmdef, lineinfos, msgs import std/[times, strutils, tables] diff --git a/nimsuggest/nimsuggest.nim b/nimsuggest/nimsuggest.nim index 6144352f050f8..0e303cafaee72 100644 --- a/nimsuggest/nimsuggest.nim +++ b/nimsuggest/nimsuggest.nim @@ -1151,9 +1151,9 @@ proc executeNoHooksV3(cmd: IdeCmd, file: AbsoluteFile, dirtyfile: AbsoluteFile, # ideSug/ideCon performs partial build of the file, thus mark it dirty for the # future calls. graph.markDirtyIfNeeded(file.string, fileIndex) - graph.recompilePartially(fileIndex) + graph.recompilePartially(fileIndex) let m = graph.getModule fileIndex - incl m.flags, sfDirty + incl m, sfDirty of ideOutline: let n = parseFile(fileIndex, graph.cache, graph.config) graph.iterateOutlineNodes(n, graph.fileSymbols(fileIndex).deduplicateSymInfoPair(false)) diff --git a/tools/enumgen.nim b/tools/enumgen.nim new file mode 100644 index 0000000000000..fdcd132f92dad --- /dev/null +++ b/tools/enumgen.nim @@ -0,0 +1,247 @@ +## Generate effective NIF representation for `Enum` + +import ".." / compiler / [ast, options] + +import std / [syncio, assertions, strutils, tables] + +# We need to duplicate this type here as ast.nim's version of it does not work +# as it sets the string values explicitly breaking our logic... +type + TCallingConventionMirror = enum + ccNimCall + ccStdCall + ccCDecl + ccSafeCall + ccSysCall + ccInline + ccNoInline + ccFastCall + ccThisCall + ccClosure + ccNoConvention + ccMember + +const + SpecialCases = [ + ("nkCommand", "cmd"), + ("nkIfStmt", "if"), + ("nkError", "err"), + ("nkType", "onlytype"), + ("nkTypeSection", "type"), + ("tySequence", "seq"), + ("tyVar", "mut"), + ("tyProc", "proctype"), + ("tyUncheckedArray", "uarray"), + ("nkExprEqExpr", "vv"), + ("nkExprColonExpr", "kv"), + ("nkDerefExpr", "deref"), + ("nkReturnStmt", "ret"), + ("nkBreakStmt", "brk"), + ("nkStmtListExpr", "expr"), + ("nkEnumFieldDef", "efld"), + ("nkNilLit", "nil"), + ("ccNoConvention", "noconv"), + ("mExpr", "exprm"), + ("mStmt", "stmtm"), + ("mEqNimrodNode", "eqnimnode"), + ("mPNimrodNode", "nimnode"), + ("mNone", "nonem"), + ("mAsgn", "asgnm"), + ("mOf", "ofm"), + ("mAddr", "addrm"), + ("mType", "typem"), + ("mStatic", "staticm"), + ("mRange", "rangem"), + ("mVar", "varm"), + ("mInSet", "contains"), + ("mNil", "nilm"), + ("tyBuiltInTypeClass", "bconcept"), + ("tyUserTypeClass", "uconcept"), + ("tyUserTypeClassInst", "uconceptinst"), + ("tyCompositeTypeClass", "cconcept"), + ("tyGenericInvocation", "ginvoke"), + ("tyGenericBody", "gbody"), + ("tyGenericInst", "ginst"), + ("tyGenericParam", "gparam"), + ("nkStmtList", "stmts"), + ("nkDotExpr", "dot"), + ("nkBracketExpr", "at") + ] + SuffixesToReplace = [ + ("Section", ""), ("Branch", ""), ("Stmt", ""), ("I", ""), + ("Expr", "x"), ("Def", "") + ] + PrefixesToReplace = [ + ("Length", "len"), + ("SetLength", "setlen"), + ("Append", "add") + ] + AdditionalNodes = [ + "nf", # "node flag" + "tf", # "type flag" + "sf", # "sym flag" + "htype", # annotated with a hidden type + "missing" + ] + +proc genEnum[E](f: var File; enumName: string; known: var OrderedTable[string, bool]; prefixLen = 2) = + var mappingA = initOrderedTable[string, E]() + var cases = "" + for e in low(E)..high(E): + var es = $e + if es.startsWith("nkHidden"): + es = es.replace("nkHidden", "nkh") # prefix will be removed + else: + for (suffix, repl) in items SuffixesToReplace: + if es.len - prefixLen > suffix.len and es.endsWith(suffix): + es.setLen es.len - len(suffix) + es.add repl + break + for (suffix, repl) in items PrefixesToReplace: + if es.len - prefixLen > suffix.len and es.substr(prefixLen).startsWith(suffix): + es = es.substr(0, prefixLen-1) & repl & es.substr(prefixLen+suffix.len) + break + + let s = es.substr(prefixLen) + var done = false + for enu, key in items SpecialCases: + if $e == enu: + assert(not mappingA.hasKey(key)) + if known.hasKey(key): echo "conflict: ", key + known[key] = true + assert key.len > 0 + mappingA[key] = e + cases.add " of " & $e & ": " & escape(key) & "\n" + done = true + break + if not done: + let key = s.toLowerAscii + if not mappingA.hasKey(key): + assert key.len > 0, $e + if known.hasKey(key): echo "conflict: ", key + known[key] = true + mappingA[key] = e + cases.add " of " & $e & ": " & escape(key) & "\n" + done = true + if not done: + var d = 0 + while d < 10: + let key = s.toLowerAscii & $d + if not mappingA.hasKey(key): + assert key.len > 0 + mappingA[key] = e + cases.add " of " & $e & ": " & escape(key) & "\n" + done = true + break + inc d + if not done: + echo "Could not map: " & s + #echo mapping + var code = "" + code.add "proc toNifTag*(s: " & enumName & "): string =\n" + code.add " case s\n" + code.add cases + code.add "\n\n" + let procname = "parse" # & enumName.substr(1) + code.add "proc " & procname & "*(t: typedesc[" & enumName & "]; s: string): " & enumName & " =\n" + code.add " case s\n" + for (k, v) in pairs mappingA: + code.add " of " & escape(k) & ": " & $v & "\n" + code.add " else: " & $low(E) & "\n\n\n" + f.write code + +proc genEnum[E](f: var File; enumName: string; prefixLen = 2) = + var known = initOrderedTable[string, bool]() + genEnum[E](f, enumName, known, prefixLen) + + +proc genFlags[E](f: var File; enumName: string; prefixLen = 2) = + var mappingA = initOrderedTable[string, E]() + var mappingB = initOrderedTable[string, E]() + var cases = "" + for e in low(E)..high(E): + let s = ($e).substr(prefixLen) + var done = false + for c in s: + if c in {'A'..'Z'}: + let key = $c.toLowerAscii + if not mappingA.hasKey(key): + mappingA[key] = e + cases.add " of " & $e & ": dest.add " & escape(key) & "\n" + done = true + break + if not done: + var d = 0 + while d < 10: + let key = $s[0].toLowerAscii & $d + if not mappingB.hasKey(key): + mappingB[key] = e + cases.add " of " & $e & ": dest.add " & escape(key) & "\n" + done = true + break + inc d + if not done: + quit "Could not map: " & s + #echo mapping + var code = "" + code.add "proc genFlags*(s: set[" & enumName & "]; dest: var string) =\n" + code.add " for e in s:\n" + code.add " case e\n" + code.add cases + code.add "\n\n" + code.add "proc parse*(t: typedesc[" & enumName & "]; s: string): set[" & enumName & "] =\n" + code.add " result = {}\n" + code.add " var i = 0\n" + code.add " while i < s.len:\n" + code.add " case s[i]\n" + for c in 'a'..'z': + var letterFound = false + var digitsFound = 0 + for d in '0'..'9': + if mappingB.hasKey($c & $d): + if not letterFound: + letterFound = true + code.add " of '" & c & "':\n" + if digitsFound == 0: + code.add " if" + else: + code.add " elif" + inc digitsFound + code.add " i+1 < s.len and s[i+1] == '" & d & "':\n" + code.add " result.incl " & $mappingB[$c & $d] & "\n" + code.add " inc i\n" + + if mappingA.hasKey($c): + if digitsFound == 0: + code.add " of '" & c & "': " + else: + code.add " else: " + code.add "result.incl " & $mappingA[$c] & "\n" + + code.add " else: discard\n" + code.add " inc i\n\n" + f.write code + +var f = open("compiler/icnif/enum2nif.nim", fmWrite) +f.write "# Generated by tools/enumgen.nim. DO NOT EDIT!\n\n" +f.write "import \"..\" / [ast, options]\n\n" +# use the same mapping for TNodeKind and TMagic so that we can detect conflicts! +var nodeTags = initOrderedTable[string, bool]() +for a in AdditionalNodes: + nodeTags[a] = true + +genEnum[TNodeKind](f, "TNodeKind", nodeTags) +genEnum[TSymKind](f, "TSymKind") +genEnum[TTypeKind](f, "TTypeKind") +genEnum[TLocKind](f, "TLocKind", 3) +genEnum[TCallingConventionMirror](f, "TCallingConvention", 2) +genEnum[TMagic](f, "TMagic", nodeTags, 1) +genEnum[TStorageLoc](f, "TStorageLoc") +genEnum[TLibKind](f, "TLibKind") +genFlags[TSymFlag](f, "TSymFlag") +genFlags[TNodeFlag](f, "TNodeFlag") +genFlags[TTypeFlag](f, "TTypeFlag") +genFlags[TLocFlag](f, "TLocFlag") +genFlags[TOption](f, "TOption", 3) + +f.close()