comparison sema/ScopeBuilder.d @ 136:2be29b296081

Lots of changes: - Parsing classes and interfaces - Fixed some seg faults in sema - Supporting "private" to some extend - And a lot of other small fixes
author johnsen@johnsen-laptop
date Fri, 11 Jul 2008 21:47:57 +0200
parents 9c48871eb816
children 6e6355fb5f0f
comparison
equal deleted inserted replaced
135:9869194de9b7 136:2be29b296081
38 foreach (stmt; d.statements) 38 foreach (stmt; d.statements)
39 visitStmt(stmt); 39 visitStmt(stmt);
40 40
41 inFunctionBodyStack.pop(); 41 inFunctionBodyStack.pop();
42 42
43 d.sym = current.symbol.createMember(d.identifier.get, d.type); 43 d.sym = current.symbol.createMember(
44 d.identifier.get,
45 d.type,
46 d.env.find(d.identifier));
44 } 47 }
45 48
46 override void visitVarDecl(VarDecl d) 49 override void visitVarDecl(VarDecl d)
47 { 50 {
48 visitExp(d.varType); 51 visitExp(d.varType);
50 53
51 if (d.init) 54 if (d.init)
52 visitExp(d.init); 55 visitExp(d.init);
53 56
54 DType t = typeOf(d.varType, d.env); 57 DType t = typeOf(d.varType, d.env);
55 d.sym = current.symbol.createAlias(d.identifier.get,d.env.find(d.varType).sym); 58 d.sym = current.symbol.createAlias(
59 d.identifier.get,
60 d.env.find(d.varType).sym,
61 d.env.find(d.identifier));
62 d.sym.type = t;
56 } 63 }
57 64
58 override void visitStructDecl(StructDecl s) 65 override void visitStructDecl(StructDecl s)
59 { 66 {
60 auto old = current.symbol; 67 auto old = current.symbol;
67 74
68 DType typeOf(Identifier id, Scope sc) 75 DType typeOf(Identifier id, Scope sc)
69 { 76 {
70 if(auto i = cast(PointerIdentifier)id) 77 if(auto i = cast(PointerIdentifier)id)
71 return (typeOf(i.pointerOf, sc)).getPointerTo(); 78 return (typeOf(i.pointerOf, sc)).getPointerTo();
72 else if(auto i = cast(ArrayIdentifier)id) 79 else if(auto i = cast(StaticArrayIdentifier)id)
73 return typeOf(i.arrayOf, sc).getAsArray(i.size); 80 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size);
74 return sc.findType(id); 81 return sc.findType(id);
75 } 82 }
76 83
77 Module[] modules; 84 Module[] modules;
78 Module current; 85 Module current;
92 } 99 }
93 100
94 override void visitStructDecl(StructDecl s) 101 override void visitStructDecl(StructDecl s)
95 { 102 {
96 auto st = s.env.findType(s.identifier).asStruct; 103 auto st = s.env.findType(s.identifier).asStruct;
97 s.sym = current.symbol.createMember(s.identifier.get, st); 104 s.sym = current.symbol.createMember(
105 s.identifier.get,
106 st,
107 s.env.find(s.identifier));
98 108
99 foreach (decl; s.decls) 109 foreach (decl; s.decls)
100 { 110 {
101 DType type; 111 DType type;
102 char[] name; 112 char[] name;
116 126
117 DType typeOf(Identifier id, Scope sc) 127 DType typeOf(Identifier id, Scope sc)
118 { 128 {
119 if(auto i = cast(PointerIdentifier)id) 129 if(auto i = cast(PointerIdentifier)id)
120 return (typeOf(i.pointerOf, sc)).getPointerTo(); 130 return (typeOf(i.pointerOf, sc)).getPointerTo();
121 else if(auto i = cast(ArrayIdentifier)id) 131 else if(auto i = cast(StaticArrayIdentifier)id)
122 return typeOf(i.arrayOf, sc).getAsArray(i.size); 132 return typeOf(i.arrayOf, sc).getAsStaticArray(i.size);
123 return sc.findType(id); 133 return sc.findType(id);
124 } 134 }
125 135
126 Module current; 136 Module current;
127 } 137 }
155 } 165 }
156 166
157 private void registerBasicTypeTo(char[] n, DType t, Scope sc, Module m) 167 private void registerBasicTypeTo(char[] n, DType t, Scope sc, Module m)
158 { 168 {
159 sc.types[n] = t; 169 sc.types[n] = t;
160 auto sym = m.symbol.createMember(n, t);
161 auto id = new Identifier(n); 170 auto id = new Identifier(n);
162 id.env = sc; 171 id.env = sc;
163 auto decl = new DummyDecl(); 172 auto decl = new DummyDecl();
173 auto sym = m.symbol.createMember(n, t, decl);
174 sym.decl = decl;
164 decl.sym = sym; 175 decl.sym = sym;
165 decl.env = sc; 176 decl.env = sc;
166 sc.put(id, decl); 177 sc.put(id, decl);
167 } 178 }
168 179
226 } 237 }
227 238
228 override void visitFuncDecl(FuncDecl d) 239 override void visitFuncDecl(FuncDecl d)
229 { 240 {
230 current().put(d.identifier, d); 241 current().put(d.identifier, d);
242 d.env = current();
231 auto sc = push(); 243 auto sc = push();
232 244
233 visitExp(d.returnType); 245 visitExp(d.returnType);
234 visitExp(d.identifier); 246 visitExp(d.identifier);
235 d.env = current();
236 sc.parentFunction = d; 247 sc.parentFunction = d;
237 foreach (arg; d.funcArgs) 248 foreach (arg; d.funcArgs)
238 visitDecl(arg); 249 visitDecl(arg);
239 foreach (stmt; d.statements) 250 foreach (stmt; d.statements)
240 { 251 {
269 auto type = new DStruct(s.identifier); 280 auto type = new DStruct(s.identifier);
270 281
271 sc.types[s.identifier.get] = type; 282 sc.types[s.identifier.get] = type;
272 283
273 sc = push(); 284 sc = push();
285 Stdout(sc).newline;
274 super.visitStructDecl(s); 286 super.visitStructDecl(s);
275 pop(sc); 287 pop(sc);
276 } 288 }
277 289
278 override void visitDeclStmt(DeclStmt d) 290 override void visitDeclStmt(DeclStmt d)