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