comparison sema/ScopeBuilder.d @ 172:01c2c49775ef

- Changed Parser to be more clean on type parsing. - Parsing int function(int), and the like, types.(Function pointers) - Fixed a design fault that made Symbol be wrong. Now Symbols are created with a factory with the help of types.
author Anders Johnsen <skabet@gmail.com>
date Thu, 24 Jul 2008 20:31:24 +0200
parents 7982eb63c0eb
children dc9bf56b7ace
comparison
equal deleted inserted replaced
171:f0385c044065 172:01c2c49775ef
8 sema.Symbol; 8 sema.Symbol;
9 9
10 import sema.Visitor, 10 import sema.Visitor,
11 basic.SmallArray; 11 basic.SmallArray;
12 12
13 class SymbolFactory
14 {
15 void put(Symbol symbol, DType type)
16 {
17 types[type] = symbol;
18 }
19
20 Symbol get(Symbol owner, DType type, Identifier i, Decl decl)
21 {
22 if (type in types)
23 return owner.createAlias(i.get, types[type], decl);
24 else
25 return owner.createMember(i.get, type, decl);
26 }
27
28 Symbol[DType] types;
29 }
30
13 class ForwardReference : Visitor!(void) 31 class ForwardReference : Visitor!(void)
14 { 32 {
33 this(SymbolFactory sf)
34 {
35 this.sf = sf;
36 }
15 override void visit(Module[] modules) 37 override void visit(Module[] modules)
16 { 38 {
17 (new TypeBuilder).visit(modules); 39 (new TypeBuilder(sf)).visit(modules);
18 this.modules = modules; 40 this.modules = modules;
19 inFunctionBodyStack.push(false); 41 inFunctionBodyStack.push(false);
20 foreach (mod; modules) 42 foreach (mod; modules)
21 { 43 {
22 current = mod; 44 current = mod;
60 82
61 if (d.init) 83 if (d.init)
62 visitExp(d.init); 84 visitExp(d.init);
63 85
64 DType t = typeOf(d.varType, d.env); 86 DType t = typeOf(d.varType, d.env);
65 d.sym = current.symbol.createAlias( 87 d.sym = sf.get(current.symbol, t, d.identifier, d);
66 d.identifier.get,
67 d.env.find(d.varType.get)[0].sym,
68 d);
69 d.sym.type = t; 88 d.sym.type = t;
70 } 89 }
71 90
72 override void visitStructDecl(StructDecl s) 91 override void visitStructDecl(StructDecl s)
73 { 92 {
97 super.visitInterfaceDecl(s); 116 super.visitInterfaceDecl(s);
98 inFunctionBodyStack.pop(); 117 inFunctionBodyStack.pop();
99 current.symbol = old; 118 current.symbol = old;
100 } 119 }
101 120
121 override void visitFunctionTypeExp(FunctionTypeExp f)
122 {
123 }
124
102 DType typeOf(Identifier id, Scope sc) 125 DType typeOf(Identifier id, Scope sc)
103 { 126 {
104 if(auto i = cast(PointerIdentifier)id) 127 if(auto i = cast(PointerTypeExp)id)
105 return (typeOf(i.pointerOf, sc)).getPointerTo(); 128 return (typeOf(i.pointerOf, sc)).getPointerTo();
106 else if(auto i = cast(StaticArrayIdentifier)id) 129 else if(auto i = cast(StaticArrayTypeExp)id)
107 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size); 130 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size);
131 else if(auto i = cast(FunctionTypeExp)id)
132 {
133 auto d = new DFunction(id);
134 d.returnType = typeOf(i.returnType, sc);
135 foreach (decl ; i.decls)
136 d.params ~= typeOf(decl.varType, sc);
137 return d;
138 }
108 return sc.findType(id.get); 139 return sc.findType(id.get);
109 } 140 }
110 141
111 Module[] modules; 142 Module[] modules;
112 Module current; 143 Module current;
113 SmallArray!(bool) inFunctionBodyStack; 144 SmallArray!(bool) inFunctionBodyStack;
145 SymbolFactory sf;
114 } 146 }
115 147
116 class TypeBuilder : Visitor!(void) 148 class TypeBuilder : Visitor!(void)
117 { 149 {
150 this(SymbolFactory sf)
151 {
152 this.sf = sf;
153 }
154
118 override void visit(Module[] modules) 155 override void visit(Module[] modules)
119 { 156 {
120 foreach (mod; modules) 157 foreach (mod; modules)
121 { 158 {
122 current = mod; 159 current = mod;
130 auto st = s.env.findType(s.identifier.get).asStruct; 167 auto st = s.env.findType(s.identifier.get).asStruct;
131 s.sym = current.symbol.createMember( 168 s.sym = current.symbol.createMember(
132 s.identifier.get, 169 s.identifier.get,
133 st, 170 st,
134 s.env.find(s.identifier.get)[0]); 171 s.env.find(s.identifier.get)[0]);
172 sf.put(s.sym, st);
135 173
136 foreach (decl; s.decls) 174 foreach (decl; s.decls)
137 { 175 {
138 DType type; 176 DType type;
139 char[] name; 177 char[] name;
158 s.sym = current.symbol.createMember( 196 s.sym = current.symbol.createMember(
159 s.identifier.get, 197 s.identifier.get,
160 st, 198 st,
161 s.env.find(s.identifier.get)[0]); 199 s.env.find(s.identifier.get)[0]);
162 200
201 sf.put(s.sym, st);
202
163 foreach (decl; s.decls) 203 foreach (decl; s.decls)
164 { 204 {
165 DType type; 205 DType type;
166 char[] name; 206 char[] name;
167 if (auto varDecl = cast(VarDecl)decl) 207 if (auto varDecl = cast(VarDecl)decl)
184 auto st = s.env.findType(s.identifier.get).asInterface; 224 auto st = s.env.findType(s.identifier.get).asInterface;
185 s.sym = current.symbol.createMember( 225 s.sym = current.symbol.createMember(
186 s.identifier.get, 226 s.identifier.get,
187 st, 227 st,
188 s.env.find(s.identifier.get)[0]); 228 s.env.find(s.identifier.get)[0]);
229 sf.put(s.sym, st);
189 230
190 foreach (decl; s.decls) 231 foreach (decl; s.decls)
191 { 232 {
192 DType type; 233 DType type;
193 char[] name; 234 char[] name;
207 } 248 }
208 249
209 250
210 DType typeOf(Identifier id, Scope sc) 251 DType typeOf(Identifier id, Scope sc)
211 { 252 {
212 if(auto i = cast(PointerIdentifier)id) 253 if(auto i = cast(PointerTypeExp)id)
213 return (typeOf(i.pointerOf, sc)).getPointerTo(); 254 return (typeOf(i.pointerOf, sc)).getPointerTo();
214 else if(auto i = cast(StaticArrayIdentifier)id) 255 else if(auto i = cast(StaticArrayTypeExp)id)
215 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size); 256 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size);
216 return sc.findType(id.get); 257 return sc.findType(id.get);
217 } 258 }
218 259
219 Module current; 260 Module current;
261 SymbolFactory sf;
220 } 262 }
221 263
222 264
223 /** 265 /**
224 Add scopes to everything, and add all identifiers that correspond to types. 266 Add scopes to everything, and add all identifiers that correspond to types.
233 mHandle = new ModuleHandler; 275 mHandle = new ModuleHandler;
234 } 276 }
235 277
236 this() 278 this()
237 { 279 {
280 sf = new SymbolFactory();
238 } 281 }
239 282
240 override void visit(Module[] modules) 283 override void visit(Module[] modules)
241 { 284 {
242 foreach(m ; modules) 285 foreach(m ; modules)
243 visitModule(m); 286 visitModule(m);
244 287
245 auto fr = new ForwardReference(); 288 auto fr = new ForwardReference(sf);
246 289
247 fr.visit(modules); 290 fr.visit(modules);
248 } 291 }
249 292
250 private void registerBasicTypeTo(char[] n, DType t, Scope sc, Module m) 293 private void registerBasicTypeTo(char[] n, DType t, Scope sc, Module m)
444 pop(sc); 487 pop(sc);
445 } 488 }
446 489
447 private: 490 private:
448 Scope[] table; 491 Scope[] table;
492 SymbolFactory sf;
449 493
450 Scope push() 494 Scope push()
451 { 495 {
452 auto sc = new Scope(current()); 496 auto sc = new Scope(current());
453 table ~= sc; 497 table ~= sc;