diff ast/Exp.d @ 88:eb5b2c719a39 new_gen

Major change to locations, tokens and expressions. A location (now SourceLocation or SLoc) is only 32 bit in size - disadvantage is that it can't find its own text. You have to go through the new SourceManager to do that. This has caused changes to a lot of stuff and removal of DataSource and the old Location Additionally Exp has gotten some location stuff, so we can give proper error messages. Not in Decl and Stmt yet, but thats coming too.
author Anders Halager <halager@gmail.com>
date Sun, 04 May 2008 18:13:46 +0200
parents 29f486ccc203
children 771ac63898e2
line wrap: on
line diff
--- a/ast/Exp.d	Sun May 04 12:58:02 2008 +0200
+++ b/ast/Exp.d	Sun May 04 18:13:46 2008 +0200
@@ -29,17 +29,36 @@
 
 class Exp
 {
-    this(ExpType expType) 
+    this(ExpType expType, SLoc loc)
     {
         this.expType = expType;
+        this.loc = loc;
     }
 
+    /// Get the type of the expression
     DType type() { return null; }
 
+    /// Indicates which type the expression is - to avoid a lot of casts
     ExpType expType;
+
+    /// The environment of the expression
     Scope env;
+
     int stmtIndex;
 
+    /**
+      The "main" location of the expression.
+      What exactly this represents varies but for most things its the start
+      while for a binary expression its the operator.
+     **/
+    SourceLocation loc;
+
+    /// Return the starting location of this expression
+    SourceLocation startLoc() { return loc; }
+
+    /// Get the full extents of the expression
+    SourceRange sourceRange() { return SourceRange(loc, loc + 1); }
+
     Exp simplify()
     {
         return this;
@@ -50,7 +69,7 @@
 {
     this(Exp exp, Exp[] args)
     {
-        super(ExpType.CallExp);
+        super(ExpType.CallExp, exp.loc);
         this.exp = exp;
         this.args = args;
     }
@@ -66,6 +85,13 @@
     Exp[] args;
     bool sret = false;
 
+    override SourceRange sourceRange()
+    {
+        SourceRange res = exp.sourceRange;
+        if (args.length > 0)
+            res = res + args[$ - 1].sourceRange;
+        return res;
+    }
 
     Exp simplify()
     {
@@ -113,12 +139,13 @@
 
 class AssignExp : Exp
 {
-    this(Exp identifier, Exp exp)
+    this(SLoc op, Exp identifier, Exp exp)
     {
-        super(ExpType.AssignExp);
+        super(ExpType.AssignExp, op);
         this.identifier = identifier;
         this.exp = exp;
     }
+
     Exp simplify()
     {
         identifier = identifier.simplify;
@@ -127,6 +154,11 @@
         return this;
     }
 
+    override SourceRange sourceRange()
+    {
+        return identifier.sourceRange + exp.sourceRange;
+    }
+
     override DType type() { return identifier.type(); }
 
     Exp identifier;
@@ -150,9 +182,9 @@
 
     char[][] getOp = ["=","==","!=","<","<=",">",">=","+","-","*","/","%"];
 
-    this(Operator op, Exp left, Exp right)
+    this(SLoc op_loc, Operator op, Exp left, Exp right)
     {
-        super(ExpType.Binary);
+        super(ExpType.Binary, op_loc);
         this.op = op;
         this.left = left;
         this.right = right;
@@ -187,12 +219,20 @@
         return myType;
     }
 
+    override SLoc startLoc() { return left.startLoc(); }
+
+    override SourceRange sourceRange()
+    {
+        return left.sourceRange + right.sourceRange;
+    }
+
     char[] resultType()
     {
         if (op >= Operator.Eq && op <= Operator.Ge)
             return "bool";
         return null;
     }
+
     Exp simplify()
     {
         left = left.simplify;
@@ -207,9 +247,9 @@
 
 class NegateExp : Exp
 {
-    this(Exp exp)
+    this(SLoc op, Exp exp)
     {
-        super(ExpType.Negate);
+        super(ExpType.Negate, op);
         this.exp = exp;
     }
 
@@ -221,14 +261,19 @@
 
     override DType type() { return exp.type(); }
 
+    override SourceRange sourceRange()
+    {
+        return SourceRange(loc) + exp.sourceRange;
+    }
+
     public Exp exp;
 }
 
 class DerefExp : Exp
 {
-    this(Exp exp)
+    this(SLoc op, Exp exp)
     {
-        super(ExpType.Deref);
+        super(ExpType.Deref, op);
         this.exp = exp;
     }
 
@@ -243,22 +288,28 @@
         return exp.type().asPointer.pointerOf; 
     }
 
+    override SourceRange sourceRange()
+    {
+        return SourceRange(loc) + exp.sourceRange;
+    }
+
     public Exp exp;
 }
 
 class IntegerLit : Exp
 {
-    this(Token t)
+    this(SLoc loc, char[] t)
     {
-        super(ExpType.IntegerLit);
-        this.token = t;
-        this.name = substitute(t.get,"_","");
+        super(ExpType.IntegerLit, loc);
+        range = SourceRange(loc, loc + t.length);
+        this.name = substitute(t, "_", "");
     }
 
     char[] get()
     {
         return name;
     }
+
     Exp simplify()
     {
         return this;
@@ -266,20 +317,24 @@
 
     override DType type() { return DType.Int; }
 
-    
+    override SourceRange sourceRange()
+    {
+        return range;
+    }
 
-    Token token;
     char[] name;
+    private SourceRange range;
 }
 
 class MemberReference : Exp
 {
-    this(Exp target, Identifier child)
+    this(SLoc dot, Exp target, Identifier child)
     {
-        super(ExpType.MemberReference);
+        super(ExpType.MemberReference, dot);
         this.target = target;
         this.child = child;
     }
+
     Exp simplify()
     {
         target = target.simplify;
@@ -293,7 +348,7 @@
 
         DStruct st = cast(DStruct)target.type;
         assert(st, "Only structs have members");
-        if (auto t = st.typeOf(child.token.get))
+        if (auto t = st.typeOf(child.name))
             myType = t;
         // no error reporting here
         else assert(0, "Referencing non-existant member");
@@ -301,6 +356,13 @@
         return myType;
     }
 
+    override SLoc startLoc() { return target.startLoc(); }
+
+    override SourceRange sourceRange()
+    {
+        return target.sourceRange + child.sourceRange;
+    }
+
     Identifier child;
     Exp target;
     private DType myType;
@@ -308,11 +370,13 @@
 
 class IndexExp : Exp
 {
-    this(Exp target, Exp index)
+    this(Exp target, SLoc left_bracket, Exp index, SLoc right_bracket)
     {
-        super(ExpType.Index);
+        super(ExpType.Index, target.startLoc);
         this.target = target;
+        this.left_bracket = left_bracket;
         this.index = index;
+        this.right_bracket = right_bracket;
     }
 
     override DType type()
@@ -325,6 +389,11 @@
         else assert(0, "Can only index pointers and arrays");
     }
 
+    override SourceRange sourceRange()
+    {
+        return target.sourceRange + SourceRange(right_bracket);
+    }
+
     Exp simplify()
     {
         target = target.simplify;
@@ -334,13 +403,14 @@
 
     Exp target;
     Exp index;
+    SLoc left_bracket, right_bracket;
 }
 
 class CastExp : Exp
 {
-    this(Identifier castType, Exp exp)
+    this(SLoc loc, Identifier castType, Exp exp)
     {
-        super(ExpType.CastExp);
+        super(ExpType.CastExp, loc);
         this.castType = castType;
         this.exp = exp;
     }
@@ -357,6 +427,11 @@
         return this;
     }
 
+    override SourceRange sourceRange()
+    {
+        return SourceRange(loc) + exp.sourceRange;
+    }
+
     Identifier castType;
     Exp exp;
 }
@@ -365,7 +440,7 @@
 {
     this(Identifier pointerOf)
     {
-        super(ExpType.PointerIdentifier);
+        super(ExpType.PointerIdentifier, pointerOf.loc);
         this.pointerOf = pointerOf;
         this.name = pointerOf.name;
     }
@@ -382,7 +457,7 @@
 {
     this(Identifier arrayOf, IntegerLit size)
     {
-        super(ExpType.ArrayIdentifier);
+        super(ExpType.ArrayIdentifier, arrayOf.loc);
         this.arrayOf = arrayOf;
         this.size = Integer.parse(size.get);
         this.name = arrayOf.name;
@@ -401,16 +476,15 @@
 
 class Identifier : Exp
 {
-    this(Token t)
+    this(SLoc loc, char[] name)
     {
-        super(ExpType.Identifier);
-        this.token = t;
-        name = t.get;
+        super(ExpType.Identifier, loc);
+        this.name = name;
     }
 
-    protected this(ExpType t)
+    protected this(ExpType t, SLoc loc)
     {
-        super(t);
+        super(t, loc);
     }
 
     override DType type()
@@ -423,7 +497,7 @@
 
     this(char[] name)
     {
-        super(ExpType.Identifier);
+        super(ExpType.Identifier, SLoc.Invalid);
         this.name = name;
     }
 
@@ -456,7 +530,6 @@
         return this;
     }
 
-    Token token;
     char[] name;
     private DType myType;
 }