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