changeset 77:13eea2c4e60d new_gen

Now able of --ast-dump-code with Pointer types and also codeGen int* x;
author Anders Johnsen <skabet@gmail.com>
date Fri, 02 May 2008 16:37:13 +0200
parents 9171f04dd9ee
children 81813366ef92
files ast/Exp.d gen/CodeGen.d sema/AstAction.d sema/DType.d sema/SymbolTableBuilder.d sema/Visitor.d tools/AstPrinter.d
diffstat 7 files changed, 82 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Exp.d	Fri May 02 15:48:57 2008 +0200
+++ b/ast/Exp.d	Fri May 02 16:37:13 2008 +0200
@@ -20,6 +20,7 @@
     ArrayReference,
     Identifier,
     ArrayIdentifier,
+    PointerIdentifier,
     AssignExp,
     CallExp,
     CastExp,
@@ -328,6 +329,28 @@
     Exp exp;
 }
 
+class PointerIdentifier : Identifier
+{
+    this(Identifier pointerOf)
+    {
+        super(ExpType.PointerIdentifier);
+        this.pointerOf = pointerOf;
+        this.name = pointerOf.name;
+    }
+
+    override DType type()
+    {
+        if (myType !is null)
+            return myType;
+        myType = new DPointer(pointerOf.type);
+        return myType;
+    }
+
+    Identifier pointerOf;
+
+    private DType myType;
+}
+
 class ArrayIdentifier : Identifier
 {
     this(Identifier arrayOf)
--- a/gen/CodeGen.d	Fri May 02 15:48:57 2008 +0200
+++ b/gen/CodeGen.d	Fri May 02 16:37:13 2008 +0200
@@ -661,6 +661,12 @@
 */
             return res;
         }
+        else if (auto f = t.asPointer)
+        {
+            Type res = PointerType.Get(llvm(f.pointerOf));
+            type_map[t] = res;
+            return res;
+        }
         assert(0, "Only integers, structs and functions are supported");
     }
 
--- a/sema/AstAction.d	Fri May 02 15:48:57 2008 +0200
+++ b/sema/AstAction.d	Fri May 02 16:37:13 2008 +0200
@@ -19,6 +19,15 @@
  */
 class AstAction : Action
 {
+    Identifier handleType(Id type)
+    {
+        if(auto t = cast(PointerId)type)
+            return new PointerIdentifier(handleType(t.id));
+        else
+            return new Identifier(type.tok);
+    }
+
+
     // -- Declarations --
     override DeclT actOnDeclarator(ref Id type, ref Id id, ExprT init)
     {
@@ -26,7 +35,7 @@
         if(type.tok.type == Tok.Struct)
             return new StructDecl(new Identifier(id.tok));
         else
-            return new VarDecl(new Identifier(type.tok), new Identifier(id.tok), exp);
+            return new VarDecl(handleType(type), new Identifier(id.tok), exp);
     }
     
     override void actOnStructMember(DeclT decl, ref Id type, ref Id name, ExprT init)
@@ -39,7 +48,7 @@
                 exp);
     }
 
-    ExprT actOnMemberReference(ExprT lhs, Location op, Id member)
+    override ExprT actOnMemberReference(ExprT lhs, Location op, Id member)
     {
         return new MemberReference(cast(Exp)lhs, new Identifier(member.tok));
     }
--- a/sema/DType.d	Fri May 02 15:48:57 2008 +0200
+++ b/sema/DType.d	Fri May 02 16:37:13 2008 +0200
@@ -35,6 +35,11 @@
     /// Return a DArray if this is one, otherwise return null
     DArray asArray() { return null; }
 
+    /// Is this type a DPointer
+    bool isPointer() { return false; }
+    /// Return a DPointer if this is one, otherwise return null
+    DPointer asPointer() { return null; }
+
     /// Is this type a DFunction
     bool isFunction() { return false; }
     /// Return a DFunction if this is one, otherwise return null
@@ -198,6 +203,22 @@
     DType arrayOf;
 }
 
+class DPointer : DType
+{
+    this(DType pointerOf, DType actual = null)
+    {
+        super(id, actual);
+        this.pointerOf = pointerOf;
+    }
+
+    override bool isPointer() { return true; }
+    override DPointer asPointer() { return this; }
+
+    int byteSize() { return DType.Int.byteSize; }
+
+    DType pointerOf;
+}
+
 class DFunction : DType
 {
     this(Identifier id, DType actual = null)
--- a/sema/SymbolTableBuilder.d	Fri May 02 15:48:57 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Fri May 02 16:37:13 2008 +0200
@@ -55,6 +55,8 @@
 
     DType typeOf(Identifier id, Scope sc)
     {
+        if(auto i = cast(PointerIdentifier)id)
+            return new DPointer(typeOf(i.pointerOf, sc));
         return sc.findType(id);
     }
 }
--- a/sema/Visitor.d	Fri May 02 15:48:57 2008 +0200
+++ b/sema/Visitor.d	Fri May 02 16:37:13 2008 +0200
@@ -78,6 +78,8 @@
                 return visitCastExp(cast(CastExp)exp);
             case ExpType.Identifier:
                 return visitIdentifier(cast(Identifier)exp);
+            case ExpType.PointerIdentifier:
+                return visitPointerIdentifier(cast(PointerIdentifier)exp);
             case ExpType.MemberReference:
                 return visitMemberReference(cast(MemberReference)exp);
             default:
@@ -273,6 +275,16 @@
             return ExpT.init;
     }
 
+    ExpT visitPointerIdentifier(PointerIdentifier exp)
+    {
+        visitExp(exp.pointerOf);
+
+        static if (is(ExpT == void))
+            return;
+        else
+            return ExpT.init;
+    }
+
     ExpT visitMemberReference(MemberReference mem)
     {
         visitExp(mem.target);
--- a/tools/AstPrinter.d	Fri May 02 15:48:57 2008 +0200
+++ b/tools/AstPrinter.d	Fri May 02 16:37:13 2008 +0200
@@ -46,9 +46,9 @@
             case DeclType.VarDecl:
                 auto varDecl = cast(VarDecl)decl;
                 printBeginLine();
-                printIdentifier(varDecl.varType);
+                printExp(varDecl.varType);
                 space;
-                printIdentifier(varDecl.identifier);
+                printExp(varDecl.identifier);
                 if(varDecl.init)
                 {
                     print(" = ");
@@ -136,6 +136,11 @@
                 auto iden = cast(Identifier)exp;
                 printIdentifier(iden);
                 break;
+            case ExpType.PointerIdentifier:
+                auto iden = cast(PointerIdentifier)exp;
+                printExp(iden.pointerOf);
+                print("*");
+                break;
             case ExpType.CallExp:
                 auto callExp = cast(CallExp)exp;
                 printExp(callExp.exp);