clang 19.0.0git
ExprClassification.cpp
Go to the documentation of this file.
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"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
20#include "llvm/Support/ErrorHandling.h"
21
22using namespace clang;
23
25
26static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E);
27static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D);
32 const Expr *trueExpr,
33 const Expr *falseExpr);
35 Cl::Kinds Kind, SourceLocation &Loc);
36
37Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {
38 assert(!TR->isReferenceType() && "Expressions can't have reference type.");
39
40 Cl::Kinds kind = ClassifyInternal(Ctx, 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())
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:
71 case Cl::CL_PRValue:
72 assert(isPRValue());
73 break;
74 }
75
77 if (Loc)
78 modifiable = IsModifiable(Ctx, this, kind, *Loc);
79 return Classification(kind, modifiable);
80}
81
82/// Classify an expression which creates a temporary, based on its type.
84 if (T->isRecordType())
86 if (T->isArrayType())
88
89 // No special classification: these don't behave differently from normal
90 // prvalues.
91 return Cl::CL_PRValue;
92}
93
95 const Expr *E,
96 ExprValueKind Kind) {
97 switch (Kind) {
98 case VK_PRValue:
99 return Lang.CPlusPlus ? ClassifyTemporary(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
109 // This function takes the first stab at classifying expressions.
110 const LangOptions &Lang = Ctx.getLangOpts();
111
112 switch (E->getStmtClass()) {
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 // C++ [expr.prim.general]p1: A string literal is an lvalue.
123 case Expr::StringLiteralClass:
124 // @encode is equivalent to its string
125 case Expr::ObjCEncodeExprClass:
126 // __func__ and friends are too.
127 case Expr::PredefinedExprClass:
128 // Property references are lvalues
129 case Expr::ObjCSubscriptRefExprClass:
130 case Expr::ObjCPropertyRefExprClass:
131 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
132 case Expr::CXXTypeidExprClass:
133 case Expr::CXXUuidofExprClass:
134 // Unresolved lookups and uncorrected typos get classified as lvalues.
135 // FIXME: Is this wise? Should they get their own kind?
136 case Expr::UnresolvedLookupExprClass:
137 case Expr::UnresolvedMemberExprClass:
138 case Expr::TypoExprClass:
139 case Expr::DependentCoawaitExprClass:
140 case Expr::CXXDependentScopeMemberExprClass:
141 case Expr::DependentScopeDeclRefExprClass:
142 // ObjC instance variables are lvalues
143 // FIXME: ObjC++0x might have different rules
144 case Expr::ObjCIvarRefExprClass:
145 case Expr::FunctionParmPackExprClass:
146 case Expr::MSPropertyRefExprClass:
147 case Expr::MSPropertySubscriptExprClass:
148 case Expr::ArraySectionExprClass:
149 case Expr::OMPArrayShapingExprClass:
150 case Expr::OMPIteratorExprClass:
151 return Cl::CL_LValue;
152
153 // C99 6.5.2.5p5 says that compound literals are lvalues.
154 // In C++, they're prvalue temporaries, except for file-scope arrays.
155 case Expr::CompoundLiteralExprClass:
156 return !E->isLValue() ? ClassifyTemporary(E->getType()) : Cl::CL_LValue;
157
158 // Expressions that are prvalues.
159 case Expr::CXXBoolLiteralExprClass:
160 case Expr::CXXPseudoDestructorExprClass:
161 case Expr::UnaryExprOrTypeTraitExprClass:
162 case Expr::CXXNewExprClass:
163 case Expr::CXXNullPtrLiteralExprClass:
164 case Expr::ImaginaryLiteralClass:
165 case Expr::GNUNullExprClass:
166 case Expr::OffsetOfExprClass:
167 case Expr::CXXThrowExprClass:
168 case Expr::ShuffleVectorExprClass:
169 case Expr::ConvertVectorExprClass:
170 case Expr::IntegerLiteralClass:
171 case Expr::FixedPointLiteralClass:
172 case Expr::CharacterLiteralClass:
173 case Expr::AddrLabelExprClass:
174 case Expr::CXXDeleteExprClass:
175 case Expr::ImplicitValueInitExprClass:
176 case Expr::BlockExprClass:
177 case Expr::FloatingLiteralClass:
178 case Expr::CXXNoexceptExprClass:
179 case Expr::CXXScalarValueInitExprClass:
180 case Expr::TypeTraitExprClass:
181 case Expr::ArrayTypeTraitExprClass:
182 case Expr::ExpressionTraitExprClass:
183 case Expr::ObjCSelectorExprClass:
184 case Expr::ObjCProtocolExprClass:
185 case Expr::ObjCStringLiteralClass:
186 case Expr::ObjCBoxedExprClass:
187 case Expr::ObjCArrayLiteralClass:
188 case Expr::ObjCDictionaryLiteralClass:
189 case Expr::ObjCBoolLiteralExprClass:
190 case Expr::ObjCAvailabilityCheckExprClass:
191 case Expr::ParenListExprClass:
192 case Expr::SizeOfPackExprClass:
193 case Expr::SubstNonTypeTemplateParmPackExprClass:
194 case Expr::AsTypeExprClass:
195 case Expr::ObjCIndirectCopyRestoreExprClass:
196 case Expr::AtomicExprClass:
197 case Expr::CXXFoldExprClass:
198 case Expr::ArrayInitLoopExprClass:
199 case Expr::ArrayInitIndexExprClass:
200 case Expr::NoInitExprClass:
201 case Expr::DesignatedInitUpdateExprClass:
202 case Expr::SourceLocExprClass:
203 case Expr::ConceptSpecializationExprClass:
204 case Expr::RequiresExprClass:
205 return Cl::CL_PRValue;
206
207 // Make HLSL this reference-like
208 case Expr::CXXThisExprClass:
209 return Lang.HLSL ? Cl::CL_LValue : Cl::CL_PRValue;
210
211 case Expr::ConstantExprClass:
212 return ClassifyInternal(Ctx, cast<ConstantExpr>(E)->getSubExpr());
213
214 // Next come the complicated cases.
215 case Expr::SubstNonTypeTemplateParmExprClass:
216 return ClassifyInternal(Ctx,
217 cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
218
219 case Expr::PackIndexingExprClass: {
220 // A pack-index-expression always expands to an id-expression.
221 // Consider it as an LValue expression.
222 if (cast<PackIndexingExpr>(E)->isInstantiationDependent())
223 return Cl::CL_LValue;
224 return ClassifyInternal(Ctx, cast<PackIndexingExpr>(E)->getSelectedExpr());
225 }
226
227 // C, C++98 [expr.sub]p1: The result is an lvalue of type "T".
228 // C++11 (DR1213): in the case of an array operand, the result is an lvalue
229 // if that operand is an lvalue and an xvalue otherwise.
230 // Subscripting vector types is more like member access.
231 case Expr::ArraySubscriptExprClass:
232 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
233 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
234 if (Lang.CPlusPlus11) {
235 // Step over the array-to-pointer decay if present, but not over the
236 // temporary materialization.
237 auto *Base = cast<ArraySubscriptExpr>(E)->getBase()->IgnoreImpCasts();
238 if (Base->getType()->isArrayType())
239 return ClassifyInternal(Ctx, Base);
240 }
241 return Cl::CL_LValue;
242
243 // Subscripting matrix types behaves like member accesses.
244 case Expr::MatrixSubscriptExprClass:
245 return ClassifyInternal(Ctx, cast<MatrixSubscriptExpr>(E)->getBase());
246
247 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
248 // function or variable and a prvalue otherwise.
249 case Expr::DeclRefExprClass:
250 if (E->getType() == Ctx.UnknownAnyTy)
251 return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
253 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
254
255 // Member access is complex.
256 case Expr::MemberExprClass:
257 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
258
259 case Expr::UnaryOperatorClass:
260 switch (cast<UnaryOperator>(E)->getOpcode()) {
261 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
262 // [...] the result is an lvalue referring to the object or function
263 // to which the expression points.
264 case UO_Deref:
265 return Cl::CL_LValue;
266
267 // GNU extensions, simply look through them.
268 case UO_Extension:
269 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
270
271 // Treat _Real and _Imag basically as if they were member
272 // expressions: l-value only if the operand is a true l-value.
273 case UO_Real:
274 case UO_Imag: {
275 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
276 Cl::Kinds K = ClassifyInternal(Ctx, Op);
277 if (K != Cl::CL_LValue) return K;
278
279 if (isa<ObjCPropertyRefExpr>(Op))
281 return Cl::CL_LValue;
282 }
283
284 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
285 // lvalue, [...]
286 // Not so in C.
287 case UO_PreInc:
288 case UO_PreDec:
289 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
290
291 default:
292 return Cl::CL_PRValue;
293 }
294
295 case Expr::RecoveryExprClass:
296 case Expr::OpaqueValueExprClass:
297 return ClassifyExprValueKind(Lang, E, E->getValueKind());
298
299 // Pseudo-object expressions can produce l-values with reference magic.
300 case Expr::PseudoObjectExprClass:
301 return ClassifyExprValueKind(Lang, E,
302 cast<PseudoObjectExpr>(E)->getValueKind());
303
304 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
305 // only specifically record class temporaries.
306 case Expr::ImplicitCastExprClass:
307 return ClassifyExprValueKind(Lang, E, E->getValueKind());
308
309 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
310 // whether the expression is an lvalue.
311 case Expr::ParenExprClass:
312 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
313
314 // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
315 // or a void expression if its result expression is, respectively, an
316 // lvalue, a function designator, or a void expression.
317 case Expr::GenericSelectionExprClass:
318 if (cast<GenericSelectionExpr>(E)->isResultDependent())
319 return Cl::CL_PRValue;
320 return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
321
322 case Expr::BinaryOperatorClass:
323 case Expr::CompoundAssignOperatorClass:
324 // C doesn't have any binary expressions that are lvalues.
325 if (Lang.CPlusPlus)
326 return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
327 return Cl::CL_PRValue;
328
329 case Expr::CallExprClass:
330 case Expr::CXXOperatorCallExprClass:
331 case Expr::CXXMemberCallExprClass:
332 case Expr::UserDefinedLiteralClass:
333 case Expr::CUDAKernelCallExprClass:
334 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx));
335
336 case Expr::CXXRewrittenBinaryOperatorClass:
337 return ClassifyInternal(
338 Ctx, cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm());
339
340 // __builtin_choose_expr is equivalent to the chosen expression.
341 case Expr::ChooseExprClass:
342 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr());
343
344 // Extended vector element access is an lvalue unless there are duplicates
345 // in the shuffle expression.
346 case Expr::ExtVectorElementExprClass:
347 if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements())
349 if (cast<ExtVectorElementExpr>(E)->isArrow())
350 return Cl::CL_LValue;
351 return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase());
352
353 // Simply look at the actual default argument.
354 case Expr::CXXDefaultArgExprClass:
355 return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
356
357 // Same idea for default initializers.
358 case Expr::CXXDefaultInitExprClass:
359 return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr());
360
361 // Same idea for temporary binding.
362 case Expr::CXXBindTemporaryExprClass:
363 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
364
365 // And the cleanups guard.
366 case Expr::ExprWithCleanupsClass:
367 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
368
369 // Casts depend completely on the target type. All casts work the same.
370 case Expr::CStyleCastExprClass:
371 case Expr::CXXFunctionalCastExprClass:
372 case Expr::CXXStaticCastExprClass:
373 case Expr::CXXDynamicCastExprClass:
374 case Expr::CXXReinterpretCastExprClass:
375 case Expr::CXXConstCastExprClass:
376 case Expr::CXXAddrspaceCastExprClass:
377 case Expr::ObjCBridgedCastExprClass:
378 case Expr::BuiltinBitCastExprClass:
379 // Only in C++ can casts be interesting at all.
380 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
381 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
382
383 case Expr::CXXUnresolvedConstructExprClass:
384 return ClassifyUnnamed(Ctx,
385 cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
386
387 case Expr::BinaryConditionalOperatorClass: {
388 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
389 const auto *co = cast<BinaryConditionalOperator>(E);
390 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
391 }
392
393 case Expr::ConditionalOperatorClass: {
394 // Once again, only C++ is interesting.
395 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
396 const auto *co = cast<ConditionalOperator>(E);
397 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
398 }
399
400 // ObjC message sends are effectively function calls, if the target function
401 // is known.
402 case Expr::ObjCMessageExprClass:
403 if (const ObjCMethodDecl *Method =
404 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
405 Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType());
406 return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
407 }
408 return Cl::CL_PRValue;
409
410 // Some C++ expressions are always class temporaries.
411 case Expr::CXXConstructExprClass:
412 case Expr::CXXInheritedCtorInitExprClass:
413 case Expr::CXXTemporaryObjectExprClass:
414 case Expr::LambdaExprClass:
415 case Expr::CXXStdInitializerListExprClass:
417
418 case Expr::VAArgExprClass:
419 return ClassifyUnnamed(Ctx, E->getType());
420
421 case Expr::DesignatedInitExprClass:
422 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
423
424 case Expr::StmtExprClass: {
425 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
426 if (const auto *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
427 return ClassifyUnnamed(Ctx, LastExpr->getType());
428 return Cl::CL_PRValue;
429 }
430
431 case Expr::PackExpansionExprClass:
432 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
433
434 case Expr::MaterializeTemporaryExprClass:
435 return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
438
439 case Expr::InitListExprClass:
440 // An init list can be an lvalue if it is bound to a reference and
441 // contains only one element. In that case, we look at that element
442 // for an exact classification. Init list creation takes care of the
443 // value kind for us, so we only need to fine-tune.
444 if (E->isPRValue())
445 return ClassifyExprValueKind(Lang, E, E->getValueKind());
446 assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
447 "Only 1-element init lists can be glvalues.");
448 return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
449
450 case Expr::CoawaitExprClass:
451 case Expr::CoyieldExprClass:
452 return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr());
453 case Expr::SYCLUniqueStableNameExprClass:
454 return Cl::CL_PRValue;
455 break;
456
457 case Expr::CXXParenListInitExprClass:
458 if (isa<ArrayType>(E->getType()))
461 }
462
463 llvm_unreachable("unhandled expression kind in classification");
464}
465
466/// ClassifyDecl - Return the classification of an expression referencing the
467/// given declaration.
468static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
469 // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
470 // function, variable, or data member and a prvalue otherwise.
471 // In C, functions are not lvalues.
472 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
473 // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
474 // special-case this.
475
476 if (const auto *M = dyn_cast<CXXMethodDecl>(D)) {
477 if (M->isImplicitObjectMemberFunction())
479 if (M->isStatic())
480 return Cl::CL_LValue;
481 return Cl::CL_PRValue;
482 }
483
484 bool islvalue;
485 if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(D))
486 islvalue = NTTParm->getType()->isReferenceType() ||
487 NTTParm->getType()->isRecordType();
488 else
489 islvalue =
492 (Ctx.getLangOpts().CPlusPlus &&
493 (isa<FunctionDecl, MSPropertyDecl, FunctionTemplateDecl>(D)));
494
495 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
496}
497
498/// ClassifyUnnamed - Return the classification of an expression yielding an
499/// unnamed value of the given type. This applies in particular to function
500/// calls and casts.
502 // In C, function calls are always rvalues.
503 if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue;
504
505 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
506 // lvalue reference type or an rvalue reference to function type, an xvalue
507 // if the result type is an rvalue reference to object type, and a prvalue
508 // otherwise.
510 return Cl::CL_LValue;
511 const auto *RV = T->getAs<RValueReferenceType>();
512 if (!RV) // Could still be a class temporary, though.
513 return ClassifyTemporary(T);
514
515 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
516}
517
519 if (E->getType() == Ctx.UnknownAnyTy)
520 return (isa<FunctionDecl>(E->getMemberDecl())
522
523 // Handle C first, it's easier.
524 if (!Ctx.getLangOpts().CPlusPlus) {
525 // C99 6.5.2.3p3
526 // For dot access, the expression is an lvalue if the first part is. For
527 // arrow access, it always is an lvalue.
528 if (E->isArrow())
529 return Cl::CL_LValue;
530 // ObjC property accesses are not lvalues, but get special treatment.
531 Expr *Base = E->getBase()->IgnoreParens();
532 if (isa<ObjCPropertyRefExpr>(Base))
534 return ClassifyInternal(Ctx, Base);
535 }
536
538 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
539 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
540 // E1.E2 is an lvalue.
541 if (const auto *Value = dyn_cast<ValueDecl>(Member))
542 if (Value->getType()->isReferenceType())
543 return Cl::CL_LValue;
544
545 // Otherwise, one of the following rules applies.
546 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
547 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
548 return Cl::CL_LValue;
549
550 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
551 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
552 // otherwise, it is a prvalue.
553 if (isa<FieldDecl>(Member)) {
554 // *E1 is an lvalue
555 if (E->isArrow())
556 return Cl::CL_LValue;
558 if (isa<ObjCPropertyRefExpr>(Base))
560 return ClassifyInternal(Ctx, E->getBase());
561 }
562
563 // -- If E2 is a [...] member function, [...]
564 // -- If it refers to a static member function [...], then E1.E2 is an
565 // lvalue; [...]
566 // -- Otherwise [...] E1.E2 is a prvalue.
567 if (const auto *Method = dyn_cast<CXXMethodDecl>(Member)) {
568 if (Method->isStatic())
569 return Cl::CL_LValue;
570 if (Method->isImplicitObjectMemberFunction())
572 return Cl::CL_PRValue;
573 }
574
575 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
576 // So is everything else we haven't handled yet.
577 return Cl::CL_PRValue;
578}
579
581 assert(Ctx.getLangOpts().CPlusPlus &&
582 "This is only relevant for C++.");
583 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
584 // Except we override this for writes to ObjC properties.
585 if (E->isAssignmentOp())
586 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
588
589 // C++ [expr.comma]p1: the result is of the same value category as its right
590 // operand, [...].
591 if (E->getOpcode() == BO_Comma)
592 return ClassifyInternal(Ctx, E->getRHS());
593
594 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
595 // is a pointer to a data member is of the same value category as its first
596 // operand.
597 if (E->getOpcode() == BO_PtrMemD)
598 return (E->getType()->isFunctionType() ||
599 E->hasPlaceholderType(BuiltinType::BoundMember))
601 : ClassifyInternal(Ctx, E->getLHS());
602
603 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
604 // second operand is a pointer to data member and a prvalue otherwise.
605 if (E->getOpcode() == BO_PtrMemI)
606 return (E->getType()->isFunctionType() ||
607 E->hasPlaceholderType(BuiltinType::BoundMember))
610
611 // All other binary operations are prvalues.
612 return Cl::CL_PRValue;
613}
614
616 const Expr *False) {
617 assert(Ctx.getLangOpts().CPlusPlus &&
618 "This is only relevant for C++.");
619
620 // C++ [expr.cond]p2
621 // If either the second or the third operand has type (cv) void,
622 // one of the following shall hold:
623 if (True->getType()->isVoidType() || False->getType()->isVoidType()) {
624 // The second or the third operand (but not both) is a (possibly
625 // parenthesized) throw-expression; the result is of the [...] value
626 // category of the other.
627 bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts());
628 bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts());
629 if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False)
630 : (FalseIsThrow ? True : nullptr))
631 return ClassifyInternal(Ctx, NonThrow);
632
633 // [Otherwise] the result [...] is a prvalue.
634 return Cl::CL_PRValue;
635 }
636
637 // Note that at this point, we have already performed all conversions
638 // according to [expr.cond]p3.
639 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
640 // same value category [...], the result is of that [...] value category.
641 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
642 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
643 RCl = ClassifyInternal(Ctx, False);
644 return LCl == RCl ? LCl : Cl::CL_PRValue;
645}
646
648 Cl::Kinds Kind, SourceLocation &Loc) {
649 // As a general rule, we only care about lvalues. But there are some rvalues
650 // for which we want to generate special results.
651 if (Kind == Cl::CL_PRValue) {
652 // For the sake of better diagnostics, we want to specifically recognize
653 // use of the GCC cast-as-lvalue extension.
654 if (const auto *CE = dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
655 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
656 Loc = CE->getExprLoc();
657 return Cl::CM_LValueCast;
658 }
659 }
660 }
661 if (Kind != Cl::CL_LValue)
662 return Cl::CM_RValue;
663
664 // This is the lvalue case.
665 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
666 if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType())
667 return Cl::CM_Function;
668
669 // Assignment to a property in ObjC is an implicit setter access. But a
670 // setter might not exist.
671 if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
672 if (Expr->isImplicitProperty() &&
673 Expr->getImplicitPropertySetter() == nullptr)
675 }
676
677 CanQualType CT = Ctx.getCanonicalType(E->getType());
678 // Const stuff is obviously not modifiable.
679 if (CT.isConstQualified())
681 if (Ctx.getLangOpts().OpenCL &&
684
685 // Arrays are not modifiable, only their elements are.
686 if (CT->isArrayType())
687 return Cl::CM_ArrayType;
688 // Incomplete types are not modifiable.
689 if (CT->isIncompleteType())
691
692 // Records with any const fields (recursively) are not modifiable.
693 if (const RecordType *R = CT->getAs<RecordType>())
694 if (R->hasConstFields())
696
697 return Cl::CM_Modifiable;
698}
699
701 Classification VC = Classify(Ctx);
702 switch (VC.getKind()) {
703 case Cl::CL_LValue: return LV_Valid;
715 }
716 llvm_unreachable("Unhandled kind");
717}
718
721 SourceLocation dummy;
722 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
723 switch (VC.getKind()) {
724 case Cl::CL_LValue: break;
735 case Cl::CL_PRValue:
736 return VC.getModifiable() == Cl::CM_LValueCast ?
738 }
739 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
740 switch (VC.getModifiable()) {
741 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
742 case Cl::CM_Modifiable: return MLV_Valid;
743 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
746 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
751 case Cl::CM_ArrayType: return MLV_ArrayType;
753 }
754 llvm_unreachable("Unhandled modifiable type");
755}
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
Defines the clang::Expr interface and subclasses for C++ expressions.
static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T)
ClassifyUnnamed - Return the classification of an expression yielding an unnamed value of the given t...
static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *trueExpr, const Expr *falseExpr)
static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D)
ClassifyDecl - Return the classification of an expression referencing the given declaration.
static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E)
static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E)
static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang, const Expr *E, ExprValueKind Kind)
static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, Cl::Kinds Kind, SourceLocation &Loc)
static Cl::Kinds ClassifyTemporary(QualType T)
Classify an expression which creates a temporary, based on its type.
static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2574
const LangOptions & getLangOpts() const
Definition: ASTContext.h:775
CanQualType OverloadTy
Definition: ASTContext.h:1119
CanQualType UnknownAnyTy
Definition: ASTContext.h:1119
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3840
Expr * getLHS() const
Definition: Expr.h:3889
Expr * getRHS() const
Definition: Expr.h:3891
static bool isAssignmentOp(Opcode Opc)
Definition: Expr.h:3975
Opcode getOpcode() const
Definition: Expr.h:3884
A binding in a decomposition declaration.
Definition: DeclCXX.h:4107
bool isConstQualified() const
Qualifiers getQualifiers() const
Retrieve all qualifiers.
CanProxy< U > getAs() const
Retrieve a canonical type pointer with a different static type, upcasting or downcasting as needed.
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1606
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
The return type of classify().
Definition: Expr.h:330
ModifiableType
The results of modification testing.
Definition: Expr.h:348
ModifiableType getModifiable() const
Definition: Expr.h:376
Kinds getKind() const
Definition: Expr.h:375
Kinds
The various classification results. Most of these mean prvalue.
Definition: Expr.h:333
This represents one expression.
Definition: Expr.h:110
LValueClassification
Definition: Expr.h:282
@ LV_ArrayTemporary
Definition: Expr.h:292
@ LV_DuplicateVectorComponents
Definition: Expr.h:286
@ LV_ClassTemporary
Definition: Expr.h:291
@ LV_InvalidMessageExpression
Definition: Expr.h:288
@ LV_NotObjectType
Definition: Expr.h:284
@ LV_MemberFunction
Definition: Expr.h:289
@ LV_InvalidExpression
Definition: Expr.h:287
@ LV_IncompleteVoidType
Definition: Expr.h:285
@ LV_Valid
Definition: Expr.h:283
@ LV_SubObjCPropertySetting
Definition: Expr.h:290
Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const
ClassifyModifiable - Classify this expression according to the C++11 expression taxonomy,...
Definition: Expr.h:417
bool isXValue() const
Definition: Expr.h:279
isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc=nullptr) const
isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, does not have an incomplet...
LValueClassification ClassifyLValue(ASTContext &Ctx) const
Reasons why an expression might not be an l-value.
ExprValueKind getValueKind() const
getValueKind - The value kind that this expression produces.
Definition: Expr.h:437
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition: Expr.cpp:3059
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3055
bool isPRValue() const
Definition: Expr.h:278
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition: Expr.h:277
ExprObjectKind getObjectKind() const
getObjectKind - The object kind that this expression produces.
Definition: Expr.h:444
isModifiableLvalueResult
Definition: Expr.h:297
@ MLV_DuplicateVectorComponents
Definition: Expr.h:301
@ MLV_LValueCast
Definition: Expr.h:303
@ MLV_InvalidMessageExpression
Definition: Expr.h:312
@ MLV_ConstQualifiedField
Definition: Expr.h:306
@ MLV_InvalidExpression
Definition: Expr.h:302
@ MLV_IncompleteType
Definition: Expr.h:304
@ MLV_Valid
Definition: Expr.h:298
@ MLV_ConstQualified
Definition: Expr.h:305
@ MLV_NoSetterProperty
Definition: Expr.h:309
@ MLV_ArrayTemporary
Definition: Expr.h:314
@ MLV_SubObjCPropertySetting
Definition: Expr.h:311
@ MLV_ConstAddrSpace
Definition: Expr.h:307
@ MLV_MemberFunction
Definition: Expr.h:310
@ MLV_NotObjectType
Definition: Expr.h:299
@ MLV_ArrayType
Definition: Expr.h:308
@ MLV_ClassTemporary
Definition: Expr.h:313
@ MLV_IncompleteVoidType
Definition: Expr.h:300
Classification Classify(ASTContext &Ctx) const
Classify - Classify this expression according to the C++11 expression taxonomy.
Definition: Expr.h:405
QualType getType() const
Definition: Expr.h:142
bool hasPlaceholderType() const
Returns whether this expression has a placeholder type.
Definition: Expr.h:516
Represents a member of a struct/union/class.
Definition: Decl.h:3058
Represents a field injected from an anonymous union/struct into the parent scope.
Definition: Decl.h:3342
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:461
A global _GUID constant.
Definition: DeclCXX.h:4289
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3172
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:3255
Expr * getBase() const
Definition: Expr.h:3249
bool isArrow() const
Definition: Expr.h:3356
This represents a decl that may have a name.
Definition: Decl.h:249
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
A (possibly-)qualified type.
Definition: Type.h:940
bool hasQualifiers() const
Determine whether this type has any qualifiers.
Definition: Type.h:7448
LangAS getAddressSpace() const
Definition: Type.h:557
An rvalue reference type, per C++11 [dcl.ref].
Definition: Type.h:3442
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5549
Encodes a location in the source.
@ NoStmtClass
Definition: Stmt.h:87
StmtClass getStmtClass() const
Definition: Stmt.h:1358
A template parameter object.
bool isVoidType() const
Definition: Type.h:7905
bool isArrayType() const
Definition: Type.h:7678
bool isReferenceType() const
Definition: Type.h:7624
bool isLValueReferenceType() const
Definition: Type.h:7628
bool isFunctionType() const
Definition: Type.h:7608
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8123
bool isRecordType() const
Definition: Type.h:7706
An artificial decl, representing a global anonymous constant value which is uniquified by value withi...
Definition: DeclCXX.h:4346
QualType getType() const
Definition: Value.cpp:234
Represents a variable declaration or definition.
Definition: Decl.h:918
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition: Address.h:294
@ OK_ObjCProperty
An Objective-C property is a logical field of an Objective-C object which is read and written via Obj...
Definition: Specifiers.h:158
ExprValueKind
The categorization of expression values, currently following the C++11 scheme.
Definition: Specifiers.h:129
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition: Specifiers.h:132
@ VK_XValue
An x-value expression is a reference to an object with independent storage but which can be "moved",...
Definition: Specifiers.h:141
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition: Specifiers.h:136
const FunctionProtoType * T