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