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