Support templates in function arguments

This commit is contained in:
Doyle 2019-03-21 00:52:31 +11:00
parent f2cbbc3a8c
commit 2f750ecc52
3 changed files with 32 additions and 14 deletions

View File

@ -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 // CPP Parsing Helpers
// //
@ -991,7 +1004,7 @@ b32 ParseCPPVariableType(CPPTokeniser *tokeniser, StringLiteral *type)
if (template_depth == 0) if (template_depth == 0)
{ {
char *expr_start = peek_token.str + 1; char *expr_start = peek_token.str + 1;
char *expr_end = token.str - 1; char *expr_end = token.str;
int expr_len = static_cast<int>(expr_end - expr_start); int expr_len = static_cast<int>(expr_end - expr_start);
template_expr.str = expr_start; template_expr.str = expr_start;
@ -1041,11 +1054,10 @@ CPPDeclLinkedList<CPPVariableDecl> *ParseCPPTypeAndVariableDecl(CPPTokeniser *to
CPPToken variable_type = token; CPPToken variable_type = token;
for (int total_asterisks_count = 0;;) for (int total_asterisks_count = 0;;)
{ {
CPPToken peek_token = CPPTokeniser_PeekToken(tokeniser); CPPToken peek_token = {};
StringLiteral variable_template_expr = {}; 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; int template_depth = 1;
while (template_depth != 0 && token.type != CPPTokenType::EndOfStream) while (template_depth != 0 && token.type != CPPTokenType::EndOfStream)
{ {
@ -1059,7 +1071,7 @@ CPPDeclLinkedList<CPPVariableDecl> *ParseCPPTypeAndVariableDecl(CPPTokeniser *to
if (template_depth == 0) if (template_depth == 0)
{ {
char *expr_start = peek_token.str + 1; char *expr_start = peek_token.str + 1;
char *expr_end = token.str - 1; char *expr_end = token.str;
int expr_len = static_cast<int>(expr_end - expr_start); int expr_len = static_cast<int>(expr_end - expr_start);
variable_template_expr.str = expr_start; variable_template_expr.str = expr_start;
@ -1438,15 +1450,21 @@ void ParseCPPInspectPrototype(CPPTokeniser *tokeniser)
for (CPPDeclLinkedList<CPPVariableDecl> *param_link = param_list; param_link; param_link = param_link->next) for (CPPDeclLinkedList<CPPVariableDecl> *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 // TODO(doyle): HACK. We should parse ptrs into the CPPVariableDecl, fixed size arrays into the name and const-ness into the type
CPPVariableDecl *decl = &param_link->value; CPPVariableDecl *decl = &param_link->value;
StringLiteral *type = &decl->type; StringLiteral *type = &decl->type;
StringLiteral *name = &decl->name; 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; char *name_end = name->str + name->len;
auto hack_decl_name = StringLiteral(name_start, static_cast<int>(name_end - name_start)); auto hack_decl_name = StringLiteral(name_start, static_cast<int>(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) if (decl->default_value.len > 0)
CPPTokeniser_SprintfToFileNoIndenting(tokeniser, " = %.*s", decl->default_value.len, decl->default_value.str); CPPTokeniser_SprintfToFileNoIndenting(tokeniser, " = %.*s", decl->default_value.len, decl->default_value.str);

View File

@ -40,4 +40,4 @@ DQN_INSPECT_GENERATE_PROTOTYPE()
void *Function2() { } void *Function2() { }
DQN_INSPECT_GENERATE_PROTOTYPE() DQN_INSPECT_GENERATE_PROTOTYPE()
Array<int const *, 3> const *const Function3() { } Array<int const *, 3> const *const Function3(Array<int, 32> const *foobar) { }

View File

@ -60,13 +60,13 @@ DqnInspect_StructMember const DqnInspect_SampleStruct_StructMembers[] =
{ {
{ {
STR_AND_LEN("Array"), STR_AND_LEN("lights"), 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 nullptr, 0, // metadata and metadata_len
0 // array_dimensions 0 // array_dimensions
}, },
{ {
STR_AND_LEN("Array"), STR_AND_LEN("camera_matrixes"), 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 nullptr, 0, // metadata and metadata_len
0 // array_dimensions 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 Function1(int a, float b = {}, char const *c = nullptr, bool e = false, int f = 1, char *g = "Hello world");
void *Function2(); void *Function2();
Array<int const *, 3> const *const Function3(); Array<int const *, 3> const *const Function3(Array<int, 32> const *foobar);
#endif // DQN_INSPECT_DQNINSPECT_TESTDATA_H #endif // DQN_INSPECT_DQNINSPECT_TESTDATA_H