new compiler feature: --expandMacro

This commit is contained in:
Araq
2019-05-29 20:47:28 +02:00
parent dc5fa90b43
commit 9ecb1aae80
7 changed files with 34 additions and 4 deletions

View File

@@ -271,6 +271,9 @@ proc enumToString*(enums: openArray[enum]): string =
- The `--hotCodeReloading` has been implemented for the native targets.
The compiler also provides a new more flexible API for handling the
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

View File

@@ -27,7 +27,7 @@ bootSwitch(usedNoGC, defined(nogc), "--gc:none")
import
os, msgs, options, nversion, condsyms, strutils, extccomp, platform,
wordrecg, parseutils, nimblecmd, idents, parseopt, sequtils, lineinfos,
pathutils
pathutils, strtabs
# but some have deps to imported modules. Yay.
bootSwitch(usedTinyC, hasTinyCBackend, "-d:tinyc")
@@ -774,6 +774,9 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
processOnOffSwitchG(conf, {optDocInternal}, arg, pass, info)
of "multimethods":
processOnOffSwitchG(conf, {optMultiMethods}, arg, pass, info)
of "expandmacro":
expectArg(conf, switch, arg, pass, info)
conf.macrosToExpand[arg] = "T"
of "":
conf.projectName = "-"
else:

View File

@@ -44,7 +44,7 @@ type
hintConditionAlwaysTrue, hintConditionAlwaysFalse, hintName, hintPattern,
hintExecuting, hintLinking, hintDependency,
hintSource, hintPerformance, hintStackTrace, hintGCStats,
hintGlobalVar,
hintGlobalVar, hintExpandMacro,
hintUser, hintUserRaw,
hintExtendedContext
@@ -117,6 +117,7 @@ const
hintStackTrace: "$1",
hintGCStats: "$1",
hintGlobalVar: "global variable declared here",
hintExpandMacro: "expanded macro: $1",
hintUser: "$1",
hintUserRaw: "$1",
hintExtendedContext: "$1",
@@ -141,7 +142,7 @@ const
"XDeclaredButNotUsed", "ConvToBaseNotNeeded", "ConvFromXtoItselfNotNeeded",
"ExprAlwaysX", "QuitCalled", "Processing", "CodeBegin", "CodeEnd", "Conf",
"Path", "CondTrue", "CondFalse", "Name", "Pattern", "Exec", "Link", "Dependency",
"Source", "Performance", "StackTrace", "GCStats", "GlobalVar",
"Source", "Performance", "StackTrace", "GCStats", "GlobalVar", "ExpandMacro",
"User", "UserRaw", "ExtendedContext",
]

View File

@@ -180,6 +180,7 @@ type
linesCompiled*: int # all lines that have been compiled
options*: TOptions # (+)
globalOptions*: TGlobalOptions # (+)
macrosToExpand*: StringTableRef
m*: MsgConfig
evalTemplateCounter*: int
evalMacroCounter*: int
@@ -308,6 +309,7 @@ proc newConfigRef*(): ConfigRef =
verbosity: 1,
options: DefaultOptions,
globalOptions: DefaultGlobalOptions,
macrosToExpand: newStringTable(modeStyleInsensitive),
m: initMsgConfig(),
evalExpr: "",
cppDefines: initSet[string](),

View File

@@ -16,7 +16,7 @@ import
procfind, lookups, pragmas, passes, semdata, semtypinst, sigmatch,
intsets, transf, vmdef, vm, idgen, aliases, cgmeth, lambdalifting,
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
@@ -467,6 +467,8 @@ proc semMacroExpr(c: PContext, n, nOrig: PNode, sym: PSym,
result = evalMacroCall(c.module, c.graph, n, nOrig, sym)
if efNoSemCheck notin 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)
popInfoContext(c.config)

View File

@@ -103,6 +103,7 @@ Advanced options:
--cppCompileToNamespace:namespace
use the provided namespace for the generated C++ code,
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
--dynlibOverride:SYMBOL marks SYMBOL so that dynlib:SYMBOL
has no effect and can be statically linked instead;

View 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