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