Mercurial > projects > ldc
annotate dmd/parse.c @ 142:a123dca8349b trunk
[svn r146] fixed some potential problems with mismatch in order of interfaces in class data layout
author | lindquist |
---|---|
date | Wed, 23 Jan 2008 15:48:19 +0100 |
parents | 0ab29b838084 |
children | 5acec6b2eef8 |
rev | line source |
---|---|
1 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2007 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <assert.h> | |
13 | |
14 #include "mem.h" | |
15 #include "lexer.h" | |
16 #include "parse.h" | |
17 #include "init.h" | |
18 #include "attrib.h" | |
19 #include "cond.h" | |
20 #include "mtype.h" | |
21 #include "template.h" | |
22 #include "staticassert.h" | |
23 #include "expression.h" | |
24 #include "statement.h" | |
25 #include "module.h" | |
26 #include "dsymbol.h" | |
27 #include "import.h" | |
28 #include "declaration.h" | |
29 #include "aggregate.h" | |
30 #include "enum.h" | |
31 #include "id.h" | |
32 #include "version.h" | |
33 | |
34 // How multiple declarations are parsed. | |
35 // If 1, treat as C. | |
36 // If 0, treat: | |
37 // int *p, i; | |
38 // as: | |
39 // int* p; | |
40 // int* i; | |
41 #define CDECLSYNTAX 0 | |
42 | |
43 // Support C cast syntax: | |
44 // (type)(expression) | |
45 #define CCASTSYNTAX 1 | |
46 | |
47 // Support C array declarations, such as | |
48 // int a[3][4]; | |
49 #define CARRAYDECL 1 | |
50 | |
51 // Support left-to-right array declarations | |
52 #define LTORARRAYDECL 1 | |
53 | |
54 | |
55 Parser::Parser(Module *module, unsigned char *base, unsigned length, int doDocComment) | |
56 : Lexer(module, base, 0, length, doDocComment, 0) | |
57 { | |
58 //printf("Parser::Parser()\n"); | |
59 md = NULL; | |
60 linkage = LINKd; | |
61 endloc = 0; | |
62 inBrackets = 0; | |
63 //nextToken(); // start up the scanner | |
64 } | |
65 | |
66 Array *Parser::parseModule() | |
67 { | |
68 Array *decldefs; | |
69 | |
70 // ModuleDeclation leads off | |
71 if (token.value == TOKmodule) | |
72 { | |
73 unsigned char *comment = token.blockComment; | |
74 | |
75 nextToken(); | |
76 if (token.value != TOKidentifier) | |
77 { error("Identifier expected following module"); | |
78 goto Lerr; | |
79 } | |
80 else | |
81 { | |
82 Array *a = NULL; | |
83 Identifier *id; | |
84 | |
85 id = token.ident; | |
86 while (nextToken() == TOKdot) | |
87 { | |
88 if (!a) | |
89 a = new Array(); | |
90 a->push(id); | |
91 nextToken(); | |
92 if (token.value != TOKidentifier) | |
93 { error("Identifier expected following package"); | |
94 goto Lerr; | |
95 } | |
96 id = token.ident; | |
97 } | |
98 | |
99 md = new ModuleDeclaration(a, id); | |
100 | |
101 if (token.value != TOKsemicolon) | |
102 error("';' expected following module declaration instead of %s", token.toChars()); | |
103 nextToken(); | |
104 addComment(mod, comment); | |
105 } | |
106 } | |
107 | |
108 decldefs = parseDeclDefs(0); | |
109 if (token.value != TOKeof) | |
110 { error("unrecognized declaration"); | |
111 goto Lerr; | |
112 } | |
113 return decldefs; | |
114 | |
115 Lerr: | |
116 while (token.value != TOKsemicolon && token.value != TOKeof) | |
117 nextToken(); | |
118 nextToken(); | |
119 return new Array(); | |
120 } | |
121 | |
122 Array *Parser::parseDeclDefs(int once) | |
123 { Dsymbol *s; | |
124 Array *decldefs; | |
125 Array *a; | |
126 Array *aelse; | |
127 enum PROT prot; | |
128 unsigned stc; | |
129 Condition *condition; | |
130 unsigned char *comment; | |
131 | |
132 //printf("Parser::parseDeclDefs()\n"); | |
133 decldefs = new Array(); | |
134 do | |
135 { | |
136 comment = token.blockComment; | |
137 switch (token.value) | |
138 { | |
139 case TOKenum: | |
140 s = parseEnum(); | |
141 break; | |
142 | |
143 case TOKstruct: | |
144 case TOKunion: | |
145 case TOKclass: | |
146 case TOKinterface: | |
147 s = parseAggregate(); | |
148 break; | |
149 | |
150 case TOKimport: | |
151 s = parseImport(decldefs, 0); | |
152 break; | |
153 | |
154 case TOKtemplate: | |
155 s = (Dsymbol *)parseTemplateDeclaration(); | |
156 break; | |
157 | |
158 case TOKmixin: | |
159 { Loc loc = this->loc; | |
160 if (peek(&token)->value == TOKlparen) | |
161 { // mixin(string) | |
162 nextToken(); | |
163 check(TOKlparen, "mixin"); | |
164 Expression *e = parseAssignExp(); | |
165 check(TOKrparen); | |
166 check(TOKsemicolon); | |
167 s = new CompileDeclaration(loc, e); | |
168 break; | |
169 } | |
170 s = parseMixin(); | |
171 break; | |
172 } | |
173 | |
174 CASE_BASIC_TYPES: | |
175 case TOKalias: | |
176 case TOKtypedef: | |
177 case TOKidentifier: | |
178 case TOKtypeof: | |
179 case TOKdot: | |
180 Ldeclaration: | |
181 a = parseDeclarations(); | |
182 decldefs->append(a); | |
183 continue; | |
184 | |
185 case TOKthis: | |
186 s = parseCtor(); | |
187 break; | |
188 | |
189 case TOKtilde: | |
190 s = parseDtor(); | |
191 break; | |
192 | |
193 case TOKinvariant: | |
194 #if 1 | |
195 s = parseInvariant(); | |
196 #else | |
197 if (peek(&token)->value == TOKlcurly) | |
198 s = parseInvariant(); | |
199 else | |
200 { | |
201 stc = STCinvariant; | |
202 goto Lstc; | |
203 } | |
204 #endif | |
205 break; | |
206 | |
207 case TOKunittest: | |
208 s = parseUnitTest(); | |
209 break; | |
210 | |
211 case TOKnew: | |
212 s = parseNew(); | |
213 break; | |
214 | |
215 case TOKdelete: | |
216 s = parseDelete(); | |
217 break; | |
218 | |
219 case TOKeof: | |
220 case TOKrcurly: | |
221 return decldefs; | |
222 | |
223 case TOKstatic: | |
224 nextToken(); | |
225 if (token.value == TOKthis) | |
226 s = parseStaticCtor(); | |
227 else if (token.value == TOKtilde) | |
228 s = parseStaticDtor(); | |
229 else if (token.value == TOKassert) | |
230 s = parseStaticAssert(); | |
231 else if (token.value == TOKif) | |
232 { condition = parseStaticIfCondition(); | |
233 a = parseBlock(); | |
234 aelse = NULL; | |
235 if (token.value == TOKelse) | |
236 { nextToken(); | |
237 aelse = parseBlock(); | |
238 } | |
239 s = new StaticIfDeclaration(condition, a, aelse); | |
240 break; | |
241 } | |
242 else if (token.value == TOKimport) | |
243 { | |
244 s = parseImport(decldefs, 1); | |
245 } | |
246 else | |
247 { stc = STCstatic; | |
248 goto Lstc2; | |
249 } | |
250 break; | |
251 | |
252 case TOKconst: stc = STCconst; goto Lstc; | |
253 case TOKfinal: stc = STCfinal; goto Lstc; | |
254 case TOKauto: stc = STCauto; goto Lstc; | |
255 case TOKscope: stc = STCscope; goto Lstc; | |
256 case TOKoverride: stc = STCoverride; goto Lstc; | |
257 case TOKabstract: stc = STCabstract; goto Lstc; | |
258 case TOKsynchronized: stc = STCsynchronized; goto Lstc; | |
259 case TOKdeprecated: stc = STCdeprecated; goto Lstc; | |
260 | |
261 Lstc: | |
262 nextToken(); | |
263 Lstc2: | |
264 switch (token.value) | |
265 { | |
266 case TOKconst: stc |= STCconst; goto Lstc; | |
267 case TOKfinal: stc |= STCfinal; goto Lstc; | |
268 case TOKauto: stc |= STCauto; goto Lstc; | |
269 case TOKscope: stc |= STCscope; goto Lstc; | |
270 case TOKoverride: stc |= STCoverride; goto Lstc; | |
271 case TOKabstract: stc |= STCabstract; goto Lstc; | |
272 case TOKsynchronized: stc |= STCsynchronized; goto Lstc; | |
273 case TOKdeprecated: stc |= STCdeprecated; goto Lstc; | |
274 //case TOKinvariant: stc |= STCinvariant; goto Lstc; | |
275 default: | |
276 break; | |
277 } | |
278 | |
279 /* Look for auto initializers: | |
280 * storage_class identifier = initializer; | |
281 */ | |
282 if (token.value == TOKidentifier && | |
283 peek(&token)->value == TOKassign) | |
284 { | |
285 while (1) | |
286 { | |
287 Identifier *ident = token.ident; | |
288 nextToken(); | |
289 nextToken(); | |
290 Initializer *init = parseInitializer(); | |
291 VarDeclaration *v = new VarDeclaration(loc, NULL, ident, init); | |
292 v->storage_class = stc; | |
293 s = v; | |
294 if (token.value == TOKsemicolon) | |
295 { | |
296 nextToken(); | |
297 } | |
298 else if (token.value == TOKcomma) | |
299 { | |
300 nextToken(); | |
301 if (token.value == TOKidentifier && | |
302 peek(&token)->value == TOKassign) | |
303 { | |
304 decldefs->push(s); | |
305 addComment(s, comment); | |
306 continue; | |
307 } | |
308 else | |
309 error("Identifier expected following comma"); | |
310 } | |
311 else | |
312 error("semicolon expected following auto declaration, not '%s'", token.toChars()); | |
313 break; | |
314 } | |
315 } | |
316 else | |
317 { a = parseBlock(); | |
318 s = new StorageClassDeclaration(stc, a); | |
319 } | |
320 break; | |
321 | |
322 case TOKextern: | |
323 if (peek(&token)->value != TOKlparen) | |
324 { stc = STCextern; | |
325 goto Lstc; | |
326 } | |
327 { | |
328 enum LINK linksave = linkage; | |
329 linkage = parseLinkage(); | |
330 a = parseBlock(); | |
331 s = new LinkDeclaration(linkage, a); | |
332 linkage = linksave; | |
333 break; | |
334 } | |
335 case TOKprivate: prot = PROTprivate; goto Lprot; | |
336 case TOKpackage: prot = PROTpackage; goto Lprot; | |
337 case TOKprotected: prot = PROTprotected; goto Lprot; | |
338 case TOKpublic: prot = PROTpublic; goto Lprot; | |
339 case TOKexport: prot = PROTexport; goto Lprot; | |
340 | |
341 Lprot: | |
342 nextToken(); | |
343 a = parseBlock(); | |
344 s = new ProtDeclaration(prot, a); | |
345 break; | |
346 | |
347 case TOKalign: | |
348 { unsigned n; | |
349 | |
350 s = NULL; | |
351 nextToken(); | |
352 if (token.value == TOKlparen) | |
353 { | |
354 nextToken(); | |
355 if (token.value == TOKint32v) | |
356 n = (unsigned)token.uns64value; | |
357 else | |
358 { error("integer expected, not %s", token.toChars()); | |
359 n = 1; | |
360 } | |
361 nextToken(); | |
362 check(TOKrparen); | |
363 } | |
364 else | |
365 n = global.structalign; // default | |
366 | |
367 a = parseBlock(); | |
368 s = new AlignDeclaration(n, a); | |
369 break; | |
370 } | |
371 | |
372 case TOKpragma: | |
373 { Identifier *ident; | |
374 Expressions *args = NULL; | |
375 | |
376 nextToken(); | |
377 check(TOKlparen); | |
378 if (token.value != TOKidentifier) | |
379 { error("pragma(identifier expected"); | |
380 goto Lerror; | |
381 } | |
382 ident = token.ident; | |
383 nextToken(); | |
384 if (token.value == TOKcomma) | |
385 args = parseArguments(); // pragma(identifier, args...) | |
386 else | |
387 check(TOKrparen); // pragma(identifier) | |
388 | |
389 if (token.value == TOKsemicolon) | |
390 a = NULL; | |
391 else | |
392 a = parseBlock(); | |
393 s = new PragmaDeclaration(loc, ident, args, a); | |
394 break; | |
395 } | |
396 | |
397 case TOKdebug: | |
398 nextToken(); | |
399 if (token.value == TOKassign) | |
400 { | |
401 nextToken(); | |
402 if (token.value == TOKidentifier) | |
403 s = new DebugSymbol(loc, token.ident); | |
404 else if (token.value == TOKint32v) | |
405 s = new DebugSymbol(loc, (unsigned)token.uns64value); | |
406 else | |
407 { error("identifier or integer expected, not %s", token.toChars()); | |
408 s = NULL; | |
409 } | |
410 nextToken(); | |
411 if (token.value != TOKsemicolon) | |
412 error("semicolon expected"); | |
413 nextToken(); | |
414 break; | |
415 } | |
416 | |
417 condition = parseDebugCondition(); | |
418 goto Lcondition; | |
419 | |
420 case TOKversion: | |
421 nextToken(); | |
422 if (token.value == TOKassign) | |
423 { | |
424 nextToken(); | |
425 if (token.value == TOKidentifier) | |
426 s = new VersionSymbol(loc, token.ident); | |
427 else if (token.value == TOKint32v) | |
428 s = new VersionSymbol(loc, (unsigned)token.uns64value); | |
429 else | |
430 { error("identifier or integer expected, not %s", token.toChars()); | |
431 s = NULL; | |
432 } | |
433 nextToken(); | |
434 if (token.value != TOKsemicolon) | |
435 error("semicolon expected"); | |
436 nextToken(); | |
437 break; | |
438 } | |
439 condition = parseVersionCondition(); | |
440 goto Lcondition; | |
441 | |
442 Lcondition: | |
443 a = parseBlock(); | |
444 aelse = NULL; | |
445 if (token.value == TOKelse) | |
446 { nextToken(); | |
447 aelse = parseBlock(); | |
448 } | |
449 s = new ConditionalDeclaration(condition, a, aelse); | |
450 break; | |
451 | |
452 case TOKsemicolon: // empty declaration | |
453 nextToken(); | |
454 continue; | |
455 | |
456 default: | |
457 error("Declaration expected, not '%s'",token.toChars()); | |
458 Lerror: | |
459 while (token.value != TOKsemicolon && token.value != TOKeof) | |
460 nextToken(); | |
461 nextToken(); | |
462 s = NULL; | |
463 continue; | |
464 } | |
465 if (s) | |
466 { decldefs->push(s); | |
467 addComment(s, comment); | |
468 } | |
469 } while (!once); | |
470 return decldefs; | |
471 } | |
472 | |
473 | |
474 /******************************************** | |
475 * Parse declarations after an align, protection, or extern decl. | |
476 */ | |
477 | |
478 Array *Parser::parseBlock() | |
479 { | |
480 Array *a = NULL; | |
481 Dsymbol *s; | |
482 | |
483 //printf("parseBlock()\n"); | |
484 switch (token.value) | |
485 { | |
486 case TOKsemicolon: | |
487 error("declaration expected following attribute, not ';'"); | |
488 nextToken(); | |
489 break; | |
490 | |
491 case TOKlcurly: | |
492 nextToken(); | |
493 a = parseDeclDefs(0); | |
494 if (token.value != TOKrcurly) | |
495 { /* { */ | |
496 error("matching '}' expected, not %s", token.toChars()); | |
497 } | |
498 else | |
499 nextToken(); | |
500 break; | |
501 | |
502 case TOKcolon: | |
503 nextToken(); | |
504 #if 0 | |
505 a = NULL; | |
506 #else | |
507 a = parseDeclDefs(0); // grab declarations up to closing curly bracket | |
508 #endif | |
509 break; | |
510 | |
511 default: | |
512 a = parseDeclDefs(1); | |
513 break; | |
514 } | |
515 return a; | |
516 } | |
517 | |
518 /********************************** | |
519 * Parse a static assertion. | |
520 */ | |
521 | |
522 StaticAssert *Parser::parseStaticAssert() | |
523 { | |
524 Loc loc = this->loc; | |
525 Expression *exp; | |
526 Expression *msg = NULL; | |
527 | |
528 //printf("parseStaticAssert()\n"); | |
529 nextToken(); | |
530 check(TOKlparen); | |
531 exp = parseAssignExp(); | |
532 if (token.value == TOKcomma) | |
533 { nextToken(); | |
534 msg = parseAssignExp(); | |
535 } | |
536 check(TOKrparen); | |
537 check(TOKsemicolon); | |
538 return new StaticAssert(loc, exp, msg); | |
539 } | |
540 | |
541 | |
542 /*********************************** | |
543 * Parse extern (linkage) | |
544 * The parser is on the 'extern' token. | |
545 */ | |
546 | |
547 enum LINK Parser::parseLinkage() | |
548 { | |
549 enum LINK link = LINKdefault; | |
550 nextToken(); | |
551 assert(token.value == TOKlparen); | |
552 nextToken(); | |
553 if (token.value == TOKidentifier) | |
554 { Identifier *id = token.ident; | |
555 | |
556 nextToken(); | |
557 if (id == Id::Windows) | |
558 link = LINKwindows; | |
559 else if (id == Id::Pascal) | |
560 link = LINKpascal; | |
561 else if (id == Id::D) | |
562 link = LINKd; | |
563 else if (id == Id::C) | |
564 { | |
565 link = LINKc; | |
566 if (token.value == TOKplusplus) | |
567 { link = LINKcpp; | |
568 nextToken(); | |
569 } | |
570 } | |
571 else if (id == Id::System) | |
572 { | |
573 #if _WIN32 | |
574 link = LINKwindows; | |
575 #else | |
576 link = LINKc; | |
577 #endif | |
578 } | |
579 else | |
580 { | |
581 error("valid linkage identifiers are D, C, C++, Pascal, Windows, System"); | |
582 link = LINKd; | |
583 } | |
584 } | |
585 else | |
586 { | |
587 link = LINKd; // default | |
588 } | |
589 check(TOKrparen); | |
590 return link; | |
591 } | |
592 | |
593 /************************************** | |
594 * Parse a debug conditional | |
595 */ | |
596 | |
597 Condition *Parser::parseDebugCondition() | |
598 { | |
599 Condition *c; | |
600 | |
601 if (token.value == TOKlparen) | |
602 { | |
603 nextToken(); | |
604 unsigned level = 1; | |
605 Identifier *id = NULL; | |
606 | |
607 if (token.value == TOKidentifier) | |
608 id = token.ident; | |
609 else if (token.value == TOKint32v) | |
610 level = (unsigned)token.uns64value; | |
611 else | |
612 error("identifier or integer expected, not %s", token.toChars()); | |
613 nextToken(); | |
614 check(TOKrparen); | |
615 c = new DebugCondition(mod, level, id); | |
616 } | |
617 else | |
618 c = new DebugCondition(mod, 1, NULL); | |
619 return c; | |
620 | |
621 } | |
622 | |
623 /************************************** | |
624 * Parse a version conditional | |
625 */ | |
626 | |
627 Condition *Parser::parseVersionCondition() | |
628 { | |
629 Condition *c; | |
630 unsigned level = 1; | |
631 Identifier *id = NULL; | |
632 | |
633 if (token.value == TOKlparen) | |
634 { | |
635 nextToken(); | |
636 if (token.value == TOKidentifier) | |
637 id = token.ident; | |
638 else if (token.value == TOKint32v) | |
639 level = (unsigned)token.uns64value; | |
640 else | |
641 error("identifier or integer expected, not %s", token.toChars()); | |
642 nextToken(); | |
643 check(TOKrparen); | |
644 | |
645 } | |
646 else | |
647 error("(condition) expected following version"); | |
648 c = new VersionCondition(mod, level, id); | |
649 return c; | |
650 | |
651 } | |
652 | |
653 /*********************************************** | |
654 * static if (expression) | |
655 * body | |
656 * else | |
657 * body | |
658 */ | |
659 | |
660 Condition *Parser::parseStaticIfCondition() | |
661 { Expression *exp; | |
662 Condition *condition; | |
663 Array *aif; | |
664 Array *aelse; | |
665 Loc loc = this->loc; | |
666 | |
667 nextToken(); | |
668 if (token.value == TOKlparen) | |
669 { | |
670 nextToken(); | |
671 exp = parseAssignExp(); | |
672 check(TOKrparen); | |
673 } | |
674 else | |
675 { error("(expression) expected following static if"); | |
676 exp = NULL; | |
677 } | |
678 condition = new StaticIfCondition(loc, exp); | |
679 return condition; | |
680 } | |
681 | |
682 | |
683 /***************************************** | |
684 * Parse a constructor definition: | |
685 * this(arguments) { body } | |
686 * Current token is 'this'. | |
687 */ | |
688 | |
689 CtorDeclaration *Parser::parseCtor() | |
690 { | |
691 CtorDeclaration *f; | |
692 Arguments *arguments; | |
693 int varargs; | |
694 Loc loc = this->loc; | |
695 | |
696 nextToken(); | |
697 arguments = parseParameters(&varargs); | |
698 f = new CtorDeclaration(loc, 0, arguments, varargs); | |
699 parseContracts(f); | |
700 return f; | |
701 } | |
702 | |
703 /***************************************** | |
704 * Parse a destructor definition: | |
705 * ~this() { body } | |
706 * Current token is '~'. | |
707 */ | |
708 | |
709 DtorDeclaration *Parser::parseDtor() | |
710 { | |
711 DtorDeclaration *f; | |
712 Loc loc = this->loc; | |
713 | |
714 nextToken(); | |
715 check(TOKthis); | |
716 check(TOKlparen); | |
717 check(TOKrparen); | |
718 | |
719 f = new DtorDeclaration(loc, 0); | |
720 parseContracts(f); | |
721 return f; | |
722 } | |
723 | |
724 /***************************************** | |
725 * Parse a static constructor definition: | |
726 * static this() { body } | |
727 * Current token is 'this'. | |
728 */ | |
729 | |
730 StaticCtorDeclaration *Parser::parseStaticCtor() | |
731 { | |
732 StaticCtorDeclaration *f; | |
733 Loc loc = this->loc; | |
734 | |
735 nextToken(); | |
736 check(TOKlparen); | |
737 check(TOKrparen); | |
738 | |
739 f = new StaticCtorDeclaration(loc, 0); | |
740 parseContracts(f); | |
741 return f; | |
742 } | |
743 | |
744 /***************************************** | |
745 * Parse a static destructor definition: | |
746 * static ~this() { body } | |
747 * Current token is '~'. | |
748 */ | |
749 | |
750 StaticDtorDeclaration *Parser::parseStaticDtor() | |
751 { | |
752 StaticDtorDeclaration *f; | |
753 Loc loc = this->loc; | |
754 | |
755 nextToken(); | |
756 check(TOKthis); | |
757 check(TOKlparen); | |
758 check(TOKrparen); | |
759 | |
760 f = new StaticDtorDeclaration(loc, 0); | |
761 parseContracts(f); | |
762 return f; | |
763 } | |
764 | |
765 /***************************************** | |
766 * Parse an invariant definition: | |
767 * invariant { body } | |
768 * Current token is 'invariant'. | |
769 */ | |
770 | |
771 InvariantDeclaration *Parser::parseInvariant() | |
772 { | |
773 InvariantDeclaration *f; | |
774 Loc loc = this->loc; | |
775 | |
776 nextToken(); | |
777 if (token.value == TOKlparen) // optional () | |
778 { | |
779 nextToken(); | |
780 check(TOKrparen); | |
781 } | |
782 | |
783 f = new InvariantDeclaration(loc, 0); | |
784 f->fbody = parseStatement(PScurly); | |
785 return f; | |
786 } | |
787 | |
788 /***************************************** | |
789 * Parse a unittest definition: | |
790 * unittest { body } | |
791 * Current token is 'unittest'. | |
792 */ | |
793 | |
794 UnitTestDeclaration *Parser::parseUnitTest() | |
795 { | |
796 UnitTestDeclaration *f; | |
797 Statement *body; | |
798 Loc loc = this->loc; | |
799 | |
800 nextToken(); | |
801 | |
802 body = parseStatement(PScurly); | |
803 | |
804 f = new UnitTestDeclaration(loc, this->loc); | |
805 f->fbody = body; | |
806 return f; | |
807 } | |
808 | |
809 /***************************************** | |
810 * Parse a new definition: | |
811 * new(arguments) { body } | |
812 * Current token is 'new'. | |
813 */ | |
814 | |
815 NewDeclaration *Parser::parseNew() | |
816 { | |
817 NewDeclaration *f; | |
818 Arguments *arguments; | |
819 int varargs; | |
820 Loc loc = this->loc; | |
821 | |
822 nextToken(); | |
823 arguments = parseParameters(&varargs); | |
824 f = new NewDeclaration(loc, 0, arguments, varargs); | |
825 parseContracts(f); | |
826 return f; | |
827 } | |
828 | |
829 /***************************************** | |
830 * Parse a delete definition: | |
831 * delete(arguments) { body } | |
832 * Current token is 'delete'. | |
833 */ | |
834 | |
835 DeleteDeclaration *Parser::parseDelete() | |
836 { | |
837 DeleteDeclaration *f; | |
838 Arguments *arguments; | |
839 int varargs; | |
840 Loc loc = this->loc; | |
841 | |
842 nextToken(); | |
843 arguments = parseParameters(&varargs); | |
844 if (varargs) | |
845 error("... not allowed in delete function parameter list"); | |
846 f = new DeleteDeclaration(loc, 0, arguments); | |
847 parseContracts(f); | |
848 return f; | |
849 } | |
850 | |
851 /********************************************** | |
852 * Parse parameter list. | |
853 */ | |
854 | |
855 Arguments *Parser::parseParameters(int *pvarargs) | |
856 { | |
857 Arguments *arguments = new Arguments(); | |
858 int varargs = 0; | |
859 int hasdefault = 0; | |
860 | |
861 check(TOKlparen); | |
862 while (1) | |
863 { Type *tb; | |
864 Identifier *ai; | |
865 Type *at; | |
866 Argument *a; | |
867 unsigned storageClass; | |
868 Expression *ae; | |
869 | |
870 ai = NULL; | |
871 storageClass = STCin; // parameter is "in" by default | |
872 switch (token.value) | |
873 { | |
874 case TOKrparen: | |
875 break; | |
876 | |
877 case TOKdotdotdot: | |
878 varargs = 1; | |
879 nextToken(); | |
880 break; | |
881 | |
882 case TOKin: | |
883 storageClass = STCin; | |
884 nextToken(); | |
885 goto L1; | |
886 | |
887 case TOKout: | |
888 storageClass = STCout; | |
889 nextToken(); | |
890 goto L1; | |
891 | |
892 case TOKinout: | |
893 case TOKref: | |
894 storageClass = STCref; | |
895 nextToken(); | |
896 goto L1; | |
897 | |
898 case TOKlazy: | |
899 storageClass = STClazy; | |
900 nextToken(); | |
901 goto L1; | |
902 | |
903 default: | |
904 L1: | |
905 tb = parseBasicType(); | |
906 at = parseDeclarator(tb, &ai); | |
907 ae = NULL; | |
908 if (token.value == TOKassign) // = defaultArg | |
909 { nextToken(); | |
910 ae = parseAssignExp(); | |
911 hasdefault = 1; | |
912 } | |
913 else | |
914 { if (hasdefault) | |
915 error("default argument expected for %s", | |
916 ai ? ai->toChars() : at->toChars()); | |
917 } | |
918 if (token.value == TOKdotdotdot) | |
919 { /* This is: | |
920 * at ai ... | |
921 */ | |
922 | |
923 if (storageClass & (STCout | STCref)) | |
924 error("variadic argument cannot be out or ref"); | |
925 varargs = 2; | |
926 a = new Argument(storageClass, at, ai, ae); | |
927 arguments->push(a); | |
928 nextToken(); | |
929 break; | |
930 } | |
931 a = new Argument(storageClass, at, ai, ae); | |
932 arguments->push(a); | |
933 if (token.value == TOKcomma) | |
934 { nextToken(); | |
935 continue; | |
936 } | |
937 break; | |
938 } | |
939 break; | |
940 } | |
941 check(TOKrparen); | |
942 *pvarargs = varargs; | |
943 return arguments; | |
944 } | |
945 | |
946 | |
947 /************************************* | |
948 */ | |
949 | |
950 EnumDeclaration *Parser::parseEnum() | |
951 { EnumDeclaration *e; | |
952 Identifier *id; | |
953 Type *t; | |
954 Loc loc = this->loc; | |
955 | |
956 //printf("Parser::parseEnum()\n"); | |
957 nextToken(); | |
958 if (token.value == TOKidentifier) | |
959 { id = token.ident; | |
960 nextToken(); | |
961 } | |
962 else | |
963 id = NULL; | |
964 | |
965 if (token.value == TOKcolon) | |
966 { | |
967 nextToken(); | |
968 t = parseBasicType(); | |
969 } | |
970 else | |
971 t = NULL; | |
972 | |
973 e = new EnumDeclaration(loc, id, t); | |
974 if (token.value == TOKsemicolon && id) | |
975 nextToken(); | |
976 else if (token.value == TOKlcurly) | |
977 { | |
978 //printf("enum definition\n"); | |
979 e->members = new Array(); | |
980 nextToken(); | |
981 unsigned char *comment = token.blockComment; | |
982 while (token.value != TOKrcurly) | |
983 { | |
984 if (token.value == TOKidentifier) | |
985 { EnumMember *em; | |
986 Expression *value; | |
987 Identifier *ident; | |
988 | |
989 loc = this->loc; | |
990 ident = token.ident; | |
991 value = NULL; | |
992 nextToken(); | |
993 if (token.value == TOKassign) | |
994 { | |
995 nextToken(); | |
996 value = parseAssignExp(); | |
997 } | |
998 em = new EnumMember(loc, ident, value); | |
999 e->members->push(em); | |
1000 if (token.value == TOKrcurly) | |
1001 ; | |
1002 else | |
1003 { addComment(em, comment); | |
1004 comment = NULL; | |
1005 check(TOKcomma); | |
1006 } | |
1007 addComment(em, comment); | |
1008 comment = token.blockComment; | |
1009 } | |
1010 else | |
1011 { error("enum member expected"); | |
1012 nextToken(); | |
1013 } | |
1014 } | |
1015 nextToken(); | |
1016 } | |
1017 else | |
1018 error("enum declaration is invalid"); | |
1019 | |
1020 return e; | |
1021 } | |
1022 | |
1023 Dsymbol *Parser::parseAggregate() | |
1024 { AggregateDeclaration *a = NULL; | |
1025 int anon = 0; | |
1026 enum TOK tok; | |
1027 Identifier *id; | |
1028 TemplateParameters *tpl = NULL; | |
1029 | |
1030 //printf("Parser::parseAggregate()\n"); | |
1031 tok = token.value; | |
1032 nextToken(); | |
1033 if (token.value != TOKidentifier) | |
1034 { id = NULL; | |
1035 } | |
1036 else | |
1037 { id = token.ident; | |
1038 nextToken(); | |
1039 | |
1040 if (token.value == TOKlparen) | |
1041 { // Class template declaration. | |
1042 | |
1043 // Gather template parameter list | |
1044 tpl = parseTemplateParameterList(); | |
1045 } | |
1046 } | |
1047 | |
1048 Loc loc = this->loc; | |
1049 switch (tok) | |
1050 { case TOKclass: | |
1051 case TOKinterface: | |
1052 { | |
1053 if (!id) | |
1054 error("anonymous classes not allowed"); | |
1055 | |
1056 // Collect base class(es) | |
1057 BaseClasses *baseclasses = NULL; | |
1058 if (token.value == TOKcolon) | |
1059 { | |
1060 nextToken(); | |
1061 baseclasses = parseBaseClasses(); | |
1062 | |
1063 if (token.value != TOKlcurly) | |
1064 error("members expected"); | |
1065 } | |
1066 | |
1067 if (tok == TOKclass) | |
1068 a = new ClassDeclaration(loc, id, baseclasses); | |
1069 else | |
1070 a = new InterfaceDeclaration(loc, id, baseclasses); | |
1071 break; | |
1072 } | |
1073 | |
1074 case TOKstruct: | |
1075 if (id) | |
1076 a = new StructDeclaration(loc, id); | |
1077 else | |
1078 anon = 1; | |
1079 break; | |
1080 | |
1081 case TOKunion: | |
1082 if (id) | |
1083 a = new UnionDeclaration(loc, id); | |
1084 else | |
1085 anon = 2; | |
1086 break; | |
1087 | |
1088 default: | |
1089 assert(0); | |
1090 break; | |
1091 } | |
1092 if (a && token.value == TOKsemicolon) | |
1093 { nextToken(); | |
1094 } | |
1095 else if (token.value == TOKlcurly) | |
1096 { | |
1097 //printf("aggregate definition\n"); | |
1098 nextToken(); | |
1099 Array *decl = parseDeclDefs(0); | |
1100 if (token.value != TOKrcurly) | |
1101 error("} expected following member declarations in aggregate"); | |
1102 nextToken(); | |
1103 if (anon) | |
1104 { | |
1105 /* Anonymous structs/unions are more like attributes. | |
1106 */ | |
1107 return new AnonDeclaration(loc, anon - 1, decl); | |
1108 } | |
1109 else | |
1110 a->members = decl; | |
1111 } | |
1112 else | |
1113 { | |
1114 error("{ } expected following aggregate declaration"); | |
1115 a = new StructDeclaration(loc, NULL); | |
1116 } | |
1117 | |
1118 if (tpl) | |
1119 { Array *decldefs; | |
1120 TemplateDeclaration *tempdecl; | |
1121 | |
1122 // Wrap a template around the aggregate declaration | |
1123 decldefs = new Array(); | |
1124 decldefs->push(a); | |
1125 tempdecl = new TemplateDeclaration(loc, id, tpl, decldefs); | |
1126 return tempdecl; | |
1127 } | |
1128 | |
1129 return a; | |
1130 } | |
1131 | |
1132 /******************************************* | |
1133 */ | |
1134 | |
1135 BaseClasses *Parser::parseBaseClasses() | |
1136 { | |
1137 enum PROT protection = PROTpublic; | |
1138 BaseClasses *baseclasses = new BaseClasses(); | |
1139 | |
1140 for (; 1; nextToken()) | |
1141 { | |
1142 switch (token.value) | |
1143 { | |
1144 case TOKidentifier: | |
1145 break; | |
1146 case TOKprivate: | |
1147 protection = PROTprivate; | |
1148 continue; | |
1149 case TOKpackage: | |
1150 protection = PROTpackage; | |
1151 continue; | |
1152 case TOKprotected: | |
1153 protection = PROTprotected; | |
1154 continue; | |
1155 case TOKpublic: | |
1156 protection = PROTpublic; | |
1157 continue; | |
1158 default: | |
1159 error("base classes expected instead of %s", token.toChars()); | |
1160 return NULL; | |
1161 } | |
1162 BaseClass *b = new BaseClass(parseBasicType(), protection); | |
1163 baseclasses->push(b); | |
1164 if (token.value != TOKcomma) | |
1165 break; | |
1166 protection = PROTpublic; | |
1167 } | |
1168 return baseclasses; | |
1169 } | |
1170 | |
1171 /************************************** | |
1172 * Parse a TemplateDeclaration. | |
1173 */ | |
1174 | |
1175 TemplateDeclaration *Parser::parseTemplateDeclaration() | |
1176 { | |
1177 TemplateDeclaration *tempdecl; | |
1178 Identifier *id; | |
1179 TemplateParameters *tpl; | |
1180 Array *decldefs; | |
1181 Loc loc = this->loc; | |
1182 | |
1183 nextToken(); | |
1184 if (token.value != TOKidentifier) | |
1185 { error("TemplateIdentifier expected following template"); | |
1186 goto Lerr; | |
1187 } | |
1188 id = token.ident; | |
1189 nextToken(); | |
1190 tpl = parseTemplateParameterList(); | |
1191 if (!tpl) | |
1192 goto Lerr; | |
1193 | |
1194 if (token.value != TOKlcurly) | |
1195 { error("members of template declaration expected"); | |
1196 goto Lerr; | |
1197 } | |
1198 else | |
1199 { | |
1200 nextToken(); | |
1201 decldefs = parseDeclDefs(0); | |
1202 if (token.value != TOKrcurly) | |
1203 { error("template member expected"); | |
1204 goto Lerr; | |
1205 } | |
1206 nextToken(); | |
1207 } | |
1208 | |
1209 tempdecl = new TemplateDeclaration(loc, id, tpl, decldefs); | |
1210 return tempdecl; | |
1211 | |
1212 Lerr: | |
1213 return NULL; | |
1214 } | |
1215 | |
1216 /****************************************** | |
1217 * Parse template parameter list. | |
1218 */ | |
1219 | |
1220 TemplateParameters *Parser::parseTemplateParameterList() | |
1221 { | |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
92
diff
changeset
|
1222 TemplateParameters *tpl = new TemplateParameters(); |
1 | 1223 |
1224 if (token.value != TOKlparen) | |
1225 { error("parenthesized TemplateParameterList expected following TemplateIdentifier"); | |
1226 goto Lerr; | |
1227 } | |
1228 nextToken(); | |
1229 | |
1230 // Get array of TemplateParameters | |
1231 if (token.value != TOKrparen) | |
1232 { int isvariadic = 0; | |
1233 | |
1234 while (1) | |
1235 { TemplateParameter *tp; | |
1236 Identifier *tp_ident = NULL; | |
1237 Type *tp_spectype = NULL; | |
1238 Type *tp_valtype = NULL; | |
1239 Type *tp_defaulttype = NULL; | |
1240 Expression *tp_specvalue = NULL; | |
1241 Expression *tp_defaultvalue = NULL; | |
1242 Token *t; | |
1243 | |
1244 // Get TemplateParameter | |
1245 | |
1246 // First, look ahead to see if it is a TypeParameter or a ValueParameter | |
1247 t = peek(&token); | |
1248 if (token.value == TOKalias) | |
1249 { // AliasParameter | |
1250 nextToken(); | |
1251 if (token.value != TOKidentifier) | |
1252 { error("Identifier expected for template parameter"); | |
1253 goto Lerr; | |
1254 } | |
1255 tp_ident = token.ident; | |
1256 nextToken(); | |
1257 if (token.value == TOKcolon) // : Type | |
1258 { | |
1259 nextToken(); | |
1260 tp_spectype = parseBasicType(); | |
1261 tp_spectype = parseDeclarator(tp_spectype, NULL); | |
1262 } | |
1263 if (token.value == TOKassign) // = Type | |
1264 { | |
1265 nextToken(); | |
1266 tp_defaulttype = parseBasicType(); | |
1267 tp_defaulttype = parseDeclarator(tp_defaulttype, NULL); | |
1268 } | |
1269 tp = new TemplateAliasParameter(loc, tp_ident, tp_spectype, tp_defaulttype); | |
1270 } | |
1271 else if (t->value == TOKcolon || t->value == TOKassign || | |
1272 t->value == TOKcomma || t->value == TOKrparen) | |
1273 { // TypeParameter | |
1274 if (token.value != TOKidentifier) | |
1275 { error("Identifier expected for template parameter"); | |
1276 goto Lerr; | |
1277 } | |
1278 tp_ident = token.ident; | |
1279 nextToken(); | |
1280 if (token.value == TOKcolon) // : Type | |
1281 { | |
1282 nextToken(); | |
1283 tp_spectype = parseBasicType(); | |
1284 tp_spectype = parseDeclarator(tp_spectype, NULL); | |
1285 } | |
1286 if (token.value == TOKassign) // = Type | |
1287 { | |
1288 nextToken(); | |
1289 tp_defaulttype = parseBasicType(); | |
1290 tp_defaulttype = parseDeclarator(tp_defaulttype, NULL); | |
1291 } | |
1292 tp = new TemplateTypeParameter(loc, tp_ident, tp_spectype, tp_defaulttype); | |
1293 } | |
1294 else if (token.value == TOKidentifier && t->value == TOKdotdotdot) | |
1295 { // ident... | |
1296 if (isvariadic) | |
1297 error("variadic template parameter must be last"); | |
1298 isvariadic = 1; | |
1299 tp_ident = token.ident; | |
1300 nextToken(); | |
1301 nextToken(); | |
1302 tp = new TemplateTupleParameter(loc, tp_ident); | |
1303 } | |
1304 else | |
1305 { // ValueParameter | |
1306 tp_valtype = parseBasicType(); | |
1307 tp_valtype = parseDeclarator(tp_valtype, &tp_ident); | |
1308 if (!tp_ident) | |
1309 { | |
1310 error("no identifier for template value parameter"); | |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
92
diff
changeset
|
1311 tp_ident = new Identifier("error", TOKidentifier); |
1 | 1312 } |
1313 if (token.value == TOKcolon) // : CondExpression | |
1314 { | |
1315 nextToken(); | |
1316 tp_specvalue = parseCondExp(); | |
1317 } | |
1318 if (token.value == TOKassign) // = CondExpression | |
1319 { | |
1320 nextToken(); | |
1321 tp_defaultvalue = parseCondExp(); | |
1322 } | |
1323 tp = new TemplateValueParameter(loc, tp_ident, tp_valtype, tp_specvalue, tp_defaultvalue); | |
1324 } | |
1325 tpl->push(tp); | |
1326 if (token.value != TOKcomma) | |
1327 break; | |
1328 nextToken(); | |
1329 } | |
1330 } | |
1331 check(TOKrparen); | |
139
0ab29b838084
[svn r143] Fixed: a few bugs in debug information, still only line info, but should be correct files now :)
lindquist
parents:
92
diff
changeset
|
1332 Lerr: |
1 | 1333 return tpl; |
1334 } | |
1335 | |
1336 /****************************************** | |
1337 * Parse template mixin. | |
1338 * mixin Foo; | |
1339 * mixin Foo!(args); | |
1340 * mixin a.b.c!(args).Foo!(args); | |
1341 * mixin Foo!(args) identifier; | |
1342 * mixin typeof(expr).identifier!(args); | |
1343 */ | |
1344 | |
1345 Dsymbol *Parser::parseMixin() | |
1346 { | |
1347 TemplateMixin *tm; | |
1348 Identifier *id; | |
1349 Type *tqual; | |
1350 Objects *tiargs; | |
1351 Array *idents; | |
1352 | |
1353 //printf("parseMixin()\n"); | |
1354 nextToken(); | |
1355 tqual = NULL; | |
1356 if (token.value == TOKdot) | |
1357 { | |
1358 id = Id::empty; | |
1359 } | |
1360 else | |
1361 { | |
1362 if (token.value == TOKtypeof) | |
1363 { Expression *exp; | |
1364 | |
1365 nextToken(); | |
1366 check(TOKlparen); | |
1367 exp = parseExpression(); | |
1368 check(TOKrparen); | |
1369 tqual = new TypeTypeof(loc, exp); | |
1370 check(TOKdot); | |
1371 } | |
1372 if (token.value != TOKidentifier) | |
1373 { | |
1374 error("identifier expected, not %s", token.toChars()); | |
1375 goto Lerr; | |
1376 } | |
1377 id = token.ident; | |
1378 nextToken(); | |
1379 } | |
1380 | |
1381 idents = new Array(); | |
1382 while (1) | |
1383 { | |
1384 tiargs = NULL; | |
1385 if (token.value == TOKnot) | |
1386 { | |
1387 nextToken(); | |
1388 tiargs = parseTemplateArgumentList(); | |
1389 } | |
1390 | |
1391 if (token.value != TOKdot) | |
1392 break; | |
1393 | |
1394 if (tiargs) | |
1395 { TemplateInstance *tempinst = new TemplateInstance(loc, id); | |
1396 tempinst->tiargs = tiargs; | |
1397 id = (Identifier *)tempinst; | |
1398 tiargs = NULL; | |
1399 } | |
1400 idents->push(id); | |
1401 | |
1402 nextToken(); | |
1403 if (token.value != TOKidentifier) | |
1404 { error("identifier expected following '.' instead of '%s'", token.toChars()); | |
1405 break; | |
1406 } | |
1407 id = token.ident; | |
1408 nextToken(); | |
1409 } | |
1410 idents->push(id); | |
1411 | |
1412 if (token.value == TOKidentifier) | |
1413 { | |
1414 id = token.ident; | |
1415 nextToken(); | |
1416 } | |
1417 else | |
1418 id = NULL; | |
1419 | |
1420 tm = new TemplateMixin(loc, id, tqual, idents, tiargs); | |
1421 if (token.value != TOKsemicolon) | |
1422 error("';' expected after mixin"); | |
1423 nextToken(); | |
1424 | |
1425 return tm; | |
1426 | |
1427 Lerr: | |
1428 return NULL; | |
1429 } | |
1430 | |
1431 /****************************************** | |
1432 * Parse template argument list. | |
1433 * Input: | |
1434 * current token is opening '(' | |
1435 * Output: | |
1436 * current token is one after closing ')' | |
1437 */ | |
1438 | |
1439 Objects *Parser::parseTemplateArgumentList() | |
1440 { | |
1441 //printf("Parser::parseTemplateArgumentList()\n"); | |
1442 Objects *tiargs = new Objects(); | |
1443 if (token.value != TOKlparen) | |
1444 { error("!(TemplateArgumentList) expected following TemplateIdentifier"); | |
1445 return tiargs; | |
1446 } | |
1447 nextToken(); | |
1448 | |
1449 // Get TemplateArgumentList | |
1450 if (token.value != TOKrparen) | |
1451 { | |
1452 while (1) | |
1453 { | |
1454 // See if it is an Expression or a Type | |
1455 if (isDeclaration(&token, 0, TOKreserved, NULL)) | |
1456 { // Type | |
1457 Type *ta; | |
1458 | |
1459 // Get TemplateArgument | |
1460 ta = parseBasicType(); | |
1461 ta = parseDeclarator(ta, NULL); | |
1462 tiargs->push(ta); | |
1463 } | |
1464 else | |
1465 { // Expression | |
1466 Expression *ea; | |
1467 | |
1468 ea = parseAssignExp(); | |
1469 tiargs->push(ea); | |
1470 } | |
1471 if (token.value != TOKcomma) | |
1472 break; | |
1473 nextToken(); | |
1474 } | |
1475 } | |
1476 check(TOKrparen, "template argument list"); | |
1477 return tiargs; | |
1478 } | |
1479 | |
1480 Import *Parser::parseImport(Array *decldefs, int isstatic) | |
1481 { Import *s; | |
1482 Identifier *id; | |
1483 Identifier *aliasid = NULL; | |
1484 Array *a; | |
1485 Loc loc; | |
1486 | |
1487 //printf("Parser::parseImport()\n"); | |
1488 do | |
1489 { | |
1490 L1: | |
1491 nextToken(); | |
1492 if (token.value != TOKidentifier) | |
1493 { error("Identifier expected following import"); | |
1494 break; | |
1495 } | |
1496 | |
1497 loc = this->loc; | |
1498 a = NULL; | |
1499 id = token.ident; | |
1500 nextToken(); | |
1501 if (!aliasid && token.value == TOKassign) | |
1502 { | |
1503 aliasid = id; | |
1504 goto L1; | |
1505 } | |
1506 while (token.value == TOKdot) | |
1507 { | |
1508 if (!a) | |
1509 a = new Array(); | |
1510 a->push(id); | |
1511 nextToken(); | |
1512 if (token.value != TOKidentifier) | |
1513 { error("Identifier expected following package"); | |
1514 break; | |
1515 } | |
1516 id = token.ident; | |
1517 nextToken(); | |
1518 } | |
1519 | |
1520 s = new Import(loc, a, token.ident, aliasid, isstatic); | |
1521 decldefs->push(s); | |
1522 | |
1523 /* Look for | |
1524 * : alias=name, alias=name; | |
1525 * syntax. | |
1526 */ | |
1527 if (token.value == TOKcolon) | |
1528 { | |
1529 do | |
1530 { Identifier *name; | |
1531 Identifier *alias; | |
1532 | |
1533 nextToken(); | |
1534 if (token.value != TOKidentifier) | |
1535 { error("Identifier expected following :"); | |
1536 break; | |
1537 } | |
1538 alias = token.ident; | |
1539 nextToken(); | |
1540 if (token.value == TOKassign) | |
1541 { | |
1542 nextToken(); | |
1543 if (token.value != TOKidentifier) | |
1544 { error("Identifier expected following %s=", alias->toChars()); | |
1545 break; | |
1546 } | |
1547 name = token.ident; | |
1548 nextToken(); | |
1549 } | |
1550 else | |
1551 { name = alias; | |
1552 alias = NULL; | |
1553 } | |
1554 s->addAlias(name, alias); | |
1555 } while (token.value == TOKcomma); | |
1556 break; // no comma-separated imports of this form | |
1557 } | |
1558 | |
1559 aliasid = NULL; | |
1560 } while (token.value == TOKcomma); | |
1561 | |
1562 if (token.value == TOKsemicolon) | |
1563 nextToken(); | |
1564 else | |
1565 { | |
1566 error("';' expected"); | |
1567 nextToken(); | |
1568 } | |
1569 | |
1570 return NULL; | |
1571 } | |
1572 | |
1573 Type *Parser::parseBasicType() | |
1574 { Type *t; | |
1575 Identifier *id; | |
1576 TypeQualified *tid; | |
1577 TemplateInstance *tempinst; | |
1578 | |
1579 //printf("parseBasicType()\n"); | |
1580 switch (token.value) | |
1581 { | |
1582 CASE_BASIC_TYPES_X(t): | |
1583 nextToken(); | |
1584 break; | |
1585 | |
1586 case TOKidentifier: | |
1587 id = token.ident; | |
1588 nextToken(); | |
1589 if (token.value == TOKnot) | |
1590 { | |
1591 nextToken(); | |
1592 tempinst = new TemplateInstance(loc, id); | |
1593 tempinst->tiargs = parseTemplateArgumentList(); | |
1594 tid = new TypeInstance(loc, tempinst); | |
1595 goto Lident2; | |
1596 } | |
1597 Lident: | |
1598 tid = new TypeIdentifier(loc, id); | |
1599 Lident2: | |
1600 while (token.value == TOKdot) | |
1601 { nextToken(); | |
1602 if (token.value != TOKidentifier) | |
1603 { error("identifier expected following '.' instead of '%s'", token.toChars()); | |
1604 break; | |
1605 } | |
1606 id = token.ident; | |
1607 nextToken(); | |
1608 if (token.value == TOKnot) | |
1609 { | |
1610 nextToken(); | |
1611 tempinst = new TemplateInstance(loc, id); | |
1612 tempinst->tiargs = parseTemplateArgumentList(); | |
1613 tid->addIdent((Identifier *)tempinst); | |
1614 } | |
1615 else | |
1616 tid->addIdent(id); | |
1617 } | |
1618 t = tid; | |
1619 break; | |
1620 | |
1621 case TOKdot: | |
1622 id = Id::empty; | |
1623 goto Lident; | |
1624 | |
1625 case TOKtypeof: | |
1626 { Expression *exp; | |
1627 | |
1628 nextToken(); | |
1629 check(TOKlparen); | |
1630 exp = parseExpression(); | |
1631 check(TOKrparen); | |
1632 tid = new TypeTypeof(loc, exp); | |
1633 goto Lident2; | |
1634 } | |
1635 | |
1636 default: | |
1637 error("basic type expected, not %s", token.toChars()); | |
1638 t = Type::tint32; | |
1639 break; | |
1640 } | |
1641 return t; | |
1642 } | |
1643 | |
1644 Type *Parser::parseBasicType2(Type *t) | |
1645 { | |
1646 Type *ts; | |
1647 Type *ta; | |
1648 | |
1649 //printf("parseBasicType2()\n"); | |
1650 while (1) | |
1651 { | |
1652 switch (token.value) | |
1653 { | |
1654 case TOKmul: | |
1655 t = new TypePointer(t); | |
1656 nextToken(); | |
1657 continue; | |
1658 | |
1659 case TOKlbracket: | |
1660 #if LTORARRAYDECL | |
1661 // Handle []. Make sure things like | |
1662 // int[3][1] a; | |
1663 // is (array[1] of array[3] of int) | |
1664 nextToken(); | |
1665 if (token.value == TOKrbracket) | |
1666 { | |
1667 t = new TypeDArray(t); // [] | |
1668 nextToken(); | |
1669 } | |
1670 else if (isDeclaration(&token, 0, TOKrbracket, NULL)) | |
1671 { // It's an associative array declaration | |
1672 Type *index; | |
1673 | |
1674 //printf("it's an associative array\n"); | |
1675 index = parseBasicType(); | |
1676 index = parseDeclarator(index, NULL); // [ type ] | |
1677 t = new TypeAArray(t, index); | |
1678 check(TOKrbracket); | |
1679 } | |
1680 else | |
1681 { | |
1682 //printf("it's [expression]\n"); | |
1683 inBrackets++; | |
1684 Expression *e = parseExpression(); // [ expression ] | |
1685 if (token.value == TOKslice) | |
1686 { Expression *e2; | |
1687 | |
1688 nextToken(); | |
1689 e2 = parseExpression(); // [ exp .. exp ] | |
1690 t = new TypeSlice(t, e, e2); | |
1691 } | |
1692 else | |
1693 t = new TypeSArray(t,e); | |
1694 inBrackets--; | |
1695 check(TOKrbracket); | |
1696 } | |
1697 continue; | |
1698 #else | |
1699 // Handle []. Make sure things like | |
1700 // int[3][1] a; | |
1701 // is (array[3] of array[1] of int) | |
1702 ts = t; | |
1703 while (token.value == TOKlbracket) | |
1704 { | |
1705 nextToken(); | |
1706 if (token.value == TOKrbracket) | |
1707 { | |
1708 ta = new TypeDArray(t); // [] | |
1709 nextToken(); | |
1710 } | |
1711 else if (isDeclaration(&token, 0, TOKrbracket, NULL)) | |
1712 { // It's an associative array declaration | |
1713 Type *index; | |
1714 | |
1715 //printf("it's an associative array\n"); | |
1716 index = parseBasicType(); | |
1717 index = parseDeclarator(index, NULL); // [ type ] | |
1718 check(TOKrbracket); | |
1719 ta = new TypeAArray(t, index); | |
1720 } | |
1721 else | |
1722 { | |
1723 //printf("it's [expression]\n"); | |
1724 Expression *e = parseExpression(); // [ expression ] | |
1725 ta = new TypeSArray(t,e); | |
1726 check(TOKrbracket); | |
1727 } | |
1728 Type **pt; | |
1729 for (pt = &ts; *pt != t; pt = &(*pt)->next) | |
1730 ; | |
1731 *pt = ta; | |
1732 } | |
1733 t = ts; | |
1734 continue; | |
1735 #endif | |
1736 | |
1737 case TOKdelegate: | |
1738 case TOKfunction: | |
1739 { // Handle delegate declaration: | |
1740 // t delegate(parameter list) | |
1741 // t function(parameter list) | |
1742 Arguments *arguments; | |
1743 int varargs; | |
1744 enum TOK save = token.value; | |
1745 | |
1746 nextToken(); | |
1747 arguments = parseParameters(&varargs); | |
1748 t = new TypeFunction(arguments, t, varargs, linkage); | |
1749 if (save == TOKdelegate) | |
1750 t = new TypeDelegate(t); | |
1751 else | |
1752 t = new TypePointer(t); // pointer to function | |
1753 continue; | |
1754 } | |
1755 | |
1756 default: | |
1757 ts = t; | |
1758 break; | |
1759 } | |
1760 break; | |
1761 } | |
1762 return ts; | |
1763 } | |
1764 | |
1765 Type *Parser::parseDeclarator(Type *t, Identifier **pident, TemplateParameters **tpl) | |
1766 { Type *ts; | |
1767 Type *ta; | |
1768 | |
1769 //printf("parseDeclarator(tpl = %p)\n", tpl); | |
1770 t = parseBasicType2(t); | |
1771 | |
1772 switch (token.value) | |
1773 { | |
1774 | |
1775 case TOKidentifier: | |
1776 if (pident) | |
1777 *pident = token.ident; | |
1778 else | |
1779 error("unexpected identifer '%s' in declarator", token.ident->toChars()); | |
1780 ts = t; | |
1781 nextToken(); | |
1782 break; | |
1783 | |
1784 case TOKlparen: | |
1785 nextToken(); | |
1786 ts = parseDeclarator(t, pident); | |
1787 check(TOKrparen); | |
1788 break; | |
1789 | |
1790 default: | |
1791 ts = t; | |
1792 break; | |
1793 } | |
1794 | |
1795 while (1) | |
1796 { | |
1797 switch (token.value) | |
1798 { | |
1799 #if CARRAYDECL | |
1800 case TOKlbracket: | |
1801 { // This is the old C-style post [] syntax. | |
1802 nextToken(); | |
1803 if (token.value == TOKrbracket) | |
1804 { | |
1805 ta = new TypeDArray(t); // [] | |
1806 nextToken(); | |
1807 } | |
1808 else if (isDeclaration(&token, 0, TOKrbracket, NULL)) | |
1809 { // It's an associative array declaration | |
1810 Type *index; | |
1811 | |
1812 //printf("it's an associative array\n"); | |
1813 index = parseBasicType(); | |
1814 index = parseDeclarator(index, NULL); // [ type ] | |
1815 check(TOKrbracket); | |
1816 ta = new TypeAArray(t, index); | |
1817 } | |
1818 else | |
1819 { | |
1820 //printf("it's [expression]\n"); | |
1821 Expression *e = parseExpression(); // [ expression ] | |
1822 ta = new TypeSArray(t, e); | |
1823 check(TOKrbracket); | |
1824 } | |
1825 Type **pt; | |
1826 for (pt = &ts; *pt != t; pt = &(*pt)->next) | |
1827 ; | |
1828 *pt = ta; | |
1829 continue; | |
1830 } | |
1831 #endif | |
1832 case TOKlparen: | |
1833 { Arguments *arguments; | |
1834 int varargs; | |
1835 | |
1836 if (tpl) | |
1837 { | |
1838 /* Look ahead to see if this is (...)(...), | |
1839 * i.e. a function template declaration | |
1840 */ | |
1841 if (peekPastParen(&token)->value == TOKlparen) | |
1842 { // It's a function template declaration | |
1843 //printf("function template declaration\n"); | |
1844 | |
1845 // Gather template parameter list | |
1846 *tpl = parseTemplateParameterList(); | |
1847 } | |
1848 } | |
1849 | |
1850 arguments = parseParameters(&varargs); | |
35
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
1851 Type *ta = new TypeFunction(arguments, t, varargs, linkage); |
3cfcb944304e
[svn r39] * Updated to DMD 1.022 with the exception of:
lindquist
parents:
19
diff
changeset
|
1852 Type **pt; |
1 | 1853 for (pt = &ts; *pt != t; pt = &(*pt)->next) |
1854 ; | |
1855 *pt = ta; | |
1856 break; | |
1857 } | |
1858 } | |
1859 break; | |
1860 } | |
1861 | |
1862 return ts; | |
1863 } | |
1864 | |
1865 /********************************** | |
1866 * Return array of Declaration *'s. | |
1867 */ | |
1868 | |
1869 Array *Parser::parseDeclarations() | |
1870 { | |
1871 enum STC storage_class; | |
1872 enum STC stc; | |
1873 Type *ts; | |
1874 Type *t; | |
1875 Type *tfirst; | |
1876 Identifier *ident; | |
1877 Array *a; | |
1878 enum TOK tok; | |
1879 unsigned char *comment = token.blockComment; | |
1880 enum LINK link = linkage; | |
1881 | |
1882 //printf("parseDeclarations()\n"); | |
1883 switch (token.value) | |
1884 { | |
1885 case TOKtypedef: | |
1886 case TOKalias: | |
1887 tok = token.value; | |
1888 nextToken(); | |
1889 break; | |
1890 | |
1891 default: | |
1892 tok = TOKreserved; | |
1893 break; | |
1894 } | |
1895 | |
1896 storage_class = STCundefined; | |
1897 while (1) | |
1898 { | |
1899 switch (token.value) | |
1900 { | |
1901 case TOKconst: stc = STCconst; goto L1; | |
1902 case TOKstatic: stc = STCstatic; goto L1; | |
1903 case TOKfinal: stc = STCfinal; goto L1; | |
1904 case TOKauto: stc = STCauto; goto L1; | |
1905 case TOKscope: stc = STCscope; goto L1; | |
1906 case TOKoverride: stc = STCoverride; goto L1; | |
1907 case TOKabstract: stc = STCabstract; goto L1; | |
1908 case TOKsynchronized: stc = STCsynchronized; goto L1; | |
1909 case TOKdeprecated: stc = STCdeprecated; goto L1; | |
1910 L1: | |
1911 if (storage_class & stc) | |
1912 error("redundant storage class '%s'", token.toChars()); | |
1913 storage_class = (STC) (storage_class | stc); | |
1914 nextToken(); | |
1915 continue; | |
1916 | |
1917 case TOKextern: | |
1918 if (peek(&token)->value != TOKlparen) | |
1919 { stc = STCextern; | |
1920 goto L1; | |
1921 } | |
1922 | |
1923 link = parseLinkage(); | |
1924 continue; | |
1925 | |
1926 default: | |
1927 break; | |
1928 } | |
1929 break; | |
1930 } | |
1931 | |
1932 a = new Array(); | |
1933 | |
1934 /* Look for auto initializers: | |
1935 * storage_class identifier = initializer; | |
1936 */ | |
1937 while (storage_class && | |
1938 token.value == TOKidentifier && | |
1939 peek(&token)->value == TOKassign) | |
1940 { | |
1941 ident = token.ident; | |
1942 nextToken(); | |
1943 nextToken(); | |
1944 Initializer *init = parseInitializer(); | |
1945 VarDeclaration *v = new VarDeclaration(loc, NULL, ident, init); | |
1946 v->storage_class = storage_class; | |
1947 a->push(v); | |
1948 if (token.value == TOKsemicolon) | |
1949 { | |
1950 nextToken(); | |
1951 addComment(v, comment); | |
1952 } | |
1953 else if (token.value == TOKcomma) | |
1954 { | |
1955 nextToken(); | |
1956 if (!(token.value == TOKidentifier && peek(&token)->value == TOKassign)) | |
1957 { | |
1958 error("Identifier expected following comma"); | |
1959 } | |
1960 else | |
1961 continue; | |
1962 } | |
1963 else | |
1964 error("semicolon expected following auto declaration, not '%s'", token.toChars()); | |
1965 return a; | |
1966 } | |
1967 | |
1968 if (token.value == TOKclass) | |
1969 { AggregateDeclaration *s; | |
1970 | |
1971 s = (AggregateDeclaration *)parseAggregate(); | |
1972 s->storage_class |= storage_class; | |
1973 a->push(s); | |
1974 addComment(s, comment); | |
1975 return a; | |
1976 } | |
1977 | |
1978 ts = parseBasicType(); | |
1979 ts = parseBasicType2(ts); | |
1980 tfirst = NULL; | |
1981 | |
1982 while (1) | |
1983 { | |
1984 Loc loc = this->loc; | |
1985 TemplateParameters *tpl = NULL; | |
1986 | |
1987 ident = NULL; | |
1988 t = parseDeclarator(ts, &ident, &tpl); | |
1989 assert(t); | |
1990 if (!tfirst) | |
1991 tfirst = t; | |
1992 else if (t != tfirst) | |
1993 error("multiple declarations must have the same type, not %s and %s", | |
1994 tfirst->toChars(), t->toChars()); | |
1995 if (!ident) | |
1996 error("no identifier for declarator %s", t->toChars()); | |
1997 | |
1998 if (tok == TOKtypedef || tok == TOKalias) | |
1999 { Declaration *v; | |
2000 Initializer *init; | |
2001 | |
2002 init = NULL; | |
2003 if (token.value == TOKassign) | |
2004 { | |
2005 nextToken(); | |
2006 init = parseInitializer(); | |
2007 } | |
2008 if (tok == TOKtypedef) | |
2009 v = new TypedefDeclaration(loc, ident, t, init); | |
2010 else | |
2011 { if (init) | |
2012 error("alias cannot have initializer"); | |
2013 v = new AliasDeclaration(loc, ident, t); | |
2014 } | |
2015 v->storage_class = storage_class; | |
2016 if (link == linkage) | |
2017 a->push(v); | |
2018 else | |
2019 { | |
2020 Array *ax = new Array(); | |
2021 ax->push(v); | |
2022 Dsymbol *s = new LinkDeclaration(link, ax); | |
2023 a->push(s); | |
2024 } | |
2025 switch (token.value) | |
2026 { case TOKsemicolon: | |
2027 nextToken(); | |
2028 addComment(v, comment); | |
2029 break; | |
2030 | |
2031 case TOKcomma: | |
2032 nextToken(); | |
2033 addComment(v, comment); | |
2034 continue; | |
2035 | |
2036 default: | |
2037 error("semicolon expected to close %s declaration", Token::toChars(tok)); | |
2038 break; | |
2039 } | |
2040 } | |
2041 else if (t->ty == Tfunction) | |
2042 { FuncDeclaration *f; | |
2043 Dsymbol *s; | |
2044 | |
2045 f = new FuncDeclaration(loc, 0, ident, storage_class, t); | |
2046 addComment(f, comment); | |
2047 parseContracts(f); | |
2048 addComment(f, NULL); | |
2049 if (link == linkage) | |
2050 { | |
2051 s = f; | |
2052 } | |
2053 else | |
2054 { | |
2055 Array *ax = new Array(); | |
2056 ax->push(f); | |
2057 s = new LinkDeclaration(link, ax); | |
2058 } | |
2059 if (tpl) // it's a function template | |
2060 { Array *decldefs; | |
2061 TemplateDeclaration *tempdecl; | |
2062 | |
2063 // Wrap a template around the aggregate declaration | |
2064 decldefs = new Array(); | |
2065 decldefs->push(s); | |
2066 tempdecl = new TemplateDeclaration(loc, s->ident, tpl, decldefs); | |
2067 s = tempdecl; | |
2068 } | |
2069 addComment(s, comment); | |
2070 a->push(s); | |
2071 } | |
2072 else | |
2073 { VarDeclaration *v; | |
2074 Initializer *init; | |
2075 | |
2076 init = NULL; | |
2077 if (token.value == TOKassign) | |
2078 { | |
2079 nextToken(); | |
2080 init = parseInitializer(); | |
2081 } | |
2082 v = new VarDeclaration(loc, t, ident, init); | |
2083 v->storage_class = storage_class; | |
2084 if (link == linkage) | |
2085 a->push(v); | |
2086 else | |
2087 { | |
2088 Array *ax = new Array(); | |
2089 ax->push(v); | |
2090 Dsymbol *s = new LinkDeclaration(link, ax); | |
2091 a->push(s); | |
2092 } | |
2093 switch (token.value) | |
2094 { case TOKsemicolon: | |
2095 nextToken(); | |
2096 addComment(v, comment); | |
2097 break; | |
2098 | |
2099 case TOKcomma: | |
2100 nextToken(); | |
2101 addComment(v, comment); | |
2102 continue; | |
2103 | |
2104 default: | |
2105 error("semicolon expected, not '%s'", token.toChars()); | |
2106 break; | |
2107 } | |
2108 } | |
2109 break; | |
2110 } | |
2111 return a; | |
2112 } | |
2113 | |
2114 /***************************************** | |
2115 * Parse contracts following function declaration. | |
2116 */ | |
2117 | |
2118 void Parser::parseContracts(FuncDeclaration *f) | |
2119 { | |
2120 Type *tb; | |
2121 enum LINK linksave = linkage; | |
2122 | |
2123 // The following is irrelevant, as it is overridden by sc->linkage in | |
2124 // TypeFunction::semantic | |
2125 linkage = LINKd; // nested functions have D linkage | |
2126 L1: | |
2127 switch (token.value) | |
2128 { | |
2129 case TOKlcurly: | |
2130 if (f->frequire || f->fensure) | |
2131 error("missing body { ... } after in or out"); | |
2132 f->fbody = parseStatement(PSsemi); | |
2133 f->endloc = endloc; | |
2134 break; | |
2135 | |
2136 case TOKbody: | |
2137 nextToken(); | |
2138 f->fbody = parseStatement(PScurly); | |
2139 f->endloc = endloc; | |
2140 break; | |
2141 | |
2142 case TOKsemicolon: | |
2143 if (f->frequire || f->fensure) | |
2144 error("missing body { ... } after in or out"); | |
2145 nextToken(); | |
2146 break; | |
2147 | |
2148 #if 0 // Do we want this for function declarations, so we can do: | |
2149 // int x, y, foo(), z; | |
2150 case TOKcomma: | |
2151 nextToken(); | |
2152 continue; | |
2153 #endif | |
2154 | |
2155 #if 0 // Dumped feature | |
2156 case TOKthrow: | |
2157 if (!f->fthrows) | |
2158 f->fthrows = new Array(); | |
2159 nextToken(); | |
2160 check(TOKlparen); | |
2161 while (1) | |
2162 { | |
2163 tb = parseBasicType(); | |
2164 f->fthrows->push(tb); | |
2165 if (token.value == TOKcomma) | |
2166 { nextToken(); | |
2167 continue; | |
2168 } | |
2169 break; | |
2170 } | |
2171 check(TOKrparen); | |
2172 goto L1; | |
2173 #endif | |
2174 | |
2175 case TOKin: | |
2176 nextToken(); | |
2177 if (f->frequire) | |
2178 error("redundant 'in' statement"); | |
2179 f->frequire = parseStatement(PScurly | PSscope); | |
2180 goto L1; | |
2181 | |
2182 case TOKout: | |
2183 // parse: out (identifier) { statement } | |
2184 nextToken(); | |
2185 if (token.value != TOKlcurly) | |
2186 { | |
2187 check(TOKlparen); | |
2188 if (token.value != TOKidentifier) | |
2189 error("(identifier) following 'out' expected, not %s", token.toChars()); | |
2190 f->outId = token.ident; | |
2191 nextToken(); | |
2192 check(TOKrparen); | |
2193 } | |
2194 if (f->fensure) | |
2195 error("redundant 'out' statement"); | |
2196 f->fensure = parseStatement(PScurly | PSscope); | |
2197 goto L1; | |
2198 | |
2199 default: | |
2200 error("semicolon expected following function declaration"); | |
2201 break; | |
2202 } | |
2203 linkage = linksave; | |
2204 } | |
2205 | |
2206 /***************************************** | |
2207 */ | |
2208 | |
2209 Initializer *Parser::parseInitializer() | |
2210 { | |
2211 StructInitializer *is; | |
2212 ArrayInitializer *ia; | |
2213 ExpInitializer *ie; | |
2214 Expression *e; | |
2215 Identifier *id; | |
2216 Initializer *value; | |
2217 int comma; | |
2218 Loc loc = this->loc; | |
2219 Token *t; | |
2220 int braces; | |
2221 | |
2222 switch (token.value) | |
2223 { | |
2224 case TOKlcurly: | |
2225 /* Scan ahead to see if it is a struct initializer or | |
2226 * a function literal. | |
2227 * If it contains a ';', it is a function literal. | |
2228 * Treat { } as a struct initializer. | |
2229 */ | |
2230 braces = 1; | |
2231 for (t = peek(&token); 1; t = peek(t)) | |
2232 { | |
2233 switch (t->value) | |
2234 { | |
2235 case TOKsemicolon: | |
2236 case TOKreturn: | |
2237 goto Lexpression; | |
2238 | |
2239 case TOKlcurly: | |
2240 braces++; | |
2241 continue; | |
2242 | |
2243 case TOKrcurly: | |
2244 if (--braces == 0) | |
2245 break; | |
2246 continue; | |
2247 | |
92 | 2248 case TOKeof: |
2249 break; | |
2250 | |
1 | 2251 default: |
2252 continue; | |
2253 } | |
2254 break; | |
2255 } | |
2256 | |
2257 is = new StructInitializer(loc); | |
2258 nextToken(); | |
2259 comma = 0; | |
2260 while (1) | |
2261 { | |
2262 switch (token.value) | |
2263 { | |
2264 case TOKidentifier: | |
2265 if (comma == 1) | |
2266 error("comma expected separating field initializers"); | |
2267 t = peek(&token); | |
2268 if (t->value == TOKcolon) | |
2269 { | |
2270 id = token.ident; | |
2271 nextToken(); | |
2272 nextToken(); // skip over ':' | |
2273 } | |
2274 else | |
2275 { id = NULL; | |
2276 } | |
2277 value = parseInitializer(); | |
2278 is->addInit(id, value); | |
2279 comma = 1; | |
2280 continue; | |
2281 | |
2282 case TOKcomma: | |
2283 nextToken(); | |
2284 comma = 2; | |
2285 continue; | |
2286 | |
2287 case TOKrcurly: // allow trailing comma's | |
2288 nextToken(); | |
2289 break; | |
2290 | |
92 | 2291 case TOKeof: |
2292 error("found EOF instead of initializer"); | |
2293 break; | |
2294 | |
1 | 2295 default: |
2296 value = parseInitializer(); | |
2297 is->addInit(NULL, value); | |
2298 comma = 1; | |
2299 continue; | |
2300 //error("found '%s' instead of field initializer", token.toChars()); | |
2301 //break; | |
2302 } | |
2303 break; | |
2304 } | |
2305 return is; | |
2306 | |
2307 case TOKlbracket: | |
2308 ia = new ArrayInitializer(loc); | |
2309 nextToken(); | |
2310 comma = 0; | |
2311 while (1) | |
2312 { | |
2313 switch (token.value) | |
2314 { | |
2315 default: | |
2316 if (comma == 1) | |
2317 { error("comma expected separating array initializers, not %s", token.toChars()); | |
2318 nextToken(); | |
2319 break; | |
2320 } | |
2321 e = parseAssignExp(); | |
2322 if (!e) | |
2323 break; | |
2324 if (token.value == TOKcolon) | |
2325 { | |
2326 nextToken(); | |
2327 value = parseInitializer(); | |
2328 } | |
2329 else | |
2330 { value = new ExpInitializer(e->loc, e); | |
2331 e = NULL; | |
2332 } | |
2333 ia->addInit(e, value); | |
2334 comma = 1; | |
2335 continue; | |
2336 | |
2337 case TOKlcurly: | |
2338 case TOKlbracket: | |
2339 if (comma == 1) | |
2340 error("comma expected separating array initializers, not %s", token.toChars()); | |
2341 value = parseInitializer(); | |
2342 ia->addInit(NULL, value); | |
2343 comma = 1; | |
2344 continue; | |
2345 | |
2346 case TOKcomma: | |
2347 nextToken(); | |
2348 comma = 2; | |
2349 continue; | |
2350 | |
2351 case TOKrbracket: // allow trailing comma's | |
2352 nextToken(); | |
2353 break; | |
2354 | |
2355 case TOKeof: | |
2356 error("found '%s' instead of array initializer", token.toChars()); | |
2357 break; | |
2358 } | |
2359 break; | |
2360 } | |
2361 return ia; | |
2362 | |
2363 case TOKvoid: | |
2364 t = peek(&token); | |
2365 if (t->value == TOKsemicolon || t->value == TOKcomma) | |
2366 { | |
2367 nextToken(); | |
2368 return new VoidInitializer(loc); | |
2369 } | |
2370 goto Lexpression; | |
2371 | |
2372 default: | |
2373 Lexpression: | |
2374 e = parseAssignExp(); | |
2375 ie = new ExpInitializer(loc, e); | |
2376 return ie; | |
2377 } | |
2378 } | |
2379 | |
2380 | |
2381 /***************************************** | |
2382 * Input: | |
2383 * flags PSxxxx | |
2384 */ | |
2385 | |
2386 Statement *Parser::parseStatement(int flags) | |
2387 { Statement *s; | |
2388 Token *t; | |
2389 Condition *condition; | |
2390 Statement *ifbody; | |
2391 Statement *elsebody; | |
2392 Loc loc = this->loc; | |
2393 | |
2394 //printf("parseStatement()\n"); | |
2395 | |
2396 if (flags & PScurly && token.value != TOKlcurly) | |
2397 error("statement expected to be { }, not %s", token.toChars()); | |
2398 | |
2399 switch (token.value) | |
2400 { | |
2401 case TOKidentifier: | |
2402 // Need to look ahead to see if it is a declaration, label, or expression | |
2403 t = peek(&token); | |
2404 if (t->value == TOKcolon) | |
2405 { // It's a label | |
2406 Identifier *ident; | |
2407 | |
2408 ident = token.ident; | |
2409 nextToken(); | |
2410 nextToken(); | |
2411 s = parseStatement(PSsemi); | |
2412 s = new LabelStatement(loc, ident, s); | |
2413 break; | |
2414 } | |
2415 // fallthrough to TOKdot | |
2416 case TOKdot: | |
2417 case TOKtypeof: | |
2418 if (isDeclaration(&token, 2, TOKreserved, NULL)) | |
2419 goto Ldeclaration; | |
2420 else | |
2421 goto Lexp; | |
2422 break; | |
2423 | |
2424 case TOKassert: | |
2425 case TOKthis: | |
2426 case TOKsuper: | |
2427 case TOKint32v: | |
2428 case TOKuns32v: | |
2429 case TOKint64v: | |
2430 case TOKuns64v: | |
2431 case TOKfloat32v: | |
2432 case TOKfloat64v: | |
2433 case TOKfloat80v: | |
2434 case TOKimaginary32v: | |
2435 case TOKimaginary64v: | |
2436 case TOKimaginary80v: | |
2437 case TOKcharv: | |
2438 case TOKwcharv: | |
2439 case TOKdcharv: | |
2440 case TOKnull: | |
2441 case TOKtrue: | |
2442 case TOKfalse: | |
2443 case TOKstring: | |
2444 case TOKlparen: | |
2445 case TOKcast: | |
2446 case TOKmul: | |
2447 case TOKmin: | |
2448 case TOKadd: | |
2449 case TOKplusplus: | |
2450 case TOKminusminus: | |
2451 case TOKnew: | |
2452 case TOKdelete: | |
2453 case TOKdelegate: | |
2454 case TOKfunction: | |
2455 case TOKtypeid: | |
2456 case TOKis: | |
2457 case TOKlbracket: | |
2458 Lexp: | |
2459 { Expression *exp; | |
2460 | |
2461 exp = parseExpression(); | |
2462 check(TOKsemicolon, "statement"); | |
2463 s = new ExpStatement(loc, exp); | |
2464 break; | |
2465 } | |
2466 | |
2467 case TOKstatic: | |
2468 { // Look ahead to see if it's static assert() or static if() | |
2469 Token *t; | |
2470 | |
2471 t = peek(&token); | |
2472 if (t->value == TOKassert) | |
2473 { | |
2474 nextToken(); | |
2475 s = new StaticAssertStatement(parseStaticAssert()); | |
2476 break; | |
2477 } | |
2478 if (t->value == TOKif) | |
2479 { | |
2480 nextToken(); | |
2481 condition = parseStaticIfCondition(); | |
2482 goto Lcondition; | |
2483 } | |
2484 goto Ldeclaration; | |
2485 } | |
2486 | |
2487 CASE_BASIC_TYPES: | |
2488 case TOKtypedef: | |
2489 case TOKalias: | |
2490 case TOKconst: | |
2491 case TOKauto: | |
2492 case TOKextern: | |
2493 case TOKfinal: | |
2494 case TOKinvariant: | |
2495 // case TOKtypeof: | |
2496 Ldeclaration: | |
2497 { Array *a; | |
2498 | |
2499 a = parseDeclarations(); | |
2500 if (a->dim > 1) | |
2501 { | |
2502 Statements *as = new Statements(); | |
2503 as->reserve(a->dim); | |
2504 for (int i = 0; i < a->dim; i++) | |
2505 { | |
2506 Dsymbol *d = (Dsymbol *)a->data[i]; | |
2507 s = new DeclarationStatement(loc, d); | |
2508 as->push(s); | |
2509 } | |
2510 s = new CompoundStatement(loc, as); | |
2511 } | |
2512 else if (a->dim == 1) | |
2513 { | |
2514 Dsymbol *d = (Dsymbol *)a->data[0]; | |
2515 s = new DeclarationStatement(loc, d); | |
2516 } | |
2517 else | |
2518 assert(0); | |
2519 if (flags & PSscope) | |
2520 s = new ScopeStatement(loc, s); | |
2521 break; | |
2522 } | |
2523 | |
2524 case TOKstruct: | |
2525 case TOKunion: | |
2526 case TOKclass: | |
2527 case TOKinterface: | |
2528 { Dsymbol *d; | |
2529 | |
2530 d = parseAggregate(); | |
2531 s = new DeclarationStatement(loc, d); | |
2532 break; | |
2533 } | |
2534 | |
2535 case TOKenum: | |
2536 { Dsymbol *d; | |
2537 | |
2538 d = parseEnum(); | |
2539 s = new DeclarationStatement(loc, d); | |
2540 break; | |
2541 } | |
2542 | |
2543 case TOKmixin: | |
2544 { t = peek(&token); | |
2545 if (t->value == TOKlparen) | |
2546 { // mixin(string) | |
2547 nextToken(); | |
2548 check(TOKlparen, "mixin"); | |
2549 Expression *e = parseAssignExp(); | |
2550 check(TOKrparen); | |
2551 check(TOKsemicolon); | |
2552 s = new CompileStatement(loc, e); | |
2553 break; | |
2554 } | |
2555 Dsymbol *d = parseMixin(); | |
2556 s = new DeclarationStatement(loc, d); | |
2557 break; | |
2558 } | |
2559 | |
2560 case TOKlcurly: | |
2561 { Statements *statements; | |
2562 | |
2563 nextToken(); | |
2564 statements = new Statements(); | |
2565 while (token.value != TOKrcurly) | |
2566 { | |
2567 statements->push(parseStatement(PSsemi | PScurlyscope)); | |
2568 } | |
2569 endloc = this->loc; | |
2570 s = new CompoundStatement(loc, statements); | |
2571 if (flags & (PSscope | PScurlyscope)) | |
2572 s = new ScopeStatement(loc, s); | |
2573 nextToken(); | |
2574 break; | |
2575 } | |
2576 | |
2577 case TOKwhile: | |
2578 { Expression *condition; | |
2579 Statement *body; | |
2580 | |
2581 nextToken(); | |
2582 check(TOKlparen); | |
2583 condition = parseExpression(); | |
2584 check(TOKrparen); | |
2585 body = parseStatement(PSscope); | |
2586 s = new WhileStatement(loc, condition, body); | |
2587 break; | |
2588 } | |
2589 | |
2590 case TOKsemicolon: | |
2591 if (!(flags & PSsemi)) | |
2592 error("use '{ }' for an empty statement, not a ';'"); | |
2593 nextToken(); | |
2594 s = new ExpStatement(loc, NULL); | |
2595 break; | |
2596 | |
2597 case TOKdo: | |
2598 { Statement *body; | |
2599 Expression *condition; | |
2600 | |
2601 nextToken(); | |
2602 body = parseStatement(PSscope); | |
2603 check(TOKwhile); | |
2604 check(TOKlparen); | |
2605 condition = parseExpression(); | |
2606 check(TOKrparen); | |
2607 s = new DoStatement(loc, body, condition); | |
2608 break; | |
2609 } | |
2610 | |
2611 case TOKfor: | |
2612 { | |
2613 Statement *init; | |
2614 Expression *condition; | |
2615 Expression *increment; | |
2616 Statement *body; | |
2617 | |
2618 nextToken(); | |
2619 check(TOKlparen); | |
2620 if (token.value == TOKsemicolon) | |
2621 { init = NULL; | |
2622 nextToken(); | |
2623 } | |
2624 else | |
2625 { init = parseStatement(0); | |
2626 } | |
2627 if (token.value == TOKsemicolon) | |
2628 { | |
2629 condition = NULL; | |
2630 nextToken(); | |
2631 } | |
2632 else | |
2633 { | |
2634 condition = parseExpression(); | |
2635 check(TOKsemicolon, "for condition"); | |
2636 } | |
2637 if (token.value == TOKrparen) | |
2638 { increment = NULL; | |
2639 nextToken(); | |
2640 } | |
2641 else | |
2642 { increment = parseExpression(); | |
2643 check(TOKrparen); | |
2644 } | |
2645 body = parseStatement(PSscope); | |
2646 s = new ForStatement(loc, init, condition, increment, body); | |
2647 if (init) | |
2648 s = new ScopeStatement(loc, s); | |
2649 break; | |
2650 } | |
2651 | |
2652 case TOKforeach: | |
2653 case TOKforeach_reverse: | |
2654 { | |
2655 enum TOK op = token.value; | |
2656 Arguments *arguments; | |
2657 | |
2658 Statement *d; | |
2659 Statement *body; | |
2660 Expression *aggr; | |
2661 | |
2662 nextToken(); | |
2663 check(TOKlparen); | |
2664 | |
2665 arguments = new Arguments(); | |
2666 | |
2667 while (1) | |
2668 { | |
2669 Type *tb; | |
2670 Identifier *ai = NULL; | |
2671 Type *at; | |
2672 unsigned storageClass; | |
2673 Argument *a; | |
2674 | |
2675 storageClass = STCin; | |
2676 if (token.value == TOKinout || token.value == TOKref) | |
2677 { storageClass = STCref; | |
2678 nextToken(); | |
2679 } | |
2680 if (token.value == TOKidentifier) | |
2681 { | |
2682 Token *t = peek(&token); | |
2683 if (t->value == TOKcomma || t->value == TOKsemicolon) | |
2684 { ai = token.ident; | |
2685 at = NULL; // infer argument type | |
2686 nextToken(); | |
2687 goto Larg; | |
2688 } | |
2689 } | |
2690 tb = parseBasicType(); | |
2691 at = parseDeclarator(tb, &ai); | |
2692 if (!ai) | |
2693 error("no identifier for declarator %s", at->toChars()); | |
2694 Larg: | |
2695 a = new Argument(storageClass, at, ai, NULL); | |
2696 arguments->push(a); | |
2697 if (token.value == TOKcomma) | |
2698 { nextToken(); | |
2699 continue; | |
2700 } | |
2701 break; | |
2702 } | |
2703 check(TOKsemicolon); | |
2704 | |
2705 aggr = parseExpression(); | |
2706 check(TOKrparen); | |
2707 body = parseStatement(0); | |
2708 s = new ForeachStatement(loc, op, arguments, aggr, body); | |
2709 break; | |
2710 } | |
2711 | |
2712 case TOKif: | |
2713 { Argument *arg = NULL; | |
2714 Expression *condition; | |
2715 Statement *ifbody; | |
2716 Statement *elsebody; | |
2717 | |
2718 nextToken(); | |
2719 check(TOKlparen); | |
2720 | |
2721 if (token.value == TOKauto) | |
2722 { | |
2723 nextToken(); | |
2724 if (token.value == TOKidentifier) | |
2725 { | |
2726 Token *t = peek(&token); | |
2727 if (t->value == TOKassign) | |
2728 { | |
2729 arg = new Argument(STCin, NULL, token.ident, NULL); | |
2730 nextToken(); | |
2731 nextToken(); | |
2732 } | |
2733 else | |
2734 { error("= expected following auto identifier"); | |
2735 goto Lerror; | |
2736 } | |
2737 } | |
2738 else | |
2739 { error("identifier expected following auto"); | |
2740 goto Lerror; | |
2741 } | |
2742 } | |
2743 else if (isDeclaration(&token, 2, TOKassign, NULL)) | |
2744 { | |
2745 Type *tb; | |
2746 Type *at; | |
2747 Identifier *ai; | |
2748 | |
2749 tb = parseBasicType(); | |
2750 at = parseDeclarator(tb, &ai); | |
2751 check(TOKassign); | |
2752 arg = new Argument(STCin, at, ai, NULL); | |
2753 } | |
2754 | |
2755 // Check for " ident;" | |
2756 else if (token.value == TOKidentifier) | |
2757 { | |
2758 Token *t = peek(&token); | |
2759 if (t->value == TOKcomma || t->value == TOKsemicolon) | |
2760 { | |
2761 arg = new Argument(STCin, NULL, token.ident, NULL); | |
2762 nextToken(); | |
2763 nextToken(); | |
2764 if (1 || !global.params.useDeprecated) | |
2765 error("if (v; e) is deprecated, use if (auto v = e)"); | |
2766 } | |
2767 } | |
2768 | |
2769 condition = parseExpression(); | |
2770 check(TOKrparen); | |
2771 ifbody = parseStatement(PSscope); | |
2772 if (token.value == TOKelse) | |
2773 { | |
2774 nextToken(); | |
2775 elsebody = parseStatement(PSscope); | |
2776 } | |
2777 else | |
2778 elsebody = NULL; | |
2779 s = new IfStatement(loc, arg, condition, ifbody, elsebody); | |
2780 break; | |
2781 } | |
2782 | |
2783 case TOKscope: | |
2784 if (peek(&token)->value != TOKlparen) | |
2785 goto Ldeclaration; // scope used as storage class | |
2786 nextToken(); | |
2787 check(TOKlparen); | |
2788 if (token.value != TOKidentifier) | |
2789 { error("scope identifier expected"); | |
2790 goto Lerror; | |
2791 } | |
2792 else | |
2793 { TOK t = TOKon_scope_exit; | |
2794 Identifier *id = token.ident; | |
2795 | |
2796 if (id == Id::exit) | |
2797 t = TOKon_scope_exit; | |
2798 else if (id == Id::failure) | |
2799 t = TOKon_scope_failure; | |
2800 else if (id == Id::success) | |
2801 t = TOKon_scope_success; | |
2802 else | |
2803 error("valid scope identifiers are exit, failure, or success, not %s", id->toChars()); | |
2804 nextToken(); | |
2805 check(TOKrparen); | |
2806 Statement *st = parseStatement(PScurlyscope); | |
2807 s = new OnScopeStatement(loc, t, st); | |
2808 break; | |
2809 } | |
2810 | |
2811 case TOKdebug: | |
2812 nextToken(); | |
2813 condition = parseDebugCondition(); | |
2814 goto Lcondition; | |
2815 | |
2816 case TOKversion: | |
2817 nextToken(); | |
2818 condition = parseVersionCondition(); | |
2819 goto Lcondition; | |
2820 | |
2821 Lcondition: | |
2822 ifbody = parseStatement(0 /*PSsemi*/); | |
2823 elsebody = NULL; | |
2824 if (token.value == TOKelse) | |
2825 { | |
2826 nextToken(); | |
2827 elsebody = parseStatement(0 /*PSsemi*/); | |
2828 } | |
2829 s = new ConditionalStatement(loc, condition, ifbody, elsebody); | |
2830 break; | |
2831 | |
2832 case TOKpragma: | |
2833 { Identifier *ident; | |
2834 Expressions *args = NULL; | |
2835 Statement *body; | |
2836 | |
2837 nextToken(); | |
2838 check(TOKlparen); | |
2839 if (token.value != TOKidentifier) | |
2840 { error("pragma(identifier expected"); | |
2841 goto Lerror; | |
2842 } | |
2843 ident = token.ident; | |
2844 nextToken(); | |
2845 if (token.value == TOKcomma) | |
2846 args = parseArguments(); // pragma(identifier, args...); | |
2847 else | |
2848 check(TOKrparen); // pragma(identifier); | |
2849 if (token.value == TOKsemicolon) | |
2850 { nextToken(); | |
2851 body = NULL; | |
2852 } | |
2853 else | |
2854 body = parseStatement(PSsemi); | |
2855 s = new PragmaStatement(loc, ident, args, body); | |
2856 break; | |
2857 } | |
2858 | |
2859 case TOKswitch: | |
2860 { Expression *condition; | |
2861 Statement *body; | |
2862 | |
2863 nextToken(); | |
2864 check(TOKlparen); | |
2865 condition = parseExpression(); | |
2866 check(TOKrparen); | |
2867 body = parseStatement(PSscope); | |
2868 s = new SwitchStatement(loc, condition, body); | |
2869 break; | |
2870 } | |
2871 | |
2872 case TOKcase: | |
2873 { Expression *exp; | |
2874 Statements *statements; | |
2875 Array cases; // array of Expression's | |
2876 | |
2877 while (1) | |
2878 { | |
2879 nextToken(); | |
2880 exp = parseAssignExp(); | |
2881 cases.push(exp); | |
2882 if (token.value != TOKcomma) | |
2883 break; | |
2884 } | |
2885 check(TOKcolon); | |
2886 | |
2887 statements = new Statements(); | |
2888 while (token.value != TOKcase && | |
2889 token.value != TOKdefault && | |
2890 token.value != TOKrcurly) | |
2891 { | |
2892 statements->push(parseStatement(PSsemi | PScurlyscope)); | |
2893 } | |
2894 s = new CompoundStatement(loc, statements); | |
2895 s = new ScopeStatement(loc, s); | |
2896 | |
2897 // Keep cases in order by building the case statements backwards | |
2898 for (int i = cases.dim; i; i--) | |
2899 { | |
2900 exp = (Expression *)cases.data[i - 1]; | |
2901 s = new CaseStatement(loc, exp, s); | |
2902 } | |
2903 break; | |
2904 } | |
2905 | |
2906 case TOKdefault: | |
2907 { | |
2908 Statements *statements; | |
2909 | |
2910 nextToken(); | |
2911 check(TOKcolon); | |
2912 | |
2913 statements = new Statements(); | |
2914 while (token.value != TOKcase && | |
2915 token.value != TOKdefault && | |
2916 token.value != TOKrcurly) | |
2917 { | |
2918 statements->push(parseStatement(PSsemi | PScurlyscope)); | |
2919 } | |
2920 s = new CompoundStatement(loc, statements); | |
2921 s = new ScopeStatement(loc, s); | |
2922 s = new DefaultStatement(loc, s); | |
2923 break; | |
2924 } | |
2925 | |
2926 case TOKreturn: | |
2927 { Expression *exp; | |
2928 | |
2929 nextToken(); | |
2930 if (token.value == TOKsemicolon) | |
2931 exp = NULL; | |
2932 else | |
2933 exp = parseExpression(); | |
2934 check(TOKsemicolon, "return statement"); | |
2935 s = new ReturnStatement(loc, exp); | |
2936 break; | |
2937 } | |
2938 | |
2939 case TOKbreak: | |
2940 { Identifier *ident; | |
2941 | |
2942 nextToken(); | |
2943 if (token.value == TOKidentifier) | |
2944 { ident = token.ident; | |
2945 nextToken(); | |
2946 } | |
2947 else | |
2948 ident = NULL; | |
2949 check(TOKsemicolon, "break statement"); | |
2950 s = new BreakStatement(loc, ident); | |
2951 break; | |
2952 } | |
2953 | |
2954 case TOKcontinue: | |
2955 { Identifier *ident; | |
2956 | |
2957 nextToken(); | |
2958 if (token.value == TOKidentifier) | |
2959 { ident = token.ident; | |
2960 nextToken(); | |
2961 } | |
2962 else | |
2963 ident = NULL; | |
2964 check(TOKsemicolon, "continue statement"); | |
2965 s = new ContinueStatement(loc, ident); | |
2966 break; | |
2967 } | |
2968 | |
2969 case TOKgoto: | |
2970 { Identifier *ident; | |
2971 | |
2972 nextToken(); | |
2973 if (token.value == TOKdefault) | |
2974 { | |
2975 nextToken(); | |
2976 s = new GotoDefaultStatement(loc); | |
2977 } | |
2978 else if (token.value == TOKcase) | |
2979 { | |
2980 Expression *exp = NULL; | |
2981 | |
2982 nextToken(); | |
2983 if (token.value != TOKsemicolon) | |
2984 exp = parseExpression(); | |
2985 s = new GotoCaseStatement(loc, exp); | |
2986 } | |
2987 else | |
2988 { | |
2989 if (token.value != TOKidentifier) | |
2990 { error("Identifier expected following goto"); | |
2991 ident = NULL; | |
2992 } | |
2993 else | |
2994 { ident = token.ident; | |
2995 nextToken(); | |
2996 } | |
2997 s = new GotoStatement(loc, ident); | |
2998 } | |
2999 check(TOKsemicolon, "goto statement"); | |
3000 break; | |
3001 } | |
3002 | |
3003 case TOKsynchronized: | |
3004 { Expression *exp; | |
3005 Statement *body; | |
3006 | |
3007 nextToken(); | |
3008 if (token.value == TOKlparen) | |
3009 { | |
3010 nextToken(); | |
3011 exp = parseExpression(); | |
3012 check(TOKrparen); | |
3013 } | |
3014 else | |
3015 exp = NULL; | |
3016 body = parseStatement(PSscope); | |
3017 s = new SynchronizedStatement(loc, exp, body); | |
3018 break; | |
3019 } | |
3020 | |
3021 case TOKwith: | |
3022 { Expression *exp; | |
3023 Statement *body; | |
3024 | |
3025 nextToken(); | |
3026 check(TOKlparen); | |
3027 exp = parseExpression(); | |
3028 check(TOKrparen); | |
3029 body = parseStatement(PSscope); | |
3030 s = new WithStatement(loc, exp, body); | |
3031 break; | |
3032 } | |
3033 | |
3034 case TOKtry: | |
3035 { Statement *body; | |
3036 Array *catches = NULL; | |
3037 Statement *finalbody = NULL; | |
3038 | |
3039 nextToken(); | |
3040 body = parseStatement(PSscope); | |
3041 while (token.value == TOKcatch) | |
3042 { | |
3043 Statement *handler; | |
3044 Catch *c; | |
3045 Type *t; | |
3046 Identifier *id; | |
3047 Loc loc = this->loc; | |
3048 | |
3049 nextToken(); | |
3050 if (token.value == TOKlcurly) | |
3051 { | |
3052 t = NULL; | |
3053 id = NULL; | |
3054 } | |
3055 else | |
3056 { | |
3057 check(TOKlparen); | |
3058 t = parseBasicType(); | |
3059 id = NULL; | |
3060 t = parseDeclarator(t, &id); | |
3061 check(TOKrparen); | |
3062 } | |
3063 handler = parseStatement(0); | |
3064 c = new Catch(loc, t, id, handler); | |
3065 if (!catches) | |
3066 catches = new Array(); | |
3067 catches->push(c); | |
3068 } | |
3069 | |
3070 if (token.value == TOKfinally) | |
3071 { nextToken(); | |
3072 finalbody = parseStatement(0); | |
3073 } | |
3074 | |
3075 s = body; | |
3076 if (!catches && !finalbody) | |
3077 error("catch or finally expected following try"); | |
3078 else | |
3079 { if (catches) | |
3080 s = new TryCatchStatement(loc, body, catches); | |
3081 if (finalbody) | |
3082 s = new TryFinallyStatement(loc, s, finalbody); | |
3083 } | |
3084 break; | |
3085 } | |
3086 | |
3087 case TOKthrow: | |
3088 { Expression *exp; | |
3089 | |
3090 nextToken(); | |
3091 exp = parseExpression(); | |
3092 check(TOKsemicolon, "throw statement"); | |
3093 s = new ThrowStatement(loc, exp); | |
3094 break; | |
3095 } | |
3096 | |
3097 case TOKvolatile: | |
3098 nextToken(); | |
3099 s = parseStatement(PSsemi | PScurlyscope); | |
3100 s = new VolatileStatement(loc, s); | |
3101 break; | |
3102 | |
3103 case TOKasm: | |
3104 { Statements *statements; | |
3105 Identifier *label; | |
3106 Loc labelloc; | |
3107 Token *toklist; | |
3108 Token **ptoklist; | |
3109 | |
3110 // Parse the asm block into a sequence of AsmStatements, | |
3111 // each AsmStatement is one instruction. | |
3112 // Separate out labels. | |
3113 // Defer parsing of AsmStatements until semantic processing. | |
3114 | |
3115 nextToken(); | |
3116 check(TOKlcurly); | |
3117 toklist = NULL; | |
3118 ptoklist = &toklist; | |
3119 label = NULL; | |
3120 statements = new Statements(); | |
3121 while (1) | |
3122 { | |
3123 switch (token.value) | |
3124 { | |
3125 case TOKidentifier: | |
3126 if (!toklist) | |
3127 { | |
3128 // Look ahead to see if it is a label | |
3129 t = peek(&token); | |
3130 if (t->value == TOKcolon) | |
3131 { // It's a label | |
3132 label = token.ident; | |
3133 labelloc = this->loc; | |
3134 nextToken(); | |
3135 nextToken(); | |
3136 continue; | |
3137 } | |
3138 } | |
3139 goto Ldefault; | |
3140 | |
3141 case TOKrcurly: | |
3142 if (toklist || label) | |
3143 { | |
3144 error("asm statements must end in ';'"); | |
3145 } | |
3146 break; | |
3147 | |
3148 case TOKsemicolon: | |
3149 s = NULL; | |
3150 if (toklist || label) | |
3151 { // Create AsmStatement from list of tokens we've saved | |
3152 s = new AsmStatement(this->loc, toklist); | |
3153 toklist = NULL; | |
3154 ptoklist = &toklist; | |
3155 if (label) | |
3156 { s = new LabelStatement(labelloc, label, s); | |
3157 label = NULL; | |
3158 } | |
3159 statements->push(s); | |
3160 } | |
3161 nextToken(); | |
3162 continue; | |
3163 | |
3164 case TOKeof: | |
3165 /* { */ | |
3166 error("matching '}' expected, not end of file"); | |
3167 break; | |
3168 | |
3169 default: | |
3170 Ldefault: | |
3171 *ptoklist = new Token(); | |
3172 memcpy(*ptoklist, &token, sizeof(Token)); | |
3173 ptoklist = &(*ptoklist)->next; | |
3174 *ptoklist = NULL; | |
3175 | |
3176 nextToken(); | |
3177 continue; | |
3178 } | |
3179 break; | |
3180 } | |
3181 s = new CompoundStatement(loc, statements); | |
3182 nextToken(); | |
3183 break; | |
3184 } | |
3185 | |
3186 default: | |
3187 error("found '%s' instead of statement", token.toChars()); | |
3188 goto Lerror; | |
3189 | |
3190 Lerror: | |
3191 while (token.value != TOKrcurly && | |
3192 token.value != TOKsemicolon && | |
3193 token.value != TOKeof) | |
3194 nextToken(); | |
3195 if (token.value == TOKsemicolon) | |
3196 nextToken(); | |
3197 s = NULL; | |
3198 break; | |
3199 } | |
3200 | |
3201 return s; | |
3202 } | |
3203 | |
3204 void Parser::check(enum TOK value) | |
3205 { | |
3206 check(loc, value); | |
3207 } | |
3208 | |
3209 void Parser::check(Loc loc, enum TOK value) | |
3210 { | |
3211 if (token.value != value) | |
3212 error(loc, "found '%s' when expecting '%s'", token.toChars(), Token::toChars(value)); | |
3213 nextToken(); | |
3214 } | |
3215 | |
3216 void Parser::check(enum TOK value, char *string) | |
3217 { | |
3218 if (token.value != value) | |
3219 error("found '%s' when expecting '%s' following '%s'", | |
3220 token.toChars(), Token::toChars(value), string); | |
3221 nextToken(); | |
3222 } | |
3223 | |
3224 /************************************ | |
3225 * Determine if the scanner is sitting on the start of a declaration. | |
3226 * Input: | |
3227 * needId 0 no identifier | |
3228 * 1 identifier optional | |
3229 * 2 must have identifier | |
3230 */ | |
3231 | |
3232 int Parser::isDeclaration(Token *t, int needId, enum TOK endtok, Token **pt) | |
3233 { | |
3234 int haveId = 0; | |
3235 | |
3236 if (!isBasicType(&t)) | |
3237 return FALSE; | |
3238 if (!isDeclarator(&t, &haveId, endtok)) | |
3239 return FALSE; | |
3240 if ( needId == 1 || | |
3241 (needId == 0 && !haveId) || | |
3242 (needId == 2 && haveId)) | |
3243 { if (pt) | |
3244 *pt = t; | |
3245 return TRUE; | |
3246 } | |
3247 else | |
3248 return FALSE; | |
3249 } | |
3250 | |
3251 int Parser::isBasicType(Token **pt) | |
3252 { | |
3253 // This code parallels parseBasicType() | |
3254 Token *t = *pt; | |
3255 Token *t2; | |
3256 int parens; | |
3257 | |
3258 switch (t->value) | |
3259 { | |
3260 CASE_BASIC_TYPES: | |
3261 t = peek(t); | |
3262 break; | |
3263 | |
3264 case TOKidentifier: | |
3265 t = peek(t); | |
3266 if (t->value == TOKnot) | |
3267 { | |
3268 goto L4; | |
3269 } | |
3270 goto L3; | |
3271 while (1) | |
3272 { | |
3273 L2: | |
3274 t = peek(t); | |
3275 L3: | |
3276 if (t->value == TOKdot) | |
3277 { | |
3278 Ldot: | |
3279 t = peek(t); | |
3280 if (t->value != TOKidentifier) | |
3281 goto Lfalse; | |
3282 t = peek(t); | |
3283 if (t->value != TOKnot) | |
3284 goto L3; | |
3285 L4: | |
3286 t = peek(t); | |
3287 if (t->value != TOKlparen) | |
3288 goto Lfalse; | |
3289 if (!skipParens(t, &t)) | |
3290 goto Lfalse; | |
3291 } | |
3292 else | |
3293 break; | |
3294 } | |
3295 break; | |
3296 | |
3297 case TOKdot: | |
3298 goto Ldot; | |
3299 | |
3300 case TOKtypeof: | |
3301 /* typeof(exp).identifier... | |
3302 */ | |
3303 t = peek(t); | |
3304 if (t->value != TOKlparen) | |
3305 goto Lfalse; | |
3306 if (!skipParens(t, &t)) | |
3307 goto Lfalse; | |
3308 goto L2; | |
3309 | |
3310 default: | |
3311 goto Lfalse; | |
3312 } | |
3313 *pt = t; | |
3314 return TRUE; | |
3315 | |
3316 Lfalse: | |
3317 return FALSE; | |
3318 } | |
3319 | |
3320 int Parser::isDeclarator(Token **pt, int *haveId, enum TOK endtok) | |
3321 { // This code parallels parseDeclarator() | |
3322 Token *t = *pt; | |
3323 int parens; | |
3324 | |
3325 //printf("Parser::isDeclarator()\n"); | |
3326 //t->print(); | |
3327 if (t->value == TOKassign) | |
3328 return FALSE; | |
3329 | |
3330 while (1) | |
3331 { | |
3332 parens = FALSE; | |
3333 switch (t->value) | |
3334 { | |
3335 case TOKmul: | |
3336 case TOKand: | |
3337 t = peek(t); | |
3338 continue; | |
3339 | |
3340 case TOKlbracket: | |
3341 t = peek(t); | |
3342 if (t->value == TOKrbracket) | |
3343 { | |
3344 t = peek(t); | |
3345 } | |
3346 else if (isDeclaration(t, 0, TOKrbracket, &t)) | |
3347 { // It's an associative array declaration | |
3348 t = peek(t); | |
3349 } | |
3350 else | |
3351 { | |
3352 // [ expression ] | |
3353 // [ expression .. expression ] | |
3354 if (!isExpression(&t)) | |
3355 return FALSE; | |
3356 if (t->value == TOKslice) | |
3357 { t = peek(t); | |
3358 if (!isExpression(&t)) | |
3359 return FALSE; | |
3360 } | |
3361 if (t->value != TOKrbracket) | |
3362 return FALSE; | |
3363 t = peek(t); | |
3364 } | |
3365 continue; | |
3366 | |
3367 case TOKidentifier: | |
3368 if (*haveId) | |
3369 return FALSE; | |
3370 *haveId = TRUE; | |
3371 t = peek(t); | |
3372 break; | |
3373 | |
3374 case TOKlparen: | |
3375 t = peek(t); | |
3376 | |
3377 if (t->value == TOKrparen) | |
3378 return FALSE; // () is not a declarator | |
3379 | |
3380 /* Regard ( identifier ) as not a declarator | |
3381 * BUG: what about ( *identifier ) in | |
3382 * f(*p)(x); | |
3383 * where f is a class instance with overloaded () ? | |
3384 * Should we just disallow C-style function pointer declarations? | |
3385 */ | |
3386 if (t->value == TOKidentifier) | |
3387 { Token *t2 = peek(t); | |
3388 if (t2->value == TOKrparen) | |
3389 return FALSE; | |
3390 } | |
3391 | |
3392 | |
3393 if (!isDeclarator(&t, haveId, TOKrparen)) | |
3394 return FALSE; | |
3395 t = peek(t); | |
3396 parens = TRUE; | |
3397 break; | |
3398 | |
3399 case TOKdelegate: | |
3400 case TOKfunction: | |
3401 t = peek(t); | |
3402 if (!isParameters(&t)) | |
3403 return FALSE; | |
3404 continue; | |
3405 } | |
3406 break; | |
3407 } | |
3408 | |
3409 while (1) | |
3410 { | |
3411 switch (t->value) | |
3412 { | |
3413 #if CARRAYDECL | |
3414 case TOKlbracket: | |
3415 parens = FALSE; | |
3416 t = peek(t); | |
3417 if (t->value == TOKrbracket) | |
3418 { | |
3419 t = peek(t); | |
3420 } | |
3421 else if (isDeclaration(t, 0, TOKrbracket, &t)) | |
3422 { // It's an associative array declaration | |
3423 t = peek(t); | |
3424 } | |
3425 else | |
3426 { | |
3427 // [ expression ] | |
3428 if (!isExpression(&t)) | |
3429 return FALSE; | |
3430 if (t->value != TOKrbracket) | |
3431 return FALSE; | |
3432 t = peek(t); | |
3433 } | |
3434 continue; | |
3435 #endif | |
3436 | |
3437 case TOKlparen: | |
3438 parens = FALSE; | |
3439 if (!isParameters(&t)) | |
3440 return FALSE; | |
3441 continue; | |
3442 | |
3443 // Valid tokens that follow a declaration | |
3444 case TOKrparen: | |
3445 case TOKrbracket: | |
3446 case TOKassign: | |
3447 case TOKcomma: | |
3448 case TOKsemicolon: | |
3449 case TOKlcurly: | |
3450 case TOKin: | |
3451 // The !parens is to disallow unnecessary parentheses | |
3452 if (!parens && (endtok == TOKreserved || endtok == t->value)) | |
3453 { *pt = t; | |
3454 return TRUE; | |
3455 } | |
3456 return FALSE; | |
3457 | |
3458 default: | |
3459 return FALSE; | |
3460 } | |
3461 } | |
3462 } | |
3463 | |
3464 | |
3465 int Parser::isParameters(Token **pt) | |
3466 { // This code parallels parseParameters() | |
3467 Token *t = *pt; | |
3468 int tmp; | |
3469 | |
3470 //printf("isParameters()\n"); | |
3471 if (t->value != TOKlparen) | |
3472 return FALSE; | |
3473 | |
3474 t = peek(t); | |
3475 while (1) | |
3476 { | |
3477 switch (t->value) | |
3478 { | |
3479 case TOKrparen: | |
3480 break; | |
3481 | |
3482 case TOKdotdotdot: | |
3483 t = peek(t); | |
3484 break; | |
3485 | |
3486 case TOKin: | |
3487 case TOKout: | |
3488 case TOKinout: | |
3489 case TOKref: | |
3490 case TOKlazy: | |
3491 t = peek(t); | |
3492 default: | |
3493 if (!isBasicType(&t)) | |
3494 return FALSE; | |
3495 tmp = FALSE; | |
3496 if (t->value != TOKdotdotdot && | |
3497 !isDeclarator(&t, &tmp, TOKreserved)) | |
3498 return FALSE; | |
3499 if (t->value == TOKassign) | |
3500 { t = peek(t); | |
3501 if (!isExpression(&t)) | |
3502 return FALSE; | |
3503 } | |
3504 if (t->value == TOKdotdotdot) | |
3505 { | |
3506 t = peek(t); | |
3507 break; | |
3508 } | |
3509 if (t->value == TOKcomma) | |
3510 { t = peek(t); | |
3511 continue; | |
3512 } | |
3513 break; | |
3514 } | |
3515 break; | |
3516 } | |
3517 if (t->value != TOKrparen) | |
3518 return FALSE; | |
3519 t = peek(t); | |
3520 *pt = t; | |
3521 return TRUE; | |
3522 } | |
3523 | |
3524 int Parser::isExpression(Token **pt) | |
3525 { | |
3526 // This is supposed to determine if something is an expression. | |
3527 // What it actually does is scan until a closing right bracket | |
3528 // is found. | |
3529 | |
3530 Token *t = *pt; | |
3531 int brnest = 0; | |
3532 int panest = 0; | |
3533 | |
3534 for (;; t = peek(t)) | |
3535 { | |
3536 switch (t->value) | |
3537 { | |
3538 case TOKlbracket: | |
3539 brnest++; | |
3540 continue; | |
3541 | |
3542 case TOKrbracket: | |
3543 if (--brnest >= 0) | |
3544 continue; | |
3545 break; | |
3546 | |
3547 case TOKlparen: | |
3548 panest++; | |
3549 continue; | |
3550 | |
3551 case TOKcomma: | |
3552 if (brnest || panest) | |
3553 continue; | |
3554 break; | |
3555 | |
3556 case TOKrparen: | |
3557 if (--panest >= 0) | |
3558 continue; | |
3559 break; | |
3560 | |
3561 case TOKslice: | |
3562 if (brnest) | |
3563 continue; | |
3564 break; | |
3565 | |
3566 case TOKeof: | |
3567 return FALSE; | |
3568 | |
3569 default: | |
3570 continue; | |
3571 } | |
3572 break; | |
3573 } | |
3574 | |
3575 *pt = t; | |
3576 return TRUE; | |
3577 } | |
3578 | |
3579 /********************************************** | |
3580 * Skip over | |
3581 * instance foo.bar(parameters...) | |
3582 * Output: | |
3583 * if (pt), *pt is set to the token following the closing ) | |
3584 * Returns: | |
3585 * 1 it's valid instance syntax | |
3586 * 0 invalid instance syntax | |
3587 */ | |
3588 | |
3589 int Parser::isTemplateInstance(Token *t, Token **pt) | |
3590 { | |
3591 t = peek(t); | |
3592 if (t->value != TOKdot) | |
3593 { | |
3594 if (t->value != TOKidentifier) | |
3595 goto Lfalse; | |
3596 t = peek(t); | |
3597 } | |
3598 while (t->value == TOKdot) | |
3599 { | |
3600 t = peek(t); | |
3601 if (t->value != TOKidentifier) | |
3602 goto Lfalse; | |
3603 t = peek(t); | |
3604 } | |
3605 if (t->value != TOKlparen) | |
3606 goto Lfalse; | |
3607 | |
3608 // Skip over the template arguments | |
3609 while (1) | |
3610 { | |
3611 while (1) | |
3612 { | |
3613 t = peek(t); | |
3614 switch (t->value) | |
3615 { | |
3616 case TOKlparen: | |
3617 if (!skipParens(t, &t)) | |
3618 goto Lfalse; | |
3619 continue; | |
3620 case TOKrparen: | |
3621 break; | |
3622 case TOKcomma: | |
3623 break; | |
3624 case TOKeof: | |
3625 case TOKsemicolon: | |
3626 goto Lfalse; | |
3627 default: | |
3628 continue; | |
3629 } | |
3630 break; | |
3631 } | |
3632 | |
3633 if (t->value != TOKcomma) | |
3634 break; | |
3635 } | |
3636 if (t->value != TOKrparen) | |
3637 goto Lfalse; | |
3638 t = peek(t); | |
3639 if (pt) | |
3640 *pt = t; | |
3641 return 1; | |
3642 | |
3643 Lfalse: | |
3644 return 0; | |
3645 } | |
3646 | |
3647 /******************************************* | |
3648 * Skip parens, brackets. | |
3649 * Input: | |
3650 * t is on opening ( | |
3651 * Output: | |
3652 * *pt is set to closing token, which is ')' on success | |
3653 * Returns: | |
3654 * !=0 successful | |
3655 * 0 some parsing error | |
3656 */ | |
3657 | |
3658 int Parser::skipParens(Token *t, Token **pt) | |
3659 { | |
3660 int parens = 0; | |
3661 | |
3662 while (1) | |
3663 { | |
3664 switch (t->value) | |
3665 { | |
3666 case TOKlparen: | |
3667 parens++; | |
3668 break; | |
3669 | |
3670 case TOKrparen: | |
3671 parens--; | |
3672 if (parens < 0) | |
3673 goto Lfalse; | |
3674 if (parens == 0) | |
3675 goto Ldone; | |
3676 break; | |
3677 | |
3678 case TOKeof: | |
3679 case TOKsemicolon: | |
3680 goto Lfalse; | |
3681 | |
3682 default: | |
3683 break; | |
3684 } | |
3685 t = peek(t); | |
3686 } | |
3687 | |
3688 Ldone: | |
3689 if (*pt) | |
3690 *pt = t; | |
3691 return 1; | |
3692 | |
3693 Lfalse: | |
3694 return 0; | |
3695 } | |
3696 | |
3697 /********************************* Expression Parser ***************************/ | |
3698 | |
3699 Expression *Parser::parsePrimaryExp() | |
3700 { Expression *e; | |
3701 Type *t; | |
3702 Identifier *id; | |
3703 enum TOK save; | |
3704 Loc loc = this->loc; | |
3705 | |
3706 switch (token.value) | |
3707 { | |
3708 case TOKidentifier: | |
3709 id = token.ident; | |
3710 nextToken(); | |
3711 if (token.value == TOKnot && peek(&token)->value == TOKlparen) | |
3712 { // identifier!(template-argument-list) | |
3713 TemplateInstance *tempinst; | |
3714 | |
3715 tempinst = new TemplateInstance(loc, id); | |
3716 nextToken(); | |
3717 tempinst->tiargs = parseTemplateArgumentList(); | |
3718 e = new ScopeExp(loc, tempinst); | |
3719 } | |
3720 else | |
3721 e = new IdentifierExp(loc, id); | |
3722 break; | |
3723 | |
3724 case TOKdollar: | |
3725 if (!inBrackets) | |
3726 error("'$' is valid only inside [] of index or slice"); | |
3727 e = new DollarExp(loc); | |
3728 nextToken(); | |
3729 break; | |
3730 | |
3731 case TOKdot: | |
3732 // Signal global scope '.' operator with "" identifier | |
3733 e = new IdentifierExp(loc, Id::empty); | |
3734 break; | |
3735 | |
3736 case TOKthis: | |
3737 e = new ThisExp(loc); | |
3738 nextToken(); | |
3739 break; | |
3740 | |
3741 case TOKsuper: | |
3742 e = new SuperExp(loc); | |
3743 nextToken(); | |
3744 break; | |
3745 | |
3746 case TOKint32v: | |
3747 e = new IntegerExp(loc, token.int32value, Type::tint32); | |
3748 nextToken(); | |
3749 break; | |
3750 | |
3751 case TOKuns32v: | |
3752 e = new IntegerExp(loc, token.uns32value, Type::tuns32); | |
3753 nextToken(); | |
3754 break; | |
3755 | |
3756 case TOKint64v: | |
3757 e = new IntegerExp(loc, token.int64value, Type::tint64); | |
3758 nextToken(); | |
3759 break; | |
3760 | |
3761 case TOKuns64v: | |
3762 e = new IntegerExp(loc, token.uns64value, Type::tuns64); | |
3763 nextToken(); | |
3764 break; | |
3765 | |
3766 case TOKfloat32v: | |
3767 e = new RealExp(loc, token.float80value, Type::tfloat32); | |
3768 nextToken(); | |
3769 break; | |
3770 | |
3771 case TOKfloat64v: | |
3772 e = new RealExp(loc, token.float80value, Type::tfloat64); | |
3773 nextToken(); | |
3774 break; | |
3775 | |
3776 case TOKfloat80v: | |
3777 e = new RealExp(loc, token.float80value, Type::tfloat80); | |
3778 nextToken(); | |
3779 break; | |
3780 | |
3781 case TOKimaginary32v: | |
3782 e = new RealExp(loc, token.float80value, Type::timaginary32); | |
3783 nextToken(); | |
3784 break; | |
3785 | |
3786 case TOKimaginary64v: | |
3787 e = new RealExp(loc, token.float80value, Type::timaginary64); | |
3788 nextToken(); | |
3789 break; | |
3790 | |
3791 case TOKimaginary80v: | |
3792 e = new RealExp(loc, token.float80value, Type::timaginary80); | |
3793 nextToken(); | |
3794 break; | |
3795 | |
3796 case TOKnull: | |
3797 e = new NullExp(loc); | |
3798 nextToken(); | |
3799 break; | |
3800 | |
3801 case TOKtrue: | |
3802 e = new IntegerExp(loc, 1, Type::tbool); | |
3803 nextToken(); | |
3804 break; | |
3805 | |
3806 case TOKfalse: | |
3807 e = new IntegerExp(loc, 0, Type::tbool); | |
3808 nextToken(); | |
3809 break; | |
3810 | |
3811 case TOKcharv: | |
3812 e = new IntegerExp(loc, token.uns32value, Type::tchar); | |
3813 nextToken(); | |
3814 break; | |
3815 | |
3816 case TOKwcharv: | |
3817 e = new IntegerExp(loc, token.uns32value, Type::twchar); | |
3818 nextToken(); | |
3819 break; | |
3820 | |
3821 case TOKdcharv: | |
3822 e = new IntegerExp(loc, token.uns32value, Type::tdchar); | |
3823 nextToken(); | |
3824 break; | |
3825 | |
3826 case TOKstring: | |
3827 { unsigned char *s; | |
3828 unsigned len; | |
3829 unsigned char postfix; | |
3830 | |
3831 // cat adjacent strings | |
3832 s = token.ustring; | |
3833 len = token.len; | |
3834 postfix = token.postfix; | |
3835 while (1) | |
3836 { | |
3837 nextToken(); | |
3838 if (token.value == TOKstring) | |
3839 { unsigned len1; | |
3840 unsigned len2; | |
3841 unsigned char *s2; | |
3842 | |
3843 if (token.postfix) | |
3844 { if (token.postfix != postfix) | |
3845 error("mismatched string literal postfixes '%c' and '%c'", postfix, token.postfix); | |
3846 postfix = token.postfix; | |
3847 } | |
3848 | |
3849 len1 = len; | |
3850 len2 = token.len; | |
3851 len = len1 + len2; | |
3852 s2 = (unsigned char *)mem.malloc((len + 1) * sizeof(unsigned char)); | |
3853 memcpy(s2, s, len1 * sizeof(unsigned char)); | |
3854 memcpy(s2 + len1, token.ustring, (len2 + 1) * sizeof(unsigned char)); | |
3855 s = s2; | |
3856 } | |
3857 else | |
3858 break; | |
3859 } | |
3860 e = new StringExp(loc, s, len, postfix); | |
3861 break; | |
3862 } | |
3863 | |
3864 CASE_BASIC_TYPES_X(t): | |
3865 nextToken(); | |
3866 L1: | |
3867 check(TOKdot, t->toChars()); | |
3868 if (token.value != TOKidentifier) | |
3869 { error("found '%s' when expecting identifier following '%s.'", token.toChars(), t->toChars()); | |
3870 goto Lerr; | |
3871 } | |
3872 e = new TypeDotIdExp(loc, t, token.ident); | |
3873 nextToken(); | |
3874 break; | |
3875 | |
3876 case TOKtypeof: | |
3877 { Expression *exp; | |
3878 | |
3879 nextToken(); | |
3880 check(TOKlparen); | |
3881 exp = parseExpression(); | |
3882 check(TOKrparen); | |
3883 t = new TypeTypeof(loc, exp); | |
3884 if (token.value == TOKdot) | |
3885 goto L1; | |
3886 e = new TypeExp(loc, t); | |
3887 break; | |
3888 } | |
3889 | |
3890 case TOKtypeid: | |
3891 { Type *t; | |
3892 | |
3893 nextToken(); | |
3894 check(TOKlparen, "typeid"); | |
3895 t = parseBasicType(); | |
3896 t = parseDeclarator(t,NULL); // ( type ) | |
3897 check(TOKrparen); | |
3898 e = new TypeidExp(loc, t); | |
3899 break; | |
3900 } | |
3901 | |
3902 case TOKis: | |
3903 { Type *targ; | |
3904 Identifier *ident = NULL; | |
3905 Type *tspec = NULL; | |
3906 enum TOK tok = TOKreserved; | |
3907 enum TOK tok2 = TOKreserved; | |
3908 Loc loc = this->loc; | |
3909 | |
3910 nextToken(); | |
3911 if (token.value == TOKlparen) | |
3912 { | |
3913 nextToken(); | |
3914 targ = parseBasicType(); | |
3915 targ = parseDeclarator(targ, &ident); | |
3916 if (token.value == TOKcolon || token.value == TOKequal) | |
3917 { | |
3918 tok = token.value; | |
3919 nextToken(); | |
3920 if (tok == TOKequal && | |
3921 (token.value == TOKtypedef || | |
3922 token.value == TOKstruct || | |
3923 token.value == TOKunion || | |
3924 token.value == TOKclass || | |
3925 token.value == TOKsuper || | |
3926 token.value == TOKenum || | |
3927 token.value == TOKinterface || | |
3928 token.value == TOKfunction || | |
3929 token.value == TOKdelegate || | |
3930 token.value == TOKreturn)) | |
3931 { | |
3932 tok2 = token.value; | |
3933 nextToken(); | |
3934 } | |
3935 else | |
3936 { | |
3937 tspec = parseBasicType(); | |
3938 tspec = parseDeclarator(tspec, NULL); | |
3939 } | |
3940 } | |
3941 check(TOKrparen); | |
3942 } | |
3943 else | |
3944 { error("(type identifier : specialization) expected following is"); | |
3945 goto Lerr; | |
3946 } | |
19 | 3947 e = new IsExp(loc, targ, ident, tok, tspec, tok2); |
1 | 3948 break; |
3949 } | |
3950 | |
3951 case TOKassert: | |
3952 { Expression *msg = NULL; | |
3953 | |
3954 nextToken(); | |
3955 check(TOKlparen, "assert"); | |
3956 e = parseAssignExp(); | |
3957 if (token.value == TOKcomma) | |
3958 { nextToken(); | |
3959 msg = parseAssignExp(); | |
3960 } | |
3961 check(TOKrparen); | |
3962 e = new AssertExp(loc, e, msg); | |
3963 break; | |
3964 } | |
3965 | |
3966 case TOKmixin: | |
3967 { | |
3968 nextToken(); | |
3969 check(TOKlparen, "mixin"); | |
3970 e = parseAssignExp(); | |
3971 check(TOKrparen); | |
3972 e = new CompileExp(loc, e); | |
3973 break; | |
3974 } | |
3975 | |
3976 case TOKimport: | |
3977 { | |
3978 nextToken(); | |
3979 check(TOKlparen, "import"); | |
3980 e = parseAssignExp(); | |
3981 check(TOKrparen); | |
3982 e = new FileExp(loc, e); | |
3983 break; | |
3984 } | |
3985 | |
3986 case TOKlparen: | |
3987 if (peekPastParen(&token)->value == TOKlcurly) | |
3988 { // (arguments) { statements... } | |
3989 save = TOKdelegate; | |
3990 goto case_delegate; | |
3991 } | |
3992 // ( expression ) | |
3993 nextToken(); | |
3994 e = parseExpression(); | |
3995 check(loc, TOKrparen); | |
3996 break; | |
3997 | |
3998 case TOKlbracket: | |
3999 { /* Parse array literals and associative array literals: | |
4000 * [ value, value, value ... ] | |
4001 * [ key:value, key:value, key:value ... ] | |
4002 */ | |
4003 Expressions *values = new Expressions(); | |
4004 Expressions *keys = NULL; | |
4005 | |
4006 nextToken(); | |
4007 if (token.value != TOKrbracket) | |
4008 { | |
4009 while (1) | |
4010 { | |
4011 Expression *e = parseAssignExp(); | |
4012 if (token.value == TOKcolon && (keys || values->dim == 0)) | |
4013 { nextToken(); | |
4014 if (!keys) | |
4015 keys = new Expressions(); | |
4016 keys->push(e); | |
4017 e = parseAssignExp(); | |
4018 } | |
4019 else if (keys) | |
4020 { error("'key:value' expected for associative array literal"); | |
4021 delete keys; | |
4022 keys = NULL; | |
4023 } | |
4024 values->push(e); | |
4025 if (token.value == TOKrbracket) | |
4026 break; | |
4027 check(TOKcomma); | |
4028 } | |
4029 } | |
4030 check(TOKrbracket); | |
4031 | |
4032 if (keys) | |
4033 e = new AssocArrayLiteralExp(loc, keys, values); | |
4034 else | |
4035 e = new ArrayLiteralExp(loc, values); | |
4036 break; | |
4037 } | |
4038 | |
4039 case TOKlcurly: | |
4040 // { statements... } | |
4041 save = TOKdelegate; | |
4042 goto case_delegate; | |
4043 | |
4044 case TOKfunction: | |
4045 case TOKdelegate: | |
4046 save = token.value; | |
4047 nextToken(); | |
4048 case_delegate: | |
4049 { | |
4050 /* function type(parameters) { body } | |
4051 * delegate type(parameters) { body } | |
4052 */ | |
4053 Arguments *arguments; | |
4054 int varargs; | |
4055 FuncLiteralDeclaration *fd; | |
4056 Type *t; | |
4057 | |
4058 if (token.value == TOKlcurly) | |
4059 { | |
4060 t = NULL; | |
4061 varargs = 0; | |
4062 arguments = new Arguments(); | |
4063 } | |
4064 else | |
4065 { | |
4066 if (token.value == TOKlparen) | |
4067 t = NULL; | |
4068 else | |
4069 { | |
4070 t = parseBasicType(); | |
4071 t = parseBasicType2(t); // function return type | |
4072 } | |
4073 arguments = parseParameters(&varargs); | |
4074 } | |
4075 t = new TypeFunction(arguments, t, varargs, linkage); | |
4076 fd = new FuncLiteralDeclaration(loc, 0, t, save, NULL); | |
4077 parseContracts(fd); | |
4078 e = new FuncExp(loc, fd); | |
4079 break; | |
4080 } | |
4081 | |
4082 default: | |
4083 error("expression expected, not '%s'", token.toChars()); | |
4084 Lerr: | |
4085 // Anything for e, as long as it's not NULL | |
4086 e = new IntegerExp(loc, 0, Type::tint32); | |
4087 nextToken(); | |
4088 break; | |
4089 } | |
4090 return parsePostExp(e); | |
4091 } | |
4092 | |
4093 Expression *Parser::parsePostExp(Expression *e) | |
4094 { | |
4095 Loc loc; | |
4096 | |
4097 while (1) | |
4098 { | |
4099 loc = this->loc; | |
4100 switch (token.value) | |
4101 { | |
4102 case TOKdot: | |
4103 nextToken(); | |
4104 if (token.value == TOKidentifier) | |
4105 { Identifier *id = token.ident; | |
4106 | |
4107 nextToken(); | |
4108 if (token.value == TOKnot && peek(&token)->value == TOKlparen) | |
4109 { // identifier!(template-argument-list) | |
4110 TemplateInstance *tempinst; | |
4111 | |
4112 tempinst = new TemplateInstance(loc, id); | |
4113 nextToken(); | |
4114 tempinst->tiargs = parseTemplateArgumentList(); | |
4115 e = new DotTemplateInstanceExp(loc, e, tempinst); | |
4116 } | |
4117 else | |
4118 e = new DotIdExp(loc, e, id); | |
4119 continue; | |
4120 } | |
4121 else if (token.value == TOKnew) | |
4122 { | |
4123 e = parseNewExp(e); | |
4124 continue; | |
4125 } | |
4126 else | |
4127 error("identifier expected following '.', not '%s'", token.toChars()); | |
4128 break; | |
4129 | |
4130 case TOKplusplus: | |
4131 e = new PostExp(TOKplusplus, loc, e); | |
4132 break; | |
4133 | |
4134 case TOKminusminus: | |
4135 e = new PostExp(TOKminusminus, loc, e); | |
4136 break; | |
4137 | |
4138 case TOKlparen: | |
4139 e = new CallExp(loc, e, parseArguments()); | |
4140 continue; | |
4141 | |
4142 case TOKlbracket: | |
4143 { // array dereferences: | |
4144 // array[index] | |
4145 // array[] | |
4146 // array[lwr .. upr] | |
4147 Expression *index; | |
4148 Expression *upr; | |
4149 | |
4150 inBrackets++; | |
4151 nextToken(); | |
4152 if (token.value == TOKrbracket) | |
4153 { // array[] | |
4154 e = new SliceExp(loc, e, NULL, NULL); | |
4155 nextToken(); | |
4156 } | |
4157 else | |
4158 { | |
4159 index = parseAssignExp(); | |
4160 if (token.value == TOKslice) | |
4161 { // array[lwr .. upr] | |
4162 nextToken(); | |
4163 upr = parseAssignExp(); | |
4164 e = new SliceExp(loc, e, index, upr); | |
4165 } | |
4166 else | |
4167 { // array[index, i2, i3, i4, ...] | |
4168 Expressions *arguments = new Expressions(); | |
4169 arguments->push(index); | |
4170 if (token.value == TOKcomma) | |
4171 { | |
4172 nextToken(); | |
4173 while (1) | |
4174 { Expression *arg; | |
4175 | |
4176 arg = parseAssignExp(); | |
4177 arguments->push(arg); | |
4178 if (token.value == TOKrbracket) | |
4179 break; | |
4180 check(TOKcomma); | |
4181 } | |
4182 } | |
4183 e = new ArrayExp(loc, e, arguments); | |
4184 } | |
4185 check(TOKrbracket); | |
4186 inBrackets--; | |
4187 } | |
4188 continue; | |
4189 } | |
4190 | |
4191 default: | |
4192 return e; | |
4193 } | |
4194 nextToken(); | |
4195 } | |
4196 } | |
4197 | |
4198 Expression *Parser::parseUnaryExp() | |
4199 { Expression *e; | |
4200 Loc loc = this->loc; | |
4201 | |
4202 switch (token.value) | |
4203 { | |
4204 case TOKand: | |
4205 nextToken(); | |
4206 e = parseUnaryExp(); | |
4207 e = new AddrExp(loc, e); | |
4208 break; | |
4209 | |
4210 case TOKplusplus: | |
4211 nextToken(); | |
4212 e = parseUnaryExp(); | |
4213 e = new AddAssignExp(loc, e, new IntegerExp(loc, 1, Type::tint32)); | |
4214 break; | |
4215 | |
4216 case TOKminusminus: | |
4217 nextToken(); | |
4218 e = parseUnaryExp(); | |
4219 e = new MinAssignExp(loc, e, new IntegerExp(loc, 1, Type::tint32)); | |
4220 break; | |
4221 | |
4222 case TOKmul: | |
4223 nextToken(); | |
4224 e = parseUnaryExp(); | |
4225 e = new PtrExp(loc, e); | |
4226 break; | |
4227 | |
4228 case TOKmin: | |
4229 nextToken(); | |
4230 e = parseUnaryExp(); | |
4231 e = new NegExp(loc, e); | |
4232 break; | |
4233 | |
4234 case TOKadd: | |
4235 nextToken(); | |
4236 e = parseUnaryExp(); | |
4237 e = new UAddExp(loc, e); | |
4238 break; | |
4239 | |
4240 case TOKnot: | |
4241 nextToken(); | |
4242 e = parseUnaryExp(); | |
4243 e = new NotExp(loc, e); | |
4244 break; | |
4245 | |
4246 case TOKtilde: | |
4247 nextToken(); | |
4248 e = parseUnaryExp(); | |
4249 e = new ComExp(loc, e); | |
4250 break; | |
4251 | |
4252 case TOKdelete: | |
4253 nextToken(); | |
4254 e = parseUnaryExp(); | |
4255 e = new DeleteExp(loc, e); | |
4256 break; | |
4257 | |
4258 case TOKnew: | |
4259 e = parseNewExp(NULL); | |
4260 break; | |
4261 | |
4262 case TOKcast: // cast(type) expression | |
4263 { Type *t; | |
4264 | |
4265 nextToken(); | |
4266 check(TOKlparen); | |
4267 t = parseBasicType(); | |
4268 t = parseDeclarator(t,NULL); // ( type ) | |
4269 check(TOKrparen); | |
4270 | |
4271 e = parseUnaryExp(); | |
4272 e = new CastExp(loc, e, t); | |
4273 break; | |
4274 } | |
4275 | |
4276 case TOKlparen: | |
4277 { Token *tk; | |
4278 | |
4279 tk = peek(&token); | |
4280 #if CCASTSYNTAX | |
4281 // If cast | |
4282 if (isDeclaration(tk, 0, TOKrparen, &tk)) | |
4283 { | |
4284 tk = peek(tk); // skip over right parenthesis | |
4285 switch (tk->value) | |
4286 { | |
4287 case TOKdot: | |
4288 case TOKplusplus: | |
4289 case TOKminusminus: | |
4290 case TOKnot: | |
4291 case TOKdelete: | |
4292 case TOKnew: | |
4293 case TOKlparen: | |
4294 case TOKidentifier: | |
4295 case TOKthis: | |
4296 case TOKsuper: | |
4297 case TOKint32v: | |
4298 case TOKuns32v: | |
4299 case TOKint64v: | |
4300 case TOKuns64v: | |
4301 case TOKfloat32v: | |
4302 case TOKfloat64v: | |
4303 case TOKfloat80v: | |
4304 case TOKimaginary32v: | |
4305 case TOKimaginary64v: | |
4306 case TOKimaginary80v: | |
4307 case TOKnull: | |
4308 case TOKtrue: | |
4309 case TOKfalse: | |
4310 case TOKcharv: | |
4311 case TOKwcharv: | |
4312 case TOKdcharv: | |
4313 case TOKstring: | |
4314 #if 0 | |
4315 case TOKtilde: | |
4316 case TOKand: | |
4317 case TOKmul: | |
4318 case TOKmin: | |
4319 case TOKadd: | |
4320 #endif | |
4321 case TOKfunction: | |
4322 case TOKdelegate: | |
4323 case TOKtypeof: | |
4324 CASE_BASIC_TYPES: // (type)int.size | |
4325 { // (type) una_exp | |
4326 Type *t; | |
4327 | |
4328 nextToken(); | |
4329 t = parseBasicType(); | |
4330 t = parseDeclarator(t,NULL); | |
4331 check(TOKrparen); | |
4332 | |
4333 // if .identifier | |
4334 if (token.value == TOKdot) | |
4335 { | |
4336 nextToken(); | |
4337 if (token.value != TOKidentifier) | |
4338 { error("Identifier expected following (type)."); | |
4339 return NULL; | |
4340 } | |
4341 e = new TypeDotIdExp(loc, t, token.ident); | |
4342 nextToken(); | |
4343 e = parsePostExp(e); | |
4344 } | |
4345 else | |
4346 { | |
4347 e = parseUnaryExp(); | |
4348 e = new CastExp(loc, e, t); | |
4349 error("C style cast illegal, use %s", e->toChars()); | |
4350 } | |
4351 return e; | |
4352 } | |
4353 } | |
4354 } | |
4355 #endif | |
4356 e = parsePrimaryExp(); | |
4357 break; | |
4358 } | |
4359 default: | |
4360 e = parsePrimaryExp(); | |
4361 break; | |
4362 } | |
4363 assert(e); | |
4364 return e; | |
4365 } | |
4366 | |
4367 Expression *Parser::parseMulExp() | |
4368 { Expression *e; | |
4369 Expression *e2; | |
4370 Loc loc = this->loc; | |
4371 | |
4372 e = parseUnaryExp(); | |
4373 while (1) | |
4374 { | |
4375 switch (token.value) | |
4376 { | |
4377 case TOKmul: nextToken(); e2 = parseUnaryExp(); e = new MulExp(loc,e,e2); continue; | |
4378 case TOKdiv: nextToken(); e2 = parseUnaryExp(); e = new DivExp(loc,e,e2); continue; | |
4379 case TOKmod: nextToken(); e2 = parseUnaryExp(); e = new ModExp(loc,e,e2); continue; | |
4380 | |
4381 default: | |
4382 break; | |
4383 } | |
4384 break; | |
4385 } | |
4386 return e; | |
4387 } | |
4388 | |
4389 Expression *Parser::parseAddExp() | |
4390 { Expression *e; | |
4391 Expression *e2; | |
4392 Loc loc = this->loc; | |
4393 | |
4394 e = parseMulExp(); | |
4395 while (1) | |
4396 { | |
4397 switch (token.value) | |
4398 { | |
4399 case TOKadd: nextToken(); e2 = parseMulExp(); e = new AddExp(loc,e,e2); continue; | |
4400 case TOKmin: nextToken(); e2 = parseMulExp(); e = new MinExp(loc,e,e2); continue; | |
4401 case TOKtilde: nextToken(); e2 = parseMulExp(); e = new CatExp(loc,e,e2); continue; | |
4402 | |
4403 default: | |
4404 break; | |
4405 } | |
4406 break; | |
4407 } | |
4408 return e; | |
4409 } | |
4410 | |
4411 Expression *Parser::parseShiftExp() | |
4412 { Expression *e; | |
4413 Expression *e2; | |
4414 Loc loc = this->loc; | |
4415 | |
4416 e = parseAddExp(); | |
4417 while (1) | |
4418 { | |
4419 switch (token.value) | |
4420 { | |
4421 case TOKshl: nextToken(); e2 = parseAddExp(); e = new ShlExp(loc,e,e2); continue; | |
4422 case TOKshr: nextToken(); e2 = parseAddExp(); e = new ShrExp(loc,e,e2); continue; | |
4423 case TOKushr: nextToken(); e2 = parseAddExp(); e = new UshrExp(loc,e,e2); continue; | |
4424 | |
4425 default: | |
4426 break; | |
4427 } | |
4428 break; | |
4429 } | |
4430 return e; | |
4431 } | |
4432 | |
4433 Expression *Parser::parseRelExp() | |
4434 { Expression *e; | |
4435 Expression *e2; | |
4436 enum TOK op; | |
4437 Loc loc = this->loc; | |
4438 | |
4439 e = parseShiftExp(); | |
4440 while (1) | |
4441 { | |
4442 switch (token.value) | |
4443 { | |
4444 case TOKlt: | |
4445 case TOKle: | |
4446 case TOKgt: | |
4447 case TOKge: | |
4448 case TOKunord: | |
4449 case TOKlg: | |
4450 case TOKleg: | |
4451 case TOKule: | |
4452 case TOKul: | |
4453 case TOKuge: | |
4454 case TOKug: | |
4455 case TOKue: | |
4456 op = token.value; | |
4457 nextToken(); | |
4458 e2 = parseShiftExp(); | |
4459 e = new CmpExp(op, loc, e, e2); | |
4460 continue; | |
4461 | |
4462 case TOKin: | |
4463 nextToken(); | |
4464 e2 = parseShiftExp(); | |
4465 e = new InExp(loc, e, e2); | |
4466 continue; | |
4467 | |
4468 default: | |
4469 break; | |
4470 } | |
4471 break; | |
4472 } | |
4473 return e; | |
4474 } | |
4475 | |
4476 Expression *Parser::parseEqualExp() | |
4477 { Expression *e; | |
4478 Expression *e2; | |
4479 Token *t; | |
4480 Loc loc = this->loc; | |
4481 | |
4482 e = parseRelExp(); | |
4483 while (1) | |
4484 { enum TOK value = token.value; | |
4485 | |
4486 switch (value) | |
4487 { | |
4488 case TOKequal: | |
4489 case TOKnotequal: | |
4490 nextToken(); | |
4491 e2 = parseRelExp(); | |
4492 e = new EqualExp(value, loc, e, e2); | |
4493 continue; | |
4494 | |
4495 case TOKidentity: | |
4496 error("'===' is no longer legal, use 'is' instead"); | |
4497 goto L1; | |
4498 | |
4499 case TOKnotidentity: | |
4500 error("'!==' is no longer legal, use '!is' instead"); | |
4501 goto L1; | |
4502 | |
4503 case TOKis: | |
4504 value = TOKidentity; | |
4505 goto L1; | |
4506 | |
4507 case TOKnot: | |
4508 // Attempt to identify '!is' | |
4509 t = peek(&token); | |
4510 if (t->value != TOKis) | |
4511 break; | |
4512 nextToken(); | |
4513 value = TOKnotidentity; | |
4514 goto L1; | |
4515 | |
4516 L1: | |
4517 nextToken(); | |
4518 e2 = parseRelExp(); | |
4519 e = new IdentityExp(value, loc, e, e2); | |
4520 continue; | |
4521 | |
4522 default: | |
4523 break; | |
4524 } | |
4525 break; | |
4526 } | |
4527 return e; | |
4528 } | |
4529 | |
4530 Expression *Parser::parseCmpExp() | |
4531 { Expression *e; | |
4532 Expression *e2; | |
4533 Token *t; | |
4534 Loc loc = this->loc; | |
4535 | |
4536 e = parseShiftExp(); | |
4537 enum TOK op = token.value; | |
4538 | |
4539 switch (op) | |
4540 { | |
4541 case TOKequal: | |
4542 case TOKnotequal: | |
4543 nextToken(); | |
4544 e2 = parseShiftExp(); | |
4545 e = new EqualExp(op, loc, e, e2); | |
4546 break; | |
4547 | |
4548 case TOKis: | |
4549 op = TOKidentity; | |
4550 goto L1; | |
4551 | |
4552 case TOKnot: | |
4553 // Attempt to identify '!is' | |
4554 t = peek(&token); | |
4555 if (t->value != TOKis) | |
4556 break; | |
4557 nextToken(); | |
4558 op = TOKnotidentity; | |
4559 goto L1; | |
4560 | |
4561 L1: | |
4562 nextToken(); | |
4563 e2 = parseShiftExp(); | |
4564 e = new IdentityExp(op, loc, e, e2); | |
4565 break; | |
4566 | |
4567 case TOKlt: | |
4568 case TOKle: | |
4569 case TOKgt: | |
4570 case TOKge: | |
4571 case TOKunord: | |
4572 case TOKlg: | |
4573 case TOKleg: | |
4574 case TOKule: | |
4575 case TOKul: | |
4576 case TOKuge: | |
4577 case TOKug: | |
4578 case TOKue: | |
4579 nextToken(); | |
4580 e2 = parseShiftExp(); | |
4581 e = new CmpExp(op, loc, e, e2); | |
4582 break; | |
4583 | |
4584 case TOKin: | |
4585 nextToken(); | |
4586 e2 = parseShiftExp(); | |
4587 e = new InExp(loc, e, e2); | |
4588 break; | |
4589 | |
4590 default: | |
4591 break; | |
4592 } | |
4593 return e; | |
4594 } | |
4595 | |
4596 Expression *Parser::parseAndExp() | |
4597 { Expression *e; | |
4598 Expression *e2; | |
4599 Loc loc = this->loc; | |
4600 | |
4601 if (global.params.Dversion == 1) | |
4602 { | |
4603 e = parseEqualExp(); | |
4604 while (token.value == TOKand) | |
4605 { | |
4606 nextToken(); | |
4607 e2 = parseEqualExp(); | |
4608 e = new AndExp(loc,e,e2); | |
4609 loc = this->loc; | |
4610 } | |
4611 } | |
4612 else | |
4613 { | |
4614 e = parseCmpExp(); | |
4615 while (token.value == TOKand) | |
4616 { | |
4617 nextToken(); | |
4618 e2 = parseCmpExp(); | |
4619 e = new AndExp(loc,e,e2); | |
4620 loc = this->loc; | |
4621 } | |
4622 } | |
4623 return e; | |
4624 } | |
4625 | |
4626 Expression *Parser::parseXorExp() | |
4627 { Expression *e; | |
4628 Expression *e2; | |
4629 Loc loc = this->loc; | |
4630 | |
4631 e = parseAndExp(); | |
4632 while (token.value == TOKxor) | |
4633 { | |
4634 nextToken(); | |
4635 e2 = parseAndExp(); | |
4636 e = new XorExp(loc, e, e2); | |
4637 } | |
4638 return e; | |
4639 } | |
4640 | |
4641 Expression *Parser::parseOrExp() | |
4642 { Expression *e; | |
4643 Expression *e2; | |
4644 Loc loc = this->loc; | |
4645 | |
4646 e = parseXorExp(); | |
4647 while (token.value == TOKor) | |
4648 { | |
4649 nextToken(); | |
4650 e2 = parseXorExp(); | |
4651 e = new OrExp(loc, e, e2); | |
4652 } | |
4653 return e; | |
4654 } | |
4655 | |
4656 Expression *Parser::parseAndAndExp() | |
4657 { Expression *e; | |
4658 Expression *e2; | |
4659 Loc loc = this->loc; | |
4660 | |
4661 e = parseOrExp(); | |
4662 while (token.value == TOKandand) | |
4663 { | |
4664 nextToken(); | |
4665 e2 = parseOrExp(); | |
4666 e = new AndAndExp(loc, e, e2); | |
4667 } | |
4668 return e; | |
4669 } | |
4670 | |
4671 Expression *Parser::parseOrOrExp() | |
4672 { Expression *e; | |
4673 Expression *e2; | |
4674 Loc loc = this->loc; | |
4675 | |
4676 e = parseAndAndExp(); | |
4677 while (token.value == TOKoror) | |
4678 { | |
4679 nextToken(); | |
4680 e2 = parseAndAndExp(); | |
4681 e = new OrOrExp(loc, e, e2); | |
4682 } | |
4683 return e; | |
4684 } | |
4685 | |
4686 Expression *Parser::parseCondExp() | |
4687 { Expression *e; | |
4688 Expression *e1; | |
4689 Expression *e2; | |
4690 Loc loc = this->loc; | |
4691 | |
4692 e = parseOrOrExp(); | |
4693 if (token.value == TOKquestion) | |
4694 { | |
4695 nextToken(); | |
4696 e1 = parseExpression(); | |
4697 check(TOKcolon); | |
4698 e2 = parseCondExp(); | |
4699 e = new CondExp(loc, e, e1, e2); | |
4700 } | |
4701 return e; | |
4702 } | |
4703 | |
4704 Expression *Parser::parseAssignExp() | |
4705 { Expression *e; | |
4706 Expression *e2; | |
4707 Loc loc; | |
4708 | |
4709 e = parseCondExp(); | |
4710 while (1) | |
4711 { | |
4712 loc = this->loc; | |
4713 switch (token.value) | |
4714 { | |
4715 #define X(tok,ector) \ | |
4716 case tok: nextToken(); e2 = parseAssignExp(); e = new ector(loc,e,e2); continue; | |
4717 | |
4718 X(TOKassign, AssignExp); | |
4719 X(TOKaddass, AddAssignExp); | |
4720 X(TOKminass, MinAssignExp); | |
4721 X(TOKmulass, MulAssignExp); | |
4722 X(TOKdivass, DivAssignExp); | |
4723 X(TOKmodass, ModAssignExp); | |
4724 X(TOKandass, AndAssignExp); | |
4725 X(TOKorass, OrAssignExp); | |
4726 X(TOKxorass, XorAssignExp); | |
4727 X(TOKshlass, ShlAssignExp); | |
4728 X(TOKshrass, ShrAssignExp); | |
4729 X(TOKushrass, UshrAssignExp); | |
4730 X(TOKcatass, CatAssignExp); | |
4731 | |
4732 #undef X | |
4733 default: | |
4734 break; | |
4735 } | |
4736 break; | |
4737 } | |
4738 return e; | |
4739 } | |
4740 | |
4741 Expression *Parser::parseExpression() | |
4742 { Expression *e; | |
4743 Expression *e2; | |
4744 Loc loc = this->loc; | |
4745 | |
4746 //printf("Parser::parseExpression()\n"); | |
4747 e = parseAssignExp(); | |
4748 while (token.value == TOKcomma) | |
4749 { | |
4750 nextToken(); | |
4751 e2 = parseAssignExp(); | |
4752 e = new CommaExp(loc, e, e2); | |
4753 loc = this->loc; | |
4754 } | |
4755 return e; | |
4756 } | |
4757 | |
4758 | |
4759 /************************* | |
4760 * Collect argument list. | |
4761 * Assume current token is '(' or '['. | |
4762 */ | |
4763 | |
4764 Expressions *Parser::parseArguments() | |
4765 { // function call | |
4766 Expressions *arguments; | |
4767 Expression *arg; | |
4768 enum TOK endtok; | |
4769 | |
4770 arguments = new Expressions(); | |
4771 if (token.value == TOKlbracket) | |
4772 endtok = TOKrbracket; | |
4773 else | |
4774 endtok = TOKrparen; | |
4775 | |
4776 { | |
4777 nextToken(); | |
4778 if (token.value != endtok) | |
4779 { | |
4780 while (1) | |
4781 { | |
4782 arg = parseAssignExp(); | |
4783 arguments->push(arg); | |
4784 if (token.value == endtok) | |
4785 break; | |
4786 check(TOKcomma); | |
4787 } | |
4788 } | |
4789 check(endtok); | |
4790 } | |
4791 return arguments; | |
4792 } | |
4793 | |
4794 /******************************************* | |
4795 */ | |
4796 | |
4797 Expression *Parser::parseNewExp(Expression *thisexp) | |
4798 { Type *t; | |
4799 Expressions *newargs; | |
4800 Expressions *arguments = NULL; | |
4801 Expression *e; | |
4802 Loc loc = this->loc; | |
4803 | |
4804 nextToken(); | |
4805 newargs = NULL; | |
4806 if (token.value == TOKlparen) | |
4807 { | |
4808 newargs = parseArguments(); | |
4809 } | |
4810 | |
4811 // An anonymous nested class starts with "class" | |
4812 if (token.value == TOKclass) | |
4813 { | |
4814 nextToken(); | |
4815 if (token.value == TOKlparen) | |
4816 arguments = parseArguments(); | |
4817 | |
4818 BaseClasses *baseclasses = NULL; | |
4819 if (token.value != TOKlcurly) | |
4820 baseclasses = parseBaseClasses(); | |
4821 | |
4822 Identifier *id = NULL; | |
4823 ClassDeclaration *cd = new ClassDeclaration(loc, id, baseclasses); | |
4824 | |
4825 if (token.value != TOKlcurly) | |
4826 { error("{ members } expected for anonymous class"); | |
4827 cd->members = NULL; | |
4828 } | |
4829 else | |
4830 { | |
4831 nextToken(); | |
4832 Array *decl = parseDeclDefs(0); | |
4833 if (token.value != TOKrcurly) | |
4834 error("class member expected"); | |
4835 nextToken(); | |
4836 cd->members = decl; | |
4837 } | |
4838 | |
4839 e = new NewAnonClassExp(loc, thisexp, newargs, cd, arguments); | |
4840 | |
4841 return e; | |
4842 } | |
4843 | |
4844 #if LTORARRAYDECL | |
4845 t = parseBasicType(); | |
4846 t = parseBasicType2(t); | |
4847 if (t->ty == Taarray) | |
4848 { | |
4849 Type *index = ((TypeAArray *)t)->index; | |
4850 | |
4851 Expression *e = index->toExpression(); | |
4852 if (e) | |
4853 { arguments = new Expressions(); | |
4854 arguments->push(e); | |
4855 t = new TypeDArray(t->next); | |
4856 } | |
4857 else | |
4858 { | |
4859 error("need size of rightmost array, not type %s", index->toChars()); | |
4860 return new NullExp(loc); | |
4861 } | |
4862 } | |
4863 else if (t->ty == Tsarray) | |
4864 { | |
4865 TypeSArray *tsa = (TypeSArray *)t; | |
4866 Expression *e = tsa->dim; | |
4867 | |
4868 arguments = new Expressions(); | |
4869 arguments->push(e); | |
4870 t = new TypeDArray(t->next); | |
4871 } | |
4872 else if (token.value == TOKlparen) | |
4873 { | |
4874 arguments = parseArguments(); | |
4875 } | |
4876 #else | |
4877 t = parseBasicType(); | |
4878 while (token.value == TOKmul) | |
4879 { t = new TypePointer(t); | |
4880 nextToken(); | |
4881 } | |
4882 if (token.value == TOKlbracket) | |
4883 { | |
4884 Expression *e; | |
4885 | |
4886 nextToken(); | |
4887 e = parseAssignExp(); | |
4888 arguments = new Array(); | |
4889 arguments->push(e); | |
4890 check(TOKrbracket); | |
4891 t = parseDeclarator(t, NULL); | |
4892 t = new TypeDArray(t); | |
4893 } | |
4894 else if (token.value == TOKlparen) | |
4895 arguments = parseArguments(); | |
4896 #endif | |
4897 e = new NewExp(loc, thisexp, newargs, t, arguments); | |
4898 return e; | |
4899 } | |
4900 | |
4901 /********************************************** | |
4902 */ | |
4903 | |
4904 void Parser::addComment(Dsymbol *s, unsigned char *blockComment) | |
4905 { | |
4906 s->addComment(combineComments(blockComment, token.lineComment)); | |
4907 } | |
4908 | |
4909 | |
4910 /********************************* ***************************/ | |
4911 |