| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "TypeLocBuilder.h" |
| 14 | #include "clang/AST/ASTConsumer.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/ASTMutationListener.h" |
| 17 | #include "clang/AST/DeclObjC.h" |
| 18 | #include "clang/AST/Expr.h" |
| 19 | #include "clang/AST/ExprObjC.h" |
| 20 | #include "clang/AST/RecursiveASTVisitor.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "clang/Sema/DeclSpec.h" |
| 23 | #include "clang/Sema/Lookup.h" |
| 24 | #include "clang/Sema/Scope.h" |
| 25 | #include "clang/Sema/ScopeInfo.h" |
| 26 | #include "clang/Sema/SemaInternal.h" |
| 27 | #include "llvm/ADT/DenseMap.h" |
| 28 | #include "llvm/ADT/DenseSet.h" |
| 29 | |
| 30 | using namespace clang; |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | bool Sema::checkInitMethod(ObjCMethodDecl *method, |
| 42 | QualType receiverTypeIfCall) { |
| 43 | if (method->isInvalidDecl()) return true; |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | const ObjCObjectType *result = |
| 52 | method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType(); |
| 53 | |
| 54 | if (result->isObjCId()) { |
| 55 | return false; |
| 56 | } else if (result->isObjCClass()) { |
| 57 | |
| 58 | } else { |
| 59 | ObjCInterfaceDecl *resultClass = result->getInterface(); |
| 60 | (0) . __assert_fail ("resultClass && \"unexpected object type!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 60, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(resultClass && "unexpected object type!"); |
| 61 | |
| 62 | |
| 63 | |
| 64 | if (!resultClass->hasDefinition()) { |
| 65 | if (receiverTypeIfCall.isNull() && |
| 66 | !isa<ObjCImplementationDecl>(method->getDeclContext())) |
| 67 | return false; |
| 68 | |
| 69 | |
| 70 | } else { |
| 71 | |
| 72 | |
| 73 | const ObjCInterfaceDecl *receiverClass = nullptr; |
| 74 | if (isa<ObjCProtocolDecl>(method->getDeclContext())) { |
| 75 | if (receiverTypeIfCall.isNull()) |
| 76 | return false; |
| 77 | |
| 78 | receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>() |
| 79 | ->getInterfaceDecl(); |
| 80 | |
| 81 | |
| 82 | if (!receiverClass) return false; |
| 83 | } else { |
| 84 | receiverClass = method->getClassInterface(); |
| 85 | (0) . __assert_fail ("receiverClass && \"method not associated with a class!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 85, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(receiverClass && "method not associated with a class!"); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | if (receiverClass->isSuperClassOf(resultClass) || |
| 90 | resultClass->isSuperClassOf(receiverClass)) |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | SourceLocation loc = method->getLocation(); |
| 96 | |
| 97 | |
| 98 | |
| 99 | if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) { |
| 100 | method->addAttr(UnavailableAttr::CreateImplicit(Context, "", |
| 101 | UnavailableAttr::IR_ARCInitReturnsUnrelated, loc)); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | Diag(loc, diag::err_arc_init_method_unrelated_result_type); |
| 107 | method->setInvalidDecl(); |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | static bool diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
| 114 | Sema &S) { |
| 115 | if (OldD->hasAttr<NoEscapeAttr>() && !NewD->hasAttr<NoEscapeAttr>()) { |
| 116 | S.Diag(NewD->getLocation(), diag::warn_overriding_method_missing_noescape); |
| 117 | S.Diag(OldD->getLocation(), diag::note_overridden_marked_noescape); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | static void diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
| 127 | const ObjCCategoryDecl *CD, |
| 128 | const ObjCProtocolDecl *PD, Sema &S) { |
| 129 | if (!diagnoseNoescape(NewD, OldD, S)) |
| 130 | S.Diag(CD->getLocation(), diag::note_cat_conform_to_noescape_prot) |
| 131 | << CD->IsClassExtension() << PD |
| 132 | << cast<ObjCMethodDecl>(NewD->getDeclContext()); |
| 133 | } |
| 134 | |
| 135 | void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, |
| 136 | const ObjCMethodDecl *Overridden) { |
| 137 | if (Overridden->hasRelatedResultType() && |
| 138 | !NewMethod->hasRelatedResultType()) { |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | QualType ResultType = NewMethod->getReturnType(); |
| 144 | SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange(); |
| 145 | |
| 146 | |
| 147 | ObjCInterfaceDecl *CurrentClass |
| 148 | = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext()); |
| 149 | if (!CurrentClass) { |
| 150 | DeclContext *DC = NewMethod->getDeclContext(); |
| 151 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC)) |
| 152 | CurrentClass = Cat->getClassInterface(); |
| 153 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC)) |
| 154 | CurrentClass = Impl->getClassInterface(); |
| 155 | else if (ObjCCategoryImplDecl *CatImpl |
| 156 | = dyn_cast<ObjCCategoryImplDecl>(DC)) |
| 157 | CurrentClass = CatImpl->getClassInterface(); |
| 158 | } |
| 159 | |
| 160 | if (CurrentClass) { |
| 161 | Diag(NewMethod->getLocation(), |
| 162 | diag::warn_related_result_type_compatibility_class) |
| 163 | << Context.getObjCInterfaceType(CurrentClass) |
| 164 | << ResultType |
| 165 | << ResultTypeRange; |
| 166 | } else { |
| 167 | Diag(NewMethod->getLocation(), |
| 168 | diag::warn_related_result_type_compatibility_protocol) |
| 169 | << ResultType |
| 170 | << ResultTypeRange; |
| 171 | } |
| 172 | |
| 173 | if (ObjCMethodFamily Family = Overridden->getMethodFamily()) |
| 174 | Diag(Overridden->getLocation(), |
| 175 | diag::note_related_result_type_family) |
| 176 | << 0 |
| 177 | << Family; |
| 178 | else |
| 179 | Diag(Overridden->getLocation(), |
| 180 | diag::note_related_result_type_overridden); |
| 181 | } |
| 182 | |
| 183 | if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() != |
| 184 | Overridden->hasAttr<NSReturnsRetainedAttr>())) { |
| 185 | Diag(NewMethod->getLocation(), |
| 186 | getLangOpts().ObjCAutoRefCount |
| 187 | ? diag::err_nsreturns_retained_attribute_mismatch |
| 188 | : diag::warn_nsreturns_retained_attribute_mismatch) |
| 189 | << 1; |
| 190 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| 191 | } |
| 192 | if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() != |
| 193 | Overridden->hasAttr<NSReturnsNotRetainedAttr>())) { |
| 194 | Diag(NewMethod->getLocation(), |
| 195 | getLangOpts().ObjCAutoRefCount |
| 196 | ? diag::err_nsreturns_retained_attribute_mismatch |
| 197 | : diag::warn_nsreturns_retained_attribute_mismatch) |
| 198 | << 0; |
| 199 | Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| 200 | } |
| 201 | |
| 202 | ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(), |
| 203 | oe = Overridden->param_end(); |
| 204 | for (ObjCMethodDecl::param_iterator ni = NewMethod->param_begin(), |
| 205 | ne = NewMethod->param_end(); |
| 206 | ni != ne && oi != oe; ++ni, ++oi) { |
| 207 | const ParmVarDecl *oldDecl = (*oi); |
| 208 | ParmVarDecl *newDecl = (*ni); |
| 209 | if (newDecl->hasAttr<NSConsumedAttr>() != |
| 210 | oldDecl->hasAttr<NSConsumedAttr>()) { |
| 211 | Diag(newDecl->getLocation(), |
| 212 | getLangOpts().ObjCAutoRefCount |
| 213 | ? diag::err_nsconsumed_attribute_mismatch |
| 214 | : diag::warn_nsconsumed_attribute_mismatch); |
| 215 | Diag(oldDecl->getLocation(), diag::note_previous_decl) << "parameter"; |
| 216 | } |
| 217 | |
| 218 | diagnoseNoescape(newDecl, oldDecl, *this); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) { |
| 225 | ObjCMethodFamily family = method->getMethodFamily(); |
| 226 | switch (family) { |
| 227 | case OMF_None: |
| 228 | case OMF_finalize: |
| 229 | case OMF_retain: |
| 230 | case OMF_release: |
| 231 | case OMF_autorelease: |
| 232 | case OMF_retainCount: |
| 233 | case OMF_self: |
| 234 | case OMF_initialize: |
| 235 | case OMF_performSelector: |
| 236 | return false; |
| 237 | |
| 238 | case OMF_dealloc: |
| 239 | if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) { |
| 240 | SourceRange ResultTypeRange = method->getReturnTypeSourceRange(); |
| 241 | if (ResultTypeRange.isInvalid()) |
| 242 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
| 243 | << method->getReturnType() |
| 244 | << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)"); |
| 245 | else |
| 246 | Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
| 247 | << method->getReturnType() |
| 248 | << FixItHint::CreateReplacement(ResultTypeRange, "void"); |
| 249 | return true; |
| 250 | } |
| 251 | return false; |
| 252 | |
| 253 | case OMF_init: |
| 254 | |
| 255 | if (checkInitMethod(method, QualType())) |
| 256 | return true; |
| 257 | |
| 258 | method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context)); |
| 259 | |
| 260 | |
| 261 | |
| 262 | if (method->hasAttr<NSReturnsRetainedAttr>()) |
| 263 | return false; |
| 264 | break; |
| 265 | |
| 266 | case OMF_alloc: |
| 267 | case OMF_copy: |
| 268 | case OMF_mutableCopy: |
| 269 | case OMF_new: |
| 270 | if (method->hasAttr<NSReturnsRetainedAttr>() || |
| 271 | method->hasAttr<NSReturnsNotRetainedAttr>() || |
| 272 | method->hasAttr<NSReturnsAutoreleasedAttr>()) |
| 273 | return false; |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context)); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | static void DiagnoseObjCImplementedDeprecations(Sema &S, const NamedDecl *ND, |
| 282 | SourceLocation ImplLoc) { |
| 283 | if (!ND) |
| 284 | return; |
| 285 | bool IsCategory = false; |
| 286 | StringRef RealizedPlatform; |
| 287 | AvailabilityResult Availability = ND->getAvailability( |
| 288 | , VersionTuple(), |
| 289 | &RealizedPlatform); |
| 290 | if (Availability != AR_Deprecated) { |
| 291 | if (isa<ObjCMethodDecl>(ND)) { |
| 292 | if (Availability != AR_Unavailable) |
| 293 | return; |
| 294 | if (RealizedPlatform.empty()) |
| 295 | RealizedPlatform = S.Context.getTargetInfo().getPlatformName(); |
| 296 | |
| 297 | |
| 298 | if (RealizedPlatform.endswith("_app_extension")) |
| 299 | return; |
| 300 | S.Diag(ImplLoc, diag::warn_unavailable_def); |
| 301 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 302 | << ND->getDeclName(); |
| 303 | return; |
| 304 | } |
| 305 | if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND)) { |
| 306 | if (!CD->getClassInterface()->isDeprecated()) |
| 307 | return; |
| 308 | ND = CD->getClassInterface(); |
| 309 | IsCategory = true; |
| 310 | } else |
| 311 | return; |
| 312 | } |
| 313 | S.Diag(ImplLoc, diag::warn_deprecated_def) |
| 314 | << (isa<ObjCMethodDecl>(ND) |
| 315 | ? 0 |
| 316 | : isa<ObjCCategoryDecl>(ND) || IsCategory ? 2 |
| 317 | : 1); |
| 318 | if (isa<ObjCMethodDecl>(ND)) |
| 319 | S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| 320 | << ND->getDeclName(); |
| 321 | else |
| 322 | S.Diag(ND->getLocation(), diag::note_previous_decl) |
| 323 | << (isa<ObjCCategoryDecl>(ND) ? "category" : "class"); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | |
| 328 | void Sema::AddAnyMethodToGlobalPool(Decl *D) { |
| 329 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
| 330 | |
| 331 | |
| 332 | if (!MDecl) |
| 333 | return; |
| 334 | if (MDecl->isInstanceMethod()) |
| 335 | AddInstanceMethodToGlobalPool(MDecl, true); |
| 336 | else |
| 337 | AddFactoryMethodToGlobalPool(MDecl, true); |
| 338 | } |
| 339 | |
| 340 | |
| 341 | |
| 342 | static bool |
| 343 | HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) { |
| 344 | QualType T = Param->getType(); |
| 345 | |
| 346 | if (const PointerType *PT = T->getAs<PointerType>()) { |
| 347 | T = PT->getPointeeType(); |
| 348 | } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| 349 | T = RT->getPointeeType(); |
| 350 | } else { |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | |
| 356 | return !T.getLocalQualifiers().hasObjCLifetime(); |
| 357 | } |
| 358 | |
| 359 | |
| 360 | |
| 361 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
| 362 | (0) . __assert_fail ("(getCurMethodDecl() == nullptr) && \"Methodparsing confused\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 362, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((getCurMethodDecl() == nullptr) && "Methodparsing confused"); |
| 363 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
| 364 | |
| 365 | PushExpressionEvaluationContext(ExprEvalContexts.back().Context); |
| 366 | |
| 367 | |
| 368 | if (!MDecl) |
| 369 | return; |
| 370 | |
| 371 | QualType ResultType = MDecl->getReturnType(); |
| 372 | if (!ResultType->isDependentType() && !ResultType->isVoidType() && |
| 373 | !MDecl->isInvalidDecl() && |
| 374 | RequireCompleteType(MDecl->getLocation(), ResultType, |
| 375 | diag::err_func_def_incomplete_result)) |
| 376 | MDecl->setInvalidDecl(); |
| 377 | |
| 378 | |
| 379 | PushDeclContext(FnBodyScope, MDecl); |
| 380 | PushFunctionScope(); |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
| 387 | |
| 388 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 389 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
| 390 | |
| 391 | |
| 392 | CheckParmsForFunctionDef(MDecl->parameters(), |
| 393 | ); |
| 394 | |
| 395 | |
| 396 | for (auto *Param : MDecl->parameters()) { |
| 397 | if (!Param->isInvalidDecl() && |
| 398 | getLangOpts().ObjCAutoRefCount && |
| 399 | !HasExplicitOwnershipAttr(*this, Param)) |
| 400 | Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) << |
| 401 | Param->getType(); |
| 402 | |
| 403 | if (Param->getIdentifier()) |
| 404 | PushOnScopeChains(Param, FnBodyScope); |
| 405 | } |
| 406 | |
| 407 | |
| 408 | if (getLangOpts().ObjCAutoRefCount) { |
| 409 | switch (MDecl->getMethodFamily()) { |
| 410 | case OMF_retain: |
| 411 | case OMF_retainCount: |
| 412 | case OMF_release: |
| 413 | case OMF_autorelease: |
| 414 | Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def) |
| 415 | << 0 << MDecl->getSelector(); |
| 416 | break; |
| 417 | |
| 418 | case OMF_None: |
| 419 | case OMF_dealloc: |
| 420 | case OMF_finalize: |
| 421 | case OMF_alloc: |
| 422 | case OMF_init: |
| 423 | case OMF_mutableCopy: |
| 424 | case OMF_copy: |
| 425 | case OMF_new: |
| 426 | case OMF_self: |
| 427 | case OMF_initialize: |
| 428 | case OMF_performSelector: |
| 429 | break; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | |
| 434 | |
| 435 | if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { |
| 436 | ObjCMethodDecl *IMD = |
| 437 | IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()); |
| 438 | |
| 439 | if (IMD) { |
| 440 | ObjCImplDecl *ImplDeclOfMethodDef = |
| 441 | dyn_cast<ObjCImplDecl>(MDecl->getDeclContext()); |
| 442 | ObjCContainerDecl *ContDeclOfMethodDecl = |
| 443 | dyn_cast<ObjCContainerDecl>(IMD->getDeclContext()); |
| 444 | ObjCImplDecl *ImplDeclOfMethodDecl = nullptr; |
| 445 | if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl)) |
| 446 | ImplDeclOfMethodDecl = OID->getImplementation(); |
| 447 | else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) { |
| 448 | if (CD->IsClassExtension()) { |
| 449 | if (ObjCInterfaceDecl *OID = CD->getClassInterface()) |
| 450 | ImplDeclOfMethodDecl = OID->getImplementation(); |
| 451 | } else |
| 452 | ImplDeclOfMethodDecl = CD->getImplementation(); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef) |
| 457 | DiagnoseObjCImplementedDeprecations(*this, IMD, MDecl->getLocation()); |
| 458 | } |
| 459 | |
| 460 | if (MDecl->getMethodFamily() == OMF_init) { |
| 461 | if (MDecl->isDesignatedInitializerForTheInterface()) { |
| 462 | getCurFunction()->ObjCIsDesignatedInit = true; |
| 463 | getCurFunction()->ObjCWarnForNoDesignatedInitChain = |
| 464 | IC->getSuperClass() != nullptr; |
| 465 | } else if (IC->hasDesignatedInitializers()) { |
| 466 | getCurFunction()->ObjCIsSecondaryInit = true; |
| 467 | getCurFunction()->ObjCWarnForNoInitDelegation = true; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) { |
| 476 | ObjCMethodFamily Family = MDecl->getMethodFamily(); |
| 477 | if (Family == OMF_dealloc) { |
| 478 | if (!(getLangOpts().ObjCAutoRefCount || |
| 479 | getLangOpts().getGC() == LangOptions::GCOnly)) |
| 480 | getCurFunction()->ObjCShouldCallSuper = true; |
| 481 | |
| 482 | } else if (Family == OMF_finalize) { |
| 483 | if (Context.getLangOpts().getGC() != LangOptions::NonGC) |
| 484 | getCurFunction()->ObjCShouldCallSuper = true; |
| 485 | |
| 486 | } else { |
| 487 | const ObjCMethodDecl *SuperMethod = |
| 488 | SuperClass->lookupMethod(MDecl->getSelector(), |
| 489 | MDecl->isInstanceMethod()); |
| 490 | getCurFunction()->ObjCShouldCallSuper = |
| 491 | (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>()); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | namespace { |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | class ObjCInterfaceValidatorCCC final : public CorrectionCandidateCallback { |
| 503 | public: |
| 504 | ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {} |
| 505 | explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl) |
| 506 | : CurrentIDecl(IDecl) {} |
| 507 | |
| 508 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 509 | ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 510 | return ID && !declaresSameEntity(ID, CurrentIDecl); |
| 511 | } |
| 512 | |
| 513 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 514 | return llvm::make_unique<ObjCInterfaceValidatorCCC>(*this); |
| 515 | } |
| 516 | |
| 517 | private: |
| 518 | ObjCInterfaceDecl *CurrentIDecl; |
| 519 | }; |
| 520 | |
| 521 | } |
| 522 | |
| 523 | static void diagnoseUseOfProtocols(Sema &TheSema, |
| 524 | ObjCContainerDecl *CD, |
| 525 | ObjCProtocolDecl *const *ProtoRefs, |
| 526 | unsigned NumProtoRefs, |
| 527 | const SourceLocation *ProtoLocs) { |
| 528 | assert(ProtoRefs); |
| 529 | |
| 530 | Sema::ContextRAII SavedContext(TheSema, CD); |
| 531 | for (unsigned i = 0; i < NumProtoRefs; ++i) { |
| 532 | (void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i], |
| 533 | , |
| 534 | , |
| 535 | ); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | void Sema:: |
| 540 | ActOnSuperClassOfClassInterface(Scope *S, |
| 541 | SourceLocation AtInterfaceLoc, |
| 542 | ObjCInterfaceDecl *IDecl, |
| 543 | IdentifierInfo *ClassName, |
| 544 | SourceLocation ClassLoc, |
| 545 | IdentifierInfo *SuperName, |
| 546 | SourceLocation SuperLoc, |
| 547 | ArrayRef<ParsedType> SuperTypeArgs, |
| 548 | SourceRange SuperTypeArgsRange) { |
| 549 | |
| 550 | NamedDecl *PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 551 | LookupOrdinaryName); |
| 552 | |
| 553 | if (!PrevDecl) { |
| 554 | |
| 555 | |
| 556 | ObjCInterfaceValidatorCCC CCC(IDecl); |
| 557 | if (TypoCorrection Corrected = CorrectTypo( |
| 558 | DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, |
| 559 | TUScope, nullptr, CCC, CTK_ErrorRecovery)) { |
| 560 | diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest) |
| 561 | << SuperName << ClassName); |
| 562 | PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | if (declaresSameEntity(PrevDecl, IDecl)) { |
| 567 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 568 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 569 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 570 | } else { |
| 571 | ObjCInterfaceDecl *SuperClassDecl = |
| 572 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 573 | QualType SuperClassType; |
| 574 | |
| 575 | |
| 576 | if (SuperClassDecl) { |
| 577 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
| 578 | SuperClassType = Context.getObjCInterfaceType(SuperClassDecl); |
| 579 | } |
| 580 | |
| 581 | if (PrevDecl && !SuperClassDecl) { |
| 582 | |
| 583 | |
| 584 | if (const TypedefNameDecl *TDecl = |
| 585 | dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 586 | QualType T = TDecl->getUnderlyingType(); |
| 587 | if (T->isObjCObjectType()) { |
| 588 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
| 589 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| 590 | SuperClassType = Context.getTypeDeclType(TDecl); |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc); |
| 597 | } |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | if (!SuperClassDecl) { |
| 607 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 608 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| 613 | if (!SuperClassDecl) |
| 614 | Diag(SuperLoc, diag::err_undef_superclass) |
| 615 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 616 | else if (RequireCompleteType(SuperLoc, |
| 617 | SuperClassType, |
| 618 | diag::err_forward_superclass, |
| 619 | SuperClassDecl->getDeclName(), |
| 620 | ClassName, |
| 621 | SourceRange(AtInterfaceLoc, ClassLoc))) { |
| 622 | SuperClassDecl = nullptr; |
| 623 | SuperClassType = QualType(); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | if (SuperClassType.isNull()) { |
| 628 | (0) . __assert_fail ("!SuperClassDecl && \"Failed to set SuperClassType?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 628, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!SuperClassDecl && "Failed to set SuperClassType?"); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | |
| 633 | TypeSourceInfo *SuperClassTInfo = nullptr; |
| 634 | if (!SuperTypeArgs.empty()) { |
| 635 | TypeResult fullSuperClassType = actOnObjCTypeArgsAndProtocolQualifiers( |
| 636 | S, |
| 637 | SuperLoc, |
| 638 | CreateParsedType(SuperClassType, |
| 639 | nullptr), |
| 640 | SuperTypeArgsRange.getBegin(), |
| 641 | SuperTypeArgs, |
| 642 | SuperTypeArgsRange.getEnd(), |
| 643 | SourceLocation(), |
| 644 | { }, |
| 645 | { }, |
| 646 | SourceLocation()); |
| 647 | if (!fullSuperClassType.isUsable()) |
| 648 | return; |
| 649 | |
| 650 | SuperClassType = GetTypeFromParser(fullSuperClassType.get(), |
| 651 | &SuperClassTInfo); |
| 652 | } |
| 653 | |
| 654 | if (!SuperClassTInfo) { |
| 655 | SuperClassTInfo = Context.getTrivialTypeSourceInfo(SuperClassType, |
| 656 | SuperLoc); |
| 657 | } |
| 658 | |
| 659 | IDecl->setSuperClass(SuperClassTInfo); |
| 660 | IDecl->setEndOfDefinitionLoc(SuperClassTInfo->getTypeLoc().getEndLoc()); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | DeclResult Sema::actOnObjCTypeParam(Scope *S, |
| 665 | ObjCTypeParamVariance variance, |
| 666 | SourceLocation varianceLoc, |
| 667 | unsigned index, |
| 668 | IdentifierInfo *paramName, |
| 669 | SourceLocation paramLoc, |
| 670 | SourceLocation colonLoc, |
| 671 | ParsedType parsedTypeBound) { |
| 672 | |
| 673 | TypeSourceInfo *typeBoundInfo = nullptr; |
| 674 | if (parsedTypeBound) { |
| 675 | |
| 676 | QualType typeBound = GetTypeFromParser(parsedTypeBound, &typeBoundInfo); |
| 677 | if (typeBound->isObjCObjectPointerType()) { |
| 678 | |
| 679 | } else if (typeBound->isObjCObjectType()) { |
| 680 | |
| 681 | |
| 682 | SourceLocation starLoc = getLocForEndOfToken( |
| 683 | typeBoundInfo->getTypeLoc().getEndLoc()); |
| 684 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 685 | diag::err_objc_type_param_bound_missing_pointer) |
| 686 | << typeBound << paramName |
| 687 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 688 | |
| 689 | |
| 690 | |
| 691 | TypeLocBuilder builder; |
| 692 | builder.pushFullCopy(typeBoundInfo->getTypeLoc()); |
| 693 | |
| 694 | |
| 695 | typeBound = Context.getObjCObjectPointerType(typeBound); |
| 696 | ObjCObjectPointerTypeLoc newT |
| 697 | = builder.push<ObjCObjectPointerTypeLoc>(typeBound); |
| 698 | newT.setStarLoc(starLoc); |
| 699 | |
| 700 | |
| 701 | typeBoundInfo = builder.getTypeSourceInfo(Context, typeBound); |
| 702 | } else { |
| 703 | |
| 704 | Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 705 | diag::err_objc_type_param_bound_nonobject) |
| 706 | << typeBound << paramName; |
| 707 | |
| 708 | |
| 709 | typeBoundInfo = nullptr; |
| 710 | } |
| 711 | |
| 712 | |
| 713 | |
| 714 | if (typeBoundInfo) { |
| 715 | QualType typeBound = typeBoundInfo->getType(); |
| 716 | TypeLoc qual = typeBoundInfo->getTypeLoc().findExplicitQualifierLoc(); |
| 717 | if (qual || typeBound.hasQualifiers()) { |
| 718 | bool diagnosed = false; |
| 719 | SourceRange rangeToRemove; |
| 720 | if (qual) { |
| 721 | if (auto attr = qual.getAs<AttributedTypeLoc>()) { |
| 722 | rangeToRemove = attr.getLocalSourceRange(); |
| 723 | if (attr.getTypePtr()->getImmediateNullability()) { |
| 724 | Diag(attr.getBeginLoc(), |
| 725 | diag::err_objc_type_param_bound_explicit_nullability) |
| 726 | << paramName << typeBound |
| 727 | << FixItHint::CreateRemoval(rangeToRemove); |
| 728 | diagnosed = true; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | if (!diagnosed) { |
| 734 | Diag(qual ? qual.getBeginLoc() |
| 735 | : typeBoundInfo->getTypeLoc().getBeginLoc(), |
| 736 | diag::err_objc_type_param_bound_qualified) |
| 737 | << paramName << typeBound |
| 738 | << typeBound.getQualifiers().getAsString() |
| 739 | << FixItHint::CreateRemoval(rangeToRemove); |
| 740 | } |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | Qualifiers quals = typeBound.getQualifiers(); |
| 746 | quals.removeCVRQualifiers(); |
| 747 | if (!quals.empty()) { |
| 748 | typeBoundInfo = |
| 749 | Context.getTrivialTypeSourceInfo(typeBound.getUnqualifiedType()); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | |
| 756 | |
| 757 | if (!typeBoundInfo) { |
| 758 | colonLoc = SourceLocation(); |
| 759 | typeBoundInfo = Context.getTrivialTypeSourceInfo(Context.getObjCIdType()); |
| 760 | } |
| 761 | |
| 762 | |
| 763 | return ObjCTypeParamDecl::Create(Context, CurContext, variance, varianceLoc, |
| 764 | index, paramLoc, paramName, colonLoc, |
| 765 | typeBoundInfo); |
| 766 | } |
| 767 | |
| 768 | ObjCTypeParamList *Sema::actOnObjCTypeParamList(Scope *S, |
| 769 | SourceLocation lAngleLoc, |
| 770 | ArrayRef<Decl *> typeParamsIn, |
| 771 | SourceLocation rAngleLoc) { |
| 772 | |
| 773 | ArrayRef<ObjCTypeParamDecl *> |
| 774 | typeParams( |
| 775 | reinterpret_cast<ObjCTypeParamDecl * const *>(typeParamsIn.data()), |
| 776 | typeParamsIn.size()); |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | llvm::SmallDenseMap<IdentifierInfo *, ObjCTypeParamDecl *> knownParams; |
| 783 | for (auto typeParam : typeParams) { |
| 784 | auto known = knownParams.find(typeParam->getIdentifier()); |
| 785 | if (known != knownParams.end()) { |
| 786 | Diag(typeParam->getLocation(), diag::err_objc_type_param_redecl) |
| 787 | << typeParam->getIdentifier() |
| 788 | << SourceRange(known->second->getLocation()); |
| 789 | |
| 790 | typeParam->setInvalidDecl(); |
| 791 | } else { |
| 792 | knownParams.insert(std::make_pair(typeParam->getIdentifier(), typeParam)); |
| 793 | |
| 794 | |
| 795 | PushOnScopeChains(typeParam, S, ); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | |
| 800 | return ObjCTypeParamList::create(Context, lAngleLoc, typeParams, rAngleLoc); |
| 801 | } |
| 802 | |
| 803 | void Sema::popObjCTypeParamList(Scope *S, ObjCTypeParamList *typeParamList) { |
| 804 | for (auto typeParam : *typeParamList) { |
| 805 | if (!typeParam->isInvalidDecl()) { |
| 806 | S->RemoveDecl(typeParam); |
| 807 | IdResolver.RemoveDecl(typeParam); |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | namespace { |
| 813 | |
| 814 | |
| 815 | enum class TypeParamListContext { |
| 816 | ForwardDeclaration, |
| 817 | Definition, |
| 818 | Category, |
| 819 | Extension |
| 820 | }; |
| 821 | } |
| 822 | |
| 823 | |
| 824 | |
| 825 | |
| 826 | static bool checkTypeParamListConsistency(Sema &S, |
| 827 | ObjCTypeParamList *prevTypeParams, |
| 828 | ObjCTypeParamList *newTypeParams, |
| 829 | TypeParamListContext newContext) { |
| 830 | |
| 831 | if (prevTypeParams->size() != newTypeParams->size()) { |
| 832 | SourceLocation diagLoc; |
| 833 | if (newTypeParams->size() > prevTypeParams->size()) { |
| 834 | diagLoc = newTypeParams->begin()[prevTypeParams->size()]->getLocation(); |
| 835 | } else { |
| 836 | diagLoc = S.getLocForEndOfToken(newTypeParams->back()->getEndLoc()); |
| 837 | } |
| 838 | |
| 839 | S.Diag(diagLoc, diag::err_objc_type_param_arity_mismatch) |
| 840 | << static_cast<unsigned>(newContext) |
| 841 | << (newTypeParams->size() > prevTypeParams->size()) |
| 842 | << prevTypeParams->size() |
| 843 | << newTypeParams->size(); |
| 844 | |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | |
| 849 | for (unsigned i = 0, n = prevTypeParams->size(); i != n; ++i) { |
| 850 | ObjCTypeParamDecl *prevTypeParam = prevTypeParams->begin()[i]; |
| 851 | ObjCTypeParamDecl *newTypeParam = newTypeParams->begin()[i]; |
| 852 | |
| 853 | |
| 854 | if (newTypeParam->getVariance() != prevTypeParam->getVariance()) { |
| 855 | if (newTypeParam->getVariance() == ObjCTypeParamVariance::Invariant && |
| 856 | newContext != TypeParamListContext::Definition) { |
| 857 | |
| 858 | |
| 859 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
| 860 | } else if (prevTypeParam->getVariance() |
| 861 | == ObjCTypeParamVariance::Invariant && |
| 862 | !(isa<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) && |
| 863 | cast<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) |
| 864 | ->getDefinition() == prevTypeParam->getDeclContext())) { |
| 865 | |
| 866 | |
| 867 | |
| 868 | } else { |
| 869 | { |
| 870 | |
| 871 | SourceLocation diagLoc = newTypeParam->getVarianceLoc(); |
| 872 | if (diagLoc.isInvalid()) |
| 873 | diagLoc = newTypeParam->getBeginLoc(); |
| 874 | |
| 875 | auto diag = S.Diag(diagLoc, |
| 876 | diag::err_objc_type_param_variance_conflict) |
| 877 | << static_cast<unsigned>(newTypeParam->getVariance()) |
| 878 | << newTypeParam->getDeclName() |
| 879 | << static_cast<unsigned>(prevTypeParam->getVariance()) |
| 880 | << prevTypeParam->getDeclName(); |
| 881 | switch (prevTypeParam->getVariance()) { |
| 882 | case ObjCTypeParamVariance::Invariant: |
| 883 | diag << FixItHint::CreateRemoval(newTypeParam->getVarianceLoc()); |
| 884 | break; |
| 885 | |
| 886 | case ObjCTypeParamVariance::Covariant: |
| 887 | case ObjCTypeParamVariance::Contravariant: { |
| 888 | StringRef newVarianceStr |
| 889 | = prevTypeParam->getVariance() == ObjCTypeParamVariance::Covariant |
| 890 | ? "__covariant" |
| 891 | : "__contravariant"; |
| 892 | if (newTypeParam->getVariance() |
| 893 | == ObjCTypeParamVariance::Invariant) { |
| 894 | diag << FixItHint::CreateInsertion(newTypeParam->getBeginLoc(), |
| 895 | (newVarianceStr + " ").str()); |
| 896 | } else { |
| 897 | diag << FixItHint::CreateReplacement(newTypeParam->getVarianceLoc(), |
| 898 | newVarianceStr); |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 905 | << prevTypeParam->getDeclName(); |
| 906 | |
| 907 | |
| 908 | newTypeParam->setVariance(prevTypeParam->getVariance()); |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | |
| 913 | if (S.Context.hasSameType(prevTypeParam->getUnderlyingType(), |
| 914 | newTypeParam->getUnderlyingType())) |
| 915 | continue; |
| 916 | |
| 917 | |
| 918 | |
| 919 | if (newTypeParam->hasExplicitBound()) { |
| 920 | SourceRange newBoundRange = newTypeParam->getTypeSourceInfo() |
| 921 | ->getTypeLoc().getSourceRange(); |
| 922 | S.Diag(newBoundRange.getBegin(), diag::err_objc_type_param_bound_conflict) |
| 923 | << newTypeParam->getUnderlyingType() |
| 924 | << newTypeParam->getDeclName() |
| 925 | << prevTypeParam->hasExplicitBound() |
| 926 | << prevTypeParam->getUnderlyingType() |
| 927 | << (newTypeParam->getDeclName() == prevTypeParam->getDeclName()) |
| 928 | << prevTypeParam->getDeclName() |
| 929 | << FixItHint::CreateReplacement( |
| 930 | newBoundRange, |
| 931 | prevTypeParam->getUnderlyingType().getAsString( |
| 932 | S.Context.getPrintingPolicy())); |
| 933 | |
| 934 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 935 | << prevTypeParam->getDeclName(); |
| 936 | |
| 937 | |
| 938 | |
| 939 | newTypeParam->setTypeSourceInfo( |
| 940 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 941 | continue; |
| 942 | } |
| 943 | |
| 944 | |
| 945 | |
| 946 | |
| 947 | if (newContext == TypeParamListContext::ForwardDeclaration || |
| 948 | newContext == TypeParamListContext::Definition) { |
| 949 | |
| 950 | SourceLocation insertionLoc |
| 951 | = S.getLocForEndOfToken(newTypeParam->getLocation()); |
| 952 | std::string newCode |
| 953 | = " : " + prevTypeParam->getUnderlyingType().getAsString( |
| 954 | S.Context.getPrintingPolicy()); |
| 955 | S.Diag(newTypeParam->getLocation(), |
| 956 | diag::err_objc_type_param_bound_missing) |
| 957 | << prevTypeParam->getUnderlyingType() |
| 958 | << newTypeParam->getDeclName() |
| 959 | << (newContext == TypeParamListContext::ForwardDeclaration) |
| 960 | << FixItHint::CreateInsertion(insertionLoc, newCode); |
| 961 | |
| 962 | S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| 963 | << prevTypeParam->getDeclName(); |
| 964 | } |
| 965 | |
| 966 | |
| 967 | newTypeParam->setTypeSourceInfo( |
| 968 | S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| 969 | } |
| 970 | |
| 971 | return false; |
| 972 | } |
| 973 | |
| 974 | Decl *Sema::ActOnStartClassInterface( |
| 975 | Scope *S, SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
| 976 | SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
| 977 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| 978 | ArrayRef<ParsedType> SuperTypeArgs, SourceRange SuperTypeArgsRange, |
| 979 | Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| 980 | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| 981 | const ParsedAttributesView &AttrList) { |
| 982 | (0) . __assert_fail ("ClassName && \"Missing class identifier\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 982, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ClassName && "Missing class identifier"); |
| 983 | |
| 984 | |
| 985 | NamedDecl *PrevDecl = |
| 986 | LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 987 | forRedeclarationInCurContext()); |
| 988 | |
| 989 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
| 990 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
| 991 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 992 | } |
| 993 | |
| 994 | |
| 995 | ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 996 | |
| 997 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | ClassName = PrevIDecl->getIdentifier(); |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | if (PrevIDecl) { |
| 1016 | if (ObjCTypeParamList *prevTypeParamList = PrevIDecl->getTypeParamList()) { |
| 1017 | if (typeParamList) { |
| 1018 | |
| 1019 | if (checkTypeParamListConsistency(*this, prevTypeParamList, |
| 1020 | typeParamList, |
| 1021 | TypeParamListContext::Definition)) { |
| 1022 | typeParamList = nullptr; |
| 1023 | } |
| 1024 | } else { |
| 1025 | Diag(ClassLoc, diag::err_objc_parameterized_forward_class_first) |
| 1026 | << ClassName; |
| 1027 | Diag(prevTypeParamList->getLAngleLoc(), diag::note_previous_decl) |
| 1028 | << ClassName; |
| 1029 | |
| 1030 | |
| 1031 | SmallVector<ObjCTypeParamDecl *, 4> clonedTypeParams; |
| 1032 | for (auto typeParam : *prevTypeParamList) { |
| 1033 | clonedTypeParams.push_back( |
| 1034 | ObjCTypeParamDecl::Create( |
| 1035 | Context, |
| 1036 | CurContext, |
| 1037 | typeParam->getVariance(), |
| 1038 | SourceLocation(), |
| 1039 | typeParam->getIndex(), |
| 1040 | SourceLocation(), |
| 1041 | typeParam->getIdentifier(), |
| 1042 | SourceLocation(), |
| 1043 | Context.getTrivialTypeSourceInfo(typeParam->getUnderlyingType()))); |
| 1044 | } |
| 1045 | |
| 1046 | typeParamList = ObjCTypeParamList::create(Context, |
| 1047 | SourceLocation(), |
| 1048 | clonedTypeParams, |
| 1049 | SourceLocation()); |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | ObjCInterfaceDecl *IDecl |
| 1055 | = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName, |
| 1056 | typeParamList, PrevIDecl, ClassLoc); |
| 1057 | if (PrevIDecl) { |
| 1058 | |
| 1059 | if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 1060 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def) |
| 1061 | << PrevIDecl->getDeclName(); |
| 1062 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1063 | IDecl->setInvalidDecl(); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
| 1068 | AddPragmaAttributes(TUScope, IDecl); |
| 1069 | PushOnScopeChains(IDecl, TUScope); |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | if (!IDecl->hasDefinition()) |
| 1074 | IDecl->startDefinition(); |
| 1075 | |
| 1076 | if (SuperName) { |
| 1077 | |
| 1078 | ContextRAII SavedContext(*this, IDecl); |
| 1079 | |
| 1080 | ActOnSuperClassOfClassInterface(S, AtInterfaceLoc, IDecl, |
| 1081 | ClassName, ClassLoc, |
| 1082 | SuperName, SuperLoc, SuperTypeArgs, |
| 1083 | SuperTypeArgsRange); |
| 1084 | } else { |
| 1085 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | if (NumProtoRefs) { |
| 1090 | diagnoseUseOfProtocols(*this, IDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1091 | NumProtoRefs, ProtoLocs); |
| 1092 | IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
| 1093 | ProtoLocs, Context); |
| 1094 | IDecl->setEndOfDefinitionLoc(EndProtoLoc); |
| 1095 | } |
| 1096 | |
| 1097 | CheckObjCDeclScope(IDecl); |
| 1098 | return ActOnObjCContainerStartDefinition(IDecl); |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | |
| 1104 | void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs, |
| 1105 | SmallVectorImpl<SourceLocation> &ProtocolLocs, |
| 1106 | IdentifierInfo *SuperName, |
| 1107 | SourceLocation SuperLoc) { |
| 1108 | if (!SuperName) |
| 1109 | return; |
| 1110 | NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| 1111 | LookupOrdinaryName); |
| 1112 | if (!IDecl) |
| 1113 | return; |
| 1114 | |
| 1115 | if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) { |
| 1116 | QualType T = TDecl->getUnderlyingType(); |
| 1117 | if (T->isObjCObjectType()) |
| 1118 | if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>()) { |
| 1119 | ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end()); |
| 1120 | |
| 1121 | |
| 1122 | |
| 1123 | |
| 1124 | ProtocolLocs.append(OPT->getNumProtocols(), SuperLoc); |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc, |
| 1132 | IdentifierInfo *AliasName, |
| 1133 | SourceLocation AliasLocation, |
| 1134 | IdentifierInfo *ClassName, |
| 1135 | SourceLocation ClassLocation) { |
| 1136 | |
| 1137 | NamedDecl *ADecl = |
| 1138 | LookupSingleName(TUScope, AliasName, AliasLocation, LookupOrdinaryName, |
| 1139 | forRedeclarationInCurContext()); |
| 1140 | if (ADecl) { |
| 1141 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
| 1142 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
| 1143 | return nullptr; |
| 1144 | } |
| 1145 | |
| 1146 | NamedDecl *CDeclU = |
| 1147 | LookupSingleName(TUScope, ClassName, ClassLocation, LookupOrdinaryName, |
| 1148 | forRedeclarationInCurContext()); |
| 1149 | if (const TypedefNameDecl *TDecl = |
| 1150 | dyn_cast_or_null<TypedefNameDecl>(CDeclU)) { |
| 1151 | QualType T = TDecl->getUnderlyingType(); |
| 1152 | if (T->isObjCObjectType()) { |
| 1153 | if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) { |
| 1154 | ClassName = IDecl->getIdentifier(); |
| 1155 | CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
| 1156 | LookupOrdinaryName, |
| 1157 | forRedeclarationInCurContext()); |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 1162 | if (!CDecl) { |
| 1163 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
| 1164 | if (CDeclU) |
| 1165 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
| 1166 | return nullptr; |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | ObjCCompatibleAliasDecl *AliasDecl = |
| 1171 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
| 1172 | |
| 1173 | if (!CheckObjCDeclScope(AliasDecl)) |
| 1174 | PushOnScopeChains(AliasDecl, TUScope); |
| 1175 | |
| 1176 | return AliasDecl; |
| 1177 | } |
| 1178 | |
| 1179 | bool Sema::CheckForwardProtocolDeclarationForCircularDependency( |
| 1180 | IdentifierInfo *PName, |
| 1181 | SourceLocation &Ploc, SourceLocation PrevLoc, |
| 1182 | const ObjCList<ObjCProtocolDecl> &PList) { |
| 1183 | |
| 1184 | bool res = false; |
| 1185 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 1186 | E = PList.end(); I != E; ++I) { |
| 1187 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| 1188 | Ploc)) { |
| 1189 | if (PDecl->getIdentifier() == PName) { |
| 1190 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 1191 | Diag(PrevLoc, diag::note_previous_definition); |
| 1192 | res = true; |
| 1193 | } |
| 1194 | |
| 1195 | if (!PDecl->hasDefinition()) |
| 1196 | continue; |
| 1197 | |
| 1198 | if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
| 1199 | PDecl->getLocation(), PDecl->getReferencedProtocols())) |
| 1200 | res = true; |
| 1201 | } |
| 1202 | } |
| 1203 | return res; |
| 1204 | } |
| 1205 | |
| 1206 | Decl *Sema::ActOnStartProtocolInterface( |
| 1207 | SourceLocation AtProtoInterfaceLoc, IdentifierInfo *ProtocolName, |
| 1208 | SourceLocation ProtocolLoc, Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| 1209 | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| 1210 | const ParsedAttributesView &AttrList) { |
| 1211 | bool err = false; |
| 1212 | |
| 1213 | (0) . __assert_fail ("ProtocolName && \"Missing protocol identifier\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 1213, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ProtocolName && "Missing protocol identifier"); |
| 1214 | ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc, |
| 1215 | forRedeclarationInCurContext()); |
| 1216 | ObjCProtocolDecl *PDecl = nullptr; |
| 1217 | if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) { |
| 1218 | |
| 1219 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
| 1220 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| 1227 | ProtocolLoc, AtProtoInterfaceLoc, |
| 1228 | ); |
| 1229 | |
| 1230 | |
| 1231 | |
| 1232 | if (getLangOpts().Modules) |
| 1233 | PushOnScopeChains(PDecl, TUScope); |
| 1234 | PDecl->startDefinition(); |
| 1235 | } else { |
| 1236 | if (PrevDecl) { |
| 1237 | |
| 1238 | |
| 1239 | ObjCList<ObjCProtocolDecl> PList; |
| 1240 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
| 1241 | err = CheckForwardProtocolDeclarationForCircularDependency( |
| 1242 | ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList); |
| 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| 1247 | ProtocolLoc, AtProtoInterfaceLoc, |
| 1248 | ); |
| 1249 | |
| 1250 | PushOnScopeChains(PDecl, TUScope); |
| 1251 | PDecl->startDefinition(); |
| 1252 | } |
| 1253 | |
| 1254 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
| 1255 | AddPragmaAttributes(TUScope, PDecl); |
| 1256 | |
| 1257 | |
| 1258 | if (PrevDecl) |
| 1259 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1260 | |
| 1261 | if (!err && NumProtoRefs ) { |
| 1262 | |
| 1263 | diagnoseUseOfProtocols(*this, PDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1264 | NumProtoRefs, ProtoLocs); |
| 1265 | PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
| 1266 | ProtoLocs, Context); |
| 1267 | } |
| 1268 | |
| 1269 | CheckObjCDeclScope(PDecl); |
| 1270 | return ActOnObjCContainerStartDefinition(PDecl); |
| 1271 | } |
| 1272 | |
| 1273 | static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl, |
| 1274 | ObjCProtocolDecl *&UndefinedProtocol) { |
| 1275 | if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) { |
| 1276 | UndefinedProtocol = PDecl; |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
| 1280 | for (auto *PI : PDecl->protocols()) |
| 1281 | if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) { |
| 1282 | UndefinedProtocol = PI; |
| 1283 | return true; |
| 1284 | } |
| 1285 | return false; |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | void |
| 1292 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer, |
| 1293 | ArrayRef<IdentifierLocPair> ProtocolId, |
| 1294 | SmallVectorImpl<Decl *> &Protocols) { |
| 1295 | for (const IdentifierLocPair &Pair : ProtocolId) { |
| 1296 | ObjCProtocolDecl *PDecl = LookupProtocol(Pair.first, Pair.second); |
| 1297 | if (!PDecl) { |
| 1298 | DeclFilterCCC<ObjCProtocolDecl> CCC{}; |
| 1299 | TypoCorrection Corrected = CorrectTypo( |
| 1300 | DeclarationNameInfo(Pair.first, Pair.second), LookupObjCProtocolName, |
| 1301 | TUScope, nullptr, CCC, CTK_ErrorRecovery); |
| 1302 | if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) |
| 1303 | diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest) |
| 1304 | << Pair.first); |
| 1305 | } |
| 1306 | |
| 1307 | if (!PDecl) { |
| 1308 | Diag(Pair.second, diag::err_undeclared_protocol) << Pair.first; |
| 1309 | continue; |
| 1310 | } |
| 1311 | |
| 1312 | if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()) |
| 1313 | PDecl = PDecl->getDefinition(); |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | if (!ForObjCContainer) { |
| 1318 | (void)DiagnoseUseOfDecl(PDecl, Pair.second); |
| 1319 | } |
| 1320 | |
| 1321 | |
| 1322 | |
| 1323 | |
| 1324 | ObjCProtocolDecl *UndefinedProtocol; |
| 1325 | |
| 1326 | if (WarnOnDeclarations && |
| 1327 | NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) { |
| 1328 | Diag(Pair.second, diag::warn_undef_protocolref) << Pair.first; |
| 1329 | Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined) |
| 1330 | << UndefinedProtocol; |
| 1331 | } |
| 1332 | Protocols.push_back(PDecl); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | namespace { |
| 1337 | |
| 1338 | |
| 1339 | class ObjCTypeArgOrProtocolValidatorCCC final |
| 1340 | : public CorrectionCandidateCallback { |
| 1341 | ASTContext &Context; |
| 1342 | Sema::LookupNameKind LookupKind; |
| 1343 | public: |
| 1344 | ObjCTypeArgOrProtocolValidatorCCC(ASTContext &context, |
| 1345 | Sema::LookupNameKind lookupKind) |
| 1346 | : Context(context), LookupKind(lookupKind) { } |
| 1347 | |
| 1348 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| 1349 | |
| 1350 | if (LookupKind != Sema::LookupOrdinaryName) { |
| 1351 | if (candidate.getCorrectionDeclAs<ObjCProtocolDecl>()) |
| 1352 | return true; |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | if (LookupKind != Sema::LookupObjCProtocolName) { |
| 1357 | |
| 1358 | if (auto typeDecl = candidate.getCorrectionDeclAs<TypeDecl>()) { |
| 1359 | |
| 1360 | |
| 1361 | |
| 1362 | if (isa<RecordDecl>(typeDecl) && !Context.getLangOpts().CPlusPlus) |
| 1363 | return false; |
| 1364 | |
| 1365 | |
| 1366 | |
| 1367 | auto type = Context.getTypeDeclType(typeDecl); |
| 1368 | if (type->isObjCObjectPointerType() || |
| 1369 | type->isBlockPointerType() || |
| 1370 | type->isDependentType() || |
| 1371 | type->isObjCObjectType()) |
| 1372 | return true; |
| 1373 | |
| 1374 | return false; |
| 1375 | } |
| 1376 | |
| 1377 | |
| 1378 | |
| 1379 | if (candidate.getCorrectionDeclAs<ObjCInterfaceDecl>()) |
| 1380 | return true; |
| 1381 | |
| 1382 | return false; |
| 1383 | } |
| 1384 | |
| 1385 | return false; |
| 1386 | } |
| 1387 | |
| 1388 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 1389 | return llvm::make_unique<ObjCTypeArgOrProtocolValidatorCCC>(*this); |
| 1390 | } |
| 1391 | }; |
| 1392 | } |
| 1393 | |
| 1394 | void Sema::DiagnoseTypeArgsAndProtocols(IdentifierInfo *ProtocolId, |
| 1395 | SourceLocation ProtocolLoc, |
| 1396 | IdentifierInfo *TypeArgId, |
| 1397 | SourceLocation TypeArgLoc, |
| 1398 | bool SelectProtocolFirst) { |
| 1399 | Diag(TypeArgLoc, diag::err_objc_type_args_and_protocols) |
| 1400 | << SelectProtocolFirst << TypeArgId << ProtocolId |
| 1401 | << SourceRange(ProtocolLoc); |
| 1402 | } |
| 1403 | |
| 1404 | void Sema::actOnObjCTypeArgsOrProtocolQualifiers( |
| 1405 | Scope *S, |
| 1406 | ParsedType baseType, |
| 1407 | SourceLocation lAngleLoc, |
| 1408 | ArrayRef<IdentifierInfo *> identifiers, |
| 1409 | ArrayRef<SourceLocation> identifierLocs, |
| 1410 | SourceLocation rAngleLoc, |
| 1411 | SourceLocation &typeArgsLAngleLoc, |
| 1412 | SmallVectorImpl<ParsedType> &typeArgs, |
| 1413 | SourceLocation &typeArgsRAngleLoc, |
| 1414 | SourceLocation &protocolLAngleLoc, |
| 1415 | SmallVectorImpl<Decl *> &protocols, |
| 1416 | SourceLocation &protocolRAngleLoc, |
| 1417 | bool warnOnIncompleteProtocols) { |
| 1418 | |
| 1419 | |
| 1420 | unsigned numProtocolsResolved = 0; |
| 1421 | auto resolvedAsProtocols = [&] { |
| 1422 | (0) . __assert_fail ("numProtocolsResolved == identifiers.size() && \"Unresolved protocols\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 1422, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(numProtocolsResolved == identifiers.size() && "Unresolved protocols"); |
| 1423 | |
| 1424 | |
| 1425 | |
| 1426 | |
| 1427 | ObjCInterfaceDecl *baseClass = nullptr; |
| 1428 | QualType base = GetTypeFromParser(baseType, nullptr); |
| 1429 | bool allAreTypeNames = false; |
| 1430 | SourceLocation firstClassNameLoc; |
| 1431 | if (!base.isNull()) { |
| 1432 | if (const auto *objcObjectType = base->getAs<ObjCObjectType>()) { |
| 1433 | baseClass = objcObjectType->getInterface(); |
| 1434 | if (baseClass) { |
| 1435 | if (auto typeParams = baseClass->getTypeParamList()) { |
| 1436 | if (typeParams->size() == numProtocolsResolved) { |
| 1437 | |
| 1438 | allAreTypeNames = true; |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | for (unsigned i = 0, n = protocols.size(); i != n; ++i) { |
| 1446 | ObjCProtocolDecl *&proto |
| 1447 | = reinterpret_cast<ObjCProtocolDecl *&>(protocols[i]); |
| 1448 | |
| 1449 | |
| 1450 | if (!warnOnIncompleteProtocols) { |
| 1451 | (void)DiagnoseUseOfDecl(proto, identifierLocs[i]); |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | if (!proto->isThisDeclarationADefinition() && proto->getDefinition()) |
| 1456 | proto = proto->getDefinition(); |
| 1457 | |
| 1458 | |
| 1459 | |
| 1460 | |
| 1461 | ObjCProtocolDecl *forwardDecl = nullptr; |
| 1462 | if (warnOnIncompleteProtocols && |
| 1463 | NestedProtocolHasNoDefinition(proto, forwardDecl)) { |
| 1464 | Diag(identifierLocs[i], diag::warn_undef_protocolref) |
| 1465 | << proto->getDeclName(); |
| 1466 | Diag(forwardDecl->getLocation(), diag::note_protocol_decl_undefined) |
| 1467 | << forwardDecl; |
| 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | |
| 1472 | |
| 1473 | if (allAreTypeNames) { |
| 1474 | if (auto *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1475 | LookupOrdinaryName)) { |
| 1476 | if (isa<ObjCInterfaceDecl>(decl)) { |
| 1477 | if (firstClassNameLoc.isInvalid()) |
| 1478 | firstClassNameLoc = identifierLocs[i]; |
| 1479 | } else if (!isa<TypeDecl>(decl)) { |
| 1480 | |
| 1481 | allAreTypeNames = false; |
| 1482 | } |
| 1483 | } else { |
| 1484 | allAreTypeNames = false; |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | |
| 1493 | if (allAreTypeNames && firstClassNameLoc.isValid()) { |
| 1494 | llvm::SmallPtrSet<ObjCProtocolDecl*, 8> knownProtocols; |
| 1495 | Context.CollectInheritedProtocols(baseClass, knownProtocols); |
| 1496 | bool allProtocolsDeclared = true; |
| 1497 | for (auto proto : protocols) { |
| 1498 | if (knownProtocols.count(static_cast<ObjCProtocolDecl *>(proto)) == 0) { |
| 1499 | allProtocolsDeclared = false; |
| 1500 | break; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | if (allProtocolsDeclared) { |
| 1505 | Diag(firstClassNameLoc, diag::warn_objc_redundant_qualified_class_type) |
| 1506 | << baseClass->getDeclName() << SourceRange(lAngleLoc, rAngleLoc) |
| 1507 | << FixItHint::CreateInsertion(getLocForEndOfToken(firstClassNameLoc), |
| 1508 | " *"); |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | protocolLAngleLoc = lAngleLoc; |
| 1513 | protocolRAngleLoc = rAngleLoc; |
| 1514 | assert(protocols.size() == identifierLocs.size()); |
| 1515 | }; |
| 1516 | |
| 1517 | |
| 1518 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1519 | ObjCProtocolDecl *proto = LookupProtocol(identifiers[i], identifierLocs[i]); |
| 1520 | protocols.push_back(proto); |
| 1521 | if (proto) |
| 1522 | ++numProtocolsResolved; |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | if (numProtocolsResolved == identifiers.size()) |
| 1527 | return resolvedAsProtocols(); |
| 1528 | |
| 1529 | |
| 1530 | |
| 1531 | |
| 1532 | |
| 1533 | typedef llvm::PointerUnion<TypeDecl *, ObjCInterfaceDecl *> TypeOrClassDecl; |
| 1534 | SmallVector<TypeOrClassDecl, 4> typeDecls; |
| 1535 | unsigned numTypeDeclsResolved = 0; |
| 1536 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1537 | NamedDecl *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| 1538 | LookupOrdinaryName); |
| 1539 | if (!decl) { |
| 1540 | typeDecls.push_back(TypeOrClassDecl()); |
| 1541 | continue; |
| 1542 | } |
| 1543 | |
| 1544 | if (auto typeDecl = dyn_cast<TypeDecl>(decl)) { |
| 1545 | typeDecls.push_back(typeDecl); |
| 1546 | ++numTypeDeclsResolved; |
| 1547 | continue; |
| 1548 | } |
| 1549 | |
| 1550 | if (auto objcClass = dyn_cast<ObjCInterfaceDecl>(decl)) { |
| 1551 | typeDecls.push_back(objcClass); |
| 1552 | ++numTypeDeclsResolved; |
| 1553 | continue; |
| 1554 | } |
| 1555 | |
| 1556 | typeDecls.push_back(TypeOrClassDecl()); |
| 1557 | } |
| 1558 | |
| 1559 | AttributeFactory attrFactory; |
| 1560 | |
| 1561 | |
| 1562 | |
| 1563 | auto resolveTypeReference = [&](TypeOrClassDecl typeDecl, SourceLocation loc) |
| 1564 | -> TypeResult { |
| 1565 | |
| 1566 | DeclSpec DS(attrFactory); |
| 1567 | const char* prevSpec; |
| 1568 | unsigned diagID; |
| 1569 | QualType type; |
| 1570 | if (auto *actualTypeDecl = typeDecl.dyn_cast<TypeDecl *>()) |
| 1571 | type = Context.getTypeDeclType(actualTypeDecl); |
| 1572 | else |
| 1573 | type = Context.getObjCInterfaceType(typeDecl.get<ObjCInterfaceDecl *>()); |
| 1574 | TypeSourceInfo *parsedTSInfo = Context.getTrivialTypeSourceInfo(type, loc); |
| 1575 | ParsedType parsedType = CreateParsedType(type, parsedTSInfo); |
| 1576 | DS.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID, |
| 1577 | parsedType, Context.getPrintingPolicy()); |
| 1578 | |
| 1579 | DS.SetRangeStart(loc); |
| 1580 | DS.SetRangeEnd(loc); |
| 1581 | |
| 1582 | |
| 1583 | Declarator D(DS, DeclaratorContext::TypeNameContext); |
| 1584 | |
| 1585 | |
| 1586 | |
| 1587 | if (type->getAs<ObjCInterfaceType>()) { |
| 1588 | SourceLocation starLoc = getLocForEndOfToken(loc); |
| 1589 | D.AddTypeInfo(DeclaratorChunk::getPointer(, starLoc, |
| 1590 | SourceLocation(), |
| 1591 | SourceLocation(), |
| 1592 | SourceLocation(), |
| 1593 | SourceLocation(), |
| 1594 | SourceLocation()), |
| 1595 | starLoc); |
| 1596 | |
| 1597 | |
| 1598 | Diag(loc, diag::err_objc_type_arg_missing_star) |
| 1599 | << type |
| 1600 | << FixItHint::CreateInsertion(starLoc, " *"); |
| 1601 | } |
| 1602 | |
| 1603 | |
| 1604 | return ActOnTypeName(S, D); |
| 1605 | }; |
| 1606 | |
| 1607 | |
| 1608 | |
| 1609 | auto resolvedAsTypeDecls = [&] { |
| 1610 | |
| 1611 | protocols.clear(); |
| 1612 | |
| 1613 | (0) . __assert_fail ("numTypeDeclsResolved == identifiers.size() && \"Unresolved type decl\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 1613, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(numTypeDeclsResolved == identifiers.size() && "Unresolved type decl"); |
| 1614 | |
| 1615 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1616 | |
| 1617 | TypeResult type = resolveTypeReference(typeDecls[i], identifierLocs[i]); |
| 1618 | if (!type.isUsable()) { |
| 1619 | typeArgs.clear(); |
| 1620 | return; |
| 1621 | } |
| 1622 | |
| 1623 | typeArgs.push_back(type.get()); |
| 1624 | } |
| 1625 | |
| 1626 | typeArgsLAngleLoc = lAngleLoc; |
| 1627 | typeArgsRAngleLoc = rAngleLoc; |
| 1628 | }; |
| 1629 | |
| 1630 | |
| 1631 | |
| 1632 | if (numTypeDeclsResolved == identifiers.size()) |
| 1633 | return resolvedAsTypeDecls(); |
| 1634 | |
| 1635 | |
| 1636 | |
| 1637 | |
| 1638 | LookupNameKind lookupKind = LookupAnyName; |
| 1639 | for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| 1640 | |
| 1641 | |
| 1642 | if (protocols[i] || typeDecls[i]) { |
| 1643 | |
| 1644 | |
| 1645 | if (lookupKind == LookupAnyName) { |
| 1646 | |
| 1647 | |
| 1648 | if (protocols[i] && typeDecls[i]) |
| 1649 | continue; |
| 1650 | |
| 1651 | |
| 1652 | |
| 1653 | lookupKind = protocols[i] ? LookupObjCProtocolName |
| 1654 | : LookupOrdinaryName; |
| 1655 | continue; |
| 1656 | } |
| 1657 | |
| 1658 | |
| 1659 | |
| 1660 | if (lookupKind == LookupObjCProtocolName && protocols[i]) |
| 1661 | continue; |
| 1662 | |
| 1663 | |
| 1664 | |
| 1665 | if (lookupKind == LookupOrdinaryName && typeDecls[i]) |
| 1666 | continue; |
| 1667 | |
| 1668 | |
| 1669 | |
| 1670 | DiagnoseTypeArgsAndProtocols(identifiers[0], identifierLocs[0], |
| 1671 | identifiers[i], identifierLocs[i], |
| 1672 | protocols[i] != nullptr); |
| 1673 | |
| 1674 | protocols.clear(); |
| 1675 | typeArgs.clear(); |
| 1676 | return; |
| 1677 | } |
| 1678 | |
| 1679 | |
| 1680 | ObjCTypeArgOrProtocolValidatorCCC CCC(Context, lookupKind); |
| 1681 | TypoCorrection corrected = |
| 1682 | CorrectTypo(DeclarationNameInfo(identifiers[i], identifierLocs[i]), |
| 1683 | lookupKind, S, nullptr, CCC, CTK_ErrorRecovery); |
| 1684 | if (corrected) { |
| 1685 | |
| 1686 | if (auto proto = corrected.getCorrectionDeclAs<ObjCProtocolDecl>()) { |
| 1687 | diagnoseTypo(corrected, |
| 1688 | PDiag(diag::err_undeclared_protocol_suggest) |
| 1689 | << identifiers[i]); |
| 1690 | lookupKind = LookupObjCProtocolName; |
| 1691 | protocols[i] = proto; |
| 1692 | ++numProtocolsResolved; |
| 1693 | continue; |
| 1694 | } |
| 1695 | |
| 1696 | |
| 1697 | if (auto typeDecl = corrected.getCorrectionDeclAs<TypeDecl>()) { |
| 1698 | diagnoseTypo(corrected, |
| 1699 | PDiag(diag::err_unknown_typename_suggest) |
| 1700 | << identifiers[i]); |
| 1701 | lookupKind = LookupOrdinaryName; |
| 1702 | typeDecls[i] = typeDecl; |
| 1703 | ++numTypeDeclsResolved; |
| 1704 | continue; |
| 1705 | } |
| 1706 | |
| 1707 | |
| 1708 | if (auto objcClass = corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1709 | diagnoseTypo(corrected, |
| 1710 | PDiag(diag::err_unknown_type_or_class_name_suggest) |
| 1711 | << identifiers[i] << true); |
| 1712 | lookupKind = LookupOrdinaryName; |
| 1713 | typeDecls[i] = objcClass; |
| 1714 | ++numTypeDeclsResolved; |
| 1715 | continue; |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | |
| 1720 | Diag(identifierLocs[i], |
| 1721 | (lookupKind == LookupAnyName ? diag::err_objc_type_arg_missing |
| 1722 | : lookupKind == LookupObjCProtocolName ? diag::err_undeclared_protocol |
| 1723 | : diag::err_unknown_typename)) |
| 1724 | << identifiers[i]; |
| 1725 | protocols.clear(); |
| 1726 | typeArgs.clear(); |
| 1727 | return; |
| 1728 | } |
| 1729 | |
| 1730 | |
| 1731 | |
| 1732 | if (numProtocolsResolved == identifiers.size()) |
| 1733 | return resolvedAsProtocols(); |
| 1734 | |
| 1735 | |
| 1736 | (0) . __assert_fail ("numTypeDeclsResolved == identifiers.size() && \"Not all types?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 1736, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(numTypeDeclsResolved == identifiers.size() && "Not all types?"); |
| 1737 | return resolvedAsTypeDecls(); |
| 1738 | } |
| 1739 | |
| 1740 | |
| 1741 | |
| 1742 | |
| 1743 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
| 1744 | ObjCInterfaceDecl *ID) { |
| 1745 | if (!ID) |
| 1746 | return; |
| 1747 | |
| 1748 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
| 1749 | for (auto *MD : ID->methods()) |
| 1750 | MethodMap[MD->getSelector()] = MD; |
| 1751 | |
| 1752 | if (MethodMap.empty()) |
| 1753 | return; |
| 1754 | for (const auto *Method : CAT->methods()) { |
| 1755 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
| 1756 | if (PrevMethod && |
| 1757 | (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) && |
| 1758 | !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
| 1759 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 1760 | << Method->getDeclName(); |
| 1761 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 1762 | } |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | |
| 1767 | Sema::DeclGroupPtrTy |
| 1768 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
| 1769 | ArrayRef<IdentifierLocPair> IdentList, |
| 1770 | const ParsedAttributesView &attrList) { |
| 1771 | SmallVector<Decl *, 8> DeclsInGroup; |
| 1772 | for (const IdentifierLocPair &IdentPair : IdentList) { |
| 1773 | IdentifierInfo *Ident = IdentPair.first; |
| 1774 | ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentPair.second, |
| 1775 | forRedeclarationInCurContext()); |
| 1776 | ObjCProtocolDecl *PDecl |
| 1777 | = ObjCProtocolDecl::Create(Context, CurContext, Ident, |
| 1778 | IdentPair.second, AtProtocolLoc, |
| 1779 | PrevDecl); |
| 1780 | |
| 1781 | PushOnScopeChains(PDecl, TUScope); |
| 1782 | CheckObjCDeclScope(PDecl); |
| 1783 | |
| 1784 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
| 1785 | AddPragmaAttributes(TUScope, PDecl); |
| 1786 | |
| 1787 | if (PrevDecl) |
| 1788 | mergeDeclAttributes(PDecl, PrevDecl); |
| 1789 | |
| 1790 | DeclsInGroup.push_back(PDecl); |
| 1791 | } |
| 1792 | |
| 1793 | return BuildDeclaratorGroup(DeclsInGroup); |
| 1794 | } |
| 1795 | |
| 1796 | Decl *Sema::ActOnStartCategoryInterface( |
| 1797 | SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
| 1798 | SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
| 1799 | IdentifierInfo *CategoryName, SourceLocation CategoryLoc, |
| 1800 | Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| 1801 | const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| 1802 | const ParsedAttributesView &AttrList) { |
| 1803 | ObjCCategoryDecl *CDecl; |
| 1804 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
| 1805 | |
| 1806 | |
| 1807 | |
| 1808 | if (!IDecl |
| 1809 | || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1810 | diag::err_category_forward_interface, |
| 1811 | CategoryName == nullptr)) { |
| 1812 | |
| 1813 | |
| 1814 | |
| 1815 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 1816 | ClassLoc, CategoryLoc, CategoryName, |
| 1817 | IDecl, typeParamList); |
| 1818 | CDecl->setInvalidDecl(); |
| 1819 | CurContext->addDecl(CDecl); |
| 1820 | |
| 1821 | if (!IDecl) |
| 1822 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
| 1823 | return ActOnObjCContainerStartDefinition(CDecl); |
| 1824 | } |
| 1825 | |
| 1826 | if (!CategoryName && IDecl->getImplementation()) { |
| 1827 | Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
| 1828 | Diag(IDecl->getImplementation()->getLocation(), |
| 1829 | diag::note_implementation_declared); |
| 1830 | } |
| 1831 | |
| 1832 | if (CategoryName) { |
| 1833 | |
| 1834 | if (ObjCCategoryDecl *Previous |
| 1835 | = IDecl->FindCategoryDeclaration(CategoryName)) { |
| 1836 | |
| 1837 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 1838 | << ClassName << CategoryName; |
| 1839 | Diag(Previous->getLocation(), diag::note_previous_definition); |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | |
| 1844 | if (typeParamList) { |
| 1845 | if (auto prevTypeParamList = IDecl->getTypeParamList()) { |
| 1846 | if (checkTypeParamListConsistency(*this, prevTypeParamList, typeParamList, |
| 1847 | CategoryName |
| 1848 | ? TypeParamListContext::Category |
| 1849 | : TypeParamListContext::Extension)) |
| 1850 | typeParamList = nullptr; |
| 1851 | } else { |
| 1852 | Diag(typeParamList->getLAngleLoc(), |
| 1853 | diag::err_objc_parameterized_category_nonclass) |
| 1854 | << (CategoryName != nullptr) |
| 1855 | << ClassName |
| 1856 | << typeParamList->getSourceRange(); |
| 1857 | |
| 1858 | typeParamList = nullptr; |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| 1863 | ClassLoc, CategoryLoc, CategoryName, IDecl, |
| 1864 | typeParamList); |
| 1865 | |
| 1866 | CurContext->addDecl(CDecl); |
| 1867 | |
| 1868 | |
| 1869 | |
| 1870 | |
| 1871 | ProcessDeclAttributeList(TUScope, CDecl, AttrList); |
| 1872 | AddPragmaAttributes(TUScope, CDecl); |
| 1873 | |
| 1874 | if (NumProtoRefs) { |
| 1875 | diagnoseUseOfProtocols(*this, CDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| 1876 | NumProtoRefs, ProtoLocs); |
| 1877 | CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
| 1878 | ProtoLocs, Context); |
| 1879 | |
| 1880 | if (CDecl->IsClassExtension()) |
| 1881 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs, |
| 1882 | NumProtoRefs, Context); |
| 1883 | } |
| 1884 | |
| 1885 | CheckObjCDeclScope(CDecl); |
| 1886 | return ActOnObjCContainerStartDefinition(CDecl); |
| 1887 | } |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | Decl *Sema::ActOnStartCategoryImplementation( |
| 1893 | SourceLocation AtCatImplLoc, |
| 1894 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 1895 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
| 1896 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
| 1897 | ObjCCategoryDecl *CatIDecl = nullptr; |
| 1898 | if (IDecl && IDecl->hasDefinition()) { |
| 1899 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 1900 | if (!CatIDecl) { |
| 1901 | |
| 1902 | |
| 1903 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, |
| 1904 | ClassLoc, CatLoc, |
| 1905 | CatName, IDecl, |
| 1906 | ); |
| 1907 | CatIDecl->setImplicit(); |
| 1908 | } |
| 1909 | } |
| 1910 | |
| 1911 | ObjCCategoryImplDecl *CDecl = |
| 1912 | ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl, |
| 1913 | ClassLoc, AtCatImplLoc, CatLoc); |
| 1914 | |
| 1915 | if (!IDecl) { |
| 1916 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
| 1917 | CDecl->setInvalidDecl(); |
| 1918 | } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1919 | diag::err_undef_interface)) { |
| 1920 | CDecl->setInvalidDecl(); |
| 1921 | } |
| 1922 | |
| 1923 | |
| 1924 | CurContext->addDecl(CDecl); |
| 1925 | |
| 1926 | |
| 1927 | |
| 1928 | if (IDecl && IDecl->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| 1929 | Diag(ClassLoc, diag::err_objc_runtime_visible_category) |
| 1930 | << IDecl->getDeclName(); |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | if (CatIDecl) { |
| 1935 | if (CatIDecl->getImplementation()) { |
| 1936 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 1937 | << CatName; |
| 1938 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 1939 | diag::note_previous_definition); |
| 1940 | CDecl->setInvalidDecl(); |
| 1941 | } else { |
| 1942 | CatIDecl->setImplementation(CDecl); |
| 1943 | |
| 1944 | |
| 1945 | DiagnoseObjCImplementedDeprecations(*this, CatIDecl, |
| 1946 | CDecl->getLocation()); |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | CheckObjCDeclScope(CDecl); |
| 1951 | return ActOnObjCContainerStartDefinition(CDecl); |
| 1952 | } |
| 1953 | |
| 1954 | Decl *Sema::ActOnStartClassImplementation( |
| 1955 | SourceLocation AtClassImplLoc, |
| 1956 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 1957 | IdentifierInfo *SuperClassname, |
| 1958 | SourceLocation SuperClassLoc) { |
| 1959 | ObjCInterfaceDecl *IDecl = nullptr; |
| 1960 | |
| 1961 | NamedDecl *PrevDecl |
| 1962 | = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| 1963 | forRedeclarationInCurContext()); |
| 1964 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
| 1965 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
| 1966 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 1967 | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
| 1968 | |
| 1969 | |
| 1970 | RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| 1971 | diag::warn_undef_interface); |
| 1972 | } else { |
| 1973 | |
| 1974 | |
| 1975 | ObjCInterfaceValidatorCCC CCC{}; |
| 1976 | TypoCorrection Corrected = |
| 1977 | CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc), |
| 1978 | LookupOrdinaryName, TUScope, nullptr, CCC, CTK_NonError); |
| 1979 | if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1980 | |
| 1981 | |
| 1982 | |
| 1983 | diagnoseTypo(Corrected, |
| 1984 | PDiag(diag::warn_undef_interface_suggest) << ClassName, |
| 1985 | ); |
| 1986 | } else { |
| 1987 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | |
| 1992 | ObjCInterfaceDecl *SDecl = nullptr; |
| 1993 | if (SuperClassname) { |
| 1994 | |
| 1995 | PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| 1996 | LookupOrdinaryName); |
| 1997 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
| 1998 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 1999 | << SuperClassname; |
| 2000 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 2001 | } else { |
| 2002 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 2003 | if (SDecl && !SDecl->hasDefinition()) |
| 2004 | SDecl = nullptr; |
| 2005 | if (!SDecl) |
| 2006 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 2007 | << SuperClassname << ClassName; |
| 2008 | else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) { |
| 2009 | |
| 2010 | |
| 2011 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
| 2012 | << SDecl->getDeclName(); |
| 2013 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | if (!IDecl) { |
| 2019 | |
| 2020 | |
| 2021 | |
| 2022 | |
| 2023 | |
| 2024 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
| 2025 | ClassName, , |
| 2026 | , ClassLoc, |
| 2027 | true); |
| 2028 | AddPragmaAttributes(TUScope, IDecl); |
| 2029 | IDecl->startDefinition(); |
| 2030 | if (SDecl) { |
| 2031 | IDecl->setSuperClass(Context.getTrivialTypeSourceInfo( |
| 2032 | Context.getObjCInterfaceType(SDecl), |
| 2033 | SuperClassLoc)); |
| 2034 | IDecl->setEndOfDefinitionLoc(SuperClassLoc); |
| 2035 | } else { |
| 2036 | IDecl->setEndOfDefinitionLoc(ClassLoc); |
| 2037 | } |
| 2038 | |
| 2039 | PushOnScopeChains(IDecl, TUScope); |
| 2040 | } else { |
| 2041 | |
| 2042 | |
| 2043 | |
| 2044 | if (!IDecl->hasDefinition()) |
| 2045 | IDecl->startDefinition(); |
| 2046 | } |
| 2047 | |
| 2048 | ObjCImplementationDecl* IMPDecl = |
| 2049 | ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl, |
| 2050 | ClassLoc, AtClassImplLoc, SuperClassLoc); |
| 2051 | |
| 2052 | if (CheckObjCDeclScope(IMPDecl)) |
| 2053 | return ActOnObjCContainerStartDefinition(IMPDecl); |
| 2054 | |
| 2055 | |
| 2056 | if (IDecl->getImplementation()) { |
| 2057 | |
| 2058 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
| 2059 | Diag(IDecl->getImplementation()->getLocation(), |
| 2060 | diag::note_previous_definition); |
| 2061 | IMPDecl->setInvalidDecl(); |
| 2062 | } else { |
| 2063 | IDecl->setImplementation(IMPDecl); |
| 2064 | PushOnScopeChains(IMPDecl, TUScope); |
| 2065 | |
| 2066 | |
| 2067 | DiagnoseObjCImplementedDeprecations(*this, IDecl, IMPDecl->getLocation()); |
| 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | |
| 2072 | if (IDecl->getSuperClass() && |
| 2073 | IDecl->getSuperClass()->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| 2074 | Diag(ClassLoc, diag::err_objc_runtime_visible_subclass) |
| 2075 | << IDecl->getDeclName() |
| 2076 | << IDecl->getSuperClass()->getDeclName(); |
| 2077 | } |
| 2078 | |
| 2079 | return ActOnObjCContainerStartDefinition(IMPDecl); |
| 2080 | } |
| 2081 | |
| 2082 | Sema::DeclGroupPtrTy |
| 2083 | Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) { |
| 2084 | SmallVector<Decl *, 64> DeclsInGroup; |
| 2085 | DeclsInGroup.reserve(Decls.size() + 1); |
| 2086 | |
| 2087 | for (unsigned i = 0, e = Decls.size(); i != e; ++i) { |
| 2088 | Decl *Dcl = Decls[i]; |
| 2089 | if (!Dcl) |
| 2090 | continue; |
| 2091 | if (Dcl->getDeclContext()->isFileContext()) |
| 2092 | Dcl->setTopLevelDeclInObjCContainer(); |
| 2093 | DeclsInGroup.push_back(Dcl); |
| 2094 | } |
| 2095 | |
| 2096 | DeclsInGroup.push_back(ObjCImpDecl); |
| 2097 | |
| 2098 | return BuildDeclaratorGroup(DeclsInGroup); |
| 2099 | } |
| 2100 | |
| 2101 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 2102 | ObjCIvarDecl **ivars, unsigned numIvars, |
| 2103 | SourceLocation RBrace) { |
| 2104 | (0) . __assert_fail ("ImpDecl && \"missing implementation decl\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 2104, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ImpDecl && "missing implementation decl"); |
| 2105 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
| 2106 | if (!IDecl) |
| 2107 | return; |
| 2108 | |
| 2109 | |
| 2110 | |
| 2111 | if (IDecl->isImplicitInterfaceDecl()) { |
| 2112 | IDecl->setEndOfDefinitionLoc(RBrace); |
| 2113 | |
| 2114 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
| 2115 | ivars[i]->setLexicalDeclContext(ImpDecl); |
| 2116 | IDecl->makeDeclVisibleInContext(ivars[i]); |
| 2117 | ImpDecl->addDecl(ivars[i]); |
| 2118 | } |
| 2119 | |
| 2120 | return; |
| 2121 | } |
| 2122 | |
| 2123 | if (numIvars == 0) |
| 2124 | return; |
| 2125 | |
| 2126 | (0) . __assert_fail ("ivars && \"missing @implementation ivars\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 2126, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ivars && "missing @implementation ivars"); |
| 2127 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
| 2128 | if (ImpDecl->getSuperClass()) |
| 2129 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 2130 | for (unsigned i = 0; i < numIvars; i++) { |
| 2131 | ObjCIvarDecl* ImplIvar = ivars[i]; |
| 2132 | if (const ObjCIvarDecl *ClsIvar = |
| 2133 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 2134 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 2135 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 2136 | continue; |
| 2137 | } |
| 2138 | |
| 2139 | for (const auto *CDecl : IDecl->visible_extensions()) { |
| 2140 | if (const ObjCIvarDecl *ClsExtIvar = |
| 2141 | CDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 2142 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 2143 | Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| 2144 | continue; |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | ImplIvar->setLexicalDeclContext(ImpDecl); |
| 2149 | IDecl->makeDeclVisibleInContext(ImplIvar); |
| 2150 | ImpDecl->addDecl(ImplIvar); |
| 2151 | } |
| 2152 | return; |
| 2153 | } |
| 2154 | |
| 2155 | |
| 2156 | |
| 2157 | unsigned j = 0; |
| 2158 | ObjCInterfaceDecl::ivar_iterator |
| 2159 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 2160 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
| 2161 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 2162 | ObjCIvarDecl* ClsIvar = *IVI; |
| 2163 | (0) . __assert_fail ("ImplIvar && \"missing implementation ivar\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 2163, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert (ImplIvar && "missing implementation ivar"); |
| 2164 | (0) . __assert_fail ("ClsIvar && \"missing class ivar\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 2164, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert (ClsIvar && "missing class ivar"); |
| 2165 | |
| 2166 | |
| 2167 | if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) { |
| 2168 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
| 2169 | << ImplIvar->getIdentifier() |
| 2170 | << ImplIvar->getType() << ClsIvar->getType(); |
| 2171 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 2172 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField() && |
| 2173 | ImplIvar->getBitWidthValue(Context) != |
| 2174 | ClsIvar->getBitWidthValue(Context)) { |
| 2175 | Diag(ImplIvar->getBitWidth()->getBeginLoc(), |
| 2176 | diag::err_conflicting_ivar_bitwidth) |
| 2177 | << ImplIvar->getIdentifier(); |
| 2178 | Diag(ClsIvar->getBitWidth()->getBeginLoc(), |
| 2179 | diag::note_previous_definition); |
| 2180 | } |
| 2181 | |
| 2182 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
| 2183 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
| 2184 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
| 2185 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 2186 | } |
| 2187 | --numIvars; |
| 2188 | } |
| 2189 | |
| 2190 | if (numIvars > 0) |
| 2191 | Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count); |
| 2192 | else if (IVI != IVE) |
| 2193 | Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count); |
| 2194 | } |
| 2195 | |
| 2196 | static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc, |
| 2197 | ObjCMethodDecl *method, |
| 2198 | bool &IncompleteImpl, |
| 2199 | unsigned DiagID, |
| 2200 | NamedDecl *NeededFor = nullptr) { |
| 2201 | |
| 2202 | if (method->getAvailability() == AR_Unavailable) |
| 2203 | return; |
| 2204 | |
| 2205 | |
| 2206 | |
| 2207 | |
| 2208 | |
| 2209 | |
| 2210 | { |
| 2211 | const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID); |
| 2212 | B << method; |
| 2213 | if (NeededFor) |
| 2214 | B << NeededFor; |
| 2215 | } |
| 2216 | |
| 2217 | |
| 2218 | SourceLocation MethodLoc = method->getBeginLoc(); |
| 2219 | if (MethodLoc.isValid()) |
| 2220 | S.Diag(MethodLoc, diag::note_method_declared_at) << method; |
| 2221 | } |
| 2222 | |
| 2223 | |
| 2224 | |
| 2225 | |
| 2226 | |
| 2227 | |
| 2228 | |
| 2229 | |
| 2230 | |
| 2231 | |
| 2232 | |
| 2233 | |
| 2234 | |
| 2235 | |
| 2236 | |
| 2237 | |
| 2238 | |
| 2239 | |
| 2240 | |
| 2241 | |
| 2242 | |
| 2243 | |
| 2244 | |
| 2245 | |
| 2246 | |
| 2247 | |
| 2248 | |
| 2249 | |
| 2250 | |
| 2251 | |
| 2252 | |
| 2253 | |
| 2254 | |
| 2255 | |
| 2256 | static bool isObjCTypeSubstitutable(ASTContext &Context, |
| 2257 | const ObjCObjectPointerType *A, |
| 2258 | const ObjCObjectPointerType *B, |
| 2259 | bool rejectId) { |
| 2260 | |
| 2261 | if (rejectId && B->isObjCIdType()) return false; |
| 2262 | |
| 2263 | |
| 2264 | |
| 2265 | |
| 2266 | |
| 2267 | if (B->isObjCQualifiedIdType()) { |
| 2268 | return A->isObjCQualifiedIdType() && |
| 2269 | Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0), |
| 2270 | QualType(B,0), |
| 2271 | false); |
| 2272 | } |
| 2273 | |
| 2274 | |
| 2275 | |
| 2276 | |
| 2277 | |
| 2278 | |
| 2279 | |
| 2280 | |
| 2281 | |
| 2282 | |
| 2283 | |
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | return Context.canAssignObjCInterfaces(A, B); |
| 2288 | } |
| 2289 | |
| 2290 | static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
| 2291 | return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); |
| 2292 | } |
| 2293 | |
| 2294 | |
| 2295 | static bool objcModifiersConflict(Decl::ObjCDeclQualifier x, |
| 2296 | Decl::ObjCDeclQualifier y) { |
| 2297 | return (x & ~Decl::OBJC_TQ_CSNullability) != |
| 2298 | (y & ~Decl::OBJC_TQ_CSNullability); |
| 2299 | } |
| 2300 | |
| 2301 | static bool CheckMethodOverrideReturn(Sema &S, |
| 2302 | ObjCMethodDecl *MethodImpl, |
| 2303 | ObjCMethodDecl *MethodDecl, |
| 2304 | bool IsProtocolMethodDecl, |
| 2305 | bool IsOverridingMode, |
| 2306 | bool Warn) { |
| 2307 | if (IsProtocolMethodDecl && |
| 2308 | objcModifiersConflict(MethodDecl->getObjCDeclQualifier(), |
| 2309 | MethodImpl->getObjCDeclQualifier())) { |
| 2310 | if (Warn) { |
| 2311 | S.Diag(MethodImpl->getLocation(), |
| 2312 | (IsOverridingMode |
| 2313 | ? diag::warn_conflicting_overriding_ret_type_modifiers |
| 2314 | : diag::warn_conflicting_ret_type_modifiers)) |
| 2315 | << MethodImpl->getDeclName() |
| 2316 | << MethodImpl->getReturnTypeSourceRange(); |
| 2317 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
| 2318 | << MethodDecl->getReturnTypeSourceRange(); |
| 2319 | } |
| 2320 | else |
| 2321 | return false; |
| 2322 | } |
| 2323 | if (Warn && IsOverridingMode && |
| 2324 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2325 | !S.Context.hasSameNullabilityTypeQualifier(MethodImpl->getReturnType(), |
| 2326 | MethodDecl->getReturnType(), |
| 2327 | false)) { |
| 2328 | auto nullabilityMethodImpl = |
| 2329 | *MethodImpl->getReturnType()->getNullability(S.Context); |
| 2330 | auto nullabilityMethodDecl = |
| 2331 | *MethodDecl->getReturnType()->getNullability(S.Context); |
| 2332 | S.Diag(MethodImpl->getLocation(), |
| 2333 | diag::warn_conflicting_nullability_attr_overriding_ret_types) |
| 2334 | << DiagNullabilityKind( |
| 2335 | nullabilityMethodImpl, |
| 2336 | ((MethodImpl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2337 | != 0)) |
| 2338 | << DiagNullabilityKind( |
| 2339 | nullabilityMethodDecl, |
| 2340 | ((MethodDecl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2341 | != 0)); |
| 2342 | S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
| 2343 | } |
| 2344 | |
| 2345 | if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(), |
| 2346 | MethodDecl->getReturnType())) |
| 2347 | return true; |
| 2348 | if (!Warn) |
| 2349 | return false; |
| 2350 | |
| 2351 | unsigned DiagID = |
| 2352 | IsOverridingMode ? diag::warn_conflicting_overriding_ret_types |
| 2353 | : diag::warn_conflicting_ret_types; |
| 2354 | |
| 2355 | |
| 2356 | |
| 2357 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 2358 | MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
| 2359 | if (const ObjCObjectPointerType *IfacePtrTy = |
| 2360 | MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
| 2361 | |
| 2362 | |
| 2363 | |
| 2364 | |
| 2365 | if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
| 2366 | return false; |
| 2367 | |
| 2368 | DiagID = |
| 2369 | IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types |
| 2370 | : diag::warn_non_covariant_ret_types; |
| 2371 | } |
| 2372 | } |
| 2373 | |
| 2374 | S.Diag(MethodImpl->getLocation(), DiagID) |
| 2375 | << MethodImpl->getDeclName() << MethodDecl->getReturnType() |
| 2376 | << MethodImpl->getReturnType() |
| 2377 | << MethodImpl->getReturnTypeSourceRange(); |
| 2378 | S.Diag(MethodDecl->getLocation(), IsOverridingMode |
| 2379 | ? diag::note_previous_declaration |
| 2380 | : diag::note_previous_definition) |
| 2381 | << MethodDecl->getReturnTypeSourceRange(); |
| 2382 | return false; |
| 2383 | } |
| 2384 | |
| 2385 | static bool CheckMethodOverrideParam(Sema &S, |
| 2386 | ObjCMethodDecl *MethodImpl, |
| 2387 | ObjCMethodDecl *MethodDecl, |
| 2388 | ParmVarDecl *ImplVar, |
| 2389 | ParmVarDecl *IfaceVar, |
| 2390 | bool IsProtocolMethodDecl, |
| 2391 | bool IsOverridingMode, |
| 2392 | bool Warn) { |
| 2393 | if (IsProtocolMethodDecl && |
| 2394 | objcModifiersConflict(ImplVar->getObjCDeclQualifier(), |
| 2395 | IfaceVar->getObjCDeclQualifier())) { |
| 2396 | if (Warn) { |
| 2397 | if (IsOverridingMode) |
| 2398 | S.Diag(ImplVar->getLocation(), |
| 2399 | diag::warn_conflicting_overriding_param_modifiers) |
| 2400 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 2401 | << MethodImpl->getDeclName(); |
| 2402 | else S.Diag(ImplVar->getLocation(), |
| 2403 | diag::warn_conflicting_param_modifiers) |
| 2404 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 2405 | << MethodImpl->getDeclName(); |
| 2406 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
| 2407 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| 2408 | } |
| 2409 | else |
| 2410 | return false; |
| 2411 | } |
| 2412 | |
| 2413 | QualType ImplTy = ImplVar->getType(); |
| 2414 | QualType IfaceTy = IfaceVar->getType(); |
| 2415 | if (Warn && IsOverridingMode && |
| 2416 | !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| 2417 | !S.Context.hasSameNullabilityTypeQualifier(ImplTy, IfaceTy, true)) { |
| 2418 | S.Diag(ImplVar->getLocation(), |
| 2419 | diag::warn_conflicting_nullability_attr_overriding_param_types) |
| 2420 | << DiagNullabilityKind( |
| 2421 | *ImplTy->getNullability(S.Context), |
| 2422 | ((ImplVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2423 | != 0)) |
| 2424 | << DiagNullabilityKind( |
| 2425 | *IfaceTy->getNullability(S.Context), |
| 2426 | ((IfaceVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| 2427 | != 0)); |
| 2428 | S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration); |
| 2429 | } |
| 2430 | if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
| 2431 | return true; |
| 2432 | |
| 2433 | if (!Warn) |
| 2434 | return false; |
| 2435 | unsigned DiagID = |
| 2436 | IsOverridingMode ? diag::warn_conflicting_overriding_param_types |
| 2437 | : diag::warn_conflicting_param_types; |
| 2438 | |
| 2439 | |
| 2440 | |
| 2441 | if (const ObjCObjectPointerType *ImplPtrTy = |
| 2442 | ImplTy->getAs<ObjCObjectPointerType>()) { |
| 2443 | if (const ObjCObjectPointerType *IfacePtrTy = |
| 2444 | IfaceTy->getAs<ObjCObjectPointerType>()) { |
| 2445 | |
| 2446 | |
| 2447 | |
| 2448 | |
| 2449 | if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
| 2450 | return false; |
| 2451 | |
| 2452 | DiagID = |
| 2453 | IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types |
| 2454 | : diag::warn_non_contravariant_param_types; |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | S.Diag(ImplVar->getLocation(), DiagID) |
| 2459 | << getTypeRange(ImplVar->getTypeSourceInfo()) |
| 2460 | << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
| 2461 | S.Diag(IfaceVar->getLocation(), |
| 2462 | (IsOverridingMode ? diag::note_previous_declaration |
| 2463 | : diag::note_previous_definition)) |
| 2464 | << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| 2465 | return false; |
| 2466 | } |
| 2467 | |
| 2468 | |
| 2469 | |
| 2470 | static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl, |
| 2471 | ObjCMethodDecl *decl) { |
| 2472 | ObjCMethodFamily implFamily = impl->getMethodFamily(); |
| 2473 | ObjCMethodFamily declFamily = decl->getMethodFamily(); |
| 2474 | if (implFamily == declFamily) return false; |
| 2475 | |
| 2476 | |
| 2477 | |
| 2478 | |
| 2479 | assert(implFamily == OMF_None || declFamily == OMF_None); |
| 2480 | |
| 2481 | |
| 2482 | if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true; |
| 2483 | |
| 2484 | const ObjCMethodDecl *unmatched = impl; |
| 2485 | ObjCMethodFamily family = declFamily; |
| 2486 | unsigned errorID = diag::err_arc_lost_method_convention; |
| 2487 | unsigned noteID = diag::note_arc_lost_method_convention; |
| 2488 | if (declFamily == OMF_None) { |
| 2489 | unmatched = decl; |
| 2490 | family = implFamily; |
| 2491 | errorID = diag::err_arc_gained_method_convention; |
| 2492 | noteID = diag::note_arc_gained_method_convention; |
| 2493 | } |
| 2494 | |
| 2495 | |
| 2496 | enum FamilySelector { |
| 2497 | F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new |
| 2498 | }; |
| 2499 | FamilySelector familySelector = FamilySelector(); |
| 2500 | |
| 2501 | switch (family) { |
| 2502 | case OMF_None: llvm_unreachable("logic error, no method convention"); |
| 2503 | case OMF_retain: |
| 2504 | case OMF_release: |
| 2505 | case OMF_autorelease: |
| 2506 | case OMF_dealloc: |
| 2507 | case OMF_finalize: |
| 2508 | case OMF_retainCount: |
| 2509 | case OMF_self: |
| 2510 | case OMF_initialize: |
| 2511 | case OMF_performSelector: |
| 2512 | |
| 2513 | |
| 2514 | return false; |
| 2515 | |
| 2516 | case OMF_init: familySelector = F_init; break; |
| 2517 | case OMF_alloc: familySelector = F_alloc; break; |
| 2518 | case OMF_copy: familySelector = F_copy; break; |
| 2519 | case OMF_mutableCopy: familySelector = F_mutableCopy; break; |
| 2520 | case OMF_new: familySelector = F_new; break; |
| 2521 | } |
| 2522 | |
| 2523 | enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn }; |
| 2524 | ReasonSelector reasonSelector; |
| 2525 | |
| 2526 | |
| 2527 | |
| 2528 | if (unmatched->getReturnType()->isObjCObjectPointerType()) { |
| 2529 | reasonSelector = R_UnrelatedReturn; |
| 2530 | } else { |
| 2531 | reasonSelector = R_NonObjectReturn; |
| 2532 | } |
| 2533 | |
| 2534 | S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector); |
| 2535 | S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector); |
| 2536 | |
| 2537 | return true; |
| 2538 | } |
| 2539 | |
| 2540 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 2541 | ObjCMethodDecl *MethodDecl, |
| 2542 | bool IsProtocolMethodDecl) { |
| 2543 | if (getLangOpts().ObjCAutoRefCount && |
| 2544 | checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl)) |
| 2545 | return; |
| 2546 | |
| 2547 | CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| 2548 | IsProtocolMethodDecl, false, |
| 2549 | true); |
| 2550 | |
| 2551 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
| 2552 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2553 | EF = MethodDecl->param_end(); |
| 2554 | IM != EM && IF != EF; ++IM, ++IF) { |
| 2555 | CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
| 2556 | IsProtocolMethodDecl, false, true); |
| 2557 | } |
| 2558 | |
| 2559 | if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
| 2560 | Diag(ImpMethodDecl->getLocation(), |
| 2561 | diag::warn_conflicting_variadic); |
| 2562 | Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method, |
| 2567 | ObjCMethodDecl *Overridden, |
| 2568 | bool IsProtocolMethodDecl) { |
| 2569 | |
| 2570 | CheckMethodOverrideReturn(*this, Method, Overridden, |
| 2571 | IsProtocolMethodDecl, true, |
| 2572 | true); |
| 2573 | |
| 2574 | for (ObjCMethodDecl::param_iterator IM = Method->param_begin(), |
| 2575 | IF = Overridden->param_begin(), EM = Method->param_end(), |
| 2576 | EF = Overridden->param_end(); |
| 2577 | IM != EM && IF != EF; ++IM, ++IF) { |
| 2578 | CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF, |
| 2579 | IsProtocolMethodDecl, true, true); |
| 2580 | } |
| 2581 | |
| 2582 | if (Method->isVariadic() != Overridden->isVariadic()) { |
| 2583 | Diag(Method->getLocation(), |
| 2584 | diag::warn_conflicting_overriding_variadic); |
| 2585 | Diag(Overridden->getLocation(), diag::note_previous_declaration); |
| 2586 | } |
| 2587 | } |
| 2588 | |
| 2589 | |
| 2590 | |
| 2591 | void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 2592 | ObjCMethodDecl *MethodDecl, |
| 2593 | bool IsProtocolMethodDecl) { |
| 2594 | |
| 2595 | |
| 2596 | |
| 2597 | if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional) |
| 2598 | return; |
| 2599 | |
| 2600 | |
| 2601 | if (MethodDecl->hasAttr<UnavailableAttr>() || |
| 2602 | MethodDecl->hasAttr<DeprecatedAttr>()) |
| 2603 | return; |
| 2604 | |
| 2605 | bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| 2606 | IsProtocolMethodDecl, false, false); |
| 2607 | if (match) |
| 2608 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
| 2609 | IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| 2610 | EF = MethodDecl->param_end(); |
| 2611 | IM != EM && IF != EF; ++IM, ++IF) { |
| 2612 | match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, |
| 2613 | *IM, *IF, |
| 2614 | IsProtocolMethodDecl, false, false); |
| 2615 | if (!match) |
| 2616 | break; |
| 2617 | } |
| 2618 | if (match) |
| 2619 | match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic()); |
| 2620 | if (match) |
| 2621 | match = !(MethodDecl->isClassMethod() && |
| 2622 | MethodDecl->getSelector() == GetNullarySelector("load", Context)); |
| 2623 | |
| 2624 | if (match) { |
| 2625 | Diag(ImpMethodDecl->getLocation(), |
| 2626 | diag::warn_category_method_impl_match); |
| 2627 | Diag(MethodDecl->getLocation(), diag::note_method_declared_at) |
| 2628 | << MethodDecl->getDeclName(); |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | |
| 2633 | |
| 2634 | |
| 2635 | |
| 2636 | |
| 2637 | |
| 2638 | typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet; |
| 2639 | typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet; |
| 2640 | |
| 2641 | static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl, |
| 2642 | ProtocolNameSet &PNS) { |
| 2643 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
| 2644 | PNS.insert(PDecl->getIdentifier()); |
| 2645 | for (const auto *PI : PDecl->protocols()) |
| 2646 | findProtocolsWithExplicitImpls(PI, PNS); |
| 2647 | } |
| 2648 | |
| 2649 | |
| 2650 | |
| 2651 | |
| 2652 | static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super, |
| 2653 | ProtocolNameSet &PNS) { |
| 2654 | if (!Super) |
| 2655 | return; |
| 2656 | |
| 2657 | for (const auto *I : Super->all_referenced_protocols()) |
| 2658 | findProtocolsWithExplicitImpls(I, PNS); |
| 2659 | |
| 2660 | findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS); |
| 2661 | } |
| 2662 | |
| 2663 | |
| 2664 | |
| 2665 | static void CheckProtocolMethodDefs(Sema &S, |
| 2666 | SourceLocation ImpLoc, |
| 2667 | ObjCProtocolDecl *PDecl, |
| 2668 | bool& IncompleteImpl, |
| 2669 | const Sema::SelectorSet &InsMap, |
| 2670 | const Sema::SelectorSet &ClsMap, |
| 2671 | ObjCContainerDecl *CDecl, |
| 2672 | LazyProtocolNameSet &ProtocolsExplictImpl) { |
| 2673 | ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
| 2674 | ObjCInterfaceDecl *IDecl = C ? C->getClassInterface() |
| 2675 | : dyn_cast<ObjCInterfaceDecl>(CDecl); |
| 2676 | (0) . __assert_fail ("IDecl && \"CheckProtocolMethodDefs - IDecl is null\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 2676, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
| 2677 | |
| 2678 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
| 2679 | ObjCInterfaceDecl *NSIDecl = nullptr; |
| 2680 | |
| 2681 | |
| 2682 | |
| 2683 | |
| 2684 | |
| 2685 | |
| 2686 | |
| 2687 | |
| 2688 | |
| 2689 | |
| 2690 | |
| 2691 | |
| 2692 | if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) { |
| 2693 | if (!ProtocolsExplictImpl) { |
| 2694 | ProtocolsExplictImpl.reset(new ProtocolNameSet); |
| 2695 | findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl); |
| 2696 | } |
| 2697 | if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) != |
| 2698 | ProtocolsExplictImpl->end()) |
| 2699 | return; |
| 2700 | |
| 2701 | |
| 2702 | |
| 2703 | Super = nullptr; |
| 2704 | } |
| 2705 | |
| 2706 | if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) { |
| 2707 | |
| 2708 | |
| 2709 | |
| 2710 | |
| 2711 | |
| 2712 | |
| 2713 | |
| 2714 | IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation"); |
| 2715 | Selector fISelector = S.Context.Selectors.getSelector(1, &II); |
| 2716 | if (InsMap.count(fISelector)) |
| 2717 | |
| 2718 | |
| 2719 | NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy")); |
| 2720 | } |
| 2721 | |
| 2722 | |
| 2723 | if (!PDecl->isThisDeclarationADefinition() && |
| 2724 | PDecl->getDefinition()) |
| 2725 | PDecl = PDecl->getDefinition(); |
| 2726 | |
| 2727 | |
| 2728 | |
| 2729 | |
| 2730 | |
| 2731 | |
| 2732 | |
| 2733 | if (!NSIDecl) |
| 2734 | for (auto *method : PDecl->instance_methods()) { |
| 2735 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 2736 | !method->isPropertyAccessor() && |
| 2737 | !InsMap.count(method->getSelector()) && |
| 2738 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2739 | true , |
| 2740 | false , |
| 2741 | true , |
| 2742 | nullptr ))) { |
| 2743 | |
| 2744 | |
| 2745 | |
| 2746 | |
| 2747 | |
| 2748 | |
| 2749 | |
| 2750 | |
| 2751 | |
| 2752 | if (ObjCMethodDecl *MethodInClass = |
| 2753 | IDecl->lookupMethod(method->getSelector(), |
| 2754 | true , |
| 2755 | true , |
| 2756 | false )) |
| 2757 | if (C || MethodInClass->isPropertyAccessor()) |
| 2758 | continue; |
| 2759 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
| 2760 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
| 2761 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, |
| 2762 | PDecl); |
| 2763 | } |
| 2764 | } |
| 2765 | } |
| 2766 | |
| 2767 | for (auto *method : PDecl->class_methods()) { |
| 2768 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 2769 | !ClsMap.count(method->getSelector()) && |
| 2770 | (!Super || !Super->lookupMethod(method->getSelector(), |
| 2771 | false , |
| 2772 | false , |
| 2773 | true , |
| 2774 | nullptr ))) { |
| 2775 | |
| 2776 | if (C && IDecl->lookupMethod(method->getSelector(), |
| 2777 | false , |
| 2778 | true , |
| 2779 | false )) |
| 2780 | continue; |
| 2781 | |
| 2782 | unsigned DIAG = diag::warn_unimplemented_protocol_method; |
| 2783 | if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
| 2784 | WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl); |
| 2785 | } |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | for (auto *PI : PDecl->protocols()) |
| 2790 | CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap, |
| 2791 | CDecl, ProtocolsExplictImpl); |
| 2792 | } |
| 2793 | |
| 2794 | |
| 2795 | |
| 2796 | |
| 2797 | void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap, |
| 2798 | const SelectorSet &ClsMap, |
| 2799 | SelectorSet &InsMapSeen, |
| 2800 | SelectorSet &ClsMapSeen, |
| 2801 | ObjCImplDecl* IMPDecl, |
| 2802 | ObjCContainerDecl* CDecl, |
| 2803 | bool &IncompleteImpl, |
| 2804 | bool ImmediateClass, |
| 2805 | bool WarnCategoryMethodImpl) { |
| 2806 | |
| 2807 | |
| 2808 | for (auto *I : CDecl->instance_methods()) { |
| 2809 | if (!InsMapSeen.insert(I->getSelector()).second) |
| 2810 | continue; |
| 2811 | if (!I->isPropertyAccessor() && |
| 2812 | !InsMap.count(I->getSelector())) { |
| 2813 | if (ImmediateClass) |
| 2814 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
| 2815 | diag::warn_undef_method_impl); |
| 2816 | continue; |
| 2817 | } else { |
| 2818 | ObjCMethodDecl *ImpMethodDecl = |
| 2819 | IMPDecl->getInstanceMethod(I->getSelector()); |
| 2820 | (0) . __assert_fail ("CDecl->getInstanceMethod(I->getSelector(), true ) && \"Expected to find the method through lookup as well\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 2821, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CDecl->getInstanceMethod(I->getSelector(), true) && |
| 2821 | (0) . __assert_fail ("CDecl->getInstanceMethod(I->getSelector(), true ) && \"Expected to find the method through lookup as well\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 2821, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expected to find the method through lookup as well"); |
| 2822 | |
| 2823 | if (ImpMethodDecl) { |
| 2824 | if (!WarnCategoryMethodImpl) |
| 2825 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
| 2826 | isa<ObjCProtocolDecl>(CDecl)); |
| 2827 | else if (!I->isPropertyAccessor()) |
| 2828 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | |
| 2834 | |
| 2835 | for (auto *I : CDecl->class_methods()) { |
| 2836 | if (!ClsMapSeen.insert(I->getSelector()).second) |
| 2837 | continue; |
| 2838 | if (!I->isPropertyAccessor() && |
| 2839 | !ClsMap.count(I->getSelector())) { |
| 2840 | if (ImmediateClass) |
| 2841 | WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl, |
| 2842 | diag::warn_undef_method_impl); |
| 2843 | } else { |
| 2844 | ObjCMethodDecl *ImpMethodDecl = |
| 2845 | IMPDecl->getClassMethod(I->getSelector()); |
| 2846 | (0) . __assert_fail ("CDecl->getClassMethod(I->getSelector(), true ) && \"Expected to find the method through lookup as well\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 2847, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CDecl->getClassMethod(I->getSelector(), true) && |
| 2847 | (0) . __assert_fail ("CDecl->getClassMethod(I->getSelector(), true ) && \"Expected to find the method through lookup as well\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 2847, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expected to find the method through lookup as well"); |
| 2848 | |
| 2849 | if (ImpMethodDecl) { |
| 2850 | if (!WarnCategoryMethodImpl) |
| 2851 | WarnConflictingTypedMethods(ImpMethodDecl, I, |
| 2852 | isa<ObjCProtocolDecl>(CDecl)); |
| 2853 | else if (!I->isPropertyAccessor()) |
| 2854 | WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl)); |
| 2855 | } |
| 2856 | } |
| 2857 | } |
| 2858 | |
| 2859 | if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) { |
| 2860 | |
| 2861 | |
| 2862 | for (auto *PI : PD->protocols()) |
| 2863 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2864 | IMPDecl, PI, IncompleteImpl, false, |
| 2865 | WarnCategoryMethodImpl); |
| 2866 | } |
| 2867 | |
| 2868 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
| 2869 | |
| 2870 | |
| 2871 | |
| 2872 | if (!WarnCategoryMethodImpl) { |
| 2873 | for (auto *Cat : I->visible_categories()) |
| 2874 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2875 | IMPDecl, Cat, IncompleteImpl, |
| 2876 | ImmediateClass && Cat->IsClassExtension(), |
| 2877 | WarnCategoryMethodImpl); |
| 2878 | } else { |
| 2879 | |
| 2880 | for (auto *Ext : I->visible_extensions()) |
| 2881 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2882 | IMPDecl, Ext, IncompleteImpl, false, |
| 2883 | WarnCategoryMethodImpl); |
| 2884 | } |
| 2885 | |
| 2886 | |
| 2887 | for (auto *PI : I->all_referenced_protocols()) |
| 2888 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2889 | IMPDecl, PI, IncompleteImpl, false, |
| 2890 | WarnCategoryMethodImpl); |
| 2891 | |
| 2892 | |
| 2893 | |
| 2894 | if (!WarnCategoryMethodImpl && I->getSuperClass()) |
| 2895 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2896 | IMPDecl, |
| 2897 | I->getSuperClass(), IncompleteImpl, false); |
| 2898 | } |
| 2899 | } |
| 2900 | |
| 2901 | |
| 2902 | |
| 2903 | |
| 2904 | void Sema::CheckCategoryVsClassMethodMatches( |
| 2905 | ObjCCategoryImplDecl *CatIMPDecl) { |
| 2906 | |
| 2907 | ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl(); |
| 2908 | if (!CatDecl) |
| 2909 | return; |
| 2910 | ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface(); |
| 2911 | if (!IDecl) |
| 2912 | return; |
| 2913 | ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass(); |
| 2914 | SelectorSet InsMap, ClsMap; |
| 2915 | |
| 2916 | for (const auto *I : CatIMPDecl->instance_methods()) { |
| 2917 | Selector Sel = I->getSelector(); |
| 2918 | |
| 2919 | |
| 2920 | |
| 2921 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true)) |
| 2922 | continue; |
| 2923 | InsMap.insert(Sel); |
| 2924 | } |
| 2925 | |
| 2926 | for (const auto *I : CatIMPDecl->class_methods()) { |
| 2927 | Selector Sel = I->getSelector(); |
| 2928 | if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false)) |
| 2929 | continue; |
| 2930 | ClsMap.insert(Sel); |
| 2931 | } |
| 2932 | if (InsMap.empty() && ClsMap.empty()) |
| 2933 | return; |
| 2934 | |
| 2935 | SelectorSet InsMapSeen, ClsMapSeen; |
| 2936 | bool IncompleteImpl = false; |
| 2937 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2938 | CatIMPDecl, IDecl, |
| 2939 | IncompleteImpl, false, |
| 2940 | true ); |
| 2941 | } |
| 2942 | |
| 2943 | void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl, |
| 2944 | ObjCContainerDecl* CDecl, |
| 2945 | bool IncompleteImpl) { |
| 2946 | SelectorSet InsMap; |
| 2947 | |
| 2948 | |
| 2949 | for (const auto *I : IMPDecl->instance_methods()) |
| 2950 | InsMap.insert(I->getSelector()); |
| 2951 | |
| 2952 | |
| 2953 | for (const auto *PImpl : IMPDecl->property_impls()) { |
| 2954 | |
| 2955 | if (PImpl->getPropertyImplementation() != ObjCPropertyImplDecl::Dynamic) |
| 2956 | continue; |
| 2957 | |
| 2958 | const auto *P = PImpl->getPropertyDecl(); |
| 2959 | if (!P) continue; |
| 2960 | |
| 2961 | InsMap.insert(P->getGetterName()); |
| 2962 | if (!P->getSetterName().isNull()) |
| 2963 | InsMap.insert(P->getSetterName()); |
| 2964 | } |
| 2965 | |
| 2966 | |
| 2967 | |
| 2968 | |
| 2969 | if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 2970 | bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties && |
| 2971 | LangOpts.ObjCRuntime.isNonFragile() && |
| 2972 | !IDecl->isObjCRequiresPropertyDefs(); |
| 2973 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties); |
| 2974 | } |
| 2975 | |
| 2976 | |
| 2977 | diagnoseNullResettableSynthesizedSetters(IMPDecl); |
| 2978 | |
| 2979 | SelectorSet ClsMap; |
| 2980 | for (const auto *I : IMPDecl->class_methods()) |
| 2981 | ClsMap.insert(I->getSelector()); |
| 2982 | |
| 2983 | |
| 2984 | |
| 2985 | SelectorSet InsMapSeen, ClsMapSeen; |
| 2986 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 2987 | IMPDecl, CDecl, |
| 2988 | IncompleteImpl, true); |
| 2989 | |
| 2990 | |
| 2991 | |
| 2992 | if (ObjCCategoryImplDecl *CatDecl = |
| 2993 | dyn_cast<ObjCCategoryImplDecl>(IMPDecl)) |
| 2994 | CheckCategoryVsClassMethodMatches(CatDecl); |
| 2995 | |
| 2996 | |
| 2997 | |
| 2998 | |
| 2999 | |
| 3000 | |
| 3001 | LazyProtocolNameSet ExplicitImplProtocols; |
| 3002 | |
| 3003 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
| 3004 | for (auto *PI : I->all_referenced_protocols()) |
| 3005 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl, |
| 3006 | InsMap, ClsMap, I, ExplicitImplProtocols); |
| 3007 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 3008 | |
| 3009 | |
| 3010 | if (!C->IsClassExtension()) { |
| 3011 | for (auto *P : C->protocols()) |
| 3012 | CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P, |
| 3013 | IncompleteImpl, InsMap, ClsMap, CDecl, |
| 3014 | ExplicitImplProtocols); |
| 3015 | DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, |
| 3016 | ); |
| 3017 | } |
| 3018 | } else |
| 3019 | llvm_unreachable("invalid ObjCContainerDecl type."); |
| 3020 | } |
| 3021 | |
| 3022 | Sema::DeclGroupPtrTy |
| 3023 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
| 3024 | IdentifierInfo **IdentList, |
| 3025 | SourceLocation *IdentLocs, |
| 3026 | ArrayRef<ObjCTypeParamList *> TypeParamLists, |
| 3027 | unsigned NumElts) { |
| 3028 | SmallVector<Decl *, 8> DeclsInGroup; |
| 3029 | for (unsigned i = 0; i != NumElts; ++i) { |
| 3030 | |
| 3031 | NamedDecl *PrevDecl |
| 3032 | = LookupSingleName(TUScope, IdentList[i], IdentLocs[i], |
| 3033 | LookupOrdinaryName, forRedeclarationInCurContext()); |
| 3034 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
| 3035 | |
| 3036 | |
| 3037 | |
| 3038 | |
| 3039 | |
| 3040 | |
| 3041 | |
| 3042 | TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl); |
| 3043 | if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) { |
| 3044 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
| 3045 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3046 | } else { |
| 3047 | |
| 3048 | |
| 3049 | |
| 3050 | |
| 3051 | if (isa<ObjCObjectType>(TDD->getUnderlyingType())) { |
| 3052 | Diag(AtClassLoc, diag::warn_forward_class_redefinition) |
| 3053 | << IdentList[i]; |
| 3054 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 3055 | continue; |
| 3056 | } |
| 3057 | } |
| 3058 | } |
| 3059 | |
| 3060 | |
| 3061 | ObjCInterfaceDecl *PrevIDecl |
| 3062 | = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| 3063 | |
| 3064 | IdentifierInfo *ClassName = IdentList[i]; |
| 3065 | if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| 3066 | |
| 3067 | |
| 3068 | |
| 3069 | |
| 3070 | |
| 3071 | |
| 3072 | |
| 3073 | |
| 3074 | |
| 3075 | |
| 3076 | |
| 3077 | |
| 3078 | ClassName = PrevIDecl->getIdentifier(); |
| 3079 | } |
| 3080 | |
| 3081 | |
| 3082 | |
| 3083 | ObjCTypeParamList *TypeParams = TypeParamLists[i]; |
| 3084 | if (PrevIDecl && TypeParams) { |
| 3085 | if (ObjCTypeParamList *PrevTypeParams = PrevIDecl->getTypeParamList()) { |
| 3086 | |
| 3087 | if (checkTypeParamListConsistency( |
| 3088 | *this, PrevTypeParams, TypeParams, |
| 3089 | TypeParamListContext::ForwardDeclaration)) { |
| 3090 | TypeParams = nullptr; |
| 3091 | } |
| 3092 | } else if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| 3093 | |
| 3094 | Diag(IdentLocs[i], diag::err_objc_parameterized_forward_class) |
| 3095 | << ClassName |
| 3096 | << TypeParams->getSourceRange(); |
| 3097 | Diag(Def->getLocation(), diag::note_defined_here) |
| 3098 | << ClassName; |
| 3099 | |
| 3100 | TypeParams = nullptr; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | ObjCInterfaceDecl *IDecl |
| 3105 | = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
| 3106 | ClassName, TypeParams, PrevIDecl, |
| 3107 | IdentLocs[i]); |
| 3108 | IDecl->setAtEndRange(IdentLocs[i]); |
| 3109 | |
| 3110 | PushOnScopeChains(IDecl, TUScope); |
| 3111 | CheckObjCDeclScope(IDecl); |
| 3112 | DeclsInGroup.push_back(IDecl); |
| 3113 | } |
| 3114 | |
| 3115 | return BuildDeclaratorGroup(DeclsInGroup); |
| 3116 | } |
| 3117 | |
| 3118 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 3119 | Sema::MethodMatchStrategy strategy, |
| 3120 | const Type *left, const Type *right); |
| 3121 | |
| 3122 | static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy, |
| 3123 | QualType leftQT, QualType rightQT) { |
| 3124 | const Type *left = |
| 3125 | Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr(); |
| 3126 | const Type *right = |
| 3127 | Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr(); |
| 3128 | |
| 3129 | if (left == right) return true; |
| 3130 | |
| 3131 | |
| 3132 | if (strategy == Sema::MMS_strict) return false; |
| 3133 | |
| 3134 | if (left->isIncompleteType() || right->isIncompleteType()) return false; |
| 3135 | |
| 3136 | |
| 3137 | |
| 3138 | |
| 3139 | |
| 3140 | TypeInfo LeftTI = Context.getTypeInfo(left); |
| 3141 | TypeInfo RightTI = Context.getTypeInfo(right); |
| 3142 | if (LeftTI.Width != RightTI.Width) |
| 3143 | return false; |
| 3144 | |
| 3145 | if (LeftTI.Align != RightTI.Align) |
| 3146 | return false; |
| 3147 | |
| 3148 | |
| 3149 | |
| 3150 | |
| 3151 | |
| 3152 | if (isa<VectorType>(left)) return isa<VectorType>(right); |
| 3153 | if (isa<VectorType>(right)) return false; |
| 3154 | |
| 3155 | |
| 3156 | |
| 3157 | |
| 3158 | |
| 3159 | if (!left->isScalarType() || !right->isScalarType()) |
| 3160 | return tryMatchRecordTypes(Context, strategy, left, right); |
| 3161 | |
| 3162 | |
| 3163 | |
| 3164 | Type::ScalarTypeKind leftSK = left->getScalarTypeKind(); |
| 3165 | Type::ScalarTypeKind rightSK = right->getScalarTypeKind(); |
| 3166 | if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral; |
| 3167 | if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral; |
| 3168 | if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer) |
| 3169 | leftSK = Type::STK_ObjCObjectPointer; |
| 3170 | if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer) |
| 3171 | rightSK = Type::STK_ObjCObjectPointer; |
| 3172 | |
| 3173 | |
| 3174 | |
| 3175 | |
| 3176 | return (leftSK == rightSK); |
| 3177 | } |
| 3178 | |
| 3179 | static bool tryMatchRecordTypes(ASTContext &Context, |
| 3180 | Sema::MethodMatchStrategy strategy, |
| 3181 | const Type *lt, const Type *rt) { |
| 3182 | assert(lt && rt && lt != rt); |
| 3183 | |
| 3184 | if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false; |
| 3185 | RecordDecl *left = cast<RecordType>(lt)->getDecl(); |
| 3186 | RecordDecl *right = cast<RecordType>(rt)->getDecl(); |
| 3187 | |
| 3188 | |
| 3189 | if (left->isUnion() != right->isUnion()) return false; |
| 3190 | |
| 3191 | |
| 3192 | if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) || |
| 3193 | (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD())) |
| 3194 | return false; |
| 3195 | |
| 3196 | |
| 3197 | TypeInfo LeftTI = Context.getTypeInfo(lt); |
| 3198 | TypeInfo RightTI = Context.getTypeInfo(rt); |
| 3199 | if (LeftTI.Width != RightTI.Width) |
| 3200 | return false; |
| 3201 | |
| 3202 | if (LeftTI.Align != RightTI.Align) |
| 3203 | return false; |
| 3204 | |
| 3205 | |
| 3206 | RecordDecl::field_iterator li = left->field_begin(), le = left->field_end(); |
| 3207 | RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end(); |
| 3208 | for (; li != le && ri != re; ++li, ++ri) { |
| 3209 | if (!matchTypes(Context, strategy, li->getType(), ri->getType())) |
| 3210 | return false; |
| 3211 | } |
| 3212 | return (li == le && ri == re); |
| 3213 | } |
| 3214 | |
| 3215 | |
| 3216 | |
| 3217 | |
| 3218 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left, |
| 3219 | const ObjCMethodDecl *right, |
| 3220 | MethodMatchStrategy strategy) { |
| 3221 | if (!matchTypes(Context, strategy, left->getReturnType(), |
| 3222 | right->getReturnType())) |
| 3223 | return false; |
| 3224 | |
| 3225 | |
| 3226 | if (left->isHidden() || right->isHidden()) |
| 3227 | return false; |
| 3228 | |
| 3229 | if (getLangOpts().ObjCAutoRefCount && |
| 3230 | (left->hasAttr<NSReturnsRetainedAttr>() |
| 3231 | != right->hasAttr<NSReturnsRetainedAttr>() || |
| 3232 | left->hasAttr<NSConsumesSelfAttr>() |
| 3233 | != right->hasAttr<NSConsumesSelfAttr>())) |
| 3234 | return false; |
| 3235 | |
| 3236 | ObjCMethodDecl::param_const_iterator |
| 3237 | li = left->param_begin(), le = left->param_end(), ri = right->param_begin(), |
| 3238 | re = right->param_end(); |
| 3239 | |
| 3240 | for (; li != le && ri != re; ++li, ++ri) { |
| 3241 | (0) . __assert_fail ("ri != right->param_end() && \"Param mismatch\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 3241, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ri != right->param_end() && "Param mismatch"); |
| 3242 | const ParmVarDecl *lparm = *li, *rparm = *ri; |
| 3243 | |
| 3244 | if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType())) |
| 3245 | return false; |
| 3246 | |
| 3247 | if (getLangOpts().ObjCAutoRefCount && |
| 3248 | lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>()) |
| 3249 | return false; |
| 3250 | } |
| 3251 | return true; |
| 3252 | } |
| 3253 | |
| 3254 | static bool isMethodContextSameForKindofLookup(ObjCMethodDecl *Method, |
| 3255 | ObjCMethodDecl *MethodInList) { |
| 3256 | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
| 3257 | auto *MethodInListProtocol = |
| 3258 | dyn_cast<ObjCProtocolDecl>(MethodInList->getDeclContext()); |
| 3259 | |
| 3260 | |
| 3261 | if ((MethodProtocol && !MethodInListProtocol) || |
| 3262 | (!MethodProtocol && MethodInListProtocol)) |
| 3263 | return false; |
| 3264 | |
| 3265 | if (MethodProtocol && MethodInListProtocol) |
| 3266 | return true; |
| 3267 | |
| 3268 | ObjCInterfaceDecl *MethodInterface = Method->getClassInterface(); |
| 3269 | ObjCInterfaceDecl *MethodInListInterface = |
| 3270 | MethodInList->getClassInterface(); |
| 3271 | return MethodInterface == MethodInListInterface; |
| 3272 | } |
| 3273 | |
| 3274 | void Sema::addMethodToGlobalList(ObjCMethodList *List, |
| 3275 | ObjCMethodDecl *Method) { |
| 3276 | |
| 3277 | |
| 3278 | if (ObjCCategoryDecl *CD = |
| 3279 | dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) |
| 3280 | if (!CD->IsClassExtension() && List->getBits() < 2) |
| 3281 | List->setBits(List->getBits() + 1); |
| 3282 | |
| 3283 | |
| 3284 | if (List->getMethod() == nullptr) { |
| 3285 | List->setMethod(Method); |
| 3286 | List->setNext(nullptr); |
| 3287 | return; |
| 3288 | } |
| 3289 | |
| 3290 | |
| 3291 | |
| 3292 | ObjCMethodList *Previous = List; |
| 3293 | ObjCMethodList *ListWithSameDeclaration = nullptr; |
| 3294 | for (; List; Previous = List, List = List->getNext()) { |
| 3295 | |
| 3296 | if (getLangOpts().isCompilingModule()) |
| 3297 | continue; |
| 3298 | |
| 3299 | bool SameDeclaration = MatchTwoMethodDeclarations(Method, |
| 3300 | List->getMethod()); |
| 3301 | |
| 3302 | |
| 3303 | |
| 3304 | |
| 3305 | |
| 3306 | |
| 3307 | |
| 3308 | |
| 3309 | |
| 3310 | |
| 3311 | if (!SameDeclaration || |
| 3312 | !isMethodContextSameForKindofLookup(Method, List->getMethod())) { |
| 3313 | |
| 3314 | |
| 3315 | |
| 3316 | if (!Method->isDefined()) |
| 3317 | List->setHasMoreThanOneDecl(true); |
| 3318 | |
| 3319 | |
| 3320 | |
| 3321 | if (Method->isDeprecated() && SameDeclaration && |
| 3322 | !ListWithSameDeclaration && !List->getMethod()->isDeprecated()) |
| 3323 | ListWithSameDeclaration = List; |
| 3324 | |
| 3325 | if (Method->isUnavailable() && SameDeclaration && |
| 3326 | !ListWithSameDeclaration && |
| 3327 | List->getMethod()->getAvailability() < AR_Deprecated) |
| 3328 | ListWithSameDeclaration = List; |
| 3329 | continue; |
| 3330 | } |
| 3331 | |
| 3332 | ObjCMethodDecl *PrevObjCMethod = List->getMethod(); |
| 3333 | |
| 3334 | |
| 3335 | if (Method->isDefined()) |
| 3336 | PrevObjCMethod->setDefined(true); |
| 3337 | else { |
| 3338 | |
| 3339 | |
| 3340 | |
| 3341 | |
| 3342 | List->setHasMoreThanOneDecl(true); |
| 3343 | } |
| 3344 | |
| 3345 | |
| 3346 | |
| 3347 | if (Method->isDeprecated()) { |
| 3348 | if (!PrevObjCMethod->isDeprecated()) |
| 3349 | List->setMethod(Method); |
| 3350 | } |
| 3351 | |
| 3352 | |
| 3353 | if (Method->isUnavailable()) { |
| 3354 | if (PrevObjCMethod->getAvailability() < AR_Deprecated) |
| 3355 | List->setMethod(Method); |
| 3356 | } |
| 3357 | |
| 3358 | return; |
| 3359 | } |
| 3360 | |
| 3361 | |
| 3362 | |
| 3363 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
| 3364 | |
| 3365 | |
| 3366 | if (ListWithSameDeclaration) { |
| 3367 | auto *List = new (Mem) ObjCMethodList(*ListWithSameDeclaration); |
| 3368 | |
| 3369 | ListWithSameDeclaration->setMethod(Method); |
| 3370 | ListWithSameDeclaration->setNext(List); |
| 3371 | return; |
| 3372 | } |
| 3373 | |
| 3374 | Previous->setNext(new (Mem) ObjCMethodList(Method)); |
| 3375 | } |
| 3376 | |
| 3377 | |
| 3378 | |
| 3379 | void Sema::ReadMethodPool(Selector Sel) { |
| 3380 | (0) . __assert_fail ("ExternalSource && \"We need an external AST source\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 3380, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ExternalSource && "We need an external AST source"); |
| 3381 | ExternalSource->ReadMethodPool(Sel); |
| 3382 | } |
| 3383 | |
| 3384 | void Sema::updateOutOfDateSelector(Selector Sel) { |
| 3385 | if (!ExternalSource) |
| 3386 | return; |
| 3387 | ExternalSource->updateOutOfDateSelector(Sel); |
| 3388 | } |
| 3389 | |
| 3390 | void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl, |
| 3391 | bool instance) { |
| 3392 | |
| 3393 | if (cast<Decl>(Method->getDeclContext())->isInvalidDecl()) |
| 3394 | return; |
| 3395 | |
| 3396 | if (ExternalSource) |
| 3397 | ReadMethodPool(Method->getSelector()); |
| 3398 | |
| 3399 | GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector()); |
| 3400 | if (Pos == MethodPool.end()) |
| 3401 | Pos = MethodPool.insert(std::make_pair(Method->getSelector(), |
| 3402 | GlobalMethods())).first; |
| 3403 | |
| 3404 | Method->setDefined(impl); |
| 3405 | |
| 3406 | ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second; |
| 3407 | addMethodToGlobalList(&Entry, Method); |
| 3408 | } |
| 3409 | |
| 3410 | |
| 3411 | |
| 3412 | |
| 3413 | |
| 3414 | |
| 3415 | static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen, |
| 3416 | ObjCMethodDecl *other) { |
| 3417 | if (!chosen->isInstanceMethod()) |
| 3418 | return false; |
| 3419 | |
| 3420 | Selector sel = chosen->getSelector(); |
| 3421 | if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length") |
| 3422 | return false; |
| 3423 | |
| 3424 | |
| 3425 | |
| 3426 | return (chosen->getReturnType()->isIntegerType()); |
| 3427 | } |
| 3428 | |
| 3429 | |
| 3430 | static bool FilterMethodsByTypeBound(ObjCMethodDecl *Method, |
| 3431 | const ObjCObjectType *TypeBound) { |
| 3432 | if (!TypeBound) |
| 3433 | return true; |
| 3434 | |
| 3435 | if (TypeBound->isObjCId()) |
| 3436 | |
| 3437 | return true; |
| 3438 | |
| 3439 | auto *BoundInterface = TypeBound->getInterface(); |
| 3440 | (0) . __assert_fail ("BoundInterface && \"unexpected object type!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 3440, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BoundInterface && "unexpected object type!"); |
| 3441 | |
| 3442 | |
| 3443 | |
| 3444 | auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext()); |
| 3445 | if (MethodProtocol) { |
| 3446 | return true; |
| 3447 | } |
| 3448 | |
| 3449 | |
| 3450 | |
| 3451 | if (ObjCInterfaceDecl *MethodInterface = Method->getClassInterface()) { |
| 3452 | |
| 3453 | |
| 3454 | |
| 3455 | return MethodInterface == BoundInterface || |
| 3456 | MethodInterface->isSuperClassOf(BoundInterface) || |
| 3457 | BoundInterface->isSuperClassOf(MethodInterface); |
| 3458 | } |
| 3459 | llvm_unreachable("unknown method context"); |
| 3460 | } |
| 3461 | |
| 3462 | |
| 3463 | |
| 3464 | bool Sema::CollectMultipleMethodsInGlobalPool( |
| 3465 | Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, |
| 3466 | bool InstanceFirst, bool CheckTheOther, |
| 3467 | const ObjCObjectType *TypeBound) { |
| 3468 | if (ExternalSource) |
| 3469 | ReadMethodPool(Sel); |
| 3470 | |
| 3471 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3472 | if (Pos == MethodPool.end()) |
| 3473 | return false; |
| 3474 | |
| 3475 | |
| 3476 | ObjCMethodList &MethList = InstanceFirst ? Pos->second.first : |
| 3477 | Pos->second.second; |
| 3478 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) |
| 3479 | if (M->getMethod() && !M->getMethod()->isHidden()) { |
| 3480 | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
| 3481 | Methods.push_back(M->getMethod()); |
| 3482 | } |
| 3483 | |
| 3484 | |
| 3485 | if (!Methods.empty()) |
| 3486 | return Methods.size() > 1; |
| 3487 | |
| 3488 | if (!CheckTheOther) |
| 3489 | return false; |
| 3490 | |
| 3491 | |
| 3492 | ObjCMethodList &MethList2 = InstanceFirst ? Pos->second.second : |
| 3493 | Pos->second.first; |
| 3494 | for (ObjCMethodList *M = &MethList2; M; M = M->getNext()) |
| 3495 | if (M->getMethod() && !M->getMethod()->isHidden()) { |
| 3496 | if (FilterMethodsByTypeBound(M->getMethod(), TypeBound)) |
| 3497 | Methods.push_back(M->getMethod()); |
| 3498 | } |
| 3499 | |
| 3500 | return Methods.size() > 1; |
| 3501 | } |
| 3502 | |
| 3503 | bool Sema::AreMultipleMethodsInGlobalPool( |
| 3504 | Selector Sel, ObjCMethodDecl *BestMethod, SourceRange R, |
| 3505 | bool receiverIdOrClass, SmallVectorImpl<ObjCMethodDecl *> &Methods) { |
| 3506 | |
| 3507 | SmallVector<ObjCMethodDecl *, 4> FilteredMethods; |
| 3508 | FilteredMethods.push_back(BestMethod); |
| 3509 | |
| 3510 | for (auto *M : Methods) |
| 3511 | if (M != BestMethod && !M->hasAttr<UnavailableAttr>()) |
| 3512 | FilteredMethods.push_back(M); |
| 3513 | |
| 3514 | if (FilteredMethods.size() > 1) |
| 3515 | DiagnoseMultipleMethodInGlobalPool(FilteredMethods, Sel, R, |
| 3516 | receiverIdOrClass); |
| 3517 | |
| 3518 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3519 | |
| 3520 | |
| 3521 | if (Pos == MethodPool.end()) |
| 3522 | return true; |
| 3523 | ObjCMethodList &MethList = |
| 3524 | BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second; |
| 3525 | return MethList.hasMoreThanOneDecl(); |
| 3526 | } |
| 3527 | |
| 3528 | ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R, |
| 3529 | bool receiverIdOrClass, |
| 3530 | bool instance) { |
| 3531 | if (ExternalSource) |
| 3532 | ReadMethodPool(Sel); |
| 3533 | |
| 3534 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3535 | if (Pos == MethodPool.end()) |
| 3536 | return nullptr; |
| 3537 | |
| 3538 | |
| 3539 | ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second; |
| 3540 | SmallVector<ObjCMethodDecl *, 4> Methods; |
| 3541 | for (ObjCMethodList *M = &MethList; M; M = M->getNext()) { |
| 3542 | if (M->getMethod() && !M->getMethod()->isHidden()) |
| 3543 | return M->getMethod(); |
| 3544 | } |
| 3545 | return nullptr; |
| 3546 | } |
| 3547 | |
| 3548 | void Sema::DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods, |
| 3549 | Selector Sel, SourceRange R, |
| 3550 | bool receiverIdOrClass) { |
| 3551 | |
| 3552 | bool issueDiagnostic = false, issueError = false; |
| 3553 | |
| 3554 | |
| 3555 | |
| 3556 | bool strictSelectorMatch = |
| 3557 | receiverIdOrClass && |
| 3558 | !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin()); |
| 3559 | if (strictSelectorMatch) { |
| 3560 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3561 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) { |
| 3562 | issueDiagnostic = true; |
| 3563 | break; |
| 3564 | } |
| 3565 | } |
| 3566 | } |
| 3567 | |
| 3568 | |
| 3569 | |
| 3570 | |
| 3571 | if (!strictSelectorMatch || |
| 3572 | (issueDiagnostic && getLangOpts().ObjCAutoRefCount)) |
| 3573 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3574 | |
| 3575 | if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) && |
| 3576 | !isAcceptableMethodMismatch(Methods[0], Methods[I])) { |
| 3577 | issueDiagnostic = true; |
| 3578 | if (getLangOpts().ObjCAutoRefCount) |
| 3579 | issueError = true; |
| 3580 | break; |
| 3581 | } |
| 3582 | } |
| 3583 | |
| 3584 | if (issueDiagnostic) { |
| 3585 | if (issueError) |
| 3586 | Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R; |
| 3587 | else if (strictSelectorMatch) |
| 3588 | Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R; |
| 3589 | else |
| 3590 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
| 3591 | |
| 3592 | Diag(Methods[0]->getBeginLoc(), |
| 3593 | issueError ? diag::note_possibility : diag::note_using) |
| 3594 | << Methods[0]->getSourceRange(); |
| 3595 | for (unsigned I = 1, N = Methods.size(); I != N; ++I) { |
| 3596 | Diag(Methods[I]->getBeginLoc(), diag::note_also_found) |
| 3597 | << Methods[I]->getSourceRange(); |
| 3598 | } |
| 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) { |
| 3603 | GlobalMethodPool::iterator Pos = MethodPool.find(Sel); |
| 3604 | if (Pos == MethodPool.end()) |
| 3605 | return nullptr; |
| 3606 | |
| 3607 | GlobalMethods &Methods = Pos->second; |
| 3608 | for (const ObjCMethodList *Method = &Methods.first; Method; |
| 3609 | Method = Method->getNext()) |
| 3610 | if (Method->getMethod() && |
| 3611 | (Method->getMethod()->isDefined() || |
| 3612 | Method->getMethod()->isPropertyAccessor())) |
| 3613 | return Method->getMethod(); |
| 3614 | |
| 3615 | for (const ObjCMethodList *Method = &Methods.second; Method; |
| 3616 | Method = Method->getNext()) |
| 3617 | if (Method->getMethod() && |
| 3618 | (Method->getMethod()->isDefined() || |
| 3619 | Method->getMethod()->isPropertyAccessor())) |
| 3620 | return Method->getMethod(); |
| 3621 | return nullptr; |
| 3622 | } |
| 3623 | |
| 3624 | static void |
| 3625 | HelperSelectorsForTypoCorrection( |
| 3626 | SmallVectorImpl<const ObjCMethodDecl *> &BestMethod, |
| 3627 | StringRef Typo, const ObjCMethodDecl * Method) { |
| 3628 | const unsigned MaxEditDistance = 1; |
| 3629 | unsigned BestEditDistance = MaxEditDistance + 1; |
| 3630 | std::string MethodName = Method->getSelector().getAsString(); |
| 3631 | |
| 3632 | unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size()); |
| 3633 | if (MinPossibleEditDistance > 0 && |
| 3634 | Typo.size() / MinPossibleEditDistance < 1) |
| 3635 | return; |
| 3636 | unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance); |
| 3637 | if (EditDistance > MaxEditDistance) |
| 3638 | return; |
| 3639 | if (EditDistance == BestEditDistance) |
| 3640 | BestMethod.push_back(Method); |
| 3641 | else if (EditDistance < BestEditDistance) { |
| 3642 | BestMethod.clear(); |
| 3643 | BestMethod.push_back(Method); |
| 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | static bool HelperIsMethodInObjCType(Sema &S, Selector Sel, |
| 3648 | QualType ObjectType) { |
| 3649 | if (ObjectType.isNull()) |
| 3650 | return true; |
| 3651 | if (S.LookupMethodInObjectType(Sel, ObjectType, true)) |
| 3652 | return true; |
| 3653 | return S.LookupMethodInObjectType(Sel, ObjectType, false) != |
| 3654 | nullptr; |
| 3655 | } |
| 3656 | |
| 3657 | const ObjCMethodDecl * |
| 3658 | Sema::SelectorsForTypoCorrection(Selector Sel, |
| 3659 | QualType ObjectType) { |
| 3660 | unsigned NumArgs = Sel.getNumArgs(); |
| 3661 | SmallVector<const ObjCMethodDecl *, 8> Methods; |
| 3662 | bool ObjectIsId = true, ObjectIsClass = true; |
| 3663 | if (ObjectType.isNull()) |
| 3664 | ObjectIsId = ObjectIsClass = false; |
| 3665 | else if (!ObjectType->isObjCObjectPointerType()) |
| 3666 | return nullptr; |
| 3667 | else if (const ObjCObjectPointerType *ObjCPtr = |
| 3668 | ObjectType->getAsObjCInterfacePointerType()) { |
| 3669 | ObjectType = QualType(ObjCPtr->getInterfaceType(), 0); |
| 3670 | ObjectIsId = ObjectIsClass = false; |
| 3671 | } |
| 3672 | else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType()) |
| 3673 | ObjectIsClass = false; |
| 3674 | else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType()) |
| 3675 | ObjectIsId = false; |
| 3676 | else |
| 3677 | return nullptr; |
| 3678 | |
| 3679 | for (GlobalMethodPool::iterator b = MethodPool.begin(), |
| 3680 | e = MethodPool.end(); b != e; b++) { |
| 3681 | |
| 3682 | for (ObjCMethodList *M = &b->second.first; M; M=M->getNext()) |
| 3683 | if (M->getMethod() && |
| 3684 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3685 | (M->getMethod()->getSelector() != Sel)) { |
| 3686 | if (ObjectIsId) |
| 3687 | Methods.push_back(M->getMethod()); |
| 3688 | else if (!ObjectIsClass && |
| 3689 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3690 | ObjectType)) |
| 3691 | Methods.push_back(M->getMethod()); |
| 3692 | } |
| 3693 | |
| 3694 | for (ObjCMethodList *M = &b->second.second; M; M=M->getNext()) |
| 3695 | if (M->getMethod() && |
| 3696 | (M->getMethod()->getSelector().getNumArgs() == NumArgs) && |
| 3697 | (M->getMethod()->getSelector() != Sel)) { |
| 3698 | if (ObjectIsClass) |
| 3699 | Methods.push_back(M->getMethod()); |
| 3700 | else if (!ObjectIsId && |
| 3701 | HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(), |
| 3702 | ObjectType)) |
| 3703 | Methods.push_back(M->getMethod()); |
| 3704 | } |
| 3705 | } |
| 3706 | |
| 3707 | SmallVector<const ObjCMethodDecl *, 8> SelectedMethods; |
| 3708 | for (unsigned i = 0, e = Methods.size(); i < e; i++) { |
| 3709 | HelperSelectorsForTypoCorrection(SelectedMethods, |
| 3710 | Sel.getAsString(), Methods[i]); |
| 3711 | } |
| 3712 | return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr; |
| 3713 | } |
| 3714 | |
| 3715 | |
| 3716 | |
| 3717 | |
| 3718 | |
| 3719 | |
| 3720 | void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID, |
| 3721 | ObjCInterfaceDecl *SID) { |
| 3722 | for (auto *Ivar : ID->ivars()) { |
| 3723 | if (Ivar->isInvalidDecl()) |
| 3724 | continue; |
| 3725 | if (IdentifierInfo *II = Ivar->getIdentifier()) { |
| 3726 | ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II); |
| 3727 | if (prevIvar) { |
| 3728 | Diag(Ivar->getLocation(), diag::err_duplicate_member) << II; |
| 3729 | Diag(prevIvar->getLocation(), diag::note_previous_declaration); |
| 3730 | Ivar->setInvalidDecl(); |
| 3731 | } |
| 3732 | } |
| 3733 | } |
| 3734 | } |
| 3735 | |
| 3736 | |
| 3737 | static void DiagnoseWeakIvars(Sema &S, ObjCImplementationDecl *ID) { |
| 3738 | if (S.getLangOpts().ObjCWeak) return; |
| 3739 | |
| 3740 | for (auto ivar = ID->getClassInterface()->all_declared_ivar_begin(); |
| 3741 | ivar; ivar = ivar->getNextIvar()) { |
| 3742 | if (ivar->isInvalidDecl()) continue; |
| 3743 | if (ivar->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 3744 | if (S.getLangOpts().ObjCWeakRuntime) { |
| 3745 | S.Diag(ivar->getLocation(), diag::err_arc_weak_disabled); |
| 3746 | } else { |
| 3747 | S.Diag(ivar->getLocation(), diag::err_arc_weak_no_runtime); |
| 3748 | } |
| 3749 | } |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | |
| 3754 | static void DiagnoseRetainableFlexibleArrayMember(Sema &S, |
| 3755 | ObjCInterfaceDecl *ID) { |
| 3756 | if (!S.getLangOpts().ObjCAutoRefCount) |
| 3757 | return; |
| 3758 | |
| 3759 | for (auto ivar = ID->all_declared_ivar_begin(); ivar; |
| 3760 | ivar = ivar->getNextIvar()) { |
| 3761 | if (ivar->isInvalidDecl()) |
| 3762 | continue; |
| 3763 | QualType IvarTy = ivar->getType(); |
| 3764 | if (IvarTy->isIncompleteArrayType() && |
| 3765 | (IvarTy.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) && |
| 3766 | IvarTy->isObjCLifetimeType()) { |
| 3767 | S.Diag(ivar->getLocation(), diag::err_flexible_array_arc_retainable); |
| 3768 | ivar->setInvalidDecl(); |
| 3769 | } |
| 3770 | } |
| 3771 | } |
| 3772 | |
| 3773 | Sema::ObjCContainerKind Sema::getObjCContainerKind() const { |
| 3774 | switch (CurContext->getDeclKind()) { |
| 3775 | case Decl::ObjCInterface: |
| 3776 | return Sema::OCK_Interface; |
| 3777 | case Decl::ObjCProtocol: |
| 3778 | return Sema::OCK_Protocol; |
| 3779 | case Decl::ObjCCategory: |
| 3780 | if (cast<ObjCCategoryDecl>(CurContext)->IsClassExtension()) |
| 3781 | return Sema::OCK_ClassExtension; |
| 3782 | return Sema::OCK_Category; |
| 3783 | case Decl::ObjCImplementation: |
| 3784 | return Sema::OCK_Implementation; |
| 3785 | case Decl::ObjCCategoryImpl: |
| 3786 | return Sema::OCK_CategoryImplementation; |
| 3787 | |
| 3788 | default: |
| 3789 | return Sema::OCK_None; |
| 3790 | } |
| 3791 | } |
| 3792 | |
| 3793 | static bool IsVariableSizedType(QualType T) { |
| 3794 | if (T->isIncompleteArrayType()) |
| 3795 | return true; |
| 3796 | const auto *RecordTy = T->getAs<RecordType>(); |
| 3797 | return (RecordTy && RecordTy->getDecl()->hasFlexibleArrayMember()); |
| 3798 | } |
| 3799 | |
| 3800 | static void DiagnoseVariableSizedIvars(Sema &S, ObjCContainerDecl *OCD) { |
| 3801 | ObjCInterfaceDecl *IntfDecl = nullptr; |
| 3802 | ObjCInterfaceDecl::ivar_range Ivars = llvm::make_range( |
| 3803 | ObjCInterfaceDecl::ivar_iterator(), ObjCInterfaceDecl::ivar_iterator()); |
| 3804 | if ((IntfDecl = dyn_cast<ObjCInterfaceDecl>(OCD))) { |
| 3805 | Ivars = IntfDecl->ivars(); |
| 3806 | } else if (auto *ImplDecl = dyn_cast<ObjCImplementationDecl>(OCD)) { |
| 3807 | IntfDecl = ImplDecl->getClassInterface(); |
| 3808 | Ivars = ImplDecl->ivars(); |
| 3809 | } else if (auto *CategoryDecl = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 3810 | if (CategoryDecl->IsClassExtension()) { |
| 3811 | IntfDecl = CategoryDecl->getClassInterface(); |
| 3812 | Ivars = CategoryDecl->ivars(); |
| 3813 | } |
| 3814 | } |
| 3815 | |
| 3816 | |
| 3817 | if (!isa<ObjCInterfaceDecl>(OCD)) { |
| 3818 | for (auto ivar : Ivars) { |
| 3819 | if (!ivar->isInvalidDecl() && IsVariableSizedType(ivar->getType())) { |
| 3820 | S.Diag(ivar->getLocation(), diag::warn_variable_sized_ivar_visibility) |
| 3821 | << ivar->getDeclName() << ivar->getType(); |
| 3822 | } |
| 3823 | } |
| 3824 | } |
| 3825 | |
| 3826 | |
| 3827 | if (!IntfDecl) |
| 3828 | return; |
| 3829 | |
| 3830 | |
| 3831 | for (ObjCIvarDecl *ivar = IntfDecl->all_declared_ivar_begin(); ivar; |
| 3832 | ivar = ivar->getNextIvar()) { |
| 3833 | if (ivar->isInvalidDecl() || !ivar->getNextIvar()) |
| 3834 | continue; |
| 3835 | QualType IvarTy = ivar->getType(); |
| 3836 | bool IsInvalidIvar = false; |
| 3837 | if (IvarTy->isIncompleteArrayType()) { |
| 3838 | S.Diag(ivar->getLocation(), diag::err_flexible_array_not_at_end) |
| 3839 | << ivar->getDeclName() << IvarTy |
| 3840 | << TTK_Class; |
| 3841 | IsInvalidIvar = true; |
| 3842 | } else if (const RecordType *RecordTy = IvarTy->getAs<RecordType>()) { |
| 3843 | if (RecordTy->getDecl()->hasFlexibleArrayMember()) { |
| 3844 | S.Diag(ivar->getLocation(), |
| 3845 | diag::err_objc_variable_sized_type_not_at_end) |
| 3846 | << ivar->getDeclName() << IvarTy; |
| 3847 | IsInvalidIvar = true; |
| 3848 | } |
| 3849 | } |
| 3850 | if (IsInvalidIvar) { |
| 3851 | S.Diag(ivar->getNextIvar()->getLocation(), |
| 3852 | diag::note_next_ivar_declaration) |
| 3853 | << ivar->getNextIvar()->getSynthesize(); |
| 3854 | ivar->setInvalidDecl(); |
| 3855 | } |
| 3856 | } |
| 3857 | |
| 3858 | |
| 3859 | |
| 3860 | |
| 3861 | ObjCIvarDecl *FirstIvar = |
| 3862 | (Ivars.begin() == Ivars.end()) ? nullptr : *Ivars.begin(); |
| 3863 | if (FirstIvar && (FirstIvar == IntfDecl->all_declared_ivar_begin())) { |
| 3864 | const ObjCInterfaceDecl *SuperClass = IntfDecl->getSuperClass(); |
| 3865 | while (SuperClass && SuperClass->ivar_empty()) |
| 3866 | SuperClass = SuperClass->getSuperClass(); |
| 3867 | if (SuperClass) { |
| 3868 | auto IvarIter = SuperClass->ivar_begin(); |
| 3869 | std::advance(IvarIter, SuperClass->ivar_size() - 1); |
| 3870 | const ObjCIvarDecl *LastIvar = *IvarIter; |
| 3871 | if (IsVariableSizedType(LastIvar->getType())) { |
| 3872 | S.Diag(FirstIvar->getLocation(), |
| 3873 | diag::warn_superclass_variable_sized_type_not_at_end) |
| 3874 | << FirstIvar->getDeclName() << LastIvar->getDeclName() |
| 3875 | << LastIvar->getType() << SuperClass->getDeclName(); |
| 3876 | S.Diag(LastIvar->getLocation(), diag::note_entity_declared_at) |
| 3877 | << LastIvar->getDeclName(); |
| 3878 | } |
| 3879 | } |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | |
| 3884 | Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods, |
| 3885 | ArrayRef<DeclGroupPtrTy> allTUVars) { |
| 3886 | if (getObjCContainerKind() == Sema::OCK_None) |
| 3887 | return nullptr; |
| 3888 | |
| 3889 | (0) . __assert_fail ("AtEnd.isValid() && \"Invalid location for '@end'\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 3889, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AtEnd.isValid() && "Invalid location for '@end'"); |
| 3890 | |
| 3891 | auto *OCD = cast<ObjCContainerDecl>(CurContext); |
| 3892 | Decl *ClassDecl = OCD; |
| 3893 | |
| 3894 | bool isInterfaceDeclKind = |
| 3895 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 3896 | || isa<ObjCProtocolDecl>(ClassDecl); |
| 3897 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
| 3898 | |
| 3899 | |
| 3900 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 3901 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 3902 | |
| 3903 | for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) { |
| 3904 | ObjCMethodDecl *Method = |
| 3905 | cast_or_null<ObjCMethodDecl>(allMethods[i]); |
| 3906 | |
| 3907 | if (!Method) continue; |
| 3908 | if (Method->isInstanceMethod()) { |
| 3909 | |
| 3910 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
| 3911 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 3912 | : false; |
| 3913 | if ((isInterfaceDeclKind && PrevMethod && !match) |
| 3914 | || (checkIdenticalMethods && match)) { |
| 3915 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 3916 | << Method->getDeclName(); |
| 3917 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3918 | Method->setInvalidDecl(); |
| 3919 | } else { |
| 3920 | if (PrevMethod) { |
| 3921 | Method->setAsRedeclaration(PrevMethod); |
| 3922 | if (!Context.getSourceManager().isInSystemHeader( |
| 3923 | Method->getLocation())) |
| 3924 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3925 | << Method->getDeclName(); |
| 3926 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3927 | } |
| 3928 | InsMap[Method->getSelector()] = Method; |
| 3929 | |
| 3930 | AddInstanceMethodToGlobalPool(Method); |
| 3931 | } |
| 3932 | } else { |
| 3933 | |
| 3934 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
| 3935 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 3936 | : false; |
| 3937 | if ((isInterfaceDeclKind && PrevMethod && !match) |
| 3938 | || (checkIdenticalMethods && match)) { |
| 3939 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 3940 | << Method->getDeclName(); |
| 3941 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3942 | Method->setInvalidDecl(); |
| 3943 | } else { |
| 3944 | if (PrevMethod) { |
| 3945 | Method->setAsRedeclaration(PrevMethod); |
| 3946 | if (!Context.getSourceManager().isInSystemHeader( |
| 3947 | Method->getLocation())) |
| 3948 | Diag(Method->getLocation(), diag::warn_duplicate_method_decl) |
| 3949 | << Method->getDeclName(); |
| 3950 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 3951 | } |
| 3952 | ClsMap[Method->getSelector()] = Method; |
| 3953 | AddFactoryMethodToGlobalPool(Method); |
| 3954 | } |
| 3955 | } |
| 3956 | } |
| 3957 | if (isa<ObjCInterfaceDecl>(ClassDecl)) { |
| 3958 | |
| 3959 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
| 3960 | |
| 3961 | |
| 3962 | |
| 3963 | |
| 3964 | if (C->IsClassExtension()) { |
| 3965 | ObjCInterfaceDecl *CCPrimary = C->getClassInterface(); |
| 3966 | DiagnoseClassExtensionDupMethods(C, CCPrimary); |
| 3967 | } |
| 3968 | } |
| 3969 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
| 3970 | if (CDecl->getIdentifier()) |
| 3971 | |
| 3972 | |
| 3973 | |
| 3974 | for (auto *I : CDecl->properties()) |
| 3975 | ProcessPropertyDecl(I); |
| 3976 | CDecl->setAtEndRange(AtEnd); |
| 3977 | } |
| 3978 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
| 3979 | IC->setAtEndRange(AtEnd); |
| 3980 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
| 3981 | |
| 3982 | |
| 3983 | |
| 3984 | |
| 3985 | |
| 3986 | for (const auto *Ext : IDecl->visible_extensions()) { |
| 3987 | for (const auto *Property : Ext->instance_properties()) { |
| 3988 | |
| 3989 | if (const ObjCPropertyImplDecl *PIDecl |
| 3990 | = IC->FindPropertyImplDecl(Property->getIdentifier(), |
| 3991 | Property->getQueryKind())) |
| 3992 | if (PIDecl->getPropertyImplementation() |
| 3993 | == ObjCPropertyImplDecl::Dynamic) |
| 3994 | continue; |
| 3995 | |
| 3996 | for (const auto *Ext : IDecl->visible_extensions()) { |
| 3997 | if (ObjCMethodDecl *GetterMethod |
| 3998 | = Ext->getInstanceMethod(Property->getGetterName())) |
| 3999 | GetterMethod->setPropertyAccessor(true); |
| 4000 | if (!Property->isReadOnly()) |
| 4001 | if (ObjCMethodDecl *SetterMethod |
| 4002 | = Ext->getInstanceMethod(Property->getSetterName())) |
| 4003 | SetterMethod->setPropertyAccessor(true); |
| 4004 | } |
| 4005 | } |
| 4006 | } |
| 4007 | ImplMethodsVsClassMethods(S, IC, IDecl); |
| 4008 | AtomicPropertySetterGetterRules(IC, IDecl); |
| 4009 | DiagnoseOwningPropertyGetterSynthesis(IC); |
| 4010 | DiagnoseUnusedBackingIvarInAccessor(S, IC); |
| 4011 | if (IDecl->hasDesignatedInitializers()) |
| 4012 | DiagnoseMissingDesignatedInitOverrides(IC, IDecl); |
| 4013 | DiagnoseWeakIvars(*this, IC); |
| 4014 | DiagnoseRetainableFlexibleArrayMember(*this, IDecl); |
| 4015 | |
| 4016 | bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>(); |
| 4017 | if (IDecl->getSuperClass() == nullptr) { |
| 4018 | |
| 4019 | |
| 4020 | if (!HasRootClassAttr) { |
| 4021 | SourceLocation DeclLoc(IDecl->getLocation()); |
| 4022 | SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc)); |
| 4023 | Diag(DeclLoc, diag::warn_objc_root_class_missing) |
| 4024 | << IDecl->getIdentifier(); |
| 4025 | |
| 4026 | |
| 4027 | NamedDecl *IF = LookupSingleName(TUScope, |
| 4028 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject), |
| 4029 | DeclLoc, LookupOrdinaryName); |
| 4030 | ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 4031 | if (NSObjectDecl && NSObjectDecl->getDefinition()) { |
| 4032 | Diag(SuperClassLoc, diag::note_objc_needs_superclass) |
| 4033 | << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject "); |
| 4034 | } else { |
| 4035 | Diag(SuperClassLoc, diag::note_objc_needs_superclass); |
| 4036 | } |
| 4037 | } |
| 4038 | } else if (HasRootClassAttr) { |
| 4039 | |
| 4040 | Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass); |
| 4041 | } |
| 4042 | |
| 4043 | if (const ObjCInterfaceDecl *Super = IDecl->getSuperClass()) { |
| 4044 | |
| 4045 | |
| 4046 | |
| 4047 | |
| 4048 | if (IDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
| 4049 | Super->hasAttr<ObjCSubclassingRestrictedAttr>()) { |
| 4050 | Diag(IC->getLocation(), diag::err_restricted_superclass_mismatch); |
| 4051 | Diag(Super->getLocation(), diag::note_class_declared); |
| 4052 | } |
| 4053 | } |
| 4054 | |
| 4055 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
| 4056 | while (IDecl->getSuperClass()) { |
| 4057 | DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass()); |
| 4058 | IDecl = IDecl->getSuperClass(); |
| 4059 | } |
| 4060 | } |
| 4061 | } |
| 4062 | SetIvarInitializers(IC); |
| 4063 | } else if (ObjCCategoryImplDecl* CatImplClass = |
| 4064 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
| 4065 | CatImplClass->setAtEndRange(AtEnd); |
| 4066 | |
| 4067 | |
| 4068 | |
| 4069 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
| 4070 | if (ObjCCategoryDecl *Cat |
| 4071 | = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) { |
| 4072 | ImplMethodsVsClassMethods(S, CatImplClass, Cat); |
| 4073 | } |
| 4074 | } |
| 4075 | } else if (const auto *IntfDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
| 4076 | if (const ObjCInterfaceDecl *Super = IntfDecl->getSuperClass()) { |
| 4077 | if (!IntfDecl->hasAttr<ObjCSubclassingRestrictedAttr>() && |
| 4078 | Super->hasAttr<ObjCSubclassingRestrictedAttr>()) { |
| 4079 | Diag(IntfDecl->getLocation(), diag::err_restricted_superclass_mismatch); |
| 4080 | Diag(Super->getLocation(), diag::note_class_declared); |
| 4081 | } |
| 4082 | } |
| 4083 | } |
| 4084 | DiagnoseVariableSizedIvars(*this, OCD); |
| 4085 | if (isInterfaceDeclKind) { |
| 4086 | |
| 4087 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
| 4088 | DeclGroupRef DG = allTUVars[i].get(); |
| 4089 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 4090 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
| 4091 | if (!VDecl->hasExternalStorage()) |
| 4092 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
| 4093 | } |
| 4094 | } |
| 4095 | } |
| 4096 | ActOnObjCContainerFinishDefinition(); |
| 4097 | |
| 4098 | for (unsigned i = 0, e = allTUVars.size(); i != e; i++) { |
| 4099 | DeclGroupRef DG = allTUVars[i].get(); |
| 4100 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 4101 | (*I)->setTopLevelDeclInObjCContainer(); |
| 4102 | Consumer.HandleTopLevelDeclInObjCContainer(DG); |
| 4103 | } |
| 4104 | |
| 4105 | ActOnDocumentableDecl(ClassDecl); |
| 4106 | return ClassDecl; |
| 4107 | } |
| 4108 | |
| 4109 | |
| 4110 | |
| 4111 | static Decl::ObjCDeclQualifier |
| 4112 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 4113 | return (Decl::ObjCDeclQualifier) (unsigned) PQTVal; |
| 4114 | } |
| 4115 | |
| 4116 | |
| 4117 | |
| 4118 | |
| 4119 | static Sema::ResultTypeCompatibilityKind |
| 4120 | CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method, |
| 4121 | ObjCInterfaceDecl *CurrentClass) { |
| 4122 | QualType ResultType = Method->getReturnType(); |
| 4123 | |
| 4124 | |
| 4125 | |
| 4126 | |
| 4127 | if (const ObjCObjectPointerType *ResultObjectType |
| 4128 | = ResultType->getAs<ObjCObjectPointerType>()) { |
| 4129 | |
| 4130 | if (ResultObjectType->isObjCIdType() || |
| 4131 | ResultObjectType->isObjCQualifiedIdType()) |
| 4132 | return Sema::RTC_Compatible; |
| 4133 | |
| 4134 | if (CurrentClass) { |
| 4135 | if (ObjCInterfaceDecl *ResultClass |
| 4136 | = ResultObjectType->getInterfaceDecl()) { |
| 4137 | |
| 4138 | if (declaresSameEntity(CurrentClass, ResultClass)) |
| 4139 | return Sema::RTC_Compatible; |
| 4140 | |
| 4141 | |
| 4142 | if (ResultClass->isSuperClassOf(CurrentClass)) |
| 4143 | return Sema::RTC_Compatible; |
| 4144 | } |
| 4145 | } else { |
| 4146 | |
| 4147 | |
| 4148 | return Sema::RTC_Unknown; |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | return Sema::RTC_Incompatible; |
| 4153 | } |
| 4154 | |
| 4155 | namespace { |
| 4156 | |
| 4157 | |
| 4158 | class OverrideSearch { |
| 4159 | public: |
| 4160 | Sema &S; |
| 4161 | ObjCMethodDecl *Method; |
| 4162 | llvm::SmallSetVector<ObjCMethodDecl*, 4> Overridden; |
| 4163 | bool Recursive; |
| 4164 | |
| 4165 | public: |
| 4166 | OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) { |
| 4167 | Selector selector = method->getSelector(); |
| 4168 | |
| 4169 | |
| 4170 | |
| 4171 | Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector); |
| 4172 | if (it == S.MethodPool.end()) { |
| 4173 | if (!S.getExternalSource()) return; |
| 4174 | S.ReadMethodPool(selector); |
| 4175 | |
| 4176 | it = S.MethodPool.find(selector); |
| 4177 | if (it == S.MethodPool.end()) |
| 4178 | return; |
| 4179 | } |
| 4180 | ObjCMethodList &list = |
| 4181 | method->isInstanceMethod() ? it->second.first : it->second.second; |
| 4182 | if (!list.getMethod()) return; |
| 4183 | |
| 4184 | ObjCContainerDecl *container |
| 4185 | = cast<ObjCContainerDecl>(method->getDeclContext()); |
| 4186 | |
| 4187 | |
| 4188 | |
| 4189 | |
| 4190 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) { |
| 4191 | searchFromContainer(container); |
| 4192 | if (ObjCInterfaceDecl *Interface = Category->getClassInterface()) |
| 4193 | searchFromContainer(Interface); |
| 4194 | } else { |
| 4195 | searchFromContainer(container); |
| 4196 | } |
| 4197 | } |
| 4198 | |
| 4199 | typedef decltype(Overridden)::iterator iterator; |
| 4200 | iterator begin() const { return Overridden.begin(); } |
| 4201 | iterator end() const { return Overridden.end(); } |
| 4202 | |
| 4203 | private: |
| 4204 | void searchFromContainer(ObjCContainerDecl *container) { |
| 4205 | if (container->isInvalidDecl()) return; |
| 4206 | |
| 4207 | switch (container->getDeclKind()) { |
| 4208 | #define OBJCCONTAINER(type, base) \ |
| 4209 | case Decl::type: \ |
| 4210 | searchFrom(cast<type##Decl>(container)); \ |
| 4211 | break; |
| 4212 | #define ABSTRACT_DECL(expansion) |
| 4213 | #define DECL(type, base) \ |
| 4214 | case Decl::type: |
| 4215 | #include "clang/AST/DeclNodes.inc" |
| 4216 | llvm_unreachable("not an ObjC container!"); |
| 4217 | } |
| 4218 | } |
| 4219 | |
| 4220 | void searchFrom(ObjCProtocolDecl *protocol) { |
| 4221 | if (!protocol->hasDefinition()) |
| 4222 | return; |
| 4223 | |
| 4224 | |
| 4225 | |
| 4226 | search(protocol->getReferencedProtocols()); |
| 4227 | } |
| 4228 | |
| 4229 | void searchFrom(ObjCCategoryDecl *category) { |
| 4230 | |
| 4231 | |
| 4232 | |
| 4233 | search(category->getReferencedProtocols()); |
| 4234 | } |
| 4235 | |
| 4236 | void searchFrom(ObjCCategoryImplDecl *impl) { |
| 4237 | |
| 4238 | |
| 4239 | |
| 4240 | if (ObjCCategoryDecl *category = impl->getCategoryDecl()) { |
| 4241 | search(category); |
| 4242 | if (ObjCInterfaceDecl *Interface = category->getClassInterface()) |
| 4243 | search(Interface); |
| 4244 | |
| 4245 | |
| 4246 | } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) { |
| 4247 | search(Interface); |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | void searchFrom(ObjCInterfaceDecl *iface) { |
| 4252 | |
| 4253 | if (!iface->hasDefinition()) |
| 4254 | return; |
| 4255 | |
| 4256 | |
| 4257 | for (auto *Cat : iface->known_categories()) |
| 4258 | search(Cat); |
| 4259 | |
| 4260 | |
| 4261 | if (ObjCInterfaceDecl *super = iface->getSuperClass()) |
| 4262 | search(super); |
| 4263 | |
| 4264 | |
| 4265 | search(iface->getReferencedProtocols()); |
| 4266 | } |
| 4267 | |
| 4268 | void searchFrom(ObjCImplementationDecl *impl) { |
| 4269 | |
| 4270 | |
| 4271 | if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) |
| 4272 | search(Interface); |
| 4273 | } |
| 4274 | |
| 4275 | void search(const ObjCProtocolList &protocols) { |
| 4276 | for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end(); |
| 4277 | i != e; ++i) |
| 4278 | search(*i); |
| 4279 | } |
| 4280 | |
| 4281 | void search(ObjCContainerDecl *container) { |
| 4282 | |
| 4283 | ObjCMethodDecl *meth = container->getMethod(Method->getSelector(), |
| 4284 | Method->isInstanceMethod(), |
| 4285 | ); |
| 4286 | |
| 4287 | |
| 4288 | if (meth) { |
| 4289 | Overridden.insert(meth); |
| 4290 | return; |
| 4291 | } |
| 4292 | |
| 4293 | |
| 4294 | |
| 4295 | |
| 4296 | |
| 4297 | Recursive = true; |
| 4298 | |
| 4299 | searchFromContainer(container); |
| 4300 | } |
| 4301 | }; |
| 4302 | } |
| 4303 | |
| 4304 | void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod, |
| 4305 | ObjCInterfaceDecl *CurrentClass, |
| 4306 | ResultTypeCompatibilityKind RTC) { |
| 4307 | |
| 4308 | OverrideSearch overrides(*this, ObjCMethod); |
| 4309 | |
| 4310 | |
| 4311 | |
| 4312 | |
| 4313 | |
| 4314 | bool hasOverriddenMethodsInBaseOrProtocol = false; |
| 4315 | for (OverrideSearch::iterator |
| 4316 | i = overrides.begin(), e = overrides.end(); i != e; ++i) { |
| 4317 | ObjCMethodDecl *overridden = *i; |
| 4318 | |
| 4319 | if (!hasOverriddenMethodsInBaseOrProtocol) { |
| 4320 | if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) || |
| 4321 | CurrentClass != overridden->getClassInterface() || |
| 4322 | overridden->isOverriding()) { |
| 4323 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 4324 | |
| 4325 | } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) { |
| 4326 | |
| 4327 | |
| 4328 | |
| 4329 | |
| 4330 | |
| 4331 | |
| 4332 | |
| 4333 | GlobalMethodPool::iterator It = |
| 4334 | MethodPool.find(ObjCMethod->getSelector()); |
| 4335 | if (It != MethodPool.end()) { |
| 4336 | ObjCMethodList &List = |
| 4337 | ObjCMethod->isInstanceMethod()? It->second.first: It->second.second; |
| 4338 | unsigned CategCount = List.getBits(); |
| 4339 | if (CategCount > 0) { |
| 4340 | |
| 4341 | |
| 4342 | if (CategCount > 1 || |
| 4343 | !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) { |
| 4344 | OverrideSearch overrides(*this, overridden); |
| 4345 | for (OverrideSearch::iterator |
| 4346 | OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) { |
| 4347 | ObjCMethodDecl *SuperOverridden = *OI; |
| 4348 | if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) || |
| 4349 | CurrentClass != SuperOverridden->getClassInterface()) { |
| 4350 | hasOverriddenMethodsInBaseOrProtocol = true; |
| 4351 | overridden->setOverriding(true); |
| 4352 | break; |
| 4353 | } |
| 4354 | } |
| 4355 | } |
| 4356 | } |
| 4357 | } |
| 4358 | } |
| 4359 | } |
| 4360 | |
| 4361 | |
| 4362 | if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType()) |
| 4363 | ObjCMethod->setRelatedResultType(); |
| 4364 | |
| 4365 | |
| 4366 | mergeObjCMethodDecls(ObjCMethod, overridden); |
| 4367 | |
| 4368 | if (ObjCMethod->isImplicit() && overridden->isImplicit()) |
| 4369 | continue; |
| 4370 | |
| 4371 | |
| 4372 | if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) || |
| 4373 | isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) |
| 4374 | CheckConflictingOverridingMethod(ObjCMethod, overridden, |
| 4375 | isa<ObjCProtocolDecl>(overridden->getDeclContext())); |
| 4376 | |
| 4377 | if (CurrentClass && overridden->getDeclContext() != CurrentClass && |
| 4378 | isa<ObjCInterfaceDecl>(overridden->getDeclContext()) && |
| 4379 | !overridden->isImplicit() ) { |
| 4380 | ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(), |
| 4381 | E = ObjCMethod->param_end(); |
| 4382 | ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(), |
| 4383 | PrevE = overridden->param_end(); |
| 4384 | for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) { |
| 4385 | (0) . __assert_fail ("PrevI != overridden->param_end() && \"Param mismatch\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 4385, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PrevI != overridden->param_end() && "Param mismatch"); |
| 4386 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 4387 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 4388 | |
| 4389 | |
| 4390 | if (!Context.typesAreCompatible(T1, T2)) { |
| 4391 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
| 4392 | << T1 << T2; |
| 4393 | Diag(overridden->getLocation(), diag::note_previous_declaration); |
| 4394 | break; |
| 4395 | } |
| 4396 | } |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol); |
| 4401 | } |
| 4402 | |
| 4403 | |
| 4404 | |
| 4405 | static QualType mergeTypeNullabilityForRedecl(Sema &S, SourceLocation loc, |
| 4406 | QualType type, |
| 4407 | bool usesCSKeyword, |
| 4408 | SourceLocation prevLoc, |
| 4409 | QualType prevType, |
| 4410 | bool prevUsesCSKeyword) { |
| 4411 | |
| 4412 | auto nullability = type->getNullability(S.Context); |
| 4413 | auto prevNullability = prevType->getNullability(S.Context); |
| 4414 | |
| 4415 | |
| 4416 | if (nullability.hasValue() == prevNullability.hasValue()) { |
| 4417 | |
| 4418 | if (!nullability) |
| 4419 | return type; |
| 4420 | |
| 4421 | |
| 4422 | if (*nullability == *prevNullability) |
| 4423 | return type; |
| 4424 | |
| 4425 | |
| 4426 | S.Diag(loc, diag::err_nullability_conflicting) |
| 4427 | << DiagNullabilityKind(*nullability, usesCSKeyword) |
| 4428 | << DiagNullabilityKind(*prevNullability, prevUsesCSKeyword); |
| 4429 | return type; |
| 4430 | } |
| 4431 | |
| 4432 | |
| 4433 | if (nullability) |
| 4434 | return type; |
| 4435 | |
| 4436 | |
| 4437 | return S.Context.getAttributedType( |
| 4438 | AttributedType::getNullabilityAttrKind(*prevNullability), |
| 4439 | type, type); |
| 4440 | } |
| 4441 | |
| 4442 | |
| 4443 | |
| 4444 | |
| 4445 | static void mergeInterfaceMethodToImpl(Sema &S, |
| 4446 | ObjCMethodDecl *method, |
| 4447 | ObjCMethodDecl *prevMethod) { |
| 4448 | |
| 4449 | if (prevMethod->hasAttr<ObjCRequiresSuperAttr>() && |
| 4450 | !method->hasAttr<ObjCRequiresSuperAttr>()) { |
| 4451 | |
| 4452 | method->addAttr( |
| 4453 | ObjCRequiresSuperAttr::CreateImplicit(S.Context, |
| 4454 | method->getLocation())); |
| 4455 | } |
| 4456 | |
| 4457 | |
| 4458 | QualType newReturnType |
| 4459 | = mergeTypeNullabilityForRedecl( |
| 4460 | S, method->getReturnTypeSourceRange().getBegin(), |
| 4461 | method->getReturnType(), |
| 4462 | method->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4463 | prevMethod->getReturnTypeSourceRange().getBegin(), |
| 4464 | prevMethod->getReturnType(), |
| 4465 | prevMethod->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4466 | method->setReturnType(newReturnType); |
| 4467 | |
| 4468 | |
| 4469 | unsigned numParams = method->param_size(); |
| 4470 | unsigned numPrevParams = prevMethod->param_size(); |
| 4471 | for (unsigned i = 0, n = std::min(numParams, numPrevParams); i != n; ++i) { |
| 4472 | ParmVarDecl *param = method->param_begin()[i]; |
| 4473 | ParmVarDecl *prevParam = prevMethod->param_begin()[i]; |
| 4474 | |
| 4475 | |
| 4476 | QualType newParamType |
| 4477 | = mergeTypeNullabilityForRedecl( |
| 4478 | S, param->getLocation(), param->getType(), |
| 4479 | param->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability, |
| 4480 | prevParam->getLocation(), prevParam->getType(), |
| 4481 | prevParam->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability); |
| 4482 | param->setType(newParamType); |
| 4483 | } |
| 4484 | } |
| 4485 | |
| 4486 | |
| 4487 | |
| 4488 | static void checkObjCMethodX86VectorTypes(Sema &SemaRef, |
| 4489 | const ObjCMethodDecl *Method) { |
| 4490 | (0) . __assert_fail ("SemaRef.getASTContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86 && \"x86-specific check invoked for a different target\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 4492, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SemaRef.getASTContext().getTargetInfo().getTriple().getArch() == |
| 4491 | (0) . __assert_fail ("SemaRef.getASTContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86 && \"x86-specific check invoked for a different target\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 4492, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> llvm::Triple::x86 && |
| 4492 | (0) . __assert_fail ("SemaRef.getASTContext().getTargetInfo().getTriple().getArch() == llvm..Triple..x86 && \"x86-specific check invoked for a different target\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 4492, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "x86-specific check invoked for a different target"); |
| 4493 | SourceLocation Loc; |
| 4494 | QualType T; |
| 4495 | for (const ParmVarDecl *P : Method->parameters()) { |
| 4496 | if (P->getType()->isVectorType()) { |
| 4497 | Loc = P->getBeginLoc(); |
| 4498 | T = P->getType(); |
| 4499 | break; |
| 4500 | } |
| 4501 | } |
| 4502 | if (Loc.isInvalid()) { |
| 4503 | if (Method->getReturnType()->isVectorType()) { |
| 4504 | Loc = Method->getReturnTypeSourceRange().getBegin(); |
| 4505 | T = Method->getReturnType(); |
| 4506 | } else |
| 4507 | return; |
| 4508 | } |
| 4509 | |
| 4510 | |
| 4511 | |
| 4512 | const auto &Triple = SemaRef.getASTContext().getTargetInfo().getTriple(); |
| 4513 | VersionTuple AcceptedInVersion; |
| 4514 | if (Triple.getOS() == llvm::Triple::IOS) |
| 4515 | AcceptedInVersion = VersionTuple(); |
| 4516 | else if (Triple.isMacOSX()) |
| 4517 | AcceptedInVersion = VersionTuple(, ); |
| 4518 | else |
| 4519 | return; |
| 4520 | if (SemaRef.getASTContext().getTargetInfo().getPlatformMinVersion() >= |
| 4521 | AcceptedInVersion) |
| 4522 | return; |
| 4523 | SemaRef.Diag(Loc, diag::err_objc_method_unsupported_param_ret_type) |
| 4524 | << T << (Method->getReturnType()->isVectorType() ? 1 |
| 4525 | : 0) |
| 4526 | << (Triple.isMacOSX() ? "macOS 10.11" : "iOS 9"); |
| 4527 | } |
| 4528 | |
| 4529 | Decl *Sema::ActOnMethodDeclaration( |
| 4530 | Scope *S, SourceLocation MethodLoc, SourceLocation EndLoc, |
| 4531 | tok::TokenKind MethodType, ObjCDeclSpec &ReturnQT, ParsedType ReturnType, |
| 4532 | ArrayRef<SourceLocation> SelectorLocs, Selector Sel, |
| 4533 | |
| 4534 | |
| 4535 | ObjCArgInfo *ArgInfo, DeclaratorChunk::ParamInfo *CParamInfo, |
| 4536 | unsigned CNumArgs, |
| 4537 | const ParsedAttributesView &AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 4538 | bool isVariadic, bool MethodDefinition) { |
| 4539 | |
| 4540 | if (!CurContext->isObjCContainer()) { |
| 4541 | Diag(MethodLoc, diag::err_missing_method_context); |
| 4542 | return nullptr; |
| 4543 | } |
| 4544 | Decl *ClassDecl = cast<ObjCContainerDecl>(CurContext); |
| 4545 | QualType resultDeclType; |
| 4546 | |
| 4547 | bool HasRelatedResultType = false; |
| 4548 | TypeSourceInfo *ReturnTInfo = nullptr; |
| 4549 | if (ReturnType) { |
| 4550 | resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo); |
| 4551 | |
| 4552 | if (CheckFunctionReturnType(resultDeclType, MethodLoc)) |
| 4553 | return nullptr; |
| 4554 | |
| 4555 | QualType bareResultType = resultDeclType; |
| 4556 | (void)AttributedType::stripOuterNullability(bareResultType); |
| 4557 | HasRelatedResultType = (bareResultType == Context.getObjCInstanceType()); |
| 4558 | } else { |
| 4559 | resultDeclType = Context.getObjCIdType(); |
| 4560 | Diag(MethodLoc, diag::warn_missing_method_return_type) |
| 4561 | << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)"); |
| 4562 | } |
| 4563 | |
| 4564 | ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create( |
| 4565 | Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext, |
| 4566 | MethodType == tok::minus, isVariadic, |
| 4567 | , |
| 4568 | , , |
| 4569 | MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional |
| 4570 | : ObjCMethodDecl::Required, |
| 4571 | HasRelatedResultType); |
| 4572 | |
| 4573 | SmallVector<ParmVarDecl*, 16> Params; |
| 4574 | |
| 4575 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
| 4576 | QualType ArgType; |
| 4577 | TypeSourceInfo *DI; |
| 4578 | |
| 4579 | if (!ArgInfo[i].Type) { |
| 4580 | ArgType = Context.getObjCIdType(); |
| 4581 | DI = nullptr; |
| 4582 | } else { |
| 4583 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
| 4584 | } |
| 4585 | |
| 4586 | LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc, |
| 4587 | LookupOrdinaryName, forRedeclarationInCurContext()); |
| 4588 | LookupName(R, S); |
| 4589 | if (R.isSingleResult()) { |
| 4590 | NamedDecl *PrevDecl = R.getFoundDecl(); |
| 4591 | if (S->isDeclScope(PrevDecl)) { |
| 4592 | Diag(ArgInfo[i].NameLoc, |
| 4593 | (MethodDefinition ? diag::warn_method_param_redefinition |
| 4594 | : diag::warn_method_param_declaration)) |
| 4595 | << ArgInfo[i].Name; |
| 4596 | Diag(PrevDecl->getLocation(), |
| 4597 | diag::note_previous_declaration); |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | SourceLocation StartLoc = DI |
| 4602 | ? DI->getTypeLoc().getBeginLoc() |
| 4603 | : ArgInfo[i].NameLoc; |
| 4604 | |
| 4605 | ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc, |
| 4606 | ArgInfo[i].NameLoc, ArgInfo[i].Name, |
| 4607 | ArgType, DI, SC_None); |
| 4608 | |
| 4609 | Param->setObjCMethodScopeInfo(i); |
| 4610 | |
| 4611 | Param->setObjCDeclQualifier( |
| 4612 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
| 4613 | |
| 4614 | |
| 4615 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
| 4616 | AddPragmaAttributes(TUScope, Param); |
| 4617 | |
| 4618 | if (Param->hasAttr<BlocksAttr>()) { |
| 4619 | Diag(Param->getLocation(), diag::err_block_on_nonlocal); |
| 4620 | Param->setInvalidDecl(); |
| 4621 | } |
| 4622 | S->AddDecl(Param); |
| 4623 | IdResolver.AddDecl(Param); |
| 4624 | |
| 4625 | Params.push_back(Param); |
| 4626 | } |
| 4627 | |
| 4628 | for (unsigned i = 0, e = CNumArgs; i != e; ++i) { |
| 4629 | ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param); |
| 4630 | QualType ArgType = Param->getType(); |
| 4631 | if (ArgType.isNull()) |
| 4632 | ArgType = Context.getObjCIdType(); |
| 4633 | else |
| 4634 | |
| 4635 | ArgType = Context.getAdjustedParameterType(ArgType); |
| 4636 | |
| 4637 | Param->setDeclContext(ObjCMethod); |
| 4638 | Params.push_back(Param); |
| 4639 | } |
| 4640 | |
| 4641 | ObjCMethod->setMethodParams(Context, Params, SelectorLocs); |
| 4642 | ObjCMethod->setObjCDeclQualifier( |
| 4643 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 4644 | |
| 4645 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
| 4646 | AddPragmaAttributes(TUScope, ObjCMethod); |
| 4647 | |
| 4648 | |
| 4649 | const ObjCMethodDecl *PrevMethod = nullptr; |
| 4650 | if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) { |
| 4651 | if (MethodType == tok::minus) { |
| 4652 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 4653 | ImpDecl->addInstanceMethod(ObjCMethod); |
| 4654 | } else { |
| 4655 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 4656 | ImpDecl->addClassMethod(ObjCMethod); |
| 4657 | } |
| 4658 | |
| 4659 | |
| 4660 | |
| 4661 | if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface()) { |
| 4662 | if (auto *IMD = IDecl->lookupMethod(ObjCMethod->getSelector(), |
| 4663 | ObjCMethod->isInstanceMethod())) { |
| 4664 | mergeInterfaceMethodToImpl(*this, ObjCMethod, IMD); |
| 4665 | |
| 4666 | |
| 4667 | if (isa<ObjCCategoryImplDecl>(ImpDecl) && IMD->isOverriding() && |
| 4668 | ObjCMethod->getSelector().getMethodFamily() == OMF_dealloc) { |
| 4669 | Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category) |
| 4670 | << ObjCMethod->getDeclName(); |
| 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | |
| 4675 | |
| 4676 | |
| 4677 | for (auto *C : IDecl->visible_categories()) |
| 4678 | for (auto &P : C->protocols()) |
| 4679 | if (auto *IMD = P->lookupMethod(ObjCMethod->getSelector(), |
| 4680 | ObjCMethod->isInstanceMethod())) { |
| 4681 | (0) . __assert_fail ("ObjCMethod->parameters().size() == IMD->parameters().size() && \"Methods have different number of parameters\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 4683, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ObjCMethod->parameters().size() == |
| 4682 | (0) . __assert_fail ("ObjCMethod->parameters().size() == IMD->parameters().size() && \"Methods have different number of parameters\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 4683, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> IMD->parameters().size() && |
| 4683 | (0) . __assert_fail ("ObjCMethod->parameters().size() == IMD->parameters().size() && \"Methods have different number of parameters\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaDeclObjC.cpp", 4683, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Methods have different number of parameters"); |
| 4684 | auto OI = IMD->param_begin(), OE = IMD->param_end(); |
| 4685 | auto NI = ObjCMethod->param_begin(); |
| 4686 | for (; OI != OE; ++OI, ++NI) |
| 4687 | diagnoseNoescape(*NI, *OI, C, P, *this); |
| 4688 | } |
| 4689 | } |
| 4690 | } else { |
| 4691 | cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod); |
| 4692 | } |
| 4693 | |
| 4694 | if (PrevMethod) { |
| 4695 | |
| 4696 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
| 4697 | << ObjCMethod->getDeclName(); |
| 4698 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 4699 | ObjCMethod->setInvalidDecl(); |
| 4700 | return ObjCMethod; |
| 4701 | } |
| 4702 | |
| 4703 | |
| 4704 | |
| 4705 | |
| 4706 | ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 4707 | if (!CurrentClass) { |
| 4708 | if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 4709 | CurrentClass = Cat->getClassInterface(); |
| 4710 | else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl)) |
| 4711 | CurrentClass = Impl->getClassInterface(); |
| 4712 | else if (ObjCCategoryImplDecl *CatImpl |
| 4713 | = dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) |
| 4714 | CurrentClass = CatImpl->getClassInterface(); |
| 4715 | } |
| 4716 | |
| 4717 | ResultTypeCompatibilityKind RTC |
| 4718 | = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass); |
| 4719 | |
| 4720 | CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC); |
| 4721 | |
| 4722 | bool ARCError = false; |
| 4723 | if (getLangOpts().ObjCAutoRefCount) |
| 4724 | ARCError = CheckARCMethodDecl(ObjCMethod); |
| 4725 | |
| 4726 | |
| 4727 | if (!ARCError && RTC == Sema::RTC_Compatible && |
| 4728 | !ObjCMethod->hasRelatedResultType() && |
| 4729 | LangOpts.ObjCInferRelatedResultType) { |
| 4730 | bool InferRelatedResultType = false; |
| 4731 | switch (ObjCMethod->getMethodFamily()) { |
| 4732 | case OMF_None: |
| 4733 | case OMF_copy: |
| 4734 | case OMF_dealloc: |
| 4735 | case OMF_finalize: |
| 4736 | case OMF_mutableCopy: |
| 4737 | case OMF_release: |
| 4738 | case OMF_retainCount: |
| 4739 | case OMF_initialize: |
| 4740 | case OMF_performSelector: |
| 4741 | break; |
| 4742 | |
| 4743 | case OMF_alloc: |
| 4744 | case OMF_new: |
| 4745 | InferRelatedResultType = ObjCMethod->isClassMethod(); |
| 4746 | break; |
| 4747 | |
| 4748 | case OMF_init: |
| 4749 | case OMF_autorelease: |
| 4750 | case OMF_retain: |
| 4751 | case OMF_self: |
| 4752 | InferRelatedResultType = ObjCMethod->isInstanceMethod(); |
| 4753 | break; |
| 4754 | } |
| 4755 | |
| 4756 | if (InferRelatedResultType && |
| 4757 | !ObjCMethod->getReturnType()->isObjCIndependentClassType()) |
| 4758 | ObjCMethod->setRelatedResultType(); |
| 4759 | } |
| 4760 | |
| 4761 | if (MethodDefinition && |
| 4762 | Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86) |
| 4763 | checkObjCMethodX86VectorTypes(*this, ObjCMethod); |
| 4764 | |
| 4765 | |
| 4766 | |
| 4767 | if (const auto *attr = ObjCMethod->getAttr<AvailabilityAttr>()) { |
| 4768 | if (ObjCMethod->isClassMethod() && |
| 4769 | ObjCMethod->getSelector().getAsString() == "load") { |
| 4770 | Diag(attr->getLocation(), diag::warn_availability_on_static_initializer) |
| 4771 | << 0; |
| 4772 | ObjCMethod->dropAttr<AvailabilityAttr>(); |
| 4773 | } |
| 4774 | } |
| 4775 | |
| 4776 | ActOnDocumentableDecl(ObjCMethod); |
| 4777 | |
| 4778 | return ObjCMethod; |
| 4779 | } |
| 4780 | |
| 4781 | bool Sema::CheckObjCDeclScope(Decl *D) { |
| 4782 | |
| 4783 | |
| 4784 | if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) |
| 4785 | return false; |
| 4786 | |
| 4787 | |
| 4788 | |
| 4789 | if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext())) |
| 4790 | return false; |
| 4791 | |
| 4792 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 4793 | D->setInvalidDecl(); |
| 4794 | |
| 4795 | return true; |
| 4796 | } |
| 4797 | |
| 4798 | |
| 4799 | |
| 4800 | void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart, |
| 4801 | IdentifierInfo *ClassName, |
| 4802 | SmallVectorImpl<Decl*> &Decls) { |
| 4803 | |
| 4804 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart); |
| 4805 | if (!Class) { |
| 4806 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 4807 | return; |
| 4808 | } |
| 4809 | if (LangOpts.ObjCRuntime.isNonFragile()) { |
| 4810 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 4811 | return; |
| 4812 | } |
| 4813 | |
| 4814 | |
| 4815 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
| 4816 | Context.DeepCollectObjCIvars(Class, true, Ivars); |
| 4817 | |
| 4818 | for (unsigned i = 0; i < Ivars.size(); i++) { |
| 4819 | const FieldDecl* ID = Ivars[i]; |
| 4820 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD); |
| 4821 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, |
| 4822 | ID->getLocation(), |
| 4823 | ID->getLocation(), |
| 4824 | ID->getIdentifier(), ID->getType(), |
| 4825 | ID->getBitWidth()); |
| 4826 | Decls.push_back(FD); |
| 4827 | } |
| 4828 | |
| 4829 | |
| 4830 | for (SmallVectorImpl<Decl*>::iterator D = Decls.begin(); |
| 4831 | D != Decls.end(); ++D) { |
| 4832 | FieldDecl *FD = cast<FieldDecl>(*D); |
| 4833 | if (getLangOpts().CPlusPlus) |
| 4834 | PushOnScopeChains(FD, S); |
| 4835 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD)) |
| 4836 | Record->addDecl(FD); |
| 4837 | } |
| 4838 | } |
| 4839 | |
| 4840 | |
| 4841 | VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T, |
| 4842 | SourceLocation StartLoc, |
| 4843 | SourceLocation IdLoc, |
| 4844 | IdentifierInfo *Id, |
| 4845 | bool Invalid) { |
| 4846 | |
| 4847 | |
| 4848 | |
| 4849 | |
| 4850 | if (T.getAddressSpace() != LangAS::Default) { |
| 4851 | Diag(IdLoc, diag::err_arg_with_address_space); |
| 4852 | Invalid = true; |
| 4853 | } |
| 4854 | |
| 4855 | |
| 4856 | |
| 4857 | if (Invalid) { |
| 4858 | |
| 4859 | } else if (T->isDependentType()) { |
| 4860 | |
| 4861 | } else if (T->isObjCQualifiedIdType()) { |
| 4862 | Invalid = true; |
| 4863 | Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm); |
| 4864 | } else if (T->isObjCIdType()) { |
| 4865 | |
| 4866 | } else if (!T->isObjCObjectPointerType()) { |
| 4867 | Invalid = true; |
| 4868 | Diag(IdLoc, diag::err_catch_param_not_objc_type); |
| 4869 | } else if (!T->getAs<ObjCObjectPointerType>()->getInterfaceType()) { |
| 4870 | Invalid = true; |
| 4871 | Diag(IdLoc, diag::err_catch_param_not_objc_type); |
| 4872 | } |
| 4873 | |
| 4874 | VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id, |
| 4875 | T, TInfo, SC_None); |
| 4876 | New->setExceptionVariable(true); |
| 4877 | |
| 4878 | |
| 4879 | if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New)) |
| 4880 | Invalid = true; |
| 4881 | |
| 4882 | if (Invalid) |
| 4883 | New->setInvalidDecl(); |
| 4884 | return New; |
| 4885 | } |
| 4886 | |
| 4887 | Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) { |
| 4888 | const DeclSpec &DS = D.getDeclSpec(); |
| 4889 | |
| 4890 | |
| 4891 | |
| 4892 | if (DS.getStorageClassSpec() == DeclSpec::SCS_register) { |
| 4893 | Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm) |
| 4894 | << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc())); |
| 4895 | } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) { |
| 4896 | Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm) |
| 4897 | << DeclSpec::getSpecifierName(SCS); |
| 4898 | } |
| 4899 | if (DS.isInlineSpecified()) |
| 4900 | Diag(DS.getInlineSpecLoc(), diag::err_inline_non_function) |
| 4901 | << getLangOpts().CPlusPlus17; |
| 4902 | if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec()) |
| 4903 | Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(), |
| 4904 | diag::err_invalid_thread) |
| 4905 | << DeclSpec::getSpecifierName(TSCS); |
| 4906 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 4907 | |
| 4908 | DiagnoseFunctionSpecifiers(D.getDeclSpec()); |
| 4909 | |
| 4910 | |
| 4911 | |
| 4912 | if (getLangOpts().CPlusPlus) |
| 4913 | CheckExtraCXXDefaultArguments(D); |
| 4914 | |
| 4915 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 4916 | QualType ExceptionType = TInfo->getType(); |
| 4917 | |
| 4918 | VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, |
| 4919 | D.getSourceRange().getBegin(), |
| 4920 | D.getIdentifierLoc(), |
| 4921 | D.getIdentifier(), |
| 4922 | D.isInvalidType()); |
| 4923 | |
| 4924 | |
| 4925 | if (D.getCXXScopeSpec().isSet()) { |
| 4926 | Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm) |
| 4927 | << D.getCXXScopeSpec().getRange(); |
| 4928 | New->setInvalidDecl(); |
| 4929 | } |
| 4930 | |
| 4931 | |
| 4932 | S->AddDecl(New); |
| 4933 | if (D.getIdentifier()) |
| 4934 | IdResolver.AddDecl(New); |
| 4935 | |
| 4936 | ProcessDeclAttributes(S, New, D); |
| 4937 | |
| 4938 | if (New->hasAttr<BlocksAttr>()) |
| 4939 | Diag(New->getLocation(), diag::err_block_on_nonlocal); |
| 4940 | return New; |
| 4941 | } |
| 4942 | |
| 4943 | |
| 4944 | |
| 4945 | void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI, |
| 4946 | SmallVectorImpl<ObjCIvarDecl*> &Ivars) { |
| 4947 | for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv; |
| 4948 | Iv= Iv->getNextIvar()) { |
| 4949 | QualType QT = Context.getBaseElementType(Iv->getType()); |
| 4950 | if (QT->isRecordType()) |
| 4951 | Ivars.push_back(Iv); |
| 4952 | } |
| 4953 | } |
| 4954 | |
| 4955 | void Sema::DiagnoseUseOfUnimplementedSelectors() { |
| 4956 | |
| 4957 | if (ExternalSource) { |
| 4958 | SmallVector<std::pair<Selector, SourceLocation>, 4> Sels; |
| 4959 | ExternalSource->ReadReferencedSelectors(Sels); |
| 4960 | for (unsigned I = 0, N = Sels.size(); I != N; ++I) |
| 4961 | ReferencedSelectors[Sels[I].first] = Sels[I].second; |
| 4962 | } |
| 4963 | |
| 4964 | |
| 4965 | |
| 4966 | |
| 4967 | if (ReferencedSelectors.empty() || |
| 4968 | !Context.AnyObjCImplementation()) |
| 4969 | return; |
| 4970 | for (auto &SelectorAndLocation : ReferencedSelectors) { |
| 4971 | Selector Sel = SelectorAndLocation.first; |
| 4972 | SourceLocation Loc = SelectorAndLocation.second; |
| 4973 | if (!LookupImplementedMethodInGlobalPool(Sel)) |
| 4974 | Diag(Loc, diag::warn_unimplemented_selector) << Sel; |
| 4975 | } |
| 4976 | } |
| 4977 | |
| 4978 | ObjCIvarDecl * |
| 4979 | Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method, |
| 4980 | const ObjCPropertyDecl *&PDecl) const { |
| 4981 | if (Method->isClassMethod()) |
| 4982 | return nullptr; |
| 4983 | const ObjCInterfaceDecl *IDecl = Method->getClassInterface(); |
| 4984 | if (!IDecl) |
| 4985 | return nullptr; |
| 4986 | Method = IDecl->lookupMethod(Method->getSelector(), , |
| 4987 | , |
| 4988 | ); |
| 4989 | if (!Method || !Method->isPropertyAccessor()) |
| 4990 | return nullptr; |
| 4991 | if ((PDecl = Method->findPropertyDecl())) |
| 4992 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) { |
| 4993 | |
| 4994 | |
| 4995 | |
| 4996 | IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable( |
| 4997 | IV->getIdentifier()); |
| 4998 | return IV; |
| 4999 | } |
| 5000 | return nullptr; |
| 5001 | } |
| 5002 | |
| 5003 | namespace { |
| 5004 | |
| 5005 | |
| 5006 | class UnusedBackingIvarChecker : |
| 5007 | public RecursiveASTVisitor<UnusedBackingIvarChecker> { |
| 5008 | public: |
| 5009 | Sema &S; |
| 5010 | const ObjCMethodDecl *Method; |
| 5011 | const ObjCIvarDecl *IvarD; |
| 5012 | bool AccessedIvar; |
| 5013 | bool InvokedSelfMethod; |
| 5014 | |
| 5015 | UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method, |
| 5016 | const ObjCIvarDecl *IvarD) |
| 5017 | : S(S), Method(Method), IvarD(IvarD), |
| 5018 | AccessedIvar(false), InvokedSelfMethod(false) { |
| 5019 | assert(IvarD); |
| 5020 | } |
| 5021 | |
| 5022 | bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { |
| 5023 | if (E->getDecl() == IvarD) { |
| 5024 | AccessedIvar = true; |
| 5025 | return false; |
| 5026 | } |
| 5027 | return true; |
| 5028 | } |
| 5029 | |
| 5030 | bool VisitObjCMessageExpr(ObjCMessageExpr *E) { |
| 5031 | if (E->getReceiverKind() == ObjCMessageExpr::Instance && |
| 5032 | S.isSelfExpr(E->getInstanceReceiver(), Method)) { |
| 5033 | InvokedSelfMethod = true; |
| 5034 | } |
| 5035 | return true; |
| 5036 | } |
| 5037 | }; |
| 5038 | } |
| 5039 | |
| 5040 | void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S, |
| 5041 | const ObjCImplementationDecl *ImplD) { |
| 5042 | if (S->hasUnrecoverableErrorOccurred()) |
| 5043 | return; |
| 5044 | |
| 5045 | for (const auto *CurMethod : ImplD->instance_methods()) { |
| 5046 | unsigned DIAG = diag::warn_unused_property_backing_ivar; |
| 5047 | SourceLocation Loc = CurMethod->getLocation(); |
| 5048 | if (Diags.isIgnored(DIAG, Loc)) |
| 5049 | continue; |
| 5050 | |
| 5051 | const ObjCPropertyDecl *PDecl; |
| 5052 | const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl); |
| 5053 | if (!IV) |
| 5054 | continue; |
| 5055 | |
| 5056 | UnusedBackingIvarChecker Checker(*this, CurMethod, IV); |
| 5057 | Checker.TraverseStmt(CurMethod->getBody()); |
| 5058 | if (Checker.AccessedIvar) |
| 5059 | continue; |
| 5060 | |
| 5061 | |
| 5062 | |
| 5063 | |
| 5064 | |
| 5065 | if (!IV->isReferenced() || !Checker.InvokedSelfMethod) { |
| 5066 | Diag(Loc, DIAG) << IV; |
| 5067 | Diag(PDecl->getLocation(), diag::note_property_declare); |
| 5068 | } |
| 5069 | } |
| 5070 | } |
| 5071 | |