blob: e293f324aba8f275dc29ff73b2d96d60cf6d3eb5 [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)) {
Benjamin Kramerbc57f3c2011-07-05 11:13:37 +0000369 if (CurPtr == StrEnd) { // Premature end.
370 DiagOffs = CurPtr-StrStart-1;
371 return diag::err_asm_invalid_escape;
372 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000373 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000374 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
Chris Lattner458cd9c2009-03-10 23:21:44 +0000377 if (isdigit(EscapedChar)) {
378 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000379 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Chris Lattnercafc2222009-03-11 22:52:17 +0000381 --CurPtr;
382 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000383 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Chris Lattner85759272009-03-11 00:23:13 +0000385 unsigned NumOperands =
386 getNumOutputs() + getNumPlusOperands() + getNumInputs();
387 if (N >= NumOperands) {
388 DiagOffs = CurPtr-StrStart-1;
389 return diag::err_asm_invalid_operand_number;
390 }
391
Chris Lattner458cd9c2009-03-10 23:21:44 +0000392 Pieces.push_back(AsmStringPiece(N, Modifier));
393 continue;
394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattner458cd9c2009-03-10 23:21:44 +0000396 // Handle %[foo], a symbolic operand reference.
397 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000398 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000400 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000401 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000402 if (NameEnd == 0)
403 return diag::err_asm_unterminated_symbolic_operand_name;
404 if (NameEnd == CurPtr)
405 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Anders Carlsson95c9ce92010-01-30 20:48:08 +0000407 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattner458cd9c2009-03-10 23:21:44 +0000409 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000410 if (N == -1) {
411 // Verify that an operand with that name exists.
412 DiagOffs = CurPtr-StrStart;
413 return diag::err_asm_unknown_symbolic_operand_name;
414 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000415 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000417 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000418 continue;
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Chris Lattner2ff0f422009-03-10 23:57:07 +0000421 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000422 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000423 }
424}
425
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000426QualType CXXCatchStmt::getCaughtType() const {
427 if (ExceptionDecl)
428 return ExceptionDecl->getType();
429 return QualType();
430}
431
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000432//===----------------------------------------------------------------------===//
433// Constructors
434//===----------------------------------------------------------------------===//
435
Sean Huntc3021132010-05-05 15:23:54 +0000436AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
437 bool isvolatile, bool msasm,
Anders Carlsson966146e2010-01-30 23:19:41 +0000438 unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000439 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000440 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
441 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000442 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000443 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson966146e2010-01-30 23:19:41 +0000444 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000445
Anders Carlsson966146e2010-01-30 23:19:41 +0000446 unsigned NumExprs = NumOutputs +NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000447
Anders Carlsson966146e2010-01-30 23:19:41 +0000448 Names = new (C) IdentifierInfo*[NumExprs];
449 std::copy(names, names + NumExprs, Names);
450
451 Exprs = new (C) Stmt*[NumExprs];
452 std::copy(exprs, exprs + NumExprs, Exprs);
453
454 Constraints = new (C) StringLiteral*[NumExprs];
455 std::copy(constraints, constraints + NumExprs, Constraints);
456
457 Clobbers = new (C) StringLiteral*[NumClobbers];
458 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000459}
460
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000461ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
462 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000463 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000464: Stmt(ObjCForCollectionStmtClass) {
465 SubExprs[ELEM] = Elem;
466 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
467 SubExprs[BODY] = Body;
468 ForLoc = FCL;
469 RParenLoc = RPL;
470}
471
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000472ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
473 Stmt **CatchStmts, unsigned NumCatchStmts,
474 Stmt *atFinallyStmt)
475 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
476 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
477{
478 Stmt **Stmts = getStmts();
479 Stmts[0] = atTryStmt;
480 for (unsigned I = 0; I != NumCatchStmts; ++I)
481 Stmts[I + 1] = CatchStmts[I];
Sean Huntc3021132010-05-05 15:23:54 +0000482
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000483 if (HasFinally)
484 Stmts[NumCatchStmts + 1] = atFinallyStmt;
485}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000486
Sean Huntc3021132010-05-05 15:23:54 +0000487ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
488 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000489 Stmt *atTryStmt,
Sean Huntc3021132010-05-05 15:23:54 +0000490 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000491 unsigned NumCatchStmts,
492 Stmt *atFinallyStmt) {
Sean Huntc3021132010-05-05 15:23:54 +0000493 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000494 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000495 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000496 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
497 atFinallyStmt);
498}
Ted Kremenekff981022008-02-01 21:28:59 +0000499
Sean Huntc3021132010-05-05 15:23:54 +0000500ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000501 unsigned NumCatchStmts,
502 bool HasFinally) {
Sean Huntc3021132010-05-05 15:23:54 +0000503 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000504 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000505 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Sean Huntc3021132010-05-05 15:23:54 +0000506 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000507}
Nico Weber608b17f2008-08-05 23:15:29 +0000508
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000509SourceRange ObjCAtTryStmt::getSourceRange() const {
510 SourceLocation EndLoc;
511 if (HasFinally)
512 EndLoc = getFinallyStmt()->getLocEnd();
513 else if (NumCatchStmts)
514 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
515 else
516 EndLoc = getTryBody()->getLocEnd();
Sean Huntc3021132010-05-05 15:23:54 +0000517
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000518 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000519}
520
Sam Weiniga1a396d2010-02-03 03:56:39 +0000521CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000522 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000523 unsigned numHandlers) {
524 std::size_t Size = sizeof(CXXTryStmt);
525 Size += ((numHandlers + 1) * sizeof(Stmt));
526
Chris Lattner32488542010-10-30 05:14:06 +0000527 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000528 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
529}
530
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000531CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
532 unsigned numHandlers) {
533 std::size_t Size = sizeof(CXXTryStmt);
534 Size += ((numHandlers + 1) * sizeof(Stmt));
535
Chris Lattner32488542010-10-30 05:14:06 +0000536 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000537 return new (Mem) CXXTryStmt(Empty, numHandlers);
538}
539
Sam Weiniga1a396d2010-02-03 03:56:39 +0000540CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000541 Stmt **handlers, unsigned numHandlers)
542 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000543 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000544 Stmts[0] = tryBlock;
545 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
546}
547
Richard Smithad762fc2011-04-14 22:09:26 +0000548CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
549 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
550 Stmt *Body, SourceLocation FL,
551 SourceLocation CL, SourceLocation RPL)
552 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
553 SubExprs[RANGE] = Range;
554 SubExprs[BEGINEND] = BeginEndStmt;
555 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
556 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
557 SubExprs[LOOPVAR] = LoopVar;
558 SubExprs[BODY] = Body;
559}
560
561Expr *CXXForRangeStmt::getRangeInit() {
562 DeclStmt *RangeStmt = getRangeStmt();
563 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
564 assert(RangeDecl &&& "for-range should have a single var decl");
565 return RangeDecl->getInit();
566}
567
568const Expr *CXXForRangeStmt::getRangeInit() const {
569 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
570}
571
572VarDecl *CXXForRangeStmt::getLoopVariable() {
573 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
574 assert(LV && "No loop variable in CXXForRangeStmt");
575 return cast<VarDecl>(LV);
576}
577
578const VarDecl *CXXForRangeStmt::getLoopVariable() const {
579 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
580}
581
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000582IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000583 Stmt *then, SourceLocation EL, Stmt *elsev)
584 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000585{
586 setConditionVariable(C, var);
587 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
588 SubExprs[THEN] = then;
589 SubExprs[ELSE] = elsev;
590}
591
592VarDecl *IfStmt::getConditionVariable() const {
593 if (!SubExprs[VAR])
594 return 0;
595
596 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
597 return cast<VarDecl>(DS->getSingleDecl());
598}
599
600void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
601 if (!V) {
602 SubExprs[VAR] = 0;
603 return;
604 }
605
606 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
607 V->getSourceRange().getBegin(),
608 V->getSourceRange().getEnd());
609}
610
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000611ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
612 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
613 SourceLocation RP)
614 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
615{
616 SubExprs[INIT] = Init;
617 setConditionVariable(C, condVar);
618 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
619 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
620 SubExprs[BODY] = Body;
621}
622
623VarDecl *ForStmt::getConditionVariable() const {
624 if (!SubExprs[CONDVAR])
625 return 0;
626
627 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
628 return cast<VarDecl>(DS->getSingleDecl());
629}
630
631void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
632 if (!V) {
633 SubExprs[CONDVAR] = 0;
634 return;
635 }
636
637 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V),
638 V->getSourceRange().getBegin(),
639 V->getSourceRange().getEnd());
640}
641
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000642SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
Ted Kremenek780d8852010-09-09 00:06:01 +0000643 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000644{
645 setConditionVariable(C, Var);
646 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
647 SubExprs[BODY] = NULL;
648}
649
650VarDecl *SwitchStmt::getConditionVariable() const {
651 if (!SubExprs[VAR])
652 return 0;
653
654 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
655 return cast<VarDecl>(DS->getSingleDecl());
656}
657
658void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
659 if (!V) {
660 SubExprs[VAR] = 0;
661 return;
662 }
663
664 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
665 V->getSourceRange().getBegin(),
666 V->getSourceRange().getEnd());
667}
668
John McCall63c00d72011-02-09 08:16:59 +0000669Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000670 if (isa<CaseStmt>(this))
671 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000672 return cast<DefaultStmt>(this)->getSubStmt();
673}
674
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000675WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
676 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000677 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000678 setConditionVariable(C, Var);
679 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
680 SubExprs[BODY] = body;
681 WhileLoc = WL;
682}
683
684VarDecl *WhileStmt::getConditionVariable() const {
685 if (!SubExprs[VAR])
686 return 0;
687
688 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
689 return cast<VarDecl>(DS->getSingleDecl());
690}
691
692void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
693 if (!V) {
694 SubExprs[VAR] = 0;
695 return;
696 }
697
698 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
699 V->getSourceRange().getBegin(),
700 V->getSourceRange().getEnd());
701}
702
Ted Kremenek82977772007-08-24 21:09:09 +0000703// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000704LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000705 if (AddrLabelExpr *E =
706 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
707 return E->getLabel();
708 return 0;
709}
Ted Kremenek82977772007-08-24 21:09:09 +0000710
Ted Kremenek82977772007-08-24 21:09:09 +0000711// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000712const Expr* ReturnStmt::getRetValue() const {
713 return cast_or_null<Expr>(RetExpr);
714}
715Expr* ReturnStmt::getRetValue() {
716 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000717}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000718
719SEHTryStmt::SEHTryStmt(bool IsCXXTry,
720 SourceLocation TryLoc,
721 Stmt *TryBlock,
722 Stmt *Handler)
723 : Stmt(SEHTryStmtClass),
724 IsCXXTry(IsCXXTry),
725 TryLoc(TryLoc)
726{
727 Children[TRY] = TryBlock;
728 Children[HANDLER] = Handler;
729}
730
731SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
732 bool IsCXXTry,
733 SourceLocation TryLoc,
734 Stmt *TryBlock,
735 Stmt *Handler) {
736 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
737}
738
739SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
740 return dyn_cast<SEHExceptStmt>(getHandler());
741}
742
743SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
744 return dyn_cast<SEHFinallyStmt>(getHandler());
745}
746
747SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
748 Expr *FilterExpr,
749 Stmt *Block)
750 : Stmt(SEHExceptStmtClass),
751 Loc(Loc)
752{
753 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
754 Children[BLOCK] = Block;
755}
756
757SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
758 SourceLocation Loc,
759 Expr *FilterExpr,
760 Stmt *Block) {
761 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
762}
763
764SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
765 Stmt *Block)
766 : Stmt(SEHFinallyStmtClass),
767 Loc(Loc),
768 Block(Block)
769{}
770
771SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
772 SourceLocation Loc,
773 Stmt *Block) {
774 return new(C)SEHFinallyStmt(Loc,Block);
775}