changeset 114:3a0cd42de9cc

Removed misc/Error.d and is now using the error system all way through.
author Anders Johnsen <skabet@gmail.com>
date Sun, 25 May 2008 16:40:38 +0200
parents d03b011c50e9
children e89c42733e10
files ast/Exp.d ast/Stmt.d basic/Messages.d dang/compiler.d gen/CodeGen.d misc/Error.d parser/Action.d sema/AstAction.d sema/ScopeCheck.d sema/TypeCheck.d
diffstat 10 files changed, 67 insertions(+), 172 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Exp.d	Sun May 25 15:48:13 2008 +0200
+++ b/ast/Exp.d	Sun May 25 16:40:38 2008 +0200
@@ -503,7 +503,11 @@
     {
         if (myType !is null)
             return myType;
-        myType = env.find(this).type;
+        if(auto s = env.find(this))
+            if(s.type)
+                myType = s.type;
+        else
+            myType = DType.Int;
         return myType;
     }
 
--- a/ast/Stmt.d	Sun May 25 15:48:13 2008 +0200
+++ b/ast/Stmt.d	Sun May 25 16:40:38 2008 +0200
@@ -8,8 +8,7 @@
        ast.Decl;
 
 import sema.Scope,
-       basic.SourceLocation,
-       misc.Error;
+       basic.SourceLocation;
 
 enum StmtType
 {
@@ -191,9 +190,11 @@
         if (all_values.length != old_values.length + new_values.length)
         {
             // overlap!
-            auto e = new Error(
+            // TODO: Move this to another sema file where it can be enforced.
+         /+   auto e = new Error(
                     "Can't have multiple cases with the same value."
                     " Values appearing in multiple cases: %0");
+                    
             //e.loc(values[0].token.location);
 
             all_values = Array.intersectionOf(old_values, new_values);
@@ -207,15 +208,18 @@
                     if (Array.bsearch(all_values, v))
                         e.tok(c.values[i].token);
             */
-            throw e;
+            throw e;+/
         }
         old_values = all_values;
     }
 
     void setDefault(Stmt[] stmts)
     {
+        // TODO: Move this to another sema file where it can be enforced.
+        /*
         if (defaultBlock.length != 0)
             throw new Error("Switch statements can't have multiple defaults");
+            */
         defaultBlock = stmts;
         if (cases.length > 0)
             cases[$ - 1].followedByDefault = true;
--- a/basic/Messages.d	Sun May 25 15:48:13 2008 +0200
+++ b/basic/Messages.d	Sun May 25 16:40:38 2008 +0200
@@ -26,8 +26,12 @@
     RenameMustBeSingleIdent,
 
 
-    // Imports
+    // sema
     CannotFindModule,
+    InvalidImplicitCast,
+    UndefinedIdentifier,
+    UndefinedType,
+    MissingMember,
 
     // Strings
     InvalidStrPrefix,
@@ -88,8 +92,12 @@
 
         // sema
         CannotFindModule    : E(Err, "Cannot find module '%0'"),
+        InvalidImplicitCast : E(Err, "Cannot make implicit cast between %0 and %1"),
+        UndefinedIdentifier : E(Err, "Undefined identifier '%0'"),
+        UndefinedType       : E(Err, "Undefined type '%0'"),
+        MissingMember       : E(Err, "%0 %1 has no member %2"),
 
-        // 
+        // literals
         InvalidStrPrefix    : E(Err, "Invalid string literal prefix"),
         InvalidStrEscape    : E(Err, "Invalid escape sequence"),
         InvalidUtf8Hex      : E(Err, "Invalid Utf8 hex char"),
--- a/dang/compiler.d	Sun May 25 15:48:13 2008 +0200
+++ b/dang/compiler.d	Sun May 25 16:40:38 2008 +0200
@@ -247,12 +247,14 @@
     StopWatch watch2;
     watch.start;
     watch2.start;
-    (new ScopeCheck).visit(modules);
+    (new ScopeCheck(messages)).visit(modules);
     auto scope_check = watch2.stop;
     watch2.start;
-    (new TypeCheck).visit(modules);
+    messages.checkErrors;
+    (new TypeCheck(messages)).visit(modules);
     auto type_check = watch2.stop;
     watch2.start;
+    messages.checkErrors;
 
     foreach (m; modules)
         foreach (decl; m.decls)
--- a/gen/CodeGen.d	Sun May 25 15:48:13 2008 +0200
+++ b/gen/CodeGen.d	Sun May 25 16:40:38 2008 +0200
@@ -11,8 +11,7 @@
        ast.Exp,
        ast.Module : DModule = Module;
 
-import misc.Error,
-       basic.SmallArray;
+import basic.SmallArray;
 
 import lexer.Token;
 
@@ -282,9 +281,10 @@
             IntegerType l = cast(IntegerType) left.type;
             IntegerType r = cast(IntegerType) right.type;
             if (l is null || r is null)
-                throw error(__LINE__, PE.NoImplicitConversion)
+                assert(0, PE.NoImplicitConversion);
+            /*
                     .arg(left.type.toString)
-                    .arg(right.type.toString);
+                    .arg(right.type.toString);*/
 
             if (l.numBits() < r.numBits())
                 left = b.buildSExt(left, r, ".cast");
@@ -460,7 +460,7 @@
                         return;
                     }
                     else
-                        throw error(__LINE__, PE.VoidRetInNonVoidFunc);
+                        assert(0, PE.VoidRetInNonVoidFunc);
 
                 RValue v = genExpression(ret.exp);
 /*                if (v.type != t)
@@ -724,11 +724,6 @@
         b.buildCall(llvm_memcpy, args[], null);
     }
 
-    Error error(uint line, char[] msg)
-    {
-        return new Error(msg);
-    }
-
     /**
       Get the LLVM Type corresponding to a DType.
 
--- a/misc/Error.d	Sun May 25 15:48:13 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,131 +0,0 @@
-module misc.Error;
-
-import tango.core.Exception,
-       Array = tango.core.Array,
-       tango.text.Util;
-
-import llvm.type;
-
-import lexer.Token,
-       sema.DType;
-
-class Error : Exception
-{
-
-    this(char[] message)
-    {
-        super(message);
-        args ~= message;
-    }
-
-    char[] toString()
-    {
-        char[256] tmp = void;
-        char[] msg = layout(tmp, args);
-        /*
-        if (location.source.name.length > 0)
-            msg = location.toString ~ ": " ~ msg;
-        else
-            msg = msg.dup;
-
-        if (toks.length > 0)
-        {
-            Array.sort(toks,
-                    (Token a, Token b)
-                    {
-                        return a.location.position - b.location.position;
-                    });
-            char[] data = toks[0].location.source.data;
-            size_t low    = toks[0].location.position;
-            size_t high   = toks[$ - 1].location.position;
-
-            size_t line_start = Array.rfind(data[0 .. low], '\n');
-            size_t line_end = high + Array.find(data[high .. $], '\n');
-            char[] line = trim(data[line_start + 1 .. line_end]);
-            char[] marks = line.dup;
-            marks[] = ' ';
-            foreach (tok; toks[0 .. $])
-            {
-                size_t p = tok.location.position - (line.ptr - data.ptr);
-                marks[p .. p + tok.length] = '~';
-            }
-            size_t p = main_tok.location.position - (line.ptr - data.ptr);
-            marks[p .. p + main_tok.length] = '^';
-
-            msg ~= "\n    ";
-            msg ~= line;
-            msg ~= "\n    ";
-            msg ~= marks;
-        }
-*/
-        return msg;
-    }
-
-    Error arg(char[] s)
-    {
-        if (args.length == 11)
-            throw new Exception("Sorry, errors only support up to 10 args");
-        args ~= s;
-        return this;
-    }
-
-    Error arg(char[][] s)
-    {
-        char[] res = s[0 .. $ - 1].join(", ");
-        if (s.length > 1)
-            res ~= " and ";
-        res ~= s[$ - 1];
-        return arg(res);
-    }
-
-    Error arg(char c)
-    {
-        return arg([c]);
-    }
-
-    Error arg(DType[] types)
-    {
-        char[][] res;
-        foreach (type; types)
-            res ~= type.name();
-        return arg(res);
-    }
-
-    Error arg(Tok[] toks...)
-    {
-        char[][] res;
-        foreach (t; toks)
-            res ~= typeToString[t];
-        return arg(res);
-    }
-
-    /*
-    Error loc(Location loc)
-    {
-        location = loc;
-        return this;
-    }
-    */
-
-    Error tok(Token tok)
-    {
-        /*
-        if (toks.length > 0)
-            assert(tok.location.source == toks[0].location.source,
-                    "Tokens must come from the same source");
-        else
-        {
-            main_tok = tok;
-            loc = tok.location;
-        }
-        toks ~= tok;
-    */
-        return this;
-    }
-
-private:
-    char[][] args;
-    //Location location;
-    Token[] toks;
-    Token main_tok;
-}
--- a/parser/Action.d	Sun May 25 15:48:13 2008 +0200
+++ b/parser/Action.d	Sun May 25 16:40:38 2008 +0200
@@ -2,8 +2,6 @@
 
 import lexer.Token;
 
-import misc.Error;
-
 /**
   Used to indicate what type of operator is used in a given binary expression
   (and unary expressions?)
--- a/sema/AstAction.d	Sun May 25 15:48:13 2008 +0200
+++ b/sema/AstAction.d	Sun May 25 16:40:38 2008 +0200
@@ -4,8 +4,7 @@
 
 import lexer.Token;
 
-import misc.Error,
-       basic.SourceManager;
+import basic.SourceManager;
 
 import ast.Module,
        ast.Exp,
--- a/sema/ScopeCheck.d	Sun May 25 15:48:13 2008 +0200
+++ b/sema/ScopeCheck.d	Sun May 25 16:40:38 2008 +0200
@@ -3,17 +3,16 @@
 import sema.Visitor,
        sema.DType;
 
-import tango.io.Stdout;
+import basic.Message;
 
-import misc.Error;
+import tango.io.Stdout;
 
 class ScopeCheck : Visitor!(void)
 {
-    int[char[]] types;
 
-    private Error error(uint line, char[] msg)
+    this(MessageHandler messages)
     {
-        return new Error(msg);
+        this.messages = messages;
     }
 
     override void visitIdentifier(Identifier i)
@@ -21,7 +20,7 @@
         auto symbol = i.env.find(i);
 
         if(symbol is null)
-            throw error(__LINE__, "Undefined identifier: '%0'")
+            messages.report(UndefinedIdentifier, i.loc)
                 .arg(i.get);
                 //.loc(i.token.location);
     }
@@ -29,7 +28,7 @@
     override void visitVarDecl(VarDecl d)
     {
         if(!d.env.findType(d.varType))
-            throw error(__LINE__, "Undefined type: '%0'")
+            messages.report(UndefinedType, d.varType.loc)
                 .arg(d.varType.get);
                 //.loc(d.varType.token.location);
 
@@ -62,7 +61,7 @@
                 auto child = m.child;
                 auto st = cast(DStruct)(target.env.find(target).type);
                 if((child.get in st.members) is null)
-                    throw error(__LINE__, "%0 %1 has no member %2")
+                    messages.report(MissingMember, m.loc)
                         .arg(st.name)
                         .arg(target.get)
                         .arg(child.get);
@@ -77,5 +76,8 @@
     {
         return (s in types? true : false);
     }
+
+    int[char[]] types;
+    MessageHandler messages;
 }
 
--- a/sema/TypeCheck.d	Sun May 25 15:48:13 2008 +0200
+++ b/sema/TypeCheck.d	Sun May 25 16:40:38 2008 +0200
@@ -5,14 +5,14 @@
 
 import tango.io.Stdout;
 
-import misc.Error,
-       basic.SourceLocation;
+import basic.SourceLocation,
+       basic.Message;
 
 class TypeCheck : Visitor!(void)
 {
-    private Error error(uint line, char[] msg)
+    this(MessageHandler messages)
     {
-        return new Error(msg);
+        this.messages = messages;
     }
 
     override void visitBinaryExp(BinaryExp exp)
@@ -22,7 +22,9 @@
         if(exp.left.type.byteSize > exp.right.type.byteSize)
         {
             if(!exp.right.type.hasImplicitConversionTo(exp.left.type))
-                throw error(__LINE__, "Cannot make implicit cast");
+                messages.report(InvalidImplicitCast, exp.loc)
+                    .arg(exp.right.type.toString)
+                    .arg(exp.left.type.toString);
 
             auto castExp = new CastExp(
                     SLoc.Invalid,
@@ -35,7 +37,9 @@
         if(exp.left.type.byteSize < exp.right.type.byteSize)
         {
             if(!exp.left.type.hasImplicitConversionTo(exp.right.type))
-                throw error(__LINE__, "Cannot make implicit cast");
+                messages.report(InvalidImplicitCast, exp.loc)
+                    .arg(exp.right.type.toString)
+                    .arg(exp.left.type.toString);
 
             auto castExp = new CastExp(
                     SLoc.Invalid,
@@ -60,7 +64,9 @@
             if(argType.byteSize != expType.byteSize)
             {
                 if(!expType.hasImplicitConversionTo(argType))
-                    throw error(__LINE__, "Cannot make implicit cast");
+                   messages.report(InvalidImplicitCast, exp.loc)
+                        .arg(expType.toString)
+                        .arg(argType.toString);
 
                 auto castExp = new CastExp(
                         SLoc.Invalid,
@@ -86,7 +92,9 @@
         if(identifierType != expType)
         {
             if(!expType.hasImplicitConversionTo(identifierType))
-                throw error(__LINE__, "Cannot make implicit cast between");
+                messages.report(InvalidImplicitCast, exp.loc)
+                    .arg(expType.toString)
+                    .arg(identifierType.toString);
 
             auto castExp = new CastExp(
                     SLoc.Invalid,
@@ -108,7 +116,9 @@
             if(returnType != expType)
             {
                 if(!expType.hasImplicitConversionTo(returnType))
-                    throw error(__LINE__, "Cannot make implicit cast");
+                   messages.report(InvalidImplicitCast, stmt.exp.loc)
+                        .arg(expType.toString)
+                        .arg(returnType.toString);
 
                 auto castExp = new CastExp(
                         SLoc.Invalid,
@@ -131,7 +141,9 @@
             if(varType.byteSize != expType.byteSize)
             {
                 if(!expType.hasImplicitConversionTo(varType))
-                    throw error(__LINE__, "Cannot make implicit cast");
+                   messages.report(InvalidImplicitCast, decl.init.loc)
+                        .arg(expType.toString)
+                        .arg(varType.toString);
 
                 auto castExp = new CastExp(
                         SLoc.Invalid,
@@ -142,5 +154,7 @@
             }
         }
     }
+
+    MessageHandler messages;
 }