mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-20 22:35:24 +00:00
implement the export/except statement
This commit is contained in:
committed by
Andreas Rumpf
parent
7297613900
commit
cf13c5fba4
@@ -108,6 +108,8 @@
|
||||
- Thread-local variables can now be declared inside procs. This implies all
|
||||
the effects of the `global` pragma.
|
||||
|
||||
- Nim now supports `except` clause in the export statement.
|
||||
|
||||
### Tool changes
|
||||
|
||||
- ``jsondoc2`` has been renamed ``jsondoc``, similar to how ``doc2`` was renamed
|
||||
|
||||
@@ -16,6 +16,13 @@ import
|
||||
proc evalImport*(c: PContext, n: PNode): PNode
|
||||
proc evalFrom*(c: PContext, n: PNode): PNode
|
||||
|
||||
proc readExceptSet*(n: PNode): IntSet =
|
||||
assert n.kind in {nkImportExceptStmt, nkExportExceptStmt}
|
||||
result = initIntSet()
|
||||
for i in 1 ..< n.len:
|
||||
let ident = lookups.considerQuotedIdent(n[i])
|
||||
result.incl(ident.id)
|
||||
|
||||
proc importPureEnumField*(c: PContext; s: PSym) =
|
||||
var check = strTableGet(c.importTable.symbols, s.name)
|
||||
if check == nil:
|
||||
@@ -198,9 +205,5 @@ proc evalImportExcept*(c: PContext, n: PNode): PNode =
|
||||
if m != nil:
|
||||
n.sons[0] = newSymNode(m)
|
||||
addDecl(c, m, n.info) # add symbol to symbol table of module
|
||||
var exceptSet = initIntSet()
|
||||
for i in countup(1, sonsLen(n) - 1):
|
||||
let ident = lookups.considerQuotedIdent(n.sons[i])
|
||||
exceptSet.incl(ident.id)
|
||||
importAllSymbolsExcept(c, m, exceptSet)
|
||||
importAllSymbolsExcept(c, m, readExceptSet(n))
|
||||
#importForwarded(c, m.ast, exceptSet)
|
||||
|
||||
@@ -2160,9 +2160,25 @@ proc semBlock(c: PContext, n: PNode): PNode =
|
||||
closeScope(c)
|
||||
dec(c.p.nestedBlockCounter)
|
||||
|
||||
proc semExportExcept(c: PContext, n: PNode): PNode =
|
||||
let moduleName = semExpr(c, n[0])
|
||||
if moduleName.kind != nkSym or moduleName.sym.kind != skModule:
|
||||
localError(n.info, "The export/except syntax expects a module name")
|
||||
return
|
||||
let exceptSet = readExceptSet(n)
|
||||
let exported = moduleName.sym
|
||||
strTableAdd(c.module.tab, exported)
|
||||
var i: TTabIter
|
||||
var s = initTabIter(i, exported.tab)
|
||||
while s != nil:
|
||||
if s.kind in ExportableSymKinds+{skModule} and
|
||||
s.name.id notin exceptSet:
|
||||
strTableAdd(c.module.tab, s)
|
||||
s = nextIter(i, exported.tab)
|
||||
result = n
|
||||
|
||||
proc semExport(c: PContext, n: PNode): PNode =
|
||||
var x = newNodeI(n.kind, n.info)
|
||||
#let L = if n.kind == nkExportExceptStmt: L = 1 else: n.len
|
||||
for i in 0..<n.len:
|
||||
let a = n.sons[i]
|
||||
var o: TOverloadIter
|
||||
@@ -2448,9 +2464,12 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
|
||||
of nkIncludeStmt:
|
||||
#if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "include")
|
||||
result = evalInclude(c, n)
|
||||
of nkExportStmt, nkExportExceptStmt:
|
||||
of nkExportStmt:
|
||||
if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "export")
|
||||
result = semExport(c, n)
|
||||
of nkExportExceptStmt:
|
||||
if not isTopLevel(c): localError(n.info, errXOnlyAtModuleScope, "export")
|
||||
result = semExportExcept(c, n)
|
||||
of nkPragmaBlock:
|
||||
result = semPragmaBlock(c, n)
|
||||
of nkStaticStmt:
|
||||
|
||||
@@ -6298,6 +6298,9 @@ modules don't need to import a module's dependencies:
|
||||
var x: MyObject
|
||||
echo $x
|
||||
|
||||
When the exported symbol is another module, all of its definitions will
|
||||
be forwarded. You can use an ``except`` list to exclude some of the symbols.
|
||||
|
||||
Note on paths
|
||||
-----------
|
||||
In module related statements, if any part of the module name /
|
||||
|
||||
4
tests/modules/definitions.nim
Normal file
4
tests/modules/definitions.nim
Normal file
@@ -0,0 +1,4 @@
|
||||
var v*: int
|
||||
proc p* = echo "proc p called"
|
||||
template t* = echo "template t expanded"
|
||||
|
||||
3
tests/modules/proxy_module.nim
Normal file
3
tests/modules/proxy_module.nim
Normal file
@@ -0,0 +1,3 @@
|
||||
import definitions
|
||||
export definitions except p
|
||||
|
||||
Reference in New Issue
Block a user