annotate dmd/AddrExp.d @ 77:ad4792a1cfd6

more D-ification container accessing
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Sun, 29 Aug 2010 14:36:55 +0100
parents 2e2a5c3f943a
children be2ab491772e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
1 module dmd.AddrExp;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
2
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
3 import dmd.Expression;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
4 import dmd.UnaExp;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
5 import dmd.MATCH;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
6 import dmd.Type;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
7 import dmd.Loc;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
8 import dmd.Scope;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.IRState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.ErrorExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.DotVarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.DelegateExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.VarDeclaration;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
16 import dmd.ThisExp;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.CommaExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.PtrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.SymOffExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.IndexExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.OverExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.TY;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
28 import dmd.TypeSArray;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
29
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.backend.elem;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.backend.Util;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
32 import dmd.codegen.Util;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
33
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 class AddrExp : UnaExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 this(Loc loc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 super(loc, TOK.TOKaddress, AddrExp.sizeof, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
41 override Expression semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 version (LOGSEMANTIC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 printf("AddrExp.semantic('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 UnaExp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 e1 = e1.toLvalue(sc, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 if (!e1.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 error("cannot take address of %s", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 return new ErrorExp();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 if (!e1.type.deco)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 /* No deco means semantic() was not run on the type.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 * We have to run semantic() on the symbol to get the right type:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 * auto x = &bar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 * pure: int bar() { return 1;}
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 * otherwise the 'pure' is missing from the type assigned to x.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 error("forward reference to %s", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 return new ErrorExp();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 //printf("test3 deco = %p\n", e1.type.deco);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 type = e1.type.pointerTo();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 // See if this should really be a delegate
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 if (e1.op == TOKdotvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 DotVarExp dve = cast(DotVarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 FuncDeclaration f = dve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 if (!dve.hasOverloads)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 f.tookAddressOf++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 Expression e = new DelegateExp(loc, dve.e1, f, dve.hasOverloads);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 else if (e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 VarDeclaration v = ve.var.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 if (v && !v.canTakeAddressOf())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 error("cannot take address of %s", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 FuncDeclaration f = ve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 if (!ve.hasOverloads ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 /* Because nested functions cannot be overloaded,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 * mark here that we took its address because castTo()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 * may not be called with an exact match.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 f.toParent2().isFuncDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 f.tookAddressOf++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 if (f.isNested())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 Expression e = new DelegateExp(loc, e1, f, ve.hasOverloads);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 if (f.needThis() && hasThis(sc))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 /* Should probably supply 'this' after overload resolution,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 * not before.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 Expression ethis = new ThisExp(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 Expression e = new DelegateExp(loc, ethis, f, ve.hasOverloads);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 return optimize(WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
129 override elem* toElem(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 elem* e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 //printf("AddrExp.toElem('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 e = e1.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 e = addressElem(e, e1.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 L2:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 e.Ety = type.totym();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 el_setLoc(e,loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
143 override MATCH implicitConvTo(Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 printf("AddrExp.implicitConvTo(this=%s, type=%s, t=%s)\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 MATCH result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 result = type.implicitConvTo(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 //printf("\tresult = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 if (result == MATCHnomatch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 // Look for pointers to functions where the functions are overloaded.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 t = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 if (e1.op == TOKoverloadset &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 (t.ty == Tpointer || t.ty == Tdelegate) && t.nextOf().ty == Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 OverExp eo = cast(OverExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 FuncDeclaration f = null;
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
165 foreach(Dsymbol s; eo.vars.a)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 FuncDeclaration f2 = s.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 assert(f2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 if (f2.overloadExactMatch(t.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 /* Error if match in more than one overload set,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 * even if one is a 'better' match than the other.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 ScopeDsymbol.multiplyDefined(loc, f, f2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 f = f2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 result = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 if (type.ty == Tpointer && type.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 t.ty == Tpointer && t.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 /* I don't think this can ever happen -
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 * it should have been
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 * converted to a SymOffExp.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 FuncDeclaration f = ve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 if (f && f.overloadExactMatch(t.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 result = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 //printf("\tresult = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
203 override Expression castTo(Scope sc, Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 Type tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 printf("AddrExp.castTo(this=%s, type=%s, t=%s)\n", toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 Expression e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 type = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 if (tb != type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 // Look for pointers to functions where the functions are overloaded.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 if (e1.op == TOKoverloadset &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 (t.ty == Tpointer || t.ty == Tdelegate) && t.nextOf().ty == Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 OverExp eo = cast(OverExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 FuncDeclaration f = null;
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
223 foreach(Dsymbol s; eo.vars.a)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 FuncDeclaration f2 = s.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 assert(f2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 if (f2.overloadExactMatch(t.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 /* Error if match in more than one overload set,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 * even if one is a 'better' match than the other.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 ScopeDsymbol.multiplyDefined(loc, f, f2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 f = f2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 f.tookAddressOf++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 SymOffExp se = new SymOffExp(loc, f, 0, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 se.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 // Let SymOffExp.castTo() do the heavy lifting
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 return se.castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 if (type.ty == Tpointer && type.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 tb.ty == Tpointer && tb.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 FuncDeclaration f = ve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 assert(0); // should be SymOffExp instead
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 f = f.overloadExactMatch(tb.nextOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 e = new VarExp(loc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 e.type = f.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 e = new AddrExp(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 e = Expression.castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
275 override Expression optimize(int result)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 //printf("AddrExp.optimize(result = %d) %s\n", result, toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 /* Rewrite &(a,b) as (a,&b)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 if (e1.op == TOKcomma)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 CommaExp ce = cast(CommaExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 AddrExp ae = new AddrExp(loc, ce.e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 ae.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 e = new CommaExp(ce.loc, ce.e1, ae);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 return e.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 if (e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 if (ve.var.storage_class & STCmanifest)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 e1 = e1.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 e1 = e1.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 // Convert &*ex to ex
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 if (e1.op == TOKstar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 Expression ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 ex = (cast(PtrExp)e1).e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 if (type.equals(ex.type))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 e = ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 e = ex.copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 if (e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 if (!ve.var.isOut() && !ve.var.isRef() &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 !ve.var.isImportedSymbol())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323 SymOffExp se = new SymOffExp(loc, ve.var, 0, ve.hasOverloads);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 se.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 return se;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 if (e1.op == TOKindex)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 // Convert &array[n] to &array+n
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 IndexExp ae = cast(IndexExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 if (ae.e2.op == TOKint64 && ae.e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 long index = ae.e2.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 VarExp ve = cast(VarExp)ae.e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 if (ve.type.ty == Tsarray
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 && !ve.var.isImportedSymbol())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 TypeSArray ts = cast(TypeSArray)ve.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 long dim = ts.dim.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 if (index < 0 || index >= dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 error("array index %jd is out of bounds [0..%jd]", index, dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 e = new SymOffExp(loc, ve.var, cast(uint)(index * ts.nextOf().size()));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353