summary refs log tree commit diff stats
path: root/compiler/importer.nim
blob: c013b93ab1a217dff3bcdcafb0c39ebcff701d0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#
#
#           The Nim Compiler
#        (c) Copyright 2013 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module implements the symbol importing mechanism.

import
  intsets, strutils, os, ast, astalgo, msgs, options, idents, lookups,
  semdata, passes, renderer, modulepaths, sigmatch, lineinfos

proc evalImport*(c: PContext, n: PNode): PNode
proc evalFrom*(c: PContext, n: PNode): PNode

proc readExceptSet*(c: PContext, n: PNode): IntSet =
  assert n.kind in {nkImportExceptStmt, nkExportExceptStmt}
  result = initIntSet()
  for i in 1 ..< n.len:
    let ident = lookups.considerQuotedIdent(c, n[i])
    result.incl(ident.id)

proc importPureEnumField*(c: PContext; s: PSym) =
  var check = strTableGet(c.importTable.symbols, s.name)
  if check == nil:
    strTableAdd(c.pureEnumFields, s)

proc rawImportSymbol(c: PContext, s: PSym) =
  # This does not handle stubs, because otherwise loading on demand would be
  # pointless in practice. So importing stubs is fine here!
  # check if we have already a symbol of the same name:
  var check = strTableGet(c.importTable.symbols, s.name)
  if check != nil and check.id != s.id:
    if s.kind notin OverloadableSyms or check.kind notin OverloadableSyms:
      # s and check need to be qualified:
      incl(c.ambiguousSymbols, s.id)
      incl(c.ambiguousSymbols, check.id)
  # thanks to 'export' feature, it could be we import the same symbol from
  # multiple sources, so we need to call 'StrTableAdd' here:
  strTableAdd(c.importTable.symbols, s)
  if s.kind == skType:
    var etyp = s.typ
    if etyp.kind in {tyBool, tyEnum}:
      for j in countup(0, sonsLen(etyp.n) - 1):
        var e = etyp.n.sons[j].sym
        if e.kind != skEnumField:
          internalError(c.config, s.info, "rawImportSymbol")
          # BUGFIX: because of aliases for enums the symbol may already
          # have been put into the symbol table
          # BUGFIX: but only iff they are the same symbols!
        var it: TIdentIter
        check = initIdentIter(it, c.importTable.symbols, e.name)
        while check != nil:
          if check.id == e.id:
            e = nil
            break
          check = nextIdentIter(it, c.importTable.symbols)
        if e != nil:
          if sfPure notin s.flags:
            rawImportSymbol(c, e)
          else:
            importPureEnumField(c, e)
  else:
    # rodgen assures that converters and patterns are no stubs
    if s.kind == skConverter: addConverter(c, s)
    if hasPattern(s): addPattern(c, s)

proc importSymbol(c: PContext, n: PNode, fromMod: PSym) =
  let ident = lookups.considerQuotedIdent(c, n)
  let s = strTableGet(fromMod.tab, ident)
  if s == nil:
    errorUndeclaredIdentifier(c, n.info, ident.s)
  else:
    when false:
      if s.kind == skStub: loadStub(s)
    if s.kind notin ExportableSymKinds:
      internalError(c.config, n.info, "importSymbol: 2")
    # for an enumeration we have to add all identifiers
    case s.kind
    of skProcKinds:
      # for a overloadable syms add all overloaded routines
      var it: TIdentIter
      var e = initIdentIter(it, fromMod.tab, s.name)
      while e != nil:
        if e.name.id != s.name.id: internalError(c.config, n.info, "importSymbol: 3")
        rawImportSymbol(c, e)
        e = nextIdentIter(it, fromMod.tab)
    else: rawImportSymbol(c, s)

proc importAllSymbolsExcept(c: PContext, fromMod: PSym, exceptSet: IntSet) =
  var i: TTabIter
  var s = initTabIter(i, fromMod.tab)
  while s != nil:
    if s.kind != skModule:
      if s.kind != skEnumField:
        if s.kind notin ExportableSymKinds:
          internalError(c.config, s.info, "importAllSymbols: " & $s.kind)
        if exceptSet.isNil or s.name.id notin exceptSet:
          rawImportSymbol(c, s)
    s = nextIter(i, fromMod.tab)

proc importAllSymbols*(c: PContext, fromMod: PSym) =
  var exceptSet: IntSet
  importAllSymbolsExcept(c, fromMod, exceptSet)

proc importForwarded(c: PContext, n: PNode, exceptSet: IntSet) =
  if n.isNil: return
  case n.kind
  of nkExportStmt:
    for a in n:
      assert a.kind == nkSym
      let s = a.sym
      if s.kind == skModule:
        importAllSymbolsExcept(c, s, exceptSet)
      elif exceptSet.isNil or s.name.id notin exceptSet:
        rawImportSymbol(c, s)
  of nkExportExceptStmt:
    localError(c.config, n.info, "'export except' not implemented")
  else:
    for i in 0..safeLen(n)-1:
      importForwarded(c, n.sons[i], exceptSet)

proc importModuleAs(c: PContext; n: PNode, realModule: PSym): PSym =
  result = realModule
  if n.kind != nkImportAs: discard
  elif n.len != 2 or n.sons[1].kind != nkIdent:
    localError(c.config, n.info, "module alias must be an identifier")
  elif n.sons[1].ident.id != realModule.name.id:
    # some misguided guy will write 'import abc.foo as foo' ...
    result = createModuleAlias(realModule, n.sons[1].ident, realModule.info,
                               c.config.options)

proc myImportModule(c: PContext, n: PNode; importStmtResult: PNode): PSym =
  var f = checkModuleName(c.config, n)
  if f != InvalidFileIDX:
    let L = c.graph.importStack.len
    let recursion = c.graph.importStack.find(f)
    c.graph.importStack.add f
    #echo "adding ", toFullPath(f), " at ", L+1
    if recursion >= 0:
      var err = ""
      for i in countup(recursion, L-1):
        if i > recursion: err.add "\n"
        err.add toFullPath(c.config, c.graph.importStack[i]) & " imports " &
                toFullPath(c.config, c.graph.importStack[i+1])
      c.recursiveDep = err
    result = importModuleAs(c, n, c.graph.importModuleCallback(c.graph, c.module, f))
    #echo "set back to ", L
    c.graph.importStack.setLen(L)
    # we cannot perform this check reliably because of
    # test: modules/import_in_config)
    when true:
      if result.info.fileIndex == c.module.info.fileIndex and
          result.info.fileIndex == n.info.fileIndex:
        localError(c.config, n.info, "A module cannot import itself")
    if sfDeprecated in result.flags:
      if result.constraint != nil:
        message(c.config, n.info, warnDeprecated, result.constraint.strVal & "; " & result.name.s)
      else:
        message(c.config, n.info, warnDeprecated, result.name.s)
    suggestSym(c.config, n.info, result, c.graph.usageSym, false)
    importStmtResult.add newStrNode(toFullPath(c.config, f), n.info)

proc impMod(c: PContext; it: PNode; importStmtResult: PNode) =
  let m = myImportModule(c, it, importStmtResult)
  if m != nil:
    var emptySet: IntSet
    # ``addDecl`` needs to be done before ``importAllSymbols``!
    addDecl(c, m, it.info) # add symbol to symbol table of module
    importAllSymbolsExcept(c, m, emptySet)
    #importForwarded(c, m.ast, emptySet)

proc evalImport(c: PContext, n: PNode): PNode =
  #result = n
  result = newNodeI(nkImportStmt, n.info)
  for i in countup(0, sonsLen(n) - 1):
    let it = n.sons[i]
    if it.kind == nkInfix and it.len == 3 and it[2].kind == nkBracket:
      let sep = it[0]
      let dir = it[1]
      let a = newNodeI(nkInfix, it.info)
      a.add sep
      a.add dir
      a.add sep # dummy entry, replaced in the loop
      for x in it[2]:
        a.sons[2] = x
        impMod(c, a, result)
    else:
      impMod(c, it, result)

proc evalFrom(c: PContext, n: PNode): PNode =
  result = newNodeI(nkImportStmt, n.info)
  checkMinSonsLen(n, 2, c.config)
  var m = myImportModule(c, n.sons[0], result)
  if m != nil:
    n.sons[0] = newSymNode(m)
    addDecl(c, m, n.info)               # add symbol to symbol table of module
    for i in countup(1, sonsLen(n) - 1):
      if n.sons[i].kind != nkNilLit:
        importSymbol(c, n.sons[i], m)

proc evalImportExcept*(c: PContext, n: PNode): PNode =
  result = newNodeI(nkImportStmt, n.info)
  checkMinSonsLen(n, 2, c.config)
  var m = myImportModule(c, n.sons[0], result)
  if m != nil:
    n.sons[0] = newSymNode(m)
    addDecl(c, m, n.info)               # add symbol to symbol table of module
    importAllSymbolsExcept(c, m, readExceptSet(c, n))
    #importForwarded(c, m.ast, exceptSet)
n class="n">flags) c &= char(t.callConv) # purity of functions doesn't have to affect the mangling (which is in fact # problematic for HCR - someone could have cached a pointer to another # function which changes its purity and suddenly the cached pointer is danglign) # IMHO anything that doesn't affect the overload resolution shouldn't be part of the mangling... # if CoType notin flags: # if tfNoSideEffect in t.flags: c &= ".noSideEffect" # if tfThread in t.flags: c &= ".thread" if tfVarargs in t.flags: c &= ".varargs" of tyArray: c &= char(t.kind) for i in 0..<t.len: c.hashType(t[i], flags-{CoIgnoreRange}) else: c &= char(t.kind) for i in 0..<t.len: c.hashType(t[i], flags) if tfNotNil in t.flags and CoType notin flags: c &= "not nil" when defined(debugSigHashes): import db_sqlite let db = open(connection="sighashes.db", user="araq", password="", database="sighashes") db.exec(sql"DROP TABLE IF EXISTS sighashes") db.exec sql"""CREATE TABLE sighashes( id integer primary key, hash varchar(5000) not null, type varchar(5000) not null, unique (hash, type))""" # select hash, type from sighashes where hash in # (select hash from sighashes group by hash having count(*) > 1) order by hash; proc hashType*(t: PType; flags: set[ConsiderFlag] = {CoType}): SigHash = var c: MD5Context md5Init c hashType c, t, flags+{CoOwnerSig} md5Final c, result.MD5Digest when defined(debugSigHashes): db.exec(sql"INSERT OR IGNORE INTO sighashes(type, hash) VALUES (?, ?)", typeToString(t), $result) proc hashProc*(s: PSym): SigHash = var c: MD5Context md5Init c hashType c, s.typ, {CoProc} var m = s while m.kind != skModule: m = m.owner let p = m.owner assert p.kind == skPackage c &= p.name.s c &= "." c &= m.name.s if sfDispatcher in s.flags: c &= ".dispatcher" # so that createThread[void]() (aka generic specialization) gets a unique # hash, we also hash the line information. This is pretty bad, but the best # solution for now: #c &= s.info.line md5Final c, result.MD5Digest proc hashNonProc*(s: PSym): SigHash = var c: MD5Context md5Init c hashSym(c, s) var it = s while it != nil: c &= it.name.s c &= "." it = it.owner # for bug #5135 we also take the position into account, but only # for parameters, because who knows what else position dependency # might cause: if s.kind == skParam: c &= s.position md5Final c, result.MD5Digest proc hashOwner*(s: PSym): SigHash = var c: MD5Context md5Init c var m = s while m.kind != skModule: m = m.owner let p = m.owner assert p.kind == skPackage c &= p.name.s c &= "." c &= m.name.s md5Final c, result.MD5Digest proc sigHash*(s: PSym): SigHash = if s.kind in routineKinds and s.typ != nil: result = hashProc(s) else: result = hashNonProc(s) proc symBodyDigest*(graph: ModuleGraph, sym: PSym): SigHash proc hashBodyTree(graph: ModuleGraph, c: var MD5Context, n: PNode) proc hashVarSymBody(graph: ModuleGraph, c: var MD5Context, s: PSym) = assert: s.kind in {skParam, skResult, skVar, skLet, skConst, skForVar} if sfGlobal notin s.flags: c &= char(s.kind) c &= s.name.s else: c &= hashNonProc(s) # this one works for let and const but not for var. True variables can change value # later on. it is user resposibility to hash his global state if required if s.ast != nil and s.ast.kind in {nkIdentDefs, nkConstDef}: hashBodyTree(graph, c, s.ast[^1]) else: hashBodyTree(graph, c, s.ast) proc hashBodyTree(graph: ModuleGraph, c: var MD5Context, n: PNode) = # hash Nim tree recursing into simply if n == nil: c &= "nil" return c &= char(n.kind) case n.kind of nkEmpty, nkNilLit, nkType: discard of nkIdent: c &= n.ident.s of nkSym: if n.sym.kind in skProcKinds: c &= symBodyDigest(graph, n.sym) elif n.sym.kind in {skParam, skResult, skVar, skLet, skConst, skForVar}: hashVarSymBody(graph, c, n.sym) else: c &= hashNonProc(n.sym) of nkProcDef, nkFuncDef, nkTemplateDef, nkMacroDef: discard # we track usage of proc symbols not their definition of nkCharLit..nkUInt64Lit: c &= n.intVal of nkFloatLit..nkFloat64Lit: c &= n.floatVal of nkStrLit..nkTripleStrLit: c &= n.strVal else: for i in 0..<n.len: hashBodyTree(graph, c, n[i]) proc symBodyDigest*(graph: ModuleGraph, sym: PSym): SigHash = ## compute unique digest of the proc/func/method symbols ## recursing into invoked symbols as well assert(sym.kind in skProcKinds, $sym.kind) graph.symBodyHashes.withValue(sym.id, value): return value[] var c: MD5Context md5Init(c) c.hashType(sym.typ, {CoProc}) c &= char(sym.kind) c.md5Final(result.MD5Digest) graph.symBodyHashes[sym.id] = result # protect from recursion in the body if sym.ast != nil: md5Init(c) c.md5Update(cast[cstring](result.addr), sizeof(result)) hashBodyTree(graph, c, getBody(graph, sym)) c.md5Final(result.MD5Digest) graph.symBodyHashes[sym.id] = result proc idOrSig*(s: PSym, currentModule: string, sigCollisions: var CountTable[SigHash]): Rope = if s.kind in routineKinds and s.typ != nil: # signatures for exported routines are reliable enough to # produce a unique name and this means produced C++ is more stable regarding # Nim changes: let sig = hashProc(s) result = rope($sig) #let m = if s.typ.callConv != ccInline: findPendingModule(m, s) else: m let counter = sigCollisions.getOrDefault(sig) #if sigs == "_jckmNePK3i2MFnWwZlp6Lg" and s.name.s == "contains": # echo "counter ", counter, " ", s.id if counter != 0: result.add "_" & rope(counter+1) # this minor hack is necessary to make tests/collections/thashes compile. # The inlined hash function's original module is ambiguous so we end up # generating duplicate names otherwise: if s.typ.callConv == ccInline: result.add rope(currentModule) sigCollisions.inc(sig) else: let sig = hashNonProc(s) result = rope($sig) let counter = sigCollisions.getOrDefault(sig) if counter != 0: result.add "_" & rope(counter+1) sigCollisions.inc(sig)