blob: e5a128247eecdd0960be5d3aff960beb59b6e180 [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +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 Lattner5c4664e2007-07-15 23:32:58 +000015#include "clang/AST/ASTContext.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000016#include "clang/AST/StmtVisitor.h"
Chris Lattnere165d942006-08-24 04:40:38 +000017#include "clang/Lex/IdentifierTable.h"
Steve Naroffd54978b2007-09-18 23:55:05 +000018// is this bad layering? I (snaroff) don't think so. Want Chris to weigh in.
19#include "clang/Parse/DeclSpec.h"
Chris Lattner1b926492006-08-23 06:42:10 +000020using namespace clang;
21
Chris Lattner0eedafe2006-08-24 04:56:27 +000022//===----------------------------------------------------------------------===//
23// Primary Expressions.
24//===----------------------------------------------------------------------===//
25
Steve Naroff408451b2007-02-26 22:17:12 +000026StringLiteral::StringLiteral(const char *strData, unsigned byteLength,
Steve Naroff53f07dc2007-05-17 21:49:33 +000027 bool Wide, QualType t, SourceLocation firstLoc,
28 SourceLocation lastLoc) :
Steve Narofff1e53692007-03-23 22:27:02 +000029 Expr(StringLiteralClass, t) {
Steve Naroffdf7855b2007-02-21 23:46:25 +000030 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerd3e98952006-10-06 05:22:26 +000031 char *AStrData = new char[byteLength];
32 memcpy(AStrData, strData, byteLength);
33 StrData = AStrData;
34 ByteLength = byteLength;
Chris Lattner882f7882006-11-04 18:52:07 +000035 IsWide = Wide;
Steve Naroff53f07dc2007-05-17 21:49:33 +000036 firstTokLoc = firstLoc;
37 lastTokLoc = lastLoc;
Chris Lattnerd3e98952006-10-06 05:22:26 +000038}
39
Steve Naroffdf7855b2007-02-21 23:46:25 +000040StringLiteral::~StringLiteral() {
Chris Lattnerd3e98952006-10-06 05:22:26 +000041 delete[] StrData;
42}
43
Chris Lattner15768702006-11-05 23:54:51 +000044bool UnaryOperator::isPostfix(Opcode Op) {
45 switch (Op) {
46 case PostInc:
47 case PostDec:
48 return true;
49 default:
50 return false;
51 }
52}
53
Chris Lattner1b926492006-08-23 06:42:10 +000054/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
55/// corresponds to, e.g. "sizeof" or "[pre]++".
56const char *UnaryOperator::getOpcodeStr(Opcode Op) {
57 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +000058 default: assert(0 && "Unknown unary operator");
Chris Lattner15768702006-11-05 23:54:51 +000059 case PostInc: return "++";
60 case PostDec: return "--";
61 case PreInc: return "++";
62 case PreDec: return "--";
Chris Lattner1b926492006-08-23 06:42:10 +000063 case AddrOf: return "&";
64 case Deref: return "*";
65 case Plus: return "+";
66 case Minus: return "-";
67 case Not: return "~";
68 case LNot: return "!";
69 case Real: return "__real";
70 case Imag: return "__imag";
Chris Lattner26115ac2006-08-24 06:10:04 +000071 case SizeOf: return "sizeof";
72 case AlignOf: return "alignof";
Chris Lattnerc52b1182006-10-25 05:45:55 +000073 case Extension: return "__extension__";
Chris Lattnerf17bd422007-08-30 17:45:32 +000074 case OffsetOf: return "__builtin_offsetof";
Chris Lattner1b926492006-08-23 06:42:10 +000075 }
76}
77
Chris Lattner0eedafe2006-08-24 04:56:27 +000078//===----------------------------------------------------------------------===//
79// Postfix Operators.
80//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +000081
Steve Naroff509fe022007-05-17 01:16:00 +000082CallExpr::CallExpr(Expr *fn, Expr **args, unsigned numargs, QualType t,
Chris Lattner9b3b9a12007-06-27 06:08:24 +000083 SourceLocation rparenloc)
Ted Kremenek85e92ec2007-08-24 18:13:47 +000084 : Expr(CallExprClass, t), NumArgs(numargs) {
85 SubExprs = new Expr*[numargs+1];
86 SubExprs[FN] = fn;
Chris Lattnere165d942006-08-24 04:40:38 +000087 for (unsigned i = 0; i != numargs; ++i)
Ted Kremenek85e92ec2007-08-24 18:13:47 +000088 SubExprs[i+ARGS_START] = args[i];
Chris Lattner9b3b9a12007-06-27 06:08:24 +000089 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +000090}
91
Steve Naroff12b04472007-08-08 22:15:55 +000092bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
93 // The following enum mimics gcc's internal "typeclass.h" file.
94 enum gcc_type_class {
95 no_type_class = -1,
96 void_type_class, integer_type_class, char_type_class,
97 enumeral_type_class, boolean_type_class,
98 pointer_type_class, reference_type_class, offset_type_class,
99 real_type_class, complex_type_class,
100 function_type_class, method_type_class,
101 record_type_class, union_type_class,
102 array_type_class, string_type_class,
103 lang_type_class
104 };
105 Result.setIsSigned(true);
106
107 // All simple function calls (e.g. func()) are implicitly cast to pointer to
108 // function. As a result, we try and obtain the DeclRefExpr from the
109 // ImplicitCastExpr.
110 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
111 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
112 return false;
113 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
114 if (!DRE)
115 return false;
116
117 // We have a DeclRefExpr.
118 if (strcmp(DRE->getDecl()->getName(), "__builtin_classify_type") == 0) {
119 // If no argument was supplied, default to "no_type_class". This isn't
120 // ideal, however it's what gcc does.
121 Result = static_cast<uint64_t>(no_type_class);
122 if (NumArgs >= 1) {
123 QualType argType = getArg(0)->getType();
124
125 if (argType->isVoidType())
126 Result = void_type_class;
127 else if (argType->isEnumeralType())
128 Result = enumeral_type_class;
129 else if (argType->isBooleanType())
130 Result = boolean_type_class;
131 else if (argType->isCharType())
132 Result = string_type_class; // gcc doesn't appear to use char_type_class
133 else if (argType->isIntegerType())
134 Result = integer_type_class;
135 else if (argType->isPointerType())
136 Result = pointer_type_class;
137 else if (argType->isReferenceType())
138 Result = reference_type_class;
139 else if (argType->isRealType())
140 Result = real_type_class;
141 else if (argType->isComplexType())
142 Result = complex_type_class;
143 else if (argType->isFunctionType())
144 Result = function_type_class;
145 else if (argType->isStructureType())
146 Result = record_type_class;
147 else if (argType->isUnionType())
148 Result = union_type_class;
149 else if (argType->isArrayType())
150 Result = array_type_class;
151 else if (argType->isUnionType())
152 Result = union_type_class;
153 else // FIXME: offset_type_class, method_type_class, & lang_type_class?
154 assert(1 && "CallExpr::isBuiltinClassifyType(): unimplemented type");
155 }
156 return true;
157 }
158 return false;
159}
160
Chris Lattner1b926492006-08-23 06:42:10 +0000161/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
162/// corresponds to, e.g. "<<=".
163const char *BinaryOperator::getOpcodeStr(Opcode Op) {
164 switch (Op) {
165 default: assert(0 && "Unknown binary operator");
166 case Mul: return "*";
167 case Div: return "/";
168 case Rem: return "%";
169 case Add: return "+";
170 case Sub: return "-";
171 case Shl: return "<<";
172 case Shr: return ">>";
173 case LT: return "<";
174 case GT: return ">";
175 case LE: return "<=";
176 case GE: return ">=";
177 case EQ: return "==";
178 case NE: return "!=";
179 case And: return "&";
180 case Xor: return "^";
181 case Or: return "|";
182 case LAnd: return "&&";
183 case LOr: return "||";
184 case Assign: return "=";
185 case MulAssign: return "*=";
186 case DivAssign: return "/=";
187 case RemAssign: return "%=";
188 case AddAssign: return "+=";
189 case SubAssign: return "-=";
190 case ShlAssign: return "<<=";
191 case ShrAssign: return ">>=";
192 case AndAssign: return "&=";
193 case XorAssign: return "^=";
194 case OrAssign: return "|=";
195 case Comma: return ",";
196 }
197}
Steve Naroff47500512007-04-19 23:00:49 +0000198
Anders Carlsson4692db02007-08-31 04:56:16 +0000199InitListExpr::InitListExpr(SourceLocation lbraceloc,
200 Expr **initexprs, unsigned numinits,
201 SourceLocation rbraceloc)
202 : Expr(InitListExprClass, QualType())
203 , NumInits(numinits)
204 , LBraceLoc(lbraceloc)
205 , RBraceLoc(rbraceloc)
206{
207 InitExprs = new Expr*[numinits];
208 for (unsigned i = 0; i != numinits; i++)
209 InitExprs[i] = initexprs[i];
210}
Chris Lattner1ec5f562007-06-27 05:38:08 +0000211
212//===----------------------------------------------------------------------===//
213// Generic Expression Routines
214//===----------------------------------------------------------------------===//
215
216/// hasLocalSideEffect - Return true if this immediate expression has side
217/// effects, not counting any sub-expressions.
218bool Expr::hasLocalSideEffect() const {
219 switch (getStmtClass()) {
220 default:
221 return false;
222 case ParenExprClass:
223 return cast<ParenExpr>(this)->getSubExpr()->hasLocalSideEffect();
224 case UnaryOperatorClass: {
225 const UnaryOperator *UO = cast<UnaryOperator>(this);
226
227 switch (UO->getOpcode()) {
228 default: return false;
229 case UnaryOperator::PostInc:
230 case UnaryOperator::PostDec:
231 case UnaryOperator::PreInc:
232 case UnaryOperator::PreDec:
233 return true; // ++/--
234
Chris Lattnera44d1162007-06-27 05:58:59 +0000235 case UnaryOperator::Deref:
236 // Dereferencing a volatile pointer is a side-effect.
237 return getType().isVolatileQualified();
238 case UnaryOperator::Real:
239 case UnaryOperator::Imag:
240 // accessing a piece of a volatile complex is a side-effect.
241 return UO->getSubExpr()->getType().isVolatileQualified();
242
Chris Lattner1ec5f562007-06-27 05:38:08 +0000243 case UnaryOperator::Extension:
244 return UO->getSubExpr()->hasLocalSideEffect();
245 }
246 }
247 case BinaryOperatorClass:
248 return cast<BinaryOperator>(this)->isAssignmentOp();
Chris Lattner86928112007-08-25 02:00:02 +0000249 case CompoundAssignOperatorClass:
Chris Lattnerd8c9fc52007-08-25 01:55:00 +0000250 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000251
Chris Lattnera44d1162007-06-27 05:58:59 +0000252 case MemberExprClass:
Chris Lattner1ec5f562007-06-27 05:38:08 +0000253 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000254 // If the base pointer or element is to a volatile pointer/field, accessing
255 // if is a side effect.
256 return getType().isVolatileQualified();
Chris Lattner1ec5f562007-06-27 05:38:08 +0000257
258 case CallExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +0000259 // TODO: check attributes for pure/const. "void foo() { strlen("bar"); }"
260 // should warn.
Chris Lattner1ec5f562007-06-27 05:38:08 +0000261 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000262
263 case CastExprClass:
264 // If this is a cast to void, check the operand. Otherwise, the result of
265 // the cast is unused.
266 if (getType()->isVoidType())
267 return cast<CastExpr>(this)->getSubExpr()->hasLocalSideEffect();
268 return false;
269 }
270}
271
Steve Naroff475cca02007-05-14 17:19:29 +0000272/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an
273/// incomplete type other than void. Nonarray expressions that can be lvalues:
Steve Naroff47500512007-04-19 23:00:49 +0000274/// - name, where name must be a variable
275/// - e[i]
276/// - (e), where e must be an lvalue
277/// - e.name, where e must be an lvalue
278/// - e->name
Steve Naroff35d85152007-05-07 00:24:15 +0000279/// - *e, the type of e cannot be a function type
Steve Naroff47500512007-04-19 23:00:49 +0000280/// - string-constant
Bill Wendlingdfc81072007-07-17 03:52:31 +0000281/// - reference type [C++ [expr]]
Steve Naroff47500512007-04-19 23:00:49 +0000282///
Bill Wendling457d1342007-07-16 07:07:56 +0000283Expr::isLvalueResult Expr::isLvalue() const {
Steve Naroff475cca02007-05-14 17:19:29 +0000284 // first, check the type (C99 6.3.2.1)
Chris Lattner0f4faa12007-07-21 05:33:26 +0000285 if (TR->isFunctionType()) // from isObjectType()
Steve Naroff9358c712007-05-27 23:58:33 +0000286 return LV_NotObjectType;
Steve Naroffe728ba32007-07-10 22:20:04 +0000287
Steve Naroffa12a6c92007-07-21 13:32:03 +0000288 if (TR->isVoidType())
Steve Naroff9358c712007-05-27 23:58:33 +0000289 return LV_IncompleteVoidType;
Bill Wendlingdfc81072007-07-17 03:52:31 +0000290
Chris Lattner0f4faa12007-07-21 05:33:26 +0000291 if (TR->isReferenceType()) // C++ [expr]
Bill Wendlingdfc81072007-07-17 03:52:31 +0000292 return LV_Valid;
293
Steve Naroff5dd642e2007-05-14 18:14:51 +0000294 // the type looks fine, now check the expression
Steve Naroff47500512007-04-19 23:00:49 +0000295 switch (getStmtClass()) {
Steve Naroff475cca02007-05-14 17:19:29 +0000296 case StringLiteralClass: // C99 6.5.1p4
Steve Naroff5dd642e2007-05-14 18:14:51 +0000297 case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2))))
Steve Naroffe728ba32007-07-10 22:20:04 +0000298 // For vectors, make sure base is an lvalue (i.e. not a function call).
299 if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType())
300 return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue();
Steve Naroff9358c712007-05-27 23:58:33 +0000301 return LV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000302 case DeclRefExprClass: // C99 6.5.1p2
Steve Naroff9358c712007-05-27 23:58:33 +0000303 if (isa<VarDecl>(cast<DeclRefExpr>(this)->getDecl()))
304 return LV_Valid;
305 break;
Anton Korobeynikovb76cda02007-07-12 15:26:50 +0000306 case MemberExprClass: { // C99 6.5.2.3p4
Steve Naroff47500512007-04-19 23:00:49 +0000307 const MemberExpr *m = cast<MemberExpr>(this);
Steve Naroff9358c712007-05-27 23:58:33 +0000308 return m->isArrow() ? LV_Valid : m->getBase()->isLvalue();
Anton Korobeynikovb76cda02007-07-12 15:26:50 +0000309 }
Steve Naroff475cca02007-05-14 17:19:29 +0000310 case UnaryOperatorClass: // C99 6.5.3p4
Steve Naroff9358c712007-05-27 23:58:33 +0000311 if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref)
312 return LV_Valid;
313 break;
Steve Naroff475cca02007-05-14 17:19:29 +0000314 case ParenExprClass: // C99 6.5.1p5
315 return cast<ParenExpr>(this)->getSubExpr()->isLvalue();
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000316 case OCUVectorElementExprClass:
317 if (cast<OCUVectorElementExpr>(this)->containsDuplicateElements())
Steve Naroff0d595ca2007-07-30 03:29:09 +0000318 return LV_DuplicateVectorComponents;
319 return LV_Valid;
Steve Naroff9358c712007-05-27 23:58:33 +0000320 default:
321 break;
Steve Naroff47500512007-04-19 23:00:49 +0000322 }
Steve Naroff9358c712007-05-27 23:58:33 +0000323 return LV_InvalidExpression;
Steve Naroff47500512007-04-19 23:00:49 +0000324}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000325
Steve Naroff475cca02007-05-14 17:19:29 +0000326/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
327/// does not have an incomplete type, does not have a const-qualified type, and
328/// if it is a structure or union, does not have any member (including,
329/// recursively, any member or element of all contained aggregates or unions)
330/// with a const-qualified type.
Bill Wendling457d1342007-07-16 07:07:56 +0000331Expr::isModifiableLvalueResult Expr::isModifiableLvalue() const {
Steve Naroff9358c712007-05-27 23:58:33 +0000332 isLvalueResult lvalResult = isLvalue();
333
334 switch (lvalResult) {
Chris Lattner1ec5f562007-06-27 05:38:08 +0000335 case LV_Valid: break;
336 case LV_NotObjectType: return MLV_NotObjectType;
337 case LV_IncompleteVoidType: return MLV_IncompleteVoidType;
Steve Naroff0d595ca2007-07-30 03:29:09 +0000338 case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
Chris Lattner1ec5f562007-06-27 05:38:08 +0000339 case LV_InvalidExpression: return MLV_InvalidExpression;
Steve Naroff9358c712007-05-27 23:58:33 +0000340 }
Steve Naroff475cca02007-05-14 17:19:29 +0000341 if (TR.isConstQualified())
Steve Naroff9358c712007-05-27 23:58:33 +0000342 return MLV_ConstQualified;
Steve Naroff475cca02007-05-14 17:19:29 +0000343 if (TR->isArrayType())
Steve Naroff9358c712007-05-27 23:58:33 +0000344 return MLV_ArrayType;
Steve Naroff475cca02007-05-14 17:19:29 +0000345 if (TR->isIncompleteType())
Steve Naroff9358c712007-05-27 23:58:33 +0000346 return MLV_IncompleteType;
347
348 if (const RecordType *r = dyn_cast<RecordType>(TR.getCanonicalType())) {
349 if (r->hasConstFields())
350 return MLV_ConstQualified;
351 }
352 return MLV_Valid;
Steve Naroff475cca02007-05-14 17:19:29 +0000353}
354
Steve Naroffb03f5942007-09-02 20:30:18 +0000355bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const {
356
357 switch (getStmtClass()) {
358 default:
359 if (Loc) *Loc = getLocStart();
360 return false;
361 case ParenExprClass:
362 return cast<ParenExpr>(this)->getSubExpr()->isConstantExpr(Ctx, Loc);
363 case StringLiteralClass:
364 case FloatingLiteralClass:
365 case IntegerLiteralClass:
366 case CharacterLiteralClass:
367 case ImaginaryLiteralClass:
368 case TypesCompatibleExprClass:
369 break;
370 case CallExprClass: {
371 const CallExpr *CE = cast<CallExpr>(this);
372 llvm::APSInt Result(32);
Hartmut Kaiseraf2584f2007-09-16 21:35:35 +0000373 Result.zextOrTrunc(
374 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroffb03f5942007-09-02 20:30:18 +0000375 if (CE->isBuiltinClassifyType(Result))
376 break;
377 if (Loc) *Loc = getLocStart();
378 return false;
379 }
380 case DeclRefExprClass:
381 if (isa<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl()))
382 break;
383 if (Loc) *Loc = getLocStart();
384 return false;
385 case UnaryOperatorClass: {
386 const UnaryOperator *Exp = cast<UnaryOperator>(this);
387
388 // Get the operand value. If this is sizeof/alignof, do not evalute the
389 // operand. This affects C99 6.6p3.
390 if (!Exp->isSizeOfAlignOfOp() &&
391 !Exp->getSubExpr()->isConstantExpr(Ctx, Loc))
392 return false;
393
394 switch (Exp->getOpcode()) {
395 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
396 // See C99 6.6p3.
397 default:
398 if (Loc) *Loc = Exp->getOperatorLoc();
399 return false;
400 case UnaryOperator::Extension:
401 return true; // FIXME: this is wrong.
402 case UnaryOperator::SizeOf:
403 case UnaryOperator::AlignOf:
404 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
405 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
406 return false;
407 break;
408 case UnaryOperator::LNot:
409 case UnaryOperator::Plus:
410 case UnaryOperator::Minus:
411 case UnaryOperator::Not:
412 break;
413 }
414 break;
415 }
416 case SizeOfAlignOfTypeExprClass: {
417 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
418 // alignof always evaluates to a constant.
419 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
420 return false;
421 break;
422 }
423 case BinaryOperatorClass: {
424 const BinaryOperator *Exp = cast<BinaryOperator>(this);
425
426 // The LHS of a constant expr is always evaluated and needed.
427 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
428 return false;
429
430 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
431 return false;
432
433 break;
434 }
435 case ImplicitCastExprClass:
436 case CastExprClass: {
437 const Expr *SubExpr;
438 SourceLocation CastLoc;
439 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
440 SubExpr = C->getSubExpr();
441 CastLoc = C->getLParenLoc();
442 } else {
443 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
444 CastLoc = getLocStart();
445 }
446 if (!SubExpr->isConstantExpr(Ctx, Loc)) {
447 if (Loc) *Loc = SubExpr->getLocStart();
448 return false;
449 }
450 break;
451 }
452 case ConditionalOperatorClass: {
453 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
454
455 if (!Exp->getCond()->isConstantExpr(Ctx, Loc))
456 return false;
457
458 if (!Exp->getLHS()->isConstantExpr(Ctx, Loc))
459 return false;
460
461 if (!Exp->getRHS()->isConstantExpr(Ctx, Loc))
462 return false;
463 break;
464 }
465 }
466
467 return true;
468}
469
Chris Lattner1f4479e2007-06-05 04:15:44 +0000470/// isIntegerConstantExpr - this recursive routine will test if an expression is
471/// an integer constant expression. Note: With the introduction of VLA's in
Steve Narofff8a28c52007-05-15 20:29:32 +0000472/// C99 the result of the sizeof operator is no longer always a constant
473/// expression. The generalization of the wording to include any subexpression
474/// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions
475/// can appear as operands to other operators (e.g. &&, ||, ?:). For instance,
476/// "0 || f()" can be treated as a constant expression. In C90 this expression,
477/// occurring in a context requiring a constant, would have been a constraint
478/// violation. FIXME: This routine currently implements C90 semantics.
479/// To properly implement C99 semantics this routine will need to evaluate
480/// expressions involving operators previously mentioned.
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000481
482/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
483/// comma, etc
Chris Lattner4ef40012007-06-11 01:28:17 +0000484///
485/// FIXME: This should ext-warn on overflow during evaluation! ISO C does not
486/// permit this.
Chris Lattnerd7372ba2007-07-18 05:21:20 +0000487///
488/// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof
489/// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer
490/// cast+dereference.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000491bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx,
492 SourceLocation *Loc, bool isEvaluated) const {
Steve Naroff8eeeb132007-05-08 21:09:37 +0000493 switch (getStmtClass()) {
Steve Naroff72cada02007-05-18 00:18:52 +0000494 default:
Chris Lattner238cbc52007-06-02 04:48:48 +0000495 if (Loc) *Loc = getLocStart();
Steve Naroff8eeeb132007-05-08 21:09:37 +0000496 return false;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000497 case ParenExprClass:
498 return cast<ParenExpr>(this)->getSubExpr()->
Chris Lattner0e9d6222007-07-15 23:26:56 +0000499 isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000500 case IntegerLiteralClass:
501 Result = cast<IntegerLiteral>(this)->getValue();
502 break;
Chris Lattner5c4664e2007-07-15 23:32:58 +0000503 case CharacterLiteralClass: {
504 const CharacterLiteral *CL = cast<CharacterLiteral>(this);
Chris Lattner9cf21c52007-09-04 02:45:27 +0000505 Result.zextOrTrunc(
506 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CL->getLoc())));
Chris Lattner5c4664e2007-07-15 23:32:58 +0000507 Result = CL->getValue();
Chris Lattner7d138432007-07-16 21:04:56 +0000508 Result.setIsUnsigned(!getType()->isSignedIntegerType());
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000509 break;
Chris Lattner5c4664e2007-07-15 23:32:58 +0000510 }
Steve Naroffa5645cb2007-08-02 04:09:23 +0000511 case TypesCompatibleExprClass: {
512 const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this);
Chris Lattner9cf21c52007-09-04 02:45:27 +0000513 Result.zextOrTrunc(
514 static_cast<uint32_t>(Ctx.getTypeSize(getType(), TCE->getLocStart())));
Steve Naroffa5645cb2007-08-02 04:09:23 +0000515 Result = TCE->typesAreCompatible();
Steve Naroffb3deb2e2007-08-02 00:13:27 +0000516 break;
Steve Naroffa5645cb2007-08-02 04:09:23 +0000517 }
Steve Naroff12b04472007-08-08 22:15:55 +0000518 case CallExprClass: {
519 const CallExpr *CE = cast<CallExpr>(this);
Chris Lattner9cf21c52007-09-04 02:45:27 +0000520 Result.zextOrTrunc(
521 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CE->getLocStart())));
Steve Naroff12b04472007-08-08 22:15:55 +0000522 if (CE->isBuiltinClassifyType(Result))
523 break;
524 if (Loc) *Loc = getLocStart();
525 return false;
526 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000527 case DeclRefExprClass:
528 if (const EnumConstantDecl *D =
529 dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) {
Chris Lattner9fba0282007-06-11 03:47:05 +0000530 Result = D->getInitVal();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000531 break;
532 }
533 if (Loc) *Loc = getLocStart();
534 return false;
535 case UnaryOperatorClass: {
536 const UnaryOperator *Exp = cast<UnaryOperator>(this);
537
538 // Get the operand value. If this is sizeof/alignof, do not evalute the
539 // operand. This affects C99 6.6p3.
Chris Lattner95688802007-08-23 21:42:50 +0000540 if (!Exp->isSizeOfAlignOfOp() &&
541 !Exp->getSubExpr()->isIntegerConstantExpr(Result, Ctx, Loc,isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000542 return false;
543
544 switch (Exp->getOpcode()) {
545 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
546 // See C99 6.6p3.
547 default:
548 if (Loc) *Loc = Exp->getOperatorLoc();
549 return false;
550 case UnaryOperator::Extension:
Chris Lattner0f0019c2007-07-18 18:38:36 +0000551 return true; // FIXME: this is wrong.
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000552 case UnaryOperator::SizeOf:
553 case UnaryOperator::AlignOf:
554 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000555 if (!Exp->getSubExpr()->getType()->isConstantSizeType(Ctx, Loc))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000556 return false;
557
Chris Lattner0f0019c2007-07-18 18:38:36 +0000558 // Return the result in the right width.
Chris Lattner9cf21c52007-09-04 02:45:27 +0000559 Result.zextOrTrunc(
560 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner0f0019c2007-07-18 18:38:36 +0000561
562 // Get information about the size or align.
563 if (Exp->getOpcode() == UnaryOperator::SizeOf)
564 Result = Ctx.getTypeSize(Exp->getSubExpr()->getType(),
565 Exp->getOperatorLoc());
566 else
567 Result = Ctx.getTypeAlign(Exp->getSubExpr()->getType(),
568 Exp->getOperatorLoc());
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000569 break;
570 case UnaryOperator::LNot: {
571 bool Val = Result != 0;
Chris Lattner9cf21c52007-09-04 02:45:27 +0000572 Result.zextOrTrunc(
573 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000574 Result = Val;
575 break;
576 }
577 case UnaryOperator::Plus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000578 break;
579 case UnaryOperator::Minus:
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000580 Result = -Result;
581 break;
582 case UnaryOperator::Not:
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000583 Result = ~Result;
584 break;
585 }
586 break;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000587 }
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000588 case SizeOfAlignOfTypeExprClass: {
589 const SizeOfAlignOfTypeExpr *Exp = cast<SizeOfAlignOfTypeExpr>(this);
590 // alignof always evaluates to a constant.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000591 if (Exp->isSizeOf() && !Exp->getArgumentType()->isConstantSizeType(Ctx,Loc))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000592 return false;
593
Chris Lattner0f0019c2007-07-18 18:38:36 +0000594 // Return the result in the right width.
Chris Lattner9cf21c52007-09-04 02:45:27 +0000595 Result.zextOrTrunc(
596 static_cast<uint32_t>(Ctx.getTypeSize(getType(), Exp->getOperatorLoc())));
Chris Lattner0f0019c2007-07-18 18:38:36 +0000597
598 // Get information about the size or align.
599 if (Exp->isSizeOf())
600 Result = Ctx.getTypeSize(Exp->getArgumentType(), Exp->getOperatorLoc());
601 else
602 Result = Ctx.getTypeAlign(Exp->getArgumentType(), Exp->getOperatorLoc());
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000603 break;
604 }
605 case BinaryOperatorClass: {
606 const BinaryOperator *Exp = cast<BinaryOperator>(this);
607
608 // The LHS of a constant expr is always evaluated and needed.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000609 if (!Exp->getLHS()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000610 return false;
611
Chris Lattner23b7eb62007-06-15 23:05:46 +0000612 llvm::APSInt RHS(Result);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000613
614 // The short-circuiting &&/|| operators don't necessarily evaluate their
615 // RHS. Make sure to pass isEvaluated down correctly.
616 if (Exp->isLogicalOp()) {
617 bool RHSEval;
618 if (Exp->getOpcode() == BinaryOperator::LAnd)
619 RHSEval = Result != 0;
620 else {
621 assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical");
622 RHSEval = Result == 0;
623 }
624
Chris Lattner0e9d6222007-07-15 23:26:56 +0000625 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc,
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000626 isEvaluated & RHSEval))
627 return false;
628 } else {
Chris Lattner0e9d6222007-07-15 23:26:56 +0000629 if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000630 return false;
631 }
632
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000633 switch (Exp->getOpcode()) {
634 default:
635 if (Loc) *Loc = getLocStart();
636 return false;
637 case BinaryOperator::Mul:
638 Result *= RHS;
639 break;
640 case BinaryOperator::Div:
641 if (RHS == 0) {
642 if (!isEvaluated) break;
643 if (Loc) *Loc = getLocStart();
644 return false;
645 }
646 Result /= RHS;
647 break;
648 case BinaryOperator::Rem:
649 if (RHS == 0) {
650 if (!isEvaluated) break;
651 if (Loc) *Loc = getLocStart();
652 return false;
653 }
654 Result %= RHS;
655 break;
656 case BinaryOperator::Add: Result += RHS; break;
657 case BinaryOperator::Sub: Result -= RHS; break;
Chris Lattner901ae1f2007-06-08 21:54:26 +0000658 case BinaryOperator::Shl:
Chris Lattner9cf21c52007-09-04 02:45:27 +0000659 Result <<=
660 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner901ae1f2007-06-08 21:54:26 +0000661 break;
662 case BinaryOperator::Shr:
Chris Lattner9cf21c52007-09-04 02:45:27 +0000663 Result >>=
664 static_cast<uint32_t>(RHS.getLimitedValue(Result.getBitWidth()-1));
Chris Lattner901ae1f2007-06-08 21:54:26 +0000665 break;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000666 case BinaryOperator::LT: Result = Result < RHS; break;
667 case BinaryOperator::GT: Result = Result > RHS; break;
668 case BinaryOperator::LE: Result = Result <= RHS; break;
669 case BinaryOperator::GE: Result = Result >= RHS; break;
670 case BinaryOperator::EQ: Result = Result == RHS; break;
671 case BinaryOperator::NE: Result = Result != RHS; break;
672 case BinaryOperator::And: Result &= RHS; break;
673 case BinaryOperator::Xor: Result ^= RHS; break;
674 case BinaryOperator::Or: Result |= RHS; break;
675 case BinaryOperator::LAnd:
676 Result = Result != 0 && RHS != 0;
677 break;
678 case BinaryOperator::LOr:
679 Result = Result != 0 || RHS != 0;
680 break;
681
682 case BinaryOperator::Comma:
683 // C99 6.6p3: "shall not contain assignment, ..., or comma operators,
684 // *except* when they are contained within a subexpression that is not
685 // evaluated". Note that Assignment can never happen due to constraints
686 // on the LHS subexpr, so we don't need to check it here.
687 if (isEvaluated) {
688 if (Loc) *Loc = getLocStart();
689 return false;
690 }
691
692 // The result of the constant expr is the RHS.
693 Result = RHS;
694 return true;
695 }
696
697 assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!");
698 break;
699 }
Chris Lattner51aff8b2007-07-15 23:54:50 +0000700 case ImplicitCastExprClass:
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000701 case CastExprClass: {
Chris Lattner51aff8b2007-07-15 23:54:50 +0000702 const Expr *SubExpr;
703 SourceLocation CastLoc;
704 if (const CastExpr *C = dyn_cast<CastExpr>(this)) {
705 SubExpr = C->getSubExpr();
706 CastLoc = C->getLParenLoc();
707 } else {
708 SubExpr = cast<ImplicitCastExpr>(this)->getSubExpr();
709 CastLoc = getLocStart();
710 }
711
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000712 // C99 6.6p6: shall only convert arithmetic types to integer types.
Chris Lattner51aff8b2007-07-15 23:54:50 +0000713 if (!SubExpr->getType()->isArithmeticType() ||
714 !getType()->isIntegerType()) {
715 if (Loc) *Loc = SubExpr->getLocStart();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000716 return false;
717 }
718
719 // Handle simple integer->integer casts.
Chris Lattner51aff8b2007-07-15 23:54:50 +0000720 if (SubExpr->getType()->isIntegerType()) {
721 if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000722 return false;
Chris Lattner51aff8b2007-07-15 23:54:50 +0000723
724 // Figure out if this is a truncate, extend or noop cast.
Chris Lattner9cf21c52007-09-04 02:45:27 +0000725 unsigned DestWidth =
726 static_cast<uint32_t>(Ctx.getTypeSize(getType(), CastLoc));
Chris Lattner51aff8b2007-07-15 23:54:50 +0000727
728 // If the input is signed, do a sign extend, noop, or truncate.
729 if (SubExpr->getType()->isSignedIntegerType())
730 Result.sextOrTrunc(DestWidth);
731 else // If the input is unsigned, do a zero extend, noop, or truncate.
732 Result.zextOrTrunc(DestWidth);
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000733 break;
734 }
735
736 // Allow floating constants that are the immediate operands of casts or that
737 // are parenthesized.
Chris Lattner51aff8b2007-07-15 23:54:50 +0000738 const Expr *Operand = SubExpr;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000739 while (const ParenExpr *PE = dyn_cast<ParenExpr>(Operand))
740 Operand = PE->getSubExpr();
741
742 if (const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand)) {
743 // FIXME: Evaluate this correctly!
744 Result = (int)FL->getValue();
745 break;
746 }
Chris Lattner6046e002007-06-05 06:39:23 +0000747 if (Loc) *Loc = Operand->getLocStart();
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000748 return false;
749 }
750 case ConditionalOperatorClass: {
751 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
752
Chris Lattner0e9d6222007-07-15 23:26:56 +0000753 if (!Exp->getCond()->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000754 return false;
755
756 const Expr *TrueExp = Exp->getLHS();
757 const Expr *FalseExp = Exp->getRHS();
758 if (Result == 0) std::swap(TrueExp, FalseExp);
759
760 // Evaluate the false one first, discard the result.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000761 if (!FalseExp->isIntegerConstantExpr(Result, Ctx, Loc, false))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000762 return false;
763 // Evalute the true one, capture the result.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000764 if (!TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated))
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000765 return false;
Chris Lattnere0da5dc2007-06-05 05:58:31 +0000766 break;
767 }
768 }
769
770 // Cases that are valid constant exprs fall through to here.
771 Result.setIsUnsigned(getType()->isUnsignedIntegerType());
772 return true;
Steve Naroff8eeeb132007-05-08 21:09:37 +0000773}
774
Chris Lattner1f4479e2007-06-05 04:15:44 +0000775
Chris Lattner7eef9192007-05-24 01:23:49 +0000776/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
777/// integer constant expression with the value zero, or if this is one that is
778/// cast to void*.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000779bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
Chris Lattner7eef9192007-05-24 01:23:49 +0000780 // Strip off a cast to void*, if it exists.
781 if (const CastExpr *CE = dyn_cast<CastExpr>(this)) {
782 // Check that it is a cast to void*.
783 if (const PointerType *PT = dyn_cast<PointerType>(CE->getType())) {
784 QualType Pointee = PT->getPointeeType();
785 if (Pointee.getQualifiers() == 0 && Pointee->isVoidType() && // to void*
786 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000787 return CE->getSubExpr()->isNullPointerConstant(Ctx);
Steve Naroffada7d422007-05-20 17:54:12 +0000788 }
Steve Naroff1d8ce442007-08-28 21:20:34 +0000789 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
Steve Naroffc5e50272007-08-29 00:00:02 +0000790 // Ignore the ImplicitCastExpr type entirely.
791 return ICE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattner7eef9192007-05-24 01:23:49 +0000792 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
793 // Accept ((void*)0) as a null pointer constant, as many other
794 // implementations do.
Chris Lattner0e9d6222007-07-15 23:26:56 +0000795 return PE->getSubExpr()->isNullPointerConstant(Ctx);
Chris Lattnere6b0f622007-05-21 05:58:35 +0000796 }
797
Chris Lattner7eef9192007-05-24 01:23:49 +0000798 // This expression must be an integer type.
799 if (!getType()->isIntegerType())
800 return false;
801
Chris Lattner1abbd412007-06-08 17:58:43 +0000802 // If we have an integer constant expression, we need to *evaluate* it and
803 // test for the value 0.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000804 llvm::APSInt Val(32);
Chris Lattner0e9d6222007-07-15 23:26:56 +0000805 return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000806}
Steve Narofff7a5da12007-07-28 23:10:27 +0000807
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000808unsigned OCUVectorElementExpr::getNumElements() const {
Chris Lattner177bd452007-08-03 16:00:20 +0000809 return strlen(Accessor.getName());
810}
811
812
Chris Lattner680918a2007-08-02 21:47:28 +0000813/// getComponentType - Determine whether the components of this access are
814/// "point" "color" or "texture" elements.
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000815OCUVectorElementExpr::ElementType
816OCUVectorElementExpr::getElementType() const {
Steve Narofff7a5da12007-07-28 23:10:27 +0000817 // derive the component type, no need to waste space.
818 const char *compStr = Accessor.getName();
Chris Lattnerf1cb1c82007-08-02 22:20:00 +0000819
Chris Lattner7e152db2007-08-02 22:33:49 +0000820 if (OCUVectorType::getPointAccessorIdx(*compStr) != -1) return Point;
821 if (OCUVectorType::getColorAccessorIdx(*compStr) != -1) return Color;
Chris Lattnerf1cb1c82007-08-02 22:20:00 +0000822
Chris Lattner7e152db2007-08-02 22:33:49 +0000823 assert(OCUVectorType::getTextureAccessorIdx(*compStr) != -1 &&
Chris Lattnerf1cb1c82007-08-02 22:20:00 +0000824 "getComponentType(): Illegal accessor");
825 return Texture;
Steve Narofff7a5da12007-07-28 23:10:27 +0000826}
Steve Naroff0d595ca2007-07-30 03:29:09 +0000827
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000828/// containsDuplicateElements - Return true if any element access is
Chris Lattner680918a2007-08-02 21:47:28 +0000829/// repeated.
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000830bool OCUVectorElementExpr::containsDuplicateElements() const {
Steve Naroff0d595ca2007-07-30 03:29:09 +0000831 const char *compStr = Accessor.getName();
832 unsigned length = strlen(compStr);
833
834 for (unsigned i = 0; i < length-1; i++) {
835 const char *s = compStr+i;
836 for (const char c = *s++; *s; s++)
837 if (c == *s)
838 return true;
839 }
840 return false;
841}
Chris Lattner885b4952007-08-02 23:36:59 +0000842
843/// getEncodedElementAccess - We encode fields with two bits per component.
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000844unsigned OCUVectorElementExpr::getEncodedElementAccess() const {
Chris Lattner885b4952007-08-02 23:36:59 +0000845 const char *compStr = Accessor.getName();
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000846 unsigned length = getNumElements();
Chris Lattner885b4952007-08-02 23:36:59 +0000847
848 unsigned Result = 0;
849
850 while (length--) {
851 Result <<= 2;
852 int Idx = OCUVectorType::getAccessorIdx(compStr[length]);
853 assert(Idx != -1 && "Invalid accessor letter");
854 Result |= Idx;
855 }
856 return Result;
857}
858
Steve Naroffd54978b2007-09-18 23:55:05 +0000859// constructor for unary messages.
860ObjCMessageExpr::ObjCMessageExpr(
Steve Naroff2cd263f2007-09-19 16:18:46 +0000861 IdentifierInfo *clsName, IdentifierInfo &methName, QualType retType,
Steve Naroffd54978b2007-09-18 23:55:05 +0000862 SourceLocation LBrac, SourceLocation RBrac)
863 : Expr(ObjCMessageExprClass, retType), Selector(methName) {
864 ClassName = clsName;
865 LBracloc = LBrac;
866 RBracloc = RBrac;
867}
868
869ObjCMessageExpr::ObjCMessageExpr(
Steve Naroff2cd263f2007-09-19 16:18:46 +0000870 Expr *fn, IdentifierInfo &methName, QualType retType,
Steve Naroffd54978b2007-09-18 23:55:05 +0000871 SourceLocation LBrac, SourceLocation RBrac)
872 : Expr(ObjCMessageExprClass, retType), Selector(methName), ClassName(0) {
873 SubExprs = new Expr*[1];
874 SubExprs[RECEIVER] = fn;
875 LBracloc = LBrac;
876 RBracloc = RBrac;
877}
878
879// constructor for keyword messages.
880ObjCMessageExpr::ObjCMessageExpr(
Steve Naroff2cd263f2007-09-19 16:18:46 +0000881 Expr *fn, IdentifierInfo &selInfo, ObjcKeywordMessage *keys, unsigned numargs,
Steve Naroffd54978b2007-09-18 23:55:05 +0000882 QualType retType, SourceLocation LBrac, SourceLocation RBrac)
883 : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(0) {
884 SubExprs = new Expr*[numargs+1];
885 SubExprs[RECEIVER] = fn;
886 for (unsigned i = 0; i != numargs; ++i)
887 SubExprs[i+ARGS_START] = static_cast<Expr *>(keys[i].KeywordExpr);
888 LBracloc = LBrac;
889 RBracloc = RBrac;
890}
891
892ObjCMessageExpr::ObjCMessageExpr(
Steve Naroff2cd263f2007-09-19 16:18:46 +0000893 IdentifierInfo *clsName, IdentifierInfo &selInfo, ObjcKeywordMessage *keys,
Steve Naroffd54978b2007-09-18 23:55:05 +0000894 unsigned numargs, QualType retType, SourceLocation LBrac, SourceLocation RBrac)
895 : Expr(ObjCMessageExprClass, retType), Selector(selInfo), ClassName(clsName) {
896 SubExprs = new Expr*[numargs+1];
897 SubExprs[RECEIVER] = 0;
898 for (unsigned i = 0; i != numargs; ++i)
899 SubExprs[i+ARGS_START] = static_cast<Expr *>(keys[i].KeywordExpr);
900 LBracloc = LBrac;
901 RBracloc = RBrac;
902}
903
904
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000905//===----------------------------------------------------------------------===//
906// Child Iterators for iterating over subexpressions/substatements
907//===----------------------------------------------------------------------===//
908
909// DeclRefExpr
Ted Kremenek23702b62007-08-24 20:06:47 +0000910Stmt::child_iterator DeclRefExpr::child_begin() { return NULL; }
911Stmt::child_iterator DeclRefExpr::child_end() { return NULL; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000912
913// PreDefinedExpr
Ted Kremenek23702b62007-08-24 20:06:47 +0000914Stmt::child_iterator PreDefinedExpr::child_begin() { return NULL; }
915Stmt::child_iterator PreDefinedExpr::child_end() { return NULL; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000916
917// IntegerLiteral
Ted Kremenek23702b62007-08-24 20:06:47 +0000918Stmt::child_iterator IntegerLiteral::child_begin() { return NULL; }
919Stmt::child_iterator IntegerLiteral::child_end() { return NULL; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000920
921// CharacterLiteral
Ted Kremenek23702b62007-08-24 20:06:47 +0000922Stmt::child_iterator CharacterLiteral::child_begin() { return NULL; }
923Stmt::child_iterator CharacterLiteral::child_end() { return NULL; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000924
925// FloatingLiteral
Ted Kremenek23702b62007-08-24 20:06:47 +0000926Stmt::child_iterator FloatingLiteral::child_begin() { return NULL; }
927Stmt::child_iterator FloatingLiteral::child_end() { return NULL; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000928
Chris Lattner1c20a172007-08-26 03:42:43 +0000929// ImaginaryLiteral
930Stmt::child_iterator ImaginaryLiteral::child_begin() {
931 return reinterpret_cast<Stmt**>(&Val);
932}
933Stmt::child_iterator ImaginaryLiteral::child_end() {
934 return reinterpret_cast<Stmt**>(&Val)+1;
935}
936
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000937// StringLiteral
Ted Kremenek23702b62007-08-24 20:06:47 +0000938Stmt::child_iterator StringLiteral::child_begin() { return NULL; }
939Stmt::child_iterator StringLiteral::child_end() { return NULL; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000940
941// ParenExpr
942Stmt::child_iterator ParenExpr::child_begin() {
943 return reinterpret_cast<Stmt**>(&Val);
944}
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000945Stmt::child_iterator ParenExpr::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +0000946 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000947}
948
949// UnaryOperator
950Stmt::child_iterator UnaryOperator::child_begin() {
951 return reinterpret_cast<Stmt**>(&Val);
952}
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000953Stmt::child_iterator UnaryOperator::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +0000954 return reinterpret_cast<Stmt**>(&Val)+1;
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000955}
956
957// SizeOfAlignOfTypeExpr
Chris Lattner1c20a172007-08-26 03:42:43 +0000958Stmt::child_iterator SizeOfAlignOfTypeExpr::child_begin() { return NULL; }
959Stmt::child_iterator SizeOfAlignOfTypeExpr::child_end() { return NULL; }
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000960
961// ArraySubscriptExpr
Ted Kremenek23702b62007-08-24 20:06:47 +0000962Stmt::child_iterator ArraySubscriptExpr::child_begin() {
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000963 return reinterpret_cast<Stmt**>(&SubExprs);
964}
Ted Kremenek23702b62007-08-24 20:06:47 +0000965Stmt::child_iterator ArraySubscriptExpr::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +0000966 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000967}
968
969// CallExpr
Ted Kremenek23702b62007-08-24 20:06:47 +0000970Stmt::child_iterator CallExpr::child_begin() {
Ted Kremenek251c9542007-08-27 21:11:44 +0000971 return reinterpret_cast<Stmt**>(&SubExprs[0]);
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000972}
Ted Kremenek23702b62007-08-24 20:06:47 +0000973Stmt::child_iterator CallExpr::child_end() {
Ted Kremenek251c9542007-08-27 21:11:44 +0000974 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000975}
Ted Kremenek23702b62007-08-24 20:06:47 +0000976
977// MemberExpr
978Stmt::child_iterator MemberExpr::child_begin() {
979 return reinterpret_cast<Stmt**>(&Base);
980}
Ted Kremenek23702b62007-08-24 20:06:47 +0000981Stmt::child_iterator MemberExpr::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +0000982 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek23702b62007-08-24 20:06:47 +0000983}
984
985// OCUVectorElementExpr
986Stmt::child_iterator OCUVectorElementExpr::child_begin() {
987 return reinterpret_cast<Stmt**>(&Base);
988}
Ted Kremenek23702b62007-08-24 20:06:47 +0000989Stmt::child_iterator OCUVectorElementExpr::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +0000990 return reinterpret_cast<Stmt**>(&Base)+1;
Ted Kremenek23702b62007-08-24 20:06:47 +0000991}
992
993// CompoundLiteralExpr
994Stmt::child_iterator CompoundLiteralExpr::child_begin() {
995 return reinterpret_cast<Stmt**>(&Init);
996}
Ted Kremenek23702b62007-08-24 20:06:47 +0000997Stmt::child_iterator CompoundLiteralExpr::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +0000998 return reinterpret_cast<Stmt**>(&Init)+1;
Ted Kremenek23702b62007-08-24 20:06:47 +0000999}
1000
1001// ImplicitCastExpr
1002Stmt::child_iterator ImplicitCastExpr::child_begin() {
1003 return reinterpret_cast<Stmt**>(&Op);
1004}
Ted Kremenek23702b62007-08-24 20:06:47 +00001005Stmt::child_iterator ImplicitCastExpr::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +00001006 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek23702b62007-08-24 20:06:47 +00001007}
1008
1009// CastExpr
1010Stmt::child_iterator CastExpr::child_begin() {
1011 return reinterpret_cast<Stmt**>(&Op);
1012}
Ted Kremenek23702b62007-08-24 20:06:47 +00001013Stmt::child_iterator CastExpr::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +00001014 return reinterpret_cast<Stmt**>(&Op)+1;
Ted Kremenek23702b62007-08-24 20:06:47 +00001015}
1016
1017// BinaryOperator
1018Stmt::child_iterator BinaryOperator::child_begin() {
1019 return reinterpret_cast<Stmt**>(&SubExprs);
1020}
Ted Kremenek23702b62007-08-24 20:06:47 +00001021Stmt::child_iterator BinaryOperator::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +00001022 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00001023}
1024
1025// ConditionalOperator
1026Stmt::child_iterator ConditionalOperator::child_begin() {
1027 return reinterpret_cast<Stmt**>(&SubExprs);
1028}
Ted Kremenek23702b62007-08-24 20:06:47 +00001029Stmt::child_iterator ConditionalOperator::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +00001030 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00001031}
1032
1033// AddrLabelExpr
1034Stmt::child_iterator AddrLabelExpr::child_begin() { return NULL; }
1035Stmt::child_iterator AddrLabelExpr::child_end() { return NULL; }
1036
Ted Kremenek23702b62007-08-24 20:06:47 +00001037// StmtExpr
1038Stmt::child_iterator StmtExpr::child_begin() {
1039 return reinterpret_cast<Stmt**>(&SubStmt);
1040}
Ted Kremenek23702b62007-08-24 20:06:47 +00001041Stmt::child_iterator StmtExpr::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +00001042 return reinterpret_cast<Stmt**>(&SubStmt)+1;
Ted Kremenek23702b62007-08-24 20:06:47 +00001043}
1044
1045// TypesCompatibleExpr
1046Stmt::child_iterator TypesCompatibleExpr::child_begin() { return NULL; }
1047Stmt::child_iterator TypesCompatibleExpr::child_end() { return NULL; }
1048
1049// ChooseExpr
1050Stmt::child_iterator ChooseExpr::child_begin() {
1051 return reinterpret_cast<Stmt**>(&SubExprs);
1052}
1053
1054Stmt::child_iterator ChooseExpr::child_end() {
Chris Lattner1c20a172007-08-26 03:42:43 +00001055 return reinterpret_cast<Stmt**>(&SubExprs)+END_EXPR;
Ted Kremenek23702b62007-08-24 20:06:47 +00001056}
1057
Anders Carlsson4692db02007-08-31 04:56:16 +00001058// InitListExpr
1059Stmt::child_iterator InitListExpr::child_begin() {
1060 return reinterpret_cast<Stmt**>(&InitExprs[0]);
1061}
1062Stmt::child_iterator InitListExpr::child_end() {
1063 return reinterpret_cast<Stmt**>(&InitExprs[NumInits]);
1064}
1065
Ted Kremenek23702b62007-08-24 20:06:47 +00001066// ObjCStringLiteral
1067Stmt::child_iterator ObjCStringLiteral::child_begin() { return NULL; }
1068Stmt::child_iterator ObjCStringLiteral::child_end() { return NULL; }
1069
1070// ObjCEncodeExpr
1071Stmt::child_iterator ObjCEncodeExpr::child_begin() { return NULL; }
1072Stmt::child_iterator ObjCEncodeExpr::child_end() { return NULL; }
1073
Steve Naroffd54978b2007-09-18 23:55:05 +00001074// ObjCMessageExpr
1075Stmt::child_iterator ObjCMessageExpr::child_begin() {
1076 return reinterpret_cast<Stmt**>(&SubExprs[0]);
1077}
1078Stmt::child_iterator ObjCMessageExpr::child_end() {
1079 return reinterpret_cast<Stmt**>(&SubExprs[NumArgs+ARGS_START]);
1080}
1081