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