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