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