-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathstatement.d
2066 lines (1779 loc) · 57.5 KB
/
statement.d
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Defines AST nodes for statements.
*
* Specification: $(LINK2 https://dlang.org/spec/statement.html, Statements)
*
* Copyright: Copyright (C) 1999-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/statement.d, _statement.d)
* Documentation: https://dlang.org/phobos/dmd_statement.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/statement.d
*/
module dmd.statement;
import core.stdc.stdarg;
import core.stdc.stdio;
import dmd.arraytypes;
import dmd.astenums;
import dmd.ast_node;
import dmd.errors;
import dmd.cond;
import dmd.declaration;
import dmd.dsymbol;
import dmd.expression;
import dmd.func;
import dmd.id;
import dmd.identifier;
import dmd.location;
import dmd.mtype;
import dmd.rootobject;
import dmd.sapply;
import dmd.staticassert;
import dmd.tokens;
import dmd.visitor;
/***********************************************************
* Specification: https://dlang.org/spec/statement.html
*/
extern (C++) abstract class Statement : ASTNode
{
const Loc loc;
const STMT stmt;
override final DYNCAST dyncast() const
{
return DYNCAST.statement;
}
final extern (D) this(const ref Loc loc, STMT stmt) @safe
{
this.loc = loc;
this.stmt = stmt;
// If this is an in{} contract scope statement (skip for determining
// inlineStatus of a function body for header content)
}
Statement syntaxCopy()
{
assert(0);
}
/*************************************
* Do syntax copy of an array of Statement's.
*/
static Statements* arraySyntaxCopy(Statements* a)
{
Statements* b = null;
if (a)
{
b = a.copy();
foreach (i, s; *a)
{
(*b)[i] = s ? s.syntaxCopy() : null;
}
}
return b;
}
Statement getRelatedLabeled()
{
return this;
}
/****************************
* Determine if an enclosed `break` would apply to this
* statement, such as if it is a loop or switch statement.
* Returns:
* `true` if it does
*/
bool hasBreak() const pure nothrow
{
//printf("Statement::hasBreak()\n");
return false;
}
/****************************
* Determine if an enclosed `continue` would apply to this
* statement, such as if it is a loop statement.
* Returns:
* `true` if it does
*/
bool hasContinue() const pure nothrow
{
return false;
}
/**********************************
* Returns:
* `true` if statement uses exception handling
*/
extern (D) final bool usesEH()
{
extern (C++) final class UsesEH : StoppableVisitor
{
alias visit = typeof(super).visit;
public:
override void visit(Statement s)
{
}
override void visit(TryCatchStatement s)
{
stop = true;
}
override void visit(TryFinallyStatement s)
{
stop = true;
}
override void visit(ScopeGuardStatement s)
{
stop = true;
}
override void visit(SynchronizedStatement s)
{
stop = true;
}
}
scope UsesEH ueh = new UsesEH();
return walkPostorder(this, ueh);
}
/**********************************
* Returns:
* `true` if statement 'comes from' somewhere else, like a goto
*/
extern (D) final bool comeFrom()
{
extern (C++) final class ComeFrom : StoppableVisitor
{
alias visit = typeof(super).visit;
public:
override void visit(Statement s)
{
}
override void visit(CaseStatement s)
{
stop = true;
}
override void visit(DefaultStatement s)
{
stop = true;
}
override void visit(LabelStatement s)
{
stop = true;
}
override void visit(AsmStatement s)
{
stop = true;
}
}
scope ComeFrom cf = new ComeFrom();
return walkPostorder(this, cf);
}
/**********************************
* Returns:
* `true` if statement has executable code.
*/
extern (D) final bool hasCode()
{
extern (C++) final class HasCode : StoppableVisitor
{
alias visit = typeof(super).visit;
public:
override void visit(Statement s)
{
stop = true;
}
override void visit(ExpStatement s)
{
if (s.exp !is null)
{
stop = s.exp.hasCode();
}
}
override void visit(CompoundStatement s)
{
}
override void visit(ScopeStatement s)
{
}
override void visit(ImportStatement s)
{
}
override void visit(CaseStatement s)
{
}
override void visit(DefaultStatement s)
{
}
override void visit(LabelStatement s)
{
}
}
scope HasCode hc = new HasCode();
return walkPostorder(this, hc);
}
/*******************************
* Find last statement in a sequence of statements.
* Returns:
* the last statement, or `null` if there isn't one
*/
inout(Statement) last() inout nothrow pure
{
return this;
}
/**************************
* Support Visitor Pattern
* Params:
* v = visitor
*/
override void accept(Visitor v)
{
v.visit(this);
}
/************************************
* Does this statement end with a return statement?
*
* I.e. is it a single return statement or some compound statement
* that unconditionally hits a return statement.
* Returns:
* return statement it ends with, otherwise null
*/
pure nothrow @nogc
inout(ReturnStatement) endsWithReturnStatement() inout { return null; }
version (IN_LLVM)
{
pure nothrow @nogc
inout(CompoundAsmStatement) endsWithAsm() inout { return null; }
}
final pure inout nothrow @nogc @trusted
{
/********************
* A cheaper method of doing downcasting of Statements.
* Returns:
* the downcast statement if it can be downcasted, otherwise `null`
*/
inout(ErrorStatement) isErrorStatement() { return stmt == STMT.Error ? cast(typeof(return))this : null; }
inout(PeelStatement) isPeelStatement() { return stmt == STMT.Peel ? cast(typeof(return))this : null; }
inout(ScopeStatement) isScopeStatement() { return stmt == STMT.Scope ? cast(typeof(return))this : null; }
inout(ExpStatement) isExpStatement() { return stmt == STMT.Exp ? cast(typeof(return))this : null; }
inout(CompoundStatement) isCompoundStatement() { return stmt == STMT.Compound ? cast(typeof(return))this : null; }
inout(ReturnStatement) isReturnStatement() { return stmt == STMT.Return ? cast(typeof(return))this : null; }
inout(IfStatement) isIfStatement() { return stmt == STMT.If ? cast(typeof(return))this : null; }
inout(ConditionalStatement) isConditionalStatement() { return stmt == STMT.Conditional ? cast(typeof(return))this : null; }
inout(StaticForeachStatement) isStaticForeachStatement() { return stmt == STMT.StaticForeach ? cast(typeof(return))this : null; }
inout(CaseStatement) isCaseStatement() { return stmt == STMT.Case ? cast(typeof(return))this : null; }
inout(DefaultStatement) isDefaultStatement() { return stmt == STMT.Default ? cast(typeof(return))this : null; }
inout(LabelStatement) isLabelStatement() { return stmt == STMT.Label ? cast(typeof(return))this : null; }
inout(GotoStatement) isGotoStatement() { return stmt == STMT.Goto ? cast(typeof(return))this : null; }
inout(GotoDefaultStatement) isGotoDefaultStatement() { return stmt == STMT.GotoDefault ? cast(typeof(return))this : null; }
inout(GotoCaseStatement) isGotoCaseStatement() { return stmt == STMT.GotoCase ? cast(typeof(return))this : null; }
inout(BreakStatement) isBreakStatement() { return stmt == STMT.Break ? cast(typeof(return))this : null; }
inout(DtorExpStatement) isDtorExpStatement() { return stmt == STMT.DtorExp ? cast(typeof(return))this : null; }
inout(MixinStatement) isMixinStatement() { return stmt == STMT.Mixin ? cast(typeof(return))this : null; }
inout(ForwardingStatement) isForwardingStatement() { return stmt == STMT.Forwarding ? cast(typeof(return))this : null; }
inout(DoStatement) isDoStatement() { return stmt == STMT.Do ? cast(typeof(return))this : null; }
inout(WhileStatement) isWhileStatement() { return stmt == STMT.While ? cast(typeof(return))this : null; }
inout(ForStatement) isForStatement() { return stmt == STMT.For ? cast(typeof(return))this : null; }
inout(ForeachStatement) isForeachStatement() { return stmt == STMT.Foreach ? cast(typeof(return))this : null; }
inout(SwitchStatement) isSwitchStatement() { return stmt == STMT.Switch ? cast(typeof(return))this : null; }
inout(ContinueStatement) isContinueStatement() { return stmt == STMT.Continue ? cast(typeof(return))this : null; }
inout(WithStatement) isWithStatement() { return stmt == STMT.With ? cast(typeof(return))this : null; }
inout(TryCatchStatement) isTryCatchStatement() { return stmt == STMT.TryCatch ? cast(typeof(return))this : null; }
inout(ThrowStatement) isThrowStatement() { return stmt == STMT.Throw ? cast(typeof(return))this : null; }
inout(DebugStatement) isDebugStatement() { return stmt == STMT.Debug ? cast(typeof(return))this : null; }
inout(TryFinallyStatement) isTryFinallyStatement() { return stmt == STMT.TryFinally ? cast(typeof(return))this : null; }
inout(ScopeGuardStatement) isScopeGuardStatement() { return stmt == STMT.ScopeGuard ? cast(typeof(return))this : null; }
inout(SwitchErrorStatement) isSwitchErrorStatement() { return stmt == STMT.SwitchError ? cast(typeof(return))this : null; }
inout(UnrolledLoopStatement) isUnrolledLoopStatement() { return stmt == STMT.UnrolledLoop ? cast(typeof(return))this : null; }
inout(ForeachRangeStatement) isForeachRangeStatement() { return stmt == STMT.ForeachRange ? cast(typeof(return))this : null; }
inout(CompoundDeclarationStatement) isCompoundDeclarationStatement() { return stmt == STMT.CompoundDeclaration ? cast(typeof(return))this : null; }
inout(CompoundAsmStatement) isCompoundAsmStatement() { return stmt == STMT.CompoundAsm ? cast(typeof(return))this : null; }
inout(PragmaStatement) isPragmaStatement() { return stmt == STMT.Pragma ? cast(typeof(return))this : null; }
inout(StaticAssertStatement) isStaticAssertStatement() { return stmt == STMT.StaticAssert ? cast(typeof(return))this : null; }
inout(CaseRangeStatement) isCaseRangeStatement() { return stmt == STMT.CaseRange ? cast(typeof(return))this : null; }
inout(SynchronizedStatement) isSynchronizedStatement() { return stmt == STMT.Synchronized ? cast(typeof(return))this : null; }
inout(AsmStatement) isAsmStatement() { return stmt == STMT.Asm ? cast(typeof(return))this : null; }
inout(InlineAsmStatement) isInlineAsmStatement() { return stmt == STMT.InlineAsm ? cast(typeof(return))this : null; }
inout(GccAsmStatement) isGccAsmStatement() { return stmt == STMT.GccAsm ? cast(typeof(return))this : null; }
inout(ImportStatement) isImportStatement() { return stmt == STMT.Import ? cast(typeof(return))this : null; }
}
}
/***********************************************************
* Any Statement that fails semantic() or has a component that is an ErrorExp or
* a TypeError should return an ErrorStatement from semantic().
*/
extern (C++) final class ErrorStatement : Statement
{
extern (D) this()
{
super(Loc.initial, STMT.Error);
import dmd.globals;
assert(global.gaggedErrors || global.errors);
}
override ErrorStatement syntaxCopy()
{
return this;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class PeelStatement : Statement
{
Statement s;
extern (D) this(Statement s) @safe
{
super(s.loc, STMT.Peel);
this.s = s;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/statement.html#ExpressionStatement
*/
extern (C++) class ExpStatement : Statement
{
Expression exp;
final extern (D) this(const ref Loc loc, Expression exp) @safe
{
super(loc, STMT.Exp);
this.exp = exp;
}
final extern (D) this(const ref Loc loc, Expression exp, STMT stmt) @safe
{
super(loc, stmt);
this.exp = exp;
}
final extern (D) this(const ref Loc loc, Dsymbol declaration) @safe
{
super(loc, STMT.Exp);
this.exp = new DeclarationExp(loc, declaration);
}
static ExpStatement create(const ref Loc loc, Expression exp) @safe
{
return new ExpStatement(loc, exp);
}
override ExpStatement syntaxCopy()
{
return new ExpStatement(loc, exp ? exp.syntaxCopy() : null);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class DtorExpStatement : ExpStatement
{
// Wraps an expression that is the destruction of 'var'
VarDeclaration var;
extern (D) this(const ref Loc loc, Expression exp, VarDeclaration var) @safe
{
super(loc, exp, STMT.DtorExp);
this.var = var;
}
override DtorExpStatement syntaxCopy()
{
return new DtorExpStatement(loc, exp ? exp.syntaxCopy() : null, var);
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/statement.html#mixin-statement
*/
// Note: was called CompileStatement
extern (C++) final class MixinStatement : Statement
{
Expressions* exps;
extern (D) this(const ref Loc loc, Expression exp)
{
Expressions* exps = new Expressions();
exps.push(exp);
this(loc, exps);
}
extern (D) this(const ref Loc loc, Expressions* exps) @safe
{
super(loc, STMT.Mixin);
this.exps = exps;
}
override MixinStatement syntaxCopy()
{
return new MixinStatement(loc, Expression.arraySyntaxCopy(exps));
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) class CompoundStatement : Statement
{
Statements* statements;
/**
* Construct a `CompoundStatement` using an already existing
* array of `Statement`s
*
* Params:
* loc = Instantiation information
* statements = An array of `Statement`s, that will referenced by this class
*/
final extern (D) this(const ref Loc loc, Statements* statements) @safe
{
super(loc, STMT.Compound);
this.statements = statements;
}
final extern (D) this(const ref Loc loc, Statements* statements, STMT stmt) @safe
{
super(loc, stmt);
this.statements = statements;
}
/**
* Construct a `CompoundStatement` from an array of `Statement`s
*
* Params:
* loc = Instantiation information
* sts = A variadic array of `Statement`s, that will copied in this class
* The entries themselves will not be copied.
*/
final extern (D) this(const ref Loc loc, Statement[] sts...)
{
super(loc, STMT.Compound);
statements = new Statements();
statements.reserve(sts.length);
foreach (s; sts)
statements.push(s);
}
static CompoundStatement create(const ref Loc loc, Statement s1, Statement s2)
{
return new CompoundStatement(loc, s1, s2);
}
override CompoundStatement syntaxCopy()
{
return new CompoundStatement(loc, Statement.arraySyntaxCopy(statements));
}
override final inout(ReturnStatement) endsWithReturnStatement() inout nothrow pure
{
foreach (s; *statements)
{
if (s)
{
if (inout rs = s.endsWithReturnStatement())
return rs;
}
}
return null;
}
override final inout(Statement) last() inout nothrow pure
{
Statement s = null;
for (size_t i = statements.length; i; --i)
{
s = cast(Statement)(*statements)[i - 1];
if (s)
{
s = cast(Statement)s.last();
if (s)
break;
}
}
return cast(inout)s;
}
override void accept(Visitor v)
{
v.visit(this);
}
version (IN_LLVM)
{
override inout(CompoundAsmStatement) endsWithAsm() inout pure nothrow @nogc
{
// make the last inner statement decide
if (statements && statements.length)
{
size_t last = statements.length - 1;
if (auto s = (*statements)[last])
return s.endsWithAsm();
}
return null;
}
}
}
/***********************************************************
*/
extern (C++) final class CompoundDeclarationStatement : CompoundStatement
{
extern (D) this(const ref Loc loc, Statements* statements) @safe
{
super(loc, statements, STMT.CompoundDeclaration);
}
override CompoundDeclarationStatement syntaxCopy()
{
return new CompoundDeclarationStatement(loc, Statement.arraySyntaxCopy(statements));
}
override 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.
*/
extern (C++) final class UnrolledLoopStatement : Statement
{
Statements* statements;
extern (D) this(const ref Loc loc, Statements* statements) @safe
{
super(loc, STMT.UnrolledLoop);
this.statements = statements;
}
override UnrolledLoopStatement syntaxCopy()
{
return new UnrolledLoopStatement(loc, Statement.arraySyntaxCopy(statements));
}
override bool hasBreak() const pure nothrow
{
return true;
}
override bool hasContinue() const pure nothrow
{
return true;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
*/
extern (C++) final class ScopeStatement : Statement
{
Statement statement;
Loc endloc; // location of closing curly bracket
extern (D) this(const ref Loc loc, Statement statement, Loc endloc) @safe
{
super(loc, STMT.Scope);
this.statement = statement;
this.endloc = endloc;
}
override ScopeStatement syntaxCopy()
{
return new ScopeStatement(loc, statement ? statement.syntaxCopy() : null, endloc);
}
override inout(ReturnStatement) endsWithReturnStatement() inout nothrow pure
{
if (statement)
return statement.endsWithReturnStatement();
return null;
}
override bool hasBreak() const pure nothrow
{
//printf("ScopeStatement::hasBreak() %s\n", toChars());
return statement ? statement.hasBreak() : false;
}
override bool hasContinue() const pure nothrow
{
return statement ? statement.hasContinue() : false;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* Statement whose symbol table contains foreach index variables in a
* local scope and forwards other members to the parent scope. This
* wraps a statement.
*
* Also see: `dmd.attrib.ForwardingAttribDeclaration`
*/
extern (C++) final class ForwardingStatement : Statement
{
/// The symbol containing the `static foreach` variables.
ForwardingScopeDsymbol sym = null;
/// The wrapped statement.
Statement statement;
extern (D) this(const ref Loc loc, ForwardingScopeDsymbol sym, Statement statement) @safe
{
super(loc, STMT.Forwarding);
this.sym = sym;
assert(statement);
this.statement = statement;
}
extern (D) this(const ref Loc loc, Statement statement) @safe
{
auto sym = new ForwardingScopeDsymbol();
sym.symtab = new DsymbolTable();
this(loc, sym, statement);
}
override ForwardingStatement syntaxCopy()
{
return new ForwardingStatement(loc, statement.syntaxCopy());
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/statement.html#while-statement
*/
extern (C++) final class WhileStatement : Statement
{
Parameter param;
Expression condition;
Statement _body;
Loc endloc; // location of closing curly bracket
extern (D) this(const ref Loc loc, Expression condition, Statement _body, Loc endloc, Parameter param = null) @safe
{
super(loc, STMT.While);
this.condition = condition;
this._body = _body;
this.endloc = endloc;
this.param = param;
}
override WhileStatement syntaxCopy()
{
return new WhileStatement(loc,
condition.syntaxCopy(),
_body ? _body.syntaxCopy() : null,
endloc, param ? param.syntaxCopy() : null);
}
override bool hasBreak() const pure nothrow
{
return true;
}
override bool hasContinue() const pure nothrow
{
return true;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/statement.html#do-statement
*/
extern (C++) final class DoStatement : Statement
{
Statement _body;
Expression condition;
Loc endloc; // location of ';' after while
extern (D) this(const ref Loc loc, Statement _body, Expression condition, Loc endloc) @safe
{
super(loc, STMT.Do);
this._body = _body;
this.condition = condition;
this.endloc = endloc;
}
override DoStatement syntaxCopy()
{
return new DoStatement(loc,
_body ? _body.syntaxCopy() : null,
condition.syntaxCopy(),
endloc);
}
override bool hasBreak() const pure nothrow
{
return true;
}
override bool hasContinue() const pure nothrow
{
return true;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/statement.html#for-statement
*/
extern (C++) final class ForStatement : Statement
{
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;
extern (D) this(const ref Loc loc, Statement _init, Expression condition, Expression increment, Statement _body, Loc endloc) @safe
{
super(loc, STMT.For);
this._init = _init;
this.condition = condition;
this.increment = increment;
this._body = _body;
this.endloc = endloc;
}
override ForStatement syntaxCopy()
{
return new ForStatement(loc,
_init ? _init.syntaxCopy() : null,
condition ? condition.syntaxCopy() : null,
increment ? increment.syntaxCopy() : null,
_body.syntaxCopy(),
endloc);
}
override Statement getRelatedLabeled()
{
return relatedLabeled ? relatedLabeled : this;
}
override bool hasBreak() const pure nothrow
{
//printf("ForStatement::hasBreak()\n");
return true;
}
override bool hasContinue() const pure nothrow
{
return true;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/statement.html#foreach-statement
*/
extern (C++) final class ForeachStatement : Statement
{
TOK op; // TOK.foreach_ or TOK.foreach_reverse_
Parameters* parameters; // array of Parameters, one for each ForeachType
Expression aggr; // ForeachAggregate
Statement _body; // NoScopeNonEmptyStatement
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
extern (D) this(const ref Loc loc, TOK op, Parameters* parameters, Expression aggr, Statement _body, Loc endloc) @safe
{
super(loc, STMT.Foreach);
this.op = op;
this.parameters = parameters;
this.aggr = aggr;
this._body = _body;
this.endloc = endloc;
}
override ForeachStatement syntaxCopy()
{
return new ForeachStatement(loc, op,
Parameter.arraySyntaxCopy(parameters),
aggr.syntaxCopy(),
_body ? _body.syntaxCopy() : null,
endloc);
}
override bool hasBreak() const pure nothrow
{
return true;
}
override bool hasContinue() const pure nothrow
{
return true;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/statement.html#foreach-range-statement
*/
extern (C++) final class ForeachRangeStatement : Statement
{
TOK op; // TOK.foreach_ or TOK.foreach_reverse_
Parameter prm; // loop index variable
Expression lwr;
Expression upr;
Statement _body;
Loc endloc; // location of closing curly bracket
VarDeclaration key;
extern (D) this(const ref Loc loc, TOK op, Parameter prm, Expression lwr, Expression upr, Statement _body, Loc endloc) @safe
{
super(loc, STMT.ForeachRange);
this.op = op;
this.prm = prm;
this.lwr = lwr;
this.upr = upr;
this._body = _body;
this.endloc = endloc;
}
override ForeachRangeStatement syntaxCopy()
{
return new ForeachRangeStatement(loc, op, prm.syntaxCopy(), lwr.syntaxCopy(), upr.syntaxCopy(), _body ? _body.syntaxCopy() : null, endloc);
}
override bool hasBreak() const pure nothrow
{
return true;
}
override bool hasContinue() const pure nothrow
{
return true;
}
override void accept(Visitor v)
{
v.visit(this);
}
}
/***********************************************************
* https://dlang.org/spec/statement.html#if-statement
*/
extern (C++) final class IfStatement : Statement
{
Parameter prm;
Expression condition;
Statement ifbody;
Statement elsebody;
VarDeclaration match; // for MatchExpression results
Loc endloc; // location of closing curly bracket
extern (D) this(const ref Loc loc, Parameter prm, Expression condition, Statement ifbody, Statement elsebody, Loc endloc) @safe
{
super(loc, STMT.If);
this.prm = prm;
this.condition = condition;
this.ifbody = ifbody;
this.elsebody = elsebody;
this.endloc = endloc;
}
override IfStatement syntaxCopy()
{
return new IfStatement(loc,
prm ? prm.syntaxCopy() : null,
condition.syntaxCopy(),
ifbody ? ifbody.syntaxCopy() : null,
elsebody ? elsebody.syntaxCopy() : null,
endloc);
}
override void accept(Visitor v)
{
v.visit(this);
}
/******
* Returns: true if `if (__ctfe)`
*/
bool isIfCtfeBlock()
{
if (auto cv = condition.isVarExp())
return cv.var.ident == Id.ctfe;
return false;
}