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