blob: 39f23fba9ed38e51febc1a4f632d90c8fcdae8b6 [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
81static bool StatSwitch = false;
82
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000083bool Stmt::CollectingStats(bool Enable) {
84 if (Enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000085 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
John McCall63c00d72011-02-09 08:16:59 +000088namespace {
89 struct good {};
90 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +000091
92 // These silly little functions have to be static inline to suppress
93 // unused warnings, and they have to be defined to suppress other
94 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +000095 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +000096
97 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +000098 template <class T> good implements_children(children_t T::*) {
99 return good();
100 }
101 static inline bad implements_children(children_t Stmt::*) {
102 return bad();
103 }
John McCall63c00d72011-02-09 08:16:59 +0000104
105 typedef SourceRange getSourceRange_t() const;
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000106 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
107 return good();
108 }
109 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
110 return bad();
111 }
John McCall63c00d72011-02-09 08:16:59 +0000112
113#define ASSERT_IMPLEMENTS_children(type) \
114 (void) sizeof(is_good(implements_children(&type::children)))
115#define ASSERT_IMPLEMENTS_getSourceRange(type) \
116 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
117}
118
119/// Check whether the various Stmt classes implement their member
120/// functions.
121static inline void check_implementations() {
122#define ABSTRACT_STMT(type)
123#define STMT(type, base) \
124 ASSERT_IMPLEMENTS_children(type); \
125 ASSERT_IMPLEMENTS_getSourceRange(type);
126#include "clang/AST/StmtNodes.inc"
127}
128
129Stmt::child_range Stmt::children() {
130 switch (getStmtClass()) {
131 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
132#define ABSTRACT_STMT(type)
133#define STMT(type, base) \
134 case Stmt::type##Class: \
135 return static_cast<type*>(this)->children();
136#include "clang/AST/StmtNodes.inc"
137 }
138 llvm_unreachable("unknown statement kind!");
139 return child_range();
140}
141
142SourceRange Stmt::getSourceRange() const {
143 switch (getStmtClass()) {
144 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
145#define ABSTRACT_STMT(type)
146#define STMT(type, base) \
147 case Stmt::type##Class: \
148 return static_cast<const type*>(this)->getSourceRange();
149#include "clang/AST/StmtNodes.inc"
150 }
151 llvm_unreachable("unknown statement kind!");
152 return SourceRange();
153}
154
Douglas Gregor025452f2009-04-17 00:04:06 +0000155void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
156 if (this->Body)
157 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000158 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000159
160 Body = new (C) Stmt*[NumStmts];
161 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
162}
Reid Spencer5f016e22007-07-11 17:01:13 +0000163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000165 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000166}
167
Steve Naroff507f2d52007-08-31 23:49:30 +0000168// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000169SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000170 if (RetExpr)
171 return SourceRange(RetLoc, RetExpr->getLocEnd());
172 else
173 return SourceRange(RetLoc);
174}
175
Ted Kremenekd48ade62007-10-01 16:34:52 +0000176bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000177 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000178 default:
179 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000180
Ted Kremenekd48ade62007-10-01 16:34:52 +0000181 case CallExprClass:
182 case ConditionalOperatorClass:
183 case ChooseExprClass:
184 case StmtExprClass:
185 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000186 return true;
187
Ted Kremenekd48ade62007-10-01 16:34:52 +0000188 case Stmt::BinaryOperatorClass: {
189 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000190 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000191 return true;
192 else
193 return false;
194 }
195 }
196}
197
Chris Lattnerb3277932009-03-10 04:59:06 +0000198Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000199 return cast<Expr>(Exprs[i]);
200}
Chris Lattnerb3277932009-03-10 04:59:06 +0000201
202/// getOutputConstraint - Return the constraint string for the specified
203/// output operand. All output constraints are known to be non-empty (either
204/// '=' or '+').
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000205llvm::StringRef AsmStmt::getOutputConstraint(unsigned i) const {
206 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000207}
Chris Lattnerb3277932009-03-10 04:59:06 +0000208
Chris Lattner85759272009-03-11 00:23:13 +0000209/// getNumPlusOperands - Return the number of output operands that have a "+"
210/// constraint.
211unsigned AsmStmt::getNumPlusOperands() const {
212 unsigned Res = 0;
213 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
214 if (isOutputPlusConstraint(i))
215 ++Res;
216 return Res;
217}
218
Chris Lattnerb3277932009-03-10 04:59:06 +0000219Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000220 return cast<Expr>(Exprs[i + NumOutputs]);
221}
Chris Lattner935f0f02011-02-21 22:09:29 +0000222void AsmStmt::setInputExpr(unsigned i, Expr *E) {
223 Exprs[i + NumOutputs] = E;
224}
225
Chris Lattnerb3277932009-03-10 04:59:06 +0000226
227/// getInputConstraint - Return the specified input constraint. Unlike output
228/// constraints, these can be empty.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000229llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
230 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000231}
232
Chris Lattner10ca96a2009-03-10 06:33:24 +0000233
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000234void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000235 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000236 StringLiteral **Constraints,
237 Stmt **Exprs,
238 unsigned NumOutputs,
Sean Huntc3021132010-05-05 15:23:54 +0000239 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000240 StringLiteral **Clobbers,
241 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000242 this->NumOutputs = NumOutputs;
243 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000244 this->NumClobbers = NumClobbers;
245
246 unsigned NumExprs = NumOutputs + NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000247
Anders Carlsson966146e2010-01-30 23:19:41 +0000248 C.Deallocate(this->Names);
249 this->Names = new (C) IdentifierInfo*[NumExprs];
250 std::copy(Names, Names + NumExprs, this->Names);
Sean Huntc3021132010-05-05 15:23:54 +0000251
Anders Carlsson966146e2010-01-30 23:19:41 +0000252 C.Deallocate(this->Exprs);
253 this->Exprs = new (C) Stmt*[NumExprs];
254 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Sean Huntc3021132010-05-05 15:23:54 +0000255
Anders Carlsson966146e2010-01-30 23:19:41 +0000256 C.Deallocate(this->Constraints);
257 this->Constraints = new (C) StringLiteral*[NumExprs];
258 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Sean Huntc3021132010-05-05 15:23:54 +0000259
Anders Carlsson966146e2010-01-30 23:19:41 +0000260 C.Deallocate(this->Clobbers);
261 this->Clobbers = new (C) StringLiteral*[NumClobbers];
262 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000263}
264
Chris Lattner10ca96a2009-03-10 06:33:24 +0000265/// getNamedOperand - Given a symbolic operand reference like %[foo],
266/// translate this into a numeric value needed to reference the same operand.
267/// This returns -1 if the operand name is invalid.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000268int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000269 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattner10ca96a2009-03-10 06:33:24 +0000271 // Check if this is an output operand.
272 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
273 if (getOutputName(i) == SymbolicName)
274 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000275 }
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Chris Lattner10ca96a2009-03-10 06:33:24 +0000277 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
278 if (getInputName(i) == SymbolicName)
279 return getNumOutputs() + NumPlusOperands + i;
280
281 // Not found.
282 return -1;
283}
284
Chris Lattner458cd9c2009-03-10 23:21:44 +0000285/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
286/// it into pieces. If the asm string is erroneous, emit errors and return
287/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000288unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
289 ASTContext &C, unsigned &DiagOffs) const {
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000290 llvm::StringRef Str = getAsmString()->getString();
291 const char *StrStart = Str.begin();
292 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000293 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Chris Lattner458cd9c2009-03-10 23:21:44 +0000295 // "Simple" inline asms have no constraints or operands, just convert the asm
296 // string to escape $'s.
297 if (isSimple()) {
298 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000299 for (; CurPtr != StrEnd; ++CurPtr) {
300 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000301 case '$':
302 Result += "$$";
303 break;
304 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000305 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000306 break;
307 }
308 }
309 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000310 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000311 }
312
313 // CurStringPiece - The current string that we are building up as we scan the
314 // asm string.
315 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Chris Lattner9bffb072010-04-23 16:29:58 +0000317 bool HasVariants = !C.Target.hasNoAsmVariants();
Sean Huntc3021132010-05-05 15:23:54 +0000318
Chris Lattner458cd9c2009-03-10 23:21:44 +0000319 while (1) {
320 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000321 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000322 if (!CurStringPiece.empty())
323 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000324 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Chris Lattner3182db12009-03-10 23:51:40 +0000327 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000328 switch (CurChar) {
329 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000330 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
331 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
332 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000333 case '%':
334 break;
335 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000336 CurStringPiece += CurChar;
337 continue;
338 }
Sean Huntc3021132010-05-05 15:23:54 +0000339
Chris Lattner458cd9c2009-03-10 23:21:44 +0000340 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000341 if (CurPtr == StrEnd) {
342 // % at end of string is invalid (no escape).
343 DiagOffs = CurPtr-StrStart-1;
344 return diag::err_asm_invalid_escape;
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Chris Lattner3182db12009-03-10 23:51:40 +0000347 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000348 if (EscapedChar == '%') { // %% -> %
349 // Escaped percentage sign.
350 CurStringPiece += '%';
351 continue;
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattner458cd9c2009-03-10 23:21:44 +0000354 if (EscapedChar == '=') { // %= -> Generate an unique ID.
355 CurStringPiece += "${:uid}";
356 continue;
357 }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattner458cd9c2009-03-10 23:21:44 +0000359 // Otherwise, we have an operand. If we have accumulated a string so far,
360 // add it to the Pieces list.
361 if (!CurStringPiece.empty()) {
362 Pieces.push_back(AsmStringPiece(CurStringPiece));
363 CurStringPiece.clear();
364 }
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Chris Lattner458cd9c2009-03-10 23:21:44 +0000366 // Handle %x4 and %x[foo] by capturing x as the modifier character.
367 char Modifier = '\0';
368 if (isalpha(EscapedChar)) {
369 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000370 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000371 }
Mike Stump1eb44332009-09-09 15:08:12 +0000372
Chris Lattner458cd9c2009-03-10 23:21:44 +0000373 if (isdigit(EscapedChar)) {
374 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000375 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattnercafc2222009-03-11 22:52:17 +0000377 --CurPtr;
378 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000379 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Chris Lattner85759272009-03-11 00:23:13 +0000381 unsigned NumOperands =
382 getNumOutputs() + getNumPlusOperands() + getNumInputs();
383 if (N >= NumOperands) {
384 DiagOffs = CurPtr-StrStart-1;
385 return diag::err_asm_invalid_operand_number;
386 }
387
Chris Lattner458cd9c2009-03-10 23:21:44 +0000388 Pieces.push_back(AsmStringPiece(N, Modifier));
389 continue;
390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattner458cd9c2009-03-10 23:21:44 +0000392 // Handle %[foo], a symbolic operand reference.
393 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000394 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000396 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000397 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000398 if (NameEnd == 0)
399 return diag::err_asm_unterminated_symbolic_operand_name;
400 if (NameEnd == CurPtr)
401 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Anders Carlsson95c9ce92010-01-30 20:48:08 +0000403 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner458cd9c2009-03-10 23:21:44 +0000405 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000406 if (N == -1) {
407 // Verify that an operand with that name exists.
408 DiagOffs = CurPtr-StrStart;
409 return diag::err_asm_unknown_symbolic_operand_name;
410 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000411 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000413 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000414 continue;
415 }
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner2ff0f422009-03-10 23:57:07 +0000417 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000418 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000419 }
420}
421
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000422QualType CXXCatchStmt::getCaughtType() const {
423 if (ExceptionDecl)
424 return ExceptionDecl->getType();
425 return QualType();
426}
427
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000428//===----------------------------------------------------------------------===//
429// Constructors
430//===----------------------------------------------------------------------===//
431
Sean Huntc3021132010-05-05 15:23:54 +0000432AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
433 bool isvolatile, bool msasm,
Anders Carlsson966146e2010-01-30 23:19:41 +0000434 unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000435 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000436 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
437 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000438 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000439 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson966146e2010-01-30 23:19:41 +0000440 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000441
Anders Carlsson966146e2010-01-30 23:19:41 +0000442 unsigned NumExprs = NumOutputs +NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000443
Anders Carlsson966146e2010-01-30 23:19:41 +0000444 Names = new (C) IdentifierInfo*[NumExprs];
445 std::copy(names, names + NumExprs, Names);
446
447 Exprs = new (C) Stmt*[NumExprs];
448 std::copy(exprs, exprs + NumExprs, Exprs);
449
450 Constraints = new (C) StringLiteral*[NumExprs];
451 std::copy(constraints, constraints + NumExprs, Constraints);
452
453 Clobbers = new (C) StringLiteral*[NumClobbers];
454 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000455}
456
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000457ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
458 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000459 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000460: Stmt(ObjCForCollectionStmtClass) {
461 SubExprs[ELEM] = Elem;
462 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
463 SubExprs[BODY] = Body;
464 ForLoc = FCL;
465 RParenLoc = RPL;
466}
467
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000468ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
469 Stmt **CatchStmts, unsigned NumCatchStmts,
470 Stmt *atFinallyStmt)
471 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
472 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
473{
474 Stmt **Stmts = getStmts();
475 Stmts[0] = atTryStmt;
476 for (unsigned I = 0; I != NumCatchStmts; ++I)
477 Stmts[I + 1] = CatchStmts[I];
Sean Huntc3021132010-05-05 15:23:54 +0000478
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000479 if (HasFinally)
480 Stmts[NumCatchStmts + 1] = atFinallyStmt;
481}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000482
Sean Huntc3021132010-05-05 15:23:54 +0000483ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
484 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000485 Stmt *atTryStmt,
Sean Huntc3021132010-05-05 15:23:54 +0000486 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000487 unsigned NumCatchStmts,
488 Stmt *atFinallyStmt) {
Sean Huntc3021132010-05-05 15:23:54 +0000489 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000490 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000491 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000492 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
493 atFinallyStmt);
494}
Ted Kremenekff981022008-02-01 21:28:59 +0000495
Sean Huntc3021132010-05-05 15:23:54 +0000496ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000497 unsigned NumCatchStmts,
498 bool HasFinally) {
Sean Huntc3021132010-05-05 15:23:54 +0000499 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000500 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000501 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Sean Huntc3021132010-05-05 15:23:54 +0000502 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000503}
Nico Weber608b17f2008-08-05 23:15:29 +0000504
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000505SourceRange ObjCAtTryStmt::getSourceRange() const {
506 SourceLocation EndLoc;
507 if (HasFinally)
508 EndLoc = getFinallyStmt()->getLocEnd();
509 else if (NumCatchStmts)
510 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
511 else
512 EndLoc = getTryBody()->getLocEnd();
Sean Huntc3021132010-05-05 15:23:54 +0000513
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000514 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000515}
516
Sam Weiniga1a396d2010-02-03 03:56:39 +0000517CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000518 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000519 unsigned numHandlers) {
520 std::size_t Size = sizeof(CXXTryStmt);
521 Size += ((numHandlers + 1) * sizeof(Stmt));
522
Chris Lattner32488542010-10-30 05:14:06 +0000523 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000524 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
525}
526
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000527CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
528 unsigned numHandlers) {
529 std::size_t Size = sizeof(CXXTryStmt);
530 Size += ((numHandlers + 1) * sizeof(Stmt));
531
Chris Lattner32488542010-10-30 05:14:06 +0000532 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000533 return new (Mem) CXXTryStmt(Empty, numHandlers);
534}
535
Sam Weiniga1a396d2010-02-03 03:56:39 +0000536CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000537 Stmt **handlers, unsigned numHandlers)
538 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000539 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000540 Stmts[0] = tryBlock;
541 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
542}
543
Richard Smithad762fc2011-04-14 22:09:26 +0000544CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
545 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
546 Stmt *Body, SourceLocation FL,
547 SourceLocation CL, SourceLocation RPL)
548 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
549 SubExprs[RANGE] = Range;
550 SubExprs[BEGINEND] = BeginEndStmt;
551 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
552 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
553 SubExprs[LOOPVAR] = LoopVar;
554 SubExprs[BODY] = Body;
555}
556
557Expr *CXXForRangeStmt::getRangeInit() {
558 DeclStmt *RangeStmt = getRangeStmt();
559 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
560 assert(RangeDecl &&& "for-range should have a single var decl");
561 return RangeDecl->getInit();
562}
563
564const Expr *CXXForRangeStmt::getRangeInit() const {
565 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
566}
567
568VarDecl *CXXForRangeStmt::getLoopVariable() {
569 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
570 assert(LV && "No loop variable in CXXForRangeStmt");
571 return cast<VarDecl>(LV);
572}
573
574const VarDecl *CXXForRangeStmt::getLoopVariable() const {
575 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
576}
577
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000578IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000579 Stmt *then, SourceLocation EL, Stmt *elsev)
580 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000581{
582 setConditionVariable(C, var);
583 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
584 SubExprs[THEN] = then;
585 SubExprs[ELSE] = elsev;
586}
587
588VarDecl *IfStmt::getConditionVariable() const {
589 if (!SubExprs[VAR])
590 return 0;
591
592 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
593 return cast<VarDecl>(DS->getSingleDecl());
594}
595
596void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
597 if (!V) {
598 SubExprs[VAR] = 0;
599 return;
600 }
601
602 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
603 V->getSourceRange().getBegin(),
604 V->getSourceRange().getEnd());
605}
606
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000607ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
608 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
609 SourceLocation RP)
610 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
611{
612 SubExprs[INIT] = Init;
613 setConditionVariable(C, condVar);
614 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
615 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
616 SubExprs[BODY] = Body;
617}
618
619VarDecl *ForStmt::getConditionVariable() const {
620 if (!SubExprs[CONDVAR])
621 return 0;
622
623 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
624 return cast<VarDecl>(DS->getSingleDecl());
625}
626
627void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
628 if (!V) {
629 SubExprs[CONDVAR] = 0;
630 return;
631 }
632
633 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V),
634 V->getSourceRange().getBegin(),
635 V->getSourceRange().getEnd());
636}
637
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000638SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
Ted Kremenek780d8852010-09-09 00:06:01 +0000639 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000640{
641 setConditionVariable(C, Var);
642 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
643 SubExprs[BODY] = NULL;
644}
645
646VarDecl *SwitchStmt::getConditionVariable() const {
647 if (!SubExprs[VAR])
648 return 0;
649
650 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
651 return cast<VarDecl>(DS->getSingleDecl());
652}
653
654void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
655 if (!V) {
656 SubExprs[VAR] = 0;
657 return;
658 }
659
660 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
661 V->getSourceRange().getBegin(),
662 V->getSourceRange().getEnd());
663}
664
John McCall63c00d72011-02-09 08:16:59 +0000665Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000666 if (isa<CaseStmt>(this))
667 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000668 return cast<DefaultStmt>(this)->getSubStmt();
669}
670
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000671WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
672 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000673 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000674 setConditionVariable(C, Var);
675 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
676 SubExprs[BODY] = body;
677 WhileLoc = WL;
678}
679
680VarDecl *WhileStmt::getConditionVariable() const {
681 if (!SubExprs[VAR])
682 return 0;
683
684 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
685 return cast<VarDecl>(DS->getSingleDecl());
686}
687
688void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
689 if (!V) {
690 SubExprs[VAR] = 0;
691 return;
692 }
693
694 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
695 V->getSourceRange().getBegin(),
696 V->getSourceRange().getEnd());
697}
698
Ted Kremenek82977772007-08-24 21:09:09 +0000699// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000700LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000701 if (AddrLabelExpr *E =
702 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
703 return E->getLabel();
704 return 0;
705}
Ted Kremenek82977772007-08-24 21:09:09 +0000706
Ted Kremenek82977772007-08-24 21:09:09 +0000707// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000708const Expr* ReturnStmt::getRetValue() const {
709 return cast_or_null<Expr>(RetExpr);
710}
711Expr* ReturnStmt::getRetValue() {
712 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000713}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000714
715SEHTryStmt::SEHTryStmt(bool IsCXXTry,
716 SourceLocation TryLoc,
717 Stmt *TryBlock,
718 Stmt *Handler)
719 : Stmt(SEHTryStmtClass),
720 IsCXXTry(IsCXXTry),
721 TryLoc(TryLoc)
722{
723 Children[TRY] = TryBlock;
724 Children[HANDLER] = Handler;
725}
726
727SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
728 bool IsCXXTry,
729 SourceLocation TryLoc,
730 Stmt *TryBlock,
731 Stmt *Handler) {
732 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
733}
734
735SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
736 return dyn_cast<SEHExceptStmt>(getHandler());
737}
738
739SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
740 return dyn_cast<SEHFinallyStmt>(getHandler());
741}
742
743SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
744 Expr *FilterExpr,
745 Stmt *Block)
746 : Stmt(SEHExceptStmtClass),
747 Loc(Loc)
748{
749 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
750 Children[BLOCK] = Block;
751}
752
753SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
754 SourceLocation Loc,
755 Expr *FilterExpr,
756 Stmt *Block) {
757 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
758}
759
760SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
761 Stmt *Block)
762 : Stmt(SEHFinallyStmtClass),
763 Loc(Loc),
764 Block(Block)
765{}
766
767SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
768 SourceLocation Loc,
769 Stmt *Block) {
770 return new(C)SEHFinallyStmt(Loc,Block);
771}