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