diff ast/Exp.d @ 129:ed815b31479b

Added a Symbol
author Anders Halager <halager@gmail.com>
date Sat, 21 Jun 2008 20:41:18 +0200
parents c3b24e7e8cf8
children 2be29b296081
line wrap: on
line diff
--- a/ast/Exp.d	Sat Jun 21 17:32:27 2008 +0200
+++ b/ast/Exp.d	Sat Jun 21 20:41:18 2008 +0200
@@ -1,6 +1,7 @@
 module ast.Exp;
 
-import tango.text.Util;
+import tango.text.Util,
+       Integer = tango.text.convert.Integer;
 import tango.io.Stdout;
 
 import ast.Decl,
@@ -9,6 +10,7 @@
 import lexer.Token;
 
 import sema.Scope,
+       sema.Symbol,
        sema.DType;
 
 import basic.LiteralParsing;
@@ -38,6 +40,23 @@
         this.loc = loc;
     }
 
+    /**
+      Get the fully qualified name for the expression (if it can be resolved to
+      one) - otherwise null is returned
+     **/
+    char[] getFQN() { return null; }
+
+    /// The same as getFQN, except that the name is mangled
+    char[] getMangledFQN() { return null; }
+
+    /**
+      Try to get the symbol the expression represents.
+
+      Returns null for most expressions as they don't represent any symbol.
+      Identifiers and member references can have a sensible value.
+     **/
+    Symbol getSymbol() { return null; }
+
     /// Get the type of the expression
     abstract DType type();
 
@@ -47,6 +66,8 @@
     /// The environment of the expression
     Scope env;
 
+    Symbol symbol;
+
     int stmtIndex;
 
     /**
@@ -96,6 +117,7 @@
 
     Exp simplify()
     {
+        /*
         if(auto t = type.asStruct)
         {
             DFunction func_t = cast(DFunction)exp.type();
@@ -134,6 +156,7 @@
 
             return i;
         }
+        */
         return this;
     }
 }
@@ -303,7 +326,7 @@
 
     override DType type() 
     {
-        return exp.type().asPointer.pointerOf; 
+        return exp.type().asPointer().pointerOf; 
     }
 
     override SourceRange sourceRange()
@@ -370,6 +393,24 @@
         this.child = child;
     }
 
+    override char[] getFQN()
+    {
+        return getSymbol().getFQN();
+    }
+
+    override char[] getMangledFQN()
+    {
+        return target.type.mangle() ~ child.getMangledFQN();
+    }
+
+    override Symbol getSymbol()
+    {
+        auto s = target.getSymbol();
+        if (s !is null)
+            return s.findMember(child.get);
+        return null;
+    }
+
     Exp simplify()
     {
         target = target.simplify;
@@ -535,15 +576,33 @@
         super(t, loc);
     }
 
+    override char[] getFQN()
+    {
+        return name;
+    }
+
+    override char[] getMangledFQN()
+    {
+        return Integer.toString(name.length) ~ name;
+    }
+
+    override Symbol getSymbol()
+    {
+        if (auto decl = env.find(this))
+            return decl.sym;
+        else
+            return null;
+    }
+
     override DType type()
     {
         if (myType !is null)
             return myType;
-        if(auto s = env.find(this))
-            if(s.type)
-                myType = s.type;
+        else if (auto sym = getSymbol)
+            myType = sym.type;
         else
             myType = DType.Int;
+
         return myType;
     }
 
@@ -557,16 +616,6 @@
     {
         return name;
     }
-    
-    char[] getMangled()
-    {
-        DType t = type;
-
-        if(name == "main")
-            return "main";
-
-        return "_D"~name~t.mangle;
-    }
 
     hash_t toHash()
     {