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