blob: 66182957d68850ecd6e19c42944bf2fe3cb5eb57 [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 {
Daniel Dunbare013d682009-10-18 20:26:12 +0000164 return getID()->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 Lattnerb3277932009-03-10 04:59:06 +0000221
222/// getInputConstraint - Return the specified input constraint. Unlike output
223/// constraints, these can be empty.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000224llvm::StringRef AsmStmt::getInputConstraint(unsigned i) const {
225 return getInputConstraintLiteral(i)->getString();
Ted Kremenekce2fc3a2008-10-27 18:40:21 +0000226}
227
Chris Lattner10ca96a2009-03-10 06:33:24 +0000228
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000229void AsmStmt::setOutputsAndInputsAndClobbers(ASTContext &C,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000230 IdentifierInfo **Names,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000231 StringLiteral **Constraints,
232 Stmt **Exprs,
233 unsigned NumOutputs,
Sean Huntc3021132010-05-05 15:23:54 +0000234 unsigned NumInputs,
Anders Carlssonfdba9c02010-01-30 19:34:25 +0000235 StringLiteral **Clobbers,
236 unsigned NumClobbers) {
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000237 this->NumOutputs = NumOutputs;
238 this->NumInputs = NumInputs;
Anders Carlsson966146e2010-01-30 23:19:41 +0000239 this->NumClobbers = NumClobbers;
240
241 unsigned NumExprs = NumOutputs + NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000242
Anders Carlsson966146e2010-01-30 23:19:41 +0000243 C.Deallocate(this->Names);
244 this->Names = new (C) IdentifierInfo*[NumExprs];
245 std::copy(Names, Names + NumExprs, this->Names);
Sean Huntc3021132010-05-05 15:23:54 +0000246
Anders Carlsson966146e2010-01-30 23:19:41 +0000247 C.Deallocate(this->Exprs);
248 this->Exprs = new (C) Stmt*[NumExprs];
249 std::copy(Exprs, Exprs + NumExprs, this->Exprs);
Sean Huntc3021132010-05-05 15:23:54 +0000250
Anders Carlsson966146e2010-01-30 23:19:41 +0000251 C.Deallocate(this->Constraints);
252 this->Constraints = new (C) StringLiteral*[NumExprs];
253 std::copy(Constraints, Constraints + NumExprs, this->Constraints);
Sean Huntc3021132010-05-05 15:23:54 +0000254
Anders Carlsson966146e2010-01-30 23:19:41 +0000255 C.Deallocate(this->Clobbers);
256 this->Clobbers = new (C) StringLiteral*[NumClobbers];
257 std::copy(Clobbers, Clobbers + NumClobbers, this->Clobbers);
Douglas Gregorcd7d5a92009-04-17 20:57:14 +0000258}
259
Chris Lattner10ca96a2009-03-10 06:33:24 +0000260/// getNamedOperand - Given a symbolic operand reference like %[foo],
261/// translate this into a numeric value needed to reference the same operand.
262/// This returns -1 if the operand name is invalid.
Anders Carlssonacb6bcb2010-01-30 20:38:10 +0000263int AsmStmt::getNamedOperand(llvm::StringRef SymbolicName) const {
Chris Lattner10ca96a2009-03-10 06:33:24 +0000264 unsigned NumPlusOperands = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattner10ca96a2009-03-10 06:33:24 +0000266 // Check if this is an output operand.
267 for (unsigned i = 0, e = getNumOutputs(); i != e; ++i) {
268 if (getOutputName(i) == SymbolicName)
269 return i;
Chris Lattner10ca96a2009-03-10 06:33:24 +0000270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattner10ca96a2009-03-10 06:33:24 +0000272 for (unsigned i = 0, e = getNumInputs(); i != e; ++i)
273 if (getInputName(i) == SymbolicName)
274 return getNumOutputs() + NumPlusOperands + i;
275
276 // Not found.
277 return -1;
278}
279
Chris Lattner458cd9c2009-03-10 23:21:44 +0000280/// AnalyzeAsmString - Analyze the asm string of the current asm, decomposing
281/// it into pieces. If the asm string is erroneous, emit errors and return
282/// true, otherwise return false.
Chris Lattnerfb5058e2009-03-10 23:41:04 +0000283unsigned AsmStmt::AnalyzeAsmString(llvm::SmallVectorImpl<AsmStringPiece>&Pieces,
284 ASTContext &C, unsigned &DiagOffs) const {
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000285 llvm::StringRef Str = getAsmString()->getString();
286 const char *StrStart = Str.begin();
287 const char *StrEnd = Str.end();
Chris Lattner3182db12009-03-10 23:51:40 +0000288 const char *CurPtr = StrStart;
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattner458cd9c2009-03-10 23:21:44 +0000290 // "Simple" inline asms have no constraints or operands, just convert the asm
291 // string to escape $'s.
292 if (isSimple()) {
293 std::string Result;
Chris Lattner3182db12009-03-10 23:51:40 +0000294 for (; CurPtr != StrEnd; ++CurPtr) {
295 switch (*CurPtr) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000296 case '$':
297 Result += "$$";
298 break;
299 default:
Chris Lattner3182db12009-03-10 23:51:40 +0000300 Result += *CurPtr;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000301 break;
302 }
303 }
304 Pieces.push_back(AsmStringPiece(Result));
Chris Lattner3182db12009-03-10 23:51:40 +0000305 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000306 }
307
308 // CurStringPiece - The current string that we are building up as we scan the
309 // asm string.
310 std::string CurStringPiece;
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Chris Lattner9bffb072010-04-23 16:29:58 +0000312 bool HasVariants = !C.Target.hasNoAsmVariants();
Sean Huntc3021132010-05-05 15:23:54 +0000313
Chris Lattner458cd9c2009-03-10 23:21:44 +0000314 while (1) {
315 // Done with the string?
Chris Lattner3182db12009-03-10 23:51:40 +0000316 if (CurPtr == StrEnd) {
Chris Lattner458cd9c2009-03-10 23:21:44 +0000317 if (!CurStringPiece.empty())
318 Pieces.push_back(AsmStringPiece(CurStringPiece));
Chris Lattner3182db12009-03-10 23:51:40 +0000319 return 0;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Chris Lattner3182db12009-03-10 23:51:40 +0000322 char CurChar = *CurPtr++;
Chris Lattner018b54e2010-04-05 18:44:00 +0000323 switch (CurChar) {
324 case '$': CurStringPiece += "$$"; continue;
Chris Lattner9bffb072010-04-23 16:29:58 +0000325 case '{': CurStringPiece += (HasVariants ? "$(" : "{"); continue;
326 case '|': CurStringPiece += (HasVariants ? "$|" : "|"); continue;
327 case '}': CurStringPiece += (HasVariants ? "$)" : "}"); continue;
Chris Lattner018b54e2010-04-05 18:44:00 +0000328 case '%':
329 break;
330 default:
Chris Lattner458cd9c2009-03-10 23:21:44 +0000331 CurStringPiece += CurChar;
332 continue;
333 }
Sean Huntc3021132010-05-05 15:23:54 +0000334
Chris Lattner458cd9c2009-03-10 23:21:44 +0000335 // Escaped "%" character in asm string.
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000336 if (CurPtr == StrEnd) {
337 // % at end of string is invalid (no escape).
338 DiagOffs = CurPtr-StrStart-1;
339 return diag::err_asm_invalid_escape;
340 }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Chris Lattner3182db12009-03-10 23:51:40 +0000342 char EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000343 if (EscapedChar == '%') { // %% -> %
344 // Escaped percentage sign.
345 CurStringPiece += '%';
346 continue;
347 }
Mike Stump1eb44332009-09-09 15:08:12 +0000348
Chris Lattner458cd9c2009-03-10 23:21:44 +0000349 if (EscapedChar == '=') { // %= -> Generate an unique ID.
350 CurStringPiece += "${:uid}";
351 continue;
352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattner458cd9c2009-03-10 23:21:44 +0000354 // Otherwise, we have an operand. If we have accumulated a string so far,
355 // add it to the Pieces list.
356 if (!CurStringPiece.empty()) {
357 Pieces.push_back(AsmStringPiece(CurStringPiece));
358 CurStringPiece.clear();
359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattner458cd9c2009-03-10 23:21:44 +0000361 // Handle %x4 and %x[foo] by capturing x as the modifier character.
362 char Modifier = '\0';
363 if (isalpha(EscapedChar)) {
364 Modifier = EscapedChar;
Chris Lattner3182db12009-03-10 23:51:40 +0000365 EscapedChar = *CurPtr++;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattner458cd9c2009-03-10 23:21:44 +0000368 if (isdigit(EscapedChar)) {
369 // %n - Assembler operand n
Chris Lattnercafc2222009-03-11 22:52:17 +0000370 unsigned N = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattnercafc2222009-03-11 22:52:17 +0000372 --CurPtr;
373 while (CurPtr != StrEnd && isdigit(*CurPtr))
Chris Lattner32a47ed2009-03-11 23:09:16 +0000374 N = N*10 + ((*CurPtr++)-'0');
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Chris Lattner85759272009-03-11 00:23:13 +0000376 unsigned NumOperands =
377 getNumOutputs() + getNumPlusOperands() + getNumInputs();
378 if (N >= NumOperands) {
379 DiagOffs = CurPtr-StrStart-1;
380 return diag::err_asm_invalid_operand_number;
381 }
382
Chris Lattner458cd9c2009-03-10 23:21:44 +0000383 Pieces.push_back(AsmStringPiece(N, Modifier));
384 continue;
385 }
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Chris Lattner458cd9c2009-03-10 23:21:44 +0000387 // Handle %[foo], a symbolic operand reference.
388 if (EscapedChar == '[') {
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000389 DiagOffs = CurPtr-StrStart-1;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000391 // Find the ']'.
Chris Lattner3182db12009-03-10 23:51:40 +0000392 const char *NameEnd = (const char*)memchr(CurPtr, ']', StrEnd-CurPtr);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000393 if (NameEnd == 0)
394 return diag::err_asm_unterminated_symbolic_operand_name;
395 if (NameEnd == CurPtr)
396 return diag::err_asm_empty_symbolic_operand_name;
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Anders Carlsson95c9ce92010-01-30 20:48:08 +0000398 llvm::StringRef SymbolicName(CurPtr, NameEnd - CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattner458cd9c2009-03-10 23:21:44 +0000400 int N = getNamedOperand(SymbolicName);
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000401 if (N == -1) {
402 // Verify that an operand with that name exists.
403 DiagOffs = CurPtr-StrStart;
404 return diag::err_asm_unknown_symbolic_operand_name;
405 }
Chris Lattner458cd9c2009-03-10 23:21:44 +0000406 Pieces.push_back(AsmStringPiece(N, Modifier));
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattnereab8cfb2009-03-11 00:06:36 +0000408 CurPtr = NameEnd+1;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000409 continue;
410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Chris Lattner2ff0f422009-03-10 23:57:07 +0000412 DiagOffs = CurPtr-StrStart-1;
Chris Lattner3182db12009-03-10 23:51:40 +0000413 return diag::err_asm_invalid_escape;
Chris Lattner458cd9c2009-03-10 23:21:44 +0000414 }
415}
416
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000417QualType CXXCatchStmt::getCaughtType() const {
418 if (ExceptionDecl)
419 return ExceptionDecl->getType();
420 return QualType();
421}
422
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000423//===----------------------------------------------------------------------===//
424// Constructors
425//===----------------------------------------------------------------------===//
426
Sean Huntc3021132010-05-05 15:23:54 +0000427AsmStmt::AsmStmt(ASTContext &C, SourceLocation asmloc, bool issimple,
428 bool isvolatile, bool msasm,
Anders Carlsson966146e2010-01-30 23:19:41 +0000429 unsigned numoutputs, unsigned numinputs,
Anders Carlssonff93dbd2010-01-30 22:25:16 +0000430 IdentifierInfo **names, StringLiteral **constraints,
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000431 Expr **exprs, StringLiteral *asmstr, unsigned numclobbers,
432 StringLiteral **clobbers, SourceLocation rparenloc)
Anders Carlssonb235fc22007-11-22 01:36:19 +0000433 : Stmt(AsmStmtClass), AsmLoc(asmloc), RParenLoc(rparenloc), AsmStr(asmstr)
Mike Stump3b11fd32010-01-04 22:37:17 +0000434 , IsSimple(issimple), IsVolatile(isvolatile), MSAsm(msasm)
Anders Carlsson966146e2010-01-30 23:19:41 +0000435 , NumOutputs(numoutputs), NumInputs(numinputs), NumClobbers(numclobbers) {
Nico Weber608b17f2008-08-05 23:15:29 +0000436
Anders Carlsson966146e2010-01-30 23:19:41 +0000437 unsigned NumExprs = NumOutputs +NumInputs;
Sean Huntc3021132010-05-05 15:23:54 +0000438
Anders Carlsson966146e2010-01-30 23:19:41 +0000439 Names = new (C) IdentifierInfo*[NumExprs];
440 std::copy(names, names + NumExprs, Names);
441
442 Exprs = new (C) Stmt*[NumExprs];
443 std::copy(exprs, exprs + NumExprs, Exprs);
444
445 Constraints = new (C) StringLiteral*[NumExprs];
446 std::copy(constraints, constraints + NumExprs, Constraints);
447
448 Clobbers = new (C) StringLiteral*[NumClobbers];
449 std::copy(clobbers, clobbers + NumClobbers, Clobbers);
Anders Carlssonb235fc22007-11-22 01:36:19 +0000450}
451
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000452ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
453 Stmt *Body, SourceLocation FCL,
Nico Weber608b17f2008-08-05 23:15:29 +0000454 SourceLocation RPL)
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000455: Stmt(ObjCForCollectionStmtClass) {
456 SubExprs[ELEM] = Elem;
457 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(Collect);
458 SubExprs[BODY] = Body;
459 ForLoc = FCL;
460 RParenLoc = RPL;
461}
462
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000463ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
464 Stmt **CatchStmts, unsigned NumCatchStmts,
465 Stmt *atFinallyStmt)
466 : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
467 NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != 0)
468{
469 Stmt **Stmts = getStmts();
470 Stmts[0] = atTryStmt;
471 for (unsigned I = 0; I != NumCatchStmts; ++I)
472 Stmts[I + 1] = CatchStmts[I];
Sean Huntc3021132010-05-05 15:23:54 +0000473
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000474 if (HasFinally)
475 Stmts[NumCatchStmts + 1] = atFinallyStmt;
476}
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000477
Sean Huntc3021132010-05-05 15:23:54 +0000478ObjCAtTryStmt *ObjCAtTryStmt::Create(ASTContext &Context,
479 SourceLocation atTryLoc,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000480 Stmt *atTryStmt,
Sean Huntc3021132010-05-05 15:23:54 +0000481 Stmt **CatchStmts,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000482 unsigned NumCatchStmts,
483 Stmt *atFinallyStmt) {
Sean Huntc3021132010-05-05 15:23:54 +0000484 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000485 (1 + NumCatchStmts + (atFinallyStmt != 0)) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000486 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000487 return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
488 atFinallyStmt);
489}
Ted Kremenekff981022008-02-01 21:28:59 +0000490
Sean Huntc3021132010-05-05 15:23:54 +0000491ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(ASTContext &Context,
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000492 unsigned NumCatchStmts,
493 bool HasFinally) {
Sean Huntc3021132010-05-05 15:23:54 +0000494 unsigned Size = sizeof(ObjCAtTryStmt) +
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000495 (1 + NumCatchStmts + HasFinally) * sizeof(Stmt *);
Chris Lattner32488542010-10-30 05:14:06 +0000496 void *Mem = Context.Allocate(Size, llvm::alignOf<ObjCAtTryStmt>());
Sean Huntc3021132010-05-05 15:23:54 +0000497 return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000498}
Nico Weber608b17f2008-08-05 23:15:29 +0000499
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000500SourceRange ObjCAtTryStmt::getSourceRange() const {
501 SourceLocation EndLoc;
502 if (HasFinally)
503 EndLoc = getFinallyStmt()->getLocEnd();
504 else if (NumCatchStmts)
505 EndLoc = getCatchStmt(NumCatchStmts - 1)->getLocEnd();
506 else
507 EndLoc = getTryBody()->getLocEnd();
Sean Huntc3021132010-05-05 15:23:54 +0000508
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +0000509 return SourceRange(AtTryLoc, EndLoc);
Chris Lattnerdb6ed172008-01-30 05:01:46 +0000510}
511
Sam Weiniga1a396d2010-02-03 03:56:39 +0000512CXXTryStmt *CXXTryStmt::Create(ASTContext &C, SourceLocation tryLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000513 Stmt *tryBlock, Stmt **handlers,
Sam Weiniga1a396d2010-02-03 03:56:39 +0000514 unsigned numHandlers) {
515 std::size_t Size = sizeof(CXXTryStmt);
516 Size += ((numHandlers + 1) * sizeof(Stmt));
517
Chris Lattner32488542010-10-30 05:14:06 +0000518 void *Mem = C.Allocate(Size, llvm::alignOf<CXXTryStmt>());
Sam Weiniga1a396d2010-02-03 03:56:39 +0000519 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers, numHandlers);
520}
521
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000522CXXTryStmt *CXXTryStmt::Create(ASTContext &C, EmptyShell Empty,
523 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>());
Argyrios Kyrtzidis7cb45e32010-07-22 16:03:56 +0000528 return new (Mem) CXXTryStmt(Empty, numHandlers);
529}
530
Sam Weiniga1a396d2010-02-03 03:56:39 +0000531CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000532 Stmt **handlers, unsigned numHandlers)
533 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(numHandlers) {
Sam Weiniga1a396d2010-02-03 03:56:39 +0000534 Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
Sam Weinigb0e4cb62010-02-03 02:09:59 +0000535 Stmts[0] = tryBlock;
536 std::copy(handlers, handlers + NumHandlers, Stmts + 1);
537}
538
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000539IfStmt::IfStmt(ASTContext &C, SourceLocation IL, VarDecl *var, Expr *cond,
Argyrios Kyrtzidis44aa1f32010-11-20 02:04:01 +0000540 Stmt *then, SourceLocation EL, Stmt *elsev)
541 : Stmt(IfStmtClass), IfLoc(IL), ElseLoc(EL)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000542{
543 setConditionVariable(C, var);
544 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
545 SubExprs[THEN] = then;
546 SubExprs[ELSE] = elsev;
547}
548
549VarDecl *IfStmt::getConditionVariable() const {
550 if (!SubExprs[VAR])
551 return 0;
552
553 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
554 return cast<VarDecl>(DS->getSingleDecl());
555}
556
557void IfStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
558 if (!V) {
559 SubExprs[VAR] = 0;
560 return;
561 }
562
563 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
564 V->getSourceRange().getBegin(),
565 V->getSourceRange().getEnd());
566}
567
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000568ForStmt::ForStmt(ASTContext &C, Stmt *Init, Expr *Cond, VarDecl *condVar,
569 Expr *Inc, Stmt *Body, SourceLocation FL, SourceLocation LP,
570 SourceLocation RP)
571 : Stmt(ForStmtClass), ForLoc(FL), LParenLoc(LP), RParenLoc(RP)
572{
573 SubExprs[INIT] = Init;
574 setConditionVariable(C, condVar);
575 SubExprs[COND] = reinterpret_cast<Stmt*>(Cond);
576 SubExprs[INC] = reinterpret_cast<Stmt*>(Inc);
577 SubExprs[BODY] = Body;
578}
579
580VarDecl *ForStmt::getConditionVariable() const {
581 if (!SubExprs[CONDVAR])
582 return 0;
583
584 DeclStmt *DS = cast<DeclStmt>(SubExprs[CONDVAR]);
585 return cast<VarDecl>(DS->getSingleDecl());
586}
587
588void ForStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
589 if (!V) {
590 SubExprs[CONDVAR] = 0;
591 return;
592 }
593
594 SubExprs[CONDVAR] = new (C) DeclStmt(DeclGroupRef(V),
595 V->getSourceRange().getBegin(),
596 V->getSourceRange().getEnd());
597}
598
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000599SwitchStmt::SwitchStmt(ASTContext &C, VarDecl *Var, Expr *cond)
Ted Kremenek780d8852010-09-09 00:06:01 +0000600 : Stmt(SwitchStmtClass), FirstCase(0), AllEnumCasesCovered(0)
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000601{
602 setConditionVariable(C, Var);
603 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
604 SubExprs[BODY] = NULL;
605}
606
607VarDecl *SwitchStmt::getConditionVariable() const {
608 if (!SubExprs[VAR])
609 return 0;
610
611 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
612 return cast<VarDecl>(DS->getSingleDecl());
613}
614
615void SwitchStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
616 if (!V) {
617 SubExprs[VAR] = 0;
618 return;
619 }
620
621 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
622 V->getSourceRange().getBegin(),
623 V->getSourceRange().getEnd());
624}
625
John McCall63c00d72011-02-09 08:16:59 +0000626Stmt *SwitchCase::getSubStmt() {
627 if (isa<CaseStmt>(this)) return cast<CaseStmt>(this)->getSubStmt();
628 return cast<DefaultStmt>(this)->getSubStmt();
629}
630
Douglas Gregor43dec6b2010-06-21 23:44:13 +0000631WhileStmt::WhileStmt(ASTContext &C, VarDecl *Var, Expr *cond, Stmt *body,
632 SourceLocation WL)
633: Stmt(WhileStmtClass)
634{
635 setConditionVariable(C, Var);
636 SubExprs[COND] = reinterpret_cast<Stmt*>(cond);
637 SubExprs[BODY] = body;
638 WhileLoc = WL;
639}
640
641VarDecl *WhileStmt::getConditionVariable() const {
642 if (!SubExprs[VAR])
643 return 0;
644
645 DeclStmt *DS = cast<DeclStmt>(SubExprs[VAR]);
646 return cast<VarDecl>(DS->getSingleDecl());
647}
648
649void WhileStmt::setConditionVariable(ASTContext &C, VarDecl *V) {
650 if (!V) {
651 SubExprs[VAR] = 0;
652 return;
653 }
654
655 SubExprs[VAR] = new (C) DeclStmt(DeclGroupRef(V),
656 V->getSourceRange().getBegin(),
657 V->getSourceRange().getEnd());
658}
659
Ted Kremenek82977772007-08-24 21:09:09 +0000660// IndirectGotoStmt
John McCall95c225d2010-10-28 08:53:48 +0000661LabelStmt *IndirectGotoStmt::getConstantTarget() {
662 if (AddrLabelExpr *E =
663 dyn_cast<AddrLabelExpr>(getTarget()->IgnoreParenImpCasts()))
664 return E->getLabel();
665 return 0;
666}
Ted Kremenek82977772007-08-24 21:09:09 +0000667
Ted Kremenek82977772007-08-24 21:09:09 +0000668// ReturnStmt
Ted Kremenek1060aff2008-06-17 03:11:08 +0000669const Expr* ReturnStmt::getRetValue() const {
670 return cast_or_null<Expr>(RetExpr);
671}
672Expr* ReturnStmt::getRetValue() {
673 return cast_or_null<Expr>(RetExpr);
Ted Kremenek82977772007-08-24 21:09:09 +0000674}