changeset 93:621cedba53ea new_gen

Removed the Symbol from semantics - it was not needed anymore. From now on you set the type by doing a setType on an Identifier.
author Anders Johnsen <skabet@gmail.com>
date Mon, 05 May 2008 20:53:13 +0200
parents 771ac63898e2
children 48bb2287c035
files ast/Decl.d ast/Exp.d basic/Message.d gen/CodeGen.d misc/Error.d sema/Scope.d sema/ScopeBuilder.d sema/Symbol.d
diffstat 8 files changed, 40 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Decl.d	Mon May 05 18:44:20 2008 +0200
+++ b/ast/Decl.d	Mon May 05 20:53:13 2008 +0200
@@ -89,7 +89,7 @@
                 auto i = new Identifier("ret.val");
                 i.env = env;
                 i.env.add(i);
-                i.env.find(i).type = s;
+                i.env.find(i).setType( s );
                 auto var = new VarDecl(returnType, i);
                 var.env = env;
                 funcArgs ~= var;
@@ -97,7 +97,7 @@
                 this.funcArgs = funcArgs;
                 t.returnType = DType.Void;
                 this.returnType = new Identifier("void");
-                env.find(identifier).type = t;
+                env.find(identifier).setType(t);
                 sret = true;
 
                 myType = null;
--- a/ast/Exp.d	Mon May 05 18:44:20 2008 +0200
+++ b/ast/Exp.d	Mon May 05 20:53:13 2008 +0200
@@ -107,7 +107,7 @@
             auto i = new Identifier("temp.var");
             i.env = f.env;
             f.env.add(i);
-            f.env.find(i).type = t;
+            f.env.find(i).setType(t);
             auto ty = new Identifier(t.name);
             auto var = new VarDecl(ty, i, null);
             Exp[] args; 
@@ -530,6 +530,11 @@
         return this;
     }
 
+    void setType(DType myType)
+    {
+        this.myType = myType;
+    }
+
     char[] name;
     private DType myType;
 }
--- a/basic/Message.d	Mon May 05 18:44:20 2008 +0200
+++ b/basic/Message.d	Mon May 05 20:53:13 2008 +0200
@@ -11,8 +11,7 @@
 
 import lexer.Token,
        lexer.Lexer,
-       sema.DType,
-       sema.Symbol;
+       sema.DType;
 
 import basic.SourceLocation,
        basic.SourceManager;
@@ -149,11 +148,6 @@
         return arg(res);
     }
 
-    Message arg(Symbol sym)
-    {
-        return arg(sym.type.name ~ " " ~ sym.id.get);
-    }
-
     Message fatal(ExitLevel exitlevel = ExitLevel.Normal)
     {
         msg_handler.checkErrors(exitlevel);
--- a/gen/CodeGen.d	Mon May 05 18:44:20 2008 +0200
+++ b/gen/CodeGen.d	Mon May 05 20:53:13 2008 +0200
@@ -197,9 +197,9 @@
 
             case DeclType.VarDecl:
                 auto varDecl = cast(VarDecl)decl;
-                auto sym = varDecl.env.find(varDecl.identifier);
-                Type t = llvm(sym.type);
-                GlobalVariable g = m.addGlobal(t, sym.id.get);
+                auto id = varDecl.env.find(varDecl.identifier);
+                Type t = llvm(id.type);
+                GlobalVariable g = m.addGlobal(t, id.get);
                 g.initializer = ConstantInt.GetS(t, 0);
                 table[varDecl.identifier.get] = g;
                 break;
@@ -296,7 +296,7 @@
                 return b.buildLoad(getPointer(exp), ".");
             case ExpType.CallExp:
                 auto callExp = cast(CallExp)exp;
-                auto func_sym = exp.env.find(cast(Identifier)callExp.exp);
+                auto id = exp.env.find(cast(Identifier)callExp.exp);
                 Value[] args;
                 foreach (arg; callExp.args)
                 {
@@ -306,8 +306,8 @@
                 }
                 // BUG: doesn't do implicit type-conversion
                 if(callExp.sret)
-                    return b.buildCall(m.getNamedFunction(func_sym.id.get), args, "");
-                return b.buildCall(m.getNamedFunction(func_sym.id.get), args, ".call");
+                    return b.buildCall(m.getNamedFunction(id.get), args, "");
+                return b.buildCall(m.getNamedFunction(id.get), args, ".call");
             case ExpType.CastExp:
                 auto castExp = cast(CastExp)exp;
                 auto value = genExpression(castExp.exp);
@@ -325,11 +325,11 @@
 
             case ExpType.Identifier:
                 auto identifier = cast(Identifier)exp;
-                auto sym = exp.env.find(identifier);
-                if(sym.type.isStruct || sym.type.isArray)
-                    return table.find(sym.id.get);
+                auto id = exp.env.find(identifier);
+                if(id.type.isStruct || id.type.isArray)
+                    return table.find(id.get);
                 else
-                    return b.buildLoad(table.find(sym.id.get), sym.id.get);
+                    return b.buildLoad(table.find(id.get), id.get);
             case ExpType.MemberReference:
                 auto v = getPointer(exp);
 //                return v;
@@ -505,8 +505,8 @@
         {
             case ExpType.Identifier:
                 auto identifier = cast(Identifier)exp;
-                auto sym = exp.env.find(identifier);
-                return table.find(sym.id.get);
+                auto id = exp.env.find(identifier);
+                return table.find(id.get);
             case ExpType.Deref:
                 auto derefExp = cast(DerefExp)exp;
                 auto target = getPointer(derefExp.exp);
@@ -536,10 +536,9 @@
                     case ExpType.Identifier:
                         auto identifier = cast(Identifier)mem.target;
                         auto child = mem.child;
-                        auto sym = exp.env.find(identifier);
-                        auto symChild = child.env.find(child);
-                        Value v = table.find(sym.id.get);
-                        DType t = sym.type;
+                        auto id = exp.env.find(identifier);
+                        Value v = table.find(id.get);
+                        DType t = id.type;
                         auto st = t.asStruct;
 
                         int i = st.indexOf(child.get);
@@ -548,7 +547,7 @@
                         vals ~= ConstantInt.Get(IntegerType.Int32, 0, false);
                         vals ~= ConstantInt.Get(IntegerType.Int32, i, false);
 
-                        Value val = b.buildGEP(v, vals, sym.id.get~"."~child.get);
+                        Value val = b.buildGEP(v, vals, id.get~"."~child.get);
                         return val;
 
                     case ExpType.MemberReference:
--- a/misc/Error.d	Mon May 05 18:44:20 2008 +0200
+++ b/misc/Error.d	Mon May 05 20:53:13 2008 +0200
@@ -7,8 +7,7 @@
 import llvm.type;
 
 import lexer.Token,
-       sema.DType,
-       sema.Symbol;
+       sema.DType;
 
 class Error : Exception
 {
@@ -100,11 +99,6 @@
         return arg(res);
     }
 
-    Error arg(Symbol sym)
-    {
-        return arg(sym.type.name ~ " " ~ sym.id.get);
-    }
-
     /*
     Error loc(Location loc)
     {
--- a/sema/Scope.d	Mon May 05 18:44:20 2008 +0200
+++ b/sema/Scope.d	Mon May 05 20:53:13 2008 +0200
@@ -6,11 +6,9 @@
        ast.Decl,
        ast.Exp;
 
+public 
 import sema.DType;
 
-public
-import sema.Symbol;
-
 class Scope
 {
     this() {}
@@ -22,15 +20,15 @@
 
     Scope enclosing;
 
-    Symbol add(Identifier id)
+    void add(Identifier id)
     {
-        auto s = new Symbol;
-        s.id = id;
-        symbols[id] = s;
-        return s;
+//        auto s = new Symbol;
+//        s.id = id;
+        symbols[id] = id;
+//        return s;
     }
 
-    Symbol find(Identifier id)
+    Identifier find(Identifier id)
     {
         if(!id)
             return null;
@@ -58,7 +56,7 @@
         if (enclosing)
             res = enclosing.names;
         foreach (id, sym; symbols)
-            res ~= sym.id.name ~ " : " ~ (sym.type is null? "?" : sym.type.name);
+            res ~= sym.name ~ " : " ~ (sym.type is null? "?" : sym.type.name);
         return res;
     }
 
@@ -102,7 +100,7 @@
     DType[char[]] types;
     int currentStmtIndex = -1;
 private:
-    Symbol[Identifier] symbols;
+    Identifier[Identifier] symbols;
     FuncDecl func;
 }
 
--- a/sema/ScopeBuilder.d	Mon May 05 18:44:20 2008 +0200
+++ b/sema/ScopeBuilder.d	Mon May 05 20:53:13 2008 +0200
@@ -26,8 +26,7 @@
         foreach (stmt; d.statements)
             visitStmt(stmt);
 
-        auto sym = d.env.find(d.identifier);
-        sym.type = d.type;
+        d.env.find(d.identifier).setType(d.type);
     }
 
     override void visitVarDecl(VarDecl d)
@@ -38,7 +37,7 @@
         if (d.init)
             visitExp(d.init);
 
-        d.env.find(d.identifier).type = typeOf(d.varType, d.env);
+        d.env.find(d.identifier).setType( typeOf(d.varType, d.env) );
     }
 
     override void visitStructDecl(StructDecl s)
@@ -109,7 +108,7 @@
 
     override void visitFuncDecl(FuncDecl d)
     {
-        auto sym = current().add(d.identifier);
+        current().add(d.identifier);
         auto sc = push();
 
         visitExp(d.returnType);
@@ -137,7 +136,7 @@
         }
 
         auto sc = current();
-        auto sym = sc.add(d.identifier);
+        sc.add(d.identifier);
         d.env = sc;
         visitExp(d.varType);
         visitExp(d.identifier);
@@ -146,7 +145,7 @@
     override void visitStructDecl(StructDecl s)
     {
         auto sc = current();
-        auto sym = sc.add(s.identifier);
+        sc.add(s.identifier);
         s.env = sc;
         auto type = new DStruct(s.identifier);
         
--- a/sema/Symbol.d	Mon May 05 18:44:20 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,12 +0,0 @@
-module sema.Symbol;
-
-public
-import sema.DType,
-       ast.Exp;
-
-class Symbol
-{
-    Identifier id;
-    DType type;
-}
-