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