changeset 63:9f8131676242 new_gen

Now Decl's have a DType type(), and should use varType and returnType to get the old type id
author Anders Halager <halager@gmail.com>
date Tue, 29 Apr 2008 15:13:38 +0200
parents 2451f0904bf6
children 91f10c34cd7b
files ast/Decl.d ast/Exp.d gen/CodeGen.d sema/DType.d sema/Declarations.d sema/SymbolTableBuilder.d sema/Visitor.d tools/AstPrinter.d tools/DotPrinter.d
diffstat 9 files changed, 63 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Decl.d	Tue Apr 29 15:00:11 2008 +0200
+++ b/ast/Decl.d	Tue Apr 29 15:13:38 2008 +0200
@@ -7,7 +7,9 @@
 
 import tango.io.Stdout;
 
-import sema.SymbolTable;
+import sema.SymbolTable,
+       sema.DType,
+       basic.SmallArray;
 
 enum DeclType
 {
@@ -27,6 +29,8 @@
     {
     }
 
+    DType type() { return null; }
+
     DeclType declType;
     Scope env;
 }
@@ -37,7 +41,7 @@
             Exp e = null)
     {
         super(DeclType.VarDecl);
-        this.type = type;
+        this.varType = type;
         this.identifier = identifier;
         this.init = e;
     }
@@ -46,7 +50,12 @@
     {
     }
 
-    Identifier type, identifier;
+    override DType type()
+    {
+        return env.find(identifier).type;
+    }
+
+    Identifier varType, identifier;
     Exp init;
 }
 
@@ -55,7 +64,7 @@
     this(Identifier type, Identifier identifier)
     {
         super(DeclType.FuncDecl);
-        this.type = type;
+        this.returnType = type;
         this.identifier = identifier;
     }
 
@@ -73,20 +82,20 @@
     {
         if(auto t = cast(DFunction)env.find(identifier).type)
         {
-            if(auto s = cast(DStruct)t.return_type)
+            if(auto s = cast(DStruct)t.returnType)
             {
                 VarDecl[] funcArgs;
                 auto i = new Identifier("ret.val");
                 i.env = env;
                 i.env.add(i);
                 i.env.find(i).type = s;
-                auto var = new VarDecl(type, i);
+                auto var = new VarDecl(returnType, i);
                 var.env = env;
                 funcArgs ~= var;
                 funcArgs ~= this.funcArgs;
                 this.funcArgs = funcArgs;
-                t.return_type = DType.Void;
-                this.type = new Identifier("void");
+                t.returnType = DType.Void;
+                this.returnType = new Identifier("void");
                 env.find(identifier).type = t;
                 sret = true;
             }
@@ -98,10 +107,26 @@
             stmt.simplify();
     }
 
-    Identifier type, identifier;
+    override DType type()
+    {
+        if (myType !is null)
+            return myType;
+
+        auto t = new DFunction(identifier);
+        t.returnType = env.findType(returnType);
+        SmallArray!(DType) array;
+        foreach (a; funcArgs)
+            array ~= a.type();
+        t.params = array.safe();
+        myType = t;
+        return myType;
+    }
+
+    Identifier returnType, identifier;
     VarDecl[] funcArgs;
     Stmt[] statements;
     bool sret = false;
+    private DType myType;
 }
 
 class StructDecl : Decl
@@ -122,7 +147,13 @@
     {
     }
 
+    override DType type()
+    {
+        return env.find(identifier).type;
+    }
+
     Identifier identifier;
     VarDecl[] vars;
+    private DType myType;
 }
 
--- a/ast/Exp.d	Tue Apr 29 15:00:11 2008 +0200
+++ b/ast/Exp.d	Tue Apr 29 15:13:38 2008 +0200
@@ -55,7 +55,7 @@
     {
         DFunction f = cast(DFunction)exp.type();
         assert(f !is null, "Can only call functions");
-        return f.return_type;
+        return f.returnType;
     }
 
     Exp exp;
@@ -69,7 +69,7 @@
         {
             DFunction func_t = cast(DFunction)exp.type();
             assert(func_t !is null, "Calling on something that isn't a function");
-            if (cast(DStruct)func_t.return_type is null)
+            if (cast(DStruct)func_t.returnType is null)
                 return this;
 
             auto call = cast(Identifier)exp;
@@ -84,9 +84,7 @@
             args ~= this.args;
             auto callExp = new CallExp(exp, args);
             callExp.env = f.env;
-            //        auto ass = new AssignExp(i, callExp);
             var.env = f.env;
-            //        ass.env = f.env;
             auto stmtVar = new DeclStmt(var);
             auto stmtCall = new ExpStmt(callExp);
             Stmt[] stmts;
--- a/gen/CodeGen.d	Tue Apr 29 15:00:11 2008 +0200
+++ b/gen/CodeGen.d	Tue Apr 29 15:13:38 2008 +0200
@@ -87,7 +87,7 @@
                 if(auto st = cast(DStruct)ret_t)
                     ret_t = DType.Void;
                 else if(auto f = cast(DFunction)ret_t)
-                    ret_t = f.return_type;
+                    ret_t = f.returnType;
                 auto func_t = FunctionType.Get(llvm(ret_t), param_types);
                 auto llfunc = m.addFunction(func_t, fd.identifier.get);
 
@@ -599,7 +599,7 @@
             foreach(param; f.params)
                 params ~= llvm(param);
 
-            Type ret_t = llvm(f.return_type);
+            Type ret_t = llvm(f.returnType);
             Type res = FunctionType.Get(ret_t, params.unsafe());
             type_map[t] = res;
             return res;
--- a/sema/DType.d	Tue Apr 29 15:00:11 2008 +0200
+++ b/sema/DType.d	Tue Apr 29 15:13:38 2008 +0200
@@ -159,6 +159,6 @@
     }
 
     DType[] params;
-    DType return_type;
+    DType returnType;
 }
 
--- a/sema/Declarations.d	Tue Apr 29 15:00:11 2008 +0200
+++ b/sema/Declarations.d	Tue Apr 29 15:13:38 2008 +0200
@@ -28,10 +28,10 @@
 
     override void visitVarDecl(VarDecl d)
     {
-        if(!d.env.findType(d.type))
+        if(!d.env.findType(d.varType))
             throw error(__LINE__, "Undefined type: '%0'")
-                .arg(d.type.get)
-                .loc(d.type.token.location);
+                .arg(d.varType.get)
+                .loc(d.varType.token.location);
 
         visitExp(d.identifier);
         if (d.init)
@@ -65,11 +65,9 @@
         }
     }
 
-
-    bool isType(char[] s)
+    private bool isType(char[] s)
     {
         return (s in types? true : false);
     }
-
 }
 
--- a/sema/SymbolTableBuilder.d	Tue Apr 29 15:00:11 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Tue Apr 29 15:13:38 2008 +0200
@@ -21,7 +21,7 @@
 
     override void visitFuncDecl(FuncDecl d)
     {
-        visitExp(d.type);
+        visitExp(d.returnType);
         visitExp(d.identifier);
         SmallArray!(DType, 8) arg_types;
         foreach (arg; d.funcArgs)
@@ -33,7 +33,7 @@
             visitStmt(stmt);
 
         auto func_t = new DFunction(d.identifier);
-        func_t.return_type = d.env.findType(d.type);
+        func_t.returnType = d.env.findType(d.returnType);
         func_t.params = arg_types.safe();
 
         auto sym = d.env.find(d.identifier);
@@ -45,8 +45,8 @@
         if (d.init)
             visitExp(d.init);
 
-        d.env.find(d.identifier).type = typeOf(d.type, d.env);
-        visitExp(d.type);
+        d.env.find(d.identifier).type = typeOf(d.varType, d.env);
+        visitExp(d.varType);
         visitExp(d.identifier);
     }
 
@@ -56,9 +56,7 @@
 
         auto st = (cast(DStruct)s.env.types[s.identifier.get]);
         foreach(varDecl ; s.vars)
-        {
-            st.addMember(typeOf(varDecl.type, varDecl.env), varDecl.identifier.get);
-        }
+            st.addMember(typeOf(varDecl.varType, varDecl.env), varDecl.identifier.get);
 
         super.visitStructDecl(s);
     }
@@ -66,13 +64,6 @@
     DType typeOf(Identifier id, Scope sc)
     {
         return sc.findType(id);
-
-        /*
-        if (auto type = id.get in types)
-            return *type;
-        DType res = new DType(id);
-        types[id.get] = res;
-        return res;*/
     }
 }
 
@@ -124,7 +115,7 @@
         auto sym = current().add(d.identifier);
         auto sc = push();
 
-        visitExp(d.type);
+        visitExp(d.returnType);
         visitExp(d.identifier);
         d.env = current();
         sc.parentFunction = d;
@@ -151,7 +142,7 @@
         auto sc = current();
         auto sym = sc.add(d.identifier);
         d.env = sc;
-        visitExp(d.type);
+        visitExp(d.varType);
         visitExp(d.identifier);
     }
 
--- a/sema/Visitor.d	Tue Apr 29 15:00:11 2008 +0200
+++ b/sema/Visitor.d	Tue Apr 29 15:13:38 2008 +0200
@@ -86,7 +86,7 @@
     // Declarations:
     DeclT visitVarDecl(VarDecl d)
     {
-        visitExp(d.type);
+        visitExp(d.varType);
         visitExp(d.identifier);
         if (d.init)
             visitExp(d.init);
@@ -99,7 +99,7 @@
 
     DeclT visitFuncDecl(FuncDecl f)
     {
-        visitExp(f.type);
+        visitExp(f.returnType);
         visitExp(f.identifier);
         foreach (arg; f.funcArgs)
             visitDecl(arg);
--- a/tools/AstPrinter.d	Tue Apr 29 15:00:11 2008 +0200
+++ b/tools/AstPrinter.d	Tue Apr 29 15:13:38 2008 +0200
@@ -33,7 +33,7 @@
             case DeclType.FuncDecl:
                 auto funcDecl = cast(FuncDecl)decl;
                 printBeginLine();
-                printIdentifier(funcDecl.type);
+                printIdentifier(funcDecl.returnType);
                 space;
                 printIdentifier(funcDecl.identifier);
                 printFuncArgs(funcDecl);
@@ -46,7 +46,7 @@
             case DeclType.VarDecl:
                 auto varDecl = cast(VarDecl)decl;
                 printBeginLine();
-                printIdentifier(varDecl.type);
+                printIdentifier(varDecl.varType);
                 space;
                 printIdentifier(varDecl.identifier);
                 if(varDecl.init)
@@ -156,7 +156,7 @@
      
         foreach(i, d; decl.funcArgs)
         {
-            printIdentifier(d.type);
+            printIdentifier(d.varType);
             if(i == 0 && decl.sret)
                 print("*");
             space;
--- a/tools/DotPrinter.d	Tue Apr 29 15:00:11 2008 +0200
+++ b/tools/DotPrinter.d	Tue Apr 29 15:13:38 2008 +0200
@@ -40,7 +40,7 @@
                 //printFuncArgs(funcDecl.funcArgs);
                 Stdout(dotId(decl))(` [label="function`);
                 Stdout(`\n name: `)(text(funcDecl.identifier));
-                Stdout(`\n return type: `)(text(funcDecl.type));
+                Stdout(`\n return type: `)(text(funcDecl.returnType));
                 Stdout(`", shape=box, fillcolor=lightblue, style=filled]`);
                 Stdout.newline;
                 //Stdout(`"`);
@@ -55,7 +55,7 @@
                 //printFuncArgs(funcDecl.funcArgs);
                 Stdout(dotId(decl))(` [label="var`);
                 Stdout(`\n name: `)(text(varDecl.identifier));
-                Stdout(`\n type: `)(text(varDecl.type));
+                Stdout(`\n type: `)(text(varDecl.varType));
                 Stdout(`"]`).newline;
 
                 if (varDecl.init !is null)