1//===- Stmt.cpp - Statement AST Node Implementation -----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Stmt class and statement subclasses.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/Stmt.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTDiagnostic.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclGroup.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/ExprCXX.h"
21#include "clang/AST/ExprConcepts.h"
22#include "clang/AST/ExprObjC.h"
23#include "clang/AST/ExprOpenMP.h"
24#include "clang/AST/StmtCXX.h"
25#include "clang/AST/StmtObjC.h"
26#include "clang/AST/StmtOpenACC.h"
27#include "clang/AST/StmtOpenMP.h"
28#include "clang/AST/StmtSYCL.h"
29#include "clang/AST/Type.h"
30#include "clang/Basic/CharInfo.h"
31#include "clang/Basic/LLVM.h"
32#include "clang/Basic/SourceLocation.h"
33#include "clang/Basic/TargetInfo.h"
34#include "clang/Lex/Token.h"
35#include "llvm/ADT/SmallVector.h"
36#include "llvm/ADT/StringExtras.h"
37#include "llvm/ADT/StringRef.h"
38#include "llvm/Support/Compiler.h"
39#include "llvm/Support/ErrorHandling.h"
40#include "llvm/Support/MathExtras.h"
41#include "llvm/Support/raw_ostream.h"
42#include <algorithm>
43#include <cassert>
44#include <cstring>
45#include <optional>
46#include <string>
47#include <utility>
48
49using namespace clang;
50
51#define STMT(CLASS, PARENT)
52#define STMT_RANGE(BASE, FIRST, LAST)
53#define LAST_STMT_RANGE(BASE, FIRST, LAST) \
54 static_assert(llvm::isUInt<NumStmtBits>(Stmt::StmtClass::LAST##Class), \
55 "The number of 'StmtClass'es is strictly bound " \
56 "by a bitfield of width NumStmtBits");
57#define ABSTRACT_STMT(STMT)
58#include "clang/AST/StmtNodes.inc"
59
60static struct StmtClassNameTable {
61 const char *Name;
62 unsigned Counter;
63 unsigned Size;
64} StmtClassInfo[Stmt::lastStmtConstant+1];
65
66static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
67 static bool Initialized = false;
68 if (Initialized)
69 return StmtClassInfo[E];
70
71 // Initialize the table on the first use.
72 Initialized = true;
73#define ABSTRACT_STMT(STMT)
74#define STMT(CLASS, PARENT) \
75 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
76 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
77#include "clang/AST/StmtNodes.inc"
78
79 return StmtClassInfo[E];
80}
81
82void *Stmt::operator new(size_t bytes, const ASTContext& C,
83 unsigned alignment) {
84 return ::operator new(Bytes: bytes, C, Alignment: alignment);
85}
86
87const char *Stmt::getStmtClassName() const {
88 return getStmtInfoTableEntry(E: (StmtClass) StmtBits.sClass).Name;
89}
90
91// Check that no statement / expression class is polymorphic. LLVM style RTTI
92// should be used instead. If absolutely needed an exception can still be added
93// here by defining the appropriate macro (but please don't do this).
94#define STMT(CLASS, PARENT) \
95 static_assert(!std::is_polymorphic<CLASS>::value, \
96 #CLASS " should not be polymorphic!");
97#include "clang/AST/StmtNodes.inc"
98
99// Check that no statement / expression class has a non-trival destructor.
100// Statements and expressions are allocated with the BumpPtrAllocator from
101// ASTContext and therefore their destructor is not executed.
102#define STMT(CLASS, PARENT) \
103 static_assert(std::is_trivially_destructible<CLASS>::value, \
104 #CLASS " should be trivially destructible!");
105// FIXME: InitListExpr is not trivially destructible due to its ASTVector.
106#define INITLISTEXPR(CLASS, PARENT)
107#include "clang/AST/StmtNodes.inc"
108
109void Stmt::PrintStats() {
110 // Ensure the table is primed.
111 getStmtInfoTableEntry(Stmt::NullStmtClass);
112
113 unsigned sum = 0;
114 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
115 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
116 if (StmtClassInfo[i].Name == nullptr) continue;
117 sum += StmtClassInfo[i].Counter;
118 }
119 llvm::errs() << " " << sum << " stmts/exprs total.\n";
120 sum = 0;
121 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
122 if (StmtClassInfo[i].Name == nullptr) continue;
123 if (StmtClassInfo[i].Counter == 0) continue;
124 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
125 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
126 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
127 << " bytes)\n";
128 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
129 }
130
131 llvm::errs() << "Total bytes = " << sum << "\n";
132}
133
134void Stmt::addStmtClass(StmtClass s) {
135 ++getStmtInfoTableEntry(E: s).Counter;
136}
137
138bool Stmt::StatisticsEnabled = false;
139void Stmt::EnableStatistics() {
140 StatisticsEnabled = true;
141}
142
143static std::pair<Stmt::Likelihood, const Attr *>
144getLikelihood(ArrayRef<const Attr *> Attrs) {
145 for (const auto *A : Attrs) {
146 if (isa<LikelyAttr>(A))
147 return std::make_pair(x: Stmt::LH_Likely, y&: A);
148
149 if (isa<UnlikelyAttr>(A))
150 return std::make_pair(x: Stmt::LH_Unlikely, y&: A);
151 }
152
153 return std::make_pair(x: Stmt::LH_None, y: nullptr);
154}
155
156static std::pair<Stmt::Likelihood, const Attr *> getLikelihood(const Stmt *S) {
157 if (const auto *AS = dyn_cast_or_null<AttributedStmt>(Val: S))
158 return getLikelihood(Attrs: AS->getAttrs());
159
160 return std::make_pair(x: Stmt::LH_None, y: nullptr);
161}
162
163Stmt::Likelihood Stmt::getLikelihood(ArrayRef<const Attr *> Attrs) {
164 return ::getLikelihood(Attrs).first;
165}
166
167Stmt::Likelihood Stmt::getLikelihood(const Stmt *S) {
168 return ::getLikelihood(S).first;
169}
170
171const Attr *Stmt::getLikelihoodAttr(const Stmt *S) {
172 return ::getLikelihood(S).second;
173}
174
175Stmt::Likelihood Stmt::getLikelihood(const Stmt *Then, const Stmt *Else) {
176 Likelihood LHT = ::getLikelihood(S: Then).first;
177 Likelihood LHE = ::getLikelihood(S: Else).first;
178 if (LHE == LH_None)
179 return LHT;
180
181 // If the same attribute is used on both branches there's a conflict.
182 if (LHT == LHE)
183 return LH_None;
184
185 if (LHT != LH_None)
186 return LHT;
187
188 // Invert the value of Else to get the value for Then.
189 return LHE == LH_Likely ? LH_Unlikely : LH_Likely;
190}
191
192std::tuple<bool, const Attr *, const Attr *>
193Stmt::determineLikelihoodConflict(const Stmt *Then, const Stmt *Else) {
194 std::pair<Likelihood, const Attr *> LHT = ::getLikelihood(S: Then);
195 std::pair<Likelihood, const Attr *> LHE = ::getLikelihood(S: Else);
196 // If the same attribute is used on both branches there's a conflict.
197 if (LHT.first != LH_None && LHT.first == LHE.first)
198 return std::make_tuple(args: true, args&: LHT.second, args&: LHE.second);
199
200 return std::make_tuple(args: false, args: nullptr, args: nullptr);
201}
202
203/// Skip no-op (attributed, compound) container stmts and skip captured
204/// stmt at the top, if \a IgnoreCaptured is true.
205Stmt *Stmt::IgnoreContainers(bool IgnoreCaptured) {
206 Stmt *S = this;
207 if (IgnoreCaptured)
208 if (auto CapS = dyn_cast_or_null<CapturedStmt>(Val: S))
209 S = CapS->getCapturedStmt();
210 while (true) {
211 if (auto AS = dyn_cast_or_null<AttributedStmt>(Val: S))
212 S = AS->getSubStmt();
213 else if (auto CS = dyn_cast_or_null<CompoundStmt>(Val: S)) {
214 if (CS->size() != 1)
215 break;
216 S = CS->body_back();
217 } else
218 break;
219 }
220 return S;
221}
222
223/// Strip off all label-like statements.
224///
225/// This will strip off label statements, case statements, attributed
226/// statements and default statements recursively.
227const Stmt *Stmt::stripLabelLikeStatements() const {
228 const Stmt *S = this;
229 while (true) {
230 if (const auto *LS = dyn_cast<LabelStmt>(Val: S))
231 S = LS->getSubStmt();
232 else if (const auto *SC = dyn_cast<SwitchCase>(Val: S))
233 S = SC->getSubStmt();
234 else if (const auto *AS = dyn_cast<AttributedStmt>(Val: S))
235 S = AS->getSubStmt();
236 else
237 return S;
238 }
239}
240
241namespace {
242
243 struct good {};
244 struct bad {};
245
246 // These silly little functions have to be static inline to suppress
247 // unused warnings, and they have to be defined to suppress other
248 // warnings.
249 static good is_good(good) { return good(); }
250
251 typedef Stmt::child_range children_t();
252 template <class T> good implements_children(children_t T::*) {
253 return good();
254 }
255 LLVM_ATTRIBUTE_UNUSED
256 static bad implements_children(children_t Stmt::*) {
257 return bad();
258 }
259
260 typedef SourceLocation getBeginLoc_t() const;
261 template <class T> good implements_getBeginLoc(getBeginLoc_t T::*) {
262 return good();
263 }
264 LLVM_ATTRIBUTE_UNUSED
265 static bad implements_getBeginLoc(getBeginLoc_t Stmt::*) { return bad(); }
266
267 typedef SourceLocation getLocEnd_t() const;
268 template <class T> good implements_getEndLoc(getLocEnd_t T::*) {
269 return good();
270 }
271 LLVM_ATTRIBUTE_UNUSED
272 static bad implements_getEndLoc(getLocEnd_t Stmt::*) { return bad(); }
273
274#define ASSERT_IMPLEMENTS_children(type) \
275 (void) is_good(implements_children(&type::children))
276#define ASSERT_IMPLEMENTS_getBeginLoc(type) \
277 (void)is_good(implements_getBeginLoc(&type::getBeginLoc))
278#define ASSERT_IMPLEMENTS_getEndLoc(type) \
279 (void)is_good(implements_getEndLoc(&type::getEndLoc))
280
281} // namespace
282
283/// Check whether the various Stmt classes implement their member
284/// functions.
285LLVM_ATTRIBUTE_UNUSED
286static inline void check_implementations() {
287#define ABSTRACT_STMT(type)
288#define STMT(type, base) \
289 ASSERT_IMPLEMENTS_children(type); \
290 ASSERT_IMPLEMENTS_getBeginLoc(type); \
291 ASSERT_IMPLEMENTS_getEndLoc(type);
292#include "clang/AST/StmtNodes.inc"
293}
294
295Stmt::child_range Stmt::children() {
296 switch (getStmtClass()) {
297 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
298#define ABSTRACT_STMT(type)
299#define STMT(type, base) \
300 case Stmt::type##Class: \
301 return static_cast<type*>(this)->children();
302#include "clang/AST/StmtNodes.inc"
303 }
304 llvm_unreachable("unknown statement kind!");
305}
306
307// Amusing macro metaprogramming hack: check whether a class provides
308// a more specific implementation of getSourceRange.
309//
310// See also Expr.cpp:getExprLoc().
311namespace {
312
313 /// This implementation is used when a class provides a custom
314 /// implementation of getSourceRange.
315 template <class S, class T>
316 SourceRange getSourceRangeImpl(const Stmt *stmt,
317 SourceRange (T::*v)() const) {
318 return static_cast<const S*>(stmt)->getSourceRange();
319 }
320
321 /// This implementation is used when a class doesn't provide a custom
322 /// implementation of getSourceRange. Overload resolution should pick it over
323 /// the implementation above because it's more specialized according to
324 /// function template partial ordering.
325 template <class S>
326 SourceRange getSourceRangeImpl(const Stmt *stmt,
327 SourceRange (Stmt::*v)() const) {
328 return SourceRange(static_cast<const S *>(stmt)->getBeginLoc(),
329 static_cast<const S *>(stmt)->getEndLoc());
330 }
331
332} // namespace
333
334SourceRange Stmt::getSourceRange() const {
335 switch (getStmtClass()) {
336 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
337#define ABSTRACT_STMT(type)
338#define STMT(type, base) \
339 case Stmt::type##Class: \
340 return getSourceRangeImpl<type>(this, &type::getSourceRange);
341#include "clang/AST/StmtNodes.inc"
342 }
343 llvm_unreachable("unknown statement kind!");
344}
345
346SourceLocation Stmt::getBeginLoc() const {
347 switch (getStmtClass()) {
348 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
349#define ABSTRACT_STMT(type)
350#define STMT(type, base) \
351 case Stmt::type##Class: \
352 return static_cast<const type *>(this)->getBeginLoc();
353#include "clang/AST/StmtNodes.inc"
354 }
355 llvm_unreachable("unknown statement kind");
356}
357
358SourceLocation Stmt::getEndLoc() const {
359 switch (getStmtClass()) {
360 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
361#define ABSTRACT_STMT(type)
362#define STMT(type, base) \
363 case Stmt::type##Class: \
364 return static_cast<const type *>(this)->getEndLoc();
365#include "clang/AST/StmtNodes.inc"
366 }
367 llvm_unreachable("unknown statement kind");
368}
369
370int64_t Stmt::getID(const ASTContext &Context) const {
371 return Context.getAllocator().identifyKnownAlignedObject<Stmt>(this);
372}
373
374CompoundStmt::CompoundStmt(ArrayRef<Stmt *> Stmts, FPOptionsOverride FPFeatures,
375 SourceLocation LB, SourceLocation RB)
376 : Stmt(CompoundStmtClass), LBraceLoc(LB), RBraceLoc(RB) {
377 CompoundStmtBits.NumStmts = Stmts.size();
378 CompoundStmtBits.HasFPFeatures = FPFeatures.requiresTrailingStorage();
379 setStmts(Stmts);
380 if (hasStoredFPFeatures())
381 setStoredFPFeatures(FPFeatures);
382}
383
384void CompoundStmt::setStmts(ArrayRef<Stmt *> Stmts) {
385 assert(CompoundStmtBits.NumStmts == Stmts.size() &&
386 "NumStmts doesn't fit in bits of CompoundStmtBits.NumStmts!");
387
388 std::copy(first: Stmts.begin(), last: Stmts.end(), result: body_begin());
389}
390
391CompoundStmt *CompoundStmt::Create(const ASTContext &C, ArrayRef<Stmt *> Stmts,
392 FPOptionsOverride FPFeatures,
393 SourceLocation LB, SourceLocation RB) {
394 void *Mem =
395 C.Allocate(totalSizeToAlloc<Stmt *, FPOptionsOverride>(
396 Stmts.size(), FPFeatures.requiresTrailingStorage()),
397 alignof(CompoundStmt));
398 return new (Mem) CompoundStmt(Stmts, FPFeatures, LB, RB);
399}
400
401CompoundStmt *CompoundStmt::CreateEmpty(const ASTContext &C, unsigned NumStmts,
402 bool HasFPFeatures) {
403 void *Mem = C.Allocate(
404 totalSizeToAlloc<Stmt *, FPOptionsOverride>(NumStmts, HasFPFeatures),
405 alignof(CompoundStmt));
406 CompoundStmt *New = new (Mem) CompoundStmt(EmptyShell());
407 New->CompoundStmtBits.NumStmts = NumStmts;
408 New->CompoundStmtBits.HasFPFeatures = HasFPFeatures;
409 return New;
410}
411
412const Expr *ValueStmt::getExprStmt() const {
413 const Stmt *S = this;
414 do {
415 if (const auto *E = dyn_cast<Expr>(Val: S))
416 return E;
417
418 if (const auto *LS = dyn_cast<LabelStmt>(Val: S))
419 S = LS->getSubStmt();
420 else if (const auto *AS = dyn_cast<AttributedStmt>(Val: S))
421 S = AS->getSubStmt();
422 else
423 llvm_unreachable("unknown kind of ValueStmt");
424 } while (isa<ValueStmt>(Val: S));
425
426 return nullptr;
427}
428
429const char *LabelStmt::getName() const {
430 return getDecl()->getIdentifier()->getNameStart();
431}
432
433AttributedStmt *AttributedStmt::Create(const ASTContext &C, SourceLocation Loc,
434 ArrayRef<const Attr*> Attrs,
435 Stmt *SubStmt) {
436 assert(!Attrs.empty() && "Attrs should not be empty");
437 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(Attrs.size()),
438 alignof(AttributedStmt));
439 return new (Mem) AttributedStmt(Loc, Attrs, SubStmt);
440}
441
442AttributedStmt *AttributedStmt::CreateEmpty(const ASTContext &C,
443 unsigned NumAttrs) {
444 assert(NumAttrs > 0 && "NumAttrs should be greater than zero");
445 void *Mem = C.Allocate(totalSizeToAlloc<const Attr *>(NumAttrs),
446 alignof(AttributedStmt));
447 return new (Mem) AttributedStmt(EmptyShell(), NumAttrs);
448}
449
450std::string AsmStmt::generateAsmString(const ASTContext &C) const {
451 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(Val: this))
452 return gccAsmStmt->generateAsmString(C);
453 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(Val: this))
454 return msAsmStmt->generateAsmString(C);
455 llvm_unreachable("unknown asm statement kind!");
456}
457
458std::string AsmStmt::getOutputConstraint(unsigned i) const {
459 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(Val: this))
460 return gccAsmStmt->getOutputConstraint(i);
461 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(Val: this))
462 return msAsmStmt->getOutputConstraint(i).str();
463 llvm_unreachable("unknown asm statement kind!");
464}
465
466const Expr *AsmStmt::getOutputExpr(unsigned i) const {
467 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(Val: this))
468 return gccAsmStmt->getOutputExpr(i);
469 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(Val: this))
470 return msAsmStmt->getOutputExpr(i);
471 llvm_unreachable("unknown asm statement kind!");
472}
473
474std::string AsmStmt::getInputConstraint(unsigned i) const {
475 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(Val: this))
476 return gccAsmStmt->getInputConstraint(i);
477 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(Val: this))
478 return msAsmStmt->getInputConstraint(i).str();
479 llvm_unreachable("unknown asm statement kind!");
480}
481
482const Expr *AsmStmt::getInputExpr(unsigned i) const {
483 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(Val: this))
484 return gccAsmStmt->getInputExpr(i);
485 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(Val: this))
486 return msAsmStmt->getInputExpr(i);
487 llvm_unreachable("unknown asm statement kind!");
488}
489
490std::string AsmStmt::getClobber(unsigned i) const {
491 if (const auto *gccAsmStmt = dyn_cast<GCCAsmStmt>(Val: this))
492 return gccAsmStmt->getClobber(i);
493 if (const auto *msAsmStmt = dyn_cast<MSAsmStmt>(Val: this))
494 return msAsmStmt->getClobber(i).str();
495 llvm_unreachable("unknown asm statement kind!");
496}
497
498/// getNumPlusOperands - Return the number of output operands that have a "+"
499/// constraint.
500unsigned AsmStmt::getNumPlusOperands() const {
501 unsigned Res = 0;
502 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
503 if (isOutputPlusConstraint(i))
504 ++Res;
505 return Res;
506}
507
508char GCCAsmStmt::AsmStringPiece::getModifier() const {
509 assert(isOperand() && "Only Operands can have modifiers.");
510 return isLetter(c: Str[0]) ? Str[0] : '\0';
511}
512
513std::string GCCAsmStmt::ExtractStringFromGCCAsmStmtComponent(const Expr *E) {
514 if (auto *SL = llvm::dyn_cast<StringLiteral>(Val: E))
515 return SL->getString().str();
516 assert(E->getDependence() == ExprDependence::None &&
517 "cannot extract a string from a dependent expression");
518 auto *CE = cast<ConstantExpr>(Val: E);
519 APValue Res = CE->getAPValueResult();
520 assert(Res.isArray() && "expected an array");
521
522 std::string Out;
523 Out.reserve(res: Res.getArraySize());
524 for (unsigned I = 0; I < Res.getArraySize(); ++I) {
525 APValue C = Res.getArrayInitializedElt(I);
526 assert(C.isInt());
527 auto Ch = static_cast<char>(C.getInt().getExtValue());
528 Out.push_back(c: Ch);
529 }
530 return Out;
531}
532
533std::string GCCAsmStmt::getAsmString() const {
534 return ExtractStringFromGCCAsmStmtComponent(E: getAsmStringExpr());
535}
536
537std::string GCCAsmStmt::getClobber(unsigned i) const {
538 return ExtractStringFromGCCAsmStmtComponent(E: getClobberExpr(i));
539}
540
541Expr *GCCAsmStmt::getOutputExpr(unsigned i) {
542 return cast<Expr>(Val: Exprs[i]);
543}
544
545/// getOutputConstraint - Return the constraint string for the specified
546/// output operand. All output constraints are known to be non-empty (either
547/// '=' or '+').
548std::string GCCAsmStmt::getOutputConstraint(unsigned i) const {
549 return ExtractStringFromGCCAsmStmtComponent(E: getOutputConstraintExpr(i));
550}
551
552Expr *GCCAsmStmt::getInputExpr(unsigned i) {
553 return cast<Expr>(Val: Exprs[i + NumOutputs]);
554}
555
556void GCCAsmStmt::setInputExpr(unsigned i, Expr *E) {
557 Exprs[i + NumOutputs] = E;
558}
559
560AddrLabelExpr *GCCAsmStmt::getLabelExpr(unsigned i) const {
561 return cast<AddrLabelExpr>(Val: Exprs[i + NumOutputs + NumInputs]);
562}
563
564StringRef GCCAsmStmt::getLabelName(unsigned i) const {
565 return getLabelExpr(i)->getLabel()->getName();
566}
567
568/// getInputConstraint - Return the specified input constraint. Unlike output
569/// constraints, these can be empty.
570std::string GCCAsmStmt::getInputConstraint(unsigned i) const {
571 return ExtractStringFromGCCAsmStmtComponent(E: getInputConstraintExpr(i));
572}
573
574void GCCAsmStmt::setOutputsAndInputsAndClobbers(
575 const ASTContext &C, IdentifierInfo **Names, Expr **Constraints,
576 Stmt **Exprs, unsigned NumOutputs, unsigned NumInputs, unsigned NumLabels,
577 Expr **Clobbers, unsigned NumClobbers) {
578 this->NumOutputs = NumOutputs;
579 this->NumInputs = NumInputs;
580 this->NumClobbers = NumClobbers;
581 this->NumLabels = NumLabels;
582
583 unsigned NumExprs = NumOutputs + NumInputs + NumLabels;
584
585 C.Deallocate(Ptr: this->Names);
586 this->Names = new (C) IdentifierInfo*[NumExprs];
587 std::copy(first: Names, last: Names + NumExprs, result: this->Names);
588
589 C.Deallocate(Ptr: this->Exprs);
590 this->Exprs = new (C) Stmt*[NumExprs];
591 std::copy(first: Exprs, last: Exprs + NumExprs, result: this->Exprs);
592
593 unsigned NumConstraints = NumOutputs + NumInputs;
594 C.Deallocate(Ptr: this->Constraints);
595 this->Constraints = new (C) Expr *[NumConstraints];
596 std::copy(first: Constraints, last: Constraints + NumConstraints, result: this->Constraints);
597
598 C.Deallocate(Ptr: this->Clobbers);
599 this->Clobbers = new (C) Expr *[NumClobbers];
600 std::copy(first: Clobbers, last: Clobbers + NumClobbers, result: this->Clobbers);
601}
602
603/// getNamedOperand - Given a symbolic operand reference like %[foo],
604/// translate this into a numeric value needed to reference the same operand.
605/// This returns -1 if the operand name is invalid.
606int GCCAsmStmt::getNamedOperand(StringRef SymbolicName) const {
607 // Check if this is an output operand.
608 unsigned NumOutputs = getNumOutputs();
609 for (unsigned i = 0; i != NumOutputs; ++i)
610 if (getOutputName(i) == SymbolicName)
611 return i;
612
613 unsigned NumInputs = getNumInputs();
614 for (unsigned i = 0; i != NumInputs; ++i)
615 if (getInputName(i) == SymbolicName)
616 return NumOutputs + i;
617
618 for (unsigned i = 0, e = getNumLabels(); i != e; ++i)
619 if (getLabelName(i) == SymbolicName)
620 return NumOutputs + NumInputs + getNumPlusOperands() + i;
621
622 // Not found.
623 return -1;
624}
625
626/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
627/// it into pieces. If the asm string is erroneous, emit errors and return
628/// true, otherwise return false.
629unsigned GCCAsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
630 const ASTContext &C, unsigned &DiagOffs) const {
631
632 std::string Str = getAsmString();
633 const char *StrStart = Str.data();
634 const char *StrEnd = Str.data() + Str.size();
635 const char *CurPtr = StrStart;
636
637 // "Simple" inline asms have no constraints or operands, just convert the asm
638 // string to escape $'s.
639 if (isSimple()) {
640 std::string Result;
641 for (; CurPtr != StrEnd; ++CurPtr) {
642 switch (*CurPtr) {
643 case '$':
644 Result += "$$";
645 break;
646 default:
647 Result += *CurPtr;
648 break;
649 }
650 }
651 Pieces.push_back(Elt: AsmStringPiece(Result));
652 return 0;
653 }
654
655 // CurStringPiece - The current string that we are building up as we scan the
656 // asm string.
657 std::string CurStringPiece;
658
659 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
660
661 unsigned LastAsmStringToken = 0;
662 unsigned LastAsmStringOffset = 0;
663
664 while (true) {
665 // Done with the string?
666 if (CurPtr == StrEnd) {
667 if (!CurStringPiece.empty())
668 Pieces.push_back(Elt: AsmStringPiece(CurStringPiece));
669 return 0;
670 }
671
672 char CurChar = *CurPtr++;
673 switch (CurChar) {
674 case '$': CurStringPiece += "$$"; continue;
675 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
676 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
677 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
678 case '%':
679 break;
680 default:
681 CurStringPiece += CurChar;
682 continue;
683 }
684
685 const TargetInfo &TI = C.getTargetInfo();
686
687 // Escaped "%" character in asm string.
688 if (CurPtr == StrEnd) {
689 // % at end of string is invalid (no escape).
690 DiagOffs = CurPtr-StrStart-1;
691 return diag::err_asm_invalid_escape;
692 }
693 // Handle escaped char and continue looping over the asm string.
694 char EscapedChar = *CurPtr++;
695 switch (EscapedChar) {
696 default:
697 // Handle target-specific escaped characters.
698 if (auto MaybeReplaceStr = TI.handleAsmEscapedChar(C: EscapedChar)) {
699 CurStringPiece += *MaybeReplaceStr;
700 continue;
701 }
702 break;
703 case '%': // %% -> %
704 case '{': // %{ -> {
705 case '}': // %} -> }
706 CurStringPiece += EscapedChar;
707 continue;
708 case '=': // %= -> Generate a unique ID.
709 CurStringPiece += "${:uid}";
710 continue;
711 }
712
713 // Otherwise, we have an operand. If we have accumulated a string so far,
714 // add it to the Pieces list.
715 if (!CurStringPiece.empty()) {
716 Pieces.push_back(Elt: AsmStringPiece(CurStringPiece));
717 CurStringPiece.clear();
718 }
719
720 // Handle operands that have asmSymbolicName (e.g., %x[foo]) and those that
721 // don't (e.g., %x4). 'x' following the '%' is the constraint modifier.
722
723 const char *Begin = CurPtr - 1; // Points to the character following '%'.
724 const char *Percent = Begin - 1; // Points to '%'.
725
726 if (isLetter(c: EscapedChar)) {
727 if (CurPtr == StrEnd) { // Premature end.
728 DiagOffs = CurPtr-StrStart-1;
729 return diag::err_asm_invalid_escape;
730 }
731
732 // Specifically handle `cc` which we will alias to `c`.
733 // Note this is the only operand modifier that exists which has two
734 // characters.
735 if (EscapedChar == 'c' && *CurPtr == 'c')
736 CurPtr++;
737
738 EscapedChar = *CurPtr++;
739 }
740
741 const SourceManager &SM = C.getSourceManager();
742 const LangOptions &LO = C.getLangOpts();
743
744 // Handle operands that don't have asmSymbolicName (e.g., %x4).
745 if (isDigit(c: EscapedChar)) {
746 // %n - Assembler operand n
747 unsigned N = 0;
748
749 --CurPtr;
750 while (CurPtr != StrEnd && isDigit(c: *CurPtr))
751 N = N*10 + ((*CurPtr++)-'0');
752
753 unsigned NumOperands = getNumOutputs() + getNumPlusOperands() +
754 getNumInputs() + getNumLabels();
755 if (N >= NumOperands) {
756 DiagOffs = CurPtr-StrStart-1;
757 return diag::err_asm_invalid_operand_number;
758 }
759
760 // Str contains "x4" (Operand without the leading %).
761 std::string Str(Begin, CurPtr - Begin);
762 // (BeginLoc, EndLoc) represents the range of the operand we are currently
763 // processing. Unlike Str, the range includes the leading '%'.
764 SourceLocation BeginLoc, EndLoc;
765 if (auto *SL = dyn_cast<StringLiteral>(Val: getAsmStringExpr())) {
766 BeginLoc =
767 SL->getLocationOfByte(ByteNo: Percent - StrStart, SM, Features: LO, Target: TI,
768 StartToken: &LastAsmStringToken, StartTokenByteOffset: &LastAsmStringOffset);
769 EndLoc =
770 SL->getLocationOfByte(ByteNo: CurPtr - StrStart, SM, Features: LO, Target: TI,
771 StartToken: &LastAsmStringToken, StartTokenByteOffset: &LastAsmStringOffset);
772 } else {
773 BeginLoc = getAsmStringExpr()->getBeginLoc();
774 EndLoc = getAsmStringExpr()->getEndLoc();
775 }
776
777 Pieces.emplace_back(Args&: N, Args: std::move(Str), Args&: BeginLoc, Args&: EndLoc);
778 continue;
779 }
780
781 // Handle operands that have asmSymbolicName (e.g., %x[foo]).
782 if (EscapedChar == '[') {
783 DiagOffs = CurPtr-StrStart-1;
784
785 // Find the ']'.
786 const char *NameEnd = (const char*)memchr(s: CurPtr, c: ']', n: StrEnd-CurPtr);
787 if (NameEnd == nullptr)
788 return diag::err_asm_unterminated_symbolic_operand_name;
789 if (NameEnd == CurPtr)
790 return diag::err_asm_empty_symbolic_operand_name;
791
792 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
793
794 int N = getNamedOperand(SymbolicName);
795 if (N == -1) {
796 // Verify that an operand with that name exists.
797 DiagOffs = CurPtr-StrStart;
798 return diag::err_asm_unknown_symbolic_operand_name;
799 }
800
801 // Str contains "x[foo]" (Operand without the leading %).
802 std::string Str(Begin, NameEnd + 1 - Begin);
803
804 // (BeginLoc, EndLoc) represents the range of the operand we are currently
805 // processing. Unlike Str, the range includes the leading '%'.
806 SourceLocation BeginLoc, EndLoc;
807 if (auto *SL = dyn_cast<StringLiteral>(Val: getAsmStringExpr())) {
808 BeginLoc =
809 SL->getLocationOfByte(ByteNo: Percent - StrStart, SM, Features: LO, Target: TI,
810 StartToken: &LastAsmStringToken, StartTokenByteOffset: &LastAsmStringOffset);
811 EndLoc =
812 SL->getLocationOfByte(ByteNo: NameEnd + 1 - StrStart, SM, Features: LO, Target: TI,
813 StartToken: &LastAsmStringToken, StartTokenByteOffset: &LastAsmStringOffset);
814 } else {
815 BeginLoc = getAsmStringExpr()->getBeginLoc();
816 EndLoc = getAsmStringExpr()->getEndLoc();
817 }
818
819 Pieces.emplace_back(Args&: N, Args: std::move(Str), Args&: BeginLoc, Args&: EndLoc);
820
821 CurPtr = NameEnd+1;
822 continue;
823 }
824
825 DiagOffs = CurPtr-StrStart-1;
826 return diag::err_asm_invalid_escape;
827 }
828}
829
830/// Assemble final IR asm string (GCC-style).
831std::string GCCAsmStmt::generateAsmString(const ASTContext &C) const {
832 // Analyze the asm string to decompose it into its pieces. We know that Sema
833 // has already done this, so it is guaranteed to be successful.
834 SmallVector<GCCAsmStmt::AsmStringPiece, 4> Pieces;
835 unsigned DiagOffs;
836 AnalyzeAsmString(Pieces, C, DiagOffs);
837
838 std::string AsmString;
839 for (const auto &Piece : Pieces) {
840 if (Piece.isString())
841 AsmString += Piece.getString();
842 else if (Piece.getModifier() == '\0')
843 AsmString += '$' + llvm::utostr(X: Piece.getOperandNo());
844 else
845 AsmString += "${" + llvm::utostr(X: Piece.getOperandNo()) + ':' +
846 Piece.getModifier() + '}';
847 }
848 return AsmString;
849}
850
851/// Assemble final IR asm string (MS-style).
852std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
853 // FIXME: This needs to be translated into the IR string representation.
854 SmallVector<StringRef, 8> Pieces;
855 AsmStr.split(A&: Pieces, Separator: "\n\t");
856 std::string MSAsmString;
857 for (size_t I = 0, E = Pieces.size(); I < E; ++I) {
858 StringRef Instruction = Pieces[I];
859 // For vex/vex2/vex3/evex masm style prefix, convert it to att style
860 // since we don't support masm style prefix in backend.
861 if (Instruction.starts_with(Prefix: "vex "))
862 MSAsmString += '{' + Instruction.substr(Start: 0, N: 3).str() + '}' +
863 Instruction.substr(Start: 3).str();
864 else if (Instruction.starts_with(Prefix: "vex2 ") ||
865 Instruction.starts_with(Prefix: "vex3 ") ||
866 Instruction.starts_with(Prefix: "evex "))
867 MSAsmString += '{' + Instruction.substr(Start: 0, N: 4).str() + '}' +
868 Instruction.substr(Start: 4).str();
869 else
870 MSAsmString += Instruction.str();
871 // If this is not the last instruction, adding back the '\n\t'.
872 if (I < E - 1)
873 MSAsmString += "\n\t";
874 }
875 return MSAsmString;
876}
877
878Expr *MSAsmStmt::getOutputExpr(unsigned i) {
879 return cast<Expr>(Val: Exprs[i]);
880}
881
882Expr *MSAsmStmt::getInputExpr(unsigned i) {
883 return cast<Expr>(Val: Exprs[i + NumOutputs]);
884}
885
886void MSAsmStmt::setInputExpr(unsigned i, Expr *E) {
887 Exprs[i + NumOutputs] = E;
888}
889
890//===----------------------------------------------------------------------===//
891// Constructors
892//===----------------------------------------------------------------------===//
893
894GCCAsmStmt::GCCAsmStmt(const ASTContext &C, SourceLocation asmloc,
895 bool issimple, bool isvolatile, unsigned numoutputs,
896 unsigned numinputs, IdentifierInfo **names,
897 Expr **constraints, Expr **exprs, Expr *asmstr,
898 unsigned numclobbers, Expr **clobbers,
899 unsigned numlabels, SourceLocation rparenloc)
900 : AsmStmt(GCCAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
901 numinputs, numclobbers),
902 RParenLoc(rparenloc), AsmStr(asmstr), NumLabels(numlabels) {
903 unsigned NumExprs = NumOutputs + NumInputs + NumLabels;
904
905 Names = new (C) IdentifierInfo*[NumExprs];
906 std::copy(first: names, last: names + NumExprs, result: Names);
907
908 Exprs = new (C) Stmt*[NumExprs];
909 std::copy(first: exprs, last: exprs + NumExprs, result: Exprs);
910
911 unsigned NumConstraints = NumOutputs + NumInputs;
912 Constraints = new (C) Expr *[NumConstraints];
913 std::copy(first: constraints, last: constraints + NumConstraints, result: Constraints);
914
915 Clobbers = new (C) Expr *[NumClobbers];
916 std::copy(first: clobbers, last: clobbers + NumClobbers, result: Clobbers);
917}
918
919MSAsmStmt::MSAsmStmt(const ASTContext &C, SourceLocation asmloc,
920 SourceLocation lbraceloc, bool issimple, bool isvolatile,
921 ArrayRef<Token> asmtoks, unsigned numoutputs,
922 unsigned numinputs,
923 ArrayRef<StringRef> constraints, ArrayRef<Expr*> exprs,
924 StringRef asmstr, ArrayRef<StringRef> clobbers,
925 SourceLocation endloc)
926 : AsmStmt(MSAsmStmtClass, asmloc, issimple, isvolatile, numoutputs,
927 numinputs, clobbers.size()), LBraceLoc(lbraceloc),
928 EndLoc(endloc), NumAsmToks(asmtoks.size()) {
929 initialize(C, AsmString: asmstr, AsmToks: asmtoks, Constraints: constraints, Exprs: exprs, Clobbers: clobbers);
930}
931
932static StringRef copyIntoContext(const ASTContext &C, StringRef str) {
933 return str.copy(A: C);
934}
935
936void MSAsmStmt::initialize(const ASTContext &C, StringRef asmstr,
937 ArrayRef<Token> asmtoks,
938 ArrayRef<StringRef> constraints,
939 ArrayRef<Expr*> exprs,
940 ArrayRef<StringRef> clobbers) {
941 assert(NumAsmToks == asmtoks.size());
942 assert(NumClobbers == clobbers.size());
943
944 assert(exprs.size() == NumOutputs + NumInputs);
945 assert(exprs.size() == constraints.size());
946
947 AsmStr = copyIntoContext(C, str: asmstr);
948
949 Exprs = new (C) Stmt*[exprs.size()];
950 std::copy(first: exprs.begin(), last: exprs.end(), result: Exprs);
951
952 AsmToks = new (C) Token[asmtoks.size()];
953 std::copy(first: asmtoks.begin(), last: asmtoks.end(), result: AsmToks);
954
955 Constraints = new (C) StringRef[exprs.size()];
956 std::transform(first: constraints.begin(), last: constraints.end(), result: Constraints,
957 unary_op: [&](StringRef Constraint) {
958 return copyIntoContext(C, str: Constraint);
959 });
960
961 Clobbers = new (C) StringRef[NumClobbers];
962 // FIXME: Avoid the allocation/copy if at all possible.
963 std::transform(first: clobbers.begin(), last: clobbers.end(), result: Clobbers,
964 unary_op: [&](StringRef Clobber) {
965 return copyIntoContext(C, str: Clobber);
966 });
967}
968
969IfStmt::IfStmt(const ASTContext &Ctx, SourceLocation IL, IfStatementKind Kind,
970 Stmt *Init, VarDecl *Var, Expr *Cond, SourceLocation LPL,
971 SourceLocation RPL, Stmt *Then, SourceLocation EL, Stmt *Else)
972 : Stmt(IfStmtClass), LParenLoc(LPL), RParenLoc(RPL) {
973 bool HasElse = Else != nullptr;
974 bool HasVar = Var != nullptr;
975 bool HasInit = Init != nullptr;
976 IfStmtBits.HasElse = HasElse;
977 IfStmtBits.HasVar = HasVar;
978 IfStmtBits.HasInit = HasInit;
979
980 setStatementKind(Kind);
981
982 setCond(Cond);
983 setThen(Then);
984 if (HasElse)
985 setElse(Else);
986 if (HasVar)
987 setConditionVariable(Ctx, V: Var);
988 if (HasInit)
989 setInit(Init);
990
991 setIfLoc(IL);
992 if (HasElse)
993 setElseLoc(EL);
994}
995
996IfStmt::IfStmt(EmptyShell Empty, bool HasElse, bool HasVar, bool HasInit)
997 : Stmt(IfStmtClass, Empty) {
998 IfStmtBits.HasElse = HasElse;
999 IfStmtBits.HasVar = HasVar;
1000 IfStmtBits.HasInit = HasInit;
1001}
1002
1003IfStmt *IfStmt::Create(const ASTContext &Ctx, SourceLocation IL,
1004 IfStatementKind Kind, Stmt *Init, VarDecl *Var,
1005 Expr *Cond, SourceLocation LPL, SourceLocation RPL,
1006 Stmt *Then, SourceLocation EL, Stmt *Else) {
1007 bool HasElse = Else != nullptr;
1008 bool HasVar = Var != nullptr;
1009 bool HasInit = Init != nullptr;
1010 void *Mem = Ctx.Allocate(
1011 totalSizeToAlloc<Stmt *, SourceLocation>(
1012 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
1013 alignof(IfStmt));
1014 return new (Mem)
1015 IfStmt(Ctx, IL, Kind, Init, Var, Cond, LPL, RPL, Then, EL, Else);
1016}
1017
1018IfStmt *IfStmt::CreateEmpty(const ASTContext &Ctx, bool HasElse, bool HasVar,
1019 bool HasInit) {
1020 void *Mem = Ctx.Allocate(
1021 totalSizeToAlloc<Stmt *, SourceLocation>(
1022 NumMandatoryStmtPtr + HasElse + HasVar + HasInit, HasElse),
1023 alignof(IfStmt));
1024 return new (Mem) IfStmt(EmptyShell(), HasElse, HasVar, HasInit);
1025}
1026
1027VarDecl *IfStmt::getConditionVariable() {
1028 auto *DS = getConditionVariableDeclStmt();
1029 if (!DS)
1030 return nullptr;
1031 return cast<VarDecl>(Val: DS->getSingleDecl());
1032}
1033
1034void IfStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
1035 assert(hasVarStorage() &&
1036 "This if statement has no storage for a condition variable!");
1037
1038 if (!V) {
1039 getTrailingObjects<Stmt *>()[varOffset()] = nullptr;
1040 return;
1041 }
1042
1043 SourceRange VarRange = V->getSourceRange();
1044 getTrailingObjects<Stmt *>()[varOffset()] = new (Ctx)
1045 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
1046}
1047
1048bool IfStmt::isObjCAvailabilityCheck() const {
1049 return isa<ObjCAvailabilityCheckExpr>(Val: getCond());
1050}
1051
1052std::optional<Stmt *> IfStmt::getNondiscardedCase(const ASTContext &Ctx) {
1053 if (!isConstexpr() || getCond()->isValueDependent())
1054 return std::nullopt;
1055 return !getCond()->EvaluateKnownConstInt(Ctx) ? getElse() : getThen();
1056}
1057
1058std::optional<const Stmt *>
1059IfStmt::getNondiscardedCase(const ASTContext &Ctx) const {
1060 if (std::optional<Stmt *> Result =
1061 const_cast<IfStmt *>(this)->getNondiscardedCase(Ctx))
1062 return *Result;
1063 return std::nullopt;
1064}
1065
1066ForStmt::ForStmt(const ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
1067 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
1068 SourceLocation RP)
1069 : Stmt(ForStmtClass), LParenLoc(LP), RParenLoc(RP)
1070{
1071 SubExprs[INIT] = Init;
1072 setConditionVariable(C, V: condVar);
1073 SubExprs[COND] = Cond;
1074 SubExprs[INC] = Inc;
1075 SubExprs[BODY] = Body;
1076 ForStmtBits.ForLoc = FL;
1077}
1078
1079VarDecl *ForStmt::getConditionVariable() const {
1080 if (!SubExprs[CONDVAR])
1081 return nullptr;
1082
1083 auto *DS = cast<DeclStmt>(Val: SubExprs[CONDVAR]);
1084 return cast<VarDecl>(Val: DS->getSingleDecl());
1085}
1086
1087void ForStmt::setConditionVariable(const ASTContext &C, VarDecl *V) {
1088 if (!V) {
1089 SubExprs[CONDVAR] = nullptr;
1090 return;
1091 }
1092
1093 SourceRange VarRange = V->getSourceRange();
1094 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V), VarRange.getBegin(),
1095 VarRange.getEnd());
1096}
1097
1098SwitchStmt::SwitchStmt(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
1099 Expr *Cond, SourceLocation LParenLoc,
1100 SourceLocation RParenLoc)
1101 : Stmt(SwitchStmtClass), FirstCase(nullptr), LParenLoc(LParenLoc),
1102 RParenLoc(RParenLoc) {
1103 bool HasInit = Init != nullptr;
1104 bool HasVar = Var != nullptr;
1105 SwitchStmtBits.HasInit = HasInit;
1106 SwitchStmtBits.HasVar = HasVar;
1107 SwitchStmtBits.AllEnumCasesCovered = false;
1108
1109 setCond(Cond);
1110 setBody(nullptr);
1111 if (HasInit)
1112 setInit(Init);
1113 if (HasVar)
1114 setConditionVariable(Ctx, VD: Var);
1115
1116 setSwitchLoc(SourceLocation{});
1117}
1118
1119SwitchStmt::SwitchStmt(EmptyShell Empty, bool HasInit, bool HasVar)
1120 : Stmt(SwitchStmtClass, Empty) {
1121 SwitchStmtBits.HasInit = HasInit;
1122 SwitchStmtBits.HasVar = HasVar;
1123 SwitchStmtBits.AllEnumCasesCovered = false;
1124}
1125
1126SwitchStmt *SwitchStmt::Create(const ASTContext &Ctx, Stmt *Init, VarDecl *Var,
1127 Expr *Cond, SourceLocation LParenLoc,
1128 SourceLocation RParenLoc) {
1129 bool HasInit = Init != nullptr;
1130 bool HasVar = Var != nullptr;
1131 void *Mem = Ctx.Allocate(
1132 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
1133 alignof(SwitchStmt));
1134 return new (Mem) SwitchStmt(Ctx, Init, Var, Cond, LParenLoc, RParenLoc);
1135}
1136
1137SwitchStmt *SwitchStmt::CreateEmpty(const ASTContext &Ctx, bool HasInit,
1138 bool HasVar) {
1139 void *Mem = Ctx.Allocate(
1140 totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasInit + HasVar),
1141 alignof(SwitchStmt));
1142 return new (Mem) SwitchStmt(EmptyShell(), HasInit, HasVar);
1143}
1144
1145VarDecl *SwitchStmt::getConditionVariable() {
1146 auto *DS = getConditionVariableDeclStmt();
1147 if (!DS)
1148 return nullptr;
1149 return cast<VarDecl>(Val: DS->getSingleDecl());
1150}
1151
1152void SwitchStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
1153 assert(hasVarStorage() &&
1154 "This switch statement has no storage for a condition variable!");
1155
1156 if (!V) {
1157 getTrailingObjects()[varOffset()] = nullptr;
1158 return;
1159 }
1160
1161 SourceRange VarRange = V->getSourceRange();
1162 getTrailingObjects()[varOffset()] = new (Ctx)
1163 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
1164}
1165
1166WhileStmt::WhileStmt(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
1167 Stmt *Body, SourceLocation WL, SourceLocation LParenLoc,
1168 SourceLocation RParenLoc)
1169 : Stmt(WhileStmtClass) {
1170 bool HasVar = Var != nullptr;
1171 WhileStmtBits.HasVar = HasVar;
1172
1173 setCond(Cond);
1174 setBody(Body);
1175 if (HasVar)
1176 setConditionVariable(Ctx, V: Var);
1177
1178 setWhileLoc(WL);
1179 setLParenLoc(LParenLoc);
1180 setRParenLoc(RParenLoc);
1181}
1182
1183WhileStmt::WhileStmt(EmptyShell Empty, bool HasVar)
1184 : Stmt(WhileStmtClass, Empty) {
1185 WhileStmtBits.HasVar = HasVar;
1186}
1187
1188WhileStmt *WhileStmt::Create(const ASTContext &Ctx, VarDecl *Var, Expr *Cond,
1189 Stmt *Body, SourceLocation WL,
1190 SourceLocation LParenLoc,
1191 SourceLocation RParenLoc) {
1192 bool HasVar = Var != nullptr;
1193 void *Mem =
1194 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1195 alignof(WhileStmt));
1196 return new (Mem) WhileStmt(Ctx, Var, Cond, Body, WL, LParenLoc, RParenLoc);
1197}
1198
1199WhileStmt *WhileStmt::CreateEmpty(const ASTContext &Ctx, bool HasVar) {
1200 void *Mem =
1201 Ctx.Allocate(totalSizeToAlloc<Stmt *>(NumMandatoryStmtPtr + HasVar),
1202 alignof(WhileStmt));
1203 return new (Mem) WhileStmt(EmptyShell(), HasVar);
1204}
1205
1206VarDecl *WhileStmt::getConditionVariable() {
1207 auto *DS = getConditionVariableDeclStmt();
1208 if (!DS)
1209 return nullptr;
1210 return cast<VarDecl>(Val: DS->getSingleDecl());
1211}
1212
1213void WhileStmt::setConditionVariable(const ASTContext &Ctx, VarDecl *V) {
1214 assert(hasVarStorage() &&
1215 "This while statement has no storage for a condition variable!");
1216
1217 if (!V) {
1218 getTrailingObjects()[varOffset()] = nullptr;
1219 return;
1220 }
1221
1222 SourceRange VarRange = V->getSourceRange();
1223 getTrailingObjects()[varOffset()] = new (Ctx)
1224 DeclStmt(DeclGroupRef(V), VarRange.getBegin(), VarRange.getEnd());
1225}
1226
1227// IndirectGotoStmt
1228LabelDecl *IndirectGotoStmt::getConstantTarget() {
1229 if (auto *E = dyn_cast<AddrLabelExpr>(Val: getTarget()->IgnoreParenImpCasts()))
1230 return E->getLabel();
1231 return nullptr;
1232}
1233
1234// ReturnStmt
1235ReturnStmt::ReturnStmt(SourceLocation RL, Expr *E, const VarDecl *NRVOCandidate)
1236 : Stmt(ReturnStmtClass), RetExpr(E) {
1237 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1238 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1239 if (HasNRVOCandidate)
1240 setNRVOCandidate(NRVOCandidate);
1241 setReturnLoc(RL);
1242}
1243
1244ReturnStmt::ReturnStmt(EmptyShell Empty, bool HasNRVOCandidate)
1245 : Stmt(ReturnStmtClass, Empty) {
1246 ReturnStmtBits.HasNRVOCandidate = HasNRVOCandidate;
1247}
1248
1249ReturnStmt *ReturnStmt::Create(const ASTContext &Ctx, SourceLocation RL,
1250 Expr *E, const VarDecl *NRVOCandidate) {
1251 bool HasNRVOCandidate = NRVOCandidate != nullptr;
1252 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1253 alignof(ReturnStmt));
1254 return new (Mem) ReturnStmt(RL, E, NRVOCandidate);
1255}
1256
1257ReturnStmt *ReturnStmt::CreateEmpty(const ASTContext &Ctx,
1258 bool HasNRVOCandidate) {
1259 void *Mem = Ctx.Allocate(totalSizeToAlloc<const VarDecl *>(HasNRVOCandidate),
1260 alignof(ReturnStmt));
1261 return new (Mem) ReturnStmt(EmptyShell(), HasNRVOCandidate);
1262}
1263
1264// CaseStmt
1265CaseStmt *CaseStmt::Create(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
1266 SourceLocation caseLoc, SourceLocation ellipsisLoc,
1267 SourceLocation colonLoc) {
1268 bool CaseStmtIsGNURange = rhs != nullptr;
1269 void *Mem = Ctx.Allocate(
1270 totalSizeToAlloc<Stmt *, SourceLocation>(
1271 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1272 alignof(CaseStmt));
1273 return new (Mem) CaseStmt(lhs, rhs, caseLoc, ellipsisLoc, colonLoc);
1274}
1275
1276CaseStmt *CaseStmt::CreateEmpty(const ASTContext &Ctx,
1277 bool CaseStmtIsGNURange) {
1278 void *Mem = Ctx.Allocate(
1279 totalSizeToAlloc<Stmt *, SourceLocation>(
1280 NumMandatoryStmtPtr + CaseStmtIsGNURange, CaseStmtIsGNURange),
1281 alignof(CaseStmt));
1282 return new (Mem) CaseStmt(EmptyShell(), CaseStmtIsGNURange);
1283}
1284
1285SEHTryStmt::SEHTryStmt(bool IsCXXTry, SourceLocation TryLoc, Stmt *TryBlock,
1286 Stmt *Handler)
1287 : Stmt(SEHTryStmtClass), IsCXXTry(IsCXXTry), TryLoc(TryLoc) {
1288 Children[TRY] = TryBlock;
1289 Children[HANDLER] = Handler;
1290}
1291
1292SEHTryStmt* SEHTryStmt::Create(const ASTContext &C, bool IsCXXTry,
1293 SourceLocation TryLoc, Stmt *TryBlock,
1294 Stmt *Handler) {
1295 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
1296}
1297
1298SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
1299 return dyn_cast<SEHExceptStmt>(Val: getHandler());
1300}
1301
1302SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
1303 return dyn_cast<SEHFinallyStmt>(Val: getHandler());
1304}
1305
1306SEHExceptStmt::SEHExceptStmt(SourceLocation Loc, Expr *FilterExpr, Stmt *Block)
1307 : Stmt(SEHExceptStmtClass), Loc(Loc) {
1308 Children[FILTER_EXPR] = FilterExpr;
1309 Children[BLOCK] = Block;
1310}
1311
1312SEHExceptStmt* SEHExceptStmt::Create(const ASTContext &C, SourceLocation Loc,
1313 Expr *FilterExpr, Stmt *Block) {
1314 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
1315}
1316
1317SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc, Stmt *Block)
1318 : Stmt(SEHFinallyStmtClass), Loc(Loc), Block(Block) {}
1319
1320SEHFinallyStmt* SEHFinallyStmt::Create(const ASTContext &C, SourceLocation Loc,
1321 Stmt *Block) {
1322 return new(C)SEHFinallyStmt(Loc,Block);
1323}
1324
1325CapturedStmt::Capture::Capture(SourceLocation Loc, VariableCaptureKind Kind,
1326 VarDecl *Var)
1327 : VarAndKind(Var, Kind), Loc(Loc) {
1328 switch (Kind) {
1329 case VCK_This:
1330 assert(!Var && "'this' capture cannot have a variable!");
1331 break;
1332 case VCK_ByRef:
1333 assert(Var && "capturing by reference must have a variable!");
1334 break;
1335 case VCK_ByCopy:
1336 assert(Var && "capturing by copy must have a variable!");
1337 break;
1338 case VCK_VLAType:
1339 assert(!Var &&
1340 "Variable-length array type capture cannot have a variable!");
1341 break;
1342 }
1343}
1344
1345CapturedStmt::VariableCaptureKind
1346CapturedStmt::Capture::getCaptureKind() const {
1347 return VarAndKind.getInt();
1348}
1349
1350VarDecl *CapturedStmt::Capture::getCapturedVar() const {
1351 assert((capturesVariable() || capturesVariableByCopy()) &&
1352 "No variable available for 'this' or VAT capture");
1353 return VarAndKind.getPointer();
1354}
1355
1356CapturedStmt::Capture *CapturedStmt::getStoredCaptures() const {
1357 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1358
1359 // Offset of the first Capture object.
1360 unsigned FirstCaptureOffset = llvm::alignTo(Value: Size, Align: alignof(Capture));
1361
1362 return reinterpret_cast<Capture *>(
1363 reinterpret_cast<char *>(const_cast<CapturedStmt *>(this))
1364 + FirstCaptureOffset);
1365}
1366
1367CapturedStmt::CapturedStmt(Stmt *S, CapturedRegionKind Kind,
1368 ArrayRef<Capture> Captures,
1369 ArrayRef<Expr *> CaptureInits,
1370 CapturedDecl *CD,
1371 RecordDecl *RD)
1372 : Stmt(CapturedStmtClass), NumCaptures(Captures.size()),
1373 CapDeclAndKind(CD, Kind), TheRecordDecl(RD) {
1374 assert( S && "null captured statement");
1375 assert(CD && "null captured declaration for captured statement");
1376 assert(RD && "null record declaration for captured statement");
1377
1378 // Copy initialization expressions.
1379 Stmt **Stored = getStoredStmts();
1380 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1381 *Stored++ = CaptureInits[I];
1382
1383 // Copy the statement being captured.
1384 *Stored = S;
1385
1386 // Copy all Capture objects.
1387 Capture *Buffer = getStoredCaptures();
1388 std::copy(first: Captures.begin(), last: Captures.end(), result: Buffer);
1389}
1390
1391CapturedStmt::CapturedStmt(EmptyShell Empty, unsigned NumCaptures)
1392 : Stmt(CapturedStmtClass, Empty), NumCaptures(NumCaptures),
1393 CapDeclAndKind(nullptr, CR_Default) {
1394 getStoredStmts()[NumCaptures] = nullptr;
1395
1396 // Construct default capture objects.
1397 Capture *Buffer = getStoredCaptures();
1398 for (unsigned I = 0, N = NumCaptures; I != N; ++I)
1399 new (Buffer++) Capture();
1400}
1401
1402CapturedStmt *CapturedStmt::Create(const ASTContext &Context, Stmt *S,
1403 CapturedRegionKind Kind,
1404 ArrayRef<Capture> Captures,
1405 ArrayRef<Expr *> CaptureInits,
1406 CapturedDecl *CD,
1407 RecordDecl *RD) {
1408 // The layout is
1409 //
1410 // -----------------------------------------------------------
1411 // | CapturedStmt, Init, ..., Init, S, Capture, ..., Capture |
1412 // ----------------^-------------------^----------------------
1413 // getStoredStmts() getStoredCaptures()
1414 //
1415 // where S is the statement being captured.
1416 //
1417 assert(CaptureInits.size() == Captures.size() && "wrong number of arguments");
1418
1419 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (Captures.size() + 1);
1420 if (!Captures.empty()) {
1421 // Realign for the following Capture array.
1422 Size = llvm::alignTo(Value: Size, Align: alignof(Capture));
1423 Size += sizeof(Capture) * Captures.size();
1424 }
1425
1426 void *Mem = Context.Allocate(Size);
1427 return new (Mem) CapturedStmt(S, Kind, Captures, CaptureInits, CD, RD);
1428}
1429
1430CapturedStmt *CapturedStmt::CreateDeserialized(const ASTContext &Context,
1431 unsigned NumCaptures) {
1432 unsigned Size = sizeof(CapturedStmt) + sizeof(Stmt *) * (NumCaptures + 1);
1433 if (NumCaptures > 0) {
1434 // Realign for the following Capture array.
1435 Size = llvm::alignTo(Value: Size, Align: alignof(Capture));
1436 Size += sizeof(Capture) * NumCaptures;
1437 }
1438
1439 void *Mem = Context.Allocate(Size);
1440 return new (Mem) CapturedStmt(EmptyShell(), NumCaptures);
1441}
1442
1443Stmt::child_range CapturedStmt::children() {
1444 // Children are captured field initializers.
1445 return child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1446}
1447
1448Stmt::const_child_range CapturedStmt::children() const {
1449 return const_child_range(getStoredStmts(), getStoredStmts() + NumCaptures);
1450}
1451
1452CapturedDecl *CapturedStmt::getCapturedDecl() {
1453 return CapDeclAndKind.getPointer();
1454}
1455
1456const CapturedDecl *CapturedStmt::getCapturedDecl() const {
1457 return CapDeclAndKind.getPointer();
1458}
1459
1460/// Set the outlined function declaration.
1461void CapturedStmt::setCapturedDecl(CapturedDecl *D) {
1462 assert(D && "null CapturedDecl");
1463 CapDeclAndKind.setPointer(D);
1464}
1465
1466/// Retrieve the captured region kind.
1467CapturedRegionKind CapturedStmt::getCapturedRegionKind() const {
1468 return CapDeclAndKind.getInt();
1469}
1470
1471/// Set the captured region kind.
1472void CapturedStmt::setCapturedRegionKind(CapturedRegionKind Kind) {
1473 CapDeclAndKind.setInt(Kind);
1474}
1475
1476bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
1477 for (const auto &I : captures()) {
1478 if (!I.capturesVariable() && !I.capturesVariableByCopy())
1479 continue;
1480 if (I.getCapturedVar()->getCanonicalDecl() == Var->getCanonicalDecl())
1481 return true;
1482 }
1483
1484 return false;
1485}
1486

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang/lib/AST/Stmt.cpp