clang 22.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 // 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::DependentCoawaitExprClass:
133 case Expr::CXXDependentScopeMemberExprClass:
134 case Expr::DependentScopeDeclRefExprClass:
135 // ObjC instance variables are lvalues
136 // FIXME: ObjC++0x might have different rules
137 case Expr::ObjCIvarRefExprClass:
138 case Expr::FunctionParmPackExprClass:
139 case Expr::MSPropertyRefExprClass:
140 case Expr::MSPropertySubscriptExprClass:
141 case Expr::ArraySectionExprClass:
142 case Expr::OMPArrayShapingExprClass:
143 case Expr::OMPIteratorExprClass:
144 case Expr::HLSLOutArgExprClass:
145 return Cl::CL_LValue;
146
147 // C++ [expr.prim.general]p1: A string literal is an lvalue.
148 case Expr::StringLiteralClass:
149 // @encode is equivalent to its string
150 case Expr::ObjCEncodeExprClass:
151 // Except we special case them as prvalues when they are used to
152 // initialize a char array.
153 return E->isLValue() ? Cl::CL_LValue : Cl::CL_PRValue;
154
155 // __func__ and friends are too.
156 // The char array initialization special case also applies
157 // when they are transparent.
158 case Expr::PredefinedExprClass: {
159 auto *PE = cast<PredefinedExpr>(E);
160 const StringLiteral *SL = PE->getFunctionName();
161 if (PE->isTransparent())
162 return SL ? ClassifyInternal(Ctx, SL) : Cl::CL_LValue;
163 assert(!SL || SL->isLValue());
164 return Cl::CL_LValue;
165 }
166
167 // C99 6.5.2.5p5 says that compound literals are lvalues.
168 // In C++, they're prvalue temporaries, except for file-scope arrays.
169 case Expr::CompoundLiteralExprClass:
170 return !E->isLValue() ? ClassifyTemporary(E->getType()) : Cl::CL_LValue;
171
172 // Expressions that are prvalues.
173 case Expr::CXXBoolLiteralExprClass:
174 case Expr::CXXPseudoDestructorExprClass:
175 case Expr::UnaryExprOrTypeTraitExprClass:
176 case Expr::CXXNewExprClass:
177 case Expr::CXXNullPtrLiteralExprClass:
178 case Expr::ImaginaryLiteralClass:
179 case Expr::GNUNullExprClass:
180 case Expr::OffsetOfExprClass:
181 case Expr::CXXThrowExprClass:
182 case Expr::ShuffleVectorExprClass:
183 case Expr::ConvertVectorExprClass:
184 case Expr::IntegerLiteralClass:
185 case Expr::FixedPointLiteralClass:
186 case Expr::CharacterLiteralClass:
187 case Expr::AddrLabelExprClass:
188 case Expr::CXXDeleteExprClass:
189 case Expr::ImplicitValueInitExprClass:
190 case Expr::BlockExprClass:
191 case Expr::FloatingLiteralClass:
192 case Expr::CXXNoexceptExprClass:
193 case Expr::CXXScalarValueInitExprClass:
194 case Expr::TypeTraitExprClass:
195 case Expr::ArrayTypeTraitExprClass:
196 case Expr::ExpressionTraitExprClass:
197 case Expr::ObjCSelectorExprClass:
198 case Expr::ObjCProtocolExprClass:
199 case Expr::ObjCStringLiteralClass:
200 case Expr::ObjCBoxedExprClass:
201 case Expr::ObjCArrayLiteralClass:
202 case Expr::ObjCDictionaryLiteralClass:
203 case Expr::ObjCBoolLiteralExprClass:
204 case Expr::ObjCAvailabilityCheckExprClass:
205 case Expr::ParenListExprClass:
206 case Expr::SizeOfPackExprClass:
207 case Expr::SubstNonTypeTemplateParmPackExprClass:
208 case Expr::AsTypeExprClass:
209 case Expr::ObjCIndirectCopyRestoreExprClass:
210 case Expr::AtomicExprClass:
211 case Expr::CXXFoldExprClass:
212 case Expr::ArrayInitLoopExprClass:
213 case Expr::ArrayInitIndexExprClass:
214 case Expr::NoInitExprClass:
215 case Expr::DesignatedInitUpdateExprClass:
216 case Expr::SourceLocExprClass:
217 case Expr::ConceptSpecializationExprClass:
218 case Expr::RequiresExprClass:
219 return Cl::CL_PRValue;
220
221 case Expr::EmbedExprClass:
222 // Nominally, this just goes through as a PRValue until we actually expand
223 // it and check it.
224 return Cl::CL_PRValue;
225
226 // Make HLSL this reference-like
227 case Expr::CXXThisExprClass:
228 return Lang.HLSL ? Cl::CL_LValue : Cl::CL_PRValue;
229
230 case Expr::ConstantExprClass:
231 return ClassifyInternal(Ctx, cast<ConstantExpr>(E)->getSubExpr());
232
233 // Next come the complicated cases.
234 case Expr::SubstNonTypeTemplateParmExprClass:
235 return ClassifyInternal(Ctx,
236 cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
237
238 case Expr::PackIndexingExprClass: {
239 // A pack-index-expression always expands to an id-expression.
240 // Consider it as an LValue expression.
241 if (cast<PackIndexingExpr>(E)->isInstantiationDependent())
242 return Cl::CL_LValue;
243 return ClassifyInternal(Ctx, cast<PackIndexingExpr>(E)->getSelectedExpr());
244 }
245
246 // C, C++98 [expr.sub]p1: The result is an lvalue of type "T".
247 // C++11 (DR1213): in the case of an array operand, the result is an lvalue
248 // if that operand is an lvalue and an xvalue otherwise.
249 // Subscripting vector types is more like member access.
250 case Expr::ArraySubscriptExprClass:
251 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
252 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
253 if (Lang.CPlusPlus11) {
254 // Step over the array-to-pointer decay if present, but not over the
255 // temporary materialization.
256 auto *Base = cast<ArraySubscriptExpr>(E)->getBase()->IgnoreImpCasts();
257 if (Base->getType()->isArrayType())
258 return ClassifyInternal(Ctx, Base);
259 }
260 return Cl::CL_LValue;
261
262 case Expr::MatrixSingleSubscriptExprClass:
263 return ClassifyInternal(Ctx, cast<MatrixSingleSubscriptExpr>(E)->getBase());
264
265 // Subscripting matrix types behaves like member accesses.
266 case Expr::MatrixSubscriptExprClass:
267 return ClassifyInternal(Ctx, cast<MatrixSubscriptExpr>(E)->getBase());
268
269 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
270 // function or variable and a prvalue otherwise.
271 case Expr::DeclRefExprClass:
272 if (E->getType() == Ctx.UnknownAnyTy)
273 return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
275 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
276
277 // Member access is complex.
278 case Expr::MemberExprClass:
279 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
280
281 case Expr::UnaryOperatorClass:
282 switch (cast<UnaryOperator>(E)->getOpcode()) {
283 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
284 // [...] the result is an lvalue referring to the object or function
285 // to which the expression points.
286 case UO_Deref:
287 return Cl::CL_LValue;
288
289 // GNU extensions, simply look through them.
290 case UO_Extension:
291 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
292
293 // Treat _Real and _Imag basically as if they were member
294 // expressions: l-value only if the operand is a true l-value.
295 case UO_Real:
296 case UO_Imag: {
297 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
298 Cl::Kinds K = ClassifyInternal(Ctx, Op);
299 if (K != Cl::CL_LValue) return K;
300
303 return Cl::CL_LValue;
304 }
305
306 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
307 // lvalue, [...]
308 // Not so in C.
309 case UO_PreInc:
310 case UO_PreDec:
311 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
312
313 default:
314 return Cl::CL_PRValue;
315 }
316
317 case Expr::RecoveryExprClass:
318 case Expr::OpaqueValueExprClass:
319 return ClassifyExprValueKind(Lang, E, E->getValueKind());
320
321 // Pseudo-object expressions can produce l-values with reference magic.
322 case Expr::PseudoObjectExprClass:
323 return ClassifyExprValueKind(Lang, E,
324 cast<PseudoObjectExpr>(E)->getValueKind());
325
326 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
327 // only specifically record class temporaries.
328 case Expr::ImplicitCastExprClass:
329 return ClassifyExprValueKind(Lang, E, E->getValueKind());
330
331 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
332 // whether the expression is an lvalue.
333 case Expr::ParenExprClass:
334 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
335
336 // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
337 // or a void expression if its result expression is, respectively, an
338 // lvalue, a function designator, or a void expression.
339 case Expr::GenericSelectionExprClass:
340 if (cast<GenericSelectionExpr>(E)->isResultDependent())
341 return Cl::CL_PRValue;
342 return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
343
344 case Expr::BinaryOperatorClass:
345 case Expr::CompoundAssignOperatorClass:
346 // C doesn't have any binary expressions that are lvalues.
347 if (Lang.CPlusPlus)
349 return Cl::CL_PRValue;
350
351 case Expr::CallExprClass:
352 case Expr::CXXOperatorCallExprClass:
353 case Expr::CXXMemberCallExprClass:
354 case Expr::UserDefinedLiteralClass:
355 case Expr::CUDAKernelCallExprClass:
356 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx));
357
358 case Expr::CXXRewrittenBinaryOperatorClass:
359 return ClassifyInternal(
360 Ctx, cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm());
361
362 // __builtin_choose_expr is equivalent to the chosen expression.
363 case Expr::ChooseExprClass:
364 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr());
365
366 // Extended vector element access is an lvalue unless there are duplicates
367 // in the shuffle expression.
368 case Expr::ExtVectorElementExprClass:
369 if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements())
371 if (cast<ExtVectorElementExpr>(E)->isArrow())
372 return Cl::CL_LValue;
373 return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase());
374
375 // Simply look at the actual default argument.
376 case Expr::CXXDefaultArgExprClass:
378
379 // Same idea for default initializers.
380 case Expr::CXXDefaultInitExprClass:
382
383 // Same idea for temporary binding.
384 case Expr::CXXBindTemporaryExprClass:
385 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
386
387 // And the cleanups guard.
388 case Expr::ExprWithCleanupsClass:
389 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
390
391 // Casts depend completely on the target type. All casts work the same.
392 case Expr::CStyleCastExprClass:
393 case Expr::CXXFunctionalCastExprClass:
394 case Expr::CXXStaticCastExprClass:
395 case Expr::CXXDynamicCastExprClass:
396 case Expr::CXXReinterpretCastExprClass:
397 case Expr::CXXConstCastExprClass:
398 case Expr::CXXAddrspaceCastExprClass:
399 case Expr::ObjCBridgedCastExprClass:
400 case Expr::BuiltinBitCastExprClass:
401 // Only in C++ can casts be interesting at all.
402 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
403 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
404
405 case Expr::CXXUnresolvedConstructExprClass:
406 return ClassifyUnnamed(Ctx,
407 cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
408
409 case Expr::BinaryConditionalOperatorClass: {
410 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
411 const auto *co = cast<BinaryConditionalOperator>(E);
412 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
413 }
414
415 case Expr::ConditionalOperatorClass: {
416 // Once again, only C++ is interesting.
417 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
418 const auto *co = cast<ConditionalOperator>(E);
419 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
420 }
421
422 // ObjC message sends are effectively function calls, if the target function
423 // is known.
424 case Expr::ObjCMessageExprClass:
425 if (const ObjCMethodDecl *Method =
426 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
427 Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType());
428 return (kind == Cl::CL_PRValue) ? Cl::CL_ObjCMessageRValue : kind;
429 }
430 return Cl::CL_PRValue;
431
432 // Some C++ expressions are always class temporaries.
433 case Expr::CXXConstructExprClass:
434 case Expr::CXXInheritedCtorInitExprClass:
435 case Expr::CXXTemporaryObjectExprClass:
436 case Expr::LambdaExprClass:
437 case Expr::CXXStdInitializerListExprClass:
439
440 case Expr::VAArgExprClass:
441 return ClassifyUnnamed(Ctx, E->getType());
442
443 case Expr::DesignatedInitExprClass:
444 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
445
446 case Expr::StmtExprClass: {
447 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
448 if (const auto *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
449 return ClassifyUnnamed(Ctx, LastExpr->getType());
450 return Cl::CL_PRValue;
451 }
452
453 case Expr::PackExpansionExprClass:
454 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
455
456 case Expr::MaterializeTemporaryExprClass:
457 return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
460
461 case Expr::InitListExprClass:
462 // An init list can be an lvalue if it is bound to a reference and
463 // contains only one element. In that case, we look at that element
464 // for an exact classification. Init list creation takes care of the
465 // value kind for us, so we only need to fine-tune.
466 if (E->isPRValue())
467 return ClassifyExprValueKind(Lang, E, E->getValueKind());
468 assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
469 "Only 1-element init lists can be glvalues.");
470 return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
471
472 case Expr::CoawaitExprClass:
473 case Expr::CoyieldExprClass:
474 return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr());
475 case Expr::SYCLUniqueStableNameExprClass:
476 case Expr::OpenACCAsteriskSizeExprClass:
477 return Cl::CL_PRValue;
478 break;
479
480 case Expr::CXXParenListInitExprClass:
481 if (isa<ArrayType>(E->getType()))
484 }
485
486 llvm_unreachable("unhandled expression kind in classification");
487}
488
489/// ClassifyDecl - Return the classification of an expression referencing the
490/// given declaration.
491static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
492 // C++ [expr.prim.id.unqual]p3: The result is an lvalue if the entity is a
493 // function, variable, or data member, or a template parameter object and a
494 // prvalue otherwise.
495 // In C, functions are not lvalues.
496 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
497 // lvalue unless it's a reference type or a class type (C++ [temp.param]p8),
498 // so we need to special-case this.
499
500 if (const auto *M = dyn_cast<CXXMethodDecl>(D)) {
501 if (M->isImplicitObjectMemberFunction())
503 if (M->isStatic())
504 return Cl::CL_LValue;
505 return Cl::CL_PRValue;
506 }
507
508 bool islvalue;
509 if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(D))
510 islvalue = NTTParm->getType()->isReferenceType() ||
511 NTTParm->getType()->isRecordType();
512 else
513 islvalue =
516 (Ctx.getLangOpts().CPlusPlus &&
518
519 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
520}
521
522/// ClassifyUnnamed - Return the classification of an expression yielding an
523/// unnamed value of the given type. This applies in particular to function
524/// calls and casts.
526 // In C, function calls are always rvalues.
527 if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue;
528
529 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
530 // lvalue reference type or an rvalue reference to function type, an xvalue
531 // if the result type is an rvalue reference to object type, and a prvalue
532 // otherwise.
533 if (T->isLValueReferenceType())
534 return Cl::CL_LValue;
535 const auto *RV = T->getAs<RValueReferenceType>();
536 if (!RV) // Could still be a class temporary, though.
537 return ClassifyTemporary(T);
538
539 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
540}
541
543 if (E->getType() == Ctx.UnknownAnyTy)
544 return (isa<FunctionDecl>(E->getMemberDecl())
546
547 // Handle C first, it's easier.
548 if (!Ctx.getLangOpts().CPlusPlus) {
549 // C99 6.5.2.3p3
550 // For dot access, the expression is an lvalue if the first part is. For
551 // arrow access, it always is an lvalue.
552 if (E->isArrow())
553 return Cl::CL_LValue;
554 // ObjC property accesses are not lvalues, but get special treatment.
555 Expr *Base = E->getBase()->IgnoreParens();
558 return ClassifyInternal(Ctx, Base);
559 }
560
562 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
563 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
564 // E1.E2 is an lvalue.
565 if (const auto *Value = dyn_cast<ValueDecl>(Member))
566 if (Value->getType()->isReferenceType())
567 return Cl::CL_LValue;
568
569 // Otherwise, one of the following rules applies.
570 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
571 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
572 return Cl::CL_LValue;
573
574 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
575 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
576 // otherwise, it is a prvalue.
577 if (isa<FieldDecl>(Member)) {
578 // *E1 is an lvalue
579 if (E->isArrow())
580 return Cl::CL_LValue;
584 return ClassifyInternal(Ctx, E->getBase());
585 }
586
587 // -- If E2 is a [...] member function, [...]
588 // -- If it refers to a static member function [...], then E1.E2 is an
589 // lvalue; [...]
590 // -- Otherwise [...] E1.E2 is a prvalue.
591 if (const auto *Method = dyn_cast<CXXMethodDecl>(Member)) {
592 if (Method->isStatic())
593 return Cl::CL_LValue;
594 if (Method->isImplicitObjectMemberFunction())
596 return Cl::CL_PRValue;
597 }
598
599 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
600 // So is everything else we haven't handled yet.
601 return Cl::CL_PRValue;
602}
603
605 assert(Ctx.getLangOpts().CPlusPlus &&
606 "This is only relevant for C++.");
607
608 // For binary operators which are unknown due to type dependence, the
609 // convention is to classify them as a prvalue. This does not matter much, but
610 // it needs to agree with how they are created.
611 if (E->getType() == Ctx.DependentTy)
612 return Cl::CL_PRValue;
613
614 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
615 // Except we override this for writes to ObjC properties.
616 if (E->isAssignmentOp())
617 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
619
620 // C++ [expr.comma]p1: the result is of the same value category as its right
621 // operand, [...].
622 if (E->getOpcode() == BO_Comma)
623 return ClassifyInternal(Ctx, E->getRHS());
624
625 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
626 // is a pointer to a data member is of the same value category as its first
627 // operand.
628 if (E->getOpcode() == BO_PtrMemD)
629 return (E->getType()->isFunctionType() ||
630 E->hasPlaceholderType(BuiltinType::BoundMember))
632 : ClassifyInternal(Ctx, E->getLHS());
633
634 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
635 // second operand is a pointer to data member and a prvalue otherwise.
636 if (E->getOpcode() == BO_PtrMemI)
637 return (E->getType()->isFunctionType() ||
638 E->hasPlaceholderType(BuiltinType::BoundMember))
641
642 // All other binary operations are prvalues.
643 return Cl::CL_PRValue;
644}
645
647 const Expr *False) {
648 assert(Ctx.getLangOpts().CPlusPlus &&
649 "This is only relevant for C++.");
650
651 // C++ [expr.cond]p2
652 // If either the second or the third operand has type (cv) void,
653 // one of the following shall hold:
654 if (True->getType()->isVoidType() || False->getType()->isVoidType()) {
655 // The second or the third operand (but not both) is a (possibly
656 // parenthesized) throw-expression; the result is of the [...] value
657 // category of the other.
658 bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts());
659 bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts());
660 if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False)
661 : (FalseIsThrow ? True : nullptr))
662 return ClassifyInternal(Ctx, NonThrow);
663
664 // [Otherwise] the result [...] is a prvalue.
665 return Cl::CL_PRValue;
666 }
667
668 // Note that at this point, we have already performed all conversions
669 // according to [expr.cond]p3.
670 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
671 // same value category [...], the result is of that [...] value category.
672 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
673 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
674 RCl = ClassifyInternal(Ctx, False);
675 return LCl == RCl ? LCl : Cl::CL_PRValue;
676}
677
679 Cl::Kinds Kind, SourceLocation &Loc) {
680 // As a general rule, we only care about lvalues. But there are some rvalues
681 // for which we want to generate special results.
682 if (Kind == Cl::CL_PRValue) {
683 // For the sake of better diagnostics, we want to specifically recognize
684 // use of the GCC cast-as-lvalue extension.
685 if (const auto *CE = dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
686 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
687 Loc = CE->getExprLoc();
688 return Cl::CM_LValueCast;
689 }
690 }
691 }
692 if (Kind != Cl::CL_LValue)
693 return Cl::CM_RValue;
694
695 // This is the lvalue case.
696 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
697 if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType())
698 return Cl::CM_Function;
699
700 // Assignment to a property in ObjC is an implicit setter access. But a
701 // setter might not exist.
702 if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
703 if (Expr->isImplicitProperty() &&
704 Expr->getImplicitPropertySetter() == nullptr)
706 }
707
708 CanQualType CT = Ctx.getCanonicalType(E->getType());
709 // Const stuff is obviously not modifiable.
710 if (CT.isConstQualified())
712 if (Ctx.getLangOpts().OpenCL &&
715
716 // Arrays are not modifiable, only their elements are.
717 if (CT->isArrayType() &&
718 !(Ctx.getLangOpts().HLSL && CT->isConstantArrayType()))
719 return Cl::CM_ArrayType;
720 // Incomplete types are not modifiable.
721 if (CT->isIncompleteType())
723
724 // Records with any const fields (recursively) are not modifiable.
725 if (const RecordType *R = CT->getAs<RecordType>())
726 if (R->hasConstFields())
728
729 return Cl::CM_Modifiable;
730}
731
750
753 SourceLocation dummy;
754 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
755 switch (VC.getKind()) {
756 case Cl::CL_LValue: break;
767 case Cl::CL_PRValue:
768 return VC.getModifiable() == Cl::CM_LValueCast ?
770 }
771 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
772 switch (VC.getModifiable()) {
773 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
774 case Cl::CM_Modifiable: return MLV_Valid;
775 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
778 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
783 case Cl::CM_ArrayType: return MLV_ArrayType;
785 }
786 llvm_unreachable("Unhandled modifiable type");
787}
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)
Expr::Classification Cl
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)
TokenType getType() const
Returns the token's type, e.g.
Expr * getExpr()
Get 'expr' part of the associated expression/statement.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
static CanQualType getCanonicalType(QualType T)
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
CanQualType DependentTy
const LangOptions & getLangOpts() const
Definition ASTContext.h:944
CanQualType OverloadTy
CanQualType UnknownAnyTy
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4038
Expr * getLHS() const
Definition Expr.h:4088
Expr * getRHS() const
Definition Expr.h:4090
static bool isAssignmentOp(Opcode Opc)
Definition Expr.h:4174
Opcode getOpcode() const
Definition Expr.h:4083
A binding in a decomposition declaration.
Definition DeclCXX.h:4181
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:1731
Stmt * body_back()
Definition Stmt.h:1799
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
The return type of classify().
Definition Expr.h:337
ModifiableType
The results of modification testing.
Definition Expr.h:355
ModifiableType getModifiable() const
Definition Expr.h:383
Kinds getKind() const
Definition Expr.h:382
Kinds
The various classification results. Most of these mean prvalue.
Definition Expr.h:340
This represents one expression.
Definition Expr.h:112
LValueClassification
Definition Expr.h:289
@ LV_ArrayTemporary
Definition Expr.h:299
@ LV_DuplicateVectorComponents
Definition Expr.h:293
@ LV_ClassTemporary
Definition Expr.h:298
@ LV_InvalidMessageExpression
Definition Expr.h:295
@ LV_NotObjectType
Definition Expr.h:291
@ LV_MemberFunction
Definition Expr.h:296
@ LV_InvalidExpression
Definition Expr.h:294
@ LV_IncompleteVoidType
Definition Expr.h:292
@ LV_Valid
Definition Expr.h:290
@ LV_SubObjCPropertySetting
Definition Expr.h:297
Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const
ClassifyModifiable - Classify this expression according to the C++11 expression taxonomy,...
Definition Expr.h:424
bool isXValue() const
Definition Expr.h:286
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:444
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition Expr.cpp:3089
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3085
bool isPRValue() const
Definition Expr.h:285
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition Expr.h:284
ExprObjectKind getObjectKind() const
getObjectKind - The object kind that this expression produces.
Definition Expr.h:451
isModifiableLvalueResult
Definition Expr.h:304
@ MLV_DuplicateVectorComponents
Definition Expr.h:308
@ MLV_LValueCast
Definition Expr.h:310
@ MLV_InvalidMessageExpression
Definition Expr.h:319
@ MLV_ConstQualifiedField
Definition Expr.h:313
@ MLV_InvalidExpression
Definition Expr.h:309
@ MLV_IncompleteType
Definition Expr.h:311
@ MLV_Valid
Definition Expr.h:305
@ MLV_ConstQualified
Definition Expr.h:312
@ MLV_NoSetterProperty
Definition Expr.h:316
@ MLV_ArrayTemporary
Definition Expr.h:321
@ MLV_SubObjCPropertySetting
Definition Expr.h:318
@ MLV_ConstAddrSpace
Definition Expr.h:314
@ MLV_MemberFunction
Definition Expr.h:317
@ MLV_NotObjectType
Definition Expr.h:306
@ MLV_ArrayType
Definition Expr.h:315
@ MLV_ClassTemporary
Definition Expr.h:320
@ MLV_IncompleteVoidType
Definition Expr.h:307
Classification Classify(ASTContext &Ctx) const
Classify - Classify this expression according to the C++11 expression taxonomy.
Definition Expr.h:412
QualType getType() const
Definition Expr.h:144
bool hasPlaceholderType() const
Returns whether this expression has a placeholder type.
Definition Expr.h:523
Represents a member of a struct/union/class.
Definition Decl.h:3160
Represents a field injected from an anonymous union/struct into the parent scope.
Definition Decl.h:3467
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
A global _GUID constant.
Definition DeclCXX.h:4394
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3364
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3447
Expr * getBase() const
Definition Expr.h:3441
bool isArrow() const
Definition Expr.h:3548
This represents a decl that may have a name.
Definition Decl.h:274
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
A (possibly-)qualified type.
Definition TypeBase.h:937
bool hasQualifiers() const
Determine whether this type has any qualifiers.
Definition TypeBase.h:8382
LangAS getAddressSpace() const
Definition TypeBase.h:571
An rvalue reference type, per C++11 [dcl.ref].
Definition TypeBase.h:3636
Encodes a location in the source.
@ NoStmtClass
Definition Stmt.h:88
StmtClass getStmtClass() const
Definition Stmt.h:1484
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1799
A template parameter object.
bool isVoidType() const
Definition TypeBase.h:8892
bool isReferenceType() const
Definition TypeBase.h:8554
bool isFunctionType() const
Definition TypeBase.h:8526
An artificial decl, representing a global anonymous constant value which is uniquified by value withi...
Definition DeclCXX.h:4451
QualType getType() const
Definition Value.cpp:237
Represents a variable declaration or definition.
Definition Decl.h:926
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ 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:161
const FunctionProtoType * T
ExprValueKind
The categorization of expression values, currently following the C++11 scheme.
Definition Specifiers.h:132
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition Specifiers.h:135
@ VK_XValue
An x-value expression is a reference to an object with independent storage but which can be "moved",...
Definition Specifiers.h:144
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition Specifiers.h:139
U cast(CodeGen::Address addr)
Definition Address.h:327