annotate dmd/AddrExp.d @ 178:e3afd1303184

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