clang 24.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)
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:
72 case Cl::CL_PRValue:
73 assert(isPRValue());
74 break;
75 }
76
78 if (Loc)
79 modifiable = IsModifiable(Ctx, this, kind, *Loc);
80 return Classification(kind, modifiable);
81}
82
83/// Classify an expression which creates a temporary, based on its type.
85 if (T->isRecordType())
87 if (T->isArrayType())
89
90 // No special classification: these don't behave differently from normal
91 // prvalues.
92 return Cl::CL_PRValue;
93}
94
96 const Expr *E,
97 ExprValueKind Kind) {
98 switch (Kind) {
99 case VK_PRValue:
100 return Lang.CPlusPlus ? ClassifyTemporary(E->getType()) : Cl::CL_PRValue;
101 case VK_LValue:
102 return Cl::CL_LValue;
103 case VK_XValue:
104 return Cl::CL_XValue;
105 }
106 llvm_unreachable("Invalid value category of implicit cast.");
107}
108
110 // This function takes the first stab at classifying expressions.
111 const LangOptions &Lang = Ctx.getLangOpts();
112
113 switch (E->getStmtClass()) {
115#define ABSTRACT_STMT(Kind)
116#define STMT(Kind, Base) case Expr::Kind##Class:
117#define EXPR(Kind, Base)
118#include "clang/AST/StmtNodes.inc"
119 llvm_unreachable("cannot classify a statement");
120
121 // First come the expressions that are always lvalues, unconditionally.
122 case Expr::ObjCIsaExprClass:
123 // Property references are lvalues
124 case Expr::ObjCSubscriptRefExprClass:
125 case Expr::ObjCPropertyRefExprClass:
126 // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
127 case Expr::CXXTypeidExprClass:
128 case Expr::CXXUuidofExprClass:
129 // Unresolved lookups and uncorrected typos get classified as lvalues.
130 // FIXME: Is this wise? Should they get their own kind?
131 case Expr::UnresolvedLookupExprClass:
132 case Expr::UnresolvedMemberExprClass:
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(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 case Expr::CXXReflectExprClass:
221 case Expr::CXXExpansionSelectExprClass:
222 return Cl::CL_PRValue;
223
224 case Expr::EmbedExprClass:
225 // Nominally, this just goes through as a PRValue until we actually expand
226 // it and check it.
227 return Cl::CL_PRValue;
228
229 // Make HLSL this reference-like
230 case Expr::CXXThisExprClass:
231 return Lang.HLSL ? Cl::CL_LValue : Cl::CL_PRValue;
232
233 case Expr::ConstantExprClass:
234 return ClassifyInternal(Ctx, cast<ConstantExpr>(E)->getSubExpr());
235
236 // Next come the complicated cases.
237 case Expr::SubstNonTypeTemplateParmExprClass:
238 return ClassifyInternal(Ctx,
239 cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
240
241 case Expr::PackIndexingExprClass: {
242 // A pack-index-expression always expands to an id-expression.
243 // Consider it as an LValue expression.
244 if (cast<PackIndexingExpr>(E)->isInstantiationDependent())
245 return Cl::CL_LValue;
246 return ClassifyInternal(Ctx, cast<PackIndexingExpr>(E)->getSelectedExpr());
247 }
248
249 // C, C++98 [expr.sub]p1: The result is an lvalue of type "T".
250 // C++11 (DR1213): in the case of an array operand, the result is an lvalue
251 // if that operand is an lvalue and an xvalue otherwise.
252 // Subscripting vector types is more like member access.
253 case Expr::ArraySubscriptExprClass:
254 if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
255 return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
256 if (Lang.CPlusPlus11) {
257 // Step over the array-to-pointer decay if present, but not over the
258 // temporary materialization.
259 auto *Base = cast<ArraySubscriptExpr>(E)->getBase()->IgnoreImpCasts();
260 if (Base->getType()->isArrayType())
261 return ClassifyInternal(Ctx, Base);
262 }
263 return Cl::CL_LValue;
264
265 case Expr::MatrixSingleSubscriptExprClass:
266 return ClassifyInternal(Ctx, cast<MatrixSingleSubscriptExpr>(E)->getBase());
267
268 // Subscripting matrix types behaves like member accesses.
269 case Expr::MatrixSubscriptExprClass:
270 return ClassifyInternal(Ctx, cast<MatrixSubscriptExpr>(E)->getBase());
271
272 // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
273 // function or variable and a prvalue otherwise.
274 case Expr::DeclRefExprClass:
275 if (E->getType() == Ctx.UnknownAnyTy)
276 return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
278 return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
279
280 // Member access is complex.
281 case Expr::MemberExprClass:
282 return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
283
284 case Expr::UnaryOperatorClass:
285 switch (cast<UnaryOperator>(E)->getOpcode()) {
286 // C++ [expr.unary.op]p1: The unary * operator performs indirection:
287 // [...] the result is an lvalue referring to the object or function
288 // to which the expression points.
289 case UO_Deref:
290 return Cl::CL_LValue;
291
292 // GNU extensions, simply look through them.
293 case UO_Extension:
294 return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
295
296 // Treat _Real and _Imag basically as if they were member
297 // expressions: l-value only if the operand is a true l-value.
298 case UO_Real:
299 case UO_Imag: {
300 const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
301 Cl::Kinds K = ClassifyInternal(Ctx, Op);
302 if (K != Cl::CL_LValue) return K;
303
306 return Cl::CL_LValue;
307 }
308
309 // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
310 // lvalue, [...]
311 // Not so in C.
312 case UO_PreInc:
313 case UO_PreDec:
314 return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
315
316 default:
317 return Cl::CL_PRValue;
318 }
319
320 case Expr::RecoveryExprClass:
321 case Expr::OpaqueValueExprClass:
322 return ClassifyExprValueKind(Lang, E, E->getValueKind());
323
324 // Pseudo-object expressions can produce l-values with reference magic.
325 case Expr::PseudoObjectExprClass:
326 return ClassifyExprValueKind(Lang, E,
327 cast<PseudoObjectExpr>(E)->getValueKind());
328
329 // Implicit casts are lvalues if they're lvalue casts. Other than that, we
330 // only specifically record class temporaries.
331 case Expr::ImplicitCastExprClass:
332 return ClassifyExprValueKind(Lang, E, E->getValueKind());
333
334 // C++ [expr.prim.general]p4: The presence of parentheses does not affect
335 // whether the expression is an lvalue.
336 case Expr::ParenExprClass:
337 return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
338
339 // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
340 // or a void expression if its result expression is, respectively, an
341 // lvalue, a function designator, or a void expression.
342 case Expr::GenericSelectionExprClass:
343 if (cast<GenericSelectionExpr>(E)->isResultDependent())
344 return Cl::CL_PRValue;
345 return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
346
347 case Expr::BinaryOperatorClass:
348 case Expr::CompoundAssignOperatorClass:
349 // C doesn't have any binary expressions that are lvalues.
350 if (Lang.CPlusPlus)
352 return Cl::CL_PRValue;
353
354 case Expr::CallExprClass:
355 case Expr::CXXOperatorCallExprClass:
356 case Expr::CXXMemberCallExprClass:
357 case Expr::UserDefinedLiteralClass:
358 case Expr::CUDAKernelCallExprClass:
359 return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx));
360
361 case Expr::CXXRewrittenBinaryOperatorClass:
362 return ClassifyInternal(
363 Ctx, cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm());
364
365 // __builtin_choose_expr is equivalent to the chosen expression.
366 case Expr::ChooseExprClass:
367 return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr());
368
369 // Extended vector element access is an lvalue unless there are duplicates
370 // in the shuffle expression.
371 case Expr::ExtVectorElementExprClass:
372 if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements())
374 if (cast<ExtVectorElementExpr>(E)->isArrow())
375 return Cl::CL_LValue;
376 return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase());
377
378 // Matrix element access is an lvalue unless there are duplicates
379 // in the shuffle expression.
380 case Expr::MatrixElementExprClass:
381 if (cast<MatrixElementExpr>(E)->containsDuplicateElements())
383 // NOTE: MatrixElementExpr is currently only used by HLSL which does not
384 // have pointers so there is no isArrow() necessary or way to test
385 // Cl::CL_LValue
386 return ClassifyInternal(Ctx, cast<MatrixElementExpr>(E)->getBase());
387
388 // Simply look at the actual default argument.
389 case Expr::CXXDefaultArgExprClass:
391
392 // Same idea for default initializers.
393 case Expr::CXXDefaultInitExprClass:
395
396 // Same idea for temporary binding.
397 case Expr::CXXBindTemporaryExprClass:
398 return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
399
400 // And the cleanups guard.
401 case Expr::ExprWithCleanupsClass:
402 return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
403
404 // Casts depend completely on the target type. All casts work the same.
405 case Expr::CStyleCastExprClass:
406 case Expr::CXXFunctionalCastExprClass:
407 case Expr::CXXStaticCastExprClass:
408 case Expr::CXXDynamicCastExprClass:
409 case Expr::CXXReinterpretCastExprClass:
410 case Expr::CXXConstCastExprClass:
411 case Expr::CXXAddrspaceCastExprClass:
412 case Expr::ObjCBridgedCastExprClass:
413 case Expr::BuiltinBitCastExprClass:
414 // Only in C++ can casts be interesting at all.
415 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
416 return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
417
418 case Expr::CXXUnresolvedConstructExprClass:
419 return ClassifyUnnamed(Ctx,
420 cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
421
422 case Expr::BinaryConditionalOperatorClass: {
423 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
424 const auto *co = cast<BinaryConditionalOperator>(E);
425 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
426 }
427
428 case Expr::ConditionalOperatorClass: {
429 // Once again, only C++ is interesting.
430 if (!Lang.CPlusPlus) return Cl::CL_PRValue;
431 const auto *co = cast<ConditionalOperator>(E);
432 return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
433 }
434
435 // ObjC message sends are effectively function calls, if the target function
436 // is known.
437 case Expr::ObjCMessageExprClass:
438 if (const ObjCMethodDecl *Method =
439 cast<ObjCMessageExpr>(E)->getMethodDecl()) {
440 Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType());
442 }
443 return Cl::CL_PRValue;
444
445 // Some C++ expressions are always class temporaries.
446 case Expr::CXXConstructExprClass:
447 case Expr::CXXInheritedCtorInitExprClass:
448 case Expr::CXXTemporaryObjectExprClass:
449 case Expr::LambdaExprClass:
450 case Expr::CXXStdInitializerListExprClass:
452
453 case Expr::VAArgExprClass:
454 return ClassifyUnnamed(Ctx, E->getType());
455
456 case Expr::DesignatedInitExprClass:
457 return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
458
459 case Expr::StmtExprClass: {
460 const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
461 if (const auto *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
462 return ClassifyUnnamed(Ctx, LastExpr->getType());
463 return Cl::CL_PRValue;
464 }
465
466 case Expr::PackExpansionExprClass:
467 return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
468
469 case Expr::MaterializeTemporaryExprClass:
470 return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
473
474 case Expr::InitListExprClass:
475 // An init list can be an lvalue if it is bound to a reference and
476 // contains only one element. In that case, we look at that element
477 // for an exact classification. Init list creation takes care of the
478 // value kind for us, so we only need to fine-tune.
479 if (E->isPRValue())
480 return ClassifyExprValueKind(Lang, E, E->getValueKind());
481 assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
482 "Only 1-element init lists can be glvalues.");
483 return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
484
485 case Expr::CoawaitExprClass:
486 case Expr::CoyieldExprClass:
487 return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr());
488 case Expr::SYCLUniqueStableNameExprClass:
489 case Expr::OpenACCAsteriskSizeExprClass:
490 return Cl::CL_PRValue;
491 break;
492
493 case Expr::CXXParenListInitExprClass:
494 if (isa<ArrayType>(E->getType()))
497 }
498
499 llvm_unreachable("unhandled expression kind in classification");
500}
501
502/// ClassifyDecl - Return the classification of an expression referencing the
503/// given declaration.
504static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
505 // C++ [expr.prim.id.unqual]p3: The result is an lvalue if the entity is a
506 // function, variable, or data member, or a template parameter object and a
507 // prvalue otherwise.
508 // In C, functions are not lvalues.
509 // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
510 // lvalue unless it's a reference type or a class type (C++ [temp.param]p8),
511 // so we need to special-case this.
512
513 if (const auto *M = dyn_cast<CXXMethodDecl>(D)) {
514 if (M->isImplicitObjectMemberFunction())
516 if (M->isStatic())
517 return Cl::CL_LValue;
518 return Cl::CL_PRValue;
519 }
520
521 bool islvalue;
522 if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(D))
523 islvalue = NTTParm->getType()->isReferenceType() ||
524 NTTParm->getType()->isRecordType();
525 else
526 islvalue =
529 (Ctx.getLangOpts().CPlusPlus &&
531
532 return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
533}
534
535/// ClassifyUnnamed - Return the classification of an expression yielding an
536/// unnamed value of the given type. This applies in particular to function
537/// calls and casts.
539 // In C, function calls are always rvalues.
540 if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue;
541
542 // C++ [expr.call]p10: A function call is an lvalue if the result type is an
543 // lvalue reference type or an rvalue reference to function type, an xvalue
544 // if the result type is an rvalue reference to object type, and a prvalue
545 // otherwise.
546 if (T->isLValueReferenceType())
547 return Cl::CL_LValue;
548 const auto *RV = T->getAs<RValueReferenceType>();
549 if (!RV) // Could still be a class temporary, though.
550 return ClassifyTemporary(T);
551
552 return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
553}
554
556 if (E->getType() == Ctx.UnknownAnyTy)
557 return (isa<FunctionDecl>(E->getMemberDecl())
559
560 // Handle C first, it's easier.
561 if (!Ctx.getLangOpts().CPlusPlus) {
562 // C99 6.5.2.3p3
563 // For dot access, the expression is an lvalue if the first part is. For
564 // arrow access, it always is an lvalue.
565 if (E->isArrow())
566 return Cl::CL_LValue;
567 // ObjC property accesses are not lvalues, but get special treatment.
568 Expr *Base = E->getBase()->IgnoreParens();
571 return ClassifyInternal(Ctx, Base);
572 }
573
575 // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
576 // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
577 // E1.E2 is an lvalue.
578 if (const auto *Value = dyn_cast<ValueDecl>(Member))
579 if (Value->getType()->isReferenceType())
580 return Cl::CL_LValue;
581
582 // Otherwise, one of the following rules applies.
583 // -- If E2 is a static member [...] then E1.E2 is an lvalue.
584 if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
585 return Cl::CL_LValue;
586
587 // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
588 // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
589 // otherwise, it is a prvalue.
590 if (isa<FieldDecl>(Member)) {
591 // *E1 is an lvalue
592 if (E->isArrow())
593 return Cl::CL_LValue;
597 return ClassifyInternal(Ctx, E->getBase());
598 }
599
600 // -- If E2 is a [...] member function, [...]
601 // -- If it refers to a static member function [...], then E1.E2 is an
602 // lvalue; [...]
603 // -- Otherwise [...] E1.E2 is a prvalue.
604 if (const auto *Method = dyn_cast<CXXMethodDecl>(Member)) {
605 if (Method->isStatic())
606 return Cl::CL_LValue;
607 if (Method->isImplicitObjectMemberFunction())
609 return Cl::CL_PRValue;
610 }
611
612 // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
613 // So is everything else we haven't handled yet.
614 return Cl::CL_PRValue;
615}
616
618 assert(Ctx.getLangOpts().CPlusPlus &&
619 "This is only relevant for C++.");
620
621 // For binary operators which are unknown due to type dependence, use the
622 // value kind assigned when the expression was created. Dependent assignment
623 // expressions can be either lvalues or prvalues depending on whether they
624 // might resolve to an overloaded operator.
625 if (E->getType() == Ctx.DependentTy)
626 return ClassifyExprValueKind(Ctx.getLangOpts(), E, E->getValueKind());
627
628 // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
629 // Except we override this for writes to ObjC properties.
630 if (E->isAssignmentOp())
631 return (E->getLHS()->getObjectKind() == OK_ObjCProperty
633
634 // C++ [expr.comma]p1: the result is of the same value category as its right
635 // operand, [...].
636 if (E->getOpcode() == BO_Comma)
637 return ClassifyInternal(Ctx, E->getRHS());
638
639 // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
640 // is a pointer to a data member is of the same value category as its first
641 // operand.
642 if (E->getOpcode() == BO_PtrMemD)
643 return (E->getType()->isFunctionType() ||
644 E->hasPlaceholderType(BuiltinType::BoundMember))
646 : ClassifyInternal(Ctx, E->getLHS());
647
648 // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
649 // second operand is a pointer to data member and a prvalue otherwise.
650 if (E->getOpcode() == BO_PtrMemI)
651 return (E->getType()->isFunctionType() ||
652 E->hasPlaceholderType(BuiltinType::BoundMember))
655
656 // All other binary operations are prvalues.
657 return Cl::CL_PRValue;
658}
659
661 const Expr *False) {
662 assert(Ctx.getLangOpts().CPlusPlus &&
663 "This is only relevant for C++.");
664
665 // C++ [expr.cond]p2
666 // If either the second or the third operand has type (cv) void,
667 // one of the following shall hold:
668 if (True->getType()->isVoidType() || False->getType()->isVoidType()) {
669 // The second or the third operand (but not both) is a (possibly
670 // parenthesized) throw-expression; the result is of the [...] value
671 // category of the other.
672 bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts());
673 bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts());
674 if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False)
675 : (FalseIsThrow ? True : nullptr))
676 return ClassifyInternal(Ctx, NonThrow);
677
678 // [Otherwise] the result [...] is a prvalue.
679 return Cl::CL_PRValue;
680 }
681
682 // Note that at this point, we have already performed all conversions
683 // according to [expr.cond]p3.
684 // C++ [expr.cond]p4: If the second and third operands are glvalues of the
685 // same value category [...], the result is of that [...] value category.
686 // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
687 Cl::Kinds LCl = ClassifyInternal(Ctx, True),
688 RCl = ClassifyInternal(Ctx, False);
689 return LCl == RCl ? LCl : Cl::CL_PRValue;
690}
691
693 Cl::Kinds Kind, SourceLocation &Loc) {
694 // As a general rule, we only care about lvalues. But there are some rvalues
695 // for which we want to generate special results.
696 if (Kind == Cl::CL_PRValue) {
697 // For the sake of better diagnostics, we want to specifically recognize
698 // use of the GCC cast-as-lvalue extension.
699 if (const auto *CE = dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
700 if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
701 Loc = CE->getExprLoc();
702 return Cl::CM_LValueCast;
703 }
704 }
705 }
706 if (Kind != Cl::CL_LValue)
707 return Cl::CM_RValue;
708
709 // This is the lvalue case.
710 // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
711 if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType())
712 return Cl::CM_Function;
713
714 // Assignment to a property in ObjC is an implicit setter access. But a
715 // setter might not exist.
716 if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
717 if (Expr->isImplicitProperty() &&
718 Expr->getImplicitPropertySetter() == nullptr)
720 }
721
722 CanQualType CT = Ctx.getCanonicalType(E->getType());
723 // Const stuff is obviously not modifiable.
724 if (CT.isConstQualified())
726 if (Ctx.getLangOpts().OpenCL &&
729
730 // Arrays are not modifiable, only their elements are.
731 if (CT->isArrayType() &&
732 !(Ctx.getLangOpts().HLSL && CT->isConstantArrayType()))
733 return Cl::CM_ArrayType;
734 // Incomplete types are not modifiable.
735 if (CT->isIncompleteType())
737
738 // Records with any const fields (recursively) are not modifiable.
739 if (const RecordType *R = CT->getAs<RecordType>())
740 if (R->hasConstFields())
742
743 return Cl::CM_Modifiable;
744}
745
766
769 SourceLocation dummy;
770 Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
771 switch (VC.getKind()) {
772 case Cl::CL_LValue: break;
785 case Cl::CL_PRValue:
786 return VC.getModifiable() == Cl::CM_LValueCast ?
788 }
789 assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
790 switch (VC.getModifiable()) {
791 case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
792 case Cl::CM_Modifiable: return MLV_Valid;
793 case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
796 llvm_unreachable("CM_LValueCast and CL_LValue don't match");
801 case Cl::CM_ArrayType: return MLV_ArrayType;
803 }
804 llvm_unreachable("Unhandled modifiable type");
805}
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.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
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:223
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:965
CanQualType OverloadTy
CanQualType UnknownAnyTy
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4044
Expr * getLHS() const
Definition Expr.h:4094
Expr * getRHS() const
Definition Expr.h:4096
static bool isAssignmentOp(Opcode Opc)
Definition Expr.h:4180
Opcode getOpcode() const
Definition Expr.h:4089
A binding in a decomposition declaration.
Definition DeclCXX.h:4206
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:1750
Stmt * body_back()
Definition Stmt.h:1818
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
The return type of classify().
Definition Expr.h:339
ModifiableType
The results of modification testing.
Definition Expr.h:358
ModifiableType getModifiable() const
Definition Expr.h:386
Kinds getKind() const
Definition Expr.h:385
Kinds
The various classification results. Most of these mean prvalue.
Definition Expr.h:342
This represents one expression.
Definition Expr.h:112
LValueClassification
Definition Expr.h:289
@ LV_DuplicateMatrixComponents
Definition Expr.h:294
@ LV_ArrayTemporary
Definition Expr.h:300
@ LV_DuplicateVectorComponents
Definition Expr.h:293
@ LV_ClassTemporary
Definition Expr.h:299
@ LV_InvalidMessageExpression
Definition Expr.h:296
@ LV_NotObjectType
Definition Expr.h:291
@ LV_MemberFunction
Definition Expr.h:297
@ LV_InvalidExpression
Definition Expr.h:295
@ LV_IncompleteVoidType
Definition Expr.h:292
@ LV_Valid
Definition Expr.h:290
@ LV_SubObjCPropertySetting
Definition Expr.h:298
Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const
ClassifyModifiable - Classify this expression according to the C++11 expression taxonomy,...
Definition Expr.h:427
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:447
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition Expr.cpp:3099
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3095
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:454
isModifiableLvalueResult
Definition Expr.h:305
@ MLV_DuplicateVectorComponents
Definition Expr.h:309
@ MLV_LValueCast
Definition Expr.h:312
@ MLV_InvalidMessageExpression
Definition Expr.h:321
@ MLV_DuplicateMatrixComponents
Definition Expr.h:310
@ MLV_ConstQualifiedField
Definition Expr.h:315
@ MLV_InvalidExpression
Definition Expr.h:311
@ MLV_IncompleteType
Definition Expr.h:313
@ MLV_Valid
Definition Expr.h:306
@ MLV_ConstQualified
Definition Expr.h:314
@ MLV_NoSetterProperty
Definition Expr.h:318
@ MLV_ArrayTemporary
Definition Expr.h:323
@ MLV_SubObjCPropertySetting
Definition Expr.h:320
@ MLV_ConstAddrSpace
Definition Expr.h:316
@ MLV_MemberFunction
Definition Expr.h:319
@ MLV_NotObjectType
Definition Expr.h:307
@ MLV_ArrayType
Definition Expr.h:317
@ MLV_ClassTemporary
Definition Expr.h:322
@ MLV_IncompleteVoidType
Definition Expr.h:308
Classification Classify(ASTContext &Ctx) const
Classify - Classify this expression according to the C++11 expression taxonomy.
Definition Expr.h:415
QualType getType() const
Definition Expr.h:144
bool hasPlaceholderType() const
Returns whether this expression has a placeholder type.
Definition Expr.h:526
Represents a member of a struct/union/class.
Definition Decl.h:3204
Represents a field injected from an anonymous union/struct into the parent scope.
Definition Decl.h:3511
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:4424
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3370
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3453
Expr * getBase() const
Definition Expr.h:3447
bool isArrow() const
Definition Expr.h:3554
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:8536
LangAS getAddressSpace() const
Definition TypeBase.h:571
An rvalue reference type, per C++11 [dcl.ref].
Definition TypeBase.h:3699
Encodes a location in the source.
@ NoStmtClass
Definition Stmt.h:89
StmtClass getStmtClass() const
Definition Stmt.h:1503
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1805
A template parameter object.
bool isVoidType() const
Definition TypeBase.h:9050
bool isReferenceType() const
Definition TypeBase.h:8708
bool isFunctionType() const
Definition TypeBase.h:8680
An artificial decl, representing a global anonymous constant value which is uniquified by value withi...
Definition DeclCXX.h:4481
QualType getType() const
Definition Value.cpp:238
Represents a variable declaration or definition.
Definition Decl.h:932
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:162
ExprValueKind
The categorization of expression values, currently following the C++11 scheme.
Definition Specifiers.h:133
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition Specifiers.h:136
@ VK_XValue
An x-value expression is a reference to an object with independent storage but which can be "moved",...
Definition Specifiers.h:145
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition Specifiers.h:140
U cast(CodeGen::Address addr)
Definition Address.h:327