Mercurial > projects > ddmd
annotate dmd/SymOffExp.d @ 179:cd48cb899aee
Updated to dmd2.040
author | korDen |
---|---|
date | Sun, 17 Oct 2010 20:56:07 +0400 |
parents | e3afd1303184 |
children | b0d41ff5e0df |
rev | line source |
---|---|
72 | 1 module dmd.SymOffExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
179 | 5 import dmd.GlobalExpressions; |
72 | 6 import dmd.Declaration; |
7 import dmd.MATCH; | |
8 import dmd.Type; | |
179 | 9 import dmd.InterState; |
72 | 10 import dmd.OutBuffer; |
11 import dmd.Loc; | |
12 import dmd.Scope; | |
13 import dmd.InlineDoState; | |
14 import dmd.HdrGenState; | |
15 import dmd.backend.dt_t; | |
0 | 16 import dmd.SymbolExp; |
17 import dmd.VarDeclaration; | |
18 import dmd.DelegateExp; | |
19 import dmd.ThisExp; | |
20 import dmd.FuncDeclaration; | |
21 import dmd.IntegerExp; | |
22 import dmd.ErrorExp; | |
72 | 23 import dmd.TY; |
0 | 24 import dmd.TOK; |
135 | 25 import dmd.STC; |
0 | 26 |
27 import dmd.backend.Symbol; | |
28 import dmd.backend.Util; | |
29 import dmd.backend.TYM; | |
72 | 30 |
0 | 31 class SymOffExp : SymbolExp |
32 { | |
33 uint offset; | |
34 | |
73 | 35 this(Loc loc, Declaration var, uint offset, bool hasOverloads = false) |
0 | 36 { |
178 | 37 register(); |
0 | 38 super(loc, TOK.TOKsymoff, SymOffExp.sizeof, var, hasOverloads); |
39 | |
40 this.offset = offset; | |
41 VarDeclaration v = var.isVarDeclaration(); | |
42 if (v && v.needThis()) | |
43 error("need 'this' for address of %s", v.toChars()); | |
44 } | |
45 | |
72 | 46 override Expression semantic(Scope sc) |
0 | 47 { |
48 version(LOGSEMANTIC) { | |
49 printf("SymOffExp::semantic('%s')\n", toChars()); | |
50 } | |
51 //var.semantic(sc); | |
52 if (!type) | |
53 type = var.type.pointerTo(); | |
54 VarDeclaration v = var.isVarDeclaration(); | |
55 if (v) | |
56 v.checkNestedReference(sc, loc); | |
57 return this; | |
58 } | |
179 | 59 |
60 Expression interpret(InterState istate) | |
61 { | |
62 version (LOG) { | |
63 writef("SymOffExp::interpret() %s\n", toChars()); | |
64 } | |
65 if (var.isFuncDeclaration() && offset == 0) | |
66 { | |
67 return this; | |
68 } | |
69 error("Cannot interpret %s at compile time", toChars()); | |
70 return EXP_CANT_INTERPRET; | |
71 | |
72 } | |
0 | 73 |
72 | 74 override void checkEscape() |
0 | 75 { |
76 VarDeclaration v = var.isVarDeclaration(); | |
77 if (v) | |
78 { | |
135 | 79 if (!v.isDataseg() && !(v.storage_class & (STC.STCref | STC.STCout))) |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
80 { /* BUG: This should be allowed: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
81 * void foo() |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
82 * { int a; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
83 * int* bar() { return &a; } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
84 * } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
85 */ |
135 | 86 error("escaping reference to local %s", v.toChars()); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
87 } |
0 | 88 } |
89 } | |
90 | |
72 | 91 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 92 { |
174 | 93 if (offset) |
94 buf.printf("(& %s+%s)", var.toChars(), offset); /// | |
95 else | |
96 buf.printf("& %s", var.toChars()); | |
0 | 97 } |
98 | |
72 | 99 override int isConst() |
0 | 100 { |
101 return 2; | |
102 } | |
103 | |
72 | 104 override bool isBool(bool result) |
0 | 105 { |
129 | 106 return result; |
0 | 107 } |
108 | |
72 | 109 override Expression doInline(InlineDoState ids) |
0 | 110 { |
111 int i; | |
112 | |
113 //printf("SymOffExp.doInline(%s)\n", toChars()); | |
114 for (i = 0; i < ids.from.dim; i++) | |
115 { | |
116 if (var is cast(Declaration)ids.from.data[i]) | |
117 { | |
118 SymOffExp se = cast(SymOffExp)copy(); | |
119 | |
120 se.var = cast(Declaration)ids.to.data[i]; | |
121 return se; | |
122 } | |
123 } | |
124 return this; | |
125 } | |
126 | |
72 | 127 override MATCH implicitConvTo(Type t) |
0 | 128 { |
129 static if (false) { | |
130 printf("SymOffExp::implicitConvTo(this=%s, type=%s, t=%s)\n", toChars(), type.toChars(), t.toChars()); | |
131 } | |
132 MATCH result = type.implicitConvTo(t); | |
133 //printf("\tresult = %d\n", result); | |
134 | |
135 if (result == MATCHnomatch) | |
136 { | |
137 // Look for pointers to functions where the functions are overloaded. | |
138 FuncDeclaration f; | |
139 | |
140 t = t.toBasetype(); | |
141 if (type.ty == Tpointer && type.nextOf().ty == Tfunction && | |
142 (t.ty == Tpointer || t.ty == Tdelegate) && t.nextOf().ty == Tfunction) | |
143 { | |
144 f = var.isFuncDeclaration(); | |
145 if (f) | |
146 { | |
147 f = f.overloadExactMatch(t.nextOf()); | |
148 if (f) | |
149 { | |
150 if ((t.ty == Tdelegate && (f.needThis() || f.isNested())) || | |
151 (t.ty == Tpointer && !(f.needThis() || f.isNested()))) | |
152 { | |
153 result = MATCHexact; | |
154 } | |
155 } | |
156 } | |
157 } | |
158 } | |
159 //printf("\tresult = %d\n", result); | |
160 return result; | |
161 } | |
162 | |
72 | 163 override Expression castTo(Scope sc, Type t) |
0 | 164 { |
165 static if (false) { | |
166 printf("SymOffExp::castTo(this=%s, type=%s, t=%s)\n", toChars(), type.toChars(), t.toChars()); | |
167 } | |
73 | 168 if (type == t && !hasOverloads) |
0 | 169 return this; |
170 | |
171 Expression e; | |
172 Type tb = t.toBasetype(); | |
173 Type typeb = type.toBasetype(); | |
174 | |
175 if (tb != typeb) | |
176 { | |
177 // Look for pointers to functions where the functions are overloaded. | |
178 FuncDeclaration f; | |
179 | |
180 if (hasOverloads && | |
181 typeb.ty == Tpointer && typeb.nextOf().ty == Tfunction && | |
182 (tb.ty == Tpointer || tb.ty == Tdelegate) && tb.nextOf().ty == Tfunction) | |
183 { | |
184 f = var.isFuncDeclaration(); | |
185 if (f) | |
186 { | |
187 f = f.overloadExactMatch(tb.nextOf()); | |
188 if (f) | |
189 { | |
190 if (tb.ty == Tdelegate) | |
191 { | |
192 if (f.needThis() && hasThis(sc)) | |
193 { | |
194 e = new DelegateExp(loc, new ThisExp(loc), f); | |
195 e = e.semantic(sc); | |
196 } | |
197 else if (f.isNested()) | |
198 { | |
199 e = new DelegateExp(loc, new IntegerExp(0), f); | |
200 e = e.semantic(sc); | |
201 } | |
202 else if (f.needThis()) | |
203 { | |
204 error("no 'this' to create delegate for %s", f.toChars()); | |
205 e = new ErrorExp(); | |
206 } | |
207 else | |
208 { | |
209 error("cannot cast from function pointer to delegate"); | |
210 e = new ErrorExp(); | |
211 } | |
212 } | |
213 else | |
214 { | |
215 e = new SymOffExp(loc, f, 0); | |
216 e.type = t; | |
217 } | |
218 version (DMDV2) { | |
219 f.tookAddressOf++; | |
220 } | |
221 return e; | |
222 } | |
223 } | |
224 } | |
225 e = Expression.castTo(sc, t); | |
226 } | |
227 else | |
228 { | |
229 e = copy(); | |
230 e.type = t; | |
73 | 231 (cast(SymOffExp)e).hasOverloads = false; |
0 | 232 } |
233 return e; | |
234 } | |
235 | |
72 | 236 override void scanForNestedRef(Scope sc) |
0 | 237 { |
72 | 238 //printf("SymOffExp.scanForNestedRef(%s)\n", toChars()); |
239 VarDeclaration v = var.isVarDeclaration(); | |
240 if (v) | |
64 | 241 v.checkNestedReference(sc, Loc(0)); |
0 | 242 } |
243 | |
72 | 244 override dt_t** toDt(dt_t** pdt) |
0 | 245 { |
246 //printf("SymOffExp.toDt('%s')\n", var.toChars()); | |
247 assert(var); | |
248 if (!(var.isDataseg() || var.isCodeseg()) || | |
249 var.needThis() || | |
250 var.isThreadlocal() | |
251 ) | |
252 { | |
253 debug writef("SymOffExp.toDt()\n"); | |
254 error("non-constant expression %s", toChars()); | |
255 return pdt; | |
256 } | |
257 | |
258 Symbol* s = var.toSymbol(); | |
259 return dtxoff(pdt, s, offset, TYnptr); | |
260 } | |
108
6da99741178e
e2ir.c changes, mainly accounts for static arrays being value types now
Trass3r
parents:
73
diff
changeset
|
261 |
6da99741178e
e2ir.c changes, mainly accounts for static arrays being value types now
Trass3r
parents:
73
diff
changeset
|
262 static if (false) |
6da99741178e
e2ir.c changes, mainly accounts for static arrays being value types now
Trass3r
parents:
73
diff
changeset
|
263 { |
6da99741178e
e2ir.c changes, mainly accounts for static arrays being value types now
Trass3r
parents:
73
diff
changeset
|
264 override elem* toElem(IRState* irs) |
6da99741178e
e2ir.c changes, mainly accounts for static arrays being value types now
Trass3r
parents:
73
diff
changeset
|
265 { |
6da99741178e
e2ir.c changes, mainly accounts for static arrays being value types now
Trass3r
parents:
73
diff
changeset
|
266 assert(false); // this function is #if 0'ed out in dmd |
6da99741178e
e2ir.c changes, mainly accounts for static arrays being value types now
Trass3r
parents:
73
diff
changeset
|
267 } |
6da99741178e
e2ir.c changes, mainly accounts for static arrays being value types now
Trass3r
parents:
73
diff
changeset
|
268 } |
0 | 269 } |
270 |