Chris Lattner | 855e51f | 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 | 959e5be | 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 | 855e51f | 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" |
Steve Naroff | 58d0d6f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | 8d03cbe | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Steve Naroff | 8df4602 | 2009-02-28 16:59:13 +0000 | [diff] [blame] | 21 | /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 22 | /// and user declared, in the method definition's AST. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 23 | void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 24 | assert(getCurMethodDecl() == 0 && "Method parsing confused"); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 25 | ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>()); |
Steve Naroff | 3ac43f9 | 2008-07-25 17:57:26 +0000 | [diff] [blame] | 26 | |
| 27 | // If we don't have a valid method decl, simply return. |
| 28 | if (!MDecl) |
| 29 | return; |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 30 | |
| 31 | // Allow the rest of sema to find private method decl implementations. |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 32 | if (MDecl->isInstanceMethod()) |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 33 | AddInstanceMethodToGlobalPool(MDecl); |
| 34 | else |
| 35 | AddFactoryMethodToGlobalPool(MDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 36 | |
| 37 | // Allow all of Sema to see that we are entering a method definition. |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 38 | PushDeclContext(FnBodyScope, MDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 39 | |
| 40 | // Create Decl objects for each parameter, entrring them in the scope for |
| 41 | // binding to their use. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 42 | |
| 43 | // Insert the invisible arguments, self and _cmd! |
Fariborz Jahanian | 91dd9d3 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 44 | MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 45 | |
Daniel Dunbar | eaf91c3 | 2008-08-26 06:07:48 +0000 | [diff] [blame] | 46 | PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| 47 | PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 97316c0 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 49 | // Introduce all of the other parameters into this scope. |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 50 | for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(), |
| 51 | E = MDecl->param_end(); PI != E; ++PI) |
| 52 | if ((*PI)->getIdentifier()) |
| 53 | PushOnScopeChains(*PI, FnBodyScope); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 56 | Sema::DeclPtrTy Sema:: |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 57 | ActOnStartClassInterface(SourceLocation AtInterfaceLoc, |
| 58 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 59 | IdentifierInfo *SuperName, SourceLocation SuperLoc, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 60 | const DeclPtrTy *ProtoRefs, unsigned NumProtoRefs, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 61 | SourceLocation EndProtoLoc, AttributeList *AttrList) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 62 | assert(ClassName && "Missing class identifier"); |
| 63 | |
| 64 | // Check for another declaration kind with the same name. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 65 | NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName); |
Douglas Gregor | 2715a1f | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 66 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 67 | // Maybe we will complain about the shadowed template parameter. |
| 68 | DiagnoseTemplateParameterShadow(ClassLoc, PrevDecl); |
| 69 | // Just pretend that we didn't see the previous declaration. |
| 70 | PrevDecl = 0; |
| 71 | } |
| 72 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 73 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 74 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 75 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 78 | ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 79 | if (IDecl) { |
| 80 | // Class already seen. Is it a forward declaration? |
Steve Naroff | 1422a62 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 81 | if (!IDecl->isForwardDecl()) { |
Chris Lattner | 9513cfd | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 82 | IDecl->setInvalidDecl(); |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 83 | Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName(); |
Chris Lattner | 5b25065 | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 84 | Diag(IDecl->getLocation(), diag::note_previous_definition); |
| 85 | |
Steve Naroff | 1422a62 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 86 | // Return the previous class interface. |
| 87 | // FIXME: don't leak the objects passed in! |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 88 | return DeclPtrTy::make(IDecl); |
Steve Naroff | 1422a62 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 89 | } else { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 90 | IDecl->setLocation(AtInterfaceLoc); |
| 91 | IDecl->setForwardDecl(false); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 92 | } |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 93 | } else { |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 94 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 95 | ClassName, ClassLoc); |
Daniel Dunbar | d8bd682 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 96 | if (AttrList) |
| 97 | ProcessDeclAttributeList(IDecl, AttrList); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 98 | |
Steve Naroff | 1520816 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 99 | ObjCInterfaceDecls[ClassName] = IDecl; |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 100 | // FIXME: PushOnScopeChains |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 101 | CurContext->addDecl(IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 102 | // Remember that this needs to be removed when the scope is popped. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 103 | TUScope->AddDecl(DeclPtrTy::make(IDecl)); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (SuperName) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 107 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 108 | PrevDecl = LookupName(TUScope, SuperName, LookupOrdinaryName); |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 109 | |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 110 | ObjCInterfaceDecl *SuperClassDecl = |
| 111 | dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 7699a53 | 2009-02-16 21:33:09 +0000 | [diff] [blame] | 112 | |
| 113 | // Diagnose classes that inherit from deprecated classes. |
| 114 | if (SuperClassDecl) |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 115 | (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
Chris Lattner | 7699a53 | 2009-02-16 21:33:09 +0000 | [diff] [blame] | 116 | |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 117 | if (PrevDecl && SuperClassDecl == 0) { |
| 118 | // The previous declaration was not a class decl. Check if we have a |
| 119 | // typedef. If we do, get the underlying class type. |
| 120 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) { |
| 121 | QualType T = TDecl->getUnderlyingType(); |
| 122 | if (T->isObjCInterfaceType()) { |
| 123 | if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) |
| 124 | SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| 125 | } |
| 126 | } |
Chris Lattner | 7699a53 | 2009-02-16 21:33:09 +0000 | [diff] [blame] | 127 | |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 128 | // This handles the following case: |
| 129 | // |
| 130 | // typedef int SuperClass; |
| 131 | // @interface MyClass : SuperClass {} @end |
| 132 | // |
| 133 | if (!SuperClassDecl) { |
| 134 | Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| 135 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 136 | } |
| 137 | } |
Chris Lattner | 7699a53 | 2009-02-16 21:33:09 +0000 | [diff] [blame] | 138 | |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 139 | if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) { |
| 140 | if (!SuperClassDecl) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 141 | Diag(SuperLoc, diag::err_undef_superclass) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 142 | << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 143 | else if (SuperClassDecl->isForwardDecl()) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 144 | Diag(SuperLoc, diag::err_undef_superclass) |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 145 | << SuperClassDecl->getDeclName() << ClassName |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 146 | << SourceRange(AtInterfaceLoc, ClassLoc); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 147 | } |
Steve Naroff | cb3beaf | 2009-02-04 17:14:05 +0000 | [diff] [blame] | 148 | IDecl->setSuperClass(SuperClassDecl); |
Steve Naroff | 7c37174 | 2008-04-11 19:35:35 +0000 | [diff] [blame] | 149 | IDecl->setSuperClassLoc(SuperLoc); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 150 | IDecl->setLocEnd(SuperLoc); |
| 151 | } else { // we have a root class. |
| 152 | IDecl->setLocEnd(ClassLoc); |
| 153 | } |
| 154 | |
Steve Naroff | 1422a62 | 2008-11-18 19:15:30 +0000 | [diff] [blame] | 155 | /// Check then save referenced protocols. |
Chris Lattner | ae1ae49 | 2008-07-26 04:13:19 +0000 | [diff] [blame] | 156 | if (NumProtoRefs) { |
Chris Lattner | df6133b | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 157 | IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs, |
| 158 | Context); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 159 | IDecl->setLocEnd(EndProtoLoc); |
| 160 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 161 | |
| 162 | CheckObjCDeclScope(IDecl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 163 | return DeclPtrTy::make(IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /// ActOnCompatiblityAlias - this action is called after complete parsing of |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 167 | /// @compatibility_alias declaration. It sets up the alias relationships. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 168 | Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc, |
| 169 | IdentifierInfo *AliasName, |
| 170 | SourceLocation AliasLocation, |
| 171 | IdentifierInfo *ClassName, |
| 172 | SourceLocation ClassLocation) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 173 | // Look for previous declaration of alias name |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 174 | NamedDecl *ADecl = LookupName(TUScope, AliasName, LookupOrdinaryName); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 175 | if (ADecl) { |
Chris Lattner | b13cb56 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 176 | if (isa<ObjCCompatibleAliasDecl>(ADecl)) |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 177 | Diag(AliasLocation, diag::warn_previous_alias_decl); |
Chris Lattner | b13cb56 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 178 | else |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 179 | Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
Chris Lattner | b13cb56 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 180 | Diag(ADecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 181 | return DeclPtrTy(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 182 | } |
| 183 | // Check for class declaration |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 184 | NamedDecl *CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName); |
Fariborz Jahanian | 2ac4cd3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 185 | if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) { |
| 186 | QualType T = TDecl->getUnderlyingType(); |
| 187 | if (T->isObjCInterfaceType()) { |
| 188 | if (NamedDecl *IDecl = T->getAsObjCInterfaceType()->getDecl()) { |
| 189 | ClassName = IDecl->getIdentifier(); |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 190 | CDeclU = LookupName(TUScope, ClassName, LookupOrdinaryName); |
Fariborz Jahanian | 2ac4cd3 | 2009-01-08 01:10:55 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 194 | ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| 195 | if (CDecl == 0) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 196 | Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 197 | if (CDeclU) |
Chris Lattner | b13cb56 | 2008-11-23 23:20:13 +0000 | [diff] [blame] | 198 | Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 199 | return DeclPtrTy(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 200 | } |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 201 | |
| 202 | // Everything checked out, instantiate a new alias declaration AST. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 203 | ObjCCompatibleAliasDecl *AliasDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 204 | ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 205 | |
| 206 | ObjCAliasDecls[AliasName] = AliasDecl; |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 207 | |
| 208 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 209 | CurContext->addDecl(AliasDecl); |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 210 | if (!CheckObjCDeclScope(AliasDecl)) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 211 | TUScope->AddDecl(DeclPtrTy::make(AliasDecl)); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 213 | return DeclPtrTy::make(AliasDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Steve Naroff | fe56a63 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 216 | void Sema::CheckForwardProtocolDeclarationForCircularDependency( |
| 217 | IdentifierInfo *PName, |
| 218 | SourceLocation &Ploc, SourceLocation PrevLoc, |
| 219 | const ObjCList<ObjCProtocolDecl> &PList) |
| 220 | { |
| 221 | for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| 222 | E = PList.end(); I != E; ++I) { |
| 223 | |
| 224 | if (ObjCProtocolDecl *PDecl = ObjCProtocols[(*I)->getIdentifier()]) { |
| 225 | if (PDecl->getIdentifier() == PName) { |
| 226 | Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| 227 | Diag(PrevLoc, diag::note_previous_definition); |
| 228 | } |
| 229 | CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
| 230 | PDecl->getLocation(), PDecl->getReferencedProtocols()); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 235 | Sema::DeclPtrTy |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 236 | Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc, |
| 237 | IdentifierInfo *ProtocolName, |
| 238 | SourceLocation ProtocolLoc, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 239 | const DeclPtrTy *ProtoRefs, |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 240 | unsigned NumProtoRefs, |
Daniel Dunbar | 28680d1 | 2008-09-26 04:48:09 +0000 | [diff] [blame] | 241 | SourceLocation EndProtoLoc, |
| 242 | AttributeList *AttrList) { |
| 243 | // FIXME: Deal with AttrList. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 244 | assert(ProtocolName && "Missing protocol identifier"); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 245 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolName]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 246 | if (PDecl) { |
| 247 | // Protocol already seen. Better be a forward protocol declaration |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 248 | if (!PDecl->isForwardDecl()) { |
Chris Lattner | 9513cfd | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 249 | PDecl->setInvalidDecl(); |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 250 | Diag(ProtocolLoc, diag::err_duplicate_protocol_def) << ProtocolName; |
Chris Lattner | 5b25065 | 2008-11-23 22:46:27 +0000 | [diff] [blame] | 251 | Diag(PDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 252 | // Just return the protocol we already had. |
| 253 | // FIXME: don't leak the objects passed in! |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 254 | return DeclPtrTy::make(PDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 255 | } |
Steve Naroff | fe56a63 | 2009-03-05 15:22:01 +0000 | [diff] [blame] | 256 | ObjCList<ObjCProtocolDecl> PList; |
| 257 | PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
| 258 | CheckForwardProtocolDeclarationForCircularDependency( |
| 259 | ProtocolName, ProtocolLoc, PDecl->getLocation(), PList); |
| 260 | PList.Destroy(Context); |
| 261 | |
Steve Naroff | 9a9e521 | 2008-08-13 16:39:22 +0000 | [diff] [blame] | 262 | // Make sure the cached decl gets a valid start location. |
| 263 | PDecl->setLocation(AtProtoInterfaceLoc); |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 264 | PDecl->setForwardDecl(false); |
Chris Lattner | c188185 | 2008-03-16 01:25:17 +0000 | [diff] [blame] | 265 | } else { |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 266 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
| 267 | AtProtoInterfaceLoc,ProtocolName); |
| 268 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 269 | CurContext->addDecl(PDecl); |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 270 | PDecl->setForwardDecl(false); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 271 | ObjCProtocols[ProtocolName] = PDecl; |
Chris Lattner | 180f7e2 | 2008-03-16 01:23:04 +0000 | [diff] [blame] | 272 | } |
Fariborz Jahanian | 8f9b1b2 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 273 | if (AttrList) |
| 274 | ProcessDeclAttributeList(PDecl, AttrList); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 275 | if (NumProtoRefs) { |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 276 | /// Check then save referenced protocols. |
Chris Lattner | df6133b | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 277 | PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 278 | PDecl->setLocEnd(EndProtoLoc); |
| 279 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 280 | |
| 281 | CheckObjCDeclScope(PDecl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 282 | return DeclPtrTy::make(PDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /// FindProtocolDeclaration - This routine looks up protocols and |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 286 | /// issues an error if they are not declared. It returns list of |
| 287 | /// protocol declarations in its 'Protocols' argument. |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 288 | void |
Chris Lattner | 2bdedd6 | 2008-07-26 04:03:38 +0000 | [diff] [blame] | 289 | Sema::FindProtocolDeclaration(bool WarnOnDeclarations, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 290 | const IdentifierLocPair *ProtocolId, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 291 | unsigned NumProtocols, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 292 | llvm::SmallVectorImpl<DeclPtrTy> &Protocols) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 293 | for (unsigned i = 0; i != NumProtocols; ++i) { |
Chris Lattner | 17d50a9 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 294 | ObjCProtocolDecl *PDecl = ObjCProtocols[ProtocolId[i].first]; |
| 295 | if (!PDecl) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 296 | Diag(ProtocolId[i].second, diag::err_undeclared_protocol) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 297 | << ProtocolId[i].first; |
Chris Lattner | 17d50a9 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 298 | continue; |
| 299 | } |
Chris Lattner | faaad13 | 2009-02-14 08:22:25 +0000 | [diff] [blame] | 300 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 301 | (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second); |
Chris Lattner | 17d50a9 | 2008-07-26 03:47:43 +0000 | [diff] [blame] | 302 | |
| 303 | // If this is a forward declaration and we are supposed to warn in this |
| 304 | // case, do it. |
| 305 | if (WarnOnDeclarations && PDecl->isForwardDecl()) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 306 | Diag(ProtocolId[i].second, diag::warn_undef_protocolref) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 307 | << ProtocolId[i].first; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 308 | Protocols.push_back(DeclPtrTy::make(PDecl)); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 312 | /// DiagnosePropertyMismatch - Compares two properties for their |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 313 | /// attributes and types and warns on a variety of inconsistencies. |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 314 | /// |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 315 | void |
| 316 | Sema::DiagnosePropertyMismatch(ObjCPropertyDecl *Property, |
| 317 | ObjCPropertyDecl *SuperProperty, |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 318 | const IdentifierInfo *inheritedName) { |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 319 | ObjCPropertyDecl::PropertyAttributeKind CAttr = |
| 320 | Property->getPropertyAttributes(); |
| 321 | ObjCPropertyDecl::PropertyAttributeKind SAttr = |
| 322 | SuperProperty->getPropertyAttributes(); |
| 323 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_readonly) |
| 324 | && (SAttr & ObjCPropertyDecl::OBJC_PR_readwrite)) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 325 | Diag(Property->getLocation(), diag::warn_readonly_property) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 326 | << Property->getDeclName() << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 327 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_copy) |
| 328 | != (SAttr & ObjCPropertyDecl::OBJC_PR_copy)) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 329 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 330 | << Property->getDeclName() << "copy" << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 331 | else if ((CAttr & ObjCPropertyDecl::OBJC_PR_retain) |
| 332 | != (SAttr & ObjCPropertyDecl::OBJC_PR_retain)) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 333 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 334 | << Property->getDeclName() << "retain" << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 335 | |
| 336 | if ((CAttr & ObjCPropertyDecl::OBJC_PR_nonatomic) |
| 337 | != (SAttr & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 338 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 339 | << Property->getDeclName() << "atomic" << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 340 | if (Property->getSetterName() != SuperProperty->getSetterName()) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 341 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 342 | << Property->getDeclName() << "setter" << inheritedName; |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 343 | if (Property->getGetterName() != SuperProperty->getGetterName()) |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 344 | Diag(Property->getLocation(), diag::warn_property_attribute) |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 345 | << Property->getDeclName() << "getter" << inheritedName; |
Steve Naroff | 2567c9e | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 346 | |
| 347 | QualType LHSType = |
| 348 | Context.getCanonicalType(SuperProperty->getType()); |
| 349 | QualType RHSType = |
| 350 | Context.getCanonicalType(Property->getType()); |
| 351 | |
| 352 | if (!Context.typesAreCompatible(LHSType, RHSType)) { |
| 353 | // FIXME: Incorporate this test with typesAreCompatible. |
| 354 | if (LHSType->isObjCQualifiedIdType() && RHSType->isObjCQualifiedIdType()) |
| 355 | if (ObjCQualifiedIdTypesAreCompatible(LHSType, RHSType, false)) |
| 356 | return; |
| 357 | Diag(Property->getLocation(), diag::warn_property_types_are_incompatible) |
| 358 | << Property->getType() << SuperProperty->getType() << inheritedName; |
| 359 | } |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /// ComparePropertiesInBaseAndSuper - This routine compares property |
| 363 | /// declarations in base and its super class, if any, and issues |
| 364 | /// diagnostics in a variety of inconsistant situations. |
| 365 | /// |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 366 | void Sema::ComparePropertiesInBaseAndSuper(ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 367 | ObjCInterfaceDecl *SDecl = IDecl->getSuperClass(); |
| 368 | if (!SDecl) |
| 369 | return; |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 370 | // FIXME: O(N^2) |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 371 | for (ObjCInterfaceDecl::prop_iterator S = SDecl->prop_begin(), |
| 372 | E = SDecl->prop_end(); S != E; ++S) { |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 373 | ObjCPropertyDecl *SuperPDecl = (*S); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 374 | // Does property in super class has declaration in current class? |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 375 | for (ObjCInterfaceDecl::prop_iterator I = IDecl->prop_begin(), |
| 376 | E = IDecl->prop_end(); I != E; ++I) { |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 377 | ObjCPropertyDecl *PDecl = (*I); |
| 378 | if (SuperPDecl->getIdentifier() == PDecl->getIdentifier()) |
Douglas Gregor | 24afd4a | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 379 | DiagnosePropertyMismatch(PDecl, SuperPDecl, |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 380 | SDecl->getIdentifier()); |
Fariborz Jahanian | 1e89de3 | 2008-04-24 19:58:34 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 385 | /// MergeOneProtocolPropertiesIntoClass - This routine goes thru the list |
| 386 | /// of properties declared in a protocol and adds them to the list |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 387 | /// of properties for current class/category if it is not there already. |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 388 | void |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 389 | Sema::MergeOneProtocolPropertiesIntoClass(Decl *CDecl, |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 390 | ObjCProtocolDecl *PDecl) { |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 391 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 392 | if (!IDecl) { |
| 393 | // Category |
| 394 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 395 | assert (CatDecl && "MergeOneProtocolPropertiesIntoClass"); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 396 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 397 | E = PDecl->prop_end(); P != E; ++P) { |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 398 | ObjCPropertyDecl *Pr = (*P); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 399 | ObjCCategoryDecl::prop_iterator CP, CE; |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 400 | // Is this property already in category's list of properties? |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 401 | for (CP = CatDecl->prop_begin(), CE = CatDecl->prop_end(); |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 402 | CP != CE; ++CP) |
| 403 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 404 | break; |
Fariborz Jahanian | faca5e2 | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 405 | if (CP != CE) |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 406 | // Property protocol already exist in class. Diagnose any mismatch. |
| 407 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
| 408 | } |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 409 | return; |
| 410 | } |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 411 | for (ObjCProtocolDecl::prop_iterator P = PDecl->prop_begin(), |
| 412 | E = PDecl->prop_end(); P != E; ++P) { |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 413 | ObjCPropertyDecl *Pr = (*P); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 414 | ObjCInterfaceDecl::prop_iterator CP, CE; |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 415 | // Is this property already in class's list of properties? |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 416 | for (CP = IDecl->prop_begin(), CE = IDecl->prop_end(); |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 417 | CP != CE; ++CP) |
| 418 | if ((*CP)->getIdentifier() == Pr->getIdentifier()) |
| 419 | break; |
Fariborz Jahanian | faca5e2 | 2009-01-09 21:04:52 +0000 | [diff] [blame] | 420 | if (CP != CE) |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 421 | // Property protocol already exist in class. Diagnose any mismatch. |
Chris Lattner | d120b9e | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 422 | DiagnosePropertyMismatch((*CP), Pr, PDecl->getIdentifier()); |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 423 | } |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /// MergeProtocolPropertiesIntoClass - This routine merges properties |
| 427 | /// declared in 'MergeItsProtocols' objects (which can be a class or an |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 428 | /// inherited protocol into the list of properties for class/category 'CDecl' |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 429 | /// |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 430 | void Sema::MergeProtocolPropertiesIntoClass(Decl *CDecl, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 431 | DeclPtrTy MergeItsProtocols) { |
| 432 | Decl *ClassDecl = MergeItsProtocols.getAs<Decl>(); |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 433 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDecl); |
| 434 | |
| 435 | if (!IDecl) { |
| 436 | // Category |
| 437 | ObjCCategoryDecl *CatDecl = static_cast<ObjCCategoryDecl*>(CDecl); |
| 438 | assert (CatDecl && "MergeProtocolPropertiesIntoClass"); |
| 439 | if (ObjCCategoryDecl *MDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
| 440 | for (ObjCCategoryDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 441 | E = MDecl->protocol_end(); P != E; ++P) |
| 442 | // Merge properties of category (*P) into IDECL's |
| 443 | MergeOneProtocolPropertiesIntoClass(CatDecl, *P); |
| 444 | |
| 445 | // Go thru the list of protocols for this category and recursively merge |
| 446 | // their properties into this class as well. |
| 447 | for (ObjCCategoryDecl::protocol_iterator P = CatDecl->protocol_begin(), |
| 448 | E = CatDecl->protocol_end(); P != E; ++P) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 449 | MergeProtocolPropertiesIntoClass(CatDecl, DeclPtrTy::make(*P)); |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 450 | } else { |
| 451 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 452 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 453 | E = MD->protocol_end(); P != E; ++P) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 454 | MergeOneProtocolPropertiesIntoClass(CatDecl, *P); |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 455 | } |
| 456 | return; |
| 457 | } |
| 458 | |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 459 | if (ObjCInterfaceDecl *MDecl = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 460 | for (ObjCInterfaceDecl::protocol_iterator P = MDecl->protocol_begin(), |
| 461 | E = MDecl->protocol_end(); P != E; ++P) |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 462 | // Merge properties of class (*P) into IDECL's |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 463 | MergeOneProtocolPropertiesIntoClass(IDecl, *P); |
| 464 | |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 465 | // Go thru the list of protocols for this class and recursively merge |
| 466 | // their properties into this class as well. |
| 467 | for (ObjCInterfaceDecl::protocol_iterator P = IDecl->protocol_begin(), |
| 468 | E = IDecl->protocol_end(); P != E; ++P) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 469 | MergeProtocolPropertiesIntoClass(IDecl, DeclPtrTy::make(*P)); |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 470 | } else { |
Argiris Kirtzidis | fc1ea13 | 2008-07-21 09:18:38 +0000 | [diff] [blame] | 471 | ObjCProtocolDecl *MD = cast<ObjCProtocolDecl>(ClassDecl); |
| 472 | for (ObjCProtocolDecl::protocol_iterator P = MD->protocol_begin(), |
| 473 | E = MD->protocol_end(); P != E; ++P) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 474 | MergeOneProtocolPropertiesIntoClass(IDecl, *P); |
Chris Lattner | 5cece46 | 2008-07-21 07:06:49 +0000 | [diff] [blame] | 475 | } |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Fariborz Jahanian | 38a1acc | 2009-03-02 19:06:08 +0000 | [diff] [blame] | 478 | /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
Fariborz Jahanian | ffb6882 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 479 | /// a class method in its extension. |
| 480 | /// |
| 481 | void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
| 482 | ObjCInterfaceDecl *ID) { |
| 483 | if (!ID) |
| 484 | return; // Possibly due to previous error |
| 485 | |
| 486 | llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
| 487 | for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(), |
| 488 | e = ID->meth_end(); i != e; ++i) { |
| 489 | ObjCMethodDecl *MD = *i; |
| 490 | MethodMap[MD->getSelector()] = MD; |
| 491 | } |
| 492 | |
| 493 | if (MethodMap.empty()) |
| 494 | return; |
| 495 | for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(), |
| 496 | e = CAT->meth_end(); i != e; ++i) { |
| 497 | ObjCMethodDecl *Method = *i; |
| 498 | const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
| 499 | if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
| 500 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| 501 | << Method->getDeclName(); |
| 502 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 507 | /// ActOnForwardProtocolDeclaration - |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 508 | Action::DeclPtrTy |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 509 | Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 510 | const IdentifierLocPair *IdentList, |
Fariborz Jahanian | 8f9b1b2 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 511 | unsigned NumElts, |
| 512 | AttributeList *attrList) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 513 | llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 514 | |
| 515 | for (unsigned i = 0; i != NumElts; ++i) { |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 516 | IdentifierInfo *Ident = IdentList[i].first; |
Chris Lattner | 7afba9c | 2008-03-16 20:19:15 +0000 | [diff] [blame] | 517 | ObjCProtocolDecl *&PDecl = ObjCProtocols[Ident]; |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 518 | if (PDecl == 0) { // Not already seen? |
| 519 | PDecl = ObjCProtocolDecl::Create(Context, CurContext, |
| 520 | IdentList[i].second, Ident); |
| 521 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 522 | CurContext->addDecl(PDecl); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 523 | } |
Fariborz Jahanian | 8f9b1b2 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 524 | if (attrList) |
| 525 | ProcessDeclAttributeList(PDecl, attrList); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 526 | Protocols.push_back(PDecl); |
| 527 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 528 | |
| 529 | ObjCForwardProtocolDecl *PDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 530 | ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc, |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 531 | &Protocols[0], Protocols.size()); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 532 | CurContext->addDecl(PDecl); |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 533 | CheckObjCDeclScope(PDecl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 534 | return DeclPtrTy::make(PDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 537 | Sema::DeclPtrTy Sema:: |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 538 | ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc, |
| 539 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 540 | IdentifierInfo *CategoryName, |
| 541 | SourceLocation CategoryLoc, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 542 | const DeclPtrTy *ProtoRefs, |
Chris Lattner | e705e5e | 2008-07-21 22:17:28 +0000 | [diff] [blame] | 543 | unsigned NumProtoRefs, |
| 544 | SourceLocation EndProtoLoc) { |
Chris Lattner | e29dc83 | 2008-03-16 20:34:23 +0000 | [diff] [blame] | 545 | ObjCCategoryDecl *CDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 546 | ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, CategoryName); |
| 547 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 548 | CurContext->addDecl(CDecl); |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 549 | |
| 550 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Fariborz Jahanian | 6669a58 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 551 | /// Check that class of this category is already completely declared. |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 552 | if (!IDecl || IDecl->isForwardDecl()) { |
| 553 | CDecl->setInvalidDecl(); |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 554 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 555 | return DeclPtrTy::make(CDecl); |
Fariborz Jahanian | 6669a58 | 2008-01-17 20:33:24 +0000 | [diff] [blame] | 556 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 557 | |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 558 | CDecl->setClassInterface(IDecl); |
Chris Lattner | de3fea8 | 2009-02-16 21:30:01 +0000 | [diff] [blame] | 559 | |
| 560 | // If the interface is deprecated, warn about it. |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 561 | (void)DiagnoseUseOfDecl(IDecl, ClassLoc); |
Chris Lattner | 1fa7f62 | 2009-02-16 21:26:43 +0000 | [diff] [blame] | 562 | |
| 563 | /// Check for duplicate interface declaration for this category |
| 564 | ObjCCategoryDecl *CDeclChain; |
| 565 | for (CDeclChain = IDecl->getCategoryList(); CDeclChain; |
| 566 | CDeclChain = CDeclChain->getNextClassCategory()) { |
| 567 | if (CategoryName && CDeclChain->getIdentifier() == CategoryName) { |
| 568 | Diag(CategoryLoc, diag::warn_dup_category_def) |
| 569 | << ClassName << CategoryName; |
| 570 | Diag(CDeclChain->getLocation(), diag::note_previous_definition); |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | if (!CDeclChain) |
| 575 | CDecl->insertNextClassCategory(); |
| 576 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 577 | if (NumProtoRefs) { |
Chris Lattner | df6133b | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 578 | CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,Context); |
Chris Lattner | 45142b9 | 2008-07-26 04:07:02 +0000 | [diff] [blame] | 579 | CDecl->setLocEnd(EndProtoLoc); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 580 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 581 | |
| 582 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 583 | return DeclPtrTy::make(CDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /// ActOnStartCategoryImplementation - Perform semantic checks on the |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 587 | /// category implementation declaration and build an ObjCCategoryImplDecl |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 588 | /// object. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 589 | Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation( |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 590 | SourceLocation AtCatImplLoc, |
| 591 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 592 | IdentifierInfo *CatName, SourceLocation CatLoc) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 593 | ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName); |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 594 | ObjCCategoryImplDecl *CDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 595 | ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName, |
| 596 | IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 597 | /// Check that class of this category is already completely declared. |
| 598 | if (!IDecl || IDecl->isForwardDecl()) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 599 | Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 600 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 601 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 602 | CurContext->addDecl(CDecl); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 604 | /// TODO: Check that CatName, category name, is not used in another |
| 605 | // implementation. |
Steve Naroff | 4b9846d | 2008-09-28 14:55:53 +0000 | [diff] [blame] | 606 | ObjCCategoryImpls.push_back(CDecl); |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 607 | |
| 608 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 609 | return DeclPtrTy::make(CDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 612 | Sema::DeclPtrTy Sema::ActOnStartClassImplementation( |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 613 | SourceLocation AtClassImplLoc, |
| 614 | IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| 615 | IdentifierInfo *SuperClassname, |
| 616 | SourceLocation SuperClassLoc) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 617 | ObjCInterfaceDecl* IDecl = 0; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 618 | // Check for another declaration kind with the same name. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 619 | NamedDecl *PrevDecl = LookupName(TUScope, ClassName, LookupOrdinaryName); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 620 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 621 | Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 622 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 9513cfd | 2009-02-23 22:00:08 +0000 | [diff] [blame] | 623 | } else { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 624 | // Is there an interface declaration of this class; if not, warn! |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 625 | IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 626 | if (!IDecl) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 627 | Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | // Check that super class name is valid class name |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 631 | ObjCInterfaceDecl* SDecl = 0; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 632 | if (SuperClassname) { |
| 633 | // Check if a different kind of symbol declared in this scope. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 634 | PrevDecl = LookupName(TUScope, SuperClassname, LookupOrdinaryName); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 635 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 636 | Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| 637 | << SuperClassname; |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 638 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 639 | } else { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 640 | SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 641 | if (!SDecl) |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 642 | Diag(SuperClassLoc, diag::err_undef_superclass) |
| 643 | << SuperClassname << ClassName; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 644 | else if (IDecl && IDecl->getSuperClass() != SDecl) { |
| 645 | // This implementation and its interface do not have the same |
| 646 | // super class. |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 647 | Diag(SuperClassLoc, diag::err_conflicting_super_class) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 648 | << SDecl->getDeclName(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 649 | Diag(SDecl->getLocation(), diag::note_previous_definition); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (!IDecl) { |
| 655 | // Legacy case of @implementation with no corresponding @interface. |
| 656 | // Build, chain & install the interface decl into the identifier. |
Daniel Dunbar | d8bd682 | 2008-08-20 18:02:42 +0000 | [diff] [blame] | 657 | |
| 658 | // FIXME: Do we support attributes on the @implementation? If so |
| 659 | // we should copy them over. |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 660 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
| 661 | ClassName, ClassLoc, false, true); |
Steve Naroff | 1520816 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 662 | ObjCInterfaceDecls[ClassName] = IDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 663 | IDecl->setSuperClass(SDecl); |
| 664 | IDecl->setLocEnd(ClassLoc); |
| 665 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 666 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 667 | CurContext->addDecl(IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 668 | // Remember that this needs to be removed when the scope is popped. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 669 | TUScope->AddDecl(DeclPtrTy::make(IDecl)); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 672 | ObjCImplementationDecl* IMPDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 673 | ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 674 | IDecl, SDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 676 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 677 | CurContext->addDecl(IMPDecl); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 678 | |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 679 | if (CheckObjCDeclScope(IMPDecl)) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 680 | return DeclPtrTy::make(IMPDecl); |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 682 | // Check that there is no duplicate implementation of this class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 683 | if (ObjCImplementations[ClassName]) |
Chris Lattner | 1b6de33 | 2008-03-16 20:53:07 +0000 | [diff] [blame] | 684 | // FIXME: Don't leak everything! |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 685 | Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 686 | else // add it to the list. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 687 | ObjCImplementations[ClassName] = IMPDecl; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 688 | return DeclPtrTy::make(IMPDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 691 | void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| 692 | ObjCIvarDecl **ivars, unsigned numIvars, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 693 | SourceLocation RBrace) { |
| 694 | assert(ImpDecl && "missing implementation decl"); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 695 | ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 696 | if (!IDecl) |
| 697 | return; |
| 698 | /// Check case of non-existing @interface decl. |
| 699 | /// (legacy objective-c @implementation decl without an @interface decl). |
| 700 | /// Add implementations's ivar to the synthesize class's ivar list. |
| 701 | if (IDecl->ImplicitInterfaceDecl()) { |
Chris Lattner | df6133b | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 702 | IDecl->setIVarList(ivars, numIvars, Context); |
| 703 | IDecl->setLocEnd(RBrace); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | // If implementation has empty ivar list, just return. |
| 707 | if (numIvars == 0) |
| 708 | return; |
| 709 | |
| 710 | assert(ivars && "missing @implementation ivars"); |
| 711 | |
| 712 | // Check interface's Ivar list against those in the implementation. |
| 713 | // names and types must match. |
| 714 | // |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 715 | unsigned j = 0; |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 716 | ObjCInterfaceDecl::ivar_iterator |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 717 | IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| 718 | for (; numIvars > 0 && IVI != IVE; ++IVI) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 719 | ObjCIvarDecl* ImplIvar = ivars[j++]; |
| 720 | ObjCIvarDecl* ClsIvar = *IVI; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 721 | assert (ImplIvar && "missing implementation ivar"); |
| 722 | assert (ClsIvar && "missing class ivar"); |
Steve Naroff | 58d0d6f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 723 | |
| 724 | // First, make sure the types match. |
Chris Lattner | b545786 | 2008-07-27 00:05:05 +0000 | [diff] [blame] | 725 | if (Context.getCanonicalType(ImplIvar->getType()) != |
| 726 | Context.getCanonicalType(ClsIvar->getType())) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 727 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 728 | << ImplIvar->getIdentifier() |
| 729 | << ImplIvar->getType() << ClsIvar->getType(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 730 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Steve Naroff | 58d0d6f | 2009-03-03 14:49:36 +0000 | [diff] [blame] | 731 | } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) { |
| 732 | Expr *ImplBitWidth = ImplIvar->getBitWidth(); |
| 733 | Expr *ClsBitWidth = ClsIvar->getBitWidth(); |
| 734 | if (ImplBitWidth->getIntegerConstantExprValue(Context).getZExtValue() != |
| 735 | ClsBitWidth->getIntegerConstantExprValue(Context).getZExtValue()) { |
| 736 | Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth) |
| 737 | << ImplIvar->getIdentifier(); |
| 738 | Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition); |
| 739 | } |
| 740 | } |
| 741 | // Make sure the names are identical. |
| 742 | if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 743 | Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 744 | << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 745 | Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 746 | } |
| 747 | --numIvars; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 748 | } |
Chris Lattner | 1cc669d | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 749 | |
| 750 | if (numIvars > 0) |
Chris Lattner | 847de6f | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 751 | Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 1cc669d | 2007-12-12 18:11:49 +0000 | [diff] [blame] | 752 | else if (IVI != IVE) |
Chris Lattner | 847de6f | 2007-12-12 18:19:52 +0000 | [diff] [blame] | 753 | Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 756 | void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, |
| 757 | bool &IncompleteImpl) { |
| 758 | if (!IncompleteImpl) { |
| 759 | Diag(ImpLoc, diag::warn_incomplete_impl); |
| 760 | IncompleteImpl = true; |
| 761 | } |
Chris Lattner | b175342 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 762 | Diag(ImpLoc, diag::warn_undef_method_impl) << method->getDeclName(); |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Fariborz Jahanian | 4e0f645 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 765 | void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| 766 | ObjCMethodDecl *IntfMethodDecl) { |
| 767 | bool err = false; |
| 768 | QualType ImpMethodQType = |
| 769 | Context.getCanonicalType(ImpMethodDecl->getResultType()); |
| 770 | QualType IntfMethodQType = |
| 771 | Context.getCanonicalType(IntfMethodDecl->getResultType()); |
| 772 | if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) |
| 773 | err = true; |
| 774 | else for (ObjCMethodDecl::param_iterator IM=ImpMethodDecl->param_begin(), |
| 775 | IF=IntfMethodDecl->param_begin(), |
| 776 | EM=ImpMethodDecl->param_end(); IM!=EM; ++IM, IF++) { |
| 777 | ImpMethodQType = Context.getCanonicalType((*IM)->getType()); |
| 778 | IntfMethodQType = Context.getCanonicalType((*IF)->getType()); |
| 779 | if (!Context.typesAreCompatible(IntfMethodQType, ImpMethodQType)) { |
| 780 | err = true; |
| 781 | break; |
| 782 | } |
| 783 | } |
| 784 | if (err) { |
| 785 | Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_types) |
| 786 | << ImpMethodDecl->getDeclName(); |
| 787 | Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition); |
| 788 | } |
| 789 | } |
| 790 | |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 791 | /// isPropertyReadonly - Return true if property is readonly, by searching |
| 792 | /// for the property in the class and in its categories and implementations |
| 793 | /// |
| 794 | bool Sema::isPropertyReadonly(ObjCPropertyDecl *PDecl, |
Steve Naroff | b0bf2c9 | 2009-02-26 19:11:32 +0000 | [diff] [blame] | 795 | ObjCInterfaceDecl *IDecl) { |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 796 | // by far the most common case. |
| 797 | if (!PDecl->isReadOnly()) |
| 798 | return false; |
| 799 | // Even if property is ready only, if interface has a user defined setter, |
| 800 | // it is not considered read only. |
| 801 | if (IDecl->getInstanceMethod(PDecl->getSetterName())) |
| 802 | return false; |
| 803 | |
| 804 | // Main class has the property as 'readonly'. Must search |
| 805 | // through the category list to see if the property's |
| 806 | // attribute has been over-ridden to 'readwrite'. |
| 807 | for (ObjCCategoryDecl *Category = IDecl->getCategoryList(); |
| 808 | Category; Category = Category->getNextClassCategory()) { |
| 809 | // Even if property is ready only, if a category has a user defined setter, |
| 810 | // it is not considered read only. |
| 811 | if (Category->getInstanceMethod(PDecl->getSetterName())) |
| 812 | return false; |
| 813 | ObjCPropertyDecl *P = |
| 814 | Category->FindPropertyDeclaration(PDecl->getIdentifier()); |
| 815 | if (P && !P->isReadOnly()) |
| 816 | return false; |
| 817 | } |
| 818 | |
| 819 | // Also, check for definition of a setter method in the implementation if |
| 820 | // all else failed. |
| 821 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurContext)) { |
| 822 | if (ObjCImplementationDecl *IMD = |
| 823 | dyn_cast<ObjCImplementationDecl>(OMD->getDeclContext())) { |
| 824 | if (IMD->getInstanceMethod(PDecl->getSetterName())) |
| 825 | return false; |
| 826 | } |
| 827 | else if (ObjCCategoryImplDecl *CIMD = |
| 828 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { |
| 829 | if (CIMD->getInstanceMethod(PDecl->getSetterName())) |
| 830 | return false; |
| 831 | } |
| 832 | } |
Steve Naroff | b0bf2c9 | 2009-02-26 19:11:32 +0000 | [diff] [blame] | 833 | // Lastly, look through the implementation (if one is in scope). |
| 834 | if (ObjCImplementationDecl *ImpDecl = |
| 835 | ObjCImplementations[IDecl->getIdentifier()]) |
| 836 | if (ImpDecl->getInstanceMethod(PDecl->getSetterName())) |
| 837 | return false; |
Fariborz Jahanian | bfdf9db | 2009-04-06 16:59:10 +0000 | [diff] [blame^] | 838 | // If all fails, look at the super class. |
| 839 | if (ObjCInterfaceDecl *SIDecl = IDecl->getSuperClass()) |
| 840 | return isPropertyReadonly(PDecl, SIDecl); |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 841 | return true; |
| 842 | } |
| 843 | |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 844 | /// FIXME: Type hierarchies in Objective-C can be deep. We could most |
| 845 | /// likely improve the efficiency of selector lookups and type |
| 846 | /// checking by associating with each protocol / interface / category |
| 847 | /// the flattened instance tables. If we used an immutable set to keep |
| 848 | /// the table then it wouldn't add significant memory cost and it |
| 849 | /// would be handy for lookups. |
| 850 | |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 851 | /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 852 | /// Declared in protocol, and those referenced by it. |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 853 | void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc, |
| 854 | ObjCProtocolDecl *PDecl, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 855 | bool& IncompleteImpl, |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 856 | const llvm::DenseSet<Selector> &InsMap, |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 857 | const llvm::DenseSet<Selector> &ClsMap, |
| 858 | ObjCInterfaceDecl *IDecl) { |
| 859 | ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
| 860 | |
| 861 | // If a method lookup fails locally we still need to look and see if |
| 862 | // the method was implemented by a base class or an inherited |
| 863 | // protocol. This lookup is slow, but occurs rarely in correct code |
| 864 | // and otherwise would terminate in a warning. |
| 865 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 866 | // check unimplemented instance methods. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 867 | for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 868 | E = PDecl->instmeth_end(); I != E; ++I) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 869 | ObjCMethodDecl *method = *I; |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 870 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
Fariborz Jahanian | 4b871b3 | 2008-11-24 22:16:00 +0000 | [diff] [blame] | 871 | !method->isSynthesized() && !InsMap.count(method->getSelector()) && |
Fariborz Jahanian | 30ecfb7 | 2009-04-03 21:51:32 +0000 | [diff] [blame] | 872 | (!Super || !Super->lookupInstanceMethod(method->getSelector()))) { |
| 873 | // Ugly, but necessary. Method declared in protcol might have |
| 874 | // have been synthesized due to a property declared in the class which |
| 875 | // uses the protocol. |
| 876 | ObjCMethodDecl *MethodInClass = |
| 877 | IDecl->lookupInstanceMethod(method->getSelector()); |
| 878 | if (!MethodInClass || !MethodInClass->isSynthesized()) |
| 879 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
| 880 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 881 | } |
| 882 | // check unimplemented class methods |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 883 | for (ObjCProtocolDecl::classmeth_iterator I = PDecl->classmeth_begin(), |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 884 | E = PDecl->classmeth_end(); I != E; ++I) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 885 | ObjCMethodDecl *method = *I; |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 886 | if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 887 | !ClsMap.count(method->getSelector()) && |
| 888 | (!Super || !Super->lookupClassMethod(method->getSelector()))) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 889 | WarnUndefinedMethod(ImpLoc, method, IncompleteImpl); |
Steve Naroff | 2ce399a | 2007-12-14 23:37:57 +0000 | [diff] [blame] | 890 | } |
Chris Lattner | 0be0882 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 891 | // Check on this protocols's referenced protocols, recursively. |
| 892 | for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(), |
| 893 | E = PDecl->protocol_end(); PI != E; ++PI) |
Daniel Dunbar | 1d706e9 | 2008-09-04 20:01:15 +0000 | [diff] [blame] | 894 | CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Chris Lattner | 7cc13bf | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 897 | void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl, |
| 898 | ObjCContainerDecl* CDecl, |
| 899 | bool IncompleteImpl) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 900 | llvm::DenseSet<Selector> InsMap; |
| 901 | // Check and see if instance methods in class interface have been |
| 902 | // implemented in the implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 903 | for (ObjCImplementationDecl::instmeth_iterator I = IMPDecl->instmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 904 | E = IMPDecl->instmeth_end(); I != E; ++I) |
| 905 | InsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 906 | |
Chris Lattner | 7cc13bf | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 907 | for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(), |
| 908 | E = CDecl->instmeth_end(); I != E; ++I) { |
Chris Lattner | 2840008 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 909 | if (!(*I)->isSynthesized() && !InsMap.count((*I)->getSelector())) { |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 910 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Chris Lattner | 2840008 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 911 | continue; |
Fariborz Jahanian | 64c4631 | 2008-12-05 01:35:25 +0000 | [diff] [blame] | 912 | } |
Chris Lattner | 2840008 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 913 | |
| 914 | ObjCMethodDecl *ImpMethodDecl = |
| 915 | IMPDecl->getInstanceMethod((*I)->getSelector()); |
| 916 | ObjCMethodDecl *IntfMethodDecl = |
Chris Lattner | 7cc13bf | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 917 | CDecl->getInstanceMethod((*I)->getSelector()); |
Chris Lattner | 2840008 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 918 | assert(IntfMethodDecl && |
| 919 | "IntfMethodDecl is null in ImplMethodsVsClassMethods"); |
| 920 | // ImpMethodDecl may be null as in a @dynamic property. |
| 921 | if (ImpMethodDecl) |
| 922 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 923 | } |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 924 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 925 | llvm::DenseSet<Selector> ClsMap; |
| 926 | // Check and see if class methods in class interface have been |
| 927 | // implemented in the implementation class. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 928 | for (ObjCImplementationDecl::classmeth_iterator I =IMPDecl->classmeth_begin(), |
Chris Lattner | 9d76c72 | 2007-12-12 17:58:05 +0000 | [diff] [blame] | 929 | E = IMPDecl->classmeth_end(); I != E; ++I) |
| 930 | ClsMap.insert((*I)->getSelector()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 7cc13bf | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 932 | for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(), |
| 933 | E = CDecl->classmeth_end(); I != E; ++I) |
Steve Naroff | b4f4851 | 2008-02-10 21:38:56 +0000 | [diff] [blame] | 934 | if (!ClsMap.count((*I)->getSelector())) |
| 935 | WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl); |
Fariborz Jahanian | 4e0f645 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 936 | else { |
| 937 | ObjCMethodDecl *ImpMethodDecl = |
| 938 | IMPDecl->getClassMethod((*I)->getSelector()); |
| 939 | ObjCMethodDecl *IntfMethodDecl = |
Chris Lattner | 7cc13bf | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 940 | CDecl->getClassMethod((*I)->getSelector()); |
Fariborz Jahanian | 4e0f645 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 941 | WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl); |
| 942 | } |
| 943 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 944 | |
| 945 | // Check the protocol list for unimplemented methods in the @implementation |
| 946 | // class. |
Chris Lattner | 7cc13bf | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 947 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) { |
| 948 | for (ObjCCategoryDecl::protocol_iterator PI = I->protocol_begin(), |
| 949 | E = I->protocol_end(); PI != E; ++PI) |
| 950 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
| 951 | InsMap, ClsMap, I); |
| 952 | // Check class extensions (unnamed categories) |
| 953 | for (ObjCCategoryDecl *Categories = I->getCategoryList(); |
| 954 | Categories; Categories = Categories->getNextClassCategory()) { |
| 955 | if (!Categories->getIdentifier()) { |
| 956 | ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl); |
| 957 | break; |
| 958 | } |
Fariborz Jahanian | 4e0f645 | 2008-12-05 18:18:52 +0000 | [diff] [blame] | 959 | } |
Chris Lattner | 7cc13bf | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 960 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) { |
| 961 | for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(), |
| 962 | E = C->protocol_end(); PI != E; ++PI) |
| 963 | CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl, |
| 964 | InsMap, ClsMap, C->getClassInterface()); |
| 965 | } else |
| 966 | assert(false && "invalid ObjCContainerDecl type."); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /// ActOnForwardClassDeclaration - |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 970 | Action::DeclPtrTy |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 971 | Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc, |
Chris Lattner | 2840008 | 2009-02-16 19:25:52 +0000 | [diff] [blame] | 972 | IdentifierInfo **IdentList, |
| 973 | unsigned NumElts) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 974 | llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 975 | |
| 976 | for (unsigned i = 0; i != NumElts; ++i) { |
| 977 | // Check for another declaration kind with the same name. |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 978 | NamedDecl *PrevDecl = LookupName(TUScope, IdentList[i], LookupOrdinaryName); |
Douglas Gregor | 2715a1f | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 979 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 980 | // Maybe we will complain about the shadowed template parameter. |
| 981 | DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl); |
| 982 | // Just pretend that we didn't see the previous declaration. |
| 983 | PrevDecl = 0; |
| 984 | } |
| 985 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 986 | if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
Steve Naroff | d254997 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 987 | // GCC apparently allows the following idiom: |
| 988 | // |
| 989 | // typedef NSObject < XCElementTogglerP > XCElementToggler; |
| 990 | // @class XCElementToggler; |
| 991 | // |
| 992 | // FIXME: Make an extension? |
| 993 | TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl); |
| 994 | if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) { |
Chris Lattner | 65cae29 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 995 | Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i]; |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 996 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
Steve Naroff | d254997 | 2008-06-05 22:57:10 +0000 | [diff] [blame] | 997 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 998 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 999 | ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1000 | if (!IDecl) { // Not already seen? Make a forward decl. |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1001 | IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc, |
| 1002 | IdentList[i], SourceLocation(), true); |
Steve Naroff | 1520816 | 2008-04-02 18:30:49 +0000 | [diff] [blame] | 1003 | ObjCInterfaceDecls[IdentList[i]] = IDecl; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1004 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1005 | // FIXME: PushOnScopeChains? |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1006 | CurContext->addDecl(IDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1007 | // Remember that this needs to be removed when the scope is popped. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1008 | TUScope->AddDecl(DeclPtrTy::make(IDecl)); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | Interfaces.push_back(IDecl); |
| 1012 | } |
| 1013 | |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1014 | ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc, |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1015 | &Interfaces[0], |
| 1016 | Interfaces.size()); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1017 | CurContext->addDecl(CDecl); |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1018 | CheckObjCDeclScope(CDecl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1019 | return DeclPtrTy::make(CDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | /// MatchTwoMethodDeclarations - Checks that two methods have matching type and |
| 1024 | /// returns true, or false, accordingly. |
| 1025 | /// TODO: Handle protocol list; such as id<p1,p2> in type comparisons |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1026 | bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method, |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1027 | const ObjCMethodDecl *PrevMethod, |
| 1028 | bool matchBasedOnSizeAndAlignment) { |
| 1029 | QualType T1 = Context.getCanonicalType(Method->getResultType()); |
| 1030 | QualType T2 = Context.getCanonicalType(PrevMethod->getResultType()); |
| 1031 | |
| 1032 | if (T1 != T2) { |
| 1033 | // The result types are different. |
| 1034 | if (!matchBasedOnSizeAndAlignment) |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1035 | return false; |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1036 | // Incomplete types don't have a size and alignment. |
| 1037 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1038 | return false; |
| 1039 | // Check is based on size and alignment. |
| 1040 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1041 | return false; |
| 1042 | } |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1043 | |
| 1044 | ObjCMethodDecl::param_iterator ParamI = Method->param_begin(), |
| 1045 | E = Method->param_end(); |
| 1046 | ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin(); |
| 1047 | |
| 1048 | for (; ParamI != E; ++ParamI, ++PrevI) { |
| 1049 | assert(PrevI != PrevMethod->param_end() && "Param mismatch"); |
| 1050 | T1 = Context.getCanonicalType((*ParamI)->getType()); |
| 1051 | T2 = Context.getCanonicalType((*PrevI)->getType()); |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1052 | if (T1 != T2) { |
| 1053 | // The result types are different. |
| 1054 | if (!matchBasedOnSizeAndAlignment) |
| 1055 | return false; |
| 1056 | // Incomplete types don't have a size and alignment. |
| 1057 | if (T1->isIncompleteType() || T2->isIncompleteType()) |
| 1058 | return false; |
| 1059 | // Check is based on size and alignment. |
| 1060 | if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2)) |
| 1061 | return false; |
| 1062 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1063 | } |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1067 | void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) { |
Chris Lattner | c13b27f | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1068 | ObjCMethodList &Entry = InstanceMethodPool[Method->getSelector()]; |
| 1069 | if (Entry.Method == 0) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1070 | // Haven't seen a method with this selector name yet - add it. |
Chris Lattner | c13b27f | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1071 | Entry.Method = Method; |
| 1072 | Entry.Next = 0; |
| 1073 | return; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1074 | } |
Chris Lattner | c13b27f | 2009-03-04 05:16:45 +0000 | [diff] [blame] | 1075 | |
| 1076 | // We've seen a method with this name, see if we have already seen this type |
| 1077 | // signature. |
| 1078 | for (ObjCMethodList *List = &Entry; List; List = List->Next) |
| 1079 | if (MatchTwoMethodDeclarations(Method, List->Method)) |
| 1080 | return; |
| 1081 | |
| 1082 | // We have a new signature for an existing method - add it. |
| 1083 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
| 1084 | Entry.Next = new ObjCMethodList(Method, Entry.Next); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Steve Naroff | ec7e088 | 2008-10-21 10:50:19 +0000 | [diff] [blame] | 1087 | // FIXME: Finish implementing -Wno-strict-selector-match. |
Steve Naroff | 68354f3 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1088 | ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel, |
| 1089 | SourceRange R) { |
| 1090 | ObjCMethodList &MethList = InstanceMethodPool[Sel]; |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1091 | bool issueWarning = false; |
Steve Naroff | 68354f3 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1092 | |
| 1093 | if (MethList.Method && MethList.Next) { |
Steve Naroff | b91afca | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 1094 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
| 1095 | // This checks if the methods differ by size & alignment. |
| 1096 | if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true)) |
| 1097 | issueWarning = true; |
| 1098 | } |
| 1099 | if (issueWarning && (MethList.Method && MethList.Next)) { |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1100 | Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R; |
Chris Lattner | ef2a3c6 | 2008-11-23 23:26:13 +0000 | [diff] [blame] | 1101 | Diag(MethList.Method->getLocStart(), diag::note_using_decl) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1102 | << MethList.Method->getSourceRange(); |
Steve Naroff | 68354f3 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1103 | for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) |
Chris Lattner | ef2a3c6 | 2008-11-23 23:26:13 +0000 | [diff] [blame] | 1104 | Diag(Next->Method->getLocStart(), diag::note_also_found_decl) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 1105 | << Next->Method->getSourceRange(); |
Steve Naroff | 68354f3 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 1106 | } |
| 1107 | return MethList.Method; |
| 1108 | } |
| 1109 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1110 | void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) { |
| 1111 | ObjCMethodList &FirstMethod = FactoryMethodPool[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1112 | if (!FirstMethod.Method) { |
| 1113 | // Haven't seen a method with this selector name yet - add it. |
| 1114 | FirstMethod.Method = Method; |
| 1115 | FirstMethod.Next = 0; |
| 1116 | } else { |
| 1117 | // We've seen a method with this name, now check the type signature(s). |
| 1118 | bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method); |
| 1119 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1120 | for (ObjCMethodList *Next = FirstMethod.Next; !match && Next; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1121 | Next = Next->Next) |
| 1122 | match = MatchTwoMethodDeclarations(Method, Next->Method); |
| 1123 | |
| 1124 | if (!match) { |
| 1125 | // We have a new signature for an existing method - add it. |
| 1126 | // This is extremely rare. Only 1% of Cocoa selectors are "overloaded". |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1127 | struct ObjCMethodList *OMI = new ObjCMethodList(Method, FirstMethod.Next); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1128 | FirstMethod.Next = OMI; |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1133 | /// ProcessPropertyDecl - Make sure that any user-defined setter/getter methods |
| 1134 | /// have the property type and issue diagnostics if they don't. |
| 1135 | /// Also synthesize a getter/setter method if none exist (and update the |
| 1136 | /// appropriate lookup tables. FIXME: Should reconsider if adding synthesized |
| 1137 | /// methods is the "right" thing to do. |
| 1138 | void Sema::ProcessPropertyDecl(ObjCPropertyDecl *property, |
| 1139 | ObjCContainerDecl *CD) { |
| 1140 | ObjCMethodDecl *GetterMethod, *SetterMethod; |
| 1141 | |
| 1142 | GetterMethod = CD->getInstanceMethod(property->getGetterName()); |
| 1143 | SetterMethod = CD->getInstanceMethod(property->getSetterName()); |
| 1144 | |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1145 | if (GetterMethod && |
Fariborz Jahanian | 88a6293 | 2008-12-06 21:48:16 +0000 | [diff] [blame] | 1146 | GetterMethod->getResultType() != property->getType()) { |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1147 | Diag(property->getLocation(), |
| 1148 | diag::err_accessor_property_type_mismatch) |
| 1149 | << property->getDeclName() |
Ted Kremenek | 7c568a4 | 2009-03-14 00:20:08 +0000 | [diff] [blame] | 1150 | << GetterMethod->getSelector(); |
Fariborz Jahanian | 88a6293 | 2008-12-06 21:48:16 +0000 | [diff] [blame] | 1151 | Diag(GetterMethod->getLocation(), diag::note_declared_at); |
| 1152 | } |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1153 | |
| 1154 | if (SetterMethod) { |
Fariborz Jahanian | 5c15054 | 2008-12-06 23:12:49 +0000 | [diff] [blame] | 1155 | if (Context.getCanonicalType(SetterMethod->getResultType()) |
| 1156 | != Context.VoidTy) |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1157 | Diag(SetterMethod->getLocation(), diag::err_setter_type_void); |
Chris Lattner | 5c6b2c6 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 1158 | if (SetterMethod->param_size() != 1 || |
| 1159 | ((*SetterMethod->param_begin())->getType() != property->getType())) { |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1160 | Diag(property->getLocation(), |
| 1161 | diag::err_accessor_property_type_mismatch) |
| 1162 | << property->getDeclName() |
Ted Kremenek | 7c568a4 | 2009-03-14 00:20:08 +0000 | [diff] [blame] | 1163 | << SetterMethod->getSelector(); |
Fariborz Jahanian | 88a6293 | 2008-12-06 21:48:16 +0000 | [diff] [blame] | 1164 | Diag(SetterMethod->getLocation(), diag::note_declared_at); |
| 1165 | } |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1166 | } |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1167 | |
| 1168 | // Synthesize getter/setter methods if none exist. |
Steve Naroff | 5492c1b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1169 | // Find the default getter and if one not found, add one. |
Steve Naroff | 4cf0d3f | 2009-01-08 20:17:34 +0000 | [diff] [blame] | 1170 | // FIXME: The synthesized property we set here is misleading. We |
| 1171 | // almost always synthesize these methods unless the user explicitly |
| 1172 | // provided prototypes (which is odd, but allowed). Sema should be |
| 1173 | // typechecking that the declarations jive in that situation (which |
| 1174 | // it is not currently). |
Steve Naroff | 5492c1b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1175 | if (!GetterMethod) { |
| 1176 | // No instance method of same name as property getter name was found. |
| 1177 | // Declare a getter method and add it to the list of methods |
| 1178 | // for this class. |
| 1179 | GetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), |
| 1180 | property->getLocation(), property->getGetterName(), |
| 1181 | property->getType(), CD, true, false, true, |
| 1182 | (property->getPropertyImplementation() == |
| 1183 | ObjCPropertyDecl::Optional) ? |
| 1184 | ObjCMethodDecl::Optional : |
| 1185 | ObjCMethodDecl::Required); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1186 | CD->addDecl(GetterMethod); |
Steve Naroff | 5492c1b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1187 | } else |
| 1188 | // A user declared getter will be synthesize when @synthesize of |
| 1189 | // the property with the same name is seen in the @implementation |
| 1190 | GetterMethod->setIsSynthesized(); |
| 1191 | property->setGetterMethodDecl(GetterMethod); |
| 1192 | |
| 1193 | // Skip setter if property is read-only. |
| 1194 | if (!property->isReadOnly()) { |
| 1195 | // Find the default setter and if one not found, add one. |
| 1196 | if (!SetterMethod) { |
| 1197 | // No instance method of same name as property setter name was found. |
| 1198 | // Declare a setter method and add it to the list of methods |
| 1199 | // for this class. |
| 1200 | SetterMethod = ObjCMethodDecl::Create(Context, property->getLocation(), |
| 1201 | property->getLocation(), |
| 1202 | property->getSetterName(), |
| 1203 | Context.VoidTy, CD, true, false, true, |
| 1204 | (property->getPropertyImplementation() == |
| 1205 | ObjCPropertyDecl::Optional) ? |
| 1206 | ObjCMethodDecl::Optional : |
| 1207 | ObjCMethodDecl::Required); |
| 1208 | // Invent the arguments for the setter. We don't bother making a |
| 1209 | // nice name for the argument. |
| 1210 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterMethod, |
| 1211 | SourceLocation(), |
| 1212 | property->getIdentifier(), |
| 1213 | property->getType(), |
| 1214 | VarDecl::None, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1215 | 0); |
Chris Lattner | df6133b | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 1216 | SetterMethod->setMethodParams(&Argument, 1, Context); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1217 | CD->addDecl(SetterMethod); |
Steve Naroff | 5492c1b | 2009-01-08 20:15:03 +0000 | [diff] [blame] | 1218 | } else |
| 1219 | // A user declared setter will be synthesize when @synthesize of |
| 1220 | // the property with the same name is seen in the @implementation |
| 1221 | SetterMethod->setIsSynthesized(); |
| 1222 | property->setSetterMethodDecl(SetterMethod); |
| 1223 | } |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1224 | // Add any synthesized methods to the global pool. This allows us to |
| 1225 | // handle the following, which is supported by GCC (and part of the design). |
| 1226 | // |
| 1227 | // @interface Foo |
| 1228 | // @property double bar; |
| 1229 | // @end |
| 1230 | // |
| 1231 | // void thisIsUnfortunate() { |
| 1232 | // id foo; |
| 1233 | // double bar = [foo bar]; |
| 1234 | // } |
| 1235 | // |
Douglas Gregor | d167538 | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1236 | if (GetterMethod) |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1237 | AddInstanceMethodToGlobalPool(GetterMethod); |
Douglas Gregor | d167538 | 2009-01-09 19:42:16 +0000 | [diff] [blame] | 1238 | if (SetterMethod) |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1239 | AddInstanceMethodToGlobalPool(SetterMethod); |
Fariborz Jahanian | faca797 | 2008-12-02 18:39:49 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1242 | // Note: For class/category implemenations, allMethods/allProperties is |
| 1243 | // always null. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1244 | void Sema::ActOnAtEnd(SourceLocation AtEndLoc, DeclPtrTy classDecl, |
| 1245 | DeclPtrTy *allMethods, unsigned allNum, |
| 1246 | DeclPtrTy *allProperties, unsigned pNum, |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1247 | DeclGroupPtrTy *allTUVars, unsigned tuvNum) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1248 | Decl *ClassDecl = classDecl.getAs<Decl>(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1249 | |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1250 | // FIXME: If we don't have a ClassDecl, we have an error. We should consider |
| 1251 | // always passing in a decl. If the decl has an error, isInvalidDecl() |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1252 | // should be true. |
| 1253 | if (!ClassDecl) |
| 1254 | return; |
| 1255 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1256 | bool isInterfaceDeclKind = |
Chris Lattner | 2d1c431 | 2008-03-16 21:17:37 +0000 | [diff] [blame] | 1257 | isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl) |
| 1258 | || isa<ObjCProtocolDecl>(ClassDecl); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1259 | bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1260 | |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1261 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
Steve Naroff | ab63fd6 | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 1262 | |
| 1263 | // FIXME: Remove these and use the ObjCContainerDecl/DeclContext. |
| 1264 | llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap; |
| 1265 | llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap; |
| 1266 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1267 | for (unsigned i = 0; i < allNum; i++ ) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1268 | ObjCMethodDecl *Method = |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1269 | cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1270 | |
| 1271 | if (!Method) continue; // Already issued a diagnostic. |
Douglas Gregor | 5d76484 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1272 | if (Method->isInstanceMethod()) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1273 | /// Check for instance method of the same name with incompatible types |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1274 | const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1275 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 1276 | : false; |
Eli Friedman | 70c167d | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1277 | if ((isInterfaceDeclKind && PrevMethod && !match) |
| 1278 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1279 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1280 | << Method->getDeclName(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1281 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1282 | } else { |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1283 | DC->addDecl(Method); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1284 | InsMap[Method->getSelector()] = Method; |
| 1285 | /// The following allows us to typecheck messages to "id". |
| 1286 | AddInstanceMethodToGlobalPool(Method); |
| 1287 | } |
| 1288 | } |
| 1289 | else { |
| 1290 | /// Check for class method of the same name with incompatible types |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1291 | const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()]; |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1292 | bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod) |
| 1293 | : false; |
Eli Friedman | 70c167d | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 1294 | if ((isInterfaceDeclKind && PrevMethod && !match) |
| 1295 | || (checkIdenticalMethods && match)) { |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1296 | Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1297 | << Method->getDeclName(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1298 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1299 | } else { |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1300 | DC->addDecl(Method); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1301 | ClsMap[Method->getSelector()] = Method; |
Steve Naroff | fe9eb6a | 2007-12-18 01:30:32 +0000 | [diff] [blame] | 1302 | /// The following allows us to typecheck messages to "Class". |
| 1303 | AddFactoryMethodToGlobalPool(Method); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1307 | if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) { |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1308 | // Compares properties declared in this class to those of its |
Fariborz Jahanian | ed98660 | 2008-05-01 00:03:38 +0000 | [diff] [blame] | 1309 | // super class. |
Fariborz Jahanian | 33973a2 | 2008-05-02 19:17:30 +0000 | [diff] [blame] | 1310 | ComparePropertiesInBaseAndSuper(I); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1311 | MergeProtocolPropertiesIntoClass(I, DeclPtrTy::make(I)); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1312 | } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) { |
Fariborz Jahanian | 37aaf4f | 2008-12-06 19:59:02 +0000 | [diff] [blame] | 1313 | // Categories are used to extend the class by declaring new methods. |
| 1314 | // By the same token, they are also used to add new properties. No |
| 1315 | // need to compare the added property to those in the class. |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1316 | |
Fariborz Jahanian | acad6d1 | 2008-12-06 23:03:39 +0000 | [diff] [blame] | 1317 | // Merge protocol properties into category |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1318 | MergeProtocolPropertiesIntoClass(C, DeclPtrTy::make(C)); |
Fariborz Jahanian | ffb6882 | 2009-03-02 19:05:07 +0000 | [diff] [blame] | 1319 | if (C->getIdentifier() == 0) |
| 1320 | DiagnoseClassExtensionDupMethods(C, C->getClassInterface()); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1321 | } |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1322 | if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) { |
| 1323 | // ProcessPropertyDecl is responsible for diagnosing conflicts with any |
| 1324 | // user-defined setter/getter. It also synthesizes setter/getter methods |
| 1325 | // and adds them to the DeclContext and global method pools. |
Chris Lattner | 2206340 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1326 | for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(), |
| 1327 | E = CDecl->prop_end(); I != E; ++I) |
| 1328 | ProcessPropertyDecl(*I, CDecl); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1329 | CDecl->setAtEndLoc(AtEndLoc); |
| 1330 | } |
| 1331 | if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1332 | IC->setLocEnd(AtEndLoc); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1333 | if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1334 | ImplMethodsVsClassMethods(IC, IDecl); |
Steve Naroff | 451f83c | 2009-01-09 15:36:25 +0000 | [diff] [blame] | 1335 | } else if (ObjCCategoryImplDecl* CatImplClass = |
| 1336 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1337 | CatImplClass->setLocEnd(AtEndLoc); |
Chris Lattner | 2206340 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1338 | |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1339 | // Find category interface decl and then check that all methods declared |
Daniel Dunbar | 0c0160f | 2008-08-27 05:40:03 +0000 | [diff] [blame] | 1340 | // in this interface are implemented in the category @implementation. |
Chris Lattner | 2206340 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1341 | if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) { |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1342 | for (ObjCCategoryDecl *Categories = IDecl->getCategoryList(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1343 | Categories; Categories = Categories->getNextClassCategory()) { |
| 1344 | if (Categories->getIdentifier() == CatImplClass->getIdentifier()) { |
Chris Lattner | 7cc13bf | 2009-03-01 00:56:52 +0000 | [diff] [blame] | 1345 | ImplMethodsVsClassMethods(CatImplClass, Categories); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1346 | break; |
| 1347 | } |
| 1348 | } |
| 1349 | } |
| 1350 | } |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1351 | if (isInterfaceDeclKind) { |
| 1352 | // Reject invalid vardecls. |
| 1353 | for (unsigned i = 0; i != tuvNum; i++) { |
| 1354 | DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>(); |
| 1355 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) |
| 1356 | if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) { |
| 1357 | if (VDecl->getStorageClass() != VarDecl::Extern && |
| 1358 | VDecl->getStorageClass() != VarDecl::PrivateExtern) |
| 1359 | Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass) |
| 1360 | << cast<NamedDecl>(ClassDecl)->getIdentifier(); |
Fariborz Jahanian | ca27732 | 2009-03-21 18:06:45 +0000 | [diff] [blame] | 1361 | } |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1362 | } |
Fariborz Jahanian | 2c7de6d | 2009-03-18 22:33:24 +0000 | [diff] [blame] | 1363 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | |
| 1367 | /// CvtQTToAstBitMask - utility routine to produce an AST bitmask for |
| 1368 | /// objective-c's type qualifier from the parser version of the same info. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1369 | static Decl::ObjCDeclQualifier |
| 1370 | CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) { |
| 1371 | Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None; |
| 1372 | if (PQTVal & ObjCDeclSpec::DQ_In) |
| 1373 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In); |
| 1374 | if (PQTVal & ObjCDeclSpec::DQ_Inout) |
| 1375 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout); |
| 1376 | if (PQTVal & ObjCDeclSpec::DQ_Out) |
| 1377 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out); |
| 1378 | if (PQTVal & ObjCDeclSpec::DQ_Bycopy) |
| 1379 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy); |
| 1380 | if (PQTVal & ObjCDeclSpec::DQ_Byref) |
| 1381 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref); |
| 1382 | if (PQTVal & ObjCDeclSpec::DQ_Oneway) |
| 1383 | ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1384 | |
| 1385 | return ret; |
| 1386 | } |
| 1387 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1388 | Sema::DeclPtrTy Sema::ActOnMethodDeclaration( |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1389 | SourceLocation MethodLoc, SourceLocation EndLoc, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1390 | tok::TokenKind MethodType, DeclPtrTy classDecl, |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1391 | ObjCDeclSpec &ReturnQT, TypeTy *ReturnType, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1392 | Selector Sel, |
| 1393 | // optional arguments. The number of types/arguments is obtained |
| 1394 | // from the Sel.getNumArgs(). |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1395 | ObjCDeclSpec *ArgQT, TypeTy **ArgTypes, IdentifierInfo **ArgNames, |
Fariborz Jahanian | 89d5304 | 2009-01-09 00:38:19 +0000 | [diff] [blame] | 1396 | llvm::SmallVectorImpl<Declarator> &Cdecls, |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1397 | AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind, |
| 1398 | bool isVariadic) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1399 | Decl *ClassDecl = classDecl.getAs<Decl>(); |
Steve Naroff | 70f1624 | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1400 | |
| 1401 | // Make sure we can establish a context for the method. |
| 1402 | if (!ClassDecl) { |
| 1403 | Diag(MethodLoc, diag::error_missing_method_context); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1404 | return DeclPtrTy(); |
Steve Naroff | 70f1624 | 2008-02-29 21:48:07 +0000 | [diff] [blame] | 1405 | } |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1406 | QualType resultDeclType; |
| 1407 | |
Steve Naroff | a442ad9 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1408 | if (ReturnType) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1409 | resultDeclType = QualType::getFromOpaquePtr(ReturnType); |
Steve Naroff | a442ad9 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1410 | |
| 1411 | // Methods cannot return interface types. All ObjC objects are |
| 1412 | // passed by reference. |
| 1413 | if (resultDeclType->isObjCInterfaceType()) { |
| 1414 | Diag(MethodLoc, diag::err_object_cannot_be_by_value) |
| 1415 | << "returned"; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1416 | return DeclPtrTy(); |
Steve Naroff | a442ad9 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1417 | } |
| 1418 | } else // get the type for "id". |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1419 | resultDeclType = Context.getObjCIdType(); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1420 | |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1421 | ObjCMethodDecl* ObjCMethod = |
| 1422 | ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType, |
Chris Lattner | 3afb2df | 2009-03-29 04:30:19 +0000 | [diff] [blame] | 1423 | cast<DeclContext>(ClassDecl), |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1424 | MethodType == tok::minus, isVariadic, |
Fariborz Jahanian | 5f2e224 | 2008-05-07 20:53:44 +0000 | [diff] [blame] | 1425 | false, |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1426 | MethodDeclKind == tok::objc_optional ? |
| 1427 | ObjCMethodDecl::Optional : |
| 1428 | ObjCMethodDecl::Required); |
| 1429 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1430 | llvm::SmallVector<ParmVarDecl*, 16> Params; |
| 1431 | |
| 1432 | for (unsigned i = 0; i < Sel.getNumArgs(); i++) { |
| 1433 | // FIXME: arg->AttrList must be stored too! |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1434 | QualType argType, originalArgType; |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1435 | |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1436 | if (ArgTypes[i]) { |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1437 | argType = QualType::getFromOpaquePtr(ArgTypes[i]); |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1438 | // Perform the default array/function conversions (C99 6.7.5.3p[7,8]). |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1439 | if (argType->isArrayType()) { // (char *[]) -> (char **) |
| 1440 | originalArgType = argType; |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1441 | argType = Context.getArrayDecayedType(argType); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1442 | } |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1443 | else if (argType->isFunctionType()) |
| 1444 | argType = Context.getPointerType(argType); |
Fariborz Jahanian | 7b4695f | 2009-01-17 21:57:49 +0000 | [diff] [blame] | 1445 | else if (argType->isObjCInterfaceType()) { |
| 1446 | // FIXME! provide more precise location for the parameter |
Steve Naroff | a442ad9 | 2009-02-20 22:59:16 +0000 | [diff] [blame] | 1447 | Diag(MethodLoc, diag::err_object_cannot_be_by_value) |
| 1448 | << "passed"; |
| 1449 | ObjCMethod->setInvalidDecl(); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1450 | return DeclPtrTy(); |
Fariborz Jahanian | 7b4695f | 2009-01-17 21:57:49 +0000 | [diff] [blame] | 1451 | } |
Steve Naroff | 46c8c7e | 2008-12-09 19:36:17 +0000 | [diff] [blame] | 1452 | } else |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1453 | argType = Context.getObjCIdType(); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1454 | ParmVarDecl* Param; |
| 1455 | if (originalArgType.isNull()) |
| 1456 | Param = ParmVarDecl::Create(Context, ObjCMethod, |
| 1457 | SourceLocation(/*FIXME*/), |
| 1458 | ArgNames[i], argType, |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1459 | VarDecl::None, 0); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1460 | else |
Douglas Gregor | 469fc9a | 2009-02-02 23:39:07 +0000 | [diff] [blame] | 1461 | Param = OriginalParmVarDecl::Create(Context, ObjCMethod, |
| 1462 | SourceLocation(/*FIXME*/), |
| 1463 | ArgNames[i], argType, originalArgType, |
| 1464 | VarDecl::None, 0); |
Fariborz Jahanian | e26cb43 | 2008-12-20 23:29:59 +0000 | [diff] [blame] | 1465 | |
Chris Lattner | eee57c0 | 2008-04-04 06:12:32 +0000 | [diff] [blame] | 1466 | Param->setObjCDeclQualifier( |
| 1467 | CvtQTToAstBitMask(ArgQT[i].getObjCDeclQualifier())); |
| 1468 | Params.push_back(Param); |
| 1469 | } |
| 1470 | |
Chris Lattner | df6133b | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 1471 | ObjCMethod->setMethodParams(&Params[0], Sel.getNumArgs(), Context); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1472 | ObjCMethod->setObjCDeclQualifier( |
| 1473 | CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier())); |
| 1474 | const ObjCMethodDecl *PrevMethod = 0; |
Daniel Dunbar | d1d847c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1475 | |
| 1476 | if (AttrList) |
| 1477 | ProcessDeclAttributeList(ObjCMethod, AttrList); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1478 | |
| 1479 | // For implementations (which can be very "coarse grain"), we add the |
| 1480 | // method now. This allows the AST to implement lookup methods that work |
| 1481 | // incrementally (without waiting until we parse the @end). It also allows |
| 1482 | // us to flag multiple declaration errors as they occur. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1483 | if (ObjCImplementationDecl *ImpDecl = |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1484 | dyn_cast<ObjCImplementationDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1485 | if (MethodType == tok::minus) { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1486 | PrevMethod = ImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1487 | ImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1488 | } else { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1489 | PrevMethod = ImpDecl->getClassMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1490 | ImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1491 | } |
| 1492 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1493 | else if (ObjCCategoryImplDecl *CatImpDecl = |
Chris Lattner | 114add6 | 2008-03-16 00:49:28 +0000 | [diff] [blame] | 1494 | dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) { |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1495 | if (MethodType == tok::minus) { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1496 | PrevMethod = CatImpDecl->getInstanceMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1497 | CatImpDecl->addInstanceMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1498 | } else { |
Steve Naroff | 74273de | 2007-12-19 22:27:04 +0000 | [diff] [blame] | 1499 | PrevMethod = CatImpDecl->getClassMethod(Sel); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1500 | CatImpDecl->addClassMethod(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1501 | } |
| 1502 | } |
| 1503 | if (PrevMethod) { |
| 1504 | // You can never have two method definitions with the same name. |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1505 | Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl) |
Chris Lattner | 3a8f294 | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1506 | << ObjCMethod->getDeclName(); |
Chris Lattner | 1336cab | 2008-11-23 23:12:31 +0000 | [diff] [blame] | 1507 | Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1508 | } |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1509 | return DeclPtrTy::make(ObjCMethod); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1512 | void Sema::CheckObjCPropertyAttributes(QualType PropertyTy, |
| 1513 | SourceLocation Loc, |
| 1514 | unsigned &Attributes) { |
| 1515 | // FIXME: Improve the reported location. |
| 1516 | |
Fariborz Jahanian | 7ad911c | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 1517 | // readonly and readwrite/assign/retain/copy conflict. |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1518 | if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
Fariborz Jahanian | 7ad911c | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 1519 | (Attributes & (ObjCDeclSpec::DQ_PR_readwrite | |
| 1520 | ObjCDeclSpec::DQ_PR_assign | |
| 1521 | ObjCDeclSpec::DQ_PR_copy | |
| 1522 | ObjCDeclSpec::DQ_PR_retain))) { |
| 1523 | const char * which = (Attributes & ObjCDeclSpec::DQ_PR_readwrite) ? |
| 1524 | "readwrite" : |
| 1525 | (Attributes & ObjCDeclSpec::DQ_PR_assign) ? |
| 1526 | "assign" : |
| 1527 | (Attributes & ObjCDeclSpec::DQ_PR_copy) ? |
| 1528 | "copy" : "retain"; |
| 1529 | |
Fariborz Jahanian | f189290 | 2008-12-08 19:28:10 +0000 | [diff] [blame] | 1530 | Diag(Loc, (Attributes & (ObjCDeclSpec::DQ_PR_readwrite)) ? |
Chris Lattner | 0eab08e | 2009-01-29 18:49:48 +0000 | [diff] [blame] | 1531 | diag::err_objc_property_attr_mutually_exclusive : |
| 1532 | diag::warn_objc_property_attr_mutually_exclusive) |
Fariborz Jahanian | 7ad911c | 2008-12-06 01:12:43 +0000 | [diff] [blame] | 1533 | << "readonly" << which; |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | // Check for copy or retain on non-object types. |
| 1537 | if ((Attributes & (ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain)) && |
| 1538 | !Context.isObjCObjectPointerType(PropertyTy)) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1539 | Diag(Loc, diag::err_objc_property_requires_object) |
| 1540 | << (Attributes & ObjCDeclSpec::DQ_PR_copy ? "copy" : "retain"); |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1541 | Attributes &= ~(ObjCDeclSpec::DQ_PR_copy | ObjCDeclSpec::DQ_PR_retain); |
| 1542 | } |
| 1543 | |
| 1544 | // Check for more than one of { assign, copy, retain }. |
| 1545 | if (Attributes & ObjCDeclSpec::DQ_PR_assign) { |
| 1546 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1547 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1548 | << "assign" << "copy"; |
| 1549 | Attributes &= ~ObjCDeclSpec::DQ_PR_copy; |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1550 | } |
| 1551 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1552 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1553 | << "assign" << "retain"; |
| 1554 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1555 | } |
| 1556 | } else if (Attributes & ObjCDeclSpec::DQ_PR_copy) { |
| 1557 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1558 | Diag(Loc, diag::err_objc_property_attr_mutually_exclusive) |
| 1559 | << "copy" << "retain"; |
| 1560 | Attributes &= ~ObjCDeclSpec::DQ_PR_retain; |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | // Warn if user supplied no assignment attribute, property is |
| 1565 | // readwrite, and this is an object type. |
| 1566 | if (!(Attributes & (ObjCDeclSpec::DQ_PR_assign | ObjCDeclSpec::DQ_PR_copy | |
| 1567 | ObjCDeclSpec::DQ_PR_retain)) && |
| 1568 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly) && |
| 1569 | Context.isObjCObjectPointerType(PropertyTy)) { |
| 1570 | // Skip this warning in gc-only mode. |
| 1571 | if (getLangOptions().getGCMode() != LangOptions::GCOnly) |
| 1572 | Diag(Loc, diag::warn_objc_property_no_assignment_attribute); |
| 1573 | |
| 1574 | // If non-gc code warn that this is likely inappropriate. |
| 1575 | if (getLangOptions().getGCMode() == LangOptions::NonGC) |
| 1576 | Diag(Loc, diag::warn_objc_property_default_assign_on_object); |
| 1577 | |
| 1578 | // FIXME: Implement warning dependent on NSCopying being |
| 1579 | // implemented. See also: |
| 1580 | // <rdar://5168496&4855821&5607453&5096644&4947311&5698469&4947014&5168496> |
| 1581 | // (please trim this list while you are at it). |
| 1582 | } |
| 1583 | } |
| 1584 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1585 | Sema::DeclPtrTy Sema::ActOnProperty(Scope *S, SourceLocation AtLoc, |
| 1586 | FieldDeclarator &FD, |
| 1587 | ObjCDeclSpec &ODS, |
| 1588 | Selector GetterSel, |
| 1589 | Selector SetterSel, |
| 1590 | DeclPtrTy ClassCategory, |
| 1591 | bool *isOverridingProperty, |
| 1592 | tok::ObjCKeywordKind MethodImplKind) { |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1593 | unsigned Attributes = ODS.getPropertyAttributes(); |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1594 | bool isReadWrite = ((Attributes & ObjCDeclSpec::DQ_PR_readwrite) || |
| 1595 | // default is readwrite! |
| 1596 | !(Attributes & ObjCDeclSpec::DQ_PR_readonly)); |
| 1597 | // property is defaulted to 'assign' if it is readwrite and is |
| 1598 | // not retain or copy |
| 1599 | bool isAssign = ((Attributes & ObjCDeclSpec::DQ_PR_assign) || |
| 1600 | (isReadWrite && |
| 1601 | !(Attributes & ObjCDeclSpec::DQ_PR_retain) && |
| 1602 | !(Attributes & ObjCDeclSpec::DQ_PR_copy))); |
| 1603 | QualType T = GetTypeForDeclarator(FD.D, S); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1604 | Decl *ClassDecl = ClassCategory.getAs<Decl>(); |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1605 | ObjCInterfaceDecl *CCPrimary = 0; // continuation class's primary class |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1606 | // May modify Attributes. |
| 1607 | CheckObjCPropertyAttributes(T, AtLoc, Attributes); |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1608 | if (ObjCCategoryDecl *CDecl = dyn_cast<ObjCCategoryDecl>(ClassDecl)) |
| 1609 | if (!CDecl->getIdentifier()) { |
Fariborz Jahanian | 89aa1e0 | 2009-04-01 23:23:53 +0000 | [diff] [blame] | 1610 | // This is a continuation class. property requires special |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1611 | // handling. |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1612 | if ((CCPrimary = CDecl->getClassInterface())) { |
| 1613 | // Find the property in continuation class's primary class only. |
| 1614 | ObjCPropertyDecl *PIDecl = 0; |
| 1615 | IdentifierInfo *PropertyId = FD.D.getIdentifier(); |
| 1616 | for (ObjCInterfaceDecl::prop_iterator I = CCPrimary->prop_begin(), |
| 1617 | E = CCPrimary->prop_end(); I != E; ++I) |
| 1618 | if ((*I)->getIdentifier() == PropertyId) { |
| 1619 | PIDecl = *I; |
| 1620 | break; |
| 1621 | } |
| 1622 | |
| 1623 | if (PIDecl) { |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1624 | // property 'PIDecl's readonly attribute will be over-ridden |
Fariborz Jahanian | 89aa1e0 | 2009-04-01 23:23:53 +0000 | [diff] [blame] | 1625 | // with continuation class's readwrite property attribute! |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1626 | unsigned PIkind = PIDecl->getPropertyAttributes(); |
| 1627 | if (isReadWrite && (PIkind & ObjCPropertyDecl::OBJC_PR_readonly)) { |
Fariborz Jahanian | 965242e | 2008-12-08 18:47:29 +0000 | [diff] [blame] | 1628 | if ((Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) != |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1629 | (PIkind & ObjCPropertyDecl::OBJC_PR_nonatomic)) |
| 1630 | Diag(AtLoc, diag::warn_property_attr_mismatch); |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1631 | PIDecl->makeitReadWriteAttribute(); |
| 1632 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
| 1633 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 1634 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
| 1635 | PIDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 1636 | PIDecl->setSetterName(SetterSel); |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1637 | // FIXME: use a common routine with addPropertyMethods. |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1638 | ObjCMethodDecl *SetterDecl = |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1639 | ObjCMethodDecl::Create(Context, AtLoc, AtLoc, SetterSel, |
| 1640 | Context.VoidTy, |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1641 | CCPrimary, |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1642 | true, false, true, |
| 1643 | ObjCMethodDecl::Required); |
Chris Lattner | df6133b | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 1644 | ParmVarDecl *Argument = ParmVarDecl::Create(Context, SetterDecl, |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1645 | SourceLocation(), |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1646 | PropertyId, |
Chris Lattner | df6133b | 2009-02-20 21:35:13 +0000 | [diff] [blame] | 1647 | T, VarDecl::None, 0); |
| 1648 | SetterDecl->setMethodParams(&Argument, 1, Context); |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1649 | PIDecl->setSetterMethodDecl(SetterDecl); |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1650 | } |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1651 | else |
| 1652 | Diag(AtLoc, diag::err_use_continuation_class) |
| 1653 | << CCPrimary->getDeclName(); |
| 1654 | *isOverridingProperty = true; |
| 1655 | return DeclPtrTy(); |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1656 | } |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1657 | // No matching property found in the primary class. Just fall thru |
| 1658 | // and add property to continuation class's primary class. |
| 1659 | ClassDecl = CCPrimary; |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1660 | } else { |
Chris Lattner | a5d4771 | 2009-02-20 21:38:52 +0000 | [diff] [blame] | 1661 | Diag(CDecl->getLocation(), diag::err_continuation_class); |
| 1662 | *isOverridingProperty = true; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1663 | return DeclPtrTy(); |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1664 | } |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1665 | } |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1666 | |
Fariborz Jahanian | f8bfa0c | 2008-12-16 17:51:01 +0000 | [diff] [blame] | 1667 | Type *t = T.getTypePtr(); |
| 1668 | if (t->isArrayType() || t->isFunctionType()) |
| 1669 | Diag(AtLoc, diag::err_property_type) << T; |
| 1670 | |
Steve Naroff | dcf1e84 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1671 | DeclContext *DC = dyn_cast<DeclContext>(ClassDecl); |
| 1672 | assert(DC && "ClassDecl is not a DeclContext"); |
| 1673 | ObjCPropertyDecl *PDecl = ObjCPropertyDecl::Create(Context, DC, AtLoc, |
Fariborz Jahanian | 0ceb4be | 2008-04-14 23:36:35 +0000 | [diff] [blame] | 1674 | FD.D.getIdentifier(), T); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1675 | DC->addDecl(PDecl); |
Chris Lattner | 2206340 | 2009-02-16 18:32:47 +0000 | [diff] [blame] | 1676 | |
| 1677 | ProcessDeclAttributes(PDecl, FD.D); |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1678 | |
Fariborz Jahanian | e4534e7 | 2008-05-07 17:43:59 +0000 | [diff] [blame] | 1679 | // Regardless of setter/getter attribute, we save the default getter/setter |
| 1680 | // selector names in anticipation of declaration of setter/getter methods. |
| 1681 | PDecl->setGetterName(GetterSel); |
| 1682 | PDecl->setSetterName(SetterSel); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1683 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1684 | if (Attributes & ObjCDeclSpec::DQ_PR_readonly) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1685 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1686 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1687 | if (Attributes & ObjCDeclSpec::DQ_PR_getter) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1688 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1689 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1690 | if (Attributes & ObjCDeclSpec::DQ_PR_setter) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1691 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1692 | |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1693 | if (isReadWrite) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1694 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1695 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1696 | if (Attributes & ObjCDeclSpec::DQ_PR_retain) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1697 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1698 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1699 | if (Attributes & ObjCDeclSpec::DQ_PR_copy) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1700 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1701 | |
Fariborz Jahanian | bcda0b4 | 2008-11-26 20:01:34 +0000 | [diff] [blame] | 1702 | if (isAssign) |
| 1703 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 1704 | |
Daniel Dunbar | 540ff47 | 2008-09-23 21:53:23 +0000 | [diff] [blame] | 1705 | if (Attributes & ObjCDeclSpec::DQ_PR_nonatomic) |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1706 | PDecl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1707 | |
Fariborz Jahanian | 4aa72a7 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 1708 | if (MethodImplKind == tok::objc_required) |
| 1709 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Required); |
| 1710 | else if (MethodImplKind == tok::objc_optional) |
| 1711 | PDecl->setPropertyImplementation(ObjCPropertyDecl::Optional); |
Fariborz Jahanian | 304f7c7 | 2009-04-02 18:44:20 +0000 | [diff] [blame] | 1712 | // A case of continuation class adding a new property in the class. This |
| 1713 | // is not what it was meant for. However, gcc supports it and so should we. |
| 1714 | // Make sure setter/getters are declared here. |
| 1715 | if (CCPrimary) |
| 1716 | ProcessPropertyDecl(PDecl, CCPrimary); |
Fariborz Jahanian | 4aa72a7 | 2008-05-05 18:51:55 +0000 | [diff] [blame] | 1717 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1718 | return DeclPtrTy::make(PDecl); |
Chris Lattner | 855e51f | 2007-12-12 07:09:47 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1721 | /// ActOnPropertyImplDecl - This routine performs semantic checks and |
| 1722 | /// builds the AST node for a property implementation declaration; declared |
| 1723 | /// as @synthesize or @dynamic. |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1724 | /// |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1725 | Sema::DeclPtrTy Sema::ActOnPropertyImplDecl(SourceLocation AtLoc, |
| 1726 | SourceLocation PropertyLoc, |
| 1727 | bool Synthesize, |
| 1728 | DeclPtrTy ClassCatImpDecl, |
| 1729 | IdentifierInfo *PropertyId, |
| 1730 | IdentifierInfo *PropertyIvar) { |
| 1731 | Decl *ClassImpDecl = ClassCatImpDecl.getAs<Decl>(); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1732 | // Make sure we have a context for the property implementation declaration. |
| 1733 | if (!ClassImpDecl) { |
| 1734 | Diag(AtLoc, diag::error_missing_property_context); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1735 | return DeclPtrTy(); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1736 | } |
| 1737 | ObjCPropertyDecl *property = 0; |
| 1738 | ObjCInterfaceDecl* IDecl = 0; |
| 1739 | // Find the class or category class where this property must have |
| 1740 | // a declaration. |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1741 | ObjCImplementationDecl *IC = 0; |
| 1742 | ObjCCategoryImplDecl* CatImplClass = 0; |
| 1743 | if ((IC = dyn_cast<ObjCImplementationDecl>(ClassImpDecl))) { |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1744 | IDecl = IC->getClassInterface(); |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1745 | // We always synthesize an interface for an implementation |
| 1746 | // without an interface decl. So, IDecl is always non-zero. |
| 1747 | assert(IDecl && |
| 1748 | "ActOnPropertyImplDecl - @implementation without @interface"); |
| 1749 | |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1750 | // Look for this property declaration in the @implementation's @interface |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1751 | property = IDecl->FindPropertyDeclaration(PropertyId); |
| 1752 | if (!property) { |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1753 | Diag(PropertyLoc, diag::error_bad_property_decl) << IDecl->getDeclName(); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1754 | return DeclPtrTy(); |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1755 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1756 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1757 | else if ((CatImplClass = dyn_cast<ObjCCategoryImplDecl>(ClassImpDecl))) { |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1758 | if (Synthesize) { |
| 1759 | Diag(AtLoc, diag::error_synthesize_category_decl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1760 | return DeclPtrTy(); |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1761 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1762 | IDecl = CatImplClass->getClassInterface(); |
| 1763 | if (!IDecl) { |
| 1764 | Diag(AtLoc, diag::error_missing_property_interface); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1765 | return DeclPtrTy(); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1766 | } |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1767 | ObjCCategoryDecl *Category = |
| 1768 | IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier()); |
| 1769 | |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1770 | // If category for this implementation not found, it is an error which |
| 1771 | // has already been reported eralier. |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1772 | if (!Category) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1773 | return DeclPtrTy(); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1774 | // Look for this property declaration in @implementation's category |
Fariborz Jahanian | ef8a3df | 2008-04-21 19:04:53 +0000 | [diff] [blame] | 1775 | property = Category->FindPropertyDeclaration(PropertyId); |
| 1776 | if (!property) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1777 | Diag(PropertyLoc, diag::error_bad_category_property_decl) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1778 | << Category->getDeclName(); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1779 | return DeclPtrTy(); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1780 | } |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1781 | } else { |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1782 | Diag(AtLoc, diag::error_bad_property_context); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1783 | return DeclPtrTy(); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1784 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1785 | ObjCIvarDecl *Ivar = 0; |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1786 | // Check that we have a valid, previously declared ivar for @synthesize |
| 1787 | if (Synthesize) { |
| 1788 | // @synthesize |
Fariborz Jahanian | 1f02800 | 2008-04-21 21:57:36 +0000 | [diff] [blame] | 1789 | if (!PropertyIvar) |
| 1790 | PropertyIvar = PropertyId; |
Fariborz Jahanian | c625fa9 | 2009-03-31 00:06:29 +0000 | [diff] [blame] | 1791 | QualType PropType = Context.getCanonicalType(property->getType()); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1792 | // Check that this is a previously declared 'ivar' in 'IDecl' interface |
Fariborz Jahanian | beae78e | 2009-02-16 19:35:27 +0000 | [diff] [blame] | 1793 | Ivar = IDecl->lookupInstanceVariable(PropertyIvar); |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1794 | if (!Ivar) { |
Fariborz Jahanian | c625fa9 | 2009-03-31 00:06:29 +0000 | [diff] [blame] | 1795 | if (getLangOptions().ObjCNonFragileABI) { |
| 1796 | Ivar = ObjCIvarDecl::Create(Context, CurContext, PropertyLoc, |
| 1797 | PropertyIvar, PropType, |
| 1798 | ObjCIvarDecl::Private, |
| 1799 | (Expr *)0); |
| 1800 | property->setPropertyIvarDecl(Ivar); |
| 1801 | } |
| 1802 | else { |
Steve Naroff | 92aeef3 | 2009-03-03 22:09:41 +0000 | [diff] [blame] | 1803 | Diag(PropertyLoc, diag::error_missing_property_ivar_decl) << PropertyId; |
Fariborz Jahanian | c625fa9 | 2009-03-31 00:06:29 +0000 | [diff] [blame] | 1804 | return DeclPtrTy(); |
| 1805 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1806 | } |
Steve Naroff | c32e45c | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 1807 | QualType IvarType = Context.getCanonicalType(Ivar->getType()); |
| 1808 | |
Steve Naroff | 631e392 | 2008-09-30 00:24:17 +0000 | [diff] [blame] | 1809 | // Check that type of property and its ivar are type compatible. |
Steve Naroff | c32e45c | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 1810 | if (PropType != IvarType) { |
Steve Naroff | 3557a34 | 2008-10-16 14:59:30 +0000 | [diff] [blame] | 1811 | if (CheckAssignmentConstraints(PropType, IvarType) != Compatible) { |
Chris Lattner | 8d75681 | 2008-11-20 06:13:02 +0000 | [diff] [blame] | 1812 | Diag(PropertyLoc, diag::error_property_ivar_type) |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1813 | << property->getDeclName() << Ivar->getDeclName(); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1814 | return DeclPtrTy(); |
Steve Naroff | c32e45c | 2008-09-30 10:07:56 +0000 | [diff] [blame] | 1815 | } |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1816 | |
| 1817 | // FIXME! Rules for properties are somewhat different that those |
| 1818 | // for assignments. Use a new routine to consolidate all cases; |
| 1819 | // specifically for property redeclarations as well as for ivars. |
| 1820 | QualType lhsType =Context.getCanonicalType(PropType).getUnqualifiedType(); |
| 1821 | QualType rhsType =Context.getCanonicalType(IvarType).getUnqualifiedType(); |
| 1822 | if (lhsType != rhsType && |
| 1823 | lhsType->isArithmeticType()) { |
| 1824 | Diag(PropertyLoc, diag::error_property_ivar_type) |
| 1825 | << property->getDeclName() << Ivar->getDeclName(); |
| 1826 | return DeclPtrTy(); |
| 1827 | } |
| 1828 | // __weak is explicit. So it works on Canonical type. |
| 1829 | if (PropType.isObjCGCWeak() && !IvarType.isObjCGCWeak()) { |
| 1830 | Diag(PropertyLoc, diag::error_weak_property) |
| 1831 | << property->getDeclName() << Ivar->getDeclName(); |
| 1832 | return DeclPtrTy(); |
| 1833 | } |
| 1834 | if ((Context.isObjCObjectPointerType(property->getType()) || |
| 1835 | PropType.isObjCGCStrong()) && IvarType.isObjCGCWeak()) { |
| 1836 | Diag(PropertyLoc, diag::error_strong_property) |
| 1837 | << property->getDeclName() << Ivar->getDeclName(); |
| 1838 | return DeclPtrTy(); |
Fariborz Jahanian | 6ec8955 | 2009-01-19 20:13:47 +0000 | [diff] [blame] | 1839 | } |
Fariborz Jahanian | 900e3dc | 2008-04-21 21:05:54 +0000 | [diff] [blame] | 1840 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1841 | } else if (PropertyIvar) { |
| 1842 | // @dynamic |
| 1843 | Diag(PropertyLoc, diag::error_dynamic_property_ivar_decl); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1844 | return DeclPtrTy(); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1845 | } |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1846 | assert (property && "ActOnPropertyImplDecl - property declaration missing"); |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1847 | ObjCPropertyImplDecl *PIDecl = |
Douglas Gregor | 6e4fa2c | 2009-01-09 00:49:46 +0000 | [diff] [blame] | 1848 | ObjCPropertyImplDecl::Create(Context, CurContext, AtLoc, PropertyLoc, |
| 1849 | property, |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1850 | (Synthesize ? |
Daniel Dunbar | 14117fc | 2008-08-26 04:47:31 +0000 | [diff] [blame] | 1851 | ObjCPropertyImplDecl::Synthesize |
| 1852 | : ObjCPropertyImplDecl::Dynamic), |
| 1853 | Ivar); |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1854 | CurContext->addDecl(PIDecl); |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1855 | if (IC) { |
| 1856 | if (Synthesize) |
| 1857 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1858 | IC->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 1859 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 1860 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1861 | << PropertyIvar; |
| 1862 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1863 | } |
| 1864 | |
| 1865 | if (ObjCPropertyImplDecl *PPIDecl = IC->FindPropertyImplDecl(PropertyId)) { |
| 1866 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 1867 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1868 | return DeclPtrTy(); |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1869 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1870 | IC->addPropertyImplementation(PIDecl); |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1871 | } |
| 1872 | else { |
| 1873 | if (Synthesize) |
| 1874 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1875 | CatImplClass->FindPropertyImplIvarDecl(PropertyIvar)) { |
| 1876 | Diag(PropertyLoc, diag::error_duplicate_ivar_use) |
| 1877 | << PropertyId << PPIDecl->getPropertyDecl()->getIdentifier() |
| 1878 | << PropertyIvar; |
| 1879 | Diag(PPIDecl->getLocation(), diag::note_previous_use); |
| 1880 | } |
| 1881 | |
| 1882 | if (ObjCPropertyImplDecl *PPIDecl = |
| 1883 | CatImplClass->FindPropertyImplDecl(PropertyId)) { |
| 1884 | Diag(PropertyLoc, diag::error_property_implemented) << PropertyId; |
| 1885 | Diag(PPIDecl->getLocation(), diag::note_previous_declaration); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1886 | return DeclPtrTy(); |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1887 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1888 | CatImplClass->addPropertyImplementation(PIDecl); |
Fariborz Jahanian | 6834228 | 2008-12-05 22:32:48 +0000 | [diff] [blame] | 1889 | } |
Fariborz Jahanian | dc0569e | 2008-04-23 00:06:01 +0000 | [diff] [blame] | 1890 | |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1891 | return DeclPtrTy::make(PIDecl); |
Fariborz Jahanian | 78f7e31 | 2008-04-18 00:19:30 +0000 | [diff] [blame] | 1892 | } |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1893 | |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1894 | bool Sema::CheckObjCDeclScope(Decl *D) { |
Douglas Gregor | 69e781f | 2009-01-06 23:51:29 +0000 | [diff] [blame] | 1895 | if (isa<TranslationUnitDecl>(CurContext->getLookupContext())) |
Anders Carlsson | 0a6ab17 | 2008-11-04 16:57:32 +0000 | [diff] [blame] | 1896 | return false; |
| 1897 | |
| 1898 | Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope); |
| 1899 | D->setInvalidDecl(); |
| 1900 | |
| 1901 | return true; |
| 1902 | } |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1903 | |
| 1904 | /// Collect the instance variables declared in an Objective-C object. Used in |
| 1905 | /// the creation of structures from objects using the @defs directive. |
| 1906 | /// FIXME: This should be consolidated with CollectObjCIvars as it is also |
| 1907 | /// part of the AST generation logic of @defs. |
| 1908 | static void CollectIvars(ObjCInterfaceDecl *Class, RecordDecl *Record, |
| 1909 | ASTContext& Ctx, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1910 | llvm::SmallVectorImpl<Sema::DeclPtrTy> &ivars) { |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1911 | if (Class->getSuperClass()) |
| 1912 | CollectIvars(Class->getSuperClass(), Record, Ctx, ivars); |
| 1913 | |
| 1914 | // For each ivar, create a fresh ObjCAtDefsFieldDecl. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1915 | for (ObjCInterfaceDecl::ivar_iterator I = Class->ivar_begin(), |
| 1916 | E = Class->ivar_end(); I != E; ++I) { |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1917 | ObjCIvarDecl* ID = *I; |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1918 | Decl *FD = ObjCAtDefsFieldDecl::Create(Ctx, Record, ID->getLocation(), |
| 1919 | ID->getIdentifier(), ID->getType(), |
| 1920 | ID->getBitWidth()); |
| 1921 | ivars.push_back(Sema::DeclPtrTy::make(FD)); |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | /// Called whenever @defs(ClassName) is encountered in the source. Inserts the |
| 1926 | /// instance variables of ClassName into Decls. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1927 | void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart, |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1928 | IdentifierInfo *ClassName, |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1929 | llvm::SmallVectorImpl<DeclPtrTy> &Decls) { |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1930 | // Check that ClassName is a valid class |
| 1931 | ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName); |
| 1932 | if (!Class) { |
| 1933 | Diag(DeclStart, diag::err_undef_interface) << ClassName; |
| 1934 | return; |
| 1935 | } |
| 1936 | // Collect the instance variables |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1937 | CollectIvars(Class, dyn_cast<RecordDecl>(TagD.getAs<Decl>()), Context, Decls); |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1938 | |
| 1939 | // Introduce all of these fields into the appropriate scope. |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1940 | for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin(); |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1941 | D != Decls.end(); ++D) { |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1942 | FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>()); |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1943 | if (getLangOptions().CPlusPlus) |
| 1944 | PushOnScopeChains(cast<FieldDecl>(FD), S); |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 1945 | else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>())) |
Douglas Gregor | 03b2ad2 | 2009-01-12 23:27:07 +0000 | [diff] [blame] | 1946 | Record->addDecl(FD); |
Chris Lattner | 9bb9abf | 2008-12-17 07:13:27 +0000 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | |