blob: e2f6c6210b1e0c6ca29198352aa82bfd1685693a [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
14#include "clang/AST/Expr.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/StmtVisitor.h"
Chris Lattner2fd1c652007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattnerd9ffbc92007-11-27 18:22:04 +000018#include "clang/Basic/TargetInfo.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// Primary Expressions.
23//===----------------------------------------------------------------------===//
24
25StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
26 bool Wide, QualType t, SourceLocation firstLoc,
27 SourceLocation lastLoc) :
28 Expr(StringLiteralClass, t) {
29 // OPTIMIZE: could allocate this appended to the StringLiteral.
30 char *AStrData = new char[byteLength];
31 memcpy(AStrData, strData, byteLength);
32 StrData = AStrData;
33 ByteLength = byteLength;
34 IsWide = Wide;
35 firstTokLoc = firstLoc;
36 lastTokLoc = lastLoc;
37}
38
39StringLiteral::~StringLiteral() {
40 delete[] StrData;
41}
42
43bool UnaryOperator::isPostfix(Opcode Op) {
44 switch (Op) {
45 case PostInc:
46 case PostDec:
47 return true;
48 default:
49 return false;
50 }
51}
52
53/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
54/// corresponds to, e.g. "sizeof" or "[pre]++".
55const char *UnaryOperator::getOpcodeStr(Opcode Op) {
56 switch (Op) {
57 default: assert(0 && "Unknown unary operator");
58 case PostInc: return "++";
59 case PostDec: return "--";
60 case PreInc: return "++";
61 case PreDec: return "--";
62 case AddrOf: return "&";
63 case Deref: return "*";
64 case Plus: return "+";
65 case Minus: return "-";
66 case Not: return "~";
67 case LNot: return "!";
68 case Real: return "__real";
69 case Imag: return "__imag";
70 case SizeOf: return "sizeof";
71 case AlignOf: return "alignof";
72 case Extension: return "__extension__";
Chris Lattner0d9bcea2007-08-30 17:45:32 +000073 case OffsetOf: return "__builtin_offsetof";
Chris Lattner4b009652007-07-25 00:24:17 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
78// Postfix Operators.
79//===----------------------------------------------------------------------===//
80
81CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
82 SourceLocation rparenloc)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000083 : Expr(CallExprClass, t), NumArgs(numargs) {
84 SubExprs = new Expr*[numargs+1];
85 SubExprs[FN] = fn;
Chris Lattner4b009652007-07-25 00:24:17 +000086 for (unsigned i = 0; i != numargs; ++i)
Ted Kremeneke4acb9c2007-08-24 18:13:47 +000087 SubExprs[i+ARGS_START] = args[i];
Chris Lattner4b009652007-07-25 00:24:17 +000088 RParenLoc = rparenloc;
89}
90
Chris Lattnerc257c0d2007-12-28 05:25:02 +000091/// setNumArgs - This changes the number of arguments present in this call.
92/// Any orphaned expressions are deleted by this, and any new operands are set
93/// to null.
94void CallExpr::setNumArgs(unsigned NumArgs) {
95 // No change, just return.
96 if (NumArgs == getNumArgs()) return;
97
98 // If shrinking # arguments, just delete the extras and forgot them.
99 if (NumArgs < getNumArgs()) {
100 for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i)
101 delete getArg(i);
102 this->NumArgs = NumArgs;
103 return;
104 }
105
106 // Otherwise, we are growing the # arguments. New an bigger argument array.
107 Expr **NewSubExprs = new Expr*[NumArgs+1];
108 // Copy over args.
109 for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i)
110 NewSubExprs[i] = SubExprs[i];
111 // Null out new args.
112 for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i)
113 NewSubExprs[i] = 0;
114
115 delete[] SubExprs;
116 SubExprs = NewSubExprs;
117 this->NumArgs = NumArgs;
118}
119
120
Steve Naroff8d3b1702007-08-08 22:15:55 +0000121bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
122 // The following enum mimics gcc's internal "typeclass.h" file.
123 enum gcc_type_class {
124 no_type_class = -1,
125 void_type_class, integer_type_class, char_type_class,
126 enumeral_type_class, boolean_type_class,
127 pointer_type_class, reference_type_class, offset_type_class,
128 real_type_class, complex_type_class,
129 function_type_class, method_type_class,
130 record_type_class, union_type_class,
131 array_type_class, string_type_class,
132 lang_type_class
133 };
134 Result.setIsSigned(true);
135
136 // All simple function calls (e.g. func()) are implicitly cast to pointer to
137 // function. As a result, we try and obtain the DeclRefExpr from the
138 // ImplicitCastExpr.
139 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
140 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
141 return false;
142 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
143 if (!DRE)
144 return false;
145
146 // We have a DeclRefExpr.
147 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
148 // If no argument was supplied, default to "no_type_class". This isn't
149 // ideal, however it's what gcc does.
150 Result = static_cast<uint64_t>(no_type_class);
151 if (NumArgs >= 1) {
152 QualType argType = getArg(0)->getType();
153
154 if (argType->isVoidType())
155 Result = void_type_class;
156 else if (argType->isEnumeralType())
157 Result = enumeral_type_class;
158 else if (argType->isBooleanType())
159 Result = boolean_type_class;
160 else if (argType->isCharType())
161 Result = string_type_class; // gcc doesn't appear to use char_type_class
162 else if (argType->isIntegerType())
163 Result = integer_type_class;
164 else if (argType->isPointerType())
165 Result = pointer_type_class;
166 else if (argType->isReferenceType())
167 Result = reference_type_class;
168 else if (argType->isRealType())
169 Result = real_type_class;
170 else if (argType->isComplexType())
171 Result = complex_type_class;
172 else if (argType->isFunctionType())
173 Result = function_type_class;
174 else if (argType->isStructureType())
175 Result = record_type_class;
176 else if (argType->isUnionType())
177 Result = union_type_class;
178 else if (argType->isArrayType())
179 Result = array_type_class;
180 else if (argType->isUnionType())
181 Result = union_type_class;
182 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner19b8f1a2007-11-08 17:56:40 +0000183 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff8d3b1702007-08-08 22:15:55 +0000184 }
185 return true;
186 }
187 return false;
188}
189
Chris Lattner4b009652007-07-25 00:24:17 +0000190/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
191/// corresponds to, e.g. "<<=".
192const char *BinaryOperator::getOpcodeStr(Opcode Op) {
193 switch (Op) {
194 default: assert(0 && "Unknown binary operator");
195 case Mul: return "*";
196 case Div: return "/";
197 case Rem: return "%";
198 case Add: return "+";
199 case Sub: return "-";
200 case Shl: return "<<";
201 case Shr: return ">>";
202 case LT: return "<";
203 case GT: return ">";
204 case LE: return "<=";
205 case GE: return ">=";
206 case EQ: return "==";
207 case NE: return "!=";
208 case And: return "&";
209 case Xor: return "^";
210 case Or: return "|";
211 case LAnd: return "&&";
212 case LOr: return "||";
213 case Assign: return "=";
214 case MulAssign: return "*=";
215 case DivAssign: return "/=";
216 case RemAssign: return "%=";
217 case AddAssign: return "+=";
218 case SubAssign: return "-=";
219 case ShlAssign: return "<<=";
220 case ShrAssign: return ">>=";
221 case AndAssign: return "&=";
222 case XorAssign: return "^=";
223 case OrAssign: return "|=";
224 case Comma: return ",";
225 }
226}
227
Anders Carlsson762b7c72007-08-31 04:56:16 +0000228InitListExpr::InitListExpr(SourceLocation lbraceloc,
229 Expr **initexprs, unsigned numinits,
230 SourceLocation rbraceloc)
231 : Expr(InitListExprClass, QualType())
232 , NumInits(numinits)
233 , LBraceLoc(lbraceloc)
234 , RBraceLoc(rbraceloc)
235{
236 InitExprs = new Expr*[numinits];
237 for (unsigned i = 0; i != numinits; i++)
238 InitExprs[i] = initexprs[i];
239}
Chris Lattner4b009652007-07-25 00:24:17 +0000240
241//===----------------------------------------------------------------------===//
242// Generic Expression Routines
243//===----------------------------------------------------------------------===//
244
245/// hasLocalSideEffect - Return true if this immediate expression has side
246/// effects, not counting any sub-expressions.
247bool Expr::hasLocalSideEffect() const {
248 switch (getStmtClass()) {
249 default:
250 return false;
251 case ParenExprClass:
252 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
253 case UnaryOperatorClass: {
254 const UnaryOperator *UO = cast<UnaryOperator>(this);
255
256 switch (UO->getOpcode()) {
257 default: return false;
258 case UnaryOperator::PostInc:
259 case UnaryOperator::PostDec:
260 case UnaryOperator::PreInc:
261 case UnaryOperator::PreDec:
262 return true; // ++/--
263
264 case UnaryOperator::Deref:
265 // Dereferencing a volatile pointer is a side-effect.
266 return getType().isVolatileQualified();
267 case UnaryOperator::Real:
268 case UnaryOperator::Imag:
269 // accessing a piece of a volatile complex is a side-effect.
270 return UO->getSubExpr()->getType().isVolatileQualified();
271
272 case UnaryOperator::Extension:
273 return UO->getSubExpr()->hasLocalSideEffect();
274 }
275 }
Chris Lattneref95ffd2007-12-01 06:07:34 +0000276 case BinaryOperatorClass: {
277 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
278 // Consider comma to have side effects if the LHS and RHS both do.
279 if (BinOp->getOpcode() == BinaryOperator::Comma)
280 return BinOp->getLHS()->hasLocalSideEffect() &&
281 BinOp->getRHS()->hasLocalSideEffect();
282
283 return BinOp->isAssignmentOp();
284 }
Chris Lattner06078d22007-08-25 02:00:02 +0000285 case CompoundAssignOperatorClass:
Chris Lattner9c0da3b2007-08-25 01:55:00 +0000286 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000287
Fariborz Jahanian363c59b2007-12-01 19:58:28 +0000288 case ConditionalOperatorClass: {
289 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
290 return Exp->getCond()->hasLocalSideEffect()
291 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
292 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
293 }
294
Chris Lattner4b009652007-07-25 00:24:17 +0000295 case MemberExprClass:
296 case ArraySubscriptExprClass:
297 // If the base pointer or element is to a volatile pointer/field, accessing
298 // if is a side effect.
299 return getType().isVolatileQualified();
300
301 case CallExprClass:
302 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
303 // should warn.
304 return true;
Chris Lattner99f5f0b2007-09-26 22:06:30 +0000305 case ObjCMessageExprClass:
306 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000307
308 case CastExprClass:
309 // If this is a cast to void, check the operand. Otherwise, the result of
310 // the cast is unused.
311 if (getType()->isVoidType())
312 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
313 return false;
314 }
315}
316
317/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
318/// incomplete type other than void. Nonarray expressions that can be lvalues:
319/// - name, where name must be a variable
320/// - e[i]
321/// - (e), where e must be an lvalue
322/// - e.name, where e must be an lvalue
323/// - e->name
324/// - *e, the type of e cannot be a function type
325/// - string-constant
Chris Lattner5bf72022007-10-30 22:53:42 +0000326/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Chris Lattner4b009652007-07-25 00:24:17 +0000327/// - reference type [C++ [expr]]
328///
329Expr::isLvalueResult Expr::isLvalue() const {
330 // first, check the type (C99 6.3.2.1)
331 if (TR->isFunctionType()) // from isObjectType()
332 return LV_NotObjectType;
333
334 if (TR->isVoidType())
335 return LV_IncompleteVoidType;
336
337 if (TR->isReferenceType()) // C++ [expr]
338 return LV_Valid;
339
340 // the type looks fine, now check the expression
341 switch (getStmtClass()) {
342 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson9e933a22007-11-30 22:47:59 +0000343 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000344 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
345 // For vectors, make sure base is an lvalue (i.e. not a function call).
346 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
347 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
348 return LV_Valid;
349 case DeclRefExprClass: // C99 6.5.1p2
350 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
351 return LV_Valid;
352 break;
353 case MemberExprClass: { // C99 6.5.2.3p4
354 const MemberExpr *m = cast<MemberExpr>(this);
355 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
356 }
Chris Lattner5bf72022007-10-30 22:53:42 +0000357 case UnaryOperatorClass:
Chris Lattner4b009652007-07-25 00:24:17 +0000358 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner5bf72022007-10-30 22:53:42 +0000359 return LV_Valid; // C99 6.5.3p4
360
361 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
362 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
363 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Chris Lattner4b009652007-07-25 00:24:17 +0000364 break;
365 case ParenExprClass: // C99 6.5.1p5
366 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffc7c66532007-12-05 04:00:10 +0000367 case CompoundLiteralExprClass: // C99 6.5.2.5p5
368 return LV_Valid;
Chris Lattnera0d03a72007-08-03 17:31:20 +0000369 case OCUVectorElementExprClass:
370 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroffba67f692007-07-30 03:29:09 +0000371 return LV_DuplicateVectorComponents;
372 return LV_Valid;
Steve Naroff46f18f22007-11-12 14:34:27 +0000373 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
374 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000375 default:
376 break;
377 }
378 return LV_InvalidExpression;
379}
380
381/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
382/// does not have an incomplete type, does not have a const-qualified type, and
383/// if it is a structure or union, does not have any member (including,
384/// recursively, any member or element of all contained aggregates or unions)
385/// with a const-qualified type.
386Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
387 isLvalueResult lvalResult = isLvalue();
388
389 switch (lvalResult) {
390 case LV_Valid: break;
391 case LV_NotObjectType: return MLV_NotObjectType;
392 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000393 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000394 case LV_InvalidExpression: return MLV_InvalidExpression;
395 }
396 if (TR.isConstQualified())
397 return MLV_ConstQualified;
398 if (TR->isArrayType())
399 return MLV_ArrayType;
400 if (TR->isIncompleteType())
401 return MLV_IncompleteType;
402
403 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
404 if (r->hasConstFields())
405 return MLV_ConstQualified;
406 }
407 return MLV_Valid;
408}
409
Chris Lattner743ec372007-11-27 21:35:27 +0000410/// hasStaticStorage - Return true if this expression has static storage
411/// duration. This means that the address of this expression is a link-time
412/// constant.
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000413bool Expr::hasStaticStorage() const {
414 switch (getStmtClass()) {
415 default:
416 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000417 case ParenExprClass:
418 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
419 case ImplicitCastExprClass:
420 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000421 case DeclRefExprClass: {
422 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
423 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
424 return VD->hasStaticStorage();
425 return false;
426 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000427 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000428 const MemberExpr *M = cast<MemberExpr>(this);
429 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000430 }
Chris Lattner743ec372007-11-27 21:35:27 +0000431 case ArraySubscriptExprClass:
432 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000433 }
434}
435
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000436bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000437 switch (getStmtClass()) {
438 default:
439 if (Loc) *Loc = getLocStart();
440 return false;
441 case ParenExprClass:
442 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
443 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000444 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000445 case FloatingLiteralClass:
446 case IntegerLiteralClass:
447 case CharacterLiteralClass:
448 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000449 case TypesCompatibleExprClass:
450 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000451 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000452 case CallExprClass: {
453 const CallExpr *CE = cast<CallExpr>(this);
454 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000455 Result.zextOrTrunc(
456 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000457 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000458 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000459 if (Loc) *Loc = getLocStart();
460 return false;
461 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000462 case DeclRefExprClass: {
463 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
464 // Accept address of function.
465 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000466 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000467 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000468 if (isa<VarDecl>(D))
469 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000470 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000471 }
Steve Narofff91f9722008-01-09 00:05:37 +0000472 case CompoundLiteralExprClass:
473 if (Loc) *Loc = getLocStart();
474 // Allow "(int []){2,4}", since the array will be converted to a pointer.
475 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000476 case UnaryOperatorClass: {
477 const UnaryOperator *Exp = cast<UnaryOperator>(this);
478
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000479 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000480 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
481 if (!Exp->getSubExpr()->hasStaticStorage()) {
482 if (Loc) *Loc = getLocStart();
483 return false;
484 }
485 return true;
486 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000487
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000488 // Get the operand value. If this is sizeof/alignof, do not evalute the
489 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000490 if (!Exp->isSizeOfAlignOfOp() &&
491 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000492 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
493 return false;
494
495 switch (Exp->getOpcode()) {
496 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
497 // See C99 6.6p3.
498 default:
499 if (Loc) *Loc = Exp->getOperatorLoc();
500 return false;
501 case UnaryOperator::Extension:
502 return true; // FIXME: this is wrong.
503 case UnaryOperator::SizeOf:
504 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000505 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000506 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000507 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
508 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000509 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000510 }
Chris Lattner06db6132007-10-18 00:20:32 +0000511 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000512 case UnaryOperator::LNot:
513 case UnaryOperator::Plus:
514 case UnaryOperator::Minus:
515 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000516 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000517 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000518 }
519 case SizeOfAlignOfTypeExprClass: {
520 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
521 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000522 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
523 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000524 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000525 }
Chris Lattner06db6132007-10-18 00:20:32 +0000526 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000527 }
528 case BinaryOperatorClass: {
529 const BinaryOperator *Exp = cast<BinaryOperator>(this);
530
531 // The LHS of a constant expr is always evaluated and needed.
532 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
533 return false;
534
535 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
536 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000537 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000538 }
539 case ImplicitCastExprClass:
540 case CastExprClass: {
541 const Expr *SubExpr;
542 SourceLocation CastLoc;
543 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
544 SubExpr = C->getSubExpr();
545 CastLoc = C->getLParenLoc();
546 } else {
547 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
548 CastLoc = getLocStart();
549 }
550 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
551 if (Loc) *Loc = SubExpr->getLocStart();
552 return false;
553 }
Chris Lattner06db6132007-10-18 00:20:32 +0000554 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000555 }
556 case ConditionalOperatorClass: {
557 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000558 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000559 // Handle the GNU extension for missing LHS.
560 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000561 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000562 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000563 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000564 }
Steve Narofff0b23542008-01-10 22:15:12 +0000565 case InitListExprClass: {
566 const InitListExpr *Exp = cast<InitListExpr>(this);
567 unsigned numInits = Exp->getNumInits();
568 for (unsigned i = 0; i < numInits; i++) {
569 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
570 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
571 return false;
572 }
573 }
574 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000575 }
Steve Narofff0b23542008-01-10 22:15:12 +0000576 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000577}
578
Chris Lattner4b009652007-07-25 00:24:17 +0000579/// isIntegerConstantExpr - this recursive routine will test if an expression is
580/// an integer constant expression. Note: With the introduction of VLA's in
581/// C99 the result of the sizeof operator is no longer always a constant
582/// expression. The generalization of the wording to include any subexpression
583/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
584/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
585/// "0 || f()" can be treated as a constant expression. In C90 this expression,
586/// occurring in a context requiring a constant, would have been a constraint
587/// violation. FIXME: This routine currently implements C90 semantics.
588/// To properly implement C99 semantics this routine will need to evaluate
589/// expressions involving operators previously mentioned.
590
591/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
592/// comma, etc
593///
594/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000595/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000596///
597/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
598/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
599/// cast+dereference.
600bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
601 SourceLocation *Loc, bool isEvaluated) const {
602 switch (getStmtClass()) {
603 default:
604 if (Loc) *Loc = getLocStart();
605 return false;
606 case ParenExprClass:
607 return cast<ParenExpr>(this)->getSubExpr()->
608 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
609 case IntegerLiteralClass:
610 Result = cast<IntegerLiteral>(this)->getValue();
611 break;
612 case CharacterLiteralClass: {
613 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000614 Result.zextOrTrunc(
615 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000616 Result = CL->getValue();
617 Result.setIsUnsigned(!getType()->isSignedIntegerType());
618 break;
619 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000620 case TypesCompatibleExprClass: {
621 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000622 Result.zextOrTrunc(
623 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000624 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000625 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000626 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000627 case CallExprClass: {
628 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000629 Result.zextOrTrunc(
630 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000631 if (CE->isBuiltinClassifyType(Result))
632 break;
633 if (Loc) *Loc = getLocStart();
634 return false;
635 }
Chris Lattner4b009652007-07-25 00:24:17 +0000636 case DeclRefExprClass:
637 if (const EnumConstantDecl *D =
638 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
639 Result = D->getInitVal();
640 break;
641 }
642 if (Loc) *Loc = getLocStart();
643 return false;
644 case UnaryOperatorClass: {
645 const UnaryOperator *Exp = cast<UnaryOperator>(this);
646
647 // Get the operand value. If this is sizeof/alignof, do not evalute the
648 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000649 if (!Exp->isSizeOfAlignOfOp() &&
650 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000651 return false;
652
653 switch (Exp->getOpcode()) {
654 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
655 // See C99 6.6p3.
656 default:
657 if (Loc) *Loc = Exp->getOperatorLoc();
658 return false;
659 case UnaryOperator::Extension:
660 return true; // FIXME: this is wrong.
661 case UnaryOperator::SizeOf:
662 case UnaryOperator::AlignOf:
663 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000664 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
665 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000666 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000667 }
Chris Lattner4b009652007-07-25 00:24:17 +0000668
669 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000670 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000671 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
672 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000673
674 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000675 if (Exp->getSubExpr()->getType()->isFunctionType()) {
676 // GCC extension: sizeof(function) = 1.
677 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
678 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000679 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
680 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000681 } else {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000682 unsigned CharSize =
683 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
684
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000685 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
686 Exp->getOperatorLoc()) / CharSize;
687 }
Chris Lattner4b009652007-07-25 00:24:17 +0000688 break;
689 case UnaryOperator::LNot: {
690 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000691 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000692 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
693 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000694 Result = Val;
695 break;
696 }
697 case UnaryOperator::Plus:
698 break;
699 case UnaryOperator::Minus:
700 Result = -Result;
701 break;
702 case UnaryOperator::Not:
703 Result = ~Result;
704 break;
705 }
706 break;
707 }
708 case SizeOfAlignOfTypeExprClass: {
709 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
710 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000711 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
712 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000713 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000714 }
Chris Lattner4b009652007-07-25 00:24:17 +0000715
716 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000717 Result.zextOrTrunc(
718 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000719
720 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000721 if (Exp->getArgumentType()->isFunctionType()) {
722 // GCC extension: sizeof(function) = 1.
723 Result = Exp->isSizeOf() ? 1 : 4;
724 } else if (Exp->isSizeOf()) {
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000725 unsigned CharSize =
726 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
727
728 Result = Ctx.getTypeSize(Exp->getArgumentType(),
729 Exp->getOperatorLoc()) / CharSize;
730 }
Chris Lattner4b009652007-07-25 00:24:17 +0000731 else
732 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000733
Chris Lattner4b009652007-07-25 00:24:17 +0000734 break;
735 }
736 case BinaryOperatorClass: {
737 const BinaryOperator *Exp = cast<BinaryOperator>(this);
738
739 // The LHS of a constant expr is always evaluated and needed.
740 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
741 return false;
742
743 llvm::APSInt RHS(Result);
744
745 // The short-circuiting &&/|| operators don't necessarily evaluate their
746 // RHS. Make sure to pass isEvaluated down correctly.
747 if (Exp->isLogicalOp()) {
748 bool RHSEval;
749 if (Exp->getOpcode() == BinaryOperator::LAnd)
750 RHSEval = Result != 0;
751 else {
752 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
753 RHSEval = Result == 0;
754 }
755
756 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
757 isEvaluated & RHSEval))
758 return false;
759 } else {
760 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
761 return false;
762 }
763
764 switch (Exp->getOpcode()) {
765 default:
766 if (Loc) *Loc = getLocStart();
767 return false;
768 case BinaryOperator::Mul:
769 Result *= RHS;
770 break;
771 case BinaryOperator::Div:
772 if (RHS == 0) {
773 if (!isEvaluated) break;
774 if (Loc) *Loc = getLocStart();
775 return false;
776 }
777 Result /= RHS;
778 break;
779 case BinaryOperator::Rem:
780 if (RHS == 0) {
781 if (!isEvaluated) break;
782 if (Loc) *Loc = getLocStart();
783 return false;
784 }
785 Result %= RHS;
786 break;
787 case BinaryOperator::Add: Result += RHS; break;
788 case BinaryOperator::Sub: Result -= RHS; break;
789 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000790 Result <<=
791 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000792 break;
793 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000794 Result >>=
795 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000796 break;
797 case BinaryOperator::LT: Result = Result < RHS; break;
798 case BinaryOperator::GT: Result = Result > RHS; break;
799 case BinaryOperator::LE: Result = Result <= RHS; break;
800 case BinaryOperator::GE: Result = Result >= RHS; break;
801 case BinaryOperator::EQ: Result = Result == RHS; break;
802 case BinaryOperator::NE: Result = Result != RHS; break;
803 case BinaryOperator::And: Result &= RHS; break;
804 case BinaryOperator::Xor: Result ^= RHS; break;
805 case BinaryOperator::Or: Result |= RHS; break;
806 case BinaryOperator::LAnd:
807 Result = Result != 0 && RHS != 0;
808 break;
809 case BinaryOperator::LOr:
810 Result = Result != 0 || RHS != 0;
811 break;
812
813 case BinaryOperator::Comma:
814 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
815 // *except* when they are contained within a subexpression that is not
816 // evaluated". Note that Assignment can never happen due to constraints
817 // on the LHS subexpr, so we don't need to check it here.
818 if (isEvaluated) {
819 if (Loc) *Loc = getLocStart();
820 return false;
821 }
822
823 // The result of the constant expr is the RHS.
824 Result = RHS;
825 return true;
826 }
827
828 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
829 break;
830 }
831 case ImplicitCastExprClass:
832 case CastExprClass: {
833 const Expr *SubExpr;
834 SourceLocation CastLoc;
835 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
836 SubExpr = C->getSubExpr();
837 CastLoc = C->getLParenLoc();
838 } else {
839 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
840 CastLoc = getLocStart();
841 }
842
843 // C99 6.6p6: shall only convert arithmetic types to integer types.
844 if (!SubExpr->getType()->isArithmeticType() ||
845 !getType()->isIntegerType()) {
846 if (Loc) *Loc = SubExpr->getLocStart();
847 return false;
848 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000849
850 uint32_t DestWidth =
851 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
852
Chris Lattner4b009652007-07-25 00:24:17 +0000853 // Handle simple integer->integer casts.
854 if (SubExpr->getType()->isIntegerType()) {
855 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
856 return false;
857
858 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000859 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000860 if (getType()->isBooleanType()) {
861 // Conversion to bool compares against zero.
862 Result = Result != 0;
863 Result.zextOrTrunc(DestWidth);
864 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000865 Result.sextOrTrunc(DestWidth);
866 else // If the input is unsigned, do a zero extend, noop, or truncate.
867 Result.zextOrTrunc(DestWidth);
868 break;
869 }
870
871 // Allow floating constants that are the immediate operands of casts or that
872 // are parenthesized.
873 const Expr *Operand = SubExpr;
874 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
875 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000876
877 // If this isn't a floating literal, we can't handle it.
878 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
879 if (!FL) {
880 if (Loc) *Loc = Operand->getLocStart();
881 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000882 }
Chris Lattner000c4102008-01-09 18:59:34 +0000883
884 // If the destination is boolean, compare against zero.
885 if (getType()->isBooleanType()) {
886 Result = !FL->getValue().isZero();
887 Result.zextOrTrunc(DestWidth);
888 break;
889 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000890
891 // Determine whether we are converting to unsigned or signed.
892 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000893
894 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
895 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000896 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000897 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
898 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000899 Result = llvm::APInt(DestWidth, 4, Space);
900 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000901 }
902 case ConditionalOperatorClass: {
903 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
904
905 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
906 return false;
907
908 const Expr *TrueExp = Exp->getLHS();
909 const Expr *FalseExp = Exp->getRHS();
910 if (Result == 0) std::swap(TrueExp, FalseExp);
911
912 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000913 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000914 return false;
915 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000916 if (TrueExp &&
917 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000918 return false;
919 break;
920 }
921 }
922
923 // Cases that are valid constant exprs fall through to here.
924 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
925 return true;
926}
927
928
929/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
930/// integer constant expression with the value zero, or if this is one that is
931/// cast to void*.
932bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
933 // Strip off a cast to void*, if it exists.
934 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
935 // Check that it is a cast to void*.
936 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
937 QualType Pointee = PT->getPointeeType();
938 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
939 CE->getSubExpr()->getType()->isIntegerType()) // from int.
940 return CE->getSubExpr()->isNullPointerConstant(Ctx);
941 }
Steve Naroff1d6b2472007-08-28 21:20:34 +0000942 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff3d052872007-08-29 00:00:02 +0000943 // Ignore the ImplicitCastExpr type entirely.
944 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000945 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
946 // Accept ((void*)0) as a null pointer constant, as many other
947 // implementations do.
948 return PE->getSubExpr()->isNullPointerConstant(Ctx);
949 }
950
951 // This expression must be an integer type.
952 if (!getType()->isIntegerType())
953 return false;
954
955 // If we have an integer constant expression, we need to *evaluate* it and
956 // test for the value 0.
957 llvm::APSInt Val(32);
958 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
959}
Steve Naroffc11705f2007-07-28 23:10:27 +0000960
Chris Lattnera0d03a72007-08-03 17:31:20 +0000961unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000962 return strlen(Accessor.getName());
963}
964
965
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000966/// getComponentType - Determine whether the components of this access are
967/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000968OCUVectorElementExpr::ElementType
969OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000970 // derive the component type, no need to waste space.
971 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000972
Chris Lattner9096b792007-08-02 22:33:49 +0000973 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
974 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000975
Chris Lattner9096b792007-08-02 22:33:49 +0000976 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000977 "getComponentType(): Illegal accessor");
978 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000979}
Steve Naroffba67f692007-07-30 03:29:09 +0000980
Chris Lattnera0d03a72007-08-03 17:31:20 +0000981/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000982/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000983bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000984 const char *compStr = Accessor.getName();
985 unsigned length = strlen(compStr);
986
987 for (unsigned i = 0; i < length-1; i++) {
988 const char *s = compStr+i;
989 for (const char c = *s++; *s; s++)
990 if (c == *s)
991 return true;
992 }
993 return false;
994}
Chris Lattner42158e72007-08-02 23:36:59 +0000995
996/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000997unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +0000998 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +0000999 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +00001000
1001 unsigned Result = 0;
1002
1003 while (length--) {
1004 Result <<= 2;
1005 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1006 assert(Idx != -1 && "Invalid accessor letter");
1007 Result |= Idx;
1008 }
1009 return Result;
1010}
1011
Steve Naroff4ed9d662007-09-27 14:38:14 +00001012// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001013ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001014 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001015 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001016 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001017 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1018 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001019 NumArgs = nargs;
1020 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001021 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001022 if (NumArgs) {
1023 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001024 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1025 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001026 LBracloc = LBrac;
1027 RBracloc = RBrac;
1028}
1029
Steve Naroff4ed9d662007-09-27 14:38:14 +00001030// constructor for class messages.
1031// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001032ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001033 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001034 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001035 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001036 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1037 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001038 NumArgs = nargs;
1039 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +00001040 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +00001041 if (NumArgs) {
1042 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001043 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1044 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001045 LBracloc = LBrac;
1046 RBracloc = RBrac;
1047}
1048
Chris Lattnerf624cd22007-10-25 00:29:32 +00001049
1050bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1051 llvm::APSInt CondVal(32);
1052 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1053 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1054 return CondVal != 0;
1055}
1056
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001057//===----------------------------------------------------------------------===//
1058// Child Iterators for iterating over subexpressions/substatements
1059//===----------------------------------------------------------------------===//
1060
1061// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001062Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1063Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001064
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001065// ObjCIvarRefExpr
1066Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1067Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1068
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001069// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001070Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1071Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001072
1073// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001074Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1075Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001076
1077// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001078Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1079Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001080
1081// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001082Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1083Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001084
Chris Lattner1de66eb2007-08-26 03:42:43 +00001085// ImaginaryLiteral
1086Stmt::child_iterator ImaginaryLiteral::child_begin() {
1087 return reinterpret_cast<Stmt**>(&Val);
1088}
1089Stmt::child_iterator ImaginaryLiteral::child_end() {
1090 return reinterpret_cast<Stmt**>(&Val)+1;
1091}
1092
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001093// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001094Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1095Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001096
1097// ParenExpr
1098Stmt::child_iterator ParenExpr::child_begin() {
1099 return reinterpret_cast<Stmt**>(&Val);
1100}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001101Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001102 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001103}
1104
1105// UnaryOperator
1106Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001107 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001108}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001109Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001110 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001111}
1112
1113// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001114Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001115 // If the type is a VLA type (and not a typedef), the size expression of the
1116 // VLA needs to be treated as an executable expression.
1117 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1118 return child_iterator(T);
1119 else
1120 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001121}
1122Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001123 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001124}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001125
1126// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001127Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001128 return reinterpret_cast<Stmt**>(&SubExprs);
1129}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001130Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001131 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001132}
1133
1134// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001135Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001136 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001137}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001138Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001139 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001140}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001141
1142// MemberExpr
1143Stmt::child_iterator MemberExpr::child_begin() {
1144 return reinterpret_cast<Stmt**>(&Base);
1145}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001146Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001147 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001148}
1149
1150// OCUVectorElementExpr
1151Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1152 return reinterpret_cast<Stmt**>(&Base);
1153}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001154Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001155 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001156}
1157
1158// CompoundLiteralExpr
1159Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1160 return reinterpret_cast<Stmt**>(&Init);
1161}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001162Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001163 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001164}
1165
1166// ImplicitCastExpr
1167Stmt::child_iterator ImplicitCastExpr::child_begin() {
1168 return reinterpret_cast<Stmt**>(&Op);
1169}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001170Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001171 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001172}
1173
1174// CastExpr
1175Stmt::child_iterator CastExpr::child_begin() {
1176 return reinterpret_cast<Stmt**>(&Op);
1177}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001178Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001179 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001180}
1181
1182// BinaryOperator
1183Stmt::child_iterator BinaryOperator::child_begin() {
1184 return reinterpret_cast<Stmt**>(&SubExprs);
1185}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001186Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001187 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001188}
1189
1190// ConditionalOperator
1191Stmt::child_iterator ConditionalOperator::child_begin() {
1192 return reinterpret_cast<Stmt**>(&SubExprs);
1193}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001194Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001195 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001196}
1197
1198// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001199Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1200Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001201
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001202// StmtExpr
1203Stmt::child_iterator StmtExpr::child_begin() {
1204 return reinterpret_cast<Stmt**>(&SubStmt);
1205}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001206Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001207 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001208}
1209
1210// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001211Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1212 return child_iterator();
1213}
1214
1215Stmt::child_iterator TypesCompatibleExpr::child_end() {
1216 return child_iterator();
1217}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001218
1219// ChooseExpr
1220Stmt::child_iterator ChooseExpr::child_begin() {
1221 return reinterpret_cast<Stmt**>(&SubExprs);
1222}
1223
1224Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001225 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001226}
1227
Anders Carlsson36760332007-10-15 20:28:48 +00001228// VAArgExpr
1229Stmt::child_iterator VAArgExpr::child_begin() {
1230 return reinterpret_cast<Stmt**>(&Val);
1231}
1232
1233Stmt::child_iterator VAArgExpr::child_end() {
1234 return reinterpret_cast<Stmt**>(&Val)+1;
1235}
1236
Anders Carlsson762b7c72007-08-31 04:56:16 +00001237// InitListExpr
1238Stmt::child_iterator InitListExpr::child_begin() {
1239 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1240}
1241Stmt::child_iterator InitListExpr::child_end() {
1242 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1243}
1244
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001245// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001246Stmt::child_iterator ObjCStringLiteral::child_begin() {
1247 return child_iterator();
1248}
1249Stmt::child_iterator ObjCStringLiteral::child_end() {
1250 return child_iterator();
1251}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001252
1253// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001254Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1255Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001256
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001257// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001258Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1259 return child_iterator();
1260}
1261Stmt::child_iterator ObjCSelectorExpr::child_end() {
1262 return child_iterator();
1263}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001264
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001265// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001266Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1267 return child_iterator();
1268}
1269Stmt::child_iterator ObjCProtocolExpr::child_end() {
1270 return child_iterator();
1271}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001272
Steve Naroffc39ca262007-09-18 23:55:05 +00001273// ObjCMessageExpr
1274Stmt::child_iterator ObjCMessageExpr::child_begin() {
1275 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1276}
1277Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001278 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001279}
1280