blob: e2aa6ca405586d9d86dc145464a5eeaa37fa428a [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 Lattner7e637512008-01-12 08:14:25 +0000375 case PreDefinedExprClass:
376 return LV_Valid;
Chris Lattner4b009652007-07-25 00:24:17 +0000377 default:
378 break;
379 }
380 return LV_InvalidExpression;
381}
382
383/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
384/// does not have an incomplete type, does not have a const-qualified type, and
385/// if it is a structure or union, does not have any member (including,
386/// recursively, any member or element of all contained aggregates or unions)
387/// with a const-qualified type.
388Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
389 isLvalueResult lvalResult = isLvalue();
390
391 switch (lvalResult) {
392 case LV_Valid: break;
393 case LV_NotObjectType: return MLV_NotObjectType;
394 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroffba67f692007-07-30 03:29:09 +0000395 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner4b009652007-07-25 00:24:17 +0000396 case LV_InvalidExpression: return MLV_InvalidExpression;
397 }
398 if (TR.isConstQualified())
399 return MLV_ConstQualified;
400 if (TR->isArrayType())
401 return MLV_ArrayType;
402 if (TR->isIncompleteType())
403 return MLV_IncompleteType;
404
405 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
406 if (r->hasConstFields())
407 return MLV_ConstQualified;
408 }
409 return MLV_Valid;
410}
411
Chris Lattner743ec372007-11-27 21:35:27 +0000412/// hasStaticStorage - Return true if this expression has static storage
413/// duration. This means that the address of this expression is a link-time
414/// constant.
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000415bool Expr::hasStaticStorage() const {
416 switch (getStmtClass()) {
417 default:
418 return false;
Chris Lattner743ec372007-11-27 21:35:27 +0000419 case ParenExprClass:
420 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
421 case ImplicitCastExprClass:
422 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Steve Naroffbe37fc02008-01-14 18:19:28 +0000423 case CompoundLiteralExprClass:
424 return cast<CompoundLiteralExpr>(this)->isFileScope();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000425 case DeclRefExprClass: {
426 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
427 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
428 return VD->hasStaticStorage();
429 return false;
430 }
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000431 case MemberExprClass: {
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000432 const MemberExpr *M = cast<MemberExpr>(this);
433 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerdb586bf2007-11-28 04:30:09 +0000434 }
Chris Lattner743ec372007-11-27 21:35:27 +0000435 case ArraySubscriptExprClass:
436 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattner7e637512008-01-12 08:14:25 +0000437 case PreDefinedExprClass:
438 return true;
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000439 }
440}
441
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000442bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000443 switch (getStmtClass()) {
444 default:
445 if (Loc) *Loc = getLocStart();
446 return false;
447 case ParenExprClass:
448 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
449 case StringLiteralClass:
Steve Naroff4fdea132007-11-09 15:00:03 +0000450 case ObjCStringLiteralClass:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000451 case FloatingLiteralClass:
452 case IntegerLiteralClass:
453 case CharacterLiteralClass:
454 case ImaginaryLiteralClass:
Anders Carlsson855d78d2007-10-17 00:52:43 +0000455 case TypesCompatibleExprClass:
456 case CXXBoolLiteralExprClass:
Chris Lattner06db6132007-10-18 00:20:32 +0000457 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000458 case CallExprClass: {
459 const CallExpr *CE = cast<CallExpr>(this);
460 llvm::APSInt Result(32);
Hartmut Kaiser38af7012007-09-16 21:35:35 +0000461 Result.zextOrTrunc(
462 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000463 if (CE->isBuiltinClassifyType(Result))
Chris Lattner06db6132007-10-18 00:20:32 +0000464 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000465 if (Loc) *Loc = getLocStart();
466 return false;
467 }
Chris Lattner42e86b62007-11-01 02:45:17 +0000468 case DeclRefExprClass: {
469 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
470 // Accept address of function.
471 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner06db6132007-10-18 00:20:32 +0000472 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000473 if (Loc) *Loc = getLocStart();
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000474 if (isa<VarDecl>(D))
475 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000476 return false;
Chris Lattner42e86b62007-11-01 02:45:17 +0000477 }
Steve Narofff91f9722008-01-09 00:05:37 +0000478 case CompoundLiteralExprClass:
479 if (Loc) *Loc = getLocStart();
480 // Allow "(int []){2,4}", since the array will be converted to a pointer.
481 return TR->isArrayType();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000482 case UnaryOperatorClass: {
483 const UnaryOperator *Exp = cast<UnaryOperator>(this);
484
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000485 // C99 6.6p9
Chris Lattner35b662f2007-12-11 23:11:17 +0000486 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
487 if (!Exp->getSubExpr()->hasStaticStorage()) {
488 if (Loc) *Loc = getLocStart();
489 return false;
490 }
491 return true;
492 }
Chris Lattnerba3ddb22007-11-13 18:05:45 +0000493
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000494 // Get the operand value. If this is sizeof/alignof, do not evalute the
495 // operand. This affects C99 6.6p3.
Steve Narofff0b23542008-01-10 22:15:12 +0000496 if (!Exp->isSizeOfAlignOfOp() &&
497 Exp->getOpcode() != UnaryOperator::OffsetOf &&
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000498 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
499 return false;
500
501 switch (Exp->getOpcode()) {
502 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
503 // See C99 6.6p3.
504 default:
505 if (Loc) *Loc = Exp->getOperatorLoc();
506 return false;
507 case UnaryOperator::Extension:
508 return true; // FIXME: this is wrong.
509 case UnaryOperator::SizeOf:
510 case UnaryOperator::AlignOf:
Steve Narofff0b23542008-01-10 22:15:12 +0000511 case UnaryOperator::OffsetOf:
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000512 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000513 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
514 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000515 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000516 }
Chris Lattner06db6132007-10-18 00:20:32 +0000517 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000518 case UnaryOperator::LNot:
519 case UnaryOperator::Plus:
520 case UnaryOperator::Minus:
521 case UnaryOperator::Not:
Chris Lattner06db6132007-10-18 00:20:32 +0000522 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000523 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000524 }
525 case SizeOfAlignOfTypeExprClass: {
526 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
527 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000528 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
529 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000530 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000531 }
Chris Lattner06db6132007-10-18 00:20:32 +0000532 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000533 }
534 case BinaryOperatorClass: {
535 const BinaryOperator *Exp = cast<BinaryOperator>(this);
536
537 // The LHS of a constant expr is always evaluated and needed.
538 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
539 return false;
540
541 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
542 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000543 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000544 }
545 case ImplicitCastExprClass:
546 case CastExprClass: {
547 const Expr *SubExpr;
548 SourceLocation CastLoc;
549 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
550 SubExpr = C->getSubExpr();
551 CastLoc = C->getLParenLoc();
552 } else {
553 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
554 CastLoc = getLocStart();
555 }
556 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
557 if (Loc) *Loc = SubExpr->getLocStart();
558 return false;
559 }
Chris Lattner06db6132007-10-18 00:20:32 +0000560 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000561 }
562 case ConditionalOperatorClass: {
563 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner06db6132007-10-18 00:20:32 +0000564 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson37365fc2007-11-30 19:04:31 +0000565 // Handle the GNU extension for missing LHS.
566 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner06db6132007-10-18 00:20:32 +0000567 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000568 return false;
Chris Lattner06db6132007-10-18 00:20:32 +0000569 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000570 }
Steve Narofff0b23542008-01-10 22:15:12 +0000571 case InitListExprClass: {
572 const InitListExpr *Exp = cast<InitListExpr>(this);
573 unsigned numInits = Exp->getNumInits();
574 for (unsigned i = 0; i < numInits; i++) {
575 if (!Exp->getInit(i)->isConstantExpr(Ctx, Loc)) {
576 if (Loc) *Loc = Exp->getInit(i)->getLocStart();
577 return false;
578 }
579 }
580 return true;
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000581 }
Steve Narofff0b23542008-01-10 22:15:12 +0000582 }
Steve Naroff7c9d72d2007-09-02 20:30:18 +0000583}
584
Chris Lattner4b009652007-07-25 00:24:17 +0000585/// isIntegerConstantExpr - this recursive routine will test if an expression is
586/// an integer constant expression. Note: With the introduction of VLA's in
587/// C99 the result of the sizeof operator is no longer always a constant
588/// expression. The generalization of the wording to include any subexpression
589/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
590/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
591/// "0 || f()" can be treated as a constant expression. In C90 this expression,
592/// occurring in a context requiring a constant, would have been a constraint
593/// violation. FIXME: This routine currently implements C90 semantics.
594/// To properly implement C99 semantics this routine will need to evaluate
595/// expressions involving operators previously mentioned.
596
597/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
598/// comma, etc
599///
600/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattner9d020b32007-09-26 00:47:26 +0000601/// permit this. This includes things like (int)1e1000
Chris Lattner4b009652007-07-25 00:24:17 +0000602///
603/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
604/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
605/// cast+dereference.
606bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
607 SourceLocation *Loc, bool isEvaluated) const {
608 switch (getStmtClass()) {
609 default:
610 if (Loc) *Loc = getLocStart();
611 return false;
612 case ParenExprClass:
613 return cast<ParenExpr>(this)->getSubExpr()->
614 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
615 case IntegerLiteralClass:
616 Result = cast<IntegerLiteral>(this)->getValue();
617 break;
618 case CharacterLiteralClass: {
619 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000620 Result.zextOrTrunc(
621 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000622 Result = CL->getValue();
623 Result.setIsUnsigned(!getType()->isSignedIntegerType());
624 break;
625 }
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000626 case TypesCompatibleExprClass: {
627 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000628 Result.zextOrTrunc(
629 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroff85f0dc52007-10-15 20:41:53 +0000630 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff1200b5a2007-08-02 00:13:27 +0000631 break;
Steve Naroffc6f0fd32007-08-02 04:09:23 +0000632 }
Steve Naroff8d3b1702007-08-08 22:15:55 +0000633 case CallExprClass: {
634 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner3496d522007-09-04 02:45:27 +0000635 Result.zextOrTrunc(
636 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff8d3b1702007-08-08 22:15:55 +0000637 if (CE->isBuiltinClassifyType(Result))
638 break;
639 if (Loc) *Loc = getLocStart();
640 return false;
641 }
Chris Lattner4b009652007-07-25 00:24:17 +0000642 case DeclRefExprClass:
643 if (const EnumConstantDecl *D =
644 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
645 Result = D->getInitVal();
646 break;
647 }
648 if (Loc) *Loc = getLocStart();
649 return false;
650 case UnaryOperatorClass: {
651 const UnaryOperator *Exp = cast<UnaryOperator>(this);
652
653 // Get the operand value. If this is sizeof/alignof, do not evalute the
654 // operand. This affects C99 6.6p3.
Chris Lattner5a9b6242007-08-23 21:42:50 +0000655 if (!Exp->isSizeOfAlignOfOp() &&
656 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000657 return false;
658
659 switch (Exp->getOpcode()) {
660 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
661 // See C99 6.6p3.
662 default:
663 if (Loc) *Loc = Exp->getOperatorLoc();
664 return false;
665 case UnaryOperator::Extension:
666 return true; // FIXME: this is wrong.
667 case UnaryOperator::SizeOf:
668 case UnaryOperator::AlignOf:
669 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000670 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
671 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000672 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000673 }
Chris Lattner4b009652007-07-25 00:24:17 +0000674
675 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000676 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000677 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
678 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000679
680 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000681 if (Exp->getSubExpr()->getType()->isFunctionType()) {
682 // GCC extension: sizeof(function) = 1.
683 Result = Exp->getOpcode() == UnaryOperator::AlignOf ? 4 : 1;
684 } else if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner4b009652007-07-25 00:24:17 +0000685 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
686 Exp->getOperatorLoc());
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000687 } else {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000688 unsigned CharSize =
689 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
690
Chris Lattnerd9ffbc92007-11-27 18:22:04 +0000691 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
692 Exp->getOperatorLoc()) / CharSize;
693 }
Chris Lattner4b009652007-07-25 00:24:17 +0000694 break;
695 case UnaryOperator::LNot: {
696 bool Val = Result != 0;
Chris Lattner3496d522007-09-04 02:45:27 +0000697 Result.zextOrTrunc(
Chris Lattner9d020b32007-09-26 00:47:26 +0000698 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
699 Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000700 Result = Val;
701 break;
702 }
703 case UnaryOperator::Plus:
704 break;
705 case UnaryOperator::Minus:
706 Result = -Result;
707 break;
708 case UnaryOperator::Not:
709 Result = ~Result;
710 break;
711 }
712 break;
713 }
714 case SizeOfAlignOfTypeExprClass: {
715 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
716 // alignof always evaluates to a constant.
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000717 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
718 if (Loc) *Loc = Exp->getOperatorLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000719 return false;
Chris Lattnerb3ff0822007-12-18 07:15:40 +0000720 }
Chris Lattner4b009652007-07-25 00:24:17 +0000721
722 // Return the result in the right width.
Chris Lattner3496d522007-09-04 02:45:27 +0000723 Result.zextOrTrunc(
724 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner4b009652007-07-25 00:24:17 +0000725
726 // Get information about the size or align.
Chris Lattnerd4dc2672008-01-02 21:54:09 +0000727 if (Exp->getArgumentType()->isFunctionType()) {
728 // GCC extension: sizeof(function) = 1.
729 Result = Exp->isSizeOf() ? 1 : 4;
730 } else if (Exp->isSizeOf()) {
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000731 unsigned CharSize =
732 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
733
734 Result = Ctx.getTypeSize(Exp->getArgumentType(),
735 Exp->getOperatorLoc()) / CharSize;
736 }
Chris Lattner4b009652007-07-25 00:24:17 +0000737 else
738 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremeneka9d0fd92007-12-17 17:38:43 +0000739
Chris Lattner4b009652007-07-25 00:24:17 +0000740 break;
741 }
742 case BinaryOperatorClass: {
743 const BinaryOperator *Exp = cast<BinaryOperator>(this);
744
745 // The LHS of a constant expr is always evaluated and needed.
746 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
747 return false;
748
749 llvm::APSInt RHS(Result);
750
751 // The short-circuiting &&/|| operators don't necessarily evaluate their
752 // RHS. Make sure to pass isEvaluated down correctly.
753 if (Exp->isLogicalOp()) {
754 bool RHSEval;
755 if (Exp->getOpcode() == BinaryOperator::LAnd)
756 RHSEval = Result != 0;
757 else {
758 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
759 RHSEval = Result == 0;
760 }
761
762 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
763 isEvaluated & RHSEval))
764 return false;
765 } else {
766 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
767 return false;
768 }
769
770 switch (Exp->getOpcode()) {
771 default:
772 if (Loc) *Loc = getLocStart();
773 return false;
774 case BinaryOperator::Mul:
775 Result *= RHS;
776 break;
777 case BinaryOperator::Div:
778 if (RHS == 0) {
779 if (!isEvaluated) break;
780 if (Loc) *Loc = getLocStart();
781 return false;
782 }
783 Result /= RHS;
784 break;
785 case BinaryOperator::Rem:
786 if (RHS == 0) {
787 if (!isEvaluated) break;
788 if (Loc) *Loc = getLocStart();
789 return false;
790 }
791 Result %= RHS;
792 break;
793 case BinaryOperator::Add: Result += RHS; break;
794 case BinaryOperator::Sub: Result -= RHS; break;
795 case BinaryOperator::Shl:
Chris Lattner3496d522007-09-04 02:45:27 +0000796 Result <<=
797 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000798 break;
799 case BinaryOperator::Shr:
Chris Lattner3496d522007-09-04 02:45:27 +0000800 Result >>=
801 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner4b009652007-07-25 00:24:17 +0000802 break;
803 case BinaryOperator::LT: Result = Result < RHS; break;
804 case BinaryOperator::GT: Result = Result > RHS; break;
805 case BinaryOperator::LE: Result = Result <= RHS; break;
806 case BinaryOperator::GE: Result = Result >= RHS; break;
807 case BinaryOperator::EQ: Result = Result == RHS; break;
808 case BinaryOperator::NE: Result = Result != RHS; break;
809 case BinaryOperator::And: Result &= RHS; break;
810 case BinaryOperator::Xor: Result ^= RHS; break;
811 case BinaryOperator::Or: Result |= RHS; break;
812 case BinaryOperator::LAnd:
813 Result = Result != 0 && RHS != 0;
814 break;
815 case BinaryOperator::LOr:
816 Result = Result != 0 || RHS != 0;
817 break;
818
819 case BinaryOperator::Comma:
820 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
821 // *except* when they are contained within a subexpression that is not
822 // evaluated". Note that Assignment can never happen due to constraints
823 // on the LHS subexpr, so we don't need to check it here.
824 if (isEvaluated) {
825 if (Loc) *Loc = getLocStart();
826 return false;
827 }
828
829 // The result of the constant expr is the RHS.
830 Result = RHS;
831 return true;
832 }
833
834 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
835 break;
836 }
837 case ImplicitCastExprClass:
838 case CastExprClass: {
839 const Expr *SubExpr;
840 SourceLocation CastLoc;
841 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
842 SubExpr = C->getSubExpr();
843 CastLoc = C->getLParenLoc();
844 } else {
845 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
846 CastLoc = getLocStart();
847 }
848
849 // C99 6.6p6: shall only convert arithmetic types to integer types.
850 if (!SubExpr->getType()->isArithmeticType() ||
851 !getType()->isIntegerType()) {
852 if (Loc) *Loc = SubExpr->getLocStart();
853 return false;
854 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000855
856 uint32_t DestWidth =
857 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
858
Chris Lattner4b009652007-07-25 00:24:17 +0000859 // Handle simple integer->integer casts.
860 if (SubExpr->getType()->isIntegerType()) {
861 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
862 return false;
863
864 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner4b009652007-07-25 00:24:17 +0000865 // If the input is signed, do a sign extend, noop, or truncate.
Chris Lattner000c4102008-01-09 18:59:34 +0000866 if (getType()->isBooleanType()) {
867 // Conversion to bool compares against zero.
868 Result = Result != 0;
869 Result.zextOrTrunc(DestWidth);
870 } else if (SubExpr->getType()->isSignedIntegerType())
Chris Lattner4b009652007-07-25 00:24:17 +0000871 Result.sextOrTrunc(DestWidth);
872 else // If the input is unsigned, do a zero extend, noop, or truncate.
873 Result.zextOrTrunc(DestWidth);
874 break;
875 }
876
877 // Allow floating constants that are the immediate operands of casts or that
878 // are parenthesized.
879 const Expr *Operand = SubExpr;
880 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
881 Operand = PE->getSubExpr();
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000882
883 // If this isn't a floating literal, we can't handle it.
884 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
885 if (!FL) {
886 if (Loc) *Loc = Operand->getLocStart();
887 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000888 }
Chris Lattner000c4102008-01-09 18:59:34 +0000889
890 // If the destination is boolean, compare against zero.
891 if (getType()->isBooleanType()) {
892 Result = !FL->getValue().isZero();
893 Result.zextOrTrunc(DestWidth);
894 break;
895 }
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000896
897 // Determine whether we are converting to unsigned or signed.
898 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattner9d020b32007-09-26 00:47:26 +0000899
900 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
901 // be called multiple times per AST.
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000902 uint64_t Space[4];
Chris Lattner9d020b32007-09-26 00:47:26 +0000903 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
904 llvm::APFloat::rmTowardZero);
Chris Lattner76a0c1b2007-09-22 19:04:13 +0000905 Result = llvm::APInt(DestWidth, 4, Space);
906 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000907 }
908 case ConditionalOperatorClass: {
909 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
910
911 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
912 return false;
913
914 const Expr *TrueExp = Exp->getLHS();
915 const Expr *FalseExp = Exp->getRHS();
916 if (Result == 0) std::swap(TrueExp, FalseExp);
917
918 // Evaluate the false one first, discard the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000919 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattner4b009652007-07-25 00:24:17 +0000920 return false;
921 // Evalute the true one, capture the result.
Anders Carlsson37365fc2007-11-30 19:04:31 +0000922 if (TrueExp &&
923 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattner4b009652007-07-25 00:24:17 +0000924 return false;
925 break;
926 }
927 }
928
929 // Cases that are valid constant exprs fall through to here.
930 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
931 return true;
932}
933
Chris Lattner4b009652007-07-25 00:24:17 +0000934/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
935/// integer constant expression with the value zero, or if this is one that is
936/// cast to void*.
937bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Steve Naroffa2e53222008-01-14 16:10:57 +0000938 // Strip off a cast to void*, if it exists.
939 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
940 // Check that it is a cast to void*.
Chris Lattner4b009652007-07-25 00:24:17 +0000941 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
942 QualType Pointee = PT->getPointeeType();
Steve Naroffa2e53222008-01-14 16:10:57 +0000943 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
944 CE->getSubExpr()->getType()->isIntegerType()) // from int.
945 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner4b009652007-07-25 00:24:17 +0000946 }
Steve Naroffa2e53222008-01-14 16:10:57 +0000947 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
948 // Ignore the ImplicitCastExpr type entirely.
949 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
950 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
951 // Accept ((void*)0) as a null pointer constant, as many other
952 // implementations do.
953 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Narofff33a9852008-01-14 02:53:34 +0000954 }
Steve Naroffa2e53222008-01-14 16:10:57 +0000955
956 // This expression must be an integer type.
957 if (!getType()->isIntegerType())
958 return false;
959
Chris Lattner4b009652007-07-25 00:24:17 +0000960 // If we have an integer constant expression, we need to *evaluate* it and
961 // test for the value 0.
962 llvm::APSInt Val(32);
Steve Naroffa2e53222008-01-14 16:10:57 +0000963 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000964}
Steve Naroffc11705f2007-07-28 23:10:27 +0000965
Chris Lattnera0d03a72007-08-03 17:31:20 +0000966unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner50547852007-08-03 16:00:20 +0000967 return strlen(Accessor.getName());
968}
969
970
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000971/// getComponentType - Determine whether the components of this access are
972/// "point" "color" or "texture" elements.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000973OCUVectorElementExpr::ElementType
974OCUVectorElementExpr::getElementType() const {
Steve Naroffc11705f2007-07-28 23:10:27 +0000975 // derive the component type, no need to waste space.
976 const char *compStr = Accessor.getName();
Chris Lattnerabf25b32007-08-02 22:20:00 +0000977
Chris Lattner9096b792007-08-02 22:33:49 +0000978 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
979 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerabf25b32007-08-02 22:20:00 +0000980
Chris Lattner9096b792007-08-02 22:33:49 +0000981 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerabf25b32007-08-02 22:20:00 +0000982 "getComponentType(): Illegal accessor");
983 return Texture;
Steve Naroffc11705f2007-07-28 23:10:27 +0000984}
Steve Naroffba67f692007-07-30 03:29:09 +0000985
Chris Lattnera0d03a72007-08-03 17:31:20 +0000986/// containsDuplicateElements - Return true if any element access is
Chris Lattnerf4bf5512007-08-02 21:47:28 +0000987/// repeated.
Chris Lattnera0d03a72007-08-03 17:31:20 +0000988bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroffba67f692007-07-30 03:29:09 +0000989 const char *compStr = Accessor.getName();
990 unsigned length = strlen(compStr);
991
992 for (unsigned i = 0; i < length-1; i++) {
993 const char *s = compStr+i;
994 for (const char c = *s++; *s; s++)
995 if (c == *s)
996 return true;
997 }
998 return false;
999}
Chris Lattner42158e72007-08-02 23:36:59 +00001000
1001/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnera0d03a72007-08-03 17:31:20 +00001002unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner42158e72007-08-02 23:36:59 +00001003 const char *compStr = Accessor.getName();
Chris Lattnera0d03a72007-08-03 17:31:20 +00001004 unsigned length = getNumElements();
Chris Lattner42158e72007-08-02 23:36:59 +00001005
1006 unsigned Result = 0;
1007
1008 while (length--) {
1009 Result <<= 2;
1010 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
1011 assert(Idx != -1 && "Invalid accessor letter");
1012 Result |= Idx;
1013 }
1014 return Result;
1015}
1016
Steve Naroff4ed9d662007-09-27 14:38:14 +00001017// constructor for instance messages.
Steve Naroff6cb1d362007-09-28 22:22:11 +00001018ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001019 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001020 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001021 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001022 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1023 MethodProto(mproto), ClassName(0) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001024 NumArgs = nargs;
1025 SubExprs = new Expr*[NumArgs+1];
Steve Naroff4ed9d662007-09-27 14:38:14 +00001026 SubExprs[RECEIVER] = receiver;
Steve Naroff9f176d12007-11-15 13:05:42 +00001027 if (NumArgs) {
1028 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001029 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1030 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001031 LBracloc = LBrac;
1032 RBracloc = RBrac;
1033}
1034
Steve Naroff4ed9d662007-09-27 14:38:14 +00001035// constructor for class messages.
1036// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroff6cb1d362007-09-28 22:22:11 +00001037ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Ted Kremenek42730c52008-01-07 19:49:32 +00001038 QualType retType, ObjCMethodDecl *mproto,
Steve Naroff1e1c3912007-11-03 16:37:59 +00001039 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff9f176d12007-11-15 13:05:42 +00001040 Expr **ArgExprs, unsigned nargs)
Steve Naroff1e1c3912007-11-03 16:37:59 +00001041 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1042 MethodProto(mproto), ClassName(clsName) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001043 NumArgs = nargs;
1044 SubExprs = new Expr*[NumArgs+1];
Steve Naroffc39ca262007-09-18 23:55:05 +00001045 SubExprs[RECEIVER] = 0;
Steve Naroff9f176d12007-11-15 13:05:42 +00001046 if (NumArgs) {
1047 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001048 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1049 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001050 LBracloc = LBrac;
1051 RBracloc = RBrac;
1052}
1053
Chris Lattnerf624cd22007-10-25 00:29:32 +00001054
1055bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1056 llvm::APSInt CondVal(32);
1057 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1058 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1059 return CondVal != 0;
1060}
1061
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001062//===----------------------------------------------------------------------===//
1063// Child Iterators for iterating over subexpressions/substatements
1064//===----------------------------------------------------------------------===//
1065
1066// DeclRefExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001067Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1068Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001069
Steve Naroff5eb2a4a2007-11-12 14:29:37 +00001070// ObjCIvarRefExpr
1071Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1072Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1073
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001074// PreDefinedExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001075Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1076Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001077
1078// IntegerLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001079Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1080Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001081
1082// CharacterLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001083Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1084Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001085
1086// FloatingLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001087Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1088Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001089
Chris Lattner1de66eb2007-08-26 03:42:43 +00001090// ImaginaryLiteral
1091Stmt::child_iterator ImaginaryLiteral::child_begin() {
1092 return reinterpret_cast<Stmt**>(&Val);
1093}
1094Stmt::child_iterator ImaginaryLiteral::child_end() {
1095 return reinterpret_cast<Stmt**>(&Val)+1;
1096}
1097
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001098// StringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001099Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1100Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001101
1102// ParenExpr
1103Stmt::child_iterator ParenExpr::child_begin() {
1104 return reinterpret_cast<Stmt**>(&Val);
1105}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001106Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001107 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001108}
1109
1110// UnaryOperator
1111Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001112 return reinterpret_cast<Stmt**>(&Val);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001113}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001114Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenek6d7ea522007-12-15 00:39:18 +00001115 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001116}
1117
1118// SizeOfAlignOfTypeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001119Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001120 // If the type is a VLA type (and not a typedef), the size expression of the
1121 // VLA needs to be treated as an executable expression.
1122 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1123 return child_iterator(T);
1124 else
1125 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001126}
1127Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenekb7cf0952007-12-14 22:52:23 +00001128 return child_iterator();
Ted Kremeneka6478552007-10-18 23:28:49 +00001129}
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001130
1131// ArraySubscriptExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001132Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001133 return reinterpret_cast<Stmt**>(&SubExprs);
1134}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001135Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001136 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001137}
1138
1139// CallExpr
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001140Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001141 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001142}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001143Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek15ede502007-08-27 21:11:44 +00001144 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremeneke4acb9c2007-08-24 18:13:47 +00001145}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001146
1147// MemberExpr
1148Stmt::child_iterator MemberExpr::child_begin() {
1149 return reinterpret_cast<Stmt**>(&Base);
1150}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001151Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001152 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001153}
1154
1155// OCUVectorElementExpr
1156Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1157 return reinterpret_cast<Stmt**>(&Base);
1158}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001159Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001160 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001161}
1162
1163// CompoundLiteralExpr
1164Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1165 return reinterpret_cast<Stmt**>(&Init);
1166}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001167Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001168 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001169}
1170
1171// ImplicitCastExpr
1172Stmt::child_iterator ImplicitCastExpr::child_begin() {
1173 return reinterpret_cast<Stmt**>(&Op);
1174}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001175Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001176 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001177}
1178
1179// CastExpr
1180Stmt::child_iterator CastExpr::child_begin() {
1181 return reinterpret_cast<Stmt**>(&Op);
1182}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001183Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001184 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001185}
1186
1187// BinaryOperator
1188Stmt::child_iterator BinaryOperator::child_begin() {
1189 return reinterpret_cast<Stmt**>(&SubExprs);
1190}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001191Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001192 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001193}
1194
1195// ConditionalOperator
1196Stmt::child_iterator ConditionalOperator::child_begin() {
1197 return reinterpret_cast<Stmt**>(&SubExprs);
1198}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001199Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001200 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001201}
1202
1203// AddrLabelExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001204Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1205Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001206
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001207// StmtExpr
1208Stmt::child_iterator StmtExpr::child_begin() {
1209 return reinterpret_cast<Stmt**>(&SubStmt);
1210}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001211Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001212 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001213}
1214
1215// TypesCompatibleExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001216Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1217 return child_iterator();
1218}
1219
1220Stmt::child_iterator TypesCompatibleExpr::child_end() {
1221 return child_iterator();
1222}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001223
1224// ChooseExpr
1225Stmt::child_iterator ChooseExpr::child_begin() {
1226 return reinterpret_cast<Stmt**>(&SubExprs);
1227}
1228
1229Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1de66eb2007-08-26 03:42:43 +00001230 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001231}
1232
Anders Carlsson36760332007-10-15 20:28:48 +00001233// VAArgExpr
1234Stmt::child_iterator VAArgExpr::child_begin() {
1235 return reinterpret_cast<Stmt**>(&Val);
1236}
1237
1238Stmt::child_iterator VAArgExpr::child_end() {
1239 return reinterpret_cast<Stmt**>(&Val)+1;
1240}
1241
Anders Carlsson762b7c72007-08-31 04:56:16 +00001242// InitListExpr
1243Stmt::child_iterator InitListExpr::child_begin() {
1244 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1245}
1246Stmt::child_iterator InitListExpr::child_end() {
1247 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1248}
1249
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001250// ObjCStringLiteral
Ted Kremeneka6478552007-10-18 23:28:49 +00001251Stmt::child_iterator ObjCStringLiteral::child_begin() {
1252 return child_iterator();
1253}
1254Stmt::child_iterator ObjCStringLiteral::child_end() {
1255 return child_iterator();
1256}
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001257
1258// ObjCEncodeExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001259Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1260Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek9fdc8a92007-08-24 20:06:47 +00001261
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001262// ObjCSelectorExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001263Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1264 return child_iterator();
1265}
1266Stmt::child_iterator ObjCSelectorExpr::child_end() {
1267 return child_iterator();
1268}
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001269
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001270// ObjCProtocolExpr
Ted Kremeneka6478552007-10-18 23:28:49 +00001271Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1272 return child_iterator();
1273}
1274Stmt::child_iterator ObjCProtocolExpr::child_end() {
1275 return child_iterator();
1276}
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001277
Steve Naroffc39ca262007-09-18 23:55:05 +00001278// ObjCMessageExpr
1279Stmt::child_iterator ObjCMessageExpr::child_begin() {
1280 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1281}
1282Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff4ed9d662007-09-27 14:38:14 +00001283 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroffc39ca262007-09-18 23:55:05 +00001284}
1285