# # # The Nim Compiler # (c) Copyright 2012 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # # Algorithms for the abstract syntax tree: hash tables, lists # and sets of nodes are supported. Efficiency is important as # the data structures here are used in various places of the compiler. import ast, astyaml, options, lineinfos, idents, rodutils, msgs import std/[hashes, intsets] import std/strutils except addf export astyaml.treeToYaml, astyaml.typeToYaml, astyaml.symToYaml, astyaml.lineInfoToStr when defined(nimPreviewSlimSystem): import std/assertions proc hashNode*(p: RootRef): Hash # these are for debugging only: They are not really deprecated, but I want # the warning so that release versions do not contain debugging statements: proc debug*(n: PSym; conf: ConfigRef = nil) {.exportc: "debugSym", deprecated.} proc debug*(n: PType; conf: ConfigRef = nil) {.exportc: "debugType", deprecated.} proc debug*(n: PNode; conf: ConfigRef = nil) {.exportc: "debugNode", deprecated.} template debug*(x: PSym|PType|PNode) {.deprecated.} = when compiles(c.config): debug(c.config, x) elif compiles(c.graph.config): debug(c.graph.config, x) else: error() template debug*(x: auto) {.deprecated.} = echo x template mdbg*: bool {.deprecated.} = when compiles(c.graph): c.module.fileIdx == c.graph.config.projectMainIdx elif compiles(c.module): c.module.fileIdx == c.config.projectMainIdx elif compiles(c.c.module): c.c.module.fileIdx == c.c.config.projectMainIdx elif compiles(m.c.module): m.c.module.fileIdx == m.c.config.projectMainIdx elif compiles(cl.c.module): cl.c.module.fileIdx == cl.c.config.projectMainIdx elif compiles(p): when compiles(p.lex): p.lex.fileIdx == p.lex.config.projectMainIdx else: p.module.module.fileIdx == p.config.projectMainIdx elif compiles(m.module.fileIdx): m.module.fileIdx == m.config.projectMainIdx elif compiles(L.fileIdx): L.fileIdx == L.config.projectMainIdx else: error() # --------------------------------------------------------------------------- proc lookupInRecord*(n: PNode, field: PIdent): PSym # ------------- table[int, int] --------------------------------------------- const InvalidKey* = low(int) type TIIPair*{.final.} = object key*, val*: int TIIPairSeq* = seq[TIIPair] TIITable*{.final.} = object # table[int, int] counter*: int data*: TIIPairSeq proc initIITable*(x: var TIITable) proc iiTableGet*(t: TIITable, key: int): int proc iiTablePut*(t: var TIITable, key, val: int) # implementation proc skipConvCastAndClosure*(n: PNode): PNode = result = n while true: case result.kind of nkObjUpConv, nkObjDownConv, nkChckRange, nkChckRangeF, nkChckRange64, nkClosure: result = result[0] of nkHiddenStdConv, nkHiddenSubConv, nkConv, nkCast: result = result[1] else: break proc sameValue*(a, b: PNode): bool = result = false case a.kind of nkCharLit..nkUInt64Lit: if b.kind in {nkCharLit..nkUInt64Lit}: result = getInt(a) == getInt(b) of nkFloatLit..nkFloat64Lit: if b.kind in {nkFloatLit..nkFloat64Lit}: result = a.floatVal == b.floatVal of nkStrLit..nkTripleStrLit: if b.kind in {nkStrLit..nkTripleStrLit}: result = a.strVal == b.strVal else: # don't raise an internal error for 'nim check': #InternalError(a.info, "SameValue") discard proc leValue*(a, b: PNode): bool = # a <= b? result = false case a.kind of nkCharLit..nkUInt64Lit: if b.kind in {nkCharLit..nkUInt64Lit}: result = getInt(a) <= getInt(b) of nkFloatLit..nkFloat64Lit: if b.kind in {nkFloatLit..nkFloat64Lit}: result = a.floatVal <= b.floatVal of nkStrLit..nkTripleStrLit: if b.kind in {nkStrLit..nkTripleStrLit}: result = a.strVal <= b.strVal else: # don't raise an internal error for 'nim check': #InternalError(a.info, "leValue") discard proc weakLeValue*(a, b: PNode): TImplication = if a.kind notin nkLiterals or b.kind notin nkLiterals: result = impUnknown else: result = if leValue(a, b): impYes else: impNo proc lookupInRecord(n: PNode, field: PIdent): PSym = result = nil case n.kind of nkRecList: for i in 0.. 0 and a[alen] != '`': dec(alen) if alen <= 0: alen = a.len var i = 1 var j = 1 while true: while i < alen and a[i] == '_': inc i while j < b.len and b[j] == '_': inc j var aa = if i < alen: toLowerAscii(a[i]) else: '\0' var bb = if j < b.len: toLowerAscii(b[j]) else: '\0' if aa != bb: return false # the characters are identical: if i >= alen: # both cursors at the end: if j >= b.len: return true # not yet at the end of 'b': return false elif j >= b.len: return false inc i inc j proc getNamedParamFromList*(list: PNode, ident: PIdent): PSym = ## Named parameters are special because a named parameter can be ## gensym'ed and then they have '\`' suffix that we need to ## ignore, see compiler / evaltempl.nim, snippet: ## ```nim ## result.add newIdentNode(getIdent(c.ic, x.name.s & "\`gensym" & $x.id), ## if c.instLines: actual.info else: templ.info) ## ``` result = nil for i in 1.." if this.useColor: this.res.add resetStyle return proc value(this: var DebugPrinter; value: PType) proc value(this: var DebugPrinter; value: PNode) proc value(this: var DebugPrinter; value: PSym) = earlyExit(this, value) this.openCurly this.key("kind") this.value(value.kind) this.key("name") this.value(value.name.s) this.key("id") this.value(value.id) if value.kind in {skField, skEnumField, skParam}: this.key("position") this.value(value.position) if card(value.flags) > 0: this.key("flags") this.value(value.flags) if this.renderSymType and value.typ != nil: this.key "typ" this.value(value.typ) this.closeCurly proc value(this: var DebugPrinter; value: PType) = earlyExit(this, value) this.openCurly this.key "kind" this.value value.kind this.key "id" this.value value.id if value.sym != nil: this.key "sym" this.value value.sym #this.value value.sym.name.s if card(value.flags) > 0: this.key "flags" this.value value.flags if value.kind in IntegralTypes and value.n != nil: this.key "n" this.value value.n this.key "sons" this.openBracket for i, a in value.ikids: if i > 0: this.comma this.value a this.closeBracket if value.n != nil: this.key "n" this.value value.n this.closeCurly proc value(this: var DebugPrinter; value: PNode) = earlyExit(this, value) this.openCurly this.key "kind" this.value value.kind if value.comment.len > 0: this.key "comment" this.value value.comment when defined(useNodeIds): this.key "id" this.value value.id if this.conf != nil: this.key "info" this.value $lineInfoToStr(this.conf, value.info) if value.flags != {}: this.key "flags" this.value value.flags if value.typ != nil: this.key "typ" this.value value.typ.kind else: this.key "typ" this.value "nil" case value.kind of nkCharLit..nkUInt64Lit: this.key "intVal" this.value value.intVal of nkFloatLit, nkFloat32Lit, nkFloat64Lit: this.key "floatVal" this.value value.floatVal.toStrMaxPrecision of nkStrLit..nkTripleStrLit: this.key "strVal" this.value value.strVal of nkSym: this.key "sym" this.value value.sym #this.value value.sym.name.s of nkIdent: if value.ident != nil: this.key "ident" this.value value.ident.s else: if this.renderSymType and value.typ != nil: this.key "typ" this.value value.typ if value.len > 0: this.key "sons" this.openBracket for i in 0..= 0: result = t.data[index].val else: result = default(T) template idTableGet*[T](t: TIdTable[T], key: PType | PSym): T = getOrDefault(t, key.itemId) proc idTableRawInsert[T](data: var TIdPairSeq[T], key: ItemId, val: T) = var h: Hash let keyId = toId(key) h = keyId and high(data) while not isNil(data[h].key): assert(toId(data[h].key) != keyId) h = nextTry(h, high(data)) assert(isNil(data[h].key)) data[h].key = key data[h].val = val proc `[]=`*[T](t: var TIdTable[T], key: ItemId, val: T) = var index: int n: TIdPairSeq[T] index = idTableRawGet(t, toId(key)) if index >= 0: assert(not isNil(t.data[index].key)) t.data[index].val = val else: if mustRehash(t.data.len, t.counter): newSeq(n, t.data.len * GrowthFactor) for i in 0..high(t.data): if not isNil(t.data[i].key): idTableRawInsert(n, t.data[i].key, t.data[i].val) assert(hasEmptySlot(n)) swap(t.data, n) idTableRawInsert(t.data, key, val) inc(t.counter) template idTablePut*[T](t: var TIdTable[T], key: PType | PSym, val: T) = t[key.itemId] = val iterator idTablePairs*[T](t: TIdTable[T]): tuple[key: ItemId, val: T] = for i in 0..high(t.data): if not isNil(t.data[i].key): yield (t.data[i].key, t.data[i].val) proc initIITable(x: var TIITable) = x.counter = 0 newSeq(x.data, StartSize) for i in 0..= 0: result = t.data[index].val else: result = InvalidKey proc iiTableRawInsert(data: var TIIPairSeq, key, val: int) = var h: Hash h = key and high(data) while data[h].key != InvalidKey: assert(data[h].key != key) h = nextTry(h, high(data)) assert(data[h].key == InvalidKey) data[h].key = key data[h].val = val proc iiTablePut(t: var TIITable, key, val: int) = var index = iiTableRawGet(t, key) if index >= 0: assert(t.data[index].key != InvalidKey) t.data[index].val = val else: if mustRehash(t.data.len, t.counter): var n: TIIPairSeq newSeq(n, t.data.len * GrowthFactor) for i in 0..high(n): n[i].key = InvalidKey for i in 0..high(t.data): if t.data[i].key != InvalidKey: iiTableRawInsert(n, t.data[i].key, t.data[i].val) swap(t.data, n) iiTableRawInsert(t.data, key, val) inc(t.counter) proc listSymbolNames*(symbols: openArray[PSym]): string = result = "" for sym in symbols: if result.len > 0: result.add ", " result.add sym.name.s proc isDiscriminantField*(n: PNode): bool = if n.kind == nkCheckedFieldExpr: sfDiscriminant in n[0][1].sym.flags elif n.kind == nkDotExpr: sfDiscriminant in n[1].sym.flags else: false