1224135Sdim//===--- SemaExprMember.cpp - Semantic Analysis for Expressions -----------===//
2224135Sdim//
3224135Sdim//                     The LLVM Compiler Infrastructure
4224135Sdim//
5224135Sdim// This file is distributed under the University of Illinois Open Source
6224135Sdim// License. See LICENSE.TXT for details.
7224135Sdim//
8224135Sdim//===----------------------------------------------------------------------===//
9224135Sdim//
10224135Sdim//  This file implements semantic analysis member access expressions.
11224135Sdim//
12224135Sdim//===----------------------------------------------------------------------===//
13224135Sdim#include "clang/Sema/SemaInternal.h"
14263509Sdim#include "clang/AST/ASTLambda.h"
15224135Sdim#include "clang/AST/DeclCXX.h"
16224135Sdim#include "clang/AST/DeclObjC.h"
17224135Sdim#include "clang/AST/DeclTemplate.h"
18224135Sdim#include "clang/AST/ExprCXX.h"
19224135Sdim#include "clang/AST/ExprObjC.h"
20224135Sdim#include "clang/Lex/Preprocessor.h"
21252723Sdim#include "clang/Sema/Lookup.h"
22252723Sdim#include "clang/Sema/Scope.h"
23252723Sdim#include "clang/Sema/ScopeInfo.h"
24224135Sdim
25224135Sdimusing namespace clang;
26224135Sdimusing namespace sema;
27224135Sdim
28252723Sdimtypedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> BaseSet;
29252723Sdimstatic bool BaseIsNotInSet(const CXXRecordDecl *Base, void *BasesPtr) {
30252723Sdim  const BaseSet &Bases = *reinterpret_cast<const BaseSet*>(BasesPtr);
31252723Sdim  return !Bases.count(Base->getCanonicalDecl());
32252723Sdim}
33252723Sdim
34224135Sdim/// Determines if the given class is provably not derived from all of
35224135Sdim/// the prospective base classes.
36252723Sdimstatic bool isProvablyNotDerivedFrom(Sema &SemaRef, CXXRecordDecl *Record,
37252723Sdim                                     const BaseSet &Bases) {
38252723Sdim  void *BasesPtr = const_cast<void*>(reinterpret_cast<const void*>(&Bases));
39252723Sdim  return BaseIsNotInSet(Record, BasesPtr) &&
40252723Sdim         Record->forallBases(BaseIsNotInSet, BasesPtr);
41224135Sdim}
42224135Sdim
43224135Sdimenum IMAKind {
44224135Sdim  /// The reference is definitely not an instance member access.
45224135Sdim  IMA_Static,
46224135Sdim
47224135Sdim  /// The reference may be an implicit instance member access.
48224135Sdim  IMA_Mixed,
49224135Sdim
50235633Sdim  /// The reference may be to an instance member, but it might be invalid if
51224135Sdim  /// so, because the context is not an instance method.
52224135Sdim  IMA_Mixed_StaticContext,
53224135Sdim
54224135Sdim  /// The reference may be to an instance member, but it is invalid if
55224135Sdim  /// so, because the context is from an unrelated class.
56224135Sdim  IMA_Mixed_Unrelated,
57224135Sdim
58224135Sdim  /// The reference is definitely an implicit instance member access.
59224135Sdim  IMA_Instance,
60224135Sdim
61224135Sdim  /// The reference may be to an unresolved using declaration.
62224135Sdim  IMA_Unresolved,
63224135Sdim
64252723Sdim  /// The reference is a contextually-permitted abstract member reference.
65252723Sdim  IMA_Abstract,
66252723Sdim
67224135Sdim  /// The reference may be to an unresolved using declaration and the
68224135Sdim  /// context is not an instance method.
69224135Sdim  IMA_Unresolved_StaticContext,
70224135Sdim
71235633Sdim  // The reference refers to a field which is not a member of the containing
72235633Sdim  // class, which is allowed because we're in C++11 mode and the context is
73235633Sdim  // unevaluated.
74235633Sdim  IMA_Field_Uneval_Context,
75235633Sdim
76224135Sdim  /// All possible referrents are instance members and the current
77224135Sdim  /// context is not an instance method.
78224135Sdim  IMA_Error_StaticContext,
79224135Sdim
80224135Sdim  /// All possible referrents are instance members of an unrelated
81224135Sdim  /// class.
82224135Sdim  IMA_Error_Unrelated
83224135Sdim};
84224135Sdim
85224135Sdim/// The given lookup names class member(s) and is not being used for
86224135Sdim/// an address-of-member expression.  Classify the type of access
87224135Sdim/// according to whether it's possible that this reference names an
88235633Sdim/// instance member.  This is best-effort in dependent contexts; it is okay to
89224135Sdim/// conservatively answer "yes", in which case some errors will simply
90224135Sdim/// not be caught until template-instantiation.
91224135Sdimstatic IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
92224135Sdim                                            Scope *CurScope,
93224135Sdim                                            const LookupResult &R) {
94224135Sdim  assert(!R.empty() && (*R.begin())->isCXXClassMember());
95224135Sdim
96224135Sdim  DeclContext *DC = SemaRef.getFunctionLevelDeclContext();
97224135Sdim
98235633Sdim  bool isStaticContext = SemaRef.CXXThisTypeOverride.isNull() &&
99235633Sdim    (!isa<CXXMethodDecl>(DC) || cast<CXXMethodDecl>(DC)->isStatic());
100224135Sdim
101224135Sdim  if (R.isUnresolvableResult())
102224135Sdim    return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved;
103224135Sdim
104224135Sdim  // Collect all the declaring classes of instance members we find.
105224135Sdim  bool hasNonInstance = false;
106235633Sdim  bool isField = false;
107252723Sdim  BaseSet Classes;
108224135Sdim  for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
109224135Sdim    NamedDecl *D = *I;
110224135Sdim
111224135Sdim    if (D->isCXXInstanceMember()) {
112252723Sdim      if (dyn_cast<FieldDecl>(D) || dyn_cast<MSPropertyDecl>(D)
113252723Sdim          || dyn_cast<IndirectFieldDecl>(D))
114235633Sdim        isField = true;
115224135Sdim
116224135Sdim      CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext());
117224135Sdim      Classes.insert(R->getCanonicalDecl());
118224135Sdim    }
119224135Sdim    else
120224135Sdim      hasNonInstance = true;
121224135Sdim  }
122224135Sdim
123224135Sdim  // If we didn't find any instance members, it can't be an implicit
124224135Sdim  // member reference.
125224135Sdim  if (Classes.empty())
126224135Sdim    return IMA_Static;
127252723Sdim
128252723Sdim  // C++11 [expr.prim.general]p12:
129252723Sdim  //   An id-expression that denotes a non-static data member or non-static
130252723Sdim  //   member function of a class can only be used:
131252723Sdim  //   (...)
132252723Sdim  //   - if that id-expression denotes a non-static data member and it
133252723Sdim  //     appears in an unevaluated operand.
134252723Sdim  //
135252723Sdim  // This rule is specific to C++11.  However, we also permit this form
136252723Sdim  // in unevaluated inline assembly operands, like the operand to a SIZE.
137252723Sdim  IMAKind AbstractInstanceResult = IMA_Static; // happens to be 'false'
138252723Sdim  assert(!AbstractInstanceResult);
139252723Sdim  switch (SemaRef.ExprEvalContexts.back().Context) {
140252723Sdim  case Sema::Unevaluated:
141252723Sdim    if (isField && SemaRef.getLangOpts().CPlusPlus11)
142252723Sdim      AbstractInstanceResult = IMA_Field_Uneval_Context;
143252723Sdim    break;
144224135Sdim
145252723Sdim  case Sema::UnevaluatedAbstract:
146252723Sdim    AbstractInstanceResult = IMA_Abstract;
147252723Sdim    break;
148252723Sdim
149252723Sdim  case Sema::ConstantEvaluated:
150252723Sdim  case Sema::PotentiallyEvaluated:
151252723Sdim  case Sema::PotentiallyEvaluatedIfUsed:
152252723Sdim    break;
153235633Sdim  }
154235633Sdim
155224135Sdim  // If the current context is not an instance method, it can't be
156224135Sdim  // an implicit member reference.
157224135Sdim  if (isStaticContext) {
158224135Sdim    if (hasNonInstance)
159235633Sdim      return IMA_Mixed_StaticContext;
160235633Sdim
161252723Sdim    return AbstractInstanceResult ? AbstractInstanceResult
162252723Sdim                                  : IMA_Error_StaticContext;
163224135Sdim  }
164224135Sdim
165224135Sdim  CXXRecordDecl *contextClass;
166224135Sdim  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
167224135Sdim    contextClass = MD->getParent()->getCanonicalDecl();
168224135Sdim  else
169224135Sdim    contextClass = cast<CXXRecordDecl>(DC);
170224135Sdim
171224135Sdim  // [class.mfct.non-static]p3:
172224135Sdim  // ...is used in the body of a non-static member function of class X,
173224135Sdim  // if name lookup (3.4.1) resolves the name in the id-expression to a
174224135Sdim  // non-static non-type member of some class C [...]
175224135Sdim  // ...if C is not X or a base class of X, the class member access expression
176224135Sdim  // is ill-formed.
177224135Sdim  if (R.getNamingClass() &&
178235633Sdim      contextClass->getCanonicalDecl() !=
179252723Sdim        R.getNamingClass()->getCanonicalDecl()) {
180252723Sdim    // If the naming class is not the current context, this was a qualified
181252723Sdim    // member name lookup, and it's sufficient to check that we have the naming
182252723Sdim    // class as a base class.
183252723Sdim    Classes.clear();
184252723Sdim    Classes.insert(R.getNamingClass()->getCanonicalDecl());
185252723Sdim  }
186224135Sdim
187224135Sdim  // If we can prove that the current context is unrelated to all the
188224135Sdim  // declaring classes, it can't be an implicit member reference (in
189224135Sdim  // which case it's an error if any of those members are selected).
190252723Sdim  if (isProvablyNotDerivedFrom(SemaRef, contextClass, Classes))
191235633Sdim    return hasNonInstance ? IMA_Mixed_Unrelated :
192252723Sdim           AbstractInstanceResult ? AbstractInstanceResult :
193252723Sdim                                    IMA_Error_Unrelated;
194224135Sdim
195224135Sdim  return (hasNonInstance ? IMA_Mixed : IMA_Instance);
196224135Sdim}
197224135Sdim
198224135Sdim/// Diagnose a reference to a field with no object available.
199235633Sdimstatic void diagnoseInstanceReference(Sema &SemaRef,
200224135Sdim                                      const CXXScopeSpec &SS,
201235633Sdim                                      NamedDecl *Rep,
202224135Sdim                                      const DeclarationNameInfo &nameInfo) {
203224135Sdim  SourceLocation Loc = nameInfo.getLoc();
204224135Sdim  SourceRange Range(Loc);
205224135Sdim  if (SS.isSet()) Range.setBegin(SS.getRange().getBegin());
206235633Sdim
207235633Sdim  DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext();
208235633Sdim  CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FunctionLevelDC);
209235633Sdim  CXXRecordDecl *ContextClass = Method ? Method->getParent() : 0;
210235633Sdim  CXXRecordDecl *RepClass = dyn_cast<CXXRecordDecl>(Rep->getDeclContext());
211235633Sdim
212235633Sdim  bool InStaticMethod = Method && Method->isStatic();
213235633Sdim  bool IsField = isa<FieldDecl>(Rep) || isa<IndirectFieldDecl>(Rep);
214235633Sdim
215235633Sdim  if (IsField && InStaticMethod)
216235633Sdim    // "invalid use of member 'x' in static member function"
217235633Sdim    SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method)
218224135Sdim        << Range << nameInfo.getName();
219235633Sdim  else if (ContextClass && RepClass && SS.isEmpty() && !InStaticMethod &&
220235633Sdim           !RepClass->Equals(ContextClass) && RepClass->Encloses(ContextClass))
221235633Sdim    // Unqualified lookup in a non-static member function found a member of an
222235633Sdim    // enclosing class.
223235633Sdim    SemaRef.Diag(Loc, diag::err_nested_non_static_member_use)
224235633Sdim      << IsField << RepClass << nameInfo.getName() << ContextClass << Range;
225235633Sdim  else if (IsField)
226224135Sdim    SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use)
227235633Sdim      << nameInfo.getName() << Range;
228235633Sdim  else
229235633Sdim    SemaRef.Diag(Loc, diag::err_member_call_without_object)
230235633Sdim      << Range;
231224135Sdim}
232224135Sdim
233224135Sdim/// Builds an expression which might be an implicit member expression.
234224135SdimExprResult
235224135SdimSema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS,
236235633Sdim                                      SourceLocation TemplateKWLoc,
237224135Sdim                                      LookupResult &R,
238224135Sdim                                const TemplateArgumentListInfo *TemplateArgs) {
239224135Sdim  switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) {
240224135Sdim  case IMA_Instance:
241235633Sdim    return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true);
242224135Sdim
243224135Sdim  case IMA_Mixed:
244224135Sdim  case IMA_Mixed_Unrelated:
245224135Sdim  case IMA_Unresolved:
246235633Sdim    return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, false);
247224135Sdim
248235633Sdim  case IMA_Field_Uneval_Context:
249235633Sdim    Diag(R.getNameLoc(), diag::warn_cxx98_compat_non_static_member_use)
250235633Sdim      << R.getLookupNameInfo().getName();
251235633Sdim    // Fall through.
252224135Sdim  case IMA_Static:
253252723Sdim  case IMA_Abstract:
254224135Sdim  case IMA_Mixed_StaticContext:
255224135Sdim  case IMA_Unresolved_StaticContext:
256235633Sdim    if (TemplateArgs || TemplateKWLoc.isValid())
257235633Sdim      return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs);
258224135Sdim    return BuildDeclarationNameExpr(SS, R, false);
259224135Sdim
260224135Sdim  case IMA_Error_StaticContext:
261224135Sdim  case IMA_Error_Unrelated:
262235633Sdim    diagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(),
263224135Sdim                              R.getLookupNameInfo());
264224135Sdim    return ExprError();
265224135Sdim  }
266224135Sdim
267224135Sdim  llvm_unreachable("unexpected instance member access kind");
268224135Sdim}
269224135Sdim
270224135Sdim/// Check an ext-vector component access expression.
271224135Sdim///
272224135Sdim/// VK should be set in advance to the value kind of the base
273224135Sdim/// expression.
274224135Sdimstatic QualType
275224135SdimCheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK,
276224135Sdim                        SourceLocation OpLoc, const IdentifierInfo *CompName,
277224135Sdim                        SourceLocation CompLoc) {
278224135Sdim  // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements,
279224135Sdim  // see FIXME there.
280224135Sdim  //
281224135Sdim  // FIXME: This logic can be greatly simplified by splitting it along
282224135Sdim  // halving/not halving and reworking the component checking.
283224135Sdim  const ExtVectorType *vecType = baseType->getAs<ExtVectorType>();
284224135Sdim
285224135Sdim  // The vector accessor can't exceed the number of elements.
286224135Sdim  const char *compStr = CompName->getNameStart();
287224135Sdim
288224135Sdim  // This flag determines whether or not the component is one of the four
289224135Sdim  // special names that indicate a subset of exactly half the elements are
290224135Sdim  // to be selected.
291224135Sdim  bool HalvingSwizzle = false;
292224135Sdim
293224135Sdim  // This flag determines whether or not CompName has an 's' char prefix,
294224135Sdim  // indicating that it is a string of hex values to be used as vector indices.
295224135Sdim  bool HexSwizzle = *compStr == 's' || *compStr == 'S';
296224135Sdim
297224135Sdim  bool HasRepeated = false;
298224135Sdim  bool HasIndex[16] = {};
299224135Sdim
300224135Sdim  int Idx;
301224135Sdim
302224135Sdim  // Check that we've found one of the special components, or that the component
303224135Sdim  // names must come from the same set.
304224135Sdim  if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
305224135Sdim      !strcmp(compStr, "even") || !strcmp(compStr, "odd")) {
306224135Sdim    HalvingSwizzle = true;
307224135Sdim  } else if (!HexSwizzle &&
308224135Sdim             (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) {
309224135Sdim    do {
310224135Sdim      if (HasIndex[Idx]) HasRepeated = true;
311224135Sdim      HasIndex[Idx] = true;
312224135Sdim      compStr++;
313224135Sdim    } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1);
314224135Sdim  } else {
315224135Sdim    if (HexSwizzle) compStr++;
316224135Sdim    while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) {
317224135Sdim      if (HasIndex[Idx]) HasRepeated = true;
318224135Sdim      HasIndex[Idx] = true;
319224135Sdim      compStr++;
320224135Sdim    }
321224135Sdim  }
322224135Sdim
323224135Sdim  if (!HalvingSwizzle && *compStr) {
324224135Sdim    // We didn't get to the end of the string. This means the component names
325224135Sdim    // didn't come from the same set *or* we encountered an illegal name.
326224135Sdim    S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal)
327226890Sdim      << StringRef(compStr, 1) << SourceRange(CompLoc);
328224135Sdim    return QualType();
329224135Sdim  }
330224135Sdim
331224135Sdim  // Ensure no component accessor exceeds the width of the vector type it
332224135Sdim  // operates on.
333224135Sdim  if (!HalvingSwizzle) {
334224135Sdim    compStr = CompName->getNameStart();
335224135Sdim
336224135Sdim    if (HexSwizzle)
337224135Sdim      compStr++;
338224135Sdim
339224135Sdim    while (*compStr) {
340224135Sdim      if (!vecType->isAccessorWithinNumElements(*compStr++)) {
341224135Sdim        S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length)
342224135Sdim          << baseType << SourceRange(CompLoc);
343224135Sdim        return QualType();
344224135Sdim      }
345224135Sdim    }
346224135Sdim  }
347224135Sdim
348224135Sdim  // The component accessor looks fine - now we need to compute the actual type.
349224135Sdim  // The vector type is implied by the component accessor. For example,
350224135Sdim  // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
351224135Sdim  // vec4.s0 is a float, vec4.s23 is a vec3, etc.
352224135Sdim  // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
353224135Sdim  unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2
354224135Sdim                                     : CompName->getLength();
355224135Sdim  if (HexSwizzle)
356224135Sdim    CompSize--;
357224135Sdim
358224135Sdim  if (CompSize == 1)
359224135Sdim    return vecType->getElementType();
360224135Sdim
361224135Sdim  if (HasRepeated) VK = VK_RValue;
362224135Sdim
363224135Sdim  QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize);
364224135Sdim  // Now look up the TypeDefDecl from the vector type. Without this,
365224135Sdim  // diagostics look bad. We want extended vector types to appear built-in.
366226890Sdim  for (Sema::ExtVectorDeclsType::iterator
367245431Sdim         I = S.ExtVectorDecls.begin(S.getExternalSource()),
368226890Sdim         E = S.ExtVectorDecls.end();
369226890Sdim       I != E; ++I) {
370226890Sdim    if ((*I)->getUnderlyingType() == VT)
371226890Sdim      return S.Context.getTypedefType(*I);
372224135Sdim  }
373226890Sdim
374224135Sdim  return VT; // should never get here (a typedef type should always be found).
375224135Sdim}
376224135Sdim
377224135Sdimstatic Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl,
378224135Sdim                                                IdentifierInfo *Member,
379224135Sdim                                                const Selector &Sel,
380224135Sdim                                                ASTContext &Context) {
381224135Sdim  if (Member)
382224135Sdim    if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member))
383224135Sdim      return PD;
384224135Sdim  if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel))
385224135Sdim    return OMD;
386224135Sdim
387224135Sdim  for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(),
388224135Sdim       E = PDecl->protocol_end(); I != E; ++I) {
389224135Sdim    if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel,
390224135Sdim                                                           Context))
391224135Sdim      return D;
392224135Sdim  }
393224135Sdim  return 0;
394224135Sdim}
395224135Sdim
396224135Sdimstatic Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy,
397224135Sdim                                      IdentifierInfo *Member,
398224135Sdim                                      const Selector &Sel,
399224135Sdim                                      ASTContext &Context) {
400224135Sdim  // Check protocols on qualified interfaces.
401224135Sdim  Decl *GDecl = 0;
402224135Sdim  for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
403224135Sdim       E = QIdTy->qual_end(); I != E; ++I) {
404224135Sdim    if (Member)
405224135Sdim      if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
406224135Sdim        GDecl = PD;
407224135Sdim        break;
408224135Sdim      }
409224135Sdim    // Also must look for a getter or setter name which uses property syntax.
410224135Sdim    if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) {
411224135Sdim      GDecl = OMD;
412224135Sdim      break;
413224135Sdim    }
414224135Sdim  }
415224135Sdim  if (!GDecl) {
416224135Sdim    for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
417224135Sdim         E = QIdTy->qual_end(); I != E; ++I) {
418224135Sdim      // Search in the protocol-qualifier list of current protocol.
419224135Sdim      GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel,
420224135Sdim                                                       Context);
421224135Sdim      if (GDecl)
422224135Sdim        return GDecl;
423224135Sdim    }
424224135Sdim  }
425224135Sdim  return GDecl;
426224135Sdim}
427224135Sdim
428224135SdimExprResult
429224135SdimSema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType,
430224135Sdim                               bool IsArrow, SourceLocation OpLoc,
431224135Sdim                               const CXXScopeSpec &SS,
432235633Sdim                               SourceLocation TemplateKWLoc,
433224135Sdim                               NamedDecl *FirstQualifierInScope,
434224135Sdim                               const DeclarationNameInfo &NameInfo,
435224135Sdim                               const TemplateArgumentListInfo *TemplateArgs) {
436224135Sdim  // Even in dependent contexts, try to diagnose base expressions with
437224135Sdim  // obviously wrong types, e.g.:
438224135Sdim  //
439224135Sdim  // T* t;
440224135Sdim  // t.f;
441224135Sdim  //
442224135Sdim  // In Obj-C++, however, the above expression is valid, since it could be
443224135Sdim  // accessing the 'f' property if T is an Obj-C interface. The extra check
444224135Sdim  // allows this, while still reporting an error if T is a struct pointer.
445224135Sdim  if (!IsArrow) {
446224135Sdim    const PointerType *PT = BaseType->getAs<PointerType>();
447235633Sdim    if (PT && (!getLangOpts().ObjC1 ||
448224135Sdim               PT->getPointeeType()->isRecordType())) {
449224135Sdim      assert(BaseExpr && "cannot happen with implicit member accesses");
450245431Sdim      Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
451245431Sdim        << BaseType << BaseExpr->getSourceRange() << NameInfo.getSourceRange();
452224135Sdim      return ExprError();
453224135Sdim    }
454224135Sdim  }
455224135Sdim
456224135Sdim  assert(BaseType->isDependentType() ||
457224135Sdim         NameInfo.getName().isDependentName() ||
458224135Sdim         isDependentScopeSpecifier(SS));
459224135Sdim
460224135Sdim  // Get the type being accessed in BaseType.  If this is an arrow, the BaseExpr
461224135Sdim  // must have pointer type, and the accessed type is the pointee.
462224135Sdim  return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType,
463224135Sdim                                                   IsArrow, OpLoc,
464224135Sdim                                               SS.getWithLocInContext(Context),
465235633Sdim                                                   TemplateKWLoc,
466224135Sdim                                                   FirstQualifierInScope,
467224135Sdim                                                   NameInfo, TemplateArgs));
468224135Sdim}
469224135Sdim
470224135Sdim/// We know that the given qualified member reference points only to
471224135Sdim/// declarations which do not belong to the static type of the base
472224135Sdim/// expression.  Diagnose the problem.
473224135Sdimstatic void DiagnoseQualifiedMemberReference(Sema &SemaRef,
474224135Sdim                                             Expr *BaseExpr,
475224135Sdim                                             QualType BaseType,
476224135Sdim                                             const CXXScopeSpec &SS,
477224135Sdim                                             NamedDecl *rep,
478224135Sdim                                       const DeclarationNameInfo &nameInfo) {
479224135Sdim  // If this is an implicit member access, use a different set of
480224135Sdim  // diagnostics.
481224135Sdim  if (!BaseExpr)
482235633Sdim    return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo);
483224135Sdim
484224135Sdim  SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated)
485224135Sdim    << SS.getRange() << rep << BaseType;
486224135Sdim}
487224135Sdim
488224135Sdim// Check whether the declarations we found through a nested-name
489224135Sdim// specifier in a member expression are actually members of the base
490224135Sdim// type.  The restriction here is:
491224135Sdim//
492224135Sdim//   C++ [expr.ref]p2:
493224135Sdim//     ... In these cases, the id-expression shall name a
494224135Sdim//     member of the class or of one of its base classes.
495224135Sdim//
496224135Sdim// So it's perfectly legitimate for the nested-name specifier to name
497224135Sdim// an unrelated class, and for us to find an overload set including
498224135Sdim// decls from classes which are not superclasses, as long as the decl
499224135Sdim// we actually pick through overload resolution is from a superclass.
500224135Sdimbool Sema::CheckQualifiedMemberReference(Expr *BaseExpr,
501224135Sdim                                         QualType BaseType,
502224135Sdim                                         const CXXScopeSpec &SS,
503224135Sdim                                         const LookupResult &R) {
504252723Sdim  CXXRecordDecl *BaseRecord =
505252723Sdim    cast_or_null<CXXRecordDecl>(computeDeclContext(BaseType));
506252723Sdim  if (!BaseRecord) {
507224135Sdim    // We can't check this yet because the base type is still
508224135Sdim    // dependent.
509224135Sdim    assert(BaseType->isDependentType());
510224135Sdim    return false;
511224135Sdim  }
512224135Sdim
513224135Sdim  for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
514224135Sdim    // If this is an implicit member reference and we find a
515224135Sdim    // non-instance member, it's not an error.
516224135Sdim    if (!BaseExpr && !(*I)->isCXXInstanceMember())
517224135Sdim      return false;
518224135Sdim
519224135Sdim    // Note that we use the DC of the decl, not the underlying decl.
520224135Sdim    DeclContext *DC = (*I)->getDeclContext();
521224135Sdim    while (DC->isTransparentContext())
522224135Sdim      DC = DC->getParent();
523224135Sdim
524224135Sdim    if (!DC->isRecord())
525224135Sdim      continue;
526224135Sdim
527252723Sdim    CXXRecordDecl *MemberRecord = cast<CXXRecordDecl>(DC)->getCanonicalDecl();
528252723Sdim    if (BaseRecord->getCanonicalDecl() == MemberRecord ||
529252723Sdim        !BaseRecord->isProvablyNotDerivedFrom(MemberRecord))
530224135Sdim      return false;
531224135Sdim  }
532224135Sdim
533224135Sdim  DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS,
534224135Sdim                                   R.getRepresentativeDecl(),
535224135Sdim                                   R.getLookupNameInfo());
536224135Sdim  return true;
537224135Sdim}
538224135Sdim
539235633Sdimnamespace {
540235633Sdim
541235633Sdim// Callback to only accept typo corrections that are either a ValueDecl or a
542263509Sdim// FunctionTemplateDecl and are declared in the current record or, for a C++
543263509Sdim// classes, one of its base classes.
544235633Sdimclass RecordMemberExprValidatorCCC : public CorrectionCandidateCallback {
545235633Sdim public:
546263509Sdim  explicit RecordMemberExprValidatorCCC(const RecordType *RTy)
547263509Sdim      : Record(RTy->getDecl()) {}
548263509Sdim
549235633Sdim  virtual bool ValidateCandidate(const TypoCorrection &candidate) {
550235633Sdim    NamedDecl *ND = candidate.getCorrectionDecl();
551263509Sdim    // Don't accept candidates that cannot be member functions, constants,
552263509Sdim    // variables, or templates.
553263509Sdim    if (!ND || !(isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)))
554263509Sdim      return false;
555263509Sdim
556263509Sdim    // Accept candidates that occur in the current record.
557263509Sdim    if (Record->containsDecl(ND))
558263509Sdim      return true;
559263509Sdim
560263509Sdim    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Record)) {
561263509Sdim      // Accept candidates that occur in any of the current class' base classes.
562263509Sdim      for (CXXRecordDecl::base_class_const_iterator BS = RD->bases_begin(),
563263509Sdim                                                    BSEnd = RD->bases_end();
564263509Sdim           BS != BSEnd; ++BS) {
565263509Sdim        if (const RecordType *BSTy = dyn_cast_or_null<RecordType>(
566263509Sdim                BS->getType().getTypePtrOrNull())) {
567263509Sdim          if (BSTy->getDecl()->containsDecl(ND))
568263509Sdim            return true;
569263509Sdim        }
570263509Sdim      }
571263509Sdim    }
572263509Sdim
573263509Sdim    return false;
574235633Sdim  }
575263509Sdim
576263509Sdim private:
577263509Sdim  const RecordDecl *const Record;
578235633Sdim};
579235633Sdim
580235633Sdim}
581235633Sdim
582224135Sdimstatic bool
583235633SdimLookupMemberExprInRecord(Sema &SemaRef, LookupResult &R,
584224135Sdim                         SourceRange BaseRange, const RecordType *RTy,
585224135Sdim                         SourceLocation OpLoc, CXXScopeSpec &SS,
586224135Sdim                         bool HasTemplateArgs) {
587224135Sdim  RecordDecl *RDecl = RTy->getDecl();
588235633Sdim  if (!SemaRef.isThisOutsideMemberFunctionBody(QualType(RTy, 0)) &&
589235633Sdim      SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0),
590245431Sdim                                  diag::err_typecheck_incomplete_tag,
591245431Sdim                                  BaseRange))
592224135Sdim    return true;
593224135Sdim
594224135Sdim  if (HasTemplateArgs) {
595224135Sdim    // LookupTemplateName doesn't expect these both to exist simultaneously.
596224135Sdim    QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0);
597224135Sdim
598224135Sdim    bool MOUS;
599224135Sdim    SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS);
600224135Sdim    return false;
601224135Sdim  }
602224135Sdim
603224135Sdim  DeclContext *DC = RDecl;
604224135Sdim  if (SS.isSet()) {
605224135Sdim    // If the member name was a qualified-id, look into the
606224135Sdim    // nested-name-specifier.
607224135Sdim    DC = SemaRef.computeDeclContext(SS, false);
608224135Sdim
609224135Sdim    if (SemaRef.RequireCompleteDeclContext(SS, DC)) {
610224135Sdim      SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag)
611224135Sdim        << SS.getRange() << DC;
612224135Sdim      return true;
613224135Sdim    }
614224135Sdim
615224135Sdim    assert(DC && "Cannot handle non-computable dependent contexts in lookup");
616224135Sdim
617224135Sdim    if (!isa<TypeDecl>(DC)) {
618224135Sdim      SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass)
619224135Sdim        << DC << SS.getRange();
620224135Sdim      return true;
621224135Sdim    }
622224135Sdim  }
623224135Sdim
624224135Sdim  // The record definition is complete, now look up the member.
625224135Sdim  SemaRef.LookupQualifiedName(R, DC);
626224135Sdim
627224135Sdim  if (!R.empty())
628224135Sdim    return false;
629224135Sdim
630224135Sdim  // We didn't find anything with the given name, so try to correct
631224135Sdim  // for typos.
632224135Sdim  DeclarationName Name = R.getLookupName();
633263509Sdim  RecordMemberExprValidatorCCC Validator(RTy);
634224135Sdim  TypoCorrection Corrected = SemaRef.CorrectTypo(R.getLookupNameInfo(),
635224135Sdim                                                 R.getLookupKind(), NULL,
636235633Sdim                                                 &SS, Validator, DC);
637224135Sdim  R.clear();
638263509Sdim  if (Corrected.isResolved() && !Corrected.isKeyword()) {
639224135Sdim    R.setLookupName(Corrected.getCorrection());
640263509Sdim    for (TypoCorrection::decl_iterator DI = Corrected.begin(),
641263509Sdim                                       DIEnd = Corrected.end();
642263509Sdim         DI != DIEnd; ++DI) {
643263509Sdim      R.addDecl(*DI);
644263509Sdim    }
645263509Sdim    R.resolveKind();
646263509Sdim
647263509Sdim    // If we're typo-correcting to an overloaded name, we don't yet have enough
648263509Sdim    // information to do overload resolution, so we don't know which previous
649263509Sdim    // declaration to point to.
650263509Sdim    if (Corrected.isOverloaded())
651263509Sdim      Corrected.setCorrectionDecl(0);
652263509Sdim    bool DroppedSpecifier =
653263509Sdim        Corrected.WillReplaceSpecifier() &&
654263509Sdim        Name.getAsString() == Corrected.getAsString(SemaRef.getLangOpts());
655263509Sdim    SemaRef.diagnoseTypo(Corrected,
656263509Sdim                         SemaRef.PDiag(diag::err_no_member_suggest)
657263509Sdim                           << Name << DC << DroppedSpecifier << SS.getRange());
658224135Sdim  }
659224135Sdim
660224135Sdim  return false;
661224135Sdim}
662224135Sdim
663224135SdimExprResult
664224135SdimSema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType,
665224135Sdim                               SourceLocation OpLoc, bool IsArrow,
666224135Sdim                               CXXScopeSpec &SS,
667235633Sdim                               SourceLocation TemplateKWLoc,
668224135Sdim                               NamedDecl *FirstQualifierInScope,
669224135Sdim                               const DeclarationNameInfo &NameInfo,
670224135Sdim                               const TemplateArgumentListInfo *TemplateArgs) {
671224135Sdim  if (BaseType->isDependentType() ||
672224135Sdim      (SS.isSet() && isDependentScopeSpecifier(SS)))
673224135Sdim    return ActOnDependentMemberExpr(Base, BaseType,
674224135Sdim                                    IsArrow, OpLoc,
675235633Sdim                                    SS, TemplateKWLoc, FirstQualifierInScope,
676224135Sdim                                    NameInfo, TemplateArgs);
677224135Sdim
678224135Sdim  LookupResult R(*this, NameInfo, LookupMemberName);
679224135Sdim
680224135Sdim  // Implicit member accesses.
681224135Sdim  if (!Base) {
682224135Sdim    QualType RecordTy = BaseType;
683224135Sdim    if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType();
684224135Sdim    if (LookupMemberExprInRecord(*this, R, SourceRange(),
685224135Sdim                                 RecordTy->getAs<RecordType>(),
686224135Sdim                                 OpLoc, SS, TemplateArgs != 0))
687224135Sdim      return ExprError();
688224135Sdim
689224135Sdim  // Explicit member accesses.
690224135Sdim  } else {
691224135Sdim    ExprResult BaseResult = Owned(Base);
692224135Sdim    ExprResult Result =
693224135Sdim      LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
694224135Sdim                       SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0);
695224135Sdim
696224135Sdim    if (BaseResult.isInvalid())
697224135Sdim      return ExprError();
698224135Sdim    Base = BaseResult.take();
699224135Sdim
700224135Sdim    if (Result.isInvalid()) {
701224135Sdim      Owned(Base);
702224135Sdim      return ExprError();
703224135Sdim    }
704224135Sdim
705224135Sdim    if (Result.get())
706245431Sdim      return Result;
707224135Sdim
708224135Sdim    // LookupMemberExpr can modify Base, and thus change BaseType
709224135Sdim    BaseType = Base->getType();
710224135Sdim  }
711224135Sdim
712224135Sdim  return BuildMemberReferenceExpr(Base, BaseType,
713235633Sdim                                  OpLoc, IsArrow, SS, TemplateKWLoc,
714235633Sdim                                  FirstQualifierInScope, R, TemplateArgs);
715224135Sdim}
716224135Sdim
717224135Sdimstatic ExprResult
718224135SdimBuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
719224135Sdim                        const CXXScopeSpec &SS, FieldDecl *Field,
720224135Sdim                        DeclAccessPair FoundDecl,
721224135Sdim                        const DeclarationNameInfo &MemberNameInfo);
722224135Sdim
723224135SdimExprResult
724224135SdimSema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS,
725224135Sdim                                               SourceLocation loc,
726224135Sdim                                               IndirectFieldDecl *indirectField,
727263509Sdim                                               DeclAccessPair foundDecl,
728224135Sdim                                               Expr *baseObjectExpr,
729224135Sdim                                               SourceLocation opLoc) {
730224135Sdim  // First, build the expression that refers to the base object.
731224135Sdim
732224135Sdim  bool baseObjectIsPointer = false;
733224135Sdim  Qualifiers baseQuals;
734224135Sdim
735224135Sdim  // Case 1:  the base of the indirect field is not a field.
736224135Sdim  VarDecl *baseVariable = indirectField->getVarDecl();
737224135Sdim  CXXScopeSpec EmptySS;
738224135Sdim  if (baseVariable) {
739224135Sdim    assert(baseVariable->getType()->isRecordType());
740224135Sdim
741224135Sdim    // In principle we could have a member access expression that
742224135Sdim    // accesses an anonymous struct/union that's a static member of
743224135Sdim    // the base object's class.  However, under the current standard,
744224135Sdim    // static data members cannot be anonymous structs or unions.
745224135Sdim    // Supporting this is as easy as building a MemberExpr here.
746224135Sdim    assert(!baseObjectExpr && "anonymous struct/union is static data member?");
747224135Sdim
748224135Sdim    DeclarationNameInfo baseNameInfo(DeclarationName(), loc);
749224135Sdim
750224135Sdim    ExprResult result
751224135Sdim      = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable);
752224135Sdim    if (result.isInvalid()) return ExprError();
753224135Sdim
754224135Sdim    baseObjectExpr = result.take();
755224135Sdim    baseObjectIsPointer = false;
756224135Sdim    baseQuals = baseObjectExpr->getType().getQualifiers();
757224135Sdim
758224135Sdim    // Case 2: the base of the indirect field is a field and the user
759224135Sdim    // wrote a member expression.
760224135Sdim  } else if (baseObjectExpr) {
761224135Sdim    // The caller provided the base object expression. Determine
762224135Sdim    // whether its a pointer and whether it adds any qualifiers to the
763224135Sdim    // anonymous struct/union fields we're looking into.
764224135Sdim    QualType objectType = baseObjectExpr->getType();
765224135Sdim
766224135Sdim    if (const PointerType *ptr = objectType->getAs<PointerType>()) {
767224135Sdim      baseObjectIsPointer = true;
768224135Sdim      objectType = ptr->getPointeeType();
769224135Sdim    } else {
770224135Sdim      baseObjectIsPointer = false;
771224135Sdim    }
772224135Sdim    baseQuals = objectType.getQualifiers();
773224135Sdim
774224135Sdim    // Case 3: the base of the indirect field is a field and we should
775224135Sdim    // build an implicit member access.
776224135Sdim  } else {
777224135Sdim    // We've found a member of an anonymous struct/union that is
778224135Sdim    // inside a non-anonymous struct/union, so in a well-formed
779224135Sdim    // program our base object expression is "this".
780235633Sdim    QualType ThisTy = getCurrentThisType();
781224135Sdim    if (ThisTy.isNull()) {
782224135Sdim      Diag(loc, diag::err_invalid_member_use_in_static_method)
783224135Sdim        << indirectField->getDeclName();
784224135Sdim      return ExprError();
785224135Sdim    }
786224135Sdim
787224135Sdim    // Our base object expression is "this".
788235633Sdim    CheckCXXThisCapture(loc);
789224135Sdim    baseObjectExpr
790224135Sdim      = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true);
791224135Sdim    baseObjectIsPointer = true;
792224135Sdim    baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers();
793224135Sdim  }
794224135Sdim
795224135Sdim  // Build the implicit member references to the field of the
796224135Sdim  // anonymous struct/union.
797224135Sdim  Expr *result = baseObjectExpr;
798224135Sdim  IndirectFieldDecl::chain_iterator
799224135Sdim  FI = indirectField->chain_begin(), FEnd = indirectField->chain_end();
800224135Sdim
801224135Sdim  // Build the first member access in the chain with full information.
802224135Sdim  if (!baseVariable) {
803224135Sdim    FieldDecl *field = cast<FieldDecl>(*FI);
804224135Sdim
805224135Sdim    // Make a nameInfo that properly uses the anonymous name.
806224135Sdim    DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
807224135Sdim
808224135Sdim    result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer,
809224135Sdim                                     EmptySS, field, foundDecl,
810224135Sdim                                     memberNameInfo).take();
811263509Sdim    if (!result)
812263509Sdim      return ExprError();
813263509Sdim
814224135Sdim    baseObjectIsPointer = false;
815224135Sdim
816224135Sdim    // FIXME: check qualified member access
817224135Sdim  }
818224135Sdim
819224135Sdim  // In all cases, we should now skip the first declaration in the chain.
820224135Sdim  ++FI;
821224135Sdim
822224135Sdim  while (FI != FEnd) {
823224135Sdim    FieldDecl *field = cast<FieldDecl>(*FI++);
824263509Sdim
825224135Sdim    // FIXME: these are somewhat meaningless
826224135Sdim    DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
827263509Sdim    DeclAccessPair fakeFoundDecl =
828263509Sdim        DeclAccessPair::make(field, field->getAccess());
829263509Sdim
830224135Sdim    result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false,
831263509Sdim                                     (FI == FEnd? SS : EmptySS), field,
832263509Sdim                                     fakeFoundDecl, memberNameInfo).take();
833224135Sdim  }
834224135Sdim
835224135Sdim  return Owned(result);
836224135Sdim}
837224135Sdim
838252723Sdimstatic ExprResult
839252723SdimBuildMSPropertyRefExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
840252723Sdim                       const CXXScopeSpec &SS,
841252723Sdim                       MSPropertyDecl *PD,
842252723Sdim                       const DeclarationNameInfo &NameInfo) {
843252723Sdim  // Property names are always simple identifiers and therefore never
844252723Sdim  // require any interesting additional storage.
845252723Sdim  return new (S.Context) MSPropertyRefExpr(BaseExpr, PD, IsArrow,
846252723Sdim                                           S.Context.PseudoObjectTy, VK_LValue,
847252723Sdim                                           SS.getWithLocInContext(S.Context),
848252723Sdim                                           NameInfo.getLoc());
849252723Sdim}
850252723Sdim
851224135Sdim/// \brief Build a MemberExpr AST node.
852235633Sdimstatic MemberExpr *BuildMemberExpr(Sema &SemaRef,
853235633Sdim                                   ASTContext &C, Expr *Base, bool isArrow,
854235633Sdim                                   const CXXScopeSpec &SS,
855235633Sdim                                   SourceLocation TemplateKWLoc,
856235633Sdim                                   ValueDecl *Member,
857224135Sdim                                   DeclAccessPair FoundDecl,
858224135Sdim                                   const DeclarationNameInfo &MemberNameInfo,
859224135Sdim                                   QualType Ty,
860224135Sdim                                   ExprValueKind VK, ExprObjectKind OK,
861224135Sdim                                   const TemplateArgumentListInfo *TemplateArgs = 0) {
862235633Sdim  assert((!isArrow || Base->isRValue()) && "-> base must be a pointer rvalue");
863235633Sdim  MemberExpr *E =
864235633Sdim      MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C),
865235633Sdim                         TemplateKWLoc, Member, FoundDecl, MemberNameInfo,
866235633Sdim                         TemplateArgs, Ty, VK, OK);
867235633Sdim  SemaRef.MarkMemberReferenced(E);
868235633Sdim  return E;
869224135Sdim}
870224135Sdim
871224135SdimExprResult
872224135SdimSema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
873224135Sdim                               SourceLocation OpLoc, bool IsArrow,
874224135Sdim                               const CXXScopeSpec &SS,
875235633Sdim                               SourceLocation TemplateKWLoc,
876224135Sdim                               NamedDecl *FirstQualifierInScope,
877224135Sdim                               LookupResult &R,
878245431Sdim                               const TemplateArgumentListInfo *TemplateArgs,
879245431Sdim                               bool SuppressQualifierCheck,
880245431Sdim                               ActOnMemberAccessExtraArgs *ExtraArgs) {
881224135Sdim  QualType BaseType = BaseExprType;
882224135Sdim  if (IsArrow) {
883224135Sdim    assert(BaseType->isPointerType());
884235633Sdim    BaseType = BaseType->castAs<PointerType>()->getPointeeType();
885224135Sdim  }
886224135Sdim  R.setBaseObjectType(BaseType);
887263509Sdim
888263509Sdim  LambdaScopeInfo *const CurLSI = getCurLambda();
889263509Sdim  // If this is an implicit member reference and the overloaded
890263509Sdim  // name refers to both static and non-static member functions
891263509Sdim  // (i.e. BaseExpr is null) and if we are currently processing a lambda,
892263509Sdim  // check if we should/can capture 'this'...
893263509Sdim  // Keep this example in mind:
894263509Sdim  //  struct X {
895263509Sdim  //   void f(int) { }
896263509Sdim  //   static void f(double) { }
897263509Sdim  //
898263509Sdim  //   int g() {
899263509Sdim  //     auto L = [=](auto a) {
900263509Sdim  //       return [](int i) {
901263509Sdim  //         return [=](auto b) {
902263509Sdim  //           f(b);
903263509Sdim  //           //f(decltype(a){});
904263509Sdim  //         };
905263509Sdim  //       };
906263509Sdim  //     };
907263509Sdim  //     auto M = L(0.0);
908263509Sdim  //     auto N = M(3);
909263509Sdim  //     N(5.32); // OK, must not error.
910263509Sdim  //     return 0;
911263509Sdim  //   }
912263509Sdim  //  };
913263509Sdim  //
914263509Sdim  if (!BaseExpr && CurLSI) {
915263509Sdim    SourceLocation Loc = R.getNameLoc();
916263509Sdim    if (SS.getRange().isValid())
917263509Sdim      Loc = SS.getRange().getBegin();
918263509Sdim    DeclContext *EnclosingFunctionCtx = CurContext->getParent()->getParent();
919263509Sdim    // If the enclosing function is not dependent, then this lambda is
920263509Sdim    // capture ready, so if we can capture this, do so.
921263509Sdim    if (!EnclosingFunctionCtx->isDependentContext()) {
922263509Sdim      // If the current lambda and all enclosing lambdas can capture 'this' -
923263509Sdim      // then go ahead and capture 'this' (since our unresolved overload set
924263509Sdim      // contains both static and non-static member functions).
925263509Sdim      if (!CheckCXXThisCapture(Loc, /*Explcit*/false, /*Diagnose*/false))
926263509Sdim        CheckCXXThisCapture(Loc);
927263509Sdim    } else if (CurContext->isDependentContext()) {
928263509Sdim      // ... since this is an implicit member reference, that might potentially
929263509Sdim      // involve a 'this' capture, mark 'this' for potential capture in
930263509Sdim      // enclosing lambdas.
931263509Sdim      if (CurLSI->ImpCaptureStyle != CurLSI->ImpCap_None)
932263509Sdim        CurLSI->addPotentialThisCapture(Loc);
933263509Sdim    }
934263509Sdim  }
935224135Sdim  const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo();
936224135Sdim  DeclarationName MemberName = MemberNameInfo.getName();
937224135Sdim  SourceLocation MemberLoc = MemberNameInfo.getLoc();
938224135Sdim
939224135Sdim  if (R.isAmbiguous())
940224135Sdim    return ExprError();
941224135Sdim
942224135Sdim  if (R.empty()) {
943224135Sdim    // Rederive where we looked up.
944224135Sdim    DeclContext *DC = (SS.isSet()
945224135Sdim                       ? computeDeclContext(SS, false)
946224135Sdim                       : BaseType->getAs<RecordType>()->getDecl());
947224135Sdim
948245431Sdim    if (ExtraArgs) {
949245431Sdim      ExprResult RetryExpr;
950245431Sdim      if (!IsArrow && BaseExpr) {
951245431Sdim        SFINAETrap Trap(*this, true);
952245431Sdim        ParsedType ObjectType;
953245431Sdim        bool MayBePseudoDestructor = false;
954245431Sdim        RetryExpr = ActOnStartCXXMemberReference(getCurScope(), BaseExpr,
955245431Sdim                                                 OpLoc, tok::arrow, ObjectType,
956245431Sdim                                                 MayBePseudoDestructor);
957245431Sdim        if (RetryExpr.isUsable() && !Trap.hasErrorOccurred()) {
958245431Sdim          CXXScopeSpec TempSS(SS);
959245431Sdim          RetryExpr = ActOnMemberAccessExpr(
960245431Sdim              ExtraArgs->S, RetryExpr.get(), OpLoc, tok::arrow, TempSS,
961245431Sdim              TemplateKWLoc, ExtraArgs->Id, ExtraArgs->ObjCImpDecl,
962245431Sdim              ExtraArgs->HasTrailingLParen);
963245431Sdim        }
964245431Sdim        if (Trap.hasErrorOccurred())
965245431Sdim          RetryExpr = ExprError();
966245431Sdim      }
967245431Sdim      if (RetryExpr.isUsable()) {
968245431Sdim        Diag(OpLoc, diag::err_no_member_overloaded_arrow)
969245431Sdim          << MemberName << DC << FixItHint::CreateReplacement(OpLoc, "->");
970245431Sdim        return RetryExpr;
971245431Sdim      }
972245431Sdim    }
973245431Sdim
974224135Sdim    Diag(R.getNameLoc(), diag::err_no_member)
975224135Sdim      << MemberName << DC
976224135Sdim      << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange());
977224135Sdim    return ExprError();
978224135Sdim  }
979224135Sdim
980224135Sdim  // Diagnose lookups that find only declarations from a non-base
981224135Sdim  // type.  This is possible for either qualified lookups (which may
982224135Sdim  // have been qualified with an unrelated type) or implicit member
983224135Sdim  // expressions (which were found with unqualified lookup and thus
984224135Sdim  // may have come from an enclosing scope).  Note that it's okay for
985224135Sdim  // lookup to find declarations from a non-base type as long as those
986224135Sdim  // aren't the ones picked by overload resolution.
987224135Sdim  if ((SS.isSet() || !BaseExpr ||
988224135Sdim       (isa<CXXThisExpr>(BaseExpr) &&
989224135Sdim        cast<CXXThisExpr>(BaseExpr)->isImplicit())) &&
990224135Sdim      !SuppressQualifierCheck &&
991224135Sdim      CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R))
992224135Sdim    return ExprError();
993235633Sdim
994224135Sdim  // Construct an unresolved result if we in fact got an unresolved
995224135Sdim  // result.
996224135Sdim  if (R.isOverloadedResult() || R.isUnresolvableResult()) {
997224135Sdim    // Suppress any lookup-related diagnostics; we'll do these when we
998224135Sdim    // pick a member.
999224135Sdim    R.suppressDiagnostics();
1000224135Sdim
1001224135Sdim    UnresolvedMemberExpr *MemExpr
1002224135Sdim      = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(),
1003224135Sdim                                     BaseExpr, BaseExprType,
1004224135Sdim                                     IsArrow, OpLoc,
1005224135Sdim                                     SS.getWithLocInContext(Context),
1006235633Sdim                                     TemplateKWLoc, MemberNameInfo,
1007224135Sdim                                     TemplateArgs, R.begin(), R.end());
1008224135Sdim
1009224135Sdim    return Owned(MemExpr);
1010224135Sdim  }
1011224135Sdim
1012224135Sdim  assert(R.isSingleResult());
1013224135Sdim  DeclAccessPair FoundDecl = R.begin().getPair();
1014224135Sdim  NamedDecl *MemberDecl = R.getFoundDecl();
1015224135Sdim
1016224135Sdim  // FIXME: diagnose the presence of template arguments now.
1017224135Sdim
1018224135Sdim  // If the decl being referenced had an error, return an error for this
1019224135Sdim  // sub-expr without emitting another error, in order to avoid cascading
1020224135Sdim  // error cases.
1021224135Sdim  if (MemberDecl->isInvalidDecl())
1022224135Sdim    return ExprError();
1023224135Sdim
1024224135Sdim  // Handle the implicit-member-access case.
1025224135Sdim  if (!BaseExpr) {
1026224135Sdim    // If this is not an instance member, convert to a non-member access.
1027224135Sdim    if (!MemberDecl->isCXXInstanceMember())
1028224135Sdim      return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl);
1029224135Sdim
1030224135Sdim    SourceLocation Loc = R.getNameLoc();
1031224135Sdim    if (SS.getRange().isValid())
1032224135Sdim      Loc = SS.getRange().getBegin();
1033235633Sdim    CheckCXXThisCapture(Loc);
1034224135Sdim    BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true);
1035224135Sdim  }
1036224135Sdim
1037224135Sdim  bool ShouldCheckUse = true;
1038224135Sdim  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) {
1039224135Sdim    // Don't diagnose the use of a virtual member function unless it's
1040224135Sdim    // explicitly qualified.
1041224135Sdim    if (MD->isVirtual() && !SS.isSet())
1042224135Sdim      ShouldCheckUse = false;
1043224135Sdim  }
1044224135Sdim
1045224135Sdim  // Check the use of this member.
1046224135Sdim  if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) {
1047224135Sdim    Owned(BaseExpr);
1048224135Sdim    return ExprError();
1049224135Sdim  }
1050224135Sdim
1051224135Sdim  if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl))
1052224135Sdim    return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow,
1053224135Sdim                                   SS, FD, FoundDecl, MemberNameInfo);
1054224135Sdim
1055252723Sdim  if (MSPropertyDecl *PD = dyn_cast<MSPropertyDecl>(MemberDecl))
1056252723Sdim    return BuildMSPropertyRefExpr(*this, BaseExpr, IsArrow, SS, PD,
1057252723Sdim                                  MemberNameInfo);
1058252723Sdim
1059224135Sdim  if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl))
1060224135Sdim    // We may have found a field within an anonymous union or struct
1061224135Sdim    // (C++ [class.union]).
1062224135Sdim    return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD,
1063263509Sdim                                                    FoundDecl, BaseExpr,
1064263509Sdim                                                    OpLoc);
1065224135Sdim
1066224135Sdim  if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) {
1067235633Sdim    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
1068235633Sdim                                 TemplateKWLoc, Var, FoundDecl, MemberNameInfo,
1069224135Sdim                                 Var->getType().getNonReferenceType(),
1070224135Sdim                                 VK_LValue, OK_Ordinary));
1071224135Sdim  }
1072224135Sdim
1073224135Sdim  if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) {
1074224135Sdim    ExprValueKind valueKind;
1075224135Sdim    QualType type;
1076224135Sdim    if (MemberFn->isInstance()) {
1077224135Sdim      valueKind = VK_RValue;
1078224135Sdim      type = Context.BoundMemberTy;
1079224135Sdim    } else {
1080224135Sdim      valueKind = VK_LValue;
1081224135Sdim      type = MemberFn->getType();
1082224135Sdim    }
1083224135Sdim
1084235633Sdim    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
1085235633Sdim                                 TemplateKWLoc, MemberFn, FoundDecl,
1086235633Sdim                                 MemberNameInfo, type, valueKind,
1087235633Sdim                                 OK_Ordinary));
1088224135Sdim  }
1089224135Sdim  assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?");
1090224135Sdim
1091224135Sdim  if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) {
1092235633Sdim    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
1093235633Sdim                                 TemplateKWLoc, Enum, FoundDecl, MemberNameInfo,
1094224135Sdim                                 Enum->getType(), VK_RValue, OK_Ordinary));
1095224135Sdim  }
1096224135Sdim
1097224135Sdim  Owned(BaseExpr);
1098224135Sdim
1099224135Sdim  // We found something that we didn't expect. Complain.
1100224135Sdim  if (isa<TypeDecl>(MemberDecl))
1101224135Sdim    Diag(MemberLoc, diag::err_typecheck_member_reference_type)
1102224135Sdim      << MemberName << BaseType << int(IsArrow);
1103224135Sdim  else
1104224135Sdim    Diag(MemberLoc, diag::err_typecheck_member_reference_unknown)
1105224135Sdim      << MemberName << BaseType << int(IsArrow);
1106224135Sdim
1107224135Sdim  Diag(MemberDecl->getLocation(), diag::note_member_declared_here)
1108224135Sdim    << MemberName;
1109224135Sdim  R.suppressDiagnostics();
1110224135Sdim  return ExprError();
1111224135Sdim}
1112224135Sdim
1113224135Sdim/// Given that normal member access failed on the given expression,
1114224135Sdim/// and given that the expression's type involves builtin-id or
1115224135Sdim/// builtin-Class, decide whether substituting in the redefinition
1116224135Sdim/// types would be profitable.  The redefinition type is whatever
1117224135Sdim/// this translation unit tried to typedef to id/Class;  we store
1118224135Sdim/// it to the side and then re-use it in places like this.
1119224135Sdimstatic bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
1120224135Sdim  const ObjCObjectPointerType *opty
1121224135Sdim    = base.get()->getType()->getAs<ObjCObjectPointerType>();
1122224135Sdim  if (!opty) return false;
1123224135Sdim
1124224135Sdim  const ObjCObjectType *ty = opty->getObjectType();
1125224135Sdim
1126224135Sdim  QualType redef;
1127224135Sdim  if (ty->isObjCId()) {
1128226890Sdim    redef = S.Context.getObjCIdRedefinitionType();
1129224135Sdim  } else if (ty->isObjCClass()) {
1130226890Sdim    redef = S.Context.getObjCClassRedefinitionType();
1131224135Sdim  } else {
1132224135Sdim    return false;
1133224135Sdim  }
1134224135Sdim
1135224135Sdim  // Do the substitution as long as the redefinition type isn't just a
1136224135Sdim  // possibly-qualified pointer to builtin-id or builtin-Class again.
1137224135Sdim  opty = redef->getAs<ObjCObjectPointerType>();
1138245431Sdim  if (opty && !opty->getObjectType()->getInterface())
1139224135Sdim    return false;
1140224135Sdim
1141224135Sdim  base = S.ImpCastExprToType(base.take(), redef, CK_BitCast);
1142224135Sdim  return true;
1143224135Sdim}
1144224135Sdim
1145226890Sdimstatic bool isRecordType(QualType T) {
1146226890Sdim  return T->isRecordType();
1147226890Sdim}
1148226890Sdimstatic bool isPointerToRecordType(QualType T) {
1149226890Sdim  if (const PointerType *PT = T->getAs<PointerType>())
1150226890Sdim    return PT->getPointeeType()->isRecordType();
1151226890Sdim  return false;
1152226890Sdim}
1153226890Sdim
1154235633Sdim/// Perform conversions on the LHS of a member access expression.
1155235633SdimExprResult
1156235633SdimSema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) {
1157235633Sdim  if (IsArrow && !Base->getType()->isFunctionType())
1158235633Sdim    return DefaultFunctionArrayLvalueConversion(Base);
1159235633Sdim
1160235633Sdim  return CheckPlaceholderExpr(Base);
1161235633Sdim}
1162235633Sdim
1163224135Sdim/// Look up the given member of the given non-type-dependent
1164224135Sdim/// expression.  This can return in one of two ways:
1165224135Sdim///  * If it returns a sentinel null-but-valid result, the caller will
1166224135Sdim///    assume that lookup was performed and the results written into
1167224135Sdim///    the provided structure.  It will take over from there.
1168224135Sdim///  * Otherwise, the returned expression will be produced in place of
1169224135Sdim///    an ordinary member expression.
1170224135Sdim///
1171224135Sdim/// The ObjCImpDecl bit is a gross hack that will need to be properly
1172224135Sdim/// fixed for ObjC++.
1173224135SdimExprResult
1174224135SdimSema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
1175224135Sdim                       bool &IsArrow, SourceLocation OpLoc,
1176224135Sdim                       CXXScopeSpec &SS,
1177224135Sdim                       Decl *ObjCImpDecl, bool HasTemplateArgs) {
1178224135Sdim  assert(BaseExpr.get() && "no base expression");
1179224135Sdim
1180224135Sdim  // Perform default conversions.
1181235633Sdim  BaseExpr = PerformMemberExprBaseConversion(BaseExpr.take(), IsArrow);
1182226890Sdim  if (BaseExpr.isInvalid())
1183226890Sdim    return ExprError();
1184224135Sdim
1185224135Sdim  QualType BaseType = BaseExpr.get()->getType();
1186224135Sdim  assert(!BaseType->isDependentType());
1187224135Sdim
1188224135Sdim  DeclarationName MemberName = R.getLookupName();
1189224135Sdim  SourceLocation MemberLoc = R.getNameLoc();
1190224135Sdim
1191224135Sdim  // For later type-checking purposes, turn arrow accesses into dot
1192224135Sdim  // accesses.  The only access type we support that doesn't follow
1193224135Sdim  // the C equivalence "a->b === (*a).b" is ObjC property accesses,
1194224135Sdim  // and those never use arrows, so this is unaffected.
1195224135Sdim  if (IsArrow) {
1196224135Sdim    if (const PointerType *Ptr = BaseType->getAs<PointerType>())
1197224135Sdim      BaseType = Ptr->getPointeeType();
1198224135Sdim    else if (const ObjCObjectPointerType *Ptr
1199224135Sdim               = BaseType->getAs<ObjCObjectPointerType>())
1200224135Sdim      BaseType = Ptr->getPointeeType();
1201224135Sdim    else if (BaseType->isRecordType()) {
1202224135Sdim      // Recover from arrow accesses to records, e.g.:
1203224135Sdim      //   struct MyRecord foo;
1204224135Sdim      //   foo->bar
1205224135Sdim      // This is actually well-formed in C++ if MyRecord has an
1206224135Sdim      // overloaded operator->, but that should have been dealt with
1207263509Sdim      // by now--or a diagnostic message already issued if a problem
1208263509Sdim      // was encountered while looking for the overloaded operator->.
1209263509Sdim      if (!getLangOpts().CPlusPlus) {
1210263509Sdim        Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
1211263509Sdim          << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
1212263509Sdim          << FixItHint::CreateReplacement(OpLoc, ".");
1213263509Sdim      }
1214224135Sdim      IsArrow = false;
1215235633Sdim    } else if (BaseType->isFunctionType()) {
1216224135Sdim      goto fail;
1217224135Sdim    } else {
1218224135Sdim      Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
1219224135Sdim        << BaseType << BaseExpr.get()->getSourceRange();
1220224135Sdim      return ExprError();
1221224135Sdim    }
1222224135Sdim  }
1223224135Sdim
1224224135Sdim  // Handle field access to simple records.
1225224135Sdim  if (const RecordType *RTy = BaseType->getAs<RecordType>()) {
1226224135Sdim    if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(),
1227224135Sdim                                 RTy, OpLoc, SS, HasTemplateArgs))
1228224135Sdim      return ExprError();
1229224135Sdim
1230224135Sdim    // Returning valid-but-null is how we indicate to the caller that
1231224135Sdim    // the lookup result was filled in.
1232224135Sdim    return Owned((Expr*) 0);
1233224135Sdim  }
1234224135Sdim
1235224135Sdim  // Handle ivar access to Objective-C objects.
1236224135Sdim  if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) {
1237226890Sdim    if (!SS.isEmpty() && !SS.isInvalid()) {
1238226890Sdim      Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1239226890Sdim        << 1 << SS.getScopeRep()
1240226890Sdim        << FixItHint::CreateRemoval(SS.getRange());
1241226890Sdim      SS.clear();
1242226890Sdim    }
1243226890Sdim
1244224135Sdim    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1245224135Sdim
1246224135Sdim    // There are three cases for the base type:
1247224135Sdim    //   - builtin id (qualified or unqualified)
1248224135Sdim    //   - builtin Class (qualified or unqualified)
1249224135Sdim    //   - an interface
1250224135Sdim    ObjCInterfaceDecl *IDecl = OTy->getInterface();
1251224135Sdim    if (!IDecl) {
1252235633Sdim      if (getLangOpts().ObjCAutoRefCount &&
1253224135Sdim          (OTy->isObjCId() || OTy->isObjCClass()))
1254224135Sdim        goto fail;
1255224135Sdim      // There's an implicit 'isa' ivar on all objects.
1256224135Sdim      // But we only actually find it this way on objects of type 'id',
1257245431Sdim      // apparently.
1258252723Sdim      if (OTy->isObjCId() && Member->isStr("isa"))
1259224135Sdim        return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc,
1260252723Sdim                                               OpLoc,
1261224135Sdim                                               Context.getObjCClassType()));
1262224135Sdim      if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
1263224135Sdim        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1264224135Sdim                                ObjCImpDecl, HasTemplateArgs);
1265224135Sdim      goto fail;
1266224135Sdim    }
1267245431Sdim
1268245431Sdim    if (RequireCompleteType(OpLoc, BaseType, diag::err_typecheck_incomplete_tag,
1269245431Sdim                            BaseExpr.get()))
1270235633Sdim      return ExprError();
1271235633Sdim
1272235633Sdim    ObjCInterfaceDecl *ClassDeclared = 0;
1273224135Sdim    ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared);
1274224135Sdim
1275224135Sdim    if (!IV) {
1276224135Sdim      // Attempt to correct for typos in ivar names.
1277235633Sdim      DeclFilterCCC<ObjCIvarDecl> Validator;
1278235633Sdim      Validator.IsObjCIvarLookup = IsArrow;
1279235633Sdim      if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
1280235633Sdim                                                 LookupMemberName, NULL, NULL,
1281235633Sdim                                                 Validator, IDecl)) {
1282235633Sdim        IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>();
1283263509Sdim        diagnoseTypo(Corrected,
1284263509Sdim                     PDiag(diag::err_typecheck_member_reference_ivar_suggest)
1285263509Sdim                          << IDecl->getDeclName() << MemberName);
1286263509Sdim
1287235633Sdim        // Figure out the class that declares the ivar.
1288235633Sdim        assert(!ClassDeclared);
1289235633Sdim        Decl *D = cast<Decl>(IV->getDeclContext());
1290235633Sdim        if (ObjCCategoryDecl *CAT = dyn_cast<ObjCCategoryDecl>(D))
1291235633Sdim          D = CAT->getClassInterface();
1292235633Sdim        ClassDeclared = cast<ObjCInterfaceDecl>(D);
1293224135Sdim      } else {
1294224135Sdim        if (IsArrow && IDecl->FindPropertyDeclaration(Member)) {
1295224135Sdim          Diag(MemberLoc,
1296224135Sdim          diag::err_property_found_suggest)
1297224135Sdim          << Member << BaseExpr.get()->getType()
1298224135Sdim          << FixItHint::CreateReplacement(OpLoc, ".");
1299224135Sdim          return ExprError();
1300224135Sdim        }
1301224135Sdim
1302224135Sdim        Diag(MemberLoc, diag::err_typecheck_member_reference_ivar)
1303224135Sdim          << IDecl->getDeclName() << MemberName
1304224135Sdim          << BaseExpr.get()->getSourceRange();
1305224135Sdim        return ExprError();
1306224135Sdim      }
1307224135Sdim    }
1308235633Sdim
1309235633Sdim    assert(ClassDeclared);
1310224135Sdim
1311224135Sdim    // If the decl being referenced had an error, return an error for this
1312224135Sdim    // sub-expr without emitting another error, in order to avoid cascading
1313224135Sdim    // error cases.
1314224135Sdim    if (IV->isInvalidDecl())
1315224135Sdim      return ExprError();
1316224135Sdim
1317224135Sdim    // Check whether we can reference this field.
1318224135Sdim    if (DiagnoseUseOfDecl(IV, MemberLoc))
1319224135Sdim      return ExprError();
1320224135Sdim    if (IV->getAccessControl() != ObjCIvarDecl::Public &&
1321224135Sdim        IV->getAccessControl() != ObjCIvarDecl::Package) {
1322224135Sdim      ObjCInterfaceDecl *ClassOfMethodDecl = 0;
1323224135Sdim      if (ObjCMethodDecl *MD = getCurMethodDecl())
1324224135Sdim        ClassOfMethodDecl =  MD->getClassInterface();
1325224135Sdim      else if (ObjCImpDecl && getCurFunctionDecl()) {
1326224135Sdim        // Case of a c-function declared inside an objc implementation.
1327224135Sdim        // FIXME: For a c-style function nested inside an objc implementation
1328224135Sdim        // class, there is no implementation context available, so we pass
1329224135Sdim        // down the context as argument to this routine. Ideally, this context
1330224135Sdim        // need be passed down in the AST node and somehow calculated from the
1331224135Sdim        // AST for a function decl.
1332224135Sdim        if (ObjCImplementationDecl *IMPD =
1333224135Sdim              dyn_cast<ObjCImplementationDecl>(ObjCImpDecl))
1334224135Sdim          ClassOfMethodDecl = IMPD->getClassInterface();
1335224135Sdim        else if (ObjCCategoryImplDecl* CatImplClass =
1336224135Sdim                   dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl))
1337224135Sdim          ClassOfMethodDecl = CatImplClass->getClassInterface();
1338224135Sdim      }
1339235633Sdim      if (!getLangOpts().DebuggerSupport) {
1340235633Sdim        if (IV->getAccessControl() == ObjCIvarDecl::Private) {
1341235633Sdim          if (!declaresSameEntity(ClassDeclared, IDecl) ||
1342235633Sdim              !declaresSameEntity(ClassOfMethodDecl, ClassDeclared))
1343235633Sdim            Diag(MemberLoc, diag::error_private_ivar_access)
1344235633Sdim              << IV->getDeclName();
1345235633Sdim        } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl))
1346235633Sdim          // @protected
1347235633Sdim          Diag(MemberLoc, diag::error_protected_ivar_access)
1348224135Sdim            << IV->getDeclName();
1349235633Sdim      }
1350224135Sdim    }
1351245431Sdim    bool warn = true;
1352235633Sdim    if (getLangOpts().ObjCAutoRefCount) {
1353224135Sdim      Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts();
1354224135Sdim      if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp))
1355224135Sdim        if (UO->getOpcode() == UO_Deref)
1356224135Sdim          BaseExp = UO->getSubExpr()->IgnoreParenCasts();
1357224135Sdim
1358224135Sdim      if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp))
1359245431Sdim        if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
1360224135Sdim          Diag(DE->getLocation(), diag::error_arc_weak_ivar_access);
1361245431Sdim          warn = false;
1362245431Sdim        }
1363224135Sdim    }
1364245431Sdim    if (warn) {
1365245431Sdim      if (ObjCMethodDecl *MD = getCurMethodDecl()) {
1366245431Sdim        ObjCMethodFamily MF = MD->getMethodFamily();
1367245431Sdim        warn = (MF != OMF_init && MF != OMF_dealloc &&
1368252723Sdim                MF != OMF_finalize &&
1369252723Sdim                !IvarBacksCurrentMethodAccessor(IDecl, MD, IV));
1370245431Sdim      }
1371245431Sdim      if (warn)
1372245431Sdim        Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName();
1373245431Sdim    }
1374224135Sdim
1375245431Sdim    ObjCIvarRefExpr *Result = new (Context) ObjCIvarRefExpr(IV, IV->getType(),
1376252723Sdim                                                            MemberLoc, OpLoc,
1377245431Sdim                                                            BaseExpr.take(),
1378245431Sdim                                                            IsArrow);
1379245431Sdim
1380245431Sdim    if (getLangOpts().ObjCAutoRefCount) {
1381245431Sdim      if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
1382245431Sdim        DiagnosticsEngine::Level Level =
1383245431Sdim          Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak,
1384245431Sdim                                   MemberLoc);
1385245431Sdim        if (Level != DiagnosticsEngine::Ignored)
1386263509Sdim          recordUseOfEvaluatedWeak(Result);
1387245431Sdim      }
1388245431Sdim    }
1389245431Sdim
1390245431Sdim    return Owned(Result);
1391224135Sdim  }
1392224135Sdim
1393224135Sdim  // Objective-C property access.
1394224135Sdim  const ObjCObjectPointerType *OPT;
1395224135Sdim  if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) {
1396226890Sdim    if (!SS.isEmpty() && !SS.isInvalid()) {
1397226890Sdim      Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1398226890Sdim        << 0 << SS.getScopeRep()
1399226890Sdim        << FixItHint::CreateRemoval(SS.getRange());
1400226890Sdim      SS.clear();
1401226890Sdim    }
1402226890Sdim
1403224135Sdim    // This actually uses the base as an r-value.
1404224135Sdim    BaseExpr = DefaultLvalueConversion(BaseExpr.take());
1405224135Sdim    if (BaseExpr.isInvalid())
1406224135Sdim      return ExprError();
1407224135Sdim
1408224135Sdim    assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType()));
1409224135Sdim
1410224135Sdim    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1411224135Sdim
1412224135Sdim    const ObjCObjectType *OT = OPT->getObjectType();
1413224135Sdim
1414224135Sdim    // id, with and without qualifiers.
1415224135Sdim    if (OT->isObjCId()) {
1416224135Sdim      // Check protocols on qualified interfaces.
1417224135Sdim      Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
1418224135Sdim      if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) {
1419224135Sdim        if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) {
1420224135Sdim          // Check the use of this declaration
1421224135Sdim          if (DiagnoseUseOfDecl(PD, MemberLoc))
1422224135Sdim            return ExprError();
1423224135Sdim
1424235633Sdim          return Owned(new (Context) ObjCPropertyRefExpr(PD,
1425235633Sdim                                                         Context.PseudoObjectTy,
1426224135Sdim                                                         VK_LValue,
1427224135Sdim                                                         OK_ObjCProperty,
1428224135Sdim                                                         MemberLoc,
1429224135Sdim                                                         BaseExpr.take()));
1430224135Sdim        }
1431224135Sdim
1432224135Sdim        if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) {
1433224135Sdim          // Check the use of this method.
1434224135Sdim          if (DiagnoseUseOfDecl(OMD, MemberLoc))
1435224135Sdim            return ExprError();
1436224135Sdim          Selector SetterSel =
1437263509Sdim            SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
1438263509Sdim                                                   PP.getSelectorTable(),
1439263509Sdim                                                   Member);
1440224135Sdim          ObjCMethodDecl *SMD = 0;
1441224135Sdim          if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0,
1442224135Sdim                                                     SetterSel, Context))
1443224135Sdim            SMD = dyn_cast<ObjCMethodDecl>(SDecl);
1444224135Sdim
1445235633Sdim          return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD,
1446235633Sdim                                                         Context.PseudoObjectTy,
1447235633Sdim                                                         VK_LValue, OK_ObjCProperty,
1448224135Sdim                                                         MemberLoc, BaseExpr.take()));
1449224135Sdim        }
1450224135Sdim      }
1451224135Sdim      // Use of id.member can only be for a property reference. Do not
1452224135Sdim      // use the 'id' redefinition in this case.
1453224135Sdim      if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
1454224135Sdim        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1455224135Sdim                                ObjCImpDecl, HasTemplateArgs);
1456224135Sdim
1457224135Sdim      return ExprError(Diag(MemberLoc, diag::err_property_not_found)
1458224135Sdim                         << MemberName << BaseType);
1459224135Sdim    }
1460224135Sdim
1461224135Sdim    // 'Class', unqualified only.
1462224135Sdim    if (OT->isObjCClass()) {
1463224135Sdim      // Only works in a method declaration (??!).
1464224135Sdim      ObjCMethodDecl *MD = getCurMethodDecl();
1465224135Sdim      if (!MD) {
1466224135Sdim        if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
1467224135Sdim          return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1468224135Sdim                                  ObjCImpDecl, HasTemplateArgs);
1469224135Sdim
1470224135Sdim        goto fail;
1471224135Sdim      }
1472224135Sdim
1473224135Sdim      // Also must look for a getter name which uses property syntax.
1474224135Sdim      Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
1475224135Sdim      ObjCInterfaceDecl *IFace = MD->getClassInterface();
1476224135Sdim      ObjCMethodDecl *Getter;
1477224135Sdim      if ((Getter = IFace->lookupClassMethod(Sel))) {
1478224135Sdim        // Check the use of this method.
1479224135Sdim        if (DiagnoseUseOfDecl(Getter, MemberLoc))
1480224135Sdim          return ExprError();
1481224135Sdim      } else
1482224135Sdim        Getter = IFace->lookupPrivateMethod(Sel, false);
1483224135Sdim      // If we found a getter then this may be a valid dot-reference, we
1484224135Sdim      // will look for the matching setter, in case it is needed.
1485224135Sdim      Selector SetterSel =
1486263509Sdim        SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
1487263509Sdim                                               PP.getSelectorTable(),
1488263509Sdim                                               Member);
1489224135Sdim      ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
1490224135Sdim      if (!Setter) {
1491224135Sdim        // If this reference is in an @implementation, also check for 'private'
1492224135Sdim        // methods.
1493224135Sdim        Setter = IFace->lookupPrivateMethod(SetterSel, false);
1494224135Sdim      }
1495224135Sdim
1496224135Sdim      if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
1497224135Sdim        return ExprError();
1498224135Sdim
1499224135Sdim      if (Getter || Setter) {
1500224135Sdim        return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
1501235633Sdim                                                       Context.PseudoObjectTy,
1502235633Sdim                                                       VK_LValue, OK_ObjCProperty,
1503224135Sdim                                                       MemberLoc, BaseExpr.take()));
1504224135Sdim      }
1505224135Sdim
1506224135Sdim      if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
1507224135Sdim        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1508224135Sdim                                ObjCImpDecl, HasTemplateArgs);
1509224135Sdim
1510224135Sdim      return ExprError(Diag(MemberLoc, diag::err_property_not_found)
1511224135Sdim                         << MemberName << BaseType);
1512224135Sdim    }
1513224135Sdim
1514224135Sdim    // Normal property access.
1515224135Sdim    return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc,
1516224135Sdim                                     MemberName, MemberLoc,
1517224135Sdim                                     SourceLocation(), QualType(), false);
1518224135Sdim  }
1519224135Sdim
1520224135Sdim  // Handle 'field access' to vectors, such as 'V.xx'.
1521224135Sdim  if (BaseType->isExtVectorType()) {
1522224135Sdim    // FIXME: this expr should store IsArrow.
1523224135Sdim    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1524224135Sdim    ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind());
1525224135Sdim    QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc,
1526224135Sdim                                           Member, MemberLoc);
1527224135Sdim    if (ret.isNull())
1528224135Sdim      return ExprError();
1529224135Sdim
1530224135Sdim    return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(),
1531224135Sdim                                                    *Member, MemberLoc));
1532224135Sdim  }
1533224135Sdim
1534224135Sdim  // Adjust builtin-sel to the appropriate redefinition type if that's
1535224135Sdim  // not just a pointer to builtin-sel again.
1536224135Sdim  if (IsArrow &&
1537224135Sdim      BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) &&
1538226890Sdim      !Context.getObjCSelRedefinitionType()->isObjCSelType()) {
1539226890Sdim    BaseExpr = ImpCastExprToType(BaseExpr.take(),
1540226890Sdim                                 Context.getObjCSelRedefinitionType(),
1541224135Sdim                                 CK_BitCast);
1542224135Sdim    return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1543224135Sdim                            ObjCImpDecl, HasTemplateArgs);
1544224135Sdim  }
1545224135Sdim
1546224135Sdim  // Failure cases.
1547224135Sdim fail:
1548224135Sdim
1549224135Sdim  // Recover from dot accesses to pointers, e.g.:
1550224135Sdim  //   type *foo;
1551224135Sdim  //   foo.bar
1552224135Sdim  // This is actually well-formed in two cases:
1553224135Sdim  //   - 'type' is an Objective C type
1554224135Sdim  //   - 'bar' is a pseudo-destructor name which happens to refer to
1555224135Sdim  //     the appropriate pointer type
1556224135Sdim  if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
1557224135Sdim    if (!IsArrow && Ptr->getPointeeType()->isRecordType() &&
1558224135Sdim        MemberName.getNameKind() != DeclarationName::CXXDestructorName) {
1559224135Sdim      Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
1560224135Sdim        << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
1561224135Sdim          << FixItHint::CreateReplacement(OpLoc, "->");
1562224135Sdim
1563224135Sdim      // Recurse as an -> access.
1564224135Sdim      IsArrow = true;
1565224135Sdim      return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1566224135Sdim                              ObjCImpDecl, HasTemplateArgs);
1567224135Sdim    }
1568224135Sdim  }
1569224135Sdim
1570224135Sdim  // If the user is trying to apply -> or . to a function name, it's probably
1571224135Sdim  // because they forgot parentheses to call that function.
1572226890Sdim  if (tryToRecoverWithCall(BaseExpr,
1573226890Sdim                           PDiag(diag::err_member_reference_needs_call),
1574226890Sdim                           /*complain*/ false,
1575235633Sdim                           IsArrow ? &isPointerToRecordType : &isRecordType)) {
1576226890Sdim    if (BaseExpr.isInvalid())
1577224135Sdim      return ExprError();
1578226890Sdim    BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take());
1579226890Sdim    return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1580226890Sdim                            ObjCImpDecl, HasTemplateArgs);
1581224135Sdim  }
1582224135Sdim
1583245431Sdim  Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
1584245431Sdim    << BaseType << BaseExpr.get()->getSourceRange() << MemberLoc;
1585224135Sdim
1586224135Sdim  return ExprError();
1587224135Sdim}
1588224135Sdim
1589224135Sdim/// The main callback when the parser finds something like
1590224135Sdim///   expression . [nested-name-specifier] identifier
1591224135Sdim///   expression -> [nested-name-specifier] identifier
1592224135Sdim/// where 'identifier' encompasses a fairly broad spectrum of
1593224135Sdim/// possibilities, including destructor and operator references.
1594224135Sdim///
1595224135Sdim/// \param OpKind either tok::arrow or tok::period
1596224135Sdim/// \param HasTrailingLParen whether the next token is '(', which
1597224135Sdim///   is used to diagnose mis-uses of special members that can
1598224135Sdim///   only be called
1599245431Sdim/// \param ObjCImpDecl the current Objective-C \@implementation
1600245431Sdim///   decl; this is an ugly hack around the fact that Objective-C
1601245431Sdim///   \@implementations aren't properly put in the context chain
1602224135SdimExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base,
1603224135Sdim                                       SourceLocation OpLoc,
1604224135Sdim                                       tok::TokenKind OpKind,
1605224135Sdim                                       CXXScopeSpec &SS,
1606235633Sdim                                       SourceLocation TemplateKWLoc,
1607224135Sdim                                       UnqualifiedId &Id,
1608224135Sdim                                       Decl *ObjCImpDecl,
1609224135Sdim                                       bool HasTrailingLParen) {
1610224135Sdim  if (SS.isSet() && SS.isInvalid())
1611224135Sdim    return ExprError();
1612224135Sdim
1613224135Sdim  // Warn about the explicit constructor calls Microsoft extension.
1614235633Sdim  if (getLangOpts().MicrosoftExt &&
1615224135Sdim      Id.getKind() == UnqualifiedId::IK_ConstructorName)
1616224135Sdim    Diag(Id.getSourceRange().getBegin(),
1617224135Sdim         diag::ext_ms_explicit_constructor_call);
1618224135Sdim
1619224135Sdim  TemplateArgumentListInfo TemplateArgsBuffer;
1620224135Sdim
1621224135Sdim  // Decompose the name into its component parts.
1622224135Sdim  DeclarationNameInfo NameInfo;
1623224135Sdim  const TemplateArgumentListInfo *TemplateArgs;
1624224135Sdim  DecomposeUnqualifiedId(Id, TemplateArgsBuffer,
1625224135Sdim                         NameInfo, TemplateArgs);
1626224135Sdim
1627224135Sdim  DeclarationName Name = NameInfo.getName();
1628224135Sdim  bool IsArrow = (OpKind == tok::arrow);
1629224135Sdim
1630224135Sdim  NamedDecl *FirstQualifierInScope
1631224135Sdim    = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S,
1632224135Sdim                       static_cast<NestedNameSpecifier*>(SS.getScopeRep())));
1633224135Sdim
1634224135Sdim  // This is a postfix expression, so get rid of ParenListExprs.
1635224135Sdim  ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
1636224135Sdim  if (Result.isInvalid()) return ExprError();
1637224135Sdim  Base = Result.take();
1638224135Sdim
1639224135Sdim  if (Base->getType()->isDependentType() || Name.isDependentName() ||
1640224135Sdim      isDependentScopeSpecifier(SS)) {
1641224135Sdim    Result = ActOnDependentMemberExpr(Base, Base->getType(),
1642224135Sdim                                      IsArrow, OpLoc,
1643235633Sdim                                      SS, TemplateKWLoc, FirstQualifierInScope,
1644224135Sdim                                      NameInfo, TemplateArgs);
1645224135Sdim  } else {
1646224135Sdim    LookupResult R(*this, NameInfo, LookupMemberName);
1647224135Sdim    ExprResult BaseResult = Owned(Base);
1648224135Sdim    Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
1649224135Sdim                              SS, ObjCImpDecl, TemplateArgs != 0);
1650224135Sdim    if (BaseResult.isInvalid())
1651224135Sdim      return ExprError();
1652224135Sdim    Base = BaseResult.take();
1653224135Sdim
1654224135Sdim    if (Result.isInvalid()) {
1655224135Sdim      Owned(Base);
1656224135Sdim      return ExprError();
1657224135Sdim    }
1658224135Sdim
1659224135Sdim    if (Result.get()) {
1660224135Sdim      // The only way a reference to a destructor can be used is to
1661224135Sdim      // immediately call it, which falls into this case.  If the
1662224135Sdim      // next token is not a '(', produce a diagnostic and build the
1663224135Sdim      // call now.
1664224135Sdim      if (!HasTrailingLParen &&
1665224135Sdim          Id.getKind() == UnqualifiedId::IK_DestructorName)
1666224135Sdim        return DiagnoseDtorReference(NameInfo.getLoc(), Result.get());
1667224135Sdim
1668245431Sdim      return Result;
1669224135Sdim    }
1670224135Sdim
1671245431Sdim    ActOnMemberAccessExtraArgs ExtraArgs = {S, Id, ObjCImpDecl, HasTrailingLParen};
1672224135Sdim    Result = BuildMemberReferenceExpr(Base, Base->getType(),
1673235633Sdim                                      OpLoc, IsArrow, SS, TemplateKWLoc,
1674245431Sdim                                      FirstQualifierInScope, R, TemplateArgs,
1675245431Sdim                                      false, &ExtraArgs);
1676224135Sdim  }
1677224135Sdim
1678245431Sdim  return Result;
1679224135Sdim}
1680224135Sdim
1681224135Sdimstatic ExprResult
1682224135SdimBuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
1683224135Sdim                        const CXXScopeSpec &SS, FieldDecl *Field,
1684224135Sdim                        DeclAccessPair FoundDecl,
1685224135Sdim                        const DeclarationNameInfo &MemberNameInfo) {
1686224135Sdim  // x.a is an l-value if 'a' has a reference type. Otherwise:
1687224135Sdim  // x.a is an l-value/x-value/pr-value if the base is (and note
1688224135Sdim  //   that *x is always an l-value), except that if the base isn't
1689224135Sdim  //   an ordinary object then we must have an rvalue.
1690224135Sdim  ExprValueKind VK = VK_LValue;
1691224135Sdim  ExprObjectKind OK = OK_Ordinary;
1692224135Sdim  if (!IsArrow) {
1693224135Sdim    if (BaseExpr->getObjectKind() == OK_Ordinary)
1694224135Sdim      VK = BaseExpr->getValueKind();
1695224135Sdim    else
1696224135Sdim      VK = VK_RValue;
1697224135Sdim  }
1698224135Sdim  if (VK != VK_RValue && Field->isBitField())
1699224135Sdim    OK = OK_BitField;
1700224135Sdim
1701224135Sdim  // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref]
1702224135Sdim  QualType MemberType = Field->getType();
1703224135Sdim  if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) {
1704224135Sdim    MemberType = Ref->getPointeeType();
1705224135Sdim    VK = VK_LValue;
1706224135Sdim  } else {
1707224135Sdim    QualType BaseType = BaseExpr->getType();
1708224135Sdim    if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType();
1709252723Sdim
1710224135Sdim    Qualifiers BaseQuals = BaseType.getQualifiers();
1711252723Sdim
1712224135Sdim    // GC attributes are never picked up by members.
1713224135Sdim    BaseQuals.removeObjCGCAttr();
1714252723Sdim
1715224135Sdim    // CVR attributes from the base are picked up by members,
1716224135Sdim    // except that 'mutable' members don't pick up 'const'.
1717224135Sdim    if (Field->isMutable()) BaseQuals.removeConst();
1718252723Sdim
1719224135Sdim    Qualifiers MemberQuals
1720224135Sdim    = S.Context.getCanonicalType(MemberType).getQualifiers();
1721252723Sdim
1722224135Sdim    assert(!MemberQuals.hasAddressSpace());
1723252723Sdim
1724252723Sdim
1725224135Sdim    Qualifiers Combined = BaseQuals + MemberQuals;
1726224135Sdim    if (Combined != MemberQuals)
1727224135Sdim      MemberType = S.Context.getQualifiedType(MemberType, Combined);
1728224135Sdim  }
1729252723Sdim
1730245431Sdim  S.UnusedPrivateFields.remove(Field);
1731245431Sdim
1732224135Sdim  ExprResult Base =
1733224135Sdim  S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
1734224135Sdim                                  FoundDecl, Field);
1735224135Sdim  if (Base.isInvalid())
1736224135Sdim    return ExprError();
1737235633Sdim  return S.Owned(BuildMemberExpr(S, S.Context, Base.take(), IsArrow, SS,
1738235633Sdim                                 /*TemplateKWLoc=*/SourceLocation(),
1739224135Sdim                                 Field, FoundDecl, MemberNameInfo,
1740224135Sdim                                 MemberType, VK, OK));
1741224135Sdim}
1742224135Sdim
1743224135Sdim/// Builds an implicit member access expression.  The current context
1744224135Sdim/// is known to be an instance method, and the given unqualified lookup
1745224135Sdim/// set is known to contain only instance members, at least one of which
1746224135Sdim/// is from an appropriate type.
1747224135SdimExprResult
1748224135SdimSema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
1749235633Sdim                              SourceLocation TemplateKWLoc,
1750224135Sdim                              LookupResult &R,
1751224135Sdim                              const TemplateArgumentListInfo *TemplateArgs,
1752224135Sdim                              bool IsKnownInstance) {
1753224135Sdim  assert(!R.empty() && !R.isAmbiguous());
1754224135Sdim
1755224135Sdim  SourceLocation loc = R.getNameLoc();
1756224135Sdim
1757224135Sdim  // We may have found a field within an anonymous union or struct
1758224135Sdim  // (C++ [class.union]).
1759224135Sdim  // FIXME: template-ids inside anonymous structs?
1760224135Sdim  if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>())
1761263509Sdim    return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD,
1762263509Sdim                                                    R.begin().getPair());
1763224135Sdim
1764224135Sdim  // If this is known to be an instance access, go ahead and build an
1765224135Sdim  // implicit 'this' expression now.
1766224135Sdim  // 'this' expression now.
1767235633Sdim  QualType ThisTy = getCurrentThisType();
1768224135Sdim  assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'");
1769224135Sdim
1770224135Sdim  Expr *baseExpr = 0; // null signifies implicit access
1771224135Sdim  if (IsKnownInstance) {
1772224135Sdim    SourceLocation Loc = R.getNameLoc();
1773224135Sdim    if (SS.getRange().isValid())
1774224135Sdim      Loc = SS.getRange().getBegin();
1775235633Sdim    CheckCXXThisCapture(Loc);
1776224135Sdim    baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true);
1777224135Sdim  }
1778224135Sdim
1779224135Sdim  return BuildMemberReferenceExpr(baseExpr, ThisTy,
1780224135Sdim                                  /*OpLoc*/ SourceLocation(),
1781224135Sdim                                  /*IsArrow*/ true,
1782235633Sdim                                  SS, TemplateKWLoc,
1783224135Sdim                                  /*FirstQualifierInScope*/ 0,
1784224135Sdim                                  R, TemplateArgs);
1785224135Sdim}
1786