2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 Copyright 1991-1996 NeXT Software, Inc.
31 #include "objc-private.h"
32 #import <objc/Protocol.h>
34 #include <objc/objc-runtime.h>
37 #include <mach-o/dyld.h>
38 #include <mach-o/ldsyms.h>
40 /* some forward declarations */
42 static struct objc_method_description *
43 lookup_method(struct objc_method_description_list *mlist, SEL aSel);
45 static struct objc_method_description *
46 lookup_class_method(struct objc_protocol_list *plist, SEL aSel);
48 static struct objc_method_description *
49 lookup_instance_method(struct objc_protocol_list *plist, SEL aSel);
51 @implementation Protocol
54 + _fixup: (OBJC_PROTOCOL_PTR)protos numElements: (int) nentries
57 for (i = 0; i < nentries; i++)
59 /* isa has been overloaded by the compiler to indicate version info */
60 protos[i] OBJC_PROTOCOL_DEREF isa = self; // install the class descriptor.
72 hdrs = _getObjcHeaders();
74 for (hp = hdrs; *hp; hp++)
76 p = (OBJC_PROTOCOL_PTR)_getObjcProtocols((headerType*)*hp, &size);
77 if (p && size) { [self _fixup:p numElements: size]; }
84 - (BOOL) conformsTo: (Protocol *)aProtocolObj
89 if (strcmp(aProtocolObj->protocol_name, protocol_name) == 0)
91 else if (protocol_list)
95 for (i = 0; i < protocol_list->count; i++)
97 Protocol *p = protocol_list->list[i];
99 if (strcmp(aProtocolObj->protocol_name, p->protocol_name) == 0)
102 if ([p conformsTo:aProtocolObj])
111 - (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSel
113 struct objc_method_description *m = lookup_method(instance_methods, aSel);
115 if (!m && protocol_list)
116 m = lookup_instance_method(protocol_list, aSel);
121 - (struct objc_method_description *) descriptionForClassMethod:(SEL)aSel
123 struct objc_method_description *m = lookup_method(class_methods, aSel);
125 if (!m && protocol_list)
126 m = lookup_class_method(protocol_list, aSel);
133 return protocol_name;
136 - (BOOL)isEqual:other
138 return [other isKindOf:[Protocol class]] && [self conformsTo: other] && [other conformsTo: self];
147 struct objc_method_description *
148 lookup_method(struct objc_method_description_list *mlist, SEL aSel)
153 for (i = 0; i < mlist->count; i++)
154 if (mlist->list[i].name == aSel)
155 return mlist->list+i;
161 struct objc_method_description *
162 lookup_instance_method(struct objc_protocol_list *plist, SEL aSel)
165 struct objc_method_description *m = 0;
167 for (i = 0; i < plist->count; i++)
169 if (plist->list[i]->instance_methods)
170 m = lookup_method(plist->list[i]->instance_methods, aSel);
172 /* depth first search */
173 if (!m && plist->list[i]->protocol_list)
174 m = lookup_instance_method(plist->list[i]->protocol_list, aSel);
183 struct objc_method_description *
184 lookup_class_method(struct objc_protocol_list *plist, SEL aSel)
187 struct objc_method_description *m = 0;
189 for (i = 0; i < plist->count; i++)
191 if (plist->list[i]->class_methods)
192 m = lookup_method(plist->list[i]->class_methods, aSel);
194 /* depth first search */
195 if (!m && plist->list[i]->protocol_list)
196 m = lookup_class_method(plist->list[i]->protocol_list, aSel);