Coverage Report

Created: 2024-01-17 10:31

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