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