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