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