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