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