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