Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1 | //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective C declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 16 | #include "clang/Sema/ExternalSemaSource.h" |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 23 | bool Sema::DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *property, |
| 24 | ObjCMethodDecl *GetterMethod, |
| 25 | SourceLocation Loc) { |
| 26 | if (GetterMethod && |
| 27 | GetterMethod->getResultType() != property->getType()) { |
| 28 | AssignConvertType result = Incompatible; |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 29 | if (property->getType()->isObjCObjectPointerType()) |
Fariborz Jahanian | 7aaa409 | 2009-05-08 21:10:00 +0000 | [diff] [blame] | 30 | result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType()); |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 31 | if (result != Compatible) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | Diag(Loc, diag::warn_accessor_property_type_mismatch) |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 33 | << property->getDeclName() |
| 34 | << GetterMethod->getSelector(); |
| 35 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 36 | return true; |
| 37 | } |
| 38 | } |
| 39 | return false; |
| 40 | } |
| 41 | |
Steve Naroff | ebf6443 | 2009-02-28 16:59:13 +0000 | [diff] [blame] | 42 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 43 | /// and user declared, in the method definition's AST. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 44 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 45 | assert(getCurMethodDecl() == 0 && "Method parsing confused"); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 46 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Steve Naroff | 394f3f4 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 48 | // If we don't have a valid method decl, simply return. |
| 49 | if (!MDecl) |
| 50 | return; |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 38c5ebd | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 52 | CurFunctionNeedsScopeChecking = false; |
| 53 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 54 | // Allow the rest of sema to find private method decl implementations. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 55 | if (MDecl->isInstanceMethod()) |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 56 | AddInstanceMethodToGlobalPool(MDecl); |
| 57 | else |
| 58 | AddFactoryMethodToGlobalPool(MDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 60 | // Allow all of Sema to see that we are entering a method definition. |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 61 | PushDeclContext(FnBodyScope, MDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 62 | |
| 63 | // Create Decl objects for each parameter, entrring them in the scope for |
| 64 | // binding to their use. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 65 | |
| 66 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 67 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 451318c | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 69 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 70 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 72 | // Introduce all of the other parameters into this scope. |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 73 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
| 74 | E = MDecl->param_end(); PI != E; ++PI) |
| 75 | if ((*PI)->getIdentifier()) |
| 76 | PushOnScopeChains(*PI, FnBodyScope); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 79 | Sema::DeclPtrTy Sema:: |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 80 | ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
| 81 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 82 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 83 | const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 84 | const SourceLocation *ProtoLocs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 85 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 86 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 88 | // Check for another declaration kind with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 89 | NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 90 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 91 | // Maybe we will complain about the shadowed template parameter. |
| 92 | DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl); |
| 93 | // Just pretend that we didn't see the previous declaration. |
| 94 | PrevDecl = 0; |
| 95 | } |
| 96 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 97 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 98 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 99 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 102 | ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 103 | if (IDecl) { |
| 104 | // Class already seen. Is it a forward declaration? |
Steve Naroff | cfe8bf3 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 105 | if (!IDecl->isForwardDecl()) { |
Chris Lattner | 1829a6d | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 106 | IDecl->setInvalidDecl(); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 107 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName(); |
Chris Lattner | b8b96af | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 108 | Diag(IDecl->getLocation(), diag::note_previous_definition); |
| 109 | |
Steve Naroff | cfe8bf3 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 110 | // Return the previous class interface. |
| 111 | // FIXME: don't leak the objects passed in! |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 112 | return DeclPtrTy::make(IDecl); |
Steve Naroff | cfe8bf3 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 113 | } else { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 114 | IDecl->setLocation(AtInterfaceLoc); |
| 115 | IDecl->setForwardDecl(false); |
Steve Naroff | 8b26cbd | 2009-09-11 00:12:01 +0000 | [diff] [blame] | 116 | IDecl->setClassLoc(ClassLoc); |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 117 | |
| 118 | // Since this ObjCInterfaceDecl was created by a forward declaration, |
| 119 | // we now add it to the DeclContext since it wasn't added before |
| 120 | // (see ActOnForwardClassDeclaration). |
| 121 | CurContext->addDecl(IDecl); |
| 122 | |
Fariborz Jahanian | 5f8f857 | 2009-11-17 19:08:08 +0000 | [diff] [blame] | 123 | if (AttrList) |
| 124 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | b752f28 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 126 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, |
Steve Naroff | d6a07aa | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 128 | ClassName, ClassLoc); |
Daniel Dunbar | f641492 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 129 | if (AttrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 130 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Steve Naroff | a7503a7 | 2009-04-23 15:15:40 +0000 | [diff] [blame] | 132 | PushOnScopeChains(IDecl, TUScope); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 133 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 135 | if (SuperName) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 136 | // Check if a different kind of symbol declared in this scope. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 137 | PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 138 | |
| 139 | if (!PrevDecl) { |
| 140 | // Try to correct for a typo in the superclass name. |
| 141 | LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName); |
| 142 | if (CorrectTypo(R, TUScope, 0) && |
| 143 | (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) { |
| 144 | Diag(SuperLoc, diag::err_undef_superclass_suggest) |
| 145 | << SuperName << ClassName << PrevDecl->getDeclName(); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 146 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) |
| 147 | << PrevDecl->getDeclName(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 151 | if (PrevDecl == IDecl) { |
| 152 | Diag(SuperLoc, diag::err_recursive_superclass) |
| 153 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 154 | IDecl->setLocEnd(ClassLoc); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 155 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | ObjCInterfaceDecl *SuperClassDecl = |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 157 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 158 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 159 | // Diagnose classes that inherit from deprecated classes. |
| 160 | if (SuperClassDecl) |
| 161 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 163 | if (PrevDecl && SuperClassDecl == 0) { |
| 164 | // The previous declaration was not a class decl. Check if we have a |
| 165 | // typedef. If we do, get the underlying class type. |
| 166 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) { |
| 167 | QualType T = TDecl->getUnderlyingType(); |
| 168 | if (T->isObjCInterfaceType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 169 | if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 170 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| 171 | } |
| 172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 174 | // This handles the following case: |
| 175 | // |
| 176 | // typedef int SuperClass; |
| 177 | // @interface MyClass : SuperClass {} @end |
| 178 | // |
| 179 | if (!SuperClassDecl) { |
| 180 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 181 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 185 | if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) { |
| 186 | if (!SuperClassDecl) |
| 187 | Diag(SuperLoc, diag::err_undef_superclass) |
| 188 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| 189 | else if (SuperClassDecl->isForwardDecl()) |
| 190 | Diag(SuperLoc, diag::err_undef_superclass) |
| 191 | << SuperClassDecl->getDeclName() << ClassName |
| 192 | << SourceRange(AtInterfaceLoc, ClassLoc); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 193 | } |
Fariborz Jahanian | fdee089 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 194 | IDecl->setSuperClass(SuperClassDecl); |
| 195 | IDecl->setSuperClassLoc(SuperLoc); |
| 196 | IDecl->setLocEnd(SuperLoc); |
Steve Naroff | 818cb9e | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 197 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 198 | } else { // we have a root class. |
| 199 | IDecl->setLocEnd(ClassLoc); |
| 200 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Steve Naroff | cfe8bf3 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 202 | /// Check then save referenced protocols. |
Chris Lattner | 06036d3 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 203 | if (NumProtoRefs) { |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 204 | IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 205 | ProtoLocs, Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 206 | IDecl->setLocEnd(EndProtoLoc); |
| 207 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 209 | CheckObjCDeclScope(IDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 210 | return DeclPtrTy::make(IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /// ActOnCompatiblityAlias - this action is called after complete parsing of |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 214 | /// @compatibility_alias declaration. It sets up the alias relationships. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 215 | Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | IdentifierInfo *AliasName, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 217 | SourceLocation AliasLocation, |
| 218 | IdentifierInfo *ClassName, |
| 219 | SourceLocation ClassLocation) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 220 | // Look for previous declaration of alias name |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 221 | NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 222 | if (ADecl) { |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 223 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 224 | Diag(AliasLocation, diag::warn_previous_alias_decl); |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 225 | else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 226 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 227 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 228 | return DeclPtrTy(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 229 | } |
| 230 | // Check for class declaration |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 231 | NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 232 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) { |
| 233 | QualType T = TDecl->getUnderlyingType(); |
| 234 | if (T->isObjCInterfaceType()) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 235 | if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) { |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 236 | ClassName = IDecl->getIdentifier(); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 237 | CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Fariborz Jahanian | 305c658 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 241 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 242 | if (CDecl == 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 243 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 244 | if (CDeclU) |
Chris Lattner | 8b265bd | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 245 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 246 | return DeclPtrTy(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 247 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 249 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 251 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 253 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 516ff43 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 254 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 255 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 256 | return DeclPtrTy::make(AliasDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 259 | void Sema::CheckForwardProtocolDeclarationForCircularDependency( |
| 260 | IdentifierInfo *PName, |
| 261 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | const ObjCList<ObjCProtocolDecl> &PList) { |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 263 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 264 | E = PList.end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 266 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) { |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 267 | if (PDecl->getIdentifier() == PName) { |
| 268 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 269 | Diag(PrevLoc, diag::note_previous_definition); |
| 270 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 272 | PDecl->getLocation(), PDecl->getReferencedProtocols()); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 277 | Sema::DeclPtrTy |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 278 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 279 | IdentifierInfo *ProtocolName, |
| 280 | SourceLocation ProtocolLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 281 | const DeclPtrTy *ProtoRefs, |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 282 | unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 283 | const SourceLocation *ProtoLocs, |
Daniel Dunbar | 246e70f | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 284 | SourceLocation EndProtoLoc, |
| 285 | AttributeList *AttrList) { |
| 286 | // FIXME: Deal with AttrList. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 287 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 288 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 289 | if (PDecl) { |
| 290 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 291 | if (!PDecl->isForwardDecl()) { |
Fariborz Jahanian | e2573e5 | 2009-04-06 23:43:32 +0000 | [diff] [blame] | 292 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
Chris Lattner | b8b96af | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 293 | Diag(PDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 294 | // Just return the protocol we already had. |
| 295 | // FIXME: don't leak the objects passed in! |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 296 | return DeclPtrTy::make(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 297 | } |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 298 | ObjCList<ObjCProtocolDecl> PList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
Steve Naroff | 61d6852 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 300 | CheckForwardProtocolDeclarationForCircularDependency( |
| 301 | ProtocolName, ProtocolLoc, PDecl->getLocation(), PList); |
| 302 | PList.Destroy(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Steve Naroff | f11b508 | 2008-08-13 16:39:22 +0000 | [diff] [blame] | 304 | // Make sure the cached decl gets a valid start location. |
| 305 | PDecl->setLocation(AtProtoInterfaceLoc); |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 306 | PDecl->setForwardDecl(false); |
Chris Lattner | 439e71f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 307 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 309 | AtProtoInterfaceLoc,ProtocolName); |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 310 | PushOnScopeChains(PDecl, TUScope); |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 311 | PDecl->setForwardDecl(false); |
Chris Lattner | cca59d7 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 312 | } |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 313 | if (AttrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 314 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 315 | if (NumProtoRefs) { |
Chris Lattner | c858105 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 316 | /// Check then save referenced protocols. |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 317 | PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
| 318 | ProtoLocs, Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 319 | PDecl->setLocEnd(EndProtoLoc); |
| 320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
| 322 | CheckObjCDeclScope(PDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 323 | return DeclPtrTy::make(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 327 | /// issues an error if they are not declared. It returns list of |
| 328 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 329 | void |
Chris Lattner | e13b959 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 330 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 331 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 332 | unsigned NumProtocols, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 333 | llvm::SmallVectorImpl<DeclPtrTy> &Protocols) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 334 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 335 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first); |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 336 | if (!PDecl) { |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 337 | LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second, |
| 338 | LookupObjCProtocolName); |
| 339 | if (CorrectTypo(R, TUScope, 0) && |
| 340 | (PDecl = R.getAsSingle<ObjCProtocolDecl>())) { |
| 341 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest) |
| 342 | << ProtocolId[i].first << R.getLookupName(); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 343 | Diag(PDecl->getLocation(), diag::note_previous_decl) |
| 344 | << PDecl->getDeclName(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | if (!PDecl) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 349 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 350 | << ProtocolId[i].first; |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 351 | continue; |
| 352 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 354 | (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); |
Chris Lattner | eacc392 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 355 | |
| 356 | // If this is a forward declaration and we are supposed to warn in this |
| 357 | // case, do it. |
| 358 | if (WarnOnDeclarations && PDecl->isForwardDecl()) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 359 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 360 | << ProtocolId[i].first; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 361 | Protocols.push_back(DeclPtrTy::make(PDecl)); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 365 | /// DiagnosePropertyMismatch - Compares two properties for their |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 366 | /// attributes and types and warns on a variety of inconsistencies. |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 367 | /// |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 368 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 370 | ObjCPropertyDecl *SuperProperty, |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 371 | const IdentifierInfo *inheritedName) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 373 | Property->getPropertyAttributes(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 375 | SuperProperty->getPropertyAttributes(); |
| 376 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 377 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 378 | Diag(Property->getLocation(), diag::warn_readonly_property) |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 379 | << Property->getDeclName() << inheritedName; |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 380 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 381 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 382 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 383 | << Property->getDeclName() << "copy" << inheritedName; |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 384 | else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) |
| 385 | != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 386 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 387 | << Property->getDeclName() << "retain" << inheritedName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 389 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 390 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 391 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 392 | << Property->getDeclName() << "atomic" << inheritedName; |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 393 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 394 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | << Property->getDeclName() << "setter" << inheritedName; |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 396 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 397 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 398 | << Property->getDeclName() << "getter" << inheritedName; |
Steve Naroff | 15edf0d | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 399 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | QualType LHSType = |
Steve Naroff | 15edf0d | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 401 | Context.getCanonicalType(SuperProperty->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | QualType RHSType = |
Steve Naroff | 15edf0d | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 403 | Context.getCanonicalType(Property->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Steve Naroff | 15edf0d | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 405 | if (!Context.typesAreCompatible(LHSType, RHSType)) { |
| 406 | // FIXME: Incorporate this test with typesAreCompatible. |
| 407 | if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType()) |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 408 | if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false)) |
Steve Naroff | 15edf0d | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 409 | return; |
| 410 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) |
| 411 | << Property->getType() << SuperProperty->getType() << inheritedName; |
| 412 | } |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /// ComparePropertiesInBaseAndSuper - This routine compares property |
| 416 | /// declarations in base and its super class, if any, and issues |
| 417 | /// diagnostics in a variety of inconsistant situations. |
| 418 | /// |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 419 | void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 420 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 421 | if (!SDecl) |
| 422 | return; |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 423 | // FIXME: O(N^2) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 424 | for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(), |
| 425 | E = SDecl->prop_end(); S != E; ++S) { |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 426 | ObjCPropertyDecl *SuperPDecl = (*S); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 427 | // Does property in super class has declaration in current class? |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 428 | for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(), |
| 429 | E = IDecl->prop_end(); I != E; ++I) { |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 430 | ObjCPropertyDecl *PDecl = (*I); |
| 431 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | DiagnosePropertyMismatch(PDecl, SuperPDecl, |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 433 | SDecl->getIdentifier()); |
Fariborz Jahanian | b5e0224 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 438 | /// MatchOneProtocolPropertiesInClass - This routine goes thru the list |
| 439 | /// of properties declared in a protocol and compares their attribute against |
| 440 | /// the same property declared in the class or category. |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 441 | void |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 442 | Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 443 | ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | 1ac2bc4 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 444 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 445 | if (!IDecl) { |
| 446 | // Category |
| 447 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 448 | assert (CatDecl && "MatchOneProtocolPropertiesInClass"); |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 449 | if (!CatDecl->IsClassExtension()) |
| 450 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 451 | E = PDecl->prop_end(); P != E; ++P) { |
| 452 | ObjCPropertyDecl *Pr = (*P); |
| 453 | ObjCCategoryDecl::prop_iterator CP, CE; |
| 454 | // Is this property already in category's list of properties? |
| 455 | for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); CP != CE; ++CP) |
| 456 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 457 | break; |
| 458 | if (CP != CE) |
| 459 | // Property protocol already exist in class. Diagnose any mismatch. |
| 460 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
| 461 | } |
Fariborz Jahanian | 1ac2bc4 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 462 | return; |
| 463 | } |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 464 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 465 | E = PDecl->prop_end(); P != E; ++P) { |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 466 | ObjCPropertyDecl *Pr = (*P); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 467 | ObjCInterfaceDecl::prop_iterator CP, CE; |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 468 | // Is this property already in class's list of properties? |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 469 | for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP) |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 470 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 471 | break; |
Fariborz Jahanian | a66793e | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 472 | if (CP != CE) |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 473 | // Property protocol already exist in class. Diagnose any mismatch. |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 474 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 475 | } |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 478 | /// CompareProperties - This routine compares properties |
| 479 | /// declared in 'ClassOrProtocol' objects (which can be a class or an |
| 480 | /// inherited protocol with the list of properties for class/category 'CDecl' |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 481 | /// |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 482 | void Sema::CompareProperties(Decl *CDecl, |
| 483 | DeclPtrTy ClassOrProtocol) { |
| 484 | Decl *ClassDecl = ClassOrProtocol.getAs<Decl>(); |
Fariborz Jahanian | 1ac2bc4 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 485 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 486 | |
| 487 | if (!IDecl) { |
| 488 | // Category |
| 489 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 490 | assert (CatDecl && "CompareProperties"); |
Fariborz Jahanian | 1ac2bc4 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 491 | if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
| 492 | for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 493 | E = MDecl->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 494 | // Match properties of category with those of protocol (*P) |
| 495 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 497 | // Go thru the list of protocols for this category and recursively match |
| 498 | // their properties with those in the category. |
Fariborz Jahanian | 1ac2bc4 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 499 | for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), |
| 500 | E = CatDecl->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 501 | CompareProperties(CatDecl, DeclPtrTy::make(*P)); |
Fariborz Jahanian | 1ac2bc4 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 502 | } else { |
| 503 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 504 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 505 | E = MD->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 506 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
Fariborz Jahanian | 1ac2bc4 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 507 | } |
| 508 | return; |
| 509 | } |
| 510 | |
Chris Lattner | b752f28 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 511 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 512 | for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 513 | E = MDecl->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 514 | // Match properties of class IDecl with those of protocol (*P). |
| 515 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 517 | // Go thru the list of protocols for this class and recursively match |
| 518 | // their properties with those declared in the class. |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 519 | for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(), |
| 520 | E = IDecl->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 521 | CompareProperties(IDecl, DeclPtrTy::make(*P)); |
Chris Lattner | b752f28 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 522 | } else { |
Argyrios Kyrtzidis | e8f0d30 | 2008-07-21 09:18:38 +0000 | [diff] [blame] | 523 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 524 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 525 | E = MD->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 526 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
Chris Lattner | b752f28 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 527 | } |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Fariborz Jahanian | 78c39c7 | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 530 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 531 | /// a class method in its extension. |
| 532 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 534 | ObjCInterfaceDecl *ID) { |
| 535 | if (!ID) |
| 536 | return; // Possibly due to previous error |
| 537 | |
| 538 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 539 | for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(), |
| 540 | e = ID->meth_end(); i != e; ++i) { |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 541 | ObjCMethodDecl *MD = *i; |
| 542 | MethodMap[MD->getSelector()] = MD; |
| 543 | } |
| 544 | |
| 545 | if (MethodMap.empty()) |
| 546 | return; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 547 | for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(), |
| 548 | e = CAT->meth_end(); i != e; ++i) { |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 549 | ObjCMethodDecl *Method = *i; |
| 550 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
| 551 | if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
| 552 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 553 | << Method->getDeclName(); |
| 554 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
Chris Lattner | 58fe03b | 2009-04-12 08:43:13 +0000 | [diff] [blame] | 559 | /// ActOnForwardProtocolDeclaration - Handle @protocol foo; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 560 | Action::DeclPtrTy |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 561 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 562 | const IdentifierLocPair *IdentList, |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 563 | unsigned NumElts, |
| 564 | AttributeList *attrList) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 565 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 566 | llvm::SmallVector<SourceLocation, 8> ProtoLocs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 568 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 569 | IdentifierInfo *Ident = IdentList[i].first; |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 570 | ObjCProtocolDecl *PDecl = LookupProtocol(Ident); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 571 | if (PDecl == 0) { // Not already seen? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 573 | IdentList[i].second, Ident); |
Douglas Gregor | 6e378de | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 574 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 575 | } |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 576 | if (attrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 577 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 578 | Protocols.push_back(PDecl); |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 579 | ProtoLocs.push_back(IdentList[i].second); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 580 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
| 582 | ObjCForwardProtocolDecl *PDecl = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 583 | ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 584 | Protocols.data(), Protocols.size(), |
| 585 | ProtoLocs.data()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 586 | CurContext->addDecl(PDecl); |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 587 | CheckObjCDeclScope(PDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 588 | return DeclPtrTy::make(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 591 | Sema::DeclPtrTy Sema:: |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 592 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 593 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 594 | IdentifierInfo *CategoryName, |
| 595 | SourceLocation CategoryLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 596 | const DeclPtrTy *ProtoRefs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 597 | unsigned NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 598 | const SourceLocation *ProtoLocs, |
Chris Lattner | 7caeabd | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 599 | SourceLocation EndProtoLoc) { |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 600 | ObjCCategoryDecl *CDecl = 0; |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 601 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc); |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 602 | if (!CategoryName) { |
| 603 | // Class extensions require a special treatment. Use an existing one. |
| 604 | for (CDecl = IDecl->getCategoryList(); CDecl; |
| 605 | CDecl = CDecl->getNextClassCategory()) |
| 606 | if (CDecl->IsClassExtension()) |
| 607 | break; |
Fariborz Jahanian | 7c453b3 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 608 | } |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 609 | if (!CDecl) { |
| 610 | CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, ClassLoc, |
| 611 | CategoryLoc, CategoryName); |
| 612 | // FIXME: PushOnScopeChains? |
| 613 | CurContext->addDecl(CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 614 | |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 615 | /// Check that class of this category is already completely declared. |
| 616 | if (!IDecl || IDecl->isForwardDecl()) { |
| 617 | CDecl->setInvalidDecl(); |
| 618 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
| 619 | return DeclPtrTy::make(CDecl); |
| 620 | } |
| 621 | |
| 622 | CDecl->setClassInterface(IDecl); |
| 623 | // Insert first use of class extension to the list of class's categories. |
| 624 | if (!CategoryName) |
| 625 | CDecl->insertNextClassCategory(); |
| 626 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 16b34b4 | 2009-02-16 21:30:01 +0000 | [diff] [blame] | 628 | // If the interface is deprecated, warn about it. |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 629 | (void)DiagnoseUseOfDecl(IDecl, ClassLoc); |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 630 | |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 631 | if (CategoryName) { |
| 632 | /// Check for duplicate interface declaration for this category |
| 633 | ObjCCategoryDecl *CDeclChain; |
| 634 | for (CDeclChain = IDecl->getCategoryList(); CDeclChain; |
| 635 | CDeclChain = CDeclChain->getNextClassCategory()) { |
| 636 | if (CDeclChain->getIdentifier() == CategoryName) { |
| 637 | // Class extensions can be declared multiple times. |
| 638 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 639 | << ClassName << CategoryName; |
| 640 | Diag(CDeclChain->getLocation(), diag::note_previous_definition); |
| 641 | break; |
| 642 | } |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 643 | } |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 644 | if (!CDeclChain) |
| 645 | CDecl->insertNextClassCategory(); |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 646 | } |
Chris Lattner | 70f1954 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 648 | if (NumProtoRefs) { |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 649 | CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 650 | ProtoLocs, Context); |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 651 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 652 | if (CDecl->IsClassExtension()) |
Fariborz Jahanian | 339798e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 653 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs, |
Douglas Gregor | 18df52b | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 654 | NumProtoRefs, ProtoLocs, |
| 655 | Context); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 656 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 658 | CheckObjCDeclScope(CDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 659 | return DeclPtrTy::make(CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 663 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 664 | /// object. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 665 | Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation( |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 666 | SourceLocation AtCatImplLoc, |
| 667 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 668 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 669 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc); |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 670 | ObjCCategoryDecl *CatIDecl = 0; |
| 671 | if (IDecl) { |
| 672 | CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| 673 | if (!CatIDecl) { |
| 674 | // Category @implementation with no corresponding @interface. |
| 675 | // Create and install one. |
| 676 | CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(), |
Douglas Gregor | 3db211b | 2010-01-16 16:38:58 +0000 | [diff] [blame] | 677 | SourceLocation(), SourceLocation(), |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 678 | CatName); |
| 679 | CatIDecl->setClassInterface(IDecl); |
| 680 | CatIDecl->insertNextClassCategory(); |
| 681 | } |
| 682 | } |
| 683 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | ObjCCategoryImplDecl *CDecl = |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 685 | ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName, |
| 686 | IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 687 | /// Check that class of this category is already completely declared. |
| 688 | if (!IDecl || IDecl->isForwardDecl()) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 689 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 691 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 692 | CurContext->addDecl(CDecl); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 693 | |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 694 | /// Check that CatName, category name, is not used in another implementation. |
| 695 | if (CatIDecl) { |
| 696 | if (CatIDecl->getImplementation()) { |
| 697 | Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| 698 | << CatName; |
| 699 | Diag(CatIDecl->getImplementation()->getLocation(), |
| 700 | diag::note_previous_definition); |
| 701 | } else |
| 702 | CatIDecl->setImplementation(CDecl); |
| 703 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 705 | CheckObjCDeclScope(CDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 706 | return DeclPtrTy::make(CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 709 | Sema::DeclPtrTy Sema::ActOnStartClassImplementation( |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 710 | SourceLocation AtClassImplLoc, |
| 711 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | IdentifierInfo *SuperClassname, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 713 | SourceLocation SuperClassLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 714 | ObjCInterfaceDecl* IDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 715 | // Check for another declaration kind with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 716 | NamedDecl *PrevDecl |
| 717 | = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 718 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 719 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 720 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 721 | } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
| 722 | // If this is a forward declaration of an interface, warn. |
| 723 | if (IDecl->isForwardDecl()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 724 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
Fariborz Jahanian | 77a6be4 | 2009-04-23 21:49:04 +0000 | [diff] [blame] | 725 | IDecl = 0; |
| 726 | } |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 727 | } else { |
| 728 | // We did not find anything with the name ClassName; try to correct for |
| 729 | // typos in the class name. |
| 730 | LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName); |
| 731 | if (CorrectTypo(R, TUScope, 0) && |
| 732 | (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) { |
Douglas Gregor | a6f2638 | 2010-01-06 23:44:25 +0000 | [diff] [blame] | 733 | // Suggest the (potentially) correct interface name. However, put the |
| 734 | // fix-it hint itself in a separate note, since changing the name in |
| 735 | // the warning would make the fix-it change semantics.However, don't |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 736 | // provide a code-modification hint or use the typo name for recovery, |
| 737 | // because this is just a warning. The program may actually be correct. |
| 738 | Diag(ClassLoc, diag::warn_undef_interface_suggest) |
| 739 | << ClassName << R.getLookupName(); |
Douglas Gregor | a6f2638 | 2010-01-06 23:44:25 +0000 | [diff] [blame] | 740 | Diag(IDecl->getLocation(), diag::note_previous_decl) |
| 741 | << R.getLookupName() |
| 742 | << CodeModificationHint::CreateReplacement(ClassLoc, |
| 743 | R.getLookupName().getAsString()); |
Douglas Gregor | 95ff742 | 2010-01-04 17:27:12 +0000 | [diff] [blame] | 744 | IDecl = 0; |
| 745 | } else { |
| 746 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| 747 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 748 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 750 | // Check that super class name is valid class name |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 751 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 752 | if (SuperClassname) { |
| 753 | // Check if a different kind of symbol declared in this scope. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 754 | PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 755 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 756 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 757 | << SuperClassname; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 758 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 759 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 761 | if (!SDecl) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 762 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 763 | << SuperClassname << ClassName; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 764 | else if (IDecl && IDecl->getSuperClass() != SDecl) { |
| 765 | // This implementation and its interface do not have the same |
| 766 | // super class. |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 767 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 768 | << SDecl->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 769 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 774 | if (!IDecl) { |
| 775 | // Legacy case of @implementation with no corresponding @interface. |
| 776 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | f641492 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 777 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 778 | // FIXME: Do we support attributes on the @implementation? If so we should |
| 779 | // copy them over. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 781 | ClassName, ClassLoc, false, true); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 782 | IDecl->setSuperClass(SDecl); |
| 783 | IDecl->setLocEnd(ClassLoc); |
Douglas Gregor | 8b9fb30 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 784 | |
| 785 | PushOnScopeChains(IDecl, TUScope); |
Daniel Dunbar | 24c8991 | 2009-04-21 21:41:56 +0000 | [diff] [blame] | 786 | } else { |
| 787 | // Mark the interface as being completed, even if it was just as |
| 788 | // @class ....; |
| 789 | // declaration; the user cannot reopen it. |
| 790 | IDecl->setForwardDecl(false); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 791 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
| 793 | ObjCImplementationDecl* IMPDecl = |
| 794 | ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 795 | IDecl, SDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 797 | if (CheckObjCDeclScope(IMPDecl)) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 798 | return DeclPtrTy::make(IMPDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 800 | // Check that there is no duplicate implementation of this class. |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 801 | if (IDecl->getImplementation()) { |
Chris Lattner | 75c9cae | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 802 | // FIXME: Don't leak everything! |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 803 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 804 | Diag(IDecl->getImplementation()->getLocation(), |
| 805 | diag::note_previous_definition); |
| 806 | } else { // add it to the list. |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 807 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 8fc463a | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 808 | PushOnScopeChains(IMPDecl, TUScope); |
Argyrios Kyrtzidis | 8a1d722 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 809 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 810 | return DeclPtrTy::make(IMPDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 813 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 814 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 815 | SourceLocation RBrace) { |
| 816 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 817 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 818 | if (!IDecl) |
| 819 | return; |
| 820 | /// Check case of non-existing @interface decl. |
| 821 | /// (legacy objective-c @implementation decl without an @interface decl). |
| 822 | /// Add implementations's ivar to the synthesize class's ivar list. |
Steve Naroff | 33feeb0 | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 823 | if (IDecl->isImplicitInterfaceDecl()) { |
Chris Lattner | 38af2de | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 824 | IDecl->setLocEnd(RBrace); |
Fariborz Jahanian | 3a21cd9 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 825 | // Add ivar's to class's DeclContext. |
| 826 | for (unsigned i = 0, e = numIvars; i != e; ++i) { |
Fariborz Jahanian | 2f14c4d | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 827 | ivars[i]->setLexicalDeclContext(ImpDecl); |
| 828 | IDecl->makeDeclVisibleInContext(ivars[i], false); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 829 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 3a21cd9 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | // If implementation has empty ivar list, just return. |
| 835 | if (numIvars == 0) |
| 836 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 838 | assert(ivars && "missing @implementation ivars"); |
Fariborz Jahanian | bd94d44 | 2010-02-19 20:58:54 +0000 | [diff] [blame] | 839 | if (LangOpts.ObjCNonFragileABI2) { |
| 840 | if (ImpDecl->getSuperClass()) |
| 841 | Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| 842 | for (unsigned i = 0; i < numIvars; i++) { |
| 843 | ObjCIvarDecl* ImplIvar = ivars[i]; |
| 844 | if (const ObjCIvarDecl *ClsIvar = |
| 845 | IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| 846 | Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| 847 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| 848 | continue; |
| 849 | } |
| 850 | if (ImplIvar->getAccessControl() != ObjCIvarDecl::Private) |
| 851 | Diag(ImplIvar->getLocation(), diag::err_non_private_ivar_declaration); |
| 852 | // Instance ivar to Implementation's DeclContext. |
| 853 | ImplIvar->setLexicalDeclContext(ImpDecl); |
| 854 | IDecl->makeDeclVisibleInContext(ImplIvar, false); |
| 855 | ImpDecl->addDecl(ImplIvar); |
| 856 | } |
| 857 | return; |
| 858 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 859 | // Check interface's Ivar list against those in the implementation. |
| 860 | // names and types must match. |
| 861 | // |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 862 | unsigned j = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 864 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 865 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 866 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 867 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 868 | assert (ImplIvar && "missing implementation ivar"); |
| 869 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 871 | // First, make sure the types match. |
Chris Lattner | 1b63eef | 2008-07-27 00:05:05 +0000 | [diff] [blame] | 872 | if (Context.getCanonicalType(ImplIvar->getType()) != |
| 873 | Context.getCanonicalType(ClsIvar->getType())) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 874 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 875 | << ImplIvar->getIdentifier() |
| 876 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 877 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 878 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) { |
| 879 | Expr *ImplBitWidth = ImplIvar->getBitWidth(); |
| 880 | Expr *ClsBitWidth = ClsIvar->getBitWidth(); |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 881 | if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() != |
| 882 | ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) { |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 883 | Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth) |
| 884 | << ImplIvar->getIdentifier(); |
| 885 | Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition); |
| 886 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 887 | } |
Steve Naroff | ca33129 | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 888 | // Make sure the names are identical. |
| 889 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 890 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 891 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 892 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 893 | } |
| 894 | --numIvars; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 895 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 897 | if (numIvars > 0) |
Chris Lattner | 0e39105 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 898 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 609e4c7 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 899 | else if (IVI != IVE) |
Chris Lattner | 0e39105 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 900 | Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 903 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
| 904 | bool &IncompleteImpl) { |
| 905 | if (!IncompleteImpl) { |
| 906 | Diag(ImpLoc, diag::warn_incomplete_impl); |
| 907 | IncompleteImpl = true; |
| 908 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 909 | Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName(); |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Fariborz Jahanian | 8daab97 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 912 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 913 | ObjCMethodDecl *IntfMethodDecl) { |
Chris Lattner | 5272b7f | 2009-04-11 18:01:59 +0000 | [diff] [blame] | 914 | if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(), |
Fariborz Jahanian | 2574a68 | 2009-05-14 23:52:54 +0000 | [diff] [blame] | 915 | ImpMethodDecl->getResultType()) && |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 916 | !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(), |
| 917 | ImpMethodDecl->getResultType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 918 | Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types) |
Chris Lattner | 3aff919 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 919 | << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType() |
| 920 | << ImpMethodDecl->getResultType(); |
Fariborz Jahanian | 8daab97 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 921 | Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition); |
| 922 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | |
Chris Lattner | 3aff919 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 924 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
| 925 | IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end(); |
| 926 | IM != EM; ++IM, ++IF) { |
Fariborz Jahanian | 3393f81 | 2009-11-18 18:56:09 +0000 | [diff] [blame] | 927 | QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType(); |
| 928 | QualType ParmImpTy = (*IM)->getType().getUnqualifiedType(); |
| 929 | if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) || |
| 930 | Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy)) |
Chris Lattner | 3aff919 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 931 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 932 | |
| 933 | Diag((*IM)->getLocation(), diag::warn_conflicting_param_types) |
Chris Lattner | 3aff919 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 934 | << ImpMethodDecl->getDeclName() << (*IF)->getType() |
| 935 | << (*IM)->getType(); |
Chris Lattner | d1e0f5a | 2009-04-11 20:14:49 +0000 | [diff] [blame] | 936 | Diag((*IF)->getLocation(), diag::note_previous_definition); |
Chris Lattner | 3aff919 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 937 | } |
Fariborz Jahanian | 8daab97 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 940 | /// isPropertyReadonly - Return true if property is readonly, by searching |
| 941 | /// for the property in the class and in its categories and implementations |
| 942 | /// |
| 943 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, |
Steve Naroff | 22dc0b0 | 2009-02-26 19:11:32 +0000 | [diff] [blame] | 944 | ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 945 | // by far the most common case. |
| 946 | if (!PDecl->isReadOnly()) |
| 947 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | // Even if property is ready only, if interface has a user defined setter, |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 949 | // it is not considered read only. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 950 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 951 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 953 | // Main class has the property as 'readonly'. Must search |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | // through the category list to see if the property's |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 955 | // attribute has been over-ridden to 'readwrite'. |
| 956 | for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); |
| 957 | Category; Category = Category->getNextClassCategory()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | // Even if property is ready only, if a category has a user defined setter, |
| 959 | // it is not considered read only. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 960 | if (Category->getInstanceMethod(PDecl->getSetterName())) |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 961 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 962 | ObjCPropertyDecl *P = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 963 | Category->FindPropertyDeclaration(PDecl->getIdentifier()); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 964 | if (P && !P->isReadOnly()) |
| 965 | return false; |
| 966 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 967 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 968 | // Also, check for definition of a setter method in the implementation if |
| 969 | // all else failed. |
| 970 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | if (ObjCImplementationDecl *IMD = |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 972 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 973 | if (IMD->getInstanceMethod(PDecl->getSetterName())) |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 974 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | } else if (ObjCCategoryImplDecl *CIMD = |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 976 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 977 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 978 | return false; |
| 979 | } |
| 980 | } |
Steve Naroff | 22dc0b0 | 2009-02-26 19:11:32 +0000 | [diff] [blame] | 981 | // Lastly, look through the implementation (if one is in scope). |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 982 | if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 983 | if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) |
Steve Naroff | 22dc0b0 | 2009-02-26 19:11:32 +0000 | [diff] [blame] | 984 | return false; |
Fariborz Jahanian | 50efe04 | 2009-04-06 16:59:10 +0000 | [diff] [blame] | 985 | // If all fails, look at the super class. |
| 986 | if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) |
| 987 | return isPropertyReadonly(PDecl, SIDecl); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 988 | return true; |
| 989 | } |
| 990 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 991 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 992 | /// improve the efficiency of selector lookups and type checking by associating |
| 993 | /// with each protocol / interface / category the flattened instance tables. If |
| 994 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 995 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 996 | |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 997 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 998 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 999 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 1000 | ObjCProtocolDecl *PDecl, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1001 | bool& IncompleteImpl, |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 1002 | const llvm::DenseSet<Selector> &InsMap, |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1003 | const llvm::DenseSet<Selector> &ClsMap, |
| 1004 | ObjCInterfaceDecl *IDecl) { |
| 1005 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1006 | ObjCInterfaceDecl *NSIDecl = 0; |
| 1007 | if (getLangOptions().NeXTRuntime) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | // check to see if class implements forwardInvocation method and objects |
| 1009 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1010 | // from one object to another. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | // Under such conditions, which means that every method possible is |
| 1012 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1013 | // found" warnings. |
| 1014 | // FIXME: Use a general GetUnarySelector method for this. |
| 1015 | IdentifierInfo* II = &Context.Idents.get("forwardInvocation"); |
| 1016 | Selector fISelector = Context.Selectors.getSelector(1, &II); |
| 1017 | if (InsMap.count(fISelector)) |
| 1018 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 1019 | // need be implemented in the implementation. |
| 1020 | NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy")); |
| 1021 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1023 | // If a method lookup fails locally we still need to look and see if |
| 1024 | // the method was implemented by a base class or an inherited |
| 1025 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 1026 | // and otherwise would terminate in a warning. |
| 1027 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1028 | // check unimplemented instance methods. |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1029 | if (!NSIDecl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1031 | E = PDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1032 | ObjCMethodDecl *method = *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1034 | !method->isSynthesized() && !InsMap.count(method->getSelector()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | (!Super || |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1036 | !Super->lookupInstanceMethod(method->getSelector()))) { |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1037 | // Ugly, but necessary. Method declared in protcol might have |
| 1038 | // have been synthesized due to a property declared in the class which |
| 1039 | // uses the protocol. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | ObjCMethodDecl *MethodInClass = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1041 | IDecl->lookupInstanceMethod(method->getSelector()); |
Fariborz Jahanian | cd18762 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1042 | if (!MethodInClass || !MethodInClass->isSynthesized()) |
| 1043 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
| 1044 | } |
| 1045 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1046 | // check unimplemented class methods |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1048 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1049 | I != E; ++I) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1050 | ObjCMethodDecl *method = *I; |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1051 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 1052 | !ClsMap.count(method->getSelector()) && |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1053 | (!Super || !Super->lookupClassMethod(method->getSelector()))) |
Steve Naroff | 3c2eb66 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 1054 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Steve Naroff | 58dbdeb | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1055 | } |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 1056 | // Check on this protocols's referenced protocols, recursively. |
| 1057 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1058 | E = PDecl->protocol_end(); PI != E; ++PI) |
Daniel Dunbar | 7ad1b1f | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1059 | CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1062 | /// MatchAllMethodDeclarations - Check methods declaraed in interface or |
| 1063 | /// or protocol against those declared in their implementations. |
| 1064 | /// |
| 1065 | void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap, |
| 1066 | const llvm::DenseSet<Selector> &ClsMap, |
| 1067 | llvm::DenseSet<Selector> &InsMapSeen, |
| 1068 | llvm::DenseSet<Selector> &ClsMapSeen, |
| 1069 | ObjCImplDecl* IMPDecl, |
| 1070 | ObjCContainerDecl* CDecl, |
| 1071 | bool &IncompleteImpl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | bool ImmediateClass) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1073 | // Check and see if instance methods in class interface have been |
| 1074 | // implemented in the implementation class. If so, their types match. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1075 | for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(), |
| 1076 | E = CDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1077 | if (InsMapSeen.count((*I)->getSelector())) |
| 1078 | continue; |
| 1079 | InsMapSeen.insert((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | if (!(*I)->isSynthesized() && |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1081 | !InsMap.count((*I)->getSelector())) { |
| 1082 | if (ImmediateClass) |
| 1083 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
| 1084 | continue; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1085 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1086 | ObjCMethodDecl *ImpMethodDecl = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1087 | IMPDecl->getInstanceMethod((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | ObjCMethodDecl *IntfMethodDecl = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1089 | CDecl->getInstanceMethod((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | assert(IntfMethodDecl && |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1091 | "IntfMethodDecl is null in ImplMethodsVsClassMethods"); |
| 1092 | // ImpMethodDecl may be null as in a @dynamic property. |
| 1093 | if (ImpMethodDecl) |
| 1094 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 1095 | } |
| 1096 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1098 | // Check and see if class methods in class interface have been |
| 1099 | // implemented in the implementation class. If so, their types match. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | for (ObjCInterfaceDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1101 | I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1102 | if (ClsMapSeen.count((*I)->getSelector())) |
| 1103 | continue; |
| 1104 | ClsMapSeen.insert((*I)->getSelector()); |
| 1105 | if (!ClsMap.count((*I)->getSelector())) { |
| 1106 | if (ImmediateClass) |
| 1107 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1108 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1109 | ObjCMethodDecl *ImpMethodDecl = |
| 1110 | IMPDecl->getClassMethod((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | ObjCMethodDecl *IntfMethodDecl = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1112 | CDecl->getClassMethod((*I)->getSelector()); |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1113 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 1114 | } |
| 1115 | } |
| 1116 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
| 1117 | // Check for any implementation of a methods declared in protocol. |
| 1118 | for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), |
| 1119 | E = I->protocol_end(); PI != E; ++PI) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1121 | IMPDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1122 | (*PI), IncompleteImpl, false); |
| 1123 | if (I->getSuperClass()) |
| 1124 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | IMPDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1126 | I->getSuperClass(), IncompleteImpl, false); |
| 1127 | } |
| 1128 | } |
| 1129 | |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1130 | /// CollectImmediateProperties - This routine collects all properties in |
| 1131 | /// the class and its conforming protocols; but not those it its super class. |
| 1132 | void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, |
| 1133 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { |
| 1134 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1135 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1136 | E = IDecl->prop_end(); P != E; ++P) { |
| 1137 | ObjCPropertyDecl *Prop = (*P); |
| 1138 | PropMap[Prop->getIdentifier()] = Prop; |
| 1139 | } |
| 1140 | // scan through class's protocols. |
| 1141 | for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(), |
| 1142 | E = IDecl->protocol_end(); PI != E; ++PI) |
| 1143 | CollectImmediateProperties((*PI), PropMap); |
| 1144 | } |
Fariborz Jahanian | 3ad230e | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1145 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1146 | if (!CATDecl->IsClassExtension()) |
| 1147 | for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), |
| 1148 | E = CATDecl->prop_end(); P != E; ++P) { |
| 1149 | ObjCPropertyDecl *Prop = (*P); |
| 1150 | PropMap[Prop->getIdentifier()] = Prop; |
| 1151 | } |
Fariborz Jahanian | 3ad230e | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1152 | // scan through class's protocols. |
| 1153 | for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(), |
| 1154 | E = CATDecl->protocol_end(); PI != E; ++PI) |
| 1155 | CollectImmediateProperties((*PI), PropMap); |
| 1156 | } |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1157 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1158 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1159 | E = PDecl->prop_end(); P != E; ++P) { |
| 1160 | ObjCPropertyDecl *Prop = (*P); |
| 1161 | ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; |
| 1162 | if (!PropEntry) |
| 1163 | PropEntry = Prop; |
| 1164 | } |
| 1165 | // scan through protocol's protocols. |
| 1166 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1167 | E = PDecl->protocol_end(); PI != E; ++PI) |
| 1168 | CollectImmediateProperties((*PI), PropMap); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1172 | /// LookupPropertyDecl - Looks up a property in the current class and all |
| 1173 | /// its protocols. |
| 1174 | ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl, |
| 1175 | IdentifierInfo *II) { |
| 1176 | if (const ObjCInterfaceDecl *IDecl = |
| 1177 | dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1178 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1179 | E = IDecl->prop_end(); P != E; ++P) { |
| 1180 | ObjCPropertyDecl *Prop = (*P); |
| 1181 | if (Prop->getIdentifier() == II) |
| 1182 | return Prop; |
| 1183 | } |
| 1184 | // scan through class's protocols. |
| 1185 | for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(), |
| 1186 | E = IDecl->protocol_end(); PI != E; ++PI) { |
| 1187 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 1188 | if (Prop) |
| 1189 | return Prop; |
| 1190 | } |
| 1191 | } |
| 1192 | else if (const ObjCProtocolDecl *PDecl = |
| 1193 | dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1194 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1195 | E = PDecl->prop_end(); P != E; ++P) { |
| 1196 | ObjCPropertyDecl *Prop = (*P); |
| 1197 | if (Prop->getIdentifier() == II) |
| 1198 | return Prop; |
| 1199 | } |
| 1200 | // scan through protocol's protocols. |
| 1201 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1202 | E = PDecl->protocol_end(); PI != E; ++PI) { |
| 1203 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 1204 | if (Prop) |
| 1205 | return Prop; |
| 1206 | } |
| 1207 | } |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1212 | void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl, |
| 1213 | ObjCContainerDecl *CDecl, |
| 1214 | const llvm::DenseSet<Selector>& InsMap) { |
| 1215 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; |
| 1216 | CollectImmediateProperties(CDecl, PropMap); |
Fariborz Jahanian | a84f2e4 | 2010-01-20 17:27:59 +0000 | [diff] [blame] | 1217 | if (PropMap.empty()) |
| 1218 | return; |
| 1219 | |
| 1220 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
| 1221 | for (ObjCImplDecl::propimpl_iterator |
| 1222 | I = IMPDecl->propimpl_begin(), |
| 1223 | EI = IMPDecl->propimpl_end(); I != EI; ++I) |
| 1224 | PropImplMap.insert((*I)->getPropertyDecl()); |
| 1225 | |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1226 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator |
| 1227 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1228 | ObjCPropertyDecl *Prop = P->second; |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1229 | // Is there a matching propery synthesize/dynamic? |
Fariborz Jahanian | a84f2e4 | 2010-01-20 17:27:59 +0000 | [diff] [blame] | 1230 | if (Prop->isInvalidDecl() || |
| 1231 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
| 1232 | PropImplMap.count(Prop)) |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1233 | continue; |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1234 | if (LangOpts.ObjCNonFragileABI2) { |
| 1235 | ActOnPropertyImplDecl(IMPDecl->getLocation(), |
| 1236 | SourceLocation(), |
| 1237 | true, DeclPtrTy::make(IMPDecl), |
| 1238 | Prop->getIdentifier(), |
| 1239 | Prop->getIdentifier()); |
| 1240 | continue; |
| 1241 | } |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1242 | if (!InsMap.count(Prop->getGetterName())) { |
| 1243 | Diag(Prop->getLocation(), |
Fariborz Jahanian | 3ad230e | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1244 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1245 | diag::warn_setter_getter_impl_required_in_category : |
| 1246 | diag::warn_setter_getter_impl_required) |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1247 | << Prop->getDeclName() << Prop->getGetterName(); |
| 1248 | Diag(IMPDecl->getLocation(), |
| 1249 | diag::note_property_impl_required); |
| 1250 | } |
| 1251 | |
| 1252 | if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { |
| 1253 | Diag(Prop->getLocation(), |
Fariborz Jahanian | 3ad230e | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1254 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1255 | diag::warn_setter_getter_impl_required_in_category : |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1256 | diag::warn_setter_getter_impl_required) |
| 1257 | << Prop->getDeclName() << Prop->getSetterName(); |
| 1258 | Diag(IMPDecl->getLocation(), |
| 1259 | diag::note_property_impl_required); |
| 1260 | } |
| 1261 | } |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1264 | void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl, |
| 1265 | ObjCContainerDecl* CDecl, |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1266 | bool IncompleteImpl) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1267 | llvm::DenseSet<Selector> InsMap; |
| 1268 | // Check and see if instance methods in class interface have been |
| 1269 | // implemented in the implementation class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1271 | I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I) |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1272 | InsMap.insert((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 1274 | // Check and see if properties declared in the interface have either 1) |
| 1275 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 1276 | // of the property in the @implementation. |
| 1277 | if (isa<ObjCInterfaceDecl>(CDecl)) |
Fariborz Jahanian | 3ac1eda | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1278 | DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap); |
| 1279 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1280 | llvm::DenseSet<Selector> ClsMap; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1282 | I = IMPDecl->classmeth_begin(), |
| 1283 | E = IMPDecl->classmeth_end(); I != E; ++I) |
Chris Lattner | 4c52509 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1284 | ClsMap.insert((*I)->getSelector()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1286 | // Check for type conflict of methods declared in a class/protocol and |
| 1287 | // its implementation; if any. |
| 1288 | llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1290 | IMPDecl, CDecl, |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1291 | IncompleteImpl, true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1292 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1293 | // Check the protocol list for unimplemented methods in the @implementation |
| 1294 | // class. |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1295 | // Check and see if class methods in class interface have been |
| 1296 | // implemented in the implementation class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1298 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | b33f3ad | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1299 | for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1300 | E = I->protocol_end(); PI != E; ++PI) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1302 | InsMap, ClsMap, I); |
| 1303 | // Check class extensions (unnamed categories) |
| 1304 | for (ObjCCategoryDecl *Categories = I->getCategoryList(); |
| 1305 | Categories; Categories = Categories->getNextClassCategory()) { |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1306 | if (Categories->IsClassExtension()) { |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1307 | ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl); |
| 1308 | break; |
| 1309 | } |
Fariborz Jahanian | 8daab97 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 1310 | } |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1311 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1312 | // For extended class, unimplemented methods in its protocols will |
| 1313 | // be reported in the primary class. |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1314 | if (!C->IsClassExtension()) { |
Fariborz Jahanian | b106fc6 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1315 | for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(), |
| 1316 | E = C->protocol_end(); PI != E; ++PI) |
| 1317 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
| 1318 | InsMap, ClsMap, C->getClassInterface()); |
Fariborz Jahanian | 3ad230e | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1319 | // Report unimplemented properties in the category as well. |
| 1320 | // When reporting on missing setter/getters, do not report when |
| 1321 | // setter/getter is implemented in category's primary class |
| 1322 | // implementation. |
| 1323 | if (ObjCInterfaceDecl *ID = C->getClassInterface()) |
| 1324 | if (ObjCImplDecl *IMP = ID->getImplementation()) { |
| 1325 | for (ObjCImplementationDecl::instmeth_iterator |
| 1326 | I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I) |
| 1327 | InsMap.insert((*I)->getSelector()); |
| 1328 | } |
| 1329 | DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap); |
| 1330 | } |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1331 | } else |
| 1332 | assert(false && "invalid ObjCContainerDecl type."); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1335 | void |
| 1336 | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, |
| 1337 | ObjCContainerDecl* IDecl) { |
| 1338 | // Rules apply in non-GC mode only |
| 1339 | if (getLangOptions().getGCMode() != LangOptions::NonGC) |
| 1340 | return; |
| 1341 | for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), |
| 1342 | E = IDecl->prop_end(); |
| 1343 | I != E; ++I) { |
| 1344 | ObjCPropertyDecl *Property = (*I); |
| 1345 | unsigned Attributes = Property->getPropertyAttributes(); |
| 1346 | // We only care about readwrite atomic property. |
| 1347 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || |
| 1348 | !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 1349 | continue; |
| 1350 | if (const ObjCPropertyImplDecl *PIDecl |
| 1351 | = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { |
| 1352 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 1353 | continue; |
| 1354 | ObjCMethodDecl *GetterMethod = |
| 1355 | IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1356 | ObjCMethodDecl *SetterMethod = |
| 1357 | IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1358 | if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { |
| 1359 | SourceLocation MethodLoc = |
| 1360 | (GetterMethod ? GetterMethod->getLocation() |
| 1361 | : SetterMethod->getLocation()); |
| 1362 | Diag(MethodLoc, diag::warn_atomic_property_rule) |
| 1363 | << Property->getIdentifier(); |
| 1364 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | /// ActOnForwardClassDeclaration - |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1371 | Action::DeclPtrTy |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1372 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | bdbde4d | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1373 | IdentifierInfo **IdentList, |
Ted Kremenek | c09cba6 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 1374 | SourceLocation *IdentLocs, |
Chris Lattner | bdbde4d | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1375 | unsigned NumElts) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1376 | llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1378 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1379 | // Check for another declaration kind with the same name. |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1380 | NamedDecl *PrevDecl |
| 1381 | = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName); |
Douglas Gregor | f57172b | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 1382 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1383 | // Maybe we will complain about the shadowed template parameter. |
| 1384 | DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl); |
| 1385 | // Just pretend that we didn't see the previous declaration. |
| 1386 | PrevDecl = 0; |
| 1387 | } |
| 1388 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1389 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | c733388 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 1390 | // GCC apparently allows the following idiom: |
| 1391 | // |
| 1392 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 1393 | // @class XCElementToggler; |
| 1394 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | // FIXME: Make an extension? |
Steve Naroff | c733388 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 1396 | TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); |
| 1397 | if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1398 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1399 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1400 | } else if (TDD) { |
| 1401 | // a forward class declaration matching a typedef name of a class refers |
| 1402 | // to the underlying class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | if (ObjCInterfaceType * OI = |
Fariborz Jahanian | cae27c5 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 1404 | dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType())) |
| 1405 | PrevDecl = OI->getDecl(); |
| 1406 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1409 | if (!IDecl) { // Not already seen? Make a forward decl. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1411 | IdentList[i], IdentLocs[i], true); |
Ted Kremenek | c32b1d8 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 1412 | |
| 1413 | // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to |
| 1414 | // the current DeclContext. This prevents clients that walk DeclContext |
| 1415 | // from seeing the imaginary ObjCInterfaceDecl until it is actually |
| 1416 | // declared later (if at all). We also take care to explicitly make |
| 1417 | // sure this declaration is visible for name lookup. |
| 1418 | PushOnScopeChains(IDecl, TUScope, false); |
| 1419 | CurContext->makeDeclVisibleInContext(IDecl, true); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | Interfaces.push_back(IDecl); |
| 1423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1425 | assert(Interfaces.size() == NumElts); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1426 | ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc, |
Ted Kremenek | 321c22f | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1427 | Interfaces.data(), IdentLocs, |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1428 | Interfaces.size()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1429 | CurContext->addDecl(CDecl); |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1430 | CheckObjCDeclScope(CDecl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1431 | return DeclPtrTy::make(CDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | |
| 1435 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 1436 | /// returns true, or false, accordingly. |
| 1437 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1439 | const ObjCMethodDecl *PrevMethod, |
| 1440 | bool matchBasedOnSizeAndAlignment) { |
| 1441 | QualType T1 = Context.getCanonicalType(Method->getResultType()); |
| 1442 | QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1444 | if (T1 != T2) { |
| 1445 | // The result types are different. |
| 1446 | if (!matchBasedOnSizeAndAlignment) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1447 | return false; |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1448 | // Incomplete types don't have a size and alignment. |
| 1449 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1450 | return false; |
| 1451 | // Check is based on size and alignment. |
| 1452 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1453 | return false; |
| 1454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1455 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1456 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 1457 | E = Method->param_end(); |
| 1458 | ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1460 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1461 | assert(PrevI != PrevMethod->param_end() && "Param mismatch"); |
| 1462 | T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1463 | T2 = Context.getCanonicalType((*PrevI)->getType()); |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1464 | if (T1 != T2) { |
| 1465 | // The result types are different. |
| 1466 | if (!matchBasedOnSizeAndAlignment) |
| 1467 | return false; |
| 1468 | // Incomplete types don't have a size and alignment. |
| 1469 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1470 | return false; |
| 1471 | // Check is based on size and alignment. |
| 1472 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1473 | return false; |
| 1474 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1475 | } |
| 1476 | return true; |
| 1477 | } |
| 1478 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1479 | /// \brief Read the contents of the instance and factory method pools |
| 1480 | /// for a given selector from external storage. |
| 1481 | /// |
| 1482 | /// This routine should only be called once, when neither the instance |
| 1483 | /// nor the factory method pool has an entry for this selector. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel, |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1485 | bool isInstance) { |
| 1486 | assert(ExternalSource && "We need an external AST source"); |
| 1487 | assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() && |
| 1488 | "Selector data already loaded into the instance method pool"); |
| 1489 | assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() && |
| 1490 | "Selector data already loaded into the factory method pool"); |
| 1491 | |
| 1492 | // Read the method list from the external source. |
| 1493 | std::pair<ObjCMethodList, ObjCMethodList> Methods |
| 1494 | = ExternalSource->ReadMethodPool(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1496 | if (isInstance) { |
| 1497 | if (Methods.second.Method) |
| 1498 | FactoryMethodPool[Sel] = Methods.second; |
| 1499 | return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | } |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1501 | |
| 1502 | if (Methods.first.Method) |
| 1503 | InstanceMethodPool[Sel] = Methods.first; |
| 1504 | |
| 1505 | return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first; |
| 1506 | } |
| 1507 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1508 | void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1509 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos |
| 1510 | = InstanceMethodPool.find(Method->getSelector()); |
| 1511 | if (Pos == InstanceMethodPool.end()) { |
| 1512 | if (ExternalSource && !FactoryMethodPool.count(Method->getSelector())) |
| 1513 | Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true); |
| 1514 | else |
| 1515 | Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(), |
| 1516 | ObjCMethodList())).first; |
| 1517 | } |
| 1518 | |
| 1519 | ObjCMethodList &Entry = Pos->second; |
Chris Lattner | b25df35 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1520 | if (Entry.Method == 0) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1521 | // Haven't seen a method with this selector name yet - add it. |
Chris Lattner | b25df35 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1522 | Entry.Method = Method; |
| 1523 | Entry.Next = 0; |
| 1524 | return; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1525 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | |
Chris Lattner | b25df35 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1527 | // We've seen a method with this name, see if we have already seen this type |
| 1528 | // signature. |
| 1529 | for (ObjCMethodList *List = &Entry; List; List = List->Next) |
| 1530 | if (MatchTwoMethodDeclarations(Method, List->Method)) |
| 1531 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Chris Lattner | b25df35 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1533 | // We have a new signature for an existing method - add it. |
| 1534 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | 298ed87 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 1535 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
| 1536 | Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Steve Naroff | 6f5f41c | 2008-10-21 10:50:19 +0000 | [diff] [blame] | 1539 | // FIXME: Finish implementing -Wno-strict-selector-match. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1540 | ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel, |
Fariborz Jahanian | 835ed7f | 2009-08-22 21:13:55 +0000 | [diff] [blame] | 1541 | SourceRange R, |
| 1542 | bool warn) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1543 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos |
| 1544 | = InstanceMethodPool.find(Sel); |
Douglas Gregor | 27a4566 | 2009-04-24 22:23:41 +0000 | [diff] [blame] | 1545 | if (Pos == InstanceMethodPool.end()) { |
| 1546 | if (ExternalSource && !FactoryMethodPool.count(Sel)) |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1547 | Pos = ReadMethodPool(Sel, /*isInstance=*/true); |
| 1548 | else |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | ObjCMethodList &MethList = Pos->second; |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1553 | bool issueWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1555 | if (MethList.Method && MethList.Next) { |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1556 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
| 1557 | // This checks if the methods differ by size & alignment. |
| 1558 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) |
Fariborz Jahanian | 835ed7f | 2009-08-22 21:13:55 +0000 | [diff] [blame] | 1559 | issueWarning = warn; |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1560 | } |
| 1561 | if (issueWarning && (MethList.Method && MethList.Next)) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1562 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 1563 | Diag(MethList.Method->getLocStart(), diag::note_using) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1564 | << MethList.Method->getSourceRange(); |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1565 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 1566 | Diag(Next->Method->getLocStart(), diag::note_also_found) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1567 | << Next->Method->getSourceRange(); |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1568 | } |
| 1569 | return MethList.Method; |
| 1570 | } |
| 1571 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1572 | void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1573 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos |
| 1574 | = FactoryMethodPool.find(Method->getSelector()); |
| 1575 | if (Pos == FactoryMethodPool.end()) { |
| 1576 | if (ExternalSource && !InstanceMethodPool.count(Method->getSelector())) |
| 1577 | Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false); |
| 1578 | else |
| 1579 | Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(), |
| 1580 | ObjCMethodList())).first; |
| 1581 | } |
| 1582 | |
| 1583 | ObjCMethodList &FirstMethod = Pos->second; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1584 | if (!FirstMethod.Method) { |
| 1585 | // Haven't seen a method with this selector name yet - add it. |
| 1586 | FirstMethod.Method = Method; |
| 1587 | FirstMethod.Next = 0; |
| 1588 | } else { |
| 1589 | // We've seen a method with this name, now check the type signature(s). |
| 1590 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | |
| 1592 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1593 | Next = Next->Next) |
| 1594 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1596 | if (!match) { |
| 1597 | // We have a new signature for an existing method - add it. |
| 1598 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | 298ed87 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 1599 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
| 1600 | ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1601 | FirstMethod.Next = OMI; |
| 1602 | } |
| 1603 | } |
| 1604 | } |
| 1605 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel, |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1607 | SourceRange R) { |
| 1608 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos |
| 1609 | = FactoryMethodPool.find(Sel); |
| 1610 | if (Pos == FactoryMethodPool.end()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | if (ExternalSource && !InstanceMethodPool.count(Sel)) |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1612 | Pos = ReadMethodPool(Sel, /*isInstance=*/false); |
| 1613 | else |
| 1614 | return 0; |
| 1615 | } |
| 1616 | |
| 1617 | ObjCMethodList &MethList = Pos->second; |
| 1618 | bool issueWarning = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1619 | |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1620 | if (MethList.Method && MethList.Next) { |
| 1621 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
| 1622 | // This checks if the methods differ by size & alignment. |
| 1623 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) |
| 1624 | issueWarning = true; |
| 1625 | } |
| 1626 | if (issueWarning && (MethList.Method && MethList.Next)) { |
| 1627 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 1628 | Diag(MethList.Method->getLocStart(), diag::note_using) |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1629 | << MethList.Method->getSourceRange(); |
| 1630 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
John McCall | 41ce66f | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 1631 | Diag(Next->Method->getLocStart(), diag::note_also_found) |
Douglas Gregor | f0aaf7a | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1632 | << Next->Method->getSourceRange(); |
| 1633 | } |
| 1634 | return MethList.Method; |
| 1635 | } |
| 1636 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1638 | /// have the property type and issue diagnostics if they don't. |
| 1639 | /// Also synthesize a getter/setter method if none exist (and update the |
| 1640 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized |
| 1641 | /// methods is the "right" thing to do. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1642 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1643 | ObjCContainerDecl *CD) { |
| 1644 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
| 1646 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1647 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1648 | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 1649 | property->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
Fariborz Jahanian | f3cd3fd | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1651 | if (SetterMethod) { |
Fariborz Jahanian | 02deae8 | 2010-01-06 00:18:12 +0000 | [diff] [blame] | 1652 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1653 | property->getPropertyAttributes(); |
| 1654 | if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && |
| 1655 | Context.getCanonicalType(SetterMethod->getResultType()) != |
| 1656 | Context.VoidTy) |
Fariborz Jahanian | f3cd3fd | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1657 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1658 | if (SetterMethod->param_size() != 1 || |
| 1659 | ((*SetterMethod->param_begin())->getType() != property->getType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | Diag(property->getLocation(), |
| 1661 | diag::warn_accessor_property_type_mismatch) |
Fariborz Jahanian | f3cd3fd | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1662 | << property->getDeclName() |
Ted Kremenek | 8af2c16 | 2009-03-14 00:20:08 +0000 | [diff] [blame] | 1663 | << SetterMethod->getSelector(); |
Fariborz Jahanian | 196d0ed | 2008-12-06 21:48:16 +0000 | [diff] [blame] | 1664 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 1665 | } |
Fariborz Jahanian | f3cd3fd | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1666 | } |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1667 | |
| 1668 | // Synthesize getter/setter methods if none exist. |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1669 | // Find the default getter and if one not found, add one. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1670 | // FIXME: The synthesized property we set here is misleading. We almost always |
| 1671 | // synthesize these methods unless the user explicitly provided prototypes |
| 1672 | // (which is odd, but allowed). Sema should be typechecking that the |
| 1673 | // declarations jive in that situation (which it is not currently). |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1674 | if (!GetterMethod) { |
| 1675 | // No instance method of same name as property getter name was found. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1676 | // Declare a getter method and add it to the list of methods |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1677 | // for this class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), |
| 1679 | property->getLocation(), property->getGetterName(), |
| 1680 | property->getType(), CD, true, false, true, |
| 1681 | (property->getPropertyImplementation() == |
| 1682 | ObjCPropertyDecl::Optional) ? |
| 1683 | ObjCMethodDecl::Optional : |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1684 | ObjCMethodDecl::Required); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1685 | CD->addDecl(GetterMethod); |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1686 | } else |
| 1687 | // A user declared getter will be synthesize when @synthesize of |
| 1688 | // the property with the same name is seen in the @implementation |
Steve Naroff | 53c9d8a | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 1689 | GetterMethod->setSynthesized(true); |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1690 | property->setGetterMethodDecl(GetterMethod); |
| 1691 | |
| 1692 | // Skip setter if property is read-only. |
| 1693 | if (!property->isReadOnly()) { |
| 1694 | // Find the default setter and if one not found, add one. |
| 1695 | if (!SetterMethod) { |
| 1696 | // No instance method of same name as property setter name was found. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | // Declare a setter method and add it to the list of methods |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1698 | // for this class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), |
| 1700 | property->getLocation(), |
| 1701 | property->getSetterName(), |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1702 | Context.VoidTy, CD, true, false, true, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | (property->getPropertyImplementation() == |
| 1704 | ObjCPropertyDecl::Optional) ? |
| 1705 | ObjCMethodDecl::Optional : |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1706 | ObjCMethodDecl::Required); |
| 1707 | // Invent the arguments for the setter. We don't bother making a |
| 1708 | // nice name for the argument. |
| 1709 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | property->getLocation(), |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1711 | property->getIdentifier(), |
| 1712 | property->getType(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1713 | /*TInfo=*/0, |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1714 | VarDecl::None, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1715 | 0); |
Steve Naroff | 53c9d8a | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 1716 | SetterMethod->setMethodParams(Context, &Argument, 1); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1717 | CD->addDecl(SetterMethod); |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1718 | } else |
| 1719 | // A user declared setter will be synthesize when @synthesize of |
| 1720 | // the property with the same name is seen in the @implementation |
Steve Naroff | 53c9d8a | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 1721 | SetterMethod->setSynthesized(true); |
Steve Naroff | 92f863b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1722 | property->setSetterMethodDecl(SetterMethod); |
| 1723 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | // Add any synthesized methods to the global pool. This allows us to |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1725 | // handle the following, which is supported by GCC (and part of the design). |
| 1726 | // |
| 1727 | // @interface Foo |
| 1728 | // @property double bar; |
| 1729 | // @end |
| 1730 | // |
| 1731 | // void thisIsUnfortunate() { |
| 1732 | // id foo; |
| 1733 | // double bar = [foo bar]; |
| 1734 | // } |
| 1735 | // |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1736 | if (GetterMethod) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1737 | AddInstanceMethodToGlobalPool(GetterMethod); |
Douglas Gregor | 6037fcb | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1738 | if (SetterMethod) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | AddInstanceMethodToGlobalPool(SetterMethod); |
Fariborz Jahanian | f3cd3fd | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1742 | /// CompareMethodParamsInBaseAndSuper - This routine compares methods with |
| 1743 | /// identical selector names in current and its super classes and issues |
| 1744 | /// a warning if any of their argument types are incompatible. |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1745 | void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl, |
| 1746 | ObjCMethodDecl *Method, |
| 1747 | bool IsInstance) { |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1748 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 1749 | if (ID == 0) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1751 | while (ObjCInterfaceDecl *SD = ID->getSuperClass()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | ObjCMethodDecl *SuperMethodDecl = |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1753 | SD->lookupMethod(Method->getSelector(), IsInstance); |
| 1754 | if (SuperMethodDecl == 0) { |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1755 | ID = SD; |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1756 | continue; |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1757 | } |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1758 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 1759 | E = Method->param_end(); |
| 1760 | ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin(); |
| 1761 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1762 | // Number of parameters are the same and is guaranteed by selector match. |
| 1763 | assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch"); |
| 1764 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1765 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 1766 | // If type of arguement of method in this class does not match its |
| 1767 | // respective argument type in the super class method, issue warning; |
| 1768 | if (!Context.typesAreCompatible(T1, T2)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1770 | << T1 << T2; |
| 1771 | Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration); |
| 1772 | return; |
| 1773 | } |
| 1774 | } |
| 1775 | ID = SD; |
| 1776 | } |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1779 | // Note: For class/category implemenations, allMethods/allProperties is |
| 1780 | // always null. |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1781 | void Sema::ActOnAtEnd(SourceRange AtEnd, |
| 1782 | DeclPtrTy classDecl, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1783 | DeclPtrTy *allMethods, unsigned allNum, |
| 1784 | DeclPtrTy *allProperties, unsigned pNum, |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1785 | DeclGroupPtrTy *allTUVars, unsigned tuvNum) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1786 | Decl *ClassDecl = classDecl.getAs<Decl>(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1787 | |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1788 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 1789 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1790 | // should be true. |
| 1791 | if (!ClassDecl) |
| 1792 | return; |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1793 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | bool isInterfaceDeclKind = |
Chris Lattner | f8d17a5 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1795 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 1796 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1797 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1798 | |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1799 | if (!isInterfaceDeclKind && AtEnd.isInvalid()) { |
| 1800 | // FIXME: This is wrong. We shouldn't be pretending that there is |
| 1801 | // an '@end' in the declaration. |
| 1802 | SourceLocation L = ClassDecl->getLocation(); |
| 1803 | AtEnd.setBegin(L); |
| 1804 | AtEnd.setEnd(L); |
| 1805 | Diag(L, diag::warn_missing_atend); |
Fariborz Jahanian | 63e963c | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1808 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1809 | |
| 1810 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 1811 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 1812 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 1813 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1814 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1815 | ObjCMethodDecl *Method = |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1816 | cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1817 | |
| 1818 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1819 | if (Method->isInstanceMethod()) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1820 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1821 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1823 | : false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1824 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1825 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1826 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1827 | << Method->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1828 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1829 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1830 | DC->addDecl(Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1831 | InsMap[Method->getSelector()] = Method; |
| 1832 | /// The following allows us to typecheck messages to "id". |
| 1833 | AddInstanceMethodToGlobalPool(Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | // verify that the instance method conforms to the same definition of |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1835 | // parent methods if it shadows one. |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1836 | CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1837 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1838 | } else { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1839 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1840 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1842 | : false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1844 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1845 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1846 | << Method->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1847 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1848 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1849 | DC->addDecl(Method); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1850 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | a56f616 | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1851 | /// The following allows us to typecheck messages to "Class". |
| 1852 | AddFactoryMethodToGlobalPool(Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | // verify that the class method conforms to the same definition of |
Fariborz Jahanian | e198f5d | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1854 | // parent methods if it shadows one. |
Fariborz Jahanian | dbdec8b | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1855 | CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1859 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | // Compares properties declared in this class to those of its |
Fariborz Jahanian | 02edb98 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 1861 | // super class. |
Fariborz Jahanian | aebf0cb | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 1862 | ComparePropertiesInBaseAndSuper(I); |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 1863 | CompareProperties(I, DeclPtrTy::make(I)); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1864 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 77e14bd | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1865 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | // By the same token, they are also used to add new properties. No |
Fariborz Jahanian | 77e14bd | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1867 | // need to compare the added property to those in the class. |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1868 | |
Fariborz Jahanian | 107089f | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 1869 | // Compare protocol properties with those in category |
| 1870 | CompareProperties(C, DeclPtrTy::make(C)); |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1871 | if (C->IsClassExtension()) |
Fariborz Jahanian | b7f95f5 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1872 | DiagnoseClassExtensionDupMethods(C, C->getClassInterface()); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1873 | } |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1874 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1875 | if (CDecl->getIdentifier()) |
| 1876 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 1877 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 1878 | // and adds them to the DeclContext and global method pools. |
| 1879 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 1880 | E = CDecl->prop_end(); |
| 1881 | I != E; ++I) |
| 1882 | ProcessPropertyDecl(*I, CDecl); |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1883 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1884 | } |
| 1885 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1886 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1887 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1888 | ImplMethodsVsClassMethods(IC, IDecl); |
Fariborz Jahanian | 7ca8b06 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1889 | AtomicPropertySetterGetterRules(IC, IDecl); |
| 1890 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1891 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | 09c4719 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1892 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1893 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1895 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | b20ef3e | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1896 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 97a5887 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1897 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1898 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1899 | Categories; Categories = Categories->getNextClassCategory()) { |
| 1900 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
Chris Lattner | cddc888 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1901 | ImplMethodsVsClassMethods(CatImplClass, Categories); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1902 | break; |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1907 | if (isInterfaceDeclKind) { |
| 1908 | // Reject invalid vardecls. |
| 1909 | for (unsigned i = 0; i != tuvNum; i++) { |
| 1910 | DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); |
| 1911 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 1912 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 5466c7b | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 1913 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 8745416 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 1914 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | b31cb7f | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1915 | } |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1916 | } |
Fariborz Jahanian | 38e24c7 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 1917 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | |
| 1921 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 1922 | /// objective-c's type qualifier from the parser version of the same info. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | static Decl::ObjCDeclQualifier |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1924 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 1925 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 1926 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 1927 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 1928 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 1929 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 1930 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 1931 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 1932 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 1933 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 1934 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 1935 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 1936 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 1937 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1938 | |
| 1939 | return ret; |
| 1940 | } |
| 1941 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1942 | Sema::DeclPtrTy Sema::ActOnMethodDeclaration( |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1943 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1944 | tok::TokenKind MethodType, DeclPtrTy classDecl, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1945 | ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1946 | Selector Sel, |
| 1947 | // optional arguments. The number of types/arguments is obtained |
| 1948 | // from the Sel.getNumArgs(). |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1949 | ObjCArgInfo *ArgInfo, |
Fariborz Jahanian | 439c658 | 2009-01-09 00:38:19 +0000 | [diff] [blame] | 1950 | llvm::SmallVectorImpl<Declarator> &Cdecls, |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1951 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 1952 | bool isVariadic) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1953 | Decl *ClassDecl = classDecl.getAs<Decl>(); |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1954 | |
| 1955 | // Make sure we can establish a context for the method. |
| 1956 | if (!ClassDecl) { |
| 1957 | Diag(MethodLoc, diag::error_missing_method_context); |
Fariborz Jahanian | 32844b3 | 2009-08-28 17:52:37 +0000 | [diff] [blame] | 1958 | FunctionLabelMap.clear(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1959 | return DeclPtrTy(); |
Steve Naroff | da323ad | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1960 | } |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1961 | QualType resultDeclType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | |
Steve Naroff | ccef371 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1963 | if (ReturnType) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1964 | resultDeclType = GetTypeFromParser(ReturnType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | |
Steve Naroff | ccef371 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1966 | // Methods cannot return interface types. All ObjC objects are |
| 1967 | // passed by reference. |
| 1968 | if (resultDeclType->isObjCInterfaceType()) { |
Chris Lattner | 2dd979f | 2009-04-11 19:08:56 +0000 | [diff] [blame] | 1969 | Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value) |
| 1970 | << 0 << resultDeclType; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1971 | return DeclPtrTy(); |
Steve Naroff | ccef371 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } else // get the type for "id". |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1974 | resultDeclType = Context.getObjCIdType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | |
| 1976 | ObjCMethodDecl* ObjCMethod = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1977 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | cast<DeclContext>(ClassDecl), |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1979 | MethodType == tok::minus, isVariadic, |
Fariborz Jahanian | 4607034 | 2008-05-07 20:53:44 +0000 | [diff] [blame] | 1980 | false, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | MethodDeclKind == tok::objc_optional ? |
| 1982 | ObjCMethodDecl::Optional : |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1983 | ObjCMethodDecl::Required); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1985 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Chris Lattner | 7db638d | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 1987 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1988 | QualType ArgType; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1989 | TypeSourceInfo *DI; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1991 | if (ArgInfo[i].Type == 0) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1992 | ArgType = Context.getObjCIdType(); |
| 1993 | DI = 0; |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1994 | } else { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1995 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Steve Naroff | 6082c62 | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1996 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
Chris Lattner | f97e8fa | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1997 | ArgType = adjustParameterType(ArgType); |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1998 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 2000 | ParmVarDecl* Param |
| 2001 | = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc, |
| 2002 | ArgInfo[i].Name, ArgType, DI, |
| 2003 | VarDecl::None, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | |
Chris Lattner | f97e8fa | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 2005 | if (ArgType->isObjCInterfaceType()) { |
| 2006 | Diag(ArgInfo[i].NameLoc, |
| 2007 | diag::err_object_cannot_be_passed_returned_by_value) |
| 2008 | << 1 << ArgType; |
| 2009 | Param->setInvalidDecl(); |
| 2010 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2012 | Param->setObjCDeclQualifier( |
Chris Lattner | e294d3f | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 2013 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | |
Chris Lattner | f97e8fa | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 2015 | // Apply the attributes to the parameter. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2016 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
Chris Lattner | 0ed844b | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 2018 | Params.push_back(Param); |
| 2019 | } |
| 2020 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2021 | ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2022 | ObjCMethod->setObjCDeclQualifier( |
| 2023 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 2024 | const ObjCMethodDecl *PrevMethod = 0; |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 2025 | |
| 2026 | if (AttrList) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2027 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2028 | |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2029 | const ObjCMethodDecl *InterfaceMD = 0; |
| 2030 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2031 | // For implementations (which can be very "coarse grain"), we add the |
| 2032 | // method now. This allows the AST to implement lookup methods that work |
| 2033 | // incrementally (without waiting until we parse the @end). It also allows |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2034 | // us to flag multiple declaration errors as they occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2035 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 6c4ae5d | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 2036 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2037 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2038 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 2039 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2040 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2041 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 2042 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2043 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2044 | InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel, |
| 2045 | MethodType == tok::minus); |
Fariborz Jahanian | 5d36ac2 | 2009-05-12 21:36:23 +0000 | [diff] [blame] | 2046 | if (AttrList) |
| 2047 | Diag(EndLoc, diag::warn_attribute_method_def); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | } else if (ObjCCategoryImplDecl *CatImpDecl = |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2049 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2050 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2051 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
| 2052 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2053 | } else { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2054 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
| 2055 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2056 | } |
Fariborz Jahanian | 5d36ac2 | 2009-05-12 21:36:23 +0000 | [diff] [blame] | 2057 | if (AttrList) |
| 2058 | Diag(EndLoc, diag::warn_attribute_method_def); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2059 | } |
| 2060 | if (PrevMethod) { |
| 2061 | // You can never have two method definitions with the same name. |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2062 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2063 | << ObjCMethod->getDeclName(); |
Chris Lattner | 5f4a682 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2064 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2066 | |
| 2067 | // If the interface declared this method, and it was deprecated there, |
| 2068 | // mark it deprecated here. |
| 2069 | if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>()) |
| 2070 | ObjCMethod->addAttr(::new (Context) DeprecatedAttr()); |
| 2071 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2072 | return DeclPtrTy::make(ObjCMethod); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2076 | SourceLocation Loc, |
| 2077 | unsigned &Attributes) { |
| 2078 | // FIXME: Improve the reported location. |
| 2079 | |
Fariborz Jahanian | 567c8df | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 2080 | // readonly and readwrite/assign/retain/copy conflict. |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2081 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
Fariborz Jahanian | 567c8df | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 2082 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 2083 | ObjCDeclSpec::DQ_PR_assign | |
| 2084 | ObjCDeclSpec::DQ_PR_copy | |
| 2085 | ObjCDeclSpec::DQ_PR_retain))) { |
| 2086 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? |
| 2087 | "readwrite" : |
| 2088 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
| 2089 | "assign" : |
| 2090 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
| 2091 | "copy" : "retain"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
| 2093 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? |
Chris Lattner | 28372fa | 2009-01-29 18:49:48 +0000 | [diff] [blame] | 2094 | diag::err_objc_property_attr_mutually_exclusive : |
| 2095 | diag::warn_objc_property_attr_mutually_exclusive) |
Fariborz Jahanian | 567c8df | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 2096 | << "readonly" << which; |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
| 2099 | // Check for copy or retain on non-object types. |
| 2100 | if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | !PropertyTy->isObjCObjectPointerType() && |
| 2102 | !PropertyTy->isBlockPointerType() && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 2103 | !Context.isObjCNSObjectType(PropertyTy)) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2104 | Diag(Loc, diag::err_objc_property_requires_object) |
| 2105 | << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2106 | Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); |
| 2107 | } |
| 2108 | |
| 2109 | // Check for more than one of { assign, copy, retain }. |
| 2110 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 2111 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2112 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2113 | << "assign" << "copy"; |
| 2114 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | } |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2116 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2117 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2118 | << "assign" << "retain"; |
| 2119 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2120 | } |
| 2121 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 2122 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2123 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2124 | << "copy" << "retain"; |
| 2125 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | // Warn if user supplied no assignment attribute, property is |
| 2130 | // readwrite, and this is an object type. |
| 2131 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | |
| 2132 | ObjCDeclSpec::DQ_PR_retain)) && |
| 2133 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 2134 | PropertyTy->isObjCObjectPointerType()) { |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2135 | // Skip this warning in gc-only mode. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | if (getLangOptions().getGCMode() != LangOptions::GCOnly) |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2137 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
| 2138 | |
| 2139 | // If non-gc code warn that this is likely inappropriate. |
| 2140 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 2141 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2143 | // FIXME: Implement warning dependent on NSCopying being |
| 2144 | // implemented. See also: |
| 2145 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 2146 | // (please trim this list while you are at it). |
| 2147 | } |
Mike Stump | 046efd9 | 2009-05-07 23:06:50 +0000 | [diff] [blame] | 2148 | |
| 2149 | if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 2150 | && getLangOptions().getGCMode() == LangOptions::GCOnly |
| 2151 | && PropertyTy->isBlockPointerType()) |
| 2152 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2156 | FieldDeclarator &FD, |
| 2157 | ObjCDeclSpec &ODS, |
| 2158 | Selector GetterSel, |
| 2159 | Selector SetterSel, |
| 2160 | DeclPtrTy ClassCategory, |
| 2161 | bool *isOverridingProperty, |
| 2162 | tok::ObjCKeywordKind MethodImplKind) { |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2163 | unsigned Attributes = ODS.getPropertyAttributes(); |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2164 | bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || |
| 2165 | // default is readwrite! |
| 2166 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2167 | // property is defaulted to 'assign' if it is readwrite and is |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2168 | // not retain or copy |
| 2169 | bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | (isReadWrite && |
| 2171 | !(Attributes & ObjCDeclSpec::DQ_PR_retain) && |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2172 | !(Attributes & ObjCDeclSpec::DQ_PR_copy))); |
| 2173 | QualType T = GetTypeForDeclarator(FD.D, S); |
Fariborz Jahanian | dd69aae | 2009-12-16 18:03:30 +0000 | [diff] [blame] | 2174 | if (T->isReferenceType()) { |
| 2175 | Diag(AtLoc, diag::error_reference_property); |
| 2176 | return DeclPtrTy(); |
| 2177 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2178 | Decl *ClassDecl = ClassCategory.getAs<Decl>(); |
Fariborz Jahanian | d09a456 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2179 | ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2180 | // May modify Attributes. |
| 2181 | CheckObjCPropertyAttributes(T, AtLoc, Attributes); |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2182 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2183 | if (CDecl->IsClassExtension()) { |
| 2184 | // Diagnose if this property is already in continuation class. |
| 2185 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
| 2186 | assert(DC && "ClassDecl is not a DeclContext"); |
| 2187 | DeclContext::lookup_result Found = DC->lookup(FD.D.getIdentifier()); |
| 2188 | if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) { |
| 2189 | Diag(AtLoc, diag::err_duplicate_property); |
| 2190 | Diag((*Found.first)->getLocation(), diag::note_property_declare); |
| 2191 | return DeclPtrTy(); |
| 2192 | } |
| 2193 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
| 2194 | FD.D.getIdentifierLoc(), |
| 2195 | FD.D.getIdentifier(), |
| 2196 | AtLoc, T); |
| 2197 | DC->addDecl(PDecl); |
| 2198 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | // This is a continuation class. property requires special |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2200 | // handling. |
Fariborz Jahanian | d09a456 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2201 | if ((CCPrimary = CDecl->getClassInterface())) { |
| 2202 | // Find the property in continuation class's primary class only. |
Fariborz Jahanian | d09a456 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2203 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 2204 | if (ObjCPropertyDecl *PIDecl = |
| 2205 | CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) { |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2206 | // property 'PIDecl's readonly attribute will be over-ridden |
Fariborz Jahanian | 22b6e06 | 2009-04-01 23:23:53 +0000 | [diff] [blame] | 2207 | // with continuation class's readwrite property attribute! |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2208 | unsigned PIkind = PIDecl->getPropertyAttributes(); |
| 2209 | if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { |
Fariborz Jahanian | 4770a4a | 2009-11-10 19:31:09 +0000 | [diff] [blame] | 2210 | unsigned retainCopyNonatomic = |
Fariborz Jahanian | d669be5 | 2009-11-06 22:59:12 +0000 | [diff] [blame] | 2211 | (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | cc667e2 | 2009-11-03 00:01:38 +0000 | [diff] [blame] | 2212 | ObjCPropertyDecl::OBJC_PR_copy | |
| 2213 | ObjCPropertyDecl::OBJC_PR_nonatomic); |
Fariborz Jahanian | 4770a4a | 2009-11-10 19:31:09 +0000 | [diff] [blame] | 2214 | if ((Attributes & retainCopyNonatomic) != |
| 2215 | (PIkind & retainCopyNonatomic)) { |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2216 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
Fariborz Jahanian | cc667e2 | 2009-11-03 00:01:38 +0000 | [diff] [blame] | 2217 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 2218 | } |
Fariborz Jahanian | b73e281 | 2010-01-06 21:38:30 +0000 | [diff] [blame] | 2219 | DeclContext *DC = dyn_cast<DeclContext>(CCPrimary); |
| 2220 | assert(DC && "ClassDecl is not a DeclContext"); |
| 2221 | DeclContext::lookup_result Found = |
| 2222 | DC->lookup(PIDecl->getDeclName()); |
| 2223 | bool PropertyInPrimaryClass = false; |
| 2224 | for (; Found.first != Found.second; ++Found.first) |
| 2225 | if (isa<ObjCPropertyDecl>(*Found.first)) { |
| 2226 | PropertyInPrimaryClass = true; |
| 2227 | break; |
| 2228 | } |
| 2229 | if (!PropertyInPrimaryClass) { |
| 2230 | // Protocol is not in the primary class. Must build one for it. |
| 2231 | ObjCDeclSpec ProtocolPropertyODS; |
| 2232 | // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and |
| 2233 | // ObjCPropertyDecl::PropertyAttributeKind have identical values. |
| 2234 | // Should consolidate both into one enum type. |
| 2235 | ProtocolPropertyODS.setPropertyAttributes( |
| 2236 | (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind); |
| 2237 | DeclPtrTy ProtocolPtrTy = |
| 2238 | ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS, |
| 2239 | PIDecl->getGetterName(), |
| 2240 | PIDecl->getSetterName(), |
| 2241 | DeclPtrTy::make(CCPrimary), isOverridingProperty, |
| 2242 | MethodImplKind); |
| 2243 | PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>(); |
| 2244 | } |
Fariborz Jahanian | d09a456 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2245 | PIDecl->makeitReadWriteAttribute(); |
| 2246 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 2247 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 2248 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 2249 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 2250 | PIDecl->setSetterName(SetterSel); |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 2251 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | Diag(AtLoc, diag::err_use_continuation_class) |
Fariborz Jahanian | d09a456 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2253 | << CCPrimary->getDeclName(); |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 2254 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 2255 | } |
Fariborz Jahanian | d09a456 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2256 | *isOverridingProperty = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | // Make sure setter decl is synthesized, and added to primary |
Fariborz Jahanian | 50c314c | 2009-04-15 19:19:03 +0000 | [diff] [blame] | 2258 | // class's list. |
| 2259 | ProcessPropertyDecl(PIDecl, CCPrimary); |
Fariborz Jahanian | d09a456 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2260 | return DeclPtrTy(); |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2261 | } |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2262 | |
Fariborz Jahanian | d09a456 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2263 | // No matching property found in the primary class. Just fall thru |
| 2264 | // and add property to continuation class's primary class. |
| 2265 | ClassDecl = CCPrimary; |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2266 | } else { |
Chris Lattner | f25df99 | 2009-02-20 21:38:52 +0000 | [diff] [blame] | 2267 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
| 2268 | *isOverridingProperty = true; |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2269 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | } |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2271 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2273 | // Issue a warning if property is 'assign' as default and its object, which is |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2274 | // gc'able conforms to NSCopying protocol |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2275 | if (getLangOptions().getGCMode() != LangOptions::NonGC && |
| 2276 | isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) |
| 2277 | if (T->isObjCObjectPointerType()) { |
| 2278 | QualType InterfaceTy = T->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2279 | if (const ObjCInterfaceType *OIT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2280 | InterfaceTy->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | b11d798 | 2009-08-14 18:06:25 +0000 | [diff] [blame] | 2281 | ObjCInterfaceDecl *IDecl = OIT->getDecl(); |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2282 | if (IDecl) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | if (ObjCProtocolDecl* PNSCopying = |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2284 | LookupProtocol(&Context.Idents.get("NSCopying"))) |
| 2285 | if (IDecl->ClassImplementsProtocol(PNSCopying, true)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | Diag(AtLoc, diag::warn_implements_nscopying) |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2287 | << FD.D.getIdentifier(); |
Fariborz Jahanian | b11d798 | 2009-08-14 18:06:25 +0000 | [diff] [blame] | 2288 | } |
Fariborz Jahanian | 0fd8904 | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2289 | } |
Fariborz Jahanian | acf2d13 | 2009-08-12 18:17:53 +0000 | [diff] [blame] | 2290 | if (T->isObjCInterfaceType()) |
| 2291 | Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2292 | |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 2293 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
| 2294 | assert(DC && "ClassDecl is not a DeclContext"); |
Chris Lattner | d1e0f5a | 2009-04-11 20:14:49 +0000 | [diff] [blame] | 2295 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | FD.D.getIdentifierLoc(), |
Fariborz Jahanian | d050240 | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 2297 | FD.D.getIdentifier(), |
| 2298 | AtLoc, T); |
Fariborz Jahanian | 6dd30fc | 2009-12-17 00:49:09 +0000 | [diff] [blame] | 2299 | DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName()); |
| 2300 | if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) { |
| 2301 | Diag(PDecl->getLocation(), diag::err_duplicate_property); |
| 2302 | Diag((*Found.first)->getLocation(), diag::note_property_declare); |
| 2303 | PDecl->setInvalidDecl(); |
| 2304 | } |
| 2305 | else |
| 2306 | DC->addDecl(PDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | |
Chris Lattner | d1e0f5a | 2009-04-11 20:14:49 +0000 | [diff] [blame] | 2308 | if (T->isArrayType() || T->isFunctionType()) { |
| 2309 | Diag(AtLoc, diag::err_property_type) << T; |
| 2310 | PDecl->setInvalidDecl(); |
| 2311 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2313 | ProcessDeclAttributes(S, PDecl, FD.D); |
Douglas Gregor | d043410 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 2314 | |
Fariborz Jahanian | 33de3f0 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 2315 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 2316 | // selector names in anticipation of declaration of setter/getter methods. |
| 2317 | PDecl->setGetterName(GetterSel); |
| 2318 | PDecl->setSetterName(SetterSel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2319 | |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2320 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2321 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2323 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2324 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2326 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2327 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2328 | |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2329 | if (isReadWrite) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2330 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2331 | |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2332 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2333 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2335 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2336 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2337 | |
Fariborz Jahanian | 8cf0bb3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2338 | if (isAssign) |
| 2339 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | |
Daniel Dunbar | 95e61fb | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2341 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2342 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2343 | |
Fariborz Jahanian | 46b55e5 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 2344 | if (MethodImplKind == tok::objc_required) |
| 2345 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 2346 | else if (MethodImplKind == tok::objc_optional) |
| 2347 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
Fariborz Jahanian | d09a456 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2348 | // A case of continuation class adding a new property in the class. This |
| 2349 | // is not what it was meant for. However, gcc supports it and so should we. |
| 2350 | // Make sure setter/getters are declared here. |
| 2351 | if (CCPrimary) |
| 2352 | ProcessPropertyDecl(PDecl, CCPrimary); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2354 | return DeclPtrTy::make(PDecl); |
Chris Lattner | 4d39148 | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Fariborz Jahanian | 77e2dde | 2010-02-09 21:49:50 +0000 | [diff] [blame] | 2357 | ObjCIvarDecl* |
| 2358 | Sema::SynthesizeNewPropertyIvar(ObjCInterfaceDecl *IDecl, |
| 2359 | IdentifierInfo *NameII) { |
| 2360 | ObjCIvarDecl *Ivar = 0; |
| 2361 | ObjCPropertyDecl *Prop = LookupPropertyDecl(IDecl, NameII); |
| 2362 | if (Prop && !Prop->isInvalidDecl()) { |
| 2363 | DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl); |
| 2364 | QualType PropType = Context.getCanonicalType(Prop->getType()); |
| 2365 | assert(EnclosingContext && |
| 2366 | "null DeclContext for synthesized ivar - SynthesizeNewPropertyIvar"); |
| 2367 | Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, |
| 2368 | Prop->getLocation(), |
| 2369 | NameII, PropType, /*Dinfo=*/0, |
| 2370 | ObjCIvarDecl::Public, |
| 2371 | (Expr *)0); |
| 2372 | Ivar->setLexicalDeclContext(IDecl); |
| 2373 | IDecl->addDecl(Ivar); |
| 2374 | Prop->setPropertyIvarDecl(Ivar); |
| 2375 | } |
| 2376 | return Ivar; |
| 2377 | } |
| 2378 | |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2379 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 2380 | /// builds the AST node for a property implementation declaration; declared |
| 2381 | /// as @synthesize or @dynamic. |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2382 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2383 | Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2384 | SourceLocation PropertyLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2385 | bool Synthesize, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2386 | DeclPtrTy ClassCatImpDecl, |
| 2387 | IdentifierInfo *PropertyId, |
| 2388 | IdentifierInfo *PropertyIvar) { |
| 2389 | Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>(); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2390 | // Make sure we have a context for the property implementation declaration. |
| 2391 | if (!ClassImpDecl) { |
| 2392 | Diag(AtLoc, diag::error_missing_property_context); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2393 | return DeclPtrTy(); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2394 | } |
| 2395 | ObjCPropertyDecl *property = 0; |
| 2396 | ObjCInterfaceDecl* IDecl = 0; |
| 2397 | // Find the class or category class where this property must have |
| 2398 | // a declaration. |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 2399 | ObjCImplementationDecl *IC = 0; |
| 2400 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 2401 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2402 | IDecl = IC->getClassInterface(); |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2403 | // We always synthesize an interface for an implementation |
| 2404 | // without an interface decl. So, IDecl is always non-zero. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | assert(IDecl && |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2406 | "ActOnPropertyImplDecl - @implementation without @interface"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2408 | // Look for this property declaration in the @implementation's @interface |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2409 | property = IDecl->FindPropertyDeclaration(PropertyId); |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2410 | if (!property) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2411 | Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2412 | return DeclPtrTy(); |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2413 | } |
Fariborz Jahanian | 3684bd4 | 2009-11-02 18:45:36 +0000 | [diff] [blame] | 2414 | if (const ObjCCategoryDecl *CD = |
| 2415 | dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { |
Fariborz Jahanian | 2576061 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2416 | if (!CD->IsClassExtension()) { |
Fariborz Jahanian | 3684bd4 | 2009-11-02 18:45:36 +0000 | [diff] [blame] | 2417 | Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); |
Fariborz Jahanian | a6f14e1 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 2418 | Diag(property->getLocation(), diag::note_property_declare); |
Fariborz Jahanian | 3684bd4 | 2009-11-02 18:45:36 +0000 | [diff] [blame] | 2419 | return DeclPtrTy(); |
| 2420 | } |
| 2421 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2422 | } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2423 | if (Synthesize) { |
| 2424 | Diag(AtLoc, diag::error_synthesize_category_decl); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2425 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2426 | } |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2427 | IDecl = CatImplClass->getClassInterface(); |
| 2428 | if (!IDecl) { |
| 2429 | Diag(AtLoc, diag::error_missing_property_interface); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2430 | return DeclPtrTy(); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2431 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2432 | ObjCCategoryDecl *Category = |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2433 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2435 | // If category for this implementation not found, it is an error which |
| 2436 | // has already been reported eralier. |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2437 | if (!Category) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2438 | return DeclPtrTy(); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2439 | // Look for this property declaration in @implementation's category |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2440 | property = Category->FindPropertyDeclaration(PropertyId); |
Fariborz Jahanian | 559c0c4 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2441 | if (!property) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2442 | Diag(PropertyLoc, diag::error_bad_category_property_decl) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2443 | << Category->getDeclName(); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2444 | return DeclPtrTy(); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2445 | } |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2446 | } else { |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2447 | Diag(AtLoc, diag::error_bad_property_context); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2448 | return DeclPtrTy(); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2449 | } |
Fariborz Jahanian | 628b96f | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 2450 | ObjCIvarDecl *Ivar = 0; |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2451 | // Check that we have a valid, previously declared ivar for @synthesize |
| 2452 | if (Synthesize) { |
| 2453 | // @synthesize |
Fariborz Jahanian | 6cdf16d | 2008-04-21 21:57:36 +0000 | [diff] [blame] | 2454 | if (!PropertyIvar) |
| 2455 | PropertyIvar = PropertyId; |
Fariborz Jahanian | af3e722 | 2009-03-31 00:06:29 +0000 | [diff] [blame] | 2456 | QualType PropType = Context.getCanonicalType(property->getType()); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2457 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
Fariborz Jahanian | 29da66e | 2009-04-13 19:30:37 +0000 | [diff] [blame] | 2458 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2459 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2460 | if (!Ivar) { |
Fariborz Jahanian | 8019a7b | 2009-06-06 16:36:41 +0000 | [diff] [blame] | 2461 | DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | assert(EnclosingContext && |
Fariborz Jahanian | 8019a7b | 2009-06-06 16:36:41 +0000 | [diff] [blame] | 2463 | "null DeclContext for synthesized ivar - ActOnPropertyImplDecl"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2464 | Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc, |
| 2465 | PropertyIvar, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2466 | ObjCIvarDecl::Public, |
| 2467 | (Expr *)0); |
Fariborz Jahanian | 11062e1 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 2468 | IDecl->makeDeclVisibleInContext(Ivar, false); |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2469 | property->setPropertyIvarDecl(Ivar); |
| 2470 | if (!getLangOptions().ObjCNonFragileABI) |
Steve Naroff | f4c00ff | 2009-03-03 22:09:41 +0000 | [diff] [blame] | 2471 | Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2472 | // Note! I deliberately want it to fall thru so, we have a |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2473 | // a property implementation and to avoid future warnings. |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2474 | } else if (getLangOptions().ObjCNonFragileABI && |
| 2475 | ClassDeclared != IDecl) { |
Fariborz Jahanian | e2f2c16 | 2009-04-30 21:39:24 +0000 | [diff] [blame] | 2476 | Diag(PropertyLoc, diag::error_ivar_in_superclass_use) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | << property->getDeclName() << Ivar->getDeclName() |
Fariborz Jahanian | 29da66e | 2009-04-13 19:30:37 +0000 | [diff] [blame] | 2478 | << ClassDeclared->getDeclName(); |
| 2479 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) |
| 2480 | << Ivar << Ivar->getNameAsCString(); |
| 2481 | // Note! I deliberately want it to fall thru so more errors are caught. |
| 2482 | } |
Steve Naroff | 3ce52d6 | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 2483 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2484 | |
Steve Naroff | fbbe0ac | 2008-09-30 00:24:17 +0000 | [diff] [blame] | 2485 | // Check that type of property and its ivar are type compatible. |
Steve Naroff | 3ce52d6 | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 2486 | if (PropType != IvarType) { |
Steve Naroff | 4fa4ab6 | 2008-10-16 14:59:30 +0000 | [diff] [blame] | 2487 | if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { |
Chris Lattner | 5dc266a | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2488 | Diag(PropertyLoc, diag::error_property_ivar_type) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2489 | << property->getDeclName() << Ivar->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2490 | // Note! I deliberately want it to fall thru so, we have a |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2491 | // a property implementation and to avoid future warnings. |
Steve Naroff | 3ce52d6 | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 2492 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2494 | // FIXME! Rules for properties are somewhat different that those |
| 2495 | // for assignments. Use a new routine to consolidate all cases; |
| 2496 | // specifically for property redeclarations as well as for ivars. |
| 2497 | QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType(); |
| 2498 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2499 | if (lhsType != rhsType && |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2500 | lhsType->isArithmeticType()) { |
| 2501 | Diag(PropertyLoc, diag::error_property_ivar_type) |
| 2502 | << property->getDeclName() << Ivar->getDeclName(); |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2503 | // Fall thru - see previous comment |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2504 | } |
| 2505 | // __weak is explicit. So it works on Canonical type. |
Fariborz Jahanian | c8bafd7 | 2009-04-07 21:25:06 +0000 | [diff] [blame] | 2506 | if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && |
| 2507 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2508 | Diag(PropertyLoc, diag::error_weak_property) |
| 2509 | << property->getDeclName() << Ivar->getDeclName(); |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2510 | // Fall thru - see previous comment |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2511 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | if ((property->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 0a9217f | 2009-04-10 22:42:54 +0000 | [diff] [blame] | 2513 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && |
| 2514 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2515 | Diag(PropertyLoc, diag::error_strong_property) |
| 2516 | << property->getDeclName() << Ivar->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2517 | // Fall thru - see previous comment |
Fariborz Jahanian | acdc33b | 2009-01-19 20:13:47 +0000 | [diff] [blame] | 2518 | } |
Fariborz Jahanian | c35b9e4 | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2519 | } |
Fariborz Jahanian | 12bac25 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2520 | } else if (PropertyIvar) |
| 2521 | // @dynamic |
| 2522 | Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2523 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | ObjCPropertyImplDecl *PIDecl = |
| 2525 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 2526 | property, |
| 2527 | (Synthesize ? |
| 2528 | ObjCPropertyImplDecl::Synthesize |
Daniel Dunbar | 9f0afd4 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 2529 | : ObjCPropertyImplDecl::Dynamic), |
| 2530 | Ivar); |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2531 | if (IC) { |
| 2532 | if (Synthesize) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2533 | if (ObjCPropertyImplDecl *PPIDecl = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2534 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 2536 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2537 | << PropertyIvar; |
| 2538 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 2539 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2540 | |
| 2541 | if (ObjCPropertyImplDecl *PPIDecl |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2542 | = IC->FindPropertyImplDecl(PropertyId)) { |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2543 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 2544 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2545 | return DeclPtrTy(); |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2546 | } |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2547 | IC->addPropertyImplementation(PIDecl); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2548 | } else { |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2549 | if (Synthesize) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2550 | if (ObjCPropertyImplDecl *PPIDecl = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2551 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2552 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 2553 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2554 | << PropertyIvar; |
| 2555 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 2556 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | |
| 2558 | if (ObjCPropertyImplDecl *PPIDecl = |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2559 | CatImplClass->FindPropertyImplDecl(PropertyId)) { |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2560 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 2561 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2562 | return DeclPtrTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | } |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2564 | CatImplClass->addPropertyImplementation(PIDecl); |
Fariborz Jahanian | ae6f6fd | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2565 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2567 | return DeclPtrTy::make(PIDecl); |
Fariborz Jahanian | f624f81 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2568 | } |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2569 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2570 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Douglas Gregor | ce35607 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 2571 | if (isa<TranslationUnitDecl>(CurContext->getLookupContext())) |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2572 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2574 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 2575 | D->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | |
Anders Carlsson | 1528145 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2577 | return true; |
| 2578 | } |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2579 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2580 | /// Called whenever @defs(ClassName) is encountered in the source. Inserts the |
| 2581 | /// instance variables of ClassName into Decls. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2583 | IdentifierInfo *ClassName, |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2584 | llvm::SmallVectorImpl<DeclPtrTy> &Decls) { |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2585 | // Check that ClassName is a valid class |
| 2586 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); |
| 2587 | if (!Class) { |
| 2588 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 2589 | return; |
| 2590 | } |
Fariborz Jahanian | 0468fb9 | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 2591 | if (LangOpts.ObjCNonFragileABI) { |
| 2592 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 2593 | return; |
| 2594 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2596 | // Collect the instance variables |
Fariborz Jahanian | 4183335 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 2597 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 2598 | Context.CollectObjCIvars(Class, RecFields); |
| 2599 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
| 2600 | for (unsigned i = 0; i < RecFields.size(); i++) { |
| 2601 | FieldDecl* ID = RecFields[i]; |
| 2602 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()); |
| 2603 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(), |
| 2604 | ID->getIdentifier(), ID->getType(), |
| 2605 | ID->getBitWidth()); |
| 2606 | Decls.push_back(Sema::DeclPtrTy::make(FD)); |
| 2607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2609 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2610 | for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin(); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2611 | D != Decls.end(); ++D) { |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2612 | FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>()); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2613 | if (getLangOptions().CPlusPlus) |
| 2614 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2615 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>())) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2616 | Record->addDecl(FD); |
Chris Lattner | cc98eac | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2617 | } |
| 2618 | } |
| 2619 | |