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