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