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