blob: 92af03a5421b30b053ccaae64f7fc10bd6d504df [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.
94 static inline good is_good(good) {}
John McCall63c00d72011-02-09 08:16:59 +000095
96 typedef Stmt::child_range children_t();
John McCallf8c7fdb2011-02-09 08:31:17 +000097 template <class T> good implements_children(children_t T::*) {}
98 static inline bad implements_children(children_t Stmt::*) {}
John McCall63c00d72011-02-09 08:16:59 +000099
100 typedef SourceRange getSourceRange_t() const;
John McCallf8c7fdb2011-02-09 08:31:17 +0000101 template <class T> good implements_getSourceRange(getSourceRange_t T::*) {}
102 static inline bad implements_getSourceRange(getSourceRange_t Stmt::*) {}
John McCall63c00d72011-02-09 08:16:59 +0000103
104#define ASSERT_IMPLEMENTS_children(type) \
105 (void) sizeof(is_good(implements_children(&type::children)))
106#define ASSERT_IMPLEMENTS_getSourceRange(type) \
107 (void) sizeof(is_good(implements_getSourceRange(&type::getSourceRange)))
108}
109
110/// Check whether the various Stmt classes implement their member
111/// functions.
112static inline void check_implementations() {
113#define ABSTRACT_STMT(type)
114#define STMT(type, base) \
115 ASSERT_IMPLEMENTS_children(type); \
116 ASSERT_IMPLEMENTS_getSourceRange(type);
117#include "clang/AST/StmtNodes.inc"
118}
119
120Stmt::child_range Stmt::children() {
121 switch (getStmtClass()) {
122 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
123#define ABSTRACT_STMT(type)
124#define STMT(type, base) \
125 case Stmt::type##Class: \
126 return static_cast<type*>(this)->children();
127#include "clang/AST/StmtNodes.inc"
128 }
129 llvm_unreachable("unknown statement kind!");
130 return child_range();
131}
132
133SourceRange Stmt::getSourceRange() const {
134 switch (getStmtClass()) {
135 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
136#define ABSTRACT_STMT(type)
137#define STMT(type, base) \
138 case Stmt::type##Class: \
139 return static_cast<const type*>(this)->getSourceRange();
140#include "clang/AST/StmtNodes.inc"
141 }
142 llvm_unreachable("unknown statement kind!");
143 return SourceRange();
144}
145
Douglas Gregor025452f2009-04-17 00:04:06 +0000146void CompoundStmt::setStmts(ASTContext &C, Stmt **Stmts, unsigned NumStmts) {
147 if (this->Body)
148 C.Deallocate(Body);
John McCall8e6285a2010-10-26 08:39:16 +0000149 this->CompoundStmtBits.NumStmts = NumStmts;
Douglas Gregor025452f2009-04-17 00:04:06 +0000150
151 Body = new (C) Stmt*[NumStmts];
152 memcpy(Body, Stmts, sizeof(Stmt *) * NumStmts);
153}
Reid Spencer5f016e22007-07-11 17:01:13 +0000154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155const char *LabelStmt::getName() const {
Daniel Dunbare013d682009-10-18 20:26:12 +0000156 return getID()->getNameStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000157}
158
Steve Naroff507f2d52007-08-31 23:49:30 +0000159// This is defined here to avoid polluting Stmt.h with importing Expr.h
Nico Weber608b17f2008-08-05 23:15:29 +0000160SourceRange ReturnStmt::getSourceRange() const {
Steve Naroff507f2d52007-08-31 23:49:30 +0000161 if (RetExpr)
162 return SourceRange(RetLoc, RetExpr->getLocEnd());
163 else
164 return SourceRange(RetLoc);
165}
166
Ted Kremenekd48ade62007-10-01 16:34:52 +0000167bool Stmt::hasImplicitControlFlow() const {
John McCall8e6285a2010-10-26 08:39:16 +0000168 switch (StmtBits.sClass) {
Ted Kremenekd48ade62007-10-01 16:34:52 +0000169 default:
170 return false;
Nico Weber608b17f2008-08-05 23:15:29 +0000171
Ted Kremenekd48ade62007-10-01 16:34:52 +0000172 case CallExprClass:
173 case ConditionalOperatorClass:
174 case ChooseExprClass:
175 case StmtExprClass:
176 case DeclStmtClass:
Nico Weber608b17f2008-08-05 23:15:29 +0000177 return true;
178
Ted Kremenekd48ade62007-10-01 16:34:52 +0000179 case Stmt::BinaryOperatorClass: {
180 const BinaryOperator* B = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +0000181 if (B->isLogicalOp() || B->getOpcode() == BO_Comma)
Ted Kremenekd48ade62007-10-01 16:34:52 +0000182 return true;
183 else
184 return false;
185 }
186 }
187}
188
Chris Lattnerb3277932009-03-10 04:59:06 +0000189Expr *AsmStmt::getOutputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000190 return cast<Expr>(Exprs[i]);
191}
Chris Lattnerb3277932009-03-10 04:59:06 +0000192
193/// getOutputConstraint - Return the constraint string for the specified
194/// output operand. All output constraints are known to be non-empty (either
195/// '=' or '+').
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000196llvm::StringRef AsmStmt::getOutputConstraint(unsigned i) const {
197 return getOutputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000198}
Chris Lattnerb3277932009-03-10 04:59:06 +0000199
Chris Lattner85759272009-03-11 00:23:13 +0000200/// getNumPlusOperands - Return the number of output operands that have a "+"
201/// constraint.
202unsigned AsmStmt::getNumPlusOperands() const {
203 unsigned Res = 0;
204 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i)
205 if (isOutputPlusConstraint(i))
206 ++Res;
207 return Res;
208}
209
Chris Lattnerb3277932009-03-10 04:59:06 +0000210Expr *AsmStmt::getInputExpr(unsigned i) {
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000211 return cast<Expr>(Exprs[i + NumOutputs]);
212}
Chris Lattnerb3277932009-03-10 04:59:06 +0000213
214/// getInputConstraint - Return the specified input constraint. Unlike output
215/// constraints, these can be empty.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000216llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
217 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000218}
219
Chris Lattner10ca96a2009-03-10 06:33:24 +0000220
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000221void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000222 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000223 StringLiteral **Constraints,
224 Stmt **Exprs,
225 unsigned NumOutputs,
Sean Huntc3021132010-05-05 15:23:54 +0000226 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000227 StringLiteral **Clobbers,
228 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000229 this->NumOutputs = NumOutputs;
230 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000231 this->NumClobbers = NumClobbers;
232
233 unsigned NumExprs = NumOutputs + NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000234
Anders Carlsson966146e2010-01-30 23:19:41 +0000235 C.Deallocate(this->Names);
236 this->Names = new (C) IdentifierInfo*[NumExprs];
237 std::copy(Names, Names + NumExprs, this->Names);
Sean Huntc3021132010-05-05 15:23:54 +0000238
Anders Carlsson966146e2010-01-30 23:19:41 +0000239 C.Deallocate(this->Exprs);
240 this->Exprs = new (C) Stmt*[NumExprs];
241 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Sean Huntc3021132010-05-05 15:23:54 +0000242
Anders Carlsson966146e2010-01-30 23:19:41 +0000243 C.Deallocate(this->Constraints);
244 this->Constraints = new (C) StringLiteral*[NumExprs];
245 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Sean Huntc3021132010-05-05 15:23:54 +0000246
Anders Carlsson966146e2010-01-30 23:19:41 +0000247 C.Deallocate(this->Clobbers);
248 this->Clobbers = new (C) StringLiteral*[NumClobbers];
249 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000250}
251
Chris Lattner10ca96a2009-03-10 06:33:24 +0000252/// getNamedOperand - Given a symbolic operand reference like %[foo],
253/// translate this into a numeric value needed to reference the same operand.
254/// This returns -1 if the operand name is invalid.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000255int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000256 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner10ca96a2009-03-10 06:33:24 +0000258 // Check if this is an output operand.
259 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
260 if (getOutputName(i) == SymbolicName)
261 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000262 }
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Chris Lattner10ca96a2009-03-10 06:33:24 +0000264 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
265 if (getInputName(i) == SymbolicName)
266 return getNumOutputs() + NumPlusOperands + i;
267
268 // Not found.
269 return -1;
270}
271
Chris Lattner458cd9c2009-03-10 23:21:44 +0000272/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
273/// it into pieces. If the asm string is erroneous, emit errors and return
274/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000275unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
276 ASTContext &C, unsigned &DiagOffs) const {
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000277 llvm::StringRef Str = getAsmString()->getString();
278 const char *StrStart = Str.begin();
279 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000280 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattner458cd9c2009-03-10 23:21:44 +0000282 // "Simple" inline asms have no constraints or operands, just convert the asm
283 // string to escape $'s.
284 if (isSimple()) {
285 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000286 for (; CurPtr != StrEnd; ++CurPtr) {
287 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000288 case '$':
289 Result += "$$";
290 break;
291 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000292 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000293 break;
294 }
295 }
296 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000297 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000298 }
299
300 // CurStringPiece - The current string that we are building up as we scan the
301 // asm string.
302 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Chris Lattner9bffb072010-04-23 16:29:58 +0000304 bool HasVariants = !C.Target.hasNoAsmVariants();
Sean Huntc3021132010-05-05 15:23:54 +0000305
Chris Lattner458cd9c2009-03-10 23:21:44 +0000306 while (1) {
307 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000308 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000309 if (!CurStringPiece.empty())
310 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000311 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Chris Lattner3182db12009-03-10 23:51:40 +0000314 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000315 switch (CurChar) {
316 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000317 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
318 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
319 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000320 case '%':
321 break;
322 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000323 CurStringPiece += CurChar;
324 continue;
325 }
Sean Huntc3021132010-05-05 15:23:54 +0000326
Chris Lattner458cd9c2009-03-10 23:21:44 +0000327 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000328 if (CurPtr == StrEnd) {
329 // % at end of string is invalid (no escape).
330 DiagOffs = CurPtr-StrStart-1;
331 return diag::err_asm_invalid_escape;
332 }
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Chris Lattner3182db12009-03-10 23:51:40 +0000334 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000335 if (EscapedChar == '%') { // %% -> %
336 // Escaped percentage sign.
337 CurStringPiece += '%';
338 continue;
339 }
Mike Stump1eb44332009-09-09 15:08:12 +0000340
Chris Lattner458cd9c2009-03-10 23:21:44 +0000341 if (EscapedChar == '=') { // %= -> Generate an unique ID.
342 CurStringPiece += "${:uid}";
343 continue;
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Chris Lattner458cd9c2009-03-10 23:21:44 +0000346 // Otherwise, we have an operand. If we have accumulated a string so far,
347 // add it to the Pieces list.
348 if (!CurStringPiece.empty()) {
349 Pieces.push_back(AsmStringPiece(CurStringPiece));
350 CurStringPiece.clear();
351 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Chris Lattner458cd9c2009-03-10 23:21:44 +0000353 // Handle %x4 and %x[foo] by capturing x as the modifier character.
354 char Modifier = '\0';
355 if (isalpha(EscapedChar)) {
356 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000357 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Chris Lattner458cd9c2009-03-10 23:21:44 +0000360 if (isdigit(EscapedChar)) {
361 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000362 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattnercafc2222009-03-11 22:52:17 +0000364 --CurPtr;
365 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000366 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattner85759272009-03-11 00:23:13 +0000368 unsigned NumOperands =
369 getNumOutputs() + getNumPlusOperands() + getNumInputs();
370 if (N >= NumOperands) {
371 DiagOffs = CurPtr-StrStart-1;
372 return diag::err_asm_invalid_operand_number;
373 }
374
Chris Lattner458cd9c2009-03-10 23:21:44 +0000375 Pieces.push_back(AsmStringPiece(N, Modifier));
376 continue;
377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Chris Lattner458cd9c2009-03-10 23:21:44 +0000379 // Handle %[foo], a symbolic operand reference.
380 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000381 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000383 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000384 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000385 if (NameEnd == 0)
386 return diag::err_asm_unterminated_symbolic_operand_name;
387 if (NameEnd == CurPtr)
388 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Anders Carlsson95c9ce92010-01-30 20:48:08 +0000390 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattner458cd9c2009-03-10 23:21:44 +0000392 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000393 if (N == -1) {
394 // Verify that an operand with that name exists.
395 DiagOffs = CurPtr-StrStart;
396 return diag::err_asm_unknown_symbolic_operand_name;
397 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000398 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000400 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000401 continue;
402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner2ff0f422009-03-10 23:57:07 +0000404 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000405 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000406 }
407}
408
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000409QualType CXXCatchStmt::getCaughtType() const {
410 if (ExceptionDecl)
411 return ExceptionDecl->getType();
412 return QualType();
413}
414
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000415//===----------------------------------------------------------------------===//
416// Constructors
417//===----------------------------------------------------------------------===//
418
Sean Huntc3021132010-05-05 15:23:54 +0000419AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
420 bool isvolatile, bool msasm,
Anders Carlsson966146e2010-01-30 23:19:41 +0000421 unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000422 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000423 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
424 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000425 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000426 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson966146e2010-01-30 23:19:41 +0000427 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000428
Anders Carlsson966146e2010-01-30 23:19:41 +0000429 unsigned NumExprs = NumOutputs +NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000430
Anders Carlsson966146e2010-01-30 23:19:41 +0000431 Names = new (C) IdentifierInfo*[NumExprs];
432 std::copy(names, names + NumExprs, Names);
433
434 Exprs = new (C) Stmt*[NumExprs];
435 std::copy(exprs, exprs + NumExprs, Exprs);
436
437 Constraints = new (C) StringLiteral*[NumExprs];
438 std::copy(constraints, constraints + NumExprs, Constraints);
439
440 Clobbers = new (C) StringLiteral*[NumClobbers];
441 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000442}
443
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000444ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
445 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000446 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000447: Stmt(ObjCForCollectionStmtClass) {
448 SubExprs[ELEM] = Elem;
449 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
450 SubExprs[BODY] = Body;
451 ForLoc = FCL;
452 RParenLoc = RPL;
453}
454
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000455ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
456 Stmt **CatchStmts, unsigned NumCatchStmts,
457 Stmt *atFinallyStmt)
458 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
459 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
460{
461 Stmt **Stmts = getStmts();
462 Stmts[0] = atTryStmt;
463 for (unsigned I = 0; I != NumCatchStmts; ++I)
464 Stmts[I + 1] = CatchStmts[I];
Sean Huntc3021132010-05-05 15:23:54 +0000465
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000466 if (HasFinally)
467 Stmts[NumCatchStmts + 1] = atFinallyStmt;
468}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000469
Sean Huntc3021132010-05-05 15:23:54 +0000470ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
471 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000472 Stmt *atTryStmt,
Sean Huntc3021132010-05-05 15:23:54 +0000473 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000474 unsigned NumCatchStmts,
475 Stmt *atFinallyStmt) {
Sean Huntc3021132010-05-05 15:23:54 +0000476 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000477 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000478 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000479 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
480 atFinallyStmt);
481}
Ted Kremenekff981022008-02-01 21:28:59 +0000482
Sean Huntc3021132010-05-05 15:23:54 +0000483ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000484 unsigned NumCatchStmts,
485 bool HasFinally) {
Sean Huntc3021132010-05-05 15:23:54 +0000486 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000487 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000488 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Sean Huntc3021132010-05-05 15:23:54 +0000489 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000490}
Nico Weber608b17f2008-08-05 23:15:29 +0000491
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000492SourceRange ObjCAtTryStmt::getSourceRange() const {
493 SourceLocation EndLoc;
494 if (HasFinally)
495 EndLoc = getFinallyStmt()->getLocEnd();
496 else if (NumCatchStmts)
497 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
498 else
499 EndLoc = getTryBody()->getLocEnd();
Sean Huntc3021132010-05-05 15:23:54 +0000500
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000501 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000502}
503
Sam Weiniga1a396d2010-02-03 03:56:39 +0000504CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000505 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000506 unsigned numHandlers) {
507 std::size_t Size = sizeof(CXXTryStmt);
508 Size += ((numHandlers + 1) * sizeof(Stmt));
509
Chris Lattner32488542010-10-30 05:14:06 +0000510 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000511 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
512}
513
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000514CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
515 unsigned numHandlers) {
516 std::size_t Size = sizeof(CXXTryStmt);
517 Size += ((numHandlers + 1) * sizeof(Stmt));
518
Chris Lattner32488542010-10-30 05:14:06 +0000519 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000520 return new (Mem) CXXTryStmt(Empty, numHandlers);
521}
522
Sam Weiniga1a396d2010-02-03 03:56:39 +0000523CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000524 Stmt **handlers, unsigned numHandlers)
525 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000526 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000527 Stmts[0] = tryBlock;
528 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
529}
530
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000531IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000532 Stmt *then, SourceLocation EL, Stmt *elsev)
533 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000534{
535 setConditionVariable(C, var);
536 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
537 SubExprs[THEN] = then;
538 SubExprs[ELSE] = elsev;
539}
540
541VarDecl *IfStmt::getConditionVariable() const {
542 if (!SubExprs[VAR])
543 return 0;
544
545 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
546 return cast<VarDecl>(DS->getSingleDecl());
547}
548
549void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
550 if (!V) {
551 SubExprs[VAR] = 0;
552 return;
553 }
554
555 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
556 V->getSourceRange().getBegin(),
557 V->getSourceRange().getEnd());
558}
559
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000560ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
561 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
562 SourceLocation RP)
563 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
564{
565 SubExprs[INIT] = Init;
566 setConditionVariable(C, condVar);
567 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
568 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
569 SubExprs[BODY] = Body;
570}
571
572VarDecl *ForStmt::getConditionVariable() const {
573 if (!SubExprs[CONDVAR])
574 return 0;
575
576 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
577 return cast<VarDecl>(DS->getSingleDecl());
578}
579
580void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
581 if (!V) {
582 SubExprs[CONDVAR] = 0;
583 return;
584 }
585
586 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V),
587 V->getSourceRange().getBegin(),
588 V->getSourceRange().getEnd());
589}
590
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000591SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
Ted Kremenek780d8852010-09-09 00:06:01 +0000592 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000593{
594 setConditionVariable(C, Var);
595 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
596 SubExprs[BODY] = NULL;
597}
598
599VarDecl *SwitchStmt::getConditionVariable() const {
600 if (!SubExprs[VAR])
601 return 0;
602
603 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
604 return cast<VarDecl>(DS->getSingleDecl());
605}
606
607void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
608 if (!V) {
609 SubExprs[VAR] = 0;
610 return;
611 }
612
613 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
614 V->getSourceRange().getBegin(),
615 V->getSourceRange().getEnd());
616}
617
John McCall63c00d72011-02-09 08:16:59 +0000618Stmt *SwitchCase::getSubStmt() {
619 if (isa<CaseStmt>(this)) return cast<CaseStmt>(this)->getSubStmt();
620 return cast<DefaultStmt>(this)->getSubStmt();
621}
622
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000623WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
624 SourceLocation WL)
625: Stmt(WhileStmtClass)
626{
627 setConditionVariable(C, Var);
628 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
629 SubExprs[BODY] = body;
630 WhileLoc = WL;
631}
632
633VarDecl *WhileStmt::getConditionVariable() const {
634 if (!SubExprs[VAR])
635 return 0;
636
637 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
638 return cast<VarDecl>(DS->getSingleDecl());
639}
640
641void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
642 if (!V) {
643 SubExprs[VAR] = 0;
644 return;
645 }
646
647 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
648 V->getSourceRange().getBegin(),
649 V->getSourceRange().getEnd());
650}
651
Ted Kremenek82977772007-08-24 21:09:09 +0000652// IndirectGotoStmt
John McCall95c225d2010-10-28 08:53:48 +0000653LabelStmt *IndirectGotoStmt::getConstantTarget() {
654 if (AddrLabelExpr *E =
655 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
656 return E->getLabel();
657 return 0;
658}
Ted Kremenek82977772007-08-24 21:09:09 +0000659
Ted Kremenek82977772007-08-24 21:09:09 +0000660// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000661const Expr* ReturnStmt::getRetValue() const {
662 return cast_or_null<Expr>(RetExpr);
663}
664Expr* ReturnStmt::getRetValue() {
665 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000666}