Coverage Report

Created: 2020-09-15 12:33

/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.57M
Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {
38
8.57M
  assert(!TR->isReferenceType() && "Expressions can't have reference type.");
39
8.57M
40
8.57M
  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.57M
  if (!Ctx.getLangOpts().CPlusPlus) {
44
    // Thus, no functions.
45
546k
    if (TR->isFunctionType() || 
TR == Ctx.OverloadTy546k
)
46
519
      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
546k
    else if (TR->isVoidType() && 
!TR.hasQualifiers()28
)
51
26
      kind = (kind == Cl::CL_LValue ? 
Cl::CL_AddressableVoid21
:
Cl::CL_Void5
);
52
546k
  }
53
8.57M
54
  // Enable this assertion for testing.
55
8.57M
  switch (kind) {
56
7.69M
  case Cl::CL_LValue: assert(getValueKind() == VK_LValue); break;
57
185k
  case Cl::CL_XValue: assert(getValueKind() == VK_XValue); break;
58
699k
  case Cl::CL_Function:
59
699k
  case Cl::CL_Void:
60
699k
  case Cl::CL_AddressableVoid:
61
699k
  case Cl::CL_DuplicateVectorComponents:
62
699k
  case Cl::CL_MemberFunction:
63
699k
  case Cl::CL_SubObjCPropertySetting:
64
699k
  case Cl::CL_ClassTemporary:
65
699k
  case Cl::CL_ArrayTemporary:
66
699k
  case Cl::CL_ObjCMessageRValue:
67
699k
  case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
68
8.57M
  }
69
8.57M
70
8.57M
  Cl::ModifiableType modifiable = Cl::CM_Untested;
71
8.57M
  if (Loc)
72
1.94M
    modifiable = IsModifiable(Ctx, this, kind, *Loc);
73
8.57M
  return Classification(kind, modifiable);
74
8.57M
}
75
76
/// Classify an expression which creates a temporary, based on its type.
77
434k
static Cl::Kinds ClassifyTemporary(QualType T) {
78
434k
  if (T->isRecordType())
79
386k
    return Cl::CL_ClassTemporary;
80
48.0k
  if (T->isArrayType())
81
40
    return Cl::CL_ArrayTemporary;
82
48.0k
83
  // No special classification: these don't behave differently from normal
84
  // prvalues.
85
48.0k
  return Cl::CL_PRValue;
86
48.0k
}
87
88
static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang,
89
                                       const Expr *E,
90
452k
                                       ExprValueKind Kind) {
91
452k
  switch (Kind) {
92
107k
  case VK_RValue:
93
107k
    return Lang.CPlusPlus ? ClassifyTemporary(E->getType()) : 
Cl::CL_PRValue9
;
94
283k
  case VK_LValue:
95
283k
    return Cl::CL_LValue;
96
61.4k
  case VK_XValue:
97
61.4k
    return Cl::CL_XValue;
98
0
  }
99
0
  llvm_unreachable("Invalid value category of implicit cast.");
100
0
}
101
102
9.05M
static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
103
  // This function takes the first stab at classifying expressions.
104
9.05M
  const LangOptions &Lang = Ctx.getLangOpts();
105
9.05M
106
9.05M
  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
    // First come the expressions that are always lvalues, unconditionally.
115
14.1k
  case Expr::ObjCIsaExprClass:
116
    // C++ [expr.prim.general]p1: A string literal is an lvalue.
117
14.1k
  case Expr::StringLiteralClass:
118
    // @encode is equivalent to its string
119
14.1k
  case Expr::ObjCEncodeExprClass:
120
    // __func__ and friends are too.
121
14.1k
  case Expr::PredefinedExprClass:
122
    // Property references are lvalues
123
14.1k
  case Expr::ObjCSubscriptRefExprClass:
124
14.1k
  case Expr::ObjCPropertyRefExprClass:
125
    // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
126
14.1k
  case Expr::CXXTypeidExprClass:
127
14.1k
  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.1k
  case Expr::UnresolvedLookupExprClass:
131
14.1k
  case Expr::UnresolvedMemberExprClass:
132
14.1k
  case Expr::TypoExprClass:
133
14.1k
  case Expr::DependentCoawaitExprClass:
134
14.1k
  case Expr::CXXDependentScopeMemberExprClass:
135
14.1k
  case Expr::DependentScopeDeclRefExprClass:
136
    // ObjC instance variables are lvalues
137
    // FIXME: ObjC++0x might have different rules
138
14.1k
  case Expr::ObjCIvarRefExprClass:
139
14.1k
  case Expr::FunctionParmPackExprClass:
140
14.1k
  case Expr::MSPropertyRefExprClass:
141
14.1k
  case Expr::MSPropertySubscriptExprClass:
142
14.1k
  case Expr::OMPArraySectionExprClass:
143
14.1k
  case Expr::OMPArrayShapingExprClass:
144
14.1k
  case Expr::OMPIteratorExprClass:
145
14.1k
    return Cl::CL_LValue;
146
14.1k
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
14.1k
152
    // Expressions that are prvalues.
153
98.4k
  case Expr::CXXBoolLiteralExprClass:
154
98.4k
  case Expr::CXXPseudoDestructorExprClass:
155
98.4k
  case Expr::UnaryExprOrTypeTraitExprClass:
156
98.4k
  case Expr::CXXNewExprClass:
157
98.4k
  case Expr::CXXThisExprClass:
158
98.4k
  case Expr::CXXNullPtrLiteralExprClass:
159
98.4k
  case Expr::ImaginaryLiteralClass:
160
98.4k
  case Expr::GNUNullExprClass:
161
98.4k
  case Expr::OffsetOfExprClass:
162
98.4k
  case Expr::CXXThrowExprClass:
163
98.4k
  case Expr::ShuffleVectorExprClass:
164
98.4k
  case Expr::ConvertVectorExprClass:
165
98.4k
  case Expr::IntegerLiteralClass:
166
98.4k
  case Expr::FixedPointLiteralClass:
167
98.4k
  case Expr::CharacterLiteralClass:
168
98.4k
  case Expr::AddrLabelExprClass:
169
98.4k
  case Expr::CXXDeleteExprClass:
170
98.4k
  case Expr::ImplicitValueInitExprClass:
171
98.4k
  case Expr::BlockExprClass:
172
98.4k
  case Expr::FloatingLiteralClass:
173
98.4k
  case Expr::CXXNoexceptExprClass:
174
98.4k
  case Expr::CXXScalarValueInitExprClass:
175
98.4k
  case Expr::TypeTraitExprClass:
176
98.4k
  case Expr::ArrayTypeTraitExprClass:
177
98.4k
  case Expr::ExpressionTraitExprClass:
178
98.4k
  case Expr::ObjCSelectorExprClass:
179
98.4k
  case Expr::ObjCProtocolExprClass:
180
98.4k
  case Expr::ObjCStringLiteralClass:
181
98.4k
  case Expr::ObjCBoxedExprClass:
182
98.4k
  case Expr::ObjCArrayLiteralClass:
183
98.4k
  case Expr::ObjCDictionaryLiteralClass:
184
98.4k
  case Expr::ObjCBoolLiteralExprClass:
185
98.4k
  case Expr::ObjCAvailabilityCheckExprClass:
186
98.4k
  case Expr::ParenListExprClass:
187
98.4k
  case Expr::SizeOfPackExprClass:
188
98.4k
  case Expr::SubstNonTypeTemplateParmPackExprClass:
189
98.4k
  case Expr::AsTypeExprClass:
190
98.4k
  case Expr::ObjCIndirectCopyRestoreExprClass:
191
98.4k
  case Expr::AtomicExprClass:
192
98.4k
  case Expr::CXXFoldExprClass:
193
98.4k
  case Expr::ArrayInitLoopExprClass:
194
98.4k
  case Expr::ArrayInitIndexExprClass:
195
98.4k
  case Expr::NoInitExprClass:
196
98.4k
  case Expr::DesignatedInitUpdateExprClass:
197
98.4k
  case Expr::SourceLocExprClass:
198
98.4k
  case Expr::ConceptSpecializationExprClass:
199
98.4k
  case Expr::RequiresExprClass:
200
98.4k
    return Cl::CL_PRValue;
201
98.4k
202
48
  case Expr::ConstantExprClass:
203
48
    return ClassifyInternal(Ctx, cast<ConstantExpr>(E)->getSubExpr());
204
98.4k
205
    // Next come the complicated cases.
206
73
  case Expr::SubstNonTypeTemplateParmExprClass:
207
73
    return ClassifyInternal(Ctx,
208
73
                 cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement());
209
98.4k
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
284k
  case Expr::ArraySubscriptExprClass:
215
284k
    if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
216
11.2k
      return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
217
273k
    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.0k
    }
224
35.0k
    return Cl::CL_LValue;
225
35.0k
226
  // Subscripting matrix types behaves like member accesses.
227
26
  case Expr::MatrixSubscriptExprClass:
228
26
    return ClassifyInternal(Ctx, cast<MatrixSubscriptExpr>(E)->getBase());
229
35.0k
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
6.76M
238
    // Member access is complex.
239
448k
  case Expr::MemberExprClass:
240
448k
    return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
241
6.76M
242
84.8k
  case Expr::UnaryOperatorClass:
243
84.8k
    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
80.1k
    case UO_Deref:
248
80.1k
      return Cl::CL_LValue;
249
0
250
      // GNU extensions, simply look through them.
251
3
    case UO_Extension:
252
3
      return ClassifyInternal(Ctx, cast<UnaryOperator>(E)->getSubExpr());
253
0
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
123
    case UO_Real:
257
123
    case UO_Imag: {
258
123
      const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
259
123
      Cl::Kinds K = ClassifyInternal(Ctx, Op);
260
123
      if (K != Cl::CL_LValue) 
return K2
;
261
121
262
121
      if (isa<ObjCPropertyRefExpr>(Op))
263
0
        return Cl::CL_SubObjCPropertySetting;
264
121
      return Cl::CL_LValue;
265
121
    }
266
121
267
      // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
268
      //   lvalue, [...]
269
      // Not so in C.
270
604
    case UO_PreInc:
271
604
    case UO_PreDec:
272
604
      return Lang.CPlusPlus ? Cl::CL_LValue : 
Cl::CL_PRValue0
;
273
604
274
4.03k
    default:
275
4.03k
      return Cl::CL_PRValue;
276
0
    }
277
0
278
379k
  case Expr::RecoveryExprClass:
279
379k
  case Expr::OpaqueValueExprClass:
280
379k
    return ClassifyExprValueKind(Lang, E, E->getValueKind());
281
379k
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
379k
287
    // Implicit casts are lvalues if they're lvalue casts. Other than that, we
288
    // only specifically record class temporaries.
289
72.7k
  case Expr::ImplicitCastExprClass:
290
72.7k
    return ClassifyExprValueKind(Lang, E, E->getValueKind());
291
379k
292
    // C++ [expr.prim.general]p4: The presence of parentheses does not affect
293
    //   whether the expression is an lvalue.
294
52.6k
  case Expr::ParenExprClass:
295
52.6k
    return ClassifyInternal(Ctx, cast<ParenExpr>(E)->getSubExpr());
296
379k
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
0
305
27.7k
  case Expr::BinaryOperatorClass:
306
27.7k
  case Expr::CompoundAssignOperatorClass:
307
    // C doesn't have any binary expressions that are lvalues.
308
27.7k
    if (Lang.CPlusPlus)
309
27.7k
      return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
310
4
    return Cl::CL_PRValue;
311
4
312
489k
  case Expr::CallExprClass:
313
489k
  case Expr::CXXOperatorCallExprClass:
314
489k
  case Expr::CXXMemberCallExprClass:
315
489k
  case Expr::UserDefinedLiteralClass:
316
489k
  case Expr::CUDAKernelCallExprClass:
317
489k
    return ClassifyUnnamed(Ctx, cast<CallExpr>(E)->getCallReturnType(Ctx));
318
489k
319
36
  case Expr::CXXRewrittenBinaryOperatorClass:
320
36
    return ClassifyInternal(
321
36
        Ctx, cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm());
322
489k
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
489k
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
79
336
    // Simply look at the actual default argument.
337
294
  case Expr::CXXDefaultArgExprClass:
338
294
    return ClassifyInternal(Ctx, cast<CXXDefaultArgExpr>(E)->getExpr());
339
79
340
    // Same idea for default initializers.
341
0
  case Expr::CXXDefaultInitExprClass:
342
0
    return ClassifyInternal(Ctx, cast<CXXDefaultInitExpr>(E)->getExpr());
343
79
344
    // Same idea for temporary binding.
345
41.2k
  case Expr::CXXBindTemporaryExprClass:
346
41.2k
    return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
347
79
348
    // And the cleanups guard.
349
405
  case Expr::ExprWithCleanupsClass:
350
405
    return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
351
79
352
    // Casts depend completely on the target type. All casts work the same.
353
154k
  case Expr::CStyleCastExprClass:
354
154k
  case Expr::CXXFunctionalCastExprClass:
355
154k
  case Expr::CXXStaticCastExprClass:
356
154k
  case Expr::CXXDynamicCastExprClass:
357
154k
  case Expr::CXXReinterpretCastExprClass:
358
154k
  case Expr::CXXConstCastExprClass:
359
154k
  case Expr::CXXAddrspaceCastExprClass:
360
154k
  case Expr::ObjCBridgedCastExprClass:
361
154k
  case Expr::BuiltinBitCastExprClass:
362
    // Only in C++ can casts be interesting at all.
363
154k
    if (!Lang.CPlusPlus) 
return Cl::CL_PRValue9
;
364
154k
    return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
365
154k
366
1.22k
  case Expr::CXXUnresolvedConstructExprClass:
367
1.22k
    return ClassifyUnnamed(Ctx,
368
1.22k
                      cast<CXXUnresolvedConstructExpr>(E)->getTypeAsWritten());
369
154k
370
75
  case Expr::BinaryConditionalOperatorClass: {
371
75
    if (!Lang.CPlusPlus) 
return Cl::CL_PRValue0
;
372
75
    const auto *co = cast<BinaryConditionalOperator>(E);
373
75
    return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
374
75
  }
375
75
376
1.53k
  case Expr::ConditionalOperatorClass: {
377
    // Once again, only C++ is interesting.
378
1.53k
    if (!Lang.CPlusPlus) 
return Cl::CL_PRValue1
;
379
1.53k
    const auto *co = cast<ConditionalOperator>(E);
380
1.53k
    return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
381
1.53k
  }
382
1.53k
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
0
393
    // Some C++ expressions are always class temporaries.
394
131k
  case Expr::CXXConstructExprClass:
395
131k
  case Expr::CXXInheritedCtorInitExprClass:
396
131k
  case Expr::CXXTemporaryObjectExprClass:
397
131k
  case Expr::LambdaExprClass:
398
131k
  case Expr::CXXStdInitializerListExprClass:
399
131k
    return Cl::CL_ClassTemporary;
400
131k
401
15
  case Expr::VAArgExprClass:
402
15
    return ClassifyUnnamed(Ctx, E->getType());
403
131k
404
0
  case Expr::DesignatedInitExprClass:
405
0
    return ClassifyInternal(Ctx, cast<DesignatedInitExpr>(E)->getInit());
406
131k
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
1
414
0
  case Expr::PackExpansionExprClass:
415
0
    return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
416
1
417
10.5k
  case Expr::MaterializeTemporaryExprClass:
418
10.5k
    return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
419
126
              ? Cl::CL_LValue
420
10.3k
              : Cl::CL_XValue;
421
1
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
12
433
149
  case Expr::CoawaitExprClass:
434
149
  case Expr::CoyieldExprClass:
435
149
    return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr());
436
0
  }
437
0
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
6.76M
451
6.76M
  if (isa<CXXMethodDecl>(D) && 
cast<CXXMethodDecl>(D)->isInstance()2.83k
)
452
1.57k
    return Cl::CL_MemberFunction;
453
6.75M
454
6.75M
  bool islvalue;
455
6.75M
  if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(D))
456
813
    islvalue = NTTParm->getType()->isReferenceType();
457
6.75M
  else
458
6.75M
    islvalue = isa<VarDecl>(D) || 
isa<FieldDecl>(D)9.57k
||
459
8.99k
               isa<IndirectFieldDecl>(D) ||
460
8.95k
               isa<BindingDecl>(D) ||
461
8.80k
               isa<MSGuidDecl>(D) ||
462
8.79k
               (Ctx.getLangOpts().CPlusPlus &&
463
8.27k
                (isa<FunctionDecl>(D) || 
isa<MSPropertyDecl>(D)2.48k
||
464
2.48k
                 isa<FunctionTemplateDecl>(D)));
465
6.75M
466
6.75M
  return islvalue ? Cl::CL_LValue : 
Cl::CL_PRValue3.80k
;
467
6.75M
}
468
469
/// ClassifyUnnamed - Return the classification of an expression yielding an
470
/// unnamed value of the given type. This applies in particular to function
471
/// calls and casts.
472
645k
static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
473
  // In C, function calls are always rvalues.
474
645k
  if (!Ctx.getLangOpts().CPlusPlus) 
return Cl::CL_PRValue12
;
475
645k
476
  // C++ [expr.call]p10: A function call is an lvalue if the result type is an
477
  //   lvalue reference type or an rvalue reference to function type, an xvalue
478
  //   if the result type is an rvalue reference to object type, and a prvalue
479
  //   otherwise.
480
645k
  if (T->isLValueReferenceType())
481
206k
    return Cl::CL_LValue;
482
438k
  const auto *RV = T->getAs<RValueReferenceType>();
483
438k
  if (!RV) // Could still be a class temporary, though.
484
325k
    return ClassifyTemporary(T);
485
113k
486
113k
  return RV->getPointeeType()->isFunctionType() ? 
Cl::CL_LValue3
:
Cl::CL_XValue113k
;
487
113k
}
488
489
448k
static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
490
448k
  if (E->getType() == Ctx.UnknownAnyTy)
491
0
    return (isa<FunctionDecl>(E->getMemberDecl())
492
0
              ? Cl::CL_PRValue : Cl::CL_LValue);
493
448k
494
  // Handle C first, it's easier.
495
448k
  if (!Ctx.getLangOpts().CPlusPlus) {
496
    // C99 6.5.2.3p3
497
    // For dot access, the expression is an lvalue if the first part is. For
498
    // arrow access, it always is an lvalue.
499
37.1k
    if (E->isArrow())
500
15.4k
      return Cl::CL_LValue;
501
    // ObjC property accesses are not lvalues, but get special treatment.
502
21.7k
    Expr *Base = E->getBase()->IgnoreParens();
503
21.7k
    if (isa<ObjCPropertyRefExpr>(Base))
504
0
      return Cl::CL_SubObjCPropertySetting;
505
21.7k
    return ClassifyInternal(Ctx, Base);
506
21.7k
  }
507
411k
508
411k
  NamedDecl *Member = E->getMemberDecl();
509
  // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
510
  // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
511
  //   E1.E2 is an lvalue.
512
411k
  if (const auto *Value = dyn_cast<ValueDecl>(Member))
513
411k
    if (Value->getType()->isReferenceType())
514
4.27k
      return Cl::CL_LValue;
515
406k
516
  //   Otherwise, one of the following rules applies.
517
  //   -- If E2 is a static member [...] then E1.E2 is an lvalue.
518
406k
  if (isa<VarDecl>(Member) && 
Member->getDeclContext()->isRecord()135
)
519
135
    return Cl::CL_LValue;
520
406k
521
  //   -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
522
  //      E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
523
  //      otherwise, it is a prvalue.
524
406k
  if (isa<FieldDecl>(Member)) {
525
    // *E1 is an lvalue
526
406k
    if (E->isArrow())
527
298k
      return Cl::CL_LValue;
528
108k
    Expr *Base = E->getBase()->IgnoreParenImpCasts();
529
108k
    if (isa<ObjCPropertyRefExpr>(Base))
530
0
      return Cl::CL_SubObjCPropertySetting;
531
108k
    return ClassifyInternal(Ctx, E->getBase());
532
108k
  }
533
97
534
  //   -- If E2 is a [...] member function, [...]
535
  //      -- If it refers to a static member function [...], then E1.E2 is an
536
  //         lvalue; [...]
537
  //      -- Otherwise [...] E1.E2 is a prvalue.
538
97
  if (const auto *Method = dyn_cast<CXXMethodDecl>(Member))
539
11
    return Method->isStatic() ? Cl::CL_LValue : 
Cl::CL_MemberFunction0
;
540
86
541
  //   -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
542
  // So is everything else we haven't handled yet.
543
86
  return Cl::CL_PRValue;
544
86
}
545
546
27.7k
static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
547
27.7k
  assert(Ctx.getLangOpts().CPlusPlus &&
548
27.7k
         "This is only relevant for C++.");
549
  // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
550
  // Except we override this for writes to ObjC properties.
551
27.7k
  if (E->isAssignmentOp())
552
852
    return (E->getLHS()->getObjectKind() == OK_ObjCProperty
553
852
              ? 
Cl::CL_PRValue0
: Cl::CL_LValue);
554
26.8k
555
  // C++ [expr.comma]p1: the result is of the same value category as its right
556
  //   operand, [...].
557
26.8k
  if (E->getOpcode() == BO_Comma)
558
888
    return ClassifyInternal(Ctx, E->getRHS());
559
25.9k
560
  // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
561
  //   is a pointer to a data member is of the same value category as its first
562
  //   operand.
563
25.9k
  if (E->getOpcode() == BO_PtrMemD)
564
87
    return (E->getType()->isFunctionType() ||
565
87
            E->hasPlaceholderType(BuiltinType::BoundMember))
566
0
             ? Cl::CL_MemberFunction
567
87
             : ClassifyInternal(Ctx, E->getLHS());
568
25.8k
569
  // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
570
  //   second operand is a pointer to data member and a prvalue otherwise.
571
25.8k
  if (E->getOpcode() == BO_PtrMemI)
572
38
    return (E->getType()->isFunctionType() ||
573
38
            E->hasPlaceholderType(BuiltinType::BoundMember))
574
0
             ? Cl::CL_MemberFunction
575
38
             : Cl::CL_LValue;
576
25.8k
577
  // All other binary operations are prvalues.
578
25.8k
  return Cl::CL_PRValue;
579
25.8k
}
580
581
static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
582
1.60k
                                     const Expr *False) {
583
1.60k
  assert(Ctx.getLangOpts().CPlusPlus &&
584
1.60k
         "This is only relevant for C++.");
585
1.60k
586
  // C++ [expr.cond]p2
587
  //   If either the second or the third operand has type (cv) void,
588
  //   one of the following shall hold:
589
1.60k
  if (True->getType()->isVoidType() || 
False->getType()->isVoidType()1.59k
) {
590
    // The second or the third operand (but not both) is a (possibly
591
    // parenthesized) throw-expression; the result is of the [...] value
592
    // category of the other.
593
39
    bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts());
594
39
    bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts());
595
39
    if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False)
596
39
                                           : (FalseIsThrow ? True : nullptr))
597
39
      return ClassifyInternal(Ctx, NonThrow);
598
0
599
    //   [Otherwise] the result [...] is a prvalue.
600
0
    return Cl::CL_PRValue;
601
0
  }
602
1.56k
603
  // Note that at this point, we have already performed all conversions
604
  // according to [expr.cond]p3.
605
  // C++ [expr.cond]p4: If the second and third operands are glvalues of the
606
  //   same value category [...], the result is of that [...] value category.
607
  // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
608
1.56k
  Cl::Kinds LCl = ClassifyInternal(Ctx, True),
609
1.56k
            RCl = ClassifyInternal(Ctx, False);
610
1.56k
  return LCl == RCl ? LCl : 
Cl::CL_PRValue0
;
611
1.56k
}
612
613
static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
614
1.94M
                                       Cl::Kinds Kind, SourceLocation &Loc) {
615
  // As a general rule, we only care about lvalues. But there are some rvalues
616
  // for which we want to generate special results.
617
1.94M
  if (Kind == Cl::CL_PRValue) {
618
    // For the sake of better diagnostics, we want to specifically recognize
619
    // use of the GCC cast-as-lvalue extension.
620
45
    if (const auto *CE = dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
621
16
      if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
622
15
        Loc = CE->getExprLoc();
623
15
        return Cl::CM_LValueCast;
624
15
      }
625
1.94M
    }
626
45
  }
627
1.94M
  if (Kind != Cl::CL_LValue)
628
82
    return Cl::CM_RValue;
629
1.94M
630
  // This is the lvalue case.
631
  // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
632
1.94M
  if (Ctx.getLangOpts().CPlusPlus && 
E->getType()->isFunctionType()1.42M
)
633
3
    return Cl::CM_Function;
634
1.94M
635
  // Assignment to a property in ObjC is an implicit setter access. But a
636
  // setter might not exist.
637
1.94M
  if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
638
0
    if (Expr->isImplicitProperty() &&
639
0
        Expr->getImplicitPropertySetter() == nullptr)
640
0
      return Cl::CM_NoSetterProperty;
641
1.94M
  }
642
1.94M
643
1.94M
  CanQualType CT = Ctx.getCanonicalType(E->getType());
644
  // Const stuff is obviously not modifiable.
645
1.94M
  if (CT.isConstQualified())
646
245
    return Cl::CM_ConstQualified;
647
1.94M
  if (Ctx.getLangOpts().OpenCL &&
648
1.84k
      CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant)
649
2
    return Cl::CM_ConstAddrSpace;
650
1.94M
651
  // Arrays are not modifiable, only their elements are.
652
1.94M
  if (CT->isArrayType())
653
2
    return Cl::CM_ArrayType;
654
  // Incomplete types are not modifiable.
655
1.94M
  if (CT->isIncompleteType())
656
2
    return Cl::CM_IncompleteType;
657
1.94M
658
  // Records with any const fields (recursively) are not modifiable.
659
1.94M
  if (const RecordType *R = CT->getAs<RecordType>())
660
2.02k
    if (R->hasConstFields())
661
11
      return Cl::CM_ConstQualifiedField;
662
1.94M
663
1.94M
  return Cl::CM_Modifiable;
664
1.94M
}
665
666
92.9k
Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
667
92.9k
  Classification VC = Classify(Ctx);
668
92.9k
  switch (VC.getKind()) {
669
90.7k
  case Cl::CL_LValue: return LV_Valid;
670
3
  case Cl::CL_XValue: return LV_InvalidExpression;
671
518
  case Cl::CL_Function: return LV_NotObjectType;
672
4
  case Cl::CL_Void: return LV_InvalidExpression;
673
14
  case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;
674
0
  case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
675
1.57k
  case Cl::CL_MemberFunction: return LV_MemberFunction;
676
0
  case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
677
136
  case Cl::CL_ClassTemporary: return LV_ClassTemporary;
678
2
  case Cl::CL_ArrayTemporary: return LV_ArrayTemporary;
679
0
  case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
680
41
  case Cl::CL_PRValue: return LV_InvalidExpression;
681
0
  }
682
0
  llvm_unreachable("Unhandled kind");
683
0
}
684
685
Expr::isModifiableLvalueResult
686
1.94M
Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
687
1.94M
  SourceLocation dummy;
688
1.93M
  Classification VC = ClassifyModifiable(Ctx, Loc ? *Loc : 
dummy7.32k
);
689
1.94M
  switch (VC.getKind()) {
690
1.94M
  case Cl::CL_LValue: break;
691
33
  case Cl::CL_XValue: return MLV_InvalidExpression;
692
1
  case Cl::CL_Function: return MLV_NotObjectType;
693
1
  case Cl::CL_Void: return MLV_InvalidExpression;
694
7
  case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;
695
8
  case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
696
0
  case Cl::CL_MemberFunction: return MLV_MemberFunction;
697
0
  case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
698
1
  case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
699
0
  case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary;
700
1
  case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
701
45
  case Cl::CL_PRValue:
702
45
    return VC.getModifiable() == Cl::CM_LValueCast ?
703
30
      
MLV_LValueCast15
: MLV_InvalidExpression;
704
1.94M
  }
705
1.94M
  assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
706
1.94M
  switch (VC.getModifiable()) {
707
0
  case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
708
1.94M
  case Cl::CM_Modifiable: return MLV_Valid;
709
0
  case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
710
3
  case Cl::CM_Function: return MLV_NotObjectType;
711
0
  case Cl::CM_LValueCast:
712
0
    llvm_unreachable("CM_LValueCast and CL_LValue don't match");
713
0
  case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
714
245
  case Cl::CM_ConstQualified: return MLV_ConstQualified;
715
11
  case Cl::CM_ConstQualifiedField: return MLV_ConstQualifiedField;
716
2
  case Cl::CM_ConstAddrSpace: return MLV_ConstAddrSpace;
717
2
  case Cl::CM_ArrayType: return MLV_ArrayType;
718
2
  case Cl::CM_IncompleteType: return MLV_IncompleteType;
719
0
  }
720
0
  llvm_unreachable("Unhandled modifiable type");
721
0
}