Mercurial > projects > ddmd
annotate dmd/Parameter.d @ 135:af1bebfd96a4 dmd2037
dmd 2.038
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Mon, 13 Sep 2010 22:19:42 +0100 |
parents | 206db751bd4c |
children | e3afd1303184 |
rev | line source |
---|---|
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
1 module dmd.Parameter; |
0 | 2 |
114 | 3 import dmd.common; |
0 | 4 import dmd.Type; |
5 import dmd.Identifier; | |
56 | 6 import dmd.TypeArray; |
7 import dmd.TypeFunction; | |
8 import dmd.TypeDelegate; | |
0 | 9 import dmd.TypeTuple; |
10 import dmd.TY; | |
11 import dmd.Expression; | |
12 import dmd.OutBuffer; | |
13 import dmd.HdrGenState; | |
14 import dmd.ArrayTypes; | |
15 import dmd.StorageClassDeclaration; | |
16 import dmd.Global; | |
17 import dmd.MOD; | |
18 import dmd.CppMangleState; | |
19 import dmd.STC; | |
20 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
21 class Parameter |
0 | 22 { |
23 //enum InOut inout; | |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
24 StorageClass storageClass; |
0 | 25 Type type; |
26 Identifier ident; | |
27 Expression defaultArg; | |
28 | |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
29 this(StorageClass storageClass, Type type, Identifier ident, Expression defaultArg) |
0 | 30 { |
31 this.type = type; | |
32 this.ident = ident; | |
33 this.storageClass = storageClass; | |
34 this.defaultArg = defaultArg; | |
35 } | |
36 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
37 Parameter clone() |
0 | 38 { |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
39 return new Parameter(storageClass, type, ident, defaultArg); |
0 | 40 } |
41 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
42 Parameter syntaxCopy() |
0 | 43 { |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
44 return new Parameter(storageClass, type ? type.syntaxCopy() : null, ident, defaultArg ? defaultArg.syntaxCopy() : null); |
0 | 45 } |
46 | |
56 | 47 /**************************************************** |
48 * Determine if parameter is a lazy array of delegates. | |
49 * If so, return the return type of those delegates. | |
50 * If not, return null. | |
51 */ | |
0 | 52 Type isLazyArray() |
53 { | |
56 | 54 // if (inout == Lazy) |
55 { | |
56 Type tb = type.toBasetype(); | |
57 if (tb.ty == Tsarray || tb.ty == Tarray) | |
58 { | |
59 Type tel = (cast(TypeArray)tb).next.toBasetype(); | |
60 if (tel.ty == Tdelegate) | |
61 { | |
62 TypeDelegate td = cast(TypeDelegate)tel; | |
63 TypeFunction tf = cast(TypeFunction)td.next; | |
64 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
65 if (!tf.varargs && Parameter.dim(tf.parameters) == 0) |
56 | 66 { |
67 return tf.next; // return type of delegate | |
68 } | |
69 } | |
70 } | |
71 } | |
72 return null; | |
0 | 73 } |
74 | |
75 void toDecoBuffer(OutBuffer buf) | |
76 { | |
77 if (storageClass & STC.STCscope) | |
78 buf.writeByte('M'); | |
79 switch (storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STClazy)) | |
80 { | |
81 case STC.STCundefined: | |
82 case STC.STCin: | |
83 break; | |
84 case STC.STCout: | |
85 buf.writeByte('J'); | |
86 break; | |
87 case STC.STCref: | |
88 buf.writeByte('K'); | |
89 break; | |
90 case STC.STClazy: | |
91 buf.writeByte('L'); | |
92 break; | |
93 } | |
94 static if (false) { | |
95 int mod = 0x100; | |
96 if (type.toBasetype().ty == TY.Tclass) | |
97 mod = 0; | |
98 type.toDecoBuffer(buf, mod); | |
99 } else { | |
100 //type.toHeadMutable().toDecoBuffer(buf, 0); | |
101 type.toDecoBuffer(buf, 0); | |
102 } | |
103 } | |
104 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
105 static Parameters arraySyntaxCopy(Parameters args) |
0 | 106 { |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
107 typeof(return) a = null; |
0 | 108 |
109 if (args) | |
110 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
111 a = new Parameters(); |
0 | 112 a.setDim(args.dim); |
113 | |
114 for (size_t i = 0; i < a.dim; i++) | |
115 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
116 auto arg = args[i]; |
0 | 117 |
118 arg = arg.syntaxCopy(); | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
119 a[i] = arg; |
0 | 120 } |
121 } | |
122 | |
123 return a; | |
124 } | |
125 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
126 static string argsTypesToChars(Parameters args, int varargs) |
0 | 127 { |
128 scope OutBuffer buf = new OutBuffer(); | |
129 | |
130 static if (true) { | |
131 HdrGenState hgs; | |
132 argsToCBuffer(buf, &hgs, args, varargs); | |
133 } else { | |
134 buf.writeByte('('); | |
135 if (args) | |
136 { | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
137 OutBuffer argbuf = new OutBuffer(); |
0 | 138 HdrGenState hgs; |
139 | |
140 for (int i = 0; i < args.dim; i++) | |
141 { | |
142 if (i) | |
143 buf.writeByte(','); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
144 auto arg = cast(Parameter)args.data[i]; |
0 | 145 argbuf.reset(); |
146 arg.type.toCBuffer2(&argbuf, &hgs, 0); | |
147 buf.write(&argbuf); | |
148 } | |
149 if (varargs) | |
150 { | |
151 if (i && varargs == 1) | |
152 buf.writeByte(','); | |
153 buf.writestring("..."); | |
154 } | |
155 } | |
156 buf.writeByte(')'); | |
157 } | |
158 return buf.toChars(); | |
159 } | |
160 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
161 static void argsCppMangle(OutBuffer buf, CppMangleState* cms, Parameters arguments, int varargs) |
0 | 162 { |
163 assert(false); | |
164 } | |
165 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
166 static void argsToCBuffer(OutBuffer buf, HdrGenState* hgs, Parameters arguments, int varargs) |
0 | 167 { |
168 buf.writeByte('('); | |
169 if (arguments) | |
170 { | |
171 int i; | |
172 scope OutBuffer argbuf = new OutBuffer(); | |
173 | |
174 for (i = 0; i < arguments.dim; i++) | |
175 { | |
176 if (i) | |
177 buf.writestring(", "); | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
178 auto arg = arguments[i]; |
0 | 179 |
135 | 180 if (arg.storageClass & STCauto) |
181 buf.writestring("auto "); | |
182 | |
0 | 183 if (arg.storageClass & STCout) |
184 buf.writestring("out "); | |
185 else if (arg.storageClass & STCref) | |
186 buf.writestring((global.params.Dversion == 1) ? "inout " : "ref "); | |
187 else if (arg.storageClass & STCin) | |
188 buf.writestring("in "); | |
189 else if (arg.storageClass & STClazy) | |
190 buf.writestring("lazy "); | |
191 else if (arg.storageClass & STCalias) | |
192 buf.writestring("alias "); | |
193 | |
131
206db751bd4c
dmdfe 2.037 compiles now
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
130
diff
changeset
|
194 StorageClass stc = arg.storageClass; |
0 | 195 if (arg.type && arg.type.mod & MODshared) |
196 stc &= ~STCshared; | |
197 | |
198 StorageClassDeclaration.stcToCBuffer(buf, stc & (STCconst | STCimmutable | STCshared | STCscope)); | |
199 | |
200 argbuf.reset(); | |
201 if (arg.storageClass & STCalias) | |
202 { | |
203 if (arg.ident) | |
204 argbuf.writestring(arg.ident.toChars()); | |
205 } | |
206 else | |
207 arg.type.toCBuffer(argbuf, arg.ident, hgs); | |
208 if (arg.defaultArg) | |
209 { | |
210 argbuf.writestring(" = "); | |
211 arg.defaultArg.toCBuffer(argbuf, hgs); | |
212 } | |
213 buf.write(argbuf); | |
214 } | |
215 if (varargs) | |
216 { | |
217 if (i && varargs == 1) | |
218 buf.writeByte(','); | |
219 buf.writestring("..."); | |
220 } | |
221 } | |
222 buf.writeByte(')'); | |
223 } | |
224 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
225 static void argsToDecoBuffer(OutBuffer buf, Parameters arguments) |
0 | 226 { |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
227 //printf("Parameter::argsToDecoBuffer()\n"); |
0 | 228 |
229 // Write argument types | |
230 if (arguments) | |
231 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
232 size_t dim = Parameter.dim(arguments); |
0 | 233 for (size_t i = 0; i < dim; i++) |
234 { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
235 auto arg = Parameter.getNth(arguments, i); |
0 | 236 arg.toDecoBuffer(buf); |
237 } | |
238 } | |
239 } | |
240 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
241 static int isTPL(Parameters arguments) |
0 | 242 { |
243 assert(false); | |
244 } | |
245 | |
246 /*************************************** | |
247 * Determine number of arguments, folding in tuples. | |
248 */ | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
249 static size_t dim(Parameters args) |
0 | 250 { |
251 size_t n = 0; | |
252 if (args) | |
253 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
254 foreach (arg; args) |
0 | 255 { |
256 Type t = arg.type.toBasetype(); | |
257 | |
258 if (t.ty == TY.Ttuple) | |
259 { | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
260 auto tu = cast(TypeTuple)t; |
0 | 261 n += dim(tu.arguments); |
262 } | |
263 else | |
264 n++; | |
265 } | |
266 } | |
267 return n; | |
268 } | |
269 | |
270 /*************************************** | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
271 * Get nth Parameter, folding in tuples. |
0 | 272 * Returns: |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
273 * Parameter nth Parameter |
0 | 274 * null not found, *pn gets incremented by the number |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
275 * of Parameters |
0 | 276 */ |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
277 static Parameter getNth(Parameters args, size_t nth, size_t* pn = null) |
0 | 278 { |
279 if (!args) | |
280 return null; | |
281 | |
282 size_t n = 0; | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
283 foreach (arg; args) |
0 | 284 { |
285 Type t = arg.type.toBasetype(); | |
286 | |
287 if (t.ty == TY.Ttuple) | |
288 { TypeTuple tu = cast(TypeTuple)t; | |
289 arg = getNth(tu.arguments, nth - n, &n); | |
290 if (arg) | |
291 return arg; | |
292 } | |
293 else if (n == nth) | |
294 return arg; | |
295 else | |
296 n++; | |
297 } | |
298 | |
299 if (pn) | |
300 *pn += n; | |
301 | |
302 return null; | |
303 } | |
16
5c9b78899f5d
Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
304 } |