Coverage Report

Created: 2020-03-31 06:27

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