Mercurial > projects > ddmd
annotate dmd/TypeFunction.d @ 130:60bb0fe4563e
dmdfe 2.037 first main iteration
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Thu, 09 Sep 2010 22:51:44 +0100 |
parents | 010eb8f0e18d |
children | 206db751bd4c |
rev | line source |
---|---|
0 | 1 module dmd.TypeFunction; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.TypeNext; |
5 import dmd.TypeSArray; | |
6 import dmd.TypeArray; | |
123 | 7 import dmd.TemplateTupleParameter; |
0 | 8 import dmd.ArrayTypes; |
9 import dmd.LINK; | |
10 import dmd.StructDeclaration; | |
11 import dmd.TypeStruct; | |
12 import dmd.Global; | |
13 import dmd.STC; | |
14 import dmd.MOD; | |
73 | 15 import dmd.PROT; |
123 | 16 import dmd.TypeIdentifier; |
17 import dmd.TemplateParameter; | |
129 | 18 import dmd.TypeInfoFunctionDeclaration; |
123 | 19 import dmd.Tuple; |
0 | 20 import dmd.Type; |
21 import dmd.Loc; | |
22 import dmd.Scope; | |
23 import dmd.Identifier; | |
24 import dmd.OutBuffer; | |
25 import dmd.HdrGenState; | |
26 import dmd.CppMangleState; | |
27 import dmd.TypeInfoDeclaration; | |
28 import dmd.MATCH; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
29 import dmd.Parameter; |
0 | 30 import dmd.Expression; |
31 import dmd.RET; | |
32 import dmd.TY; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
33 import dmd.TRUST; |
0 | 34 import dmd.Util; |
123 | 35 import dmd.TemplateInstance : isTuple; |
0 | 36 |
37 import dmd.backend.TYPE; | |
38 import dmd.backend.PARAM; | |
39 import dmd.backend.Util; | |
40 import dmd.backend.TYM; | |
41 import dmd.backend.TF; | |
42 import dmd.backend.mTY; | |
43 | |
44 import core.stdc.stdlib; | |
45 import core.stdc.string; | |
46 | |
129 | 47 import std.stdio; |
48 | |
0 | 49 class TypeFunction : TypeNext |
50 { | |
51 // .next is the return type | |
52 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
53 Parameters parameters; // function parameters |
0 | 54 int varargs; // 1: T t, ...) style for variable number of arguments |
55 // 2: T t ...) style for variable number of arguments | |
56 bool isnothrow; // true: nothrow | |
57 bool ispure; // true: pure | |
58 bool isproperty; // can be called without parentheses | |
59 bool isref; // true: returns a reference | |
60 LINK linkage; // calling convention | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
61 TRUST trust; // level of trust |
0 | 62 |
63 int inuse; | |
64 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
65 this(Parameters parameters, Type treturn, int varargs, LINK linkage) |
0 | 66 { |
67 super(TY.Tfunction, treturn); | |
68 | |
69 //if (!treturn) *(char*)0=0; | |
70 // assert(treturn); | |
71 assert(0 <= varargs && varargs <= 2); | |
72 this.parameters = parameters; | |
73 this.varargs = varargs; | |
74 this.linkage = linkage; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
75 this.trust = TRUSTdefault; |
0 | 76 } |
77 | |
72 | 78 override Type syntaxCopy() |
0 | 79 { |
80 Type treturn = next ? next.syntaxCopy() : null; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
81 auto params = Parameter.arraySyntaxCopy(parameters); |
0 | 82 TypeFunction t = new TypeFunction(params, treturn, varargs, linkage); |
83 t.mod = mod; | |
84 t.isnothrow = isnothrow; | |
85 t.ispure = ispure; | |
86 t.isproperty = isproperty; | |
87 t.isref = isref; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
88 t.trust = trust; |
0 | 89 |
90 return t; | |
91 } | |
92 | |
93 version (DumbClone) { | |
94 } else { | |
95 final TypeFunction cloneTo(TypeFunction t) | |
96 { | |
97 super.cloneTo(t); | |
98 | |
99 // these 3 should be set by ctor | |
100 assert(t.parameters is null); | |
101 assert(t.varargs == varargs); | |
102 assert(t.linkage == linkage); | |
103 | |
104 t.isnothrow = isnothrow; | |
105 t.ispure = ispure; | |
106 t.isproperty = isproperty; | |
107 t.isref = isref; | |
108 t.inuse = inuse; | |
109 | |
110 if (parameters) | |
111 { | |
112 t.parameters = parameters.copy(); | |
129 | 113 foreach (arg; parameters) |
0 | 114 { |
115 Argument cpy = arg.clone(); | |
129 | 116 t.parameters[i] = cpy; |
0 | 117 } |
118 } | |
119 | |
120 return t; | |
121 } | |
122 | |
123 TypeFunction clone() | |
124 { | |
129 | 125 assert(this.classinfo is TypeFunction.classinfo); |
0 | 126 return cloneTo(new TypeFunction(null, next, varargs, linkage)); |
127 } | |
128 } | |
72 | 129 override Type semantic(Loc loc, Scope sc) |
0 | 130 { |
131 if (deco) // if semantic() already run | |
132 { | |
133 //printf("already done\n"); | |
134 return this; | |
135 } | |
136 //printf("TypeFunction.semantic() this = %p\n", this); | |
137 //printf("TypeFunction.semantic() %s, sc.stc = %x\n", toChars(), sc.stc); | |
138 | |
139 /* Copy in order to not mess up original. | |
140 * This can produce redundant copies if inferring return type, | |
141 * as semantic() will get called again on this. | |
142 */ | |
143 | |
129 | 144 TypeFunction tf = cloneThis(this); |
0 | 145 |
146 if (sc.stc & STC.STCpure) | |
147 tf.ispure = true; | |
148 if (sc.stc & STC.STCnothrow) | |
149 tf.isnothrow = true; | |
150 if (sc.stc & STC.STCref) | |
151 tf.isref = true; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
152 if (sc.stc & STCsafe) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
153 tf.trust = TRUST.TRUSTsafe; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
154 if (sc.stc & STCtrusted) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
155 tf.trust = TRUST.TRUSTtrusted; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
156 if (sc.stc & STCproperty) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
157 tf.isproperty = true; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
158 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
159 tf.linkage = sc.linkage; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
160 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
161 /* If the parent is @safe, then this function defaults to safe |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
162 * too. |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
163 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
164 if (tf.trust == TRUST.TRUSTdefault) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
165 for (Dsymbol p = sc.func; p; p = p.toParent2()) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
166 { FuncDeclaration fd = p.isFuncDeclaration(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
167 if (fd) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
168 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
169 if (fd.isSafe()) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
170 tf.trust = TRUST.TRUSTsafe; // default to @safe |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
171 break; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
172 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
173 } |
0 | 174 |
175 if (tf.next) | |
176 { | |
177 tf.next = tf.next.semantic(loc,sc); | |
96 | 178 version(SARRAYVALUE) {} else |
179 { | |
0 | 180 if (tf.next.toBasetype().ty == TY.Tsarray) |
129 | 181 { |
182 error(loc, "functions cannot return static array %s", tf.next.toChars()); | |
0 | 183 tf.next = Type.terror; |
184 } | |
96 | 185 } |
0 | 186 if (tf.next.toBasetype().ty == TY.Tfunction) |
129 | 187 { |
188 error(loc, "functions cannot return a function"); | |
0 | 189 tf.next = Type.terror; |
190 } | |
191 if (tf.next.toBasetype().ty == TY.Ttuple) | |
129 | 192 { |
193 error(loc, "functions cannot return a tuple"); | |
0 | 194 tf.next = Type.terror; |
195 } | |
196 if (tf.next.isauto() && !(sc.flags & SCOPE.SCOPEctor)) | |
129 | 197 error(loc, "functions cannot return scope %s", tf.next.toChars()); |
0 | 198 } |
199 | |
200 if (tf.parameters) | |
73 | 201 { |
202 /* Create a scope for evaluating the default arguments for the parameters | |
203 */ | |
204 Scope argsc = sc.push(); | |
205 argsc.stc = STCundefined; // don't inherit storage class | |
206 argsc.protection = PROT.PROTpublic; | |
207 | |
208 size_t dim = Argument.dim(tf.parameters); | |
0 | 209 |
210 for (size_t i = 0; i < dim; i++) | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
211 { auto arg = Parameter.getNth(tf.parameters, i); |
0 | 212 |
213 tf.inuse++; | |
73 | 214 arg.type = arg.type.semantic(loc, argsc); |
0 | 215 if (tf.inuse == 1) tf.inuse--; |
216 | |
217 arg.type = arg.type.addStorageClass(arg.storageClass); | |
218 | |
219 if (arg.storageClass & (STC.STCauto | STC.STCalias | STC.STCstatic)) | |
220 { | |
221 if (!arg.type) | |
222 continue; | |
223 } | |
224 | |
225 Type t = arg.type.toBasetype(); | |
226 | |
227 if (arg.storageClass & (STC.STCout | STC.STCref | STC.STClazy)) | |
228 { | |
96 | 229 //if (t.ty == TY.Tsarray) |
230 //error(loc, "cannot have out or ref parameter of type %s", t.toChars()); | |
231 if (arg.storageClass & STC.STCout && arg.type.mod & (STCconst | STCimmutable)) | |
232 error(loc, "cannot have const or immutabl out parameter of type %s", t.toChars()); | |
0 | 233 } |
234 if (!(arg.storageClass & STC.STClazy) && t.ty == TY.Tvoid) | |
235 error(loc, "cannot have parameter of type %s", arg.type.toChars()); | |
236 | |
237 if (arg.defaultArg) | |
238 { | |
73 | 239 arg.defaultArg = arg.defaultArg.semantic(argsc); |
240 arg.defaultArg = resolveProperties(argsc, arg.defaultArg); | |
241 arg.defaultArg = arg.defaultArg.implicitCastTo(argsc, arg.type); | |
0 | 242 } |
243 | |
244 /* If arg turns out to be a tuple, the number of parameters may | |
245 * change. | |
246 */ | |
247 if (t.ty == TY.Ttuple) | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
248 { dim = Parameter.dim(tf.parameters); |
0 | 249 i--; |
250 } | |
251 } | |
73 | 252 argsc.pop(); |
0 | 253 } |
254 if (tf.next) | |
255 tf.deco = tf.merge().deco; | |
256 | |
257 if (tf.inuse) | |
258 { error(loc, "recursive type"); | |
259 tf.inuse = 0; | |
260 return terror; | |
261 } | |
262 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
263 if (tf.isproperty && (tf.varargs || Parameter.dim(tf.parameters) > 1)) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
264 error(loc, "properties can only have zero or one parameter"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
265 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
266 if (tf.varargs == 1 && tf.linkage != LINK.LINKd && Parameter.dim(tf.parameters) == 0) |
0 | 267 error(loc, "variadic functions with non-D linkage must have at least one parameter"); |
268 | |
269 /* Don't return merge(), because arg identifiers and default args | |
270 * can be different | |
271 * even though the types match | |
272 */ | |
273 return tf; | |
274 } | |
275 | |
72 | 276 override void toDecoBuffer(OutBuffer buf, int flag) |
0 | 277 { |
278 ubyte mc; | |
279 | |
280 //printf("TypeFunction.toDecoBuffer() this = %p %s\n", this, toChars()); | |
281 //static int nest; if (++nest == 50) *(char*)0=0; | |
282 if (inuse) | |
283 { | |
284 inuse = 2; // flag error to caller | |
285 return; | |
286 } | |
287 inuse++; | |
288 static if (true) { | |
289 if (mod & MOD.MODshared) | |
290 buf.writeByte('O'); | |
291 if (mod & MOD.MODconst) | |
292 buf.writeByte('x'); | |
293 else if (mod & MOD.MODinvariant) | |
294 buf.writeByte('y'); | |
295 } | |
296 switch (linkage) | |
297 { | |
298 case LINK.LINKd: mc = 'F'; break; | |
299 case LINK.LINKc: mc = 'U'; break; | |
300 case LINK.LINKwindows: mc = 'W'; break; | |
301 case LINK.LINKpascal: mc = 'V'; break; | |
302 case LINK.LINKcpp: mc = 'R'; break; | |
303 } | |
304 buf.writeByte(mc); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
305 if (ispure || isnothrow || isproperty || isref || trust) |
0 | 306 { |
307 if (ispure) | |
308 buf.writestring("Na"); | |
309 if (isnothrow) | |
310 buf.writestring("Nb"); | |
311 if (isref) | |
312 buf.writestring("Nc"); | |
313 if (isproperty) | |
314 buf.writestring("Nd"); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
315 switch (trust) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
316 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
317 case TRUST.TRUSTtrusted: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
318 buf.writestring("Ne"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
319 break; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
320 case TRUST.TRUSTsafe: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
321 buf.writestring("Nd"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
322 break; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
323 } |
0 | 324 } |
325 // Write argument types | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
326 Parameter.argsToDecoBuffer(buf, parameters); |
0 | 327 //if (buf.data[buf.offset - 1] == '@') halt(); |
328 buf.writeByte('Z' - varargs); // mark end of arg list | |
79 | 329 assert(next); |
0 | 330 next.toDecoBuffer(buf); |
331 inuse--; | |
332 } | |
333 | |
72 | 334 override void toCBuffer(OutBuffer buf, Identifier ident, HdrGenState* hgs) |
0 | 335 { |
336 //printf("TypeFunction.toCBuffer() this = %p\n", this); | |
337 string p = null; | |
338 | |
339 if (inuse) | |
340 { | |
341 inuse = 2; // flag error to caller | |
342 return; | |
343 } | |
344 inuse++; | |
345 | |
346 /* Use 'storage class' style for attributes | |
347 */ | |
348 if (mod & MODconst) | |
349 buf.writestring("const "); | |
350 if (mod & MODinvariant) | |
351 buf.writestring("immutable "); | |
352 if (mod & MODshared) | |
353 buf.writestring("shared "); | |
354 | |
355 if (ispure) | |
356 buf.writestring("pure "); | |
357 if (isnothrow) | |
358 buf.writestring("nothrow "); | |
359 if (isproperty) | |
360 buf.writestring("@property "); | |
361 if (isref) | |
362 buf.writestring("ref "); | |
363 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
364 switch (trust) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
365 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
366 case TRUST.TRUSTtrusted: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
367 buf.writestring("@trusted "); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
368 break; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
369 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
370 case TRUST.TRUSTsafe: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
371 buf.writestring("@safe "); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
372 break; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
373 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
374 |
0 | 375 if (next && (!ident || ident.toHChars2() == ident.toChars())) |
376 next.toCBuffer2(buf, hgs, MODundefined); | |
377 if (hgs.ddoc != 1) | |
378 { | |
379 switch (linkage) | |
380 { | |
381 case LINKd: p = null; break; | |
73 | 382 case LINKc: p = " C"; break; |
383 case LINKwindows: p = " Windows"; break; | |
384 case LINKpascal: p = " Pascal"; break; | |
385 case LINKcpp: p = " C++"; break; | |
0 | 386 default: |
387 assert(0); | |
388 } | |
389 } | |
390 | |
391 if (!hgs.hdrgen && p) | |
392 buf.writestring(p); | |
393 if (ident) | |
394 { | |
395 buf.writeByte(' '); | |
396 buf.writestring(ident.toHChars2()); | |
397 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
398 Parameter.argsToCBuffer(buf, hgs, parameters, varargs); |
0 | 399 inuse--; |
400 } | |
401 | |
72 | 402 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) |
0 | 403 { |
404 //printf("TypeFunction::toCBuffer2() this = %p, ref = %d\n", this, isref); | |
405 string p; | |
406 | |
407 if (inuse) | |
408 { | |
409 inuse = 2; // flag error to caller | |
410 return; | |
411 } | |
412 | |
413 inuse++; | |
414 if (next) | |
415 next.toCBuffer2(buf, hgs, MODundefined); | |
416 | |
417 if (hgs.ddoc != 1) | |
418 { | |
419 switch (linkage) | |
420 { | |
421 case LINKd: p = null; break; | |
422 case LINKc: p = "C "; break; | |
423 case LINKwindows: p = "Windows "; break; | |
424 case LINKpascal: p = "Pascal "; break; | |
425 case LINKcpp: p = "C++ "; break; | |
426 default: assert(0); | |
427 } | |
428 } | |
429 | |
430 if (!hgs.hdrgen && p) | |
431 buf.writestring(p); | |
432 buf.writestring(" function"); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
433 Parameter.argsToCBuffer(buf, hgs, parameters, varargs); |
0 | 434 |
435 /* Use postfix style for attributes | |
436 */ | |
437 if (mod != this.mod) | |
438 { | |
439 modToBuffer(buf); | |
440 } | |
441 | |
442 if (ispure) | |
443 buf.writestring(" pure"); | |
444 if (isnothrow) | |
445 buf.writestring(" nothrow"); | |
446 if (isproperty) | |
447 buf.writestring(" @property"); | |
448 if (isref) | |
449 buf.writestring(" ref"); | |
450 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
451 switch (trust) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
452 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
453 case TRUSTtrusted: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
454 buf.writestring(" @trusted"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
455 break; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
456 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
457 case TRUSTsafe: |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
458 buf.writestring(" @safe"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
459 break; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
460 } |
0 | 461 inuse--; |
462 } | |
463 | |
72 | 464 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) |
0 | 465 { |
123 | 466 //printf("TypeFunction.deduceType()\n"); |
467 //printf("\tthis = %d, ", ty); print(); | |
468 //printf("\ttparam = %d, ", tparam.ty); tparam.print(); | |
469 | |
470 // Extra check that function characteristics must match | |
471 if (tparam && tparam.ty == Tfunction) | |
472 { | |
473 TypeFunction tp = cast(TypeFunction)tparam; | |
474 if (varargs != tp.varargs || | |
475 linkage != tp.linkage) | |
476 return MATCHnomatch; | |
477 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
478 size_t nfargs = Parameter.dim(this.parameters); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
479 size_t nfparams = Parameter.dim(tp.parameters); |
123 | 480 |
481 /* See if tuple match | |
482 */ | |
483 if (nfparams > 0 && nfargs >= nfparams - 1) | |
484 { | |
485 /* See if 'A' of the template parameter matches 'A' | |
486 * of the type of the last function parameter. | |
487 */ | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
488 auto fparam = Parameter.getNth(tp.parameters, nfparams - 1); |
123 | 489 assert(fparam); |
490 assert(fparam.type); | |
491 if (fparam.type.ty != Tident) | |
492 goto L1; | |
493 TypeIdentifier tid = cast(TypeIdentifier)fparam.type; | |
494 if (tid.idents.dim) | |
495 goto L1; | |
496 | |
497 /* Look through parameters to find tuple matching tid.ident | |
498 */ | |
499 size_t tupi = 0; | |
500 for (; 1; tupi++) | |
501 { | |
502 if (tupi == parameters.dim) | |
503 goto L1; | |
504 TemplateParameter t = parameters[tupi]; | |
505 TemplateTupleParameter tup = t.isTemplateTupleParameter(); | |
506 if (tup && tup.ident.equals(tid.ident)) | |
507 break; | |
508 } | |
509 | |
510 /* The types of the function arguments [nfparams - 1 .. nfargs] | |
511 * now form the tuple argument. | |
512 */ | |
513 int tuple_dim = nfargs - (nfparams - 1); | |
514 | |
515 /* See if existing tuple, and whether it matches or not | |
516 */ | |
517 Object o = dedtypes[tupi]; | |
518 if (o) | |
519 { | |
520 // Existing deduced argument must be a tuple, and must match | |
521 Tuple t = isTuple(o); | |
522 if (!t || t.objects.dim != tuple_dim) | |
523 return MATCHnomatch; | |
524 for (size_t i = 0; i < tuple_dim; i++) | |
525 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
526 auto arg = Parameter.getNth(this.parameters, nfparams - 1 + i); |
123 | 527 if (!arg.type.equals(t.objects[i])) |
528 return MATCHnomatch; | |
529 } | |
530 } | |
531 else | |
532 { // Create new tuple | |
533 Tuple t = new Tuple(); | |
534 t.objects.setDim(tuple_dim); | |
535 for (size_t i = 0; i < tuple_dim; i++) | |
536 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
537 auto arg = Parameter.getNth(this.parameters, nfparams - 1 + i); |
123 | 538 t.objects[i] = arg.type; |
539 } | |
540 dedtypes[tupi] = t; | |
541 } | |
542 nfparams--; // don't consider the last parameter for type deduction | |
543 goto L2; | |
544 } | |
545 | |
546 L1: | |
547 if (nfargs != nfparams) | |
548 return MATCHnomatch; | |
549 L2: | |
550 for (size_t i = 0; i < nfparams; i++) | |
551 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
552 auto a = Parameter.getNth(this.parameters, i); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
553 auto ap = Parameter.getNth(tp.parameters, i); |
123 | 554 if (a.storageClass != ap.storageClass || |
555 !a.type.deduceType(sc, ap.type, parameters, dedtypes)) | |
556 return MATCHnomatch; | |
557 } | |
558 } | |
559 return Type.deduceType(sc, tparam, parameters, dedtypes); | |
0 | 560 } |
561 | |
72 | 562 override TypeInfoDeclaration getTypeInfoDeclaration() |
0 | 563 { |
129 | 564 return new TypeInfoFunctionDeclaration(this); |
0 | 565 } |
566 | |
72 | 567 override Type reliesOnTident() |
0 | 568 { |
569 if (parameters) | |
570 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
123
diff
changeset
|
571 foreach (arg; parameters) |
0 | 572 { |
573 Type t = arg.type.reliesOnTident(); | |
574 if (t) | |
575 return t; | |
576 } | |
577 } | |
578 return next.reliesOnTident(); | |
579 } | |
580 | |
581 version (CPP_MANGLE) { | |
582 void toCppMangle(OutBuffer buf, CppMangleState* cms) | |
583 { | |
584 assert(false); | |
585 } | |
586 } | |
587 | |
588 /*************************** | |
589 * Examine function signature for parameter p and see if | |
590 * p can 'escape' the scope of the function. | |
591 */ | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
592 bool parameterEscapes(Parameter p) |
0 | 593 { |
594 /* Scope parameters do not escape. | |
595 * Allow 'lazy' to imply 'scope' - | |
596 * lazy parameters can be passed along | |
597 * as lazy parameters to the next function, but that isn't | |
598 * escaping. | |
599 */ | |
600 if (p.storageClass & (STC.STCscope | STC.STClazy)) | |
601 return false; | |
602 | |
603 if (ispure) | |
604 { /* With pure functions, we need only be concerned if p escapes | |
605 * via any return statement. | |
606 */ | |
607 Type tret = nextOf().toBasetype(); | |
608 if (!isref && !tret.hasPointers()) | |
609 { /* The result has no references, so p could not be escaping | |
610 * that way. | |
611 */ | |
612 return false; | |
613 } | |
614 } | |
615 | |
616 /* Assume it escapes in the absence of better information. | |
617 */ | |
618 return true; | |
619 } | |
620 | |
621 /******************************** | |
622 * 'args' are being matched to function 'this' | |
623 * Determine match level. | |
624 * Returns: | |
625 * MATCHxxxx | |
626 */ | |
627 MATCH callMatch(Expression ethis, Expressions args) | |
628 { | |
629 //printf("TypeFunction.callMatch() %s\n", toChars()); | |
630 MATCH match = MATCH.MATCHexact; // assume exact match | |
631 | |
632 if (ethis) | |
633 { | |
634 Type t = ethis.type; | |
635 if (t.toBasetype().ty == TY.Tpointer) | |
636 t = t.toBasetype().nextOf(); // change struct* to struct | |
637 | |
638 if (t.mod != mod) | |
639 { | |
640 if (mod == MOD.MODconst) | |
641 match = MATCH.MATCHconst; | |
642 else | |
643 return MATCH.MATCHnomatch; | |
644 } | |
645 } | |
646 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
647 size_t nparams = Parameter.dim(parameters); |
0 | 648 size_t nargs = args ? args.dim : 0; |
649 if (nparams == nargs) { | |
650 ; | |
651 } else if (nargs > nparams) | |
652 { | |
653 if (varargs == 0) | |
654 goto Nomatch; // too many args; no match | |
655 match = MATCH.MATCHconvert; // match ... with a "conversion" match level | |
656 } | |
657 | |
658 for (size_t u = 0; u < nparams; u++) | |
659 { | |
660 MATCH m; | |
661 Expression arg; | |
662 | |
663 // BUG: what about out and ref? | |
664 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
665 auto p = Parameter.getNth(parameters, u); |
0 | 666 assert(p); |
667 if (u >= nargs) | |
668 { | |
669 if (p.defaultArg) | |
670 continue; | |
671 if (varargs == 2 && u + 1 == nparams) | |
672 goto L1; | |
673 goto Nomatch; // not enough arguments | |
674 } | |
675 | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
96
diff
changeset
|
676 arg = cast(Expression)args[u]; |
0 | 677 assert(arg); |
96 | 678 // writef("arg: %s, type: %s\n", arg.toChars(), arg.type.toChars()); |
679 | |
0 | 680 |
681 // Non-lvalues do not match ref or out parameters | |
96 | 682 if (p.storageClass & (STC.STCref | STC.STCout)) |
683 { | |
684 if (!arg.isLvalue()) | |
685 goto Nomatch; | |
686 } | |
687 | |
688 if (p.storageClass & STCref) | |
689 { | |
690 /* Don't allow static arrays to be passed to mutable refereces | |
691 * to static arrays if the argument cannot be modified. | |
692 */ | |
693 Type targb = arg.type.toBasetype(); | |
694 Type tparb = p.type.toBasetype(); | |
695 //writef("%s\n", targb.toChars()); | |
696 //writef("%s\n", tparb.toChars()); | |
697 if (targb.nextOf() && tparb.ty == Tsarray && | |
698 targb.nextOf().mod != tparb.nextOf().mod && | |
699 !tparb.nextOf().isConst()) | |
700 goto Nomatch; | |
701 } | |
0 | 702 |
703 if (p.storageClass & STC.STClazy && p.type.ty == TY.Tvoid && arg.type.ty != TY.Tvoid) | |
704 m = MATCH.MATCHconvert; | |
705 else | |
706 m = arg.implicitConvTo(p.type); | |
707 //printf("\tm = %d\n", m); | |
708 if (m == MATCH.MATCHnomatch) // if no match | |
709 { | |
710 L1: | |
711 if (varargs == 2 && u + 1 == nparams) // if last varargs param | |
712 { | |
713 Type tb = p.type.toBasetype(); | |
714 TypeSArray tsa; | |
715 long sz; | |
716 | |
717 switch (tb.ty) | |
718 { | |
719 case TY.Tsarray: | |
720 tsa = cast(TypeSArray)tb; | |
721 sz = tsa.dim.toInteger(); | |
722 if (sz != nargs - u) | |
723 goto Nomatch; | |
724 case TY.Tarray: | |
725 { | |
726 TypeArray ta = cast(TypeArray)tb; | |
727 for (; u < nargs; u++) | |
728 { | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
96
diff
changeset
|
729 arg = cast(Expression)args[u]; |
0 | 730 assert(arg); |
731 static if (true) { | |
732 /* If lazy array of delegates, | |
733 * convert arg(s) to delegate(s) | |
734 */ | |
735 Type tret = p.isLazyArray(); | |
736 if (tret) | |
737 { | |
738 if (ta.next.equals(arg.type)) | |
739 { | |
740 m = MATCH.MATCHexact; | |
741 } | |
742 else | |
743 { | |
744 m = arg.implicitConvTo(tret); | |
745 if (m == MATCH.MATCHnomatch) | |
746 { | |
747 if (tret.toBasetype().ty == TY.Tvoid) | |
748 m = MATCH.MATCHconvert; | |
749 } | |
750 } | |
751 } | |
752 else | |
753 m = arg.implicitConvTo(ta.next); | |
754 } else { | |
755 m = arg.implicitConvTo(ta.next); | |
756 } | |
757 if (m == MATCH.MATCHnomatch) | |
758 goto Nomatch; | |
759 | |
760 if (m < match) | |
761 match = m; | |
762 } | |
763 goto Ldone; | |
764 } | |
765 | |
766 case TY.Tclass: | |
767 // Should see if there's a constructor match? | |
768 // Or just leave it ambiguous? | |
769 goto Ldone; | |
770 | |
771 default: | |
772 goto Nomatch; | |
773 } | |
774 } | |
775 | |
776 goto Nomatch; | |
777 } | |
778 | |
779 if (m < match) | |
780 match = m; // pick worst match | |
781 } | |
782 | |
783 Ldone: | |
784 //printf("match = %d\n", match); | |
785 return match; | |
786 | |
787 Nomatch: | |
788 //printf("no match\n"); | |
789 return MATCH.MATCHnomatch; | |
790 } | |
791 | |
96 | 792 override type* toCtype() |
0 | 793 { |
794 if (ctype) { | |
795 return ctype; | |
796 } | |
797 | |
798 type* t; | |
799 if (true) | |
800 { | |
801 param_t* paramtypes; | |
802 tym_t tyf; | |
803 type* tp; | |
804 | |
805 paramtypes = null; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
806 size_t nparams = Parameter.dim(parameters); |
0 | 807 for (size_t i = 0; i < nparams; i++) |
808 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
129
diff
changeset
|
809 auto arg = Parameter.getNth(parameters, i); |
0 | 810 tp = arg.type.toCtype(); |
811 if (arg.storageClass & (STC.STCout | STC.STCref)) | |
812 { | |
813 // C doesn't have reference types, so it's really a pointer | |
814 // to the parameter type | |
815 tp = type_allocn(TYM.TYref, tp); | |
816 } | |
817 param_append_type(¶mtypes,tp); | |
818 } | |
819 tyf = totym(); | |
820 t = type_alloc(tyf); | |
821 t.Tflags |= TF.TFprototype; | |
822 if (varargs != 1) | |
823 t.Tflags |= TF.TFfixed; | |
824 ctype = t; | |
825 t.Tnext = next.toCtype(); | |
826 t.Tnext.Tcount++; | |
827 t.Tparamtypes = paramtypes; | |
828 } | |
829 ctype = t; | |
830 return t; | |
831 } | |
832 | |
833 /*************************** | |
834 * Determine return style of function - whether in registers or | |
835 * through a hidden pointer to the caller's stack. | |
836 */ | |
96 | 837 RET retStyle() |
0 | 838 { |
839 //printf("TypeFunction.retStyle() %s\n", toChars()); | |
95 | 840 version (DMDV2) |
841 { | |
0 | 842 if (isref) |
843 return RET.RETregs; // returns a pointer | |
844 } | |
845 | |
846 Type tn = next.toBasetype(); | |
95 | 847 Type tns = tn; |
848 ulong sz = tn.size(); | |
0 | 849 |
95 | 850 version(SARRAYVALUE) |
851 { | |
852 if (tn.ty == Tsarray) | |
853 { | |
854 do | |
855 { | |
856 tns = tns.nextOf().toBasetype(); | |
857 } while (tns.ty == Tsarray); | |
858 if (tns.ty != Tstruct) | |
859 { | |
860 if (global.params.isLinux && linkage != LINKd) | |
861 {} | |
862 else | |
863 { | |
864 switch (sz) | |
865 { case 1: | |
866 case 2: | |
867 case 4: | |
868 case 8: | |
96 | 869 return RET.RETregs; // return small structs in regs |
870 // (not 3 byte structs!) | |
95 | 871 default: |
872 break; | |
873 } | |
874 } | |
96 | 875 return RET.RETstack; |
95 | 876 } |
877 } | |
878 } | |
879 if (tns.ty == TY.Tstruct) | |
0 | 880 { |
881 StructDeclaration sd = (cast(TypeStruct)tn).sym; | |
882 if (global.params.isLinux && linkage != LINK.LINKd) { | |
883 ; | |
884 } | |
95 | 885 ///version (DMDV2) { // TODO: |
886 else if (sd.dtor || sd.cpctor) | |
887 { | |
0 | 888 } |
889 ///} | |
890 else | |
891 { | |
95 | 892 switch (sz) |
0 | 893 { |
894 case 1: | |
895 case 2: | |
896 case 4: | |
897 case 8: | |
898 return RET.RETregs; // return small structs in regs | |
899 // (not 3 byte structs!) | |
900 default: | |
901 break; | |
902 } | |
903 } | |
904 return RET.RETstack; | |
905 } | |
906 else if ((global.params.isLinux || global.params.isOSX || global.params.isFreeBSD || global.params.isSolaris) && | |
907 linkage == LINK.LINKc && | |
908 tn.iscomplex()) | |
909 { | |
910 if (tn.ty == TY.Tcomplex32) | |
911 return RET.RETregs; // in EDX:EAX, not ST1:ST0 | |
912 else | |
913 return RET.RETstack; | |
914 } | |
915 else | |
916 return RET.RETregs; | |
917 } | |
918 | |
72 | 919 override TYM totym() |
0 | 920 { |
921 TYM tyf; | |
922 | |
923 //printf("TypeFunction.totym(), linkage = %d\n", linkage); | |
924 switch (linkage) | |
925 { | |
926 case LINK.LINKwindows: | |
927 tyf = (varargs == 1) ? TYM.TYnfunc : TYM.TYnsfunc; | |
928 break; | |
929 | |
930 case LINK.LINKpascal: | |
931 tyf = (varargs == 1) ? TYM.TYnfunc : TYM.TYnpfunc; | |
932 break; | |
933 | |
934 case LINK.LINKc: | |
935 tyf = TYM.TYnfunc; | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
936 version (POSIX) {///TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS |
0 | 937 if (retStyle() == RET.RETstack) |
938 tyf = TYM.TYhfunc; | |
939 } | |
940 break; | |
941 | |
942 case LINK.LINKd: | |
943 tyf = (varargs == 1) ? TYM.TYnfunc : TYM.TYjfunc; | |
944 break; | |
945 | |
946 case LINK.LINKcpp: | |
947 tyf = TYM.TYnfunc; | |
948 break; | |
949 | |
950 default: | |
951 writef("linkage = %d\n", linkage); | |
952 assert(0); | |
953 } | |
954 version (DMDV2) { | |
955 if (isnothrow) | |
956 tyf |= mTY.mTYnothrow; | |
957 } | |
958 return tyf; | |
959 } | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
960 } |