Coverage Report

Created: 2019-07-24 05:18

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