Mercurial > projects > dang
comparison sema/BuildScopes.d @ 194:08f68d684047
Rename some files. Hopefully we can get a more iterative sema pass, that's a lot easier to "get startet with".
Also added support for alias.
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Tue, 29 Jul 2008 13:54:44 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
193:658178183018 | 194:08f68d684047 |
---|---|
1 module sema.BuildScopes; | |
2 | |
3 import tango.io.Stdout, | |
4 tango.core.Array : find; | |
5 | |
6 import sema.Scope; | |
7 | |
8 import sema.Visitor; | |
9 | |
10 /** | |
11 Add scopes to everything, and add all identifiers that correspond to types. | |
12 Types/Symbols are added by ForwardReference. | |
13 **/ | |
14 class BuildScopes : Visitor!(void) | |
15 { | |
16 static ModuleHandler mHandle; | |
17 | |
18 static this() | |
19 { | |
20 mHandle = new ModuleHandler; | |
21 } | |
22 | |
23 override void visit(Module[] modules) | |
24 { | |
25 foreach(m ; modules) | |
26 visitModule(m); | |
27 } | |
28 | |
29 private void registerBasicTypeTo(char[] n, DType t, Scope sc, Module m) | |
30 { | |
31 sc.types[n] = t; | |
32 auto id = new Identifier(n); | |
33 id.env = sc; | |
34 auto decl = new DummyDecl(); | |
35 auto sym = m.symbol.createMember(n, t, decl); | |
36 sym.decl = decl; | |
37 decl.sym = sym; | |
38 decl.env = sc; | |
39 sc.put(id.get, decl); | |
40 } | |
41 | |
42 override void visitModule(Module m) | |
43 { | |
44 auto root = new Scope; | |
45 table ~= root; | |
46 | |
47 m.symbol = new Symbol; | |
48 | |
49 registerBasicTypeTo("void", DType.Void, root, m); | |
50 registerBasicTypeTo("bool", DType.Bool, root, m); | |
51 registerBasicTypeTo("byte", DType.Byte, root, m); | |
52 registerBasicTypeTo("ubyte", DType.UByte, root, m); | |
53 registerBasicTypeTo("short", DType.Short, root, m); | |
54 registerBasicTypeTo("ushort", DType.UShort, root, m); | |
55 registerBasicTypeTo("int", DType.Int, root, m); | |
56 registerBasicTypeTo("uint", DType.UInt, root, m); | |
57 registerBasicTypeTo("long", DType.Long, root, m); | |
58 registerBasicTypeTo("ulong", DType.ULong, root, m); | |
59 | |
60 registerBasicTypeTo("char", DType.Char, root, m); | |
61 registerBasicTypeTo("wchar", DType.WChar, root, m); | |
62 registerBasicTypeTo("dchar", DType.DChar, root, m); | |
63 | |
64 registerBasicTypeTo("float", DType.Float, root, m); | |
65 registerBasicTypeTo("double", DType.Double, root, m); | |
66 registerBasicTypeTo("real", DType.Real, root, m); | |
67 | |
68 current().inModule = m; | |
69 current().mHandle = mHandle; | |
70 mHandle.add(m); | |
71 m.env = current(); | |
72 super.visitModule(m); | |
73 } | |
74 | |
75 override void visitDecl(Decl d) | |
76 { | |
77 d.env = current(); | |
78 super.visitDecl(d); | |
79 } | |
80 | |
81 override void visitImportDecl(ImportDecl i) | |
82 { | |
83 i.env.imports ~= i; | |
84 super.visitImportDecl(i); | |
85 } | |
86 | |
87 override void visitStmt(Stmt s) | |
88 { | |
89 s.env = current(); | |
90 s.stmtIndex = s.env.stmtIndex; | |
91 super.visitStmt(s); | |
92 } | |
93 | |
94 override void visitExp(Exp e) | |
95 { | |
96 e.env = current(); | |
97 e.stmtIndex = e.env.stmtIndex; | |
98 super.visitExp(e); | |
99 } | |
100 | |
101 override void visitFuncDecl(FuncDecl d) | |
102 { | |
103 current().put(d.identifier.get, d); | |
104 d.env = current(); | |
105 auto sc = push(); | |
106 | |
107 visitExp(d.returnType); | |
108 visitExp(d.identifier); | |
109 sc.parentFunction = d; | |
110 foreach (arg; d.funcArgs) | |
111 visitDecl(arg); | |
112 foreach (stmt; d.statements) | |
113 { | |
114 sc.currentStmtIndex++; | |
115 visitStmt(stmt); | |
116 } | |
117 pop(sc); | |
118 } | |
119 | |
120 override void visitVarDecl(VarDecl d) | |
121 { | |
122 if (d.init) | |
123 visitExp(d.init); | |
124 | |
125 if (need_push > 0 && current().parentFunction !is null) { | |
126 push(); | |
127 --need_push; | |
128 } | |
129 | |
130 if(d.identifier) | |
131 { | |
132 auto sc = current(); | |
133 sc.put(d.identifier.get, d); | |
134 d.env = sc; | |
135 visitExp(d.varType); | |
136 visitExp(d.identifier); | |
137 } | |
138 } | |
139 | |
140 override void visitStructDecl(StructDecl s) | |
141 { | |
142 auto sc = current(); | |
143 sc.put(s.identifier.get, s); | |
144 s.env = sc; | |
145 auto type = new DStruct(s.identifier); | |
146 | |
147 sc.types[s.identifier.get] = type; | |
148 | |
149 sc = push(); | |
150 super.visitStructDecl(s); | |
151 pop(sc); | |
152 } | |
153 | |
154 override void visitClassDecl(ClassDecl s) | |
155 { | |
156 auto sc = current(); | |
157 sc.put(s.identifier.get, s); | |
158 s.env = sc; | |
159 auto type = new DClass(s.identifier); | |
160 | |
161 sc.types[s.identifier.get] = type; | |
162 | |
163 sc = push(); | |
164 super.visitClassDecl(s); | |
165 pop(sc); | |
166 } | |
167 | |
168 override void visitInterfaceDecl(InterfaceDecl s) | |
169 { | |
170 auto sc = current(); | |
171 sc.put(s.identifier.get, s); | |
172 s.env = sc; | |
173 auto type = new DInterface(s.identifier); | |
174 | |
175 sc.types[s.identifier.get] = type; | |
176 | |
177 sc = push(); | |
178 super.visitInterfaceDecl(s); | |
179 pop(sc); | |
180 } | |
181 | |
182 override void visitAliasDecl(AliasDecl a) | |
183 { | |
184 a.env = current(); | |
185 // auto decl = cast(VarDecl)a.decl; | |
186 // auto sc = current(); | |
187 // super.visitDecl(a.decl); | |
188 } | |
189 | |
190 override void visitDeclStmt(DeclStmt d) | |
191 { | |
192 ++need_push; | |
193 super.visitDeclStmt(d); | |
194 } | |
195 private uint need_push = 0; | |
196 | |
197 override void visitIfStmt(IfStmt s) | |
198 { | |
199 s.env = current(); | |
200 visitExp(s.cond); | |
201 auto sc = push(); | |
202 visitStmt(s.then_body); | |
203 pop(sc); | |
204 | |
205 if (s.else_body !is null) | |
206 { | |
207 sc = push(); | |
208 visitStmt(s.else_body); | |
209 pop(sc); | |
210 } | |
211 } | |
212 | |
213 override void visitWhileStmt(WhileStmt s) | |
214 { | |
215 s.env = current(); | |
216 auto sc = push(); | |
217 super.visitWhileStmt(s); | |
218 pop(sc); | |
219 } | |
220 | |
221 override void visitForStmt(ForStmt s) | |
222 { | |
223 s.env = current(); | |
224 auto sc = push(); | |
225 super.visitForStmt(s); | |
226 pop(sc); | |
227 } | |
228 | |
229 override void visitCompoundStmt(CompoundStatement s) | |
230 { | |
231 s.env = current(); | |
232 auto sc = push(); | |
233 super.visitCompoundStmt(s); | |
234 pop(sc); | |
235 } | |
236 | |
237 private: | |
238 Scope[] table; | |
239 | |
240 Scope push() | |
241 { | |
242 auto sc = new Scope(current()); | |
243 table ~= sc; | |
244 return sc; | |
245 } | |
246 | |
247 Scope pop(Scope sc = null) | |
248 { | |
249 if (sc !is null) | |
250 { | |
251 table.length = table.find(sc); | |
252 return sc; | |
253 } | |
254 | |
255 auto res = table[$ - 1]; | |
256 table.length = table.length - 1; | |
257 return res; | |
258 } | |
259 | |
260 Scope current() | |
261 { | |
262 return table[$ - 1]; | |
263 } | |
264 } | |
265 |