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