# HG changeset patch # User Aziz K?ksal # Date 1199656729 -3600 # Node ID 6d449e777f5dac7c1bb6c552ab6cc13cc2f9b4bc # Parent f58fd84c0d1881ef43e1f09a63966c3ca4d75814 Added semantic code to insert symbols into the scope. diff -r f58fd84c0d18 -r 6d449e777f5d trunk/src/dil/Messages.d --- a/trunk/src/dil/Messages.d Sun Jan 06 21:07:37 2008 +0100 +++ b/trunk/src/dil/Messages.d Sun Jan 06 22:58:49 2008 +0100 @@ -143,6 +143,7 @@ auto ExpectedNameForThisTempParam = "expected name for 'this' template parameter, not '{}'"; auto ExpectedIdentOrInt = "expected an identifier or an integer, not '{}'"; // Semantic analysis: + auto DeclConflictsWithDecl = "declaration '{}' conflicts with declaration @{}"; auto VariableConflictsWithDecl = "variable '{}' conflicts with declaration @{}"; auto InterfaceCantHaveVariables = "an interface can't have member variables"; } diff -r f58fd84c0d18 -r 6d449e777f5d trunk/src/dil/ast/Declarations.d --- a/trunk/src/dil/ast/Declarations.d Sun Jan 06 21:07:37 2008 +0100 +++ b/trunk/src/dil/ast/Declarations.d Sun Jan 06 22:58:49 2008 +0100 @@ -106,7 +106,7 @@ /++ Illegal declarations encompass all tokens that don't start a DeclarationDefinition. - See_Also: dil.Token.isDeclDefStartToken() + See_Also: dil.lexer.Token.isDeclDefStartToken() +/ class IllegalDeclaration : Declaration { @@ -279,15 +279,17 @@ this.bases = bases; } - Class class_; /// The class symbol for this declaration. + Class symbol; /// The class symbol for this declaration. override void semantic(Scope scop) { - if (class_) + if (symbol) return; - class_ = new Class(name, this); + symbol = new Class(name, this); + // Insert into current scope. + scop.insert(symbol, name); // Create a new scope. - scop = scop.push(class_); + scop = scop.push(symbol); // Continue semantic analysis. decls && decls.semantic(scop); scop.pop(); @@ -310,15 +312,17 @@ alias dil.semantic.Symbols.Interface Interface; - Interface interface_; /// The interface symbol for this declaration. + Interface symbol; /// The interface symbol for this declaration. override void semantic(Scope scop) { - if (interface_) + if (symbol) return; - interface_ = new Interface(name, this); + symbol = new Interface(name, this); + // Insert into current scope. + scop.insert(symbol, name); // Create a new scope. - scop = scop.push(interface_); + scop = scop.push(symbol); // Continue semantic analysis. decls && decls.semantic(scop); scop.pop(); @@ -341,15 +345,17 @@ this.alignSize = alignSize; } - Struct struct_; /// The struct symbol for this declaration. + Struct symbol; /// The struct symbol for this declaration. override void semantic(Scope scop) { - if (struct_) + if (symbol) return; - struct_ = new Struct(name, this); + symbol = new Struct(name, this); + // Insert into current scope. + scop.insert(symbol, name); // Create a new scope. - scop = scop.push(struct_); + scop = scop.push(symbol); // Continue semantic analysis. decls && decls.semantic(scop); scop.pop(); @@ -366,15 +372,17 @@ addOptChild(decls); } - Union union_; /// The union symbol for this declaration. + Union symbol; /// The union symbol for this declaration. override void semantic(Scope scop) { - if (union_) + if (symbol) return; - union_ = new Union(name, this); + symbol = new Union(name, this); + // Insert into current scope. + scop.insert(symbol, name); // Create a new scope. - scop = scop.push(union_); + scop = scop.push(symbol); // Continue semantic analysis. decls && decls.semantic(scop); scop.pop(); diff -r f58fd84c0d18 -r 6d449e777f5d trunk/src/dil/semantic/Scope.d --- a/trunk/src/dil/semantic/Scope.d Sun Jan 06 21:07:37 2008 +0100 +++ b/trunk/src/dil/semantic/Scope.d Sun Jan 06 22:58:49 2008 +0100 @@ -7,6 +7,7 @@ import dil.semantic.Symbol; import dil.semantic.Symbols; import dil.lexer.Token; +import dil.lexer.Identifier; import dil.Information; import dil.Messages; import common; @@ -30,12 +31,20 @@ return null; } - /++ - Add a symbol to this scope. - +/ - void add(Symbol sym) + /// Insert a symbol into this scope. + void insert(Symbol sym, Identifier* ident) { - + auto sym2 = symbol.lookup(ident); + if (sym2) + { + auto loc = sym2.node.begin.getLocation(); + 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.