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