Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Expr.cpp - Expression AST Node Implementation --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr class and subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 14 | #include "clang/AST/Expr.h" |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/APValue.h" |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | da5a6b6 | 2007-11-27 18:22:04 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Primary Expressions. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 29 | /// getValueAsApproximateDouble - This returns the value as an inaccurate |
| 30 | /// double. Note that this may cause loss of precision, but is useful for |
| 31 | /// debugging dumps, etc. |
| 32 | double FloatingLiteral::getValueAsApproximateDouble() const { |
| 33 | llvm::APFloat V = getValue(); |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 34 | bool ignored; |
| 35 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 36 | &ignored); |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 37 | return V.convertToDouble(); |
| 38 | } |
| 39 | |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 40 | StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData, |
| 41 | unsigned ByteLength, bool Wide, |
| 42 | QualType Ty, |
| 43 | SourceLocation *Loc, unsigned NumStrs) { |
| 44 | // Allocate enough space for the StringLiteral plus an array of locations for |
| 45 | // any concatenated string tokens. |
| 46 | void *Mem = C.Allocate(sizeof(StringLiteral)+ |
| 47 | sizeof(SourceLocation)*(NumStrs-1), |
| 48 | llvm::alignof<StringLiteral>()); |
| 49 | StringLiteral *SL = new (Mem) StringLiteral(Ty); |
| 50 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | // OPTIMIZE: could allocate this appended to the StringLiteral. |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 52 | char *AStrData = new (C, 1) char[ByteLength]; |
| 53 | memcpy(AStrData, StrData, ByteLength); |
| 54 | SL->StrData = AStrData; |
| 55 | SL->ByteLength = ByteLength; |
| 56 | SL->IsWide = Wide; |
| 57 | SL->TokLocs[0] = Loc[0]; |
| 58 | SL->NumConcatenated = NumStrs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 60 | if (NumStrs != 1) |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 61 | memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1)); |
| 62 | return SL; |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 66 | void StringLiteral::Destroy(ASTContext &C) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 67 | C.Deallocate(const_cast<char*>(StrData)); |
Ted Kremenek | 353ffce | 2009-02-09 17:10:09 +0000 | [diff] [blame] | 68 | this->~StringLiteral(); |
| 69 | C.Deallocate(this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 73 | /// corresponds to, e.g. "sizeof" or "[pre]++". |
| 74 | const char *UnaryOperator::getOpcodeStr(Opcode Op) { |
| 75 | switch (Op) { |
| 76 | default: assert(0 && "Unknown unary operator"); |
| 77 | case PostInc: return "++"; |
| 78 | case PostDec: return "--"; |
| 79 | case PreInc: return "++"; |
| 80 | case PreDec: return "--"; |
| 81 | case AddrOf: return "&"; |
| 82 | case Deref: return "*"; |
| 83 | case Plus: return "+"; |
| 84 | case Minus: return "-"; |
| 85 | case Not: return "~"; |
| 86 | case LNot: return "!"; |
| 87 | case Real: return "__real"; |
| 88 | case Imag: return "__imag"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | case Extension: return "__extension__"; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 90 | case OffsetOf: return "__builtin_offsetof"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | // Postfix Operators. |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 98 | CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 99 | unsigned numargs, QualType t, SourceLocation rparenloc) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 100 | : Expr(SC, t, |
| 101 | fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 102 | fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 103 | NumArgs(numargs) { |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 104 | |
| 105 | SubExprs = new (C) Stmt*[numargs+1]; |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 106 | SubExprs[FN] = fn; |
| 107 | for (unsigned i = 0; i != numargs; ++i) |
| 108 | SubExprs[i+ARGS_START] = args[i]; |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 110 | RParenLoc = rparenloc; |
| 111 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 113 | CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, |
| 114 | QualType t, SourceLocation rparenloc) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 115 | : Expr(CallExprClass, t, |
| 116 | fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 117 | fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 118 | NumArgs(numargs) { |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 119 | |
| 120 | SubExprs = new (C) Stmt*[numargs+1]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 121 | SubExprs[FN] = fn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | for (unsigned i = 0; i != numargs; ++i) |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 123 | SubExprs[i+ARGS_START] = args[i]; |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 124 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | RParenLoc = rparenloc; |
| 126 | } |
| 127 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 128 | void CallExpr::Destroy(ASTContext& C) { |
| 129 | DestroyChildren(C); |
| 130 | if (SubExprs) C.Deallocate(SubExprs); |
| 131 | this->~CallExpr(); |
| 132 | C.Deallocate(this); |
| 133 | } |
| 134 | |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 135 | /// setNumArgs - This changes the number of arguments present in this call. |
| 136 | /// Any orphaned expressions are deleted by this, and any new operands are set |
| 137 | /// to null. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 138 | void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) { |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 139 | // No change, just return. |
| 140 | if (NumArgs == getNumArgs()) return; |
| 141 | |
| 142 | // If shrinking # arguments, just delete the extras and forgot them. |
| 143 | if (NumArgs < getNumArgs()) { |
| 144 | for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 145 | getArg(i)->Destroy(C); |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 146 | this->NumArgs = NumArgs; |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | // Otherwise, we are growing the # arguments. New an bigger argument array. |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 151 | Stmt **NewSubExprs = new Stmt*[NumArgs+1]; |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 152 | // Copy over args. |
| 153 | for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i) |
| 154 | NewSubExprs[i] = SubExprs[i]; |
| 155 | // Null out new args. |
| 156 | for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) |
| 157 | NewSubExprs[i] = 0; |
| 158 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 159 | delete [] SubExprs; |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 160 | SubExprs = NewSubExprs; |
| 161 | this->NumArgs = NumArgs; |
| 162 | } |
| 163 | |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 164 | /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If |
| 165 | /// not, return 0. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 166 | unsigned CallExpr::isBuiltinCall(ASTContext &Context) const { |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 167 | // All simple function calls (e.g. func()) are implicitly cast to pointer to |
| 168 | // function. As a result, we try and obtain the DeclRefExpr from the |
| 169 | // ImplicitCastExpr. |
| 170 | const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); |
| 171 | if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 172 | return 0; |
| 173 | |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 174 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); |
| 175 | if (!DRE) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 176 | return 0; |
| 177 | |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 178 | const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 179 | if (!FDecl) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | |
Douglas Gregor | 4fcd399 | 2008-11-21 15:30:19 +0000 | [diff] [blame] | 182 | if (!FDecl->getIdentifier()) |
| 183 | return 0; |
| 184 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 185 | return FDecl->getBuiltinID(Context); |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 186 | } |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 187 | |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 188 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 190 | /// corresponds to, e.g. "<<=". |
| 191 | const char *BinaryOperator::getOpcodeStr(Opcode Op) { |
| 192 | switch (Op) { |
Douglas Gregor | baf5348 | 2009-03-12 22:51:37 +0000 | [diff] [blame] | 193 | case PtrMemD: return ".*"; |
| 194 | case PtrMemI: return "->*"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | case Mul: return "*"; |
| 196 | case Div: return "/"; |
| 197 | case Rem: return "%"; |
| 198 | case Add: return "+"; |
| 199 | case Sub: return "-"; |
| 200 | case Shl: return "<<"; |
| 201 | case Shr: return ">>"; |
| 202 | case LT: return "<"; |
| 203 | case GT: return ">"; |
| 204 | case LE: return "<="; |
| 205 | case GE: return ">="; |
| 206 | case EQ: return "=="; |
| 207 | case NE: return "!="; |
| 208 | case And: return "&"; |
| 209 | case Xor: return "^"; |
| 210 | case Or: return "|"; |
| 211 | case LAnd: return "&&"; |
| 212 | case LOr: return "||"; |
| 213 | case Assign: return "="; |
| 214 | case MulAssign: return "*="; |
| 215 | case DivAssign: return "/="; |
| 216 | case RemAssign: return "%="; |
| 217 | case AddAssign: return "+="; |
| 218 | case SubAssign: return "-="; |
| 219 | case ShlAssign: return "<<="; |
| 220 | case ShrAssign: return ">>="; |
| 221 | case AndAssign: return "&="; |
| 222 | case XorAssign: return "^="; |
| 223 | case OrAssign: return "|="; |
| 224 | case Comma: return ","; |
| 225 | } |
Douglas Gregor | baf5348 | 2009-03-12 22:51:37 +0000 | [diff] [blame] | 226 | |
| 227 | return ""; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 230 | BinaryOperator::Opcode |
| 231 | BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) { |
| 232 | switch (OO) { |
| 233 | case OO_Plus: return Add; |
| 234 | case OO_Minus: return Sub; |
| 235 | case OO_Star: return Mul; |
| 236 | case OO_Slash: return Div; |
| 237 | case OO_Percent: return Rem; |
| 238 | case OO_Caret: return Xor; |
| 239 | case OO_Amp: return And; |
| 240 | case OO_Pipe: return Or; |
| 241 | case OO_Equal: return Assign; |
| 242 | case OO_Less: return LT; |
| 243 | case OO_Greater: return GT; |
| 244 | case OO_PlusEqual: return AddAssign; |
| 245 | case OO_MinusEqual: return SubAssign; |
| 246 | case OO_StarEqual: return MulAssign; |
| 247 | case OO_SlashEqual: return DivAssign; |
| 248 | case OO_PercentEqual: return RemAssign; |
| 249 | case OO_CaretEqual: return XorAssign; |
| 250 | case OO_AmpEqual: return AndAssign; |
| 251 | case OO_PipeEqual: return OrAssign; |
| 252 | case OO_LessLess: return Shl; |
| 253 | case OO_GreaterGreater: return Shr; |
| 254 | case OO_LessLessEqual: return ShlAssign; |
| 255 | case OO_GreaterGreaterEqual: return ShrAssign; |
| 256 | case OO_EqualEqual: return EQ; |
| 257 | case OO_ExclaimEqual: return NE; |
| 258 | case OO_LessEqual: return LE; |
| 259 | case OO_GreaterEqual: return GE; |
| 260 | case OO_AmpAmp: return LAnd; |
| 261 | case OO_PipePipe: return LOr; |
| 262 | case OO_Comma: return Comma; |
| 263 | case OO_ArrowStar: return PtrMemI; |
| 264 | default: assert(false && "Not an overloadable binary operator"); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) { |
| 269 | static const OverloadedOperatorKind OverOps[] = { |
| 270 | /* .* Cannot be overloaded */OO_None, OO_ArrowStar, |
| 271 | OO_Star, OO_Slash, OO_Percent, |
| 272 | OO_Plus, OO_Minus, |
| 273 | OO_LessLess, OO_GreaterGreater, |
| 274 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 275 | OO_EqualEqual, OO_ExclaimEqual, |
| 276 | OO_Amp, |
| 277 | OO_Caret, |
| 278 | OO_Pipe, |
| 279 | OO_AmpAmp, |
| 280 | OO_PipePipe, |
| 281 | OO_Equal, OO_StarEqual, |
| 282 | OO_SlashEqual, OO_PercentEqual, |
| 283 | OO_PlusEqual, OO_MinusEqual, |
| 284 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 285 | OO_AmpEqual, OO_CaretEqual, |
| 286 | OO_PipeEqual, |
| 287 | OO_Comma |
| 288 | }; |
| 289 | return OverOps[Opc]; |
| 290 | } |
| 291 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 292 | InitListExpr::InitListExpr(SourceLocation lbraceloc, |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 293 | Expr **initExprs, unsigned numInits, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 294 | SourceLocation rbraceloc) |
Steve Naroff | c5ae899 | 2008-05-01 02:04:18 +0000 | [diff] [blame] | 295 | : Expr(InitListExprClass, QualType()), |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 296 | LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0), |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 297 | UnionFieldInit(0), HadArrayRangeDesignator(false) { |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 298 | |
| 299 | InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 300 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 301 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 302 | void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) { |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 303 | for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); |
Daniel Dunbar | f592c92 | 2009-02-16 22:42:44 +0000 | [diff] [blame] | 304 | Idx < LastIdx; ++Idx) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 305 | delete InitExprs[Idx]; |
| 306 | InitExprs.resize(NumInits, 0); |
| 307 | } |
| 308 | |
| 309 | Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) { |
| 310 | if (Init >= InitExprs.size()) { |
| 311 | InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0); |
| 312 | InitExprs.back() = expr; |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | Expr *Result = cast_or_null<Expr>(InitExprs[Init]); |
| 317 | InitExprs[Init] = expr; |
| 318 | return Result; |
| 319 | } |
| 320 | |
Steve Naroff | bfdcae6 | 2008-09-04 15:31:07 +0000 | [diff] [blame] | 321 | /// getFunctionType - Return the underlying function type for this block. |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 322 | /// |
| 323 | const FunctionType *BlockExpr::getFunctionType() const { |
| 324 | return getType()->getAsBlockPointerType()-> |
| 325 | getPointeeType()->getAsFunctionType(); |
| 326 | } |
| 327 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 328 | SourceLocation BlockExpr::getCaretLocation() const { |
| 329 | return TheBlock->getCaretLocation(); |
| 330 | } |
| 331 | const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); } |
| 332 | Stmt *BlockExpr::getBody() { return TheBlock->getBody(); } |
| 333 | |
| 334 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | //===----------------------------------------------------------------------===// |
| 336 | // Generic Expression Routines |
| 337 | //===----------------------------------------------------------------------===// |
| 338 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 339 | /// isUnusedResultAWarning - Return true if this immediate expression should |
| 340 | /// be warned about if the result is unused. If so, fill in Loc and Ranges |
| 341 | /// with location to warn on and the source range[s] to report with the |
| 342 | /// warning. |
| 343 | bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, |
| 344 | SourceRange &R2) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 345 | switch (getStmtClass()) { |
| 346 | default: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 347 | Loc = getExprLoc(); |
| 348 | R1 = getSourceRange(); |
| 349 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | case ParenExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 351 | return cast<ParenExpr>(this)->getSubExpr()-> |
| 352 | isUnusedResultAWarning(Loc, R1, R2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | case UnaryOperatorClass: { |
| 354 | const UnaryOperator *UO = cast<UnaryOperator>(this); |
| 355 | |
| 356 | switch (UO->getOpcode()) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 357 | default: break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 358 | case UnaryOperator::PostInc: |
| 359 | case UnaryOperator::PostDec: |
| 360 | case UnaryOperator::PreInc: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 361 | case UnaryOperator::PreDec: // ++/-- |
| 362 | return false; // Not a warning. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | case UnaryOperator::Deref: |
| 364 | // Dereferencing a volatile pointer is a side-effect. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 365 | if (getType().isVolatileQualified()) |
| 366 | return false; |
| 367 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 368 | case UnaryOperator::Real: |
| 369 | case UnaryOperator::Imag: |
| 370 | // accessing a piece of a volatile complex is a side-effect. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 371 | if (UO->getSubExpr()->getType().isVolatileQualified()) |
| 372 | return false; |
| 373 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 374 | case UnaryOperator::Extension: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 375 | return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 376 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 377 | Loc = UO->getOperatorLoc(); |
| 378 | R1 = UO->getSubExpr()->getSourceRange(); |
| 379 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 380 | } |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 381 | case BinaryOperatorClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 382 | const BinaryOperator *BO = cast<BinaryOperator>(this); |
| 383 | // Consider comma to have side effects if the LHS or RHS does. |
| 384 | if (BO->getOpcode() == BinaryOperator::Comma) |
| 385 | return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) || |
| 386 | BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2); |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 387 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 388 | if (BO->isAssignmentOp()) |
| 389 | return false; |
| 390 | Loc = BO->getOperatorLoc(); |
| 391 | R1 = BO->getLHS()->getSourceRange(); |
| 392 | R2 = BO->getRHS()->getSourceRange(); |
| 393 | return true; |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 394 | } |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 395 | case CompoundAssignOperatorClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 396 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 397 | |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 398 | case ConditionalOperatorClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 399 | // The condition must be evaluated, but if either the LHS or RHS is a |
| 400 | // warning, warn about them. |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 401 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
Mike Stump | befbcf4 | 2009-02-27 03:16:57 +0000 | [diff] [blame] | 402 | if (Exp->getLHS() && Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 403 | return true; |
| 404 | return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2); |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 407 | case MemberExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 408 | // If the base pointer or element is to a volatile pointer/field, accessing |
| 409 | // it is a side effect. |
| 410 | if (getType().isVolatileQualified()) |
| 411 | return false; |
| 412 | Loc = cast<MemberExpr>(this)->getMemberLoc(); |
| 413 | R1 = SourceRange(Loc, Loc); |
| 414 | R2 = cast<MemberExpr>(this)->getBase()->getSourceRange(); |
| 415 | return true; |
| 416 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | case ArraySubscriptExprClass: |
| 418 | // If the base pointer or element is to a volatile pointer/field, accessing |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 419 | // it is a side effect. |
| 420 | if (getType().isVolatileQualified()) |
| 421 | return false; |
| 422 | Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc(); |
| 423 | R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange(); |
| 424 | R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange(); |
| 425 | return true; |
Eli Friedman | 211f6ad | 2008-05-27 15:24:04 +0000 | [diff] [blame] | 426 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | case CallExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 428 | case CXXOperatorCallExprClass: { |
| 429 | // If this is a direct call, get the callee. |
| 430 | const CallExpr *CE = cast<CallExpr>(this); |
| 431 | const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts(); |
| 432 | if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) { |
| 433 | // If the callee has attribute pure, const, or warn_unused_result, warn |
| 434 | // about it. void foo() { strlen("bar"); } should warn. |
| 435 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl())) |
| 436 | if (FD->getAttr<WarnUnusedResultAttr>() || |
| 437 | FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) { |
| 438 | Loc = CE->getCallee()->getLocStart(); |
| 439 | R1 = CE->getCallee()->getSourceRange(); |
| 440 | |
| 441 | if (unsigned NumArgs = CE->getNumArgs()) |
| 442 | R2 = SourceRange(CE->getArg(0)->getLocStart(), |
| 443 | CE->getArg(NumArgs-1)->getLocEnd()); |
| 444 | return true; |
| 445 | } |
| 446 | } |
| 447 | return false; |
| 448 | } |
Chris Lattner | a9c0102 | 2007-09-26 22:06:30 +0000 | [diff] [blame] | 449 | case ObjCMessageExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 450 | return false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 451 | case StmtExprClass: { |
| 452 | // Statement exprs don't logically have side effects themselves, but are |
| 453 | // sometimes used in macros in ways that give them a type that is unused. |
| 454 | // For example ({ blah; foo(); }) will end up with a type if foo has a type. |
| 455 | // however, if the result of the stmt expr is dead, we don't want to emit a |
| 456 | // warning. |
| 457 | const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); |
| 458 | if (!CS->body_empty()) |
| 459 | if (const Expr *E = dyn_cast<Expr>(CS->body_back())) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 460 | return E->isUnusedResultAWarning(Loc, R1, R2); |
| 461 | |
| 462 | Loc = cast<StmtExpr>(this)->getLParenLoc(); |
| 463 | R1 = getSourceRange(); |
| 464 | return true; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 465 | } |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 466 | case CStyleCastExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 467 | // If this is a cast to void, check the operand. Otherwise, the result of |
| 468 | // the cast is unused. |
| 469 | if (getType()->isVoidType()) |
| 470 | return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc, |
| 471 | R1, R2); |
| 472 | Loc = cast<CStyleCastExpr>(this)->getLParenLoc(); |
| 473 | R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange(); |
| 474 | return true; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 475 | case CXXFunctionalCastExprClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 476 | // If this is a cast to void, check the operand. Otherwise, the result of |
| 477 | // the cast is unused. |
| 478 | if (getType()->isVoidType()) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 479 | return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc, |
| 480 | R1, R2); |
| 481 | Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc(); |
| 482 | R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange(); |
| 483 | return true; |
| 484 | |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 485 | case ImplicitCastExprClass: |
| 486 | // Check the operand, since implicit casts are inserted by Sema |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 487 | return cast<ImplicitCastExpr>(this) |
| 488 | ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 490 | case CXXDefaultArgExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 491 | return cast<CXXDefaultArgExpr>(this) |
| 492 | ->getExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 493 | |
| 494 | case CXXNewExprClass: |
| 495 | // FIXME: In theory, there might be new expressions that don't have side |
| 496 | // effects (e.g. a placement new with an uninitialized POD). |
| 497 | case CXXDeleteExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 498 | return false; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 499 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 502 | /// DeclCanBeLvalue - Determine whether the given declaration can be |
| 503 | /// an lvalue. This is a helper routine for isLvalue. |
| 504 | static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 505 | // C++ [temp.param]p6: |
| 506 | // A non-type non-reference template-parameter is not an lvalue. |
| 507 | if (const NonTypeTemplateParmDecl *NTTParm |
| 508 | = dyn_cast<NonTypeTemplateParmDecl>(Decl)) |
| 509 | return NTTParm->getType()->isReferenceType(); |
| 510 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 511 | return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) || |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 512 | // C++ 3.10p2: An lvalue refers to an object or function. |
| 513 | (Ctx.getLangOptions().CPlusPlus && |
| 514 | (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl))); |
| 515 | } |
| 516 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 517 | /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an |
| 518 | /// incomplete type other than void. Nonarray expressions that can be lvalues: |
| 519 | /// - name, where name must be a variable |
| 520 | /// - e[i] |
| 521 | /// - (e), where e must be an lvalue |
| 522 | /// - e.name, where e must be an lvalue |
| 523 | /// - e->name |
| 524 | /// - *e, the type of e cannot be a function type |
| 525 | /// - string-constant |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 526 | /// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 527 | /// - reference type [C++ [expr]] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 528 | /// |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 529 | Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 530 | // first, check the type (C99 6.3.2.1). Expressions with function |
| 531 | // type in C are not lvalues, but they can be lvalues in C++. |
| 532 | if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 533 | return LV_NotObjectType; |
| 534 | |
Steve Naroff | acb818a | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 535 | // Allow qualified void which is an incomplete type other than void (yuck). |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 536 | if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers()) |
Steve Naroff | acb818a | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 537 | return LV_IncompleteVoidType; |
| 538 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 539 | /// FIXME: Expressions can't have reference type, so the following |
| 540 | /// isn't needed. |
Chris Lattner | cb4f9a6 | 2007-07-21 05:33:26 +0000 | [diff] [blame] | 541 | if (TR->isReferenceType()) // C++ [expr] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 542 | return LV_Valid; |
| 543 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 544 | // the type looks fine, now check the expression |
| 545 | switch (getStmtClass()) { |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 546 | case StringLiteralClass: // C99 6.5.1p4 |
| 547 | case ObjCEncodeExprClass: // @encode behaves like its string in every way. |
Anders Carlsson | 7323a62 | 2007-11-30 22:47:59 +0000 | [diff] [blame] | 548 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 549 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
| 550 | // For vectors, make sure base is an lvalue (i.e. not a function call). |
| 551 | if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 552 | return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 553 | return LV_Valid; |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 554 | case DeclRefExprClass: |
| 555 | case QualifiedDeclRefExprClass: { // C99 6.5.1p2 |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 556 | const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl(); |
| 557 | if (DeclCanBeLvalue(RefdDecl, Ctx)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 558 | return LV_Valid; |
| 559 | break; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 560 | } |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 561 | case BlockDeclRefExprClass: { |
| 562 | const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 563 | if (isa<VarDecl>(BDR->getDecl())) |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 564 | return LV_Valid; |
| 565 | break; |
| 566 | } |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 567 | case MemberExprClass: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 568 | const MemberExpr *m = cast<MemberExpr>(this); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 569 | if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4: |
| 570 | NamedDecl *Member = m->getMemberDecl(); |
| 571 | // C++ [expr.ref]p4: |
| 572 | // If E2 is declared to have type "reference to T", then E1.E2 |
| 573 | // is an lvalue. |
| 574 | if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) |
| 575 | if (Value->getType()->isReferenceType()) |
| 576 | return LV_Valid; |
| 577 | |
| 578 | // -- If E2 is a static data member [...] then E1.E2 is an lvalue. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 579 | if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord()) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 580 | return LV_Valid; |
| 581 | |
| 582 | // -- If E2 is a non-static data member [...]. If E1 is an |
| 583 | // lvalue, then E1.E2 is an lvalue. |
| 584 | if (isa<FieldDecl>(Member)) |
| 585 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); |
| 586 | |
| 587 | // -- If it refers to a static member function [...], then |
| 588 | // E1.E2 is an lvalue. |
| 589 | // -- Otherwise, if E1.E2 refers to a non-static member |
| 590 | // function [...], then E1.E2 is not an lvalue. |
| 591 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) |
| 592 | return Method->isStatic()? LV_Valid : LV_MemberFunction; |
| 593 | |
| 594 | // -- If E2 is a member enumerator [...], the expression E1.E2 |
| 595 | // is not an lvalue. |
| 596 | if (isa<EnumConstantDecl>(Member)) |
| 597 | return LV_InvalidExpression; |
| 598 | |
| 599 | // Not an lvalue. |
| 600 | return LV_InvalidExpression; |
| 601 | } |
| 602 | |
| 603 | // C99 6.5.2.3p4 |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 604 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 605 | } |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 606 | case UnaryOperatorClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 607 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 608 | return LV_Valid; // C99 6.5.3p4 |
| 609 | |
| 610 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || |
Chris Lattner | baf0d66 | 2008-07-25 18:07:19 +0000 | [diff] [blame] | 611 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag || |
| 612 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension) |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 613 | return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 614 | |
| 615 | if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1 |
| 616 | (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc || |
| 617 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec)) |
| 618 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 619 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 620 | case ImplicitCastExprClass: |
| 621 | return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid |
| 622 | : LV_InvalidExpression; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 623 | case ParenExprClass: // C99 6.5.1p5 |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 624 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 625 | case BinaryOperatorClass: |
| 626 | case CompoundAssignOperatorClass: { |
| 627 | const BinaryOperator *BinOp = cast<BinaryOperator>(this); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 628 | |
| 629 | if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1 |
| 630 | BinOp->getOpcode() == BinaryOperator::Comma) |
| 631 | return BinOp->getRHS()->isLvalue(Ctx); |
| 632 | |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 633 | // C++ [expr.mptr.oper]p6 |
| 634 | if ((BinOp->getOpcode() == BinaryOperator::PtrMemD || |
| 635 | BinOp->getOpcode() == BinaryOperator::PtrMemI) && |
| 636 | !BinOp->getType()->isFunctionType()) |
| 637 | return BinOp->getLHS()->isLvalue(Ctx); |
| 638 | |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 639 | if (!BinOp->isAssignmentOp()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 640 | return LV_InvalidExpression; |
| 641 | |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 642 | if (Ctx.getLangOptions().CPlusPlus) |
| 643 | // C++ [expr.ass]p1: |
| 644 | // The result of an assignment operation [...] is an lvalue. |
| 645 | return LV_Valid; |
| 646 | |
| 647 | |
| 648 | // C99 6.5.16: |
| 649 | // An assignment expression [...] is not an lvalue. |
| 650 | return LV_InvalidExpression; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 651 | } |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 652 | case CallExprClass: |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 653 | case CXXOperatorCallExprClass: |
| 654 | case CXXMemberCallExprClass: { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 655 | // C++ [expr.call]p10: |
| 656 | // A function call is an lvalue if and only if the result type |
| 657 | // is a reference. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 658 | QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType(); |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 659 | if (const PointerType *FnTypePtr = CalleeType->getAsPointerType()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 660 | CalleeType = FnTypePtr->getPointeeType(); |
| 661 | if (const FunctionType *FnType = CalleeType->getAsFunctionType()) |
| 662 | if (FnType->getResultType()->isReferenceType()) |
| 663 | return LV_Valid; |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 664 | |
| 665 | break; |
| 666 | } |
Steve Naroff | e638639 | 2007-12-05 04:00:10 +0000 | [diff] [blame] | 667 | case CompoundLiteralExprClass: // C99 6.5.2.5p5 |
| 668 | return LV_Valid; |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 669 | case ChooseExprClass: |
| 670 | // __builtin_choose_expr is an lvalue if the selected operand is. |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 671 | return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 672 | case ExtVectorElementExprClass: |
| 673 | if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements()) |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 674 | return LV_DuplicateVectorComponents; |
| 675 | return LV_Valid; |
Steve Naroff | 027282d | 2007-11-12 14:34:27 +0000 | [diff] [blame] | 676 | case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. |
| 677 | return LV_Valid; |
Steve Naroff | 799a6a6 | 2008-05-30 23:23:16 +0000 | [diff] [blame] | 678 | case ObjCPropertyRefExprClass: // FIXME: check if read-only property. |
| 679 | return LV_Valid; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 680 | case ObjCKVCRefExprClass: // FIXME: check if read-only property. |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 681 | return LV_Valid; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 682 | case PredefinedExprClass: |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 683 | return LV_Valid; |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 684 | case VAArgExprClass: |
Daniel Dunbar | adadd8d | 2009-02-12 09:21:08 +0000 | [diff] [blame] | 685 | return LV_NotObjectType; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 686 | case CXXDefaultArgExprClass: |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 687 | return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx); |
Argyrios Kyrtzidis | 24b41fa | 2008-09-11 04:22:26 +0000 | [diff] [blame] | 688 | case CXXConditionDeclExprClass: |
| 689 | return LV_Valid; |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 690 | case CStyleCastExprClass: |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 691 | case CXXFunctionalCastExprClass: |
| 692 | case CXXStaticCastExprClass: |
| 693 | case CXXDynamicCastExprClass: |
| 694 | case CXXReinterpretCastExprClass: |
| 695 | case CXXConstCastExprClass: |
| 696 | // The result of an explicit cast is an lvalue if the type we are |
| 697 | // casting to is a reference type. See C++ [expr.cast]p1, |
| 698 | // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2, |
| 699 | // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1. |
| 700 | if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType()) |
| 701 | return LV_Valid; |
| 702 | break; |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 703 | case CXXTypeidExprClass: |
| 704 | // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ... |
| 705 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 706 | default: |
| 707 | break; |
| 708 | } |
| 709 | return LV_InvalidExpression; |
| 710 | } |
| 711 | |
| 712 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 713 | /// does not have an incomplete type, does not have a const-qualified type, and |
| 714 | /// if it is a structure or union, does not have any member (including, |
| 715 | /// recursively, any member or element of all contained aggregates or unions) |
| 716 | /// with a const-qualified type. |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 717 | Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const { |
| 718 | isLvalueResult lvalResult = isLvalue(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 719 | |
| 720 | switch (lvalResult) { |
Douglas Gregor | ae8d467 | 2008-10-22 00:03:08 +0000 | [diff] [blame] | 721 | case LV_Valid: |
| 722 | // C++ 3.10p11: Functions cannot be modified, but pointers to |
| 723 | // functions can be modifiable. |
| 724 | if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) |
| 725 | return MLV_NotObjectType; |
| 726 | break; |
| 727 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 728 | case LV_NotObjectType: return MLV_NotObjectType; |
| 729 | case LV_IncompleteVoidType: return MLV_IncompleteVoidType; |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 730 | case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 731 | case LV_InvalidExpression: |
| 732 | // If the top level is a C-style cast, and the subexpression is a valid |
| 733 | // lvalue, then this is probably a use of the old-school "cast as lvalue" |
| 734 | // GCC extension. We don't support it, but we want to produce good |
| 735 | // diagnostics when it happens so that the user knows why. |
| 736 | if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this)) |
| 737 | if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) |
| 738 | return MLV_LValueCast; |
| 739 | return MLV_InvalidExpression; |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 740 | case LV_MemberFunction: return MLV_MemberFunction; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 741 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 742 | |
| 743 | QualType CT = Ctx.getCanonicalType(getType()); |
| 744 | |
| 745 | if (CT.isConstQualified()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 746 | return MLV_ConstQualified; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 747 | if (CT->isArrayType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 748 | return MLV_ArrayType; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 749 | if (CT->isIncompleteType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 750 | return MLV_IncompleteType; |
| 751 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 752 | if (const RecordType *r = CT->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 753 | if (r->hasConstFields()) |
| 754 | return MLV_ConstQualified; |
| 755 | } |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 756 | // The following is illegal: |
| 757 | // void takeclosure(void (^C)(void)); |
| 758 | // void func() { int x = 1; takeclosure(^{ x = 7 }); } |
| 759 | // |
| 760 | if (getStmtClass() == BlockDeclRefExprClass) { |
| 761 | const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); |
| 762 | if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl())) |
| 763 | return MLV_NotBlockQualified; |
| 764 | } |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 765 | |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 766 | // Assigning to an 'implicit' property? |
Fariborz Jahanian | 6669db9 | 2008-11-25 17:56:43 +0000 | [diff] [blame] | 767 | else if (getStmtClass() == ObjCKVCRefExprClass) { |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 768 | const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this); |
| 769 | if (KVCExpr->getSetterMethod() == 0) |
| 770 | return MLV_NoSetterProperty; |
| 771 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 772 | return MLV_Valid; |
| 773 | } |
| 774 | |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 775 | /// hasGlobalStorage - Return true if this expression has static storage |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 776 | /// duration. This means that the address of this expression is a link-time |
| 777 | /// constant. |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 778 | bool Expr::hasGlobalStorage() const { |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 779 | switch (getStmtClass()) { |
| 780 | default: |
| 781 | return false; |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 782 | case ParenExprClass: |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 783 | return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage(); |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 784 | case ImplicitCastExprClass: |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 785 | return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 786 | case CompoundLiteralExprClass: |
| 787 | return cast<CompoundLiteralExpr>(this)->isFileScope(); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 788 | case DeclRefExprClass: |
| 789 | case QualifiedDeclRefExprClass: { |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 790 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
| 791 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 792 | return VD->hasGlobalStorage(); |
Seo Sanghyeon | 63f067f | 2008-04-04 09:45:30 +0000 | [diff] [blame] | 793 | if (isa<FunctionDecl>(D)) |
| 794 | return true; |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 795 | return false; |
| 796 | } |
Chris Lattner | fb70806 | 2007-11-28 04:30:09 +0000 | [diff] [blame] | 797 | case MemberExprClass: { |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 798 | const MemberExpr *M = cast<MemberExpr>(this); |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 799 | return !M->isArrow() && M->getBase()->hasGlobalStorage(); |
Chris Lattner | fb70806 | 2007-11-28 04:30:09 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 801 | case ArraySubscriptExprClass: |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 802 | return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage(); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 803 | case PredefinedExprClass: |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 804 | return true; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 805 | case CXXDefaultArgExprClass: |
| 806 | return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage(); |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 810 | /// isOBJCGCCandidate - Check if an expression is objc gc'able. |
| 811 | /// |
| 812 | bool Expr::isOBJCGCCandidate() const { |
| 813 | switch (getStmtClass()) { |
| 814 | default: |
| 815 | return false; |
| 816 | case ObjCIvarRefExprClass: |
| 817 | return true; |
Fariborz Jahanian | 207c521 | 2009-02-23 18:59:50 +0000 | [diff] [blame] | 818 | case Expr::UnaryOperatorClass: |
| 819 | return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 820 | case ParenExprClass: |
| 821 | return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(); |
| 822 | case ImplicitCastExprClass: |
| 823 | return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(); |
| 824 | case DeclRefExprClass: |
| 825 | case QualifiedDeclRefExprClass: { |
| 826 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
| 827 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 828 | return VD->hasGlobalStorage(); |
| 829 | return false; |
| 830 | } |
| 831 | case MemberExprClass: { |
| 832 | const MemberExpr *M = cast<MemberExpr>(this); |
| 833 | return !M->isArrow() && M->getBase()->isOBJCGCCandidate(); |
| 834 | } |
| 835 | case ArraySubscriptExprClass: |
| 836 | return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(); |
| 837 | } |
| 838 | } |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 839 | Expr* Expr::IgnoreParens() { |
| 840 | Expr* E = this; |
| 841 | while (ParenExpr* P = dyn_cast<ParenExpr>(E)) |
| 842 | E = P->getSubExpr(); |
| 843 | |
| 844 | return E; |
| 845 | } |
| 846 | |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 847 | /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr |
| 848 | /// or CastExprs or ImplicitCastExprs, returning their operand. |
| 849 | Expr *Expr::IgnoreParenCasts() { |
| 850 | Expr *E = this; |
| 851 | while (true) { |
| 852 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) |
| 853 | E = P->getSubExpr(); |
| 854 | else if (CastExpr *P = dyn_cast<CastExpr>(E)) |
| 855 | E = P->getSubExpr(); |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 856 | else |
| 857 | return E; |
| 858 | } |
| 859 | } |
| 860 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 861 | /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the |
| 862 | /// value (including ptr->int casts of the same size). Strip off any |
| 863 | /// ParenExpr or CastExprs, returning their operand. |
| 864 | Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) { |
| 865 | Expr *E = this; |
| 866 | while (true) { |
| 867 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
| 868 | E = P->getSubExpr(); |
| 869 | continue; |
| 870 | } |
| 871 | |
| 872 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
| 873 | // We ignore integer <-> casts that are of the same width, ptr<->ptr and |
| 874 | // ptr<->int casts of the same width. We also ignore all identify casts. |
| 875 | Expr *SE = P->getSubExpr(); |
| 876 | |
| 877 | if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) { |
| 878 | E = SE; |
| 879 | continue; |
| 880 | } |
| 881 | |
| 882 | if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) && |
| 883 | (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) && |
| 884 | Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) { |
| 885 | E = SE; |
| 886 | continue; |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | return E; |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 895 | /// hasAnyTypeDependentArguments - Determines if any of the expressions |
| 896 | /// in Exprs is type-dependent. |
| 897 | bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 898 | for (unsigned I = 0; I < NumExprs; ++I) |
| 899 | if (Exprs[I]->isTypeDependent()) |
| 900 | return true; |
| 901 | |
| 902 | return false; |
| 903 | } |
| 904 | |
| 905 | /// hasAnyValueDependentArguments - Determines if any of the expressions |
| 906 | /// in Exprs is value-dependent. |
| 907 | bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 908 | for (unsigned I = 0; I < NumExprs; ++I) |
| 909 | if (Exprs[I]->isValueDependent()) |
| 910 | return true; |
| 911 | |
| 912 | return false; |
| 913 | } |
| 914 | |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 915 | bool Expr::isConstantInitializer(ASTContext &Ctx) const { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 916 | // This function is attempting whether an expression is an initializer |
| 917 | // which can be evaluated at compile-time. isEvaluatable handles most |
| 918 | // of the cases, but it can't deal with some initializer-specific |
| 919 | // expressions, and it can't deal with aggregates; we deal with those here, |
| 920 | // and fall back to isEvaluatable for the other cases. |
| 921 | |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 922 | // FIXME: This function assumes the variable being assigned to |
| 923 | // isn't a reference type! |
| 924 | |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 925 | switch (getStmtClass()) { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 926 | default: break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 927 | case StringLiteralClass: |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 928 | case ObjCEncodeExprClass: |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 929 | return true; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 930 | case CompoundLiteralExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 931 | // This handles gcc's extension that allows global initializers like |
| 932 | // "struct x {int x;} x = (struct x) {};". |
| 933 | // FIXME: This accepts other cases it shouldn't! |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 934 | const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 935 | return Exp->isConstantInitializer(Ctx); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 936 | } |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 937 | case InitListExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 938 | // FIXME: This doesn't deal with fields with reference types correctly. |
| 939 | // FIXME: This incorrectly allows pointers cast to integers to be assigned |
| 940 | // to bitfields. |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 941 | const InitListExpr *Exp = cast<InitListExpr>(this); |
| 942 | unsigned numInits = Exp->getNumInits(); |
| 943 | for (unsigned i = 0; i < numInits; i++) { |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 944 | if (!Exp->getInit(i)->isConstantInitializer(Ctx)) |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 945 | return false; |
| 946 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 947 | return true; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 948 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 949 | case ImplicitValueInitExprClass: |
| 950 | return true; |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 951 | case ParenExprClass: { |
| 952 | return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); |
| 953 | } |
| 954 | case UnaryOperatorClass: { |
| 955 | const UnaryOperator* Exp = cast<UnaryOperator>(this); |
| 956 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 957 | return Exp->getSubExpr()->isConstantInitializer(Ctx); |
| 958 | break; |
| 959 | } |
| 960 | case CStyleCastExprClass: |
| 961 | // Handle casts with a destination that's a struct or union; this |
| 962 | // deals with both the gcc no-op struct cast extension and the |
| 963 | // cast-to-union extension. |
| 964 | if (getType()->isRecordType()) |
| 965 | return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); |
| 966 | break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 969 | return isEvaluatable(Ctx); |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 972 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 973 | /// an integer constant expression. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 974 | |
| 975 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 976 | /// comma, etc |
| 977 | /// |
Chris Lattner | ce0afc0 | 2007-07-18 05:21:20 +0000 | [diff] [blame] | 978 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 979 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 980 | /// cast+dereference. |
Daniel Dunbar | 2d6744f | 2009-02-18 00:47:45 +0000 | [diff] [blame] | 981 | |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 982 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 983 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 984 | // Note that to reduce code duplication, this helper does no evaluation |
| 985 | // itself; the caller checks whether the expression is evaluatable, and |
| 986 | // in the rare cases where CheckICE actually cares about the evaluated |
| 987 | // value, it calls into Evalute. |
| 988 | // |
| 989 | // Meanings of Val: |
| 990 | // 0: This expression is an ICE if it can be evaluated by Evaluate. |
| 991 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 992 | // a legal subexpression for an ICE. This return value is used to handle |
| 993 | // the comma operator in C99 mode. |
| 994 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 995 | |
| 996 | struct ICEDiag { |
| 997 | unsigned Val; |
| 998 | SourceLocation Loc; |
| 999 | |
| 1000 | public: |
| 1001 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 1002 | ICEDiag() : Val(0) {} |
| 1003 | }; |
| 1004 | |
| 1005 | ICEDiag NoDiag() { return ICEDiag(); } |
| 1006 | |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1007 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 1008 | Expr::EvalResult EVResult; |
| 1009 | if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || |
| 1010 | !EVResult.Val.isInt()) { |
| 1011 | return ICEDiag(2, E->getLocStart()); |
| 1012 | } |
| 1013 | return NoDiag(); |
| 1014 | } |
| 1015 | |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1016 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
| 1017 | if (!E->getType()->isIntegralType()) { |
| 1018 | return ICEDiag(2, E->getLocStart()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1019 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1020 | |
| 1021 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1022 | default: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1023 | return ICEDiag(2, E->getLocStart()); |
| 1024 | case Expr::ParenExprClass: |
| 1025 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
| 1026 | case Expr::IntegerLiteralClass: |
| 1027 | case Expr::CharacterLiteralClass: |
| 1028 | case Expr::CXXBoolLiteralExprClass: |
| 1029 | case Expr::CXXZeroInitValueExprClass: |
| 1030 | case Expr::TypesCompatibleExprClass: |
| 1031 | case Expr::UnaryTypeTraitExprClass: |
| 1032 | return NoDiag(); |
| 1033 | case Expr::CallExprClass: |
| 1034 | case Expr::CXXOperatorCallExprClass: { |
| 1035 | const CallExpr *CE = cast<CallExpr>(E); |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1036 | if (CE->isBuiltinCall(Ctx)) |
| 1037 | return CheckEvalInICE(E, Ctx); |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1038 | return ICEDiag(2, E->getLocStart()); |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 1039 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1040 | case Expr::DeclRefExprClass: |
| 1041 | case Expr::QualifiedDeclRefExprClass: |
| 1042 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 1043 | return NoDiag(); |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 1044 | if (Ctx.getLangOptions().CPlusPlus && |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1045 | E->getType().getCVRQualifiers() == QualType::Const) { |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 1046 | // C++ 7.1.5.1p2 |
| 1047 | // A variable of non-volatile const-qualified integral or enumeration |
| 1048 | // type initialized by an ICE can be used in ICEs. |
| 1049 | if (const VarDecl *Dcl = |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1050 | dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) { |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 1051 | if (const Expr *Init = Dcl->getInit()) |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1052 | return CheckICE(Init, Ctx); |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 1053 | } |
| 1054 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1055 | return ICEDiag(2, E->getLocStart()); |
| 1056 | case Expr::UnaryOperatorClass: { |
| 1057 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1058 | switch (Exp->getOpcode()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1059 | default: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1060 | return ICEDiag(2, E->getLocStart()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1061 | case UnaryOperator::Extension: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1062 | case UnaryOperator::LNot: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1063 | case UnaryOperator::Plus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1064 | case UnaryOperator::Minus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1065 | case UnaryOperator::Not: |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1066 | case UnaryOperator::Real: |
| 1067 | case UnaryOperator::Imag: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1068 | return CheckICE(Exp->getSubExpr(), Ctx); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1069 | case UnaryOperator::OffsetOf: |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1070 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 1071 | // Evaluate matches the proposed gcc behavior for cases like |
| 1072 | // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect |
| 1073 | // compliance: we should warn earlier for offsetof expressions with |
| 1074 | // array subscripts that aren't ICEs, and if the array subscripts |
| 1075 | // are ICEs, the value of the offsetof must be an integer constant. |
| 1076 | return CheckEvalInICE(E, Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1077 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1078 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1079 | case Expr::SizeOfAlignOfExprClass: { |
| 1080 | const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E); |
| 1081 | if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType()) |
| 1082 | return ICEDiag(2, E->getLocStart()); |
| 1083 | return NoDiag(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1084 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1085 | case Expr::BinaryOperatorClass: { |
| 1086 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1087 | switch (Exp->getOpcode()) { |
| 1088 | default: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1089 | return ICEDiag(2, E->getLocStart()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1090 | case BinaryOperator::Mul: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1091 | case BinaryOperator::Div: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1092 | case BinaryOperator::Rem: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1093 | case BinaryOperator::Add: |
| 1094 | case BinaryOperator::Sub: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1095 | case BinaryOperator::Shl: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1096 | case BinaryOperator::Shr: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1097 | case BinaryOperator::LT: |
| 1098 | case BinaryOperator::GT: |
| 1099 | case BinaryOperator::LE: |
| 1100 | case BinaryOperator::GE: |
| 1101 | case BinaryOperator::EQ: |
| 1102 | case BinaryOperator::NE: |
| 1103 | case BinaryOperator::And: |
| 1104 | case BinaryOperator::Xor: |
| 1105 | case BinaryOperator::Or: |
| 1106 | case BinaryOperator::Comma: { |
| 1107 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 1108 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1109 | if (Exp->getOpcode() == BinaryOperator::Div || |
| 1110 | Exp->getOpcode() == BinaryOperator::Rem) { |
| 1111 | // Evaluate gives an error for undefined Div/Rem, so make sure |
| 1112 | // we don't evaluate one. |
| 1113 | if (LHSResult.Val != 2 && RHSResult.Val != 2) { |
| 1114 | llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx); |
| 1115 | if (REval == 0) |
| 1116 | return ICEDiag(1, E->getLocStart()); |
| 1117 | if (REval.isSigned() && REval.isAllOnesValue()) { |
| 1118 | llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx); |
| 1119 | if (LEval.isMinSignedValue()) |
| 1120 | return ICEDiag(1, E->getLocStart()); |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | if (Exp->getOpcode() == BinaryOperator::Comma) { |
| 1125 | if (Ctx.getLangOptions().C99) { |
| 1126 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 1127 | // if it isn't evaluated. |
| 1128 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 1129 | return ICEDiag(1, E->getLocStart()); |
| 1130 | } else { |
| 1131 | // In both C89 and C++, commas in ICEs are illegal. |
| 1132 | return ICEDiag(2, E->getLocStart()); |
| 1133 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1134 | } |
| 1135 | if (LHSResult.Val >= RHSResult.Val) |
| 1136 | return LHSResult; |
| 1137 | return RHSResult; |
| 1138 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1139 | case BinaryOperator::LAnd: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1140 | case BinaryOperator::LOr: { |
| 1141 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 1142 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 1143 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 1144 | // Rare case where the RHS has a comma "side-effect"; we need |
| 1145 | // to actually check the condition to see whether the side |
| 1146 | // with the comma is evaluated. |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1147 | if ((Exp->getOpcode() == BinaryOperator::LAnd) != |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1148 | (Exp->getLHS()->EvaluateAsInt(Ctx) == 0)) |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1149 | return RHSResult; |
| 1150 | return NoDiag(); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1151 | } |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1152 | |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1153 | if (LHSResult.Val >= RHSResult.Val) |
| 1154 | return LHSResult; |
| 1155 | return RHSResult; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1156 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1157 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1158 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1159 | case Expr::ImplicitCastExprClass: |
| 1160 | case Expr::CStyleCastExprClass: |
| 1161 | case Expr::CXXFunctionalCastExprClass: { |
| 1162 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 1163 | if (SubExpr->getType()->isIntegralType()) |
| 1164 | return CheckICE(SubExpr, Ctx); |
| 1165 | if (isa<FloatingLiteral>(SubExpr->IgnoreParens())) |
| 1166 | return NoDiag(); |
| 1167 | return ICEDiag(2, E->getLocStart()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1168 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1169 | case Expr::ConditionalOperatorClass: { |
| 1170 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1171 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 1172 | // then only the true side is actually considered in an integer constant |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1173 | // expression, and it is fully evaluated. This is an important GNU |
| 1174 | // extension. See GCC PR38377 for discussion. |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1175 | if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1176 | if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) { |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1177 | Expr::EvalResult EVResult; |
| 1178 | if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || |
| 1179 | !EVResult.Val.isInt()) { |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1180 | return ICEDiag(2, E->getLocStart()); |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1181 | } |
| 1182 | return NoDiag(); |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1183 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1184 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
| 1185 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 1186 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 1187 | if (CondResult.Val == 2) |
| 1188 | return CondResult; |
| 1189 | if (TrueResult.Val == 2) |
| 1190 | return TrueResult; |
| 1191 | if (FalseResult.Val == 2) |
| 1192 | return FalseResult; |
| 1193 | if (CondResult.Val == 1) |
| 1194 | return CondResult; |
| 1195 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 1196 | return NoDiag(); |
| 1197 | // Rare case where the diagnostics depend on which side is evaluated |
| 1198 | // Note that if we get here, CondResult is 0, and at least one of |
| 1199 | // TrueResult and FalseResult is non-zero. |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1200 | if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) { |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1201 | return FalseResult; |
| 1202 | } |
| 1203 | return TrueResult; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1204 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1205 | case Expr::CXXDefaultArgExprClass: |
| 1206 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1207 | case Expr::ChooseExprClass: { |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 1208 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1209 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1211 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1212 | |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1213 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 1214 | SourceLocation *Loc, bool isEvaluated) const { |
| 1215 | ICEDiag d = CheckICE(this, Ctx); |
| 1216 | if (d.Val != 0) { |
| 1217 | if (Loc) *Loc = d.Loc; |
| 1218 | return false; |
| 1219 | } |
| 1220 | EvalResult EvalResult; |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1221 | if (!Evaluate(EvalResult, Ctx)) |
| 1222 | assert(0 && "ICE cannot be evaluated!"); |
| 1223 | assert(!EvalResult.HasSideEffects && "ICE with side effects!"); |
| 1224 | assert(EvalResult.Val.isInt() && "ICE that isn't integer!"); |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1225 | Result = EvalResult.Val.getInt(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1226 | return true; |
| 1227 | } |
| 1228 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1229 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 1230 | /// integer constant expression with the value zero, or if this is one that is |
| 1231 | /// cast to void*. |
Anders Carlsson | efa9b38 | 2008-12-01 02:13:57 +0000 | [diff] [blame] | 1232 | bool Expr::isNullPointerConstant(ASTContext &Ctx) const |
| 1233 | { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1234 | // Strip off a cast to void*, if it exists. Except in C++. |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1235 | if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { |
Sebastian Redl | 6215dee | 2008-11-04 11:45:54 +0000 | [diff] [blame] | 1236 | if (!Ctx.getLangOptions().CPlusPlus) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1237 | // Check that it is a cast to void*. |
| 1238 | if (const PointerType *PT = CE->getType()->getAsPointerType()) { |
| 1239 | QualType Pointee = PT->getPointeeType(); |
| 1240 | if (Pointee.getCVRQualifiers() == 0 && |
| 1241 | Pointee->isVoidType() && // to void* |
| 1242 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1243 | return CE->getSubExpr()->isNullPointerConstant(Ctx); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1244 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1245 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1246 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 1247 | // Ignore the ImplicitCastExpr type entirely. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1248 | return ICE->getSubExpr()->isNullPointerConstant(Ctx); |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1249 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 1250 | // Accept ((void*)0) as a null pointer constant, as many other |
| 1251 | // implementations do. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1252 | return PE->getSubExpr()->isNullPointerConstant(Ctx); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1253 | } else if (const CXXDefaultArgExpr *DefaultArg |
| 1254 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1255 | // See through default argument expressions |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1256 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1257 | } else if (isa<GNUNullExpr>(this)) { |
| 1258 | // The GNU __null extension is always a null pointer constant. |
| 1259 | return true; |
Steve Naroff | aaffbf7 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 1260 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1261 | |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1262 | // This expression must be an integer type. |
| 1263 | if (!getType()->isIntegerType()) |
| 1264 | return false; |
| 1265 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | // If we have an integer constant expression, we need to *evaluate* it and |
| 1267 | // test for the value 0. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1268 | // FIXME: We should probably return false if we're compiling in strict mode |
| 1269 | // and Diag is not null (this indicates that the value was foldable but not |
| 1270 | // an ICE. |
| 1271 | EvalResult Result; |
Anders Carlsson | efa9b38 | 2008-12-01 02:13:57 +0000 | [diff] [blame] | 1272 | return Evaluate(Result, Ctx) && !Result.HasSideEffects && |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1273 | Result.Val.isInt() && Result.Val.getInt() == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1274 | } |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1275 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1276 | /// isBitField - Return true if this expression is a bit-field. |
| 1277 | bool Expr::isBitField() { |
| 1278 | Expr *E = this->IgnoreParenCasts(); |
| 1279 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1280 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) |
| 1281 | return Field->isBitField(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1282 | return false; |
| 1283 | } |
| 1284 | |
Chris Lattner | 2140e90 | 2009-02-16 22:14:05 +0000 | [diff] [blame] | 1285 | /// isArrow - Return true if the base expression is a pointer to vector, |
| 1286 | /// return false if the base expression is a vector. |
| 1287 | bool ExtVectorElementExpr::isArrow() const { |
| 1288 | return getBase()->getType()->isPointerType(); |
| 1289 | } |
| 1290 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1291 | unsigned ExtVectorElementExpr::getNumElements() const { |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1292 | if (const VectorType *VT = getType()->getAsVectorType()) |
| 1293 | return VT->getNumElements(); |
| 1294 | return 1; |
Chris Lattner | 4d0ac88 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1297 | /// containsDuplicateElements - Return true if any element access is repeated. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1298 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1299 | const char *compStr = Accessor.getName(); |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1300 | unsigned length = Accessor.getLength(); |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 1301 | |
| 1302 | // Halving swizzles do not contain duplicate elements. |
| 1303 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
| 1304 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) |
| 1305 | return false; |
| 1306 | |
| 1307 | // Advance past s-char prefix on hex swizzles. |
| 1308 | if (*compStr == 's') { |
| 1309 | compStr++; |
| 1310 | length--; |
| 1311 | } |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1312 | |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1313 | for (unsigned i = 0; i != length-1; i++) { |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1314 | const char *s = compStr+i; |
| 1315 | for (const char c = *s++; *s; s++) |
| 1316 | if (c == *s) |
| 1317 | return true; |
| 1318 | } |
| 1319 | return false; |
| 1320 | } |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1321 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1322 | /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1323 | void ExtVectorElementExpr::getEncodedElementAccess( |
| 1324 | llvm::SmallVectorImpl<unsigned> &Elts) const { |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1325 | const char *compStr = Accessor.getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1326 | if (*compStr == 's') |
| 1327 | compStr++; |
| 1328 | |
| 1329 | bool isHi = !strcmp(compStr, "hi"); |
| 1330 | bool isLo = !strcmp(compStr, "lo"); |
| 1331 | bool isEven = !strcmp(compStr, "even"); |
| 1332 | bool isOdd = !strcmp(compStr, "odd"); |
| 1333 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1334 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 1335 | uint64_t Index; |
| 1336 | |
| 1337 | if (isHi) |
| 1338 | Index = e + i; |
| 1339 | else if (isLo) |
| 1340 | Index = i; |
| 1341 | else if (isEven) |
| 1342 | Index = 2 * i; |
| 1343 | else if (isOdd) |
| 1344 | Index = 2 * i + 1; |
| 1345 | else |
| 1346 | Index = ExtVectorType::getAccessorIdx(compStr[i]); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1347 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1348 | Elts.push_back(Index); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1349 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1352 | // constructor for instance messages. |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1353 | ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1354 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1355 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1356 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1357 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1358 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1359 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1360 | SubExprs = new Stmt*[NumArgs+1]; |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1361 | SubExprs[RECEIVER] = receiver; |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1362 | if (NumArgs) { |
| 1363 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1364 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1365 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1366 | LBracloc = LBrac; |
| 1367 | RBracloc = RBrac; |
| 1368 | } |
| 1369 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1370 | // constructor for class messages. |
| 1371 | // FIXME: clsName should be typed to ObjCInterfaceType |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1372 | ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1373 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1374 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1375 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1376 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1377 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1378 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1379 | SubExprs = new Stmt*[NumArgs+1]; |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1380 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown); |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1381 | if (NumArgs) { |
| 1382 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1383 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1384 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1385 | LBracloc = LBrac; |
| 1386 | RBracloc = RBrac; |
| 1387 | } |
| 1388 | |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1389 | // constructor for class messages. |
| 1390 | ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, |
| 1391 | QualType retType, ObjCMethodDecl *mproto, |
| 1392 | SourceLocation LBrac, SourceLocation RBrac, |
| 1393 | Expr **ArgExprs, unsigned nargs) |
| 1394 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
| 1395 | MethodProto(mproto) { |
| 1396 | NumArgs = nargs; |
| 1397 | SubExprs = new Stmt*[NumArgs+1]; |
| 1398 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown); |
| 1399 | if (NumArgs) { |
| 1400 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1401 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1402 | } |
| 1403 | LBracloc = LBrac; |
| 1404 | RBracloc = RBrac; |
| 1405 | } |
| 1406 | |
| 1407 | ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const { |
| 1408 | uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; |
| 1409 | switch (x & Flags) { |
| 1410 | default: |
| 1411 | assert(false && "Invalid ObjCMessageExpr."); |
| 1412 | case IsInstMeth: |
| 1413 | return ClassInfo(0, 0); |
| 1414 | case IsClsMethDeclUnknown: |
| 1415 | return ClassInfo(0, (IdentifierInfo*) (x & ~Flags)); |
| 1416 | case IsClsMethDeclKnown: { |
| 1417 | ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags); |
| 1418 | return ClassInfo(D, D->getIdentifier()); |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1423 | bool ChooseExpr::isConditionTrue(ASTContext &C) const { |
Daniel Dunbar | 32442bb | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 1424 | return getCond()->getIntegerConstantExprValue(C) != 0; |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1427 | void SizeOfAlignOfExpr::Destroy(ASTContext& C) { |
| 1428 | // Override default behavior of traversing children. If this has a type |
| 1429 | // operand and the type is a variable-length array, the child iteration |
| 1430 | // will iterate over the size expression. However, this expression belongs |
| 1431 | // to the type, not to this, so we don't want to delete it. |
| 1432 | // We still want to delete this expression. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1433 | if (isArgumentType()) { |
| 1434 | this->~SizeOfAlignOfExpr(); |
| 1435 | C.Deallocate(this); |
| 1436 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1437 | else |
| 1438 | Expr::Destroy(C); |
Daniel Dunbar | 9048891 | 2008-08-28 18:02:04 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1441 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1442 | // DesignatedInitExpr |
| 1443 | //===----------------------------------------------------------------------===// |
| 1444 | |
| 1445 | IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { |
| 1446 | assert(Kind == FieldDesignator && "Only valid on a field designator"); |
| 1447 | if (Field.NameOrField & 0x01) |
| 1448 | return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); |
| 1449 | else |
| 1450 | return getField()->getIdentifier(); |
| 1451 | } |
| 1452 | |
| 1453 | DesignatedInitExpr * |
| 1454 | DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, |
| 1455 | unsigned NumDesignators, |
| 1456 | Expr **IndexExprs, unsigned NumIndexExprs, |
| 1457 | SourceLocation ColonOrEqualLoc, |
| 1458 | bool UsesColonSyntax, Expr *Init) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 1459 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
| 1460 | sizeof(Designator) * NumDesignators + |
| 1461 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1462 | DesignatedInitExpr *DIE |
| 1463 | = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, |
| 1464 | ColonOrEqualLoc, UsesColonSyntax, |
| 1465 | NumIndexExprs + 1); |
| 1466 | |
| 1467 | // Fill in the designators |
| 1468 | unsigned ExpectedNumSubExprs = 0; |
| 1469 | designators_iterator Desig = DIE->designators_begin(); |
| 1470 | for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) { |
| 1471 | new (static_cast<void*>(Desig)) Designator(Designators[Idx]); |
| 1472 | if (Designators[Idx].isArrayDesignator()) |
| 1473 | ++ExpectedNumSubExprs; |
| 1474 | else if (Designators[Idx].isArrayRangeDesignator()) |
| 1475 | ExpectedNumSubExprs += 2; |
| 1476 | } |
| 1477 | assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!"); |
| 1478 | |
| 1479 | // Fill in the subexpressions, including the initializer expression. |
| 1480 | child_iterator Child = DIE->child_begin(); |
| 1481 | *Child++ = Init; |
| 1482 | for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child) |
| 1483 | *Child = IndexExprs[Idx]; |
| 1484 | |
| 1485 | return DIE; |
| 1486 | } |
| 1487 | |
| 1488 | SourceRange DesignatedInitExpr::getSourceRange() const { |
| 1489 | SourceLocation StartLoc; |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 1490 | Designator &First = |
| 1491 | *const_cast<DesignatedInitExpr*>(this)->designators_begin(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1492 | if (First.isFieldDesignator()) { |
| 1493 | if (UsesColonSyntax) |
| 1494 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); |
| 1495 | else |
| 1496 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); |
| 1497 | } else |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 1498 | StartLoc = |
| 1499 | SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1500 | return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); |
| 1501 | } |
| 1502 | |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 1503 | DesignatedInitExpr::designators_iterator |
| 1504 | DesignatedInitExpr::designators_begin() { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1505 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1506 | Ptr += sizeof(DesignatedInitExpr); |
| 1507 | return static_cast<Designator*>(static_cast<void*>(Ptr)); |
| 1508 | } |
| 1509 | |
| 1510 | DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() { |
| 1511 | return designators_begin() + NumDesignators; |
| 1512 | } |
| 1513 | |
| 1514 | Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) { |
| 1515 | assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); |
| 1516 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1517 | Ptr += sizeof(DesignatedInitExpr); |
| 1518 | Ptr += sizeof(Designator) * NumDesignators; |
| 1519 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1520 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 1521 | } |
| 1522 | |
| 1523 | Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { |
| 1524 | assert(D.Kind == Designator::ArrayRangeDesignator && |
| 1525 | "Requires array range designator"); |
| 1526 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1527 | Ptr += sizeof(DesignatedInitExpr); |
| 1528 | Ptr += sizeof(Designator) * NumDesignators; |
| 1529 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1530 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 1531 | } |
| 1532 | |
| 1533 | Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { |
| 1534 | assert(D.Kind == Designator::ArrayRangeDesignator && |
| 1535 | "Requires array range designator"); |
| 1536 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1537 | Ptr += sizeof(DesignatedInitExpr); |
| 1538 | Ptr += sizeof(Designator) * NumDesignators; |
| 1539 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1540 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); |
| 1541 | } |
| 1542 | |
| 1543 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 1544 | // ExprIterator. |
| 1545 | //===----------------------------------------------------------------------===// |
| 1546 | |
| 1547 | Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } |
| 1548 | Expr* ExprIterator::operator*() const { return cast<Expr>(*I); } |
| 1549 | Expr* ExprIterator::operator->() const { return cast<Expr>(*I); } |
| 1550 | const Expr* ConstExprIterator::operator[](size_t idx) const { |
| 1551 | return cast<Expr>(I[idx]); |
| 1552 | } |
| 1553 | const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } |
| 1554 | const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } |
| 1555 | |
| 1556 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1557 | // Child Iterators for iterating over subexpressions/substatements |
| 1558 | //===----------------------------------------------------------------------===// |
| 1559 | |
| 1560 | // DeclRefExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1561 | Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } |
| 1562 | Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1563 | |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1564 | // ObjCIvarRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1565 | Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } |
| 1566 | Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1567 | |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1568 | // ObjCPropertyRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1569 | Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } |
| 1570 | Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } |
Steve Naroff | ae78407 | 2008-05-30 00:40:33 +0000 | [diff] [blame] | 1571 | |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 1572 | // ObjCKVCRefExpr |
| 1573 | Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; } |
| 1574 | Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; } |
| 1575 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 1576 | // ObjCSuperExpr |
| 1577 | Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); } |
| 1578 | Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); } |
| 1579 | |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1580 | // PredefinedExpr |
| 1581 | Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } |
| 1582 | Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1583 | |
| 1584 | // IntegerLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1585 | Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } |
| 1586 | Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1587 | |
| 1588 | // CharacterLiteral |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 1589 | Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();} |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1590 | Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1591 | |
| 1592 | // FloatingLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1593 | Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } |
| 1594 | Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1595 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1596 | // ImaginaryLiteral |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1597 | Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } |
| 1598 | Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1599 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1600 | // StringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1601 | Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } |
| 1602 | Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1603 | |
| 1604 | // ParenExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1605 | Stmt::child_iterator ParenExpr::child_begin() { return &Val; } |
| 1606 | Stmt::child_iterator ParenExpr::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1607 | |
| 1608 | // UnaryOperator |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1609 | Stmt::child_iterator UnaryOperator::child_begin() { return &Val; } |
| 1610 | Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1611 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1612 | // SizeOfAlignOfExpr |
| 1613 | Stmt::child_iterator SizeOfAlignOfExpr::child_begin() { |
| 1614 | // If this is of a type and the type is a VLA type (and not a typedef), the |
| 1615 | // size expression of the VLA needs to be treated as an executable expression. |
| 1616 | // Why isn't this weirdness documented better in StmtIterator? |
| 1617 | if (isArgumentType()) { |
| 1618 | if (VariableArrayType* T = dyn_cast<VariableArrayType>( |
| 1619 | getArgumentType().getTypePtr())) |
| 1620 | return child_iterator(T); |
| 1621 | return child_iterator(); |
| 1622 | } |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 1623 | return child_iterator(&Argument.Ex); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1624 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1625 | Stmt::child_iterator SizeOfAlignOfExpr::child_end() { |
| 1626 | if (isArgumentType()) |
| 1627 | return child_iterator(); |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 1628 | return child_iterator(&Argument.Ex + 1); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1629 | } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1630 | |
| 1631 | // ArraySubscriptExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1632 | Stmt::child_iterator ArraySubscriptExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1633 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1634 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1635 | Stmt::child_iterator ArraySubscriptExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1636 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | // CallExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1640 | Stmt::child_iterator CallExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1641 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1642 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1643 | Stmt::child_iterator CallExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1644 | return &SubExprs[0]+NumArgs+ARGS_START; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1645 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1646 | |
| 1647 | // MemberExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1648 | Stmt::child_iterator MemberExpr::child_begin() { return &Base; } |
| 1649 | Stmt::child_iterator MemberExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1650 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1651 | // ExtVectorElementExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1652 | Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } |
| 1653 | Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1654 | |
| 1655 | // CompoundLiteralExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1656 | Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } |
| 1657 | Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1658 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1659 | // CastExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1660 | Stmt::child_iterator CastExpr::child_begin() { return &Op; } |
| 1661 | Stmt::child_iterator CastExpr::child_end() { return &Op+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1662 | |
| 1663 | // BinaryOperator |
| 1664 | Stmt::child_iterator BinaryOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1665 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1666 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1667 | Stmt::child_iterator BinaryOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1668 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | // ConditionalOperator |
| 1672 | Stmt::child_iterator ConditionalOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1673 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1674 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1675 | Stmt::child_iterator ConditionalOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1676 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | // AddrLabelExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1680 | Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } |
| 1681 | Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1682 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1683 | // StmtExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1684 | Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } |
| 1685 | Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1686 | |
| 1687 | // TypesCompatibleExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1688 | Stmt::child_iterator TypesCompatibleExpr::child_begin() { |
| 1689 | return child_iterator(); |
| 1690 | } |
| 1691 | |
| 1692 | Stmt::child_iterator TypesCompatibleExpr::child_end() { |
| 1693 | return child_iterator(); |
| 1694 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1695 | |
| 1696 | // ChooseExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1697 | Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } |
| 1698 | Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1699 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1700 | // GNUNullExpr |
| 1701 | Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); } |
| 1702 | Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); } |
| 1703 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1704 | // ShuffleVectorExpr |
| 1705 | Stmt::child_iterator ShuffleVectorExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1706 | return &SubExprs[0]; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1707 | } |
| 1708 | Stmt::child_iterator ShuffleVectorExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1709 | return &SubExprs[0]+NumExprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1712 | // VAArgExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1713 | Stmt::child_iterator VAArgExpr::child_begin() { return &Val; } |
| 1714 | Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; } |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1715 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1716 | // InitListExpr |
| 1717 | Stmt::child_iterator InitListExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1718 | return InitExprs.size() ? &InitExprs[0] : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1719 | } |
| 1720 | Stmt::child_iterator InitListExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1721 | return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1724 | // DesignatedInitExpr |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1725 | Stmt::child_iterator DesignatedInitExpr::child_begin() { |
| 1726 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1727 | Ptr += sizeof(DesignatedInitExpr); |
| 1728 | Ptr += sizeof(Designator) * NumDesignators; |
| 1729 | return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1730 | } |
| 1731 | Stmt::child_iterator DesignatedInitExpr::child_end() { |
| 1732 | return child_iterator(&*child_begin() + NumSubExprs); |
| 1733 | } |
| 1734 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1735 | // ImplicitValueInitExpr |
| 1736 | Stmt::child_iterator ImplicitValueInitExpr::child_begin() { |
| 1737 | return child_iterator(); |
| 1738 | } |
| 1739 | |
| 1740 | Stmt::child_iterator ImplicitValueInitExpr::child_end() { |
| 1741 | return child_iterator(); |
| 1742 | } |
| 1743 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1744 | // ObjCStringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1745 | Stmt::child_iterator ObjCStringLiteral::child_begin() { |
Chris Lattner | c6c16af | 2009-02-18 06:53:08 +0000 | [diff] [blame] | 1746 | return &String; |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1747 | } |
| 1748 | Stmt::child_iterator ObjCStringLiteral::child_end() { |
Chris Lattner | c6c16af | 2009-02-18 06:53:08 +0000 | [diff] [blame] | 1749 | return &String+1; |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1750 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1751 | |
| 1752 | // ObjCEncodeExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1753 | Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } |
| 1754 | Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1755 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1756 | // ObjCSelectorExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1757 | Stmt::child_iterator ObjCSelectorExpr::child_begin() { |
| 1758 | return child_iterator(); |
| 1759 | } |
| 1760 | Stmt::child_iterator ObjCSelectorExpr::child_end() { |
| 1761 | return child_iterator(); |
| 1762 | } |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1763 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1764 | // ObjCProtocolExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1765 | Stmt::child_iterator ObjCProtocolExpr::child_begin() { |
| 1766 | return child_iterator(); |
| 1767 | } |
| 1768 | Stmt::child_iterator ObjCProtocolExpr::child_end() { |
| 1769 | return child_iterator(); |
| 1770 | } |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1771 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1772 | // ObjCMessageExpr |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1773 | Stmt::child_iterator ObjCMessageExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1774 | return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START; |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1775 | } |
| 1776 | Stmt::child_iterator ObjCMessageExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1777 | return &SubExprs[0]+ARGS_START+getNumArgs(); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1780 | // Blocks |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1781 | Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } |
| 1782 | Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1783 | |
Ted Kremenek | 9da13f9 | 2008-09-26 23:24:14 +0000 | [diff] [blame] | 1784 | Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} |
| 1785 | Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } |