Mercurial > projects > dil
diff trunk/src/dil/semantic/Scope.d @ 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 | 1ae72234db26 |
children | c24be8d4f6ab |
line wrap: on
line diff
--- 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); - } }