Skip to content

Commit

Permalink
cc: analyzer: store function return type
Browse files Browse the repository at this point in the history
  • Loading branch information
atirut-w committed Jul 29, 2024
1 parent b4de0bd commit 0f5b771
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/cc/analyzer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "CParser.h"
#include "types.hpp"
#include <analyzer.hpp>
#include <any>

Expand All @@ -14,12 +15,14 @@ any Analyzer::visitCompilationUnit(CParser::CompilationUnitContext *ctx)
any Analyzer::visitFunctionDefinition(CParser::FunctionDefinitionContext *ctx)
{
string name = ctx->declarator()->directDeclarator()->directDeclarator()->Identifier()->getText();
string return_type = ctx->declarationSpecifiers()->declarationSpecifier()[0]->typeSpecifier()->getText();

if (meta.functions.find(name) == meta.functions.end())
{
meta.functions[name] = FunctionMeta();
}
current_function = &meta.functions[name];
current_function->return_type = &primitives[return_type];

if (auto *itemlist_ctx = ctx->compoundStatement()->blockItemList())
{
Expand Down
2 changes: 2 additions & 0 deletions src/cc/include/analyzer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <CBaseVisitor.h>
#include <map>
#include <string>
#include <types.hpp>

struct SymbolMeta
{
Expand All @@ -17,6 +18,7 @@ struct LocalMeta

struct FunctionMeta
{
Type *return_type;
// Offset into the local frame for local variables
std::map<std::string, LocalMeta> variables;
int local_alloc = 0;
Expand Down

0 comments on commit 0f5b771

Please sign in to comment.