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