From 2f750ecc52b7113c1db3891c09c7de61f92ef1ae Mon Sep 17 00:00:00 2001 From: Doyle Date: Thu, 21 Mar 2019 00:52:31 +1100 Subject: [PATCH] Support templates in function arguments --- Code/DqnInspect.h | 38 ++++++++++++++++++++------- Data/DqnInspect_TestData.h | 2 +- Data/DqnInspect_TestDataGenerated.cpp | 6 ++--- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/Code/DqnInspect.h b/Code/DqnInspect.h index b2afd92..b741888 100644 --- a/Code/DqnInspect.h +++ b/Code/DqnInspect.h @@ -644,6 +644,19 @@ void CPPTokeniser_SkipToIndentLevel(CPPTokeniser *tokeniser, int indent_level) } } +bool CPPTokeniser_AcceptTokenIfType(CPPTokeniser *tokeniser, CPPTokenType type, CPPToken *token) +{ + CPPToken check = CPPTokeniser_PeekToken(tokeniser); + bool result = (check.type == type); + if (result && token) + { + CPPTokeniser_NextToken(tokeniser); + *token = check; + } + + return result; +} + // // CPP Parsing Helpers // @@ -991,7 +1004,7 @@ b32 ParseCPPVariableType(CPPTokeniser *tokeniser, StringLiteral *type) if (template_depth == 0) { char *expr_start = peek_token.str + 1; - char *expr_end = token.str - 1; + char *expr_end = token.str; int expr_len = static_cast(expr_end - expr_start); template_expr.str = expr_start; @@ -1041,11 +1054,10 @@ CPPDeclLinkedList *ParseCPPTypeAndVariableDecl(CPPTokeniser *to CPPToken variable_type = token; for (int total_asterisks_count = 0;;) { - CPPToken peek_token = CPPTokeniser_PeekToken(tokeniser); + CPPToken peek_token = {}; StringLiteral variable_template_expr = {}; - if (peek_token.type == CPPTokenType::LessThan) + if (CPPTokeniser_AcceptTokenIfType(tokeniser, CPPTokenType::LessThan, &peek_token)) { - token = CPPTokeniser_NextToken(tokeniser); int template_depth = 1; while (template_depth != 0 && token.type != CPPTokenType::EndOfStream) { @@ -1059,7 +1071,7 @@ CPPDeclLinkedList *ParseCPPTypeAndVariableDecl(CPPTokeniser *to if (template_depth == 0) { char *expr_start = peek_token.str + 1; - char *expr_end = token.str - 1; + char *expr_end = token.str; int expr_len = static_cast(expr_end - expr_start); variable_template_expr.str = expr_start; @@ -1438,15 +1450,21 @@ void ParseCPPInspectPrototype(CPPTokeniser *tokeniser) for (CPPDeclLinkedList *param_link = param_list; param_link; param_link = param_link->next) { // TODO(doyle): HACK. We should parse ptrs into the CPPVariableDecl, fixed size arrays into the name and const-ness into the type - CPPVariableDecl *decl = ¶m_link->value; - StringLiteral *type = &decl->type; - StringLiteral *name = &decl->name; + CPPVariableDecl *decl = ¶m_link->value; + StringLiteral *type = &decl->type; + char *type_end = (decl->template_expr.len > 0) ? decl->template_expr.str + decl->template_expr.len + 1 // +1 for the ending ">" on the template + : type->str + type->len; - char *name_start = type->str + (type->len + 1); + StringLiteral *name = &decl->name; + char *name_start = type_end + 1; char *name_end = name->str + name->len; auto hack_decl_name = StringLiteral(name_start, static_cast(name_end - name_start)); - CPPTokeniser_SprintfToFileNoIndenting(tokeniser, "%.*s %.*s", decl->type.len, decl->type.str, hack_decl_name.len, hack_decl_name.str); + CPPTokeniser_SprintfToFileNoIndenting(tokeniser, "%.*s", type->len, type->str); + if (decl->template_expr.len > 0) + CPPTokeniser_SprintfToFileNoIndenting(tokeniser, "<%.*s>", decl->template_expr.len, decl->template_expr.str); + CPPTokeniser_SprintfToFileNoIndenting(tokeniser, " %.*s", hack_decl_name.len, hack_decl_name.str); + if (decl->default_value.len > 0) CPPTokeniser_SprintfToFileNoIndenting(tokeniser, " = %.*s", decl->default_value.len, decl->default_value.str); diff --git a/Data/DqnInspect_TestData.h b/Data/DqnInspect_TestData.h index 3cbfa8e..391a5e0 100644 --- a/Data/DqnInspect_TestData.h +++ b/Data/DqnInspect_TestData.h @@ -40,4 +40,4 @@ DQN_INSPECT_GENERATE_PROTOTYPE() void *Function2() { } DQN_INSPECT_GENERATE_PROTOTYPE() -Array const *const Function3() { } +Array const *const Function3(Array const *foobar) { } diff --git a/Data/DqnInspect_TestDataGenerated.cpp b/Data/DqnInspect_TestDataGenerated.cpp index 7512bbb..52cdaed 100644 --- a/Data/DqnInspect_TestDataGenerated.cpp +++ b/Data/DqnInspect_TestDataGenerated.cpp @@ -60,13 +60,13 @@ DqnInspect_StructMember const DqnInspect_SampleStruct_StructMembers[] = { { STR_AND_LEN("Array"), STR_AND_LEN("lights"), - STR_AND_LEN("V3, 3"), // template_expr + STR_AND_LEN("V3, 32"), // template_expr nullptr, 0, // metadata and metadata_len 0 // array_dimensions }, { STR_AND_LEN("Array"), STR_AND_LEN("camera_matrixes"), - STR_AND_LEN("V4, 3"), // template_expr + STR_AND_LEN("V4, 32"), // template_expr nullptr, 0, // metadata and metadata_len 0 // array_dimensions }, @@ -226,7 +226,7 @@ DqnInspect_Struct const *DqnInspect_GetStruct(SampleStruct const *val) void Function1(int a, float b = {}, char const *c = nullptr, bool e = false, int f = 1, char *g = "Hello world"); void *Function2(); -Array const *const Function3(); +Array const *const Function3(Array const *foobar); #endif // DQN_INSPECT_DQNINSPECT_TESTDATA_H