1//===- Stmt.h - Classes for representing statements -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the Stmt interface and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_STMT_H
15#define LLVM_CLANG_AST_STMT_H
16
17#include "clang/AST/DeclGroup.h"
18#include "clang/AST/StmtIterator.h"
19#include "clang/Basic/CapturedStmt.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LLVM.h"
22#include "clang/Basic/SourceLocation.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/PointerIntPair.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/iterator.h"
27#include "llvm/ADT/iterator_range.h"
28#include "llvm/Support/Casting.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/ErrorHandling.h"
31#include <algorithm>
32#include <cassert>
33#include <cstddef>
34#include <iterator>
35#include <string>
36
37namespace llvm {
38
39class FoldingSetNodeID;
40
41} // namespace llvm
42
43namespace clang {
44
45class ASTContext;
46class Attr;
47class CapturedDecl;
48class Decl;
49class Expr;
50class LabelDecl;
51class ODRHash;
52class PrinterHelper;
53struct PrintingPolicy;
54class RecordDecl;
55class SourceManager;
56class StringLiteral;
57class Token;
58class VarDecl;
59
60//===----------------------------------------------------------------------===//
61// AST classes for statements.
62//===----------------------------------------------------------------------===//
63
64/// Stmt - This represents one statement.
65///
66class alignas(void *) Stmt {
67public:
68 enum StmtClass {
69 NoStmtClass = 0,
70#define STMT(CLASS, PARENT) CLASS##Class,
71#define STMT_RANGE(BASE, FIRST, LAST) \
72 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class,
73#define LAST_STMT_RANGE(BASE, FIRST, LAST) \
74 first##BASE##Constant=FIRST##Class, last##BASE##Constant=LAST##Class
75#define ABSTRACT_STMT(STMT)
76#include "clang/AST/StmtNodes.inc"
77 };
78
79 // Make vanilla 'new' and 'delete' illegal for Stmts.
80protected:
81 friend class ASTStmtReader;
82 friend class ASTStmtWriter;
83
84 void *operator new(size_t bytes) noexcept {
85 llvm_unreachable("Stmts cannot be allocated with regular 'new'.");
86 }
87
88 void operator delete(void *data) noexcept {
89 llvm_unreachable("Stmts cannot be released with regular 'delete'.");
90 }
91
92 class StmtBitfields {
93 friend class Stmt;
94
95 /// \brief The statement class.
96 unsigned sClass : 8;
97 };
98 enum { NumStmtBits = 8 };
99
100 class CompoundStmtBitfields {
101 friend class CompoundStmt;
102
103 unsigned : NumStmtBits;
104
105 unsigned NumStmts : 32 - NumStmtBits;
106 };
107
108 class IfStmtBitfields {
109 friend class IfStmt;
110
111 unsigned : NumStmtBits;
112
113 unsigned IsConstexpr : 1;
114 };
115
116 class ExprBitfields {
117 friend class ASTStmtReader; // deserialization
118 friend class AtomicExpr; // ctor
119 friend class BlockDeclRefExpr; // ctor
120 friend class CallExpr; // ctor
121 friend class CXXConstructExpr; // ctor
122 friend class CXXDependentScopeMemberExpr; // ctor
123 friend class CXXNewExpr; // ctor
124 friend class CXXUnresolvedConstructExpr; // ctor
125 friend class DeclRefExpr; // computeDependence
126 friend class DependentScopeDeclRefExpr; // ctor
127 friend class DesignatedInitExpr; // ctor
128 friend class Expr;
129 friend class InitListExpr; // ctor
130 friend class ObjCArrayLiteral; // ctor
131 friend class ObjCDictionaryLiteral; // ctor
132 friend class ObjCMessageExpr; // ctor
133 friend class OffsetOfExpr; // ctor
134 friend class OpaqueValueExpr; // ctor
135 friend class OverloadExpr; // ctor
136 friend class ParenListExpr; // ctor
137 friend class PseudoObjectExpr; // ctor
138 friend class ShuffleVectorExpr; // ctor
139
140 unsigned : NumStmtBits;
141
142 unsigned ValueKind : 2;
143 unsigned ObjectKind : 3;
144 unsigned TypeDependent : 1;
145 unsigned ValueDependent : 1;
146 unsigned InstantiationDependent : 1;
147 unsigned ContainsUnexpandedParameterPack : 1;
148 };
149 enum { NumExprBits = 17 };
150
151 class CharacterLiteralBitfields {
152 friend class CharacterLiteral;
153
154 unsigned : NumExprBits;
155
156 unsigned Kind : 3;
157 };
158
159 enum APFloatSemantics {
160 IEEEhalf,
161 IEEEsingle,
162 IEEEdouble,
163 x87DoubleExtended,
164 IEEEquad,
165 PPCDoubleDouble
166 };
167
168 class FloatingLiteralBitfields {
169 friend class FloatingLiteral;
170
171 unsigned : NumExprBits;
172
173 unsigned Semantics : 3; // Provides semantics for APFloat construction
174 unsigned IsExact : 1;
175 };
176
177 class UnaryExprOrTypeTraitExprBitfields {
178 friend class UnaryExprOrTypeTraitExpr;
179
180 unsigned : NumExprBits;
181
182 unsigned Kind : 2;
183 unsigned IsType : 1; // true if operand is a type, false if an expression.
184 };
185
186 class DeclRefExprBitfields {
187 friend class ASTStmtReader; // deserialization
188 friend class DeclRefExpr;
189
190 unsigned : NumExprBits;
191
192 unsigned HasQualifier : 1;
193 unsigned HasTemplateKWAndArgsInfo : 1;
194 unsigned HasFoundDecl : 1;
195 unsigned HadMultipleCandidates : 1;
196 unsigned RefersToEnclosingVariableOrCapture : 1;
197 };
198
199 class CastExprBitfields {
200 friend class CastExpr;
201
202 unsigned : NumExprBits;
203
204 unsigned Kind : 6;
205 unsigned BasePathSize : 32 - 6 - NumExprBits;
206 };
207
208 class CallExprBitfields {
209 friend class CallExpr;
210
211 unsigned : NumExprBits;
212
213 unsigned NumPreArgs : 1;
214 };
215
216 class ExprWithCleanupsBitfields {
217 friend class ASTStmtReader; // deserialization
218 friend class ExprWithCleanups;
219
220 unsigned : NumExprBits;
221
222 // When false, it must not have side effects.
223 unsigned CleanupsHaveSideEffects : 1;
224
225 unsigned NumObjects : 32 - 1 - NumExprBits;
226 };
227
228 class PseudoObjectExprBitfields {
229 friend class ASTStmtReader; // deserialization
230 friend class PseudoObjectExpr;
231
232 unsigned : NumExprBits;
233
234 // These don't need to be particularly wide, because they're
235 // strictly limited by the forms of expressions we permit.
236 unsigned NumSubExprs : 8;
237 unsigned ResultIndex : 32 - 8 - NumExprBits;
238 };
239
240 class ObjCIndirectCopyRestoreExprBitfields {
241 friend class ObjCIndirectCopyRestoreExpr;
242
243 unsigned : NumExprBits;
244
245 unsigned ShouldCopy : 1;
246 };
247
248 class InitListExprBitfields {
249 friend class InitListExpr;
250
251 unsigned : NumExprBits;
252
253 /// Whether this initializer list originally had a GNU array-range
254 /// designator in it. This is a temporary marker used by CodeGen.
255 unsigned HadArrayRangeDesignator : 1;
256 };
257
258 class TypeTraitExprBitfields {
259 friend class ASTStmtReader;
260 friend class ASTStmtWriter;
261 friend class TypeTraitExpr;
262
263 unsigned : NumExprBits;
264
265 /// \brief The kind of type trait, which is a value of a TypeTrait enumerator.
266 unsigned Kind : 8;
267
268 /// \brief If this expression is not value-dependent, this indicates whether
269 /// the trait evaluated true or false.
270 unsigned Value : 1;
271
272 /// \brief The number of arguments to this type trait.
273 unsigned NumArgs : 32 - 8 - 1 - NumExprBits;
274 };
275
276 class CoawaitExprBitfields {
277 friend class CoawaitExpr;
278
279 unsigned : NumExprBits;
280
281 unsigned IsImplicit : 1;
282 };
283
284 union {
285 StmtBitfields StmtBits;
286 CompoundStmtBitfields CompoundStmtBits;
287 IfStmtBitfields IfStmtBits;
288 ExprBitfields ExprBits;
289 CharacterLiteralBitfields CharacterLiteralBits;
290 FloatingLiteralBitfields FloatingLiteralBits;
291 UnaryExprOrTypeTraitExprBitfields UnaryExprOrTypeTraitExprBits;
292 DeclRefExprBitfields DeclRefExprBits;
293 CastExprBitfields CastExprBits;
294 CallExprBitfields CallExprBits;
295 ExprWithCleanupsBitfields ExprWithCleanupsBits;
296 PseudoObjectExprBitfields PseudoObjectExprBits;
297 ObjCIndirectCopyRestoreExprBitfields ObjCIndirectCopyRestoreExprBits;
298 InitListExprBitfields InitListExprBits;
299 TypeTraitExprBitfields TypeTraitExprBits;
300 CoawaitExprBitfields CoawaitBits;
301 };
302
303public:
304 // Only allow allocation of Stmts using the allocator in ASTContext
305 // or by doing a placement new.
306 void* operator new(size_t bytes, const ASTContext& C,
307 unsigned alignment = 8);
308
309 void* operator new(size_t bytes, const ASTContext* C,
310 unsigned alignment = 8) {
311 return operator new(bytes, *C, alignment);
312 }
313
314 void *operator new(size_t bytes, void *mem) noexcept { return mem; }
315
316 void operator delete(void *, const ASTContext &, unsigned) noexcept {}
317 void operator delete(void *, const ASTContext *, unsigned) noexcept {}
318 void operator delete(void *, size_t) noexcept {}
319 void operator delete(void *, void *) noexcept {}
320
321public:
322 /// \brief A placeholder type used to construct an empty shell of a
323 /// type, that will be filled in later (e.g., by some
324 /// de-serialization).
325 struct EmptyShell {};
326
327protected:
328 /// Iterator for iterating over Stmt * arrays that contain only Expr *
329 ///
330 /// This is needed because AST nodes use Stmt* arrays to store
331 /// references to children (to be compatible with StmtIterator).
332 struct ExprIterator
333 : llvm::iterator_adaptor_base<ExprIterator, Stmt **,
334 std::random_access_iterator_tag, Expr *> {
335 ExprIterator() : iterator_adaptor_base(nullptr) {}
336 ExprIterator(Stmt **I) : iterator_adaptor_base(I) {}
337
338 reference operator*() const {
339 assert((*I)->getStmtClass() >= firstExprConstant &&
340 (*I)->getStmtClass() <= lastExprConstant);
341 return *reinterpret_cast<Expr **>(I);
342 }
343 };
344
345 /// Const iterator for iterating over Stmt * arrays that contain only Expr *
346 struct ConstExprIterator
347 : llvm::iterator_adaptor_base<ConstExprIterator, const Stmt *const *,
348 std::random_access_iterator_tag,
349 const Expr *const> {
350 ConstExprIterator() : iterator_adaptor_base(nullptr) {}
351 ConstExprIterator(const Stmt *const *I) : iterator_adaptor_base(I) {}
352
353 reference operator*() const {
354 assert((*I)->getStmtClass() >= firstExprConstant &&
355 (*I)->getStmtClass() <= lastExprConstant);
356 return *reinterpret_cast<const Expr *const *>(I);
357 }
358 };
359
360private:
361 /// \brief Whether statistic collection is enabled.
362 static bool StatisticsEnabled;
363
364protected:
365 /// \brief Construct an empty statement.
366 explicit Stmt(StmtClass SC, EmptyShell) : Stmt(SC) {}
367
368public:
369 Stmt(StmtClass SC) {
370 static_assert(sizeof(*this) == sizeof(void *),
371 "changing bitfields changed sizeof(Stmt)");
372 static_assert(sizeof(*this) % alignof(void *) == 0,
373 "Insufficient alignment!");
374 StmtBits.sClass = SC;
375 if (StatisticsEnabled) Stmt::addStmtClass(SC);
376 }
377
378 StmtClass getStmtClass() const {
379 return static_cast<StmtClass>(StmtBits.sClass);
380 }
381
382 const char *getStmtClassName() const;
383
384 /// SourceLocation tokens are not useful in isolation - they are low level
385 /// value objects created/interpreted by SourceManager. We assume AST
386 /// clients will have a pointer to the respective SourceManager.
387 SourceRange getSourceRange() const LLVM_READONLY;
388 SourceLocation getLocStart() const LLVM_READONLY;
389 SourceLocation getLocEnd() const LLVM_READONLY;
390
391 // global temp stats (until we have a per-module visitor)
392 static void addStmtClass(const StmtClass s);
393 static void EnableStatistics();
394 static void PrintStats();
395
396 /// \brief Dumps the specified AST fragment and all subtrees to
397 /// \c llvm::errs().
398 void dump() const;
399 void dump(SourceManager &SM) const;
400 void dump(raw_ostream &OS, SourceManager &SM) const;
401 void dump(raw_ostream &OS) const;
402
403 /// dumpColor - same as dump(), but forces color highlighting.
404 void dumpColor() const;
405
406 /// dumpPretty/printPretty - These two methods do a "pretty print" of the AST
407 /// back to its original source language syntax.
408 void dumpPretty(const ASTContext &Context) const;
409 void printPretty(raw_ostream &OS, PrinterHelper *Helper,
410 const PrintingPolicy &Policy, unsigned Indentation = 0,
411 const ASTContext *Context = nullptr) const;
412
413 /// viewAST - Visualize an AST rooted at this Stmt* using GraphViz. Only
414 /// works on systems with GraphViz (Mac OS X) or dot+gv installed.
415 void viewAST() const;
416
417 /// Skip past any implicit AST nodes which might surround this
418 /// statement, such as ExprWithCleanups or ImplicitCastExpr nodes.
419 Stmt *IgnoreImplicit();
420 const Stmt *IgnoreImplicit() const {
421 return const_cast<Stmt *>(this)->IgnoreImplicit();
422 }
423
424 /// \brief Skip no-op (attributed, compound) container stmts and skip captured
425 /// stmt at the top, if \a IgnoreCaptured is true.
426 Stmt *IgnoreContainers(bool IgnoreCaptured = false);
427 const Stmt *IgnoreContainers(bool IgnoreCaptured = false) const {
428 return const_cast<Stmt *>(this)->IgnoreContainers(IgnoreCaptured);
429 }
430
431 const Stmt *stripLabelLikeStatements() const;
432 Stmt *stripLabelLikeStatements() {
433 return const_cast<Stmt*>(
434 const_cast<const Stmt*>(this)->stripLabelLikeStatements());
435 }
436
437 /// Child Iterators: All subclasses must implement 'children'
438 /// to permit easy iteration over the substatements/subexpessions of an
439 /// AST node. This permits easy iteration over all nodes in the AST.
440 using child_iterator = StmtIterator;
441 using const_child_iterator = ConstStmtIterator;
442
443 using child_range = llvm::iterator_range<child_iterator>;
444 using const_child_range = llvm::iterator_range<const_child_iterator>;
445
446 child_range children();
447 const_child_range children() const {
448 auto Children = const_cast<Stmt *>(this)->children();
449 return const_child_range(Children.begin(), Children.end());
450 }
451
452 child_iterator child_begin() { return children().begin(); }
453 child_iterator child_end() { return children().end(); }
454
455 const_child_iterator child_begin() const { return children().begin(); }
456 const_child_iterator child_end() const { return children().end(); }
457
458 /// \brief Produce a unique representation of the given statement.
459 ///
460 /// \param ID once the profiling operation is complete, will contain
461 /// the unique representation of the given statement.
462 ///
463 /// \param Context the AST context in which the statement resides
464 ///
465 /// \param Canonical whether the profile should be based on the canonical
466 /// representation of this statement (e.g., where non-type template
467 /// parameters are identified by index/level rather than their
468 /// declaration pointers) or the exact representation of the statement as
469 /// written in the source.
470 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
471 bool Canonical) const;
472
473 /// \brief Calculate a unique representation for a statement that is
474 /// stable across compiler invocations.
475 ///
476 /// \param ID profile information will be stored in ID.
477 ///
478 /// \param Hash an ODRHash object which will be called where pointers would
479 /// have been used in the Profile function.
480 void ProcessODRHash(llvm::FoldingSetNodeID &ID, ODRHash& Hash) const;
481};
482
483/// DeclStmt - Adaptor class for mixing declarations with statements and
484/// expressions. For example, CompoundStmt mixes statements, expressions
485/// and declarations (variables, types). Another example is ForStmt, where
486/// the first statement can be an expression or a declaration.
487class DeclStmt : public Stmt {
488 DeclGroupRef DG;
489 SourceLocation StartLoc, EndLoc;
490
491public:
492 DeclStmt(DeclGroupRef dg, SourceLocation startLoc, SourceLocation endLoc)
493 : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {}
494
495 /// \brief Build an empty declaration statement.
496 explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) {}
497
498 /// isSingleDecl - This method returns true if this DeclStmt refers
499 /// to a single Decl.
500 bool isSingleDecl() const {
501 return DG.isSingleDecl();
502 }
503
504 const Decl *getSingleDecl() const { return DG.getSingleDecl(); }
505 Decl *getSingleDecl() { return DG.getSingleDecl(); }
506
507 const DeclGroupRef getDeclGroup() const { return DG; }
508 DeclGroupRef getDeclGroup() { return DG; }
509 void setDeclGroup(DeclGroupRef DGR) { DG = DGR; }
510
511 SourceLocation getStartLoc() const { return StartLoc; }
512 void setStartLoc(SourceLocation L) { StartLoc = L; }
513 SourceLocation getEndLoc() const { return EndLoc; }
514 void setEndLoc(SourceLocation L) { EndLoc = L; }
515
516 SourceLocation getLocStart() const LLVM_READONLY { return StartLoc; }
517 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
518
519 static bool classof(const Stmt *T) {
520 return T->getStmtClass() == DeclStmtClass;
521 }
522
523 // Iterators over subexpressions.
524 child_range children() {
525 return child_range(child_iterator(DG.begin(), DG.end()),
526 child_iterator(DG.end(), DG.end()));
527 }
528
529 using decl_iterator = DeclGroupRef::iterator;
530 using const_decl_iterator = DeclGroupRef::const_iterator;
531 using decl_range = llvm::iterator_range<decl_iterator>;
532 using decl_const_range = llvm::iterator_range<const_decl_iterator>;
533
534 decl_range decls() { return decl_range(decl_begin(), decl_end()); }
535 decl_const_range decls() const {
536 return decl_const_range(decl_begin(), decl_end());
537 }
538 decl_iterator decl_begin() { return DG.begin(); }
539 decl_iterator decl_end() { return DG.end(); }
540 const_decl_iterator decl_begin() const { return DG.begin(); }
541 const_decl_iterator decl_end() const { return DG.end(); }
542
543 using reverse_decl_iterator = std::reverse_iterator<decl_iterator>;
544
545 reverse_decl_iterator decl_rbegin() {
546 return reverse_decl_iterator(decl_end());
547 }
548
549 reverse_decl_iterator decl_rend() {
550 return reverse_decl_iterator(decl_begin());
551 }
552};
553
554/// NullStmt - This is the null statement ";": C99 6.8.3p3.
555///
556class NullStmt : public Stmt {
557 SourceLocation SemiLoc;
558
559 /// \brief True if the null statement was preceded by an empty macro, e.g:
560 /// @code
561 /// #define CALL(x)
562 /// CALL(0);
563 /// @endcode
564 bool HasLeadingEmptyMacro = false;
565
566public:
567 friend class ASTStmtReader;
568 friend class ASTStmtWriter;
569
570 NullStmt(SourceLocation L, bool hasLeadingEmptyMacro = false)
571 : Stmt(NullStmtClass), SemiLoc(L),
572 HasLeadingEmptyMacro(hasLeadingEmptyMacro) {}
573
574 /// \brief Build an empty null statement.
575 explicit NullStmt(EmptyShell Empty) : Stmt(NullStmtClass, Empty) {}
576
577 SourceLocation getSemiLoc() const { return SemiLoc; }
578 void setSemiLoc(SourceLocation L) { SemiLoc = L; }
579
580 bool hasLeadingEmptyMacro() const { return HasLeadingEmptyMacro; }
581
582 SourceLocation getLocStart() const LLVM_READONLY { return SemiLoc; }
583 SourceLocation getLocEnd() const LLVM_READONLY { return SemiLoc; }
584
585 static bool classof(const Stmt *T) {
586 return T->getStmtClass() == NullStmtClass;
587 }
588
589 child_range children() {
590 return child_range(child_iterator(), child_iterator());
591 }
592};
593
594/// CompoundStmt - This represents a group of statements like { stmt stmt }.
595class CompoundStmt : public Stmt {
596 friend class ASTStmtReader;
597
598 Stmt** Body = nullptr;
599 SourceLocation LBraceLoc, RBraceLoc;
600
601public:
602 CompoundStmt(const ASTContext &C, ArrayRef<Stmt*> Stmts,
603 SourceLocation LB, SourceLocation RB);
604
605 // \brief Build an empty compound statement with a location.
606 explicit CompoundStmt(SourceLocation Loc)
607 : Stmt(CompoundStmtClass), LBraceLoc(Loc), RBraceLoc(Loc) {
608 CompoundStmtBits.NumStmts = 0;
609 }
610
611 // \brief Build an empty compound statement.
612 explicit CompoundStmt(EmptyShell Empty) : Stmt(CompoundStmtClass, Empty) {
613 CompoundStmtBits.NumStmts = 0;
614 }
615
616 void setStmts(const ASTContext &C, ArrayRef<Stmt *> Stmts);
617
618 bool body_empty() const { return CompoundStmtBits.NumStmts == 0; }
619 unsigned size() const { return CompoundStmtBits.NumStmts; }
620
621 using body_iterator = Stmt **;
622 using body_range = llvm::iterator_range<body_iterator>;
623
624 body_range body() { return body_range(body_begin(), body_end()); }
625 body_iterator body_begin() { return Body; }
626 body_iterator body_end() { return Body + size(); }
627 Stmt *body_front() { return !body_empty() ? Body[0] : nullptr; }
628 Stmt *body_back() { return !body_empty() ? Body[size()-1] : nullptr; }
629
630 void setLastStmt(Stmt *S) {
631 assert(!body_empty() && "setLastStmt");
632 Body[size()-1] = S;
633 }
634
635 using const_body_iterator = Stmt* const *;
636 using body_const_range = llvm::iterator_range<const_body_iterator>;
637
638 body_const_range body() const {
639 return body_const_range(body_begin(), body_end());
640 }
641
642 const_body_iterator body_begin() const { return Body; }
643 const_body_iterator body_end() const { return Body + size(); }
644
645 const Stmt *body_front() const {
646 return !body_empty() ? Body[0] : nullptr;
647 }
648
649 const Stmt *body_back() const {
650 return !body_empty() ? Body[size() - 1] : nullptr;
651 }
652
653 using reverse_body_iterator = std::reverse_iterator<body_iterator>;
654
655 reverse_body_iterator body_rbegin() {
656 return reverse_body_iterator(body_end());
657 }
658
659 reverse_body_iterator body_rend() {
660 return reverse_body_iterator(body_begin());
661 }
662
663 using const_reverse_body_iterator =
664 std::reverse_iterator<const_body_iterator>;
665
666 const_reverse_body_iterator body_rbegin() const {
667 return const_reverse_body_iterator(body_end());
668 }
669
670 const_reverse_body_iterator body_rend() const {
671 return const_reverse_body_iterator(body_begin());
672 }
673
674 SourceLocation getLocStart() const LLVM_READONLY { return LBraceLoc; }
675 SourceLocation getLocEnd() const LLVM_READONLY { return RBraceLoc; }
676
677 SourceLocation getLBracLoc() const { return LBraceLoc; }
678 SourceLocation getRBracLoc() const { return RBraceLoc; }
679
680 static bool classof(const Stmt *T) {
681 return T->getStmtClass() == CompoundStmtClass;
682 }
683
684 // Iterators
685 child_range children() {
686 return child_range(Body, Body + CompoundStmtBits.NumStmts);
687 }
688
689 const_child_range children() const {
690 return const_child_range(child_iterator(Body),
691 child_iterator(Body + CompoundStmtBits.NumStmts));
692 }
693};
694
695// SwitchCase is the base class for CaseStmt and DefaultStmt,
696class SwitchCase : public Stmt {
697protected:
698 // A pointer to the following CaseStmt or DefaultStmt class,
699 // used by SwitchStmt.
700 SwitchCase *NextSwitchCase = nullptr;
701 SourceLocation KeywordLoc;
702 SourceLocation ColonLoc;
703
704 SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc)
705 : Stmt(SC), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {}
706
707 SwitchCase(StmtClass SC, EmptyShell) : Stmt(SC) {}
708
709public:
710 const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; }
711
712 SwitchCase *getNextSwitchCase() { return NextSwitchCase; }
713
714 void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; }
715
716 SourceLocation getKeywordLoc() const { return KeywordLoc; }
717 void setKeywordLoc(SourceLocation L) { KeywordLoc = L; }
718 SourceLocation getColonLoc() const { return ColonLoc; }
719 void setColonLoc(SourceLocation L) { ColonLoc = L; }
720
721 Stmt *getSubStmt();
722 const Stmt *getSubStmt() const {
723 return const_cast<SwitchCase*>(this)->getSubStmt();
724 }
725
726 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
727 SourceLocation getLocEnd() const LLVM_READONLY;
728
729 static bool classof(const Stmt *T) {
730 return T->getStmtClass() == CaseStmtClass ||
731 T->getStmtClass() == DefaultStmtClass;
732 }
733};
734
735class CaseStmt : public SwitchCase {
736 SourceLocation EllipsisLoc;
737 enum { LHS, RHS, SUBSTMT, END_EXPR };
738 Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for
739 // GNU "case 1 ... 4" extension
740
741public:
742 CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc,
743 SourceLocation ellipsisLoc, SourceLocation colonLoc)
744 : SwitchCase(CaseStmtClass, caseLoc, colonLoc) {
745 SubExprs[SUBSTMT] = nullptr;
746 SubExprs[LHS] = reinterpret_cast<Stmt*>(lhs);
747 SubExprs[RHS] = reinterpret_cast<Stmt*>(rhs);
748 EllipsisLoc = ellipsisLoc;
749 }
750
751 /// \brief Build an empty switch case statement.
752 explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) {}
753
754 SourceLocation getCaseLoc() const { return KeywordLoc; }
755 void setCaseLoc(SourceLocation L) { KeywordLoc = L; }
756 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
757 void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; }
758 SourceLocation getColonLoc() const { return ColonLoc; }
759 void setColonLoc(SourceLocation L) { ColonLoc = L; }
760
761 Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
762 Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
763 Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }
764
765 const Expr *getLHS() const {
766 return reinterpret_cast<const Expr*>(SubExprs[LHS]);
767 }
768
769 const Expr *getRHS() const {
770 return reinterpret_cast<const Expr*>(SubExprs[RHS]);
771 }
772
773 const Stmt *getSubStmt() const { return SubExprs[SUBSTMT]; }
774
775 void setSubStmt(Stmt *S) { SubExprs[SUBSTMT] = S; }
776 void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast<Stmt*>(Val); }
777 void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast<Stmt*>(Val); }
778
779 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
780
781 SourceLocation getLocEnd() const LLVM_READONLY {
782 // Handle deeply nested case statements with iteration instead of recursion.
783 const CaseStmt *CS = this;
784 while (const CaseStmt *CS2 = dyn_cast<CaseStmt>(CS->getSubStmt()))
785 CS = CS2;
786
787 return CS->getSubStmt()->getLocEnd();
788 }
789
790 static bool classof(const Stmt *T) {
791 return T->getStmtClass() == CaseStmtClass;
792 }
793
794 // Iterators
795 child_range children() {
796 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
797 }
798};
799
800class DefaultStmt : public SwitchCase {
801 Stmt* SubStmt;
802
803public:
804 DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) :
805 SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {}
806
807 /// \brief Build an empty default statement.
808 explicit DefaultStmt(EmptyShell Empty)
809 : SwitchCase(DefaultStmtClass, Empty) {}
810
811 Stmt *getSubStmt() { return SubStmt; }
812 const Stmt *getSubStmt() const { return SubStmt; }
813 void setSubStmt(Stmt *S) { SubStmt = S; }
814
815 SourceLocation getDefaultLoc() const { return KeywordLoc; }
816 void setDefaultLoc(SourceLocation L) { KeywordLoc = L; }
817 SourceLocation getColonLoc() const { return ColonLoc; }
818 void setColonLoc(SourceLocation L) { ColonLoc = L; }
819
820 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
821 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
822
823 static bool classof(const Stmt *T) {
824 return T->getStmtClass() == DefaultStmtClass;
825 }
826
827 // Iterators
828 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
829};
830
831inline SourceLocation SwitchCase::getLocEnd() const {
832 if (const CaseStmt *CS = dyn_cast<CaseStmt>(this))
833 return CS->getLocEnd();
834 return cast<DefaultStmt>(this)->getLocEnd();
835}
836
837/// LabelStmt - Represents a label, which has a substatement. For example:
838/// foo: return;
839class LabelStmt : public Stmt {
840 SourceLocation IdentLoc;
841 LabelDecl *TheDecl;
842 Stmt *SubStmt;
843
844public:
845 LabelStmt(SourceLocation IL, LabelDecl *D, Stmt *substmt)
846 : Stmt(LabelStmtClass), IdentLoc(IL), TheDecl(D), SubStmt(substmt) {
847 static_assert(sizeof(LabelStmt) ==
848 2 * sizeof(SourceLocation) + 2 * sizeof(void *),
849 "LabelStmt too big");
850 }
851
852 // \brief Build an empty label statement.
853 explicit LabelStmt(EmptyShell Empty) : Stmt(LabelStmtClass, Empty) {}
854
855 SourceLocation getIdentLoc() const { return IdentLoc; }
856 LabelDecl *getDecl() const { return TheDecl; }
857 void setDecl(LabelDecl *D) { TheDecl = D; }
858 const char *getName() const;
859 Stmt *getSubStmt() { return SubStmt; }
860 const Stmt *getSubStmt() const { return SubStmt; }
861 void setIdentLoc(SourceLocation L) { IdentLoc = L; }
862 void setSubStmt(Stmt *SS) { SubStmt = SS; }
863
864 SourceLocation getLocStart() const LLVM_READONLY { return IdentLoc; }
865 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
866
867 child_range children() { return child_range(&SubStmt, &SubStmt+1); }
868
869 static bool classof(const Stmt *T) {
870 return T->getStmtClass() == LabelStmtClass;
871 }
872};
873
874/// \brief Represents an attribute applied to a statement.
875///
876/// Represents an attribute applied to a statement. For example:
877/// [[omp::for(...)]] for (...) { ... }
878class AttributedStmt : public Stmt {
879 friend class ASTStmtReader;
880
881 Stmt *SubStmt;
882 SourceLocation AttrLoc;
883 unsigned NumAttrs;
884
885 AttributedStmt(SourceLocation Loc, ArrayRef<const Attr*> Attrs, Stmt *SubStmt)
886 : Stmt(AttributedStmtClass), SubStmt(SubStmt), AttrLoc(Loc),
887 NumAttrs(Attrs.size()) {
888 std::copy(Attrs.begin(), Attrs.end(), getAttrArrayPtr());
889 }
890
891 explicit AttributedStmt(EmptyShell Empty, unsigned NumAttrs)
892 : Stmt(AttributedStmtClass, Empty), NumAttrs(NumAttrs) {
893 std::fill_n(getAttrArrayPtr(), NumAttrs, nullptr);
894 }
895
896 const Attr *const *getAttrArrayPtr() const {
897 return reinterpret_cast<const Attr *const *>(this + 1);
898 }
899 const Attr **getAttrArrayPtr() {
900 return reinterpret_cast<const Attr **>(this + 1);
901 }
902
903public:
904 static AttributedStmt *Create(const ASTContext &C, SourceLocation Loc,
905 ArrayRef<const Attr*> Attrs, Stmt *SubStmt);
906
907 // \brief Build an empty attributed statement.
908 static AttributedStmt *CreateEmpty(const ASTContext &C, unsigned NumAttrs);
909
910 SourceLocation getAttrLoc() const { return AttrLoc; }
911 ArrayRef<const Attr*> getAttrs() const {
912 return llvm::makeArrayRef(getAttrArrayPtr(), NumAttrs);
913 }
914
915 Stmt *getSubStmt() { return SubStmt; }
916 const Stmt *getSubStmt() const { return SubStmt; }
917
918 SourceLocation getLocStart() const LLVM_READONLY { return AttrLoc; }
919 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
920
921 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
922
923 static bool classof(const Stmt *T) {
924 return T->getStmtClass() == AttributedStmtClass;
925 }
926};
927
928/// IfStmt - This represents an if/then/else.
929class IfStmt : public Stmt {
930 enum { INIT, VAR, COND, THEN, ELSE, END_EXPR };
931 Stmt* SubExprs[END_EXPR];
932
933 SourceLocation IfLoc;
934 SourceLocation ElseLoc;
935
936public:
937 IfStmt(const ASTContext &C, SourceLocation IL,
938 bool IsConstexpr, Stmt *init, VarDecl *var, Expr *cond,
939 Stmt *then, SourceLocation EL = SourceLocation(),
940 Stmt *elsev = nullptr);
941
942 /// \brief Build an empty if/then/else statement
943 explicit IfStmt(EmptyShell Empty) : Stmt(IfStmtClass, Empty) {}
944
945 /// \brief Retrieve the variable declared in this "if" statement, if any.
946 ///
947 /// In the following example, "x" is the condition variable.
948 /// \code
949 /// if (int x = foo()) {
950 /// printf("x is %d", x);
951 /// }
952 /// \endcode
953 VarDecl *getConditionVariable() const;
954 void setConditionVariable(const ASTContext &C, VarDecl *V);
955
956 /// If this IfStmt has a condition variable, return the faux DeclStmt
957 /// associated with the creation of that condition variable.
958 const DeclStmt *getConditionVariableDeclStmt() const {
959 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
960 }
961
962 Stmt *getInit() { return SubExprs[INIT]; }
963 const Stmt *getInit() const { return SubExprs[INIT]; }
964 void setInit(Stmt *S) { SubExprs[INIT] = S; }
965 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
966 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
967 const Stmt *getThen() const { return SubExprs[THEN]; }
968 void setThen(Stmt *S) { SubExprs[THEN] = S; }
969 const Stmt *getElse() const { return SubExprs[ELSE]; }
970 void setElse(Stmt *S) { SubExprs[ELSE] = S; }
971
972 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
973 Stmt *getThen() { return SubExprs[THEN]; }
974 Stmt *getElse() { return SubExprs[ELSE]; }
975
976 SourceLocation getIfLoc() const { return IfLoc; }
977 void setIfLoc(SourceLocation L) { IfLoc = L; }
978 SourceLocation getElseLoc() const { return ElseLoc; }
979 void setElseLoc(SourceLocation L) { ElseLoc = L; }
980
981 bool isConstexpr() const { return IfStmtBits.IsConstexpr; }
982 void setConstexpr(bool C) { IfStmtBits.IsConstexpr = C; }
983
984 bool isObjCAvailabilityCheck() const;
985
986 SourceLocation getLocStart() const LLVM_READONLY { return IfLoc; }
987
988 SourceLocation getLocEnd() const LLVM_READONLY {
989 if (SubExprs[ELSE])
990 return SubExprs[ELSE]->getLocEnd();
991 else
992 return SubExprs[THEN]->getLocEnd();
993 }
994
995 // Iterators over subexpressions. The iterators will include iterating
996 // over the initialization expression referenced by the condition variable.
997 child_range children() {
998 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
999 }
1000
1001 static bool classof(const Stmt *T) {
1002 return T->getStmtClass() == IfStmtClass;
1003 }
1004};
1005
1006/// SwitchStmt - This represents a 'switch' stmt.
1007class SwitchStmt : public Stmt {
1008 SourceLocation SwitchLoc;
1009 enum { INIT, VAR, COND, BODY, END_EXPR };
1010 Stmt* SubExprs[END_EXPR];
1011
1012 // This points to a linked list of case and default statements and, if the
1013 // SwitchStmt is a switch on an enum value, records whether all the enum
1014 // values were covered by CaseStmts. The coverage information value is meant
1015 // to be a hint for possible clients.
1016 llvm::PointerIntPair<SwitchCase *, 1, bool> FirstCase;
1017
1018public:
1019 SwitchStmt(const ASTContext &C, Stmt *Init, VarDecl *Var, Expr *cond);
1020
1021 /// \brief Build a empty switch statement.
1022 explicit SwitchStmt(EmptyShell Empty) : Stmt(SwitchStmtClass, Empty) {}
1023
1024 /// \brief Retrieve the variable declared in this "switch" statement, if any.
1025 ///
1026 /// In the following example, "x" is the condition variable.
1027 /// \code
1028 /// switch (int x = foo()) {
1029 /// case 0: break;
1030 /// // ...
1031 /// }
1032 /// \endcode
1033 VarDecl *getConditionVariable() const;
1034 void setConditionVariable(const ASTContext &C, VarDecl *V);
1035
1036 /// If this SwitchStmt has a condition variable, return the faux DeclStmt
1037 /// associated with the creation of that condition variable.
1038 const DeclStmt *getConditionVariableDeclStmt() const {
1039 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1040 }
1041
1042 Stmt *getInit() { return SubExprs[INIT]; }
1043 const Stmt *getInit() const { return SubExprs[INIT]; }
1044 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1045 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1046 const Stmt *getBody() const { return SubExprs[BODY]; }
1047 const SwitchCase *getSwitchCaseList() const { return FirstCase.getPointer(); }
1048
1049 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1050 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt *>(E); }
1051 Stmt *getBody() { return SubExprs[BODY]; }
1052 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1053 SwitchCase *getSwitchCaseList() { return FirstCase.getPointer(); }
1054
1055 /// \brief Set the case list for this switch statement.
1056 void setSwitchCaseList(SwitchCase *SC) { FirstCase.setPointer(SC); }
1057
1058 SourceLocation getSwitchLoc() const { return SwitchLoc; }
1059 void setSwitchLoc(SourceLocation L) { SwitchLoc = L; }
1060
1061 void setBody(Stmt *S, SourceLocation SL) {
1062 SubExprs[BODY] = S;
1063 SwitchLoc = SL;
1064 }
1065
1066 void addSwitchCase(SwitchCase *SC) {
1067 assert(!SC->getNextSwitchCase()
1068 && "case/default already added to a switch");
1069 SC->setNextSwitchCase(FirstCase.getPointer());
1070 FirstCase.setPointer(SC);
1071 }
1072
1073 /// Set a flag in the SwitchStmt indicating that if the 'switch (X)' is a
1074 /// switch over an enum value then all cases have been explicitly covered.
1075 void setAllEnumCasesCovered() { FirstCase.setInt(true); }
1076
1077 /// Returns true if the SwitchStmt is a switch of an enum value and all cases
1078 /// have been explicitly covered.
1079 bool isAllEnumCasesCovered() const { return FirstCase.getInt(); }
1080
1081 SourceLocation getLocStart() const LLVM_READONLY { return SwitchLoc; }
1082
1083 SourceLocation getLocEnd() const LLVM_READONLY {
1084 return SubExprs[BODY] ? SubExprs[BODY]->getLocEnd() : SubExprs[COND]->getLocEnd();
1085 }
1086
1087 // Iterators
1088 child_range children() {
1089 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1090 }
1091
1092 static bool classof(const Stmt *T) {
1093 return T->getStmtClass() == SwitchStmtClass;
1094 }
1095};
1096
1097/// WhileStmt - This represents a 'while' stmt.
1098class WhileStmt : public Stmt {
1099 SourceLocation WhileLoc;
1100 enum { VAR, COND, BODY, END_EXPR };
1101 Stmt* SubExprs[END_EXPR];
1102
1103public:
1104 WhileStmt(const ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
1105 SourceLocation WL);
1106
1107 /// \brief Build an empty while statement.
1108 explicit WhileStmt(EmptyShell Empty) : Stmt(WhileStmtClass, Empty) {}
1109
1110 /// \brief Retrieve the variable declared in this "while" statement, if any.
1111 ///
1112 /// In the following example, "x" is the condition variable.
1113 /// \code
1114 /// while (int x = random()) {
1115 /// // ...
1116 /// }
1117 /// \endcode
1118 VarDecl *getConditionVariable() const;
1119 void setConditionVariable(const ASTContext &C, VarDecl *V);
1120
1121 /// If this WhileStmt has a condition variable, return the faux DeclStmt
1122 /// associated with the creation of that condition variable.
1123 const DeclStmt *getConditionVariableDeclStmt() const {
1124 return reinterpret_cast<DeclStmt*>(SubExprs[VAR]);
1125 }
1126
1127 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1128 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1129 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1130 Stmt *getBody() { return SubExprs[BODY]; }
1131 const Stmt *getBody() const { return SubExprs[BODY]; }
1132 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1133
1134 SourceLocation getWhileLoc() const { return WhileLoc; }
1135 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1136
1137 SourceLocation getLocStart() const LLVM_READONLY { return WhileLoc; }
1138
1139 SourceLocation getLocEnd() const LLVM_READONLY {
1140 return SubExprs[BODY]->getLocEnd();
1141 }
1142
1143 static bool classof(const Stmt *T) {
1144 return T->getStmtClass() == WhileStmtClass;
1145 }
1146
1147 // Iterators
1148 child_range children() {
1149 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1150 }
1151};
1152
1153/// DoStmt - This represents a 'do/while' stmt.
1154class DoStmt : public Stmt {
1155 SourceLocation DoLoc;
1156 enum { BODY, COND, END_EXPR };
1157 Stmt* SubExprs[END_EXPR];
1158 SourceLocation WhileLoc;
1159 SourceLocation RParenLoc; // Location of final ')' in do stmt condition.
1160
1161public:
1162 DoStmt(Stmt *body, Expr *cond, SourceLocation DL, SourceLocation WL,
1163 SourceLocation RP)
1164 : Stmt(DoStmtClass), DoLoc(DL), WhileLoc(WL), RParenLoc(RP) {
1165 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
1166 SubExprs[BODY] = body;
1167 }
1168
1169 /// \brief Build an empty do-while statement.
1170 explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) {}
1171
1172 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1173 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1174 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1175 Stmt *getBody() { return SubExprs[BODY]; }
1176 const Stmt *getBody() const { return SubExprs[BODY]; }
1177 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1178
1179 SourceLocation getDoLoc() const { return DoLoc; }
1180 void setDoLoc(SourceLocation L) { DoLoc = L; }
1181 SourceLocation getWhileLoc() const { return WhileLoc; }
1182 void setWhileLoc(SourceLocation L) { WhileLoc = L; }
1183
1184 SourceLocation getRParenLoc() const { return RParenLoc; }
1185 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1186
1187 SourceLocation getLocStart() const LLVM_READONLY { return DoLoc; }
1188 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1189
1190 static bool classof(const Stmt *T) {
1191 return T->getStmtClass() == DoStmtClass;
1192 }
1193
1194 // Iterators
1195 child_range children() {
1196 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1197 }
1198};
1199
1200/// ForStmt - This represents a 'for (init;cond;inc)' stmt. Note that any of
1201/// the init/cond/inc parts of the ForStmt will be null if they were not
1202/// specified in the source.
1203class ForStmt : public Stmt {
1204 SourceLocation ForLoc;
1205 enum { INIT, CONDVAR, COND, INC, BODY, END_EXPR };
1206 Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt.
1207 SourceLocation LParenLoc, RParenLoc;
1208
1209public:
1210 ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1211 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1212 SourceLocation RP);
1213
1214 /// \brief Build an empty for statement.
1215 explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) {}
1216
1217 Stmt *getInit() { return SubExprs[INIT]; }
1218
1219 /// \brief Retrieve the variable declared in this "for" statement, if any.
1220 ///
1221 /// In the following example, "y" is the condition variable.
1222 /// \code
1223 /// for (int x = random(); int y = mangle(x); ++x) {
1224 /// // ...
1225 /// }
1226 /// \endcode
1227 VarDecl *getConditionVariable() const;
1228 void setConditionVariable(const ASTContext &C, VarDecl *V);
1229
1230 /// If this ForStmt has a condition variable, return the faux DeclStmt
1231 /// associated with the creation of that condition variable.
1232 const DeclStmt *getConditionVariableDeclStmt() const {
1233 return reinterpret_cast<DeclStmt*>(SubExprs[CONDVAR]);
1234 }
1235
1236 Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
1237 Expr *getInc() { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1238 Stmt *getBody() { return SubExprs[BODY]; }
1239
1240 const Stmt *getInit() const { return SubExprs[INIT]; }
1241 const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
1242 const Expr *getInc() const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
1243 const Stmt *getBody() const { return SubExprs[BODY]; }
1244
1245 void setInit(Stmt *S) { SubExprs[INIT] = S; }
1246 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
1247 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
1248 void setBody(Stmt *S) { SubExprs[BODY] = S; }
1249
1250 SourceLocation getForLoc() const { return ForLoc; }
1251 void setForLoc(SourceLocation L) { ForLoc = L; }
1252 SourceLocation getLParenLoc() const { return LParenLoc; }
1253 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
1254 SourceLocation getRParenLoc() const { return RParenLoc; }
1255 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1256
1257 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
1258
1259 SourceLocation getLocEnd() const LLVM_READONLY {
1260 return SubExprs[BODY]->getLocEnd();
1261 }
1262
1263 static bool classof(const Stmt *T) {
1264 return T->getStmtClass() == ForStmtClass;
1265 }
1266
1267 // Iterators
1268 child_range children() {
1269 return child_range(&SubExprs[0], &SubExprs[0]+END_EXPR);
1270 }
1271};
1272
1273/// GotoStmt - This represents a direct goto.
1274class GotoStmt : public Stmt {
1275 LabelDecl *Label;
1276 SourceLocation GotoLoc;
1277 SourceLocation LabelLoc;
1278
1279public:
1280 GotoStmt(LabelDecl *label, SourceLocation GL, SourceLocation LL)
1281 : Stmt(GotoStmtClass), Label(label), GotoLoc(GL), LabelLoc(LL) {}
1282
1283 /// \brief Build an empty goto statement.
1284 explicit GotoStmt(EmptyShell Empty) : Stmt(GotoStmtClass, Empty) {}
1285
1286 LabelDecl *getLabel() const { return Label; }
1287 void setLabel(LabelDecl *D) { Label = D; }
1288
1289 SourceLocation getGotoLoc() const { return GotoLoc; }
1290 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1291 SourceLocation getLabelLoc() const { return LabelLoc; }
1292 void setLabelLoc(SourceLocation L) { LabelLoc = L; }
1293
1294 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1295 SourceLocation getLocEnd() const LLVM_READONLY { return LabelLoc; }
1296
1297 static bool classof(const Stmt *T) {
1298 return T->getStmtClass() == GotoStmtClass;
1299 }
1300
1301 // Iterators
1302 child_range children() {
1303 return child_range(child_iterator(), child_iterator());
1304 }
1305};
1306
1307/// IndirectGotoStmt - This represents an indirect goto.
1308class IndirectGotoStmt : public Stmt {
1309 SourceLocation GotoLoc;
1310 SourceLocation StarLoc;
1311 Stmt *Target;
1312
1313public:
1314 IndirectGotoStmt(SourceLocation gotoLoc, SourceLocation starLoc,
1315 Expr *target)
1316 : Stmt(IndirectGotoStmtClass), GotoLoc(gotoLoc), StarLoc(starLoc),
1317 Target((Stmt*)target) {}
1318
1319 /// \brief Build an empty indirect goto statement.
1320 explicit IndirectGotoStmt(EmptyShell Empty)
1321 : Stmt(IndirectGotoStmtClass, Empty) {}
1322
1323 void setGotoLoc(SourceLocation L) { GotoLoc = L; }
1324 SourceLocation getGotoLoc() const { return GotoLoc; }
1325 void setStarLoc(SourceLocation L) { StarLoc = L; }
1326 SourceLocation getStarLoc() const { return StarLoc; }
1327
1328 Expr *getTarget() { return reinterpret_cast<Expr*>(Target); }
1329 const Expr *getTarget() const {return reinterpret_cast<const Expr*>(Target);}
1330 void setTarget(Expr *E) { Target = reinterpret_cast<Stmt*>(E); }
1331
1332 /// getConstantTarget - Returns the fixed target of this indirect
1333 /// goto, if one exists.
1334 LabelDecl *getConstantTarget();
1335 const LabelDecl *getConstantTarget() const {
1336 return const_cast<IndirectGotoStmt*>(this)->getConstantTarget();
1337 }
1338
1339 SourceLocation getLocStart() const LLVM_READONLY { return GotoLoc; }
1340 SourceLocation getLocEnd() const LLVM_READONLY { return Target->getLocEnd(); }
1341
1342 static bool classof(const Stmt *T) {
1343 return T->getStmtClass() == IndirectGotoStmtClass;
1344 }
1345
1346 // Iterators
1347 child_range children() { return child_range(&Target, &Target+1); }
1348};
1349
1350/// ContinueStmt - This represents a continue.
1351class ContinueStmt : public Stmt {
1352 SourceLocation ContinueLoc;
1353
1354public:
1355 ContinueStmt(SourceLocation CL) : Stmt(ContinueStmtClass), ContinueLoc(CL) {}
1356
1357 /// \brief Build an empty continue statement.
1358 explicit ContinueStmt(EmptyShell Empty) : Stmt(ContinueStmtClass, Empty) {}
1359
1360 SourceLocation getContinueLoc() const { return ContinueLoc; }
1361 void setContinueLoc(SourceLocation L) { ContinueLoc = L; }
1362
1363 SourceLocation getLocStart() const LLVM_READONLY { return ContinueLoc; }
1364 SourceLocation getLocEnd() const LLVM_READONLY { return ContinueLoc; }
1365
1366 static bool classof(const Stmt *T) {
1367 return T->getStmtClass() == ContinueStmtClass;
1368 }
1369
1370 // Iterators
1371 child_range children() {
1372 return child_range(child_iterator(), child_iterator());
1373 }
1374};
1375
1376/// BreakStmt - This represents a break.
1377class BreakStmt : public Stmt {
1378 SourceLocation BreakLoc;
1379
1380public:
1381 BreakStmt(SourceLocation BL) : Stmt(BreakStmtClass), BreakLoc(BL) {
1382 static_assert(sizeof(BreakStmt) == 2 * sizeof(SourceLocation),
1383 "BreakStmt too large");
1384 }
1385
1386 /// \brief Build an empty break statement.
1387 explicit BreakStmt(EmptyShell Empty) : Stmt(BreakStmtClass, Empty) {}
1388
1389 SourceLocation getBreakLoc() const { return BreakLoc; }
1390 void setBreakLoc(SourceLocation L) { BreakLoc = L; }
1391
1392 SourceLocation getLocStart() const LLVM_READONLY { return BreakLoc; }
1393 SourceLocation getLocEnd() const LLVM_READONLY { return BreakLoc; }
1394
1395 static bool classof(const Stmt *T) {
1396 return T->getStmtClass() == BreakStmtClass;
1397 }
1398
1399 // Iterators
1400 child_range children() {
1401 return child_range(child_iterator(), child_iterator());
1402 }
1403};
1404
1405/// ReturnStmt - This represents a return, optionally of an expression:
1406/// return;
1407/// return 4;
1408///
1409/// Note that GCC allows return with no argument in a function declared to
1410/// return a value, and it allows returning a value in functions declared to
1411/// return void. We explicitly model this in the AST, which means you can't
1412/// depend on the return type of the function and the presence of an argument.
1413class ReturnStmt : public Stmt {
1414 SourceLocation RetLoc;
1415 Stmt *RetExpr;
1416 const VarDecl *NRVOCandidate;
1417
1418public:
1419 explicit ReturnStmt(SourceLocation RL) : ReturnStmt(RL, nullptr, nullptr) {}
1420
1421 ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1422 : Stmt(ReturnStmtClass), RetLoc(RL), RetExpr((Stmt *)E),
1423 NRVOCandidate(NRVOCandidate) {}
1424
1425 /// \brief Build an empty return expression.
1426 explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) {}
1427
1428 const Expr *getRetValue() const;
1429 Expr *getRetValue();
1430 void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); }
1431
1432 SourceLocation getReturnLoc() const { return RetLoc; }
1433 void setReturnLoc(SourceLocation L) { RetLoc = L; }
1434
1435 /// \brief Retrieve the variable that might be used for the named return
1436 /// value optimization.
1437 ///
1438 /// The optimization itself can only be performed if the variable is
1439 /// also marked as an NRVO object.
1440 const VarDecl *getNRVOCandidate() const { return NRVOCandidate; }
1441 void setNRVOCandidate(const VarDecl *Var) { NRVOCandidate = Var; }
1442
1443 SourceLocation getLocStart() const LLVM_READONLY { return RetLoc; }
1444
1445 SourceLocation getLocEnd() const LLVM_READONLY {
1446 return RetExpr ? RetExpr->getLocEnd() : RetLoc;
1447 }
1448
1449 static bool classof(const Stmt *T) {
1450 return T->getStmtClass() == ReturnStmtClass;
1451 }
1452
1453 // Iterators
1454 child_range children() {
1455 if (RetExpr) return child_range(&RetExpr, &RetExpr+1);
1456 return child_range(child_iterator(), child_iterator());
1457 }
1458};
1459
1460/// AsmStmt is the base class for GCCAsmStmt and MSAsmStmt.
1461class AsmStmt : public Stmt {
1462protected:
1463 friend class ASTStmtReader;
1464
1465 SourceLocation AsmLoc;
1466
1467 /// \brief True if the assembly statement does not have any input or output
1468 /// operands.
1469 bool IsSimple;
1470
1471 /// \brief If true, treat this inline assembly as having side effects.
1472 /// This assembly statement should not be optimized, deleted or moved.
1473 bool IsVolatile;
1474
1475 unsigned NumOutputs;
1476 unsigned NumInputs;
1477 unsigned NumClobbers;
1478
1479 Stmt **Exprs = nullptr;
1480
1481 AsmStmt(StmtClass SC, SourceLocation asmloc, bool issimple, bool isvolatile,
1482 unsigned numoutputs, unsigned numinputs, unsigned numclobbers)
1483 : Stmt (SC), AsmLoc(asmloc), IsSimple(issimple), IsVolatile(isvolatile),
1484 NumOutputs(numoutputs), NumInputs(numinputs),
1485 NumClobbers(numclobbers) {}
1486
1487public:
1488 /// \brief Build an empty inline-assembly statement.
1489 explicit AsmStmt(StmtClass SC, EmptyShell Empty) : Stmt(SC, Empty) {}
1490
1491 SourceLocation getAsmLoc() const { return AsmLoc; }
1492 void setAsmLoc(SourceLocation L) { AsmLoc = L; }
1493
1494 bool isSimple() const { return IsSimple; }
1495 void setSimple(bool V) { IsSimple = V; }
1496
1497 bool isVolatile() const { return IsVolatile; }
1498 void setVolatile(bool V) { IsVolatile = V; }
1499
1500 SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); }
1501 SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); }
1502
1503 //===--- Asm String Analysis ---===//
1504
1505 /// Assemble final IR asm string.
1506 std::string generateAsmString(const ASTContext &C) const;
1507
1508 //===--- Output operands ---===//
1509
1510 unsigned getNumOutputs() const { return NumOutputs; }
1511
1512 /// getOutputConstraint - Return the constraint string for the specified
1513 /// output operand. All output constraints are known to be non-empty (either
1514 /// '=' or '+').
1515 StringRef getOutputConstraint(unsigned i) const;
1516
1517 /// isOutputPlusConstraint - Return true if the specified output constraint
1518 /// is a "+" constraint (which is both an input and an output) or false if it
1519 /// is an "=" constraint (just an output).
1520 bool isOutputPlusConstraint(unsigned i) const {
1521 return getOutputConstraint(i)[0] == '+';
1522 }
1523
1524 const Expr *getOutputExpr(unsigned i) const;
1525
1526 /// getNumPlusOperands - Return the number of output operands that have a "+"
1527 /// constraint.
1528 unsigned getNumPlusOperands() const;
1529
1530 //===--- Input operands ---===//
1531
1532 unsigned getNumInputs() const { return NumInputs; }
1533
1534 /// getInputConstraint - Return the specified input constraint. Unlike output
1535 /// constraints, these can be empty.
1536 StringRef getInputConstraint(unsigned i) const;
1537
1538 const Expr *getInputExpr(unsigned i) const;
1539
1540 //===--- Other ---===//
1541
1542 unsigned getNumClobbers() const { return NumClobbers; }
1543 StringRef getClobber(unsigned i) const;
1544
1545 static bool classof(const Stmt *T) {
1546 return T->getStmtClass() == GCCAsmStmtClass ||
1547 T->getStmtClass() == MSAsmStmtClass;
1548 }
1549
1550 // Input expr iterators.
1551
1552 using inputs_iterator = ExprIterator;
1553 using const_inputs_iterator = ConstExprIterator;
1554 using inputs_range = llvm::iterator_range<inputs_iterator>;
1555 using inputs_const_range = llvm::iterator_range<const_inputs_iterator>;
1556
1557 inputs_iterator begin_inputs() {
1558 return &Exprs[0] + NumOutputs;
1559 }
1560
1561 inputs_iterator end_inputs() {
1562 return &Exprs[0] + NumOutputs + NumInputs;
1563 }
1564
1565 inputs_range inputs() { return inputs_range(begin_inputs(), end_inputs()); }
1566
1567 const_inputs_iterator begin_inputs() const {
1568 return &Exprs[0] + NumOutputs;
1569 }
1570
1571 const_inputs_iterator end_inputs() const {
1572 return &Exprs[0] + NumOutputs + NumInputs;
1573 }
1574
1575 inputs_const_range inputs() const {
1576 return inputs_const_range(begin_inputs(), end_inputs());
1577 }
1578
1579 // Output expr iterators.
1580
1581 using outputs_iterator = ExprIterator;
1582 using const_outputs_iterator = ConstExprIterator;
1583 using outputs_range = llvm::iterator_range<outputs_iterator>;
1584 using outputs_const_range = llvm::iterator_range<const_outputs_iterator>;
1585
1586 outputs_iterator begin_outputs() {
1587 return &Exprs[0];
1588 }
1589
1590 outputs_iterator end_outputs() {
1591 return &Exprs[0] + NumOutputs;
1592 }
1593
1594 outputs_range outputs() {
1595 return outputs_range(begin_outputs(), end_outputs());
1596 }
1597
1598 const_outputs_iterator begin_outputs() const {
1599 return &Exprs[0];
1600 }
1601
1602 const_outputs_iterator end_outputs() const {
1603 return &Exprs[0] + NumOutputs;
1604 }
1605
1606 outputs_const_range outputs() const {
1607 return outputs_const_range(begin_outputs(), end_outputs());
1608 }
1609
1610 child_range children() {
1611 return child_range(&Exprs[0], &Exprs[0] + NumOutputs + NumInputs);
1612 }
1613};
1614
1615/// This represents a GCC inline-assembly statement extension.
1616class GCCAsmStmt : public AsmStmt {
1617 friend class ASTStmtReader;
1618
1619 SourceLocation RParenLoc;
1620 StringLiteral *AsmStr;
1621
1622 // FIXME: If we wanted to, we could allocate all of these in one big array.
1623 StringLiteral **Constraints = nullptr;
1624 StringLiteral **Clobbers = nullptr;
1625 IdentifierInfo **Names = nullptr;
1626
1627public:
1628 GCCAsmStmt(const ASTContext &C, SourceLocation asmloc, bool issimple,
1629 bool isvolatile, unsigned numoutputs, unsigned numinputs,
1630 IdentifierInfo **names, StringLiteral **constraints, Expr **exprs,
1631 StringLiteral *asmstr, unsigned numclobbers,
1632 StringLiteral **clobbers, SourceLocation rparenloc);
1633
1634 /// \brief Build an empty inline-assembly statement.
1635 explicit GCCAsmStmt(EmptyShell Empty) : AsmStmt(GCCAsmStmtClass, Empty) {}
1636
1637 SourceLocation getRParenLoc() const { return RParenLoc; }
1638 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
1639
1640 //===--- Asm String Analysis ---===//
1641
1642 const StringLiteral *getAsmString() const { return AsmStr; }
1643 StringLiteral *getAsmString() { return AsmStr; }
1644 void setAsmString(StringLiteral *E) { AsmStr = E; }
1645
1646 /// AsmStringPiece - this is part of a decomposed asm string specification
1647 /// (for use with the AnalyzeAsmString function below). An asm string is
1648 /// considered to be a concatenation of these parts.
1649 class AsmStringPiece {
1650 public:
1651 enum Kind {
1652 String, // String in .ll asm string form, "$" -> "$$" and "%%" -> "%".
1653 Operand // Operand reference, with optional modifier %c4.
1654 };
1655
1656 private:
1657 Kind MyKind;
1658 std::string Str;
1659 unsigned OperandNo;
1660
1661 // Source range for operand references.
1662 CharSourceRange Range;
1663
1664 public:
1665 AsmStringPiece(const std::string &S) : MyKind(String), Str(S) {}
1666 AsmStringPiece(unsigned OpNo, const std::string &S, SourceLocation Begin,
1667 SourceLocation End)
1668 : MyKind(Operand), Str(S), OperandNo(OpNo),
1669 Range(CharSourceRange::getCharRange(Begin, End)) {}
1670
1671 bool isString() const { return MyKind == String; }
1672 bool isOperand() const { return MyKind == Operand; }
1673
1674 const std::string &getString() const {
1675 return Str;
1676 }
1677
1678 unsigned getOperandNo() const {
1679 assert(isOperand());
1680 return OperandNo;
1681 }
1682
1683 CharSourceRange getRange() const {
1684 assert(isOperand() && "Range is currently used only for Operands.");
1685 return Range;
1686 }
1687
1688 /// getModifier - Get the modifier for this operand, if present. This
1689 /// returns '\0' if there was no modifier.
1690 char getModifier() const;
1691 };
1692
1693 /// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
1694 /// it into pieces. If the asm string is erroneous, emit errors and return
1695 /// true, otherwise return false. This handles canonicalization and
1696 /// translation of strings from GCC syntax to LLVM IR syntax, and handles
1697 //// flattening of named references like %[foo] to Operand AsmStringPiece's.
1698 unsigned AnalyzeAsmString(SmallVectorImpl<AsmStringPiece> &Pieces,
1699 const ASTContext &C, unsigned &DiagOffs) const;
1700
1701 /// Assemble final IR asm string.
1702 std::string generateAsmString(const ASTContext &C) const;
1703
1704 //===--- Output operands ---===//
1705
1706 IdentifierInfo *getOutputIdentifier(unsigned i) const {
1707 return Names[i];
1708 }
1709
1710 StringRef getOutputName(unsigned i) const {
1711 if (IdentifierInfo *II = getOutputIdentifier(i))
1712 return II->getName();
1713
1714 return StringRef();
1715 }
1716
1717 StringRef getOutputConstraint(unsigned i) const;
1718
1719 const StringLiteral *getOutputConstraintLiteral(unsigned i) const {
1720 return Constraints[i];
1721 }
1722 StringLiteral *getOutputConstraintLiteral(unsigned i) {
1723 return Constraints[i];
1724 }
1725
1726 Expr *getOutputExpr(unsigned i);
1727
1728 const Expr *getOutputExpr(unsigned i) const {
1729 return const_cast<GCCAsmStmt*>(this)->getOutputExpr(i);
1730 }
1731
1732 //===--- Input operands ---===//
1733
1734 IdentifierInfo *getInputIdentifier(unsigned i) const {
1735 return Names[i + NumOutputs];
1736 }
1737
1738 StringRef getInputName(unsigned i) const {
1739 if (IdentifierInfo *II = getInputIdentifier(i))
1740 return II->getName();
1741
1742 return StringRef();
1743 }
1744
1745 StringRef getInputConstraint(unsigned i) const;
1746
1747 const StringLiteral *getInputConstraintLiteral(unsigned i) const {
1748 return Constraints[i + NumOutputs];
1749 }
1750 StringLiteral *getInputConstraintLiteral(unsigned i) {
1751 return Constraints[i + NumOutputs];
1752 }
1753
1754 Expr *getInputExpr(unsigned i);
1755 void setInputExpr(unsigned i, Expr *E);
1756
1757 const Expr *getInputExpr(unsigned i) const {
1758 return const_cast<GCCAsmStmt*>(this)->getInputExpr(i);
1759 }
1760
1761private:
1762 void setOutputsAndInputsAndClobbers(const ASTContext &C,
1763 IdentifierInfo **Names,
1764 StringLiteral **Constraints,
1765 Stmt **Exprs,
1766 unsigned NumOutputs,
1767 unsigned NumInputs,
1768 StringLiteral **Clobbers,
1769 unsigned NumClobbers);
1770
1771public:
1772 //===--- Other ---===//
1773
1774 /// getNamedOperand - Given a symbolic operand reference like %[foo],
1775 /// translate this into a numeric value needed to reference the same operand.
1776 /// This returns -1 if the operand name is invalid.
1777 int getNamedOperand(StringRef SymbolicName) const;
1778
1779 StringRef getClobber(unsigned i) const;
1780
1781 StringLiteral *getClobberStringLiteral(unsigned i) { return Clobbers[i]; }
1782 const StringLiteral *getClobberStringLiteral(unsigned i) const {
1783 return Clobbers[i];
1784 }
1785
1786 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1787 SourceLocation getLocEnd() const LLVM_READONLY { return RParenLoc; }
1788
1789 static bool classof(const Stmt *T) {
1790 return T->getStmtClass() == GCCAsmStmtClass;
1791 }
1792};
1793
1794/// This represents a Microsoft inline-assembly statement extension.
1795class MSAsmStmt : public AsmStmt {
1796 friend class ASTStmtReader;
1797
1798 SourceLocation LBraceLoc, EndLoc;
1799 StringRef AsmStr;
1800
1801 unsigned NumAsmToks = 0;
1802
1803 Token *AsmToks = nullptr;
1804 StringRef *Constraints = nullptr;
1805 StringRef *Clobbers = nullptr;
1806
1807public:
1808 MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
1809 SourceLocation lbraceloc, bool issimple, bool isvolatile,
1810 ArrayRef<Token> asmtoks, unsigned numoutputs, unsigned numinputs,
1811 ArrayRef<StringRef> constraints,
1812 ArrayRef<Expr*> exprs, StringRef asmstr,
1813 ArrayRef<StringRef> clobbers, SourceLocation endloc);
1814
1815 /// \brief Build an empty MS-style inline-assembly statement.
1816 explicit MSAsmStmt(EmptyShell Empty) : AsmStmt(MSAsmStmtClass, Empty) {}
1817
1818 SourceLocation getLBraceLoc() const { return LBraceLoc; }
1819 void setLBraceLoc(SourceLocation L) { LBraceLoc = L; }
1820 SourceLocation getEndLoc() const { return EndLoc; }
1821 void setEndLoc(SourceLocation L) { EndLoc = L; }
1822
1823 bool hasBraces() const { return LBraceLoc.isValid(); }
1824
1825 unsigned getNumAsmToks() { return NumAsmToks; }
1826 Token *getAsmToks() { return AsmToks; }
1827
1828 //===--- Asm String Analysis ---===//
1829 StringRef getAsmString() const { return AsmStr; }
1830
1831 /// Assemble final IR asm string.
1832 std::string generateAsmString(const ASTContext &C) const;
1833
1834 //===--- Output operands ---===//
1835
1836 StringRef getOutputConstraint(unsigned i) const {
1837 assert(i < NumOutputs);
1838 return Constraints[i];
1839 }
1840
1841 Expr *getOutputExpr(unsigned i);
1842
1843 const Expr *getOutputExpr(unsigned i) const {
1844 return const_cast<MSAsmStmt*>(this)->getOutputExpr(i);
1845 }
1846
1847 //===--- Input operands ---===//
1848
1849 StringRef getInputConstraint(unsigned i) const {
1850 assert(i < NumInputs);
1851 return Constraints[i + NumOutputs];
1852 }
1853
1854 Expr *getInputExpr(unsigned i);
1855 void setInputExpr(unsigned i, Expr *E);
1856
1857 const Expr *getInputExpr(unsigned i) const {
1858 return const_cast<MSAsmStmt*>(this)->getInputExpr(i);
1859 }
1860
1861 //===--- Other ---===//
1862
1863 ArrayRef<StringRef> getAllConstraints() const {
1864 return llvm::makeArrayRef(Constraints, NumInputs + NumOutputs);
1865 }
1866
1867 ArrayRef<StringRef> getClobbers() const {
1868 return llvm::makeArrayRef(Clobbers, NumClobbers);
1869 }
1870
1871 ArrayRef<Expr*> getAllExprs() const {
1872 return llvm::makeArrayRef(reinterpret_cast<Expr**>(Exprs),
1873 NumInputs + NumOutputs);
1874 }
1875
1876 StringRef getClobber(unsigned i) const { return getClobbers()[i]; }
1877
1878private:
1879 void initialize(const ASTContext &C, StringRef AsmString,
1880 ArrayRef<Token> AsmToks, ArrayRef<StringRef> Constraints,
1881 ArrayRef<Expr*> Exprs, ArrayRef<StringRef> Clobbers);
1882
1883public:
1884 SourceLocation getLocStart() const LLVM_READONLY { return AsmLoc; }
1885 SourceLocation getLocEnd() const LLVM_READONLY { return EndLoc; }
1886
1887 static bool classof(const Stmt *T) {
1888 return T->getStmtClass() == MSAsmStmtClass;
1889 }
1890
1891 child_range children() {
1892 return child_range(&Exprs[0], &Exprs[NumInputs + NumOutputs]);
1893 }
1894};
1895
1896class SEHExceptStmt : public Stmt {
1897 friend class ASTReader;
1898 friend class ASTStmtReader;
1899
1900 SourceLocation Loc;
1901 Stmt *Children[2];
1902
1903 enum { FILTER_EXPR, BLOCK };
1904
1905 SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block);
1906 explicit SEHExceptStmt(EmptyShell E) : Stmt(SEHExceptStmtClass, E) {}
1907
1908public:
1909 static SEHExceptStmt* Create(const ASTContext &C,
1910 SourceLocation ExceptLoc,
1911 Expr *FilterExpr,
1912 Stmt *Block);
1913
1914 SourceLocation getLocStart() const LLVM_READONLY { return getExceptLoc(); }
1915 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1916
1917 SourceLocation getExceptLoc() const { return Loc; }
1918 SourceLocation getEndLoc() const { return getBlock()->getLocEnd(); }
1919
1920 Expr *getFilterExpr() const {
1921 return reinterpret_cast<Expr*>(Children[FILTER_EXPR]);
1922 }
1923
1924 CompoundStmt *getBlock() const {
1925 return cast<CompoundStmt>(Children[BLOCK]);
1926 }
1927
1928 child_range children() {
1929 return child_range(Children,Children+2);
1930 }
1931
1932 static bool classof(const Stmt *T) {
1933 return T->getStmtClass() == SEHExceptStmtClass;
1934 }
1935};
1936
1937class SEHFinallyStmt : public Stmt {
1938 friend class ASTReader;
1939 friend class ASTStmtReader;
1940
1941 SourceLocation Loc;
1942 Stmt *Block;
1943
1944 SEHFinallyStmt(SourceLocation Loc, Stmt *Block);
1945 explicit SEHFinallyStmt(EmptyShell E) : Stmt(SEHFinallyStmtClass, E) {}
1946
1947public:
1948 static SEHFinallyStmt* Create(const ASTContext &C,
1949 SourceLocation FinallyLoc,
1950 Stmt *Block);
1951
1952 SourceLocation getLocStart() const LLVM_READONLY { return getFinallyLoc(); }
1953 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1954
1955 SourceLocation getFinallyLoc() const { return Loc; }
1956 SourceLocation getEndLoc() const { return Block->getLocEnd(); }
1957
1958 CompoundStmt *getBlock() const { return cast<CompoundStmt>(Block); }
1959
1960 child_range children() {
1961 return child_range(&Block,&Block+1);
1962 }
1963
1964 static bool classof(const Stmt *T) {
1965 return T->getStmtClass() == SEHFinallyStmtClass;
1966 }
1967};
1968
1969class SEHTryStmt : public Stmt {
1970 friend class ASTReader;
1971 friend class ASTStmtReader;
1972
1973 bool IsCXXTry;
1974 SourceLocation TryLoc;
1975 Stmt *Children[2];
1976
1977 enum { TRY = 0, HANDLER = 1 };
1978
1979 SEHTryStmt(bool isCXXTry, // true if 'try' otherwise '__try'
1980 SourceLocation TryLoc,
1981 Stmt *TryBlock,
1982 Stmt *Handler);
1983
1984 explicit SEHTryStmt(EmptyShell E) : Stmt(SEHTryStmtClass, E) {}
1985
1986public:
1987 static SEHTryStmt* Create(const ASTContext &C, bool isCXXTry,
1988 SourceLocation TryLoc, Stmt *TryBlock,
1989 Stmt *Handler);
1990
1991 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
1992 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1993
1994 SourceLocation getTryLoc() const { return TryLoc; }
1995 SourceLocation getEndLoc() const { return Children[HANDLER]->getLocEnd(); }
1996
1997 bool getIsCXXTry() const { return IsCXXTry; }
1998
1999 CompoundStmt* getTryBlock() const {
2000 return cast<CompoundStmt>(Children[TRY]);
2001 }
2002
2003 Stmt *getHandler() const { return Children[HANDLER]; }
2004
2005 /// Returns 0 if not defined
2006 SEHExceptStmt *getExceptHandler() const;
2007 SEHFinallyStmt *getFinallyHandler() const;
2008
2009 child_range children() {
2010 return child_range(Children,Children+2);
2011 }
2012
2013 static bool classof(const Stmt *T) {
2014 return T->getStmtClass() == SEHTryStmtClass;
2015 }
2016};
2017
2018/// Represents a __leave statement.
2019class SEHLeaveStmt : public Stmt {
2020 SourceLocation LeaveLoc;
2021
2022public:
2023 explicit SEHLeaveStmt(SourceLocation LL)
2024 : Stmt(SEHLeaveStmtClass), LeaveLoc(LL) {}
2025
2026 /// \brief Build an empty __leave statement.
2027 explicit SEHLeaveStmt(EmptyShell Empty) : Stmt(SEHLeaveStmtClass, Empty) {}
2028
2029 SourceLocation getLeaveLoc() const { return LeaveLoc; }
2030 void setLeaveLoc(SourceLocation L) { LeaveLoc = L; }
2031
2032 SourceLocation getLocStart() const LLVM_READONLY { return LeaveLoc; }
2033 SourceLocation getLocEnd() const LLVM_READONLY { return LeaveLoc; }
2034
2035 static bool classof(const Stmt *T) {
2036 return T->getStmtClass() == SEHLeaveStmtClass;
2037 }
2038
2039 // Iterators
2040 child_range children() {
2041 return child_range(child_iterator(), child_iterator());
2042 }
2043};
2044
2045/// \brief This captures a statement into a function. For example, the following
2046/// pragma annotated compound statement can be represented as a CapturedStmt,
2047/// and this compound statement is the body of an anonymous outlined function.
2048/// @code
2049/// #pragma omp parallel
2050/// {
2051/// compute();
2052/// }
2053/// @endcode
2054class CapturedStmt : public Stmt {
2055public:
2056 /// \brief The different capture forms: by 'this', by reference, capture for
2057 /// variable-length array type etc.
2058 enum VariableCaptureKind {
2059 VCK_This,
2060 VCK_ByRef,
2061 VCK_ByCopy,
2062 VCK_VLAType,
2063 };
2064
2065 /// \brief Describes the capture of either a variable, or 'this', or
2066 /// variable-length array type.
2067 class Capture {
2068 llvm::PointerIntPair<VarDecl *, 2, VariableCaptureKind> VarAndKind;
2069 SourceLocation Loc;
2070
2071 public:
2072 friend class ASTStmtReader;
2073
2074 /// \brief Create a new capture.
2075 ///
2076 /// \param Loc The source location associated with this capture.
2077 ///
2078 /// \param Kind The kind of capture (this, ByRef, ...).
2079 ///
2080 /// \param Var The variable being captured, or null if capturing this.
2081 Capture(SourceLocation Loc, VariableCaptureKind Kind,
2082 VarDecl *Var = nullptr);
2083
2084 /// \brief Determine the kind of capture.
2085 VariableCaptureKind getCaptureKind() const;
2086
2087 /// \brief Retrieve the source location at which the variable or 'this' was
2088 /// first used.
2089 SourceLocation getLocation() const { return Loc; }
2090
2091 /// \brief Determine whether this capture handles the C++ 'this' pointer.
2092 bool capturesThis() const { return getCaptureKind() == VCK_This; }
2093
2094 /// \brief Determine whether this capture handles a variable (by reference).
2095 bool capturesVariable() const { return getCaptureKind() == VCK_ByRef; }
2096
2097 /// \brief Determine whether this capture handles a variable by copy.
2098 bool capturesVariableByCopy() const {
2099 return getCaptureKind() == VCK_ByCopy;
2100 }
2101
2102 /// \brief Determine whether this capture handles a variable-length array
2103 /// type.
2104 bool capturesVariableArrayType() const {
2105 return getCaptureKind() == VCK_VLAType;
2106 }
2107
2108 /// \brief Retrieve the declaration of the variable being captured.
2109 ///
2110 /// This operation is only valid if this capture captures a variable.
2111 VarDecl *getCapturedVar() const;
2112 };
2113
2114private:
2115 /// \brief The number of variable captured, including 'this'.
2116 unsigned NumCaptures;
2117
2118 /// \brief The pointer part is the implicit the outlined function and the
2119 /// int part is the captured region kind, 'CR_Default' etc.
2120 llvm::PointerIntPair<CapturedDecl *, 1, CapturedRegionKind> CapDeclAndKind;
2121
2122 /// \brief The record for captured variables, a RecordDecl or CXXRecordDecl.
2123 RecordDecl *TheRecordDecl = nullptr;
2124
2125 /// \brief Construct a captured statement.
2126 CapturedStmt(Stmt *S, CapturedRegionKind Kind, ArrayRef<Capture> Captures,
2127 ArrayRef<Expr *> CaptureInits, CapturedDecl *CD, RecordDecl *RD);
2128
2129 /// \brief Construct an empty captured statement.
2130 CapturedStmt(EmptyShell Empty, unsigned NumCaptures);
2131
2132 Stmt **getStoredStmts() { return reinterpret_cast<Stmt **>(this + 1); }
2133
2134 Stmt *const *getStoredStmts() const {
2135 return reinterpret_cast<Stmt *const *>(this + 1);
2136 }
2137
2138 Capture *getStoredCaptures() const;
2139
2140 void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
2141
2142public:
2143 friend class ASTStmtReader;
2144
2145 static CapturedStmt *Create(const ASTContext &Context, Stmt *S,
2146 CapturedRegionKind Kind,
2147 ArrayRef<Capture> Captures,
2148 ArrayRef<Expr *> CaptureInits,
2149 CapturedDecl *CD, RecordDecl *RD);
2150
2151 static CapturedStmt *CreateDeserialized(const ASTContext &Context,
2152 unsigned NumCaptures);
2153
2154 /// \brief Retrieve the statement being captured.
2155 Stmt *getCapturedStmt() { return getStoredStmts()[NumCaptures]; }
2156 const Stmt *getCapturedStmt() const { return getStoredStmts()[NumCaptures]; }
2157
2158 /// \brief Retrieve the outlined function declaration.
2159 CapturedDecl *getCapturedDecl();
2160 const CapturedDecl *getCapturedDecl() const;
2161
2162 /// \brief Set the outlined function declaration.
2163 void setCapturedDecl(CapturedDecl *D);
2164
2165 /// \brief Retrieve the captured region kind.
2166 CapturedRegionKind getCapturedRegionKind() const;
2167
2168 /// \brief Set the captured region kind.
2169 void setCapturedRegionKind(CapturedRegionKind Kind);
2170
2171 /// \brief Retrieve the record declaration for captured variables.
2172 const RecordDecl *getCapturedRecordDecl() const { return TheRecordDecl; }
2173
2174 /// \brief Set the record declaration for captured variables.
2175 void setCapturedRecordDecl(RecordDecl *D) {
2176 assert(D && "null RecordDecl");
2177 TheRecordDecl = D;
2178 }
2179
2180 /// \brief True if this variable has been captured.
2181 bool capturesVariable(const VarDecl *Var) const;
2182
2183 /// \brief An iterator that walks over the captures.
2184 using capture_iterator = Capture *;
2185 using const_capture_iterator = const Capture *;
2186 using capture_range = llvm::iterator_range<capture_iterator>;
2187 using capture_const_range = llvm::iterator_range<const_capture_iterator>;
2188
2189 capture_range captures() {
2190 return capture_range(capture_begin(), capture_end());
2191 }
2192 capture_const_range captures() const {
2193 return capture_const_range(capture_begin(), capture_end());
2194 }
2195
2196 /// \brief Retrieve an iterator pointing to the first capture.
2197 capture_iterator capture_begin() { return getStoredCaptures(); }
2198 const_capture_iterator capture_begin() const { return getStoredCaptures(); }
2199
2200 /// \brief Retrieve an iterator pointing past the end of the sequence of
2201 /// captures.
2202 capture_iterator capture_end() const {
2203 return getStoredCaptures() + NumCaptures;
2204 }
2205
2206 /// \brief Retrieve the number of captures, including 'this'.
2207 unsigned capture_size() const { return NumCaptures; }
2208
2209 /// \brief Iterator that walks over the capture initialization arguments.
2210 using capture_init_iterator = Expr **;
2211 using capture_init_range = llvm::iterator_range<capture_init_iterator>;
2212
2213 /// \brief Const iterator that walks over the capture initialization
2214 /// arguments.
2215 using const_capture_init_iterator = Expr *const *;
2216 using const_capture_init_range =
2217 llvm::iterator_range<const_capture_init_iterator>;
2218
2219 capture_init_range capture_inits() {
2220 return capture_init_range(capture_init_begin(), capture_init_end());
2221 }
2222
2223 const_capture_init_range capture_inits() const {
2224 return const_capture_init_range(capture_init_begin(), capture_init_end());
2225 }
2226
2227 /// \brief Retrieve the first initialization argument.
2228 capture_init_iterator capture_init_begin() {
2229 return reinterpret_cast<Expr **>(getStoredStmts());
2230 }
2231
2232 const_capture_init_iterator capture_init_begin() const {
2233 return reinterpret_cast<Expr *const *>(getStoredStmts());
2234 }
2235
2236 /// \brief Retrieve the iterator pointing one past the last initialization
2237 /// argument.
2238 capture_init_iterator capture_init_end() {
2239 return capture_init_begin() + NumCaptures;
2240 }
2241
2242 const_capture_init_iterator capture_init_end() const {
2243 return capture_init_begin() + NumCaptures;
2244 }
2245
2246 SourceLocation getLocStart() const LLVM_READONLY {
2247 return getCapturedStmt()->getLocStart();
2248 }
2249
2250 SourceLocation getLocEnd() const LLVM_READONLY {
2251 return getCapturedStmt()->getLocEnd();
2252 }
2253
2254 SourceRange getSourceRange() const LLVM_READONLY {
2255 return getCapturedStmt()->getSourceRange();
2256 }
2257
2258 static bool classof(const Stmt *T) {
2259 return T->getStmtClass() == CapturedStmtClass;
2260 }
2261
2262 child_range children();
2263};
2264
2265} // namespace clang
2266
2267#endif // LLVM_CLANG_AST_STMT_H
2268