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