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