ast2 static inline with -H, fix #188, --replace support
This commit is contained in:
parent
485c2f1ab2
commit
ab0439e60e
8 changed files with 61 additions and 16 deletions
|
|
@ -184,6 +184,7 @@ Options:
|
||||||
-E=, --prefix= strings {} strip prefix from identifiers
|
-E=, --prefix= strings {} strip prefix from identifiers
|
||||||
-p, --preprocess bool false run preprocessor on header
|
-p, --preprocess bool false run preprocessor on header
|
||||||
-r, --recurse bool false process #include files
|
-r, --recurse bool false process #include files
|
||||||
|
-G=, --replace= strings {} replace X with Y in identifiers, X1=Y1,X2=Y2, @X for regex
|
||||||
-s, --stub bool false stub out undefined type references as objects
|
-s, --stub bool false stub out undefined type references as objects
|
||||||
-F=, --suffix= strings {} strip suffix from identifiers
|
-F=, --suffix= strings {} strip suffix from identifiers
|
||||||
-O=, --symOverride= strings {} skip generating specified symbols
|
-O=, --symOverride= strings {} skip generating specified symbols
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
##[
|
##[
|
||||||
Module that should import everything so that `nim doc --project nimtero/all` runs docs on everything.
|
The following modules are available to users of Nimterop.
|
||||||
]##
|
]##
|
||||||
|
|
||||||
# TODO: make sure it does import everything.
|
|
||||||
|
|
||||||
import "."/[docs, cimport, build, types, plugin]
|
import "."/[docs, cimport, build, types, plugin]
|
||||||
|
|
|
||||||
|
|
@ -1636,6 +1636,22 @@ proc addDecl(nimState: NimState, node: TSNode) =
|
||||||
# Regular var
|
# Regular var
|
||||||
discard
|
discard
|
||||||
|
|
||||||
|
proc addDef(nimState: NimState, node: TSNode) =
|
||||||
|
# Wrap static inline definition if {.header.} mode is specified
|
||||||
|
#
|
||||||
|
# Without {.header.} the definition will not be available to the C compiler
|
||||||
|
# and will fail at link time
|
||||||
|
decho("addDef()")
|
||||||
|
nimState.printDebug(node)
|
||||||
|
let
|
||||||
|
start = getStartAtom(node)
|
||||||
|
if node[start+1].getName() == "function_declarator":
|
||||||
|
if nimState.includeHeader():
|
||||||
|
nimState.addProc(node[start+1], node[start])
|
||||||
|
else:
|
||||||
|
necho &"\n# proc '$1' skipped - static inline procs require 'includeHeader'" %
|
||||||
|
nimState.getNodeVal(node[start+1].getAtom())
|
||||||
|
|
||||||
proc processNode(nimState: NimState, node: TSNode): bool =
|
proc processNode(nimState: NimState, node: TSNode): bool =
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
|
|
@ -1658,11 +1674,7 @@ proc processNode(nimState: NimState, node: TSNode): bool =
|
||||||
of "declaration":
|
of "declaration":
|
||||||
nimState.addDecl(node)
|
nimState.addDecl(node)
|
||||||
of "function_definition":
|
of "function_definition":
|
||||||
# Handle static inline
|
nimState.addDef(node)
|
||||||
let
|
|
||||||
start = getStartAtom(node)
|
|
||||||
if node[start+1].getName() == "function_declarator":
|
|
||||||
nimState.addProc(node[start+1], node[start])
|
|
||||||
else:
|
else:
|
||||||
# Unknown
|
# Unknown
|
||||||
result = false
|
result = false
|
||||||
|
|
|
||||||
|
|
@ -137,6 +137,13 @@ proc getIdentifier*(nimState: NimState, name: string, kind: NimSymKind, parent="
|
||||||
if result.endsWith(str):
|
if result.endsWith(str):
|
||||||
result = result[0 .. ^(str.len+1)]
|
result = result[0 .. ^(str.len+1)]
|
||||||
|
|
||||||
|
# --replace from CLI if specified
|
||||||
|
for name, value in nimState.gState.replace.pairs:
|
||||||
|
if name.len > 1 and name[0] == '@':
|
||||||
|
result = result.replace(re(name[1 .. ^1]), value)
|
||||||
|
else:
|
||||||
|
result = result.replace(name, value)
|
||||||
|
|
||||||
checkIdentifier(result, $kind, parent, name)
|
checkIdentifier(result, $kind, parent, name)
|
||||||
|
|
||||||
if result in gReserved or (result == "object" and kind != nskType):
|
if result in gReserved or (result == "object" and kind != nskType):
|
||||||
|
|
@ -738,6 +745,6 @@ proc loadPlugin*(gState: State, sourcePath: string) =
|
||||||
|
|
||||||
proc expandSymlinkAbs*(path: string): string =
|
proc expandSymlinkAbs*(path: string): string =
|
||||||
try:
|
try:
|
||||||
result = path.expandSymlink().absolutePath(path.parentDir()).normalizedPath()
|
result = path.expandFilename().expandSymlink().normalizedPath()
|
||||||
except:
|
except:
|
||||||
result = path
|
result = path
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,8 @@ type
|
||||||
|
|
||||||
code*, convention*, dynlib*, mode*, nim*, overrides*, pluginSource*, pluginSourcePath*: string
|
code*, convention*, dynlib*, mode*, nim*, overrides*, pluginSource*, pluginSourcePath*: string
|
||||||
|
|
||||||
|
replace*: OrderedTableRef[string, string]
|
||||||
|
|
||||||
feature*: seq[Feature]
|
feature*: seq[Feature]
|
||||||
|
|
||||||
onSymbol*, onSymbolOverride*: OnSymbol
|
onSymbol*, onSymbolOverride*: OnSymbol
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import os, osproc, strformat, strutils, times
|
import os, osproc, strformat, strutils, tables, times
|
||||||
|
|
||||||
import "."/treesitter/[api, c, cpp]
|
import "."/treesitter/[api, c, cpp]
|
||||||
|
|
||||||
|
|
@ -67,6 +67,7 @@ proc main(
|
||||||
prefix: seq[string] = @[],
|
prefix: seq[string] = @[],
|
||||||
preprocess = false,
|
preprocess = false,
|
||||||
recurse = false,
|
recurse = false,
|
||||||
|
replace: seq[string] = @[],
|
||||||
stub = false,
|
stub = false,
|
||||||
suffix: seq[string] = @[],
|
suffix: seq[string] = @[],
|
||||||
symOverride: seq[string] = @[],
|
symOverride: seq[string] = @[],
|
||||||
|
|
@ -91,6 +92,7 @@ proc main(
|
||||||
prefix: prefix,
|
prefix: prefix,
|
||||||
preprocess: preprocess,
|
preprocess: preprocess,
|
||||||
recurse: recurse,
|
recurse: recurse,
|
||||||
|
replace: newOrderedTable[string, string](),
|
||||||
suffix: suffix,
|
suffix: suffix,
|
||||||
symOverride: symOverride
|
symOverride: symOverride
|
||||||
)
|
)
|
||||||
|
|
@ -104,6 +106,14 @@ proc main(
|
||||||
gState.prefix = gState.prefix.getSplitComma()
|
gState.prefix = gState.prefix.getSplitComma()
|
||||||
gState.suffix = gState.suffix.getSplitComma()
|
gState.suffix = gState.suffix.getSplitComma()
|
||||||
|
|
||||||
|
# Replace => Table
|
||||||
|
for i in replace.getSplitComma():
|
||||||
|
let
|
||||||
|
nv = i.split("=", maxsplit = 1)
|
||||||
|
name = nv[0]
|
||||||
|
value = if nv.len == 2: nv[1] else: ""
|
||||||
|
gState.replace[name] = value
|
||||||
|
|
||||||
if pluginSourcePath.nBl:
|
if pluginSourcePath.nBl:
|
||||||
gState.loadPlugin(pluginSourcePath)
|
gState.loadPlugin(pluginSourcePath)
|
||||||
|
|
||||||
|
|
@ -205,10 +215,11 @@ when isMainModule:
|
||||||
"pgrammar": "print grammar",
|
"pgrammar": "print grammar",
|
||||||
"pluginSourcePath": "nim file to build and load as a plugin",
|
"pluginSourcePath": "nim file to build and load as a plugin",
|
||||||
"pnim": "print Nim output",
|
"pnim": "print Nim output",
|
||||||
|
"prefix": "strip prefix from identifiers",
|
||||||
"preprocess": "run preprocessor on header",
|
"preprocess": "run preprocessor on header",
|
||||||
"recurse": "process #include files",
|
"recurse": "process #include files",
|
||||||
|
"replace": "replace X with Y in identifiers, X1=Y1,X2=Y2, @X for regex",
|
||||||
"source" : "C/C++ source/header",
|
"source" : "C/C++ source/header",
|
||||||
"prefix": "strip prefix from identifiers",
|
|
||||||
"stub": "stub out undefined type references as objects",
|
"stub": "stub out undefined type references as objects",
|
||||||
"suffix": "strip suffix from identifiers",
|
"suffix": "strip suffix from identifiers",
|
||||||
"symOverride": "skip generating specified symbols"
|
"symOverride": "skip generating specified symbols"
|
||||||
|
|
@ -229,6 +240,7 @@ when isMainModule:
|
||||||
"prefix": 'E',
|
"prefix": 'E',
|
||||||
"preprocess": 'p',
|
"preprocess": 'p',
|
||||||
"recurse": 'r',
|
"recurse": 'r',
|
||||||
|
"replace": 'G',
|
||||||
"stub": 's',
|
"stub": 's',
|
||||||
"suffix": 'F',
|
"suffix": 'F',
|
||||||
"symOverride": 'O'
|
"symOverride": 'O'
|
||||||
|
|
|
||||||
|
|
@ -198,6 +198,10 @@ typedef struct {
|
||||||
enum { NEV6 = 8 * 8, NEV7 } f8;
|
enum { NEV6 = 8 * 8, NEV7 } f8;
|
||||||
} nested;
|
} nested;
|
||||||
|
|
||||||
|
static inline int sitest1(int f1) {
|
||||||
|
return f1 * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// DUPLICATES
|
// DUPLICATES
|
||||||
|
|
@ -390,6 +394,10 @@ typedef struct {
|
||||||
enum { NEV6 = 8 * 8, NEV7 } f8;
|
enum { NEV6 = 8 * 8, NEV7 } f8;
|
||||||
} nested;
|
} nested;
|
||||||
|
|
||||||
|
static inline int sitest1(int f1) {
|
||||||
|
return f1 * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ cOverride:
|
||||||
type
|
type
|
||||||
A1* = A0
|
A1* = A0
|
||||||
|
|
||||||
cImport(path, flags="-f:ast2 -ENK_,SDL_" & flags)
|
cImport(path, flags="-f:ast2 -ENK_,SDL_ -GVICE=SLICE" & flags)
|
||||||
|
|
||||||
proc getPragmas(n: NimNode): HashSet[string] =
|
proc getPragmas(n: NimNode): HashSet[string] =
|
||||||
# Find all pragmas in AST, return as "name" or "name:value" in set
|
# Find all pragmas in AST, return as "name" or "name:value" in set
|
||||||
|
|
@ -355,9 +355,10 @@ testFields(fieldfuncfunc,
|
||||||
|
|
||||||
assert func2 is proc (f1: cint; sfunc2: proc (f1: cint; ssfunc2: proc (f1: cint): ptr cint {.cdecl.}): ptr cint {.cdecl.}): ptr cint {.cdecl.}
|
assert func2 is proc (f1: cint; sfunc2: proc (f1: cint; ssfunc2: proc (f1: cint): ptr cint {.cdecl.}): ptr cint {.cdecl.}): ptr cint {.cdecl.}
|
||||||
|
|
||||||
assert BASS_DEVICEINFO is object
|
# Test --replace VICE=SLICE
|
||||||
testFields(BASS_DEVICEINFO, "name|driver|flags!cstring|cstring|cint")
|
assert BASS_DESLICEINFO is object
|
||||||
checkPragmas(BASS_DEVICEINFO, pHeaderImpBy)
|
testFields(BASS_DESLICEINFO, "name|driver|flags!cstring|cstring|cint")
|
||||||
|
checkPragmas(BASS_DESLICEINFO, pHeaderImpBy)
|
||||||
|
|
||||||
# Issue #183
|
# Issue #183
|
||||||
assert GPU_Target is object
|
assert GPU_Target is object
|
||||||
|
|
@ -419,4 +420,8 @@ assert NEV7 == 65
|
||||||
|
|
||||||
assert nested is object
|
assert nested is object
|
||||||
testFields(nested, "f1|f2|f3|f4|f5|f6|f7|f8!NT1|Type_tast2h1|NT3|Type_tast2h3|NU2|Union_tast2h1|NE1|Enum_tast2h2")
|
testFields(nested, "f1|f2|f3|f4|f5|f6|f7|f8!NT1|Type_tast2h1|NT3|Type_tast2h3|NU2|Union_tast2h1|NE1|Enum_tast2h2")
|
||||||
checkPragmas(nested, pHeaderImpBy)
|
checkPragmas(nested, pHeaderImpBy)
|
||||||
|
|
||||||
|
when defined(HEADER):
|
||||||
|
assert sitest1(5) == 10
|
||||||
|
assert sitest1(10) == 20
|
||||||
Loading…
Add table
Add a link
Reference in a new issue