Coverage Report

Created: 2022-07-16 07:03

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