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