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