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