12b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//===--- SemaExprMember.cpp - Semantic Analysis for Expressions -----------===//
22b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
32b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//                     The LLVM Compiler Infrastructure
42b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
52b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// This file is distributed under the University of Illinois Open Source
62b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// License. See LICENSE.TXT for details.
72b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
82b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//===----------------------------------------------------------------------===//
92b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//  This file implements semantic analysis member access expressions.
112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//===----------------------------------------------------------------------===//
132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/Sema/SemaInternal.h"
142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/Sema/Lookup.h"
152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/Sema/Scope.h"
162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/DeclCXX.h"
172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/DeclObjC.h"
182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/DeclTemplate.h"
192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/ExprCXX.h"
202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/AST/ExprObjC.h"
212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor#include "clang/Lex/Preprocessor.h"
222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorusing namespace clang;
242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorusing namespace sema;
252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Determines if the given class is provably not derived from all of
272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// the prospective base classes.
282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic bool IsProvablyNotDerivedFrom(Sema &SemaRef,
292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     CXXRecordDecl *Record,
302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                            const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) {
312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Bases.count(Record->getCanonicalDecl()))
322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  RecordDecl *RD = Record->getDefinition();
352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!RD) return false;
362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Record = cast<CXXRecordDecl>(RD);
372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(),
392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         E = Record->bases_end(); I != E; ++I) {
402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType());
412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>();
422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!BaseRT) return false;
432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl());
452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases))
462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return false;
472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return true;
502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorenum IMAKind {
532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference is definitely not an instance member access.
542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Static,
552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference may be an implicit instance member access.
572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Mixed,
582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
599bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman  /// The reference may be to an instance member, but it might be invalid if
602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// so, because the context is not an instance method.
612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Mixed_StaticContext,
622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference may be to an instance member, but it is invalid if
642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// so, because the context is from an unrelated class.
652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Mixed_Unrelated,
662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference is definitely an implicit instance member access.
682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Instance,
692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference may be to an unresolved using declaration.
712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Unresolved,
722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// The reference may be to an unresolved using declaration and the
742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// context is not an instance method.
752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Unresolved_StaticContext,
762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
77ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // The reference refers to a field which is not a member of the containing
78ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // class, which is allowed because we're in C++11 mode and the context is
79ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  // unevaluated.
80ef331b783bb96a0f0e34afdb7ef46677dc4764cbEli Friedman  IMA_Field_Uneval_Context,
819bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman
822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// All possible referrents are instance members and the current
832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// context is not an instance method.
842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Error_StaticContext,
852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// All possible referrents are instance members of an unrelated
872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  /// class.
882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IMA_Error_Unrelated
892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor};
902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The given lookup names class member(s) and is not being used for
922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// an address-of-member expression.  Classify the type of access
932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// according to whether it's possible that this reference names an
949bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman/// instance member.  This is best-effort in dependent contexts; it is okay to
952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// conservatively answer "yes", in which case some errors will simply
962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// not be caught until template-instantiation.
972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                            Scope *CurScope,
992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                            const LookupResult &R) {
1002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!R.empty() && (*R.begin())->isCXXClassMember());
1012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclContext *DC = SemaRef.getFunctionLevelDeclContext();
1032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
104cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  bool isStaticContext = SemaRef.CXXThisTypeOverride.isNull() &&
105cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor    (!isa<CXXMethodDecl>(DC) || cast<CXXMethodDecl>(DC)->isStatic());
1062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.isUnresolvableResult())
1082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved;
1092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Collect all the declaring classes of instance members we find.
1112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool hasNonInstance = false;
1129bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman  bool isField = false;
1132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes;
1142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
1152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    NamedDecl *D = *I;
1162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (D->isCXXInstanceMember()) {
1181dfc4ba88714d8ac9a85dba051cf94e57f7b3e04Aaron Ballman      if (dyn_cast<FieldDecl>(D) || dyn_cast<IndirectFieldDecl>(D))
1199bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman        isField = true;
1202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext());
1222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Classes.insert(R->getCanonicalDecl());
1232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
1242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else
1252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      hasNonInstance = true;
1262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
1272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If we didn't find any instance members, it can't be an implicit
1292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // member reference.
1302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Classes.empty())
1312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return IMA_Static;
1322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
133d390de9c6312684c5e5b333f434199e193c7467aRichard Smith  bool IsCXX11UnevaluatedField = false;
1344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (SemaRef.getLangOpts().CPlusPlus0x && isField) {
1352c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    // C++11 [expr.prim.general]p12:
1362c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //   An id-expression that denotes a non-static data member or non-static
1372c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //   member function of a class can only be used:
1382c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //   (...)
1392c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //   - if that id-expression denotes a non-static data member and it
1402c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    //     appears in an unevaluated operand.
1412c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    const Sema::ExpressionEvaluationContextRecord& record
1422c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith      = SemaRef.ExprEvalContexts.back();
1432c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith    if (record.Context == Sema::Unevaluated)
144d390de9c6312684c5e5b333f434199e193c7467aRichard Smith      IsCXX11UnevaluatedField = true;
1452c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith  }
1462c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith
1472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If the current context is not an instance method, it can't be
1482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // an implicit member reference.
1492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (isStaticContext) {
1502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (hasNonInstance)
1512c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith      return IMA_Mixed_StaticContext;
1522c8aee454dac03e4918f0bb6e7fb849953056abaRichard Smith
153d390de9c6312684c5e5b333f434199e193c7467aRichard Smith    return IsCXX11UnevaluatedField ? IMA_Field_Uneval_Context
154d390de9c6312684c5e5b333f434199e193c7467aRichard Smith                                   : IMA_Error_StaticContext;
1552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
1562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  CXXRecordDecl *contextClass;
1582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
1592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    contextClass = MD->getParent()->getCanonicalDecl();
1602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  else
1612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    contextClass = cast<CXXRecordDecl>(DC);
1622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // [class.mfct.non-static]p3:
1642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // ...is used in the body of a non-static member function of class X,
1652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // if name lookup (3.4.1) resolves the name in the id-expression to a
1662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // non-static non-type member of some class C [...]
1672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // ...if C is not X or a base class of X, the class member access expression
1682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // is ill-formed.
1692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.getNamingClass() &&
170d08d599da101f3fe3fd79e853f1dcea6be89d7c2DeLesley Hutchins      contextClass->getCanonicalDecl() !=
171d08d599da101f3fe3fd79e853f1dcea6be89d7c2DeLesley Hutchins        R.getNamingClass()->getCanonicalDecl() &&
1722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      contextClass->isProvablyNotDerivedFrom(R.getNamingClass()))
173d390de9c6312684c5e5b333f434199e193c7467aRichard Smith    return hasNonInstance ? IMA_Mixed_Unrelated :
174d390de9c6312684c5e5b333f434199e193c7467aRichard Smith           IsCXX11UnevaluatedField ? IMA_Field_Uneval_Context :
175d390de9c6312684c5e5b333f434199e193c7467aRichard Smith                                     IMA_Error_Unrelated;
1762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If we can prove that the current context is unrelated to all the
1782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // declaring classes, it can't be an implicit member reference (in
1792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // which case it's an error if any of those members are selected).
1802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsProvablyNotDerivedFrom(SemaRef, contextClass, Classes))
181d390de9c6312684c5e5b333f434199e193c7467aRichard Smith    return hasNonInstance ? IMA_Mixed_Unrelated :
182d390de9c6312684c5e5b333f434199e193c7467aRichard Smith           IsCXX11UnevaluatedField ? IMA_Field_Uneval_Context :
183d390de9c6312684c5e5b333f434199e193c7467aRichard Smith                                     IMA_Error_Unrelated;
1842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return (hasNonInstance ? IMA_Mixed : IMA_Instance);
1862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
1872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Diagnose a reference to a field with no object available.
189a85cf39786fffd6860a940523be01eb02a4935c0Richard Smithstatic void diagnoseInstanceReference(Sema &SemaRef,
1902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      const CXXScopeSpec &SS,
191a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith                                      NamedDecl *Rep,
1922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      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)
2162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    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
2596fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines/// Determine whether input char is from rgba component set.
2606fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hinesstatic bool
2616fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen HinesIsRGBA(char c) {
2626fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines  switch (c) {
2636fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines  case 'r':
2646fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines  case 'g':
2656fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines  case 'b':
2666fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines  case 'a':
2676fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines    return true;
2686fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines  default:
2696fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines    return false;
2706fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines  }
2716fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines}
2726fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines
2732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Check an ext-vector component access expression.
2742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///
2752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// VK should be set in advance to the value kind of the base
2762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression.
2772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic QualType
2782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorCheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK,
2792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        SourceLocation OpLoc, const IdentifierInfo *CompName,
2802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        SourceLocation CompLoc) {
2812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements,
2822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // see FIXME there.
2832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //
2842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: This logic can be greatly simplified by splitting it along
2852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // halving/not halving and reworking the component checking.
2862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ExtVectorType *vecType = baseType->getAs<ExtVectorType>();
2872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // The vector accessor can't exceed the number of elements.
2892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const char *compStr = CompName->getNameStart();
2902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This flag determines whether or not the component is one of the four
2922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // special names that indicate a subset of exactly half the elements are
2932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // to be selected.
2942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool HalvingSwizzle = false;
2952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
2962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This flag determines whether or not CompName has an 's' char prefix,
2972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // indicating that it is a string of hex values to be used as vector indices.
2982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool HexSwizzle = *compStr == 's' || *compStr == 'S';
2992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool HasRepeated = false;
3012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool HasIndex[16] = {};
3022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  int Idx;
3042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Check that we've found one of the special components, or that the component
3062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // names must come from the same set.
3072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
3082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      !strcmp(compStr, "even") || !strcmp(compStr, "odd")) {
3092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    HalvingSwizzle = true;
3102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else if (!HexSwizzle &&
3112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor             (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) {
3126fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines    bool HasRGBA = IsRGBA(*compStr);
3132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    do {
3146fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines      // If we mix/match rgba with xyzw, break to signal that we encountered
3156fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines      // an illegal name.
3166fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines      if (HasRGBA != IsRGBA(*compStr))
3176fbdfec363f8c74e0a4fdb23fa3303cbf5b1fb49Stephen Hines        break;
3182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (HasIndex[Idx]) HasRepeated = true;
3192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      HasIndex[Idx] = true;
3202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      compStr++;
3212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1);
3222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
3232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (HexSwizzle) compStr++;
3242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) {
3252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (HasIndex[Idx]) HasRepeated = true;
3262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      HasIndex[Idx] = true;
3272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      compStr++;
3282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
3292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
3302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!HalvingSwizzle && *compStr) {
3322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // We didn't get to the end of the string. This means the component names
3332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // didn't come from the same set *or* we encountered an illegal name.
3342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal)
3355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      << StringRef(compStr, 1) << SourceRange(CompLoc);
3362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return QualType();
3372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
3382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Ensure no component accessor exceeds the width of the vector type it
3402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // operates on.
3412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!HalvingSwizzle) {
3422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    compStr = CompName->getNameStart();
3432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (HexSwizzle)
3452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      compStr++;
3462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    while (*compStr) {
3482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!vecType->isAccessorWithinNumElements(*compStr++)) {
3492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length)
3502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << baseType << SourceRange(CompLoc);
3512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return QualType();
3522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
3532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
3542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
3552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // The component accessor looks fine - now we need to compute the actual type.
3572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // The vector type is implied by the component accessor. For example,
3582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
3592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // vec4.s0 is a float, vec4.s23 is a vec3, etc.
3602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
3612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2
3622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     : CompName->getLength();
3632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (HexSwizzle)
3642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    CompSize--;
3652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CompSize == 1)
3672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return vecType->getElementType();
3682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (HasRepeated) VK = VK_RValue;
3702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize);
3722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Now look up the TypeDefDecl from the vector type. Without this,
3732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // diagostics look bad. We want extended vector types to appear built-in.
374d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor  for (Sema::ExtVectorDeclsType::iterator
375d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor         I = S.ExtVectorDecls.begin(S.ExternalSource),
376d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor         E = S.ExtVectorDecls.end();
377d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor       I != E; ++I) {
378d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor    if ((*I)->getUnderlyingType() == VT)
379d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor      return S.Context.getTypedefType(*I);
3802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
381d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor
3822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return VT; // should never get here (a typedef type should always be found).
3832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
3842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl,
3862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                IdentifierInfo *Member,
3872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                const Selector &Sel,
3882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                ASTContext &Context) {
3892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Member)
3902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member))
3912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return PD;
3922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel))
3932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return OMD;
3942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
3952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(),
3962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor       E = PDecl->protocol_end(); I != E; ++I) {
3972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel,
3982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                           Context))
3992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return D;
4002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
4012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return 0;
4022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
4032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy,
4052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      IdentifierInfo *Member,
4062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      const Selector &Sel,
4072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      ASTContext &Context) {
4082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Check protocols on qualified interfaces.
4092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Decl *GDecl = 0;
4102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
4112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor       E = QIdTy->qual_end(); I != E; ++I) {
4122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Member)
4132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
4142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        GDecl = PD;
4152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        break;
4162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
4172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Also must look for a getter or setter name which uses property syntax.
4182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) {
4192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      GDecl = OMD;
4202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      break;
4212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
4222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
4232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!GDecl) {
4242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
4252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         E = QIdTy->qual_end(); I != E; ++I) {
4262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Search in the protocol-qualifier list of current protocol.
4272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel,
4282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                       Context);
4292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (GDecl)
4302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return GDecl;
4312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
4322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
4332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return GDecl;
4342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
4352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
4372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType,
4382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               bool IsArrow, SourceLocation OpLoc,
4392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const CXXScopeSpec &SS,
440e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                               SourceLocation TemplateKWLoc,
4412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               NamedDecl *FirstQualifierInScope,
4422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const DeclarationNameInfo &NameInfo,
4432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const TemplateArgumentListInfo *TemplateArgs) {
4442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Even in dependent contexts, try to diagnose base expressions with
4452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // obviously wrong types, e.g.:
4462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //
4472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // T* t;
4482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // t.f;
4492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //
4502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // In Obj-C++, however, the above expression is valid, since it could be
4512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // accessing the 'f' property if T is an Obj-C interface. The extra check
4522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // allows this, while still reporting an error if T is a struct pointer.
4532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!IsArrow) {
4542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    const PointerType *PT = BaseType->getAs<PointerType>();
4554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (PT && (!getLangOpts().ObjC1 ||
4562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor               PT->getPointeeType()->isRecordType())) {
4572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      assert(BaseExpr && "cannot happen with implicit member accesses");
4587d90fe5a941efc106237d23badec816ed65e267fMatt Beaumont-Gay      Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
45973664a4e5aee9216c37bd123aa002430ccb5431dMatt Beaumont-Gay        << BaseType << BaseExpr->getSourceRange() << NameInfo.getSourceRange();
4602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
4612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
4622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
4632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(BaseType->isDependentType() ||
4652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         NameInfo.getName().isDependentName() ||
4662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         isDependentScopeSpecifier(SS));
4672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Get the type being accessed in BaseType.  If this is an arrow, the BaseExpr
4692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // must have pointer type, and the accessed type is the pointee.
4702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType,
4712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                   IsArrow, OpLoc,
4722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               SS.getWithLocInContext(Context),
473e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                                   TemplateKWLoc,
4742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                   FirstQualifierInScope,
4752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                   NameInfo, TemplateArgs));
4762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
4772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// We know that the given qualified member reference points only to
4792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// declarations which do not belong to the static type of the base
4802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression.  Diagnose the problem.
4812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic void DiagnoseQualifiedMemberReference(Sema &SemaRef,
4822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                             Expr *BaseExpr,
4832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                             QualType BaseType,
4842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                             const CXXScopeSpec &SS,
4852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                             NamedDecl *rep,
4862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       const DeclarationNameInfo &nameInfo) {
4872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If this is an implicit member access, use a different set of
4882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // diagnostics.
4892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!BaseExpr)
490a85cf39786fffd6860a940523be01eb02a4935c0Richard Smith    return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo);
4912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated)
4932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    << SS.getRange() << rep << BaseType;
4942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
4952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
4962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// Check whether the declarations we found through a nested-name
4972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// specifier in a member expression are actually members of the base
4982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// type.  The restriction here is:
4992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
5002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//   C++ [expr.ref]p2:
5012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//     ... In these cases, the id-expression shall name a
5022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//     member of the class or of one of its base classes.
5032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor//
5042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// So it's perfectly legitimate for the nested-name specifier to name
5052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// an unrelated class, and for us to find an overload set including
5062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// decls from classes which are not superclasses, as long as the decl
5072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// we actually pick through overload resolution is from a superclass.
5082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorbool Sema::CheckQualifiedMemberReference(Expr *BaseExpr,
5092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                         QualType BaseType,
5102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                         const CXXScopeSpec &SS,
5112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                         const LookupResult &R) {
5122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const RecordType *BaseRT = BaseType->getAs<RecordType>();
5132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!BaseRT) {
5142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // We can't check this yet because the base type is still
5152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // dependent.
5162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(BaseType->isDependentType());
5172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
5182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
5192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl());
5202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
5222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If this is an implicit member reference and we find a
5232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // non-instance member, it's not an error.
5242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!BaseExpr && !(*I)->isCXXInstanceMember())
5252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return false;
5262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Note that we use the DC of the decl, not the underlying decl.
5282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclContext *DC = (*I)->getDeclContext();
5292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    while (DC->isTransparentContext())
5302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      DC = DC->getParent();
5312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!DC->isRecord())
5332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      continue;
5342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord;
5362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl());
5372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord))
5392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return false;
5402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
5412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS,
5432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   R.getRepresentativeDecl(),
5442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   R.getLookupNameInfo());
5452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return true;
5462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
5472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
548e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrainnamespace {
549e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain
550e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain// Callback to only accept typo corrections that are either a ValueDecl or a
551e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain// FunctionTemplateDecl.
552e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrainclass RecordMemberExprValidatorCCC : public CorrectionCandidateCallback {
553e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain public:
554e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain  virtual bool ValidateCandidate(const TypoCorrection &candidate) {
555e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain    NamedDecl *ND = candidate.getCorrectionDecl();
556e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain    return ND && (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND));
557e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain  }
558e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain};
559e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain
560e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain}
561e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain
5622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic bool
563cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas GregorLookupMemberExprInRecord(Sema &SemaRef, LookupResult &R,
5642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         SourceRange BaseRange, const RecordType *RTy,
5652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         SourceLocation OpLoc, CXXScopeSpec &SS,
5662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         bool HasTemplateArgs) {
5672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  RecordDecl *RDecl = RTy->getDecl();
568cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor  if (!SemaRef.isThisOutsideMemberFunctionBody(QualType(RTy, 0)) &&
569cefc3afac14d29de5aba7810cc8fe6c858949e9dDouglas Gregor      SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0),
570d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  diag::err_typecheck_incomplete_tag,
571d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                  BaseRange))
5722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return true;
5732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (HasTemplateArgs) {
5752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // LookupTemplateName doesn't expect these both to exist simultaneously.
5762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0);
5772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    bool MOUS;
5792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS);
5802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
5812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
5822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclContext *DC = RDecl;
5842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (SS.isSet()) {
5852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If the member name was a qualified-id, look into the
5862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // nested-name-specifier.
5872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DC = SemaRef.computeDeclContext(SS, false);
5882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (SemaRef.RequireCompleteDeclContext(SS, DC)) {
5902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag)
5912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << SS.getRange() << DC;
5922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return true;
5932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
5942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(DC && "Cannot handle non-computable dependent contexts in lookup");
5962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
5972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!isa<TypeDecl>(DC)) {
5982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass)
5992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << DC << SS.getRange();
6002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return true;
6012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
6022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
6032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // The record definition is complete, now look up the member.
6052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SemaRef.LookupQualifiedName(R, DC);
6062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!R.empty())
6082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
6092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // We didn't find anything with the given name, so try to correct
6112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // for typos.
6122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName Name = R.getLookupName();
613e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain  RecordMemberExprValidatorCCC Validator;
614d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor  TypoCorrection Corrected = SemaRef.CorrectTypo(R.getLookupNameInfo(),
615d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor                                                 R.getLookupKind(), NULL,
61616e46dd0c284296cea819dfbf67942ecef02894dKaelyn Uhrain                                                 &SS, Validator, DC);
617d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor  R.clear();
618e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain  if (NamedDecl *ND = Corrected.getCorrectionDecl()) {
619d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    std::string CorrectedStr(
6204e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        Corrected.getAsString(SemaRef.getLangOpts()));
621d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    std::string CorrectedQuotedStr(
6224e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie        Corrected.getQuoted(SemaRef.getLangOpts()));
623d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    R.setLookupName(Corrected.getCorrection());
624d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    R.addDecl(ND);
6252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest)
626d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      << Name << DC << CorrectedQuotedStr << SS.getRange()
627d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr);
628d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor    SemaRef.Diag(ND->getLocation(), diag::note_previous_decl)
629d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor      << ND->getDeclName();
6302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
6312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return false;
6332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
6342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
6362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType,
6372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               SourceLocation OpLoc, bool IsArrow,
6382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               CXXScopeSpec &SS,
639e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                               SourceLocation TemplateKWLoc,
6402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               NamedDecl *FirstQualifierInScope,
6412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const DeclarationNameInfo &NameInfo,
6422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const TemplateArgumentListInfo *TemplateArgs) {
6432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (BaseType->isDependentType() ||
6442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      (SS.isSet() && isDependentScopeSpecifier(SS)))
6452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ActOnDependentMemberExpr(Base, BaseType,
6462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                    IsArrow, OpLoc,
647e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                    SS, TemplateKWLoc, FirstQualifierInScope,
6482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                    NameInfo, TemplateArgs);
6492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  LookupResult R(*this, NameInfo, LookupMemberName);
6512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Implicit member accesses.
6532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!Base) {
6542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType RecordTy = BaseType;
6552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType();
6562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (LookupMemberExprInRecord(*this, R, SourceRange(),
6572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 RecordTy->getAs<RecordType>(),
6582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 OpLoc, SS, TemplateArgs != 0))
6592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
6602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Explicit member accesses.
6622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
6632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprResult BaseResult = Owned(Base);
6642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprResult Result =
6652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
6662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0);
6672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseResult.isInvalid())
6692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
6702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Base = BaseResult.take();
6712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.isInvalid()) {
6732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Owned(Base);
6742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
6752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
6762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.get())
6783fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer      return Result;
6792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // LookupMemberExpr can modify Base, and thus change BaseType
6812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseType = Base->getType();
6822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
6832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return BuildMemberReferenceExpr(Base, BaseType,
685e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                  OpLoc, IsArrow, SS, TemplateKWLoc,
686e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                  FirstQualifierInScope, R, TemplateArgs);
6872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
6882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic ExprResult
6902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorBuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
6912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const CXXScopeSpec &SS, FieldDecl *Field,
6922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        DeclAccessPair FoundDecl,
6932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const DeclarationNameInfo &MemberNameInfo);
6942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
6952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
6962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS,
6972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               SourceLocation loc,
6982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               IndirectFieldDecl *indirectField,
6992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               Expr *baseObjectExpr,
7002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               SourceLocation opLoc) {
7012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // First, build the expression that refers to the base object.
7022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool baseObjectIsPointer = false;
7042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Qualifiers baseQuals;
7052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Case 1:  the base of the indirect field is not a field.
7072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  VarDecl *baseVariable = indirectField->getVarDecl();
7082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  CXXScopeSpec EmptySS;
7092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (baseVariable) {
7102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(baseVariable->getType()->isRecordType());
7112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // In principle we could have a member access expression that
7132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // accesses an anonymous struct/union that's a static member of
7142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // the base object's class.  However, under the current standard,
7152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // static data members cannot be anonymous structs or unions.
7162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Supporting this is as easy as building a MemberExpr here.
7172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(!baseObjectExpr && "anonymous struct/union is static data member?");
7182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclarationNameInfo baseNameInfo(DeclarationName(), loc);
7202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprResult result
7222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable);
7232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (result.isInvalid()) return ExprError();
7242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectExpr = result.take();
7262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectIsPointer = false;
7272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseQuals = baseObjectExpr->getType().getQualifiers();
7282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Case 2: the base of the indirect field is a field and the user
7302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // wrote a member expression.
7312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else if (baseObjectExpr) {
7322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // The caller provided the base object expression. Determine
7332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // whether its a pointer and whether it adds any qualifiers to the
7342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // anonymous struct/union fields we're looking into.
7352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType objectType = baseObjectExpr->getType();
7362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (const PointerType *ptr = objectType->getAs<PointerType>()) {
7382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      baseObjectIsPointer = true;
7392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      objectType = ptr->getPointeeType();
7402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } else {
7412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      baseObjectIsPointer = false;
7422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
7432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseQuals = objectType.getQualifiers();
7442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Case 3: the base of the indirect field is a field and we should
7462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // build an implicit member access.
7472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
7482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // We've found a member of an anonymous struct/union that is
7492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // inside a non-anonymous struct/union, so in a well-formed
7502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // program our base object expression is "this".
751341350ee62abd1ad818e1e3d926cd718960e439bDouglas Gregor    QualType ThisTy = getCurrentThisType();
7522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (ThisTy.isNull()) {
7532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(loc, diag::err_invalid_member_use_in_static_method)
7542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << indirectField->getDeclName();
7552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
7562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
7572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Our base object expression is "this".
75972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    CheckCXXThisCapture(loc);
7602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectExpr
7612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true);
7622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectIsPointer = true;
7632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers();
7642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
7652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Build the implicit member references to the field of the
7672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // anonymous struct/union.
7682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Expr *result = baseObjectExpr;
7692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  IndirectFieldDecl::chain_iterator
7702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  FI = indirectField->chain_begin(), FEnd = indirectField->chain_end();
7712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Build the first member access in the chain with full information.
7732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!baseVariable) {
7742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    FieldDecl *field = cast<FieldDecl>(*FI);
7752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // FIXME: use the real found-decl info!
7772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess());
7782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Make a nameInfo that properly uses the anonymous name.
7802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
7812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer,
7832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     EmptySS, field, foundDecl,
7842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     memberNameInfo).take();
7852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseObjectIsPointer = false;
7862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // FIXME: check qualified member access
7882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
7892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // In all cases, we should now skip the first declaration in the chain.
7912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ++FI;
7922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  while (FI != FEnd) {
7942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    FieldDecl *field = cast<FieldDecl>(*FI++);
7952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
7962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // FIXME: these are somewhat meaningless
7972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
7982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess());
7992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false,
8012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     (FI == FEnd? SS : EmptySS), field,
8022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     foundDecl, memberNameInfo).take();
8032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
8042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return Owned(result);
8062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
8072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \brief Build a MemberExpr AST node.
8095f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedmanstatic MemberExpr *BuildMemberExpr(Sema &SemaRef,
8105f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                   ASTContext &C, Expr *Base, bool isArrow,
811e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   const CXXScopeSpec &SS,
812e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   SourceLocation TemplateKWLoc,
813e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                   ValueDecl *Member,
8142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   DeclAccessPair FoundDecl,
8152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   const DeclarationNameInfo &MemberNameInfo,
8162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   QualType Ty,
8172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   ExprValueKind VK, ExprObjectKind OK,
8182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   const TemplateArgumentListInfo *TemplateArgs = 0) {
8193bad5b10cc05fe000340fc6e4efc4d8b9af0a62cRichard Smith  assert((!isArrow || Base->isRValue()) && "-> base must be a pointer rvalue");
8205f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  MemberExpr *E =
8215f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman      MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C),
8225f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                         TemplateKWLoc, Member, FoundDecl, MemberNameInfo,
8235f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                         TemplateArgs, Ty, VK, OK);
8245f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  SemaRef.MarkMemberReferenced(E);
8255f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  return E;
8262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
8272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
8292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
8302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               SourceLocation OpLoc, bool IsArrow,
8312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               const CXXScopeSpec &SS,
832e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                               SourceLocation TemplateKWLoc,
8332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               NamedDecl *FirstQualifierInScope,
8342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                               LookupResult &R,
8352b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                               const TemplateArgumentListInfo *TemplateArgs,
8362b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                               bool SuppressQualifierCheck,
8372b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                               ActOnMemberAccessExtraArgs *ExtraArgs) {
8382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType BaseType = BaseExprType;
8392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsArrow) {
8402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(BaseType->isPointerType());
841e859fbf5938fc0f8ca5aa115c35c66732174f513John McCall    BaseType = BaseType->castAs<PointerType>()->getPointeeType();
8422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
8432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  R.setBaseObjectType(BaseType);
8442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo();
8462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName MemberName = MemberNameInfo.getName();
8472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation MemberLoc = MemberNameInfo.getLoc();
8482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.isAmbiguous())
8502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
8512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.empty()) {
8532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Rederive where we looked up.
8542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    DeclContext *DC = (SS.isSet()
8552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       ? computeDeclContext(SS, false)
8562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       : BaseType->getAs<RecordType>()->getDecl());
8572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8582b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain    if (ExtraArgs) {
8592b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      ExprResult RetryExpr;
8602b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      if (!IsArrow && BaseExpr) {
861111263cf8d829f5a3d0a3c9164f65c1c8223ac7eKaelyn Uhrain        SFINAETrap Trap(*this, true);
8622b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        ParsedType ObjectType;
8632b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        bool MayBePseudoDestructor = false;
8642b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        RetryExpr = ActOnStartCXXMemberReference(getCurScope(), BaseExpr,
8652b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                                                 OpLoc, tok::arrow, ObjectType,
8662b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                                                 MayBePseudoDestructor);
8672b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        if (RetryExpr.isUsable() && !Trap.hasErrorOccurred()) {
8682b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain          CXXScopeSpec TempSS(SS);
8692b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain          RetryExpr = ActOnMemberAccessExpr(
8702b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain              ExtraArgs->S, RetryExpr.get(), OpLoc, tok::arrow, TempSS,
8712b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain              TemplateKWLoc, ExtraArgs->Id, ExtraArgs->ObjCImpDecl,
8722b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain              ExtraArgs->HasTrailingLParen);
8732b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        }
8742b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        if (Trap.hasErrorOccurred())
8752b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain          RetryExpr = ExprError();
8762b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      }
8772b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      if (RetryExpr.isUsable()) {
8782b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        Diag(OpLoc, diag::err_no_member_overloaded_arrow)
8792b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain          << MemberName << DC << FixItHint::CreateReplacement(OpLoc, "->");
8802b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain        return RetryExpr;
8812b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain      }
8822b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain    }
8832b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain
8842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(R.getNameLoc(), diag::err_no_member)
8852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << MemberName << DC
8862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange());
8872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
8882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
8892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
8902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Diagnose lookups that find only declarations from a non-base
8912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // type.  This is possible for either qualified lookups (which may
8922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // have been qualified with an unrelated type) or implicit member
8932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // expressions (which were found with unqualified lookup and thus
8942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // may have come from an enclosing scope).  Note that it's okay for
8952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // lookup to find declarations from a non-base type as long as those
8962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // aren't the ones picked by overload resolution.
8972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if ((SS.isSet() || !BaseExpr ||
8982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor       (isa<CXXThisExpr>(BaseExpr) &&
8992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        cast<CXXThisExpr>(BaseExpr)->isImplicit())) &&
9002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      !SuppressQualifierCheck &&
9012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R))
9022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
903d12505054130b24f7696440efdbd1aa660feb6f3Fariborz Jahanian
9042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Construct an unresolved result if we in fact got an unresolved
9052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // result.
9062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (R.isOverloadedResult() || R.isUnresolvableResult()) {
9072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Suppress any lookup-related diagnostics; we'll do these when we
9082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // pick a member.
9092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    R.suppressDiagnostics();
9102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    UnresolvedMemberExpr *MemExpr
9122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(),
9132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     BaseExpr, BaseExprType,
9142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     IsArrow, OpLoc,
9152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     SS.getWithLocInContext(Context),
916e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                     TemplateKWLoc, MemberNameInfo,
9172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     TemplateArgs, R.begin(), R.end());
9182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned(MemExpr);
9202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(R.isSingleResult());
9232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclAccessPair FoundDecl = R.begin().getPair();
9242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  NamedDecl *MemberDecl = R.getFoundDecl();
9252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: diagnose the presence of template arguments now.
9272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If the decl being referenced had an error, return an error for this
9292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // sub-expr without emitting another error, in order to avoid cascading
9302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // error cases.
9312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (MemberDecl->isInvalidDecl())
9322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
9332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle the implicit-member-access case.
9352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!BaseExpr) {
9362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If this is not an instance member, convert to a non-member access.
9372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!MemberDecl->isCXXInstanceMember())
9382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl);
9392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SourceLocation Loc = R.getNameLoc();
9412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (SS.getRange().isValid())
9422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Loc = SS.getRange().getBegin();
94372899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    CheckCXXThisCapture(Loc);
9442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true);
9452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool ShouldCheckUse = true;
9482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) {
9492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Don't diagnose the use of a virtual member function unless it's
9502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // explicitly qualified.
9512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (MD->isVirtual() && !SS.isSet())
9522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ShouldCheckUse = false;
9532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Check the use of this member.
9562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) {
9572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Owned(BaseExpr);
9582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
9592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl))
9622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow,
9632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                   SS, FD, FoundDecl, MemberNameInfo);
9642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl))
9662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // We may have found a field within an anonymous union or struct
9672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // (C++ [class.union]).
9682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD,
9692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                    BaseExpr, OpLoc);
9702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) {
9725f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
9735f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 TemplateKWLoc, Var, FoundDecl, MemberNameInfo,
9742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 Var->getType().getNonReferenceType(),
9752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 VK_LValue, OK_Ordinary));
9762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) {
9792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprValueKind valueKind;
9802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType type;
9812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (MemberFn->isInstance()) {
9822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      valueKind = VK_RValue;
9832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      type = Context.BoundMemberTy;
9842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } else {
9852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      valueKind = VK_LValue;
9862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      type = MemberFn->getType();
9872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
9882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9895f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
9905f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 TemplateKWLoc, MemberFn, FoundDecl,
9915f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 MemberNameInfo, type, valueKind,
9925f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 OK_Ordinary));
9932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
9942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?");
9952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
9962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) {
9975f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
9985f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman                                 TemplateKWLoc, Enum, FoundDecl, MemberNameInfo,
9992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 Enum->getType(), VK_RValue, OK_Ordinary));
10002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
10012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Owned(BaseExpr);
10032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // We found something that we didn't expect. Complain.
10052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (isa<TypeDecl>(MemberDecl))
10062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(MemberLoc, diag::err_typecheck_member_reference_type)
10072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << MemberName << BaseType << int(IsArrow);
10082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  else
10092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(MemberLoc, diag::err_typecheck_member_reference_unknown)
10102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      << MemberName << BaseType << int(IsArrow);
10112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Diag(MemberDecl->getLocation(), diag::note_member_declared_here)
10132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    << MemberName;
10142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  R.suppressDiagnostics();
10152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return ExprError();
10162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
10172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Given that normal member access failed on the given expression,
10192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// and given that the expression's type involves builtin-id or
10202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// builtin-Class, decide whether substituting in the redefinition
10212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// types would be profitable.  The redefinition type is whatever
10222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// this translation unit tried to typedef to id/Class;  we store
10232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// it to the side and then re-use it in places like this.
10242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
10252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ObjCObjectPointerType *opty
10262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    = base.get()->getType()->getAs<ObjCObjectPointerType>();
10272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!opty) return false;
10282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ObjCObjectType *ty = opty->getObjectType();
10302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType redef;
10322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (ty->isObjCId()) {
103301a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor    redef = S.Context.getObjCIdRedefinitionType();
10342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else if (ty->isObjCClass()) {
103501a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor    redef = S.Context.getObjCClassRedefinitionType();
10362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
10372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
10382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
10392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Do the substitution as long as the redefinition type isn't just a
10412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // possibly-qualified pointer to builtin-id or builtin-Class again.
10422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  opty = redef->getAs<ObjCObjectPointerType>();
10432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (opty && !opty->getObjectType()->getInterface() != 0)
10442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return false;
10452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  base = S.ImpCastExprToType(base.take(), redef, CK_BitCast);
10472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return true;
10482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
10492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10506dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool isRecordType(QualType T) {
10516dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return T->isRecordType();
10526dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
10536dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool isPointerToRecordType(QualType T) {
10546dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (const PointerType *PT = T->getAs<PointerType>())
10556dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return PT->getPointeeType()->isRecordType();
10566dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  return false;
10576dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall}
10586dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall
1059cda80f83ec59293222fe8b92617c8e9f41725f76Richard Smith/// Perform conversions on the LHS of a member access expression.
1060cda80f83ec59293222fe8b92617c8e9f41725f76Richard SmithExprResult
1061cda80f83ec59293222fe8b92617c8e9f41725f76Richard SmithSema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) {
1062059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman  if (IsArrow && !Base->getType()->isFunctionType())
1063059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman    return DefaultFunctionArrayLvalueConversion(Base);
1064cda80f83ec59293222fe8b92617c8e9f41725f76Richard Smith
1065059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman  return CheckPlaceholderExpr(Base);
1066cda80f83ec59293222fe8b92617c8e9f41725f76Richard Smith}
1067cda80f83ec59293222fe8b92617c8e9f41725f76Richard Smith
10682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Look up the given member of the given non-type-dependent
10692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression.  This can return in one of two ways:
10702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///  * If it returns a sentinel null-but-valid result, the caller will
10712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///    assume that lookup was performed and the results written into
10722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///    the provided structure.  It will take over from there.
10732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///  * Otherwise, the returned expression will be produced in place of
10742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///    an ordinary member expression.
10752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///
10762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The ObjCImpDecl bit is a gross hack that will need to be properly
10772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// fixed for ObjC++.
10782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
10792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
10802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       bool &IsArrow, SourceLocation OpLoc,
10812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       CXXScopeSpec &SS,
10822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       Decl *ObjCImpDecl, bool HasTemplateArgs) {
10832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(BaseExpr.get() && "no base expression");
10842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Perform default conversions.
1086cda80f83ec59293222fe8b92617c8e9f41725f76Richard Smith  BaseExpr = PerformMemberExprBaseConversion(BaseExpr.take(), IsArrow);
10876dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (BaseExpr.isInvalid())
10886dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return ExprError();
10892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType BaseType = BaseExpr.get()->getType();
10912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!BaseType->isDependentType());
10922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName MemberName = R.getLookupName();
10942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation MemberLoc = R.getNameLoc();
10952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
10962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // For later type-checking purposes, turn arrow accesses into dot
10972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // accesses.  The only access type we support that doesn't follow
10982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // the C equivalence "a->b === (*a).b" is ObjC property accesses,
10992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // and those never use arrows, so this is unaffected.
11002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsArrow) {
11012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (const PointerType *Ptr = BaseType->getAs<PointerType>())
11022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      BaseType = Ptr->getPointeeType();
11032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else if (const ObjCObjectPointerType *Ptr
11042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor               = BaseType->getAs<ObjCObjectPointerType>())
11052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      BaseType = Ptr->getPointeeType();
11062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else if (BaseType->isRecordType()) {
11072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Recover from arrow accesses to records, e.g.:
11082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      //   struct MyRecord foo;
11092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      //   foo->bar
11102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // This is actually well-formed in C++ if MyRecord has an
11112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // overloaded operator->, but that should have been dealt with
11122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // by now.
11132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
11142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
11152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << FixItHint::CreateReplacement(OpLoc, ".");
11162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      IsArrow = false;
1117059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman    } else if (BaseType->isFunctionType()) {
11182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      goto fail;
11192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    } else {
11202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
11212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << BaseExpr.get()->getSourceRange();
11222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
11232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
11242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
11252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle field access to simple records.
11272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const RecordType *RTy = BaseType->getAs<RecordType>()) {
11282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(),
11292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 RTy, OpLoc, SS, HasTemplateArgs))
11302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
11312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Returning valid-but-null is how we indicate to the caller that
11332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // the lookup result was filled in.
11342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned((Expr*) 0);
11352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
11362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle ivar access to Objective-C objects.
11382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) {
11395a706dc1b17f875c7fce20f1fbf9ca372be4c331Douglas Gregor    if (!SS.isEmpty() && !SS.isInvalid()) {
1140b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1141b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << 1 << SS.getScopeRep()
1142b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << FixItHint::CreateRemoval(SS.getRange());
1143b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      SS.clear();
1144b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor    }
1145b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor
11462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
11472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // There are three cases for the base type:
11492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    //   - builtin id (qualified or unqualified)
11502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    //   - builtin Class (qualified or unqualified)
11512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    //   - an interface
11522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ObjCInterfaceDecl *IDecl = OTy->getInterface();
11532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IDecl) {
11544e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (getLangOpts().ObjCAutoRefCount &&
11552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          (OTy->isObjCId() || OTy->isObjCClass()))
11562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        goto fail;
11572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // There's an implicit 'isa' ivar on all objects.
11582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // But we only actually find it this way on objects of type 'id',
11592502ec84432fc38db63b7f80d90fabf6ebd83039Eric Christopher      // apparently.
1160556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian      if (OTy->isObjCId() && Member->isStr("isa")) {
1161556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian        Diag(MemberLoc, diag::warn_objc_isa_use);
11622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc,
11632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               Context.getObjCClassType()));
1164556b1d0f3a039a691ed4f6dd91b8587435f30b0bFariborz Jahanian      }
11652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
11672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
11682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                ObjCImpDecl, HasTemplateArgs);
11692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      goto fail;
11702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
1171091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian    else if (Member && Member->isStr("isa")) {
1172091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      // If an ivar is (1) the first ivar in a root class and (2) named `isa`,
1173091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      // then issue the same deprecated warning that id->isa gets.
1174091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      ObjCInterfaceDecl *ClassDeclared = 0;
1175091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      if (ObjCIvarDecl *IV =
1176091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian            IDecl->lookupInstanceVariable(Member, ClassDeclared)) {
1177091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian        if (!ClassDeclared->getSuperClass()
1178091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian            && (*ClassDeclared->ivar_begin()) == IV) {
1179091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian          Diag(MemberLoc, diag::warn_objc_isa_use);
1180091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian          Diag(IV->getLocation(), diag::note_ivar_decl);
1181091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian        }
1182091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian      }
1183091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian    }
1184091005954a2e42e6f699dfef25369b3654397536Fariborz Jahanian
1185d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (RequireCompleteType(OpLoc, BaseType, diag::err_typecheck_incomplete_tag,
1186d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                            BaseExpr.get()))
1187d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor      return ExprError();
1188d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor
11892c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek    ObjCInterfaceDecl *ClassDeclared = 0;
11902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared);
11912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
11922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IV) {
11932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Attempt to correct for typos in ivar names.
1194e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain      DeclFilterCCC<ObjCIvarDecl> Validator;
1195e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain      Validator.IsObjCIvarLookup = IsArrow;
1196e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain      if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
1197e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain                                                 LookupMemberName, NULL, NULL,
119816e46dd0c284296cea819dfbf67942ecef02894dKaelyn Uhrain                                                 Validator, IDecl)) {
1199e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain        IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>();
12002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Diag(R.getNameLoc(),
12012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor             diag::err_typecheck_member_reference_ivar_suggest)
12022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << IDecl->getDeclName() << MemberName << IV->getDeclName()
12032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << FixItHint::CreateReplacement(R.getNameLoc(),
12042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                          IV->getNameAsString());
12052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Diag(IV->getLocation(), diag::note_previous_decl)
12062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << IV->getDeclName();
12072c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek
12082c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        // Figure out the class that declares the ivar.
12092c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        assert(!ClassDeclared);
12102c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        Decl *D = cast<Decl>(IV->getDeclContext());
12112c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        if (ObjCCategoryDecl *CAT = dyn_cast<ObjCCategoryDecl>(D))
12122c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek          D = CAT->getClassInterface();
12132c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek        ClassDeclared = cast<ObjCInterfaceDecl>(D);
12142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      } else {
12156326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian        if (IsArrow && IDecl->FindPropertyDeclaration(Member)) {
12166326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          Diag(MemberLoc,
12176326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          diag::err_property_found_suggest)
12186326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          << Member << BaseExpr.get()->getType()
12196326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          << FixItHint::CreateReplacement(OpLoc, ".");
12206326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian          return ExprError();
12216326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian        }
12222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Diag(MemberLoc, diag::err_typecheck_member_reference_ivar)
12242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << IDecl->getDeclName() << MemberName
12252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << BaseExpr.get()->getSourceRange();
12262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return ExprError();
12272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
12282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
12292c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek
12302c085ede8d0186b6b91d553fdb0d95798501c20fTed Kremenek    assert(ClassDeclared);
12312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // If the decl being referenced had an error, return an error for this
12332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // sub-expr without emitting another error, in order to avoid cascading
12342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // error cases.
12352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IV->isInvalidDecl())
12362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
12372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Check whether we can reference this field.
12392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (DiagnoseUseOfDecl(IV, MemberLoc))
12402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
12412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IV->getAccessControl() != ObjCIvarDecl::Public &&
12422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        IV->getAccessControl() != ObjCIvarDecl::Package) {
12432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCInterfaceDecl *ClassOfMethodDecl = 0;
12442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ObjCMethodDecl *MD = getCurMethodDecl())
12452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        ClassOfMethodDecl =  MD->getClassInterface();
12462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      else if (ObjCImpDecl && getCurFunctionDecl()) {
12472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // Case of a c-function declared inside an objc implementation.
12482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // FIXME: For a c-style function nested inside an objc implementation
12492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // class, there is no implementation context available, so we pass
12502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // down the context as argument to this routine. Ideally, this context
12512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // need be passed down in the AST node and somehow calculated from the
12522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // AST for a function decl.
12532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ObjCImplementationDecl *IMPD =
12542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor              dyn_cast<ObjCImplementationDecl>(ObjCImpDecl))
12552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          ClassOfMethodDecl = IMPD->getClassInterface();
12562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        else if (ObjCCategoryImplDecl* CatImplClass =
12572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                   dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl))
12582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          ClassOfMethodDecl = CatImplClass->getClassInterface();
12592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
12604e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!getLangOpts().DebuggerSupport) {
1261458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian        if (IV->getAccessControl() == ObjCIvarDecl::Private) {
1262458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian          if (!declaresSameEntity(ClassDeclared, IDecl) ||
1263458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian              !declaresSameEntity(ClassOfMethodDecl, ClassDeclared))
1264458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian            Diag(MemberLoc, diag::error_private_ivar_access)
1265458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian              << IV->getDeclName();
1266458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian        } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl))
1267458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian          // @protected
1268458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian          Diag(MemberLoc, diag::error_protected_ivar_access)
12692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            << IV->getDeclName();
1270458a7fbe7349c7906fa4ca96c09e6ceb02aa8ea7Fariborz Jahanian      }
12712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
1272b25466e8b33285a13d0303461db37e903ec505c1Fariborz Jahanian    bool warn = true;
12734e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjCAutoRefCount) {
12742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts();
12752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp))
12762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (UO->getOpcode() == UO_Deref)
12772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          BaseExp = UO->getSubExpr()->IgnoreParenCasts();
12782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp))
1280b25466e8b33285a13d0303461db37e903ec505c1Fariborz Jahanian        if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
12812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          Diag(DE->getLocation(), diag::error_arc_weak_ivar_access);
1282b25466e8b33285a13d0303461db37e903ec505c1Fariborz Jahanian          warn = false;
1283b25466e8b33285a13d0303461db37e903ec505c1Fariborz Jahanian        }
12842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
1285ed6662dcd95a3349b6c94bb0df2ff4378a029df8Fariborz Jahanian    if (warn) {
1286cff863fd803874d251ef8725d5c08dec90924627Fariborz Jahanian      if (ObjCMethodDecl *MD = getCurMethodDecl()) {
1287cff863fd803874d251ef8725d5c08dec90924627Fariborz Jahanian        ObjCMethodFamily MF = MD->getMethodFamily();
1288cff863fd803874d251ef8725d5c08dec90924627Fariborz Jahanian        warn = (MF != OMF_init && MF != OMF_dealloc &&
1289cff863fd803874d251ef8725d5c08dec90924627Fariborz Jahanian                MF != OMF_finalize);
1290cff863fd803874d251ef8725d5c08dec90924627Fariborz Jahanian      }
1291cff863fd803874d251ef8725d5c08dec90924627Fariborz Jahanian      if (warn)
1292cff863fd803874d251ef8725d5c08dec90924627Fariborz Jahanian        Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName();
12932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
12942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(),
12952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               MemberLoc, BaseExpr.take(),
12962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               IsArrow));
12972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
12982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
12992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Objective-C property access.
13002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const ObjCObjectPointerType *OPT;
13012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) {
13025a706dc1b17f875c7fce20f1fbf9ca372be4c331Douglas Gregor    if (!SS.isEmpty() && !SS.isInvalid()) {
1303b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1304b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << 0 << SS.getScopeRep()
1305b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor        << FixItHint::CreateRemoval(SS.getRange());
1306b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor      SS.clear();
1307b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor    }
1308b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor
13092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // This actually uses the base as an r-value.
13102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseExpr = DefaultLvalueConversion(BaseExpr.take());
13112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseExpr.isInvalid())
13122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
13132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType()));
13152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
13172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    const ObjCObjectType *OT = OPT->getObjectType();
13192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // id, with and without qualifiers.
13212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (OT->isObjCId()) {
13222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Check protocols on qualified interfaces.
13232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
13242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) {
13252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) {
13262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          // Check the use of this declaration
13272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          if (DiagnoseUseOfDecl(PD, MemberLoc))
13282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            return ExprError();
13292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1330e859fbf5938fc0f8ca5aa115c35c66732174f513John McCall          return Owned(new (Context) ObjCPropertyRefExpr(PD,
1331e859fbf5938fc0f8ca5aa115c35c66732174f513John McCall                                                         Context.PseudoObjectTy,
13322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         VK_LValue,
13332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         OK_ObjCProperty,
13342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         MemberLoc,
13352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         BaseExpr.take()));
13362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        }
13372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) {
13392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          // Check the use of this method.
13402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          if (DiagnoseUseOfDecl(OMD, MemberLoc))
13412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            return ExprError();
13422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          Selector SetterSel =
13432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            SelectorTable::constructSetterName(PP.getIdentifierTable(),
13442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                               PP.getSelectorTable(), Member);
13452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          ObjCMethodDecl *SMD = 0;
13462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0,
13472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                     SetterSel, Context))
13482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor            SMD = dyn_cast<ObjCMethodDecl>(SDecl);
13492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1350e859fbf5938fc0f8ca5aa115c35c66732174f513John McCall          return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD,
1351e859fbf5938fc0f8ca5aa115c35c66732174f513John McCall                                                         Context.PseudoObjectTy,
1352e859fbf5938fc0f8ca5aa115c35c66732174f513John McCall                                                         VK_LValue, OK_ObjCProperty,
13532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                         MemberLoc, BaseExpr.take()));
13542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        }
13552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Use of id.member can only be for a property reference. Do not
13572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // use the 'id' redefinition in this case.
13582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
13592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
13602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                ObjCImpDecl, HasTemplateArgs);
13612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError(Diag(MemberLoc, diag::err_property_not_found)
13632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         << MemberName << BaseType);
13642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
13652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // 'Class', unqualified only.
13672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (OT->isObjCClass()) {
13682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Only works in a method declaration (??!).
13692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCMethodDecl *MD = getCurMethodDecl();
13702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!MD) {
13712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
13722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
13732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  ObjCImpDecl, HasTemplateArgs);
13742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        goto fail;
13762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
13782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Also must look for a getter name which uses property syntax.
13792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
13802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCInterfaceDecl *IFace = MD->getClassInterface();
13812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCMethodDecl *Getter;
13822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if ((Getter = IFace->lookupClassMethod(Sel))) {
13832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // Check the use of this method.
13842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        if (DiagnoseUseOfDecl(Getter, MemberLoc))
13852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          return ExprError();
13862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      } else
13872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Getter = IFace->lookupPrivateMethod(Sel, false);
13882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // If we found a getter then this may be a valid dot-reference, we
13892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // will look for the matching setter, in case it is needed.
13902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Selector SetterSel =
13912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        SelectorTable::constructSetterName(PP.getIdentifierTable(),
13922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                           PP.getSelectorTable(), Member);
13932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
13942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!Setter) {
13952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // If this reference is in an @implementation, also check for 'private'
13962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        // methods.
13972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        Setter = IFace->lookupPrivateMethod(SetterSel, false);
13982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
13992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
14012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return ExprError();
14022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (Getter || Setter) {
14042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
1405e859fbf5938fc0f8ca5aa115c35c66732174f513John McCall                                                       Context.PseudoObjectTy,
1406e859fbf5938fc0f8ca5aa115c35c66732174f513John McCall                                                       VK_LValue, OK_ObjCProperty,
14072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                       MemberLoc, BaseExpr.take()));
14082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      }
14092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
14112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
14122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                ObjCImpDecl, HasTemplateArgs);
14132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError(Diag(MemberLoc, diag::err_property_not_found)
14152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         << MemberName << BaseType);
14162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
14172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // Normal property access.
14196326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian    return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc,
14206326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian                                     MemberName, MemberLoc,
14212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                     SourceLocation(), QualType(), false);
14222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Handle 'field access' to vectors, such as 'V.xx'.
14252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (BaseType->isExtVectorType()) {
14262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // FIXME: this expr should store IsArrow.
14272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
14282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind());
14292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc,
14302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                           Member, MemberLoc);
14312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (ret.isNull())
14322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
14332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(),
14352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                                    *Member, MemberLoc));
14362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Adjust builtin-sel to the appropriate redefinition type if that's
14392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // not just a pointer to builtin-sel again.
14402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsArrow &&
14412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) &&
144201a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor      !Context.getObjCSelRedefinitionType()->isObjCSelType()) {
144301a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor    BaseExpr = ImpCastExprToType(BaseExpr.take(),
144401a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor                                 Context.getObjCSelRedefinitionType(),
14452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 CK_BitCast);
14462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
14472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                            ObjCImpDecl, HasTemplateArgs);
14482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Failure cases.
14512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor fail:
14522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Recover from dot accesses to pointers, e.g.:
14542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   type *foo;
14552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   foo.bar
14562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This is actually well-formed in two cases:
14572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   - 'type' is an Objective C type
14582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   - 'bar' is a pseudo-destructor name which happens to refer to
14592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //     the appropriate pointer type
14602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
14612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (!IsArrow && Ptr->getPointeeType()->isRecordType() &&
14622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        MemberName.getNameKind() != DeclarationName::CXXDestructorName) {
14632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
14642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
14652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          << FixItHint::CreateReplacement(OpLoc, "->");
14662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // Recurse as an -> access.
14682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      IsArrow = true;
14692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
14702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              ObjCImpDecl, HasTemplateArgs);
14712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
14722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If the user is trying to apply -> or . to a function name, it's probably
14752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // because they forgot parentheses to call that function.
14766dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall  if (tryToRecoverWithCall(BaseExpr,
14776dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                           PDiag(diag::err_member_reference_needs_call),
14786dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                           /*complain*/ false,
1479059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman                           IsArrow ? &isPointerToRecordType : &isRecordType)) {
14806dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    if (BaseExpr.isInvalid())
14812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
14826dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take());
14836dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall    return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
14846dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall                            ObjCImpDecl, HasTemplateArgs);
14852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
14862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14877d90fe5a941efc106237d23badec816ed65e267fMatt Beaumont-Gay  Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
148873664a4e5aee9216c37bd123aa002430ccb5431dMatt Beaumont-Gay    << BaseType << BaseExpr.get()->getSourceRange() << MemberLoc;
14892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return ExprError();
14912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
14922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
14932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The main callback when the parser finds something like
14942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   expression . [nested-name-specifier] identifier
14952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   expression -> [nested-name-specifier] identifier
14962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// where 'identifier' encompasses a fairly broad spectrum of
14972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// possibilities, including destructor and operator references.
14982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///
14992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param OpKind either tok::arrow or tok::period
15002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param HasTrailingLParen whether the next token is '(', which
15012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   is used to diagnose mis-uses of special members that can
15022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor///   only be called
1503699c9044c7d53a2774d0dd261a6901dd2c4a545fJames Dennett/// \param ObjCImpDecl the current Objective-C \@implementation
1504699c9044c7d53a2774d0dd261a6901dd2c4a545fJames Dennett///   decl; this is an ugly hack around the fact that Objective-C
1505699c9044c7d53a2774d0dd261a6901dd2c4a545fJames Dennett///   \@implementations aren't properly put in the context chain
15062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base,
15072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       SourceLocation OpLoc,
15082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       tok::TokenKind OpKind,
15092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       CXXScopeSpec &SS,
1510e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                       SourceLocation TemplateKWLoc,
15112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       UnqualifiedId &Id,
15122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       Decl *ObjCImpDecl,
15132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                       bool HasTrailingLParen) {
15142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (SS.isSet() && SS.isInvalid())
15152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
15162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Warn about the explicit constructor calls Microsoft extension.
15184e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().MicrosoftExt &&
15192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Id.getKind() == UnqualifiedId::IK_ConstructorName)
15202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Diag(Id.getSourceRange().getBegin(),
15212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor         diag::ext_ms_explicit_constructor_call);
15222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  TemplateArgumentListInfo TemplateArgsBuffer;
15242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Decompose the name into its component parts.
15262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationNameInfo NameInfo;
15272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  const TemplateArgumentListInfo *TemplateArgs;
15282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DecomposeUnqualifiedId(Id, TemplateArgsBuffer,
15292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                         NameInfo, TemplateArgs);
15302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  DeclarationName Name = NameInfo.getName();
15322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  bool IsArrow = (OpKind == tok::arrow);
15332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  NamedDecl *FirstQualifierInScope
15352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S,
15362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                       static_cast<NestedNameSpecifier*>(SS.getScopeRep())));
15372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // This is a postfix expression, so get rid of ParenListExprs.
15392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
15402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Result.isInvalid()) return ExprError();
15412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Base = Result.take();
15422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Base->getType()->isDependentType() || Name.isDependentName() ||
15442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      isDependentScopeSpecifier(SS)) {
15452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Result = ActOnDependentMemberExpr(Base, Base->getType(),
15462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      IsArrow, OpLoc,
1547e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      SS, TemplateKWLoc, FirstQualifierInScope,
15482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                      NameInfo, TemplateArgs);
15492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
15502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    LookupResult R(*this, NameInfo, LookupMemberName);
15512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    ExprResult BaseResult = Owned(Base);
15522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
15532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              SS, ObjCImpDecl, TemplateArgs != 0);
15542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseResult.isInvalid())
15552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
15562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Base = BaseResult.take();
15572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.isInvalid()) {
15592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Owned(Base);
15602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      return ExprError();
15612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
15622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Result.get()) {
15642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // The only way a reference to a destructor can be used is to
15652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // immediately call it, which falls into this case.  If the
15662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // next token is not a '(', produce a diagnostic and build the
15672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      // call now.
15682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      if (!HasTrailingLParen &&
15692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor          Id.getKind() == UnqualifiedId::IK_DestructorName)
15702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor        return DiagnoseDtorReference(NameInfo.getLoc(), Result.get());
15712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15723fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer      return Result;
15732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    }
15742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15752b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain    ActOnMemberAccessExtraArgs ExtraArgs = {S, Id, ObjCImpDecl, HasTrailingLParen};
15762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Result = BuildMemberReferenceExpr(Base, Base->getType(),
1577e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                      OpLoc, IsArrow, SS, TemplateKWLoc,
15782b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                                      FirstQualifierInScope, R, TemplateArgs,
15792b90f7637e846ea555dedac14e7f5065d85c5d3bKaelyn Uhrain                                      false, &ExtraArgs);
15802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
15812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15823fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  return Result;
15832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
15842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
15852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic ExprResult
15862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorBuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
15872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const CXXScopeSpec &SS, FieldDecl *Field,
15882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        DeclAccessPair FoundDecl,
15892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                        const DeclarationNameInfo &MemberNameInfo) {
15902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // x.a is an l-value if 'a' has a reference type. Otherwise:
15912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // x.a is an l-value/x-value/pr-value if the base is (and note
15922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   that *x is always an l-value), except that if the base isn't
15932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  //   an ordinary object then we must have an rvalue.
15942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprValueKind VK = VK_LValue;
15952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprObjectKind OK = OK_Ordinary;
15962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (!IsArrow) {
15972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (BaseExpr->getObjectKind() == OK_Ordinary)
15982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      VK = BaseExpr->getValueKind();
15992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    else
16002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      VK = VK_RValue;
16012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
16022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (VK != VK_RValue && Field->isBitField())
16032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    OK = OK_BitField;
16042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref]
16062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  QualType MemberType = Field->getType();
16072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) {
16082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    MemberType = Ref->getPointeeType();
16092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    VK = VK_LValue;
16102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  } else {
16112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    QualType BaseType = BaseExpr->getType();
16122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType();
16132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Qualifiers BaseQuals = BaseType.getQualifiers();
16152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // GC attributes are never picked up by members.
16172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    BaseQuals.removeObjCGCAttr();
16182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // CVR attributes from the base are picked up by members,
16202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // except that 'mutable' members don't pick up 'const'.
16212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Field->isMutable()) BaseQuals.removeConst();
16222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Qualifiers MemberQuals
16242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    = S.Context.getCanonicalType(MemberType).getQualifiers();
16252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    // TR 18037 does not allow fields to be declared with address spaces.
16272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    assert(!MemberQuals.hasAddressSpace());
16282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    Qualifiers Combined = BaseQuals + MemberQuals;
16302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (Combined != MemberQuals)
16312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      MemberType = S.Context.getQualifiedType(MemberType, Combined);
16322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
16332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
1634f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper  S.UnusedPrivateFields.remove(Field);
1635f8cc02e50553b5c3bc6570bff0c47ac7db85fe8dDaniel Jasper
16362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  ExprResult Base =
16372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
16382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  FoundDecl, Field);
16392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (Base.isInvalid())
16402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return ExprError();
16415f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  return S.Owned(BuildMemberExpr(S, S.Context, Base.take(), IsArrow, SS,
1642e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                 /*TemplateKWLoc=*/SourceLocation(),
16432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 Field, FoundDecl, MemberNameInfo,
16442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                 MemberType, VK, OK));
16452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
16462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Builds an implicit member access expression.  The current context
16482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// is known to be an instance method, and the given unqualified lookup
16492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// set is known to contain only instance members, at least one of which
16502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// is from an appropriate type.
16512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult
16522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
1653e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                              SourceLocation TemplateKWLoc,
16542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              LookupResult &R,
16552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              const TemplateArgumentListInfo *TemplateArgs,
16562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                              bool IsKnownInstance) {
16572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!R.empty() && !R.isAmbiguous());
16582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  SourceLocation loc = R.getNameLoc();
16602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // We may have found a field within an anonymous union or struct
16622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // (C++ [class.union]).
16632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // FIXME: template-ids inside anonymous structs?
16642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>())
16652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD);
16662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // If this is known to be an instance access, go ahead and build an
16682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // implicit 'this' expression now.
16692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  // 'this' expression now.
1670341350ee62abd1ad818e1e3d926cd718960e439bDouglas Gregor  QualType ThisTy = getCurrentThisType();
16712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'");
16722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  Expr *baseExpr = 0; // null signifies implicit access
16742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  if (IsKnownInstance) {
16752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    SourceLocation Loc = R.getNameLoc();
16762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    if (SS.getRange().isValid())
16772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor      Loc = SS.getRange().getBegin();
167872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman    CheckCXXThisCapture(Loc);
16792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor    baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true);
16802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  }
16812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor
16822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor  return BuildMemberReferenceExpr(baseExpr, ThisTy,
16832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  /*OpLoc*/ SourceLocation(),
16842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  /*IsArrow*/ true,
1685e4b92761b43ced611c417ae478568610f1ad7b1eAbramo Bagnara                                  SS, TemplateKWLoc,
16862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  /*FirstQualifierInScope*/ 0,
16872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor                                  R, TemplateArgs);
16882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}
1689