Chris Lattner | da463fe | 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 | 5b12ab8 | 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 | da463fe | 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 | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 16 | #include "clang/Sema/ExternalSemaSource.h" |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 34fb672 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Fariborz Jahanian | fe9e394 | 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 | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 29 | if (property->getType()->isObjCObjectPointerType()) |
Fariborz Jahanian | 3e68a1f | 2009-05-08 21:10:00 +0000 | [diff] [blame] | 30 | result = CheckAssignmentConstraints(GetterMethod->getResultType(), property->getType()); |
Fariborz Jahanian | fe9e394 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 31 | if (result != Compatible) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | Diag(Loc, diag::warn_accessor_property_type_mismatch) |
Fariborz Jahanian | fe9e394 | 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 | 70f41d6 | 2009-02-28 16:59:13 +0000 | [diff] [blame] | 42 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 43 | /// and user declared, in the method definition's AST. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 44 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) { |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 45 | assert(getCurMethodDecl() == 0 && "Method parsing confused"); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 46 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Steve Naroff | 542cd5d | 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 | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 9fecd74 | 2009-04-19 05:21:20 +0000 | [diff] [blame] | 52 | CurFunctionNeedsScopeChecking = false; |
| 53 | |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 54 | // Allow the rest of sema to find private method decl implementations. |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 55 | if (MDecl->isInstanceMethod()) |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 56 | AddInstanceMethodToGlobalPool(MDecl); |
| 57 | else |
| 58 | AddFactoryMethodToGlobalPool(MDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Chris Lattner | da463fe | 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 | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 61 | PushDeclContext(FnBodyScope, MDecl); |
Chris Lattner | da463fe | 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 | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 65 | |
| 66 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | 3d8552a | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 67 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 279d1cc | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 69 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 70 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 72 | // Introduce all of the other parameters into this scope. |
Chris Lattner | a499715 | 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 | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 79 | Sema::DeclPtrTy Sema:: |
Chris Lattner | d7352d6 | 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 | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 83 | const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 84 | const SourceLocation *ProtoLocs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 85 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 86 | assert(ClassName && "Missing class identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 88 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 89 | NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 90 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 5101c24 | 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 | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 97 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 98 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 99 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 100 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 102 | ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 103 | if (IDecl) { |
| 104 | // Class already seen. Is it a forward declaration? |
Steve Naroff | 119f60e | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 105 | if (!IDecl->isForwardDecl()) { |
Chris Lattner | d13b8b5 | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 106 | IDecl->setInvalidDecl(); |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 107 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName(); |
Chris Lattner | e6447ef | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 108 | Diag(IDecl->getLocation(), diag::note_previous_definition); |
| 109 | |
Steve Naroff | 119f60e | 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 | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 112 | return DeclPtrTy::make(IDecl); |
Steve Naroff | 119f60e | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 113 | } else { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 114 | IDecl->setLocation(AtInterfaceLoc); |
| 115 | IDecl->setForwardDecl(false); |
Steve Naroff | e0064d2 | 2009-09-11 00:12:01 +0000 | [diff] [blame] | 116 | IDecl->setClassLoc(ClassLoc); |
Ted Kremenek | 707ece6 | 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 | d361239 | 2009-11-17 19:08:08 +0000 | [diff] [blame] | 123 | if (AttrList) |
| 124 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | ca1e848 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 126 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, |
Steve Naroff | 5a4611c | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 128 | ClassName, ClassLoc); |
Daniel Dunbar | 73a73f5 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 129 | if (AttrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 130 | ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Steve Naroff | 3c301dc | 2009-04-23 15:15:40 +0000 | [diff] [blame] | 132 | PushOnScopeChains(IDecl, TUScope); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 133 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 135 | if (SuperName) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 136 | // Check if a different kind of symbol declared in this scope. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 137 | PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName); |
Douglas Gregor | 35b0bac | 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 | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 146 | Diag(PrevDecl->getLocation(), diag::note_previous_decl) |
| 147 | << PrevDecl->getDeclName(); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Fariborz Jahanian | 5582f23 | 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 | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 155 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | ObjCInterfaceDecl *SuperClassDecl = |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 157 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 158 | |
Fariborz Jahanian | 5582f23 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | |
Fariborz Jahanian | 5582f23 | 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 | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 169 | if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 170 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| 171 | } |
| 172 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Fariborz Jahanian | 5582f23 | 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 | 189d41f | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Fariborz Jahanian | 5582f23 | 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 | 189d41f | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 193 | } |
Fariborz Jahanian | 5582f23 | 2009-07-09 22:08:26 +0000 | [diff] [blame] | 194 | IDecl->setSuperClass(SuperClassDecl); |
| 195 | IDecl->setSuperClassLoc(SuperLoc); |
| 196 | IDecl->setLocEnd(SuperLoc); |
Steve Naroff | 189d41f | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 197 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 198 | } else { // we have a root class. |
| 199 | IDecl->setLocEnd(ClassLoc); |
| 200 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Steve Naroff | 119f60e | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 202 | /// Check then save referenced protocols. |
Chris Lattner | df59f5a | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 203 | if (NumProtoRefs) { |
Chris Lattner | 2229872 | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 204 | IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 205 | ProtoLocs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 206 | IDecl->setLocEnd(EndProtoLoc); |
| 207 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 209 | CheckObjCDeclScope(IDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 210 | return DeclPtrTy::make(IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /// ActOnCompatiblityAlias - this action is called after complete parsing of |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 214 | /// @compatibility_alias declaration. It sets up the alias relationships. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 215 | Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | IdentifierInfo *AliasName, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 217 | SourceLocation AliasLocation, |
| 218 | IdentifierInfo *ClassName, |
| 219 | SourceLocation ClassLocation) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 220 | // Look for previous declaration of alias name |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 221 | NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 222 | if (ADecl) { |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 223 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 224 | Diag(AliasLocation, diag::warn_previous_alias_decl); |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 225 | else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 226 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 227 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 228 | return DeclPtrTy(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 229 | } |
| 230 | // Check for class declaration |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 231 | NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Fariborz Jahanian | 17290c3 | 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 | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 235 | if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) { |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 236 | ClassName = IDecl->getIdentifier(); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 237 | CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Fariborz Jahanian | 17290c3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 241 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 242 | if (CDecl == 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 243 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 244 | if (CDeclU) |
Chris Lattner | d068503 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 245 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 246 | return DeclPtrTy(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 247 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 249 | // Everything checked out, instantiate a new alias declaration AST. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 251 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 253 | if (!CheckObjCDeclScope(AliasDecl)) |
Douglas Gregor | 38feed8 | 2009-04-24 02:57:34 +0000 | [diff] [blame] | 254 | PushOnScopeChains(AliasDecl, TUScope); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 255 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 256 | return DeclPtrTy::make(AliasDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 259 | void Sema::CheckForwardProtocolDeclarationForCircularDependency( |
| 260 | IdentifierInfo *PName, |
| 261 | SourceLocation &Ploc, SourceLocation PrevLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | const ObjCList<ObjCProtocolDecl> &PList) { |
Steve Naroff | 41d09ad | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 266 | if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) { |
Steve Naroff | 41d09ad | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 272 | PDecl->getLocation(), PDecl->getReferencedProtocols()); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 277 | Sema::DeclPtrTy |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 278 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 279 | IdentifierInfo *ProtocolName, |
| 280 | SourceLocation ProtocolLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 281 | const DeclPtrTy *ProtoRefs, |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 282 | unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 283 | const SourceLocation *ProtoLocs, |
Daniel Dunbar | 26e2ab4 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 284 | SourceLocation EndProtoLoc, |
| 285 | AttributeList *AttrList) { |
| 286 | // FIXME: Deal with AttrList. |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 287 | assert(ProtocolName && "Missing protocol identifier"); |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 288 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 289 | if (PDecl) { |
| 290 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | 5074f8f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 291 | if (!PDecl->isForwardDecl()) { |
Fariborz Jahanian | 54d569c | 2009-04-06 23:43:32 +0000 | [diff] [blame] | 292 | Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
Chris Lattner | e6447ef | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 293 | Diag(PDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 5074f8f | 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 | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 296 | return DeclPtrTy::make(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 297 | } |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 298 | ObjCList<ObjCProtocolDecl> PList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
Steve Naroff | 41d09ad | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 300 | CheckForwardProtocolDeclarationForCircularDependency( |
| 301 | ProtocolName, ProtocolLoc, PDecl->getLocation(), PList); |
| 302 | PList.Destroy(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Steve Naroff | eb03dac | 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 | 5074f8f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 306 | PDecl->setForwardDecl(false); |
Chris Lattner | 5074f8f | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 307 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 309 | AtProtoInterfaceLoc,ProtocolName); |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 310 | PushOnScopeChains(PDecl, TUScope); |
Chris Lattner | acc04a9 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 311 | PDecl->setForwardDecl(false); |
Chris Lattner | f87ca0a | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 312 | } |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 313 | if (AttrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 314 | ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 315 | if (NumProtoRefs) { |
Chris Lattner | acc04a9 | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 316 | /// Check then save referenced protocols. |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 317 | PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
| 318 | ProtoLocs, Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 319 | PDecl->setLocEnd(EndProtoLoc); |
| 320 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
| 322 | CheckObjCDeclScope(PDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 323 | return DeclPtrTy::make(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | c7dfbfd | 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 | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 329 | void |
Chris Lattner | 3bbae00 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 330 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 331 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 332 | unsigned NumProtocols, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 333 | llvm::SmallVectorImpl<DeclPtrTy> &Protocols) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 334 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 335 | ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first); |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 336 | if (!PDecl) { |
Douglas Gregor | 35b0bac | 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 | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 343 | Diag(PDecl->getLocation(), diag::note_previous_decl) |
| 344 | << PDecl->getDeclName(); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
| 348 | if (!PDecl) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 349 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 350 | << ProtocolId[i].first; |
Chris Lattner | 9c1842b | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 351 | continue; |
| 352 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 354 | (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); |
Chris Lattner | 9c1842b | 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 | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 359 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 360 | << ProtocolId[i].first; |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 361 | Protocols.push_back(DeclPtrTy::make(PDecl)); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 365 | /// DiagnosePropertyMismatch - Compares two properties for their |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 366 | /// attributes and types and warns on a variety of inconsistencies. |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 367 | /// |
Fariborz Jahanian | 7cf1886 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 368 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
Fariborz Jahanian | 7cf1886 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 370 | ObjCPropertyDecl *SuperProperty, |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 371 | const IdentifierInfo *inheritedName) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 373 | Property->getPropertyAttributes(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
Fariborz Jahanian | 0a070ff | 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 | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 378 | Diag(Property->getLocation(), diag::warn_readonly_property) |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 379 | << Property->getDeclName() << inheritedName; |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 380 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 381 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 382 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 383 | << Property->getDeclName() << "copy" << inheritedName; |
Fariborz Jahanian | 0a070ff | 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 | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 386 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 387 | << Property->getDeclName() << "retain" << inheritedName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 389 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 390 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 391 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 392 | << Property->getDeclName() << "atomic" << inheritedName; |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 393 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 394 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | << Property->getDeclName() << "setter" << inheritedName; |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 396 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 397 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 398 | << Property->getDeclName() << "getter" << inheritedName; |
Steve Naroff | 3e1181e | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 399 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 400 | QualType LHSType = |
Steve Naroff | 3e1181e | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 401 | Context.getCanonicalType(SuperProperty->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | QualType RHSType = |
Steve Naroff | 3e1181e | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 403 | Context.getCanonicalType(Property->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Steve Naroff | 3e1181e | 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 | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 408 | if (Context.ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false)) |
Steve Naroff | 3e1181e | 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 | 0a070ff | 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 | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 419 | void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 420 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 421 | if (!SDecl) |
| 422 | return; |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 423 | // FIXME: O(N^2) |
Argyrios Kyrtzidis | cfbfe78 | 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 | 7cf1886 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 426 | ObjCPropertyDecl *SuperPDecl = (*S); |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 427 | // Does property in super class has declaration in current class? |
Argyrios Kyrtzidis | cfbfe78 | 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 | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 430 | ObjCPropertyDecl *PDecl = (*I); |
| 431 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | DiagnosePropertyMismatch(PDecl, SuperPDecl, |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 433 | SDecl->getIdentifier()); |
Fariborz Jahanian | 0a070ff | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Fariborz Jahanian | cdb8575 | 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 | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 441 | void |
Fariborz Jahanian | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 442 | Sema::MatchOneProtocolPropertiesInClass(Decl *CDecl, |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 443 | ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | d2c2ad5 | 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 | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 448 | assert (CatDecl && "MatchOneProtocolPropertiesInClass"); |
Fariborz Jahanian | 30a4292 | 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 | d2c2ad5 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 462 | return; |
| 463 | } |
Argyrios Kyrtzidis | cfbfe78 | 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 | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 466 | ObjCPropertyDecl *Pr = (*P); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 467 | ObjCInterfaceDecl::prop_iterator CP, CE; |
Fariborz Jahanian | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 468 | // Is this property already in class's list of properties? |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 469 | for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); CP != CE; ++CP) |
Fariborz Jahanian | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 470 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 471 | break; |
Fariborz Jahanian | 519976c | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 472 | if (CP != CE) |
Fariborz Jahanian | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 473 | // Property protocol already exist in class. Diagnose any mismatch. |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 474 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
Fariborz Jahanian | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 475 | } |
Fariborz Jahanian | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Fariborz Jahanian | cdb8575 | 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 | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 481 | /// |
Fariborz Jahanian | cdb8575 | 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 | d2c2ad5 | 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 | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 490 | assert (CatDecl && "CompareProperties"); |
Fariborz Jahanian | d2c2ad5 | 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 | cdb8575 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Fariborz Jahanian | cdb8575 | 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 | d2c2ad5 | 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 | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 501 | CompareProperties(CatDecl, DeclPtrTy::make(*P)); |
Fariborz Jahanian | d2c2ad5 | 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 | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 506 | MatchOneProtocolPropertiesInClass(CatDecl, *P); |
Fariborz Jahanian | d2c2ad5 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 507 | } |
| 508 | return; |
| 509 | } |
| 510 | |
Chris Lattner | ca1e848 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 511 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Fariborz Jahanian | 98a6c4f | 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 | cdb8575 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Fariborz Jahanian | cdb8575 | 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 | 98a6c4f | 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 | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 521 | CompareProperties(IDecl, DeclPtrTy::make(*P)); |
Chris Lattner | ca1e848 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 522 | } else { |
Argyrios Kyrtzidis | b3fa863 | 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 | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 526 | MatchOneProtocolPropertiesInClass(IDecl, *P); |
Chris Lattner | ca1e848 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 527 | } |
Fariborz Jahanian | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Fariborz Jahanian | abf63e7b | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 530 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 531 | /// a class method in its extension. |
| 532 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
Fariborz Jahanian | 33afd77 | 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 | cfbfe78 | 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 | 33afd77 | 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 | cfbfe78 | 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 | 33afd77 | 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 | dac168d | 2009-04-12 08:43:13 +0000 | [diff] [blame] | 559 | /// ActOnForwardProtocolDeclaration - Handle @protocol foo; |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 560 | Action::DeclPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 561 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 562 | const IdentifierLocPair *IdentList, |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 563 | unsigned NumElts, |
| 564 | AttributeList *attrList) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 565 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 566 | llvm::SmallVector<SourceLocation, 8> ProtoLocs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 568 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 569 | IdentifierInfo *Ident = IdentList[i].first; |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 570 | ObjCProtocolDecl *PDecl = LookupProtocol(Ident); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 571 | if (PDecl == 0) { // Not already seen? |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 573 | IdentList[i].second, Ident); |
Douglas Gregor | de9f17e | 2009-04-23 23:18:26 +0000 | [diff] [blame] | 574 | PushOnScopeChains(PDecl, TUScope); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 575 | } |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 576 | if (attrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 577 | ProcessDeclAttributeList(TUScope, PDecl, attrList); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 578 | Protocols.push_back(PDecl); |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 579 | ProtoLocs.push_back(IdentList[i].second); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 580 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 581 | |
| 582 | ObjCForwardProtocolDecl *PDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 583 | ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 584 | Protocols.data(), Protocols.size(), |
| 585 | ProtoLocs.data()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 586 | CurContext->addDecl(PDecl); |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 587 | CheckObjCDeclScope(PDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 588 | return DeclPtrTy::make(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 591 | Sema::DeclPtrTy Sema:: |
Chris Lattner | d7352d6 | 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 | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 596 | const DeclPtrTy *ProtoRefs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 597 | unsigned NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 598 | const SourceLocation *ProtoLocs, |
Chris Lattner | d7352d6 | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 599 | SourceLocation EndProtoLoc) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 600 | ObjCCategoryDecl *CDecl = 0; |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 601 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc); |
Fariborz Jahanian | 30a4292 | 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 | 9a5124a | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 608 | } |
Fariborz Jahanian | 30a4292 | 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 | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 614 | |
Fariborz Jahanian | 30a4292 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | |
Chris Lattner | ced903b | 2009-02-16 21:30:01 +0000 | [diff] [blame] | 628 | // If the interface is deprecated, warn about it. |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 629 | (void)DiagnoseUseOfDecl(IDecl, ClassLoc); |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 630 | |
Fariborz Jahanian | 30a4292 | 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 | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 643 | } |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 644 | if (!CDeclChain) |
| 645 | CDecl->insertNextClassCategory(); |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 646 | } |
Chris Lattner | 9018ca8 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 647 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 648 | if (NumProtoRefs) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 649 | CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 650 | ProtoLocs, Context); |
Fariborz Jahanian | 092cd6e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 651 | // Protocols in the class extension belong to the class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 652 | if (CDecl->IsClassExtension()) |
Fariborz Jahanian | 092cd6e | 2009-10-05 20:41:32 +0000 | [diff] [blame] | 653 | IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs, |
Douglas Gregor | 002b671 | 2010-01-16 15:02:53 +0000 | [diff] [blame] | 654 | NumProtoRefs, ProtoLocs, |
| 655 | Context); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 656 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 657 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 658 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 659 | return DeclPtrTy::make(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 663 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 664 | /// object. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 665 | Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 666 | SourceLocation AtCatImplLoc, |
| 667 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 668 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 669 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc); |
Argyrios Kyrtzidis | 6d9fab7 | 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 | 071676f | 2010-01-16 16:38:58 +0000 | [diff] [blame] | 677 | SourceLocation(), SourceLocation(), |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 678 | CatName); |
| 679 | CatIDecl->setClassInterface(IDecl); |
| 680 | CatIDecl->insertNextClassCategory(); |
| 681 | } |
| 682 | } |
| 683 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | ObjCCategoryImplDecl *CDecl = |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 685 | ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName, |
| 686 | IDecl); |
Chris Lattner | da463fe | 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 | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 689 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 691 | // FIXME: PushOnScopeChains? |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 692 | CurContext->addDecl(CDecl); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 693 | |
Argyrios Kyrtzidis | 6d9fab7 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 704 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 705 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 706 | return DeclPtrTy::make(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 709 | Sema::DeclPtrTy Sema::ActOnStartClassImplementation( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 710 | SourceLocation AtClassImplLoc, |
| 711 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 712 | IdentifierInfo *SuperClassname, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 713 | SourceLocation SuperClassLoc) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 714 | ObjCInterfaceDecl* IDecl = 0; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 715 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 716 | NamedDecl *PrevDecl |
| 717 | = LookupSingleName(TUScope, ClassName, LookupOrdinaryName); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 718 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 719 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 720 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 40f7a00 | 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 | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 724 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
Fariborz Jahanian | 6f0f25b | 2009-04-23 21:49:04 +0000 | [diff] [blame] | 725 | IDecl = 0; |
| 726 | } |
Douglas Gregor | 40f7a00 | 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 | 10f1e4d | 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 | 40f7a00 | 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 | 10f1e4d | 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 | 40f7a00 | 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 | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 748 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 750 | // Check that super class name is valid class name |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 751 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | da463fe | 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 | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 754 | PrevDecl = LookupSingleName(TUScope, SuperClassname, LookupOrdinaryName); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 755 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 756 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 757 | << SuperClassname; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 758 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 759 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 761 | if (!SDecl) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 762 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 763 | << SuperClassname << ClassName; |
Chris Lattner | da463fe | 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 | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 767 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 768 | << SDecl->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 769 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 773 | |
Chris Lattner | da463fe | 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 | 73a73f5 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 777 | |
Mike Stump | 87c57ac | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 781 | ClassName, ClassLoc, false, true); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 782 | IDecl->setSuperClass(SDecl); |
| 783 | IDecl->setLocEnd(ClassLoc); |
Douglas Gregor | ac345a3 | 2009-04-24 00:16:12 +0000 | [diff] [blame] | 784 | |
| 785 | PushOnScopeChains(IDecl, TUScope); |
Daniel Dunbar | d1148a7 | 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 | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 791 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
| 793 | ObjCImplementationDecl* IMPDecl = |
| 794 | ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 795 | IDecl, SDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 796 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 797 | if (CheckObjCDeclScope(IMPDecl)) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 798 | return DeclPtrTy::make(IMPDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 800 | // Check that there is no duplicate implementation of this class. |
Argyrios Kyrtzidis | 43cee935 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 801 | if (IDecl->getImplementation()) { |
Chris Lattner | 36ac1ca | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 802 | // FIXME: Don't leak everything! |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 803 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Argyrios Kyrtzidis | 43cee935 | 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 | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 807 | IDecl->setImplementation(IMPDecl); |
Douglas Gregor | 79947a2 | 2009-04-24 00:11:27 +0000 | [diff] [blame] | 808 | PushOnScopeChains(IMPDecl, TUScope); |
Argyrios Kyrtzidis | 6d9fab7 | 2009-07-21 00:05:53 +0000 | [diff] [blame] | 809 | } |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 810 | return DeclPtrTy::make(IMPDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 813 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 814 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 815 | SourceLocation RBrace) { |
| 816 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 817 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | da463fe | 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 | aac654a | 2009-04-20 20:09:33 +0000 | [diff] [blame] | 823 | if (IDecl->isImplicitInterfaceDecl()) { |
Chris Lattner | 2229872 | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 824 | IDecl->setLocEnd(RBrace); |
Fariborz Jahanian | 20912d6 | 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 | c0309cd | 2010-02-17 18:10:54 +0000 | [diff] [blame] | 827 | ivars[i]->setLexicalDeclContext(ImpDecl); |
| 828 | IDecl->makeDeclVisibleInContext(ivars[i], false); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 829 | ImpDecl->addDecl(ivars[i]); |
Fariborz Jahanian | 20912d6 | 2010-02-17 17:00:07 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Chris Lattner | da463fe | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 838 | assert(ivars && "missing @implementation ivars"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 840 | // Check interface's Ivar list against those in the implementation. |
| 841 | // names and types must match. |
| 842 | // |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 843 | unsigned j = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 845 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 846 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 847 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 848 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 849 | assert (ImplIvar && "missing implementation ivar"); |
| 850 | assert (ClsIvar && "missing class ivar"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 852 | // First, make sure the types match. |
Chris Lattner | 35ffe33 | 2008-07-27 00:05:05 +0000 | [diff] [blame] | 853 | if (Context.getCanonicalType(ImplIvar->getType()) != |
| 854 | Context.getCanonicalType(ClsIvar->getType())) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 855 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 856 | << ImplIvar->getIdentifier() |
| 857 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 858 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 859 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) { |
| 860 | Expr *ImplBitWidth = ImplIvar->getBitWidth(); |
| 861 | Expr *ClsBitWidth = ClsIvar->getBitWidth(); |
Eli Friedman | 1c4a175 | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 862 | if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() != |
| 863 | ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) { |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 864 | Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth) |
| 865 | << ImplIvar->getIdentifier(); |
| 866 | Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition); |
| 867 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | } |
Steve Naroff | 157599f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 869 | // Make sure the names are identical. |
| 870 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 871 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 872 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 873 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 874 | } |
| 875 | --numIvars; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 876 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 878 | if (numIvars > 0) |
Chris Lattner | 83021e9 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 879 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 0f29d98 | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 880 | else if (IVI != IVE) |
Chris Lattner | 83021e9 | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 881 | Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 884 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
| 885 | bool &IncompleteImpl) { |
| 886 | if (!IncompleteImpl) { |
| 887 | Diag(ImpLoc, diag::warn_incomplete_impl); |
| 888 | IncompleteImpl = true; |
| 889 | } |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 890 | Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName(); |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 893 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 894 | ObjCMethodDecl *IntfMethodDecl) { |
Chris Lattner | a9d0ffe | 2009-04-11 18:01:59 +0000 | [diff] [blame] | 895 | if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(), |
Fariborz Jahanian | 0c20bdd | 2009-05-14 23:52:54 +0000 | [diff] [blame] | 896 | ImpMethodDecl->getResultType()) && |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 897 | !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(), |
| 898 | ImpMethodDecl->getResultType())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types) |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 900 | << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType() |
| 901 | << ImpMethodDecl->getResultType(); |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 902 | Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition); |
| 903 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 905 | for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
| 906 | IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end(); |
| 907 | IM != EM; ++IM, ++IF) { |
Fariborz Jahanian | 41e803d | 2009-11-18 18:56:09 +0000 | [diff] [blame] | 908 | QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType(); |
| 909 | QualType ParmImpTy = (*IM)->getType().getUnqualifiedType(); |
| 910 | if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) || |
| 911 | Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy)) |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 912 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | |
| 914 | Diag((*IM)->getLocation(), diag::warn_conflicting_param_types) |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 915 | << ImpMethodDecl->getDeclName() << (*IF)->getType() |
| 916 | << (*IM)->getType(); |
Chris Lattner | 5300acc | 2009-04-11 20:14:49 +0000 | [diff] [blame] | 917 | Diag((*IF)->getLocation(), diag::note_previous_definition); |
Chris Lattner | 67f35b0 | 2009-04-11 19:58:42 +0000 | [diff] [blame] | 918 | } |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 921 | /// isPropertyReadonly - Return true if property is readonly, by searching |
| 922 | /// for the property in the class and in its categories and implementations |
| 923 | /// |
| 924 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, |
Steve Naroff | de68001 | 2009-02-26 19:11:32 +0000 | [diff] [blame] | 925 | ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 926 | // by far the most common case. |
| 927 | if (!PDecl->isReadOnly()) |
| 928 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | // Even if property is ready only, if interface has a user defined setter, |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 930 | // it is not considered read only. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 931 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 932 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 933 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 934 | // Main class has the property as 'readonly'. Must search |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | // through the category list to see if the property's |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 936 | // attribute has been over-ridden to 'readwrite'. |
| 937 | for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); |
| 938 | Category; Category = Category->getNextClassCategory()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | // Even if property is ready only, if a category has a user defined setter, |
| 940 | // it is not considered read only. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 941 | if (Category->getInstanceMethod(PDecl->getSetterName())) |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 942 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | ObjCPropertyDecl *P = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 944 | Category->FindPropertyDeclaration(PDecl->getIdentifier()); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 945 | if (P && !P->isReadOnly()) |
| 946 | return false; |
| 947 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 949 | // Also, check for definition of a setter method in the implementation if |
| 950 | // all else failed. |
| 951 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | if (ObjCImplementationDecl *IMD = |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 953 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 954 | if (IMD->getInstanceMethod(PDecl->getSetterName())) |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 955 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | } else if (ObjCCategoryImplDecl *CIMD = |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 957 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 958 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 959 | return false; |
| 960 | } |
| 961 | } |
Steve Naroff | de68001 | 2009-02-26 19:11:32 +0000 | [diff] [blame] | 962 | // Lastly, look through the implementation (if one is in scope). |
Argyrios Kyrtzidis | 43cee935 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 963 | if (ObjCImplementationDecl *ImpDecl = IDecl->getImplementation()) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 964 | if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) |
Steve Naroff | de68001 | 2009-02-26 19:11:32 +0000 | [diff] [blame] | 965 | return false; |
Fariborz Jahanian | 15e3a5c | 2009-04-06 16:59:10 +0000 | [diff] [blame] | 966 | // If all fails, look at the super class. |
| 967 | if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) |
| 968 | return isPropertyReadonly(PDecl, SIDecl); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 969 | return true; |
| 970 | } |
| 971 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 972 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| 973 | /// improve the efficiency of selector lookups and type checking by associating |
| 974 | /// with each protocol / interface / category the flattened instance tables. If |
| 975 | /// we used an immutable set to keep the table then it wouldn't add significant |
| 976 | /// memory cost and it would be handy for lookups. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 977 | |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 978 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 979 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 980 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 981 | ObjCProtocolDecl *PDecl, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 982 | bool& IncompleteImpl, |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 983 | const llvm::DenseSet<Selector> &InsMap, |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 984 | const llvm::DenseSet<Selector> &ClsMap, |
| 985 | ObjCInterfaceDecl *IDecl) { |
| 986 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 987 | ObjCInterfaceDecl *NSIDecl = 0; |
| 988 | if (getLangOptions().NeXTRuntime) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | // check to see if class implements forwardInvocation method and objects |
| 990 | // of this class are derived from 'NSProxy' so that to forward requests |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 991 | // from one object to another. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 992 | // Under such conditions, which means that every method possible is |
| 993 | // implemented in the class, we should not issue "Method definition not |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 994 | // found" warnings. |
| 995 | // FIXME: Use a general GetUnarySelector method for this. |
| 996 | IdentifierInfo* II = &Context.Idents.get("forwardInvocation"); |
| 997 | Selector fISelector = Context.Selectors.getSelector(1, &II); |
| 998 | if (InsMap.count(fISelector)) |
| 999 | // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| 1000 | // need be implemented in the implementation. |
| 1001 | NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy")); |
| 1002 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1004 | // If a method lookup fails locally we still need to look and see if |
| 1005 | // the method was implemented by a base class or an inherited |
| 1006 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 1007 | // and otherwise would terminate in a warning. |
| 1008 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1009 | // check unimplemented instance methods. |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1010 | if (!NSIDecl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1012 | E = PDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1013 | ObjCMethodDecl *method = *I; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1015 | !method->isSynthesized() && !InsMap.count(method->getSelector()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | (!Super || |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1017 | !Super->lookupInstanceMethod(method->getSelector()))) { |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1018 | // Ugly, but necessary. Method declared in protcol might have |
| 1019 | // have been synthesized due to a property declared in the class which |
| 1020 | // uses the protocol. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1021 | ObjCMethodDecl *MethodInClass = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1022 | IDecl->lookupInstanceMethod(method->getSelector()); |
Fariborz Jahanian | db3a4c1 | 2009-05-22 17:12:32 +0000 | [diff] [blame] | 1023 | if (!MethodInClass || !MethodInClass->isSynthesized()) |
| 1024 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
| 1025 | } |
| 1026 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1027 | // check unimplemented class methods |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1029 | I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1030 | I != E; ++I) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1031 | ObjCMethodDecl *method = *I; |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1032 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 1033 | !ClsMap.count(method->getSelector()) && |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1034 | (!Super || !Super->lookupClassMethod(method->getSelector()))) |
Steve Naroff | 15833ed | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 1035 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Steve Naroff | 3ce37a6 | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 1036 | } |
Chris Lattner | 390d39a | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 1037 | // Check on this protocols's referenced protocols, recursively. |
| 1038 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1039 | E = PDecl->protocol_end(); PI != E; ++PI) |
Daniel Dunbar | c7dfbfd | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 1040 | CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1043 | /// MatchAllMethodDeclarations - Check methods declaraed in interface or |
| 1044 | /// or protocol against those declared in their implementations. |
| 1045 | /// |
| 1046 | void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap, |
| 1047 | const llvm::DenseSet<Selector> &ClsMap, |
| 1048 | llvm::DenseSet<Selector> &InsMapSeen, |
| 1049 | llvm::DenseSet<Selector> &ClsMapSeen, |
| 1050 | ObjCImplDecl* IMPDecl, |
| 1051 | ObjCContainerDecl* CDecl, |
| 1052 | bool &IncompleteImpl, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | bool ImmediateClass) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1054 | // Check and see if instance methods in class interface have been |
| 1055 | // implemented in the implementation class. If so, their types match. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1056 | for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(), |
| 1057 | E = CDecl->instmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1058 | if (InsMapSeen.count((*I)->getSelector())) |
| 1059 | continue; |
| 1060 | InsMapSeen.insert((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | if (!(*I)->isSynthesized() && |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1062 | !InsMap.count((*I)->getSelector())) { |
| 1063 | if (ImmediateClass) |
| 1064 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
| 1065 | continue; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1066 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | ObjCMethodDecl *ImpMethodDecl = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1068 | IMPDecl->getInstanceMethod((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1069 | ObjCMethodDecl *IntfMethodDecl = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1070 | CDecl->getInstanceMethod((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | assert(IntfMethodDecl && |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1072 | "IntfMethodDecl is null in ImplMethodsVsClassMethods"); |
| 1073 | // ImpMethodDecl may be null as in a @dynamic property. |
| 1074 | if (ImpMethodDecl) |
| 1075 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 1076 | } |
| 1077 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1079 | // Check and see if class methods in class interface have been |
| 1080 | // implemented in the implementation class. If so, their types match. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | for (ObjCInterfaceDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1082 | I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1083 | if (ClsMapSeen.count((*I)->getSelector())) |
| 1084 | continue; |
| 1085 | ClsMapSeen.insert((*I)->getSelector()); |
| 1086 | if (!ClsMap.count((*I)->getSelector())) { |
| 1087 | if (ImmediateClass) |
| 1088 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1089 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1090 | ObjCMethodDecl *ImpMethodDecl = |
| 1091 | IMPDecl->getClassMethod((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | ObjCMethodDecl *IntfMethodDecl = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1093 | CDecl->getClassMethod((*I)->getSelector()); |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1094 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 1095 | } |
| 1096 | } |
| 1097 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
| 1098 | // Check for any implementation of a methods declared in protocol. |
| 1099 | for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), |
| 1100 | E = I->protocol_end(); PI != E; ++PI) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1102 | IMPDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1103 | (*PI), IncompleteImpl, false); |
| 1104 | if (I->getSuperClass()) |
| 1105 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | IMPDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1107 | I->getSuperClass(), IncompleteImpl, false); |
| 1108 | } |
| 1109 | } |
| 1110 | |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1111 | /// CollectImmediateProperties - This routine collects all properties in |
| 1112 | /// the class and its conforming protocols; but not those it its super class. |
| 1113 | void Sema::CollectImmediateProperties(ObjCContainerDecl *CDecl, |
| 1114 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap) { |
| 1115 | if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1116 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1117 | E = IDecl->prop_end(); P != E; ++P) { |
| 1118 | ObjCPropertyDecl *Prop = (*P); |
| 1119 | PropMap[Prop->getIdentifier()] = Prop; |
| 1120 | } |
| 1121 | // scan through class's protocols. |
| 1122 | for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(), |
| 1123 | E = IDecl->protocol_end(); PI != E; ++PI) |
| 1124 | CollectImmediateProperties((*PI), PropMap); |
| 1125 | } |
Fariborz Jahanian | 4f8a571 | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1126 | if (ObjCCategoryDecl *CATDecl = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1127 | if (!CATDecl->IsClassExtension()) |
| 1128 | for (ObjCContainerDecl::prop_iterator P = CATDecl->prop_begin(), |
| 1129 | E = CATDecl->prop_end(); P != E; ++P) { |
| 1130 | ObjCPropertyDecl *Prop = (*P); |
| 1131 | PropMap[Prop->getIdentifier()] = Prop; |
| 1132 | } |
Fariborz Jahanian | 4f8a571 | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1133 | // scan through class's protocols. |
| 1134 | for (ObjCInterfaceDecl::protocol_iterator PI = CATDecl->protocol_begin(), |
| 1135 | E = CATDecl->protocol_end(); PI != E; ++PI) |
| 1136 | CollectImmediateProperties((*PI), PropMap); |
| 1137 | } |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1138 | else if (ObjCProtocolDecl *PDecl = dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1139 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1140 | E = PDecl->prop_end(); P != E; ++P) { |
| 1141 | ObjCPropertyDecl *Prop = (*P); |
| 1142 | ObjCPropertyDecl *&PropEntry = PropMap[Prop->getIdentifier()]; |
| 1143 | if (!PropEntry) |
| 1144 | PropEntry = Prop; |
| 1145 | } |
| 1146 | // scan through protocol's protocols. |
| 1147 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1148 | E = PDecl->protocol_end(); PI != E; ++PI) |
| 1149 | CollectImmediateProperties((*PI), PropMap); |
| 1150 | } |
| 1151 | } |
| 1152 | |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1153 | /// LookupPropertyDecl - Looks up a property in the current class and all |
| 1154 | /// its protocols. |
| 1155 | ObjCPropertyDecl *Sema::LookupPropertyDecl(const ObjCContainerDecl *CDecl, |
| 1156 | IdentifierInfo *II) { |
| 1157 | if (const ObjCInterfaceDecl *IDecl = |
| 1158 | dyn_cast<ObjCInterfaceDecl>(CDecl)) { |
| 1159 | for (ObjCContainerDecl::prop_iterator P = IDecl->prop_begin(), |
| 1160 | E = IDecl->prop_end(); P != E; ++P) { |
| 1161 | ObjCPropertyDecl *Prop = (*P); |
| 1162 | if (Prop->getIdentifier() == II) |
| 1163 | return Prop; |
| 1164 | } |
| 1165 | // scan through class's protocols. |
| 1166 | for (ObjCInterfaceDecl::protocol_iterator PI = IDecl->protocol_begin(), |
| 1167 | E = IDecl->protocol_end(); PI != E; ++PI) { |
| 1168 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 1169 | if (Prop) |
| 1170 | return Prop; |
| 1171 | } |
| 1172 | } |
| 1173 | else if (const ObjCProtocolDecl *PDecl = |
| 1174 | dyn_cast<ObjCProtocolDecl>(CDecl)) { |
| 1175 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 1176 | E = PDecl->prop_end(); P != E; ++P) { |
| 1177 | ObjCPropertyDecl *Prop = (*P); |
| 1178 | if (Prop->getIdentifier() == II) |
| 1179 | return Prop; |
| 1180 | } |
| 1181 | // scan through protocol's protocols. |
| 1182 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 1183 | E = PDecl->protocol_end(); PI != E; ++PI) { |
| 1184 | ObjCPropertyDecl *Prop = LookupPropertyDecl((*PI), II); |
| 1185 | if (Prop) |
| 1186 | return Prop; |
| 1187 | } |
| 1188 | } |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1193 | void Sema::DiagnoseUnimplementedProperties(ObjCImplDecl* IMPDecl, |
| 1194 | ObjCContainerDecl *CDecl, |
| 1195 | const llvm::DenseSet<Selector>& InsMap) { |
| 1196 | llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*> PropMap; |
| 1197 | CollectImmediateProperties(CDecl, PropMap); |
Fariborz Jahanian | b36606a | 2010-01-20 17:27:59 +0000 | [diff] [blame] | 1198 | if (PropMap.empty()) |
| 1199 | return; |
| 1200 | |
| 1201 | llvm::DenseSet<ObjCPropertyDecl *> PropImplMap; |
| 1202 | for (ObjCImplDecl::propimpl_iterator |
| 1203 | I = IMPDecl->propimpl_begin(), |
| 1204 | EI = IMPDecl->propimpl_end(); I != EI; ++I) |
| 1205 | PropImplMap.insert((*I)->getPropertyDecl()); |
| 1206 | |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1207 | for (llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>::iterator |
| 1208 | P = PropMap.begin(), E = PropMap.end(); P != E; ++P) { |
| 1209 | ObjCPropertyDecl *Prop = P->second; |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1210 | // Is there a matching propery synthesize/dynamic? |
Fariborz Jahanian | b36606a | 2010-01-20 17:27:59 +0000 | [diff] [blame] | 1211 | if (Prop->isInvalidDecl() || |
| 1212 | Prop->getPropertyImplementation() == ObjCPropertyDecl::Optional || |
| 1213 | PropImplMap.count(Prop)) |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1214 | continue; |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1215 | if (LangOpts.ObjCNonFragileABI2) { |
| 1216 | ActOnPropertyImplDecl(IMPDecl->getLocation(), |
| 1217 | SourceLocation(), |
| 1218 | true, DeclPtrTy::make(IMPDecl), |
| 1219 | Prop->getIdentifier(), |
| 1220 | Prop->getIdentifier()); |
| 1221 | continue; |
| 1222 | } |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1223 | if (!InsMap.count(Prop->getGetterName())) { |
| 1224 | Diag(Prop->getLocation(), |
Fariborz Jahanian | 4f8a571 | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1225 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1226 | diag::warn_setter_getter_impl_required_in_category : |
| 1227 | diag::warn_setter_getter_impl_required) |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1228 | << Prop->getDeclName() << Prop->getGetterName(); |
| 1229 | Diag(IMPDecl->getLocation(), |
| 1230 | diag::note_property_impl_required); |
| 1231 | } |
| 1232 | |
| 1233 | if (!Prop->isReadOnly() && !InsMap.count(Prop->getSetterName())) { |
| 1234 | Diag(Prop->getLocation(), |
Fariborz Jahanian | 4f8a571 | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1235 | isa<ObjCCategoryDecl>(CDecl) ? |
| 1236 | diag::warn_setter_getter_impl_required_in_category : |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1237 | diag::warn_setter_getter_impl_required) |
| 1238 | << Prop->getDeclName() << Prop->getSetterName(); |
| 1239 | Diag(IMPDecl->getLocation(), |
| 1240 | diag::note_property_impl_required); |
| 1241 | } |
| 1242 | } |
Fariborz Jahanian | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl, |
| 1246 | ObjCContainerDecl* CDecl, |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1247 | bool IncompleteImpl) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1248 | llvm::DenseSet<Selector> InsMap; |
| 1249 | // Check and see if instance methods in class interface have been |
| 1250 | // implemented in the implementation class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1252 | I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I) |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1253 | InsMap.insert((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 1255 | // Check and see if properties declared in the interface have either 1) |
| 1256 | // an implementation or 2) there is a @synthesize/@dynamic implementation |
| 1257 | // of the property in the @implementation. |
| 1258 | if (isa<ObjCInterfaceDecl>(CDecl)) |
Fariborz Jahanian | 98609b3 | 2010-01-20 01:51:55 +0000 | [diff] [blame] | 1259 | DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap); |
| 1260 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1261 | llvm::DenseSet<Selector> ClsMap; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1262 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1263 | I = IMPDecl->classmeth_begin(), |
| 1264 | E = IMPDecl->classmeth_end(); I != E; ++I) |
Chris Lattner | 061227a | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 1265 | ClsMap.insert((*I)->getSelector()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1266 | |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1267 | // Check for type conflict of methods declared in a class/protocol and |
| 1268 | // its implementation; if any. |
| 1269 | llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen, |
| 1271 | IMPDecl, CDecl, |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1272 | IncompleteImpl, true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1274 | // Check the protocol list for unimplemented methods in the @implementation |
| 1275 | // class. |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1276 | // Check and see if class methods in class interface have been |
| 1277 | // implemented in the implementation class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1279 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
Fariborz Jahanian | 07b7165 | 2009-05-01 20:07:12 +0000 | [diff] [blame] | 1280 | for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(), |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1281 | E = I->protocol_end(); PI != E; ++PI) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1282 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1283 | InsMap, ClsMap, I); |
| 1284 | // Check class extensions (unnamed categories) |
| 1285 | for (ObjCCategoryDecl *Categories = I->getCategoryList(); |
| 1286 | Categories; Categories = Categories->getNextClassCategory()) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1287 | if (Categories->IsClassExtension()) { |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1288 | ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl); |
| 1289 | break; |
| 1290 | } |
Fariborz Jahanian | 7988d7d | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 1291 | } |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1292 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1293 | // For extended class, unimplemented methods in its protocols will |
| 1294 | // be reported in the primary class. |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1295 | if (!C->IsClassExtension()) { |
Fariborz Jahanian | 8764c74 | 2009-10-05 21:32:49 +0000 | [diff] [blame] | 1296 | for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(), |
| 1297 | E = C->protocol_end(); PI != E; ++PI) |
| 1298 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
| 1299 | InsMap, ClsMap, C->getClassInterface()); |
Fariborz Jahanian | 4f8a571 | 2010-01-20 19:36:21 +0000 | [diff] [blame] | 1300 | // Report unimplemented properties in the category as well. |
| 1301 | // When reporting on missing setter/getters, do not report when |
| 1302 | // setter/getter is implemented in category's primary class |
| 1303 | // implementation. |
| 1304 | if (ObjCInterfaceDecl *ID = C->getClassInterface()) |
| 1305 | if (ObjCImplDecl *IMP = ID->getImplementation()) { |
| 1306 | for (ObjCImplementationDecl::instmeth_iterator |
| 1307 | I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I) |
| 1308 | InsMap.insert((*I)->getSelector()); |
| 1309 | } |
| 1310 | DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap); |
| 1311 | } |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1312 | } else |
| 1313 | assert(false && "invalid ObjCContainerDecl type."); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1316 | void |
| 1317 | Sema::AtomicPropertySetterGetterRules (ObjCImplDecl* IMPDecl, |
| 1318 | ObjCContainerDecl* IDecl) { |
| 1319 | // Rules apply in non-GC mode only |
| 1320 | if (getLangOptions().getGCMode() != LangOptions::NonGC) |
| 1321 | return; |
| 1322 | for (ObjCContainerDecl::prop_iterator I = IDecl->prop_begin(), |
| 1323 | E = IDecl->prop_end(); |
| 1324 | I != E; ++I) { |
| 1325 | ObjCPropertyDecl *Property = (*I); |
| 1326 | unsigned Attributes = Property->getPropertyAttributes(); |
| 1327 | // We only care about readwrite atomic property. |
| 1328 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) || |
| 1329 | !(Attributes & ObjCPropertyDecl::OBJC_PR_readwrite)) |
| 1330 | continue; |
| 1331 | if (const ObjCPropertyImplDecl *PIDecl |
| 1332 | = IMPDecl->FindPropertyImplDecl(Property->getIdentifier())) { |
| 1333 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 1334 | continue; |
| 1335 | ObjCMethodDecl *GetterMethod = |
| 1336 | IMPDecl->getInstanceMethod(Property->getGetterName()); |
| 1337 | ObjCMethodDecl *SetterMethod = |
| 1338 | IMPDecl->getInstanceMethod(Property->getSetterName()); |
| 1339 | if ((GetterMethod && !SetterMethod) || (!GetterMethod && SetterMethod)) { |
| 1340 | SourceLocation MethodLoc = |
| 1341 | (GetterMethod ? GetterMethod->getLocation() |
| 1342 | : SetterMethod->getLocation()); |
| 1343 | Diag(MethodLoc, diag::warn_atomic_property_rule) |
| 1344 | << Property->getIdentifier(); |
| 1345 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1346 | } |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | /// ActOnForwardClassDeclaration - |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1352 | Action::DeclPtrTy |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1353 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1354 | IdentifierInfo **IdentList, |
Ted Kremenek | a26da85 | 2009-11-17 23:12:20 +0000 | [diff] [blame] | 1355 | SourceLocation *IdentLocs, |
Chris Lattner | 99a8331 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 1356 | unsigned NumElts) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1357 | llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1359 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1360 | // Check for another declaration kind with the same name. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1361 | NamedDecl *PrevDecl |
| 1362 | = LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName); |
Douglas Gregor | 5daeee2 | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 1363 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 1364 | // Maybe we will complain about the shadowed template parameter. |
| 1365 | DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl); |
| 1366 | // Just pretend that we didn't see the previous declaration. |
| 1367 | PrevDecl = 0; |
| 1368 | } |
| 1369 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1370 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | 946166f | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 1371 | // GCC apparently allows the following idiom: |
| 1372 | // |
| 1373 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 1374 | // @class XCElementToggler; |
| 1375 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | // FIXME: Make an extension? |
Steve Naroff | 946166f | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 1377 | TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); |
| 1378 | if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1379 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1380 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1381 | } else if (TDD) { |
| 1382 | // a forward class declaration matching a typedef name of a class refers |
| 1383 | // to the underlying class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | if (ObjCInterfaceType * OI = |
Fariborz Jahanian | 0d45181 | 2009-05-07 21:49:26 +0000 | [diff] [blame] | 1385 | dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType())) |
| 1386 | PrevDecl = OI->getDecl(); |
| 1387 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1388 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1389 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1390 | if (!IDecl) { // Not already seen? Make a forward decl. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1391 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1392 | IdentList[i], IdentLocs[i], true); |
Ted Kremenek | 707ece6 | 2009-11-17 22:58:30 +0000 | [diff] [blame] | 1393 | |
| 1394 | // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to |
| 1395 | // the current DeclContext. This prevents clients that walk DeclContext |
| 1396 | // from seeing the imaginary ObjCInterfaceDecl until it is actually |
| 1397 | // declared later (if at all). We also take care to explicitly make |
| 1398 | // sure this declaration is visible for name lookup. |
| 1399 | PushOnScopeChains(IDecl, TUScope, false); |
| 1400 | CurContext->makeDeclVisibleInContext(IDecl, true); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | Interfaces.push_back(IDecl); |
| 1404 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1406 | assert(Interfaces.size() == NumElts); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1407 | ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc, |
Ted Kremenek | 9b124e1 | 2009-11-18 00:28:11 +0000 | [diff] [blame] | 1408 | Interfaces.data(), IdentLocs, |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1409 | Interfaces.size()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1410 | CurContext->addDecl(CDecl); |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1411 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1412 | return DeclPtrTy::make(CDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | |
| 1416 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 1417 | /// returns true, or false, accordingly. |
| 1418 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1420 | const ObjCMethodDecl *PrevMethod, |
| 1421 | bool matchBasedOnSizeAndAlignment) { |
| 1422 | QualType T1 = Context.getCanonicalType(Method->getResultType()); |
| 1423 | QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1425 | if (T1 != T2) { |
| 1426 | // The result types are different. |
| 1427 | if (!matchBasedOnSizeAndAlignment) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1428 | return false; |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1429 | // Incomplete types don't have a size and alignment. |
| 1430 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1431 | return false; |
| 1432 | // Check is based on size and alignment. |
| 1433 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1434 | return false; |
| 1435 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1436 | |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1437 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 1438 | E = Method->param_end(); |
| 1439 | ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1440 | |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1441 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1442 | assert(PrevI != PrevMethod->param_end() && "Param mismatch"); |
| 1443 | T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1444 | T2 = Context.getCanonicalType((*PrevI)->getType()); |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1445 | if (T1 != T2) { |
| 1446 | // The result types are different. |
| 1447 | if (!matchBasedOnSizeAndAlignment) |
| 1448 | return false; |
| 1449 | // Incomplete types don't have a size and alignment. |
| 1450 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1451 | return false; |
| 1452 | // Check is based on size and alignment. |
| 1453 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1454 | return false; |
| 1455 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1456 | } |
| 1457 | return true; |
| 1458 | } |
| 1459 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1460 | /// \brief Read the contents of the instance and factory method pools |
| 1461 | /// for a given selector from external storage. |
| 1462 | /// |
| 1463 | /// This routine should only be called once, when neither the instance |
| 1464 | /// nor the factory method pool has an entry for this selector. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel, |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1466 | bool isInstance) { |
| 1467 | assert(ExternalSource && "We need an external AST source"); |
| 1468 | assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() && |
| 1469 | "Selector data already loaded into the instance method pool"); |
| 1470 | assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() && |
| 1471 | "Selector data already loaded into the factory method pool"); |
| 1472 | |
| 1473 | // Read the method list from the external source. |
| 1474 | std::pair<ObjCMethodList, ObjCMethodList> Methods |
| 1475 | = ExternalSource->ReadMethodPool(Sel); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1477 | if (isInstance) { |
| 1478 | if (Methods.second.Method) |
| 1479 | FactoryMethodPool[Sel] = Methods.second; |
| 1480 | return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | } |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1482 | |
| 1483 | if (Methods.first.Method) |
| 1484 | InstanceMethodPool[Sel] = Methods.first; |
| 1485 | |
| 1486 | return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first; |
| 1487 | } |
| 1488 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1489 | void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1490 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos |
| 1491 | = InstanceMethodPool.find(Method->getSelector()); |
| 1492 | if (Pos == InstanceMethodPool.end()) { |
| 1493 | if (ExternalSource && !FactoryMethodPool.count(Method->getSelector())) |
| 1494 | Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true); |
| 1495 | else |
| 1496 | Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(), |
| 1497 | ObjCMethodList())).first; |
| 1498 | } |
| 1499 | |
| 1500 | ObjCMethodList &Entry = Pos->second; |
Chris Lattner | 7b26b29 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1501 | if (Entry.Method == 0) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1502 | // Haven't seen a method with this selector name yet - add it. |
Chris Lattner | 7b26b29 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1503 | Entry.Method = Method; |
| 1504 | Entry.Next = 0; |
| 1505 | return; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1506 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1507 | |
Chris Lattner | 7b26b29 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1508 | // We've seen a method with this name, see if we have already seen this type |
| 1509 | // signature. |
| 1510 | for (ObjCMethodList *List = &Entry; List; List = List->Next) |
| 1511 | if (MatchTwoMethodDeclarations(Method, List->Method)) |
| 1512 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | |
Chris Lattner | 7b26b29 | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1514 | // We have a new signature for an existing method - add it. |
| 1515 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | da4abf1 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 1516 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
| 1517 | Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Steve Naroff | 9ebc050 | 2008-10-21 10:50:19 +0000 | [diff] [blame] | 1520 | // FIXME: Finish implementing -Wno-strict-selector-match. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel, |
Fariborz Jahanian | cbf10f5 | 2009-08-22 21:13:55 +0000 | [diff] [blame] | 1522 | SourceRange R, |
| 1523 | bool warn) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1524 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos |
| 1525 | = InstanceMethodPool.find(Sel); |
Douglas Gregor | 9a1899b | 2009-04-24 22:23:41 +0000 | [diff] [blame] | 1526 | if (Pos == InstanceMethodPool.end()) { |
| 1527 | if (ExternalSource && !FactoryMethodPool.count(Sel)) |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1528 | Pos = ReadMethodPool(Sel, /*isInstance=*/true); |
| 1529 | else |
| 1530 | return 0; |
| 1531 | } |
| 1532 | |
| 1533 | ObjCMethodList &MethList = Pos->second; |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1534 | bool issueWarning = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1535 | |
Steve Naroff | 4a82d81 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1536 | if (MethList.Method && MethList.Next) { |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1537 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
| 1538 | // This checks if the methods differ by size & alignment. |
| 1539 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) |
Fariborz Jahanian | cbf10f5 | 2009-08-22 21:13:55 +0000 | [diff] [blame] | 1540 | issueWarning = warn; |
Steve Naroff | a0ed165 | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1541 | } |
| 1542 | if (issueWarning && (MethList.Method && MethList.Next)) { |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1543 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 1544 | Diag(MethList.Method->getLocStart(), diag::note_using) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1545 | << MethList.Method->getSourceRange(); |
Steve Naroff | 4a82d81 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1546 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 1547 | Diag(Next->Method->getLocStart(), diag::note_also_found) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1548 | << Next->Method->getSourceRange(); |
Steve Naroff | 4a82d81 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1549 | } |
| 1550 | return MethList.Method; |
| 1551 | } |
| 1552 | |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1553 | void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1554 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos |
| 1555 | = FactoryMethodPool.find(Method->getSelector()); |
| 1556 | if (Pos == FactoryMethodPool.end()) { |
| 1557 | if (ExternalSource && !InstanceMethodPool.count(Method->getSelector())) |
| 1558 | Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false); |
| 1559 | else |
| 1560 | Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(), |
| 1561 | ObjCMethodList())).first; |
| 1562 | } |
| 1563 | |
| 1564 | ObjCMethodList &FirstMethod = Pos->second; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1565 | if (!FirstMethod.Method) { |
| 1566 | // Haven't seen a method with this selector name yet - add it. |
| 1567 | FirstMethod.Method = Method; |
| 1568 | FirstMethod.Next = 0; |
| 1569 | } else { |
| 1570 | // We've seen a method with this name, now check the type signature(s). |
| 1571 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1572 | |
| 1573 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1574 | Next = Next->Next) |
| 1575 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1577 | if (!match) { |
| 1578 | // We have a new signature for an existing method - add it. |
| 1579 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | da4abf1 | 2010-02-11 00:53:01 +0000 | [diff] [blame] | 1580 | ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>(); |
| 1581 | ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1582 | FirstMethod.Next = OMI; |
| 1583 | } |
| 1584 | } |
| 1585 | } |
| 1586 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1587 | ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel, |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1588 | SourceRange R) { |
| 1589 | llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos |
| 1590 | = FactoryMethodPool.find(Sel); |
| 1591 | if (Pos == FactoryMethodPool.end()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | if (ExternalSource && !InstanceMethodPool.count(Sel)) |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1593 | Pos = ReadMethodPool(Sel, /*isInstance=*/false); |
| 1594 | else |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
| 1598 | ObjCMethodList &MethList = Pos->second; |
| 1599 | bool issueWarning = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1601 | if (MethList.Method && MethList.Next) { |
| 1602 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
| 1603 | // This checks if the methods differ by size & alignment. |
| 1604 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) |
| 1605 | issueWarning = true; |
| 1606 | } |
| 1607 | if (issueWarning && (MethList.Method && MethList.Next)) { |
| 1608 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 1609 | Diag(MethList.Method->getLocStart(), diag::note_using) |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1610 | << MethList.Method->getSourceRange(); |
| 1611 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
John McCall | e29c5cd | 2009-12-10 19:51:03 +0000 | [diff] [blame] | 1612 | Diag(Next->Method->getLocStart(), diag::note_also_found) |
Douglas Gregor | c78d346 | 2009-04-24 21:10:55 +0000 | [diff] [blame] | 1613 | << Next->Method->getSourceRange(); |
| 1614 | } |
| 1615 | return MethList.Method; |
| 1616 | } |
| 1617 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1619 | /// have the property type and issue diagnostics if they don't. |
| 1620 | /// Also synthesize a getter/setter method if none exist (and update the |
| 1621 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized |
| 1622 | /// methods is the "right" thing to do. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1624 | ObjCContainerDecl *CD) { |
| 1625 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | |
| 1627 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1628 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | DiagnosePropertyAccessorMismatch(property, GetterMethod, |
Fariborz Jahanian | fe9e394 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 1630 | property->getLocation()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | |
Fariborz Jahanian | eae373e | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1632 | if (SetterMethod) { |
Fariborz Jahanian | 1a5f292 | 2010-01-06 00:18:12 +0000 | [diff] [blame] | 1633 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 1634 | property->getPropertyAttributes(); |
| 1635 | if ((!(CAttr & ObjCPropertyDecl::OBJC_PR_readonly)) && |
| 1636 | Context.getCanonicalType(SetterMethod->getResultType()) != |
| 1637 | Context.VoidTy) |
Fariborz Jahanian | eae373e | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1638 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1639 | if (SetterMethod->param_size() != 1 || |
| 1640 | ((*SetterMethod->param_begin())->getType() != property->getType())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | Diag(property->getLocation(), |
| 1642 | diag::warn_accessor_property_type_mismatch) |
Fariborz Jahanian | eae373e | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1643 | << property->getDeclName() |
Ted Kremenek | 9a46804 | 2009-03-14 00:20:08 +0000 | [diff] [blame] | 1644 | << SetterMethod->getSelector(); |
Fariborz Jahanian | ff83998 | 2008-12-06 21:48:16 +0000 | [diff] [blame] | 1645 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 1646 | } |
Fariborz Jahanian | eae373e | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1647 | } |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1648 | |
| 1649 | // Synthesize getter/setter methods if none exist. |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1650 | // Find the default getter and if one not found, add one. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1651 | // FIXME: The synthesized property we set here is misleading. We almost always |
| 1652 | // synthesize these methods unless the user explicitly provided prototypes |
| 1653 | // (which is odd, but allowed). Sema should be typechecking that the |
| 1654 | // declarations jive in that situation (which it is not currently). |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1655 | if (!GetterMethod) { |
| 1656 | // No instance method of same name as property getter name was found. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | // Declare a getter method and add it to the list of methods |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1658 | // for this class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), |
| 1660 | property->getLocation(), property->getGetterName(), |
| 1661 | property->getType(), CD, true, false, true, |
| 1662 | (property->getPropertyImplementation() == |
| 1663 | ObjCPropertyDecl::Optional) ? |
| 1664 | ObjCMethodDecl::Optional : |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1665 | ObjCMethodDecl::Required); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1666 | CD->addDecl(GetterMethod); |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1667 | } else |
| 1668 | // A user declared getter will be synthesize when @synthesize of |
| 1669 | // the property with the same name is seen in the @implementation |
Steve Naroff | 04f2d14 | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 1670 | GetterMethod->setSynthesized(true); |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1671 | property->setGetterMethodDecl(GetterMethod); |
| 1672 | |
| 1673 | // Skip setter if property is read-only. |
| 1674 | if (!property->isReadOnly()) { |
| 1675 | // Find the default setter and if one not found, add one. |
| 1676 | if (!SetterMethod) { |
| 1677 | // No instance method of same name as property setter name was found. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | // Declare a setter method and add it to the list of methods |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1679 | // for this class. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), |
| 1681 | property->getLocation(), |
| 1682 | property->getSetterName(), |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1683 | Context.VoidTy, CD, true, false, true, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | (property->getPropertyImplementation() == |
| 1685 | ObjCPropertyDecl::Optional) ? |
| 1686 | ObjCMethodDecl::Optional : |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1687 | ObjCMethodDecl::Required); |
| 1688 | // Invent the arguments for the setter. We don't bother making a |
| 1689 | // nice name for the argument. |
| 1690 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | property->getLocation(), |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1692 | property->getIdentifier(), |
| 1693 | property->getType(), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1694 | /*TInfo=*/0, |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1695 | VarDecl::None, |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1696 | 0); |
Steve Naroff | 04f2d14 | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 1697 | SetterMethod->setMethodParams(Context, &Argument, 1); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1698 | CD->addDecl(SetterMethod); |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1699 | } else |
| 1700 | // A user declared setter will be synthesize when @synthesize of |
| 1701 | // the property with the same name is seen in the @implementation |
Steve Naroff | 04f2d14 | 2009-04-20 15:06:07 +0000 | [diff] [blame] | 1702 | SetterMethod->setSynthesized(true); |
Steve Naroff | e538c5f | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1703 | property->setSetterMethodDecl(SetterMethod); |
| 1704 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | // Add any synthesized methods to the global pool. This allows us to |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1706 | // handle the following, which is supported by GCC (and part of the design). |
| 1707 | // |
| 1708 | // @interface Foo |
| 1709 | // @property double bar; |
| 1710 | // @end |
| 1711 | // |
| 1712 | // void thisIsUnfortunate() { |
| 1713 | // id foo; |
| 1714 | // double bar = [foo bar]; |
| 1715 | // } |
| 1716 | // |
Douglas Gregor | 020713e | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1717 | if (GetterMethod) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1718 | AddInstanceMethodToGlobalPool(GetterMethod); |
Douglas Gregor | 020713e | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1719 | if (SetterMethod) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | AddInstanceMethodToGlobalPool(SetterMethod); |
Fariborz Jahanian | eae373e | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1723 | /// CompareMethodParamsInBaseAndSuper - This routine compares methods with |
| 1724 | /// identical selector names in current and its super classes and issues |
| 1725 | /// a warning if any of their argument types are incompatible. |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1726 | void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl, |
| 1727 | ObjCMethodDecl *Method, |
| 1728 | bool IsInstance) { |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1729 | ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl); |
| 1730 | if (ID == 0) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1732 | while (ObjCInterfaceDecl *SD = ID->getSuperClass()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1733 | ObjCMethodDecl *SuperMethodDecl = |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1734 | SD->lookupMethod(Method->getSelector(), IsInstance); |
| 1735 | if (SuperMethodDecl == 0) { |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1736 | ID = SD; |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1737 | continue; |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1738 | } |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1739 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 1740 | E = Method->param_end(); |
| 1741 | ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin(); |
| 1742 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1743 | // Number of parameters are the same and is guaranteed by selector match. |
| 1744 | assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch"); |
| 1745 | QualType T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1746 | QualType T2 = Context.getCanonicalType((*PrevI)->getType()); |
| 1747 | // If type of arguement of method in this class does not match its |
| 1748 | // respective argument type in the super class method, issue warning; |
| 1749 | if (!Context.typesAreCompatible(T1, T2)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super) |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1751 | << T1 << T2; |
| 1752 | Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration); |
| 1753 | return; |
| 1754 | } |
| 1755 | } |
| 1756 | ID = SD; |
| 1757 | } |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1760 | // Note: For class/category implemenations, allMethods/allProperties is |
| 1761 | // always null. |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1762 | void Sema::ActOnAtEnd(SourceRange AtEnd, |
| 1763 | DeclPtrTy classDecl, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1764 | DeclPtrTy *allMethods, unsigned allNum, |
| 1765 | DeclPtrTy *allProperties, unsigned pNum, |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1766 | DeclGroupPtrTy *allTUVars, unsigned tuvNum) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1767 | Decl *ClassDecl = classDecl.getAs<Decl>(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1768 | |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1769 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 1770 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1771 | // should be true. |
| 1772 | if (!ClassDecl) |
| 1773 | return; |
Fariborz Jahanian | 9290ede | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1774 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | bool isInterfaceDeclKind = |
Chris Lattner | 219b3e9 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1776 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 1777 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1778 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1779 | |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1780 | if (!isInterfaceDeclKind && AtEnd.isInvalid()) { |
| 1781 | // FIXME: This is wrong. We shouldn't be pretending that there is |
| 1782 | // an '@end' in the declaration. |
| 1783 | SourceLocation L = ClassDecl->getLocation(); |
| 1784 | AtEnd.setBegin(L); |
| 1785 | AtEnd.setEnd(L); |
| 1786 | Diag(L, diag::warn_missing_atend); |
Fariborz Jahanian | 9290ede | 2009-11-16 18:57:01 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1789 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
Steve Naroff | 35c62ae | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1790 | |
| 1791 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 1792 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 1793 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 1794 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1795 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1796 | ObjCMethodDecl *Method = |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1797 | cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>()); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1798 | |
| 1799 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1800 | if (Method->isInstanceMethod()) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1801 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1802 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1804 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1806 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1807 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1808 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1809 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1810 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1811 | DC->addDecl(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1812 | InsMap[Method->getSelector()] = Method; |
| 1813 | /// The following allows us to typecheck messages to "id". |
| 1814 | AddInstanceMethodToGlobalPool(Method); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | // verify that the instance method conforms to the same definition of |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1816 | // parent methods if it shadows one. |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1817 | CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1818 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1819 | } else { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1820 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1821 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1823 | : false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1824 | if ((isInterfaceDeclKind && PrevMethod && !match) |
Eli Friedman | 42b1e9e | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1825 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1826 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1827 | << Method->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1828 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1829 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1830 | DC->addDecl(Method); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1831 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | 1d2538c | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1832 | /// The following allows us to typecheck messages to "Class". |
| 1833 | AddFactoryMethodToGlobalPool(Method); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | // verify that the class method conforms to the same definition of |
Fariborz Jahanian | b61af4c | 2009-08-04 17:01:09 +0000 | [diff] [blame] | 1835 | // parent methods if it shadows one. |
Fariborz Jahanian | 10ff786 | 2009-08-04 01:07:16 +0000 | [diff] [blame] | 1836 | CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | } |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1840 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | // Compares properties declared in this class to those of its |
Fariborz Jahanian | 7cf1886 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 1842 | // super class. |
Fariborz Jahanian | 98a6c4f | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 1843 | ComparePropertiesInBaseAndSuper(I); |
Fariborz Jahanian | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 1844 | CompareProperties(I, DeclPtrTy::make(I)); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1845 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 62293f4 | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1846 | // Categories are used to extend the class by declaring new methods. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | // By the same token, they are also used to add new properties. No |
Fariborz Jahanian | 62293f4 | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1848 | // need to compare the added property to those in the class. |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1849 | |
Fariborz Jahanian | cdb8575 | 2010-01-18 18:41:16 +0000 | [diff] [blame] | 1850 | // Compare protocol properties with those in category |
| 1851 | CompareProperties(C, DeclPtrTy::make(C)); |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1852 | if (C->IsClassExtension()) |
Fariborz Jahanian | 33afd77 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1853 | DiagnoseClassExtensionDupMethods(C, C->getClassInterface()); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1854 | } |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1855 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 1856 | if (CDecl->getIdentifier()) |
| 1857 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 1858 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 1859 | // and adds them to the DeclContext and global method pools. |
| 1860 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 1861 | E = CDecl->prop_end(); |
| 1862 | I != E; ++I) |
| 1863 | ProcessPropertyDecl(*I, CDecl); |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1864 | CDecl->setAtEndRange(AtEnd); |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1865 | } |
| 1866 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1867 | IC->setAtEndRange(AtEnd); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1868 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1869 | ImplMethodsVsClassMethods(IC, IDecl); |
Fariborz Jahanian | 13e0c90 | 2009-11-11 22:40:11 +0000 | [diff] [blame] | 1870 | AtomicPropertySetterGetterRules(IC, IDecl); |
| 1871 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1872 | } else if (ObjCCategoryImplDecl* CatImplClass = |
Steve Naroff | b3a8798 | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1873 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Ted Kremenek | c7c6431 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 1874 | CatImplClass->setAtEndRange(AtEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1876 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | 4684f37 | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1877 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 41fd42e | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1878 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1879 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1880 | Categories; Categories = Categories->getNextClassCategory()) { |
| 1881 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
Chris Lattner | 9ef10f4 | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1882 | ImplMethodsVsClassMethods(CatImplClass, Categories); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1883 | break; |
| 1884 | } |
| 1885 | } |
| 1886 | } |
| 1887 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1888 | if (isInterfaceDeclKind) { |
| 1889 | // Reject invalid vardecls. |
| 1890 | for (unsigned i = 0; i != tuvNum; i++) { |
| 1891 | DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); |
| 1892 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 1893 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
Daniel Dunbar | 0ca1660 | 2009-04-14 02:25:56 +0000 | [diff] [blame] | 1894 | if (!VDecl->hasExternalStorage()) |
Steve Naroff | 42959b2 | 2009-04-13 17:58:46 +0000 | [diff] [blame] | 1895 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass); |
Fariborz Jahanian | 629aed9 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1896 | } |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1897 | } |
Fariborz Jahanian | 3654e65 | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 1898 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | |
| 1902 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 1903 | /// objective-c's type qualifier from the parser version of the same info. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | static Decl::ObjCDeclQualifier |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1905 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 1906 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 1907 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 1908 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 1909 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 1910 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 1911 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 1912 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 1913 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 1914 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 1915 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 1916 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 1917 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 1918 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1919 | |
| 1920 | return ret; |
| 1921 | } |
| 1922 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1923 | Sema::DeclPtrTy Sema::ActOnMethodDeclaration( |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1924 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1925 | tok::TokenKind MethodType, DeclPtrTy classDecl, |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1926 | ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1927 | Selector Sel, |
| 1928 | // optional arguments. The number of types/arguments is obtained |
| 1929 | // from the Sel.getNumArgs(). |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1930 | ObjCArgInfo *ArgInfo, |
Fariborz Jahanian | e84858c | 2009-01-09 00:38:19 +0000 | [diff] [blame] | 1931 | llvm::SmallVectorImpl<Declarator> &Cdecls, |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1932 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 1933 | bool isVariadic) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1934 | Decl *ClassDecl = classDecl.getAs<Decl>(); |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1935 | |
| 1936 | // Make sure we can establish a context for the method. |
| 1937 | if (!ClassDecl) { |
| 1938 | Diag(MethodLoc, diag::error_missing_method_context); |
Fariborz Jahanian | b1771e4 | 2009-08-28 17:52:37 +0000 | [diff] [blame] | 1939 | FunctionLabelMap.clear(); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1940 | return DeclPtrTy(); |
Steve Naroff | 83777fe | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1941 | } |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1942 | QualType resultDeclType; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1944 | if (ReturnType) { |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1945 | resultDeclType = GetTypeFromParser(ReturnType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1946 | |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1947 | // Methods cannot return interface types. All ObjC objects are |
| 1948 | // passed by reference. |
| 1949 | if (resultDeclType->isObjCInterfaceType()) { |
Chris Lattner | de5a531 | 2009-04-11 19:08:56 +0000 | [diff] [blame] | 1950 | Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value) |
| 1951 | << 0 << resultDeclType; |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1952 | return DeclPtrTy(); |
Steve Naroff | 3260641 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1953 | } |
| 1954 | } else // get the type for "id". |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1955 | resultDeclType = Context.getObjCIdType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1956 | |
| 1957 | ObjCMethodDecl* ObjCMethod = |
Chris Lattner | 8d8829e | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1958 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1959 | cast<DeclContext>(ClassDecl), |
Chris Lattner | 8d8829e | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1960 | MethodType == tok::minus, isVariadic, |
Fariborz Jahanian | 8983f17 | 2008-05-07 20:53:44 +0000 | [diff] [blame] | 1961 | false, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | MethodDeclKind == tok::objc_optional ? |
| 1963 | ObjCMethodDecl::Optional : |
Chris Lattner | 8d8829e | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1964 | ObjCMethodDecl::Required); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1966 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | |
Chris Lattner | 23b0faf | 2009-04-11 19:42:43 +0000 | [diff] [blame] | 1968 | for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1969 | QualType ArgType; |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1970 | TypeSourceInfo *DI; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1972 | if (ArgInfo[i].Type == 0) { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1973 | ArgType = Context.getObjCIdType(); |
| 1974 | DI = 0; |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1975 | } else { |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1976 | ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI); |
Steve Naroff | b0498ee | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1977 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1978 | ArgType = adjustParameterType(ArgType); |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1979 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1980 | |
John McCall | 856bbea | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 1981 | ParmVarDecl* Param |
| 1982 | = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc, |
| 1983 | ArgInfo[i].Name, ArgType, DI, |
| 1984 | VarDecl::None, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1986 | if (ArgType->isObjCInterfaceType()) { |
| 1987 | Diag(ArgInfo[i].NameLoc, |
| 1988 | diag::err_object_cannot_be_passed_returned_by_value) |
| 1989 | << 1 << ArgType; |
| 1990 | Param->setInvalidDecl(); |
| 1991 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1993 | Param->setObjCDeclQualifier( |
Chris Lattner | d8626fd | 2009-04-11 18:57:04 +0000 | [diff] [blame] | 1994 | CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
Chris Lattner | 9713a1c | 2009-04-11 19:34:56 +0000 | [diff] [blame] | 1996 | // Apply the attributes to the parameter. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1997 | ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Chris Lattner | c5ffed4 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1999 | Params.push_back(Param); |
| 2000 | } |
| 2001 | |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2002 | ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs()); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2003 | ObjCMethod->setObjCDeclQualifier( |
| 2004 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 2005 | const ObjCMethodDecl *PrevMethod = 0; |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 2006 | |
| 2007 | if (AttrList) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2008 | ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2010 | const ObjCMethodDecl *InterfaceMD = 0; |
| 2011 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | // For implementations (which can be very "coarse grain"), we add the |
| 2013 | // method now. This allows the AST to implement lookup methods that work |
| 2014 | // incrementally (without waiting until we parse the @end). It also allows |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2015 | // us to flag multiple declaration errors as they occur. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 8d8829e | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 2017 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2018 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2019 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
| 2020 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2021 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2022 | PrevMethod = ImpDecl->getClassMethod(Sel); |
| 2023 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2024 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2025 | InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel, |
| 2026 | MethodType == tok::minus); |
Fariborz Jahanian | 2bd617c | 2009-05-12 21:36:23 +0000 | [diff] [blame] | 2027 | if (AttrList) |
| 2028 | Diag(EndLoc, diag::warn_attribute_method_def); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | } else if (ObjCCategoryImplDecl *CatImpDecl = |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2030 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2031 | if (MethodType == tok::minus) { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2032 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
| 2033 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2034 | } else { |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2035 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
| 2036 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2037 | } |
Fariborz Jahanian | 2bd617c | 2009-05-12 21:36:23 +0000 | [diff] [blame] | 2038 | if (AttrList) |
| 2039 | Diag(EndLoc, diag::warn_attribute_method_def); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2040 | } |
| 2041 | if (PrevMethod) { |
| 2042 | // You can never have two method definitions with the same name. |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2043 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | e4b9569 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2044 | << ObjCMethod->getDeclName(); |
Chris Lattner | 0369c57 | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 2045 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2046 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2047 | |
| 2048 | // If the interface declared this method, and it was deprecated there, |
| 2049 | // mark it deprecated here. |
| 2050 | if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>()) |
| 2051 | ObjCMethod->addAttr(::new (Context) DeprecatedAttr()); |
| 2052 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2053 | return DeclPtrTy::make(ObjCMethod); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2057 | SourceLocation Loc, |
| 2058 | unsigned &Attributes) { |
| 2059 | // FIXME: Improve the reported location. |
| 2060 | |
Fariborz Jahanian | 5a3422f | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 2061 | // readonly and readwrite/assign/retain/copy conflict. |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2062 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
Fariborz Jahanian | 5a3422f | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 2063 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 2064 | ObjCDeclSpec::DQ_PR_assign | |
| 2065 | ObjCDeclSpec::DQ_PR_copy | |
| 2066 | ObjCDeclSpec::DQ_PR_retain))) { |
| 2067 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? |
| 2068 | "readwrite" : |
| 2069 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
| 2070 | "assign" : |
| 2071 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
| 2072 | "copy" : "retain"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | |
| 2074 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? |
Chris Lattner | 409f555 | 2009-01-29 18:49:48 +0000 | [diff] [blame] | 2075 | diag::err_objc_property_attr_mutually_exclusive : |
| 2076 | diag::warn_objc_property_attr_mutually_exclusive) |
Fariborz Jahanian | 5a3422f | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 2077 | << "readonly" << which; |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | // Check for copy or retain on non-object types. |
| 2081 | if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | !PropertyTy->isObjCObjectPointerType() && |
| 2083 | !PropertyTy->isBlockPointerType() && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 2084 | !Context.isObjCNSObjectType(PropertyTy)) { |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2085 | Diag(Loc, diag::err_objc_property_requires_object) |
| 2086 | << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2087 | Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); |
| 2088 | } |
| 2089 | |
| 2090 | // Check for more than one of { assign, copy, retain }. |
| 2091 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 2092 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2093 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2094 | << "assign" << "copy"; |
| 2095 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2096 | } |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2097 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2098 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2099 | << "assign" << "retain"; |
| 2100 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2101 | } |
| 2102 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 2103 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2104 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 2105 | << "copy" << "retain"; |
| 2106 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | // Warn if user supplied no assignment attribute, property is |
| 2111 | // readwrite, and this is an object type. |
| 2112 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | |
| 2113 | ObjCDeclSpec::DQ_PR_retain)) && |
| 2114 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 2115 | PropertyTy->isObjCObjectPointerType()) { |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2116 | // Skip this warning in gc-only mode. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | if (getLangOptions().getGCMode() != LangOptions::GCOnly) |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2118 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
| 2119 | |
| 2120 | // If non-gc code warn that this is likely inappropriate. |
| 2121 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 2122 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2124 | // FIXME: Implement warning dependent on NSCopying being |
| 2125 | // implemented. See also: |
| 2126 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 2127 | // (please trim this list while you are at it). |
| 2128 | } |
Mike Stump | 5580bdc | 2009-05-07 23:06:50 +0000 | [diff] [blame] | 2129 | |
| 2130 | if (!(Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 2131 | && getLangOptions().getGCMode() == LangOptions::GCOnly |
| 2132 | && PropertyTy->isBlockPointerType()) |
| 2133 | Diag(Loc, diag::warn_objc_property_copy_missing_on_block); |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2137 | FieldDeclarator &FD, |
| 2138 | ObjCDeclSpec &ODS, |
| 2139 | Selector GetterSel, |
| 2140 | Selector SetterSel, |
| 2141 | DeclPtrTy ClassCategory, |
| 2142 | bool *isOverridingProperty, |
| 2143 | tok::ObjCKeywordKind MethodImplKind) { |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2144 | unsigned Attributes = ODS.getPropertyAttributes(); |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2145 | bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || |
| 2146 | // default is readwrite! |
| 2147 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | // property is defaulted to 'assign' if it is readwrite and is |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2149 | // not retain or copy |
| 2150 | bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2151 | (isReadWrite && |
| 2152 | !(Attributes & ObjCDeclSpec::DQ_PR_retain) && |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2153 | !(Attributes & ObjCDeclSpec::DQ_PR_copy))); |
| 2154 | QualType T = GetTypeForDeclarator(FD.D, S); |
Fariborz Jahanian | 00857fc | 2009-12-16 18:03:30 +0000 | [diff] [blame] | 2155 | if (T->isReferenceType()) { |
| 2156 | Diag(AtLoc, diag::error_reference_property); |
| 2157 | return DeclPtrTy(); |
| 2158 | } |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2159 | Decl *ClassDecl = ClassCategory.getAs<Decl>(); |
Fariborz Jahanian | 38a5c96 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2160 | ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2161 | // May modify Attributes. |
| 2162 | CheckObjCPropertyAttributes(T, AtLoc, Attributes); |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2163 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2164 | if (CDecl->IsClassExtension()) { |
| 2165 | // Diagnose if this property is already in continuation class. |
| 2166 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
| 2167 | assert(DC && "ClassDecl is not a DeclContext"); |
| 2168 | DeclContext::lookup_result Found = DC->lookup(FD.D.getIdentifier()); |
| 2169 | if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) { |
| 2170 | Diag(AtLoc, diag::err_duplicate_property); |
| 2171 | Diag((*Found.first)->getLocation(), diag::note_property_declare); |
| 2172 | return DeclPtrTy(); |
| 2173 | } |
| 2174 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
| 2175 | FD.D.getIdentifierLoc(), |
| 2176 | FD.D.getIdentifier(), |
| 2177 | AtLoc, T); |
| 2178 | DC->addDecl(PDecl); |
| 2179 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2180 | // This is a continuation class. property requires special |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2181 | // handling. |
Fariborz Jahanian | 38a5c96 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2182 | if ((CCPrimary = CDecl->getClassInterface())) { |
| 2183 | // Find the property in continuation class's primary class only. |
Fariborz Jahanian | 38a5c96 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2184 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
Fariborz Jahanian | de8db16 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 2185 | if (ObjCPropertyDecl *PIDecl = |
| 2186 | CCPrimary->FindPropertyVisibleInPrimaryClass(PropertyId)) { |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2187 | // property 'PIDecl's readonly attribute will be over-ridden |
Fariborz Jahanian | e4fd640 | 2009-04-01 23:23:53 +0000 | [diff] [blame] | 2188 | // with continuation class's readwrite property attribute! |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2189 | unsigned PIkind = PIDecl->getPropertyAttributes(); |
| 2190 | if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { |
Fariborz Jahanian | dc21610 | 2009-11-10 19:31:09 +0000 | [diff] [blame] | 2191 | unsigned retainCopyNonatomic = |
Fariborz Jahanian | 3600f41 | 2009-11-06 22:59:12 +0000 | [diff] [blame] | 2192 | (ObjCPropertyDecl::OBJC_PR_retain | |
Fariborz Jahanian | a386d95 | 2009-11-03 00:01:38 +0000 | [diff] [blame] | 2193 | ObjCPropertyDecl::OBJC_PR_copy | |
| 2194 | ObjCPropertyDecl::OBJC_PR_nonatomic); |
Fariborz Jahanian | dc21610 | 2009-11-10 19:31:09 +0000 | [diff] [blame] | 2195 | if ((Attributes & retainCopyNonatomic) != |
| 2196 | (PIkind & retainCopyNonatomic)) { |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2197 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
Fariborz Jahanian | a386d95 | 2009-11-03 00:01:38 +0000 | [diff] [blame] | 2198 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 2199 | } |
Fariborz Jahanian | 8e356bf | 2010-01-06 21:38:30 +0000 | [diff] [blame] | 2200 | DeclContext *DC = dyn_cast<DeclContext>(CCPrimary); |
| 2201 | assert(DC && "ClassDecl is not a DeclContext"); |
| 2202 | DeclContext::lookup_result Found = |
| 2203 | DC->lookup(PIDecl->getDeclName()); |
| 2204 | bool PropertyInPrimaryClass = false; |
| 2205 | for (; Found.first != Found.second; ++Found.first) |
| 2206 | if (isa<ObjCPropertyDecl>(*Found.first)) { |
| 2207 | PropertyInPrimaryClass = true; |
| 2208 | break; |
| 2209 | } |
| 2210 | if (!PropertyInPrimaryClass) { |
| 2211 | // Protocol is not in the primary class. Must build one for it. |
| 2212 | ObjCDeclSpec ProtocolPropertyODS; |
| 2213 | // FIXME. Assuming that ObjCDeclSpec::ObjCPropertyAttributeKind and |
| 2214 | // ObjCPropertyDecl::PropertyAttributeKind have identical values. |
| 2215 | // Should consolidate both into one enum type. |
| 2216 | ProtocolPropertyODS.setPropertyAttributes( |
| 2217 | (ObjCDeclSpec::ObjCPropertyAttributeKind)PIkind); |
| 2218 | DeclPtrTy ProtocolPtrTy = |
| 2219 | ActOnProperty(S, AtLoc, FD, ProtocolPropertyODS, |
| 2220 | PIDecl->getGetterName(), |
| 2221 | PIDecl->getSetterName(), |
| 2222 | DeclPtrTy::make(CCPrimary), isOverridingProperty, |
| 2223 | MethodImplKind); |
| 2224 | PIDecl = ProtocolPtrTy.getAs<ObjCPropertyDecl>(); |
| 2225 | } |
Fariborz Jahanian | 38a5c96 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2226 | PIDecl->makeitReadWriteAttribute(); |
| 2227 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 2228 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 2229 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 2230 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 2231 | PIDecl->setSetterName(SetterSel); |
Fariborz Jahanian | de8db16 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 2232 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | Diag(AtLoc, diag::err_use_continuation_class) |
Fariborz Jahanian | 38a5c96 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2234 | << CCPrimary->getDeclName(); |
Fariborz Jahanian | de8db16 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 2235 | Diag(PIDecl->getLocation(), diag::note_property_declare); |
| 2236 | } |
Fariborz Jahanian | 38a5c96 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2237 | *isOverridingProperty = true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | // Make sure setter decl is synthesized, and added to primary |
Fariborz Jahanian | 47b2108 | 2009-04-15 19:19:03 +0000 | [diff] [blame] | 2239 | // class's list. |
| 2240 | ProcessPropertyDecl(PIDecl, CCPrimary); |
Fariborz Jahanian | 38a5c96 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2241 | return DeclPtrTy(); |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2242 | } |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2243 | |
Fariborz Jahanian | 38a5c96 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2244 | // No matching property found in the primary class. Just fall thru |
| 2245 | // and add property to continuation class's primary class. |
| 2246 | ClassDecl = CCPrimary; |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2247 | } else { |
Chris Lattner | 68d42c7 | 2009-02-20 21:38:52 +0000 | [diff] [blame] | 2248 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
| 2249 | *isOverridingProperty = true; |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2250 | return DeclPtrTy(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | } |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2252 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2254 | // Issue a warning if property is 'assign' as default and its object, which is |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | // gc'able conforms to NSCopying protocol |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2256 | if (getLangOptions().getGCMode() != LangOptions::NonGC && |
| 2257 | isAssign && !(Attributes & ObjCDeclSpec::DQ_PR_assign)) |
| 2258 | if (T->isObjCObjectPointerType()) { |
| 2259 | QualType InterfaceTy = T->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | if (const ObjCInterfaceType *OIT = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2261 | InterfaceTy->getAs<ObjCInterfaceType>()) { |
Fariborz Jahanian | 83b000c | 2009-08-14 18:06:25 +0000 | [diff] [blame] | 2262 | ObjCInterfaceDecl *IDecl = OIT->getDecl(); |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2263 | if (IDecl) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | if (ObjCProtocolDecl* PNSCopying = |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2265 | LookupProtocol(&Context.Idents.get("NSCopying"))) |
| 2266 | if (IDecl->ClassImplementsProtocol(PNSCopying, true)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | Diag(AtLoc, diag::warn_implements_nscopying) |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2268 | << FD.D.getIdentifier(); |
Fariborz Jahanian | 83b000c | 2009-08-14 18:06:25 +0000 | [diff] [blame] | 2269 | } |
Fariborz Jahanian | 3f8917a | 2009-08-11 22:02:25 +0000 | [diff] [blame] | 2270 | } |
Fariborz Jahanian | 887cd6a | 2009-08-12 18:17:53 +0000 | [diff] [blame] | 2271 | if (T->isObjCInterfaceType()) |
| 2272 | Diag(FD.D.getIdentifierLoc(), diag::err_statically_allocated_object); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | |
Steve Naroff | ba3dc38 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 2274 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
| 2275 | assert(DC && "ClassDecl is not a DeclContext"); |
Chris Lattner | 5300acc | 2009-04-11 20:14:49 +0000 | [diff] [blame] | 2276 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2277 | FD.D.getIdentifierLoc(), |
Fariborz Jahanian | da8ec2b | 2010-01-21 17:36:00 +0000 | [diff] [blame] | 2278 | FD.D.getIdentifier(), |
| 2279 | AtLoc, T); |
Fariborz Jahanian | 057a17e | 2009-12-17 00:49:09 +0000 | [diff] [blame] | 2280 | DeclContext::lookup_result Found = DC->lookup(PDecl->getDeclName()); |
| 2281 | if (Found.first != Found.second && isa<ObjCPropertyDecl>(*Found.first)) { |
| 2282 | Diag(PDecl->getLocation(), diag::err_duplicate_property); |
| 2283 | Diag((*Found.first)->getLocation(), diag::note_property_declare); |
| 2284 | PDecl->setInvalidDecl(); |
| 2285 | } |
| 2286 | else |
| 2287 | DC->addDecl(PDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | |
Chris Lattner | 5300acc | 2009-04-11 20:14:49 +0000 | [diff] [blame] | 2289 | if (T->isArrayType() || T->isFunctionType()) { |
| 2290 | Diag(AtLoc, diag::err_property_type) << T; |
| 2291 | PDecl->setInvalidDecl(); |
| 2292 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2294 | ProcessDeclAttributes(S, PDecl, FD.D); |
Douglas Gregor | c25d7a7 | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 2295 | |
Fariborz Jahanian | ec6e4c8 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 2296 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 2297 | // selector names in anticipation of declaration of setter/getter methods. |
| 2298 | PDecl->setGetterName(GetterSel); |
| 2299 | PDecl->setSetterName(SetterSel); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2300 | |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2301 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2302 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2304 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2305 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2306 | |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2307 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2308 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2309 | |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2310 | if (isReadWrite) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2311 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2313 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2314 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2315 | |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2316 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2317 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Fariborz Jahanian | f8ef9f3 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 2319 | if (isAssign) |
| 2320 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | |
Daniel Dunbar | e8a06e6 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 2322 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 2323 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | |
Fariborz Jahanian | 8d91686 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 2325 | if (MethodImplKind == tok::objc_required) |
| 2326 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 2327 | else if (MethodImplKind == tok::objc_optional) |
| 2328 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
Fariborz Jahanian | 38a5c96 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 2329 | // A case of continuation class adding a new property in the class. This |
| 2330 | // is not what it was meant for. However, gcc supports it and so should we. |
| 2331 | // Make sure setter/getters are declared here. |
| 2332 | if (CCPrimary) |
| 2333 | ProcessPropertyDecl(PDecl, CCPrimary); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2335 | return DeclPtrTy::make(PDecl); |
Chris Lattner | da463fe | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
Fariborz Jahanian | 5db5281 | 2010-02-09 21:49:50 +0000 | [diff] [blame] | 2338 | ObjCIvarDecl* |
| 2339 | Sema::SynthesizeNewPropertyIvar(ObjCInterfaceDecl *IDecl, |
| 2340 | IdentifierInfo *NameII) { |
| 2341 | ObjCIvarDecl *Ivar = 0; |
| 2342 | ObjCPropertyDecl *Prop = LookupPropertyDecl(IDecl, NameII); |
| 2343 | if (Prop && !Prop->isInvalidDecl()) { |
| 2344 | DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl); |
| 2345 | QualType PropType = Context.getCanonicalType(Prop->getType()); |
| 2346 | assert(EnclosingContext && |
| 2347 | "null DeclContext for synthesized ivar - SynthesizeNewPropertyIvar"); |
| 2348 | Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, |
| 2349 | Prop->getLocation(), |
| 2350 | NameII, PropType, /*Dinfo=*/0, |
| 2351 | ObjCIvarDecl::Public, |
| 2352 | (Expr *)0); |
| 2353 | Ivar->setLexicalDeclContext(IDecl); |
| 2354 | IDecl->addDecl(Ivar); |
| 2355 | Prop->setPropertyIvarDecl(Ivar); |
| 2356 | } |
| 2357 | return Ivar; |
| 2358 | } |
| 2359 | |
Fariborz Jahanian | a054e99 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2360 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 2361 | /// builds the AST node for a property implementation declaration; declared |
| 2362 | /// as @synthesize or @dynamic. |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2363 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2365 | SourceLocation PropertyLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | bool Synthesize, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2367 | DeclPtrTy ClassCatImpDecl, |
| 2368 | IdentifierInfo *PropertyId, |
| 2369 | IdentifierInfo *PropertyIvar) { |
| 2370 | Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>(); |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2371 | // Make sure we have a context for the property implementation declaration. |
| 2372 | if (!ClassImpDecl) { |
| 2373 | Diag(AtLoc, diag::error_missing_property_context); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2374 | return DeclPtrTy(); |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2375 | } |
| 2376 | ObjCPropertyDecl *property = 0; |
| 2377 | ObjCInterfaceDecl* IDecl = 0; |
| 2378 | // Find the class or category class where this property must have |
| 2379 | // a declaration. |
Fariborz Jahanian | 6efdf1d | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 2380 | ObjCImplementationDecl *IC = 0; |
| 2381 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 2382 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 2383 | IDecl = IC->getClassInterface(); |
Fariborz Jahanian | f2a7d7c | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2384 | // We always synthesize an interface for an implementation |
| 2385 | // without an interface decl. So, IDecl is always non-zero. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | assert(IDecl && |
Fariborz Jahanian | f2a7d7c | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2387 | "ActOnPropertyImplDecl - @implementation without @interface"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2388 | |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2389 | // Look for this property declaration in the @implementation's @interface |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2390 | property = IDecl->FindPropertyDeclaration(PropertyId); |
Fariborz Jahanian | a054e99 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2391 | if (!property) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2392 | Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2393 | return DeclPtrTy(); |
Fariborz Jahanian | a054e99 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2394 | } |
Fariborz Jahanian | ec344ed | 2009-11-02 18:45:36 +0000 | [diff] [blame] | 2395 | if (const ObjCCategoryDecl *CD = |
| 2396 | dyn_cast<ObjCCategoryDecl>(property->getDeclContext())) { |
Fariborz Jahanian | 30a4292 | 2010-02-15 21:55:26 +0000 | [diff] [blame] | 2397 | if (!CD->IsClassExtension()) { |
Fariborz Jahanian | ec344ed | 2009-11-02 18:45:36 +0000 | [diff] [blame] | 2398 | Diag(PropertyLoc, diag::error_category_property) << CD->getDeclName(); |
Fariborz Jahanian | de8db16 | 2009-11-02 22:45:15 +0000 | [diff] [blame] | 2399 | Diag(property->getLocation(), diag::note_property_declare); |
Fariborz Jahanian | ec344ed | 2009-11-02 18:45:36 +0000 | [diff] [blame] | 2400 | return DeclPtrTy(); |
| 2401 | } |
| 2402 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2403 | } else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
Fariborz Jahanian | f2a7d7c | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2404 | if (Synthesize) { |
| 2405 | Diag(AtLoc, diag::error_synthesize_category_decl); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2406 | return DeclPtrTy(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | } |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2408 | IDecl = CatImplClass->getClassInterface(); |
| 2409 | if (!IDecl) { |
| 2410 | Diag(AtLoc, diag::error_missing_property_interface); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2411 | return DeclPtrTy(); |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2412 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | ObjCCategoryDecl *Category = |
Fariborz Jahanian | a054e99 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2414 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2416 | // If category for this implementation not found, it is an error which |
| 2417 | // has already been reported eralier. |
Fariborz Jahanian | a054e99 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2418 | if (!Category) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2419 | return DeclPtrTy(); |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2420 | // Look for this property declaration in @implementation's category |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2421 | property = Category->FindPropertyDeclaration(PropertyId); |
Fariborz Jahanian | a054e99 | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 2422 | if (!property) { |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2423 | Diag(PropertyLoc, diag::error_bad_category_property_decl) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2424 | << Category->getDeclName(); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2425 | return DeclPtrTy(); |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2426 | } |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2427 | } else { |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2428 | Diag(AtLoc, diag::error_bad_property_context); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2429 | return DeclPtrTy(); |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2430 | } |
Fariborz Jahanian | 6efdf1d | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 2431 | ObjCIvarDecl *Ivar = 0; |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2432 | // Check that we have a valid, previously declared ivar for @synthesize |
| 2433 | if (Synthesize) { |
| 2434 | // @synthesize |
Fariborz Jahanian | c6bec7b | 2008-04-21 21:57:36 +0000 | [diff] [blame] | 2435 | if (!PropertyIvar) |
| 2436 | PropertyIvar = PropertyId; |
Fariborz Jahanian | b35b4a9 | 2009-03-31 00:06:29 +0000 | [diff] [blame] | 2437 | QualType PropType = Context.getCanonicalType(property->getType()); |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2438 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
Fariborz Jahanian | b6d5b54 | 2009-04-13 19:30:37 +0000 | [diff] [blame] | 2439 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2440 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar, ClassDeclared); |
Fariborz Jahanian | f2a7d7c | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2441 | if (!Ivar) { |
Fariborz Jahanian | 28c5a8b | 2009-06-06 16:36:41 +0000 | [diff] [blame] | 2442 | DeclContext *EnclosingContext = cast_or_null<DeclContext>(IDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | assert(EnclosingContext && |
Fariborz Jahanian | 28c5a8b | 2009-06-06 16:36:41 +0000 | [diff] [blame] | 2444 | "null DeclContext for synthesized ivar - ActOnPropertyImplDecl"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2445 | Ivar = ObjCIvarDecl::Create(Context, EnclosingContext, PropertyLoc, |
| 2446 | PropertyIvar, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2447 | ObjCIvarDecl::Public, |
| 2448 | (Expr *)0); |
Fariborz Jahanian | aef6622 | 2010-02-19 00:31:17 +0000 | [diff] [blame] | 2449 | IDecl->makeDeclVisibleInContext(Ivar, false); |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2450 | property->setPropertyIvarDecl(Ivar); |
| 2451 | if (!getLangOptions().ObjCNonFragileABI) |
Steve Naroff | c03f6b9 | 2009-03-03 22:09:41 +0000 | [diff] [blame] | 2452 | Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2453 | // Note! I deliberately want it to fall thru so, we have a |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2454 | // a property implementation and to avoid future warnings. |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2455 | } else if (getLangOptions().ObjCNonFragileABI && |
| 2456 | ClassDeclared != IDecl) { |
Fariborz Jahanian | 68592fc | 2009-04-30 21:39:24 +0000 | [diff] [blame] | 2457 | Diag(PropertyLoc, diag::error_ivar_in_superclass_use) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2458 | << property->getDeclName() << Ivar->getDeclName() |
Fariborz Jahanian | b6d5b54 | 2009-04-13 19:30:37 +0000 | [diff] [blame] | 2459 | << ClassDeclared->getDeclName(); |
| 2460 | Diag(Ivar->getLocation(), diag::note_previous_access_declaration) |
| 2461 | << Ivar << Ivar->getNameAsCString(); |
| 2462 | // Note! I deliberately want it to fall thru so more errors are caught. |
| 2463 | } |
Steve Naroff | 9f9774c | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 2464 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | |
Steve Naroff | 506e717 | 2008-09-30 00:24:17 +0000 | [diff] [blame] | 2466 | // Check that type of property and its ivar are type compatible. |
Steve Naroff | 9f9774c | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 2467 | if (PropType != IvarType) { |
Steve Naroff | 9546eee | 2008-10-16 14:59:30 +0000 | [diff] [blame] | 2468 | if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { |
Chris Lattner | f7e3f6d | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 2469 | Diag(PropertyLoc, diag::error_property_ivar_type) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2470 | << property->getDeclName() << Ivar->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2471 | // Note! I deliberately want it to fall thru so, we have a |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2472 | // a property implementation and to avoid future warnings. |
Steve Naroff | 9f9774c | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 2473 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2475 | // FIXME! Rules for properties are somewhat different that those |
| 2476 | // for assignments. Use a new routine to consolidate all cases; |
| 2477 | // specifically for property redeclarations as well as for ivars. |
| 2478 | QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType(); |
| 2479 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2480 | if (lhsType != rhsType && |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2481 | lhsType->isArithmeticType()) { |
| 2482 | Diag(PropertyLoc, diag::error_property_ivar_type) |
| 2483 | << property->getDeclName() << Ivar->getDeclName(); |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2484 | // Fall thru - see previous comment |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2485 | } |
| 2486 | // __weak is explicit. So it works on Canonical type. |
Fariborz Jahanian | 9ecb84b | 2009-04-07 21:25:06 +0000 | [diff] [blame] | 2487 | if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak() && |
| 2488 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2489 | Diag(PropertyLoc, diag::error_weak_property) |
| 2490 | << property->getDeclName() << Ivar->getDeclName(); |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2491 | // Fall thru - see previous comment |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2492 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | if ((property->getType()->isObjCObjectPointerType() || |
Fariborz Jahanian | 60331be | 2009-04-10 22:42:54 +0000 | [diff] [blame] | 2494 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak() && |
| 2495 | getLangOptions().getGCMode() != LangOptions::NonGC) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2496 | Diag(PropertyLoc, diag::error_strong_property) |
| 2497 | << property->getDeclName() << Ivar->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | // Fall thru - see previous comment |
Fariborz Jahanian | 54fa418 | 2009-01-19 20:13:47 +0000 | [diff] [blame] | 2499 | } |
Fariborz Jahanian | f2a7d7c | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 2500 | } |
Fariborz Jahanian | 6c6aea9 | 2009-04-14 23:15:21 +0000 | [diff] [blame] | 2501 | } else if (PropertyIvar) |
| 2502 | // @dynamic |
| 2503 | Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2504 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2505 | ObjCPropertyImplDecl *PIDecl = |
| 2506 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 2507 | property, |
| 2508 | (Synthesize ? |
| 2509 | ObjCPropertyImplDecl::Synthesize |
Daniel Dunbar | 3b4fdb0 | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 2510 | : ObjCPropertyImplDecl::Dynamic), |
| 2511 | Ivar); |
Fariborz Jahanian | fbbaf6a | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2512 | if (IC) { |
| 2513 | if (Synthesize) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2514 | if (ObjCPropertyImplDecl *PPIDecl = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2515 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2516 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 2517 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
Fariborz Jahanian | fbbaf6a | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2518 | << PropertyIvar; |
| 2519 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 2520 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | |
| 2522 | if (ObjCPropertyImplDecl *PPIDecl |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2523 | = IC->FindPropertyImplDecl(PropertyId)) { |
Fariborz Jahanian | fbbaf6a | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2524 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 2525 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2526 | return DeclPtrTy(); |
Fariborz Jahanian | fbbaf6a | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2527 | } |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2528 | IC->addPropertyImplementation(PIDecl); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2529 | } else { |
Fariborz Jahanian | fbbaf6a | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2530 | if (Synthesize) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | if (ObjCPropertyImplDecl *PPIDecl = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2532 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2533 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 2534 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
Fariborz Jahanian | fbbaf6a | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2535 | << PropertyIvar; |
| 2536 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 2537 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | |
| 2539 | if (ObjCPropertyImplDecl *PPIDecl = |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2540 | CatImplClass->FindPropertyImplDecl(PropertyId)) { |
Fariborz Jahanian | fbbaf6a | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2541 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 2542 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2543 | return DeclPtrTy(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | } |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2545 | CatImplClass->addPropertyImplementation(PIDecl); |
Fariborz Jahanian | fbbaf6a | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 2546 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2548 | return DeclPtrTy::make(PIDecl); |
Fariborz Jahanian | ffe97a3 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 2549 | } |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2550 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2551 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Douglas Gregor | 6ad0ef5 | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 2552 | if (isa<TranslationUnitDecl>(CurContext->getLookupContext())) |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2553 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2555 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 2556 | D->setInvalidDecl(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | |
Anders Carlsson | a6b508a | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 2558 | return true; |
| 2559 | } |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2560 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2561 | /// Called whenever @defs(ClassName) is encountered in the source. Inserts the |
| 2562 | /// instance variables of ClassName into Decls. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2564 | IdentifierInfo *ClassName, |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2565 | llvm::SmallVectorImpl<DeclPtrTy> &Decls) { |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2566 | // Check that ClassName is a valid class |
| 2567 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); |
| 2568 | if (!Class) { |
| 2569 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 2570 | return; |
| 2571 | } |
Fariborz Jahanian | ece1b2b | 2009-04-21 20:28:41 +0000 | [diff] [blame] | 2572 | if (LangOpts.ObjCNonFragileABI) { |
| 2573 | Diag(DeclStart, diag::err_atdef_nonfragile_interface); |
| 2574 | return; |
| 2575 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2577 | // Collect the instance variables |
Fariborz Jahanian | 7dae114 | 2009-06-04 17:08:55 +0000 | [diff] [blame] | 2578 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
| 2579 | Context.CollectObjCIvars(Class, RecFields); |
| 2580 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
| 2581 | for (unsigned i = 0; i < RecFields.size(); i++) { |
| 2582 | FieldDecl* ID = RecFields[i]; |
| 2583 | RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()); |
| 2584 | Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(), |
| 2585 | ID->getIdentifier(), ID->getType(), |
| 2586 | ID->getBitWidth()); |
| 2587 | Decls.push_back(Sema::DeclPtrTy::make(FD)); |
| 2588 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2589 | |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2590 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2591 | for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin(); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2592 | D != Decls.end(); ++D) { |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2593 | FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>()); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2594 | if (getLangOptions().CPlusPlus) |
| 2595 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 2596 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>())) |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2597 | Record->addDecl(FD); |
Chris Lattner | 438e501 | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | |