Mercurial > projects > ddmd
annotate dmd/Parser.d @ 34:544b922227c7
update to work with dmd 2.048
author | korDen |
---|---|
date | Sat, 21 Aug 2010 05:46:08 +0400 |
parents | 1b81e14880ef |
children | 51605de93870 |
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 | |
225 Array parseModule() | |
226 { | |
227 Array decldefs; | |
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(); | |
302 return new Array(); | |
303 } | |
304 | |
305 Array parseDeclDefs(int once) | |
306 { | |
307 Dsymbol s; | |
308 Array decldefs; | |
309 Array a; | |
310 Array aelse; | |
311 PROT prot; | |
312 STC stc; | |
313 STC storageClass; | |
314 Condition condition; | |
315 ubyte* comment; | |
316 | |
317 //printf("Parser.parseDeclDefs()\n"); | |
318 decldefs = new Array(); | |
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) | |
736 { decldefs.push(cast(void*)s); | |
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) { | |
751 Array parseAutoDeclarations(STC storageClass, ubyte* comment) | |
752 { | |
753 Array a = new Array; | |
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(); | |
762 VarDeclaration v = new VarDeclaration(loc, null, ident, init); | |
763 v.storage_class = storageClass; | |
764 a.push(cast(void*)v); | |
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 */ | |
792 Array parseBlock() | |
793 { | |
794 Array a = null; | |
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; | |
877 Array decldefs; | |
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 | |
1248 Array decldefs = new Array(); | |
1249 decldefs.push(cast(void*)fd); | |
1250 TemplateDeclaration tempdecl = new TemplateDeclaration(fd.loc, fd.ident, tpl, null, decldefs); | |
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 | |
1597 Array decldefs = new Array(); | |
1598 decldefs.push(cast(void*)f); | |
1599 TemplateDeclaration tempdecl = | |
1600 new TemplateDeclaration(loc, f.ident, tpl, constraint, decldefs); | |
1601 return tempdecl; | |
1602 } | |
1603 | |
1604 /* Just a regular constructor | |
1605 */ | |
1606 int varargs; | |
1607 Arguments arguments = parseParameters(&varargs); | |
1608 CtorDeclaration f = new CtorDeclaration(loc, Loc(0), arguments, varargs); | |
1609 parseContracts(f); | |
1610 return f; | |
1611 } | |
1612 | |
1613 PostBlitDeclaration parsePostBlit() | |
1614 { | |
1615 assert(false); | |
1616 } | |
1617 | |
1618 /***************************************** | |
1619 * Parse a destructor definition: | |
1620 * ~this() { body } | |
1621 * Current token is '~'. | |
1622 */ | |
1623 DtorDeclaration parseDtor() | |
1624 { | |
1625 DtorDeclaration f; | |
1626 Loc loc = this.loc; | |
1627 | |
1628 nextToken(); | |
1629 check(TOKthis); | |
1630 check(TOKlparen); | |
1631 check(TOKrparen); | |
1632 | |
1633 f = new DtorDeclaration(loc, Loc(0)); | |
1634 parseContracts(f); | |
1635 return f; | |
1636 } | |
1637 | |
1638 /***************************************** | |
1639 * Parse a static constructor definition: | |
1640 * static this() { body } | |
1641 * Current token is 'this'. | |
1642 */ | |
1643 StaticCtorDeclaration parseStaticCtor() | |
1644 { | |
1645 StaticCtorDeclaration f; | |
1646 Loc loc = this.loc; | |
1647 | |
1648 nextToken(); | |
1649 check(TOKlparen); | |
1650 check(TOKrparen); | |
1651 | |
1652 f = new StaticCtorDeclaration(loc, Loc(0)); | |
1653 parseContracts(f); | |
1654 return f; | |
1655 } | |
1656 | |
1657 /***************************************** | |
1658 * Parse a static destructor definition: | |
1659 * static ~this() { body } | |
1660 * Current token is '~'. | |
1661 */ | |
1662 StaticDtorDeclaration parseStaticDtor() | |
1663 { | |
1664 StaticDtorDeclaration f; | |
1665 Loc loc = this.loc; | |
1666 | |
1667 nextToken(); | |
1668 check(TOKthis); | |
1669 check(TOKlparen); | |
1670 check(TOKrparen); | |
1671 | |
1672 f = new StaticDtorDeclaration(loc, Loc(0)); | |
1673 parseContracts(f); | |
1674 return f; | |
1675 } | |
1676 | |
1677 /***************************************** | |
1678 * Parse an invariant definition: | |
1679 * invariant() { body } | |
1680 * Current token is 'invariant'. | |
1681 */ | |
1682 InvariantDeclaration parseInvariant() | |
1683 { | |
1684 InvariantDeclaration f; | |
1685 Loc loc = this.loc; | |
1686 | |
1687 nextToken(); | |
1688 if (token.value == TOKlparen) // optional () | |
1689 { | |
1690 nextToken(); | |
1691 check(TOKrparen); | |
1692 } | |
1693 | |
1694 f = new InvariantDeclaration(loc, Loc(0)); | |
1695 f.fbody = parseStatement(ParseStatementFlags.PScurly); | |
1696 return f; | |
1697 } | |
1698 | |
1699 /***************************************** | |
1700 * Parse a unittest definition: | |
1701 * unittest { body } | |
1702 * Current token is 'unittest'. | |
1703 */ | |
1704 UnitTestDeclaration parseUnitTest() | |
1705 { | |
1706 Loc loc = this.loc; | |
1707 | |
1708 nextToken(); | |
1709 | |
1710 UnitTestDeclaration f = new UnitTestDeclaration(loc, this.loc); | |
1711 f.fbody = parseStatement(ParseStatementFlags.PScurly); | |
1712 | |
1713 return f; | |
1714 } | |
1715 | |
1716 NewDeclaration parseNew() | |
1717 { | |
1718 assert(false); | |
1719 } | |
1720 | |
1721 DeleteDeclaration parseDelete() | |
1722 { | |
1723 assert(false); | |
1724 } | |
1725 | |
1726 Arguments parseParameters(int* pvarargs) | |
1727 { | |
1728 Arguments arguments = new Arguments(); | |
1729 int varargs = 0; | |
1730 int hasdefault = 0; | |
1731 | |
1732 check(TOK.TOKlparen); | |
1733 while (1) | |
1734 { Type *tb; | |
1735 Identifier ai = null; | |
1736 Type at; | |
1737 Argument a; | |
1738 STC storageClass = STC.STCundefined; | |
1739 STC stc; | |
1740 Expression ae; | |
1741 | |
1742 for ( ;1; nextToken()) | |
1743 { | |
1744 switch (token.value) | |
1745 { | |
1746 case TOK.TOKrparen: | |
1747 break; | |
1748 | |
1749 case TOK.TOKdotdotdot: | |
1750 varargs = 1; | |
1751 nextToken(); | |
1752 break; | |
1753 | |
1754 case TOK.TOKconst: | |
1755 if (peek(&token).value == TOK.TOKlparen) | |
1756 goto Ldefault; | |
1757 stc = STC.STCconst; | |
1758 goto L2; | |
1759 | |
1760 case TOK.TOKinvariant: | |
1761 case TOK.TOKimmutable: | |
1762 if (peek(&token).value == TOK.TOKlparen) | |
1763 goto Ldefault; | |
1764 stc = STC.STCimmutable; | |
1765 goto L2; | |
1766 | |
1767 case TOK.TOKshared: | |
1768 if (peek(&token).value == TOK.TOKlparen) | |
1769 goto Ldefault; | |
1770 stc = STC.STCshared; | |
1771 goto L2; | |
1772 | |
1773 case TOK.TOKin: stc = STC.STCin; goto L2; | |
1774 case TOK.TOKout: stc = STC.STCout; goto L2; | |
1775 case TOK.TOKinout: | |
1776 case TOK.TOKref: stc = STC.STCref; goto L2; | |
1777 case TOK.TOKlazy: stc = STC.STClazy; goto L2; | |
1778 case TOK.TOKscope: stc = STC.STCscope; goto L2; | |
1779 case TOK.TOKfinal: stc = STC.STCfinal; goto L2; | |
1780 L2: | |
1781 if (storageClass & stc || | |
1782 (storageClass & STC.STCin && stc & (STC.STCconst | STC.STCscope)) || | |
1783 (stc & STC.STCin && storageClass & (STC.STCconst | STC.STCscope)) | |
1784 ) | |
1785 error("redundant storage class %s", Token.toChars(token.value)); | |
1786 storageClass |= stc; | |
1787 composeStorageClass(storageClass); | |
1788 continue; | |
1789 | |
1790 static if (false) { | |
1791 case TOK.TOKstatic: stc = STC.STCstatic; goto L2; | |
1792 case TOK.TOKauto: storageClass = STC.STCauto; goto L4; | |
1793 case TOK.TOKalias: storageClass = STC.STCalias; goto L4; | |
1794 L4: | |
1795 nextToken(); | |
1796 if (token.value == TOK.TOKidentifier) | |
1797 { ai = token.ident; | |
1798 nextToken(); | |
1799 } | |
1800 else | |
1801 ai = null; | |
1802 at = null; // no type | |
1803 ae = null; // no default argument | |
1804 if (token.value == TOK.TOKassign) // = defaultArg | |
1805 { nextToken(); | |
1806 ae = parseDefaultInitExp(); | |
1807 hasdefault = 1; | |
1808 } | |
1809 else | |
1810 { if (hasdefault) | |
1811 error("default argument expected for alias %s", | |
1812 ai ? ai.toChars() : ""); | |
1813 } | |
1814 goto L3; | |
1815 } | |
1816 | |
1817 default: | |
1818 Ldefault: | |
1819 stc = (storageClass & (STC.STCin | STC.STCout | STC.STCref | STC.STClazy)); | |
1820 if (stc & (stc - 1)) // if stc is not a power of 2 | |
1821 error("incompatible parameter storage classes"); | |
1822 if ((storageClass & (STC.STCconst | STC.STCout)) == (STC.STCconst | STC.STCout)) | |
1823 error("out cannot be const"); | |
1824 if ((storageClass & (STC.STCimmutable | STC.STCout)) == (STC.STCimmutable | STC.STCout)) | |
1825 error("out cannot be immutable"); | |
1826 if ((storageClass & STC.STCscope) && | |
1827 (storageClass & (STC.STCref | STC.STCout))) | |
1828 error("scope cannot be ref or out"); | |
1829 at = parseType(&ai); | |
1830 ae = null; | |
1831 if (token.value == TOK.TOKassign) // = defaultArg | |
1832 { nextToken(); | |
1833 ae = parseDefaultInitExp(); | |
1834 hasdefault = 1; | |
1835 } | |
1836 else | |
1837 { if (hasdefault) | |
1838 error("default argument expected for %s", | |
1839 ai ? ai.toChars() : at.toChars()); | |
1840 } | |
1841 if (token.value == TOK.TOKdotdotdot) | |
1842 { /* This is: | |
1843 * at ai ... | |
1844 */ | |
1845 | |
1846 if (storageClass & (STC.STCout | STC.STCref)) | |
1847 error("variadic argument cannot be out or ref"); | |
1848 varargs = 2; | |
1849 a = new Argument(storageClass, at, ai, ae); | |
1850 arguments.push(cast(void*)a); | |
1851 nextToken(); | |
1852 break; | |
1853 } | |
1854 L3: | |
1855 a = new Argument(storageClass, at, ai, ae); | |
1856 arguments.push(cast(void*)a); | |
1857 if (token.value == TOK.TOKcomma) | |
1858 { nextToken(); | |
1859 goto L1; | |
1860 } | |
1861 break; | |
1862 } | |
1863 break; | |
1864 } | |
1865 break; | |
1866 | |
1867 L1: ; | |
1868 } | |
1869 check(TOK.TOKrparen); | |
1870 *pvarargs = varargs; | |
1871 return arguments; | |
1872 } | |
1873 | |
1874 EnumDeclaration parseEnum() | |
1875 { | |
1876 EnumDeclaration e; | |
1877 Identifier id; | |
1878 Type memtype; | |
1879 Loc loc = this.loc; | |
1880 | |
1881 //printf("Parser.parseEnum()\n"); | |
1882 nextToken(); | |
1883 if (token.value == TOK.TOKidentifier) | |
1884 { | |
1885 id = token.ident; | |
1886 nextToken(); | |
1887 } | |
1888 else | |
1889 id = null; | |
1890 | |
1891 if (token.value == TOK.TOKcolon) | |
1892 { | |
1893 nextToken(); | |
1894 memtype = parseBasicType(); | |
1895 memtype = parseDeclarator(memtype, null, null); | |
1896 } | |
1897 else | |
1898 memtype = null; | |
1899 | |
1900 e = new EnumDeclaration(loc, id, memtype); | |
1901 if (token.value == TOK.TOKsemicolon && id) | |
1902 nextToken(); | |
1903 else if (token.value == TOK.TOKlcurly) | |
1904 { | |
1905 //printf("enum definition\n"); | |
1906 e.members = new Array(); | |
1907 nextToken(); | |
1908 ubyte* comment = token.blockComment; | |
1909 while (token.value != TOK.TOKrcurly) | |
1910 { | |
1911 /* Can take the following forms: | |
1912 * 1. ident | |
1913 * 2. ident = value | |
1914 * 3. type ident = value | |
1915 */ | |
1916 | |
1917 loc = this.loc; | |
1918 | |
1919 Type type = null; | |
1920 Identifier ident; | |
1921 Token* tp = peek(&token); | |
1922 if (token.value == TOK.TOKidentifier && | |
1923 (tp.value == TOK.TOKassign || tp.value == TOK.TOKcomma || tp.value == TOK.TOKrcurly)) | |
1924 { | |
1925 ident = token.ident; | |
1926 type = null; | |
1927 nextToken(); | |
1928 } | |
1929 else | |
1930 { | |
1931 type = parseType(&ident, null); | |
1932 if (id || memtype) | |
1933 error("type only allowed if anonymous enum and no enum type"); | |
1934 } | |
1935 | |
1936 Expression value; | |
1937 if (token.value == TOK.TOKassign) | |
1938 { | |
1939 nextToken(); | |
1940 value = parseAssignExp(); | |
1941 } | |
1942 else | |
1943 { | |
1944 value = null; | |
1945 if (type) | |
1946 error("if type, there must be an initializer"); | |
1947 } | |
1948 | |
1949 EnumMember em = new EnumMember(loc, ident, value, type); | |
1950 e.members.push(cast(void*)em); | |
1951 | |
1952 if (token.value == TOK.TOKrcurly) { | |
1953 ; | |
1954 } else { | |
1955 addComment(em, comment); | |
1956 comment = null; | |
1957 check(TOK.TOKcomma); | |
1958 } | |
1959 addComment(em, comment); | |
1960 comment = token.blockComment; | |
1961 } | |
1962 nextToken(); | |
1963 } | |
1964 else | |
1965 error("enum declaration is invalid"); | |
1966 | |
1967 //printf("-parseEnum() %s\n", e.toChars()); | |
1968 return e; | |
1969 } | |
1970 | |
1971 Dsymbol parseAggregate() | |
1972 { | |
1973 AggregateDeclaration a = null; | |
1974 int anon = 0; | |
1975 TOK tok; | |
1976 Identifier id; | |
1977 TemplateParameters tpl = null; | |
1978 Expression constraint = null; | |
1979 | |
1980 //printf("Parser.parseAggregate()\n"); | |
1981 tok = token.value; | |
1982 nextToken(); | |
1983 if (token.value != TOK.TOKidentifier) | |
1984 { | |
1985 id = null; | |
1986 } | |
1987 else | |
1988 { | |
1989 id = token.ident; | |
1990 nextToken(); | |
1991 | |
1992 if (token.value == TOK.TOKlparen) | |
1993 { | |
1994 // Class template declaration. | |
1995 | |
1996 // Gather template parameter list | |
1997 tpl = parseTemplateParameterList(); | |
1998 constraint = parseConstraint(); | |
1999 } | |
2000 } | |
2001 | |
2002 Loc loc = this.loc; | |
2003 switch (tok) | |
2004 { case TOK.TOKclass: | |
2005 case TOK.TOKinterface: | |
2006 { | |
2007 if (!id) | |
2008 error("anonymous classes not allowed"); | |
2009 | |
2010 // Collect base class(es) | |
2011 BaseClasses baseclasses = null; | |
2012 if (token.value == TOK.TOKcolon) | |
2013 { | |
2014 nextToken(); | |
2015 baseclasses = parseBaseClasses(); | |
2016 | |
2017 if (token.value != TOK.TOKlcurly) | |
2018 error("members expected"); | |
2019 } | |
2020 | |
2021 if (tok == TOK.TOKclass) | |
2022 a = new ClassDeclaration(loc, id, baseclasses); | |
2023 else | |
2024 a = new InterfaceDeclaration(loc, id, baseclasses); | |
2025 break; | |
2026 } | |
2027 | |
2028 case TOK.TOKstruct: | |
2029 if (id) | |
2030 a = new StructDeclaration(loc, id); | |
2031 else | |
2032 anon = 1; | |
2033 break; | |
2034 | |
2035 case TOK.TOKunion: | |
2036 if (id) | |
2037 a = new UnionDeclaration(loc, id); | |
2038 else | |
2039 anon = 2; | |
2040 break; | |
2041 | |
2042 default: | |
2043 assert(0); | |
2044 break; | |
2045 } | |
2046 if (a && token.value == TOK.TOKsemicolon) | |
2047 { nextToken(); | |
2048 } | |
2049 else if (token.value == TOK.TOKlcurly) | |
2050 { | |
2051 //printf("aggregate definition\n"); | |
2052 nextToken(); | |
2053 Array decl = parseDeclDefs(0); | |
2054 if (token.value != TOK.TOKrcurly) | |
2055 error("} expected following member declarations in aggregate"); | |
2056 nextToken(); | |
2057 if (anon) | |
2058 { | |
2059 /* Anonymous structs/unions are more like attributes. | |
2060 */ | |
2061 return new AnonDeclaration(loc, anon - 1, decl); | |
2062 } | |
2063 else | |
2064 a.members = decl; | |
2065 } | |
2066 else | |
2067 { | |
2068 error("{ } expected following aggregate declaration"); | |
2069 a = new StructDeclaration(loc, null); | |
2070 } | |
2071 | |
2072 if (tpl) | |
2073 { // Wrap a template around the aggregate declaration | |
2074 | |
2075 Array decldefs = new Array(); | |
2076 decldefs.push(cast(void*)a); | |
2077 TemplateDeclaration tempdecl = | |
2078 new TemplateDeclaration(loc, id, tpl, constraint, decldefs); | |
2079 return tempdecl; | |
2080 } | |
2081 | |
2082 return a; | |
2083 } | |
2084 | |
2085 BaseClasses parseBaseClasses() | |
2086 { | |
2087 BaseClasses baseclasses = new BaseClasses(); | |
2088 | |
2089 for (; 1; nextToken()) | |
2090 { | |
2091 PROT protection = PROT.PROTpublic; | |
2092 switch (token.value) | |
2093 { | |
2094 case TOK.TOKprivate: | |
2095 protection = PROT.PROTprivate; | |
2096 nextToken(); | |
2097 break; | |
2098 case TOK.TOKpackage: | |
2099 protection = PROT.PROTpackage; | |
2100 nextToken(); | |
2101 break; | |
2102 case TOK.TOKprotected: | |
2103 protection = PROT.PROTprotected; | |
2104 nextToken(); | |
2105 break; | |
2106 case TOK.TOKpublic: | |
2107 protection = PROT.PROTpublic; | |
2108 nextToken(); | |
2109 break; | |
2110 default: | |
2111 break; /// | |
2112 } | |
2113 if (token.value == TOK.TOKidentifier) | |
2114 { | |
2115 BaseClass b = new BaseClass(parseBasicType(), protection); | |
2116 baseclasses.push(cast(void*)b); | |
2117 if (token.value != TOK.TOKcomma) | |
2118 break; | |
2119 } | |
2120 else | |
2121 { | |
2122 error("base classes expected instead of %s", token.toChars()); | |
2123 return null; | |
2124 } | |
2125 } | |
2126 return baseclasses; | |
2127 } | |
2128 | |
2129 Import parseImport(Array decldefs, int isstatic) | |
2130 { | |
2131 Import s; | |
2132 Identifier id; | |
2133 Identifier aliasid = null; | |
2134 Array a; | |
2135 Loc loc; | |
2136 | |
2137 //printf("Parser.parseImport()\n"); | |
2138 do | |
2139 { | |
2140 L1: | |
2141 nextToken(); | |
2142 if (token.value != TOK.TOKidentifier) | |
2143 { | |
2144 error("Identifier expected following import"); | |
2145 break; | |
2146 } | |
2147 | |
2148 loc = this.loc; | |
2149 a = null; | |
2150 id = token.ident; | |
2151 nextToken(); | |
2152 if (!aliasid && token.value == TOK.TOKassign) | |
2153 { | |
2154 aliasid = id; | |
2155 goto L1; | |
2156 } | |
2157 while (token.value == TOK.TOKdot) | |
2158 { | |
2159 if (!a) | |
2160 a = new Array(); | |
2161 a.push(cast(void*)id); | |
2162 nextToken(); | |
2163 if (token.value != TOK.TOKidentifier) | |
2164 { | |
2165 error("identifier expected following package"); | |
2166 break; | |
2167 } | |
2168 id = token.ident; | |
2169 nextToken(); | |
2170 } | |
2171 | |
2172 s = new Import(loc, a, id, aliasid, isstatic); | |
2173 decldefs.push(cast(void*)s); | |
2174 | |
2175 /* Look for | |
2176 * : alias=name, alias=name; | |
2177 * syntax. | |
2178 */ | |
2179 if (token.value == TOK.TOKcolon) | |
2180 { | |
2181 do | |
2182 { | |
2183 Identifier name; | |
2184 | |
2185 nextToken(); | |
2186 if (token.value != TOK.TOKidentifier) | |
2187 { | |
2188 error("Identifier expected following :"); | |
2189 break; | |
2190 } | |
2191 Identifier alias_ = token.ident; | |
2192 nextToken(); | |
2193 if (token.value == TOK.TOKassign) | |
2194 { | |
2195 nextToken(); | |
2196 if (token.value != TOK.TOKidentifier) | |
2197 { | |
2198 error("Identifier expected following %s=", alias_.toChars()); | |
2199 break; | |
2200 } | |
2201 name = token.ident; | |
2202 nextToken(); | |
2203 } | |
2204 else | |
2205 { | |
2206 name = alias_; | |
2207 alias_ = null; | |
2208 } | |
2209 s.addAlias(name, alias_); | |
2210 } while (token.value == TOK.TOKcomma); | |
2211 | |
2212 break; // no comma-separated imports of this form | |
2213 } | |
2214 | |
2215 aliasid = null; | |
2216 | |
2217 } while (token.value == TOK.TOKcomma); | |
2218 | |
2219 if (token.value == TOK.TOKsemicolon) | |
2220 nextToken(); | |
2221 else | |
2222 { | |
2223 error("';' expected"); | |
2224 nextToken(); | |
2225 } | |
2226 | |
2227 return null; | |
2228 } | |
2229 | |
2230 Type parseType(Identifier* pident = null, TemplateParameters* tpl = null) | |
2231 { | |
2232 Type t; | |
2233 | |
2234 /* Take care of the storage class prefixes that | |
2235 * serve as type attributes: | |
2236 * const shared, shared const, const, invariant, shared | |
2237 */ | |
2238 if (token.value == TOK.TOKconst && peekNext() == TOK.TOKshared && peekNext2() != TOK.TOKlparen || | |
2239 token.value == TOK.TOKshared && peekNext() == TOK.TOKconst && peekNext2() != TOK.TOKlparen) | |
2240 { | |
2241 nextToken(); | |
2242 nextToken(); | |
2243 /* shared const type | |
2244 */ | |
2245 t = parseType(pident, tpl); | |
2246 t = t.makeSharedConst(); | |
2247 return t; | |
2248 } | |
2249 else if (token.value == TOK.TOKconst && peekNext() != TOK.TOKlparen) | |
2250 { | |
2251 nextToken(); | |
2252 /* const type | |
2253 */ | |
2254 t = parseType(pident, tpl); | |
2255 t = t.makeConst(); | |
2256 return t; | |
2257 } | |
2258 else if ((token.value == TOK.TOKinvariant || token.value == TOK.TOKimmutable) && | |
2259 peekNext() != TOK.TOKlparen) | |
2260 { | |
2261 nextToken(); | |
2262 /* invariant type | |
2263 */ | |
2264 t = parseType(pident, tpl); | |
2265 t = t.makeInvariant(); | |
2266 return t; | |
2267 } | |
2268 else if (token.value == TOK.TOKshared && peekNext() != TOK.TOKlparen) | |
2269 { | |
2270 nextToken(); | |
2271 /* shared type | |
2272 */ | |
2273 t = parseType(pident, tpl); | |
2274 t = t.makeShared(); | |
2275 return t; | |
2276 } | |
2277 else | |
2278 t = parseBasicType(); | |
2279 t = parseDeclarator(t, pident, tpl); | |
2280 return t; | |
2281 } | |
2282 | |
2283 Type parseBasicType() | |
2284 { | |
2285 Type t; | |
2286 Identifier id; | |
2287 TypeQualified tid; | |
2288 | |
2289 //printf("parseBasicType()\n"); | |
2290 switch (token.value) | |
2291 { | |
2292 case TOK.TOKvoid: t = Type.tvoid; goto LabelX; | |
2293 case TOK.TOKint8: t = Type.tint8; goto LabelX; | |
2294 case TOK.TOKuns8: t = Type.tuns8; goto LabelX; | |
2295 case TOK.TOKint16: t = Type.tint16; goto LabelX; | |
2296 case TOK.TOKuns16: t = Type.tuns16; goto LabelX; | |
2297 case TOK.TOKint32: t = Type.tint32; goto LabelX; | |
2298 case TOK.TOKuns32: t = Type.tuns32; goto LabelX; | |
2299 case TOK.TOKint64: t = Type.tint64; goto LabelX; | |
2300 case TOK.TOKuns64: t = Type.tuns64; goto LabelX; | |
2301 case TOK.TOKfloat32: t = Type.tfloat32; goto LabelX; | |
2302 case TOK.TOKfloat64: t = Type.tfloat64; goto LabelX; | |
2303 case TOK.TOKfloat80: t = Type.tfloat80; goto LabelX; | |
2304 case TOK.TOKimaginary32: t = Type.timaginary32; goto LabelX; | |
2305 case TOK.TOKimaginary64: t = Type.timaginary64; goto LabelX; | |
2306 case TOK.TOKimaginary80: t = Type.timaginary80; goto LabelX; | |
2307 case TOK.TOKcomplex32: t = Type.tcomplex32; goto LabelX; | |
2308 case TOK.TOKcomplex64: t = Type.tcomplex64; goto LabelX; | |
2309 case TOK.TOKcomplex80: t = Type.tcomplex80; goto LabelX; | |
2310 case TOK.TOKbit: t = Type.tbit; goto LabelX; | |
2311 case TOK.TOKbool: t = Type.tbool; goto LabelX; | |
2312 case TOK.TOKchar: t = Type.tchar; goto LabelX; | |
2313 case TOK.TOKwchar: t = Type.twchar; goto LabelX; | |
2314 case TOK.TOKdchar: t = Type.tdchar; goto LabelX; | |
2315 LabelX: | |
2316 nextToken(); | |
2317 break; | |
2318 | |
2319 case TOK.TOKidentifier: | |
2320 id = token.ident; | |
2321 nextToken(); | |
2322 if (token.value == TOK.TOKnot) | |
2323 { // ident!(template_arguments) | |
2324 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
2325 nextToken(); | |
2326 if (token.value == TOK.TOKlparen) | |
2327 // ident!(template_arguments) | |
2328 tempinst.tiargs = parseTemplateArgumentList(); | |
2329 else | |
2330 // ident!template_argument | |
2331 tempinst.tiargs = parseTemplateArgument(); | |
2332 tid = new TypeInstance(loc, tempinst); | |
2333 goto Lident2; | |
2334 } | |
2335 Lident: | |
2336 tid = new TypeIdentifier(loc, id); | |
2337 Lident2: | |
2338 while (token.value == TOK.TOKdot) | |
2339 { nextToken(); | |
2340 if (token.value != TOK.TOKidentifier) | |
2341 { error("identifier expected following '.' instead of '%s'", token.toChars()); | |
2342 break; | |
2343 } | |
2344 id = token.ident; | |
2345 nextToken(); | |
2346 if (token.value == TOK.TOKnot) | |
2347 { | |
2348 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
2349 nextToken(); | |
2350 if (token.value == TOK.TOKlparen) | |
2351 // ident!(template_arguments) | |
2352 tempinst.tiargs = parseTemplateArgumentList(); | |
2353 else | |
2354 // ident!template_argument | |
2355 tempinst.tiargs = parseTemplateArgument(); | |
2356 tid.addIdent(cast(Identifier)tempinst); | |
2357 } | |
2358 else | |
2359 tid.addIdent(id); | |
2360 } | |
2361 t = tid; | |
2362 break; | |
2363 | |
2364 case TOK.TOKdot: | |
2365 // Leading . as in .foo | |
2366 id = Id.empty; | |
2367 goto Lident; | |
2368 | |
2369 case TOK.TOKtypeof: | |
2370 // typeof(expression) | |
2371 tid = parseTypeof(); | |
2372 goto Lident2; | |
2373 | |
2374 case TOK.TOKconst: | |
2375 // const(type) | |
2376 nextToken(); | |
2377 check(TOK.TOKlparen); | |
2378 t = parseType(); | |
2379 check(TOK.TOKrparen); | |
2380 if (t.isShared()) | |
2381 t = t.makeSharedConst(); | |
2382 else | |
2383 t = t.makeConst(); | |
2384 break; | |
2385 | |
2386 case TOK.TOKinvariant: | |
2387 case TOK.TOKimmutable: | |
2388 // invariant(type) | |
2389 nextToken(); | |
2390 check(TOK.TOKlparen); | |
2391 t = parseType(); | |
2392 check(TOK.TOKrparen); | |
2393 t = t.makeInvariant(); | |
2394 break; | |
2395 | |
2396 case TOK.TOKshared: | |
2397 // shared(type) | |
2398 nextToken(); | |
2399 check(TOK.TOKlparen); | |
2400 t = parseType(); | |
2401 check(TOK.TOKrparen); | |
2402 if (t.isConst()) | |
2403 t = t.makeSharedConst(); | |
2404 else | |
2405 t = t.makeShared(); | |
2406 break; | |
2407 | |
2408 default: | |
2409 error("basic type expected, not %s", token.toChars()); | |
2410 t = Type.tint32; | |
2411 break; | |
2412 } | |
2413 return t; | |
2414 } | |
2415 | |
2416 Type parseBasicType2(Type t) | |
2417 { | |
2418 //printf("parseBasicType2()\n"); | |
2419 while (1) | |
2420 { | |
2421 switch (token.value) | |
2422 { | |
2423 case TOK.TOKmul: | |
2424 t = new TypePointer(t); | |
2425 nextToken(); | |
2426 continue; | |
2427 | |
2428 case TOK.TOKlbracket: | |
2429 // Handle []. Make sure things like | |
2430 // int[3][1] a; | |
2431 // is (array[1] of array[3] of int) | |
2432 nextToken(); | |
2433 if (token.value == TOK.TOKrbracket) | |
2434 { | |
2435 t = new TypeDArray(t); // [] | |
2436 nextToken(); | |
2437 } | |
2438 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) | |
2439 { // It's an associative array declaration | |
2440 | |
2441 //printf("it's an associative array\n"); | |
2442 Type index = parseType(); // [ type ] | |
2443 t = new TypeAArray(t, index); | |
2444 check(TOK.TOKrbracket); | |
2445 } | |
2446 else | |
2447 { | |
2448 //printf("it's type[expression]\n"); | |
2449 inBrackets++; | |
2450 Expression e = parseExpression(); // [ expression ] | |
2451 if (token.value == TOK.TOKslice) | |
2452 { | |
2453 nextToken(); | |
2454 Expression e2 = parseExpression(); // [ exp .. exp ] | |
2455 t = new TypeSlice(t, e, e2); | |
2456 } | |
2457 else | |
2458 t = new TypeSArray(t,e); | |
2459 inBrackets--; | |
2460 check(TOK.TOKrbracket); | |
2461 } | |
2462 continue; | |
2463 | |
2464 case TOK.TOKdelegate: | |
2465 case TOK.TOKfunction: | |
2466 { // Handle delegate declaration: | |
2467 // t delegate(parameter list) nothrow pure | |
2468 // t function(parameter list) nothrow pure | |
2469 Arguments arguments; | |
2470 int varargs; | |
2471 bool ispure = false; | |
2472 bool isnothrow = false; | |
2473 TOK save = token.value; | |
2474 | |
2475 nextToken(); | |
2476 arguments = parseParameters(&varargs); | |
2477 while (1) | |
2478 { // Postfixes | |
2479 if (token.value == TOK.TOKpure) | |
2480 ispure = true; | |
2481 else if (token.value == TOK.TOKnothrow) | |
2482 isnothrow = true; | |
2483 else | |
2484 break; | |
2485 nextToken(); | |
2486 } | |
2487 TypeFunction tf = new TypeFunction(arguments, t, varargs, linkage); | |
2488 tf.ispure = ispure; | |
2489 tf.isnothrow = isnothrow; | |
2490 if (save == TOK.TOKdelegate) | |
2491 t = new TypeDelegate(tf); | |
2492 else | |
2493 t = new TypePointer(tf); // pointer to function | |
2494 continue; | |
2495 } | |
2496 | |
2497 default: | |
2498 return t; | |
2499 } | |
2500 assert(0); | |
2501 } | |
2502 assert(0); | |
2503 return null; | |
2504 } | |
2505 | |
2506 Type parseDeclarator(Type t, Identifier* pident, TemplateParameters* tpl = null) | |
2507 { | |
2508 Type ts; | |
2509 | |
2510 //printf("parseDeclarator(tpl = %p)\n", tpl); | |
2511 t = parseBasicType2(t); | |
2512 | |
2513 switch (token.value) | |
2514 { | |
2515 | |
2516 case TOK.TOKidentifier: | |
2517 if (pident) | |
2518 *pident = token.ident; | |
2519 else | |
2520 error("unexpected identifer '%s' in declarator", token.ident.toChars()); | |
2521 ts = t; | |
2522 nextToken(); | |
2523 break; | |
2524 | |
2525 case TOK.TOKlparen: | |
2526 /* Parse things with parentheses around the identifier, like: | |
2527 * int (*ident[3])[] | |
2528 * although the D style would be: | |
2529 * int[]*[3] ident | |
2530 */ | |
2531 nextToken(); | |
2532 ts = parseDeclarator(t, pident); | |
2533 check(TOK.TOKrparen); | |
2534 break; | |
2535 | |
2536 default: | |
2537 ts = t; | |
2538 break; | |
2539 } | |
2540 | |
2541 // parse DeclaratorSuffixes | |
2542 while (1) | |
2543 { | |
2544 switch (token.value) | |
2545 { | |
2546 version (CARRAYDECL) { | |
2547 /* Support C style array syntax: | |
2548 * int ident[] | |
2549 * as opposed to D-style: | |
2550 * int[] ident | |
2551 */ | |
2552 case TOK.TOKlbracket: | |
2553 { // This is the old C-style post [] syntax. | |
2554 TypeNext ta; | |
2555 nextToken(); | |
2556 if (token.value == TOK.TOKrbracket) | |
2557 { // It's a dynamic array | |
2558 ta = new TypeDArray(t); // [] | |
2559 nextToken(); | |
2560 } | |
2561 else if (isDeclaration(&token, 0, TOK.TOKrbracket, null)) | |
2562 { // It's an associative array | |
2563 | |
2564 //printf("it's an associative array\n"); | |
2565 Type index = parseType(); // [ type ] | |
2566 check(TOK.TOKrbracket); | |
2567 ta = new TypeAArray(t, index); | |
2568 } | |
2569 else | |
2570 { | |
2571 //printf("It's a static array\n"); | |
2572 Expression e = parseExpression(); // [ expression ] | |
2573 ta = new TypeSArray(t, e); | |
2574 check(TOK.TOKrbracket); | |
2575 } | |
2576 | |
2577 /* Insert ta into | |
2578 * ts . ... . t | |
2579 * so that | |
2580 * ts . ... . ta . t | |
2581 */ | |
2582 Type* pt; | |
2583 for (pt = &ts; *pt !is t; pt = &(cast(TypeNext)*pt).next) { | |
2584 ; | |
2585 } | |
2586 *pt = ta; | |
2587 continue; | |
2588 } | |
2589 } | |
2590 case TOK.TOKlparen: | |
2591 { | |
2592 if (tpl) | |
2593 { | |
2594 /* Look ahead to see if this is (...)(...), | |
2595 * i.e. a function template declaration | |
2596 */ | |
2597 if (peekPastParen(&token).value == TOK.TOKlparen) | |
2598 { | |
2599 //printf("function template declaration\n"); | |
2600 | |
2601 // Gather template parameter list | |
2602 *tpl = parseTemplateParameterList(); | |
2603 } | |
2604 } | |
2605 | |
2606 int varargs; | |
2607 Arguments arguments = parseParameters(&varargs); | |
2608 Type tf = new TypeFunction(arguments, t, varargs, linkage); | |
2609 | |
2610 /* Parse const/invariant/nothrow/pure postfix | |
2611 */ | |
2612 while (1) | |
2613 { | |
2614 switch (token.value) | |
2615 { | |
2616 case TOK.TOKconst: | |
2617 if (tf.isShared()) | |
2618 tf = tf.makeSharedConst(); | |
2619 else | |
2620 tf = tf.makeConst(); | |
2621 nextToken(); | |
2622 continue; | |
2623 | |
2624 case TOK.TOKinvariant: | |
2625 case TOK.TOKimmutable: | |
2626 tf = tf.makeInvariant(); | |
2627 nextToken(); | |
2628 continue; | |
2629 | |
2630 case TOK.TOKshared: | |
2631 if (tf.isConst()) | |
2632 tf = tf.makeSharedConst(); | |
2633 else | |
2634 tf = tf.makeShared(); | |
2635 nextToken(); | |
2636 continue; | |
2637 | |
2638 case TOK.TOKnothrow: | |
2639 (cast(TypeFunction)tf).isnothrow = 1; | |
2640 nextToken(); | |
2641 continue; | |
2642 | |
2643 case TOK.TOKpure: | |
2644 (cast(TypeFunction)tf).ispure = 1; | |
2645 nextToken(); | |
2646 continue; | |
2647 | |
2648 case TOK.TOKat: | |
2649 nextToken(); | |
2650 if (token.value != TOK.TOKidentifier) | |
2651 { error("attribute identifier expected"); | |
2652 nextToken(); | |
2653 continue; | |
2654 } | |
2655 Identifier id = token.ident; | |
2656 if (id is Id.property) | |
2657 (cast(TypeFunction)tf).ispure = 1; | |
2658 else | |
2659 error("valid attribute identifiers are property, not %s", id.toChars()); | |
2660 nextToken(); | |
2661 continue; | |
2662 default: | |
2663 break; /// | |
2664 } | |
2665 break; | |
2666 } | |
2667 | |
2668 /* Insert tf into | |
2669 * ts . ... . t | |
2670 * so that | |
2671 * ts . ... . tf . t | |
2672 */ | |
2673 Type* pt; | |
2674 for (pt = &ts; *pt !is t; pt = &(cast(TypeNext)*pt).next) { | |
2675 ; | |
2676 } | |
2677 *pt = tf; | |
2678 break; | |
2679 } | |
2680 | |
2681 default: | |
2682 break; /// | |
2683 } | |
2684 break; | |
2685 } | |
2686 | |
2687 return ts; | |
2688 } | |
2689 | |
2690 Array parseDeclarations(STC storage_class) | |
2691 { | |
2692 STC stc; | |
2693 Type ts; | |
2694 Type t; | |
2695 Type tfirst; | |
2696 Identifier ident; | |
2697 Array a; | |
2698 TOK tok = TOK.TOKreserved; | |
2699 ubyte* comment = token.blockComment; | |
2700 LINK link = linkage; | |
2701 | |
2702 //printf("parseDeclarations() %s\n", token.toChars()); | |
2703 if (storage_class) | |
2704 { ts = null; // infer type | |
2705 goto L2; | |
2706 } | |
2707 | |
2708 switch (token.value) | |
2709 { | |
2710 case TOK.TOKalias: | |
2711 /* Look for: | |
2712 * alias identifier this; | |
2713 */ | |
2714 tok = token.value; | |
2715 nextToken(); | |
2716 if (token.value == TOK.TOKidentifier && peek(&token).value == TOK.TOKthis) | |
2717 { | |
2718 AliasThis s = new AliasThis(this.loc, token.ident); | |
2719 nextToken(); | |
2720 check(TOK.TOKthis); | |
2721 check(TOK.TOKsemicolon); | |
2722 a = new Array(); | |
2723 a.push(cast(void*)s); | |
2724 addComment(s, comment); | |
2725 return a; | |
2726 } | |
2727 break; | |
2728 case TOK.TOKtypedef: | |
2729 tok = token.value; | |
2730 nextToken(); | |
2731 break; | |
2732 default: | |
2733 break; | |
2734 } | |
2735 | |
2736 storage_class = STC.STCundefined; | |
2737 while (1) | |
2738 { | |
2739 switch (token.value) | |
2740 { | |
2741 case TOK.TOKconst: | |
2742 if (peek(&token).value == TOK.TOKlparen) | |
2743 break; // const as type constructor | |
2744 stc = STC.STCconst; // const as storage class | |
2745 goto L1; | |
2746 | |
2747 case TOK.TOKinvariant: | |
2748 case TOK.TOKimmutable: | |
2749 if (peek(&token).value == TOK.TOKlparen) | |
2750 break; | |
2751 stc = STC.STCimmutable; | |
2752 goto L1; | |
2753 | |
2754 case TOK.TOKshared: | |
2755 if (peek(&token).value == TOK.TOKlparen) | |
2756 break; | |
2757 stc = STC.STCshared; | |
2758 goto L1; | |
2759 | |
2760 case TOK.TOKstatic: stc = STC.STCstatic; goto L1; | |
2761 case TOK.TOKfinal: stc = STC.STCfinal; goto L1; | |
2762 case TOK.TOKauto: stc = STC.STCauto; goto L1; | |
2763 case TOK.TOKscope: stc = STC.STCscope; goto L1; | |
2764 case TOK.TOKoverride: stc = STC.STCoverride; goto L1; | |
2765 case TOK.TOKabstract: stc = STC.STCabstract; goto L1; | |
2766 case TOK.TOKsynchronized: stc = STC.STCsynchronized; goto L1; | |
2767 case TOK.TOKdeprecated: stc = STC.STCdeprecated; goto L1; | |
2768 version (DMDV2) { | |
2769 case TOK.TOKnothrow: stc = STC.STCnothrow; goto L1; | |
2770 case TOK.TOKpure: stc = STC.STCpure; goto L1; | |
2771 case TOK.TOKref: stc = STC.STCref; goto L1; | |
2772 case TOK.TOKtls: stc = STC.STCtls; goto L1; | |
2773 case TOK.TOKgshared: stc = STC.STCgshared; goto L1; | |
2774 case TOK.TOKenum: stc = STC.STCmanifest; goto L1; | |
2775 } | |
2776 L1: | |
2777 if (storage_class & stc) | |
2778 error("redundant storage class '%s'", token.toChars()); | |
2779 storage_class = (storage_class | stc); | |
2780 composeStorageClass(storage_class); | |
2781 nextToken(); | |
2782 continue; | |
2783 | |
2784 case TOK.TOKextern: | |
2785 if (peek(&token).value != TOK.TOKlparen) | |
2786 { stc = STC.STCextern; | |
2787 goto L1; | |
2788 } | |
2789 | |
2790 link = parseLinkage(); | |
2791 continue; | |
2792 | |
2793 default: | |
2794 break; | |
2795 } | |
2796 break; | |
2797 } | |
2798 | |
2799 /* Look for auto initializers: | |
2800 * storage_class identifier = initializer; | |
2801 */ | |
2802 if (storage_class && | |
2803 token.value == TOK.TOKidentifier && | |
2804 peek(&token).value == TOK.TOKassign) | |
2805 { | |
2806 return parseAutoDeclarations(storage_class, comment); | |
2807 } | |
2808 | |
2809 if (token.value == TOK.TOKclass) | |
2810 { | |
2811 AggregateDeclaration s = cast(AggregateDeclaration)parseAggregate(); | |
2812 s.storage_class |= storage_class; | |
2813 a = new Array(); | |
2814 a.push(cast(void*)s); | |
2815 addComment(s, comment); | |
2816 return a; | |
2817 } | |
2818 | |
2819 /* Look for return type inference for template functions. | |
2820 */ | |
2821 { | |
2822 Token *tk; | |
2823 if (storage_class && | |
2824 token.value == TOK.TOKidentifier && | |
2825 (tk = peek(&token)).value == TOK.TOKlparen && | |
2826 skipParens(tk, &tk) && | |
2827 peek(tk).value == TOK.TOKlparen) | |
2828 { | |
2829 ts = null; | |
2830 } | |
2831 else | |
2832 { | |
2833 ts = parseBasicType(); | |
2834 ts = parseBasicType2(ts); | |
2835 } | |
2836 } | |
2837 | |
2838 L2: | |
2839 tfirst = null; | |
2840 a = new Array(); | |
2841 | |
2842 while (1) | |
2843 { | |
2844 Loc loc = this.loc; | |
2845 TemplateParameters tpl = null; | |
2846 | |
2847 ident = null; | |
2848 t = parseDeclarator(ts, &ident, &tpl); | |
2849 assert(t); | |
2850 if (!tfirst) | |
2851 tfirst = t; | |
2852 else if (t != tfirst) | |
2853 error("multiple declarations must have the same type, not %s and %s", | |
2854 tfirst.toChars(), t.toChars()); | |
2855 if (!ident) | |
2856 error("no identifier for declarator %s", t.toChars()); | |
2857 | |
2858 if (tok == TOK.TOKtypedef || tok == TOK.TOKalias) | |
2859 { Declaration v; | |
2860 Initializer init = null; | |
2861 | |
2862 if (token.value == TOK.TOKassign) | |
2863 { | |
2864 nextToken(); | |
2865 init = parseInitializer(); | |
2866 } | |
2867 if (tok == TOK.TOKtypedef) | |
2868 v = new TypedefDeclaration(loc, ident, t, init); | |
2869 else | |
2870 { if (init) | |
2871 error("alias cannot have initializer"); | |
2872 v = new AliasDeclaration(loc, ident, t); | |
2873 } | |
2874 v.storage_class = storage_class; | |
2875 if (link == linkage) | |
2876 a.push(cast(void*)v); | |
2877 else | |
2878 { | |
2879 Array ax = new Array(); | |
2880 ax.push(cast(void*)v); | |
2881 Dsymbol s = new LinkDeclaration(link, ax); | |
2882 a.push(cast(void*)s); | |
2883 } | |
2884 switch (token.value) | |
2885 { case TOK.TOKsemicolon: | |
2886 nextToken(); | |
2887 addComment(v, comment); | |
2888 break; | |
2889 | |
2890 case TOK.TOKcomma: | |
2891 nextToken(); | |
2892 addComment(v, comment); | |
2893 continue; | |
2894 | |
2895 default: | |
2896 error("semicolon expected to close %s declaration", Token.toChars(tok)); | |
2897 break; | |
2898 } | |
2899 } | |
2900 else if (t.ty == TY.Tfunction) | |
2901 { | |
2902 TypeFunction tf = cast(TypeFunction)t; | |
2903 Expression constraint = null; | |
2904 static if (false) { | |
2905 if (Argument.isTPL(tf.parameters)) | |
2906 { | |
2907 if (!tpl) | |
2908 tpl = new TemplateParameters(); | |
2909 } | |
2910 } | |
2911 FuncDeclaration f = | |
2912 new FuncDeclaration(loc, Loc(0), ident, storage_class, t); | |
2913 addComment(f, comment); | |
2914 if (tpl) | |
2915 constraint = parseConstraint(); | |
2916 parseContracts(f); | |
2917 addComment(f, null); | |
2918 Dsymbol s; | |
2919 if (link == linkage) | |
2920 { | |
2921 s = f; | |
2922 } | |
2923 else | |
2924 { | |
2925 Array ax = new Array(); | |
2926 ax.push(cast(void*)f); | |
2927 s = new LinkDeclaration(link, ax); | |
2928 } | |
2929 /* A template parameter list means it's a function template | |
2930 */ | |
2931 if (tpl) | |
2932 { | |
2933 // Wrap a template around the function declaration | |
2934 Array decldefs = new Array(); | |
2935 decldefs.push(cast(void*)s); | |
2936 TemplateDeclaration tempdecl = | |
2937 new TemplateDeclaration(loc, s.ident, tpl, constraint, decldefs); | |
2938 s = tempdecl; | |
2939 } | |
2940 addComment(s, comment); | |
2941 a.push(cast(void*)s); | |
2942 } | |
2943 else | |
2944 { | |
2945 Initializer init = null; | |
2946 if (token.value == TOK.TOKassign) | |
2947 { | |
2948 nextToken(); | |
2949 init = parseInitializer(); | |
2950 } | |
2951 | |
2952 VarDeclaration v = new VarDeclaration(loc, t, ident, init); | |
2953 v.storage_class = storage_class; | |
2954 if (link == linkage) | |
2955 a.push(cast(void*)v); | |
2956 else | |
2957 { | |
2958 Array ax = new Array(); | |
2959 ax.push(cast(void*)v); | |
2960 Dsymbol s = new LinkDeclaration(link, ax); | |
2961 a.push(cast(void*)s); | |
2962 } | |
2963 switch (token.value) | |
2964 { case TOK.TOKsemicolon: | |
2965 nextToken(); | |
2966 addComment(v, comment); | |
2967 break; | |
2968 | |
2969 case TOK.TOKcomma: | |
2970 nextToken(); | |
2971 addComment(v, comment); | |
2972 continue; | |
2973 | |
2974 default: | |
2975 error("semicolon expected, not '%s'", token.toChars()); | |
2976 break; | |
2977 } | |
2978 } | |
2979 break; | |
2980 } | |
2981 return a; | |
2982 } | |
2983 | |
2984 void parseContracts(FuncDeclaration f) | |
2985 { | |
2986 Type tb; | |
2987 LINK linksave = linkage; | |
2988 | |
2989 // The following is irrelevant, as it is overridden by sc.linkage in | |
2990 // TypeFunction.semantic | |
2991 linkage = LINK.LINKd; // nested functions have D linkage | |
2992 L1: | |
2993 switch (token.value) | |
2994 { | |
2995 case TOK.TOKlcurly: | |
2996 if (f.frequire || f.fensure) | |
2997 error("missing body { ... } after in or out"); | |
2998 f.fbody = parseStatement(ParseStatementFlags.PSsemi); | |
2999 f.endloc = endloc; | |
3000 break; | |
3001 | |
3002 case TOK.TOKbody: | |
3003 nextToken(); | |
3004 f.fbody = parseStatement(ParseStatementFlags.PScurly); | |
3005 f.endloc = endloc; | |
3006 break; | |
3007 | |
3008 case TOK.TOKsemicolon: | |
3009 if (f.frequire || f.fensure) | |
3010 error("missing body { ... } after in or out"); | |
3011 nextToken(); | |
3012 break; | |
3013 | |
3014 static if (false) { // Do we want this for function declarations, so we can do: | |
3015 // int x, y, foo(), z; | |
3016 case TOK.TOKcomma: | |
3017 nextToken(); | |
3018 continue; | |
3019 } | |
3020 | |
3021 static if (false) { // Dumped feature | |
3022 case TOK.TOKthrow: | |
3023 if (!f.fthrows) | |
3024 f.fthrows = new Array(); | |
3025 nextToken(); | |
3026 check(TOK.TOKlparen); | |
3027 while (1) | |
3028 { | |
3029 tb = parseBasicType(); | |
3030 f.fthrows.push(tb); | |
3031 if (token.value == TOK.TOKcomma) | |
3032 { nextToken(); | |
3033 continue; | |
3034 } | |
3035 break; | |
3036 } | |
3037 check(TOK.TOKrparen); | |
3038 goto L1; | |
3039 } | |
3040 | |
3041 case TOK.TOKin: | |
3042 nextToken(); | |
3043 if (f.frequire) | |
3044 error("redundant 'in' statement"); | |
3045 f.frequire = parseStatement(ParseStatementFlags.PScurly | ParseStatementFlags.PSscope); | |
3046 goto L1; | |
3047 | |
3048 case TOK.TOKout: | |
3049 // parse: out (identifier) { statement } | |
3050 nextToken(); | |
3051 if (token.value != TOK.TOKlcurly) | |
3052 { | |
3053 check(TOK.TOKlparen); | |
3054 if (token.value != TOK.TOKidentifier) | |
3055 error("(identifier) following 'out' expected, not %s", token.toChars()); | |
3056 f.outId = token.ident; | |
3057 nextToken(); | |
3058 check(TOK.TOKrparen); | |
3059 } | |
3060 if (f.fensure) | |
3061 error("redundant 'out' statement"); | |
3062 f.fensure = parseStatement(ParseStatementFlags.PScurly | ParseStatementFlags.PSscope); | |
3063 goto L1; | |
3064 | |
3065 default: | |
3066 error("semicolon expected following function declaration"); | |
3067 break; | |
3068 } | |
3069 linkage = linksave; | |
3070 } | |
3071 | |
3072 Statement parseStatement(ParseStatementFlags flags) | |
3073 { | |
3074 Statement s; | |
3075 Token* t; | |
3076 Condition condition; | |
3077 Statement ifbody; | |
3078 Statement elsebody; | |
3079 bool isfinal; | |
3080 Loc loc = this.loc; | |
3081 | |
3082 //printf("parseStatement()\n"); | |
3083 | |
3084 if (flags & ParseStatementFlags.PScurly && token.value != TOK.TOKlcurly) | |
3085 error("statement expected to be { }, not %s", token.toChars()); | |
3086 | |
3087 switch (token.value) | |
3088 { | |
3089 case TOK.TOKidentifier: | |
3090 /* A leading identifier can be a declaration, label, or expression. | |
3091 * The easiest case to check first is label: | |
3092 */ | |
3093 t = peek(&token); | |
3094 if (t.value == TOK.TOKcolon) | |
3095 { // It's a label | |
3096 | |
3097 Identifier ident = token.ident; | |
3098 nextToken(); | |
3099 nextToken(); | |
3100 s = parseStatement(ParseStatementFlags.PSsemi); | |
3101 s = new LabelStatement(loc, ident, s); | |
3102 break; | |
3103 } | |
3104 // fallthrough to TOK.TOKdot | |
3105 case TOK.TOKdot: | |
3106 case TOK.TOKtypeof: | |
3107 if (isDeclaration(&token, 2, TOK.TOKreserved, null)) | |
3108 goto Ldeclaration; | |
3109 else | |
3110 goto Lexp; | |
3111 break; | |
3112 | |
3113 case TOK.TOKassert: | |
3114 case TOK.TOKthis: | |
3115 case TOK.TOKsuper: | |
3116 case TOK.TOKint32v: | |
3117 case TOK.TOKuns32v: | |
3118 case TOK.TOKint64v: | |
3119 case TOK.TOKuns64v: | |
3120 case TOK.TOKfloat32v: | |
3121 case TOK.TOKfloat64v: | |
3122 case TOK.TOKfloat80v: | |
3123 case TOK.TOKimaginary32v: | |
3124 case TOK.TOKimaginary64v: | |
3125 case TOK.TOKimaginary80v: | |
3126 case TOK.TOKcharv: | |
3127 case TOK.TOKwcharv: | |
3128 case TOK.TOKdcharv: | |
3129 case TOK.TOKnull: | |
3130 case TOK.TOKtrue: | |
3131 case TOK.TOKfalse: | |
3132 case TOK.TOKstring: | |
3133 case TOK.TOKlparen: | |
3134 case TOK.TOKcast: | |
3135 case TOK.TOKmul: | |
3136 case TOK.TOKmin: | |
3137 case TOK.TOKadd: | |
3138 case TOK.TOKplusplus: | |
3139 case TOK.TOKminusminus: | |
3140 case TOK.TOKnew: | |
3141 case TOK.TOKdelete: | |
3142 case TOK.TOKdelegate: | |
3143 case TOK.TOKfunction: | |
3144 case TOK.TOKtypeid: | |
3145 case TOK.TOKis: | |
3146 case TOK.TOKlbracket: | |
3147 version (DMDV2) { | |
3148 case TOK.TOKtraits: | |
3149 case TOK.TOKfile: | |
3150 case TOK.TOKline: | |
3151 } | |
3152 Lexp: | |
3153 { Expression exp; | |
3154 | |
3155 exp = parseExpression(); | |
3156 check(TOK.TOKsemicolon, "statement"); | |
3157 s = new ExpStatement(loc, exp); | |
3158 break; | |
3159 } | |
3160 | |
3161 case TOK.TOKstatic: | |
3162 { // Look ahead to see if it's static assert() or static if() | |
3163 Token *tt; | |
3164 | |
3165 tt = peek(&token); | |
3166 if (tt.value == TOK.TOKassert) | |
3167 { | |
3168 nextToken(); | |
3169 s = new StaticAssertStatement(parseStaticAssert()); | |
3170 break; | |
3171 } | |
3172 if (tt.value == TOK.TOKif) | |
3173 { | |
3174 nextToken(); | |
3175 condition = parseStaticIfCondition(); | |
3176 goto Lcondition; | |
3177 } | |
3178 goto Ldeclaration; | |
3179 } | |
3180 | |
3181 case TOK.TOKfinal: | |
3182 if (peekNext() == TOK.TOKswitch) | |
3183 { | |
3184 nextToken(); | |
3185 isfinal = true; | |
3186 goto Lswitch; | |
3187 } | |
3188 goto Ldeclaration; | |
3189 | |
3190 case TOK.TOKwchar: case TOK.TOKdchar: | |
3191 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
3192 case TOK.TOKint8: case TOK.TOKuns8: | |
3193 case TOK.TOKint16: case TOK.TOKuns16: | |
3194 case TOK.TOKint32: case TOK.TOKuns32: | |
3195 case TOK.TOKint64: case TOK.TOKuns64: | |
3196 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
3197 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
3198 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
3199 case TOK.TOKvoid: | |
3200 case TOK.TOKtypedef: | |
3201 case TOK.TOKalias: | |
3202 case TOK.TOKconst: | |
3203 case TOK.TOKauto: | |
3204 case TOK.TOKextern: | |
3205 case TOK.TOKinvariant: | |
3206 version (DMDV2) { | |
3207 case TOK.TOKimmutable: | |
3208 case TOK.TOKshared: | |
3209 case TOK.TOKnothrow: | |
3210 case TOK.TOKpure: | |
3211 case TOK.TOKtls: | |
3212 case TOK.TOKgshared: | |
3213 } | |
3214 // case TOK.TOKtypeof: | |
3215 Ldeclaration: | |
3216 { Array a; | |
3217 | |
3218 a = parseDeclarations(STC.STCundefined); | |
3219 if (a.dim > 1) | |
3220 { | |
3221 Statements as = new Statements(); | |
3222 as.reserve(a.dim); | |
3223 for (int i = 0; i < a.dim; i++) | |
3224 { | |
3225 Dsymbol d = cast(Dsymbol)a.data[i]; | |
3226 s = new DeclarationStatement(loc, d); | |
3227 as.push(cast(void*)s); | |
3228 } | |
3229 s = new CompoundDeclarationStatement(loc, as); | |
3230 } | |
3231 else if (a.dim == 1) | |
3232 { | |
3233 Dsymbol d = cast(Dsymbol)a.data[0]; | |
3234 s = new DeclarationStatement(loc, d); | |
3235 } | |
3236 else | |
3237 assert(0); | |
3238 if (flags & ParseStatementFlags.PSscope) | |
3239 s = new ScopeStatement(loc, s); | |
3240 break; | |
3241 } | |
3242 | |
3243 case TOK.TOKstruct: | |
3244 case TOK.TOKunion: | |
3245 case TOK.TOKclass: | |
3246 case TOK.TOKinterface: | |
3247 { Dsymbol d; | |
3248 | |
3249 d = parseAggregate(); | |
3250 s = new DeclarationStatement(loc, d); | |
3251 break; | |
3252 } | |
3253 | |
3254 case TOK.TOKenum: | |
3255 { /* Determine if this is a manifest constant declaration, | |
3256 * or a conventional enum. | |
3257 */ | |
3258 Dsymbol d; | |
3259 Token* tt = peek(&token); | |
3260 if (tt.value == TOK.TOKlcurly || tt.value == TOK.TOKcolon) | |
3261 d = parseEnum(); | |
3262 else if (tt.value != TOK.TOKidentifier) | |
3263 goto Ldeclaration; | |
3264 else | |
3265 { | |
3266 tt = peek(tt); | |
3267 if (tt.value == TOK.TOKlcurly || tt.value == TOK.TOKcolon || | |
3268 tt.value == TOK.TOKsemicolon) | |
3269 d = parseEnum(); | |
3270 else | |
3271 goto Ldeclaration; | |
3272 } | |
3273 s = new DeclarationStatement(loc, d); | |
3274 break; | |
3275 } | |
3276 | |
3277 case TOK.TOKmixin: | |
3278 { t = peek(&token); | |
3279 if (t.value == TOK.TOKlparen) | |
3280 { // mixin(string) | |
3281 nextToken(); | |
3282 check(TOK.TOKlparen, "mixin"); | |
3283 Expression e = parseAssignExp(); | |
3284 check(TOK.TOKrparen); | |
3285 check(TOK.TOKsemicolon); | |
3286 s = new CompileStatement(loc, e); | |
3287 break; | |
3288 } | |
3289 Dsymbol d = parseMixin(); | |
3290 s = new DeclarationStatement(loc, d); | |
3291 break; | |
3292 } | |
3293 | |
3294 case TOK.TOKlcurly: | |
3295 { Statements statements; | |
3296 | |
3297 nextToken(); | |
3298 statements = new Statements(); | |
3299 while (token.value != TOK.TOKrcurly) | |
3300 { | |
3301 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); | |
3302 } | |
3303 endloc = this.loc; | |
3304 s = new CompoundStatement(loc, statements); | |
3305 if (flags & (ParseStatementFlags.PSscope | ParseStatementFlags.PScurlyscope)) | |
3306 s = new ScopeStatement(loc, s); | |
3307 nextToken(); | |
3308 break; | |
3309 } | |
3310 | |
3311 case TOK.TOKwhile: | |
3312 { Expression condition2; | |
3313 Statement body_; | |
3314 | |
3315 nextToken(); | |
3316 check(TOK.TOKlparen); | |
3317 condition2 = parseExpression(); | |
3318 check(TOK.TOKrparen); | |
3319 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3320 s = new WhileStatement(loc, condition2, body_); | |
3321 break; | |
3322 } | |
3323 | |
3324 case TOK.TOKsemicolon: | |
3325 if (!(flags & ParseStatementFlags.PSsemi)) | |
3326 error("use '{ }' for an empty statement, not a ';'"); | |
3327 nextToken(); | |
3328 s = new ExpStatement(loc, null); | |
3329 break; | |
3330 | |
3331 case TOK.TOKdo: | |
3332 { Statement body_; | |
3333 Expression condition2; | |
3334 | |
3335 nextToken(); | |
3336 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3337 check(TOK.TOKwhile); | |
3338 check(TOK.TOKlparen); | |
3339 condition2 = parseExpression(); | |
3340 check(TOK.TOKrparen); | |
3341 s = new DoStatement(loc, body_, condition2); | |
3342 break; | |
3343 } | |
3344 | |
3345 case TOK.TOKfor: | |
3346 { | |
3347 Statement init; | |
3348 Expression condition2; | |
3349 Expression increment; | |
3350 Statement body_; | |
3351 | |
3352 nextToken(); | |
3353 check(TOK.TOKlparen); | |
3354 if (token.value == TOK.TOKsemicolon) | |
3355 { init = null; | |
3356 nextToken(); | |
3357 } | |
3358 else | |
3359 { init = parseStatement(cast(ParseStatementFlags)0); | |
3360 } | |
3361 if (token.value == TOK.TOKsemicolon) | |
3362 { | |
3363 condition2 = null; | |
3364 nextToken(); | |
3365 } | |
3366 else | |
3367 { | |
3368 condition2 = parseExpression(); | |
3369 check(TOK.TOKsemicolon, "for condition"); | |
3370 } | |
3371 if (token.value == TOK.TOKrparen) | |
3372 { increment = null; | |
3373 nextToken(); | |
3374 } | |
3375 else | |
3376 { increment = parseExpression(); | |
3377 check(TOK.TOKrparen); | |
3378 } | |
3379 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3380 s = new ForStatement(loc, init, condition2, increment, body_); | |
3381 if (init) | |
3382 s = new ScopeStatement(loc, s); | |
3383 break; | |
3384 } | |
3385 | |
3386 case TOK.TOKforeach: | |
3387 case TOK.TOKforeach_reverse: | |
3388 { | |
3389 TOK op = token.value; | |
3390 Arguments arguments; | |
3391 | |
3392 Statement d; | |
3393 Statement body_; | |
3394 Expression aggr; | |
3395 | |
3396 nextToken(); | |
3397 check(TOK.TOKlparen); | |
3398 | |
3399 arguments = new Arguments(); | |
3400 | |
3401 while (1) | |
3402 { | |
3403 Type tb; | |
3404 Identifier ai = null; | |
3405 Type at; | |
3406 STC storageClass = STC.STCundefined; | |
3407 Argument a; | |
3408 | |
3409 if (token.value == TOK.TOKinout || token.value == TOK.TOKref) | |
3410 { storageClass = STC.STCref; | |
3411 nextToken(); | |
3412 } | |
3413 if (token.value == TOK.TOKidentifier) | |
3414 { | |
3415 Token *tt = peek(&token); | |
3416 if (tt.value == TOK.TOKcomma || tt.value == TOK.TOKsemicolon) | |
3417 { ai = token.ident; | |
3418 at = null; // infer argument type | |
3419 nextToken(); | |
3420 goto Larg; | |
3421 } | |
3422 } | |
3423 at = parseType(&ai); | |
3424 if (!ai) | |
3425 error("no identifier for declarator %s", at.toChars()); | |
3426 Larg: | |
3427 a = new Argument(storageClass, at, ai, null); | |
3428 arguments.push(cast(void*)a); | |
3429 if (token.value == TOK.TOKcomma) | |
3430 { nextToken(); | |
3431 continue; | |
3432 } | |
3433 break; | |
3434 } | |
3435 check(TOK.TOKsemicolon); | |
3436 | |
3437 aggr = parseExpression(); | |
3438 if (token.value == TOK.TOKslice && arguments.dim == 1) | |
3439 { | |
3440 Argument a = cast(Argument)arguments.data[0]; | |
3441 delete arguments; | |
3442 nextToken(); | |
3443 Expression upr = parseExpression(); | |
3444 check(TOK.TOKrparen); | |
3445 body_ = parseStatement(cast(ParseStatementFlags)0); | |
3446 s = new ForeachRangeStatement(loc, op, a, aggr, upr, body_); | |
3447 } | |
3448 else | |
3449 { | |
3450 check(TOK.TOKrparen); | |
3451 body_ = parseStatement(cast(ParseStatementFlags)0); | |
3452 s = new ForeachStatement(loc, op, arguments, aggr, body_); | |
3453 } | |
3454 break; | |
3455 } | |
3456 | |
3457 case TOK.TOKif: | |
3458 { Argument arg = null; | |
3459 Expression condition2; | |
3460 Statement ifbody2; | |
3461 Statement elsebody2; | |
3462 | |
3463 nextToken(); | |
3464 check(TOK.TOKlparen); | |
3465 | |
3466 if (token.value == TOK.TOKauto) | |
3467 { | |
3468 nextToken(); | |
3469 if (token.value == TOK.TOKidentifier) | |
3470 { | |
3471 Token *tt = peek(&token); | |
3472 if (tt.value == TOK.TOKassign) | |
3473 { | |
3474 arg = new Argument(STC.STCundefined, null, token.ident, null); | |
3475 nextToken(); | |
3476 nextToken(); | |
3477 } | |
3478 else | |
3479 { error("= expected following auto identifier"); | |
3480 goto Lerror; | |
3481 } | |
3482 } | |
3483 else | |
3484 { error("identifier expected following auto"); | |
3485 goto Lerror; | |
3486 } | |
3487 } | |
3488 else if (isDeclaration(&token, 2, TOK.TOKassign, null)) | |
3489 { | |
3490 Type at; | |
3491 Identifier ai; | |
3492 | |
3493 at = parseType(&ai); | |
3494 check(TOK.TOKassign); | |
3495 arg = new Argument(STC.STCundefined, at, ai, null); | |
3496 } | |
3497 | |
3498 // Check for " ident;" | |
3499 else if (token.value == TOK.TOKidentifier) | |
3500 { | |
3501 Token *tt = peek(&token); | |
3502 if (tt.value == TOK.TOKcomma || tt.value == TOK.TOKsemicolon) | |
3503 { | |
3504 arg = new Argument(STC.STCundefined, null, token.ident, null); | |
3505 nextToken(); | |
3506 nextToken(); | |
3507 if (1 || !global.params.useDeprecated) | |
3508 error("if (v; e) is deprecated, use if (auto v = e)"); | |
3509 } | |
3510 } | |
3511 | |
3512 condition2 = parseExpression(); | |
3513 check(TOK.TOKrparen); | |
3514 ifbody2 = parseStatement(ParseStatementFlags.PSscope); | |
3515 if (token.value == TOK.TOKelse) | |
3516 { | |
3517 nextToken(); | |
3518 elsebody2 = parseStatement(ParseStatementFlags.PSscope); | |
3519 } | |
3520 else | |
3521 elsebody2 = null; | |
3522 s = new IfStatement(loc, arg, condition2, ifbody2, elsebody2); | |
3523 break; | |
3524 } | |
3525 | |
3526 case TOK.TOKscope: | |
3527 if (peek(&token).value != TOK.TOKlparen) | |
3528 goto Ldeclaration; // scope used as storage class | |
3529 nextToken(); | |
3530 check(TOK.TOKlparen); | |
3531 if (token.value != TOK.TOKidentifier) | |
3532 { error("scope identifier expected"); | |
3533 goto Lerror; | |
3534 } | |
3535 else | |
3536 { TOK tt = TOK.TOKon_scope_exit; | |
3537 Identifier id = token.ident; | |
3538 | |
3539 if (id == Id.exit) | |
3540 tt = TOK.TOKon_scope_exit; | |
3541 else if (id == Id.failure) | |
3542 tt = TOK.TOKon_scope_failure; | |
3543 else if (id == Id.success) | |
3544 tt = TOK.TOKon_scope_success; | |
3545 else | |
3546 error("valid scope identifiers are exit, failure, or success, not %s", id.toChars()); | |
3547 nextToken(); | |
3548 check(TOK.TOKrparen); | |
3549 Statement st = parseStatement(ParseStatementFlags.PScurlyscope); | |
3550 s = new OnScopeStatement(loc, tt, st); | |
3551 break; | |
3552 } | |
3553 | |
3554 case TOK.TOKdebug: | |
3555 nextToken(); | |
3556 condition = parseDebugCondition(); | |
3557 goto Lcondition; | |
3558 | |
3559 case TOK.TOKversion: | |
3560 nextToken(); | |
3561 condition = parseVersionCondition(); | |
3562 goto Lcondition; | |
3563 | |
3564 Lcondition: | |
3565 ifbody = parseStatement(cast(ParseStatementFlags)0 /*ParseStatementFlags.PSsemi*/); | |
3566 elsebody = null; | |
3567 if (token.value == TOK.TOKelse) | |
3568 { | |
3569 nextToken(); | |
3570 elsebody = parseStatement(cast(ParseStatementFlags)0 /*ParseStatementFlags.PSsemi*/); | |
3571 } | |
3572 s = new ConditionalStatement(loc, condition, ifbody, elsebody); | |
3573 break; | |
3574 | |
3575 case TOK.TOKpragma: | |
3576 { Identifier ident; | |
3577 Expressions args = null; | |
3578 Statement body_; | |
3579 | |
3580 nextToken(); | |
3581 check(TOK.TOKlparen); | |
3582 if (token.value != TOK.TOKidentifier) | |
3583 { error("pragma(identifier expected"); | |
3584 goto Lerror; | |
3585 } | |
3586 ident = token.ident; | |
3587 nextToken(); | |
3588 if (token.value == TOK.TOKcomma && peekNext() != TOK.TOKrparen) | |
3589 args = parseArguments(); // pragma(identifier, args...); | |
3590 else | |
3591 check(TOK.TOKrparen); // pragma(identifier); | |
3592 if (token.value == TOK.TOKsemicolon) | |
3593 { nextToken(); | |
3594 body_ = null; | |
3595 } | |
3596 else | |
3597 body_ = parseStatement(ParseStatementFlags.PSsemi); | |
3598 s = new PragmaStatement(loc, ident, args, body_); | |
3599 break; | |
3600 } | |
3601 | |
3602 case TOK.TOKswitch: | |
3603 isfinal = false; | |
3604 goto Lswitch; | |
3605 | |
3606 Lswitch: | |
3607 { | |
3608 nextToken(); | |
3609 check(TOK.TOKlparen); | |
3610 Expression condition2 = parseExpression(); | |
3611 check(TOK.TOKrparen); | |
3612 Statement body_ = parseStatement(ParseStatementFlags.PSscope); | |
3613 s = new SwitchStatement(loc, condition2, body_, isfinal); | |
3614 break; | |
3615 } | |
3616 | |
3617 case TOK.TOKcase: | |
3618 { Expression exp; | |
3619 Statements statements; | |
3620 scope Array cases = new Array(); // array of Expression's | |
3621 Expression last = null; | |
3622 | |
3623 while (1) | |
3624 { | |
3625 nextToken(); | |
3626 exp = parseAssignExp(); | |
3627 cases.push(cast(void*)exp); | |
3628 if (token.value != TOK.TOKcomma) | |
3629 break; | |
3630 } | |
3631 check(TOK.TOKcolon); | |
3632 | |
3633 version (DMDV2) { | |
3634 /* case exp: .. case last: | |
3635 */ | |
3636 if (token.value == TOK.TOKslice) | |
3637 { | |
3638 if (cases.dim > 1) | |
3639 error("only one case allowed for start of case range"); | |
3640 nextToken(); | |
3641 check(TOK.TOKcase); | |
3642 last = parseAssignExp(); | |
3643 check(TOK.TOKcolon); | |
3644 } | |
3645 } | |
3646 | |
3647 statements = new Statements(); | |
3648 while (token.value != TOK.TOKcase && | |
3649 token.value != TOK.TOKdefault && | |
3650 token.value != TOK.TOKrcurly) | |
3651 { | |
3652 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); | |
3653 } | |
3654 s = new CompoundStatement(loc, statements); | |
3655 s = new ScopeStatement(loc, s); | |
3656 | |
3657 ///version (DMDV2) { | |
3658 if (last) | |
3659 { | |
3660 s = new CaseRangeStatement(loc, exp, last, s); | |
3661 } | |
3662 else | |
3663 ///} | |
3664 { | |
3665 // Keep cases in order by building the case statements backwards | |
3666 for (int i = cases.dim; i; i--) | |
3667 { | |
3668 exp = cast(Expression)cases.data[i - 1]; | |
3669 s = new CaseStatement(loc, exp, s); | |
3670 } | |
3671 } | |
3672 break; | |
3673 } | |
3674 | |
3675 case TOK.TOKdefault: | |
3676 { | |
3677 Statements statements; | |
3678 | |
3679 nextToken(); | |
3680 check(TOK.TOKcolon); | |
3681 | |
3682 statements = new Statements(); | |
3683 while (token.value != TOK.TOKcase && | |
3684 token.value != TOK.TOKdefault && | |
3685 token.value != TOK.TOKrcurly) | |
3686 { | |
3687 statements.push(cast(void*)parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope)); | |
3688 } | |
3689 s = new CompoundStatement(loc, statements); | |
3690 s = new ScopeStatement(loc, s); | |
3691 s = new DefaultStatement(loc, s); | |
3692 break; | |
3693 } | |
3694 | |
3695 case TOK.TOKreturn: | |
3696 { Expression exp; | |
3697 | |
3698 nextToken(); | |
3699 if (token.value == TOK.TOKsemicolon) | |
3700 exp = null; | |
3701 else | |
3702 exp = parseExpression(); | |
3703 check(TOK.TOKsemicolon, "return statement"); | |
3704 s = new ReturnStatement(loc, exp); | |
3705 break; | |
3706 } | |
3707 | |
3708 case TOK.TOKbreak: | |
3709 { Identifier ident; | |
3710 | |
3711 nextToken(); | |
3712 if (token.value == TOK.TOKidentifier) | |
3713 { ident = token.ident; | |
3714 nextToken(); | |
3715 } | |
3716 else | |
3717 ident = null; | |
3718 check(TOK.TOKsemicolon, "break statement"); | |
3719 s = new BreakStatement(loc, ident); | |
3720 break; | |
3721 } | |
3722 | |
3723 case TOK.TOKcontinue: | |
3724 { Identifier ident; | |
3725 | |
3726 nextToken(); | |
3727 if (token.value == TOK.TOKidentifier) | |
3728 { ident = token.ident; | |
3729 nextToken(); | |
3730 } | |
3731 else | |
3732 ident = null; | |
3733 check(TOK.TOKsemicolon, "continue statement"); | |
3734 s = new ContinueStatement(loc, ident); | |
3735 break; | |
3736 } | |
3737 | |
3738 case TOK.TOKgoto: | |
3739 { Identifier ident; | |
3740 | |
3741 nextToken(); | |
3742 if (token.value == TOK.TOKdefault) | |
3743 { | |
3744 nextToken(); | |
3745 s = new GotoDefaultStatement(loc); | |
3746 } | |
3747 else if (token.value == TOK.TOKcase) | |
3748 { | |
3749 Expression exp = null; | |
3750 | |
3751 nextToken(); | |
3752 if (token.value != TOK.TOKsemicolon) | |
3753 exp = parseExpression(); | |
3754 s = new GotoCaseStatement(loc, exp); | |
3755 } | |
3756 else | |
3757 { | |
3758 if (token.value != TOK.TOKidentifier) | |
3759 { error("Identifier expected following goto"); | |
3760 ident = null; | |
3761 } | |
3762 else | |
3763 { ident = token.ident; | |
3764 nextToken(); | |
3765 } | |
3766 s = new GotoStatement(loc, ident); | |
3767 } | |
3768 check(TOK.TOKsemicolon, "goto statement"); | |
3769 break; | |
3770 } | |
3771 | |
3772 case TOK.TOKsynchronized: | |
3773 { Expression exp; | |
3774 Statement body_; | |
3775 | |
3776 nextToken(); | |
3777 if (token.value == TOK.TOKlparen) | |
3778 { | |
3779 nextToken(); | |
3780 exp = parseExpression(); | |
3781 check(TOK.TOKrparen); | |
3782 } | |
3783 else | |
3784 exp = null; | |
3785 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3786 s = new SynchronizedStatement(loc, exp, body_); | |
3787 break; | |
3788 } | |
3789 | |
3790 case TOK.TOKwith: | |
3791 { Expression exp; | |
3792 Statement body_; | |
3793 | |
3794 nextToken(); | |
3795 check(TOK.TOKlparen); | |
3796 exp = parseExpression(); | |
3797 check(TOK.TOKrparen); | |
3798 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3799 s = new WithStatement(loc, exp, body_); | |
3800 break; | |
3801 } | |
3802 | |
3803 case TOK.TOKtry: | |
3804 { Statement body_; | |
3805 Array catches = null; | |
3806 Statement finalbody = null; | |
3807 | |
3808 nextToken(); | |
3809 body_ = parseStatement(ParseStatementFlags.PSscope); | |
3810 while (token.value == TOK.TOKcatch) | |
3811 { | |
3812 Statement handler; | |
3813 Catch c; | |
3814 Type tt; | |
3815 Identifier id; | |
3816 Loc loc2 = this.loc; | |
3817 | |
3818 nextToken(); | |
3819 if (token.value == TOK.TOKlcurly) | |
3820 { | |
3821 tt = null; | |
3822 id = null; | |
3823 } | |
3824 else | |
3825 { | |
3826 check(TOK.TOKlparen); | |
3827 id = null; | |
3828 tt = parseType(&id); | |
3829 check(TOK.TOKrparen); | |
3830 } | |
3831 handler = parseStatement(cast(ParseStatementFlags)0); | |
3832 c = new Catch(loc2, tt, id, handler); | |
3833 if (!catches) | |
3834 catches = new Array(); | |
3835 catches.push(cast(void*)c); | |
3836 } | |
3837 | |
3838 if (token.value == TOK.TOKfinally) | |
3839 { nextToken(); | |
3840 finalbody = parseStatement(cast(ParseStatementFlags)0); | |
3841 } | |
3842 | |
3843 s = body_; | |
3844 if (!catches && !finalbody) | |
3845 error("catch or finally expected following try"); | |
3846 else | |
3847 { if (catches) | |
3848 s = new TryCatchStatement(loc, body_, catches); | |
3849 if (finalbody) | |
3850 s = new TryFinallyStatement(loc, s, finalbody); | |
3851 } | |
3852 break; | |
3853 } | |
3854 | |
3855 case TOK.TOKthrow: | |
3856 { Expression exp; | |
3857 | |
3858 nextToken(); | |
3859 exp = parseExpression(); | |
3860 check(TOK.TOKsemicolon, "throw statement"); | |
3861 s = new ThrowStatement(loc, exp); | |
3862 break; | |
3863 } | |
3864 | |
3865 case TOK.TOKvolatile: | |
3866 nextToken(); | |
3867 s = parseStatement(ParseStatementFlags.PSsemi | ParseStatementFlags.PScurlyscope); | |
3868 version (DMDV2) { | |
3869 if (!global.params.useDeprecated) | |
3870 error("volatile statements deprecated; used synchronized statements instead"); | |
3871 } | |
3872 s = new VolatileStatement(loc, s); | |
3873 break; | |
3874 | |
3875 case TOK.TOKasm: | |
3876 { Statements statements; | |
3877 Identifier label; | |
3878 Loc labelloc; | |
3879 Token* toklist; | |
3880 Token** ptoklist; | |
3881 | |
3882 // Parse the asm block into a sequence of AsmStatements, | |
3883 // each AsmStatement is one instruction. | |
3884 // Separate out labels. | |
3885 // Defer parsing of AsmStatements until semantic processing. | |
3886 | |
3887 nextToken(); | |
3888 check(TOK.TOKlcurly); | |
3889 toklist = null; | |
3890 ptoklist = &toklist; | |
3891 label = null; | |
3892 statements = new Statements(); | |
3893 while (1) | |
3894 { | |
3895 switch (token.value) | |
3896 { | |
3897 case TOK.TOKidentifier: | |
3898 if (!toklist) | |
3899 { | |
3900 // Look ahead to see if it is a label | |
3901 t = peek(&token); | |
3902 if (t.value == TOK.TOKcolon) | |
3903 { // It's a label | |
3904 label = token.ident; | |
3905 labelloc = this.loc; | |
3906 nextToken(); | |
3907 nextToken(); | |
3908 continue; | |
3909 } | |
3910 } | |
3911 goto Ldefault; | |
3912 | |
3913 case TOK.TOKrcurly: | |
3914 if (toklist || label) | |
3915 { | |
3916 error("asm statements must end in ';'"); | |
3917 } | |
3918 break; | |
3919 | |
3920 case TOK.TOKsemicolon: | |
3921 s = null; | |
3922 if (toklist || label) | |
3923 { // Create AsmStatement from list of tokens we've saved | |
3924 s = new AsmStatement(this.loc, toklist); | |
3925 toklist = null; | |
3926 ptoklist = &toklist; | |
3927 if (label) | |
3928 { | |
3929 s = new LabelStatement(labelloc, label, s); | |
3930 label = null; | |
3931 } | |
3932 statements.push(cast(void*)s); | |
3933 } | |
3934 nextToken(); | |
3935 continue; | |
3936 | |
3937 case TOK.TOKeof: | |
3938 /* { */ | |
3939 error("matching '}' expected, not end of file"); | |
3940 break; | |
3941 | |
3942 default: | |
3943 Ldefault: | |
3944 *ptoklist = new Token(); | |
3945 memcpy(*ptoklist, &token, Token.sizeof); | |
3946 ptoklist = &(*ptoklist).next; | |
3947 *ptoklist = null; | |
3948 | |
3949 nextToken(); | |
3950 continue; | |
3951 } | |
3952 break; | |
3953 } | |
3954 s = new CompoundStatement(loc, statements); | |
3955 nextToken(); | |
3956 break; | |
3957 } | |
3958 | |
3959 default: | |
3960 error("found '%s' instead of statement", token.toChars()); | |
3961 goto Lerror; | |
3962 | |
3963 Lerror: | |
3964 while (token.value != TOK.TOKrcurly && | |
3965 token.value != TOK.TOKsemicolon && | |
3966 token.value != TOK.TOKeof) | |
3967 nextToken(); | |
3968 if (token.value == TOK.TOKsemicolon) | |
3969 nextToken(); | |
3970 s = null; | |
3971 break; | |
3972 } | |
3973 | |
3974 return s; | |
3975 } | |
3976 | |
3977 /***************************************** | |
3978 * Parse initializer for variable declaration. | |
3979 */ | |
3980 Initializer parseInitializer() | |
3981 { | |
3982 StructInitializer is_; | |
3983 ArrayInitializer ia; | |
3984 ExpInitializer ie; | |
3985 Expression e; | |
3986 Identifier id; | |
3987 Initializer value; | |
3988 int comma; | |
3989 Loc loc = this.loc; | |
3990 Token* t; | |
3991 int braces; | |
3992 int brackets; | |
3993 | |
3994 switch (token.value) | |
3995 { | |
3996 case TOK.TOKlcurly: | |
3997 /* Scan ahead to see if it is a struct initializer or | |
3998 * a function literal. | |
3999 * If it contains a ';', it is a function literal. | |
4000 * Treat { } as a struct initializer. | |
4001 */ | |
4002 braces = 1; | |
4003 for (t = peek(&token); 1; t = peek(t)) | |
4004 { | |
4005 switch (t.value) | |
4006 { | |
4007 case TOK.TOKsemicolon: | |
4008 case TOK.TOKreturn: | |
4009 goto Lexpression; | |
4010 | |
4011 case TOK.TOKlcurly: | |
4012 braces++; | |
4013 continue; | |
4014 | |
4015 case TOK.TOKrcurly: | |
4016 if (--braces == 0) | |
4017 break; | |
4018 continue; | |
4019 | |
4020 case TOK.TOKeof: | |
4021 break; | |
4022 | |
4023 default: | |
4024 continue; | |
4025 } | |
4026 break; | |
4027 } | |
4028 | |
4029 is_ = new StructInitializer(loc); | |
4030 nextToken(); | |
4031 comma = 0; | |
4032 while (1) | |
4033 { | |
4034 switch (token.value) | |
4035 { | |
4036 case TOK.TOKidentifier: | |
4037 if (comma == 1) | |
4038 error("comma expected separating field initializers"); | |
4039 t = peek(&token); | |
4040 if (t.value == TOK.TOKcolon) | |
4041 { | |
4042 id = token.ident; | |
4043 nextToken(); | |
4044 nextToken(); // skip over ':' | |
4045 } | |
4046 else | |
4047 { | |
4048 id = null; | |
4049 } | |
4050 value = parseInitializer(); | |
4051 is_.addInit(id, value); | |
4052 comma = 1; | |
4053 continue; | |
4054 | |
4055 case TOK.TOKcomma: | |
4056 nextToken(); | |
4057 comma = 2; | |
4058 continue; | |
4059 | |
4060 case TOK.TOKrcurly: // allow trailing comma's | |
4061 nextToken(); | |
4062 break; | |
4063 | |
4064 case TOK.TOKeof: | |
4065 error("found EOF instead of initializer"); | |
4066 break; | |
4067 | |
4068 default: | |
4069 value = parseInitializer(); | |
4070 is_.addInit(null, value); | |
4071 comma = 1; | |
4072 continue; | |
4073 //error("found '%s' instead of field initializer", token.toChars()); | |
4074 //break; | |
4075 } | |
4076 break; | |
4077 } | |
4078 return is_; | |
4079 | |
4080 case TOK.TOKlbracket: | |
4081 /* Scan ahead to see if it is an array initializer or | |
4082 * an expression. | |
4083 * If it ends with a ';' ',' or '}', it is an array initializer. | |
4084 */ | |
4085 brackets = 1; | |
4086 for (t = peek(&token); 1; t = peek(t)) | |
4087 { | |
4088 switch (t.value) | |
4089 { | |
4090 case TOK.TOKlbracket: | |
4091 brackets++; | |
4092 continue; | |
4093 | |
4094 case TOK.TOKrbracket: | |
4095 if (--brackets == 0) | |
4096 { | |
4097 t = peek(t); | |
4098 if (t.value != TOK.TOKsemicolon && | |
4099 t.value != TOK.TOKcomma && | |
4100 t.value != TOK.TOKrcurly) | |
4101 goto Lexpression; | |
4102 break; | |
4103 } | |
4104 continue; | |
4105 | |
4106 case TOK.TOKeof: | |
4107 break; | |
4108 | |
4109 default: | |
4110 continue; | |
4111 } | |
4112 break; | |
4113 } | |
4114 | |
4115 ia = new ArrayInitializer(loc); | |
4116 nextToken(); | |
4117 comma = 0; | |
4118 while (true) | |
4119 { | |
4120 switch (token.value) | |
4121 { | |
4122 default: | |
4123 if (comma == 1) | |
4124 { | |
4125 error("comma expected separating array initializers, not %s", token.toChars()); | |
4126 nextToken(); | |
4127 break; | |
4128 } | |
4129 e = parseAssignExp(); | |
4130 if (!e) | |
4131 break; | |
4132 if (token.value == TOK.TOKcolon) | |
4133 { | |
4134 nextToken(); | |
4135 value = parseInitializer(); | |
4136 } | |
4137 else | |
4138 { value = new ExpInitializer(e.loc, e); | |
4139 e = null; | |
4140 } | |
4141 ia.addInit(e, value); | |
4142 comma = 1; | |
4143 continue; | |
4144 | |
4145 case TOK.TOKlcurly: | |
4146 case TOK.TOKlbracket: | |
4147 if (comma == 1) | |
4148 error("comma expected separating array initializers, not %s", token.toChars()); | |
4149 value = parseInitializer(); | |
4150 ia.addInit(null, value); | |
4151 comma = 1; | |
4152 continue; | |
4153 | |
4154 case TOK.TOKcomma: | |
4155 nextToken(); | |
4156 comma = 2; | |
4157 continue; | |
4158 | |
4159 case TOK.TOKrbracket: // allow trailing comma's | |
4160 nextToken(); | |
4161 break; | |
4162 | |
4163 case TOK.TOKeof: | |
4164 error("found '%s' instead of array initializer", token.toChars()); | |
4165 break; | |
4166 } | |
4167 break; | |
4168 } | |
4169 return ia; | |
4170 | |
4171 case TOK.TOKvoid: | |
4172 t = peek(&token); | |
4173 if (t.value == TOK.TOKsemicolon || t.value == TOK.TOKcomma) | |
4174 { | |
4175 nextToken(); | |
4176 return new VoidInitializer(loc); | |
4177 } | |
4178 goto Lexpression; | |
4179 | |
4180 default: | |
4181 Lexpression: | |
4182 e = parseAssignExp(); | |
4183 ie = new ExpInitializer(loc, e); | |
4184 return ie; | |
4185 } | |
4186 } | |
4187 | |
4188 /***************************************** | |
4189 * Parses default argument initializer expression that is an assign expression, | |
4190 * with special handling for __FILE__ and __LINE__. | |
4191 */ | |
4192 version (DMDV2) { | |
4193 Expression parseDefaultInitExp() | |
4194 { | |
4195 if (token.value == TOK.TOKfile || | |
4196 token.value == TOK.TOKline) | |
4197 { | |
4198 Token* t = peek(&token); | |
4199 if (t.value == TOK.TOKcomma || t.value == TOK.TOKrparen) | |
4200 { | |
4201 Expression e; | |
4202 | |
4203 if (token.value == TOK.TOKfile) | |
4204 e = new FileInitExp(loc); | |
4205 else | |
4206 e = new LineInitExp(loc); | |
4207 nextToken(); | |
4208 return e; | |
4209 } | |
4210 } | |
4211 | |
4212 Expression e = parseAssignExp(); | |
4213 return e; | |
4214 } | |
4215 } | |
4216 void check(Loc loc, TOK value) | |
4217 { | |
4218 if (token.value != value) | |
4219 error(loc, "found '%s' when expecting '%s'", token.toChars(), Token.toChars(value)); | |
4220 nextToken(); | |
4221 } | |
4222 | |
4223 void check(TOK value) | |
4224 { | |
4225 check(loc, value); | |
4226 } | |
4227 | |
4228 void check(TOK value, string string_) | |
4229 { | |
4230 if (token.value != value) { | |
4231 error("found '%s' when expecting '%s' following '%s'", token.toChars(), Token.toChars(value), string_); | |
4232 } | |
4233 nextToken(); | |
4234 } | |
4235 | |
4236 /************************************ | |
4237 * Determine if the scanner is sitting on the start of a declaration. | |
4238 * Input: | |
4239 * needId 0 no identifier | |
4240 * 1 identifier optional | |
4241 * 2 must have identifier | |
4242 * Output: | |
4243 * if *pt is not null, it is set to the ending token, which would be endtok | |
4244 */ | |
4245 | |
4246 bool isDeclaration(Token* t, int needId, TOK endtok, Token** pt) | |
4247 { | |
4248 //printf("isDeclaration(needId = %d)\n", needId); | |
4249 int haveId = 0; | |
4250 | |
4251 version (DMDV2) { | |
4252 if ((t.value == TOK.TOKconst || | |
4253 t.value == TOK.TOKinvariant || | |
4254 t.value == TOK.TOKimmutable || | |
4255 t.value == TOK.TOKshared) && | |
4256 peek(t).value != TOK.TOKlparen) | |
4257 { | |
4258 /* const type | |
4259 * immutable type | |
4260 * shared type | |
4261 */ | |
4262 t = peek(t); | |
4263 } | |
4264 } | |
4265 | |
4266 if (!isBasicType(&t)) | |
4267 { | |
4268 goto Lisnot; | |
4269 } | |
4270 if (!isDeclarator(&t, &haveId, endtok)) | |
4271 goto Lisnot; | |
4272 if ( needId == 1 || | |
4273 (needId == 0 && !haveId) || | |
4274 (needId == 2 && haveId)) | |
4275 { | |
4276 if (pt) | |
4277 *pt = t; | |
4278 goto Lis; | |
4279 } | |
4280 else | |
4281 goto Lisnot; | |
4282 | |
4283 Lis: | |
4284 //printf("\tis declaration, t = %s\n", t.toChars()); | |
4285 return true; | |
4286 | |
4287 Lisnot: | |
4288 //printf("\tis not declaration\n"); | |
4289 return false; | |
4290 } | |
4291 | |
4292 bool isBasicType(Token** pt) | |
4293 { | |
4294 // This code parallels parseBasicType() | |
4295 Token* t = *pt; | |
4296 Token* t2; | |
4297 int parens; | |
4298 int haveId = 0; | |
4299 | |
4300 switch (t.value) | |
4301 { | |
4302 case TOKwchar: | |
4303 case TOKdchar: | |
4304 case TOKbit: | |
4305 case TOKbool: | |
4306 case TOKchar: | |
4307 case TOKint8: | |
4308 case TOKuns8: | |
4309 case TOKint16: | |
4310 case TOKuns16: | |
4311 case TOKint32: | |
4312 case TOKuns32: | |
4313 case TOKint64: | |
4314 case TOKuns64: | |
4315 case TOKfloat32: | |
4316 case TOKfloat64: | |
4317 case TOKfloat80: | |
4318 case TOKimaginary32: | |
4319 case TOKimaginary64: | |
4320 case TOKimaginary80: | |
4321 case TOKcomplex32: | |
4322 case TOKcomplex64: | |
4323 case TOKcomplex80: | |
4324 case TOKvoid: | |
4325 t = peek(t); | |
4326 break; | |
4327 | |
4328 case TOK.TOKidentifier: | |
4329 L5: | |
4330 t = peek(t); | |
4331 if (t.value == TOK.TOKnot) | |
4332 { | |
4333 goto L4; | |
4334 } | |
4335 goto L3; | |
4336 while (1) | |
4337 { | |
4338 L2: | |
4339 t = peek(t); | |
4340 L3: | |
4341 if (t.value == TOK.TOKdot) | |
4342 { | |
4343 Ldot: | |
4344 t = peek(t); | |
4345 if (t.value != TOK.TOKidentifier) | |
4346 goto Lfalse; | |
4347 t = peek(t); | |
4348 if (t.value != TOK.TOKnot) | |
4349 goto L3; | |
4350 L4: | |
4351 /* Seen a ! | |
4352 * Look for: | |
4353 * !( args ), !identifier, etc. | |
4354 */ | |
4355 t = peek(t); | |
4356 switch (t.value) | |
4357 { case TOK.TOKidentifier: | |
4358 goto L5; | |
4359 case TOK.TOKlparen: | |
4360 if (!skipParens(t, &t)) | |
4361 goto Lfalse; | |
4362 break; | |
4363 case TOK.TOKwchar: case TOK.TOKdchar: | |
4364 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
4365 case TOK.TOKint8: case TOK.TOKuns8: | |
4366 case TOK.TOKint16: case TOK.TOKuns16: | |
4367 case TOK.TOKint32: case TOK.TOKuns32: | |
4368 case TOK.TOKint64: case TOK.TOKuns64: | |
4369 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
4370 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
4371 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
4372 case TOK.TOKvoid: | |
4373 case TOK.TOKint32v: | |
4374 case TOK.TOKuns32v: | |
4375 case TOK.TOKint64v: | |
4376 case TOK.TOKuns64v: | |
4377 case TOK.TOKfloat32v: | |
4378 case TOK.TOKfloat64v: | |
4379 case TOK.TOKfloat80v: | |
4380 case TOK.TOKimaginary32v: | |
4381 case TOK.TOKimaginary64v: | |
4382 case TOK.TOKimaginary80v: | |
4383 case TOK.TOKnull: | |
4384 case TOK.TOKtrue: | |
4385 case TOK.TOKfalse: | |
4386 case TOK.TOKcharv: | |
4387 case TOK.TOKwcharv: | |
4388 case TOK.TOKdcharv: | |
4389 case TOK.TOKstring: | |
4390 case TOK.TOKfile: | |
4391 case TOK.TOKline: | |
4392 goto L2; | |
4393 default: | |
4394 goto Lfalse; | |
4395 } | |
4396 } | |
4397 else | |
4398 break; | |
4399 } | |
4400 break; | |
4401 | |
4402 case TOK.TOKdot: | |
4403 goto Ldot; | |
4404 | |
4405 case TOK.TOKtypeof: | |
4406 /* typeof(exp).identifier... | |
4407 */ | |
4408 t = peek(t); | |
4409 if (t.value != TOK.TOKlparen) | |
4410 goto Lfalse; | |
4411 if (!skipParens(t, &t)) | |
4412 goto Lfalse; | |
4413 goto L2; | |
4414 | |
4415 case TOK.TOKconst: | |
4416 case TOK.TOKinvariant: | |
4417 case TOK.TOKimmutable: | |
4418 case TOK.TOKshared: | |
4419 // const(type) or immutable(type) or shared(type) | |
4420 t = peek(t); | |
4421 if (t.value != TOK.TOKlparen) | |
4422 goto Lfalse; | |
4423 t = peek(t); | |
4424 if (!isDeclaration(t, 0, TOK.TOKrparen, &t)) | |
4425 { | |
4426 goto Lfalse; | |
4427 } | |
4428 t = peek(t); | |
4429 break; | |
4430 | |
4431 default: | |
4432 goto Lfalse; | |
4433 } | |
4434 *pt = t; | |
4435 //printf("is\n"); | |
4436 return true; | |
4437 | |
4438 Lfalse: | |
4439 //printf("is not\n"); | |
4440 return false; | |
4441 } | |
4442 | |
4443 bool isDeclarator(Token** pt, int* haveId, TOK endtok) | |
4444 { | |
4445 // This code parallels parseDeclarator() | |
4446 Token* t = *pt; | |
4447 bool parens; | |
4448 | |
4449 //printf("Parser.isDeclarator()\n"); | |
4450 //t.print(); | |
4451 if (t.value == TOK.TOKassign) | |
4452 return false; | |
4453 | |
4454 while (true) | |
4455 { | |
4456 parens = false; | |
4457 switch (t.value) | |
4458 { | |
4459 case TOK.TOKmul: | |
4460 //case TOKand: | |
4461 t = peek(t); | |
4462 continue; | |
4463 | |
4464 case TOK.TOKlbracket: | |
4465 t = peek(t); | |
4466 if (t.value == TOK.TOKrbracket) | |
4467 { | |
4468 t = peek(t); | |
4469 } | |
4470 else if (isDeclaration(t, 0, TOK.TOKrbracket, &t)) | |
4471 { | |
4472 // It's an associative array declaration | |
4473 t = peek(t); | |
4474 } | |
4475 else | |
4476 { | |
4477 // [ expression ] | |
4478 // [ expression .. expression ] | |
4479 if (!isExpression(&t)) | |
4480 return false; | |
4481 if (t.value == TOK.TOKslice) | |
4482 { | |
4483 t = peek(t); | |
4484 if (!isExpression(&t)) | |
4485 return false; | |
4486 } | |
4487 if (t.value != TOK.TOKrbracket) | |
4488 return false; | |
4489 t = peek(t); | |
4490 } | |
4491 continue; | |
4492 | |
4493 case TOK.TOKidentifier: | |
4494 if (*haveId) | |
4495 return false; | |
4496 *haveId = true; | |
4497 t = peek(t); | |
4498 break; | |
4499 | |
4500 case TOK.TOKlparen: | |
4501 t = peek(t); | |
4502 | |
4503 if (t.value == TOK.TOKrparen) | |
4504 return false; // () is not a declarator | |
4505 | |
4506 /* Regard ( identifier ) as not a declarator | |
4507 * BUG: what about ( *identifier ) in | |
4508 * f(*p)(x); | |
4509 * where f is a class instance with overloaded () ? | |
4510 * Should we just disallow C-style function pointer declarations? | |
4511 */ | |
4512 if (t.value == TOK.TOKidentifier) | |
4513 { | |
4514 Token *t2 = peek(t); | |
4515 if (t2.value == TOK.TOKrparen) | |
4516 return false; | |
4517 } | |
4518 | |
4519 if (!isDeclarator(&t, haveId, TOK.TOKrparen)) | |
4520 return false; | |
4521 t = peek(t); | |
4522 parens = true; | |
4523 break; | |
4524 | |
4525 case TOK.TOKdelegate: | |
4526 case TOK.TOKfunction: | |
4527 t = peek(t); | |
4528 if (!isParameters(&t)) | |
4529 return false; | |
4530 continue; | |
4531 default: | |
4532 break; /// | |
4533 } | |
4534 break; | |
4535 } | |
4536 | |
4537 while (1) | |
4538 { | |
4539 switch (t.value) | |
4540 { | |
4541 version (CARRAYDECL) { | |
4542 case TOK.TOKlbracket: | |
4543 parens = false; | |
4544 t = peek(t); | |
4545 if (t.value == TOK.TOKrbracket) | |
4546 { | |
4547 t = peek(t); | |
4548 } | |
4549 else if (isDeclaration(t, 0, TOKrbracket, &t)) | |
4550 { | |
4551 // It's an associative array declaration | |
4552 t = peek(t); | |
4553 } | |
4554 else | |
4555 { | |
4556 // [ expression ] | |
4557 if (!isExpression(&t)) | |
4558 return false; | |
4559 if (t.value != TOK.TOKrbracket) | |
4560 return false; | |
4561 t = peek(t); | |
4562 } | |
4563 continue; | |
4564 } | |
4565 | |
4566 case TOK.TOKlparen: | |
4567 parens = false; | |
4568 if (!isParameters(&t)) | |
4569 return false; | |
4570 version (DMDV2) { | |
4571 while (true) | |
4572 { | |
4573 switch (t.value) | |
4574 { | |
4575 case TOK.TOKconst: | |
4576 case TOK.TOKinvariant: | |
4577 case TOK.TOKimmutable: | |
4578 case TOK.TOKshared: | |
4579 case TOK.TOKpure: | |
4580 case TOK.TOKnothrow: | |
4581 t = peek(t); | |
4582 continue; | |
4583 case TOK.TOKat: | |
4584 t = peek(t); // skip '@' | |
4585 t = peek(t); // skip identifier | |
4586 continue; | |
4587 default: | |
4588 break; | |
4589 } | |
4590 break; | |
4591 } | |
4592 } | |
4593 continue; | |
4594 | |
4595 // Valid tokens that follow a declaration | |
4596 case TOK.TOKrparen: | |
4597 case TOK.TOKrbracket: | |
4598 case TOK.TOKassign: | |
4599 case TOK.TOKcomma: | |
4600 case TOK.TOKsemicolon: | |
4601 case TOK.TOKlcurly: | |
4602 case TOK.TOKin: | |
4603 // The !parens is to disallow unnecessary parentheses | |
4604 if (!parens && (endtok == TOK.TOKreserved || endtok == t.value)) | |
4605 { | |
4606 *pt = t; | |
4607 return true; | |
4608 } | |
4609 return false; | |
4610 | |
4611 default: | |
4612 return false; | |
4613 } | |
4614 } | |
4615 } | |
4616 | |
4617 bool isParameters(Token** pt) | |
4618 { | |
4619 // This code parallels parseParameters() | |
4620 Token* t = *pt; | |
4621 | |
4622 //printf("isParameters()\n"); | |
4623 if (t.value != TOKlparen) | |
4624 return false; | |
4625 | |
4626 t = peek(t); | |
4627 for (;1; t = peek(t)) | |
4628 { | |
4629 L1: | |
4630 switch (t.value) | |
4631 { | |
4632 case TOKrparen: | |
4633 break; | |
4634 | |
4635 case TOKdotdotdot: | |
4636 t = peek(t); | |
4637 break; | |
4638 | |
4639 case TOKin: | |
4640 case TOKout: | |
4641 case TOKinout: | |
4642 case TOKref: | |
4643 case TOKlazy: | |
4644 case TOKfinal: | |
4645 continue; | |
4646 | |
4647 case TOKconst: | |
4648 case TOKinvariant: | |
4649 case TOKimmutable: | |
4650 case TOKshared: | |
4651 t = peek(t); | |
4652 if (t.value == TOKlparen) | |
4653 { | |
4654 t = peek(t); | |
4655 if (!isDeclaration(t, 0, TOKrparen, &t)) | |
4656 return false; | |
4657 t = peek(t); // skip past closing ')' | |
4658 goto L2; | |
4659 } | |
4660 goto L1; | |
4661 | |
4662 static if (false) { | |
4663 case TOKstatic: | |
4664 continue; | |
4665 case TOKauto: | |
4666 case TOKalias: | |
4667 t = peek(t); | |
4668 if (t.value == TOKidentifier) | |
4669 t = peek(t); | |
4670 if (t.value == TOKassign) | |
4671 { t = peek(t); | |
4672 if (!isExpression(&t)) | |
4673 return false; | |
4674 } | |
4675 goto L3; | |
4676 } | |
4677 | |
4678 default: | |
4679 { | |
4680 if (!isBasicType(&t)) | |
4681 return false; | |
4682 L2: | |
4683 int tmp = false; | |
4684 if (t.value != TOKdotdotdot && | |
4685 !isDeclarator(&t, &tmp, TOKreserved)) | |
4686 return false; | |
4687 if (t.value == TOKassign) | |
4688 { | |
4689 t = peek(t); | |
4690 if (!isExpression(&t)) | |
4691 return false; | |
4692 } | |
4693 if (t.value == TOKdotdotdot) | |
4694 { | |
4695 t = peek(t); | |
4696 break; | |
4697 } | |
4698 } | |
4699 L3: | |
4700 if (t.value == TOKcomma) | |
4701 { | |
4702 continue; | |
4703 } | |
4704 break; | |
4705 } | |
4706 break; | |
4707 } | |
4708 | |
4709 if (t.value != TOKrparen) | |
4710 return false; | |
4711 | |
4712 t = peek(t); | |
4713 *pt = t; | |
4714 return true; | |
4715 } | |
4716 | |
4717 bool isExpression(Token** pt) | |
4718 { | |
4719 // This is supposed to determine if something is an expression. | |
4720 // What it actually does is scan until a closing right bracket | |
4721 // is found. | |
4722 | |
4723 Token* t = *pt; | |
4724 int brnest = 0; | |
4725 int panest = 0; | |
4726 int curlynest = 0; | |
4727 | |
4728 for (;; t = peek(t)) | |
4729 { | |
4730 switch (t.value) | |
4731 { | |
4732 case TOKlbracket: | |
4733 brnest++; | |
4734 continue; | |
4735 | |
4736 case TOKrbracket: | |
4737 if (--brnest >= 0) | |
4738 continue; | |
4739 break; | |
4740 | |
4741 case TOKlparen: | |
4742 panest++; | |
4743 continue; | |
4744 | |
4745 case TOKcomma: | |
4746 if (brnest || panest) | |
4747 continue; | |
4748 break; | |
4749 | |
4750 case TOKrparen: | |
4751 if (--panest >= 0) | |
4752 continue; | |
4753 break; | |
4754 | |
4755 case TOKlcurly: | |
4756 curlynest++; | |
4757 continue; | |
4758 | |
4759 case TOKrcurly: | |
4760 if (--curlynest >= 0) | |
4761 continue; | |
4762 return false; | |
4763 | |
4764 case TOKslice: | |
4765 if (brnest) | |
4766 continue; | |
4767 break; | |
4768 | |
4769 case TOKsemicolon: | |
4770 if (curlynest) | |
4771 continue; | |
4772 return false; | |
4773 | |
4774 case TOKeof: | |
4775 return false; | |
4776 | |
4777 default: | |
4778 continue; | |
4779 } | |
4780 break; | |
4781 } | |
4782 | |
4783 *pt = t; | |
4784 return true; | |
4785 } | |
4786 | |
4787 int isTemplateInstance(Token t, Token* pt) | |
4788 { | |
4789 assert(false); | |
4790 } | |
4791 | |
4792 /******************************************* | |
4793 * Skip parens, brackets. | |
4794 * Input: | |
4795 * t is on opening ( | |
4796 * Output: | |
4797 * *pt is set to closing token, which is ')' on success | |
4798 * Returns: | |
4799 * !=0 successful | |
4800 * 0 some parsing error | |
4801 */ | |
4802 bool skipParens(Token* t, Token** pt) | |
4803 { | |
4804 int parens = 0; | |
4805 | |
4806 while (1) | |
4807 { | |
4808 switch (t.value) | |
4809 { | |
4810 case TOKlparen: | |
4811 parens++; | |
4812 break; | |
4813 | |
4814 case TOKrparen: | |
4815 parens--; | |
4816 if (parens < 0) | |
4817 goto Lfalse; | |
4818 if (parens == 0) | |
4819 goto Ldone; | |
4820 break; | |
4821 | |
4822 case TOKeof: | |
4823 case TOKsemicolon: | |
4824 goto Lfalse; | |
4825 | |
4826 default: | |
4827 break; | |
4828 } | |
4829 t = peek(t); | |
4830 } | |
4831 | |
4832 Ldone: | |
4833 if (*pt) | |
4834 *pt = t; | |
4835 return true; | |
4836 | |
4837 Lfalse: | |
4838 return false; | |
4839 } | |
4840 | |
4841 Expression parseExpression() | |
4842 { | |
4843 Expression e; | |
4844 Expression e2; | |
4845 Loc loc = this.loc; | |
4846 | |
4847 //printf("Parser.parseExpression() loc = %d\n", loc.linnum); | |
4848 e = parseAssignExp(); | |
4849 while (token.value == TOK.TOKcomma) | |
4850 { | |
4851 nextToken(); | |
4852 e2 = parseAssignExp(); | |
4853 e = new CommaExp(loc, e, e2); | |
4854 loc = this.loc; | |
4855 } | |
4856 return e; | |
4857 } | |
4858 | |
4859 Expression parsePrimaryExp() | |
4860 { | |
4861 Expression e; | |
4862 Type t; | |
4863 Identifier id; | |
4864 TOK save; | |
4865 Loc loc = this.loc; | |
4866 | |
4867 //printf("parsePrimaryExp(): loc = %d\n", loc.linnum); | |
4868 switch (token.value) | |
4869 { | |
4870 case TOK.TOKidentifier: | |
4871 id = token.ident; | |
4872 nextToken(); | |
4873 if (token.value == TOK.TOKnot && peekNext() != TOK.TOKis) | |
4874 { // identifier!(template-argument-list) | |
4875 TemplateInstance tempinst; | |
4876 | |
4877 tempinst = new TemplateInstance(loc, id); | |
4878 nextToken(); | |
4879 if (token.value == TOK.TOKlparen) | |
4880 // ident!(template_arguments) | |
4881 tempinst.tiargs = parseTemplateArgumentList(); | |
4882 else | |
4883 // ident!template_argument | |
4884 tempinst.tiargs = parseTemplateArgument(); | |
4885 e = new ScopeExp(loc, tempinst); | |
4886 } | |
4887 else | |
4888 e = new IdentifierExp(loc, id); | |
4889 break; | |
4890 | |
4891 case TOK.TOKdollar: | |
4892 if (!inBrackets) | |
4893 error("'$' is valid only inside [] of index or slice"); | |
4894 e = new DollarExp(loc); | |
4895 nextToken(); | |
4896 break; | |
4897 | |
4898 case TOK.TOKdot: | |
4899 // Signal global scope '.' operator with "" identifier | |
4900 e = new IdentifierExp(loc, Id.empty); | |
4901 break; | |
4902 | |
4903 case TOK.TOKthis: | |
4904 e = new ThisExp(loc); | |
4905 nextToken(); | |
4906 break; | |
4907 | |
4908 case TOK.TOKsuper: | |
4909 e = new SuperExp(loc); | |
4910 nextToken(); | |
4911 break; | |
4912 | |
4913 case TOK.TOKint32v: | |
4914 e = new IntegerExp(loc, token.int32value, Type.tint32); | |
4915 nextToken(); | |
4916 break; | |
4917 | |
4918 case TOK.TOKuns32v: | |
4919 e = new IntegerExp(loc, token.uns32value, Type.tuns32); | |
4920 nextToken(); | |
4921 break; | |
4922 | |
4923 case TOK.TOKint64v: | |
4924 e = new IntegerExp(loc, token.int64value, Type.tint64); | |
4925 nextToken(); | |
4926 break; | |
4927 | |
4928 case TOK.TOKuns64v: | |
4929 e = new IntegerExp(loc, token.uns64value, Type.tuns64); | |
4930 nextToken(); | |
4931 break; | |
4932 | |
4933 case TOK.TOKfloat32v: | |
4934 e = new RealExp(loc, token.float80value, Type.tfloat32); | |
4935 nextToken(); | |
4936 break; | |
4937 | |
4938 case TOK.TOKfloat64v: | |
4939 e = new RealExp(loc, token.float80value, Type.tfloat64); | |
4940 nextToken(); | |
4941 break; | |
4942 | |
4943 case TOK.TOKfloat80v: | |
4944 e = new RealExp(loc, token.float80value, Type.tfloat80); | |
4945 nextToken(); | |
4946 break; | |
4947 | |
4948 case TOK.TOKimaginary32v: | |
4949 e = new RealExp(loc, token.float80value, Type.timaginary32); | |
4950 nextToken(); | |
4951 break; | |
4952 | |
4953 case TOK.TOKimaginary64v: | |
4954 e = new RealExp(loc, token.float80value, Type.timaginary64); | |
4955 nextToken(); | |
4956 break; | |
4957 | |
4958 case TOK.TOKimaginary80v: | |
4959 e = new RealExp(loc, token.float80value, Type.timaginary80); | |
4960 nextToken(); | |
4961 break; | |
4962 | |
4963 case TOK.TOKnull: | |
4964 e = new NullExp(loc); | |
4965 nextToken(); | |
4966 break; | |
4967 | |
4968 version (DMDV2) { | |
4969 case TOK.TOKfile: | |
4970 { | |
4971 string s = loc.filename ? loc.filename : mod.ident.toChars(); | |
4972 e = new StringExp(loc, s, 0); | |
4973 nextToken(); | |
4974 break; | |
4975 } | |
4976 | |
4977 case TOK.TOKline: | |
4978 e = new IntegerExp(loc, loc.linnum, Type.tint32); | |
4979 nextToken(); | |
4980 break; | |
4981 } | |
4982 | |
4983 case TOK.TOKtrue: | |
4984 e = new IntegerExp(loc, 1, Type.tbool); | |
4985 nextToken(); | |
4986 break; | |
4987 | |
4988 case TOK.TOKfalse: | |
4989 e = new IntegerExp(loc, 0, Type.tbool); | |
4990 nextToken(); | |
4991 break; | |
4992 | |
4993 case TOK.TOKcharv: | |
4994 e = new IntegerExp(loc, token.uns32value, Type.tchar); | |
4995 nextToken(); | |
4996 break; | |
4997 | |
4998 case TOK.TOKwcharv: | |
4999 e = new IntegerExp(loc, token.uns32value, Type.twchar); | |
5000 nextToken(); | |
5001 break; | |
5002 | |
5003 case TOK.TOKdcharv: | |
5004 e = new IntegerExp(loc, token.uns32value, Type.tdchar); | |
5005 nextToken(); | |
5006 break; | |
5007 | |
5008 case TOK.TOKstring: | |
5009 { | |
5010 const(char)* s; | |
5011 uint len; | |
5012 ubyte postfix; | |
5013 | |
5014 // cat adjacent strings | |
5015 s = token.ustring; | |
5016 len = token.len; | |
5017 postfix = token.postfix; | |
5018 while (1) | |
5019 { | |
5020 nextToken(); | |
5021 if (token.value == TOK.TOKstring) | |
5022 { uint len1; | |
5023 uint len2; | |
5024 char* s2; | |
5025 | |
5026 if (token.postfix) | |
5027 { if (token.postfix != postfix) | |
5028 error("mismatched string literal postfixes '%c' and '%c'", postfix, token.postfix); | |
5029 postfix = token.postfix; | |
5030 } | |
5031 | |
5032 len1 = len; | |
5033 len2 = token.len; | |
5034 len = len1 + len2; | |
2 | 5035 s2 = cast(char*)GC.malloc((len + 1) * ubyte.sizeof); |
0 | 5036 memcpy(s2, s, len1 * ubyte.sizeof); |
5037 memcpy(s2 + len1, token.ustring, (len2 + 1) * ubyte.sizeof); | |
5038 s = s2; | |
5039 } | |
5040 else | |
5041 break; | |
5042 } | |
5043 e = new StringExp(loc, assumeUnique(s[0..len]), postfix); | |
5044 break; | |
5045 } | |
5046 | |
5047 case TOK.TOKvoid: t = Type.tvoid; goto LabelX; | |
5048 case TOK.TOKint8: t = Type.tint8; goto LabelX; | |
5049 case TOK.TOKuns8: t = Type.tuns8; goto LabelX; | |
5050 case TOK.TOKint16: t = Type.tint16; goto LabelX; | |
5051 case TOK.TOKuns16: t = Type.tuns16; goto LabelX; | |
5052 case TOK.TOKint32: t = Type.tint32; goto LabelX; | |
5053 case TOK.TOKuns32: t = Type.tuns32; goto LabelX; | |
5054 case TOK.TOKint64: t = Type.tint64; goto LabelX; | |
5055 case TOK.TOKuns64: t = Type.tuns64; goto LabelX; | |
5056 case TOK.TOKfloat32: t = Type.tfloat32; goto LabelX; | |
5057 case TOK.TOKfloat64: t = Type.tfloat64; goto LabelX; | |
5058 case TOK.TOKfloat80: t = Type.tfloat80; goto LabelX; | |
5059 case TOK.TOKimaginary32: t = Type.timaginary32; goto LabelX; | |
5060 case TOK.TOKimaginary64: t = Type.timaginary64; goto LabelX; | |
5061 case TOK.TOKimaginary80: t = Type.timaginary80; goto LabelX; | |
5062 case TOK.TOKcomplex32: t = Type.tcomplex32; goto LabelX; | |
5063 case TOK.TOKcomplex64: t = Type.tcomplex64; goto LabelX; | |
5064 case TOK.TOKcomplex80: t = Type.tcomplex80; goto LabelX; | |
5065 case TOK.TOKbit: t = Type.tbit; goto LabelX; | |
5066 case TOK.TOKbool: t = Type.tbool; goto LabelX; | |
5067 case TOK.TOKchar: t = Type.tchar; goto LabelX; | |
5068 case TOK.TOKwchar: t = Type.twchar; goto LabelX; | |
5069 case TOK.TOKdchar: t = Type.tdchar; goto LabelX; | |
5070 LabelX: | |
5071 nextToken(); | |
5072 L1: | |
5073 check(TOK.TOKdot, t.toChars()); | |
5074 if (token.value != TOK.TOKidentifier) | |
5075 { error("found '%s' when expecting identifier following '%s.'", token.toChars(), t.toChars()); | |
5076 goto Lerr; | |
5077 } | |
5078 e = typeDotIdExp(loc, t, token.ident); | |
5079 nextToken(); | |
5080 break; | |
5081 | |
5082 case TOK.TOKtypeof: | |
5083 { | |
5084 t = parseTypeof(); | |
5085 e = new TypeExp(loc, t); | |
5086 break; | |
5087 } | |
5088 | |
5089 case TOK.TOKtypeid: | |
5090 { Type tt; | |
5091 | |
5092 nextToken(); | |
5093 check(TOK.TOKlparen, "typeid"); | |
5094 tt = parseType(); // ( type ) | |
5095 check(TOK.TOKrparen); | |
5096 e = new TypeidExp(loc, tt); | |
5097 break; | |
5098 } | |
5099 | |
5100 version (DMDV2) { | |
5101 case TOK.TOKtraits: | |
5102 { /* __traits(identifier, args...) | |
5103 */ | |
5104 Identifier ident; | |
5105 Objects args = null; | |
5106 | |
5107 nextToken(); | |
5108 check(TOK.TOKlparen); | |
5109 if (token.value != TOK.TOKidentifier) | |
5110 { error("__traits(identifier, args...) expected"); | |
5111 goto Lerr; | |
5112 } | |
5113 ident = token.ident; | |
5114 nextToken(); | |
5115 if (token.value == TOK.TOKcomma) | |
5116 args = parseTemplateArgumentList2(); // __traits(identifier, args...) | |
5117 else | |
5118 check(TOK.TOKrparen); // __traits(identifier) | |
5119 | |
5120 e = new TraitsExp(loc, ident, args); | |
5121 break; | |
5122 } | |
5123 } | |
5124 | |
5125 case TOK.TOKis: | |
5126 { Type targ; | |
5127 Identifier ident = null; | |
5128 Type tspec = null; | |
5129 TOK tok = TOK.TOKreserved; | |
5130 TOK tok2 = TOK.TOKreserved; | |
5131 TemplateParameters tpl = null; | |
5132 Loc loc2 = this.loc; | |
5133 | |
5134 nextToken(); | |
5135 if (token.value == TOK.TOKlparen) | |
5136 { | |
5137 nextToken(); | |
5138 targ = parseType(&ident); | |
5139 if (token.value == TOK.TOKcolon || token.value == TOK.TOKequal) | |
5140 { | |
5141 tok = token.value; | |
5142 nextToken(); | |
5143 if (tok == TOK.TOKequal && | |
5144 (token.value == TOK.TOKtypedef || | |
5145 token.value == TOK.TOKstruct || | |
5146 token.value == TOK.TOKunion || | |
5147 token.value == TOK.TOKclass || | |
5148 token.value == TOK.TOKsuper || | |
5149 token.value == TOK.TOKenum || | |
5150 token.value == TOK.TOKinterface || | |
5151 ///version (DMDV2) { | |
5152 token.value == TOK.TOKconst && peek(&token).value == TOK.TOKrparen || | |
5153 token.value == TOK.TOKinvariant && peek(&token).value == TOK.TOKrparen || | |
5154 token.value == TOK.TOKimmutable && peek(&token).value == TOK.TOKrparen || | |
5155 token.value == TOK.TOKshared && peek(&token).value == TOK.TOKrparen || | |
5156 ///} | |
5157 token.value == TOK.TOKfunction || | |
5158 token.value == TOK.TOKdelegate || | |
5159 token.value == TOK.TOKreturn)) | |
5160 { | |
5161 tok2 = token.value; | |
5162 nextToken(); | |
5163 } | |
5164 else | |
5165 { | |
5166 tspec = parseType(); | |
5167 } | |
5168 } | |
5169 if (ident && tspec) | |
5170 { | |
5171 if (token.value == TOK.TOKcomma) | |
5172 tpl = parseTemplateParameterList(1); | |
5173 else | |
5174 { tpl = new TemplateParameters(); | |
5175 check(TOK.TOKrparen); | |
5176 } | |
5177 TemplateParameter tp = new TemplateTypeParameter(loc2, ident, null, null); | |
5178 tpl.insert(0, cast(void*)tp); | |
5179 } | |
5180 else | |
5181 check(TOK.TOKrparen); | |
5182 } | |
5183 else | |
5184 { error("(type identifier : specialization) expected following is"); | |
5185 goto Lerr; | |
5186 } | |
5187 e = new IsExp(loc2, targ, ident, tok, tspec, tok2, tpl); | |
5188 break; | |
5189 } | |
5190 | |
5191 case TOK.TOKassert: | |
5192 { | |
5193 Expression msg = null; | |
5194 | |
5195 nextToken(); | |
5196 check(TOK.TOKlparen, "assert"); | |
5197 e = parseAssignExp(); | |
5198 if (token.value == TOK.TOKcomma) | |
5199 { nextToken(); | |
5200 msg = parseAssignExp(); | |
5201 } | |
5202 check(TOK.TOKrparen); | |
5203 e = new AssertExp(loc, e, msg); | |
5204 break; | |
5205 } | |
5206 | |
5207 case TOK.TOKmixin: | |
5208 { | |
5209 nextToken(); | |
5210 check(TOK.TOKlparen, "mixin"); | |
5211 e = parseAssignExp(); | |
5212 check(TOK.TOKrparen); | |
5213 e = new CompileExp(loc, e); | |
5214 break; | |
5215 } | |
5216 | |
5217 case TOK.TOKimport: | |
5218 { | |
5219 nextToken(); | |
5220 check(TOK.TOKlparen, "import"); | |
5221 e = parseAssignExp(); | |
5222 check(TOK.TOKrparen); | |
5223 e = new FileExp(loc, e); | |
5224 break; | |
5225 } | |
5226 | |
5227 case TOK.TOKlparen: | |
5228 if (peekPastParen(&token).value == TOK.TOKlcurly) | |
5229 { // (arguments) { statements... } | |
5230 save = TOK.TOKdelegate; | |
5231 goto case_delegate; | |
5232 } | |
5233 // ( expression ) | |
5234 nextToken(); | |
5235 e = parseExpression(); | |
5236 check(loc, TOK.TOKrparen); | |
5237 break; | |
5238 | |
5239 case TOK.TOKlbracket: | |
5240 { /* Parse array literals and associative array literals: | |
5241 * [ value, value, value ... ] | |
5242 * [ key:value, key:value, key:value ... ] | |
5243 */ | |
5244 Expressions values = new Expressions(); | |
5245 Expressions keys = null; | |
5246 | |
5247 nextToken(); | |
5248 if (token.value != TOK.TOKrbracket) | |
5249 { | |
5250 while (token.value != TOK.TOKeof) | |
5251 { | |
5252 Expression e2 = parseAssignExp(); | |
5253 if (token.value == TOK.TOKcolon && (keys || values.dim == 0)) | |
5254 { | |
5255 nextToken(); | |
5256 if (!keys) | |
5257 keys = new Expressions(); | |
5258 keys.push(cast(void*)e2); | |
5259 e2 = parseAssignExp(); | |
5260 } | |
5261 else if (keys) | |
5262 { | |
5263 error("'key:value' expected for associative array literal"); | |
5264 delete keys; | |
5265 keys = null; | |
5266 } | |
5267 values.push(cast(void*)e2); | |
5268 if (token.value == TOK.TOKrbracket) | |
5269 break; | |
5270 check(TOK.TOKcomma); | |
5271 } | |
5272 } | |
5273 check(TOK.TOKrbracket); | |
5274 | |
5275 if (keys) | |
5276 e = new AssocArrayLiteralExp(loc, keys, values); | |
5277 else | |
5278 e = new ArrayLiteralExp(loc, values); | |
5279 break; | |
5280 } | |
5281 | |
5282 case TOK.TOKlcurly: | |
5283 // { statements... } | |
5284 save = TOK.TOKdelegate; | |
5285 goto case_delegate; | |
5286 | |
5287 case TOK.TOKfunction: | |
5288 case TOK.TOKdelegate: | |
5289 save = token.value; | |
5290 nextToken(); | |
5291 case_delegate: | |
5292 { | |
5293 /* function type(parameters) { body } pure nothrow | |
5294 * delegate type(parameters) { body } pure nothrow | |
5295 * (parameters) { body } | |
5296 * { body } | |
5297 */ | |
5298 Arguments arguments; | |
5299 int varargs; | |
5300 FuncLiteralDeclaration fd; | |
5301 Type tt; | |
5302 bool isnothrow = false; | |
5303 bool ispure = false; | |
5304 | |
5305 if (token.value == TOK.TOKlcurly) | |
5306 { | |
5307 tt = null; | |
5308 varargs = 0; | |
5309 arguments = new Arguments(); | |
5310 } | |
5311 else | |
5312 { | |
5313 if (token.value == TOK.TOKlparen) | |
5314 tt = null; | |
5315 else | |
5316 { | |
5317 tt = parseBasicType(); | |
5318 tt = parseBasicType2(tt); // function return type | |
5319 } | |
5320 arguments = parseParameters(&varargs); | |
5321 while (1) | |
5322 { | |
5323 if (token.value == TOK.TOKpure) | |
5324 ispure = true; | |
5325 else if (token.value == TOK.TOKnothrow) | |
5326 isnothrow = true; | |
5327 else | |
5328 break; | |
5329 nextToken(); | |
5330 } | |
5331 } | |
5332 | |
5333 TypeFunction tf = new TypeFunction(arguments, tt, varargs, linkage); | |
5334 tf.ispure = ispure; | |
5335 tf.isnothrow = isnothrow; | |
5336 fd = new FuncLiteralDeclaration(loc, Loc(0), tf, save, null); | |
5337 parseContracts(fd); | |
5338 e = new FuncExp(loc, fd); | |
5339 break; | |
5340 } | |
5341 | |
5342 default: | |
5343 error("expression expected, not '%s'", token.toChars()); | |
5344 Lerr: | |
5345 // Anything for e, as long as it's not null | |
5346 e = new IntegerExp(loc, 0, Type.tint32); | |
5347 nextToken(); | |
5348 break; | |
5349 } | |
5350 return e; | |
5351 } | |
5352 | |
5353 Expression parseUnaryExp() | |
5354 { | |
5355 Expression e; | |
5356 Loc loc = this.loc; | |
5357 | |
5358 switch (token.value) | |
5359 { | |
5360 case TOK.TOKand: | |
5361 nextToken(); | |
5362 e = parseUnaryExp(); | |
5363 e = new AddrExp(loc, e); | |
5364 break; | |
5365 | |
5366 case TOK.TOKplusplus: | |
5367 nextToken(); | |
5368 e = parseUnaryExp(); | |
5369 e = new AddAssignExp(loc, e, new IntegerExp(loc, 1, Type.tint32)); | |
5370 break; | |
5371 | |
5372 case TOK.TOKminusminus: | |
5373 nextToken(); | |
5374 e = parseUnaryExp(); | |
5375 e = new MinAssignExp(loc, e, new IntegerExp(loc, 1, Type.tint32)); | |
5376 break; | |
5377 | |
5378 case TOK.TOKmul: | |
5379 nextToken(); | |
5380 e = parseUnaryExp(); | |
5381 e = new PtrExp(loc, e); | |
5382 break; | |
5383 | |
5384 case TOK.TOKmin: | |
5385 nextToken(); | |
5386 e = parseUnaryExp(); | |
5387 e = new NegExp(loc, e); | |
5388 break; | |
5389 | |
5390 case TOK.TOKadd: | |
5391 nextToken(); | |
5392 e = parseUnaryExp(); | |
5393 e = new UAddExp(loc, e); | |
5394 break; | |
5395 | |
5396 case TOK.TOKnot: | |
5397 nextToken(); | |
5398 e = parseUnaryExp(); | |
5399 e = new NotExp(loc, e); | |
5400 break; | |
5401 | |
5402 case TOK.TOKtilde: | |
5403 nextToken(); | |
5404 e = parseUnaryExp(); | |
5405 e = new ComExp(loc, e); | |
5406 break; | |
5407 | |
5408 case TOK.TOKdelete: | |
5409 nextToken(); | |
5410 e = parseUnaryExp(); | |
5411 e = new DeleteExp(loc, e); | |
5412 break; | |
5413 | |
5414 case TOK.TOKnew: | |
5415 e = parseNewExp(null); | |
5416 break; | |
5417 | |
5418 case TOK.TOKcast: // cast(type) expression | |
5419 { | |
5420 nextToken(); | |
5421 check(TOK.TOKlparen); | |
5422 /* Look for cast(), cast(const), cast(immutable), | |
5423 * cast(shared), cast(shared const) | |
5424 */ | |
5425 MOD m; | |
5426 if (token.value == TOK.TOKrparen) | |
5427 { | |
5428 m = MOD.MODundefined; | |
5429 goto Lmod1; | |
5430 } | |
5431 else if (token.value == TOK.TOKconst && peekNext() == TOK.TOKrparen) | |
5432 { | |
5433 m = MOD.MODconst; | |
5434 goto Lmod2; | |
5435 } | |
5436 else if ((token.value == TOK.TOKimmutable || token.value == TOK.TOKinvariant) && peekNext() == TOK.TOKrparen) | |
5437 { | |
5438 m = MOD.MODinvariant; | |
5439 goto Lmod2; | |
5440 } | |
5441 else if (token.value == TOK.TOKshared && peekNext() == TOK.TOKrparen) | |
5442 { | |
5443 m = MOD.MODshared; | |
5444 goto Lmod2; | |
5445 } | |
5446 else if (token.value == TOK.TOKconst && peekNext() == TOK.TOKshared && peekNext2() == TOK.TOKrparen || | |
5447 token.value == TOK.TOKshared && peekNext() == TOK.TOKconst && peekNext2() == TOK.TOKrparen) | |
5448 { | |
5449 m = MOD.MODshared | MOD.MODconst; | |
5450 nextToken(); | |
5451 Lmod2: | |
5452 nextToken(); | |
5453 Lmod1: | |
5454 nextToken(); | |
5455 e = parseUnaryExp(); | |
5456 e = new CastExp(loc, e, m); | |
5457 } | |
5458 else | |
5459 { | |
5460 Type t = parseType(); // ( type ) | |
5461 check(TOK.TOKrparen); | |
5462 e = parseUnaryExp(); | |
5463 e = new CastExp(loc, e, t); | |
5464 } | |
5465 break; | |
5466 } | |
5467 | |
5468 case TOK.TOKlparen: | |
5469 { Token *tk; | |
5470 | |
5471 tk = peek(&token); | |
5472 version (CCASTSYNTAX) { | |
5473 // If cast | |
5474 if (isDeclaration(tk, 0, TOK.TOKrparen, &tk)) | |
5475 { | |
5476 tk = peek(tk); // skip over right parenthesis | |
5477 switch (tk.value) | |
5478 { | |
5479 case TOK.TOKnot: | |
5480 tk = peek(tk); | |
5481 if (tk.value == TOK.TOKis) // !is | |
5482 break; | |
5483 case TOK.TOKdot: | |
5484 case TOK.TOKplusplus: | |
5485 case TOK.TOKminusminus: | |
5486 case TOK.TOKdelete: | |
5487 case TOK.TOKnew: | |
5488 case TOK.TOKlparen: | |
5489 case TOK.TOKidentifier: | |
5490 case TOK.TOKthis: | |
5491 case TOK.TOKsuper: | |
5492 case TOK.TOKint32v: | |
5493 case TOK.TOKuns32v: | |
5494 case TOK.TOKint64v: | |
5495 case TOK.TOKuns64v: | |
5496 case TOK.TOKfloat32v: | |
5497 case TOK.TOKfloat64v: | |
5498 case TOK.TOKfloat80v: | |
5499 case TOK.TOKimaginary32v: | |
5500 case TOK.TOKimaginary64v: | |
5501 case TOK.TOKimaginary80v: | |
5502 case TOK.TOKnull: | |
5503 case TOK.TOKtrue: | |
5504 case TOK.TOKfalse: | |
5505 case TOK.TOKcharv: | |
5506 case TOK.TOKwcharv: | |
5507 case TOK.TOKdcharv: | |
5508 case TOK.TOKstring: | |
5509 static if (false) { | |
5510 case TOK.TOKtilde: | |
5511 case TOK.TOKand: | |
5512 case TOK.TOKmul: | |
5513 case TOK.TOKmin: | |
5514 case TOK.TOKadd: | |
5515 } | |
5516 case TOK.TOKfunction: | |
5517 case TOK.TOKdelegate: | |
5518 case TOK.TOKtypeof: | |
5519 version (DMDV2) { | |
5520 case TOK.TOKfile: | |
5521 case TOK.TOKline: | |
5522 } | |
5523 case TOK.TOKwchar: case TOK.TOKdchar: | |
5524 case TOK.TOKbit: case TOK.TOKbool: case TOK.TOKchar: | |
5525 case TOK.TOKint8: case TOK.TOKuns8: | |
5526 case TOK.TOKint16: case TOK.TOKuns16: | |
5527 case TOK.TOKint32: case TOK.TOKuns32: | |
5528 case TOK.TOKint64: case TOK.TOKuns64: | |
5529 case TOK.TOKfloat32: case TOK.TOKfloat64: case TOK.TOKfloat80: | |
5530 case TOK.TOKimaginary32: case TOK.TOKimaginary64: case TOK.TOKimaginary80: | |
5531 case TOK.TOKcomplex32: case TOK.TOKcomplex64: case TOK.TOKcomplex80: | |
5532 case TOK.TOKvoid: // (type)int.size | |
5533 { | |
5534 // (type) una_exp | |
5535 nextToken(); | |
5536 Type t = parseType(); | |
5537 check(TOK.TOKrparen); | |
5538 | |
5539 // if .identifier | |
5540 if (token.value == TOK.TOKdot) | |
5541 { | |
5542 nextToken(); | |
5543 if (token.value != TOK.TOKidentifier) | |
5544 { | |
5545 error("Identifier expected following (type)."); | |
5546 return null; | |
5547 } | |
5548 e = typeDotIdExp(loc, t, token.ident); | |
5549 nextToken(); | |
5550 e = parsePostExp(e); | |
5551 } | |
5552 else | |
5553 { | |
5554 e = parseUnaryExp(); | |
5555 e = new CastExp(loc, e, t); | |
5556 error("C style cast illegal, use %s", e.toChars()); | |
5557 } | |
5558 return e; | |
5559 } | |
5560 | |
5561 default: | |
5562 break; /// | |
5563 } | |
5564 } | |
5565 } | |
5566 e = parsePrimaryExp(); | |
5567 e = parsePostExp(e); | |
5568 break; | |
5569 } | |
5570 default: | |
5571 e = parsePrimaryExp(); | |
5572 e = parsePostExp(e); | |
5573 break; | |
5574 } | |
5575 assert(e); | |
5576 return e; | |
5577 } | |
5578 | |
5579 Expression parsePostExp(Expression e) | |
5580 { | |
5581 Loc loc; | |
5582 | |
5583 while (1) | |
5584 { | |
5585 loc = this.loc; | |
5586 switch (token.value) | |
5587 { | |
5588 case TOK.TOKdot: | |
5589 nextToken(); | |
5590 if (token.value == TOK.TOKidentifier) | |
5591 { Identifier id = token.ident; | |
5592 | |
5593 nextToken(); | |
5594 if (token.value == TOK.TOKnot && peekNext() != TOK.TOKis) | |
5595 { // identifier!(template-argument-list) | |
5596 TemplateInstance tempinst = new TemplateInstance(loc, id); | |
5597 nextToken(); | |
5598 if (token.value == TOK.TOKlparen) | |
5599 // ident!(template_arguments) | |
5600 tempinst.tiargs = parseTemplateArgumentList(); | |
5601 else | |
5602 // ident!template_argument | |
5603 tempinst.tiargs = parseTemplateArgument(); | |
5604 e = new DotTemplateInstanceExp(loc, e, tempinst); | |
5605 } | |
5606 else | |
5607 e = new DotIdExp(loc, e, id); | |
5608 continue; | |
5609 } | |
5610 else if (token.value == TOK.TOKnew) | |
5611 { | |
5612 e = parseNewExp(e); | |
5613 continue; | |
5614 } | |
5615 else | |
5616 error("identifier expected following '.', not '%s'", token.toChars()); | |
5617 break; | |
5618 | |
5619 case TOK.TOKplusplus: | |
5620 e = new PostExp(TOK.TOKplusplus, loc, e); | |
5621 break; | |
5622 | |
5623 case TOK.TOKminusminus: | |
5624 e = new PostExp(TOK.TOKminusminus, loc, e); | |
5625 break; | |
5626 | |
5627 case TOK.TOKlparen: | |
5628 e = new CallExp(loc, e, parseArguments()); | |
5629 continue; | |
5630 | |
5631 case TOK.TOKlbracket: | |
5632 { // array dereferences: | |
5633 // array[index] | |
5634 // array[] | |
5635 // array[lwr .. upr] | |
5636 Expression index; | |
5637 Expression upr; | |
5638 | |
5639 inBrackets++; | |
5640 nextToken(); | |
5641 if (token.value == TOK.TOKrbracket) | |
5642 { // array[] | |
5643 e = new SliceExp(loc, e, null, null); | |
5644 nextToken(); | |
5645 } | |
5646 else | |
5647 { | |
5648 index = parseAssignExp(); | |
5649 if (token.value == TOK.TOKslice) | |
5650 { // array[lwr .. upr] | |
5651 nextToken(); | |
5652 upr = parseAssignExp(); | |
5653 e = new SliceExp(loc, e, index, upr); | |
5654 } | |
5655 else | |
5656 { // array[index, i2, i3, i4, ...] | |
5657 Expressions arguments = new Expressions(); | |
5658 arguments.push(cast(void*)index); | |
5659 if (token.value == TOK.TOKcomma) | |
5660 { | |
5661 nextToken(); | |
5662 while (1) | |
5663 { Expression arg; | |
5664 | |
5665 arg = parseAssignExp(); | |
5666 arguments.push(cast(void*)arg); | |
5667 if (token.value == TOK.TOKrbracket) | |
5668 break; | |
5669 check(TOK.TOKcomma); | |
5670 } | |
5671 } | |
5672 e = new ArrayExp(loc, e, arguments); | |
5673 } | |
5674 check(TOK.TOKrbracket); | |
5675 inBrackets--; | |
5676 } | |
5677 continue; | |
5678 } | |
5679 | |
5680 default: | |
5681 return e; | |
5682 } | |
5683 nextToken(); | |
5684 } | |
5685 | |
5686 assert(false); | |
5687 } | |
5688 | |
5689 Expression parseMulExp() | |
5690 { | |
5691 Expression e; | |
5692 Expression e2; | |
5693 Loc loc = this.loc; | |
5694 | |
5695 e = parseUnaryExp(); | |
5696 while (1) | |
5697 { | |
5698 switch (token.value) | |
5699 { | |
5700 case TOK.TOKmul: nextToken(); e2 = parseUnaryExp(); e = new MulExp(loc,e,e2); continue; | |
5701 case TOK.TOKdiv: nextToken(); e2 = parseUnaryExp(); e = new DivExp(loc,e,e2); continue; | |
5702 case TOK.TOKmod: nextToken(); e2 = parseUnaryExp(); e = new ModExp(loc,e,e2); continue; | |
5703 | |
5704 default: | |
5705 break; | |
5706 } | |
5707 break; | |
5708 } | |
5709 return e; | |
5710 } | |
5711 | |
5712 Expression parseShiftExp() | |
5713 { | |
5714 Expression e; | |
5715 Expression e2; | |
5716 Loc loc = this.loc; | |
5717 | |
5718 e = parseAddExp(); | |
5719 while (1) | |
5720 { | |
5721 switch (token.value) | |
5722 { | |
5723 case TOK.TOKshl: nextToken(); e2 = parseAddExp(); e = new ShlExp(loc,e,e2); continue; | |
5724 case TOK.TOKshr: nextToken(); e2 = parseAddExp(); e = new ShrExp(loc,e,e2); continue; | |
5725 case TOK.TOKushr: nextToken(); e2 = parseAddExp(); e = new UshrExp(loc,e,e2); continue; | |
5726 | |
5727 default: | |
5728 break; | |
5729 } | |
5730 break; | |
5731 } | |
5732 return e; | |
5733 } | |
5734 | |
5735 Expression parseAddExp() | |
5736 { | |
5737 Expression e; | |
5738 Expression e2; | |
5739 Loc loc = this.loc; | |
5740 | |
5741 e = parseMulExp(); | |
5742 while (1) | |
5743 { | |
5744 switch (token.value) | |
5745 { | |
5746 case TOK.TOKadd: nextToken(); e2 = parseMulExp(); e = new AddExp(loc,e,e2); continue; | |
5747 case TOK.TOKmin: nextToken(); e2 = parseMulExp(); e = new MinExp(loc,e,e2); continue; | |
5748 case TOK.TOKtilde: nextToken(); e2 = parseMulExp(); e = new CatExp(loc,e,e2); continue; | |
5749 | |
5750 default: | |
5751 break; | |
5752 } | |
5753 break; | |
5754 } | |
5755 return e; | |
5756 } | |
5757 | |
5758 Expression parseRelExp() | |
5759 { | |
5760 assert(false); | |
5761 } | |
5762 | |
5763 Expression parseEqualExp() | |
5764 { | |
5765 assert(false); | |
5766 } | |
5767 | |
5768 Expression parseCmpExp() | |
5769 { | |
5770 Expression e; | |
5771 Expression e2; | |
5772 Token* t; | |
5773 Loc loc = this.loc; | |
5774 | |
5775 e = parseShiftExp(); | |
5776 TOK op = token.value; | |
5777 | |
5778 switch (op) | |
5779 { | |
5780 case TOK.TOKequal: | |
5781 case TOK.TOKnotequal: | |
5782 nextToken(); | |
5783 e2 = parseShiftExp(); | |
5784 e = new EqualExp(op, loc, e, e2); | |
5785 break; | |
5786 | |
5787 case TOK.TOKis: | |
5788 op = TOK.TOKidentity; | |
5789 goto L1; | |
5790 | |
5791 case TOK.TOKnot: | |
5792 // Attempt to identify '!is' | |
5793 t = peek(&token); | |
5794 if (t.value != TOK.TOKis) | |
5795 break; | |
5796 nextToken(); | |
5797 op = TOK.TOKnotidentity; | |
5798 goto L1; | |
5799 | |
5800 L1: | |
5801 nextToken(); | |
5802 e2 = parseShiftExp(); | |
5803 e = new IdentityExp(op, loc, e, e2); | |
5804 break; | |
5805 | |
5806 case TOK.TOKlt: | |
5807 case TOK.TOKle: | |
5808 case TOK.TOKgt: | |
5809 case TOK.TOKge: | |
5810 case TOK.TOKunord: | |
5811 case TOK.TOKlg: | |
5812 case TOK.TOKleg: | |
5813 case TOK.TOKule: | |
5814 case TOK.TOKul: | |
5815 case TOK.TOKuge: | |
5816 case TOK.TOKug: | |
5817 case TOK.TOKue: | |
5818 nextToken(); | |
5819 e2 = parseShiftExp(); | |
5820 e = new CmpExp(op, loc, e, e2); | |
5821 break; | |
5822 | |
5823 case TOK.TOKin: | |
5824 nextToken(); | |
5825 e2 = parseShiftExp(); | |
5826 e = new InExp(loc, e, e2); | |
5827 break; | |
5828 | |
5829 default: | |
5830 break; | |
5831 } | |
5832 return e; | |
5833 } | |
5834 | |
5835 Expression parseAndExp() | |
5836 { | |
5837 Expression e; | |
5838 Expression e2; | |
5839 Loc loc = this.loc; | |
5840 | |
5841 if (global.params.Dversion == 1) | |
5842 { | |
5843 e = parseEqualExp(); | |
5844 while (token.value == TOK.TOKand) | |
5845 { | |
5846 nextToken(); | |
5847 e2 = parseEqualExp(); | |
5848 e = new AndExp(loc,e,e2); | |
5849 loc = this.loc; | |
5850 } | |
5851 } | |
5852 else | |
5853 { | |
5854 e = parseCmpExp(); | |
5855 while (token.value == TOK.TOKand) | |
5856 { | |
5857 nextToken(); | |
5858 e2 = parseCmpExp(); | |
5859 e = new AndExp(loc,e,e2); | |
5860 loc = this.loc; | |
5861 } | |
5862 } | |
5863 return e; | |
5864 } | |
5865 | |
5866 Expression parseXorExp() | |
5867 { | |
5868 Expression e; | |
5869 Expression e2; | |
5870 Loc loc = this.loc; | |
5871 | |
5872 e = parseAndExp(); | |
5873 while (token.value == TOK.TOKxor) | |
5874 { | |
5875 nextToken(); | |
5876 e2 = parseAndExp(); | |
5877 e = new XorExp(loc, e, e2); | |
5878 } | |
5879 | |
5880 return e; | |
5881 } | |
5882 | |
5883 Expression parseOrExp() | |
5884 { | |
5885 Expression e; | |
5886 Expression e2; | |
5887 Loc loc = this.loc; | |
5888 | |
5889 e = parseXorExp(); | |
5890 while (token.value == TOK.TOKor) | |
5891 { | |
5892 nextToken(); | |
5893 e2 = parseXorExp(); | |
5894 e = new OrExp(loc, e, e2); | |
5895 } | |
5896 return e; | |
5897 } | |
5898 | |
5899 Expression parseAndAndExp() | |
5900 { | |
5901 Expression e; | |
5902 Expression e2; | |
5903 Loc loc = this.loc; | |
5904 | |
5905 e = parseOrExp(); | |
5906 while (token.value == TOK.TOKandand) | |
5907 { | |
5908 nextToken(); | |
5909 e2 = parseOrExp(); | |
5910 e = new AndAndExp(loc, e, e2); | |
5911 } | |
5912 return e; | |
5913 } | |
5914 | |
5915 Expression parseOrOrExp() | |
5916 { | |
5917 Expression e; | |
5918 Expression e2; | |
5919 Loc loc = this.loc; | |
5920 | |
5921 e = parseAndAndExp(); | |
5922 while (token.value == TOK.TOKoror) | |
5923 { | |
5924 nextToken(); | |
5925 e2 = parseAndAndExp(); | |
5926 e = new OrOrExp(loc, e, e2); | |
5927 } | |
5928 | |
5929 return e; | |
5930 } | |
5931 | |
5932 Expression parseCondExp() | |
5933 { | |
5934 Expression e; | |
5935 Expression e1; | |
5936 Expression e2; | |
5937 Loc loc = this.loc; | |
5938 | |
5939 e = parseOrOrExp(); | |
5940 if (token.value == TOK.TOKquestion) | |
5941 { | |
5942 nextToken(); | |
5943 e1 = parseExpression(); | |
5944 check(TOK.TOKcolon); | |
5945 e2 = parseCondExp(); | |
5946 e = new CondExp(loc, e, e1, e2); | |
5947 } | |
5948 return e; | |
5949 } | |
5950 | |
5951 Expression parseAssignExp() | |
5952 { | |
5953 Expression e; | |
5954 Expression e2; | |
5955 Loc loc; | |
5956 | |
5957 e = parseCondExp(); | |
5958 while (1) | |
5959 { | |
5960 loc = this.loc; | |
5961 switch (token.value) | |
5962 { | |
5963 case TOK.TOKassign: nextToken(); e2 = parseAssignExp(); e = new AssignExp(loc,e,e2); continue; | |
5964 case TOK.TOKaddass: nextToken(); e2 = parseAssignExp(); e = new AddAssignExp(loc,e,e2); continue; | |
5965 case TOK.TOKminass: nextToken(); e2 = parseAssignExp(); e = new MinAssignExp(loc,e,e2); continue; | |
5966 case TOK.TOKmulass: nextToken(); e2 = parseAssignExp(); e = new MulAssignExp(loc,e,e2); continue; | |
5967 case TOK.TOKdivass: nextToken(); e2 = parseAssignExp(); e = new DivAssignExp(loc,e,e2); continue; | |
5968 case TOK.TOKmodass: nextToken(); e2 = parseAssignExp(); e = new ModAssignExp(loc,e,e2); continue; | |
5969 case TOK.TOKandass: nextToken(); e2 = parseAssignExp(); e = new AndAssignExp(loc,e,e2); continue; | |
5970 case TOK.TOKorass: nextToken(); e2 = parseAssignExp(); e = new OrAssignExp(loc,e,e2); continue; | |
5971 case TOK.TOKxorass: nextToken(); e2 = parseAssignExp(); e = new XorAssignExp(loc,e,e2); continue; | |
5972 case TOK.TOKshlass: nextToken(); e2 = parseAssignExp(); e = new ShlAssignExp(loc,e,e2); continue; | |
5973 case TOK.TOKshrass: nextToken(); e2 = parseAssignExp(); e = new ShrAssignExp(loc,e,e2); continue; | |
5974 case TOK.TOKushrass: nextToken(); e2 = parseAssignExp(); e = new UshrAssignExp(loc,e,e2); continue; | |
5975 case TOK.TOKcatass: nextToken(); e2 = parseAssignExp(); e = new CatAssignExp(loc,e,e2); continue; | |
5976 | |
5977 default: | |
5978 break; | |
5979 } | |
5980 break; | |
5981 } | |
5982 | |
5983 return e; | |
5984 } | |
5985 | |
5986 /************************* | |
5987 * Collect argument list. | |
5988 * Assume current token is ',', '(' or '['. | |
5989 */ | |
5990 Expressions parseArguments() | |
5991 { | |
5992 // function call | |
5993 Expressions arguments = new Expressions(); | |
5994 Expression arg; | |
5995 TOK endtok; | |
5996 | |
5997 if (token.value == TOK.TOKlbracket) | |
5998 endtok = TOK.TOKrbracket; | |
5999 else | |
6000 endtok = TOK.TOKrparen; | |
6001 | |
6002 { | |
6003 nextToken(); | |
6004 if (token.value != endtok) | |
6005 { | |
6006 while (1) | |
6007 { | |
6008 arg = parseAssignExp(); | |
6009 arguments.push(cast(void*)arg); | |
6010 if (token.value == endtok) | |
6011 break; | |
6012 check(TOK.TOKcomma); | |
6013 } | |
6014 } | |
6015 check(endtok); | |
6016 } | |
6017 return arguments; | |
6018 } | |
6019 | |
6020 Expression parseNewExp(Expression thisexp) | |
6021 { | |
6022 Type t; | |
6023 Expressions newargs; | |
6024 Expressions arguments = null; | |
6025 Expression e; | |
6026 Loc loc = this.loc; | |
6027 | |
6028 nextToken(); | |
6029 newargs = null; | |
6030 if (token.value == TOKlparen) | |
6031 { | |
6032 newargs = parseArguments(); | |
6033 } | |
6034 | |
6035 // An anonymous nested class starts with "class" | |
6036 if (token.value == TOKclass) | |
6037 { | |
6038 nextToken(); | |
6039 if (token.value == TOKlparen) | |
6040 arguments = parseArguments(); | |
6041 | |
6042 BaseClasses baseclasses = null; | |
6043 if (token.value != TOKlcurly) | |
6044 baseclasses = parseBaseClasses(); | |
6045 | |
6046 Identifier id = null; | |
6047 ClassDeclaration cd = new ClassDeclaration(loc, id, baseclasses); | |
6048 | |
6049 if (token.value != TOKlcurly) | |
6050 { | |
6051 error("{ members } expected for anonymous class"); | |
6052 cd.members = null; | |
6053 } | |
6054 else | |
6055 { | |
6056 nextToken(); | |
6057 Array decl = parseDeclDefs(0); | |
6058 if (token.value != TOKrcurly) | |
6059 error("class member expected"); | |
6060 nextToken(); | |
6061 cd.members = decl; | |
6062 } | |
6063 | |
6064 e = new NewAnonClassExp(loc, thisexp, newargs, cd, arguments); | |
6065 | |
6066 return e; | |
6067 } | |
6068 | |
6069 t = parseBasicType(); | |
6070 t = parseBasicType2(t); | |
6071 if (t.ty == Taarray) | |
6072 { | |
6073 TypeAArray taa = cast(TypeAArray)t; | |
6074 Type index = taa.index; | |
6075 | |
6076 Expression e2 = index.toExpression(); | |
6077 if (e2) | |
6078 { | |
6079 arguments = new Expressions(); | |
6080 arguments.push(cast(void*)e2); | |
6081 t = new TypeDArray(taa.next); | |
6082 } | |
6083 else | |
6084 { | |
6085 error("need size of rightmost array, not type %s", index.toChars()); | |
6086 return new NullExp(loc); | |
6087 } | |
6088 } | |
6089 else if (t.ty == Tsarray) | |
6090 { | |
6091 TypeSArray tsa = cast(TypeSArray)t; | |
6092 Expression ee = tsa.dim; | |
6093 | |
6094 arguments = new Expressions(); | |
6095 arguments.push(cast(void*)ee); | |
6096 t = new TypeDArray(tsa.next); | |
6097 } | |
6098 else if (token.value == TOKlparen) | |
6099 { | |
6100 arguments = parseArguments(); | |
6101 } | |
6102 | |
6103 e = new NewExp(loc, thisexp, newargs, t, arguments); | |
6104 return e; | |
6105 } | |
6106 | |
6107 void addComment(Dsymbol s, ubyte* blockComment) | |
6108 { | |
6109 s.addComment(combineComments(blockComment, token.lineComment)); | |
6110 token.lineComment = null; | |
6111 } | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
6112 } |