Mercurial > projects > ddmd
annotate dmd/expression/Util.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 |
---|---|
0 | 1 module dmd.expression.Util; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.Expression; |
5 import dmd.Loc; | |
53 | 6 import dmd.RealExp; |
0 | 7 import dmd.Scope; |
8 import dmd.FuncExp; | |
9 import dmd.DelegateExp; | |
10 import dmd.LINK; | |
11 import dmd.NullExp; | |
12 import dmd.SymOffExp; | |
13 import dmd.ExpInitializer; | |
14 import dmd.Lexer; | |
15 import dmd.TypeSArray; | |
16 import dmd.TypeArray; | |
17 import dmd.VarDeclaration; | |
18 import dmd.VoidInitializer; | |
19 import dmd.DeclarationExp; | |
20 import dmd.VarExp; | |
21 import dmd.NewExp; | |
22 import dmd.STC; | |
23 import dmd.WANT; | |
24 import dmd.IndexExp; | |
25 import dmd.AssignExp; | |
26 import dmd.CommaExp; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
27 import dmd.CondExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
28 import dmd.Parameter; |
0 | 29 import dmd.DefaultInitExp; |
30 import dmd.Identifier; | |
31 import dmd.Dsymbol; | |
32 import dmd.Global; | |
33 import dmd.ScopeDsymbol; | |
34 import dmd.DotIdExp; | |
35 import dmd.DotVarExp; | |
36 import dmd.CallExp; | |
37 import dmd.TY; | |
38 import dmd.MATCH; | |
53 | 39 import dmd.BUILTIN; |
0 | 40 import dmd.TypeFunction; |
41 import dmd.declaration.Match; | |
42 import dmd.ArrayTypes; | |
43 import dmd.Declaration; | |
44 import dmd.FuncAliasDeclaration; | |
45 import dmd.AliasDeclaration; | |
46 import dmd.FuncDeclaration; | |
47 import dmd.TemplateDeclaration; | |
48 import dmd.AggregateDeclaration; | |
49 import dmd.IntegerExp; | |
50 import dmd.Type; | |
51 import dmd.TOK; | |
52 import dmd.TypeExp; | |
53 import dmd.TypeTuple; | |
54 import dmd.TupleExp; | |
55 import dmd.OutBuffer; | |
56 import dmd.HdrGenState; | |
57 import dmd.ClassDeclaration; | |
58 import dmd.TypeClass; | |
59 import dmd.StructDeclaration; | |
60 import dmd.TypeStruct; | |
61 import dmd.MOD; | |
62 import dmd.PROT; | |
63 import dmd.PREC; | |
64 import dmd.Util; | |
65 import dmd.TypeAArray; | |
66 import dmd.Id; | |
114 | 67 import dmd.PtrExp; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
68 import dmd.ErrorExp; |
0 | 69 |
70 import std.stdio : writef; | |
71 | |
129 | 72 import core.stdc.math; |
73 import core.stdc.string; | |
0 | 74 |
75 /*********************************** | |
76 * Utility to build a function call out of this reference and argument. | |
77 */ | |
78 Expression build_overload(Loc loc, Scope sc, Expression ethis, Expression earg, Identifier id) | |
79 { | |
80 Expression e; | |
81 | |
82 //printf("build_overload(id = '%s')\n", id.toChars()); | |
83 //earg.print(); | |
84 //earg.type.print(); | |
85 e = new DotIdExp(loc, ethis, id); | |
86 | |
87 if (earg) | |
88 e = new CallExp(loc, e, earg); | |
89 else | |
90 e = new CallExp(loc, e); | |
91 | |
92 e = e.semantic(sc); | |
93 return e; | |
94 } | |
95 | |
96 /*************************************** | |
97 * Search for function funcid in aggregate ad. | |
98 */ | |
99 | |
100 Dsymbol search_function(ScopeDsymbol ad, Identifier funcid) | |
101 { | |
102 Dsymbol s; | |
103 FuncDeclaration fd; | |
104 TemplateDeclaration td; | |
105 | |
106 s = ad.search(Loc(0), funcid, 0); | |
107 if (s) | |
108 { | |
109 Dsymbol s2; | |
110 | |
111 //printf("search_function: s = '%s'\n", s.kind()); | |
112 s2 = s.toAlias(); | |
113 //printf("search_function: s2 = '%s'\n", s2.kind()); | |
114 fd = s2.isFuncDeclaration(); | |
115 if (fd && fd.type.ty == TY.Tfunction) | |
116 return fd; | |
117 | |
118 td = s2.isTemplateDeclaration(); | |
119 if (td) | |
120 return td; | |
121 } | |
122 | |
123 return null; | |
124 } | |
125 | |
126 /******************************************** | |
127 * Find function in overload list that exactly matches t. | |
128 */ | |
129 | |
130 /*************************************************** | |
131 * Visit each overloaded function in turn, and call | |
132 * dg(param, f) on it. | |
133 * Exit when no more, or dg(param, f) returns 1. | |
134 * Returns: | |
135 * 0 continue | |
136 * 1 done | |
137 */ | |
138 | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
139 int overloadApply(FuncDeclaration fstart, int delegate(void*, FuncDeclaration) dg, void* param) |
0 | 140 { |
141 FuncDeclaration f; | |
142 Declaration d; | |
143 Declaration next; | |
144 | |
145 for (d = fstart; d; d = next) | |
146 { | |
147 FuncAliasDeclaration fa = d.isFuncAliasDeclaration(); | |
148 | |
149 if (fa) | |
150 { | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
151 if (overloadApply(fa.funcalias, dg, param)) |
0 | 152 return 1; |
153 next = fa.overnext; | |
154 } | |
155 else | |
156 { | |
157 AliasDeclaration a = d.isAliasDeclaration(); | |
158 | |
159 if (a) | |
160 { | |
161 Dsymbol s = a.toAlias(); | |
162 next = s.isDeclaration(); | |
163 if (next is a) | |
164 break; | |
165 if (next is fstart) | |
166 break; | |
167 } | |
168 else | |
169 { | |
170 f = d.isFuncDeclaration(); | |
171 if (f is null) | |
172 { | |
173 d.error("is aliased to a function"); | |
174 break; // BUG: should print error message? | |
175 } | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
176 if (dg(param, f)) |
0 | 177 return 1; |
178 | |
179 next = f.overnext; | |
180 } | |
181 } | |
182 } | |
183 return 0; | |
184 } | |
185 | |
186 /******************************************** | |
187 * Decide which function matches the arguments best. | |
188 */ | |
189 | |
190 struct Param2 | |
191 { | |
192 Match* m; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
193 version(DMDV2) { |
0 | 194 Expression ethis; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
195 int property; // 0: unintialized |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
196 // 1: seen @property |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
197 // 2: not @property |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
198 } |
0 | 199 Expressions arguments; |
200 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
201 int fp2(void* param, FuncDeclaration f) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
202 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
203 auto p = cast(Param2*)param; |
0 | 204 MATCH match; |
205 | |
206 if (f != m.lastf) // skip duplicates | |
207 { | |
208 m.anyf = f; | |
209 TypeFunction tf = cast(TypeFunction)f.type; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
210 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
211 int property = (tf.isproperty) ? 1 : 2; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
212 if (p.property == 0) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
213 p.property = property; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
214 else if (p.property != property) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
215 error(f.loc, "cannot overload both property and non-property functions"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
216 |
0 | 217 match = tf.callMatch(f.needThis() ? ethis : null, arguments); |
98 | 218 //printf("test: match = %d\n", match); |
0 | 219 if (match != MATCH.MATCHnomatch) |
220 { | |
221 if (match > m.last) | |
222 goto LfIsBetter; | |
223 | |
224 if (match < m.last) | |
225 goto LlastIsBetter; | |
226 | |
227 /* See if one of the matches overrides the other. | |
228 */ | |
229 if (m.lastf.overrides(f)) | |
230 goto LlastIsBetter; | |
231 else if (f.overrides(m.lastf)) | |
232 goto LfIsBetter; | |
233 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
234 version(DMDV2) { |
0 | 235 /* Try to disambiguate using template-style partial ordering rules. |
236 * In essence, if f() and g() are ambiguous, if f() can call g(), | |
237 * but g() cannot call f(), then pick f(). | |
238 * This is because f() is "more specialized." | |
239 */ | |
240 { | |
241 MATCH c1 = f.leastAsSpecialized(m.lastf); | |
242 MATCH c2 = m.lastf.leastAsSpecialized(f); | |
243 //printf("c1 = %d, c2 = %d\n", c1, c2); | |
244 if (c1 > c2) | |
245 goto LfIsBetter; | |
246 if (c1 < c2) | |
247 goto LlastIsBetter; | |
248 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
249 } |
0 | 250 |
251 Lambiguous: | |
252 m.nextf = f; | |
253 m.count++; | |
254 return 0; | |
255 | |
256 LfIsBetter: | |
257 m.last = match; | |
258 m.lastf = f; | |
259 m.count = 1; | |
260 return 0; | |
261 | |
262 LlastIsBetter: | |
263 return 0; | |
264 } | |
265 } | |
266 return 0; | |
267 } | |
268 } | |
269 | |
270 struct Param1 | |
271 { | |
272 Type t; // type to match | |
273 FuncDeclaration f; // return value | |
274 | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
275 int fp1(void*, FuncDeclaration f) |
0 | 276 { |
277 if (t.equals(f.type)) | |
278 { | |
279 this.f = f; | |
280 return 1; | |
281 } | |
282 | |
283 version (DMDV2) { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
284 /* Allow covariant matches, as long as the return type |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
285 * is just a const conversion. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
286 * This allows things like pure functions to match with an impure function type. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
287 */ |
0 | 288 if (t.ty == Tfunction) |
289 { | |
290 TypeFunction tf = cast(TypeFunction)f.type; | |
291 if (tf.covariant(t) == 1 && | |
292 tf.nextOf().implicitConvTo(t.nextOf()) >= MATCHconst) | |
293 { | |
294 this.f = f; | |
295 return 1; | |
296 } | |
297 } | |
298 } | |
299 return 0; | |
300 } | |
301 } | |
302 | |
303 void overloadResolveX(Match* m, FuncDeclaration fstart, Expression ethis, Expressions arguments) | |
304 { | |
305 Param2 p; | |
306 p.m = m; | |
307 p.ethis = ethis; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
308 p.property = 0; |
0 | 309 p.arguments = arguments; |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
310 overloadApply(fstart, &p.fp2, &p); |
0 | 311 } |
312 | |
313 void templateResolve(Match* m, TemplateDeclaration td, Scope sc, Loc loc, Objects targsi, Expression ethis, Expressions arguments) | |
314 { | |
315 FuncDeclaration fd; | |
316 | |
317 assert(td); | |
318 fd = td.deduceFunctionTemplate(sc, loc, targsi, ethis, arguments); | |
319 if (!fd) | |
320 return; | |
321 m.anyf = fd; | |
322 if (m.last >= MATCH.MATCHexact) | |
323 { | |
324 m.nextf = fd; | |
325 m.count++; | |
326 } | |
327 else | |
328 { | |
329 m.last = MATCH.MATCHexact; | |
330 m.lastf = fd; | |
331 m.count = 1; | |
332 } | |
333 } | |
334 | |
335 /****************************** | |
336 * Perform semantic() on an array of Expressions. | |
337 */ | |
338 | |
339 void arrayExpressionSemantic(Expressions exps, Scope sc) | |
340 { | |
341 if (exps) | |
342 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
343 foreach (ref Expression e; exps) |
0 | 344 { |
345 e = e.semantic(sc); | |
346 } | |
347 } | |
348 } | |
349 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
350 Expressions arrayExpressionToCommonType(Scope sc, Expressions exps, Type *pt) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
351 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
352 //version(DMDV1) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
353 // /* The first element sets the type |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
354 // */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
355 // Type *t0 = NULL; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
356 // for (size_t i = 0; i < exps->dim; i++) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
357 // { Expression *e = (Expression *)exps->data[i]; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
358 // |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
359 // if (!e->type) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
360 // { error("%s has no value", e->toChars()); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
361 // e = new ErrorExp(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
362 // } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
363 // e = resolveProperties(sc, e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
364 // |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
365 // if (!t0) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
366 // t0 = e->type; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
367 // else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
368 // e = e->implicitCastTo(sc, t0); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
369 // exps->data[i] = (void *)e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
370 // } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
371 // |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
372 // if (!t0) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
373 // t0 = Type::tvoid; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
374 // if (pt) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
375 // *pt = t0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
376 // |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
377 // // Eventually, we want to make this copy-on-write |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
378 // return exps; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
379 //} |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
380 version(DMDV2) { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
381 /* The type is determined by applying ?: to each pair. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
382 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
383 /* Still have a problem with: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
384 * ubyte[][] = [ cast(ubyte[])"hello", [1]]; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
385 * which works if the array literal is initialized top down with the ubyte[][] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
386 * type, but fails with this function doing bottom up typing. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
387 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
388 //printf("arrayExpressionToCommonType()\n"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
389 scope integerexp = new IntegerExp(0); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
390 scope condexp = new CondExp(Loc(0), integerexp, null, null); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
391 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
392 Type t0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
393 Expression e0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
394 int j0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
395 foreach (size_t i, Expression e; exps) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
396 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
397 e = resolveProperties(sc, e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
398 if (!e.type) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
399 { error("%s has no value", e.toChars()); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
400 e = new ErrorExp(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
401 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
402 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
403 if (t0) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
404 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
405 if (t0 != e.type) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
406 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
407 /* This applies ?: to merge the types. It's backwards; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
408 * ?: should call this function to merge types. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
409 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
410 condexp.type = null; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
411 condexp.e1 = e0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
412 condexp.e2 = e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
413 condexp.semantic(sc); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
414 exps[j0] = condexp.e1; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
415 e = condexp.e2; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
416 j0 = i; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
417 e0 = e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
418 t0 = e0.type; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
419 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
420 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
421 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
422 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
423 j0 = i; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
424 e0 = e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
425 t0 = e.type; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
426 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
427 exps[i] = e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
428 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
429 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
430 if (t0) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
431 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
432 foreach (ref Expression e; exps) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
433 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
434 e = e.implicitCastTo(sc, t0); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
435 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
436 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
437 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
438 t0 = Type.tvoid; // [] is typed as void[] |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
439 if (pt) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
440 *pt = t0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
441 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
442 // Eventually, we want to make this copy-on-write |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
443 return exps; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
444 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
445 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
446 |
0 | 447 /**************************************** |
448 * Preprocess arguments to function. | |
449 */ | |
450 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
451 void preFunctionParameters(Loc loc, Scope sc, Expressions exps) |
0 | 452 { |
453 if (exps) | |
454 { | |
455 expandTuples(exps); | |
57 | 456 |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
457 foreach (size_t i, ref Expression arg; exps) |
0 | 458 { |
459 if (!arg.type) | |
460 { | |
461 debug { | |
57 | 462 if (!global.gag) { |
0 | 463 writef("1: \n"); |
57 | 464 } |
0 | 465 } |
466 arg.error("%s is not an expression", arg.toChars()); | |
467 arg = new IntegerExp(arg.loc, 0, Type.tint32); | |
468 } | |
469 | |
470 arg = resolveProperties(sc, arg); | |
471 | |
472 //arg.rvalue(); | |
473 static if (false) { | |
474 if (arg.type.ty == TY.Tfunction) | |
475 { | |
476 arg = new AddrExp(arg.loc, arg); | |
477 arg = arg.semantic(sc); | |
478 } | |
479 } | |
480 } | |
481 } | |
482 } | |
483 | |
484 /************************************************************* | |
485 * Given var, we need to get the | |
486 * right 'this' pointer if var is in an outer class, but our | |
487 * existing 'this' pointer is in an inner class. | |
488 * Input: | |
489 * e1 existing 'this' | |
490 * ad struct or class we need the correct 'this' for | |
491 * var the specific member of ad we're accessing | |
492 */ | |
493 | |
494 Expression getRightThis(Loc loc, Scope sc, AggregateDeclaration ad, Expression e1, Declaration var) | |
495 { | |
496 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1.toChars(), ad.toChars(), var.toChars()); | |
497 L1: | |
498 Type t = e1.type.toBasetype(); | |
499 //printf("e1.type = %s, var.type = %s\n", e1.type.toChars(), var.type.toChars()); | |
500 | |
501 /* If e1 is not the 'this' pointer for ad | |
502 */ | |
503 if (ad && !(t.ty == TY.Tpointer && t.nextOf().ty == TY.Tstruct && (cast(TypeStruct)t.nextOf()).sym == ad) && !(t.ty == TY.Tstruct && (cast(TypeStruct)t).sym == ad)) | |
504 { | |
505 ClassDeclaration cd = ad.isClassDeclaration(); | |
506 ClassDeclaration tcd = t.isClassHandle(); | |
507 | |
508 /* e1 is the right this if ad is a base class of e1 | |
509 */ | |
510 if (!cd || !tcd || !(tcd == cd || cd.isBaseOf(tcd, null))) | |
511 { | |
512 /* Only classes can be inner classes with an 'outer' | |
513 * member pointing to the enclosing class instance | |
514 */ | |
515 if (tcd && tcd.isNested()) | |
516 { | |
517 /* e1 is the 'this' pointer for an inner class: tcd. | |
518 * Rewrite it as the 'this' pointer for the outer class. | |
519 */ | |
520 | |
521 e1 = new DotVarExp(loc, e1, tcd.vthis); | |
522 e1.type = tcd.vthis.type; | |
523 // Do not call checkNestedRef() | |
524 //e1 = e1.semantic(sc); | |
525 | |
526 // Skip up over nested functions, and get the enclosing | |
527 // class type. | |
528 int n = 0; | |
529 Dsymbol s; | |
530 for (s = tcd.toParent(); s && s.isFuncDeclaration(); s = s.toParent()) | |
531 { | |
532 FuncDeclaration f = s.isFuncDeclaration(); | |
533 if (f.vthis) | |
534 { | |
535 //printf("rewriting e1 to %s's this\n", f.toChars()); | |
536 n++; | |
537 e1 = new VarExp(loc, f.vthis); | |
538 } | |
539 } | |
540 if (s && s.isClassDeclaration()) | |
541 { | |
542 e1.type = s.isClassDeclaration().type; | |
543 if (n > 1) | |
544 e1 = e1.semantic(sc); | |
545 } | |
546 else | |
547 e1 = e1.semantic(sc); | |
548 goto L1; | |
549 } | |
550 /* Can't find a path from e1 to ad | |
551 */ | |
552 e1.error("this for %s needs to be type %s not type %s", var.toChars(), ad.toChars(), t.toChars()); | |
553 } | |
554 } | |
555 return e1; | |
556 } | |
557 | |
558 /******************************************* | |
559 * Given a symbol that could be either a FuncDeclaration or | |
560 * a function template, resolve it to a function symbol. | |
561 * sc instantiation scope | |
562 * loc instantiation location | |
563 * targsi initial list of template arguments | |
564 * ethis if !null, the 'this' pointer argument | |
565 * fargs arguments to function | |
566 * flags 1: do not issue error message on no match, just return null | |
567 */ | |
568 | |
569 FuncDeclaration resolveFuncCall(Scope sc, Loc loc, Dsymbol s, | |
570 Objects tiargs, | |
571 Expression ethis, | |
572 Expressions arguments, | |
573 int flags) | |
574 { | |
575 if (!s) | |
576 return null; // no match | |
577 FuncDeclaration f = s.isFuncDeclaration(); | |
578 if (f) | |
579 f = f.overloadResolve(loc, ethis, arguments); | |
580 else | |
581 { | |
582 TemplateDeclaration td = s.isTemplateDeclaration(); | |
583 assert(td); | |
584 f = td.deduceFunctionTemplate(sc, loc, tiargs, null, arguments, flags); | |
585 } | |
586 return f; | |
587 } | |
588 | |
589 /**************************************** | |
590 * Now that we know the exact type of the function we're calling, | |
591 * the arguments[] need to be adjusted: | |
592 * 1. implicitly convert argument to the corresponding parameter type | |
593 * 2. add default arguments for any missing arguments | |
594 * 3. do default promotions on arguments corresponding to ... | |
595 * 4. add hidden _arguments[] argument | |
596 * 5. call copy constructor for struct value arguments | |
597 */ | |
598 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
599 void functionParameters(Loc loc, Scope sc, TypeFunction tf, Expressions arguments) |
0 | 600 { |
601 uint n; | |
602 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
603 //printf("functionParameters()\n"); |
0 | 604 assert(arguments); |
605 size_t nargs = arguments ? arguments.dim : 0; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
606 size_t nparams = Parameter.dim(tf.parameters); |
0 | 607 |
608 if (nargs > nparams && tf.varargs == 0) | |
609 error(loc, "expected %zu arguments, not %zu for non-variadic function type %s", nparams, nargs, tf.toChars()); | |
610 | |
611 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
612 | |
613 int done = 0; | |
614 for (size_t i = 0; i < n; i++) | |
615 { | |
616 Expression arg; | |
617 | |
618 if (i < nargs) | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
619 arg = arguments[i]; |
0 | 620 else |
621 arg = null; | |
622 | |
623 Type tb; | |
624 | |
625 if (i < nparams) | |
626 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
627 auto p = Parameter.getNth(tf.parameters, i); |
0 | 628 |
629 if (!arg) | |
630 { | |
631 if (!p.defaultArg) | |
632 { | |
633 if (tf.varargs == 2 && i + 1 == nparams) | |
634 goto L2; | |
635 | |
636 error(loc, "expected %d function arguments, not %d", nparams, nargs); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
637 return; |
0 | 638 } |
639 arg = p.defaultArg; | |
640 arg = arg.copy(); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
641 version (DMDV2) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
642 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
643 arg = arg.resolveLoc(loc, sc); // __FILE__ and __LINE__ |
0 | 644 } |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
645 arguments.push(arg); |
0 | 646 nargs++; |
647 } | |
648 | |
649 if (tf.varargs == 2 && i + 1 == nparams) | |
650 { | |
651 //printf("\t\tvarargs == 2, p.type = '%s'\n", p.type.toChars()); | |
652 if (arg.implicitConvTo(p.type)) | |
653 { | |
654 if (nargs != nparams) | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
655 { |
0 | 656 error(loc, "expected %zu function arguments, not %zu", nparams, nargs); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
657 return; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
658 } |
0 | 659 goto L1; |
660 } | |
661 L2: | |
662 tb = p.type.toBasetype(); /// | |
663 Type tret = p.isLazyArray(); | |
664 switch (tb.ty) | |
665 { | |
666 case TY.Tsarray: | |
667 case TY.Tarray: | |
668 { // Create a static array variable v of type arg.type | |
669 version (IN_GCC) { | |
670 /* GCC 4.0 does not like zero length arrays used like | |
671 this; pass a null array value instead. Could also | |
672 just make a one-element array. */ | |
673 if (nargs - i == 0) | |
674 { | |
675 arg = new NullExp(loc); | |
676 break; | |
677 } | |
678 } | |
679 Identifier id = Lexer.uniqueId("__arrayArg"); | |
680 Type t = new TypeSArray((cast(TypeArray)tb).next, new IntegerExp(nargs - i)); | |
681 t = t.semantic(loc, sc); | |
682 VarDeclaration v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
683 v.semantic(sc); | |
684 v.parent = sc.parent; | |
685 //sc.insert(v); | |
686 | |
687 Expression c = new DeclarationExp(Loc(0), v); | |
688 c.type = v.type; | |
689 | |
690 for (size_t u = i; u < nargs; u++) | |
691 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
692 auto a = arguments[u]; |
0 | 693 if (tret && !(cast(TypeArray)tb).next.equals(a.type)) |
694 a = a.toDelegate(sc, tret); | |
695 | |
696 Expression e = new VarExp(loc, v); | |
697 e = new IndexExp(loc, e, new IntegerExp(u + 1 - nparams)); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
698 auto ae = new AssignExp(loc, e, a); |
0 | 699 |
700 version (DMDV2) { | |
701 ae.op = TOK.TOKconstruct; | |
702 } | |
703 | |
704 if (c) | |
705 c = new CommaExp(loc, c, ae); | |
706 else | |
707 c = ae; | |
708 } | |
709 | |
710 arg = new VarExp(loc, v); | |
711 if (c) | |
712 arg = new CommaExp(loc, c, arg); | |
713 break; | |
714 } | |
715 | |
716 case TY.Tclass: | |
717 { /* Set arg to be: | |
718 * new Tclass(arg0, arg1, ..., argn) | |
719 */ | |
720 Expressions args = new Expressions(); | |
721 args.setDim(nargs - i); | |
722 for (size_t u = i; u < nargs; u++) | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
723 args[u - i] = arguments[u]; |
0 | 724 arg = new NewExp(loc, null, null, p.type, args); |
725 break; | |
726 } | |
727 | |
728 default: | |
729 if (!arg) | |
730 { | |
731 error(loc, "not enough arguments"); | |
732 return; | |
733 } | |
734 break; | |
735 } | |
736 | |
737 arg = arg.semantic(sc); | |
738 //printf("\targ = '%s'\n", arg.toChars()); | |
739 arguments.setDim(i + 1); | |
740 done = 1; | |
741 } | |
742 | |
743 L1: | |
744 if (!(p.storageClass & STC.STClazy && p.type.ty == TY.Tvoid)) | |
745 { | |
746 if (p.type != arg.type) | |
747 { | |
748 //printf("arg.type = %s, p.type = %s\n", arg.type.toChars(), p.type.toChars()); | |
109 | 749 if (arg.op == TOKtype) |
750 arg.error("cannot pass type %s as function argument", arg.toChars()); | |
0 | 751 arg = arg.implicitCastTo(sc, p.type); |
752 arg = arg.optimize(WANT.WANTvalue); | |
753 } | |
754 } | |
755 if (p.storageClass & STC.STCref) | |
756 { | |
757 arg = arg.toLvalue(sc, arg); | |
758 } | |
759 else if (p.storageClass & STC.STCout) | |
760 { | |
761 arg = arg.modifiableLvalue(sc, arg); | |
762 } | |
763 | |
109 | 764 tb = arg.type.toBasetype(); |
765 version(SARRAYVALUE) {} else | |
766 { | |
0 | 767 // Convert static arrays to pointers |
768 if (tb.ty == TY.Tsarray) | |
769 { | |
770 arg = arg.checkToPointer(); | |
771 } | |
109 | 772 } |
0 | 773 version (DMDV2) { |
774 if (tb.ty == TY.Tstruct && !(p.storageClass & (STC.STCref | STC.STCout))) | |
775 { | |
776 arg = callCpCtor(loc, sc, arg); | |
777 } | |
778 } | |
779 | |
780 // Convert lazy argument to a delegate | |
781 if (p.storageClass & STC.STClazy) | |
782 { | |
783 arg = arg.toDelegate(sc, p.type); | |
784 } | |
785 version (DMDV2) { | |
786 /* Look for arguments that cannot 'escape' from the called | |
787 * function. | |
788 */ | |
789 if (!tf.parameterEscapes(p)) | |
790 { | |
791 /* Function literals can only appear once, so if this | |
792 * appearance was scoped, there cannot be any others. | |
793 */ | |
794 if (arg.op == TOK.TOKfunction) | |
795 { | |
796 FuncExp fe = cast(FuncExp)arg; | |
797 fe.fd.tookAddressOf = 0; | |
798 } | |
799 | |
800 /* For passing a delegate to a scoped parameter, | |
801 * this doesn't count as taking the address of it. | |
802 * We only worry about 'escaping' references to the function. | |
803 */ | |
804 else if (arg.op == TOK.TOKdelegate) | |
805 { | |
806 DelegateExp de = cast(DelegateExp)arg; | |
807 if (de.e1.op == TOK.TOKvar) | |
808 { | |
809 VarExp ve = cast(VarExp)de.e1; | |
810 FuncDeclaration f = ve.var.isFuncDeclaration(); | |
811 if (f) | |
812 { | |
813 f.tookAddressOf--; | |
814 //printf("tookAddressOf = %d\n", f.tookAddressOf); | |
815 } | |
816 } | |
817 } | |
818 } | |
819 } | |
820 } | |
821 else | |
822 { | |
823 // If not D linkage, do promotions | |
824 if (tf.linkage != LINK.LINKd) | |
825 { | |
826 // Promote bytes, words, etc., to ints | |
827 arg = arg.integralPromotions(sc); | |
828 | |
829 // Promote floats to doubles | |
830 switch (arg.type.ty) | |
831 { | |
832 case TY.Tfloat32: | |
833 arg = arg.castTo(sc, Type.tfloat64); | |
834 break; | |
835 | |
836 case TY.Timaginary32: | |
837 arg = arg.castTo(sc, Type.timaginary64); | |
838 break; | |
839 default: | |
840 break; | |
841 } | |
842 } | |
843 | |
844 // Convert static arrays to dynamic arrays | |
845 tb = arg.type.toBasetype(); | |
846 if (tb.ty == TY.Tsarray) | |
847 { | |
848 TypeSArray ts = cast(TypeSArray)tb; | |
849 Type ta = ts.next.arrayOf(); | |
850 if (ts.size(arg.loc) == 0) | |
851 { | |
852 arg = new NullExp(arg.loc); | |
853 arg.type = ta; | |
854 } | |
855 else | |
856 { | |
857 arg = arg.castTo(sc, ta); | |
858 } | |
859 } | |
860 version (DMDV2) { | |
73 | 861 if (tb.ty == Tstruct) |
0 | 862 { |
863 arg = callCpCtor(loc, sc, arg); | |
864 } | |
73 | 865 } |
0 | 866 |
73 | 867 // Give error for overloaded function addresses |
868 if (arg.op == TOKsymoff) | |
869 { | |
870 SymOffExp se = cast(SymOffExp)arg; | |
871 version (DMDV2) { | |
872 bool aux = (se.hasOverloads != 0); | |
873 } else { | |
874 bool aux = true; | |
0 | 875 } |
73 | 876 if (aux && !se.var.isFuncDeclaration().isUnique()) |
877 arg.error("function %s is overloaded", arg.toChars()); | |
878 } | |
879 | |
880 arg.rvalue(); | |
881 } | |
882 | |
0 | 883 arg = arg.optimize(WANT.WANTvalue); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
884 arguments[i] = arg; |
0 | 885 if (done) |
886 break; | |
887 } | |
888 | |
889 // If D linkage and variadic, add _arguments[] as first argument | |
890 if (tf.linkage == LINK.LINKd && tf.varargs == 1) | |
891 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
892 assert(arguments.dim >= nparams); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
893 auto e = createTypeInfoArray(sc, &arguments[nparams], arguments.dim - nparams); |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
894 arguments.insert(0, e); |
0 | 895 } |
896 } | |
897 | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
898 /****************************** |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
899 * Perform canThrow() on an array of Expressions. |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
900 */ |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
901 |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
902 version (DMDV2) { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
903 bool arrayExpressionCanThrow(Expressions exps) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
904 { |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
905 if (exps) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
906 { |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
907 foreach (e; exps) |
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
908 { |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
909 if (e && e.canThrow()) |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
910 return true; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
911 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
912 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
913 return false; |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
914 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
915 } |
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
916 |
0 | 917 /**************************************** |
918 * Expand tuples. | |
919 */ | |
920 | |
921 void expandTuples(Expressions exps) | |
922 { | |
923 //printf("expandTuples()\n"); | |
924 if (exps) | |
925 { | |
926 for (size_t i = 0; i < exps.dim; i++) | |
927 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
928 auto arg = exps[i]; |
0 | 929 if (!arg) |
930 continue; | |
931 | |
932 // Look for tuple with 0 members | |
933 if (arg.op == TOK.TOKtype) | |
57 | 934 { |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
935 auto e = cast(TypeExp)arg; |
0 | 936 if (e.type.toBasetype().ty == TY.Ttuple) |
937 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
938 auto tt = cast(TypeTuple)e.type.toBasetype(); |
0 | 939 |
940 if (!tt.arguments || tt.arguments.dim == 0) | |
941 { | |
942 exps.remove(i); | |
943 if (i == exps.dim) | |
944 return; | |
945 i--; | |
946 continue; | |
947 } | |
948 } | |
949 } | |
950 | |
951 // Inline expand all the tuples | |
952 while (arg.op == TOK.TOKtuple) | |
953 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
954 auto te = cast(TupleExp)arg; |
0 | 955 |
956 exps.remove(i); // remove arg | |
57 | 957 exps.insert(i, te.exps); // replace with tuple contents |
0 | 958 |
959 if (i == exps.dim) | |
960 return; // empty tuple, no more arguments | |
961 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
962 arg = exps[i]; |
0 | 963 } |
964 } | |
965 } | |
966 } | |
967 | |
968 /************************************************** | |
969 * Write out argument types to buf. | |
970 */ | |
971 | |
972 void argExpTypesToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
973 { | |
974 if (arguments) | |
975 { | |
976 scope OutBuffer argbuf = new OutBuffer(); | |
977 | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
978 foreach (size_t i, Expression arg; arguments) |
0 | 979 { |
980 if (i) | |
981 buf.writeByte(','); | |
982 | |
983 argbuf.reset(); | |
984 arg.type.toCBuffer2(argbuf, hgs, MOD.MODundefined); | |
985 buf.write(argbuf); | |
986 } | |
987 } | |
988 } | |
989 | |
990 /**************************************** | |
991 * Determine if scope sc has package level access to s. | |
992 */ | |
993 | |
994 bool hasPackageAccess(Scope sc, Dsymbol s) | |
995 { | |
996 version (LOG) { | |
997 printf("hasPackageAccess(s = '%s', sc = '%p')\n", s.toChars(), sc); | |
998 } | |
999 | |
1000 for (; s; s = s.parent) | |
1001 { | |
1002 if (s.isPackage() && !s.isModule()) | |
1003 break; | |
1004 } | |
1005 version (LOG) { | |
1006 if (s) | |
1007 printf("\tthis is in package '%s'\n", s.toChars()); | |
1008 } | |
1009 | |
1010 if (s && s == sc.module_.parent) | |
1011 { | |
1012 version (LOG) { | |
1013 printf("\ts is in same package as sc\n"); | |
1014 } | |
1015 return true; | |
1016 } | |
1017 | |
1018 | |
1019 version (LOG) { | |
1020 printf("\tno package access\n"); | |
1021 } | |
1022 | |
1023 return false; | |
1024 } | |
1025 | |
1026 /********************************************* | |
1027 * Call copy constructor for struct value argument. | |
1028 */ | |
1029 version (DMDV2) { | |
1030 Expression callCpCtor(Loc loc, Scope sc, Expression e) | |
1031 { | |
1032 Type tb = e.type.toBasetype(); | |
1033 assert(tb.ty == Tstruct); | |
1034 StructDeclaration sd = (cast(TypeStruct)tb).sym; | |
1035 if (sd.cpctor) | |
1036 { | |
1037 /* Create a variable tmp, and replace the argument e with: | |
1038 * (tmp = e),tmp | |
1039 * and let AssignExp() handle the construction. | |
1040 * This is not the most efficent, ideally tmp would be constructed | |
1041 * directly onto the stack. | |
1042 */ | |
1043 Identifier idtmp = Lexer.uniqueId("__tmp"); | |
1044 VarDeclaration tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(Loc(0), e)); | |
1045 Expression ae = new DeclarationExp(loc, tmp); | |
1046 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
1047 e = e.semantic(sc); | |
1048 } | |
1049 return e; | |
1050 } | |
1051 } | |
1052 | |
1053 /*************************************** | |
1054 * Create a static array of TypeInfo references | |
1055 * corresponding to an array of Expression's. | |
1056 * Used to supply hidden _arguments[] value for variadic D functions. | |
1057 */ | |
1058 | |
1059 Expression createTypeInfoArray(Scope sc, Expression* exps, int dim) | |
1060 { | |
79 | 1061 static if (true) |
1062 { | |
57 | 1063 /* Get the corresponding TypeInfo_Tuple and |
1064 * point at its elements[]. | |
1065 */ | |
1066 | |
1067 /* Create the TypeTuple corresponding to the types of args[] | |
1068 */ | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1069 auto args = new Parameters; |
57 | 1070 args.setDim(dim); |
1071 for (size_t i = 0; i < dim; i++) | |
1072 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1073 auto arg = new Parameter(STCin, exps[i].type, null, null); |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1074 args[i] = arg; |
57 | 1075 } |
1076 TypeTuple tup = new TypeTuple(args); | |
1077 Expression e = tup.getTypeInfo(sc); | |
1078 e = e.optimize(WANTvalue); | |
1079 assert(e.op == TOKsymoff); // should be SymOffExp | |
1080 | |
79 | 1081 version (BREAKABI) |
1082 { | |
57 | 1083 /* |
1084 * Should just pass a reference to TypeInfo_Tuple instead, | |
1085 * but that would require existing code to be recompiled. | |
1086 * Source compatibility can be maintained by computing _arguments[] | |
1087 * at the start of the called function by offseting into the | |
1088 * TypeInfo_Tuple reference. | |
1089 */ | |
1090 | |
79 | 1091 } |
1092 else | |
1093 { | |
57 | 1094 // Advance to elements[] member of TypeInfo_Tuple |
1095 SymOffExp se = cast(SymOffExp)e; | |
1096 se.offset += PTRSIZE + PTRSIZE; | |
1097 | |
1098 // Set type to TypeInfo[]* | |
1099 se.type = Type.typeinfo.type.arrayOf().pointerTo(); | |
1100 | |
1101 // Indirect to get the _arguments[] value | |
1102 e = new PtrExp(Loc(0), se); | |
1103 e.type = se.type.next; | |
1104 } | |
1105 return e; | |
79 | 1106 } // of static if (true) |
1107 else | |
1108 { | |
57 | 1109 /* Improvements: |
1110 * 1) create an array literal instead, | |
1111 * as it would eliminate the extra dereference of loading the | |
1112 * static variable. | |
1113 */ | |
1114 | |
1115 ArrayInitializer ai = new ArrayInitializer(Loc(0)); | |
1116 VarDeclaration v; | |
1117 Type t; | |
1118 Expression e; | |
1119 scope OutBuffer buf = new OutBuffer(); | |
1120 Identifier id; | |
1121 string name; | |
1122 | |
1123 // Generate identifier for _arguments[] | |
1124 buf.writestring("_arguments_"); | |
1125 for (int i = 0; i < dim; i++) | |
1126 { | |
1127 t = exps[i].type; | |
1128 t.toDecoBuffer(buf); | |
1129 } | |
1130 buf.writeByte(0); | |
1131 id = Lexer.idPool(buf.extractString()); | |
1132 | |
1133 Module m = sc.module_; | |
1134 Dsymbol s = m.symtab.lookup(id); | |
1135 | |
1136 if (s && s.parent == m) | |
1137 { | |
1138 // Use existing one | |
1139 v = s.isVarDeclaration(); | |
1140 assert(v); | |
1141 } | |
1142 else | |
1143 { | |
1144 // Generate new one | |
1145 | |
1146 for (int i = 0; i < dim; i++) | |
1147 { | |
1148 t = exps[i].type; | |
1149 e = t.getTypeInfo(sc); | |
1150 ai.addInit(new IntegerExp(i), new ExpInitializer(0, e)); | |
1151 } | |
1152 | |
1153 t = Type.typeinfo.type.arrayOf(); | |
1154 ai.type = t; | |
1155 v = new VarDeclaration(0, t, id, ai); | |
1156 m.members.push(v); | |
79 | 1157 m.symtabInsert(v); |
57 | 1158 sc = sc.push(); |
1159 sc.linkage = LINKc; | |
1160 sc.stc = STCstatic | STCcomdat; | |
1161 ai.semantic(sc, t); | |
1162 v.semantic(sc); | |
1163 v.parent = m; | |
1164 sc = sc.pop(); | |
1165 } | |
1166 e = new VarExp(0, v); | |
1167 e = e.semantic(sc); | |
1168 return e; | |
1169 } | |
0 | 1170 } |
1171 | |
1172 /************************************** | |
1173 * Evaluate builtin function. | |
1174 * Return result: null if cannot evaluate it. | |
1175 */ | |
1176 | |
53 | 1177 extern(C) extern real sinl(real); |
1178 extern(C) extern real cosl(real); | |
1179 extern(C) extern real tanl(real); | |
1180 extern(C) extern real sqrtl(real); | |
1181 extern(C) extern real fabsl(real); | |
1182 | |
0 | 1183 Expression eval_builtin(BUILTIN builtin, Expressions arguments) |
1184 { | |
53 | 1185 assert(arguments && arguments.dim); |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1186 auto arg0 = arguments[0]; |
53 | 1187 Expression e = null; |
1188 switch (builtin) | |
1189 { | |
1190 case BUILTINsin: | |
1191 if (arg0.op == TOKfloat64) | |
1192 e = new RealExp(Loc(0), sinl(arg0.toReal()), arg0.type); | |
1193 break; | |
1194 | |
1195 case BUILTINcos: | |
1196 if (arg0.op == TOKfloat64) | |
1197 e = new RealExp(Loc(0), cosl(arg0.toReal()), arg0.type); | |
1198 break; | |
1199 | |
1200 case BUILTINtan: | |
1201 if (arg0.op == TOKfloat64) | |
1202 e = new RealExp(Loc(0), tanl(arg0.toReal()), arg0.type); | |
1203 break; | |
1204 | |
1205 case BUILTINsqrt: | |
1206 if (arg0.op == TOKfloat64) | |
1207 e = new RealExp(Loc(0), sqrtl(arg0.toReal()), arg0.type); | |
1208 break; | |
1209 | |
1210 case BUILTINfabs: | |
1211 if (arg0.op == TOKfloat64) | |
1212 e = new RealExp(Loc(0), fabsl(arg0.toReal()), arg0.type); | |
1213 break; | |
1214 } | |
1215 return e; | |
0 | 1216 } |
1217 | |
1218 Expression fromConstInitializer(int result, Expression e1) | |
1219 { | |
1220 //printf("fromConstInitializer(result = %x, %s)\n", result, e1.toChars()); | |
1221 //static int xx; if (xx++ == 10) assert(0); | |
1222 Expression e = e1; | |
1223 if (e1.op == TOK.TOKvar) | |
1224 { | |
1225 VarExp ve = cast(VarExp)e1; | |
1226 VarDeclaration v = ve.var.isVarDeclaration(); | |
1227 e = expandVar(result, v); | |
1228 if (e) | |
1229 { | |
1230 if (e.type != e1.type) | |
1231 { | |
1232 // Type 'paint' operation | |
1233 e = e.copy(); | |
1234 e.type = e1.type; | |
1235 } | |
79 | 1236 e.loc = e1.loc; |
0 | 1237 } |
1238 else | |
1239 { | |
1240 e = e1; | |
1241 } | |
1242 } | |
1243 return e; | |
1244 } | |
1245 | |
1246 /************************************* | |
1247 * If variable has a const initializer, | |
1248 * return that initializer. | |
1249 */ | |
1250 | |
1251 Expression expandVar(int result, VarDeclaration v) | |
1252 { | |
1253 //printf("expandVar(result = %d, v = %p, %s)\n", result, v, v ? v.toChars() : "null"); | |
1254 | |
1255 Expression e = null; | |
1256 if (!v) | |
1257 return e; | |
1258 | |
1259 if (v.isConst() || v.isInvariant() || v.storage_class & STC.STCmanifest) | |
1260 { | |
1261 if (!v.type) | |
1262 { | |
1263 //error("ICE"); | |
1264 return e; | |
1265 } | |
1266 | |
1267 Type tb = v.type.toBasetype(); | |
1268 if (result & WANT.WANTinterpret || v.storage_class & STC.STCmanifest || (tb.ty != TY.Tsarray && tb.ty != TY.Tstruct)) | |
1269 { | |
1270 if (v.init) | |
1271 { | |
1272 if (v.inuse) | |
1273 { | |
1274 if (v.storage_class & STC.STCmanifest) | |
1275 v.error("recursive initialization of constant"); | |
1276 goto L1; | |
1277 } | |
1278 Expression ei = v.init.toExpression(); | |
1279 if (!ei) | |
1280 goto L1; | |
1281 if (ei.op == TOK.TOKconstruct || ei.op == TOK.TOKblit) | |
1282 { | |
1283 AssignExp ae = cast(AssignExp)ei; | |
1284 ei = ae.e2; | |
1285 if (ei.isConst() != 1 && ei.op != TOK.TOKstring) | |
1286 goto L1; | |
1287 if (ei.type != v.type) | |
1288 goto L1; | |
1289 } | |
1290 if (v.scope_) | |
1291 { | |
1292 v.inuse++; | |
1293 e = ei.syntaxCopy(); | |
1294 e = e.semantic(v.scope_); | |
1295 e = e.implicitCastTo(v.scope_, v.type); | |
1296 // enabling this line causes test22 in test suite to fail | |
1297 //ei.type = e.type; | |
1298 v.scope_ = null; | |
1299 v.inuse--; | |
1300 } | |
1301 else if (!ei.type) | |
1302 { | |
1303 goto L1; | |
1304 } | |
1305 else | |
1306 // Should remove the copy() operation by | |
1307 // making all mods to expressions copy-on-write | |
1308 e = ei.copy(); | |
1309 } | |
1310 else | |
1311 { | |
1312 static if (true) { | |
1313 goto L1; | |
1314 } else { | |
1315 // BUG: what if const is initialized in constructor? | |
1316 e = v.type.defaultInit(); | |
1317 e.loc = e1.loc; | |
1318 } | |
1319 } | |
1320 if (e.type != v.type) | |
1321 { | |
1322 e = e.castTo(null, v.type); | |
1323 } | |
1324 v.inuse++; | |
1325 e = e.optimize(result); | |
1326 v.inuse--; | |
1327 } | |
1328 } | |
1329 L1: | |
1330 //if (e) printf("\te = %s, e.type = %s\n", e.toChars(), e.type.toChars()); | |
1331 return e; | |
1332 } | |
1333 | |
1334 /**************************************** | |
1335 * Check access to d for expression e.d | |
1336 */ | |
1337 | |
1338 void accessCheck(Loc loc, Scope sc, Expression e, Declaration d) | |
1339 { | |
1340 version (LOG) { | |
1341 if (e) | |
1342 { | |
1343 printf("accessCheck(%s . %s)\n", e.toChars(), d.toChars()); | |
1344 printf("\te.type = %s\n", e.type.toChars()); | |
1345 } | |
1346 else | |
1347 { | |
1348 //printf("accessCheck(%s)\n", d.toChars()); | |
1349 } | |
1350 } | |
1351 if (!e) | |
1352 { | |
1353 if (d.prot() == PROT.PROTprivate && d.getModule() != sc.module_ || | |
1354 d.prot() == PROT.PROTpackage && !hasPackageAccess(sc, d)) | |
1355 | |
1356 error(loc, "%s %s.%s is not accessible from %s", | |
1357 d.kind(), d.getModule().toChars(), d.toChars(), sc.module_.toChars()); | |
1358 } | |
1359 else if (e.type.ty == TY.Tclass) | |
1360 { | |
1361 // Do access check | |
1362 ClassDeclaration cd; | |
1363 | |
1364 cd = cast(ClassDeclaration)((cast(TypeClass)e.type).sym); | |
1365 static if (true) { | |
1366 if (e.op == TOK.TOKsuper) | |
1367 { | |
1368 ClassDeclaration cd2 = sc.func.toParent().isClassDeclaration(); | |
1369 if (cd2) | |
1370 cd = cd2; | |
1371 } | |
1372 } | |
1373 cd.accessCheck(loc, sc, d); | |
1374 } | |
1375 else if (e.type.ty == TY.Tstruct) | |
1376 { | |
1377 // Do access check | |
1378 StructDeclaration cd = cast(StructDeclaration)((cast(TypeStruct)e.type).sym); | |
1379 cd.accessCheck(loc, sc, d); | |
1380 } | |
1381 } | |
1382 | |
1383 /***************************************** | |
1384 * Given array of arguments and an aggregate type, | |
1385 * if any of the argument types are missing, attempt to infer | |
1386 * them from the aggregate type. | |
1387 */ | |
1388 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1389 void inferApplyArgTypes(TOK op, Parameters arguments, Expression aggr) |
0 | 1390 { |
1391 if (!arguments || !arguments.dim) | |
1392 return; | |
1393 | |
1394 /* Return if no arguments need types. | |
1395 */ | |
1396 for (size_t u = 0; 1; u++) | |
1397 { | |
1398 if (u == arguments.dim) | |
1399 return; | |
1400 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1401 auto arg = arguments[u]; |
0 | 1402 if (!arg.type) |
1403 break; | |
1404 } | |
1405 | |
1406 AggregateDeclaration ad; | |
1407 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1408 auto arg = arguments[0]; |
0 | 1409 Type taggr = aggr.type; |
1410 if (!taggr) | |
1411 return; | |
1412 Type tab = taggr.toBasetype(); | |
1413 switch (tab.ty) | |
1414 { | |
1415 case TY.Tarray: | |
1416 case TY.Tsarray: | |
1417 case TY.Ttuple: | |
1418 if (arguments.dim == 2) | |
1419 { | |
1420 if (!arg.type) | |
1421 arg.type = Type.tsize_t; // key type | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1422 arg = arguments[1]; |
0 | 1423 } |
1424 if (!arg.type && tab.ty != TY.Ttuple) | |
1425 arg.type = tab.nextOf(); // value type | |
1426 break; | |
1427 | |
1428 case TY.Taarray: | |
1429 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1430 auto taa = cast(TypeAArray)tab; |
0 | 1431 |
1432 if (arguments.dim == 2) | |
1433 { | |
1434 if (!arg.type) | |
1435 arg.type = taa.index; // key type | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
1436 arg = arguments[1]; |
0 | 1437 } |
1438 if (!arg.type) | |
1439 arg.type = taa.next; // value type | |
1440 break; | |
1441 } | |
1442 | |
1443 case TY.Tclass: | |
1444 ad = (cast(TypeClass)tab).sym; | |
1445 goto Laggr; | |
1446 | |
1447 case TY.Tstruct: | |
1448 ad = (cast(TypeStruct)tab).sym; | |
1449 goto Laggr; | |
1450 | |
1451 Laggr: | |
1452 if (arguments.dim == 1) | |
1453 { | |
1454 if (!arg.type) | |
1455 { | |
1456 /* Look for a head() or rear() overload | |
1457 */ | |
1458 Identifier id = (op == TOK.TOKforeach) ? Id.Fhead : Id.Ftoe; | |
1459 Dsymbol s = search_function(ad, id); | |
1460 FuncDeclaration fd = s ? s.isFuncDeclaration() : null; | |
1461 if (!fd) | |
1462 { | |
1463 if (s && s.isTemplateDeclaration()) | |
1464 break; | |
1465 goto Lapply; | |
1466 } | |
1467 arg.type = fd.type.nextOf(); | |
1468 } | |
1469 break; | |
1470 } | |
1471 | |
1472 Lapply: | |
1473 { /* Look for an | |
1474 * int opApply(int delegate(ref Type [, ...]) dg); | |
1475 * overload | |
1476 */ | |
1477 Dsymbol s = search_function(ad, (op == TOK.TOKforeach_reverse) ? Id.applyReverse : Id.apply); | |
1478 if (s) | |
1479 { | |
1480 FuncDeclaration fd = s.isFuncDeclaration(); | |
1481 if (fd) | |
1482 { | |
1483 inferApplyArgTypesX(fd, arguments); | |
1484 break; | |
1485 } | |
1486 static if (false) { | |
1487 TemplateDeclaration td = s.isTemplateDeclaration(); | |
1488 if (td) | |
1489 { | |
1490 inferApplyArgTypesZ(td, arguments); | |
1491 break; | |
1492 } | |
1493 } | |
1494 } | |
1495 break; | |
1496 } | |
1497 | |
1498 case TY.Tdelegate: | |
1499 { | |
1500 if (0 && aggr.op == TOK.TOKdelegate) | |
1501 { | |
1502 DelegateExp de = cast(DelegateExp)aggr; | |
1503 | |
1504 FuncDeclaration fd = de.func.isFuncDeclaration(); | |
1505 if (fd) | |
1506 inferApplyArgTypesX(fd, arguments); | |
1507 } | |
1508 else | |
1509 { | |
1510 inferApplyArgTypesY(cast(TypeFunction)tab.nextOf(), arguments); | |
1511 } | |
1512 break; | |
1513 } | |
1514 | |
1515 default: | |
1516 break; // ignore error, caught later | |
1517 } | |
1518 } | |
1519 | |
1520 struct Param3 | |
1521 { | |
1522 /******************************** | |
1523 * Recursive helper function, | |
1524 * analogous to func.overloadResolveX(). | |
1525 */ | |
1526 | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
1527 int fp3(void*, FuncDeclaration f) |
0 | 1528 { |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1529 auto tf = cast(TypeFunction)f.type; |
0 | 1530 if (inferApplyArgTypesY(tf, arguments) == 1) |
1531 return 0; | |
1532 | |
1533 if (arguments.dim == 0) | |
1534 return 1; | |
1535 | |
1536 return 0; | |
1537 } | |
1538 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1539 Parameters arguments; |
0 | 1540 } |
1541 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1542 void inferApplyArgTypesX(FuncDeclaration fstart, Parameters arguments) |
0 | 1543 { |
1544 Param3 p3; | |
1545 p3.arguments = arguments; | |
17
ddae60498573
Implemented mixin statements, DefaultInitExps, FileInitExps, LineInitExps and __traits.
Robert Clipsham <robert@octarineparrot.com>
parents:
16
diff
changeset
|
1546 overloadApply(fstart, &p3.fp3, cast(void*)arguments); |
0 | 1547 } |
1548 | |
1549 /****************************** | |
1550 * Infer arguments from type of function. | |
1551 * Returns: | |
1552 * 0 match for this function | |
1553 * 1 no match for this function | |
1554 */ | |
1555 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1556 int inferApplyArgTypesY(TypeFunction tf, Parameters arguments) |
0 | 1557 { |
1558 size_t nparams; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1559 Parameter p; |
0 | 1560 |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1561 if (Parameter.dim(tf.parameters) != 1) |
0 | 1562 goto Lnomatch; |
1563 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1564 p = Parameter.getNth(tf.parameters, 0); |
0 | 1565 if (p.type.ty != TY.Tdelegate) |
1566 goto Lnomatch; | |
1567 | |
1568 tf = cast(TypeFunction)p.type.nextOf(); | |
1569 assert(tf.ty == TY.Tfunction); | |
1570 | |
1571 /* We now have tf, the type of the delegate. Match it against | |
1572 * the arguments, filling in missing argument types. | |
1573 */ | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1574 nparams = Parameter.dim(tf.parameters); |
0 | 1575 if (nparams == 0 || tf.varargs) |
1576 goto Lnomatch; // not enough parameters | |
1577 if (arguments.dim != nparams) | |
1578 goto Lnomatch; // not enough parameters | |
1579 | |
1580 for (size_t u = 0; u < nparams; u++) | |
1581 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1582 auto arg = arguments[u]; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1583 auto param = Parameter.getNth(tf.parameters, u); |
0 | 1584 if (arg.type) |
1585 { | |
1586 if (!arg.type.equals(param.type)) | |
1587 { | |
1588 /* Cannot resolve argument types. Indicate an | |
1589 * error by setting the number of arguments to 0. | |
1590 */ | |
1591 arguments.dim = 0; | |
1592 goto Lmatch; | |
1593 } | |
1594 continue; | |
1595 } | |
1596 arg.type = param.type; | |
1597 } | |
1598 | |
1599 Lmatch: | |
1600 return 0; | |
1601 | |
1602 Lnomatch: | |
1603 return 1; | |
1604 } | |
1605 | |
1606 /************************************************** | |
1607 * Write expression out to buf, but wrap it | |
1608 * in ( ) if its precedence is less than pr. | |
1609 */ | |
1610 | |
1611 void expToCBuffer(OutBuffer buf, HdrGenState* hgs, Expression e, PREC pr) | |
1612 { | |
1613 //if (precedence[e.op] == 0) e.dump(0); | |
1614 if (precedence[e.op] < pr || | |
1615 /* Despite precedence, we don't allow a<b<c expressions. | |
1616 * They must be parenthesized. | |
1617 */ | |
1618 (pr == PREC.PREC_rel && precedence[e.op] == pr)) | |
1619 { | |
1620 buf.writeByte('('); | |
1621 e.toCBuffer(buf, hgs); | |
1622 buf.writeByte(')'); | |
1623 } | |
1624 else | |
1625 e.toCBuffer(buf, hgs); | |
1626 } | |
1627 | |
1628 /************************************************** | |
1629 * Write out argument list to buf. | |
1630 */ | |
1631 | |
1632 void argsToCBuffer(OutBuffer buf, Expressions arguments, HdrGenState* hgs) | |
1633 { | |
1634 if (arguments) | |
1635 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1636 foreach (size_t i, Expression arg; arguments) |
0 | 1637 { |
1638 if (arg) | |
1639 { | |
1640 if (i) | |
1641 buf.writeByte(','); | |
1642 expToCBuffer(buf, hgs, arg, PREC.PREC_assign); | |
1643 } | |
1644 } | |
1645 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
1646 } |
53 | 1647 |
1648 ulong getMask(ulong v) | |
1649 { | |
1650 ulong u = 0; | |
1651 if (v >= 0x80) | |
1652 u = 0xFF; | |
1653 while (u < v) | |
1654 u = (u << 1) | 1; | |
1655 return u; | |
64 | 1656 } |
1657 | |
1658 /****************************** | |
1659 * Perform scanForNestedRef() on an array of Expressions. | |
1660 */ | |
1661 | |
1662 void arrayExpressionScanForNestedRef(Scope sc, Expressions a) | |
1663 { | |
1664 //printf("arrayExpressionScanForNestedRef(%p)\n", a); | |
1665 if (a) | |
1666 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
79
diff
changeset
|
1667 foreach (e; a) |
64 | 1668 { |
1669 if (e) | |
1670 { | |
1671 e.scanForNestedRef(sc); | |
1672 } | |
1673 } | |
1674 } | |
129 | 1675 } |
1676 | |
1677 void realToMangleBuffer(OutBuffer buf, real value) | |
1678 { | |
1679 /* Rely on %A to get portable mangling. | |
1680 * Must munge result to get only identifier characters. | |
1681 * | |
1682 * Possible values from %A => mangled result | |
1683 * NAN => NAN | |
1684 * -INF => NINF | |
1685 * INF => INF | |
1686 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79 | |
1687 * 0X1.9P+2 => 19P2 | |
1688 */ | |
1689 | |
1690 if (isnan(value)) | |
1691 buf.writestring("NAN"); // no -NAN bugs | |
1692 else | |
1693 { | |
1694 char buffer[32]; | |
1695 int n = sprintf(buffer.ptr, "%LA", value); | |
1696 assert(n > 0 && n < buffer.sizeof); | |
1697 for (int i = 0; i < n; i++) | |
1698 { char c = buffer[i]; | |
1699 | |
1700 switch (c) | |
1701 { | |
1702 case '-': | |
1703 buf.writeByte('N'); | |
1704 break; | |
1705 | |
1706 case '+': | |
1707 case 'X': | |
1708 case '.': | |
1709 break; | |
1710 | |
1711 case '0': | |
1712 if (i < 2) | |
1713 break; // skip leading 0X | |
1714 default: | |
1715 buf.writeByte(c); | |
1716 break; | |
1717 } | |
1718 } | |
1719 } | |
1720 } | |
1721 | |
1722 /******************************** | |
1723 * Test to see if two reals are the same. | |
1724 * Regard NaN's as equivalent. | |
1725 * Regard +0 and -0 as different. | |
1726 */ | |
1727 | |
1728 int RealEquals(real x1, real x2) | |
1729 { | |
1730 return (isnan(x1) && isnan(x2)) || | |
1731 /* In some cases, the REALPAD bytes get garbage in them, | |
1732 * so be sure and ignore them. | |
1733 */ | |
1734 memcmp(&x1, &x2, REALSIZE - REALPAD) == 0; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
1735 } |