Skip to content

Commit

Permalink
[ast] introduce Identifier type to AST
Browse files Browse the repository at this point in the history
Identifiers are the building block for enums and defines. With
Identifiers, we also have better error message for undefined names.
Instead of:

```
16.26: syntax error, unexpected ), expecting (
```

We get:

```
Unknown identifier: 'TASK_NEW'
```
  • Loading branch information
mmarchini committed Apr 12, 2019
1 parent 179af52 commit 389d55f
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ast/ast.cpp
Expand Up @@ -21,6 +21,10 @@ void Builtin::accept(Visitor &v) {
v.visit(*this);
}

void Identifier::accept(Visitor &v) {
v.visit(*this);
}

void PositionalParameter::accept(Visitor &v) {
v.visit(*this);
}
Expand Down
9 changes: 9 additions & 0 deletions src/ast/ast.h
Expand Up @@ -63,6 +63,14 @@ class StackMode : public Expression {
void accept(Visitor &v) override;
};

class Identifier : public Expression {
public:
explicit Identifier(std::string ident) : ident(ident) {}
std::string ident;

void accept(Visitor &v) override;
};

class Builtin : public Expression {
public:
explicit Builtin(std::string ident) : ident(is_deprecated(ident)) {}
Expand Down Expand Up @@ -288,6 +296,7 @@ class Visitor {
virtual void visit(PositionalParameter &integer) = 0;
virtual void visit(String &string) = 0;
virtual void visit(Builtin &builtin) = 0;
virtual void visit(Identifier &identifier) = 0;
virtual void visit(StackMode &mode) = 0;
virtual void visit(Call &call) = 0;
virtual void visit(Map &map) = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/ast/codegen_llvm.cpp
Expand Up @@ -45,6 +45,12 @@ void CodegenLLVM::visit(String &string)
expr_ = buf;
}

void CodegenLLVM::visit(Identifier &identifier)
{
std::cerr << "unknown identifier \"" << identifier.ident << "\"" << std::endl;
abort();
}

void CodegenLLVM::visit(Builtin &builtin)
{
if (builtin.ident == "nsecs")
Expand Down
1 change: 1 addition & 0 deletions src/ast/codegen_llvm.h
Expand Up @@ -30,6 +30,7 @@ class CodegenLLVM : public Visitor {
void visit(Integer &integer) override;
void visit(PositionalParameter &param) override;
void visit(String &string) override;
void visit(Identifier &identifier) override;
void visit(Builtin &builtin) override;
void visit(StackMode &) override { };
void visit(Call &call) override;
Expand Down
6 changes: 6 additions & 0 deletions src/ast/printer.cpp
Expand Up @@ -43,6 +43,12 @@ void Printer::visit(Builtin &builtin)
out_ << indent << "builtin: " << builtin.ident << std::endl;
}

void Printer::visit(Identifier &identifier)
{
std::string indent(depth_, ' ');
out_ << indent << "identifier: " << identifier.ident << std::endl;
}

void Printer::visit(Call &call)
{
std::string indent(depth_, ' ');
Expand Down
1 change: 1 addition & 0 deletions src/ast/printer.h
Expand Up @@ -14,6 +14,7 @@ class Printer : public Visitor {
void visit(PositionalParameter &param) override;
void visit(String &string) override;
void visit(StackMode &mode) override;
void visit(Identifier &identifier) override;
void visit(Builtin &builtin) override;
void visit(Call &call) override;
void visit(Map &map) override;
Expand Down
6 changes: 6 additions & 0 deletions src/ast/semantic_analyser.cpp
Expand Up @@ -59,6 +59,12 @@ void SemanticAnalyser::visit(StackMode &mode)
}
}

void SemanticAnalyser::visit(Identifier &identifier)
{
identifier.type = SizedType(Type::none, 0);
err_ << "Unknown identifier: '" << identifier.ident << "'" << std::endl;
}

void SemanticAnalyser::visit(Builtin &builtin)
{
if (builtin.ident == "nsecs" ||
Expand Down
1 change: 1 addition & 0 deletions src/ast/semantic_analyser.h
Expand Up @@ -23,6 +23,7 @@ class SemanticAnalyser : public Visitor {
void visit(PositionalParameter &param) override;
void visit(String &string) override;
void visit(StackMode &mode) override;
void visit(Identifier &identifier) override;
void visit(Builtin &builtin) override;
void visit(Call &call) override;
void visit(Map &map) override;
Expand Down
1 change: 1 addition & 0 deletions src/parser.yy
Expand Up @@ -203,6 +203,7 @@ stmt : expr { $$ = new ast::ExprStatement($1); }
expr : INT { $$ = new ast::Integer($1); }
| STRING { $$ = new ast::String($1); }
| BUILTIN { $$ = new ast::Builtin($1); }
| IDENT { $$ = new ast::Identifier($1); }
| STACK_MODE { $$ = new ast::StackMode($1); }
| ternary { $$ = $1; }
| param { $$ = $1; }
Expand Down
1 change: 1 addition & 0 deletions src/tracepoint_format_parser.h
Expand Up @@ -17,6 +17,7 @@ class TracepointArgsVisitor : public Visitor
void visit(__attribute__((unused)) PositionalParameter &integer) override { }; // Leaf
void visit(__attribute__((unused)) String &string) override { }; // Leaf
void visit(__attribute__((unused)) StackMode &mode) override { }; // Leaf
void visit(__attribute__((unused)) Identifier &identifier) override { }; // Leaf
void visit(Builtin &builtin) override { // Leaf
if (builtin.ident == "args")
probe_->need_tp_args_structs = true;
Expand Down

0 comments on commit 389d55f

Please sign in to comment.