Coverage Report

Created: 2021-09-21 08:58

/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
9.23M
Cl Expr::ClassifyImpl(ASTContext &Ctx, SourceLocation *Loc) const {
38
9.23M
  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
9.23M
  if (!Ctx.getLangOpts().CPlusPlus) {
44
    // Thus, no functions.
45
581k
    if (TR->isFunctionType() || 
TR == Ctx.OverloadTy581k
)
46
530
      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
581k
    else if (TR->isVoidType() && 
!TR.hasQualifiers()28
)
51
26
      kind = (kind == Cl::CL_LValue ? 
Cl::CL_AddressableVoid21
:
Cl::CL_Void5
);
52
581k
  }
53
54
  // Enable this assertion for testing.
55
9.23M
  switch (kind) {
56
8.29M
  case Cl::CL_LValue:
57
8.29M
    assert(isLValue());
58
0
    break;
59
188k
  case Cl::CL_XValue:
60
188k
    assert(isXValue());
61
0
    break;
62
530
  case Cl::CL_Function:
63
535
  case Cl::CL_Void:
64
556
  case Cl::CL_AddressableVoid:
65
564
  case Cl::CL_DuplicateVectorComponents:
66
2.21k
  case Cl::CL_MemberFunction:
67
2.21k
  case Cl::CL_SubObjCPropertySetting:
68
551k
  case Cl::CL_ClassTemporary:
69
551k
  case Cl::CL_ArrayTemporary:
70
551k
  case Cl::CL_ObjCMessageRValue:
71
748k
  case Cl::CL_PRValue:
72
748k
    assert(isPRValue());
73
0
    break;
74
9.23M
  }
75
76
9.23M
  Cl::ModifiableType modifiable = Cl::CM_Untested;
77
9.23M
  if (Loc)
78
2.03M
    modifiable = IsModifiable(Ctx, this, kind, *Loc);
79
9.23M
  return Classification(kind, modifiable);
80
9.23M
}
81
82
/// Classify an expression which creates a temporary, based on its type.
83
467k
static Cl::Kinds ClassifyTemporary(QualType T) {
84
467k
  if (T->isRecordType())
85
416k
    return Cl::CL_ClassTemporary;
86
51.1k
  if (T->isArrayType())
87
40
    return Cl::CL_ArrayTemporary;
88
89
  // No special classification: these don't behave differently from normal
90
  // prvalues.
91
51.0k
  return Cl::CL_PRValue;
92
51.1k
}
93
94
static Cl::Kinds ClassifyExprValueKind(const LangOptions &Lang,
95
                                       const Expr *E,
96
486k
                                       ExprValueKind Kind) {
97
486k
  switch (Kind) {
98
113k
  case VK_PRValue:
99
113k
    return Lang.CPlusPlus ? 
ClassifyTemporary(E->getType())113k
:
Cl::CL_PRValue9
;
100
313k
  case VK_LValue:
101
313k
    return Cl::CL_LValue;
102
59.8k
  case VK_XValue:
103
59.8k
    return Cl::CL_XValue;
104
486k
  }
105
0
  llvm_unreachable("Invalid value category of implicit cast.");
106
0
}
107
108
9.74M
static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) {
109
  // This function takes the first stab at classifying expressions.
110
9.74M
  const LangOptions &Lang = Ctx.getLangOpts();
111
112
9.74M
  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
12.4k
  case Expr::StringLiteralClass:
124
    // @encode is equivalent to its string
125
12.4k
  case Expr::ObjCEncodeExprClass:
126
    // __func__ and friends are too.
127
12.4k
  case Expr::PredefinedExprClass:
128
    // Property references are lvalues
129
12.4k
  case Expr::ObjCSubscriptRefExprClass:
130
12.4k
  case Expr::ObjCPropertyRefExprClass:
131
    // C++ [expr.typeid]p1: The result of a typeid expression is an lvalue of...
132
19.3k
  case Expr::CXXTypeidExprClass:
133
19.5k
  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
19.9k
  case Expr::UnresolvedLookupExprClass:
137
19.9k
  case Expr::UnresolvedMemberExprClass:
138
19.9k
  case Expr::TypoExprClass:
139
19.9k
  case Expr::DependentCoawaitExprClass:
140
19.9k
  case Expr::CXXDependentScopeMemberExprClass:
141
19.9k
  case Expr::DependentScopeDeclRefExprClass:
142
    // ObjC instance variables are lvalues
143
    // FIXME: ObjC++0x might have different rules
144
21.6k
  case Expr::ObjCIvarRefExprClass:
145
21.6k
  case Expr::FunctionParmPackExprClass:
146
21.6k
  case Expr::MSPropertyRefExprClass:
147
21.6k
  case Expr::MSPropertySubscriptExprClass:
148
21.6k
  case Expr::OMPArraySectionExprClass:
149
21.6k
  case Expr::OMPArrayShapingExprClass:
150
21.6k
  case Expr::OMPIteratorExprClass:
151
21.6k
    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.25k
  case Expr::CompoundLiteralExprClass:
156
2.25k
    return !E->isLValue() ? 
ClassifyTemporary(E->getType())2.21k
:
Cl::CL_LValue42
;
157
158
    // Expressions that are prvalues.
159
762
  case Expr::CXXBoolLiteralExprClass:
160
762
  case Expr::CXXPseudoDestructorExprClass:
161
1.01k
  case Expr::UnaryExprOrTypeTraitExprClass:
162
5.36k
  case Expr::CXXNewExprClass:
163
5.39k
  case Expr::CXXThisExprClass:
164
8.05k
  case Expr::CXXNullPtrLiteralExprClass:
165
8.06k
  case Expr::ImaginaryLiteralClass:
166
8.08k
  case Expr::GNUNullExprClass:
167
8.08k
  case Expr::OffsetOfExprClass:
168
8.08k
  case Expr::CXXThrowExprClass:
169
8.08k
  case Expr::ShuffleVectorExprClass:
170
8.08k
  case Expr::ConvertVectorExprClass:
171
104k
  case Expr::IntegerLiteralClass:
172
104k
  case Expr::FixedPointLiteralClass:
173
105k
  case Expr::CharacterLiteralClass:
174
105k
  case Expr::AddrLabelExprClass:
175
105k
  case Expr::CXXDeleteExprClass:
176
105k
  case Expr::ImplicitValueInitExprClass:
177
105k
  case Expr::BlockExprClass:
178
110k
  case Expr::FloatingLiteralClass:
179
110k
  case Expr::CXXNoexceptExprClass:
180
111k
  case Expr::CXXScalarValueInitExprClass:
181
111k
  case Expr::TypeTraitExprClass:
182
111k
  case Expr::ArrayTypeTraitExprClass:
183
111k
  case Expr::ExpressionTraitExprClass:
184
111k
  case Expr::ObjCSelectorExprClass:
185
111k
  case Expr::ObjCProtocolExprClass:
186
111k
  case Expr::ObjCStringLiteralClass:
187
111k
  case Expr::ObjCBoxedExprClass:
188
111k
  case Expr::ObjCArrayLiteralClass:
189
111k
  case Expr::ObjCDictionaryLiteralClass:
190
111k
  case Expr::ObjCBoolLiteralExprClass:
191
111k
  case Expr::ObjCAvailabilityCheckExprClass:
192
111k
  case Expr::ParenListExprClass:
193
111k
  case Expr::SizeOfPackExprClass:
194
111k
  case Expr::SubstNonTypeTemplateParmPackExprClass:
195
111k
  case Expr::AsTypeExprClass:
196
111k
  case Expr::ObjCIndirectCopyRestoreExprClass:
197
111k
  case Expr::AtomicExprClass:
198
111k
  case Expr::CXXFoldExprClass:
199
111k
  case Expr::ArrayInitLoopExprClass:
200
111k
  case Expr::ArrayInitIndexExprClass:
201
111k
  case Expr::NoInitExprClass:
202
111k
  case Expr::DesignatedInitUpdateExprClass:
203
111k
  case Expr::SourceLocExprClass:
204
111k
  case Expr::ConceptSpecializationExprClass:
205
111k
  case Expr::RequiresExprClass:
206
111k
    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
311k
  case Expr::ArraySubscriptExprClass:
221
311k
    if (cast<ArraySubscriptExpr>(E)->getBase()->getType()->isVectorType())
222
11.4k
      return ClassifyInternal(Ctx, cast<ArraySubscriptExpr>(E)->getBase());
223
300k
    if (Lang.CPlusPlus11) {
224
      // Step over the array-to-pointer decay if present, but not over the
225
      // temporary materialization.
226
285k
      auto *Base = cast<ArraySubscriptExpr>(E)->getBase()->IgnoreImpCasts();
227
285k
      if (Base->getType()->isArrayType())
228
252k
        return ClassifyInternal(Ctx, Base);
229
285k
    }
230
48.2k
    return Cl::CL_LValue;
231
232
  // Subscripting matrix types behaves like member accesses.
233
26
  case Expr::MatrixSubscriptExprClass:
234
26
    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
7.27M
  case Expr::DeclRefExprClass:
239
7.27M
    if (E->getType() == Ctx.UnknownAnyTy)
240
0
      return isa<FunctionDecl>(cast<DeclRefExpr>(E)->getDecl())
241
0
               ? Cl::CL_PRValue : Cl::CL_LValue;
242
7.27M
    return ClassifyDecl(Ctx, cast<DeclRefExpr>(E)->getDecl());
243
244
    // Member access is complex.
245
482k
  case Expr::MemberExprClass:
246
482k
    return ClassifyMemberExpr(Ctx, cast<MemberExpr>(E));
247
248
96.5k
  case Expr::UnaryOperatorClass:
249
96.5k
    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
91.7k
    case UO_Deref:
254
91.7k
      return Cl::CL_LValue;
255
256
      // GNU extensions, simply look through them.
257
101
    case UO_Extension:
258
101
      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
110
    case UO_Real:
263
229
    case UO_Imag: {
264
229
      const Expr *Op = cast<UnaryOperator>(E)->getSubExpr()->IgnoreParens();
265
229
      Cl::Kinds K = ClassifyInternal(Ctx, Op);
266
229
      if (K != Cl::CL_LValue) 
return K2
;
267
268
227
      if (isa<ObjCPropertyRefExpr>(Op))
269
0
        return Cl::CL_SubObjCPropertySetting;
270
227
      return Cl::CL_LValue;
271
227
    }
272
273
      // C++ [expr.pre.incr]p1: The result is the updated operand; it is an
274
      //   lvalue, [...]
275
      // Not so in C.
276
129
    case UO_PreInc:
277
133
    case UO_PreDec:
278
133
      return Lang.CPlusPlus ? Cl::CL_LValue : 
Cl::CL_PRValue0
;
279
280
4.33k
    default:
281
4.33k
      return Cl::CL_PRValue;
282
96.5k
    }
283
284
271
  case Expr::RecoveryExprClass:
285
410k
  case Expr::OpaqueValueExprClass:
286
410k
    return ClassifyExprValueKind(Lang, E, E->getValueKind());
287
288
    // Pseudo-object expressions can produce l-values with reference magic.
289
566
  case Expr::PseudoObjectExprClass:
290
566
    return ClassifyExprValueKind(Lang, E,
291
566
                                 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
75.5k
  case Expr::ImplicitCastExprClass:
296
75.5k
    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
58.0k
  case Expr::ParenExprClass:
301
58.0k
    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
28.9k
  case Expr::BinaryOperatorClass:
312
29.1k
  case Expr::CompoundAssignOperatorClass:
313
    // C doesn't have any binary expressions that are lvalues.
314
29.1k
    if (Lang.CPlusPlus)
315
29.1k
      return ClassifyBinaryOp(Ctx, cast<BinaryOperator>(E));
316
3
    return Cl::CL_PRValue;
317
318
272k
  case Expr::CallExprClass:
319
410k
  case Expr::CXXOperatorCallExprClass:
320
514k
  case Expr::CXXMemberCallExprClass:
321
514k
  case Expr::UserDefinedLiteralClass:
322
514k
  case Expr::CUDAKernelCallExprClass:
323
514k
    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
294
  case Expr::CXXDefaultArgExprClass:
344
294
    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
44.3k
  case Expr::CXXBindTemporaryExprClass:
352
44.3k
    return ClassifyInternal(Ctx, cast<CXXBindTemporaryExpr>(E)->getSubExpr());
353
354
    // And the cleanups guard.
355
522
  case Expr::ExprWithCleanupsClass:
356
522
    return ClassifyInternal(Ctx, cast<ExprWithCleanups>(E)->getSubExpr());
357
358
    // Casts depend completely on the target type. All casts work the same.
359
5.54k
  case Expr::CStyleCastExprClass:
360
97.9k
  case Expr::CXXFunctionalCastExprClass:
361
164k
  case Expr::CXXStaticCastExprClass:
362
164k
  case Expr::CXXDynamicCastExprClass:
363
164k
  case Expr::CXXReinterpretCastExprClass:
364
164k
  case Expr::CXXConstCastExprClass:
365
164k
  case Expr::CXXAddrspaceCastExprClass:
366
164k
  case Expr::ObjCBridgedCastExprClass:
367
164k
  case Expr::BuiltinBitCastExprClass:
368
    // Only in C++ can casts be interesting at all.
369
164k
    if (!Lang.CPlusPlus) 
return Cl::CL_PRValue8
;
370
164k
    return ClassifyUnnamed(Ctx, cast<ExplicitCastExpr>(E)->getTypeAsWritten());
371
372
1.27k
  case Expr::CXXUnresolvedConstructExprClass:
373
1.27k
    return ClassifyUnnamed(Ctx,
374
1.27k
                      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
1.69k
  case Expr::ConditionalOperatorClass: {
383
    // Once again, only C++ is interesting.
384
1.69k
    if (!Lang.CPlusPlus) 
return Cl::CL_PRValue1
;
385
1.69k
    const auto *co = cast<ConditionalOperator>(E);
386
1.69k
    return ClassifyConditional(Ctx, co->getTrueExpr(), co->getFalseExpr());
387
1.69k
  }
388
389
    // ObjC message sends are effectively function calls, if the target function
390
    // is known.
391
120
  case Expr::ObjCMessageExprClass:
392
120
    if (const ObjCMethodDecl *Method =
393
120
          cast<ObjCMessageExpr>(E)->getMethodDecl()) {
394
120
      Cl::Kinds kind = ClassifyUnnamed(Ctx, Method->getReturnType());
395
120
      return (kind == Cl::CL_PRValue) ? 
Cl::CL_ObjCMessageRValue7
:
kind113
;
396
120
    }
397
0
    return Cl::CL_PRValue;
398
399
    // Some C++ expressions are always class temporaries.
400
9.28k
  case Expr::CXXConstructExprClass:
401
9.28k
  case Expr::CXXInheritedCtorInitExprClass:
402
114k
  case Expr::CXXTemporaryObjectExprClass:
403
132k
  case Expr::LambdaExprClass:
404
132k
  case Expr::CXXStdInitializerListExprClass:
405
132k
    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
136
  case Expr::StmtExprClass: {
414
136
    const CompoundStmt *S = cast<StmtExpr>(E)->getSubStmt();
415
136
    if (const auto *LastExpr = dyn_cast_or_null<Expr>(S->body_back()))
416
135
      return ClassifyUnnamed(Ctx, LastExpr->getType());
417
1
    return Cl::CL_PRValue;
418
136
  }
419
420
0
  case Expr::PackExpansionExprClass:
421
0
    return ClassifyInternal(Ctx, cast<PackExpansionExpr>(E)->getPattern());
422
423
11.2k
  case Expr::MaterializeTemporaryExprClass:
424
11.2k
    return cast<MaterializeTemporaryExpr>(E)->isBoundToLvalueReference()
425
11.2k
              ? 
Cl::CL_LValue127
426
11.2k
              : 
Cl::CL_XValue11.1k
;
427
428
32
  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
32
    if (E->isPRValue())
434
16
      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
20
  case Expr::CoawaitExprClass:
440
149
  case Expr::CoyieldExprClass:
441
149
    return ClassifyInternal(Ctx, cast<CoroutineSuspendExpr>(E)->getResumeExpr());
442
0
  case Expr::SYCLUniqueStableNameExprClass:
443
0
    return Cl::CL_PRValue;
444
0
    break;
445
9.74M
  }
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
7.27M
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
7.27M
  if (isa<CXXMethodDecl>(D) && 
cast<CXXMethodDecl>(D)->isInstance()2.98k
)
461
1.64k
    return Cl::CL_MemberFunction;
462
463
7.27M
  bool islvalue;
464
7.27M
  if (const auto *NTTParm = dyn_cast<NonTypeTemplateParmDecl>(D))
465
777
    islvalue = NTTParm->getType()->isReferenceType() ||
466
777
               
NTTParm->getType()->isRecordType()763
;
467
7.27M
  else
468
7.27M
    islvalue = isa<VarDecl>(D) || 
isa<FieldDecl>(D)9.02k
||
469
7.27M
               
isa<IndirectFieldDecl>(D)8.36k
||
470
7.27M
               
isa<BindingDecl>(D)8.32k
||
471
7.27M
               
isa<MSGuidDecl>(D)8.16k
||
472
7.27M
               
isa<TemplateParamObjectDecl>(D)8.15k
||
473
7.27M
               
(8.09k
Ctx.getLangOpts().CPlusPlus8.09k
&&
474
8.09k
                
(7.56k
isa<FunctionDecl>(D)7.56k
||
isa<MSPropertyDecl>(D)1.69k
||
475
7.56k
                 
isa<FunctionTemplateDecl>(D)1.68k
));
476
477
7.27M
  return islvalue ? 
Cl::CL_LValue7.27M
:
Cl::CL_PRValue2.96k
;
478
7.27M
}
479
480
/// ClassifyUnnamed - Return the classification of an expression yielding an
481
/// unnamed value of the given type. This applies in particular to function
482
/// calls and casts.
483
681k
static Cl::Kinds ClassifyUnnamed(ASTContext &Ctx, QualType T) {
484
  // In C, function calls are always rvalues.
485
681k
  if (!Ctx.getLangOpts().CPlusPlus) 
return Cl::CL_PRValue12
;
486
487
  // C++ [expr.call]p10: A function call is an lvalue if the result type is an
488
  //   lvalue reference type or an rvalue reference to function type, an xvalue
489
  //   if the result type is an rvalue reference to object type, and a prvalue
490
  //   otherwise.
491
681k
  if (T->isLValueReferenceType())
492
210k
    return Cl::CL_LValue;
493
470k
  const auto *RV = T->getAs<RValueReferenceType>();
494
470k
  if (!RV) // Could still be a class temporary, though.
495
352k
    return ClassifyTemporary(T);
496
497
117k
  return RV->getPointeeType()->isFunctionType() ? 
Cl::CL_LValue11
:
Cl::CL_XValue117k
;
498
470k
}
499
500
482k
static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) {
501
482k
  if (E->getType() == Ctx.UnknownAnyTy)
502
0
    return (isa<FunctionDecl>(E->getMemberDecl())
503
0
              ? Cl::CL_PRValue : Cl::CL_LValue);
504
505
  // Handle C first, it's easier.
506
482k
  if (!Ctx.getLangOpts().CPlusPlus) {
507
    // C99 6.5.2.3p3
508
    // For dot access, the expression is an lvalue if the first part is. For
509
    // arrow access, it always is an lvalue.
510
32.9k
    if (E->isArrow())
511
16.2k
      return Cl::CL_LValue;
512
    // ObjC property accesses are not lvalues, but get special treatment.
513
16.6k
    Expr *Base = E->getBase()->IgnoreParens();
514
16.6k
    if (isa<ObjCPropertyRefExpr>(Base))
515
0
      return Cl::CL_SubObjCPropertySetting;
516
16.6k
    return ClassifyInternal(Ctx, Base);
517
16.6k
  }
518
519
449k
  NamedDecl *Member = E->getMemberDecl();
520
  // C++ [expr.ref]p3: E1->E2 is converted to the equivalent form (*(E1)).E2.
521
  // C++ [expr.ref]p4: If E2 is declared to have type "reference to T", then
522
  //   E1.E2 is an lvalue.
523
449k
  if (const auto *Value = dyn_cast<ValueDecl>(Member))
524
449k
    if (Value->getType()->isReferenceType())
525
4.06k
      return Cl::CL_LValue;
526
527
  //   Otherwise, one of the following rules applies.
528
  //   -- If E2 is a static member [...] then E1.E2 is an lvalue.
529
445k
  if (isa<VarDecl>(Member) && 
Member->getDeclContext()->isRecord()145
)
530
145
    return Cl::CL_LValue;
531
532
  //   -- If E2 is a non-static data member [...]. If E1 is an lvalue, then
533
  //      E1.E2 is an lvalue; if E1 is an xvalue, then E1.E2 is an xvalue;
534
  //      otherwise, it is a prvalue.
535
445k
  if (isa<FieldDecl>(Member)) {
536
    // *E1 is an lvalue
537
445k
    if (E->isArrow())
538
317k
      return Cl::CL_LValue;
539
128k
    Expr *Base = E->getBase()->IgnoreParenImpCasts();
540
128k
    if (isa<ObjCPropertyRefExpr>(Base))
541
0
      return Cl::CL_SubObjCPropertySetting;
542
128k
    return ClassifyInternal(Ctx, E->getBase());
543
128k
  }
544
545
  //   -- If E2 is a [...] member function, [...]
546
  //      -- If it refers to a static member function [...], then E1.E2 is an
547
  //         lvalue; [...]
548
  //      -- Otherwise [...] E1.E2 is a prvalue.
549
99
  if (const auto *Method = dyn_cast<CXXMethodDecl>(Member))
550
13
    return Method->isStatic() ? Cl::CL_LValue : 
Cl::CL_MemberFunction0
;
551
552
  //   -- If E2 is a member enumerator [...], the expression E1.E2 is a prvalue.
553
  // So is everything else we haven't handled yet.
554
86
  return Cl::CL_PRValue;
555
99
}
556
557
29.1k
static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) {
558
29.1k
  assert(Ctx.getLangOpts().CPlusPlus &&
559
29.1k
         "This is only relevant for C++.");
560
  // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand.
561
  // Except we override this for writes to ObjC properties.
562
29.1k
  if (E->isAssignmentOp())
563
918
    return (E->getLHS()->getObjectKind() == OK_ObjCProperty
564
918
              ? 
Cl::CL_PRValue0
: Cl::CL_LValue);
565
566
  // C++ [expr.comma]p1: the result is of the same value category as its right
567
  //   operand, [...].
568
28.1k
  if (E->getOpcode() == BO_Comma)
569
894
    return ClassifyInternal(Ctx, E->getRHS());
570
571
  // C++ [expr.mptr.oper]p6: The result of a .* expression whose second operand
572
  //   is a pointer to a data member is of the same value category as its first
573
  //   operand.
574
27.2k
  if (E->getOpcode() == BO_PtrMemD)
575
93
    return (E->getType()->isFunctionType() ||
576
93
            E->hasPlaceholderType(BuiltinType::BoundMember))
577
93
             ? 
Cl::CL_MemberFunction0
578
93
             : ClassifyInternal(Ctx, E->getLHS());
579
580
  // C++ [expr.mptr.oper]p6: The result of an ->* expression is an lvalue if its
581
  //   second operand is a pointer to data member and a prvalue otherwise.
582
27.1k
  if (E->getOpcode() == BO_PtrMemI)
583
58
    return (E->getType()->isFunctionType() ||
584
58
            E->hasPlaceholderType(BuiltinType::BoundMember))
585
58
             ? 
Cl::CL_MemberFunction0
586
58
             : Cl::CL_LValue;
587
588
  // All other binary operations are prvalues.
589
27.1k
  return Cl::CL_PRValue;
590
27.1k
}
591
592
static Cl::Kinds ClassifyConditional(ASTContext &Ctx, const Expr *True,
593
1.78k
                                     const Expr *False) {
594
1.78k
  assert(Ctx.getLangOpts().CPlusPlus &&
595
1.78k
         "This is only relevant for C++.");
596
597
  // C++ [expr.cond]p2
598
  //   If either the second or the third operand has type (cv) void,
599
  //   one of the following shall hold:
600
1.78k
  if (True->getType()->isVoidType() || 
False->getType()->isVoidType()1.75k
) {
601
    // The second or the third operand (but not both) is a (possibly
602
    // parenthesized) throw-expression; the result is of the [...] value
603
    // category of the other.
604
47
    bool TrueIsThrow = isa<CXXThrowExpr>(True->IgnoreParenImpCasts());
605
47
    bool FalseIsThrow = isa<CXXThrowExpr>(False->IgnoreParenImpCasts());
606
47
    if (const Expr *NonThrow = TrueIsThrow ? (FalseIsThrow ? nullptr : False)
607
47
                                           : (FalseIsThrow ? True : nullptr))
608
47
      return ClassifyInternal(Ctx, NonThrow);
609
610
    //   [Otherwise] the result [...] is a prvalue.
611
0
    return Cl::CL_PRValue;
612
47
  }
613
614
  // Note that at this point, we have already performed all conversions
615
  // according to [expr.cond]p3.
616
  // C++ [expr.cond]p4: If the second and third operands are glvalues of the
617
  //   same value category [...], the result is of that [...] value category.
618
  // C++ [expr.cond]p5: Otherwise, the result is a prvalue.
619
1.73k
  Cl::Kinds LCl = ClassifyInternal(Ctx, True),
620
1.73k
            RCl = ClassifyInternal(Ctx, False);
621
1.73k
  return LCl == RCl ? LCl : 
Cl::CL_PRValue0
;
622
1.78k
}
623
624
static Cl::ModifiableType IsModifiable(ASTContext &Ctx, const Expr *E,
625
2.03M
                                       Cl::Kinds Kind, SourceLocation &Loc) {
626
  // As a general rule, we only care about lvalues. But there are some rvalues
627
  // for which we want to generate special results.
628
2.03M
  if (Kind == Cl::CL_PRValue) {
629
    // For the sake of better diagnostics, we want to specifically recognize
630
    // use of the GCC cast-as-lvalue extension.
631
45
    if (const auto *CE = dyn_cast<ExplicitCastExpr>(E->IgnoreParens())) {
632
16
      if (CE->getSubExpr()->IgnoreParenImpCasts()->isLValue()) {
633
15
        Loc = CE->getExprLoc();
634
15
        return Cl::CM_LValueCast;
635
15
      }
636
16
    }
637
45
  }
638
2.03M
  if (Kind != Cl::CL_LValue)
639
82
    return Cl::CM_RValue;
640
641
  // This is the lvalue case.
642
  // Functions are lvalues in C++, but not modifiable. (C++ [basic.lval]p6)
643
2.03M
  if (Ctx.getLangOpts().CPlusPlus && 
E->getType()->isFunctionType()1.48M
)
644
3
    return Cl::CM_Function;
645
646
  // Assignment to a property in ObjC is an implicit setter access. But a
647
  // setter might not exist.
648
2.03M
  if (const auto *Expr = dyn_cast<ObjCPropertyRefExpr>(E)) {
649
0
    if (Expr->isImplicitProperty() &&
650
0
        Expr->getImplicitPropertySetter() == nullptr)
651
0
      return Cl::CM_NoSetterProperty;
652
0
  }
653
654
2.03M
  CanQualType CT = Ctx.getCanonicalType(E->getType());
655
  // Const stuff is obviously not modifiable.
656
2.03M
  if (CT.isConstQualified())
657
257
    return Cl::CM_ConstQualified;
658
2.03M
  if (Ctx.getLangOpts().OpenCL &&
659
2.03M
      
CT.getQualifiers().getAddressSpace() == LangAS::opencl_constant2.32k
)
660
2
    return Cl::CM_ConstAddrSpace;
661
662
  // Arrays are not modifiable, only their elements are.
663
2.03M
  if (CT->isArrayType())
664
2
    return Cl::CM_ArrayType;
665
  // Incomplete types are not modifiable.
666
2.03M
  if (CT->isIncompleteType())
667
2
    return Cl::CM_IncompleteType;
668
669
  // Records with any const fields (recursively) are not modifiable.
670
2.03M
  if (const RecordType *R = CT->getAs<RecordType>())
671
1.72k
    if (R->hasConstFields())
672
11
      return Cl::CM_ConstQualifiedField;
673
674
2.03M
  return Cl::CM_Modifiable;
675
2.03M
}
676
677
108k
Expr::LValueClassification Expr::ClassifyLValue(ASTContext &Ctx) const {
678
108k
  Classification VC = Classify(Ctx);
679
108k
  switch (VC.getKind()) {
680
106k
  case Cl::CL_LValue: return LV_Valid;
681
3
  case Cl::CL_XValue: return LV_InvalidExpression;
682
529
  case Cl::CL_Function: return LV_NotObjectType;
683
4
  case Cl::CL_Void: return LV_InvalidExpression;
684
14
  case Cl::CL_AddressableVoid: return LV_IncompleteVoidType;
685
0
  case Cl::CL_DuplicateVectorComponents: return LV_DuplicateVectorComponents;
686
1.64k
  case Cl::CL_MemberFunction: return LV_MemberFunction;
687
0
  case Cl::CL_SubObjCPropertySetting: return LV_SubObjCPropertySetting;
688
138
  case Cl::CL_ClassTemporary: return LV_ClassTemporary;
689
2
  case Cl::CL_ArrayTemporary: return LV_ArrayTemporary;
690
0
  case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
691
41
  case Cl::CL_PRValue: return LV_InvalidExpression;
692
108k
  }
693
0
  llvm_unreachable("Unhandled kind");
694
0
}
695
696
Expr::isModifiableLvalueResult
697
2.03M
Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const {
698
2.03M
  SourceLocation dummy;
699
2.03M
  Classification VC = ClassifyModifiable(Ctx, Loc ? 
*Loc2.02M
:
dummy8.28k
);
700
2.03M
  switch (VC.getKind()) {
701
2.03M
  case Cl::CL_LValue: break;
702
33
  case Cl::CL_XValue: return MLV_InvalidExpression;
703
1
  case Cl::CL_Function: return MLV_NotObjectType;
704
1
  case Cl::CL_Void: return MLV_InvalidExpression;
705
7
  case Cl::CL_AddressableVoid: return MLV_IncompleteVoidType;
706
8
  case Cl::CL_DuplicateVectorComponents: return MLV_DuplicateVectorComponents;
707
0
  case Cl::CL_MemberFunction: return MLV_MemberFunction;
708
0
  case Cl::CL_SubObjCPropertySetting: return MLV_SubObjCPropertySetting;
709
1
  case Cl::CL_ClassTemporary: return MLV_ClassTemporary;
710
0
  case Cl::CL_ArrayTemporary: return MLV_ArrayTemporary;
711
1
  case Cl::CL_ObjCMessageRValue: return MLV_InvalidMessageExpression;
712
45
  case Cl::CL_PRValue:
713
45
    return VC.getModifiable() == Cl::CM_LValueCast ?
714
30
      
MLV_LValueCast15
: MLV_InvalidExpression;
715
2.03M
  }
716
2.03M
  assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
717
0
  switch (VC.getModifiable()) {
718
0
  case Cl::CM_Untested: llvm_unreachable("Did not test modifiability");
719
2.03M
  case Cl::CM_Modifiable: return MLV_Valid;
720
0
  case Cl::CM_RValue: llvm_unreachable("CM_RValue and CL_LValue don't match");
721
3
  case Cl::CM_Function: return MLV_NotObjectType;
722
0
  case Cl::CM_LValueCast:
723
0
    llvm_unreachable("CM_LValueCast and CL_LValue don't match");
724
0
  case Cl::CM_NoSetterProperty: return MLV_NoSetterProperty;
725
257
  case Cl::CM_ConstQualified: return MLV_ConstQualified;
726
11
  case Cl::CM_ConstQualifiedField: return MLV_ConstQualifiedField;
727
2
  case Cl::CM_ConstAddrSpace: return MLV_ConstAddrSpace;
728
2
  case Cl::CM_ArrayType: return MLV_ArrayType;
729
2
  case Cl::CM_IncompleteType: return MLV_IncompleteType;
730
2.03M
  }
731
0
  llvm_unreachable("Unhandled modifiable type");
732
0
}