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