\r
#include "repr.h"\r
\r
+// Transform ANTLR-generated types to corresponding IR types recursively\r
+\r
Type getType(TocParser::TypeContext * ctx);\r
Variable getVariable(TocParser::VarContext * ctx);\r
Body getBody(TocParser::BodyContext * ctx, std::shared_ptr<Context> parent);\r
\r
Stmt getStmt(TocParser::StmtContext * ctx, std::shared_ptr<Context> parent);\r
\r
+// all of these functions get the relevant information\r
+// from the parse tree and call each other for sub expressions\r
+// the getVariable is called for variable declarations and parameter definitions\r
+// for example, because they have the same rule in the grammar file\r
+\r
Type getType(TocParser::TypeContext * ctx)\r
{\r
Type result;\r
isStaticArray ? atoi(m->INT_LIT()->toString().c_str()) : -1\r
);\r
}\r
+ if (ctx->genericInstantiation() != nullptr)\r
+ {\r
+ for (auto g : ctx->genericInstantiation()->type())\r
+ {\r
+ result.genericInstantiation.push_back(getType(g));\r
+ }\r
+ }\r
return result;\r
}\r
Variable getVariable(TocParser::VarContext * ctx)\r
Function result;\r
result.name = ctx->funcName()->NAME()->toString();\r
result.returnType = getType(ctx->type());\r
+ if (ctx->genericDecl() != nullptr)\r
+ {\r
+ for (auto t : ctx->genericDecl()->typeName())\r
+ {\r
+ result.genericTypeNames.push_back(t->getText());\r
+ }\r
+ }\r
+\r
if (!ctx->parameter()->var().empty())\r
{\r
for (auto p : ctx->parameter()->var())\r
result.parameters.push_back(getVariable(p));\r
}\r
+\r
if (ctx->body() != nullptr)\r
{\r
result.body = getBody(ctx->body(), parent);\r
{\r
Struct result;\r
result.name = ctx->structName()->NAME()->toString();\r
+ if (ctx->genericDecl() != nullptr)\r
+ {\r
+ for (auto t : ctx->genericDecl()->typeName())\r
+ {\r
+ result.genericTypeNames.push_back(t->getText());\r
+ }\r
+ }\r
+\r
for (auto m : ctx->structMember())\r
{\r
if (m->structVar() != nullptr)\r
Namespace result;\r
result.ctx = std::make_unique<Context>();\r
result.name = ctx->typeName()->getText();\r
+ result.ctx->name = result.name;\r
for (auto d : ctx->decl())\r
{\r
if (d->varDecl() != nullptr)\r
}\r
if (d->funcDecl() != nullptr)\r
{\r
- result.functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
+ result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
}\r
if (d->structDecl() != nullptr)\r
{\r
- result.structs.push_back(getStruct(d->structDecl(), result.ctx));\r
+ result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));\r
}\r
if (d->namespaceDecl() != nullptr)\r
{\r
- result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
+ result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
}\r
}\r
return result;\r
}\r
if (d->funcDecl() != nullptr)\r
{\r
- result.functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
+ result.ctx->functions.push_back(getFunction(d->funcDecl()->func(), result.ctx));\r
}\r
if (d->structDecl() != nullptr)\r
{\r
- result.structs.push_back(getStruct(d->structDecl(), result.ctx));\r
+ result.ctx->structs.push_back(getStruct(d->structDecl(), result.ctx));\r
}\r
if (d->namespaceDecl() != nullptr)\r
{\r
- result.namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
+ result.ctx->namespaces.push_back(getNamespace(d->namespaceDecl(), result.ctx));\r
}\r
}\r
return result;\r
\r
\r
\r
-\r
+// Expressions are somewhat of an exception, because some of their\r
+// grammar rules are recursive, so they have to be defined\r
+// in a single rule using Labels (https://github.com/antlr/antlr4/blob/master/doc/parser-rules.md#alternative-labels)\r
+// Because this results in a polymorphic type, getExpr for the base expression type\r
+// is always called and from there the polymorphic type is determined at runtime\r
Expr getExpr(TocParser::FuncExprContext * ctx)\r
{\r
Expr result;\r
for (auto n : ctx->namespaceSpecifier())\r
result._func.namespacePrefixes.push_back(n->typeName()->getText());\r
result._func.functionName = ctx->funcName()->NAME()->toString();\r
+ if (ctx->genericInstantiation() != nullptr)\r
+ {\r
+ for (auto g : ctx->genericInstantiation()->type())\r
+ {\r
+ result._func.genericInstantiation.push_back(getType(g));\r
+ }\r
+ }\r
for (auto e : ctx->expr())\r
result._func.arguments.push_back(getExpr(e));\r
return result;\r
result.type = ExprType::Method;\r
result._method.expr = std::make_unique<Expr>(getExpr(ctx->expr(0)));\r
result._method.methodName = ctx->funcName()->NAME()->toString();\r
+ if (ctx->genericInstantiation() != nullptr)\r
+ {\r
+ for (auto g : ctx->genericInstantiation()->type())\r
+ {\r
+ result._method.genericInstantiation.push_back(getType(g));\r
+ }\r
+ }\r
for (int i = 1; i < ctx->expr().size(); i++)\r
result._method.arguments.push_back(getExpr(ctx->expr(i)));\r
return result;\r
result.type = ExprType::Dot;\r
result._dot.expr = std::make_unique<Expr>(getExpr(ctx->expr()));\r
result._dot.identifier = ctx->varName()->getText();\r
+ result._dot.isPointer = ctx->arrow() != nullptr;\r
return result;\r
}\r
Expr getExpr(TocParser::PrefixOpExprContext * ctx)\r
\r
\r
\r
-\r
+// this is always called for Expression rules\r
+// attempt dynamic_cast at runtime and call corresponding\r
+// function\r
Expr getExpr(TocParser::ExprContext * ctx)\r
{\r
Expr result;\r