Mercurial > projects > dil
comparison 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 |
comparison
equal
deleted
inserted
replaced
691:276e2866f5fd | 692:d33895f679eb |
---|---|
4 +/ | 4 +/ |
5 module dil.semantic.Scope; | 5 module dil.semantic.Scope; |
6 | 6 |
7 import dil.semantic.Symbol; | 7 import dil.semantic.Symbol; |
8 import dil.semantic.Symbols; | 8 import dil.semantic.Symbols; |
9 import dil.lexer.Token; | |
10 import dil.lexer.Identifier; | 9 import dil.lexer.Identifier; |
11 import dil.Information; | |
12 import dil.Messages; | |
13 import common; | 10 import common; |
14 | 11 |
15 class Scope | 12 class Scope |
16 { | 13 { |
17 Scope parent; /// The surrounding scope. | 14 Scope parent; /// The surrounding scope, or null if this is the root scope. |
18 InfoManager infoMan; /// Collects errors reported during the semantic phase. | |
19 | 15 |
20 ScopeSymbol symbol; /// The current symbol with the symbol table. | 16 ScopeSymbol symbol; /// The current symbol with the symbol table. |
21 | 17 |
22 this() | 18 this(Scope parent, ScopeSymbol symbol) |
23 { | 19 { |
20 this.parent = parent; | |
21 this.symbol = symbol; | |
24 } | 22 } |
25 | 23 |
26 /++ | 24 /++ |
27 Find an identifier in this scope. | 25 Find a symbol in this scope. |
26 Params: | |
27 name = the name of the symbol. | |
28 +/ | 28 +/ |
29 Symbol find(char[] ident) | 29 Symbol lookup(Identifier* name) |
30 { | 30 { |
31 return null; | 31 return symbol.lookup(name); |
32 } | |
33 | |
34 /// Insert a symbol into this scope. | |
35 void insert(Symbol sym, Identifier* ident) | |
36 { | |
37 auto sym2 = symbol.lookup(ident); | |
38 if (sym2) | |
39 { | |
40 auto loc = sym2.node.begin.getErrorLocation(); | |
41 auto locString = Format("{}({},{})", loc.filePath, loc.lineNum, loc.colNum); | |
42 error(sym.node.begin, MSG.DeclConflictsWithDecl, ident.str, locString); | |
43 } | |
44 else | |
45 symbol.insert(sym, ident); | |
46 // Set the current scope symbol as the parent. | |
47 sym.parent = symbol; | |
48 } | |
49 | |
50 /// Insert a new variable symbol into this scope. | |
51 void insert(Variable var) | |
52 { | |
53 auto sym = symbol.lookup(var.name); | |
54 if (sym) | |
55 { | |
56 auto loc = sym.node.begin.getErrorLocation(); | |
57 auto locString = Format("{}({},{})", loc.filePath, loc.lineNum, loc.colNum); | |
58 error(var.node.begin, MSG.VariableConflictsWithDecl, var.name.str, locString); | |
59 } | |
60 else | |
61 symbol.insert(var, var.name); | |
62 // Set the current scope symbol as the parent. | |
63 var.parent = symbol; | |
64 } | 32 } |
65 | 33 |
66 /++ | 34 /++ |
67 Create and enter a new inner scope. | 35 Create a new inner scope and return that. |
68 +/ | 36 +/ |
69 Scope enter(ScopeSymbol symbol) | 37 Scope enter(ScopeSymbol symbol) |
70 { | 38 { |
71 auto sc = new Scope(); | 39 return new Scope(this, symbol); |
72 sc.parent = this; | |
73 sc.infoMan = this.infoMan; | |
74 sc.symbol = symbol; | |
75 return sc; | |
76 } | 40 } |
77 | 41 |
78 /++ | 42 /++ |
79 Destroy this scope and return the outer scope. | 43 Destroy this scope and return the outer scope. |
80 +/ | 44 +/ |
83 auto sc = parent; | 47 auto sc = parent; |
84 // delete this; | 48 // delete this; |
85 return sc; | 49 return sc; |
86 } | 50 } |
87 | 51 |
88 bool isInterface() | |
89 { | |
90 return symbol.isInterface; | |
91 } | |
92 | |
93 /// Search for the enclosing Class scope. | 52 /// Search for the enclosing Class scope. |
94 Scope classScope() | 53 Scope classScope() |
95 { | 54 { |
96 auto scop = this; | 55 auto scop = this; |
97 while (scop) | 56 do |
98 { | 57 { |
99 if (scop.symbol.isClass) | 58 if (scop.symbol.isClass) |
100 return scop; | 59 return scop; |
101 scop = scop.parent; | 60 scop = scop.parent; |
102 } | 61 } while (scop) |
103 return null; | 62 return null; |
104 } | 63 } |
105 | 64 |
106 /// Search for the enclosing Module scope. | 65 /// Search for the enclosing Module scope. |
107 Scope moduleScope() | 66 Scope moduleScope() |
108 { | 67 { |
109 auto scop = this; | 68 auto scop = this; |
110 while (scop) | 69 do |
111 { | 70 { |
112 if (scop.symbol.isModule) | 71 if (scop.symbol.isModule) |
113 return scop; | 72 return scop; |
114 scop = scop.parent; | 73 scop = scop.parent; |
115 } | 74 } while (scop) |
116 return null; | 75 return null; |
117 } | 76 } |
118 | |
119 void error(Token* token, MID mid) | |
120 { | |
121 auto location = token.getErrorLocation(); | |
122 infoMan ~= new SemanticError(location, GetMsg(mid)); | |
123 } | |
124 | |
125 void error(Token* token, char[] formatMsg, ...) | |
126 { | |
127 auto location = token.getErrorLocation(); | |
128 auto msg = Format(_arguments, _argptr, formatMsg); | |
129 infoMan ~= new SemanticError(location, msg); | |
130 } | |
131 } | 77 } |