From 9ecb1aae80e73ef246d7f719753e8a68b6b01fa0 Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 29 May 2019 20:47:28 +0200 Subject: new compiler feature: --expandMacro --- compiler/commands.nim | 5 ++++- compiler/lineinfos.nim | 5 +++-- compiler/options.nim | 2 ++ compiler/sem.nim | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) (limited to 'compiler') diff --git a/compiler/commands.nim b/compiler/commands.nim index 298fbdc89..cf7fef24d 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -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: diff --git a/compiler/lineinfos.nim b/compiler/lineinfos.nim index 908c5f5ed..b84d8c895 100644 --- a/compiler/lineinfos.nim +++ b/compiler/lineinfos.nim @@ -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", ] diff --git a/compiler/options.nim b/compiler/options.nim index bacef5b4a..833af6846 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -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](), diff --git a/compiler/sem.nim b/compiler/sem.nim index a4e6a427b..3c5d036dc 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -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) -- cgit 1.4.1-2-gfad0