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