changeset 692:d33895f679eb

Tidied up the class Scope a bit.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Tue, 22 Jan 2008 18:32:39 +0100
parents 276e2866f5fd
children 56100b270897
files trunk/src/dil/semantic/Pass1.d trunk/src/dil/semantic/Pass2.d trunk/src/dil/semantic/Scope.d trunk/src/dil/semantic/Types.d
diffstat 4 files changed, 27 insertions(+), 77 deletions(-) [+]
line wrap: on
line diff
--- a/trunk/src/dil/semantic/Pass1.d	Tue Jan 22 18:05:37 2008 +0100
+++ b/trunk/src/dil/semantic/Pass1.d	Tue Jan 22 18:32:39 2008 +0100
@@ -57,9 +57,7 @@
   {
     assert(modul.root !is null);
     // Create module scope.
-    scop = new Scope();
-    scop.symbol = modul; // Set this module as the scope's symbol.
-    scop.infoMan = modul.infoMan;
+    scop = new Scope(null, modul);
     visit(modul.root);
   }
 
@@ -298,7 +296,7 @@
   D visit(VariablesDeclaration vd)
   {
     // Error if we are in an interface.
-    if (scop.isInterface)
+    if (scop.symbol.isInterface)
       return error(vd.begin, MSG.InterfaceCantHaveVariables), vd;
 
     // Insert variable symbols in this declaration into the symbol table.
--- a/trunk/src/dil/semantic/Pass2.d	Tue Jan 22 18:05:37 2008 +0100
+++ b/trunk/src/dil/semantic/Pass2.d	Tue Jan 22 18:32:39 2008 +0100
@@ -41,9 +41,7 @@
   {
     assert(modul.root !is null);
     // Create module scope.
-    scop = new Scope();
-    scop.symbol = modul; // Set this module as the scope's symbol.
-    scop.infoMan = modul.infoMan;
+    scop = new Scope(null, modul);
     visit(modul.root);
   }
 
@@ -104,6 +102,11 @@
     return md;
   }
 
+  T visit(ArrayType t)
+  {
+    return t;
+  }
+
   T visit(PointerType t)
   {
     t.type = visitT(t.next).type.ptrTo();
@@ -261,7 +264,7 @@
     {
       auto loc = me.begin.getErrorLocation();
       auto filePath = loc.filePath;
-      auto parser = new Parser(stringExpr.getString(), filePath, scop.infoMan);
+      auto parser = new Parser(stringExpr.getString(), filePath, modul.infoMan);
       expr = parser.start2();
       expr = visitE(expr); // Check expression.
     }
--- a/trunk/src/dil/semantic/Scope.d	Tue Jan 22 18:05:37 2008 +0100
+++ b/trunk/src/dil/semantic/Scope.d	Tue Jan 22 18:32:39 2008 +0100
@@ -6,73 +6,37 @@
 
 import dil.semantic.Symbol;
 import dil.semantic.Symbols;
-import dil.lexer.Token;
 import dil.lexer.Identifier;
-import dil.Information;
-import dil.Messages;
 import common;
 
 class Scope
 {
-  Scope parent; /// The surrounding scope.
-  InfoManager infoMan; /// Collects errors reported during the semantic phase.
+  Scope parent; /// The surrounding scope, or null if this is the root scope.
 
   ScopeSymbol symbol; /// The current symbol with the symbol table.
 
-  this()
+  this(Scope parent, ScopeSymbol symbol)
   {
+    this.parent = parent;
+    this.symbol = symbol;
   }
 
   /++
-    Find an identifier in this scope.
+    Find a symbol in this scope.
+    Params:
+      name = the name of the symbol.
   +/
-  Symbol find(char[] ident)
-  {
-    return null;
-  }
-
-  /// Insert a symbol into this scope.
-  void insert(Symbol sym, Identifier* ident)
+  Symbol lookup(Identifier* name)
   {
-    auto sym2 = symbol.lookup(ident);
-    if (sym2)
-    {
-      auto loc = sym2.node.begin.getErrorLocation();
-      auto locString = Format("{}({},{})", loc.filePath, loc.lineNum, loc.colNum);
-      error(sym.node.begin, MSG.DeclConflictsWithDecl, ident.str, locString);
-    }
-    else
-      symbol.insert(sym, ident);
-    // Set the current scope symbol as the parent.
-    sym.parent = symbol;
-  }
-
-  /// Insert a new variable symbol into this scope.
-  void insert(Variable var)
-  {
-    auto sym = symbol.lookup(var.name);
-    if (sym)
-    {
-      auto loc = sym.node.begin.getErrorLocation();
-      auto locString = Format("{}({},{})", loc.filePath, loc.lineNum, loc.colNum);
-      error(var.node.begin, MSG.VariableConflictsWithDecl, var.name.str, locString);
-    }
-    else
-      symbol.insert(var, var.name);
-    // Set the current scope symbol as the parent.
-    var.parent = symbol;
+    return symbol.lookup(name);
   }
 
   /++
-    Create and enter a new inner scope.
+    Create a new inner scope and return that.
   +/
   Scope enter(ScopeSymbol symbol)
   {
-    auto sc = new Scope();
-    sc.parent = this;
-    sc.infoMan = this.infoMan;
-    sc.symbol = symbol;
-    return sc;
+    return new Scope(this, symbol);
   }
 
   /++
@@ -85,21 +49,16 @@
     return sc;
   }
 
-  bool isInterface()
-  {
-    return symbol.isInterface;
-  }
-
   /// Search for the enclosing Class scope.
   Scope classScope()
   {
     auto scop = this;
-    while (scop)
+    do
     {
       if (scop.symbol.isClass)
         return scop;
       scop = scop.parent;
-    }
+    } while (scop)
     return null;
   }
 
@@ -107,25 +66,12 @@
   Scope moduleScope()
   {
     auto scop = this;
-    while (scop)
+    do
     {
       if (scop.symbol.isModule)
         return scop;
       scop = scop.parent;
-    }
+    } while (scop)
     return null;
   }
-
-  void error(Token* token, MID mid)
-  {
-    auto location = token.getErrorLocation();
-    infoMan ~= new SemanticError(location, GetMsg(mid));
-  }
-
-  void error(Token* token, char[] formatMsg, ...)
-  {
-    auto location = token.getErrorLocation();
-    auto msg = Format(_arguments, _argptr, formatMsg);
-    infoMan ~= new SemanticError(location, msg);
-  }
 }
--- a/trunk/src/dil/semantic/Types.d	Tue Jan 22 18:05:37 2008 +0100
+++ b/trunk/src/dil/semantic/Types.d	Tue Jan 22 18:32:39 2008 +0100
@@ -50,6 +50,7 @@
   }
 }
 
+/// Dynamic array.
 class TypeDArray : Type
 {
   this(Type next)
@@ -58,6 +59,7 @@
   }
 }
 
+/// Associative array.
 class TypeAArray : Type
 {
   Type keyType;
@@ -68,6 +70,7 @@
   }
 }
 
+/// Static array.
 class TypeSArray : Type
 {
   size_t dimension;