comparison dmd/Argument.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children 5c9b78899f5d
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
1 module dmd.Argument;
2
3 import dmd.Type;
4 import dmd.Identifier;
5 import dmd.TypeTuple;
6 import dmd.TY;
7 import dmd.Expression;
8 import dmd.OutBuffer;
9 import dmd.HdrGenState;
10 import dmd.ArrayTypes;
11 import dmd.StorageClassDeclaration;
12 import dmd.Global;
13 import dmd.MOD;
14 import dmd.CppMangleState;
15 import dmd.STC;
16
17 class Argument
18 {
19 //enum InOut inout;
20 STC storageClass;
21 Type type;
22 Identifier ident;
23 Expression defaultArg;
24
25 this(STC storageClass, Type type, Identifier ident, Expression defaultArg)
26 {
27 this.type = type;
28 this.ident = ident;
29 this.storageClass = storageClass;
30 this.defaultArg = defaultArg;
31 }
32
33 Argument clone()
34 {
35 return new Argument(storageClass, type, ident, defaultArg);
36 }
37
38 Argument syntaxCopy()
39 {
40 return new Argument(storageClass, type ? type.syntaxCopy() : null, ident, defaultArg ? defaultArg.syntaxCopy() : null);
41 }
42
43 Type isLazyArray()
44 {
45 assert(false);
46 }
47
48 void toDecoBuffer(OutBuffer buf)
49 {
50 if (storageClass & STC.STCscope)
51 buf.writeByte('M');
52 switch (storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STClazy))
53 {
54 case STC.STCundefined:
55 case STC.STCin:
56 break;
57 case STC.STCout:
58 buf.writeByte('J');
59 break;
60 case STC.STCref:
61 buf.writeByte('K');
62 break;
63 case STC.STClazy:
64 buf.writeByte('L');
65 break;
66 }
67 static if (false) {
68 int mod = 0x100;
69 if (type.toBasetype().ty == TY.Tclass)
70 mod = 0;
71 type.toDecoBuffer(buf, mod);
72 } else {
73 //type.toHeadMutable().toDecoBuffer(buf, 0);
74 type.toDecoBuffer(buf, 0);
75 }
76 }
77
78 static Arguments arraySyntaxCopy(Arguments args)
79 {
80 Arguments a = null;
81
82 if (args)
83 {
84 a = new Arguments();
85 a.setDim(args.dim);
86
87 for (size_t i = 0; i < a.dim; i++)
88 {
89 Argument arg = cast(Argument)args.data[i];
90
91 arg = arg.syntaxCopy();
92 a.data[i] = cast(void*)arg;
93 }
94 }
95
96 return a;
97 }
98
99 static string argsTypesToChars(Arguments args, int varargs)
100 {
101 scope OutBuffer buf = new OutBuffer();
102
103 static if (true) {
104 HdrGenState hgs;
105 argsToCBuffer(buf, &hgs, args, varargs);
106 } else {
107 buf.writeByte('(');
108 if (args)
109 {
110 OutBuffer argbuf;
111 HdrGenState hgs;
112
113 for (int i = 0; i < args.dim; i++)
114 {
115 if (i)
116 buf.writeByte(',');
117 Argument arg = cast(Argument)args.data[i];
118 argbuf.reset();
119 arg.type.toCBuffer2(&argbuf, &hgs, 0);
120 buf.write(&argbuf);
121 }
122 if (varargs)
123 {
124 if (i && varargs == 1)
125 buf.writeByte(',');
126 buf.writestring("...");
127 }
128 }
129 buf.writeByte(')');
130 }
131 return buf.toChars();
132 }
133
134 static void argsCppMangle(OutBuffer buf, CppMangleState* cms, Arguments arguments, int varargs)
135 {
136 assert(false);
137 }
138
139 static void argsToCBuffer(OutBuffer buf, HdrGenState* hgs, Arguments arguments, int varargs)
140 {
141 buf.writeByte('(');
142 if (arguments)
143 {
144 int i;
145 scope OutBuffer argbuf = new OutBuffer();
146
147 for (i = 0; i < arguments.dim; i++)
148 {
149 if (i)
150 buf.writestring(", ");
151 Argument arg = cast(Argument)arguments.data[i];
152
153 if (arg.storageClass & STCout)
154 buf.writestring("out ");
155 else if (arg.storageClass & STCref)
156 buf.writestring((global.params.Dversion == 1) ? "inout " : "ref ");
157 else if (arg.storageClass & STCin)
158 buf.writestring("in ");
159 else if (arg.storageClass & STClazy)
160 buf.writestring("lazy ");
161 else if (arg.storageClass & STCalias)
162 buf.writestring("alias ");
163 else if (arg.storageClass & STCauto)
164 buf.writestring("auto ");
165
166 uint stc = arg.storageClass;
167 if (arg.type && arg.type.mod & MODshared)
168 stc &= ~STCshared;
169
170 StorageClassDeclaration.stcToCBuffer(buf, stc & (STCconst | STCimmutable | STCshared | STCscope));
171
172 argbuf.reset();
173 if (arg.storageClass & STCalias)
174 {
175 if (arg.ident)
176 argbuf.writestring(arg.ident.toChars());
177 }
178 else
179 arg.type.toCBuffer(argbuf, arg.ident, hgs);
180 if (arg.defaultArg)
181 {
182 argbuf.writestring(" = ");
183 arg.defaultArg.toCBuffer(argbuf, hgs);
184 }
185 buf.write(argbuf);
186 }
187 if (varargs)
188 {
189 if (i && varargs == 1)
190 buf.writeByte(',');
191 buf.writestring("...");
192 }
193 }
194 buf.writeByte(')');
195 }
196
197 static void argsToDecoBuffer(OutBuffer buf, Arguments arguments)
198 {
199 //printf("Argument::argsToDecoBuffer()\n");
200
201 // Write argument types
202 if (arguments)
203 {
204 size_t dim = Argument.dim(arguments);
205 for (size_t i = 0; i < dim; i++)
206 {
207 Argument arg = Argument.getNth(arguments, i);
208 arg.toDecoBuffer(buf);
209 }
210 }
211 }
212
213 static int isTPL(Arguments arguments)
214 {
215 assert(false);
216 }
217
218 /***************************************
219 * Determine number of arguments, folding in tuples.
220 */
221 static size_t dim(Arguments args)
222 {
223 size_t n = 0;
224 if (args)
225 {
226 for (size_t i = 0; i < args.dim; i++)
227 {
228 Argument arg = cast(Argument)args.data[i];
229 Type t = arg.type.toBasetype();
230
231 if (t.ty == TY.Ttuple)
232 {
233 TypeTuple tu = cast(TypeTuple)t;
234 n += dim(tu.arguments);
235 }
236 else
237 n++;
238 }
239 }
240 return n;
241 }
242
243 /***************************************
244 * Get nth Argument, folding in tuples.
245 * Returns:
246 * Argument nth Argument
247 * null not found, *pn gets incremented by the number
248 * of Arguments
249 */
250 static Argument getNth(Arguments args, size_t nth, size_t* pn = null)
251 {
252 if (!args)
253 return null;
254
255 size_t n = 0;
256 for (size_t i = 0; i < args.dim; i++)
257 {
258 Argument arg = cast(Argument)args.data[i];
259 Type t = arg.type.toBasetype();
260
261 if (t.ty == TY.Ttuple)
262 { TypeTuple tu = cast(TypeTuple)t;
263 arg = getNth(tu.arguments, nth - n, &n);
264 if (arg)
265 return arg;
266 }
267 else if (n == nth)
268 return arg;
269 else
270 n++;
271 }
272
273 if (pn)
274 *pn += n;
275
276 return null;
277 }
278 }