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