SemaExprMember.cpp revision cefc3afac14d29de5aba7810cc8fe6c858949e9d
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()) {
1182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (dyn_cast<FieldDecl>(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");
4392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union)
4402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << BaseExpr->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),
5512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              SemaRef.PDiag(diag::err_typecheck_incomplete_tag)
5522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas 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,
8162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         const TemplateArgumentListInfo *TemplateArgs,
8172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               bool SuppressQualifierCheck) {
8182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType BaseType = BaseExprType;
8192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsArrow) {
8202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(BaseType->isPointerType());
8213c3b7f90a863af43fa63043d396553ecf205351cJohn McCall    BaseType = BaseType->castAs<PointerType>()->getPointeeType();
8222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
8232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  R.setBaseObjectType(BaseType);
8242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo();
8262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName MemberName = MemberNameInfo.getName();
8272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation MemberLoc = MemberNameInfo.getLoc();
8282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.isAmbiguous())
8302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
8312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.empty()) {
8332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Rederive where we looked up.
8342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclContext *DC = (SS.isSet()
8352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       ? computeDeclContext(SS, false)
8362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       : BaseType->getAs<RecordType>()->getDecl());
8372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(R.getNameLoc(), diag::err_no_member)
8392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << MemberName << DC
8402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange());
8412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
8422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
8432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Diagnose lookups that find only declarations from a non-base
8452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // type.  This is possible for either qualified lookups (which may
8462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // have been qualified with an unrelated type) or implicit member
8472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // expressions (which were found with unqualified lookup and thus
8482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // may have come from an enclosing scope).  Note that it's okay for
8492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // lookup to find declarations from a non-base type as long as those
8502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // aren't the ones picked by overload resolution.
8512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if ((SS.isSet() || !BaseExpr ||
8522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor       (isa<CXXThisExpr>(BaseExpr) &&
8532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        cast<CXXThisExpr>(BaseExpr)->isImplicit())) &&
8542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      !SuppressQualifierCheck &&
8552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R))
8562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
857d12505054130b24f7696440efdbd1aa660feb6f3Fariborz Jahanian
8582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Construct an unresolved result if we in fact got an unresolved
8592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // result.
8602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.isOverloadedResult() || R.isUnresolvableResult()) {
8612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Suppress any lookup-related diagnostics; we'll do these when we
8622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // pick a member.
8632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    R.suppressDiagnostics();
8642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    UnresolvedMemberExpr *MemExpr
8662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(),
8672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     BaseExpr, BaseExprType,
8682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     IsArrow, OpLoc,
8692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     SS.getWithLocInContext(Context),
870e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                     TemplateKWLoc, MemberNameInfo,
8712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     TemplateArgs, R.begin(), R.end());
8722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned(MemExpr);
8742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
8752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(R.isSingleResult());
8772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclAccessPair FoundDecl = R.begin().getPair();
8782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  NamedDecl *MemberDecl = R.getFoundDecl();
8792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: diagnose the presence of template arguments now.
8812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If the decl being referenced had an error, return an error for this
8832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // sub-expr without emitting another error, in order to avoid cascading
8842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // error cases.
8852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (MemberDecl->isInvalidDecl())
8862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
8872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle the implicit-member-access case.
8892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!BaseExpr) {
8902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If this is not an instance member, convert to a non-member access.
8912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!MemberDecl->isCXXInstanceMember())
8922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl);
8932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SourceLocation Loc = R.getNameLoc();
8952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (SS.getRange().isValid())
8962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Loc = SS.getRange().getBegin();
89772899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    CheckCXXThisCapture(Loc);
8982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true);
8992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool ShouldCheckUse = true;
9022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) {
9032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Don't diagnose the use of a virtual member function unless it's
9042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // explicitly qualified.
9052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (MD->isVirtual() && !SS.isSet())
9062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ShouldCheckUse = false;
9072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Check the use of this member.
9102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) {
9112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Owned(BaseExpr);
9122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
9132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl))
9162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow,
9172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   SS, FD, FoundDecl, MemberNameInfo);
9182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl))
9202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // We may have found a field within an anonymous union or struct
9212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // (C++ [class.union]).
9222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD,
9232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                    BaseExpr, OpLoc);
9242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) {
9265f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
9275f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 TemplateKWLoc, Var, FoundDecl, MemberNameInfo,
9282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 Var->getType().getNonReferenceType(),
9292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 VK_LValue, OK_Ordinary));
9302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) {
9332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprValueKind valueKind;
9342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType type;
9352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (MemberFn->isInstance()) {
9362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      valueKind = VK_RValue;
9372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      type = Context.BoundMemberTy;
9382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } else {
9392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      valueKind = VK_LValue;
9402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      type = MemberFn->getType();
9412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
9422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9435f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
9445f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 TemplateKWLoc, MemberFn, FoundDecl,
9455f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 MemberNameInfo, type, valueKind,
9465f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 OK_Ordinary));
9472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?");
9492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) {
9515f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
9525f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 TemplateKWLoc, Enum, FoundDecl, MemberNameInfo,
9532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 Enum->getType(), VK_RValue, OK_Ordinary));
9542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Owned(BaseExpr);
9572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // We found something that we didn't expect. Complain.
9592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (isa<TypeDecl>(MemberDecl))
9602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(MemberLoc, diag::err_typecheck_member_reference_type)
9612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << MemberName << BaseType << int(IsArrow);
9622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  else
9632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(MemberLoc, diag::err_typecheck_member_reference_unknown)
9642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << MemberName << BaseType << int(IsArrow);
9652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Diag(MemberDecl->getLocation(), diag::note_member_declared_here)
9672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    << MemberName;
9682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  R.suppressDiagnostics();
9692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return ExprError();
9702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
9712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Given that normal member access failed on the given expression,
9732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// and given that the expression's type involves builtin-id or
9742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// builtin-Class, decide whether substituting in the redefinition
9752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// types would be profitable.  The redefinition type is whatever
9762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// this translation unit tried to typedef to id/Class;  we store
9772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// it to the side and then re-use it in places like this.
9782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
9792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ObjCObjectPointerType *opty
9802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    = base.get()->getType()->getAs<ObjCObjectPointerType>();
9812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!opty) return false;
9822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ObjCObjectType *ty = opty->getObjectType();
9842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType redef;
9862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (ty->isObjCId()) {
98701a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor    redef = S.Context.getObjCIdRedefinitionType();
9882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else if (ty->isObjCClass()) {
98901a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor    redef = S.Context.getObjCClassRedefinitionType();
9902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
9912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
9922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Do the substitution as long as the redefinition type isn't just a
9952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // possibly-qualified pointer to builtin-id or builtin-Class again.
9962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  opty = redef->getAs<ObjCObjectPointerType>();
9972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (opty && !opty->getObjectType()->getInterface() != 0)
9982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
9992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  base = S.ImpCastExprToType(base.take(), redef, CK_BitCast);
10012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return true;
10022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
10032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10046dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool isRecordType(QualType T) {
10056dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return T->isRecordType();
10066dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
10076dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool isPointerToRecordType(QualType T) {
10086dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (const PointerType *PT = T->getAs<PointerType>())
10096dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return PT->getPointeeType()->isRecordType();
10106dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return false;
10116dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
10126dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
10139138b4e96429cbaae00c52c15c960f72b6645088Richard Smith/// Perform conversions on the LHS of a member access expression.
10149138b4e96429cbaae00c52c15c960f72b6645088Richard SmithExprResult
10159138b4e96429cbaae00c52c15c960f72b6645088Richard SmithSema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) {
1016059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman  if (IsArrow && !Base->getType()->isFunctionType())
1017059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman    return DefaultFunctionArrayLvalueConversion(Base);
10189138b4e96429cbaae00c52c15c960f72b6645088Richard Smith
1019059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman  return CheckPlaceholderExpr(Base);
10209138b4e96429cbaae00c52c15c960f72b6645088Richard Smith}
10219138b4e96429cbaae00c52c15c960f72b6645088Richard Smith
10222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Look up the given member of the given non-type-dependent
10232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression.  This can return in one of two ways:
10242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///  * If it returns a sentinel null-but-valid result, the caller will
10252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///    assume that lookup was performed and the results written into
10262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///    the provided structure.  It will take over from there.
10272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///  * Otherwise, the returned expression will be produced in place of
10282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///    an ordinary member expression.
10292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///
10302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The ObjCImpDecl bit is a gross hack that will need to be properly
10312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// fixed for ObjC++.
10322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
10332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
10342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       bool &IsArrow, SourceLocation OpLoc,
10352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       CXXScopeSpec &SS,
10362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       Decl *ObjCImpDecl, bool HasTemplateArgs) {
10372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(BaseExpr.get() && "no base expression");
10382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Perform default conversions.
10409138b4e96429cbaae00c52c15c960f72b6645088Richard Smith  BaseExpr = PerformMemberExprBaseConversion(BaseExpr.take(), IsArrow);
10416dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (BaseExpr.isInvalid())
10426dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return ExprError();
10432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType BaseType = BaseExpr.get()->getType();
10452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!BaseType->isDependentType());
10462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName MemberName = R.getLookupName();
10482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation MemberLoc = R.getNameLoc();
10492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // For later type-checking purposes, turn arrow accesses into dot
10512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // accesses.  The only access type we support that doesn't follow
10522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // the C equivalence "a->b === (*a).b" is ObjC property accesses,
10532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // and those never use arrows, so this is unaffected.
10542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsArrow) {
10552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (const PointerType *Ptr = BaseType->getAs<PointerType>())
10562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      BaseType = Ptr->getPointeeType();
10572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else if (const ObjCObjectPointerType *Ptr
10582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor               = BaseType->getAs<ObjCObjectPointerType>())
10592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      BaseType = Ptr->getPointeeType();
10602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else if (BaseType->isRecordType()) {
10612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Recover from arrow accesses to records, e.g.:
10622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      //   struct MyRecord foo;
10632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      //   foo->bar
10642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // This is actually well-formed in C++ if MyRecord has an
10652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // overloaded operator->, but that should have been dealt with
10662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // by now.
10672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
10682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
10692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << FixItHint::CreateReplacement(OpLoc, ".");
10702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      IsArrow = false;
1071059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman    } else if (BaseType->isFunctionType()) {
10722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      goto fail;
10732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } else {
10742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
10752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << BaseExpr.get()->getSourceRange();
10762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
10772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
10782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
10792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle field access to simple records.
10812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const RecordType *RTy = BaseType->getAs<RecordType>()) {
10822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(),
10832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 RTy, OpLoc, SS, HasTemplateArgs))
10842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
10852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Returning valid-but-null is how we indicate to the caller that
10872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // the lookup result was filled in.
10882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned((Expr*) 0);
10892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
10902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle ivar access to Objective-C objects.
10922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) {
10935a706dc1b17f875c7fce20f1fbf9ca372be4c331Douglas Gregor    if (!SS.isEmpty() && !SS.isInvalid()) {
1094b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1095b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << 1 << SS.getScopeRep()
1096b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << FixItHint::CreateRemoval(SS.getRange());
1097b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      SS.clear();
1098b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor    }
1099b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor
11002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
11012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // There are three cases for the base type:
11032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    //   - builtin id (qualified or unqualified)
11042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    //   - builtin Class (qualified or unqualified)
11052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    //   - an interface
11062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ObjCInterfaceDecl *IDecl = OTy->getInterface();
11072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IDecl) {
11084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().ObjCAutoRefCount &&
11092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          (OTy->isObjCId() || OTy->isObjCClass()))
11102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        goto fail;
11112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // There's an implicit 'isa' ivar on all objects.
11122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // But we only actually find it this way on objects of type 'id',
1113556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian      // apparently.ghjg
1114556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian      if (OTy->isObjCId() && Member->isStr("isa")) {
1115556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian        Diag(MemberLoc, diag::warn_objc_isa_use);
11162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc,
11172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               Context.getObjCClassType()));
1118556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian      }
11192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
11212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
11222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                ObjCImpDecl, HasTemplateArgs);
11232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      goto fail;
11242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
11252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1126d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor    if (RequireCompleteType(OpLoc, BaseType,
1127d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor                            PDiag(diag::err_typecheck_incomplete_tag)
1128d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor                              << BaseExpr.get()->getSourceRange()))
1129d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      return ExprError();
1130d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
11312c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek    ObjCInterfaceDecl *ClassDeclared = 0;
11322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared);
11332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IV) {
11352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Attempt to correct for typos in ivar names.
1136e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain      DeclFilterCCC<ObjCIvarDecl> Validator;
1137e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain      Validator.IsObjCIvarLookup = IsArrow;
1138e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain      if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
1139e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain                                                 LookupMemberName, NULL, NULL,
114016e46dd0c284296cea819dfbf67942ecef02894dKaelyn Uhrain                                                 Validator, IDecl)) {
1141e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain        IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>();
11422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Diag(R.getNameLoc(),
11432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor             diag::err_typecheck_member_reference_ivar_suggest)
11442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << IDecl->getDeclName() << MemberName << IV->getDeclName()
11452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << FixItHint::CreateReplacement(R.getNameLoc(),
11462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                          IV->getNameAsString());
11472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Diag(IV->getLocation(), diag::note_previous_decl)
11482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << IV->getDeclName();
11492c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek
11502c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        // Figure out the class that declares the ivar.
11512c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        assert(!ClassDeclared);
11522c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        Decl *D = cast<Decl>(IV->getDeclContext());
11532c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        if (ObjCCategoryDecl *CAT = dyn_cast<ObjCCategoryDecl>(D))
11542c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek          D = CAT->getClassInterface();
11552c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        ClassDeclared = cast<ObjCInterfaceDecl>(D);
11562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      } else {
11576326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian        if (IsArrow && IDecl->FindPropertyDeclaration(Member)) {
11586326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          Diag(MemberLoc,
11596326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          diag::err_property_found_suggest)
11606326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          << Member << BaseExpr.get()->getType()
11616326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          << FixItHint::CreateReplacement(OpLoc, ".");
11626326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          return ExprError();
11636326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian        }
11642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Diag(MemberLoc, diag::err_typecheck_member_reference_ivar)
11662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << IDecl->getDeclName() << MemberName
11672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << BaseExpr.get()->getSourceRange();
11682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return ExprError();
11692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
11702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
11712c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek
11722c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek    assert(ClassDeclared);
11732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If the decl being referenced had an error, return an error for this
11752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // sub-expr without emitting another error, in order to avoid cascading
11762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // error cases.
11772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IV->isInvalidDecl())
11782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
11792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Check whether we can reference this field.
11812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (DiagnoseUseOfDecl(IV, MemberLoc))
11822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
11832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IV->getAccessControl() != ObjCIvarDecl::Public &&
11842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        IV->getAccessControl() != ObjCIvarDecl::Package) {
11852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCInterfaceDecl *ClassOfMethodDecl = 0;
11862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ObjCMethodDecl *MD = getCurMethodDecl())
11872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        ClassOfMethodDecl =  MD->getClassInterface();
11882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      else if (ObjCImpDecl && getCurFunctionDecl()) {
11892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // Case of a c-function declared inside an objc implementation.
11902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // FIXME: For a c-style function nested inside an objc implementation
11912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // class, there is no implementation context available, so we pass
11922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // down the context as argument to this routine. Ideally, this context
11932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // need be passed down in the AST node and somehow calculated from the
11942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // AST for a function decl.
11952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ObjCImplementationDecl *IMPD =
11962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor              dyn_cast<ObjCImplementationDecl>(ObjCImpDecl))
11972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          ClassOfMethodDecl = IMPD->getClassInterface();
11982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        else if (ObjCCategoryImplDecl* CatImplClass =
11992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                   dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl))
12002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          ClassOfMethodDecl = CatImplClass->getClassInterface();
12012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
12024e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!getLangOpts().DebuggerSupport) {
1203458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian        if (IV->getAccessControl() == ObjCIvarDecl::Private) {
1204458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian          if (!declaresSameEntity(ClassDeclared, IDecl) ||
1205458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian              !declaresSameEntity(ClassOfMethodDecl, ClassDeclared))
1206458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian            Diag(MemberLoc, diag::error_private_ivar_access)
1207458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian              << IV->getDeclName();
1208458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian        } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl))
1209458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian          // @protected
1210458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian          Diag(MemberLoc, diag::error_protected_ivar_access)
12112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            << IV->getDeclName();
1212458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian      }
12132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
12144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount) {
12152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts();
12162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp))
12172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (UO->getOpcode() == UO_Deref)
12182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          BaseExp = UO->getSubExpr()->IgnoreParenCasts();
12192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp))
12212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
12222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          Diag(DE->getLocation(), diag::error_arc_weak_ivar_access);
12232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
12242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(),
12262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               MemberLoc, BaseExpr.take(),
12272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               IsArrow));
12282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
12292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Objective-C property access.
12312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ObjCObjectPointerType *OPT;
12322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) {
12335a706dc1b17f875c7fce20f1fbf9ca372be4c331Douglas Gregor    if (!SS.isEmpty() && !SS.isInvalid()) {
1234b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1235b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << 0 << SS.getScopeRep()
1236b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << FixItHint::CreateRemoval(SS.getRange());
1237b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      SS.clear();
1238b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor    }
1239b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor
12402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // This actually uses the base as an r-value.
12412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseExpr = DefaultLvalueConversion(BaseExpr.take());
12422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseExpr.isInvalid())
12432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
12442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType()));
12462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
12482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    const ObjCObjectType *OT = OPT->getObjectType();
12502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // id, with and without qualifiers.
12522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (OT->isObjCId()) {
12532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Check protocols on qualified interfaces.
12542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
12552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) {
12562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) {
12572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          // Check the use of this declaration
12582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          if (DiagnoseUseOfDecl(PD, MemberLoc))
12592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            return ExprError();
12602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12613c3b7f90a863af43fa63043d396553ecf205351cJohn McCall          return Owned(new (Context) ObjCPropertyRefExpr(PD,
12623c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                         Context.PseudoObjectTy,
12632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         VK_LValue,
12642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         OK_ObjCProperty,
12652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         MemberLoc,
12662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         BaseExpr.take()));
12672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        }
12682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) {
12702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          // Check the use of this method.
12712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          if (DiagnoseUseOfDecl(OMD, MemberLoc))
12722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            return ExprError();
12732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          Selector SetterSel =
12742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            SelectorTable::constructSetterName(PP.getIdentifierTable(),
12752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               PP.getSelectorTable(), Member);
12762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          ObjCMethodDecl *SMD = 0;
12772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0,
12782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                     SetterSel, Context))
12792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            SMD = dyn_cast<ObjCMethodDecl>(SDecl);
12802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12813c3b7f90a863af43fa63043d396553ecf205351cJohn McCall          return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD,
12823c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                         Context.PseudoObjectTy,
12833c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                         VK_LValue, OK_ObjCProperty,
12842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         MemberLoc, BaseExpr.take()));
12852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        }
12862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
12872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Use of id.member can only be for a property reference. Do not
12882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // use the 'id' redefinition in this case.
12892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
12902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
12912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                ObjCImpDecl, HasTemplateArgs);
12922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError(Diag(MemberLoc, diag::err_property_not_found)
12942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         << MemberName << BaseType);
12952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
12962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // 'Class', unqualified only.
12982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (OT->isObjCClass()) {
12992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Only works in a method declaration (??!).
13002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCMethodDecl *MD = getCurMethodDecl();
13012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!MD) {
13022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
13032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
13042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  ObjCImpDecl, HasTemplateArgs);
13052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        goto fail;
13072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Also must look for a getter name which uses property syntax.
13102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
13112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCInterfaceDecl *IFace = MD->getClassInterface();
13122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCMethodDecl *Getter;
13132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if ((Getter = IFace->lookupClassMethod(Sel))) {
13142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // Check the use of this method.
13152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (DiagnoseUseOfDecl(Getter, MemberLoc))
13162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          return ExprError();
13172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      } else
13182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Getter = IFace->lookupPrivateMethod(Sel, false);
13192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // If we found a getter then this may be a valid dot-reference, we
13202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // will look for the matching setter, in case it is needed.
13212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Selector SetterSel =
13222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        SelectorTable::constructSetterName(PP.getIdentifierTable(),
13232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                           PP.getSelectorTable(), Member);
13242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
13252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!Setter) {
13262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // If this reference is in an @implementation, also check for 'private'
13272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // methods.
13282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Setter = IFace->lookupPrivateMethod(SetterSel, false);
13292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Look through local category implementations associated with the class.
13312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!Setter)
13322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Setter = IFace->getCategoryClassMethod(SetterSel);
13332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
13352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return ExprError();
13362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (Getter || Setter) {
13382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
13393c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                       Context.PseudoObjectTy,
13403c3b7f90a863af43fa63043d396553ecf205351cJohn McCall                                                       VK_LValue, OK_ObjCProperty,
13412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                       MemberLoc, BaseExpr.take()));
13422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
13452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
13462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                ObjCImpDecl, HasTemplateArgs);
13472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError(Diag(MemberLoc, diag::err_property_not_found)
13492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         << MemberName << BaseType);
13502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
13512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Normal property access.
13536326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian    return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc,
13546326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian                                     MemberName, MemberLoc,
13552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     SourceLocation(), QualType(), false);
13562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
13572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle 'field access' to vectors, such as 'V.xx'.
13592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (BaseType->isExtVectorType()) {
13602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // FIXME: this expr should store IsArrow.
13612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
13622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind());
13632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc,
13642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                           Member, MemberLoc);
13652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (ret.isNull())
13662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
13672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(),
13692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                    *Member, MemberLoc));
13702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
13712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Adjust builtin-sel to the appropriate redefinition type if that's
13732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // not just a pointer to builtin-sel again.
13742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsArrow &&
13752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) &&
137601a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor      !Context.getObjCSelRedefinitionType()->isObjCSelType()) {
137701a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor    BaseExpr = ImpCastExprToType(BaseExpr.take(),
137801a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor                                 Context.getObjCSelRedefinitionType(),
13792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 CK_BitCast);
13802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
13812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                            ObjCImpDecl, HasTemplateArgs);
13822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
13832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Failure cases.
13852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor fail:
13862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Recover from dot accesses to pointers, e.g.:
13882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   type *foo;
13892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   foo.bar
13902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This is actually well-formed in two cases:
13912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   - 'type' is an Objective C type
13922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   - 'bar' is a pseudo-destructor name which happens to refer to
13932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //     the appropriate pointer type
13942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
13952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IsArrow && Ptr->getPointeeType()->isRecordType() &&
13962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        MemberName.getNameKind() != DeclarationName::CXXDestructorName) {
13972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
13982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
13992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << FixItHint::CreateReplacement(OpLoc, "->");
14002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Recurse as an -> access.
14022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      IsArrow = true;
14032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
14042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              ObjCImpDecl, HasTemplateArgs);
14052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
14062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If the user is trying to apply -> or . to a function name, it's probably
14092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // because they forgot parentheses to call that function.
14106dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (tryToRecoverWithCall(BaseExpr,
14116dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                           PDiag(diag::err_member_reference_needs_call),
14126dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                           /*complain*/ false,
1413059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman                           IsArrow ? &isPointerToRecordType : &isRecordType)) {
14146dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    if (BaseExpr.isInvalid())
14152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
14166dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take());
14176dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
14186dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                            ObjCImpDecl, HasTemplateArgs);
14192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union)
14222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    << BaseType << BaseExpr.get()->getSourceRange();
14232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return ExprError();
14252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
14262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The main callback when the parser finds something like
14282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   expression . [nested-name-specifier] identifier
14292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   expression -> [nested-name-specifier] identifier
14302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// where 'identifier' encompasses a fairly broad spectrum of
14312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// possibilities, including destructor and operator references.
14322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///
14332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param OpKind either tok::arrow or tok::period
14342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param HasTrailingLParen whether the next token is '(', which
14352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   is used to diagnose mis-uses of special members that can
14362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   only be called
14372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param ObjCImpDecl the current ObjC @implementation decl;
14382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   this is an ugly hack around the fact that ObjC @implementations
14392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   aren't properly put in the context chain
14402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base,
14412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       SourceLocation OpLoc,
14422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       tok::TokenKind OpKind,
14432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       CXXScopeSpec &SS,
1444e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                       SourceLocation TemplateKWLoc,
14452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       UnqualifiedId &Id,
14462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       Decl *ObjCImpDecl,
14472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       bool HasTrailingLParen) {
14482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (SS.isSet() && SS.isInvalid())
14492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
14502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Warn about the explicit constructor calls Microsoft extension.
14524e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().MicrosoftExt &&
14532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Id.getKind() == UnqualifiedId::IK_ConstructorName)
14542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(Id.getSourceRange().getBegin(),
14552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         diag::ext_ms_explicit_constructor_call);
14562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  TemplateArgumentListInfo TemplateArgsBuffer;
14582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Decompose the name into its component parts.
14602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationNameInfo NameInfo;
14612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const TemplateArgumentListInfo *TemplateArgs;
14622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DecomposeUnqualifiedId(Id, TemplateArgsBuffer,
14632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         NameInfo, TemplateArgs);
14642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName Name = NameInfo.getName();
14662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool IsArrow = (OpKind == tok::arrow);
14672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  NamedDecl *FirstQualifierInScope
14692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S,
14702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       static_cast<NestedNameSpecifier*>(SS.getScopeRep())));
14712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This is a postfix expression, so get rid of ParenListExprs.
14732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
14742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Result.isInvalid()) return ExprError();
14752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Base = Result.take();
14762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Base->getType()->isDependentType() || Name.isDependentName() ||
14782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      isDependentScopeSpecifier(SS)) {
14792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Result = ActOnDependentMemberExpr(Base, Base->getType(),
14802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      IsArrow, OpLoc,
1481e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      SS, TemplateKWLoc, FirstQualifierInScope,
14822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      NameInfo, TemplateArgs);
14832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
14842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    LookupResult R(*this, NameInfo, LookupMemberName);
14852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprResult BaseResult = Owned(Base);
14862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
14872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              SS, ObjCImpDecl, TemplateArgs != 0);
14882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseResult.isInvalid())
14892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
14902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Base = BaseResult.take();
14912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.isInvalid()) {
14932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Owned(Base);
14942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
14952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
14962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.get()) {
14982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // The only way a reference to a destructor can be used is to
14992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // immediately call it, which falls into this case.  If the
15002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // next token is not a '(', produce a diagnostic and build the
15012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // call now.
15022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!HasTrailingLParen &&
15032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          Id.getKind() == UnqualifiedId::IK_DestructorName)
15042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return DiagnoseDtorReference(NameInfo.getLoc(), Result.get());
15052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return move(Result);
15072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
15082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Result = BuildMemberReferenceExpr(Base, Base->getType(),
1510e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      OpLoc, IsArrow, SS, TemplateKWLoc,
1511e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      FirstQualifierInScope, R, TemplateArgs);
15122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
15132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return move(Result);
15152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
15162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic ExprResult
15182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorBuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
15192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const CXXScopeSpec &SS, FieldDecl *Field,
15202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        DeclAccessPair FoundDecl,
15212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const DeclarationNameInfo &MemberNameInfo) {
15222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // x.a is an l-value if 'a' has a reference type. Otherwise:
15232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // x.a is an l-value/x-value/pr-value if the base is (and note
15242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   that *x is always an l-value), except that if the base isn't
15252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   an ordinary object then we must have an rvalue.
15262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprValueKind VK = VK_LValue;
15272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprObjectKind OK = OK_Ordinary;
15282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!IsArrow) {
15292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseExpr->getObjectKind() == OK_Ordinary)
15302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      VK = BaseExpr->getValueKind();
15312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else
15322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      VK = VK_RValue;
15332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
15342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (VK != VK_RValue && Field->isBitField())
15352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    OK = OK_BitField;
15362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref]
15382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType MemberType = Field->getType();
15392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) {
15402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    MemberType = Ref->getPointeeType();
15412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    VK = VK_LValue;
15422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
15432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType BaseType = BaseExpr->getType();
15442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType();
15452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Qualifiers BaseQuals = BaseType.getQualifiers();
15472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // GC attributes are never picked up by members.
15492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseQuals.removeObjCGCAttr();
15502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // CVR attributes from the base are picked up by members,
15522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // except that 'mutable' members don't pick up 'const'.
15532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Field->isMutable()) BaseQuals.removeConst();
15542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Qualifiers MemberQuals
15562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    = S.Context.getCanonicalType(MemberType).getQualifiers();
15572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // TR 18037 does not allow fields to be declared with address spaces.
15592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(!MemberQuals.hasAddressSpace());
15602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Qualifiers Combined = BaseQuals + MemberQuals;
15622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Combined != MemberQuals)
15632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      MemberType = S.Context.getQualifiedType(MemberType, Combined);
15642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
15652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprResult Base =
15672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
15682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  FoundDecl, Field);
15692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Base.isInvalid())
15702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
15715f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  return S.Owned(BuildMemberExpr(S, S.Context, Base.take(), IsArrow, SS,
1572e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                 /*TemplateKWLoc=*/SourceLocation(),
15732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 Field, FoundDecl, MemberNameInfo,
15742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 MemberType, VK, OK));
15752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
15762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Builds an implicit member access expression.  The current context
15782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// is known to be an instance method, and the given unqualified lookup
15792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// set is known to contain only instance members, at least one of which
15802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// is from an appropriate type.
15812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
15822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
1583e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                              SourceLocation TemplateKWLoc,
15842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              LookupResult &R,
15852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              const TemplateArgumentListInfo *TemplateArgs,
15862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              bool IsKnownInstance) {
15872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!R.empty() && !R.isAmbiguous());
15882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation loc = R.getNameLoc();
15902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // We may have found a field within an anonymous union or struct
15922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // (C++ [class.union]).
15932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: template-ids inside anonymous structs?
15942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>())
15952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD);
15962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If this is known to be an instance access, go ahead and build an
15982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // implicit 'this' expression now.
15992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // 'this' expression now.
1600341350ee62abd1ad818e1e3d926cd718960e439bDouglas Gregor  QualType ThisTy = getCurrentThisType();
16012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'");
16022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Expr *baseExpr = 0; // null signifies implicit access
16042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsKnownInstance) {
16052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SourceLocation Loc = R.getNameLoc();
16062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (SS.getRange().isValid())
16072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Loc = SS.getRange().getBegin();
160872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    CheckCXXThisCapture(Loc);
16092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true);
16102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
16112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return BuildMemberReferenceExpr(baseExpr, ThisTy,
16132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  /*OpLoc*/ SourceLocation(),
16142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  /*IsArrow*/ true,
1615e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                  SS, TemplateKWLoc,
16162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  /*FirstQualifierInScope*/ 0,
16172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  R, TemplateArgs);
16182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
1619