Compare commits

...
Sign in to create a new pull request.

8 commits

Author SHA1 Message Date
Joey Yakimowich-Payne
98beef2997 Rename currentExprSkipIdentValidation -> skipIdentValidation 2020-05-14 17:23:43 -06:00
Joey Yakimowich-Payne
8962a85152 Cleanup code 2020-05-13 22:36:40 -06:00
Joey Yakimowich-Payne
3bbdbeb870 Preserve type array ast 2020-05-13 22:19:37 -06:00
Joey Yakimowich-Payne
ec5ba997b7 Get rid of extra var 2020-05-13 22:04:05 -06:00
Joey Yakimowich-Payne
0deb2939bf Make undefined constants keep types 2020-05-13 22:04:05 -06:00
Joey Yakimowich-Payne
485770dfaf Disable windows check 2020-05-13 22:04:05 -06:00
Joey Yakimowich-Payne
97bdff30e5 Change to static const 2020-05-13 22:04:05 -06:00
Joey Yakimowich-Payne
d40a931232 Fix undefined identifiers resulting in incorrect types 2020-05-13 22:04:05 -06:00
6 changed files with 39 additions and 12 deletions

View file

@ -951,10 +951,10 @@ proc getTypeArray(gState: State, node: TSNode, tident: PNode, name: string): PNo
# type name[X] => array[X, type]
let
# Size of array could be a Nim expression
size = gState.parseCExpression(gState.getNodeVal(cnode[1]))
if size.kind != nkNone:
result = gState.newArrayTree(cnode, result, size)
cnode = cnode[0]
size = gState.parseCExpression(gState.getNodeVal(cnode[1]), skipIdentValidation = true)
result = gState.newArrayTree(cnode, result, size)
cnode = cnode[0]
elif cnode.len == 1:
# type name[] = UncheckedArray[type]
result = gState.newArrayTree(cnode, result)

View file

@ -45,14 +45,14 @@ proc getExprIdent*(gState: State, identName: string, kind = nskConst, parent = "
##
## Returns PNode(nkNone) if the identifier is blank
result = newNode(nkNone)
if identName notin gState.skippedSyms:
if gState.skipIdentValidation or identName notin gState.skippedSyms:
var ident = identName
if ident != "_":
# Process the identifier through cPlugin
ident = gState.getIdentifier(ident, kind, parent)
if kind == nskType:
result = gState.getIdent(ident)
elif ident.nBl and ident in gState.constIdentifiers:
elif gState.skipIdentValidation or ident.nBl and ident in gState.constIdentifiers:
if gState.currentTyCastName.nBl:
ident = ident & "." & gState.currentTyCastName
result = gState.getIdent(ident)
@ -591,7 +591,7 @@ proc processTSNode(gState: State, node: TSNode, typeofNode: var PNode): PNode =
decho "NODE RESULT: ", result
proc parseCExpression*(gState: State, codeRoot: TSNode, name = ""): PNode =
proc parseCExpression*(gState: State, codeRoot: TSNode): PNode =
## Parse a c expression from a root ts node
# This var is used for keeping track of the type of the first
@ -607,14 +607,16 @@ proc parseCExpression*(gState: State, codeRoot: TSNode, name = ""): PNode =
decho "UNEXPECTED EXCEPTION: ", e.msg
result = newNode(nkNone)
proc parseCExpression*(gState: State, code: string, name = ""): PNode =
proc parseCExpression*(gState: State, code: string, name = "", skipIdentValidation = false): PNode =
## Convert the C string to a nim PNode tree
gState.currentExpr = code
gState.currentTyCastName = name
gState.skipIdentValidation = skipIdentValidation
withCodeAst(gState.currentExpr, gState.mode):
result = gState.parseCExpression(root, name)
result = gState.parseCExpression(root)
# Clear the state
gState.currentExpr = ""
gState.currentTyCastName = ""
gState.currentTyCastName = ""
gState.skipIdentValidation = false

View file

@ -114,6 +114,9 @@ type
# Used for the exprparser.nim module
currentExpr*, currentTyCastName*: string
# Controls whether or not the current expression
# should validate idents against currently defined idents
skipIdentValidation*: bool
# Legacy AST fields, remove when ast2 becomes default
constStr*, enumStr*, procStr*, typeStr*: string

View file

@ -4,6 +4,8 @@ import "."/treesitter/[api, c, cpp]
import "."/[ast, ast2, build, globals, getters, grammar, tshelp]
{.passC: "-DNIMTEROP".}
proc process(gState: State, path: string, astTable: AstTable) =
doAssert existsFile(path), &"Invalid path {path}"

View file

@ -42,13 +42,29 @@ extern "C" {
#define REG_STR "regular string"
#define NOTSUPPORTEDSTR "not a " REG_STR
#define NULLCHAR '\0'/* comments should not break things*/
#define OCTCHAR '\012' // nor should this comment
#define NULLCHAR '\0'
#define OCTCHAR '\012'
#define HEXCHAR '\xFE'
#define TRICKYSTR "\x4E\034\nfoo\0\'\"\r\v\a\b\e\f\t\\\?bar"
#define ALLSHL (SHL1 | SHL2 | SHL3)
#ifdef NIMTEROP
#define SOME_CONST 8
#endif
struct some_struct_s
{
int x;
};
struct parent_struct_s
{
struct some_struct_s s[SOME_CONST];
};
typedef struct some_struct_s SOME_ARRAY[SOME_CONST];
struct A0;
struct A1 {};
typedef struct A2;

View file

@ -35,6 +35,7 @@ cOverride:
type
A1* = A0
cDefine("SOME_CONST=100")
cImport(path, flags="-f:ast2 -ENK_,SDL_ -GVICE=SLICE -TMyInt=cint" & flags)
proc getPragmas(n: NimNode): HashSet[string] =
@ -155,6 +156,9 @@ assert typeof(POINTERPOINTERPOINTEREXPR) is (ptr ptr ptr cint)
assert ALLSHL == (SHL1 or SHL2 or SHL3)
assert typeof(parent_struct_s().s) is array[100, some_struct_s]
assert typeof(SOME_ARRAY) is array[100, some_struct_s]
assert A0 is object
testFields(A0, "f1!cint")
checkPragmas(A0, pHeaderBy, istype = false)