blob: 5871a5c8682551f887967241253b8ef27293308b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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
Steve Naroff13b7c5f2007-08-08 22:15:55 +000091bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
92 // The following enum mimics gcc's internal "typeclass.h" file.
93 enum gcc_type_class {
94 no_type_class = -1,
95 void_type_class, integer_type_class, char_type_class,
96 enumeral_type_class, boolean_type_class,
97 pointer_type_class, reference_type_class, offset_type_class,
98 real_type_class, complex_type_class,
99 function_type_class, method_type_class,
100 record_type_class, union_type_class,
101 array_type_class, string_type_class,
102 lang_type_class
103 };
104 Result.setIsSigned(true);
105
106 // All simple function calls (e.g. func()) are implicitly cast to pointer to
107 // function. As a result, we try and obtain the DeclRefExpr from the
108 // ImplicitCastExpr.
109 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
110 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
111 return false;
112 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
113 if (!DRE)
114 return false;
115
116 // We have a DeclRefExpr.
117 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
118 // If no argument was supplied, default to "no_type_class". This isn't
119 // ideal, however it's what gcc does.
120 Result = static_cast<uint64_t>(no_type_class);
121 if (NumArgs >= 1) {
122 QualType argType = getArg(0)->getType();
123
124 if (argType->isVoidType())
125 Result = void_type_class;
126 else if (argType->isEnumeralType())
127 Result = enumeral_type_class;
128 else if (argType->isBooleanType())
129 Result = boolean_type_class;
130 else if (argType->isCharType())
131 Result = string_type_class; // gcc doesn't appear to use char_type_class
132 else if (argType->isIntegerType())
133 Result = integer_type_class;
134 else if (argType->isPointerType())
135 Result = pointer_type_class;
136 else if (argType->isReferenceType())
137 Result = reference_type_class;
138 else if (argType->isRealType())
139 Result = real_type_class;
140 else if (argType->isComplexType())
141 Result = complex_type_class;
142 else if (argType->isFunctionType())
143 Result = function_type_class;
144 else if (argType->isStructureType())
145 Result = record_type_class;
146 else if (argType->isUnionType())
147 Result = union_type_class;
148 else if (argType->isArrayType())
149 Result = array_type_class;
150 else if (argType->isUnionType())
151 Result = union_type_class;
152 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
Chris Lattner3ef5bc02007-11-08 17:56:40 +0000153 assert(0 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000154 }
155 return true;
156 }
157 return false;
158}
159
Reid Spencer5f016e22007-07-11 17:01:13 +0000160/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
161/// corresponds to, e.g. "<<=".
162const char *BinaryOperator::getOpcodeStr(Opcode Op) {
163 switch (Op) {
164 default: assert(0 && "Unknown binary operator");
165 case Mul: return "*";
166 case Div: return "/";
167 case Rem: return "%";
168 case Add: return "+";
169 case Sub: return "-";
170 case Shl: return "<<";
171 case Shr: return ">>";
172 case LT: return "<";
173 case GT: return ">";
174 case LE: return "<=";
175 case GE: return ">=";
176 case EQ: return "==";
177 case NE: return "!=";
178 case And: return "&";
179 case Xor: return "^";
180 case Or: return "|";
181 case LAnd: return "&&";
182 case LOr: return "||";
183 case Assign: return "=";
184 case MulAssign: return "*=";
185 case DivAssign: return "/=";
186 case RemAssign: return "%=";
187 case AddAssign: return "+=";
188 case SubAssign: return "-=";
189 case ShlAssign: return "<<=";
190 case ShrAssign: return ">>=";
191 case AndAssign: return "&=";
192 case XorAssign: return "^=";
193 case OrAssign: return "|=";
194 case Comma: return ",";
195 }
196}
197
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000198InitListExpr::InitListExpr(SourceLocation lbraceloc,
199 Expr **initexprs, unsigned numinits,
200 SourceLocation rbraceloc)
201 : Expr(InitListExprClass, QualType())
202 , NumInits(numinits)
203 , LBraceLoc(lbraceloc)
204 , RBraceLoc(rbraceloc)
205{
206 InitExprs = new Expr*[numinits];
207 for (unsigned i = 0; i != numinits; i++)
208 InitExprs[i] = initexprs[i];
209}
Reid Spencer5f016e22007-07-11 17:01:13 +0000210
211//===----------------------------------------------------------------------===//
212// Generic Expression Routines
213//===----------------------------------------------------------------------===//
214
215/// hasLocalSideEffect - Return true if this immediate expression has side
216/// effects, not counting any sub-expressions.
217bool Expr::hasLocalSideEffect() const {
218 switch (getStmtClass()) {
219 default:
220 return false;
221 case ParenExprClass:
222 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
223 case UnaryOperatorClass: {
224 const UnaryOperator *UO = cast<UnaryOperator>(this);
225
226 switch (UO->getOpcode()) {
227 default: return false;
228 case UnaryOperator::PostInc:
229 case UnaryOperator::PostDec:
230 case UnaryOperator::PreInc:
231 case UnaryOperator::PreDec:
232 return true; // ++/--
233
234 case UnaryOperator::Deref:
235 // Dereferencing a volatile pointer is a side-effect.
236 return getType().isVolatileQualified();
237 case UnaryOperator::Real:
238 case UnaryOperator::Imag:
239 // accessing a piece of a volatile complex is a side-effect.
240 return UO->getSubExpr()->getType().isVolatileQualified();
241
242 case UnaryOperator::Extension:
243 return UO->getSubExpr()->hasLocalSideEffect();
244 }
245 }
Chris Lattnere7716e62007-12-01 06:07:34 +0000246 case BinaryOperatorClass: {
247 const BinaryOperator *BinOp = cast<BinaryOperator>(this);
248 // Consider comma to have side effects if the LHS and RHS both do.
249 if (BinOp->getOpcode() == BinaryOperator::Comma)
250 return BinOp->getLHS()->hasLocalSideEffect() &&
251 BinOp->getRHS()->hasLocalSideEffect();
252
253 return BinOp->isAssignmentOp();
254 }
Chris Lattnereb14fe82007-08-25 02:00:02 +0000255 case CompoundAssignOperatorClass:
Chris Lattner1f683e92007-08-25 01:55:00 +0000256 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000257
258 case MemberExprClass:
259 case ArraySubscriptExprClass:
260 // If the base pointer or element is to a volatile pointer/field, accessing
261 // if is a side effect.
262 return getType().isVolatileQualified();
263
264 case CallExprClass:
265 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
266 // should warn.
267 return true;
Chris Lattnera9c01022007-09-26 22:06:30 +0000268 case ObjCMessageExprClass:
269 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000270
271 case CastExprClass:
272 // If this is a cast to void, check the operand. Otherwise, the result of
273 // the cast is unused.
274 if (getType()->isVoidType())
275 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
276 return false;
277 }
278}
279
280/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
281/// incomplete type other than void. Nonarray expressions that can be lvalues:
282/// - name, where name must be a variable
283/// - e[i]
284/// - (e), where e must be an lvalue
285/// - e.name, where e must be an lvalue
286/// - e->name
287/// - *e, the type of e cannot be a function type
288/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000289/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000290/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000291///
Bill Wendlingca51c972007-07-16 07:07:56 +0000292Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000293 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000294 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 return LV_NotObjectType;
296
Steve Naroff731ec572007-07-21 13:32:03 +0000297 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000299
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000300 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000301 return LV_Valid;
302
Reid Spencer5f016e22007-07-11 17:01:13 +0000303 // the type looks fine, now check the expression
304 switch (getStmtClass()) {
305 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000306 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
308 // For vectors, make sure base is an lvalue (i.e. not a function call).
309 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
310 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
311 return LV_Valid;
312 case DeclRefExprClass: // C99 6.5.1p2
313 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
314 return LV_Valid;
315 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000316 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 const MemberExpr *m = cast<MemberExpr>(this);
318 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000319 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000320 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000322 return LV_Valid; // C99 6.5.3p4
323
324 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
325 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
326 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 break;
328 case ParenExprClass: // C99 6.5.1p5
329 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattner6481a572007-08-03 17:31:20 +0000330 case OCUVectorElementExprClass:
331 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000332 return LV_DuplicateVectorComponents;
333 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000334 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
335 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000336 default:
337 break;
338 }
339 return LV_InvalidExpression;
340}
341
342/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
343/// does not have an incomplete type, does not have a const-qualified type, and
344/// if it is a structure or union, does not have any member (including,
345/// recursively, any member or element of all contained aggregates or unions)
346/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000347Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 isLvalueResult lvalResult = isLvalue();
349
350 switch (lvalResult) {
351 case LV_Valid: break;
352 case LV_NotObjectType: return MLV_NotObjectType;
353 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000354 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 case LV_InvalidExpression: return MLV_InvalidExpression;
356 }
357 if (TR.isConstQualified())
358 return MLV_ConstQualified;
359 if (TR->isArrayType())
360 return MLV_ArrayType;
361 if (TR->isIncompleteType())
362 return MLV_IncompleteType;
363
364 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
365 if (r->hasConstFields())
366 return MLV_ConstQualified;
367 }
368 return MLV_Valid;
369}
370
Chris Lattner4cc62712007-11-27 21:35:27 +0000371/// hasStaticStorage - Return true if this expression has static storage
372/// duration. This means that the address of this expression is a link-time
373/// constant.
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000374bool Expr::hasStaticStorage() const {
375 switch (getStmtClass()) {
376 default:
377 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000378 case ParenExprClass:
379 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
380 case ImplicitCastExprClass:
381 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000382 case DeclRefExprClass: {
383 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
384 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
385 return VD->hasStaticStorage();
386 return false;
387 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000388 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000389 const MemberExpr *M = cast<MemberExpr>(this);
390 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000391 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000392 case ArraySubscriptExprClass:
393 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000394 }
395}
396
Steve Naroff38374b02007-09-02 20:30:18 +0000397bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff38374b02007-09-02 20:30:18 +0000398 switch (getStmtClass()) {
399 default:
400 if (Loc) *Loc = getLocStart();
401 return false;
402 case ParenExprClass:
403 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
404 case StringLiteralClass:
Steve Naroff5d37e322007-11-09 15:00:03 +0000405 case ObjCStringLiteralClass:
Steve Naroff38374b02007-09-02 20:30:18 +0000406 case FloatingLiteralClass:
407 case IntegerLiteralClass:
408 case CharacterLiteralClass:
409 case ImaginaryLiteralClass:
Anders Carlsson1a86b332007-10-17 00:52:43 +0000410 case TypesCompatibleExprClass:
411 case CXXBoolLiteralExprClass:
Chris Lattner2777e492007-10-18 00:20:32 +0000412 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000413 case CallExprClass: {
414 const CallExpr *CE = cast<CallExpr>(this);
415 llvm::APSInt Result(32);
Hartmut Kaiser86fd3552007-09-16 21:35:35 +0000416 Result.zextOrTrunc(
417 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff38374b02007-09-02 20:30:18 +0000418 if (CE->isBuiltinClassifyType(Result))
Chris Lattner2777e492007-10-18 00:20:32 +0000419 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000420 if (Loc) *Loc = getLocStart();
421 return false;
422 }
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000423 case DeclRefExprClass: {
424 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
425 // Accept address of function.
426 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner2777e492007-10-18 00:20:32 +0000427 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000428 if (Loc) *Loc = getLocStart();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000429 if (isa<VarDecl>(D))
430 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000431 return false;
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000432 }
Steve Naroff38374b02007-09-02 20:30:18 +0000433 case UnaryOperatorClass: {
434 const UnaryOperator *Exp = cast<UnaryOperator>(this);
435
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000436 // C99 6.6p9
437 if (Exp->getOpcode() == UnaryOperator::AddrOf)
438 return Exp->getSubExpr()->hasStaticStorage();
439
Steve Naroff38374b02007-09-02 20:30:18 +0000440 // Get the operand value. If this is sizeof/alignof, do not evalute the
441 // operand. This affects C99 6.6p3.
442 if (!Exp->isSizeOfAlignOfOp() &&
443 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
444 return false;
445
446 switch (Exp->getOpcode()) {
447 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
448 // See C99 6.6p3.
449 default:
450 if (Loc) *Loc = Exp->getOperatorLoc();
451 return false;
452 case UnaryOperator::Extension:
453 return true; // FIXME: this is wrong.
454 case UnaryOperator::SizeOf:
455 case UnaryOperator::AlignOf:
456 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
457 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
458 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000459 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000460 case UnaryOperator::LNot:
461 case UnaryOperator::Plus:
462 case UnaryOperator::Minus:
463 case UnaryOperator::Not:
Chris Lattner2777e492007-10-18 00:20:32 +0000464 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000465 }
Steve Naroff38374b02007-09-02 20:30:18 +0000466 }
467 case SizeOfAlignOfTypeExprClass: {
468 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
469 // alignof always evaluates to a constant.
470 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
471 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000472 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000473 }
474 case BinaryOperatorClass: {
475 const BinaryOperator *Exp = cast<BinaryOperator>(this);
476
477 // The LHS of a constant expr is always evaluated and needed.
478 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
479 return false;
480
481 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
482 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000483 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000484 }
485 case ImplicitCastExprClass:
486 case CastExprClass: {
487 const Expr *SubExpr;
488 SourceLocation CastLoc;
489 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
490 SubExpr = C->getSubExpr();
491 CastLoc = C->getLParenLoc();
492 } else {
493 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
494 CastLoc = getLocStart();
495 }
496 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
497 if (Loc) *Loc = SubExpr->getLocStart();
498 return false;
499 }
Chris Lattner2777e492007-10-18 00:20:32 +0000500 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000501 }
502 case ConditionalOperatorClass: {
503 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner2777e492007-10-18 00:20:32 +0000504 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson39073232007-11-30 19:04:31 +0000505 // Handle the GNU extension for missing LHS.
506 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner2777e492007-10-18 00:20:32 +0000507 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff38374b02007-09-02 20:30:18 +0000508 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000509 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000510 }
511 }
512
513 return true;
514}
515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516/// isIntegerConstantExpr - this recursive routine will test if an expression is
517/// an integer constant expression. Note: With the introduction of VLA's in
518/// C99 the result of the sizeof operator is no longer always a constant
519/// expression. The generalization of the wording to include any subexpression
520/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
521/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
522/// "0 || f()" can be treated as a constant expression. In C90 this expression,
523/// occurring in a context requiring a constant, would have been a constraint
524/// violation. FIXME: This routine currently implements C90 semantics.
525/// To properly implement C99 semantics this routine will need to evaluate
526/// expressions involving operators previously mentioned.
527
528/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
529/// comma, etc
530///
531/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000532/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000533///
534/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
535/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
536/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000537bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
538 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 switch (getStmtClass()) {
540 default:
541 if (Loc) *Loc = getLocStart();
542 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 case ParenExprClass:
544 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000545 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 case IntegerLiteralClass:
547 Result = cast<IntegerLiteral>(this)->getValue();
548 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000549 case CharacterLiteralClass: {
550 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000551 Result.zextOrTrunc(
552 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000553 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000554 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000555 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000556 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000557 case TypesCompatibleExprClass: {
558 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000559 Result.zextOrTrunc(
560 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffec0550f2007-10-15 20:41:53 +0000561 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff389cecc2007-08-02 00:13:27 +0000562 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000563 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000564 case CallExprClass: {
565 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000566 Result.zextOrTrunc(
567 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000568 if (CE->isBuiltinClassifyType(Result))
569 break;
570 if (Loc) *Loc = getLocStart();
571 return false;
572 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 case DeclRefExprClass:
574 if (const EnumConstantDecl *D =
575 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
576 Result = D->getInitVal();
577 break;
578 }
579 if (Loc) *Loc = getLocStart();
580 return false;
581 case UnaryOperatorClass: {
582 const UnaryOperator *Exp = cast<UnaryOperator>(this);
583
584 // Get the operand value. If this is sizeof/alignof, do not evalute the
585 // operand. This affects C99 6.6p3.
Chris Lattner602dafd2007-08-23 21:42:50 +0000586 if (!Exp->isSizeOfAlignOfOp() &&
587 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 return false;
589
590 switch (Exp->getOpcode()) {
591 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
592 // See C99 6.6p3.
593 default:
594 if (Loc) *Loc = Exp->getOperatorLoc();
595 return false;
596 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000597 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000598 case UnaryOperator::SizeOf:
599 case UnaryOperator::AlignOf:
600 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner590b6642007-07-15 23:26:56 +0000601 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 return false;
603
Chris Lattner76e773a2007-07-18 18:38:36 +0000604 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000605 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000606 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
607 Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000608
609 // Get information about the size or align.
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000610 if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner76e773a2007-07-18 18:38:36 +0000611 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
612 Exp->getOperatorLoc());
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000613 } else {
614 unsigned CharSize = Ctx.Target.getCharWidth(Exp->getOperatorLoc());
615 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
616 Exp->getOperatorLoc()) / CharSize;
617 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 break;
619 case UnaryOperator::LNot: {
620 bool Val = Result != 0;
Chris Lattner701e5eb2007-09-04 02:45:27 +0000621 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000622 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
623 Exp->getOperatorLoc())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 Result = Val;
625 break;
626 }
627 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 break;
629 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 Result = -Result;
631 break;
632 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 Result = ~Result;
634 break;
635 }
636 break;
637 }
638 case SizeOfAlignOfTypeExprClass: {
639 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
640 // alignof always evaluates to a constant.
Chris Lattner590b6642007-07-15 23:26:56 +0000641 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 return false;
643
Chris Lattner76e773a2007-07-18 18:38:36 +0000644 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000645 Result.zextOrTrunc(
646 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000647
648 // Get information about the size or align.
649 if (Exp->isSizeOf())
650 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
651 else
652 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 break;
654 }
655 case BinaryOperatorClass: {
656 const BinaryOperator *Exp = cast<BinaryOperator>(this);
657
658 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000659 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000660 return false;
661
662 llvm::APSInt RHS(Result);
663
664 // The short-circuiting &&/|| operators don't necessarily evaluate their
665 // RHS. Make sure to pass isEvaluated down correctly.
666 if (Exp->isLogicalOp()) {
667 bool RHSEval;
668 if (Exp->getOpcode() == BinaryOperator::LAnd)
669 RHSEval = Result != 0;
670 else {
671 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
672 RHSEval = Result == 0;
673 }
674
Chris Lattner590b6642007-07-15 23:26:56 +0000675 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 isEvaluated & RHSEval))
677 return false;
678 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000679 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 return false;
681 }
682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 switch (Exp->getOpcode()) {
684 default:
685 if (Loc) *Loc = getLocStart();
686 return false;
687 case BinaryOperator::Mul:
688 Result *= RHS;
689 break;
690 case BinaryOperator::Div:
691 if (RHS == 0) {
692 if (!isEvaluated) break;
693 if (Loc) *Loc = getLocStart();
694 return false;
695 }
696 Result /= RHS;
697 break;
698 case BinaryOperator::Rem:
699 if (RHS == 0) {
700 if (!isEvaluated) break;
701 if (Loc) *Loc = getLocStart();
702 return false;
703 }
704 Result %= RHS;
705 break;
706 case BinaryOperator::Add: Result += RHS; break;
707 case BinaryOperator::Sub: Result -= RHS; break;
708 case BinaryOperator::Shl:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000709 Result <<=
710 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 break;
712 case BinaryOperator::Shr:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000713 Result >>=
714 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 break;
716 case BinaryOperator::LT: Result = Result < RHS; break;
717 case BinaryOperator::GT: Result = Result > RHS; break;
718 case BinaryOperator::LE: Result = Result <= RHS; break;
719 case BinaryOperator::GE: Result = Result >= RHS; break;
720 case BinaryOperator::EQ: Result = Result == RHS; break;
721 case BinaryOperator::NE: Result = Result != RHS; break;
722 case BinaryOperator::And: Result &= RHS; break;
723 case BinaryOperator::Xor: Result ^= RHS; break;
724 case BinaryOperator::Or: Result |= RHS; break;
725 case BinaryOperator::LAnd:
726 Result = Result != 0 && RHS != 0;
727 break;
728 case BinaryOperator::LOr:
729 Result = Result != 0 || RHS != 0;
730 break;
731
732 case BinaryOperator::Comma:
733 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
734 // *except* when they are contained within a subexpression that is not
735 // evaluated". Note that Assignment can never happen due to constraints
736 // on the LHS subexpr, so we don't need to check it here.
737 if (isEvaluated) {
738 if (Loc) *Loc = getLocStart();
739 return false;
740 }
741
742 // The result of the constant expr is the RHS.
743 Result = RHS;
744 return true;
745 }
746
747 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
748 break;
749 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000750 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000752 const Expr *SubExpr;
753 SourceLocation CastLoc;
754 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
755 SubExpr = C->getSubExpr();
756 CastLoc = C->getLParenLoc();
757 } else {
758 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
759 CastLoc = getLocStart();
760 }
761
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000763 if (!SubExpr->getType()->isArithmeticType() ||
764 !getType()->isIntegerType()) {
765 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 return false;
767 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000768
769 uint32_t DestWidth =
770 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
771
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000773 if (SubExpr->getType()->isIntegerType()) {
774 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000776
777 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000778 // If the input is signed, do a sign extend, noop, or truncate.
779 if (SubExpr->getType()->isSignedIntegerType())
780 Result.sextOrTrunc(DestWidth);
781 else // If the input is unsigned, do a zero extend, noop, or truncate.
782 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 break;
784 }
785
786 // Allow floating constants that are the immediate operands of casts or that
787 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000788 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
790 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +0000791
792 // If this isn't a floating literal, we can't handle it.
793 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
794 if (!FL) {
795 if (Loc) *Loc = Operand->getLocStart();
796 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000798
799 // Determine whether we are converting to unsigned or signed.
800 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +0000801
802 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
803 // be called multiple times per AST.
Chris Lattner987b15d2007-09-22 19:04:13 +0000804 uint64_t Space[4];
Chris Lattnerccc213f2007-09-26 00:47:26 +0000805 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
806 llvm::APFloat::rmTowardZero);
Chris Lattner987b15d2007-09-22 19:04:13 +0000807 Result = llvm::APInt(DestWidth, 4, Space);
808 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 }
810 case ConditionalOperatorClass: {
811 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
812
Chris Lattner590b6642007-07-15 23:26:56 +0000813 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 return false;
815
816 const Expr *TrueExp = Exp->getLHS();
817 const Expr *FalseExp = Exp->getRHS();
818 if (Result == 0) std::swap(TrueExp, FalseExp);
819
820 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000821 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 return false;
823 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000824 if (TrueExp &&
825 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 break;
828 }
829 }
830
831 // Cases that are valid constant exprs fall through to here.
832 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
833 return true;
834}
835
836
837/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
838/// integer constant expression with the value zero, or if this is one that is
839/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000840bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000841 // Strip off a cast to void*, if it exists.
842 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
843 // Check that it is a cast to void*.
844 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
845 QualType Pointee = PT->getPointeeType();
846 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
847 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner590b6642007-07-15 23:26:56 +0000848 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 }
Steve Naroff7269f2d2007-08-28 21:20:34 +0000850 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff19a6ebd2007-08-29 00:00:02 +0000851 // Ignore the ImplicitCastExpr type entirely.
852 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
854 // Accept ((void*)0) as a null pointer constant, as many other
855 // implementations do.
Chris Lattner590b6642007-07-15 23:26:56 +0000856 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 }
858
859 // This expression must be an integer type.
860 if (!getType()->isIntegerType())
861 return false;
862
863 // If we have an integer constant expression, we need to *evaluate* it and
864 // test for the value 0.
865 llvm::APSInt Val(32);
Chris Lattner590b6642007-07-15 23:26:56 +0000866 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000867}
Steve Naroff31a45842007-07-28 23:10:27 +0000868
Chris Lattner6481a572007-08-03 17:31:20 +0000869unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000870 return strlen(Accessor.getName());
871}
872
873
Chris Lattnercb92a112007-08-02 21:47:28 +0000874/// getComponentType - Determine whether the components of this access are
875/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +0000876OCUVectorElementExpr::ElementType
877OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +0000878 // derive the component type, no need to waste space.
879 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000880
Chris Lattner88dca042007-08-02 22:33:49 +0000881 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
882 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000883
Chris Lattner88dca042007-08-02 22:33:49 +0000884 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000885 "getComponentType(): Illegal accessor");
886 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000887}
Steve Narofffec0b492007-07-30 03:29:09 +0000888
Chris Lattner6481a572007-08-03 17:31:20 +0000889/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +0000890/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +0000891bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +0000892 const char *compStr = Accessor.getName();
893 unsigned length = strlen(compStr);
894
895 for (unsigned i = 0; i < length-1; i++) {
896 const char *s = compStr+i;
897 for (const char c = *s++; *s; s++)
898 if (c == *s)
899 return true;
900 }
901 return false;
902}
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000903
904/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +0000905unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000906 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +0000907 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000908
909 unsigned Result = 0;
910
911 while (length--) {
912 Result <<= 2;
913 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
914 assert(Idx != -1 && "Invalid accessor letter");
915 Result |= Idx;
916 }
917 return Result;
918}
919
Steve Naroff68d331a2007-09-27 14:38:14 +0000920// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +0000921ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroffdb611d52007-11-03 16:37:59 +0000922 QualType retType, ObjcMethodDecl *mproto,
923 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +0000924 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +0000925 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
926 MethodProto(mproto), ClassName(0) {
Steve Naroff49f109c2007-11-15 13:05:42 +0000927 NumArgs = nargs;
928 SubExprs = new Expr*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +0000929 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +0000930 if (NumArgs) {
931 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +0000932 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
933 }
Steve Naroff563477d2007-09-18 23:55:05 +0000934 LBracloc = LBrac;
935 RBracloc = RBrac;
936}
937
Steve Naroff68d331a2007-09-27 14:38:14 +0000938// constructor for class messages.
939// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +0000940ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroffdb611d52007-11-03 16:37:59 +0000941 QualType retType, ObjcMethodDecl *mproto,
942 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +0000943 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +0000944 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
945 MethodProto(mproto), ClassName(clsName) {
Steve Naroff49f109c2007-11-15 13:05:42 +0000946 NumArgs = nargs;
947 SubExprs = new Expr*[NumArgs+1];
Steve Naroff563477d2007-09-18 23:55:05 +0000948 SubExprs[RECEIVER] = 0;
Steve Naroff49f109c2007-11-15 13:05:42 +0000949 if (NumArgs) {
950 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +0000951 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
952 }
Steve Naroff563477d2007-09-18 23:55:05 +0000953 LBracloc = LBrac;
954 RBracloc = RBrac;
955}
956
Chris Lattner27437ca2007-10-25 00:29:32 +0000957
958bool ChooseExpr::isConditionTrue(ASTContext &C) const {
959 llvm::APSInt CondVal(32);
960 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
961 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
962 return CondVal != 0;
963}
964
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000965//===----------------------------------------------------------------------===//
966// Child Iterators for iterating over subexpressions/substatements
967//===----------------------------------------------------------------------===//
968
969// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +0000970Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
971Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000972
Steve Naroff7779db42007-11-12 14:29:37 +0000973// ObjCIvarRefExpr
974Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
975Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
976
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000977// PreDefinedExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +0000978Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
979Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000980
981// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +0000982Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
983Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000984
985// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +0000986Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
987Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000988
989// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +0000990Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
991Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000992
Chris Lattner5d661452007-08-26 03:42:43 +0000993// ImaginaryLiteral
994Stmt::child_iterator ImaginaryLiteral::child_begin() {
995 return reinterpret_cast<Stmt**>(&Val);
996}
997Stmt::child_iterator ImaginaryLiteral::child_end() {
998 return reinterpret_cast<Stmt**>(&Val)+1;
999}
1000
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001001// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001002Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1003Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001004
1005// ParenExpr
1006Stmt::child_iterator ParenExpr::child_begin() {
1007 return reinterpret_cast<Stmt**>(&Val);
1008}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001009Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001010 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001011}
1012
1013// UnaryOperator
1014Stmt::child_iterator UnaryOperator::child_begin() {
1015 return reinterpret_cast<Stmt**>(&Val);
1016}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001017Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001018 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001019}
1020
1021// SizeOfAlignOfTypeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001022Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
1023 return child_iterator();
1024}
1025Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
1026 return child_iterator();
1027}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001028
1029// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001030Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001031 return reinterpret_cast<Stmt**>(&SubExprs);
1032}
Ted Kremenek1237c672007-08-24 20:06:47 +00001033Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001034 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001035}
1036
1037// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001038Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001039 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001040}
Ted Kremenek1237c672007-08-24 20:06:47 +00001041Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001042 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001043}
Ted Kremenek1237c672007-08-24 20:06:47 +00001044
1045// MemberExpr
1046Stmt::child_iterator MemberExpr::child_begin() {
1047 return reinterpret_cast<Stmt**>(&Base);
1048}
Ted Kremenek1237c672007-08-24 20:06:47 +00001049Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001050 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001051}
1052
1053// OCUVectorElementExpr
1054Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1055 return reinterpret_cast<Stmt**>(&Base);
1056}
Ted Kremenek1237c672007-08-24 20:06:47 +00001057Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001058 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001059}
1060
1061// CompoundLiteralExpr
1062Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1063 return reinterpret_cast<Stmt**>(&Init);
1064}
Ted Kremenek1237c672007-08-24 20:06:47 +00001065Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001066 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001067}
1068
1069// ImplicitCastExpr
1070Stmt::child_iterator ImplicitCastExpr::child_begin() {
1071 return reinterpret_cast<Stmt**>(&Op);
1072}
Ted Kremenek1237c672007-08-24 20:06:47 +00001073Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001074 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001075}
1076
1077// CastExpr
1078Stmt::child_iterator CastExpr::child_begin() {
1079 return reinterpret_cast<Stmt**>(&Op);
1080}
Ted Kremenek1237c672007-08-24 20:06:47 +00001081Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001082 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001083}
1084
1085// BinaryOperator
1086Stmt::child_iterator BinaryOperator::child_begin() {
1087 return reinterpret_cast<Stmt**>(&SubExprs);
1088}
Ted Kremenek1237c672007-08-24 20:06:47 +00001089Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001090 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001091}
1092
1093// ConditionalOperator
1094Stmt::child_iterator ConditionalOperator::child_begin() {
1095 return reinterpret_cast<Stmt**>(&SubExprs);
1096}
Ted Kremenek1237c672007-08-24 20:06:47 +00001097Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001098 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001099}
1100
1101// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001102Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1103Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001104
Ted Kremenek1237c672007-08-24 20:06:47 +00001105// StmtExpr
1106Stmt::child_iterator StmtExpr::child_begin() {
1107 return reinterpret_cast<Stmt**>(&SubStmt);
1108}
Ted Kremenek1237c672007-08-24 20:06:47 +00001109Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001110 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001111}
1112
1113// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001114Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1115 return child_iterator();
1116}
1117
1118Stmt::child_iterator TypesCompatibleExpr::child_end() {
1119 return child_iterator();
1120}
Ted Kremenek1237c672007-08-24 20:06:47 +00001121
1122// ChooseExpr
1123Stmt::child_iterator ChooseExpr::child_begin() {
1124 return reinterpret_cast<Stmt**>(&SubExprs);
1125}
1126
1127Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001128 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001129}
1130
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001131// VAArgExpr
1132Stmt::child_iterator VAArgExpr::child_begin() {
1133 return reinterpret_cast<Stmt**>(&Val);
1134}
1135
1136Stmt::child_iterator VAArgExpr::child_end() {
1137 return reinterpret_cast<Stmt**>(&Val)+1;
1138}
1139
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001140// InitListExpr
1141Stmt::child_iterator InitListExpr::child_begin() {
1142 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1143}
1144Stmt::child_iterator InitListExpr::child_end() {
1145 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1146}
1147
Ted Kremenek1237c672007-08-24 20:06:47 +00001148// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001149Stmt::child_iterator ObjCStringLiteral::child_begin() {
1150 return child_iterator();
1151}
1152Stmt::child_iterator ObjCStringLiteral::child_end() {
1153 return child_iterator();
1154}
Ted Kremenek1237c672007-08-24 20:06:47 +00001155
1156// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001157Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1158Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001159
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001160// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001161Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1162 return child_iterator();
1163}
1164Stmt::child_iterator ObjCSelectorExpr::child_end() {
1165 return child_iterator();
1166}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001167
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001168// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001169Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1170 return child_iterator();
1171}
1172Stmt::child_iterator ObjCProtocolExpr::child_end() {
1173 return child_iterator();
1174}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001175
Steve Naroff563477d2007-09-18 23:55:05 +00001176// ObjCMessageExpr
1177Stmt::child_iterator ObjCMessageExpr::child_begin() {
1178 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1179}
1180Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff68d331a2007-09-27 14:38:14 +00001181 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroff563477d2007-09-18 23:55:05 +00001182}
1183