changeset 648:4ae7b13aaac8

Moved some semantic() methods to SemanticPass1.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Mon, 14 Jan 2008 22:34:09 +0100
parents 6a1cb6768bd2
children 3ebe76ad680e
files trunk/src/dil/ast/Declarations.d trunk/src/dil/ast/Statements.d trunk/src/dil/ast/Types.d trunk/src/dil/ast/Visitor.d trunk/src/dil/semantic/Module.d trunk/src/dil/semantic/Pass1.d
diffstat 6 files changed, 150 insertions(+), 225 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/ast/Declarations.d	Mon Jan 14 21:16:36 2008 +0100
+++ b/trunk/src/dil/ast/Declarations.d	Mon Jan 14 22:34:09 2008 +0100
@@ -11,12 +11,8 @@
 import dil.ast.Parameters;
 import dil.ast.BaseClass;
 import dil.lexer.IdTable;
-import dil.semantic.Scope;
-import dil.semantic.Analysis;
 import dil.semantic.Symbols;
-import dil.semantic.Types;
 import dil.Enums;
-import dil.Messages;
 import common;
 
 abstract class Declaration : Node
@@ -31,16 +27,6 @@
   StorageClass stc; /// The storage class of this declaration.
   Protection prot;  /// The protection attribute of this declaration.
 
-  void semantic(Scope sc)
-  {
-    foreach (node; this.children)
-    {
-      assert(node !is null);
-      if (node.category == NodeCategory.Declaration)
-        (cast(Declaration)cast(void*)node).semantic(sc);
-    }
-  }
-
   final bool isStatic()
   {
     return !!(stc & StorageClass.Static);
@@ -80,15 +66,6 @@
   {
     addChildren(ds.children);
   }
-
-  override void semantic(Scope scop)
-  {
-    foreach (node; this.children)
-    {
-      assert(node.category == NodeCategory.Declaration);
-      (cast(Declaration)cast(void*)node).semantic(scop);
-    }
-  }
 }
 
 /// Single semicolon.
@@ -98,9 +75,6 @@
   {
     mixin(set_kind);
   }
-
-  override void semantic(Scope)
-  {}
 }
 
 /++
@@ -114,9 +88,6 @@
   {
     mixin(set_kind);
   }
-
-  override void semantic(Scope)
-  {}
 }
 
 /// FQN = fully qualified name
@@ -205,21 +176,6 @@
     addChild(decl);
     this.decl = decl;
   }
-
-  override void semantic(Scope scop)
-  {
-    /+
-    decl.semantic(scop); // call semantic() or do SA in if statements?
-    if (auto fd = TryCast!(FunctionDeclaration)(decl))
-    {
-      // TODO: do SA here?
-    }
-    else if (auto vd = TryCast!(VariableDeclaration)(decl))
-    {
-      // TODO: do SA here?
-    }
-    +/
-  }
 }
 
 class TypedefDeclaration : Declaration
@@ -231,21 +187,6 @@
     addChild(decl);
     this.decl = decl;
   }
-
-  override void semantic(Scope scop)
-  {
-    /+
-    decl.semantic(scop); // call semantic() or do SA in if statements?
-    if (auto fd = TryCast!(FunctionDeclaration)(decl))
-    {
-      // TODO: do SA here?
-    }
-    else if (auto vd = TryCast!(VariableDeclaration)(decl))
-    {
-      // TODO: do SA here?
-    }
-    +/
-  }
 }
 
 class EnumDeclaration : Declaration
@@ -266,41 +207,6 @@
   }
 
   Enum symbol;
-
-  override void semantic(Scope scop)
-  {
-    /+
-    // Create the symbol.
-    symbol = new Enum(name, this);
-    // Type semantics.
-    Type type = Types.Int; // Default to integer.
-    if (baseType)
-      type = baseType.semantic(scop);
-    auto enumType = new EnumType(symbol, type);
-    // Set the base type of the enum symbol.
-    symbol.setType(enumType);
-    if (name)
-    { // Insert named enum into scope.
-      scop.insert(symbol, symbol.ident);
-      // Create new scope.
-      scop = scop.push(symbol);
-    }
-    // Semantic on members.
-    foreach (member; members)
-    {
-      auto value = member.value;
-      if (value)
-      {
-        // value = value.semantic(scop);
-        // value = value.evaluate();
-      }
-      auto variable = new Variable(StorageClass.Const, LinkageType.None, type, member.name, member);
-      scop.insert(variable, variable.ident);
-    }
-    if (name)
-      scop.pop();
-    +/
-  }
 }
 
 class EnumMember : Node
@@ -347,20 +253,6 @@
   }
 
   Class symbol; /// The class symbol for this declaration.
-
-  override void semantic(Scope scop)
-  {
-    if (symbol)
-      return;
-    symbol = new Class(name, this);
-    // Insert into current scope.
-    scop.insert(symbol, name);
-    // Create a new scope.
-    scop = scop.enter(symbol);
-    // Continue semantic analysis.
-    decls && decls.semantic(scop);
-    scop.exit();
-  }
 }
 
 class InterfaceDeclaration : AggregateDeclaration
@@ -380,20 +272,6 @@
   alias dil.semantic.Symbols.Interface Interface;
 
   Interface symbol; /// The interface symbol for this declaration.
-
-  override void semantic(Scope scop)
-  {
-    if (symbol)
-      return;
-    symbol = new Interface(name, this);
-    // Insert into current scope.
-    scop.insert(symbol, name);
-    // Create a new scope.
-    scop = scop.enter(symbol);
-    // Continue semantic analysis.
-    decls && decls.semantic(scop);
-    scop.exit();
-  }
 }
 
 class StructDeclaration : AggregateDeclaration
@@ -413,20 +291,6 @@
   }
 
   Struct symbol; /// The struct symbol for this declaration.
-
-  override void semantic(Scope scop)
-  {
-    if (symbol)
-      return;
-    symbol = new Struct(name, this);
-    // Insert into current scope.
-    scop.insert(symbol, name);
-    // Create a new scope.
-    scop = scop.enter(symbol);
-    // Continue semantic analysis.
-    decls && decls.semantic(scop);
-    scop.exit();
-  }
 }
 
 class UnionDeclaration : AggregateDeclaration
@@ -440,20 +304,6 @@
   }
 
   Union symbol; /// The union symbol for this declaration.
-
-  override void semantic(Scope scop)
-  {
-    if (symbol)
-      return;
-    symbol = new Union(name, this);
-    // Insert into current scope.
-    scop.insert(symbol, name);
-    // Create a new scope.
-    scop = scop.enter(symbol);
-    // Continue semantic analysis.
-    decls && decls.semantic(scop);
-    scop.exit();
-  }
 }
 
 class ConstructorDeclaration : Declaration
@@ -570,40 +420,6 @@
   }
 
   Variable[] variables;
-
-  override void semantic(Scope scop)
-  {
-    Type type;
-
-    if (typeNode)
-      // Get type from typeNode.
-      type = typeNode.semantic(scop);
-    else
-    { // Infer type from first initializer.
-      auto firstValue = values[0];
-//       firstValue = firstValue.semantic(scop);
-      type = firstValue.type;
-    }
-    assert(type !is null);
-
-    // Check for interface.
-    if (scop.isInterface)
-      return scop.error(begin, MSG.InterfaceCantHaveVariables);
-
-    // Iterate over variable identifiers in this declaration.
-    foreach (i, ident; idents)
-    {
-      // Perform semantic analysis on value.
-//       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);
-      variables ~= variable;
-      // Add to scope.
-      scop.insert(variable);
-    }
-  }
 }
 
 class InvariantDeclaration : Declaration
@@ -826,12 +642,6 @@
     this.ident = ident;
     this.args = args;
   }
-
-  override void semantic(Scope scop)
-  {
-    pragmaSemantic(scop, begin, ident, args);
-    decls.semantic(scop);
-  }
 }
 
 class MixinDeclaration : Declaration
--- a/trunk/src/dil/ast/Statements.d	Mon Jan 14 21:16:36 2008 +0100
+++ b/trunk/src/dil/ast/Statements.d	Mon Jan 14 22:34:09 2008 +0100
@@ -10,8 +10,6 @@
 import dil.ast.Types;
 import dil.ast.Parameters;
 import dil.lexer.IdTable;
-import dil.semantic.Scope;
-import dil.semantic.Analysis;
 
 abstract class Statement : Node
 {
@@ -19,11 +17,6 @@
   {
     super(NodeCategory.Statement);
   }
-
-  void semantic(Scope scop)
-  {
-
-  }
 }
 
 class Statements : Statement
@@ -488,12 +481,6 @@
     this.args = args;
     this.pragmaBody = pragmaBody;
   }
-
-  override void semantic(Scope scop)
-  {
-    pragmaSemantic(scop, begin, ident, args);
-    pragmaBody.semantic(scop);
-  }
 }
 
 class MixinStatement : Statement
--- a/trunk/src/dil/ast/Types.d	Mon Jan 14 21:16:36 2008 +0100
+++ b/trunk/src/dil/ast/Types.d	Mon Jan 14 22:34:09 2008 +0100
@@ -9,7 +9,6 @@
 import dil.ast.Parameters;
 import dil.lexer.Identifier;
 import dil.Enums;
-import dil.semantic.Scope;
 import dil.semantic.Types;
 
 enum TID
@@ -71,11 +70,6 @@
     this.tid = tid;
     this.next = next;
   }
-
-  Type semantic(Scope scop)
-  {
-    return Types.Error;
-  }
 }
 
 class UndefinedType : TypeNode
--- a/trunk/src/dil/ast/Visitor.d	Mon Jan 14 21:16:36 2008 +0100
+++ b/trunk/src/dil/ast/Visitor.d	Mon Jan 14 22:34:09 2008 +0100
@@ -23,7 +23,7 @@
   char[] text;
   foreach (className; classNames)
   {
-    text ~= "returnType!(\""~className~"\")" ~ " visit(" ~ className ~ ");\n";
+    text ~= "returnType!(\""~className~"\") visit("~className~");\n";
   }
   return text;
 }
@@ -100,6 +100,17 @@
     return cast(T function(Visitor,T))dispatch_vtable[n.kind];
   }
 
+  Declaration visit(Declaration n)
+  { return visitD(n); }
+  Statement visit(Statement n)
+  { return visitS(n); }
+  Expression visit(Expression n)
+  { return visitE(n); }
+  TypeNode visit(TypeNode n)
+  { return visitT(n); }
+  Node visit(Node n)
+  { return visitN(n); }
+
   Declaration visitD(Declaration n)
   {
     // Do first dispatch. Second dispatch is done in the called function.
--- a/trunk/src/dil/semantic/Module.d	Mon Jan 14 21:16:36 2008 +0100
+++ b/trunk/src/dil/semantic/Module.d	Mon Jan 14 22:34:09 2008 +0100
@@ -94,7 +94,7 @@
     auto scop = new Scope();
     scop.symbol = this; // Set this module as the scope's symbol.
     scop.infoMan = this.infoMan;
-    this.root.semantic(scop);
+//     this.root.semantic(scop);
   }
 
   /// Returns true if there are errors in the source file.
--- a/trunk/src/dil/semantic/Pass1.d	Mon Jan 14 21:16:36 2008 +0100
+++ b/trunk/src/dil/semantic/Pass1.d	Mon Jan 14 22:34:09 2008 +0100
@@ -12,12 +12,16 @@
        dil.ast.Types,
        dil.ast.BaseClass,
        dil.ast.Parameters;
-
 import dil.semantic.Symbol,
        dil.semantic.Symbols,
        dil.semantic.Types,
        dil.semantic.Scope,
-       dil.semantic.Module;
+       dil.semantic.Module,
+       dil.semantic.Analysis;
+import dil.Location;
+import dil.Information;
+import dil.Messages;
+import common;
 
 class SemanticPass1 : Visitor
 {
@@ -46,6 +50,13 @@
     scop = scop.exit();
   }
 
+  void error(Token* token, char[] formatMsg, ...)
+  {
+    auto location = token.getLocation();
+    auto msg = Format(_arguments, _argptr, formatMsg);
+    modul.infoMan ~= new SemanticError(location, msg);
+  }
+
 override
 {
   Declaration visit(Declarations d)
@@ -60,18 +71,83 @@
 
   Declaration visit(IllegalDeclaration)
   { assert(0, "semantic pass on invalid AST"); return null; }
-  Declaration visit(EmptyDeclaration)
-  { return null; }
+
+  Declaration visit(EmptyDeclaration ed)
+  { return ed; }
+
   Declaration visit(ModuleDeclaration)
   { return null; }
   Declaration visit(ImportDeclaration)
   { return null; }
-  Declaration visit(AliasDeclaration)
-  { return null; }
-  Declaration visit(TypedefDeclaration)
-  { return null; }
-  Declaration visit(EnumDeclaration)
-  { return null; }
+
+//   Declaration visit(AliasDeclaration ad)
+//   {
+    /+
+    decl.semantic(scop); // call semantic() or do SA in if statements?
+    if (auto fd = TryCast!(FunctionDeclaration)(decl))
+    {
+      // TODO: do SA here?
+    }
+    else if (auto vd = TryCast!(VariableDeclaration)(decl))
+    {
+      // TODO: do SA here?
+    }
+    +/
+//     return ad;
+//   }
+
+  Declaration visit(TypedefDeclaration td)
+  {
+    /+
+    decl.semantic(scop); // call semantic() or do SA in if statements?
+    if (auto fd = TryCast!(FunctionDeclaration)(decl))
+    {
+      // TODO: do SA here?
+    }
+    else if (auto vd = TryCast!(VariableDeclaration)(decl))
+    {
+      // TODO: do SA here?
+    }
+    +/
+    return td;
+  }
+
+  Declaration visit(EnumDeclaration ed)
+  {
+    /+
+    // Create the symbol.
+    symbol = new Enum(name, this);
+    // Type semantics.
+    Type type = Types.Int; // Default to integer.
+    if (baseType)
+      type = baseType.semantic(scop);
+    auto enumType = new EnumType(symbol, type);
+    // Set the base type of the enum symbol.
+    symbol.setType(enumType);
+    if (name)
+    { // Insert named enum into scope.
+      scop.insert(symbol, symbol.ident);
+      // Create new scope.
+      scop = scop.push(symbol);
+    }
+    // Semantic on members.
+    foreach (member; members)
+    {
+      auto value = member.value;
+      if (value)
+      {
+        // value = value.semantic(scop);
+        // value = value.evaluate();
+      }
+      auto variable = new Variable(StorageClass.Const, LinkageType.None, type, member.name, member);
+      scop.insert(variable, variable.ident);
+    }
+    if (name)
+      scop.pop();
+    +/
+    return ed;
+  }
+
   Declaration visit(EnumMember)
   { return null; }
 
@@ -141,8 +217,42 @@
   { return null; }
   Declaration visit(FunctionDeclaration)
   { return null; }
-  Declaration visit(VariableDeclaration)
-  { return null; }
+
+  Declaration visit(VariableDeclaration vd)
+  {
+    Type type;
+
+    if (vd.typeNode)
+      // Get type from typeNode.
+      type = Types.Undefined; // visitN(d.typeNode);
+    else
+    { // Infer type from first initializer.
+      auto firstValue = vd.values[0];
+      firstValue = visitE(firstValue);
+      type = firstValue.type;
+    }
+    assert(type !is null);
+
+    // Check for interface.
+    if (scop.isInterface)
+      return error(vd.begin, MSG.InterfaceCantHaveVariables), vd;
+
+    // Iterate over variable identifiers in this declaration.
+    foreach (i, ident; vd.idents)
+    {
+      // Perform semantic analysis on value.
+      if (vd.values[i])
+        vd.values[i] = visitE(vd.values[i]);
+      // Create a new variable symbol.
+      // TODO: pass 'prot' to constructor.
+      auto variable = new Variable(vd.stc, vd.linkageType, type, ident, vd);
+      vd.variables ~= variable;
+      // Add to scope.
+      scop.insert(variable);
+    }
+    return vd;
+  }
+
   Declaration visit(InvariantDeclaration)
   { return null; }
   Declaration visit(UnittestDeclaration)
@@ -171,8 +281,21 @@
   { return null; }
   Declaration visit(AlignDeclaration)
   { return null; }
-  Declaration visit(PragmaDeclaration)
-  { return null; }
+
+  Declaration visit(PragmaDeclaration d)
+  {
+    pragmaSemantic(scop, d.begin, d.ident, d.args);
+    visitD(d.decls);
+    return d;
+  }
+
+  Statement visit(PragmaStatement s)
+  {
+    pragmaSemantic(scop, s.begin, s.ident, s.args);
+    visitS(s.pragmaBody);
+    return s;
+  }
+
   Declaration visit(MixinDeclaration)
   { return null; }