1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
|
/* Compiler implementation of the D programming language
* Copyright (C) 1999-2022 by The D Language Foundation, All Rights Reserved
* written by Walter Bright
* https://www.digitalmars.com
* Distributed under the Boost Software License, Version 1.0.
* https://www.boost.org/LICENSE_1_0.txt
* https://github.com/dlang/dmd/blob/master/src/dmd/statement.h
*/
#pragma once
#include "arraytypes.h"
#include "ast_node.h"
#include "dsymbol.h"
#include "visitor.h"
#include "tokens.h"
struct Scope;
class Expression;
class LabelDsymbol;
class Identifier;
class IfStatement;
class ExpStatement;
class DefaultStatement;
class VarDeclaration;
class Condition;
class ErrorStatement;
class ReturnStatement;
class CompoundStatement;
class Parameter;
class StaticAssert;
class AsmStatement;
class GotoStatement;
class ScopeStatement;
class TryCatchStatement;
class TryFinallyStatement;
class CaseStatement;
class DefaultStatement;
class LabelStatement;
class StaticForeach;
// Back end
struct code;
/* How a statement exits; this is returned by blockExit()
*/
enum BE
{
BEnone = 0,
BEfallthru = 1,
BEthrow = 2,
BEreturn = 4,
BEgoto = 8,
BEhalt = 0x10,
BEbreak = 0x20,
BEcontinue = 0x40,
BEerrthrow = 0x80,
BEany = (BEfallthru | BEthrow | BEreturn | BEgoto | BEhalt)
};
typedef unsigned char STMT;
enum
{
STMTerror,
STMTpeel,
STMTexp, STMTdtorExp,
STMTcompile,
STMTcompound, STMTcompoundDeclaration, STMTcompoundAsm,
STMTunrolledLoop,
STMTscope,
STMTforwarding,
STMTwhile,
STMTdo,
STMTfor,
STMTforeach,
STMTforeachRange,
STMTif,
STMTconditional,
STMTstaticForeach,
STMTpragma,
STMTstaticAssert,
STMTswitch,
STMTcase,
STMTcaseRange,
STMTdefault,
STMTgotoDefault,
STMTgotoCase,
STMTswitchError,
STMTreturn,
STMTbreak,
STMTcontinue,
STMTsynchronized,
STMTwith,
STMTtryCatch,
STMTtryFinally,
STMTscopeGuard,
STMTthrow,
STMTdebug,
STMTgoto,
STMTlabel,
STMTasm, STMTinlineAsm, STMTgccAsm,
STMTimport
};
class Statement : public ASTNode
{
public:
Loc loc;
STMT stmt;
virtual Statement *syntaxCopy();
const char *toChars() const;
void error(const char *format, ...);
void warning(const char *format, ...);
void deprecation(const char *format, ...);
virtual Statement *getRelatedLabeled() { return this; }
virtual bool hasBreak() const;
virtual bool hasContinue() const;
bool usesEH();
bool comeFrom();
bool hasCode();
virtual Statement *last();
virtual ReturnStatement *endsWithReturnStatement() { return NULL; }
ErrorStatement *isErrorStatement() { return stmt == STMTerror ? (ErrorStatement*)this : NULL; }
ScopeStatement *isScopeStatement() { return stmt == STMTscope ? (ScopeStatement*)this : NULL; }
ExpStatement *isExpStatement() { return stmt == STMTexp ? (ExpStatement*)this : NULL; }
CompoundStatement *isCompoundStatement() { return stmt == STMTcompound ? (CompoundStatement*)this : NULL; }
ReturnStatement *isReturnStatement() { return stmt == STMTreturn ? (ReturnStatement*)this : NULL; }
IfStatement *isIfStatement() { return stmt == STMTif ? (IfStatement*)this : NULL; }
ConditionalStatement *isConditionalStatement() { return stmt == STMTconditional ? (ConditionalStatement*)this : NULL; }
StaticForeachStatement *isStaticForeachStatement() { return stmt == STMTstaticForeach ? (StaticForeachStatement*)this : NULL; }
CaseStatement *isCaseStatement() { return stmt == STMTcase ? (CaseStatement*)this : NULL; }
DefaultStatement *isDefaultStatement() { return stmt == STMTdefault ? (DefaultStatement*)this : NULL; }
LabelStatement *isLabelStatement() { return stmt == STMTlabel ? (LabelStatement*)this : NULL; }
GotoDefaultStatement *isGotoDefaultStatement() { return stmt == STMTgotoDefault ? (GotoDefaultStatement*)this : NULL; }
GotoCaseStatement *isGotoCaseStatement() { return stmt == STMTgotoCase ? (GotoCaseStatement*)this : NULL; }
BreakStatement *isBreakStatement() { return stmt == STMTbreak ? (BreakStatement*)this : NULL; }
DtorExpStatement *isDtorExpStatement() { return stmt == STMTdtorExp ? (DtorExpStatement*)this : NULL; }
CompileStatement *isCompileStatement() { return stmt == STMTcompile ? (CompileStatement*)this : NULL; }
ForwardingStatement *isForwardingStatement() { return stmt == STMTforwarding ? (ForwardingStatement*)this : NULL; }
DoStatement *isDoStatement() { return stmt == STMTdo ? (DoStatement*)this : NULL; }
ForStatement *isForStatement() { return stmt == STMTfor ? (ForStatement*)this : NULL; }
ForeachStatement *isForeachStatement() { return stmt == STMTforeach ? (ForeachStatement*)this : NULL; }
SwitchStatement *isSwitchStatement() { return stmt == STMTswitch ? (SwitchStatement*)this : NULL; }
ContinueStatement *isContinueStatement() { return stmt == STMTcontinue ? (ContinueStatement*)this : NULL; }
WithStatement *isWithStatement() { return stmt == STMTwith ? (WithStatement*)this : NULL; }
TryCatchStatement *isTryCatchStatement() { return stmt == STMTtryCatch ? (TryCatchStatement*)this : NULL; }
ThrowStatement *isThrowStatement() { return stmt == STMTthrow ? (ThrowStatement*)this : NULL; }
DebugStatement *isDebugStatement() { return stmt == STMTdebug ? (DebugStatement*)this : NULL; }
TryFinallyStatement *isTryFinallyStatement() { return stmt == STMTtryFinally ? (TryFinallyStatement*)this : NULL; }
ScopeGuardStatement *isScopeGuardStatement() { return stmt == STMTscopeGuard ? (ScopeGuardStatement*)this : NULL; }
SwitchErrorStatement *isSwitchErrorStatement() { return stmt == STMTswitchError ? (SwitchErrorStatement*)this : NULL; }
UnrolledLoopStatement *isUnrolledLoopStatement() { return stmt == STMTunrolledLoop ? (UnrolledLoopStatement*)this : NULL; }
ForeachRangeStatement *isForeachRangeStatement() { return stmt == STMTforeachRange ? (ForeachRangeStatement*)this : NULL; }
CompoundDeclarationStatement *isCompoundDeclarationStatement() { return stmt == STMTcompoundDeclaration ? (CompoundDeclarationStatement*)this : NULL; }
void accept(Visitor *v) { v->visit(this); }
};
/** Any Statement that fails semantic() or has a component that is an ErrorExp or
* a TypeError should return an ErrorStatement from semantic().
*/
class ErrorStatement : public Statement
{
public:
ErrorStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class PeelStatement : public Statement
{
public:
Statement *s;
void accept(Visitor *v) { v->visit(this); }
};
class ExpStatement : public Statement
{
public:
Expression *exp;
static ExpStatement *create(const Loc &loc, Expression *exp);
ExpStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class DtorExpStatement : public ExpStatement
{
public:
/* Wraps an expression that is the destruction of 'var'
*/
VarDeclaration *var;
DtorExpStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class CompileStatement : public Statement
{
public:
Expressions *exps;
CompileStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class CompoundStatement : public Statement
{
public:
Statements *statements;
static CompoundStatement *create(const Loc &loc, Statement *s1, Statement *s2);
CompoundStatement *syntaxCopy();
ReturnStatement *endsWithReturnStatement();
Statement *last();
void accept(Visitor *v) { v->visit(this); }
};
class CompoundDeclarationStatement : public CompoundStatement
{
public:
CompoundDeclarationStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
/* The purpose of this is so that continue will go to the next
* of the statements, and break will go to the end of the statements.
*/
class UnrolledLoopStatement : public Statement
{
public:
Statements *statements;
UnrolledLoopStatement *syntaxCopy();
bool hasBreak() const;
bool hasContinue() const;
void accept(Visitor *v) { v->visit(this); }
};
class ScopeStatement : public Statement
{
public:
Statement *statement;
Loc endloc; // location of closing curly bracket
ScopeStatement *syntaxCopy();
ReturnStatement *endsWithReturnStatement();
bool hasBreak() const;
bool hasContinue() const;
void accept(Visitor *v) { v->visit(this); }
};
class ForwardingStatement : public Statement
{
public:
ForwardingScopeDsymbol *sym;
Statement *statement;
ForwardingStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class WhileStatement : public Statement
{
public:
Parameter *param;
Expression *condition;
Statement *_body;
Loc endloc; // location of closing curly bracket
WhileStatement *syntaxCopy();
bool hasBreak() const;
bool hasContinue() const;
void accept(Visitor *v) { v->visit(this); }
};
class DoStatement : public Statement
{
public:
Statement *_body;
Expression *condition;
Loc endloc; // location of ';' after while
DoStatement *syntaxCopy();
bool hasBreak() const;
bool hasContinue() const;
void accept(Visitor *v) { v->visit(this); }
};
class ForStatement : public Statement
{
public:
Statement *_init;
Expression *condition;
Expression *increment;
Statement *_body;
Loc endloc; // location of closing curly bracket
// When wrapped in try/finally clauses, this points to the outermost one,
// which may have an associated label. Internal break/continue statements
// treat that label as referring to this loop.
Statement *relatedLabeled;
ForStatement *syntaxCopy();
Statement *getRelatedLabeled() { return relatedLabeled ? relatedLabeled : this; }
bool hasBreak() const;
bool hasContinue() const;
void accept(Visitor *v) { v->visit(this); }
};
class ForeachStatement : public Statement
{
public:
TOK op; // TOKforeach or TOKforeach_reverse
Parameters *parameters; // array of Parameter*'s
Expression *aggr;
Statement *_body;
Loc endloc; // location of closing curly bracket
VarDeclaration *key;
VarDeclaration *value;
FuncDeclaration *func; // function we're lexically in
Statements *cases; // put breaks, continues, gotos and returns here
ScopeStatements *gotos; // forward referenced goto's go here
ForeachStatement *syntaxCopy();
bool hasBreak() const;
bool hasContinue() const;
void accept(Visitor *v) { v->visit(this); }
};
class ForeachRangeStatement : public Statement
{
public:
TOK op; // TOKforeach or TOKforeach_reverse
Parameter *prm; // loop index variable
Expression *lwr;
Expression *upr;
Statement *_body;
Loc endloc; // location of closing curly bracket
VarDeclaration *key;
ForeachRangeStatement *syntaxCopy();
bool hasBreak() const;
bool hasContinue() const;
void accept(Visitor *v) { v->visit(this); }
};
class IfStatement : public Statement
{
public:
Parameter *prm;
Expression *condition;
Statement *ifbody;
Statement *elsebody;
VarDeclaration *match; // for MatchExpression results
Loc endloc; // location of closing curly bracket
IfStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class ConditionalStatement : public Statement
{
public:
Condition *condition;
Statement *ifbody;
Statement *elsebody;
ConditionalStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class StaticForeachStatement : public Statement
{
public:
StaticForeach *sfe;
StaticForeachStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class PragmaStatement : public Statement
{
public:
Identifier *ident;
Expressions *args; // array of Expression's
Statement *_body;
PragmaStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class StaticAssertStatement : public Statement
{
public:
StaticAssert *sa;
StaticAssertStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class SwitchStatement : public Statement
{
public:
Expression *condition;
Statement *_body;
bool isFinal;
DefaultStatement *sdefault;
Statement *tryBody; // set to TryCatchStatement or TryFinallyStatement if in _body portion
TryFinallyStatement *tf;
GotoCaseStatements gotoCases; // array of unresolved GotoCaseStatement's
CaseStatements *cases; // array of CaseStatement's
int hasNoDefault; // !=0 if no default statement
int hasVars; // !=0 if has variable case values
VarDeclaration *lastVar;
SwitchStatement *syntaxCopy();
bool hasBreak() const;
void accept(Visitor *v) { v->visit(this); }
};
class CaseStatement : public Statement
{
public:
Expression *exp;
Statement *statement;
int index; // which case it is (since we sort this)
VarDeclaration *lastVar;
void* extra; // for use by Statement_toIR()
CaseStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class CaseRangeStatement : public Statement
{
public:
Expression *first;
Expression *last;
Statement *statement;
CaseRangeStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class DefaultStatement : public Statement
{
public:
Statement *statement;
VarDeclaration *lastVar;
DefaultStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class GotoDefaultStatement : public Statement
{
public:
SwitchStatement *sw;
GotoDefaultStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class GotoCaseStatement : public Statement
{
public:
Expression *exp; // NULL, or which case to goto
CaseStatement *cs; // case statement it resolves to
GotoCaseStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class SwitchErrorStatement : public Statement
{
public:
Expression *exp;
void accept(Visitor *v) { v->visit(this); }
};
class ReturnStatement : public Statement
{
public:
Expression *exp;
size_t caseDim;
ReturnStatement *syntaxCopy();
ReturnStatement *endsWithReturnStatement() { return this; }
void accept(Visitor *v) { v->visit(this); }
};
class BreakStatement : public Statement
{
public:
Identifier *ident;
BreakStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class ContinueStatement : public Statement
{
public:
Identifier *ident;
ContinueStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class SynchronizedStatement : public Statement
{
public:
Expression *exp;
Statement *_body;
SynchronizedStatement *syntaxCopy();
bool hasBreak() const;
bool hasContinue() const;
void accept(Visitor *v) { v->visit(this); }
};
class WithStatement : public Statement
{
public:
Expression *exp;
Statement *_body;
VarDeclaration *wthis;
Loc endloc;
WithStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class TryCatchStatement : public Statement
{
public:
Statement *_body;
Catches *catches;
Statement *tryBody; /// set to enclosing TryCatchStatement or TryFinallyStatement if in _body portion
TryCatchStatement *syntaxCopy();
bool hasBreak() const;
void accept(Visitor *v) { v->visit(this); }
};
class Catch : public RootObject
{
public:
Loc loc;
Type *type;
Identifier *ident;
Statement *handler;
VarDeclaration *var;
// set if semantic processing errors
bool errors;
// was generated by the compiler,
// wasn't present in source code
bool internalCatch;
Catch *syntaxCopy();
};
class TryFinallyStatement : public Statement
{
public:
Statement *_body;
Statement *finalbody;
Statement *tryBody; // set to enclosing TryCatchStatement or TryFinallyStatement if in _body portion
bool bodyFallsThru; // true if _body falls through to finally
static TryFinallyStatement *create(const Loc &loc, Statement *body, Statement *finalbody);
TryFinallyStatement *syntaxCopy();
bool hasBreak() const;
bool hasContinue() const;
void accept(Visitor *v) { v->visit(this); }
};
class ScopeGuardStatement : public Statement
{
public:
TOK tok;
Statement *statement;
ScopeGuardStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class ThrowStatement : public Statement
{
public:
Expression *exp;
// was generated by the compiler,
// wasn't present in source code
bool internalThrow;
ThrowStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class DebugStatement : public Statement
{
public:
Statement *statement;
DebugStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class GotoStatement : public Statement
{
public:
Identifier *ident;
LabelDsymbol *label;
Statement *tryBody; /// set to enclosing TryCatchStatement or TryFinallyStatement if in _body portion
TryFinallyStatement *tf;
ScopeGuardStatement *os;
VarDeclaration *lastVar;
GotoStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class LabelStatement : public Statement
{
public:
Identifier *ident;
Statement *statement;
Statement *tryBody; /// set to enclosing TryCatchStatement or TryFinallyStatement if in _body portion
TryFinallyStatement *tf;
ScopeGuardStatement *os;
VarDeclaration *lastVar;
Statement *gotoTarget; // interpret
void* extra; // used by Statement_toIR()
bool breaks; // someone did a 'break ident'
LabelStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class LabelDsymbol : public Dsymbol
{
public:
LabelStatement *statement;
bool deleted; // set if rewritten to return in foreach delegate
bool iasm; // set if used by inline assembler
static LabelDsymbol *create(Identifier *ident);
LabelDsymbol *isLabel();
void accept(Visitor *v) { v->visit(this); }
};
Statement* asmSemantic(AsmStatement *s, Scope *sc);
class AsmStatement : public Statement
{
public:
Token *tokens;
AsmStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class InlineAsmStatement : public AsmStatement
{
public:
code *asmcode;
unsigned asmalign; // alignment of this statement
unsigned regs; // mask of registers modified (must match regm_t in back end)
bool refparam; // true if function parameter is referenced
bool naked; // true if function is to be naked
InlineAsmStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
// A GCC asm statement - assembler instructions with D expression operands
class GccAsmStatement : public AsmStatement
{
public:
StorageClass stc; // attributes of the asm {} block
Expression *insn; // string expression that is the template for assembler code
Expressions *args; // input and output operands of the statement
unsigned outputargs; // of the operands in 'args', the number of output operands
Identifiers *names; // list of symbolic names for the operands
Expressions *constraints; // list of string constants specifying constraints on operands
Expressions *clobbers; // list of string constants specifying clobbers and scratch registers
Identifiers *labels; // list of goto labels
GotoStatements *gotos; // of the goto labels, the equivalent statements they represent
GccAsmStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
// a complete asm {} block
class CompoundAsmStatement : public CompoundStatement
{
public:
StorageClass stc; // postfix attributes like nothrow/pure/@trusted
CompoundAsmStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
class ImportStatement : public Statement
{
public:
Dsymbols *imports; // Array of Import's
ImportStatement *syntaxCopy();
void accept(Visitor *v) { v->visit(this); }
};
|