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