comparison sema/ScopeBuilder.d @ 129:ed815b31479b

Added a Symbol
author Anders Halager <halager@gmail.com>
date Sat, 21 Jun 2008 20:41:18 +0200
parents c0b531362ca6
children 6545a8d59596
comparison
equal deleted inserted replaced
128:7264c61088c4 129:ed815b31479b
2 2
3 import tango.io.Stdout, 3 import tango.io.Stdout,
4 tango.core.Array : find; 4 tango.core.Array : find;
5 5
6 public 6 public
7 import sema.Scope; 7 import sema.Scope,
8 sema.Symbol;
8 9
9 import sema.Visitor, 10 import sema.Visitor,
10 basic.SmallArray; 11 basic.SmallArray;
11 12
12 class ForwardReference : Visitor!(void) 13 class ForwardReference : Visitor!(void)
13 { 14 {
14 override void visit(Module[] modules) 15 override void visit(Module[] modules)
15 { 16 {
16 this.modules = modules; 17 this.modules = modules;
17 super.visit(modules); 18 inFunctionBodyStack.push(false);
19 foreach (mod; modules)
20 {
21 current = mod;
22 foreach (decl; mod.decls)
23 visitDecl(decl);
24 }
18 } 25 }
19 26
20 override void visitFuncDecl(FuncDecl d) 27 override void visitFuncDecl(FuncDecl d)
21 { 28 {
29
22 visitExp(d.returnType); 30 visitExp(d.returnType);
23 visitExp(d.identifier); 31 visitExp(d.identifier);
24 foreach (arg; d.funcArgs) 32 foreach (arg; d.funcArgs)
25 visitDecl(arg); 33 visitDecl(arg);
34
35 inFunctionBodyStack.push(true);
36
26 foreach (stmt; d.statements) 37 foreach (stmt; d.statements)
27 visitStmt(stmt); 38 visitStmt(stmt);
28 39
29 d.env.find(d.identifier).setType(d.type); 40 inFunctionBodyStack.pop();
41
42 d.sym = current.symbol.createMember(d.identifier.get, d.type);
30 } 43 }
31 44
32 override void visitVarDecl(VarDecl d) 45 override void visitVarDecl(VarDecl d)
33 { 46 {
34 visitExp(d.varType); 47 visitExp(d.varType);
35 visitExp(d.identifier); 48 visitExp(d.identifier);
36 49
37 if (d.init) 50 if (d.init)
38 visitExp(d.init); 51 visitExp(d.init);
39 52
40 d.env.find(d.identifier).setType( typeOf(d.varType, d.env) ); 53 DType t = typeOf(d.varType, d.env);
54 /*
55 if (inFunctionBodyStack.peek())
56 {
57 Stdout.formatln("?? {}, {}, {}", d.varType.get, d.identifier.get, t);
58 auto find = d.env.find(d.varType);
59 d.sym = d.env.find(d.varType).sym;
60 }
61 else
62 */
63 d.sym = current.symbol.createMember(d.identifier.get, t);
41 } 64 }
42 65
43 override void visitStructDecl(StructDecl s) 66 override void visitStructDecl(StructDecl s)
44 { 67 {
68 auto st = s.env.findType(s.identifier).asStruct;
69 s.sym = current.symbol.createMember(s.identifier.get, st);
70 //s.env.set(s.identifier, s);
71
72 auto old = current.symbol;
73 current.symbol = s.sym;
74 inFunctionBodyStack.push(false);
45 super.visitStructDecl(s); 75 super.visitStructDecl(s);
46 76 inFunctionBodyStack.pop();
47 DType[char[]] types; 77 current.symbol = old;
48 78
49 auto st = s.env.types[s.identifier.get].asStruct;
50 foreach (decl; s.decls) 79 foreach (decl; s.decls)
80 {
81 DType type;
82 char[] name;
51 if (auto varDecl = cast(VarDecl)decl) 83 if (auto varDecl = cast(VarDecl)decl)
52 st.addMember(typeOf(varDecl.varType, varDecl.env), varDecl.identifier.get); 84 {
85 type = typeOf(varDecl.varType, varDecl.env);
86 name = varDecl.identifier.get;
87 }
53 else if (auto fd = cast(FuncDecl)decl) 88 else if (auto fd = cast(FuncDecl)decl)
54 st.addMember(fd.type, fd.identifier.get); 89 {
90 type = fd.type;
91 name = fd.identifier.get;
92 }
93 st.addMember(type, name);
94 }
55 } 95 }
56 96
57 DType typeOf(Identifier id, Scope sc) 97 DType typeOf(Identifier id, Scope sc)
58 { 98 {
59 if(auto i = cast(PointerIdentifier)id) 99 if(auto i = cast(PointerIdentifier)id)
60 return (typeOf(i.pointerOf, sc)).getPointerTo(); 100 return (typeOf(i.pointerOf, sc)).getPointerTo();
61 if(auto i = cast(ArrayIdentifier)id) 101 else if(auto i = cast(ArrayIdentifier)id)
62 return typeOf(i.arrayOf, sc).getAsArray(i.size); 102 return typeOf(i.arrayOf, sc).getAsArray(i.size);
63 return sc.findType(id); 103 return sc.findType(id);
64 } 104 }
65 105
66 Module[] modules; 106 Module[] modules;
107 Module current;
108 SmallArray!(bool) inFunctionBodyStack;
67 } 109 }
68 110
111 /**
112 Add scopes to everything, and add all identifiers that correspond to types.
113 Types/Symbols are added by ForwardReference.
114 **/
69 class ScopeBuilder : Visitor!(void) 115 class ScopeBuilder : Visitor!(void)
70 { 116 {
71 static ModuleHandler mHandle; 117 static ModuleHandler mHandle;
72 118
73 static this() 119 static this()
87 auto fr = new ForwardReference(); 133 auto fr = new ForwardReference();
88 134
89 fr.visit(modules); 135 fr.visit(modules);
90 } 136 }
91 137
138 private void registerBasicTypeTo(char[] n, DType t, Scope sc, Module m)
139 {
140 sc.types[n] = t;
141 auto sym = m.symbol.createMember(n, t);
142 auto id = new Identifier(n);
143 id.env = sc;
144 auto decl = new DummyDecl();
145 decl.sym = sym;
146 decl.env = sc;
147 sc.put(id, decl);
148 }
149
92 override void visitModule(Module m) 150 override void visitModule(Module m)
93 { 151 {
94 table ~= new Scope; 152 auto root = new Scope;
95 table[table.length-1].types["void"] = DType.Void; 153 table ~= root;
96 table[table.length-1].types["bool"] = DType.Bool; 154
97 table[table.length-1].types["byte"] = DType.Byte; 155 m.symbol = new Symbol;
98 table[table.length-1].types["ubyte"] = DType.UByte; 156
99 table[table.length-1].types["short"] = DType.Short; 157 registerBasicTypeTo("void", DType.Void, root, m);
100 table[table.length-1].types["ushort"] = DType.UShort; 158 registerBasicTypeTo("bool", DType.Bool, root, m);
101 table[table.length-1].types["int"] = DType.Int; 159 registerBasicTypeTo("byte", DType.Byte, root, m);
102 table[table.length-1].types["uint"] = DType.UInt; 160 registerBasicTypeTo("ubyte", DType.UByte, root, m);
103 table[table.length-1].types["long"] = DType.Long; 161 registerBasicTypeTo("short", DType.Short, root, m);
104 table[table.length-1].types["ulong"] = DType.ULong; 162 registerBasicTypeTo("ushort", DType.UShort, root, m);
105 table[table.length-1].types["char"] = DType.Char; 163 registerBasicTypeTo("int", DType.Int, root, m);
106 table[table.length-1].types["wchar"] = DType.WChar; 164 registerBasicTypeTo("uint", DType.UInt, root, m);
107 table[table.length-1].types["dchar"] = DType.DChar; 165 registerBasicTypeTo("long", DType.Long, root, m);
108 166 registerBasicTypeTo("ulong", DType.ULong, root, m);
109 table[table.length-1].types["float"] = DType.Float; 167
110 table[table.length-1].types["double"] = DType.Double; 168 registerBasicTypeTo("char", DType.Char, root, m);
111 table[table.length-1].types["real"] = DType.Real; 169 registerBasicTypeTo("wchar", DType.WChar, root, m);
170 registerBasicTypeTo("dchar", DType.DChar, root, m);
171
172 registerBasicTypeTo("float", DType.Float, root, m);
173 registerBasicTypeTo("double", DType.Double, root, m);
174 registerBasicTypeTo("real", DType.Real, root, m);
112 175
113 current().inModule = m; 176 current().inModule = m;
114 current().mHandle = mHandle; 177 current().mHandle = mHandle;
115 mHandle.add(m); 178 mHandle.add(m);
116 m.env = current(); 179 m.env = current();
143 super.visitExp(e); 206 super.visitExp(e);
144 } 207 }
145 208
146 override void visitFuncDecl(FuncDecl d) 209 override void visitFuncDecl(FuncDecl d)
147 { 210 {
148 current().add(d.identifier); 211 current().put(d.identifier, d);
149 auto sc = push(); 212 auto sc = push();
150 213
151 visitExp(d.returnType); 214 visitExp(d.returnType);
152 visitExp(d.identifier); 215 visitExp(d.identifier);
153 d.env = current(); 216 d.env = current();
171 push(); 234 push();
172 --need_push; 235 --need_push;
173 } 236 }
174 237
175 auto sc = current(); 238 auto sc = current();
176 sc.add(d.identifier); 239 sc.put(d.identifier, d);
177 d.env = sc; 240 d.env = sc;
178 visitExp(d.varType); 241 visitExp(d.varType);
179 visitExp(d.identifier); 242 visitExp(d.identifier);
180 } 243 }
181 244
182 override void visitStructDecl(StructDecl s) 245 override void visitStructDecl(StructDecl s)
183 { 246 {
184 auto sc = current(); 247 auto sc = current();
185 sc.add(s.identifier); 248 sc.put(s.identifier, s);
186 s.env = sc; 249 s.env = sc;
187 auto type = new DStruct(s.identifier); 250 auto type = new DStruct(s.identifier);
188 251
189 sc.types[s.identifier.get] = type; 252 sc.types[s.identifier.get] = type;
190 253
191 sc = push(); 254 sc = push();
192 super.visitStructDecl(s); 255 super.visitStructDecl(s);
193 pop(sc); 256 pop(sc);