Coverage Report

Created: 2020-11-24 06:42

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