blob: d957e33aa126b8daf30b8b4017d2e5cbef7b35f1 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar64789f82008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000015#include "clang/AST/APValue.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner1eee9402008-10-06 06:40:35 +000017#include "clang/AST/DeclObjC.h"
Douglas Gregor6573cfd2008-10-21 23:43:52 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/AST/StmtVisitor.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000021#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Primary Expressions.
26//===----------------------------------------------------------------------===//
27
Chris Lattnere0391b22008-06-07 22:13:43 +000028/// getValueAsApproximateDouble - This returns the value as an inaccurate
29/// double. Note that this may cause loss of precision, but is useful for
30/// debugging dumps, etc.
31double FloatingLiteral::getValueAsApproximateDouble() const {
32 llvm::APFloat V = getValue();
Dale Johannesen2461f612008-10-09 23:02:32 +000033 bool ignored;
34 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
35 &ignored);
Chris Lattnere0391b22008-06-07 22:13:43 +000036 return V.convertToDouble();
37}
38
39
Chris Lattner4b009652007-07-25 00:24:17 +000040StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
41 bool Wide, QualType t, SourceLocation firstLoc,
42 SourceLocation lastLoc) :
43 Expr(StringLiteralClass, t) {
44 // OPTIMIZE: could allocate this appended to the StringLiteral.
45 char *AStrData = new char[byteLength];
46 memcpy(AStrData, strData, byteLength);
47 StrData = AStrData;
48 ByteLength = byteLength;
49 IsWide = Wide;
50 firstTokLoc = firstLoc;
51 lastTokLoc = lastLoc;
52}
53
54StringLiteral::~StringLiteral() {
55 delete[] StrData;
56}
57
58bool UnaryOperator::isPostfix(Opcode Op) {
59 switch (Op) {
60 case PostInc:
61 case PostDec:
62 return true;
63 default:
64 return false;
65 }
66}
67
Ted Kremenek97318dd2008-07-23 22:18:43 +000068bool UnaryOperator::isPrefix(Opcode Op) {
69 switch (Op) {
70 case PreInc:
71 case PreDec:
72 return true;
73 default:
74 return false;
75 }
76}
77
Chris Lattner4b009652007-07-25 00:24:17 +000078/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
79/// corresponds to, e.g. "sizeof" or "[pre]++".
80const char *UnaryOperator::getOpcodeStr(Opcode Op) {
81 switch (Op) {
82 default: assert(0 && "Unknown unary operator");
83 case PostInc: return "++";
84 case PostDec: return "--";
85 case PreInc: return "++";
86 case PreDec: return "--";
87 case AddrOf: return "&";
88 case Deref: return "*";
89 case Plus: return "+";
90 case Minus: return "-";
91 case Not: return "~";
92 case LNot: return "!";
93 case Real: return "__real";
94 case Imag: return "__imag";
95 case SizeOf: return "sizeof";
96 case AlignOf: return "alignof";
97 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000098 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000099 }
100}
101
102//===----------------------------------------------------------------------===//
103// Postfix Operators.
104//===----------------------------------------------------------------------===//
105
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000106
Chris Lattner4b009652007-07-25 00:24:17 +0000107CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
108 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000109 : Expr(CallExprClass, t), NumArgs(numargs) {
Ted Kremenek2719e982008-06-17 02:43:46 +0000110 SubExprs = new Stmt*[numargs+1];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000111 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +0000112 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +0000113 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +0000114 RParenLoc = rparenloc;
115}
116
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000117/// setNumArgs - This changes the number of arguments present in this call.
118/// Any orphaned expressions are deleted by this, and any new operands are set
119/// to null.
120void CallExpr::setNumArgs(unsigned NumArgs) {
121 // No change, just return.
122 if (NumArgs == getNumArgs()) return;
123
124 // If shrinking # arguments, just delete the extras and forgot them.
125 if (NumArgs < getNumArgs()) {
126 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
127 delete getArg(i);
128 this->NumArgs = NumArgs;
129 return;
130 }
131
132 // Otherwise, we are growing the # arguments. New an bigger argument array.
Ted Kremenek2719e982008-06-17 02:43:46 +0000133 Stmt **NewSubExprs = new Stmt*[NumArgs+1];
Chris Lattnerc257c0d2007-12-28 05:25:02 +0000134 // Copy over args.
135 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
136 NewSubExprs[i] = SubExprs[i];
137 // Null out new args.
138 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
139 NewSubExprs[i] = 0;
140
141 delete[] SubExprs;
142 SubExprs = NewSubExprs;
143 this->NumArgs = NumArgs;
144}
145
Chris Lattnerc24915f2008-10-06 05:00:53 +0000146/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
147/// not, return 0.
148unsigned CallExpr::isBuiltinCall() const {
Steve Naroff44aec4c2008-01-31 01:07:12 +0000149 // All simple function calls (e.g. func()) are implicitly cast to pointer to
150 // function. As a result, we try and obtain the DeclRefExpr from the
151 // ImplicitCastExpr.
152 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
153 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnerc24915f2008-10-06 05:00:53 +0000154 return 0;
155
Steve Naroff44aec4c2008-01-31 01:07:12 +0000156 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
157 if (!DRE)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000158 return 0;
159
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000160 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
161 if (!FDecl)
Chris Lattnerc24915f2008-10-06 05:00:53 +0000162 return 0;
163
164 return FDecl->getIdentifier()->getBuiltinID();
165}
Anders Carlsson2ed959f2008-01-31 02:13:57 +0000166
Chris Lattnerc24915f2008-10-06 05:00:53 +0000167
Chris Lattner4b009652007-07-25 00:24:17 +0000168/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
169/// corresponds to, e.g. "<<=".
170const char *BinaryOperator::getOpcodeStr(Opcode Op) {
171 switch (Op) {
172 default: assert(0 && "Unknown binary operator");
173 case Mul: return "*";
174 case Div: return "/";
175 case Rem: return "%";
176 case Add: return "+";
177 case Sub: return "-";
178 case Shl: return "<<";
179 case Shr: return ">>";
180 case LT: return "<";
181 case GT: return ">";
182 case LE: return "<=";
183 case GE: return ">=";
184 case EQ: return "==";
185 case NE: return "!=";
186 case And: return "&";
187 case Xor: return "^";
188 case Or: return "|";
189 case LAnd: return "&&";
190 case LOr: return "||";
191 case Assign: return "=";
192 case MulAssign: return "*=";
193 case DivAssign: return "/=";
194 case RemAssign: return "%=";
195 case AddAssign: return "+=";
196 case SubAssign: return "-=";
197 case ShlAssign: return "<<=";
198 case ShrAssign: return ">>=";
199 case AndAssign: return "&=";
200 case XorAssign: return "^=";
201 case OrAssign: return "|=";
202 case Comma: return ",";
203 }
204}
205
Anders Carlsson762b7c72007-08-31 04:56:16 +0000206InitListExpr::InitListExpr(SourceLocation lbraceloc,
207 Expr **initexprs, unsigned numinits,
208 SourceLocation rbraceloc)
Steve Naroff2e335472008-05-01 02:04:18 +0000209 : Expr(InitListExprClass, QualType()),
210 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc)
Anders Carlsson762b7c72007-08-31 04:56:16 +0000211{
Anders Carlsson762b7c72007-08-31 04:56:16 +0000212 for (unsigned i = 0; i != numinits; i++)
Steve Naroff2e335472008-05-01 02:04:18 +0000213 InitExprs.push_back(initexprs[i]);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000214}
Chris Lattner4b009652007-07-25 00:24:17 +0000215
Steve Naroff6f373332008-09-04 15:31:07 +0000216/// getFunctionType - Return the underlying function type for this block.
Steve Naroff52a81c02008-09-03 18:15:37 +0000217///
218const FunctionType *BlockExpr::getFunctionType() const {
219 return getType()->getAsBlockPointerType()->
220 getPointeeType()->getAsFunctionType();
221}
222
Steve Naroff9ac456d2008-10-08 17:01:13 +0000223SourceLocation BlockExpr::getCaretLocation() const {
224 return TheBlock->getCaretLocation();
225}
226const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); }
227Stmt *BlockExpr::getBody() { return TheBlock->getBody(); }
228
229
Chris Lattner4b009652007-07-25 00:24:17 +0000230//===----------------------------------------------------------------------===//
231// Generic Expression Routines
232//===----------------------------------------------------------------------===//
233
234/// hasLocalSideEffect - Return true if this immediate expression has side
235/// effects, not counting any sub-expressions.
236bool Expr::hasLocalSideEffect() const {
237 switch (getStmtClass()) {
238 default:
239 return false;
240 case ParenExprClass:
241 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
242 case UnaryOperatorClass: {
243 const UnaryOperator *UO = cast<UnaryOperator>(this);
244
245 switch (UO->getOpcode()) {
246 default: return false;
247 case UnaryOperator::PostInc:
248 case UnaryOperator::PostDec:
249 case UnaryOperator::PreInc:
250 case UnaryOperator::PreDec:
251 return true; // ++/--
252
253 case UnaryOperator::Deref:
254 // Dereferencing a volatile pointer is a side-effect.
255 return getType().isVolatileQualified();
256 case UnaryOperator::Real:
257 case UnaryOperator::Imag:
258 // accessing a piece of a volatile complex is a side-effect.
259 return UO->getSubExpr()->getType().isVolatileQualified();
260
261 case UnaryOperator::Extension:
262 return UO->getSubExpr()->hasLocalSideEffect();
263 }
264 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000265 case BinaryOperatorClass: {
266 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
267 // Consider comma to have side effects if the LHS and RHS both do.
268 if (BinOp->getOpcode() == BinaryOperator::Comma)
269 return BinOp->getLHS()->hasLocalSideEffect() &&
270 BinOp->getRHS()->hasLocalSideEffect();
271
272 return BinOp->isAssignmentOp();
273 }
Chris Lattner06078d22007-08-25 02:00:02 +0000274 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000275 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000276
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000277 case ConditionalOperatorClass: {
278 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
279 return Exp->getCond()->hasLocalSideEffect()
280 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
281 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
282 }
283
Chris Lattner4b009652007-07-25 00:24:17 +0000284 case MemberExprClass:
285 case ArraySubscriptExprClass:
286 // If the base pointer or element is to a volatile pointer/field, accessing
287 // if is a side effect.
288 return getType().isVolatileQualified();
Eli Friedman21fd0292008-05-27 15:24:04 +0000289
Chris Lattner4b009652007-07-25 00:24:17 +0000290 case CallExprClass:
291 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
292 // should warn.
293 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000294 case ObjCMessageExprClass:
295 return true;
Chris Lattner200964f2008-07-26 19:51:01 +0000296 case StmtExprClass: {
297 // Statement exprs don't logically have side effects themselves, but are
298 // sometimes used in macros in ways that give them a type that is unused.
299 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
300 // however, if the result of the stmt expr is dead, we don't want to emit a
301 // warning.
302 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
303 if (!CS->body_empty())
304 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
305 return E->hasLocalSideEffect();
306 return false;
307 }
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000308 case ExplicitCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000309 case CXXFunctionalCastExprClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000310 // If this is a cast to void, check the operand. Otherwise, the result of
311 // the cast is unused.
312 if (getType()->isVoidType())
313 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
314 return false;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000315
Eli Friedmanb924c7f2008-05-19 21:24:43 +0000316 case ImplicitCastExprClass:
317 // Check the operand, since implicit casts are inserted by Sema
318 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasLocalSideEffect();
319
Chris Lattner3e254fb2008-04-08 04:40:51 +0000320 case CXXDefaultArgExprClass:
321 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasLocalSideEffect();
Chris Lattner4b009652007-07-25 00:24:17 +0000322 }
323}
324
325/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
326/// incomplete type other than void. Nonarray expressions that can be lvalues:
327/// - name, where name must be a variable
328/// - e[i]
329/// - (e), where e must be an lvalue
330/// - e.name, where e must be an lvalue
331/// - e->name
332/// - *e, the type of e cannot be a function type
333/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000334/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000335/// - reference type [C++ [expr]]
336///
Chris Lattner25168a52008-07-26 21:30:36 +0000337Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const {
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000338 // first, check the type (C99 6.3.2.1). Expressions with function
339 // type in C are not lvalues, but they can be lvalues in C++.
340 if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType())
Chris Lattner4b009652007-07-25 00:24:17 +0000341 return LV_NotObjectType;
342
Steve Naroffec7736d2008-02-10 01:39:04 +0000343 // Allow qualified void which is an incomplete type other than void (yuck).
Chris Lattner25168a52008-07-26 21:30:36 +0000344 if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers())
Steve Naroffec7736d2008-02-10 01:39:04 +0000345 return LV_IncompleteVoidType;
346
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000347 /// FIXME: Expressions can't have reference type, so the following
348 /// isn't needed.
Chris Lattner4b009652007-07-25 00:24:17 +0000349 if (TR->isReferenceType()) // C++ [expr]
350 return LV_Valid;
351
352 // the type looks fine, now check the expression
353 switch (getStmtClass()) {
354 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000355 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000356 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
357 // For vectors, make sure base is an lvalue (i.e. not a function call).
358 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
Chris Lattner25168a52008-07-26 21:30:36 +0000359 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000360 return LV_Valid;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000361 case DeclRefExprClass: { // C99 6.5.1p2
362 const Decl *RefdDecl = cast<DeclRefExpr>(this)->getDecl();
Douglas Gregor6573cfd2008-10-21 23:43:52 +0000363 if (isa<VarDecl>(RefdDecl) ||
364 isa<ImplicitParamDecl>(RefdDecl) ||
365 // C++ 3.10p2: An lvalue refers to an object or function.
366 isa<FunctionDecl>(RefdDecl) ||
367 isa<OverloadedFunctionDecl>(RefdDecl))
Chris Lattner4b009652007-07-25 00:24:17 +0000368 return LV_Valid;
369 break;
Chris Lattner8c7c6a12008-06-17 18:05:57 +0000370 }
Steve Naroffd6163f32008-09-05 22:11:13 +0000371 case BlockDeclRefExprClass: {
372 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
Steve Naroff076d6cb2008-09-26 14:41:28 +0000373 if (isa<VarDecl>(BDR->getDecl()))
Steve Naroffd6163f32008-09-05 22:11:13 +0000374 return LV_Valid;
375 break;
376 }
Chris Lattner4b009652007-07-25 00:24:17 +0000377 case MemberExprClass: { // C99 6.5.2.3p4
378 const MemberExpr *m = cast<MemberExpr>(this);
Chris Lattner25168a52008-07-26 21:30:36 +0000379 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000380 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000381 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000382 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000383 return LV_Valid; // C99 6.5.3p4
384
385 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
Chris Lattner1b843a22008-07-25 18:07:19 +0000386 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag ||
387 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension)
Chris Lattner25168a52008-07-26 21:30:36 +0000388 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000389 break;
390 case ParenExprClass: // C99 6.5.1p5
Chris Lattner25168a52008-07-26 21:30:36 +0000391 return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx);
Steve Naroffc7c66532007-12-05 04:00:10 +0000392 case CompoundLiteralExprClass: // C99 6.5.2.5p5
393 return LV_Valid;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000394 case ExtVectorElementExprClass:
395 if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000396 return LV_DuplicateVectorComponents;
397 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000398 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
399 return LV_Valid;
Steve Naroff8fff8ce2008-05-30 23:23:16 +0000400 case ObjCPropertyRefExprClass: // FIXME: check if read-only property.
401 return LV_Valid;
Chris Lattner69909292008-08-10 01:53:14 +0000402 case PredefinedExprClass:
403 return (cast<PredefinedExpr>(this)->getIdentType()
404 == PredefinedExpr::CXXThis
Chris Lattnerc21abaf2008-07-25 23:30:42 +0000405 ? LV_InvalidExpression : LV_Valid);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000406 case CXXDefaultArgExprClass:
Chris Lattner25168a52008-07-26 21:30:36 +0000407 return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx);
Argiris Kirtzidisc821c862008-09-11 04:22:26 +0000408 case CXXConditionDeclExprClass:
409 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000410 default:
411 break;
412 }
413 return LV_InvalidExpression;
414}
415
416/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
417/// does not have an incomplete type, does not have a const-qualified type, and
418/// if it is a structure or union, does not have any member (including,
419/// recursively, any member or element of all contained aggregates or unions)
420/// with a const-qualified type.
Chris Lattner25168a52008-07-26 21:30:36 +0000421Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const {
422 isLvalueResult lvalResult = isLvalue(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000423
424 switch (lvalResult) {
425 case LV_Valid: break;
426 case LV_NotObjectType: return MLV_NotObjectType;
427 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000428 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000429 case LV_InvalidExpression: return MLV_InvalidExpression;
430 }
Chris Lattnera1923f62008-08-04 07:31:14 +0000431
432 QualType CT = Ctx.getCanonicalType(getType());
433
434 if (CT.isConstQualified())
Chris Lattner4b009652007-07-25 00:24:17 +0000435 return MLV_ConstQualified;
Chris Lattnera1923f62008-08-04 07:31:14 +0000436 if (CT->isArrayType())
Chris Lattner4b009652007-07-25 00:24:17 +0000437 return MLV_ArrayType;
Chris Lattnera1923f62008-08-04 07:31:14 +0000438 if (CT->isIncompleteType())
Chris Lattner4b009652007-07-25 00:24:17 +0000439 return MLV_IncompleteType;
440
Chris Lattnera1923f62008-08-04 07:31:14 +0000441 if (const RecordType *r = CT->getAsRecordType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000442 if (r->hasConstFields())
443 return MLV_ConstQualified;
444 }
Steve Naroff076d6cb2008-09-26 14:41:28 +0000445 // The following is illegal:
446 // void takeclosure(void (^C)(void));
447 // void func() { int x = 1; takeclosure(^{ x = 7 }); }
448 //
449 if (getStmtClass() == BlockDeclRefExprClass) {
450 const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this);
451 if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl()))
452 return MLV_NotBlockQualified;
453 }
Chris Lattner4b009652007-07-25 00:24:17 +0000454 return MLV_Valid;
455}
456
Ted Kremenek5778d622008-02-27 18:39:48 +0000457/// hasGlobalStorage - Return true if this expression has static storage
Chris Lattner743ec372007-11-27 21:35:27 +0000458/// duration. This means that the address of this expression is a link-time
459/// constant.
Ted Kremenek5778d622008-02-27 18:39:48 +0000460bool Expr::hasGlobalStorage() const {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000461 switch (getStmtClass()) {
462 default:
463 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000464 case ParenExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000465 return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage();
Chris Lattner743ec372007-11-27 21:35:27 +0000466 case ImplicitCastExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000467 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000468 case CompoundLiteralExprClass:
469 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000470 case DeclRefExprClass: {
471 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
472 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
Ted Kremenek5778d622008-02-27 18:39:48 +0000473 return VD->hasGlobalStorage();
Seo Sanghyeone330ae72008-04-04 09:45:30 +0000474 if (isa<FunctionDecl>(D))
475 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000476 return false;
477 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000478 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000479 const MemberExpr *M = cast<MemberExpr>(this);
Ted Kremenek5778d622008-02-27 18:39:48 +0000480 return !M->isArrow() && M->getBase()->hasGlobalStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000481 }
Chris Lattner743ec372007-11-27 21:35:27 +0000482 case ArraySubscriptExprClass:
Ted Kremenek5778d622008-02-27 18:39:48 +0000483 return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage();
Chris Lattner69909292008-08-10 01:53:14 +0000484 case PredefinedExprClass:
Chris Lattner7e637512008-01-12 08:14:25 +0000485 return true;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000486 case CXXDefaultArgExprClass:
487 return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000488 }
489}
490
Ted Kremenek87e30c52008-01-17 16:57:34 +0000491Expr* Expr::IgnoreParens() {
492 Expr* E = this;
493 while (ParenExpr* P = dyn_cast<ParenExpr>(E))
494 E = P->getSubExpr();
495
496 return E;
497}
498
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000499/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
500/// or CastExprs or ImplicitCastExprs, returning their operand.
501Expr *Expr::IgnoreParenCasts() {
502 Expr *E = this;
503 while (true) {
504 if (ParenExpr *P = dyn_cast<ParenExpr>(E))
505 E = P->getSubExpr();
506 else if (CastExpr *P = dyn_cast<CastExpr>(E))
507 E = P->getSubExpr();
Chris Lattner7a48d9c2008-02-13 01:02:39 +0000508 else
509 return E;
510 }
511}
512
513
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000514bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000515 switch (getStmtClass()) {
516 default:
517 if (Loc) *Loc = getLocStart();
518 return false;
519 case ParenExprClass:
520 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
521 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000522 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000523 case FloatingLiteralClass:
524 case IntegerLiteralClass:
525 case CharacterLiteralClass:
526 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000527 case TypesCompatibleExprClass:
528 case CXXBoolLiteralExprClass:
Anders Carlsson458deb72008-08-23 18:49:32 +0000529 case AddrLabelExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000530 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000531 case CallExprClass: {
532 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner2d9a3f62008-10-06 06:49:02 +0000533
534 // Allow any constant foldable calls to builtins.
535 if (CE->isBuiltinCall() && CE->isEvaluatable(Ctx))
Steve Naroff44aec4c2008-01-31 01:07:12 +0000536 return true;
Chris Lattner2d9a3f62008-10-06 06:49:02 +0000537
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000538 if (Loc) *Loc = getLocStart();
539 return false;
540 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000541 case DeclRefExprClass: {
542 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
543 // Accept address of function.
544 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000545 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000546 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000547 if (isa<VarDecl>(D))
548 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000549 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000550 }
Steve Narofff91f9722008-01-09 00:05:37 +0000551 case CompoundLiteralExprClass:
552 if (Loc) *Loc = getLocStart();
553 // Allow "(int []){2,4}", since the array will be converted to a pointer.
Nate Begemanc4e28e42008-01-25 05:34:48 +0000554 // Allow "(vector type){2,4}" since the elements are all constant.
555 return TR->isArrayType() || TR->isVectorType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000556 case UnaryOperatorClass: {
557 const UnaryOperator *Exp = cast<UnaryOperator>(this);
558
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000559 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000560 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
Ted Kremenek5778d622008-02-27 18:39:48 +0000561 if (!Exp->getSubExpr()->hasGlobalStorage()) {
Chris Lattner35b662f2007-12-11 23:11:17 +0000562 if (Loc) *Loc = getLocStart();
563 return false;
564 }
565 return true;
566 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000567
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000568 // Get the operand value. If this is sizeof/alignof, do not evalute the
569 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000570 if (!Exp->isSizeOfAlignOfOp() &&
571 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000572 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
573 return false;
574
575 switch (Exp->getOpcode()) {
576 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
577 // See C99 6.6p3.
578 default:
579 if (Loc) *Loc = Exp->getOperatorLoc();
580 return false;
581 case UnaryOperator::Extension:
582 return true; // FIXME: this is wrong.
583 case UnaryOperator::SizeOf:
584 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000585 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000586 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000587 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000588 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000589 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000590 }
Chris Lattner06db6132007-10-18 00:20:32 +0000591 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000592 case UnaryOperator::LNot:
593 case UnaryOperator::Plus:
594 case UnaryOperator::Minus:
595 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000596 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000597 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000598 }
599 case SizeOfAlignOfTypeExprClass: {
600 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
601 // alignof always evaluates to a constant.
Chris Lattner20515462008-02-21 05:45:29 +0000602 if (Exp->isSizeOf() && !Exp->getArgumentType()->isVoidType() &&
603 !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000604 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000605 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000606 }
Chris Lattner06db6132007-10-18 00:20:32 +0000607 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000608 }
609 case BinaryOperatorClass: {
610 const BinaryOperator *Exp = cast<BinaryOperator>(this);
611
612 // The LHS of a constant expr is always evaluated and needed.
613 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
614 return false;
615
616 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
617 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000618 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000619 }
620 case ImplicitCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000621 case ExplicitCastExprClass:
622 case CXXFunctionalCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000623 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
624 SourceLocation CastLoc = getLocStart();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000625 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
626 if (Loc) *Loc = SubExpr->getLocStart();
627 return false;
628 }
Chris Lattner06db6132007-10-18 00:20:32 +0000629 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000630 }
631 case ConditionalOperatorClass: {
632 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000633 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000634 // Handle the GNU extension for missing LHS.
635 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000636 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000637 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000638 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000639 }
Steve Narofff0b23542008-01-10 22:15:12 +0000640 case InitListExprClass: {
641 const InitListExpr *Exp = cast<InitListExpr>(this);
642 unsigned numInits = Exp->getNumInits();
643 for (unsigned i = 0; i < numInits; i++) {
644 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
645 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
646 return false;
647 }
648 }
649 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000650 }
Chris Lattner3e254fb2008-04-08 04:40:51 +0000651 case CXXDefaultArgExprClass:
652 return cast<CXXDefaultArgExpr>(this)->getExpr()->isConstantExpr(Ctx, Loc);
Steve Narofff0b23542008-01-10 22:15:12 +0000653 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000654}
655
Chris Lattner4b009652007-07-25 00:24:17 +0000656/// isIntegerConstantExpr - this recursive routine will test if an expression is
657/// an integer constant expression. Note: With the introduction of VLA's in
658/// C99 the result of the sizeof operator is no longer always a constant
659/// expression. The generalization of the wording to include any subexpression
660/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
661/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
Nuno Lopese1b10a12008-07-08 21:13:06 +0000662/// "0 || f()" can be treated as a constant expression. In C90 this expression,
Chris Lattner4b009652007-07-25 00:24:17 +0000663/// occurring in a context requiring a constant, would have been a constraint
664/// violation. FIXME: This routine currently implements C90 semantics.
665/// To properly implement C99 semantics this routine will need to evaluate
666/// expressions involving operators previously mentioned.
667
668/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
669/// comma, etc
670///
671/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000672/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000673///
674/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
675/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
676/// cast+dereference.
677bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
678 SourceLocation *Loc, bool isEvaluated) const {
679 switch (getStmtClass()) {
680 default:
681 if (Loc) *Loc = getLocStart();
682 return false;
683 case ParenExprClass:
684 return cast<ParenExpr>(this)->getSubExpr()->
685 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
686 case IntegerLiteralClass:
687 Result = cast<IntegerLiteral>(this)->getValue();
688 break;
689 case CharacterLiteralClass: {
690 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000691 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000692 Result = CL->getValue();
693 Result.setIsUnsigned(!getType()->isSignedIntegerType());
694 break;
695 }
Anders Carlssoned6c2142008-08-23 21:12:35 +0000696 case CXXBoolLiteralExprClass: {
697 const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this);
698 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
699 Result = BL->getValue();
700 Result.setIsUnsigned(!getType()->isSignedIntegerType());
701 break;
702 }
Argiris Kirtzidis750eb972008-08-23 19:35:47 +0000703 case CXXZeroInitValueExprClass:
704 Result.clear();
705 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000706 case TypesCompatibleExprClass: {
707 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000708 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000709 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000710 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000711 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000712 case CallExprClass: {
713 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner8cd0e932008-03-05 18:54:05 +0000714 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner1eee9402008-10-06 06:40:35 +0000715
716 // If this is a call to a builtin function, constant fold it otherwise
717 // reject it.
718 if (CE->isBuiltinCall()) {
719 APValue ResultAP;
720 if (CE->tryEvaluate(ResultAP, Ctx)) {
721 Result = ResultAP.getInt();
722 break; // It is a constant, expand it.
723 }
724 }
725
Steve Naroff8d3b1702007-08-08 22:15:55 +0000726 if (Loc) *Loc = getLocStart();
727 return false;
728 }
Chris Lattner4b009652007-07-25 00:24:17 +0000729 case DeclRefExprClass:
730 if (const EnumConstantDecl *D =
731 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
732 Result = D->getInitVal();
733 break;
734 }
735 if (Loc) *Loc = getLocStart();
736 return false;
737 case UnaryOperatorClass: {
738 const UnaryOperator *Exp = cast<UnaryOperator>(this);
739
740 // Get the operand value. If this is sizeof/alignof, do not evalute the
741 // operand. This affects C99 6.6p3.
Anders Carlsson52774ad2008-01-29 15:56:48 +0000742 if (!Exp->isSizeOfAlignOfOp() && !Exp->isOffsetOfOp() &&
Chris Lattner5a9b6242007-08-23 21:42:50 +0000743 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000744 return false;
745
746 switch (Exp->getOpcode()) {
747 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
748 // See C99 6.6p3.
749 default:
750 if (Loc) *Loc = Exp->getOperatorLoc();
751 return false;
752 case UnaryOperator::Extension:
753 return true; // FIXME: this is wrong.
754 case UnaryOperator::SizeOf:
755 case UnaryOperator::AlignOf:
Chris Lattner20515462008-02-21 05:45:29 +0000756 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000757 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000758
759 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
760 if (Exp->getSubExpr()->getType()->isVoidType()) {
761 Result = 1;
762 break;
763 }
764
Chris Lattner4b009652007-07-25 00:24:17 +0000765 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000766 if (!Exp->getSubExpr()->getType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000767 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000768 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000769 }
Chris Lattner4b009652007-07-25 00:24:17 +0000770
Chris Lattner4b009652007-07-25 00:24:17 +0000771 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000772 if (Exp->getSubExpr()->getType()->isFunctionType()) {
773 // GCC extension: sizeof(function) = 1.
774 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000775 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000776 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson1f86b032008-02-18 07:10:45 +0000777 if (Exp->getOpcode() == UnaryOperator::AlignOf)
Chris Lattner8cd0e932008-03-05 18:54:05 +0000778 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType()) / CharSize;
Anders Carlsson1f86b032008-02-18 07:10:45 +0000779 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000780 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType()) / CharSize;
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000781 }
Chris Lattner4b009652007-07-25 00:24:17 +0000782 break;
783 case UnaryOperator::LNot: {
Chris Lattnerf00fdc02008-01-25 19:16:19 +0000784 bool Val = Result == 0;
Chris Lattner8cd0e932008-03-05 18:54:05 +0000785 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner4b009652007-07-25 00:24:17 +0000786 Result = Val;
787 break;
788 }
789 case UnaryOperator::Plus:
790 break;
791 case UnaryOperator::Minus:
792 Result = -Result;
793 break;
794 case UnaryOperator::Not:
795 Result = ~Result;
796 break;
Anders Carlsson52774ad2008-01-29 15:56:48 +0000797 case UnaryOperator::OffsetOf:
Daniel Dunbar461d08c2008-08-28 18:42:20 +0000798 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Anders Carlsson52774ad2008-01-29 15:56:48 +0000799 Result = Exp->evaluateOffsetOf(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000800 }
801 break;
802 }
803 case SizeOfAlignOfTypeExprClass: {
804 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
Chris Lattner20515462008-02-21 05:45:29 +0000805
806 // Return the result in the right width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000807 Result.zextOrTrunc(static_cast<uint32_t>(Ctx.getTypeSize(getType())));
Chris Lattner20515462008-02-21 05:45:29 +0000808
809 // sizeof(void) and __alignof__(void) = 1 as a gcc extension.
810 if (Exp->getArgumentType()->isVoidType()) {
811 Result = 1;
812 break;
813 }
814
815 // alignof always evaluates to a constant, sizeof does if arg is not VLA.
Eli Friedman62f67fd2008-02-15 12:20:59 +0000816 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType()) {
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000817 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000818 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000819 }
Chris Lattner4b009652007-07-25 00:24:17 +0000820
Chris Lattner4b009652007-07-25 00:24:17 +0000821 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000822 if (Exp->getArgumentType()->isFunctionType()) {
823 // GCC extension: sizeof(function) = 1.
824 Result = Exp->isSizeOf() ? 1 : 4;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000825 } else {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000826 unsigned CharSize = Ctx.Target.getCharWidth();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000827 if (Exp->isSizeOf())
Chris Lattner8cd0e932008-03-05 18:54:05 +0000828 Result = Ctx.getTypeSize(Exp->getArgumentType()) / CharSize;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000829 else
Chris Lattner8cd0e932008-03-05 18:54:05 +0000830 Result = Ctx.getTypeAlign(Exp->getArgumentType()) / CharSize;
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000831 }
Chris Lattner4b009652007-07-25 00:24:17 +0000832 break;
833 }
834 case BinaryOperatorClass: {
835 const BinaryOperator *Exp = cast<BinaryOperator>(this);
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000836 llvm::APSInt LHS, RHS;
837
838 // Initialize result to have correct signedness and width.
839 Result = llvm::APSInt(static_cast<uint32_t>(Ctx.getTypeSize(getType())),
840 !getType()->isSignedIntegerType());
Chris Lattner4b009652007-07-25 00:24:17 +0000841
842 // The LHS of a constant expr is always evaluated and needed.
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000843 if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000844 return false;
845
Chris Lattner4b009652007-07-25 00:24:17 +0000846 // The short-circuiting &&/|| operators don't necessarily evaluate their
847 // RHS. Make sure to pass isEvaluated down correctly.
848 if (Exp->isLogicalOp()) {
849 bool RHSEval;
850 if (Exp->getOpcode() == BinaryOperator::LAnd)
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000851 RHSEval = LHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000852 else {
853 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000854 RHSEval = LHS == 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000855 }
856
857 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
858 isEvaluated & RHSEval))
859 return false;
860 } else {
861 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
862 return false;
863 }
864
865 switch (Exp->getOpcode()) {
866 default:
867 if (Loc) *Loc = getLocStart();
868 return false;
869 case BinaryOperator::Mul:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000870 Result = LHS * RHS;
Chris Lattner4b009652007-07-25 00:24:17 +0000871 break;
872 case BinaryOperator::Div:
873 if (RHS == 0) {
874 if (!isEvaluated) break;
875 if (Loc) *Loc = getLocStart();
876 return false;
877 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000878 Result = LHS / RHS;
Chris Lattner4b009652007-07-25 00:24:17 +0000879 break;
880 case BinaryOperator::Rem:
881 if (RHS == 0) {
882 if (!isEvaluated) break;
883 if (Loc) *Loc = getLocStart();
884 return false;
885 }
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000886 Result = LHS % RHS;
Chris Lattner4b009652007-07-25 00:24:17 +0000887 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000888 case BinaryOperator::Add: Result = LHS + RHS; break;
889 case BinaryOperator::Sub: Result = LHS - RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000890 case BinaryOperator::Shl:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000891 Result = LHS <<
892 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
893 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000894 case BinaryOperator::Shr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000895 Result = LHS >>
896 static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000897 break;
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000898 case BinaryOperator::LT: Result = LHS < RHS; break;
899 case BinaryOperator::GT: Result = LHS > RHS; break;
900 case BinaryOperator::LE: Result = LHS <= RHS; break;
901 case BinaryOperator::GE: Result = LHS >= RHS; break;
902 case BinaryOperator::EQ: Result = LHS == RHS; break;
903 case BinaryOperator::NE: Result = LHS != RHS; break;
904 case BinaryOperator::And: Result = LHS & RHS; break;
905 case BinaryOperator::Xor: Result = LHS ^ RHS; break;
906 case BinaryOperator::Or: Result = LHS | RHS; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000907 case BinaryOperator::LAnd:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000908 Result = LHS != 0 && RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000909 break;
910 case BinaryOperator::LOr:
Daniel Dunbarf173b2c2008-09-22 23:53:24 +0000911 Result = LHS != 0 || RHS != 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000912 break;
913
914 case BinaryOperator::Comma:
915 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
916 // *except* when they are contained within a subexpression that is not
917 // evaluated". Note that Assignment can never happen due to constraints
918 // on the LHS subexpr, so we don't need to check it here.
919 if (isEvaluated) {
920 if (Loc) *Loc = getLocStart();
921 return false;
922 }
923
924 // The result of the constant expr is the RHS.
925 Result = RHS;
926 return true;
927 }
928
929 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
930 break;
931 }
932 case ImplicitCastExprClass:
Argiris Kirtzidis7a1e7412008-08-22 15:38:55 +0000933 case ExplicitCastExprClass:
934 case CXXFunctionalCastExprClass: {
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +0000935 const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr();
936 SourceLocation CastLoc = getLocStart();
Chris Lattner4b009652007-07-25 00:24:17 +0000937
938 // C99 6.6p6: shall only convert arithmetic types to integer types.
939 if (!SubExpr->getType()->isArithmeticType() ||
940 !getType()->isIntegerType()) {
941 if (Loc) *Loc = SubExpr->getLocStart();
942 return false;
943 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000944
Chris Lattner8cd0e932008-03-05 18:54:05 +0000945 uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType()));
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000946
Chris Lattner4b009652007-07-25 00:24:17 +0000947 // Handle simple integer->integer casts.
948 if (SubExpr->getType()->isIntegerType()) {
949 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
950 return false;
951
952 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000953 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000954 if (getType()->isBooleanType()) {
955 // Conversion to bool compares against zero.
956 Result = Result != 0;
957 Result.zextOrTrunc(DestWidth);
958 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000959 Result.sextOrTrunc(DestWidth);
960 else // If the input is unsigned, do a zero extend, noop, or truncate.
961 Result.zextOrTrunc(DestWidth);
962 break;
963 }
964
965 // Allow floating constants that are the immediate operands of casts or that
966 // are parenthesized.
967 const Expr *Operand = SubExpr;
968 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
969 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000970
971 // If this isn't a floating literal, we can't handle it.
972 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
973 if (!FL) {
974 if (Loc) *Loc = Operand->getLocStart();
975 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000976 }
Chris Lattner000c4102008-01-09 18:59:34 +0000977
978 // If the destination is boolean, compare against zero.
979 if (getType()->isBooleanType()) {
980 Result = !FL->getValue().isZero();
981 Result.zextOrTrunc(DestWidth);
982 break;
983 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000984
985 // Determine whether we are converting to unsigned or signed.
986 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000987
988 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
989 // be called multiple times per AST.
Dale Johannesen2461f612008-10-09 23:02:32 +0000990 uint64_t Space[4];
991 bool ignored;
Chris Lattner9d020b32007-09-26 00:47:26 +0000992 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
Dale Johannesen2461f612008-10-09 23:02:32 +0000993 llvm::APFloat::rmTowardZero,
994 &ignored);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000995 Result = llvm::APInt(DestWidth, 4, Space);
996 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000997 }
998 case ConditionalOperatorClass: {
999 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
1000
1001 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
1002 return false;
1003
1004 const Expr *TrueExp = Exp->getLHS();
1005 const Expr *FalseExp = Exp->getRHS();
1006 if (Result == 0) std::swap(TrueExp, FalseExp);
1007
1008 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001009 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +00001010 return false;
1011 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +00001012 if (TrueExp &&
1013 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +00001014 return false;
1015 break;
1016 }
Chris Lattner3e254fb2008-04-08 04:40:51 +00001017 case CXXDefaultArgExprClass:
1018 return cast<CXXDefaultArgExpr>(this)
1019 ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Chris Lattner4b009652007-07-25 00:24:17 +00001020 }
1021
1022 // Cases that are valid constant exprs fall through to here.
1023 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
1024 return true;
1025}
1026
Chris Lattner4b009652007-07-25 00:24:17 +00001027/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
1028/// integer constant expression with the value zero, or if this is one that is
1029/// cast to void*.
1030bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +00001031 // Strip off a cast to void*, if it exists.
Argiris Kirtzidisc45e2fb2008-08-18 23:01:59 +00001032 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Steve Naroffa2e53222008-01-14 16:10:57 +00001033 // Check that it is a cast to void*.
Eli Friedmand899dbe2008-02-13 17:29:58 +00001034 if (const PointerType *PT = CE->getType()->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001035 QualType Pointee = PT->getPointeeType();
Chris Lattner35fef522008-02-20 20:55:12 +00001036 if (Pointee.getCVRQualifiers() == 0 &&
1037 Pointee->isVoidType() && // to void*
Steve Naroffa2e53222008-01-14 16:10:57 +00001038 CE->getSubExpr()->getType()->isIntegerType()) // from int.
1039 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +00001040 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001041 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
1042 // Ignore the ImplicitCastExpr type entirely.
1043 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
1044 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
1045 // Accept ((void*)0) as a null pointer constant, as many other
1046 // implementations do.
1047 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner97316c02008-04-10 02:22:51 +00001048 } else if (const CXXDefaultArgExpr *DefaultArg
1049 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner3e254fb2008-04-08 04:40:51 +00001050 // See through default argument expressions
1051 return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +00001052 }
Steve Naroffa2e53222008-01-14 16:10:57 +00001053
1054 // This expression must be an integer type.
1055 if (!getType()->isIntegerType())
1056 return false;
1057
Chris Lattner4b009652007-07-25 00:24:17 +00001058 // If we have an integer constant expression, we need to *evaluate* it and
1059 // test for the value 0.
1060 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +00001061 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001062}
Steve Naroffc11705f2007-07-28 23:10:27 +00001063
Nate Begemanaf6ed502008-04-18 23:10:10 +00001064unsigned ExtVectorElementExpr::getNumElements() const {
Nate Begemanc8e51f82008-05-09 06:41:27 +00001065 if (const VectorType *VT = getType()->getAsVectorType())
1066 return VT->getNumElements();
1067 return 1;
Chris Lattner50547852007-08-03 16:00:20 +00001068}
1069
Nate Begemanc8e51f82008-05-09 06:41:27 +00001070/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001071bool ExtVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +00001072 const char *compStr = Accessor.getName();
1073 unsigned length = strlen(compStr);
1074
1075 for (unsigned i = 0; i < length-1; i++) {
1076 const char *s = compStr+i;
1077 for (const char c = *s++; *s; s++)
1078 if (c == *s)
1079 return true;
1080 }
1081 return false;
1082}
Chris Lattner42158e72007-08-02 23:36:59 +00001083
Nate Begemanc8e51f82008-05-09 06:41:27 +00001084/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemana1ae7442008-05-13 21:03:02 +00001085void ExtVectorElementExpr::getEncodedElementAccess(
1086 llvm::SmallVectorImpl<unsigned> &Elts) const {
Chris Lattner42158e72007-08-02 23:36:59 +00001087 const char *compStr = Accessor.getName();
Nate Begemanc8e51f82008-05-09 06:41:27 +00001088
1089 bool isHi = !strcmp(compStr, "hi");
1090 bool isLo = !strcmp(compStr, "lo");
1091 bool isEven = !strcmp(compStr, "e");
1092 bool isOdd = !strcmp(compStr, "o");
1093
1094 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
1095 uint64_t Index;
1096
1097 if (isHi)
1098 Index = e + i;
1099 else if (isLo)
1100 Index = i;
1101 else if (isEven)
1102 Index = 2 * i;
1103 else if (isOdd)
1104 Index = 2 * i + 1;
1105 else
1106 Index = ExtVectorType::getAccessorIdx(compStr[i]);
Chris Lattner42158e72007-08-02 23:36:59 +00001107
Nate Begemana1ae7442008-05-13 21:03:02 +00001108 Elts.push_back(Index);
Chris Lattner42158e72007-08-02 23:36:59 +00001109 }
Nate Begemanc8e51f82008-05-09 06:41:27 +00001110}
1111
Steve Naroff4ed9d662007-09-27 14:38:14 +00001112// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001113ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001114 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001115 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001116 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001117 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001118 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001119 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001120 SubExprs = new Stmt*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001121 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001122 if (NumArgs) {
1123 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001124 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1125 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001126 LBracloc = LBrac;
1127 RBracloc = RBrac;
1128}
1129
Steve Naroff4ed9d662007-09-27 14:38:14 +00001130// constructor for class messages.
1131// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001132ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001133 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001134 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001135 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001136 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001137 MethodProto(mproto) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001138 NumArgs = nargs;
Ted Kremenek2719e982008-06-17 02:43:46 +00001139 SubExprs = new Stmt*[NumArgs+1];
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001140 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown);
Steve Naroff9f176d12007-11-15 13:05:42 +00001141 if (NumArgs) {
1142 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001143 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1144 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001145 LBracloc = LBrac;
1146 RBracloc = RBrac;
1147}
1148
Ted Kremenekee2c9fd2008-06-24 15:50:53 +00001149// constructor for class messages.
1150ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo,
1151 QualType retType, ObjCMethodDecl *mproto,
1152 SourceLocation LBrac, SourceLocation RBrac,
1153 Expr **ArgExprs, unsigned nargs)
1154: Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1155MethodProto(mproto) {
1156 NumArgs = nargs;
1157 SubExprs = new Stmt*[NumArgs+1];
1158 SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown);
1159 if (NumArgs) {
1160 for (unsigned i = 0; i != NumArgs; ++i)
1161 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1162 }
1163 LBracloc = LBrac;
1164 RBracloc = RBrac;
1165}
1166
1167ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const {
1168 uintptr_t x = (uintptr_t) SubExprs[RECEIVER];
1169 switch (x & Flags) {
1170 default:
1171 assert(false && "Invalid ObjCMessageExpr.");
1172 case IsInstMeth:
1173 return ClassInfo(0, 0);
1174 case IsClsMethDeclUnknown:
1175 return ClassInfo(0, (IdentifierInfo*) (x & ~Flags));
1176 case IsClsMethDeclKnown: {
1177 ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags);
1178 return ClassInfo(D, D->getIdentifier());
1179 }
1180 }
1181}
1182
Chris Lattnerf624cd22007-10-25 00:29:32 +00001183bool ChooseExpr::isConditionTrue(ASTContext &C) const {
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001184 return getCond()->getIntegerConstantExprValue(C) != 0;
Chris Lattnerf624cd22007-10-25 00:29:32 +00001185}
1186
Anders Carlsson52774ad2008-01-29 15:56:48 +00001187static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E)
1188{
1189 if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
1190 QualType Ty = ME->getBase()->getType();
1191
1192 RecordDecl *RD = Ty->getAsRecordType()->getDecl();
Chris Lattner8cd0e932008-03-05 18:54:05 +00001193 const ASTRecordLayout &RL = C.getASTRecordLayout(RD);
Anders Carlsson52774ad2008-01-29 15:56:48 +00001194 FieldDecl *FD = ME->getMemberDecl();
1195
1196 // FIXME: This is linear time.
1197 unsigned i = 0, e = 0;
1198 for (i = 0, e = RD->getNumMembers(); i != e; i++) {
1199 if (RD->getMember(i) == FD)
1200 break;
1201 }
1202
1203 return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase());
1204 } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
1205 const Expr *Base = ASE->getBase();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001206
Chris Lattner8cd0e932008-03-05 18:54:05 +00001207 int64_t size = C.getTypeSize(ASE->getType());
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +00001208 size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue();
Anders Carlsson52774ad2008-01-29 15:56:48 +00001209
1210 return size + evaluateOffsetOf(C, Base);
1211 } else if (isa<CompoundLiteralExpr>(E))
1212 return 0;
1213
1214 assert(0 && "Unknown offsetof subexpression!");
1215 return 0;
1216}
1217
1218int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const
1219{
1220 assert(Opc == OffsetOf && "Unary operator not offsetof!");
1221
Chris Lattner8cd0e932008-03-05 18:54:05 +00001222 unsigned CharSize = C.Target.getCharWidth();
Ted Kremenek2719e982008-06-17 02:43:46 +00001223 return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize;
Anders Carlsson52774ad2008-01-29 15:56:48 +00001224}
1225
Daniel Dunbar7cfb85b2008-08-28 18:02:04 +00001226void SizeOfAlignOfTypeExpr::Destroy(ASTContext& C) {
1227 // Override default behavior of traversing children. We do not want
1228 // to delete the type.
1229}
1230
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001231//===----------------------------------------------------------------------===//
1232// Child Iterators for iterating over subexpressions/substatements
1233//===----------------------------------------------------------------------===//
1234
1235// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001236Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1237Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001238
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001239// ObjCIvarRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001240Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; }
1241Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; }
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001242
Steve Naroff6f786252008-06-02 23:03:37 +00001243// ObjCPropertyRefExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001244Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; }
1245Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; }
Steve Naroff05391d22008-05-30 00:40:33 +00001246
Chris Lattner69909292008-08-10 01:53:14 +00001247// PredefinedExpr
1248Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); }
1249Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001250
1251// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001252Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1253Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001254
1255// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001256Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1257Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001258
1259// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001260Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1261Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001262
Chris Lattner1de66eb2007-08-26 03:42:43 +00001263// ImaginaryLiteral
Ted Kremenek2719e982008-06-17 02:43:46 +00001264Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; }
1265Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; }
Chris Lattner1de66eb2007-08-26 03:42:43 +00001266
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001267// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001268Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1269Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001270
1271// ParenExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001272Stmt::child_iterator ParenExpr::child_begin() { return &Val; }
1273Stmt::child_iterator ParenExpr::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001274
1275// UnaryOperator
Ted Kremenek2719e982008-06-17 02:43:46 +00001276Stmt::child_iterator UnaryOperator::child_begin() { return &Val; }
1277Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001278
1279// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001280Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001281 // If the type is a VLA type (and not a typedef), the size expression of the
1282 // VLA needs to be treated as an executable expression.
1283 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1284 return child_iterator(T);
1285 else
Ted Kremenekc9e6d5c2008-10-07 23:35:42 +00001286 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001287}
1288Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekc9e6d5c2008-10-07 23:35:42 +00001289 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001290}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001291
1292// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001293Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001294 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001295}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001296Stmt::child_iterator ArraySubscriptExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001297 return &SubExprs[0]+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001298}
1299
1300// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001301Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001302 return &SubExprs[0];
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001303}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001304Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001305 return &SubExprs[0]+NumArgs+ARGS_START;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001306}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001307
1308// MemberExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001309Stmt::child_iterator MemberExpr::child_begin() { return &Base; }
1310Stmt::child_iterator MemberExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001311
Nate Begemanaf6ed502008-04-18 23:10:10 +00001312// ExtVectorElementExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001313Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; }
1314Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001315
1316// CompoundLiteralExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001317Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; }
1318Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001319
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001320// CastExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001321Stmt::child_iterator CastExpr::child_begin() { return &Op; }
1322Stmt::child_iterator CastExpr::child_end() { return &Op+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001323
1324// BinaryOperator
1325Stmt::child_iterator BinaryOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001326 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001327}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001328Stmt::child_iterator BinaryOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001329 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001330}
1331
1332// ConditionalOperator
1333Stmt::child_iterator ConditionalOperator::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001334 return &SubExprs[0];
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001335}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001336Stmt::child_iterator ConditionalOperator::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001337 return &SubExprs[0]+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001338}
1339
1340// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001341Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1342Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001343
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001344// StmtExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001345Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; }
1346Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001347
1348// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001349Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1350 return child_iterator();
1351}
1352
1353Stmt::child_iterator TypesCompatibleExpr::child_end() {
1354 return child_iterator();
1355}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001356
1357// ChooseExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001358Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; }
1359Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001360
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001361// OverloadExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001362Stmt::child_iterator OverloadExpr::child_begin() { return &SubExprs[0]; }
1363Stmt::child_iterator OverloadExpr::child_end() { return &SubExprs[0]+NumExprs; }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00001364
Eli Friedmand0e9d092008-05-14 19:38:39 +00001365// ShuffleVectorExpr
1366Stmt::child_iterator ShuffleVectorExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001367 return &SubExprs[0];
Eli Friedmand0e9d092008-05-14 19:38:39 +00001368}
1369Stmt::child_iterator ShuffleVectorExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001370 return &SubExprs[0]+NumExprs;
Eli Friedmand0e9d092008-05-14 19:38:39 +00001371}
1372
Anders Carlsson36760332007-10-15 20:28:48 +00001373// VAArgExpr
Ted Kremenek2719e982008-06-17 02:43:46 +00001374Stmt::child_iterator VAArgExpr::child_begin() { return &Val; }
1375Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; }
Anders Carlsson36760332007-10-15 20:28:48 +00001376
Anders Carlsson762b7c72007-08-31 04:56:16 +00001377// InitListExpr
1378Stmt::child_iterator InitListExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001379 return InitExprs.size() ? &InitExprs[0] : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001380}
1381Stmt::child_iterator InitListExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001382 return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0;
Anders Carlsson762b7c72007-08-31 04:56:16 +00001383}
1384
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001385// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001386Stmt::child_iterator ObjCStringLiteral::child_begin() {
1387 return child_iterator();
1388}
1389Stmt::child_iterator ObjCStringLiteral::child_end() {
1390 return child_iterator();
1391}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001392
1393// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001394Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1395Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001396
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001397// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001398Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1399 return child_iterator();
1400}
1401Stmt::child_iterator ObjCSelectorExpr::child_end() {
1402 return child_iterator();
1403}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001404
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001405// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001406Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1407 return child_iterator();
1408}
1409Stmt::child_iterator ObjCProtocolExpr::child_end() {
1410 return child_iterator();
1411}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001412
Steve Naroffc39ca262007-09-18 23:55:05 +00001413// ObjCMessageExpr
Ted Kremenekd029a6f2008-05-01 17:26:20 +00001414Stmt::child_iterator ObjCMessageExpr::child_begin() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001415 return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START;
Steve Naroffc39ca262007-09-18 23:55:05 +00001416}
1417Stmt::child_iterator ObjCMessageExpr::child_end() {
Ted Kremenek2719e982008-06-17 02:43:46 +00001418 return &SubExprs[0]+ARGS_START+getNumArgs();
Steve Naroffc39ca262007-09-18 23:55:05 +00001419}
1420
Steve Naroff52a81c02008-09-03 18:15:37 +00001421// Blocks
Steve Naroff9ac456d2008-10-08 17:01:13 +00001422Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); }
1423Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); }
Steve Naroff52a81c02008-09-03 18:15:37 +00001424
Ted Kremenek4a1f5de2008-09-26 23:24:14 +00001425Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();}
1426Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); }