blob: 0deaaa83bada479e45515c6557a6770a89cb1cdd [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Stmt.cpp - Statement AST Node Implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Stmt class and statement subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Stmt.h"
15#include "clang/AST/ExprCXX.h"
Steve Narofff494b572008-05-29 21:12:08 +000016#include "clang/AST/ExprObjC.h"
Chris Lattner16f00492009-04-26 01:32:48 +000017#include "clang/AST/StmtCXX.h"
18#include "clang/AST/StmtObjC.h"
Sebastian Redl4b07b292008-12-22 19:15:10 +000019#include "clang/AST/Type.h"
Ted Kremenek11e5a7f2009-02-06 01:42:09 +000020#include "clang/AST/ASTContext.h"
Chris Lattner3182db12009-03-10 23:51:40 +000021#include "clang/AST/ASTDiagnostic.h"
Chris Lattner9bffb072010-04-23 16:29:58 +000022#include "clang/Basic/TargetInfo.h"
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000023#include "llvm/Support/raw_ostream.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
Reid Spencer5f016e22007-07-11 17:01:13 +000026static struct StmtClassNameTable {
Chris Lattner63381352007-08-25 01:42:24 +000027 const char *Name;
28 unsigned Counter;
29 unsigned Size;
Sean Hunt4bfe1962010-05-05 15:24:00 +000030} StmtClassInfo[Stmt::lastStmtConstant+1];
Chris Lattner63381352007-08-25 01:42:24 +000031
32static StmtClassNameTable &getStmtInfoTableEntry(Stmt::StmtClass E) {
33 static bool Initialized = false;
34 if (Initialized)
35 return StmtClassInfo[E];
36
37 // Intialize the table on the first use.
38 Initialized = true;
Sean Hunt7381d5c2010-05-18 06:22:21 +000039#define ABSTRACT_STMT(STMT)
Douglas Gregorf2cad862008-11-14 12:46:07 +000040#define STMT(CLASS, PARENT) \
41 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Name = #CLASS; \
42 StmtClassInfo[(unsigned)Stmt::CLASS##Class].Size = sizeof(CLASS);
Sean Hunt4bfe1962010-05-05 15:24:00 +000043#include "clang/AST/StmtNodes.inc"
Nico Weber608b17f2008-08-05 23:15:29 +000044
Chris Lattner63381352007-08-25 01:42:24 +000045 return StmtClassInfo[E];
46}
47
Reid Spencer5f016e22007-07-11 17:01:13 +000048const char *Stmt::getStmtClassName() const {
John McCall8e6285a2010-10-26 08:39:16 +000049 return getStmtInfoTableEntry((StmtClass) StmtBits.sClass).Name;
Reid Spencer5f016e22007-07-11 17:01:13 +000050}
51
52void Stmt::PrintStats() {
Chris Lattner63381352007-08-25 01:42:24 +000053 // Ensure the table is primed.
54 getStmtInfoTableEntry(Stmt::NullStmtClass);
Nico Weber608b17f2008-08-05 23:15:29 +000055
Reid Spencer5f016e22007-07-11 17:01:13 +000056 unsigned sum = 0;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000057 llvm::errs() << "\n*** Stmt/Expr Stats:\n";
Sean Hunt4bfe1962010-05-05 15:24:00 +000058 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000059 if (StmtClassInfo[i].Name == 0) continue;
60 sum += StmtClassInfo[i].Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000062 llvm::errs() << " " << sum << " stmts/exprs total.\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000063 sum = 0;
Sean Hunt4bfe1962010-05-05 15:24:00 +000064 for (int i = 0; i != Stmt::lastStmtConstant+1; i++) {
Chris Lattner63381352007-08-25 01:42:24 +000065 if (StmtClassInfo[i].Name == 0) continue;
Douglas Gregordbe833d2009-05-26 14:40:08 +000066 if (StmtClassInfo[i].Counter == 0) continue;
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000067 llvm::errs() << " " << StmtClassInfo[i].Counter << " "
68 << StmtClassInfo[i].Name << ", " << StmtClassInfo[i].Size
69 << " each (" << StmtClassInfo[i].Counter*StmtClassInfo[i].Size
70 << " bytes)\n";
Chris Lattner63381352007-08-25 01:42:24 +000071 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
Chandler Carruthb43c8ec2011-07-04 06:13:27 +000073
74 llvm::errs() << "Total bytes = " << sum << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000075}
76
77void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000078 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000079}
80
Daniel Dunbar02892a62012-03-05 21:42:49 +000081bool Stmt::StatisticsEnabled = false;
82void Stmt::EnableStatistics() {
83 StatisticsEnabled = true;
Reid Spencer5f016e22007-07-11 17:01:13 +000084}
85
John McCall7e5e5f42011-07-07 06:58:02 +000086Stmt *Stmt::IgnoreImplicit() {
87 Stmt *s = this;
88
89 if (ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(s))
90 s = ewc->getSubExpr();
91
92 while (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(s))
93 s = ice->getSubExpr();
94
95 return s;
96}
97
Chandler Carrutha1364be2011-09-10 00:02:34 +000098/// \brief Strip off all label-like statements.
99///
100/// This will strip off label statements, case statements, and default
101/// statements recursively.
102const Stmt *Stmt::stripLabelLikeStatements() const {
103 const Stmt *S = this;
104 while (true) {
105 if (const LabelStmt *LS = dyn_cast<LabelStmt>(S))
106 S = LS->getSubStmt();
107 else if (const SwitchCase *SC = dyn_cast<SwitchCase>(S))
108 S = SC->getSubStmt();
109 else
110 return S;
111 }
112}
113
John McCall63c00d72011-02-09 08:16:59 +0000114namespace {
115 struct good {};
116 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +0000117
118 // These silly little functions have to be static inline to suppress
119 // unused warnings, and they have to be defined to suppress other
120 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000121 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +0000122
123 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000124 template <class T> good implements_children(children_t T::*) {
125 return good();
126 }
127 static inline bad implements_children(children_t Stmt::*) {
128 return bad();
129 }
John McCall63c00d72011-02-09 08:16:59 +0000130
131 typedef SourceRange getSourceRange_t() const;
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000132 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
133 return good();
134 }
135 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
136 return bad();
137 }
John McCall63c00d72011-02-09 08:16:59 +0000138
139#define ASSERT_IMPLEMENTS_children(type) \
140 (void) sizeof(is_good(implements_children(&type::children)))
141#define ASSERT_IMPLEMENTS_getSourceRange(type) \
142 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
143}
144
145/// Check whether the various Stmt classes implement their member
146/// functions.
147static inline void check_implementations() {
148#define ABSTRACT_STMT(type)
149#define STMT(type, base) \
150 ASSERT_IMPLEMENTS_children(type); \
151 ASSERT_IMPLEMENTS_getSourceRange(type);
152#include "clang/AST/StmtNodes.inc"
153}
154
155Stmt::child_range Stmt::children() {
156 switch (getStmtClass()) {
157 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
158#define ABSTRACT_STMT(type)
159#define STMT(type, base) \
160 case Stmt::type##Class: \
161 return static_cast<type*>(this)->children();
162#include "clang/AST/StmtNodes.inc"
163 }
164 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000165}
166
167SourceRange Stmt::getSourceRange() const {
168 switch (getStmtClass()) {
169 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
170#define ABSTRACT_STMT(type)
171#define STMT(type, base) \
172 case Stmt::type##Class: \
173 return static_cast<const type*>(this)->getSourceRange();
174#include "clang/AST/StmtNodes.inc"
175 }
176 llvm_unreachable("unknown statement kind!");
John McCall63c00d72011-02-09 08:16:59 +0000177}
178
Douglas Gregor025452f2009-04-17 00:04:06 +0000179void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
180 if (this->Body)
181 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000182 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000183
184 Body = new (C) Stmt*[NumStmts];
185 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
186}
Reid Spencer5f016e22007-07-11 17:01:13 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000189 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000190}
191
Steve Naroff507f2d52007-08-31 23:49:30 +0000192// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000193SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000194 if (RetExpr)
195 return SourceRange(RetLoc, RetExpr->getLocEnd());
196 else
197 return SourceRange(RetLoc);
198}
199
Ted Kremenekd48ade62007-10-01 16:34:52 +0000200bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000201 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000202 default:
203 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000204
Ted Kremenekd48ade62007-10-01 16:34:52 +0000205 case CallExprClass:
206 case ConditionalOperatorClass:
207 case ChooseExprClass:
208 case StmtExprClass:
209 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000210 return true;
211
Ted Kremenekd48ade62007-10-01 16:34:52 +0000212 case Stmt::BinaryOperatorClass: {
213 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000214 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000215 return true;
216 else
217 return false;
218 }
219 }
220}
221
Chris Lattnerb3277932009-03-10 04:59:06 +0000222Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000223 return cast<Expr>(Exprs[i]);
224}
Chris Lattnerb3277932009-03-10 04:59:06 +0000225
226/// getOutputConstraint - Return the constraint string for the specified
227/// output operand. All output constraints are known to be non-empty (either
228/// '=' or '+').
Chris Lattner5f9e2722011-07-23 10:55:15 +0000229StringRef AsmStmt::getOutputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000230 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000231}
Chris Lattnerb3277932009-03-10 04:59:06 +0000232
Chris Lattner85759272009-03-11 00:23:13 +0000233/// getNumPlusOperands - Return the number of output operands that have a "+"
234/// constraint.
235unsigned AsmStmt::getNumPlusOperands() const {
236 unsigned Res = 0;
237 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
238 if (isOutputPlusConstraint(i))
239 ++Res;
240 return Res;
241}
242
Chris Lattnerb3277932009-03-10 04:59:06 +0000243Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000244 return cast<Expr>(Exprs[i + NumOutputs]);
245}
Chris Lattner935f0f02011-02-21 22:09:29 +0000246void AsmStmt::setInputExpr(unsigned i, Expr *E) {
247 Exprs[i + NumOutputs] = E;
248}
249
Chris Lattnerb3277932009-03-10 04:59:06 +0000250
251/// getInputConstraint - Return the specified input constraint. Unlike output
252/// constraints, these can be empty.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253StringRef AsmStmt::getInputConstraint(unsigned i) const {
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000254 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000255}
256
Chris Lattner10ca96a2009-03-10 06:33:24 +0000257
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000258void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000259 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000260 StringLiteral **Constraints,
261 Stmt **Exprs,
262 unsigned NumOutputs,
Sean Huntc3021132010-05-05 15:23:54 +0000263 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000264 StringLiteral **Clobbers,
265 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000266 this->NumOutputs = NumOutputs;
267 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000268 this->NumClobbers = NumClobbers;
269
270 unsigned NumExprs = NumOutputs + NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000271
Anders Carlsson966146e2010-01-30 23:19:41 +0000272 C.Deallocate(this->Names);
273 this->Names = new (C) IdentifierInfo*[NumExprs];
274 std::copy(Names, Names + NumExprs, this->Names);
Sean Huntc3021132010-05-05 15:23:54 +0000275
Anders Carlsson966146e2010-01-30 23:19:41 +0000276 C.Deallocate(this->Exprs);
277 this->Exprs = new (C) Stmt*[NumExprs];
278 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Sean Huntc3021132010-05-05 15:23:54 +0000279
Anders Carlsson966146e2010-01-30 23:19:41 +0000280 C.Deallocate(this->Constraints);
281 this->Constraints = new (C) StringLiteral*[NumExprs];
282 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Sean Huntc3021132010-05-05 15:23:54 +0000283
Anders Carlsson966146e2010-01-30 23:19:41 +0000284 C.Deallocate(this->Clobbers);
285 this->Clobbers = new (C) StringLiteral*[NumClobbers];
286 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000287}
288
Chris Lattner10ca96a2009-03-10 06:33:24 +0000289/// getNamedOperand - Given a symbolic operand reference like %[foo],
290/// translate this into a numeric value needed to reference the same operand.
291/// This returns -1 if the operand name is invalid.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000292int AsmStmt::getNamedOperand(StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000293 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattner10ca96a2009-03-10 06:33:24 +0000295 // Check if this is an output operand.
296 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
297 if (getOutputName(i) == SymbolicName)
298 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000299 }
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Chris Lattner10ca96a2009-03-10 06:33:24 +0000301 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
302 if (getInputName(i) == SymbolicName)
303 return getNumOutputs() + NumPlusOperands + i;
304
305 // Not found.
306 return -1;
307}
308
Chris Lattner458cd9c2009-03-10 23:21:44 +0000309/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
310/// it into pieces. If the asm string is erroneous, emit errors and return
311/// true, otherwise return false.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000312unsigned AsmStmt::AnalyzeAsmString(SmallVectorImpl<AsmStringPiece>&Pieces,
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000313 ASTContext &C, unsigned &DiagOffs) const {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000314 StringRef Str = getAsmString()->getString();
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000315 const char *StrStart = Str.begin();
316 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000317 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000318
Chris Lattner458cd9c2009-03-10 23:21:44 +0000319 // "Simple" inline asms have no constraints or operands, just convert the asm
320 // string to escape $'s.
321 if (isSimple()) {
322 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000323 for (; CurPtr != StrEnd; ++CurPtr) {
324 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000325 case '$':
326 Result += "$$";
327 break;
328 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000329 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000330 break;
331 }
332 }
333 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000334 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000335 }
336
337 // CurStringPiece - The current string that we are building up as we scan the
338 // asm string.
339 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000341 bool HasVariants = !C.getTargetInfo().hasNoAsmVariants();
Sean Huntc3021132010-05-05 15:23:54 +0000342
Chris Lattner458cd9c2009-03-10 23:21:44 +0000343 while (1) {
344 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000345 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000346 if (!CurStringPiece.empty())
347 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000348 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000349 }
Mike Stump1eb44332009-09-09 15:08:12 +0000350
Chris Lattner3182db12009-03-10 23:51:40 +0000351 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000352 switch (CurChar) {
353 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000354 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
355 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
356 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000357 case '%':
358 break;
359 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000360 CurStringPiece += CurChar;
361 continue;
362 }
Sean Huntc3021132010-05-05 15:23:54 +0000363
Chris Lattner458cd9c2009-03-10 23:21:44 +0000364 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000365 if (CurPtr == StrEnd) {
366 // % at end of string is invalid (no escape).
367 DiagOffs = CurPtr-StrStart-1;
368 return diag::err_asm_invalid_escape;
369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattner3182db12009-03-10 23:51:40 +0000371 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000372 if (EscapedChar == '%') { // %% -> %
373 // Escaped percentage sign.
374 CurStringPiece += '%';
375 continue;
376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
Chris Lattner458cd9c2009-03-10 23:21:44 +0000378 if (EscapedChar == '=') { // %= -> Generate an unique ID.
379 CurStringPiece += "${:uid}";
380 continue;
381 }
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattner458cd9c2009-03-10 23:21:44 +0000383 // Otherwise, we have an operand. If we have accumulated a string so far,
384 // add it to the Pieces list.
385 if (!CurStringPiece.empty()) {
386 Pieces.push_back(AsmStringPiece(CurStringPiece));
387 CurStringPiece.clear();
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Chris Lattner458cd9c2009-03-10 23:21:44 +0000390 // Handle %x4 and %x[foo] by capturing x as the modifier character.
391 char Modifier = '\0';
392 if (isalpha(EscapedChar)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000393 if (CurPtr == StrEnd) { // Premature end.
394 DiagOffs = CurPtr-StrStart-1;
395 return diag::err_asm_invalid_escape;
396 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000397 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000398 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattner458cd9c2009-03-10 23:21:44 +0000401 if (isdigit(EscapedChar)) {
402 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000403 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattnercafc2222009-03-11 22:52:17 +0000405 --CurPtr;
406 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000407 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattner85759272009-03-11 00:23:13 +0000409 unsigned NumOperands =
410 getNumOutputs() + getNumPlusOperands() + getNumInputs();
411 if (N >= NumOperands) {
412 DiagOffs = CurPtr-StrStart-1;
413 return diag::err_asm_invalid_operand_number;
414 }
415
Chris Lattner458cd9c2009-03-10 23:21:44 +0000416 Pieces.push_back(AsmStringPiece(N, Modifier));
417 continue;
418 }
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner458cd9c2009-03-10 23:21:44 +0000420 // Handle %[foo], a symbolic operand reference.
421 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000422 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000424 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000425 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000426 if (NameEnd == 0)
427 return diag::err_asm_unterminated_symbolic_operand_name;
428 if (NameEnd == CurPtr)
429 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattner5f9e2722011-07-23 10:55:15 +0000431 StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Chris Lattner458cd9c2009-03-10 23:21:44 +0000433 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000434 if (N == -1) {
435 // Verify that an operand with that name exists.
436 DiagOffs = CurPtr-StrStart;
437 return diag::err_asm_unknown_symbolic_operand_name;
438 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000439 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000441 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000442 continue;
443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner2ff0f422009-03-10 23:57:07 +0000445 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000446 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000447 }
448}
449
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000450QualType CXXCatchStmt::getCaughtType() const {
451 if (ExceptionDecl)
452 return ExceptionDecl->getType();
453 return QualType();
454}
455
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000456//===----------------------------------------------------------------------===//
457// Constructors
458//===----------------------------------------------------------------------===//
459
Sean Huntc3021132010-05-05 15:23:54 +0000460AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
461 bool isvolatile, bool msasm,
Anders Carlsson966146e2010-01-30 23:19:41 +0000462 unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000463 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000464 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
465 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000466 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000467 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson966146e2010-01-30 23:19:41 +0000468 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000469
Anders Carlsson966146e2010-01-30 23:19:41 +0000470 unsigned NumExprs = NumOutputs +NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000471
Anders Carlsson966146e2010-01-30 23:19:41 +0000472 Names = new (C) IdentifierInfo*[NumExprs];
473 std::copy(names, names + NumExprs, Names);
474
475 Exprs = new (C) Stmt*[NumExprs];
476 std::copy(exprs, exprs + NumExprs, Exprs);
477
478 Constraints = new (C) StringLiteral*[NumExprs];
479 std::copy(constraints, constraints + NumExprs, Constraints);
480
481 Clobbers = new (C) StringLiteral*[NumClobbers];
482 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000483}
484
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000485ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
486 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000487 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000488: Stmt(ObjCForCollectionStmtClass) {
489 SubExprs[ELEM] = Elem;
490 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
491 SubExprs[BODY] = Body;
492 ForLoc = FCL;
493 RParenLoc = RPL;
494}
495
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000496ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
497 Stmt **CatchStmts, unsigned NumCatchStmts,
498 Stmt *atFinallyStmt)
499 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
500 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
501{
502 Stmt **Stmts = getStmts();
503 Stmts[0] = atTryStmt;
504 for (unsigned I = 0; I != NumCatchStmts; ++I)
505 Stmts[I + 1] = CatchStmts[I];
Sean Huntc3021132010-05-05 15:23:54 +0000506
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000507 if (HasFinally)
508 Stmts[NumCatchStmts + 1] = atFinallyStmt;
509}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000510
Sean Huntc3021132010-05-05 15:23:54 +0000511ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
512 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000513 Stmt *atTryStmt,
Sean Huntc3021132010-05-05 15:23:54 +0000514 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000515 unsigned NumCatchStmts,
516 Stmt *atFinallyStmt) {
Sean Huntc3021132010-05-05 15:23:54 +0000517 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000518 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000519 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000520 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
521 atFinallyStmt);
522}
Ted Kremenekff981022008-02-01 21:28:59 +0000523
Sean Huntc3021132010-05-05 15:23:54 +0000524ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000525 unsigned NumCatchStmts,
526 bool HasFinally) {
Sean Huntc3021132010-05-05 15:23:54 +0000527 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000528 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000529 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Sean Huntc3021132010-05-05 15:23:54 +0000530 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000531}
Nico Weber608b17f2008-08-05 23:15:29 +0000532
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000533SourceRange ObjCAtTryStmt::getSourceRange() const {
534 SourceLocation EndLoc;
535 if (HasFinally)
536 EndLoc = getFinallyStmt()->getLocEnd();
537 else if (NumCatchStmts)
538 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
539 else
540 EndLoc = getTryBody()->getLocEnd();
Sean Huntc3021132010-05-05 15:23:54 +0000541
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000542 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000543}
544
Sam Weiniga1a396d2010-02-03 03:56:39 +0000545CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000546 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000547 unsigned numHandlers) {
548 std::size_t Size = sizeof(CXXTryStmt);
549 Size += ((numHandlers + 1) * sizeof(Stmt));
550
Chris Lattner32488542010-10-30 05:14:06 +0000551 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000552 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
553}
554
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000555CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
556 unsigned numHandlers) {
557 std::size_t Size = sizeof(CXXTryStmt);
558 Size += ((numHandlers + 1) * sizeof(Stmt));
559
Chris Lattner32488542010-10-30 05:14:06 +0000560 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000561 return new (Mem) CXXTryStmt(Empty, numHandlers);
562}
563
Sam Weiniga1a396d2010-02-03 03:56:39 +0000564CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000565 Stmt **handlers, unsigned numHandlers)
566 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000567 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000568 Stmts[0] = tryBlock;
569 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
570}
571
Richard Smithad762fc2011-04-14 22:09:26 +0000572CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
573 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
574 Stmt *Body, SourceLocation FL,
575 SourceLocation CL, SourceLocation RPL)
576 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
577 SubExprs[RANGE] = Range;
578 SubExprs[BEGINEND] = BeginEndStmt;
579 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
580 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
581 SubExprs[LOOPVAR] = LoopVar;
582 SubExprs[BODY] = Body;
583}
584
585Expr *CXXForRangeStmt::getRangeInit() {
586 DeclStmt *RangeStmt = getRangeStmt();
587 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
588 assert(RangeDecl &&& "for-range should have a single var decl");
589 return RangeDecl->getInit();
590}
591
592const Expr *CXXForRangeStmt::getRangeInit() const {
593 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
594}
595
596VarDecl *CXXForRangeStmt::getLoopVariable() {
597 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
598 assert(LV && "No loop variable in CXXForRangeStmt");
599 return cast<VarDecl>(LV);
600}
601
602const VarDecl *CXXForRangeStmt::getLoopVariable() const {
603 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
604}
605
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000606IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000607 Stmt *then, SourceLocation EL, Stmt *elsev)
608 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000609{
610 setConditionVariable(C, var);
611 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
612 SubExprs[THEN] = then;
613 SubExprs[ELSE] = elsev;
614}
615
616VarDecl *IfStmt::getConditionVariable() const {
617 if (!SubExprs[VAR])
618 return 0;
619
620 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
621 return cast<VarDecl>(DS->getSingleDecl());
622}
623
624void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
625 if (!V) {
626 SubExprs[VAR] = 0;
627 return;
628 }
629
630 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
631 V->getSourceRange().getBegin(),
632 V->getSourceRange().getEnd());
633}
634
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000635ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
636 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
637 SourceLocation RP)
638 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
639{
640 SubExprs[INIT] = Init;
641 setConditionVariable(C, condVar);
642 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
643 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
644 SubExprs[BODY] = Body;
645}
646
647VarDecl *ForStmt::getConditionVariable() const {
648 if (!SubExprs[CONDVAR])
649 return 0;
650
651 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
652 return cast<VarDecl>(DS->getSingleDecl());
653}
654
655void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
656 if (!V) {
657 SubExprs[CONDVAR] = 0;
658 return;
659 }
660
661 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V),
662 V->getSourceRange().getBegin(),
663 V->getSourceRange().getEnd());
664}
665
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000666SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
Ted Kremenek780d8852010-09-09 00:06:01 +0000667 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000668{
669 setConditionVariable(C, Var);
670 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
671 SubExprs[BODY] = NULL;
672}
673
674VarDecl *SwitchStmt::getConditionVariable() const {
675 if (!SubExprs[VAR])
676 return 0;
677
678 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
679 return cast<VarDecl>(DS->getSingleDecl());
680}
681
682void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
683 if (!V) {
684 SubExprs[VAR] = 0;
685 return;
686 }
687
688 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
689 V->getSourceRange().getBegin(),
690 V->getSourceRange().getEnd());
691}
692
John McCall63c00d72011-02-09 08:16:59 +0000693Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000694 if (isa<CaseStmt>(this))
695 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000696 return cast<DefaultStmt>(this)->getSubStmt();
697}
698
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000699WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
700 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000701 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000702 setConditionVariable(C, Var);
703 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
704 SubExprs[BODY] = body;
705 WhileLoc = WL;
706}
707
708VarDecl *WhileStmt::getConditionVariable() const {
709 if (!SubExprs[VAR])
710 return 0;
711
712 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
713 return cast<VarDecl>(DS->getSingleDecl());
714}
715
716void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
717 if (!V) {
718 SubExprs[VAR] = 0;
719 return;
720 }
721
722 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
723 V->getSourceRange().getBegin(),
724 V->getSourceRange().getEnd());
725}
726
Ted Kremenek82977772007-08-24 21:09:09 +0000727// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000728LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000729 if (AddrLabelExpr *E =
730 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
731 return E->getLabel();
732 return 0;
733}
Ted Kremenek82977772007-08-24 21:09:09 +0000734
Ted Kremenek82977772007-08-24 21:09:09 +0000735// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000736const Expr* ReturnStmt::getRetValue() const {
737 return cast_or_null<Expr>(RetExpr);
738}
739Expr* ReturnStmt::getRetValue() {
740 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000741}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000742
743SEHTryStmt::SEHTryStmt(bool IsCXXTry,
744 SourceLocation TryLoc,
745 Stmt *TryBlock,
746 Stmt *Handler)
747 : Stmt(SEHTryStmtClass),
748 IsCXXTry(IsCXXTry),
749 TryLoc(TryLoc)
750{
751 Children[TRY] = TryBlock;
752 Children[HANDLER] = Handler;
753}
754
755SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
756 bool IsCXXTry,
757 SourceLocation TryLoc,
758 Stmt *TryBlock,
759 Stmt *Handler) {
760 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
761}
762
763SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
764 return dyn_cast<SEHExceptStmt>(getHandler());
765}
766
767SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
768 return dyn_cast<SEHFinallyStmt>(getHandler());
769}
770
771SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
772 Expr *FilterExpr,
773 Stmt *Block)
774 : Stmt(SEHExceptStmtClass),
775 Loc(Loc)
776{
777 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
778 Children[BLOCK] = Block;
779}
780
781SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
782 SourceLocation Loc,
783 Expr *FilterExpr,
784 Stmt *Block) {
785 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
786}
787
788SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
789 Stmt *Block)
790 : Stmt(SEHFinallyStmtClass),
791 Loc(Loc),
792 Block(Block)
793{}
794
795SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
796 SourceLocation Loc,
797 Stmt *Block) {
798 return new(C)SEHFinallyStmt(Loc,Block);
799}