# HG changeset patch # User Aziz K?ksal # Date 1201023159 -3600 # Node ID d33895f679ebece4db267a225b224c01e2b9129b # Parent 276e2866f5fd37eb5bb56b8bf938d4802932858f Tidied up the class Scope a bit. diff -r 276e2866f5fd -r d33895f679eb trunk/src/dil/semantic/Pass1.d --- 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. diff -r 276e2866f5fd -r d33895f679eb trunk/src/dil/semantic/Pass2.d --- 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. } diff -r 276e2866f5fd -r d33895f679eb trunk/src/dil/semantic/Scope.d --- 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); - } } diff -r 276e2866f5fd -r d33895f679eb trunk/src/dil/semantic/Types.d --- 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;