blob: 22767053db01a52b6006025037d46c5e0844876a [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
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +0000258 case ConditionalOperatorClass: {
259 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
260 return Exp->getCond()->hasLocalSideEffect()
261 || (Exp->getLHS() && Exp->getLHS()->hasLocalSideEffect())
262 || (Exp->getRHS() && Exp->getRHS()->hasLocalSideEffect());
263 }
264
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 case MemberExprClass:
266 case ArraySubscriptExprClass:
267 // If the base pointer or element is to a volatile pointer/field, accessing
268 // if is a side effect.
269 return getType().isVolatileQualified();
270
271 case CallExprClass:
272 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
273 // should warn.
274 return true;
Chris Lattnera9c01022007-09-26 22:06:30 +0000275 case ObjCMessageExprClass:
276 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000277
278 case CastExprClass:
279 // If this is a cast to void, check the operand. Otherwise, the result of
280 // the cast is unused.
281 if (getType()->isVoidType())
282 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
283 return false;
284 }
285}
286
287/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
288/// incomplete type other than void. Nonarray expressions that can be lvalues:
289/// - name, where name must be a variable
290/// - e[i]
291/// - (e), where e must be an lvalue
292/// - e.name, where e must be an lvalue
293/// - e->name
294/// - *e, the type of e cannot be a function type
295/// - string-constant
Chris Lattner7da36f62007-10-30 22:53:42 +0000296/// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000297/// - reference type [C++ [expr]]
Reid Spencer5f016e22007-07-11 17:01:13 +0000298///
Bill Wendlingca51c972007-07-16 07:07:56 +0000299Expr::isLvalueResult Expr::isLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000300 // first, check the type (C99 6.3.2.1)
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000301 if (TR->isFunctionType()) // from isObjectType()
Reid Spencer5f016e22007-07-11 17:01:13 +0000302 return LV_NotObjectType;
303
Steve Naroff731ec572007-07-21 13:32:03 +0000304 if (TR->isVoidType())
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 return LV_IncompleteVoidType;
Bill Wendling08ad47c2007-07-17 03:52:31 +0000306
Chris Lattnercb4f9a62007-07-21 05:33:26 +0000307 if (TR->isReferenceType()) // C++ [expr]
Bill Wendling08ad47c2007-07-17 03:52:31 +0000308 return LV_Valid;
309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // the type looks fine, now check the expression
311 switch (getStmtClass()) {
312 case StringLiteralClass: // C99 6.5.1p4
Anders Carlsson7323a622007-11-30 22:47:59 +0000313 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
315 // For vectors, make sure base is an lvalue (i.e. not a function call).
316 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
317 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
318 return LV_Valid;
319 case DeclRefExprClass: // C99 6.5.1p2
320 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
321 return LV_Valid;
322 break;
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000323 case MemberExprClass: { // C99 6.5.2.3p4
Reid Spencer5f016e22007-07-11 17:01:13 +0000324 const MemberExpr *m = cast<MemberExpr>(this);
325 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovfdd75662007-07-12 15:26:50 +0000326 }
Chris Lattner7da36f62007-10-30 22:53:42 +0000327 case UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000328 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
Chris Lattner7da36f62007-10-30 22:53:42 +0000329 return LV_Valid; // C99 6.5.3p4
330
331 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real ||
332 cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag)
333 return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(); // GNU.
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 break;
335 case ParenExprClass: // C99 6.5.1p5
336 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Steve Naroffe6386392007-12-05 04:00:10 +0000337 case CompoundLiteralExprClass: // C99 6.5.2.5p5
338 return LV_Valid;
Chris Lattner6481a572007-08-03 17:31:20 +0000339 case OCUVectorElementExprClass:
340 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Narofffec0b492007-07-30 03:29:09 +0000341 return LV_DuplicateVectorComponents;
342 return LV_Valid;
Steve Naroff027282d2007-11-12 14:34:27 +0000343 case ObjCIvarRefExprClass: // ObjC instance variables are lvalues.
344 return LV_Valid;
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 default:
346 break;
347 }
348 return LV_InvalidExpression;
349}
350
351/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
352/// does not have an incomplete type, does not have a const-qualified type, and
353/// if it is a structure or union, does not have any member (including,
354/// recursively, any member or element of all contained aggregates or unions)
355/// with a const-qualified type.
Bill Wendlingca51c972007-07-16 07:07:56 +0000356Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 isLvalueResult lvalResult = isLvalue();
358
359 switch (lvalResult) {
360 case LV_Valid: break;
361 case LV_NotObjectType: return MLV_NotObjectType;
362 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Narofffec0b492007-07-30 03:29:09 +0000363 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 case LV_InvalidExpression: return MLV_InvalidExpression;
365 }
366 if (TR.isConstQualified())
367 return MLV_ConstQualified;
368 if (TR->isArrayType())
369 return MLV_ArrayType;
370 if (TR->isIncompleteType())
371 return MLV_IncompleteType;
372
373 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
374 if (r->hasConstFields())
375 return MLV_ConstQualified;
376 }
377 return MLV_Valid;
378}
379
Chris Lattner4cc62712007-11-27 21:35:27 +0000380/// hasStaticStorage - Return true if this expression has static storage
381/// duration. This means that the address of this expression is a link-time
382/// constant.
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000383bool Expr::hasStaticStorage() const {
384 switch (getStmtClass()) {
385 default:
386 return false;
Chris Lattner4cc62712007-11-27 21:35:27 +0000387 case ParenExprClass:
388 return cast<ParenExpr>(this)->getSubExpr()->hasStaticStorage();
389 case ImplicitCastExprClass:
390 return cast<ImplicitCastExpr>(this)->getSubExpr()->hasStaticStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000391 case DeclRefExprClass: {
392 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
393 if (const VarDecl *VD = dyn_cast<VarDecl>(D))
394 return VD->hasStaticStorage();
395 return false;
396 }
Chris Lattnerfb708062007-11-28 04:30:09 +0000397 case MemberExprClass: {
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000398 const MemberExpr *M = cast<MemberExpr>(this);
399 return !M->isArrow() && M->getBase()->hasStaticStorage();
Chris Lattnerfb708062007-11-28 04:30:09 +0000400 }
Chris Lattner4cc62712007-11-27 21:35:27 +0000401 case ArraySubscriptExprClass:
402 return cast<ArraySubscriptExpr>(this)->getBase()->hasStaticStorage();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000403 }
404}
405
Steve Naroff38374b02007-09-02 20:30:18 +0000406bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
Steve Naroff38374b02007-09-02 20:30:18 +0000407 switch (getStmtClass()) {
408 default:
409 if (Loc) *Loc = getLocStart();
410 return false;
411 case ParenExprClass:
412 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
413 case StringLiteralClass:
Steve Naroff5d37e322007-11-09 15:00:03 +0000414 case ObjCStringLiteralClass:
Steve Naroff38374b02007-09-02 20:30:18 +0000415 case FloatingLiteralClass:
416 case IntegerLiteralClass:
417 case CharacterLiteralClass:
418 case ImaginaryLiteralClass:
Anders Carlsson1a86b332007-10-17 00:52:43 +0000419 case TypesCompatibleExprClass:
420 case CXXBoolLiteralExprClass:
Chris Lattner2777e492007-10-18 00:20:32 +0000421 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000422 case CallExprClass: {
423 const CallExpr *CE = cast<CallExpr>(this);
424 llvm::APSInt Result(32);
Hartmut Kaiser86fd3552007-09-16 21:35:35 +0000425 Result.zextOrTrunc(
426 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff38374b02007-09-02 20:30:18 +0000427 if (CE->isBuiltinClassifyType(Result))
Chris Lattner2777e492007-10-18 00:20:32 +0000428 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000429 if (Loc) *Loc = getLocStart();
430 return false;
431 }
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000432 case DeclRefExprClass: {
433 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
434 // Accept address of function.
435 if (isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D))
Chris Lattner2777e492007-10-18 00:20:32 +0000436 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000437 if (Loc) *Loc = getLocStart();
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000438 if (isa<VarDecl>(D))
439 return TR->isArrayType();
Steve Naroff38374b02007-09-02 20:30:18 +0000440 return false;
Chris Lattner4ef8dd62007-11-01 02:45:17 +0000441 }
Steve Naroff38374b02007-09-02 20:30:18 +0000442 case UnaryOperatorClass: {
443 const UnaryOperator *Exp = cast<UnaryOperator>(this);
444
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000445 // C99 6.6p9
Chris Lattner239c15e2007-12-11 23:11:17 +0000446 if (Exp->getOpcode() == UnaryOperator::AddrOf) {
447 if (!Exp->getSubExpr()->hasStaticStorage()) {
448 if (Loc) *Loc = getLocStart();
449 return false;
450 }
451 return true;
452 }
Chris Lattner1d09ecc2007-11-13 18:05:45 +0000453
Steve Naroff38374b02007-09-02 20:30:18 +0000454 // Get the operand value. If this is sizeof/alignof, do not evalute the
455 // operand. This affects C99 6.6p3.
456 if (!Exp->isSizeOfAlignOfOp() &&
457 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
458 return false;
459
460 switch (Exp->getOpcode()) {
461 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
462 // See C99 6.6p3.
463 default:
464 if (Loc) *Loc = Exp->getOperatorLoc();
465 return false;
466 case UnaryOperator::Extension:
467 return true; // FIXME: this is wrong.
468 case UnaryOperator::SizeOf:
469 case UnaryOperator::AlignOf:
470 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
471 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
472 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000473 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000474 case UnaryOperator::LNot:
475 case UnaryOperator::Plus:
476 case UnaryOperator::Minus:
477 case UnaryOperator::Not:
Chris Lattner2777e492007-10-18 00:20:32 +0000478 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000479 }
Steve Naroff38374b02007-09-02 20:30:18 +0000480 }
481 case SizeOfAlignOfTypeExprClass: {
482 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
483 // alignof always evaluates to a constant.
484 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
485 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000486 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000487 }
488 case BinaryOperatorClass: {
489 const BinaryOperator *Exp = cast<BinaryOperator>(this);
490
491 // The LHS of a constant expr is always evaluated and needed.
492 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
493 return false;
494
495 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
496 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000497 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000498 }
499 case ImplicitCastExprClass:
500 case CastExprClass: {
501 const Expr *SubExpr;
502 SourceLocation CastLoc;
503 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
504 SubExpr = C->getSubExpr();
505 CastLoc = C->getLParenLoc();
506 } else {
507 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
508 CastLoc = getLocStart();
509 }
510 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
511 if (Loc) *Loc = SubExpr->getLocStart();
512 return false;
513 }
Chris Lattner2777e492007-10-18 00:20:32 +0000514 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000515 }
516 case ConditionalOperatorClass: {
517 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Chris Lattner2777e492007-10-18 00:20:32 +0000518 if (!Exp->getCond()->isConstantExpr(Ctx, Loc) ||
Anders Carlsson39073232007-11-30 19:04:31 +0000519 // Handle the GNU extension for missing LHS.
520 !(Exp->getLHS() && Exp->getLHS()->isConstantExpr(Ctx, Loc)) ||
Chris Lattner2777e492007-10-18 00:20:32 +0000521 !Exp->getRHS()->isConstantExpr(Ctx, Loc))
Steve Naroff38374b02007-09-02 20:30:18 +0000522 return false;
Chris Lattner2777e492007-10-18 00:20:32 +0000523 return true;
Steve Naroff38374b02007-09-02 20:30:18 +0000524 }
525 }
526
527 return true;
528}
529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530/// isIntegerConstantExpr - this recursive routine will test if an expression is
531/// an integer constant expression. Note: With the introduction of VLA's in
532/// C99 the result of the sizeof operator is no longer always a constant
533/// expression. The generalization of the wording to include any subexpression
534/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
535/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
536/// "0 || f()" can be treated as a constant expression. In C90 this expression,
537/// occurring in a context requiring a constant, would have been a constraint
538/// violation. FIXME: This routine currently implements C90 semantics.
539/// To properly implement C99 semantics this routine will need to evaluate
540/// expressions involving operators previously mentioned.
541
542/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
543/// comma, etc
544///
545/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
Chris Lattnerccc213f2007-09-26 00:47:26 +0000546/// permit this. This includes things like (int)1e1000
Chris Lattnerce0afc02007-07-18 05:21:20 +0000547///
548/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
549/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
550/// cast+dereference.
Chris Lattner590b6642007-07-15 23:26:56 +0000551bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
552 SourceLocation *Loc, bool isEvaluated) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 switch (getStmtClass()) {
554 default:
555 if (Loc) *Loc = getLocStart();
556 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 case ParenExprClass:
558 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner590b6642007-07-15 23:26:56 +0000559 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Reid Spencer5f016e22007-07-11 17:01:13 +0000560 case IntegerLiteralClass:
561 Result = cast<IntegerLiteral>(this)->getValue();
562 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000563 case CharacterLiteralClass: {
564 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000565 Result.zextOrTrunc(
566 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner2eadfb62007-07-15 23:32:58 +0000567 Result = CL->getValue();
Chris Lattnerf0fbcb32007-07-16 21:04:56 +0000568 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 break;
Chris Lattner2eadfb62007-07-15 23:32:58 +0000570 }
Steve Naroff7b658aa2007-08-02 04:09:23 +0000571 case TypesCompatibleExprClass: {
572 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000573 Result.zextOrTrunc(
574 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffec0550f2007-10-15 20:41:53 +0000575 Result = Ctx.typesAreCompatible(TCE->getArgType1(), TCE->getArgType2());
Steve Naroff389cecc2007-08-02 00:13:27 +0000576 break;
Steve Naroff7b658aa2007-08-02 04:09:23 +0000577 }
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000578 case CallExprClass: {
579 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner701e5eb2007-09-04 02:45:27 +0000580 Result.zextOrTrunc(
581 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000582 if (CE->isBuiltinClassifyType(Result))
583 break;
584 if (Loc) *Loc = getLocStart();
585 return false;
586 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 case DeclRefExprClass:
588 if (const EnumConstantDecl *D =
589 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
590 Result = D->getInitVal();
591 break;
592 }
593 if (Loc) *Loc = getLocStart();
594 return false;
595 case UnaryOperatorClass: {
596 const UnaryOperator *Exp = cast<UnaryOperator>(this);
597
598 // Get the operand value. If this is sizeof/alignof, do not evalute the
599 // operand. This affects C99 6.6p3.
Chris Lattner602dafd2007-08-23 21:42:50 +0000600 if (!Exp->isSizeOfAlignOfOp() &&
601 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 return false;
603
604 switch (Exp->getOpcode()) {
605 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
606 // See C99 6.6p3.
607 default:
608 if (Loc) *Loc = Exp->getOperatorLoc();
609 return false;
610 case UnaryOperator::Extension:
Chris Lattner76e773a2007-07-18 18:38:36 +0000611 return true; // FIXME: this is wrong.
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 case UnaryOperator::SizeOf:
613 case UnaryOperator::AlignOf:
614 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner590b6642007-07-15 23:26:56 +0000615 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 return false;
617
Chris Lattner76e773a2007-07-18 18:38:36 +0000618 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000619 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000620 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
621 Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000622
623 // Get information about the size or align.
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000624 if (Exp->getOpcode() == UnaryOperator::AlignOf) {
Chris Lattner76e773a2007-07-18 18:38:36 +0000625 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
626 Exp->getOperatorLoc());
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000627 } else {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000628 unsigned CharSize =
629 Ctx.Target.getCharWidth(Ctx.getFullLoc(Exp->getOperatorLoc()));
630
Chris Lattnerda5a6b62007-11-27 18:22:04 +0000631 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
632 Exp->getOperatorLoc()) / CharSize;
633 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 break;
635 case UnaryOperator::LNot: {
636 bool Val = Result != 0;
Chris Lattner701e5eb2007-09-04 02:45:27 +0000637 Result.zextOrTrunc(
Chris Lattnerccc213f2007-09-26 00:47:26 +0000638 static_cast<uint32_t>(Ctx.getTypeSize(getType(),
639 Exp->getOperatorLoc())));
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 Result = Val;
641 break;
642 }
643 case UnaryOperator::Plus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 break;
645 case UnaryOperator::Minus:
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 Result = -Result;
647 break;
648 case UnaryOperator::Not:
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 Result = ~Result;
650 break;
651 }
652 break;
653 }
654 case SizeOfAlignOfTypeExprClass: {
655 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
656 // alignof always evaluates to a constant.
Chris Lattner590b6642007-07-15 23:26:56 +0000657 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 return false;
659
Chris Lattner76e773a2007-07-18 18:38:36 +0000660 // Return the result in the right width.
Chris Lattner701e5eb2007-09-04 02:45:27 +0000661 Result.zextOrTrunc(
662 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner76e773a2007-07-18 18:38:36 +0000663
664 // Get information about the size or align.
665 if (Exp->isSizeOf())
666 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
667 else
668 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 break;
670 }
671 case BinaryOperatorClass: {
672 const BinaryOperator *Exp = cast<BinaryOperator>(this);
673
674 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner590b6642007-07-15 23:26:56 +0000675 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 return false;
677
678 llvm::APSInt RHS(Result);
679
680 // The short-circuiting &&/|| operators don't necessarily evaluate their
681 // RHS. Make sure to pass isEvaluated down correctly.
682 if (Exp->isLogicalOp()) {
683 bool RHSEval;
684 if (Exp->getOpcode() == BinaryOperator::LAnd)
685 RHSEval = Result != 0;
686 else {
687 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
688 RHSEval = Result == 0;
689 }
690
Chris Lattner590b6642007-07-15 23:26:56 +0000691 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 isEvaluated & RHSEval))
693 return false;
694 } else {
Chris Lattner590b6642007-07-15 23:26:56 +0000695 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 return false;
697 }
698
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 switch (Exp->getOpcode()) {
700 default:
701 if (Loc) *Loc = getLocStart();
702 return false;
703 case BinaryOperator::Mul:
704 Result *= RHS;
705 break;
706 case BinaryOperator::Div:
707 if (RHS == 0) {
708 if (!isEvaluated) break;
709 if (Loc) *Loc = getLocStart();
710 return false;
711 }
712 Result /= RHS;
713 break;
714 case BinaryOperator::Rem:
715 if (RHS == 0) {
716 if (!isEvaluated) break;
717 if (Loc) *Loc = getLocStart();
718 return false;
719 }
720 Result %= RHS;
721 break;
722 case BinaryOperator::Add: Result += RHS; break;
723 case BinaryOperator::Sub: Result -= RHS; break;
724 case BinaryOperator::Shl:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000725 Result <<=
726 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 break;
728 case BinaryOperator::Shr:
Chris Lattner701e5eb2007-09-04 02:45:27 +0000729 Result >>=
730 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 break;
732 case BinaryOperator::LT: Result = Result < RHS; break;
733 case BinaryOperator::GT: Result = Result > RHS; break;
734 case BinaryOperator::LE: Result = Result <= RHS; break;
735 case BinaryOperator::GE: Result = Result >= RHS; break;
736 case BinaryOperator::EQ: Result = Result == RHS; break;
737 case BinaryOperator::NE: Result = Result != RHS; break;
738 case BinaryOperator::And: Result &= RHS; break;
739 case BinaryOperator::Xor: Result ^= RHS; break;
740 case BinaryOperator::Or: Result |= RHS; break;
741 case BinaryOperator::LAnd:
742 Result = Result != 0 && RHS != 0;
743 break;
744 case BinaryOperator::LOr:
745 Result = Result != 0 || RHS != 0;
746 break;
747
748 case BinaryOperator::Comma:
749 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
750 // *except* when they are contained within a subexpression that is not
751 // evaluated". Note that Assignment can never happen due to constraints
752 // on the LHS subexpr, so we don't need to check it here.
753 if (isEvaluated) {
754 if (Loc) *Loc = getLocStart();
755 return false;
756 }
757
758 // The result of the constant expr is the RHS.
759 Result = RHS;
760 return true;
761 }
762
763 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
764 break;
765 }
Chris Lattner26dc7b32007-07-15 23:54:50 +0000766 case ImplicitCastExprClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 case CastExprClass: {
Chris Lattner26dc7b32007-07-15 23:54:50 +0000768 const Expr *SubExpr;
769 SourceLocation CastLoc;
770 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
771 SubExpr = C->getSubExpr();
772 CastLoc = C->getLParenLoc();
773 } else {
774 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
775 CastLoc = getLocStart();
776 }
777
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000779 if (!SubExpr->getType()->isArithmeticType() ||
780 !getType()->isIntegerType()) {
781 if (Loc) *Loc = SubExpr->getLocStart();
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 return false;
783 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000784
785 uint32_t DestWidth =
786 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
787
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 // Handle simple integer->integer casts.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000789 if (SubExpr->getType()->isIntegerType()) {
790 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 return false;
Chris Lattner26dc7b32007-07-15 23:54:50 +0000792
793 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000794 // If the input is signed, do a sign extend, noop, or truncate.
795 if (SubExpr->getType()->isSignedIntegerType())
796 Result.sextOrTrunc(DestWidth);
797 else // If the input is unsigned, do a zero extend, noop, or truncate.
798 Result.zextOrTrunc(DestWidth);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 break;
800 }
801
802 // Allow floating constants that are the immediate operands of casts or that
803 // are parenthesized.
Chris Lattner26dc7b32007-07-15 23:54:50 +0000804 const Expr *Operand = SubExpr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
806 Operand = PE->getSubExpr();
Chris Lattner987b15d2007-09-22 19:04:13 +0000807
808 // If this isn't a floating literal, we can't handle it.
809 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand);
810 if (!FL) {
811 if (Loc) *Loc = Operand->getLocStart();
812 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 }
Chris Lattner987b15d2007-09-22 19:04:13 +0000814
815 // Determine whether we are converting to unsigned or signed.
816 bool DestSigned = getType()->isSignedIntegerType();
Chris Lattnerccc213f2007-09-26 00:47:26 +0000817
818 // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can
819 // be called multiple times per AST.
Chris Lattner987b15d2007-09-22 19:04:13 +0000820 uint64_t Space[4];
Chris Lattnerccc213f2007-09-26 00:47:26 +0000821 (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned,
822 llvm::APFloat::rmTowardZero);
Chris Lattner987b15d2007-09-22 19:04:13 +0000823 Result = llvm::APInt(DestWidth, 4, Space);
824 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 }
826 case ConditionalOperatorClass: {
827 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
828
Chris Lattner590b6642007-07-15 23:26:56 +0000829 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000830 return false;
831
832 const Expr *TrueExp = Exp->getLHS();
833 const Expr *FalseExp = Exp->getRHS();
834 if (Result == 0) std::swap(TrueExp, FalseExp);
835
836 // Evaluate the false one first, discard the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000837 if (FalseExp && !FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 return false;
839 // Evalute the true one, capture the result.
Anders Carlsson39073232007-11-30 19:04:31 +0000840 if (TrueExp &&
841 !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000843 break;
844 }
845 }
846
847 // Cases that are valid constant exprs fall through to here.
848 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
849 return true;
850}
851
852
853/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
854/// integer constant expression with the value zero, or if this is one that is
855/// cast to void*.
Chris Lattner590b6642007-07-15 23:26:56 +0000856bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000857 // Strip off a cast to void*, if it exists.
858 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
859 // Check that it is a cast to void*.
860 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
861 QualType Pointee = PT->getPointeeType();
862 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
863 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner590b6642007-07-15 23:26:56 +0000864 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 }
Steve Naroff7269f2d2007-08-28 21:20:34 +0000866 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroff19a6ebd2007-08-29 00:00:02 +0000867 // Ignore the ImplicitCastExpr type entirely.
868 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
870 // Accept ((void*)0) as a null pointer constant, as many other
871 // implementations do.
Chris Lattner590b6642007-07-15 23:26:56 +0000872 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 }
874
875 // This expression must be an integer type.
876 if (!getType()->isIntegerType())
877 return false;
878
879 // If we have an integer constant expression, we need to *evaluate* it and
880 // test for the value 0.
881 llvm::APSInt Val(32);
Chris Lattner590b6642007-07-15 23:26:56 +0000882 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000883}
Steve Naroff31a45842007-07-28 23:10:27 +0000884
Chris Lattner6481a572007-08-03 17:31:20 +0000885unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner4d0ac882007-08-03 16:00:20 +0000886 return strlen(Accessor.getName());
887}
888
889
Chris Lattnercb92a112007-08-02 21:47:28 +0000890/// getComponentType - Determine whether the components of this access are
891/// "point" "color" or "texture" elements.
Chris Lattner6481a572007-08-03 17:31:20 +0000892OCUVectorElementExpr::ElementType
893OCUVectorElementExpr::getElementType() const {
Steve Naroff31a45842007-07-28 23:10:27 +0000894 // derive the component type, no need to waste space.
895 const char *compStr = Accessor.getName();
Chris Lattnerb4878f42007-08-02 22:20:00 +0000896
Chris Lattner88dca042007-08-02 22:33:49 +0000897 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
898 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerb4878f42007-08-02 22:20:00 +0000899
Chris Lattner88dca042007-08-02 22:33:49 +0000900 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerb4878f42007-08-02 22:20:00 +0000901 "getComponentType(): Illegal accessor");
902 return Texture;
Steve Naroff31a45842007-07-28 23:10:27 +0000903}
Steve Narofffec0b492007-07-30 03:29:09 +0000904
Chris Lattner6481a572007-08-03 17:31:20 +0000905/// containsDuplicateElements - Return true if any element access is
Chris Lattnercb92a112007-08-02 21:47:28 +0000906/// repeated.
Chris Lattner6481a572007-08-03 17:31:20 +0000907bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Narofffec0b492007-07-30 03:29:09 +0000908 const char *compStr = Accessor.getName();
909 unsigned length = strlen(compStr);
910
911 for (unsigned i = 0; i < length-1; i++) {
912 const char *s = compStr+i;
913 for (const char c = *s++; *s; s++)
914 if (c == *s)
915 return true;
916 }
917 return false;
918}
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000919
920/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattner6481a572007-08-03 17:31:20 +0000921unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000922 const char *compStr = Accessor.getName();
Chris Lattner6481a572007-08-03 17:31:20 +0000923 unsigned length = getNumElements();
Chris Lattnerb8f849d2007-08-02 23:36:59 +0000924
925 unsigned Result = 0;
926
927 while (length--) {
928 Result <<= 2;
929 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
930 assert(Idx != -1 && "Invalid accessor letter");
931 Result |= Idx;
932 }
933 return Result;
934}
935
Steve Naroff68d331a2007-09-27 14:38:14 +0000936// constructor for instance messages.
Steve Naroffbcfb06a2007-09-28 22:22:11 +0000937ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo,
Steve Naroffdb611d52007-11-03 16:37:59 +0000938 QualType retType, ObjcMethodDecl *mproto,
939 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +0000940 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +0000941 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
942 MethodProto(mproto), ClassName(0) {
Steve Naroff49f109c2007-11-15 13:05:42 +0000943 NumArgs = nargs;
944 SubExprs = new Expr*[NumArgs+1];
Steve Naroff68d331a2007-09-27 14:38:14 +0000945 SubExprs[RECEIVER] = receiver;
Steve Naroff49f109c2007-11-15 13:05:42 +0000946 if (NumArgs) {
947 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +0000948 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
949 }
Steve Naroff563477d2007-09-18 23:55:05 +0000950 LBracloc = LBrac;
951 RBracloc = RBrac;
952}
953
Steve Naroff68d331a2007-09-27 14:38:14 +0000954// constructor for class messages.
955// FIXME: clsName should be typed to ObjCInterfaceType
Steve Naroffbcfb06a2007-09-28 22:22:11 +0000956ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo,
Steve Naroffdb611d52007-11-03 16:37:59 +0000957 QualType retType, ObjcMethodDecl *mproto,
958 SourceLocation LBrac, SourceLocation RBrac,
Steve Naroff49f109c2007-11-15 13:05:42 +0000959 Expr **ArgExprs, unsigned nargs)
Steve Naroffdb611d52007-11-03 16:37:59 +0000960 : Expr(ObjCMessageExprClass, retType), SelName(selInfo),
961 MethodProto(mproto), ClassName(clsName) {
Steve Naroff49f109c2007-11-15 13:05:42 +0000962 NumArgs = nargs;
963 SubExprs = new Expr*[NumArgs+1];
Steve Naroff563477d2007-09-18 23:55:05 +0000964 SubExprs[RECEIVER] = 0;
Steve Naroff49f109c2007-11-15 13:05:42 +0000965 if (NumArgs) {
966 for (unsigned i = 0; i != NumArgs; ++i)
Steve Naroff68d331a2007-09-27 14:38:14 +0000967 SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]);
968 }
Steve Naroff563477d2007-09-18 23:55:05 +0000969 LBracloc = LBrac;
970 RBracloc = RBrac;
971}
972
Chris Lattner27437ca2007-10-25 00:29:32 +0000973
974bool ChooseExpr::isConditionTrue(ASTContext &C) const {
975 llvm::APSInt CondVal(32);
976 bool IsConst = getCond()->isIntegerConstantExpr(CondVal, C);
977 assert(IsConst && "Condition of choose expr must be i-c-e"); IsConst=IsConst;
978 return CondVal != 0;
979}
980
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000981//===----------------------------------------------------------------------===//
982// Child Iterators for iterating over subexpressions/substatements
983//===----------------------------------------------------------------------===//
984
985// DeclRefExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +0000986Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); }
987Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000988
Steve Naroff7779db42007-11-12 14:29:37 +0000989// ObjCIvarRefExpr
990Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return child_iterator(); }
991Stmt::child_iterator ObjCIvarRefExpr::child_end() { return child_iterator(); }
992
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000993// PreDefinedExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +0000994Stmt::child_iterator PreDefinedExpr::child_begin() { return child_iterator(); }
995Stmt::child_iterator PreDefinedExpr::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000996
997// IntegerLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +0000998Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); }
999Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001000
1001// CharacterLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001002Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator(); }
1003Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001004
1005// FloatingLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001006Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); }
1007Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001008
Chris Lattner5d661452007-08-26 03:42:43 +00001009// ImaginaryLiteral
1010Stmt::child_iterator ImaginaryLiteral::child_begin() {
1011 return reinterpret_cast<Stmt**>(&Val);
1012}
1013Stmt::child_iterator ImaginaryLiteral::child_end() {
1014 return reinterpret_cast<Stmt**>(&Val)+1;
1015}
1016
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001017// StringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001018Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); }
1019Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); }
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001020
1021// ParenExpr
1022Stmt::child_iterator ParenExpr::child_begin() {
1023 return reinterpret_cast<Stmt**>(&Val);
1024}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001025Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001026 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001027}
1028
1029// UnaryOperator
1030Stmt::child_iterator UnaryOperator::child_begin() {
1031 return reinterpret_cast<Stmt**>(&Val);
1032}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001033Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001034 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001035}
1036
1037// SizeOfAlignOfTypeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001038Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001039 // If the type is a VLA type (and not a typedef), the size expression of the
1040 // VLA needs to be treated as an executable expression.
1041 if (VariableArrayType* T = dyn_cast<VariableArrayType>(Ty.getTypePtr()))
1042 return child_iterator(T);
1043 else
1044 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001045}
1046Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() {
Ted Kremenek699e9fb2007-12-14 22:52:23 +00001047 return child_iterator();
Ted Kremenek9ac59282007-10-18 23:28:49 +00001048}
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001049
1050// ArraySubscriptExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001051Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001052 return reinterpret_cast<Stmt**>(&SubExprs);
1053}
Ted Kremenek1237c672007-08-24 20:06:47 +00001054Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001055 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001056}
1057
1058// CallExpr
Ted Kremenek1237c672007-08-24 20:06:47 +00001059Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001060 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001061}
Ted Kremenek1237c672007-08-24 20:06:47 +00001062Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek42a29772007-08-27 21:11:44 +00001063 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek77ed8e42007-08-24 18:13:47 +00001064}
Ted Kremenek1237c672007-08-24 20:06:47 +00001065
1066// MemberExpr
1067Stmt::child_iterator MemberExpr::child_begin() {
1068 return reinterpret_cast<Stmt**>(&Base);
1069}
Ted Kremenek1237c672007-08-24 20:06:47 +00001070Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001071 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001072}
1073
1074// OCUVectorElementExpr
1075Stmt::child_iterator OCUVectorElementExpr::child_begin() {
1076 return reinterpret_cast<Stmt**>(&Base);
1077}
Ted Kremenek1237c672007-08-24 20:06:47 +00001078Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001079 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001080}
1081
1082// CompoundLiteralExpr
1083Stmt::child_iterator CompoundLiteralExpr::child_begin() {
1084 return reinterpret_cast<Stmt**>(&Init);
1085}
Ted Kremenek1237c672007-08-24 20:06:47 +00001086Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001087 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001088}
1089
1090// ImplicitCastExpr
1091Stmt::child_iterator ImplicitCastExpr::child_begin() {
1092 return reinterpret_cast<Stmt**>(&Op);
1093}
Ted Kremenek1237c672007-08-24 20:06:47 +00001094Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001095 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001096}
1097
1098// CastExpr
1099Stmt::child_iterator CastExpr::child_begin() {
1100 return reinterpret_cast<Stmt**>(&Op);
1101}
Ted Kremenek1237c672007-08-24 20:06:47 +00001102Stmt::child_iterator CastExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001103 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001104}
1105
1106// BinaryOperator
1107Stmt::child_iterator BinaryOperator::child_begin() {
1108 return reinterpret_cast<Stmt**>(&SubExprs);
1109}
Ted Kremenek1237c672007-08-24 20:06:47 +00001110Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001111 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001112}
1113
1114// ConditionalOperator
1115Stmt::child_iterator ConditionalOperator::child_begin() {
1116 return reinterpret_cast<Stmt**>(&SubExprs);
1117}
Ted Kremenek1237c672007-08-24 20:06:47 +00001118Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001119 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001120}
1121
1122// AddrLabelExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001123Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); }
1124Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001125
Ted Kremenek1237c672007-08-24 20:06:47 +00001126// StmtExpr
1127Stmt::child_iterator StmtExpr::child_begin() {
1128 return reinterpret_cast<Stmt**>(&SubStmt);
1129}
Ted Kremenek1237c672007-08-24 20:06:47 +00001130Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001131 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek1237c672007-08-24 20:06:47 +00001132}
1133
1134// TypesCompatibleExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001135Stmt::child_iterator TypesCompatibleExpr::child_begin() {
1136 return child_iterator();
1137}
1138
1139Stmt::child_iterator TypesCompatibleExpr::child_end() {
1140 return child_iterator();
1141}
Ted Kremenek1237c672007-08-24 20:06:47 +00001142
1143// ChooseExpr
1144Stmt::child_iterator ChooseExpr::child_begin() {
1145 return reinterpret_cast<Stmt**>(&SubExprs);
1146}
1147
1148Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner5d661452007-08-26 03:42:43 +00001149 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek1237c672007-08-24 20:06:47 +00001150}
1151
Anders Carlsson7c50aca2007-10-15 20:28:48 +00001152// VAArgExpr
1153Stmt::child_iterator VAArgExpr::child_begin() {
1154 return reinterpret_cast<Stmt**>(&Val);
1155}
1156
1157Stmt::child_iterator VAArgExpr::child_end() {
1158 return reinterpret_cast<Stmt**>(&Val)+1;
1159}
1160
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001161// InitListExpr
1162Stmt::child_iterator InitListExpr::child_begin() {
1163 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1164}
1165Stmt::child_iterator InitListExpr::child_end() {
1166 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1167}
1168
Ted Kremenek1237c672007-08-24 20:06:47 +00001169// ObjCStringLiteral
Ted Kremenek9ac59282007-10-18 23:28:49 +00001170Stmt::child_iterator ObjCStringLiteral::child_begin() {
1171 return child_iterator();
1172}
1173Stmt::child_iterator ObjCStringLiteral::child_end() {
1174 return child_iterator();
1175}
Ted Kremenek1237c672007-08-24 20:06:47 +00001176
1177// ObjCEncodeExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001178Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); }
1179Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); }
Ted Kremenek1237c672007-08-24 20:06:47 +00001180
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001181// ObjCSelectorExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001182Stmt::child_iterator ObjCSelectorExpr::child_begin() {
1183 return child_iterator();
1184}
1185Stmt::child_iterator ObjCSelectorExpr::child_end() {
1186 return child_iterator();
1187}
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001188
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001189// ObjCProtocolExpr
Ted Kremenek9ac59282007-10-18 23:28:49 +00001190Stmt::child_iterator ObjCProtocolExpr::child_begin() {
1191 return child_iterator();
1192}
1193Stmt::child_iterator ObjCProtocolExpr::child_end() {
1194 return child_iterator();
1195}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001196
Steve Naroff563477d2007-09-18 23:55:05 +00001197// ObjCMessageExpr
1198Stmt::child_iterator ObjCMessageExpr::child_begin() {
1199 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1200}
1201Stmt::child_iterator ObjCMessageExpr::child_end() {
Steve Naroff68d331a2007-09-27 14:38:14 +00001202 return reinterpret_cast<Stmt**>(&SubExprs[getNumArgs()+ARGS_START]);
Steve Naroff563477d2007-09-18 23:55:05 +00001203}
1204