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