Coverage Report

Created: 2020-02-18 08:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/AST/Stmt.h
Line
Count
Source (jump to first uncovered line)
1
//===- Stmt.h - Classes for representing statements -------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines the Stmt interface and subclasses.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_AST_STMT_H
14
#define LLVM_CLANG_AST_STMT_H
15
16
#include "clang/AST/DeclGroup.h"
17
#include "clang/AST/StmtIterator.h"
18
#include "clang/Basic/CapturedStmt.h"
19
#include "clang/Basic/IdentifierTable.h"
20
#include "clang/Basic/LLVM.h"
21
#include "clang/Basic/SourceLocation.h"
22
#include "llvm/ADT/ArrayRef.h"
23
#include "llvm/ADT/PointerIntPair.h"
24
#include "llvm/ADT/StringRef.h"
25
#include "llvm/ADT/iterator.h"
26
#include "llvm/ADT/iterator_range.h"
27
#include "llvm/Support/Casting.h"
28
#include "llvm/Support/Compiler.h"
29
#include "llvm/Support/ErrorHandling.h"
30
#include <algorithm>
31
#include <cassert>
32
#include <cstddef>
33
#include <iterator>
34
#include <string>
35
36
namespace llvm {
37
38
class FoldingSetNodeID;
39
40
} // namespace llvm
41
42
namespace clang {
43
44
class ASTContext;
45
class Attr;
46
class CapturedDecl;
47
class Decl;
48
class Expr;
49
class AddrLabelExpr;
50
class LabelDecl;
51
class ODRHash;
52
class PrinterHelper;
53
struct PrintingPolicy;
54
class RecordDecl;
55
class SourceManager;
56
class StringLiteral;
57
class Token;
58
class VarDecl;
59
60
//===----------------------------------------------------------------------===//
61
// AST classes for statements.
62
//===----------------------------------------------------------------------===//
63
64
/// Stmt - This represents one statement.
65
///
66
class alignas(void *) Stmt {
67
public:
68
  enum StmtClass {
69
    NoStmtClass = 0,
70
#define STMT(CLASS, PARENT) CLASS##Class,
71
#define STMT_RANGE(BASE, FIRST, LAST) \
72
        first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
73
#define LAST_STMT_RANGE(BASE, FIRST, LAST) \
74
        first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
75
#define ABSTRACT_STMT(STMT)
76
#include "clang/AST/StmtNodes.inc"
77
  };
78
79
  // Make vanilla 'new' and 'delete' illegal for Stmts.
80
protected:
81
  friend class ASTStmtReader;
82
  friend class ASTStmtWriter;
83
84
0
  void *operator new(size_t bytes) noexcept {
85
0
    llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
86
0
  }
87
88
0
  void operator delete(void *data) noexcept {
89
0
    llvm_unreachable("Stmts cannot be released with regular 'delete'.");
90
0
  }
91
92
  //===--- Statement bitfields classes ---===//
93
94
  class StmtBitfields {
95
    friend class ASTStmtReader;
96
    friend class ASTStmtWriter;
97
    friend class Stmt;
98
99
    /// The statement class.
100
    unsigned sClass : 8;
101
102
    /// This bit is set only for the Stmts that are the structured-block of
103
    /// OpenMP executable directives. Directives that have a structured block
104
    /// are called "non-standalone" directives.
105
    /// I.e. those returned by OMPExecutableDirective::getStructuredBlock().
106
    unsigned IsOMPStructuredBlock : 1;
107
  };
108
  enum { NumStmtBits = 9 };
109
110
  class NullStmtBitfields {
111
    friend class ASTStmtReader;
112
    friend class ASTStmtWriter;
113
    friend class NullStmt;
114
115
    unsigned : NumStmtBits;
116
117
    /// True if the null statement was preceded by an empty macro, e.g:
118
    /// @code
119
    ///   #define CALL(x)
120
    ///   CALL(0);
121
    /// @endcode
122
    unsigned HasLeadingEmptyMacro : 1;
123
124
    /// The location of the semi-colon.
125
    SourceLocation SemiLoc;
126
  };
127
128
  class CompoundStmtBitfields {
129
    friend class ASTStmtReader;
130
    friend class CompoundStmt;
131
132
    unsigned : NumStmtBits;
133
134
    unsigned NumStmts : 32 - NumStmtBits;
135
136
    /// The location of the opening "{".
137
    SourceLocation LBraceLoc;
138
  };
139
140
  class LabelStmtBitfields {
141
    friend class LabelStmt;
142
143
    unsigned : NumStmtBits;
144
145
    SourceLocation IdentLoc;
146
  };
147
148
  class AttributedStmtBitfields {
149
    friend class ASTStmtReader;
150
    friend class AttributedStmt;
151
152
    unsigned : NumStmtBits;
153
154
    /// Number of attributes.
155
    unsigned NumAttrs : 32 - NumStmtBits;
156
157
    /// The location of the attribute.
158
    SourceLocation AttrLoc;
159
  };
160
161
  class IfStmtBitfields {
162
    friend class ASTStmtReader;
163
    friend class IfStmt;
164
165
    unsigned : NumStmtBits;
166
167
    /// True if this if statement is a constexpr if.
168
    unsigned IsConstexpr : 1;
169
170
    /// True if this if statement has storage for an else statement.
171
    unsigned HasElse : 1;
172
173
    /// True if this if statement has storage for a variable declaration.
174
    unsigned HasVar : 1;
175
176
    /// True if this if statement has storage for an init statement.
177
    unsigned HasInit : 1;
178
179
    /// The location of the "if".
180
    SourceLocation IfLoc;
181
  };
182
183
  class SwitchStmtBitfields {
184
    friend class SwitchStmt;
185
186
    unsigned : NumStmtBits;
187
188
    /// True if the SwitchStmt has storage for an init statement.
189
    unsigned HasInit : 1;
190
191
    /// True if the SwitchStmt has storage for a condition variable.
192
    unsigned HasVar : 1;
193
194
    /// If the SwitchStmt is a switch on an enum value, records whether all
195
    /// the enum values were covered by CaseStmts.  The coverage information
196
    /// value is meant to be a hint for possible clients.
197
    unsigned AllEnumCasesCovered : 1;
198
199
    /// The location of the "switch".
200
    SourceLocation SwitchLoc;
201
  };
202
203
  class WhileStmtBitfields {
204
    friend class ASTStmtReader;
205
    friend class WhileStmt;
206
207
    unsigned : NumStmtBits;
208
209
    /// True if the WhileStmt has storage for a condition variable.
210
    unsigned HasVar : 1;
211
212
    /// The location of the "while".
213
    SourceLocation WhileLoc;
214
  };
215
216
  class DoStmtBitfields {
217
    friend class DoStmt;
218
219
    unsigned : NumStmtBits;
220
221
    /// The location of the "do".
222
    SourceLocation DoLoc;
223
  };
224
225
  class ForStmtBitfields {
226
    friend class ForStmt;
227
228
    unsigned : NumStmtBits;
229
230
    /// The location of the "for".
231
    SourceLocation ForLoc;
232
  };
233
234
  class GotoStmtBitfields {
235
    friend class GotoStmt;
236
    friend class IndirectGotoStmt;
237
238
    unsigned : NumStmtBits;
239
240
    /// The location of the "goto".
241
    SourceLocation GotoLoc;
242
  };
243
244
  class ContinueStmtBitfields {
245
    friend class ContinueStmt;
246
247
    unsigned : NumStmtBits;
248
249
    /// The location of the "continue".
250
    SourceLocation ContinueLoc;
251
  };
252
253
  class BreakStmtBitfields {
254
    friend class BreakStmt;
255
256
    unsigned : NumStmtBits;
257
258
    /// The location of the "break".
259
    SourceLocation BreakLoc;
260
  };
261
262
  class ReturnStmtBitfields {
263
    friend class ReturnStmt;
264
265
    unsigned : NumStmtBits;
266
267
    /// True if this ReturnStmt has storage for an NRVO candidate.
268
    unsigned HasNRVOCandidate : 1;
269
270
    /// The location of the "return".
271
    SourceLocation RetLoc;
272
  };
273
274
  class SwitchCaseBitfields {
275
    friend class SwitchCase;
276
    friend class CaseStmt;
277
278
    unsigned : NumStmtBits;
279
280
    /// Used by CaseStmt to store whether it is a case statement
281
    /// of the form case LHS ... RHS (a GNU extension).
282
    unsigned CaseStmtIsGNURange : 1;
283
284
    /// The location of the "case" or "default" keyword.
285
    SourceLocation KeywordLoc;
286
  };
287
288
  //===--- Expression bitfields classes ---===//
289
290
  class ExprBitfields {
291
    friend class ASTStmtReader; // deserialization
292
    friend class AtomicExpr; // ctor
293
    friend class BlockDeclRefExpr; // ctor
294
    friend class CallExpr; // ctor
295
    friend class CXXConstructExpr; // ctor
296
    friend class CXXDependentScopeMemberExpr; // ctor
297
    friend class CXXNewExpr; // ctor
298
    friend class CXXUnresolvedConstructExpr; // ctor
299
    friend class DeclRefExpr; // computeDependence
300
    friend class DependentScopeDeclRefExpr; // ctor
301
    friend class DesignatedInitExpr; // ctor
302
    friend class Expr;
303
    friend class InitListExpr; // ctor
304
    friend class ObjCArrayLiteral; // ctor
305
    friend class ObjCDictionaryLiteral; // ctor
306
    friend class ObjCMessageExpr; // ctor
307
    friend class OffsetOfExpr; // ctor
308
    friend class OpaqueValueExpr; // ctor
309
    friend class OverloadExpr; // ctor
310
    friend class ParenListExpr; // ctor
311
    friend class PseudoObjectExpr; // ctor
312
    friend class ShuffleVectorExpr; // ctor
313
314
    unsigned : NumStmtBits;
315
316
    unsigned ValueKind : 2;
317
    unsigned ObjectKind : 3;
318
    unsigned TypeDependent : 1;
319
    unsigned ValueDependent : 1;
320
    unsigned InstantiationDependent : 1;
321
    unsigned ContainsUnexpandedParameterPack : 1;
322
  };
323
  enum { NumExprBits = NumStmtBits + 9 };
324
325
  class ConstantExprBitfields {
326
    friend class ASTStmtReader;
327
    friend class ASTStmtWriter;
328
    friend class ConstantExpr;
329
330
    unsigned : NumExprBits;
331
332
    /// The kind of result that is trail-allocated.
333
    unsigned ResultKind : 2;
334
335
    /// Kind of Result as defined by APValue::Kind
336
    unsigned APValueKind : 4;
337
338
    /// When ResultKind == RSK_Int64. whether the trail-allocated integer is
339
    /// signed.
340
    unsigned IsUnsigned : 1;
341
342
    /// When ResultKind == RSK_Int64. the BitWidth of the trail-allocated
343
    /// integer. 7 bits because it is the minimal number of bit to represent a
344
    /// value from 0 to 64 (the size of the trail-allocated number).
345
    unsigned BitWidth : 7;
346
347
    /// When ResultKind == RSK_APValue. Wether the ASTContext will cleanup the
348
    /// destructor on the trail-allocated APValue.
349
    unsigned HasCleanup : 1;
350
351
    /// Whether this ConstantExpr was created for immediate invocation.
352
    unsigned IsImmediateInvocation : 1;
353
  };
354
355
  class PredefinedExprBitfields {
356
    friend class ASTStmtReader;
357
    friend class PredefinedExpr;
358
359
    unsigned : NumExprBits;
360
361
    /// The kind of this PredefinedExpr. One of the enumeration values
362
    /// in PredefinedExpr::IdentKind.
363
    unsigned Kind : 4;
364
365
    /// True if this PredefinedExpr has a trailing "StringLiteral *"
366
    /// for the predefined identifier.
367
    unsigned HasFunctionName : 1;
368
369
    /// The location of this PredefinedExpr.
370
    SourceLocation Loc;
371
  };
372
373
  class DeclRefExprBitfields {
374
    friend class ASTStmtReader; // deserialization
375
    friend class DeclRefExpr;
376
377
    unsigned : NumExprBits;
378
379
    unsigned HasQualifier : 1;
380
    unsigned HasTemplateKWAndArgsInfo : 1;
381
    unsigned HasFoundDecl : 1;
382
    unsigned HadMultipleCandidates : 1;
383
    unsigned RefersToEnclosingVariableOrCapture : 1;
384
    unsigned NonOdrUseReason : 2;
385
386
    /// The location of the declaration name itself.
387
    SourceLocation Loc;
388
  };
389
390
391
  class FloatingLiteralBitfields {
392
    friend class FloatingLiteral;
393
394
    unsigned : NumExprBits;
395
396
    unsigned Semantics : 3; // Provides semantics for APFloat construction
397
    unsigned IsExact : 1;
398
  };
399
400
  class StringLiteralBitfields {
401
    friend class ASTStmtReader;
402
    friend class StringLiteral;
403
404
    unsigned : NumExprBits;
405
406
    /// The kind of this string literal.
407
    /// One of the enumeration values of StringLiteral::StringKind.
408
    unsigned Kind : 3;
409
410
    /// The width of a single character in bytes. Only values of 1, 2,
411
    /// and 4 bytes are supported. StringLiteral::mapCharByteWidth maps
412
    /// the target + string kind to the appropriate CharByteWidth.
413
    unsigned CharByteWidth : 3;
414
415
    unsigned IsPascal : 1;
416
417
    /// The number of concatenated token this string is made of.
418
    /// This is the number of trailing SourceLocation.
419
    unsigned NumConcatenated;
420
  };
421
422
  class CharacterLiteralBitfields {
423
    friend class CharacterLiteral;
424
425
    unsigned : NumExprBits;
426
427
    unsigned Kind : 3;
428
  };
429
430
  class UnaryOperatorBitfields {
431
    friend class UnaryOperator;
432
433
    unsigned : NumExprBits;
434
435
    unsigned Opc : 5;
436
    unsigned CanOverflow : 1;
437
438
    SourceLocation Loc;
439
  };
440
441
  class UnaryExprOrTypeTraitExprBitfields {
442
    friend class UnaryExprOrTypeTraitExpr;
443
444
    unsigned : NumExprBits;
445
446
    unsigned Kind : 3;
447
    unsigned IsType : 1; // true if operand is a type, false if an expression.
448
  };
449
450
  class ArraySubscriptExprBitfields {
451
    friend class ArraySubscriptExpr;
452
453
    unsigned : NumExprBits;
454
455
    SourceLocation RBracketLoc;
456
  };
457
458
  class CallExprBitfields {
459
    friend class CallExpr;
460
461
    unsigned : NumExprBits;
462
463
    unsigned NumPreArgs : 1;
464
465
    /// True if the callee of the call expression was found using ADL.
466
    unsigned UsesADL : 1;
467
468
    /// Padding used to align OffsetToTrailingObjects to a byte multiple.
469
    unsigned : 24 - 2 - NumExprBits;
470
471
    /// The offset in bytes from the this pointer to the start of the
472
    /// trailing objects belonging to CallExpr. Intentionally byte sized
473
    /// for faster access.
474
    unsigned OffsetToTrailingObjects : 8;
475
  };
476
  enum { NumCallExprBits = 32 };
477
478
  class MemberExprBitfields {
479
    friend class ASTStmtReader;
480
    friend class MemberExpr;
481
482
    unsigned : NumExprBits;
483
484
    /// IsArrow - True if this is "X->F", false if this is "X.F".
485
    unsigned IsArrow : 1;
486
487
    /// True if this member expression used a nested-name-specifier to
488
    /// refer to the member, e.g., "x->Base::f", or found its member via
489
    /// a using declaration.  When true, a MemberExprNameQualifier
490
    /// structure is allocated immediately after the MemberExpr.
491
    unsigned HasQualifierOrFoundDecl : 1;
492
493
    /// True if this member expression specified a template keyword
494
    /// and/or a template argument list explicitly, e.g., x->f<int>,
495
    /// x->template f, x->template f<int>.
496
    /// When true, an ASTTemplateKWAndArgsInfo structure and its
497
    /// TemplateArguments (if any) are present.
498
    unsigned HasTemplateKWAndArgsInfo : 1;
499
500
    /// True if this member expression refers to a method that
501
    /// was resolved from an overloaded set having size greater than 1.
502
    unsigned HadMultipleCandidates : 1;
503
504
    /// Value of type NonOdrUseReason indicating why this MemberExpr does
505
    /// not constitute an odr-use of the named declaration. Meaningful only
506
    /// when naming a static member.
507
    unsigned NonOdrUseReason : 2;
508
509
    /// This is the location of the -> or . in the expression.
510
    SourceLocation OperatorLoc;
511
  };
512
513
  class CastExprBitfields {
514
    friend class CastExpr;
515
    friend class ImplicitCastExpr;
516
517
    unsigned : NumExprBits;
518
519
    unsigned Kind : 6;
520
    unsigned PartOfExplicitCast : 1; // Only set for ImplicitCastExpr.
521
522
    /// The number of CXXBaseSpecifiers in the cast. 14 bits would be enough
523
    /// here. ([implimits] Direct and indirect base classes [16384]).
524
    unsigned BasePathSize;
525
  };
526
527
  class BinaryOperatorBitfields {
528
    friend class BinaryOperator;
529
530
    unsigned : NumExprBits;
531
532
    unsigned Opc : 6;
533
534
    /// This is only meaningful for operations on floating point
535
    /// types and 0 otherwise.
536
    unsigned FPFeatures : 8;
537
538
    SourceLocation OpLoc;
539
  };
540
541
  class InitListExprBitfields {
542
    friend class InitListExpr;
543
544
    unsigned : NumExprBits;
545
546
    /// Whether this initializer list originally had a GNU array-range
547
    /// designator in it. This is a temporary marker used by CodeGen.
548
    unsigned HadArrayRangeDesignator : 1;
549
  };
550
551
  class ParenListExprBitfields {
552
    friend class ASTStmtReader;
553
    friend class ParenListExpr;
554
555
    unsigned : NumExprBits;
556
557
    /// The number of expressions in the paren list.
558
    unsigned NumExprs;
559
  };
560
561
  class GenericSelectionExprBitfields {
562
    friend class ASTStmtReader;
563
    friend class GenericSelectionExpr;
564
565
    unsigned : NumExprBits;
566
567
    /// The location of the "_Generic".
568
    SourceLocation GenericLoc;
569
  };
570
571
  class PseudoObjectExprBitfields {
572
    friend class ASTStmtReader; // deserialization
573
    friend class PseudoObjectExpr;
574
575
    unsigned : NumExprBits;
576
577
    // These don't need to be particularly wide, because they're
578
    // strictly limited by the forms of expressions we permit.
579
    unsigned NumSubExprs : 8;
580
    unsigned ResultIndex : 32 - 8 - NumExprBits;
581
  };
582
583
  class SourceLocExprBitfields {
584
    friend class ASTStmtReader;
585
    friend class SourceLocExpr;
586
587
    unsigned : NumExprBits;
588
589
    /// The kind of source location builtin represented by the SourceLocExpr.
590
    /// Ex. __builtin_LINE, __builtin_FUNCTION, ect.
591
    unsigned Kind : 2;
592
  };
593
594
  //===--- C++ Expression bitfields classes ---===//
595
596
  class CXXOperatorCallExprBitfields {
597
    friend class ASTStmtReader;
598
    friend class CXXOperatorCallExpr;
599
600
    unsigned : NumCallExprBits;
601
602
    /// The kind of this overloaded operator. One of the enumerator
603
    /// value of OverloadedOperatorKind.
604
    unsigned OperatorKind : 6;
605
606
    // Only meaningful for floating point types.
607
    unsigned FPFeatures : 8;
608
  };
609
610
  class CXXRewrittenBinaryOperatorBitfields {
611
    friend class ASTStmtReader;
612
    friend class CXXRewrittenBinaryOperator;
613
614
    unsigned : NumCallExprBits;
615
616
    unsigned IsReversed : 1;
617
  };
618
619
  class CXXBoolLiteralExprBitfields {
620
    friend class CXXBoolLiteralExpr;
621
622
    unsigned : NumExprBits;
623
624
    /// The value of the boolean literal.
625
    unsigned Value : 1;
626
627
    /// The location of the boolean literal.
628
    SourceLocation Loc;
629
  };
630
631
  class CXXNullPtrLiteralExprBitfields {
632
    friend class CXXNullPtrLiteralExpr;
633
634
    unsigned : NumExprBits;
635
636
    /// The location of the null pointer literal.
637
    SourceLocation Loc;
638
  };
639
640
  class CXXThisExprBitfields {
641
    friend class CXXThisExpr;
642
643
    unsigned : NumExprBits;
644
645
    /// Whether this is an implicit "this".
646
    unsigned IsImplicit : 1;
647
648
    /// The location of the "this".
649
    SourceLocation Loc;
650
  };
651
652
  class CXXThrowExprBitfields {
653
    friend class ASTStmtReader;
654
    friend class CXXThrowExpr;
655
656
    unsigned : NumExprBits;
657
658
    /// Whether the thrown variable (if any) is in scope.
659
    unsigned IsThrownVariableInScope : 1;
660
661
    /// The location of the "throw".
662
    SourceLocation ThrowLoc;
663
  };
664
665
  class CXXDefaultArgExprBitfields {
666
    friend class ASTStmtReader;
667
    friend class CXXDefaultArgExpr;
668
669
    unsigned : NumExprBits;
670
671
    /// The location where the default argument expression was used.
672
    SourceLocation Loc;
673
  };
674
675
  class CXXDefaultInitExprBitfields {
676
    friend class ASTStmtReader;
677
    friend class CXXDefaultInitExpr;
678
679
    unsigned : NumExprBits;
680
681
    /// The location where the default initializer expression was used.
682
    SourceLocation Loc;
683
  };
684
685
  class CXXScalarValueInitExprBitfields {
686
    friend class ASTStmtReader;
687
    friend class CXXScalarValueInitExpr;
688
689
    unsigned : NumExprBits;
690
691
    SourceLocation RParenLoc;
692
  };
693
694
  class CXXNewExprBitfields {
695
    friend class ASTStmtReader;
696
    friend class ASTStmtWriter;
697
    friend class CXXNewExpr;
698
699
    unsigned : NumExprBits;
700
701
    /// Was the usage ::new, i.e. is the global new to be used?
702
    unsigned IsGlobalNew : 1;
703
704
    /// Do we allocate an array? If so, the first trailing "Stmt *" is the
705
    /// size expression.
706
    unsigned IsArray : 1;
707
708
    /// Should the alignment be passed to the allocation function?
709
    unsigned ShouldPassAlignment : 1;
710
711
    /// If this is an array allocation, does the usual deallocation
712
    /// function for the allocated type want to know the allocated size?
713
    unsigned UsualArrayDeleteWantsSize : 1;
714
715
    /// What kind of initializer do we have? Could be none, parens, or braces.
716
    /// In storage, we distinguish between "none, and no initializer expr", and
717
    /// "none, but an implicit initializer expr".
718
    unsigned StoredInitializationStyle : 2;
719
720
    /// True if the allocated type was expressed as a parenthesized type-id.
721
    unsigned IsParenTypeId : 1;
722
723
    /// The number of placement new arguments.
724
    unsigned NumPlacementArgs;
725
  };
726
727
  class CXXDeleteExprBitfields {
728
    friend class ASTStmtReader;
729
    friend class CXXDeleteExpr;
730
731
    unsigned : NumExprBits;
732
733
    /// Is this a forced global delete, i.e. "::delete"?
734
    unsigned GlobalDelete : 1;
735
736
    /// Is this the array form of delete, i.e. "delete[]"?
737
    unsigned ArrayForm : 1;
738
739
    /// ArrayFormAsWritten can be different from ArrayForm if 'delete' is
740
    /// applied to pointer-to-array type (ArrayFormAsWritten will be false
741
    /// while ArrayForm will be true).
742
    unsigned ArrayFormAsWritten : 1;
743
744
    /// Does the usual deallocation function for the element type require
745
    /// a size_t argument?
746
    unsigned UsualArrayDeleteWantsSize : 1;
747
748
    /// Location of the expression.
749
    SourceLocation Loc;
750
  };
751
752
  class TypeTraitExprBitfields {
753
    friend class ASTStmtReader;
754
    friend class ASTStmtWriter;
755
    friend class TypeTraitExpr;
756
757
    unsigned : NumExprBits;
758
759
    /// The kind of type trait, which is a value of a TypeTrait enumerator.
760
    unsigned Kind : 8;
761
762
    /// If this expression is not value-dependent, this indicates whether
763
    /// the trait evaluated true or false.
764
    unsigned Value : 1;
765
766
    /// The number of arguments to this type trait.
767
    unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
768
  };
769
770
  class DependentScopeDeclRefExprBitfields {
771
    friend class ASTStmtReader;
772
    friend class ASTStmtWriter;
773
    friend class DependentScopeDeclRefExpr;
774
775
    unsigned : NumExprBits;
776
777
    /// Whether the name includes info for explicit template
778
    /// keyword and arguments.
779
    unsigned HasTemplateKWAndArgsInfo : 1;
780
  };
781
782
  class CXXConstructExprBitfields {
783
    friend class ASTStmtReader;
784
    friend class CXXConstructExpr;
785
786
    unsigned : NumExprBits;
787
788
    unsigned Elidable : 1;
789
    unsigned HadMultipleCandidates : 1;
790
    unsigned ListInitialization : 1;
791
    unsigned StdInitListInitialization : 1;
792
    unsigned ZeroInitialization : 1;
793
    unsigned ConstructionKind : 3;
794
795
    SourceLocation Loc;
796
  };
797
798
  class ExprWithCleanupsBitfields {
799
    friend class ASTStmtReader; // deserialization
800
    friend class ExprWithCleanups;
801
802
    unsigned : NumExprBits;
803
804
    // When false, it must not have side effects.
805
    unsigned CleanupsHaveSideEffects : 1;
806
807
    unsigned NumObjects : 32 - 1 - NumExprBits;
808
  };
809
810
  class CXXUnresolvedConstructExprBitfields {
811
    friend class ASTStmtReader;
812
    friend class CXXUnresolvedConstructExpr;
813
814
    unsigned : NumExprBits;
815
816
    /// The number of arguments used to construct the type.
817
    unsigned NumArgs;
818
  };
819
820
  class CXXDependentScopeMemberExprBitfields {
821
    friend class ASTStmtReader;
822
    friend class CXXDependentScopeMemberExpr;
823
824
    unsigned : NumExprBits;
825
826
    /// Whether this member expression used the '->' operator or
827
    /// the '.' operator.
828
    unsigned IsArrow : 1;
829
830
    /// Whether this member expression has info for explicit template
831
    /// keyword and arguments.
832
    unsigned HasTemplateKWAndArgsInfo : 1;
833
834
    /// See getFirstQualifierFoundInScope() and the comment listing
835
    /// the trailing objects.
836
    unsigned HasFirstQualifierFoundInScope : 1;
837
838
    /// The location of the '->' or '.' operator.
839
    SourceLocation OperatorLoc;
840
  };
841
842
  class OverloadExprBitfields {
843
    friend class ASTStmtReader;
844
    friend class OverloadExpr;
845
846
    unsigned : NumExprBits;
847
848
    /// Whether the name includes info for explicit template
849
    /// keyword and arguments.
850
    unsigned HasTemplateKWAndArgsInfo : 1;
851
852
    /// Padding used by the derived classes to store various bits. If you
853
    /// need to add some data here, shrink this padding and add your data
854
    /// above. NumOverloadExprBits also needs to be updated.
855
    unsigned : 32 - NumExprBits - 1;
856
857
    /// The number of results.
858
    unsigned NumResults;
859
  };
860
  enum { NumOverloadExprBits = NumExprBits + 1 };
861
862
  class UnresolvedLookupExprBitfields {
863
    friend class ASTStmtReader;
864
    friend class UnresolvedLookupExpr;
865
866
    unsigned : NumOverloadExprBits;
867
868
    /// True if these lookup results should be extended by
869
    /// argument-dependent lookup if this is the operand of a function call.
870
    unsigned RequiresADL : 1;
871
872
    /// True if these lookup results are overloaded.  This is pretty trivially
873
    /// rederivable if we urgently need to kill this field.
874
    unsigned Overloaded : 1;
875
  };
876
  static_assert(sizeof(UnresolvedLookupExprBitfields) <= 4,
877
                "UnresolvedLookupExprBitfields must be <= than 4 bytes to"
878
                "avoid trashing OverloadExprBitfields::NumResults!");
879
880
  class UnresolvedMemberExprBitfields {
881
    friend class ASTStmtReader;
882
    friend class UnresolvedMemberExpr;
883
884
    unsigned : NumOverloadExprBits;
885
886
    /// Whether this member expression used the '->' operator or
887
    /// the '.' operator.
888
    unsigned IsArrow : 1;
889
890
    /// Whether the lookup results contain an unresolved using declaration.
891
    unsigned HasUnresolvedUsing : 1;
892
  };
893
  static_assert(sizeof(UnresolvedMemberExprBitfields) <= 4,
894
                "UnresolvedMemberExprBitfields must be <= than 4 bytes to"
895
                "avoid trashing OverloadExprBitfields::NumResults!");
896
897
  class CXXNoexceptExprBitfields {
898
    friend class ASTStmtReader;
899
    friend class CXXNoexceptExpr;
900
901
    unsigned : NumExprBits;
902
903
    unsigned Value : 1;
904
  };
905
906
  class SubstNonTypeTemplateParmExprBitfields {
907
    friend class ASTStmtReader;
908
    friend class SubstNonTypeTemplateParmExpr;
909
910
    unsigned : NumExprBits;
911
912
    /// The location of the non-type template parameter reference.
913
    SourceLocation NameLoc;
914
  };
915
916
  class RequiresExprBitfields {
917
    friend class ASTStmtReader;
918
    friend class ASTStmtWriter;
919
    friend class RequiresExpr;
920
921
    unsigned : NumExprBits;
922
923
    unsigned IsSatisfied : 1;
924
    SourceLocation RequiresKWLoc;
925
  };
926
927
  //===--- C++ Coroutines TS bitfields classes ---===//
928
929
  class CoawaitExprBitfields {
930
    friend class CoawaitExpr;
931
932
    unsigned : NumExprBits;
933
934
    unsigned IsImplicit : 1;
935
  };
936
937
  //===--- Obj-C Expression bitfields classes ---===//
938
939
  class ObjCIndirectCopyRestoreExprBitfields {
940
    friend class ObjCIndirectCopyRestoreExpr;
941
942
    unsigned : NumExprBits;
943
944
    unsigned ShouldCopy : 1;
945
  };
946
947
  //===--- Clang Extensions bitfields classes ---===//
948
949
  class OpaqueValueExprBitfields {
950
    friend class ASTStmtReader;
951
    friend class OpaqueValueExpr;
952
953
    unsigned : NumExprBits;
954
955
    /// The OVE is a unique semantic reference to its source expression if this
956
    /// bit is set to true.
957
    unsigned IsUnique : 1;
958
959
    SourceLocation Loc;
960
  };
961
962
  union {
963
    // Same order as in StmtNodes.td.
964
    // Statements
965
    StmtBitfields StmtBits;
966
    NullStmtBitfields NullStmtBits;
967
    CompoundStmtBitfields CompoundStmtBits;
968
    LabelStmtBitfields LabelStmtBits;
969
    AttributedStmtBitfields AttributedStmtBits;
970
    IfStmtBitfields IfStmtBits;
971
    SwitchStmtBitfields SwitchStmtBits;
972
    WhileStmtBitfields WhileStmtBits;
973
    DoStmtBitfields DoStmtBits;
974
    ForStmtBitfields ForStmtBits;
975
    GotoStmtBitfields GotoStmtBits;
976
    ContinueStmtBitfields ContinueStmtBits;
977
    BreakStmtBitfields BreakStmtBits;
978
    ReturnStmtBitfields ReturnStmtBits;
979
    SwitchCaseBitfields SwitchCaseBits;
980
981
    // Expressions
982
    ExprBitfields ExprBits;
983
    ConstantExprBitfields ConstantExprBits;
984
    PredefinedExprBitfields PredefinedExprBits;
985
    DeclRefExprBitfields DeclRefExprBits;
986
    FloatingLiteralBitfields FloatingLiteralBits;
987
    StringLiteralBitfields StringLiteralBits;
988
    CharacterLiteralBitfields CharacterLiteralBits;
989
    UnaryOperatorBitfields UnaryOperatorBits;
990
    UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
991
    ArraySubscriptExprBitfields ArraySubscriptExprBits;
992
    CallExprBitfields CallExprBits;
993
    MemberExprBitfields MemberExprBits;
994
    CastExprBitfields CastExprBits;
995
    BinaryOperatorBitfields BinaryOperatorBits;
996
    InitListExprBitfields InitListExprBits;
997
    ParenListExprBitfields ParenListExprBits;
998
    GenericSelectionExprBitfields GenericSelectionExprBits;
999
    PseudoObjectExprBitfields PseudoObjectExprBits;
1000
    SourceLocExprBitfields SourceLocExprBits;
1001
1002
    // C++ Expressions
1003
    CXXOperatorCallExprBitfields CXXOperatorCallExprBits;
1004
    CXXRewrittenBinaryOperatorBitfields CXXRewrittenBinaryOperatorBits;
1005
    CXXBoolLiteralExprBitfields CXXBoolLiteralExprBits;
1006
    CXXNullPtrLiteralExprBitfields CXXNullPtrLiteralExprBits;
1007
    CXXThisExprBitfields CXXThisExprBits;
1008
    CXXThrowExprBitfields CXXThrowExprBits;
1009
    CXXDefaultArgExprBitfields CXXDefaultArgExprBits;
1010
    CXXDefaultInitExprBitfields CXXDefaultInitExprBits;
1011
    CXXScalarValueInitExprBitfields CXXScalarValueInitExprBits;
1012
    CXXNewExprBitfields CXXNewExprBits;
1013
    CXXDeleteExprBitfields CXXDeleteExprBits;
1014
    TypeTraitExprBitfields TypeTraitExprBits;
1015
    DependentScopeDeclRefExprBitfields DependentScopeDeclRefExprBits;
1016
    CXXConstructExprBitfields CXXConstructExprBits;
1017
    ExprWithCleanupsBitfields ExprWithCleanupsBits;
1018
    CXXUnresolvedConstructExprBitfields CXXUnresolvedConstructExprBits;
1019
    CXXDependentScopeMemberExprBitfields CXXDependentScopeMemberExprBits;
1020
    OverloadExprBitfields OverloadExprBits;
1021
    UnresolvedLookupExprBitfields UnresolvedLookupExprBits;
1022
    UnresolvedMemberExprBitfields UnresolvedMemberExprBits;
1023
    CXXNoexceptExprBitfields CXXNoexceptExprBits;
1024
    SubstNonTypeTemplateParmExprBitfields SubstNonTypeTemplateParmExprBits;
1025
    RequiresExprBitfields RequiresExprBits;
1026
1027
    // C++ Coroutines TS expressions
1028
    CoawaitExprBitfields CoawaitBits;
1029
1030
    // Obj-C Expressions
1031
    ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
1032
1033
    // Clang Extensions
1034
    OpaqueValueExprBitfields OpaqueValueExprBits;
1035
  };
1036
1037
public:
1038
  // Only allow allocation of Stmts using the allocator in ASTContext
1039
  // or by doing a placement new.
1040
  void* operator new(size_t bytes, const ASTContext& C,
1041
                     unsigned alignment = 8);
1042
1043
  void* operator new(size_t bytes, const ASTContext* C,
1044
9.90k
                     unsigned alignment = 8) {
1045
9.90k
    return operator new(bytes, *C, alignment);
1046
9.90k
  }
1047
1048
66.3M
  void *operator new(size_t bytes, void *mem) noexcept { return mem; }
1049
1050
0
  void operator delete(void *, const ASTContext &, unsigned) noexcept {}
1051
0
  void operator delete(void *, const ASTContext *, unsigned) noexcept {}
1052
0
  void operator delete(void *, size_t) noexcept {}
1053
0
  void operator delete(void *, void *) noexcept {}
1054
1055
public:
1056
  /// A placeholder type used to construct an empty shell of a
1057
  /// type, that will be filled in later (e.g., by some
1058
  /// de-serialization).
1059
  struct EmptyShell {};
1060
1061
protected:
1062
  /// Iterator for iterating over Stmt * arrays that contain only T *.
1063
  ///
1064
  /// This is needed because AST nodes use Stmt* arrays to store
1065
  /// references to children (to be compatible with StmtIterator).
1066
  template<typename T, typename TPtr = T *, typename StmtPtr = Stmt *>
1067
  struct CastIterator
1068
      : llvm::iterator_adaptor_base<CastIterator<T, TPtr, StmtPtr>, StmtPtr *,
1069
                                    std::random_access_iterator_tag, TPtr> {
1070
    using Base = typename CastIterator::iterator_adaptor_base;
1071
1072
    CastIterator() : Base(nullptr) {}
1073
6.04M
    CastIterator(StmtPtr *I) : Base(I) {}
clang::Stmt::CastIterator<clang::Expr, clang::Expr const* const, clang::Stmt const* const>::CastIterator(clang::Stmt const* const*)
Line
Count
Source
1073
725k
    CastIterator(StmtPtr *I) : Base(I) {}
clang::Stmt::CastIterator<clang::Expr, clang::Expr*, clang::Stmt*>::CastIterator(clang::Stmt**)
Line
Count
Source
1073
5.31M
    CastIterator(StmtPtr *I) : Base(I) {}
clang::Stmt::CastIterator<clang::AddrLabelExpr, clang::AddrLabelExpr const* const, clang::Stmt const* const>::CastIterator(clang::Stmt const* const*)
Line
Count
Source
1073
280
    CastIterator(StmtPtr *I) : Base(I) {}
clang::Stmt::CastIterator<clang::AddrLabelExpr, clang::AddrLabelExpr*, clang::Stmt*>::CastIterator(clang::Stmt**)
Line
Count
Source
1073
250
    CastIterator(StmtPtr *I) : Base(I) {}
1074
1075
6.51M
    typename Base::value_type operator*() const {
1076
6.51M
      return cast_or_null<T>(*this->I);
1077
6.51M
    }
clang::Stmt::CastIterator<clang::Expr, clang::Expr const* const, clang::Stmt const* const>::operator*() const
Line
Count
Source
1075
1.78M
    typename Base::value_type operator*() const {
1076
1.78M
      return cast_or_null<T>(*this->I);
1077
1.78M
    }
clang::Stmt::CastIterator<clang::Expr, clang::Expr*, clang::Stmt*>::operator*() const
Line
Count
Source
1075
4.72M
    typename Base::value_type operator*() const {
1076
4.72M
      return cast_or_null<T>(*this->I);
1077
4.72M
    }
clang::Stmt::CastIterator<clang::AddrLabelExpr, clang::AddrLabelExpr const* const, clang::Stmt const* const>::operator*() const
Line
Count
Source
1075
85
    typename Base::value_type operator*() const {
1076
85
      return cast_or_null<T>(*this->I);
1077
85
    }
clang::Stmt::CastIterator<clang::AddrLabelExpr, clang::AddrLabelExpr*, clang::Stmt*>::operator*() const
Line
Count
Source
1075
21
    typename Base::value_type operator*() const {
1076
21
      return cast_or_null<T>(*this->I);
1077
21
    }
1078
  };
1079
1080
  /// Const iterator for iterating over Stmt * arrays that contain only T *.
1081
  template <typename T>
1082
  using ConstCastIterator = CastIterator<T, const T *const, const Stmt *const>;
1083
1084
  using ExprIterator = CastIterator<Expr>;
1085
  using ConstExprIterator = ConstCastIterator<Expr>;
1086
1087
private:
1088
  /// Whether statistic collection is enabled.
1089
  static bool StatisticsEnabled;
1090
1091
protected:
1092
  /// Construct an empty statement.
1093
99.0k
  explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
1094
1095
public:
1096
  Stmt() = delete;
1097
  Stmt(const Stmt &) = delete;
1098
  Stmt(Stmt &&) = delete;
1099
  Stmt &operator=(const Stmt &) = delete;
1100
  Stmt &operator=(Stmt &&) = delete;
1101
1102
94.5M
  Stmt(StmtClass SC) {
1103
94.5M
    static_assert(sizeof(*this) <= 8,
1104
94.5M
                  "changing bitfields changed sizeof(Stmt)");
1105
94.5M
    static_assert(sizeof(*this) % alignof(void *) == 0,
1106
94.5M
                  "Insufficient alignment!");
1107
94.5M
    StmtBits.sClass = SC;
1108
94.5M
    StmtBits.IsOMPStructuredBlock = false;
1109
94.5M
    if (StatisticsEnabled) 
Stmt::addStmtClass(SC)13
;
1110
94.5M
  }
1111
1112
5.51G
  StmtClass getStmtClass() const {
1113
5.51G
    return static_cast<StmtClass>(StmtBits.sClass);
1114
5.51G
  }
1115
1116
  const char *getStmtClassName() const;
1117
1118
22.9k
  bool isOMPStructuredBlock() const { return StmtBits.IsOMPStructuredBlock; }
1119
2.03M
  void setIsOMPStructuredBlock(bool IsOMPStructuredBlock) {
1120
2.03M
    StmtBits.IsOMPStructuredBlock = IsOMPStructuredBlock;
1121
2.03M
  }
1122
1123
  /// SourceLocation tokens are not useful in isolation - they are low level
1124
  /// value objects created/interpreted by SourceManager. We assume AST
1125
  /// clients will have a pointer to the respective SourceManager.
1126
  SourceRange getSourceRange() const LLVM_READONLY;
1127
  SourceLocation getBeginLoc() const LLVM_READONLY;
1128
  SourceLocation getEndLoc() const LLVM_READONLY;
1129
1130
  // global temp stats (until we have a per-module visitor)
1131
  static void addStmtClass(const StmtClass s);
1132
  static void EnableStatistics();
1133
  static void PrintStats();
1134
1135
  /// Dumps the specified AST fragment and all subtrees to
1136
  /// \c llvm::errs().
1137
  void dump() const;
1138
  void dump(SourceManager &SM) const;
1139
  void dump(raw_ostream &OS, SourceManager &SM) const;
1140
  void dump(raw_ostream &OS) const;
1141
1142
  /// \return Unique reproducible object identifier
1143
  int64_t getID(const ASTContext &Context) const;
1144
1145
  /// dumpColor - same as dump(), but forces color highlighting.
1146
  void dumpColor() const;
1147
1148
  /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
1149
  /// back to its original source language syntax.
1150
  void dumpPretty(const ASTContext &Context) const;
1151
  void printPretty(raw_ostream &OS, PrinterHelper *Helper,
1152
                   const PrintingPolicy &Policy, unsigned Indentation = 0,
1153
                   StringRef NewlineSymbol = "\n",
1154
                   const ASTContext *Context = nullptr) const;
1155
1156
  /// Pretty-prints in JSON format.
1157
  void printJson(raw_ostream &Out, PrinterHelper *Helper,
1158
                 const PrintingPolicy &Policy, bool AddQuotes) const;
1159
1160
  /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz.  Only
1161
  ///   works on systems with GraphViz (Mac OS X) or dot+gv installed.
1162
  void viewAST() const;
1163
1164
  /// Skip no-op (attributed, compound) container stmts and skip captured
1165
  /// stmt at the top, if \a IgnoreCaptured is true.
1166
  Stmt *IgnoreContainers(bool IgnoreCaptured = false);
1167
50.4k
  const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
1168
50.4k
    return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
1169
50.4k
  }
1170
1171
  const Stmt *stripLabelLikeStatements() const;
1172
461k
  Stmt *stripLabelLikeStatements() {
1173
461k
    return const_cast<Stmt*>(
1174
461k
      const_cast<const Stmt*>(this)->stripLabelLikeStatements());
1175
461k
  }
1176
1177
  /// Child Iterators: All subclasses must implement 'children'
1178
  /// to permit easy iteration over the substatements/subexpessions of an
1179
  /// AST node.  This permits easy iteration over all nodes in the AST.
1180
  using child_iterator = StmtIterator;
1181
  using const_child_iterator = ConstStmtIterator;
1182
1183
  using child_range = llvm::iterator_range<child_iterator>;
1184
  using const_child_range = llvm::iterator_range<const_child_iterator>;
1185
1186
  child_range children();
1187
1188
49.0M
  const_child_range children() const {
1189
49.0M
    auto Children = const_cast<Stmt *>(this)->children();
1190
49.0M
    return const_child_range(Children.begin(), Children.end());
1191
49.0M
  }
1192
1193
4.46M
  child_iterator child_begin() { return children().begin(); }
1194
18
  child_iterator child_end() { return children().end(); }
1195
1196
766k
  const_child_iterator child_begin() const { return children().begin(); }
1197
4.39k
  const_child_iterator child_end() const { return children().end(); }
1198
1199
  /// Produce a unique representation of the given statement.
1200
  ///
1201
  /// \param ID once the profiling operation is complete, will contain
1202
  /// the unique representation of the given statement.
1203
  ///
1204
  /// \param Context the AST context in which the statement resides
1205
  ///
1206
  /// \param Canonical whether the profile should be based on the canonical
1207
  /// representation of this statement (e.g., where non-type template
1208
  /// parameters are identified by index/level rather than their
1209
  /// declaration pointers) or the exact representation of the statement as
1210
  /// written in the source.
1211
  void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
1212
               bool Canonical) const;
1213
1214
  /// Calculate a unique representation for a statement that is
1215
  /// stable across compiler invocations.
1216
  ///
1217
  /// \param ID profile information will be stored in ID.
1218
  ///
1219
  /// \param Hash an ODRHash object which will be called where pointers would
1220
  /// have been used in the Profile function.
1221
  void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
1222
};
1223
1224
/// DeclStmt - Adaptor class for mixing declarations with statements and
1225
/// expressions. For example, CompoundStmt mixes statements, expressions
1226
/// and declarations (variables, types). Another example is ForStmt, where
1227
/// the first statement can be an expression or a declaration.
1228
class DeclStmt : public Stmt {
1229
  DeclGroupRef DG;
1230
  SourceLocation StartLoc, EndLoc;
1231
1232
public:
1233
  DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
1234
1.34M
      : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
1235
1236
  /// Build an empty declaration statement.
1237
22.0k
  explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
1238
1239
  /// isSingleDecl - This method returns true if this DeclStmt refers
1240
  /// to a single Decl.
1241
529k
  bool isSingleDecl() const { return DG.isSingleDecl(); }
1242
1243
82.3k
  const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
1244
327k
  Decl *getSingleDecl() { return DG.getSingleDecl(); }
1245
1246
1.78k
  const DeclGroupRef getDeclGroup() const { return DG; }
1247
115k
  DeclGroupRef getDeclGroup() { return DG; }
1248
22.0k
  void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
1249
1250
22.0k
  void setStartLoc(SourceLocation L) { StartLoc = L; }
1251
538k
  SourceLocation getEndLoc() const { return EndLoc; }
1252
22.0k
  void setEndLoc(SourceLocation L) { EndLoc = L; }
1253
1254
911k
  SourceLocation getBeginLoc() const LLVM_READONLY { return StartLoc; }
1255
1256
1.73M
  static bool classof(const Stmt *T) {
1257
1.73M
    return T->getStmtClass() == DeclStmtClass;
1258
1.73M
  }
1259
1260
  // Iterators over subexpressions.
1261
352k
  child_range children() {
1262
352k
    return child_range(child_iterator(DG.begin(), DG.end()),
1263
352k
                       child_iterator(DG.end(), DG.end()));
1264
352k
  }
1265
1266
0
  const_child_range children() const {
1267
0
    auto Children = const_cast<DeclStmt *>(this)->children();
1268
0
    return const_child_range(Children);
1269
0
  }
1270
1271
  using decl_iterator = DeclGroupRef::iterator;
1272
  using const_decl_iterator = DeclGroupRef::const_iterator;
1273
  using decl_range = llvm::iterator_range<decl_iterator>;
1274
  using decl_const_range = llvm::iterator_range<const_decl_iterator>;
1275
1276
410k
  decl_range decls() { return decl_range(decl_begin(), decl_end()); }
1277
1278
278k
  decl_const_range decls() const {
1279
278k
    return decl_const_range(decl_begin(), decl_end());
1280
278k
  }
1281
1282
527k
  decl_iterator decl_begin() { return DG.begin(); }
1283
413k
  decl_iterator decl_end() { return DG.end(); }
1284
321k
  const_decl_iterator decl_begin() const { return DG.begin(); }
1285
278k
  const_decl_iterator decl_end() const { return DG.end(); }
1286
1287
  using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
1288
1289
2.43k
  reverse_decl_iterator decl_rbegin() {
1290
2.43k
    return reverse_decl_iterator(decl_end());
1291
2.43k
  }
1292
1293
2.43k
  reverse_decl_iterator decl_rend() {
1294
2.43k
    return reverse_decl_iterator(decl_begin());
1295
2.43k
  }
1296
};
1297
1298
/// NullStmt - This is the null statement ";": C99 6.8.3p3.
1299
///
1300
class NullStmt : public Stmt {
1301
public:
1302
  NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
1303
32.6k
      : Stmt(NullStmtClass) {
1304
32.6k
    NullStmtBits.HasLeadingEmptyMacro = hasLeadingEmptyMacro;
1305
32.6k
    setSemiLoc(L);
1306
32.6k
  }
1307
1308
  /// Build an empty null statement.
1309
427
  explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
1310
1311
11.5k
  SourceLocation getSemiLoc() const { return NullStmtBits.SemiLoc; }
1312
33.1k
  void setSemiLoc(SourceLocation L) { NullStmtBits.SemiLoc = L; }
1313
1314
1.02k
  bool hasLeadingEmptyMacro() const {
1315
1.02k
    return NullStmtBits.HasLeadingEmptyMacro;
1316
1.02k
  }
1317
1318
2.22k
  SourceLocation getBeginLoc() const { return getSemiLoc(); }
1319
4.21k
  SourceLocation getEndLoc() const { return getSemiLoc(); }
1320
1321
963k
  static bool classof(const Stmt *T) {
1322
963k
    return T->getStmtClass() == NullStmtClass;
1323
963k
  }
1324
1325
34.8k
  child_range children() {
1326
34.8k
    return child_range(child_iterator(), child_iterator());
1327
34.8k
  }
1328
1329
0
  const_child_range children() const {
1330
0
    return const_child_range(const_child_iterator(), const_child_iterator());
1331
0
  }
1332
};
1333
1334
/// CompoundStmt - This represents a group of statements like { stmt stmt }.
1335
class CompoundStmt final : public Stmt,
1336
                           private llvm::TrailingObjects<CompoundStmt, Stmt *> {
1337
  friend class ASTStmtReader;
1338
  friend TrailingObjects;
1339
1340
  /// The location of the closing "}". LBraceLoc is stored in CompoundStmtBits.
1341
  SourceLocation RBraceLoc;
1342
1343
  CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
1344
32.3k
  explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
1345
1346
  void setStmts(ArrayRef<Stmt *> Stmts);
1347
1348
public:
1349
  static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
1350
                              SourceLocation LB, SourceLocation RB);
1351
1352
  // Build an empty compound statement with a location.
1353
  explicit CompoundStmt(SourceLocation Loc)
1354
27.0k
      : Stmt(CompoundStmtClass), RBraceLoc(Loc) {
1355
27.0k
    CompoundStmtBits.NumStmts = 0;
1356
27.0k
    CompoundStmtBits.LBraceLoc = Loc;
1357
27.0k
  }
1358
1359
  // Build an empty compound statement.
1360
  static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
1361
1362
271k
  bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
1363
3.26M
  unsigned size() const { return CompoundStmtBits.NumStmts; }
1364
1365
  using body_iterator = Stmt **;
1366
  using body_range = llvm::iterator_range<body_iterator>;
1367
1368
1.06M
  body_range body() { return body_range(body_begin(), body_end()); }
1369
8.10M
  body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
1370
2.44M
  body_iterator body_end() { return body_begin() + size(); }
1371
716
  Stmt *body_front() { return !body_empty() ? body_begin()[0] : 
nullptr0
; }
1372
1373
82.7k
  Stmt *body_back() {
1374
82.7k
    return !body_empty() ? 
body_begin()[size() - 1]17.1k
:
nullptr65.6k
;
1375
82.7k
  }
1376
1377
  using const_body_iterator = Stmt *const *;
1378
  using body_const_range = llvm::iterator_range<const_body_iterator>;
1379
1380
386k
  body_const_range body() const {
1381
386k
    return body_const_range(body_begin(), body_end());
1382
386k
  }
1383
1384
979k
  const_body_iterator body_begin() const {
1385
979k
    return getTrailingObjects<Stmt *>();
1386
979k
  }
1387
1388
486k
  const_body_iterator body_end() const { return body_begin() + size(); }
1389
1390
1
  const Stmt *body_front() const {
1391
1
    return !body_empty() ? body_begin()[0] : 
nullptr0
;
1392
1
  }
1393
1394
4.60k
  const Stmt *body_back() const {
1395
4.60k
    return !body_empty() ? body_begin()[size() - 1] : 
nullptr0
;
1396
4.60k
  }
1397
1398
  using reverse_body_iterator = std::reverse_iterator<body_iterator>;
1399
1400
304k
  reverse_body_iterator body_rbegin() {
1401
304k
    return reverse_body_iterator(body_end());
1402
304k
  }
1403
1404
162k
  reverse_body_iterator body_rend() {
1405
162k
    return reverse_body_iterator(body_begin());
1406
162k
  }
1407
1408
  using const_reverse_body_iterator =
1409
      std::reverse_iterator<const_body_iterator>;
1410
1411
98.5k
  const_reverse_body_iterator body_rbegin() const {
1412
98.5k
    return const_reverse_body_iterator(body_end());
1413
98.5k
  }
1414
1415
98.5k
  const_reverse_body_iterator body_rend() const {
1416
98.5k
    return const_reverse_body_iterator(body_begin());
1417
98.5k
  }
1418
1419
  // Get the Stmt that StmtExpr would consider to be the result of this
1420
  // compound statement. This is used by StmtExpr to properly emulate the GCC
1421
  // compound expression extension, which ignores trailing NullStmts when
1422
  // getting the result of the expression.
1423
  // i.e. ({ 5;;; })
1424
  //           ^^ ignored
1425
  // If we don't find something that isn't a NullStmt, just return the last
1426
  // Stmt.
1427
430k
  Stmt *getStmtExprResult() {
1428
430k
    for (auto *B : llvm::reverse(body())) {
1429
364k
      if (!isa<NullStmt>(B))
1430
364k
        return B;
1431
364k
    }
1432
430k
    
return body_back()65.6k
;
1433
430k
  }
1434
1435
237k
  const Stmt *getStmtExprResult() const {
1436
237k
    return const_cast<CompoundStmt *>(this)->getStmtExprResult();
1437
237k
  }
1438
1439
572k
  SourceLocation getBeginLoc() const { return CompoundStmtBits.LBraceLoc; }
1440
3.54M
  SourceLocation getEndLoc() const { return RBraceLoc; }
1441
1442
497k
  SourceLocation getLBracLoc() const { return CompoundStmtBits.LBraceLoc; }
1443
425k
  SourceLocation getRBracLoc() const { return RBraceLoc; }
1444
1445
2.28M
  static bool classof(const Stmt *T) {
1446
2.28M
    return T->getStmtClass() == CompoundStmtClass;
1447
2.28M
  }
1448
1449
  // Iterators
1450
1.06M
  child_range children() { return child_range(body_begin(), body_end()); }
1451
1452
90
  const_child_range children() const {
1453
90
    return const_child_range(body_begin(), body_end());
1454
90
  }
1455
};
1456
1457
// SwitchCase is the base class for CaseStmt and DefaultStmt,
1458
class SwitchCase : public Stmt {
1459
protected:
1460
  /// The location of the ":".
1461
  SourceLocation ColonLoc;
1462
1463
  // The location of the "case" or "default" keyword. Stored in SwitchCaseBits.
1464
  // SourceLocation KeywordLoc;
1465
1466
  /// A pointer to the following CaseStmt or DefaultStmt class,
1467
  /// used by SwitchStmt.
1468
  SwitchCase *NextSwitchCase = nullptr;
1469
1470
  SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
1471
24.9k
      : Stmt(SC), ColonLoc(ColonLoc) {
1472
24.9k
    setKeywordLoc(KWLoc);
1473
24.9k
  }
1474
1475
111
  SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
1476
1477
public:
1478
832
  const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
1479
63.5k
  SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
1480
25.0k
  void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
1481
1482
28.2k
  SourceLocation getKeywordLoc() const { return SwitchCaseBits.KeywordLoc; }
1483
25.1k
  void setKeywordLoc(SourceLocation L) { SwitchCaseBits.KeywordLoc = L; }
1484
6.57k
  SourceLocation getColonLoc() const { return ColonLoc; }
1485
111
  void setColonLoc(SourceLocation L) { ColonLoc = L; }
1486
1487
  inline Stmt *getSubStmt();
1488
3.52k
  const Stmt *getSubStmt() const {
1489
3.52k
    return const_cast<SwitchCase *>(this)->getSubStmt();
1490
3.52k
  }
1491
1492
38
  SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1493
  inline SourceLocation getEndLoc() const LLVM_READONLY;
1494
1495
3.87M
  static bool classof(const Stmt *T) {
1496
3.87M
    return T->getStmtClass() == CaseStmtClass ||
1497
3.87M
           
T->getStmtClass() == DefaultStmtClass3.83M
;
1498
3.87M
  }
1499
};
1500
1501
/// CaseStmt - Represent a case statement. It can optionally be a GNU case
1502
/// statement of the form LHS ... RHS representing a range of cases.
1503
class CaseStmt final
1504
    : public SwitchCase,
1505
      private llvm::TrailingObjects<CaseStmt, Stmt *, SourceLocation> {
1506
  friend TrailingObjects;
1507
1508
  // CaseStmt is followed by several trailing objects, some of which optional.
1509
  // Note that it would be more convenient to put the optional trailing objects
1510
  // at the end but this would impact children().
1511
  // The trailing objects are in order:
1512
  //
1513
  // * A "Stmt *" for the LHS of the case statement. Always present.
1514
  //
1515
  // * A "Stmt *" for the RHS of the case statement. This is a GNU extension
1516
  //   which allow ranges in cases statement of the form LHS ... RHS.
1517
  //   Present if and only if caseStmtIsGNURange() is true.
1518
  //
1519
  // * A "Stmt *" for the substatement of the case statement. Always present.
1520
  //
1521
  // * A SourceLocation for the location of the ... if this is a case statement
1522
  //   with a range. Present if and only if caseStmtIsGNURange() is true.
1523
  enum { LhsOffset = 0, SubStmtOffsetFromRhs = 1 };
1524
  enum { NumMandatoryStmtPtr = 2 };
1525
1526
14.8k
  unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1527
14.8k
    return NumMandatoryStmtPtr + caseStmtIsGNURange();
1528
14.8k
  }
1529
1530
0
  unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
1531
0
    return caseStmtIsGNURange();
1532
0
  }
1533
1534
45.0k
  unsigned lhsOffset() const { return LhsOffset; }
1535
79.0k
  unsigned rhsOffset() const { return LhsOffset + caseStmtIsGNURange(); }
1536
78.4k
  unsigned subStmtOffset() const { return rhsOffset() + SubStmtOffsetFromRhs; }
1537
1538
  /// Build a case statement assuming that the storage for the
1539
  /// trailing objects has been properly allocated.
1540
  CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
1541
           SourceLocation ellipsisLoc, SourceLocation colonLoc)
1542
22.8k
      : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
1543
22.8k
    // Handle GNU case statements of the form LHS ... RHS.
1544
22.8k
    bool IsGNURange = rhs != nullptr;
1545
22.8k
    SwitchCaseBits.CaseStmtIsGNURange = IsGNURange;
1546
22.8k
    setLHS(lhs);
1547
22.8k
    setSubStmt(nullptr);
1548
22.8k
    if (IsGNURange) {
1549
98
      setRHS(rhs);
1550
98
      setEllipsisLoc(ellipsisLoc);
1551
98
    }
1552
22.8k
  }
1553
1554
  /// Build an empty switch case statement.
1555
  explicit CaseStmt(EmptyShell Empty, bool CaseStmtIsGNURange)
1556
99
      : SwitchCase(CaseStmtClass, Empty) {
1557
99
    SwitchCaseBits.CaseStmtIsGNURange = CaseStmtIsGNURange;
1558
99
  }
1559
1560
public:
1561
  /// Build a case statement.
1562
  static CaseStmt *Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1563
                          SourceLocation caseLoc, SourceLocation ellipsisLoc,
1564
                          SourceLocation colonLoc);
1565
1566
  /// Build an empty case statement.
1567
  static CaseStmt *CreateEmpty(const ASTContext &Ctx, bool CaseStmtIsGNURange);
1568
1569
  /// True if this case statement is of the form case LHS ... RHS, which
1570
  /// is a GNU extension. In this case the RHS can be obtained with getRHS()
1571
  /// and the location of the ellipsis can be obtained with getEllipsisLoc().
1572
119k
  bool caseStmtIsGNURange() const { return SwitchCaseBits.CaseStmtIsGNURange; }
1573
1574
1.36k
  SourceLocation getCaseLoc() const { return getKeywordLoc(); }
1575
0
  void setCaseLoc(SourceLocation L) { setKeywordLoc(L); }
1576
1577
  /// Get the location of the ... in a case statement of the form LHS ... RHS.
1578
455
  SourceLocation getEllipsisLoc() const {
1579
455
    return caseStmtIsGNURange() ? 
*getTrailingObjects<SourceLocation>()6
1580
455
                                : 
SourceLocation()449
;
1581
455
  }
1582
1583
  /// Set the location of the ... in a case statement of the form LHS ... RHS.
1584
  /// Assert that this case statement is of this form.
1585
98
  void setEllipsisLoc(SourceLocation L) {
1586
98
    assert(
1587
98
        caseStmtIsGNURange() &&
1588
98
        "setEllipsisLoc but this is not a case stmt of the form LHS ... RHS!");
1589
98
    *getTrailingObjects<SourceLocation>() = L;
1590
98
  }
1591
1592
19.8k
  Expr *getLHS() {
1593
19.8k
    return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
1594
19.8k
  }
1595
1596
2.26k
  const Expr *getLHS() const {
1597
2.26k
    return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
1598
2.26k
  }
1599
1600
22.9k
  void setLHS(Expr *Val) {
1601
22.9k
    getTrailingObjects<Stmt *>()[lhsOffset()] = reinterpret_cast<Stmt *>(Val);
1602
22.9k
  }
1603
1604
11.8k
  Expr *getRHS() {
1605
11.8k
    return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
1606
333
                                      getTrailingObjects<Stmt *>()[rhsOffset()])
1607
11.8k
                                : 
nullptr11.5k
;
1608
11.8k
  }
1609
1610
2.21k
  const Expr *getRHS() const {
1611
2.21k
    return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
1612
150
                                      getTrailingObjects<Stmt *>()[rhsOffset()])
1613
2.21k
                                : 
nullptr2.06k
;
1614
2.21k
  }
1615
1616
98
  void setRHS(Expr *Val) {
1617
98
    assert(caseStmtIsGNURange() &&
1618
98
           "setRHS but this is not a case stmt of the form LHS ... RHS!");
1619
98
    getTrailingObjects<Stmt *>()[rhsOffset()] = reinterpret_cast<Stmt *>(Val);
1620
98
  }
1621
1622
29.2k
  Stmt *getSubStmt() { return getTrailingObjects<Stmt *>()[subStmtOffset()]; }
1623
3.43k
  const Stmt *getSubStmt() const {
1624
3.43k
    return getTrailingObjects<Stmt *>()[subStmtOffset()];
1625
3.43k
  }
1626
1627
45.7k
  void setSubStmt(Stmt *S) {
1628
45.7k
    getTrailingObjects<Stmt *>()[subStmtOffset()] = S;
1629
45.7k
  }
1630
1631
19.2k
  SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1632
749
  SourceLocation getEndLoc() const LLVM_READONLY {
1633
749
    // Handle deeply nested case statements with iteration instead of recursion.
1634
749
    const CaseStmt *CS = this;
1635
760
    while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
1636
11
      CS = CS2;
1637
749
1638
749
    return CS->getSubStmt()->getEndLoc();
1639
749
  }
1640
1641
124k
  static bool classof(const Stmt *T) {
1642
124k
    return T->getStmtClass() == CaseStmtClass;
1643
124k
  }
1644
1645
  // Iterators
1646
14.7k
  child_range children() {
1647
14.7k
    return child_range(getTrailingObjects<Stmt *>(),
1648
14.7k
                       getTrailingObjects<Stmt *>() +
1649
14.7k
                           numTrailingObjects(OverloadToken<Stmt *>()));
1650
14.7k
  }
1651
1652
0
  const_child_range children() const {
1653
0
    return const_child_range(getTrailingObjects<Stmt *>(),
1654
0
                             getTrailingObjects<Stmt *>() +
1655
0
                                 numTrailingObjects(OverloadToken<Stmt *>()));
1656
0
  }
1657
};
1658
1659
class DefaultStmt : public SwitchCase {
1660
  Stmt *SubStmt;
1661
1662
public:
1663
  DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt)
1664
2.17k
      : SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
1665
1666
  /// Build an empty default statement.
1667
  explicit DefaultStmt(EmptyShell Empty)
1668
12
      : SwitchCase(DefaultStmtClass, Empty) {}
1669
1670
3.01k
  Stmt *getSubStmt() { return SubStmt; }
1671
167
  const Stmt *getSubStmt() const { return SubStmt; }
1672
12
  void setSubStmt(Stmt *S) { SubStmt = S; }
1673
1674
132
  SourceLocation getDefaultLoc() const { return getKeywordLoc(); }
1675
0
  void setDefaultLoc(SourceLocation L) { setKeywordLoc(L); }
1676
1677
1.39k
  SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1678
164
  SourceLocation getEndLoc() const LLVM_READONLY {
1679
164
    return SubStmt->getEndLoc();
1680
164
  }
1681
1682
22.0k
  static bool classof(const Stmt *T) {
1683
22.0k
    return T->getStmtClass() == DefaultStmtClass;
1684
22.0k
  }
1685
1686
  // Iterators
1687
2.00k
  child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1688
1689
0
  const_child_range children() const {
1690
0
    return const_child_range(&SubStmt, &SubStmt + 1);
1691
0
  }
1692
};
1693
1694
0
SourceLocation SwitchCase::getEndLoc() const {
1695
0
  if (const auto *CS = dyn_cast<CaseStmt>(this))
1696
0
    return CS->getEndLoc();
1697
0
  else if (const auto *DS = dyn_cast<DefaultStmt>(this))
1698
0
    return DS->getEndLoc();
1699
0
  llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
1700
0
}
1701
1702
22.9k
Stmt *SwitchCase::getSubStmt() {
1703
22.9k
  if (auto *CS = dyn_cast<CaseStmt>(this))
1704
21.2k
    return CS->getSubStmt();
1705
1.72k
  else if (auto *DS = dyn_cast<DefaultStmt>(this))
1706
1.72k
    return DS->getSubStmt();
1707
0
  llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
1708
0
}
1709
1710
/// Represents a statement that could possibly have a value and type. This
1711
/// covers expression-statements, as well as labels and attributed statements.
1712
///
1713
/// Value statements have a special meaning when they are the last non-null
1714
/// statement in a GNU statement expression, where they determine the value
1715
/// of the statement expression.
1716
class ValueStmt : public Stmt {
1717
protected:
1718
  using Stmt::Stmt;
1719
1720
public:
1721
  const Expr *getExprStmt() const;
1722
0
  Expr *getExprStmt() {
1723
0
    const ValueStmt *ConstThis = this;
1724
0
    return const_cast<Expr*>(ConstThis->getExprStmt());
1725
0
  }
1726
1727
12.3k
  static bool classof(const Stmt *T) {
1728
12.3k
    return T->getStmtClass() >= firstValueStmtConstant &&
1729
12.3k
           
T->getStmtClass() <= lastValueStmtConstant11.0k
;
1730
12.3k
  }
1731
};
1732
1733
/// LabelStmt - Represents a label, which has a substatement.  For example:
1734
///    foo: return;
1735
class LabelStmt : public ValueStmt {
1736
  LabelDecl *TheDecl;
1737
  Stmt *SubStmt;
1738
1739
public:
1740
  /// Build a label statement.
1741
  LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
1742
3.72k
      : ValueStmt(LabelStmtClass), TheDecl(D), SubStmt(substmt) {
1743
3.72k
    setIdentLoc(IL);
1744
3.72k
  }
1745
1746
  /// Build an empty label statement.
1747
10
  explicit LabelStmt(EmptyShell Empty) : ValueStmt(LabelStmtClass, Empty) {}
1748
1749
666
  SourceLocation getIdentLoc() const { return LabelStmtBits.IdentLoc; }
1750
3.73k
  void setIdentLoc(SourceLocation L) { LabelStmtBits.IdentLoc = L; }
1751
1752
1.88k
  LabelDecl *getDecl() const { return TheDecl; }
1753
10
  void setDecl(LabelDecl *D) { TheDecl = D; }
1754
1755
  const char *getName() const;
1756
3.84k
  Stmt *getSubStmt() { return SubStmt; }
1757
1758
747
  const Stmt *getSubStmt() const { return SubStmt; }
1759
10
  void setSubStmt(Stmt *SS) { SubStmt = SS; }
1760
1761
349
  SourceLocation getBeginLoc() const { return getIdentLoc(); }
1762
300
  SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
1763
1764
1.72k
  child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1765
1766
0
  const_child_range children() const {
1767
0
    return const_child_range(&SubStmt, &SubStmt + 1);
1768
0
  }
1769
1770
5.85M
  static bool classof(const Stmt *T) {
1771
5.85M
    return T->getStmtClass() == LabelStmtClass;
1772
5.85M
  }
1773
};
1774
1775
/// Represents an attribute applied to a statement.
1776
///
1777
/// Represents an attribute applied to a statement. For example:
1778
///   [[omp::for(...)]] for (...) { ... }
1779
class AttributedStmt final
1780
    : public ValueStmt,
1781
      private llvm::TrailingObjects<AttributedStmt, const Attr *> {
1782
  friend class ASTStmtReader;
1783
  friend TrailingObjects;
1784
1785
  Stmt *SubStmt;
1786
1787
  AttributedStmt(SourceLocation Loc, ArrayRef<const Attr *> Attrs,
1788
                 Stmt *SubStmt)
1789
1.09k
      : ValueStmt(AttributedStmtClass), SubStmt(SubStmt) {
1790
1.09k
    AttributedStmtBits.NumAttrs = Attrs.size();
1791
1.09k
    AttributedStmtBits.AttrLoc = Loc;
1792
1.09k
    std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
1793
1.09k
  }
1794
1795
  explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
1796
10
      : ValueStmt(AttributedStmtClass, Empty) {
1797
10
    AttributedStmtBits.NumAttrs = NumAttrs;
1798
10
    AttributedStmtBits.AttrLoc = SourceLocation{};
1799
10
    std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
1800
10
  }
1801
1802
372
  const Attr *const *getAttrArrayPtr() const {
1803
372
    return getTrailingObjects<const Attr *>();
1804
372
  }
1805
1.11k
  const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
1806
1807
public:
1808
  static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
1809
                                ArrayRef<const Attr *> Attrs, Stmt *SubStmt);
1810
1811
  // Build an empty attributed statement.
1812
  static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
1813
1814
116
  SourceLocation getAttrLoc() const { return AttributedStmtBits.AttrLoc; }
1815
372
  ArrayRef<const Attr *> getAttrs() const {
1816
372
    return llvm::makeArrayRef(getAttrArrayPtr(), AttributedStmtBits.NumAttrs);
1817
372
  }
1818
1819
150
  Stmt *getSubStmt() { return SubStmt; }
1820
154
  const Stmt *getSubStmt() const { return SubStmt; }
1821
1822
25
  SourceLocation getBeginLoc() const { return getAttrLoc(); }
1823
11
  SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
1824
1825
1.09k
  child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1826
1827
0
  const_child_range children() const {
1828
0
    return const_child_range(&SubStmt, &SubStmt + 1);
1829
0
  }
1830
1831
923k
  static bool classof(const Stmt *T) {
1832
923k
    return T->getStmtClass() == AttributedStmtClass;
1833
923k
  }
1834
};
1835
1836
/// IfStmt - This represents an if/then/else.
1837
class IfStmt final
1838
    : public Stmt,
1839
      private llvm::TrailingObjects<IfStmt, Stmt *, SourceLocation> {
1840
  friend TrailingObjects;
1841
1842
  // IfStmt is followed by several trailing objects, some of which optional.
1843
  // Note that it would be more convenient to put the optional trailing
1844
  // objects at then end but this would change the order of the children.
1845
  // The trailing objects are in order:
1846
  //
1847
  // * A "Stmt *" for the init statement.
1848
  //    Present if and only if hasInitStorage().
1849
  //
1850
  // * A "Stmt *" for the condition variable.
1851
  //    Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
1852
  //
1853
  // * A "Stmt *" for the condition.
1854
  //    Always present. This is in fact a "Expr *".
1855
  //
1856
  // * A "Stmt *" for the then statement.
1857
  //    Always present.
1858
  //
1859
  // * A "Stmt *" for the else statement.
1860
  //    Present if and only if hasElseStorage().
1861
  //
1862
  // * A "SourceLocation" for the location of the "else".
1863
  //    Present if and only if hasElseStorage().
1864
  enum { InitOffset = 0, ThenOffsetFromCond = 1, ElseOffsetFromCond = 2 };
1865
  enum { NumMandatoryStmtPtr = 2 };
1866
1867
257k
  unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1868
257k
    return NumMandatoryStmtPtr + hasElseStorage() + hasVarStorage() +
1869
257k
           hasInitStorage();
1870
257k
  }
1871
1872
0
  unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
1873
0
    return hasElseStorage();
1874
0
  }
1875
1876
483
  unsigned initOffset() const { return InitOffset; }
1877
1.15k
  unsigned varOffset() const { return InitOffset + hasInitStorage(); }
1878
1.79M
  unsigned condOffset() const {
1879
1.79M
    return InitOffset + hasInitStorage() + hasVarStorage();
1880
1.79M
  }
1881
679k
  unsigned thenOffset() const { return condOffset() + ThenOffsetFromCond; }
1882
143k
  unsigned elseOffset() const { return condOffset() + ElseOffsetFromCond; }
1883
1884
  /// Build an if/then/else statement.
1885
  IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr, Stmt *Init,
1886
         VarDecl *Var, Expr *Cond, Stmt *Then, SourceLocation EL, Stmt *Else);
1887
1888
  /// Build an empty if/then/else statement.
1889
  explicit IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit);
1890
1891
public:
1892
  /// Create an IfStmt.
1893
  static IfStmt *Create(const ASTContext &Ctx, SourceLocation IL,
1894
                        bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond,
1895
                        Stmt *Then, SourceLocation EL = SourceLocation(),
1896
                        Stmt *Else = nullptr);
1897
1898
  /// Create an empty IfStmt optionally with storage for an else statement,
1899
  /// condition variable and init expression.
1900
  static IfStmt *CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
1901
                             bool HasInit);
1902
1903
  /// True if this IfStmt has the storage for an init statement.
1904
2.32M
  bool hasInitStorage() const { return IfStmtBits.HasInit; }
1905
1906
  /// True if this IfStmt has storage for a variable declaration.
1907
2.42M
  bool hasVarStorage() const { return IfStmtBits.HasVar; }
1908
1909
  /// True if this IfStmt has storage for an else statement.
1910
733k
  bool hasElseStorage() const { return IfStmtBits.HasElse; }
1911
1912
301k
  Expr *getCond() {
1913
301k
    return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1914
301k
  }
1915
1916
248k
  const Expr *getCond() const {
1917
248k
    return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1918
248k
  }
1919
1920
420k
  void setCond(Expr *Cond) {
1921
420k
    getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
1922
420k
  }
1923
1924
129k
  Stmt *getThen() { return getTrailingObjects<Stmt *>()[thenOffset()]; }
1925
130k
  const Stmt *getThen() const {
1926
130k
    return getTrailingObjects<Stmt *>()[thenOffset()];
1927
130k
  }
1928
1929
420k
  void setThen(Stmt *Then) {
1930
420k
    getTrailingObjects<Stmt *>()[thenOffset()] = Then;
1931
420k
  }
1932
1933
141k
  Stmt *getElse() {
1934
141k
    return hasElseStorage() ? 
getTrailingObjects<Stmt *>()[elseOffset()]36.6k
1935
141k
                            : 
nullptr104k
;
1936
141k
  }
1937
1938
134k
  const Stmt *getElse() const {
1939
134k
    return hasElseStorage() ? 
getTrailingObjects<Stmt *>()[elseOffset()]21.6k
1940
134k
                            : 
nullptr112k
;
1941
134k
  }
1942
1943
85.2k
  void setElse(Stmt *Else) {
1944
85.2k
    assert(hasElseStorage() &&
1945
85.2k
           "This if statement has no storage for an else statement!");
1946
85.2k
    getTrailingObjects<Stmt *>()[elseOffset()] = Else;
1947
85.2k
  }
1948
1949
  /// Retrieve the variable declared in this "if" statement, if any.
1950
  ///
1951
  /// In the following example, "x" is the condition variable.
1952
  /// \code
1953
  /// if (int x = foo()) {
1954
  ///   printf("x is %d", x);
1955
  /// }
1956
  /// \endcode
1957
  VarDecl *getConditionVariable();
1958
111k
  const VarDecl *getConditionVariable() const {
1959
111k
    return const_cast<IfStmt *>(this)->getConditionVariable();
1960
111k
  }
1961
1962
  /// Set the condition variable for this if statement.
1963
  /// The if statement must have storage for the condition variable.
1964
  void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
1965
1966
  /// If this IfStmt has a condition variable, return the faux DeclStmt
1967
  /// associated with the creation of that condition variable.
1968
371k
  DeclStmt *getConditionVariableDeclStmt() {
1969
371k
    return hasVarStorage() ? static_cast<DeclStmt *>(
1970
792
                                 getTrailingObjects<Stmt *>()[varOffset()])
1971
371k
                           : 
nullptr370k
;
1972
371k
  }
1973
1974
84
  const DeclStmt *getConditionVariableDeclStmt() const {
1975
84
    return hasVarStorage() ? static_cast<DeclStmt *>(
1976
3
                                 getTrailingObjects<Stmt *>()[varOffset()])
1977
84
                           : 
nullptr81
;
1978
84
  }
1979
1980
204k
  Stmt *getInit() {
1981
204k
    return hasInitStorage() ? 
getTrailingObjects<Stmt *>()[initOffset()]38
1982
204k
                            : 
nullptr204k
;
1983
204k
  }
1984
1985
65.2k
  const Stmt *getInit() const {
1986
65.2k
    return hasInitStorage() ? 
getTrailingObjects<Stmt *>()[initOffset()]253
1987
65.2k
                            : 
nullptr64.9k
;
1988
65.2k
  }
1989
1990
192
  void setInit(Stmt *Init) {
1991
192
    assert(hasInitStorage() &&
1992
192
           "This if statement has no storage for an init statement!");
1993
192
    getTrailingObjects<Stmt *>()[initOffset()] = Init;
1994
192
  }
1995
1996
155k
  SourceLocation getIfLoc() const { return IfStmtBits.IfLoc; }
1997
420k
  void setIfLoc(SourceLocation IfLoc) { IfStmtBits.IfLoc = IfLoc; }
1998
1999
29.0k
  SourceLocation getElseLoc() const {
2000
29.0k
    return hasElseStorage() ? 
*getTrailingObjects<SourceLocation>()16.9k
2001
29.0k
                            : 
SourceLocation()12.1k
;
2002
29.0k
  }
2003
2004
85.2k
  void setElseLoc(SourceLocation ElseLoc) {
2005
85.2k
    assert(hasElseStorage() &&
2006
85.2k
           "This if statement has no storage for an else statement!");
2007
85.2k
    *getTrailingObjects<SourceLocation>() = ElseLoc;
2008
85.2k
  }
2009
2010
199k
  bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
2011
420k
  void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
2012
2013
  /// If this is an 'if constexpr', determine which substatement will be taken.
2014
  /// Otherwise, or if the condition is value-dependent, returns None.
2015
  Optional<const Stmt*> getNondiscardedCase(const ASTContext &Ctx) const;
2016
2017
  bool isObjCAvailabilityCheck() const;
2018
2019
70.0k
  SourceLocation getBeginLoc() const { return getIfLoc(); }
2020
6.78k
  SourceLocation getEndLoc() const LLVM_READONLY {
2021
6.78k
    if (getElse())
2022
1.16k
      return getElse()->getEndLoc();
2023
5.61k
    return getThen()->getEndLoc();
2024
5.61k
  }
2025
2026
  // Iterators over subexpressions.  The iterators will include iterating
2027
  // over the initialization expression referenced by the condition variable.
2028
155k
  child_range children() {
2029
155k
    return child_range(getTrailingObjects<Stmt *>(),
2030
155k
                       getTrailingObjects<Stmt *>() +
2031
155k
                           numTrailingObjects(OverloadToken<Stmt *>()));
2032
155k
  }
2033
2034
0
  const_child_range children() const {
2035
0
    return const_child_range(getTrailingObjects<Stmt *>(),
2036
0
                             getTrailingObjects<Stmt *>() +
2037
0
                                 numTrailingObjects(OverloadToken<Stmt *>()));
2038
0
  }
2039
2040
252k
  static bool classof(const Stmt *T) {
2041
252k
    return T->getStmtClass() == IfStmtClass;
2042
252k
  }
2043
};
2044
2045
/// SwitchStmt - This represents a 'switch' stmt.
2046
class SwitchStmt final : public Stmt,
2047
                         private llvm::TrailingObjects<SwitchStmt, Stmt *> {
2048
  friend TrailingObjects;
2049
2050
  /// Points to a linked list of case and default statements.
2051
  SwitchCase *FirstCase;
2052
2053
  // SwitchStmt is followed by several trailing objects,
2054
  // some of which optional. Note that it would be more convenient to
2055
  // put the optional trailing objects at the end but this would change
2056
  // the order in children().
2057
  // The trailing objects are in order:
2058
  //
2059
  // * A "Stmt *" for the init statement.
2060
  //    Present if and only if hasInitStorage().
2061
  //
2062
  // * A "Stmt *" for the condition variable.
2063
  //    Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
2064
  //
2065
  // * A "Stmt *" for the condition.
2066
  //    Always present. This is in fact an "Expr *".
2067
  //
2068
  // * A "Stmt *" for the body.
2069
  //    Always present.
2070
  enum { InitOffset = 0, BodyOffsetFromCond = 1 };
2071
  enum { NumMandatoryStmtPtr = 2 };
2072
2073
8.44k
  unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
2074
8.44k
    return NumMandatoryStmtPtr + hasInitStorage() + hasVarStorage();
2075
8.44k
  }
2076
2077
229
  unsigned initOffset() const { return InitOffset; }
2078
226
  unsigned varOffset() const { return InitOffset + hasInitStorage(); }
2079
58.7k
  unsigned condOffset() const {
2080
58.7k
    return InitOffset + hasInitStorage() + hasVarStorage();
2081
58.7k
  }
2082
17.5k
  unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
2083
2084
  /// Build a switch statement.
2085
  SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var, Expr *Cond);
2086
2087
  /// Build a empty switch statement.
2088
  explicit SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar);
2089
2090
public:
2091
  /// Create a switch statement.
2092
  static SwitchStmt *Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
2093
                            Expr *Cond);
2094
2095
  /// Create an empty switch statement optionally with storage for
2096
  /// an init expression and a condition variable.
2097
  static SwitchStmt *CreateEmpty(const ASTContext &Ctx, bool HasInit,
2098
                                 bool HasVar);
2099
2100
  /// True if this SwitchStmt has storage for an init statement.
2101
75.5k
  bool hasInitStorage() const { return SwitchStmtBits.HasInit; }
2102
2103
  /// True if this SwitchStmt has storage for a condition variable.
2104
76.3k
  bool hasVarStorage() const { return SwitchStmtBits.HasVar; }
2105
2106
33.2k
  Expr *getCond() {
2107
33.2k
    return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2108
33.2k
  }
2109
2110
1.82k
  const Expr *getCond() const {
2111
1.82k
    return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2112
1.82k
  }
2113
2114
6.15k
  void setCond(Expr *Cond) {
2115
6.15k
    getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
2116
6.15k
  }
2117
2118
3.64k
  Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
2119
1.67k
  const Stmt *getBody() const {
2120
1.67k
    return getTrailingObjects<Stmt *>()[bodyOffset()];
2121
1.67k
  }
2122
2123
12.2k
  void setBody(Stmt *Body) {
2124
12.2k
    getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
2125
12.2k
  }
2126
2127
7.22k
  Stmt *getInit() {
2128
7.22k
    return hasInitStorage() ? 
getTrailingObjects<Stmt *>()[initOffset()]94
2129
7.22k
                            : 
nullptr7.13k
;
2130
7.22k
  }
2131
2132
759
  const Stmt *getInit() const {
2133
759
    return hasInitStorage() ? 
getTrailingObjects<Stmt *>()[initOffset()]56
2134
759
                            : 
nullptr703
;
2135
759
  }
2136
2137
79
  void setInit(Stmt *Init) {
2138
79
    assert(hasInitStorage() &&
2139
79
           "This switch statement has no storage for an init statement!");
2140
79
    getTrailingObjects<Stmt *>()[initOffset()] = Init;
2141
79
  }
2142
2143
  /// Retrieve the variable declared in this "switch" statement, if any.
2144
  ///
2145
  /// In the following example, "x" is the condition variable.
2146
  /// \code
2147
  /// switch (int x = foo()) {
2148
  ///   case 0: break;
2149
  ///   // ...
2150
  /// }
2151
  /// \endcode
2152
  VarDecl *getConditionVariable();
2153
1.78k
  const VarDecl *getConditionVariable() const {
2154
1.78k
    return const_cast<SwitchStmt *>(this)->getConditionVariable();
2155
1.78k
  }
2156
2157
  /// Set the condition variable in this switch statement.
2158
  /// The switch statement must have storage for it.
2159
  void setConditionVariable(const ASTContext &Ctx, VarDecl *VD);
2160
2161
  /// If this SwitchStmt has a condition variable, return the faux DeclStmt
2162
  /// associated with the creation of that condition variable.
2163
9.04k
  DeclStmt *getConditionVariableDeclStmt() {
2164
9.04k
    return hasVarStorage() ? static_cast<DeclStmt *>(
2165
170
                                 getTrailingObjects<Stmt *>()[varOffset()])
2166
9.04k
                           : 
nullptr8.87k
;
2167
9.04k
  }
2168
2169
2
  const DeclStmt *getConditionVariableDeclStmt() const {
2170
2
    return hasVarStorage() ? static_cast<DeclStmt *>(
2171
0
                                 getTrailingObjects<Stmt *>()[varOffset()])
2172
2
                           : nullptr;
2173
2
  }
2174
2175
11.6k
  SwitchCase *getSwitchCaseList() { return FirstCase; }
2176
365
  const SwitchCase *getSwitchCaseList() const { return FirstCase; }
2177
50
  void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
2178
2179
2.44k
  SourceLocation getSwitchLoc() const { return SwitchStmtBits.SwitchLoc; }
2180
12.2k
  void setSwitchLoc(SourceLocation L) { SwitchStmtBits.SwitchLoc = L; }
2181
2182
6.09k
  void setBody(Stmt *S, SourceLocation SL) {
2183
6.09k
    setBody(S);
2184
6.09k
    setSwitchLoc(SL);
2185
6.09k
  }
2186
2187
24.9k
  void addSwitchCase(SwitchCase *SC) {
2188
24.9k
    assert(!SC->getNextSwitchCase() &&
2189
24.9k
           "case/default already added to a switch");
2190
24.9k
    SC->setNextSwitchCase(FirstCase);
2191
24.9k
    FirstCase = SC;
2192
24.9k
  }
2193
2194
  /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
2195
  /// switch over an enum value then all cases have been explicitly covered.
2196
429
  void setAllEnumCasesCovered() { SwitchStmtBits.AllEnumCasesCovered = true; }
2197
2198
  /// Returns true if the SwitchStmt is a switch of an enum value and all cases
2199
  /// have been explicitly covered.
2200
1.84k
  bool isAllEnumCasesCovered() const {
2201
1.84k
    return SwitchStmtBits.AllEnumCasesCovered;
2202
1.84k
  }
2203
2204
971
  SourceLocation getBeginLoc() const { return getSwitchLoc(); }
2205
516
  SourceLocation getEndLoc() const LLVM_READONLY {
2206
516
    return getBody() ? getBody()->getEndLoc()
2207
516
                     : 
reinterpret_cast<const Stmt *>(getCond())->getEndLoc()0
;
2208
516
  }
2209
2210
  // Iterators
2211
8.44k
  child_range children() {
2212
8.44k
    return child_range(getTrailingObjects<Stmt *>(),
2213
8.44k
                       getTrailingObjects<Stmt *>() +
2214
8.44k
                           numTrailingObjects(OverloadToken<Stmt *>()));
2215
8.44k
  }
2216
2217
0
  const_child_range children() const {
2218
0
    return const_child_range(getTrailingObjects<Stmt *>(),
2219
0
                             getTrailingObjects<Stmt *>() +
2220
0
                                 numTrailingObjects(OverloadToken<Stmt *>()));
2221
0
  }
2222
2223
74.3k
  static bool classof(const Stmt *T) {
2224
74.3k
    return T->getStmtClass() == SwitchStmtClass;
2225
74.3k
  }
2226
};
2227
2228
/// WhileStmt - This represents a 'while' stmt.
2229
class WhileStmt final : public Stmt,
2230
                        private llvm::TrailingObjects<WhileStmt, Stmt *> {
2231
  friend TrailingObjects;
2232
2233
  // WhileStmt is followed by several trailing objects,
2234
  // some of which optional. Note that it would be more
2235
  // convenient to put the optional trailing object at the end
2236
  // but this would affect children().
2237
  // The trailing objects are in order:
2238
  //
2239
  // * A "Stmt *" for the condition variable.
2240
  //    Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
2241
  //
2242
  // * A "Stmt *" for the condition.
2243
  //    Always present. This is in fact an "Expr *".
2244
  //
2245
  // * A "Stmt *" for the body.
2246
  //    Always present.
2247
  //
2248
  enum { VarOffset = 0, BodyOffsetFromCond = 1 };
2249
  enum { NumMandatoryStmtPtr = 2 };
2250
2251
652
  unsigned varOffset() const { return VarOffset; }
2252
152k
  unsigned condOffset() const { return VarOffset + hasVarStorage(); }
2253
63.5k
  unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
2254
2255
19.5k
  unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
2256
19.5k
    return NumMandatoryStmtPtr + hasVarStorage();
2257
19.5k
  }
2258
2259
  /// Build a while statement.
2260
  WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond, Stmt *Body,
2261
            SourceLocation WL);
2262
2263
  /// Build an empty while statement.
2264
  explicit WhileStmt(EmptyShell Empty, bool HasVar);
2265
2266
public:
2267
  /// Create a while statement.
2268
  static WhileStmt *Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
2269
                           Stmt *Body, SourceLocation WL);
2270
2271
  /// Create an empty while statement optionally with storage for
2272
  /// a condition variable.
2273
  static WhileStmt *CreateEmpty(const ASTContext &Ctx, bool HasVar);
2274
2275
  /// True if this WhileStmt has storage for a condition variable.
2276
190k
  bool hasVarStorage() const { return WhileStmtBits.HasVar; }
2277
2278
27.3k
  Expr *getCond() {
2279
27.3k
    return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2280
27.3k
  }
2281
2282
19.9k
  const Expr *getCond() const {
2283
19.9k
    return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2284
19.9k
  }
2285
2286
41.8k
  void setCond(Expr *Cond) {
2287
41.8k
    getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
2288
41.8k
  }
2289
2290
9.86k
  Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
2291
11.8k
  const Stmt *getBody() const {
2292
11.8k
    return getTrailingObjects<Stmt *>()[bodyOffset()];
2293
11.8k
  }
2294
2295
41.8k
  void setBody(Stmt *Body) {
2296
41.8k
    getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
2297
41.8k
  }
2298
2299
  /// Retrieve the variable declared in this "while" statement, if any.
2300
  ///
2301
  /// In the following example, "x" is the condition variable.
2302
  /// \code
2303
  /// while (int x = random()) {
2304
  ///   // ...
2305
  /// }
2306
  /// \endcode
2307
  VarDecl *getConditionVariable();
2308
8.41k
  const VarDecl *getConditionVariable() const {
2309
8.41k
    return const_cast<WhileStmt *>(this)->getConditionVariable();
2310
8.41k
  }
2311
2312
  /// Set the condition variable of this while statement.
2313
  /// The while statement must have storage for it.
2314
  void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
2315
2316
  /// If this WhileStmt has a condition variable, return the faux DeclStmt
2317
  /// associated with the creation of that condition variable.
2318
17.9k
  DeclStmt *getConditionVariableDeclStmt() {
2319
17.9k
    return hasVarStorage() ? static_cast<DeclStmt *>(
2320
214
                                 getTrailingObjects<Stmt *>()[varOffset()])
2321
17.9k
                           : 
nullptr17.7k
;
2322
17.9k
  }
2323
2324
0
  const DeclStmt *getConditionVariableDeclStmt() const {
2325
0
    return hasVarStorage() ? static_cast<DeclStmt *>(
2326
0
                                 getTrailingObjects<Stmt *>()[varOffset()])
2327
0
                           : nullptr;
2328
0
  }
2329
2330
12.8k
  SourceLocation getWhileLoc() const { return WhileStmtBits.WhileLoc; }
2331
41.8k
  void setWhileLoc(SourceLocation L) { WhileStmtBits.WhileLoc = L; }
2332
2333
5.62k
  SourceLocation getBeginLoc() const { return getWhileLoc(); }
2334
1.97k
  SourceLocation getEndLoc() const LLVM_READONLY {
2335
1.97k
    return getBody()->getEndLoc();
2336
1.97k
  }
2337
2338
64.4k
  static bool classof(const Stmt *T) {
2339
64.4k
    return T->getStmtClass() == WhileStmtClass;
2340
64.4k
  }
2341
2342
  // Iterators
2343
19.5k
  child_range children() {
2344
19.5k
    return child_range(getTrailingObjects<Stmt *>(),
2345
19.5k
                       getTrailingObjects<Stmt *>() +
2346
19.5k
                           numTrailingObjects(OverloadToken<Stmt *>()));
2347
19.5k
  }
2348
2349
0
  const_child_range children() const {
2350
0
    return const_child_range(getTrailingObjects<Stmt *>(),
2351
0
                             getTrailingObjects<Stmt *>() +
2352
0
                                 numTrailingObjects(OverloadToken<Stmt *>()));
2353
0
  }
2354
};
2355
2356
/// DoStmt - This represents a 'do/while' stmt.
2357
class DoStmt : public Stmt {
2358
  enum { BODY, COND, END_EXPR };
2359
  Stmt *SubExprs[END_EXPR];
2360
  SourceLocation WhileLoc;
2361
  SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
2362
2363
public:
2364
  DoStmt(Stmt *Body, Expr *Cond, SourceLocation DL, SourceLocation WL,
2365
         SourceLocation RP)
2366
7.76k
      : Stmt(DoStmtClass), WhileLoc(WL), RParenLoc(RP) {
2367
7.76k
    setCond(Cond);
2368
7.76k
    setBody(Body);
2369
7.76k
    setDoLoc(DL);
2370
7.76k
  }
2371
2372
  /// Build an empty do-while statement.
2373
20
  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
2374
2375
3.62k
  Expr *getCond() { return reinterpret_cast<Expr *>(SubExprs[COND]); }
2376
1.00k
  const Expr *getCond() const {
2377
1.00k
    return reinterpret_cast<Expr *>(SubExprs[COND]);
2378
1.00k
  }
2379
2380
7.78k
  void setCond(Expr *Cond) { SubExprs[COND] = reinterpret_cast<Stmt *>(Cond); }
2381
2382
3.32k
  Stmt *getBody() { return SubExprs[BODY]; }
2383
784
  const Stmt *getBody() const { return SubExprs[BODY]; }
2384
7.78k
  void setBody(Stmt *Body) { SubExprs[BODY] = Body; }
2385
2386
2.85k
  SourceLocation getDoLoc() const { return DoStmtBits.DoLoc; }
2387
7.78k
  void setDoLoc(SourceLocation L) { DoStmtBits.DoLoc = L; }
2388
2.07k
  SourceLocation getWhileLoc() const { return WhileLoc; }
2389
20
  void setWhileLoc(SourceLocation L) { WhileLoc = L; }
2390
2.27k
  SourceLocation getRParenLoc() const { return RParenLoc; }
2391
20
  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2392
2393
1.26k
  SourceLocation getBeginLoc() const { return getDoLoc(); }
2394
690
  SourceLocation getEndLoc() const { return getRParenLoc(); }
2395
2396
15.1k
  static bool classof(const Stmt *T) {
2397
15.1k
    return T->getStmtClass() == DoStmtClass;
2398
15.1k
  }
2399
2400
  // Iterators
2401
3.73k
  child_range children() {
2402
3.73k
    return child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
2403
3.73k
  }
2404
2405
0
  const_child_range children() const {
2406
0
    return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
2407
0
  }
2408
};
2409
2410
/// ForStmt - This represents a 'for (init;cond;inc)' stmt.  Note that any of
2411
/// the init/cond/inc parts of the ForStmt will be null if they were not
2412
/// specified in the source.
2413
class ForStmt : public Stmt {
2414
  enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
2415
  Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
2416
  SourceLocation LParenLoc, RParenLoc;
2417
2418
public:
2419
  ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
2420
          Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
2421
          SourceLocation RP);
2422
2423
  /// Build an empty for statement.
2424
6.15k
  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
2425
2426
269k
  Stmt *getInit() { return SubExprs[INIT]; }
2427
2428
  /// Retrieve the variable declared in this "for" statement, if any.
2429
  ///
2430
  /// In the following example, "y" is the condition variable.
2431
  /// \code
2432
  /// for (int x = random(); int y = mangle(x); ++x) {
2433
  ///   // ...
2434
  /// }
2435
  /// \endcode
2436
  VarDecl *getConditionVariable() const;
2437
  void setConditionVariable(const ASTContext &C, VarDecl *V);
2438
2439
  /// If this ForStmt has a condition variable, return the faux DeclStmt
2440
  /// associated with the creation of that condition variable.
2441
38
  const DeclStmt *getConditionVariableDeclStmt() const {
2442
38
    return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
2443
38
  }
2444
2445
306k
  Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
2446
252k
  Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
2447
464k
  Stmt *getBody() { return SubExprs[BODY]; }
2448
2449
17.2k
  const Stmt *getInit() const { return SubExprs[INIT]; }
2450
87.2k
  const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
2451
82.4k
  const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
2452
106k
  const Stmt *getBody() const { return SubExprs[BODY]; }
2453
2454
6.15k
  void setInit(Stmt *S) { SubExprs[INIT] = S; }
2455
6.15k
  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
2456
6.15k
  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
2457
6.15k
  void setBody(Stmt *S) { SubExprs[BODY] = S; }
2458
2459
732k
  SourceLocation getForLoc() const { return ForStmtBits.ForLoc; }
2460
6.15k
  void setForLoc(SourceLocation L) { ForStmtBits.ForLoc = L; }
2461
61.0k
  SourceLocation getLParenLoc() const { return LParenLoc; }
2462
6.15k
  void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2463
61.7k
  SourceLocation getRParenLoc() const { return RParenLoc; }
2464
6.15k
  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2465
2466
467k
  SourceLocation getBeginLoc() const { return getForLoc(); }
2467
38.4k
  SourceLocation getEndLoc() const { return getBody()->getEndLoc(); }
2468
2469
862k
  static bool classof(const Stmt *T) {
2470
862k
    return T->getStmtClass() == ForStmtClass;
2471
862k
  }
2472
2473
  // Iterators
2474
202k
  child_range children() {
2475
202k
    return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
2476
202k
  }
2477
2478
0
  const_child_range children() const {
2479
0
    return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
2480
0
  }
2481
};
2482
2483
/// GotoStmt - This represents a direct goto.
2484
class GotoStmt : public Stmt {
2485
  LabelDecl *Label;
2486
  SourceLocation LabelLoc;
2487
2488
public:
2489
  GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
2490
7.07k
      : Stmt(GotoStmtClass), Label(label), LabelLoc(LL) {
2491
7.07k
    setGotoLoc(GL);
2492
7.07k
  }
2493
2494
  /// Build an empty goto statement.
2495
6
  explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
2496
2497
17.7k
  LabelDecl *getLabel() const { return Label; }
2498
6
  void setLabel(LabelDecl *D) { Label = D; }
2499
2500
4.00k
  SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
2501
7.08k
  void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
2502
511
  SourceLocation getLabelLoc() const { return LabelLoc; }
2503
6
  void setLabelLoc(SourceLocation L) { LabelLoc = L; }
2504
2505
204
  SourceLocation getBeginLoc() const { return getGotoLoc(); }
2506
73
  SourceLocation getEndLoc() const { return getLabelLoc(); }
2507
2508
23.8k
  static bool classof(const Stmt *T) {
2509
23.8k
    return T->getStmtClass() == GotoStmtClass;
2510
23.8k
  }
2511
2512
  // Iterators
2513
8.54k
  child_range children() {
2514
8.54k
    return child_range(child_iterator(), child_iterator());
2515
8.54k
  }
2516
2517
0
  const_child_range children() const {
2518
0
    return const_child_range(const_child_iterator(), const_child_iterator());
2519
0
  }
2520
};
2521
2522
/// IndirectGotoStmt - This represents an indirect goto.
2523
class IndirectGotoStmt : public Stmt {
2524
  SourceLocation StarLoc;
2525
  Stmt *Target;
2526
2527
public:
2528
  IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc, Expr *target)
2529
114
      : Stmt(IndirectGotoStmtClass), StarLoc(starLoc) {
2530
114
    setTarget(target);
2531
114
    setGotoLoc(gotoLoc);
2532
114
  }
2533
2534
  /// Build an empty indirect goto statement.
2535
  explicit IndirectGotoStmt(EmptyShell Empty)
2536
1
      : Stmt(IndirectGotoStmtClass, Empty) {}
2537
2538
115
  void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
2539
61
  SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
2540
1
  void setStarLoc(SourceLocation L) { StarLoc = L; }
2541
5
  SourceLocation getStarLoc() const { return StarLoc; }
2542
2543
184
  Expr *getTarget() { return reinterpret_cast<Expr *>(Target); }
2544
30
  const Expr *getTarget() const {
2545
30
    return reinterpret_cast<const Expr *>(Target);
2546
30
  }
2547
115
  void setTarget(Expr *E) { Target = reinterpret_cast<Stmt *>(E); }
2548
2549
  /// getConstantTarget - Returns the fixed target of this indirect
2550
  /// goto, if one exists.
2551
  LabelDecl *getConstantTarget();
2552
22
  const LabelDecl *getConstantTarget() const {
2553
22
    return const_cast<IndirectGotoStmt *>(this)->getConstantTarget();
2554
22
  }
2555
2556
49
  SourceLocation getBeginLoc() const { return getGotoLoc(); }
2557
9
  SourceLocation getEndLoc() const LLVM_READONLY { return Target->getEndLoc(); }
2558
2559
4.68k
  static bool classof(const Stmt *T) {
2560
4.68k
    return T->getStmtClass() == IndirectGotoStmtClass;
2561
4.68k
  }
2562
2563
  // Iterators
2564
133
  child_range children() { return child_range(&Target, &Target + 1); }
2565
2566
0
  const_child_range children() const {
2567
0
    return const_child_range(&Target, &Target + 1);
2568
0
  }
2569
};
2570
2571
/// ContinueStmt - This represents a continue.
2572
class ContinueStmt : public Stmt {
2573
public:
2574
5.15k
  ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass) {
2575
5.15k
    setContinueLoc(CL);
2576
5.15k
  }
2577
2578
  /// Build an empty continue statement.
2579
7
  explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
2580
2581
4.40k
  SourceLocation getContinueLoc() const { return ContinueStmtBits.ContinueLoc; }
2582
5.15k
  void setContinueLoc(SourceLocation L) { ContinueStmtBits.ContinueLoc = L; }
2583
2584
4.23k
  SourceLocation getBeginLoc() const { return getContinueLoc(); }
2585
44
  SourceLocation getEndLoc() const { return getContinueLoc(); }
2586
2587
14.3k
  static bool classof(const Stmt *T) {
2588
14.3k
    return T->getStmtClass() == ContinueStmtClass;
2589
14.3k
  }
2590
2591
  // Iterators
2592
1.15k
  child_range children() {
2593
1.15k
    return child_range(child_iterator(), child_iterator());
2594
1.15k
  }
2595
2596
0
  const_child_range children() const {
2597
0
    return const_child_range(const_child_iterator(), const_child_iterator());
2598
0
  }
2599
};
2600
2601
/// BreakStmt - This represents a break.
2602
class BreakStmt : public Stmt {
2603
public:
2604
32.0k
  BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass) {
2605
32.0k
    setBreakLoc(BL);
2606
32.0k
  }
2607
2608
  /// Build an empty break statement.
2609
93
  explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
2610
2611
8.43k
  SourceLocation getBreakLoc() const { return BreakStmtBits.BreakLoc; }
2612
32.1k
  void setBreakLoc(SourceLocation L) { BreakStmtBits.BreakLoc = L; }
2613
2614
3.06k
  SourceLocation getBeginLoc() const { return getBreakLoc(); }
2615
350
  SourceLocation getEndLoc() const { return getBreakLoc(); }
2616
2617
14.0k
  static bool classof(const Stmt *T) {
2618
14.0k
    return T->getStmtClass() == BreakStmtClass;
2619
14.0k
  }
2620
2621
  // Iterators
2622
23.9k
  child_range children() {
2623
23.9k
    return child_range(child_iterator(), child_iterator());
2624
23.9k
  }
2625
2626
0
  const_child_range children() const {
2627
0
    return const_child_range(const_child_iterator(), const_child_iterator());
2628
0
  }
2629
};
2630
2631
/// ReturnStmt - This represents a return, optionally of an expression:
2632
///   return;
2633
///   return 4;
2634
///
2635
/// Note that GCC allows return with no argument in a function declared to
2636
/// return a value, and it allows returning a value in functions declared to
2637
/// return void.  We explicitly model this in the AST, which means you can't
2638
/// depend on the return type of the function and the presence of an argument.
2639
class ReturnStmt final
2640
    : public Stmt,
2641
      private llvm::TrailingObjects<ReturnStmt, const VarDecl *> {
2642
  friend TrailingObjects;
2643
2644
  /// The return expression.
2645
  Stmt *RetExpr;
2646
2647
  // ReturnStmt is followed optionally by a trailing "const VarDecl *"
2648
  // for the NRVO candidate. Present if and only if hasNRVOCandidate().
2649
2650
  /// True if this ReturnStmt has storage for an NRVO candidate.
2651
5.22M
  bool hasNRVOCandidate() const { return ReturnStmtBits.HasNRVOCandidate; }
2652
2653
0
  unsigned numTrailingObjects(OverloadToken<const VarDecl *>) const {
2654
0
    return hasNRVOCandidate();
2655
0
  }
2656
2657
  /// Build a return statement.
2658
  ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate);
2659
2660
  /// Build an empty return statement.
2661
  explicit ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate);
2662
2663
public:
2664
  /// Create a return statement.
2665
  static ReturnStmt *Create(const ASTContext &Ctx, SourceLocation RL, Expr *E,
2666
                            const VarDecl *NRVOCandidate);
2667
2668
  /// Create an empty return statement, optionally with
2669
  /// storage for an NRVO candidate.
2670
  static ReturnStmt *CreateEmpty(const ASTContext &Ctx, bool HasNRVOCandidate);
2671
2672
454k
  Expr *getRetValue() { return reinterpret_cast<Expr *>(RetExpr); }
2673
343k
  const Expr *getRetValue() const { return reinterpret_cast<Expr *>(RetExpr); }
2674
13.1k
  void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt *>(E); }
2675
2676
  /// Retrieve the variable that might be used for the named return
2677
  /// value optimization.
2678
  ///
2679
  /// The optimization itself can only be performed if the variable is
2680
  /// also marked as an NRVO object.
2681
5.18M
  const VarDecl *getNRVOCandidate() const {
2682
5.18M
    return hasNRVOCandidate() ? 
*getTrailingObjects<const VarDecl *>()108k
2683
5.18M
                              : 
nullptr5.07M
;
2684
5.18M
  }
2685
2686
  /// Set the variable that might be used for the named return value
2687
  /// optimization. The return statement must have storage for it,
2688
  /// which is the case if and only if hasNRVOCandidate() is true.
2689
42.5k
  void setNRVOCandidate(const VarDecl *Var) {
2690
42.5k
    assert(hasNRVOCandidate() &&
2691
42.5k
           "This return statement has no storage for an NRVO candidate!");
2692
42.5k
    *getTrailingObjects<const VarDecl *>() = Var;
2693
42.5k
  }
2694
2695
613k
  SourceLocation getReturnLoc() const { return ReturnStmtBits.RetLoc; }
2696
2.50M
  void setReturnLoc(SourceLocation L) { ReturnStmtBits.RetLoc = L; }
2697
2698
327k
  SourceLocation getBeginLoc() const { return getReturnLoc(); }
2699
110k
  SourceLocation getEndLoc() const LLVM_READONLY {
2700
110k
    return RetExpr ? 
RetExpr->getEndLoc()104k
:
getReturnLoc()5.39k
;
2701
110k
  }
2702
2703
5.83M
  static bool classof(const Stmt *T) {
2704
5.83M
    return T->getStmtClass() == ReturnStmtClass;
2705
5.83M
  }
2706
2707
  // Iterators
2708
461k
  child_range children() {
2709
461k
    if (RetExpr)
2710
443k
      return child_range(&RetExpr, &RetExpr + 1);
2711
17.3k
    return child_range(child_iterator(), child_iterator());
2712
17.3k
  }
2713
2714
0
  const_child_range children() const {
2715
0
    if (RetExpr)
2716
0
      return const_child_range(&RetExpr, &RetExpr + 1);
2717
0
    return const_child_range(const_child_iterator(), const_child_iterator());
2718
0
  }
2719
};
2720
2721
/// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
2722
class AsmStmt : public Stmt {
2723
protected:
2724
  friend class ASTStmtReader;
2725
2726
  SourceLocation AsmLoc;
2727
2728
  /// True if the assembly statement does not have any input or output
2729
  /// operands.
2730
  bool IsSimple;
2731
2732
  /// If true, treat this inline assembly as having side effects.
2733
  /// This assembly statement should not be optimized, deleted or moved.
2734
  bool IsVolatile;
2735
2736
  unsigned NumOutputs;
2737
  unsigned NumInputs;
2738
  unsigned NumClobbers;
2739
2740
  Stmt **Exprs = nullptr;
2741
2742
  AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
2743
          unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
2744
      : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
2745
        NumOutputs(numoutputs), NumInputs(numinputs),
2746
4.36k
        NumClobbers(numclobbers) {}
2747
2748
public:
2749
  /// Build an empty inline-assembly statement.
2750
9
  explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
2751
2752
275
  SourceLocation getAsmLoc() const { return AsmLoc; }
2753
9
  void setAsmLoc(SourceLocation L) { AsmLoc = L; }
2754
2755
5.37k
  bool isSimple() const { return IsSimple; }
2756
9
  void setSimple(bool V) { IsSimple = V; }
2757
2758
1.60k
  bool isVolatile() const { return IsVolatile; }
2759
9
  void setVolatile(bool V) { IsVolatile = V; }
2760
2761
0
  SourceLocation getBeginLoc() const LLVM_READONLY { return {}; }
2762
0
  SourceLocation getEndLoc() const LLVM_READONLY { return {}; }
2763
2764
  //===--- Asm String Analysis ---===//
2765
2766
  /// Assemble final IR asm string.
2767
  std::string generateAsmString(const ASTContext &C) const;
2768
2769
  //===--- Output operands ---===//
2770
2771
20.8k
  unsigned getNumOutputs() const { return NumOutputs; }
2772
2773
  /// getOutputConstraint - Return the constraint string for the specified
2774
  /// output operand.  All output constraints are known to be non-empty (either
2775
  /// '=' or '+').
2776
  StringRef getOutputConstraint(unsigned i) const;
2777
2778
  /// isOutputPlusConstraint - Return true if the specified output constraint
2779
  /// is a "+" constraint (which is both an input and an output) or false if it
2780
  /// is an "=" constraint (just an output).
2781
6.57k
  bool isOutputPlusConstraint(unsigned i) const {
2782
6.57k
    return getOutputConstraint(i)[0] == '+';
2783
6.57k
  }
2784
2785
  const Expr *getOutputExpr(unsigned i) const;
2786
2787
  /// getNumPlusOperands - Return the number of output operands that have a "+"
2788
  /// constraint.
2789
  unsigned getNumPlusOperands() const;
2790
2791
  //===--- Input operands ---===//
2792
2793
13.9k
  unsigned getNumInputs() const { return NumInputs; }
2794
2795
  /// getInputConstraint - Return the specified input constraint.  Unlike output
2796
  /// constraints, these can be empty.
2797
  StringRef getInputConstraint(unsigned i) const;
2798
2799
  const Expr *getInputExpr(unsigned i) const;
2800
2801
  //===--- Other ---===//
2802
2803
2.01k
  unsigned getNumClobbers() const { return NumClobbers; }
2804
  StringRef getClobber(unsigned i) const;
2805
2806
3.56k
  static bool classof(const Stmt *T) {
2807
3.56k
    return T->getStmtClass() == GCCAsmStmtClass ||
2808
3.56k
      
T->getStmtClass() == MSAsmStmtClass2.35k
;
2809
3.56k
  }
2810
2811
  // Input expr iterators.
2812
2813
  using inputs_iterator = ExprIterator;
2814
  using const_inputs_iterator = ConstExprIterator;
2815
  using inputs_range = llvm::iterator_range<inputs_iterator>;
2816
  using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
2817
2818
3
  inputs_iterator begin_inputs() {
2819
3
    return &Exprs[0] + NumOutputs;
2820
3
  }
2821
2822
3
  inputs_iterator end_inputs() {
2823
3
    return &Exprs[0] + NumOutputs + NumInputs;
2824
3
  }
2825
2826
3
  inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
2827
2828
0
  const_inputs_iterator begin_inputs() const {
2829
0
    return &Exprs[0] + NumOutputs;
2830
0
  }
2831
2832
0
  const_inputs_iterator end_inputs() const {
2833
0
    return &Exprs[0] + NumOutputs + NumInputs;
2834
0
  }
2835
2836
0
  inputs_const_range inputs() const {
2837
0
    return inputs_const_range(begin_inputs(), end_inputs());
2838
0
  }
2839
2840
  // Output expr iterators.
2841
2842
  using outputs_iterator = ExprIterator;
2843
  using const_outputs_iterator = ConstExprIterator;
2844
  using outputs_range = llvm::iterator_range<outputs_iterator>;
2845
  using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
2846
2847
3
  outputs_iterator begin_outputs() {
2848
3
    return &Exprs[0];
2849
3
  }
2850
2851
3
  outputs_iterator end_outputs() {
2852
3
    return &Exprs[0] + NumOutputs;
2853
3
  }
2854
2855
3
  outputs_range outputs() {
2856
3
    return outputs_range(begin_outputs(), end_outputs());
2857
3
  }
2858
2859
2
  const_outputs_iterator begin_outputs() const {
2860
2
    return &Exprs[0];
2861
2
  }
2862
2863
2
  const_outputs_iterator end_outputs() const {
2864
2
    return &Exprs[0] + NumOutputs;
2865
2
  }
2866
2867
2
  outputs_const_range outputs() const {
2868
2
    return outputs_const_range(begin_outputs(), end_outputs());
2869
2
  }
2870
2871
1.78k
  child_range children() {
2872
1.78k
    return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
2873
1.78k
  }
2874
2875
0
  const_child_range children() const {
2876
0
    return const_child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
2877
0
  }
2878
};
2879
2880
/// This represents a GCC inline-assembly statement extension.
2881
class GCCAsmStmt : public AsmStmt {
2882
  friend class ASTStmtReader;
2883
2884
  SourceLocation RParenLoc;
2885
  StringLiteral *AsmStr;
2886
2887
  // FIXME: If we wanted to, we could allocate all of these in one big array.
2888
  StringLiteral **Constraints = nullptr;
2889
  StringLiteral **Clobbers = nullptr;
2890
  IdentifierInfo **Names = nullptr;
2891
  unsigned NumLabels = 0;
2892
2893
public:
2894
  GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
2895
             bool isvolatile, unsigned numoutputs, unsigned numinputs,
2896
             IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
2897
             StringLiteral *asmstr, unsigned numclobbers,
2898
             StringLiteral **clobbers, unsigned numlabels,
2899
             SourceLocation rparenloc);
2900
2901
  /// Build an empty inline-assembly statement.
2902
9
  explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
2903
2904
117
  SourceLocation getRParenLoc() const { return RParenLoc; }
2905
9
  void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2906
2907
  //===--- Asm String Analysis ---===//
2908
2909
18.8k
  const StringLiteral *getAsmString() const { return AsmStr; }
2910
301
  StringLiteral *getAsmString() { return AsmStr; }
2911
9
  void setAsmString(StringLiteral *E) { AsmStr = E; }
2912
2913
  /// AsmStringPiece - this is part of a decomposed asm string specification
2914
  /// (for use with the AnalyzeAsmString function below).  An asm string is
2915
  /// considered to be a concatenation of these parts.
2916
  class AsmStringPiece {
2917
  public:
2918
    enum Kind {
2919
      String,  // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
2920
      Operand  // Operand reference, with optional modifier %c4.
2921
    };
2922
2923
  private:
2924
    Kind MyKind;
2925
    std::string Str;
2926
    unsigned OperandNo;
2927
2928
    // Source range for operand references.
2929
    CharSourceRange Range;
2930
2931
  public:
2932
8.69k
    AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
2933
    AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
2934
                   SourceLocation End)
2935
        : MyKind(Operand), Str(S), OperandNo(OpNo),
2936
6.17k
          Range(CharSourceRange::getCharRange(Begin, End)) {}
2937
2938
4.24k
    bool isString() const { return MyKind == String; }
2939
23.0k
    bool isOperand() const { return MyKind == Operand; }
2940
2941
2.36k
    const std::string &getString() const { return Str; }
2942
2943
6.18k
    unsigned getOperandNo() const {
2944
6.18k
      assert(isOperand());
2945
6.18k
      return OperandNo;
2946
6.18k
    }
2947
2948
32
    CharSourceRange getRange() const {
2949
32
      assert(isOperand() && "Range is currently used only for Operands.");
2950
32
      return Range;
2951
32
    }
2952
2953
    /// getModifier - Get the modifier for this operand, if present.  This
2954
    /// returns '\0' if there was no modifier.
2955
    char getModifier() const;
2956
  };
2957
2958
  /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
2959
  /// it into pieces.  If the asm string is erroneous, emit errors and return
2960
  /// true, otherwise return false.  This handles canonicalization and
2961
  /// translation of strings from GCC syntax to LLVM IR syntax, and handles
2962
  //// flattening of named references like %[foo] to Operand AsmStringPiece's.
2963
  unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
2964
                            const ASTContext &C, unsigned &DiagOffs) const;
2965
2966
  /// Assemble final IR asm string.
2967
  std::string generateAsmString(const ASTContext &C) const;
2968
2969
  //===--- Output operands ---===//
2970
2971
1.79k
  IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; }
2972
2973
1.46k
  StringRef getOutputName(unsigned i) const {
2974
1.46k
    if (IdentifierInfo *II = getOutputIdentifier(i))
2975
150
      return II->getName();
2976
1.31k
2977
1.31k
    return {};
2978
1.31k
  }
2979
2980
  StringRef getOutputConstraint(unsigned i) const;
2981
2982
8.93k
  const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
2983
8.93k
    return Constraints[i];
2984
8.93k
  }
2985
506
  StringLiteral *getOutputConstraintLiteral(unsigned i) {
2986
506
    return Constraints[i];
2987
506
  }
2988
2989
  Expr *getOutputExpr(unsigned i);
2990
2991
1.18k
  const Expr *getOutputExpr(unsigned i) const {
2992
1.18k
    return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
2993
1.18k
  }
2994
2995
  //===--- Input operands ---===//
2996
2997
1.71k
  IdentifierInfo *getInputIdentifier(unsigned i) const {
2998
1.71k
    return Names[i + NumOutputs];
2999
1.71k
  }
3000
3001
1.47k
  StringRef getInputName(unsigned i) const {
3002
1.47k
    if (IdentifierInfo *II = getInputIdentifier(i))
3003
127
      return II->getName();
3004
1.34k
3005
1.34k
    return {};
3006
1.34k
  }
3007
3008
  StringRef getInputConstraint(unsigned i) const;
3009
3010
2.51k
  const StringLiteral *getInputConstraintLiteral(unsigned i) const {
3011
2.51k
    return Constraints[i + NumOutputs];
3012
2.51k
  }
3013
374
  StringLiteral *getInputConstraintLiteral(unsigned i) {
3014
374
    return Constraints[i + NumOutputs];
3015
374
  }
3016
3017
  Expr *getInputExpr(unsigned i);
3018
  void setInputExpr(unsigned i, Expr *E);
3019
3020
1.17k
  const Expr *getInputExpr(unsigned i) const {
3021
1.17k
    return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
3022
1.17k
  }
3023
3024
  //===--- Labels ---===//
3025
3026
9.72k
  bool isAsmGoto() const {
3027
9.72k
    return NumLabels > 0;
3028
9.72k
  }
3029
3030
6.39k
  unsigned getNumLabels() const {
3031
6.39k
    return NumLabels;
3032
6.39k
  }
3033
3034
2
  IdentifierInfo *getLabelIdentifier(unsigned i) const {
3035
2
    return Names[i + NumInputs];
3036
2
  }
3037
3038
  AddrLabelExpr *getLabelExpr(unsigned i) const;
3039
  StringRef getLabelName(unsigned i) const;
3040
  using labels_iterator = CastIterator<AddrLabelExpr>;
3041
  using const_labels_iterator = ConstCastIterator<AddrLabelExpr>;
3042
  using labels_range = llvm::iterator_range<labels_iterator>;
3043
  using labels_const_range = llvm::iterator_range<const_labels_iterator>;
3044
3045
125
  labels_iterator begin_labels() {
3046
125
    return &Exprs[0] + NumInputs;
3047
125
  }
3048
3049
125
  labels_iterator end_labels() {
3050
125
    return &Exprs[0] + NumInputs + NumLabels;
3051
125
  }
3052
3053
125
  labels_range labels() {
3054
125
    return labels_range(begin_labels(), end_labels());
3055
125
  }
3056
3057
140
  const_labels_iterator begin_labels() const {
3058
140
    return &Exprs[0] + NumInputs;
3059
140
  }
3060
3061
140
  const_labels_iterator end_labels() const {
3062
140
    return &Exprs[0] + NumInputs + NumLabels;
3063
140
  }
3064
3065
140
  labels_const_range labels() const {
3066
140
    return labels_const_range(begin_labels(), end_labels());
3067
140
  }
3068
3069
private:
3070
  void setOutputsAndInputsAndClobbers(const ASTContext &C,
3071
                                      IdentifierInfo **Names,
3072
                                      StringLiteral **Constraints,
3073
                                      Stmt **Exprs,
3074
                                      unsigned NumOutputs,
3075
                                      unsigned NumInputs,
3076
                                      unsigned NumLabels,
3077
                                      StringLiteral **Clobbers,
3078
                                      unsigned NumClobbers);
3079
3080
public:
3081
  //===--- Other ---===//
3082
3083
  /// getNamedOperand - Given a symbolic operand reference like %[foo],
3084
  /// translate this into a numeric value needed to reference the same operand.
3085
  /// This returns -1 if the operand name is invalid.
3086
  int getNamedOperand(StringRef SymbolicName) const;
3087
3088
  StringRef getClobber(unsigned i) const;
3089
3090
122
  StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
3091
852
  const StringLiteral *getClobberStringLiteral(unsigned i) const {
3092
852
    return Clobbers[i];
3093
852
  }
3094
3095
62
  SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
3096
15
  SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
3097
3098
41.3k
  static bool classof(const Stmt *T) {
3099
41.3k
    return T->getStmtClass() == GCCAsmStmtClass;
3100
41.3k
  }
3101
};
3102
3103
/// This represents a Microsoft inline-assembly statement extension.
3104
class MSAsmStmt : public AsmStmt {
3105
  friend class ASTStmtReader;
3106
3107
  SourceLocation LBraceLoc, EndLoc;
3108
  StringRef AsmStr;
3109
3110
  unsigned NumAsmToks = 0;
3111
3112
  Token *AsmToks = nullptr;
3113
  StringRef *Constraints = nullptr;
3114
  StringRef *Clobbers = nullptr;
3115
3116
public:
3117
  MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
3118
            SourceLocation lbraceloc, bool issimple, bool isvolatile,
3119
            ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
3120
            ArrayRef<StringRef> constraints,
3121
            ArrayRef<Expr*> exprs, StringRef asmstr,
3122
            ArrayRef<StringRef> clobbers, SourceLocation endloc);
3123
3124
  /// Build an empty MS-style inline-assembly statement.
3125
0
  explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
3126
3127
1
  SourceLocation getLBraceLoc() const { return LBraceLoc; }
3128
0
  void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
3129
1
  SourceLocation getEndLoc() const { return EndLoc; }
3130
0
  void setEndLoc(SourceLocation L) { EndLoc = L; }
3131
3132
0
  bool hasBraces() const { return LBraceLoc.isValid(); }
3133
3134
1
  unsigned getNumAsmToks() { return NumAsmToks; }
3135
1
  Token *getAsmToks() { return AsmToks; }
3136
3137
  //===--- Asm String Analysis ---===//
3138
1
  StringRef getAsmString() const { return AsmStr; }
3139
3140
  /// Assemble final IR asm string.
3141
  std::string generateAsmString(const ASTContext &C) const;
3142
3143
  //===--- Output operands ---===//
3144
3145
42
  StringRef getOutputConstraint(unsigned i) const {
3146
42
    assert(i < NumOutputs);
3147
42
    return Constraints[i];
3148
42
  }
3149
3150
  Expr *getOutputExpr(unsigned i);
3151
3152
21
  const Expr *getOutputExpr(unsigned i) const {
3153
21
    return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
3154
21
  }
3155
3156
  //===--- Input operands ---===//
3157
3158
216
  StringRef getInputConstraint(unsigned i) const {
3159
216
    assert(i < NumInputs);
3160
216
    return Constraints[i + NumOutputs];
3161
216
  }
3162
3163
  Expr *getInputExpr(unsigned i);
3164
  void setInputExpr(unsigned i, Expr *E);
3165
3166
108
  const Expr *getInputExpr(unsigned i) const {
3167
108
    return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
3168
108
  }
3169
3170
  //===--- Other ---===//
3171
3172
1
  ArrayRef<StringRef> getAllConstraints() const {
3173
1
    return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
3174
1
  }
3175
3176
189
  ArrayRef<StringRef> getClobbers() const {
3177
189
    return llvm::makeArrayRef(Clobbers, NumClobbers);
3178
189
  }
3179
3180
1
  ArrayRef<Expr*> getAllExprs() const {
3181
1
    return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
3182
1
                              NumInputs + NumOutputs);
3183
1
  }
3184
3185
188
  StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
3186
3187
private:
3188
  void initialize(const ASTContext &C, StringRef AsmString,
3189
                  ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
3190
                  ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
3191
3192
public:
3193
0
  SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
3194
3195
4.41k
  static bool classof(const Stmt *T) {
3196
4.41k
    return T->getStmtClass() == MSAsmStmtClass;
3197
4.41k
  }
3198
3199
35
  child_range children() {
3200
35
    return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
3201
35
  }
3202
3203
0
  const_child_range children() const {
3204
0
    return const_child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
3205
0
  }
3206
};
3207
3208
class SEHExceptStmt : public Stmt {
3209
  friend class ASTReader;
3210
  friend class ASTStmtReader;
3211
3212
  SourceLocation  Loc;
3213
  Stmt *Children[2];
3214
3215
  enum { FILTER_EXPR, BLOCK };
3216
3217
  SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
3218
1
  explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
3219
3220
public:
3221
  static SEHExceptStmt* Create(const ASTContext &C,
3222
                               SourceLocation ExceptLoc,
3223
                               Expr *FilterExpr,
3224
                               Stmt *Block);
3225
3226
24
  SourceLocation getBeginLoc() const LLVM_READONLY { return getExceptLoc(); }
3227
3228
32
  SourceLocation getExceptLoc() const { return Loc; }
3229
48
  SourceLocation getEndLoc() const { return getBlock()->getEndLoc(); }
3230
3231
87
  Expr *getFilterExpr() const {
3232
87
    return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
3233
87
  }
3234
3235
173
  CompoundStmt *getBlock() const {
3236
173
    return cast<CompoundStmt>(Children[BLOCK]);
3237
173
  }
3238
3239
8
  child_range children() {
3240
8
    return child_range(Children, Children+2);
3241
8
  }
3242
3243
0
  const_child_range children() const {
3244
0
    return const_child_range(Children, Children + 2);
3245
0
  }
3246
3247
400
  static bool classof(const Stmt *T) {
3248
400
    return T->getStmtClass() == SEHExceptStmtClass;
3249
400
  }
3250
};
3251
3252
class SEHFinallyStmt : public Stmt {
3253
  friend class ASTReader;
3254
  friend class ASTStmtReader;
3255
3256
  SourceLocation  Loc;
3257
  Stmt *Block;
3258
3259
  SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
3260
0
  explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
3261
3262
public:
3263
  static SEHFinallyStmt* Create(const ASTContext &C,
3264
                                SourceLocation FinallyLoc,
3265
                                Stmt *Block);
3266
3267
53
  SourceLocation getBeginLoc() const LLVM_READONLY { return getFinallyLoc(); }
3268
3269
54
  SourceLocation getFinallyLoc() const { return Loc; }
3270
106
  SourceLocation getEndLoc() const { return Block->getEndLoc(); }
3271
3272
129
  CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
3273
3274
53
  child_range children() {
3275
53
    return child_range(&Block,&Block+1);
3276
53
  }
3277
3278
0
  const_child_range children() const {
3279
0
    return const_child_range(&Block, &Block + 1);
3280
0
  }
3281
3282
601
  static bool classof(const Stmt *T) {
3283
601
    return T->getStmtClass() == SEHFinallyStmtClass;
3284
601
  }
3285
};
3286
3287
class SEHTryStmt : public Stmt {
3288
  friend class ASTReader;
3289
  friend class ASTStmtReader;
3290
3291
  bool IsCXXTry;
3292
  SourceLocation  TryLoc;
3293
  Stmt *Children[2];
3294
3295
  enum { TRY = 0, HANDLER = 1 };
3296
3297
  SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
3298
             SourceLocation TryLoc,
3299
             Stmt *TryBlock,
3300
             Stmt *Handler);
3301
3302
1
  explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
3303
3304
public:
3305
  static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
3306
                            SourceLocation TryLoc, Stmt *TryBlock,
3307
                            Stmt *Handler);
3308
3309
77
  SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
3310
3311
84
  SourceLocation getTryLoc() const { return TryLoc; }
3312
77
  SourceLocation getEndLoc() const { return Children[HANDLER]->getEndLoc(); }
3313
3314
7
  bool getIsCXXTry() const { return IsCXXTry; }
3315
3316
309
  CompoundStmt* getTryBlock() const {
3317
309
    return cast<CompoundStmt>(Children[TRY]);
3318
309
  }
3319
3320
603
  Stmt *getHandler() const { return Children[HANDLER]; }
3321
3322
  /// Returns 0 if not defined
3323
  SEHExceptStmt  *getExceptHandler() const;
3324
  SEHFinallyStmt *getFinallyHandler() const;
3325
3326
61
  child_range children() {
3327
61
    return child_range(Children, Children+2);
3328
61
  }
3329
3330
0
  const_child_range children() const {
3331
0
    return const_child_range(Children, Children + 2);
3332
0
  }
3333
3334
276
  static bool classof(const Stmt *T) {
3335
276
    return T->getStmtClass() == SEHTryStmtClass;
3336
276
  }
3337
};
3338
3339
/// Represents a __leave statement.
3340
class SEHLeaveStmt : public Stmt {
3341
  SourceLocation LeaveLoc;
3342
3343
public:
3344
  explicit SEHLeaveStmt(SourceLocation LL)
3345
24
      : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
3346
3347
  /// Build an empty __leave statement.
3348
0
  explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
3349
3350
0
  SourceLocation getLeaveLoc() const { return LeaveLoc; }
3351
0
  void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
3352
3353
0
  SourceLocation getBeginLoc() const LLVM_READONLY { return LeaveLoc; }
3354
0
  SourceLocation getEndLoc() const LLVM_READONLY { return LeaveLoc; }
3355
3356
17
  static bool classof(const Stmt *T) {
3357
17
    return T->getStmtClass() == SEHLeaveStmtClass;
3358
17
  }
3359
3360
  // Iterators
3361
6
  child_range children() {
3362
6
    return child_range(child_iterator(), child_iterator());
3363
6
  }
3364
3365
0
  const_child_range children() const {
3366
0
    return const_child_range(const_child_iterator(), const_child_iterator());
3367
0
  }
3368
};
3369
3370
/// This captures a statement into a function. For example, the following
3371
/// pragma annotated compound statement can be represented as a CapturedStmt,
3372
/// and this compound statement is the body of an anonymous outlined function.
3373
/// @code
3374
/// #pragma omp parallel
3375
/// {
3376
///   compute();
3377
/// }
3378
/// @endcode
3379
class CapturedStmt : public Stmt {
3380
public:
3381
  /// The different capture forms: by 'this', by reference, capture for
3382
  /// variable-length array type etc.
3383
  enum VariableCaptureKind {
3384
    VCK_This,
3385
    VCK_ByRef,
3386
    VCK_ByCopy,
3387
    VCK_VLAType,
3388
  };
3389
3390
  /// Describes the capture of either a variable, or 'this', or
3391
  /// variable-length array type.
3392
  class Capture {
3393
    llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
3394
    SourceLocation Loc;
3395
3396
  public:
3397
    friend class ASTStmtReader;
3398
3399
    /// Create a new capture.
3400
    ///
3401
    /// \param Loc The source location associated with this capture.
3402
    ///
3403
    /// \param Kind The kind of capture (this, ByRef, ...).
3404
    ///
3405
    /// \param Var The variable being captured, or null if capturing this.
3406
    Capture(SourceLocation Loc, VariableCaptureKind Kind,
3407
            VarDecl *Var = nullptr);
3408
3409
    /// Determine the kind of capture.
3410
    VariableCaptureKind getCaptureKind() const;
3411
3412
    /// Retrieve the source location at which the variable or 'this' was
3413
    /// first used.
3414
324k
    SourceLocation getLocation() const { return Loc; }
3415
3416
    /// Determine whether this capture handles the C++ 'this' pointer.
3417
122k
    bool capturesThis() const { return getCaptureKind() == VCK_This; }
3418
3419
    /// Determine whether this capture handles a variable (by reference).
3420
2.38M
    bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
3421
3422
    /// Determine whether this capture handles a variable by copy.
3423
846k
    bool capturesVariableByCopy() const {
3424
846k
      return getCaptureKind() == VCK_ByCopy;
3425
846k
    }
3426
3427
    /// Determine whether this capture handles a variable-length array
3428
    /// type.
3429
63.8k
    bool capturesVariableArrayType() const {
3430
63.8k
      return getCaptureKind() == VCK_VLAType;
3431
63.8k
    }
3432
3433
    /// Retrieve the declaration of the variable being captured.
3434
    ///
3435
    /// This operation is only valid if this capture captures a variable.
3436
    VarDecl *getCapturedVar() const;
3437
  };
3438
3439
private:
3440
  /// The number of variable captured, including 'this'.
3441
  unsigned NumCaptures;
3442
3443
  /// The pointer part is the implicit the outlined function and the
3444
  /// int part is the captured region kind, 'CR_Default' etc.
3445
  llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind;
3446
3447
  /// The record for captured variables, a RecordDecl or CXXRecordDecl.
3448
  RecordDecl *TheRecordDecl = nullptr;
3449
3450
  /// Construct a captured statement.
3451
  CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
3452
               ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
3453
3454
  /// Construct an empty captured statement.
3455
  CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
3456
3457
2.76M
  Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
3458
3459
958k
  Stmt *const *getStoredStmts() const {
3460
958k
    return reinterpret_cast<Stmt *const *>(this + 1);
3461
958k
  }
3462
3463
  Capture *getStoredCaptures() const;
3464
3465
22.9k
  void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
3466
3467
public:
3468
  friend class ASTStmtReader;
3469
3470
  static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
3471
                              CapturedRegionKind Kind,
3472
                              ArrayRef<Capture> Captures,
3473
                              ArrayRef<Expr *> CaptureInits,
3474
                              CapturedDecl *CD, RecordDecl *RD);
3475
3476
  static CapturedStmt *CreateDeserialized(const ASTContext &Context,
3477
                                          unsigned NumCaptures);
3478
3479
  /// Retrieve the statement being captured.
3480
1.63M
  Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
3481
930k
  const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
3482
3483
  /// Retrieve the outlined function declaration.
3484
  CapturedDecl *getCapturedDecl();
3485
  const CapturedDecl *getCapturedDecl() const;
3486
3487
  /// Set the outlined function declaration.
3488
  void setCapturedDecl(CapturedDecl *D);
3489
3490
  /// Retrieve the captured region kind.
3491
  CapturedRegionKind getCapturedRegionKind() const;
3492
3493
  /// Set the captured region kind.
3494
  void setCapturedRegionKind(CapturedRegionKind Kind);
3495
3496
  /// Retrieve the record declaration for captured variables.
3497
74.9k
  const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
3498
3499
  /// Set the record declaration for captured variables.
3500
22.9k
  void setCapturedRecordDecl(RecordDecl *D) {
3501
22.9k
    assert(D && "null RecordDecl");
3502
22.9k
    TheRecordDecl = D;
3503
22.9k
  }
3504
3505
  /// True if this variable has been captured.
3506
  bool capturesVariable(const VarDecl *Var) const;
3507
3508
  /// An iterator that walks over the captures.
3509
  using capture_iterator = Capture *;
3510
  using const_capture_iterator = const Capture *;
3511
  using capture_range = llvm::iterator_range<capture_iterator>;
3512
  using capture_const_range = llvm::iterator_range<const_capture_iterator>;
3513
3514
265k
  capture_range captures() {
3515
265k
    return capture_range(capture_begin(), capture_end());
3516
265k
  }
3517
647k
  capture_const_range captures() const {
3518
647k
    return capture_const_range(capture_begin(), capture_end());
3519
647k
  }
3520
3521
  /// Retrieve an iterator pointing to the first capture.
3522
289k
  capture_iterator capture_begin() { return getStoredCaptures(); }
3523
668k
  const_capture_iterator capture_begin() const { return getStoredCaptures(); }
3524
3525
  /// Retrieve an iterator pointing past the end of the sequence of
3526
  /// captures.
3527
958k
  capture_iterator capture_end() const {
3528
958k
    return getStoredCaptures() + NumCaptures;
3529
958k
  }
3530
3531
  /// Retrieve the number of captures, including 'this'.
3532
62
  unsigned capture_size() const { return NumCaptures; }
3533
3534
  /// Iterator that walks over the capture initialization arguments.
3535
  using capture_init_iterator = Expr **;
3536
  using capture_init_range = llvm::iterator_range<capture_init_iterator>;
3537
3538
  /// Const iterator that walks over the capture initialization
3539
  /// arguments.
3540
  using const_capture_init_iterator = Expr *const *;
3541
  using const_capture_init_range =
3542
      llvm::iterator_range<const_capture_init_iterator>;
3543
3544
23.6k
  capture_init_range capture_inits() {
3545
23.6k
    return capture_init_range(capture_init_begin(), capture_init_end());
3546
23.6k
  }
3547
3548
0
  const_capture_init_range capture_inits() const {
3549
0
    return const_capture_init_range(capture_init_begin(), capture_init_end());
3550
0
  }
3551
3552
  /// Retrieve the first initialization argument.
3553
93.1k
  capture_init_iterator capture_init_begin() {
3554
93.1k
    return reinterpret_cast<Expr **>(getStoredStmts());
3555
93.1k
  }
3556
3557
27.9k
  const_capture_init_iterator capture_init_begin() const {
3558
27.9k
    return reinterpret_cast<Expr *const *>(getStoredStmts());
3559
27.9k
  }
3560
3561
  /// Retrieve the iterator pointing one past the last initialization
3562
  /// argument.
3563
46.5k
  capture_init_iterator capture_init_end() {
3564
46.5k
    return capture_init_begin() + NumCaptures;
3565
46.5k
  }
3566
3567
13.9k
  const_capture_init_iterator capture_init_end() const {
3568
13.9k
    return capture_init_begin() + NumCaptures;
3569
13.9k
  }
3570
3571
714k
  SourceLocation getBeginLoc() const LLVM_READONLY {
3572
714k
    return getCapturedStmt()->getBeginLoc();
3573
714k
  }
3574
3575
7
  SourceLocation getEndLoc() const LLVM_READONLY {
3576
7
    return getCapturedStmt()->getEndLoc();
3577
7
  }
3578
3579
6.88k
  SourceRange getSourceRange() const LLVM_READONLY {
3580
6.88k
    return getCapturedStmt()->getSourceRange();
3581
6.88k
  }
3582
3583
2.42M
  static bool classof(const Stmt *T) {
3584
2.42M
    return T->getStmtClass() == CapturedStmtClass;
3585
2.42M
  }
3586
3587
  child_range children();
3588
3589
  const_child_range children() const;
3590
};
3591
3592
} // namespace clang
3593
3594
#endif // LLVM_CLANG_AST_STMT_H