| 1 | //===- ExprClassification.cpp - Expression AST Node Implementation --------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements Expr::classify. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/Expr.h" |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/DeclCXX.h" |
| 16 | #include "clang/AST/DeclObjC.h" |
| 17 | #include "clang/AST/DeclTemplate.h" |
| 18 | #include "clang/AST/ExprCXX.h" |
| 19 | #include "clang/AST/ExprObjC.h" |
| 20 | #include "llvm/Support/ErrorHandling.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | using Cl = Expr::Classification; |
| 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 Expr *trueExpr, |
| 33 | const Expr *falseExpr); |
| 34 | static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, |
| 35 | Cl::Kinds Kind, SourceLocation &Loc); |
| 36 | |
| 37 | Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const { |
| 38 | assert(!TR->isReferenceType() && "Expressions can't have reference type." ); |
| 39 | |
| 40 | Cl::Kinds kind = ClassifyInternal(Ctx, E: this); |
| 41 | // C99 6.3.2.1: An lvalue is an expression with an object type or an |
| 42 | // incomplete type other than void. |
| 43 | if (!Ctx.getLangOpts().CPlusPlus) { |
| 44 | // Thus, no functions. |
| 45 | if (TR->isFunctionType() || TR == Ctx.OverloadTy) |
| 46 | kind = Cl::CL_Function; |
| 47 | // No void either, but qualified void is OK because it is "other than void". |
| 48 | // Void "lvalues" are classified as addressable void values, which are void |
| 49 | // expressions whose address can be taken. |
| 50 | else if (TR->isVoidType() && !TR.hasQualifiers()) |
| 51 | kind = (kind == Cl::CL_LValue ? Cl::CL_AddressableVoid : Cl::CL_Void); |
| 52 | } |
| 53 | |
| 54 | // Enable this assertion for testing. |
| 55 | switch (kind) { |
| 56 | case Cl::CL_LValue: |
| 57 | assert(isLValue()); |
| 58 | break; |
| 59 | case Cl::CL_XValue: |
| 60 | assert(isXValue()); |
| 61 | break; |
| 62 | case Cl::CL_Function: |
| 63 | case Cl::CL_Void: |
| 64 | case Cl::CL_AddressableVoid: |
| 65 | case Cl::CL_DuplicateVectorComponents: |
| 66 | case Cl::CL_MemberFunction: |
| 67 | case Cl::CL_SubObjCPropertySetting: |
| 68 | case Cl::CL_ClassTemporary: |
| 69 | case Cl::CL_ArrayTemporary: |
| 70 | case Cl::CL_ObjCMessageRValue: |
| 71 | case Cl::CL_PRValue: |
| 72 | assert(isPRValue()); |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | Cl::ModifiableType modifiable = Cl::CM_Untested; |
| 77 | if (Loc) |
| 78 | modifiable = IsModifiable(Ctx, E: this, Kind: kind, Loc&: *Loc); |
| 79 | return Classification(kind, modifiable); |
| 80 | } |
| 81 | |
| 82 | /// Classify an expression which creates a temporary, based on its type. |
| 83 | static Cl::Kinds ClassifyTemporary(QualType T) { |
| 84 | if (T->isRecordType()) |
| 85 | return Cl::CL_ClassTemporary; |
| 86 | if (T->isArrayType()) |
| 87 | return Cl::CL_ArrayTemporary; |
| 88 | |
| 89 | // No special classification: these don't behave differently from normal |
| 90 | // prvalues. |
| 91 | return Cl::CL_PRValue; |
| 92 | } |
| 93 | |
| 94 | static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang, |
| 95 | const Expr *E, |
| 96 | ExprValueKind Kind) { |
| 97 | switch (Kind) { |
| 98 | case VK_PRValue: |
| 99 | return Lang.CPlusPlus ? ClassifyTemporary(T: E->getType()) : Cl::CL_PRValue; |
| 100 | case VK_LValue: |
| 101 | return Cl::CL_LValue; |
| 102 | case VK_XValue: |
| 103 | return Cl::CL_XValue; |
| 104 | } |
| 105 | llvm_unreachable("Invalid value category of implicit cast." ); |
| 106 | } |
| 107 | |
| 108 | static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) { |
| 109 | // This function takes the first stab at classifying expressions. |
| 110 | const LangOptions &Lang = Ctx.getLangOpts(); |
| 111 | |
| 112 | switch (E->getStmtClass()) { |
| 113 | case Stmt::NoStmtClass: |
| 114 | #define ABSTRACT_STMT(Kind) |
| 115 | #define STMT(Kind, Base) case Expr::Kind##Class: |
| 116 | #define EXPR(Kind, Base) |
| 117 | #include "clang/AST/StmtNodes.inc" |
| 118 | llvm_unreachable("cannot classify a statement" ); |
| 119 | |
| 120 | // First come the expressions that are always lvalues, unconditionally. |
| 121 | case Expr::ObjCIsaExprClass: |
| 122 | // Property references are lvalues |
| 123 | case Expr::ObjCSubscriptRefExprClass: |
| 124 | case Expr::ObjCPropertyRefExprClass: |
| 125 | // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of... |
| 126 | case Expr::CXXTypeidExprClass: |
| 127 | case Expr::CXXUuidofExprClass: |
| 128 | // Unresolved lookups and uncorrected typos get classified as lvalues. |
| 129 | // FIXME: Is this wise? Should they get their own kind? |
| 130 | case Expr::UnresolvedLookupExprClass: |
| 131 | case Expr::UnresolvedMemberExprClass: |
| 132 | case Expr::TypoExprClass: |
| 133 | case Expr::DependentCoawaitExprClass: |
| 134 | case Expr::CXXDependentScopeMemberExprClass: |
| 135 | case Expr::DependentScopeDeclRefExprClass: |
| 136 | // ObjC instance variables are lvalues |
| 137 | // FIXME: ObjC++0x might have different rules |
| 138 | case Expr::ObjCIvarRefExprClass: |
| 139 | case Expr::FunctionParmPackExprClass: |
| 140 | case Expr::MSPropertyRefExprClass: |
| 141 | case Expr::MSPropertySubscriptExprClass: |
| 142 | case Expr::ArraySectionExprClass: |
| 143 | case Expr::OMPArrayShapingExprClass: |
| 144 | case Expr::OMPIteratorExprClass: |
| 145 | case Expr::HLSLOutArgExprClass: |
| 146 | return Cl::CL_LValue; |
| 147 | |
| 148 | // C++ [expr.prim.general]p1: A string literal is an lvalue. |
| 149 | case Expr::StringLiteralClass: |
| 150 | // @encode is equivalent to its string |
| 151 | case Expr::ObjCEncodeExprClass: |
| 152 | // Except we special case them as prvalues when they are used to |
| 153 | // initialize a char array. |
| 154 | return E->isLValue() ? Cl::CL_LValue : Cl::CL_PRValue; |
| 155 | |
| 156 | // __func__ and friends are too. |
| 157 | // The char array initialization special case also applies |
| 158 | // when they are transparent. |
| 159 | case Expr::PredefinedExprClass: { |
| 160 | auto *PE = cast<PredefinedExpr>(E); |
| 161 | const StringLiteral *SL = PE->getFunctionName(); |
| 162 | if (PE->isTransparent()) |
| 163 | return SL ? ClassifyInternal(Ctx, SL) : Cl::CL_LValue; |
| 164 | assert(!SL || SL->isLValue()); |
| 165 | return Cl::CL_LValue; |
| 166 | } |
| 167 | |
| 168 | // C99 6.5.2.5p5 says that compound literals are lvalues. |
| 169 | // In C++, they're prvalue temporaries, except for file-scope arrays. |
| 170 | case Expr::CompoundLiteralExprClass: |
| 171 | return !E->isLValue() ? ClassifyTemporary(T: E->getType()) : Cl::CL_LValue; |
| 172 | |
| 173 | // Expressions that are prvalues. |
| 174 | case Expr::CXXBoolLiteralExprClass: |
| 175 | case Expr::CXXPseudoDestructorExprClass: |
| 176 | case Expr::UnaryExprOrTypeTraitExprClass: |
| 177 | case Expr::CXXNewExprClass: |
| 178 | case Expr::CXXNullPtrLiteralExprClass: |
| 179 | case Expr::ImaginaryLiteralClass: |
| 180 | case Expr::GNUNullExprClass: |
| 181 | case Expr::OffsetOfExprClass: |
| 182 | case Expr::CXXThrowExprClass: |
| 183 | case Expr::ShuffleVectorExprClass: |
| 184 | case Expr::ConvertVectorExprClass: |
| 185 | case Expr::IntegerLiteralClass: |
| 186 | case Expr::FixedPointLiteralClass: |
| 187 | case Expr::CharacterLiteralClass: |
| 188 | case Expr::AddrLabelExprClass: |
| 189 | case Expr::CXXDeleteExprClass: |
| 190 | case Expr::ImplicitValueInitExprClass: |
| 191 | case Expr::BlockExprClass: |
| 192 | case Expr::FloatingLiteralClass: |
| 193 | case Expr::CXXNoexceptExprClass: |
| 194 | case Expr::CXXScalarValueInitExprClass: |
| 195 | case Expr::TypeTraitExprClass: |
| 196 | case Expr::ArrayTypeTraitExprClass: |
| 197 | case Expr::ExpressionTraitExprClass: |
| 198 | case Expr::ObjCSelectorExprClass: |
| 199 | case Expr::ObjCProtocolExprClass: |
| 200 | case Expr::ObjCStringLiteralClass: |
| 201 | case Expr::ObjCBoxedExprClass: |
| 202 | case Expr::ObjCArrayLiteralClass: |
| 203 | case Expr::ObjCDictionaryLiteralClass: |
| 204 | case Expr::ObjCBoolLiteralExprClass: |
| 205 | case Expr::ObjCAvailabilityCheckExprClass: |
| 206 | case Expr::ParenListExprClass: |
| 207 | case Expr::SizeOfPackExprClass: |
| 208 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
| 209 | case Expr::AsTypeExprClass: |
| 210 | case Expr::ObjCIndirectCopyRestoreExprClass: |
| 211 | case Expr::AtomicExprClass: |
| 212 | case Expr::CXXFoldExprClass: |
| 213 | case Expr::ArrayInitLoopExprClass: |
| 214 | case Expr::ArrayInitIndexExprClass: |
| 215 | case Expr::NoInitExprClass: |
| 216 | case Expr::DesignatedInitUpdateExprClass: |
| 217 | case Expr::SourceLocExprClass: |
| 218 | case Expr::ConceptSpecializationExprClass: |
| 219 | case Expr::RequiresExprClass: |
| 220 | return Cl::CL_PRValue; |
| 221 | |
| 222 | case Expr::EmbedExprClass: |
| 223 | // Nominally, this just goes through as a PRValue until we actually expand |
| 224 | // it and check it. |
| 225 | return Cl::CL_PRValue; |
| 226 | |
| 227 | // Make HLSL this reference-like |
| 228 | case Expr::CXXThisExprClass: |
| 229 | return Lang.HLSL ? Cl::CL_LValue : Cl::CL_PRValue; |
| 230 | |
| 231 | case Expr::ConstantExprClass: |
| 232 | return ClassifyInternal(Ctx, cast<ConstantExpr>(E)->getSubExpr()); |
| 233 | |
| 234 | // Next come the complicated cases. |
| 235 | case Expr::SubstNonTypeTemplateParmExprClass: |
| 236 | return ClassifyInternal(Ctx, |
| 237 | cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement()); |
| 238 | |
| 239 | case Expr::PackIndexingExprClass: { |
| 240 | // A pack-index-expression always expands to an id-expression. |
| 241 | // Consider it as an LValue expression. |
| 242 | if (cast<PackIndexingExpr>(E)->isInstantiationDependent()) |
| 243 | return Cl::CL_LValue; |
| 244 | return ClassifyInternal(Ctx, cast<PackIndexingExpr>(E)->getSelectedExpr()); |
| 245 | } |
| 246 | |
| 247 | // C, C++98 [expr.sub]p1: The result is an lvalue of type "T". |
| 248 | // C++11 (DR1213): in the case of an array operand, the result is an lvalue |
| 249 | // if that operand is an lvalue and an xvalue otherwise. |
| 250 | // Subscripting vector types is more like member access. |
| 251 | case Expr::ArraySubscriptExprClass: |
| 252 | if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType()) |
| 253 | return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase()); |
| 254 | if (Lang.CPlusPlus11) { |
| 255 | // Step over the array-to-pointer decay if present, but not over the |
| 256 | // temporary materialization. |
| 257 | auto *Base = cast<ArraySubscriptExpr>(E)->getBase()->IgnoreImpCasts(); |
| 258 | if (Base->getType()->isArrayType()) |
| 259 | return ClassifyInternal(Ctx, Base); |
| 260 | } |
| 261 | return Cl::CL_LValue; |
| 262 | |
| 263 | // Subscripting matrix types behaves like member accesses. |
| 264 | case Expr::MatrixSubscriptExprClass: |
| 265 | return ClassifyInternal(Ctx, cast<MatrixSubscriptExpr>(E)->getBase()); |
| 266 | |
| 267 | // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a |
| 268 | // function or variable and a prvalue otherwise. |
| 269 | case Expr::DeclRefExprClass: |
| 270 | if (E->getType() == Ctx.UnknownAnyTy) |
| 271 | return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl()) |
| 272 | ? Cl::CL_PRValue : Cl::CL_LValue; |
| 273 | return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl()); |
| 274 | |
| 275 | // Member access is complex. |
| 276 | case Expr::MemberExprClass: |
| 277 | return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E)); |
| 278 | |
| 279 | case Expr::UnaryOperatorClass: |
| 280 | switch (cast<UnaryOperator>(E)->getOpcode()) { |
| 281 | // C++ [expr.unary.op]p1: The unary * operator performs indirection: |
| 282 | // [...] the result is an lvalue referring to the object or function |
| 283 | // to which the expression points. |
| 284 | case UO_Deref: |
| 285 | return Cl::CL_LValue; |
| 286 | |
| 287 | // GNU extensions, simply look through them. |
| 288 | case UO_Extension: |
| 289 | return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr()); |
| 290 | |
| 291 | // Treat _Real and _Imag basically as if they were member |
| 292 | // expressions: l-value only if the operand is a true l-value. |
| 293 | case UO_Real: |
| 294 | case UO_Imag: { |
| 295 | const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens(); |
| 296 | Cl::Kinds K = ClassifyInternal(Ctx, E: Op); |
| 297 | if (K != Cl::CL_LValue) return K; |
| 298 | |
| 299 | if (isa<ObjCPropertyRefExpr>(Op)) |
| 300 | return Cl::CL_SubObjCPropertySetting; |
| 301 | return Cl::CL_LValue; |
| 302 | } |
| 303 | |
| 304 | // C++ [expr.pre.incr]p1: The result is the updated operand; it is an |
| 305 | // lvalue, [...] |
| 306 | // Not so in C. |
| 307 | case UO_PreInc: |
| 308 | case UO_PreDec: |
| 309 | return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue; |
| 310 | |
| 311 | default: |
| 312 | return Cl::CL_PRValue; |
| 313 | } |
| 314 | |
| 315 | case Expr::RecoveryExprClass: |
| 316 | case Expr::OpaqueValueExprClass: |
| 317 | return ClassifyExprValueKind(Lang, E, Kind: E->getValueKind()); |
| 318 | |
| 319 | // Pseudo-object expressions can produce l-values with reference magic. |
| 320 | case Expr::PseudoObjectExprClass: |
| 321 | return ClassifyExprValueKind(Lang, E, |
| 322 | cast<PseudoObjectExpr>(E)->getValueKind()); |
| 323 | |
| 324 | // Implicit casts are lvalues if they're lvalue casts. Other than that, we |
| 325 | // only specifically record class temporaries. |
| 326 | case Expr::ImplicitCastExprClass: |
| 327 | return ClassifyExprValueKind(Lang, E, Kind: E->getValueKind()); |
| 328 | |
| 329 | // C++ [expr.prim.general]p4: The presence of parentheses does not affect |
| 330 | // whether the expression is an lvalue. |
| 331 | case Expr::ParenExprClass: |
| 332 | return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr()); |
| 333 | |
| 334 | // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator, |
| 335 | // or a void expression if its result expression is, respectively, an |
| 336 | // lvalue, a function designator, or a void expression. |
| 337 | case Expr::GenericSelectionExprClass: |
| 338 | if (cast<GenericSelectionExpr>(E)->isResultDependent()) |
| 339 | return Cl::CL_PRValue; |
| 340 | return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr()); |
| 341 | |
| 342 | case Expr::BinaryOperatorClass: |
| 343 | case Expr::CompoundAssignOperatorClass: |
| 344 | // C doesn't have any binary expressions that are lvalues. |
| 345 | if (Lang.CPlusPlus) |
| 346 | return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E)); |
| 347 | return Cl::CL_PRValue; |
| 348 | |
| 349 | case Expr::CallExprClass: |
| 350 | case Expr::CXXOperatorCallExprClass: |
| 351 | case Expr::CXXMemberCallExprClass: |
| 352 | case Expr::UserDefinedLiteralClass: |
| 353 | case Expr::CUDAKernelCallExprClass: |
| 354 | return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx)); |
| 355 | |
| 356 | case Expr::CXXRewrittenBinaryOperatorClass: |
| 357 | return ClassifyInternal( |
| 358 | Ctx, cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm()); |
| 359 | |
| 360 | // __builtin_choose_expr is equivalent to the chosen expression. |
| 361 | case Expr::ChooseExprClass: |
| 362 | return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr()); |
| 363 | |
| 364 | // Extended vector element access is an lvalue unless there are duplicates |
| 365 | // in the shuffle expression. |
| 366 | case Expr::ExtVectorElementExprClass: |
| 367 | if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements()) |
| 368 | return Cl::CL_DuplicateVectorComponents; |
| 369 | if (cast<ExtVectorElementExpr>(E)->isArrow()) |
| 370 | return Cl::CL_LValue; |
| 371 | return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase()); |
| 372 | |
| 373 | // Simply look at the actual default argument. |
| 374 | case Expr::CXXDefaultArgExprClass: |
| 375 | return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr()); |
| 376 | |
| 377 | // Same idea for default initializers. |
| 378 | case Expr::CXXDefaultInitExprClass: |
| 379 | return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr()); |
| 380 | |
| 381 | // Same idea for temporary binding. |
| 382 | case Expr::CXXBindTemporaryExprClass: |
| 383 | return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr()); |
| 384 | |
| 385 | // And the cleanups guard. |
| 386 | case Expr::ExprWithCleanupsClass: |
| 387 | return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr()); |
| 388 | |
| 389 | // Casts depend completely on the target type. All casts work the same. |
| 390 | case Expr::CStyleCastExprClass: |
| 391 | case Expr::CXXFunctionalCastExprClass: |
| 392 | case Expr::CXXStaticCastExprClass: |
| 393 | case Expr::CXXDynamicCastExprClass: |
| 394 | case Expr::CXXReinterpretCastExprClass: |
| 395 | case Expr::CXXConstCastExprClass: |
| 396 | case Expr::CXXAddrspaceCastExprClass: |
| 397 | case Expr::ObjCBridgedCastExprClass: |
| 398 | case Expr::BuiltinBitCastExprClass: |
| 399 | // Only in C++ can casts be interesting at all. |
| 400 | if (!Lang.CPlusPlus) return Cl::CL_PRValue; |
| 401 | return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten()); |
| 402 | |
| 403 | case Expr::CXXUnresolvedConstructExprClass: |
| 404 | return ClassifyUnnamed(Ctx, |
| 405 | cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten()); |
| 406 | |
| 407 | case Expr::BinaryConditionalOperatorClass: { |
| 408 | if (!Lang.CPlusPlus) return Cl::CL_PRValue; |
| 409 | const auto *co = cast<BinaryConditionalOperator>(E); |
| 410 | return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); |
| 411 | } |
| 412 | |
| 413 | case Expr::ConditionalOperatorClass: { |
| 414 | // Once again, only C++ is interesting. |
| 415 | if (!Lang.CPlusPlus) return Cl::CL_PRValue; |
| 416 | const auto *co = cast<ConditionalOperator>(E); |
| 417 | return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr()); |
| 418 | } |
| 419 | |
| 420 | // ObjC message sends are effectively function calls, if the target function |
| 421 | // is known. |
| 422 | case Expr::ObjCMessageExprClass: |
| 423 | if (const ObjCMethodDecl *Method = |
| 424 | cast<ObjCMessageExpr>(E)->getMethodDecl()) { |
| 425 | Cl::Kinds kind = ClassifyUnnamed(Ctx, T: Method->getReturnType()); |
| 426 | return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind; |
| 427 | } |
| 428 | return Cl::CL_PRValue; |
| 429 | |
| 430 | // Some C++ expressions are always class temporaries. |
| 431 | case Expr::CXXConstructExprClass: |
| 432 | case Expr::CXXInheritedCtorInitExprClass: |
| 433 | case Expr::CXXTemporaryObjectExprClass: |
| 434 | case Expr::LambdaExprClass: |
| 435 | case Expr::CXXStdInitializerListExprClass: |
| 436 | return Cl::CL_ClassTemporary; |
| 437 | |
| 438 | case Expr::VAArgExprClass: |
| 439 | return ClassifyUnnamed(Ctx, T: E->getType()); |
| 440 | |
| 441 | case Expr::DesignatedInitExprClass: |
| 442 | return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit()); |
| 443 | |
| 444 | case Expr::StmtExprClass: { |
| 445 | const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt(); |
| 446 | if (const auto *LastExpr = dyn_cast_or_null<Expr>(S->body_back())) |
| 447 | return ClassifyUnnamed(Ctx, LastExpr->getType()); |
| 448 | return Cl::CL_PRValue; |
| 449 | } |
| 450 | |
| 451 | case Expr::PackExpansionExprClass: |
| 452 | return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern()); |
| 453 | |
| 454 | case Expr::MaterializeTemporaryExprClass: |
| 455 | return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference() |
| 456 | ? Cl::CL_LValue |
| 457 | : Cl::CL_XValue; |
| 458 | |
| 459 | case Expr::InitListExprClass: |
| 460 | // An init list can be an lvalue if it is bound to a reference and |
| 461 | // contains only one element. In that case, we look at that element |
| 462 | // for an exact classification. Init list creation takes care of the |
| 463 | // value kind for us, so we only need to fine-tune. |
| 464 | if (E->isPRValue()) |
| 465 | return ClassifyExprValueKind(Lang, E, Kind: E->getValueKind()); |
| 466 | assert(cast<InitListExpr>(E)->getNumInits() == 1 && |
| 467 | "Only 1-element init lists can be glvalues." ); |
| 468 | return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0)); |
| 469 | |
| 470 | case Expr::CoawaitExprClass: |
| 471 | case Expr::CoyieldExprClass: |
| 472 | return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr()); |
| 473 | case Expr::SYCLUniqueStableNameExprClass: |
| 474 | case Expr::OpenACCAsteriskSizeExprClass: |
| 475 | return Cl::CL_PRValue; |
| 476 | break; |
| 477 | |
| 478 | case Expr::CXXParenListInitExprClass: |
| 479 | if (isa<ArrayType>(E->getType())) |
| 480 | return Cl::CL_ArrayTemporary; |
| 481 | return Cl::CL_ClassTemporary; |
| 482 | } |
| 483 | |
| 484 | llvm_unreachable("unhandled expression kind in classification" ); |
| 485 | } |
| 486 | |
| 487 | /// ClassifyDecl - Return the classification of an expression referencing the |
| 488 | /// given declaration. |
| 489 | static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) { |
| 490 | // C++ [expr.prim.id.unqual]p3: The result is an lvalue if the entity is a |
| 491 | // function, variable, or data member, or a template parameter object and a |
| 492 | // prvalue otherwise. |
| 493 | // In C, functions are not lvalues. |
| 494 | // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an |
| 495 | // lvalue unless it's a reference type or a class type (C++ [temp.param]p8), |
| 496 | // so we need to special-case this. |
| 497 | |
| 498 | if (const auto *M = dyn_cast<CXXMethodDecl>(Val: D)) { |
| 499 | if (M->isImplicitObjectMemberFunction()) |
| 500 | return Cl::CL_MemberFunction; |
| 501 | if (M->isStatic()) |
| 502 | return Cl::CL_LValue; |
| 503 | return Cl::CL_PRValue; |
| 504 | } |
| 505 | |
| 506 | bool islvalue; |
| 507 | if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(Val: D)) |
| 508 | islvalue = NTTParm->getType()->isReferenceType() || |
| 509 | NTTParm->getType()->isRecordType(); |
| 510 | else |
| 511 | islvalue = |
| 512 | isa<VarDecl, FieldDecl, IndirectFieldDecl, BindingDecl, MSGuidDecl, |
| 513 | UnnamedGlobalConstantDecl, TemplateParamObjectDecl>(Val: D) || |
| 514 | (Ctx.getLangOpts().CPlusPlus && |
| 515 | (isa<FunctionDecl, MSPropertyDecl, FunctionTemplateDecl>(Val: D))); |
| 516 | |
| 517 | return islvalue ? Cl::CL_LValue : Cl::CL_PRValue; |
| 518 | } |
| 519 | |
| 520 | /// ClassifyUnnamed - Return the classification of an expression yielding an |
| 521 | /// unnamed value of the given type. This applies in particular to function |
| 522 | /// calls and casts. |
| 523 | static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) { |
| 524 | // In C, function calls are always rvalues. |
| 525 | if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue; |
| 526 | |
| 527 | // C++ [expr.call]p10: A function call is an lvalue if the result type is an |
| 528 | // lvalue reference type or an rvalue reference to function type, an xvalue |
| 529 | // if the result type is an rvalue reference to object type, and a prvalue |
| 530 | // otherwise. |
| 531 | if (T->isLValueReferenceType()) |
| 532 | return Cl::CL_LValue; |
| 533 | const auto *RV = T->getAs<RValueReferenceType>(); |
| 534 | if (!RV) // Could still be a class temporary, though. |
| 535 | return ClassifyTemporary(T); |
| 536 | |
| 537 | return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue; |
| 538 | } |
| 539 | |
| 540 | static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) { |
| 541 | if (E->getType() == Ctx.UnknownAnyTy) |
| 542 | return (isa<FunctionDecl>(Val: E->getMemberDecl()) |
| 543 | ? Cl::CL_PRValue : Cl::CL_LValue); |
| 544 | |
| 545 | // Handle C first, it's easier. |
| 546 | if (!Ctx.getLangOpts().CPlusPlus) { |
| 547 | // C99 6.5.2.3p3 |
| 548 | // For dot access, the expression is an lvalue if the first part is. For |
| 549 | // arrow access, it always is an lvalue. |
| 550 | if (E->isArrow()) |
| 551 | return Cl::CL_LValue; |
| 552 | // ObjC property accesses are not lvalues, but get special treatment. |
| 553 | Expr *Base = E->getBase()->IgnoreParens(); |
| 554 | if (isa<ObjCPropertyRefExpr>(Val: Base)) |
| 555 | return Cl::CL_SubObjCPropertySetting; |
| 556 | return ClassifyInternal(Ctx, E: Base); |
| 557 | } |
| 558 | |
| 559 | NamedDecl *Member = E->getMemberDecl(); |
| 560 | // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2. |
| 561 | // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then |
| 562 | // E1.E2 is an lvalue. |
| 563 | if (const auto *Value = dyn_cast<ValueDecl>(Member)) |
| 564 | if (Value->getType()->isReferenceType()) |
| 565 | return Cl::CL_LValue; |
| 566 | |
| 567 | // Otherwise, one of the following rules applies. |
| 568 | // -- If E2 is a static member [...] then E1.E2 is an lvalue. |
| 569 | if (isa<VarDecl>(Val: Member) && Member->getDeclContext()->isRecord()) |
| 570 | return Cl::CL_LValue; |
| 571 | |
| 572 | // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then |
| 573 | // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue; |
| 574 | // otherwise, it is a prvalue. |
| 575 | if (isa<FieldDecl>(Val: Member)) { |
| 576 | // *E1 is an lvalue |
| 577 | if (E->isArrow()) |
| 578 | return Cl::CL_LValue; |
| 579 | Expr *Base = E->getBase()->IgnoreParenImpCasts(); |
| 580 | if (isa<ObjCPropertyRefExpr>(Val: Base)) |
| 581 | return Cl::CL_SubObjCPropertySetting; |
| 582 | return ClassifyInternal(Ctx, E: E->getBase()); |
| 583 | } |
| 584 | |
| 585 | // -- If E2 is a [...] member function, [...] |
| 586 | // -- If it refers to a static member function [...], then E1.E2 is an |
| 587 | // lvalue; [...] |
| 588 | // -- Otherwise [...] E1.E2 is a prvalue. |
| 589 | if (const auto *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 590 | if (Method->isStatic()) |
| 591 | return Cl::CL_LValue; |
| 592 | if (Method->isImplicitObjectMemberFunction()) |
| 593 | return Cl::CL_MemberFunction; |
| 594 | return Cl::CL_PRValue; |
| 595 | } |
| 596 | |
| 597 | // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue. |
| 598 | // So is everything else we haven't handled yet. |
| 599 | return Cl::CL_PRValue; |
| 600 | } |
| 601 | |
| 602 | static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) { |
| 603 | assert(Ctx.getLangOpts().CPlusPlus && |
| 604 | "This is only relevant for C++." ); |
| 605 | // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand. |
| 606 | // Except we override this for writes to ObjC properties. |
| 607 | if (E->isAssignmentOp()) |
| 608 | return (E->getLHS()->getObjectKind() == OK_ObjCProperty |
| 609 | ? Cl::CL_PRValue : Cl::CL_LValue); |
| 610 | |
| 611 | // C++ [expr.comma]p1: the result is of the same value category as its right |
| 612 | // operand, [...]. |
| 613 | if (E->getOpcode() == BO_Comma) |
| 614 | return ClassifyInternal(Ctx, E: E->getRHS()); |
| 615 | |
| 616 | // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand |
| 617 | // is a pointer to a data member is of the same value category as its first |
| 618 | // operand. |
| 619 | if (E->getOpcode() == BO_PtrMemD) |
| 620 | return (E->getType()->isFunctionType() || |
| 621 | E->hasPlaceholderType(BuiltinType::BoundMember)) |
| 622 | ? Cl::CL_MemberFunction |
| 623 | : ClassifyInternal(Ctx, E: E->getLHS()); |
| 624 | |
| 625 | // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its |
| 626 | // second operand is a pointer to data member and a prvalue otherwise. |
| 627 | if (E->getOpcode() == BO_PtrMemI) |
| 628 | return (E->getType()->isFunctionType() || |
| 629 | E->hasPlaceholderType(BuiltinType::BoundMember)) |
| 630 | ? Cl::CL_MemberFunction |
| 631 | : Cl::CL_LValue; |
| 632 | |
| 633 | // All other binary operations are prvalues. |
| 634 | return Cl::CL_PRValue; |
| 635 | } |
| 636 | |
| 637 | static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True, |
| 638 | const Expr *False) { |
| 639 | assert(Ctx.getLangOpts().CPlusPlus && |
| 640 | "This is only relevant for C++." ); |
| 641 | |
| 642 | // C++ [expr.cond]p2 |
| 643 | // If either the second or the third operand has type (cv) void, |
| 644 | // one of the following shall hold: |
| 645 | if (True->getType()->isVoidType() || False->getType()->isVoidType()) { |
| 646 | // The second or the third operand (but not both) is a (possibly |
| 647 | // parenthesized) throw-expression; the result is of the [...] value |
| 648 | // category of the other. |
| 649 | bool TrueIsThrow = isa<CXXThrowExpr>(Val: True->IgnoreParenImpCasts()); |
| 650 | bool FalseIsThrow = isa<CXXThrowExpr>(Val: False->IgnoreParenImpCasts()); |
| 651 | if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False) |
| 652 | : (FalseIsThrow ? True : nullptr)) |
| 653 | return ClassifyInternal(Ctx, E: NonThrow); |
| 654 | |
| 655 | // [Otherwise] the result [...] is a prvalue. |
| 656 | return Cl::CL_PRValue; |
| 657 | } |
| 658 | |
| 659 | // Note that at this point, we have already performed all conversions |
| 660 | // according to [expr.cond]p3. |
| 661 | // C++ [expr.cond]p4: If the second and third operands are glvalues of the |
| 662 | // same value category [...], the result is of that [...] value category. |
| 663 | // C++ [expr.cond]p5: Otherwise, the result is a prvalue. |
| 664 | Cl::Kinds LCl = ClassifyInternal(Ctx, E: True), |
| 665 | RCl = ClassifyInternal(Ctx, E: False); |
| 666 | return LCl == RCl ? LCl : Cl::CL_PRValue; |
| 667 | } |
| 668 | |
| 669 | static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, |
| 670 | Cl::Kinds Kind, SourceLocation &Loc) { |
| 671 | // As a general rule, we only care about lvalues. But there are some rvalues |
| 672 | // for which we want to generate special results. |
| 673 | if (Kind == Cl::CL_PRValue) { |
| 674 | // For the sake of better diagnostics, we want to specifically recognize |
| 675 | // use of the GCC cast-as-lvalue extension. |
| 676 | if (const auto *CE = dyn_cast<ExplicitCastExpr>(Val: E->IgnoreParens())) { |
| 677 | if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) { |
| 678 | Loc = CE->getExprLoc(); |
| 679 | return Cl::CM_LValueCast; |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | if (Kind != Cl::CL_LValue) |
| 684 | return Cl::CM_RValue; |
| 685 | |
| 686 | // This is the lvalue case. |
| 687 | // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6) |
| 688 | if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType()) |
| 689 | return Cl::CM_Function; |
| 690 | |
| 691 | // Assignment to a property in ObjC is an implicit setter access. But a |
| 692 | // setter might not exist. |
| 693 | if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(Val: E)) { |
| 694 | if (Expr->isImplicitProperty() && |
| 695 | Expr->getImplicitPropertySetter() == nullptr) |
| 696 | return Cl::CM_NoSetterProperty; |
| 697 | } |
| 698 | |
| 699 | CanQualType CT = Ctx.getCanonicalType(T: E->getType()); |
| 700 | // Const stuff is obviously not modifiable. |
| 701 | if (CT.isConstQualified()) |
| 702 | return Cl::CM_ConstQualified; |
| 703 | if (Ctx.getLangOpts().OpenCL && |
| 704 | CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant) |
| 705 | return Cl::CM_ConstAddrSpace; |
| 706 | |
| 707 | // Arrays are not modifiable, only their elements are. |
| 708 | if (CT->isArrayType() && |
| 709 | !(Ctx.getLangOpts().HLSL && CT->isConstantArrayType())) |
| 710 | return Cl::CM_ArrayType; |
| 711 | // Incomplete types are not modifiable. |
| 712 | if (CT->isIncompleteType()) |
| 713 | return Cl::CM_IncompleteType; |
| 714 | |
| 715 | // Records with any const fields (recursively) are not modifiable. |
| 716 | if (const RecordType *R = CT->getAs<RecordType>()) |
| 717 | if (R->hasConstFields()) |
| 718 | return Cl::CM_ConstQualifiedField; |
| 719 | |
| 720 | return Cl::CM_Modifiable; |
| 721 | } |
| 722 | |
| 723 | Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const { |
| 724 | Classification VC = Classify(Ctx); |
| 725 | switch (VC.getKind()) { |
| 726 | case Cl::CL_LValue: return LV_Valid; |
| 727 | case Cl::CL_XValue: return LV_InvalidExpression; |
| 728 | case Cl::CL_Function: return LV_NotObjectType; |
| 729 | case Cl::CL_Void: return LV_InvalidExpression; |
| 730 | case Cl::CL_AddressableVoid: return LV_IncompleteVoidType; |
| 731 | case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents; |
| 732 | case Cl::CL_MemberFunction: return LV_MemberFunction; |
| 733 | case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting; |
| 734 | case Cl::CL_ClassTemporary: return LV_ClassTemporary; |
| 735 | case Cl::CL_ArrayTemporary: return LV_ArrayTemporary; |
| 736 | case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression; |
| 737 | case Cl::CL_PRValue: return LV_InvalidExpression; |
| 738 | } |
| 739 | llvm_unreachable("Unhandled kind" ); |
| 740 | } |
| 741 | |
| 742 | Expr::isModifiableLvalueResult |
| 743 | Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const { |
| 744 | SourceLocation dummy; |
| 745 | Classification VC = ClassifyModifiable(Ctx, Loc&: Loc ? *Loc : dummy); |
| 746 | switch (VC.getKind()) { |
| 747 | case Cl::CL_LValue: break; |
| 748 | case Cl::CL_XValue: return MLV_InvalidExpression; |
| 749 | case Cl::CL_Function: return MLV_NotObjectType; |
| 750 | case Cl::CL_Void: return MLV_InvalidExpression; |
| 751 | case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType; |
| 752 | case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; |
| 753 | case Cl::CL_MemberFunction: return MLV_MemberFunction; |
| 754 | case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting; |
| 755 | case Cl::CL_ClassTemporary: return MLV_ClassTemporary; |
| 756 | case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary; |
| 757 | case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression; |
| 758 | case Cl::CL_PRValue: |
| 759 | return VC.getModifiable() == Cl::CM_LValueCast ? |
| 760 | MLV_LValueCast : MLV_InvalidExpression; |
| 761 | } |
| 762 | assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind" ); |
| 763 | switch (VC.getModifiable()) { |
| 764 | case Cl::CM_Untested: llvm_unreachable("Did not test modifiability" ); |
| 765 | case Cl::CM_Modifiable: return MLV_Valid; |
| 766 | case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match" ); |
| 767 | case Cl::CM_Function: return MLV_NotObjectType; |
| 768 | case Cl::CM_LValueCast: |
| 769 | llvm_unreachable("CM_LValueCast and CL_LValue don't match" ); |
| 770 | case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty; |
| 771 | case Cl::CM_ConstQualified: return MLV_ConstQualified; |
| 772 | case Cl::CM_ConstQualifiedField: return MLV_ConstQualifiedField; |
| 773 | case Cl::CM_ConstAddrSpace: return MLV_ConstAddrSpace; |
| 774 | case Cl::CM_ArrayType: return MLV_ArrayType; |
| 775 | case Cl::CM_IncompleteType: return MLV_IncompleteType; |
| 776 | } |
| 777 | llvm_unreachable("Unhandled modifiable type" ); |
| 778 | } |
| 779 | |