clang API Documentation

ExprClassification.cpp
Go to the documentation of this file.
00001 //===--- ExprClassification.cpp - Expression AST Node Implementation ------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements Expr::classify.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Support/ErrorHandling.h"
00015 #include "clang/AST/Expr.h"
00016 #include "clang/AST/ExprCXX.h"
00017 #include "clang/AST/ExprObjC.h"
00018 #include "clang/AST/ASTContext.h"
00019 #include "clang/AST/DeclObjC.h"
00020 #include "clang/AST/DeclCXX.h"
00021 #include "clang/AST/DeclTemplate.h"
00022 using namespace clang;
00023 
00024 typedef Expr::Classification Cl;
00025 
00026 static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E);
00027 static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D);
00028 static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T);
00029 static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E);
00030 static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E);
00031 static Cl::Kinds ClassifyConditional(ASTContext &Ctx,
00032                                      const Expr *trueExpr,
00033                                      const Expr *falseExpr);
00034 static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
00035                                        Cl::Kinds Kind, SourceLocation &Loc);
00036 
00037 static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang,
00038                                        const Expr *E,
00039                                        ExprValueKind Kind) {
00040   switch (Kind) {
00041   case VK_RValue:
00042     return Lang.CPlusPlus && E->getType()->isRecordType() ?
00043       Cl::CL_ClassTemporary : Cl::CL_PRValue;
00044   case VK_LValue:
00045     return Cl::CL_LValue;
00046   case VK_XValue:
00047     return Cl::CL_XValue;
00048   }
00049   llvm_unreachable("Invalid value category of implicit cast.");
00050 }
00051 
00052 Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {
00053   assert(!TR->isReferenceType() && "Expressions can't have reference type.");
00054 
00055   Cl::Kinds kind = ClassifyInternal(Ctx, this);
00056   // C99 6.3.2.1: An lvalue is an expression with an object type or an
00057   //   incomplete type other than void.
00058   if (!Ctx.getLangOpts().CPlusPlus) {
00059     // Thus, no functions.
00060     if (TR->isFunctionType() || TR == Ctx.OverloadTy)
00061       kind = Cl::CL_Function;
00062     // No void either, but qualified void is OK because it is "other than void".
00063     // Void "lvalues" are classified as addressable void values, which are void
00064     // expressions whose address can be taken.
00065     else if (TR->isVoidType() && !TR.hasQualifiers())
00066       kind = (kind == Cl::CL_LValue ? Cl::CL_AddressableVoid : Cl::CL_Void);
00067   }
00068 
00069   // Enable this assertion for testing.
00070   switch (kind) {
00071   case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break;
00072   case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break;
00073   case Cl::CL_Function:
00074   case Cl::CL_Void:
00075   case Cl::CL_AddressableVoid:
00076   case Cl::CL_DuplicateVectorComponents:
00077   case Cl::CL_MemberFunction:
00078   case Cl::CL_SubObjCPropertySetting:
00079   case Cl::CL_ClassTemporary:
00080   case Cl::CL_ObjCMessageRValue:
00081   case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
00082   }
00083 
00084   Cl::ModifiableType modifiable = Cl::CM_Untested;
00085   if (Loc)
00086     modifiable = IsModifiable(Ctx, this, kind, *Loc);
00087   return Classification(kind, modifiable);
00088 }
00089 
00090 static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
00091   // This function takes the first stab at classifying expressions.
00092   const LangOptions &Lang = Ctx.getLangOpts();
00093 
00094   switch (E->getStmtClass()) {
00095   case Stmt::NoStmtClass:
00096 #define ABSTRACT_STMT(Kind)
00097 #define STMT(Kind, Base) case Expr::Kind##Class:
00098 #define EXPR(Kind, Base)
00099 #include "clang/AST/StmtNodes.inc"
00100     llvm_unreachable("cannot classify a statement");
00101 
00102     // First come the expressions that are always lvalues, unconditionally.
00103   case Expr::ObjCIsaExprClass:
00104     // C++ [expr.prim.general]p1: A string literal is an lvalue.
00105   case Expr::StringLiteralClass:
00106     // @encode is equivalent to its string
00107   case Expr::ObjCEncodeExprClass:
00108     // __func__ and friends are too.
00109   case Expr::PredefinedExprClass:
00110     // Property references are lvalues
00111   case Expr::ObjCSubscriptRefExprClass:
00112   case Expr::ObjCPropertyRefExprClass:
00113     // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
00114   case Expr::CXXTypeidExprClass:
00115     // Unresolved lookups get classified as lvalues.
00116     // FIXME: Is this wise? Should they get their own kind?
00117   case Expr::UnresolvedLookupExprClass:
00118   case Expr::UnresolvedMemberExprClass:
00119   case Expr::CXXDependentScopeMemberExprClass:
00120   case Expr::DependentScopeDeclRefExprClass:
00121     // ObjC instance variables are lvalues
00122     // FIXME: ObjC++0x might have different rules
00123   case Expr::ObjCIvarRefExprClass:
00124     return Cl::CL_LValue;
00125 
00126     // C99 6.5.2.5p5 says that compound literals are lvalues.
00127     // In C++, they're class temporaries.
00128   case Expr::CompoundLiteralExprClass:
00129     return Ctx.getLangOpts().CPlusPlus? Cl::CL_ClassTemporary 
00130                                          : Cl::CL_LValue;
00131 
00132     // Expressions that are prvalues.
00133   case Expr::CXXBoolLiteralExprClass:
00134   case Expr::CXXPseudoDestructorExprClass:
00135   case Expr::UnaryExprOrTypeTraitExprClass:
00136   case Expr::CXXNewExprClass:
00137   case Expr::CXXThisExprClass:
00138   case Expr::CXXNullPtrLiteralExprClass:
00139   case Expr::ImaginaryLiteralClass:
00140   case Expr::GNUNullExprClass:
00141   case Expr::OffsetOfExprClass:
00142   case Expr::CXXThrowExprClass:
00143   case Expr::ShuffleVectorExprClass:
00144   case Expr::IntegerLiteralClass:
00145   case Expr::CharacterLiteralClass:
00146   case Expr::AddrLabelExprClass:
00147   case Expr::CXXDeleteExprClass:
00148   case Expr::ImplicitValueInitExprClass:
00149   case Expr::BlockExprClass:
00150   case Expr::FloatingLiteralClass:
00151   case Expr::CXXNoexceptExprClass:
00152   case Expr::CXXScalarValueInitExprClass:
00153   case Expr::UnaryTypeTraitExprClass:
00154   case Expr::BinaryTypeTraitExprClass:
00155   case Expr::TypeTraitExprClass:
00156   case Expr::ArrayTypeTraitExprClass:
00157   case Expr::ExpressionTraitExprClass:
00158   case Expr::ObjCSelectorExprClass:
00159   case Expr::ObjCProtocolExprClass:
00160   case Expr::ObjCStringLiteralClass:
00161   case Expr::ObjCBoxedExprClass:
00162   case Expr::ObjCArrayLiteralClass:
00163   case Expr::ObjCDictionaryLiteralClass:
00164   case Expr::ObjCBoolLiteralExprClass:
00165   case Expr::ParenListExprClass:
00166   case Expr::SizeOfPackExprClass:
00167   case Expr::SubstNonTypeTemplateParmPackExprClass:
00168   case Expr::AsTypeExprClass:
00169   case Expr::ObjCIndirectCopyRestoreExprClass:
00170   case Expr::AtomicExprClass:
00171     return Cl::CL_PRValue;
00172 
00173     // Next come the complicated cases.
00174   case Expr::SubstNonTypeTemplateParmExprClass:
00175     return ClassifyInternal(Ctx,
00176                  cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
00177 
00178     // C++ [expr.sub]p1: The result is an lvalue of type "T".
00179     // However, subscripting vector types is more like member access.
00180   case Expr::ArraySubscriptExprClass:
00181     if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
00182       return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
00183     return Cl::CL_LValue;
00184 
00185     // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
00186     //   function or variable and a prvalue otherwise.
00187   case Expr::DeclRefExprClass:
00188     if (E->getType() == Ctx.UnknownAnyTy)
00189       return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
00190                ? Cl::CL_PRValue : Cl::CL_LValue;
00191     return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
00192 
00193     // Member access is complex.
00194   case Expr::MemberExprClass:
00195     return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
00196 
00197   case Expr::UnaryOperatorClass:
00198     switch (cast<UnaryOperator>(E)->getOpcode()) {
00199       // C++ [expr.unary.op]p1: The unary * operator performs indirection:
00200       //   [...] the result is an lvalue referring to the object or function
00201       //   to which the expression points.
00202     case UO_Deref:
00203       return Cl::CL_LValue;
00204 
00205       // GNU extensions, simply look through them.
00206     case UO_Extension:
00207       return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
00208 
00209     // Treat _Real and _Imag basically as if they were member
00210     // expressions:  l-value only if the operand is a true l-value.
00211     case UO_Real:
00212     case UO_Imag: {
00213       const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
00214       Cl::Kinds K = ClassifyInternal(Ctx, Op);
00215       if (K != Cl::CL_LValue) return K;
00216 
00217       if (isa<ObjCPropertyRefExpr>(Op))
00218         return Cl::CL_SubObjCPropertySetting;
00219       return Cl::CL_LValue;
00220     }
00221 
00222       // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
00223       //   lvalue, [...]
00224       // Not so in C.
00225     case UO_PreInc:
00226     case UO_PreDec:
00227       return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
00228 
00229     default:
00230       return Cl::CL_PRValue;
00231     }
00232 
00233   case Expr::OpaqueValueExprClass:
00234     return ClassifyExprValueKind(Lang, E, E->getValueKind());
00235 
00236     // Pseudo-object expressions can produce l-values with reference magic.
00237   case Expr::PseudoObjectExprClass:
00238     return ClassifyExprValueKind(Lang, E,
00239                                  cast<PseudoObjectExpr>(E)->getValueKind());
00240 
00241     // Implicit casts are lvalues if they're lvalue casts. Other than that, we
00242     // only specifically record class temporaries.
00243   case Expr::ImplicitCastExprClass:
00244     return ClassifyExprValueKind(Lang, E, E->getValueKind());
00245 
00246     // C++ [expr.prim.general]p4: The presence of parentheses does not affect
00247     //   whether the expression is an lvalue.
00248   case Expr::ParenExprClass:
00249     return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
00250 
00251     // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
00252     // or a void expression if its result expression is, respectively, an
00253     // lvalue, a function designator, or a void expression.
00254   case Expr::GenericSelectionExprClass:
00255     if (cast<GenericSelectionExpr>(E)->isResultDependent())
00256       return Cl::CL_PRValue;
00257     return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
00258 
00259   case Expr::BinaryOperatorClass:
00260   case Expr::CompoundAssignOperatorClass:
00261     // C doesn't have any binary expressions that are lvalues.
00262     if (Lang.CPlusPlus)
00263       return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
00264     return Cl::CL_PRValue;
00265 
00266   case Expr::CallExprClass:
00267   case Expr::CXXOperatorCallExprClass:
00268   case Expr::CXXMemberCallExprClass:
00269   case Expr::UserDefinedLiteralClass:
00270   case Expr::CUDAKernelCallExprClass:
00271     return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType());
00272 
00273     // __builtin_choose_expr is equivalent to the chosen expression.
00274   case Expr::ChooseExprClass:
00275     return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr(Ctx));
00276 
00277     // Extended vector element access is an lvalue unless there are duplicates
00278     // in the shuffle expression.
00279   case Expr::ExtVectorElementExprClass:
00280     return cast<ExtVectorElementExpr>(E)->containsDuplicateElements() ?
00281       Cl::CL_DuplicateVectorComponents : Cl::CL_LValue;
00282 
00283     // Simply look at the actual default argument.
00284   case Expr::CXXDefaultArgExprClass:
00285     return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
00286 
00287     // Same idea for temporary binding.
00288   case Expr::CXXBindTemporaryExprClass:
00289     return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
00290 
00291     // And the cleanups guard.
00292   case Expr::ExprWithCleanupsClass:
00293     return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
00294 
00295     // Casts depend completely on the target type. All casts work the same.
00296   case Expr::CStyleCastExprClass:
00297   case Expr::CXXFunctionalCastExprClass:
00298   case Expr::CXXStaticCastExprClass:
00299   case Expr::CXXDynamicCastExprClass:
00300   case Expr::CXXReinterpretCastExprClass:
00301   case Expr::CXXConstCastExprClass:
00302   case Expr::ObjCBridgedCastExprClass:
00303     // Only in C++ can casts be interesting at all.
00304     if (!Lang.CPlusPlus) return Cl::CL_PRValue;
00305     return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
00306 
00307   case Expr::CXXUnresolvedConstructExprClass:
00308     return ClassifyUnnamed(Ctx, 
00309                       cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
00310       
00311   case Expr::BinaryConditionalOperatorClass: {
00312     if (!Lang.CPlusPlus) return Cl::CL_PRValue;
00313     const BinaryConditionalOperator *co = cast<BinaryConditionalOperator>(E);
00314     return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
00315   }
00316 
00317   case Expr::ConditionalOperatorClass: {
00318     // Once again, only C++ is interesting.
00319     if (!Lang.CPlusPlus) return Cl::CL_PRValue;
00320     const ConditionalOperator *co = cast<ConditionalOperator>(E);
00321     return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
00322   }
00323 
00324     // ObjC message sends are effectively function calls, if the target function
00325     // is known.
00326   case Expr::ObjCMessageExprClass:
00327     if (const ObjCMethodDecl *Method =
00328           cast<ObjCMessageExpr>(E)->getMethodDecl()) {
00329       Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getResultType());
00330       return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
00331     }
00332     return Cl::CL_PRValue;
00333       
00334     // Some C++ expressions are always class temporaries.
00335   case Expr::CXXConstructExprClass:
00336   case Expr::CXXTemporaryObjectExprClass:
00337   case Expr::LambdaExprClass:
00338     return Cl::CL_ClassTemporary;
00339 
00340   case Expr::VAArgExprClass:
00341     return ClassifyUnnamed(Ctx, E->getType());
00342 
00343   case Expr::DesignatedInitExprClass:
00344     return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
00345 
00346   case Expr::StmtExprClass: {
00347     const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
00348     if (const Expr *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
00349       return ClassifyUnnamed(Ctx, LastExpr->getType());
00350     return Cl::CL_PRValue;
00351   }
00352 
00353   case Expr::CXXUuidofExprClass:
00354     return Cl::CL_LValue;
00355 
00356   case Expr::PackExpansionExprClass:
00357     return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
00358 
00359   case Expr::MaterializeTemporaryExprClass:
00360     return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
00361               ? Cl::CL_LValue 
00362               : Cl::CL_XValue;
00363 
00364   case Expr::InitListExprClass:
00365     // An init list can be an lvalue if it is bound to a reference and
00366     // contains only one element. In that case, we look at that element
00367     // for an exact classification. Init list creation takes care of the
00368     // value kind for us, so we only need to fine-tune.
00369     if (E->isRValue())
00370       return ClassifyExprValueKind(Lang, E, E->getValueKind());
00371     assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
00372            "Only 1-element init lists can be glvalues.");
00373     return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
00374   }
00375 
00376   llvm_unreachable("unhandled expression kind in classification");
00377 }
00378 
00379 /// ClassifyDecl - Return the classification of an expression referencing the
00380 /// given declaration.
00381 static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
00382   // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
00383   //   function, variable, or data member and a prvalue otherwise.
00384   // In C, functions are not lvalues.
00385   // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
00386   // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
00387   // special-case this.
00388 
00389   if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
00390     return Cl::CL_MemberFunction;
00391 
00392   bool islvalue;
00393   if (const NonTypeTemplateParmDecl *NTTParm =
00394         dyn_cast<NonTypeTemplateParmDecl>(D))
00395     islvalue = NTTParm->getType()->isReferenceType();
00396   else
00397     islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
00398     isa<IndirectFieldDecl>(D) ||
00399       (Ctx.getLangOpts().CPlusPlus &&
00400         (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)));
00401 
00402   return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
00403 }
00404 
00405 /// ClassifyUnnamed - Return the classification of an expression yielding an
00406 /// unnamed value of the given type. This applies in particular to function
00407 /// calls and casts.
00408 static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
00409   // In C, function calls are always rvalues.
00410   if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue;
00411 
00412   // C++ [expr.call]p10: A function call is an lvalue if the result type is an
00413   //   lvalue reference type or an rvalue reference to function type, an xvalue
00414   //   if the result type is an rvalue reference to object type, and a prvalue
00415   //   otherwise.
00416   if (T->isLValueReferenceType())
00417     return Cl::CL_LValue;
00418   const RValueReferenceType *RV = T->getAs<RValueReferenceType>();
00419   if (!RV) // Could still be a class temporary, though.
00420     return T->isRecordType() ? Cl::CL_ClassTemporary : Cl::CL_PRValue;
00421 
00422   return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
00423 }
00424 
00425 static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
00426   if (E->getType() == Ctx.UnknownAnyTy)
00427     return (isa<FunctionDecl>(E->getMemberDecl())
00428               ? Cl::CL_PRValue : Cl::CL_LValue);
00429 
00430   // Handle C first, it's easier.
00431   if (!Ctx.getLangOpts().CPlusPlus) {
00432     // C99 6.5.2.3p3
00433     // For dot access, the expression is an lvalue if the first part is. For
00434     // arrow access, it always is an lvalue.
00435     if (E->isArrow())
00436       return Cl::CL_LValue;
00437     // ObjC property accesses are not lvalues, but get special treatment.
00438     Expr *Base = E->getBase()->IgnoreParens();
00439     if (isa<ObjCPropertyRefExpr>(Base))
00440       return Cl::CL_SubObjCPropertySetting;
00441     return ClassifyInternal(Ctx, Base);
00442   }
00443 
00444   NamedDecl *Member = E->getMemberDecl();
00445   // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
00446   // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
00447   //   E1.E2 is an lvalue.
00448   if (ValueDecl *Value = dyn_cast<ValueDecl>(Member))
00449     if (Value->getType()->isReferenceType())
00450       return Cl::CL_LValue;
00451 
00452   //   Otherwise, one of the following rules applies.
00453   //   -- If E2 is a static member [...] then E1.E2 is an lvalue.
00454   if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
00455     return Cl::CL_LValue;
00456 
00457   //   -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
00458   //      E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
00459   //      otherwise, it is a prvalue.
00460   if (isa<FieldDecl>(Member)) {
00461     // *E1 is an lvalue
00462     if (E->isArrow())
00463       return Cl::CL_LValue;
00464     Expr *Base = E->getBase()->IgnoreParenImpCasts();
00465     if (isa<ObjCPropertyRefExpr>(Base))
00466       return Cl::CL_SubObjCPropertySetting;
00467     return ClassifyInternal(Ctx, E->getBase());
00468   }
00469 
00470   //   -- If E2 is a [...] member function, [...]
00471   //      -- If it refers to a static member function [...], then E1.E2 is an
00472   //         lvalue; [...]
00473   //      -- Otherwise [...] E1.E2 is a prvalue.
00474   if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member))
00475     return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
00476 
00477   //   -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
00478   // So is everything else we haven't handled yet.
00479   return Cl::CL_PRValue;
00480 }
00481 
00482 static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
00483   assert(Ctx.getLangOpts().CPlusPlus &&
00484          "This is only relevant for C++.");
00485   // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
00486   // Except we override this for writes to ObjC properties.
00487   if (E->isAssignmentOp())
00488     return (E->getLHS()->getObjectKind() == OK_ObjCProperty
00489               ? Cl::CL_PRValue : Cl::CL_LValue);
00490 
00491   // C++ [expr.comma]p1: the result is of the same value category as its right
00492   //   operand, [...].
00493   if (E->getOpcode() == BO_Comma)
00494     return ClassifyInternal(Ctx, E->getRHS());
00495 
00496   // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
00497   //   is a pointer to a data member is of the same value category as its first
00498   //   operand.
00499   if (E->getOpcode() == BO_PtrMemD)
00500     return (E->getType()->isFunctionType() ||
00501             E->hasPlaceholderType(BuiltinType::BoundMember))
00502              ? Cl::CL_MemberFunction 
00503              : ClassifyInternal(Ctx, E->getLHS());
00504 
00505   // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
00506   //   second operand is a pointer to data member and a prvalue otherwise.
00507   if (E->getOpcode() == BO_PtrMemI)
00508     return (E->getType()->isFunctionType() ||
00509             E->hasPlaceholderType(BuiltinType::BoundMember))
00510              ? Cl::CL_MemberFunction 
00511              : Cl::CL_LValue;
00512 
00513   // All other binary operations are prvalues.
00514   return Cl::CL_PRValue;
00515 }
00516 
00517 static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
00518                                      const Expr *False) {
00519   assert(Ctx.getLangOpts().CPlusPlus &&
00520          "This is only relevant for C++.");
00521 
00522   // C++ [expr.cond]p2
00523   //   If either the second or the third operand has type (cv) void, [...]
00524   //   the result [...] is a prvalue.
00525   if (True->getType()->isVoidType() || False->getType()->isVoidType())
00526     return Cl::CL_PRValue;
00527 
00528   // Note that at this point, we have already performed all conversions
00529   // according to [expr.cond]p3.
00530   // C++ [expr.cond]p4: If the second and third operands are glvalues of the
00531   //   same value category [...], the result is of that [...] value category.
00532   // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
00533   Cl::Kinds LCl = ClassifyInternal(Ctx, True),
00534             RCl = ClassifyInternal(Ctx, False);
00535   return LCl == RCl ? LCl : Cl::CL_PRValue;
00536 }
00537 
00538 static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
00539                                        Cl::Kinds Kind, SourceLocation &Loc) {
00540   // As a general rule, we only care about lvalues. But there are some rvalues
00541   // for which we want to generate special results.
00542   if (Kind == Cl::CL_PRValue) {
00543     // For the sake of better diagnostics, we want to specifically recognize
00544     // use of the GCC cast-as-lvalue extension.
00545     if (const ExplicitCastExpr *CE =
00546           dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
00547       if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
00548         Loc = CE->getExprLoc();
00549         return Cl::CM_LValueCast;
00550       }
00551     }
00552   }
00553   if (Kind != Cl::CL_LValue)
00554     return Cl::CM_RValue;
00555 
00556   // This is the lvalue case.
00557   // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
00558   if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType())
00559     return Cl::CM_Function;
00560 
00561   // Assignment to a property in ObjC is an implicit setter access. But a
00562   // setter might not exist.
00563   if (const ObjCPropertyRefExpr *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
00564     if (Expr->isImplicitProperty() && Expr->getImplicitPropertySetter() == 0)
00565       return Cl::CM_NoSetterProperty;
00566   }
00567 
00568   CanQualType CT = Ctx.getCanonicalType(E->getType());
00569   // Const stuff is obviously not modifiable.
00570   if (CT.isConstQualified())
00571     return Cl::CM_ConstQualified;
00572 
00573   // Arrays are not modifiable, only their elements are.
00574   if (CT->isArrayType())
00575     return Cl::CM_ArrayType;
00576   // Incomplete types are not modifiable.
00577   if (CT->isIncompleteType())
00578     return Cl::CM_IncompleteType;
00579 
00580   // Records with any const fields (recursively) are not modifiable.
00581   if (const RecordType *R = CT->getAs<RecordType>()) {
00582     assert((E->getObjectKind() == OK_ObjCProperty ||
00583             !Ctx.getLangOpts().CPlusPlus) &&
00584            "C++ struct assignment should be resolved by the "
00585            "copy assignment operator.");
00586     if (R->hasConstFields())
00587       return Cl::CM_ConstQualified;
00588   }
00589 
00590   return Cl::CM_Modifiable;
00591 }
00592 
00593 Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
00594   Classification VC = Classify(Ctx);
00595   switch (VC.getKind()) {
00596   case Cl::CL_LValue: return LV_Valid;
00597   case Cl::CL_XValue: return LV_InvalidExpression;
00598   case Cl::CL_Function: return LV_NotObjectType;
00599   case Cl::CL_Void: return LV_InvalidExpression;
00600   case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;
00601   case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
00602   case Cl::CL_MemberFunction: return LV_MemberFunction;
00603   case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
00604   case Cl::CL_ClassTemporary: return LV_ClassTemporary;
00605   case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
00606   case Cl::CL_PRValue: return LV_InvalidExpression;
00607   }
00608   llvm_unreachable("Unhandled kind");
00609 }
00610 
00611 Expr::isModifiableLvalueResult
00612 Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
00613   SourceLocation dummy;
00614   Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
00615   switch (VC.getKind()) {
00616   case Cl::CL_LValue: break;
00617   case Cl::CL_XValue: return MLV_InvalidExpression;
00618   case Cl::CL_Function: return MLV_NotObjectType;
00619   case Cl::CL_Void: return MLV_InvalidExpression;
00620   case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;
00621   case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
00622   case Cl::CL_MemberFunction: return MLV_MemberFunction;
00623   case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
00624   case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
00625   case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
00626   case Cl::CL_PRValue:
00627     return VC.getModifiable() == Cl::CM_LValueCast ?
00628       MLV_LValueCast : MLV_InvalidExpression;
00629   }
00630   assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
00631   switch (VC.getModifiable()) {
00632   case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
00633   case Cl::CM_Modifiable: return MLV_Valid;
00634   case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
00635   case Cl::CM_Function: return MLV_NotObjectType;
00636   case Cl::CM_LValueCast:
00637     llvm_unreachable("CM_LValueCast and CL_LValue don't match");
00638   case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
00639   case Cl::CM_ConstQualified: return MLV_ConstQualified;
00640   case Cl::CM_ArrayType: return MLV_ArrayType;
00641   case Cl::CM_IncompleteType: return MLV_IncompleteType;
00642   }
00643   llvm_unreachable("Unhandled modifiable type");
00644 }