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