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