new compiler feature: --expandMacro
This commit is contained in:
parent
dc5fa90b43
commit
9ecb1aae80
7 changed files with 34 additions and 4 deletions
|
|
@ -271,6 +271,9 @@ proc enumToString*(enums: openArray[enum]): string =
|
||||||
- The `--hotCodeReloading` has been implemented for the native targets.
|
- The `--hotCodeReloading` has been implemented for the native targets.
|
||||||
The compiler also provides a new more flexible API for handling the
|
The compiler also provides a new more flexible API for handling the
|
||||||
hot code reloading events in the code.
|
hot code reloading events in the code.
|
||||||
|
- The compiler nows supports a ``--expandMacro:macroNameHere`` switch
|
||||||
|
for easy introspection into what a macro expands into.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Bugfixes
|
### Bugfixes
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ bootSwitch(usedNoGC, defined(nogc), "--gc:none")
|
||||||
import
|
import
|
||||||
os, msgs, options, nversion, condsyms, strutils, extccomp, platform,
|
os, msgs, options, nversion, condsyms, strutils, extccomp, platform,
|
||||||
wordrecg, parseutils, nimblecmd, idents, parseopt, sequtils, lineinfos,
|
wordrecg, parseutils, nimblecmd, idents, parseopt, sequtils, lineinfos,
|
||||||
pathutils
|
pathutils, strtabs
|
||||||
|
|
||||||
# but some have deps to imported modules. Yay.
|
# but some have deps to imported modules. Yay.
|
||||||
bootSwitch(usedTinyC, hasTinyCBackend, "-d:tinyc")
|
bootSwitch(usedTinyC, hasTinyCBackend, "-d:tinyc")
|
||||||
|
|
@ -774,6 +774,9 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
|
||||||
processOnOffSwitchG(conf, {optDocInternal}, arg, pass, info)
|
processOnOffSwitchG(conf, {optDocInternal}, arg, pass, info)
|
||||||
of "multimethods":
|
of "multimethods":
|
||||||
processOnOffSwitchG(conf, {optMultiMethods}, arg, pass, info)
|
processOnOffSwitchG(conf, {optMultiMethods}, arg, pass, info)
|
||||||
|
of "expandmacro":
|
||||||
|
expectArg(conf, switch, arg, pass, info)
|
||||||
|
conf.macrosToExpand[arg] = "T"
|
||||||
of "":
|
of "":
|
||||||
conf.projectName = "-"
|
conf.projectName = "-"
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ type
|
||||||
hintConditionAlwaysTrue, hintConditionAlwaysFalse, hintName, hintPattern,
|
hintConditionAlwaysTrue, hintConditionAlwaysFalse, hintName, hintPattern,
|
||||||
hintExecuting, hintLinking, hintDependency,
|
hintExecuting, hintLinking, hintDependency,
|
||||||
hintSource, hintPerformance, hintStackTrace, hintGCStats,
|
hintSource, hintPerformance, hintStackTrace, hintGCStats,
|
||||||
hintGlobalVar,
|
hintGlobalVar, hintExpandMacro,
|
||||||
hintUser, hintUserRaw,
|
hintUser, hintUserRaw,
|
||||||
hintExtendedContext
|
hintExtendedContext
|
||||||
|
|
||||||
|
|
@ -117,6 +117,7 @@ const
|
||||||
hintStackTrace: "$1",
|
hintStackTrace: "$1",
|
||||||
hintGCStats: "$1",
|
hintGCStats: "$1",
|
||||||
hintGlobalVar: "global variable declared here",
|
hintGlobalVar: "global variable declared here",
|
||||||
|
hintExpandMacro: "expanded macro: $1",
|
||||||
hintUser: "$1",
|
hintUser: "$1",
|
||||||
hintUserRaw: "$1",
|
hintUserRaw: "$1",
|
||||||
hintExtendedContext: "$1",
|
hintExtendedContext: "$1",
|
||||||
|
|
@ -141,7 +142,7 @@ const
|
||||||
"XDeclaredButNotUsed", "ConvToBaseNotNeeded", "ConvFromXtoItselfNotNeeded",
|
"XDeclaredButNotUsed", "ConvToBaseNotNeeded", "ConvFromXtoItselfNotNeeded",
|
||||||
"ExprAlwaysX", "QuitCalled", "Processing", "CodeBegin", "CodeEnd", "Conf",
|
"ExprAlwaysX", "QuitCalled", "Processing", "CodeBegin", "CodeEnd", "Conf",
|
||||||
"Path", "CondTrue", "CondFalse", "Name", "Pattern", "Exec", "Link", "Dependency",
|
"Path", "CondTrue", "CondFalse", "Name", "Pattern", "Exec", "Link", "Dependency",
|
||||||
"Source", "Performance", "StackTrace", "GCStats", "GlobalVar",
|
"Source", "Performance", "StackTrace", "GCStats", "GlobalVar", "ExpandMacro",
|
||||||
"User", "UserRaw", "ExtendedContext",
|
"User", "UserRaw", "ExtendedContext",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -180,6 +180,7 @@ type
|
||||||
linesCompiled*: int # all lines that have been compiled
|
linesCompiled*: int # all lines that have been compiled
|
||||||
options*: TOptions # (+)
|
options*: TOptions # (+)
|
||||||
globalOptions*: TGlobalOptions # (+)
|
globalOptions*: TGlobalOptions # (+)
|
||||||
|
macrosToExpand*: StringTableRef
|
||||||
m*: MsgConfig
|
m*: MsgConfig
|
||||||
evalTemplateCounter*: int
|
evalTemplateCounter*: int
|
||||||
evalMacroCounter*: int
|
evalMacroCounter*: int
|
||||||
|
|
@ -308,6 +309,7 @@ proc newConfigRef*(): ConfigRef =
|
||||||
verbosity: 1,
|
verbosity: 1,
|
||||||
options: DefaultOptions,
|
options: DefaultOptions,
|
||||||
globalOptions: DefaultGlobalOptions,
|
globalOptions: DefaultGlobalOptions,
|
||||||
|
macrosToExpand: newStringTable(modeStyleInsensitive),
|
||||||
m: initMsgConfig(),
|
m: initMsgConfig(),
|
||||||
evalExpr: "",
|
evalExpr: "",
|
||||||
cppDefines: initSet[string](),
|
cppDefines: initSet[string](),
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import
|
||||||
procfind, lookups, pragmas, passes, semdata, semtypinst, sigmatch,
|
procfind, lookups, pragmas, passes, semdata, semtypinst, sigmatch,
|
||||||
intsets, transf, vmdef, vm, idgen, aliases, cgmeth, lambdalifting,
|
intsets, transf, vmdef, vm, idgen, aliases, cgmeth, lambdalifting,
|
||||||
evaltempl, patterns, parampatterns, sempass2, linter, semmacrosanity,
|
evaltempl, patterns, parampatterns, sempass2, linter, semmacrosanity,
|
||||||
lowerings, pluginsupport, plugins/active, rod, lineinfos
|
lowerings, pluginsupport, plugins/active, rod, lineinfos, strtabs
|
||||||
|
|
||||||
from modulegraphs import ModuleGraph, PPassContext, onUse, onDef, onDefResolveForward
|
from modulegraphs import ModuleGraph, PPassContext, onUse, onDef, onDefResolveForward
|
||||||
|
|
||||||
|
|
@ -467,6 +467,8 @@ proc semMacroExpr(c: PContext, n, nOrig: PNode, sym: PSym,
|
||||||
result = evalMacroCall(c.module, c.graph, n, nOrig, sym)
|
result = evalMacroCall(c.module, c.graph, n, nOrig, sym)
|
||||||
if efNoSemCheck notin flags:
|
if efNoSemCheck notin flags:
|
||||||
result = semAfterMacroCall(c, n, result, sym, flags)
|
result = semAfterMacroCall(c, n, result, sym, flags)
|
||||||
|
if c.config.macrosToExpand.hasKey(sym.name.s):
|
||||||
|
message(c.config, nOrig.info, hintExpandMacro, renderTree(result))
|
||||||
result = wrapInComesFrom(nOrig.info, sym, result)
|
result = wrapInComesFrom(nOrig.info, sym, result)
|
||||||
popInfoContext(c.config)
|
popInfoContext(c.config)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -103,6 +103,7 @@ Advanced options:
|
||||||
--cppCompileToNamespace:namespace
|
--cppCompileToNamespace:namespace
|
||||||
use the provided namespace for the generated C++ code,
|
use the provided namespace for the generated C++ code,
|
||||||
if no namespace is provided "Nim" will be used
|
if no namespace is provided "Nim" will be used
|
||||||
|
--expandMacro:MACRO dump every generated AST from MACRO
|
||||||
--excludePath:PATH exclude a path from the list of search paths
|
--excludePath:PATH exclude a path from the list of search paths
|
||||||
--dynlibOverride:SYMBOL marks SYMBOL so that dynlib:SYMBOL
|
--dynlibOverride:SYMBOL marks SYMBOL so that dynlib:SYMBOL
|
||||||
has no effect and can be statically linked instead;
|
has no effect and can be statically linked instead;
|
||||||
|
|
|
||||||
18
tests/compilerfeatures/texpandmacro.nim
Normal file
18
tests/compilerfeatures/texpandmacro.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
discard """
|
||||||
|
cmd: "nim c --expandMacro:foo $file"
|
||||||
|
nimout: '''Hint: expanded macro:
|
||||||
|
echo ["injected echo"]
|
||||||
|
var x = 4 [ExpandMacro]
|
||||||
|
'''
|
||||||
|
output: '''injected echo'''
|
||||||
|
"""
|
||||||
|
|
||||||
|
import macros
|
||||||
|
|
||||||
|
macro foo(x: untyped): untyped =
|
||||||
|
result = quote do:
|
||||||
|
echo "injected echo"
|
||||||
|
`x`
|
||||||
|
|
||||||
|
foo:
|
||||||
|
var x = 4
|
||||||
Loading…
Add table
Add a link
Reference in a new issue