Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 1 | //===--- ExprClassification.cpp - Expression AST Node Implementation ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements Expr::classify. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 0010bca | 2010-06-29 00:23:11 +0000 | [diff] [blame] | 14 | #include "llvm/Support/ErrorHandling.h" |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/AST/ExprObjC.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
| 21 | #include "clang/AST/DeclTemplate.h" |
| 22 | using namespace clang; |
| 23 | |
| 24 | typedef Expr::Classification Cl; |
| 25 | |
| 26 | static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E); |
| 27 | static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D); |
| 28 | static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T); |
| 29 | static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E); |
| 30 | static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E); |
| 31 | static Cl::Kinds ClassifyConditional(ASTContext &Ctx, |
| 32 | const ConditionalOperator *E); |
| 33 | static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, |
| 34 | Cl::Kinds Kind, SourceLocation &Loc); |
| 35 | |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 36 | static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang, |
| 37 | const Expr *E, |
| 38 | ExprValueKind Kind) { |
| 39 | switch (Kind) { |
| 40 | case VK_RValue: |
| 41 | return Lang.CPlusPlus && E->getType()->isRecordType() ? |
| 42 | Cl::CL_ClassTemporary : Cl::CL_PRValue; |
| 43 | case VK_LValue: |
| 44 | return Cl::CL_LValue; |
| 45 | case VK_XValue: |
| 46 | return Cl::CL_XValue; |
| 47 | } |
| 48 | llvm_unreachable("Invalid value category of implicit cast."); |
| 49 | return Cl::CL_PRValue; |
| 50 | } |
| 51 | |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 52 | Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const { |
| 53 | assert(!TR->isReferenceType() && "Expressions can't have reference type."); |
| 54 | |
| 55 | Cl::Kinds kind = ClassifyInternal(Ctx, this); |
| 56 | // C99 6.3.2.1: An lvalue is an expression with an object type or an |
| 57 | // incomplete type other than void. |
| 58 | if (!Ctx.getLangOptions().CPlusPlus) { |
| 59 | // Thus, no functions. |
| 60 | if (TR->isFunctionType() || TR == Ctx.OverloadTy) |
| 61 | kind = Cl::CL_Function; |
| 62 | // No void either, but qualified void is OK because it is "other than void". |
| 63 | else if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers()) |
| 64 | kind = Cl::CL_Void; |
| 65 | } |
| 66 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 67 | #if 0 |
| 68 | // Enable this assertion for testing. |
| 69 | switch (kind) { |
| 70 | case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break; |
| 71 | case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break; |
| 72 | case Cl::CL_Function: |
| 73 | case Cl::CL_Void: |
| 74 | case Cl::CL_DuplicateVectorComponents: |
| 75 | case Cl::CL_MemberFunction: |
| 76 | case Cl::CL_SubObjCPropertySetting: |
| 77 | case Cl::CL_ClassTemporary: |
| 78 | case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break; |
| 79 | } |
| 80 | #endif |
| 81 | |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 82 | Cl::ModifiableType modifiable = Cl::CM_Untested; |
| 83 | if (Loc) |
| 84 | modifiable = IsModifiable(Ctx, this, kind, *Loc); |
| 85 | return Classification(kind, modifiable); |
| 86 | } |
| 87 | |
| 88 | static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) { |
| 89 | // This function takes the first stab at classifying expressions. |
| 90 | const LangOptions &Lang = Ctx.getLangOptions(); |
| 91 | |
| 92 | switch (E->getStmtClass()) { |
| 93 | // First come the expressions that are always lvalues, unconditionally. |
Douglas Gregor | 4178b57 | 2010-09-14 21:51:42 +0000 | [diff] [blame] | 94 | case Stmt::NoStmtClass: |
| 95 | #define STMT(Kind, Base) case Expr::Kind##Class: |
| 96 | #define EXPR(Kind, Base) |
| 97 | #include "clang/AST/StmtNodes.inc" |
| 98 | llvm_unreachable("cannot classify a statement"); |
| 99 | break; |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 100 | case Expr::ObjCIsaExprClass: |
| 101 | // C++ [expr.prim.general]p1: A string literal is an lvalue. |
| 102 | case Expr::StringLiteralClass: |
| 103 | // @encode is equivalent to its string |
| 104 | case Expr::ObjCEncodeExprClass: |
| 105 | // __func__ and friends are too. |
| 106 | case Expr::PredefinedExprClass: |
| 107 | // Property references are lvalues |
| 108 | case Expr::ObjCPropertyRefExprClass: |
| 109 | case Expr::ObjCImplicitSetterGetterRefExprClass: |
| 110 | // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of... |
| 111 | case Expr::CXXTypeidExprClass: |
| 112 | // Unresolved lookups get classified as lvalues. |
| 113 | // FIXME: Is this wise? Should they get their own kind? |
| 114 | case Expr::UnresolvedLookupExprClass: |
| 115 | case Expr::UnresolvedMemberExprClass: |
Douglas Gregor | 4178b57 | 2010-09-14 21:51:42 +0000 | [diff] [blame] | 116 | case Expr::CXXDependentScopeMemberExprClass: |
| 117 | case Expr::CXXUnresolvedConstructExprClass: |
| 118 | case Expr::DependentScopeDeclRefExprClass: |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 119 | // ObjC instance variables are lvalues |
| 120 | // FIXME: ObjC++0x might have different rules |
| 121 | case Expr::ObjCIvarRefExprClass: |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 122 | return Cl::CL_LValue; |
Douglas Gregor | 4178b57 | 2010-09-14 21:51:42 +0000 | [diff] [blame] | 123 | // C99 6.5.2.5p5 says that compound literals are lvalues. |
| 124 | // In C++, they're class temporaries. |
| 125 | case Expr::CompoundLiteralExprClass: |
| 126 | return Ctx.getLangOptions().CPlusPlus? Cl::CL_ClassTemporary |
| 127 | : Cl::CL_LValue; |
| 128 | |
| 129 | // Expressions that are prvalues. |
| 130 | case Expr::CXXBoolLiteralExprClass: |
| 131 | case Expr::CXXPseudoDestructorExprClass: |
| 132 | case Expr::SizeOfAlignOfExprClass: |
| 133 | case Expr::CXXNewExprClass: |
| 134 | case Expr::CXXThisExprClass: |
| 135 | case Expr::CXXNullPtrLiteralExprClass: |
| 136 | case Expr::TypesCompatibleExprClass: |
| 137 | case Expr::ImaginaryLiteralClass: |
| 138 | case Expr::GNUNullExprClass: |
| 139 | case Expr::OffsetOfExprClass: |
| 140 | case Expr::CXXThrowExprClass: |
| 141 | case Expr::ShuffleVectorExprClass: |
| 142 | case Expr::IntegerLiteralClass: |
Douglas Gregor | 4178b57 | 2010-09-14 21:51:42 +0000 | [diff] [blame] | 143 | case Expr::CharacterLiteralClass: |
| 144 | case Expr::AddrLabelExprClass: |
| 145 | case Expr::CXXDeleteExprClass: |
| 146 | case Expr::ImplicitValueInitExprClass: |
| 147 | case Expr::BlockExprClass: |
| 148 | case Expr::FloatingLiteralClass: |
| 149 | case Expr::CXXNoexceptExprClass: |
| 150 | case Expr::CXXScalarValueInitExprClass: |
| 151 | case Expr::UnaryTypeTraitExprClass: |
| 152 | case Expr::ObjCSelectorExprClass: |
| 153 | case Expr::ObjCProtocolExprClass: |
| 154 | case Expr::ObjCStringLiteralClass: |
| 155 | case Expr::ParenListExprClass: |
| 156 | case Expr::InitListExprClass: |
| 157 | return Cl::CL_PRValue; |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 158 | |
| 159 | // Next come the complicated cases. |
| 160 | |
| 161 | // C++ [expr.sub]p1: The result is an lvalue of type "T". |
| 162 | // However, subscripting vector types is more like member access. |
| 163 | case Expr::ArraySubscriptExprClass: |
| 164 | if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType()) |
| 165 | return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase()); |
| 166 | return Cl::CL_LValue; |
| 167 | |
| 168 | // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a |
| 169 | // function or variable and a prvalue otherwise. |
| 170 | case Expr::DeclRefExprClass: |
| 171 | return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl()); |
| 172 | // We deal with names referenced from blocks the same way. |
| 173 | case Expr::BlockDeclRefExprClass: |
| 174 | return ClassifyDecl(Ctx, cast<BlockDeclRefExpr>(E)->getDecl()); |
| 175 | |
| 176 | // Member access is complex. |
| 177 | case Expr::MemberExprClass: |
| 178 | return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E)); |
| 179 | |
| 180 | case Expr::UnaryOperatorClass: |
| 181 | switch (cast<UnaryOperator>(E)->getOpcode()) { |
| 182 | // C++ [expr.unary.op]p1: The unary * operator performs indirection: |
| 183 | // [...] the result is an lvalue referring to the object or function |
| 184 | // to which the expression points. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 185 | case UO_Deref: |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 186 | return Cl::CL_LValue; |
| 187 | |
| 188 | // GNU extensions, simply look through them. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 189 | case UO_Extension: |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 190 | return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr()); |
| 191 | |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 192 | // Treat _Real and _Imag basically as if they were member |
| 193 | // expressions: l-value only if the operand is a true l-value. |
| 194 | case UO_Real: |
| 195 | case UO_Imag: { |
| 196 | const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
| 197 | Cl::Kinds K = ClassifyInternal(Ctx, Op); |
| 198 | if (K != Cl::CL_LValue) return K; |
| 199 | |
| 200 | if (isa<ObjCPropertyRefExpr>(Op) || |
| 201 | isa<ObjCImplicitSetterGetterRefExpr>(Op)) |
| 202 | return Cl::CL_SubObjCPropertySetting; |
| 203 | return Cl::CL_LValue; |
| 204 | } |
| 205 | |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 206 | // C++ [expr.pre.incr]p1: The result is the updated operand; it is an |
| 207 | // lvalue, [...] |
| 208 | // Not so in C. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 209 | case UO_PreInc: |
| 210 | case UO_PreDec: |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 211 | return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue; |
| 212 | |
| 213 | default: |
| 214 | return Cl::CL_PRValue; |
| 215 | } |
| 216 | |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 217 | case Expr::OpaqueValueExprClass: |
| 218 | return ClassifyExprValueKind(Lang, E, |
| 219 | cast<OpaqueValueExpr>(E)->getValueKind()); |
| 220 | |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 221 | // Implicit casts are lvalues if they're lvalue casts. Other than that, we |
| 222 | // only specifically record class temporaries. |
| 223 | case Expr::ImplicitCastExprClass: |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 224 | return ClassifyExprValueKind(Lang, E, |
| 225 | cast<ImplicitCastExpr>(E)->getValueKind()); |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 226 | |
| 227 | // C++ [expr.prim.general]p4: The presence of parentheses does not affect |
| 228 | // whether the expression is an lvalue. |
| 229 | case Expr::ParenExprClass: |
| 230 | return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr()); |
| 231 | |
| 232 | case Expr::BinaryOperatorClass: |
| 233 | case Expr::CompoundAssignOperatorClass: |
| 234 | // C doesn't have any binary expressions that are lvalues. |
| 235 | if (Lang.CPlusPlus) |
| 236 | return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E)); |
| 237 | return Cl::CL_PRValue; |
| 238 | |
| 239 | case Expr::CallExprClass: |
| 240 | case Expr::CXXOperatorCallExprClass: |
| 241 | case Expr::CXXMemberCallExprClass: |
| 242 | return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType()); |
| 243 | |
| 244 | // __builtin_choose_expr is equivalent to the chosen expression. |
| 245 | case Expr::ChooseExprClass: |
| 246 | return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx)); |
| 247 | |
| 248 | // Extended vector element access is an lvalue unless there are duplicates |
| 249 | // in the shuffle expression. |
| 250 | case Expr::ExtVectorElementExprClass: |
| 251 | return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ? |
| 252 | Cl::CL_DuplicateVectorComponents : Cl::CL_LValue; |
| 253 | |
| 254 | // Simply look at the actual default argument. |
| 255 | case Expr::CXXDefaultArgExprClass: |
| 256 | return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr()); |
| 257 | |
| 258 | // Same idea for temporary binding. |
| 259 | case Expr::CXXBindTemporaryExprClass: |
| 260 | return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr()); |
| 261 | |
| 262 | // And the temporary lifetime guard. |
| 263 | case Expr::CXXExprWithTemporariesClass: |
| 264 | return ClassifyInternal(Ctx, cast<CXXExprWithTemporaries>(E)->getSubExpr()); |
| 265 | |
| 266 | // Casts depend completely on the target type. All casts work the same. |
| 267 | case Expr::CStyleCastExprClass: |
| 268 | case Expr::CXXFunctionalCastExprClass: |
| 269 | case Expr::CXXStaticCastExprClass: |
| 270 | case Expr::CXXDynamicCastExprClass: |
| 271 | case Expr::CXXReinterpretCastExprClass: |
| 272 | case Expr::CXXConstCastExprClass: |
| 273 | // Only in C++ can casts be interesting at all. |
| 274 | if (!Lang.CPlusPlus) return Cl::CL_PRValue; |
| 275 | return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten()); |
| 276 | |
| 277 | case Expr::ConditionalOperatorClass: |
| 278 | // Once again, only C++ is interesting. |
| 279 | if (!Lang.CPlusPlus) return Cl::CL_PRValue; |
| 280 | return ClassifyConditional(Ctx, cast<ConditionalOperator>(E)); |
| 281 | |
| 282 | // ObjC message sends are effectively function calls, if the target function |
| 283 | // is known. |
| 284 | case Expr::ObjCMessageExprClass: |
| 285 | if (const ObjCMethodDecl *Method = |
| 286 | cast<ObjCMessageExpr>(E)->getMethodDecl()) { |
| 287 | return ClassifyUnnamed(Ctx, Method->getResultType()); |
| 288 | } |
Douglas Gregor | 4178b57 | 2010-09-14 21:51:42 +0000 | [diff] [blame] | 289 | return Cl::CL_PRValue; |
| 290 | |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 291 | // Some C++ expressions are always class temporaries. |
| 292 | case Expr::CXXConstructExprClass: |
| 293 | case Expr::CXXTemporaryObjectExprClass: |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 294 | return Cl::CL_ClassTemporary; |
| 295 | |
Douglas Gregor | 4178b57 | 2010-09-14 21:51:42 +0000 | [diff] [blame] | 296 | case Expr::VAArgExprClass: |
| 297 | return ClassifyUnnamed(Ctx, E->getType()); |
| 298 | |
| 299 | case Expr::DesignatedInitExprClass: |
| 300 | return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit()); |
| 301 | |
| 302 | case Expr::StmtExprClass: { |
| 303 | const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt(); |
| 304 | if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back())) |
Douglas Gregor | 226cbfc | 2010-09-15 01:37:48 +0000 | [diff] [blame] | 305 | return ClassifyUnnamed(Ctx, LastExpr->getType()); |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 306 | return Cl::CL_PRValue; |
| 307 | } |
Douglas Gregor | 4178b57 | 2010-09-14 21:51:42 +0000 | [diff] [blame] | 308 | |
| 309 | case Expr::CXXUuidofExprClass: |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 310 | return Cl::CL_PRValue; |
Douglas Gregor | 4178b57 | 2010-09-14 21:51:42 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | llvm_unreachable("unhandled expression kind in classification"); |
| 314 | return Cl::CL_LValue; |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /// ClassifyDecl - Return the classification of an expression referencing the |
| 318 | /// given declaration. |
| 319 | static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) { |
| 320 | // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a |
| 321 | // function, variable, or data member and a prvalue otherwise. |
| 322 | // In C, functions are not lvalues. |
| 323 | // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an |
| 324 | // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to |
| 325 | // special-case this. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 326 | |
| 327 | if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) |
| 328 | return Cl::CL_MemberFunction; |
| 329 | |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 330 | bool islvalue; |
| 331 | if (const NonTypeTemplateParmDecl *NTTParm = |
| 332 | dyn_cast<NonTypeTemplateParmDecl>(D)) |
| 333 | islvalue = NTTParm->getType()->isReferenceType(); |
| 334 | else |
| 335 | islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) || |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame^] | 336 | isa<IndirectFieldDecl>(D) || |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 337 | (Ctx.getLangOptions().CPlusPlus && |
| 338 | (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D))); |
| 339 | |
| 340 | return islvalue ? Cl::CL_LValue : Cl::CL_PRValue; |
| 341 | } |
| 342 | |
| 343 | /// ClassifyUnnamed - Return the classification of an expression yielding an |
| 344 | /// unnamed value of the given type. This applies in particular to function |
| 345 | /// calls and casts. |
| 346 | static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) { |
| 347 | // In C, function calls are always rvalues. |
| 348 | if (!Ctx.getLangOptions().CPlusPlus) return Cl::CL_PRValue; |
| 349 | |
| 350 | // C++ [expr.call]p10: A function call is an lvalue if the result type is an |
| 351 | // lvalue reference type or an rvalue reference to function type, an xvalue |
| 352 | // if the result type is an rvalue refernence to object type, and a prvalue |
| 353 | // otherwise. |
| 354 | if (T->isLValueReferenceType()) |
| 355 | return Cl::CL_LValue; |
| 356 | const RValueReferenceType *RV = T->getAs<RValueReferenceType>(); |
| 357 | if (!RV) // Could still be a class temporary, though. |
| 358 | return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue; |
| 359 | |
| 360 | return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue; |
| 361 | } |
| 362 | |
| 363 | static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) { |
| 364 | // Handle C first, it's easier. |
| 365 | if (!Ctx.getLangOptions().CPlusPlus) { |
| 366 | // C99 6.5.2.3p3 |
| 367 | // For dot access, the expression is an lvalue if the first part is. For |
| 368 | // arrow access, it always is an lvalue. |
| 369 | if (E->isArrow()) |
| 370 | return Cl::CL_LValue; |
| 371 | // ObjC property accesses are not lvalues, but get special treatment. |
John McCall | b418d74 | 2010-11-16 10:08:07 +0000 | [diff] [blame] | 372 | Expr *Base = E->getBase()->IgnoreParens(); |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 373 | if (isa<ObjCPropertyRefExpr>(Base) || |
| 374 | isa<ObjCImplicitSetterGetterRefExpr>(Base)) |
| 375 | return Cl::CL_SubObjCPropertySetting; |
| 376 | return ClassifyInternal(Ctx, Base); |
| 377 | } |
| 378 | |
| 379 | NamedDecl *Member = E->getMemberDecl(); |
| 380 | // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2. |
| 381 | // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then |
| 382 | // E1.E2 is an lvalue. |
| 383 | if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) |
| 384 | if (Value->getType()->isReferenceType()) |
| 385 | return Cl::CL_LValue; |
| 386 | |
| 387 | // Otherwise, one of the following rules applies. |
| 388 | // -- If E2 is a static member [...] then E1.E2 is an lvalue. |
| 389 | if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord()) |
| 390 | return Cl::CL_LValue; |
| 391 | |
| 392 | // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then |
| 393 | // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue; |
| 394 | // otherwise, it is a prvalue. |
| 395 | if (isa<FieldDecl>(Member)) { |
| 396 | // *E1 is an lvalue |
| 397 | if (E->isArrow()) |
| 398 | return Cl::CL_LValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 399 | Expr *Base = E->getBase()->IgnoreParenImpCasts(); |
| 400 | if (isa<ObjCPropertyRefExpr>(Base) || |
| 401 | isa<ObjCImplicitSetterGetterRefExpr>(Base)) |
| 402 | return Cl::CL_SubObjCPropertySetting; |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 403 | return ClassifyInternal(Ctx, E->getBase()); |
| 404 | } |
| 405 | |
| 406 | // -- If E2 is a [...] member function, [...] |
| 407 | // -- If it refers to a static member function [...], then E1.E2 is an |
| 408 | // lvalue; [...] |
| 409 | // -- Otherwise [...] E1.E2 is a prvalue. |
| 410 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) |
| 411 | return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction; |
| 412 | |
| 413 | // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue. |
| 414 | // So is everything else we haven't handled yet. |
| 415 | return Cl::CL_PRValue; |
| 416 | } |
| 417 | |
| 418 | static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) { |
| 419 | assert(Ctx.getLangOptions().CPlusPlus && |
| 420 | "This is only relevant for C++."); |
| 421 | // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand. |
| 422 | if (E->isAssignmentOp()) |
| 423 | return Cl::CL_LValue; |
| 424 | |
| 425 | // C++ [expr.comma]p1: the result is of the same value category as its right |
| 426 | // operand, [...]. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 427 | if (E->getOpcode() == BO_Comma) |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 428 | return ClassifyInternal(Ctx, E->getRHS()); |
| 429 | |
| 430 | // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand |
| 431 | // is a pointer to a data member is of the same value category as its first |
| 432 | // operand. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 433 | if (E->getOpcode() == BO_PtrMemD) |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 434 | return E->getType()->isFunctionType() ? Cl::CL_MemberFunction : |
| 435 | ClassifyInternal(Ctx, E->getLHS()); |
| 436 | |
| 437 | // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its |
| 438 | // second operand is a pointer to data member and a prvalue otherwise. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 439 | if (E->getOpcode() == BO_PtrMemI) |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 440 | return E->getType()->isFunctionType() ? |
| 441 | Cl::CL_MemberFunction : Cl::CL_LValue; |
| 442 | |
| 443 | // All other binary operations are prvalues. |
| 444 | return Cl::CL_PRValue; |
| 445 | } |
| 446 | |
| 447 | static Cl::Kinds ClassifyConditional(ASTContext &Ctx, |
| 448 | const ConditionalOperator *E) { |
| 449 | assert(Ctx.getLangOptions().CPlusPlus && |
| 450 | "This is only relevant for C++."); |
| 451 | |
| 452 | Expr *True = E->getTrueExpr(); |
| 453 | Expr *False = E->getFalseExpr(); |
| 454 | // C++ [expr.cond]p2 |
| 455 | // If either the second or the third operand has type (cv) void, [...] |
| 456 | // the result [...] is a prvalue. |
| 457 | if (True->getType()->isVoidType() || False->getType()->isVoidType()) |
| 458 | return Cl::CL_PRValue; |
| 459 | |
| 460 | // Note that at this point, we have already performed all conversions |
| 461 | // according to [expr.cond]p3. |
| 462 | // C++ [expr.cond]p4: If the second and third operands are glvalues of the |
| 463 | // same value category [...], the result is of that [...] value category. |
| 464 | // C++ [expr.cond]p5: Otherwise, the result is a prvalue. |
| 465 | Cl::Kinds LCl = ClassifyInternal(Ctx, True), |
| 466 | RCl = ClassifyInternal(Ctx, False); |
| 467 | return LCl == RCl ? LCl : Cl::CL_PRValue; |
| 468 | } |
| 469 | |
| 470 | static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, |
| 471 | Cl::Kinds Kind, SourceLocation &Loc) { |
| 472 | // As a general rule, we only care about lvalues. But there are some rvalues |
| 473 | // for which we want to generate special results. |
| 474 | if (Kind == Cl::CL_PRValue) { |
| 475 | // For the sake of better diagnostics, we want to specifically recognize |
| 476 | // use of the GCC cast-as-lvalue extension. |
| 477 | if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(E->IgnoreParens())){ |
| 478 | if (CE->getSubExpr()->Classify(Ctx).isLValue()) { |
| 479 | Loc = CE->getLParenLoc(); |
| 480 | return Cl::CM_LValueCast; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | if (Kind != Cl::CL_LValue) |
| 485 | return Cl::CM_RValue; |
| 486 | |
| 487 | // This is the lvalue case. |
| 488 | // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6) |
| 489 | if (Ctx.getLangOptions().CPlusPlus && E->getType()->isFunctionType()) |
| 490 | return Cl::CM_Function; |
| 491 | |
| 492 | // You cannot assign to a variable outside a block from within the block if |
| 493 | // it is not marked __block, e.g. |
| 494 | // void takeclosure(void (^C)(void)); |
| 495 | // void func() { int x = 1; takeclosure(^{ x = 7; }); } |
| 496 | if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(E)) { |
| 497 | if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl())) |
| 498 | return Cl::CM_NotBlockQualified; |
| 499 | } |
| 500 | |
| 501 | // Assignment to a property in ObjC is an implicit setter access. But a |
| 502 | // setter might not exist. |
| 503 | if (const ObjCImplicitSetterGetterRefExpr *Expr = |
| 504 | dyn_cast<ObjCImplicitSetterGetterRefExpr>(E)) { |
| 505 | if (Expr->getSetterMethod() == 0) |
| 506 | return Cl::CM_NoSetterProperty; |
| 507 | } |
| 508 | |
| 509 | CanQualType CT = Ctx.getCanonicalType(E->getType()); |
| 510 | // Const stuff is obviously not modifiable. |
| 511 | if (CT.isConstQualified()) |
| 512 | return Cl::CM_ConstQualified; |
| 513 | // Arrays are not modifiable, only their elements are. |
| 514 | if (CT->isArrayType()) |
| 515 | return Cl::CM_ArrayType; |
| 516 | // Incomplete types are not modifiable. |
| 517 | if (CT->isIncompleteType()) |
| 518 | return Cl::CM_IncompleteType; |
| 519 | |
| 520 | // Records with any const fields (recursively) are not modifiable. |
| 521 | if (const RecordType *R = CT->getAs<RecordType>()) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 522 | assert((isa<ObjCImplicitSetterGetterRefExpr>(E) || |
| 523 | isa<ObjCPropertyRefExpr>(E) || |
Fariborz Jahanian | 4088ec0 | 2010-09-09 23:01:10 +0000 | [diff] [blame] | 524 | !Ctx.getLangOptions().CPlusPlus) && |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 525 | "C++ struct assignment should be resolved by the " |
| 526 | "copy assignment operator."); |
| 527 | if (R->hasConstFields()) |
| 528 | return Cl::CM_ConstQualified; |
| 529 | } |
| 530 | |
| 531 | return Cl::CM_Modifiable; |
| 532 | } |
| 533 | |
| 534 | Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { |
| 535 | Classification VC = Classify(Ctx); |
| 536 | switch (VC.getKind()) { |
| 537 | case Cl::CL_LValue: return LV_Valid; |
| 538 | case Cl::CL_XValue: return LV_InvalidExpression; |
| 539 | case Cl::CL_Function: return LV_NotObjectType; |
| 540 | case Cl::CL_Void: return LV_IncompleteVoidType; |
| 541 | case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents; |
| 542 | case Cl::CL_MemberFunction: return LV_MemberFunction; |
| 543 | case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting; |
| 544 | case Cl::CL_ClassTemporary: return LV_ClassTemporary; |
| 545 | case Cl::CL_PRValue: return LV_InvalidExpression; |
| 546 | } |
Chandler Carruth | 0010bca | 2010-06-29 00:23:11 +0000 | [diff] [blame] | 547 | llvm_unreachable("Unhandled kind"); |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | Expr::isModifiableLvalueResult |
| 551 | Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const { |
| 552 | SourceLocation dummy; |
| 553 | Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy); |
| 554 | switch (VC.getKind()) { |
| 555 | case Cl::CL_LValue: break; |
| 556 | case Cl::CL_XValue: return MLV_InvalidExpression; |
| 557 | case Cl::CL_Function: return MLV_NotObjectType; |
| 558 | case Cl::CL_Void: return MLV_IncompleteVoidType; |
| 559 | case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; |
| 560 | case Cl::CL_MemberFunction: return MLV_MemberFunction; |
| 561 | case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting; |
| 562 | case Cl::CL_ClassTemporary: return MLV_ClassTemporary; |
| 563 | case Cl::CL_PRValue: |
| 564 | return VC.getModifiable() == Cl::CM_LValueCast ? |
| 565 | MLV_LValueCast : MLV_InvalidExpression; |
| 566 | } |
| 567 | assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind"); |
| 568 | switch (VC.getModifiable()) { |
Chandler Carruth | 0010bca | 2010-06-29 00:23:11 +0000 | [diff] [blame] | 569 | case Cl::CM_Untested: llvm_unreachable("Did not test modifiability"); |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 570 | case Cl::CM_Modifiable: return MLV_Valid; |
Chandler Carruth | 0010bca | 2010-06-29 00:23:11 +0000 | [diff] [blame] | 571 | case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match"); |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 572 | case Cl::CM_Function: return MLV_NotObjectType; |
| 573 | case Cl::CM_LValueCast: |
Chandler Carruth | 0010bca | 2010-06-29 00:23:11 +0000 | [diff] [blame] | 574 | llvm_unreachable("CM_LValueCast and CL_LValue don't match"); |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 575 | case Cl::CM_NotBlockQualified: return MLV_NotBlockQualified; |
| 576 | case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty; |
| 577 | case Cl::CM_ConstQualified: return MLV_ConstQualified; |
| 578 | case Cl::CM_ArrayType: return MLV_ArrayType; |
| 579 | case Cl::CM_IncompleteType: return MLV_IncompleteType; |
| 580 | } |
Chandler Carruth | 0010bca | 2010-06-29 00:23:11 +0000 | [diff] [blame] | 581 | llvm_unreachable("Unhandled modifiable type"); |
Sebastian Redl | 2111c85 | 2010-06-28 15:09:07 +0000 | [diff] [blame] | 582 | } |