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