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