From 7c5b760a94753fbd00d3d7cc4826fa26dced9700 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Thu, 26 Mar 2015 14:43:42 +0100 Subject: [PATCH] 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. --- src/Nodes.hpp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Nodes.hpp b/src/Nodes.hpp index f4b43d9..997852f 100644 --- a/src/Nodes.hpp +++ b/src/Nodes.hpp @@ -23,6 +23,8 @@ #include "Emitter.hpp" #include "Pointers.hpp" +#include "location.hh" + #include #include #include @@ -52,20 +54,34 @@ enum class Operator { 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: virtual ~Emittable() {} 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 SemiExpression { +class SemiExpression: public Localizable { public: SemiExpression(Operator op, Expression *l): op(op), left(l) {} @@ -321,7 +337,7 @@ private: Pointer> args; }; -class BranchCase { +class BranchCase: public Localizable { public: BranchCase(SemiExpression *c, PointerList *b): condition(c), body(b) {} @@ -341,7 +357,7 @@ private: class Branch: public Statement { public: - class Body { + class Body: public Localizable { public: Body(PointerList *c, PointerList *e = nullptr): cases(c), els(e) {} @@ -380,7 +396,7 @@ private: Function *makeMain(PointerList *body); -class FunArg { +class FunArg: public Localizable { public: FunArg(Id *n, Type t, bool p): name(n), type(t), pointer(p) {}