clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SemaExprMember.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include/clang/Sema -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../include -I /usr/src/gnu/usr.bin/clang/libclangSema/obj -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/lib/Sema/SemaExprMember.cpp
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 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 | |
| 31 | |
| 32 | static bool isProvablyNotDerivedFrom(Sema &SemaRef, CXXRecordDecl *Record, |
| 33 | const BaseSet &Bases) { |
| 34 | auto BaseIsNotInSet = [&Bases](const CXXRecordDecl *Base) { |
| 35 | return !Bases.count(Base->getCanonicalDecl()); |
| 36 | }; |
| 37 | return BaseIsNotInSet(Record) && Record->forallBases(BaseIsNotInSet); |
| 38 | } |
| 39 | |
| 40 | enum IMAKind { |
| 41 | |
| 42 | IMA_Static, |
| 43 | |
| 44 | |
| 45 | IMA_Mixed, |
| 46 | |
| 47 | |
| 48 | |
| 49 | IMA_Mixed_StaticContext, |
| 50 | |
| 51 | |
| 52 | |
| 53 | IMA_Mixed_Unrelated, |
| 54 | |
| 55 | |
| 56 | IMA_Instance, |
| 57 | |
| 58 | |
| 59 | IMA_Unresolved, |
| 60 | |
| 61 | |
| 62 | IMA_Abstract, |
| 63 | |
| 64 | |
| 65 | |
| 66 | IMA_Unresolved_StaticContext, |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | IMA_Field_Uneval_Context, |
| 72 | |
| 73 | |
| 74 | |
| 75 | IMA_Error_StaticContext, |
| 76 | |
| 77 | |
| 78 | |
| 79 | IMA_Error_Unrelated |
| 80 | }; |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 89 | const LookupResult &R) { |
| 90 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
| 91 | |
| 92 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
| 93 | |
| 94 | bool isStaticContext = SemaRef.CXXThisTypeOverride.isNull() && |
| 95 | (!isa<CXXMethodDecl>(DC) || cast<CXXMethodDecl>(DC)->isStatic()); |
| 96 | |
| 97 | if (R.isUnresolvableResult()) |
| 98 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 99 | |
| 100 | |
| 101 | bool hasNonInstance = false; |
| 102 | bool isField = false; |
| 103 | BaseSet Classes; |
| 104 | for (NamedDecl *D : R) { |
| 105 | |
| 106 | D = D->getUnderlyingDecl(); |
| 107 | |
| 108 | if (D->isCXXInstanceMember()) { |
| 109 | isField |= isa<FieldDecl>(D) || isa<MSPropertyDecl>(D) || |
| 110 | isa<IndirectFieldDecl>(D); |
| 111 | |
| 112 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
| 113 | Classes.insert(R->getCanonicalDecl()); |
| 114 | } else |
| 115 | hasNonInstance = true; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | if (Classes.empty()) |
| 121 | return IMA_Static; |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | IMAKind AbstractInstanceResult = IMA_Static; |
| 133 | assert(!AbstractInstanceResult); |
| 134 | switch (SemaRef.ExprEvalContexts.back().Context) { |
| 135 | case Sema::ExpressionEvaluationContext::Unevaluated: |
| 136 | case Sema::ExpressionEvaluationContext::UnevaluatedList: |
| 137 | if (isField && SemaRef.getLangOpts().CPlusPlus11) |
| 138 | AbstractInstanceResult = IMA_Field_Uneval_Context; |
| 139 | break; |
| 140 | |
| 141 | case Sema::ExpressionEvaluationContext::UnevaluatedAbstract: |
| 142 | AbstractInstanceResult = IMA_Abstract; |
| 143 | break; |
| 144 | |
| 145 | case Sema::ExpressionEvaluationContext::DiscardedStatement: |
| 146 | case Sema::ExpressionEvaluationContext::ConstantEvaluated: |
| 147 | case Sema::ExpressionEvaluationContext::PotentiallyEvaluated: |
| 148 | case Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed: |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
| 154 | if (isStaticContext) { |
| 155 | if (hasNonInstance) |
| 156 | return IMA_Mixed_StaticContext; |
| 157 | |
| 158 | return AbstractInstanceResult ? AbstractInstanceResult |
| 159 | : IMA_Error_StaticContext; |
| 160 | } |
| 161 | |
| 162 | CXXRecordDecl *contextClass; |
| 163 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) |
| 164 | contextClass = MD->getParent()->getCanonicalDecl(); |
| 165 | else |
| 166 | contextClass = cast<CXXRecordDecl>(DC); |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | if (R.getNamingClass() && |
| 175 | contextClass->getCanonicalDecl() != |
| 176 | R.getNamingClass()->getCanonicalDecl()) { |
| 177 | |
| 178 | |
| 179 | |
| 180 | Classes.clear(); |
| 181 | Classes.insert(R.getNamingClass()->getCanonicalDecl()); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | if (isProvablyNotDerivedFrom(SemaRef, contextClass, Classes)) |
| 188 | return hasNonInstance ? IMA_Mixed_Unrelated : |
| 189 | AbstractInstanceResult ? AbstractInstanceResult : |
| 190 | IMA_Error_Unrelated; |
| 191 | |
| 192 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | static void diagnoseInstanceReference(Sema &SemaRef, |
| 197 | const CXXScopeSpec &SS, |
| 198 | NamedDecl *Rep, |
| 199 | const DeclarationNameInfo &nameInfo) { |
| 200 | SourceLocation Loc = nameInfo.getLoc(); |
| 201 | SourceRange Range(Loc); |
| 202 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 203 | |
| 204 | |
| 205 | Rep = Rep->getUnderlyingDecl(); |
| 206 | |
| 207 | DeclContext *FunctionLevelDC = SemaRef.getFunctionLevelDeclContext(); |
| 208 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FunctionLevelDC); |
| 209 | CXXRecordDecl *ContextClass = Method ? Method->getParent() : nullptr; |
| 210 | CXXRecordDecl *RepClass = dyn_cast<CXXRecordDecl>(Rep->getDeclContext()); |
| 211 | |
| 212 | bool InStaticMethod = Method && Method->isStatic(); |
| 213 | bool IsField = isa<FieldDecl>(Rep) || isa<IndirectFieldDecl>(Rep); |
| 214 | |
| 215 | if (IsField && InStaticMethod) |
| 216 | |
| 217 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| 218 | << Range << nameInfo.getName(); |
| 219 | else if (ContextClass && RepClass && SS.isEmpty() && !InStaticMethod && |
| 220 | !RepClass->Equals(ContextClass) && RepClass->Encloses(ContextClass)) |
| 221 | |
| 222 | |
| 223 | SemaRef.Diag(Loc, diag::err_nested_non_static_member_use) |
| 224 | << IsField << RepClass << nameInfo.getName() << ContextClass << Range; |
| 225 | else if (IsField) |
| 226 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
| 227 | << nameInfo.getName() << Range; |
| 228 | else |
| 229 | SemaRef.Diag(Loc, diag::err_member_call_without_object) |
| 230 | << Range; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | ExprResult Sema::BuildPossibleImplicitMemberExpr( |
| 235 | const CXXScopeSpec &SS, SourceLocation TemplateKWLoc, LookupResult &R, |
| 236 | const TemplateArgumentListInfo *TemplateArgs, const Scope *S, |
| 237 | UnresolvedLookupExpr *AsULE) { |
| 238 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 239 | case IMA_Instance: |
| 240 | return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, true, S); |
| 241 | |
| 242 | case IMA_Mixed: |
| 243 | case IMA_Mixed_Unrelated: |
| 244 | case IMA_Unresolved: |
| 245 | return BuildImplicitMemberExpr(SS, TemplateKWLoc, R, TemplateArgs, false, |
| 246 | S); |
| 247 | |
| 248 | case IMA_Field_Uneval_Context: |
| 249 | Diag(R.getNameLoc(), diag::warn_cxx98_compat_non_static_member_use) |
| 250 | << R.getLookupNameInfo().getName(); |
| 251 | LLVM_FALLTHROUGH; |
| 252 | case IMA_Static: |
| 253 | case IMA_Abstract: |
| 254 | case IMA_Mixed_StaticContext: |
| 255 | case IMA_Unresolved_StaticContext: |
| 256 | if (TemplateArgs || TemplateKWLoc.isValid()) |
| 257 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs); |
| 258 | return AsULE ? AsULE : BuildDeclarationNameExpr(SS, R, false); |
| 259 | |
| 260 | case IMA_Error_StaticContext: |
| 261 | case IMA_Error_Unrelated: |
| 262 | diagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
| 263 | R.getLookupNameInfo()); |
| 264 | return ExprError(); |
| 265 | } |
| 266 | |
| 267 | llvm_unreachable("unexpected instance member access kind"); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | static bool |
| 272 | IsRGBA(char c) { |
| 273 | switch (c) { |
| 274 | case 'r': |
| 275 | case 'g': |
| 276 | case 'b': |
| 277 | case 'a': |
| 278 | return true; |
| 279 | default: |
| 280 | return false; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | static bool IsValidOpenCLComponentSwizzleLength(unsigned len) |
| 288 | { |
| 289 | return (len >= 1 && len <= 4) || len == 8 || len == 16; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | static QualType |
| 297 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 298 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
| 299 | SourceLocation CompLoc) { |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
| 16 | | Assuming the object is not a 'ExtVectorType' | |
|
| 17 | | 'vecType' initialized to a null pointer value | |
|
| 306 | |
| 307 | |
| 308 | const char *compStr = CompName->getNameStart(); |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | bool HalvingSwizzle = false; |
| 314 | |
| 315 | |
| 316 | |
| 317 | bool HexSwizzle = (*compStr == 's' || *compStr == 'S') && compStr[1]; |
| 18 | | Assuming the condition is false | |
|
| 19 | | Assuming the condition is false | |
|
| 318 | |
| 319 | bool HasRepeated = false; |
| 320 | bool HasIndex[16] = {}; |
| 321 | |
| 322 | int Idx; |
| 323 | |
| 324 | |
| 325 | |
| 326 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
| 20 | | Assuming the condition is false | |
|
| 21 | | Assuming the condition is false | |
|
| |
| 327 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 22 | | Assuming the condition is false | |
|
| 23 | | Assuming the condition is false | |
|
| 328 | HalvingSwizzle = true; |
| 329 | } else if (!HexSwizzle && |
| |
| 330 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 331 | bool HasRGBA = IsRGBA(*compStr); |
| 332 | do { |
| 333 | |
| 334 | if (HasRGBA != IsRGBA(*compStr)) |
| 335 | break; |
| 336 | if (HasIndex[Idx]) HasRepeated = true; |
| 337 | HasIndex[Idx] = true; |
| 338 | compStr++; |
| 339 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 340 | |
| 341 | |
| 342 | if (HasRGBA || (*compStr && IsRGBA(*compStr))) { |
| 343 | if (S.getLangOpts().OpenCL && S.getLangOpts().OpenCLVersion < 300) { |
| 344 | const char *DiagBegin = HasRGBA ? CompName->getNameStart() : compStr; |
| 345 | S.Diag(OpLoc, diag::ext_opencl_ext_vector_type_rgba_selector) |
| 346 | << StringRef(DiagBegin, 1) << SourceRange(CompLoc); |
| 347 | } |
| 348 | } |
| 349 | } else { |
| 350 | if (HexSwizzle) compStr++; |
| |
| 351 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 27 | | Loop condition is false. Execution continues on line 358 | |
|
| 352 | if (HasIndex[Idx]) HasRepeated = true; |
| 353 | HasIndex[Idx] = true; |
| 354 | compStr++; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (!HalvingSwizzle && *compStr) { |
| 28 | | Assuming the condition is false | |
|
| |
| 359 | |
| 360 | |
| 361 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 362 | << StringRef(compStr, 1) << SourceRange(CompLoc); |
| 363 | return QualType(); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | |
| 368 | if (!HalvingSwizzle) { |
| |
| 369 | compStr = CompName->getNameStart(); |
| 370 | |
| 371 | if (HexSwizzle) |
| |
| 372 | compStr++; |
| 373 | |
| 374 | while (*compStr) { |
| 32 | | Loop condition is false. Execution continues on line 385 | |
|
| 375 | if (!vecType->isAccessorWithinNumElements(*compStr++, HexSwizzle)) { |
| 376 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 377 | << baseType << SourceRange(CompLoc); |
| 378 | return QualType(); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | |
| 384 | |
| 385 | if (S.getLangOpts().OpenCL && !HalvingSwizzle) { |
| 33 | | Assuming field 'OpenCL' is 0 | |
|
| 386 | unsigned SwizzleLength = CompName->getLength(); |
| 387 | |
| 388 | if (HexSwizzle) |
| 389 | SwizzleLength--; |
| 390 | |
| 391 | if (IsValidOpenCLComponentSwizzleLength(SwizzleLength) == false) { |
| 392 | S.Diag(OpLoc, diag::err_opencl_ext_vector_component_invalid_length) |
| 393 | << SwizzleLength << SourceRange(CompLoc); |
| 394 | return QualType(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
| |
| 404 | : CompName->getLength(); |
| 405 | if (HexSwizzle) |
| |
| 406 | CompSize--; |
| 407 | |
| 408 | if (CompSize == 1) |
| 36 | | Assuming 'CompSize' is not equal to 1 | |
|
| |
| 409 | return vecType->getElementType(); |
| 410 | |
| 411 | if (HasRepeated) |
| |
| 412 | VK = VK_PRValue; |
| 413 | |
| 414 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
| 39 | | Called C++ object pointer is null |
|
| 415 | |
| 416 | |
| 417 | for (Sema::ExtVectorDeclsType::iterator |
| 418 | I = S.ExtVectorDecls.begin(S.getExternalSource()), |
| 419 | E = S.ExtVectorDecls.end(); |
| 420 | I != E; ++I) { |
| 421 | if ((*I)->getUnderlyingType() == VT) |
| 422 | return S.Context.getTypedefType(*I); |
| 423 | } |
| 424 | |
| 425 | return VT; |
| 426 | } |
| 427 | |
| 428 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
| 429 | IdentifierInfo *Member, |
| 430 | const Selector &Sel, |
| 431 | ASTContext &Context) { |
| 432 | if (Member) |
| 433 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration( |
| 434 | Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) |
| 435 | return PD; |
| 436 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
| 437 | return OMD; |
| 438 | |
| 439 | for (const auto *I : PDecl->protocols()) { |
| 440 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(I, Member, Sel, |
| 441 | Context)) |
| 442 | return D; |
| 443 | } |
| 444 | return nullptr; |
| 445 | } |
| 446 | |
| 447 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 448 | IdentifierInfo *Member, |
| 449 | const Selector &Sel, |
| 450 | ASTContext &Context) { |
| 451 | |
| 452 | Decl *GDecl = nullptr; |
| 453 | for (const auto *I : QIdTy->quals()) { |
| 454 | if (Member) |
| 455 | if (ObjCPropertyDecl *PD = I->FindPropertyDeclaration( |
| 456 | Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
| 457 | GDecl = PD; |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | if (ObjCMethodDecl *OMD = I->getInstanceMethod(Sel)) { |
| 462 | GDecl = OMD; |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | if (!GDecl) { |
| 467 | for (const auto *I : QIdTy->quals()) { |
| 468 | |
| 469 | GDecl = FindGetterSetterNameDeclFromProtocolList(I, Member, Sel, Context); |
| 470 | if (GDecl) |
| 471 | return GDecl; |
| 472 | } |
| 473 | } |
| 474 | return GDecl; |
| 475 | } |
| 476 | |
| 477 | ExprResult |
| 478 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
| 479 | bool IsArrow, SourceLocation OpLoc, |
| 480 | const CXXScopeSpec &SS, |
| 481 | SourceLocation TemplateKWLoc, |
| 482 | NamedDecl *FirstQualifierInScope, |
| 483 | const DeclarationNameInfo &NameInfo, |
| 484 | const TemplateArgumentListInfo *TemplateArgs) { |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | if (!IsArrow) { |
| 495 | const PointerType *PT = BaseType->getAs<PointerType>(); |
| 496 | if (PT && (!getLangOpts().ObjC || |
| 497 | PT->getPointeeType()->isRecordType())) { |
| 498 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
| 499 | Diag(OpLoc, diag::err_typecheck_member_reference_struct_union) |
| 500 | << BaseType << BaseExpr->getSourceRange() << NameInfo.getSourceRange(); |
| 501 | return ExprError(); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | assert(BaseType->isDependentType() || |
| 506 | NameInfo.getName().isDependentName() || |
| 507 | isDependentScopeSpecifier(SS)); |
| 508 | |
| 509 | |
| 510 | |
| 511 | return CXXDependentScopeMemberExpr::Create( |
| 512 | Context, BaseExpr, BaseType, IsArrow, OpLoc, |
| 513 | SS.getWithLocInContext(Context), TemplateKWLoc, FirstQualifierInScope, |
| 514 | NameInfo, TemplateArgs); |
| 515 | } |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 521 | Expr *BaseExpr, |
| 522 | QualType BaseType, |
| 523 | const CXXScopeSpec &SS, |
| 524 | NamedDecl *rep, |
| 525 | const DeclarationNameInfo &nameInfo) { |
| 526 | |
| 527 | |
| 528 | if (!BaseExpr) |
| 529 | return diagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
| 530 | |
| 531 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 532 | << SS.getRange() << rep << BaseType; |
| 533 | } |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 548 | QualType BaseType, |
| 549 | const CXXScopeSpec &SS, |
| 550 | const LookupResult &R) { |
| 551 | CXXRecordDecl *BaseRecord = |
| 552 | cast_or_null<CXXRecordDecl>(computeDeclContext(BaseType)); |
| 553 | if (!BaseRecord) { |
| 554 | |
| 555 | |
| 556 | assert(BaseType->isDependentType()); |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 561 | |
| 562 | |
| 563 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
| 564 | return false; |
| 565 | |
| 566 | |
| 567 | DeclContext *DC = (*I)->getDeclContext(); |
| 568 | while (DC->isTransparentContext()) |
| 569 | DC = DC->getParent(); |
| 570 | |
| 571 | if (!DC->isRecord()) |
| 572 | continue; |
| 573 | |
| 574 | CXXRecordDecl *MemberRecord = cast<CXXRecordDecl>(DC)->getCanonicalDecl(); |
| 575 | if (BaseRecord->getCanonicalDecl() == MemberRecord || |
| 576 | !BaseRecord->isProvablyNotDerivedFrom(MemberRecord)) |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 581 | R.getRepresentativeDecl(), |
| 582 | R.getLookupNameInfo()); |
| 583 | return true; |
| 584 | } |
| 585 | |
| 586 | namespace { |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | class RecordMemberExprValidatorCCC final : public CorrectionCandidateCallback { |
| 592 | public: |
| 593 | explicit RecordMemberExprValidatorCCC(const RecordType *RTy) |
| 594 | : Record(RTy->getDecl()) { |
| 595 | |
| 596 | |
| 597 | WantTypeSpecifiers = false; |
| 598 | WantExpressionKeywords = false; |
| 599 | WantCXXNamedCasts = false; |
| 600 | WantFunctionLikeCasts = false; |
| 601 | WantRemainingKeywords = false; |
| 602 | } |
| 603 | |
| 604 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 605 | NamedDecl *ND = candidate.getCorrectionDecl(); |
| 606 | |
| 607 | |
| 608 | if (!ND || !(isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND))) |
| 609 | return false; |
| 610 | |
| 611 | |
| 612 | if (Record->containsDecl(ND)) |
| 613 | return true; |
| 614 | |
| 615 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Record)) { |
| 616 | |
| 617 | for (const auto &BS : RD->bases()) { |
| 618 | if (const RecordType *BSTy = |
| 619 | dyn_cast_or_null<RecordType>(BS.getType().getTypePtrOrNull())) { |
| 620 | if (BSTy->getDecl()->containsDecl(ND)) |
| 621 | return true; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 630 | return std::make_unique<RecordMemberExprValidatorCCC>(*this); |
| 631 | } |
| 632 | |
| 633 | private: |
| 634 | const RecordDecl *const Record; |
| 635 | }; |
| 636 | |
| 637 | } |
| 638 | |
| 639 | static bool LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 640 | Expr *BaseExpr, |
| 641 | const RecordType *RTy, |
| 642 | SourceLocation OpLoc, bool IsArrow, |
| 643 | CXXScopeSpec &SS, bool HasTemplateArgs, |
| 644 | SourceLocation TemplateKWLoc, |
| 645 | TypoExpr *&TE) { |
| 646 | SourceRange BaseRange = BaseExpr ? BaseExpr->getSourceRange() : SourceRange(); |
| 647 | RecordDecl *RDecl = RTy->getDecl(); |
| 648 | if (!SemaRef.isThisOutsideMemberFunctionBody(QualType(RTy, 0)) && |
| 649 | SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
| 650 | diag::err_typecheck_incomplete_tag, |
| 651 | BaseRange)) |
| 652 | return true; |
| 653 | |
| 654 | if (HasTemplateArgs || TemplateKWLoc.isValid()) { |
| 655 | |
| 656 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 657 | |
| 658 | bool MOUS; |
| 659 | return SemaRef.LookupTemplateName(R, nullptr, SS, ObjectType, false, MOUS, |
| 660 | TemplateKWLoc); |
| 661 | } |
| 662 | |
| 663 | DeclContext *DC = RDecl; |
| 664 | if (SS.isSet()) { |
| 665 | |
| 666 | |
| 667 | DC = SemaRef.computeDeclContext(SS, false); |
| 668 | |
| 669 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
| 670 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 671 | << SS.getRange() << DC; |
| 672 | return true; |
| 673 | } |
| 674 | |
| 675 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 676 | |
| 677 | if (!isa<TypeDecl>(DC)) { |
| 678 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 679 | << DC << SS.getRange(); |
| 680 | return true; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | |
| 685 | SemaRef.LookupQualifiedName(R, DC, SS); |
| 686 | |
| 687 | if (!R.empty()) |
| 688 | return false; |
| 689 | |
| 690 | DeclarationName Typo = R.getLookupName(); |
| 691 | SourceLocation TypoLoc = R.getNameLoc(); |
| 692 | |
| 693 | struct QueryState { |
| 694 | Sema &SemaRef; |
| 695 | DeclarationNameInfo NameInfo; |
| 696 | Sema::LookupNameKind LookupKind; |
| 697 | Sema::RedeclarationKind Redecl; |
| 698 | }; |
| 699 | QueryState Q = {R.getSema(), R.getLookupNameInfo(), R.getLookupKind(), |
| 700 | R.redeclarationKind()}; |
| 701 | RecordMemberExprValidatorCCC CCC(RTy); |
| 702 | TE = SemaRef.CorrectTypoDelayed( |
| 703 | R.getLookupNameInfo(), R.getLookupKind(), nullptr, &SS, CCC, |
| 704 | [=, &SemaRef](const TypoCorrection &TC) { |
| 705 | if (TC) { |
| 706 | assert(!TC.isKeyword() && |
| 707 | "Got a keyword as a correction for a member!"); |
| 708 | bool DroppedSpecifier = |
| 709 | TC.WillReplaceSpecifier() && |
| 710 | Typo.getAsString() == TC.getAsString(SemaRef.getLangOpts()); |
| 711 | SemaRef.diagnoseTypo(TC, SemaRef.PDiag(diag::err_no_member_suggest) |
| 712 | << Typo << DC << DroppedSpecifier |
| 713 | << SS.getRange()); |
| 714 | } else { |
| 715 | SemaRef.Diag(TypoLoc, diag::err_no_member) << Typo << DC << BaseRange; |
| 716 | } |
| 717 | }, |
| 718 | [=](Sema &SemaRef, TypoExpr *TE, TypoCorrection TC) mutable { |
| 719 | LookupResult R(Q.SemaRef, Q.NameInfo, Q.LookupKind, Q.Redecl); |
| 720 | R.clear(); |
| 721 | R.suppressDiagnostics(); |
| 722 | R.setLookupName(TC.getCorrection()); |
| 723 | for (NamedDecl *ND : TC) |
| 724 | R.addDecl(ND); |
| 725 | R.resolveKind(); |
| 726 | return SemaRef.BuildMemberReferenceExpr( |
| 727 | BaseExpr, BaseExpr->getType(), OpLoc, IsArrow, SS, SourceLocation(), |
| 728 | nullptr, R, nullptr, nullptr); |
| 729 | }, |
| 730 | Sema::CTK_ErrorRecovery, DC); |
| 731 | |
| 732 | return false; |
| 733 | } |
| 734 | |
| 735 | static ExprResult LookupMemberExpr(Sema &S, LookupResult &R, |
| 736 | ExprResult &BaseExpr, bool &IsArrow, |
| 737 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 738 | Decl *ObjCImpDecl, bool HasTemplateArgs, |
| 739 | SourceLocation TemplateKWLoc); |
| 740 | |
| 741 | ExprResult |
| 742 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
| 743 | SourceLocation OpLoc, bool IsArrow, |
| 744 | CXXScopeSpec &SS, |
| 745 | SourceLocation TemplateKWLoc, |
| 746 | NamedDecl *FirstQualifierInScope, |
| 747 | const DeclarationNameInfo &NameInfo, |
| 748 | const TemplateArgumentListInfo *TemplateArgs, |
| 749 | const Scope *S, |
| 750 | ActOnMemberAccessExtraArgs *ExtraArgs) { |
| 751 | if (BaseType->isDependentType() || |
| 752 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
| 753 | return ActOnDependentMemberExpr(Base, BaseType, |
| 754 | IsArrow, OpLoc, |
| 755 | SS, TemplateKWLoc, FirstQualifierInScope, |
| 756 | NameInfo, TemplateArgs); |
| 757 | |
| 758 | LookupResult R(*this, NameInfo, LookupMemberName); |
| 759 | |
| 760 | |
| 761 | if (!Base) { |
| 762 | TypoExpr *TE = nullptr; |
| 763 | QualType RecordTy = BaseType; |
| 764 | if (IsArrow) RecordTy = RecordTy->castAs<PointerType>()->getPointeeType(); |
| 765 | if (LookupMemberExprInRecord( |
| 766 | *this, R, nullptr, RecordTy->getAs<RecordType>(), OpLoc, IsArrow, |
| 767 | SS, TemplateArgs != nullptr, TemplateKWLoc, TE)) |
| 768 | return ExprError(); |
| 769 | if (TE) |
| 770 | return TE; |
| 771 | |
| 772 | |
| 773 | } else { |
| 774 | ExprResult BaseResult = Base; |
| 775 | ExprResult Result = |
| 776 | LookupMemberExpr(*this, R, BaseResult, IsArrow, OpLoc, SS, |
| 777 | ExtraArgs ? ExtraArgs->ObjCImpDecl : nullptr, |
| 778 | TemplateArgs != nullptr, TemplateKWLoc); |
| 779 | |
| 780 | if (BaseResult.isInvalid()) |
| 781 | return ExprError(); |
| 782 | Base = BaseResult.get(); |
| 783 | |
| 784 | if (Result.isInvalid()) |
| 785 | return ExprError(); |
| 786 | |
| 787 | if (Result.get()) |
| 788 | return Result; |
| 789 | |
| 790 | |
| 791 | BaseType = Base->getType(); |
| 792 | } |
| 793 | |
| 794 | return BuildMemberReferenceExpr(Base, BaseType, |
| 795 | OpLoc, IsArrow, SS, TemplateKWLoc, |
| 796 | FirstQualifierInScope, R, TemplateArgs, S, |
| 797 | false, ExtraArgs); |
| 798 | } |
| 799 | |
| 800 | ExprResult |
| 801 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 802 | SourceLocation loc, |
| 803 | IndirectFieldDecl *indirectField, |
| 804 | DeclAccessPair foundDecl, |
| 805 | Expr *baseObjectExpr, |
| 806 | SourceLocation opLoc) { |
| 807 | |
| 808 | |
| 809 | |
| 810 | VarDecl *baseVariable = indirectField->getVarDecl(); |
| 811 | CXXScopeSpec EmptySS; |
| 812 | if (baseVariable) { |
| 813 | assert(baseVariable->getType()->isRecordType()); |
| 814 | |
| 815 | |
| 816 | |
| 817 | |
| 818 | |
| 819 | |
| 820 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 821 | |
| 822 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 823 | |
| 824 | ExprResult result |
| 825 | = BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
| 826 | if (result.isInvalid()) return ExprError(); |
| 827 | |
| 828 | baseObjectExpr = result.get(); |
| 829 | } |
| 830 | |
| 831 | assert((baseVariable || baseObjectExpr) && |
| 832 | "referencing anonymous struct/union without a base variable or " |
| 833 | "expression"); |
| 834 | |
| 835 | |
| 836 | |
| 837 | Expr *result = baseObjectExpr; |
| 838 | IndirectFieldDecl::chain_iterator |
| 839 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
| 840 | |
| 841 | |
| 842 | |
| 843 | if (!baseVariable) { |
| 844 | FieldDecl *field = cast<FieldDecl>(*FI); |
| 845 | |
| 846 | bool baseObjectIsPointer = baseObjectExpr->getType()->isPointerType(); |
| 847 | |
| 848 | |
| 849 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 850 | |
| 851 | |
| 852 | result = |
| 853 | BuildFieldReferenceExpr(result, baseObjectIsPointer, SourceLocation(), |
| 854 | SS, field, foundDecl, memberNameInfo) |
| 855 | .get(); |
| 856 | if (!result) |
| 857 | return ExprError(); |
| 858 | } |
| 859 | |
| 860 | |
| 861 | ++FI; |
| 862 | |
| 863 | while (FI != FEnd) { |
| 864 | FieldDecl *field = cast<FieldDecl>(*FI++); |
| 865 | |
| 866 | |
| 867 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 868 | DeclAccessPair fakeFoundDecl = |
| 869 | DeclAccessPair::make(field, field->getAccess()); |
| 870 | |
| 871 | result = |
| 872 | BuildFieldReferenceExpr(result, false, SourceLocation(), |
| 873 | (FI == FEnd ? SS : EmptySS), field, |
| 874 | fakeFoundDecl, memberNameInfo) |
| 875 | .get(); |
| 876 | } |
| 877 | |
| 878 | return result; |
| 879 | } |
| 880 | |
| 881 | static ExprResult |
| 882 | BuildMSPropertyRefExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 883 | const CXXScopeSpec &SS, |
| 884 | MSPropertyDecl *PD, |
| 885 | const DeclarationNameInfo &NameInfo) { |
| 886 | |
| 887 | |
| 888 | return new (S.Context) MSPropertyRefExpr(BaseExpr, PD, IsArrow, |
| 889 | S.Context.PseudoObjectTy, VK_LValue, |
| 890 | SS.getWithLocInContext(S.Context), |
| 891 | NameInfo.getLoc()); |
| 892 | } |
| 893 | |
| 894 | MemberExpr *Sema::BuildMemberExpr( |
| 895 | Expr *Base, bool IsArrow, SourceLocation OpLoc, const CXXScopeSpec *SS, |
| 896 | SourceLocation TemplateKWLoc, ValueDecl *Member, DeclAccessPair FoundDecl, |
| 897 | bool HadMultipleCandidates, const DeclarationNameInfo &MemberNameInfo, |
| 898 | QualType Ty, ExprValueKind VK, ExprObjectKind OK, |
| 899 | const TemplateArgumentListInfo *TemplateArgs) { |
| 900 | NestedNameSpecifierLoc NNS = |
| 901 | SS ? SS->getWithLocInContext(Context) : NestedNameSpecifierLoc(); |
| 902 | return BuildMemberExpr(Base, IsArrow, OpLoc, NNS, TemplateKWLoc, Member, |
| 903 | FoundDecl, HadMultipleCandidates, MemberNameInfo, Ty, |
| 904 | VK, OK, TemplateArgs); |
| 905 | } |
| 906 | |
| 907 | MemberExpr *Sema::BuildMemberExpr( |
| 908 | Expr *Base, bool IsArrow, SourceLocation OpLoc, NestedNameSpecifierLoc NNS, |
| 909 | SourceLocation TemplateKWLoc, ValueDecl *Member, DeclAccessPair FoundDecl, |
| 910 | bool HadMultipleCandidates, const DeclarationNameInfo &MemberNameInfo, |
| 911 | QualType Ty, ExprValueKind VK, ExprObjectKind OK, |
| 912 | const TemplateArgumentListInfo *TemplateArgs) { |
| 913 | assert((!IsArrow || Base->isPRValue()) && |
| 914 | "-> base must be a pointer prvalue"); |
| 915 | MemberExpr *E = |
| 916 | MemberExpr::Create(Context, Base, IsArrow, OpLoc, NNS, TemplateKWLoc, |
| 917 | Member, FoundDecl, MemberNameInfo, TemplateArgs, Ty, |
| 918 | VK, OK, getNonOdrUseReasonInCurrentContext(Member)); |
| 919 | E->setHadMultipleCandidates(HadMultipleCandidates); |
| 920 | MarkMemberReferenced(E); |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | if (auto *FPT = Ty->getAs<FunctionProtoType>()) { |
| 927 | if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) { |
| 928 | if (auto *NewFPT = ResolveExceptionSpec(MemberNameInfo.getLoc(), FPT)) |
| 929 | E->setType(Context.getQualifiedType(NewFPT, Ty.getQualifiers())); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | return E; |
| 934 | } |
| 935 | |
| 936 | |
| 937 | static bool IsInFnTryBlockHandler(const Scope *S) { |
| 938 | |
| 939 | |
| 940 | |
| 941 | for (; S != S->getFnParent(); S = S->getParent()) { |
| 942 | if (S->getFlags() & Scope::FnTryCatchScope) |
| 943 | return (S->getFlags() & Scope::TryScope) != Scope::TryScope; |
| 944 | } |
| 945 | return false; |
| 946 | } |
| 947 | |
| 948 | ExprResult |
| 949 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
| 950 | SourceLocation OpLoc, bool IsArrow, |
| 951 | const CXXScopeSpec &SS, |
| 952 | SourceLocation TemplateKWLoc, |
| 953 | NamedDecl *FirstQualifierInScope, |
| 954 | LookupResult &R, |
| 955 | const TemplateArgumentListInfo *TemplateArgs, |
| 956 | const Scope *S, |
| 957 | bool SuppressQualifierCheck, |
| 958 | ActOnMemberAccessExtraArgs *ExtraArgs) { |
| 959 | QualType BaseType = BaseExprType; |
| 960 | if (IsArrow) { |
| 961 | assert(BaseType->isPointerType()); |
| 962 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
| 963 | } |
| 964 | R.setBaseObjectType(BaseType); |
| 965 | |
| 966 | |
| 967 | |
| 968 | if (!IsArrow && BaseExpr && BaseExpr->isPRValue()) { |
| 969 | ExprResult Converted = TemporaryMaterializationConversion(BaseExpr); |
| 970 | if (Converted.isInvalid()) |
| 971 | return ExprError(); |
| 972 | BaseExpr = Converted.get(); |
| 973 | } |
| 974 | |
| 975 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 976 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 977 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
| 978 | |
| 979 | if (R.isAmbiguous()) |
| 980 | return ExprError(); |
| 981 | |
| 982 | |
| 983 | |
| 984 | |
| 985 | const auto *FD = getCurFunctionDecl(); |
| 986 | if (S && BaseExpr && FD && |
| 987 | (isa<CXXDestructorDecl>(FD) || isa<CXXConstructorDecl>(FD)) && |
| 988 | isa<CXXThisExpr>(BaseExpr->IgnoreImpCasts()) && |
| 989 | IsInFnTryBlockHandler(S)) |
| 990 | Diag(MemberLoc, diag::warn_cdtor_function_try_handler_mem_expr) |
| 991 | << isa<CXXDestructorDecl>(FD); |
| 992 | |
| 993 | if (R.empty()) { |
| 994 | |
| 995 | DeclContext *DC = (SS.isSet() |
| 996 | ? computeDeclContext(SS, false) |
| 997 | : BaseType->castAs<RecordType>()->getDecl()); |
| 998 | |
| 999 | if (ExtraArgs) { |
| 1000 | ExprResult RetryExpr; |
| 1001 | if (!IsArrow && BaseExpr) { |
| 1002 | SFINAETrap Trap(*this, true); |
| 1003 | ParsedType ObjectType; |
| 1004 | bool MayBePseudoDestructor = false; |
| 1005 | RetryExpr = ActOnStartCXXMemberReference(getCurScope(), BaseExpr, |
| 1006 | OpLoc, tok::arrow, ObjectType, |
| 1007 | MayBePseudoDestructor); |
| 1008 | if (RetryExpr.isUsable() && !Trap.hasErrorOccurred()) { |
| 1009 | CXXScopeSpec TempSS(SS); |
| 1010 | RetryExpr = ActOnMemberAccessExpr( |
| 1011 | ExtraArgs->S, RetryExpr.get(), OpLoc, tok::arrow, TempSS, |
| 1012 | TemplateKWLoc, ExtraArgs->Id, ExtraArgs->ObjCImpDecl); |
| 1013 | } |
| 1014 | if (Trap.hasErrorOccurred()) |
| 1015 | RetryExpr = ExprError(); |
| 1016 | } |
| 1017 | if (RetryExpr.isUsable()) { |
| 1018 | Diag(OpLoc, diag::err_no_member_overloaded_arrow) |
| 1019 | << MemberName << DC << FixItHint::CreateReplacement(OpLoc, "->"); |
| 1020 | return RetryExpr; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1025 | << MemberName << DC |
| 1026 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
| 1027 | return ExprError(); |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | |
| 1036 | |
| 1037 | if ((SS.isSet() || !BaseExpr || |
| 1038 | (isa<CXXThisExpr>(BaseExpr) && |
| 1039 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
| 1040 | !SuppressQualifierCheck && |
| 1041 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
| 1042 | return ExprError(); |
| 1043 | |
| 1044 | |
| 1045 | |
| 1046 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
| 1047 | |
| 1048 | |
| 1049 | R.suppressDiagnostics(); |
| 1050 | |
| 1051 | UnresolvedMemberExpr *MemExpr |
| 1052 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
| 1053 | BaseExpr, BaseExprType, |
| 1054 | IsArrow, OpLoc, |
| 1055 | SS.getWithLocInContext(Context), |
| 1056 | TemplateKWLoc, MemberNameInfo, |
| 1057 | TemplateArgs, R.begin(), R.end()); |
| 1058 | |
| 1059 | return MemExpr; |
| 1060 | } |
| 1061 | |
| 1062 | assert(R.isSingleResult()); |
| 1063 | DeclAccessPair FoundDecl = R.begin().getPair(); |
| 1064 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | if (MemberDecl->isInvalidDecl()) |
| 1072 | return ExprError(); |
| 1073 | |
| 1074 | |
| 1075 | if (!BaseExpr) { |
| 1076 | |
| 1077 | if (!MemberDecl->isCXXInstanceMember()) { |
| 1078 | |
| 1079 | |
| 1080 | if (TemplateArgs || TemplateKWLoc.isValid()) |
| 1081 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs); |
| 1082 | |
| 1083 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl, |
| 1084 | FoundDecl, TemplateArgs); |
| 1085 | } |
| 1086 | SourceLocation Loc = R.getNameLoc(); |
| 1087 | if (SS.getRange().isValid()) |
| 1088 | Loc = SS.getRange().getBegin(); |
| 1089 | BaseExpr = BuildCXXThisExpr(Loc, BaseExprType, true); |
| 1090 | } |
| 1091 | |
| 1092 | |
| 1093 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 1094 | return ExprError(); |
| 1095 | |
| 1096 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 1097 | return BuildFieldReferenceExpr(BaseExpr, IsArrow, OpLoc, SS, FD, FoundDecl, |
| 1098 | MemberNameInfo); |
| 1099 | |
| 1100 | if (MSPropertyDecl *PD = dyn_cast<MSPropertyDecl>(MemberDecl)) |
| 1101 | return BuildMSPropertyRefExpr(*this, BaseExpr, IsArrow, SS, PD, |
| 1102 | MemberNameInfo); |
| 1103 | |
| 1104 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 1105 | |
| 1106 | |
| 1107 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
| 1108 | FoundDecl, BaseExpr, |
| 1109 | OpLoc); |
| 1110 | |
| 1111 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 1112 | return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, Var, |
| 1113 | FoundDecl, false, |
| 1114 | MemberNameInfo, Var->getType().getNonReferenceType(), |
| 1115 | VK_LValue, OK_Ordinary); |
| 1116 | } |
| 1117 | |
| 1118 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 1119 | ExprValueKind valueKind; |
| 1120 | QualType type; |
| 1121 | if (MemberFn->isInstance()) { |
| 1122 | valueKind = VK_PRValue; |
| 1123 | type = Context.BoundMemberTy; |
| 1124 | } else { |
| 1125 | valueKind = VK_LValue; |
| 1126 | type = MemberFn->getType(); |
| 1127 | } |
| 1128 | |
| 1129 | return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, |
| 1130 | MemberFn, FoundDecl, false, |
| 1131 | MemberNameInfo, type, valueKind, OK_Ordinary); |
| 1132 | } |
| 1133 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
| 1134 | |
| 1135 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 1136 | return BuildMemberExpr(BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, Enum, |
| 1137 | FoundDecl, false, |
| 1138 | MemberNameInfo, Enum->getType(), VK_PRValue, |
| 1139 | OK_Ordinary); |
| 1140 | } |
| 1141 | |
| 1142 | if (VarTemplateDecl *VarTempl = dyn_cast<VarTemplateDecl>(MemberDecl)) { |
| 1143 | if (!TemplateArgs) { |
| 1144 | diagnoseMissingTemplateArguments(TemplateName(VarTempl), MemberLoc); |
| 1145 | return ExprError(); |
| 1146 | } |
| 1147 | |
| 1148 | DeclResult VDecl = CheckVarTemplateId(VarTempl, TemplateKWLoc, |
| 1149 | MemberNameInfo.getLoc(), *TemplateArgs); |
| 1150 | if (VDecl.isInvalid()) |
| 1151 | return ExprError(); |
| 1152 | |
| 1153 | |
| 1154 | if (!VDecl.get()) |
| 1155 | return ActOnDependentMemberExpr( |
| 1156 | BaseExpr, BaseExpr->getType(), IsArrow, OpLoc, SS, TemplateKWLoc, |
| 1157 | FirstQualifierInScope, MemberNameInfo, TemplateArgs); |
| 1158 | |
| 1159 | VarDecl *Var = cast<VarDecl>(VDecl.get()); |
| 1160 | if (!Var->getTemplateSpecializationKind()) |
| 1161 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, MemberLoc); |
| 1162 | |
| 1163 | return BuildMemberExpr( |
| 1164 | BaseExpr, IsArrow, OpLoc, &SS, TemplateKWLoc, Var, FoundDecl, |
| 1165 | false, MemberNameInfo, |
| 1166 | Var->getType().getNonReferenceType(), VK_LValue, OK_Ordinary); |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | if (isa<TypeDecl>(MemberDecl)) |
| 1171 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
| 1172 | << MemberName << BaseType << int(IsArrow); |
| 1173 | else |
| 1174 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 1175 | << MemberName << BaseType << int(IsArrow); |
| 1176 | |
| 1177 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 1178 | << MemberName; |
| 1179 | R.suppressDiagnostics(); |
| 1180 | return ExprError(); |
| 1181 | } |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) { |
| 1190 | const ObjCObjectPointerType *opty |
| 1191 | = base.get()->getType()->getAs<ObjCObjectPointerType>(); |
| 1192 | if (!opty) return false; |
| 1193 | |
| 1194 | const ObjCObjectType *ty = opty->getObjectType(); |
| 1195 | |
| 1196 | QualType redef; |
| 1197 | if (ty->isObjCId()) { |
| 1198 | redef = S.Context.getObjCIdRedefinitionType(); |
| 1199 | } else if (ty->isObjCClass()) { |
| 1200 | redef = S.Context.getObjCClassRedefinitionType(); |
| 1201 | } else { |
| 1202 | return false; |
| 1203 | } |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 1208 | if (opty && !opty->getObjectType()->getInterface()) |
| 1209 | return false; |
| 1210 | |
| 1211 | base = S.ImpCastExprToType(base.get(), redef, CK_BitCast); |
| 1212 | return true; |
| 1213 | } |
| 1214 | |
| 1215 | static bool isRecordType(QualType T) { |
| 1216 | return T->isRecordType(); |
| 1217 | } |
| 1218 | static bool isPointerToRecordType(QualType T) { |
| 1219 | if (const PointerType *PT = T->getAs<PointerType>()) |
| 1220 | return PT->getPointeeType()->isRecordType(); |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | |
| 1225 | ExprResult |
| 1226 | Sema::PerformMemberExprBaseConversion(Expr *Base, bool IsArrow) { |
| 1227 | if (IsArrow && !Base->getType()->isFunctionType()) |
| 1228 | return DefaultFunctionArrayLvalueConversion(Base); |
| 1229 | |
| 1230 | return CheckPlaceholderExpr(Base); |
| 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | static ExprResult LookupMemberExpr(Sema &S, LookupResult &R, |
| 1244 | ExprResult &BaseExpr, bool &IsArrow, |
| 1245 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 1246 | Decl *ObjCImpDecl, bool HasTemplateArgs, |
| 1247 | SourceLocation TemplateKWLoc) { |
| 1248 | assert(BaseExpr.get() && "no base expression"); |
| 1249 | |
| 1250 | |
| 1251 | BaseExpr = S.PerformMemberExprBaseConversion(BaseExpr.get(), IsArrow); |
| 1252 | if (BaseExpr.isInvalid()) |
| 1 | Assuming the condition is false | |
|
| |
| 1253 | return ExprError(); |
| 1254 | |
| 1255 | QualType BaseType = BaseExpr.get()->getType(); |
| 1256 | assert(!BaseType->isDependentType()); |
| 1257 | |
| 1258 | DeclarationName MemberName = R.getLookupName(); |
| 1259 | SourceLocation MemberLoc = R.getNameLoc(); |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | if (IsArrow) { |
| |
| 1266 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 1267 | BaseType = Ptr->getPointeeType(); |
| 1268 | else if (const ObjCObjectPointerType *Ptr |
| 1269 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 1270 | BaseType = Ptr->getPointeeType(); |
| 1271 | else if (BaseType->isRecordType()) { |
| 1272 | |
| 1273 | |
| 1274 | |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | if (!S.getLangOpts().CPlusPlus) { |
| 1280 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 1281 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
| 1282 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 1283 | } |
| 1284 | IsArrow = false; |
| 1285 | } else if (BaseType->isFunctionType()) { |
| 1286 | goto fail; |
| 1287 | } else { |
| 1288 | S.Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 1289 | << BaseType << BaseExpr.get()->getSourceRange(); |
| 1290 | return ExprError(); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | |
| 1295 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
| 4 | | Assuming the object is not a 'RecordType' | |
|
| |
| 1296 | TypoExpr *TE = nullptr; |
| 1297 | if (LookupMemberExprInRecord(S, R, BaseExpr.get(), RTy, OpLoc, IsArrow, SS, |
| 1298 | HasTemplateArgs, TemplateKWLoc, TE)) |
| 1299 | return ExprError(); |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
| 1304 | |
| 1305 | return ExprResult(TE); |
| 1306 | } |
| 1307 | |
| 1308 | |
| 1309 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
| 6 | | Assuming the object is not a 'ObjCObjectType' | |
|
| |
| 1310 | if (!SS.isEmpty() && !SS.isInvalid()) { |
| 1311 | S.Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access) |
| 1312 | << 1 << SS.getScopeRep() |
| 1313 | << FixItHint::CreateRemoval(SS.getRange()); |
| 1314 | SS.clear(); |
| 1315 | } |
| 1316 | |
| 1317 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 1324 | if (!IDecl) { |
| 1325 | if (S.getLangOpts().ObjCAutoRefCount && |
| 1326 | (OTy->isObjCId() || OTy->isObjCClass())) |
| 1327 | goto fail; |
| 1328 | |
| 1329 | |
| 1330 | |
| 1331 | if (OTy->isObjCId() && Member->isStr("isa")) |
| 1332 | return new (S.Context) ObjCIsaExpr(BaseExpr.get(), IsArrow, MemberLoc, |
| 1333 | OpLoc, S.Context.getObjCClassType()); |
| 1334 | if (ShouldTryAgainWithRedefinitionType(S, BaseExpr)) |
| 1335 | return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS, |
| 1336 | ObjCImpDecl, HasTemplateArgs, TemplateKWLoc); |
| 1337 | goto fail; |
| 1338 | } |
| 1339 | |
| 1340 | if (S.RequireCompleteType(OpLoc, BaseType, |
| 1341 | diag::err_typecheck_incomplete_tag, |
| 1342 | BaseExpr.get())) |
| 1343 | return ExprError(); |
| 1344 | |
| 1345 | ObjCInterfaceDecl *ClassDeclared = nullptr; |
| 1346 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 1347 | |
| 1348 | if (!IV) { |
| 1349 | |
| 1350 | DeclFilterCCC<ObjCIvarDecl> Validator{}; |
| 1351 | Validator.IsObjCIvarLookup = IsArrow; |
| 1352 | if (TypoCorrection Corrected = S.CorrectTypo( |
| 1353 | R.getLookupNameInfo(), Sema::LookupMemberName, nullptr, nullptr, |
| 1354 | Validator, Sema::CTK_ErrorRecovery, IDecl)) { |
| 1355 | IV = Corrected.getCorrectionDeclAs<ObjCIvarDecl>(); |
| 1356 | S.diagnoseTypo( |
| 1357 | Corrected, |
| 1358 | S.PDiag(diag::err_typecheck_member_reference_ivar_suggest) |
| 1359 | << IDecl->getDeclName() << MemberName); |
| 1360 | |
| 1361 | |
| 1362 | assert(!ClassDeclared); |
| 1363 | |
| 1364 | Decl *D = cast<Decl>(IV->getDeclContext()); |
| 1365 | if (auto *Category = dyn_cast<ObjCCategoryDecl>(D)) |
| 1366 | D = Category->getClassInterface(); |
| 1367 | |
| 1368 | if (auto *Implementation = dyn_cast<ObjCImplementationDecl>(D)) |
| 1369 | ClassDeclared = Implementation->getClassInterface(); |
| 1370 | else if (auto *Interface = dyn_cast<ObjCInterfaceDecl>(D)) |
| 1371 | ClassDeclared = Interface; |
| 1372 | |
| 1373 | assert(ClassDeclared && "cannot query interface"); |
| 1374 | } else { |
| 1375 | if (IsArrow && |
| 1376 | IDecl->FindPropertyDeclaration( |
| 1377 | Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
| 1378 | S.Diag(MemberLoc, diag::err_property_found_suggest) |
| 1379 | << Member << BaseExpr.get()->getType() |
| 1380 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 1381 | return ExprError(); |
| 1382 | } |
| 1383 | |
| 1384 | S.Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 1385 | << IDecl->getDeclName() << MemberName |
| 1386 | << BaseExpr.get()->getSourceRange(); |
| 1387 | return ExprError(); |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | assert(ClassDeclared); |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | if (IV->isInvalidDecl()) |
| 1397 | return ExprError(); |
| 1398 | |
| 1399 | |
| 1400 | if (S.DiagnoseUseOfDecl(IV, MemberLoc)) |
| 1401 | return ExprError(); |
| 1402 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 1403 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 1404 | ObjCInterfaceDecl *ClassOfMethodDecl = nullptr; |
| 1405 | if (ObjCMethodDecl *MD = S.getCurMethodDecl()) |
| 1406 | ClassOfMethodDecl = MD->getClassInterface(); |
| 1407 | else if (ObjCImpDecl && S.getCurFunctionDecl()) { |
| 1408 | |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | if (ObjCImplementationDecl *IMPD = |
| 1415 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 1416 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 1417 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 1418 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 1419 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 1420 | } |
| 1421 | if (!S.getLangOpts().DebuggerSupport) { |
| 1422 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 1423 | if (!declaresSameEntity(ClassDeclared, IDecl) || |
| 1424 | !declaresSameEntity(ClassOfMethodDecl, ClassDeclared)) |
| 1425 | S.Diag(MemberLoc, diag::err_private_ivar_access) |
| 1426 | << IV->getDeclName(); |
| 1427 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 1428 | |
| 1429 | S.Diag(MemberLoc, diag::err_protected_ivar_access) |
| 1430 | << IV->getDeclName(); |
| 1431 | } |
| 1432 | } |
| 1433 | bool warn = true; |
| 1434 | if (S.getLangOpts().ObjCWeak) { |
| 1435 | Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts(); |
| 1436 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp)) |
| 1437 | if (UO->getOpcode() == UO_Deref) |
| 1438 | BaseExp = UO->getSubExpr()->IgnoreParenCasts(); |
| 1439 | |
| 1440 | if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp)) |
| 1441 | if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 1442 | S.Diag(DE->getLocation(), diag::err_arc_weak_ivar_access); |
| 1443 | warn = false; |
| 1444 | } |
| 1445 | } |
| 1446 | if (warn) { |
| 1447 | if (ObjCMethodDecl *MD = S.getCurMethodDecl()) { |
| 1448 | ObjCMethodFamily MF = MD->getMethodFamily(); |
| 1449 | warn = (MF != OMF_init && MF != OMF_dealloc && |
| 1450 | MF != OMF_finalize && |
| 1451 | !S.IvarBacksCurrentMethodAccessor(IDecl, MD, IV)); |
| 1452 | } |
| 1453 | if (warn) |
| 1454 | S.Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName(); |
| 1455 | } |
| 1456 | |
| 1457 | ObjCIvarRefExpr *Result = new (S.Context) ObjCIvarRefExpr( |
| 1458 | IV, IV->getUsageType(BaseType), MemberLoc, OpLoc, BaseExpr.get(), |
| 1459 | IsArrow); |
| 1460 | |
| 1461 | if (IV->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 1462 | if (!S.isUnevaluatedContext() && |
| 1463 | !S.Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, MemberLoc)) |
| 1464 | S.getCurFunction()->recordUseOfWeak(Result); |
| 1465 | } |
| 1466 | |
| 1467 | return Result; |
| 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | const ObjCObjectPointerType *OPT; |
| 1472 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 8 | | Assuming the object is not a 'ObjCObjectPointerType' | |
|
| |
| |
| 1473 | if (!SS.isEmpty() && !SS.isInvalid()) { |
| 1474 | S.Diag(SS.getRange().getBegin(), diag::err_qualified_objc_access) |
| 1475 | << 0 << SS.getScopeRep() << FixItHint::CreateRemoval(SS.getRange()); |
| 1476 | SS.clear(); |
| 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | BaseExpr = S.DefaultLvalueConversion(BaseExpr.get()); |
| 1481 | if (BaseExpr.isInvalid()) |
| 1482 | return ExprError(); |
| 1483 | |
| 1484 | assert(S.Context.hasSameUnqualifiedType(BaseType, |
| 1485 | BaseExpr.get()->getType())); |
| 1486 | |
| 1487 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 1488 | |
| 1489 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 1490 | |
| 1491 | |
| 1492 | if (OT->isObjCId()) { |
| 1493 | |
| 1494 | Selector Sel = S.PP.getSelectorTable().getNullarySelector(Member); |
| 1495 | if (Decl *PMDecl = |
| 1496 | FindGetterSetterNameDecl(OPT, Member, Sel, S.Context)) { |
| 1497 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 1498 | |
| 1499 | if (S.DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1500 | return ExprError(); |
| 1501 | |
| 1502 | return new (S.Context) |
| 1503 | ObjCPropertyRefExpr(PD, S.Context.PseudoObjectTy, VK_LValue, |
| 1504 | OK_ObjCProperty, MemberLoc, BaseExpr.get()); |
| 1505 | } |
| 1506 | |
| 1507 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 1508 | Selector SetterSel = |
| 1509 | SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(), |
| 1510 | S.PP.getSelectorTable(), |
| 1511 | Member); |
| 1512 | ObjCMethodDecl *SMD = nullptr; |
| 1513 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, |
| 1514 | nullptr, |
| 1515 | SetterSel, S.Context)) |
| 1516 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
| 1517 | |
| 1518 | return new (S.Context) |
| 1519 | ObjCPropertyRefExpr(OMD, SMD, S.Context.PseudoObjectTy, VK_LValue, |
| 1520 | OK_ObjCProperty, MemberLoc, BaseExpr.get()); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | |
| 1525 | if (IsArrow && ShouldTryAgainWithRedefinitionType(S, BaseExpr)) |
| 1526 | return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS, |
| 1527 | ObjCImpDecl, HasTemplateArgs, TemplateKWLoc); |
| 1528 | |
| 1529 | return ExprError(S.Diag(MemberLoc, diag::err_property_not_found) |
| 1530 | << MemberName << BaseType); |
| 1531 | } |
| 1532 | |
| 1533 | |
| 1534 | if (OT->isObjCClass()) { |
| 1535 | |
| 1536 | ObjCMethodDecl *MD = S.getCurMethodDecl(); |
| 1537 | if (!MD) { |
| 1538 | if (ShouldTryAgainWithRedefinitionType(S, BaseExpr)) |
| 1539 | return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS, |
| 1540 | ObjCImpDecl, HasTemplateArgs, TemplateKWLoc); |
| 1541 | |
| 1542 | goto fail; |
| 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | Selector Sel = S.PP.getSelectorTable().getNullarySelector(Member); |
| 1547 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 1548 | if (!IFace) |
| 1549 | goto fail; |
| 1550 | |
| 1551 | ObjCMethodDecl *Getter; |
| 1552 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 1553 | |
| 1554 | if (S.DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1555 | return ExprError(); |
| 1556 | } else |
| 1557 | Getter = IFace->lookupPrivateMethod(Sel, false); |
| 1558 | |
| 1559 | |
| 1560 | Selector SetterSel = |
| 1561 | SelectorTable::constructSetterSelector(S.PP.getIdentifierTable(), |
| 1562 | S.PP.getSelectorTable(), |
| 1563 | Member); |
| 1564 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 1565 | if (!Setter) { |
| 1566 | |
| 1567 | |
| 1568 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
| 1569 | } |
| 1570 | |
| 1571 | if (Setter && S.DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1572 | return ExprError(); |
| 1573 | |
| 1574 | if (Getter || Setter) { |
| 1575 | return new (S.Context) ObjCPropertyRefExpr( |
| 1576 | Getter, Setter, S.Context.PseudoObjectTy, VK_LValue, |
| 1577 | OK_ObjCProperty, MemberLoc, BaseExpr.get()); |
| 1578 | } |
| 1579 | |
| 1580 | if (ShouldTryAgainWithRedefinitionType(S, BaseExpr)) |
| 1581 | return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS, |
| 1582 | ObjCImpDecl, HasTemplateArgs, TemplateKWLoc); |
| 1583 | |
| 1584 | return ExprError(S.Diag(MemberLoc, diag::err_property_not_found) |
| 1585 | << MemberName << BaseType); |
| 1586 | } |
| 1587 | |
| 1588 | |
| 1589 | return S.HandleExprPropertyRefExpr(OPT, BaseExpr.get(), OpLoc, MemberName, |
| 1590 | MemberLoc, SourceLocation(), QualType(), |
| 1591 | false); |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | if (BaseType->isExtVectorType()) { |
| |
| 1596 | |
| 1597 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 1598 | ExprValueKind VK; |
| 1599 | if (IsArrow) |
| |
| 1600 | VK = VK_LValue; |
| 1601 | else { |
| 1602 | if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(BaseExpr.get())) |
| 13 | | Assuming the object is a 'PseudoObjectExpr' | |
|
| |
| 1603 | VK = POE->getSyntacticForm()->getValueKind(); |
| 1604 | else |
| 1605 | VK = BaseExpr.get()->getValueKind(); |
| 1606 | } |
| 1607 | |
| 1608 | QualType ret = CheckExtVectorComponent(S, BaseType, VK, OpLoc, |
| 15 | | Calling 'CheckExtVectorComponent' | |
|
| 1609 | Member, MemberLoc); |
| 1610 | if (ret.isNull()) |
| 1611 | return ExprError(); |
| 1612 | Qualifiers BaseQ = |
| 1613 | S.Context.getCanonicalType(BaseExpr.get()->getType()).getQualifiers(); |
| 1614 | ret = S.Context.getQualifiedType(ret, BaseQ); |
| 1615 | |
| 1616 | return new (S.Context) |
| 1617 | ExtVectorElementExpr(ret, VK, BaseExpr.get(), *Member, MemberLoc); |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | if (IsArrow && BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 1623 | !S.Context.getObjCSelRedefinitionType()->isObjCSelType()) { |
| 1624 | BaseExpr = S.ImpCastExprToType( |
| 1625 | BaseExpr.get(), S.Context.getObjCSelRedefinitionType(), CK_BitCast); |
| 1626 | return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS, |
| 1627 | ObjCImpDecl, HasTemplateArgs, TemplateKWLoc); |
| 1628 | } |
| 1629 | |
| 1630 | |
| 1631 | fail: |
| 1632 | |
| 1633 | |
| 1634 | |
| 1635 | |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | |
| 1640 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 1641 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 1642 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| 1643 | S.Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 1644 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
| 1645 | << FixItHint::CreateReplacement(OpLoc, "->"); |
| 1646 | |
| 1647 | |
| 1648 | IsArrow = true; |
| 1649 | return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS, |
| 1650 | ObjCImpDecl, HasTemplateArgs, TemplateKWLoc); |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | |
| 1655 | |
| 1656 | if (S.tryToRecoverWithCall( |
| 1657 | BaseExpr, S.PDiag(diag::err_member_reference_needs_call), |
| 1658 | false, |
| 1659 | IsArrow ? &isPointerToRecordType : &isRecordType)) { |
| 1660 | if (BaseExpr.isInvalid()) |
| 1661 | return ExprError(); |
| 1662 | BaseExpr = S.DefaultFunctionArrayConversion(BaseExpr.get()); |
| 1663 | return LookupMemberExpr(S, R, BaseExpr, IsArrow, OpLoc, SS, |
| 1664 | ObjCImpDecl, HasTemplateArgs, TemplateKWLoc); |
| 1665 | } |
| 1666 | |
| 1667 | S.Diag(OpLoc, diag::err_typecheck_member_reference_struct_union) |
| 1668 | << BaseType << BaseExpr.get()->getSourceRange() << MemberLoc; |
| 1669 | |
| 1670 | return ExprError(); |
| 1671 | } |
| 1672 | |
| 1673 | |
| 1674 | |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 | |
| 1679 | |
| 1680 | |
| 1681 | |
| 1682 | |
| 1683 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
| 1684 | SourceLocation OpLoc, |
| 1685 | tok::TokenKind OpKind, |
| 1686 | CXXScopeSpec &SS, |
| 1687 | SourceLocation TemplateKWLoc, |
| 1688 | UnqualifiedId &Id, |
| 1689 | Decl *ObjCImpDecl) { |
| 1690 | if (SS.isSet() && SS.isInvalid()) |
| 1691 | return ExprError(); |
| 1692 | |
| 1693 | |
| 1694 | if (getLangOpts().MicrosoftExt && |
| 1695 | Id.getKind() == UnqualifiedIdKind::IK_ConstructorName) |
| 1696 | Diag(Id.getSourceRange().getBegin(), |
| 1697 | diag::ext_ms_explicit_constructor_call); |
| 1698 | |
| 1699 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 1700 | |
| 1701 | |
| 1702 | DeclarationNameInfo NameInfo; |
| 1703 | const TemplateArgumentListInfo *TemplateArgs; |
| 1704 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, |
| 1705 | NameInfo, TemplateArgs); |
| 1706 | |
| 1707 | DeclarationName Name = NameInfo.getName(); |
| 1708 | bool IsArrow = (OpKind == tok::arrow); |
| 1709 | |
| 1710 | NamedDecl *FirstQualifierInScope |
| 1711 | = (!SS.isSet() ? nullptr : FindFirstQualifierInScope(S, SS.getScopeRep())); |
| 1712 | |
| 1713 | |
| 1714 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
| 1715 | if (Result.isInvalid()) return ExprError(); |
| 1716 | Base = Result.get(); |
| 1717 | |
| 1718 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 1719 | isDependentScopeSpecifier(SS)) { |
| 1720 | return ActOnDependentMemberExpr(Base, Base->getType(), IsArrow, OpLoc, SS, |
| 1721 | TemplateKWLoc, FirstQualifierInScope, |
| 1722 | NameInfo, TemplateArgs); |
| 1723 | } |
| 1724 | |
| 1725 | ActOnMemberAccessExtraArgs ExtraArgs = {S, Id, ObjCImpDecl}; |
| 1726 | ExprResult Res = BuildMemberReferenceExpr( |
| 1727 | Base, Base->getType(), OpLoc, IsArrow, SS, TemplateKWLoc, |
| 1728 | FirstQualifierInScope, NameInfo, TemplateArgs, S, &ExtraArgs); |
| 1729 | |
| 1730 | if (!Res.isInvalid() && isa<MemberExpr>(Res.get())) |
| 1731 | CheckMemberAccessOfNoDeref(cast<MemberExpr>(Res.get())); |
| 1732 | |
| 1733 | return Res; |
| 1734 | } |
| 1735 | |
| 1736 | void Sema::CheckMemberAccessOfNoDeref(const MemberExpr *E) { |
| 1737 | if (isUnevaluatedContext()) |
| 1738 | return; |
| 1739 | |
| 1740 | QualType ResultTy = E->getType(); |
| 1741 | |
| 1742 | |
| 1743 | |
| 1744 | |
| 1745 | |
| 1746 | |
| 1747 | |
| 1748 | if (ResultTy->isArrayType()) { |
| 1749 | if (!E->isArrow()) { |
| 1750 | |
| 1751 | |
| 1752 | |
| 1753 | |
| 1754 | |
| 1755 | |
| 1756 | CheckAddressOfNoDeref(E->getBase()); |
| 1757 | } |
| 1758 | } else if (E->isArrow()) { |
| 1759 | if (const auto *Ptr = dyn_cast<PointerType>( |
| 1760 | E->getBase()->getType().getDesugaredType(Context))) { |
| 1761 | if (Ptr->getPointeeType()->hasAttr(attr::NoDeref)) |
| 1762 | ExprEvalContexts.back().PossibleDerefs.insert(E); |
| 1763 | } |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | ExprResult |
| 1768 | Sema::BuildFieldReferenceExpr(Expr *BaseExpr, bool IsArrow, |
| 1769 | SourceLocation OpLoc, const CXXScopeSpec &SS, |
| 1770 | FieldDecl *Field, DeclAccessPair FoundDecl, |
| 1771 | const DeclarationNameInfo &MemberNameInfo) { |
| 1772 | |
| 1773 | |
| 1774 | |
| 1775 | |
| 1776 | ExprValueKind VK = VK_LValue; |
| 1777 | ExprObjectKind OK = OK_Ordinary; |
| 1778 | if (!IsArrow) { |
| 1779 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 1780 | VK = BaseExpr->getValueKind(); |
| 1781 | else |
| 1782 | VK = VK_PRValue; |
| 1783 | } |
| 1784 | if (VK != VK_PRValue && Field->isBitField()) |
| 1785 | OK = OK_BitField; |
| 1786 | |
| 1787 | |
| 1788 | QualType MemberType = Field->getType(); |
| 1789 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 1790 | MemberType = Ref->getPointeeType(); |
| 1791 | VK = VK_LValue; |
| 1792 | } else { |
| 1793 | QualType BaseType = BaseExpr->getType(); |
| 1794 | if (IsArrow) BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
| 1795 | |
| 1796 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 1797 | |
| 1798 | |
| 1799 | BaseQuals.removeObjCGCAttr(); |
| 1800 | |
| 1801 | |
| 1802 | |
| 1803 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 1804 | |
| 1805 | Qualifiers MemberQuals = |
| 1806 | Context.getCanonicalType(MemberType).getQualifiers(); |
| 1807 | |
| 1808 | assert(!MemberQuals.hasAddressSpace()); |
| 1809 | |
| 1810 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 1811 | if (Combined != MemberQuals) |
| 1812 | MemberType = Context.getQualifiedType(MemberType, Combined); |
| 1813 | |
| 1814 | |
| 1815 | |
| 1816 | |
| 1817 | |
| 1818 | if (BaseType->hasAttr(attr::NoDeref)) |
| 1819 | MemberType = |
| 1820 | Context.getAttributedType(attr::NoDeref, MemberType, MemberType); |
| 1821 | } |
| 1822 | |
| 1823 | auto *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1824 | if (!(CurMethod && CurMethod->isDefaulted())) |
| 1825 | UnusedPrivateFields.remove(Field); |
| 1826 | |
| 1827 | ExprResult Base = PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 1828 | FoundDecl, Field); |
| 1829 | if (Base.isInvalid()) |
| 1830 | return ExprError(); |
| 1831 | |
| 1832 | |
| 1833 | |
| 1834 | if (getLangOpts().OpenMP && IsArrow && |
| 1835 | !CurContext->isDependentContext() && |
| 1836 | isa<CXXThisExpr>(Base.get()->IgnoreParenImpCasts())) { |
| 1837 | if (auto *PrivateCopy = isOpenMPCapturedDecl(Field)) { |
| 1838 | return getOpenMPCapturedExpr(PrivateCopy, VK, OK, |
| 1839 | MemberNameInfo.getLoc()); |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | return BuildMemberExpr(Base.get(), IsArrow, OpLoc, &SS, |
| 1844 | SourceLocation(), Field, FoundDecl, |
| 1845 | false, MemberNameInfo, |
| 1846 | MemberType, VK, OK); |
| 1847 | } |
| 1848 | |
| 1849 | |
| 1850 | |
| 1851 | |
| 1852 | |
| 1853 | ExprResult |
| 1854 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1855 | SourceLocation TemplateKWLoc, |
| 1856 | LookupResult &R, |
| 1857 | const TemplateArgumentListInfo *TemplateArgs, |
| 1858 | bool IsKnownInstance, const Scope *S) { |
| 1859 | assert(!R.empty() && !R.isAmbiguous()); |
| 1860 | |
| 1861 | SourceLocation loc = R.getNameLoc(); |
| 1862 | |
| 1863 | |
| 1864 | |
| 1865 | QualType ThisTy = getCurrentThisType(); |
| 1866 | assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'"); |
| 1867 | |
| 1868 | Expr *baseExpr = nullptr; |
| 1869 | if (IsKnownInstance) { |
| 1870 | SourceLocation Loc = R.getNameLoc(); |
| 1871 | if (SS.getRange().isValid()) |
| 1872 | Loc = SS.getRange().getBegin(); |
| 1873 | baseExpr = BuildCXXThisExpr(loc, ThisTy, true); |
| 1874 | } |
| 1875 | |
| 1876 | return BuildMemberReferenceExpr(baseExpr, ThisTy, |
| 1877 | SourceLocation(), |
| 1878 | true, |
| 1879 | SS, TemplateKWLoc, |
| 1880 | nullptr, |
| 1881 | R, TemplateArgs, S); |
| 1882 | } |