annotate dmd/expression/Util.d @ 79:43073c7c7769

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