blob: 380ad94ca224804702577e6a900c4d5ab6109321 [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"
Torok Edwinf42e4a62009-08-24 13:25:12 +000023#include <cstdio>
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;
57 fprintf(stderr, "*** 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 }
62 fprintf(stderr, " %d stmts/exprs total.\n", sum);
63 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;
Nico Weber608b17f2008-08-05 23:15:29 +000067 fprintf(stderr, " %d %s, %d each (%d bytes)\n",
Chris Lattner63381352007-08-25 01:42:24 +000068 StmtClassInfo[i].Counter, StmtClassInfo[i].Name,
69 StmtClassInfo[i].Size,
70 StmtClassInfo[i].Counter*StmtClassInfo[i].Size);
71 sum += StmtClassInfo[i].Counter*StmtClassInfo[i].Size;
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
73 fprintf(stderr, "Total bytes = %d\n", sum);
74}
75
76void Stmt::addStmtClass(StmtClass s) {
Chris Lattner63381352007-08-25 01:42:24 +000077 ++getStmtInfoTableEntry(s).Counter;
Reid Spencer5f016e22007-07-11 17:01:13 +000078}
79
80static bool StatSwitch = false;
81
Kovarththanan Rajaratnam2024f4d2009-11-29 14:54:35 +000082bool Stmt::CollectingStats(bool Enable) {
83 if (Enable) StatSwitch = true;
Chris Lattner63381352007-08-25 01:42:24 +000084 return StatSwitch;
Reid Spencer5f016e22007-07-11 17:01:13 +000085}
86
John McCall63c00d72011-02-09 08:16:59 +000087namespace {
88 struct good {};
89 struct bad {};
John McCallf8c7fdb2011-02-09 08:31:17 +000090
91 // These silly little functions have to be static inline to suppress
92 // unused warnings, and they have to be defined to suppress other
93 // warnings.
Nick Lewycky086eb9f2011-02-09 08:42:57 +000094 static inline good is_good(good) { return good(); }
John McCall63c00d72011-02-09 08:16:59 +000095
96 typedef Stmt::child_range children_t();
Nick Lewycky086eb9f2011-02-09 08:42:57 +000097 template <class T> good implements_children(children_t T::*) {
98 return good();
99 }
100 static inline bad implements_children(children_t Stmt::*) {
101 return bad();
102 }
John McCall63c00d72011-02-09 08:16:59 +0000103
104 typedef SourceRange getSourceRange_t() const;
Nick Lewycky086eb9f2011-02-09 08:42:57 +0000105 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {
106 return good();
107 }
108 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {
109 return bad();
110 }
John McCall63c00d72011-02-09 08:16:59 +0000111
112#define ASSERT_IMPLEMENTS_children(type) \
113 (void) sizeof(is_good(implements_children(&type::children)))
114#define ASSERT_IMPLEMENTS_getSourceRange(type) \
115 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
116}
117
118/// Check whether the various Stmt classes implement their member
119/// functions.
120static inline void check_implementations() {
121#define ABSTRACT_STMT(type)
122#define STMT(type, base) \
123 ASSERT_IMPLEMENTS_children(type); \
124 ASSERT_IMPLEMENTS_getSourceRange(type);
125#include "clang/AST/StmtNodes.inc"
126}
127
128Stmt::child_range Stmt::children() {
129 switch (getStmtClass()) {
130 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
131#define ABSTRACT_STMT(type)
132#define STMT(type, base) \
133 case Stmt::type##Class: \
134 return static_cast<type*>(this)->children();
135#include "clang/AST/StmtNodes.inc"
136 }
137 llvm_unreachable("unknown statement kind!");
138 return child_range();
139}
140
141SourceRange Stmt::getSourceRange() const {
142 switch (getStmtClass()) {
143 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
144#define ABSTRACT_STMT(type)
145#define STMT(type, base) \
146 case Stmt::type##Class: \
147 return static_cast<const type*>(this)->getSourceRange();
148#include "clang/AST/StmtNodes.inc"
149 }
150 llvm_unreachable("unknown statement kind!");
151 return SourceRange();
152}
153
Douglas Gregor025452f2009-04-17 00:04:06 +0000154void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
155 if (this->Body)
156 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000157 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000158
159 Body = new (C) Stmt*[NumStmts];
160 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
161}
Reid Spencer5f016e22007-07-11 17:01:13 +0000162
Reid Spencer5f016e22007-07-11 17:01:13 +0000163const char *LabelStmt::getName() const {
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000164 return getDecl()->getIdentifier()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000165}
166
Steve Naroff507f2d52007-08-31 23:49:30 +0000167// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000168SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000169 if (RetExpr)
170 return SourceRange(RetLoc, RetExpr->getLocEnd());
171 else
172 return SourceRange(RetLoc);
173}
174
Ted Kremenekd48ade62007-10-01 16:34:52 +0000175bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000176 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000177 default:
178 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000179
Ted Kremenekd48ade62007-10-01 16:34:52 +0000180 case CallExprClass:
181 case ConditionalOperatorClass:
182 case ChooseExprClass:
183 case StmtExprClass:
184 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000185 return true;
186
Ted Kremenekd48ade62007-10-01 16:34:52 +0000187 case Stmt::BinaryOperatorClass: {
188 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000189 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000190 return true;
191 else
192 return false;
193 }
194 }
195}
196
Chris Lattnerb3277932009-03-10 04:59:06 +0000197Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000198 return cast<Expr>(Exprs[i]);
199}
Chris Lattnerb3277932009-03-10 04:59:06 +0000200
201/// getOutputConstraint - Return the constraint string for the specified
202/// output operand. All output constraints are known to be non-empty (either
203/// '=' or '+').
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000204llvm::StringRef AsmStmt::getOutputConstraint(unsigned i) const {
205 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000206}
Chris Lattnerb3277932009-03-10 04:59:06 +0000207
Chris Lattner85759272009-03-11 00:23:13 +0000208/// getNumPlusOperands - Return the number of output operands that have a "+"
209/// constraint.
210unsigned AsmStmt::getNumPlusOperands() const {
211 unsigned Res = 0;
212 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
213 if (isOutputPlusConstraint(i))
214 ++Res;
215 return Res;
216}
217
Chris Lattnerb3277932009-03-10 04:59:06 +0000218Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000219 return cast<Expr>(Exprs[i + NumOutputs]);
220}
Chris Lattner935f0f02011-02-21 22:09:29 +0000221void AsmStmt::setInputExpr(unsigned i, Expr *E) {
222 Exprs[i + NumOutputs] = E;
223}
224
Chris Lattnerb3277932009-03-10 04:59:06 +0000225
226/// getInputConstraint - Return the specified input constraint. Unlike output
227/// constraints, these can be empty.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000228llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
229 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000230}
231
Chris Lattner10ca96a2009-03-10 06:33:24 +0000232
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000233void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000234 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000235 StringLiteral **Constraints,
236 Stmt **Exprs,
237 unsigned NumOutputs,
Sean Huntc3021132010-05-05 15:23:54 +0000238 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000239 StringLiteral **Clobbers,
240 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000241 this->NumOutputs = NumOutputs;
242 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000243 this->NumClobbers = NumClobbers;
244
245 unsigned NumExprs = NumOutputs + NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000246
Anders Carlsson966146e2010-01-30 23:19:41 +0000247 C.Deallocate(this->Names);
248 this->Names = new (C) IdentifierInfo*[NumExprs];
249 std::copy(Names, Names + NumExprs, this->Names);
Sean Huntc3021132010-05-05 15:23:54 +0000250
Anders Carlsson966146e2010-01-30 23:19:41 +0000251 C.Deallocate(this->Exprs);
252 this->Exprs = new (C) Stmt*[NumExprs];
253 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Sean Huntc3021132010-05-05 15:23:54 +0000254
Anders Carlsson966146e2010-01-30 23:19:41 +0000255 C.Deallocate(this->Constraints);
256 this->Constraints = new (C) StringLiteral*[NumExprs];
257 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Sean Huntc3021132010-05-05 15:23:54 +0000258
Anders Carlsson966146e2010-01-30 23:19:41 +0000259 C.Deallocate(this->Clobbers);
260 this->Clobbers = new (C) StringLiteral*[NumClobbers];
261 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000262}
263
Chris Lattner10ca96a2009-03-10 06:33:24 +0000264/// getNamedOperand - Given a symbolic operand reference like %[foo],
265/// translate this into a numeric value needed to reference the same operand.
266/// This returns -1 if the operand name is invalid.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000267int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000268 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattner10ca96a2009-03-10 06:33:24 +0000270 // Check if this is an output operand.
271 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
272 if (getOutputName(i) == SymbolicName)
273 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattner10ca96a2009-03-10 06:33:24 +0000276 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
277 if (getInputName(i) == SymbolicName)
278 return getNumOutputs() + NumPlusOperands + i;
279
280 // Not found.
281 return -1;
282}
283
Chris Lattner458cd9c2009-03-10 23:21:44 +0000284/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
285/// it into pieces. If the asm string is erroneous, emit errors and return
286/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000287unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
288 ASTContext &C, unsigned &DiagOffs) const {
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000289 llvm::StringRef Str = getAsmString()->getString();
290 const char *StrStart = Str.begin();
291 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000292 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Chris Lattner458cd9c2009-03-10 23:21:44 +0000294 // "Simple" inline asms have no constraints or operands, just convert the asm
295 // string to escape $'s.
296 if (isSimple()) {
297 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000298 for (; CurPtr != StrEnd; ++CurPtr) {
299 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000300 case '$':
301 Result += "$$";
302 break;
303 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000304 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000305 break;
306 }
307 }
308 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000309 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000310 }
311
312 // CurStringPiece - The current string that we are building up as we scan the
313 // asm string.
314 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Chris Lattner9bffb072010-04-23 16:29:58 +0000316 bool HasVariants = !C.Target.hasNoAsmVariants();
Sean Huntc3021132010-05-05 15:23:54 +0000317
Chris Lattner458cd9c2009-03-10 23:21:44 +0000318 while (1) {
319 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000320 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000321 if (!CurStringPiece.empty())
322 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000323 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000324 }
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner3182db12009-03-10 23:51:40 +0000326 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000327 switch (CurChar) {
328 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000329 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
330 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
331 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000332 case '%':
333 break;
334 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000335 CurStringPiece += CurChar;
336 continue;
337 }
Sean Huntc3021132010-05-05 15:23:54 +0000338
Chris Lattner458cd9c2009-03-10 23:21:44 +0000339 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000340 if (CurPtr == StrEnd) {
341 // % at end of string is invalid (no escape).
342 DiagOffs = CurPtr-StrStart-1;
343 return diag::err_asm_invalid_escape;
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattner3182db12009-03-10 23:51:40 +0000346 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000347 if (EscapedChar == '%') { // %% -> %
348 // Escaped percentage sign.
349 CurStringPiece += '%';
350 continue;
351 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Chris Lattner458cd9c2009-03-10 23:21:44 +0000353 if (EscapedChar == '=') { // %= -> Generate an unique ID.
354 CurStringPiece += "${:uid}";
355 continue;
356 }
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattner458cd9c2009-03-10 23:21:44 +0000358 // Otherwise, we have an operand. If we have accumulated a string so far,
359 // add it to the Pieces list.
360 if (!CurStringPiece.empty()) {
361 Pieces.push_back(AsmStringPiece(CurStringPiece));
362 CurStringPiece.clear();
363 }
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattner458cd9c2009-03-10 23:21:44 +0000365 // Handle %x4 and %x[foo] by capturing x as the modifier character.
366 char Modifier = '\0';
367 if (isalpha(EscapedChar)) {
368 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000369 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner458cd9c2009-03-10 23:21:44 +0000372 if (isdigit(EscapedChar)) {
373 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000374 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattnercafc2222009-03-11 22:52:17 +0000376 --CurPtr;
377 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000378 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Chris Lattner85759272009-03-11 00:23:13 +0000380 unsigned NumOperands =
381 getNumOutputs() + getNumPlusOperands() + getNumInputs();
382 if (N >= NumOperands) {
383 DiagOffs = CurPtr-StrStart-1;
384 return diag::err_asm_invalid_operand_number;
385 }
386
Chris Lattner458cd9c2009-03-10 23:21:44 +0000387 Pieces.push_back(AsmStringPiece(N, Modifier));
388 continue;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner458cd9c2009-03-10 23:21:44 +0000391 // Handle %[foo], a symbolic operand reference.
392 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000393 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000395 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000396 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000397 if (NameEnd == 0)
398 return diag::err_asm_unterminated_symbolic_operand_name;
399 if (NameEnd == CurPtr)
400 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Anders Carlsson95c9ce92010-01-30 20:48:08 +0000402 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner458cd9c2009-03-10 23:21:44 +0000404 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000405 if (N == -1) {
406 // Verify that an operand with that name exists.
407 DiagOffs = CurPtr-StrStart;
408 return diag::err_asm_unknown_symbolic_operand_name;
409 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000410 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000412 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000413 continue;
414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattner2ff0f422009-03-10 23:57:07 +0000416 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000417 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000418 }
419}
420
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000421QualType CXXCatchStmt::getCaughtType() const {
422 if (ExceptionDecl)
423 return ExceptionDecl->getType();
424 return QualType();
425}
426
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000427//===----------------------------------------------------------------------===//
428// Constructors
429//===----------------------------------------------------------------------===//
430
Sean Huntc3021132010-05-05 15:23:54 +0000431AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
432 bool isvolatile, bool msasm,
Anders Carlsson966146e2010-01-30 23:19:41 +0000433 unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000434 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000435 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
436 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000437 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000438 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson966146e2010-01-30 23:19:41 +0000439 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000440
Anders Carlsson966146e2010-01-30 23:19:41 +0000441 unsigned NumExprs = NumOutputs +NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000442
Anders Carlsson966146e2010-01-30 23:19:41 +0000443 Names = new (C) IdentifierInfo*[NumExprs];
444 std::copy(names, names + NumExprs, Names);
445
446 Exprs = new (C) Stmt*[NumExprs];
447 std::copy(exprs, exprs + NumExprs, Exprs);
448
449 Constraints = new (C) StringLiteral*[NumExprs];
450 std::copy(constraints, constraints + NumExprs, Constraints);
451
452 Clobbers = new (C) StringLiteral*[NumClobbers];
453 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000454}
455
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000456ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
457 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000458 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000459: Stmt(ObjCForCollectionStmtClass) {
460 SubExprs[ELEM] = Elem;
461 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
462 SubExprs[BODY] = Body;
463 ForLoc = FCL;
464 RParenLoc = RPL;
465}
466
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000467ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
468 Stmt **CatchStmts, unsigned NumCatchStmts,
469 Stmt *atFinallyStmt)
470 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
471 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
472{
473 Stmt **Stmts = getStmts();
474 Stmts[0] = atTryStmt;
475 for (unsigned I = 0; I != NumCatchStmts; ++I)
476 Stmts[I + 1] = CatchStmts[I];
Sean Huntc3021132010-05-05 15:23:54 +0000477
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000478 if (HasFinally)
479 Stmts[NumCatchStmts + 1] = atFinallyStmt;
480}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000481
Sean Huntc3021132010-05-05 15:23:54 +0000482ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
483 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000484 Stmt *atTryStmt,
Sean Huntc3021132010-05-05 15:23:54 +0000485 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000486 unsigned NumCatchStmts,
487 Stmt *atFinallyStmt) {
Sean Huntc3021132010-05-05 15:23:54 +0000488 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000489 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000490 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000491 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
492 atFinallyStmt);
493}
Ted Kremenekff981022008-02-01 21:28:59 +0000494
Sean Huntc3021132010-05-05 15:23:54 +0000495ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000496 unsigned NumCatchStmts,
497 bool HasFinally) {
Sean Huntc3021132010-05-05 15:23:54 +0000498 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000499 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000500 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Sean Huntc3021132010-05-05 15:23:54 +0000501 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000502}
Nico Weber608b17f2008-08-05 23:15:29 +0000503
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000504SourceRange ObjCAtTryStmt::getSourceRange() const {
505 SourceLocation EndLoc;
506 if (HasFinally)
507 EndLoc = getFinallyStmt()->getLocEnd();
508 else if (NumCatchStmts)
509 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
510 else
511 EndLoc = getTryBody()->getLocEnd();
Sean Huntc3021132010-05-05 15:23:54 +0000512
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000513 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000514}
515
Sam Weiniga1a396d2010-02-03 03:56:39 +0000516CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000517 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000518 unsigned numHandlers) {
519 std::size_t Size = sizeof(CXXTryStmt);
520 Size += ((numHandlers + 1) * sizeof(Stmt));
521
Chris Lattner32488542010-10-30 05:14:06 +0000522 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000523 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
524}
525
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000526CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
527 unsigned numHandlers) {
528 std::size_t Size = sizeof(CXXTryStmt);
529 Size += ((numHandlers + 1) * sizeof(Stmt));
530
Chris Lattner32488542010-10-30 05:14:06 +0000531 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000532 return new (Mem) CXXTryStmt(Empty, numHandlers);
533}
534
Sam Weiniga1a396d2010-02-03 03:56:39 +0000535CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000536 Stmt **handlers, unsigned numHandlers)
537 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000538 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000539 Stmts[0] = tryBlock;
540 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
541}
542
Richard Smithad762fc2011-04-14 22:09:26 +0000543CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, DeclStmt *BeginEndStmt,
544 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
545 Stmt *Body, SourceLocation FL,
546 SourceLocation CL, SourceLocation RPL)
547 : Stmt(CXXForRangeStmtClass), ForLoc(FL), ColonLoc(CL), RParenLoc(RPL) {
548 SubExprs[RANGE] = Range;
549 SubExprs[BEGINEND] = BeginEndStmt;
550 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
551 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
552 SubExprs[LOOPVAR] = LoopVar;
553 SubExprs[BODY] = Body;
554}
555
556Expr *CXXForRangeStmt::getRangeInit() {
557 DeclStmt *RangeStmt = getRangeStmt();
558 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
559 assert(RangeDecl &&& "for-range should have a single var decl");
560 return RangeDecl->getInit();
561}
562
563const Expr *CXXForRangeStmt::getRangeInit() const {
564 return const_cast<CXXForRangeStmt*>(this)->getRangeInit();
565}
566
567VarDecl *CXXForRangeStmt::getLoopVariable() {
568 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
569 assert(LV && "No loop variable in CXXForRangeStmt");
570 return cast<VarDecl>(LV);
571}
572
573const VarDecl *CXXForRangeStmt::getLoopVariable() const {
574 return const_cast<CXXForRangeStmt*>(this)->getLoopVariable();
575}
576
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000577IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000578 Stmt *then, SourceLocation EL, Stmt *elsev)
579 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000580{
581 setConditionVariable(C, var);
582 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
583 SubExprs[THEN] = then;
584 SubExprs[ELSE] = elsev;
585}
586
587VarDecl *IfStmt::getConditionVariable() const {
588 if (!SubExprs[VAR])
589 return 0;
590
591 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
592 return cast<VarDecl>(DS->getSingleDecl());
593}
594
595void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
596 if (!V) {
597 SubExprs[VAR] = 0;
598 return;
599 }
600
601 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
602 V->getSourceRange().getBegin(),
603 V->getSourceRange().getEnd());
604}
605
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000606ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
607 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
608 SourceLocation RP)
609 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
610{
611 SubExprs[INIT] = Init;
612 setConditionVariable(C, condVar);
613 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
614 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
615 SubExprs[BODY] = Body;
616}
617
618VarDecl *ForStmt::getConditionVariable() const {
619 if (!SubExprs[CONDVAR])
620 return 0;
621
622 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
623 return cast<VarDecl>(DS->getSingleDecl());
624}
625
626void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
627 if (!V) {
628 SubExprs[CONDVAR] = 0;
629 return;
630 }
631
632 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V),
633 V->getSourceRange().getBegin(),
634 V->getSourceRange().getEnd());
635}
636
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000637SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
Ted Kremenek780d8852010-09-09 00:06:01 +0000638 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000639{
640 setConditionVariable(C, Var);
641 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
642 SubExprs[BODY] = NULL;
643}
644
645VarDecl *SwitchStmt::getConditionVariable() const {
646 if (!SubExprs[VAR])
647 return 0;
648
649 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
650 return cast<VarDecl>(DS->getSingleDecl());
651}
652
653void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
654 if (!V) {
655 SubExprs[VAR] = 0;
656 return;
657 }
658
659 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
660 V->getSourceRange().getBegin(),
661 V->getSourceRange().getEnd());
662}
663
John McCall63c00d72011-02-09 08:16:59 +0000664Stmt *SwitchCase::getSubStmt() {
Chris Lattnerc4002c72011-02-28 00:18:06 +0000665 if (isa<CaseStmt>(this))
666 return cast<CaseStmt>(this)->getSubStmt();
John McCall63c00d72011-02-09 08:16:59 +0000667 return cast<DefaultStmt>(this)->getSubStmt();
668}
669
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000670WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
671 SourceLocation WL)
Chris Lattnerc4002c72011-02-28 00:18:06 +0000672 : Stmt(WhileStmtClass) {
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000673 setConditionVariable(C, Var);
674 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
675 SubExprs[BODY] = body;
676 WhileLoc = WL;
677}
678
679VarDecl *WhileStmt::getConditionVariable() const {
680 if (!SubExprs[VAR])
681 return 0;
682
683 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
684 return cast<VarDecl>(DS->getSingleDecl());
685}
686
687void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
688 if (!V) {
689 SubExprs[VAR] = 0;
690 return;
691 }
692
693 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
694 V->getSourceRange().getBegin(),
695 V->getSourceRange().getEnd());
696}
697
Ted Kremenek82977772007-08-24 21:09:09 +0000698// IndirectGotoStmt
Chris Lattnerad8dcf42011-02-17 07:39:24 +0000699LabelDecl *IndirectGotoStmt::getConstantTarget() {
John McCall95c225d2010-10-28 08:53:48 +0000700 if (AddrLabelExpr *E =
701 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
702 return E->getLabel();
703 return 0;
704}
Ted Kremenek82977772007-08-24 21:09:09 +0000705
Ted Kremenek82977772007-08-24 21:09:09 +0000706// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000707const Expr* ReturnStmt::getRetValue() const {
708 return cast_or_null<Expr>(RetExpr);
709}
710Expr* ReturnStmt::getRetValue() {
711 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000712}
John Wiegley28bbe4b2011-04-28 01:08:34 +0000713
714SEHTryStmt::SEHTryStmt(bool IsCXXTry,
715 SourceLocation TryLoc,
716 Stmt *TryBlock,
717 Stmt *Handler)
718 : Stmt(SEHTryStmtClass),
719 IsCXXTry(IsCXXTry),
720 TryLoc(TryLoc)
721{
722 Children[TRY] = TryBlock;
723 Children[HANDLER] = Handler;
724}
725
726SEHTryStmt* SEHTryStmt::Create(ASTContext &C,
727 bool IsCXXTry,
728 SourceLocation TryLoc,
729 Stmt *TryBlock,
730 Stmt *Handler) {
731 return new(C) SEHTryStmt(IsCXXTry,TryLoc,TryBlock,Handler);
732}
733
734SEHExceptStmt* SEHTryStmt::getExceptHandler() const {
735 return dyn_cast<SEHExceptStmt>(getHandler());
736}
737
738SEHFinallyStmt* SEHTryStmt::getFinallyHandler() const {
739 return dyn_cast<SEHFinallyStmt>(getHandler());
740}
741
742SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
743 Expr *FilterExpr,
744 Stmt *Block)
745 : Stmt(SEHExceptStmtClass),
746 Loc(Loc)
747{
748 Children[FILTER_EXPR] = reinterpret_cast<Stmt*>(FilterExpr);
749 Children[BLOCK] = Block;
750}
751
752SEHExceptStmt* SEHExceptStmt::Create(ASTContext &C,
753 SourceLocation Loc,
754 Expr *FilterExpr,
755 Stmt *Block) {
756 return new(C) SEHExceptStmt(Loc,FilterExpr,Block);
757}
758
759SEHFinallyStmt::SEHFinallyStmt(SourceLocation Loc,
760 Stmt *Block)
761 : Stmt(SEHFinallyStmtClass),
762 Loc(Loc),
763 Block(Block)
764{}
765
766SEHFinallyStmt* SEHFinallyStmt::Create(ASTContext &C,
767 SourceLocation Loc,
768 Stmt *Block) {
769 return new(C)SEHFinallyStmt(Loc,Block);
770}