changeset 645:89ee7802c978

Moved semantic() methods of expressions to class SemanticPass1.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Mon, 14 Jan 2008 16:01:21 +0100
parents a0643a4d4501
children 68953760d569
files trunk/src/dil/ast/Declarations.d trunk/src/dil/ast/Expression.d trunk/src/dil/ast/Expressions.d trunk/src/dil/semantic/Pass1.d
diffstat 4 files changed, 97 insertions(+), 157 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/ast/Declarations.d	Mon Jan 14 15:11:24 2008 +0100
+++ b/trunk/src/dil/ast/Declarations.d	Mon Jan 14 16:01:21 2008 +0100
@@ -581,7 +581,7 @@
     else
     { // Infer type from first initializer.
       auto firstValue = values[0];
-      firstValue = firstValue.semantic(scop);
+//       firstValue = firstValue.semantic(scop);
       type = firstValue.type;
     }
     assert(type !is null);
@@ -594,8 +594,8 @@
     foreach (i, ident; idents)
     {
       // Perform semantic analysis on value.
-      if (values[i])
-        values[i] = values[i].semantic(scop);
+//       if (values[i])
+//         values[i] = values[i].semantic(scop);
       // Create a new variable symbol.
       // TODO: pass 'prot' to constructor.
       auto variable = new Variable(stc, linkageType, type, ident, this);
--- a/trunk/src/dil/ast/Expression.d	Mon Jan 14 15:11:24 2008 +0100
+++ b/trunk/src/dil/ast/Expression.d	Mon Jan 14 16:01:21 2008 +0100
@@ -5,10 +5,10 @@
 module dil.ast.Expression;
 
 import dil.ast.Node;
-import dil.semantic.Scope;
 import dil.semantic.Types;
 import common;
 
+/// The root class of all expressions.
 abstract class Expression : Node
 {
   Type type; /// The type of this expression.
@@ -18,29 +18,8 @@
     super(NodeCategory.Expression);
   }
 
-  // Semantic analysis:
-
-  Expression semantic(Scope scop)
-  {
-    debug Stdout("SA for "~this.classinfo.name).newline;
-    if (!type)
-      type = Types.Undefined;
-    return this;
-  }
-
   Expression evaluate()
   {
     return null;
   }
-
-  import dil.Messages;
-  void error(Scope scop, MID mid)
-  {
-    scop.error(this.begin, mid);
-  }
-
-  void error(Scope scop, char[] msg)
-  {
-
-  }
 }
--- a/trunk/src/dil/ast/Expressions.d	Mon Jan 14 15:11:24 2008 +0100
+++ b/trunk/src/dil/ast/Expressions.d	Mon Jan 14 16:01:21 2008 +0100
@@ -36,13 +36,6 @@
     this.right = right;
     this.tok = tok;
   }
-
-  override Expression semantic(Scope scop)
-  {
-    left = left.semantic(scop);
-    right = right.semantic(scop);
-    return this;
-  }
 }
 
 class CondExpression : BinaryExpression
@@ -583,26 +576,7 @@
     this.specialToken = specialToken;
   }
 
-  Expression e; /// The expression created in the semantic phase.
-
-  override Expression semantic(Scope)
-  {
-    if (type)
-      return e;
-    switch (specialToken.type)
-    {
-    case TOK.LINE, TOK.VERSION:
-      e = new IntExpression(specialToken.uint_, Types.Uint);
-      break;
-    case TOK.FILE, TOK.DATE, TOK.TIME, TOK.TIMESTAMP, TOK.VENDOR:
-      e = new StringExpression(specialToken.str);
-      break;
-    default:
-      assert(0);
-    }
-    type = e.type;
-    return e;
-  }
+  Expression value; /// The expression created in the semantic phase.
 }
 
 class TemplateInstanceExpression : Expression
@@ -646,13 +620,6 @@
     this();
     this.type = type;
   }
-
-  override Expression semantic(Scope)
-  {
-    if (!type)
-      type = Types.Void_ptr;
-    return this;
-  }
 }
 
 class DollarExpression : Expression
@@ -661,16 +628,6 @@
   {
     mixin(set_kind);
   }
-
-  override Expression semantic(Scope scop)
-  {
-    if (type)
-      return this;
-    type = Types.Size_t;
-    // if (!scop.inArraySubscript)
-    //   error(scop, "$ can only be in an array subscript.");
-    return this;
-  }
 }
 
 class BoolExpression : Expression
@@ -680,17 +637,7 @@
     mixin(set_kind);
   }
 
-  Expression e;
-  override Expression semantic(Scope scop)
-  {
-    if (type)
-      return this;
-    assert(this.begin !is null);
-    auto b = (this.begin.type == TOK.True) ? true : false;
-    e = new IntExpression(b, Types.Bool);
-    type = Types.Bool;
-    return this;
-  }
+  Expression value; /// IntExpression of type int.
 }
 
 class IntExpression : Expression
@@ -722,22 +669,6 @@
     }
     this(token.ulong_, type);
   }
-
-  override Expression semantic(Scope)
-  {
-    if (type)
-      return this;
-
-    if (number & 0x8000_0000_0000_0000)
-      type = Types.Ulong; // 0xFFFF_FFFF_FFFF_FFFF
-    else if (number & 0xFFFF_FFFF_0000_0000)
-      type = Types.Long; // 0x7FFF_FFFF_FFFF_FFFF
-    else if (number & 0x8000_0000)
-      type = Types.Uint; // 0xFFFF_FFFF
-    else
-      type = Types.Int; // 0x7FFF_FFFF
-    return this;
-  }
 }
 
 class RealExpression : Expression
@@ -773,14 +704,6 @@
     }
     this(token.real_, type);
   }
-
-  override Expression semantic(Scope)
-  {
-    if (type)
-      return this;
-    type = Types.Double;
-    return this;
-  }
 }
 
 /++
@@ -797,14 +720,6 @@
     this.number = number;
     this.type = type;
   }
-
-  override Expression semantic(Scope)
-  {
-    if (type)
-      return this;
-    type = Types.Cdouble;
-    return this;
-  }
 }
 
 class CharExpression : Expression
@@ -815,19 +730,6 @@
     mixin(set_kind);
     this.character = character;
   }
-
-  override Expression semantic(Scope scop)
-  {
-    if (type)
-      return this;
-    if (character <= 0xFF)
-      type = Types.Char;
-    else if (character <= 0xFFFF)
-      type = Types.Wchar;
-    else
-      type = Types.Dchar;
-    return this;
-  }
 }
 
 class StringExpression : Expression
@@ -867,13 +769,6 @@
     this(cast(ubyte[])str, Types.Dchar);
   }
 
-  override Expression semantic(Scope scop)
-  {
-    if (type)
-      return this;
-    return this;
-  }
-
   char[] getString()
   {
     char[] buffer;
@@ -930,31 +825,6 @@
     addChild(expr);
     this.expr = expr;
   }
-
-  override Expression semantic(Scope scop)
-  {
-    if (type)
-      return this.expr;
-    // TODO:
-    auto expr = this.expr.semantic(scop);
-    expr = expr.evaluate();
-    if (expr is null)
-      return this;
-    auto strExpr = TryCast!(StringExpression)(expr);
-    if (strExpr is null)
-     error(scop, MSG.MixinArgumentMustBeString);
-    else
-    {
-      auto loc = this.begin.getLocation();
-      auto filePath = loc.filePath;
-      auto parser = new_ExpressionParser(strExpr.getString(), filePath, scop.infoMan);
-      expr = parser.parse();
-      expr = expr.semantic(scop);
-    }
-    this.expr = expr;
-    this.type = expr.type;
-    return expr;
-  }
 }
 
 class ImportExpression : Expression
--- a/trunk/src/dil/semantic/Pass1.d	Mon Jan 14 15:11:24 2008 +0100
+++ b/trunk/src/dil/semantic/Pass1.d	Mon Jan 14 16:01:21 2008 +0100
@@ -15,6 +15,7 @@
 
 import dil.semantic.Symbol,
        dil.semantic.Symbols,
+       dil.semantic.Types,
        dil.semantic.Scope,
        dil.semantic.Module;
 
@@ -192,6 +193,96 @@
   Expression visit(AndAndExpression)
   { return null; }
 
+  Expression visit(SpecialTokenExpression e)
+  {
+    if (e.type)
+      return e.value;
+    switch (e.specialToken.type)
+    {
+    case TOK.LINE, TOK.VERSION:
+      e.value = new IntExpression(e.specialToken.uint_, Types.Uint);
+      break;
+    case TOK.FILE, TOK.DATE, TOK.TIME, TOK.TIMESTAMP, TOK.VENDOR:
+      e.value = new StringExpression(e.specialToken.str);
+      break;
+    default:
+      assert(0);
+    }
+    e.type = e.value.type;
+    return e.value;
+  }
+
+  Expression visit(DollarExpression e)
+  {
+    if (e.type)
+      return e;
+    e.type = Types.Size_t;
+    // if (!inArraySubscript)
+    //   error("$ can only be in an array subscript.");
+    return e;
+  }
+
+  Expression visit(NullExpression e)
+  {
+    if (!e.type)
+      e.type = Types.Void_ptr;
+    return e;
+  }
+
+  Expression visit(BoolExpression e)
+  {
+    if (e.type)
+      return e;
+    assert(e.begin !is null);
+    auto b = (e.begin.type == TOK.True) ? true : false;
+    e.value = new IntExpression(b, Types.Bool);
+    e.type = Types.Bool;
+    return e;
+  }
+
+  Expression visit(IntExpression e)
+  {
+    if (e.type)
+      return e;
+
+    if (e.number & 0x8000_0000_0000_0000)
+      e.type = Types.Ulong; // 0xFFFF_FFFF_FFFF_FFFF
+    else if (e.number & 0xFFFF_FFFF_0000_0000)
+      e.type = Types.Long; // 0x7FFF_FFFF_FFFF_FFFF
+    else if (e.number & 0x8000_0000)
+      e.type = Types.Uint; // 0xFFFF_FFFF
+    else
+      e.type = Types.Int; // 0x7FFF_FFFF
+    return e;
+  }
+
+  Expression visit(RealExpression e)
+  {
+    if (e.type)
+      e.type = Types.Double;
+    return e;
+  }
+
+  Expression visit(ComplexExpression e)
+  {
+    if (!e.type)
+      e.type = Types.Cdouble;
+    return e;
+  }
+
+  Expression visit(CharExpression e)
+  {
+    if (e.type)
+      return e;
+    if (e.character <= 0xFF)
+      e.type = Types.Char;
+    else if (e.character <= 0xFFFF)
+      e.type = Types.Wchar;
+    else
+      e.type = Types.Dchar;
+    return e;
+  }
+
   Expression visit(MixinExpression me)
   {
     /+