Add location traits to AST nodes.

This way, we keep trace of the exact source location where a
statement or symbol was defined and we are able to emit
more precise error messages in LLVM mode.
This commit is contained in:
Stefano Sanfilippo 2015-03-26 14:43:42 +01:00
parent 5c05d96b39
commit 7c5b760a94

View File

@ -23,6 +23,8 @@
#include "Emitter.hpp" #include "Emitter.hpp"
#include "Pointers.hpp" #include "Pointers.hpp"
#include "location.hh"
#include <functional> #include <functional>
#include <unordered_set> #include <unordered_set>
#include <boost/optional.hpp> #include <boost/optional.hpp>
@ -52,20 +54,34 @@ enum class Operator {
std::ostream& operator<<(std::ostream&, Operator const&); std::ostream& operator<<(std::ostream&, Operator const&);
class Emittable { class Localizable {
public:
void setLocation(location const& l) {
loc = l;
}
location const& getLocation() const {
return loc;
}
private:
location loc;
};
class Emittable: public Localizable {
public: public:
virtual ~Emittable() {} virtual ~Emittable() {}
virtual bool emit(Emitter *emitter) const = 0; virtual bool emit(Emitter *emitter) const = 0;
}; };
class Statement: public Emittable {}; class Statement: virtual public Emittable {};
class Expression: public Emittable {}; class Expression: virtual public Emittable {};
class SimpleExpression: public Expression {}; class SimpleExpression: public Expression {};
class SemiExpression { class SemiExpression: public Localizable {
public: public:
SemiExpression(Operator op, Expression *l): op(op), left(l) {} SemiExpression(Operator op, Expression *l): op(op), left(l) {}
@ -321,7 +337,7 @@ private:
Pointer<PointerList<Expression>> args; Pointer<PointerList<Expression>> args;
}; };
class BranchCase { class BranchCase: public Localizable {
public: public:
BranchCase(SemiExpression *c, PointerList<Statement> *b): condition(c), body(b) {} BranchCase(SemiExpression *c, PointerList<Statement> *b): condition(c), body(b) {}
@ -341,7 +357,7 @@ private:
class Branch: public Statement { class Branch: public Statement {
public: public:
class Body { class Body: public Localizable {
public: public:
Body(PointerList<BranchCase> *c, PointerList<Statement> *e = nullptr): cases(c), els(e) {} Body(PointerList<BranchCase> *c, PointerList<Statement> *e = nullptr): cases(c), els(e) {}
@ -380,7 +396,7 @@ private:
Function *makeMain(PointerList<Statement> *body); Function *makeMain(PointerList<Statement> *body);
class FunArg { class FunArg: public Localizable {
public: public:
FunArg(Id *n, Type t, bool p): name(n), type(t), pointer(p) {} FunArg(Id *n, Type t, bool p): name(n), type(t), pointer(p) {}