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