changeset 183:8ea749b7da91

Fixed a few errors so that two more tests passes. Also, now you only need a type in a function param.
author Anders Johnsen <skabet@gmail.com>
date Fri, 25 Jul 2008 10:59:16 +0200
parents 4e703658eca0
children 86a2ede00e9a
files ast/Exp.d ast/Stmt.d basic/Messages.d gen/CodeGen.d parser/Parser.d sema/TypeCheck.d tests/code/function_pointer_2.d tests/parser/for_2.d tests/sema/shift_1.d tests/sema/type_check_1.d
diffstat 10 files changed, 83 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Exp.d	Fri Jul 25 01:40:08 2008 +0200
+++ b/ast/Exp.d	Fri Jul 25 10:59:16 2008 +0200
@@ -296,6 +296,8 @@
 
     override DType type() 
     {
+        if (_type)
+            return _type;
         return exp.type().asPointer().pointerOf; 
     }
 
@@ -304,6 +306,7 @@
         return SourceRange(loc) + exp.sourceRange;
     }
 
+    DType _type;
     public Exp exp;
 }
 
--- a/ast/Stmt.d	Fri Jul 25 01:40:08 2008 +0200
+++ b/ast/Stmt.d	Fri Jul 25 10:59:16 2008 +0200
@@ -194,7 +194,8 @@
 
     override void simplify()
     {
-        cond = cond.simplify();
+        if (cond)
+            cond = cond.simplify();
         forBody.simplify();
     }
 
--- a/basic/Messages.d	Fri Jul 25 01:40:08 2008 +0200
+++ b/basic/Messages.d	Fri Jul 25 10:59:16 2008 +0200
@@ -43,6 +43,8 @@
     CandidateNr,
     NoMethodByName,
     NoMachingMethod,
+    CannotReassignSArray,
+    CanOnlyDerefPointers,
 
     // Strings
     InvalidStrPrefix,
@@ -124,6 +126,8 @@
             : E(Err, "Can't have multiple cases with the same value."
                      " Values appearing in multiple cases: %0"),
         InvalidCaseValue    : E(Err, "Case values must be integers"),
+        CannotReassignSArray: E(Err, "Cannot reassign static arrays"),
+        CanOnlyDerefPointers: E(Err, "Can only deref pointers, not '%0'"),
 
         // literals
         InvalidStrPrefix    : E(Err, "Invalid string literal prefix"),
--- a/gen/CodeGen.d	Fri Jul 25 01:40:08 2008 +0200
+++ b/gen/CodeGen.d	Fri Jul 25 10:59:16 2008 +0200
@@ -95,23 +95,22 @@
             (FuncDecl fd)
             {
                 Type[] param_types;
-                foreach (i, p; fd.funcArgs)
+                foreach (i, p; fd.type.params)
                 {
-                    DType t = p.identifier.type;
-                    if (auto st = t.asStruct())
+                    if (auto st = p.asStruct())
                     {
                         Type pointer = PointerType.Get(llvm(st));
                         param_types ~= pointer;
                     }
-                    else if (auto ar = t.asArray())
+                    else if (auto ar = p.asArray())
                     {
                         Type pointer = PointerType.Get(llvm(ar));
                         param_types ~= pointer;
                     }
                     else
-                        param_types ~= llvm(t);
+                        param_types ~= llvm(p);
                 }
-                auto ret_t = fd.identifier.type;
+                auto ret_t = fd.type.returnType;
                 if(auto st = cast(DStruct)ret_t)
                     ret_t = DType.Void;
                 else if(auto f = cast(DFunction)ret_t)
@@ -124,7 +123,7 @@
                     if(i == 0 && fd.sret)
                         llfunc.addParamAttr(0, ParamAttr.StructRet);
 
-                    DType t = p.identifier.type;
+                    DType t = fd.type.params[i];
                     if (auto st = t.asStruct)
                     {
                         if (i == 0 && fd.sret)
@@ -598,9 +597,24 @@
             +/
             case StmtType.For:
                 auto fStmt = cast(ForStmt)stmt;
-                genStmt(fStmt.init);
-                scope inc = new ExpStmt(fStmt.incre);
-                genLoop(stmt.env, fStmt.cond, false, fStmt.forBody, inc);
+                Stmt[] stmts;
+                if(fStmt.init)
+                    genStmt(fStmt.init);
+                ExpStmt inc;
+                if(fStmt.incre)
+                    stmts ~= new ExpStmt(fStmt.incre);
+                Exp cond;
+                if(fStmt.cond)
+                    cond = fStmt.cond;
+                else
+                {
+                    auto i = new IntegerLit(fStmt.loc,"1");
+                    i.number.type = NumberType.Int;
+                    i.number.integer = 1;
+                    cond = i;
+                }
+                stmts ~= fStmt.forBody;
+                genLoop(stmt.env, cond, false, stmts);
                 break;
             case StmtType.Switch:
                 auto sw = cast(SwitchStmt)stmt;
--- a/parser/Parser.d	Fri Jul 25 01:40:08 2008 +0200
+++ b/parser/Parser.d	Fri Jul 25 10:59:16 2008 +0200
@@ -167,8 +167,9 @@
                 messages.report(UnexpectedLinkType, t.location);
         }
 
-        require(Tok.CloseParentheses);
-        
+        if (!isa(Tok.CloseParentheses))
+            messages.report(UnexpectedTokSingle, peek.location);
+
         return e;
     }
 
--- a/sema/TypeCheck.d	Fri Jul 25 01:40:08 2008 +0200
+++ b/sema/TypeCheck.d	Fri Jul 25 10:59:16 2008 +0200
@@ -82,6 +82,24 @@
                 }
             }
         }
+        if (exp.op >= Operator.LeftShift &&
+            exp.op <= Operator.UnsignedRightShift)
+        {}  // FIXME: When we have const-system we need to check for 
+            //        right site being larger then the bitsize of the 
+            //        left
+    }
+
+    override void visitDerefExp(DerefExp exp)
+    {
+        if (!exp.exp.type.isPointer)
+        {
+            messages.report(CanOnlyDerefPointers,
+                    [exp.exp.sourceRange][], 
+                    [exp.loc])
+                .arg(exp.exp.type.toString);
+
+            exp._type = DType.Int;
+        }
     }
 
     override void visitCallExp(CallExp exp)
@@ -333,6 +351,11 @@
             castExp.env = exp.exp.env;
             exp.exp = castExp;
         }
+
+        if (expType.isStaticArray)
+            messages.report(CannotReassignSArray, 
+                    [exp.identifier.sourceRange, exp.exp.sourceRange][], 
+                    [exp.loc]);
     }
 
     override void visitReturnStmt(ReturnStmt stmt)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/code/function_pointer_2.d	Fri Jul 25 10:59:16 2008 +0200
@@ -0,0 +1,10 @@
+
+int main()
+{
+    int function(int) f = &foo;
+    f();
+}
+
+int foo(int x)
+{
+}
--- a/tests/parser/for_2.d	Fri Jul 25 01:40:08 2008 +0200
+++ b/tests/parser/for_2.d	Fri Jul 25 10:59:16 2008 +0200
@@ -1,6 +1,5 @@
 
-
-int main()
+void main()
 {
     for( ; ; ) 
     {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/sema/shift_1.d	Fri Jul 25 10:59:16 2008 +0200
@@ -0,0 +1,6 @@
+
+int main()
+{
+    int c;
+    c << 33;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/sema/type_check_1.d	Fri Jul 25 10:59:16 2008 +0200
@@ -0,0 +1,7 @@
+//fail
+int main()
+{
+    int function(int s) m;
+    m = &main;
+}
+