blob: 88ba6a6eff83ae8d75aa789f693393de23a3f352 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Expr.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/StmtVisitor.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000017#include "clang/Basic/IdentifierTable.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000018#include "clang/Basic/TargetInfo.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner73d0d4f2007-08-30 17:45:32 +000073 case OffsetOf: return "__builtin_offsetof";
Reid Spencer5f016e22007-07-11 17:01:13 +000074 }
75}
76
77//===----------------------------------------------------------------------===//
78// Postfix Operators.
79//===----------------------------------------------------------------------===//
80
81CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
82 SourceLocation rparenloc)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000083 : Expr(CallExprClass, t), NumArgs(numargs) {
84 SubExprs = new Expr*[numargs+1];
85 SubExprs[FN] = fn;
Reid Spencer5f016e22007-07-11 17:01:13 +000086 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek77ed8e42007-08-24 18:13:47 +000087 SubExprs[i+ARGS_START] = args[i];
Reid Spencer5f016e22007-07-11 17:01:13 +000088 RParenLoc = rparenloc;
89}
90
Chris Lattnerd18b3292007-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 Naroff13b7c5f2007-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 Lattner3ef5bc02007-11-08 17:56:40 +0000183 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000184 }
185 return true;
186 }
187 return false;
188}
189
Reid Spencer5f016e22007-07-11 17:01:13 +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 Carlsson66b5a8a2007-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}
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnere7716e62007-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 Lattnereb14fe82007-08-25 02:00:02 +0000285 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000286 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000287
Fariborz Jahanianab38e4b2007-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
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattnera9c01022007-09-26 22:06:30 +0000305 case ObjCMessageExprClass:
306 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner7da36f62007-10-30 22:53:42 +0000326/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000327/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000328///
Bill Wendlingca51c972007-07-16 07:07:56 +0000329Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000330 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000331 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 return LV_NotObjectType;
333
Steve Naroff731ec572007-07-21 13:32:03 +0000334 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000335 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000336
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000337 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000338 return LV_Valid;
339
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // the type looks fine, now check the expression
341 switch (getStmtClass()) {
342 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000343 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +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;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000353 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 const MemberExpr *m = cast<MemberExpr>(this);
355 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000356 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000357 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 break;
365 case ParenExprClass: // C99 6.5.1p5
366 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffe6386392007-12-05 04:00:10 +0000367 case CompoundLiteralExprClass: // C99 6.5.2.5p5
368 return LV_Valid;
Chris Lattner6481a572007-08-03 17:31:20 +0000369 case OCUVectorElementExprClass:
370 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000371 return LV_DuplicateVectorComponents;
372 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000373 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
374 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +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.
Bill Wendlingca51c972007-07-16 07:07:56 +0000386Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 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 Narofffec0b492007-07-30 03:29:09 +0000393 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner4cc62712007-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 Lattner1d09ecc2007-11-13 18:05:45 +0000413bool Expr::hasStaticStorage() const {
414 switch (getStmtClass()) {
415 default:
416 return false;
Chris Lattner4cc62712007-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 Lattner1d09ecc2007-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 Lattnerfb708062007-11-28 04:30:09 +0000427 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000428 const MemberExpr *M = cast<MemberExpr>(this);
429 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000430 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000431 case ArraySubscriptExprClass:
432 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000433 }
434}
435
Steve Naroff38374b02007-09-02 20:30:18 +0000436bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff38374b02007-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 Naroff5d37e322007-11-09 15:00:03 +0000444 case ObjCStringLiteralClass:
Steve Naroff38374b02007-09-02 20:30:18 +0000445 case FloatingLiteralClass:
446 case IntegerLiteralClass:
447 case CharacterLiteralClass:
448 case ImaginaryLiteralClass:
Anders Carlsson1a86b332007-10-17 00:52:43 +0000449 case TypesCompatibleExprClass:
450 case CXXBoolLiteralExprClass:
Chris Lattner2777e492007-10-18 00:20:32 +0000451 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000452 case CallExprClass: {
453 const CallExpr *CE = cast<CallExpr>(this);
454 llvm::APSInt Result(32);
Hartmut Kaiser86fd3552007-09-16 21:35:35 +0000455 Result.zextOrTrunc(
456 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff38374b02007-09-02 20:30:18 +0000457 if (CE->isBuiltinClassifyType(Result))
Chris Lattner2777e492007-10-18 00:20:32 +0000458 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000459 if (Loc) *Loc = getLocStart();
460 return false;
461 }
Chris Lattner4ef8dd62007-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 Lattner2777e492007-10-18 00:20:32 +0000466 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000467 if (Loc) *Loc = getLocStart();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000468 if (isa<VarDecl>(D))
469 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000470 return false;
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000471 }
Steve Naroff38374b02007-09-02 20:30:18 +0000472 case UnaryOperatorClass: {
473 const UnaryOperator *Exp = cast<UnaryOperator>(this);
474
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000475 // C99 6.6p9
Chris Lattner239c15e2007-12-11 23:11:17 +0000476 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
477 if (!Exp->getSubExpr()->hasStaticStorage()) {
478 if (Loc) *Loc = getLocStart();
479 return false;
480 }
481 return true;
482 }
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000483
Steve Naroff38374b02007-09-02 20:30:18 +0000484 // Get the operand value. If this is sizeof/alignof, do not evalute the
485 // operand. This affects C99 6.6p3.
486 if (!Exp->isSizeOfAlignOfOp() &&
487 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
488 return false;
489
490 switch (Exp->getOpcode()) {
491 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
492 // See C99 6.6p3.
493 default:
494 if (Loc) *Loc = Exp->getOperatorLoc();
495 return false;
496 case UnaryOperator::Extension:
497 return true; // FIXME: this is wrong.
498 case UnaryOperator::SizeOf:
499 case UnaryOperator::AlignOf:
500 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000501 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
502 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000503 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000504 }
Chris Lattner2777e492007-10-18 00:20:32 +0000505 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000506 case UnaryOperator::LNot:
507 case UnaryOperator::Plus:
508 case UnaryOperator::Minus:
509 case UnaryOperator::Not:
Chris Lattner2777e492007-10-18 00:20:32 +0000510 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000511 }
Steve Naroff38374b02007-09-02 20:30:18 +0000512 }
513 case SizeOfAlignOfTypeExprClass: {
514 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
515 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000516 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
517 if (Loc) *Loc = Exp->getOperatorLoc();
Steve Naroff38374b02007-09-02 20:30:18 +0000518 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000519 }
Chris Lattner2777e492007-10-18 00:20:32 +0000520 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000521 }
522 case BinaryOperatorClass: {
523 const BinaryOperator *Exp = cast<BinaryOperator>(this);
524
525 // The LHS of a constant expr is always evaluated and needed.
526 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
527 return false;
528
529 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
530 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000531 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000532 }
533 case ImplicitCastExprClass:
534 case CastExprClass: {
535 const Expr *SubExpr;
536 SourceLocation CastLoc;
537 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
538 SubExpr = C->getSubExpr();
539 CastLoc = C->getLParenLoc();
540 } else {
541 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
542 CastLoc = getLocStart();
543 }
544 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
545 if (Loc) *Loc = SubExpr->getLocStart();
546 return false;
547 }
Chris Lattner2777e492007-10-18 00:20:32 +0000548 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000549 }
550 case ConditionalOperatorClass: {
551 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner2777e492007-10-18 00:20:32 +0000552 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson39073232007-11-30 19:04:31 +0000553 // Handle the GNU extension for missing LHS.
554 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner2777e492007-10-18 00:20:32 +0000555 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff38374b02007-09-02 20:30:18 +0000556 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000557 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000558 }
559 }
560
561 return true;
562}
563
Reid Spencer5f016e22007-07-11 17:01:13 +0000564/// isIntegerConstantExpr - this recursive routine will test if an expression is
565/// an integer constant expression. Note: With the introduction of VLA's in
566/// C99 the result of the sizeof operator is no longer always a constant
567/// expression. The generalization of the wording to include any subexpression
568/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
569/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
570/// "0 || f()" can be treated as a constant expression. In C90 this expression,
571/// occurring in a context requiring a constant, would have been a constraint
572/// violation. FIXME: This routine currently implements C90 semantics.
573/// To properly implement C99 semantics this routine will need to evaluate
574/// expressions involving operators previously mentioned.
575
576/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
577/// comma, etc
578///
579/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000580/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000581///
582/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
583/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
584/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000585bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
586 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 switch (getStmtClass()) {
588 default:
589 if (Loc) *Loc = getLocStart();
590 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000591 case ParenExprClass:
592 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000593 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 case IntegerLiteralClass:
595 Result = cast<IntegerLiteral>(this)->getValue();
596 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000597 case CharacterLiteralClass: {
598 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000599 Result.zextOrTrunc(
600 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000601 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000602 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000604 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000605 case TypesCompatibleExprClass: {
606 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000607 Result.zextOrTrunc(
608 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffec0550f2007-10-15 20:41:53 +0000609 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff389cecc2007-08-02 00:13:27 +0000610 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000611 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000612 case CallExprClass: {
613 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000614 Result.zextOrTrunc(
615 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000616 if (CE->isBuiltinClassifyType(Result))
617 break;
618 if (Loc) *Loc = getLocStart();
619 return false;
620 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 case DeclRefExprClass:
622 if (const EnumConstantDecl *D =
623 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
624 Result = D->getInitVal();
625 break;
626 }
627 if (Loc) *Loc = getLocStart();
628 return false;
629 case UnaryOperatorClass: {
630 const UnaryOperator *Exp = cast<UnaryOperator>(this);
631
632 // Get the operand value. If this is sizeof/alignof, do not evalute the
633 // operand. This affects C99 6.6p3.
Chris Lattner602dafd2007-08-23 21:42:50 +0000634 if (!Exp->isSizeOfAlignOfOp() &&
635 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 return false;
637
638 switch (Exp->getOpcode()) {
639 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
640 // See C99 6.6p3.
641 default:
642 if (Loc) *Loc = Exp->getOperatorLoc();
643 return false;
644 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000645 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 case UnaryOperator::SizeOf:
647 case UnaryOperator::AlignOf:
648 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner65383472007-12-18 07:15:40 +0000649 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx)) {
650 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000652 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000653
Chris Lattner76e773a2007-07-18 18:38:36 +0000654 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000655 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000656 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
657 Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000658
659 // Get information about the size or align.
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000660 if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner76e773a2007-07-18 18:38:36 +0000661 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
662 Exp->getOperatorLoc());
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000663 } else {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000664 unsigned CharSize =
665 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
666
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000667 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
668 Exp->getOperatorLoc()) / CharSize;
669 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 break;
671 case UnaryOperator::LNot: {
672 bool Val = Result != 0;
Chris Lattner701e5eb2007-09-04 02:45:27 +0000673 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000674 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
675 Exp->getOperatorLoc())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 Result = Val;
677 break;
678 }
679 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 break;
681 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 Result = -Result;
683 break;
684 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 Result = ~Result;
686 break;
687 }
688 break;
689 }
690 case SizeOfAlignOfTypeExprClass: {
691 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
692 // alignof always evaluates to a constant.
Chris Lattner65383472007-12-18 07:15:40 +0000693 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx)) {
694 if (Loc) *Loc = Exp->getOperatorLoc();
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 return false;
Chris Lattner65383472007-12-18 07:15:40 +0000696 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000697
Chris Lattner76e773a2007-07-18 18:38:36 +0000698 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000699 Result.zextOrTrunc(
700 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000701
702 // Get information about the size or align.
Ted Kremenek060e4702007-12-17 17:38:43 +0000703 if (Exp->isSizeOf()) {
704 unsigned CharSize =
705 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
706
707 Result = Ctx.getTypeSize(Exp->getArgumentType(),
708 Exp->getOperatorLoc()) / CharSize;
709 }
Chris Lattner76e773a2007-07-18 18:38:36 +0000710 else
711 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Ted Kremenek060e4702007-12-17 17:38:43 +0000712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 break;
714 }
715 case BinaryOperatorClass: {
716 const BinaryOperator *Exp = cast<BinaryOperator>(this);
717
718 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000719 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 return false;
721
722 llvm::APSInt RHS(Result);
723
724 // The short-circuiting &&/|| operators don't necessarily evaluate their
725 // RHS. Make sure to pass isEvaluated down correctly.
726 if (Exp->isLogicalOp()) {
727 bool RHSEval;
728 if (Exp->getOpcode() == BinaryOperator::LAnd)
729 RHSEval = Result != 0;
730 else {
731 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
732 RHSEval = Result == 0;
733 }
734
Chris Lattner590b6642007-07-15 23:26:56 +0000735 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000736 isEvaluated & RHSEval))
737 return false;
738 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000739 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 return false;
741 }
742
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 switch (Exp->getOpcode()) {
744 default:
745 if (Loc) *Loc = getLocStart();
746 return false;
747 case BinaryOperator::Mul:
748 Result *= RHS;
749 break;
750 case BinaryOperator::Div:
751 if (RHS == 0) {
752 if (!isEvaluated) break;
753 if (Loc) *Loc = getLocStart();
754 return false;
755 }
756 Result /= RHS;
757 break;
758 case BinaryOperator::Rem:
759 if (RHS == 0) {
760 if (!isEvaluated) break;
761 if (Loc) *Loc = getLocStart();
762 return false;
763 }
764 Result %= RHS;
765 break;
766 case BinaryOperator::Add: Result += RHS; break;
767 case BinaryOperator::Sub: Result -= RHS; break;
768 case BinaryOperator::Shl:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000769 Result <<=
770 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 break;
772 case BinaryOperator::Shr:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000773 Result >>=
774 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 break;
776 case BinaryOperator::LT: Result = Result < RHS; break;
777 case BinaryOperator::GT: Result = Result > RHS; break;
778 case BinaryOperator::LE: Result = Result <= RHS; break;
779 case BinaryOperator::GE: Result = Result >= RHS; break;
780 case BinaryOperator::EQ: Result = Result == RHS; break;
781 case BinaryOperator::NE: Result = Result != RHS; break;
782 case BinaryOperator::And: Result &= RHS; break;
783 case BinaryOperator::Xor: Result ^= RHS; break;
784 case BinaryOperator::Or: Result |= RHS; break;
785 case BinaryOperator::LAnd:
786 Result = Result != 0 && RHS != 0;
787 break;
788 case BinaryOperator::LOr:
789 Result = Result != 0 || RHS != 0;
790 break;
791
792 case BinaryOperator::Comma:
793 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
794 // *except* when they are contained within a subexpression that is not
795 // evaluated". Note that Assignment can never happen due to constraints
796 // on the LHS subexpr, so we don't need to check it here.
797 if (isEvaluated) {
798 if (Loc) *Loc = getLocStart();
799 return false;
800 }
801
802 // The result of the constant expr is the RHS.
803 Result = RHS;
804 return true;
805 }
806
807 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
808 break;
809 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000810 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000812 const Expr *SubExpr;
813 SourceLocation CastLoc;
814 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
815 SubExpr = C->getSubExpr();
816 CastLoc = C->getLParenLoc();
817 } else {
818 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
819 CastLoc = getLocStart();
820 }
821
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000823 if (!SubExpr->getType()->isArithmeticType() ||
824 !getType()->isIntegerType()) {
825 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 return false;
827 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000828
829 uint32_t DestWidth =
830 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
831
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000833 if (SubExpr->getType()->isIntegerType()) {
834 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000836
837 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000838 // If the input is signed, do a sign extend, noop, or truncate.
839 if (SubExpr->getType()->isSignedIntegerType())
840 Result.sextOrTrunc(DestWidth);
841 else // If the input is unsigned, do a zero extend, noop, or truncate.
842 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000843 break;
844 }
845
846 // Allow floating constants that are the immediate operands of casts or that
847 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000848 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
850 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +0000851
852 // If this isn't a floating literal, we can't handle it.
853 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
854 if (!FL) {
855 if (Loc) *Loc = Operand->getLocStart();
856 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000858
859 // Determine whether we are converting to unsigned or signed.
860 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +0000861
862 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
863 // be called multiple times per AST.
Chris Lattner987b15d2007-09-22 19:04:13 +0000864 uint64_t Space[4];
Chris Lattnerccc213f2007-09-26 00:47:26 +0000865 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
866 llvm::APFloat::rmTowardZero);
Chris Lattner987b15d2007-09-22 19:04:13 +0000867 Result = llvm::APInt(DestWidth, 4, Space);
868 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 }
870 case ConditionalOperatorClass: {
871 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
872
Chris Lattner590b6642007-07-15 23:26:56 +0000873 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000874 return false;
875
876 const Expr *TrueExp = Exp->getLHS();
877 const Expr *FalseExp = Exp->getRHS();
878 if (Result == 0) std::swap(TrueExp, FalseExp);
879
880 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000881 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 return false;
883 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000884 if (TrueExp &&
885 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 break;
888 }
889 }
890
891 // Cases that are valid constant exprs fall through to here.
892 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
893 return true;
894}
895
896
897/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
898/// integer constant expression with the value zero, or if this is one that is
899/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000900bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 // Strip off a cast to void*, if it exists.
902 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
903 // Check that it is a cast to void*.
904 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
905 QualType Pointee = PT->getPointeeType();
906 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
907 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner590b6642007-07-15 23:26:56 +0000908 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 }
Steve Naroff7269f2d2007-08-28 21:20:34 +0000910 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff19a6ebd2007-08-29 00:00:02 +0000911 // Ignore the ImplicitCastExpr type entirely.
912 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
914 // Accept ((void*)0) as a null pointer constant, as many other
915 // implementations do.
Chris Lattner590b6642007-07-15 23:26:56 +0000916 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 }
918
919 // This expression must be an integer type.
920 if (!getType()->isIntegerType())
921 return false;
922
923 // If we have an integer constant expression, we need to *evaluate* it and
924 // test for the value 0.
925 llvm::APSInt Val(32);
Chris Lattner590b6642007-07-15 23:26:56 +0000926 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000927}
Steve Naroff31a45842007-07-28 23:10:27 +0000928
Chris Lattner6481a572007-08-03 17:31:20 +0000929unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000930 return strlen(Accessor.getName());
931}
932
933
Chris Lattnercb92a112007-08-02 21:47:28 +0000934/// getComponentType - Determine whether the components of this access are
935/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +0000936OCUVectorElementExpr::ElementType
937OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +0000938 // derive the component type, no need to waste space.
939 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000940
Chris Lattner88dca042007-08-02 22:33:49 +0000941 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
942 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000943
Chris Lattner88dca042007-08-02 22:33:49 +0000944 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000945 "getComponentType(): Illegal accessor");
946 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000947}
Steve Narofffec0b492007-07-30 03:29:09 +0000948
Chris Lattner6481a572007-08-03 17:31:20 +0000949/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +0000950/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +0000951bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +0000952 const char *compStr = Accessor.getName();
953 unsigned length = strlen(compStr);
954
955 for (unsigned i = 0; i < length-1; i++) {
956 const char *s = compStr+i;
957 for (const char c = *s++; *s; s++)
958 if (c == *s)
959 return true;
960 }
961 return false;
962}
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000963
964/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +0000965unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000966 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +0000967 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000968
969 unsigned Result = 0;
970
971 while (length--) {
972 Result <<= 2;
973 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
974 assert(Idx != -1 && "Invalid accessor letter");
975 Result |= Idx;
976 }
977 return Result;
978}
979
Steve Naroff68d331a2007-09-27 14:38:14 +0000980// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +0000981ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroffdb611d52007-11-03 16:37:59 +0000982 QualType retType, ObjcMethodDecl *mproto,
983 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +0000984 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +0000985 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
986 MethodProto(mproto), ClassName(0) {
Steve Naroff49f109c2007-11-15 13:05:42 +0000987 NumArgs = nargs;
988 SubExprs = new Expr*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +0000989 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +0000990 if (NumArgs) {
991 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +0000992 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
993 }
Steve Naroff563477d2007-09-18 23:55:05 +0000994 LBracloc = LBrac;
995 RBracloc = RBrac;
996}
997
Steve Naroff68d331a2007-09-27 14:38:14 +0000998// constructor for class messages.
999// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +00001000ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroffdb611d52007-11-03 16:37:59 +00001001 QualType retType, ObjcMethodDecl *mproto,
1002 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +00001003 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +00001004 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
1005 MethodProto(mproto), ClassName(clsName) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001006 NumArgs = nargs;
1007 SubExprs = new Expr*[NumArgs+1];
Steve Naroff563477d2007-09-18 23:55:05 +00001008 SubExprs[RECEIVER] = 0;
Steve Naroff49f109c2007-11-15 13:05:42 +00001009 if (NumArgs) {
1010 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +00001011 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
1012 }
Steve Naroff563477d2007-09-18 23:55:05 +00001013 LBracloc = LBrac;
1014 RBracloc = RBrac;
1015}
1016
Chris Lattner27437ca2007-10-25 00:29:32 +00001017
1018bool ChooseExpr::isConditionTrue(ASTContext &C) const {
1019 llvm::APSInt CondVal(32);
1020 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
1021 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
1022 return CondVal != 0;
1023}
1024
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001025//===----------------------------------------------------------------------===//
1026// Child Iterators for iterating over subexpressions/substatements
1027//===----------------------------------------------------------------------===//
1028
1029// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001030Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
1031Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001032
Steve Naroff7779db42007-11-12 14:29:37 +00001033// ObjCIvarRefExpr
1034Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
1035Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
1036
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001037// PreDefinedExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001038Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
1039Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001040
1041// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001042Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
1043Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001044
1045// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001046Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1047Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001048
1049// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001050Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1051Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001052
Chris Lattner5d661452007-08-26 03:42:43 +00001053// ImaginaryLiteral
1054Stmt::child_iterator ImaginaryLiteral::child_begin() {
1055 return reinterpret_cast<Stmt**>(&Val);
1056}
1057Stmt::child_iterator ImaginaryLiteral::child_end() {
1058 return reinterpret_cast<Stmt**>(&Val)+1;
1059}
1060
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001061// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001062Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1063Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001064
1065// ParenExpr
1066Stmt::child_iterator ParenExpr::child_begin() {
1067 return reinterpret_cast<Stmt**>(&Val);
1068}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001069Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001070 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001071}
1072
1073// UnaryOperator
1074Stmt::child_iterator UnaryOperator::child_begin() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001075 return reinterpret_cast<Stmt**>(&Val);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001076}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001077Stmt::child_iterator UnaryOperator::child_end() {
Ted Kremenekf816f772007-12-15 00:39:18 +00001078 return reinterpret_cast<Stmt**>(&Val+1);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001079}
1080
1081// SizeOfAlignOfTypeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001082Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001083 // If the type is a VLA type (and not a typedef), the size expression of the
1084 // VLA needs to be treated as an executable expression.
1085 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1086 return child_iterator(T);
1087 else
1088 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001089}
1090Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001091 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001092}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001093
1094// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001095Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001096 return reinterpret_cast<Stmt**>(&SubExprs);
1097}
Ted Kremenek1237c672007-08-24 20:06:47 +00001098Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001099 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001100}
1101
1102// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001103Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001104 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001105}
Ted Kremenek1237c672007-08-24 20:06:47 +00001106Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001107 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001108}
Ted Kremenek1237c672007-08-24 20:06:47 +00001109
1110// MemberExpr
1111Stmt::child_iterator MemberExpr::child_begin() {
1112 return reinterpret_cast<Stmt**>(&Base);
1113}
Ted Kremenek1237c672007-08-24 20:06:47 +00001114Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001115 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001116}
1117
1118// OCUVectorElementExpr
1119Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1120 return reinterpret_cast<Stmt**>(&Base);
1121}
Ted Kremenek1237c672007-08-24 20:06:47 +00001122Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001123 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001124}
1125
1126// CompoundLiteralExpr
1127Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1128 return reinterpret_cast<Stmt**>(&Init);
1129}
Ted Kremenek1237c672007-08-24 20:06:47 +00001130Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001131 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001132}
1133
1134// ImplicitCastExpr
1135Stmt::child_iterator ImplicitCastExpr::child_begin() {
1136 return reinterpret_cast<Stmt**>(&Op);
1137}
Ted Kremenek1237c672007-08-24 20:06:47 +00001138Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001139 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001140}
1141
1142// CastExpr
1143Stmt::child_iterator CastExpr::child_begin() {
1144 return reinterpret_cast<Stmt**>(&Op);
1145}
Ted Kremenek1237c672007-08-24 20:06:47 +00001146Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001147 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001148}
1149
1150// BinaryOperator
1151Stmt::child_iterator BinaryOperator::child_begin() {
1152 return reinterpret_cast<Stmt**>(&SubExprs);
1153}
Ted Kremenek1237c672007-08-24 20:06:47 +00001154Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001155 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001156}
1157
1158// ConditionalOperator
1159Stmt::child_iterator ConditionalOperator::child_begin() {
1160 return reinterpret_cast<Stmt**>(&SubExprs);
1161}
Ted Kremenek1237c672007-08-24 20:06:47 +00001162Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001163 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001164}
1165
1166// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001167Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1168Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001169
Ted Kremenek1237c672007-08-24 20:06:47 +00001170// StmtExpr
1171Stmt::child_iterator StmtExpr::child_begin() {
1172 return reinterpret_cast<Stmt**>(&SubStmt);
1173}
Ted Kremenek1237c672007-08-24 20:06:47 +00001174Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001175 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001176}
1177
1178// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001179Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1180 return child_iterator();
1181}
1182
1183Stmt::child_iterator TypesCompatibleExpr::child_end() {
1184 return child_iterator();
1185}
Ted Kremenek1237c672007-08-24 20:06:47 +00001186
1187// ChooseExpr
1188Stmt::child_iterator ChooseExpr::child_begin() {
1189 return reinterpret_cast<Stmt**>(&SubExprs);
1190}
1191
1192Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001193 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001194}
1195
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001196// VAArgExpr
1197Stmt::child_iterator VAArgExpr::child_begin() {
1198 return reinterpret_cast<Stmt**>(&Val);
1199}
1200
1201Stmt::child_iterator VAArgExpr::child_end() {
1202 return reinterpret_cast<Stmt**>(&Val)+1;
1203}
1204
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001205// InitListExpr
1206Stmt::child_iterator InitListExpr::child_begin() {
1207 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1208}
1209Stmt::child_iterator InitListExpr::child_end() {
1210 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1211}
1212
Ted Kremenek1237c672007-08-24 20:06:47 +00001213// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001214Stmt::child_iterator ObjCStringLiteral::child_begin() {
1215 return child_iterator();
1216}
1217Stmt::child_iterator ObjCStringLiteral::child_end() {
1218 return child_iterator();
1219}
Ted Kremenek1237c672007-08-24 20:06:47 +00001220
1221// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001222Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1223Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001224
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001225// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001226Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1227 return child_iterator();
1228}
1229Stmt::child_iterator ObjCSelectorExpr::child_end() {
1230 return child_iterator();
1231}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001232
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001233// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001234Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1235 return child_iterator();
1236}
1237Stmt::child_iterator ObjCProtocolExpr::child_end() {
1238 return child_iterator();
1239}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001240
Steve Naroff563477d2007-09-18 23:55:05 +00001241// ObjCMessageExpr
1242Stmt::child_iterator ObjCMessageExpr::child_begin() {
1243 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1244}
1245Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff68d331a2007-09-27 14:38:14 +00001246 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroff563477d2007-09-18 23:55:05 +00001247}
1248