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