1f4a2713aSLionel Sambuc //===--- SemaExprMember.cpp - Semantic Analysis for Expressions -----------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements semantic analysis member access expressions.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13*0a6a1f1dSLionel Sambuc #include "clang/Sema/Overload.h"
14f4a2713aSLionel Sambuc #include "clang/AST/ASTLambda.h"
15f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
16f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclTemplate.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
19f4a2713aSLionel Sambuc #include "clang/AST/ExprObjC.h"
20f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
21f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
22f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
23f4a2713aSLionel Sambuc #include "clang/Sema/ScopeInfo.h"
24*0a6a1f1dSLionel Sambuc #include "clang/Sema/SemaInternal.h"
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc using namespace clang;
27f4a2713aSLionel Sambuc using namespace sema;
28f4a2713aSLionel Sambuc
29f4a2713aSLionel Sambuc typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> BaseSet;
BaseIsNotInSet(const CXXRecordDecl * Base,void * BasesPtr)30f4a2713aSLionel Sambuc static bool BaseIsNotInSet(const CXXRecordDecl *Base, void *BasesPtr) {
31f4a2713aSLionel Sambuc const BaseSet &Bases = *reinterpret_cast<const BaseSet*>(BasesPtr);
32f4a2713aSLionel Sambuc return !Bases.count(Base->getCanonicalDecl());
33f4a2713aSLionel Sambuc }
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc /// Determines if the given class is provably not derived from all of
36f4a2713aSLionel Sambuc /// the prospective base classes.
isProvablyNotDerivedFrom(Sema & SemaRef,CXXRecordDecl * Record,const BaseSet & Bases)37f4a2713aSLionel Sambuc static bool isProvablyNotDerivedFrom(Sema &SemaRef, CXXRecordDecl *Record,
38f4a2713aSLionel Sambuc const BaseSet &Bases) {
39f4a2713aSLionel Sambuc void *BasesPtr = const_cast<void*>(reinterpret_cast<const void*>(&Bases));
40f4a2713aSLionel Sambuc return BaseIsNotInSet(Record, BasesPtr) &&
41f4a2713aSLionel Sambuc Record->forallBases(BaseIsNotInSet, BasesPtr);
42f4a2713aSLionel Sambuc }
43f4a2713aSLionel Sambuc
44f4a2713aSLionel Sambuc enum IMAKind {
45f4a2713aSLionel Sambuc /// The reference is definitely not an instance member access.
46f4a2713aSLionel Sambuc IMA_Static,
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc /// The reference may be an implicit instance member access.
49f4a2713aSLionel Sambuc IMA_Mixed,
50f4a2713aSLionel Sambuc
51f4a2713aSLionel Sambuc /// The reference may be to an instance member, but it might be invalid if
52f4a2713aSLionel Sambuc /// so, because the context is not an instance method.
53f4a2713aSLionel Sambuc IMA_Mixed_StaticContext,
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc /// The reference may be to an instance member, but it is invalid if
56f4a2713aSLionel Sambuc /// so, because the context is from an unrelated class.
57f4a2713aSLionel Sambuc IMA_Mixed_Unrelated,
58f4a2713aSLionel Sambuc
59f4a2713aSLionel Sambuc /// The reference is definitely an implicit instance member access.
60f4a2713aSLionel Sambuc IMA_Instance,
61f4a2713aSLionel Sambuc
62f4a2713aSLionel Sambuc /// The reference may be to an unresolved using declaration.
63f4a2713aSLionel Sambuc IMA_Unresolved,
64f4a2713aSLionel Sambuc
65f4a2713aSLionel Sambuc /// The reference is a contextually-permitted abstract member reference.
66f4a2713aSLionel Sambuc IMA_Abstract,
67f4a2713aSLionel Sambuc
68f4a2713aSLionel Sambuc /// The reference may be to an unresolved using declaration and the
69f4a2713aSLionel Sambuc /// context is not an instance method.
70f4a2713aSLionel Sambuc IMA_Unresolved_StaticContext,
71f4a2713aSLionel Sambuc
72f4a2713aSLionel Sambuc // The reference refers to a field which is not a member of the containing
73f4a2713aSLionel Sambuc // class, which is allowed because we're in C++11 mode and the context is
74f4a2713aSLionel Sambuc // unevaluated.
75f4a2713aSLionel Sambuc IMA_Field_Uneval_Context,
76f4a2713aSLionel Sambuc
77f4a2713aSLionel Sambuc /// All possible referrents are instance members and the current
78f4a2713aSLionel Sambuc /// context is not an instance method.
79f4a2713aSLionel Sambuc IMA_Error_StaticContext,
80f4a2713aSLionel Sambuc
81f4a2713aSLionel Sambuc /// All possible referrents are instance members of an unrelated
82f4a2713aSLionel Sambuc /// class.
83f4a2713aSLionel Sambuc IMA_Error_Unrelated
84f4a2713aSLionel Sambuc };
85f4a2713aSLionel Sambuc
86f4a2713aSLionel Sambuc /// The given lookup names class member(s) and is not being used for
87f4a2713aSLionel Sambuc /// an address-of-member expression. Classify the type of access
88f4a2713aSLionel Sambuc /// according to whether it's possible that this reference names an
89f4a2713aSLionel Sambuc /// instance member. This is best-effort in dependent contexts; it is okay to
90f4a2713aSLionel Sambuc /// conservatively answer "yes", in which case some errors will simply
91f4a2713aSLionel Sambuc /// not be caught until template-instantiation.
ClassifyImplicitMemberAccess(Sema & SemaRef,const LookupResult & R)92f4a2713aSLionel Sambuc static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
93f4a2713aSLionel Sambuc const LookupResult &R) {
94f4a2713aSLionel Sambuc assert(!R.empty() && (*R.begin())->isCXXClassMember());
95f4a2713aSLionel Sambuc
96f4a2713aSLionel Sambuc DeclContext *DC = SemaRef.getFunctionLevelDeclContext();
97f4a2713aSLionel Sambuc
98f4a2713aSLionel Sambuc bool isStaticContext = SemaRef.CXXThisTypeOverride.isNull() &&
99f4a2713aSLionel Sambuc (!isa<CXXMethodDecl>(DC) || cast<CXXMethodDecl>(DC)->isStatic());
100f4a2713aSLionel Sambuc
101f4a2713aSLionel Sambuc if (R.isUnresolvableResult())
102f4a2713aSLionel Sambuc return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved;
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc // Collect all the declaring classes of instance members we find.
105f4a2713aSLionel Sambuc bool hasNonInstance = false;
106f4a2713aSLionel Sambuc bool isField = false;
107f4a2713aSLionel Sambuc BaseSet Classes;
108f4a2713aSLionel Sambuc for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
109f4a2713aSLionel Sambuc NamedDecl *D = *I;
110f4a2713aSLionel Sambuc
111f4a2713aSLionel Sambuc if (D->isCXXInstanceMember()) {
112f4a2713aSLionel Sambuc if (dyn_cast<FieldDecl>(D) || dyn_cast<MSPropertyDecl>(D)
113f4a2713aSLionel Sambuc || dyn_cast<IndirectFieldDecl>(D))
114f4a2713aSLionel Sambuc isField = true;
115f4a2713aSLionel Sambuc
116f4a2713aSLionel Sambuc CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext());
117f4a2713aSLionel Sambuc Classes.insert(R->getCanonicalDecl());
118f4a2713aSLionel Sambuc }
119f4a2713aSLionel Sambuc else
120f4a2713aSLionel Sambuc hasNonInstance = true;
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc
123f4a2713aSLionel Sambuc // If we didn't find any instance members, it can't be an implicit
124f4a2713aSLionel Sambuc // member reference.
125f4a2713aSLionel Sambuc if (Classes.empty())
126f4a2713aSLionel Sambuc return IMA_Static;
127f4a2713aSLionel Sambuc
128f4a2713aSLionel Sambuc // C++11 [expr.prim.general]p12:
129f4a2713aSLionel Sambuc // An id-expression that denotes a non-static data member or non-static
130f4a2713aSLionel Sambuc // member function of a class can only be used:
131f4a2713aSLionel Sambuc // (...)
132f4a2713aSLionel Sambuc // - if that id-expression denotes a non-static data member and it
133f4a2713aSLionel Sambuc // appears in an unevaluated operand.
134f4a2713aSLionel Sambuc //
135f4a2713aSLionel Sambuc // This rule is specific to C++11. However, we also permit this form
136f4a2713aSLionel Sambuc // in unevaluated inline assembly operands, like the operand to a SIZE.
137f4a2713aSLionel Sambuc IMAKind AbstractInstanceResult = IMA_Static; // happens to be 'false'
138f4a2713aSLionel Sambuc assert(!AbstractInstanceResult);
139f4a2713aSLionel Sambuc switch (SemaRef.ExprEvalContexts.back().Context) {
140f4a2713aSLionel Sambuc case Sema::Unevaluated:
141f4a2713aSLionel Sambuc if (isField && SemaRef.getLangOpts().CPlusPlus11)
142f4a2713aSLionel Sambuc AbstractInstanceResult = IMA_Field_Uneval_Context;
143f4a2713aSLionel Sambuc break;
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc case Sema::UnevaluatedAbstract:
146f4a2713aSLionel Sambuc AbstractInstanceResult = IMA_Abstract;
147f4a2713aSLionel Sambuc break;
148f4a2713aSLionel Sambuc
149f4a2713aSLionel Sambuc case Sema::ConstantEvaluated:
150f4a2713aSLionel Sambuc case Sema::PotentiallyEvaluated:
151f4a2713aSLionel Sambuc case Sema::PotentiallyEvaluatedIfUsed:
152f4a2713aSLionel Sambuc break;
153f4a2713aSLionel Sambuc }
154f4a2713aSLionel Sambuc
155f4a2713aSLionel Sambuc // If the current context is not an instance method, it can't be
156f4a2713aSLionel Sambuc // an implicit member reference.
157f4a2713aSLionel Sambuc if (isStaticContext) {
158f4a2713aSLionel Sambuc if (hasNonInstance)
159f4a2713aSLionel Sambuc return IMA_Mixed_StaticContext;
160f4a2713aSLionel Sambuc
161f4a2713aSLionel Sambuc return AbstractInstanceResult ? AbstractInstanceResult
162f4a2713aSLionel Sambuc : IMA_Error_StaticContext;
163f4a2713aSLionel Sambuc }
164f4a2713aSLionel Sambuc
165f4a2713aSLionel Sambuc CXXRecordDecl *contextClass;
166f4a2713aSLionel Sambuc if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
167f4a2713aSLionel Sambuc contextClass = MD->getParent()->getCanonicalDecl();
168f4a2713aSLionel Sambuc else
169f4a2713aSLionel Sambuc contextClass = cast<CXXRecordDecl>(DC);
170f4a2713aSLionel Sambuc
171f4a2713aSLionel Sambuc // [class.mfct.non-static]p3:
172f4a2713aSLionel Sambuc // ...is used in the body of a non-static member function of class X,
173f4a2713aSLionel Sambuc // if name lookup (3.4.1) resolves the name in the id-expression to a
174f4a2713aSLionel Sambuc // non-static non-type member of some class C [...]
175f4a2713aSLionel Sambuc // ...if C is not X or a base class of X, the class member access expression
176f4a2713aSLionel Sambuc // is ill-formed.
177f4a2713aSLionel Sambuc if (R.getNamingClass() &&
178f4a2713aSLionel Sambuc contextClass->getCanonicalDecl() !=
179f4a2713aSLionel Sambuc R.getNamingClass()->getCanonicalDecl()) {
180f4a2713aSLionel Sambuc // If the naming class is not the current context, this was a qualified
181f4a2713aSLionel Sambuc // member name lookup, and it's sufficient to check that we have the naming
182f4a2713aSLionel Sambuc // class as a base class.
183f4a2713aSLionel Sambuc Classes.clear();
184f4a2713aSLionel Sambuc Classes.insert(R.getNamingClass()->getCanonicalDecl());
185f4a2713aSLionel Sambuc }
186f4a2713aSLionel Sambuc
187f4a2713aSLionel Sambuc // If we can prove that the current context is unrelated to all the
188f4a2713aSLionel Sambuc // declaring classes, it can't be an implicit member reference (in
189f4a2713aSLionel Sambuc // which case it's an error if any of those members are selected).
190f4a2713aSLionel Sambuc if (isProvablyNotDerivedFrom(SemaRef, contextClass, Classes))
191f4a2713aSLionel Sambuc return hasNonInstance ? IMA_Mixed_Unrelated :
192f4a2713aSLionel Sambuc AbstractInstanceResult ? AbstractInstanceResult :
193f4a2713aSLionel Sambuc IMA_Error_Unrelated;
194f4a2713aSLionel Sambuc
195f4a2713aSLionel Sambuc return (hasNonInstance ? IMA_Mixed : IMA_Instance);
196f4a2713aSLionel Sambuc }
197f4a2713aSLionel Sambuc
198f4a2713aSLionel Sambuc /// Diagnose a reference to a field with no object available.
diagnoseInstanceReference(Sema & SemaRef,const CXXScopeSpec & SS,NamedDecl * Rep,const DeclarationNameInfo & nameInfo)199f4a2713aSLionel Sambuc static void diagnoseInstanceReference(Sema &SemaRef,
200f4a2713aSLionel Sambuc const CXXScopeSpec &SS,
201f4a2713aSLionel Sambuc NamedDecl *Rep,
202f4a2713aSLionel Sambuc const DeclarationNameInfo &nameInfo) {
203f4a2713aSLionel Sambuc SourceLocation Loc = nameInfo.getLoc();
204f4a2713aSLionel Sambuc SourceRange Range(Loc);
205f4a2713aSLionel Sambuc if (SS.isSet()) Range.setBegin(SS.getRange().getBegin());
206f4a2713aSLionel Sambuc
207*0a6a1f1dSLionel Sambuc // Look through using shadow decls and aliases.
208*0a6a1f1dSLionel Sambuc Rep = Rep->getUnderlyingDecl();
209*0a6a1f1dSLionel Sambuc
210f4a2713aSLionel Sambuc DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext();
211f4a2713aSLionel Sambuc CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FunctionLevelDC);
212*0a6a1f1dSLionel Sambuc CXXRecordDecl *ContextClass = Method ? Method->getParent() : nullptr;
213f4a2713aSLionel Sambuc CXXRecordDecl *RepClass = dyn_cast<CXXRecordDecl>(Rep->getDeclContext());
214f4a2713aSLionel Sambuc
215f4a2713aSLionel Sambuc bool InStaticMethod = Method && Method->isStatic();
216f4a2713aSLionel Sambuc bool IsField = isa<FieldDecl>(Rep) || isa<IndirectFieldDecl>(Rep);
217f4a2713aSLionel Sambuc
218f4a2713aSLionel Sambuc if (IsField && InStaticMethod)
219f4a2713aSLionel Sambuc // "invalid use of member 'x' in static member function"
220f4a2713aSLionel Sambuc SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method)
221f4a2713aSLionel Sambuc << Range << nameInfo.getName();
222f4a2713aSLionel Sambuc else if (ContextClass && RepClass && SS.isEmpty() && !InStaticMethod &&
223f4a2713aSLionel Sambuc !RepClass->Equals(ContextClass) && RepClass->Encloses(ContextClass))
224f4a2713aSLionel Sambuc // Unqualified lookup in a non-static member function found a member of an
225f4a2713aSLionel Sambuc // enclosing class.
226f4a2713aSLionel Sambuc SemaRef.Diag(Loc, diag::err_nested_non_static_member_use)
227f4a2713aSLionel Sambuc << IsField << RepClass << nameInfo.getName() << ContextClass << Range;
228f4a2713aSLionel Sambuc else if (IsField)
229f4a2713aSLionel Sambuc SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use)
230f4a2713aSLionel Sambuc << nameInfo.getName() << Range;
231f4a2713aSLionel Sambuc else
232f4a2713aSLionel Sambuc SemaRef.Diag(Loc, diag::err_member_call_without_object)
233f4a2713aSLionel Sambuc << Range;
234f4a2713aSLionel Sambuc }
235f4a2713aSLionel Sambuc
236f4a2713aSLionel Sambuc /// Builds an expression which might be an implicit member expression.
237f4a2713aSLionel Sambuc ExprResult
BuildPossibleImplicitMemberExpr(const CXXScopeSpec & SS,SourceLocation TemplateKWLoc,LookupResult & R,const TemplateArgumentListInfo * TemplateArgs)238f4a2713aSLionel Sambuc Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS,
239f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
240f4a2713aSLionel Sambuc LookupResult &R,
241f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs) {
242*0a6a1f1dSLionel Sambuc switch (ClassifyImplicitMemberAccess(*this, R)) {
243f4a2713aSLionel Sambuc case IMA_Instance:
244f4a2713aSLionel Sambuc return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true);
245f4a2713aSLionel Sambuc
246f4a2713aSLionel Sambuc case IMA_Mixed:
247f4a2713aSLionel Sambuc case IMA_Mixed_Unrelated:
248f4a2713aSLionel Sambuc case IMA_Unresolved:
249f4a2713aSLionel Sambuc return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, false);
250f4a2713aSLionel Sambuc
251f4a2713aSLionel Sambuc case IMA_Field_Uneval_Context:
252f4a2713aSLionel Sambuc Diag(R.getNameLoc(), diag::warn_cxx98_compat_non_static_member_use)
253f4a2713aSLionel Sambuc << R.getLookupNameInfo().getName();
254f4a2713aSLionel Sambuc // Fall through.
255f4a2713aSLionel Sambuc case IMA_Static:
256f4a2713aSLionel Sambuc case IMA_Abstract:
257f4a2713aSLionel Sambuc case IMA_Mixed_StaticContext:
258f4a2713aSLionel Sambuc case IMA_Unresolved_StaticContext:
259f4a2713aSLionel Sambuc if (TemplateArgs || TemplateKWLoc.isValid())
260f4a2713aSLionel Sambuc return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs);
261f4a2713aSLionel Sambuc return BuildDeclarationNameExpr(SS, R, false);
262f4a2713aSLionel Sambuc
263f4a2713aSLionel Sambuc case IMA_Error_StaticContext:
264f4a2713aSLionel Sambuc case IMA_Error_Unrelated:
265f4a2713aSLionel Sambuc diagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(),
266f4a2713aSLionel Sambuc R.getLookupNameInfo());
267f4a2713aSLionel Sambuc return ExprError();
268f4a2713aSLionel Sambuc }
269f4a2713aSLionel Sambuc
270f4a2713aSLionel Sambuc llvm_unreachable("unexpected instance member access kind");
271f4a2713aSLionel Sambuc }
272f4a2713aSLionel Sambuc
273f4a2713aSLionel Sambuc /// Check an ext-vector component access expression.
274f4a2713aSLionel Sambuc ///
275f4a2713aSLionel Sambuc /// VK should be set in advance to the value kind of the base
276f4a2713aSLionel Sambuc /// expression.
277f4a2713aSLionel Sambuc static QualType
CheckExtVectorComponent(Sema & S,QualType baseType,ExprValueKind & VK,SourceLocation OpLoc,const IdentifierInfo * CompName,SourceLocation CompLoc)278f4a2713aSLionel Sambuc CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK,
279f4a2713aSLionel Sambuc SourceLocation OpLoc, const IdentifierInfo *CompName,
280f4a2713aSLionel Sambuc SourceLocation CompLoc) {
281f4a2713aSLionel Sambuc // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements,
282f4a2713aSLionel Sambuc // see FIXME there.
283f4a2713aSLionel Sambuc //
284f4a2713aSLionel Sambuc // FIXME: This logic can be greatly simplified by splitting it along
285f4a2713aSLionel Sambuc // halving/not halving and reworking the component checking.
286f4a2713aSLionel Sambuc const ExtVectorType *vecType = baseType->getAs<ExtVectorType>();
287f4a2713aSLionel Sambuc
288f4a2713aSLionel Sambuc // The vector accessor can't exceed the number of elements.
289f4a2713aSLionel Sambuc const char *compStr = CompName->getNameStart();
290f4a2713aSLionel Sambuc
291f4a2713aSLionel Sambuc // This flag determines whether or not the component is one of the four
292f4a2713aSLionel Sambuc // special names that indicate a subset of exactly half the elements are
293f4a2713aSLionel Sambuc // to be selected.
294f4a2713aSLionel Sambuc bool HalvingSwizzle = false;
295f4a2713aSLionel Sambuc
296f4a2713aSLionel Sambuc // This flag determines whether or not CompName has an 's' char prefix,
297f4a2713aSLionel Sambuc // indicating that it is a string of hex values to be used as vector indices.
298*0a6a1f1dSLionel Sambuc bool HexSwizzle = (*compStr == 's' || *compStr == 'S') && compStr[1];
299f4a2713aSLionel Sambuc
300f4a2713aSLionel Sambuc bool HasRepeated = false;
301f4a2713aSLionel Sambuc bool HasIndex[16] = {};
302f4a2713aSLionel Sambuc
303f4a2713aSLionel Sambuc int Idx;
304f4a2713aSLionel Sambuc
305f4a2713aSLionel Sambuc // Check that we've found one of the special components, or that the component
306f4a2713aSLionel Sambuc // names must come from the same set.
307f4a2713aSLionel Sambuc if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
308f4a2713aSLionel Sambuc !strcmp(compStr, "even") || !strcmp(compStr, "odd")) {
309f4a2713aSLionel Sambuc HalvingSwizzle = true;
310f4a2713aSLionel Sambuc } else if (!HexSwizzle &&
311f4a2713aSLionel Sambuc (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) {
312f4a2713aSLionel Sambuc do {
313f4a2713aSLionel Sambuc if (HasIndex[Idx]) HasRepeated = true;
314f4a2713aSLionel Sambuc HasIndex[Idx] = true;
315f4a2713aSLionel Sambuc compStr++;
316f4a2713aSLionel Sambuc } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1);
317f4a2713aSLionel Sambuc } else {
318f4a2713aSLionel Sambuc if (HexSwizzle) compStr++;
319f4a2713aSLionel Sambuc while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) {
320f4a2713aSLionel Sambuc if (HasIndex[Idx]) HasRepeated = true;
321f4a2713aSLionel Sambuc HasIndex[Idx] = true;
322f4a2713aSLionel Sambuc compStr++;
323f4a2713aSLionel Sambuc }
324f4a2713aSLionel Sambuc }
325f4a2713aSLionel Sambuc
326f4a2713aSLionel Sambuc if (!HalvingSwizzle && *compStr) {
327f4a2713aSLionel Sambuc // We didn't get to the end of the string. This means the component names
328f4a2713aSLionel Sambuc // didn't come from the same set *or* we encountered an illegal name.
329f4a2713aSLionel Sambuc S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal)
330f4a2713aSLionel Sambuc << StringRef(compStr, 1) << SourceRange(CompLoc);
331f4a2713aSLionel Sambuc return QualType();
332f4a2713aSLionel Sambuc }
333f4a2713aSLionel Sambuc
334f4a2713aSLionel Sambuc // Ensure no component accessor exceeds the width of the vector type it
335f4a2713aSLionel Sambuc // operates on.
336f4a2713aSLionel Sambuc if (!HalvingSwizzle) {
337f4a2713aSLionel Sambuc compStr = CompName->getNameStart();
338f4a2713aSLionel Sambuc
339f4a2713aSLionel Sambuc if (HexSwizzle)
340f4a2713aSLionel Sambuc compStr++;
341f4a2713aSLionel Sambuc
342f4a2713aSLionel Sambuc while (*compStr) {
343f4a2713aSLionel Sambuc if (!vecType->isAccessorWithinNumElements(*compStr++)) {
344f4a2713aSLionel Sambuc S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length)
345f4a2713aSLionel Sambuc << baseType << SourceRange(CompLoc);
346f4a2713aSLionel Sambuc return QualType();
347f4a2713aSLionel Sambuc }
348f4a2713aSLionel Sambuc }
349f4a2713aSLionel Sambuc }
350f4a2713aSLionel Sambuc
351f4a2713aSLionel Sambuc // The component accessor looks fine - now we need to compute the actual type.
352f4a2713aSLionel Sambuc // The vector type is implied by the component accessor. For example,
353f4a2713aSLionel Sambuc // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
354f4a2713aSLionel Sambuc // vec4.s0 is a float, vec4.s23 is a vec3, etc.
355f4a2713aSLionel Sambuc // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
356f4a2713aSLionel Sambuc unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2
357f4a2713aSLionel Sambuc : CompName->getLength();
358f4a2713aSLionel Sambuc if (HexSwizzle)
359f4a2713aSLionel Sambuc CompSize--;
360f4a2713aSLionel Sambuc
361f4a2713aSLionel Sambuc if (CompSize == 1)
362f4a2713aSLionel Sambuc return vecType->getElementType();
363f4a2713aSLionel Sambuc
364f4a2713aSLionel Sambuc if (HasRepeated) VK = VK_RValue;
365f4a2713aSLionel Sambuc
366f4a2713aSLionel Sambuc QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize);
367f4a2713aSLionel Sambuc // Now look up the TypeDefDecl from the vector type. Without this,
368f4a2713aSLionel Sambuc // diagostics look bad. We want extended vector types to appear built-in.
369f4a2713aSLionel Sambuc for (Sema::ExtVectorDeclsType::iterator
370f4a2713aSLionel Sambuc I = S.ExtVectorDecls.begin(S.getExternalSource()),
371f4a2713aSLionel Sambuc E = S.ExtVectorDecls.end();
372f4a2713aSLionel Sambuc I != E; ++I) {
373f4a2713aSLionel Sambuc if ((*I)->getUnderlyingType() == VT)
374f4a2713aSLionel Sambuc return S.Context.getTypedefType(*I);
375f4a2713aSLionel Sambuc }
376f4a2713aSLionel Sambuc
377f4a2713aSLionel Sambuc return VT; // should never get here (a typedef type should always be found).
378f4a2713aSLionel Sambuc }
379f4a2713aSLionel Sambuc
FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl * PDecl,IdentifierInfo * Member,const Selector & Sel,ASTContext & Context)380f4a2713aSLionel Sambuc static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl,
381f4a2713aSLionel Sambuc IdentifierInfo *Member,
382f4a2713aSLionel Sambuc const Selector &Sel,
383f4a2713aSLionel Sambuc ASTContext &Context) {
384f4a2713aSLionel Sambuc if (Member)
385f4a2713aSLionel Sambuc if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member))
386f4a2713aSLionel Sambuc return PD;
387f4a2713aSLionel Sambuc if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel))
388f4a2713aSLionel Sambuc return OMD;
389f4a2713aSLionel Sambuc
390*0a6a1f1dSLionel Sambuc for (const auto *I : PDecl->protocols()) {
391*0a6a1f1dSLionel Sambuc if (Decl *D = FindGetterSetterNameDeclFromProtocolList(I, Member, Sel,
392f4a2713aSLionel Sambuc Context))
393f4a2713aSLionel Sambuc return D;
394f4a2713aSLionel Sambuc }
395*0a6a1f1dSLionel Sambuc return nullptr;
396f4a2713aSLionel Sambuc }
397f4a2713aSLionel Sambuc
FindGetterSetterNameDecl(const ObjCObjectPointerType * QIdTy,IdentifierInfo * Member,const Selector & Sel,ASTContext & Context)398f4a2713aSLionel Sambuc static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy,
399f4a2713aSLionel Sambuc IdentifierInfo *Member,
400f4a2713aSLionel Sambuc const Selector &Sel,
401f4a2713aSLionel Sambuc ASTContext &Context) {
402f4a2713aSLionel Sambuc // Check protocols on qualified interfaces.
403*0a6a1f1dSLionel Sambuc Decl *GDecl = nullptr;
404*0a6a1f1dSLionel Sambuc for (const auto *I : QIdTy->quals()) {
405f4a2713aSLionel Sambuc if (Member)
406*0a6a1f1dSLionel Sambuc if (ObjCPropertyDecl *PD = I->FindPropertyDeclaration(Member)) {
407f4a2713aSLionel Sambuc GDecl = PD;
408f4a2713aSLionel Sambuc break;
409f4a2713aSLionel Sambuc }
410f4a2713aSLionel Sambuc // Also must look for a getter or setter name which uses property syntax.
411*0a6a1f1dSLionel Sambuc if (ObjCMethodDecl *OMD = I->getInstanceMethod(Sel)) {
412f4a2713aSLionel Sambuc GDecl = OMD;
413f4a2713aSLionel Sambuc break;
414f4a2713aSLionel Sambuc }
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc if (!GDecl) {
417*0a6a1f1dSLionel Sambuc for (const auto *I : QIdTy->quals()) {
418f4a2713aSLionel Sambuc // Search in the protocol-qualifier list of current protocol.
419*0a6a1f1dSLionel Sambuc GDecl = FindGetterSetterNameDeclFromProtocolList(I, Member, Sel, Context);
420f4a2713aSLionel Sambuc if (GDecl)
421f4a2713aSLionel Sambuc return GDecl;
422f4a2713aSLionel Sambuc }
423f4a2713aSLionel Sambuc }
424f4a2713aSLionel Sambuc return GDecl;
425f4a2713aSLionel Sambuc }
426f4a2713aSLionel Sambuc
427f4a2713aSLionel Sambuc ExprResult
ActOnDependentMemberExpr(Expr * BaseExpr,QualType BaseType,bool IsArrow,SourceLocation OpLoc,const CXXScopeSpec & SS,SourceLocation TemplateKWLoc,NamedDecl * FirstQualifierInScope,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * TemplateArgs)428f4a2713aSLionel Sambuc Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType,
429f4a2713aSLionel Sambuc bool IsArrow, SourceLocation OpLoc,
430f4a2713aSLionel Sambuc const CXXScopeSpec &SS,
431f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
432f4a2713aSLionel Sambuc NamedDecl *FirstQualifierInScope,
433f4a2713aSLionel Sambuc const DeclarationNameInfo &NameInfo,
434f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs) {
435f4a2713aSLionel Sambuc // Even in dependent contexts, try to diagnose base expressions with
436f4a2713aSLionel Sambuc // obviously wrong types, e.g.:
437f4a2713aSLionel Sambuc //
438f4a2713aSLionel Sambuc // T* t;
439f4a2713aSLionel Sambuc // t.f;
440f4a2713aSLionel Sambuc //
441f4a2713aSLionel Sambuc // In Obj-C++, however, the above expression is valid, since it could be
442f4a2713aSLionel Sambuc // accessing the 'f' property if T is an Obj-C interface. The extra check
443f4a2713aSLionel Sambuc // allows this, while still reporting an error if T is a struct pointer.
444f4a2713aSLionel Sambuc if (!IsArrow) {
445f4a2713aSLionel Sambuc const PointerType *PT = BaseType->getAs<PointerType>();
446f4a2713aSLionel Sambuc if (PT && (!getLangOpts().ObjC1 ||
447f4a2713aSLionel Sambuc PT->getPointeeType()->isRecordType())) {
448f4a2713aSLionel Sambuc assert(BaseExpr && "cannot happen with implicit member accesses");
449f4a2713aSLionel Sambuc Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
450f4a2713aSLionel Sambuc << BaseType << BaseExpr->getSourceRange() << NameInfo.getSourceRange();
451f4a2713aSLionel Sambuc return ExprError();
452f4a2713aSLionel Sambuc }
453f4a2713aSLionel Sambuc }
454f4a2713aSLionel Sambuc
455f4a2713aSLionel Sambuc assert(BaseType->isDependentType() ||
456f4a2713aSLionel Sambuc NameInfo.getName().isDependentName() ||
457f4a2713aSLionel Sambuc isDependentScopeSpecifier(SS));
458f4a2713aSLionel Sambuc
459f4a2713aSLionel Sambuc // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr
460f4a2713aSLionel Sambuc // must have pointer type, and the accessed type is the pointee.
461*0a6a1f1dSLionel Sambuc return CXXDependentScopeMemberExpr::Create(
462*0a6a1f1dSLionel Sambuc Context, BaseExpr, BaseType, IsArrow, OpLoc,
463*0a6a1f1dSLionel Sambuc SS.getWithLocInContext(Context), TemplateKWLoc, FirstQualifierInScope,
464*0a6a1f1dSLionel Sambuc NameInfo, TemplateArgs);
465f4a2713aSLionel Sambuc }
466f4a2713aSLionel Sambuc
467f4a2713aSLionel Sambuc /// We know that the given qualified member reference points only to
468f4a2713aSLionel Sambuc /// declarations which do not belong to the static type of the base
469f4a2713aSLionel Sambuc /// expression. Diagnose the problem.
DiagnoseQualifiedMemberReference(Sema & SemaRef,Expr * BaseExpr,QualType BaseType,const CXXScopeSpec & SS,NamedDecl * rep,const DeclarationNameInfo & nameInfo)470f4a2713aSLionel Sambuc static void DiagnoseQualifiedMemberReference(Sema &SemaRef,
471f4a2713aSLionel Sambuc Expr *BaseExpr,
472f4a2713aSLionel Sambuc QualType BaseType,
473f4a2713aSLionel Sambuc const CXXScopeSpec &SS,
474f4a2713aSLionel Sambuc NamedDecl *rep,
475f4a2713aSLionel Sambuc const DeclarationNameInfo &nameInfo) {
476f4a2713aSLionel Sambuc // If this is an implicit member access, use a different set of
477f4a2713aSLionel Sambuc // diagnostics.
478f4a2713aSLionel Sambuc if (!BaseExpr)
479f4a2713aSLionel Sambuc return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo);
480f4a2713aSLionel Sambuc
481f4a2713aSLionel Sambuc SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated)
482f4a2713aSLionel Sambuc << SS.getRange() << rep << BaseType;
483f4a2713aSLionel Sambuc }
484f4a2713aSLionel Sambuc
485f4a2713aSLionel Sambuc // Check whether the declarations we found through a nested-name
486f4a2713aSLionel Sambuc // specifier in a member expression are actually members of the base
487f4a2713aSLionel Sambuc // type. The restriction here is:
488f4a2713aSLionel Sambuc //
489f4a2713aSLionel Sambuc // C++ [expr.ref]p2:
490f4a2713aSLionel Sambuc // ... In these cases, the id-expression shall name a
491f4a2713aSLionel Sambuc // member of the class or of one of its base classes.
492f4a2713aSLionel Sambuc //
493f4a2713aSLionel Sambuc // So it's perfectly legitimate for the nested-name specifier to name
494f4a2713aSLionel Sambuc // an unrelated class, and for us to find an overload set including
495f4a2713aSLionel Sambuc // decls from classes which are not superclasses, as long as the decl
496f4a2713aSLionel Sambuc // we actually pick through overload resolution is from a superclass.
CheckQualifiedMemberReference(Expr * BaseExpr,QualType BaseType,const CXXScopeSpec & SS,const LookupResult & R)497f4a2713aSLionel Sambuc bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr,
498f4a2713aSLionel Sambuc QualType BaseType,
499f4a2713aSLionel Sambuc const CXXScopeSpec &SS,
500f4a2713aSLionel Sambuc const LookupResult &R) {
501f4a2713aSLionel Sambuc CXXRecordDecl *BaseRecord =
502f4a2713aSLionel Sambuc cast_or_null<CXXRecordDecl>(computeDeclContext(BaseType));
503f4a2713aSLionel Sambuc if (!BaseRecord) {
504f4a2713aSLionel Sambuc // We can't check this yet because the base type is still
505f4a2713aSLionel Sambuc // dependent.
506f4a2713aSLionel Sambuc assert(BaseType->isDependentType());
507f4a2713aSLionel Sambuc return false;
508f4a2713aSLionel Sambuc }
509f4a2713aSLionel Sambuc
510f4a2713aSLionel Sambuc for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
511f4a2713aSLionel Sambuc // If this is an implicit member reference and we find a
512f4a2713aSLionel Sambuc // non-instance member, it's not an error.
513f4a2713aSLionel Sambuc if (!BaseExpr && !(*I)->isCXXInstanceMember())
514f4a2713aSLionel Sambuc return false;
515f4a2713aSLionel Sambuc
516f4a2713aSLionel Sambuc // Note that we use the DC of the decl, not the underlying decl.
517f4a2713aSLionel Sambuc DeclContext *DC = (*I)->getDeclContext();
518f4a2713aSLionel Sambuc while (DC->isTransparentContext())
519f4a2713aSLionel Sambuc DC = DC->getParent();
520f4a2713aSLionel Sambuc
521f4a2713aSLionel Sambuc if (!DC->isRecord())
522f4a2713aSLionel Sambuc continue;
523f4a2713aSLionel Sambuc
524f4a2713aSLionel Sambuc CXXRecordDecl *MemberRecord = cast<CXXRecordDecl>(DC)->getCanonicalDecl();
525f4a2713aSLionel Sambuc if (BaseRecord->getCanonicalDecl() == MemberRecord ||
526f4a2713aSLionel Sambuc !BaseRecord->isProvablyNotDerivedFrom(MemberRecord))
527f4a2713aSLionel Sambuc return false;
528f4a2713aSLionel Sambuc }
529f4a2713aSLionel Sambuc
530f4a2713aSLionel Sambuc DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS,
531f4a2713aSLionel Sambuc R.getRepresentativeDecl(),
532f4a2713aSLionel Sambuc R.getLookupNameInfo());
533f4a2713aSLionel Sambuc return true;
534f4a2713aSLionel Sambuc }
535f4a2713aSLionel Sambuc
536f4a2713aSLionel Sambuc namespace {
537f4a2713aSLionel Sambuc
538f4a2713aSLionel Sambuc // Callback to only accept typo corrections that are either a ValueDecl or a
539f4a2713aSLionel Sambuc // FunctionTemplateDecl and are declared in the current record or, for a C++
540f4a2713aSLionel Sambuc // classes, one of its base classes.
541f4a2713aSLionel Sambuc class RecordMemberExprValidatorCCC : public CorrectionCandidateCallback {
542f4a2713aSLionel Sambuc public:
RecordMemberExprValidatorCCC(const RecordType * RTy)543f4a2713aSLionel Sambuc explicit RecordMemberExprValidatorCCC(const RecordType *RTy)
544*0a6a1f1dSLionel Sambuc : Record(RTy->getDecl()) {
545*0a6a1f1dSLionel Sambuc // Don't add bare keywords to the consumer since they will always fail
546*0a6a1f1dSLionel Sambuc // validation by virtue of not being associated with any decls.
547*0a6a1f1dSLionel Sambuc WantTypeSpecifiers = false;
548*0a6a1f1dSLionel Sambuc WantExpressionKeywords = false;
549*0a6a1f1dSLionel Sambuc WantCXXNamedCasts = false;
550*0a6a1f1dSLionel Sambuc WantFunctionLikeCasts = false;
551*0a6a1f1dSLionel Sambuc WantRemainingKeywords = false;
552*0a6a1f1dSLionel Sambuc }
553f4a2713aSLionel Sambuc
ValidateCandidate(const TypoCorrection & candidate)554*0a6a1f1dSLionel Sambuc bool ValidateCandidate(const TypoCorrection &candidate) override {
555f4a2713aSLionel Sambuc NamedDecl *ND = candidate.getCorrectionDecl();
556f4a2713aSLionel Sambuc // Don't accept candidates that cannot be member functions, constants,
557f4a2713aSLionel Sambuc // variables, or templates.
558f4a2713aSLionel Sambuc if (!ND || !(isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)))
559f4a2713aSLionel Sambuc return false;
560f4a2713aSLionel Sambuc
561f4a2713aSLionel Sambuc // Accept candidates that occur in the current record.
562f4a2713aSLionel Sambuc if (Record->containsDecl(ND))
563f4a2713aSLionel Sambuc return true;
564f4a2713aSLionel Sambuc
565f4a2713aSLionel Sambuc if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Record)) {
566f4a2713aSLionel Sambuc // Accept candidates that occur in any of the current class' base classes.
567*0a6a1f1dSLionel Sambuc for (const auto &BS : RD->bases()) {
568*0a6a1f1dSLionel Sambuc if (const RecordType *BSTy =
569*0a6a1f1dSLionel Sambuc dyn_cast_or_null<RecordType>(BS.getType().getTypePtrOrNull())) {
570f4a2713aSLionel Sambuc if (BSTy->getDecl()->containsDecl(ND))
571f4a2713aSLionel Sambuc return true;
572f4a2713aSLionel Sambuc }
573f4a2713aSLionel Sambuc }
574f4a2713aSLionel Sambuc }
575f4a2713aSLionel Sambuc
576f4a2713aSLionel Sambuc return false;
577f4a2713aSLionel Sambuc }
578f4a2713aSLionel Sambuc
579f4a2713aSLionel Sambuc private:
580f4a2713aSLionel Sambuc const RecordDecl *const Record;
581f4a2713aSLionel Sambuc };
582f4a2713aSLionel Sambuc
583f4a2713aSLionel Sambuc }
584f4a2713aSLionel Sambuc
LookupMemberExprInRecord(Sema & SemaRef,LookupResult & R,Expr * BaseExpr,const RecordType * RTy,SourceLocation OpLoc,bool IsArrow,CXXScopeSpec & SS,bool HasTemplateArgs,TypoExpr * & TE)585*0a6a1f1dSLionel Sambuc static bool LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R,
586*0a6a1f1dSLionel Sambuc Expr *BaseExpr,
587*0a6a1f1dSLionel Sambuc const RecordType *RTy,
588*0a6a1f1dSLionel Sambuc SourceLocation OpLoc, bool IsArrow,
589*0a6a1f1dSLionel Sambuc CXXScopeSpec &SS, bool HasTemplateArgs,
590*0a6a1f1dSLionel Sambuc TypoExpr *&TE) {
591*0a6a1f1dSLionel Sambuc SourceRange BaseRange = BaseExpr ? BaseExpr->getSourceRange() : SourceRange();
592f4a2713aSLionel Sambuc RecordDecl *RDecl = RTy->getDecl();
593f4a2713aSLionel Sambuc if (!SemaRef.isThisOutsideMemberFunctionBody(QualType(RTy, 0)) &&
594f4a2713aSLionel Sambuc SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0),
595f4a2713aSLionel Sambuc diag::err_typecheck_incomplete_tag,
596f4a2713aSLionel Sambuc BaseRange))
597f4a2713aSLionel Sambuc return true;
598f4a2713aSLionel Sambuc
599f4a2713aSLionel Sambuc if (HasTemplateArgs) {
600f4a2713aSLionel Sambuc // LookupTemplateName doesn't expect these both to exist simultaneously.
601f4a2713aSLionel Sambuc QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0);
602f4a2713aSLionel Sambuc
603f4a2713aSLionel Sambuc bool MOUS;
604*0a6a1f1dSLionel Sambuc SemaRef.LookupTemplateName(R, nullptr, SS, ObjectType, false, MOUS);
605f4a2713aSLionel Sambuc return false;
606f4a2713aSLionel Sambuc }
607f4a2713aSLionel Sambuc
608f4a2713aSLionel Sambuc DeclContext *DC = RDecl;
609f4a2713aSLionel Sambuc if (SS.isSet()) {
610f4a2713aSLionel Sambuc // If the member name was a qualified-id, look into the
611f4a2713aSLionel Sambuc // nested-name-specifier.
612f4a2713aSLionel Sambuc DC = SemaRef.computeDeclContext(SS, false);
613f4a2713aSLionel Sambuc
614f4a2713aSLionel Sambuc if (SemaRef.RequireCompleteDeclContext(SS, DC)) {
615f4a2713aSLionel Sambuc SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag)
616f4a2713aSLionel Sambuc << SS.getRange() << DC;
617f4a2713aSLionel Sambuc return true;
618f4a2713aSLionel Sambuc }
619f4a2713aSLionel Sambuc
620f4a2713aSLionel Sambuc assert(DC && "Cannot handle non-computable dependent contexts in lookup");
621f4a2713aSLionel Sambuc
622f4a2713aSLionel Sambuc if (!isa<TypeDecl>(DC)) {
623f4a2713aSLionel Sambuc SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass)
624f4a2713aSLionel Sambuc << DC << SS.getRange();
625f4a2713aSLionel Sambuc return true;
626f4a2713aSLionel Sambuc }
627f4a2713aSLionel Sambuc }
628f4a2713aSLionel Sambuc
629f4a2713aSLionel Sambuc // The record definition is complete, now look up the member.
630*0a6a1f1dSLionel Sambuc SemaRef.LookupQualifiedName(R, DC, SS);
631f4a2713aSLionel Sambuc
632f4a2713aSLionel Sambuc if (!R.empty())
633f4a2713aSLionel Sambuc return false;
634f4a2713aSLionel Sambuc
635*0a6a1f1dSLionel Sambuc DeclarationName Typo = R.getLookupName();
636*0a6a1f1dSLionel Sambuc SourceLocation TypoLoc = R.getNameLoc();
637*0a6a1f1dSLionel Sambuc TE = SemaRef.CorrectTypoDelayed(
638*0a6a1f1dSLionel Sambuc R.getLookupNameInfo(), R.getLookupKind(), nullptr, &SS,
639*0a6a1f1dSLionel Sambuc llvm::make_unique<RecordMemberExprValidatorCCC>(RTy),
640*0a6a1f1dSLionel Sambuc [=, &SemaRef](const TypoCorrection &TC) {
641*0a6a1f1dSLionel Sambuc if (TC) {
642*0a6a1f1dSLionel Sambuc assert(!TC.isKeyword() &&
643*0a6a1f1dSLionel Sambuc "Got a keyword as a correction for a member!");
644f4a2713aSLionel Sambuc bool DroppedSpecifier =
645*0a6a1f1dSLionel Sambuc TC.WillReplaceSpecifier() &&
646*0a6a1f1dSLionel Sambuc Typo.getAsString() == TC.getAsString(SemaRef.getLangOpts());
647*0a6a1f1dSLionel Sambuc SemaRef.diagnoseTypo(TC, SemaRef.PDiag(diag::err_no_member_suggest)
648*0a6a1f1dSLionel Sambuc << Typo << DC << DroppedSpecifier
649*0a6a1f1dSLionel Sambuc << SS.getRange());
650*0a6a1f1dSLionel Sambuc } else {
651*0a6a1f1dSLionel Sambuc SemaRef.Diag(TypoLoc, diag::err_no_member) << Typo << DC << BaseRange;
652f4a2713aSLionel Sambuc }
653*0a6a1f1dSLionel Sambuc },
654*0a6a1f1dSLionel Sambuc [=](Sema &SemaRef, TypoExpr *TE, TypoCorrection TC) mutable {
655*0a6a1f1dSLionel Sambuc R.clear(); // Ensure there's no decls lingering in the shared state.
656*0a6a1f1dSLionel Sambuc R.suppressDiagnostics();
657*0a6a1f1dSLionel Sambuc R.setLookupName(TC.getCorrection());
658*0a6a1f1dSLionel Sambuc for (NamedDecl *ND : TC)
659*0a6a1f1dSLionel Sambuc R.addDecl(ND);
660*0a6a1f1dSLionel Sambuc R.resolveKind();
661*0a6a1f1dSLionel Sambuc return SemaRef.BuildMemberReferenceExpr(
662*0a6a1f1dSLionel Sambuc BaseExpr, BaseExpr->getType(), OpLoc, IsArrow, SS, SourceLocation(),
663*0a6a1f1dSLionel Sambuc nullptr, R, nullptr);
664*0a6a1f1dSLionel Sambuc },
665*0a6a1f1dSLionel Sambuc Sema::CTK_ErrorRecovery, DC);
666f4a2713aSLionel Sambuc
667f4a2713aSLionel Sambuc return false;
668f4a2713aSLionel Sambuc }
669f4a2713aSLionel Sambuc
670*0a6a1f1dSLionel Sambuc static ExprResult LookupMemberExpr(Sema &S, LookupResult &R,
671*0a6a1f1dSLionel Sambuc ExprResult &BaseExpr, bool &IsArrow,
672*0a6a1f1dSLionel Sambuc SourceLocation OpLoc, CXXScopeSpec &SS,
673*0a6a1f1dSLionel Sambuc Decl *ObjCImpDecl, bool HasTemplateArgs);
674*0a6a1f1dSLionel Sambuc
675f4a2713aSLionel Sambuc ExprResult
BuildMemberReferenceExpr(Expr * Base,QualType BaseType,SourceLocation OpLoc,bool IsArrow,CXXScopeSpec & SS,SourceLocation TemplateKWLoc,NamedDecl * FirstQualifierInScope,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * TemplateArgs,ActOnMemberAccessExtraArgs * ExtraArgs)676f4a2713aSLionel Sambuc Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType,
677f4a2713aSLionel Sambuc SourceLocation OpLoc, bool IsArrow,
678f4a2713aSLionel Sambuc CXXScopeSpec &SS,
679f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
680f4a2713aSLionel Sambuc NamedDecl *FirstQualifierInScope,
681f4a2713aSLionel Sambuc const DeclarationNameInfo &NameInfo,
682*0a6a1f1dSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs,
683*0a6a1f1dSLionel Sambuc ActOnMemberAccessExtraArgs *ExtraArgs) {
684f4a2713aSLionel Sambuc if (BaseType->isDependentType() ||
685f4a2713aSLionel Sambuc (SS.isSet() && isDependentScopeSpecifier(SS)))
686f4a2713aSLionel Sambuc return ActOnDependentMemberExpr(Base, BaseType,
687f4a2713aSLionel Sambuc IsArrow, OpLoc,
688f4a2713aSLionel Sambuc SS, TemplateKWLoc, FirstQualifierInScope,
689f4a2713aSLionel Sambuc NameInfo, TemplateArgs);
690f4a2713aSLionel Sambuc
691f4a2713aSLionel Sambuc LookupResult R(*this, NameInfo, LookupMemberName);
692f4a2713aSLionel Sambuc
693f4a2713aSLionel Sambuc // Implicit member accesses.
694f4a2713aSLionel Sambuc if (!Base) {
695*0a6a1f1dSLionel Sambuc TypoExpr *TE = nullptr;
696f4a2713aSLionel Sambuc QualType RecordTy = BaseType;
697f4a2713aSLionel Sambuc if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType();
698*0a6a1f1dSLionel Sambuc if (LookupMemberExprInRecord(*this, R, nullptr,
699*0a6a1f1dSLionel Sambuc RecordTy->getAs<RecordType>(), OpLoc, IsArrow,
700*0a6a1f1dSLionel Sambuc SS, TemplateArgs != nullptr, TE))
701f4a2713aSLionel Sambuc return ExprError();
702*0a6a1f1dSLionel Sambuc if (TE)
703*0a6a1f1dSLionel Sambuc return TE;
704f4a2713aSLionel Sambuc
705f4a2713aSLionel Sambuc // Explicit member accesses.
706f4a2713aSLionel Sambuc } else {
707*0a6a1f1dSLionel Sambuc ExprResult BaseResult = Base;
708*0a6a1f1dSLionel Sambuc ExprResult Result = LookupMemberExpr(
709*0a6a1f1dSLionel Sambuc *this, R, BaseResult, IsArrow, OpLoc, SS,
710*0a6a1f1dSLionel Sambuc ExtraArgs ? ExtraArgs->ObjCImpDecl : nullptr,
711*0a6a1f1dSLionel Sambuc TemplateArgs != nullptr);
712f4a2713aSLionel Sambuc
713f4a2713aSLionel Sambuc if (BaseResult.isInvalid())
714f4a2713aSLionel Sambuc return ExprError();
715*0a6a1f1dSLionel Sambuc Base = BaseResult.get();
716f4a2713aSLionel Sambuc
717*0a6a1f1dSLionel Sambuc if (Result.isInvalid())
718f4a2713aSLionel Sambuc return ExprError();
719f4a2713aSLionel Sambuc
720f4a2713aSLionel Sambuc if (Result.get())
721f4a2713aSLionel Sambuc return Result;
722f4a2713aSLionel Sambuc
723f4a2713aSLionel Sambuc // LookupMemberExpr can modify Base, and thus change BaseType
724f4a2713aSLionel Sambuc BaseType = Base->getType();
725f4a2713aSLionel Sambuc }
726f4a2713aSLionel Sambuc
727f4a2713aSLionel Sambuc return BuildMemberReferenceExpr(Base, BaseType,
728f4a2713aSLionel Sambuc OpLoc, IsArrow, SS, TemplateKWLoc,
729*0a6a1f1dSLionel Sambuc FirstQualifierInScope, R, TemplateArgs,
730*0a6a1f1dSLionel Sambuc false, ExtraArgs);
731f4a2713aSLionel Sambuc }
732f4a2713aSLionel Sambuc
733f4a2713aSLionel Sambuc static ExprResult
734f4a2713aSLionel Sambuc BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
735f4a2713aSLionel Sambuc const CXXScopeSpec &SS, FieldDecl *Field,
736f4a2713aSLionel Sambuc DeclAccessPair FoundDecl,
737f4a2713aSLionel Sambuc const DeclarationNameInfo &MemberNameInfo);
738f4a2713aSLionel Sambuc
739f4a2713aSLionel Sambuc ExprResult
BuildAnonymousStructUnionMemberReference(const CXXScopeSpec & SS,SourceLocation loc,IndirectFieldDecl * indirectField,DeclAccessPair foundDecl,Expr * baseObjectExpr,SourceLocation opLoc)740f4a2713aSLionel Sambuc Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS,
741f4a2713aSLionel Sambuc SourceLocation loc,
742f4a2713aSLionel Sambuc IndirectFieldDecl *indirectField,
743f4a2713aSLionel Sambuc DeclAccessPair foundDecl,
744f4a2713aSLionel Sambuc Expr *baseObjectExpr,
745f4a2713aSLionel Sambuc SourceLocation opLoc) {
746f4a2713aSLionel Sambuc // First, build the expression that refers to the base object.
747f4a2713aSLionel Sambuc
748f4a2713aSLionel Sambuc bool baseObjectIsPointer = false;
749f4a2713aSLionel Sambuc Qualifiers baseQuals;
750f4a2713aSLionel Sambuc
751f4a2713aSLionel Sambuc // Case 1: the base of the indirect field is not a field.
752f4a2713aSLionel Sambuc VarDecl *baseVariable = indirectField->getVarDecl();
753f4a2713aSLionel Sambuc CXXScopeSpec EmptySS;
754f4a2713aSLionel Sambuc if (baseVariable) {
755f4a2713aSLionel Sambuc assert(baseVariable->getType()->isRecordType());
756f4a2713aSLionel Sambuc
757f4a2713aSLionel Sambuc // In principle we could have a member access expression that
758f4a2713aSLionel Sambuc // accesses an anonymous struct/union that's a static member of
759f4a2713aSLionel Sambuc // the base object's class. However, under the current standard,
760f4a2713aSLionel Sambuc // static data members cannot be anonymous structs or unions.
761f4a2713aSLionel Sambuc // Supporting this is as easy as building a MemberExpr here.
762f4a2713aSLionel Sambuc assert(!baseObjectExpr && "anonymous struct/union is static data member?");
763f4a2713aSLionel Sambuc
764f4a2713aSLionel Sambuc DeclarationNameInfo baseNameInfo(DeclarationName(), loc);
765f4a2713aSLionel Sambuc
766f4a2713aSLionel Sambuc ExprResult result
767f4a2713aSLionel Sambuc = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable);
768f4a2713aSLionel Sambuc if (result.isInvalid()) return ExprError();
769f4a2713aSLionel Sambuc
770*0a6a1f1dSLionel Sambuc baseObjectExpr = result.get();
771f4a2713aSLionel Sambuc baseObjectIsPointer = false;
772f4a2713aSLionel Sambuc baseQuals = baseObjectExpr->getType().getQualifiers();
773f4a2713aSLionel Sambuc
774f4a2713aSLionel Sambuc // Case 2: the base of the indirect field is a field and the user
775f4a2713aSLionel Sambuc // wrote a member expression.
776f4a2713aSLionel Sambuc } else if (baseObjectExpr) {
777f4a2713aSLionel Sambuc // The caller provided the base object expression. Determine
778f4a2713aSLionel Sambuc // whether its a pointer and whether it adds any qualifiers to the
779f4a2713aSLionel Sambuc // anonymous struct/union fields we're looking into.
780f4a2713aSLionel Sambuc QualType objectType = baseObjectExpr->getType();
781f4a2713aSLionel Sambuc
782f4a2713aSLionel Sambuc if (const PointerType *ptr = objectType->getAs<PointerType>()) {
783f4a2713aSLionel Sambuc baseObjectIsPointer = true;
784f4a2713aSLionel Sambuc objectType = ptr->getPointeeType();
785f4a2713aSLionel Sambuc } else {
786f4a2713aSLionel Sambuc baseObjectIsPointer = false;
787f4a2713aSLionel Sambuc }
788f4a2713aSLionel Sambuc baseQuals = objectType.getQualifiers();
789f4a2713aSLionel Sambuc
790f4a2713aSLionel Sambuc // Case 3: the base of the indirect field is a field and we should
791f4a2713aSLionel Sambuc // build an implicit member access.
792f4a2713aSLionel Sambuc } else {
793f4a2713aSLionel Sambuc // We've found a member of an anonymous struct/union that is
794f4a2713aSLionel Sambuc // inside a non-anonymous struct/union, so in a well-formed
795f4a2713aSLionel Sambuc // program our base object expression is "this".
796f4a2713aSLionel Sambuc QualType ThisTy = getCurrentThisType();
797f4a2713aSLionel Sambuc if (ThisTy.isNull()) {
798f4a2713aSLionel Sambuc Diag(loc, diag::err_invalid_member_use_in_static_method)
799f4a2713aSLionel Sambuc << indirectField->getDeclName();
800f4a2713aSLionel Sambuc return ExprError();
801f4a2713aSLionel Sambuc }
802f4a2713aSLionel Sambuc
803f4a2713aSLionel Sambuc // Our base object expression is "this".
804f4a2713aSLionel Sambuc CheckCXXThisCapture(loc);
805f4a2713aSLionel Sambuc baseObjectExpr
806f4a2713aSLionel Sambuc = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true);
807f4a2713aSLionel Sambuc baseObjectIsPointer = true;
808f4a2713aSLionel Sambuc baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers();
809f4a2713aSLionel Sambuc }
810f4a2713aSLionel Sambuc
811f4a2713aSLionel Sambuc // Build the implicit member references to the field of the
812f4a2713aSLionel Sambuc // anonymous struct/union.
813f4a2713aSLionel Sambuc Expr *result = baseObjectExpr;
814f4a2713aSLionel Sambuc IndirectFieldDecl::chain_iterator
815f4a2713aSLionel Sambuc FI = indirectField->chain_begin(), FEnd = indirectField->chain_end();
816f4a2713aSLionel Sambuc
817f4a2713aSLionel Sambuc // Build the first member access in the chain with full information.
818f4a2713aSLionel Sambuc if (!baseVariable) {
819f4a2713aSLionel Sambuc FieldDecl *field = cast<FieldDecl>(*FI);
820f4a2713aSLionel Sambuc
821f4a2713aSLionel Sambuc // Make a nameInfo that properly uses the anonymous name.
822f4a2713aSLionel Sambuc DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
823f4a2713aSLionel Sambuc
824f4a2713aSLionel Sambuc result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer,
825f4a2713aSLionel Sambuc EmptySS, field, foundDecl,
826*0a6a1f1dSLionel Sambuc memberNameInfo).get();
827f4a2713aSLionel Sambuc if (!result)
828f4a2713aSLionel Sambuc return ExprError();
829f4a2713aSLionel Sambuc
830f4a2713aSLionel Sambuc // FIXME: check qualified member access
831f4a2713aSLionel Sambuc }
832f4a2713aSLionel Sambuc
833f4a2713aSLionel Sambuc // In all cases, we should now skip the first declaration in the chain.
834f4a2713aSLionel Sambuc ++FI;
835f4a2713aSLionel Sambuc
836f4a2713aSLionel Sambuc while (FI != FEnd) {
837f4a2713aSLionel Sambuc FieldDecl *field = cast<FieldDecl>(*FI++);
838f4a2713aSLionel Sambuc
839f4a2713aSLionel Sambuc // FIXME: these are somewhat meaningless
840f4a2713aSLionel Sambuc DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
841f4a2713aSLionel Sambuc DeclAccessPair fakeFoundDecl =
842f4a2713aSLionel Sambuc DeclAccessPair::make(field, field->getAccess());
843f4a2713aSLionel Sambuc
844f4a2713aSLionel Sambuc result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false,
845f4a2713aSLionel Sambuc (FI == FEnd? SS : EmptySS), field,
846*0a6a1f1dSLionel Sambuc fakeFoundDecl, memberNameInfo).get();
847f4a2713aSLionel Sambuc }
848f4a2713aSLionel Sambuc
849*0a6a1f1dSLionel Sambuc return result;
850f4a2713aSLionel Sambuc }
851f4a2713aSLionel Sambuc
852f4a2713aSLionel Sambuc static ExprResult
BuildMSPropertyRefExpr(Sema & S,Expr * BaseExpr,bool IsArrow,const CXXScopeSpec & SS,MSPropertyDecl * PD,const DeclarationNameInfo & NameInfo)853f4a2713aSLionel Sambuc BuildMSPropertyRefExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
854f4a2713aSLionel Sambuc const CXXScopeSpec &SS,
855f4a2713aSLionel Sambuc MSPropertyDecl *PD,
856f4a2713aSLionel Sambuc const DeclarationNameInfo &NameInfo) {
857f4a2713aSLionel Sambuc // Property names are always simple identifiers and therefore never
858f4a2713aSLionel Sambuc // require any interesting additional storage.
859f4a2713aSLionel Sambuc return new (S.Context) MSPropertyRefExpr(BaseExpr, PD, IsArrow,
860f4a2713aSLionel Sambuc S.Context.PseudoObjectTy, VK_LValue,
861f4a2713aSLionel Sambuc SS.getWithLocInContext(S.Context),
862f4a2713aSLionel Sambuc NameInfo.getLoc());
863f4a2713aSLionel Sambuc }
864f4a2713aSLionel Sambuc
865f4a2713aSLionel Sambuc /// \brief Build a MemberExpr AST node.
866*0a6a1f1dSLionel Sambuc static MemberExpr *
BuildMemberExpr(Sema & SemaRef,ASTContext & C,Expr * Base,bool isArrow,const CXXScopeSpec & SS,SourceLocation TemplateKWLoc,ValueDecl * Member,DeclAccessPair FoundDecl,const DeclarationNameInfo & MemberNameInfo,QualType Ty,ExprValueKind VK,ExprObjectKind OK,const TemplateArgumentListInfo * TemplateArgs=nullptr)867*0a6a1f1dSLionel Sambuc BuildMemberExpr(Sema &SemaRef, ASTContext &C, Expr *Base, bool isArrow,
868*0a6a1f1dSLionel Sambuc const CXXScopeSpec &SS, SourceLocation TemplateKWLoc,
869*0a6a1f1dSLionel Sambuc ValueDecl *Member, DeclAccessPair FoundDecl,
870*0a6a1f1dSLionel Sambuc const DeclarationNameInfo &MemberNameInfo, QualType Ty,
871f4a2713aSLionel Sambuc ExprValueKind VK, ExprObjectKind OK,
872*0a6a1f1dSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs = nullptr) {
873f4a2713aSLionel Sambuc assert((!isArrow || Base->isRValue()) && "-> base must be a pointer rvalue");
874f4a2713aSLionel Sambuc MemberExpr *E =
875f4a2713aSLionel Sambuc MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C),
876f4a2713aSLionel Sambuc TemplateKWLoc, Member, FoundDecl, MemberNameInfo,
877f4a2713aSLionel Sambuc TemplateArgs, Ty, VK, OK);
878f4a2713aSLionel Sambuc SemaRef.MarkMemberReferenced(E);
879f4a2713aSLionel Sambuc return E;
880f4a2713aSLionel Sambuc }
881f4a2713aSLionel Sambuc
882f4a2713aSLionel Sambuc ExprResult
BuildMemberReferenceExpr(Expr * BaseExpr,QualType BaseExprType,SourceLocation OpLoc,bool IsArrow,const CXXScopeSpec & SS,SourceLocation TemplateKWLoc,NamedDecl * FirstQualifierInScope,LookupResult & R,const TemplateArgumentListInfo * TemplateArgs,bool SuppressQualifierCheck,ActOnMemberAccessExtraArgs * ExtraArgs)883f4a2713aSLionel Sambuc Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
884f4a2713aSLionel Sambuc SourceLocation OpLoc, bool IsArrow,
885f4a2713aSLionel Sambuc const CXXScopeSpec &SS,
886f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
887f4a2713aSLionel Sambuc NamedDecl *FirstQualifierInScope,
888f4a2713aSLionel Sambuc LookupResult &R,
889f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs,
890f4a2713aSLionel Sambuc bool SuppressQualifierCheck,
891f4a2713aSLionel Sambuc ActOnMemberAccessExtraArgs *ExtraArgs) {
892f4a2713aSLionel Sambuc QualType BaseType = BaseExprType;
893f4a2713aSLionel Sambuc if (IsArrow) {
894f4a2713aSLionel Sambuc assert(BaseType->isPointerType());
895f4a2713aSLionel Sambuc BaseType = BaseType->castAs<PointerType>()->getPointeeType();
896f4a2713aSLionel Sambuc }
897f4a2713aSLionel Sambuc R.setBaseObjectType(BaseType);
898f4a2713aSLionel Sambuc
899f4a2713aSLionel Sambuc LambdaScopeInfo *const CurLSI = getCurLambda();
900f4a2713aSLionel Sambuc // If this is an implicit member reference and the overloaded
901f4a2713aSLionel Sambuc // name refers to both static and non-static member functions
902f4a2713aSLionel Sambuc // (i.e. BaseExpr is null) and if we are currently processing a lambda,
903f4a2713aSLionel Sambuc // check if we should/can capture 'this'...
904f4a2713aSLionel Sambuc // Keep this example in mind:
905f4a2713aSLionel Sambuc // struct X {
906f4a2713aSLionel Sambuc // void f(int) { }
907f4a2713aSLionel Sambuc // static void f(double) { }
908f4a2713aSLionel Sambuc //
909f4a2713aSLionel Sambuc // int g() {
910f4a2713aSLionel Sambuc // auto L = [=](auto a) {
911f4a2713aSLionel Sambuc // return [](int i) {
912f4a2713aSLionel Sambuc // return [=](auto b) {
913f4a2713aSLionel Sambuc // f(b);
914f4a2713aSLionel Sambuc // //f(decltype(a){});
915f4a2713aSLionel Sambuc // };
916f4a2713aSLionel Sambuc // };
917f4a2713aSLionel Sambuc // };
918f4a2713aSLionel Sambuc // auto M = L(0.0);
919f4a2713aSLionel Sambuc // auto N = M(3);
920f4a2713aSLionel Sambuc // N(5.32); // OK, must not error.
921f4a2713aSLionel Sambuc // return 0;
922f4a2713aSLionel Sambuc // }
923f4a2713aSLionel Sambuc // };
924f4a2713aSLionel Sambuc //
925f4a2713aSLionel Sambuc if (!BaseExpr && CurLSI) {
926f4a2713aSLionel Sambuc SourceLocation Loc = R.getNameLoc();
927f4a2713aSLionel Sambuc if (SS.getRange().isValid())
928f4a2713aSLionel Sambuc Loc = SS.getRange().getBegin();
929f4a2713aSLionel Sambuc DeclContext *EnclosingFunctionCtx = CurContext->getParent()->getParent();
930f4a2713aSLionel Sambuc // If the enclosing function is not dependent, then this lambda is
931f4a2713aSLionel Sambuc // capture ready, so if we can capture this, do so.
932f4a2713aSLionel Sambuc if (!EnclosingFunctionCtx->isDependentContext()) {
933f4a2713aSLionel Sambuc // If the current lambda and all enclosing lambdas can capture 'this' -
934f4a2713aSLionel Sambuc // then go ahead and capture 'this' (since our unresolved overload set
935f4a2713aSLionel Sambuc // contains both static and non-static member functions).
936f4a2713aSLionel Sambuc if (!CheckCXXThisCapture(Loc, /*Explcit*/false, /*Diagnose*/false))
937f4a2713aSLionel Sambuc CheckCXXThisCapture(Loc);
938f4a2713aSLionel Sambuc } else if (CurContext->isDependentContext()) {
939f4a2713aSLionel Sambuc // ... since this is an implicit member reference, that might potentially
940f4a2713aSLionel Sambuc // involve a 'this' capture, mark 'this' for potential capture in
941f4a2713aSLionel Sambuc // enclosing lambdas.
942f4a2713aSLionel Sambuc if (CurLSI->ImpCaptureStyle != CurLSI->ImpCap_None)
943f4a2713aSLionel Sambuc CurLSI->addPotentialThisCapture(Loc);
944f4a2713aSLionel Sambuc }
945f4a2713aSLionel Sambuc }
946f4a2713aSLionel Sambuc const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo();
947f4a2713aSLionel Sambuc DeclarationName MemberName = MemberNameInfo.getName();
948f4a2713aSLionel Sambuc SourceLocation MemberLoc = MemberNameInfo.getLoc();
949f4a2713aSLionel Sambuc
950f4a2713aSLionel Sambuc if (R.isAmbiguous())
951f4a2713aSLionel Sambuc return ExprError();
952f4a2713aSLionel Sambuc
953f4a2713aSLionel Sambuc if (R.empty()) {
954f4a2713aSLionel Sambuc // Rederive where we looked up.
955f4a2713aSLionel Sambuc DeclContext *DC = (SS.isSet()
956f4a2713aSLionel Sambuc ? computeDeclContext(SS, false)
957f4a2713aSLionel Sambuc : BaseType->getAs<RecordType>()->getDecl());
958f4a2713aSLionel Sambuc
959f4a2713aSLionel Sambuc if (ExtraArgs) {
960f4a2713aSLionel Sambuc ExprResult RetryExpr;
961f4a2713aSLionel Sambuc if (!IsArrow && BaseExpr) {
962f4a2713aSLionel Sambuc SFINAETrap Trap(*this, true);
963f4a2713aSLionel Sambuc ParsedType ObjectType;
964f4a2713aSLionel Sambuc bool MayBePseudoDestructor = false;
965f4a2713aSLionel Sambuc RetryExpr = ActOnStartCXXMemberReference(getCurScope(), BaseExpr,
966f4a2713aSLionel Sambuc OpLoc, tok::arrow, ObjectType,
967f4a2713aSLionel Sambuc MayBePseudoDestructor);
968f4a2713aSLionel Sambuc if (RetryExpr.isUsable() && !Trap.hasErrorOccurred()) {
969f4a2713aSLionel Sambuc CXXScopeSpec TempSS(SS);
970f4a2713aSLionel Sambuc RetryExpr = ActOnMemberAccessExpr(
971f4a2713aSLionel Sambuc ExtraArgs->S, RetryExpr.get(), OpLoc, tok::arrow, TempSS,
972f4a2713aSLionel Sambuc TemplateKWLoc, ExtraArgs->Id, ExtraArgs->ObjCImpDecl,
973f4a2713aSLionel Sambuc ExtraArgs->HasTrailingLParen);
974f4a2713aSLionel Sambuc }
975f4a2713aSLionel Sambuc if (Trap.hasErrorOccurred())
976f4a2713aSLionel Sambuc RetryExpr = ExprError();
977f4a2713aSLionel Sambuc }
978f4a2713aSLionel Sambuc if (RetryExpr.isUsable()) {
979f4a2713aSLionel Sambuc Diag(OpLoc, diag::err_no_member_overloaded_arrow)
980f4a2713aSLionel Sambuc << MemberName << DC << FixItHint::CreateReplacement(OpLoc, "->");
981f4a2713aSLionel Sambuc return RetryExpr;
982f4a2713aSLionel Sambuc }
983f4a2713aSLionel Sambuc }
984f4a2713aSLionel Sambuc
985f4a2713aSLionel Sambuc Diag(R.getNameLoc(), diag::err_no_member)
986f4a2713aSLionel Sambuc << MemberName << DC
987f4a2713aSLionel Sambuc << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange());
988f4a2713aSLionel Sambuc return ExprError();
989f4a2713aSLionel Sambuc }
990f4a2713aSLionel Sambuc
991f4a2713aSLionel Sambuc // Diagnose lookups that find only declarations from a non-base
992f4a2713aSLionel Sambuc // type. This is possible for either qualified lookups (which may
993f4a2713aSLionel Sambuc // have been qualified with an unrelated type) or implicit member
994f4a2713aSLionel Sambuc // expressions (which were found with unqualified lookup and thus
995f4a2713aSLionel Sambuc // may have come from an enclosing scope). Note that it's okay for
996f4a2713aSLionel Sambuc // lookup to find declarations from a non-base type as long as those
997f4a2713aSLionel Sambuc // aren't the ones picked by overload resolution.
998f4a2713aSLionel Sambuc if ((SS.isSet() || !BaseExpr ||
999f4a2713aSLionel Sambuc (isa<CXXThisExpr>(BaseExpr) &&
1000f4a2713aSLionel Sambuc cast<CXXThisExpr>(BaseExpr)->isImplicit())) &&
1001f4a2713aSLionel Sambuc !SuppressQualifierCheck &&
1002f4a2713aSLionel Sambuc CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R))
1003f4a2713aSLionel Sambuc return ExprError();
1004f4a2713aSLionel Sambuc
1005f4a2713aSLionel Sambuc // Construct an unresolved result if we in fact got an unresolved
1006f4a2713aSLionel Sambuc // result.
1007f4a2713aSLionel Sambuc if (R.isOverloadedResult() || R.isUnresolvableResult()) {
1008f4a2713aSLionel Sambuc // Suppress any lookup-related diagnostics; we'll do these when we
1009f4a2713aSLionel Sambuc // pick a member.
1010f4a2713aSLionel Sambuc R.suppressDiagnostics();
1011f4a2713aSLionel Sambuc
1012f4a2713aSLionel Sambuc UnresolvedMemberExpr *MemExpr
1013f4a2713aSLionel Sambuc = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(),
1014f4a2713aSLionel Sambuc BaseExpr, BaseExprType,
1015f4a2713aSLionel Sambuc IsArrow, OpLoc,
1016f4a2713aSLionel Sambuc SS.getWithLocInContext(Context),
1017f4a2713aSLionel Sambuc TemplateKWLoc, MemberNameInfo,
1018f4a2713aSLionel Sambuc TemplateArgs, R.begin(), R.end());
1019f4a2713aSLionel Sambuc
1020*0a6a1f1dSLionel Sambuc return MemExpr;
1021f4a2713aSLionel Sambuc }
1022f4a2713aSLionel Sambuc
1023f4a2713aSLionel Sambuc assert(R.isSingleResult());
1024f4a2713aSLionel Sambuc DeclAccessPair FoundDecl = R.begin().getPair();
1025f4a2713aSLionel Sambuc NamedDecl *MemberDecl = R.getFoundDecl();
1026f4a2713aSLionel Sambuc
1027f4a2713aSLionel Sambuc // FIXME: diagnose the presence of template arguments now.
1028f4a2713aSLionel Sambuc
1029f4a2713aSLionel Sambuc // If the decl being referenced had an error, return an error for this
1030f4a2713aSLionel Sambuc // sub-expr without emitting another error, in order to avoid cascading
1031f4a2713aSLionel Sambuc // error cases.
1032f4a2713aSLionel Sambuc if (MemberDecl->isInvalidDecl())
1033f4a2713aSLionel Sambuc return ExprError();
1034f4a2713aSLionel Sambuc
1035f4a2713aSLionel Sambuc // Handle the implicit-member-access case.
1036f4a2713aSLionel Sambuc if (!BaseExpr) {
1037f4a2713aSLionel Sambuc // If this is not an instance member, convert to a non-member access.
1038f4a2713aSLionel Sambuc if (!MemberDecl->isCXXInstanceMember())
1039f4a2713aSLionel Sambuc return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl);
1040f4a2713aSLionel Sambuc
1041f4a2713aSLionel Sambuc SourceLocation Loc = R.getNameLoc();
1042f4a2713aSLionel Sambuc if (SS.getRange().isValid())
1043f4a2713aSLionel Sambuc Loc = SS.getRange().getBegin();
1044f4a2713aSLionel Sambuc CheckCXXThisCapture(Loc);
1045f4a2713aSLionel Sambuc BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true);
1046f4a2713aSLionel Sambuc }
1047f4a2713aSLionel Sambuc
1048f4a2713aSLionel Sambuc bool ShouldCheckUse = true;
1049f4a2713aSLionel Sambuc if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) {
1050f4a2713aSLionel Sambuc // Don't diagnose the use of a virtual member function unless it's
1051f4a2713aSLionel Sambuc // explicitly qualified.
1052f4a2713aSLionel Sambuc if (MD->isVirtual() && !SS.isSet())
1053f4a2713aSLionel Sambuc ShouldCheckUse = false;
1054f4a2713aSLionel Sambuc }
1055f4a2713aSLionel Sambuc
1056f4a2713aSLionel Sambuc // Check the use of this member.
1057*0a6a1f1dSLionel Sambuc if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc))
1058f4a2713aSLionel Sambuc return ExprError();
1059f4a2713aSLionel Sambuc
1060f4a2713aSLionel Sambuc if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl))
1061f4a2713aSLionel Sambuc return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow,
1062f4a2713aSLionel Sambuc SS, FD, FoundDecl, MemberNameInfo);
1063f4a2713aSLionel Sambuc
1064f4a2713aSLionel Sambuc if (MSPropertyDecl *PD = dyn_cast<MSPropertyDecl>(MemberDecl))
1065f4a2713aSLionel Sambuc return BuildMSPropertyRefExpr(*this, BaseExpr, IsArrow, SS, PD,
1066f4a2713aSLionel Sambuc MemberNameInfo);
1067f4a2713aSLionel Sambuc
1068f4a2713aSLionel Sambuc if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl))
1069f4a2713aSLionel Sambuc // We may have found a field within an anonymous union or struct
1070f4a2713aSLionel Sambuc // (C++ [class.union]).
1071f4a2713aSLionel Sambuc return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD,
1072f4a2713aSLionel Sambuc FoundDecl, BaseExpr,
1073f4a2713aSLionel Sambuc OpLoc);
1074f4a2713aSLionel Sambuc
1075f4a2713aSLionel Sambuc if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) {
1076*0a6a1f1dSLionel Sambuc return BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS, TemplateKWLoc,
1077*0a6a1f1dSLionel Sambuc Var, FoundDecl, MemberNameInfo,
1078*0a6a1f1dSLionel Sambuc Var->getType().getNonReferenceType(), VK_LValue,
1079*0a6a1f1dSLionel Sambuc OK_Ordinary);
1080f4a2713aSLionel Sambuc }
1081f4a2713aSLionel Sambuc
1082f4a2713aSLionel Sambuc if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) {
1083f4a2713aSLionel Sambuc ExprValueKind valueKind;
1084f4a2713aSLionel Sambuc QualType type;
1085f4a2713aSLionel Sambuc if (MemberFn->isInstance()) {
1086f4a2713aSLionel Sambuc valueKind = VK_RValue;
1087f4a2713aSLionel Sambuc type = Context.BoundMemberTy;
1088f4a2713aSLionel Sambuc } else {
1089f4a2713aSLionel Sambuc valueKind = VK_LValue;
1090f4a2713aSLionel Sambuc type = MemberFn->getType();
1091f4a2713aSLionel Sambuc }
1092f4a2713aSLionel Sambuc
1093*0a6a1f1dSLionel Sambuc return BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS, TemplateKWLoc,
1094*0a6a1f1dSLionel Sambuc MemberFn, FoundDecl, MemberNameInfo, type, valueKind,
1095*0a6a1f1dSLionel Sambuc OK_Ordinary);
1096f4a2713aSLionel Sambuc }
1097f4a2713aSLionel Sambuc assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?");
1098f4a2713aSLionel Sambuc
1099f4a2713aSLionel Sambuc if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) {
1100*0a6a1f1dSLionel Sambuc return BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS, TemplateKWLoc,
1101*0a6a1f1dSLionel Sambuc Enum, FoundDecl, MemberNameInfo, Enum->getType(),
1102*0a6a1f1dSLionel Sambuc VK_RValue, OK_Ordinary);
1103f4a2713aSLionel Sambuc }
1104f4a2713aSLionel Sambuc
1105f4a2713aSLionel Sambuc // We found something that we didn't expect. Complain.
1106f4a2713aSLionel Sambuc if (isa<TypeDecl>(MemberDecl))
1107f4a2713aSLionel Sambuc Diag(MemberLoc, diag::err_typecheck_member_reference_type)
1108f4a2713aSLionel Sambuc << MemberName << BaseType << int(IsArrow);
1109f4a2713aSLionel Sambuc else
1110f4a2713aSLionel Sambuc Diag(MemberLoc, diag::err_typecheck_member_reference_unknown)
1111f4a2713aSLionel Sambuc << MemberName << BaseType << int(IsArrow);
1112f4a2713aSLionel Sambuc
1113f4a2713aSLionel Sambuc Diag(MemberDecl->getLocation(), diag::note_member_declared_here)
1114f4a2713aSLionel Sambuc << MemberName;
1115f4a2713aSLionel Sambuc R.suppressDiagnostics();
1116f4a2713aSLionel Sambuc return ExprError();
1117f4a2713aSLionel Sambuc }
1118f4a2713aSLionel Sambuc
1119f4a2713aSLionel Sambuc /// Given that normal member access failed on the given expression,
1120f4a2713aSLionel Sambuc /// and given that the expression's type involves builtin-id or
1121f4a2713aSLionel Sambuc /// builtin-Class, decide whether substituting in the redefinition
1122f4a2713aSLionel Sambuc /// types would be profitable. The redefinition type is whatever
1123f4a2713aSLionel Sambuc /// this translation unit tried to typedef to id/Class; we store
1124f4a2713aSLionel Sambuc /// it to the side and then re-use it in places like this.
ShouldTryAgainWithRedefinitionType(Sema & S,ExprResult & base)1125f4a2713aSLionel Sambuc static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
1126f4a2713aSLionel Sambuc const ObjCObjectPointerType *opty
1127f4a2713aSLionel Sambuc = base.get()->getType()->getAs<ObjCObjectPointerType>();
1128f4a2713aSLionel Sambuc if (!opty) return false;
1129f4a2713aSLionel Sambuc
1130f4a2713aSLionel Sambuc const ObjCObjectType *ty = opty->getObjectType();
1131f4a2713aSLionel Sambuc
1132f4a2713aSLionel Sambuc QualType redef;
1133f4a2713aSLionel Sambuc if (ty->isObjCId()) {
1134f4a2713aSLionel Sambuc redef = S.Context.getObjCIdRedefinitionType();
1135f4a2713aSLionel Sambuc } else if (ty->isObjCClass()) {
1136f4a2713aSLionel Sambuc redef = S.Context.getObjCClassRedefinitionType();
1137f4a2713aSLionel Sambuc } else {
1138f4a2713aSLionel Sambuc return false;
1139f4a2713aSLionel Sambuc }
1140f4a2713aSLionel Sambuc
1141f4a2713aSLionel Sambuc // Do the substitution as long as the redefinition type isn't just a
1142f4a2713aSLionel Sambuc // possibly-qualified pointer to builtin-id or builtin-Class again.
1143f4a2713aSLionel Sambuc opty = redef->getAs<ObjCObjectPointerType>();
1144f4a2713aSLionel Sambuc if (opty && !opty->getObjectType()->getInterface())
1145f4a2713aSLionel Sambuc return false;
1146f4a2713aSLionel Sambuc
1147*0a6a1f1dSLionel Sambuc base = S.ImpCastExprToType(base.get(), redef, CK_BitCast);
1148f4a2713aSLionel Sambuc return true;
1149f4a2713aSLionel Sambuc }
1150f4a2713aSLionel Sambuc
isRecordType(QualType T)1151f4a2713aSLionel Sambuc static bool isRecordType(QualType T) {
1152f4a2713aSLionel Sambuc return T->isRecordType();
1153f4a2713aSLionel Sambuc }
isPointerToRecordType(QualType T)1154f4a2713aSLionel Sambuc static bool isPointerToRecordType(QualType T) {
1155f4a2713aSLionel Sambuc if (const PointerType *PT = T->getAs<PointerType>())
1156f4a2713aSLionel Sambuc return PT->getPointeeType()->isRecordType();
1157f4a2713aSLionel Sambuc return false;
1158f4a2713aSLionel Sambuc }
1159f4a2713aSLionel Sambuc
1160f4a2713aSLionel Sambuc /// Perform conversions on the LHS of a member access expression.
1161f4a2713aSLionel Sambuc ExprResult
PerformMemberExprBaseConversion(Expr * Base,bool IsArrow)1162f4a2713aSLionel Sambuc Sema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) {
1163f4a2713aSLionel Sambuc if (IsArrow && !Base->getType()->isFunctionType())
1164f4a2713aSLionel Sambuc return DefaultFunctionArrayLvalueConversion(Base);
1165f4a2713aSLionel Sambuc
1166f4a2713aSLionel Sambuc return CheckPlaceholderExpr(Base);
1167f4a2713aSLionel Sambuc }
1168f4a2713aSLionel Sambuc
1169f4a2713aSLionel Sambuc /// Look up the given member of the given non-type-dependent
1170f4a2713aSLionel Sambuc /// expression. This can return in one of two ways:
1171f4a2713aSLionel Sambuc /// * If it returns a sentinel null-but-valid result, the caller will
1172f4a2713aSLionel Sambuc /// assume that lookup was performed and the results written into
1173f4a2713aSLionel Sambuc /// the provided structure. It will take over from there.
1174f4a2713aSLionel Sambuc /// * Otherwise, the returned expression will be produced in place of
1175f4a2713aSLionel Sambuc /// an ordinary member expression.
1176f4a2713aSLionel Sambuc ///
1177f4a2713aSLionel Sambuc /// The ObjCImpDecl bit is a gross hack that will need to be properly
1178f4a2713aSLionel Sambuc /// fixed for ObjC++.
LookupMemberExpr(Sema & S,LookupResult & R,ExprResult & BaseExpr,bool & IsArrow,SourceLocation OpLoc,CXXScopeSpec & SS,Decl * ObjCImpDecl,bool HasTemplateArgs)1179*0a6a1f1dSLionel Sambuc static ExprResult LookupMemberExpr(Sema &S, LookupResult &R,
1180*0a6a1f1dSLionel Sambuc ExprResult &BaseExpr, bool &IsArrow,
1181*0a6a1f1dSLionel Sambuc SourceLocation OpLoc, CXXScopeSpec &SS,
1182f4a2713aSLionel Sambuc Decl *ObjCImpDecl, bool HasTemplateArgs) {
1183f4a2713aSLionel Sambuc assert(BaseExpr.get() && "no base expression");
1184f4a2713aSLionel Sambuc
1185f4a2713aSLionel Sambuc // Perform default conversions.
1186*0a6a1f1dSLionel Sambuc BaseExpr = S.PerformMemberExprBaseConversion(BaseExpr.get(), IsArrow);
1187f4a2713aSLionel Sambuc if (BaseExpr.isInvalid())
1188f4a2713aSLionel Sambuc return ExprError();
1189f4a2713aSLionel Sambuc
1190f4a2713aSLionel Sambuc QualType BaseType = BaseExpr.get()->getType();
1191f4a2713aSLionel Sambuc assert(!BaseType->isDependentType());
1192f4a2713aSLionel Sambuc
1193f4a2713aSLionel Sambuc DeclarationName MemberName = R.getLookupName();
1194f4a2713aSLionel Sambuc SourceLocation MemberLoc = R.getNameLoc();
1195f4a2713aSLionel Sambuc
1196f4a2713aSLionel Sambuc // For later type-checking purposes, turn arrow accesses into dot
1197f4a2713aSLionel Sambuc // accesses. The only access type we support that doesn't follow
1198f4a2713aSLionel Sambuc // the C equivalence "a->b === (*a).b" is ObjC property accesses,
1199f4a2713aSLionel Sambuc // and those never use arrows, so this is unaffected.
1200f4a2713aSLionel Sambuc if (IsArrow) {
1201f4a2713aSLionel Sambuc if (const PointerType *Ptr = BaseType->getAs<PointerType>())
1202f4a2713aSLionel Sambuc BaseType = Ptr->getPointeeType();
1203f4a2713aSLionel Sambuc else if (const ObjCObjectPointerType *Ptr
1204f4a2713aSLionel Sambuc = BaseType->getAs<ObjCObjectPointerType>())
1205f4a2713aSLionel Sambuc BaseType = Ptr->getPointeeType();
1206f4a2713aSLionel Sambuc else if (BaseType->isRecordType()) {
1207f4a2713aSLionel Sambuc // Recover from arrow accesses to records, e.g.:
1208f4a2713aSLionel Sambuc // struct MyRecord foo;
1209f4a2713aSLionel Sambuc // foo->bar
1210f4a2713aSLionel Sambuc // This is actually well-formed in C++ if MyRecord has an
1211f4a2713aSLionel Sambuc // overloaded operator->, but that should have been dealt with
1212f4a2713aSLionel Sambuc // by now--or a diagnostic message already issued if a problem
1213f4a2713aSLionel Sambuc // was encountered while looking for the overloaded operator->.
1214*0a6a1f1dSLionel Sambuc if (!S.getLangOpts().CPlusPlus) {
1215*0a6a1f1dSLionel Sambuc S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
1216f4a2713aSLionel Sambuc << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
1217f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(OpLoc, ".");
1218f4a2713aSLionel Sambuc }
1219f4a2713aSLionel Sambuc IsArrow = false;
1220f4a2713aSLionel Sambuc } else if (BaseType->isFunctionType()) {
1221f4a2713aSLionel Sambuc goto fail;
1222f4a2713aSLionel Sambuc } else {
1223*0a6a1f1dSLionel Sambuc S.Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
1224f4a2713aSLionel Sambuc << BaseType << BaseExpr.get()->getSourceRange();
1225f4a2713aSLionel Sambuc return ExprError();
1226f4a2713aSLionel Sambuc }
1227f4a2713aSLionel Sambuc }
1228f4a2713aSLionel Sambuc
1229f4a2713aSLionel Sambuc // Handle field access to simple records.
1230f4a2713aSLionel Sambuc if (const RecordType *RTy = BaseType->getAs<RecordType>()) {
1231*0a6a1f1dSLionel Sambuc TypoExpr *TE = nullptr;
1232*0a6a1f1dSLionel Sambuc if (LookupMemberExprInRecord(S, R, BaseExpr.get(), RTy,
1233*0a6a1f1dSLionel Sambuc OpLoc, IsArrow, SS, HasTemplateArgs, TE))
1234f4a2713aSLionel Sambuc return ExprError();
1235f4a2713aSLionel Sambuc
1236f4a2713aSLionel Sambuc // Returning valid-but-null is how we indicate to the caller that
1237*0a6a1f1dSLionel Sambuc // the lookup result was filled in. If typo correction was attempted and
1238*0a6a1f1dSLionel Sambuc // failed, the lookup result will have been cleared--that combined with the
1239*0a6a1f1dSLionel Sambuc // valid-but-null ExprResult will trigger the appropriate diagnostics.
1240*0a6a1f1dSLionel Sambuc return ExprResult(TE);
1241f4a2713aSLionel Sambuc }
1242f4a2713aSLionel Sambuc
1243f4a2713aSLionel Sambuc // Handle ivar access to Objective-C objects.
1244f4a2713aSLionel Sambuc if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) {
1245f4a2713aSLionel Sambuc if (!SS.isEmpty() && !SS.isInvalid()) {
1246*0a6a1f1dSLionel Sambuc S.Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1247f4a2713aSLionel Sambuc << 1 << SS.getScopeRep()
1248f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(SS.getRange());
1249f4a2713aSLionel Sambuc SS.clear();
1250f4a2713aSLionel Sambuc }
1251f4a2713aSLionel Sambuc
1252f4a2713aSLionel Sambuc IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1253f4a2713aSLionel Sambuc
1254f4a2713aSLionel Sambuc // There are three cases for the base type:
1255f4a2713aSLionel Sambuc // - builtin id (qualified or unqualified)
1256f4a2713aSLionel Sambuc // - builtin Class (qualified or unqualified)
1257f4a2713aSLionel Sambuc // - an interface
1258f4a2713aSLionel Sambuc ObjCInterfaceDecl *IDecl = OTy->getInterface();
1259f4a2713aSLionel Sambuc if (!IDecl) {
1260*0a6a1f1dSLionel Sambuc if (S.getLangOpts().ObjCAutoRefCount &&
1261f4a2713aSLionel Sambuc (OTy->isObjCId() || OTy->isObjCClass()))
1262f4a2713aSLionel Sambuc goto fail;
1263f4a2713aSLionel Sambuc // There's an implicit 'isa' ivar on all objects.
1264f4a2713aSLionel Sambuc // But we only actually find it this way on objects of type 'id',
1265f4a2713aSLionel Sambuc // apparently.
1266f4a2713aSLionel Sambuc if (OTy->isObjCId() && Member->isStr("isa"))
1267*0a6a1f1dSLionel Sambuc return new (S.Context) ObjCIsaExpr(BaseExpr.get(), IsArrow, MemberLoc,
1268*0a6a1f1dSLionel Sambuc OpLoc, S.Context.getObjCClassType());
1269*0a6a1f1dSLionel Sambuc if (ShouldTryAgainWithRedefinitionType(S, BaseExpr))
1270*0a6a1f1dSLionel Sambuc return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1271f4a2713aSLionel Sambuc ObjCImpDecl, HasTemplateArgs);
1272f4a2713aSLionel Sambuc goto fail;
1273f4a2713aSLionel Sambuc }
1274f4a2713aSLionel Sambuc
1275*0a6a1f1dSLionel Sambuc if (S.RequireCompleteType(OpLoc, BaseType,
1276*0a6a1f1dSLionel Sambuc diag::err_typecheck_incomplete_tag,
1277f4a2713aSLionel Sambuc BaseExpr.get()))
1278f4a2713aSLionel Sambuc return ExprError();
1279f4a2713aSLionel Sambuc
1280*0a6a1f1dSLionel Sambuc ObjCInterfaceDecl *ClassDeclared = nullptr;
1281f4a2713aSLionel Sambuc ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared);
1282f4a2713aSLionel Sambuc
1283f4a2713aSLionel Sambuc if (!IV) {
1284f4a2713aSLionel Sambuc // Attempt to correct for typos in ivar names.
1285*0a6a1f1dSLionel Sambuc auto Validator = llvm::make_unique<DeclFilterCCC<ObjCIvarDecl>>();
1286*0a6a1f1dSLionel Sambuc Validator->IsObjCIvarLookup = IsArrow;
1287*0a6a1f1dSLionel Sambuc if (TypoCorrection Corrected = S.CorrectTypo(
1288*0a6a1f1dSLionel Sambuc R.getLookupNameInfo(), Sema::LookupMemberName, nullptr, nullptr,
1289*0a6a1f1dSLionel Sambuc std::move(Validator), Sema::CTK_ErrorRecovery, IDecl)) {
1290f4a2713aSLionel Sambuc IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>();
1291*0a6a1f1dSLionel Sambuc S.diagnoseTypo(
1292*0a6a1f1dSLionel Sambuc Corrected,
1293*0a6a1f1dSLionel Sambuc S.PDiag(diag::err_typecheck_member_reference_ivar_suggest)
1294f4a2713aSLionel Sambuc << IDecl->getDeclName() << MemberName);
1295f4a2713aSLionel Sambuc
1296f4a2713aSLionel Sambuc // Figure out the class that declares the ivar.
1297f4a2713aSLionel Sambuc assert(!ClassDeclared);
1298f4a2713aSLionel Sambuc Decl *D = cast<Decl>(IV->getDeclContext());
1299f4a2713aSLionel Sambuc if (ObjCCategoryDecl *CAT = dyn_cast<ObjCCategoryDecl>(D))
1300f4a2713aSLionel Sambuc D = CAT->getClassInterface();
1301f4a2713aSLionel Sambuc ClassDeclared = cast<ObjCInterfaceDecl>(D);
1302f4a2713aSLionel Sambuc } else {
1303f4a2713aSLionel Sambuc if (IsArrow && IDecl->FindPropertyDeclaration(Member)) {
1304*0a6a1f1dSLionel Sambuc S.Diag(MemberLoc, diag::err_property_found_suggest)
1305f4a2713aSLionel Sambuc << Member << BaseExpr.get()->getType()
1306f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(OpLoc, ".");
1307f4a2713aSLionel Sambuc return ExprError();
1308f4a2713aSLionel Sambuc }
1309f4a2713aSLionel Sambuc
1310*0a6a1f1dSLionel Sambuc S.Diag(MemberLoc, diag::err_typecheck_member_reference_ivar)
1311f4a2713aSLionel Sambuc << IDecl->getDeclName() << MemberName
1312f4a2713aSLionel Sambuc << BaseExpr.get()->getSourceRange();
1313f4a2713aSLionel Sambuc return ExprError();
1314f4a2713aSLionel Sambuc }
1315f4a2713aSLionel Sambuc }
1316f4a2713aSLionel Sambuc
1317f4a2713aSLionel Sambuc assert(ClassDeclared);
1318f4a2713aSLionel Sambuc
1319f4a2713aSLionel Sambuc // If the decl being referenced had an error, return an error for this
1320f4a2713aSLionel Sambuc // sub-expr without emitting another error, in order to avoid cascading
1321f4a2713aSLionel Sambuc // error cases.
1322f4a2713aSLionel Sambuc if (IV->isInvalidDecl())
1323f4a2713aSLionel Sambuc return ExprError();
1324f4a2713aSLionel Sambuc
1325f4a2713aSLionel Sambuc // Check whether we can reference this field.
1326*0a6a1f1dSLionel Sambuc if (S.DiagnoseUseOfDecl(IV, MemberLoc))
1327f4a2713aSLionel Sambuc return ExprError();
1328f4a2713aSLionel Sambuc if (IV->getAccessControl() != ObjCIvarDecl::Public &&
1329f4a2713aSLionel Sambuc IV->getAccessControl() != ObjCIvarDecl::Package) {
1330*0a6a1f1dSLionel Sambuc ObjCInterfaceDecl *ClassOfMethodDecl = nullptr;
1331*0a6a1f1dSLionel Sambuc if (ObjCMethodDecl *MD = S.getCurMethodDecl())
1332f4a2713aSLionel Sambuc ClassOfMethodDecl = MD->getClassInterface();
1333*0a6a1f1dSLionel Sambuc else if (ObjCImpDecl && S.getCurFunctionDecl()) {
1334f4a2713aSLionel Sambuc // Case of a c-function declared inside an objc implementation.
1335f4a2713aSLionel Sambuc // FIXME: For a c-style function nested inside an objc implementation
1336f4a2713aSLionel Sambuc // class, there is no implementation context available, so we pass
1337f4a2713aSLionel Sambuc // down the context as argument to this routine. Ideally, this context
1338f4a2713aSLionel Sambuc // need be passed down in the AST node and somehow calculated from the
1339f4a2713aSLionel Sambuc // AST for a function decl.
1340f4a2713aSLionel Sambuc if (ObjCImplementationDecl *IMPD =
1341f4a2713aSLionel Sambuc dyn_cast<ObjCImplementationDecl>(ObjCImpDecl))
1342f4a2713aSLionel Sambuc ClassOfMethodDecl = IMPD->getClassInterface();
1343f4a2713aSLionel Sambuc else if (ObjCCategoryImplDecl* CatImplClass =
1344f4a2713aSLionel Sambuc dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl))
1345f4a2713aSLionel Sambuc ClassOfMethodDecl = CatImplClass->getClassInterface();
1346f4a2713aSLionel Sambuc }
1347*0a6a1f1dSLionel Sambuc if (!S.getLangOpts().DebuggerSupport) {
1348f4a2713aSLionel Sambuc if (IV->getAccessControl() == ObjCIvarDecl::Private) {
1349f4a2713aSLionel Sambuc if (!declaresSameEntity(ClassDeclared, IDecl) ||
1350f4a2713aSLionel Sambuc !declaresSameEntity(ClassOfMethodDecl, ClassDeclared))
1351*0a6a1f1dSLionel Sambuc S.Diag(MemberLoc, diag::error_private_ivar_access)
1352f4a2713aSLionel Sambuc << IV->getDeclName();
1353f4a2713aSLionel Sambuc } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl))
1354f4a2713aSLionel Sambuc // @protected
1355*0a6a1f1dSLionel Sambuc S.Diag(MemberLoc, diag::error_protected_ivar_access)
1356f4a2713aSLionel Sambuc << IV->getDeclName();
1357f4a2713aSLionel Sambuc }
1358f4a2713aSLionel Sambuc }
1359f4a2713aSLionel Sambuc bool warn = true;
1360*0a6a1f1dSLionel Sambuc if (S.getLangOpts().ObjCAutoRefCount) {
1361f4a2713aSLionel Sambuc Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts();
1362f4a2713aSLionel Sambuc if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp))
1363f4a2713aSLionel Sambuc if (UO->getOpcode() == UO_Deref)
1364f4a2713aSLionel Sambuc BaseExp = UO->getSubExpr()->IgnoreParenCasts();
1365f4a2713aSLionel Sambuc
1366f4a2713aSLionel Sambuc if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp))
1367f4a2713aSLionel Sambuc if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
1368*0a6a1f1dSLionel Sambuc S.Diag(DE->getLocation(), diag::error_arc_weak_ivar_access);
1369f4a2713aSLionel Sambuc warn = false;
1370f4a2713aSLionel Sambuc }
1371f4a2713aSLionel Sambuc }
1372f4a2713aSLionel Sambuc if (warn) {
1373*0a6a1f1dSLionel Sambuc if (ObjCMethodDecl *MD = S.getCurMethodDecl()) {
1374f4a2713aSLionel Sambuc ObjCMethodFamily MF = MD->getMethodFamily();
1375f4a2713aSLionel Sambuc warn = (MF != OMF_init && MF != OMF_dealloc &&
1376f4a2713aSLionel Sambuc MF != OMF_finalize &&
1377*0a6a1f1dSLionel Sambuc !S.IvarBacksCurrentMethodAccessor(IDecl, MD, IV));
1378f4a2713aSLionel Sambuc }
1379f4a2713aSLionel Sambuc if (warn)
1380*0a6a1f1dSLionel Sambuc S.Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName();
1381f4a2713aSLionel Sambuc }
1382f4a2713aSLionel Sambuc
1383*0a6a1f1dSLionel Sambuc ObjCIvarRefExpr *Result = new (S.Context) ObjCIvarRefExpr(
1384*0a6a1f1dSLionel Sambuc IV, IV->getType(), MemberLoc, OpLoc, BaseExpr.get(), IsArrow);
1385f4a2713aSLionel Sambuc
1386*0a6a1f1dSLionel Sambuc if (S.getLangOpts().ObjCAutoRefCount) {
1387f4a2713aSLionel Sambuc if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
1388*0a6a1f1dSLionel Sambuc if (!S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, MemberLoc))
1389*0a6a1f1dSLionel Sambuc S.recordUseOfEvaluatedWeak(Result);
1390f4a2713aSLionel Sambuc }
1391f4a2713aSLionel Sambuc }
1392f4a2713aSLionel Sambuc
1393*0a6a1f1dSLionel Sambuc return Result;
1394f4a2713aSLionel Sambuc }
1395f4a2713aSLionel Sambuc
1396f4a2713aSLionel Sambuc // Objective-C property access.
1397f4a2713aSLionel Sambuc const ObjCObjectPointerType *OPT;
1398f4a2713aSLionel Sambuc if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) {
1399f4a2713aSLionel Sambuc if (!SS.isEmpty() && !SS.isInvalid()) {
1400*0a6a1f1dSLionel Sambuc S.Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1401*0a6a1f1dSLionel Sambuc << 0 << SS.getScopeRep() << FixItHint::CreateRemoval(SS.getRange());
1402f4a2713aSLionel Sambuc SS.clear();
1403f4a2713aSLionel Sambuc }
1404f4a2713aSLionel Sambuc
1405f4a2713aSLionel Sambuc // This actually uses the base as an r-value.
1406*0a6a1f1dSLionel Sambuc BaseExpr = S.DefaultLvalueConversion(BaseExpr.get());
1407f4a2713aSLionel Sambuc if (BaseExpr.isInvalid())
1408f4a2713aSLionel Sambuc return ExprError();
1409f4a2713aSLionel Sambuc
1410*0a6a1f1dSLionel Sambuc assert(S.Context.hasSameUnqualifiedType(BaseType,
1411*0a6a1f1dSLionel Sambuc BaseExpr.get()->getType()));
1412f4a2713aSLionel Sambuc
1413f4a2713aSLionel Sambuc IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1414f4a2713aSLionel Sambuc
1415f4a2713aSLionel Sambuc const ObjCObjectType *OT = OPT->getObjectType();
1416f4a2713aSLionel Sambuc
1417f4a2713aSLionel Sambuc // id, with and without qualifiers.
1418f4a2713aSLionel Sambuc if (OT->isObjCId()) {
1419f4a2713aSLionel Sambuc // Check protocols on qualified interfaces.
1420*0a6a1f1dSLionel Sambuc Selector Sel = S.PP.getSelectorTable().getNullarySelector(Member);
1421*0a6a1f1dSLionel Sambuc if (Decl *PMDecl =
1422*0a6a1f1dSLionel Sambuc FindGetterSetterNameDecl(OPT, Member, Sel, S.Context)) {
1423f4a2713aSLionel Sambuc if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) {
1424f4a2713aSLionel Sambuc // Check the use of this declaration
1425*0a6a1f1dSLionel Sambuc if (S.DiagnoseUseOfDecl(PD, MemberLoc))
1426f4a2713aSLionel Sambuc return ExprError();
1427f4a2713aSLionel Sambuc
1428*0a6a1f1dSLionel Sambuc return new (S.Context)
1429*0a6a1f1dSLionel Sambuc ObjCPropertyRefExpr(PD, S.Context.PseudoObjectTy, VK_LValue,
1430*0a6a1f1dSLionel Sambuc OK_ObjCProperty, MemberLoc, BaseExpr.get());
1431f4a2713aSLionel Sambuc }
1432f4a2713aSLionel Sambuc
1433f4a2713aSLionel Sambuc if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) {
1434f4a2713aSLionel Sambuc // Check the use of this method.
1435*0a6a1f1dSLionel Sambuc if (S.DiagnoseUseOfDecl(OMD, MemberLoc))
1436f4a2713aSLionel Sambuc return ExprError();
1437f4a2713aSLionel Sambuc Selector SetterSel =
1438*0a6a1f1dSLionel Sambuc SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(),
1439*0a6a1f1dSLionel Sambuc S.PP.getSelectorTable(),
1440f4a2713aSLionel Sambuc Member);
1441*0a6a1f1dSLionel Sambuc ObjCMethodDecl *SMD = nullptr;
1442*0a6a1f1dSLionel Sambuc if (Decl *SDecl = FindGetterSetterNameDecl(OPT,
1443*0a6a1f1dSLionel Sambuc /*Property id*/ nullptr,
1444*0a6a1f1dSLionel Sambuc SetterSel, S.Context))
1445f4a2713aSLionel Sambuc SMD = dyn_cast<ObjCMethodDecl>(SDecl);
1446f4a2713aSLionel Sambuc
1447*0a6a1f1dSLionel Sambuc return new (S.Context)
1448*0a6a1f1dSLionel Sambuc ObjCPropertyRefExpr(OMD, SMD, S.Context.PseudoObjectTy, VK_LValue,
1449*0a6a1f1dSLionel Sambuc OK_ObjCProperty, MemberLoc, BaseExpr.get());
1450f4a2713aSLionel Sambuc }
1451f4a2713aSLionel Sambuc }
1452f4a2713aSLionel Sambuc // Use of id.member can only be for a property reference. Do not
1453f4a2713aSLionel Sambuc // use the 'id' redefinition in this case.
1454*0a6a1f1dSLionel Sambuc if (IsArrow && ShouldTryAgainWithRedefinitionType(S, BaseExpr))
1455*0a6a1f1dSLionel Sambuc return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1456f4a2713aSLionel Sambuc ObjCImpDecl, HasTemplateArgs);
1457f4a2713aSLionel Sambuc
1458*0a6a1f1dSLionel Sambuc return ExprError(S.Diag(MemberLoc, diag::err_property_not_found)
1459f4a2713aSLionel Sambuc << MemberName << BaseType);
1460f4a2713aSLionel Sambuc }
1461f4a2713aSLionel Sambuc
1462f4a2713aSLionel Sambuc // 'Class', unqualified only.
1463f4a2713aSLionel Sambuc if (OT->isObjCClass()) {
1464f4a2713aSLionel Sambuc // Only works in a method declaration (??!).
1465*0a6a1f1dSLionel Sambuc ObjCMethodDecl *MD = S.getCurMethodDecl();
1466f4a2713aSLionel Sambuc if (!MD) {
1467*0a6a1f1dSLionel Sambuc if (ShouldTryAgainWithRedefinitionType(S, BaseExpr))
1468*0a6a1f1dSLionel Sambuc return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1469f4a2713aSLionel Sambuc ObjCImpDecl, HasTemplateArgs);
1470f4a2713aSLionel Sambuc
1471f4a2713aSLionel Sambuc goto fail;
1472f4a2713aSLionel Sambuc }
1473f4a2713aSLionel Sambuc
1474f4a2713aSLionel Sambuc // Also must look for a getter name which uses property syntax.
1475*0a6a1f1dSLionel Sambuc Selector Sel = S.PP.getSelectorTable().getNullarySelector(Member);
1476f4a2713aSLionel Sambuc ObjCInterfaceDecl *IFace = MD->getClassInterface();
1477f4a2713aSLionel Sambuc ObjCMethodDecl *Getter;
1478f4a2713aSLionel Sambuc if ((Getter = IFace->lookupClassMethod(Sel))) {
1479f4a2713aSLionel Sambuc // Check the use of this method.
1480*0a6a1f1dSLionel Sambuc if (S.DiagnoseUseOfDecl(Getter, MemberLoc))
1481f4a2713aSLionel Sambuc return ExprError();
1482f4a2713aSLionel Sambuc } else
1483f4a2713aSLionel Sambuc Getter = IFace->lookupPrivateMethod(Sel, false);
1484f4a2713aSLionel Sambuc // If we found a getter then this may be a valid dot-reference, we
1485f4a2713aSLionel Sambuc // will look for the matching setter, in case it is needed.
1486f4a2713aSLionel Sambuc Selector SetterSel =
1487*0a6a1f1dSLionel Sambuc SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(),
1488*0a6a1f1dSLionel Sambuc S.PP.getSelectorTable(),
1489f4a2713aSLionel Sambuc Member);
1490f4a2713aSLionel Sambuc ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
1491f4a2713aSLionel Sambuc if (!Setter) {
1492f4a2713aSLionel Sambuc // If this reference is in an @implementation, also check for 'private'
1493f4a2713aSLionel Sambuc // methods.
1494f4a2713aSLionel Sambuc Setter = IFace->lookupPrivateMethod(SetterSel, false);
1495f4a2713aSLionel Sambuc }
1496f4a2713aSLionel Sambuc
1497*0a6a1f1dSLionel Sambuc if (Setter && S.DiagnoseUseOfDecl(Setter, MemberLoc))
1498f4a2713aSLionel Sambuc return ExprError();
1499f4a2713aSLionel Sambuc
1500f4a2713aSLionel Sambuc if (Getter || Setter) {
1501*0a6a1f1dSLionel Sambuc return new (S.Context) ObjCPropertyRefExpr(
1502*0a6a1f1dSLionel Sambuc Getter, Setter, S.Context.PseudoObjectTy, VK_LValue,
1503*0a6a1f1dSLionel Sambuc OK_ObjCProperty, MemberLoc, BaseExpr.get());
1504f4a2713aSLionel Sambuc }
1505f4a2713aSLionel Sambuc
1506*0a6a1f1dSLionel Sambuc if (ShouldTryAgainWithRedefinitionType(S, BaseExpr))
1507*0a6a1f1dSLionel Sambuc return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1508f4a2713aSLionel Sambuc ObjCImpDecl, HasTemplateArgs);
1509f4a2713aSLionel Sambuc
1510*0a6a1f1dSLionel Sambuc return ExprError(S.Diag(MemberLoc, diag::err_property_not_found)
1511f4a2713aSLionel Sambuc << MemberName << BaseType);
1512f4a2713aSLionel Sambuc }
1513f4a2713aSLionel Sambuc
1514f4a2713aSLionel Sambuc // Normal property access.
1515*0a6a1f1dSLionel Sambuc return S.HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc, MemberName,
1516*0a6a1f1dSLionel Sambuc MemberLoc, SourceLocation(), QualType(),
1517*0a6a1f1dSLionel Sambuc false);
1518f4a2713aSLionel Sambuc }
1519f4a2713aSLionel Sambuc
1520f4a2713aSLionel Sambuc // Handle 'field access' to vectors, such as 'V.xx'.
1521f4a2713aSLionel Sambuc if (BaseType->isExtVectorType()) {
1522f4a2713aSLionel Sambuc // FIXME: this expr should store IsArrow.
1523f4a2713aSLionel Sambuc IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1524f4a2713aSLionel Sambuc ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind());
1525*0a6a1f1dSLionel Sambuc QualType ret = CheckExtVectorComponent(S, BaseType, VK, OpLoc,
1526f4a2713aSLionel Sambuc Member, MemberLoc);
1527f4a2713aSLionel Sambuc if (ret.isNull())
1528f4a2713aSLionel Sambuc return ExprError();
1529f4a2713aSLionel Sambuc
1530*0a6a1f1dSLionel Sambuc return new (S.Context)
1531*0a6a1f1dSLionel Sambuc ExtVectorElementExpr(ret, VK, BaseExpr.get(), *Member, MemberLoc);
1532f4a2713aSLionel Sambuc }
1533f4a2713aSLionel Sambuc
1534f4a2713aSLionel Sambuc // Adjust builtin-sel to the appropriate redefinition type if that's
1535f4a2713aSLionel Sambuc // not just a pointer to builtin-sel again.
1536*0a6a1f1dSLionel Sambuc if (IsArrow && BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) &&
1537*0a6a1f1dSLionel Sambuc !S.Context.getObjCSelRedefinitionType()->isObjCSelType()) {
1538*0a6a1f1dSLionel Sambuc BaseExpr = S.ImpCastExprToType(
1539*0a6a1f1dSLionel Sambuc BaseExpr.get(), S.Context.getObjCSelRedefinitionType(), CK_BitCast);
1540*0a6a1f1dSLionel Sambuc return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1541f4a2713aSLionel Sambuc ObjCImpDecl, HasTemplateArgs);
1542f4a2713aSLionel Sambuc }
1543f4a2713aSLionel Sambuc
1544f4a2713aSLionel Sambuc // Failure cases.
1545f4a2713aSLionel Sambuc fail:
1546f4a2713aSLionel Sambuc
1547f4a2713aSLionel Sambuc // Recover from dot accesses to pointers, e.g.:
1548f4a2713aSLionel Sambuc // type *foo;
1549f4a2713aSLionel Sambuc // foo.bar
1550f4a2713aSLionel Sambuc // This is actually well-formed in two cases:
1551f4a2713aSLionel Sambuc // - 'type' is an Objective C type
1552f4a2713aSLionel Sambuc // - 'bar' is a pseudo-destructor name which happens to refer to
1553f4a2713aSLionel Sambuc // the appropriate pointer type
1554f4a2713aSLionel Sambuc if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
1555f4a2713aSLionel Sambuc if (!IsArrow && Ptr->getPointeeType()->isRecordType() &&
1556f4a2713aSLionel Sambuc MemberName.getNameKind() != DeclarationName::CXXDestructorName) {
1557*0a6a1f1dSLionel Sambuc S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
1558f4a2713aSLionel Sambuc << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
1559f4a2713aSLionel Sambuc << FixItHint::CreateReplacement(OpLoc, "->");
1560f4a2713aSLionel Sambuc
1561f4a2713aSLionel Sambuc // Recurse as an -> access.
1562f4a2713aSLionel Sambuc IsArrow = true;
1563*0a6a1f1dSLionel Sambuc return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1564f4a2713aSLionel Sambuc ObjCImpDecl, HasTemplateArgs);
1565f4a2713aSLionel Sambuc }
1566f4a2713aSLionel Sambuc }
1567f4a2713aSLionel Sambuc
1568f4a2713aSLionel Sambuc // If the user is trying to apply -> or . to a function name, it's probably
1569f4a2713aSLionel Sambuc // because they forgot parentheses to call that function.
1570*0a6a1f1dSLionel Sambuc if (S.tryToRecoverWithCall(
1571*0a6a1f1dSLionel Sambuc BaseExpr, S.PDiag(diag::err_member_reference_needs_call),
1572f4a2713aSLionel Sambuc /*complain*/ false,
1573f4a2713aSLionel Sambuc IsArrow ? &isPointerToRecordType : &isRecordType)) {
1574f4a2713aSLionel Sambuc if (BaseExpr.isInvalid())
1575f4a2713aSLionel Sambuc return ExprError();
1576*0a6a1f1dSLionel Sambuc BaseExpr = S.DefaultFunctionArrayConversion(BaseExpr.get());
1577*0a6a1f1dSLionel Sambuc return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS,
1578f4a2713aSLionel Sambuc ObjCImpDecl, HasTemplateArgs);
1579f4a2713aSLionel Sambuc }
1580f4a2713aSLionel Sambuc
1581*0a6a1f1dSLionel Sambuc S.Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
1582f4a2713aSLionel Sambuc << BaseType << BaseExpr.get()->getSourceRange() << MemberLoc;
1583f4a2713aSLionel Sambuc
1584f4a2713aSLionel Sambuc return ExprError();
1585f4a2713aSLionel Sambuc }
1586f4a2713aSLionel Sambuc
1587f4a2713aSLionel Sambuc /// The main callback when the parser finds something like
1588f4a2713aSLionel Sambuc /// expression . [nested-name-specifier] identifier
1589f4a2713aSLionel Sambuc /// expression -> [nested-name-specifier] identifier
1590f4a2713aSLionel Sambuc /// where 'identifier' encompasses a fairly broad spectrum of
1591f4a2713aSLionel Sambuc /// possibilities, including destructor and operator references.
1592f4a2713aSLionel Sambuc ///
1593f4a2713aSLionel Sambuc /// \param OpKind either tok::arrow or tok::period
1594f4a2713aSLionel Sambuc /// \param HasTrailingLParen whether the next token is '(', which
1595f4a2713aSLionel Sambuc /// is used to diagnose mis-uses of special members that can
1596f4a2713aSLionel Sambuc /// only be called
1597f4a2713aSLionel Sambuc /// \param ObjCImpDecl the current Objective-C \@implementation
1598f4a2713aSLionel Sambuc /// decl; this is an ugly hack around the fact that Objective-C
1599f4a2713aSLionel Sambuc /// \@implementations aren't properly put in the context chain
ActOnMemberAccessExpr(Scope * S,Expr * Base,SourceLocation OpLoc,tok::TokenKind OpKind,CXXScopeSpec & SS,SourceLocation TemplateKWLoc,UnqualifiedId & Id,Decl * ObjCImpDecl,bool HasTrailingLParen)1600f4a2713aSLionel Sambuc ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base,
1601f4a2713aSLionel Sambuc SourceLocation OpLoc,
1602f4a2713aSLionel Sambuc tok::TokenKind OpKind,
1603f4a2713aSLionel Sambuc CXXScopeSpec &SS,
1604f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
1605f4a2713aSLionel Sambuc UnqualifiedId &Id,
1606f4a2713aSLionel Sambuc Decl *ObjCImpDecl,
1607f4a2713aSLionel Sambuc bool HasTrailingLParen) {
1608f4a2713aSLionel Sambuc if (SS.isSet() && SS.isInvalid())
1609f4a2713aSLionel Sambuc return ExprError();
1610f4a2713aSLionel Sambuc
1611*0a6a1f1dSLionel Sambuc // The only way a reference to a destructor can be used is to
1612*0a6a1f1dSLionel Sambuc // immediately call it. If the next token is not a '(', produce
1613*0a6a1f1dSLionel Sambuc // a diagnostic and build the call now.
1614*0a6a1f1dSLionel Sambuc if (!HasTrailingLParen &&
1615*0a6a1f1dSLionel Sambuc Id.getKind() == UnqualifiedId::IK_DestructorName) {
1616*0a6a1f1dSLionel Sambuc ExprResult DtorAccess =
1617*0a6a1f1dSLionel Sambuc ActOnMemberAccessExpr(S, Base, OpLoc, OpKind, SS, TemplateKWLoc, Id,
1618*0a6a1f1dSLionel Sambuc ObjCImpDecl, /*HasTrailingLParen*/true);
1619*0a6a1f1dSLionel Sambuc if (DtorAccess.isInvalid())
1620*0a6a1f1dSLionel Sambuc return DtorAccess;
1621*0a6a1f1dSLionel Sambuc return DiagnoseDtorReference(Id.getLocStart(), DtorAccess.get());
1622*0a6a1f1dSLionel Sambuc }
1623*0a6a1f1dSLionel Sambuc
1624f4a2713aSLionel Sambuc // Warn about the explicit constructor calls Microsoft extension.
1625f4a2713aSLionel Sambuc if (getLangOpts().MicrosoftExt &&
1626f4a2713aSLionel Sambuc Id.getKind() == UnqualifiedId::IK_ConstructorName)
1627f4a2713aSLionel Sambuc Diag(Id.getSourceRange().getBegin(),
1628f4a2713aSLionel Sambuc diag::ext_ms_explicit_constructor_call);
1629f4a2713aSLionel Sambuc
1630f4a2713aSLionel Sambuc TemplateArgumentListInfo TemplateArgsBuffer;
1631f4a2713aSLionel Sambuc
1632f4a2713aSLionel Sambuc // Decompose the name into its component parts.
1633f4a2713aSLionel Sambuc DeclarationNameInfo NameInfo;
1634f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs;
1635f4a2713aSLionel Sambuc DecomposeUnqualifiedId(Id, TemplateArgsBuffer,
1636f4a2713aSLionel Sambuc NameInfo, TemplateArgs);
1637f4a2713aSLionel Sambuc
1638f4a2713aSLionel Sambuc DeclarationName Name = NameInfo.getName();
1639f4a2713aSLionel Sambuc bool IsArrow = (OpKind == tok::arrow);
1640f4a2713aSLionel Sambuc
1641f4a2713aSLionel Sambuc NamedDecl *FirstQualifierInScope
1642*0a6a1f1dSLionel Sambuc = (!SS.isSet() ? nullptr : FindFirstQualifierInScope(S, SS.getScopeRep()));
1643f4a2713aSLionel Sambuc
1644f4a2713aSLionel Sambuc // This is a postfix expression, so get rid of ParenListExprs.
1645f4a2713aSLionel Sambuc ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
1646f4a2713aSLionel Sambuc if (Result.isInvalid()) return ExprError();
1647*0a6a1f1dSLionel Sambuc Base = Result.get();
1648f4a2713aSLionel Sambuc
1649f4a2713aSLionel Sambuc if (Base->getType()->isDependentType() || Name.isDependentName() ||
1650f4a2713aSLionel Sambuc isDependentScopeSpecifier(SS)) {
1651*0a6a1f1dSLionel Sambuc return ActOnDependentMemberExpr(Base, Base->getType(), IsArrow, OpLoc, SS,
1652*0a6a1f1dSLionel Sambuc TemplateKWLoc, FirstQualifierInScope,
1653f4a2713aSLionel Sambuc NameInfo, TemplateArgs);
1654f4a2713aSLionel Sambuc }
1655f4a2713aSLionel Sambuc
1656*0a6a1f1dSLionel Sambuc ActOnMemberAccessExtraArgs ExtraArgs = {S, Id, ObjCImpDecl,
1657*0a6a1f1dSLionel Sambuc HasTrailingLParen};
1658*0a6a1f1dSLionel Sambuc return BuildMemberReferenceExpr(Base, Base->getType(), OpLoc, IsArrow, SS,
1659*0a6a1f1dSLionel Sambuc TemplateKWLoc, FirstQualifierInScope,
1660*0a6a1f1dSLionel Sambuc NameInfo, TemplateArgs, &ExtraArgs);
1661f4a2713aSLionel Sambuc }
1662f4a2713aSLionel Sambuc
1663f4a2713aSLionel Sambuc static ExprResult
BuildFieldReferenceExpr(Sema & S,Expr * BaseExpr,bool IsArrow,const CXXScopeSpec & SS,FieldDecl * Field,DeclAccessPair FoundDecl,const DeclarationNameInfo & MemberNameInfo)1664f4a2713aSLionel Sambuc BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
1665f4a2713aSLionel Sambuc const CXXScopeSpec &SS, FieldDecl *Field,
1666f4a2713aSLionel Sambuc DeclAccessPair FoundDecl,
1667f4a2713aSLionel Sambuc const DeclarationNameInfo &MemberNameInfo) {
1668f4a2713aSLionel Sambuc // x.a is an l-value if 'a' has a reference type. Otherwise:
1669f4a2713aSLionel Sambuc // x.a is an l-value/x-value/pr-value if the base is (and note
1670f4a2713aSLionel Sambuc // that *x is always an l-value), except that if the base isn't
1671f4a2713aSLionel Sambuc // an ordinary object then we must have an rvalue.
1672f4a2713aSLionel Sambuc ExprValueKind VK = VK_LValue;
1673f4a2713aSLionel Sambuc ExprObjectKind OK = OK_Ordinary;
1674f4a2713aSLionel Sambuc if (!IsArrow) {
1675f4a2713aSLionel Sambuc if (BaseExpr->getObjectKind() == OK_Ordinary)
1676f4a2713aSLionel Sambuc VK = BaseExpr->getValueKind();
1677f4a2713aSLionel Sambuc else
1678f4a2713aSLionel Sambuc VK = VK_RValue;
1679f4a2713aSLionel Sambuc }
1680f4a2713aSLionel Sambuc if (VK != VK_RValue && Field->isBitField())
1681f4a2713aSLionel Sambuc OK = OK_BitField;
1682f4a2713aSLionel Sambuc
1683f4a2713aSLionel Sambuc // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref]
1684f4a2713aSLionel Sambuc QualType MemberType = Field->getType();
1685f4a2713aSLionel Sambuc if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) {
1686f4a2713aSLionel Sambuc MemberType = Ref->getPointeeType();
1687f4a2713aSLionel Sambuc VK = VK_LValue;
1688f4a2713aSLionel Sambuc } else {
1689f4a2713aSLionel Sambuc QualType BaseType = BaseExpr->getType();
1690f4a2713aSLionel Sambuc if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType();
1691f4a2713aSLionel Sambuc
1692f4a2713aSLionel Sambuc Qualifiers BaseQuals = BaseType.getQualifiers();
1693f4a2713aSLionel Sambuc
1694f4a2713aSLionel Sambuc // GC attributes are never picked up by members.
1695f4a2713aSLionel Sambuc BaseQuals.removeObjCGCAttr();
1696f4a2713aSLionel Sambuc
1697f4a2713aSLionel Sambuc // CVR attributes from the base are picked up by members,
1698f4a2713aSLionel Sambuc // except that 'mutable' members don't pick up 'const'.
1699f4a2713aSLionel Sambuc if (Field->isMutable()) BaseQuals.removeConst();
1700f4a2713aSLionel Sambuc
1701f4a2713aSLionel Sambuc Qualifiers MemberQuals
1702f4a2713aSLionel Sambuc = S.Context.getCanonicalType(MemberType).getQualifiers();
1703f4a2713aSLionel Sambuc
1704f4a2713aSLionel Sambuc assert(!MemberQuals.hasAddressSpace());
1705f4a2713aSLionel Sambuc
1706f4a2713aSLionel Sambuc
1707f4a2713aSLionel Sambuc Qualifiers Combined = BaseQuals + MemberQuals;
1708f4a2713aSLionel Sambuc if (Combined != MemberQuals)
1709f4a2713aSLionel Sambuc MemberType = S.Context.getQualifiedType(MemberType, Combined);
1710f4a2713aSLionel Sambuc }
1711f4a2713aSLionel Sambuc
1712f4a2713aSLionel Sambuc S.UnusedPrivateFields.remove(Field);
1713f4a2713aSLionel Sambuc
1714f4a2713aSLionel Sambuc ExprResult Base =
1715f4a2713aSLionel Sambuc S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
1716f4a2713aSLionel Sambuc FoundDecl, Field);
1717f4a2713aSLionel Sambuc if (Base.isInvalid())
1718f4a2713aSLionel Sambuc return ExprError();
1719*0a6a1f1dSLionel Sambuc return BuildMemberExpr(S, S.Context, Base.get(), IsArrow, SS,
1720*0a6a1f1dSLionel Sambuc /*TemplateKWLoc=*/SourceLocation(), Field, FoundDecl,
1721*0a6a1f1dSLionel Sambuc MemberNameInfo, MemberType, VK, OK);
1722f4a2713aSLionel Sambuc }
1723f4a2713aSLionel Sambuc
1724f4a2713aSLionel Sambuc /// Builds an implicit member access expression. The current context
1725f4a2713aSLionel Sambuc /// is known to be an instance method, and the given unqualified lookup
1726f4a2713aSLionel Sambuc /// set is known to contain only instance members, at least one of which
1727f4a2713aSLionel Sambuc /// is from an appropriate type.
1728f4a2713aSLionel Sambuc ExprResult
BuildImplicitMemberExpr(const CXXScopeSpec & SS,SourceLocation TemplateKWLoc,LookupResult & R,const TemplateArgumentListInfo * TemplateArgs,bool IsKnownInstance)1729f4a2713aSLionel Sambuc Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
1730f4a2713aSLionel Sambuc SourceLocation TemplateKWLoc,
1731f4a2713aSLionel Sambuc LookupResult &R,
1732f4a2713aSLionel Sambuc const TemplateArgumentListInfo *TemplateArgs,
1733f4a2713aSLionel Sambuc bool IsKnownInstance) {
1734f4a2713aSLionel Sambuc assert(!R.empty() && !R.isAmbiguous());
1735f4a2713aSLionel Sambuc
1736f4a2713aSLionel Sambuc SourceLocation loc = R.getNameLoc();
1737f4a2713aSLionel Sambuc
1738f4a2713aSLionel Sambuc // If this is known to be an instance access, go ahead and build an
1739f4a2713aSLionel Sambuc // implicit 'this' expression now.
1740f4a2713aSLionel Sambuc // 'this' expression now.
1741f4a2713aSLionel Sambuc QualType ThisTy = getCurrentThisType();
1742f4a2713aSLionel Sambuc assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'");
1743f4a2713aSLionel Sambuc
1744*0a6a1f1dSLionel Sambuc Expr *baseExpr = nullptr; // null signifies implicit access
1745f4a2713aSLionel Sambuc if (IsKnownInstance) {
1746f4a2713aSLionel Sambuc SourceLocation Loc = R.getNameLoc();
1747f4a2713aSLionel Sambuc if (SS.getRange().isValid())
1748f4a2713aSLionel Sambuc Loc = SS.getRange().getBegin();
1749f4a2713aSLionel Sambuc CheckCXXThisCapture(Loc);
1750f4a2713aSLionel Sambuc baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true);
1751f4a2713aSLionel Sambuc }
1752f4a2713aSLionel Sambuc
1753f4a2713aSLionel Sambuc return BuildMemberReferenceExpr(baseExpr, ThisTy,
1754f4a2713aSLionel Sambuc /*OpLoc*/ SourceLocation(),
1755f4a2713aSLionel Sambuc /*IsArrow*/ true,
1756f4a2713aSLionel Sambuc SS, TemplateKWLoc,
1757*0a6a1f1dSLionel Sambuc /*FirstQualifierInScope*/ nullptr,
1758f4a2713aSLionel Sambuc R, TemplateArgs);
1759f4a2713aSLionel Sambuc }
1760