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