clang API Documentation

SemaExprMember.cpp
Go to the documentation of this file.
00001 //===--- SemaExprMember.cpp - Semantic Analysis for Expressions -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements semantic analysis member access expressions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 #include "clang/Sema/SemaInternal.h"
00014 #include "clang/Sema/Lookup.h"
00015 #include "clang/Sema/Scope.h"
00016 #include "clang/AST/DeclCXX.h"
00017 #include "clang/AST/DeclObjC.h"
00018 #include "clang/AST/DeclTemplate.h"
00019 #include "clang/AST/ExprCXX.h"
00020 #include "clang/AST/ExprObjC.h"
00021 #include "clang/Lex/Preprocessor.h"
00022 
00023 using namespace clang;
00024 using namespace sema;
00025 
00026 /// Determines if the given class is provably not derived from all of
00027 /// the prospective base classes.
00028 static bool IsProvablyNotDerivedFrom(Sema &SemaRef,
00029                                      CXXRecordDecl *Record,
00030                             const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) {
00031   if (Bases.count(Record->getCanonicalDecl()))
00032     return false;
00033 
00034   RecordDecl *RD = Record->getDefinition();
00035   if (!RD) return false;
00036   Record = cast<CXXRecordDecl>(RD);
00037 
00038   for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(),
00039          E = Record->bases_end(); I != E; ++I) {
00040     CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType());
00041     CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>();
00042     if (!BaseRT) return false;
00043 
00044     CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl());
00045     if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases))
00046       return false;
00047   }
00048 
00049   return true;
00050 }
00051 
00052 enum IMAKind {
00053   /// The reference is definitely not an instance member access.
00054   IMA_Static,
00055 
00056   /// The reference may be an implicit instance member access.
00057   IMA_Mixed,
00058 
00059   /// The reference may be to an instance member, but it might be invalid if
00060   /// so, because the context is not an instance method.
00061   IMA_Mixed_StaticContext,
00062 
00063   /// The reference may be to an instance member, but it is invalid if
00064   /// so, because the context is from an unrelated class.
00065   IMA_Mixed_Unrelated,
00066 
00067   /// The reference is definitely an implicit instance member access.
00068   IMA_Instance,
00069 
00070   /// The reference may be to an unresolved using declaration.
00071   IMA_Unresolved,
00072 
00073   /// The reference may be to an unresolved using declaration and the
00074   /// context is not an instance method.
00075   IMA_Unresolved_StaticContext,
00076 
00077   // The reference refers to a field which is not a member of the containing
00078   // class, which is allowed because we're in C++11 mode and the context is
00079   // unevaluated.
00080   IMA_Field_Uneval_Context,
00081 
00082   /// All possible referrents are instance members and the current
00083   /// context is not an instance method.
00084   IMA_Error_StaticContext,
00085 
00086   /// All possible referrents are instance members of an unrelated
00087   /// class.
00088   IMA_Error_Unrelated
00089 };
00090 
00091 /// The given lookup names class member(s) and is not being used for
00092 /// an address-of-member expression.  Classify the type of access
00093 /// according to whether it's possible that this reference names an
00094 /// instance member.  This is best-effort in dependent contexts; it is okay to
00095 /// conservatively answer "yes", in which case some errors will simply
00096 /// not be caught until template-instantiation.
00097 static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
00098                                             Scope *CurScope,
00099                                             const LookupResult &R) {
00100   assert(!R.empty() && (*R.begin())->isCXXClassMember());
00101 
00102   DeclContext *DC = SemaRef.getFunctionLevelDeclContext();
00103 
00104   bool isStaticContext = SemaRef.CXXThisTypeOverride.isNull() &&
00105     (!isa<CXXMethodDecl>(DC) || cast<CXXMethodDecl>(DC)->isStatic());
00106 
00107   if (R.isUnresolvableResult())
00108     return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved;
00109 
00110   // Collect all the declaring classes of instance members we find.
00111   bool hasNonInstance = false;
00112   bool isField = false;
00113   llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes;
00114   for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
00115     NamedDecl *D = *I;
00116 
00117     if (D->isCXXInstanceMember()) {
00118       if (dyn_cast<FieldDecl>(D))
00119         isField = true;
00120 
00121       CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext());
00122       Classes.insert(R->getCanonicalDecl());
00123     }
00124     else
00125       hasNonInstance = true;
00126   }
00127 
00128   // If we didn't find any instance members, it can't be an implicit
00129   // member reference.
00130   if (Classes.empty())
00131     return IMA_Static;
00132 
00133   bool IsCXX11UnevaluatedField = false;
00134   if (SemaRef.getLangOpts().CPlusPlus0x && isField) {
00135     // C++11 [expr.prim.general]p12:
00136     //   An id-expression that denotes a non-static data member or non-static
00137     //   member function of a class can only be used:
00138     //   (...)
00139     //   - if that id-expression denotes a non-static data member and it
00140     //     appears in an unevaluated operand.
00141     const Sema::ExpressionEvaluationContextRecord& record
00142       = SemaRef.ExprEvalContexts.back();
00143     if (record.Context == Sema::Unevaluated)
00144       IsCXX11UnevaluatedField = true;
00145   }
00146 
00147   // If the current context is not an instance method, it can't be
00148   // an implicit member reference.
00149   if (isStaticContext) {
00150     if (hasNonInstance)
00151       return IMA_Mixed_StaticContext;
00152 
00153     return IsCXX11UnevaluatedField ? IMA_Field_Uneval_Context
00154                                    : IMA_Error_StaticContext;
00155   }
00156 
00157   CXXRecordDecl *contextClass;
00158   if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
00159     contextClass = MD->getParent()->getCanonicalDecl();
00160   else
00161     contextClass = cast<CXXRecordDecl>(DC);
00162 
00163   // [class.mfct.non-static]p3: 
00164   // ...is used in the body of a non-static member function of class X,
00165   // if name lookup (3.4.1) resolves the name in the id-expression to a
00166   // non-static non-type member of some class C [...]
00167   // ...if C is not X or a base class of X, the class member access expression
00168   // is ill-formed.
00169   if (R.getNamingClass() &&
00170       contextClass->getCanonicalDecl() !=
00171         R.getNamingClass()->getCanonicalDecl() &&
00172       contextClass->isProvablyNotDerivedFrom(R.getNamingClass()))
00173     return hasNonInstance ? IMA_Mixed_Unrelated :
00174            IsCXX11UnevaluatedField ? IMA_Field_Uneval_Context :
00175                                      IMA_Error_Unrelated;
00176 
00177   // If we can prove that the current context is unrelated to all the
00178   // declaring classes, it can't be an implicit member reference (in
00179   // which case it's an error if any of those members are selected).
00180   if (IsProvablyNotDerivedFrom(SemaRef, contextClass, Classes))
00181     return hasNonInstance ? IMA_Mixed_Unrelated :
00182            IsCXX11UnevaluatedField ? IMA_Field_Uneval_Context :
00183                                      IMA_Error_Unrelated;
00184 
00185   return (hasNonInstance ? IMA_Mixed : IMA_Instance);
00186 }
00187 
00188 /// Diagnose a reference to a field with no object available.
00189 static void diagnoseInstanceReference(Sema &SemaRef,
00190                                       const CXXScopeSpec &SS,
00191                                       NamedDecl *Rep,
00192                                       const DeclarationNameInfo &nameInfo) {
00193   SourceLocation Loc = nameInfo.getLoc();
00194   SourceRange Range(Loc);
00195   if (SS.isSet()) Range.setBegin(SS.getRange().getBegin());
00196 
00197   DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext();
00198   CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FunctionLevelDC);
00199   CXXRecordDecl *ContextClass = Method ? Method->getParent() : 0;
00200   CXXRecordDecl *RepClass = dyn_cast<CXXRecordDecl>(Rep->getDeclContext());
00201 
00202   bool InStaticMethod = Method && Method->isStatic();
00203   bool IsField = isa<FieldDecl>(Rep) || isa<IndirectFieldDecl>(Rep);
00204 
00205   if (IsField && InStaticMethod)
00206     // "invalid use of member 'x' in static member function"
00207     SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method)
00208         << Range << nameInfo.getName();
00209   else if (ContextClass && RepClass && SS.isEmpty() && !InStaticMethod &&
00210            !RepClass->Equals(ContextClass) && RepClass->Encloses(ContextClass))
00211     // Unqualified lookup in a non-static member function found a member of an
00212     // enclosing class.
00213     SemaRef.Diag(Loc, diag::err_nested_non_static_member_use)
00214       << IsField << RepClass << nameInfo.getName() << ContextClass << Range;
00215   else if (IsField)
00216     SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use)
00217       << nameInfo.getName() << Range;
00218   else
00219     SemaRef.Diag(Loc, diag::err_member_call_without_object)
00220       << Range;
00221 }
00222 
00223 /// Builds an expression which might be an implicit member expression.
00224 ExprResult
00225 Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS,
00226                                       SourceLocation TemplateKWLoc,
00227                                       LookupResult &R,
00228                                 const TemplateArgumentListInfo *TemplateArgs) {
00229   switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) {
00230   case IMA_Instance:
00231     return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true);
00232 
00233   case IMA_Mixed:
00234   case IMA_Mixed_Unrelated:
00235   case IMA_Unresolved:
00236     return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, false);
00237 
00238   case IMA_Field_Uneval_Context:
00239     Diag(R.getNameLoc(), diag::warn_cxx98_compat_non_static_member_use)
00240       << R.getLookupNameInfo().getName();
00241     // Fall through.
00242   case IMA_Static:
00243   case IMA_Mixed_StaticContext:
00244   case IMA_Unresolved_StaticContext:
00245     if (TemplateArgs || TemplateKWLoc.isValid())
00246       return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs);
00247     return BuildDeclarationNameExpr(SS, R, false);
00248 
00249   case IMA_Error_StaticContext:
00250   case IMA_Error_Unrelated:
00251     diagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(),
00252                               R.getLookupNameInfo());
00253     return ExprError();
00254   }
00255 
00256   llvm_unreachable("unexpected instance member access kind");
00257 }
00258 
00259 /// Check an ext-vector component access expression.
00260 ///
00261 /// VK should be set in advance to the value kind of the base
00262 /// expression.
00263 static QualType
00264 CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK,
00265                         SourceLocation OpLoc, const IdentifierInfo *CompName,
00266                         SourceLocation CompLoc) {
00267   // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements,
00268   // see FIXME there.
00269   //
00270   // FIXME: This logic can be greatly simplified by splitting it along
00271   // halving/not halving and reworking the component checking.
00272   const ExtVectorType *vecType = baseType->getAs<ExtVectorType>();
00273 
00274   // The vector accessor can't exceed the number of elements.
00275   const char *compStr = CompName->getNameStart();
00276 
00277   // This flag determines whether or not the component is one of the four
00278   // special names that indicate a subset of exactly half the elements are
00279   // to be selected.
00280   bool HalvingSwizzle = false;
00281 
00282   // This flag determines whether or not CompName has an 's' char prefix,
00283   // indicating that it is a string of hex values to be used as vector indices.
00284   bool HexSwizzle = *compStr == 's' || *compStr == 'S';
00285 
00286   bool HasRepeated = false;
00287   bool HasIndex[16] = {};
00288 
00289   int Idx;
00290 
00291   // Check that we've found one of the special components, or that the component
00292   // names must come from the same set.
00293   if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
00294       !strcmp(compStr, "even") || !strcmp(compStr, "odd")) {
00295     HalvingSwizzle = true;
00296   } else if (!HexSwizzle &&
00297              (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) {
00298     do {
00299       if (HasIndex[Idx]) HasRepeated = true;
00300       HasIndex[Idx] = true;
00301       compStr++;
00302     } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1);
00303   } else {
00304     if (HexSwizzle) compStr++;
00305     while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) {
00306       if (HasIndex[Idx]) HasRepeated = true;
00307       HasIndex[Idx] = true;
00308       compStr++;
00309     }
00310   }
00311 
00312   if (!HalvingSwizzle && *compStr) {
00313     // We didn't get to the end of the string. This means the component names
00314     // didn't come from the same set *or* we encountered an illegal name.
00315     S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal)
00316       << StringRef(compStr, 1) << SourceRange(CompLoc);
00317     return QualType();
00318   }
00319 
00320   // Ensure no component accessor exceeds the width of the vector type it
00321   // operates on.
00322   if (!HalvingSwizzle) {
00323     compStr = CompName->getNameStart();
00324 
00325     if (HexSwizzle)
00326       compStr++;
00327 
00328     while (*compStr) {
00329       if (!vecType->isAccessorWithinNumElements(*compStr++)) {
00330         S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length)
00331           << baseType << SourceRange(CompLoc);
00332         return QualType();
00333       }
00334     }
00335   }
00336 
00337   // The component accessor looks fine - now we need to compute the actual type.
00338   // The vector type is implied by the component accessor. For example,
00339   // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
00340   // vec4.s0 is a float, vec4.s23 is a vec3, etc.
00341   // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
00342   unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2
00343                                      : CompName->getLength();
00344   if (HexSwizzle)
00345     CompSize--;
00346 
00347   if (CompSize == 1)
00348     return vecType->getElementType();
00349 
00350   if (HasRepeated) VK = VK_RValue;
00351 
00352   QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize);
00353   // Now look up the TypeDefDecl from the vector type. Without this,
00354   // diagostics look bad. We want extended vector types to appear built-in.
00355   for (Sema::ExtVectorDeclsType::iterator 
00356          I = S.ExtVectorDecls.begin(S.ExternalSource),
00357          E = S.ExtVectorDecls.end(); 
00358        I != E; ++I) {
00359     if ((*I)->getUnderlyingType() == VT)
00360       return S.Context.getTypedefType(*I);
00361   }
00362   
00363   return VT; // should never get here (a typedef type should always be found).
00364 }
00365 
00366 static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl,
00367                                                 IdentifierInfo *Member,
00368                                                 const Selector &Sel,
00369                                                 ASTContext &Context) {
00370   if (Member)
00371     if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member))
00372       return PD;
00373   if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel))
00374     return OMD;
00375 
00376   for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(),
00377        E = PDecl->protocol_end(); I != E; ++I) {
00378     if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel,
00379                                                            Context))
00380       return D;
00381   }
00382   return 0;
00383 }
00384 
00385 static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy,
00386                                       IdentifierInfo *Member,
00387                                       const Selector &Sel,
00388                                       ASTContext &Context) {
00389   // Check protocols on qualified interfaces.
00390   Decl *GDecl = 0;
00391   for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
00392        E = QIdTy->qual_end(); I != E; ++I) {
00393     if (Member)
00394       if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
00395         GDecl = PD;
00396         break;
00397       }
00398     // Also must look for a getter or setter name which uses property syntax.
00399     if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) {
00400       GDecl = OMD;
00401       break;
00402     }
00403   }
00404   if (!GDecl) {
00405     for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(),
00406          E = QIdTy->qual_end(); I != E; ++I) {
00407       // Search in the protocol-qualifier list of current protocol.
00408       GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, 
00409                                                        Context);
00410       if (GDecl)
00411         return GDecl;
00412     }
00413   }
00414   return GDecl;
00415 }
00416 
00417 ExprResult
00418 Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType,
00419                                bool IsArrow, SourceLocation OpLoc,
00420                                const CXXScopeSpec &SS,
00421                                SourceLocation TemplateKWLoc,
00422                                NamedDecl *FirstQualifierInScope,
00423                                const DeclarationNameInfo &NameInfo,
00424                                const TemplateArgumentListInfo *TemplateArgs) {
00425   // Even in dependent contexts, try to diagnose base expressions with
00426   // obviously wrong types, e.g.:
00427   //
00428   // T* t;
00429   // t.f;
00430   //
00431   // In Obj-C++, however, the above expression is valid, since it could be
00432   // accessing the 'f' property if T is an Obj-C interface. The extra check
00433   // allows this, while still reporting an error if T is a struct pointer.
00434   if (!IsArrow) {
00435     const PointerType *PT = BaseType->getAs<PointerType>();
00436     if (PT && (!getLangOpts().ObjC1 ||
00437                PT->getPointeeType()->isRecordType())) {
00438       assert(BaseExpr && "cannot happen with implicit member accesses");
00439       Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
00440         << BaseType << BaseExpr->getSourceRange() << NameInfo.getSourceRange();
00441       return ExprError();
00442     }
00443   }
00444 
00445   assert(BaseType->isDependentType() ||
00446          NameInfo.getName().isDependentName() ||
00447          isDependentScopeSpecifier(SS));
00448 
00449   // Get the type being accessed in BaseType.  If this is an arrow, the BaseExpr
00450   // must have pointer type, and the accessed type is the pointee.
00451   return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType,
00452                                                    IsArrow, OpLoc,
00453                                                SS.getWithLocInContext(Context),
00454                                                    TemplateKWLoc,
00455                                                    FirstQualifierInScope,
00456                                                    NameInfo, TemplateArgs));
00457 }
00458 
00459 /// We know that the given qualified member reference points only to
00460 /// declarations which do not belong to the static type of the base
00461 /// expression.  Diagnose the problem.
00462 static void DiagnoseQualifiedMemberReference(Sema &SemaRef,
00463                                              Expr *BaseExpr,
00464                                              QualType BaseType,
00465                                              const CXXScopeSpec &SS,
00466                                              NamedDecl *rep,
00467                                        const DeclarationNameInfo &nameInfo) {
00468   // If this is an implicit member access, use a different set of
00469   // diagnostics.
00470   if (!BaseExpr)
00471     return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo);
00472 
00473   SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated)
00474     << SS.getRange() << rep << BaseType;
00475 }
00476 
00477 // Check whether the declarations we found through a nested-name
00478 // specifier in a member expression are actually members of the base
00479 // type.  The restriction here is:
00480 //
00481 //   C++ [expr.ref]p2:
00482 //     ... In these cases, the id-expression shall name a
00483 //     member of the class or of one of its base classes.
00484 //
00485 // So it's perfectly legitimate for the nested-name specifier to name
00486 // an unrelated class, and for us to find an overload set including
00487 // decls from classes which are not superclasses, as long as the decl
00488 // we actually pick through overload resolution is from a superclass.
00489 bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr,
00490                                          QualType BaseType,
00491                                          const CXXScopeSpec &SS,
00492                                          const LookupResult &R) {
00493   const RecordType *BaseRT = BaseType->getAs<RecordType>();
00494   if (!BaseRT) {
00495     // We can't check this yet because the base type is still
00496     // dependent.
00497     assert(BaseType->isDependentType());
00498     return false;
00499   }
00500   CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl());
00501 
00502   for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
00503     // If this is an implicit member reference and we find a
00504     // non-instance member, it's not an error.
00505     if (!BaseExpr && !(*I)->isCXXInstanceMember())
00506       return false;
00507 
00508     // Note that we use the DC of the decl, not the underlying decl.
00509     DeclContext *DC = (*I)->getDeclContext();
00510     while (DC->isTransparentContext())
00511       DC = DC->getParent();
00512 
00513     if (!DC->isRecord())
00514       continue;
00515     
00516     llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord;
00517     MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl());
00518 
00519     if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord))
00520       return false;
00521   }
00522 
00523   DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS,
00524                                    R.getRepresentativeDecl(),
00525                                    R.getLookupNameInfo());
00526   return true;
00527 }
00528 
00529 namespace {
00530 
00531 // Callback to only accept typo corrections that are either a ValueDecl or a
00532 // FunctionTemplateDecl.
00533 class RecordMemberExprValidatorCCC : public CorrectionCandidateCallback {
00534  public:
00535   virtual bool ValidateCandidate(const TypoCorrection &candidate) {
00536     NamedDecl *ND = candidate.getCorrectionDecl();
00537     return ND && (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND));
00538   }
00539 };
00540 
00541 }
00542 
00543 static bool
00544 LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, 
00545                          SourceRange BaseRange, const RecordType *RTy,
00546                          SourceLocation OpLoc, CXXScopeSpec &SS,
00547                          bool HasTemplateArgs) {
00548   RecordDecl *RDecl = RTy->getDecl();
00549   if (!SemaRef.isThisOutsideMemberFunctionBody(QualType(RTy, 0)) &&
00550       SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0),
00551                                   diag::err_typecheck_incomplete_tag,
00552                                   BaseRange))
00553     return true;
00554 
00555   if (HasTemplateArgs) {
00556     // LookupTemplateName doesn't expect these both to exist simultaneously.
00557     QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0);
00558 
00559     bool MOUS;
00560     SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS);
00561     return false;
00562   }
00563 
00564   DeclContext *DC = RDecl;
00565   if (SS.isSet()) {
00566     // If the member name was a qualified-id, look into the
00567     // nested-name-specifier.
00568     DC = SemaRef.computeDeclContext(SS, false);
00569 
00570     if (SemaRef.RequireCompleteDeclContext(SS, DC)) {
00571       SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag)
00572         << SS.getRange() << DC;
00573       return true;
00574     }
00575 
00576     assert(DC && "Cannot handle non-computable dependent contexts in lookup");
00577 
00578     if (!isa<TypeDecl>(DC)) {
00579       SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass)
00580         << DC << SS.getRange();
00581       return true;
00582     }
00583   }
00584 
00585   // The record definition is complete, now look up the member.
00586   SemaRef.LookupQualifiedName(R, DC);
00587 
00588   if (!R.empty())
00589     return false;
00590 
00591   // We didn't find anything with the given name, so try to correct
00592   // for typos.
00593   DeclarationName Name = R.getLookupName();
00594   RecordMemberExprValidatorCCC Validator;
00595   TypoCorrection Corrected = SemaRef.CorrectTypo(R.getLookupNameInfo(),
00596                                                  R.getLookupKind(), NULL,
00597                                                  &SS, Validator, DC);
00598   R.clear();
00599   if (NamedDecl *ND = Corrected.getCorrectionDecl()) {
00600     std::string CorrectedStr(
00601         Corrected.getAsString(SemaRef.getLangOpts()));
00602     std::string CorrectedQuotedStr(
00603         Corrected.getQuoted(SemaRef.getLangOpts()));
00604     R.setLookupName(Corrected.getCorrection());
00605     R.addDecl(ND);
00606     SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest)
00607       << Name << DC << CorrectedQuotedStr << SS.getRange()
00608       << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr);
00609     SemaRef.Diag(ND->getLocation(), diag::note_previous_decl)
00610       << ND->getDeclName();
00611   }
00612 
00613   return false;
00614 }
00615 
00616 ExprResult
00617 Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType,
00618                                SourceLocation OpLoc, bool IsArrow,
00619                                CXXScopeSpec &SS,
00620                                SourceLocation TemplateKWLoc,
00621                                NamedDecl *FirstQualifierInScope,
00622                                const DeclarationNameInfo &NameInfo,
00623                                const TemplateArgumentListInfo *TemplateArgs) {
00624   if (BaseType->isDependentType() ||
00625       (SS.isSet() && isDependentScopeSpecifier(SS)))
00626     return ActOnDependentMemberExpr(Base, BaseType,
00627                                     IsArrow, OpLoc,
00628                                     SS, TemplateKWLoc, FirstQualifierInScope,
00629                                     NameInfo, TemplateArgs);
00630 
00631   LookupResult R(*this, NameInfo, LookupMemberName);
00632 
00633   // Implicit member accesses.
00634   if (!Base) {
00635     QualType RecordTy = BaseType;
00636     if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType();
00637     if (LookupMemberExprInRecord(*this, R, SourceRange(),
00638                                  RecordTy->getAs<RecordType>(),
00639                                  OpLoc, SS, TemplateArgs != 0))
00640       return ExprError();
00641 
00642   // Explicit member accesses.
00643   } else {
00644     ExprResult BaseResult = Owned(Base);
00645     ExprResult Result =
00646       LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
00647                        SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0);
00648 
00649     if (BaseResult.isInvalid())
00650       return ExprError();
00651     Base = BaseResult.take();
00652 
00653     if (Result.isInvalid()) {
00654       Owned(Base);
00655       return ExprError();
00656     }
00657 
00658     if (Result.get())
00659       return move(Result);
00660 
00661     // LookupMemberExpr can modify Base, and thus change BaseType
00662     BaseType = Base->getType();
00663   }
00664 
00665   return BuildMemberReferenceExpr(Base, BaseType,
00666                                   OpLoc, IsArrow, SS, TemplateKWLoc,
00667                                   FirstQualifierInScope, R, TemplateArgs);
00668 }
00669 
00670 static ExprResult
00671 BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
00672                         const CXXScopeSpec &SS, FieldDecl *Field,
00673                         DeclAccessPair FoundDecl,
00674                         const DeclarationNameInfo &MemberNameInfo);
00675 
00676 ExprResult
00677 Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS,
00678                                                SourceLocation loc,
00679                                                IndirectFieldDecl *indirectField,
00680                                                Expr *baseObjectExpr,
00681                                                SourceLocation opLoc) {
00682   // First, build the expression that refers to the base object.
00683   
00684   bool baseObjectIsPointer = false;
00685   Qualifiers baseQuals;
00686   
00687   // Case 1:  the base of the indirect field is not a field.
00688   VarDecl *baseVariable = indirectField->getVarDecl();
00689   CXXScopeSpec EmptySS;
00690   if (baseVariable) {
00691     assert(baseVariable->getType()->isRecordType());
00692     
00693     // In principle we could have a member access expression that
00694     // accesses an anonymous struct/union that's a static member of
00695     // the base object's class.  However, under the current standard,
00696     // static data members cannot be anonymous structs or unions.
00697     // Supporting this is as easy as building a MemberExpr here.
00698     assert(!baseObjectExpr && "anonymous struct/union is static data member?");
00699     
00700     DeclarationNameInfo baseNameInfo(DeclarationName(), loc);
00701     
00702     ExprResult result 
00703       = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable);
00704     if (result.isInvalid()) return ExprError();
00705     
00706     baseObjectExpr = result.take();    
00707     baseObjectIsPointer = false;
00708     baseQuals = baseObjectExpr->getType().getQualifiers();
00709     
00710     // Case 2: the base of the indirect field is a field and the user
00711     // wrote a member expression.
00712   } else if (baseObjectExpr) {
00713     // The caller provided the base object expression. Determine
00714     // whether its a pointer and whether it adds any qualifiers to the
00715     // anonymous struct/union fields we're looking into.
00716     QualType objectType = baseObjectExpr->getType();
00717     
00718     if (const PointerType *ptr = objectType->getAs<PointerType>()) {
00719       baseObjectIsPointer = true;
00720       objectType = ptr->getPointeeType();
00721     } else {
00722       baseObjectIsPointer = false;
00723     }
00724     baseQuals = objectType.getQualifiers();
00725     
00726     // Case 3: the base of the indirect field is a field and we should
00727     // build an implicit member access.
00728   } else {
00729     // We've found a member of an anonymous struct/union that is
00730     // inside a non-anonymous struct/union, so in a well-formed
00731     // program our base object expression is "this".
00732     QualType ThisTy = getCurrentThisType();
00733     if (ThisTy.isNull()) {
00734       Diag(loc, diag::err_invalid_member_use_in_static_method)
00735         << indirectField->getDeclName();
00736       return ExprError();
00737     }
00738     
00739     // Our base object expression is "this".
00740     CheckCXXThisCapture(loc);
00741     baseObjectExpr 
00742       = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true);
00743     baseObjectIsPointer = true;
00744     baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers();
00745   }
00746   
00747   // Build the implicit member references to the field of the
00748   // anonymous struct/union.
00749   Expr *result = baseObjectExpr;
00750   IndirectFieldDecl::chain_iterator
00751   FI = indirectField->chain_begin(), FEnd = indirectField->chain_end();
00752   
00753   // Build the first member access in the chain with full information.
00754   if (!baseVariable) {
00755     FieldDecl *field = cast<FieldDecl>(*FI);
00756     
00757     // FIXME: use the real found-decl info!
00758     DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess());
00759     
00760     // Make a nameInfo that properly uses the anonymous name.
00761     DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
00762     
00763     result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer,
00764                                      EmptySS, field, foundDecl,
00765                                      memberNameInfo).take();
00766     baseObjectIsPointer = false;
00767     
00768     // FIXME: check qualified member access
00769   }
00770   
00771   // In all cases, we should now skip the first declaration in the chain.
00772   ++FI;
00773   
00774   while (FI != FEnd) {
00775     FieldDecl *field = cast<FieldDecl>(*FI++);
00776     
00777     // FIXME: these are somewhat meaningless
00778     DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
00779     DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess());
00780     
00781     result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false,
00782                                      (FI == FEnd? SS : EmptySS), field, 
00783                                      foundDecl, memberNameInfo).take();
00784   }
00785   
00786   return Owned(result);
00787 }
00788 
00789 /// \brief Build a MemberExpr AST node.
00790 static MemberExpr *BuildMemberExpr(Sema &SemaRef,
00791                                    ASTContext &C, Expr *Base, bool isArrow,
00792                                    const CXXScopeSpec &SS,
00793                                    SourceLocation TemplateKWLoc,
00794                                    ValueDecl *Member,
00795                                    DeclAccessPair FoundDecl,
00796                                    const DeclarationNameInfo &MemberNameInfo,
00797                                    QualType Ty,
00798                                    ExprValueKind VK, ExprObjectKind OK,
00799                                    const TemplateArgumentListInfo *TemplateArgs = 0) {
00800   assert((!isArrow || Base->isRValue()) && "-> base must be a pointer rvalue");
00801   MemberExpr *E =
00802       MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C),
00803                          TemplateKWLoc, Member, FoundDecl, MemberNameInfo,
00804                          TemplateArgs, Ty, VK, OK);
00805   SemaRef.MarkMemberReferenced(E);
00806   return E;
00807 }
00808 
00809 ExprResult
00810 Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
00811                                SourceLocation OpLoc, bool IsArrow,
00812                                const CXXScopeSpec &SS,
00813                                SourceLocation TemplateKWLoc,
00814                                NamedDecl *FirstQualifierInScope,
00815                                LookupResult &R,
00816                                const TemplateArgumentListInfo *TemplateArgs,
00817                                bool SuppressQualifierCheck,
00818                                ActOnMemberAccessExtraArgs *ExtraArgs) {
00819   QualType BaseType = BaseExprType;
00820   if (IsArrow) {
00821     assert(BaseType->isPointerType());
00822     BaseType = BaseType->castAs<PointerType>()->getPointeeType();
00823   }
00824   R.setBaseObjectType(BaseType);
00825 
00826   const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo();
00827   DeclarationName MemberName = MemberNameInfo.getName();
00828   SourceLocation MemberLoc = MemberNameInfo.getLoc();
00829 
00830   if (R.isAmbiguous())
00831     return ExprError();
00832 
00833   if (R.empty()) {
00834     // Rederive where we looked up.
00835     DeclContext *DC = (SS.isSet()
00836                        ? computeDeclContext(SS, false)
00837                        : BaseType->getAs<RecordType>()->getDecl());
00838 
00839     if (ExtraArgs) {
00840       ExprResult RetryExpr;
00841       if (!IsArrow && BaseExpr) {
00842         SFINAETrap Trap(*this, true);
00843         ParsedType ObjectType;
00844         bool MayBePseudoDestructor = false;
00845         RetryExpr = ActOnStartCXXMemberReference(getCurScope(), BaseExpr,
00846                                                  OpLoc, tok::arrow, ObjectType,
00847                                                  MayBePseudoDestructor);
00848         if (RetryExpr.isUsable() && !Trap.hasErrorOccurred()) {
00849           CXXScopeSpec TempSS(SS);
00850           RetryExpr = ActOnMemberAccessExpr(
00851               ExtraArgs->S, RetryExpr.get(), OpLoc, tok::arrow, TempSS,
00852               TemplateKWLoc, ExtraArgs->Id, ExtraArgs->ObjCImpDecl,
00853               ExtraArgs->HasTrailingLParen);
00854         }
00855         if (Trap.hasErrorOccurred())
00856           RetryExpr = ExprError();
00857       }
00858       if (RetryExpr.isUsable()) {
00859         Diag(OpLoc, diag::err_no_member_overloaded_arrow)
00860           << MemberName << DC << FixItHint::CreateReplacement(OpLoc, "->");
00861         return RetryExpr;
00862       }
00863     }
00864 
00865     Diag(R.getNameLoc(), diag::err_no_member)
00866       << MemberName << DC
00867       << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange());
00868     return ExprError();
00869   }
00870 
00871   // Diagnose lookups that find only declarations from a non-base
00872   // type.  This is possible for either qualified lookups (which may
00873   // have been qualified with an unrelated type) or implicit member
00874   // expressions (which were found with unqualified lookup and thus
00875   // may have come from an enclosing scope).  Note that it's okay for
00876   // lookup to find declarations from a non-base type as long as those
00877   // aren't the ones picked by overload resolution.
00878   if ((SS.isSet() || !BaseExpr ||
00879        (isa<CXXThisExpr>(BaseExpr) &&
00880         cast<CXXThisExpr>(BaseExpr)->isImplicit())) &&
00881       !SuppressQualifierCheck &&
00882       CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R))
00883     return ExprError();
00884   
00885   // Construct an unresolved result if we in fact got an unresolved
00886   // result.
00887   if (R.isOverloadedResult() || R.isUnresolvableResult()) {
00888     // Suppress any lookup-related diagnostics; we'll do these when we
00889     // pick a member.
00890     R.suppressDiagnostics();
00891 
00892     UnresolvedMemberExpr *MemExpr
00893       = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(),
00894                                      BaseExpr, BaseExprType,
00895                                      IsArrow, OpLoc,
00896                                      SS.getWithLocInContext(Context),
00897                                      TemplateKWLoc, MemberNameInfo,
00898                                      TemplateArgs, R.begin(), R.end());
00899 
00900     return Owned(MemExpr);
00901   }
00902 
00903   assert(R.isSingleResult());
00904   DeclAccessPair FoundDecl = R.begin().getPair();
00905   NamedDecl *MemberDecl = R.getFoundDecl();
00906 
00907   // FIXME: diagnose the presence of template arguments now.
00908 
00909   // If the decl being referenced had an error, return an error for this
00910   // sub-expr without emitting another error, in order to avoid cascading
00911   // error cases.
00912   if (MemberDecl->isInvalidDecl())
00913     return ExprError();
00914 
00915   // Handle the implicit-member-access case.
00916   if (!BaseExpr) {
00917     // If this is not an instance member, convert to a non-member access.
00918     if (!MemberDecl->isCXXInstanceMember())
00919       return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl);
00920 
00921     SourceLocation Loc = R.getNameLoc();
00922     if (SS.getRange().isValid())
00923       Loc = SS.getRange().getBegin();
00924     CheckCXXThisCapture(Loc);
00925     BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true);
00926   }
00927 
00928   bool ShouldCheckUse = true;
00929   if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) {
00930     // Don't diagnose the use of a virtual member function unless it's
00931     // explicitly qualified.
00932     if (MD->isVirtual() && !SS.isSet())
00933       ShouldCheckUse = false;
00934   }
00935 
00936   // Check the use of this member.
00937   if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) {
00938     Owned(BaseExpr);
00939     return ExprError();
00940   }
00941 
00942   if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl))
00943     return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow,
00944                                    SS, FD, FoundDecl, MemberNameInfo);
00945 
00946   if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl))
00947     // We may have found a field within an anonymous union or struct
00948     // (C++ [class.union]).
00949     return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD,
00950                                                     BaseExpr, OpLoc);
00951 
00952   if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) {
00953     return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
00954                                  TemplateKWLoc, Var, FoundDecl, MemberNameInfo,
00955                                  Var->getType().getNonReferenceType(),
00956                                  VK_LValue, OK_Ordinary));
00957   }
00958 
00959   if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) {
00960     ExprValueKind valueKind;
00961     QualType type;
00962     if (MemberFn->isInstance()) {
00963       valueKind = VK_RValue;
00964       type = Context.BoundMemberTy;
00965     } else {
00966       valueKind = VK_LValue;
00967       type = MemberFn->getType();
00968     }
00969 
00970     return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS, 
00971                                  TemplateKWLoc, MemberFn, FoundDecl, 
00972                                  MemberNameInfo, type, valueKind,
00973                                  OK_Ordinary));
00974   }
00975   assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?");
00976 
00977   if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) {
00978     return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
00979                                  TemplateKWLoc, Enum, FoundDecl, MemberNameInfo,
00980                                  Enum->getType(), VK_RValue, OK_Ordinary));
00981   }
00982 
00983   Owned(BaseExpr);
00984 
00985   // We found something that we didn't expect. Complain.
00986   if (isa<TypeDecl>(MemberDecl))
00987     Diag(MemberLoc, diag::err_typecheck_member_reference_type)
00988       << MemberName << BaseType << int(IsArrow);
00989   else
00990     Diag(MemberLoc, diag::err_typecheck_member_reference_unknown)
00991       << MemberName << BaseType << int(IsArrow);
00992 
00993   Diag(MemberDecl->getLocation(), diag::note_member_declared_here)
00994     << MemberName;
00995   R.suppressDiagnostics();
00996   return ExprError();
00997 }
00998 
00999 /// Given that normal member access failed on the given expression,
01000 /// and given that the expression's type involves builtin-id or
01001 /// builtin-Class, decide whether substituting in the redefinition
01002 /// types would be profitable.  The redefinition type is whatever
01003 /// this translation unit tried to typedef to id/Class;  we store
01004 /// it to the side and then re-use it in places like this.
01005 static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
01006   const ObjCObjectPointerType *opty
01007     = base.get()->getType()->getAs<ObjCObjectPointerType>();
01008   if (!opty) return false;
01009 
01010   const ObjCObjectType *ty = opty->getObjectType();
01011 
01012   QualType redef;
01013   if (ty->isObjCId()) {
01014     redef = S.Context.getObjCIdRedefinitionType();
01015   } else if (ty->isObjCClass()) {
01016     redef = S.Context.getObjCClassRedefinitionType();
01017   } else {
01018     return false;
01019   }
01020 
01021   // Do the substitution as long as the redefinition type isn't just a
01022   // possibly-qualified pointer to builtin-id or builtin-Class again.
01023   opty = redef->getAs<ObjCObjectPointerType>();
01024   if (opty && !opty->getObjectType()->getInterface() != 0)
01025     return false;
01026 
01027   base = S.ImpCastExprToType(base.take(), redef, CK_BitCast);
01028   return true;
01029 }
01030 
01031 static bool isRecordType(QualType T) {
01032   return T->isRecordType();
01033 }
01034 static bool isPointerToRecordType(QualType T) {
01035   if (const PointerType *PT = T->getAs<PointerType>())
01036     return PT->getPointeeType()->isRecordType();
01037   return false;
01038 }
01039 
01040 /// Perform conversions on the LHS of a member access expression.
01041 ExprResult
01042 Sema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) {
01043   if (IsArrow && !Base->getType()->isFunctionType())
01044     return DefaultFunctionArrayLvalueConversion(Base);
01045 
01046   return CheckPlaceholderExpr(Base);
01047 }
01048 
01049 /// Look up the given member of the given non-type-dependent
01050 /// expression.  This can return in one of two ways:
01051 ///  * If it returns a sentinel null-but-valid result, the caller will
01052 ///    assume that lookup was performed and the results written into
01053 ///    the provided structure.  It will take over from there.
01054 ///  * Otherwise, the returned expression will be produced in place of
01055 ///    an ordinary member expression.
01056 ///
01057 /// The ObjCImpDecl bit is a gross hack that will need to be properly
01058 /// fixed for ObjC++.
01059 ExprResult
01060 Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
01061                        bool &IsArrow, SourceLocation OpLoc,
01062                        CXXScopeSpec &SS,
01063                        Decl *ObjCImpDecl, bool HasTemplateArgs) {
01064   assert(BaseExpr.get() && "no base expression");
01065 
01066   // Perform default conversions.
01067   BaseExpr = PerformMemberExprBaseConversion(BaseExpr.take(), IsArrow);
01068   if (BaseExpr.isInvalid())
01069     return ExprError();
01070 
01071   QualType BaseType = BaseExpr.get()->getType();
01072   assert(!BaseType->isDependentType());
01073 
01074   DeclarationName MemberName = R.getLookupName();
01075   SourceLocation MemberLoc = R.getNameLoc();
01076 
01077   // For later type-checking purposes, turn arrow accesses into dot
01078   // accesses.  The only access type we support that doesn't follow
01079   // the C equivalence "a->b === (*a).b" is ObjC property accesses,
01080   // and those never use arrows, so this is unaffected.
01081   if (IsArrow) {
01082     if (const PointerType *Ptr = BaseType->getAs<PointerType>())
01083       BaseType = Ptr->getPointeeType();
01084     else if (const ObjCObjectPointerType *Ptr
01085                = BaseType->getAs<ObjCObjectPointerType>())
01086       BaseType = Ptr->getPointeeType();
01087     else if (BaseType->isRecordType()) {
01088       // Recover from arrow accesses to records, e.g.:
01089       //   struct MyRecord foo;
01090       //   foo->bar
01091       // This is actually well-formed in C++ if MyRecord has an
01092       // overloaded operator->, but that should have been dealt with
01093       // by now.
01094       Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
01095         << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
01096         << FixItHint::CreateReplacement(OpLoc, ".");
01097       IsArrow = false;
01098     } else if (BaseType->isFunctionType()) {
01099       goto fail;
01100     } else {
01101       Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
01102         << BaseType << BaseExpr.get()->getSourceRange();
01103       return ExprError();
01104     }
01105   }
01106 
01107   // Handle field access to simple records.
01108   if (const RecordType *RTy = BaseType->getAs<RecordType>()) {
01109     if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(),
01110                                  RTy, OpLoc, SS, HasTemplateArgs))
01111       return ExprError();
01112 
01113     // Returning valid-but-null is how we indicate to the caller that
01114     // the lookup result was filled in.
01115     return Owned((Expr*) 0);
01116   }
01117 
01118   // Handle ivar access to Objective-C objects.
01119   if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) {
01120     if (!SS.isEmpty() && !SS.isInvalid()) {
01121       Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
01122         << 1 << SS.getScopeRep()
01123         << FixItHint::CreateRemoval(SS.getRange());
01124       SS.clear();
01125     }
01126     
01127     IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
01128 
01129     // There are three cases for the base type:
01130     //   - builtin id (qualified or unqualified)
01131     //   - builtin Class (qualified or unqualified)
01132     //   - an interface
01133     ObjCInterfaceDecl *IDecl = OTy->getInterface();
01134     if (!IDecl) {
01135       if (getLangOpts().ObjCAutoRefCount &&
01136           (OTy->isObjCId() || OTy->isObjCClass()))
01137         goto fail;
01138       // There's an implicit 'isa' ivar on all objects.
01139       // But we only actually find it this way on objects of type 'id',
01140       // apparently.ghjg
01141       if (OTy->isObjCId() && Member->isStr("isa")) {
01142         Diag(MemberLoc, diag::warn_objc_isa_use);
01143         return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc,
01144                                                Context.getObjCClassType()));
01145       }
01146 
01147       if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
01148         return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
01149                                 ObjCImpDecl, HasTemplateArgs);
01150       goto fail;
01151     }
01152 
01153     if (RequireCompleteType(OpLoc, BaseType, diag::err_typecheck_incomplete_tag,
01154                             BaseExpr.get()))
01155       return ExprError();
01156     
01157     ObjCInterfaceDecl *ClassDeclared = 0;
01158     ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared);
01159 
01160     if (!IV) {
01161       // Attempt to correct for typos in ivar names.
01162       DeclFilterCCC<ObjCIvarDecl> Validator;
01163       Validator.IsObjCIvarLookup = IsArrow;
01164       if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
01165                                                  LookupMemberName, NULL, NULL,
01166                                                  Validator, IDecl)) {
01167         IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>();
01168         Diag(R.getNameLoc(),
01169              diag::err_typecheck_member_reference_ivar_suggest)
01170           << IDecl->getDeclName() << MemberName << IV->getDeclName()
01171           << FixItHint::CreateReplacement(R.getNameLoc(),
01172                                           IV->getNameAsString());
01173         Diag(IV->getLocation(), diag::note_previous_decl)
01174           << IV->getDeclName();
01175         
01176         // Figure out the class that declares the ivar.
01177         assert(!ClassDeclared);
01178         Decl *D = cast<Decl>(IV->getDeclContext());
01179         if (ObjCCategoryDecl *CAT = dyn_cast<ObjCCategoryDecl>(D))
01180           D = CAT->getClassInterface();
01181         ClassDeclared = cast<ObjCInterfaceDecl>(D);
01182       } else {
01183         if (IsArrow && IDecl->FindPropertyDeclaration(Member)) {
01184           Diag(MemberLoc, 
01185           diag::err_property_found_suggest)
01186           << Member << BaseExpr.get()->getType()
01187           << FixItHint::CreateReplacement(OpLoc, ".");
01188           return ExprError();
01189         }
01190 
01191         Diag(MemberLoc, diag::err_typecheck_member_reference_ivar)
01192           << IDecl->getDeclName() << MemberName
01193           << BaseExpr.get()->getSourceRange();
01194         return ExprError();
01195       }
01196     }
01197     
01198     assert(ClassDeclared);
01199 
01200     // If the decl being referenced had an error, return an error for this
01201     // sub-expr without emitting another error, in order to avoid cascading
01202     // error cases.
01203     if (IV->isInvalidDecl())
01204       return ExprError();
01205 
01206     // Check whether we can reference this field.
01207     if (DiagnoseUseOfDecl(IV, MemberLoc))
01208       return ExprError();
01209     if (IV->getAccessControl() != ObjCIvarDecl::Public &&
01210         IV->getAccessControl() != ObjCIvarDecl::Package) {
01211       ObjCInterfaceDecl *ClassOfMethodDecl = 0;
01212       if (ObjCMethodDecl *MD = getCurMethodDecl())
01213         ClassOfMethodDecl =  MD->getClassInterface();
01214       else if (ObjCImpDecl && getCurFunctionDecl()) {
01215         // Case of a c-function declared inside an objc implementation.
01216         // FIXME: For a c-style function nested inside an objc implementation
01217         // class, there is no implementation context available, so we pass
01218         // down the context as argument to this routine. Ideally, this context
01219         // need be passed down in the AST node and somehow calculated from the
01220         // AST for a function decl.
01221         if (ObjCImplementationDecl *IMPD =
01222               dyn_cast<ObjCImplementationDecl>(ObjCImpDecl))
01223           ClassOfMethodDecl = IMPD->getClassInterface();
01224         else if (ObjCCategoryImplDecl* CatImplClass =
01225                    dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl))
01226           ClassOfMethodDecl = CatImplClass->getClassInterface();
01227       }
01228       if (!getLangOpts().DebuggerSupport) {
01229         if (IV->getAccessControl() == ObjCIvarDecl::Private) {
01230           if (!declaresSameEntity(ClassDeclared, IDecl) ||
01231               !declaresSameEntity(ClassOfMethodDecl, ClassDeclared))
01232             Diag(MemberLoc, diag::error_private_ivar_access)
01233               << IV->getDeclName();
01234         } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl))
01235           // @protected
01236           Diag(MemberLoc, diag::error_protected_ivar_access)
01237             << IV->getDeclName();
01238       }
01239     }
01240     if (getLangOpts().ObjCAutoRefCount) {
01241       Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts();
01242       if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp))
01243         if (UO->getOpcode() == UO_Deref)
01244           BaseExp = UO->getSubExpr()->IgnoreParenCasts();
01245       
01246       if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp))
01247         if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
01248           Diag(DE->getLocation(), diag::error_arc_weak_ivar_access);
01249     }
01250 
01251     return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(),
01252                                                MemberLoc, BaseExpr.take(),
01253                                                IsArrow));
01254   }
01255 
01256   // Objective-C property access.
01257   const ObjCObjectPointerType *OPT;
01258   if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) {
01259     if (!SS.isEmpty() && !SS.isInvalid()) {
01260       Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
01261         << 0 << SS.getScopeRep()
01262         << FixItHint::CreateRemoval(SS.getRange());
01263       SS.clear();
01264     }
01265 
01266     // This actually uses the base as an r-value.
01267     BaseExpr = DefaultLvalueConversion(BaseExpr.take());
01268     if (BaseExpr.isInvalid())
01269       return ExprError();
01270 
01271     assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType()));
01272 
01273     IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
01274 
01275     const ObjCObjectType *OT = OPT->getObjectType();
01276 
01277     // id, with and without qualifiers.
01278     if (OT->isObjCId()) {
01279       // Check protocols on qualified interfaces.
01280       Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
01281       if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) {
01282         if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) {
01283           // Check the use of this declaration
01284           if (DiagnoseUseOfDecl(PD, MemberLoc))
01285             return ExprError();
01286 
01287           return Owned(new (Context) ObjCPropertyRefExpr(PD,
01288                                                          Context.PseudoObjectTy,
01289                                                          VK_LValue,
01290                                                          OK_ObjCProperty,
01291                                                          MemberLoc, 
01292                                                          BaseExpr.take()));
01293         }
01294 
01295         if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) {
01296           // Check the use of this method.
01297           if (DiagnoseUseOfDecl(OMD, MemberLoc))
01298             return ExprError();
01299           Selector SetterSel =
01300             SelectorTable::constructSetterName(PP.getIdentifierTable(),
01301                                                PP.getSelectorTable(), Member);
01302           ObjCMethodDecl *SMD = 0;
01303           if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, 
01304                                                      SetterSel, Context))
01305             SMD = dyn_cast<ObjCMethodDecl>(SDecl);
01306           
01307           return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD,
01308                                                          Context.PseudoObjectTy,
01309                                                          VK_LValue, OK_ObjCProperty,
01310                                                          MemberLoc, BaseExpr.take()));
01311         }
01312       }
01313       // Use of id.member can only be for a property reference. Do not
01314       // use the 'id' redefinition in this case.
01315       if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
01316         return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
01317                                 ObjCImpDecl, HasTemplateArgs);
01318 
01319       return ExprError(Diag(MemberLoc, diag::err_property_not_found)
01320                          << MemberName << BaseType);
01321     }
01322 
01323     // 'Class', unqualified only.
01324     if (OT->isObjCClass()) {
01325       // Only works in a method declaration (??!).
01326       ObjCMethodDecl *MD = getCurMethodDecl();
01327       if (!MD) {
01328         if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
01329           return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
01330                                   ObjCImpDecl, HasTemplateArgs);
01331 
01332         goto fail;
01333       }
01334 
01335       // Also must look for a getter name which uses property syntax.
01336       Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
01337       ObjCInterfaceDecl *IFace = MD->getClassInterface();
01338       ObjCMethodDecl *Getter;
01339       if ((Getter = IFace->lookupClassMethod(Sel))) {
01340         // Check the use of this method.
01341         if (DiagnoseUseOfDecl(Getter, MemberLoc))
01342           return ExprError();
01343       } else
01344         Getter = IFace->lookupPrivateMethod(Sel, false);
01345       // If we found a getter then this may be a valid dot-reference, we
01346       // will look for the matching setter, in case it is needed.
01347       Selector SetterSel =
01348         SelectorTable::constructSetterName(PP.getIdentifierTable(),
01349                                            PP.getSelectorTable(), Member);
01350       ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
01351       if (!Setter) {
01352         // If this reference is in an @implementation, also check for 'private'
01353         // methods.
01354         Setter = IFace->lookupPrivateMethod(SetterSel, false);
01355       }
01356       // Look through local category implementations associated with the class.
01357       if (!Setter)
01358         Setter = IFace->getCategoryClassMethod(SetterSel);
01359 
01360       if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
01361         return ExprError();
01362 
01363       if (Getter || Setter) {
01364         return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
01365                                                        Context.PseudoObjectTy,
01366                                                        VK_LValue, OK_ObjCProperty,
01367                                                        MemberLoc, BaseExpr.take()));
01368       }
01369 
01370       if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
01371         return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
01372                                 ObjCImpDecl, HasTemplateArgs);
01373 
01374       return ExprError(Diag(MemberLoc, diag::err_property_not_found)
01375                          << MemberName << BaseType);
01376     }
01377 
01378     // Normal property access.
01379     return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc, 
01380                                      MemberName, MemberLoc,
01381                                      SourceLocation(), QualType(), false);
01382   }
01383 
01384   // Handle 'field access' to vectors, such as 'V.xx'.
01385   if (BaseType->isExtVectorType()) {
01386     // FIXME: this expr should store IsArrow.
01387     IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
01388     ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind());
01389     QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc,
01390                                            Member, MemberLoc);
01391     if (ret.isNull())
01392       return ExprError();
01393 
01394     return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(),
01395                                                     *Member, MemberLoc));
01396   }
01397 
01398   // Adjust builtin-sel to the appropriate redefinition type if that's
01399   // not just a pointer to builtin-sel again.
01400   if (IsArrow &&
01401       BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) &&
01402       !Context.getObjCSelRedefinitionType()->isObjCSelType()) {
01403     BaseExpr = ImpCastExprToType(BaseExpr.take(), 
01404                                  Context.getObjCSelRedefinitionType(),
01405                                  CK_BitCast);
01406     return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
01407                             ObjCImpDecl, HasTemplateArgs);
01408   }
01409 
01410   // Failure cases.
01411  fail:
01412 
01413   // Recover from dot accesses to pointers, e.g.:
01414   //   type *foo;
01415   //   foo.bar
01416   // This is actually well-formed in two cases:
01417   //   - 'type' is an Objective C type
01418   //   - 'bar' is a pseudo-destructor name which happens to refer to
01419   //     the appropriate pointer type
01420   if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
01421     if (!IsArrow && Ptr->getPointeeType()->isRecordType() &&
01422         MemberName.getNameKind() != DeclarationName::CXXDestructorName) {
01423       Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
01424         << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
01425           << FixItHint::CreateReplacement(OpLoc, "->");
01426 
01427       // Recurse as an -> access.
01428       IsArrow = true;
01429       return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
01430                               ObjCImpDecl, HasTemplateArgs);
01431     }
01432   }
01433 
01434   // If the user is trying to apply -> or . to a function name, it's probably
01435   // because they forgot parentheses to call that function.
01436   if (tryToRecoverWithCall(BaseExpr,
01437                            PDiag(diag::err_member_reference_needs_call),
01438                            /*complain*/ false,
01439                            IsArrow ? &isPointerToRecordType : &isRecordType)) {
01440     if (BaseExpr.isInvalid())
01441       return ExprError();
01442     BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take());
01443     return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
01444                             ObjCImpDecl, HasTemplateArgs);
01445   }
01446 
01447   Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
01448     << BaseType << BaseExpr.get()->getSourceRange() << MemberLoc;
01449 
01450   return ExprError();
01451 }
01452 
01453 /// The main callback when the parser finds something like
01454 ///   expression . [nested-name-specifier] identifier
01455 ///   expression -> [nested-name-specifier] identifier
01456 /// where 'identifier' encompasses a fairly broad spectrum of
01457 /// possibilities, including destructor and operator references.
01458 ///
01459 /// \param OpKind either tok::arrow or tok::period
01460 /// \param HasTrailingLParen whether the next token is '(', which
01461 ///   is used to diagnose mis-uses of special members that can
01462 ///   only be called
01463 /// \param ObjCImpDecl the current ObjC @implementation decl;
01464 ///   this is an ugly hack around the fact that ObjC @implementations
01465 ///   aren't properly put in the context chain
01466 ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base,
01467                                        SourceLocation OpLoc,
01468                                        tok::TokenKind OpKind,
01469                                        CXXScopeSpec &SS,
01470                                        SourceLocation TemplateKWLoc,
01471                                        UnqualifiedId &Id,
01472                                        Decl *ObjCImpDecl,
01473                                        bool HasTrailingLParen) {
01474   if (SS.isSet() && SS.isInvalid())
01475     return ExprError();
01476 
01477   // Warn about the explicit constructor calls Microsoft extension.
01478   if (getLangOpts().MicrosoftExt &&
01479       Id.getKind() == UnqualifiedId::IK_ConstructorName)
01480     Diag(Id.getSourceRange().getBegin(),
01481          diag::ext_ms_explicit_constructor_call);
01482 
01483   TemplateArgumentListInfo TemplateArgsBuffer;
01484 
01485   // Decompose the name into its component parts.
01486   DeclarationNameInfo NameInfo;
01487   const TemplateArgumentListInfo *TemplateArgs;
01488   DecomposeUnqualifiedId(Id, TemplateArgsBuffer,
01489                          NameInfo, TemplateArgs);
01490 
01491   DeclarationName Name = NameInfo.getName();
01492   bool IsArrow = (OpKind == tok::arrow);
01493 
01494   NamedDecl *FirstQualifierInScope
01495     = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S,
01496                        static_cast<NestedNameSpecifier*>(SS.getScopeRep())));
01497 
01498   // This is a postfix expression, so get rid of ParenListExprs.
01499   ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
01500   if (Result.isInvalid()) return ExprError();
01501   Base = Result.take();
01502 
01503   if (Base->getType()->isDependentType() || Name.isDependentName() ||
01504       isDependentScopeSpecifier(SS)) {
01505     Result = ActOnDependentMemberExpr(Base, Base->getType(),
01506                                       IsArrow, OpLoc,
01507                                       SS, TemplateKWLoc, FirstQualifierInScope,
01508                                       NameInfo, TemplateArgs);
01509   } else {
01510     LookupResult R(*this, NameInfo, LookupMemberName);
01511     ExprResult BaseResult = Owned(Base);
01512     Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
01513                               SS, ObjCImpDecl, TemplateArgs != 0);
01514     if (BaseResult.isInvalid())
01515       return ExprError();
01516     Base = BaseResult.take();
01517 
01518     if (Result.isInvalid()) {
01519       Owned(Base);
01520       return ExprError();
01521     }
01522 
01523     if (Result.get()) {
01524       // The only way a reference to a destructor can be used is to
01525       // immediately call it, which falls into this case.  If the
01526       // next token is not a '(', produce a diagnostic and build the
01527       // call now.
01528       if (!HasTrailingLParen &&
01529           Id.getKind() == UnqualifiedId::IK_DestructorName)
01530         return DiagnoseDtorReference(NameInfo.getLoc(), Result.get());
01531 
01532       return move(Result);
01533     }
01534 
01535     ActOnMemberAccessExtraArgs ExtraArgs = {S, Id, ObjCImpDecl, HasTrailingLParen};
01536     Result = BuildMemberReferenceExpr(Base, Base->getType(),
01537                                       OpLoc, IsArrow, SS, TemplateKWLoc,
01538                                       FirstQualifierInScope, R, TemplateArgs,
01539                                       false, &ExtraArgs);
01540   }
01541 
01542   return move(Result);
01543 }
01544 
01545 static ExprResult
01546 BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
01547                         const CXXScopeSpec &SS, FieldDecl *Field,
01548                         DeclAccessPair FoundDecl,
01549                         const DeclarationNameInfo &MemberNameInfo) {
01550   // x.a is an l-value if 'a' has a reference type. Otherwise:
01551   // x.a is an l-value/x-value/pr-value if the base is (and note
01552   //   that *x is always an l-value), except that if the base isn't
01553   //   an ordinary object then we must have an rvalue.
01554   ExprValueKind VK = VK_LValue;
01555   ExprObjectKind OK = OK_Ordinary;
01556   if (!IsArrow) {
01557     if (BaseExpr->getObjectKind() == OK_Ordinary)
01558       VK = BaseExpr->getValueKind();
01559     else
01560       VK = VK_RValue;
01561   }
01562   if (VK != VK_RValue && Field->isBitField())
01563     OK = OK_BitField;
01564   
01565   // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref]
01566   QualType MemberType = Field->getType();
01567   if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) {
01568     MemberType = Ref->getPointeeType();
01569     VK = VK_LValue;
01570   } else {
01571     QualType BaseType = BaseExpr->getType();
01572     if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType();
01573     
01574     Qualifiers BaseQuals = BaseType.getQualifiers();
01575     
01576     // GC attributes are never picked up by members.
01577     BaseQuals.removeObjCGCAttr();
01578     
01579     // CVR attributes from the base are picked up by members,
01580     // except that 'mutable' members don't pick up 'const'.
01581     if (Field->isMutable()) BaseQuals.removeConst();
01582     
01583     Qualifiers MemberQuals
01584     = S.Context.getCanonicalType(MemberType).getQualifiers();
01585     
01586     // TR 18037 does not allow fields to be declared with address spaces.
01587     assert(!MemberQuals.hasAddressSpace());
01588     
01589     Qualifiers Combined = BaseQuals + MemberQuals;
01590     if (Combined != MemberQuals)
01591       MemberType = S.Context.getQualifiedType(MemberType, Combined);
01592   }
01593   
01594   ExprResult Base =
01595   S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
01596                                   FoundDecl, Field);
01597   if (Base.isInvalid())
01598     return ExprError();
01599   return S.Owned(BuildMemberExpr(S, S.Context, Base.take(), IsArrow, SS,
01600                                  /*TemplateKWLoc=*/SourceLocation(),
01601                                  Field, FoundDecl, MemberNameInfo,
01602                                  MemberType, VK, OK));
01603 }
01604 
01605 /// Builds an implicit member access expression.  The current context
01606 /// is known to be an instance method, and the given unqualified lookup
01607 /// set is known to contain only instance members, at least one of which
01608 /// is from an appropriate type.
01609 ExprResult
01610 Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
01611                               SourceLocation TemplateKWLoc,
01612                               LookupResult &R,
01613                               const TemplateArgumentListInfo *TemplateArgs,
01614                               bool IsKnownInstance) {
01615   assert(!R.empty() && !R.isAmbiguous());
01616   
01617   SourceLocation loc = R.getNameLoc();
01618   
01619   // We may have found a field within an anonymous union or struct
01620   // (C++ [class.union]).
01621   // FIXME: template-ids inside anonymous structs?
01622   if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>())
01623     return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD);
01624   
01625   // If this is known to be an instance access, go ahead and build an
01626   // implicit 'this' expression now.
01627   // 'this' expression now.
01628   QualType ThisTy = getCurrentThisType();
01629   assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'");
01630   
01631   Expr *baseExpr = 0; // null signifies implicit access
01632   if (IsKnownInstance) {
01633     SourceLocation Loc = R.getNameLoc();
01634     if (SS.getRange().isValid())
01635       Loc = SS.getRange().getBegin();
01636     CheckCXXThisCapture(Loc);
01637     baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true);
01638   }
01639   
01640   return BuildMemberReferenceExpr(baseExpr, ThisTy,
01641                                   /*OpLoc*/ SourceLocation(),
01642                                   /*IsArrow*/ true,
01643                                   SS, TemplateKWLoc,
01644                                   /*FirstQualifierInScope*/ 0,
01645                                   R, TemplateArgs);
01646 }