blob: 3442b819892ea2ad525365b1c836e40ed08ac167 [file] [log] [blame]
Douglas Gregor5476205b2011-06-23 00:49:38 +00001//===--- SemaExprMember.cpp - Semantic Analysis for Expressions -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis member access expressions.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Sema/SemaInternal.h"
Faisal Valia17d19f2013-11-07 05:17:06 +000014#include "clang/AST/ASTLambda.h"
Douglas Gregor5476205b2011-06-23 00:49:38 +000015#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
20#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Sema/Lookup.h"
22#include "clang/Sema/Scope.h"
23#include "clang/Sema/ScopeInfo.h"
Douglas Gregor5476205b2011-06-23 00:49:38 +000024
25using namespace clang;
26using namespace sema;
27
Richard Smithd80b2d52012-11-22 00:24:47 +000028typedef llvm::SmallPtrSet<const CXXRecordDecl*, 4> BaseSet;
29static bool BaseIsNotInSet(const CXXRecordDecl *Base, void *BasesPtr) {
30 const BaseSet &Bases = *reinterpret_cast<const BaseSet*>(BasesPtr);
31 return !Bases.count(Base->getCanonicalDecl());
32}
33
Douglas Gregor5476205b2011-06-23 00:49:38 +000034/// Determines if the given class is provably not derived from all of
35/// the prospective base classes.
Richard Smithd80b2d52012-11-22 00:24:47 +000036static bool isProvablyNotDerivedFrom(Sema &SemaRef, CXXRecordDecl *Record,
37 const BaseSet &Bases) {
38 void *BasesPtr = const_cast<void*>(reinterpret_cast<const void*>(&Bases));
39 return BaseIsNotInSet(Record, BasesPtr) &&
40 Record->forallBases(BaseIsNotInSet, BasesPtr);
Douglas Gregor5476205b2011-06-23 00:49:38 +000041}
42
43enum IMAKind {
44 /// The reference is definitely not an instance member access.
45 IMA_Static,
46
47 /// The reference may be an implicit instance member access.
48 IMA_Mixed,
49
Eli Friedman7bda7f72012-01-18 03:53:45 +000050 /// The reference may be to an instance member, but it might be invalid if
Douglas Gregor5476205b2011-06-23 00:49:38 +000051 /// so, because the context is not an instance method.
52 IMA_Mixed_StaticContext,
53
54 /// The reference may be to an instance member, but it is invalid if
55 /// so, because the context is from an unrelated class.
56 IMA_Mixed_Unrelated,
57
58 /// The reference is definitely an implicit instance member access.
59 IMA_Instance,
60
61 /// The reference may be to an unresolved using declaration.
62 IMA_Unresolved,
63
John McCallf413f5e2013-05-03 00:10:13 +000064 /// The reference is a contextually-permitted abstract member reference.
65 IMA_Abstract,
66
Douglas Gregor5476205b2011-06-23 00:49:38 +000067 /// The reference may be to an unresolved using declaration and the
68 /// context is not an instance method.
69 IMA_Unresolved_StaticContext,
70
Eli Friedman456f0182012-01-20 01:26:23 +000071 // The reference refers to a field which is not a member of the containing
72 // class, which is allowed because we're in C++11 mode and the context is
73 // unevaluated.
74 IMA_Field_Uneval_Context,
Eli Friedman7bda7f72012-01-18 03:53:45 +000075
Douglas Gregor5476205b2011-06-23 00:49:38 +000076 /// All possible referrents are instance members and the current
77 /// context is not an instance method.
78 IMA_Error_StaticContext,
79
80 /// All possible referrents are instance members of an unrelated
81 /// class.
82 IMA_Error_Unrelated
83};
84
85/// The given lookup names class member(s) and is not being used for
86/// an address-of-member expression. Classify the type of access
87/// according to whether it's possible that this reference names an
Eli Friedman7bda7f72012-01-18 03:53:45 +000088/// instance member. This is best-effort in dependent contexts; it is okay to
Douglas Gregor5476205b2011-06-23 00:49:38 +000089/// conservatively answer "yes", in which case some errors will simply
90/// not be caught until template-instantiation.
91static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef,
92 Scope *CurScope,
93 const LookupResult &R) {
94 assert(!R.empty() && (*R.begin())->isCXXClassMember());
95
96 DeclContext *DC = SemaRef.getFunctionLevelDeclContext();
97
Douglas Gregor3024f072012-04-16 07:05:22 +000098 bool isStaticContext = SemaRef.CXXThisTypeOverride.isNull() &&
99 (!isa<CXXMethodDecl>(DC) || cast<CXXMethodDecl>(DC)->isStatic());
Douglas Gregor5476205b2011-06-23 00:49:38 +0000100
101 if (R.isUnresolvableResult())
102 return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved;
103
104 // Collect all the declaring classes of instance members we find.
105 bool hasNonInstance = false;
Eli Friedman7bda7f72012-01-18 03:53:45 +0000106 bool isField = false;
Richard Smithd80b2d52012-11-22 00:24:47 +0000107 BaseSet Classes;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000108 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
109 NamedDecl *D = *I;
110
111 if (D->isCXXInstanceMember()) {
John McCall5e77d762013-04-16 07:28:30 +0000112 if (dyn_cast<FieldDecl>(D) || dyn_cast<MSPropertyDecl>(D)
113 || dyn_cast<IndirectFieldDecl>(D))
Eli Friedman7bda7f72012-01-18 03:53:45 +0000114 isField = true;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000115
116 CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext());
117 Classes.insert(R->getCanonicalDecl());
118 }
119 else
120 hasNonInstance = true;
121 }
122
123 // If we didn't find any instance members, it can't be an implicit
124 // member reference.
125 if (Classes.empty())
126 return IMA_Static;
John McCallf413f5e2013-05-03 00:10:13 +0000127
128 // C++11 [expr.prim.general]p12:
129 // An id-expression that denotes a non-static data member or non-static
130 // member function of a class can only be used:
131 // (...)
132 // - if that id-expression denotes a non-static data member and it
133 // appears in an unevaluated operand.
134 //
135 // This rule is specific to C++11. However, we also permit this form
136 // in unevaluated inline assembly operands, like the operand to a SIZE.
137 IMAKind AbstractInstanceResult = IMA_Static; // happens to be 'false'
138 assert(!AbstractInstanceResult);
139 switch (SemaRef.ExprEvalContexts.back().Context) {
140 case Sema::Unevaluated:
141 if (isField && SemaRef.getLangOpts().CPlusPlus11)
142 AbstractInstanceResult = IMA_Field_Uneval_Context;
143 break;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000144
John McCallf413f5e2013-05-03 00:10:13 +0000145 case Sema::UnevaluatedAbstract:
146 AbstractInstanceResult = IMA_Abstract;
147 break;
148
149 case Sema::ConstantEvaluated:
150 case Sema::PotentiallyEvaluated:
151 case Sema::PotentiallyEvaluatedIfUsed:
152 break;
Richard Smitheae99682012-02-25 10:04:07 +0000153 }
154
Douglas Gregor5476205b2011-06-23 00:49:38 +0000155 // If the current context is not an instance method, it can't be
156 // an implicit member reference.
157 if (isStaticContext) {
158 if (hasNonInstance)
Richard Smitheae99682012-02-25 10:04:07 +0000159 return IMA_Mixed_StaticContext;
160
John McCallf413f5e2013-05-03 00:10:13 +0000161 return AbstractInstanceResult ? AbstractInstanceResult
162 : IMA_Error_StaticContext;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000163 }
164
165 CXXRecordDecl *contextClass;
166 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC))
167 contextClass = MD->getParent()->getCanonicalDecl();
168 else
169 contextClass = cast<CXXRecordDecl>(DC);
170
171 // [class.mfct.non-static]p3:
172 // ...is used in the body of a non-static member function of class X,
173 // if name lookup (3.4.1) resolves the name in the id-expression to a
174 // non-static non-type member of some class C [...]
175 // ...if C is not X or a base class of X, the class member access expression
176 // is ill-formed.
177 if (R.getNamingClass() &&
DeLesley Hutchins5b330db2012-02-25 00:11:55 +0000178 contextClass->getCanonicalDecl() !=
Richard Smithd80b2d52012-11-22 00:24:47 +0000179 R.getNamingClass()->getCanonicalDecl()) {
180 // If the naming class is not the current context, this was a qualified
181 // member name lookup, and it's sufficient to check that we have the naming
182 // class as a base class.
183 Classes.clear();
Richard Smithb2c5f962012-11-22 00:40:54 +0000184 Classes.insert(R.getNamingClass()->getCanonicalDecl());
Richard Smithd80b2d52012-11-22 00:24:47 +0000185 }
Douglas Gregor5476205b2011-06-23 00:49:38 +0000186
187 // If we can prove that the current context is unrelated to all the
188 // declaring classes, it can't be an implicit member reference (in
189 // which case it's an error if any of those members are selected).
Richard Smithd80b2d52012-11-22 00:24:47 +0000190 if (isProvablyNotDerivedFrom(SemaRef, contextClass, Classes))
Richard Smith2a986112012-02-25 10:20:59 +0000191 return hasNonInstance ? IMA_Mixed_Unrelated :
John McCallf413f5e2013-05-03 00:10:13 +0000192 AbstractInstanceResult ? AbstractInstanceResult :
193 IMA_Error_Unrelated;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000194
195 return (hasNonInstance ? IMA_Mixed : IMA_Instance);
196}
197
198/// Diagnose a reference to a field with no object available.
Richard Smithfa0a1f52012-04-05 01:13:04 +0000199static void diagnoseInstanceReference(Sema &SemaRef,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000200 const CXXScopeSpec &SS,
Richard Smithfa0a1f52012-04-05 01:13:04 +0000201 NamedDecl *Rep,
Eli Friedman456f0182012-01-20 01:26:23 +0000202 const DeclarationNameInfo &nameInfo) {
Douglas Gregor5476205b2011-06-23 00:49:38 +0000203 SourceLocation Loc = nameInfo.getLoc();
204 SourceRange Range(Loc);
205 if (SS.isSet()) Range.setBegin(SS.getRange().getBegin());
Eli Friedman7bda7f72012-01-18 03:53:45 +0000206
Richard Smithfa0a1f52012-04-05 01:13:04 +0000207 DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext();
208 CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FunctionLevelDC);
Craig Topperc3ec1492014-05-26 06:22:03 +0000209 CXXRecordDecl *ContextClass = Method ? Method->getParent() : nullptr;
Richard Smithfa0a1f52012-04-05 01:13:04 +0000210 CXXRecordDecl *RepClass = dyn_cast<CXXRecordDecl>(Rep->getDeclContext());
211
212 bool InStaticMethod = Method && Method->isStatic();
213 bool IsField = isa<FieldDecl>(Rep) || isa<IndirectFieldDecl>(Rep);
214
215 if (IsField && InStaticMethod)
216 // "invalid use of member 'x' in static member function"
217 SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method)
218 << Range << nameInfo.getName();
219 else if (ContextClass && RepClass && SS.isEmpty() && !InStaticMethod &&
220 !RepClass->Equals(ContextClass) && RepClass->Encloses(ContextClass))
221 // Unqualified lookup in a non-static member function found a member of an
222 // enclosing class.
223 SemaRef.Diag(Loc, diag::err_nested_non_static_member_use)
224 << IsField << RepClass << nameInfo.getName() << ContextClass << Range;
225 else if (IsField)
Eli Friedman456f0182012-01-20 01:26:23 +0000226 SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use)
Richard Smithfa0a1f52012-04-05 01:13:04 +0000227 << nameInfo.getName() << Range;
228 else
229 SemaRef.Diag(Loc, diag::err_member_call_without_object)
230 << Range;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000231}
232
233/// Builds an expression which might be an implicit member expression.
234ExprResult
235Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000236 SourceLocation TemplateKWLoc,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000237 LookupResult &R,
238 const TemplateArgumentListInfo *TemplateArgs) {
239 switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) {
240 case IMA_Instance:
Abramo Bagnara7945c982012-01-27 09:46:47 +0000241 return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000242
243 case IMA_Mixed:
244 case IMA_Mixed_Unrelated:
245 case IMA_Unresolved:
Abramo Bagnara7945c982012-01-27 09:46:47 +0000246 return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, false);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000247
Richard Smith2a986112012-02-25 10:20:59 +0000248 case IMA_Field_Uneval_Context:
249 Diag(R.getNameLoc(), diag::warn_cxx98_compat_non_static_member_use)
250 << R.getLookupNameInfo().getName();
251 // Fall through.
Douglas Gregor5476205b2011-06-23 00:49:38 +0000252 case IMA_Static:
John McCallf413f5e2013-05-03 00:10:13 +0000253 case IMA_Abstract:
Douglas Gregor5476205b2011-06-23 00:49:38 +0000254 case IMA_Mixed_StaticContext:
255 case IMA_Unresolved_StaticContext:
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000256 if (TemplateArgs || TemplateKWLoc.isValid())
257 return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000258 return BuildDeclarationNameExpr(SS, R, false);
259
260 case IMA_Error_StaticContext:
261 case IMA_Error_Unrelated:
Richard Smithfa0a1f52012-04-05 01:13:04 +0000262 diagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(),
Douglas Gregor5476205b2011-06-23 00:49:38 +0000263 R.getLookupNameInfo());
264 return ExprError();
265 }
266
267 llvm_unreachable("unexpected instance member access kind");
Douglas Gregor5476205b2011-06-23 00:49:38 +0000268}
269
270/// Check an ext-vector component access expression.
271///
272/// VK should be set in advance to the value kind of the base
273/// expression.
274static QualType
275CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK,
276 SourceLocation OpLoc, const IdentifierInfo *CompName,
277 SourceLocation CompLoc) {
278 // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements,
279 // see FIXME there.
280 //
281 // FIXME: This logic can be greatly simplified by splitting it along
282 // halving/not halving and reworking the component checking.
283 const ExtVectorType *vecType = baseType->getAs<ExtVectorType>();
284
285 // The vector accessor can't exceed the number of elements.
286 const char *compStr = CompName->getNameStart();
287
288 // This flag determines whether or not the component is one of the four
289 // special names that indicate a subset of exactly half the elements are
290 // to be selected.
291 bool HalvingSwizzle = false;
292
293 // This flag determines whether or not CompName has an 's' char prefix,
294 // indicating that it is a string of hex values to be used as vector indices.
Fariborz Jahanian275542a2014-04-03 19:43:01 +0000295 bool HexSwizzle = (*compStr == 's' || *compStr == 'S') && compStr[1];
Douglas Gregor5476205b2011-06-23 00:49:38 +0000296
297 bool HasRepeated = false;
298 bool HasIndex[16] = {};
299
300 int Idx;
301
302 // Check that we've found one of the special components, or that the component
303 // names must come from the same set.
304 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
305 !strcmp(compStr, "even") || !strcmp(compStr, "odd")) {
306 HalvingSwizzle = true;
307 } else if (!HexSwizzle &&
308 (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) {
309 do {
310 if (HasIndex[Idx]) HasRepeated = true;
311 HasIndex[Idx] = true;
312 compStr++;
313 } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1);
314 } else {
315 if (HexSwizzle) compStr++;
316 while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) {
317 if (HasIndex[Idx]) HasRepeated = true;
318 HasIndex[Idx] = true;
319 compStr++;
320 }
321 }
322
323 if (!HalvingSwizzle && *compStr) {
324 // We didn't get to the end of the string. This means the component names
325 // didn't come from the same set *or* we encountered an illegal name.
326 S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000327 << StringRef(compStr, 1) << SourceRange(CompLoc);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000328 return QualType();
329 }
330
331 // Ensure no component accessor exceeds the width of the vector type it
332 // operates on.
333 if (!HalvingSwizzle) {
334 compStr = CompName->getNameStart();
335
336 if (HexSwizzle)
337 compStr++;
338
339 while (*compStr) {
340 if (!vecType->isAccessorWithinNumElements(*compStr++)) {
341 S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length)
342 << baseType << SourceRange(CompLoc);
343 return QualType();
344 }
345 }
346 }
347
348 // The component accessor looks fine - now we need to compute the actual type.
349 // The vector type is implied by the component accessor. For example,
350 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
351 // vec4.s0 is a float, vec4.s23 is a vec3, etc.
352 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
353 unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2
354 : CompName->getLength();
355 if (HexSwizzle)
356 CompSize--;
357
358 if (CompSize == 1)
359 return vecType->getElementType();
360
361 if (HasRepeated) VK = VK_RValue;
362
363 QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize);
364 // Now look up the TypeDefDecl from the vector type. Without this,
365 // diagostics look bad. We want extended vector types to appear built-in.
Douglas Gregorb7098a32011-07-28 00:39:29 +0000366 for (Sema::ExtVectorDeclsType::iterator
Axel Naumanndd433f02012-10-18 19:05:02 +0000367 I = S.ExtVectorDecls.begin(S.getExternalSource()),
Douglas Gregorb7098a32011-07-28 00:39:29 +0000368 E = S.ExtVectorDecls.end();
369 I != E; ++I) {
370 if ((*I)->getUnderlyingType() == VT)
371 return S.Context.getTypedefType(*I);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000372 }
Douglas Gregorb7098a32011-07-28 00:39:29 +0000373
Douglas Gregor5476205b2011-06-23 00:49:38 +0000374 return VT; // should never get here (a typedef type should always be found).
375}
376
377static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl,
378 IdentifierInfo *Member,
379 const Selector &Sel,
380 ASTContext &Context) {
381 if (Member)
382 if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member))
383 return PD;
384 if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel))
385 return OMD;
386
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000387 for (const auto *I : PDecl->protocols()) {
388 if (Decl *D = FindGetterSetterNameDeclFromProtocolList(I, Member, Sel,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000389 Context))
390 return D;
391 }
Craig Topperc3ec1492014-05-26 06:22:03 +0000392 return nullptr;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000393}
394
395static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy,
396 IdentifierInfo *Member,
397 const Selector &Sel,
398 ASTContext &Context) {
399 // Check protocols on qualified interfaces.
Craig Topperc3ec1492014-05-26 06:22:03 +0000400 Decl *GDecl = nullptr;
Aaron Ballman83731462014-03-17 16:14:00 +0000401 for (const auto *I : QIdTy->quals()) {
Douglas Gregor5476205b2011-06-23 00:49:38 +0000402 if (Member)
Aaron Ballman83731462014-03-17 16:14:00 +0000403 if (ObjCPropertyDecl *PD = I->FindPropertyDeclaration(Member)) {
Douglas Gregor5476205b2011-06-23 00:49:38 +0000404 GDecl = PD;
405 break;
406 }
407 // Also must look for a getter or setter name which uses property syntax.
Aaron Ballman83731462014-03-17 16:14:00 +0000408 if (ObjCMethodDecl *OMD = I->getInstanceMethod(Sel)) {
Douglas Gregor5476205b2011-06-23 00:49:38 +0000409 GDecl = OMD;
410 break;
411 }
412 }
413 if (!GDecl) {
Aaron Ballman83731462014-03-17 16:14:00 +0000414 for (const auto *I : QIdTy->quals()) {
Douglas Gregor5476205b2011-06-23 00:49:38 +0000415 // Search in the protocol-qualifier list of current protocol.
Aaron Ballman83731462014-03-17 16:14:00 +0000416 GDecl = FindGetterSetterNameDeclFromProtocolList(I, Member, Sel, Context);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000417 if (GDecl)
418 return GDecl;
419 }
420 }
421 return GDecl;
422}
423
424ExprResult
425Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType,
426 bool IsArrow, SourceLocation OpLoc,
427 const CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000428 SourceLocation TemplateKWLoc,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000429 NamedDecl *FirstQualifierInScope,
430 const DeclarationNameInfo &NameInfo,
431 const TemplateArgumentListInfo *TemplateArgs) {
432 // Even in dependent contexts, try to diagnose base expressions with
433 // obviously wrong types, e.g.:
434 //
435 // T* t;
436 // t.f;
437 //
438 // In Obj-C++, however, the above expression is valid, since it could be
439 // accessing the 'f' property if T is an Obj-C interface. The extra check
440 // allows this, while still reporting an error if T is a struct pointer.
441 if (!IsArrow) {
442 const PointerType *PT = BaseType->getAs<PointerType>();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000443 if (PT && (!getLangOpts().ObjC1 ||
Douglas Gregor5476205b2011-06-23 00:49:38 +0000444 PT->getPointeeType()->isRecordType())) {
445 assert(BaseExpr && "cannot happen with implicit member accesses");
Matt Beaumont-Gayd9f244af2012-04-21 01:12:48 +0000446 Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
Matt Beaumont-Gay025321b2012-04-21 02:13:04 +0000447 << BaseType << BaseExpr->getSourceRange() << NameInfo.getSourceRange();
Douglas Gregor5476205b2011-06-23 00:49:38 +0000448 return ExprError();
449 }
450 }
451
452 assert(BaseType->isDependentType() ||
453 NameInfo.getName().isDependentName() ||
454 isDependentScopeSpecifier(SS));
455
456 // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr
457 // must have pointer type, and the accessed type is the pointee.
458 return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType,
459 IsArrow, OpLoc,
460 SS.getWithLocInContext(Context),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000461 TemplateKWLoc,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000462 FirstQualifierInScope,
463 NameInfo, TemplateArgs));
464}
465
466/// We know that the given qualified member reference points only to
467/// declarations which do not belong to the static type of the base
468/// expression. Diagnose the problem.
469static void DiagnoseQualifiedMemberReference(Sema &SemaRef,
470 Expr *BaseExpr,
471 QualType BaseType,
472 const CXXScopeSpec &SS,
473 NamedDecl *rep,
474 const DeclarationNameInfo &nameInfo) {
475 // If this is an implicit member access, use a different set of
476 // diagnostics.
477 if (!BaseExpr)
Richard Smithfa0a1f52012-04-05 01:13:04 +0000478 return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000479
480 SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated)
481 << SS.getRange() << rep << BaseType;
482}
483
484// Check whether the declarations we found through a nested-name
485// specifier in a member expression are actually members of the base
486// type. The restriction here is:
487//
488// C++ [expr.ref]p2:
489// ... In these cases, the id-expression shall name a
490// member of the class or of one of its base classes.
491//
492// So it's perfectly legitimate for the nested-name specifier to name
493// an unrelated class, and for us to find an overload set including
494// decls from classes which are not superclasses, as long as the decl
495// we actually pick through overload resolution is from a superclass.
496bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr,
497 QualType BaseType,
498 const CXXScopeSpec &SS,
499 const LookupResult &R) {
Richard Smithd80b2d52012-11-22 00:24:47 +0000500 CXXRecordDecl *BaseRecord =
501 cast_or_null<CXXRecordDecl>(computeDeclContext(BaseType));
502 if (!BaseRecord) {
Douglas Gregor5476205b2011-06-23 00:49:38 +0000503 // We can't check this yet because the base type is still
504 // dependent.
505 assert(BaseType->isDependentType());
506 return false;
507 }
Douglas Gregor5476205b2011-06-23 00:49:38 +0000508
509 for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) {
510 // If this is an implicit member reference and we find a
511 // non-instance member, it's not an error.
512 if (!BaseExpr && !(*I)->isCXXInstanceMember())
513 return false;
514
515 // Note that we use the DC of the decl, not the underlying decl.
516 DeclContext *DC = (*I)->getDeclContext();
517 while (DC->isTransparentContext())
518 DC = DC->getParent();
519
520 if (!DC->isRecord())
521 continue;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000522
Richard Smithd80b2d52012-11-22 00:24:47 +0000523 CXXRecordDecl *MemberRecord = cast<CXXRecordDecl>(DC)->getCanonicalDecl();
524 if (BaseRecord->getCanonicalDecl() == MemberRecord ||
525 !BaseRecord->isProvablyNotDerivedFrom(MemberRecord))
Douglas Gregor5476205b2011-06-23 00:49:38 +0000526 return false;
527 }
528
529 DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS,
530 R.getRepresentativeDecl(),
531 R.getLookupNameInfo());
532 return true;
533}
534
Kaelyn Uhrain3658e6a2012-01-13 21:28:55 +0000535namespace {
536
537// Callback to only accept typo corrections that are either a ValueDecl or a
Kaelyn Uhrain8aa8da82013-10-19 00:05:00 +0000538// FunctionTemplateDecl and are declared in the current record or, for a C++
539// classes, one of its base classes.
Kaelyn Uhrain3658e6a2012-01-13 21:28:55 +0000540class RecordMemberExprValidatorCCC : public CorrectionCandidateCallback {
541 public:
Kaelyn Uhrain8aa8da82013-10-19 00:05:00 +0000542 explicit RecordMemberExprValidatorCCC(const RecordType *RTy)
543 : Record(RTy->getDecl()) {}
544
Craig Toppere14c0f82014-03-12 04:55:44 +0000545 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhrain3658e6a2012-01-13 21:28:55 +0000546 NamedDecl *ND = candidate.getCorrectionDecl();
Kaelyn Uhrain8aa8da82013-10-19 00:05:00 +0000547 // Don't accept candidates that cannot be member functions, constants,
548 // variables, or templates.
549 if (!ND || !(isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)))
550 return false;
551
552 // Accept candidates that occur in the current record.
553 if (Record->containsDecl(ND))
554 return true;
555
556 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Record)) {
557 // Accept candidates that occur in any of the current class' base classes.
Aaron Ballman574705e2014-03-13 15:41:46 +0000558 for (const auto &BS : RD->bases()) {
Kaelyn Uhrain8aa8da82013-10-19 00:05:00 +0000559 if (const RecordType *BSTy = dyn_cast_or_null<RecordType>(
Aaron Ballman574705e2014-03-13 15:41:46 +0000560 BS.getType().getTypePtrOrNull())) {
Kaelyn Uhrain8aa8da82013-10-19 00:05:00 +0000561 if (BSTy->getDecl()->containsDecl(ND))
562 return true;
563 }
564 }
565 }
566
567 return false;
Kaelyn Uhrain3658e6a2012-01-13 21:28:55 +0000568 }
Kaelyn Uhrain8aa8da82013-10-19 00:05:00 +0000569
570 private:
571 const RecordDecl *const Record;
Kaelyn Uhrain3658e6a2012-01-13 21:28:55 +0000572};
573
574}
575
Douglas Gregor5476205b2011-06-23 00:49:38 +0000576static bool
Douglas Gregor3024f072012-04-16 07:05:22 +0000577LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000578 SourceRange BaseRange, const RecordType *RTy,
579 SourceLocation OpLoc, CXXScopeSpec &SS,
580 bool HasTemplateArgs) {
581 RecordDecl *RDecl = RTy->getDecl();
Douglas Gregor3024f072012-04-16 07:05:22 +0000582 if (!SemaRef.isThisOutsideMemberFunctionBody(QualType(RTy, 0)) &&
583 SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0),
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000584 diag::err_typecheck_incomplete_tag,
585 BaseRange))
Douglas Gregor5476205b2011-06-23 00:49:38 +0000586 return true;
587
588 if (HasTemplateArgs) {
589 // LookupTemplateName doesn't expect these both to exist simultaneously.
590 QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0);
591
592 bool MOUS;
Craig Topperc3ec1492014-05-26 06:22:03 +0000593 SemaRef.LookupTemplateName(R, nullptr, SS, ObjectType, false, MOUS);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000594 return false;
595 }
596
597 DeclContext *DC = RDecl;
598 if (SS.isSet()) {
599 // If the member name was a qualified-id, look into the
600 // nested-name-specifier.
601 DC = SemaRef.computeDeclContext(SS, false);
602
603 if (SemaRef.RequireCompleteDeclContext(SS, DC)) {
604 SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag)
605 << SS.getRange() << DC;
606 return true;
607 }
608
609 assert(DC && "Cannot handle non-computable dependent contexts in lookup");
610
611 if (!isa<TypeDecl>(DC)) {
612 SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass)
613 << DC << SS.getRange();
614 return true;
615 }
616 }
617
618 // The record definition is complete, now look up the member.
619 SemaRef.LookupQualifiedName(R, DC);
620
621 if (!R.empty())
622 return false;
623
624 // We didn't find anything with the given name, so try to correct
625 // for typos.
626 DeclarationName Name = R.getLookupName();
Kaelyn Uhrain8aa8da82013-10-19 00:05:00 +0000627 RecordMemberExprValidatorCCC Validator(RTy);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000628 TypoCorrection Corrected = SemaRef.CorrectTypo(R.getLookupNameInfo(),
Craig Topperc3ec1492014-05-26 06:22:03 +0000629 R.getLookupKind(), nullptr,
John Thompson2255f2c2014-04-23 12:57:01 +0000630 &SS, Validator,
631 Sema::CTK_ErrorRecovery, DC);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000632 R.clear();
Nick Lewyckyd1b0df42013-05-07 22:14:37 +0000633 if (Corrected.isResolved() && !Corrected.isKeyword()) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000634 R.setLookupName(Corrected.getCorrection());
Nick Lewyckyd1b0df42013-05-07 22:14:37 +0000635 for (TypoCorrection::decl_iterator DI = Corrected.begin(),
636 DIEnd = Corrected.end();
637 DI != DIEnd; ++DI) {
638 R.addDecl(*DI);
639 }
640 R.resolveKind();
641
Nick Lewyckyd1b0df42013-05-07 22:14:37 +0000642 // If we're typo-correcting to an overloaded name, we don't yet have enough
643 // information to do overload resolution, so we don't know which previous
644 // declaration to point to.
Richard Smithf9b15102013-08-17 00:46:16 +0000645 if (Corrected.isOverloaded())
Craig Topperc3ec1492014-05-26 06:22:03 +0000646 Corrected.setCorrectionDecl(nullptr);
Richard Smithf9b15102013-08-17 00:46:16 +0000647 bool DroppedSpecifier =
648 Corrected.WillReplaceSpecifier() &&
649 Name.getAsString() == Corrected.getAsString(SemaRef.getLangOpts());
650 SemaRef.diagnoseTypo(Corrected,
651 SemaRef.PDiag(diag::err_no_member_suggest)
652 << Name << DC << DroppedSpecifier << SS.getRange());
Douglas Gregor5476205b2011-06-23 00:49:38 +0000653 }
654
655 return false;
656}
657
658ExprResult
659Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType,
660 SourceLocation OpLoc, bool IsArrow,
661 CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000662 SourceLocation TemplateKWLoc,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000663 NamedDecl *FirstQualifierInScope,
664 const DeclarationNameInfo &NameInfo,
665 const TemplateArgumentListInfo *TemplateArgs) {
666 if (BaseType->isDependentType() ||
667 (SS.isSet() && isDependentScopeSpecifier(SS)))
668 return ActOnDependentMemberExpr(Base, BaseType,
669 IsArrow, OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000670 SS, TemplateKWLoc, FirstQualifierInScope,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000671 NameInfo, TemplateArgs);
672
673 LookupResult R(*this, NameInfo, LookupMemberName);
674
675 // Implicit member accesses.
676 if (!Base) {
677 QualType RecordTy = BaseType;
678 if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType();
679 if (LookupMemberExprInRecord(*this, R, SourceRange(),
680 RecordTy->getAs<RecordType>(),
Craig Topperc3ec1492014-05-26 06:22:03 +0000681 OpLoc, SS, TemplateArgs != nullptr))
Douglas Gregor5476205b2011-06-23 00:49:38 +0000682 return ExprError();
683
684 // Explicit member accesses.
685 } else {
686 ExprResult BaseResult = Owned(Base);
687 ExprResult Result =
688 LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
Craig Topperc3ec1492014-05-26 06:22:03 +0000689 SS, /*ObjCImpDecl*/ nullptr, TemplateArgs != nullptr);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000690
691 if (BaseResult.isInvalid())
692 return ExprError();
693 Base = BaseResult.take();
694
695 if (Result.isInvalid()) {
696 Owned(Base);
697 return ExprError();
698 }
699
700 if (Result.get())
Benjamin Kramer62b95d82012-08-23 21:35:17 +0000701 return Result;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000702
703 // LookupMemberExpr can modify Base, and thus change BaseType
704 BaseType = Base->getType();
705 }
706
707 return BuildMemberReferenceExpr(Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000708 OpLoc, IsArrow, SS, TemplateKWLoc,
709 FirstQualifierInScope, R, TemplateArgs);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000710}
711
712static ExprResult
713BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
714 const CXXScopeSpec &SS, FieldDecl *Field,
715 DeclAccessPair FoundDecl,
716 const DeclarationNameInfo &MemberNameInfo);
717
718ExprResult
719Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS,
720 SourceLocation loc,
721 IndirectFieldDecl *indirectField,
Eli Friedmancccd0642013-07-16 00:01:31 +0000722 DeclAccessPair foundDecl,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000723 Expr *baseObjectExpr,
724 SourceLocation opLoc) {
725 // First, build the expression that refers to the base object.
726
727 bool baseObjectIsPointer = false;
728 Qualifiers baseQuals;
729
730 // Case 1: the base of the indirect field is not a field.
731 VarDecl *baseVariable = indirectField->getVarDecl();
732 CXXScopeSpec EmptySS;
733 if (baseVariable) {
734 assert(baseVariable->getType()->isRecordType());
735
736 // In principle we could have a member access expression that
737 // accesses an anonymous struct/union that's a static member of
738 // the base object's class. However, under the current standard,
739 // static data members cannot be anonymous structs or unions.
740 // Supporting this is as easy as building a MemberExpr here.
741 assert(!baseObjectExpr && "anonymous struct/union is static data member?");
742
743 DeclarationNameInfo baseNameInfo(DeclarationName(), loc);
744
745 ExprResult result
746 = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable);
747 if (result.isInvalid()) return ExprError();
748
749 baseObjectExpr = result.take();
750 baseObjectIsPointer = false;
751 baseQuals = baseObjectExpr->getType().getQualifiers();
752
753 // Case 2: the base of the indirect field is a field and the user
754 // wrote a member expression.
755 } else if (baseObjectExpr) {
756 // The caller provided the base object expression. Determine
757 // whether its a pointer and whether it adds any qualifiers to the
758 // anonymous struct/union fields we're looking into.
759 QualType objectType = baseObjectExpr->getType();
760
761 if (const PointerType *ptr = objectType->getAs<PointerType>()) {
762 baseObjectIsPointer = true;
763 objectType = ptr->getPointeeType();
764 } else {
765 baseObjectIsPointer = false;
766 }
767 baseQuals = objectType.getQualifiers();
768
769 // Case 3: the base of the indirect field is a field and we should
770 // build an implicit member access.
771 } else {
772 // We've found a member of an anonymous struct/union that is
773 // inside a non-anonymous struct/union, so in a well-formed
774 // program our base object expression is "this".
Douglas Gregor09deffa2011-10-18 16:47:30 +0000775 QualType ThisTy = getCurrentThisType();
Douglas Gregor5476205b2011-06-23 00:49:38 +0000776 if (ThisTy.isNull()) {
777 Diag(loc, diag::err_invalid_member_use_in_static_method)
778 << indirectField->getDeclName();
779 return ExprError();
780 }
781
782 // Our base object expression is "this".
Eli Friedman73a04092012-01-07 04:59:52 +0000783 CheckCXXThisCapture(loc);
Douglas Gregor5476205b2011-06-23 00:49:38 +0000784 baseObjectExpr
785 = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true);
786 baseObjectIsPointer = true;
787 baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers();
788 }
789
790 // Build the implicit member references to the field of the
791 // anonymous struct/union.
792 Expr *result = baseObjectExpr;
793 IndirectFieldDecl::chain_iterator
794 FI = indirectField->chain_begin(), FEnd = indirectField->chain_end();
795
796 // Build the first member access in the chain with full information.
797 if (!baseVariable) {
798 FieldDecl *field = cast<FieldDecl>(*FI);
799
Douglas Gregor5476205b2011-06-23 00:49:38 +0000800 // Make a nameInfo that properly uses the anonymous name.
801 DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
802
803 result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer,
804 EmptySS, field, foundDecl,
805 memberNameInfo).take();
Eli Friedmancccd0642013-07-16 00:01:31 +0000806 if (!result)
807 return ExprError();
808
Douglas Gregor5476205b2011-06-23 00:49:38 +0000809 // FIXME: check qualified member access
810 }
811
812 // In all cases, we should now skip the first declaration in the chain.
813 ++FI;
814
815 while (FI != FEnd) {
816 FieldDecl *field = cast<FieldDecl>(*FI++);
Eli Friedmancccd0642013-07-16 00:01:31 +0000817
Douglas Gregor5476205b2011-06-23 00:49:38 +0000818 // FIXME: these are somewhat meaningless
819 DeclarationNameInfo memberNameInfo(field->getDeclName(), loc);
Eli Friedmancccd0642013-07-16 00:01:31 +0000820 DeclAccessPair fakeFoundDecl =
821 DeclAccessPair::make(field, field->getAccess());
822
Douglas Gregor5476205b2011-06-23 00:49:38 +0000823 result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false,
Eli Friedmancccd0642013-07-16 00:01:31 +0000824 (FI == FEnd? SS : EmptySS), field,
825 fakeFoundDecl, memberNameInfo).take();
Douglas Gregor5476205b2011-06-23 00:49:38 +0000826 }
827
828 return Owned(result);
829}
830
John McCall5e77d762013-04-16 07:28:30 +0000831static ExprResult
832BuildMSPropertyRefExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
833 const CXXScopeSpec &SS,
834 MSPropertyDecl *PD,
835 const DeclarationNameInfo &NameInfo) {
836 // Property names are always simple identifiers and therefore never
837 // require any interesting additional storage.
838 return new (S.Context) MSPropertyRefExpr(BaseExpr, PD, IsArrow,
839 S.Context.PseudoObjectTy, VK_LValue,
840 SS.getWithLocInContext(S.Context),
841 NameInfo.getLoc());
842}
843
Douglas Gregor5476205b2011-06-23 00:49:38 +0000844/// \brief Build a MemberExpr AST node.
Craig Topperc3ec1492014-05-26 06:22:03 +0000845static MemberExpr *
846BuildMemberExpr(Sema &SemaRef, ASTContext &C, Expr *Base, bool isArrow,
847 const CXXScopeSpec &SS, SourceLocation TemplateKWLoc,
848 ValueDecl *Member, DeclAccessPair FoundDecl,
849 const DeclarationNameInfo &MemberNameInfo, QualType Ty,
850 ExprValueKind VK, ExprObjectKind OK,
851 const TemplateArgumentListInfo *TemplateArgs = nullptr) {
Richard Smith08b12f12011-10-27 22:11:44 +0000852 assert((!isArrow || Base->isRValue()) && "-> base must be a pointer rvalue");
Eli Friedmanfa0df832012-02-02 03:46:19 +0000853 MemberExpr *E =
854 MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C),
855 TemplateKWLoc, Member, FoundDecl, MemberNameInfo,
856 TemplateArgs, Ty, VK, OK);
857 SemaRef.MarkMemberReferenced(E);
858 return E;
Douglas Gregor5476205b2011-06-23 00:49:38 +0000859}
860
861ExprResult
862Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
863 SourceLocation OpLoc, bool IsArrow,
864 const CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000865 SourceLocation TemplateKWLoc,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000866 NamedDecl *FirstQualifierInScope,
867 LookupResult &R,
Kaelyn Uhrain76e07342012-04-25 19:49:54 +0000868 const TemplateArgumentListInfo *TemplateArgs,
869 bool SuppressQualifierCheck,
870 ActOnMemberAccessExtraArgs *ExtraArgs) {
Douglas Gregor5476205b2011-06-23 00:49:38 +0000871 QualType BaseType = BaseExprType;
872 if (IsArrow) {
873 assert(BaseType->isPointerType());
John McCall526ab472011-10-25 17:37:35 +0000874 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
Douglas Gregor5476205b2011-06-23 00:49:38 +0000875 }
876 R.setBaseObjectType(BaseType);
Faisal Valia17d19f2013-11-07 05:17:06 +0000877
878 LambdaScopeInfo *const CurLSI = getCurLambda();
879 // If this is an implicit member reference and the overloaded
880 // name refers to both static and non-static member functions
881 // (i.e. BaseExpr is null) and if we are currently processing a lambda,
882 // check if we should/can capture 'this'...
883 // Keep this example in mind:
884 // struct X {
885 // void f(int) { }
886 // static void f(double) { }
887 //
888 // int g() {
889 // auto L = [=](auto a) {
890 // return [](int i) {
891 // return [=](auto b) {
892 // f(b);
893 // //f(decltype(a){});
894 // };
895 // };
896 // };
897 // auto M = L(0.0);
898 // auto N = M(3);
899 // N(5.32); // OK, must not error.
900 // return 0;
901 // }
902 // };
903 //
904 if (!BaseExpr && CurLSI) {
905 SourceLocation Loc = R.getNameLoc();
906 if (SS.getRange().isValid())
907 Loc = SS.getRange().getBegin();
908 DeclContext *EnclosingFunctionCtx = CurContext->getParent()->getParent();
909 // If the enclosing function is not dependent, then this lambda is
910 // capture ready, so if we can capture this, do so.
911 if (!EnclosingFunctionCtx->isDependentContext()) {
912 // If the current lambda and all enclosing lambdas can capture 'this' -
913 // then go ahead and capture 'this' (since our unresolved overload set
914 // contains both static and non-static member functions).
915 if (!CheckCXXThisCapture(Loc, /*Explcit*/false, /*Diagnose*/false))
916 CheckCXXThisCapture(Loc);
917 } else if (CurContext->isDependentContext()) {
918 // ... since this is an implicit member reference, that might potentially
919 // involve a 'this' capture, mark 'this' for potential capture in
920 // enclosing lambdas.
921 if (CurLSI->ImpCaptureStyle != CurLSI->ImpCap_None)
922 CurLSI->addPotentialThisCapture(Loc);
923 }
924 }
Douglas Gregor5476205b2011-06-23 00:49:38 +0000925 const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo();
926 DeclarationName MemberName = MemberNameInfo.getName();
927 SourceLocation MemberLoc = MemberNameInfo.getLoc();
928
929 if (R.isAmbiguous())
930 return ExprError();
931
932 if (R.empty()) {
933 // Rederive where we looked up.
934 DeclContext *DC = (SS.isSet()
935 ? computeDeclContext(SS, false)
936 : BaseType->getAs<RecordType>()->getDecl());
937
Kaelyn Uhrain76e07342012-04-25 19:49:54 +0000938 if (ExtraArgs) {
939 ExprResult RetryExpr;
940 if (!IsArrow && BaseExpr) {
Kaelyn Uhraind4ea98a2012-05-01 01:17:53 +0000941 SFINAETrap Trap(*this, true);
Kaelyn Uhrain76e07342012-04-25 19:49:54 +0000942 ParsedType ObjectType;
943 bool MayBePseudoDestructor = false;
944 RetryExpr = ActOnStartCXXMemberReference(getCurScope(), BaseExpr,
945 OpLoc, tok::arrow, ObjectType,
946 MayBePseudoDestructor);
947 if (RetryExpr.isUsable() && !Trap.hasErrorOccurred()) {
948 CXXScopeSpec TempSS(SS);
949 RetryExpr = ActOnMemberAccessExpr(
950 ExtraArgs->S, RetryExpr.get(), OpLoc, tok::arrow, TempSS,
951 TemplateKWLoc, ExtraArgs->Id, ExtraArgs->ObjCImpDecl,
952 ExtraArgs->HasTrailingLParen);
953 }
954 if (Trap.hasErrorOccurred())
955 RetryExpr = ExprError();
956 }
957 if (RetryExpr.isUsable()) {
958 Diag(OpLoc, diag::err_no_member_overloaded_arrow)
959 << MemberName << DC << FixItHint::CreateReplacement(OpLoc, "->");
960 return RetryExpr;
961 }
962 }
963
Douglas Gregor5476205b2011-06-23 00:49:38 +0000964 Diag(R.getNameLoc(), diag::err_no_member)
965 << MemberName << DC
966 << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange());
967 return ExprError();
968 }
969
970 // Diagnose lookups that find only declarations from a non-base
971 // type. This is possible for either qualified lookups (which may
972 // have been qualified with an unrelated type) or implicit member
973 // expressions (which were found with unqualified lookup and thus
974 // may have come from an enclosing scope). Note that it's okay for
975 // lookup to find declarations from a non-base type as long as those
976 // aren't the ones picked by overload resolution.
977 if ((SS.isSet() || !BaseExpr ||
978 (isa<CXXThisExpr>(BaseExpr) &&
979 cast<CXXThisExpr>(BaseExpr)->isImplicit())) &&
980 !SuppressQualifierCheck &&
981 CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R))
982 return ExprError();
Fariborz Jahanian502d2ee2011-10-17 21:00:22 +0000983
Douglas Gregor5476205b2011-06-23 00:49:38 +0000984 // Construct an unresolved result if we in fact got an unresolved
985 // result.
986 if (R.isOverloadedResult() || R.isUnresolvableResult()) {
987 // Suppress any lookup-related diagnostics; we'll do these when we
988 // pick a member.
989 R.suppressDiagnostics();
990
991 UnresolvedMemberExpr *MemExpr
992 = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(),
993 BaseExpr, BaseExprType,
994 IsArrow, OpLoc,
995 SS.getWithLocInContext(Context),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000996 TemplateKWLoc, MemberNameInfo,
Douglas Gregor5476205b2011-06-23 00:49:38 +0000997 TemplateArgs, R.begin(), R.end());
998
999 return Owned(MemExpr);
1000 }
1001
1002 assert(R.isSingleResult());
1003 DeclAccessPair FoundDecl = R.begin().getPair();
1004 NamedDecl *MemberDecl = R.getFoundDecl();
1005
1006 // FIXME: diagnose the presence of template arguments now.
1007
1008 // If the decl being referenced had an error, return an error for this
1009 // sub-expr without emitting another error, in order to avoid cascading
1010 // error cases.
1011 if (MemberDecl->isInvalidDecl())
1012 return ExprError();
1013
1014 // Handle the implicit-member-access case.
1015 if (!BaseExpr) {
1016 // If this is not an instance member, convert to a non-member access.
1017 if (!MemberDecl->isCXXInstanceMember())
1018 return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl);
1019
1020 SourceLocation Loc = R.getNameLoc();
1021 if (SS.getRange().isValid())
1022 Loc = SS.getRange().getBegin();
Eli Friedman73a04092012-01-07 04:59:52 +00001023 CheckCXXThisCapture(Loc);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001024 BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true);
1025 }
1026
1027 bool ShouldCheckUse = true;
1028 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) {
1029 // Don't diagnose the use of a virtual member function unless it's
1030 // explicitly qualified.
1031 if (MD->isVirtual() && !SS.isSet())
1032 ShouldCheckUse = false;
1033 }
1034
1035 // Check the use of this member.
1036 if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) {
1037 Owned(BaseExpr);
1038 return ExprError();
1039 }
1040
Douglas Gregor5476205b2011-06-23 00:49:38 +00001041 if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl))
1042 return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow,
1043 SS, FD, FoundDecl, MemberNameInfo);
1044
John McCall5e77d762013-04-16 07:28:30 +00001045 if (MSPropertyDecl *PD = dyn_cast<MSPropertyDecl>(MemberDecl))
1046 return BuildMSPropertyRefExpr(*this, BaseExpr, IsArrow, SS, PD,
1047 MemberNameInfo);
1048
Douglas Gregor5476205b2011-06-23 00:49:38 +00001049 if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl))
1050 // We may have found a field within an anonymous union or struct
1051 // (C++ [class.union]).
1052 return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD,
Eli Friedmancccd0642013-07-16 00:01:31 +00001053 FoundDecl, BaseExpr,
1054 OpLoc);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001055
1056 if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) {
Eli Friedmanfa0df832012-02-02 03:46:19 +00001057 return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
1058 TemplateKWLoc, Var, FoundDecl, MemberNameInfo,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001059 Var->getType().getNonReferenceType(),
1060 VK_LValue, OK_Ordinary));
1061 }
1062
1063 if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) {
1064 ExprValueKind valueKind;
1065 QualType type;
1066 if (MemberFn->isInstance()) {
1067 valueKind = VK_RValue;
1068 type = Context.BoundMemberTy;
1069 } else {
1070 valueKind = VK_LValue;
1071 type = MemberFn->getType();
1072 }
1073
Eli Friedmanfa0df832012-02-02 03:46:19 +00001074 return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
1075 TemplateKWLoc, MemberFn, FoundDecl,
1076 MemberNameInfo, type, valueKind,
1077 OK_Ordinary));
Douglas Gregor5476205b2011-06-23 00:49:38 +00001078 }
1079 assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?");
1080
1081 if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) {
Eli Friedmanfa0df832012-02-02 03:46:19 +00001082 return Owned(BuildMemberExpr(*this, Context, BaseExpr, IsArrow, SS,
1083 TemplateKWLoc, Enum, FoundDecl, MemberNameInfo,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001084 Enum->getType(), VK_RValue, OK_Ordinary));
1085 }
1086
1087 Owned(BaseExpr);
1088
1089 // We found something that we didn't expect. Complain.
1090 if (isa<TypeDecl>(MemberDecl))
1091 Diag(MemberLoc, diag::err_typecheck_member_reference_type)
1092 << MemberName << BaseType << int(IsArrow);
1093 else
1094 Diag(MemberLoc, diag::err_typecheck_member_reference_unknown)
1095 << MemberName << BaseType << int(IsArrow);
1096
1097 Diag(MemberDecl->getLocation(), diag::note_member_declared_here)
1098 << MemberName;
1099 R.suppressDiagnostics();
1100 return ExprError();
1101}
1102
1103/// Given that normal member access failed on the given expression,
1104/// and given that the expression's type involves builtin-id or
1105/// builtin-Class, decide whether substituting in the redefinition
1106/// types would be profitable. The redefinition type is whatever
1107/// this translation unit tried to typedef to id/Class; we store
1108/// it to the side and then re-use it in places like this.
1109static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) {
1110 const ObjCObjectPointerType *opty
1111 = base.get()->getType()->getAs<ObjCObjectPointerType>();
1112 if (!opty) return false;
1113
1114 const ObjCObjectType *ty = opty->getObjectType();
1115
1116 QualType redef;
1117 if (ty->isObjCId()) {
Douglas Gregor97673472011-08-11 20:58:55 +00001118 redef = S.Context.getObjCIdRedefinitionType();
Douglas Gregor5476205b2011-06-23 00:49:38 +00001119 } else if (ty->isObjCClass()) {
Douglas Gregor97673472011-08-11 20:58:55 +00001120 redef = S.Context.getObjCClassRedefinitionType();
Douglas Gregor5476205b2011-06-23 00:49:38 +00001121 } else {
1122 return false;
1123 }
1124
1125 // Do the substitution as long as the redefinition type isn't just a
1126 // possibly-qualified pointer to builtin-id or builtin-Class again.
1127 opty = redef->getAs<ObjCObjectPointerType>();
Richard Trieuf20d9052012-10-12 17:48:40 +00001128 if (opty && !opty->getObjectType()->getInterface())
Douglas Gregor5476205b2011-06-23 00:49:38 +00001129 return false;
1130
1131 base = S.ImpCastExprToType(base.take(), redef, CK_BitCast);
1132 return true;
1133}
1134
John McCall50a2c2c2011-10-11 23:14:30 +00001135static bool isRecordType(QualType T) {
1136 return T->isRecordType();
1137}
1138static bool isPointerToRecordType(QualType T) {
1139 if (const PointerType *PT = T->getAs<PointerType>())
1140 return PT->getPointeeType()->isRecordType();
1141 return false;
1142}
1143
Richard Smithcab9a7d2011-10-26 19:06:56 +00001144/// Perform conversions on the LHS of a member access expression.
1145ExprResult
1146Sema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) {
Eli Friedman9a766c42012-01-13 02:20:01 +00001147 if (IsArrow && !Base->getType()->isFunctionType())
1148 return DefaultFunctionArrayLvalueConversion(Base);
Richard Smithcab9a7d2011-10-26 19:06:56 +00001149
Eli Friedman9a766c42012-01-13 02:20:01 +00001150 return CheckPlaceholderExpr(Base);
Richard Smithcab9a7d2011-10-26 19:06:56 +00001151}
1152
Douglas Gregor5476205b2011-06-23 00:49:38 +00001153/// Look up the given member of the given non-type-dependent
1154/// expression. This can return in one of two ways:
1155/// * If it returns a sentinel null-but-valid result, the caller will
1156/// assume that lookup was performed and the results written into
1157/// the provided structure. It will take over from there.
1158/// * Otherwise, the returned expression will be produced in place of
1159/// an ordinary member expression.
1160///
1161/// The ObjCImpDecl bit is a gross hack that will need to be properly
1162/// fixed for ObjC++.
1163ExprResult
1164Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
1165 bool &IsArrow, SourceLocation OpLoc,
1166 CXXScopeSpec &SS,
1167 Decl *ObjCImpDecl, bool HasTemplateArgs) {
1168 assert(BaseExpr.get() && "no base expression");
1169
1170 // Perform default conversions.
Richard Smithcab9a7d2011-10-26 19:06:56 +00001171 BaseExpr = PerformMemberExprBaseConversion(BaseExpr.take(), IsArrow);
John McCall50a2c2c2011-10-11 23:14:30 +00001172 if (BaseExpr.isInvalid())
1173 return ExprError();
Douglas Gregor5476205b2011-06-23 00:49:38 +00001174
Douglas Gregor5476205b2011-06-23 00:49:38 +00001175 QualType BaseType = BaseExpr.get()->getType();
1176 assert(!BaseType->isDependentType());
1177
1178 DeclarationName MemberName = R.getLookupName();
1179 SourceLocation MemberLoc = R.getNameLoc();
1180
1181 // For later type-checking purposes, turn arrow accesses into dot
1182 // accesses. The only access type we support that doesn't follow
1183 // the C equivalence "a->b === (*a).b" is ObjC property accesses,
1184 // and those never use arrows, so this is unaffected.
1185 if (IsArrow) {
1186 if (const PointerType *Ptr = BaseType->getAs<PointerType>())
1187 BaseType = Ptr->getPointeeType();
1188 else if (const ObjCObjectPointerType *Ptr
1189 = BaseType->getAs<ObjCObjectPointerType>())
1190 BaseType = Ptr->getPointeeType();
1191 else if (BaseType->isRecordType()) {
1192 // Recover from arrow accesses to records, e.g.:
1193 // struct MyRecord foo;
1194 // foo->bar
1195 // This is actually well-formed in C++ if MyRecord has an
1196 // overloaded operator->, but that should have been dealt with
Kaelyn Uhrain0c51de42013-07-31 17:38:24 +00001197 // by now--or a diagnostic message already issued if a problem
1198 // was encountered while looking for the overloaded operator->.
Kaelyn Uhrainbd6ddaa2013-10-31 20:32:56 +00001199 if (!getLangOpts().CPlusPlus) {
1200 Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
1201 << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
1202 << FixItHint::CreateReplacement(OpLoc, ".");
1203 }
Douglas Gregor5476205b2011-06-23 00:49:38 +00001204 IsArrow = false;
Eli Friedman9a766c42012-01-13 02:20:01 +00001205 } else if (BaseType->isFunctionType()) {
Douglas Gregor5476205b2011-06-23 00:49:38 +00001206 goto fail;
1207 } else {
1208 Diag(MemberLoc, diag::err_typecheck_member_reference_arrow)
1209 << BaseType << BaseExpr.get()->getSourceRange();
1210 return ExprError();
1211 }
1212 }
1213
1214 // Handle field access to simple records.
1215 if (const RecordType *RTy = BaseType->getAs<RecordType>()) {
1216 if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(),
1217 RTy, OpLoc, SS, HasTemplateArgs))
1218 return ExprError();
1219
1220 // Returning valid-but-null is how we indicate to the caller that
1221 // the lookup result was filled in.
Craig Topperc3ec1492014-05-26 06:22:03 +00001222 return Owned((Expr*) nullptr);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001223 }
1224
1225 // Handle ivar access to Objective-C objects.
1226 if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) {
Douglas Gregorbcc95392011-10-10 16:09:49 +00001227 if (!SS.isEmpty() && !SS.isInvalid()) {
Douglas Gregor12340e52011-10-09 23:22:49 +00001228 Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1229 << 1 << SS.getScopeRep()
1230 << FixItHint::CreateRemoval(SS.getRange());
1231 SS.clear();
1232 }
1233
Douglas Gregor5476205b2011-06-23 00:49:38 +00001234 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1235
1236 // There are three cases for the base type:
1237 // - builtin id (qualified or unqualified)
1238 // - builtin Class (qualified or unqualified)
1239 // - an interface
1240 ObjCInterfaceDecl *IDecl = OTy->getInterface();
1241 if (!IDecl) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001242 if (getLangOpts().ObjCAutoRefCount &&
Douglas Gregor5476205b2011-06-23 00:49:38 +00001243 (OTy->isObjCId() || OTy->isObjCClass()))
1244 goto fail;
1245 // There's an implicit 'isa' ivar on all objects.
1246 // But we only actually find it this way on objects of type 'id',
Eric Christopherae6b9d22012-08-16 23:50:37 +00001247 // apparently.
Fariborz Jahanian84510742013-03-27 21:19:25 +00001248 if (OTy->isObjCId() && Member->isStr("isa"))
Douglas Gregor5476205b2011-06-23 00:49:38 +00001249 return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc,
Fariborz Jahanian06bb7f72013-03-28 19:50:55 +00001250 OpLoc,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001251 Context.getObjCClassType()));
Douglas Gregor5476205b2011-06-23 00:49:38 +00001252 if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
1253 return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1254 ObjCImpDecl, HasTemplateArgs);
1255 goto fail;
1256 }
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +00001257
Douglas Gregor7bfb2d02012-05-04 16:32:21 +00001258 if (RequireCompleteType(OpLoc, BaseType, diag::err_typecheck_incomplete_tag,
1259 BaseExpr.get()))
Douglas Gregor5dbf4eb2012-01-02 17:18:37 +00001260 return ExprError();
Craig Topperc3ec1492014-05-26 06:22:03 +00001261
1262 ObjCInterfaceDecl *ClassDeclared = nullptr;
Douglas Gregor5476205b2011-06-23 00:49:38 +00001263 ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared);
1264
1265 if (!IV) {
1266 // Attempt to correct for typos in ivar names.
Kaelyn Uhrain3658e6a2012-01-13 21:28:55 +00001267 DeclFilterCCC<ObjCIvarDecl> Validator;
1268 Validator.IsObjCIvarLookup = IsArrow;
1269 if (TypoCorrection Corrected = CorrectTypo(R.getLookupNameInfo(),
Craig Topperc3ec1492014-05-26 06:22:03 +00001270 LookupMemberName, nullptr,
1271 nullptr, Validator,
1272 CTK_ErrorRecovery, IDecl)) {
Kaelyn Uhrain3658e6a2012-01-13 21:28:55 +00001273 IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>();
Richard Smithf9b15102013-08-17 00:46:16 +00001274 diagnoseTypo(Corrected,
1275 PDiag(diag::err_typecheck_member_reference_ivar_suggest)
1276 << IDecl->getDeclName() << MemberName);
1277
Ted Kremenek679b4782012-03-17 00:53:39 +00001278 // Figure out the class that declares the ivar.
1279 assert(!ClassDeclared);
1280 Decl *D = cast<Decl>(IV->getDeclContext());
1281 if (ObjCCategoryDecl *CAT = dyn_cast<ObjCCategoryDecl>(D))
1282 D = CAT->getClassInterface();
1283 ClassDeclared = cast<ObjCInterfaceDecl>(D);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001284 } else {
Fariborz Jahanianc297cd82011-06-28 00:00:52 +00001285 if (IsArrow && IDecl->FindPropertyDeclaration(Member)) {
1286 Diag(MemberLoc,
1287 diag::err_property_found_suggest)
1288 << Member << BaseExpr.get()->getType()
1289 << FixItHint::CreateReplacement(OpLoc, ".");
1290 return ExprError();
1291 }
Douglas Gregor5476205b2011-06-23 00:49:38 +00001292
1293 Diag(MemberLoc, diag::err_typecheck_member_reference_ivar)
1294 << IDecl->getDeclName() << MemberName
1295 << BaseExpr.get()->getSourceRange();
1296 return ExprError();
1297 }
1298 }
Ted Kremenek679b4782012-03-17 00:53:39 +00001299
1300 assert(ClassDeclared);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001301
1302 // If the decl being referenced had an error, return an error for this
1303 // sub-expr without emitting another error, in order to avoid cascading
1304 // error cases.
1305 if (IV->isInvalidDecl())
1306 return ExprError();
1307
1308 // Check whether we can reference this field.
1309 if (DiagnoseUseOfDecl(IV, MemberLoc))
1310 return ExprError();
1311 if (IV->getAccessControl() != ObjCIvarDecl::Public &&
1312 IV->getAccessControl() != ObjCIvarDecl::Package) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001313 ObjCInterfaceDecl *ClassOfMethodDecl = nullptr;
Douglas Gregor5476205b2011-06-23 00:49:38 +00001314 if (ObjCMethodDecl *MD = getCurMethodDecl())
1315 ClassOfMethodDecl = MD->getClassInterface();
1316 else if (ObjCImpDecl && getCurFunctionDecl()) {
1317 // Case of a c-function declared inside an objc implementation.
1318 // FIXME: For a c-style function nested inside an objc implementation
1319 // class, there is no implementation context available, so we pass
1320 // down the context as argument to this routine. Ideally, this context
1321 // need be passed down in the AST node and somehow calculated from the
1322 // AST for a function decl.
1323 if (ObjCImplementationDecl *IMPD =
1324 dyn_cast<ObjCImplementationDecl>(ObjCImpDecl))
1325 ClassOfMethodDecl = IMPD->getClassInterface();
1326 else if (ObjCCategoryImplDecl* CatImplClass =
1327 dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl))
1328 ClassOfMethodDecl = CatImplClass->getClassInterface();
1329 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001330 if (!getLangOpts().DebuggerSupport) {
Fariborz Jahaniand6cb4a82012-03-07 00:58:41 +00001331 if (IV->getAccessControl() == ObjCIvarDecl::Private) {
1332 if (!declaresSameEntity(ClassDeclared, IDecl) ||
1333 !declaresSameEntity(ClassOfMethodDecl, ClassDeclared))
1334 Diag(MemberLoc, diag::error_private_ivar_access)
1335 << IV->getDeclName();
1336 } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl))
1337 // @protected
1338 Diag(MemberLoc, diag::error_protected_ivar_access)
Douglas Gregor5476205b2011-06-23 00:49:38 +00001339 << IV->getDeclName();
Fariborz Jahaniand6cb4a82012-03-07 00:58:41 +00001340 }
Douglas Gregor5476205b2011-06-23 00:49:38 +00001341 }
Fariborz Jahanian285a7cc2012-08-07 23:48:10 +00001342 bool warn = true;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001343 if (getLangOpts().ObjCAutoRefCount) {
Douglas Gregor5476205b2011-06-23 00:49:38 +00001344 Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts();
1345 if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp))
1346 if (UO->getOpcode() == UO_Deref)
1347 BaseExp = UO->getSubExpr()->IgnoreParenCasts();
1348
1349 if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp))
Fariborz Jahanian285a7cc2012-08-07 23:48:10 +00001350 if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
Douglas Gregor5476205b2011-06-23 00:49:38 +00001351 Diag(DE->getLocation(), diag::error_arc_weak_ivar_access);
Fariborz Jahanian285a7cc2012-08-07 23:48:10 +00001352 warn = false;
1353 }
Douglas Gregor5476205b2011-06-23 00:49:38 +00001354 }
Fariborz Jahaniana5063a62012-08-08 16:41:04 +00001355 if (warn) {
Fariborz Jahaniana7c9f882012-08-07 16:38:44 +00001356 if (ObjCMethodDecl *MD = getCurMethodDecl()) {
1357 ObjCMethodFamily MF = MD->getMethodFamily();
1358 warn = (MF != OMF_init && MF != OMF_dealloc &&
Fariborz Jahaniana934a022013-02-14 19:07:19 +00001359 MF != OMF_finalize &&
1360 !IvarBacksCurrentMethodAccessor(IDecl, MD, IV));
Fariborz Jahaniana7c9f882012-08-07 16:38:44 +00001361 }
1362 if (warn)
1363 Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName();
1364 }
Jordan Rose657b5f42012-09-28 22:21:35 +00001365
1366 ObjCIvarRefExpr *Result = new (Context) ObjCIvarRefExpr(IV, IV->getType(),
Fariborz Jahanianf12ff4df2013-04-02 18:57:54 +00001367 MemberLoc, OpLoc,
Jordan Rose657b5f42012-09-28 22:21:35 +00001368 BaseExpr.take(),
1369 IsArrow);
1370
1371 if (getLangOpts().ObjCAutoRefCount) {
1372 if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
1373 DiagnosticsEngine::Level Level =
1374 Diags.getDiagnosticLevel(diag::warn_arc_repeated_use_of_weak,
1375 MemberLoc);
1376 if (Level != DiagnosticsEngine::Ignored)
Fariborz Jahanian6f829e32013-05-21 21:20:26 +00001377 recordUseOfEvaluatedWeak(Result);
Jordan Rose657b5f42012-09-28 22:21:35 +00001378 }
1379 }
1380
1381 return Owned(Result);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001382 }
1383
1384 // Objective-C property access.
1385 const ObjCObjectPointerType *OPT;
1386 if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) {
Douglas Gregorbcc95392011-10-10 16:09:49 +00001387 if (!SS.isEmpty() && !SS.isInvalid()) {
Douglas Gregor12340e52011-10-09 23:22:49 +00001388 Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access)
1389 << 0 << SS.getScopeRep()
1390 << FixItHint::CreateRemoval(SS.getRange());
1391 SS.clear();
1392 }
1393
Douglas Gregor5476205b2011-06-23 00:49:38 +00001394 // This actually uses the base as an r-value.
1395 BaseExpr = DefaultLvalueConversion(BaseExpr.take());
1396 if (BaseExpr.isInvalid())
1397 return ExprError();
1398
1399 assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType()));
1400
1401 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1402
1403 const ObjCObjectType *OT = OPT->getObjectType();
1404
1405 // id, with and without qualifiers.
1406 if (OT->isObjCId()) {
1407 // Check protocols on qualified interfaces.
1408 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
1409 if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) {
1410 if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) {
1411 // Check the use of this declaration
1412 if (DiagnoseUseOfDecl(PD, MemberLoc))
1413 return ExprError();
1414
John McCall526ab472011-10-25 17:37:35 +00001415 return Owned(new (Context) ObjCPropertyRefExpr(PD,
1416 Context.PseudoObjectTy,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001417 VK_LValue,
1418 OK_ObjCProperty,
1419 MemberLoc,
1420 BaseExpr.take()));
1421 }
1422
1423 if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) {
1424 // Check the use of this method.
1425 if (DiagnoseUseOfDecl(OMD, MemberLoc))
1426 return ExprError();
1427 Selector SetterSel =
Adrian Prantla4ce9062013-06-07 22:29:12 +00001428 SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
1429 PP.getSelectorTable(),
1430 Member);
Craig Topperc3ec1492014-05-26 06:22:03 +00001431 ObjCMethodDecl *SMD = nullptr;
1432 if (Decl *SDecl = FindGetterSetterNameDecl(OPT,
1433 /*Property id*/nullptr,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001434 SetterSel, Context))
1435 SMD = dyn_cast<ObjCMethodDecl>(SDecl);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001436
John McCall526ab472011-10-25 17:37:35 +00001437 return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD,
1438 Context.PseudoObjectTy,
1439 VK_LValue, OK_ObjCProperty,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001440 MemberLoc, BaseExpr.take()));
1441 }
1442 }
1443 // Use of id.member can only be for a property reference. Do not
1444 // use the 'id' redefinition in this case.
1445 if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
1446 return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1447 ObjCImpDecl, HasTemplateArgs);
1448
1449 return ExprError(Diag(MemberLoc, diag::err_property_not_found)
1450 << MemberName << BaseType);
1451 }
1452
1453 // 'Class', unqualified only.
1454 if (OT->isObjCClass()) {
1455 // Only works in a method declaration (??!).
1456 ObjCMethodDecl *MD = getCurMethodDecl();
1457 if (!MD) {
1458 if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
1459 return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1460 ObjCImpDecl, HasTemplateArgs);
1461
1462 goto fail;
1463 }
1464
1465 // Also must look for a getter name which uses property syntax.
1466 Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
1467 ObjCInterfaceDecl *IFace = MD->getClassInterface();
1468 ObjCMethodDecl *Getter;
1469 if ((Getter = IFace->lookupClassMethod(Sel))) {
1470 // Check the use of this method.
1471 if (DiagnoseUseOfDecl(Getter, MemberLoc))
1472 return ExprError();
1473 } else
1474 Getter = IFace->lookupPrivateMethod(Sel, false);
1475 // If we found a getter then this may be a valid dot-reference, we
1476 // will look for the matching setter, in case it is needed.
1477 Selector SetterSel =
Adrian Prantla4ce9062013-06-07 22:29:12 +00001478 SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
1479 PP.getSelectorTable(),
1480 Member);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001481 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
1482 if (!Setter) {
1483 // If this reference is in an @implementation, also check for 'private'
1484 // methods.
1485 Setter = IFace->lookupPrivateMethod(SetterSel, false);
1486 }
Douglas Gregor5476205b2011-06-23 00:49:38 +00001487
1488 if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
1489 return ExprError();
1490
1491 if (Getter || Setter) {
Douglas Gregor5476205b2011-06-23 00:49:38 +00001492 return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
John McCall526ab472011-10-25 17:37:35 +00001493 Context.PseudoObjectTy,
1494 VK_LValue, OK_ObjCProperty,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001495 MemberLoc, BaseExpr.take()));
1496 }
1497
1498 if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr))
1499 return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1500 ObjCImpDecl, HasTemplateArgs);
1501
1502 return ExprError(Diag(MemberLoc, diag::err_property_not_found)
1503 << MemberName << BaseType);
1504 }
1505
1506 // Normal property access.
Fariborz Jahanianc297cd82011-06-28 00:00:52 +00001507 return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc,
1508 MemberName, MemberLoc,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001509 SourceLocation(), QualType(), false);
1510 }
1511
1512 // Handle 'field access' to vectors, such as 'V.xx'.
1513 if (BaseType->isExtVectorType()) {
1514 // FIXME: this expr should store IsArrow.
1515 IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
1516 ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind());
1517 QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc,
1518 Member, MemberLoc);
1519 if (ret.isNull())
1520 return ExprError();
1521
1522 return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(),
1523 *Member, MemberLoc));
1524 }
1525
1526 // Adjust builtin-sel to the appropriate redefinition type if that's
1527 // not just a pointer to builtin-sel again.
1528 if (IsArrow &&
1529 BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) &&
Douglas Gregor97673472011-08-11 20:58:55 +00001530 !Context.getObjCSelRedefinitionType()->isObjCSelType()) {
1531 BaseExpr = ImpCastExprToType(BaseExpr.take(),
1532 Context.getObjCSelRedefinitionType(),
Douglas Gregor5476205b2011-06-23 00:49:38 +00001533 CK_BitCast);
1534 return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1535 ObjCImpDecl, HasTemplateArgs);
1536 }
1537
1538 // Failure cases.
1539 fail:
1540
1541 // Recover from dot accesses to pointers, e.g.:
1542 // type *foo;
1543 // foo.bar
1544 // This is actually well-formed in two cases:
1545 // - 'type' is an Objective C type
1546 // - 'bar' is a pseudo-destructor name which happens to refer to
1547 // the appropriate pointer type
1548 if (const PointerType *Ptr = BaseType->getAs<PointerType>()) {
1549 if (!IsArrow && Ptr->getPointeeType()->isRecordType() &&
1550 MemberName.getNameKind() != DeclarationName::CXXDestructorName) {
1551 Diag(OpLoc, diag::err_typecheck_member_reference_suggestion)
1552 << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange()
1553 << FixItHint::CreateReplacement(OpLoc, "->");
1554
1555 // Recurse as an -> access.
1556 IsArrow = true;
1557 return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1558 ObjCImpDecl, HasTemplateArgs);
1559 }
1560 }
1561
1562 // If the user is trying to apply -> or . to a function name, it's probably
1563 // because they forgot parentheses to call that function.
John McCall50a2c2c2011-10-11 23:14:30 +00001564 if (tryToRecoverWithCall(BaseExpr,
1565 PDiag(diag::err_member_reference_needs_call),
1566 /*complain*/ false,
Eli Friedman9a766c42012-01-13 02:20:01 +00001567 IsArrow ? &isPointerToRecordType : &isRecordType)) {
John McCall50a2c2c2011-10-11 23:14:30 +00001568 if (BaseExpr.isInvalid())
Douglas Gregor5476205b2011-06-23 00:49:38 +00001569 return ExprError();
John McCall50a2c2c2011-10-11 23:14:30 +00001570 BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take());
1571 return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS,
1572 ObjCImpDecl, HasTemplateArgs);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001573 }
1574
Matt Beaumont-Gayd9f244af2012-04-21 01:12:48 +00001575 Diag(OpLoc, diag::err_typecheck_member_reference_struct_union)
Matt Beaumont-Gay025321b2012-04-21 02:13:04 +00001576 << BaseType << BaseExpr.get()->getSourceRange() << MemberLoc;
Douglas Gregor5476205b2011-06-23 00:49:38 +00001577
1578 return ExprError();
1579}
1580
1581/// The main callback when the parser finds something like
1582/// expression . [nested-name-specifier] identifier
1583/// expression -> [nested-name-specifier] identifier
1584/// where 'identifier' encompasses a fairly broad spectrum of
1585/// possibilities, including destructor and operator references.
1586///
1587/// \param OpKind either tok::arrow or tok::period
1588/// \param HasTrailingLParen whether the next token is '(', which
1589/// is used to diagnose mis-uses of special members that can
1590/// only be called
James Dennett2a4d13c2012-06-15 07:13:21 +00001591/// \param ObjCImpDecl the current Objective-C \@implementation
1592/// decl; this is an ugly hack around the fact that Objective-C
1593/// \@implementations aren't properly put in the context chain
Douglas Gregor5476205b2011-06-23 00:49:38 +00001594ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base,
1595 SourceLocation OpLoc,
1596 tok::TokenKind OpKind,
1597 CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001598 SourceLocation TemplateKWLoc,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001599 UnqualifiedId &Id,
1600 Decl *ObjCImpDecl,
1601 bool HasTrailingLParen) {
1602 if (SS.isSet() && SS.isInvalid())
1603 return ExprError();
1604
1605 // Warn about the explicit constructor calls Microsoft extension.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001606 if (getLangOpts().MicrosoftExt &&
Douglas Gregor5476205b2011-06-23 00:49:38 +00001607 Id.getKind() == UnqualifiedId::IK_ConstructorName)
1608 Diag(Id.getSourceRange().getBegin(),
1609 diag::ext_ms_explicit_constructor_call);
1610
1611 TemplateArgumentListInfo TemplateArgsBuffer;
1612
1613 // Decompose the name into its component parts.
1614 DeclarationNameInfo NameInfo;
1615 const TemplateArgumentListInfo *TemplateArgs;
1616 DecomposeUnqualifiedId(Id, TemplateArgsBuffer,
1617 NameInfo, TemplateArgs);
1618
1619 DeclarationName Name = NameInfo.getName();
1620 bool IsArrow = (OpKind == tok::arrow);
1621
1622 NamedDecl *FirstQualifierInScope
Craig Topperc3ec1492014-05-26 06:22:03 +00001623 = (!SS.isSet() ? nullptr : FindFirstQualifierInScope(S, SS.getScopeRep()));
Douglas Gregor5476205b2011-06-23 00:49:38 +00001624
1625 // This is a postfix expression, so get rid of ParenListExprs.
1626 ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base);
1627 if (Result.isInvalid()) return ExprError();
1628 Base = Result.take();
1629
1630 if (Base->getType()->isDependentType() || Name.isDependentName() ||
1631 isDependentScopeSpecifier(SS)) {
1632 Result = ActOnDependentMemberExpr(Base, Base->getType(),
1633 IsArrow, OpLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001634 SS, TemplateKWLoc, FirstQualifierInScope,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001635 NameInfo, TemplateArgs);
1636 } else {
1637 LookupResult R(*this, NameInfo, LookupMemberName);
1638 ExprResult BaseResult = Owned(Base);
1639 Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc,
Craig Topperc3ec1492014-05-26 06:22:03 +00001640 SS, ObjCImpDecl, TemplateArgs != nullptr);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001641 if (BaseResult.isInvalid())
1642 return ExprError();
1643 Base = BaseResult.take();
1644
1645 if (Result.isInvalid()) {
1646 Owned(Base);
1647 return ExprError();
1648 }
1649
1650 if (Result.get()) {
1651 // The only way a reference to a destructor can be used is to
1652 // immediately call it, which falls into this case. If the
1653 // next token is not a '(', produce a diagnostic and build the
1654 // call now.
1655 if (!HasTrailingLParen &&
1656 Id.getKind() == UnqualifiedId::IK_DestructorName)
1657 return DiagnoseDtorReference(NameInfo.getLoc(), Result.get());
1658
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001659 return Result;
Douglas Gregor5476205b2011-06-23 00:49:38 +00001660 }
1661
Kaelyn Uhrain76e07342012-04-25 19:49:54 +00001662 ActOnMemberAccessExtraArgs ExtraArgs = {S, Id, ObjCImpDecl, HasTrailingLParen};
Douglas Gregor5476205b2011-06-23 00:49:38 +00001663 Result = BuildMemberReferenceExpr(Base, Base->getType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001664 OpLoc, IsArrow, SS, TemplateKWLoc,
Kaelyn Uhrain76e07342012-04-25 19:49:54 +00001665 FirstQualifierInScope, R, TemplateArgs,
1666 false, &ExtraArgs);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001667 }
1668
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001669 return Result;
Douglas Gregor5476205b2011-06-23 00:49:38 +00001670}
1671
1672static ExprResult
1673BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow,
1674 const CXXScopeSpec &SS, FieldDecl *Field,
1675 DeclAccessPair FoundDecl,
1676 const DeclarationNameInfo &MemberNameInfo) {
1677 // x.a is an l-value if 'a' has a reference type. Otherwise:
1678 // x.a is an l-value/x-value/pr-value if the base is (and note
1679 // that *x is always an l-value), except that if the base isn't
1680 // an ordinary object then we must have an rvalue.
1681 ExprValueKind VK = VK_LValue;
1682 ExprObjectKind OK = OK_Ordinary;
1683 if (!IsArrow) {
1684 if (BaseExpr->getObjectKind() == OK_Ordinary)
1685 VK = BaseExpr->getValueKind();
1686 else
1687 VK = VK_RValue;
1688 }
1689 if (VK != VK_RValue && Field->isBitField())
1690 OK = OK_BitField;
1691
1692 // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref]
1693 QualType MemberType = Field->getType();
1694 if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) {
1695 MemberType = Ref->getPointeeType();
1696 VK = VK_LValue;
1697 } else {
1698 QualType BaseType = BaseExpr->getType();
1699 if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType();
Matt Arsenault376f7202013-02-26 21:16:00 +00001700
Douglas Gregor5476205b2011-06-23 00:49:38 +00001701 Qualifiers BaseQuals = BaseType.getQualifiers();
Matt Arsenault376f7202013-02-26 21:16:00 +00001702
Douglas Gregor5476205b2011-06-23 00:49:38 +00001703 // GC attributes are never picked up by members.
1704 BaseQuals.removeObjCGCAttr();
Matt Arsenault376f7202013-02-26 21:16:00 +00001705
Douglas Gregor5476205b2011-06-23 00:49:38 +00001706 // CVR attributes from the base are picked up by members,
1707 // except that 'mutable' members don't pick up 'const'.
1708 if (Field->isMutable()) BaseQuals.removeConst();
Matt Arsenault376f7202013-02-26 21:16:00 +00001709
Douglas Gregor5476205b2011-06-23 00:49:38 +00001710 Qualifiers MemberQuals
1711 = S.Context.getCanonicalType(MemberType).getQualifiers();
Matt Arsenault376f7202013-02-26 21:16:00 +00001712
Douglas Gregor5476205b2011-06-23 00:49:38 +00001713 assert(!MemberQuals.hasAddressSpace());
Matt Arsenault376f7202013-02-26 21:16:00 +00001714
1715
Douglas Gregor5476205b2011-06-23 00:49:38 +00001716 Qualifiers Combined = BaseQuals + MemberQuals;
1717 if (Combined != MemberQuals)
1718 MemberType = S.Context.getQualifiedType(MemberType, Combined);
1719 }
Matt Arsenault376f7202013-02-26 21:16:00 +00001720
Daniel Jasper0baec5492012-06-06 08:32:04 +00001721 S.UnusedPrivateFields.remove(Field);
1722
Douglas Gregor5476205b2011-06-23 00:49:38 +00001723 ExprResult Base =
1724 S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(),
1725 FoundDecl, Field);
1726 if (Base.isInvalid())
1727 return ExprError();
Eli Friedmanfa0df832012-02-02 03:46:19 +00001728 return S.Owned(BuildMemberExpr(S, S.Context, Base.take(), IsArrow, SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001729 /*TemplateKWLoc=*/SourceLocation(),
Douglas Gregor5476205b2011-06-23 00:49:38 +00001730 Field, FoundDecl, MemberNameInfo,
1731 MemberType, VK, OK));
1732}
1733
1734/// Builds an implicit member access expression. The current context
1735/// is known to be an instance method, and the given unqualified lookup
1736/// set is known to contain only instance members, at least one of which
1737/// is from an appropriate type.
1738ExprResult
1739Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001740 SourceLocation TemplateKWLoc,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001741 LookupResult &R,
1742 const TemplateArgumentListInfo *TemplateArgs,
1743 bool IsKnownInstance) {
1744 assert(!R.empty() && !R.isAmbiguous());
1745
1746 SourceLocation loc = R.getNameLoc();
Richard Smith59d26d22014-01-17 22:29:43 +00001747
Douglas Gregor5476205b2011-06-23 00:49:38 +00001748 // If this is known to be an instance access, go ahead and build an
1749 // implicit 'this' expression now.
1750 // 'this' expression now.
Douglas Gregor09deffa2011-10-18 16:47:30 +00001751 QualType ThisTy = getCurrentThisType();
Douglas Gregor5476205b2011-06-23 00:49:38 +00001752 assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'");
Craig Topperc3ec1492014-05-26 06:22:03 +00001753
1754 Expr *baseExpr = nullptr; // null signifies implicit access
Douglas Gregor5476205b2011-06-23 00:49:38 +00001755 if (IsKnownInstance) {
1756 SourceLocation Loc = R.getNameLoc();
1757 if (SS.getRange().isValid())
1758 Loc = SS.getRange().getBegin();
Eli Friedman73a04092012-01-07 04:59:52 +00001759 CheckCXXThisCapture(Loc);
Douglas Gregor5476205b2011-06-23 00:49:38 +00001760 baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true);
1761 }
Craig Topperc3ec1492014-05-26 06:22:03 +00001762
Douglas Gregor5476205b2011-06-23 00:49:38 +00001763 return BuildMemberReferenceExpr(baseExpr, ThisTy,
1764 /*OpLoc*/ SourceLocation(),
1765 /*IsArrow*/ true,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001766 SS, TemplateKWLoc,
Craig Topperc3ec1492014-05-26 06:22:03 +00001767 /*FirstQualifierInScope*/ nullptr,
Douglas Gregor5476205b2011-06-23 00:49:38 +00001768 R, TemplateArgs);
1769}