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

source code of clang/include/clang/AST/Stmt.h