Mercurial > projects > ddmd
annotate dmd/Parser.d @ 74:7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Sun, 29 Aug 2010 09:43:40 +0100 |
parents | 51605de93870 |
children | ad4792a1cfd6 |
rev | line source |
---|---|
0 | 1 module dmd.Parser; |
2 | |
3 import dmd.Lexer; | |
4 import dmd.PostBlitDeclaration; | |
5 import dmd.FileInitExp; | |
6 import dmd.LineInitExp; | |
7 import dmd.EnumMember; | |
8 import dmd.CtorDeclaration; | |
9 import dmd.ShlAssignExp; | |
10 import dmd.ShrAssignExp; | |
11 import dmd.UshrAssignExp; | |
12 import dmd.CatAssignExp; | |
13 import dmd.StaticIfCondition; | |
14 import dmd.TraitsExp; | |
13 | 15 import dmd.TemplateMixin; |
0 | 16 import dmd.BaseClass; |
17 import dmd.AssignExp; | |
18 import dmd.TemplateInstance; | |
19 import dmd.NewExp; | |
20 import dmd.ArrayExp; | |
21 import dmd.DotTemplateInstanceExp; | |
22 import dmd.ClassDeclaration; | |
23 import dmd.NewAnonClassExp; | |
24 import dmd.InterfaceDeclaration; | |
25 import dmd.StructDeclaration; | |
26 import dmd.UnionDeclaration; | |
27 import dmd.AnonDeclaration; | |
28 import dmd.StructInitializer; | |
29 import dmd.ArrayInitializer; | |
30 import dmd.ExpInitializer; | |
31 import dmd.TemplateAliasParameter; | |
32 import dmd.TemplateTupleParameter; | |
33 import dmd.TemplateThisParameter; | |
34 import dmd.TemplateValueParameter; | |
35 import dmd.VoidInitializer; | |
36 import dmd.VersionCondition; | |
37 import dmd.DotIdExp; | |
38 import dmd.DebugCondition; | |
39 import dmd.PostExp; | |
40 import dmd.CallExp; | |
41 import dmd.SliceExp; | |
42 import dmd.FuncExp; | |
43 import dmd.AssocArrayLiteralExp; | |
44 import dmd.ArrayLiteralExp; | |
45 import dmd.IsExp; | |
46 import dmd.FuncLiteralDeclaration; | |
47 import dmd.AssertExp; | |
48 import dmd.CompileExp; | |
49 import dmd.FileExp; | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
50 import dmd.TemplateMixin; |
0 | 51 import dmd.TemplateParameter; |
52 import dmd.TemplateTypeParameter; | |
53 import dmd.TypeidExp; | |
54 import dmd.StringExp; | |
55 import dmd.ScopeExp; | |
56 import dmd.IdentifierExp; | |
57 import dmd.DollarExp; | |
58 import dmd.ThisExp; | |
59 import dmd.SuperExp; | |
60 import dmd.NullExp; | |
61 import dmd.RealExp; | |
62 import dmd.TypeExp; | |
63 import dmd.AddrExp; | |
64 import dmd.MOD; | |
65 import dmd.IntegerExp; | |
66 import dmd.CastExp; | |
67 import dmd.PtrExp; | |
68 import dmd.NegExp; | |
69 import dmd.XorAssignExp; | |
70 import dmd.OrAssignExp; | |
71 import dmd.UAddExp; | |
72 import dmd.NotExp; | |
73 import dmd.ComExp; | |
74 import dmd.DeleteExp; | |
75 import dmd.MulAssignExp; | |
76 import dmd.ModAssignExp; | |
77 import dmd.MinAssignExp; | |
78 import dmd.DivAssignExp; | |
79 import dmd.AndAssignExp; | |
80 import dmd.AddAssignExp; | |
81 import dmd.ModuleDeclaration; | |
82 import dmd.CaseRangeStatement; | |
83 import dmd.CommaExp; | |
84 import dmd.XorExp; | |
85 import dmd.CondExp; | |
86 import dmd.CmpExp; | |
87 import dmd.InExp; | |
88 import dmd.OrOrExp; | |
89 import dmd.OrExp; | |
90 import dmd.AddExp; | |
91 import dmd.MinExp; | |
92 import dmd.CatExp; | |
93 import dmd.AndAndExp; | |
94 import dmd.EqualExp; | |
95 import dmd.ShlExp; | |
96 import dmd.ShrExp; | |
97 import dmd.DivExp; | |
98 import dmd.MulExp; | |
99 import dmd.ModExp; | |
100 import dmd.UshrExp; | |
101 import dmd.IdentityExp; | |
102 import dmd.AndExp; | |
103 import dmd.Id; | |
104 import dmd.LabelStatement; | |
105 import dmd.ExpStatement; | |
106 import dmd.StaticAssertStatement; | |
107 import dmd.DeclarationStatement; | |
108 import dmd.ScopeStatement; | |
109 import dmd.PragmaStatement; | |
110 import dmd.WhileStatement; | |
111 import dmd.DoStatement; | |
112 import dmd.ForStatement; | |
113 import dmd.OnScopeStatement; | |
114 import dmd.IfStatement; | |
115 import dmd.SwitchStatement; | |
116 import dmd.CaseStatement; | |
117 import dmd.DefaultStatement; | |
118 import dmd.GotoDefaultStatement; | |
119 import dmd.GotoCaseStatement; | |
120 import dmd.GotoStatement; | |
121 import dmd.SynchronizedStatement; | |
122 import dmd.WithStatement; | |
123 import dmd.Catch; | |
124 import dmd.TryCatchStatement; | |
125 import dmd.TryFinallyStatement; | |
126 import dmd.ThrowStatement; | |
127 import dmd.VolatileStatement; | |
128 import dmd.ReturnStatement; | |
129 import dmd.BreakStatement; | |
130 import dmd.ContinueStatement; | |
131 import dmd.AsmStatement; | |
132 import dmd.TypeReturn; | |
133 import dmd.TypeTypeof; | |
134 import dmd.ForeachRangeStatement; | |
135 import dmd.ForeachStatement; | |
136 import dmd.CompileStatement; | |
137 import dmd.CompoundStatement; | |
138 import dmd.ConditionalStatement; | |
139 import dmd.CompoundDeclarationStatement; | |
140 import dmd.Argument; | |
141 import dmd.ParseStatementFlags; | |
142 import dmd.TypeNext; | |
143 import dmd.TypeInstance; | |
144 import dmd.TypePointer; | |
145 import dmd.TypeDArray; | |
146 import dmd.TypeAArray; | |
147 import dmd.TypeSlice; | |
148 import dmd.TypeSArray; | |
149 import dmd.TemplateInstance; | |
150 import dmd.TypeIdentifier; | |
151 import dmd.VarDeclaration; | |
152 import dmd.TypeFunction; | |
153 import dmd.TypeDelegate; | |
154 import dmd.TY; | |
155 import dmd.LinkDeclaration; | |
156 import dmd.Declaration; | |
157 import dmd.AggregateDeclaration; | |
158 import dmd.TypedefDeclaration; | |
159 import dmd.AliasDeclaration; | |
160 import dmd.LINK; | |
161 import dmd.Loc; | |
162 import dmd.Module; | |
163 import dmd.Array; | |
164 import dmd.Expression; | |
165 import dmd.TemplateDeclaration; | |
166 import dmd.ArrayTypes; | |
167 import dmd.Dsymbol; | |
168 import dmd.StaticAssert; | |
169 import dmd.TypeQualified; | |
170 import dmd.Condition; | |
171 import dmd.PostBlitDeclaration; | |
172 import dmd.DtorDeclaration; | |
173 import dmd.ConditionalDeclaration; | |
174 import dmd.StaticCtorDeclaration; | |
175 import dmd.StaticDtorDeclaration; | |
176 import dmd.InvariantDeclaration; | |
177 import dmd.UnitTestDeclaration; | |
178 import dmd.NewDeclaration; | |
179 import dmd.DeleteDeclaration; | |
180 import dmd.EnumDeclaration; | |
181 import dmd.Import; | |
182 import dmd.Type; | |
183 import dmd.Identifier; | |
184 import dmd.FuncDeclaration; | |
185 import dmd.Statement; | |
186 import dmd.Initializer; | |
187 import dmd.Token; | |
188 import dmd.TOK; | |
189 import dmd.ParseStatementFlags; | |
190 import dmd.PROT; | |
191 import dmd.STC; | |
192 import dmd.Util; | |
193 import dmd.CompileDeclaration; | |
194 import dmd.StaticIfDeclaration; | |
195 import dmd.StorageClassDeclaration; | |
196 import dmd.LinkDeclaration; | |
197 import dmd.ProtDeclaration; | |
198 import dmd.AlignDeclaration; | |
199 import dmd.PragmaDeclaration; | |
200 import dmd.DebugSymbol; | |
201 import dmd.VersionSymbol; | |
202 import dmd.AliasThis; | |
203 import dmd.Global; | |
204 | |
205 import core.stdc.string : memcpy; | |
206 | |
34 | 207 import std.exception; |
4 | 208 import core.memory; |
0 | 209 |
210 class Parser : Lexer | |
211 { | |
212 ModuleDeclaration md; | |
213 LINK linkage; | |
214 Loc endloc; // set to location of last right curly | |
215 int inBrackets; // inside [] of array index or slice | |
216 | |
217 this(Module module_, ubyte* base, uint length, int doDocComment) | |
218 { | |
219 super(module_, base, 0, length, doDocComment, 0); | |
220 //printf("Parser.Parser()\n"); | |
221 linkage = LINK.LINKd; | |
222 //nextToken(); // start up the scanner | |
223 } | |
224 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
225 Dsymbols parseModule() |
0 | 226 { |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
227 typeof(return) decldefs; |
0 | 228 |
229 // ModuleDeclation leads off | |
230 if (token.value == TOK.TOKmodule) | |
231 { | |
232 ubyte* comment = token.blockComment; | |
233 bool safe = false; | |
234 | |
235 nextToken(); | |
236 version (DMDV2) { | |
237 if (token.value == TOK.TOKlparen) | |
238 { | |
239 nextToken(); | |
240 if (token.value != TOK.TOKidentifier) | |
241 { | |
242 error("module (system) identifier expected"); | |
243 goto Lerr; | |
244 } | |
245 Identifier id = token.ident; | |
246 | |
247 if (id is Id.system) | |
248 safe = true; | |
249 else | |
250 error("(safe) expected, not %s", id.toChars()); | |
251 nextToken(); | |
252 check(TOK.TOKrparen); | |
253 } | |
254 } | |
255 | |
256 if (token.value != TOK.TOKidentifier) | |
257 { | |
258 error("Identifier expected following module"); | |
259 goto Lerr; | |
260 } | |
261 else | |
262 { | |
263 Array a = null; | |
264 Identifier id = token.ident; | |
265 while (nextToken() == TOK.TOKdot) | |
266 { | |
267 if (!a) | |
268 a = new Array(); | |
269 a.push(cast(void*)id); | |
270 nextToken(); | |
271 if (token.value != TOK.TOKidentifier) | |
272 { error("Identifier expected following package"); | |
273 goto Lerr; | |
274 } | |
275 id = token.ident; | |
276 } | |
277 | |
278 md = new ModuleDeclaration(a, id, safe); | |
279 | |
280 if (token.value != TOK.TOKsemicolon) | |
281 error("';' expected following module declaration instead of %s", token.toChars()); | |
282 | |
283 nextToken(); | |
284 addComment(mod, comment); | |
285 } | |
286 } | |
287 | |
288 decldefs = parseDeclDefs(0); | |
289 if (token.value != TOK.TOKeof) | |
290 { | |
291 error("unrecognized declaration"); | |
292 goto Lerr; | |
293 } | |
294 | |
295 return decldefs; | |
296 | |
297 Lerr: | |
298 while (token.value != TOK.TOKsemicolon && token.value != TOK.TOKeof) | |
299 nextToken(); | |
300 | |
301 nextToken(); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
302 return new Dsymbols(); |
0 | 303 } |
304 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
305 Dsymbols parseDeclDefs(int once) |
0 | 306 { |
307 Dsymbol s; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
308 Dsymbols decldefs; |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
309 Dsymbols a; |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
310 Dsymbols aelse; |
0 | 311 PROT prot; |
312 STC stc; | |
313 STC storageClass; | |
314 Condition condition; | |
315 ubyte* comment; | |
316 | |
317 //printf("Parser.parseDeclDefs()\n"); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
318 decldefs = new Dsymbols(); |
0 | 319 do |
320 { | |
321 comment = token.blockComment; | |
322 storageClass = STC.STCundefined; | |
323 switch (token.value) | |
324 { | |
325 case TOK.TOKenum: | |
326 { /* Determine if this is a manifest constant declaration, | |
327 * or a conventional enum. | |
328 */ | |
329 Token *t = peek(&token); | |
330 if (t.value == TOK.TOKlcurly || t.value == TOK.TOKcolon) | |
331 s = parseEnum(); | |
332 else if (t.value != TOK.TOKidentifier) | |
333 goto Ldeclaration; | |
334 else | |
335 { | |
336 t = peek(t); | |
337 if (t.value == TOK.TOKlcurly || t.value == TOK.TOKcolon || | |
338 t.value == TOK.TOKsemicolon) | |
339 s = parseEnum(); | |
340 else | |
341 goto Ldeclaration; | |
342 } | |
343 break; | |
344 } | |
345 | |
346 case TOK.TOKstruct: | |
347 case TOK.TOKunion: | |
348 case TOK.TOKclass: | |
349 case TOK.TOKinterface: | |
350 s = parseAggregate(); | |
351 break; | |
352 | |
353 case TOK.TOKimport: | |
354 s = parseImport(decldefs, 0); | |
355 break; | |
356 | |
357 case TOK.TOKtemplate: | |
358 s = cast(Dsymbol)parseTemplateDeclaration(); | |
359 break; | |
360 | |
361 case TOK.TOKmixin: | |
362 { Loc loc = this.loc; | |
363 if (peek(&token).value == TOK.TOKlparen) | |
364 { // mixin(string) | |
365 nextToken(); | |
366 check(TOK.TOKlparen, "mixin"); | |
367 Expression e = parseAssignExp(); | |
368 check(TOK.TOKrparen); | |
369 check(TOK.TOKsemicolon); | |
370 s = new CompileDeclaration(loc, e); | |
371 break; | |
372 } | |
373 s = parseMixin(); | |
374 break; | |
375 } | |
376 | |
377 case TOK.TOKwchar: case TOK.TOKdchar: | |
378 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
379 case TOK.TOKint8: case TOK.TOKuns8: | |
380 case TOK.TOKint16: case TOK.TOKuns16: | |
381 case TOK.TOKint32: case TOK.TOKuns32: | |
382 case TOK.TOKint64: case TOK.TOKuns64: | |
383 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
384 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
385 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
386 case TOK.TOKvoid: | |
387 case TOK.TOKalias: | |
388 case TOK.TOKtypedef: | |
389 case TOK.TOKidentifier: | |
390 case TOK.TOKtypeof: | |
391 case TOK.TOKdot: | |
392 Ldeclaration: | |
393 a = parseDeclarations(STC.STCundefined); | |
394 decldefs.append(a); | |
395 continue; | |
396 | |
397 case TOK.TOKthis: | |
398 s = parseCtor(); | |
399 break; | |
400 | |
401 static if (false) { // dead end, use this(this){} instead | |
402 case TOK.TOKassign: | |
403 s = parsePostBlit(); | |
404 break; | |
405 } | |
406 case TOK.TOKtilde: | |
407 s = parseDtor(); | |
408 break; | |
409 | |
410 case TOK.TOKinvariant: | |
411 { Token *t; | |
412 t = peek(&token); | |
413 if (t.value == TOK.TOKlparen) | |
414 { | |
415 if (peek(t).value == TOK.TOKrparen) | |
416 // invariant() forms start of class invariant | |
417 s = parseInvariant(); | |
418 else | |
419 // invariant(type) | |
420 goto Ldeclaration; | |
421 } | |
422 else | |
423 { | |
424 stc = STC.STCimmutable; | |
425 goto Lstc; | |
426 } | |
427 break; | |
428 } | |
429 | |
430 case TOK.TOKunittest: | |
431 s = parseUnitTest(); | |
432 break; | |
433 | |
434 case TOK.TOKnew: | |
435 s = parseNew(); | |
436 break; | |
437 | |
438 case TOK.TOKdelete: | |
439 s = parseDelete(); | |
440 break; | |
441 | |
442 case TOK.TOKeof: | |
443 case TOK.TOKrcurly: | |
444 return decldefs; | |
445 | |
446 case TOK.TOKstatic: | |
447 nextToken(); | |
448 if (token.value == TOK.TOKthis) | |
449 s = parseStaticCtor(); | |
450 else if (token.value == TOK.TOKtilde) | |
451 s = parseStaticDtor(); | |
452 else if (token.value == TOK.TOKassert) | |
453 s = parseStaticAssert(); | |
454 else if (token.value == TOK.TOKif) | |
455 { condition = parseStaticIfCondition(); | |
456 a = parseBlock(); | |
457 aelse = null; | |
458 if (token.value == TOK.TOKelse) | |
459 { nextToken(); | |
460 aelse = parseBlock(); | |
461 } | |
462 s = new StaticIfDeclaration(condition, a, aelse); | |
463 break; | |
464 } | |
465 else if (token.value == TOK.TOKimport) | |
466 { | |
467 s = parseImport(decldefs, 1); | |
468 } | |
469 else | |
470 { stc = STC.STCstatic; | |
471 goto Lstc2; | |
472 } | |
473 break; | |
474 | |
475 case TOK.TOKconst: | |
476 if (peek(&token).value == TOK.TOKlparen) | |
477 goto Ldeclaration; | |
478 stc = STC.STCconst; | |
479 goto Lstc; | |
480 | |
481 case TOK.TOKimmutable: | |
482 if (peek(&token).value == TOK.TOKlparen) | |
483 goto Ldeclaration; | |
484 stc = STC.STCimmutable; | |
485 goto Lstc; | |
486 | |
487 case TOK.TOKshared: | |
488 if (peek(&token).value == TOK.TOKlparen) | |
489 goto Ldeclaration; | |
490 stc = STC.STCshared; | |
491 goto Lstc; | |
492 | |
493 case TOK.TOKfinal: stc = STC.STCfinal; goto Lstc; | |
494 case TOK.TOKauto: stc = STC.STCauto; goto Lstc; | |
495 case TOK.TOKscope: stc = STC.STCscope; goto Lstc; | |
496 case TOK.TOKoverride: stc = STC.STCoverride; goto Lstc; | |
497 case TOK.TOKabstract: stc = STC.STCabstract; goto Lstc; | |
498 case TOK.TOKsynchronized: stc = STC.STCsynchronized; goto Lstc; | |
499 case TOK.TOKdeprecated: stc = STC.STCdeprecated; goto Lstc; | |
500 version (DMDV2) { | |
501 case TOK.TOKnothrow: stc = STC.STCnothrow; goto Lstc; | |
502 case TOK.TOKpure: stc = STC.STCpure; goto Lstc; | |
503 case TOK.TOKref: stc = STC.STCref; goto Lstc; | |
504 case TOK.TOKtls: stc = STC.STCtls; goto Lstc; | |
505 case TOK.TOKgshared: | |
506 stc = STC.STCgshared; goto Lstc; | |
507 //case TOK.TOKmanifest: stc = STC.STCmanifest; goto Lstc; | |
508 } | |
509 | |
510 Lstc: | |
511 if (storageClass & stc) | |
512 error("redundant storage class %s", Token.toChars(token.value)); | |
513 composeStorageClass(storageClass | stc); | |
514 nextToken(); | |
515 Lstc2: | |
516 storageClass |= stc; | |
517 switch (token.value) | |
518 { | |
519 case TOK.TOKconst: | |
520 case TOK.TOKinvariant: | |
521 case TOK.TOKimmutable: | |
522 case TOK.TOKshared: | |
523 // If followed by a (, it is not a storage class | |
524 if (peek(&token).value == TOK.TOKlparen) | |
525 break; | |
526 if (token.value == TOK.TOKconst) | |
527 stc = STC.STCconst; | |
528 else if (token.value == TOK.TOKshared) | |
529 stc = STC.STCshared; | |
530 else | |
531 stc = STC.STCimmutable; | |
532 goto Lstc; | |
533 case TOK.TOKfinal: stc = STC.STCfinal; goto Lstc; | |
534 case TOK.TOKauto: stc = STC.STCauto; goto Lstc; | |
535 case TOK.TOKscope: stc = STC.STCscope; goto Lstc; | |
536 case TOK.TOKoverride: stc = STC.STCoverride; goto Lstc; | |
537 case TOK.TOKabstract: stc = STC.STCabstract; goto Lstc; | |
538 case TOK.TOKsynchronized: stc = STC.STCsynchronized; goto Lstc; | |
539 case TOK.TOKdeprecated: stc = STC.STCdeprecated; goto Lstc; | |
540 case TOK.TOKnothrow: stc = STC.STCnothrow; goto Lstc; | |
541 case TOK.TOKpure: stc = STC.STCpure; goto Lstc; | |
542 case TOK.TOKref: stc = STC.STCref; goto Lstc; | |
543 case TOK.TOKtls: stc = STC.STCtls; goto Lstc; | |
544 case TOK.TOKgshared: stc = STC.STCgshared; goto Lstc; | |
545 //case TOK.TOKmanifest: stc = STC.STCmanifest; goto Lstc; | |
546 default: | |
547 break; | |
548 } | |
549 | |
550 /* Look for auto initializers: | |
551 * storage_class identifier = initializer; | |
552 */ | |
553 if (token.value == TOK.TOKidentifier && | |
554 peek(&token).value == TOK.TOKassign) | |
555 { | |
556 a = parseAutoDeclarations(storageClass, comment); | |
557 decldefs.append(a); | |
558 continue; | |
559 } | |
560 | |
561 /* Look for return type inference for template functions. | |
562 */ | |
563 Token *tk; | |
564 if (token.value == TOK.TOKidentifier && | |
565 (tk = peek(&token)).value == TOK.TOKlparen && | |
566 skipParens(tk, &tk) && | |
567 (peek(tk).value == TOK.TOKlparen || | |
568 peek(tk).value == TOK.TOKlcurly) | |
569 ) | |
570 { | |
571 a = parseDeclarations(storageClass); | |
572 decldefs.append(a); | |
573 continue; | |
574 } | |
575 a = parseBlock(); | |
576 s = new StorageClassDeclaration(storageClass, a); | |
577 break; | |
578 | |
579 case TOK.TOKextern: | |
580 if (peek(&token).value != TOK.TOKlparen) | |
581 { | |
582 stc = STC.STCextern; | |
583 goto Lstc; | |
584 } | |
585 { | |
586 LINK linksave = linkage; | |
587 linkage = parseLinkage(); | |
588 a = parseBlock(); | |
589 s = new LinkDeclaration(linkage, a); | |
590 linkage = linksave; | |
591 break; | |
592 } | |
593 | |
594 case TOK.TOKprivate: prot = PROT.PROTprivate; goto Lprot; | |
595 case TOK.TOKpackage: prot = PROT.PROTpackage; goto Lprot; | |
596 case TOK.TOKprotected: prot = PROT.PROTprotected; goto Lprot; | |
597 case TOK.TOKpublic: prot = PROT.PROTpublic; goto Lprot; | |
598 case TOK.TOKexport: prot = PROT.PROTexport; goto Lprot; | |
599 Lprot: | |
600 nextToken(); | |
601 switch (token.value) | |
602 { | |
603 case TOK.TOKprivate: | |
604 case TOK.TOKpackage: | |
605 case TOK.TOKprotected: | |
606 case TOK.TOKpublic: | |
607 case TOK.TOKexport: | |
608 error("redundant protection attribute"); | |
609 break; | |
610 default: | |
611 break; | |
612 } | |
613 a = parseBlock(); | |
614 s = new ProtDeclaration(prot, a); | |
615 break; | |
616 | |
617 case TOK.TOKalign: | |
618 { uint n; | |
619 | |
620 s = null; | |
621 nextToken(); | |
622 if (token.value == TOK.TOKlparen) | |
623 { | |
624 nextToken(); | |
625 if (token.value == TOK.TOKint32v) | |
626 n = cast(uint)token.uns64value; | |
627 else | |
628 { error("integer expected, not %s", token.toChars()); | |
629 n = 1; | |
630 } | |
631 nextToken(); | |
632 check(TOK.TOKrparen); | |
633 } | |
634 else | |
635 n = global.structalign; // default | |
636 | |
637 a = parseBlock(); | |
638 s = new AlignDeclaration(n, a); | |
639 break; | |
640 } | |
641 | |
642 case TOK.TOKpragma: | |
643 { Identifier ident; | |
644 Expressions args = null; | |
645 | |
646 nextToken(); | |
647 check(TOK.TOKlparen); | |
648 if (token.value != TOK.TOKidentifier) | |
649 { error("pragma(identifier expected"); | |
650 goto Lerror; | |
651 } | |
652 ident = token.ident; | |
653 nextToken(); | |
654 if (token.value == TOK.TOKcomma && peekNext() != TOK.TOKrparen) | |
655 args = parseArguments(); // pragma(identifier, args...) | |
656 else | |
657 check(TOK.TOKrparen); // pragma(identifier) | |
658 | |
659 if (token.value == TOK.TOKsemicolon) | |
660 a = null; | |
661 else | |
662 a = parseBlock(); | |
663 s = new PragmaDeclaration(loc, ident, args, a); | |
664 break; | |
665 } | |
666 | |
667 case TOK.TOKdebug: | |
668 nextToken(); | |
669 if (token.value == TOK.TOKassign) | |
670 { | |
671 nextToken(); | |
672 if (token.value == TOK.TOKidentifier) | |
673 s = new DebugSymbol(loc, token.ident); | |
674 else if (token.value == TOK.TOKint32v) | |
675 s = new DebugSymbol(loc, cast(uint)token.uns64value); | |
676 else | |
677 { error("identifier or integer expected, not %s", token.toChars()); | |
678 s = null; | |
679 } | |
680 nextToken(); | |
681 if (token.value != TOK.TOKsemicolon) | |
682 error("semicolon expected"); | |
683 nextToken(); | |
684 break; | |
685 } | |
686 | |
687 condition = parseDebugCondition(); | |
688 goto Lcondition; | |
689 | |
690 case TOK.TOKversion: | |
691 nextToken(); | |
692 if (token.value == TOK.TOKassign) | |
693 { | |
694 nextToken(); | |
695 if (token.value == TOK.TOKidentifier) | |
696 s = new VersionSymbol(loc, token.ident); | |
697 else if (token.value == TOK.TOKint32v) | |
698 s = new VersionSymbol(loc, cast(uint)token.uns64value); | |
699 else | |
700 { error("identifier or integer expected, not %s", token.toChars()); | |
701 s = null; | |
702 } | |
703 nextToken(); | |
704 if (token.value != TOK.TOKsemicolon) | |
705 error("semicolon expected"); | |
706 nextToken(); | |
707 break; | |
708 } | |
709 condition = parseVersionCondition(); | |
710 goto Lcondition; | |
711 | |
712 Lcondition: | |
713 a = parseBlock(); | |
714 aelse = null; | |
715 if (token.value == TOK.TOKelse) | |
716 { nextToken(); | |
717 aelse = parseBlock(); | |
718 } | |
719 s = new ConditionalDeclaration(condition, a, aelse); | |
720 break; | |
721 | |
722 case TOK.TOKsemicolon: // empty declaration | |
723 nextToken(); | |
724 continue; | |
725 | |
726 default: | |
727 error("Declaration expected, not '%s'",token.toChars()); | |
728 Lerror: | |
729 while (token.value != TOK.TOKsemicolon && token.value != TOK.TOKeof) | |
730 nextToken(); | |
731 nextToken(); | |
732 s = null; | |
733 continue; | |
734 } | |
735 if (s) | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
736 { decldefs.push(s); |
0 | 737 addComment(s, comment); |
738 } | |
739 } while (!once); | |
740 return decldefs; | |
741 } | |
742 | |
743 /***************************************** | |
744 * Parse auto declarations of the form: | |
745 * storageClass ident = init, ident = init, ... ; | |
746 * and return the array of them. | |
747 * Starts with token on the first ident. | |
748 * Ends with scanner past closing ';' | |
749 */ | |
750 version (DMDV2) { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
751 Dsymbols parseAutoDeclarations(STC storageClass, ubyte* comment) |
0 | 752 { |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
753 auto a = new Dsymbols; |
0 | 754 |
755 while (true) | |
756 { | |
757 Identifier ident = token.ident; | |
758 nextToken(); // skip over ident | |
759 assert(token.value == TOKassign); | |
760 nextToken(); // skip over '=' | |
761 Initializer init = parseInitializer(); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
762 auto v = new VarDeclaration(loc, null, ident, init); |
0 | 763 v.storage_class = storageClass; |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
764 a.push(v); |
0 | 765 if (token.value == TOKsemicolon) |
766 { | |
767 nextToken(); | |
768 addComment(v, comment); | |
769 } | |
770 else if (token.value == TOKcomma) | |
771 { | |
772 nextToken(); | |
773 if (token.value == TOKidentifier && | |
774 peek(&token).value == TOKassign) | |
775 { | |
776 addComment(v, comment); | |
777 continue; | |
778 } | |
779 else | |
780 error("Identifier expected following comma"); | |
781 } | |
782 else | |
783 error("semicolon expected following auto declaration, not '%s'", token.toChars()); | |
784 break; | |
785 } | |
786 return a; | |
787 } | |
788 } | |
789 /******************************************** | |
790 * Parse declarations after an align, protection, or extern decl. | |
791 */ | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
792 Dsymbols parseBlock() |
0 | 793 { |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
794 Dsymbols a = null; |
0 | 795 Dsymbol ss; |
796 | |
797 //printf("parseBlock()\n"); | |
798 switch (token.value) | |
799 { | |
800 case TOK.TOKsemicolon: | |
801 error("declaration expected following attribute, not ';'"); | |
802 nextToken(); | |
803 break; | |
804 | |
805 case TOK.TOKeof: | |
806 error("declaration expected following attribute, not EOF"); | |
807 break; | |
808 | |
809 case TOK.TOKlcurly: | |
810 nextToken(); | |
811 a = parseDeclDefs(0); | |
812 if (token.value != TOK.TOKrcurly) | |
813 { /* { */ | |
814 error("matching '}' expected, not %s", token.toChars()); | |
815 } | |
816 else | |
817 nextToken(); | |
818 break; | |
819 | |
820 case TOK.TOKcolon: | |
821 nextToken(); | |
822 static if (false) { | |
823 a = null; | |
824 } else { | |
825 a = parseDeclDefs(0); // grab declarations up to closing curly bracket | |
826 } | |
827 break; | |
828 | |
829 default: | |
830 a = parseDeclDefs(1); | |
831 break; | |
832 } | |
833 return a; | |
834 } | |
835 | |
836 void composeStorageClass(STC stc) | |
837 { | |
838 STC u = stc; | |
839 u &= STC.STCconst | STC.STCimmutable | STC.STCmanifest; | |
840 if (u & (u - 1)) | |
841 error("conflicting storage class %s", Token.toChars(token.value)); | |
842 | |
843 u = stc; | |
844 u &= STC.STCgshared | STC.STCshared | STC.STCtls; | |
845 if (u & (u - 1)) | |
846 error("conflicting storage class %s", Token.toChars(token.value)); | |
847 } | |
848 | |
849 /************************************** | |
850 * Parse constraint. | |
851 * Constraint is of the form: | |
852 * if ( ConstraintExpression ) | |
853 */ | |
854 version (DMDV2) { | |
855 Expression parseConstraint() | |
856 { | |
857 Expression e = null; | |
858 | |
859 if (token.value == TOKif) | |
860 { | |
861 nextToken(); // skip over 'if' | |
862 check(TOKlparen); | |
863 e = parseExpression(); | |
864 check(TOKrparen); | |
865 } | |
866 return e; | |
867 } | |
868 } | |
869 /************************************** | |
870 * Parse a TemplateDeclaration. | |
871 */ | |
872 TemplateDeclaration parseTemplateDeclaration() | |
873 { | |
874 TemplateDeclaration tempdecl; | |
875 Identifier id; | |
876 TemplateParameters tpl; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
877 Dsymbols decldefs; |
0 | 878 Expression constraint = null; |
879 Loc loc = this.loc; | |
880 | |
881 nextToken(); | |
882 if (token.value != TOKidentifier) | |
883 { | |
884 error("TemplateIdentifier expected following template"); | |
885 goto Lerr; | |
886 } | |
887 id = token.ident; | |
888 nextToken(); | |
889 tpl = parseTemplateParameterList(); | |
890 if (!tpl) | |
891 goto Lerr; | |
892 | |
893 constraint = parseConstraint(); | |
894 | |
895 if (token.value != TOKlcurly) | |
896 { | |
897 error("members of template declaration expected"); | |
898 goto Lerr; | |
899 } | |
900 else | |
901 { | |
902 nextToken(); | |
903 decldefs = parseDeclDefs(0); | |
904 if (token.value != TOKrcurly) | |
905 { | |
906 error("template member expected"); | |
907 goto Lerr; | |
908 } | |
909 nextToken(); | |
910 } | |
911 | |
912 tempdecl = new TemplateDeclaration(loc, id, tpl, constraint, decldefs); | |
913 return tempdecl; | |
914 | |
915 Lerr: | |
916 return null; | |
917 } | |
918 | |
919 /****************************************** | |
920 * Parse template parameter list. | |
921 * Input: | |
922 * flag 0: parsing "( list )" | |
923 * 1: parsing non-empty "list )" | |
924 */ | |
925 TemplateParameters parseTemplateParameterList(int flag = 0) | |
926 { | |
927 TemplateParameters tpl = new TemplateParameters(); | |
928 | |
929 if (!flag && token.value != TOKlparen) | |
930 { | |
931 error("parenthesized TemplateParameterList expected following TemplateIdentifier"); | |
932 goto Lerr; | |
933 } | |
934 nextToken(); | |
935 | |
936 // Get array of TemplateParameters | |
937 if (flag || token.value != TOKrparen) | |
938 { | |
939 int isvariadic = 0; | |
940 | |
941 while (true) | |
942 { | |
943 TemplateParameter tp; | |
944 Identifier tp_ident = null; | |
945 Type tp_spectype = null; | |
946 Type tp_valtype = null; | |
947 Type tp_defaulttype = null; | |
948 Expression tp_specvalue = null; | |
949 Expression tp_defaultvalue = null; | |
950 Token* t; | |
951 | |
952 // Get TemplateParameter | |
953 | |
954 // First, look ahead to see if it is a TypeParameter or a ValueParameter | |
955 t = peek(&token); | |
956 if (token.value == TOKalias) | |
957 { | |
958 // AliasParameter | |
959 nextToken(); | |
960 Type spectype = null; | |
961 if (isDeclaration(&token, 2, TOKreserved, null)) | |
962 { | |
963 spectype = parseType(&tp_ident); | |
964 } | |
965 else | |
966 { | |
967 if (token.value != TOKidentifier) | |
968 { | |
969 error("identifier expected for template alias parameter"); | |
970 goto Lerr; | |
971 } | |
972 tp_ident = token.ident; | |
973 nextToken(); | |
974 } | |
975 Object spec = null; | |
976 if (token.value == TOKcolon) // : Type | |
977 { | |
978 nextToken(); | |
979 if (isDeclaration(&token, 0, TOKreserved, null)) | |
980 spec = parseType(); | |
981 else | |
982 spec = parseCondExp(); | |
983 } | |
984 Object def = null; | |
985 if (token.value == TOKassign) // = Type | |
986 { | |
987 nextToken(); | |
988 if (isDeclaration(&token, 0, TOKreserved, null)) | |
989 def = parseType(); | |
990 else | |
991 def = parseCondExp(); | |
992 } | |
993 tp = new TemplateAliasParameter(loc, tp_ident, spectype, spec, def); | |
994 } | |
995 else if (t.value == TOKcolon || t.value == TOKassign || | |
996 t.value == TOKcomma || t.value == TOKrparen) | |
997 { // TypeParameter | |
998 if (token.value != TOKidentifier) | |
999 { error("identifier expected for template type parameter"); | |
1000 goto Lerr; | |
1001 } | |
1002 tp_ident = token.ident; | |
1003 nextToken(); | |
1004 if (token.value == TOKcolon) // : Type | |
1005 { | |
1006 nextToken(); | |
1007 tp_spectype = parseType(); | |
1008 } | |
1009 if (token.value == TOKassign) // = Type | |
1010 { | |
1011 nextToken(); | |
1012 tp_defaulttype = parseType(); | |
1013 } | |
1014 tp = new TemplateTypeParameter(loc, tp_ident, tp_spectype, tp_defaulttype); | |
1015 } | |
1016 else if (token.value == TOKidentifier && t.value == TOKdotdotdot) | |
1017 { // ident... | |
1018 if (isvariadic) | |
1019 error("variadic template parameter must be last"); | |
1020 isvariadic = 1; | |
1021 tp_ident = token.ident; | |
1022 nextToken(); | |
1023 nextToken(); | |
1024 tp = new TemplateTupleParameter(loc, tp_ident); | |
1025 } | |
1026 /// version (DMDV2) { | |
1027 else if (token.value == TOKthis) | |
1028 { // ThisParameter | |
1029 nextToken(); | |
1030 if (token.value != TOKidentifier) | |
1031 { error("identifier expected for template this parameter"); | |
1032 goto Lerr; | |
1033 } | |
1034 tp_ident = token.ident; | |
1035 nextToken(); | |
1036 if (token.value == TOKcolon) // : Type | |
1037 { | |
1038 nextToken(); | |
1039 tp_spectype = parseType(); | |
1040 } | |
1041 if (token.value == TOKassign) // = Type | |
1042 { | |
1043 nextToken(); | |
1044 tp_defaulttype = parseType(); | |
1045 } | |
1046 tp = new TemplateThisParameter(loc, tp_ident, tp_spectype, tp_defaulttype); | |
1047 } | |
1048 /// } | |
1049 else | |
1050 { // ValueParameter | |
1051 tp_valtype = parseType(&tp_ident); | |
1052 if (!tp_ident) | |
1053 { | |
1054 error("identifier expected for template value parameter"); | |
1055 tp_ident = new Identifier("error", TOKidentifier); | |
1056 } | |
1057 if (token.value == TOKcolon) // : CondExpression | |
1058 { | |
1059 nextToken(); | |
1060 tp_specvalue = parseCondExp(); | |
1061 } | |
1062 if (token.value == TOKassign) // = CondExpression | |
1063 { | |
1064 nextToken(); | |
1065 tp_defaultvalue = parseDefaultInitExp(); | |
1066 } | |
1067 tp = new TemplateValueParameter(loc, tp_ident, tp_valtype, tp_specvalue, tp_defaultvalue); | |
1068 } | |
1069 tpl.push(cast(void*)tp); | |
1070 if (token.value != TOKcomma) | |
1071 break; | |
1072 nextToken(); | |
1073 } | |
1074 } | |
1075 check(TOKrparen); | |
1076 | |
1077 Lerr: | |
1078 return tpl; | |
1079 } | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1080 |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1081 /****************************************** |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1082 * Parse template mixin. |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1083 * mixin Foo; |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1084 * mixin Foo!(args); |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1085 * mixin a.b.c!(args).Foo!(args); |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1086 * mixin Foo!(args) identifier; |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1087 * mixin typeof(expr).identifier!(args); |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1088 */ |
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1089 |
0 | 1090 Dsymbol parseMixin() |
1091 { | |
13 | 1092 TemplateMixin tm; |
1093 Identifier id; | |
1094 Type tqual; | |
1095 Objects tiargs; | |
1096 Array idents; | |
1097 | |
1098 //printf("parseMixin()\n"); | |
1099 nextToken(); | |
1100 tqual = null; | |
1101 if (token.value == TOKdot) | |
1102 { | |
1103 id = Id.empty; | |
1104 } | |
1105 else | |
1106 { | |
1107 if (token.value == TOKtypeof) | |
1108 { | |
1109 tqual = parseTypeof(); | |
1110 check(TOKdot); | |
1111 } | |
1112 if (token.value != TOKidentifier) | |
1113 { | |
1114 error("identifier expected, not %s", token.toChars()); | |
1115 id = Id.empty; | |
1116 } | |
1117 else | |
1118 id = token.ident; | |
1119 nextToken(); | |
1120 } | |
1121 | |
1122 idents = new Array(); | |
1123 while (1) | |
1124 { | |
1125 tiargs = null; | |
1126 if (token.value == TOKnot) | |
1127 { | |
1128 nextToken(); | |
1129 if (token.value == TOKlparen) | |
1130 tiargs = parseTemplateArgumentList(); | |
1131 else | |
1132 tiargs = parseTemplateArgument(); | |
1133 } | |
1134 if (token.value != TOKdot) | |
1135 break; | |
1136 if (tiargs) | |
1137 { | |
1138 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
1139 tempinst.tiargs = tiargs; | |
1140 id = cast(Identifier)tempinst; | |
1141 tiargs = null; | |
1142 } | |
1143 idents.push(cast(void*)id); | |
1144 nextToken(); | |
1145 if (token.value != TOKidentifier) | |
1146 { | |
1147 error("identifier expected following '.' instead of '%s'", token.toChars()); | |
1148 break; | |
1149 } | |
1150 id = token.ident; | |
1151 nextToken(); | |
1152 } | |
1153 idents.push(cast(void*)id); | |
1154 if (token.value == TOKidentifier) | |
1155 { | |
1156 id = token.ident; | |
1157 nextToken(); | |
1158 } | |
1159 else | |
1160 id = null; | |
1161 | |
1162 tm = new TemplateMixin(loc, id, tqual, idents, tiargs); | |
1163 if (token.value != TOKsemicolon) | |
1164 error("';' expected after mixin"); | |
1165 nextToken(); | |
1166 return tm; | |
0 | 1167 } |
1168 | |
1169 /****************************************** | |
1170 * Parse template argument list. | |
1171 * Input: | |
1172 * current token is opening '(' | |
1173 * Output: | |
1174 * current token is one after closing ')' | |
1175 */ | |
1176 Objects parseTemplateArgumentList() | |
1177 { | |
1178 //printf("Parser.parseTemplateArgumentList()\n"); | |
1179 if (token.value != TOKlparen && token.value != TOKlcurly) | |
1180 { | |
1181 error("!(TemplateArgumentList) expected following TemplateIdentifier"); | |
1182 return new Objects(); | |
1183 } | |
1184 return parseTemplateArgumentList2(); | |
1185 } | |
1186 | |
1187 Objects parseTemplateArgumentList2() | |
1188 { | |
1189 //printf("Parser.parseTemplateArgumentList2()\n"); | |
1190 Objects tiargs = new Objects(); | |
1191 TOK endtok = TOKrparen; | |
1192 nextToken(); | |
1193 | |
1194 // Get TemplateArgumentList | |
1195 if (token.value != endtok) | |
1196 { | |
1197 while (1) | |
1198 { | |
1199 // See if it is an Expression or a Type | |
1200 if (isDeclaration(&token, 0, TOKreserved, null)) | |
1201 { // Template argument is a type | |
1202 Type ta = parseType(); | |
1203 tiargs.push(cast(void*)ta); | |
1204 } | |
1205 else | |
1206 { // Template argument is an expression | |
1207 Expression ea = parseAssignExp(); | |
1208 | |
1209 if (ea.op == TOKfunction) | |
1210 { | |
1211 FuncLiteralDeclaration fd = (cast(FuncExp)ea).fd; | |
1212 if (fd.type.ty == Tfunction) | |
1213 { | |
1214 TypeFunction tf = cast(TypeFunction)fd.type; | |
1215 /* If there are parameters that consist of only an identifier, | |
1216 * rather than assuming the identifier is a type, as we would | |
1217 * for regular function declarations, assume the identifier | |
1218 * is the parameter name, and we're building a template with | |
1219 * a deduced type. | |
1220 */ | |
1221 TemplateParameters tpl = null; | |
1222 for (int i = 0; i < tf.parameters.dim; i++) | |
1223 { | |
1224 Argument param = cast(Argument)tf.parameters.data[i]; | |
1225 if (param.ident is null && | |
1226 param.type && | |
1227 param.type.ty == Tident && | |
1228 (cast(TypeIdentifier)param.type).idents.dim == 0 | |
1229 ) | |
1230 { | |
1231 /* Switch parameter type to parameter identifier, | |
1232 * parameterize with template type parameter _T | |
1233 */ | |
1234 TypeIdentifier pt = cast(TypeIdentifier)param.type; | |
1235 param.ident = pt.ident; | |
1236 Identifier id = Lexer.uniqueId("__T"); | |
1237 param.type = new TypeIdentifier(pt.loc, id); | |
1238 TemplateParameter tp = new TemplateTypeParameter(fd.loc, id, null, null); | |
1239 if (!tpl) | |
1240 tpl = new TemplateParameters(); | |
1241 tpl.push(cast(void*)tp); | |
1242 } | |
1243 } | |
1244 | |
1245 if (tpl) | |
1246 { | |
1247 // Wrap a template around function fd | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1248 auto decldefs = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1249 decldefs.push(fd); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1250 auto tempdecl = new TemplateDeclaration(fd.loc, fd.ident, tpl, null, decldefs); |
0 | 1251 tempdecl.literal = 1; // it's a template 'literal' |
1252 tiargs.push(cast(void*)tempdecl); | |
1253 goto L1; | |
1254 } | |
1255 } | |
1256 } | |
1257 | |
1258 tiargs.push(cast(void*)ea); | |
1259 } | |
1260 L1: | |
1261 if (token.value != TOKcomma) | |
1262 break; | |
1263 nextToken(); | |
1264 } | |
1265 } | |
1266 check(endtok, "template argument list"); | |
1267 return tiargs; | |
1268 } | |
1269 | |
2 | 1270 /***************************** |
1271 * Parse single template argument, to support the syntax: | |
1272 * foo!arg | |
1273 * Input: | |
1274 * current token is the arg | |
1275 */ | |
0 | 1276 Objects parseTemplateArgument() |
1277 { | |
2 | 1278 //printf("parseTemplateArgument()\n"); |
1279 Objects tiargs = new Objects(); | |
1280 Type ta; | |
1281 switch (token.value) | |
1282 { | |
1283 case TOKidentifier: | |
1284 ta = new TypeIdentifier(loc, token.ident); | |
1285 goto LabelX; | |
1286 | |
1287 case TOKvoid: ta = Type.tvoid; goto LabelX; | |
1288 case TOKint8: ta = Type.tint8; goto LabelX; | |
1289 case TOKuns8: ta = Type.tuns8; goto LabelX; | |
1290 case TOKint16: ta = Type.tint16; goto LabelX; | |
1291 case TOKuns16: ta = Type.tuns16; goto LabelX; | |
1292 case TOKint32: ta = Type.tint32; goto LabelX; | |
1293 case TOKuns32: ta = Type.tuns32; goto LabelX; | |
1294 case TOKint64: ta = Type.tint64; goto LabelX; | |
1295 case TOKuns64: ta = Type.tuns64; goto LabelX; | |
1296 case TOKfloat32: ta = Type.tfloat32; goto LabelX; | |
1297 case TOKfloat64: ta = Type.tfloat64; goto LabelX; | |
1298 case TOKfloat80: ta = Type.tfloat80; goto LabelX; | |
1299 case TOKimaginary32: ta = Type.timaginary32; goto LabelX; | |
1300 case TOKimaginary64: ta = Type.timaginary64; goto LabelX; | |
1301 case TOKimaginary80: ta = Type.timaginary80; goto LabelX; | |
1302 case TOKcomplex32: ta = Type.tcomplex32; goto LabelX; | |
1303 case TOKcomplex64: ta = Type.tcomplex64; goto LabelX; | |
1304 case TOKcomplex80: ta = Type.tcomplex80; goto LabelX; | |
1305 case TOKbit: ta = Type.tbit; goto LabelX; | |
1306 case TOKbool: ta = Type.tbool; goto LabelX; | |
1307 case TOKchar: ta = Type.tchar; goto LabelX; | |
1308 case TOKwchar: ta = Type.twchar; goto LabelX; | |
1309 case TOKdchar: ta = Type.tdchar; goto LabelX; | |
1310 LabelX: | |
1311 tiargs.push(cast(void*)ta); | |
1312 nextToken(); | |
1313 break; | |
1314 | |
1315 case TOKint32v: | |
1316 case TOKuns32v: | |
1317 case TOKint64v: | |
1318 case TOKuns64v: | |
1319 case TOKfloat32v: | |
1320 case TOKfloat64v: | |
1321 case TOKfloat80v: | |
1322 case TOKimaginary32v: | |
1323 case TOKimaginary64v: | |
1324 case TOKimaginary80v: | |
1325 case TOKnull: | |
1326 case TOKtrue: | |
1327 case TOKfalse: | |
1328 case TOKcharv: | |
1329 case TOKwcharv: | |
1330 case TOKdcharv: | |
1331 case TOKstring: | |
1332 case TOKfile: | |
1333 case TOKline: | |
1334 { | |
1335 // Template argument is an expression | |
1336 Expression ea = parsePrimaryExp(); | |
1337 tiargs.push(cast(void*)ea); | |
1338 break; | |
1339 } | |
1340 | |
1341 default: | |
1342 error("template argument expected following !"); | |
1343 break; | |
1344 } | |
1345 | |
1346 if (token.value == TOKnot) | |
1347 error("multiple ! arguments are not allowed"); | |
1348 return tiargs; | |
0 | 1349 } |
1350 | |
1351 /********************************** | |
1352 * Parse a static assertion. | |
1353 */ | |
1354 StaticAssert parseStaticAssert() | |
1355 { | |
1356 Loc loc = this.loc; | |
1357 Expression exp; | |
1358 Expression msg = null; | |
1359 | |
1360 //printf("parseStaticAssert()\n"); | |
1361 nextToken(); | |
1362 check(TOK.TOKlparen); | |
1363 exp = parseAssignExp(); | |
1364 if (token.value == TOK.TOKcomma) | |
1365 { | |
1366 nextToken(); | |
1367 msg = parseAssignExp(); | |
1368 } | |
1369 | |
1370 check(TOK.TOKrparen); | |
1371 check(TOK.TOKsemicolon); | |
1372 | |
1373 return new StaticAssert(loc, exp, msg); | |
1374 } | |
1375 | |
1376 TypeQualified parseTypeof() | |
1377 { | |
1378 TypeQualified t; | |
1379 Loc loc = this.loc; | |
1380 | |
1381 nextToken(); | |
1382 check(TOK.TOKlparen); | |
1383 if (token.value == TOK.TOKreturn) // typeof(return) | |
1384 { | |
1385 nextToken(); | |
1386 t = new TypeReturn(loc); | |
1387 } | |
1388 else | |
1389 { | |
1390 Expression exp = parseExpression(); // typeof(expression) | |
1391 t = new TypeTypeof(loc, exp); | |
1392 } | |
1393 check(TOK.TOKrparen); | |
1394 return t; | |
1395 } | |
1396 | |
1397 /*********************************** | |
1398 * Parse extern (linkage) | |
1399 * The parser is on the 'extern' token. | |
1400 */ | |
1401 LINK parseLinkage() | |
1402 { | |
1403 LINK link = LINK.LINKdefault; | |
1404 nextToken(); | |
1405 assert(token.value == TOK.TOKlparen); | |
1406 nextToken(); | |
1407 if (token.value == TOK.TOKidentifier) | |
1408 { | |
1409 Identifier id = token.ident; | |
1410 | |
1411 nextToken(); | |
1412 if (id == Id.Windows) | |
1413 link = LINK.LINKwindows; | |
1414 else if (id == Id.Pascal) | |
1415 link = LINK.LINKpascal; | |
1416 else if (id == Id.D) | |
1417 link = LINK.LINKd; | |
1418 else if (id == Id.C) | |
1419 { | |
1420 link = LINK.LINKc; | |
1421 if (token.value == TOK.TOKplusplus) | |
1422 { | |
1423 link = LINK.LINKcpp; | |
1424 nextToken(); | |
1425 } | |
1426 } | |
1427 else if (id == Id.System) | |
1428 { | |
1429 version (_WIN32) { | |
1430 link = LINK.LINKwindows; | |
1431 } else { | |
1432 link = LINK.LINKc; | |
1433 } | |
1434 } | |
1435 else | |
1436 { | |
1437 error("valid linkage identifiers are D, C, C++, Pascal, Windows, System"); | |
1438 link = LINK.LINKd; | |
1439 } | |
1440 } | |
1441 else | |
1442 { | |
1443 link = LINK.LINKd; // default | |
1444 } | |
1445 check(TOK.TOKrparen); | |
1446 | |
1447 return link; | |
1448 } | |
1449 | |
1450 | |
1451 /************************************** | |
1452 * Parse a debug conditional | |
1453 */ | |
1454 Condition parseDebugCondition() | |
1455 { | |
1456 Condition c; | |
1457 | |
1458 if (token.value == TOK.TOKlparen) | |
1459 { | |
1460 nextToken(); | |
1461 uint level = 1; | |
1462 Identifier id = null; | |
1463 | |
1464 if (token.value == TOK.TOKidentifier) | |
1465 id = token.ident; | |
1466 else if (token.value == TOK.TOKint32v) | |
1467 level = cast(uint)token.uns64value; | |
1468 else | |
1469 error("identifier or integer expected, not %s", token.toChars()); | |
1470 | |
1471 nextToken(); | |
1472 check(TOK.TOKrparen); | |
1473 | |
1474 c = new DebugCondition(mod, level, id); | |
1475 } | |
1476 else | |
1477 c = new DebugCondition(mod, 1, null); | |
1478 | |
1479 return c; | |
1480 } | |
1481 | |
1482 /************************************** | |
1483 * Parse a version conditional | |
1484 */ | |
1485 Condition parseVersionCondition() | |
1486 { | |
1487 Condition c; | |
1488 uint level = 1; | |
1489 Identifier id = null; | |
1490 | |
1491 if (token.value == TOK.TOKlparen) | |
1492 { | |
1493 nextToken(); | |
1494 if (token.value == TOK.TOKidentifier) | |
1495 id = token.ident; | |
1496 else if (token.value == TOK.TOKint32v) | |
1497 level = cast(uint)token.uns64value; | |
1498 else { | |
1499 version (DMDV2) { | |
1500 /* Allow: | |
1501 * version (unittest) | |
1502 * even though unittest is a keyword | |
1503 */ | |
1504 if (token.value == TOK.TOKunittest) | |
1505 id = Lexer.idPool(Token.toChars(TOK.TOKunittest)); | |
1506 else | |
1507 error("identifier or integer expected, not %s", token.toChars()); | |
1508 } else { | |
1509 error("identifier or integer expected, not %s", token.toChars()); | |
1510 } | |
1511 } | |
1512 nextToken(); | |
1513 check(TOK.TOKrparen); | |
1514 } | |
1515 else | |
1516 error("(condition) expected following version"); | |
1517 | |
1518 c = new VersionCondition(mod, level, id); | |
1519 | |
1520 return c; | |
1521 } | |
1522 | |
1523 /*********************************************** | |
1524 * static if (expression) | |
1525 * body | |
1526 * else | |
1527 * body | |
1528 */ | |
1529 Condition parseStaticIfCondition() | |
1530 { | |
1531 Expression exp; | |
1532 Condition condition; | |
1533 Array aif; | |
1534 Array aelse; | |
1535 Loc loc = this.loc; | |
1536 | |
1537 nextToken(); | |
1538 if (token.value == TOKlparen) | |
1539 { | |
1540 nextToken(); | |
1541 exp = parseAssignExp(); | |
1542 check(TOKrparen); | |
1543 } | |
1544 else | |
1545 { | |
1546 error("(expression) expected following static if"); | |
1547 exp = null; | |
1548 } | |
1549 condition = new StaticIfCondition(loc, exp); | |
1550 return condition; | |
1551 } | |
1552 | |
1553 /***************************************** | |
1554 * Parse a constructor definition: | |
1555 * this(parameters) { body } | |
1556 * or postblit: | |
1557 * this(this) { body } | |
1558 * or constructor template: | |
1559 * this(templateparameters)(parameters) { body } | |
1560 * Current token is 'this'. | |
1561 */ | |
1562 | |
1563 Dsymbol parseCtor() | |
1564 { | |
1565 Loc loc = this.loc; | |
1566 | |
1567 nextToken(); | |
1568 if (token.value == TOK.TOKlparen && peek(&token).value == TOK.TOKthis) | |
1569 { // this(this) { ... } | |
1570 nextToken(); | |
1571 nextToken(); | |
1572 check(TOK.TOKrparen); | |
1573 PostBlitDeclaration f = new PostBlitDeclaration(loc, Loc(0)); | |
1574 parseContracts(f); | |
1575 return f; | |
1576 } | |
1577 | |
1578 /* Look ahead to see if: | |
1579 * this(...)(...) | |
1580 * which is a constructor template | |
1581 */ | |
1582 TemplateParameters tpl = null; | |
1583 if (token.value == TOK.TOKlparen && peekPastParen(&token).value == TOK.TOKlparen) | |
1584 { tpl = parseTemplateParameterList(); | |
1585 | |
1586 int varargs; | |
1587 Arguments arguments = parseParameters(&varargs); | |
1588 | |
1589 Expression constraint = null; | |
1590 if (tpl) | |
1591 constraint = parseConstraint(); | |
1592 | |
1593 CtorDeclaration f = new CtorDeclaration(loc, Loc(0), arguments, varargs); | |
1594 parseContracts(f); | |
1595 | |
1596 // Wrap a template around it | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1597 auto decldefs = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1598 decldefs.push(f); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1599 auto tempdecl = new TemplateDeclaration(loc, f.ident, tpl, constraint, decldefs); |
0 | 1600 return tempdecl; |
1601 } | |
1602 | |
1603 /* Just a regular constructor | |
1604 */ | |
1605 int varargs; | |
1606 Arguments arguments = parseParameters(&varargs); | |
1607 CtorDeclaration f = new CtorDeclaration(loc, Loc(0), arguments, varargs); | |
1608 parseContracts(f); | |
1609 return f; | |
1610 } | |
1611 | |
1612 PostBlitDeclaration parsePostBlit() | |
1613 { | |
1614 assert(false); | |
1615 } | |
1616 | |
1617 /***************************************** | |
1618 * Parse a destructor definition: | |
1619 * ~this() { body } | |
1620 * Current token is '~'. | |
1621 */ | |
1622 DtorDeclaration parseDtor() | |
1623 { | |
1624 DtorDeclaration f; | |
1625 Loc loc = this.loc; | |
1626 | |
1627 nextToken(); | |
1628 check(TOKthis); | |
1629 check(TOKlparen); | |
1630 check(TOKrparen); | |
1631 | |
1632 f = new DtorDeclaration(loc, Loc(0)); | |
1633 parseContracts(f); | |
1634 return f; | |
1635 } | |
1636 | |
1637 /***************************************** | |
1638 * Parse a static constructor definition: | |
1639 * static this() { body } | |
1640 * Current token is 'this'. | |
1641 */ | |
1642 StaticCtorDeclaration parseStaticCtor() | |
1643 { | |
1644 StaticCtorDeclaration f; | |
1645 Loc loc = this.loc; | |
1646 | |
1647 nextToken(); | |
1648 check(TOKlparen); | |
1649 check(TOKrparen); | |
1650 | |
1651 f = new StaticCtorDeclaration(loc, Loc(0)); | |
1652 parseContracts(f); | |
1653 return f; | |
1654 } | |
1655 | |
1656 /***************************************** | |
1657 * Parse a static destructor definition: | |
1658 * static ~this() { body } | |
1659 * Current token is '~'. | |
1660 */ | |
1661 StaticDtorDeclaration parseStaticDtor() | |
1662 { | |
1663 StaticDtorDeclaration f; | |
1664 Loc loc = this.loc; | |
1665 | |
1666 nextToken(); | |
1667 check(TOKthis); | |
1668 check(TOKlparen); | |
1669 check(TOKrparen); | |
1670 | |
1671 f = new StaticDtorDeclaration(loc, Loc(0)); | |
1672 parseContracts(f); | |
1673 return f; | |
1674 } | |
1675 | |
1676 /***************************************** | |
1677 * Parse an invariant definition: | |
1678 * invariant() { body } | |
1679 * Current token is 'invariant'. | |
1680 */ | |
1681 InvariantDeclaration parseInvariant() | |
1682 { | |
1683 InvariantDeclaration f; | |
1684 Loc loc = this.loc; | |
1685 | |
1686 nextToken(); | |
1687 if (token.value == TOKlparen) // optional () | |
1688 { | |
1689 nextToken(); | |
1690 check(TOKrparen); | |
1691 } | |
1692 | |
1693 f = new InvariantDeclaration(loc, Loc(0)); | |
1694 f.fbody = parseStatement(ParseStatementFlags.PScurly); | |
1695 return f; | |
1696 } | |
1697 | |
1698 /***************************************** | |
1699 * Parse a unittest definition: | |
1700 * unittest { body } | |
1701 * Current token is 'unittest'. | |
1702 */ | |
1703 UnitTestDeclaration parseUnitTest() | |
1704 { | |
1705 Loc loc = this.loc; | |
1706 | |
1707 nextToken(); | |
1708 | |
1709 UnitTestDeclaration f = new UnitTestDeclaration(loc, this.loc); | |
1710 f.fbody = parseStatement(ParseStatementFlags.PScurly); | |
1711 | |
1712 return f; | |
1713 } | |
1714 | |
1715 NewDeclaration parseNew() | |
1716 { | |
1717 assert(false); | |
1718 } | |
1719 | |
1720 DeleteDeclaration parseDelete() | |
1721 { | |
1722 assert(false); | |
1723 } | |
1724 | |
1725 Arguments parseParameters(int* pvarargs) | |
1726 { | |
1727 Arguments arguments = new Arguments(); | |
1728 int varargs = 0; | |
1729 int hasdefault = 0; | |
1730 | |
1731 check(TOK.TOKlparen); | |
1732 while (1) | |
1733 { Type *tb; | |
1734 Identifier ai = null; | |
1735 Type at; | |
1736 Argument a; | |
1737 STC storageClass = STC.STCundefined; | |
1738 STC stc; | |
1739 Expression ae; | |
1740 | |
1741 for ( ;1; nextToken()) | |
1742 { | |
1743 switch (token.value) | |
1744 { | |
1745 case TOK.TOKrparen: | |
1746 break; | |
1747 | |
1748 case TOK.TOKdotdotdot: | |
1749 varargs = 1; | |
1750 nextToken(); | |
1751 break; | |
1752 | |
1753 case TOK.TOKconst: | |
1754 if (peek(&token).value == TOK.TOKlparen) | |
1755 goto Ldefault; | |
1756 stc = STC.STCconst; | |
1757 goto L2; | |
1758 | |
1759 case TOK.TOKinvariant: | |
1760 case TOK.TOKimmutable: | |
1761 if (peek(&token).value == TOK.TOKlparen) | |
1762 goto Ldefault; | |
1763 stc = STC.STCimmutable; | |
1764 goto L2; | |
1765 | |
1766 case TOK.TOKshared: | |
1767 if (peek(&token).value == TOK.TOKlparen) | |
1768 goto Ldefault; | |
1769 stc = STC.STCshared; | |
1770 goto L2; | |
1771 | |
1772 case TOK.TOKin: stc = STC.STCin; goto L2; | |
1773 case TOK.TOKout: stc = STC.STCout; goto L2; | |
1774 case TOK.TOKinout: | |
1775 case TOK.TOKref: stc = STC.STCref; goto L2; | |
1776 case TOK.TOKlazy: stc = STC.STClazy; goto L2; | |
1777 case TOK.TOKscope: stc = STC.STCscope; goto L2; | |
1778 case TOK.TOKfinal: stc = STC.STCfinal; goto L2; | |
1779 L2: | |
1780 if (storageClass & stc || | |
1781 (storageClass & STC.STCin && stc & (STC.STCconst | STC.STCscope)) || | |
1782 (stc & STC.STCin && storageClass & (STC.STCconst | STC.STCscope)) | |
1783 ) | |
1784 error("redundant storage class %s", Token.toChars(token.value)); | |
1785 storageClass |= stc; | |
1786 composeStorageClass(storageClass); | |
1787 continue; | |
1788 | |
1789 static if (false) { | |
1790 case TOK.TOKstatic: stc = STC.STCstatic; goto L2; | |
1791 case TOK.TOKauto: storageClass = STC.STCauto; goto L4; | |
1792 case TOK.TOKalias: storageClass = STC.STCalias; goto L4; | |
1793 L4: | |
1794 nextToken(); | |
1795 if (token.value == TOK.TOKidentifier) | |
1796 { ai = token.ident; | |
1797 nextToken(); | |
1798 } | |
1799 else | |
1800 ai = null; | |
1801 at = null; // no type | |
1802 ae = null; // no default argument | |
1803 if (token.value == TOK.TOKassign) // = defaultArg | |
1804 { nextToken(); | |
1805 ae = parseDefaultInitExp(); | |
1806 hasdefault = 1; | |
1807 } | |
1808 else | |
1809 { if (hasdefault) | |
1810 error("default argument expected for alias %s", | |
1811 ai ? ai.toChars() : ""); | |
1812 } | |
1813 goto L3; | |
1814 } | |
1815 | |
1816 default: | |
1817 Ldefault: | |
1818 stc = (storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STClazy)); | |
1819 if (stc & (stc - 1)) // if stc is not a power of 2 | |
1820 error("incompatible parameter storage classes"); | |
1821 if ((storageClass & (STC.STCconst | STC.STCout)) == (STC.STCconst | STC.STCout)) | |
1822 error("out cannot be const"); | |
1823 if ((storageClass & (STC.STCimmutable | STC.STCout)) == (STC.STCimmutable | STC.STCout)) | |
1824 error("out cannot be immutable"); | |
1825 if ((storageClass & STC.STCscope) && | |
1826 (storageClass & (STC.STCref | STC.STCout))) | |
1827 error("scope cannot be ref or out"); | |
1828 at = parseType(&ai); | |
1829 ae = null; | |
1830 if (token.value == TOK.TOKassign) // = defaultArg | |
1831 { nextToken(); | |
1832 ae = parseDefaultInitExp(); | |
1833 hasdefault = 1; | |
1834 } | |
1835 else | |
1836 { if (hasdefault) | |
1837 error("default argument expected for %s", | |
1838 ai ? ai.toChars() : at.toChars()); | |
1839 } | |
1840 if (token.value == TOK.TOKdotdotdot) | |
1841 { /* This is: | |
1842 * at ai ... | |
1843 */ | |
1844 | |
1845 if (storageClass & (STC.STCout | STC.STCref)) | |
1846 error("variadic argument cannot be out or ref"); | |
1847 varargs = 2; | |
1848 a = new Argument(storageClass, at, ai, ae); | |
1849 arguments.push(cast(void*)a); | |
1850 nextToken(); | |
1851 break; | |
1852 } | |
1853 L3: | |
1854 a = new Argument(storageClass, at, ai, ae); | |
1855 arguments.push(cast(void*)a); | |
1856 if (token.value == TOK.TOKcomma) | |
1857 { nextToken(); | |
1858 goto L1; | |
1859 } | |
1860 break; | |
1861 } | |
1862 break; | |
1863 } | |
1864 break; | |
1865 | |
1866 L1: ; | |
1867 } | |
1868 check(TOK.TOKrparen); | |
1869 *pvarargs = varargs; | |
1870 return arguments; | |
1871 } | |
1872 | |
1873 EnumDeclaration parseEnum() | |
1874 { | |
1875 EnumDeclaration e; | |
1876 Identifier id; | |
1877 Type memtype; | |
1878 Loc loc = this.loc; | |
1879 | |
1880 //printf("Parser.parseEnum()\n"); | |
1881 nextToken(); | |
1882 if (token.value == TOK.TOKidentifier) | |
1883 { | |
1884 id = token.ident; | |
1885 nextToken(); | |
1886 } | |
1887 else | |
1888 id = null; | |
1889 | |
1890 if (token.value == TOK.TOKcolon) | |
1891 { | |
1892 nextToken(); | |
1893 memtype = parseBasicType(); | |
1894 memtype = parseDeclarator(memtype, null, null); | |
1895 } | |
1896 else | |
1897 memtype = null; | |
1898 | |
1899 e = new EnumDeclaration(loc, id, memtype); | |
1900 if (token.value == TOK.TOKsemicolon && id) | |
1901 nextToken(); | |
1902 else if (token.value == TOK.TOKlcurly) | |
1903 { | |
1904 //printf("enum definition\n"); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1905 e.members = new Dsymbols(); |
0 | 1906 nextToken(); |
1907 ubyte* comment = token.blockComment; | |
1908 while (token.value != TOK.TOKrcurly) | |
1909 { | |
1910 /* Can take the following forms: | |
1911 * 1. ident | |
1912 * 2. ident = value | |
1913 * 3. type ident = value | |
1914 */ | |
1915 | |
1916 loc = this.loc; | |
1917 | |
1918 Type type = null; | |
1919 Identifier ident; | |
1920 Token* tp = peek(&token); | |
1921 if (token.value == TOK.TOKidentifier && | |
1922 (tp.value == TOK.TOKassign || tp.value == TOK.TOKcomma || tp.value == TOK.TOKrcurly)) | |
1923 { | |
1924 ident = token.ident; | |
1925 type = null; | |
1926 nextToken(); | |
1927 } | |
1928 else | |
1929 { | |
1930 type = parseType(&ident, null); | |
1931 if (id || memtype) | |
1932 error("type only allowed if anonymous enum and no enum type"); | |
1933 } | |
1934 | |
1935 Expression value; | |
1936 if (token.value == TOK.TOKassign) | |
1937 { | |
1938 nextToken(); | |
1939 value = parseAssignExp(); | |
1940 } | |
1941 else | |
1942 { | |
1943 value = null; | |
1944 if (type) | |
1945 error("if type, there must be an initializer"); | |
1946 } | |
1947 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1948 auto em = new EnumMember(loc, ident, value, type); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
1949 e.members.push(em); |
0 | 1950 |
1951 if (token.value == TOK.TOKrcurly) { | |
1952 ; | |
1953 } else { | |
1954 addComment(em, comment); | |
1955 comment = null; | |
1956 check(TOK.TOKcomma); | |
1957 } | |
1958 addComment(em, comment); | |
1959 comment = token.blockComment; | |
1960 } | |
1961 nextToken(); | |
1962 } | |
1963 else | |
1964 error("enum declaration is invalid"); | |
1965 | |
1966 //printf("-parseEnum() %s\n", e.toChars()); | |
1967 return e; | |
1968 } | |
1969 | |
1970 Dsymbol parseAggregate() | |
1971 { | |
1972 AggregateDeclaration a = null; | |
1973 int anon = 0; | |
1974 TOK tok; | |
1975 Identifier id; | |
1976 TemplateParameters tpl = null; | |
1977 Expression constraint = null; | |
1978 | |
1979 //printf("Parser.parseAggregate()\n"); | |
1980 tok = token.value; | |
1981 nextToken(); | |
1982 if (token.value != TOK.TOKidentifier) | |
1983 { | |
1984 id = null; | |
1985 } | |
1986 else | |
1987 { | |
1988 id = token.ident; | |
1989 nextToken(); | |
1990 | |
1991 if (token.value == TOK.TOKlparen) | |
1992 { | |
1993 // Class template declaration. | |
1994 | |
1995 // Gather template parameter list | |
1996 tpl = parseTemplateParameterList(); | |
1997 constraint = parseConstraint(); | |
1998 } | |
1999 } | |
2000 | |
2001 Loc loc = this.loc; | |
2002 switch (tok) | |
2003 { case TOK.TOKclass: | |
2004 case TOK.TOKinterface: | |
2005 { | |
2006 if (!id) | |
2007 error("anonymous classes not allowed"); | |
2008 | |
2009 // Collect base class(es) | |
2010 BaseClasses baseclasses = null; | |
2011 if (token.value == TOK.TOKcolon) | |
2012 { | |
2013 nextToken(); | |
2014 baseclasses = parseBaseClasses(); | |
2015 | |
2016 if (token.value != TOK.TOKlcurly) | |
2017 error("members expected"); | |
2018 } | |
2019 | |
2020 if (tok == TOK.TOKclass) | |
2021 a = new ClassDeclaration(loc, id, baseclasses); | |
2022 else | |
2023 a = new InterfaceDeclaration(loc, id, baseclasses); | |
2024 break; | |
2025 } | |
2026 | |
2027 case TOK.TOKstruct: | |
2028 if (id) | |
2029 a = new StructDeclaration(loc, id); | |
2030 else | |
2031 anon = 1; | |
2032 break; | |
2033 | |
2034 case TOK.TOKunion: | |
2035 if (id) | |
2036 a = new UnionDeclaration(loc, id); | |
2037 else | |
2038 anon = 2; | |
2039 break; | |
2040 | |
2041 default: | |
2042 assert(0); | |
2043 break; | |
2044 } | |
2045 if (a && token.value == TOK.TOKsemicolon) | |
2046 { nextToken(); | |
2047 } | |
2048 else if (token.value == TOK.TOKlcurly) | |
2049 { | |
2050 //printf("aggregate definition\n"); | |
2051 nextToken(); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2052 auto decl = parseDeclDefs(0); |
0 | 2053 if (token.value != TOK.TOKrcurly) |
2054 error("} expected following member declarations in aggregate"); | |
2055 nextToken(); | |
2056 if (anon) | |
2057 { | |
2058 /* Anonymous structs/unions are more like attributes. | |
2059 */ | |
2060 return new AnonDeclaration(loc, anon - 1, decl); | |
2061 } | |
2062 else | |
2063 a.members = decl; | |
2064 } | |
2065 else | |
2066 { | |
2067 error("{ } expected following aggregate declaration"); | |
2068 a = new StructDeclaration(loc, null); | |
2069 } | |
2070 | |
2071 if (tpl) | |
2072 { // Wrap a template around the aggregate declaration | |
2073 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2074 auto decldefs = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2075 decldefs.push(a); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2076 auto tempdecl = new TemplateDeclaration(loc, id, tpl, constraint, decldefs); |
0 | 2077 return tempdecl; |
2078 } | |
2079 | |
2080 return a; | |
2081 } | |
2082 | |
2083 BaseClasses parseBaseClasses() | |
2084 { | |
2085 BaseClasses baseclasses = new BaseClasses(); | |
2086 | |
2087 for (; 1; nextToken()) | |
2088 { | |
2089 PROT protection = PROT.PROTpublic; | |
2090 switch (token.value) | |
2091 { | |
2092 case TOK.TOKprivate: | |
2093 protection = PROT.PROTprivate; | |
2094 nextToken(); | |
2095 break; | |
2096 case TOK.TOKpackage: | |
2097 protection = PROT.PROTpackage; | |
2098 nextToken(); | |
2099 break; | |
2100 case TOK.TOKprotected: | |
2101 protection = PROT.PROTprotected; | |
2102 nextToken(); | |
2103 break; | |
2104 case TOK.TOKpublic: | |
2105 protection = PROT.PROTpublic; | |
2106 nextToken(); | |
2107 break; | |
2108 default: | |
2109 break; /// | |
2110 } | |
2111 if (token.value == TOK.TOKidentifier) | |
2112 { | |
2113 BaseClass b = new BaseClass(parseBasicType(), protection); | |
2114 baseclasses.push(cast(void*)b); | |
2115 if (token.value != TOK.TOKcomma) | |
2116 break; | |
2117 } | |
2118 else | |
2119 { | |
2120 error("base classes expected instead of %s", token.toChars()); | |
2121 return null; | |
2122 } | |
2123 } | |
2124 return baseclasses; | |
2125 } | |
2126 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2127 Import parseImport(Dsymbols decldefs, int isstatic) |
0 | 2128 { |
2129 Import s; | |
2130 Identifier id; | |
2131 Identifier aliasid = null; | |
2132 Array a; | |
2133 Loc loc; | |
2134 | |
2135 //printf("Parser.parseImport()\n"); | |
2136 do | |
2137 { | |
2138 L1: | |
2139 nextToken(); | |
2140 if (token.value != TOK.TOKidentifier) | |
2141 { | |
2142 error("Identifier expected following import"); | |
2143 break; | |
2144 } | |
2145 | |
2146 loc = this.loc; | |
2147 a = null; | |
2148 id = token.ident; | |
2149 nextToken(); | |
2150 if (!aliasid && token.value == TOK.TOKassign) | |
2151 { | |
2152 aliasid = id; | |
2153 goto L1; | |
2154 } | |
2155 while (token.value == TOK.TOKdot) | |
2156 { | |
2157 if (!a) | |
2158 a = new Array(); | |
2159 a.push(cast(void*)id); | |
2160 nextToken(); | |
2161 if (token.value != TOK.TOKidentifier) | |
2162 { | |
2163 error("identifier expected following package"); | |
2164 break; | |
2165 } | |
2166 id = token.ident; | |
2167 nextToken(); | |
2168 } | |
2169 | |
2170 s = new Import(loc, a, id, aliasid, isstatic); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2171 decldefs.push(s); |
0 | 2172 |
2173 /* Look for | |
2174 * : alias=name, alias=name; | |
2175 * syntax. | |
2176 */ | |
2177 if (token.value == TOK.TOKcolon) | |
2178 { | |
2179 do | |
2180 { | |
2181 Identifier name; | |
2182 | |
2183 nextToken(); | |
2184 if (token.value != TOK.TOKidentifier) | |
2185 { | |
2186 error("Identifier expected following :"); | |
2187 break; | |
2188 } | |
2189 Identifier alias_ = token.ident; | |
2190 nextToken(); | |
2191 if (token.value == TOK.TOKassign) | |
2192 { | |
2193 nextToken(); | |
2194 if (token.value != TOK.TOKidentifier) | |
2195 { | |
2196 error("Identifier expected following %s=", alias_.toChars()); | |
2197 break; | |
2198 } | |
2199 name = token.ident; | |
2200 nextToken(); | |
2201 } | |
2202 else | |
2203 { | |
2204 name = alias_; | |
2205 alias_ = null; | |
2206 } | |
2207 s.addAlias(name, alias_); | |
2208 } while (token.value == TOK.TOKcomma); | |
2209 | |
2210 break; // no comma-separated imports of this form | |
2211 } | |
2212 | |
2213 aliasid = null; | |
2214 | |
2215 } while (token.value == TOK.TOKcomma); | |
2216 | |
2217 if (token.value == TOK.TOKsemicolon) | |
2218 nextToken(); | |
2219 else | |
2220 { | |
2221 error("';' expected"); | |
2222 nextToken(); | |
2223 } | |
2224 | |
2225 return null; | |
2226 } | |
2227 | |
2228 Type parseType(Identifier* pident = null, TemplateParameters* tpl = null) | |
2229 { | |
2230 Type t; | |
2231 | |
2232 /* Take care of the storage class prefixes that | |
2233 * serve as type attributes: | |
2234 * const shared, shared const, const, invariant, shared | |
2235 */ | |
2236 if (token.value == TOK.TOKconst && peekNext() == TOK.TOKshared && peekNext2() != TOK.TOKlparen || | |
2237 token.value == TOK.TOKshared && peekNext() == TOK.TOKconst && peekNext2() != TOK.TOKlparen) | |
2238 { | |
2239 nextToken(); | |
2240 nextToken(); | |
2241 /* shared const type | |
2242 */ | |
2243 t = parseType(pident, tpl); | |
2244 t = t.makeSharedConst(); | |
2245 return t; | |
2246 } | |
2247 else if (token.value == TOK.TOKconst && peekNext() != TOK.TOKlparen) | |
2248 { | |
2249 nextToken(); | |
2250 /* const type | |
2251 */ | |
2252 t = parseType(pident, tpl); | |
2253 t = t.makeConst(); | |
2254 return t; | |
2255 } | |
2256 else if ((token.value == TOK.TOKinvariant || token.value == TOK.TOKimmutable) && | |
2257 peekNext() != TOK.TOKlparen) | |
2258 { | |
2259 nextToken(); | |
2260 /* invariant type | |
2261 */ | |
2262 t = parseType(pident, tpl); | |
2263 t = t.makeInvariant(); | |
2264 return t; | |
2265 } | |
2266 else if (token.value == TOK.TOKshared && peekNext() != TOK.TOKlparen) | |
2267 { | |
2268 nextToken(); | |
2269 /* shared type | |
2270 */ | |
2271 t = parseType(pident, tpl); | |
2272 t = t.makeShared(); | |
2273 return t; | |
2274 } | |
2275 else | |
2276 t = parseBasicType(); | |
2277 t = parseDeclarator(t, pident, tpl); | |
2278 return t; | |
2279 } | |
2280 | |
2281 Type parseBasicType() | |
2282 { | |
2283 Type t; | |
2284 Identifier id; | |
2285 TypeQualified tid; | |
2286 | |
2287 //printf("parseBasicType()\n"); | |
2288 switch (token.value) | |
2289 { | |
2290 case TOK.TOKvoid: t = Type.tvoid; goto LabelX; | |
2291 case TOK.TOKint8: t = Type.tint8; goto LabelX; | |
2292 case TOK.TOKuns8: t = Type.tuns8; goto LabelX; | |
2293 case TOK.TOKint16: t = Type.tint16; goto LabelX; | |
2294 case TOK.TOKuns16: t = Type.tuns16; goto LabelX; | |
2295 case TOK.TOKint32: t = Type.tint32; goto LabelX; | |
2296 case TOK.TOKuns32: t = Type.tuns32; goto LabelX; | |
2297 case TOK.TOKint64: t = Type.tint64; goto LabelX; | |
2298 case TOK.TOKuns64: t = Type.tuns64; goto LabelX; | |
2299 case TOK.TOKfloat32: t = Type.tfloat32; goto LabelX; | |
2300 case TOK.TOKfloat64: t = Type.tfloat64; goto LabelX; | |
2301 case TOK.TOKfloat80: t = Type.tfloat80; goto LabelX; | |
2302 case TOK.TOKimaginary32: t = Type.timaginary32; goto LabelX; | |
2303 case TOK.TOKimaginary64: t = Type.timaginary64; goto LabelX; | |
2304 case TOK.TOKimaginary80: t = Type.timaginary80; goto LabelX; | |
2305 case TOK.TOKcomplex32: t = Type.tcomplex32; goto LabelX; | |
2306 case TOK.TOKcomplex64: t = Type.tcomplex64; goto LabelX; | |
2307 case TOK.TOKcomplex80: t = Type.tcomplex80; goto LabelX; | |
2308 case TOK.TOKbit: t = Type.tbit; goto LabelX; | |
2309 case TOK.TOKbool: t = Type.tbool; goto LabelX; | |
2310 case TOK.TOKchar: t = Type.tchar; goto LabelX; | |
2311 case TOK.TOKwchar: t = Type.twchar; goto LabelX; | |
2312 case TOK.TOKdchar: t = Type.tdchar; goto LabelX; | |
2313 LabelX: | |
2314 nextToken(); | |
2315 break; | |
2316 | |
2317 case TOK.TOKidentifier: | |
2318 id = token.ident; | |
2319 nextToken(); | |
2320 if (token.value == TOK.TOKnot) | |
2321 { // ident!(template_arguments) | |
2322 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
2323 nextToken(); | |
2324 if (token.value == TOK.TOKlparen) | |
2325 // ident!(template_arguments) | |
2326 tempinst.tiargs = parseTemplateArgumentList(); | |
2327 else | |
2328 // ident!template_argument | |
2329 tempinst.tiargs = parseTemplateArgument(); | |
2330 tid = new TypeInstance(loc, tempinst); | |
2331 goto Lident2; | |
2332 } | |
2333 Lident: | |
2334 tid = new TypeIdentifier(loc, id); | |
2335 Lident2: | |
2336 while (token.value == TOK.TOKdot) | |
2337 { nextToken(); | |
2338 if (token.value != TOK.TOKidentifier) | |
2339 { error("identifier expected following '.' instead of '%s'", token.toChars()); | |
2340 break; | |
2341 } | |
2342 id = token.ident; | |
2343 nextToken(); | |
2344 if (token.value == TOK.TOKnot) | |
2345 { | |
2346 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
2347 nextToken(); | |
2348 if (token.value == TOK.TOKlparen) | |
2349 // ident!(template_arguments) | |
2350 tempinst.tiargs = parseTemplateArgumentList(); | |
2351 else | |
2352 // ident!template_argument | |
2353 tempinst.tiargs = parseTemplateArgument(); | |
56 | 2354 tid.addIdent(tempinst); |
0 | 2355 } |
2356 else | |
2357 tid.addIdent(id); | |
2358 } | |
2359 t = tid; | |
2360 break; | |
2361 | |
2362 case TOK.TOKdot: | |
2363 // Leading . as in .foo | |
2364 id = Id.empty; | |
2365 goto Lident; | |
2366 | |
2367 case TOK.TOKtypeof: | |
2368 // typeof(expression) | |
2369 tid = parseTypeof(); | |
2370 goto Lident2; | |
2371 | |
2372 case TOK.TOKconst: | |
2373 // const(type) | |
2374 nextToken(); | |
2375 check(TOK.TOKlparen); | |
2376 t = parseType(); | |
2377 check(TOK.TOKrparen); | |
2378 if (t.isShared()) | |
2379 t = t.makeSharedConst(); | |
2380 else | |
2381 t = t.makeConst(); | |
2382 break; | |
2383 | |
2384 case TOK.TOKinvariant: | |
2385 case TOK.TOKimmutable: | |
2386 // invariant(type) | |
2387 nextToken(); | |
2388 check(TOK.TOKlparen); | |
2389 t = parseType(); | |
2390 check(TOK.TOKrparen); | |
2391 t = t.makeInvariant(); | |
2392 break; | |
2393 | |
2394 case TOK.TOKshared: | |
2395 // shared(type) | |
2396 nextToken(); | |
2397 check(TOK.TOKlparen); | |
2398 t = parseType(); | |
2399 check(TOK.TOKrparen); | |
2400 if (t.isConst()) | |
2401 t = t.makeSharedConst(); | |
2402 else | |
2403 t = t.makeShared(); | |
2404 break; | |
2405 | |
2406 default: | |
2407 error("basic type expected, not %s", token.toChars()); | |
2408 t = Type.tint32; | |
2409 break; | |
2410 } | |
2411 return t; | |
2412 } | |
2413 | |
2414 Type parseBasicType2(Type t) | |
2415 { | |
2416 //printf("parseBasicType2()\n"); | |
2417 while (1) | |
2418 { | |
2419 switch (token.value) | |
2420 { | |
2421 case TOK.TOKmul: | |
2422 t = new TypePointer(t); | |
2423 nextToken(); | |
2424 continue; | |
2425 | |
2426 case TOK.TOKlbracket: | |
2427 // Handle []. Make sure things like | |
2428 // int[3][1] a; | |
2429 // is (array[1] of array[3] of int) | |
2430 nextToken(); | |
2431 if (token.value == TOK.TOKrbracket) | |
2432 { | |
2433 t = new TypeDArray(t); // [] | |
2434 nextToken(); | |
2435 } | |
2436 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) | |
2437 { // It's an associative array declaration | |
2438 | |
2439 //printf("it's an associative array\n"); | |
2440 Type index = parseType(); // [ type ] | |
2441 t = new TypeAArray(t, index); | |
2442 check(TOK.TOKrbracket); | |
2443 } | |
2444 else | |
2445 { | |
2446 //printf("it's type[expression]\n"); | |
2447 inBrackets++; | |
2448 Expression e = parseExpression(); // [ expression ] | |
2449 if (token.value == TOK.TOKslice) | |
2450 { | |
2451 nextToken(); | |
2452 Expression e2 = parseExpression(); // [ exp .. exp ] | |
2453 t = new TypeSlice(t, e, e2); | |
2454 } | |
2455 else | |
2456 t = new TypeSArray(t,e); | |
2457 inBrackets--; | |
2458 check(TOK.TOKrbracket); | |
2459 } | |
2460 continue; | |
2461 | |
2462 case TOK.TOKdelegate: | |
2463 case TOK.TOKfunction: | |
2464 { // Handle delegate declaration: | |
2465 // t delegate(parameter list) nothrow pure | |
2466 // t function(parameter list) nothrow pure | |
2467 Arguments arguments; | |
2468 int varargs; | |
2469 bool ispure = false; | |
2470 bool isnothrow = false; | |
2471 TOK save = token.value; | |
2472 | |
2473 nextToken(); | |
2474 arguments = parseParameters(&varargs); | |
2475 while (1) | |
2476 { // Postfixes | |
2477 if (token.value == TOK.TOKpure) | |
2478 ispure = true; | |
2479 else if (token.value == TOK.TOKnothrow) | |
2480 isnothrow = true; | |
2481 else | |
2482 break; | |
2483 nextToken(); | |
2484 } | |
2485 TypeFunction tf = new TypeFunction(arguments, t, varargs, linkage); | |
2486 tf.ispure = ispure; | |
2487 tf.isnothrow = isnothrow; | |
2488 if (save == TOK.TOKdelegate) | |
2489 t = new TypeDelegate(tf); | |
2490 else | |
2491 t = new TypePointer(tf); // pointer to function | |
2492 continue; | |
2493 } | |
2494 | |
2495 default: | |
2496 return t; | |
2497 } | |
2498 assert(0); | |
2499 } | |
2500 assert(0); | |
2501 return null; | |
2502 } | |
2503 | |
2504 Type parseDeclarator(Type t, Identifier* pident, TemplateParameters* tpl = null) | |
2505 { | |
2506 Type ts; | |
2507 | |
2508 //printf("parseDeclarator(tpl = %p)\n", tpl); | |
2509 t = parseBasicType2(t); | |
2510 | |
2511 switch (token.value) | |
2512 { | |
2513 | |
2514 case TOK.TOKidentifier: | |
2515 if (pident) | |
2516 *pident = token.ident; | |
2517 else | |
2518 error("unexpected identifer '%s' in declarator", token.ident.toChars()); | |
2519 ts = t; | |
2520 nextToken(); | |
2521 break; | |
2522 | |
2523 case TOK.TOKlparen: | |
2524 /* Parse things with parentheses around the identifier, like: | |
2525 * int (*ident[3])[] | |
2526 * although the D style would be: | |
2527 * int[]*[3] ident | |
2528 */ | |
2529 nextToken(); | |
2530 ts = parseDeclarator(t, pident); | |
2531 check(TOK.TOKrparen); | |
2532 break; | |
2533 | |
2534 default: | |
2535 ts = t; | |
2536 break; | |
2537 } | |
2538 | |
2539 // parse DeclaratorSuffixes | |
2540 while (1) | |
2541 { | |
2542 switch (token.value) | |
2543 { | |
2544 version (CARRAYDECL) { | |
2545 /* Support C style array syntax: | |
2546 * int ident[] | |
2547 * as opposed to D-style: | |
2548 * int[] ident | |
2549 */ | |
2550 case TOK.TOKlbracket: | |
2551 { // This is the old C-style post [] syntax. | |
2552 TypeNext ta; | |
2553 nextToken(); | |
2554 if (token.value == TOK.TOKrbracket) | |
2555 { // It's a dynamic array | |
2556 ta = new TypeDArray(t); // [] | |
2557 nextToken(); | |
2558 } | |
2559 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) | |
2560 { // It's an associative array | |
2561 | |
2562 //printf("it's an associative array\n"); | |
2563 Type index = parseType(); // [ type ] | |
2564 check(TOK.TOKrbracket); | |
2565 ta = new TypeAArray(t, index); | |
2566 } | |
2567 else | |
2568 { | |
2569 //printf("It's a static array\n"); | |
2570 Expression e = parseExpression(); // [ expression ] | |
2571 ta = new TypeSArray(t, e); | |
2572 check(TOK.TOKrbracket); | |
2573 } | |
2574 | |
2575 /* Insert ta into | |
2576 * ts . ... . t | |
2577 * so that | |
2578 * ts . ... . ta . t | |
2579 */ | |
2580 Type* pt; | |
2581 for (pt = &ts; *pt !is t; pt = &(cast(TypeNext)*pt).next) { | |
2582 ; | |
2583 } | |
2584 *pt = ta; | |
2585 continue; | |
2586 } | |
2587 } | |
2588 case TOK.TOKlparen: | |
2589 { | |
2590 if (tpl) | |
2591 { | |
2592 /* Look ahead to see if this is (...)(...), | |
2593 * i.e. a function template declaration | |
2594 */ | |
2595 if (peekPastParen(&token).value == TOK.TOKlparen) | |
2596 { | |
2597 //printf("function template declaration\n"); | |
2598 | |
2599 // Gather template parameter list | |
2600 *tpl = parseTemplateParameterList(); | |
2601 } | |
2602 } | |
2603 | |
2604 int varargs; | |
2605 Arguments arguments = parseParameters(&varargs); | |
2606 Type tf = new TypeFunction(arguments, t, varargs, linkage); | |
2607 | |
2608 /* Parse const/invariant/nothrow/pure postfix | |
2609 */ | |
2610 while (1) | |
2611 { | |
2612 switch (token.value) | |
2613 { | |
2614 case TOK.TOKconst: | |
2615 if (tf.isShared()) | |
2616 tf = tf.makeSharedConst(); | |
2617 else | |
2618 tf = tf.makeConst(); | |
2619 nextToken(); | |
2620 continue; | |
2621 | |
2622 case TOK.TOKinvariant: | |
2623 case TOK.TOKimmutable: | |
2624 tf = tf.makeInvariant(); | |
2625 nextToken(); | |
2626 continue; | |
2627 | |
2628 case TOK.TOKshared: | |
2629 if (tf.isConst()) | |
2630 tf = tf.makeSharedConst(); | |
2631 else | |
2632 tf = tf.makeShared(); | |
2633 nextToken(); | |
2634 continue; | |
2635 | |
2636 case TOK.TOKnothrow: | |
2637 (cast(TypeFunction)tf).isnothrow = 1; | |
2638 nextToken(); | |
2639 continue; | |
2640 | |
2641 case TOK.TOKpure: | |
2642 (cast(TypeFunction)tf).ispure = 1; | |
2643 nextToken(); | |
2644 continue; | |
2645 | |
2646 case TOK.TOKat: | |
2647 nextToken(); | |
2648 if (token.value != TOK.TOKidentifier) | |
2649 { error("attribute identifier expected"); | |
2650 nextToken(); | |
2651 continue; | |
2652 } | |
2653 Identifier id = token.ident; | |
2654 if (id is Id.property) | |
2655 (cast(TypeFunction)tf).ispure = 1; | |
2656 else | |
2657 error("valid attribute identifiers are property, not %s", id.toChars()); | |
2658 nextToken(); | |
2659 continue; | |
2660 default: | |
2661 break; /// | |
2662 } | |
2663 break; | |
2664 } | |
2665 | |
2666 /* Insert tf into | |
2667 * ts . ... . t | |
2668 * so that | |
2669 * ts . ... . tf . t | |
2670 */ | |
2671 Type* pt; | |
2672 for (pt = &ts; *pt !is t; pt = &(cast(TypeNext)*pt).next) { | |
2673 ; | |
2674 } | |
2675 *pt = tf; | |
2676 break; | |
2677 } | |
2678 | |
2679 default: | |
2680 break; /// | |
2681 } | |
2682 break; | |
2683 } | |
2684 | |
2685 return ts; | |
2686 } | |
2687 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2688 Dsymbols parseDeclarations(STC storage_class) |
0 | 2689 { |
2690 STC stc; | |
2691 Type ts; | |
2692 Type t; | |
2693 Type tfirst; | |
2694 Identifier ident; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2695 Dsymbols a; |
0 | 2696 TOK tok = TOK.TOKreserved; |
2697 ubyte* comment = token.blockComment; | |
2698 LINK link = linkage; | |
2699 | |
2700 //printf("parseDeclarations() %s\n", token.toChars()); | |
2701 if (storage_class) | |
2702 { ts = null; // infer type | |
2703 goto L2; | |
2704 } | |
2705 | |
2706 switch (token.value) | |
2707 { | |
2708 case TOK.TOKalias: | |
2709 /* Look for: | |
2710 * alias identifier this; | |
2711 */ | |
2712 tok = token.value; | |
2713 nextToken(); | |
2714 if (token.value == TOK.TOKidentifier && peek(&token).value == TOK.TOKthis) | |
2715 { | |
2716 AliasThis s = new AliasThis(this.loc, token.ident); | |
2717 nextToken(); | |
2718 check(TOK.TOKthis); | |
2719 check(TOK.TOKsemicolon); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2720 a = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2721 a.push(s); |
0 | 2722 addComment(s, comment); |
2723 return a; | |
2724 } | |
2725 break; | |
2726 case TOK.TOKtypedef: | |
2727 tok = token.value; | |
2728 nextToken(); | |
2729 break; | |
2730 default: | |
2731 break; | |
2732 } | |
2733 | |
2734 storage_class = STC.STCundefined; | |
2735 while (1) | |
2736 { | |
2737 switch (token.value) | |
2738 { | |
2739 case TOK.TOKconst: | |
2740 if (peek(&token).value == TOK.TOKlparen) | |
2741 break; // const as type constructor | |
2742 stc = STC.STCconst; // const as storage class | |
2743 goto L1; | |
2744 | |
2745 case TOK.TOKinvariant: | |
2746 case TOK.TOKimmutable: | |
2747 if (peek(&token).value == TOK.TOKlparen) | |
2748 break; | |
2749 stc = STC.STCimmutable; | |
2750 goto L1; | |
2751 | |
2752 case TOK.TOKshared: | |
2753 if (peek(&token).value == TOK.TOKlparen) | |
2754 break; | |
2755 stc = STC.STCshared; | |
2756 goto L1; | |
2757 | |
2758 case TOK.TOKstatic: stc = STC.STCstatic; goto L1; | |
2759 case TOK.TOKfinal: stc = STC.STCfinal; goto L1; | |
2760 case TOK.TOKauto: stc = STC.STCauto; goto L1; | |
2761 case TOK.TOKscope: stc = STC.STCscope; goto L1; | |
2762 case TOK.TOKoverride: stc = STC.STCoverride; goto L1; | |
2763 case TOK.TOKabstract: stc = STC.STCabstract; goto L1; | |
2764 case TOK.TOKsynchronized: stc = STC.STCsynchronized; goto L1; | |
2765 case TOK.TOKdeprecated: stc = STC.STCdeprecated; goto L1; | |
2766 version (DMDV2) { | |
2767 case TOK.TOKnothrow: stc = STC.STCnothrow; goto L1; | |
2768 case TOK.TOKpure: stc = STC.STCpure; goto L1; | |
2769 case TOK.TOKref: stc = STC.STCref; goto L1; | |
2770 case TOK.TOKtls: stc = STC.STCtls; goto L1; | |
2771 case TOK.TOKgshared: stc = STC.STCgshared; goto L1; | |
2772 case TOK.TOKenum: stc = STC.STCmanifest; goto L1; | |
2773 } | |
2774 L1: | |
2775 if (storage_class & stc) | |
2776 error("redundant storage class '%s'", token.toChars()); | |
2777 storage_class = (storage_class | stc); | |
2778 composeStorageClass(storage_class); | |
2779 nextToken(); | |
2780 continue; | |
2781 | |
2782 case TOK.TOKextern: | |
2783 if (peek(&token).value != TOK.TOKlparen) | |
2784 { stc = STC.STCextern; | |
2785 goto L1; | |
2786 } | |
2787 | |
2788 link = parseLinkage(); | |
2789 continue; | |
2790 | |
2791 default: | |
2792 break; | |
2793 } | |
2794 break; | |
2795 } | |
2796 | |
2797 /* Look for auto initializers: | |
2798 * storage_class identifier = initializer; | |
2799 */ | |
2800 if (storage_class && | |
2801 token.value == TOK.TOKidentifier && | |
2802 peek(&token).value == TOK.TOKassign) | |
2803 { | |
2804 return parseAutoDeclarations(storage_class, comment); | |
2805 } | |
2806 | |
2807 if (token.value == TOK.TOKclass) | |
2808 { | |
2809 AggregateDeclaration s = cast(AggregateDeclaration)parseAggregate(); | |
2810 s.storage_class |= storage_class; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2811 a = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2812 a.push(s); |
0 | 2813 addComment(s, comment); |
2814 return a; | |
2815 } | |
2816 | |
2817 /* Look for return type inference for template functions. | |
2818 */ | |
2819 { | |
2820 Token *tk; | |
2821 if (storage_class && | |
2822 token.value == TOK.TOKidentifier && | |
2823 (tk = peek(&token)).value == TOK.TOKlparen && | |
2824 skipParens(tk, &tk) && | |
2825 peek(tk).value == TOK.TOKlparen) | |
2826 { | |
2827 ts = null; | |
2828 } | |
2829 else | |
2830 { | |
2831 ts = parseBasicType(); | |
2832 ts = parseBasicType2(ts); | |
2833 } | |
2834 } | |
2835 | |
2836 L2: | |
2837 tfirst = null; | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2838 a = new Dsymbols(); |
0 | 2839 |
2840 while (1) | |
2841 { | |
2842 Loc loc = this.loc; | |
2843 TemplateParameters tpl = null; | |
2844 | |
2845 ident = null; | |
2846 t = parseDeclarator(ts, &ident, &tpl); | |
2847 assert(t); | |
2848 if (!tfirst) | |
2849 tfirst = t; | |
2850 else if (t != tfirst) | |
2851 error("multiple declarations must have the same type, not %s and %s", | |
2852 tfirst.toChars(), t.toChars()); | |
2853 if (!ident) | |
2854 error("no identifier for declarator %s", t.toChars()); | |
2855 | |
2856 if (tok == TOK.TOKtypedef || tok == TOK.TOKalias) | |
2857 { Declaration v; | |
2858 Initializer init = null; | |
2859 | |
2860 if (token.value == TOK.TOKassign) | |
2861 { | |
2862 nextToken(); | |
2863 init = parseInitializer(); | |
2864 } | |
2865 if (tok == TOK.TOKtypedef) | |
2866 v = new TypedefDeclaration(loc, ident, t, init); | |
2867 else | |
2868 { if (init) | |
2869 error("alias cannot have initializer"); | |
2870 v = new AliasDeclaration(loc, ident, t); | |
2871 } | |
2872 v.storage_class = storage_class; | |
2873 if (link == linkage) | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2874 a.push(v); |
0 | 2875 else |
2876 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2877 auto ax = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2878 ax.push(v); |
0 | 2879 Dsymbol s = new LinkDeclaration(link, ax); |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2880 a.push(s); |
0 | 2881 } |
2882 switch (token.value) | |
2883 { case TOK.TOKsemicolon: | |
2884 nextToken(); | |
2885 addComment(v, comment); | |
2886 break; | |
2887 | |
2888 case TOK.TOKcomma: | |
2889 nextToken(); | |
2890 addComment(v, comment); | |
2891 continue; | |
2892 | |
2893 default: | |
2894 error("semicolon expected to close %s declaration", Token.toChars(tok)); | |
2895 break; | |
2896 } | |
2897 } | |
2898 else if (t.ty == TY.Tfunction) | |
2899 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2900 auto tf = cast(TypeFunction)t; |
0 | 2901 Expression constraint = null; |
2902 static if (false) { | |
2903 if (Argument.isTPL(tf.parameters)) | |
2904 { | |
2905 if (!tpl) | |
2906 tpl = new TemplateParameters(); | |
2907 } | |
2908 } | |
2909 FuncDeclaration f = | |
2910 new FuncDeclaration(loc, Loc(0), ident, storage_class, t); | |
2911 addComment(f, comment); | |
2912 if (tpl) | |
2913 constraint = parseConstraint(); | |
2914 parseContracts(f); | |
2915 addComment(f, null); | |
2916 Dsymbol s; | |
2917 if (link == linkage) | |
2918 { | |
2919 s = f; | |
2920 } | |
2921 else | |
2922 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2923 auto ax = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2924 ax.push(f); |
0 | 2925 s = new LinkDeclaration(link, ax); |
2926 } | |
2927 /* A template parameter list means it's a function template | |
2928 */ | |
2929 if (tpl) | |
2930 { | |
2931 // Wrap a template around the function declaration | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2932 auto decldefs = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2933 decldefs.push(s); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2934 auto tempdecl = |
0 | 2935 new TemplateDeclaration(loc, s.ident, tpl, constraint, decldefs); |
2936 s = tempdecl; | |
2937 } | |
2938 addComment(s, comment); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2939 a.push(s); |
0 | 2940 } |
2941 else | |
2942 { | |
2943 Initializer init = null; | |
2944 if (token.value == TOK.TOKassign) | |
2945 { | |
2946 nextToken(); | |
2947 init = parseInitializer(); | |
2948 } | |
2949 | |
2950 VarDeclaration v = new VarDeclaration(loc, t, ident, init); | |
2951 v.storage_class = storage_class; | |
2952 if (link == linkage) | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2953 a.push(v); |
0 | 2954 else |
2955 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2956 auto ax = new Dsymbols(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2957 ax.push(v); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2958 auto s = new LinkDeclaration(link, ax); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
2959 a.push(s); |
0 | 2960 } |
2961 switch (token.value) | |
2962 { case TOK.TOKsemicolon: | |
2963 nextToken(); | |
2964 addComment(v, comment); | |
2965 break; | |
2966 | |
2967 case TOK.TOKcomma: | |
2968 nextToken(); | |
2969 addComment(v, comment); | |
2970 continue; | |
2971 | |
2972 default: | |
2973 error("semicolon expected, not '%s'", token.toChars()); | |
2974 break; | |
2975 } | |
2976 } | |
2977 break; | |
2978 } | |
2979 return a; | |
2980 } | |
2981 | |
2982 void parseContracts(FuncDeclaration f) | |
2983 { | |
2984 Type tb; | |
2985 LINK linksave = linkage; | |
2986 | |
2987 // The following is irrelevant, as it is overridden by sc.linkage in | |
2988 // TypeFunction.semantic | |
2989 linkage = LINK.LINKd; // nested functions have D linkage | |
2990 L1: | |
2991 switch (token.value) | |
2992 { | |
2993 case TOK.TOKlcurly: | |
2994 if (f.frequire || f.fensure) | |
2995 error("missing body { ... } after in or out"); | |
2996 f.fbody = parseStatement(ParseStatementFlags.PSsemi); | |
2997 f.endloc = endloc; | |
2998 break; | |
2999 | |
3000 case TOK.TOKbody: | |
3001 nextToken(); | |
3002 f.fbody = parseStatement(ParseStatementFlags.PScurly); | |
3003 f.endloc = endloc; | |
3004 break; | |
3005 | |
3006 case TOK.TOKsemicolon: | |
3007 if (f.frequire || f.fensure) | |
3008 error("missing body { ... } after in or out"); | |
3009 nextToken(); | |
3010 break; | |
3011 | |
3012 static if (false) { // Do we want this for function declarations, so we can do: | |
3013 // int x, y, foo(), z; | |
3014 case TOK.TOKcomma: | |
3015 nextToken(); | |
3016 continue; | |
3017 } | |
3018 | |
3019 static if (false) { // Dumped feature | |
3020 case TOK.TOKthrow: | |
3021 if (!f.fthrows) | |
3022 f.fthrows = new Array(); | |
3023 nextToken(); | |
3024 check(TOK.TOKlparen); | |
3025 while (1) | |
3026 { | |
3027 tb = parseBasicType(); | |
3028 f.fthrows.push(tb); | |
3029 if (token.value == TOK.TOKcomma) | |
3030 { nextToken(); | |
3031 continue; | |
3032 } | |
3033 break; | |
3034 } | |
3035 check(TOK.TOKrparen); | |
3036 goto L1; | |
3037 } | |
3038 | |
3039 case TOK.TOKin: | |
3040 nextToken(); | |
3041 if (f.frequire) | |
3042 error("redundant 'in' statement"); | |
3043 f.frequire = parseStatement(ParseStatementFlags.PScurly | ParseStatementFlags.PSscope); | |
3044 goto L1; | |
3045 | |
3046 case TOK.TOKout: | |
3047 // parse: out (identifier) { statement } | |
3048 nextToken(); | |
3049 if (token.value != TOK.TOKlcurly) | |
3050 { | |
3051 check(TOK.TOKlparen); | |
3052 if (token.value != TOK.TOKidentifier) | |
3053 error("(identifier) following 'out' expected, not %s", token.toChars()); | |
3054 f.outId = token.ident; | |
3055 nextToken(); | |
3056 check(TOK.TOKrparen); | |
3057 } | |
3058 if (f.fensure) | |
3059 error("redundant 'out' statement"); | |
3060 f.fensure = parseStatement(ParseStatementFlags.PScurly | ParseStatementFlags.PSscope); | |
3061 goto L1; | |
3062 | |
3063 default: | |
3064 error("semicolon expected following function declaration"); | |
3065 break; | |
3066 } | |
3067 linkage = linksave; | |
3068 } | |
3069 | |
3070 Statement parseStatement(ParseStatementFlags flags) | |
3071 { | |
3072 Statement s; | |
3073 Token* t; | |
3074 Condition condition; | |
3075 Statement ifbody; | |
3076 Statement elsebody; | |
3077 bool isfinal; | |
3078 Loc loc = this.loc; | |
3079 | |
3080 //printf("parseStatement()\n"); | |
3081 | |
3082 if (flags & ParseStatementFlags.PScurly && token.value != TOK.TOKlcurly) | |
3083 error("statement expected to be { }, not %s", token.toChars()); | |
3084 | |
3085 switch (token.value) | |
3086 { | |
3087 case TOK.TOKidentifier: | |
3088 /* A leading identifier can be a declaration, label, or expression. | |
3089 * The easiest case to check first is label: | |
3090 */ | |
3091 t = peek(&token); | |
3092 if (t.value == TOK.TOKcolon) | |
3093 { // It's a label | |
3094 | |
3095 Identifier ident = token.ident; | |
3096 nextToken(); | |
3097 nextToken(); | |
3098 s = parseStatement(ParseStatementFlags.PSsemi); | |
3099 s = new LabelStatement(loc, ident, s); | |
3100 break; | |
3101 } | |
3102 // fallthrough to TOK.TOKdot | |
3103 case TOK.TOKdot: | |
3104 case TOK.TOKtypeof: | |
3105 if (isDeclaration(&token, 2, TOK.TOKreserved, null)) | |
3106 goto Ldeclaration; | |
3107 else | |
3108 goto Lexp; | |
3109 break; | |
3110 | |
3111 case TOK.TOKassert: | |
3112 case TOK.TOKthis: | |
3113 case TOK.TOKsuper: | |
3114 case TOK.TOKint32v: | |
3115 case TOK.TOKuns32v: | |
3116 case TOK.TOKint64v: | |
3117 case TOK.TOKuns64v: | |
3118 case TOK.TOKfloat32v: | |
3119 case TOK.TOKfloat64v: | |
3120 case TOK.TOKfloat80v: | |
3121 case TOK.TOKimaginary32v: | |
3122 case TOK.TOKimaginary64v: | |
3123 case TOK.TOKimaginary80v: | |
3124 case TOK.TOKcharv: | |
3125 case TOK.TOKwcharv: | |
3126 case TOK.TOKdcharv: | |
3127 case TOK.TOKnull: | |
3128 case TOK.TOKtrue: | |
3129 case TOK.TOKfalse: | |
3130 case TOK.TOKstring: | |
3131 case TOK.TOKlparen: | |
3132 case TOK.TOKcast: | |
3133 case TOK.TOKmul: | |
3134 case TOK.TOKmin: | |
3135 case TOK.TOKadd: | |
3136 case TOK.TOKplusplus: | |
3137 case TOK.TOKminusminus: | |
3138 case TOK.TOKnew: | |
3139 case TOK.TOKdelete: | |
3140 case TOK.TOKdelegate: | |
3141 case TOK.TOKfunction: | |
3142 case TOK.TOKtypeid: | |
3143 case TOK.TOKis: | |
3144 case TOK.TOKlbracket: | |
3145 version (DMDV2) { | |
3146 case TOK.TOKtraits: | |
3147 case TOK.TOKfile: | |
3148 case TOK.TOKline: | |
3149 } | |
3150 Lexp: | |
3151 { Expression exp; | |
3152 | |
3153 exp = parseExpression(); | |
3154 check(TOK.TOKsemicolon, "statement"); | |
3155 s = new ExpStatement(loc, exp); | |
3156 break; | |
3157 } | |
3158 | |
3159 case TOK.TOKstatic: | |
3160 { // Look ahead to see if it's static assert() or static if() | |
3161 Token *tt; | |
3162 | |
3163 tt = peek(&token); | |
3164 if (tt.value == TOK.TOKassert) | |
3165 { | |
3166 nextToken(); | |
3167 s = new StaticAssertStatement(parseStaticAssert()); | |
3168 break; | |
3169 } | |
3170 if (tt.value == TOK.TOKif) | |
3171 { | |
3172 nextToken(); | |
3173 condition = parseStaticIfCondition(); | |
3174 goto Lcondition; | |
3175 } | |
3176 goto Ldeclaration; | |
3177 } | |
3178 | |
3179 case TOK.TOKfinal: | |
3180 if (peekNext() == TOK.TOKswitch) | |
3181 { | |
3182 nextToken(); | |
3183 isfinal = true; | |
3184 goto Lswitch; | |
3185 } | |
3186 goto Ldeclaration; | |
3187 | |
3188 case TOK.TOKwchar: case TOK.TOKdchar: | |
3189 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
3190 case TOK.TOKint8: case TOK.TOKuns8: | |
3191 case TOK.TOKint16: case TOK.TOKuns16: | |
3192 case TOK.TOKint32: case TOK.TOKuns32: | |
3193 case TOK.TOKint64: case TOK.TOKuns64: | |
3194 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
3195 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
3196 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
3197 case TOK.TOKvoid: | |
3198 case TOK.TOKtypedef: | |
3199 case TOK.TOKalias: | |
3200 case TOK.TOKconst: | |
3201 case TOK.TOKauto: | |
3202 case TOK.TOKextern: | |
3203 case TOK.TOKinvariant: | |
3204 version (DMDV2) { | |
3205 case TOK.TOKimmutable: | |
3206 case TOK.TOKshared: | |
3207 case TOK.TOKnothrow: | |
3208 case TOK.TOKpure: | |
3209 case TOK.TOKtls: | |
3210 case TOK.TOKgshared: | |
3211 } | |
3212 // case TOK.TOKtypeof: | |
3213 Ldeclaration: | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
3214 { Dsymbols a; |
0 | 3215 |
3216 a = parseDeclarations(STC.STCundefined); | |
3217 if (a.dim > 1) | |
3218 { | |
3219 Statements as = new Statements(); | |
3220 as.reserve(a.dim); | |
3221 for (int i = 0; i < a.dim; i++) | |
3222 { | |
3223 Dsymbol d = cast(Dsymbol)a.data[i]; | |
3224 s = new DeclarationStatement(loc, d); | |
3225 as.push(cast(void*)s); | |
3226 } | |
3227 s = new CompoundDeclarationStatement(loc, as); | |
3228 } | |
3229 else if (a.dim == 1) | |
3230 { | |
3231 Dsymbol d = cast(Dsymbol)a.data[0]; | |
3232 s = new DeclarationStatement(loc, d); | |
3233 } | |
3234 else | |
3235 assert(0); | |
3236 if (flags & ParseStatementFlags.PSscope) | |
3237 s = new ScopeStatement(loc, s); | |
3238 break; | |
3239 } | |
3240 | |
3241 case TOK.TOKstruct: | |
3242 case TOK.TOKunion: | |
3243 case TOK.TOKclass: | |
3244 case TOK.TOKinterface: | |
3245 { Dsymbol d; | |
3246 | |
3247 d = parseAggregate(); | |
3248 s = new DeclarationStatement(loc, d); | |
3249 break; | |
3250 } | |
3251 | |
3252 case TOK.TOKenum: | |
3253 { /* Determine if this is a manifest constant declaration, | |
3254 * or a conventional enum. | |
3255 */ | |
3256 Dsymbol d; | |
3257 Token* tt = peek(&token); | |
3258 if (tt.value == TOK.TOKlcurly || tt.value == TOK.TOKcolon) | |
3259 d = parseEnum(); | |
3260 else if (tt.value != TOK.TOKidentifier) | |
3261 goto Ldeclaration; | |
3262 else | |
3263 { | |
3264 tt = peek(tt); | |
3265 if (tt.value == TOK.TOKlcurly || tt.value == TOK.TOKcolon || | |
3266 tt.value == TOK.TOKsemicolon) | |
3267 d = parseEnum(); | |
3268 else | |
3269 goto Ldeclaration; | |
3270 } | |
3271 s = new DeclarationStatement(loc, d); | |
3272 break; | |
3273 } | |
3274 | |
3275 case TOK.TOKmixin: | |
3276 { t = peek(&token); | |
3277 if (t.value == TOK.TOKlparen) | |
3278 { // mixin(string) | |
3279 nextToken(); | |
3280 check(TOK.TOKlparen, "mixin"); | |
3281 Expression e = parseAssignExp(); | |
3282 check(TOK.TOKrparen); | |
3283 check(TOK.TOKsemicolon); | |
3284 s = new CompileStatement(loc, e); | |
3285 break; | |
3286 } | |
3287 Dsymbol d = parseMixin(); | |
3288 s = new DeclarationStatement(loc, d); | |
3289 break; | |
3290 } | |
3291 | |
3292 case TOK.TOKlcurly: | |
3293 { Statements statements; | |
3294 | |
3295 nextToken(); | |
3296 statements = new Statements(); | |
3297 while (token.value != TOK.TOKrcurly) | |
3298 { | |
3299 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); | |
3300 } | |
3301 endloc = this.loc; | |
3302 s = new CompoundStatement(loc, statements); | |
3303 if (flags & (ParseStatementFlags.PSscope | ParseStatementFlags.PScurlyscope)) | |
3304 s = new ScopeStatement(loc, s); | |
3305 nextToken(); | |
3306 break; | |
3307 } | |
3308 | |
3309 case TOK.TOKwhile: | |
3310 { Expression condition2; | |
3311 Statement body_; | |
3312 | |
3313 nextToken(); | |
3314 check(TOK.TOKlparen); | |
3315 condition2 = parseExpression(); | |
3316 check(TOK.TOKrparen); | |
3317 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3318 s = new WhileStatement(loc, condition2, body_); | |
3319 break; | |
3320 } | |
3321 | |
3322 case TOK.TOKsemicolon: | |
3323 if (!(flags & ParseStatementFlags.PSsemi)) | |
3324 error("use '{ }' for an empty statement, not a ';'"); | |
3325 nextToken(); | |
3326 s = new ExpStatement(loc, null); | |
3327 break; | |
3328 | |
3329 case TOK.TOKdo: | |
3330 { Statement body_; | |
3331 Expression condition2; | |
3332 | |
3333 nextToken(); | |
3334 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3335 check(TOK.TOKwhile); | |
3336 check(TOK.TOKlparen); | |
3337 condition2 = parseExpression(); | |
3338 check(TOK.TOKrparen); | |
3339 s = new DoStatement(loc, body_, condition2); | |
3340 break; | |
3341 } | |
3342 | |
3343 case TOK.TOKfor: | |
3344 { | |
3345 Statement init; | |
3346 Expression condition2; | |
3347 Expression increment; | |
3348 Statement body_; | |
3349 | |
3350 nextToken(); | |
3351 check(TOK.TOKlparen); | |
3352 if (token.value == TOK.TOKsemicolon) | |
3353 { init = null; | |
3354 nextToken(); | |
3355 } | |
3356 else | |
3357 { init = parseStatement(cast(ParseStatementFlags)0); | |
3358 } | |
3359 if (token.value == TOK.TOKsemicolon) | |
3360 { | |
3361 condition2 = null; | |
3362 nextToken(); | |
3363 } | |
3364 else | |
3365 { | |
3366 condition2 = parseExpression(); | |
3367 check(TOK.TOKsemicolon, "for condition"); | |
3368 } | |
3369 if (token.value == TOK.TOKrparen) | |
3370 { increment = null; | |
3371 nextToken(); | |
3372 } | |
3373 else | |
3374 { increment = parseExpression(); | |
3375 check(TOK.TOKrparen); | |
3376 } | |
3377 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3378 s = new ForStatement(loc, init, condition2, increment, body_); | |
3379 if (init) | |
3380 s = new ScopeStatement(loc, s); | |
3381 break; | |
3382 } | |
3383 | |
3384 case TOK.TOKforeach: | |
3385 case TOK.TOKforeach_reverse: | |
3386 { | |
3387 TOK op = token.value; | |
3388 Arguments arguments; | |
3389 | |
3390 Statement d; | |
3391 Statement body_; | |
3392 Expression aggr; | |
3393 | |
3394 nextToken(); | |
3395 check(TOK.TOKlparen); | |
3396 | |
3397 arguments = new Arguments(); | |
3398 | |
3399 while (1) | |
3400 { | |
3401 Type tb; | |
3402 Identifier ai = null; | |
3403 Type at; | |
3404 STC storageClass = STC.STCundefined; | |
3405 Argument a; | |
3406 | |
3407 if (token.value == TOK.TOKinout || token.value == TOK.TOKref) | |
3408 { storageClass = STC.STCref; | |
3409 nextToken(); | |
3410 } | |
3411 if (token.value == TOK.TOKidentifier) | |
3412 { | |
3413 Token *tt = peek(&token); | |
3414 if (tt.value == TOK.TOKcomma || tt.value == TOK.TOKsemicolon) | |
3415 { ai = token.ident; | |
3416 at = null; // infer argument type | |
3417 nextToken(); | |
3418 goto Larg; | |
3419 } | |
3420 } | |
3421 at = parseType(&ai); | |
3422 if (!ai) | |
3423 error("no identifier for declarator %s", at.toChars()); | |
3424 Larg: | |
3425 a = new Argument(storageClass, at, ai, null); | |
3426 arguments.push(cast(void*)a); | |
3427 if (token.value == TOK.TOKcomma) | |
3428 { nextToken(); | |
3429 continue; | |
3430 } | |
3431 break; | |
3432 } | |
3433 check(TOK.TOKsemicolon); | |
3434 | |
3435 aggr = parseExpression(); | |
3436 if (token.value == TOK.TOKslice && arguments.dim == 1) | |
3437 { | |
3438 Argument a = cast(Argument)arguments.data[0]; | |
3439 delete arguments; | |
3440 nextToken(); | |
3441 Expression upr = parseExpression(); | |
3442 check(TOK.TOKrparen); | |
3443 body_ = parseStatement(cast(ParseStatementFlags)0); | |
3444 s = new ForeachRangeStatement(loc, op, a, aggr, upr, body_); | |
3445 } | |
3446 else | |
3447 { | |
3448 check(TOK.TOKrparen); | |
3449 body_ = parseStatement(cast(ParseStatementFlags)0); | |
3450 s = new ForeachStatement(loc, op, arguments, aggr, body_); | |
3451 } | |
3452 break; | |
3453 } | |
3454 | |
3455 case TOK.TOKif: | |
3456 { Argument arg = null; | |
3457 Expression condition2; | |
3458 Statement ifbody2; | |
3459 Statement elsebody2; | |
3460 | |
3461 nextToken(); | |
3462 check(TOK.TOKlparen); | |
3463 | |
3464 if (token.value == TOK.TOKauto) | |
3465 { | |
3466 nextToken(); | |
3467 if (token.value == TOK.TOKidentifier) | |
3468 { | |
3469 Token *tt = peek(&token); | |
3470 if (tt.value == TOK.TOKassign) | |
3471 { | |
3472 arg = new Argument(STC.STCundefined, null, token.ident, null); | |
3473 nextToken(); | |
3474 nextToken(); | |
3475 } | |
3476 else | |
3477 { error("= expected following auto identifier"); | |
3478 goto Lerror; | |
3479 } | |
3480 } | |
3481 else | |
3482 { error("identifier expected following auto"); | |
3483 goto Lerror; | |
3484 } | |
3485 } | |
3486 else if (isDeclaration(&token, 2, TOK.TOKassign, null)) | |
3487 { | |
3488 Type at; | |
3489 Identifier ai; | |
3490 | |
3491 at = parseType(&ai); | |
3492 check(TOK.TOKassign); | |
3493 arg = new Argument(STC.STCundefined, at, ai, null); | |
3494 } | |
3495 | |
3496 // Check for " ident;" | |
3497 else if (token.value == TOK.TOKidentifier) | |
3498 { | |
3499 Token *tt = peek(&token); | |
3500 if (tt.value == TOK.TOKcomma || tt.value == TOK.TOKsemicolon) | |
3501 { | |
3502 arg = new Argument(STC.STCundefined, null, token.ident, null); | |
3503 nextToken(); | |
3504 nextToken(); | |
3505 if (1 || !global.params.useDeprecated) | |
3506 error("if (v; e) is deprecated, use if (auto v = e)"); | |
3507 } | |
3508 } | |
3509 | |
3510 condition2 = parseExpression(); | |
3511 check(TOK.TOKrparen); | |
3512 ifbody2 = parseStatement(ParseStatementFlags.PSscope); | |
3513 if (token.value == TOK.TOKelse) | |
3514 { | |
3515 nextToken(); | |
3516 elsebody2 = parseStatement(ParseStatementFlags.PSscope); | |
3517 } | |
3518 else | |
3519 elsebody2 = null; | |
3520 s = new IfStatement(loc, arg, condition2, ifbody2, elsebody2); | |
3521 break; | |
3522 } | |
3523 | |
3524 case TOK.TOKscope: | |
3525 if (peek(&token).value != TOK.TOKlparen) | |
3526 goto Ldeclaration; // scope used as storage class | |
3527 nextToken(); | |
3528 check(TOK.TOKlparen); | |
3529 if (token.value != TOK.TOKidentifier) | |
3530 { error("scope identifier expected"); | |
3531 goto Lerror; | |
3532 } | |
3533 else | |
3534 { TOK tt = TOK.TOKon_scope_exit; | |
3535 Identifier id = token.ident; | |
3536 | |
3537 if (id == Id.exit) | |
3538 tt = TOK.TOKon_scope_exit; | |
3539 else if (id == Id.failure) | |
3540 tt = TOK.TOKon_scope_failure; | |
3541 else if (id == Id.success) | |
3542 tt = TOK.TOKon_scope_success; | |
3543 else | |
3544 error("valid scope identifiers are exit, failure, or success, not %s", id.toChars()); | |
3545 nextToken(); | |
3546 check(TOK.TOKrparen); | |
3547 Statement st = parseStatement(ParseStatementFlags.PScurlyscope); | |
3548 s = new OnScopeStatement(loc, tt, st); | |
3549 break; | |
3550 } | |
3551 | |
3552 case TOK.TOKdebug: | |
3553 nextToken(); | |
3554 condition = parseDebugCondition(); | |
3555 goto Lcondition; | |
3556 | |
3557 case TOK.TOKversion: | |
3558 nextToken(); | |
3559 condition = parseVersionCondition(); | |
3560 goto Lcondition; | |
3561 | |
3562 Lcondition: | |
3563 ifbody = parseStatement(cast(ParseStatementFlags)0 /*ParseStatementFlags.PSsemi*/); | |
3564 elsebody = null; | |
3565 if (token.value == TOK.TOKelse) | |
3566 { | |
3567 nextToken(); | |
3568 elsebody = parseStatement(cast(ParseStatementFlags)0 /*ParseStatementFlags.PSsemi*/); | |
3569 } | |
3570 s = new ConditionalStatement(loc, condition, ifbody, elsebody); | |
3571 break; | |
3572 | |
3573 case TOK.TOKpragma: | |
3574 { Identifier ident; | |
3575 Expressions args = null; | |
3576 Statement body_; | |
3577 | |
3578 nextToken(); | |
3579 check(TOK.TOKlparen); | |
3580 if (token.value != TOK.TOKidentifier) | |
3581 { error("pragma(identifier expected"); | |
3582 goto Lerror; | |
3583 } | |
3584 ident = token.ident; | |
3585 nextToken(); | |
3586 if (token.value == TOK.TOKcomma && peekNext() != TOK.TOKrparen) | |
3587 args = parseArguments(); // pragma(identifier, args...); | |
3588 else | |
3589 check(TOK.TOKrparen); // pragma(identifier); | |
3590 if (token.value == TOK.TOKsemicolon) | |
3591 { nextToken(); | |
3592 body_ = null; | |
3593 } | |
3594 else | |
3595 body_ = parseStatement(ParseStatementFlags.PSsemi); | |
3596 s = new PragmaStatement(loc, ident, args, body_); | |
3597 break; | |
3598 } | |
3599 | |
3600 case TOK.TOKswitch: | |
3601 isfinal = false; | |
3602 goto Lswitch; | |
3603 | |
3604 Lswitch: | |
3605 { | |
3606 nextToken(); | |
3607 check(TOK.TOKlparen); | |
3608 Expression condition2 = parseExpression(); | |
3609 check(TOK.TOKrparen); | |
3610 Statement body_ = parseStatement(ParseStatementFlags.PSscope); | |
3611 s = new SwitchStatement(loc, condition2, body_, isfinal); | |
3612 break; | |
3613 } | |
3614 | |
3615 case TOK.TOKcase: | |
3616 { Expression exp; | |
3617 Statements statements; | |
3618 scope Array cases = new Array(); // array of Expression's | |
3619 Expression last = null; | |
3620 | |
3621 while (1) | |
3622 { | |
3623 nextToken(); | |
3624 exp = parseAssignExp(); | |
3625 cases.push(cast(void*)exp); | |
3626 if (token.value != TOK.TOKcomma) | |
3627 break; | |
3628 } | |
3629 check(TOK.TOKcolon); | |
3630 | |
3631 version (DMDV2) { | |
3632 /* case exp: .. case last: | |
3633 */ | |
3634 if (token.value == TOK.TOKslice) | |
3635 { | |
3636 if (cases.dim > 1) | |
3637 error("only one case allowed for start of case range"); | |
3638 nextToken(); | |
3639 check(TOK.TOKcase); | |
3640 last = parseAssignExp(); | |
3641 check(TOK.TOKcolon); | |
3642 } | |
3643 } | |
3644 | |
3645 statements = new Statements(); | |
3646 while (token.value != TOK.TOKcase && | |
3647 token.value != TOK.TOKdefault && | |
3648 token.value != TOK.TOKrcurly) | |
3649 { | |
3650 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); | |
3651 } | |
3652 s = new CompoundStatement(loc, statements); | |
3653 s = new ScopeStatement(loc, s); | |
3654 | |
3655 ///version (DMDV2) { | |
3656 if (last) | |
3657 { | |
3658 s = new CaseRangeStatement(loc, exp, last, s); | |
3659 } | |
3660 else | |
3661 ///} | |
3662 { | |
3663 // Keep cases in order by building the case statements backwards | |
3664 for (int i = cases.dim; i; i--) | |
3665 { | |
3666 exp = cast(Expression)cases.data[i - 1]; | |
3667 s = new CaseStatement(loc, exp, s); | |
3668 } | |
3669 } | |
3670 break; | |
3671 } | |
3672 | |
3673 case TOK.TOKdefault: | |
3674 { | |
3675 Statements statements; | |
3676 | |
3677 nextToken(); | |
3678 check(TOK.TOKcolon); | |
3679 | |
3680 statements = new Statements(); | |
3681 while (token.value != TOK.TOKcase && | |
3682 token.value != TOK.TOKdefault && | |
3683 token.value != TOK.TOKrcurly) | |
3684 { | |
3685 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); | |
3686 } | |
3687 s = new CompoundStatement(loc, statements); | |
3688 s = new ScopeStatement(loc, s); | |
3689 s = new DefaultStatement(loc, s); | |
3690 break; | |
3691 } | |
3692 | |
3693 case TOK.TOKreturn: | |
3694 { Expression exp; | |
3695 | |
3696 nextToken(); | |
3697 if (token.value == TOK.TOKsemicolon) | |
3698 exp = null; | |
3699 else | |
3700 exp = parseExpression(); | |
3701 check(TOK.TOKsemicolon, "return statement"); | |
3702 s = new ReturnStatement(loc, exp); | |
3703 break; | |
3704 } | |
3705 | |
3706 case TOK.TOKbreak: | |
3707 { Identifier ident; | |
3708 | |
3709 nextToken(); | |
3710 if (token.value == TOK.TOKidentifier) | |
3711 { ident = token.ident; | |
3712 nextToken(); | |
3713 } | |
3714 else | |
3715 ident = null; | |
3716 check(TOK.TOKsemicolon, "break statement"); | |
3717 s = new BreakStatement(loc, ident); | |
3718 break; | |
3719 } | |
3720 | |
3721 case TOK.TOKcontinue: | |
3722 { Identifier ident; | |
3723 | |
3724 nextToken(); | |
3725 if (token.value == TOK.TOKidentifier) | |
3726 { ident = token.ident; | |
3727 nextToken(); | |
3728 } | |
3729 else | |
3730 ident = null; | |
3731 check(TOK.TOKsemicolon, "continue statement"); | |
3732 s = new ContinueStatement(loc, ident); | |
3733 break; | |
3734 } | |
3735 | |
3736 case TOK.TOKgoto: | |
3737 { Identifier ident; | |
3738 | |
3739 nextToken(); | |
3740 if (token.value == TOK.TOKdefault) | |
3741 { | |
3742 nextToken(); | |
3743 s = new GotoDefaultStatement(loc); | |
3744 } | |
3745 else if (token.value == TOK.TOKcase) | |
3746 { | |
3747 Expression exp = null; | |
3748 | |
3749 nextToken(); | |
3750 if (token.value != TOK.TOKsemicolon) | |
3751 exp = parseExpression(); | |
3752 s = new GotoCaseStatement(loc, exp); | |
3753 } | |
3754 else | |
3755 { | |
3756 if (token.value != TOK.TOKidentifier) | |
3757 { error("Identifier expected following goto"); | |
3758 ident = null; | |
3759 } | |
3760 else | |
3761 { ident = token.ident; | |
3762 nextToken(); | |
3763 } | |
3764 s = new GotoStatement(loc, ident); | |
3765 } | |
3766 check(TOK.TOKsemicolon, "goto statement"); | |
3767 break; | |
3768 } | |
3769 | |
3770 case TOK.TOKsynchronized: | |
3771 { Expression exp; | |
3772 Statement body_; | |
3773 | |
3774 nextToken(); | |
3775 if (token.value == TOK.TOKlparen) | |
3776 { | |
3777 nextToken(); | |
3778 exp = parseExpression(); | |
3779 check(TOK.TOKrparen); | |
3780 } | |
3781 else | |
3782 exp = null; | |
3783 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3784 s = new SynchronizedStatement(loc, exp, body_); | |
3785 break; | |
3786 } | |
3787 | |
3788 case TOK.TOKwith: | |
3789 { Expression exp; | |
3790 Statement body_; | |
3791 | |
3792 nextToken(); | |
3793 check(TOK.TOKlparen); | |
3794 exp = parseExpression(); | |
3795 check(TOK.TOKrparen); | |
3796 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3797 s = new WithStatement(loc, exp, body_); | |
3798 break; | |
3799 } | |
3800 | |
3801 case TOK.TOKtry: | |
3802 { Statement body_; | |
3803 Array catches = null; | |
3804 Statement finalbody = null; | |
3805 | |
3806 nextToken(); | |
3807 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3808 while (token.value == TOK.TOKcatch) | |
3809 { | |
3810 Statement handler; | |
3811 Catch c; | |
3812 Type tt; | |
3813 Identifier id; | |
3814 Loc loc2 = this.loc; | |
3815 | |
3816 nextToken(); | |
3817 if (token.value == TOK.TOKlcurly) | |
3818 { | |
3819 tt = null; | |
3820 id = null; | |
3821 } | |
3822 else | |
3823 { | |
3824 check(TOK.TOKlparen); | |
3825 id = null; | |
3826 tt = parseType(&id); | |
3827 check(TOK.TOKrparen); | |
3828 } | |
3829 handler = parseStatement(cast(ParseStatementFlags)0); | |
3830 c = new Catch(loc2, tt, id, handler); | |
3831 if (!catches) | |
3832 catches = new Array(); | |
3833 catches.push(cast(void*)c); | |
3834 } | |
3835 | |
3836 if (token.value == TOK.TOKfinally) | |
3837 { nextToken(); | |
3838 finalbody = parseStatement(cast(ParseStatementFlags)0); | |
3839 } | |
3840 | |
3841 s = body_; | |
3842 if (!catches && !finalbody) | |
3843 error("catch or finally expected following try"); | |
3844 else | |
3845 { if (catches) | |
3846 s = new TryCatchStatement(loc, body_, catches); | |
3847 if (finalbody) | |
3848 s = new TryFinallyStatement(loc, s, finalbody); | |
3849 } | |
3850 break; | |
3851 } | |
3852 | |
3853 case TOK.TOKthrow: | |
3854 { Expression exp; | |
3855 | |
3856 nextToken(); | |
3857 exp = parseExpression(); | |
3858 check(TOK.TOKsemicolon, "throw statement"); | |
3859 s = new ThrowStatement(loc, exp); | |
3860 break; | |
3861 } | |
3862 | |
3863 case TOK.TOKvolatile: | |
3864 nextToken(); | |
3865 s = parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope); | |
3866 version (DMDV2) { | |
3867 if (!global.params.useDeprecated) | |
3868 error("volatile statements deprecated; used synchronized statements instead"); | |
3869 } | |
3870 s = new VolatileStatement(loc, s); | |
3871 break; | |
3872 | |
3873 case TOK.TOKasm: | |
3874 { Statements statements; | |
3875 Identifier label; | |
3876 Loc labelloc; | |
3877 Token* toklist; | |
3878 Token** ptoklist; | |
3879 | |
3880 // Parse the asm block into a sequence of AsmStatements, | |
3881 // each AsmStatement is one instruction. | |
3882 // Separate out labels. | |
3883 // Defer parsing of AsmStatements until semantic processing. | |
3884 | |
3885 nextToken(); | |
3886 check(TOK.TOKlcurly); | |
3887 toklist = null; | |
3888 ptoklist = &toklist; | |
3889 label = null; | |
3890 statements = new Statements(); | |
3891 while (1) | |
3892 { | |
3893 switch (token.value) | |
3894 { | |
3895 case TOK.TOKidentifier: | |
3896 if (!toklist) | |
3897 { | |
3898 // Look ahead to see if it is a label | |
3899 t = peek(&token); | |
3900 if (t.value == TOK.TOKcolon) | |
3901 { // It's a label | |
3902 label = token.ident; | |
3903 labelloc = this.loc; | |
3904 nextToken(); | |
3905 nextToken(); | |
3906 continue; | |
3907 } | |
3908 } | |
3909 goto Ldefault; | |
3910 | |
3911 case TOK.TOKrcurly: | |
3912 if (toklist || label) | |
3913 { | |
3914 error("asm statements must end in ';'"); | |
3915 } | |
3916 break; | |
3917 | |
3918 case TOK.TOKsemicolon: | |
3919 s = null; | |
3920 if (toklist || label) | |
3921 { // Create AsmStatement from list of tokens we've saved | |
3922 s = new AsmStatement(this.loc, toklist); | |
3923 toklist = null; | |
3924 ptoklist = &toklist; | |
3925 if (label) | |
3926 { | |
3927 s = new LabelStatement(labelloc, label, s); | |
3928 label = null; | |
3929 } | |
3930 statements.push(cast(void*)s); | |
3931 } | |
3932 nextToken(); | |
3933 continue; | |
3934 | |
3935 case TOK.TOKeof: | |
3936 /* { */ | |
3937 error("matching '}' expected, not end of file"); | |
3938 break; | |
3939 | |
3940 default: | |
3941 Ldefault: | |
3942 *ptoklist = new Token(); | |
3943 memcpy(*ptoklist, &token, Token.sizeof); | |
3944 ptoklist = &(*ptoklist).next; | |
3945 *ptoklist = null; | |
3946 | |
3947 nextToken(); | |
3948 continue; | |
3949 } | |
3950 break; | |
3951 } | |
3952 s = new CompoundStatement(loc, statements); | |
3953 nextToken(); | |
3954 break; | |
3955 } | |
3956 | |
3957 default: | |
3958 error("found '%s' instead of statement", token.toChars()); | |
3959 goto Lerror; | |
3960 | |
3961 Lerror: | |
3962 while (token.value != TOK.TOKrcurly && | |
3963 token.value != TOK.TOKsemicolon && | |
3964 token.value != TOK.TOKeof) | |
3965 nextToken(); | |
3966 if (token.value == TOK.TOKsemicolon) | |
3967 nextToken(); | |
3968 s = null; | |
3969 break; | |
3970 } | |
3971 | |
3972 return s; | |
3973 } | |
3974 | |
3975 /***************************************** | |
3976 * Parse initializer for variable declaration. | |
3977 */ | |
3978 Initializer parseInitializer() | |
3979 { | |
3980 StructInitializer is_; | |
3981 ArrayInitializer ia; | |
3982 ExpInitializer ie; | |
3983 Expression e; | |
3984 Identifier id; | |
3985 Initializer value; | |
3986 int comma; | |
3987 Loc loc = this.loc; | |
3988 Token* t; | |
3989 int braces; | |
3990 int brackets; | |
3991 | |
3992 switch (token.value) | |
3993 { | |
3994 case TOK.TOKlcurly: | |
3995 /* Scan ahead to see if it is a struct initializer or | |
3996 * a function literal. | |
3997 * If it contains a ';', it is a function literal. | |
3998 * Treat { } as a struct initializer. | |
3999 */ | |
4000 braces = 1; | |
4001 for (t = peek(&token); 1; t = peek(t)) | |
4002 { | |
4003 switch (t.value) | |
4004 { | |
4005 case TOK.TOKsemicolon: | |
4006 case TOK.TOKreturn: | |
4007 goto Lexpression; | |
4008 | |
4009 case TOK.TOKlcurly: | |
4010 braces++; | |
4011 continue; | |
4012 | |
4013 case TOK.TOKrcurly: | |
4014 if (--braces == 0) | |
4015 break; | |
4016 continue; | |
4017 | |
4018 case TOK.TOKeof: | |
4019 break; | |
4020 | |
4021 default: | |
4022 continue; | |
4023 } | |
4024 break; | |
4025 } | |
4026 | |
4027 is_ = new StructInitializer(loc); | |
4028 nextToken(); | |
4029 comma = 0; | |
4030 while (1) | |
4031 { | |
4032 switch (token.value) | |
4033 { | |
4034 case TOK.TOKidentifier: | |
4035 if (comma == 1) | |
4036 error("comma expected separating field initializers"); | |
4037 t = peek(&token); | |
4038 if (t.value == TOK.TOKcolon) | |
4039 { | |
4040 id = token.ident; | |
4041 nextToken(); | |
4042 nextToken(); // skip over ':' | |
4043 } | |
4044 else | |
4045 { | |
4046 id = null; | |
4047 } | |
4048 value = parseInitializer(); | |
4049 is_.addInit(id, value); | |
4050 comma = 1; | |
4051 continue; | |
4052 | |
4053 case TOK.TOKcomma: | |
4054 nextToken(); | |
4055 comma = 2; | |
4056 continue; | |
4057 | |
4058 case TOK.TOKrcurly: // allow trailing comma's | |
4059 nextToken(); | |
4060 break; | |
4061 | |
4062 case TOK.TOKeof: | |
4063 error("found EOF instead of initializer"); | |
4064 break; | |
4065 | |
4066 default: | |
4067 value = parseInitializer(); | |
4068 is_.addInit(null, value); | |
4069 comma = 1; | |
4070 continue; | |
4071 //error("found '%s' instead of field initializer", token.toChars()); | |
4072 //break; | |
4073 } | |
4074 break; | |
4075 } | |
4076 return is_; | |
4077 | |
4078 case TOK.TOKlbracket: | |
4079 /* Scan ahead to see if it is an array initializer or | |
4080 * an expression. | |
4081 * If it ends with a ';' ',' or '}', it is an array initializer. | |
4082 */ | |
4083 brackets = 1; | |
4084 for (t = peek(&token); 1; t = peek(t)) | |
4085 { | |
4086 switch (t.value) | |
4087 { | |
4088 case TOK.TOKlbracket: | |
4089 brackets++; | |
4090 continue; | |
4091 | |
4092 case TOK.TOKrbracket: | |
4093 if (--brackets == 0) | |
4094 { | |
4095 t = peek(t); | |
4096 if (t.value != TOK.TOKsemicolon && | |
4097 t.value != TOK.TOKcomma && | |
4098 t.value != TOK.TOKrcurly) | |
4099 goto Lexpression; | |
4100 break; | |
4101 } | |
4102 continue; | |
4103 | |
4104 case TOK.TOKeof: | |
4105 break; | |
4106 | |
4107 default: | |
4108 continue; | |
4109 } | |
4110 break; | |
4111 } | |
4112 | |
4113 ia = new ArrayInitializer(loc); | |
4114 nextToken(); | |
4115 comma = 0; | |
4116 while (true) | |
4117 { | |
4118 switch (token.value) | |
4119 { | |
4120 default: | |
4121 if (comma == 1) | |
4122 { | |
4123 error("comma expected separating array initializers, not %s", token.toChars()); | |
4124 nextToken(); | |
4125 break; | |
4126 } | |
4127 e = parseAssignExp(); | |
4128 if (!e) | |
4129 break; | |
4130 if (token.value == TOK.TOKcolon) | |
4131 { | |
4132 nextToken(); | |
4133 value = parseInitializer(); | |
4134 } | |
4135 else | |
4136 { value = new ExpInitializer(e.loc, e); | |
4137 e = null; | |
4138 } | |
4139 ia.addInit(e, value); | |
4140 comma = 1; | |
4141 continue; | |
4142 | |
4143 case TOK.TOKlcurly: | |
4144 case TOK.TOKlbracket: | |
4145 if (comma == 1) | |
4146 error("comma expected separating array initializers, not %s", token.toChars()); | |
4147 value = parseInitializer(); | |
4148 ia.addInit(null, value); | |
4149 comma = 1; | |
4150 continue; | |
4151 | |
4152 case TOK.TOKcomma: | |
4153 nextToken(); | |
4154 comma = 2; | |
4155 continue; | |
4156 | |
4157 case TOK.TOKrbracket: // allow trailing comma's | |
4158 nextToken(); | |
4159 break; | |
4160 | |
4161 case TOK.TOKeof: | |
4162 error("found '%s' instead of array initializer", token.toChars()); | |
4163 break; | |
4164 } | |
4165 break; | |
4166 } | |
4167 return ia; | |
4168 | |
4169 case TOK.TOKvoid: | |
4170 t = peek(&token); | |
4171 if (t.value == TOK.TOKsemicolon || t.value == TOK.TOKcomma) | |
4172 { | |
4173 nextToken(); | |
4174 return new VoidInitializer(loc); | |
4175 } | |
4176 goto Lexpression; | |
4177 | |
4178 default: | |
4179 Lexpression: | |
4180 e = parseAssignExp(); | |
4181 ie = new ExpInitializer(loc, e); | |
4182 return ie; | |
4183 } | |
4184 } | |
4185 | |
4186 /***************************************** | |
4187 * Parses default argument initializer expression that is an assign expression, | |
4188 * with special handling for __FILE__ and __LINE__. | |
4189 */ | |
4190 version (DMDV2) { | |
4191 Expression parseDefaultInitExp() | |
4192 { | |
4193 if (token.value == TOK.TOKfile || | |
4194 token.value == TOK.TOKline) | |
4195 { | |
4196 Token* t = peek(&token); | |
4197 if (t.value == TOK.TOKcomma || t.value == TOK.TOKrparen) | |
4198 { | |
4199 Expression e; | |
4200 | |
4201 if (token.value == TOK.TOKfile) | |
4202 e = new FileInitExp(loc); | |
4203 else | |
4204 e = new LineInitExp(loc); | |
4205 nextToken(); | |
4206 return e; | |
4207 } | |
4208 } | |
4209 | |
4210 Expression e = parseAssignExp(); | |
4211 return e; | |
4212 } | |
4213 } | |
4214 void check(Loc loc, TOK value) | |
4215 { | |
4216 if (token.value != value) | |
4217 error(loc, "found '%s' when expecting '%s'", token.toChars(), Token.toChars(value)); | |
4218 nextToken(); | |
4219 } | |
4220 | |
4221 void check(TOK value) | |
4222 { | |
4223 check(loc, value); | |
4224 } | |
4225 | |
4226 void check(TOK value, string string_) | |
4227 { | |
4228 if (token.value != value) { | |
4229 error("found '%s' when expecting '%s' following '%s'", token.toChars(), Token.toChars(value), string_); | |
4230 } | |
4231 nextToken(); | |
4232 } | |
4233 | |
4234 /************************************ | |
4235 * Determine if the scanner is sitting on the start of a declaration. | |
4236 * Input: | |
4237 * needId 0 no identifier | |
4238 * 1 identifier optional | |
4239 * 2 must have identifier | |
4240 * Output: | |
4241 * if *pt is not null, it is set to the ending token, which would be endtok | |
4242 */ | |
4243 | |
4244 bool isDeclaration(Token* t, int needId, TOK endtok, Token** pt) | |
4245 { | |
4246 //printf("isDeclaration(needId = %d)\n", needId); | |
4247 int haveId = 0; | |
4248 | |
4249 version (DMDV2) { | |
4250 if ((t.value == TOK.TOKconst || | |
4251 t.value == TOK.TOKinvariant || | |
4252 t.value == TOK.TOKimmutable || | |
4253 t.value == TOK.TOKshared) && | |
4254 peek(t).value != TOK.TOKlparen) | |
4255 { | |
4256 /* const type | |
4257 * immutable type | |
4258 * shared type | |
4259 */ | |
4260 t = peek(t); | |
4261 } | |
4262 } | |
4263 | |
4264 if (!isBasicType(&t)) | |
4265 { | |
4266 goto Lisnot; | |
4267 } | |
4268 if (!isDeclarator(&t, &haveId, endtok)) | |
4269 goto Lisnot; | |
4270 if ( needId == 1 || | |
4271 (needId == 0 && !haveId) || | |
4272 (needId == 2 && haveId)) | |
4273 { | |
4274 if (pt) | |
4275 *pt = t; | |
4276 goto Lis; | |
4277 } | |
4278 else | |
4279 goto Lisnot; | |
4280 | |
4281 Lis: | |
4282 //printf("\tis declaration, t = %s\n", t.toChars()); | |
4283 return true; | |
4284 | |
4285 Lisnot: | |
4286 //printf("\tis not declaration\n"); | |
4287 return false; | |
4288 } | |
4289 | |
4290 bool isBasicType(Token** pt) | |
4291 { | |
4292 // This code parallels parseBasicType() | |
4293 Token* t = *pt; | |
4294 Token* t2; | |
4295 int parens; | |
4296 int haveId = 0; | |
4297 | |
4298 switch (t.value) | |
4299 { | |
4300 case TOKwchar: | |
4301 case TOKdchar: | |
4302 case TOKbit: | |
4303 case TOKbool: | |
4304 case TOKchar: | |
4305 case TOKint8: | |
4306 case TOKuns8: | |
4307 case TOKint16: | |
4308 case TOKuns16: | |
4309 case TOKint32: | |
4310 case TOKuns32: | |
4311 case TOKint64: | |
4312 case TOKuns64: | |
4313 case TOKfloat32: | |
4314 case TOKfloat64: | |
4315 case TOKfloat80: | |
4316 case TOKimaginary32: | |
4317 case TOKimaginary64: | |
4318 case TOKimaginary80: | |
4319 case TOKcomplex32: | |
4320 case TOKcomplex64: | |
4321 case TOKcomplex80: | |
4322 case TOKvoid: | |
4323 t = peek(t); | |
4324 break; | |
4325 | |
4326 case TOK.TOKidentifier: | |
4327 L5: | |
4328 t = peek(t); | |
4329 if (t.value == TOK.TOKnot) | |
4330 { | |
4331 goto L4; | |
4332 } | |
4333 goto L3; | |
4334 while (1) | |
4335 { | |
4336 L2: | |
4337 t = peek(t); | |
4338 L3: | |
4339 if (t.value == TOK.TOKdot) | |
4340 { | |
4341 Ldot: | |
4342 t = peek(t); | |
4343 if (t.value != TOK.TOKidentifier) | |
4344 goto Lfalse; | |
4345 t = peek(t); | |
4346 if (t.value != TOK.TOKnot) | |
4347 goto L3; | |
4348 L4: | |
4349 /* Seen a ! | |
4350 * Look for: | |
4351 * !( args ), !identifier, etc. | |
4352 */ | |
4353 t = peek(t); | |
4354 switch (t.value) | |
4355 { case TOK.TOKidentifier: | |
4356 goto L5; | |
4357 case TOK.TOKlparen: | |
4358 if (!skipParens(t, &t)) | |
4359 goto Lfalse; | |
4360 break; | |
4361 case TOK.TOKwchar: case TOK.TOKdchar: | |
4362 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
4363 case TOK.TOKint8: case TOK.TOKuns8: | |
4364 case TOK.TOKint16: case TOK.TOKuns16: | |
4365 case TOK.TOKint32: case TOK.TOKuns32: | |
4366 case TOK.TOKint64: case TOK.TOKuns64: | |
4367 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
4368 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
4369 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
4370 case TOK.TOKvoid: | |
4371 case TOK.TOKint32v: | |
4372 case TOK.TOKuns32v: | |
4373 case TOK.TOKint64v: | |
4374 case TOK.TOKuns64v: | |
4375 case TOK.TOKfloat32v: | |
4376 case TOK.TOKfloat64v: | |
4377 case TOK.TOKfloat80v: | |
4378 case TOK.TOKimaginary32v: | |
4379 case TOK.TOKimaginary64v: | |
4380 case TOK.TOKimaginary80v: | |
4381 case TOK.TOKnull: | |
4382 case TOK.TOKtrue: | |
4383 case TOK.TOKfalse: | |
4384 case TOK.TOKcharv: | |
4385 case TOK.TOKwcharv: | |
4386 case TOK.TOKdcharv: | |
4387 case TOK.TOKstring: | |
4388 case TOK.TOKfile: | |
4389 case TOK.TOKline: | |
4390 goto L2; | |
4391 default: | |
4392 goto Lfalse; | |
4393 } | |
4394 } | |
4395 else | |
4396 break; | |
4397 } | |
4398 break; | |
4399 | |
4400 case TOK.TOKdot: | |
4401 goto Ldot; | |
4402 | |
4403 case TOK.TOKtypeof: | |
4404 /* typeof(exp).identifier... | |
4405 */ | |
4406 t = peek(t); | |
4407 if (t.value != TOK.TOKlparen) | |
4408 goto Lfalse; | |
4409 if (!skipParens(t, &t)) | |
4410 goto Lfalse; | |
4411 goto L2; | |
4412 | |
4413 case TOK.TOKconst: | |
4414 case TOK.TOKinvariant: | |
4415 case TOK.TOKimmutable: | |
4416 case TOK.TOKshared: | |
4417 // const(type) or immutable(type) or shared(type) | |
4418 t = peek(t); | |
4419 if (t.value != TOK.TOKlparen) | |
4420 goto Lfalse; | |
4421 t = peek(t); | |
4422 if (!isDeclaration(t, 0, TOK.TOKrparen, &t)) | |
4423 { | |
4424 goto Lfalse; | |
4425 } | |
4426 t = peek(t); | |
4427 break; | |
4428 | |
4429 default: | |
4430 goto Lfalse; | |
4431 } | |
4432 *pt = t; | |
4433 //printf("is\n"); | |
4434 return true; | |
4435 | |
4436 Lfalse: | |
4437 //printf("is not\n"); | |
4438 return false; | |
4439 } | |
4440 | |
4441 bool isDeclarator(Token** pt, int* haveId, TOK endtok) | |
4442 { | |
4443 // This code parallels parseDeclarator() | |
4444 Token* t = *pt; | |
4445 bool parens; | |
4446 | |
4447 //printf("Parser.isDeclarator()\n"); | |
4448 //t.print(); | |
4449 if (t.value == TOK.TOKassign) | |
4450 return false; | |
4451 | |
4452 while (true) | |
4453 { | |
4454 parens = false; | |
4455 switch (t.value) | |
4456 { | |
4457 case TOK.TOKmul: | |
4458 //case TOKand: | |
4459 t = peek(t); | |
4460 continue; | |
4461 | |
4462 case TOK.TOKlbracket: | |
4463 t = peek(t); | |
4464 if (t.value == TOK.TOKrbracket) | |
4465 { | |
4466 t = peek(t); | |
4467 } | |
4468 else if (isDeclaration(t, 0, TOK.TOKrbracket, &t)) | |
4469 { | |
4470 // It's an associative array declaration | |
4471 t = peek(t); | |
4472 } | |
4473 else | |
4474 { | |
4475 // [ expression ] | |
4476 // [ expression .. expression ] | |
4477 if (!isExpression(&t)) | |
4478 return false; | |
4479 if (t.value == TOK.TOKslice) | |
4480 { | |
4481 t = peek(t); | |
4482 if (!isExpression(&t)) | |
4483 return false; | |
4484 } | |
4485 if (t.value != TOK.TOKrbracket) | |
4486 return false; | |
4487 t = peek(t); | |
4488 } | |
4489 continue; | |
4490 | |
4491 case TOK.TOKidentifier: | |
4492 if (*haveId) | |
4493 return false; | |
4494 *haveId = true; | |
4495 t = peek(t); | |
4496 break; | |
4497 | |
4498 case TOK.TOKlparen: | |
4499 t = peek(t); | |
4500 | |
4501 if (t.value == TOK.TOKrparen) | |
4502 return false; // () is not a declarator | |
4503 | |
4504 /* Regard ( identifier ) as not a declarator | |
4505 * BUG: what about ( *identifier ) in | |
4506 * f(*p)(x); | |
4507 * where f is a class instance with overloaded () ? | |
4508 * Should we just disallow C-style function pointer declarations? | |
4509 */ | |
4510 if (t.value == TOK.TOKidentifier) | |
4511 { | |
4512 Token *t2 = peek(t); | |
4513 if (t2.value == TOK.TOKrparen) | |
4514 return false; | |
4515 } | |
4516 | |
4517 if (!isDeclarator(&t, haveId, TOK.TOKrparen)) | |
4518 return false; | |
4519 t = peek(t); | |
4520 parens = true; | |
4521 break; | |
4522 | |
4523 case TOK.TOKdelegate: | |
4524 case TOK.TOKfunction: | |
4525 t = peek(t); | |
4526 if (!isParameters(&t)) | |
4527 return false; | |
4528 continue; | |
4529 default: | |
4530 break; /// | |
4531 } | |
4532 break; | |
4533 } | |
4534 | |
4535 while (1) | |
4536 { | |
4537 switch (t.value) | |
4538 { | |
4539 version (CARRAYDECL) { | |
4540 case TOK.TOKlbracket: | |
4541 parens = false; | |
4542 t = peek(t); | |
4543 if (t.value == TOK.TOKrbracket) | |
4544 { | |
4545 t = peek(t); | |
4546 } | |
4547 else if (isDeclaration(t, 0, TOKrbracket, &t)) | |
4548 { | |
4549 // It's an associative array declaration | |
4550 t = peek(t); | |
4551 } | |
4552 else | |
4553 { | |
4554 // [ expression ] | |
4555 if (!isExpression(&t)) | |
4556 return false; | |
4557 if (t.value != TOK.TOKrbracket) | |
4558 return false; | |
4559 t = peek(t); | |
4560 } | |
4561 continue; | |
4562 } | |
4563 | |
4564 case TOK.TOKlparen: | |
4565 parens = false; | |
4566 if (!isParameters(&t)) | |
4567 return false; | |
4568 version (DMDV2) { | |
4569 while (true) | |
4570 { | |
4571 switch (t.value) | |
4572 { | |
4573 case TOK.TOKconst: | |
4574 case TOK.TOKinvariant: | |
4575 case TOK.TOKimmutable: | |
4576 case TOK.TOKshared: | |
4577 case TOK.TOKpure: | |
4578 case TOK.TOKnothrow: | |
4579 t = peek(t); | |
4580 continue; | |
4581 case TOK.TOKat: | |
4582 t = peek(t); // skip '@' | |
4583 t = peek(t); // skip identifier | |
4584 continue; | |
4585 default: | |
4586 break; | |
4587 } | |
4588 break; | |
4589 } | |
4590 } | |
4591 continue; | |
4592 | |
4593 // Valid tokens that follow a declaration | |
4594 case TOK.TOKrparen: | |
4595 case TOK.TOKrbracket: | |
4596 case TOK.TOKassign: | |
4597 case TOK.TOKcomma: | |
4598 case TOK.TOKsemicolon: | |
4599 case TOK.TOKlcurly: | |
4600 case TOK.TOKin: | |
4601 // The !parens is to disallow unnecessary parentheses | |
4602 if (!parens && (endtok == TOK.TOKreserved || endtok == t.value)) | |
4603 { | |
4604 *pt = t; | |
4605 return true; | |
4606 } | |
4607 return false; | |
4608 | |
4609 default: | |
4610 return false; | |
4611 } | |
4612 } | |
4613 } | |
4614 | |
4615 bool isParameters(Token** pt) | |
4616 { | |
4617 // This code parallels parseParameters() | |
4618 Token* t = *pt; | |
4619 | |
4620 //printf("isParameters()\n"); | |
4621 if (t.value != TOKlparen) | |
4622 return false; | |
4623 | |
4624 t = peek(t); | |
4625 for (;1; t = peek(t)) | |
4626 { | |
4627 L1: | |
4628 switch (t.value) | |
4629 { | |
4630 case TOKrparen: | |
4631 break; | |
4632 | |
4633 case TOKdotdotdot: | |
4634 t = peek(t); | |
4635 break; | |
4636 | |
4637 case TOKin: | |
4638 case TOKout: | |
4639 case TOKinout: | |
4640 case TOKref: | |
4641 case TOKlazy: | |
4642 case TOKfinal: | |
4643 continue; | |
4644 | |
4645 case TOKconst: | |
4646 case TOKinvariant: | |
4647 case TOKimmutable: | |
4648 case TOKshared: | |
4649 t = peek(t); | |
4650 if (t.value == TOKlparen) | |
4651 { | |
4652 t = peek(t); | |
4653 if (!isDeclaration(t, 0, TOKrparen, &t)) | |
4654 return false; | |
4655 t = peek(t); // skip past closing ')' | |
4656 goto L2; | |
4657 } | |
4658 goto L1; | |
4659 | |
4660 static if (false) { | |
4661 case TOKstatic: | |
4662 continue; | |
4663 case TOKauto: | |
4664 case TOKalias: | |
4665 t = peek(t); | |
4666 if (t.value == TOKidentifier) | |
4667 t = peek(t); | |
4668 if (t.value == TOKassign) | |
4669 { t = peek(t); | |
4670 if (!isExpression(&t)) | |
4671 return false; | |
4672 } | |
4673 goto L3; | |
4674 } | |
4675 | |
4676 default: | |
4677 { | |
4678 if (!isBasicType(&t)) | |
4679 return false; | |
4680 L2: | |
4681 int tmp = false; | |
4682 if (t.value != TOKdotdotdot && | |
4683 !isDeclarator(&t, &tmp, TOKreserved)) | |
4684 return false; | |
4685 if (t.value == TOKassign) | |
4686 { | |
4687 t = peek(t); | |
4688 if (!isExpression(&t)) | |
4689 return false; | |
4690 } | |
4691 if (t.value == TOKdotdotdot) | |
4692 { | |
4693 t = peek(t); | |
4694 break; | |
4695 } | |
4696 } | |
4697 L3: | |
4698 if (t.value == TOKcomma) | |
4699 { | |
4700 continue; | |
4701 } | |
4702 break; | |
4703 } | |
4704 break; | |
4705 } | |
4706 | |
4707 if (t.value != TOKrparen) | |
4708 return false; | |
4709 | |
4710 t = peek(t); | |
4711 *pt = t; | |
4712 return true; | |
4713 } | |
4714 | |
4715 bool isExpression(Token** pt) | |
4716 { | |
4717 // This is supposed to determine if something is an expression. | |
4718 // What it actually does is scan until a closing right bracket | |
4719 // is found. | |
4720 | |
4721 Token* t = *pt; | |
4722 int brnest = 0; | |
4723 int panest = 0; | |
4724 int curlynest = 0; | |
4725 | |
4726 for (;; t = peek(t)) | |
4727 { | |
4728 switch (t.value) | |
4729 { | |
4730 case TOKlbracket: | |
4731 brnest++; | |
4732 continue; | |
4733 | |
4734 case TOKrbracket: | |
4735 if (--brnest >= 0) | |
4736 continue; | |
4737 break; | |
4738 | |
4739 case TOKlparen: | |
4740 panest++; | |
4741 continue; | |
4742 | |
4743 case TOKcomma: | |
4744 if (brnest || panest) | |
4745 continue; | |
4746 break; | |
4747 | |
4748 case TOKrparen: | |
4749 if (--panest >= 0) | |
4750 continue; | |
4751 break; | |
4752 | |
4753 case TOKlcurly: | |
4754 curlynest++; | |
4755 continue; | |
4756 | |
4757 case TOKrcurly: | |
4758 if (--curlynest >= 0) | |
4759 continue; | |
4760 return false; | |
4761 | |
4762 case TOKslice: | |
4763 if (brnest) | |
4764 continue; | |
4765 break; | |
4766 | |
4767 case TOKsemicolon: | |
4768 if (curlynest) | |
4769 continue; | |
4770 return false; | |
4771 | |
4772 case TOKeof: | |
4773 return false; | |
4774 | |
4775 default: | |
4776 continue; | |
4777 } | |
4778 break; | |
4779 } | |
4780 | |
4781 *pt = t; | |
4782 return true; | |
4783 } | |
4784 | |
4785 int isTemplateInstance(Token t, Token* pt) | |
4786 { | |
4787 assert(false); | |
4788 } | |
4789 | |
4790 /******************************************* | |
4791 * Skip parens, brackets. | |
4792 * Input: | |
4793 * t is on opening ( | |
4794 * Output: | |
4795 * *pt is set to closing token, which is ')' on success | |
4796 * Returns: | |
4797 * !=0 successful | |
4798 * 0 some parsing error | |
4799 */ | |
4800 bool skipParens(Token* t, Token** pt) | |
4801 { | |
4802 int parens = 0; | |
4803 | |
4804 while (1) | |
4805 { | |
4806 switch (t.value) | |
4807 { | |
4808 case TOKlparen: | |
4809 parens++; | |
4810 break; | |
4811 | |
4812 case TOKrparen: | |
4813 parens--; | |
4814 if (parens < 0) | |
4815 goto Lfalse; | |
4816 if (parens == 0) | |
4817 goto Ldone; | |
4818 break; | |
4819 | |
4820 case TOKeof: | |
4821 case TOKsemicolon: | |
4822 goto Lfalse; | |
4823 | |
4824 default: | |
4825 break; | |
4826 } | |
4827 t = peek(t); | |
4828 } | |
4829 | |
4830 Ldone: | |
4831 if (*pt) | |
4832 *pt = t; | |
4833 return true; | |
4834 | |
4835 Lfalse: | |
4836 return false; | |
4837 } | |
4838 | |
4839 Expression parseExpression() | |
4840 { | |
4841 Expression e; | |
4842 Expression e2; | |
4843 Loc loc = this.loc; | |
4844 | |
4845 //printf("Parser.parseExpression() loc = %d\n", loc.linnum); | |
4846 e = parseAssignExp(); | |
4847 while (token.value == TOK.TOKcomma) | |
4848 { | |
4849 nextToken(); | |
4850 e2 = parseAssignExp(); | |
4851 e = new CommaExp(loc, e, e2); | |
4852 loc = this.loc; | |
4853 } | |
4854 return e; | |
4855 } | |
4856 | |
4857 Expression parsePrimaryExp() | |
4858 { | |
4859 Expression e; | |
4860 Type t; | |
4861 Identifier id; | |
4862 TOK save; | |
4863 Loc loc = this.loc; | |
4864 | |
4865 //printf("parsePrimaryExp(): loc = %d\n", loc.linnum); | |
4866 switch (token.value) | |
4867 { | |
4868 case TOK.TOKidentifier: | |
4869 id = token.ident; | |
4870 nextToken(); | |
4871 if (token.value == TOK.TOKnot && peekNext() != TOK.TOKis) | |
4872 { // identifier!(template-argument-list) | |
4873 TemplateInstance tempinst; | |
4874 | |
4875 tempinst = new TemplateInstance(loc, id); | |
4876 nextToken(); | |
4877 if (token.value == TOK.TOKlparen) | |
4878 // ident!(template_arguments) | |
4879 tempinst.tiargs = parseTemplateArgumentList(); | |
4880 else | |
4881 // ident!template_argument | |
4882 tempinst.tiargs = parseTemplateArgument(); | |
4883 e = new ScopeExp(loc, tempinst); | |
4884 } | |
4885 else | |
4886 e = new IdentifierExp(loc, id); | |
4887 break; | |
4888 | |
4889 case TOK.TOKdollar: | |
4890 if (!inBrackets) | |
4891 error("'$' is valid only inside [] of index or slice"); | |
4892 e = new DollarExp(loc); | |
4893 nextToken(); | |
4894 break; | |
4895 | |
4896 case TOK.TOKdot: | |
4897 // Signal global scope '.' operator with "" identifier | |
4898 e = new IdentifierExp(loc, Id.empty); | |
4899 break; | |
4900 | |
4901 case TOK.TOKthis: | |
4902 e = new ThisExp(loc); | |
4903 nextToken(); | |
4904 break; | |
4905 | |
4906 case TOK.TOKsuper: | |
4907 e = new SuperExp(loc); | |
4908 nextToken(); | |
4909 break; | |
4910 | |
4911 case TOK.TOKint32v: | |
4912 e = new IntegerExp(loc, token.int32value, Type.tint32); | |
4913 nextToken(); | |
4914 break; | |
4915 | |
4916 case TOK.TOKuns32v: | |
4917 e = new IntegerExp(loc, token.uns32value, Type.tuns32); | |
4918 nextToken(); | |
4919 break; | |
4920 | |
4921 case TOK.TOKint64v: | |
4922 e = new IntegerExp(loc, token.int64value, Type.tint64); | |
4923 nextToken(); | |
4924 break; | |
4925 | |
4926 case TOK.TOKuns64v: | |
4927 e = new IntegerExp(loc, token.uns64value, Type.tuns64); | |
4928 nextToken(); | |
4929 break; | |
4930 | |
4931 case TOK.TOKfloat32v: | |
4932 e = new RealExp(loc, token.float80value, Type.tfloat32); | |
4933 nextToken(); | |
4934 break; | |
4935 | |
4936 case TOK.TOKfloat64v: | |
4937 e = new RealExp(loc, token.float80value, Type.tfloat64); | |
4938 nextToken(); | |
4939 break; | |
4940 | |
4941 case TOK.TOKfloat80v: | |
4942 e = new RealExp(loc, token.float80value, Type.tfloat80); | |
4943 nextToken(); | |
4944 break; | |
4945 | |
4946 case TOK.TOKimaginary32v: | |
4947 e = new RealExp(loc, token.float80value, Type.timaginary32); | |
4948 nextToken(); | |
4949 break; | |
4950 | |
4951 case TOK.TOKimaginary64v: | |
4952 e = new RealExp(loc, token.float80value, Type.timaginary64); | |
4953 nextToken(); | |
4954 break; | |
4955 | |
4956 case TOK.TOKimaginary80v: | |
4957 e = new RealExp(loc, token.float80value, Type.timaginary80); | |
4958 nextToken(); | |
4959 break; | |
4960 | |
4961 case TOK.TOKnull: | |
4962 e = new NullExp(loc); | |
4963 nextToken(); | |
4964 break; | |
4965 | |
4966 version (DMDV2) { | |
4967 case TOK.TOKfile: | |
4968 { | |
4969 string s = loc.filename ? loc.filename : mod.ident.toChars(); | |
4970 e = new StringExp(loc, s, 0); | |
4971 nextToken(); | |
4972 break; | |
4973 } | |
4974 | |
4975 case TOK.TOKline: | |
4976 e = new IntegerExp(loc, loc.linnum, Type.tint32); | |
4977 nextToken(); | |
4978 break; | |
4979 } | |
4980 | |
4981 case TOK.TOKtrue: | |
4982 e = new IntegerExp(loc, 1, Type.tbool); | |
4983 nextToken(); | |
4984 break; | |
4985 | |
4986 case TOK.TOKfalse: | |
4987 e = new IntegerExp(loc, 0, Type.tbool); | |
4988 nextToken(); | |
4989 break; | |
4990 | |
4991 case TOK.TOKcharv: | |
4992 e = new IntegerExp(loc, token.uns32value, Type.tchar); | |
4993 nextToken(); | |
4994 break; | |
4995 | |
4996 case TOK.TOKwcharv: | |
4997 e = new IntegerExp(loc, token.uns32value, Type.twchar); | |
4998 nextToken(); | |
4999 break; | |
5000 | |
5001 case TOK.TOKdcharv: | |
5002 e = new IntegerExp(loc, token.uns32value, Type.tdchar); | |
5003 nextToken(); | |
5004 break; | |
5005 | |
5006 case TOK.TOKstring: | |
5007 { | |
5008 const(char)* s; | |
5009 uint len; | |
5010 ubyte postfix; | |
5011 | |
5012 // cat adjacent strings | |
5013 s = token.ustring; | |
5014 len = token.len; | |
5015 postfix = token.postfix; | |
5016 while (1) | |
5017 { | |
5018 nextToken(); | |
5019 if (token.value == TOK.TOKstring) | |
5020 { uint len1; | |
5021 uint len2; | |
5022 char* s2; | |
5023 | |
5024 if (token.postfix) | |
5025 { if (token.postfix != postfix) | |
5026 error("mismatched string literal postfixes '%c' and '%c'", postfix, token.postfix); | |
5027 postfix = token.postfix; | |
5028 } | |
5029 | |
5030 len1 = len; | |
5031 len2 = token.len; | |
5032 len = len1 + len2; | |
2 | 5033 s2 = cast(char*)GC.malloc((len + 1) * ubyte.sizeof); |
0 | 5034 memcpy(s2, s, len1 * ubyte.sizeof); |
5035 memcpy(s2 + len1, token.ustring, (len2 + 1) * ubyte.sizeof); | |
5036 s = s2; | |
5037 } | |
5038 else | |
5039 break; | |
5040 } | |
5041 e = new StringExp(loc, assumeUnique(s[0..len]), postfix); | |
5042 break; | |
5043 } | |
5044 | |
5045 case TOK.TOKvoid: t = Type.tvoid; goto LabelX; | |
5046 case TOK.TOKint8: t = Type.tint8; goto LabelX; | |
5047 case TOK.TOKuns8: t = Type.tuns8; goto LabelX; | |
5048 case TOK.TOKint16: t = Type.tint16; goto LabelX; | |
5049 case TOK.TOKuns16: t = Type.tuns16; goto LabelX; | |
5050 case TOK.TOKint32: t = Type.tint32; goto LabelX; | |
5051 case TOK.TOKuns32: t = Type.tuns32; goto LabelX; | |
5052 case TOK.TOKint64: t = Type.tint64; goto LabelX; | |
5053 case TOK.TOKuns64: t = Type.tuns64; goto LabelX; | |
5054 case TOK.TOKfloat32: t = Type.tfloat32; goto LabelX; | |
5055 case TOK.TOKfloat64: t = Type.tfloat64; goto LabelX; | |
5056 case TOK.TOKfloat80: t = Type.tfloat80; goto LabelX; | |
5057 case TOK.TOKimaginary32: t = Type.timaginary32; goto LabelX; | |
5058 case TOK.TOKimaginary64: t = Type.timaginary64; goto LabelX; | |
5059 case TOK.TOKimaginary80: t = Type.timaginary80; goto LabelX; | |
5060 case TOK.TOKcomplex32: t = Type.tcomplex32; goto LabelX; | |
5061 case TOK.TOKcomplex64: t = Type.tcomplex64; goto LabelX; | |
5062 case TOK.TOKcomplex80: t = Type.tcomplex80; goto LabelX; | |
5063 case TOK.TOKbit: t = Type.tbit; goto LabelX; | |
5064 case TOK.TOKbool: t = Type.tbool; goto LabelX; | |
5065 case TOK.TOKchar: t = Type.tchar; goto LabelX; | |
5066 case TOK.TOKwchar: t = Type.twchar; goto LabelX; | |
5067 case TOK.TOKdchar: t = Type.tdchar; goto LabelX; | |
5068 LabelX: | |
5069 nextToken(); | |
5070 L1: | |
5071 check(TOK.TOKdot, t.toChars()); | |
5072 if (token.value != TOK.TOKidentifier) | |
5073 { error("found '%s' when expecting identifier following '%s.'", token.toChars(), t.toChars()); | |
5074 goto Lerr; | |
5075 } | |
5076 e = typeDotIdExp(loc, t, token.ident); | |
5077 nextToken(); | |
5078 break; | |
5079 | |
5080 case TOK.TOKtypeof: | |
5081 { | |
5082 t = parseTypeof(); | |
5083 e = new TypeExp(loc, t); | |
5084 break; | |
5085 } | |
5086 | |
5087 case TOK.TOKtypeid: | |
5088 { Type tt; | |
5089 | |
5090 nextToken(); | |
5091 check(TOK.TOKlparen, "typeid"); | |
5092 tt = parseType(); // ( type ) | |
5093 check(TOK.TOKrparen); | |
5094 e = new TypeidExp(loc, tt); | |
5095 break; | |
5096 } | |
5097 | |
5098 version (DMDV2) { | |
5099 case TOK.TOKtraits: | |
5100 { /* __traits(identifier, args...) | |
5101 */ | |
5102 Identifier ident; | |
5103 Objects args = null; | |
5104 | |
5105 nextToken(); | |
5106 check(TOK.TOKlparen); | |
5107 if (token.value != TOK.TOKidentifier) | |
5108 { error("__traits(identifier, args...) expected"); | |
5109 goto Lerr; | |
5110 } | |
5111 ident = token.ident; | |
5112 nextToken(); | |
5113 if (token.value == TOK.TOKcomma) | |
5114 args = parseTemplateArgumentList2(); // __traits(identifier, args...) | |
5115 else | |
5116 check(TOK.TOKrparen); // __traits(identifier) | |
5117 | |
5118 e = new TraitsExp(loc, ident, args); | |
5119 break; | |
5120 } | |
5121 } | |
5122 | |
5123 case TOK.TOKis: | |
5124 { Type targ; | |
5125 Identifier ident = null; | |
5126 Type tspec = null; | |
5127 TOK tok = TOK.TOKreserved; | |
5128 TOK tok2 = TOK.TOKreserved; | |
5129 TemplateParameters tpl = null; | |
5130 Loc loc2 = this.loc; | |
5131 | |
5132 nextToken(); | |
5133 if (token.value == TOK.TOKlparen) | |
5134 { | |
5135 nextToken(); | |
5136 targ = parseType(&ident); | |
5137 if (token.value == TOK.TOKcolon || token.value == TOK.TOKequal) | |
5138 { | |
5139 tok = token.value; | |
5140 nextToken(); | |
5141 if (tok == TOK.TOKequal && | |
5142 (token.value == TOK.TOKtypedef || | |
5143 token.value == TOK.TOKstruct || | |
5144 token.value == TOK.TOKunion || | |
5145 token.value == TOK.TOKclass || | |
5146 token.value == TOK.TOKsuper || | |
5147 token.value == TOK.TOKenum || | |
5148 token.value == TOK.TOKinterface || | |
5149 ///version (DMDV2) { | |
5150 token.value == TOK.TOKconst && peek(&token).value == TOK.TOKrparen || | |
5151 token.value == TOK.TOKinvariant && peek(&token).value == TOK.TOKrparen || | |
5152 token.value == TOK.TOKimmutable && peek(&token).value == TOK.TOKrparen || | |
5153 token.value == TOK.TOKshared && peek(&token).value == TOK.TOKrparen || | |
5154 ///} | |
5155 token.value == TOK.TOKfunction || | |
5156 token.value == TOK.TOKdelegate || | |
5157 token.value == TOK.TOKreturn)) | |
5158 { | |
5159 tok2 = token.value; | |
5160 nextToken(); | |
5161 } | |
5162 else | |
5163 { | |
5164 tspec = parseType(); | |
5165 } | |
5166 } | |
5167 if (ident && tspec) | |
5168 { | |
5169 if (token.value == TOK.TOKcomma) | |
5170 tpl = parseTemplateParameterList(1); | |
5171 else | |
5172 { tpl = new TemplateParameters(); | |
5173 check(TOK.TOKrparen); | |
5174 } | |
5175 TemplateParameter tp = new TemplateTypeParameter(loc2, ident, null, null); | |
5176 tpl.insert(0, cast(void*)tp); | |
5177 } | |
5178 else | |
5179 check(TOK.TOKrparen); | |
5180 } | |
5181 else | |
5182 { error("(type identifier : specialization) expected following is"); | |
5183 goto Lerr; | |
5184 } | |
5185 e = new IsExp(loc2, targ, ident, tok, tspec, tok2, tpl); | |
5186 break; | |
5187 } | |
5188 | |
5189 case TOK.TOKassert: | |
5190 { | |
5191 Expression msg = null; | |
5192 | |
5193 nextToken(); | |
5194 check(TOK.TOKlparen, "assert"); | |
5195 e = parseAssignExp(); | |
5196 if (token.value == TOK.TOKcomma) | |
5197 { nextToken(); | |
5198 msg = parseAssignExp(); | |
5199 } | |
5200 check(TOK.TOKrparen); | |
5201 e = new AssertExp(loc, e, msg); | |
5202 break; | |
5203 } | |
5204 | |
5205 case TOK.TOKmixin: | |
5206 { | |
5207 nextToken(); | |
5208 check(TOK.TOKlparen, "mixin"); | |
5209 e = parseAssignExp(); | |
5210 check(TOK.TOKrparen); | |
5211 e = new CompileExp(loc, e); | |
5212 break; | |
5213 } | |
5214 | |
5215 case TOK.TOKimport: | |
5216 { | |
5217 nextToken(); | |
5218 check(TOK.TOKlparen, "import"); | |
5219 e = parseAssignExp(); | |
5220 check(TOK.TOKrparen); | |
5221 e = new FileExp(loc, e); | |
5222 break; | |
5223 } | |
5224 | |
5225 case TOK.TOKlparen: | |
5226 if (peekPastParen(&token).value == TOK.TOKlcurly) | |
5227 { // (arguments) { statements... } | |
5228 save = TOK.TOKdelegate; | |
5229 goto case_delegate; | |
5230 } | |
5231 // ( expression ) | |
5232 nextToken(); | |
5233 e = parseExpression(); | |
5234 check(loc, TOK.TOKrparen); | |
5235 break; | |
5236 | |
5237 case TOK.TOKlbracket: | |
5238 { /* Parse array literals and associative array literals: | |
5239 * [ value, value, value ... ] | |
5240 * [ key:value, key:value, key:value ... ] | |
5241 */ | |
5242 Expressions values = new Expressions(); | |
5243 Expressions keys = null; | |
5244 | |
5245 nextToken(); | |
5246 if (token.value != TOK.TOKrbracket) | |
5247 { | |
5248 while (token.value != TOK.TOKeof) | |
5249 { | |
5250 Expression e2 = parseAssignExp(); | |
5251 if (token.value == TOK.TOKcolon && (keys || values.dim == 0)) | |
5252 { | |
5253 nextToken(); | |
5254 if (!keys) | |
5255 keys = new Expressions(); | |
5256 keys.push(cast(void*)e2); | |
5257 e2 = parseAssignExp(); | |
5258 } | |
5259 else if (keys) | |
5260 { | |
5261 error("'key:value' expected for associative array literal"); | |
5262 delete keys; | |
5263 keys = null; | |
5264 } | |
5265 values.push(cast(void*)e2); | |
5266 if (token.value == TOK.TOKrbracket) | |
5267 break; | |
5268 check(TOK.TOKcomma); | |
5269 } | |
5270 } | |
5271 check(TOK.TOKrbracket); | |
5272 | |
5273 if (keys) | |
5274 e = new AssocArrayLiteralExp(loc, keys, values); | |
5275 else | |
5276 e = new ArrayLiteralExp(loc, values); | |
5277 break; | |
5278 } | |
5279 | |
5280 case TOK.TOKlcurly: | |
5281 // { statements... } | |
5282 save = TOK.TOKdelegate; | |
5283 goto case_delegate; | |
5284 | |
5285 case TOK.TOKfunction: | |
5286 case TOK.TOKdelegate: | |
5287 save = token.value; | |
5288 nextToken(); | |
5289 case_delegate: | |
5290 { | |
5291 /* function type(parameters) { body } pure nothrow | |
5292 * delegate type(parameters) { body } pure nothrow | |
5293 * (parameters) { body } | |
5294 * { body } | |
5295 */ | |
5296 Arguments arguments; | |
5297 int varargs; | |
5298 FuncLiteralDeclaration fd; | |
5299 Type tt; | |
5300 bool isnothrow = false; | |
5301 bool ispure = false; | |
5302 | |
5303 if (token.value == TOK.TOKlcurly) | |
5304 { | |
5305 tt = null; | |
5306 varargs = 0; | |
5307 arguments = new Arguments(); | |
5308 } | |
5309 else | |
5310 { | |
5311 if (token.value == TOK.TOKlparen) | |
5312 tt = null; | |
5313 else | |
5314 { | |
5315 tt = parseBasicType(); | |
5316 tt = parseBasicType2(tt); // function return type | |
5317 } | |
5318 arguments = parseParameters(&varargs); | |
5319 while (1) | |
5320 { | |
5321 if (token.value == TOK.TOKpure) | |
5322 ispure = true; | |
5323 else if (token.value == TOK.TOKnothrow) | |
5324 isnothrow = true; | |
5325 else | |
5326 break; | |
5327 nextToken(); | |
5328 } | |
5329 } | |
5330 | |
5331 TypeFunction tf = new TypeFunction(arguments, tt, varargs, linkage); | |
5332 tf.ispure = ispure; | |
5333 tf.isnothrow = isnothrow; | |
5334 fd = new FuncLiteralDeclaration(loc, Loc(0), tf, save, null); | |
5335 parseContracts(fd); | |
5336 e = new FuncExp(loc, fd); | |
5337 break; | |
5338 } | |
5339 | |
5340 default: | |
5341 error("expression expected, not '%s'", token.toChars()); | |
5342 Lerr: | |
5343 // Anything for e, as long as it's not null | |
5344 e = new IntegerExp(loc, 0, Type.tint32); | |
5345 nextToken(); | |
5346 break; | |
5347 } | |
5348 return e; | |
5349 } | |
5350 | |
5351 Expression parseUnaryExp() | |
5352 { | |
5353 Expression e; | |
5354 Loc loc = this.loc; | |
5355 | |
5356 switch (token.value) | |
5357 { | |
5358 case TOK.TOKand: | |
5359 nextToken(); | |
5360 e = parseUnaryExp(); | |
5361 e = new AddrExp(loc, e); | |
5362 break; | |
5363 | |
5364 case TOK.TOKplusplus: | |
5365 nextToken(); | |
5366 e = parseUnaryExp(); | |
5367 e = new AddAssignExp(loc, e, new IntegerExp(loc, 1, Type.tint32)); | |
5368 break; | |
5369 | |
5370 case TOK.TOKminusminus: | |
5371 nextToken(); | |
5372 e = parseUnaryExp(); | |
5373 e = new MinAssignExp(loc, e, new IntegerExp(loc, 1, Type.tint32)); | |
5374 break; | |
5375 | |
5376 case TOK.TOKmul: | |
5377 nextToken(); | |
5378 e = parseUnaryExp(); | |
5379 e = new PtrExp(loc, e); | |
5380 break; | |
5381 | |
5382 case TOK.TOKmin: | |
5383 nextToken(); | |
5384 e = parseUnaryExp(); | |
5385 e = new NegExp(loc, e); | |
5386 break; | |
5387 | |
5388 case TOK.TOKadd: | |
5389 nextToken(); | |
5390 e = parseUnaryExp(); | |
5391 e = new UAddExp(loc, e); | |
5392 break; | |
5393 | |
5394 case TOK.TOKnot: | |
5395 nextToken(); | |
5396 e = parseUnaryExp(); | |
5397 e = new NotExp(loc, e); | |
5398 break; | |
5399 | |
5400 case TOK.TOKtilde: | |
5401 nextToken(); | |
5402 e = parseUnaryExp(); | |
5403 e = new ComExp(loc, e); | |
5404 break; | |
5405 | |
5406 case TOK.TOKdelete: | |
5407 nextToken(); | |
5408 e = parseUnaryExp(); | |
5409 e = new DeleteExp(loc, e); | |
5410 break; | |
5411 | |
5412 case TOK.TOKnew: | |
5413 e = parseNewExp(null); | |
5414 break; | |
5415 | |
5416 case TOK.TOKcast: // cast(type) expression | |
5417 { | |
5418 nextToken(); | |
5419 check(TOK.TOKlparen); | |
5420 /* Look for cast(), cast(const), cast(immutable), | |
5421 * cast(shared), cast(shared const) | |
5422 */ | |
5423 MOD m; | |
5424 if (token.value == TOK.TOKrparen) | |
5425 { | |
5426 m = MOD.MODundefined; | |
5427 goto Lmod1; | |
5428 } | |
5429 else if (token.value == TOK.TOKconst && peekNext() == TOK.TOKrparen) | |
5430 { | |
5431 m = MOD.MODconst; | |
5432 goto Lmod2; | |
5433 } | |
5434 else if ((token.value == TOK.TOKimmutable || token.value == TOK.TOKinvariant) && peekNext() == TOK.TOKrparen) | |
5435 { | |
5436 m = MOD.MODinvariant; | |
5437 goto Lmod2; | |
5438 } | |
5439 else if (token.value == TOK.TOKshared && peekNext() == TOK.TOKrparen) | |
5440 { | |
5441 m = MOD.MODshared; | |
5442 goto Lmod2; | |
5443 } | |
5444 else if (token.value == TOK.TOKconst && peekNext() == TOK.TOKshared && peekNext2() == TOK.TOKrparen || | |
5445 token.value == TOK.TOKshared && peekNext() == TOK.TOKconst && peekNext2() == TOK.TOKrparen) | |
5446 { | |
5447 m = MOD.MODshared | MOD.MODconst; | |
5448 nextToken(); | |
5449 Lmod2: | |
5450 nextToken(); | |
5451 Lmod1: | |
5452 nextToken(); | |
5453 e = parseUnaryExp(); | |
5454 e = new CastExp(loc, e, m); | |
5455 } | |
5456 else | |
5457 { | |
5458 Type t = parseType(); // ( type ) | |
5459 check(TOK.TOKrparen); | |
5460 e = parseUnaryExp(); | |
5461 e = new CastExp(loc, e, t); | |
5462 } | |
5463 break; | |
5464 } | |
5465 | |
5466 case TOK.TOKlparen: | |
5467 { Token *tk; | |
5468 | |
5469 tk = peek(&token); | |
5470 version (CCASTSYNTAX) { | |
5471 // If cast | |
5472 if (isDeclaration(tk, 0, TOK.TOKrparen, &tk)) | |
5473 { | |
5474 tk = peek(tk); // skip over right parenthesis | |
5475 switch (tk.value) | |
5476 { | |
5477 case TOK.TOKnot: | |
5478 tk = peek(tk); | |
5479 if (tk.value == TOK.TOKis) // !is | |
5480 break; | |
5481 case TOK.TOKdot: | |
5482 case TOK.TOKplusplus: | |
5483 case TOK.TOKminusminus: | |
5484 case TOK.TOKdelete: | |
5485 case TOK.TOKnew: | |
5486 case TOK.TOKlparen: | |
5487 case TOK.TOKidentifier: | |
5488 case TOK.TOKthis: | |
5489 case TOK.TOKsuper: | |
5490 case TOK.TOKint32v: | |
5491 case TOK.TOKuns32v: | |
5492 case TOK.TOKint64v: | |
5493 case TOK.TOKuns64v: | |
5494 case TOK.TOKfloat32v: | |
5495 case TOK.TOKfloat64v: | |
5496 case TOK.TOKfloat80v: | |
5497 case TOK.TOKimaginary32v: | |
5498 case TOK.TOKimaginary64v: | |
5499 case TOK.TOKimaginary80v: | |
5500 case TOK.TOKnull: | |
5501 case TOK.TOKtrue: | |
5502 case TOK.TOKfalse: | |
5503 case TOK.TOKcharv: | |
5504 case TOK.TOKwcharv: | |
5505 case TOK.TOKdcharv: | |
5506 case TOK.TOKstring: | |
5507 static if (false) { | |
5508 case TOK.TOKtilde: | |
5509 case TOK.TOKand: | |
5510 case TOK.TOKmul: | |
5511 case TOK.TOKmin: | |
5512 case TOK.TOKadd: | |
5513 } | |
5514 case TOK.TOKfunction: | |
5515 case TOK.TOKdelegate: | |
5516 case TOK.TOKtypeof: | |
5517 version (DMDV2) { | |
5518 case TOK.TOKfile: | |
5519 case TOK.TOKline: | |
5520 } | |
5521 case TOK.TOKwchar: case TOK.TOKdchar: | |
5522 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
5523 case TOK.TOKint8: case TOK.TOKuns8: | |
5524 case TOK.TOKint16: case TOK.TOKuns16: | |
5525 case TOK.TOKint32: case TOK.TOKuns32: | |
5526 case TOK.TOKint64: case TOK.TOKuns64: | |
5527 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
5528 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
5529 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
5530 case TOK.TOKvoid: // (type)int.size | |
5531 { | |
5532 // (type) una_exp | |
5533 nextToken(); | |
5534 Type t = parseType(); | |
5535 check(TOK.TOKrparen); | |
5536 | |
5537 // if .identifier | |
5538 if (token.value == TOK.TOKdot) | |
5539 { | |
5540 nextToken(); | |
5541 if (token.value != TOK.TOKidentifier) | |
5542 { | |
5543 error("Identifier expected following (type)."); | |
5544 return null; | |
5545 } | |
5546 e = typeDotIdExp(loc, t, token.ident); | |
5547 nextToken(); | |
5548 e = parsePostExp(e); | |
5549 } | |
5550 else | |
5551 { | |
5552 e = parseUnaryExp(); | |
5553 e = new CastExp(loc, e, t); | |
5554 error("C style cast illegal, use %s", e.toChars()); | |
5555 } | |
5556 return e; | |
5557 } | |
5558 | |
5559 default: | |
5560 break; /// | |
5561 } | |
5562 } | |
5563 } | |
5564 e = parsePrimaryExp(); | |
5565 e = parsePostExp(e); | |
5566 break; | |
5567 } | |
5568 default: | |
5569 e = parsePrimaryExp(); | |
5570 e = parsePostExp(e); | |
5571 break; | |
5572 } | |
5573 assert(e); | |
5574 return e; | |
5575 } | |
5576 | |
5577 Expression parsePostExp(Expression e) | |
5578 { | |
5579 Loc loc; | |
5580 | |
5581 while (1) | |
5582 { | |
5583 loc = this.loc; | |
5584 switch (token.value) | |
5585 { | |
5586 case TOK.TOKdot: | |
5587 nextToken(); | |
5588 if (token.value == TOK.TOKidentifier) | |
5589 { Identifier id = token.ident; | |
5590 | |
5591 nextToken(); | |
5592 if (token.value == TOK.TOKnot && peekNext() != TOK.TOKis) | |
5593 { // identifier!(template-argument-list) | |
5594 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
5595 nextToken(); | |
5596 if (token.value == TOK.TOKlparen) | |
5597 // ident!(template_arguments) | |
5598 tempinst.tiargs = parseTemplateArgumentList(); | |
5599 else | |
5600 // ident!template_argument | |
5601 tempinst.tiargs = parseTemplateArgument(); | |
5602 e = new DotTemplateInstanceExp(loc, e, tempinst); | |
5603 } | |
5604 else | |
5605 e = new DotIdExp(loc, e, id); | |
5606 continue; | |
5607 } | |
5608 else if (token.value == TOK.TOKnew) | |
5609 { | |
5610 e = parseNewExp(e); | |
5611 continue; | |
5612 } | |
5613 else | |
5614 error("identifier expected following '.', not '%s'", token.toChars()); | |
5615 break; | |
5616 | |
5617 case TOK.TOKplusplus: | |
5618 e = new PostExp(TOK.TOKplusplus, loc, e); | |
5619 break; | |
5620 | |
5621 case TOK.TOKminusminus: | |
5622 e = new PostExp(TOK.TOKminusminus, loc, e); | |
5623 break; | |
5624 | |
5625 case TOK.TOKlparen: | |
5626 e = new CallExp(loc, e, parseArguments()); | |
5627 continue; | |
5628 | |
5629 case TOK.TOKlbracket: | |
5630 { // array dereferences: | |
5631 // array[index] | |
5632 // array[] | |
5633 // array[lwr .. upr] | |
5634 Expression index; | |
5635 Expression upr; | |
5636 | |
5637 inBrackets++; | |
5638 nextToken(); | |
5639 if (token.value == TOK.TOKrbracket) | |
5640 { // array[] | |
5641 e = new SliceExp(loc, e, null, null); | |
5642 nextToken(); | |
5643 } | |
5644 else | |
5645 { | |
5646 index = parseAssignExp(); | |
5647 if (token.value == TOK.TOKslice) | |
5648 { // array[lwr .. upr] | |
5649 nextToken(); | |
5650 upr = parseAssignExp(); | |
5651 e = new SliceExp(loc, e, index, upr); | |
5652 } | |
5653 else | |
5654 { // array[index, i2, i3, i4, ...] | |
5655 Expressions arguments = new Expressions(); | |
5656 arguments.push(cast(void*)index); | |
5657 if (token.value == TOK.TOKcomma) | |
5658 { | |
5659 nextToken(); | |
5660 while (1) | |
5661 { Expression arg; | |
5662 | |
5663 arg = parseAssignExp(); | |
5664 arguments.push(cast(void*)arg); | |
5665 if (token.value == TOK.TOKrbracket) | |
5666 break; | |
5667 check(TOK.TOKcomma); | |
5668 } | |
5669 } | |
5670 e = new ArrayExp(loc, e, arguments); | |
5671 } | |
5672 check(TOK.TOKrbracket); | |
5673 inBrackets--; | |
5674 } | |
5675 continue; | |
5676 } | |
5677 | |
5678 default: | |
5679 return e; | |
5680 } | |
5681 nextToken(); | |
5682 } | |
5683 | |
5684 assert(false); | |
5685 } | |
5686 | |
5687 Expression parseMulExp() | |
5688 { | |
5689 Expression e; | |
5690 Expression e2; | |
5691 Loc loc = this.loc; | |
5692 | |
5693 e = parseUnaryExp(); | |
5694 while (1) | |
5695 { | |
5696 switch (token.value) | |
5697 { | |
5698 case TOK.TOKmul: nextToken(); e2 = parseUnaryExp(); e = new MulExp(loc,e,e2); continue; | |
5699 case TOK.TOKdiv: nextToken(); e2 = parseUnaryExp(); e = new DivExp(loc,e,e2); continue; | |
5700 case TOK.TOKmod: nextToken(); e2 = parseUnaryExp(); e = new ModExp(loc,e,e2); continue; | |
5701 | |
5702 default: | |
5703 break; | |
5704 } | |
5705 break; | |
5706 } | |
5707 return e; | |
5708 } | |
5709 | |
5710 Expression parseShiftExp() | |
5711 { | |
5712 Expression e; | |
5713 Expression e2; | |
5714 Loc loc = this.loc; | |
5715 | |
5716 e = parseAddExp(); | |
5717 while (1) | |
5718 { | |
5719 switch (token.value) | |
5720 { | |
5721 case TOK.TOKshl: nextToken(); e2 = parseAddExp(); e = new ShlExp(loc,e,e2); continue; | |
5722 case TOK.TOKshr: nextToken(); e2 = parseAddExp(); e = new ShrExp(loc,e,e2); continue; | |
5723 case TOK.TOKushr: nextToken(); e2 = parseAddExp(); e = new UshrExp(loc,e,e2); continue; | |
5724 | |
5725 default: | |
5726 break; | |
5727 } | |
5728 break; | |
5729 } | |
5730 return e; | |
5731 } | |
5732 | |
5733 Expression parseAddExp() | |
5734 { | |
5735 Expression e; | |
5736 Expression e2; | |
5737 Loc loc = this.loc; | |
5738 | |
5739 e = parseMulExp(); | |
5740 while (1) | |
5741 { | |
5742 switch (token.value) | |
5743 { | |
5744 case TOK.TOKadd: nextToken(); e2 = parseMulExp(); e = new AddExp(loc,e,e2); continue; | |
5745 case TOK.TOKmin: nextToken(); e2 = parseMulExp(); e = new MinExp(loc,e,e2); continue; | |
5746 case TOK.TOKtilde: nextToken(); e2 = parseMulExp(); e = new CatExp(loc,e,e2); continue; | |
5747 | |
5748 default: | |
5749 break; | |
5750 } | |
5751 break; | |
5752 } | |
5753 return e; | |
5754 } | |
5755 | |
5756 Expression parseRelExp() | |
5757 { | |
5758 assert(false); | |
5759 } | |
5760 | |
5761 Expression parseEqualExp() | |
5762 { | |
5763 assert(false); | |
5764 } | |
5765 | |
5766 Expression parseCmpExp() | |
5767 { | |
5768 Expression e; | |
5769 Expression e2; | |
5770 Token* t; | |
5771 Loc loc = this.loc; | |
5772 | |
5773 e = parseShiftExp(); | |
5774 TOK op = token.value; | |
5775 | |
5776 switch (op) | |
5777 { | |
5778 case TOK.TOKequal: | |
5779 case TOK.TOKnotequal: | |
5780 nextToken(); | |
5781 e2 = parseShiftExp(); | |
5782 e = new EqualExp(op, loc, e, e2); | |
5783 break; | |
5784 | |
5785 case TOK.TOKis: | |
5786 op = TOK.TOKidentity; | |
5787 goto L1; | |
5788 | |
5789 case TOK.TOKnot: | |
5790 // Attempt to identify '!is' | |
5791 t = peek(&token); | |
5792 if (t.value != TOK.TOKis) | |
5793 break; | |
5794 nextToken(); | |
5795 op = TOK.TOKnotidentity; | |
5796 goto L1; | |
5797 | |
5798 L1: | |
5799 nextToken(); | |
5800 e2 = parseShiftExp(); | |
5801 e = new IdentityExp(op, loc, e, e2); | |
5802 break; | |
5803 | |
5804 case TOK.TOKlt: | |
5805 case TOK.TOKle: | |
5806 case TOK.TOKgt: | |
5807 case TOK.TOKge: | |
5808 case TOK.TOKunord: | |
5809 case TOK.TOKlg: | |
5810 case TOK.TOKleg: | |
5811 case TOK.TOKule: | |
5812 case TOK.TOKul: | |
5813 case TOK.TOKuge: | |
5814 case TOK.TOKug: | |
5815 case TOK.TOKue: | |
5816 nextToken(); | |
5817 e2 = parseShiftExp(); | |
5818 e = new CmpExp(op, loc, e, e2); | |
5819 break; | |
5820 | |
5821 case TOK.TOKin: | |
5822 nextToken(); | |
5823 e2 = parseShiftExp(); | |
5824 e = new InExp(loc, e, e2); | |
5825 break; | |
5826 | |
5827 default: | |
5828 break; | |
5829 } | |
5830 return e; | |
5831 } | |
5832 | |
5833 Expression parseAndExp() | |
5834 { | |
5835 Expression e; | |
5836 Expression e2; | |
5837 Loc loc = this.loc; | |
5838 | |
5839 if (global.params.Dversion == 1) | |
5840 { | |
5841 e = parseEqualExp(); | |
5842 while (token.value == TOK.TOKand) | |
5843 { | |
5844 nextToken(); | |
5845 e2 = parseEqualExp(); | |
5846 e = new AndExp(loc,e,e2); | |
5847 loc = this.loc; | |
5848 } | |
5849 } | |
5850 else | |
5851 { | |
5852 e = parseCmpExp(); | |
5853 while (token.value == TOK.TOKand) | |
5854 { | |
5855 nextToken(); | |
5856 e2 = parseCmpExp(); | |
5857 e = new AndExp(loc,e,e2); | |
5858 loc = this.loc; | |
5859 } | |
5860 } | |
5861 return e; | |
5862 } | |
5863 | |
5864 Expression parseXorExp() | |
5865 { | |
5866 Expression e; | |
5867 Expression e2; | |
5868 Loc loc = this.loc; | |
5869 | |
5870 e = parseAndExp(); | |
5871 while (token.value == TOK.TOKxor) | |
5872 { | |
5873 nextToken(); | |
5874 e2 = parseAndExp(); | |
5875 e = new XorExp(loc, e, e2); | |
5876 } | |
5877 | |
5878 return e; | |
5879 } | |
5880 | |
5881 Expression parseOrExp() | |
5882 { | |
5883 Expression e; | |
5884 Expression e2; | |
5885 Loc loc = this.loc; | |
5886 | |
5887 e = parseXorExp(); | |
5888 while (token.value == TOK.TOKor) | |
5889 { | |
5890 nextToken(); | |
5891 e2 = parseXorExp(); | |
5892 e = new OrExp(loc, e, e2); | |
5893 } | |
5894 return e; | |
5895 } | |
5896 | |
5897 Expression parseAndAndExp() | |
5898 { | |
5899 Expression e; | |
5900 Expression e2; | |
5901 Loc loc = this.loc; | |
5902 | |
5903 e = parseOrExp(); | |
5904 while (token.value == TOK.TOKandand) | |
5905 { | |
5906 nextToken(); | |
5907 e2 = parseOrExp(); | |
5908 e = new AndAndExp(loc, e, e2); | |
5909 } | |
5910 return e; | |
5911 } | |
5912 | |
5913 Expression parseOrOrExp() | |
5914 { | |
5915 Expression e; | |
5916 Expression e2; | |
5917 Loc loc = this.loc; | |
5918 | |
5919 e = parseAndAndExp(); | |
5920 while (token.value == TOK.TOKoror) | |
5921 { | |
5922 nextToken(); | |
5923 e2 = parseAndAndExp(); | |
5924 e = new OrOrExp(loc, e, e2); | |
5925 } | |
5926 | |
5927 return e; | |
5928 } | |
5929 | |
5930 Expression parseCondExp() | |
5931 { | |
5932 Expression e; | |
5933 Expression e1; | |
5934 Expression e2; | |
5935 Loc loc = this.loc; | |
5936 | |
5937 e = parseOrOrExp(); | |
5938 if (token.value == TOK.TOKquestion) | |
5939 { | |
5940 nextToken(); | |
5941 e1 = parseExpression(); | |
5942 check(TOK.TOKcolon); | |
5943 e2 = parseCondExp(); | |
5944 e = new CondExp(loc, e, e1, e2); | |
5945 } | |
5946 return e; | |
5947 } | |
5948 | |
5949 Expression parseAssignExp() | |
5950 { | |
5951 Expression e; | |
5952 Expression e2; | |
5953 Loc loc; | |
5954 | |
5955 e = parseCondExp(); | |
5956 while (1) | |
5957 { | |
5958 loc = this.loc; | |
5959 switch (token.value) | |
5960 { | |
5961 case TOK.TOKassign: nextToken(); e2 = parseAssignExp(); e = new AssignExp(loc,e,e2); continue; | |
5962 case TOK.TOKaddass: nextToken(); e2 = parseAssignExp(); e = new AddAssignExp(loc,e,e2); continue; | |
5963 case TOK.TOKminass: nextToken(); e2 = parseAssignExp(); e = new MinAssignExp(loc,e,e2); continue; | |
5964 case TOK.TOKmulass: nextToken(); e2 = parseAssignExp(); e = new MulAssignExp(loc,e,e2); continue; | |
5965 case TOK.TOKdivass: nextToken(); e2 = parseAssignExp(); e = new DivAssignExp(loc,e,e2); continue; | |
5966 case TOK.TOKmodass: nextToken(); e2 = parseAssignExp(); e = new ModAssignExp(loc,e,e2); continue; | |
5967 case TOK.TOKandass: nextToken(); e2 = parseAssignExp(); e = new AndAssignExp(loc,e,e2); continue; | |
5968 case TOK.TOKorass: nextToken(); e2 = parseAssignExp(); e = new OrAssignExp(loc,e,e2); continue; | |
5969 case TOK.TOKxorass: nextToken(); e2 = parseAssignExp(); e = new XorAssignExp(loc,e,e2); continue; | |
5970 case TOK.TOKshlass: nextToken(); e2 = parseAssignExp(); e = new ShlAssignExp(loc,e,e2); continue; | |
5971 case TOK.TOKshrass: nextToken(); e2 = parseAssignExp(); e = new ShrAssignExp(loc,e,e2); continue; | |
5972 case TOK.TOKushrass: nextToken(); e2 = parseAssignExp(); e = new UshrAssignExp(loc,e,e2); continue; | |
5973 case TOK.TOKcatass: nextToken(); e2 = parseAssignExp(); e = new CatAssignExp(loc,e,e2); continue; | |
5974 | |
5975 default: | |
5976 break; | |
5977 } | |
5978 break; | |
5979 } | |
5980 | |
5981 return e; | |
5982 } | |
5983 | |
5984 /************************* | |
5985 * Collect argument list. | |
5986 * Assume current token is ',', '(' or '['. | |
5987 */ | |
5988 Expressions parseArguments() | |
5989 { | |
5990 // function call | |
5991 Expressions arguments = new Expressions(); | |
5992 Expression arg; | |
5993 TOK endtok; | |
5994 | |
5995 if (token.value == TOK.TOKlbracket) | |
5996 endtok = TOK.TOKrbracket; | |
5997 else | |
5998 endtok = TOK.TOKrparen; | |
5999 | |
6000 { | |
6001 nextToken(); | |
6002 if (token.value != endtok) | |
6003 { | |
6004 while (1) | |
6005 { | |
6006 arg = parseAssignExp(); | |
6007 arguments.push(cast(void*)arg); | |
6008 if (token.value == endtok) | |
6009 break; | |
6010 check(TOK.TOKcomma); | |
6011 } | |
6012 } | |
6013 check(endtok); | |
6014 } | |
6015 return arguments; | |
6016 } | |
6017 | |
6018 Expression parseNewExp(Expression thisexp) | |
6019 { | |
6020 Type t; | |
6021 Expressions newargs; | |
6022 Expressions arguments = null; | |
6023 Expression e; | |
6024 Loc loc = this.loc; | |
6025 | |
6026 nextToken(); | |
6027 newargs = null; | |
6028 if (token.value == TOKlparen) | |
6029 { | |
6030 newargs = parseArguments(); | |
6031 } | |
6032 | |
6033 // An anonymous nested class starts with "class" | |
6034 if (token.value == TOKclass) | |
6035 { | |
6036 nextToken(); | |
6037 if (token.value == TOKlparen) | |
6038 arguments = parseArguments(); | |
6039 | |
6040 BaseClasses baseclasses = null; | |
6041 if (token.value != TOKlcurly) | |
6042 baseclasses = parseBaseClasses(); | |
6043 | |
6044 Identifier id = null; | |
6045 ClassDeclaration cd = new ClassDeclaration(loc, id, baseclasses); | |
6046 | |
6047 if (token.value != TOKlcurly) | |
6048 { | |
6049 error("{ members } expected for anonymous class"); | |
6050 cd.members = null; | |
6051 } | |
6052 else | |
6053 { | |
6054 nextToken(); | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
56
diff
changeset
|
6055 auto decl = parseDeclDefs(0); |
0 | 6056 if (token.value != TOKrcurly) |
6057 error("class member expected"); | |
6058 nextToken(); | |
6059 cd.members = decl; | |
6060 } | |
6061 | |
6062 e = new NewAnonClassExp(loc, thisexp, newargs, cd, arguments); | |
6063 | |
6064 return e; | |
6065 } | |
6066 | |
6067 t = parseBasicType(); | |
6068 t = parseBasicType2(t); | |
6069 if (t.ty == Taarray) | |
6070 { | |
6071 TypeAArray taa = cast(TypeAArray)t; | |
6072 Type index = taa.index; | |
6073 | |
6074 Expression e2 = index.toExpression(); | |
6075 if (e2) | |
6076 { | |
6077 arguments = new Expressions(); | |
6078 arguments.push(cast(void*)e2); | |
6079 t = new TypeDArray(taa.next); | |
6080 } | |
6081 else | |
6082 { | |
6083 error("need size of rightmost array, not type %s", index.toChars()); | |
6084 return new NullExp(loc); | |
6085 } | |
6086 } | |
6087 else if (t.ty == Tsarray) | |
6088 { | |
6089 TypeSArray tsa = cast(TypeSArray)t; | |
6090 Expression ee = tsa.dim; | |
6091 | |
6092 arguments = new Expressions(); | |
6093 arguments.push(cast(void*)ee); | |
6094 t = new TypeDArray(tsa.next); | |
6095 } | |
6096 else if (token.value == TOKlparen) | |
6097 { | |
6098 arguments = parseArguments(); | |
6099 } | |
6100 | |
6101 e = new NewExp(loc, thisexp, newargs, t, arguments); | |
6102 return e; | |
6103 } | |
6104 | |
6105 void addComment(Dsymbol s, ubyte* blockComment) | |
6106 { | |
6107 s.addComment(combineComments(blockComment, token.lineComment)); | |
6108 token.lineComment = null; | |
6109 } | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
6110 } |