SemaExprMember.cpp revision 091005954a2e42e6f699dfef25369b3654397536
12b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//===--- SemaExprMember.cpp - Semantic Analysis for Expressions -----------===//
22b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
32b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//                     The LLVM Compiler Infrastructure
42b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
52b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// This file is distributed under the University of Illinois Open Source
62b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// License. See LICENSE.TXT for details.
72b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
82b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//===----------------------------------------------------------------------===//
92b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//  This file implements semantic analysis member access expressions.
112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//===----------------------------------------------------------------------===//
132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/Sema/SemaInternal.h"
142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/Sema/Lookup.h"
152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/Sema/Scope.h"
162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/DeclCXX.h"
172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/DeclObjC.h"
182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/DeclTemplate.h"
192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/ExprCXX.h"
202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/ExprObjC.h"
212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/Lex/Preprocessor.h"
222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorusing namespace clang;
242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorusing namespace sema;
252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Determines if the given class is provably not derived from all of
272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// the prospective base classes.
282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic bool IsProvablyNotDerivedFrom(Sema &SemaRef,
292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     CXXRecordDecl *Record,
302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                            const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) {
312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Bases.count(Record->getCanonicalDecl()))
322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  RecordDecl *RD = Record->getDefinition();
352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!RD) return false;
362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Record = cast<CXXRecordDecl>(RD);
372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(),
392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         E = Record->bases_end(); I != E; ++I) {
402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType());
412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>();
422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!BaseRT) return false;
432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl());
452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases))
462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return false;
472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return true;
502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorenum IMAKind {
532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference is definitely not an instance member access.
542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Static,
552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference may be an implicit instance member access.
572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Mixed,
582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
599bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman  /// The reference may be to an instance member, but it might be invalid if
602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// so, because the context is not an instance method.
612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Mixed_StaticContext,
622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference may be to an instance member, but it is invalid if
642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// so, because the context is from an unrelated class.
652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Mixed_Unrelated,
662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference is definitely an implicit instance member access.
682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Instance,
692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference may be to an unresolved using declaration.
712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Unresolved,
722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference may be to an unresolved using declaration and the
742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// context is not an instance method.
752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Unresolved_StaticContext,
762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
77ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // The reference refers to a field which is not a member of the containing
78ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // class, which is allowed because we're in C++11 mode and the context is
79ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // unevaluated.
80ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  IMA_Field_Uneval_Context,
819bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman
822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// All possible referrents are instance members and the current
832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// context is not an instance method.
842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Error_StaticContext,
852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// All possible referrents are instance members of an unrelated
872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// class.
882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Error_Unrelated
892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor};
902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The given lookup names class member(s) and is not being used for
922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// an address-of-member expression.  Classify the type of access
932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// according to whether it's possible that this reference names an
949bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman/// instance member.  This is best-effort in dependent contexts; it is okay to
952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// conservatively answer "yes", in which case some errors will simply
962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// not be caught until template-instantiation.
972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                            Scope *CurScope,
992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                            const LookupResult &R) {
1002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!R.empty() && (*R.begin())->isCXXClassMember());
1012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclContext *DC = SemaRef.getFunctionLevelDeclContext();
1032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
104cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  bool isStaticContext = SemaRef.CXXThisTypeOverride.isNull() &&
105cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    (!isa<CXXMethodDecl>(DC) || cast<CXXMethodDecl>(DC)->isStatic());
1062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.isUnresolvableResult())
1082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved;
1092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Collect all the declaring classes of instance members we find.
1112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool hasNonInstance = false;
1129bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman  bool isField = false;
1132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes;
1142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    NamedDecl *D = *I;
1162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (D->isCXXInstanceMember()) {
1181dfc4ba88714d8ac9a85dba051cf94e57f7b3e04Aaron Ballman      if (dyn_cast<FieldDecl>(D) || dyn_cast<IndirectFieldDecl>(D))
1199bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman        isField = true;
1202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext());
1222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Classes.insert(R->getCanonicalDecl());
1232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
1242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else
1252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      hasNonInstance = true;
1262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
1272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If we didn't find any instance members, it can't be an implicit
1292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // member reference.
1302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Classes.empty())
1312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return IMA_Static;
1322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
133d390de9c6312684c5e5b333f434199e193c7467aRichard Smith  bool IsCXX11UnevaluatedField = false;
1344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus0x && isField) {
1352c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    // C++11 [expr.prim.general]p12:
1362c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //   An id-expression that denotes a non-static data member or non-static
1372c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //   member function of a class can only be used:
1382c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //   (...)
1392c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //   - if that id-expression denotes a non-static data member and it
1402c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //     appears in an unevaluated operand.
1412c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    const Sema::ExpressionEvaluationContextRecord& record
1422c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith      = SemaRef.ExprEvalContexts.back();
1432c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    if (record.Context == Sema::Unevaluated)
144d390de9c6312684c5e5b333f434199e193c7467aRichard Smith      IsCXX11UnevaluatedField = true;
1452c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith  }
1462c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith
1472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If the current context is not an instance method, it can't be
1482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // an implicit member reference.
1492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (isStaticContext) {
1502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (hasNonInstance)
1512c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith      return IMA_Mixed_StaticContext;
1522c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith
153d390de9c6312684c5e5b333f434199e193c7467aRichard Smith    return IsCXX11UnevaluatedField ? IMA_Field_Uneval_Context
154d390de9c6312684c5e5b333f434199e193c7467aRichard Smith                                   : IMA_Error_StaticContext;
1552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
1562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  CXXRecordDecl *contextClass;
1582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
1592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    contextClass = MD->getParent()->getCanonicalDecl();
1602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  else
1612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    contextClass = cast<CXXRecordDecl>(DC);
1622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // [class.mfct.non-static]p3:
1642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // ...is used in the body of a non-static member function of class X,
1652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // if name lookup (3.4.1) resolves the name in the id-expression to a
1662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // non-static non-type member of some class C [...]
1672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // ...if C is not X or a base class of X, the class member access expression
1682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // is ill-formed.
1692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.getNamingClass() &&
170d08d599da101f3fe3fd79e853f1dcea6be89d7c2DeLesley Hutchins      contextClass->getCanonicalDecl() !=
171d08d599da101f3fe3fd79e853f1dcea6be89d7c2DeLesley Hutchins        R.getNamingClass()->getCanonicalDecl() &&
1722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      contextClass->isProvablyNotDerivedFrom(R.getNamingClass()))
173d390de9c6312684c5e5b333f434199e193c7467aRichard Smith    return hasNonInstance ? IMA_Mixed_Unrelated :
174d390de9c6312684c5e5b333f434199e193c7467aRichard Smith           IsCXX11UnevaluatedField ? IMA_Field_Uneval_Context :
175d390de9c6312684c5e5b333f434199e193c7467aRichard Smith                                     IMA_Error_Unrelated;
1762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If we can prove that the current context is unrelated to all the
1782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // declaring classes, it can't be an implicit member reference (in
1792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // which case it's an error if any of those members are selected).
1802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsProvablyNotDerivedFrom(SemaRef, contextClass, Classes))
181d390de9c6312684c5e5b333f434199e193c7467aRichard Smith    return hasNonInstance ? IMA_Mixed_Unrelated :
182d390de9c6312684c5e5b333f434199e193c7467aRichard Smith           IsCXX11UnevaluatedField ? IMA_Field_Uneval_Context :
183d390de9c6312684c5e5b333f434199e193c7467aRichard Smith                                     IMA_Error_Unrelated;
1842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return (hasNonInstance ? IMA_Mixed : IMA_Instance);
1862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
1872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Diagnose a reference to a field with no object available.
189a85cf39786fffd6860a940523be01eb02a4935c0Richard Smithstatic void diagnoseInstanceReference(Sema &SemaRef,
1902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      const CXXScopeSpec &SS,
191a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith                                      NamedDecl *Rep,
192ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman                                      const DeclarationNameInfo &nameInfo) {
1932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation Loc = nameInfo.getLoc();
1942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceRange Range(Loc);
1952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (SS.isSet()) Range.setBegin(SS.getRange().getBegin());
1969bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman
197a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext();
198a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FunctionLevelDC);
199a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  CXXRecordDecl *ContextClass = Method ? Method->getParent() : 0;
200a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  CXXRecordDecl *RepClass = dyn_cast<CXXRecordDecl>(Rep->getDeclContext());
201a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith
202a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  bool InStaticMethod = Method && Method->isStatic();
203a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  bool IsField = isa<FieldDecl>(Rep) || isa<IndirectFieldDecl>(Rep);
204a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith
205a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  if (IsField && InStaticMethod)
206a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith    // "invalid use of member 'x' in static member function"
207a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith    SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method)
208a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith        << Range << nameInfo.getName();
209a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  else if (ContextClass && RepClass && SS.isEmpty() && !InStaticMethod &&
210a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith           !RepClass->Equals(ContextClass) && RepClass->Encloses(ContextClass))
211a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith    // Unqualified lookup in a non-static member function found a member of an
212a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith    // enclosing class.
213a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith    SemaRef.Diag(Loc, diag::err_nested_non_static_member_use)
214a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith      << IsField << RepClass << nameInfo.getName() << ContextClass << Range;
215a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  else if (IsField)
216ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman    SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use)
217a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith      << nameInfo.getName() << Range;
218a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith  else
219a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith    SemaRef.Diag(Loc, diag::err_member_call_without_object)
220a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith      << Range;
2212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
2222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Builds an expression which might be an implicit member expression.
2242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
2252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS,
226e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      SourceLocation TemplateKWLoc,
2272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      LookupResult &R,
2282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                const TemplateArgumentListInfo *TemplateArgs) {
2292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) {
2302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  case IMA_Instance:
231e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true);
2322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  case IMA_Mixed:
2342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  case IMA_Mixed_Unrelated:
2352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  case IMA_Unresolved:
236e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara    return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, false);
2372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
238d390de9c6312684c5e5b333f434199e193c7467aRichard Smith  case IMA_Field_Uneval_Context:
239d390de9c6312684c5e5b333f434199e193c7467aRichard Smith    Diag(R.getNameLoc(), diag::warn_cxx98_compat_non_static_member_use)
240d390de9c6312684c5e5b333f434199e193c7467aRichard Smith      << R.getLookupNameInfo().getName();
241d390de9c6312684c5e5b333f434199e193c7467aRichard Smith    // Fall through.
2422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  case IMA_Static:
2432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  case IMA_Mixed_StaticContext:
2442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  case IMA_Unresolved_StaticContext:
2459d9922af13edf3ddf8804a41a98d997324fdd58eAbramo Bagnara    if (TemplateArgs || TemplateKWLoc.isValid())
2469d9922af13edf3ddf8804a41a98d997324fdd58eAbramo Bagnara      return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs);
2472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildDeclarationNameExpr(SS, R, false);
2482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  case IMA_Error_StaticContext:
2502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  case IMA_Error_Unrelated:
251a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith    diagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(),
2522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              R.getLookupNameInfo());
2532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
2542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
2552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  llvm_unreachable("unexpected instance member access kind");
2572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
2582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Check an ext-vector component access expression.
2602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///
2612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// VK should be set in advance to the value kind of the base
2622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression.
2632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic QualType
2642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorCheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK,
2652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        SourceLocation OpLoc, const IdentifierInfo *CompName,
2662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        SourceLocation CompLoc) {
2672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements,
2682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // see FIXME there.
2692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //
2702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: This logic can be greatly simplified by splitting it along
2712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // halving/not halving and reworking the component checking.
2722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ExtVectorType *vecType = baseType->getAs<ExtVectorType>();
2732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // The vector accessor can't exceed the number of elements.
2752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const char *compStr = CompName->getNameStart();
2762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This flag determines whether or not the component is one of the four
2782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // special names that indicate a subset of exactly half the elements are
2792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // to be selected.
2802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool HalvingSwizzle = false;
2812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This flag determines whether or not CompName has an 's' char prefix,
2832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // indicating that it is a string of hex values to be used as vector indices.
2842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool HexSwizzle = *compStr == 's' || *compStr == 'S';
2852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool HasRepeated = false;
2872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool HasIndex[16] = {};
2882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  int Idx;
2902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Check that we've found one of the special components, or that the component
2922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // names must come from the same set.
2932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
2942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      !strcmp(compStr, "even") || !strcmp(compStr, "odd")) {
2952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    HalvingSwizzle = true;
2962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else if (!HexSwizzle &&
2972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor             (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) {
2982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    do {
2992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (HasIndex[Idx]) HasRepeated = true;
3002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      HasIndex[Idx] = true;
3012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      compStr++;
3022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1);
3032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
3042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (HexSwizzle) compStr++;
3052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) {
3062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (HasIndex[Idx]) HasRepeated = true;
3072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      HasIndex[Idx] = true;
3082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      compStr++;
3092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
3102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
3112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!HalvingSwizzle && *compStr) {
3132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // We didn't get to the end of the string. This means the component names
3142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // didn't come from the same set *or* we encountered an illegal name.
3152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal)
3165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      << StringRef(compStr, 1) << SourceRange(CompLoc);
3172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return QualType();
3182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
3192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Ensure no component accessor exceeds the width of the vector type it
3212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // operates on.
3222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!HalvingSwizzle) {
3232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    compStr = CompName->getNameStart();
3242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (HexSwizzle)
3262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      compStr++;
3272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    while (*compStr) {
3292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!vecType->isAccessorWithinNumElements(*compStr++)) {
3302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length)
3312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << baseType << SourceRange(CompLoc);
3322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return QualType();
3332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
3342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
3352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
3362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // The component accessor looks fine - now we need to compute the actual type.
3382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // The vector type is implied by the component accessor. For example,
3392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
3402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // vec4.s0 is a float, vec4.s23 is a vec3, etc.
3412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
3422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2
3432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     : CompName->getLength();
3442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (HexSwizzle)
3452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    CompSize--;
3462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CompSize == 1)
3482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return vecType->getElementType();
3492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (HasRepeated) VK = VK_RValue;
3512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize);
3532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Now look up the TypeDefDecl from the vector type. Without this,
3542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // diagostics look bad. We want extended vector types to appear built-in.
355d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor  for (Sema::ExtVectorDeclsType::iterator
356d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor         I = S.ExtVectorDecls.begin(S.ExternalSource),
357d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor         E = S.ExtVectorDecls.end();
358d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor       I != E; ++I) {
359d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor    if ((*I)->getUnderlyingType() == VT)
360d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor      return S.Context.getTypedefType(*I);
3612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
362d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor
3632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return VT; // should never get here (a typedef type should always be found).
3642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
3652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl,
3672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                IdentifierInfo *Member,
3682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                const Selector &Sel,
3692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                ASTContext &Context) {
3702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Member)
3712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member))
3722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return PD;
3732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel))
3742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return OMD;
3752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(),
3772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor       E = PDecl->protocol_end(); I != E; ++I) {
3782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel,
3792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                           Context))
3802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return D;
3812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
3822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return 0;
3832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
3842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy,
3862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      IdentifierInfo *Member,
3872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      const Selector &Sel,
3882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      ASTContext &Context) {
3892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Check protocols on qualified interfaces.
3902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Decl *GDecl = 0;
3912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
3922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor       E = QIdTy->qual_end(); I != E; ++I) {
3932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Member)
3942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
3952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        GDecl = PD;
3962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        break;
3972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
3982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Also must look for a getter or setter name which uses property syntax.
3992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) {
4002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      GDecl = OMD;
4012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      break;
4022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
4032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
4042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!GDecl) {
4052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
4062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         E = QIdTy->qual_end(); I != E; ++I) {
4072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Search in the protocol-qualifier list of current protocol.
4082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel,
4092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                       Context);
4102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (GDecl)
4112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return GDecl;
4122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
4132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
4142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return GDecl;
4152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
4162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
4182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType,
4192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               bool IsArrow, SourceLocation OpLoc,
4202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const CXXScopeSpec &SS,
421e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                               SourceLocation TemplateKWLoc,
4222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               NamedDecl *FirstQualifierInScope,
4232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const DeclarationNameInfo &NameInfo,
4242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const TemplateArgumentListInfo *TemplateArgs) {
4252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Even in dependent contexts, try to diagnose base expressions with
4262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // obviously wrong types, e.g.:
4272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //
4282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // T* t;
4292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // t.f;
4302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //
4312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // In Obj-C++, however, the above expression is valid, since it could be
4322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // accessing the 'f' property if T is an Obj-C interface. The extra check
4332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // allows this, while still reporting an error if T is a struct pointer.
4342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!IsArrow) {
4352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    const PointerType *PT = BaseType->getAs<PointerType>();
4364e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (PT && (!getLangOpts().ObjC1 ||
4372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor               PT->getPointeeType()->isRecordType())) {
4382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      assert(BaseExpr && "cannot happen with implicit member accesses");
4397d90fe5a941efc106237d23badec816ed65e267fMatt Beaumont-Gay      Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
44073664a4e5aee9216c37bd123aa002430ccb5431dMatt Beaumont-Gay        << BaseType << BaseExpr->getSourceRange() << NameInfo.getSourceRange();
4412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
4422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
4432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
4442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(BaseType->isDependentType() ||
4462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         NameInfo.getName().isDependentName() ||
4472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         isDependentScopeSpecifier(SS));
4482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Get the type being accessed in BaseType.  If this is an arrow, the BaseExpr
4502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // must have pointer type, and the accessed type is the pointee.
4512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType,
4522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                   IsArrow, OpLoc,
4532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               SS.getWithLocInContext(Context),
454e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                   TemplateKWLoc,
4552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                   FirstQualifierInScope,
4562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                   NameInfo, TemplateArgs));
4572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
4582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// We know that the given qualified member reference points only to
4602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// declarations which do not belong to the static type of the base
4612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression.  Diagnose the problem.
4622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic void DiagnoseQualifiedMemberReference(Sema &SemaRef,
4632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                             Expr *BaseExpr,
4642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                             QualType BaseType,
4652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                             const CXXScopeSpec &SS,
4662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                             NamedDecl *rep,
4672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       const DeclarationNameInfo &nameInfo) {
4682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If this is an implicit member access, use a different set of
4692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // diagnostics.
4702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!BaseExpr)
471a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith    return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo);
4722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated)
4742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    << SS.getRange() << rep << BaseType;
4752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
4762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// Check whether the declarations we found through a nested-name
4782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// specifier in a member expression are actually members of the base
4792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// type.  The restriction here is:
4802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
4812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//   C++ [expr.ref]p2:
4822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//     ... In these cases, the id-expression shall name a
4832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//     member of the class or of one of its base classes.
4842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
4852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// So it's perfectly legitimate for the nested-name specifier to name
4862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// an unrelated class, and for us to find an overload set including
4872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// decls from classes which are not superclasses, as long as the decl
4882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// we actually pick through overload resolution is from a superclass.
4892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorbool Sema::CheckQualifiedMemberReference(Expr *BaseExpr,
4902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                         QualType BaseType,
4912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                         const CXXScopeSpec &SS,
4922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                         const LookupResult &R) {
4932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const RecordType *BaseRT = BaseType->getAs<RecordType>();
4942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!BaseRT) {
4952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // We can't check this yet because the base type is still
4962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // dependent.
4972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(BaseType->isDependentType());
4982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
4992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
5002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl());
5012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
5032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If this is an implicit member reference and we find a
5042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // non-instance member, it's not an error.
5052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!BaseExpr && !(*I)->isCXXInstanceMember())
5062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return false;
5072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Note that we use the DC of the decl, not the underlying decl.
5092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclContext *DC = (*I)->getDeclContext();
5102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    while (DC->isTransparentContext())
5112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      DC = DC->getParent();
5122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!DC->isRecord())
5142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      continue;
5152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord;
5172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl());
5182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord))
5202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return false;
5212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
5222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS,
5242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   R.getRepresentativeDecl(),
5252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   R.getLookupNameInfo());
5262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return true;
5272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
5282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
529e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrainnamespace {
530e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain
531e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain// Callback to only accept typo corrections that are either a ValueDecl or a
532e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain// FunctionTemplateDecl.
533e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrainclass RecordMemberExprValidatorCCC : public CorrectionCandidateCallback {
534e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain public:
535e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain  virtual bool ValidateCandidate(const TypoCorrection &candidate) {
536e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain    NamedDecl *ND = candidate.getCorrectionDecl();
537e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain    return ND && (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND));
538e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain  }
539e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain};
540e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain
541e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain}
542e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain
5432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic bool
544cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas GregorLookupMemberExprInRecord(Sema &SemaRef, LookupResult &R,
5452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         SourceRange BaseRange, const RecordType *RTy,
5462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         SourceLocation OpLoc, CXXScopeSpec &SS,
5472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         bool HasTemplateArgs) {
5482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  RecordDecl *RDecl = RTy->getDecl();
549cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (!SemaRef.isThisOutsideMemberFunctionBody(QualType(RTy, 0)) &&
550cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor      SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0),
551d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  diag::err_typecheck_incomplete_tag,
552d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  BaseRange))
5532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return true;
5542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (HasTemplateArgs) {
5562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // LookupTemplateName doesn't expect these both to exist simultaneously.
5572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0);
5582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    bool MOUS;
5602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS);
5612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
5622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
5632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclContext *DC = RDecl;
5652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (SS.isSet()) {
5662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If the member name was a qualified-id, look into the
5672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // nested-name-specifier.
5682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DC = SemaRef.computeDeclContext(SS, false);
5692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (SemaRef.RequireCompleteDeclContext(SS, DC)) {
5712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag)
5722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << SS.getRange() << DC;
5732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return true;
5742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
5752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(DC && "Cannot handle non-computable dependent contexts in lookup");
5772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!isa<TypeDecl>(DC)) {
5792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass)
5802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << DC << SS.getRange();
5812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return true;
5822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
5832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
5842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // The record definition is complete, now look up the member.
5862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SemaRef.LookupQualifiedName(R, DC);
5872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!R.empty())
5892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
5902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // We didn't find anything with the given name, so try to correct
5922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // for typos.
5932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName Name = R.getLookupName();
594e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain  RecordMemberExprValidatorCCC Validator;
595d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor  TypoCorrection Corrected = SemaRef.CorrectTypo(R.getLookupNameInfo(),
596d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor                                                 R.getLookupKind(), NULL,
59716e46dd0c284296cea819dfbf67942ecef02894dKaelyn Uhrain                                                 &SS, Validator, DC);
598d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor  R.clear();
599e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain  if (NamedDecl *ND = Corrected.getCorrectionDecl()) {
600d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    std::string CorrectedStr(
6014e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        Corrected.getAsString(SemaRef.getLangOpts()));
602d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    std::string CorrectedQuotedStr(
6034e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        Corrected.getQuoted(SemaRef.getLangOpts()));
604d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    R.setLookupName(Corrected.getCorrection());
605d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    R.addDecl(ND);
6062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest)
607d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      << Name << DC << CorrectedQuotedStr << SS.getRange()
608d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr);
609d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    SemaRef.Diag(ND->getLocation(), diag::note_previous_decl)
610d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      << ND->getDeclName();
6112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
6122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return false;
6142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
6152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
6172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType,
6182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               SourceLocation OpLoc, bool IsArrow,
6192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               CXXScopeSpec &SS,
620e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                               SourceLocation TemplateKWLoc,
6212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               NamedDecl *FirstQualifierInScope,
6222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const DeclarationNameInfo &NameInfo,
6232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const TemplateArgumentListInfo *TemplateArgs) {
6242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (BaseType->isDependentType() ||
6252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      (SS.isSet() && isDependentScopeSpecifier(SS)))
6262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ActOnDependentMemberExpr(Base, BaseType,
6272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                    IsArrow, OpLoc,
628e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    SS, TemplateKWLoc, FirstQualifierInScope,
6292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                    NameInfo, TemplateArgs);
6302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  LookupResult R(*this, NameInfo, LookupMemberName);
6322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Implicit member accesses.
6342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!Base) {
6352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType RecordTy = BaseType;
6362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType();
6372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (LookupMemberExprInRecord(*this, R, SourceRange(),
6382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 RecordTy->getAs<RecordType>(),
6392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 OpLoc, SS, TemplateArgs != 0))
6402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
6412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Explicit member accesses.
6432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
6442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprResult BaseResult = Owned(Base);
6452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprResult Result =
6462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
6472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0);
6482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseResult.isInvalid())
6502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
6512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Base = BaseResult.take();
6522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.isInvalid()) {
6542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Owned(Base);
6552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
6562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
6572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.get())
6592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return move(Result);
6602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // LookupMemberExpr can modify Base, and thus change BaseType
6622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseType = Base->getType();
6632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
6642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return BuildMemberReferenceExpr(Base, BaseType,
666e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                  OpLoc, IsArrow, SS, TemplateKWLoc,
667e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                  FirstQualifierInScope, R, TemplateArgs);
6682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
6692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic ExprResult
6712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorBuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
6722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const CXXScopeSpec &SS, FieldDecl *Field,
6732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        DeclAccessPair FoundDecl,
6742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const DeclarationNameInfo &MemberNameInfo);
6752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
6772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS,
6782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               SourceLocation loc,
6792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               IndirectFieldDecl *indirectField,
6802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               Expr *baseObjectExpr,
6812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               SourceLocation opLoc) {
6822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // First, build the expression that refers to the base object.
6832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool baseObjectIsPointer = false;
6852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Qualifiers baseQuals;
6862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Case 1:  the base of the indirect field is not a field.
6882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  VarDecl *baseVariable = indirectField->getVarDecl();
6892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  CXXScopeSpec EmptySS;
6902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (baseVariable) {
6912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(baseVariable->getType()->isRecordType());
6922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // In principle we could have a member access expression that
6942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // accesses an anonymous struct/union that's a static member of
6952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // the base object's class.  However, under the current standard,
6962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // static data members cannot be anonymous structs or unions.
6972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Supporting this is as easy as building a MemberExpr here.
6982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(!baseObjectExpr && "anonymous struct/union is static data member?");
6992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclarationNameInfo baseNameInfo(DeclarationName(), loc);
7012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprResult result
7032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable);
7042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (result.isInvalid()) return ExprError();
7052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectExpr = result.take();
7072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectIsPointer = false;
7082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseQuals = baseObjectExpr->getType().getQualifiers();
7092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Case 2: the base of the indirect field is a field and the user
7112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // wrote a member expression.
7122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else if (baseObjectExpr) {
7132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // The caller provided the base object expression. Determine
7142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // whether its a pointer and whether it adds any qualifiers to the
7152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // anonymous struct/union fields we're looking into.
7162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType objectType = baseObjectExpr->getType();
7172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (const PointerType *ptr = objectType->getAs<PointerType>()) {
7192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      baseObjectIsPointer = true;
7202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      objectType = ptr->getPointeeType();
7212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } else {
7222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      baseObjectIsPointer = false;
7232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
7242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseQuals = objectType.getQualifiers();
7252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Case 3: the base of the indirect field is a field and we should
7272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // build an implicit member access.
7282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
7292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // We've found a member of an anonymous struct/union that is
7302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // inside a non-anonymous struct/union, so in a well-formed
7312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // program our base object expression is "this".
732341350ee62abd1ad818e1e3d926cd718960e439bDouglas Gregor    QualType ThisTy = getCurrentThisType();
7332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (ThisTy.isNull()) {
7342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(loc, diag::err_invalid_member_use_in_static_method)
7352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << indirectField->getDeclName();
7362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
7372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
7382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Our base object expression is "this".
74072899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    CheckCXXThisCapture(loc);
7412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectExpr
7422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true);
7432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectIsPointer = true;
7442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers();
7452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
7462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Build the implicit member references to the field of the
7482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // anonymous struct/union.
7492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Expr *result = baseObjectExpr;
7502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IndirectFieldDecl::chain_iterator
7512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  FI = indirectField->chain_begin(), FEnd = indirectField->chain_end();
7522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Build the first member access in the chain with full information.
7542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!baseVariable) {
7552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    FieldDecl *field = cast<FieldDecl>(*FI);
7562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // FIXME: use the real found-decl info!
7582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess());
7592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Make a nameInfo that properly uses the anonymous name.
7612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
7622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer,
7642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     EmptySS, field, foundDecl,
7652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     memberNameInfo).take();
7662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectIsPointer = false;
7672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // FIXME: check qualified member access
7692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
7702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // In all cases, we should now skip the first declaration in the chain.
7722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ++FI;
7732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  while (FI != FEnd) {
7752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    FieldDecl *field = cast<FieldDecl>(*FI++);
7762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // FIXME: these are somewhat meaningless
7782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
7792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess());
7802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false,
7822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     (FI == FEnd? SS : EmptySS), field,
7832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     foundDecl, memberNameInfo).take();
7842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
7852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return Owned(result);
7872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
7882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \brief Build a MemberExpr AST node.
7905f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedmanstatic MemberExpr *BuildMemberExpr(Sema &SemaRef,
7915f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                   ASTContext &C, Expr *Base, bool isArrow,
792e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   const CXXScopeSpec &SS,
793e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   SourceLocation TemplateKWLoc,
794e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   ValueDecl *Member,
7952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   DeclAccessPair FoundDecl,
7962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   const DeclarationNameInfo &MemberNameInfo,
7972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   QualType Ty,
7982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   ExprValueKind VK, ExprObjectKind OK,
7992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   const TemplateArgumentListInfo *TemplateArgs = 0) {
8004f87062cb411d5a31cf39f1ac576bba4123930f2Richard Smith  assert((!isArrow || Base->isRValue()) && "-> base must be a pointer rvalue");
8015f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  MemberExpr *E =
8025f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman      MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C),
8035f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                         TemplateKWLoc, Member, FoundDecl, MemberNameInfo,
8045f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                         TemplateArgs, Ty, VK, OK);
8055f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  SemaRef.MarkMemberReferenced(E);
8065f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  return E;
8072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
8082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
8102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
8112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               SourceLocation OpLoc, bool IsArrow,
8122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const CXXScopeSpec &SS,
813e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                               SourceLocation TemplateKWLoc,
8142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               NamedDecl *FirstQualifierInScope,
8152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               LookupResult &R,
8162b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                               const TemplateArgumentListInfo *TemplateArgs,
8172b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                               bool SuppressQualifierCheck,
8182b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                               ActOnMemberAccessExtraArgs *ExtraArgs) {
8192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType BaseType = BaseExprType;
8202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsArrow) {
8212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(BaseType->isPointerType());
8223c3b7f90a863af43fa63043d396553ecf205351cJohn McCall    BaseType = BaseType->castAs<PointerType>()->getPointeeType();
8232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
8242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  R.setBaseObjectType(BaseType);
8252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo();
8272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName MemberName = MemberNameInfo.getName();
8282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation MemberLoc = MemberNameInfo.getLoc();
8292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.isAmbiguous())
8312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
8322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.empty()) {
8342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Rederive where we looked up.
8352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclContext *DC = (SS.isSet()
8362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       ? computeDeclContext(SS, false)
8372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       : BaseType->getAs<RecordType>()->getDecl());
8382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8392b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain    if (ExtraArgs) {
8402b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      ExprResult RetryExpr;
8412b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      if (!IsArrow && BaseExpr) {
842111263cf8d829f5a3d0a3c9164f65c1c8223ac7eKaelyn Uhrain        SFINAETrap Trap(*this, true);
8432b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        ParsedType ObjectType;
8442b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        bool MayBePseudoDestructor = false;
8452b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        RetryExpr = ActOnStartCXXMemberReference(getCurScope(), BaseExpr,
8462b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                                                 OpLoc, tok::arrow, ObjectType,
8472b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                                                 MayBePseudoDestructor);
8482b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        if (RetryExpr.isUsable() && !Trap.hasErrorOccurred()) {
8492b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain          CXXScopeSpec TempSS(SS);
8502b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain          RetryExpr = ActOnMemberAccessExpr(
8512b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain              ExtraArgs->S, RetryExpr.get(), OpLoc, tok::arrow, TempSS,
8522b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain              TemplateKWLoc, ExtraArgs->Id, ExtraArgs->ObjCImpDecl,
8532b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain              ExtraArgs->HasTrailingLParen);
8542b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        }
8552b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        if (Trap.hasErrorOccurred())
8562b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain          RetryExpr = ExprError();
8572b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      }
8582b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      if (RetryExpr.isUsable()) {
8592b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        Diag(OpLoc, diag::err_no_member_overloaded_arrow)
8602b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain          << MemberName << DC << FixItHint::CreateReplacement(OpLoc, "->");
8612b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        return RetryExpr;
8622b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      }
8632b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain    }
8642b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain
8652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(R.getNameLoc(), diag::err_no_member)
8662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << MemberName << DC
8672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange());
8682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
8692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
8702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Diagnose lookups that find only declarations from a non-base
8722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // type.  This is possible for either qualified lookups (which may
8732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // have been qualified with an unrelated type) or implicit member
8742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // expressions (which were found with unqualified lookup and thus
8752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // may have come from an enclosing scope).  Note that it's okay for
8762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // lookup to find declarations from a non-base type as long as those
8772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // aren't the ones picked by overload resolution.
8782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if ((SS.isSet() || !BaseExpr ||
8792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor       (isa<CXXThisExpr>(BaseExpr) &&
8802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        cast<CXXThisExpr>(BaseExpr)->isImplicit())) &&
8812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      !SuppressQualifierCheck &&
8822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R))
8832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
884d12505054130b24f7696440efdbd1aa660feb6f3Fariborz Jahanian
8852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Construct an unresolved result if we in fact got an unresolved
8862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // result.
8872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.isOverloadedResult() || R.isUnresolvableResult()) {
8882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Suppress any lookup-related diagnostics; we'll do these when we
8892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // pick a member.
8902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    R.suppressDiagnostics();
8912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    UnresolvedMemberExpr *MemExpr
8932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(),
8942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     BaseExpr, BaseExprType,
8952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     IsArrow, OpLoc,
8962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     SS.getWithLocInContext(Context),
897e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                     TemplateKWLoc, MemberNameInfo,
8982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     TemplateArgs, R.begin(), R.end());
8992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned(MemExpr);
9012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(R.isSingleResult());
9042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclAccessPair FoundDecl = R.begin().getPair();
9052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  NamedDecl *MemberDecl = R.getFoundDecl();
9062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: diagnose the presence of template arguments now.
9082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If the decl being referenced had an error, return an error for this
9102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // sub-expr without emitting another error, in order to avoid cascading
9112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // error cases.
9122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (MemberDecl->isInvalidDecl())
9132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
9142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle the implicit-member-access case.
9162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!BaseExpr) {
9172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If this is not an instance member, convert to a non-member access.
9182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!MemberDecl->isCXXInstanceMember())
9192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl);
9202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SourceLocation Loc = R.getNameLoc();
9222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (SS.getRange().isValid())
9232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Loc = SS.getRange().getBegin();
92472899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    CheckCXXThisCapture(Loc);
9252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true);
9262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool ShouldCheckUse = true;
9292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) {
9302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Don't diagnose the use of a virtual member function unless it's
9312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // explicitly qualified.
9322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (MD->isVirtual() && !SS.isSet())
9332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ShouldCheckUse = false;
9342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Check the use of this member.
9372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) {
9382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Owned(BaseExpr);
9392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
9402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl))
9432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow,
9442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   SS, FD, FoundDecl, MemberNameInfo);
9452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl))
9472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // We may have found a field within an anonymous union or struct
9482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // (C++ [class.union]).
9492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD,
9502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                    BaseExpr, OpLoc);
9512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) {
9535f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
9545f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 TemplateKWLoc, Var, FoundDecl, MemberNameInfo,
9552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 Var->getType().getNonReferenceType(),
9562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 VK_LValue, OK_Ordinary));
9572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) {
9602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprValueKind valueKind;
9612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType type;
9622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (MemberFn->isInstance()) {
9632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      valueKind = VK_RValue;
9642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      type = Context.BoundMemberTy;
9652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } else {
9662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      valueKind = VK_LValue;
9672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      type = MemberFn->getType();
9682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
9692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9705f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
9715f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 TemplateKWLoc, MemberFn, FoundDecl,
9725f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 MemberNameInfo, type, valueKind,
9735f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 OK_Ordinary));
9742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?");
9762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) {
9785f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
9795f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 TemplateKWLoc, Enum, FoundDecl, MemberNameInfo,
9802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 Enum->getType(), VK_RValue, OK_Ordinary));
9812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Owned(BaseExpr);
9842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // We found something that we didn't expect. Complain.
9862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (isa<TypeDecl>(MemberDecl))
9872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(MemberLoc, diag::err_typecheck_member_reference_type)
9882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << MemberName << BaseType << int(IsArrow);
9892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  else
9902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(MemberLoc, diag::err_typecheck_member_reference_unknown)
9912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << MemberName << BaseType << int(IsArrow);
9922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Diag(MemberDecl->getLocation(), diag::note_member_declared_here)
9942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    << MemberName;
9952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  R.suppressDiagnostics();
9962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return ExprError();
9972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
9982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Given that normal member access failed on the given expression,
10002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// and given that the expression's type involves builtin-id or
10012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// builtin-Class, decide whether substituting in the redefinition
10022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// types would be profitable.  The redefinition type is whatever
10032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// this translation unit tried to typedef to id/Class;  we store
10042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// it to the side and then re-use it in places like this.
10052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
10062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ObjCObjectPointerType *opty
10072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    = base.get()->getType()->getAs<ObjCObjectPointerType>();
10082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!opty) return false;
10092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ObjCObjectType *ty = opty->getObjectType();
10112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType redef;
10132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (ty->isObjCId()) {
101401a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor    redef = S.Context.getObjCIdRedefinitionType();
10152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else if (ty->isObjCClass()) {
101601a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor    redef = S.Context.getObjCClassRedefinitionType();
10172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
10182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
10192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
10202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Do the substitution as long as the redefinition type isn't just a
10222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // possibly-qualified pointer to builtin-id or builtin-Class again.
10232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  opty = redef->getAs<ObjCObjectPointerType>();
10242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (opty && !opty->getObjectType()->getInterface() != 0)
10252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
10262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  base = S.ImpCastExprToType(base.take(), redef, CK_BitCast);
10282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return true;
10292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
10302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10316dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool isRecordType(QualType T) {
10326dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return T->isRecordType();
10336dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
10346dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool isPointerToRecordType(QualType T) {
10356dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (const PointerType *PT = T->getAs<PointerType>())
10366dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return PT->getPointeeType()->isRecordType();
10376dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return false;
10386dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
10396dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
10409138b4e96429cbaae00c52c15c960f72b6645088Richard Smith/// Perform conversions on the LHS of a member access expression.
10419138b4e96429cbaae00c52c15c960f72b6645088Richard SmithExprResult
10429138b4e96429cbaae00c52c15c960f72b6645088Richard SmithSema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) {
1043059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman  if (IsArrow && !Base->getType()->isFunctionType())
1044059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman    return DefaultFunctionArrayLvalueConversion(Base);
10459138b4e96429cbaae00c52c15c960f72b6645088Richard Smith
1046059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman  return CheckPlaceholderExpr(Base);
10479138b4e96429cbaae00c52c15c960f72b6645088Richard Smith}
10489138b4e96429cbaae00c52c15c960f72b6645088Richard Smith
10492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Look up the given member of the given non-type-dependent
10502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression.  This can return in one of two ways:
10512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///  * If it returns a sentinel null-but-valid result, the caller will
10522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///    assume that lookup was performed and the results written into
10532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///    the provided structure.  It will take over from there.
10542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///  * Otherwise, the returned expression will be produced in place of
10552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///    an ordinary member expression.
10562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///
10572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The ObjCImpDecl bit is a gross hack that will need to be properly
10582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// fixed for ObjC++.
10592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
10602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
10612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       bool &IsArrow, SourceLocation OpLoc,
10622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       CXXScopeSpec &SS,
10632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       Decl *ObjCImpDecl, bool HasTemplateArgs) {
10642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(BaseExpr.get() && "no base expression");
10652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Perform default conversions.
10679138b4e96429cbaae00c52c15c960f72b6645088Richard Smith  BaseExpr = PerformMemberExprBaseConversion(BaseExpr.take(), IsArrow);
10686dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (BaseExpr.isInvalid())
10696dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return ExprError();
10702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType BaseType = BaseExpr.get()->getType();
10722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!BaseType->isDependentType());
10732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName MemberName = R.getLookupName();
10752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation MemberLoc = R.getNameLoc();
10762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // For later type-checking purposes, turn arrow accesses into dot
10782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // accesses.  The only access type we support that doesn't follow
10792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // the C equivalence "a->b === (*a).b" is ObjC property accesses,
10802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // and those never use arrows, so this is unaffected.
10812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsArrow) {
10822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (const PointerType *Ptr = BaseType->getAs<PointerType>())
10832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      BaseType = Ptr->getPointeeType();
10842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else if (const ObjCObjectPointerType *Ptr
10852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor               = BaseType->getAs<ObjCObjectPointerType>())
10862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      BaseType = Ptr->getPointeeType();
10872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else if (BaseType->isRecordType()) {
10882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Recover from arrow accesses to records, e.g.:
10892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      //   struct MyRecord foo;
10902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      //   foo->bar
10912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // This is actually well-formed in C++ if MyRecord has an
10922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // overloaded operator->, but that should have been dealt with
10932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // by now.
10942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
10952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
10962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << FixItHint::CreateReplacement(OpLoc, ".");
10972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      IsArrow = false;
1098059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman    } else if (BaseType->isFunctionType()) {
10992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      goto fail;
11002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } else {
11012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
11022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << BaseExpr.get()->getSourceRange();
11032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
11042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
11052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
11062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle field access to simple records.
11082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const RecordType *RTy = BaseType->getAs<RecordType>()) {
11092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(),
11102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 RTy, OpLoc, SS, HasTemplateArgs))
11112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
11122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Returning valid-but-null is how we indicate to the caller that
11142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // the lookup result was filled in.
11152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned((Expr*) 0);
11162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
11172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle ivar access to Objective-C objects.
11192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) {
11205a706dc1b17f875c7fce20f1fbf9ca372be4c331Douglas Gregor    if (!SS.isEmpty() && !SS.isInvalid()) {
1121b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1122b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << 1 << SS.getScopeRep()
1123b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << FixItHint::CreateRemoval(SS.getRange());
1124b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      SS.clear();
1125b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor    }
1126b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor
11272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
11282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // There are three cases for the base type:
11302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    //   - builtin id (qualified or unqualified)
11312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    //   - builtin Class (qualified or unqualified)
11322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    //   - an interface
11332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ObjCInterfaceDecl *IDecl = OTy->getInterface();
11342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IDecl) {
11354e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().ObjCAutoRefCount &&
11362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          (OTy->isObjCId() || OTy->isObjCClass()))
11372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        goto fail;
11382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // There's an implicit 'isa' ivar on all objects.
11392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // But we only actually find it this way on objects of type 'id',
1140556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian      // apparently.ghjg
1141556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian      if (OTy->isObjCId() && Member->isStr("isa")) {
1142556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian        Diag(MemberLoc, diag::warn_objc_isa_use);
11432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc,
11442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               Context.getObjCClassType()));
1145556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian      }
11462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
11482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
11492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                ObjCImpDecl, HasTemplateArgs);
11502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      goto fail;
11512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
1152091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian    else if (Member && Member->isStr("isa")) {
1153091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      // If an ivar is (1) the first ivar in a root class and (2) named `isa`,
1154091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      // then issue the same deprecated warning that id->isa gets.
1155091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      ObjCInterfaceDecl *ClassDeclared = 0;
1156091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      if (ObjCIvarDecl *IV =
1157091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian            IDecl->lookupInstanceVariable(Member, ClassDeclared)) {
1158091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian        if (!ClassDeclared->getSuperClass()
1159091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian            && (*ClassDeclared->ivar_begin()) == IV) {
1160091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian          Diag(MemberLoc, diag::warn_objc_isa_use);
1161091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian          Diag(IV->getLocation(), diag::note_ivar_decl);
1162091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian        }
1163091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      }
1164091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian    }
1165091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian
1166d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(OpLoc, BaseType, diag::err_typecheck_incomplete_tag,
1167d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                            BaseExpr.get()))
1168d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      return ExprError();
1169d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
11702c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek    ObjCInterfaceDecl *ClassDeclared = 0;
11712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared);
11722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IV) {
11742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Attempt to correct for typos in ivar names.
1175e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain      DeclFilterCCC<ObjCIvarDecl> Validator;
1176e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain      Validator.IsObjCIvarLookup = IsArrow;
1177e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain      if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
1178e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain                                                 LookupMemberName, NULL, NULL,
117916e46dd0c284296cea819dfbf67942ecef02894dKaelyn Uhrain                                                 Validator, IDecl)) {
1180e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain        IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>();
11812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Diag(R.getNameLoc(),
11822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor             diag::err_typecheck_member_reference_ivar_suggest)
11832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << IDecl->getDeclName() << MemberName << IV->getDeclName()
11842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << FixItHint::CreateReplacement(R.getNameLoc(),
11852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                          IV->getNameAsString());
11862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Diag(IV->getLocation(), diag::note_previous_decl)
11872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << IV->getDeclName();
11882c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek
11892c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        // Figure out the class that declares the ivar.
11902c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        assert(!ClassDeclared);
11912c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        Decl *D = cast<Decl>(IV->getDeclContext());
11922c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        if (ObjCCategoryDecl *CAT = dyn_cast<ObjCCategoryDecl>(D))
11932c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek          D = CAT->getClassInterface();
11942c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        ClassDeclared = cast<ObjCInterfaceDecl>(D);
11952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      } else {
11966326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian        if (IsArrow && IDecl->FindPropertyDeclaration(Member)) {
11976326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          Diag(MemberLoc,
11986326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          diag::err_property_found_suggest)
11996326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          << Member << BaseExpr.get()->getType()
12006326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          << FixItHint::CreateReplacement(OpLoc, ".");
12016326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          return ExprError();
12026326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian        }
12032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Diag(MemberLoc, diag::err_typecheck_member_reference_ivar)
12052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << IDecl->getDeclName() << MemberName
12062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << BaseExpr.get()->getSourceRange();
12072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return ExprError();
12082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
12092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
12102c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek
12112c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek    assert(ClassDeclared);
12122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If the decl being referenced had an error, return an error for this
12142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // sub-expr without emitting another error, in order to avoid cascading
12152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // error cases.
12162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IV->isInvalidDecl())
12172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
12182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Check whether we can reference this field.
12202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (DiagnoseUseOfDecl(IV, MemberLoc))
12212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
12222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IV->getAccessControl() != ObjCIvarDecl::Public &&
12232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        IV->getAccessControl() != ObjCIvarDecl::Package) {
12242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCInterfaceDecl *ClassOfMethodDecl = 0;
12252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ObjCMethodDecl *MD = getCurMethodDecl())
12262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        ClassOfMethodDecl =  MD->getClassInterface();
12272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      else if (ObjCImpDecl && getCurFunctionDecl()) {
12282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // Case of a c-function declared inside an objc implementation.
12292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // FIXME: For a c-style function nested inside an objc implementation
12302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // class, there is no implementation context available, so we pass
12312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // down the context as argument to this routine. Ideally, this context
12322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // need be passed down in the AST node and somehow calculated from the
12332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // AST for a function decl.
12342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ObjCImplementationDecl *IMPD =
12352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor              dyn_cast<ObjCImplementationDecl>(ObjCImpDecl))
12362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          ClassOfMethodDecl = IMPD->getClassInterface();
12372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        else if (ObjCCategoryImplDecl* CatImplClass =
12382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                   dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl))
12392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          ClassOfMethodDecl = CatImplClass->getClassInterface();
12402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
12414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!getLangOpts().DebuggerSupport) {
1242458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian        if (IV->getAccessControl() == ObjCIvarDecl::Private) {
1243458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian          if (!declaresSameEntity(ClassDeclared, IDecl) ||
1244458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian              !declaresSameEntity(ClassOfMethodDecl, ClassDeclared))
1245458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian            Diag(MemberLoc, diag::error_private_ivar_access)
1246458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian              << IV->getDeclName();
1247458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian        } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl))
1248458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian          // @protected
1249458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian          Diag(MemberLoc, diag::error_protected_ivar_access)
12502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            << IV->getDeclName();
1251458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian      }
12522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
12534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount) {
12542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts();
12552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp))
12562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (UO->getOpcode() == UO_Deref)
12572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          BaseExp = UO->getSubExpr()->IgnoreParenCasts();
12582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp))
12602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
12612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          Diag(DE->getLocation(), diag::error_arc_weak_ivar_access);
12622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
12632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(),
12652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               MemberLoc, BaseExpr.take(),
12662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               IsArrow));
12672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
12682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Objective-C property access.
12702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ObjCObjectPointerType *OPT;
12712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) {
12725a706dc1b17f875c7fce20f1fbf9ca372be4c331Douglas Gregor    if (!SS.isEmpty() && !SS.isInvalid()) {
1273b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1274b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << 0 << SS.getScopeRep()
1275b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << FixItHint::CreateRemoval(SS.getRange());
1276b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      SS.clear();
1277b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor    }
1278b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor
12792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // This actually uses the base as an r-value.
12802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseExpr = DefaultLvalueConversion(BaseExpr.take());
12812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseExpr.isInvalid())
12822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
12832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType()));
12852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
12872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    const ObjCObjectType *OT = OPT->getObjectType();
12892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // id, with and without qualifiers.
12912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (OT->isObjCId()) {
12922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Check protocols on qualified interfaces.
12932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
12942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) {
12952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) {
12962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          // Check the use of this declaration
12972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          if (DiagnoseUseOfDecl(PD, MemberLoc))
12982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            return ExprError();
12992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13003c3b7f90a863af43fa63043d396553ecf205351cJohn McCall          return Owned(new (Context) ObjCPropertyRefExpr(PD,
13013c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                         Context.PseudoObjectTy,
13022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         VK_LValue,
13032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         OK_ObjCProperty,
13042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         MemberLoc,
13052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         BaseExpr.take()));
13062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        }
13072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) {
13092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          // Check the use of this method.
13102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          if (DiagnoseUseOfDecl(OMD, MemberLoc))
13112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            return ExprError();
13122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          Selector SetterSel =
13132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            SelectorTable::constructSetterName(PP.getIdentifierTable(),
13142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               PP.getSelectorTable(), Member);
13152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          ObjCMethodDecl *SMD = 0;
13162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0,
13172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                     SetterSel, Context))
13182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            SMD = dyn_cast<ObjCMethodDecl>(SDecl);
13192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13203c3b7f90a863af43fa63043d396553ecf205351cJohn McCall          return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD,
13213c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                         Context.PseudoObjectTy,
13223c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                         VK_LValue, OK_ObjCProperty,
13232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         MemberLoc, BaseExpr.take()));
13242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        }
13252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Use of id.member can only be for a property reference. Do not
13272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // use the 'id' redefinition in this case.
13282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
13292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
13302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                ObjCImpDecl, HasTemplateArgs);
13312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError(Diag(MemberLoc, diag::err_property_not_found)
13332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         << MemberName << BaseType);
13342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
13352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // 'Class', unqualified only.
13372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (OT->isObjCClass()) {
13382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Only works in a method declaration (??!).
13392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCMethodDecl *MD = getCurMethodDecl();
13402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!MD) {
13412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
13422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
13432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  ObjCImpDecl, HasTemplateArgs);
13442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        goto fail;
13462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Also must look for a getter name which uses property syntax.
13492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
13502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCInterfaceDecl *IFace = MD->getClassInterface();
13512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCMethodDecl *Getter;
13522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if ((Getter = IFace->lookupClassMethod(Sel))) {
13532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // Check the use of this method.
13542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (DiagnoseUseOfDecl(Getter, MemberLoc))
13552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          return ExprError();
13562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      } else
13572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Getter = IFace->lookupPrivateMethod(Sel, false);
13582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // If we found a getter then this may be a valid dot-reference, we
13592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // will look for the matching setter, in case it is needed.
13602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Selector SetterSel =
13612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        SelectorTable::constructSetterName(PP.getIdentifierTable(),
13622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                           PP.getSelectorTable(), Member);
13632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
13642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!Setter) {
13652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // If this reference is in an @implementation, also check for 'private'
13662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // methods.
13672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Setter = IFace->lookupPrivateMethod(SetterSel, false);
13682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Look through local category implementations associated with the class.
13702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!Setter)
13712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Setter = IFace->getCategoryClassMethod(SetterSel);
13722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
13742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return ExprError();
13752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (Getter || Setter) {
13772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
13783c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                       Context.PseudoObjectTy,
13793c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                       VK_LValue, OK_ObjCProperty,
13802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                       MemberLoc, BaseExpr.take()));
13812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
13842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
13852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                ObjCImpDecl, HasTemplateArgs);
13862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError(Diag(MemberLoc, diag::err_property_not_found)
13882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         << MemberName << BaseType);
13892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
13902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Normal property access.
13926326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian    return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc,
13936326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian                                     MemberName, MemberLoc,
13942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     SourceLocation(), QualType(), false);
13952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
13962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle 'field access' to vectors, such as 'V.xx'.
13982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (BaseType->isExtVectorType()) {
13992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // FIXME: this expr should store IsArrow.
14002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
14012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind());
14022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc,
14032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                           Member, MemberLoc);
14042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (ret.isNull())
14052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
14062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(),
14082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                    *Member, MemberLoc));
14092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Adjust builtin-sel to the appropriate redefinition type if that's
14122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // not just a pointer to builtin-sel again.
14132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsArrow &&
14142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) &&
141501a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor      !Context.getObjCSelRedefinitionType()->isObjCSelType()) {
141601a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor    BaseExpr = ImpCastExprToType(BaseExpr.take(),
141701a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor                                 Context.getObjCSelRedefinitionType(),
14182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 CK_BitCast);
14192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
14202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                            ObjCImpDecl, HasTemplateArgs);
14212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Failure cases.
14242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor fail:
14252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Recover from dot accesses to pointers, e.g.:
14272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   type *foo;
14282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   foo.bar
14292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This is actually well-formed in two cases:
14302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   - 'type' is an Objective C type
14312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   - 'bar' is a pseudo-destructor name which happens to refer to
14322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //     the appropriate pointer type
14332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
14342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IsArrow && Ptr->getPointeeType()->isRecordType() &&
14352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        MemberName.getNameKind() != DeclarationName::CXXDestructorName) {
14362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
14372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
14382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << FixItHint::CreateReplacement(OpLoc, "->");
14392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Recurse as an -> access.
14412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      IsArrow = true;
14422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
14432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              ObjCImpDecl, HasTemplateArgs);
14442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
14452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If the user is trying to apply -> or . to a function name, it's probably
14482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // because they forgot parentheses to call that function.
14496dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (tryToRecoverWithCall(BaseExpr,
14506dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                           PDiag(diag::err_member_reference_needs_call),
14516dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                           /*complain*/ false,
1452059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman                           IsArrow ? &isPointerToRecordType : &isRecordType)) {
14536dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    if (BaseExpr.isInvalid())
14542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
14556dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take());
14566dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
14576dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                            ObjCImpDecl, HasTemplateArgs);
14582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14607d90fe5a941efc106237d23badec816ed65e267fMatt Beaumont-Gay  Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
146173664a4e5aee9216c37bd123aa002430ccb5431dMatt Beaumont-Gay    << BaseType << BaseExpr.get()->getSourceRange() << MemberLoc;
14622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return ExprError();
14642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
14652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The main callback when the parser finds something like
14672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   expression . [nested-name-specifier] identifier
14682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   expression -> [nested-name-specifier] identifier
14692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// where 'identifier' encompasses a fairly broad spectrum of
14702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// possibilities, including destructor and operator references.
14712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///
14722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param OpKind either tok::arrow or tok::period
14732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param HasTrailingLParen whether the next token is '(', which
14742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   is used to diagnose mis-uses of special members that can
14752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   only be called
1476699c9044c7d53a2774d0dd261a6901dd2c4a545fJames Dennett/// \param ObjCImpDecl the current Objective-C \@implementation
1477699c9044c7d53a2774d0dd261a6901dd2c4a545fJames Dennett///   decl; this is an ugly hack around the fact that Objective-C
1478699c9044c7d53a2774d0dd261a6901dd2c4a545fJames Dennett///   \@implementations aren't properly put in the context chain
14792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base,
14802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       SourceLocation OpLoc,
14812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       tok::TokenKind OpKind,
14822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       CXXScopeSpec &SS,
1483e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                       SourceLocation TemplateKWLoc,
14842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       UnqualifiedId &Id,
14852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       Decl *ObjCImpDecl,
14862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       bool HasTrailingLParen) {
14872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (SS.isSet() && SS.isInvalid())
14882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
14892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Warn about the explicit constructor calls Microsoft extension.
14914e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().MicrosoftExt &&
14922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Id.getKind() == UnqualifiedId::IK_ConstructorName)
14932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(Id.getSourceRange().getBegin(),
14942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         diag::ext_ms_explicit_constructor_call);
14952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  TemplateArgumentListInfo TemplateArgsBuffer;
14972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Decompose the name into its component parts.
14992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationNameInfo NameInfo;
15002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const TemplateArgumentListInfo *TemplateArgs;
15012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DecomposeUnqualifiedId(Id, TemplateArgsBuffer,
15022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         NameInfo, TemplateArgs);
15032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName Name = NameInfo.getName();
15052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool IsArrow = (OpKind == tok::arrow);
15062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  NamedDecl *FirstQualifierInScope
15082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S,
15092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       static_cast<NestedNameSpecifier*>(SS.getScopeRep())));
15102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This is a postfix expression, so get rid of ParenListExprs.
15122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
15132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Result.isInvalid()) return ExprError();
15142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Base = Result.take();
15152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Base->getType()->isDependentType() || Name.isDependentName() ||
15172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      isDependentScopeSpecifier(SS)) {
15182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Result = ActOnDependentMemberExpr(Base, Base->getType(),
15192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      IsArrow, OpLoc,
1520e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      SS, TemplateKWLoc, FirstQualifierInScope,
15212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      NameInfo, TemplateArgs);
15222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
15232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    LookupResult R(*this, NameInfo, LookupMemberName);
15242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprResult BaseResult = Owned(Base);
15252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
15262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              SS, ObjCImpDecl, TemplateArgs != 0);
15272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseResult.isInvalid())
15282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
15292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Base = BaseResult.take();
15302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.isInvalid()) {
15322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Owned(Base);
15332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
15342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
15352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.get()) {
15372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // The only way a reference to a destructor can be used is to
15382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // immediately call it, which falls into this case.  If the
15392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // next token is not a '(', produce a diagnostic and build the
15402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // call now.
15412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!HasTrailingLParen &&
15422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          Id.getKind() == UnqualifiedId::IK_DestructorName)
15432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return DiagnoseDtorReference(NameInfo.getLoc(), Result.get());
15442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return move(Result);
15462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
15472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15482b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain    ActOnMemberAccessExtraArgs ExtraArgs = {S, Id, ObjCImpDecl, HasTrailingLParen};
15492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Result = BuildMemberReferenceExpr(Base, Base->getType(),
1550e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      OpLoc, IsArrow, SS, TemplateKWLoc,
15512b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                                      FirstQualifierInScope, R, TemplateArgs,
15522b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                                      false, &ExtraArgs);
15532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
15542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return move(Result);
15562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
15572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic ExprResult
15592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorBuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
15602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const CXXScopeSpec &SS, FieldDecl *Field,
15612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        DeclAccessPair FoundDecl,
15622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const DeclarationNameInfo &MemberNameInfo) {
15632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // x.a is an l-value if 'a' has a reference type. Otherwise:
15642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // x.a is an l-value/x-value/pr-value if the base is (and note
15652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   that *x is always an l-value), except that if the base isn't
15662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   an ordinary object then we must have an rvalue.
15672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprValueKind VK = VK_LValue;
15682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprObjectKind OK = OK_Ordinary;
15692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!IsArrow) {
15702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseExpr->getObjectKind() == OK_Ordinary)
15712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      VK = BaseExpr->getValueKind();
15722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else
15732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      VK = VK_RValue;
15742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
15752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (VK != VK_RValue && Field->isBitField())
15762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    OK = OK_BitField;
15772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref]
15792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType MemberType = Field->getType();
15802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) {
15812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    MemberType = Ref->getPointeeType();
15822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    VK = VK_LValue;
15832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
15842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType BaseType = BaseExpr->getType();
15852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType();
15862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Qualifiers BaseQuals = BaseType.getQualifiers();
15882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // GC attributes are never picked up by members.
15902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseQuals.removeObjCGCAttr();
15912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // CVR attributes from the base are picked up by members,
15932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // except that 'mutable' members don't pick up 'const'.
15942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Field->isMutable()) BaseQuals.removeConst();
15952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Qualifiers MemberQuals
15972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    = S.Context.getCanonicalType(MemberType).getQualifiers();
15982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // TR 18037 does not allow fields to be declared with address spaces.
16002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(!MemberQuals.hasAddressSpace());
16012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Qualifiers Combined = BaseQuals + MemberQuals;
16032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Combined != MemberQuals)
16042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      MemberType = S.Context.getQualifiedType(MemberType, Combined);
16052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
16062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1607f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  S.UnusedPrivateFields.remove(Field);
1608f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
16092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprResult Base =
16102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
16112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  FoundDecl, Field);
16122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Base.isInvalid())
16132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
16145f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  return S.Owned(BuildMemberExpr(S, S.Context, Base.take(), IsArrow, SS,
1615e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                 /*TemplateKWLoc=*/SourceLocation(),
16162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 Field, FoundDecl, MemberNameInfo,
16172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 MemberType, VK, OK));
16182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
16192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Builds an implicit member access expression.  The current context
16212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// is known to be an instance method, and the given unqualified lookup
16222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// set is known to contain only instance members, at least one of which
16232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// is from an appropriate type.
16242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
16252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
1626e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                              SourceLocation TemplateKWLoc,
16272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              LookupResult &R,
16282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              const TemplateArgumentListInfo *TemplateArgs,
16292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              bool IsKnownInstance) {
16302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!R.empty() && !R.isAmbiguous());
16312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation loc = R.getNameLoc();
16332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // We may have found a field within an anonymous union or struct
16352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // (C++ [class.union]).
16362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: template-ids inside anonymous structs?
16372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>())
16382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD);
16392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If this is known to be an instance access, go ahead and build an
16412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // implicit 'this' expression now.
16422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // 'this' expression now.
1643341350ee62abd1ad818e1e3d926cd718960e439bDouglas Gregor  QualType ThisTy = getCurrentThisType();
16442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'");
16452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Expr *baseExpr = 0; // null signifies implicit access
16472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsKnownInstance) {
16482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SourceLocation Loc = R.getNameLoc();
16492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (SS.getRange().isValid())
16502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Loc = SS.getRange().getBegin();
165172899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    CheckCXXThisCapture(Loc);
16522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true);
16532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
16542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return BuildMemberReferenceExpr(baseExpr, ThisTy,
16562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  /*OpLoc*/ SourceLocation(),
16572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  /*IsArrow*/ true,
1658e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                  SS, TemplateKWLoc,
16592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  /*FirstQualifierInScope*/ 0,
16602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  R, TemplateArgs);
16612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
1662