annotate dmd/AddrExp.d @ 109:ceda59b4d255

expression.c changes, now only ddoc should be left
author Trass3r
date Tue, 31 Aug 2010 22:08:52 +0200
parents be2ab491772e
children e28b18c23469
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 type = e1.type.pointerTo();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 // See if this should really be a delegate
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 if (e1.op == TOKdotvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 DotVarExp dve = cast(DotVarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 FuncDeclaration f = dve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 if (!dve.hasOverloads)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 f.tookAddressOf++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 Expression e = new DelegateExp(loc, dve.e1, f, dve.hasOverloads);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 else if (e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 VarDeclaration v = ve.var.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 if (v && !v.canTakeAddressOf())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 error("cannot take address of %s", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 FuncDeclaration f = ve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 if (!ve.hasOverloads ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 /* Because nested functions cannot be overloaded,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 * mark here that we took its address because castTo()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 * may not be called with an exact match.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 f.toParent2().isFuncDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 f.tookAddressOf++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 if (f.isNested())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 Expression e = new DelegateExp(loc, e1, f, ve.hasOverloads);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 if (f.needThis() && hasThis(sc))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 /* Should probably supply 'this' after overload resolution,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 * not before.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 Expression ethis = new ThisExp(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 Expression e = new DelegateExp(loc, ethis, f, ve.hasOverloads);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 return optimize(WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
128 override elem* toElem(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 elem* e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 //printf("AddrExp.toElem('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 e = e1.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 e = addressElem(e, e1.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 L2:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 e.Ety = type.totym();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 el_setLoc(e,loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
142 override MATCH implicitConvTo(Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 printf("AddrExp.implicitConvTo(this=%s, type=%s, t=%s)\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 MATCH result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 result = type.implicitConvTo(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 //printf("\tresult = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 if (result == MATCHnomatch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 // Look for pointers to functions where the functions are overloaded.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 t = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 if (e1.op == TOKoverloadset &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 (t.ty == Tpointer || t.ty == Tdelegate) && t.nextOf().ty == Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 OverExp eo = cast(OverExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 FuncDeclaration f = null;
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
164 foreach (s; eo.vars.a)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
166 auto f2 = s.isFuncDeclaration();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 assert(f2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 if (f2.overloadExactMatch(t.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 /* Error if match in more than one overload set,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 * even if one is a 'better' match than the other.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 ScopeDsymbol.multiplyDefined(loc, f, f2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 f = f2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 result = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 }
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 if (type.ty == Tpointer && type.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 t.ty == Tpointer && t.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 /* I don't think this can ever happen -
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 * it should have been
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 * converted to a SymOffExp.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 FuncDeclaration f = ve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 if (f && f.overloadExactMatch(t.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 result = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 //printf("\tresult = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
202 override Expression castTo(Scope sc, Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 Type tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 printf("AddrExp.castTo(this=%s, type=%s, t=%s)\n", toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 Expression e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 type = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 if (tb != type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 // Look for pointers to functions where the functions are overloaded.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 if (e1.op == TOKoverloadset &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 (t.ty == Tpointer || t.ty == Tdelegate) && t.nextOf().ty == Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 OverExp eo = cast(OverExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 FuncDeclaration f = null;
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
222 foreach (s; eo.vars.a)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
224 auto f2 = s.isFuncDeclaration();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 assert(f2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 if (f2.overloadExactMatch(t.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 /* Error if match in more than one overload set,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 * even if one is a 'better' match than the other.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 ScopeDsymbol.multiplyDefined(loc, f, f2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 f = f2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 f.tookAddressOf++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 SymOffExp se = new SymOffExp(loc, f, 0, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 se.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 // Let SymOffExp.castTo() do the heavy lifting
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 return se.castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 }
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 if (type.ty == Tpointer && type.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 tb.ty == Tpointer && tb.nextOf().ty == Tfunction &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 FuncDeclaration f = ve.var.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 assert(0); // should be SymOffExp instead
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 f = f.overloadExactMatch(tb.nextOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 if (f)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 e = new VarExp(loc, f);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 e.type = f.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 e = new AddrExp(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 e = Expression.castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
274 override Expression optimize(int result)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 //printf("AddrExp.optimize(result = %d) %s\n", result, toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 /* Rewrite &(a,b) as (a,&b)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 if (e1.op == TOKcomma)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 CommaExp ce = cast(CommaExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 AddrExp ae = new AddrExp(loc, ce.e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 ae.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 e = new CommaExp(ce.loc, ce.e1, ae);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 return e.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 if (e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 if (ve.var.storage_class & STCmanifest)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 e1 = e1.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 e1 = e1.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 // Convert &*ex to ex
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 if (e1.op == TOKstar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 Expression ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 ex = (cast(PtrExp)e1).e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 if (type.equals(ex.type))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 e = ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 e = ex.copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 if (e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 VarExp ve = cast(VarExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 if (!ve.var.isOut() && !ve.var.isRef() &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 !ve.var.isImportedSymbol())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 SymOffExp se = new SymOffExp(loc, ve.var, 0, ve.hasOverloads);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323 se.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 return se;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 if (e1.op == TOKindex)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 // Convert &array[n] to &array+n
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 IndexExp ae = cast(IndexExp)e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 if (ae.e2.op == TOKint64 && ae.e1.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 long index = ae.e2.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 VarExp ve = cast(VarExp)ae.e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 if (ve.type.ty == Tsarray
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 && !ve.var.isImportedSymbol())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 TypeSArray ts = cast(TypeSArray)ve.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 long dim = ts.dim.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 if (index < 0 || index >= dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 error("array index %jd is out of bounds [0..%jd]", index, dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 e = new SymOffExp(loc, ve.var, cast(uint)(index * ts.nextOf().size()));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 e.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352