1 //===- Stmt.h - Classes for representing statements -------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the Stmt interface and subclasses.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_STMT_H
14 #define LLVM_CLANG_AST_STMT_H
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"
38 class FoldingSetNodeID;
53 struct PrintingPolicy;
60 //===----------------------------------------------------------------------===//
61 // AST classes for statements.
62 //===----------------------------------------------------------------------===//
64 /// Stmt - This represents one statement.
66 class alignas(void *) Stmt {
70 #define STMT(CLASS, PARENT) CLASS##Class,
71 #define STMT_RANGE(BASE, FIRST, LAST) \
72 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
73 #define LAST_STMT_RANGE(BASE, FIRST, LAST) \
74 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
75 #define ABSTRACT_STMT(STMT)
76 #include "clang/AST/StmtNodes.inc"
79 // Make vanilla 'new' and 'delete' illegal for Stmts.
81 friend class ASTStmtReader;
82 friend class ASTStmtWriter;
84 void *operator new(size_t bytes) noexcept {
85 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
88 void operator delete(void *data) noexcept {
89 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
92 //===--- Statement bitfields classes ---===//
95 friend class ASTStmtReader;
96 friend class ASTStmtWriter;
99 /// The statement class.
102 /// This bit is set only for the Stmts that are the structured-block of
103 /// OpenMP executable directives. Directives that have a structured block
104 /// are called "non-standalone" directives.
105 /// I.e. those returned by OMPExecutableDirective::getStructuredBlock().
106 unsigned IsOMPStructuredBlock : 1;
108 enum { NumStmtBits = 9 };
110 class NullStmtBitfields {
111 friend class ASTStmtReader;
112 friend class ASTStmtWriter;
113 friend class NullStmt;
115 unsigned : NumStmtBits;
117 /// True if the null statement was preceded by an empty macro, e.g:
122 unsigned HasLeadingEmptyMacro : 1;
124 /// The location of the semi-colon.
125 SourceLocation SemiLoc;
128 class CompoundStmtBitfields {
129 friend class ASTStmtReader;
130 friend class CompoundStmt;
132 unsigned : NumStmtBits;
134 unsigned NumStmts : 32 - NumStmtBits;
136 /// The location of the opening "{".
137 SourceLocation LBraceLoc;
140 class LabelStmtBitfields {
141 friend class LabelStmt;
143 unsigned : NumStmtBits;
145 SourceLocation IdentLoc;
148 class AttributedStmtBitfields {
149 friend class ASTStmtReader;
150 friend class AttributedStmt;
152 unsigned : NumStmtBits;
154 /// Number of attributes.
155 unsigned NumAttrs : 32 - NumStmtBits;
157 /// The location of the attribute.
158 SourceLocation AttrLoc;
161 class IfStmtBitfields {
162 friend class ASTStmtReader;
165 unsigned : NumStmtBits;
167 /// True if this if statement is a constexpr if.
168 unsigned IsConstexpr : 1;
170 /// True if this if statement has storage for an else statement.
171 unsigned HasElse : 1;
173 /// True if this if statement has storage for a variable declaration.
176 /// True if this if statement has storage for an init statement.
177 unsigned HasInit : 1;
179 /// The location of the "if".
180 SourceLocation IfLoc;
183 class SwitchStmtBitfields {
184 friend class SwitchStmt;
186 unsigned : NumStmtBits;
188 /// True if the SwitchStmt has storage for an init statement.
189 unsigned HasInit : 1;
191 /// True if the SwitchStmt has storage for a condition variable.
194 /// If the SwitchStmt is a switch on an enum value, records whether all
195 /// the enum values were covered by CaseStmts. The coverage information
196 /// value is meant to be a hint for possible clients.
197 unsigned AllEnumCasesCovered : 1;
199 /// The location of the "switch".
200 SourceLocation SwitchLoc;
203 class WhileStmtBitfields {
204 friend class ASTStmtReader;
205 friend class WhileStmt;
207 unsigned : NumStmtBits;
209 /// True if the WhileStmt has storage for a condition variable.
212 /// The location of the "while".
213 SourceLocation WhileLoc;
216 class DoStmtBitfields {
219 unsigned : NumStmtBits;
221 /// The location of the "do".
222 SourceLocation DoLoc;
225 class ForStmtBitfields {
226 friend class ForStmt;
228 unsigned : NumStmtBits;
230 /// The location of the "for".
231 SourceLocation ForLoc;
234 class GotoStmtBitfields {
235 friend class GotoStmt;
236 friend class IndirectGotoStmt;
238 unsigned : NumStmtBits;
240 /// The location of the "goto".
241 SourceLocation GotoLoc;
244 class ContinueStmtBitfields {
245 friend class ContinueStmt;
247 unsigned : NumStmtBits;
249 /// The location of the "continue".
250 SourceLocation ContinueLoc;
253 class BreakStmtBitfields {
254 friend class BreakStmt;
256 unsigned : NumStmtBits;
258 /// The location of the "break".
259 SourceLocation BreakLoc;
262 class ReturnStmtBitfields {
263 friend class ReturnStmt;
265 unsigned : NumStmtBits;
267 /// True if this ReturnStmt has storage for an NRVO candidate.
268 unsigned HasNRVOCandidate : 1;
270 /// The location of the "return".
271 SourceLocation RetLoc;
274 class SwitchCaseBitfields {
275 friend class SwitchCase;
276 friend class CaseStmt;
278 unsigned : NumStmtBits;
280 /// Used by CaseStmt to store whether it is a case statement
281 /// of the form case LHS ... RHS (a GNU extension).
282 unsigned CaseStmtIsGNURange : 1;
284 /// The location of the "case" or "default" keyword.
285 SourceLocation KeywordLoc;
288 //===--- Expression bitfields classes ---===//
290 class ExprBitfields {
291 friend class ASTStmtReader; // deserialization
292 friend class AtomicExpr; // ctor
293 friend class BlockDeclRefExpr; // ctor
294 friend class CallExpr; // ctor
295 friend class CXXConstructExpr; // ctor
296 friend class CXXDependentScopeMemberExpr; // ctor
297 friend class CXXNewExpr; // ctor
298 friend class CXXUnresolvedConstructExpr; // ctor
299 friend class DeclRefExpr; // computeDependence
300 friend class DependentScopeDeclRefExpr; // ctor
301 friend class DesignatedInitExpr; // ctor
303 friend class InitListExpr; // ctor
304 friend class ObjCArrayLiteral; // ctor
305 friend class ObjCDictionaryLiteral; // ctor
306 friend class ObjCMessageExpr; // ctor
307 friend class OffsetOfExpr; // ctor
308 friend class OpaqueValueExpr; // ctor
309 friend class OverloadExpr; // ctor
310 friend class ParenListExpr; // ctor
311 friend class PseudoObjectExpr; // ctor
312 friend class ShuffleVectorExpr; // ctor
314 unsigned : NumStmtBits;
316 unsigned ValueKind : 2;
317 unsigned ObjectKind : 3;
318 unsigned TypeDependent : 1;
319 unsigned ValueDependent : 1;
320 unsigned InstantiationDependent : 1;
321 unsigned ContainsUnexpandedParameterPack : 1;
323 enum { NumExprBits = NumStmtBits + 9 };
325 class ConstantExprBitfields {
326 friend class ASTStmtReader;
327 friend class ASTStmtWriter;
328 friend class ConstantExpr;
330 unsigned : NumExprBits;
332 /// The kind of result that is trail-allocated.
333 unsigned ResultKind : 2;
335 /// Kind of Result as defined by APValue::Kind
336 unsigned APValueKind : 4;
338 /// When ResultKind == RSK_Int64. whether the trail-allocated integer is
340 unsigned IsUnsigned : 1;
342 /// When ResultKind == RSK_Int64. the BitWidth of the trail-allocated
343 /// integer. 7 bits because it is the minimal number of bit to represent a
344 /// value from 0 to 64 (the size of the trail-allocated number).
345 unsigned BitWidth : 7;
347 /// When ResultKind == RSK_APValue. Wether the ASTContext will cleanup the
348 /// destructor on the trail-allocated APValue.
349 unsigned HasCleanup : 1;
352 class PredefinedExprBitfields {
353 friend class ASTStmtReader;
354 friend class PredefinedExpr;
356 unsigned : NumExprBits;
358 /// The kind of this PredefinedExpr. One of the enumeration values
359 /// in PredefinedExpr::IdentKind.
362 /// True if this PredefinedExpr has a trailing "StringLiteral *"
363 /// for the predefined identifier.
364 unsigned HasFunctionName : 1;
366 /// The location of this PredefinedExpr.
370 class DeclRefExprBitfields {
371 friend class ASTStmtReader; // deserialization
372 friend class DeclRefExpr;
374 unsigned : NumExprBits;
376 unsigned HasQualifier : 1;
377 unsigned HasTemplateKWAndArgsInfo : 1;
378 unsigned HasFoundDecl : 1;
379 unsigned HadMultipleCandidates : 1;
380 unsigned RefersToEnclosingVariableOrCapture : 1;
381 unsigned NonOdrUseReason : 2;
383 /// The location of the declaration name itself.
388 class FloatingLiteralBitfields {
389 friend class FloatingLiteral;
391 unsigned : NumExprBits;
393 unsigned Semantics : 3; // Provides semantics for APFloat construction
394 unsigned IsExact : 1;
397 class StringLiteralBitfields {
398 friend class ASTStmtReader;
399 friend class StringLiteral;
401 unsigned : NumExprBits;
403 /// The kind of this string literal.
404 /// One of the enumeration values of StringLiteral::StringKind.
407 /// The width of a single character in bytes. Only values of 1, 2,
408 /// and 4 bytes are supported. StringLiteral::mapCharByteWidth maps
409 /// the target + string kind to the appropriate CharByteWidth.
410 unsigned CharByteWidth : 3;
412 unsigned IsPascal : 1;
414 /// The number of concatenated token this string is made of.
415 /// This is the number of trailing SourceLocation.
416 unsigned NumConcatenated;
419 class CharacterLiteralBitfields {
420 friend class CharacterLiteral;
422 unsigned : NumExprBits;
427 class UnaryOperatorBitfields {
428 friend class UnaryOperator;
430 unsigned : NumExprBits;
433 unsigned CanOverflow : 1;
438 class UnaryExprOrTypeTraitExprBitfields {
439 friend class UnaryExprOrTypeTraitExpr;
441 unsigned : NumExprBits;
444 unsigned IsType : 1; // true if operand is a type, false if an expression.
447 class ArraySubscriptExprBitfields {
448 friend class ArraySubscriptExpr;
450 unsigned : NumExprBits;
452 SourceLocation RBracketLoc;
455 class CallExprBitfields {
456 friend class CallExpr;
458 unsigned : NumExprBits;
460 unsigned NumPreArgs : 1;
462 /// True if the callee of the call expression was found using ADL.
463 unsigned UsesADL : 1;
465 /// Padding used to align OffsetToTrailingObjects to a byte multiple.
466 unsigned : 24 - 2 - NumExprBits;
468 /// The offset in bytes from the this pointer to the start of the
469 /// trailing objects belonging to CallExpr. Intentionally byte sized
470 /// for faster access.
471 unsigned OffsetToTrailingObjects : 8;
473 enum { NumCallExprBits = 32 };
475 class MemberExprBitfields {
476 friend class ASTStmtReader;
477 friend class MemberExpr;
479 unsigned : NumExprBits;
481 /// IsArrow - True if this is "X->F", false if this is "X.F".
482 unsigned IsArrow : 1;
484 /// True if this member expression used a nested-name-specifier to
485 /// refer to the member, e.g., "x->Base::f", or found its member via
486 /// a using declaration. When true, a MemberExprNameQualifier
487 /// structure is allocated immediately after the MemberExpr.
488 unsigned HasQualifierOrFoundDecl : 1;
490 /// True if this member expression specified a template keyword
491 /// and/or a template argument list explicitly, e.g., x->f<int>,
492 /// x->template f, x->template f<int>.
493 /// When true, an ASTTemplateKWAndArgsInfo structure and its
494 /// TemplateArguments (if any) are present.
495 unsigned HasTemplateKWAndArgsInfo : 1;
497 /// True if this member expression refers to a method that
498 /// was resolved from an overloaded set having size greater than 1.
499 unsigned HadMultipleCandidates : 1;
501 /// Value of type NonOdrUseReason indicating why this MemberExpr does
502 /// not constitute an odr-use of the named declaration. Meaningful only
503 /// when naming a static member.
504 unsigned NonOdrUseReason : 2;
506 /// This is the location of the -> or . in the expression.
507 SourceLocation OperatorLoc;
510 class CastExprBitfields {
511 friend class CastExpr;
512 friend class ImplicitCastExpr;
514 unsigned : NumExprBits;
517 unsigned PartOfExplicitCast : 1; // Only set for ImplicitCastExpr.
519 /// The number of CXXBaseSpecifiers in the cast. 14 bits would be enough
520 /// here. ([implimits] Direct and indirect base classes [16384]).
521 unsigned BasePathSize;
524 class BinaryOperatorBitfields {
525 friend class BinaryOperator;
527 unsigned : NumExprBits;
531 /// This is only meaningful for operations on floating point
532 /// types and 0 otherwise.
533 unsigned FPFeatures : 3;
535 SourceLocation OpLoc;
538 class InitListExprBitfields {
539 friend class InitListExpr;
541 unsigned : NumExprBits;
543 /// Whether this initializer list originally had a GNU array-range
544 /// designator in it. This is a temporary marker used by CodeGen.
545 unsigned HadArrayRangeDesignator : 1;
548 class ParenListExprBitfields {
549 friend class ASTStmtReader;
550 friend class ParenListExpr;
552 unsigned : NumExprBits;
554 /// The number of expressions in the paren list.
558 class GenericSelectionExprBitfields {
559 friend class ASTStmtReader;
560 friend class GenericSelectionExpr;
562 unsigned : NumExprBits;
564 /// The location of the "_Generic".
565 SourceLocation GenericLoc;
568 class PseudoObjectExprBitfields {
569 friend class ASTStmtReader; // deserialization
570 friend class PseudoObjectExpr;
572 unsigned : NumExprBits;
574 // These don't need to be particularly wide, because they're
575 // strictly limited by the forms of expressions we permit.
576 unsigned NumSubExprs : 8;
577 unsigned ResultIndex : 32 - 8 - NumExprBits;
580 class SourceLocExprBitfields {
581 friend class ASTStmtReader;
582 friend class SourceLocExpr;
584 unsigned : NumExprBits;
586 /// The kind of source location builtin represented by the SourceLocExpr.
587 /// Ex. __builtin_LINE, __builtin_FUNCTION, ect.
591 class StmtExprBitfields {
592 friend class ASTStmtReader;
593 friend class StmtExpr;
595 unsigned : NumExprBits;
597 /// The number of levels of template parameters enclosing this statement
598 /// expression. Used to determine if a statement expression remains
599 /// dependent after instantiation.
600 unsigned TemplateDepth;
603 //===--- C++ Expression bitfields classes ---===//
605 class CXXOperatorCallExprBitfields {
606 friend class ASTStmtReader;
607 friend class CXXOperatorCallExpr;
609 unsigned : NumCallExprBits;
611 /// The kind of this overloaded operator. One of the enumerator
612 /// value of OverloadedOperatorKind.
613 unsigned OperatorKind : 6;
615 // Only meaningful for floating point types.
616 unsigned FPFeatures : 3;
619 class CXXRewrittenBinaryOperatorBitfields {
620 friend class ASTStmtReader;
621 friend class CXXRewrittenBinaryOperator;
623 unsigned : NumCallExprBits;
625 unsigned IsReversed : 1;
628 class CXXBoolLiteralExprBitfields {
629 friend class CXXBoolLiteralExpr;
631 unsigned : NumExprBits;
633 /// The value of the boolean literal.
636 /// The location of the boolean literal.
640 class CXXNullPtrLiteralExprBitfields {
641 friend class CXXNullPtrLiteralExpr;
643 unsigned : NumExprBits;
645 /// The location of the null pointer literal.
649 class CXXThisExprBitfields {
650 friend class CXXThisExpr;
652 unsigned : NumExprBits;
654 /// Whether this is an implicit "this".
655 unsigned IsImplicit : 1;
657 /// The location of the "this".
661 class CXXThrowExprBitfields {
662 friend class ASTStmtReader;
663 friend class CXXThrowExpr;
665 unsigned : NumExprBits;
667 /// Whether the thrown variable (if any) is in scope.
668 unsigned IsThrownVariableInScope : 1;
670 /// The location of the "throw".
671 SourceLocation ThrowLoc;
674 class CXXDefaultArgExprBitfields {
675 friend class ASTStmtReader;
676 friend class CXXDefaultArgExpr;
678 unsigned : NumExprBits;
680 /// The location where the default argument expression was used.
684 class CXXDefaultInitExprBitfields {
685 friend class ASTStmtReader;
686 friend class CXXDefaultInitExpr;
688 unsigned : NumExprBits;
690 /// The location where the default initializer expression was used.
694 class CXXScalarValueInitExprBitfields {
695 friend class ASTStmtReader;
696 friend class CXXScalarValueInitExpr;
698 unsigned : NumExprBits;
700 SourceLocation RParenLoc;
703 class CXXNewExprBitfields {
704 friend class ASTStmtReader;
705 friend class ASTStmtWriter;
706 friend class CXXNewExpr;
708 unsigned : NumExprBits;
710 /// Was the usage ::new, i.e. is the global new to be used?
711 unsigned IsGlobalNew : 1;
713 /// Do we allocate an array? If so, the first trailing "Stmt *" is the
715 unsigned IsArray : 1;
717 /// Should the alignment be passed to the allocation function?
718 unsigned ShouldPassAlignment : 1;
720 /// If this is an array allocation, does the usual deallocation
721 /// function for the allocated type want to know the allocated size?
722 unsigned UsualArrayDeleteWantsSize : 1;
724 /// What kind of initializer do we have? Could be none, parens, or braces.
725 /// In storage, we distinguish between "none, and no initializer expr", and
726 /// "none, but an implicit initializer expr".
727 unsigned StoredInitializationStyle : 2;
729 /// True if the allocated type was expressed as a parenthesized type-id.
730 unsigned IsParenTypeId : 1;
732 /// The number of placement new arguments.
733 unsigned NumPlacementArgs;
736 class CXXDeleteExprBitfields {
737 friend class ASTStmtReader;
738 friend class CXXDeleteExpr;
740 unsigned : NumExprBits;
742 /// Is this a forced global delete, i.e. "::delete"?
743 unsigned GlobalDelete : 1;
745 /// Is this the array form of delete, i.e. "delete[]"?
746 unsigned ArrayForm : 1;
748 /// ArrayFormAsWritten can be different from ArrayForm if 'delete' is
749 /// applied to pointer-to-array type (ArrayFormAsWritten will be false
750 /// while ArrayForm will be true).
751 unsigned ArrayFormAsWritten : 1;
753 /// Does the usual deallocation function for the element type require
754 /// a size_t argument?
755 unsigned UsualArrayDeleteWantsSize : 1;
757 /// Location of the expression.
761 class TypeTraitExprBitfields {
762 friend class ASTStmtReader;
763 friend class ASTStmtWriter;
764 friend class TypeTraitExpr;
766 unsigned : NumExprBits;
768 /// The kind of type trait, which is a value of a TypeTrait enumerator.
771 /// If this expression is not value-dependent, this indicates whether
772 /// the trait evaluated true or false.
775 /// The number of arguments to this type trait.
776 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
779 class DependentScopeDeclRefExprBitfields {
780 friend class ASTStmtReader;
781 friend class ASTStmtWriter;
782 friend class DependentScopeDeclRefExpr;
784 unsigned : NumExprBits;
786 /// Whether the name includes info for explicit template
787 /// keyword and arguments.
788 unsigned HasTemplateKWAndArgsInfo : 1;
791 class CXXConstructExprBitfields {
792 friend class ASTStmtReader;
793 friend class CXXConstructExpr;
795 unsigned : NumExprBits;
797 unsigned Elidable : 1;
798 unsigned HadMultipleCandidates : 1;
799 unsigned ListInitialization : 1;
800 unsigned StdInitListInitialization : 1;
801 unsigned ZeroInitialization : 1;
802 unsigned ConstructionKind : 3;
807 class ExprWithCleanupsBitfields {
808 friend class ASTStmtReader; // deserialization
809 friend class ExprWithCleanups;
811 unsigned : NumExprBits;
813 // When false, it must not have side effects.
814 unsigned CleanupsHaveSideEffects : 1;
816 unsigned NumObjects : 32 - 1 - NumExprBits;
819 class CXXUnresolvedConstructExprBitfields {
820 friend class ASTStmtReader;
821 friend class CXXUnresolvedConstructExpr;
823 unsigned : NumExprBits;
825 /// The number of arguments used to construct the type.
829 class CXXDependentScopeMemberExprBitfields {
830 friend class ASTStmtReader;
831 friend class CXXDependentScopeMemberExpr;
833 unsigned : NumExprBits;
835 /// Whether this member expression used the '->' operator or
836 /// the '.' operator.
837 unsigned IsArrow : 1;
839 /// Whether this member expression has info for explicit template
840 /// keyword and arguments.
841 unsigned HasTemplateKWAndArgsInfo : 1;
843 /// See getFirstQualifierFoundInScope() and the comment listing
844 /// the trailing objects.
845 unsigned HasFirstQualifierFoundInScope : 1;
847 /// The location of the '->' or '.' operator.
848 SourceLocation OperatorLoc;
851 class OverloadExprBitfields {
852 friend class ASTStmtReader;
853 friend class OverloadExpr;
855 unsigned : NumExprBits;
857 /// Whether the name includes info for explicit template
858 /// keyword and arguments.
859 unsigned HasTemplateKWAndArgsInfo : 1;
861 /// Padding used by the derived classes to store various bits. If you
862 /// need to add some data here, shrink this padding and add your data
863 /// above. NumOverloadExprBits also needs to be updated.
864 unsigned : 32 - NumExprBits - 1;
866 /// The number of results.
869 enum { NumOverloadExprBits = NumExprBits + 1 };
871 class UnresolvedLookupExprBitfields {
872 friend class ASTStmtReader;
873 friend class UnresolvedLookupExpr;
875 unsigned : NumOverloadExprBits;
877 /// True if these lookup results should be extended by
878 /// argument-dependent lookup if this is the operand of a function call.
879 unsigned RequiresADL : 1;
881 /// True if these lookup results are overloaded. This is pretty trivially
882 /// rederivable if we urgently need to kill this field.
883 unsigned Overloaded : 1;
885 static_assert(sizeof(UnresolvedLookupExprBitfields) <= 4,
886 "UnresolvedLookupExprBitfields must be <= than 4 bytes to"
887 "avoid trashing OverloadExprBitfields::NumResults!");
889 class UnresolvedMemberExprBitfields {
890 friend class ASTStmtReader;
891 friend class UnresolvedMemberExpr;
893 unsigned : NumOverloadExprBits;
895 /// Whether this member expression used the '->' operator or
896 /// the '.' operator.
897 unsigned IsArrow : 1;
899 /// Whether the lookup results contain an unresolved using declaration.
900 unsigned HasUnresolvedUsing : 1;
902 static_assert(sizeof(UnresolvedMemberExprBitfields) <= 4,
903 "UnresolvedMemberExprBitfields must be <= than 4 bytes to"
904 "avoid trashing OverloadExprBitfields::NumResults!");
906 class CXXNoexceptExprBitfields {
907 friend class ASTStmtReader;
908 friend class CXXNoexceptExpr;
910 unsigned : NumExprBits;
915 class SubstNonTypeTemplateParmExprBitfields {
916 friend class ASTStmtReader;
917 friend class SubstNonTypeTemplateParmExpr;
919 unsigned : NumExprBits;
921 /// The location of the non-type template parameter reference.
922 SourceLocation NameLoc;
925 class RequiresExprBitfields {
926 friend class ASTStmtReader;
927 friend class ASTStmtWriter;
928 friend class RequiresExpr;
930 unsigned : NumExprBits;
932 unsigned IsSatisfied : 1;
933 SourceLocation RequiresKWLoc;
936 //===--- C++ Coroutines TS bitfields classes ---===//
938 class CoawaitExprBitfields {
939 friend class CoawaitExpr;
941 unsigned : NumExprBits;
943 unsigned IsImplicit : 1;
946 //===--- Obj-C Expression bitfields classes ---===//
948 class ObjCIndirectCopyRestoreExprBitfields {
949 friend class ObjCIndirectCopyRestoreExpr;
951 unsigned : NumExprBits;
953 unsigned ShouldCopy : 1;
956 //===--- Clang Extensions bitfields classes ---===//
958 class OpaqueValueExprBitfields {
959 friend class ASTStmtReader;
960 friend class OpaqueValueExpr;
962 unsigned : NumExprBits;
964 /// The OVE is a unique semantic reference to its source expression if this
965 /// bit is set to true.
966 unsigned IsUnique : 1;
972 // Same order as in StmtNodes.td.
974 StmtBitfields StmtBits;
975 NullStmtBitfields NullStmtBits;
976 CompoundStmtBitfields CompoundStmtBits;
977 LabelStmtBitfields LabelStmtBits;
978 AttributedStmtBitfields AttributedStmtBits;
979 IfStmtBitfields IfStmtBits;
980 SwitchStmtBitfields SwitchStmtBits;
981 WhileStmtBitfields WhileStmtBits;
982 DoStmtBitfields DoStmtBits;
983 ForStmtBitfields ForStmtBits;
984 GotoStmtBitfields GotoStmtBits;
985 ContinueStmtBitfields ContinueStmtBits;
986 BreakStmtBitfields BreakStmtBits;
987 ReturnStmtBitfields ReturnStmtBits;
988 SwitchCaseBitfields SwitchCaseBits;
991 ExprBitfields ExprBits;
992 ConstantExprBitfields ConstantExprBits;
993 PredefinedExprBitfields PredefinedExprBits;
994 DeclRefExprBitfields DeclRefExprBits;
995 FloatingLiteralBitfields FloatingLiteralBits;
996 StringLiteralBitfields StringLiteralBits;
997 CharacterLiteralBitfields CharacterLiteralBits;
998 UnaryOperatorBitfields UnaryOperatorBits;
999 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
1000 ArraySubscriptExprBitfields ArraySubscriptExprBits;
1001 CallExprBitfields CallExprBits;
1002 MemberExprBitfields MemberExprBits;
1003 CastExprBitfields CastExprBits;
1004 BinaryOperatorBitfields BinaryOperatorBits;
1005 InitListExprBitfields InitListExprBits;
1006 ParenListExprBitfields ParenListExprBits;
1007 GenericSelectionExprBitfields GenericSelectionExprBits;
1008 PseudoObjectExprBitfields PseudoObjectExprBits;
1009 SourceLocExprBitfields SourceLocExprBits;
1012 StmtExprBitfields StmtExprBits;
1015 CXXOperatorCallExprBitfields CXXOperatorCallExprBits;
1016 CXXRewrittenBinaryOperatorBitfields CXXRewrittenBinaryOperatorBits;
1017 CXXBoolLiteralExprBitfields CXXBoolLiteralExprBits;
1018 CXXNullPtrLiteralExprBitfields CXXNullPtrLiteralExprBits;
1019 CXXThisExprBitfields CXXThisExprBits;
1020 CXXThrowExprBitfields CXXThrowExprBits;
1021 CXXDefaultArgExprBitfields CXXDefaultArgExprBits;
1022 CXXDefaultInitExprBitfields CXXDefaultInitExprBits;
1023 CXXScalarValueInitExprBitfields CXXScalarValueInitExprBits;
1024 CXXNewExprBitfields CXXNewExprBits;
1025 CXXDeleteExprBitfields CXXDeleteExprBits;
1026 TypeTraitExprBitfields TypeTraitExprBits;
1027 DependentScopeDeclRefExprBitfields DependentScopeDeclRefExprBits;
1028 CXXConstructExprBitfields CXXConstructExprBits;
1029 ExprWithCleanupsBitfields ExprWithCleanupsBits;
1030 CXXUnresolvedConstructExprBitfields CXXUnresolvedConstructExprBits;
1031 CXXDependentScopeMemberExprBitfields CXXDependentScopeMemberExprBits;
1032 OverloadExprBitfields OverloadExprBits;
1033 UnresolvedLookupExprBitfields UnresolvedLookupExprBits;
1034 UnresolvedMemberExprBitfields UnresolvedMemberExprBits;
1035 CXXNoexceptExprBitfields CXXNoexceptExprBits;
1036 SubstNonTypeTemplateParmExprBitfields SubstNonTypeTemplateParmExprBits;
1037 RequiresExprBitfields RequiresExprBits;
1039 // C++ Coroutines TS expressions
1040 CoawaitExprBitfields CoawaitBits;
1042 // Obj-C Expressions
1043 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
1046 OpaqueValueExprBitfields OpaqueValueExprBits;
1050 // Only allow allocation of Stmts using the allocator in ASTContext
1051 // or by doing a placement new.
1052 void* operator new(size_t bytes, const ASTContext& C,
1053 unsigned alignment = 8);
1055 void* operator new(size_t bytes, const ASTContext* C,
1056 unsigned alignment = 8) {
1057 return operator new(bytes, *C, alignment);
1060 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
1062 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
1063 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
1064 void operator delete(void *, size_t) noexcept {}
1065 void operator delete(void *, void *) noexcept {}
1068 /// A placeholder type used to construct an empty shell of a
1069 /// type, that will be filled in later (e.g., by some
1070 /// de-serialization).
1071 struct EmptyShell {};
1074 /// Iterator for iterating over Stmt * arrays that contain only T *.
1076 /// This is needed because AST nodes use Stmt* arrays to store
1077 /// references to children (to be compatible with StmtIterator).
1078 template<typename T, typename TPtr = T *, typename StmtPtr = Stmt *>
1080 : llvm::iterator_adaptor_base<CastIterator<T, TPtr, StmtPtr>, StmtPtr *,
1081 std::random_access_iterator_tag, TPtr> {
1082 using Base = typename CastIterator::iterator_adaptor_base;
1084 CastIterator() : Base(nullptr) {}
1085 CastIterator(StmtPtr *I) : Base(I) {}
1087 typename Base::value_type operator*() const {
1088 return cast_or_null<T>(*this->I);
1092 /// Const iterator for iterating over Stmt * arrays that contain only T *.
1093 template <typename T>
1094 using ConstCastIterator = CastIterator<T, const T *const, const Stmt *const>;
1096 using ExprIterator = CastIterator<Expr>;
1097 using ConstExprIterator = ConstCastIterator<Expr>;
1100 /// Whether statistic collection is enabled.
1101 static bool StatisticsEnabled;
1104 /// Construct an empty statement.
1105 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
1109 Stmt(const Stmt &) = delete;
1110 Stmt(Stmt &&) = delete;
1111 Stmt &operator=(const Stmt &) = delete;
1112 Stmt &operator=(Stmt &&) = delete;
1114 Stmt(StmtClass SC) {
1115 static_assert(sizeof(*this) <= 8,
1116 "changing bitfields changed sizeof(Stmt)");
1117 static_assert(sizeof(*this) % alignof(void *) == 0,
1118 "Insufficient alignment!");
1119 StmtBits.sClass = SC;
1120 StmtBits.IsOMPStructuredBlock = false;
1121 if (StatisticsEnabled) Stmt::addStmtClass(SC);
1124 StmtClass getStmtClass() const {
1125 return static_cast<StmtClass>(StmtBits.sClass);
1128 const char *getStmtClassName() const;
1130 bool isOMPStructuredBlock() const { return StmtBits.IsOMPStructuredBlock; }
1131 void setIsOMPStructuredBlock(bool IsOMPStructuredBlock) {
1132 StmtBits.IsOMPStructuredBlock = IsOMPStructuredBlock;
1135 /// SourceLocation tokens are not useful in isolation - they are low level
1136 /// value objects created/interpreted by SourceManager. We assume AST
1137 /// clients will have a pointer to the respective SourceManager.
1138 SourceRange getSourceRange() const LLVM_READONLY;
1139 SourceLocation getBeginLoc() const LLVM_READONLY;
1140 SourceLocation getEndLoc() const LLVM_READONLY;
1142 // global temp stats (until we have a per-module visitor)
1143 static void addStmtClass(const StmtClass s);
1144 static void EnableStatistics();
1145 static void PrintStats();
1147 /// Dumps the specified AST fragment and all subtrees to
1148 /// \c llvm::errs().
1150 void dump(SourceManager &SM) const;
1151 void dump(raw_ostream &OS, SourceManager &SM) const;
1152 void dump(raw_ostream &OS) const;
1154 /// \return Unique reproducible object identifier
1155 int64_t getID(const ASTContext &Context) const;
1157 /// dumpColor - same as dump(), but forces color highlighting.
1158 void dumpColor() const;
1160 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
1161 /// back to its original source language syntax.
1162 void dumpPretty(const ASTContext &Context) const;
1163 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
1164 const PrintingPolicy &Policy, unsigned Indentation = 0,
1165 StringRef NewlineSymbol = "\n",
1166 const ASTContext *Context = nullptr) const;
1168 /// Pretty-prints in JSON format.
1169 void printJson(raw_ostream &Out, PrinterHelper *Helper,
1170 const PrintingPolicy &Policy, bool AddQuotes) const;
1172 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
1173 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
1174 void viewAST() const;
1176 /// Skip no-op (attributed, compound) container stmts and skip captured
1177 /// stmt at the top, if \a IgnoreCaptured is true.
1178 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
1179 const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
1180 return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
1183 const Stmt *stripLabelLikeStatements() const;
1184 Stmt *stripLabelLikeStatements() {
1185 return const_cast<Stmt*>(
1186 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
1189 /// Child Iterators: All subclasses must implement 'children'
1190 /// to permit easy iteration over the substatements/subexpessions of an
1191 /// AST node. This permits easy iteration over all nodes in the AST.
1192 using child_iterator = StmtIterator;
1193 using const_child_iterator = ConstStmtIterator;
1195 using child_range = llvm::iterator_range<child_iterator>;
1196 using const_child_range = llvm::iterator_range<const_child_iterator>;
1198 child_range children();
1200 const_child_range children() const {
1201 auto Children = const_cast<Stmt *>(this)->children();
1202 return const_child_range(Children.begin(), Children.end());
1205 child_iterator child_begin() { return children().begin(); }
1206 child_iterator child_end() { return children().end(); }
1208 const_child_iterator child_begin() const { return children().begin(); }
1209 const_child_iterator child_end() const { return children().end(); }
1211 /// Produce a unique representation of the given statement.
1213 /// \param ID once the profiling operation is complete, will contain
1214 /// the unique representation of the given statement.
1216 /// \param Context the AST context in which the statement resides
1218 /// \param Canonical whether the profile should be based on the canonical
1219 /// representation of this statement (e.g., where non-type template
1220 /// parameters are identified by index/level rather than their
1221 /// declaration pointers) or the exact representation of the statement as
1222 /// written in the source.
1223 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
1224 bool Canonical) const;
1226 /// Calculate a unique representation for a statement that is
1227 /// stable across compiler invocations.
1229 /// \param ID profile information will be stored in ID.
1231 /// \param Hash an ODRHash object which will be called where pointers would
1232 /// have been used in the Profile function.
1233 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
1236 /// DeclStmt - Adaptor class for mixing declarations with statements and
1237 /// expressions. For example, CompoundStmt mixes statements, expressions
1238 /// and declarations (variables, types). Another example is ForStmt, where
1239 /// the first statement can be an expression or a declaration.
1240 class DeclStmt : public Stmt {
1242 SourceLocation StartLoc, EndLoc;
1245 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
1246 : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
1248 /// Build an empty declaration statement.
1249 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
1251 /// isSingleDecl - This method returns true if this DeclStmt refers
1252 /// to a single Decl.
1253 bool isSingleDecl() const { return DG.isSingleDecl(); }
1255 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
1256 Decl *getSingleDecl() { return DG.getSingleDecl(); }
1258 const DeclGroupRef getDeclGroup() const { return DG; }
1259 DeclGroupRef getDeclGroup() { return DG; }
1260 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
1262 void setStartLoc(SourceLocation L) { StartLoc = L; }
1263 SourceLocation getEndLoc() const { return EndLoc; }
1264 void setEndLoc(SourceLocation L) { EndLoc = L; }
1266 SourceLocation getBeginLoc() const LLVM_READONLY { return StartLoc; }
1268 static bool classof(const Stmt *T) {
1269 return T->getStmtClass() == DeclStmtClass;
1272 // Iterators over subexpressions.
1273 child_range children() {
1274 return child_range(child_iterator(DG.begin(), DG.end()),
1275 child_iterator(DG.end(), DG.end()));
1278 const_child_range children() const {
1279 auto Children = const_cast<DeclStmt *>(this)->children();
1280 return const_child_range(Children);
1283 using decl_iterator = DeclGroupRef::iterator;
1284 using const_decl_iterator = DeclGroupRef::const_iterator;
1285 using decl_range = llvm::iterator_range<decl_iterator>;
1286 using decl_const_range = llvm::iterator_range<const_decl_iterator>;
1288 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
1290 decl_const_range decls() const {
1291 return decl_const_range(decl_begin(), decl_end());
1294 decl_iterator decl_begin() { return DG.begin(); }
1295 decl_iterator decl_end() { return DG.end(); }
1296 const_decl_iterator decl_begin() const { return DG.begin(); }
1297 const_decl_iterator decl_end() const { return DG.end(); }
1299 using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
1301 reverse_decl_iterator decl_rbegin() {
1302 return reverse_decl_iterator(decl_end());
1305 reverse_decl_iterator decl_rend() {
1306 return reverse_decl_iterator(decl_begin());
1310 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
1312 class NullStmt : public Stmt {
1314 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
1315 : Stmt(NullStmtClass) {
1316 NullStmtBits.HasLeadingEmptyMacro = hasLeadingEmptyMacro;
1320 /// Build an empty null statement.
1321 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
1323 SourceLocation getSemiLoc() const { return NullStmtBits.SemiLoc; }
1324 void setSemiLoc(SourceLocation L) { NullStmtBits.SemiLoc = L; }
1326 bool hasLeadingEmptyMacro() const {
1327 return NullStmtBits.HasLeadingEmptyMacro;
1330 SourceLocation getBeginLoc() const { return getSemiLoc(); }
1331 SourceLocation getEndLoc() const { return getSemiLoc(); }
1333 static bool classof(const Stmt *T) {
1334 return T->getStmtClass() == NullStmtClass;
1337 child_range children() {
1338 return child_range(child_iterator(), child_iterator());
1341 const_child_range children() const {
1342 return const_child_range(const_child_iterator(), const_child_iterator());
1346 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
1347 class CompoundStmt final : public Stmt,
1348 private llvm::TrailingObjects<CompoundStmt, Stmt *> {
1349 friend class ASTStmtReader;
1350 friend TrailingObjects;
1352 /// The location of the closing "}". LBraceLoc is stored in CompoundStmtBits.
1353 SourceLocation RBraceLoc;
1355 CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
1356 explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
1358 void setStmts(ArrayRef<Stmt *> Stmts);
1361 static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
1362 SourceLocation LB, SourceLocation RB);
1364 // Build an empty compound statement with a location.
1365 explicit CompoundStmt(SourceLocation Loc)
1366 : Stmt(CompoundStmtClass), RBraceLoc(Loc) {
1367 CompoundStmtBits.NumStmts = 0;
1368 CompoundStmtBits.LBraceLoc = Loc;
1371 // Build an empty compound statement.
1372 static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
1374 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
1375 unsigned size() const { return CompoundStmtBits.NumStmts; }
1377 using body_iterator = Stmt **;
1378 using body_range = llvm::iterator_range<body_iterator>;
1380 body_range body() { return body_range(body_begin(), body_end()); }
1381 body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
1382 body_iterator body_end() { return body_begin() + size(); }
1383 Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
1386 return !body_empty() ? body_begin()[size() - 1] : nullptr;
1389 using const_body_iterator = Stmt *const *;
1390 using body_const_range = llvm::iterator_range<const_body_iterator>;
1392 body_const_range body() const {
1393 return body_const_range(body_begin(), body_end());
1396 const_body_iterator body_begin() const {
1397 return getTrailingObjects<Stmt *>();
1400 const_body_iterator body_end() const { return body_begin() + size(); }
1402 const Stmt *body_front() const {
1403 return !body_empty() ? body_begin()[0] : nullptr;
1406 const Stmt *body_back() const {
1407 return !body_empty() ? body_begin()[size() - 1] : nullptr;
1410 using reverse_body_iterator = std::reverse_iterator<body_iterator>;
1412 reverse_body_iterator body_rbegin() {
1413 return reverse_body_iterator(body_end());
1416 reverse_body_iterator body_rend() {
1417 return reverse_body_iterator(body_begin());
1420 using const_reverse_body_iterator =
1421 std::reverse_iterator<const_body_iterator>;
1423 const_reverse_body_iterator body_rbegin() const {
1424 return const_reverse_body_iterator(body_end());
1427 const_reverse_body_iterator body_rend() const {
1428 return const_reverse_body_iterator(body_begin());
1431 // Get the Stmt that StmtExpr would consider to be the result of this
1432 // compound statement. This is used by StmtExpr to properly emulate the GCC
1433 // compound expression extension, which ignores trailing NullStmts when
1434 // getting the result of the expression.
1437 // If we don't find something that isn't a NullStmt, just return the last
1439 Stmt *getStmtExprResult() {
1440 for (auto *B : llvm::reverse(body())) {
1441 if (!isa<NullStmt>(B))
1447 const Stmt *getStmtExprResult() const {
1448 return const_cast<CompoundStmt *>(this)->getStmtExprResult();
1451 SourceLocation getBeginLoc() const { return CompoundStmtBits.LBraceLoc; }
1452 SourceLocation getEndLoc() const { return RBraceLoc; }
1454 SourceLocation getLBracLoc() const { return CompoundStmtBits.LBraceLoc; }
1455 SourceLocation getRBracLoc() const { return RBraceLoc; }
1457 static bool classof(const Stmt *T) {
1458 return T->getStmtClass() == CompoundStmtClass;
1462 child_range children() { return child_range(body_begin(), body_end()); }
1464 const_child_range children() const {
1465 return const_child_range(body_begin(), body_end());
1469 // SwitchCase is the base class for CaseStmt and DefaultStmt,
1470 class SwitchCase : public Stmt {
1472 /// The location of the ":".
1473 SourceLocation ColonLoc;
1475 // The location of the "case" or "default" keyword. Stored in SwitchCaseBits.
1476 // SourceLocation KeywordLoc;
1478 /// A pointer to the following CaseStmt or DefaultStmt class,
1479 /// used by SwitchStmt.
1480 SwitchCase *NextSwitchCase = nullptr;
1482 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
1483 : Stmt(SC), ColonLoc(ColonLoc) {
1484 setKeywordLoc(KWLoc);
1487 SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
1490 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
1491 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
1492 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
1494 SourceLocation getKeywordLoc() const { return SwitchCaseBits.KeywordLoc; }
1495 void setKeywordLoc(SourceLocation L) { SwitchCaseBits.KeywordLoc = L; }
1496 SourceLocation getColonLoc() const { return ColonLoc; }
1497 void setColonLoc(SourceLocation L) { ColonLoc = L; }
1499 inline Stmt *getSubStmt();
1500 const Stmt *getSubStmt() const {
1501 return const_cast<SwitchCase *>(this)->getSubStmt();
1504 SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1505 inline SourceLocation getEndLoc() const LLVM_READONLY;
1507 static bool classof(const Stmt *T) {
1508 return T->getStmtClass() == CaseStmtClass ||
1509 T->getStmtClass() == DefaultStmtClass;
1513 /// CaseStmt - Represent a case statement. It can optionally be a GNU case
1514 /// statement of the form LHS ... RHS representing a range of cases.
1515 class CaseStmt final
1516 : public SwitchCase,
1517 private llvm::TrailingObjects<CaseStmt, Stmt *, SourceLocation> {
1518 friend TrailingObjects;
1520 // CaseStmt is followed by several trailing objects, some of which optional.
1521 // Note that it would be more convenient to put the optional trailing objects
1522 // at the end but this would impact children().
1523 // The trailing objects are in order:
1525 // * A "Stmt *" for the LHS of the case statement. Always present.
1527 // * A "Stmt *" for the RHS of the case statement. This is a GNU extension
1528 // which allow ranges in cases statement of the form LHS ... RHS.
1529 // Present if and only if caseStmtIsGNURange() is true.
1531 // * A "Stmt *" for the substatement of the case statement. Always present.
1533 // * A SourceLocation for the location of the ... if this is a case statement
1534 // with a range. Present if and only if caseStmtIsGNURange() is true.
1535 enum { LhsOffset = 0, SubStmtOffsetFromRhs = 1 };
1536 enum { NumMandatoryStmtPtr = 2 };
1538 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1539 return NumMandatoryStmtPtr + caseStmtIsGNURange();
1542 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
1543 return caseStmtIsGNURange();
1546 unsigned lhsOffset() const { return LhsOffset; }
1547 unsigned rhsOffset() const { return LhsOffset + caseStmtIsGNURange(); }
1548 unsigned subStmtOffset() const { return rhsOffset() + SubStmtOffsetFromRhs; }
1550 /// Build a case statement assuming that the storage for the
1551 /// trailing objects has been properly allocated.
1552 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
1553 SourceLocation ellipsisLoc, SourceLocation colonLoc)
1554 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
1555 // Handle GNU case statements of the form LHS ... RHS.
1556 bool IsGNURange = rhs != nullptr;
1557 SwitchCaseBits.CaseStmtIsGNURange = IsGNURange;
1559 setSubStmt(nullptr);
1562 setEllipsisLoc(ellipsisLoc);
1566 /// Build an empty switch case statement.
1567 explicit CaseStmt(EmptyShell Empty, bool CaseStmtIsGNURange)
1568 : SwitchCase(CaseStmtClass, Empty) {
1569 SwitchCaseBits.CaseStmtIsGNURange = CaseStmtIsGNURange;
1573 /// Build a case statement.
1574 static CaseStmt *Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1575 SourceLocation caseLoc, SourceLocation ellipsisLoc,
1576 SourceLocation colonLoc);
1578 /// Build an empty case statement.
1579 static CaseStmt *CreateEmpty(const ASTContext &Ctx, bool CaseStmtIsGNURange);
1581 /// True if this case statement is of the form case LHS ... RHS, which
1582 /// is a GNU extension. In this case the RHS can be obtained with getRHS()
1583 /// and the location of the ellipsis can be obtained with getEllipsisLoc().
1584 bool caseStmtIsGNURange() const { return SwitchCaseBits.CaseStmtIsGNURange; }
1586 SourceLocation getCaseLoc() const { return getKeywordLoc(); }
1587 void setCaseLoc(SourceLocation L) { setKeywordLoc(L); }
1589 /// Get the location of the ... in a case statement of the form LHS ... RHS.
1590 SourceLocation getEllipsisLoc() const {
1591 return caseStmtIsGNURange() ? *getTrailingObjects<SourceLocation>()
1595 /// Set the location of the ... in a case statement of the form LHS ... RHS.
1596 /// Assert that this case statement is of this form.
1597 void setEllipsisLoc(SourceLocation L) {
1599 caseStmtIsGNURange() &&
1600 "setEllipsisLoc but this is not a case stmt of the form LHS ... RHS!");
1601 *getTrailingObjects<SourceLocation>() = L;
1605 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
1608 const Expr *getLHS() const {
1609 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
1612 void setLHS(Expr *Val) {
1613 getTrailingObjects<Stmt *>()[lhsOffset()] = reinterpret_cast<Stmt *>(Val);
1617 return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
1618 getTrailingObjects<Stmt *>()[rhsOffset()])
1622 const Expr *getRHS() const {
1623 return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
1624 getTrailingObjects<Stmt *>()[rhsOffset()])
1628 void setRHS(Expr *Val) {
1629 assert(caseStmtIsGNURange() &&
1630 "setRHS but this is not a case stmt of the form LHS ... RHS!");
1631 getTrailingObjects<Stmt *>()[rhsOffset()] = reinterpret_cast<Stmt *>(Val);
1634 Stmt *getSubStmt() { return getTrailingObjects<Stmt *>()[subStmtOffset()]; }
1635 const Stmt *getSubStmt() const {
1636 return getTrailingObjects<Stmt *>()[subStmtOffset()];
1639 void setSubStmt(Stmt *S) {
1640 getTrailingObjects<Stmt *>()[subStmtOffset()] = S;
1643 SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1644 SourceLocation getEndLoc() const LLVM_READONLY {
1645 // Handle deeply nested case statements with iteration instead of recursion.
1646 const CaseStmt *CS = this;
1647 while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
1650 return CS->getSubStmt()->getEndLoc();
1653 static bool classof(const Stmt *T) {
1654 return T->getStmtClass() == CaseStmtClass;
1658 child_range children() {
1659 return child_range(getTrailingObjects<Stmt *>(),
1660 getTrailingObjects<Stmt *>() +
1661 numTrailingObjects(OverloadToken<Stmt *>()));
1664 const_child_range children() const {
1665 return const_child_range(getTrailingObjects<Stmt *>(),
1666 getTrailingObjects<Stmt *>() +
1667 numTrailingObjects(OverloadToken<Stmt *>()));
1671 class DefaultStmt : public SwitchCase {
1675 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt)
1676 : SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
1678 /// Build an empty default statement.
1679 explicit DefaultStmt(EmptyShell Empty)
1680 : SwitchCase(DefaultStmtClass, Empty) {}
1682 Stmt *getSubStmt() { return SubStmt; }
1683 const Stmt *getSubStmt() const { return SubStmt; }
1684 void setSubStmt(Stmt *S) { SubStmt = S; }
1686 SourceLocation getDefaultLoc() const { return getKeywordLoc(); }
1687 void setDefaultLoc(SourceLocation L) { setKeywordLoc(L); }
1689 SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1690 SourceLocation getEndLoc() const LLVM_READONLY {
1691 return SubStmt->getEndLoc();
1694 static bool classof(const Stmt *T) {
1695 return T->getStmtClass() == DefaultStmtClass;
1699 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1701 const_child_range children() const {
1702 return const_child_range(&SubStmt, &SubStmt + 1);
1706 SourceLocation SwitchCase::getEndLoc() const {
1707 if (const auto *CS = dyn_cast<CaseStmt>(this))
1708 return CS->getEndLoc();
1709 else if (const auto *DS = dyn_cast<DefaultStmt>(this))
1710 return DS->getEndLoc();
1711 llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
1714 Stmt *SwitchCase::getSubStmt() {
1715 if (auto *CS = dyn_cast<CaseStmt>(this))
1716 return CS->getSubStmt();
1717 else if (auto *DS = dyn_cast<DefaultStmt>(this))
1718 return DS->getSubStmt();
1719 llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
1722 /// Represents a statement that could possibly have a value and type. This
1723 /// covers expression-statements, as well as labels and attributed statements.
1725 /// Value statements have a special meaning when they are the last non-null
1726 /// statement in a GNU statement expression, where they determine the value
1727 /// of the statement expression.
1728 class ValueStmt : public Stmt {
1733 const Expr *getExprStmt() const;
1734 Expr *getExprStmt() {
1735 const ValueStmt *ConstThis = this;
1736 return const_cast<Expr*>(ConstThis->getExprStmt());
1739 static bool classof(const Stmt *T) {
1740 return T->getStmtClass() >= firstValueStmtConstant &&
1741 T->getStmtClass() <= lastValueStmtConstant;
1745 /// LabelStmt - Represents a label, which has a substatement. For example:
1747 class LabelStmt : public ValueStmt {
1752 /// Build a label statement.
1753 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
1754 : ValueStmt(LabelStmtClass), TheDecl(D), SubStmt(substmt) {
1758 /// Build an empty label statement.
1759 explicit LabelStmt(EmptyShell Empty) : ValueStmt(LabelStmtClass, Empty) {}
1761 SourceLocation getIdentLoc() const { return LabelStmtBits.IdentLoc; }
1762 void setIdentLoc(SourceLocation L) { LabelStmtBits.IdentLoc = L; }
1764 LabelDecl *getDecl() const { return TheDecl; }
1765 void setDecl(LabelDecl *D) { TheDecl = D; }
1767 const char *getName() const;
1768 Stmt *getSubStmt() { return SubStmt; }
1770 const Stmt *getSubStmt() const { return SubStmt; }
1771 void setSubStmt(Stmt *SS) { SubStmt = SS; }
1773 SourceLocation getBeginLoc() const { return getIdentLoc(); }
1774 SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
1776 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1778 const_child_range children() const {
1779 return const_child_range(&SubStmt, &SubStmt + 1);
1782 static bool classof(const Stmt *T) {
1783 return T->getStmtClass() == LabelStmtClass;
1787 /// Represents an attribute applied to a statement.
1789 /// Represents an attribute applied to a statement. For example:
1790 /// [[omp::for(...)]] for (...) { ... }
1791 class AttributedStmt final
1793 private llvm::TrailingObjects<AttributedStmt, const Attr *> {
1794 friend class ASTStmtReader;
1795 friend TrailingObjects;
1799 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr *> Attrs,
1801 : ValueStmt(AttributedStmtClass), SubStmt(SubStmt) {
1802 AttributedStmtBits.NumAttrs = Attrs.size();
1803 AttributedStmtBits.AttrLoc = Loc;
1804 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
1807 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
1808 : ValueStmt(AttributedStmtClass, Empty) {
1809 AttributedStmtBits.NumAttrs = NumAttrs;
1810 AttributedStmtBits.AttrLoc = SourceLocation{};
1811 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
1814 const Attr *const *getAttrArrayPtr() const {
1815 return getTrailingObjects<const Attr *>();
1817 const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
1820 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
1821 ArrayRef<const Attr *> Attrs, Stmt *SubStmt);
1823 // Build an empty attributed statement.
1824 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
1826 SourceLocation getAttrLoc() const { return AttributedStmtBits.AttrLoc; }
1827 ArrayRef<const Attr *> getAttrs() const {
1828 return llvm::makeArrayRef(getAttrArrayPtr(), AttributedStmtBits.NumAttrs);
1831 Stmt *getSubStmt() { return SubStmt; }
1832 const Stmt *getSubStmt() const { return SubStmt; }
1834 SourceLocation getBeginLoc() const { return getAttrLoc(); }
1835 SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
1837 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1839 const_child_range children() const {
1840 return const_child_range(&SubStmt, &SubStmt + 1);
1843 static bool classof(const Stmt *T) {
1844 return T->getStmtClass() == AttributedStmtClass;
1848 /// IfStmt - This represents an if/then/else.
1851 private llvm::TrailingObjects<IfStmt, Stmt *, SourceLocation> {
1852 friend TrailingObjects;
1854 // IfStmt is followed by several trailing objects, some of which optional.
1855 // Note that it would be more convenient to put the optional trailing
1856 // objects at then end but this would change the order of the children.
1857 // The trailing objects are in order:
1859 // * A "Stmt *" for the init statement.
1860 // Present if and only if hasInitStorage().
1862 // * A "Stmt *" for the condition variable.
1863 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
1865 // * A "Stmt *" for the condition.
1866 // Always present. This is in fact a "Expr *".
1868 // * A "Stmt *" for the then statement.
1871 // * A "Stmt *" for the else statement.
1872 // Present if and only if hasElseStorage().
1874 // * A "SourceLocation" for the location of the "else".
1875 // Present if and only if hasElseStorage().
1876 enum { InitOffset = 0, ThenOffsetFromCond = 1, ElseOffsetFromCond = 2 };
1877 enum { NumMandatoryStmtPtr = 2 };
1879 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1880 return NumMandatoryStmtPtr + hasElseStorage() + hasVarStorage() +
1884 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
1885 return hasElseStorage();
1888 unsigned initOffset() const { return InitOffset; }
1889 unsigned varOffset() const { return InitOffset + hasInitStorage(); }
1890 unsigned condOffset() const {
1891 return InitOffset + hasInitStorage() + hasVarStorage();
1893 unsigned thenOffset() const { return condOffset() + ThenOffsetFromCond; }
1894 unsigned elseOffset() const { return condOffset() + ElseOffsetFromCond; }
1896 /// Build an if/then/else statement.
1897 IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr, Stmt *Init,
1898 VarDecl *Var, Expr *Cond, Stmt *Then, SourceLocation EL, Stmt *Else);
1900 /// Build an empty if/then/else statement.
1901 explicit IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit);
1904 /// Create an IfStmt.
1905 static IfStmt *Create(const ASTContext &Ctx, SourceLocation IL,
1906 bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond,
1907 Stmt *Then, SourceLocation EL = SourceLocation(),
1908 Stmt *Else = nullptr);
1910 /// Create an empty IfStmt optionally with storage for an else statement,
1911 /// condition variable and init expression.
1912 static IfStmt *CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
1915 /// True if this IfStmt has the storage for an init statement.
1916 bool hasInitStorage() const { return IfStmtBits.HasInit; }
1918 /// True if this IfStmt has storage for a variable declaration.
1919 bool hasVarStorage() const { return IfStmtBits.HasVar; }
1921 /// True if this IfStmt has storage for an else statement.
1922 bool hasElseStorage() const { return IfStmtBits.HasElse; }
1925 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1928 const Expr *getCond() const {
1929 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1932 void setCond(Expr *Cond) {
1933 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
1936 Stmt *getThen() { return getTrailingObjects<Stmt *>()[thenOffset()]; }
1937 const Stmt *getThen() const {
1938 return getTrailingObjects<Stmt *>()[thenOffset()];
1941 void setThen(Stmt *Then) {
1942 getTrailingObjects<Stmt *>()[thenOffset()] = Then;
1946 return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()]
1950 const Stmt *getElse() const {
1951 return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()]
1955 void setElse(Stmt *Else) {
1956 assert(hasElseStorage() &&
1957 "This if statement has no storage for an else statement!");
1958 getTrailingObjects<Stmt *>()[elseOffset()] = Else;
1961 /// Retrieve the variable declared in this "if" statement, if any.
1963 /// In the following example, "x" is the condition variable.
1965 /// if (int x = foo()) {
1966 /// printf("x is %d", x);
1969 VarDecl *getConditionVariable();
1970 const VarDecl *getConditionVariable() const {
1971 return const_cast<IfStmt *>(this)->getConditionVariable();
1974 /// Set the condition variable for this if statement.
1975 /// The if statement must have storage for the condition variable.
1976 void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
1978 /// If this IfStmt has a condition variable, return the faux DeclStmt
1979 /// associated with the creation of that condition variable.
1980 DeclStmt *getConditionVariableDeclStmt() {
1981 return hasVarStorage() ? static_cast<DeclStmt *>(
1982 getTrailingObjects<Stmt *>()[varOffset()])
1986 const DeclStmt *getConditionVariableDeclStmt() const {
1987 return hasVarStorage() ? static_cast<DeclStmt *>(
1988 getTrailingObjects<Stmt *>()[varOffset()])
1993 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
1997 const Stmt *getInit() const {
1998 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
2002 void setInit(Stmt *Init) {
2003 assert(hasInitStorage() &&
2004 "This if statement has no storage for an init statement!");
2005 getTrailingObjects<Stmt *>()[initOffset()] = Init;
2008 SourceLocation getIfLoc() const { return IfStmtBits.IfLoc; }
2009 void setIfLoc(SourceLocation IfLoc) { IfStmtBits.IfLoc = IfLoc; }
2011 SourceLocation getElseLoc() const {
2012 return hasElseStorage() ? *getTrailingObjects<SourceLocation>()
2016 void setElseLoc(SourceLocation ElseLoc) {
2017 assert(hasElseStorage() &&
2018 "This if statement has no storage for an else statement!");
2019 *getTrailingObjects<SourceLocation>() = ElseLoc;
2022 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
2023 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
2025 /// If this is an 'if constexpr', determine which substatement will be taken.
2026 /// Otherwise, or if the condition is value-dependent, returns None.
2027 Optional<const Stmt*> getNondiscardedCase(const ASTContext &Ctx) const;
2029 bool isObjCAvailabilityCheck() const;
2031 SourceLocation getBeginLoc() const { return getIfLoc(); }
2032 SourceLocation getEndLoc() const LLVM_READONLY {
2034 return getElse()->getEndLoc();
2035 return getThen()->getEndLoc();
2038 // Iterators over subexpressions. The iterators will include iterating
2039 // over the initialization expression referenced by the condition variable.
2040 child_range children() {
2041 return child_range(getTrailingObjects<Stmt *>(),
2042 getTrailingObjects<Stmt *>() +
2043 numTrailingObjects(OverloadToken<Stmt *>()));
2046 const_child_range children() const {
2047 return const_child_range(getTrailingObjects<Stmt *>(),
2048 getTrailingObjects<Stmt *>() +
2049 numTrailingObjects(OverloadToken<Stmt *>()));
2052 static bool classof(const Stmt *T) {
2053 return T->getStmtClass() == IfStmtClass;
2057 /// SwitchStmt - This represents a 'switch' stmt.
2058 class SwitchStmt final : public Stmt,
2059 private llvm::TrailingObjects<SwitchStmt, Stmt *> {
2060 friend TrailingObjects;
2062 /// Points to a linked list of case and default statements.
2063 SwitchCase *FirstCase;
2065 // SwitchStmt is followed by several trailing objects,
2066 // some of which optional. Note that it would be more convenient to
2067 // put the optional trailing objects at the end but this would change
2068 // the order in children().
2069 // The trailing objects are in order:
2071 // * A "Stmt *" for the init statement.
2072 // Present if and only if hasInitStorage().
2074 // * A "Stmt *" for the condition variable.
2075 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
2077 // * A "Stmt *" for the condition.
2078 // Always present. This is in fact an "Expr *".
2080 // * A "Stmt *" for the body.
2082 enum { InitOffset = 0, BodyOffsetFromCond = 1 };
2083 enum { NumMandatoryStmtPtr = 2 };
2085 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
2086 return NumMandatoryStmtPtr + hasInitStorage() + hasVarStorage();
2089 unsigned initOffset() const { return InitOffset; }
2090 unsigned varOffset() const { return InitOffset + hasInitStorage(); }
2091 unsigned condOffset() const {
2092 return InitOffset + hasInitStorage() + hasVarStorage();
2094 unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
2096 /// Build a switch statement.
2097 SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var, Expr *Cond);
2099 /// Build a empty switch statement.
2100 explicit SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar);
2103 /// Create a switch statement.
2104 static SwitchStmt *Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
2107 /// Create an empty switch statement optionally with storage for
2108 /// an init expression and a condition variable.
2109 static SwitchStmt *CreateEmpty(const ASTContext &Ctx, bool HasInit,
2112 /// True if this SwitchStmt has storage for an init statement.
2113 bool hasInitStorage() const { return SwitchStmtBits.HasInit; }
2115 /// True if this SwitchStmt has storage for a condition variable.
2116 bool hasVarStorage() const { return SwitchStmtBits.HasVar; }
2119 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2122 const Expr *getCond() const {
2123 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2126 void setCond(Expr *Cond) {
2127 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
2130 Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
2131 const Stmt *getBody() const {
2132 return getTrailingObjects<Stmt *>()[bodyOffset()];
2135 void setBody(Stmt *Body) {
2136 getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
2140 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
2144 const Stmt *getInit() const {
2145 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
2149 void setInit(Stmt *Init) {
2150 assert(hasInitStorage() &&
2151 "This switch statement has no storage for an init statement!");
2152 getTrailingObjects<Stmt *>()[initOffset()] = Init;
2155 /// Retrieve the variable declared in this "switch" statement, if any.
2157 /// In the following example, "x" is the condition variable.
2159 /// switch (int x = foo()) {
2164 VarDecl *getConditionVariable();
2165 const VarDecl *getConditionVariable() const {
2166 return const_cast<SwitchStmt *>(this)->getConditionVariable();
2169 /// Set the condition variable in this switch statement.
2170 /// The switch statement must have storage for it.
2171 void setConditionVariable(const ASTContext &Ctx, VarDecl *VD);
2173 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
2174 /// associated with the creation of that condition variable.
2175 DeclStmt *getConditionVariableDeclStmt() {
2176 return hasVarStorage() ? static_cast<DeclStmt *>(
2177 getTrailingObjects<Stmt *>()[varOffset()])
2181 const DeclStmt *getConditionVariableDeclStmt() const {
2182 return hasVarStorage() ? static_cast<DeclStmt *>(
2183 getTrailingObjects<Stmt *>()[varOffset()])
2187 SwitchCase *getSwitchCaseList() { return FirstCase; }
2188 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
2189 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
2191 SourceLocation getSwitchLoc() const { return SwitchStmtBits.SwitchLoc; }
2192 void setSwitchLoc(SourceLocation L) { SwitchStmtBits.SwitchLoc = L; }
2194 void setBody(Stmt *S, SourceLocation SL) {
2199 void addSwitchCase(SwitchCase *SC) {
2200 assert(!SC->getNextSwitchCase() &&
2201 "case/default already added to a switch");
2202 SC->setNextSwitchCase(FirstCase);
2206 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
2207 /// switch over an enum value then all cases have been explicitly covered.
2208 void setAllEnumCasesCovered() { SwitchStmtBits.AllEnumCasesCovered = true; }
2210 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
2211 /// have been explicitly covered.
2212 bool isAllEnumCasesCovered() const {
2213 return SwitchStmtBits.AllEnumCasesCovered;
2216 SourceLocation getBeginLoc() const { return getSwitchLoc(); }
2217 SourceLocation getEndLoc() const LLVM_READONLY {
2218 return getBody() ? getBody()->getEndLoc()
2219 : reinterpret_cast<const Stmt *>(getCond())->getEndLoc();
2223 child_range children() {
2224 return child_range(getTrailingObjects<Stmt *>(),
2225 getTrailingObjects<Stmt *>() +
2226 numTrailingObjects(OverloadToken<Stmt *>()));
2229 const_child_range children() const {
2230 return const_child_range(getTrailingObjects<Stmt *>(),
2231 getTrailingObjects<Stmt *>() +
2232 numTrailingObjects(OverloadToken<Stmt *>()));
2235 static bool classof(const Stmt *T) {
2236 return T->getStmtClass() == SwitchStmtClass;
2240 /// WhileStmt - This represents a 'while' stmt.
2241 class WhileStmt final : public Stmt,
2242 private llvm::TrailingObjects<WhileStmt, Stmt *> {
2243 friend TrailingObjects;
2245 // WhileStmt is followed by several trailing objects,
2246 // some of which optional. Note that it would be more
2247 // convenient to put the optional trailing object at the end
2248 // but this would affect children().
2249 // The trailing objects are in order:
2251 // * A "Stmt *" for the condition variable.
2252 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
2254 // * A "Stmt *" for the condition.
2255 // Always present. This is in fact an "Expr *".
2257 // * A "Stmt *" for the body.
2260 enum { VarOffset = 0, BodyOffsetFromCond = 1 };
2261 enum { NumMandatoryStmtPtr = 2 };
2263 unsigned varOffset() const { return VarOffset; }
2264 unsigned condOffset() const { return VarOffset + hasVarStorage(); }
2265 unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
2267 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
2268 return NumMandatoryStmtPtr + hasVarStorage();
2271 /// Build a while statement.
2272 WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond, Stmt *Body,
2275 /// Build an empty while statement.
2276 explicit WhileStmt(EmptyShell Empty, bool HasVar);
2279 /// Create a while statement.
2280 static WhileStmt *Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
2281 Stmt *Body, SourceLocation WL);
2283 /// Create an empty while statement optionally with storage for
2284 /// a condition variable.
2285 static WhileStmt *CreateEmpty(const ASTContext &Ctx, bool HasVar);
2287 /// True if this WhileStmt has storage for a condition variable.
2288 bool hasVarStorage() const { return WhileStmtBits.HasVar; }
2291 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2294 const Expr *getCond() const {
2295 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2298 void setCond(Expr *Cond) {
2299 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
2302 Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
2303 const Stmt *getBody() const {
2304 return getTrailingObjects<Stmt *>()[bodyOffset()];
2307 void setBody(Stmt *Body) {
2308 getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
2311 /// Retrieve the variable declared in this "while" statement, if any.
2313 /// In the following example, "x" is the condition variable.
2315 /// while (int x = random()) {
2319 VarDecl *getConditionVariable();
2320 const VarDecl *getConditionVariable() const {
2321 return const_cast<WhileStmt *>(this)->getConditionVariable();
2324 /// Set the condition variable of this while statement.
2325 /// The while statement must have storage for it.
2326 void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
2328 /// If this WhileStmt has a condition variable, return the faux DeclStmt
2329 /// associated with the creation of that condition variable.
2330 DeclStmt *getConditionVariableDeclStmt() {
2331 return hasVarStorage() ? static_cast<DeclStmt *>(
2332 getTrailingObjects<Stmt *>()[varOffset()])
2336 const DeclStmt *getConditionVariableDeclStmt() const {
2337 return hasVarStorage() ? static_cast<DeclStmt *>(
2338 getTrailingObjects<Stmt *>()[varOffset()])
2342 SourceLocation getWhileLoc() const { return WhileStmtBits.WhileLoc; }
2343 void setWhileLoc(SourceLocation L) { WhileStmtBits.WhileLoc = L; }
2345 SourceLocation getBeginLoc() const { return getWhileLoc(); }
2346 SourceLocation getEndLoc() const LLVM_READONLY {
2347 return getBody()->getEndLoc();
2350 static bool classof(const Stmt *T) {
2351 return T->getStmtClass() == WhileStmtClass;
2355 child_range children() {
2356 return child_range(getTrailingObjects<Stmt *>(),
2357 getTrailingObjects<Stmt *>() +
2358 numTrailingObjects(OverloadToken<Stmt *>()));
2361 const_child_range children() const {
2362 return const_child_range(getTrailingObjects<Stmt *>(),
2363 getTrailingObjects<Stmt *>() +
2364 numTrailingObjects(OverloadToken<Stmt *>()));
2368 /// DoStmt - This represents a 'do/while' stmt.
2369 class DoStmt : public Stmt {
2370 enum { BODY, COND, END_EXPR };
2371 Stmt *SubExprs[END_EXPR];
2372 SourceLocation WhileLoc;
2373 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
2376 DoStmt(Stmt *Body, Expr *Cond, SourceLocation DL, SourceLocation WL,
2378 : Stmt(DoStmtClass), WhileLoc(WL), RParenLoc(RP) {
2384 /// Build an empty do-while statement.
2385 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
2387 Expr *getCond() { return reinterpret_cast<Expr *>(SubExprs[COND]); }
2388 const Expr *getCond() const {
2389 return reinterpret_cast<Expr *>(SubExprs[COND]);
2392 void setCond(Expr *Cond) { SubExprs[COND] = reinterpret_cast<Stmt *>(Cond); }
2394 Stmt *getBody() { return SubExprs[BODY]; }
2395 const Stmt *getBody() const { return SubExprs[BODY]; }
2396 void setBody(Stmt *Body) { SubExprs[BODY] = Body; }
2398 SourceLocation getDoLoc() const { return DoStmtBits.DoLoc; }
2399 void setDoLoc(SourceLocation L) { DoStmtBits.DoLoc = L; }
2400 SourceLocation getWhileLoc() const { return WhileLoc; }
2401 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
2402 SourceLocation getRParenLoc() const { return RParenLoc; }
2403 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2405 SourceLocation getBeginLoc() const { return getDoLoc(); }
2406 SourceLocation getEndLoc() const { return getRParenLoc(); }
2408 static bool classof(const Stmt *T) {
2409 return T->getStmtClass() == DoStmtClass;
2413 child_range children() {
2414 return child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
2417 const_child_range children() const {
2418 return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
2422 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
2423 /// the init/cond/inc parts of the ForStmt will be null if they were not
2424 /// specified in the source.
2425 class ForStmt : public Stmt {
2426 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
2427 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
2428 SourceLocation LParenLoc, RParenLoc;
2431 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
2432 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
2435 /// Build an empty for statement.
2436 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
2438 Stmt *getInit() { return SubExprs[INIT]; }
2440 /// Retrieve the variable declared in this "for" statement, if any.
2442 /// In the following example, "y" is the condition variable.
2444 /// for (int x = random(); int y = mangle(x); ++x) {
2448 VarDecl *getConditionVariable() const;
2449 void setConditionVariable(const ASTContext &C, VarDecl *V);
2451 /// If this ForStmt has a condition variable, return the faux DeclStmt
2452 /// associated with the creation of that condition variable.
2453 const DeclStmt *getConditionVariableDeclStmt() const {
2454 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
2457 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
2458 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
2459 Stmt *getBody() { return SubExprs[BODY]; }
2461 const Stmt *getInit() const { return SubExprs[INIT]; }
2462 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
2463 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
2464 const Stmt *getBody() const { return SubExprs[BODY]; }
2466 void setInit(Stmt *S) { SubExprs[INIT] = S; }
2467 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
2468 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
2469 void setBody(Stmt *S) { SubExprs[BODY] = S; }
2471 SourceLocation getForLoc() const { return ForStmtBits.ForLoc; }
2472 void setForLoc(SourceLocation L) { ForStmtBits.ForLoc = L; }
2473 SourceLocation getLParenLoc() const { return LParenLoc; }
2474 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2475 SourceLocation getRParenLoc() const { return RParenLoc; }
2476 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2478 SourceLocation getBeginLoc() const { return getForLoc(); }
2479 SourceLocation getEndLoc() const { return getBody()->getEndLoc(); }
2481 static bool classof(const Stmt *T) {
2482 return T->getStmtClass() == ForStmtClass;
2486 child_range children() {
2487 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
2490 const_child_range children() const {
2491 return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
2495 /// GotoStmt - This represents a direct goto.
2496 class GotoStmt : public Stmt {
2498 SourceLocation LabelLoc;
2501 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
2502 : Stmt(GotoStmtClass), Label(label), LabelLoc(LL) {
2506 /// Build an empty goto statement.
2507 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
2509 LabelDecl *getLabel() const { return Label; }
2510 void setLabel(LabelDecl *D) { Label = D; }
2512 SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
2513 void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
2514 SourceLocation getLabelLoc() const { return LabelLoc; }
2515 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
2517 SourceLocation getBeginLoc() const { return getGotoLoc(); }
2518 SourceLocation getEndLoc() const { return getLabelLoc(); }
2520 static bool classof(const Stmt *T) {
2521 return T->getStmtClass() == GotoStmtClass;
2525 child_range children() {
2526 return child_range(child_iterator(), child_iterator());
2529 const_child_range children() const {
2530 return const_child_range(const_child_iterator(), const_child_iterator());
2534 /// IndirectGotoStmt - This represents an indirect goto.
2535 class IndirectGotoStmt : public Stmt {
2536 SourceLocation StarLoc;
2540 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc, Expr *target)
2541 : Stmt(IndirectGotoStmtClass), StarLoc(starLoc) {
2543 setGotoLoc(gotoLoc);
2546 /// Build an empty indirect goto statement.
2547 explicit IndirectGotoStmt(EmptyShell Empty)
2548 : Stmt(IndirectGotoStmtClass, Empty) {}
2550 void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
2551 SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
2552 void setStarLoc(SourceLocation L) { StarLoc = L; }
2553 SourceLocation getStarLoc() const { return StarLoc; }
2555 Expr *getTarget() { return reinterpret_cast<Expr *>(Target); }
2556 const Expr *getTarget() const {
2557 return reinterpret_cast<const Expr *>(Target);
2559 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt *>(E); }
2561 /// getConstantTarget - Returns the fixed target of this indirect
2562 /// goto, if one exists.
2563 LabelDecl *getConstantTarget();
2564 const LabelDecl *getConstantTarget() const {
2565 return const_cast<IndirectGotoStmt *>(this)->getConstantTarget();
2568 SourceLocation getBeginLoc() const { return getGotoLoc(); }
2569 SourceLocation getEndLoc() const LLVM_READONLY { return Target->getEndLoc(); }
2571 static bool classof(const Stmt *T) {
2572 return T->getStmtClass() == IndirectGotoStmtClass;
2576 child_range children() { return child_range(&Target, &Target + 1); }
2578 const_child_range children() const {
2579 return const_child_range(&Target, &Target + 1);
2583 /// ContinueStmt - This represents a continue.
2584 class ContinueStmt : public Stmt {
2586 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass) {
2590 /// Build an empty continue statement.
2591 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
2593 SourceLocation getContinueLoc() const { return ContinueStmtBits.ContinueLoc; }
2594 void setContinueLoc(SourceLocation L) { ContinueStmtBits.ContinueLoc = L; }
2596 SourceLocation getBeginLoc() const { return getContinueLoc(); }
2597 SourceLocation getEndLoc() const { return getContinueLoc(); }
2599 static bool classof(const Stmt *T) {
2600 return T->getStmtClass() == ContinueStmtClass;
2604 child_range children() {
2605 return child_range(child_iterator(), child_iterator());
2608 const_child_range children() const {
2609 return const_child_range(const_child_iterator(), const_child_iterator());
2613 /// BreakStmt - This represents a break.
2614 class BreakStmt : public Stmt {
2616 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass) {
2620 /// Build an empty break statement.
2621 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
2623 SourceLocation getBreakLoc() const { return BreakStmtBits.BreakLoc; }
2624 void setBreakLoc(SourceLocation L) { BreakStmtBits.BreakLoc = L; }
2626 SourceLocation getBeginLoc() const { return getBreakLoc(); }
2627 SourceLocation getEndLoc() const { return getBreakLoc(); }
2629 static bool classof(const Stmt *T) {
2630 return T->getStmtClass() == BreakStmtClass;
2634 child_range children() {
2635 return child_range(child_iterator(), child_iterator());
2638 const_child_range children() const {
2639 return const_child_range(const_child_iterator(), const_child_iterator());
2643 /// ReturnStmt - This represents a return, optionally of an expression:
2647 /// Note that GCC allows return with no argument in a function declared to
2648 /// return a value, and it allows returning a value in functions declared to
2649 /// return void. We explicitly model this in the AST, which means you can't
2650 /// depend on the return type of the function and the presence of an argument.
2651 class ReturnStmt final
2653 private llvm::TrailingObjects<ReturnStmt, const VarDecl *> {
2654 friend TrailingObjects;
2656 /// The return expression.
2659 // ReturnStmt is followed optionally by a trailing "const VarDecl *"
2660 // for the NRVO candidate. Present if and only if hasNRVOCandidate().
2662 /// True if this ReturnStmt has storage for an NRVO candidate.
2663 bool hasNRVOCandidate() const { return ReturnStmtBits.HasNRVOCandidate; }
2665 unsigned numTrailingObjects(OverloadToken<const VarDecl *>) const {
2666 return hasNRVOCandidate();
2669 /// Build a return statement.
2670 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate);
2672 /// Build an empty return statement.
2673 explicit ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate);
2676 /// Create a return statement.
2677 static ReturnStmt *Create(const ASTContext &Ctx, SourceLocation RL, Expr *E,
2678 const VarDecl *NRVOCandidate);
2680 /// Create an empty return statement, optionally with
2681 /// storage for an NRVO candidate.
2682 static ReturnStmt *CreateEmpty(const ASTContext &Ctx, bool HasNRVOCandidate);
2684 Expr *getRetValue() { return reinterpret_cast<Expr *>(RetExpr); }
2685 const Expr *getRetValue() const { return reinterpret_cast<Expr *>(RetExpr); }
2686 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt *>(E); }
2688 /// Retrieve the variable that might be used for the named return
2689 /// value optimization.
2691 /// The optimization itself can only be performed if the variable is
2692 /// also marked as an NRVO object.
2693 const VarDecl *getNRVOCandidate() const {
2694 return hasNRVOCandidate() ? *getTrailingObjects<const VarDecl *>()
2698 /// Set the variable that might be used for the named return value
2699 /// optimization. The return statement must have storage for it,
2700 /// which is the case if and only if hasNRVOCandidate() is true.
2701 void setNRVOCandidate(const VarDecl *Var) {
2702 assert(hasNRVOCandidate() &&
2703 "This return statement has no storage for an NRVO candidate!");
2704 *getTrailingObjects<const VarDecl *>() = Var;
2707 SourceLocation getReturnLoc() const { return ReturnStmtBits.RetLoc; }
2708 void setReturnLoc(SourceLocation L) { ReturnStmtBits.RetLoc = L; }
2710 SourceLocation getBeginLoc() const { return getReturnLoc(); }
2711 SourceLocation getEndLoc() const LLVM_READONLY {
2712 return RetExpr ? RetExpr->getEndLoc() : getReturnLoc();
2715 static bool classof(const Stmt *T) {
2716 return T->getStmtClass() == ReturnStmtClass;
2720 child_range children() {
2722 return child_range(&RetExpr, &RetExpr + 1);
2723 return child_range(child_iterator(), child_iterator());
2726 const_child_range children() const {
2728 return const_child_range(&RetExpr, &RetExpr + 1);
2729 return const_child_range(const_child_iterator(), const_child_iterator());
2733 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
2734 class AsmStmt : public Stmt {
2736 friend class ASTStmtReader;
2738 SourceLocation AsmLoc;
2740 /// True if the assembly statement does not have any input or output
2744 /// If true, treat this inline assembly as having side effects.
2745 /// This assembly statement should not be optimized, deleted or moved.
2748 unsigned NumOutputs;
2750 unsigned NumClobbers;
2752 Stmt **Exprs = nullptr;
2754 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
2755 unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
2756 : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
2757 NumOutputs(numoutputs), NumInputs(numinputs),
2758 NumClobbers(numclobbers) {}
2761 /// Build an empty inline-assembly statement.
2762 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
2764 SourceLocation getAsmLoc() const { return AsmLoc; }
2765 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
2767 bool isSimple() const { return IsSimple; }
2768 void setSimple(bool V) { IsSimple = V; }
2770 bool isVolatile() const { return IsVolatile; }
2771 void setVolatile(bool V) { IsVolatile = V; }
2773 SourceLocation getBeginLoc() const LLVM_READONLY { return {}; }
2774 SourceLocation getEndLoc() const LLVM_READONLY { return {}; }
2776 //===--- Asm String Analysis ---===//
2778 /// Assemble final IR asm string.
2779 std::string generateAsmString(const ASTContext &C) const;
2781 //===--- Output operands ---===//
2783 unsigned getNumOutputs() const { return NumOutputs; }
2785 /// getOutputConstraint - Return the constraint string for the specified
2786 /// output operand. All output constraints are known to be non-empty (either
2788 StringRef getOutputConstraint(unsigned i) const;
2790 /// isOutputPlusConstraint - Return true if the specified output constraint
2791 /// is a "+" constraint (which is both an input and an output) or false if it
2792 /// is an "=" constraint (just an output).
2793 bool isOutputPlusConstraint(unsigned i) const {
2794 return getOutputConstraint(i)[0] == '+';
2797 const Expr *getOutputExpr(unsigned i) const;
2799 /// getNumPlusOperands - Return the number of output operands that have a "+"
2801 unsigned getNumPlusOperands() const;
2803 //===--- Input operands ---===//
2805 unsigned getNumInputs() const { return NumInputs; }
2807 /// getInputConstraint - Return the specified input constraint. Unlike output
2808 /// constraints, these can be empty.
2809 StringRef getInputConstraint(unsigned i) const;
2811 const Expr *getInputExpr(unsigned i) const;
2813 //===--- Other ---===//
2815 unsigned getNumClobbers() const { return NumClobbers; }
2816 StringRef getClobber(unsigned i) const;
2818 static bool classof(const Stmt *T) {
2819 return T->getStmtClass() == GCCAsmStmtClass ||
2820 T->getStmtClass() == MSAsmStmtClass;
2823 // Input expr iterators.
2825 using inputs_iterator = ExprIterator;
2826 using const_inputs_iterator = ConstExprIterator;
2827 using inputs_range = llvm::iterator_range<inputs_iterator>;
2828 using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
2830 inputs_iterator begin_inputs() {
2831 return &Exprs[0] + NumOutputs;
2834 inputs_iterator end_inputs() {
2835 return &Exprs[0] + NumOutputs + NumInputs;
2838 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
2840 const_inputs_iterator begin_inputs() const {
2841 return &Exprs[0] + NumOutputs;
2844 const_inputs_iterator end_inputs() const {
2845 return &Exprs[0] + NumOutputs + NumInputs;
2848 inputs_const_range inputs() const {
2849 return inputs_const_range(begin_inputs(), end_inputs());
2852 // Output expr iterators.
2854 using outputs_iterator = ExprIterator;
2855 using const_outputs_iterator = ConstExprIterator;
2856 using outputs_range = llvm::iterator_range<outputs_iterator>;
2857 using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
2859 outputs_iterator begin_outputs() {
2863 outputs_iterator end_outputs() {
2864 return &Exprs[0] + NumOutputs;
2867 outputs_range outputs() {
2868 return outputs_range(begin_outputs(), end_outputs());
2871 const_outputs_iterator begin_outputs() const {
2875 const_outputs_iterator end_outputs() const {
2876 return &Exprs[0] + NumOutputs;
2879 outputs_const_range outputs() const {
2880 return outputs_const_range(begin_outputs(), end_outputs());
2883 child_range children() {
2884 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
2887 const_child_range children() const {
2888 return const_child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
2892 /// This represents a GCC inline-assembly statement extension.
2893 class GCCAsmStmt : public AsmStmt {
2894 friend class ASTStmtReader;
2896 SourceLocation RParenLoc;
2897 StringLiteral *AsmStr;
2899 // FIXME: If we wanted to, we could allocate all of these in one big array.
2900 StringLiteral **Constraints = nullptr;
2901 StringLiteral **Clobbers = nullptr;
2902 IdentifierInfo **Names = nullptr;
2903 unsigned NumLabels = 0;
2906 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
2907 bool isvolatile, unsigned numoutputs, unsigned numinputs,
2908 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
2909 StringLiteral *asmstr, unsigned numclobbers,
2910 StringLiteral **clobbers, unsigned numlabels,
2911 SourceLocation rparenloc);
2913 /// Build an empty inline-assembly statement.
2914 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
2916 SourceLocation getRParenLoc() const { return RParenLoc; }
2917 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2919 //===--- Asm String Analysis ---===//
2921 const StringLiteral *getAsmString() const { return AsmStr; }
2922 StringLiteral *getAsmString() { return AsmStr; }
2923 void setAsmString(StringLiteral *E) { AsmStr = E; }
2925 /// AsmStringPiece - this is part of a decomposed asm string specification
2926 /// (for use with the AnalyzeAsmString function below). An asm string is
2927 /// considered to be a concatenation of these parts.
2928 class AsmStringPiece {
2931 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
2932 Operand // Operand reference, with optional modifier %c4.
2940 // Source range for operand references.
2941 CharSourceRange Range;
2944 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
2945 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
2947 : MyKind(Operand), Str(S), OperandNo(OpNo),
2948 Range(CharSourceRange::getCharRange(Begin, End)) {}
2950 bool isString() const { return MyKind == String; }
2951 bool isOperand() const { return MyKind == Operand; }
2953 const std::string &getString() const { return Str; }
2955 unsigned getOperandNo() const {
2956 assert(isOperand());
2960 CharSourceRange getRange() const {
2961 assert(isOperand() && "Range is currently used only for Operands.");
2965 /// getModifier - Get the modifier for this operand, if present. This
2966 /// returns '\0' if there was no modifier.
2967 char getModifier() const;
2970 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
2971 /// it into pieces. If the asm string is erroneous, emit errors and return
2972 /// true, otherwise return false. This handles canonicalization and
2973 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
2974 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
2975 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
2976 const ASTContext &C, unsigned &DiagOffs) const;
2978 /// Assemble final IR asm string.
2979 std::string generateAsmString(const ASTContext &C) const;
2981 //===--- Output operands ---===//
2983 IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; }
2985 StringRef getOutputName(unsigned i) const {
2986 if (IdentifierInfo *II = getOutputIdentifier(i))
2987 return II->getName();
2992 StringRef getOutputConstraint(unsigned i) const;
2994 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
2995 return Constraints[i];
2997 StringLiteral *getOutputConstraintLiteral(unsigned i) {
2998 return Constraints[i];
3001 Expr *getOutputExpr(unsigned i);
3003 const Expr *getOutputExpr(unsigned i) const {
3004 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
3007 //===--- Input operands ---===//
3009 IdentifierInfo *getInputIdentifier(unsigned i) const {
3010 return Names[i + NumOutputs];
3013 StringRef getInputName(unsigned i) const {
3014 if (IdentifierInfo *II = getInputIdentifier(i))
3015 return II->getName();
3020 StringRef getInputConstraint(unsigned i) const;
3022 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
3023 return Constraints[i + NumOutputs];
3025 StringLiteral *getInputConstraintLiteral(unsigned i) {
3026 return Constraints[i + NumOutputs];
3029 Expr *getInputExpr(unsigned i);
3030 void setInputExpr(unsigned i, Expr *E);
3032 const Expr *getInputExpr(unsigned i) const {
3033 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
3036 //===--- Labels ---===//
3038 bool isAsmGoto() const {
3039 return NumLabels > 0;
3042 unsigned getNumLabels() const {
3046 IdentifierInfo *getLabelIdentifier(unsigned i) const {
3047 return Names[i + NumInputs];
3050 AddrLabelExpr *getLabelExpr(unsigned i) const;
3051 StringRef getLabelName(unsigned i) const;
3052 using labels_iterator = CastIterator<AddrLabelExpr>;
3053 using const_labels_iterator = ConstCastIterator<AddrLabelExpr>;
3054 using labels_range = llvm::iterator_range<labels_iterator>;
3055 using labels_const_range = llvm::iterator_range<const_labels_iterator>;
3057 labels_iterator begin_labels() {
3058 return &Exprs[0] + NumInputs;
3061 labels_iterator end_labels() {
3062 return &Exprs[0] + NumInputs + NumLabels;
3065 labels_range labels() {
3066 return labels_range(begin_labels(), end_labels());
3069 const_labels_iterator begin_labels() const {
3070 return &Exprs[0] + NumInputs;
3073 const_labels_iterator end_labels() const {
3074 return &Exprs[0] + NumInputs + NumLabels;
3077 labels_const_range labels() const {
3078 return labels_const_range(begin_labels(), end_labels());
3082 void setOutputsAndInputsAndClobbers(const ASTContext &C,
3083 IdentifierInfo **Names,
3084 StringLiteral **Constraints,
3086 unsigned NumOutputs,
3089 StringLiteral **Clobbers,
3090 unsigned NumClobbers);
3093 //===--- Other ---===//
3095 /// getNamedOperand - Given a symbolic operand reference like %[foo],
3096 /// translate this into a numeric value needed to reference the same operand.
3097 /// This returns -1 if the operand name is invalid.
3098 int getNamedOperand(StringRef SymbolicName) const;
3100 StringRef getClobber(unsigned i) const;
3102 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
3103 const StringLiteral *getClobberStringLiteral(unsigned i) const {
3107 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
3108 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
3110 static bool classof(const Stmt *T) {
3111 return T->getStmtClass() == GCCAsmStmtClass;
3115 /// This represents a Microsoft inline-assembly statement extension.
3116 class MSAsmStmt : public AsmStmt {
3117 friend class ASTStmtReader;
3119 SourceLocation LBraceLoc, EndLoc;
3122 unsigned NumAsmToks = 0;
3124 Token *AsmToks = nullptr;
3125 StringRef *Constraints = nullptr;
3126 StringRef *Clobbers = nullptr;
3129 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
3130 SourceLocation lbraceloc, bool issimple, bool isvolatile,
3131 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
3132 ArrayRef<StringRef> constraints,
3133 ArrayRef<Expr*> exprs, StringRef asmstr,
3134 ArrayRef<StringRef> clobbers, SourceLocation endloc);
3136 /// Build an empty MS-style inline-assembly statement.
3137 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
3139 SourceLocation getLBraceLoc() const { return LBraceLoc; }
3140 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
3141 SourceLocation getEndLoc() const { return EndLoc; }
3142 void setEndLoc(SourceLocation L) { EndLoc = L; }
3144 bool hasBraces() const { return LBraceLoc.isValid(); }
3146 unsigned getNumAsmToks() { return NumAsmToks; }
3147 Token *getAsmToks() { return AsmToks; }
3149 //===--- Asm String Analysis ---===//
3150 StringRef getAsmString() const { return AsmStr; }
3152 /// Assemble final IR asm string.
3153 std::string generateAsmString(const ASTContext &C) const;
3155 //===--- Output operands ---===//
3157 StringRef getOutputConstraint(unsigned i) const {
3158 assert(i < NumOutputs);
3159 return Constraints[i];
3162 Expr *getOutputExpr(unsigned i);
3164 const Expr *getOutputExpr(unsigned i) const {
3165 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
3168 //===--- Input operands ---===//
3170 StringRef getInputConstraint(unsigned i) const {
3171 assert(i < NumInputs);
3172 return Constraints[i + NumOutputs];
3175 Expr *getInputExpr(unsigned i);
3176 void setInputExpr(unsigned i, Expr *E);
3178 const Expr *getInputExpr(unsigned i) const {
3179 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
3182 //===--- Other ---===//
3184 ArrayRef<StringRef> getAllConstraints() const {
3185 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
3188 ArrayRef<StringRef> getClobbers() const {
3189 return llvm::makeArrayRef(Clobbers, NumClobbers);
3192 ArrayRef<Expr*> getAllExprs() const {
3193 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
3194 NumInputs + NumOutputs);
3197 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
3200 void initialize(const ASTContext &C, StringRef AsmString,
3201 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
3202 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
3205 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
3207 static bool classof(const Stmt *T) {
3208 return T->getStmtClass() == MSAsmStmtClass;
3211 child_range children() {
3212 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
3215 const_child_range children() const {
3216 return const_child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
3220 class SEHExceptStmt : public Stmt {
3221 friend class ASTReader;
3222 friend class ASTStmtReader;
3227 enum { FILTER_EXPR, BLOCK };
3229 SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
3230 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
3233 static SEHExceptStmt* Create(const ASTContext &C,
3234 SourceLocation ExceptLoc,
3238 SourceLocation getBeginLoc() const LLVM_READONLY { return getExceptLoc(); }
3240 SourceLocation getExceptLoc() const { return Loc; }
3241 SourceLocation getEndLoc() const { return getBlock()->getEndLoc(); }
3243 Expr *getFilterExpr() const {
3244 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
3247 CompoundStmt *getBlock() const {
3248 return cast<CompoundStmt>(Children[BLOCK]);
3251 child_range children() {
3252 return child_range(Children, Children+2);
3255 const_child_range children() const {
3256 return const_child_range(Children, Children + 2);
3259 static bool classof(const Stmt *T) {
3260 return T->getStmtClass() == SEHExceptStmtClass;
3264 class SEHFinallyStmt : public Stmt {
3265 friend class ASTReader;
3266 friend class ASTStmtReader;
3271 SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
3272 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
3275 static SEHFinallyStmt* Create(const ASTContext &C,
3276 SourceLocation FinallyLoc,
3279 SourceLocation getBeginLoc() const LLVM_READONLY { return getFinallyLoc(); }
3281 SourceLocation getFinallyLoc() const { return Loc; }
3282 SourceLocation getEndLoc() const { return Block->getEndLoc(); }
3284 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
3286 child_range children() {
3287 return child_range(&Block,&Block+1);
3290 const_child_range children() const {
3291 return const_child_range(&Block, &Block + 1);
3294 static bool classof(const Stmt *T) {
3295 return T->getStmtClass() == SEHFinallyStmtClass;
3299 class SEHTryStmt : public Stmt {
3300 friend class ASTReader;
3301 friend class ASTStmtReader;
3304 SourceLocation TryLoc;
3307 enum { TRY = 0, HANDLER = 1 };
3309 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
3310 SourceLocation TryLoc,
3314 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
3317 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
3318 SourceLocation TryLoc, Stmt *TryBlock,
3321 SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
3323 SourceLocation getTryLoc() const { return TryLoc; }
3324 SourceLocation getEndLoc() const { return Children[HANDLER]->getEndLoc(); }
3326 bool getIsCXXTry() const { return IsCXXTry; }
3328 CompoundStmt* getTryBlock() const {
3329 return cast<CompoundStmt>(Children[TRY]);
3332 Stmt *getHandler() const { return Children[HANDLER]; }
3334 /// Returns 0 if not defined
3335 SEHExceptStmt *getExceptHandler() const;
3336 SEHFinallyStmt *getFinallyHandler() const;
3338 child_range children() {
3339 return child_range(Children, Children+2);
3342 const_child_range children() const {
3343 return const_child_range(Children, Children + 2);
3346 static bool classof(const Stmt *T) {
3347 return T->getStmtClass() == SEHTryStmtClass;
3351 /// Represents a __leave statement.
3352 class SEHLeaveStmt : public Stmt {
3353 SourceLocation LeaveLoc;
3356 explicit SEHLeaveStmt(SourceLocation LL)
3357 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
3359 /// Build an empty __leave statement.
3360 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
3362 SourceLocation getLeaveLoc() const { return LeaveLoc; }
3363 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
3365 SourceLocation getBeginLoc() const LLVM_READONLY { return LeaveLoc; }
3366 SourceLocation getEndLoc() const LLVM_READONLY { return LeaveLoc; }
3368 static bool classof(const Stmt *T) {
3369 return T->getStmtClass() == SEHLeaveStmtClass;
3373 child_range children() {
3374 return child_range(child_iterator(), child_iterator());
3377 const_child_range children() const {
3378 return const_child_range(const_child_iterator(), const_child_iterator());
3382 /// This captures a statement into a function. For example, the following
3383 /// pragma annotated compound statement can be represented as a CapturedStmt,
3384 /// and this compound statement is the body of an anonymous outlined function.
3386 /// #pragma omp parallel
3391 class CapturedStmt : public Stmt {
3393 /// The different capture forms: by 'this', by reference, capture for
3394 /// variable-length array type etc.
3395 enum VariableCaptureKind {
3402 /// Describes the capture of either a variable, or 'this', or
3403 /// variable-length array type.
3405 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
3409 friend class ASTStmtReader;
3411 /// Create a new capture.
3413 /// \param Loc The source location associated with this capture.
3415 /// \param Kind The kind of capture (this, ByRef, ...).
3417 /// \param Var The variable being captured, or null if capturing this.
3418 Capture(SourceLocation Loc, VariableCaptureKind Kind,
3419 VarDecl *Var = nullptr);
3421 /// Determine the kind of capture.
3422 VariableCaptureKind getCaptureKind() const;
3424 /// Retrieve the source location at which the variable or 'this' was
3426 SourceLocation getLocation() const { return Loc; }
3428 /// Determine whether this capture handles the C++ 'this' pointer.
3429 bool capturesThis() const { return getCaptureKind() == VCK_This; }
3431 /// Determine whether this capture handles a variable (by reference).
3432 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
3434 /// Determine whether this capture handles a variable by copy.
3435 bool capturesVariableByCopy() const {
3436 return getCaptureKind() == VCK_ByCopy;
3439 /// Determine whether this capture handles a variable-length array
3441 bool capturesVariableArrayType() const {
3442 return getCaptureKind() == VCK_VLAType;
3445 /// Retrieve the declaration of the variable being captured.
3447 /// This operation is only valid if this capture captures a variable.
3448 VarDecl *getCapturedVar() const;
3452 /// The number of variable captured, including 'this'.
3453 unsigned NumCaptures;
3455 /// The pointer part is the implicit the outlined function and the
3456 /// int part is the captured region kind, 'CR_Default' etc.
3457 llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind;
3459 /// The record for captured variables, a RecordDecl or CXXRecordDecl.
3460 RecordDecl *TheRecordDecl = nullptr;
3462 /// Construct a captured statement.
3463 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
3464 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
3466 /// Construct an empty captured statement.
3467 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
3469 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
3471 Stmt *const *getStoredStmts() const {
3472 return reinterpret_cast<Stmt *const *>(this + 1);
3475 Capture *getStoredCaptures() const;
3477 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
3480 friend class ASTStmtReader;
3482 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
3483 CapturedRegionKind Kind,
3484 ArrayRef<Capture> Captures,
3485 ArrayRef<Expr *> CaptureInits,
3486 CapturedDecl *CD, RecordDecl *RD);
3488 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
3489 unsigned NumCaptures);
3491 /// Retrieve the statement being captured.
3492 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
3493 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
3495 /// Retrieve the outlined function declaration.
3496 CapturedDecl *getCapturedDecl();
3497 const CapturedDecl *getCapturedDecl() const;
3499 /// Set the outlined function declaration.
3500 void setCapturedDecl(CapturedDecl *D);
3502 /// Retrieve the captured region kind.
3503 CapturedRegionKind getCapturedRegionKind() const;
3505 /// Set the captured region kind.
3506 void setCapturedRegionKind(CapturedRegionKind Kind);
3508 /// Retrieve the record declaration for captured variables.
3509 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
3511 /// Set the record declaration for captured variables.
3512 void setCapturedRecordDecl(RecordDecl *D) {
3513 assert(D && "null RecordDecl");
3517 /// True if this variable has been captured.
3518 bool capturesVariable(const VarDecl *Var) const;
3520 /// An iterator that walks over the captures.
3521 using capture_iterator = Capture *;
3522 using const_capture_iterator = const Capture *;
3523 using capture_range = llvm::iterator_range<capture_iterator>;
3524 using capture_const_range = llvm::iterator_range<const_capture_iterator>;
3526 capture_range captures() {
3527 return capture_range(capture_begin(), capture_end());
3529 capture_const_range captures() const {
3530 return capture_const_range(capture_begin(), capture_end());
3533 /// Retrieve an iterator pointing to the first capture.
3534 capture_iterator capture_begin() { return getStoredCaptures(); }
3535 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
3537 /// Retrieve an iterator pointing past the end of the sequence of
3539 capture_iterator capture_end() const {
3540 return getStoredCaptures() + NumCaptures;
3543 /// Retrieve the number of captures, including 'this'.
3544 unsigned capture_size() const { return NumCaptures; }
3546 /// Iterator that walks over the capture initialization arguments.
3547 using capture_init_iterator = Expr **;
3548 using capture_init_range = llvm::iterator_range<capture_init_iterator>;
3550 /// Const iterator that walks over the capture initialization
3552 using const_capture_init_iterator = Expr *const *;
3553 using const_capture_init_range =
3554 llvm::iterator_range<const_capture_init_iterator>;
3556 capture_init_range capture_inits() {
3557 return capture_init_range(capture_init_begin(), capture_init_end());
3560 const_capture_init_range capture_inits() const {
3561 return const_capture_init_range(capture_init_begin(), capture_init_end());
3564 /// Retrieve the first initialization argument.
3565 capture_init_iterator capture_init_begin() {
3566 return reinterpret_cast<Expr **>(getStoredStmts());
3569 const_capture_init_iterator capture_init_begin() const {
3570 return reinterpret_cast<Expr *const *>(getStoredStmts());
3573 /// Retrieve the iterator pointing one past the last initialization
3575 capture_init_iterator capture_init_end() {
3576 return capture_init_begin() + NumCaptures;
3579 const_capture_init_iterator capture_init_end() const {
3580 return capture_init_begin() + NumCaptures;
3583 SourceLocation getBeginLoc() const LLVM_READONLY {
3584 return getCapturedStmt()->getBeginLoc();
3587 SourceLocation getEndLoc() const LLVM_READONLY {
3588 return getCapturedStmt()->getEndLoc();
3591 SourceRange getSourceRange() const LLVM_READONLY {
3592 return getCapturedStmt()->getSourceRange();
3595 static bool classof(const Stmt *T) {
3596 return T->getStmtClass() == CapturedStmtClass;
3599 child_range children();
3601 const_child_range children() const;
3604 } // namespace clang
3606 #endif // LLVM_CLANG_AST_STMT_H