SemaExprMember.cpp revision 9bc291d5c00f47383ce7358e6309abf45324b028
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 779bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman // The reference is an instance data member access, which is allowed 789bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman // because we're in C++11 mode and the context is unevaluated. 799bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman IMA_Field_Uneval_StaticContext, 809bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman 819bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman // The reference is an instance data member access, which may be allowed 829bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman // because we're in C++11 mode and the context may be unevaluated 839bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman // (i.e. the context is PotentiallyPotentiallyEvaluated). 849bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman IMA_Field_PPE_StaticContext, 859bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman 862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor /// All possible referrents are instance members and the current 872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor /// context is not an instance method. 882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IMA_Error_StaticContext, 892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor /// All possible referrents are instance members of an unrelated 912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor /// class. 922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IMA_Error_Unrelated 932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor}; 942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The given lookup names class member(s) and is not being used for 962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// an address-of-member expression. Classify the type of access 972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// according to whether it's possible that this reference names an 989bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman/// instance member. This is best-effort in dependent contexts; it is okay to 992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// conservatively answer "yes", in which case some errors will simply 1002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// not be caught until template-instantiation. 1012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, 1022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Scope *CurScope, 1032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const LookupResult &R) { 1042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(!R.empty() && (*R.begin())->isCXXClassMember()); 1052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); 1072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool isStaticContext = 1092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor (!isa<CXXMethodDecl>(DC) || 1102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor cast<CXXMethodDecl>(DC)->isStatic()); 1112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // C++0x [expr.prim]p4: 1132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Otherwise, if a member-declarator declares a non-static data member 1142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // of a class X, the expression this is a prvalue of type "pointer to X" 1152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // within the optional brace-or-equal-initializer. 1162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (CurScope->getFlags() & Scope::ThisScope) 1172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor isStaticContext = false; 1182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (R.isUnresolvableResult()) 1202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; 1212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Collect all the declaring classes of instance members we find. 1232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool hasNonInstance = false; 1249bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman bool isField = false; 1252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; 1262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { 1272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NamedDecl *D = *I; 1282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (D->isCXXInstanceMember()) { 1302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (dyn_cast<FieldDecl>(D)) 1319bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman isField = true; 1322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); 1342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Classes.insert(R->getCanonicalDecl()); 1352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 1362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor else 1372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor hasNonInstance = true; 1382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 1392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If we didn't find any instance members, it can't be an implicit 1412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // member reference. 1422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Classes.empty()) 1432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return IMA_Static; 1442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If the current context is not an instance method, it can't be 1462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // an implicit member reference. 1472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (isStaticContext) { 1482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (hasNonInstance) 1492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return IMA_Mixed_StaticContext; 1502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1519bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman if (SemaRef.getLangOptions().CPlusPlus0x && isField) { 152f6702a3927147655206ae729a84339c4fda4c651Richard Smith // C++11 [expr.prim.general]p12: 1532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // An id-expression that denotes a non-static data member or non-static 1542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // member function of a class can only be used: 1552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // (...) 1562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // - if that id-expression denotes a non-static data member and it 1572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // appears in an unevaluated operand. 1582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const Sema::ExpressionEvaluationContextRecord& record 1592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor = SemaRef.ExprEvalContexts.back(); 1609bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman if (record.Context == Sema::Unevaluated) 1619bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman return IMA_Field_Uneval_StaticContext; 1629bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman if (record.Context == Sema::PotentiallyPotentiallyEvaluated) 1639bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman return IMA_Field_PPE_StaticContext; 1642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 1652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return IMA_Error_StaticContext; 1672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 1682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CXXRecordDecl *contextClass; 1702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) 1712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor contextClass = MD->getParent()->getCanonicalDecl(); 1722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor else 1732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor contextClass = cast<CXXRecordDecl>(DC); 1742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // [class.mfct.non-static]p3: 1762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // ...is used in the body of a non-static member function of class X, 1772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // if name lookup (3.4.1) resolves the name in the id-expression to a 1782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // non-static non-type member of some class C [...] 1792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // ...if C is not X or a base class of X, the class member access expression 1802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // is ill-formed. 1812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (R.getNamingClass() && 1822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor contextClass != R.getNamingClass()->getCanonicalDecl() && 1832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor contextClass->isProvablyNotDerivedFrom(R.getNamingClass())) 1842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); 1852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If we can prove that the current context is unrelated to all the 1872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // declaring classes, it can't be an implicit member reference (in 1882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // which case it's an error if any of those members are selected). 1892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IsProvablyNotDerivedFrom(SemaRef, contextClass, Classes)) 1902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); 1912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return (hasNonInstance ? IMA_Mixed : IMA_Instance); 1932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 1942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Diagnose a reference to a field with no object available. 1962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic void DiagnoseInstanceReference(Sema &SemaRef, 1972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const CXXScopeSpec &SS, 1982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NamedDecl *rep, 1999bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman const DeclarationNameInfo &nameInfo, 2009bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman bool DelayPPEDiag = false) { 2012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation Loc = nameInfo.getLoc(); 2022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceRange Range(Loc); 2032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); 2042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (isa<FieldDecl>(rep) || isa<IndirectFieldDecl>(rep)) { 2062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { 2072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (MD->isStatic()) { 2082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // "invalid use of member 'x' in static member function" 2099bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman if (DelayPPEDiag) 2109bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman SemaRef.ExprEvalContexts.back().addDiagnostic(Loc, 2119bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman SemaRef.PDiag(diag::err_invalid_member_use_in_static_method) 2129bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman << Range << nameInfo.getName()); 2139bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman else 2149bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) 2159bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman << Range << nameInfo.getName(); 2162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return; 2172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 2182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 2199bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman 2209bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman if (DelayPPEDiag) 2219bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman SemaRef.ExprEvalContexts.back().addDiagnostic(Loc, 2229bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman SemaRef.PDiag(diag::err_invalid_non_static_member_use) 2239bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman << nameInfo.getName() << Range); 2249bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman else 2259bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) 2269bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman << nameInfo.getName() << Range; 2272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return; 2282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 2299bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman 2309bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman assert(!DelayPPEDiag && "Only need to delay diagnostic for fields"); 2319bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman 2322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; 2332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 2342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Builds an expression which might be an implicit member expression. 2362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult 2372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, 2382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor LookupResult &R, 2392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const TemplateArgumentListInfo *TemplateArgs) { 2402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) { 2412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor case IMA_Instance: 2422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); 2432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor case IMA_Mixed: 2452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor case IMA_Mixed_Unrelated: 2462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor case IMA_Unresolved: 2472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); 2482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2499bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman case IMA_Field_PPE_StaticContext: 2509bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), 2519bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman R.getLookupNameInfo(), /*DelayPPEDiag*/true); 2529bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman // FALL-THROUGH 2539bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman 2542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor case IMA_Static: 2552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor case IMA_Mixed_StaticContext: 2562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor case IMA_Unresolved_StaticContext: 2579bc291d5c00f47383ce7358e6309abf45324b028Eli Friedman case IMA_Field_Uneval_StaticContext: 2582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (TemplateArgs) 2592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); 2602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildDeclarationNameExpr(SS, R, false); 2612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor case IMA_Error_StaticContext: 2632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor case IMA_Error_Unrelated: 2642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), 2652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor R.getLookupNameInfo()); 2662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 2672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 2682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor llvm_unreachable("unexpected instance member access kind"); 2702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 2712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Check an ext-vector component access expression. 2732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// 2742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// VK should be set in advance to the value kind of the base 2752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression. 2762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic QualType 2772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorCheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, 2782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation OpLoc, const IdentifierInfo *CompName, 2792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation CompLoc) { 2802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, 2812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // see FIXME there. 2822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // 2832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // FIXME: This logic can be greatly simplified by splitting it along 2842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // halving/not halving and reworking the component checking. 2852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); 2862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // The vector accessor can't exceed the number of elements. 2882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const char *compStr = CompName->getNameStart(); 2892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // This flag determines whether or not the component is one of the four 2912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // special names that indicate a subset of exactly half the elements are 2922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // to be selected. 2932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool HalvingSwizzle = false; 2942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // This flag determines whether or not CompName has an 's' char prefix, 2962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // indicating that it is a string of hex values to be used as vector indices. 2972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool HexSwizzle = *compStr == 's' || *compStr == 'S'; 2982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 2992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool HasRepeated = false; 3002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool HasIndex[16] = {}; 3012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor int Idx; 3032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Check that we've found one of the special components, or that the component 3052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // names must come from the same set. 3062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || 3072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { 3082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor HalvingSwizzle = true; 3092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else if (!HexSwizzle && 3102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { 3112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor do { 3122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (HasIndex[Idx]) HasRepeated = true; 3132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor HasIndex[Idx] = true; 3142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor compStr++; 3152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); 3162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 3172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (HexSwizzle) compStr++; 3182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { 3192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (HasIndex[Idx]) HasRepeated = true; 3202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor HasIndex[Idx] = true; 3212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor compStr++; 3222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 3232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 3242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!HalvingSwizzle && *compStr) { 3262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // We didn't get to the end of the string. This means the component names 3272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // didn't come from the same set *or* we encountered an illegal name. 3282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) 3295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner << StringRef(compStr, 1) << SourceRange(CompLoc); 3302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return QualType(); 3312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 3322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Ensure no component accessor exceeds the width of the vector type it 3342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // operates on. 3352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!HalvingSwizzle) { 3362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor compStr = CompName->getNameStart(); 3372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (HexSwizzle) 3392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor compStr++; 3402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor while (*compStr) { 3422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!vecType->isAccessorWithinNumElements(*compStr++)) { 3432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) 3442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << baseType << SourceRange(CompLoc); 3452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return QualType(); 3462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 3472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 3482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 3492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // The component accessor looks fine - now we need to compute the actual type. 3512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // The vector type is implied by the component accessor. For example, 3522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. 3532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // vec4.s0 is a float, vec4.s23 is a vec3, etc. 3542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. 3552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 3562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor : CompName->getLength(); 3572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (HexSwizzle) 3582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CompSize--; 3592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (CompSize == 1) 3612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return vecType->getElementType(); 3622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (HasRepeated) VK = VK_RValue; 3642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); 3662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Now look up the TypeDefDecl from the vector type. Without this, 3672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // diagostics look bad. We want extended vector types to appear built-in. 368d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor for (Sema::ExtVectorDeclsType::iterator 369d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor I = S.ExtVectorDecls.begin(S.ExternalSource), 370d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor E = S.ExtVectorDecls.end(); 371d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor I != E; ++I) { 372d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor if ((*I)->getUnderlyingType() == VT) 373d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor return S.Context.getTypedefType(*I); 3742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 375d58a0a55e64a7c410a80e9d6dcd899e61e99cc4dDouglas Gregor 3762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return VT; // should never get here (a typedef type should always be found). 3772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 3782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, 3802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IdentifierInfo *Member, 3812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const Selector &Sel, 3822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ASTContext &Context) { 3832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Member) 3842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) 3852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return PD; 3862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) 3872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return OMD; 3882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), 3902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor E = PDecl->protocol_end(); I != E; ++I) { 3912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, 3922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Context)) 3932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return D; 3942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 3952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return 0; 3962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 3972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 3982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, 3992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IdentifierInfo *Member, 4002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const Selector &Sel, 4012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ASTContext &Context) { 4022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Check protocols on qualified interfaces. 4032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Decl *GDecl = 0; 4042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), 4052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor E = QIdTy->qual_end(); I != E; ++I) { 4062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Member) 4072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { 4082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor GDecl = PD; 4092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor break; 4102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 4112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Also must look for a getter or setter name which uses property syntax. 4122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { 4132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor GDecl = OMD; 4142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor break; 4152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 4162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 4172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!GDecl) { 4182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), 4192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor E = QIdTy->qual_end(); I != E; ++I) { 4202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Search in the protocol-qualifier list of current protocol. 4212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, 4222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Context); 4232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (GDecl) 4242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return GDecl; 4252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 4262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 4272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return GDecl; 4282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 4292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 4302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult 4312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, 4322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool IsArrow, SourceLocation OpLoc, 4332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const CXXScopeSpec &SS, 4342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NamedDecl *FirstQualifierInScope, 4352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const DeclarationNameInfo &NameInfo, 4362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const TemplateArgumentListInfo *TemplateArgs) { 4372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Even in dependent contexts, try to diagnose base expressions with 4382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // obviously wrong types, e.g.: 4392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // 4402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // T* t; 4412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // t.f; 4422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // 4432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // In Obj-C++, however, the above expression is valid, since it could be 4442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // accessing the 'f' property if T is an Obj-C interface. The extra check 4452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // allows this, while still reporting an error if T is a struct pointer. 4462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!IsArrow) { 4472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const PointerType *PT = BaseType->getAs<PointerType>(); 4482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (PT && (!getLangOptions().ObjC1 || 4492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor PT->getPointeeType()->isRecordType())) { 4502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(BaseExpr && "cannot happen with implicit member accesses"); 4512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) 4522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << BaseType << BaseExpr->getSourceRange(); 4532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 4542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 4552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 4562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 4572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(BaseType->isDependentType() || 4582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NameInfo.getName().isDependentName() || 4592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor isDependentScopeSpecifier(SS)); 4602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 4612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr 4622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // must have pointer type, and the accessed type is the pointee. 4632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, 4642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IsArrow, OpLoc, 4652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SS.getWithLocInContext(Context), 4662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor FirstQualifierInScope, 4672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NameInfo, TemplateArgs)); 4682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 4692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 4702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// We know that the given qualified member reference points only to 4712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// declarations which do not belong to the static type of the base 4722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression. Diagnose the problem. 4732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic void DiagnoseQualifiedMemberReference(Sema &SemaRef, 4742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Expr *BaseExpr, 4752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType BaseType, 4762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const CXXScopeSpec &SS, 4772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NamedDecl *rep, 4782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const DeclarationNameInfo &nameInfo) { 4792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If this is an implicit member access, use a different set of 4802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // diagnostics. 4812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!BaseExpr) 4822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return DiagnoseInstanceReference(SemaRef, SS, rep, nameInfo); 4832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 4842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) 4852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << SS.getRange() << rep << BaseType; 4862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 4872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 4882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// Check whether the declarations we found through a nested-name 4892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// specifier in a member expression are actually members of the base 4902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// type. The restriction here is: 4912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// 4922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// C++ [expr.ref]p2: 4932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// ... In these cases, the id-expression shall name a 4942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// member of the class or of one of its base classes. 4952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// 4962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// So it's perfectly legitimate for the nested-name specifier to name 4972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// an unrelated class, and for us to find an overload set including 4982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// decls from classes which are not superclasses, as long as the decl 4992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor// we actually pick through overload resolution is from a superclass. 5002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorbool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, 5012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType BaseType, 5022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const CXXScopeSpec &SS, 5032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const LookupResult &R) { 5042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const RecordType *BaseRT = BaseType->getAs<RecordType>(); 5052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!BaseRT) { 5062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // We can't check this yet because the base type is still 5072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // dependent. 5082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(BaseType->isDependentType()); 5092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return false; 5102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 5112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); 5122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { 5142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If this is an implicit member reference and we find a 5152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // non-instance member, it's not an error. 5162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!BaseExpr && !(*I)->isCXXInstanceMember()) 5172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return false; 5182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Note that we use the DC of the decl, not the underlying decl. 5202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclContext *DC = (*I)->getDeclContext(); 5212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor while (DC->isTransparentContext()) 5222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DC = DC->getParent(); 5232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!DC->isRecord()) 5252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor continue; 5262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; 5282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); 5292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) 5312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return false; 5322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 5332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, 5352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor R.getRepresentativeDecl(), 5362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor R.getLookupNameInfo()); 5372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return true; 5382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 5392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 540e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrainnamespace { 541e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain 542e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain// Callback to only accept typo corrections that are either a ValueDecl or a 543e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain// FunctionTemplateDecl. 544e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrainclass RecordMemberExprValidatorCCC : public CorrectionCandidateCallback { 545e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain public: 546e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain virtual bool ValidateCandidate(const TypoCorrection &candidate) { 547e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain NamedDecl *ND = candidate.getCorrectionDecl(); 548e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain return ND && (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)); 549e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain } 550e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain}; 551e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain 552e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain} 553e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain 5542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic bool 5552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorLookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, 5562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceRange BaseRange, const RecordType *RTy, 5572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation OpLoc, CXXScopeSpec &SS, 5582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool HasTemplateArgs) { 5592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor RecordDecl *RDecl = RTy->getDecl(); 5602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), 5612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SemaRef.PDiag(diag::err_typecheck_incomplete_tag) 5622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << BaseRange)) 5632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return true; 5642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (HasTemplateArgs) { 5662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // LookupTemplateName doesn't expect these both to exist simultaneously. 5672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); 5682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool MOUS; 5702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); 5712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return false; 5722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 5732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclContext *DC = RDecl; 5752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (SS.isSet()) { 5762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If the member name was a qualified-id, look into the 5772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // nested-name-specifier. 5782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DC = SemaRef.computeDeclContext(SS, false); 5792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (SemaRef.RequireCompleteDeclContext(SS, DC)) { 5812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) 5822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << SS.getRange() << DC; 5832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return true; 5842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 5852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(DC && "Cannot handle non-computable dependent contexts in lookup"); 5872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!isa<TypeDecl>(DC)) { 5892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) 5902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << DC << SS.getRange(); 5912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return true; 5922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 5932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 5942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // The record definition is complete, now look up the member. 5962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SemaRef.LookupQualifiedName(R, DC); 5972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 5982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!R.empty()) 5992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return false; 6002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // We didn't find anything with the given name, so try to correct 6022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // for typos. 6032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclarationName Name = R.getLookupName(); 604e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain RecordMemberExprValidatorCCC Validator; 605d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor TypoCorrection Corrected = SemaRef.CorrectTypo(R.getLookupNameInfo(), 606d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor R.getLookupKind(), NULL, 607e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain &SS, &Validator, DC); 608d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor R.clear(); 609e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain if (NamedDecl *ND = Corrected.getCorrectionDecl()) { 610d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor std::string CorrectedStr( 611d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Corrected.getAsString(SemaRef.getLangOptions())); 612d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor std::string CorrectedQuotedStr( 613d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor Corrected.getQuoted(SemaRef.getLangOptions())); 614d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor R.setLookupName(Corrected.getCorrection()); 615d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor R.addDecl(ND); 6162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) 617d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor << Name << DC << CorrectedQuotedStr << SS.getRange() 618d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); 619d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) 620d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor << ND->getDeclName(); 6212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 6222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return false; 6242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 6252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult 6272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, 6282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation OpLoc, bool IsArrow, 6292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CXXScopeSpec &SS, 6302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NamedDecl *FirstQualifierInScope, 6312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const DeclarationNameInfo &NameInfo, 6322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const TemplateArgumentListInfo *TemplateArgs) { 6332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (BaseType->isDependentType() || 6342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor (SS.isSet() && isDependentScopeSpecifier(SS))) 6352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ActOnDependentMemberExpr(Base, BaseType, 6362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IsArrow, OpLoc, 6372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SS, FirstQualifierInScope, 6382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NameInfo, TemplateArgs); 6392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor LookupResult R(*this, NameInfo, LookupMemberName); 6412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Implicit member accesses. 6432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!Base) { 6442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType RecordTy = BaseType; 6452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); 6462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (LookupMemberExprInRecord(*this, R, SourceRange(), 6472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor RecordTy->getAs<RecordType>(), 6482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor OpLoc, SS, TemplateArgs != 0)) 6492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 6502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Explicit member accesses. 6522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 6532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprResult BaseResult = Owned(Base); 6542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprResult Result = 6552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, 6562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); 6572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (BaseResult.isInvalid()) 6592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 6602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Base = BaseResult.take(); 6612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Result.isInvalid()) { 6632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Owned(Base); 6642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 6652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 6662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Result.get()) 6682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return move(Result); 6692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // LookupMemberExpr can modify Base, and thus change BaseType 6712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseType = Base->getType(); 6722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 6732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildMemberReferenceExpr(Base, BaseType, 6752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor OpLoc, IsArrow, SS, FirstQualifierInScope, 6762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor R, TemplateArgs); 6772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 6782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic ExprResult 6802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorBuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, 6812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const CXXScopeSpec &SS, FieldDecl *Field, 6822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclAccessPair FoundDecl, 6832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const DeclarationNameInfo &MemberNameInfo); 6842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult 6862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, 6872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation loc, 6882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IndirectFieldDecl *indirectField, 6892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Expr *baseObjectExpr, 6902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation opLoc) { 6912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // First, build the expression that refers to the base object. 6922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool baseObjectIsPointer = false; 6942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Qualifiers baseQuals; 6952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 6962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Case 1: the base of the indirect field is not a field. 6972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor VarDecl *baseVariable = indirectField->getVarDecl(); 6982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CXXScopeSpec EmptySS; 6992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (baseVariable) { 7002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(baseVariable->getType()->isRecordType()); 7012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // In principle we could have a member access expression that 7032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // accesses an anonymous struct/union that's a static member of 7042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // the base object's class. However, under the current standard, 7052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // static data members cannot be anonymous structs or unions. 7062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Supporting this is as easy as building a MemberExpr here. 7072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(!baseObjectExpr && "anonymous struct/union is static data member?"); 7082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclarationNameInfo baseNameInfo(DeclarationName(), loc); 7102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprResult result 7122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); 7132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (result.isInvalid()) return ExprError(); 7142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseObjectExpr = result.take(); 7162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseObjectIsPointer = false; 7172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseQuals = baseObjectExpr->getType().getQualifiers(); 7182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Case 2: the base of the indirect field is a field and the user 7202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // wrote a member expression. 7212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else if (baseObjectExpr) { 7222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // The caller provided the base object expression. Determine 7232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // whether its a pointer and whether it adds any qualifiers to the 7242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // anonymous struct/union fields we're looking into. 7252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType objectType = baseObjectExpr->getType(); 7262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (const PointerType *ptr = objectType->getAs<PointerType>()) { 7282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseObjectIsPointer = true; 7292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor objectType = ptr->getPointeeType(); 7302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 7312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseObjectIsPointer = false; 7322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 7332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseQuals = objectType.getQualifiers(); 7342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Case 3: the base of the indirect field is a field and we should 7362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // build an implicit member access. 7372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 7382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // We've found a member of an anonymous struct/union that is 7392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // inside a non-anonymous struct/union, so in a well-formed 7402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // program our base object expression is "this". 741341350ee62abd1ad818e1e3d926cd718960e439bDouglas Gregor QualType ThisTy = getCurrentThisType(); 7422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ThisTy.isNull()) { 7432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(loc, diag::err_invalid_member_use_in_static_method) 7442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << indirectField->getDeclName(); 7452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 7462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 7472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Our base object expression is "this". 74972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman CheckCXXThisCapture(loc); 7502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseObjectExpr 7512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true); 7522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseObjectIsPointer = true; 7532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers(); 7542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 7552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Build the implicit member references to the field of the 7572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // anonymous struct/union. 7582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Expr *result = baseObjectExpr; 7592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IndirectFieldDecl::chain_iterator 7602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); 7612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Build the first member access in the chain with full information. 7632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!baseVariable) { 7642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor FieldDecl *field = cast<FieldDecl>(*FI); 7652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // FIXME: use the real found-decl info! 7672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); 7682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Make a nameInfo that properly uses the anonymous name. 7702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); 7712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, 7732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor EmptySS, field, foundDecl, 7742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor memberNameInfo).take(); 7752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseObjectIsPointer = false; 7762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // FIXME: check qualified member access 7782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 7792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // In all cases, we should now skip the first declaration in the chain. 7812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ++FI; 7822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor while (FI != FEnd) { 7842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor FieldDecl *field = cast<FieldDecl>(*FI++); 7852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // FIXME: these are somewhat meaningless 7872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); 7882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); 7892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, 7912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor (FI == FEnd? SS : EmptySS), field, 7922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor foundDecl, memberNameInfo).take(); 7932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 7942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(result); 7962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 7972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 7982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \brief Build a MemberExpr AST node. 7992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, 8002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const CXXScopeSpec &SS, ValueDecl *Member, 8012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclAccessPair FoundDecl, 8022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const DeclarationNameInfo &MemberNameInfo, 8032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType Ty, 8042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprValueKind VK, ExprObjectKind OK, 8052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const TemplateArgumentListInfo *TemplateArgs = 0) { 8064f87062cb411d5a31cf39f1ac576bba4123930f2Richard Smith assert((!isArrow || Base->isRValue()) && "-> base must be a pointer rvalue"); 8072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C), 8082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Member, FoundDecl, MemberNameInfo, 8092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor TemplateArgs, Ty, VK, OK); 8102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 8112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult 8132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, 8142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation OpLoc, bool IsArrow, 8152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const CXXScopeSpec &SS, 8162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NamedDecl *FirstQualifierInScope, 8172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor LookupResult &R, 8182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const TemplateArgumentListInfo *TemplateArgs, 8192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool SuppressQualifierCheck) { 8202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType BaseType = BaseExprType; 8212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IsArrow) { 8222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(BaseType->isPointerType()); 8233c3b7f90a863af43fa63043d396553ecf205351cJohn McCall BaseType = BaseType->castAs<PointerType>()->getPointeeType(); 8242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 8252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor R.setBaseObjectType(BaseType); 8262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); 8282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclarationName MemberName = MemberNameInfo.getName(); 8292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation MemberLoc = MemberNameInfo.getLoc(); 8302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (R.isAmbiguous()) 8322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 8332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (R.empty()) { 8352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Rederive where we looked up. 8362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclContext *DC = (SS.isSet() 8372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ? computeDeclContext(SS, false) 8382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor : BaseType->getAs<RecordType>()->getDecl()); 8392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(R.getNameLoc(), diag::err_no_member) 8412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << MemberName << DC 8422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); 8432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 8442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 8452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Diagnose lookups that find only declarations from a non-base 8472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // type. This is possible for either qualified lookups (which may 8482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // have been qualified with an unrelated type) or implicit member 8492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // expressions (which were found with unqualified lookup and thus 8502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // may have come from an enclosing scope). Note that it's okay for 8512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // lookup to find declarations from a non-base type as long as those 8522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // aren't the ones picked by overload resolution. 8532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if ((SS.isSet() || !BaseExpr || 8542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor (isa<CXXThisExpr>(BaseExpr) && 8552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor cast<CXXThisExpr>(BaseExpr)->isImplicit())) && 8562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor !SuppressQualifierCheck && 8572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) 8582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 859d12505054130b24f7696440efdbd1aa660feb6f3Fariborz Jahanian 8602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Construct an unresolved result if we in fact got an unresolved 8612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // result. 8622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (R.isOverloadedResult() || R.isUnresolvableResult()) { 8632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Suppress any lookup-related diagnostics; we'll do these when we 8642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // pick a member. 8652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor R.suppressDiagnostics(); 8662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor UnresolvedMemberExpr *MemExpr 8682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), 8692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseExpr, BaseExprType, 8702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IsArrow, OpLoc, 8712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SS.getWithLocInContext(Context), 8722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberNameInfo, 8732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor TemplateArgs, R.begin(), R.end()); 8742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(MemExpr); 8762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 8772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(R.isSingleResult()); 8792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclAccessPair FoundDecl = R.begin().getPair(); 8802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NamedDecl *MemberDecl = R.getFoundDecl(); 8812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // FIXME: diagnose the presence of template arguments now. 8832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If the decl being referenced had an error, return an error for this 8852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // sub-expr without emitting another error, in order to avoid cascading 8862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // error cases. 8872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (MemberDecl->isInvalidDecl()) 8882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 8892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Handle the implicit-member-access case. 8912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!BaseExpr) { 8922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If this is not an instance member, convert to a non-member access. 8932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!MemberDecl->isCXXInstanceMember()) 8942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); 8952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 8962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation Loc = R.getNameLoc(); 8972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (SS.getRange().isValid()) 8982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Loc = SS.getRange().getBegin(); 89972899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman CheckCXXThisCapture(Loc); 9002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); 9012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 9022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool ShouldCheckUse = true; 9042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { 9052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Don't diagnose the use of a virtual member function unless it's 9062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // explicitly qualified. 9072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (MD->isVirtual() && !SS.isSet()) 9082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ShouldCheckUse = false; 9092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 9102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Check the use of this member. 9122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { 9132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Owned(BaseExpr); 9142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 9152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 9162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) 9182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, 9192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SS, FD, FoundDecl, MemberNameInfo); 9202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) 9222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // We may have found a field within an anonymous union or struct 9232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // (C++ [class.union]). 9242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, 9252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseExpr, OpLoc); 9262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { 9282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MarkDeclarationReferenced(MemberLoc, Var); 9292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, 9302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Var, FoundDecl, MemberNameInfo, 9312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Var->getType().getNonReferenceType(), 9322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor VK_LValue, OK_Ordinary)); 9332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 9342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { 9362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprValueKind valueKind; 9372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType type; 9382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (MemberFn->isInstance()) { 9392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor valueKind = VK_RValue; 9402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor type = Context.BoundMemberTy; 9412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 9422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor valueKind = VK_LValue; 9432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor type = MemberFn->getType(); 9442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 9452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MarkDeclarationReferenced(MemberLoc, MemberDecl); 9472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, 9482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberFn, FoundDecl, MemberNameInfo, 9492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor type, valueKind, OK_Ordinary)); 9502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 9512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); 9522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { 9542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MarkDeclarationReferenced(MemberLoc, MemberDecl); 9552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, 9562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Enum, FoundDecl, MemberNameInfo, 9572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Enum->getType(), VK_RValue, OK_Ordinary)); 9582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 9592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Owned(BaseExpr); 9612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // We found something that we didn't expect. Complain. 9632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (isa<TypeDecl>(MemberDecl)) 9642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(MemberLoc, diag::err_typecheck_member_reference_type) 9652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << MemberName << BaseType << int(IsArrow); 9662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor else 9672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) 9682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << MemberName << BaseType << int(IsArrow); 9692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(MemberDecl->getLocation(), diag::note_member_declared_here) 9712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << MemberName; 9722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor R.suppressDiagnostics(); 9732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 9742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 9752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Given that normal member access failed on the given expression, 9772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// and given that the expression's type involves builtin-id or 9782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// builtin-Class, decide whether substituting in the redefinition 9792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// types would be profitable. The redefinition type is whatever 9802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// this translation unit tried to typedef to id/Class; we store 9812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// it to the side and then re-use it in places like this. 9822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) { 9832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const ObjCObjectPointerType *opty 9842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor = base.get()->getType()->getAs<ObjCObjectPointerType>(); 9852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!opty) return false; 9862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const ObjCObjectType *ty = opty->getObjectType(); 9882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType redef; 9902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ty->isObjCId()) { 99101a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor redef = S.Context.getObjCIdRedefinitionType(); 9922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else if (ty->isObjCClass()) { 99301a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor redef = S.Context.getObjCClassRedefinitionType(); 9942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 9952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return false; 9962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 9972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 9982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Do the substitution as long as the redefinition type isn't just a 9992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // possibly-qualified pointer to builtin-id or builtin-Class again. 10002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor opty = redef->getAs<ObjCObjectPointerType>(); 10012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (opty && !opty->getObjectType()->getInterface() != 0) 10022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return false; 10032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 10042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor base = S.ImpCastExprToType(base.take(), redef, CK_BitCast); 10052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return true; 10062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 10072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 10086dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool isRecordType(QualType T) { 10096dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall return T->isRecordType(); 10106dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall} 10116dbba4fc128e2e2f5b26be996392bd32c0707f13John McCallstatic bool isPointerToRecordType(QualType T) { 10126dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall if (const PointerType *PT = T->getAs<PointerType>()) 10136dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall return PT->getPointeeType()->isRecordType(); 10146dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall return false; 10156dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall} 10166dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall 10179138b4e96429cbaae00c52c15c960f72b6645088Richard Smith/// Perform conversions on the LHS of a member access expression. 10189138b4e96429cbaae00c52c15c960f72b6645088Richard SmithExprResult 10199138b4e96429cbaae00c52c15c960f72b6645088Richard SmithSema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) { 1020059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman if (IsArrow && !Base->getType()->isFunctionType()) 1021059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman return DefaultFunctionArrayLvalueConversion(Base); 10229138b4e96429cbaae00c52c15c960f72b6645088Richard Smith 1023059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman return CheckPlaceholderExpr(Base); 10249138b4e96429cbaae00c52c15c960f72b6645088Richard Smith} 10259138b4e96429cbaae00c52c15c960f72b6645088Richard Smith 10262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Look up the given member of the given non-type-dependent 10272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression. This can return in one of two ways: 10282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// * If it returns a sentinel null-but-valid result, the caller will 10292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// assume that lookup was performed and the results written into 10302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// the provided structure. It will take over from there. 10312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// * Otherwise, the returned expression will be produced in place of 10322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// an ordinary member expression. 10332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// 10342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The ObjCImpDecl bit is a gross hack that will need to be properly 10352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// fixed for ObjC++. 10362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult 10372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, 10382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool &IsArrow, SourceLocation OpLoc, 10392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CXXScopeSpec &SS, 10402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Decl *ObjCImpDecl, bool HasTemplateArgs) { 10412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(BaseExpr.get() && "no base expression"); 10422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 10432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Perform default conversions. 10449138b4e96429cbaae00c52c15c960f72b6645088Richard Smith BaseExpr = PerformMemberExprBaseConversion(BaseExpr.take(), IsArrow); 10456dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall if (BaseExpr.isInvalid()) 10466dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall return ExprError(); 10472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 10482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType BaseType = BaseExpr.get()->getType(); 10492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(!BaseType->isDependentType()); 10502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 10512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclarationName MemberName = R.getLookupName(); 10522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation MemberLoc = R.getNameLoc(); 10532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 10542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // For later type-checking purposes, turn arrow accesses into dot 10552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // accesses. The only access type we support that doesn't follow 10562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // the C equivalence "a->b === (*a).b" is ObjC property accesses, 10572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // and those never use arrows, so this is unaffected. 10582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IsArrow) { 10592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (const PointerType *Ptr = BaseType->getAs<PointerType>()) 10602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseType = Ptr->getPointeeType(); 10612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor else if (const ObjCObjectPointerType *Ptr 10622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor = BaseType->getAs<ObjCObjectPointerType>()) 10632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseType = Ptr->getPointeeType(); 10642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor else if (BaseType->isRecordType()) { 10652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Recover from arrow accesses to records, e.g.: 10662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // struct MyRecord foo; 10672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // foo->bar 10682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // This is actually well-formed in C++ if MyRecord has an 10692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // overloaded operator->, but that should have been dealt with 10702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // by now. 10712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) 10722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() 10732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << FixItHint::CreateReplacement(OpLoc, "."); 10742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IsArrow = false; 1075059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman } else if (BaseType->isFunctionType()) { 10762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor goto fail; 10772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 10782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) 10792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << BaseType << BaseExpr.get()->getSourceRange(); 10802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 10812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 10822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 10832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 10842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Handle field access to simple records. 10852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (const RecordType *RTy = BaseType->getAs<RecordType>()) { 10862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(), 10872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor RTy, OpLoc, SS, HasTemplateArgs)) 10882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 10892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 10902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Returning valid-but-null is how we indicate to the caller that 10912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // the lookup result was filled in. 10922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned((Expr*) 0); 10932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 10942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 10952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Handle ivar access to Objective-C objects. 10962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { 10975a706dc1b17f875c7fce20f1fbf9ca372be4c331Douglas Gregor if (!SS.isEmpty() && !SS.isInvalid()) { 1098b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access) 1099b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor << 1 << SS.getScopeRep() 1100b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor << FixItHint::CreateRemoval(SS.getRange()); 1101b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor SS.clear(); 1102b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor } 1103b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor 11042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); 11052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 11062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // There are three cases for the base type: 11072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // - builtin id (qualified or unqualified) 11082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // - builtin Class (qualified or unqualified) 11092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // - an interface 11102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCInterfaceDecl *IDecl = OTy->getInterface(); 11112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!IDecl) { 11122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (getLangOptions().ObjCAutoRefCount && 11132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor (OTy->isObjCId() || OTy->isObjCClass())) 11142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor goto fail; 11152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // There's an implicit 'isa' ivar on all objects. 11162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // But we only actually find it this way on objects of type 'id', 11172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // apparently. 11182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (OTy->isObjCId() && Member->isStr("isa")) 11192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc, 11202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Context.getObjCClassType())); 11212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 11222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) 11232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, 11242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCImpDecl, HasTemplateArgs); 11252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor goto fail; 11262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 11272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 1128d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor if (RequireCompleteType(OpLoc, BaseType, 1129d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor PDiag(diag::err_typecheck_incomplete_tag) 1130d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor << BaseExpr.get()->getSourceRange())) 1131d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor return ExprError(); 1132d07cc36c71558b62889691184dd04655a33fd12aDouglas Gregor 11332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCInterfaceDecl *ClassDeclared; 11342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); 11352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 11362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!IV) { 11372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Attempt to correct for typos in ivar names. 1138e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain DeclFilterCCC<ObjCIvarDecl> Validator; 1139e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain Validator.IsObjCIvarLookup = IsArrow; 1140e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(), 1141e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain LookupMemberName, NULL, NULL, 1142e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain &Validator, IDecl)) { 1143e4c7f90da208ed2caeab784b32f416a50eed8da3Kaelyn Uhrain IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>(); 11442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(R.getNameLoc(), 11452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor diag::err_typecheck_member_reference_ivar_suggest) 11462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << IDecl->getDeclName() << MemberName << IV->getDeclName() 11472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << FixItHint::CreateReplacement(R.getNameLoc(), 11482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IV->getNameAsString()); 11492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(IV->getLocation(), diag::note_previous_decl) 11502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << IV->getDeclName(); 11512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 11526326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian if (IsArrow && IDecl->FindPropertyDeclaration(Member)) { 11536326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian Diag(MemberLoc, 11546326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian diag::err_property_found_suggest) 11556326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian << Member << BaseExpr.get()->getType() 11566326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian << FixItHint::CreateReplacement(OpLoc, "."); 11576326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian return ExprError(); 11586326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian } 11592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 11602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) 11612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << IDecl->getDeclName() << MemberName 11622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << BaseExpr.get()->getSourceRange(); 11632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 11642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 11652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 11662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 11672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If the decl being referenced had an error, return an error for this 11682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // sub-expr without emitting another error, in order to avoid cascading 11692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // error cases. 11702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IV->isInvalidDecl()) 11712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 11722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 11732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Check whether we can reference this field. 11742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (DiagnoseUseOfDecl(IV, MemberLoc)) 11752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 11762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IV->getAccessControl() != ObjCIvarDecl::Public && 11772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IV->getAccessControl() != ObjCIvarDecl::Package) { 11782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCInterfaceDecl *ClassOfMethodDecl = 0; 11792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ObjCMethodDecl *MD = getCurMethodDecl()) 11802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ClassOfMethodDecl = MD->getClassInterface(); 11812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor else if (ObjCImpDecl && getCurFunctionDecl()) { 11822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Case of a c-function declared inside an objc implementation. 11832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // FIXME: For a c-style function nested inside an objc implementation 11842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // class, there is no implementation context available, so we pass 11852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // down the context as argument to this routine. Ideally, this context 11862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // need be passed down in the AST node and somehow calculated from the 11872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // AST for a function decl. 11882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ObjCImplementationDecl *IMPD = 11892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) 11902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ClassOfMethodDecl = IMPD->getClassInterface(); 11912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor else if (ObjCCategoryImplDecl* CatImplClass = 11922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) 11932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ClassOfMethodDecl = CatImplClass->getClassInterface(); 11942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 11952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 11962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IV->getAccessControl() == ObjCIvarDecl::Private) { 119760ef308e51c71b760d7f598c1b763ceb7b768148Douglas Gregor if (!declaresSameEntity(ClassDeclared, IDecl) || 119860ef308e51c71b760d7f598c1b763ceb7b768148Douglas Gregor !declaresSameEntity(ClassOfMethodDecl, ClassDeclared)) 11992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(MemberLoc, diag::error_private_ivar_access) 12002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << IV->getDeclName(); 12012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) 12022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // @protected 12032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(MemberLoc, diag::error_protected_ivar_access) 12042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << IV->getDeclName(); 12052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 12062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (getLangOptions().ObjCAutoRefCount) { 12072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts(); 12082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp)) 12092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (UO->getOpcode() == UO_Deref) 12102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseExp = UO->getSubExpr()->IgnoreParenCasts(); 12112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp)) 12132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) 12142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(DE->getLocation(), diag::error_arc_weak_ivar_access); 12152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 12162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), 12182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberLoc, BaseExpr.take(), 12192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IsArrow)); 12202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 12212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Objective-C property access. 12232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const ObjCObjectPointerType *OPT; 12242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { 12255a706dc1b17f875c7fce20f1fbf9ca372be4c331Douglas Gregor if (!SS.isEmpty() && !SS.isInvalid()) { 1226b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access) 1227b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor << 0 << SS.getScopeRep() 1228b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor << FixItHint::CreateRemoval(SS.getRange()); 1229b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor SS.clear(); 1230b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor } 1231b5ae92f2f52df88ae14504d3a3f2bddb479829b6Douglas Gregor 12322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // This actually uses the base as an r-value. 12332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseExpr = DefaultLvalueConversion(BaseExpr.take()); 12342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (BaseExpr.isInvalid()) 12352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 12362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType())); 12382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); 12402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const ObjCObjectType *OT = OPT->getObjectType(); 12422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // id, with and without qualifiers. 12442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (OT->isObjCId()) { 12452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Check protocols on qualified interfaces. 12462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Selector Sel = PP.getSelectorTable().getNullarySelector(Member); 12472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { 12482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { 12492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Check the use of this declaration 12502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (DiagnoseUseOfDecl(PD, MemberLoc)) 12512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 12522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12533c3b7f90a863af43fa63043d396553ecf205351cJohn McCall return Owned(new (Context) ObjCPropertyRefExpr(PD, 12543c3b7f90a863af43fa63043d396553ecf205351cJohn McCall Context.PseudoObjectTy, 12552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor VK_LValue, 12562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor OK_ObjCProperty, 12572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberLoc, 12582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseExpr.take())); 12592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 12602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { 12622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Check the use of this method. 12632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (DiagnoseUseOfDecl(OMD, MemberLoc)) 12642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 12652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Selector SetterSel = 12662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SelectorTable::constructSetterName(PP.getIdentifierTable(), 12672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor PP.getSelectorTable(), Member); 12682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCMethodDecl *SMD = 0; 12692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, 12702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SetterSel, Context)) 12712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SMD = dyn_cast<ObjCMethodDecl>(SDecl); 12722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12733c3b7f90a863af43fa63043d396553ecf205351cJohn McCall return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, 12743c3b7f90a863af43fa63043d396553ecf205351cJohn McCall Context.PseudoObjectTy, 12753c3b7f90a863af43fa63043d396553ecf205351cJohn McCall VK_LValue, OK_ObjCProperty, 12762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberLoc, BaseExpr.take())); 12772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 12782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 12792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Use of id.member can only be for a property reference. Do not 12802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // use the 'id' redefinition in this case. 12812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) 12822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, 12832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCImpDecl, HasTemplateArgs); 12842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(Diag(MemberLoc, diag::err_property_not_found) 12862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << MemberName << BaseType); 12872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 12882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // 'Class', unqualified only. 12902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (OT->isObjCClass()) { 12912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Only works in a method declaration (??!). 12922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCMethodDecl *MD = getCurMethodDecl(); 12932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!MD) { 12942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) 12952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, 12962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCImpDecl, HasTemplateArgs); 12972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 12982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor goto fail; 12992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 13002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Also must look for a getter name which uses property syntax. 13022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Selector Sel = PP.getSelectorTable().getNullarySelector(Member); 13032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCInterfaceDecl *IFace = MD->getClassInterface(); 13042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCMethodDecl *Getter; 13052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if ((Getter = IFace->lookupClassMethod(Sel))) { 13062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Check the use of this method. 13072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (DiagnoseUseOfDecl(Getter, MemberLoc)) 13082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 13092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else 13102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Getter = IFace->lookupPrivateMethod(Sel, false); 13112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If we found a getter then this may be a valid dot-reference, we 13122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // will look for the matching setter, in case it is needed. 13132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Selector SetterSel = 13142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SelectorTable::constructSetterName(PP.getIdentifierTable(), 13152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor PP.getSelectorTable(), Member); 13162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); 13172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!Setter) { 13182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If this reference is in an @implementation, also check for 'private' 13192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // methods. 13202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Setter = IFace->lookupPrivateMethod(SetterSel, false); 13212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 13222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Look through local category implementations associated with the class. 13232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!Setter) 13242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Setter = IFace->getCategoryClassMethod(SetterSel); 13252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) 13272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 13282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Getter || Setter) { 13302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, 13313c3b7f90a863af43fa63043d396553ecf205351cJohn McCall Context.PseudoObjectTy, 13323c3b7f90a863af43fa63043d396553ecf205351cJohn McCall VK_LValue, OK_ObjCProperty, 13332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberLoc, BaseExpr.take())); 13342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 13352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) 13372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, 13382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCImpDecl, HasTemplateArgs); 13392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(Diag(MemberLoc, diag::err_property_not_found) 13412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << MemberName << BaseType); 13422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 13432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Normal property access. 13456326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc, 13466326e05fe8c2ff92b65b4759a91e45fad5ef886fFariborz Jahanian MemberName, MemberLoc, 13472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation(), QualType(), false); 13482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 13492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Handle 'field access' to vectors, such as 'V.xx'. 13512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (BaseType->isExtVectorType()) { 13522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // FIXME: this expr should store IsArrow. 13532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); 13542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind()); 13552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, 13562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Member, MemberLoc); 13572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (ret.isNull()) 13582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 13592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(), 13612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor *Member, MemberLoc)); 13622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 13632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Adjust builtin-sel to the appropriate redefinition type if that's 13652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // not just a pointer to builtin-sel again. 13662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IsArrow && 13672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && 136801a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor !Context.getObjCSelRedefinitionType()->isObjCSelType()) { 136901a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor BaseExpr = ImpCastExprToType(BaseExpr.take(), 137001a4cf11777bb34c35f5d251a9e95eb736d0842bDouglas Gregor Context.getObjCSelRedefinitionType(), 13712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CK_BitCast); 13722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, 13732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCImpDecl, HasTemplateArgs); 13742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 13752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Failure cases. 13772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor fail: 13782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Recover from dot accesses to pointers, e.g.: 13802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // type *foo; 13812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // foo.bar 13822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // This is actually well-formed in two cases: 13832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // - 'type' is an Objective C type 13842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // - 'bar' is a pseudo-destructor name which happens to refer to 13852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // the appropriate pointer type 13862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { 13872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!IsArrow && Ptr->getPointeeType()->isRecordType() && 13882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberName.getNameKind() != DeclarationName::CXXDestructorName) { 13892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) 13902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() 13912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << FixItHint::CreateReplacement(OpLoc, "->"); 13922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 13932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Recurse as an -> access. 13942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IsArrow = true; 13952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, 13962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ObjCImpDecl, HasTemplateArgs); 13972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 13982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 13992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If the user is trying to apply -> or . to a function name, it's probably 14012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // because they forgot parentheses to call that function. 14026dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall if (tryToRecoverWithCall(BaseExpr, 14036dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall PDiag(diag::err_member_reference_needs_call), 14046dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall /*complain*/ false, 1405059d578c7d45f687a81bcc97ab80404256a5287fEli Friedman IsArrow ? &isPointerToRecordType : &isRecordType)) { 14066dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall if (BaseExpr.isInvalid()) 14072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 14086dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); 14096dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, 14106dbba4fc128e2e2f5b26be996392bd32c0707f13John McCall ObjCImpDecl, HasTemplateArgs); 14112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 14122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) 14142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor << BaseType << BaseExpr.get()->getSourceRange(); 14152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 14172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 14182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// The main callback when the parser finds something like 14202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression . [nested-name-specifier] identifier 14212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// expression -> [nested-name-specifier] identifier 14222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// where 'identifier' encompasses a fairly broad spectrum of 14232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// possibilities, including destructor and operator references. 14242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// 14252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param OpKind either tok::arrow or tok::period 14262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param HasTrailingLParen whether the next token is '(', which 14272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// is used to diagnose mis-uses of special members that can 14282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// only be called 14292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// \param ObjCImpDecl the current ObjC @implementation decl; 14302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// this is an ugly hack around the fact that ObjC @implementations 14312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// aren't properly put in the context chain 14322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, 14332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation OpLoc, 14342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor tok::TokenKind OpKind, 14352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor CXXScopeSpec &SS, 14362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor UnqualifiedId &Id, 14372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Decl *ObjCImpDecl, 14382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool HasTrailingLParen) { 14392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (SS.isSet() && SS.isInvalid()) 14402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 14412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Warn about the explicit constructor calls Microsoft extension. 144362ec1f2fd7368542bb926c04797fb07023547694Francois Pichet if (getLangOptions().MicrosoftExt && 14442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Id.getKind() == UnqualifiedId::IK_ConstructorName) 14452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Diag(Id.getSourceRange().getBegin(), 14462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor diag::ext_ms_explicit_constructor_call); 14472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor TemplateArgumentListInfo TemplateArgsBuffer; 14492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Decompose the name into its component parts. 14512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclarationNameInfo NameInfo; 14522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const TemplateArgumentListInfo *TemplateArgs; 14532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DecomposeUnqualifiedId(Id, TemplateArgsBuffer, 14542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NameInfo, TemplateArgs); 14552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclarationName Name = NameInfo.getName(); 14572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool IsArrow = (OpKind == tok::arrow); 14582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NamedDecl *FirstQualifierInScope 14602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, 14612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); 14622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // This is a postfix expression, so get rid of ParenListExprs. 14642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); 14652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Result.isInvalid()) return ExprError(); 14662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Base = Result.take(); 14672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Base->getType()->isDependentType() || Name.isDependentName() || 14692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor isDependentScopeSpecifier(SS)) { 14702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Result = ActOnDependentMemberExpr(Base, Base->getType(), 14712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor IsArrow, OpLoc, 14722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SS, FirstQualifierInScope, 14732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor NameInfo, TemplateArgs); 14742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 14752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor LookupResult R(*this, NameInfo, LookupMemberName); 14762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprResult BaseResult = Owned(Base); 14772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, 14782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SS, ObjCImpDecl, TemplateArgs != 0); 14792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (BaseResult.isInvalid()) 14802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 14812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Base = BaseResult.take(); 14822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Result.isInvalid()) { 14842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Owned(Base); 14852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 14862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 14872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Result.get()) { 14892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // The only way a reference to a destructor can be used is to 14902b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // immediately call it, which falls into this case. If the 14912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // next token is not a '(', produce a diagnostic and build the 14922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // call now. 14932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!HasTrailingLParen && 14942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Id.getKind() == UnqualifiedId::IK_DestructorName) 14952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); 14962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 14972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return move(Result); 14982b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 14992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Result = BuildMemberReferenceExpr(Base, Base->getType(), 15012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor OpLoc, IsArrow, SS, FirstQualifierInScope, 15022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor R, TemplateArgs); 15032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 15042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return move(Result); 15062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 15072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregorstatic ExprResult 15092b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorBuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, 15102b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const CXXScopeSpec &SS, FieldDecl *Field, 15112b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor DeclAccessPair FoundDecl, 15122b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const DeclarationNameInfo &MemberNameInfo) { 15132b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // x.a is an l-value if 'a' has a reference type. Otherwise: 15142b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // x.a is an l-value/x-value/pr-value if the base is (and note 15152b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // that *x is always an l-value), except that if the base isn't 15162b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // an ordinary object then we must have an rvalue. 15172b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprValueKind VK = VK_LValue; 15182b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprObjectKind OK = OK_Ordinary; 15192b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (!IsArrow) { 15202b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (BaseExpr->getObjectKind() == OK_Ordinary) 15212b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor VK = BaseExpr->getValueKind(); 15222b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor else 15232b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor VK = VK_RValue; 15242b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 15252b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (VK != VK_RValue && Field->isBitField()) 15262b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor OK = OK_BitField; 15272b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15282b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] 15292b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType MemberType = Field->getType(); 15302b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { 15312b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberType = Ref->getPointeeType(); 15322b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor VK = VK_LValue; 15332b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } else { 15342b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor QualType BaseType = BaseExpr->getType(); 15352b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); 15362b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15372b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Qualifiers BaseQuals = BaseType.getQualifiers(); 15382b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15392b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // GC attributes are never picked up by members. 15402b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor BaseQuals.removeObjCGCAttr(); 15412b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15422b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // CVR attributes from the base are picked up by members, 15432b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // except that 'mutable' members don't pick up 'const'. 15442b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Field->isMutable()) BaseQuals.removeConst(); 15452b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15462b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Qualifiers MemberQuals 15472b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor = S.Context.getCanonicalType(MemberType).getQualifiers(); 15482b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15492b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // TR 18037 does not allow fields to be declared with address spaces. 15502b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(!MemberQuals.hasAddressSpace()); 15512b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15522b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Qualifiers Combined = BaseQuals + MemberQuals; 15532b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Combined != MemberQuals) 15542b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberType = S.Context.getQualifiedType(MemberType, Combined); 15552b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 15562b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15572b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); 15582b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor ExprResult Base = 15592b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), 15602b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor FoundDecl, Field); 15612b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (Base.isInvalid()) 15622b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return ExprError(); 15632b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return S.Owned(BuildMemberExpr(S.Context, Base.take(), IsArrow, SS, 15642b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Field, FoundDecl, MemberNameInfo, 15652b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor MemberType, VK, OK)); 15662b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 15672b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15682b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// Builds an implicit member access expression. The current context 15692b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// is known to be an instance method, and the given unqualified lookup 15702b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// set is known to contain only instance members, at least one of which 15712b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor/// is from an appropriate type. 15722b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorExprResult 15732b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas GregorSema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, 15742b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor LookupResult &R, 15752b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor const TemplateArgumentListInfo *TemplateArgs, 15762b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor bool IsKnownInstance) { 15772b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(!R.empty() && !R.isAmbiguous()); 15782b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15792b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation loc = R.getNameLoc(); 15802b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15812b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // We may have found a field within an anonymous union or struct 15822b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // (C++ [class.union]). 15832b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // FIXME: template-ids inside anonymous structs? 15842b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) 15852b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD); 15862b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15872b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // If this is known to be an instance access, go ahead and build an 15882b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // implicit 'this' expression now. 15892b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor // 'this' expression now. 1590341350ee62abd1ad818e1e3d926cd718960e439bDouglas Gregor QualType ThisTy = getCurrentThisType(); 15912b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'"); 15922b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 15932b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Expr *baseExpr = 0; // null signifies implicit access 15942b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (IsKnownInstance) { 15952b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SourceLocation Loc = R.getNameLoc(); 15962b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor if (SS.getRange().isValid()) 15972b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor Loc = SS.getRange().getBegin(); 159872899c34e3d1abfffa241ad0ce5c4bf175e5ea51Eli Friedman CheckCXXThisCapture(Loc); 15992b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true); 16002b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor } 16012b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor 16022b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor return BuildMemberReferenceExpr(baseExpr, ThisTy, 16032b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor /*OpLoc*/ SourceLocation(), 16042b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor /*IsArrow*/ true, 16052b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor SS, 16062b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor /*FirstQualifierInScope*/ 0, 16072b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor R, TemplateArgs); 16082b1ad8b42bbbe00a1845e566f52f1941b8dbc725Douglas Gregor} 1609