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 //===--- C++ Expression bitfields classes ---===//
593 class CXXOperatorCallExprBitfields {
594 friend class ASTStmtReader;
595 friend class CXXOperatorCallExpr;
597 unsigned : NumCallExprBits;
599 /// The kind of this overloaded operator. One of the enumerator
600 /// value of OverloadedOperatorKind.
601 unsigned OperatorKind : 6;
603 // Only meaningful for floating point types.
604 unsigned FPFeatures : 3;
607 class CXXBoolLiteralExprBitfields {
608 friend class CXXBoolLiteralExpr;
610 unsigned : NumExprBits;
612 /// The value of the boolean literal.
615 /// The location of the boolean literal.
619 class CXXNullPtrLiteralExprBitfields {
620 friend class CXXNullPtrLiteralExpr;
622 unsigned : NumExprBits;
624 /// The location of the null pointer literal.
628 class CXXThisExprBitfields {
629 friend class CXXThisExpr;
631 unsigned : NumExprBits;
633 /// Whether this is an implicit "this".
634 unsigned IsImplicit : 1;
636 /// The location of the "this".
640 class CXXThrowExprBitfields {
641 friend class ASTStmtReader;
642 friend class CXXThrowExpr;
644 unsigned : NumExprBits;
646 /// Whether the thrown variable (if any) is in scope.
647 unsigned IsThrownVariableInScope : 1;
649 /// The location of the "throw".
650 SourceLocation ThrowLoc;
653 class CXXDefaultArgExprBitfields {
654 friend class ASTStmtReader;
655 friend class CXXDefaultArgExpr;
657 unsigned : NumExprBits;
659 /// The location where the default argument expression was used.
663 class CXXDefaultInitExprBitfields {
664 friend class ASTStmtReader;
665 friend class CXXDefaultInitExpr;
667 unsigned : NumExprBits;
669 /// The location where the default initializer expression was used.
673 class CXXScalarValueInitExprBitfields {
674 friend class ASTStmtReader;
675 friend class CXXScalarValueInitExpr;
677 unsigned : NumExprBits;
679 SourceLocation RParenLoc;
682 class CXXNewExprBitfields {
683 friend class ASTStmtReader;
684 friend class ASTStmtWriter;
685 friend class CXXNewExpr;
687 unsigned : NumExprBits;
689 /// Was the usage ::new, i.e. is the global new to be used?
690 unsigned IsGlobalNew : 1;
692 /// Do we allocate an array? If so, the first trailing "Stmt *" is the
694 unsigned IsArray : 1;
696 /// Should the alignment be passed to the allocation function?
697 unsigned ShouldPassAlignment : 1;
699 /// If this is an array allocation, does the usual deallocation
700 /// function for the allocated type want to know the allocated size?
701 unsigned UsualArrayDeleteWantsSize : 1;
703 /// What kind of initializer do we have? Could be none, parens, or braces.
704 /// In storage, we distinguish between "none, and no initializer expr", and
705 /// "none, but an implicit initializer expr".
706 unsigned StoredInitializationStyle : 2;
708 /// True if the allocated type was expressed as a parenthesized type-id.
709 unsigned IsParenTypeId : 1;
711 /// The number of placement new arguments.
712 unsigned NumPlacementArgs;
715 class CXXDeleteExprBitfields {
716 friend class ASTStmtReader;
717 friend class CXXDeleteExpr;
719 unsigned : NumExprBits;
721 /// Is this a forced global delete, i.e. "::delete"?
722 unsigned GlobalDelete : 1;
724 /// Is this the array form of delete, i.e. "delete[]"?
725 unsigned ArrayForm : 1;
727 /// ArrayFormAsWritten can be different from ArrayForm if 'delete' is
728 /// applied to pointer-to-array type (ArrayFormAsWritten will be false
729 /// while ArrayForm will be true).
730 unsigned ArrayFormAsWritten : 1;
732 /// Does the usual deallocation function for the element type require
733 /// a size_t argument?
734 unsigned UsualArrayDeleteWantsSize : 1;
736 /// Location of the expression.
740 class TypeTraitExprBitfields {
741 friend class ASTStmtReader;
742 friend class ASTStmtWriter;
743 friend class TypeTraitExpr;
745 unsigned : NumExprBits;
747 /// The kind of type trait, which is a value of a TypeTrait enumerator.
750 /// If this expression is not value-dependent, this indicates whether
751 /// the trait evaluated true or false.
754 /// The number of arguments to this type trait.
755 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
758 class DependentScopeDeclRefExprBitfields {
759 friend class ASTStmtReader;
760 friend class ASTStmtWriter;
761 friend class DependentScopeDeclRefExpr;
763 unsigned : NumExprBits;
765 /// Whether the name includes info for explicit template
766 /// keyword and arguments.
767 unsigned HasTemplateKWAndArgsInfo : 1;
770 class CXXConstructExprBitfields {
771 friend class ASTStmtReader;
772 friend class CXXConstructExpr;
774 unsigned : NumExprBits;
776 unsigned Elidable : 1;
777 unsigned HadMultipleCandidates : 1;
778 unsigned ListInitialization : 1;
779 unsigned StdInitListInitialization : 1;
780 unsigned ZeroInitialization : 1;
781 unsigned ConstructionKind : 3;
786 class ExprWithCleanupsBitfields {
787 friend class ASTStmtReader; // deserialization
788 friend class ExprWithCleanups;
790 unsigned : NumExprBits;
792 // When false, it must not have side effects.
793 unsigned CleanupsHaveSideEffects : 1;
795 unsigned NumObjects : 32 - 1 - NumExprBits;
798 class CXXUnresolvedConstructExprBitfields {
799 friend class ASTStmtReader;
800 friend class CXXUnresolvedConstructExpr;
802 unsigned : NumExprBits;
804 /// The number of arguments used to construct the type.
808 class CXXDependentScopeMemberExprBitfields {
809 friend class ASTStmtReader;
810 friend class CXXDependentScopeMemberExpr;
812 unsigned : NumExprBits;
814 /// Whether this member expression used the '->' operator or
815 /// the '.' operator.
816 unsigned IsArrow : 1;
818 /// Whether this member expression has info for explicit template
819 /// keyword and arguments.
820 unsigned HasTemplateKWAndArgsInfo : 1;
822 /// See getFirstQualifierFoundInScope() and the comment listing
823 /// the trailing objects.
824 unsigned HasFirstQualifierFoundInScope : 1;
826 /// The location of the '->' or '.' operator.
827 SourceLocation OperatorLoc;
830 class OverloadExprBitfields {
831 friend class ASTStmtReader;
832 friend class OverloadExpr;
834 unsigned : NumExprBits;
836 /// Whether the name includes info for explicit template
837 /// keyword and arguments.
838 unsigned HasTemplateKWAndArgsInfo : 1;
840 /// Padding used by the derived classes to store various bits. If you
841 /// need to add some data here, shrink this padding and add your data
842 /// above. NumOverloadExprBits also needs to be updated.
843 unsigned : 32 - NumExprBits - 1;
845 /// The number of results.
848 enum { NumOverloadExprBits = NumExprBits + 1 };
850 class UnresolvedLookupExprBitfields {
851 friend class ASTStmtReader;
852 friend class UnresolvedLookupExpr;
854 unsigned : NumOverloadExprBits;
856 /// True if these lookup results should be extended by
857 /// argument-dependent lookup if this is the operand of a function call.
858 unsigned RequiresADL : 1;
860 /// True if these lookup results are overloaded. This is pretty trivially
861 /// rederivable if we urgently need to kill this field.
862 unsigned Overloaded : 1;
864 static_assert(sizeof(UnresolvedLookupExprBitfields) <= 4,
865 "UnresolvedLookupExprBitfields must be <= than 4 bytes to"
866 "avoid trashing OverloadExprBitfields::NumResults!");
868 class UnresolvedMemberExprBitfields {
869 friend class ASTStmtReader;
870 friend class UnresolvedMemberExpr;
872 unsigned : NumOverloadExprBits;
874 /// Whether this member expression used the '->' operator or
875 /// the '.' operator.
876 unsigned IsArrow : 1;
878 /// Whether the lookup results contain an unresolved using declaration.
879 unsigned HasUnresolvedUsing : 1;
881 static_assert(sizeof(UnresolvedMemberExprBitfields) <= 4,
882 "UnresolvedMemberExprBitfields must be <= than 4 bytes to"
883 "avoid trashing OverloadExprBitfields::NumResults!");
885 class CXXNoexceptExprBitfields {
886 friend class ASTStmtReader;
887 friend class CXXNoexceptExpr;
889 unsigned : NumExprBits;
894 class SubstNonTypeTemplateParmExprBitfields {
895 friend class ASTStmtReader;
896 friend class SubstNonTypeTemplateParmExpr;
898 unsigned : NumExprBits;
900 /// The location of the non-type template parameter reference.
901 SourceLocation NameLoc;
904 //===--- C++ Coroutines TS bitfields classes ---===//
906 class CoawaitExprBitfields {
907 friend class CoawaitExpr;
909 unsigned : NumExprBits;
911 unsigned IsImplicit : 1;
914 //===--- Obj-C Expression bitfields classes ---===//
916 class ObjCIndirectCopyRestoreExprBitfields {
917 friend class ObjCIndirectCopyRestoreExpr;
919 unsigned : NumExprBits;
921 unsigned ShouldCopy : 1;
924 //===--- Clang Extensions bitfields classes ---===//
926 class OpaqueValueExprBitfields {
927 friend class ASTStmtReader;
928 friend class OpaqueValueExpr;
930 unsigned : NumExprBits;
932 /// The OVE is a unique semantic reference to its source expression if this
933 /// bit is set to true.
934 unsigned IsUnique : 1;
940 // Same order as in StmtNodes.td.
942 StmtBitfields StmtBits;
943 NullStmtBitfields NullStmtBits;
944 CompoundStmtBitfields CompoundStmtBits;
945 LabelStmtBitfields LabelStmtBits;
946 AttributedStmtBitfields AttributedStmtBits;
947 IfStmtBitfields IfStmtBits;
948 SwitchStmtBitfields SwitchStmtBits;
949 WhileStmtBitfields WhileStmtBits;
950 DoStmtBitfields DoStmtBits;
951 ForStmtBitfields ForStmtBits;
952 GotoStmtBitfields GotoStmtBits;
953 ContinueStmtBitfields ContinueStmtBits;
954 BreakStmtBitfields BreakStmtBits;
955 ReturnStmtBitfields ReturnStmtBits;
956 SwitchCaseBitfields SwitchCaseBits;
959 ExprBitfields ExprBits;
960 ConstantExprBitfields ConstantExprBits;
961 PredefinedExprBitfields PredefinedExprBits;
962 DeclRefExprBitfields DeclRefExprBits;
963 FloatingLiteralBitfields FloatingLiteralBits;
964 StringLiteralBitfields StringLiteralBits;
965 CharacterLiteralBitfields CharacterLiteralBits;
966 UnaryOperatorBitfields UnaryOperatorBits;
967 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
968 ArraySubscriptExprBitfields ArraySubscriptExprBits;
969 CallExprBitfields CallExprBits;
970 MemberExprBitfields MemberExprBits;
971 CastExprBitfields CastExprBits;
972 BinaryOperatorBitfields BinaryOperatorBits;
973 InitListExprBitfields InitListExprBits;
974 ParenListExprBitfields ParenListExprBits;
975 GenericSelectionExprBitfields GenericSelectionExprBits;
976 PseudoObjectExprBitfields PseudoObjectExprBits;
977 SourceLocExprBitfields SourceLocExprBits;
980 CXXOperatorCallExprBitfields CXXOperatorCallExprBits;
981 CXXBoolLiteralExprBitfields CXXBoolLiteralExprBits;
982 CXXNullPtrLiteralExprBitfields CXXNullPtrLiteralExprBits;
983 CXXThisExprBitfields CXXThisExprBits;
984 CXXThrowExprBitfields CXXThrowExprBits;
985 CXXDefaultArgExprBitfields CXXDefaultArgExprBits;
986 CXXDefaultInitExprBitfields CXXDefaultInitExprBits;
987 CXXScalarValueInitExprBitfields CXXScalarValueInitExprBits;
988 CXXNewExprBitfields CXXNewExprBits;
989 CXXDeleteExprBitfields CXXDeleteExprBits;
990 TypeTraitExprBitfields TypeTraitExprBits;
991 DependentScopeDeclRefExprBitfields DependentScopeDeclRefExprBits;
992 CXXConstructExprBitfields CXXConstructExprBits;
993 ExprWithCleanupsBitfields ExprWithCleanupsBits;
994 CXXUnresolvedConstructExprBitfields CXXUnresolvedConstructExprBits;
995 CXXDependentScopeMemberExprBitfields CXXDependentScopeMemberExprBits;
996 OverloadExprBitfields OverloadExprBits;
997 UnresolvedLookupExprBitfields UnresolvedLookupExprBits;
998 UnresolvedMemberExprBitfields UnresolvedMemberExprBits;
999 CXXNoexceptExprBitfields CXXNoexceptExprBits;
1000 SubstNonTypeTemplateParmExprBitfields SubstNonTypeTemplateParmExprBits;
1002 // C++ Coroutines TS expressions
1003 CoawaitExprBitfields CoawaitBits;
1005 // Obj-C Expressions
1006 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
1009 OpaqueValueExprBitfields OpaqueValueExprBits;
1013 // Only allow allocation of Stmts using the allocator in ASTContext
1014 // or by doing a placement new.
1015 void* operator new(size_t bytes, const ASTContext& C,
1016 unsigned alignment = 8);
1018 void* operator new(size_t bytes, const ASTContext* C,
1019 unsigned alignment = 8) {
1020 return operator new(bytes, *C, alignment);
1023 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
1025 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
1026 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
1027 void operator delete(void *, size_t) noexcept {}
1028 void operator delete(void *, void *) noexcept {}
1031 /// A placeholder type used to construct an empty shell of a
1032 /// type, that will be filled in later (e.g., by some
1033 /// de-serialization).
1034 struct EmptyShell {};
1037 /// Iterator for iterating over Stmt * arrays that contain only T *.
1039 /// This is needed because AST nodes use Stmt* arrays to store
1040 /// references to children (to be compatible with StmtIterator).
1041 template<typename T, typename TPtr = T *, typename StmtPtr = Stmt *>
1043 : llvm::iterator_adaptor_base<CastIterator<T, TPtr, StmtPtr>, StmtPtr *,
1044 std::random_access_iterator_tag, TPtr> {
1045 using Base = typename CastIterator::iterator_adaptor_base;
1047 CastIterator() : Base(nullptr) {}
1048 CastIterator(StmtPtr *I) : Base(I) {}
1050 typename Base::value_type operator*() const {
1051 return cast_or_null<T>(*this->I);
1055 /// Const iterator for iterating over Stmt * arrays that contain only T *.
1056 template <typename T>
1057 using ConstCastIterator = CastIterator<T, const T *const, const Stmt *const>;
1059 using ExprIterator = CastIterator<Expr>;
1060 using ConstExprIterator = ConstCastIterator<Expr>;
1063 /// Whether statistic collection is enabled.
1064 static bool StatisticsEnabled;
1067 /// Construct an empty statement.
1068 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
1072 Stmt(const Stmt &) = delete;
1073 Stmt(Stmt &&) = delete;
1074 Stmt &operator=(const Stmt &) = delete;
1075 Stmt &operator=(Stmt &&) = delete;
1077 Stmt(StmtClass SC) {
1078 static_assert(sizeof(*this) <= 8,
1079 "changing bitfields changed sizeof(Stmt)");
1080 static_assert(sizeof(*this) % alignof(void *) == 0,
1081 "Insufficient alignment!");
1082 StmtBits.sClass = SC;
1083 StmtBits.IsOMPStructuredBlock = false;
1084 if (StatisticsEnabled) Stmt::addStmtClass(SC);
1087 StmtClass getStmtClass() const {
1088 return static_cast<StmtClass>(StmtBits.sClass);
1091 const char *getStmtClassName() const;
1093 bool isOMPStructuredBlock() const { return StmtBits.IsOMPStructuredBlock; }
1094 void setIsOMPStructuredBlock(bool IsOMPStructuredBlock) {
1095 StmtBits.IsOMPStructuredBlock = IsOMPStructuredBlock;
1098 /// SourceLocation tokens are not useful in isolation - they are low level
1099 /// value objects created/interpreted by SourceManager. We assume AST
1100 /// clients will have a pointer to the respective SourceManager.
1101 SourceRange getSourceRange() const LLVM_READONLY;
1102 SourceLocation getBeginLoc() const LLVM_READONLY;
1103 SourceLocation getEndLoc() const LLVM_READONLY;
1105 // global temp stats (until we have a per-module visitor)
1106 static void addStmtClass(const StmtClass s);
1107 static void EnableStatistics();
1108 static void PrintStats();
1110 /// Dumps the specified AST fragment and all subtrees to
1111 /// \c llvm::errs().
1113 void dump(SourceManager &SM) const;
1114 void dump(raw_ostream &OS, SourceManager &SM) const;
1115 void dump(raw_ostream &OS) const;
1117 /// \return Unique reproducible object identifier
1118 int64_t getID(const ASTContext &Context) const;
1120 /// dumpColor - same as dump(), but forces color highlighting.
1121 void dumpColor() const;
1123 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
1124 /// back to its original source language syntax.
1125 void dumpPretty(const ASTContext &Context) const;
1126 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
1127 const PrintingPolicy &Policy, unsigned Indentation = 0,
1128 StringRef NewlineSymbol = "\n",
1129 const ASTContext *Context = nullptr) const;
1131 /// Pretty-prints in JSON format.
1132 void printJson(raw_ostream &Out, PrinterHelper *Helper,
1133 const PrintingPolicy &Policy, bool AddQuotes) const;
1135 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
1136 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
1137 void viewAST() const;
1139 /// Skip no-op (attributed, compound) container stmts and skip captured
1140 /// stmt at the top, if \a IgnoreCaptured is true.
1141 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
1142 const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
1143 return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
1146 const Stmt *stripLabelLikeStatements() const;
1147 Stmt *stripLabelLikeStatements() {
1148 return const_cast<Stmt*>(
1149 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
1152 /// Child Iterators: All subclasses must implement 'children'
1153 /// to permit easy iteration over the substatements/subexpessions of an
1154 /// AST node. This permits easy iteration over all nodes in the AST.
1155 using child_iterator = StmtIterator;
1156 using const_child_iterator = ConstStmtIterator;
1158 using child_range = llvm::iterator_range<child_iterator>;
1159 using const_child_range = llvm::iterator_range<const_child_iterator>;
1161 child_range children();
1163 const_child_range children() const {
1164 auto Children = const_cast<Stmt *>(this)->children();
1165 return const_child_range(Children.begin(), Children.end());
1168 child_iterator child_begin() { return children().begin(); }
1169 child_iterator child_end() { return children().end(); }
1171 const_child_iterator child_begin() const { return children().begin(); }
1172 const_child_iterator child_end() const { return children().end(); }
1174 /// Produce a unique representation of the given statement.
1176 /// \param ID once the profiling operation is complete, will contain
1177 /// the unique representation of the given statement.
1179 /// \param Context the AST context in which the statement resides
1181 /// \param Canonical whether the profile should be based on the canonical
1182 /// representation of this statement (e.g., where non-type template
1183 /// parameters are identified by index/level rather than their
1184 /// declaration pointers) or the exact representation of the statement as
1185 /// written in the source.
1186 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
1187 bool Canonical) const;
1189 /// Calculate a unique representation for a statement that is
1190 /// stable across compiler invocations.
1192 /// \param ID profile information will be stored in ID.
1194 /// \param Hash an ODRHash object which will be called where pointers would
1195 /// have been used in the Profile function.
1196 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
1199 /// DeclStmt - Adaptor class for mixing declarations with statements and
1200 /// expressions. For example, CompoundStmt mixes statements, expressions
1201 /// and declarations (variables, types). Another example is ForStmt, where
1202 /// the first statement can be an expression or a declaration.
1203 class DeclStmt : public Stmt {
1205 SourceLocation StartLoc, EndLoc;
1208 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
1209 : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
1211 /// Build an empty declaration statement.
1212 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
1214 /// isSingleDecl - This method returns true if this DeclStmt refers
1215 /// to a single Decl.
1216 bool isSingleDecl() const { return DG.isSingleDecl(); }
1218 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
1219 Decl *getSingleDecl() { return DG.getSingleDecl(); }
1221 const DeclGroupRef getDeclGroup() const { return DG; }
1222 DeclGroupRef getDeclGroup() { return DG; }
1223 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
1225 void setStartLoc(SourceLocation L) { StartLoc = L; }
1226 SourceLocation getEndLoc() const { return EndLoc; }
1227 void setEndLoc(SourceLocation L) { EndLoc = L; }
1229 SourceLocation getBeginLoc() const LLVM_READONLY { return StartLoc; }
1231 static bool classof(const Stmt *T) {
1232 return T->getStmtClass() == DeclStmtClass;
1235 // Iterators over subexpressions.
1236 child_range children() {
1237 return child_range(child_iterator(DG.begin(), DG.end()),
1238 child_iterator(DG.end(), DG.end()));
1241 const_child_range children() const {
1242 auto Children = const_cast<DeclStmt *>(this)->children();
1243 return const_child_range(Children);
1246 using decl_iterator = DeclGroupRef::iterator;
1247 using const_decl_iterator = DeclGroupRef::const_iterator;
1248 using decl_range = llvm::iterator_range<decl_iterator>;
1249 using decl_const_range = llvm::iterator_range<const_decl_iterator>;
1251 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
1253 decl_const_range decls() const {
1254 return decl_const_range(decl_begin(), decl_end());
1257 decl_iterator decl_begin() { return DG.begin(); }
1258 decl_iterator decl_end() { return DG.end(); }
1259 const_decl_iterator decl_begin() const { return DG.begin(); }
1260 const_decl_iterator decl_end() const { return DG.end(); }
1262 using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
1264 reverse_decl_iterator decl_rbegin() {
1265 return reverse_decl_iterator(decl_end());
1268 reverse_decl_iterator decl_rend() {
1269 return reverse_decl_iterator(decl_begin());
1273 /// NullStmt - This is the null statement ";": C99 6.8.3p3.
1275 class NullStmt : public Stmt {
1277 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
1278 : Stmt(NullStmtClass) {
1279 NullStmtBits.HasLeadingEmptyMacro = hasLeadingEmptyMacro;
1283 /// Build an empty null statement.
1284 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
1286 SourceLocation getSemiLoc() const { return NullStmtBits.SemiLoc; }
1287 void setSemiLoc(SourceLocation L) { NullStmtBits.SemiLoc = L; }
1289 bool hasLeadingEmptyMacro() const {
1290 return NullStmtBits.HasLeadingEmptyMacro;
1293 SourceLocation getBeginLoc() const { return getSemiLoc(); }
1294 SourceLocation getEndLoc() const { return getSemiLoc(); }
1296 static bool classof(const Stmt *T) {
1297 return T->getStmtClass() == NullStmtClass;
1300 child_range children() {
1301 return child_range(child_iterator(), child_iterator());
1304 const_child_range children() const {
1305 return const_child_range(const_child_iterator(), const_child_iterator());
1309 /// CompoundStmt - This represents a group of statements like { stmt stmt }.
1310 class CompoundStmt final : public Stmt,
1311 private llvm::TrailingObjects<CompoundStmt, Stmt *> {
1312 friend class ASTStmtReader;
1313 friend TrailingObjects;
1315 /// The location of the closing "}". LBraceLoc is stored in CompoundStmtBits.
1316 SourceLocation RBraceLoc;
1318 CompoundStmt(ArrayRef<Stmt *> Stmts, SourceLocation LB, SourceLocation RB);
1319 explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {}
1321 void setStmts(ArrayRef<Stmt *> Stmts);
1324 static CompoundStmt *Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
1325 SourceLocation LB, SourceLocation RB);
1327 // Build an empty compound statement with a location.
1328 explicit CompoundStmt(SourceLocation Loc)
1329 : Stmt(CompoundStmtClass), RBraceLoc(Loc) {
1330 CompoundStmtBits.NumStmts = 0;
1331 CompoundStmtBits.LBraceLoc = Loc;
1334 // Build an empty compound statement.
1335 static CompoundStmt *CreateEmpty(const ASTContext &C, unsigned NumStmts);
1337 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
1338 unsigned size() const { return CompoundStmtBits.NumStmts; }
1340 using body_iterator = Stmt **;
1341 using body_range = llvm::iterator_range<body_iterator>;
1343 body_range body() { return body_range(body_begin(), body_end()); }
1344 body_iterator body_begin() { return getTrailingObjects<Stmt *>(); }
1345 body_iterator body_end() { return body_begin() + size(); }
1346 Stmt *body_front() { return !body_empty() ? body_begin()[0] : nullptr; }
1349 return !body_empty() ? body_begin()[size() - 1] : nullptr;
1352 using const_body_iterator = Stmt *const *;
1353 using body_const_range = llvm::iterator_range<const_body_iterator>;
1355 body_const_range body() const {
1356 return body_const_range(body_begin(), body_end());
1359 const_body_iterator body_begin() const {
1360 return getTrailingObjects<Stmt *>();
1363 const_body_iterator body_end() const { return body_begin() + size(); }
1365 const Stmt *body_front() const {
1366 return !body_empty() ? body_begin()[0] : nullptr;
1369 const Stmt *body_back() const {
1370 return !body_empty() ? body_begin()[size() - 1] : nullptr;
1373 using reverse_body_iterator = std::reverse_iterator<body_iterator>;
1375 reverse_body_iterator body_rbegin() {
1376 return reverse_body_iterator(body_end());
1379 reverse_body_iterator body_rend() {
1380 return reverse_body_iterator(body_begin());
1383 using const_reverse_body_iterator =
1384 std::reverse_iterator<const_body_iterator>;
1386 const_reverse_body_iterator body_rbegin() const {
1387 return const_reverse_body_iterator(body_end());
1390 const_reverse_body_iterator body_rend() const {
1391 return const_reverse_body_iterator(body_begin());
1394 // Get the Stmt that StmtExpr would consider to be the result of this
1395 // compound statement. This is used by StmtExpr to properly emulate the GCC
1396 // compound expression extension, which ignores trailing NullStmts when
1397 // getting the result of the expression.
1400 // If we don't find something that isn't a NullStmt, just return the last
1402 Stmt *getStmtExprResult() {
1403 for (auto *B : llvm::reverse(body())) {
1404 if (!isa<NullStmt>(B))
1410 const Stmt *getStmtExprResult() const {
1411 return const_cast<CompoundStmt *>(this)->getStmtExprResult();
1414 SourceLocation getBeginLoc() const { return CompoundStmtBits.LBraceLoc; }
1415 SourceLocation getEndLoc() const { return RBraceLoc; }
1417 SourceLocation getLBracLoc() const { return CompoundStmtBits.LBraceLoc; }
1418 SourceLocation getRBracLoc() const { return RBraceLoc; }
1420 static bool classof(const Stmt *T) {
1421 return T->getStmtClass() == CompoundStmtClass;
1425 child_range children() { return child_range(body_begin(), body_end()); }
1427 const_child_range children() const {
1428 return const_child_range(body_begin(), body_end());
1432 // SwitchCase is the base class for CaseStmt and DefaultStmt,
1433 class SwitchCase : public Stmt {
1435 /// The location of the ":".
1436 SourceLocation ColonLoc;
1438 // The location of the "case" or "default" keyword. Stored in SwitchCaseBits.
1439 // SourceLocation KeywordLoc;
1441 /// A pointer to the following CaseStmt or DefaultStmt class,
1442 /// used by SwitchStmt.
1443 SwitchCase *NextSwitchCase = nullptr;
1445 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
1446 : Stmt(SC), ColonLoc(ColonLoc) {
1447 setKeywordLoc(KWLoc);
1450 SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
1453 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
1454 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
1455 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
1457 SourceLocation getKeywordLoc() const { return SwitchCaseBits.KeywordLoc; }
1458 void setKeywordLoc(SourceLocation L) { SwitchCaseBits.KeywordLoc = L; }
1459 SourceLocation getColonLoc() const { return ColonLoc; }
1460 void setColonLoc(SourceLocation L) { ColonLoc = L; }
1462 inline Stmt *getSubStmt();
1463 const Stmt *getSubStmt() const {
1464 return const_cast<SwitchCase *>(this)->getSubStmt();
1467 SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1468 inline SourceLocation getEndLoc() const LLVM_READONLY;
1470 static bool classof(const Stmt *T) {
1471 return T->getStmtClass() == CaseStmtClass ||
1472 T->getStmtClass() == DefaultStmtClass;
1476 /// CaseStmt - Represent a case statement. It can optionally be a GNU case
1477 /// statement of the form LHS ... RHS representing a range of cases.
1478 class CaseStmt final
1479 : public SwitchCase,
1480 private llvm::TrailingObjects<CaseStmt, Stmt *, SourceLocation> {
1481 friend TrailingObjects;
1483 // CaseStmt is followed by several trailing objects, some of which optional.
1484 // Note that it would be more convenient to put the optional trailing objects
1485 // at the end but this would impact children().
1486 // The trailing objects are in order:
1488 // * A "Stmt *" for the LHS of the case statement. Always present.
1490 // * A "Stmt *" for the RHS of the case statement. This is a GNU extension
1491 // which allow ranges in cases statement of the form LHS ... RHS.
1492 // Present if and only if caseStmtIsGNURange() is true.
1494 // * A "Stmt *" for the substatement of the case statement. Always present.
1496 // * A SourceLocation for the location of the ... if this is a case statement
1497 // with a range. Present if and only if caseStmtIsGNURange() is true.
1498 enum { LhsOffset = 0, SubStmtOffsetFromRhs = 1 };
1499 enum { NumMandatoryStmtPtr = 2 };
1501 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1502 return NumMandatoryStmtPtr + caseStmtIsGNURange();
1505 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
1506 return caseStmtIsGNURange();
1509 unsigned lhsOffset() const { return LhsOffset; }
1510 unsigned rhsOffset() const { return LhsOffset + caseStmtIsGNURange(); }
1511 unsigned subStmtOffset() const { return rhsOffset() + SubStmtOffsetFromRhs; }
1513 /// Build a case statement assuming that the storage for the
1514 /// trailing objects has been properly allocated.
1515 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
1516 SourceLocation ellipsisLoc, SourceLocation colonLoc)
1517 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
1518 // Handle GNU case statements of the form LHS ... RHS.
1519 bool IsGNURange = rhs != nullptr;
1520 SwitchCaseBits.CaseStmtIsGNURange = IsGNURange;
1522 setSubStmt(nullptr);
1525 setEllipsisLoc(ellipsisLoc);
1529 /// Build an empty switch case statement.
1530 explicit CaseStmt(EmptyShell Empty, bool CaseStmtIsGNURange)
1531 : SwitchCase(CaseStmtClass, Empty) {
1532 SwitchCaseBits.CaseStmtIsGNURange = CaseStmtIsGNURange;
1536 /// Build a case statement.
1537 static CaseStmt *Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1538 SourceLocation caseLoc, SourceLocation ellipsisLoc,
1539 SourceLocation colonLoc);
1541 /// Build an empty case statement.
1542 static CaseStmt *CreateEmpty(const ASTContext &Ctx, bool CaseStmtIsGNURange);
1544 /// True if this case statement is of the form case LHS ... RHS, which
1545 /// is a GNU extension. In this case the RHS can be obtained with getRHS()
1546 /// and the location of the ellipsis can be obtained with getEllipsisLoc().
1547 bool caseStmtIsGNURange() const { return SwitchCaseBits.CaseStmtIsGNURange; }
1549 SourceLocation getCaseLoc() const { return getKeywordLoc(); }
1550 void setCaseLoc(SourceLocation L) { setKeywordLoc(L); }
1552 /// Get the location of the ... in a case statement of the form LHS ... RHS.
1553 SourceLocation getEllipsisLoc() const {
1554 return caseStmtIsGNURange() ? *getTrailingObjects<SourceLocation>()
1558 /// Set the location of the ... in a case statement of the form LHS ... RHS.
1559 /// Assert that this case statement is of this form.
1560 void setEllipsisLoc(SourceLocation L) {
1562 caseStmtIsGNURange() &&
1563 "setEllipsisLoc but this is not a case stmt of the form LHS ... RHS!");
1564 *getTrailingObjects<SourceLocation>() = L;
1568 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
1571 const Expr *getLHS() const {
1572 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[lhsOffset()]);
1575 void setLHS(Expr *Val) {
1576 getTrailingObjects<Stmt *>()[lhsOffset()] = reinterpret_cast<Stmt *>(Val);
1580 return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
1581 getTrailingObjects<Stmt *>()[rhsOffset()])
1585 const Expr *getRHS() const {
1586 return caseStmtIsGNURange() ? reinterpret_cast<Expr *>(
1587 getTrailingObjects<Stmt *>()[rhsOffset()])
1591 void setRHS(Expr *Val) {
1592 assert(caseStmtIsGNURange() &&
1593 "setRHS but this is not a case stmt of the form LHS ... RHS!");
1594 getTrailingObjects<Stmt *>()[rhsOffset()] = reinterpret_cast<Stmt *>(Val);
1597 Stmt *getSubStmt() { return getTrailingObjects<Stmt *>()[subStmtOffset()]; }
1598 const Stmt *getSubStmt() const {
1599 return getTrailingObjects<Stmt *>()[subStmtOffset()];
1602 void setSubStmt(Stmt *S) {
1603 getTrailingObjects<Stmt *>()[subStmtOffset()] = S;
1606 SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1607 SourceLocation getEndLoc() const LLVM_READONLY {
1608 // Handle deeply nested case statements with iteration instead of recursion.
1609 const CaseStmt *CS = this;
1610 while (const auto *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
1613 return CS->getSubStmt()->getEndLoc();
1616 static bool classof(const Stmt *T) {
1617 return T->getStmtClass() == CaseStmtClass;
1621 child_range children() {
1622 return child_range(getTrailingObjects<Stmt *>(),
1623 getTrailingObjects<Stmt *>() +
1624 numTrailingObjects(OverloadToken<Stmt *>()));
1627 const_child_range children() const {
1628 return const_child_range(getTrailingObjects<Stmt *>(),
1629 getTrailingObjects<Stmt *>() +
1630 numTrailingObjects(OverloadToken<Stmt *>()));
1634 class DefaultStmt : public SwitchCase {
1638 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt)
1639 : SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
1641 /// Build an empty default statement.
1642 explicit DefaultStmt(EmptyShell Empty)
1643 : SwitchCase(DefaultStmtClass, Empty) {}
1645 Stmt *getSubStmt() { return SubStmt; }
1646 const Stmt *getSubStmt() const { return SubStmt; }
1647 void setSubStmt(Stmt *S) { SubStmt = S; }
1649 SourceLocation getDefaultLoc() const { return getKeywordLoc(); }
1650 void setDefaultLoc(SourceLocation L) { setKeywordLoc(L); }
1652 SourceLocation getBeginLoc() const { return getKeywordLoc(); }
1653 SourceLocation getEndLoc() const LLVM_READONLY {
1654 return SubStmt->getEndLoc();
1657 static bool classof(const Stmt *T) {
1658 return T->getStmtClass() == DefaultStmtClass;
1662 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1664 const_child_range children() const {
1665 return const_child_range(&SubStmt, &SubStmt + 1);
1669 SourceLocation SwitchCase::getEndLoc() const {
1670 if (const auto *CS = dyn_cast<CaseStmt>(this))
1671 return CS->getEndLoc();
1672 else if (const auto *DS = dyn_cast<DefaultStmt>(this))
1673 return DS->getEndLoc();
1674 llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
1677 Stmt *SwitchCase::getSubStmt() {
1678 if (auto *CS = dyn_cast<CaseStmt>(this))
1679 return CS->getSubStmt();
1680 else if (auto *DS = dyn_cast<DefaultStmt>(this))
1681 return DS->getSubStmt();
1682 llvm_unreachable("SwitchCase is neither a CaseStmt nor a DefaultStmt!");
1685 /// Represents a statement that could possibly have a value and type. This
1686 /// covers expression-statements, as well as labels and attributed statements.
1688 /// Value statements have a special meaning when they are the last non-null
1689 /// statement in a GNU statement expression, where they determine the value
1690 /// of the statement expression.
1691 class ValueStmt : public Stmt {
1696 const Expr *getExprStmt() const;
1697 Expr *getExprStmt() {
1698 const ValueStmt *ConstThis = this;
1699 return const_cast<Expr*>(ConstThis->getExprStmt());
1702 static bool classof(const Stmt *T) {
1703 return T->getStmtClass() >= firstValueStmtConstant &&
1704 T->getStmtClass() <= lastValueStmtConstant;
1708 /// LabelStmt - Represents a label, which has a substatement. For example:
1710 class LabelStmt : public ValueStmt {
1715 /// Build a label statement.
1716 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
1717 : ValueStmt(LabelStmtClass), TheDecl(D), SubStmt(substmt) {
1721 /// Build an empty label statement.
1722 explicit LabelStmt(EmptyShell Empty) : ValueStmt(LabelStmtClass, Empty) {}
1724 SourceLocation getIdentLoc() const { return LabelStmtBits.IdentLoc; }
1725 void setIdentLoc(SourceLocation L) { LabelStmtBits.IdentLoc = L; }
1727 LabelDecl *getDecl() const { return TheDecl; }
1728 void setDecl(LabelDecl *D) { TheDecl = D; }
1730 const char *getName() const;
1731 Stmt *getSubStmt() { return SubStmt; }
1733 const Stmt *getSubStmt() const { return SubStmt; }
1734 void setSubStmt(Stmt *SS) { SubStmt = SS; }
1736 SourceLocation getBeginLoc() const { return getIdentLoc(); }
1737 SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
1739 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1741 const_child_range children() const {
1742 return const_child_range(&SubStmt, &SubStmt + 1);
1745 static bool classof(const Stmt *T) {
1746 return T->getStmtClass() == LabelStmtClass;
1750 /// Represents an attribute applied to a statement.
1752 /// Represents an attribute applied to a statement. For example:
1753 /// [[omp::for(...)]] for (...) { ... }
1754 class AttributedStmt final
1756 private llvm::TrailingObjects<AttributedStmt, const Attr *> {
1757 friend class ASTStmtReader;
1758 friend TrailingObjects;
1762 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr *> Attrs,
1764 : ValueStmt(AttributedStmtClass), SubStmt(SubStmt) {
1765 AttributedStmtBits.NumAttrs = Attrs.size();
1766 AttributedStmtBits.AttrLoc = Loc;
1767 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
1770 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
1771 : ValueStmt(AttributedStmtClass, Empty) {
1772 AttributedStmtBits.NumAttrs = NumAttrs;
1773 AttributedStmtBits.AttrLoc = SourceLocation{};
1774 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
1777 const Attr *const *getAttrArrayPtr() const {
1778 return getTrailingObjects<const Attr *>();
1780 const Attr **getAttrArrayPtr() { return getTrailingObjects<const Attr *>(); }
1783 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
1784 ArrayRef<const Attr *> Attrs, Stmt *SubStmt);
1786 // Build an empty attributed statement.
1787 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
1789 SourceLocation getAttrLoc() const { return AttributedStmtBits.AttrLoc; }
1790 ArrayRef<const Attr *> getAttrs() const {
1791 return llvm::makeArrayRef(getAttrArrayPtr(), AttributedStmtBits.NumAttrs);
1794 Stmt *getSubStmt() { return SubStmt; }
1795 const Stmt *getSubStmt() const { return SubStmt; }
1797 SourceLocation getBeginLoc() const { return getAttrLoc(); }
1798 SourceLocation getEndLoc() const LLVM_READONLY { return SubStmt->getEndLoc();}
1800 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
1802 const_child_range children() const {
1803 return const_child_range(&SubStmt, &SubStmt + 1);
1806 static bool classof(const Stmt *T) {
1807 return T->getStmtClass() == AttributedStmtClass;
1811 /// IfStmt - This represents an if/then/else.
1814 private llvm::TrailingObjects<IfStmt, Stmt *, SourceLocation> {
1815 friend TrailingObjects;
1817 // IfStmt is followed by several trailing objects, some of which optional.
1818 // Note that it would be more convenient to put the optional trailing
1819 // objects at then end but this would change the order of the children.
1820 // The trailing objects are in order:
1822 // * A "Stmt *" for the init statement.
1823 // Present if and only if hasInitStorage().
1825 // * A "Stmt *" for the condition variable.
1826 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
1828 // * A "Stmt *" for the condition.
1829 // Always present. This is in fact a "Expr *".
1831 // * A "Stmt *" for the then statement.
1834 // * A "Stmt *" for the else statement.
1835 // Present if and only if hasElseStorage().
1837 // * A "SourceLocation" for the location of the "else".
1838 // Present if and only if hasElseStorage().
1839 enum { InitOffset = 0, ThenOffsetFromCond = 1, ElseOffsetFromCond = 2 };
1840 enum { NumMandatoryStmtPtr = 2 };
1842 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
1843 return NumMandatoryStmtPtr + hasElseStorage() + hasVarStorage() +
1847 unsigned numTrailingObjects(OverloadToken<SourceLocation>) const {
1848 return hasElseStorage();
1851 unsigned initOffset() const { return InitOffset; }
1852 unsigned varOffset() const { return InitOffset + hasInitStorage(); }
1853 unsigned condOffset() const {
1854 return InitOffset + hasInitStorage() + hasVarStorage();
1856 unsigned thenOffset() const { return condOffset() + ThenOffsetFromCond; }
1857 unsigned elseOffset() const { return condOffset() + ElseOffsetFromCond; }
1859 /// Build an if/then/else statement.
1860 IfStmt(const ASTContext &Ctx, SourceLocation IL, bool IsConstexpr, Stmt *Init,
1861 VarDecl *Var, Expr *Cond, Stmt *Then, SourceLocation EL, Stmt *Else);
1863 /// Build an empty if/then/else statement.
1864 explicit IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit);
1867 /// Create an IfStmt.
1868 static IfStmt *Create(const ASTContext &Ctx, SourceLocation IL,
1869 bool IsConstexpr, Stmt *Init, VarDecl *Var, Expr *Cond,
1870 Stmt *Then, SourceLocation EL = SourceLocation(),
1871 Stmt *Else = nullptr);
1873 /// Create an empty IfStmt optionally with storage for an else statement,
1874 /// condition variable and init expression.
1875 static IfStmt *CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
1878 /// True if this IfStmt has the storage for an init statement.
1879 bool hasInitStorage() const { return IfStmtBits.HasInit; }
1881 /// True if this IfStmt has storage for a variable declaration.
1882 bool hasVarStorage() const { return IfStmtBits.HasVar; }
1884 /// True if this IfStmt has storage for an else statement.
1885 bool hasElseStorage() const { return IfStmtBits.HasElse; }
1888 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1891 const Expr *getCond() const {
1892 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
1895 void setCond(Expr *Cond) {
1896 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
1899 Stmt *getThen() { return getTrailingObjects<Stmt *>()[thenOffset()]; }
1900 const Stmt *getThen() const {
1901 return getTrailingObjects<Stmt *>()[thenOffset()];
1904 void setThen(Stmt *Then) {
1905 getTrailingObjects<Stmt *>()[thenOffset()] = Then;
1909 return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()]
1913 const Stmt *getElse() const {
1914 return hasElseStorage() ? getTrailingObjects<Stmt *>()[elseOffset()]
1918 void setElse(Stmt *Else) {
1919 assert(hasElseStorage() &&
1920 "This if statement has no storage for an else statement!");
1921 getTrailingObjects<Stmt *>()[elseOffset()] = Else;
1924 /// Retrieve the variable declared in this "if" statement, if any.
1926 /// In the following example, "x" is the condition variable.
1928 /// if (int x = foo()) {
1929 /// printf("x is %d", x);
1932 VarDecl *getConditionVariable();
1933 const VarDecl *getConditionVariable() const {
1934 return const_cast<IfStmt *>(this)->getConditionVariable();
1937 /// Set the condition variable for this if statement.
1938 /// The if statement must have storage for the condition variable.
1939 void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
1941 /// If this IfStmt has a condition variable, return the faux DeclStmt
1942 /// associated with the creation of that condition variable.
1943 DeclStmt *getConditionVariableDeclStmt() {
1944 return hasVarStorage() ? static_cast<DeclStmt *>(
1945 getTrailingObjects<Stmt *>()[varOffset()])
1949 const DeclStmt *getConditionVariableDeclStmt() const {
1950 return hasVarStorage() ? static_cast<DeclStmt *>(
1951 getTrailingObjects<Stmt *>()[varOffset()])
1956 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
1960 const Stmt *getInit() const {
1961 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
1965 void setInit(Stmt *Init) {
1966 assert(hasInitStorage() &&
1967 "This if statement has no storage for an init statement!");
1968 getTrailingObjects<Stmt *>()[initOffset()] = Init;
1971 SourceLocation getIfLoc() const { return IfStmtBits.IfLoc; }
1972 void setIfLoc(SourceLocation IfLoc) { IfStmtBits.IfLoc = IfLoc; }
1974 SourceLocation getElseLoc() const {
1975 return hasElseStorage() ? *getTrailingObjects<SourceLocation>()
1979 void setElseLoc(SourceLocation ElseLoc) {
1980 assert(hasElseStorage() &&
1981 "This if statement has no storage for an else statement!");
1982 *getTrailingObjects<SourceLocation>() = ElseLoc;
1985 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
1986 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
1988 bool isObjCAvailabilityCheck() const;
1990 SourceLocation getBeginLoc() const { return getIfLoc(); }
1991 SourceLocation getEndLoc() const LLVM_READONLY {
1993 return getElse()->getEndLoc();
1994 return getThen()->getEndLoc();
1997 // Iterators over subexpressions. The iterators will include iterating
1998 // over the initialization expression referenced by the condition variable.
1999 child_range children() {
2000 return child_range(getTrailingObjects<Stmt *>(),
2001 getTrailingObjects<Stmt *>() +
2002 numTrailingObjects(OverloadToken<Stmt *>()));
2005 const_child_range children() const {
2006 return const_child_range(getTrailingObjects<Stmt *>(),
2007 getTrailingObjects<Stmt *>() +
2008 numTrailingObjects(OverloadToken<Stmt *>()));
2011 static bool classof(const Stmt *T) {
2012 return T->getStmtClass() == IfStmtClass;
2016 /// SwitchStmt - This represents a 'switch' stmt.
2017 class SwitchStmt final : public Stmt,
2018 private llvm::TrailingObjects<SwitchStmt, Stmt *> {
2019 friend TrailingObjects;
2021 /// Points to a linked list of case and default statements.
2022 SwitchCase *FirstCase;
2024 // SwitchStmt is followed by several trailing objects,
2025 // some of which optional. Note that it would be more convenient to
2026 // put the optional trailing objects at the end but this would change
2027 // the order in children().
2028 // The trailing objects are in order:
2030 // * A "Stmt *" for the init statement.
2031 // Present if and only if hasInitStorage().
2033 // * A "Stmt *" for the condition variable.
2034 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
2036 // * A "Stmt *" for the condition.
2037 // Always present. This is in fact an "Expr *".
2039 // * A "Stmt *" for the body.
2041 enum { InitOffset = 0, BodyOffsetFromCond = 1 };
2042 enum { NumMandatoryStmtPtr = 2 };
2044 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
2045 return NumMandatoryStmtPtr + hasInitStorage() + hasVarStorage();
2048 unsigned initOffset() const { return InitOffset; }
2049 unsigned varOffset() const { return InitOffset + hasInitStorage(); }
2050 unsigned condOffset() const {
2051 return InitOffset + hasInitStorage() + hasVarStorage();
2053 unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
2055 /// Build a switch statement.
2056 SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var, Expr *Cond);
2058 /// Build a empty switch statement.
2059 explicit SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar);
2062 /// Create a switch statement.
2063 static SwitchStmt *Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
2066 /// Create an empty switch statement optionally with storage for
2067 /// an init expression and a condition variable.
2068 static SwitchStmt *CreateEmpty(const ASTContext &Ctx, bool HasInit,
2071 /// True if this SwitchStmt has storage for an init statement.
2072 bool hasInitStorage() const { return SwitchStmtBits.HasInit; }
2074 /// True if this SwitchStmt has storage for a condition variable.
2075 bool hasVarStorage() const { return SwitchStmtBits.HasVar; }
2078 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2081 const Expr *getCond() const {
2082 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2085 void setCond(Expr *Cond) {
2086 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
2089 Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
2090 const Stmt *getBody() const {
2091 return getTrailingObjects<Stmt *>()[bodyOffset()];
2094 void setBody(Stmt *Body) {
2095 getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
2099 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
2103 const Stmt *getInit() const {
2104 return hasInitStorage() ? getTrailingObjects<Stmt *>()[initOffset()]
2108 void setInit(Stmt *Init) {
2109 assert(hasInitStorage() &&
2110 "This switch statement has no storage for an init statement!");
2111 getTrailingObjects<Stmt *>()[initOffset()] = Init;
2114 /// Retrieve the variable declared in this "switch" statement, if any.
2116 /// In the following example, "x" is the condition variable.
2118 /// switch (int x = foo()) {
2123 VarDecl *getConditionVariable();
2124 const VarDecl *getConditionVariable() const {
2125 return const_cast<SwitchStmt *>(this)->getConditionVariable();
2128 /// Set the condition variable in this switch statement.
2129 /// The switch statement must have storage for it.
2130 void setConditionVariable(const ASTContext &Ctx, VarDecl *VD);
2132 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
2133 /// associated with the creation of that condition variable.
2134 DeclStmt *getConditionVariableDeclStmt() {
2135 return hasVarStorage() ? static_cast<DeclStmt *>(
2136 getTrailingObjects<Stmt *>()[varOffset()])
2140 const DeclStmt *getConditionVariableDeclStmt() const {
2141 return hasVarStorage() ? static_cast<DeclStmt *>(
2142 getTrailingObjects<Stmt *>()[varOffset()])
2146 SwitchCase *getSwitchCaseList() { return FirstCase; }
2147 const SwitchCase *getSwitchCaseList() const { return FirstCase; }
2148 void setSwitchCaseList(SwitchCase *SC) { FirstCase = SC; }
2150 SourceLocation getSwitchLoc() const { return SwitchStmtBits.SwitchLoc; }
2151 void setSwitchLoc(SourceLocation L) { SwitchStmtBits.SwitchLoc = L; }
2153 void setBody(Stmt *S, SourceLocation SL) {
2158 void addSwitchCase(SwitchCase *SC) {
2159 assert(!SC->getNextSwitchCase() &&
2160 "case/default already added to a switch");
2161 SC->setNextSwitchCase(FirstCase);
2165 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
2166 /// switch over an enum value then all cases have been explicitly covered.
2167 void setAllEnumCasesCovered() { SwitchStmtBits.AllEnumCasesCovered = true; }
2169 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
2170 /// have been explicitly covered.
2171 bool isAllEnumCasesCovered() const {
2172 return SwitchStmtBits.AllEnumCasesCovered;
2175 SourceLocation getBeginLoc() const { return getSwitchLoc(); }
2176 SourceLocation getEndLoc() const LLVM_READONLY {
2177 return getBody() ? getBody()->getEndLoc()
2178 : reinterpret_cast<const Stmt *>(getCond())->getEndLoc();
2182 child_range children() {
2183 return child_range(getTrailingObjects<Stmt *>(),
2184 getTrailingObjects<Stmt *>() +
2185 numTrailingObjects(OverloadToken<Stmt *>()));
2188 const_child_range children() const {
2189 return const_child_range(getTrailingObjects<Stmt *>(),
2190 getTrailingObjects<Stmt *>() +
2191 numTrailingObjects(OverloadToken<Stmt *>()));
2194 static bool classof(const Stmt *T) {
2195 return T->getStmtClass() == SwitchStmtClass;
2199 /// WhileStmt - This represents a 'while' stmt.
2200 class WhileStmt final : public Stmt,
2201 private llvm::TrailingObjects<WhileStmt, Stmt *> {
2202 friend TrailingObjects;
2204 // WhileStmt is followed by several trailing objects,
2205 // some of which optional. Note that it would be more
2206 // convenient to put the optional trailing object at the end
2207 // but this would affect children().
2208 // The trailing objects are in order:
2210 // * A "Stmt *" for the condition variable.
2211 // Present if and only if hasVarStorage(). This is in fact a "DeclStmt *".
2213 // * A "Stmt *" for the condition.
2214 // Always present. This is in fact an "Expr *".
2216 // * A "Stmt *" for the body.
2219 enum { VarOffset = 0, BodyOffsetFromCond = 1 };
2220 enum { NumMandatoryStmtPtr = 2 };
2222 unsigned varOffset() const { return VarOffset; }
2223 unsigned condOffset() const { return VarOffset + hasVarStorage(); }
2224 unsigned bodyOffset() const { return condOffset() + BodyOffsetFromCond; }
2226 unsigned numTrailingObjects(OverloadToken<Stmt *>) const {
2227 return NumMandatoryStmtPtr + hasVarStorage();
2230 /// Build a while statement.
2231 WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond, Stmt *Body,
2234 /// Build an empty while statement.
2235 explicit WhileStmt(EmptyShell Empty, bool HasVar);
2238 /// Create a while statement.
2239 static WhileStmt *Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
2240 Stmt *Body, SourceLocation WL);
2242 /// Create an empty while statement optionally with storage for
2243 /// a condition variable.
2244 static WhileStmt *CreateEmpty(const ASTContext &Ctx, bool HasVar);
2246 /// True if this WhileStmt has storage for a condition variable.
2247 bool hasVarStorage() const { return WhileStmtBits.HasVar; }
2250 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2253 const Expr *getCond() const {
2254 return reinterpret_cast<Expr *>(getTrailingObjects<Stmt *>()[condOffset()]);
2257 void setCond(Expr *Cond) {
2258 getTrailingObjects<Stmt *>()[condOffset()] = reinterpret_cast<Stmt *>(Cond);
2261 Stmt *getBody() { return getTrailingObjects<Stmt *>()[bodyOffset()]; }
2262 const Stmt *getBody() const {
2263 return getTrailingObjects<Stmt *>()[bodyOffset()];
2266 void setBody(Stmt *Body) {
2267 getTrailingObjects<Stmt *>()[bodyOffset()] = Body;
2270 /// Retrieve the variable declared in this "while" statement, if any.
2272 /// In the following example, "x" is the condition variable.
2274 /// while (int x = random()) {
2278 VarDecl *getConditionVariable();
2279 const VarDecl *getConditionVariable() const {
2280 return const_cast<WhileStmt *>(this)->getConditionVariable();
2283 /// Set the condition variable of this while statement.
2284 /// The while statement must have storage for it.
2285 void setConditionVariable(const ASTContext &Ctx, VarDecl *V);
2287 /// If this WhileStmt has a condition variable, return the faux DeclStmt
2288 /// associated with the creation of that condition variable.
2289 DeclStmt *getConditionVariableDeclStmt() {
2290 return hasVarStorage() ? static_cast<DeclStmt *>(
2291 getTrailingObjects<Stmt *>()[varOffset()])
2295 const DeclStmt *getConditionVariableDeclStmt() const {
2296 return hasVarStorage() ? static_cast<DeclStmt *>(
2297 getTrailingObjects<Stmt *>()[varOffset()])
2301 SourceLocation getWhileLoc() const { return WhileStmtBits.WhileLoc; }
2302 void setWhileLoc(SourceLocation L) { WhileStmtBits.WhileLoc = L; }
2304 SourceLocation getBeginLoc() const { return getWhileLoc(); }
2305 SourceLocation getEndLoc() const LLVM_READONLY {
2306 return getBody()->getEndLoc();
2309 static bool classof(const Stmt *T) {
2310 return T->getStmtClass() == WhileStmtClass;
2314 child_range children() {
2315 return child_range(getTrailingObjects<Stmt *>(),
2316 getTrailingObjects<Stmt *>() +
2317 numTrailingObjects(OverloadToken<Stmt *>()));
2320 const_child_range children() const {
2321 return const_child_range(getTrailingObjects<Stmt *>(),
2322 getTrailingObjects<Stmt *>() +
2323 numTrailingObjects(OverloadToken<Stmt *>()));
2327 /// DoStmt - This represents a 'do/while' stmt.
2328 class DoStmt : public Stmt {
2329 enum { BODY, COND, END_EXPR };
2330 Stmt *SubExprs[END_EXPR];
2331 SourceLocation WhileLoc;
2332 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
2335 DoStmt(Stmt *Body, Expr *Cond, SourceLocation DL, SourceLocation WL,
2337 : Stmt(DoStmtClass), WhileLoc(WL), RParenLoc(RP) {
2343 /// Build an empty do-while statement.
2344 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
2346 Expr *getCond() { return reinterpret_cast<Expr *>(SubExprs[COND]); }
2347 const Expr *getCond() const {
2348 return reinterpret_cast<Expr *>(SubExprs[COND]);
2351 void setCond(Expr *Cond) { SubExprs[COND] = reinterpret_cast<Stmt *>(Cond); }
2353 Stmt *getBody() { return SubExprs[BODY]; }
2354 const Stmt *getBody() const { return SubExprs[BODY]; }
2355 void setBody(Stmt *Body) { SubExprs[BODY] = Body; }
2357 SourceLocation getDoLoc() const { return DoStmtBits.DoLoc; }
2358 void setDoLoc(SourceLocation L) { DoStmtBits.DoLoc = L; }
2359 SourceLocation getWhileLoc() const { return WhileLoc; }
2360 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
2361 SourceLocation getRParenLoc() const { return RParenLoc; }
2362 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2364 SourceLocation getBeginLoc() const { return getDoLoc(); }
2365 SourceLocation getEndLoc() const { return getRParenLoc(); }
2367 static bool classof(const Stmt *T) {
2368 return T->getStmtClass() == DoStmtClass;
2372 child_range children() {
2373 return child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
2376 const_child_range children() const {
2377 return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
2381 /// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
2382 /// the init/cond/inc parts of the ForStmt will be null if they were not
2383 /// specified in the source.
2384 class ForStmt : public Stmt {
2385 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
2386 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
2387 SourceLocation LParenLoc, RParenLoc;
2390 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
2391 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
2394 /// Build an empty for statement.
2395 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
2397 Stmt *getInit() { return SubExprs[INIT]; }
2399 /// Retrieve the variable declared in this "for" statement, if any.
2401 /// In the following example, "y" is the condition variable.
2403 /// for (int x = random(); int y = mangle(x); ++x) {
2407 VarDecl *getConditionVariable() const;
2408 void setConditionVariable(const ASTContext &C, VarDecl *V);
2410 /// If this ForStmt has a condition variable, return the faux DeclStmt
2411 /// associated with the creation of that condition variable.
2412 const DeclStmt *getConditionVariableDeclStmt() const {
2413 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
2416 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
2417 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
2418 Stmt *getBody() { return SubExprs[BODY]; }
2420 const Stmt *getInit() const { return SubExprs[INIT]; }
2421 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
2422 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
2423 const Stmt *getBody() const { return SubExprs[BODY]; }
2425 void setInit(Stmt *S) { SubExprs[INIT] = S; }
2426 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
2427 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
2428 void setBody(Stmt *S) { SubExprs[BODY] = S; }
2430 SourceLocation getForLoc() const { return ForStmtBits.ForLoc; }
2431 void setForLoc(SourceLocation L) { ForStmtBits.ForLoc = L; }
2432 SourceLocation getLParenLoc() const { return LParenLoc; }
2433 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2434 SourceLocation getRParenLoc() const { return RParenLoc; }
2435 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2437 SourceLocation getBeginLoc() const { return getForLoc(); }
2438 SourceLocation getEndLoc() const { return getBody()->getEndLoc(); }
2440 static bool classof(const Stmt *T) {
2441 return T->getStmtClass() == ForStmtClass;
2445 child_range children() {
2446 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
2449 const_child_range children() const {
2450 return const_child_range(&SubExprs[0], &SubExprs[0] + END_EXPR);
2454 /// GotoStmt - This represents a direct goto.
2455 class GotoStmt : public Stmt {
2457 SourceLocation LabelLoc;
2460 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
2461 : Stmt(GotoStmtClass), Label(label), LabelLoc(LL) {
2465 /// Build an empty goto statement.
2466 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
2468 LabelDecl *getLabel() const { return Label; }
2469 void setLabel(LabelDecl *D) { Label = D; }
2471 SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
2472 void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
2473 SourceLocation getLabelLoc() const { return LabelLoc; }
2474 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
2476 SourceLocation getBeginLoc() const { return getGotoLoc(); }
2477 SourceLocation getEndLoc() const { return getLabelLoc(); }
2479 static bool classof(const Stmt *T) {
2480 return T->getStmtClass() == GotoStmtClass;
2484 child_range children() {
2485 return child_range(child_iterator(), child_iterator());
2488 const_child_range children() const {
2489 return const_child_range(const_child_iterator(), const_child_iterator());
2493 /// IndirectGotoStmt - This represents an indirect goto.
2494 class IndirectGotoStmt : public Stmt {
2495 SourceLocation StarLoc;
2499 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc, Expr *target)
2500 : Stmt(IndirectGotoStmtClass), StarLoc(starLoc) {
2502 setGotoLoc(gotoLoc);
2505 /// Build an empty indirect goto statement.
2506 explicit IndirectGotoStmt(EmptyShell Empty)
2507 : Stmt(IndirectGotoStmtClass, Empty) {}
2509 void setGotoLoc(SourceLocation L) { GotoStmtBits.GotoLoc = L; }
2510 SourceLocation getGotoLoc() const { return GotoStmtBits.GotoLoc; }
2511 void setStarLoc(SourceLocation L) { StarLoc = L; }
2512 SourceLocation getStarLoc() const { return StarLoc; }
2514 Expr *getTarget() { return reinterpret_cast<Expr *>(Target); }
2515 const Expr *getTarget() const {
2516 return reinterpret_cast<const Expr *>(Target);
2518 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt *>(E); }
2520 /// getConstantTarget - Returns the fixed target of this indirect
2521 /// goto, if one exists.
2522 LabelDecl *getConstantTarget();
2523 const LabelDecl *getConstantTarget() const {
2524 return const_cast<IndirectGotoStmt *>(this)->getConstantTarget();
2527 SourceLocation getBeginLoc() const { return getGotoLoc(); }
2528 SourceLocation getEndLoc() const LLVM_READONLY { return Target->getEndLoc(); }
2530 static bool classof(const Stmt *T) {
2531 return T->getStmtClass() == IndirectGotoStmtClass;
2535 child_range children() { return child_range(&Target, &Target + 1); }
2537 const_child_range children() const {
2538 return const_child_range(&Target, &Target + 1);
2542 /// ContinueStmt - This represents a continue.
2543 class ContinueStmt : public Stmt {
2545 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass) {
2549 /// Build an empty continue statement.
2550 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
2552 SourceLocation getContinueLoc() const { return ContinueStmtBits.ContinueLoc; }
2553 void setContinueLoc(SourceLocation L) { ContinueStmtBits.ContinueLoc = L; }
2555 SourceLocation getBeginLoc() const { return getContinueLoc(); }
2556 SourceLocation getEndLoc() const { return getContinueLoc(); }
2558 static bool classof(const Stmt *T) {
2559 return T->getStmtClass() == ContinueStmtClass;
2563 child_range children() {
2564 return child_range(child_iterator(), child_iterator());
2567 const_child_range children() const {
2568 return const_child_range(const_child_iterator(), const_child_iterator());
2572 /// BreakStmt - This represents a break.
2573 class BreakStmt : public Stmt {
2575 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass) {
2579 /// Build an empty break statement.
2580 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
2582 SourceLocation getBreakLoc() const { return BreakStmtBits.BreakLoc; }
2583 void setBreakLoc(SourceLocation L) { BreakStmtBits.BreakLoc = L; }
2585 SourceLocation getBeginLoc() const { return getBreakLoc(); }
2586 SourceLocation getEndLoc() const { return getBreakLoc(); }
2588 static bool classof(const Stmt *T) {
2589 return T->getStmtClass() == BreakStmtClass;
2593 child_range children() {
2594 return child_range(child_iterator(), child_iterator());
2597 const_child_range children() const {
2598 return const_child_range(const_child_iterator(), const_child_iterator());
2602 /// ReturnStmt - This represents a return, optionally of an expression:
2606 /// Note that GCC allows return with no argument in a function declared to
2607 /// return a value, and it allows returning a value in functions declared to
2608 /// return void. We explicitly model this in the AST, which means you can't
2609 /// depend on the return type of the function and the presence of an argument.
2610 class ReturnStmt final
2612 private llvm::TrailingObjects<ReturnStmt, const VarDecl *> {
2613 friend TrailingObjects;
2615 /// The return expression.
2618 // ReturnStmt is followed optionally by a trailing "const VarDecl *"
2619 // for the NRVO candidate. Present if and only if hasNRVOCandidate().
2621 /// True if this ReturnStmt has storage for an NRVO candidate.
2622 bool hasNRVOCandidate() const { return ReturnStmtBits.HasNRVOCandidate; }
2624 unsigned numTrailingObjects(OverloadToken<const VarDecl *>) const {
2625 return hasNRVOCandidate();
2628 /// Build a return statement.
2629 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate);
2631 /// Build an empty return statement.
2632 explicit ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate);
2635 /// Create a return statement.
2636 static ReturnStmt *Create(const ASTContext &Ctx, SourceLocation RL, Expr *E,
2637 const VarDecl *NRVOCandidate);
2639 /// Create an empty return statement, optionally with
2640 /// storage for an NRVO candidate.
2641 static ReturnStmt *CreateEmpty(const ASTContext &Ctx, bool HasNRVOCandidate);
2643 Expr *getRetValue() { return reinterpret_cast<Expr *>(RetExpr); }
2644 const Expr *getRetValue() const { return reinterpret_cast<Expr *>(RetExpr); }
2645 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt *>(E); }
2647 /// Retrieve the variable that might be used for the named return
2648 /// value optimization.
2650 /// The optimization itself can only be performed if the variable is
2651 /// also marked as an NRVO object.
2652 const VarDecl *getNRVOCandidate() const {
2653 return hasNRVOCandidate() ? *getTrailingObjects<const VarDecl *>()
2657 /// Set the variable that might be used for the named return value
2658 /// optimization. The return statement must have storage for it,
2659 /// which is the case if and only if hasNRVOCandidate() is true.
2660 void setNRVOCandidate(const VarDecl *Var) {
2661 assert(hasNRVOCandidate() &&
2662 "This return statement has no storage for an NRVO candidate!");
2663 *getTrailingObjects<const VarDecl *>() = Var;
2666 SourceLocation getReturnLoc() const { return ReturnStmtBits.RetLoc; }
2667 void setReturnLoc(SourceLocation L) { ReturnStmtBits.RetLoc = L; }
2669 SourceLocation getBeginLoc() const { return getReturnLoc(); }
2670 SourceLocation getEndLoc() const LLVM_READONLY {
2671 return RetExpr ? RetExpr->getEndLoc() : getReturnLoc();
2674 static bool classof(const Stmt *T) {
2675 return T->getStmtClass() == ReturnStmtClass;
2679 child_range children() {
2681 return child_range(&RetExpr, &RetExpr + 1);
2682 return child_range(child_iterator(), child_iterator());
2685 const_child_range children() const {
2687 return const_child_range(&RetExpr, &RetExpr + 1);
2688 return const_child_range(const_child_iterator(), const_child_iterator());
2692 /// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
2693 class AsmStmt : public Stmt {
2695 friend class ASTStmtReader;
2697 SourceLocation AsmLoc;
2699 /// True if the assembly statement does not have any input or output
2703 /// If true, treat this inline assembly as having side effects.
2704 /// This assembly statement should not be optimized, deleted or moved.
2707 unsigned NumOutputs;
2709 unsigned NumClobbers;
2711 Stmt **Exprs = nullptr;
2713 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
2714 unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
2715 : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
2716 NumOutputs(numoutputs), NumInputs(numinputs),
2717 NumClobbers(numclobbers) {}
2720 /// Build an empty inline-assembly statement.
2721 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
2723 SourceLocation getAsmLoc() const { return AsmLoc; }
2724 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
2726 bool isSimple() const { return IsSimple; }
2727 void setSimple(bool V) { IsSimple = V; }
2729 bool isVolatile() const { return IsVolatile; }
2730 void setVolatile(bool V) { IsVolatile = V; }
2732 SourceLocation getBeginLoc() const LLVM_READONLY { return {}; }
2733 SourceLocation getEndLoc() const LLVM_READONLY { return {}; }
2735 //===--- Asm String Analysis ---===//
2737 /// Assemble final IR asm string.
2738 std::string generateAsmString(const ASTContext &C) const;
2740 //===--- Output operands ---===//
2742 unsigned getNumOutputs() const { return NumOutputs; }
2744 /// getOutputConstraint - Return the constraint string for the specified
2745 /// output operand. All output constraints are known to be non-empty (either
2747 StringRef getOutputConstraint(unsigned i) const;
2749 /// isOutputPlusConstraint - Return true if the specified output constraint
2750 /// is a "+" constraint (which is both an input and an output) or false if it
2751 /// is an "=" constraint (just an output).
2752 bool isOutputPlusConstraint(unsigned i) const {
2753 return getOutputConstraint(i)[0] == '+';
2756 const Expr *getOutputExpr(unsigned i) const;
2758 /// getNumPlusOperands - Return the number of output operands that have a "+"
2760 unsigned getNumPlusOperands() const;
2762 //===--- Input operands ---===//
2764 unsigned getNumInputs() const { return NumInputs; }
2766 /// getInputConstraint - Return the specified input constraint. Unlike output
2767 /// constraints, these can be empty.
2768 StringRef getInputConstraint(unsigned i) const;
2770 const Expr *getInputExpr(unsigned i) const;
2772 //===--- Other ---===//
2774 unsigned getNumClobbers() const { return NumClobbers; }
2775 StringRef getClobber(unsigned i) const;
2777 static bool classof(const Stmt *T) {
2778 return T->getStmtClass() == GCCAsmStmtClass ||
2779 T->getStmtClass() == MSAsmStmtClass;
2782 // Input expr iterators.
2784 using inputs_iterator = ExprIterator;
2785 using const_inputs_iterator = ConstExprIterator;
2786 using inputs_range = llvm::iterator_range<inputs_iterator>;
2787 using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
2789 inputs_iterator begin_inputs() {
2790 return &Exprs[0] + NumOutputs;
2793 inputs_iterator end_inputs() {
2794 return &Exprs[0] + NumOutputs + NumInputs;
2797 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
2799 const_inputs_iterator begin_inputs() const {
2800 return &Exprs[0] + NumOutputs;
2803 const_inputs_iterator end_inputs() const {
2804 return &Exprs[0] + NumOutputs + NumInputs;
2807 inputs_const_range inputs() const {
2808 return inputs_const_range(begin_inputs(), end_inputs());
2811 // Output expr iterators.
2813 using outputs_iterator = ExprIterator;
2814 using const_outputs_iterator = ConstExprIterator;
2815 using outputs_range = llvm::iterator_range<outputs_iterator>;
2816 using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
2818 outputs_iterator begin_outputs() {
2822 outputs_iterator end_outputs() {
2823 return &Exprs[0] + NumOutputs;
2826 outputs_range outputs() {
2827 return outputs_range(begin_outputs(), end_outputs());
2830 const_outputs_iterator begin_outputs() const {
2834 const_outputs_iterator end_outputs() const {
2835 return &Exprs[0] + NumOutputs;
2838 outputs_const_range outputs() const {
2839 return outputs_const_range(begin_outputs(), end_outputs());
2842 child_range children() {
2843 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
2846 const_child_range children() const {
2847 return const_child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
2851 /// This represents a GCC inline-assembly statement extension.
2852 class GCCAsmStmt : public AsmStmt {
2853 friend class ASTStmtReader;
2855 SourceLocation RParenLoc;
2856 StringLiteral *AsmStr;
2858 // FIXME: If we wanted to, we could allocate all of these in one big array.
2859 StringLiteral **Constraints = nullptr;
2860 StringLiteral **Clobbers = nullptr;
2861 IdentifierInfo **Names = nullptr;
2862 unsigned NumLabels = 0;
2865 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
2866 bool isvolatile, unsigned numoutputs, unsigned numinputs,
2867 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
2868 StringLiteral *asmstr, unsigned numclobbers,
2869 StringLiteral **clobbers, unsigned numlabels,
2870 SourceLocation rparenloc);
2872 /// Build an empty inline-assembly statement.
2873 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
2875 SourceLocation getRParenLoc() const { return RParenLoc; }
2876 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
2878 //===--- Asm String Analysis ---===//
2880 const StringLiteral *getAsmString() const { return AsmStr; }
2881 StringLiteral *getAsmString() { return AsmStr; }
2882 void setAsmString(StringLiteral *E) { AsmStr = E; }
2884 /// AsmStringPiece - this is part of a decomposed asm string specification
2885 /// (for use with the AnalyzeAsmString function below). An asm string is
2886 /// considered to be a concatenation of these parts.
2887 class AsmStringPiece {
2890 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
2891 Operand // Operand reference, with optional modifier %c4.
2899 // Source range for operand references.
2900 CharSourceRange Range;
2903 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
2904 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
2906 : MyKind(Operand), Str(S), OperandNo(OpNo),
2907 Range(CharSourceRange::getCharRange(Begin, End)) {}
2909 bool isString() const { return MyKind == String; }
2910 bool isOperand() const { return MyKind == Operand; }
2912 const std::string &getString() const { return Str; }
2914 unsigned getOperandNo() const {
2915 assert(isOperand());
2919 CharSourceRange getRange() const {
2920 assert(isOperand() && "Range is currently used only for Operands.");
2924 /// getModifier - Get the modifier for this operand, if present. This
2925 /// returns '\0' if there was no modifier.
2926 char getModifier() const;
2929 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
2930 /// it into pieces. If the asm string is erroneous, emit errors and return
2931 /// true, otherwise return false. This handles canonicalization and
2932 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
2933 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
2934 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
2935 const ASTContext &C, unsigned &DiagOffs) const;
2937 /// Assemble final IR asm string.
2938 std::string generateAsmString(const ASTContext &C) const;
2940 //===--- Output operands ---===//
2942 IdentifierInfo *getOutputIdentifier(unsigned i) const { return Names[i]; }
2944 StringRef getOutputName(unsigned i) const {
2945 if (IdentifierInfo *II = getOutputIdentifier(i))
2946 return II->getName();
2951 StringRef getOutputConstraint(unsigned i) const;
2953 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
2954 return Constraints[i];
2956 StringLiteral *getOutputConstraintLiteral(unsigned i) {
2957 return Constraints[i];
2960 Expr *getOutputExpr(unsigned i);
2962 const Expr *getOutputExpr(unsigned i) const {
2963 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
2966 //===--- Input operands ---===//
2968 IdentifierInfo *getInputIdentifier(unsigned i) const {
2969 return Names[i + NumOutputs];
2972 StringRef getInputName(unsigned i) const {
2973 if (IdentifierInfo *II = getInputIdentifier(i))
2974 return II->getName();
2979 StringRef getInputConstraint(unsigned i) const;
2981 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
2982 return Constraints[i + NumOutputs];
2984 StringLiteral *getInputConstraintLiteral(unsigned i) {
2985 return Constraints[i + NumOutputs];
2988 Expr *getInputExpr(unsigned i);
2989 void setInputExpr(unsigned i, Expr *E);
2991 const Expr *getInputExpr(unsigned i) const {
2992 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
2995 //===--- Labels ---===//
2997 bool isAsmGoto() const {
2998 return NumLabels > 0;
3001 unsigned getNumLabels() const {
3005 IdentifierInfo *getLabelIdentifier(unsigned i) const {
3006 return Names[i + NumInputs];
3009 AddrLabelExpr *getLabelExpr(unsigned i) const;
3010 StringRef getLabelName(unsigned i) const;
3011 using labels_iterator = CastIterator<AddrLabelExpr>;
3012 using const_labels_iterator = ConstCastIterator<AddrLabelExpr>;
3013 using labels_range = llvm::iterator_range<labels_iterator>;
3014 using labels_const_range = llvm::iterator_range<const_labels_iterator>;
3016 labels_iterator begin_labels() {
3017 return &Exprs[0] + NumInputs;
3020 labels_iterator end_labels() {
3021 return &Exprs[0] + NumInputs + NumLabels;
3024 labels_range labels() {
3025 return labels_range(begin_labels(), end_labels());
3028 const_labels_iterator begin_labels() const {
3029 return &Exprs[0] + NumInputs;
3032 const_labels_iterator end_labels() const {
3033 return &Exprs[0] + NumInputs + NumLabels;
3036 labels_const_range labels() const {
3037 return labels_const_range(begin_labels(), end_labels());
3041 void setOutputsAndInputsAndClobbers(const ASTContext &C,
3042 IdentifierInfo **Names,
3043 StringLiteral **Constraints,
3045 unsigned NumOutputs,
3048 StringLiteral **Clobbers,
3049 unsigned NumClobbers);
3052 //===--- Other ---===//
3054 /// getNamedOperand - Given a symbolic operand reference like %[foo],
3055 /// translate this into a numeric value needed to reference the same operand.
3056 /// This returns -1 if the operand name is invalid.
3057 int getNamedOperand(StringRef SymbolicName) const;
3059 StringRef getClobber(unsigned i) const;
3061 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
3062 const StringLiteral *getClobberStringLiteral(unsigned i) const {
3066 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
3067 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
3069 static bool classof(const Stmt *T) {
3070 return T->getStmtClass() == GCCAsmStmtClass;
3074 /// This represents a Microsoft inline-assembly statement extension.
3075 class MSAsmStmt : public AsmStmt {
3076 friend class ASTStmtReader;
3078 SourceLocation LBraceLoc, EndLoc;
3081 unsigned NumAsmToks = 0;
3083 Token *AsmToks = nullptr;
3084 StringRef *Constraints = nullptr;
3085 StringRef *Clobbers = nullptr;
3088 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
3089 SourceLocation lbraceloc, bool issimple, bool isvolatile,
3090 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
3091 ArrayRef<StringRef> constraints,
3092 ArrayRef<Expr*> exprs, StringRef asmstr,
3093 ArrayRef<StringRef> clobbers, SourceLocation endloc);
3095 /// Build an empty MS-style inline-assembly statement.
3096 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
3098 SourceLocation getLBraceLoc() const { return LBraceLoc; }
3099 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
3100 SourceLocation getEndLoc() const { return EndLoc; }
3101 void setEndLoc(SourceLocation L) { EndLoc = L; }
3103 bool hasBraces() const { return LBraceLoc.isValid(); }
3105 unsigned getNumAsmToks() { return NumAsmToks; }
3106 Token *getAsmToks() { return AsmToks; }
3108 //===--- Asm String Analysis ---===//
3109 StringRef getAsmString() const { return AsmStr; }
3111 /// Assemble final IR asm string.
3112 std::string generateAsmString(const ASTContext &C) const;
3114 //===--- Output operands ---===//
3116 StringRef getOutputConstraint(unsigned i) const {
3117 assert(i < NumOutputs);
3118 return Constraints[i];
3121 Expr *getOutputExpr(unsigned i);
3123 const Expr *getOutputExpr(unsigned i) const {
3124 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
3127 //===--- Input operands ---===//
3129 StringRef getInputConstraint(unsigned i) const {
3130 assert(i < NumInputs);
3131 return Constraints[i + NumOutputs];
3134 Expr *getInputExpr(unsigned i);
3135 void setInputExpr(unsigned i, Expr *E);
3137 const Expr *getInputExpr(unsigned i) const {
3138 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
3141 //===--- Other ---===//
3143 ArrayRef<StringRef> getAllConstraints() const {
3144 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
3147 ArrayRef<StringRef> getClobbers() const {
3148 return llvm::makeArrayRef(Clobbers, NumClobbers);
3151 ArrayRef<Expr*> getAllExprs() const {
3152 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
3153 NumInputs + NumOutputs);
3156 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
3159 void initialize(const ASTContext &C, StringRef AsmString,
3160 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
3161 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
3164 SourceLocation getBeginLoc() const LLVM_READONLY { return AsmLoc; }
3166 static bool classof(const Stmt *T) {
3167 return T->getStmtClass() == MSAsmStmtClass;
3170 child_range children() {
3171 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
3174 const_child_range children() const {
3175 return const_child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
3179 class SEHExceptStmt : public Stmt {
3180 friend class ASTReader;
3181 friend class ASTStmtReader;
3186 enum { FILTER_EXPR, BLOCK };
3188 SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
3189 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
3192 static SEHExceptStmt* Create(const ASTContext &C,
3193 SourceLocation ExceptLoc,
3197 SourceLocation getBeginLoc() const LLVM_READONLY { return getExceptLoc(); }
3199 SourceLocation getExceptLoc() const { return Loc; }
3200 SourceLocation getEndLoc() const { return getBlock()->getEndLoc(); }
3202 Expr *getFilterExpr() const {
3203 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
3206 CompoundStmt *getBlock() const {
3207 return cast<CompoundStmt>(Children[BLOCK]);
3210 child_range children() {
3211 return child_range(Children, Children+2);
3214 const_child_range children() const {
3215 return const_child_range(Children, Children + 2);
3218 static bool classof(const Stmt *T) {
3219 return T->getStmtClass() == SEHExceptStmtClass;
3223 class SEHFinallyStmt : public Stmt {
3224 friend class ASTReader;
3225 friend class ASTStmtReader;
3230 SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
3231 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
3234 static SEHFinallyStmt* Create(const ASTContext &C,
3235 SourceLocation FinallyLoc,
3238 SourceLocation getBeginLoc() const LLVM_READONLY { return getFinallyLoc(); }
3240 SourceLocation getFinallyLoc() const { return Loc; }
3241 SourceLocation getEndLoc() const { return Block->getEndLoc(); }
3243 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
3245 child_range children() {
3246 return child_range(&Block,&Block+1);
3249 const_child_range children() const {
3250 return const_child_range(&Block, &Block + 1);
3253 static bool classof(const Stmt *T) {
3254 return T->getStmtClass() == SEHFinallyStmtClass;
3258 class SEHTryStmt : public Stmt {
3259 friend class ASTReader;
3260 friend class ASTStmtReader;
3263 SourceLocation TryLoc;
3266 enum { TRY = 0, HANDLER = 1 };
3268 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
3269 SourceLocation TryLoc,
3273 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
3276 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
3277 SourceLocation TryLoc, Stmt *TryBlock,
3280 SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
3282 SourceLocation getTryLoc() const { return TryLoc; }
3283 SourceLocation getEndLoc() const { return Children[HANDLER]->getEndLoc(); }
3285 bool getIsCXXTry() const { return IsCXXTry; }
3287 CompoundStmt* getTryBlock() const {
3288 return cast<CompoundStmt>(Children[TRY]);
3291 Stmt *getHandler() const { return Children[HANDLER]; }
3293 /// Returns 0 if not defined
3294 SEHExceptStmt *getExceptHandler() const;
3295 SEHFinallyStmt *getFinallyHandler() const;
3297 child_range children() {
3298 return child_range(Children, Children+2);
3301 const_child_range children() const {
3302 return const_child_range(Children, Children + 2);
3305 static bool classof(const Stmt *T) {
3306 return T->getStmtClass() == SEHTryStmtClass;
3310 /// Represents a __leave statement.
3311 class SEHLeaveStmt : public Stmt {
3312 SourceLocation LeaveLoc;
3315 explicit SEHLeaveStmt(SourceLocation LL)
3316 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
3318 /// Build an empty __leave statement.
3319 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
3321 SourceLocation getLeaveLoc() const { return LeaveLoc; }
3322 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
3324 SourceLocation getBeginLoc() const LLVM_READONLY { return LeaveLoc; }
3325 SourceLocation getEndLoc() const LLVM_READONLY { return LeaveLoc; }
3327 static bool classof(const Stmt *T) {
3328 return T->getStmtClass() == SEHLeaveStmtClass;
3332 child_range children() {
3333 return child_range(child_iterator(), child_iterator());
3336 const_child_range children() const {
3337 return const_child_range(const_child_iterator(), const_child_iterator());
3341 /// This captures a statement into a function. For example, the following
3342 /// pragma annotated compound statement can be represented as a CapturedStmt,
3343 /// and this compound statement is the body of an anonymous outlined function.
3345 /// #pragma omp parallel
3350 class CapturedStmt : public Stmt {
3352 /// The different capture forms: by 'this', by reference, capture for
3353 /// variable-length array type etc.
3354 enum VariableCaptureKind {
3361 /// Describes the capture of either a variable, or 'this', or
3362 /// variable-length array type.
3364 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
3368 friend class ASTStmtReader;
3370 /// Create a new capture.
3372 /// \param Loc The source location associated with this capture.
3374 /// \param Kind The kind of capture (this, ByRef, ...).
3376 /// \param Var The variable being captured, or null if capturing this.
3377 Capture(SourceLocation Loc, VariableCaptureKind Kind,
3378 VarDecl *Var = nullptr);
3380 /// Determine the kind of capture.
3381 VariableCaptureKind getCaptureKind() const;
3383 /// Retrieve the source location at which the variable or 'this' was
3385 SourceLocation getLocation() const { return Loc; }
3387 /// Determine whether this capture handles the C++ 'this' pointer.
3388 bool capturesThis() const { return getCaptureKind() == VCK_This; }
3390 /// Determine whether this capture handles a variable (by reference).
3391 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
3393 /// Determine whether this capture handles a variable by copy.
3394 bool capturesVariableByCopy() const {
3395 return getCaptureKind() == VCK_ByCopy;
3398 /// Determine whether this capture handles a variable-length array
3400 bool capturesVariableArrayType() const {
3401 return getCaptureKind() == VCK_VLAType;
3404 /// Retrieve the declaration of the variable being captured.
3406 /// This operation is only valid if this capture captures a variable.
3407 VarDecl *getCapturedVar() const;
3411 /// The number of variable captured, including 'this'.
3412 unsigned NumCaptures;
3414 /// The pointer part is the implicit the outlined function and the
3415 /// int part is the captured region kind, 'CR_Default' etc.
3416 llvm::PointerIntPair<CapturedDecl *, 2, CapturedRegionKind> CapDeclAndKind;
3418 /// The record for captured variables, a RecordDecl or CXXRecordDecl.
3419 RecordDecl *TheRecordDecl = nullptr;
3421 /// Construct a captured statement.
3422 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
3423 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
3425 /// Construct an empty captured statement.
3426 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
3428 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
3430 Stmt *const *getStoredStmts() const {
3431 return reinterpret_cast<Stmt *const *>(this + 1);
3434 Capture *getStoredCaptures() const;
3436 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
3439 friend class ASTStmtReader;
3441 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
3442 CapturedRegionKind Kind,
3443 ArrayRef<Capture> Captures,
3444 ArrayRef<Expr *> CaptureInits,
3445 CapturedDecl *CD, RecordDecl *RD);
3447 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
3448 unsigned NumCaptures);
3450 /// Retrieve the statement being captured.
3451 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
3452 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
3454 /// Retrieve the outlined function declaration.
3455 CapturedDecl *getCapturedDecl();
3456 const CapturedDecl *getCapturedDecl() const;
3458 /// Set the outlined function declaration.
3459 void setCapturedDecl(CapturedDecl *D);
3461 /// Retrieve the captured region kind.
3462 CapturedRegionKind getCapturedRegionKind() const;
3464 /// Set the captured region kind.
3465 void setCapturedRegionKind(CapturedRegionKind Kind);
3467 /// Retrieve the record declaration for captured variables.
3468 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
3470 /// Set the record declaration for captured variables.
3471 void setCapturedRecordDecl(RecordDecl *D) {
3472 assert(D && "null RecordDecl");
3476 /// True if this variable has been captured.
3477 bool capturesVariable(const VarDecl *Var) const;
3479 /// An iterator that walks over the captures.
3480 using capture_iterator = Capture *;
3481 using const_capture_iterator = const Capture *;
3482 using capture_range = llvm::iterator_range<capture_iterator>;
3483 using capture_const_range = llvm::iterator_range<const_capture_iterator>;
3485 capture_range captures() {
3486 return capture_range(capture_begin(), capture_end());
3488 capture_const_range captures() const {
3489 return capture_const_range(capture_begin(), capture_end());
3492 /// Retrieve an iterator pointing to the first capture.
3493 capture_iterator capture_begin() { return getStoredCaptures(); }
3494 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
3496 /// Retrieve an iterator pointing past the end of the sequence of
3498 capture_iterator capture_end() const {
3499 return getStoredCaptures() + NumCaptures;
3502 /// Retrieve the number of captures, including 'this'.
3503 unsigned capture_size() const { return NumCaptures; }
3505 /// Iterator that walks over the capture initialization arguments.
3506 using capture_init_iterator = Expr **;
3507 using capture_init_range = llvm::iterator_range<capture_init_iterator>;
3509 /// Const iterator that walks over the capture initialization
3511 using const_capture_init_iterator = Expr *const *;
3512 using const_capture_init_range =
3513 llvm::iterator_range<const_capture_init_iterator>;
3515 capture_init_range capture_inits() {
3516 return capture_init_range(capture_init_begin(), capture_init_end());
3519 const_capture_init_range capture_inits() const {
3520 return const_capture_init_range(capture_init_begin(), capture_init_end());
3523 /// Retrieve the first initialization argument.
3524 capture_init_iterator capture_init_begin() {
3525 return reinterpret_cast<Expr **>(getStoredStmts());
3528 const_capture_init_iterator capture_init_begin() const {
3529 return reinterpret_cast<Expr *const *>(getStoredStmts());
3532 /// Retrieve the iterator pointing one past the last initialization
3534 capture_init_iterator capture_init_end() {
3535 return capture_init_begin() + NumCaptures;
3538 const_capture_init_iterator capture_init_end() const {
3539 return capture_init_begin() + NumCaptures;
3542 SourceLocation getBeginLoc() const LLVM_READONLY {
3543 return getCapturedStmt()->getBeginLoc();
3546 SourceLocation getEndLoc() const LLVM_READONLY {
3547 return getCapturedStmt()->getEndLoc();
3550 SourceRange getSourceRange() const LLVM_READONLY {
3551 return getCapturedStmt()->getSourceRange();
3554 static bool classof(const Stmt *T) {
3555 return T->getStmtClass() == CapturedStmtClass;
3558 child_range children();
3560 const_child_range children() const;
3563 } // namespace clang
3565 #endif // LLVM_CLANG_AST_STMT_H