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