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