clang  14.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"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "clang/AST/ExprObjC.h"
20 #include "llvm/Support/ErrorHandling.h"
21 
22 using namespace clang;
23 
25 
26 static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E);
27 static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D);
29 static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E);
32  const Expr *trueExpr,
33  const Expr *falseExpr);
34 static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
36 
37 Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {
38  assert(!TR->isReferenceType() && "Expressions can't have reference type.");
39 
40  Cl::Kinds kind = ClassifyInternal(Ctx, 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:
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())
85  return Cl::CL_ClassTemporary;
86  if (T->isArrayType())
87  return Cl::CL_ArrayTemporary;
88 
89  // No special classification: these don't behave differently from normal
90  // prvalues.
91  return Cl::CL_PRValue;
92 }
93 
95  const Expr *E,
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 
108 static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
109  // This function takes the first stab at classifying expressions.
110  const LangOptions &Lang = Ctx.getLangOpts();
111 
112  switch (E->getStmtClass()) {
113  case Stmt::NoStmtClass:
114 #define ABSTRACT_STMT(Kind)
115 #define STMT(Kind, Base) case Expr::Kind##Class:
116 #define EXPR(Kind, Base)
117 #include "clang/AST/StmtNodes.inc"
118  llvm_unreachable("cannot classify a statement");
119 
120  // First come the expressions that are always lvalues, unconditionally.
121  case Expr::ObjCIsaExprClass:
122  // 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::OMPArraySectionExprClass:
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::CXXThisExprClass:
164  case Expr::CXXNullPtrLiteralExprClass:
165  case Expr::ImaginaryLiteralClass:
166  case Expr::GNUNullExprClass:
167  case Expr::OffsetOfExprClass:
168  case Expr::CXXThrowExprClass:
169  case Expr::ShuffleVectorExprClass:
170  case Expr::ConvertVectorExprClass:
171  case Expr::IntegerLiteralClass:
172  case Expr::FixedPointLiteralClass:
173  case Expr::CharacterLiteralClass:
174  case Expr::AddrLabelExprClass:
175  case Expr::CXXDeleteExprClass:
176  case Expr::ImplicitValueInitExprClass:
177  case Expr::BlockExprClass:
178  case Expr::FloatingLiteralClass:
179  case Expr::CXXNoexceptExprClass:
180  case Expr::CXXScalarValueInitExprClass:
181  case Expr::TypeTraitExprClass:
182  case Expr::ArrayTypeTraitExprClass:
183  case Expr::ExpressionTraitExprClass:
184  case Expr::ObjCSelectorExprClass:
185  case Expr::ObjCProtocolExprClass:
186  case Expr::ObjCStringLiteralClass:
187  case Expr::ObjCBoxedExprClass:
188  case Expr::ObjCArrayLiteralClass:
189  case Expr::ObjCDictionaryLiteralClass:
190  case Expr::ObjCBoolLiteralExprClass:
191  case Expr::ObjCAvailabilityCheckExprClass:
192  case Expr::ParenListExprClass:
193  case Expr::SizeOfPackExprClass:
194  case Expr::SubstNonTypeTemplateParmPackExprClass:
195  case Expr::AsTypeExprClass:
196  case Expr::ObjCIndirectCopyRestoreExprClass:
197  case Expr::AtomicExprClass:
198  case Expr::CXXFoldExprClass:
199  case Expr::ArrayInitLoopExprClass:
200  case Expr::ArrayInitIndexExprClass:
201  case Expr::NoInitExprClass:
202  case Expr::DesignatedInitUpdateExprClass:
203  case Expr::SourceLocExprClass:
204  case Expr::ConceptSpecializationExprClass:
205  case Expr::RequiresExprClass:
206  return Cl::CL_PRValue;
207 
208  case Expr::ConstantExprClass:
209  return ClassifyInternal(Ctx, cast<ConstantExpr>(E)->getSubExpr());
210 
211  // Next come the complicated cases.
212  case Expr::SubstNonTypeTemplateParmExprClass:
213  return ClassifyInternal(Ctx,
214  cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
215 
216  // C, C++98 [expr.sub]p1: The result is an lvalue of type "T".
217  // C++11 (DR1213): in the case of an array operand, the result is an lvalue
218  // if that operand is an lvalue and an xvalue otherwise.
219  // Subscripting vector types is more like member access.
220  case Expr::ArraySubscriptExprClass:
221  if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
222  return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
223  if (Lang.CPlusPlus11) {
224  // Step over the array-to-pointer decay if present, but not over the
225  // temporary materialization.
226  auto *Base = cast<ArraySubscriptExpr>(E)->getBase()->IgnoreImpCasts();
227  if (Base->getType()->isArrayType())
228  return ClassifyInternal(Ctx, Base);
229  }
230  return Cl::CL_LValue;
231 
232  // Subscripting matrix types behaves like member accesses.
233  case Expr::MatrixSubscriptExprClass:
234  return ClassifyInternal(Ctx, cast<MatrixSubscriptExpr>(E)->getBase());
235 
236  // C++ [expr.prim.general]p3: The result is an lvalue if the entity is a
237  // function or variable and a prvalue otherwise.
238  case Expr::DeclRefExprClass:
239  if (E->getType() == Ctx.UnknownAnyTy)
240  return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
242  return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
243 
244  // Member access is complex.
245  case Expr::MemberExprClass:
246  return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
247 
248  case Expr::UnaryOperatorClass:
249  switch (cast<UnaryOperator>(E)->getOpcode()) {
250  // C++ [expr.unary.op]p1: The unary * operator performs indirection:
251  // [...] the result is an lvalue referring to the object or function
252  // to which the expression points.
253  case UO_Deref:
254  return Cl::CL_LValue;
255 
256  // GNU extensions, simply look through them.
257  case UO_Extension:
258  return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
259 
260  // Treat _Real and _Imag basically as if they were member
261  // expressions: l-value only if the operand is a true l-value.
262  case UO_Real:
263  case UO_Imag: {
264  const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
265  Cl::Kinds K = ClassifyInternal(Ctx, Op);
266  if (K != Cl::CL_LValue) return K;
267 
268  if (isa<ObjCPropertyRefExpr>(Op))
270  return Cl::CL_LValue;
271  }
272 
273  // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
274  // lvalue, [...]
275  // Not so in C.
276  case UO_PreInc:
277  case UO_PreDec:
278  return Lang.CPlusPlus ? Cl::CL_LValue : Cl::CL_PRValue;
279 
280  default:
281  return Cl::CL_PRValue;
282  }
283 
284  case Expr::RecoveryExprClass:
285  case Expr::OpaqueValueExprClass:
286  return ClassifyExprValueKind(Lang, E, E->getValueKind());
287 
288  // Pseudo-object expressions can produce l-values with reference magic.
289  case Expr::PseudoObjectExprClass:
290  return ClassifyExprValueKind(Lang, E,
291  cast<PseudoObjectExpr>(E)->getValueKind());
292 
293  // Implicit casts are lvalues if they're lvalue casts. Other than that, we
294  // only specifically record class temporaries.
295  case Expr::ImplicitCastExprClass:
296  return ClassifyExprValueKind(Lang, E, E->getValueKind());
297 
298  // C++ [expr.prim.general]p4: The presence of parentheses does not affect
299  // whether the expression is an lvalue.
300  case Expr::ParenExprClass:
301  return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
302 
303  // C11 6.5.1.1p4: [A generic selection] is an lvalue, a function designator,
304  // or a void expression if its result expression is, respectively, an
305  // lvalue, a function designator, or a void expression.
306  case Expr::GenericSelectionExprClass:
307  if (cast<GenericSelectionExpr>(E)->isResultDependent())
308  return Cl::CL_PRValue;
309  return ClassifyInternal(Ctx,cast<GenericSelectionExpr>(E)->getResultExpr());
310 
311  case Expr::BinaryOperatorClass:
312  case Expr::CompoundAssignOperatorClass:
313  // C doesn't have any binary expressions that are lvalues.
314  if (Lang.CPlusPlus)
315  return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
316  return Cl::CL_PRValue;
317 
318  case Expr::CallExprClass:
319  case Expr::CXXOperatorCallExprClass:
320  case Expr::CXXMemberCallExprClass:
321  case Expr::UserDefinedLiteralClass:
322  case Expr::CUDAKernelCallExprClass:
323  return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx));
324 
325  case Expr::CXXRewrittenBinaryOperatorClass:
326  return ClassifyInternal(
327  Ctx, cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm());
328 
329  // __builtin_choose_expr is equivalent to the chosen expression.
330  case Expr::ChooseExprClass:
331  return ClassifyInternal(Ctx, cast<ChooseExpr>(E)->getChosenSubExpr());
332 
333  // Extended vector element access is an lvalue unless there are duplicates
334  // in the shuffle expression.
335  case Expr::ExtVectorElementExprClass:
336  if (cast<ExtVectorElementExpr>(E)->containsDuplicateElements())
338  if (cast<ExtVectorElementExpr>(E)->isArrow())
339  return Cl::CL_LValue;
340  return ClassifyInternal(Ctx, cast<ExtVectorElementExpr>(E)->getBase());
341 
342  // Simply look at the actual default argument.
343  case Expr::CXXDefaultArgExprClass:
344  return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
345 
346  // Same idea for default initializers.
347  case Expr::CXXDefaultInitExprClass:
348  return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr());
349 
350  // Same idea for temporary binding.
351  case Expr::CXXBindTemporaryExprClass:
352  return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
353 
354  // And the cleanups guard.
355  case Expr::ExprWithCleanupsClass:
356  return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
357 
358  // Casts depend completely on the target type. All casts work the same.
359  case Expr::CStyleCastExprClass:
360  case Expr::CXXFunctionalCastExprClass:
361  case Expr::CXXStaticCastExprClass:
362  case Expr::CXXDynamicCastExprClass:
363  case Expr::CXXReinterpretCastExprClass:
364  case Expr::CXXConstCastExprClass:
365  case Expr::CXXAddrspaceCastExprClass:
366  case Expr::ObjCBridgedCastExprClass:
367  case Expr::BuiltinBitCastExprClass:
368  // Only in C++ can casts be interesting at all.
369  if (!Lang.CPlusPlus) return Cl::CL_PRValue;
370  return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
371 
372  case Expr::CXXUnresolvedConstructExprClass:
373  return ClassifyUnnamed(Ctx,
374  cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
375 
376  case Expr::BinaryConditionalOperatorClass: {
377  if (!Lang.CPlusPlus) return Cl::CL_PRValue;
378  const auto *co = cast<BinaryConditionalOperator>(E);
379  return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
380  }
381 
382  case Expr::ConditionalOperatorClass: {
383  // Once again, only C++ is interesting.
384  if (!Lang.CPlusPlus) return Cl::CL_PRValue;
385  const auto *co = cast<ConditionalOperator>(E);
386  return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
387  }
388 
389  // ObjC message sends are effectively function calls, if the target function
390  // is known.
391  case Expr::ObjCMessageExprClass:
392  if (const ObjCMethodDecl *Method =
393  cast<ObjCMessageExpr>(E)->getMethodDecl()) {
394  Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType());
396  }
397  return Cl::CL_PRValue;
398 
399  // Some C++ expressions are always class temporaries.
400  case Expr::CXXConstructExprClass:
401  case Expr::CXXInheritedCtorInitExprClass:
402  case Expr::CXXTemporaryObjectExprClass:
403  case Expr::LambdaExprClass:
404  case Expr::CXXStdInitializerListExprClass:
405  return Cl::CL_ClassTemporary;
406 
407  case Expr::VAArgExprClass:
408  return ClassifyUnnamed(Ctx, E->getType());
409 
410  case Expr::DesignatedInitExprClass:
411  return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
412 
413  case Expr::StmtExprClass: {
414  const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
415  if (const auto *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
416  return ClassifyUnnamed(Ctx, LastExpr->getType());
417  return Cl::CL_PRValue;
418  }
419 
420  case Expr::PackExpansionExprClass:
421  return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
422 
423  case Expr::MaterializeTemporaryExprClass:
424  return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
425  ? Cl::CL_LValue
426  : Cl::CL_XValue;
427 
428  case Expr::InitListExprClass:
429  // An init list can be an lvalue if it is bound to a reference and
430  // contains only one element. In that case, we look at that element
431  // for an exact classification. Init list creation takes care of the
432  // value kind for us, so we only need to fine-tune.
433  if (E->isPRValue())
434  return ClassifyExprValueKind(Lang, E, E->getValueKind());
435  assert(cast<InitListExpr>(E)->getNumInits() == 1 &&
436  "Only 1-element init lists can be glvalues.");
437  return ClassifyInternal(Ctx, cast<InitListExpr>(E)->getInit(0));
438 
439  case Expr::CoawaitExprClass:
440  case Expr::CoyieldExprClass:
441  return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr());
442  case Expr::SYCLUniqueStableNameExprClass:
443  return Cl::CL_PRValue;
444  break;
445  }
446 
447  llvm_unreachable("unhandled expression kind in classification");
448 }
449 
450 /// ClassifyDecl - Return the classification of an expression referencing the
451 /// given declaration.
452 static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D) {
453  // C++ [expr.prim.general]p6: The result is an lvalue if the entity is a
454  // function, variable, or data member and a prvalue otherwise.
455  // In C, functions are not lvalues.
456  // In addition, NonTypeTemplateParmDecl derives from VarDecl but isn't an
457  // lvalue unless it's a reference type (C++ [temp.param]p6), so we need to
458  // special-case this.
459 
460  if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance())
461  return Cl::CL_MemberFunction;
462 
463  bool islvalue;
464  if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(D))
465  islvalue = NTTParm->getType()->isReferenceType() ||
466  NTTParm->getType()->isRecordType();
467  else
468  islvalue = isa<VarDecl>(D) || isa<FieldDecl>(D) ||
469  isa<IndirectFieldDecl>(D) ||
470  isa<BindingDecl>(D) ||
471  isa<MSGuidDecl>(D) ||
472  isa<TemplateParamObjectDecl>(D) ||
473  (Ctx.getLangOpts().CPlusPlus &&
474  (isa<FunctionDecl>(D) || isa<MSPropertyDecl>(D) ||
475  isa<FunctionTemplateDecl>(D)));
476 
477  return islvalue ? Cl::CL_LValue : Cl::CL_PRValue;
478 }
479 
480 /// ClassifyUnnamed - Return the classification of an expression yielding an
481 /// unnamed value of the given type. This applies in particular to function
482 /// calls and casts.
484  // In C, function calls are always rvalues.
485  if (!Ctx.getLangOpts().CPlusPlus) return Cl::CL_PRValue;
486 
487  // C++ [expr.call]p10: A function call is an lvalue if the result type is an
488  // lvalue reference type or an rvalue reference to function type, an xvalue
489  // if the result type is an rvalue reference to object type, and a prvalue
490  // otherwise.
491  if (T->isLValueReferenceType())
492  return Cl::CL_LValue;
493  const auto *RV = T->getAs<RValueReferenceType>();
494  if (!RV) // Could still be a class temporary, though.
495  return ClassifyTemporary(T);
496 
497  return RV->getPointeeType()->isFunctionType() ? Cl::CL_LValue : Cl::CL_XValue;
498 }
499 
501  if (E->getType() == Ctx.UnknownAnyTy)
502  return (isa<FunctionDecl>(E->getMemberDecl())
504 
505  // Handle C first, it's easier.
506  if (!Ctx.getLangOpts().CPlusPlus) {
507  // C99 6.5.2.3p3
508  // For dot access, the expression is an lvalue if the first part is. For
509  // arrow access, it always is an lvalue.
510  if (E->isArrow())
511  return Cl::CL_LValue;
512  // ObjC property accesses are not lvalues, but get special treatment.
513  Expr *Base = E->getBase()->IgnoreParens();
514  if (isa<ObjCPropertyRefExpr>(Base))
516  return ClassifyInternal(Ctx, Base);
517  }
518 
520  // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
521  // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
522  // E1.E2 is an lvalue.
523  if (const auto *Value = dyn_cast<ValueDecl>(Member))
524  if (Value->getType()->isReferenceType())
525  return Cl::CL_LValue;
526 
527  // Otherwise, one of the following rules applies.
528  // -- If E2 is a static member [...] then E1.E2 is an lvalue.
529  if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord())
530  return Cl::CL_LValue;
531 
532  // -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
533  // E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
534  // otherwise, it is a prvalue.
535  if (isa<FieldDecl>(Member)) {
536  // *E1 is an lvalue
537  if (E->isArrow())
538  return Cl::CL_LValue;
540  if (isa<ObjCPropertyRefExpr>(Base))
542  return ClassifyInternal(Ctx, E->getBase());
543  }
544 
545  // -- If E2 is a [...] member function, [...]
546  // -- If it refers to a static member function [...], then E1.E2 is an
547  // lvalue; [...]
548  // -- Otherwise [...] E1.E2 is a prvalue.
549  if (const auto *Method = dyn_cast<CXXMethodDecl>(Member))
550  return Method->isStatic() ? Cl::CL_LValue : Cl::CL_MemberFunction;
551 
552  // -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
553  // So is everything else we haven't handled yet.
554  return Cl::CL_PRValue;
555 }
556 
558  assert(Ctx.getLangOpts().CPlusPlus &&
559  "This is only relevant for C++.");
560  // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
561  // Except we override this for writes to ObjC properties.
562  if (E->isAssignmentOp())
563  return (E->getLHS()->getObjectKind() == OK_ObjCProperty
565 
566  // C++ [expr.comma]p1: the result is of the same value category as its right
567  // operand, [...].
568  if (E->getOpcode() == BO_Comma)
569  return ClassifyInternal(Ctx, E->getRHS());
570 
571  // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
572  // is a pointer to a data member is of the same value category as its first
573  // operand.
574  if (E->getOpcode() == BO_PtrMemD)
575  return (E->getType()->isFunctionType() ||
576  E->hasPlaceholderType(BuiltinType::BoundMember))
578  : ClassifyInternal(Ctx, E->getLHS());
579 
580  // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
581  // second operand is a pointer to data member and a prvalue otherwise.
582  if (E->getOpcode() == BO_PtrMemI)
583  return (E->getType()->isFunctionType() ||
584  E->hasPlaceholderType(BuiltinType::BoundMember))
586  : Cl::CL_LValue;
587 
588  // All other binary operations are prvalues.
589  return Cl::CL_PRValue;
590 }
591 
592 static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
593  const Expr *False) {
594  assert(Ctx.getLangOpts().CPlusPlus &&
595  "This is only relevant for C++.");
596 
597  // C++ [expr.cond]p2
598  // If either the second or the third operand has type (cv) void,
599  // one of the following shall hold:
600  if (True->getType()->isVoidType() || False->getType()->isVoidType()) {
601  // The second or the third operand (but not both) is a (possibly
602  // parenthesized) throw-expression; the result is of the [...] value
603  // category of the other.
604  bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts());
605  bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts());
606  if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False)
607  : (FalseIsThrow ? True : nullptr))
608  return ClassifyInternal(Ctx, NonThrow);
609 
610  // [Otherwise] the result [...] is a prvalue.
611  return Cl::CL_PRValue;
612  }
613 
614  // Note that at this point, we have already performed all conversions
615  // according to [expr.cond]p3.
616  // C++ [expr.cond]p4: If the second and third operands are glvalues of the
617  // same value category [...], the result is of that [...] value category.
618  // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
619  Cl::Kinds LCl = ClassifyInternal(Ctx, True),
620  RCl = ClassifyInternal(Ctx, False);
621  return LCl == RCl ? LCl : Cl::CL_PRValue;
622 }
623 
625  Cl::Kinds Kind, SourceLocation &Loc) {
626  // As a general rule, we only care about lvalues. But there are some rvalues
627  // for which we want to generate special results.
628  if (Kind == Cl::CL_PRValue) {
629  // For the sake of better diagnostics, we want to specifically recognize
630  // use of the GCC cast-as-lvalue extension.
631  if (const auto *CE = dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
632  if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
633  Loc = CE->getExprLoc();
634  return Cl::CM_LValueCast;
635  }
636  }
637  }
638  if (Kind != Cl::CL_LValue)
639  return Cl::CM_RValue;
640 
641  // This is the lvalue case.
642  // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
643  if (Ctx.getLangOpts().CPlusPlus && E->getType()->isFunctionType())
644  return Cl::CM_Function;
645 
646  // Assignment to a property in ObjC is an implicit setter access. But a
647  // setter might not exist.
648  if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
649  if (Expr->isImplicitProperty() &&
650  Expr->getImplicitPropertySetter() == nullptr)
652  }
653 
654  CanQualType CT = Ctx.getCanonicalType(E->getType());
655  // Const stuff is obviously not modifiable.
656  if (CT.isConstQualified())
657  return Cl::CM_ConstQualified;
658  if (Ctx.getLangOpts().OpenCL &&
660  return Cl::CM_ConstAddrSpace;
661 
662  // Arrays are not modifiable, only their elements are.
663  if (CT->isArrayType())
664  return Cl::CM_ArrayType;
665  // Incomplete types are not modifiable.
666  if (CT->isIncompleteType())
667  return Cl::CM_IncompleteType;
668 
669  // Records with any const fields (recursively) are not modifiable.
670  if (const RecordType *R = CT->getAs<RecordType>())
671  if (R->hasConstFields())
673 
674  return Cl::CM_Modifiable;
675 }
676 
678  Classification VC = Classify(Ctx);
679  switch (VC.getKind()) {
680  case Cl::CL_LValue: return LV_Valid;
681  case Cl::CL_XValue: return LV_InvalidExpression;
682  case Cl::CL_Function: return LV_NotObjectType;
683  case Cl::CL_Void: return LV_InvalidExpression;
692  }
693  llvm_unreachable("Unhandled kind");
694 }
695 
698  SourceLocation dummy;
699  Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : dummy);
700  switch (VC.getKind()) {
701  case Cl::CL_LValue: break;
703  case Cl::CL_Function: return MLV_NotObjectType;
704  case Cl::CL_Void: return MLV_InvalidExpression;
712  case Cl::CL_PRValue:
713  return VC.getModifiable() == Cl::CM_LValueCast ?
715  }
716  assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
717  switch (VC.getModifiable()) {
718  case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
719  case Cl::CM_Modifiable: return MLV_Valid;
720  case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
721  case Cl::CM_Function: return MLV_NotObjectType;
722  case Cl::CM_LValueCast:
723  llvm_unreachable("CM_LValueCast and CL_LValue don't match");
728  case Cl::CM_ArrayType: return MLV_ArrayType;
730  }
731  llvm_unreachable("Unhandled modifiable type");
732 }
clang::Expr::LV_InvalidExpression
@ LV_InvalidExpression
Definition: Expr.h:280
clang::Expr::getValueKind
ExprValueKind getValueKind() const
getValueKind - The value kind that this expression produces.
Definition: Expr.h:430
clang::Expr::MLV_IncompleteType
@ MLV_IncompleteType
Definition: Expr.h:297
clang::Type::isRecordType
bool isRecordType() const
Definition: Type.h:6762
clang::BinaryOperator::isAssignmentOp
static bool isAssignmentOp(Opcode Opc)
Definition: Expr.h:3938
clang::Expr::LValueClassification
LValueClassification
Definition: Expr.h:275
clang::Expr::isLValue
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition: Expr.h:270
clang::Expr::MLV_DuplicateVectorComponents
@ MLV_DuplicateVectorComponents
Definition: Expr.h:294
clang::Expr::Classification::CL_Function
@ CL_Function
Definition: Expr.h:329
clang::Expr::Classification::CM_LValueCast
@ CM_LValueCast
Definition: Expr.h:346
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::Expr::MLV_ConstQualified
@ MLV_ConstQualified
Definition: Expr.h:298
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
clang::Expr::MLV_MemberFunction
@ MLV_MemberFunction
Definition: Expr.h:303
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::Expr::getObjectKind
ExprObjectKind getObjectKind() const
getObjectKind - The object kind that this expression produces.
Definition: Expr.h:437
clang::Expr::LV_ClassTemporary
@ LV_ClassTemporary
Definition: Expr.h:284
DeclCXX.h
clang::VK_XValue
@ VK_XValue
An x-value expression is a reference to an object with independent storage but which can be "moved",...
Definition: Specifiers.h:129
clang::Expr::isModifiableLvalue
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...
Definition: ExprClassification.cpp:697
clang::Expr::MLV_ArrayTemporary
@ MLV_ArrayTemporary
Definition: Expr.h:307
clang::ASTContext::OverloadTy
CanQualType OverloadTy
Definition: ASTContext.h:1103
clang::CanQual::isConstQualified
bool isConstQualified() const
Definition: CanonicalType.h:137
clang::Expr::isPRValue
bool isPRValue() const
Definition: Expr.h:271
clang::Type::isVoidType
bool isVoidType() const
Definition: Type.h:6955
clang::Expr::Classification::CM_Untested
@ CM_Untested
Definition: Expr.h:342
clang::Expr::Classification::CL_AddressableVoid
@ CL_AddressableVoid
Definition: Expr.h:331
clang::BinaryOperator::getOpcode
Opcode getOpcode() const
Definition: Expr.h:3847
IsModifiable
static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E, Cl::Kinds Kind, SourceLocation &Loc)
Definition: ExprClassification.cpp:624
clang::Expr::isXValue
bool isXValue() const
Definition: Expr.h:272
ClassifyExprValueKind
static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang, const Expr *E, ExprValueKind Kind)
Definition: ExprClassification.cpp:94
ClassifyDecl
static Cl::Kinds ClassifyDecl(ASTContext &Ctx, const Decl *D)
ClassifyDecl - Return the classification of an expression referencing the given declaration.
Definition: ExprClassification.cpp:452
clang::Stmt::NoStmtClass
@ NoStmtClass
Definition: Stmt.h:72
DeclObjC.h
clang::Expr::Classification::CM_ConstQualifiedField
@ CM_ConstQualifiedField
Definition: Expr.h:349
clang::Expr::MLV_ArrayType
@ MLV_ArrayType
Definition: Expr.h:301
clang::Expr::Classification::CL_ArrayTemporary
@ CL_ArrayTemporary
Definition: Expr.h:336
clang::Expr::MLV_LValueCast
@ MLV_LValueCast
Definition: Expr.h:296
clang::Qualifiers::getAddressSpace
LangAS getAddressSpace() const
Definition: Type.h:361
clang::Expr::Classification::CM_ArrayType
@ CM_ArrayType
Definition: Expr.h:351
clang::Expr::Classification::CM_ConstQualified
@ CM_ConstQualified
Definition: Expr.h:348
clang::Type::isReferenceType
bool isReferenceType() const
Definition: Type.h:6684
clang::RecordType
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4613
clang::CompoundStmt
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1399
DeclTemplate.h
clang::Expr::MLV_ClassTemporary
@ MLV_ClassTemporary
Definition: Expr.h:306
clang::BinaryOperator
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3803
clang::MemberExpr::isArrow
bool isArrow() const
Definition: Expr.h:3346
clang::Expr::ClassifyLValue
LValueClassification ClassifyLValue(ASTContext &Ctx) const
Reasons why an expression might not be an l-value.
Definition: ExprClassification.cpp:677
clang::Expr::LV_Valid
@ LV_Valid
Definition: Expr.h:276
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::Expr::hasPlaceholderType
bool hasPlaceholderType() const
Returns whether this expression has a placeholder type.
Definition: Expr.h:502
clang::DeclaratorContext::Member
@ Member
clang::Expr::LV_DuplicateVectorComponents
@ LV_DuplicateVectorComponents
Definition: Expr.h:279
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7161
clang::CanQual::getQualifiers
Qualifiers getQualifiers() const
Retrieve all qualifiers.
Definition: CanonicalType.h:129
clang::Expr::MLV_NotObjectType
@ MLV_NotObjectType
Definition: Expr.h:292
clang::Expr::Classification::CL_ObjCMessageRValue
@ CL_ObjCMessageRValue
Definition: Expr.h:337
clang::Expr::MLV_InvalidExpression
@ MLV_InvalidExpression
Definition: Expr.h:295
clang::ASTContext::UnknownAnyTy
CanQualType UnknownAnyTy
Definition: ASTContext.h:1103
clang::Type::isLValueReferenceType
bool isLValueReferenceType() const
Definition: Type.h:6688
clang::Expr::MLV_ConstQualifiedField
@ MLV_ConstQualifiedField
Definition: Expr.h:299
clang::Type::isFunctionType
bool isFunctionType() const
Definition: Type.h:6668
Expr.h
clang::LangAS::opencl_constant
@ opencl_constant
ASTContext.h
clang::Expr::MLV_IncompleteVoidType
@ MLV_IncompleteVoidType
Definition: Expr.h:293
clang::Expr::Classification::CM_NoSetterProperty
@ CM_NoSetterProperty
Definition: Expr.h:347
clang::ASTContext::getCanonicalType
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
Definition: ASTContext.h:2468
clang::Expr::Classification::getModifiable
ModifiableType getModifiable() const
Definition: Expr.h:369
ClassifyConditional
static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *trueExpr, const Expr *falseExpr)
Definition: ExprClassification.cpp:592
clang::Expr::Classification::Kinds
Kinds
The various classification results. Most of these mean prvalue.
Definition: Expr.h:326
ExprObjC.h
clang::CanQual< Type >
ExprCXX.h
Base
clang::Expr::MLV_SubObjCPropertySetting
@ MLV_SubObjCPropertySetting
Definition: Expr.h:304
clang::QualType::hasQualifiers
bool hasQualifiers() const
Determine whether this type has any qualifiers.
Definition: Type.h:6499
clang::MemberExpr::getMemberDecl
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition: Expr.h:3245
clang::VK_LValue
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition: Specifiers.h:124
clang::MemberExpr::getBase
Expr * getBase() const
Definition: Expr.h:3239
clang::VK_PRValue
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition: Specifiers.h:120
clang::Expr::Classification::CM_Function
@ CM_Function
Definition: Expr.h:345
clang::Stmt::getStmtClass
StmtClass getStmtClass() const
Definition: Stmt.h:1163
clang::Expr::Classification::CL_SubObjCPropertySetting
@ CL_SubObjCPropertySetting
Definition: Expr.h:334
clang::BinaryOperator::getLHS
Expr * getLHS() const
Definition: Expr.h:3852
clang::Expr::Classification::ModifiableType
ModifiableType
The results of modification testing.
Definition: Expr.h:341
ClassifyMemberExpr
static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E)
Definition: ExprClassification.cpp:500
Value
Value
Definition: UninitializedValues.cpp:102
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:89
clang::Expr::LV_SubObjCPropertySetting
@ LV_SubObjCPropertySetting
Definition: Expr.h:283
clang::Expr::IgnoreParenImpCasts
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition: Expr.cpp:2919
clang::Expr::MLV_InvalidMessageExpression
@ MLV_InvalidMessageExpression
Definition: Expr.h:305
clang::Expr::Classification::CL_MemberFunction
@ CL_MemberFunction
Definition: Expr.h:333
clang::Expr::LV_MemberFunction
@ LV_MemberFunction
Definition: Expr.h:282
clang::ExprValueKind
ExprValueKind
The categorization of expression values, currently following the C++11 scheme.
Definition: Specifiers.h:117
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:58
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
clang::Expr::Classification::CM_IncompleteType
@ CM_IncompleteType
Definition: Expr.h:352
clang::Expr::LV_NotObjectType
@ LV_NotObjectType
Definition: Expr.h:277
clang::RValueReferenceType
An rvalue reference type, per C++11 [dcl.ref].
Definition: Type.h:2813
clang::ObjCMethodDecl
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:139
clang::OK_ObjCProperty
@ 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:146
clang::Expr::Classify
Classification Classify(ASTContext &Ctx) const
Classify - Classify this expression according to the C++11 expression taxonomy.
Definition: Expr.h:398
clang::Expr::LV_InvalidMessageExpression
@ LV_InvalidMessageExpression
Definition: Expr.h:281
clang::Expr::IgnoreParens
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:2915
clang
Definition: CalledOnceCheck.h:17
clang::Expr::LV_ArrayTemporary
@ LV_ArrayTemporary
Definition: Expr.h:285
clang::Expr::LV_IncompleteVoidType
@ LV_IncompleteVoidType
Definition: Expr.h:278
clang::Type::isArrayType
bool isArrayType() const
Definition: Type.h:6738
clang::Expr::Classification::CL_Void
@ CL_Void
Definition: Expr.h:330
clang::Expr::Classification::CL_PRValue
@ CL_PRValue
Definition: Expr.h:338
clang::BinaryOperator::getRHS
Expr * getRHS() const
Definition: Expr.h:3854
clang::Expr::MLV_ConstAddrSpace
@ MLV_ConstAddrSpace
Definition: Expr.h:300
clang::Expr::Classification::CL_DuplicateVectorComponents
@ CL_DuplicateVectorComponents
Definition: Expr.h:332
clang::Expr::getType
QualType getType() const
Definition: Expr.h:141
clang::Expr::Classification::CM_ConstAddrSpace
@ CM_ConstAddrSpace
Definition: Expr.h:350
clang::Expr::Classification::getKind
Kinds getKind() const
Definition: Expr.h:368
clang::Expr::MLV_Valid
@ MLV_Valid
Definition: Expr.h:291
clang::Expr::ClassifyModifiable
Classification ClassifyModifiable(ASTContext &Ctx, SourceLocation &Loc) const
ClassifyModifiable - Classify this expression according to the C++11 expression taxonomy,...
Definition: Expr.h:410
clang::MemberExpr
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3162
ClassifyBinaryOp
static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E)
Definition: ExprClassification.cpp:557
clang::CanQual::getAs
CanProxy< U > getAs() const
Retrieve a canonical type pointer with a different static type, upcasting or downcasting as needed.
Definition: CanonicalType.h:653
clang::Expr::Classification
The return type of classify().
Definition: Expr.h:323
clang::Expr::Classification::CL_ClassTemporary
@ CL_ClassTemporary
Definition: Expr.h:335
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::Expr::isModifiableLvalueResult
isModifiableLvalueResult
Definition: Expr.h:290
ClassifyTemporary
static Cl::Kinds ClassifyTemporary(QualType T)
Classify an expression which creates a temporary, based on its type.
Definition: ExprClassification.cpp:83
clang::Expr::Classification::CM_Modifiable
@ CM_Modifiable
Definition: Expr.h:343
clang::diag::kind
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:62
clang::Expr::Classification::CL_XValue
@ CL_XValue
Definition: Expr.h:328
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:765
clang::Expr::MLV_NoSetterProperty
@ MLV_NoSetterProperty
Definition: Expr.h:302
clang::Expr::Classification::CL_LValue
@ CL_LValue
Definition: Expr.h:327
clang::Expr::Classification::CM_RValue
@ CM_RValue
Definition: Expr.h:344
ClassifyUnnamed
static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T)
ClassifyUnnamed - Return the classification of an expression yielding an unnamed value of the given t...
Definition: ExprClassification.cpp:483
ClassifyInternal
static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E)
Definition: ExprClassification.cpp:108