Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaExprMember.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SemaExprMember.cpp - Semantic Analysis for Expressions -----------===//
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 semantic analysis member access expressions.
10
//
11
//===----------------------------------------------------------------------===//
12
#include "clang/Sema/Overload.h"
13
#include "clang/AST/ASTLambda.h"
14
#include "clang/AST/DeclCXX.h"
15
#include "clang/AST/DeclObjC.h"
16
#include "clang/AST/DeclTemplate.h"
17
#include "clang/AST/ExprCXX.h"
18
#include "clang/AST/ExprObjC.h"
19
#include "clang/Lex/Preprocessor.h"
20
#include "clang/Sema/Lookup.h"
21
#include "clang/Sema/Scope.h"
22
#include "clang/Sema/ScopeInfo.h"
23
#include "clang/Sema/SemaInternal.h"
24
25
using namespace clang;
26
using namespace sema;
27
28
typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> BaseSet;
29
30
/// Determines if the given class is provably not derived from all of
31
/// the prospective base classes.
32
static bool isProvablyNotDerivedFrom(Sema &SemaRef, CXXRecordDecl *Record,
33
694k
                                     const BaseSet &Bases) {
34
700k
  auto BaseIsNotInSet = [&Bases](const CXXRecordDecl *Base) {
35
700k
    return !Bases.count(Base->getCanonicalDecl());
36
700k
  };
37
694k
  return BaseIsNotInSet(Record) && 
Record->forallBases(BaseIsNotInSet)6.47k
;
38
694k
}
39
40
enum IMAKind {
41
  /// The reference is definitely not an instance member access.
42
  IMA_Static,
43
44
  /// The reference may be an implicit instance member access.
45
  IMA_Mixed,
46
47
  /// The reference may be to an instance member, but it might be invalid if
48
  /// so, because the context is not an instance method.
49
  IMA_Mixed_StaticContext,
50
51
  /// The reference may be to an instance member, but it is invalid if
52
  /// so, because the context is from an unrelated class.
53
  IMA_Mixed_Unrelated,
54
55
  /// The reference is definitely an implicit instance member access.
56
  IMA_Instance,
57
58
  /// The reference may be to an unresolved using declaration.
59
  IMA_Unresolved,
60
61
  /// The reference is a contextually-permitted abstract member reference.
62
  IMA_Abstract,
63
64
  /// The reference may be to an unresolved using declaration and the
65
  /// context is not an instance method.
66
  IMA_Unresolved_StaticContext,
67
68
  // The reference refers to a field which is not a member of the containing
69
  // class, which is allowed because we're in C++11 mode and the context is
70
  // unevaluated.
71
  IMA_Field_Uneval_Context,
72
73
  /// All possible referrents are instance members and the current
74
  /// context is not an instance method.
75
  IMA_Error_StaticContext,
76
77
  /// All possible referrents are instance members of an unrelated
78
  /// class.
79
  IMA_Error_Unrelated
80
};
81
82
/// The given lookup names class member(s) and is not being used for
83
/// an address-of-member expression.  Classify the type of access
84
/// according to whether it's possible that this reference names an
85
/// instance member.  This is best-effort in dependent contexts; it is okay to
86
/// conservatively answer "yes", in which case some errors will simply
87
/// not be caught until template-instantiation.
88
static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
89
1.61M
                                            const LookupResult &R) {
90
1.61M
  assert(!R.empty() && (*R.begin())->isCXXClassMember());
91
1.61M
92
1.61M
  DeclContext *DC = SemaRef.getFunctionLevelDeclContext();
93
1.61M
94
1.61M
  bool isStaticContext = SemaRef.CXXThisTypeOverride.isNull() &&
95
1.61M
    
(1.61M
!isa<CXXMethodDecl>(DC)1.61M
||
cast<CXXMethodDecl>(DC)->isStatic()942k
);
96
1.61M
97
1.61M
  if (R.isUnresolvableResult())
98
102
    return isStaticContext ? 
IMA_Unresolved_StaticContext52
:
IMA_Unresolved50
;
99
1.61M
100
1.61M
  // Collect all the declaring classes of instance members we find.
101
1.61M
  bool hasNonInstance = false;
102
1.61M
  bool isField = false;
103
1.61M
  BaseSet Classes;
104
1.98M
  for (NamedDecl *D : R) {
105
1.98M
    // Look through any using decls.
106
1.98M
    D = D->getUnderlyingDecl();
107
1.98M
108
1.98M
    if (D->isCXXInstanceMember()) {
109
1.03M
      isField |= isa<FieldDecl>(D) || 
isa<MSPropertyDecl>(D)655k
||
110
1.03M
                 
isa<IndirectFieldDecl>(D)655k
;
111
1.03M
112
1.03M
      CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext());
113
1.03M
      Classes.insert(R->getCanonicalDecl());
114
1.03M
    } else
115
950k
      hasNonInstance = true;
116
1.98M
  }
117
1.61M
118
1.61M
  // If we didn't find any instance members, it can't be an implicit
119
1.61M
  // member reference.
120
1.61M
  if (Classes.empty())
121
922k
    return IMA_Static;
122
695k
123
695k
  // C++11 [expr.prim.general]p12:
124
695k
  //   An id-expression that denotes a non-static data member or non-static
125
695k
  //   member function of a class can only be used:
126
695k
  //   (...)
127
695k
  //   - if that id-expression denotes a non-static data member and it
128
695k
  //     appears in an unevaluated operand.
129
695k
  //
130
695k
  // This rule is specific to C++11.  However, we also permit this form
131
695k
  // in unevaluated inline assembly operands, like the operand to a SIZE.
132
695k
  IMAKind AbstractInstanceResult = IMA_Static; // happens to be 'false'
133
695k
  assert(!AbstractInstanceResult);
134
695k
  switch (SemaRef.ExprEvalContexts.back().Context) {
135
3.31k
  case Sema::ExpressionEvaluationContext::Unevaluated:
136
3.31k
  case Sema::ExpressionEvaluationContext::UnevaluatedList:
137
3.31k
    if (isField && 
SemaRef.getLangOpts().CPlusPlus113.20k
)
138
3.15k
      AbstractInstanceResult = IMA_Field_Uneval_Context;
139
3.31k
    break;
140
3.31k
141
3.31k
  case Sema::ExpressionEvaluationContext::UnevaluatedAbstract:
142
6
    AbstractInstanceResult = IMA_Abstract;
143
6
    break;
144
3.31k
145
691k
  case Sema::ExpressionEvaluationContext::DiscardedStatement:
146
691k
  case Sema::ExpressionEvaluationContext::ConstantEvaluated:
147
691k
  case Sema::ExpressionEvaluationContext::PotentiallyEvaluated:
148
691k
  case Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed:
149
691k
    break;
150
695k
  }
151
695k
152
695k
  // If the current context is not an instance method, it can't be
153
695k
  // an implicit member reference.
154
695k
  if (isStaticContext) {
155
604
    if (hasNonInstance)
156
307
      return IMA_Mixed_StaticContext;
157
297
158
297
    return AbstractInstanceResult ? 
AbstractInstanceResult218
159
297
                                  : 
IMA_Error_StaticContext79
;
160
297
  }
161
694k
162
694k
  CXXRecordDecl *contextClass;
163
694k
  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
164
693k
    contextClass = MD->getParent()->getCanonicalDecl();
165
1.20k
  else
166
1.20k
    contextClass = cast<CXXRecordDecl>(DC);
167
694k
168
694k
  // [class.mfct.non-static]p3:
169
694k
  // ...is used in the body of a non-static member function of class X,
170
694k
  // if name lookup (3.4.1) resolves the name in the id-expression to a
171
694k
  // non-static non-type member of some class C [...]
172
694k
  // ...if C is not X or a base class of X, the class member access expression
173
694k
  // is ill-formed.
174
694k
  if (R.getNamingClass() &&
175
694k
      contextClass->getCanonicalDecl() !=
176
694k
        R.getNamingClass()->getCanonicalDecl()) {
177
4.40k
    // If the naming class is not the current context, this was a qualified
178
4.40k
    // member name lookup, and it's sufficient to check that we have the naming
179
4.40k
    // class as a base class.
180
4.40k
    Classes.clear();
181
4.40k
    Classes.insert(R.getNamingClass()->getCanonicalDecl());
182
4.40k
  }
183
694k
184
694k
  // If we can prove that the current context is unrelated to all the
185
694k
  // declaring classes, it can't be an implicit member reference (in
186
694k
  // which case it's an error if any of those members are selected).
187
694k
  if (isProvablyNotDerivedFrom(SemaRef, contextClass, Classes))
188
34
    return hasNonInstance ? 
IMA_Mixed_Unrelated7
:
189
34
           
AbstractInstanceResult 27
?
AbstractInstanceResult8
:
190
27
                                    
IMA_Error_Unrelated19
;
191
694k
192
694k
  return (hasNonInstance ? 
IMA_Mixed1.51k
:
IMA_Instance692k
);
193
694k
}
194
195
/// Diagnose a reference to a field with no object available.
196
static void diagnoseInstanceReference(Sema &SemaRef,
197
                                      const CXXScopeSpec &SS,
198
                                      NamedDecl *Rep,
199
100
                                      const DeclarationNameInfo &nameInfo) {
200
100
  SourceLocation Loc = nameInfo.getLoc();
201
100
  SourceRange Range(Loc);
202
100
  if (SS.isSet()) 
Range.setBegin(SS.getRange().getBegin())40
;
203
100
204
100
  // Look through using shadow decls and aliases.
205
100
  Rep = Rep->getUnderlyingDecl();
206
100
207
100
  DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext();
208
100
  CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FunctionLevelDC);
209
100
  CXXRecordDecl *ContextClass = Method ? 
Method->getParent()49
:
nullptr51
;
210
100
  CXXRecordDecl *RepClass = dyn_cast<CXXRecordDecl>(Rep->getDeclContext());
211
100
212
100
  bool InStaticMethod = Method && 
Method->isStatic()49
;
213
100
  bool IsField = isa<FieldDecl>(Rep) || 
isa<IndirectFieldDecl>(Rep)34
;
214
100
215
100
  if (IsField && 
InStaticMethod68
)
216
18
    // "invalid use of member 'x' in static member function"
217
18
    SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method)
218
18
        << Range << nameInfo.getName();
219
82
  else if (ContextClass && 
RepClass31
&&
SS.isEmpty()31
&&
!InStaticMethod20
&&
220
82
           
!RepClass->Equals(ContextClass)13
&&
RepClass->Encloses(ContextClass)13
)
221
13
    // Unqualified lookup in a non-static member function found a member of an
222
13
    // enclosing class.
223
13
    SemaRef.Diag(Loc, diag::err_nested_non_static_member_use)
224
13
      << IsField << RepClass << nameInfo.getName() << ContextClass << Range;
225
69
  else if (IsField)
226
40
    SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use)
227
40
      << nameInfo.getName() << Range;
228
29
  else
229
29
    SemaRef.Diag(Loc, diag::err_member_call_without_object)
230
29
      << Range;
231
100
}
232
233
/// Builds an expression which might be an implicit member expression.
234
ExprResult
235
Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS,
236
                                      SourceLocation TemplateKWLoc,
237
                                      LookupResult &R,
238
                                const TemplateArgumentListInfo *TemplateArgs,
239
1.61M
                                      const Scope *S) {
240
1.61M
  switch (ClassifyImplicitMemberAccess(*this, R)) {
241
692k
  case IMA_Instance:
242
692k
    return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true, S);
243
0
244
1.57k
  case IMA_Mixed:
245
1.57k
  case IMA_Mixed_Unrelated:
246
1.57k
  case IMA_Unresolved:
247
1.57k
    return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, false,
248
1.57k
                                   S);
249
1.57k
250
1.57k
  case IMA_Field_Uneval_Context:
251
220
    Diag(R.getNameLoc(), diag::warn_cxx98_compat_non_static_member_use)
252
220
      << R.getLookupNameInfo().getName();
253
220
    LLVM_FALLTHROUGH;
254
922k
  case IMA_Static:
255
922k
  case IMA_Abstract:
256
922k
  case IMA_Mixed_StaticContext:
257
922k
  case IMA_Unresolved_StaticContext:
258
922k
    if (TemplateArgs || 
TemplateKWLoc.isValid()910k
)
259
12.6k
      return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs);
260
910k
    return BuildDeclarationNameExpr(SS, R, false);
261
910k
262
910k
  case IMA_Error_StaticContext:
263
98
  case IMA_Error_Unrelated:
264
98
    diagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(),
265
98
                              R.getLookupNameInfo());
266
98
    return ExprError();
267
0
  }
268
0
269
0
  llvm_unreachable("unexpected instance member access kind");
270
0
}
271
272
/// Determine whether input char is from rgba component set.
273
static bool
274
1.03k
IsRGBA(char c) {
275
1.03k
  switch (c) {
276
154
  case 'r':
277
154
  case 'g':
278
154
  case 'b':
279
154
  case 'a':
280
154
    return true;
281
880
  default:
282
880
    return false;
283
1.03k
  }
284
1.03k
}
285
286
// OpenCL v1.1, s6.1.7
287
// The component swizzle length must be in accordance with the acceptable
288
// vector sizes.
289
static bool IsValidOpenCLComponentSwizzleLength(unsigned len)
290
44
{
291
44
  return (len >= 1 && len <= 4) || 
len == 82
||
len == 162
;
292
44
}
293
294
/// Check an ext-vector component access expression.
295
///
296
/// VK should be set in advance to the value kind of the base
297
/// expression.
298
static QualType
299
CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK,
300
                        SourceLocation OpLoc, const IdentifierInfo *CompName,
301
476
                        SourceLocation CompLoc) {
302
476
  // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements,
303
476
  // see FIXME there.
304
476
  //
305
476
  // FIXME: This logic can be greatly simplified by splitting it along
306
476
  // halving/not halving and reworking the component checking.
307
476
  const ExtVectorType *vecType = baseType->getAs<ExtVectorType>();
308
476
309
476
  // The vector accessor can't exceed the number of elements.
310
476
  const char *compStr = CompName->getNameStart();
311
476
312
476
  // This flag determines whether or not the component is one of the four
313
476
  // special names that indicate a subset of exactly half the elements are
314
476
  // to be selected.
315
476
  bool HalvingSwizzle = false;
316
476
317
476
  // This flag determines whether or not CompName has an 's' char prefix,
318
476
  // indicating that it is a string of hex values to be used as vector indices.
319
476
  bool HexSwizzle = (*compStr == 's' || 
*compStr == 'S'452
) &&
compStr[1]25
;
320
476
321
476
  bool HasRepeated = false;
322
476
  bool HasIndex[16] = {};
323
476
324
476
  int Idx;
325
476
326
476
  // Check that we've found one of the special components, or that the component
327
476
  // names must come from the same set.
328
476
  if (!strcmp(compStr, "hi") || 
!strcmp(compStr, "lo")462
||
329
476
      
!strcmp(compStr, "even")444
||
!strcmp(compStr, "odd")432
) {
330
46
    HalvingSwizzle = true;
331
430
  } else if (!HexSwizzle &&
332
430
             
(Idx = vecType->getPointAccessorIdx(*compStr)) != -1410
) {
333
405
    bool HasRGBA = IsRGBA(*compStr);
334
625
    do {
335
625
      // Ensure that xyzw and rgba components don't intermingle.
336
625
      if (HasRGBA != IsRGBA(*compStr))
337
4
        break;
338
621
      if (HasIndex[Idx]) 
HasRepeated = true33
;
339
621
      HasIndex[Idx] = true;
340
621
      compStr++;
341
621
    } while (*compStr && 
(Idx = vecType->getPointAccessorIdx(*compStr)) != -1222
);
342
405
343
405
    // Emit a warning if an rgba selector is used earlier than OpenCL 2.2
344
405
    if (HasRGBA || 
(362
*compStr362
&&
IsRGBA(*compStr)4
)) {
345
46
      if (S.getLangOpts().OpenCL && 
S.getLangOpts().OpenCLVersion < 2204
) {
346
4
        const char *DiagBegin = HasRGBA ? 
CompName->getNameStart()2
:
compStr2
;
347
4
        S.Diag(OpLoc, diag::ext_opencl_ext_vector_type_rgba_selector)
348
4
          << StringRef(DiagBegin, 1)
349
4
          << S.getLangOpts().OpenCLVersion << SourceRange(CompLoc);
350
4
      }
351
46
    }
352
405
  } else {
353
25
    if (HexSwizzle) 
compStr++20
;
354
62
    while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) {
355
37
      if (HasIndex[Idx]) 
HasRepeated = true0
;
356
37
      HasIndex[Idx] = true;
357
37
      compStr++;
358
37
    }
359
25
  }
360
476
361
476
  if (!HalvingSwizzle && 
*compStr430
) {
362
12
    // We didn't get to the end of the string. This means the component names
363
12
    // didn't come from the same set *or* we encountered an illegal name.
364
12
    S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal)
365
12
      << StringRef(compStr, 1) << SourceRange(CompLoc);
366
12
    return QualType();
367
12
  }
368
464
369
464
  // Ensure no component accessor exceeds the width of the vector type it
370
464
  // operates on.
371
464
  if (!HalvingSwizzle) {
372
418
    compStr = CompName->getNameStart();
373
418
374
418
    if (HexSwizzle)
375
19
      compStr++;
376
418
377
1.04k
    while (*compStr) {
378
636
      if (!vecType->isAccessorWithinNumElements(*compStr++, HexSwizzle)) {
379
9
        S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length)
380
9
          << baseType << SourceRange(CompLoc);
381
9
        return QualType();
382
9
      }
383
636
    }
384
418
  }
385
464
386
464
  // OpenCL mode requires swizzle length to be in accordance with accepted
387
464
  // sizes. Clang however supports arbitrary lengths for other languages.
388
464
  
if (455
S.getLangOpts().OpenCL455
&&
!HalvingSwizzle47
) {
389
44
    unsigned SwizzleLength = CompName->getLength();
390
44
391
44
    if (HexSwizzle)
392
6
      SwizzleLength--;
393
44
394
44
    if (IsValidOpenCLComponentSwizzleLength(SwizzleLength) == false) {
395
2
      S.Diag(OpLoc, diag::err_opencl_ext_vector_component_invalid_length)
396
2
        << SwizzleLength << SourceRange(CompLoc);
397
2
      return QualType();
398
2
    }
399
453
  }
400
453
401
453
  // The component accessor looks fine - now we need to compute the actual type.
402
453
  // The vector type is implied by the component accessor. For example,
403
453
  // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
404
453
  // vec4.s0 is a float, vec4.s23 is a vec3, etc.
405
453
  // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
406
453
  unsigned CompSize = HalvingSwizzle ? 
(vecType->getNumElements() + 1) / 246
407
453
                                     : 
CompName->getLength()407
;
408
453
  if (HexSwizzle)
409
16
    CompSize--;
410
453
411
453
  if (CompSize == 1)
412
311
    return vecType->getElementType();
413
142
414
142
  if (HasRepeated) 
VK = VK_RValue20
;
415
142
416
142
  QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize);
417
142
  // Now look up the TypeDefDecl from the vector type. Without this,
418
142
  // diagostics look bad. We want extended vector types to appear built-in.
419
142
  for (Sema::ExtVectorDeclsType::iterator
420
142
         I = S.ExtVectorDecls.begin(S.getExternalSource()),
421
142
         E = S.ExtVectorDecls.end();
422
268
       I != E; 
++I126
) {
423
253
    if ((*I)->getUnderlyingType() == VT)
424
127
      return S.Context.getTypedefType(*I);
425
253
  }
426
142
427
142
  
return VT15
; // should never get here (a typedef type should always be found).
428
142
}
429
430
static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl,
431
                                                IdentifierInfo *Member,
432
                                                const Selector &Sel,
433
8
                                                ASTContext &Context) {
434
8
  if (Member)
435
4
    if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(
436
0
            Member, ObjCPropertyQueryKind::OBJC_PR_query_instance))
437
0
      return PD;
438
8
  if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel))
439
0
    return OMD;
440
8
441
8
  for (const auto *I : PDecl->protocols()) {
442
0
    if (Decl *D = FindGetterSetterNameDeclFromProtocolList(I, Member, Sel,
443
0
                                                           Context))
444
0
      return D;
445
0
  }
446
8
  return nullptr;
447
8
}
448
449
static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy,
450
                                      IdentifierInfo *Member,
451
                                      const Selector &Sel,
452
44
                                      ASTContext &Context) {
453
44
  // Check protocols on qualified interfaces.
454
44
  Decl *GDecl = nullptr;
455
44
  for (const auto *I : QIdTy->quals()) {
456
38
    if (Member)
457
27
      if (ObjCPropertyDecl *PD = I->FindPropertyDeclaration(
458
12
              Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) {
459
12
        GDecl = PD;
460
12
        break;
461
12
      }
462
26
    // Also must look for a getter or setter name which uses property syntax.
463
26
    if (ObjCMethodDecl *OMD = I->getInstanceMethod(Sel)) {
464
18
      GDecl = OMD;
465
18
      break;
466
18
    }
467
26
  }
468
44
  if (!GDecl) {
469
14
    for (const auto *I : QIdTy->quals()) {
470
8
      // Search in the protocol-qualifier list of current protocol.
471
8
      GDecl = FindGetterSetterNameDeclFromProtocolList(I, Member, Sel, Context);
472
8
      if (GDecl)
473
0
        return GDecl;
474
8
    }
475
14
  }
476
44
  return GDecl;
477
44
}
478
479
ExprResult
480
Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType,
481
                               bool IsArrow, SourceLocation OpLoc,
482
                               const CXXScopeSpec &SS,
483
                               SourceLocation TemplateKWLoc,
484
                               NamedDecl *FirstQualifierInScope,
485
                               const DeclarationNameInfo &NameInfo,
486
787k
                               const TemplateArgumentListInfo *TemplateArgs) {
487
787k
  // Even in dependent contexts, try to diagnose base expressions with
488
787k
  // obviously wrong types, e.g.:
489
787k
  //
490
787k
  // T* t;
491
787k
  // t.f;
492
787k
  //
493
787k
  // In Obj-C++, however, the above expression is valid, since it could be
494
787k
  // accessing the 'f' property if T is an Obj-C interface. The extra check
495
787k
  // allows this, while still reporting an error if T is a struct pointer.
496
787k
  if (!IsArrow) {
497
612k
    const PointerType *PT = BaseType->getAs<PointerType>();
498
612k
    if (PT && 
(6
!getLangOpts().ObjC6
||
499
6
               
PT->getPointeeType()->isRecordType()4
)) {
500
2
      assert(BaseExpr && "cannot happen with implicit member accesses");
501
2
      Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
502
2
        << BaseType << BaseExpr->getSourceRange() << NameInfo.getSourceRange();
503
2
      return ExprError();
504
2
    }
505
787k
  }
506
787k
507
787k
  assert(BaseType->isDependentType() ||
508
787k
         NameInfo.getName().isDependentName() ||
509
787k
         isDependentScopeSpecifier(SS));
510
787k
511
787k
  // Get the type being accessed in BaseType.  If this is an arrow, the BaseExpr
512
787k
  // must have pointer type, and the accessed type is the pointee.
513
787k
  return CXXDependentScopeMemberExpr::Create(
514
787k
      Context, BaseExpr, BaseType, IsArrow, OpLoc,
515
787k
      SS.getWithLocInContext(Context), TemplateKWLoc, FirstQualifierInScope,
516
787k
      NameInfo, TemplateArgs);
517
787k
}
518
519
/// We know that the given qualified member reference points only to
520
/// declarations which do not belong to the static type of the base
521
/// expression.  Diagnose the problem.
522
static void DiagnoseQualifiedMemberReference(Sema &SemaRef,
523
                                             Expr *BaseExpr,
524
                                             QualType BaseType,
525
                                             const CXXScopeSpec &SS,
526
                                             NamedDecl *rep,
527
26
                                       const DeclarationNameInfo &nameInfo) {
528
26
  // If this is an implicit member access, use a different set of
529
26
  // diagnostics.
530
26
  if (!BaseExpr)
531
2
    return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo);
532
24
533
24
  SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated)
534
24
    << SS.getRange() << rep << BaseType;
535
24
}
536
537
// Check whether the declarations we found through a nested-name
538
// specifier in a member expression are actually members of the base
539
// type.  The restriction here is:
540
//
541
//   C++ [expr.ref]p2:
542
//     ... In these cases, the id-expression shall name a
543
//     member of the class or of one of its base classes.
544
//
545
// So it's perfectly legitimate for the nested-name specifier to name
546
// an unrelated class, and for us to find an overload set including
547
// decls from classes which are not superclasses, as long as the decl
548
// we actually pick through overload resolution is from a superclass.
549
bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr,
550
                                         QualType BaseType,
551
                                         const CXXScopeSpec &SS,
552
777k
                                         const LookupResult &R) {
553
777k
  CXXRecordDecl *BaseRecord =
554
777k
    cast_or_null<CXXRecordDecl>(computeDeclContext(BaseType));
555
777k
  if (!BaseRecord) {
556
0
    // We can't check this yet because the base type is still
557
0
    // dependent.
558
0
    assert(BaseType->isDependentType());
559
0
    return false;
560
0
  }
561
777k
562
777k
  
for (LookupResult::iterator I = R.begin(), E = R.end(); 777k
I != E;
++I46
) {
563
777k
    // If this is an implicit member reference and we find a
564
777k
    // non-instance member, it's not an error.
565
777k
    if (!BaseExpr && 
!(*I)->isCXXInstanceMember()27.3k
)
566
11.0k
      return false;
567
766k
568
766k
    // Note that we use the DC of the decl, not the underlying decl.
569
766k
    DeclContext *DC = (*I)->getDeclContext();
570
766k
    while (DC->isTransparentContext())
571
1
      DC = DC->getParent();
572
766k
573
766k
    if (!DC->isRecord())
574
3
      continue;
575
766k
576
766k
    CXXRecordDecl *MemberRecord = cast<CXXRecordDecl>(DC)->getCanonicalDecl();
577
766k
    if (BaseRecord->getCanonicalDecl() == MemberRecord ||
578
766k
        
!BaseRecord->isProvablyNotDerivedFrom(MemberRecord)9.68k
)
579
766k
      return false;
580
766k
  }
581
777k
582
777k
  DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS,
583
26
                                   R.getRepresentativeDecl(),
584
26
                                   R.getLookupNameInfo());
585
26
  return true;
586
777k
}
587
588
namespace {
589
590
// Callback to only accept typo corrections that are either a ValueDecl or a
591
// FunctionTemplateDecl and are declared in the current record or, for a C++
592
// classes, one of its base classes.
593
class RecordMemberExprValidatorCCC final : public CorrectionCandidateCallback {
594
public:
595
  explicit RecordMemberExprValidatorCCC(const RecordType *RTy)
596
292
      : Record(RTy->getDecl()) {
597
292
    // Don't add bare keywords to the consumer since they will always fail
598
292
    // validation by virtue of not being associated with any decls.
599
292
    WantTypeSpecifiers = false;
600
292
    WantExpressionKeywords = false;
601
292
    WantCXXNamedCasts = false;
602
292
    WantFunctionLikeCasts = false;
603
292
    WantRemainingKeywords = false;
604
292
  }
605
606
181
  bool ValidateCandidate(const TypoCorrection &candidate) override {
607
181
    NamedDecl *ND = candidate.getCorrectionDecl();
608
181
    // Don't accept candidates that cannot be member functions, constants,
609
181
    // variables, or templates.
610
181
    if (!ND || 
!(179
isa<ValueDecl>(ND)179
||
isa<FunctionTemplateDecl>(ND)5
))
611
6
      return false;
612
175
613
175
    // Accept candidates that occur in the current record.
614
175
    if (Record->containsDecl(ND))
615
141
      return true;
616
34
617
34
    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Record)) {
618
34
      // Accept candidates that occur in any of the current class' base classes.
619
34
      for (const auto &BS : RD->bases()) {
620
34
        if (const RecordType *BSTy =
621
6
                dyn_cast_or_null<RecordType>(BS.getType().getTypePtrOrNull())) {
622
6
          if (BSTy->getDecl()->containsDecl(ND))
623
6
            return true;
624
6
        }
625
34
      }
626
34
    }
627
34
628
34
    
return false28
;
629
34
  }
630
631
178
  std::unique_ptr<CorrectionCandidateCallback> clone() override {
632
178
    return std::make_unique<RecordMemberExprValidatorCCC>(*this);
633
178
  }
634
635
private:
636
  const RecordDecl *const Record;
637
};
638
639
}
640
641
static bool LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R,
642
                                     Expr *BaseExpr,
643
                                     const RecordType *RTy,
644
                                     SourceLocation OpLoc, bool IsArrow,
645
                                     CXXScopeSpec &SS, bool HasTemplateArgs,
646
                                     SourceLocation TemplateKWLoc,
647
420k
                                     TypoExpr *&TE) {
648
420k
  SourceRange BaseRange = BaseExpr ? 
BaseExpr->getSourceRange()406k
:
SourceRange()13.4k
;
649
420k
  RecordDecl *RDecl = RTy->getDecl();
650
420k
  if (!SemaRef.isThisOutsideMemberFunctionBody(QualType(RTy, 0)) &&
651
420k
      SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0),
652
420k
                                  diag::err_typecheck_incomplete_tag,
653
420k
                                  BaseRange))
654
3
    return true;
655
420k
656
420k
  if (HasTemplateArgs || 
TemplateKWLoc.isValid()418k
) {
657
1.83k
    // LookupTemplateName doesn't expect these both to exist simultaneously.
658
1.83k
    QualType ObjectType = SS.isSet() ? 
QualType()56
:
QualType(RTy, 0)1.78k
;
659
1.83k
660
1.83k
    bool MOUS;
661
1.83k
    return SemaRef.LookupTemplateName(R, nullptr, SS, ObjectType, false, MOUS,
662
1.83k
                                      TemplateKWLoc);
663
1.83k
  }
664
418k
665
418k
  DeclContext *DC = RDecl;
666
418k
  if (SS.isSet()) {
667
13.9k
    // If the member name was a qualified-id, look into the
668
13.9k
    // nested-name-specifier.
669
13.9k
    DC = SemaRef.computeDeclContext(SS, false);
670
13.9k
671
13.9k
    if (SemaRef.RequireCompleteDeclContext(SS, DC)) {
672
0
      SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag)
673
0
          << SS.getRange() << DC;
674
0
      return true;
675
0
    }
676
13.9k
677
13.9k
    assert(DC && "Cannot handle non-computable dependent contexts in lookup");
678
13.9k
679
13.9k
    if (!isa<TypeDecl>(DC)) {
680
16
      SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass)
681
16
          << DC << SS.getRange();
682
16
      return true;
683
16
    }
684
418k
  }
685
418k
686
418k
  // The record definition is complete, now look up the member.
687
418k
  SemaRef.LookupQualifiedName(R, DC, SS);
688
418k
689
418k
  if (!R.empty())
690
418k
    return false;
691
292
692
292
  DeclarationName Typo = R.getLookupName();
693
292
  SourceLocation TypoLoc = R.getNameLoc();
694
292
695
292
  struct QueryState {
696
292
    Sema &SemaRef;
697
292
    DeclarationNameInfo NameInfo;
698
292
    Sema::LookupNameKind LookupKind;
699
292
    Sema::RedeclarationKind Redecl;
700
292
  };
701
292
  QueryState Q = {R.getSema(), R.getLookupNameInfo(), R.getLookupKind(),
702
292
                  R.redeclarationKind()};
703
292
  RecordMemberExprValidatorCCC CCC(RTy);
704
292
  TE = SemaRef.CorrectTypoDelayed(
705
292
      R.getLookupNameInfo(), R.getLookupKind(), nullptr, &SS, CCC,
706
292
      [=, &SemaRef](const TypoCorrection &TC) {
707
90
        if (TC) {
708
60
          assert(!TC.isKeyword() &&
709
60
                 "Got a keyword as a correction for a member!");
710
60
          bool DroppedSpecifier =
711
60
              TC.WillReplaceSpecifier() &&
712
60
              
Typo.getAsString() == TC.getAsString(SemaRef.getLangOpts())0
;
713
60
          SemaRef.diagnoseTypo(TC, SemaRef.PDiag(diag::err_no_member_suggest)
714
60
                                       << Typo << DC << DroppedSpecifier
715
60
                                       << SS.getRange());
716
60
        } else {
717
30
          SemaRef.Diag(TypoLoc, diag::err_no_member) << Typo << DC << BaseRange;
718
30
        }
719
90
      },
720
292
      [=](Sema &SemaRef, TypoExpr *TE, TypoCorrection TC) mutable {
721
152
        LookupResult R(Q.SemaRef, Q.NameInfo, Q.LookupKind, Q.Redecl);
722
152
        R.clear(); // Ensure there's no decls lingering in the shared state.
723
152
        R.suppressDiagnostics();
724
152
        R.setLookupName(TC.getCorrection());
725
152
        for (NamedDecl *ND : TC)
726
157
          R.addDecl(ND);
727
152
        R.resolveKind();
728
152
        return SemaRef.BuildMemberReferenceExpr(
729
152
            BaseExpr, BaseExpr->getType(), OpLoc, IsArrow, SS, SourceLocation(),
730
152
            nullptr, R, nullptr, nullptr);
731
152
      },
732
292
      Sema::CTK_ErrorRecovery, DC);
733
292
734
292
  return false;
735
292
}
736
737
static ExprResult LookupMemberExpr(Sema &S, LookupResult &R,
738
                                   ExprResult &BaseExpr, bool &IsArrow,
739
                                   SourceLocation OpLoc, CXXScopeSpec &SS,
740
                                   Decl *ObjCImpDecl, bool HasTemplateArgs,
741
                                   SourceLocation TemplateKWLoc);
742
743
ExprResult
744
Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType,
745
                               SourceLocation OpLoc, bool IsArrow,
746
                               CXXScopeSpec &SS,
747
                               SourceLocation TemplateKWLoc,
748
                               NamedDecl *FirstQualifierInScope,
749
                               const DeclarationNameInfo &NameInfo,
750
                               const TemplateArgumentListInfo *TemplateArgs,
751
                               const Scope *S,
752
425k
                               ActOnMemberAccessExtraArgs *ExtraArgs) {
753
425k
  if (BaseType->isDependentType() ||
754
425k
      
(424k
SS.isSet()424k
&&
isDependentScopeSpecifier(SS)14.0k
))
755
1.23k
    return ActOnDependentMemberExpr(Base, BaseType,
756
1.23k
                                    IsArrow, OpLoc,
757
1.23k
                                    SS, TemplateKWLoc, FirstQualifierInScope,
758
1.23k
                                    NameInfo, TemplateArgs);
759
424k
760
424k
  LookupResult R(*this, NameInfo, LookupMemberName);
761
424k
762
424k
  // Implicit member accesses.
763
424k
  if (!Base) {
764
13.4k
    TypoExpr *TE = nullptr;
765
13.4k
    QualType RecordTy = BaseType;
766
13.4k
    if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType();
767
13.4k
    if (LookupMemberExprInRecord(
768
13.4k
            *this, R, nullptr, RecordTy->getAs<RecordType>(), OpLoc, IsArrow,
769
13.4k
            SS, TemplateArgs != nullptr, TemplateKWLoc, TE))
770
6
      return ExprError();
771
13.4k
    if (TE)
772
0
      return TE;
773
410k
774
410k
  // Explicit member accesses.
775
410k
  } else {
776
410k
    ExprResult BaseResult = Base;
777
410k
    ExprResult Result =
778
410k
        LookupMemberExpr(*this, R, BaseResult, IsArrow, OpLoc, SS,
779
410k
                         ExtraArgs ? 
ExtraArgs->ObjCImpDecl330k
:
nullptr80.3k
,
780
410k
                         TemplateArgs != nullptr, TemplateKWLoc);
781
410k
782
410k
    if (BaseResult.isInvalid())
783
1
      return ExprError();
784
410k
    Base = BaseResult.get();
785
410k
786
410k
    if (Result.isInvalid())
787
135
      return ExprError();
788
410k
789
410k
    if (Result.get())
790
3.75k
      return Result;
791
406k
792
406k
    // LookupMemberExpr can modify Base, and thus change BaseType
793
406k
    BaseType = Base->getType();
794
406k
  }
795
424k
796
424k
  return BuildMemberReferenceExpr(Base, BaseType,
797
420k
                                  OpLoc, IsArrow, SS, TemplateKWLoc,
798
420k
                                  FirstQualifierInScope, R, TemplateArgs, S,
799
420k
                                  false, ExtraArgs);
800
424k
}
801
802
ExprResult
803
Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS,
804
                                               SourceLocation loc,
805
                                               IndirectFieldDecl *indirectField,
806
                                               DeclAccessPair foundDecl,
807
                                               Expr *baseObjectExpr,
808
4.02k
                                               SourceLocation opLoc) {
809
4.02k
  // First, build the expression that refers to the base object.
810
4.02k
811
4.02k
  // Case 1:  the base of the indirect field is not a field.
812
4.02k
  VarDecl *baseVariable = indirectField->getVarDecl();
813
4.02k
  CXXScopeSpec EmptySS;
814
4.02k
  if (baseVariable) {
815
68
    assert(baseVariable->getType()->isRecordType());
816
68
817
68
    // In principle we could have a member access expression that
818
68
    // accesses an anonymous struct/union that's a static member of
819
68
    // the base object's class.  However, under the current standard,
820
68
    // static data members cannot be anonymous structs or unions.
821
68
    // Supporting this is as easy as building a MemberExpr here.
822
68
    assert(!baseObjectExpr && "anonymous struct/union is static data member?");
823
68
824
68
    DeclarationNameInfo baseNameInfo(DeclarationName(), loc);
825
68
826
68
    ExprResult result
827
68
      = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable);
828
68
    if (result.isInvalid()) 
return ExprError()0
;
829
68
830
68
    baseObjectExpr = result.get();
831
68
  }
832
4.02k
833
4.02k
  assert((baseVariable || baseObjectExpr) &&
834
4.02k
         "referencing anonymous struct/union without a base variable or "
835
4.02k
         "expression");
836
4.02k
837
4.02k
  // Build the implicit member references to the field of the
838
4.02k
  // anonymous struct/union.
839
4.02k
  Expr *result = baseObjectExpr;
840
4.02k
  IndirectFieldDecl::chain_iterator
841
4.02k
  FI = indirectField->chain_begin(), FEnd = indirectField->chain_end();
842
4.02k
843
4.02k
  // Case 2: the base of the indirect field is a field and the user
844
4.02k
  // wrote a member expression.
845
4.02k
  if (!baseVariable) {
846
3.95k
    FieldDecl *field = cast<FieldDecl>(*FI);
847
3.95k
848
3.95k
    bool baseObjectIsPointer = baseObjectExpr->getType()->isPointerType();
849
3.95k
850
3.95k
    // Make a nameInfo that properly uses the anonymous name.
851
3.95k
    DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
852
3.95k
853
3.95k
    // Build the first member access in the chain with full information.
854
3.95k
    result =
855
3.95k
        BuildFieldReferenceExpr(result, baseObjectIsPointer, SourceLocation(),
856
3.95k
                                SS, field, foundDecl, memberNameInfo)
857
3.95k
            .get();
858
3.95k
    if (!result)
859
1
      return ExprError();
860
4.02k
  }
861
4.02k
862
4.02k
  // In all cases, we should now skip the first declaration in the chain.
863
4.02k
  ++FI;
864
4.02k
865
8.18k
  while (FI != FEnd) {
866
4.15k
    FieldDecl *field = cast<FieldDecl>(*FI++);
867
4.15k
868
4.15k
    // FIXME: these are somewhat meaningless
869
4.15k
    DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
870
4.15k
    DeclAccessPair fakeFoundDecl =
871
4.15k
        DeclAccessPair::make(field, field->getAccess());
872
4.15k
873
4.15k
    result =
874
4.15k
        BuildFieldReferenceExpr(result, /*isarrow*/ false, SourceLocation(),
875
4.15k
                                (FI == FEnd ? 
SS4.02k
:
EmptySS134
), field,
876
4.15k
                                fakeFoundDecl, memberNameInfo)
877
4.15k
            .get();
878
4.15k
  }
879
4.02k
880
4.02k
  return result;
881
4.02k
}
882
883
static ExprResult
884
BuildMSPropertyRefExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
885
                       const CXXScopeSpec &SS,
886
                       MSPropertyDecl *PD,
887
223
                       const DeclarationNameInfo &NameInfo) {
888
223
  // Property names are always simple identifiers and therefore never
889
223
  // require any interesting additional storage.
890
223
  return new (S.Context) MSPropertyRefExpr(BaseExpr, PD, IsArrow,
891
223
                                           S.Context.PseudoObjectTy, VK_LValue,
892
223
                                           SS.getWithLocInContext(S.Context),
893
223
                                           NameInfo.getLoc());
894
223
}
895
896
MemberExpr *Sema::BuildMemberExpr(
897
    Expr *Base, bool IsArrow, SourceLocation OpLoc, const CXXScopeSpec *SS,
898
    SourceLocation TemplateKWLoc, ValueDecl *Member, DeclAccessPair FoundDecl,
899
    bool HadMultipleCandidates, const DeclarationNameInfo &MemberNameInfo,
900
    QualType Ty, ExprValueKind VK, ExprObjectKind OK,
901
997k
    const TemplateArgumentListInfo *TemplateArgs) {
902
997k
  NestedNameSpecifierLoc NNS =
903
997k
      SS ? SS->getWithLocInContext(Context) : 
NestedNameSpecifierLoc()0
;
904
997k
  return BuildMemberExpr(Base, IsArrow, OpLoc, NNS, TemplateKWLoc, Member,
905
997k
                         FoundDecl, HadMultipleCandidates, MemberNameInfo, Ty,
906
997k
                         VK, OK, TemplateArgs);
907
997k
}
908
909
MemberExpr *Sema::BuildMemberExpr(
910
    Expr *Base, bool IsArrow, SourceLocation OpLoc, NestedNameSpecifierLoc NNS,
911
    SourceLocation TemplateKWLoc, ValueDecl *Member, DeclAccessPair FoundDecl,
912
    bool HadMultipleCandidates, const DeclarationNameInfo &MemberNameInfo,
913
    QualType Ty, ExprValueKind VK, ExprObjectKind OK,
914
1.06M
    const TemplateArgumentListInfo *TemplateArgs) {
915
1.06M
  assert((!IsArrow || Base->isRValue()) && "-> base must be a pointer rvalue");
916
1.06M
  MemberExpr *E =
917
1.06M
      MemberExpr::Create(Context, Base, IsArrow, OpLoc, NNS, TemplateKWLoc,
918
1.06M
                         Member, FoundDecl, MemberNameInfo, TemplateArgs, Ty,
919
1.06M
                         VK, OK, getNonOdrUseReasonInCurrentContext(Member));
920
1.06M
  E->setHadMultipleCandidates(HadMultipleCandidates);
921
1.06M
  MarkMemberReferenced(E);
922
1.06M
923
1.06M
  // C++ [except.spec]p17:
924
1.06M
  //   An exception-specification is considered to be needed when:
925
1.06M
  //   - in an expression the function is the unique lookup result or the
926
1.06M
  //     selected member of a set of overloaded functions
927
1.06M
  if (auto *FPT = Ty->getAs<FunctionProtoType>()) {
928
709
    if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
929
0
      if (auto *NewFPT = ResolveExceptionSpec(MemberNameInfo.getLoc(), FPT))
930
0
        E->setType(Context.getQualifiedType(NewFPT, Ty.getQualifiers()));
931
0
    }
932
709
  }
933
1.06M
934
1.06M
  return E;
935
1.06M
}
936
937
/// Determine if the given scope is within a function-try-block handler.
938
91.3k
static bool IsInFnTryBlockHandler(const Scope *S) {
939
91.3k
  // Walk the scope stack until finding a FnTryCatchScope, or leave the
940
91.3k
  // function scope. If a FnTryCatchScope is found, check whether the TryScope
941
91.3k
  // flag is set. If it is not, it's a function-try-block handler.
942
223k
  for (; S != S->getFnParent(); 
S = S->getParent()132k
) {
943
132k
    if (S->getFlags() & Scope::FnTryCatchScope)
944
10
      return (S->getFlags() & Scope::TryScope) != Scope::TryScope;
945
132k
  }
946
91.3k
  
return false91.3k
;
947
91.3k
}
948
949
VarDecl *
950
Sema::getVarTemplateSpecialization(VarTemplateDecl *VarTempl,
951
                      const TemplateArgumentListInfo *TemplateArgs,
952
                      const DeclarationNameInfo &MemberNameInfo,
953
75
                      SourceLocation TemplateKWLoc) {
954
75
  if (!TemplateArgs) {
955
0
    diagnoseMissingTemplateArguments(TemplateName(VarTempl),
956
0
                                     MemberNameInfo.getBeginLoc());
957
0
    return nullptr;
958
0
  }
959
75
960
75
  DeclResult VDecl = CheckVarTemplateId(VarTempl, TemplateKWLoc,
961
75
                                        MemberNameInfo.getLoc(), *TemplateArgs);
962
75
  if (VDecl.isInvalid())
963
0
    return nullptr;
964
75
  VarDecl *Var = cast<VarDecl>(VDecl.get());
965
75
  if (!Var->getTemplateSpecializationKind())
966
57
    Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation,
967
57
                                       MemberNameInfo.getLoc());
968
75
  return Var;
969
75
}
970
971
ExprResult
972
Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
973
                               SourceLocation OpLoc, bool IsArrow,
974
                               const CXXScopeSpec &SS,
975
                               SourceLocation TemplateKWLoc,
976
                               NamedDecl *FirstQualifierInScope,
977
                               LookupResult &R,
978
                               const TemplateArgumentListInfo *TemplateArgs,
979
                               const Scope *S,
980
                               bool SuppressQualifierCheck,
981
1.22M
                               ActOnMemberAccessExtraArgs *ExtraArgs) {
982
1.22M
  QualType BaseType = BaseExprType;
983
1.22M
  if (IsArrow) {
984
904k
    assert(BaseType->isPointerType());
985
904k
    BaseType = BaseType->castAs<PointerType>()->getPointeeType();
986
904k
  }
987
1.22M
  R.setBaseObjectType(BaseType);
988
1.22M
989
1.22M
  // C++1z [expr.ref]p2:
990
1.22M
  //   For the first option (dot) the first expression shall be a glvalue [...]
991
1.22M
  if (!IsArrow && 
BaseExpr318k
&&
BaseExpr->isRValue()318k
) {
992
6.28k
    ExprResult Converted = TemporaryMaterializationConversion(BaseExpr);
993
6.28k
    if (Converted.isInvalid())
994
0
      return ExprError();
995
6.28k
    BaseExpr = Converted.get();
996
6.28k
  }
997
1.22M
998
1.22M
999
1.22M
  const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo();
1000
1.22M
  DeclarationName MemberName = MemberNameInfo.getName();
1001
1.22M
  SourceLocation MemberLoc = MemberNameInfo.getLoc();
1002
1.22M
1003
1.22M
  if (R.isAmbiguous())
1004
19
    return ExprError();
1005
1.22M
1006
1.22M
  // [except.handle]p10: Referring to any non-static member or base class of an
1007
1.22M
  // object in the handler for a function-try-block of a constructor or
1008
1.22M
  // destructor for that object results in undefined behavior.
1009
1.22M
  const auto *FD = getCurFunctionDecl();
1010
1.22M
  if (S && 
BaseExpr1.02M
&&
FD1.01M
&&
1011
1.22M
      
(1.01M
isa<CXXDestructorDecl>(FD)1.01M
||
isa<CXXConstructorDecl>(FD)997k
) &&
1012
1.22M
      
isa<CXXThisExpr>(BaseExpr->IgnoreImpCasts())106k
&&
1013
1.22M
      
IsInFnTryBlockHandler(S)91.3k
)
1014
10
    Diag(MemberLoc, diag::warn_cdtor_function_try_handler_mem_expr)
1015
10
        << isa<CXXDestructorDecl>(FD);
1016
1.22M
1017
1.22M
  if (R.empty()) {
1018
196
    // Rederive where we looked up.
1019
196
    DeclContext *DC = (SS.isSet()
1020
196
                       ? 
computeDeclContext(SS, false)13
1021
196
                       : 
BaseType->castAs<RecordType>()->getDecl()183
);
1022
196
1023
196
    if (ExtraArgs) {
1024
97
      ExprResult RetryExpr;
1025
97
      if (!IsArrow && 
BaseExpr79
) {
1026
79
        SFINAETrap Trap(*this, true);
1027
79
        ParsedType ObjectType;
1028
79
        bool MayBePseudoDestructor = false;
1029
79
        RetryExpr = ActOnStartCXXMemberReference(getCurScope(), BaseExpr,
1030
79
                                                 OpLoc, tok::arrow, ObjectType,
1031
79
                                                 MayBePseudoDestructor);
1032
79
        if (RetryExpr.isUsable() && 
!Trap.hasErrorOccurred()75
) {
1033
7
          CXXScopeSpec TempSS(SS);
1034
7
          RetryExpr = ActOnMemberAccessExpr(
1035
7
              ExtraArgs->S, RetryExpr.get(), OpLoc, tok::arrow, TempSS,
1036
7
              TemplateKWLoc, ExtraArgs->Id, ExtraArgs->ObjCImpDecl);
1037
7
        }
1038
79
        if (Trap.hasErrorOccurred())
1039
72
          RetryExpr = ExprError();
1040
79
      }
1041
97
      if (RetryExpr.isUsable()) {
1042
7
        Diag(OpLoc, diag::err_no_member_overloaded_arrow)
1043
7
          << MemberName << DC << FixItHint::CreateReplacement(OpLoc, "->");
1044
7
        return RetryExpr;
1045
7
      }
1046
189
    }
1047
189
1048
189
    Diag(R.getNameLoc(), diag::err_no_member)
1049
189
      << MemberName << DC
1050
189
      << (BaseExpr ? 
BaseExpr->getSourceRange()182
:
SourceRange()7
);
1051
189
    return ExprError();
1052
189
  }
1053
1.22M
1054
1.22M
  // Diagnose lookups that find only declarations from a non-base
1055
1.22M
  // type.  This is possible for either qualified lookups (which may
1056
1.22M
  // have been qualified with an unrelated type) or implicit member
1057
1.22M
  // expressions (which were found with unqualified lookup and thus
1058
1.22M
  // may have come from an enclosing scope).  Note that it's okay for
1059
1.22M
  // lookup to find declarations from a non-base type as long as those
1060
1.22M
  // aren't the ones picked by overload resolution.
1061
1.22M
  if ((SS.isSet() || 
!BaseExpr1.20M
||
1062
1.22M
       
(1.19M
isa<CXXThisExpr>(BaseExpr)1.19M
&&
1063
1.19M
        
cast<CXXThisExpr>(BaseExpr)->isImplicit()757k
)) &&
1064
1.22M
      
!SuppressQualifierCheck777k
&&
1065
1.22M
      
CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)777k
)
1066
26
    return ExprError();
1067
1.22M
1068
1.22M
  // Construct an unresolved result if we in fact got an unresolved
1069
1.22M
  // result.
1070
1.22M
  if (R.isOverloadedResult() || 
R.isUnresolvableResult()1.00M
) {
1071
221k
    // Suppress any lookup-related diagnostics; we'll do these when we
1072
221k
    // pick a member.
1073
221k
    R.suppressDiagnostics();
1074
221k
1075
221k
    UnresolvedMemberExpr *MemExpr
1076
221k
      = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(),
1077
221k
                                     BaseExpr, BaseExprType,
1078
221k
                                     IsArrow, OpLoc,
1079
221k
                                     SS.getWithLocInContext(Context),
1080
221k
                                     TemplateKWLoc, MemberNameInfo,
1081
221k
                                     TemplateArgs, R.begin(), R.end());
1082
221k
1083
221k
    return MemExpr;
1084
221k
  }
1085
1.00M
1086
1.00M
  assert(R.isSingleResult());
1087
1.00M
  DeclAccessPair FoundDecl = R.begin().getPair();
1088
1.00M
  NamedDecl *MemberDecl = R.getFoundDecl();
1089
1.00M
1090
1.00M
  // FIXME: diagnose the presence of template arguments now.
1091
1.00M
1092
1.00M
  // If the decl being referenced had an error, return an error for this
1093
1.00M
  // sub-expr without emitting another error, in order to avoid cascading
1094
1.00M
  // error cases.
1095
1.00M
  if (MemberDecl->isInvalidDecl())
1096
23
    return ExprError();
1097
1.00M
1098
1.00M
  // Handle the implicit-member-access case.
1099
1.00M
  if (!BaseExpr) {
1100
9.13k
    // If this is not an instance member, convert to a non-member access.
1101
9.13k
    if (!MemberDecl->isCXXInstanceMember()) {
1102
7.01k
      // If this is a variable template, get the instantiated variable
1103
7.01k
      // declaration corresponding to the supplied template arguments
1104
7.01k
      // (while emitting diagnostics as necessary) that will be referenced
1105
7.01k
      // by this expression.
1106
7.01k
      assert((!TemplateArgs || isa<VarTemplateDecl>(MemberDecl)) &&
1107
7.01k
             "How did we get template arguments here sans a variable template");
1108
7.01k
      if (isa<VarTemplateDecl>(MemberDecl)) {
1109
1
        MemberDecl = getVarTemplateSpecialization(
1110
1
            cast<VarTemplateDecl>(MemberDecl), TemplateArgs,
1111
1
            R.getLookupNameInfo(), TemplateKWLoc);
1112
1
        if (!MemberDecl)
1113
0
          return ExprError();
1114
7.01k
      }
1115
7.01k
      return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl,
1116
7.01k
                                      FoundDecl, TemplateArgs);
1117
7.01k
    }
1118
2.12k
    SourceLocation Loc = R.getNameLoc();
1119
2.12k
    if (SS.getRange().isValid())
1120
2.08k
      Loc = SS.getRange().getBegin();
1121
2.12k
    BaseExpr = BuildCXXThisExpr(Loc, BaseExprType, /*IsImplicit=*/true);
1122
2.12k
  }
1123
1.00M
1124
1.00M
  // Check the use of this member.
1125
1.00M
  
if (994k
DiagnoseUseOfDecl(MemberDecl, MemberLoc)994k
)
1126
29
    return ExprError();
1127
994k
1128
994k
  if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl))
1129
744k
    return BuildFieldReferenceExpr(BaseExpr, IsArrow, OpLoc, SS, FD, FoundDecl,
1130
744k
                                   MemberNameInfo);
1131
250k
1132
250k
  if (MSPropertyDecl *PD = dyn_cast<MSPropertyDecl>(MemberDecl))
1133
223
    return BuildMSPropertyRefExpr(*this, BaseExpr, IsArrow, SS, PD,
1134
223
                                  MemberNameInfo);
1135
249k
1136
249k
  if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl))
1137
3.95k
    // We may have found a field within an anonymous union or struct
1138
3.95k
    // (C++ [class.union]).
1139
3.95k
    return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD,
1140
3.95k
                                                    FoundDecl, BaseExpr,
1141
3.95k
                                                    OpLoc);
1142
246k
1143
246k
  if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) {
1144
267
    return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, Var,
1145
267
                           FoundDecl, /*HadMultipleCandidates=*/false,
1146
267
                           MemberNameInfo, Var->getType().getNonReferenceType(),
1147
267
                           VK_LValue, OK_Ordinary);
1148
267
  }
1149
245k
1150
245k
  if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) {
1151
245k
    ExprValueKind valueKind;
1152
245k
    QualType type;
1153
245k
    if (MemberFn->isInstance()) {
1154
245k
      valueKind = VK_RValue;
1155
245k
      type = Context.BoundMemberTy;
1156
245k
    } else {
1157
247
      valueKind = VK_LValue;
1158
247
      type = MemberFn->getType();
1159
247
    }
1160
245k
1161
245k
    return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc,
1162
245k
                           MemberFn, FoundDecl, /*HadMultipleCandidates=*/false,
1163
245k
                           MemberNameInfo, type, valueKind, OK_Ordinary);
1164
245k
  }
1165
136
  assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?");
1166
136
1167
136
  if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) {
1168
58
    return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, Enum,
1169
58
                           FoundDecl, /*HadMultipleCandidates=*/false,
1170
58
                           MemberNameInfo, Enum->getType(), VK_RValue,
1171
58
                           OK_Ordinary);
1172
58
  }
1173
78
  if (VarTemplateDecl *VarTempl = dyn_cast<VarTemplateDecl>(MemberDecl)) {
1174
72
    if (VarDecl *Var = getVarTemplateSpecialization(
1175
72
            VarTempl, TemplateArgs, MemberNameInfo, TemplateKWLoc))
1176
72
      return BuildMemberExpr(
1177
72
          BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, Var, FoundDecl,
1178
72
          /*HadMultipleCandidates=*/false, MemberNameInfo,
1179
72
          Var->getType().getNonReferenceType(), VK_LValue, OK_Ordinary);
1180
0
    return ExprError();
1181
0
  }
1182
6
1183
6
  // We found something that we didn't expect. Complain.
1184
6
  if (isa<TypeDecl>(MemberDecl))
1185
6
    Diag(MemberLoc, diag::err_typecheck_member_reference_type)
1186
6
      << MemberName << BaseType << int(IsArrow);
1187
0
  else
1188
0
    Diag(MemberLoc, diag::err_typecheck_member_reference_unknown)
1189
0
      << MemberName << BaseType << int(IsArrow);
1190
6
1191
6
  Diag(MemberDecl->getLocation(), diag::note_member_declared_here)
1192
6
    << MemberName;
1193
6
  R.suppressDiagnostics();
1194
6
  return ExprError();
1195
6
}
1196
1197
/// Given that normal member access failed on the given expression,
1198
/// and given that the expression's type involves builtin-id or
1199
/// builtin-Class, decide whether substituting in the redefinition
1200
/// types would be profitable.  The redefinition type is whatever
1201
/// this translation unit tried to typedef to id/Class;  we store
1202
/// it to the side and then re-use it in places like this.
1203
4
static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
1204
4
  const ObjCObjectPointerType *opty
1205
4
    = base.get()->getType()->getAs<ObjCObjectPointerType>();
1206
4
  if (!opty) 
return false0
;
1207
4
1208
4
  const ObjCObjectType *ty = opty->getObjectType();
1209
4
1210
4
  QualType redef;
1211
4
  if (ty->isObjCId()) {
1212
2
    redef = S.Context.getObjCIdRedefinitionType();
1213
2
  } else if (ty->isObjCClass()) {
1214
2
    redef = S.Context.getObjCClassRedefinitionType();
1215
2
  } else {
1216
0
    return false;
1217
0
  }
1218
4
1219
4
  // Do the substitution as long as the redefinition type isn't just a
1220
4
  // possibly-qualified pointer to builtin-id or builtin-Class again.
1221
4
  opty = redef->getAs<ObjCObjectPointerType>();
1222
4
  if (opty && 
!opty->getObjectType()->getInterface()1
)
1223
1
    return false;
1224
3
1225
3
  base = S.ImpCastExprToType(base.get(), redef, CK_BitCast);
1226
3
  return true;
1227
3
}
1228
1229
6
static bool isRecordType(QualType T) {
1230
6
  return T->isRecordType();
1231
6
}
1232
5
static bool isPointerToRecordType(QualType T) {
1233
5
  if (const PointerType *PT = T->getAs<PointerType>())
1234
5
    return PT->getPointeeType()->isRecordType();
1235
0
  return false;
1236
0
}
1237
1238
/// Perform conversions on the LHS of a member access expression.
1239
ExprResult
1240
469k
Sema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) {
1241
469k
  if (IsArrow && 
!Base->getType()->isFunctionType()180k
)
1242
180k
    return DefaultFunctionArrayLvalueConversion(Base);
1243
288k
1244
288k
  return CheckPlaceholderExpr(Base);
1245
288k
}
1246
1247
/// Look up the given member of the given non-type-dependent
1248
/// expression.  This can return in one of two ways:
1249
///  * If it returns a sentinel null-but-valid result, the caller will
1250
///    assume that lookup was performed and the results written into
1251
///    the provided structure.  It will take over from there.
1252
///  * Otherwise, the returned expression will be produced in place of
1253
///    an ordinary member expression.
1254
///
1255
/// The ObjCImpDecl bit is a gross hack that will need to be properly
1256
/// fixed for ObjC++.
1257
static ExprResult LookupMemberExpr(Sema &S, LookupResult &R,
1258
                                   ExprResult &BaseExpr, bool &IsArrow,
1259
                                   SourceLocation OpLoc, CXXScopeSpec &SS,
1260
                                   Decl *ObjCImpDecl, bool HasTemplateArgs,
1261
410k
                                   SourceLocation TemplateKWLoc) {
1262
410k
  assert(BaseExpr.get() && "no base expression");
1263
410k
1264
410k
  // Perform default conversions.
1265
410k
  BaseExpr = S.PerformMemberExprBaseConversion(BaseExpr.get(), IsArrow);
1266
410k
  if (BaseExpr.isInvalid())
1267
1
    return ExprError();
1268
410k
1269
410k
  QualType BaseType = BaseExpr.get()->getType();
1270
410k
  assert(!BaseType->isDependentType());
1271
410k
1272
410k
  DeclarationName MemberName = R.getLookupName();
1273
410k
  SourceLocation MemberLoc = R.getNameLoc();
1274
410k
1275
410k
  // For later type-checking purposes, turn arrow accesses into dot
1276
410k
  // accesses.  The only access type we support that doesn't follow
1277
410k
  // the C equivalence "a->b === (*a).b" is ObjC property accesses,
1278
410k
  // and those never use arrows, so this is unaffected.
1279
410k
  if (IsArrow) {
1280
123k
    if (const PointerType *Ptr = BaseType->getAs<PointerType>())
1281
122k
      BaseType = Ptr->getPointeeType();
1282
1.11k
    else if (const ObjCObjectPointerType *Ptr
1283
1.08k
               = BaseType->getAs<ObjCObjectPointerType>())
1284
1.08k
      BaseType = Ptr->getPointeeType();
1285
29
    else if (BaseType->isRecordType()) {
1286
24
      // Recover from arrow accesses to records, e.g.:
1287
24
      //   struct MyRecord foo;
1288
24
      //   foo->bar
1289
24
      // This is actually well-formed in C++ if MyRecord has an
1290
24
      // overloaded operator->, but that should have been dealt with
1291
24
      // by now--or a diagnostic message already issued if a problem
1292
24
      // was encountered while looking for the overloaded operator->.
1293
24
      if (!S.getLangOpts().CPlusPlus) {
1294
1
        S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
1295
1
          << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
1296
1
          << FixItHint::CreateReplacement(OpLoc, ".");
1297
1
      }
1298
24
      IsArrow = false;
1299
24
    } else 
if (5
BaseType->isFunctionType()5
) {
1300
5
      goto fail;
1301
5
    } else {
1302
0
      S.Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
1303
0
        << BaseType << BaseExpr.get()->getSourceRange();
1304
0
      return ExprError();
1305
0
    }
1306
410k
  }
1307
410k
1308
410k
  // Handle field access to simple records.
1309
410k
  if (const RecordType *RTy = BaseType->getAs<RecordType>()) {
1310
406k
    TypoExpr *TE = nullptr;
1311
406k
    if (LookupMemberExprInRecord(S, R, BaseExpr.get(), RTy, OpLoc, IsArrow, SS,
1312
406k
                                 HasTemplateArgs, TemplateKWLoc, TE))
1313
22
      return ExprError();
1314
406k
1315
406k
    // Returning valid-but-null is how we indicate to the caller that
1316
406k
    // the lookup result was filled in. If typo correction was attempted and
1317
406k
    // failed, the lookup result will have been cleared--that combined with the
1318
406k
    // valid-but-null ExprResult will trigger the appropriate diagnostics.
1319
406k
    return ExprResult(TE);
1320
406k
  }
1321
3.79k
1322
3.79k
  // Handle ivar access to Objective-C objects.
1323
3.79k
  if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) {
1324
1.11k
    if (!SS.isEmpty() && 
!SS.isInvalid()2
) {
1325
1
      S.Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1326
1
        << 1 << SS.getScopeRep()
1327
1
        << FixItHint::CreateRemoval(SS.getRange());
1328
1
      SS.clear();
1329
1
    }
1330
1.11k
1331
1.11k
    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1332
1.11k
1333
1.11k
    // There are three cases for the base type:
1334
1.11k
    //   - builtin id (qualified or unqualified)
1335
1.11k
    //   - builtin Class (qualified or unqualified)
1336
1.11k
    //   - an interface
1337
1.11k
    ObjCInterfaceDecl *IDecl = OTy->getInterface();
1338
1.11k
    if (!IDecl) {
1339
74
      if (S.getLangOpts().ObjCAutoRefCount &&
1340
74
          
(8
OTy->isObjCId()8
||
OTy->isObjCClass()4
))
1341
8
        goto fail;
1342
66
      // There's an implicit 'isa' ivar on all objects.
1343
66
      // But we only actually find it this way on objects of type 'id',
1344
66
      // apparently.
1345
66
      if (OTy->isObjCId() && 
Member->isStr("isa")64
)
1346
62
        return new (S.Context) ObjCIsaExpr(BaseExpr.get(), IsArrow, MemberLoc,
1347
62
                                           OpLoc, S.Context.getObjCClassType());
1348
4
      if (ShouldTryAgainWithRedefinitionType(S, BaseExpr))
1349
3
        return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1350
3
                                ObjCImpDecl, HasTemplateArgs, TemplateKWLoc);
1351
1
      goto fail;
1352
1
    }
1353
1.04k
1354
1.04k
    if (S.RequireCompleteType(OpLoc, BaseType,
1355
1.04k
                              diag::err_typecheck_incomplete_tag,
1356
1.04k
                              BaseExpr.get()))
1357
0
      return ExprError();
1358
1.04k
1359
1.04k
    ObjCInterfaceDecl *ClassDeclared = nullptr;
1360
1.04k
    ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared);
1361
1.04k
1362
1.04k
    if (!IV) {
1363
24
      // Attempt to correct for typos in ivar names.
1364
24
      DeclFilterCCC<ObjCIvarDecl> Validator{};
1365
24
      Validator.IsObjCIvarLookup = IsArrow;
1366
24
      if (TypoCorrection Corrected = S.CorrectTypo(
1367
9
              R.getLookupNameInfo(), Sema::LookupMemberName, nullptr, nullptr,
1368
9
              Validator, Sema::CTK_ErrorRecovery, IDecl)) {
1369
9
        IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>();
1370
9
        S.diagnoseTypo(
1371
9
            Corrected,
1372
9
            S.PDiag(diag::err_typecheck_member_reference_ivar_suggest)
1373
9
                << IDecl->getDeclName() << MemberName);
1374
9
1375
9
        // Figure out the class that declares the ivar.
1376
9
        assert(!ClassDeclared);
1377
9
1378
9
        Decl *D = cast<Decl>(IV->getDeclContext());
1379
9
        if (auto *Category = dyn_cast<ObjCCategoryDecl>(D))
1380
1
          D = Category->getClassInterface();
1381
9
1382
9
        if (auto *Implementation = dyn_cast<ObjCImplementationDecl>(D))
1383
1
          ClassDeclared = Implementation->getClassInterface();
1384
8
        else if (auto *Interface = dyn_cast<ObjCInterfaceDecl>(D))
1385
8
          ClassDeclared = Interface;
1386
9
1387
9
        assert(ClassDeclared && "cannot query interface");
1388
15
      } else {
1389
15
        if (IsArrow &&
1390
15
            IDecl->FindPropertyDeclaration(
1391
15
                Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) {
1392
2
          S.Diag(MemberLoc, diag::err_property_found_suggest)
1393
2
              << Member << BaseExpr.get()->getType()
1394
2
              << FixItHint::CreateReplacement(OpLoc, ".");
1395
2
          return ExprError();
1396
2
        }
1397
13
1398
13
        S.Diag(MemberLoc, diag::err_typecheck_member_reference_ivar)
1399
13
            << IDecl->getDeclName() << MemberName
1400
13
            << BaseExpr.get()->getSourceRange();
1401
13
        return ExprError();
1402
13
      }
1403
24
    }
1404
1.02k
1405
1.02k
    assert(ClassDeclared);
1406
1.02k
1407
1.02k
    // If the decl being referenced had an error, return an error for this
1408
1.02k
    // sub-expr without emitting another error, in order to avoid cascading
1409
1.02k
    // error cases.
1410
1.02k
    if (IV->isInvalidDecl())
1411
0
      return ExprError();
1412
1.02k
1413
1.02k
    // Check whether we can reference this field.
1414
1.02k
    if (S.DiagnoseUseOfDecl(IV, MemberLoc))
1415
0
      return ExprError();
1416
1.02k
    if (IV->getAccessControl() != ObjCIvarDecl::Public &&
1417
1.02k
        
IV->getAccessControl() != ObjCIvarDecl::Package533
) {
1418
523
      ObjCInterfaceDecl *ClassOfMethodDecl = nullptr;
1419
523
      if (ObjCMethodDecl *MD = S.getCurMethodDecl())
1420
480
        ClassOfMethodDecl =  MD->getClassInterface();
1421
43
      else if (ObjCImpDecl && 
S.getCurFunctionDecl()23
) {
1422
23
        // Case of a c-function declared inside an objc implementation.
1423
23
        // FIXME: For a c-style function nested inside an objc implementation
1424
23
        // class, there is no implementation context available, so we pass
1425
23
        // down the context as argument to this routine. Ideally, this context
1426
23
        // need be passed down in the AST node and somehow calculated from the
1427
23
        // AST for a function decl.
1428
23
        if (ObjCImplementationDecl *IMPD =
1429
19
              dyn_cast<ObjCImplementationDecl>(ObjCImpDecl))
1430
19
          ClassOfMethodDecl = IMPD->getClassInterface();
1431
4
        else if (ObjCCategoryImplDecl* CatImplClass =
1432
4
                   dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl))
1433
4
          ClassOfMethodDecl = CatImplClass->getClassInterface();
1434
23
      }
1435
523
      if (!S.getLangOpts().DebuggerSupport) {
1436
513
        if (IV->getAccessControl() == ObjCIvarDecl::Private) {
1437
54
          if (!declaresSameEntity(ClassDeclared, IDecl) ||
1438
54
              
!declaresSameEntity(ClassOfMethodDecl, ClassDeclared)51
)
1439
10
            S.Diag(MemberLoc, diag::err_private_ivar_access)
1440
10
              << IV->getDeclName();
1441
459
        } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl))
1442
14
          // @protected
1443
14
          S.Diag(MemberLoc, diag::err_protected_ivar_access)
1444
14
              << IV->getDeclName();
1445
513
      }
1446
523
    }
1447
1.02k
    bool warn = true;
1448
1.02k
    if (S.getLangOpts().ObjCWeak) {
1449
68
      Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts();
1450
68
      if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp))
1451
4
        if (UO->getOpcode() == UO_Deref)
1452
4
          BaseExp = UO->getSubExpr()->IgnoreParenCasts();
1453
68
1454
68
      if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp))
1455
60
        if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
1456
9
          S.Diag(DE->getLocation(), diag::err_arc_weak_ivar_access);
1457
9
          warn = false;
1458
9
        }
1459
68
    }
1460
1.02k
    if (warn) {
1461
1.01k
      if (ObjCMethodDecl *MD = S.getCurMethodDecl()) {
1462
571
        ObjCMethodFamily MF = MD->getMethodFamily();
1463
571
        warn = (MF != OMF_init && 
MF != OMF_dealloc382
&&
1464
571
                
MF != OMF_finalize379
&&
1465
571
                
!S.IvarBacksCurrentMethodAccessor(IDecl, MD, IV)379
);
1466
571
      }
1467
1.01k
      if (warn)
1468
824
        S.Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName();
1469
1.01k
    }
1470
1.02k
1471
1.02k
    ObjCIvarRefExpr *Result = new (S.Context) ObjCIvarRefExpr(
1472
1.02k
        IV, IV->getUsageType(BaseType), MemberLoc, OpLoc, BaseExpr.get(),
1473
1.02k
        IsArrow);
1474
1.02k
1475
1.02k
    if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
1476
22
      if (!S.isUnevaluatedContext() &&
1477
22
          !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, MemberLoc))
1478
20
        S.getCurFunction()->recordUseOfWeak(Result);
1479
22
    }
1480
1.02k
1481
1.02k
    return Result;
1482
1.02k
  }
1483
2.68k
1484
2.68k
  // Objective-C property access.
1485
2.68k
  const ObjCObjectPointerType *OPT;
1486
2.68k
  if (!IsArrow && 
(OPT = BaseType->getAs<ObjCObjectPointerType>())2.67k
) {
1487
2.13k
    if (!SS.isEmpty() && 
!SS.isInvalid()2
) {
1488
1
      S.Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1489
1
          << 0 << SS.getScopeRep() << FixItHint::CreateRemoval(SS.getRange());
1490
1
      SS.clear();
1491
1
    }
1492
2.13k
1493
2.13k
    // This actually uses the base as an r-value.
1494
2.13k
    BaseExpr = S.DefaultLvalueConversion(BaseExpr.get());
1495
2.13k
    if (BaseExpr.isInvalid())
1496
0
      return ExprError();
1497
2.13k
1498
2.13k
    assert(S.Context.hasSameUnqualifiedType(BaseType,
1499
2.13k
                                            BaseExpr.get()->getType()));
1500
2.13k
1501
2.13k
    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1502
2.13k
1503
2.13k
    const ObjCObjectType *OT = OPT->getObjectType();
1504
2.13k
1505
2.13k
    // id, with and without qualifiers.
1506
2.13k
    if (OT->isObjCId()) {
1507
33
      // Check protocols on qualified interfaces.
1508
33
      Selector Sel = S.PP.getSelectorTable().getNullarySelector(Member);
1509
33
      if (Decl *PMDecl =
1510
23
              FindGetterSetterNameDecl(OPT, Member, Sel, S.Context)) {
1511
23
        if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) {
1512
12
          // Check the use of this declaration
1513
12
          if (S.DiagnoseUseOfDecl(PD, MemberLoc))
1514
0
            return ExprError();
1515
12
1516
12
          return new (S.Context)
1517
12
              ObjCPropertyRefExpr(PD, S.Context.PseudoObjectTy, VK_LValue,
1518
12
                                  OK_ObjCProperty, MemberLoc, BaseExpr.get());
1519
12
        }
1520
11
1521
11
        if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) {
1522
11
          Selector SetterSel =
1523
11
            SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(),
1524
11
                                                   S.PP.getSelectorTable(),
1525
11
                                                   Member);
1526
11
          ObjCMethodDecl *SMD = nullptr;
1527
11
          if (Decl *SDecl = FindGetterSetterNameDecl(OPT,
1528
7
                                                     /*Property id*/ nullptr,
1529
7
                                                     SetterSel, S.Context))
1530
7
            SMD = dyn_cast<ObjCMethodDecl>(SDecl);
1531
11
1532
11
          return new (S.Context)
1533
11
              ObjCPropertyRefExpr(OMD, SMD, S.Context.PseudoObjectTy, VK_LValue,
1534
11
                                  OK_ObjCProperty, MemberLoc, BaseExpr.get());
1535
11
        }
1536
10
      }
1537
10
      // Use of id.member can only be for a property reference. Do not
1538
10
      // use the 'id' redefinition in this case.
1539
10
      if (IsArrow && 
ShouldTryAgainWithRedefinitionType(S, BaseExpr)0
)
1540
0
        return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1541
0
                                ObjCImpDecl, HasTemplateArgs, TemplateKWLoc);
1542
10
1543
10
      return ExprError(S.Diag(MemberLoc, diag::err_property_not_found)
1544
10
                         << MemberName << BaseType);
1545
10
    }
1546
2.10k
1547
2.10k
    // 'Class', unqualified only.
1548
2.10k
    if (OT->isObjCClass()) {
1549
6
      // Only works in a method declaration (??!).
1550
6
      ObjCMethodDecl *MD = S.getCurMethodDecl();
1551
6
      if (!MD) {
1552
0
        if (ShouldTryAgainWithRedefinitionType(S, BaseExpr))
1553
0
          return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1554
0
                                  ObjCImpDecl, HasTemplateArgs, TemplateKWLoc);
1555
0
1556
0
        goto fail;
1557
0
      }
1558
6
1559
6
      // Also must look for a getter name which uses property syntax.
1560
6
      Selector Sel = S.PP.getSelectorTable().getNullarySelector(Member);
1561
6
      ObjCInterfaceDecl *IFace = MD->getClassInterface();
1562
6
      if (!IFace)
1563
1
        goto fail;
1564
5
1565
5
      ObjCMethodDecl *Getter;
1566
5
      if ((Getter = IFace->lookupClassMethod(Sel))) {
1567
3
        // Check the use of this method.
1568
3
        if (S.DiagnoseUseOfDecl(Getter, MemberLoc))
1569
0
          return ExprError();
1570
2
      } else
1571
2
        Getter = IFace->lookupPrivateMethod(Sel, false);
1572
5
      // If we found a getter then this may be a valid dot-reference, we
1573
5
      // will look for the matching setter, in case it is needed.
1574
5
      Selector SetterSel =
1575
5
        SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(),
1576
5
                                               S.PP.getSelectorTable(),
1577
5
                                               Member);
1578
5
      ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
1579
5
      if (!Setter) {
1580
3
        // If this reference is in an @implementation, also check for 'private'
1581
3
        // methods.
1582
3
        Setter = IFace->lookupPrivateMethod(SetterSel, false);
1583
3
      }
1584
5
1585
5
      if (Setter && 
S.DiagnoseUseOfDecl(Setter, MemberLoc)3
)
1586
0
        return ExprError();
1587
5
1588
5
      if (Getter || 
Setter0
) {
1589
5
        return new (S.Context) ObjCPropertyRefExpr(
1590
5
            Getter, Setter, S.Context.PseudoObjectTy, VK_LValue,
1591
5
            OK_ObjCProperty, MemberLoc, BaseExpr.get());
1592
5
      }
1593
0
1594
0
      if (ShouldTryAgainWithRedefinitionType(S, BaseExpr))
1595
0
        return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1596
0
                                ObjCImpDecl, HasTemplateArgs, TemplateKWLoc);
1597
0
1598
0
      return ExprError(S.Diag(MemberLoc, diag::err_property_not_found)
1599
0
                         << MemberName << BaseType);
1600
0
    }
1601
2.09k
1602
2.09k
    // Normal property access.
1603
2.09k
    return S.HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc, MemberName,
1604
2.09k
                                       MemberLoc, SourceLocation(), QualType(),
1605
2.09k
                                       false);
1606
2.09k
  }
1607
547
1608
547
  // Handle 'field access' to vectors, such as 'V.xx'.
1609
547
  if (BaseType->isExtVectorType()) {
1610
476
    // FIXME: this expr should store IsArrow.
1611
476
    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1612
476
    ExprValueKind VK;
1613
476
    if (IsArrow)
1614
5
      VK = VK_LValue;
1615
471
    else {
1616
471
      if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(BaseExpr.get()))
1617
2
        VK = POE->getSyntacticForm()->getValueKind();
1618
469
      else
1619
469
        VK = BaseExpr.get()->getValueKind();
1620
471
    }
1621
476
1622
476
    QualType ret = CheckExtVectorComponent(S, BaseType, VK, OpLoc,
1623
476
                                           Member, MemberLoc);
1624
476
    if (ret.isNull())
1625
23
      return ExprError();
1626
453
    Qualifiers BaseQ =
1627
453
        S.Context.getCanonicalType(BaseExpr.get()->getType()).getQualifiers();
1628
453
    ret = S.Context.getQualifiedType(ret, BaseQ);
1629
453
1630
453
    return new (S.Context)
1631
453
        ExtVectorElementExpr(ret, VK, BaseExpr.get(), *Member, MemberLoc);
1632
453
  }
1633
71
1634
71
  // Adjust builtin-sel to the appropriate redefinition type if that's
1635
71
  // not just a pointer to builtin-sel again.
1636
71
  if (IsArrow && 
BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel)6
&&
1637
71
      
!S.Context.getObjCSelRedefinitionType()->isObjCSelType()2
) {
1638
2
    BaseExpr = S.ImpCastExprToType(
1639
2
        BaseExpr.get(), S.Context.getObjCSelRedefinitionType(), CK_BitCast);
1640
2
    return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1641
2
                            ObjCImpDecl, HasTemplateArgs, TemplateKWLoc);
1642
2
  }
1643
84
1644
84
  // Failure cases.
1645
84
 fail:
1646
84
1647
84
  // Recover from dot accesses to pointers, e.g.:
1648
84
  //   type *foo;
1649
84
  //   foo.bar
1650
84
  // This is actually well-formed in two cases:
1651
84
  //   - 'type' is an Objective C type
1652
84
  //   - 'bar' is a pseudo-destructor name which happens to refer to
1653
84
  //     the appropriate pointer type
1654
84
  if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
1655
29
    if (!IsArrow && 
Ptr->getPointeeType()->isRecordType()28
&&
1656
29
        
MemberName.getNameKind() != DeclarationName::CXXDestructorName25
) {
1657
25
      S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
1658
25
          << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
1659
25
          << FixItHint::CreateReplacement(OpLoc, "->");
1660
25
1661
25
      // Recurse as an -> access.
1662
25
      IsArrow = true;
1663
25
      return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1664
25
                              ObjCImpDecl, HasTemplateArgs, TemplateKWLoc);
1665
25
    }
1666
59
  }
1667
59
1668
59
  // If the user is trying to apply -> or . to a function name, it's probably
1669
59
  // because they forgot parentheses to call that function.
1670
59
  if (S.tryToRecoverWithCall(
1671
59
          BaseExpr, S.PDiag(diag::err_member_reference_needs_call),
1672
59
          /*complain*/ false,
1673
59
          IsArrow ? 
&isPointerToRecordType18
:
&isRecordType41
)) {
1674
11
    if (BaseExpr.isInvalid())
1675
0
      return ExprError();
1676
11
    BaseExpr = S.DefaultFunctionArrayConversion(BaseExpr.get());
1677
11
    return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1678
11
                            ObjCImpDecl, HasTemplateArgs, TemplateKWLoc);
1679
11
  }
1680
48
1681
48
  S.Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
1682
48
    << BaseType << BaseExpr.get()->getSourceRange() << MemberLoc;
1683
48
1684
48
  return ExprError();
1685
48
}
1686
1687
/// The main callback when the parser finds something like
1688
///   expression . [nested-name-specifier] identifier
1689
///   expression -> [nested-name-specifier] identifier
1690
/// where 'identifier' encompasses a fairly broad spectrum of
1691
/// possibilities, including destructor and operator references.
1692
///
1693
/// \param OpKind either tok::arrow or tok::period
1694
/// \param ObjCImpDecl the current Objective-C \@implementation
1695
///   decl; this is an ugly hack around the fact that Objective-C
1696
///   \@implementations aren't properly put in the context chain
1697
ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base,
1698
                                       SourceLocation OpLoc,
1699
                                       tok::TokenKind OpKind,
1700
                                       CXXScopeSpec &SS,
1701
                                       SourceLocation TemplateKWLoc,
1702
                                       UnqualifiedId &Id,
1703
1.11M
                                       Decl *ObjCImpDecl) {
1704
1.11M
  if (SS.isSet() && 
SS.isInvalid()588
)
1705
0
    return ExprError();
1706
1.11M
1707
1.11M
  // Warn about the explicit constructor calls Microsoft extension.
1708
1.11M
  if (getLangOpts().MicrosoftExt &&
1709
1.11M
      
Id.getKind() == UnqualifiedIdKind::IK_ConstructorName2.42k
)
1710
13
    Diag(Id.getSourceRange().getBegin(),
1711
13
         diag::ext_ms_explicit_constructor_call);
1712
1.11M
1713
1.11M
  TemplateArgumentListInfo TemplateArgsBuffer;
1714
1.11M
1715
1.11M
  // Decompose the name into its component parts.
1716
1.11M
  DeclarationNameInfo NameInfo;
1717
1.11M
  const TemplateArgumentListInfo *TemplateArgs;
1718
1.11M
  DecomposeUnqualifiedId(Id, TemplateArgsBuffer,
1719
1.11M
                         NameInfo, TemplateArgs);
1720
1.11M
1721
1.11M
  DeclarationName Name = NameInfo.getName();
1722
1.11M
  bool IsArrow = (OpKind == tok::arrow);
1723
1.11M
1724
1.11M
  NamedDecl *FirstQualifierInScope
1725
1.11M
    = (!SS.isSet() ? 
nullptr1.11M
:
FindFirstQualifierInScope(S, SS.getScopeRep())588
);
1726
1.11M
1727
1.11M
  // This is a postfix expression, so get rid of ParenListExprs.
1728
1.11M
  ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
1729
1.11M
  if (Result.isInvalid()) 
return ExprError()0
;
1730
1.11M
  Base = Result.get();
1731
1.11M
1732
1.11M
  if (Base->getType()->isDependentType() || 
Name.isDependentName()330k
||
1733
1.11M
      
isDependentScopeSpecifier(SS)330k
) {
1734
786k
    return ActOnDependentMemberExpr(Base, Base->getType(), IsArrow, OpLoc, SS,
1735
786k
                                    TemplateKWLoc, FirstQualifierInScope,
1736
786k
                                    NameInfo, TemplateArgs);
1737
786k
  }
1738
330k
1739
330k
  ActOnMemberAccessExtraArgs ExtraArgs = {S, Id, ObjCImpDecl};
1740
330k
  ExprResult Res = BuildMemberReferenceExpr(
1741
330k
      Base, Base->getType(), OpLoc, IsArrow, SS, TemplateKWLoc,
1742
330k
      FirstQualifierInScope, NameInfo, TemplateArgs, S, &ExtraArgs);
1743
330k
1744
330k
  if (!Res.isInvalid() && 
isa<MemberExpr>(Res.get())330k
)
1745
301k
    CheckMemberAccessOfNoDeref(cast<MemberExpr>(Res.get()));
1746
330k
1747
330k
  return Res;
1748
330k
}
1749
1750
301k
void Sema::CheckMemberAccessOfNoDeref(const MemberExpr *E) {
1751
301k
  QualType ResultTy = E->getType();
1752
301k
1753
301k
  // Do not warn on member accesses to arrays since this returns an array
1754
301k
  // lvalue and does not actually dereference memory.
1755
301k
  if (isa<ArrayType>(ResultTy))
1756
35.4k
    return;
1757
266k
1758
266k
  if (E->isArrow()) {
1759
99.5k
    if (const auto *Ptr = dyn_cast<PointerType>(
1760
99.5k
            E->getBase()->getType().getDesugaredType(Context))) {
1761
99.5k
      if (Ptr->getPointeeType()->hasAttr(attr::NoDeref))
1762
14
        ExprEvalContexts.back().PossibleDerefs.insert(E);
1763
99.5k
    }
1764
99.5k
  }
1765
266k
}
1766
1767
ExprResult
1768
Sema::BuildFieldReferenceExpr(Expr *BaseExpr, bool IsArrow,
1769
                              SourceLocation OpLoc, const CXXScopeSpec &SS,
1770
                              FieldDecl *Field, DeclAccessPair FoundDecl,
1771
753k
                              const DeclarationNameInfo &MemberNameInfo) {
1772
753k
  // x.a is an l-value if 'a' has a reference type. Otherwise:
1773
753k
  // x.a is an l-value/x-value/pr-value if the base is (and note
1774
753k
  //   that *x is always an l-value), except that if the base isn't
1775
753k
  //   an ordinary object then we must have an rvalue.
1776
753k
  ExprValueKind VK = VK_LValue;
1777
753k
  ExprObjectKind OK = OK_Ordinary;
1778
753k
  if (!IsArrow) {
1779
218k
    if (BaseExpr->getObjectKind() == OK_Ordinary)
1780
218k
      VK = BaseExpr->getValueKind();
1781
0
    else
1782
0
      VK = VK_RValue;
1783
218k
  }
1784
753k
  if (VK != VK_RValue && 
Field->isBitField()752k
)
1785
3.58k
    OK = OK_BitField;
1786
753k
1787
753k
  // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref]
1788
753k
  QualType MemberType = Field->getType();
1789
753k
  if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) {
1790
7.69k
    MemberType = Ref->getPointeeType();
1791
7.69k
    VK = VK_LValue;
1792
745k
  } else {
1793
745k
    QualType BaseType = BaseExpr->getType();
1794
745k
    if (IsArrow) 
BaseType = BaseType->getAs<PointerType>()->getPointeeType()527k
;
1795
745k
1796
745k
    Qualifiers BaseQuals = BaseType.getQualifiers();
1797
745k
1798
745k
    // GC attributes are never picked up by members.
1799
745k
    BaseQuals.removeObjCGCAttr();
1800
745k
1801
745k
    // CVR attributes from the base are picked up by members,
1802
745k
    // except that 'mutable' members don't pick up 'const'.
1803
745k
    if (Field->isMutable()) 
BaseQuals.removeConst()13.5k
;
1804
745k
1805
745k
    Qualifiers MemberQuals =
1806
745k
        Context.getCanonicalType(MemberType).getQualifiers();
1807
745k
1808
745k
    assert(!MemberQuals.hasAddressSpace());
1809
745k
1810
745k
    Qualifiers Combined = BaseQuals + MemberQuals;
1811
745k
    if (Combined != MemberQuals)
1812
187k
      MemberType = Context.getQualifiedType(MemberType, Combined);
1813
745k
  }
1814
753k
1815
753k
  auto *CurMethod = dyn_cast<CXXMethodDecl>(CurContext);
1816
753k
  if (!(CurMethod && 
CurMethod->isDefaulted()509k
))
1817
716k
    UnusedPrivateFields.remove(Field);
1818
753k
1819
753k
  ExprResult Base = PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
1820
753k
                                                  FoundDecl, Field);
1821
753k
  if (Base.isInvalid())
1822
38
    return ExprError();
1823
752k
1824
752k
  // Build a reference to a private copy for non-static data members in
1825
752k
  // non-static member functions, privatized by OpenMP constructs.
1826
752k
  if (getLangOpts().OpenMP && 
IsArrow27.4k
&&
1827
752k
      
!CurContext->isDependentContext()14.7k
&&
1828
752k
      
isa<CXXThisExpr>(Base.get()->IgnoreParenImpCasts())11.6k
) {
1829
11.0k
    if (auto *PrivateCopy = isOpenMPCapturedDecl(Field)) {
1830
1.96k
      return getOpenMPCapturedExpr(PrivateCopy, VK, OK,
1831
1.96k
                                   MemberNameInfo.getLoc());
1832
1.96k
    }
1833
751k
  }
1834
751k
1835
751k
  return BuildMemberExpr(Base.get(), IsArrow, OpLoc, &SS,
1836
751k
                         /*TemplateKWLoc=*/SourceLocation(), Field, FoundDecl,
1837
751k
                         /*HadMultipleCandidates=*/false, MemberNameInfo,
1838
751k
                         MemberType, VK, OK);
1839
751k
}
1840
1841
/// Builds an implicit member access expression.  The current context
1842
/// is known to be an instance method, and the given unqualified lookup
1843
/// set is known to contain only instance members, at least one of which
1844
/// is from an appropriate type.
1845
ExprResult
1846
Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
1847
                              SourceLocation TemplateKWLoc,
1848
                              LookupResult &R,
1849
                              const TemplateArgumentListInfo *TemplateArgs,
1850
694k
                              bool IsKnownInstance, const Scope *S) {
1851
694k
  assert(!R.empty() && !R.isAmbiguous());
1852
694k
1853
694k
  SourceLocation loc = R.getNameLoc();
1854
694k
1855
694k
  // If this is known to be an instance access, go ahead and build an
1856
694k
  // implicit 'this' expression now.
1857
694k
  // 'this' expression now.
1858
694k
  QualType ThisTy = getCurrentThisType();
1859
694k
  assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'");
1860
694k
1861
694k
  Expr *baseExpr = nullptr; // null signifies implicit access
1862
694k
  if (IsKnownInstance) {
1863
692k
    SourceLocation Loc = R.getNameLoc();
1864
692k
    if (SS.getRange().isValid())
1865
4.16k
      Loc = SS.getRange().getBegin();
1866
692k
    baseExpr = BuildCXXThisExpr(loc, ThisTy, /*IsImplicit=*/true);
1867
692k
  }
1868
694k
1869
694k
  return BuildMemberReferenceExpr(baseExpr, ThisTy,
1870
694k
                                  /*OpLoc*/ SourceLocation(),
1871
694k
                                  /*IsArrow*/ true,
1872
694k
                                  SS, TemplateKWLoc,
1873
694k
                                  /*FirstQualifierInScope*/ nullptr,
1874
694k
                                  R, TemplateArgs, S);
1875
694k
}