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