]> git.saurik.com Git - apple/objc4.git/blob - runtime/Protocol.m
objc4-235.tar.gz
[apple/objc4.git] / runtime / Protocol.m
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 Protocol.h
27 Copyright 1991-1996 NeXT Software, Inc.
28 */
29
30
31 #include "objc-private.h"
32 #import <objc/Protocol.h>
33
34 #include <objc/objc-runtime.h>
35 #include <stdlib.h>
36
37 #include <mach-o/dyld.h>
38 #include <mach-o/ldsyms.h>
39
40 /* some forward declarations */
41
42 static struct objc_method_description *
43 lookup_method(struct objc_method_description_list *mlist, SEL aSel);
44
45 static struct objc_method_description *
46 lookup_class_method(struct objc_protocol_list *plist, SEL aSel);
47
48 static struct objc_method_description *
49 lookup_instance_method(struct objc_protocol_list *plist, SEL aSel);
50
51 @implementation Protocol
52
53
54 + _fixup: (OBJC_PROTOCOL_PTR)protos numElements: (int) nentries
55 {
56 int i;
57 for (i = 0; i < nentries; i++)
58 {
59 /* isa has been overloaded by the compiler to indicate version info */
60 protos[i] OBJC_PROTOCOL_DEREF isa = self; // install the class descriptor.
61 }
62
63 return self;
64 }
65
66 + load
67 {
68 OBJC_PROTOCOL_PTR p;
69 int size;
70 headerType **hp;
71 headerType **hdrs;
72 hdrs = _getObjcHeaders();
73
74 for (hp = hdrs; *hp; hp++)
75 {
76 p = (OBJC_PROTOCOL_PTR)_getObjcProtocols((headerType*)*hp, &size);
77 if (p && size) { [self _fixup:p numElements: size]; }
78 }
79 free (hdrs);
80
81 return self;
82 }
83
84 - (BOOL) conformsTo: (Protocol *)aProtocolObj
85 {
86 if (!aProtocolObj)
87 return NO;
88
89 if (strcmp(aProtocolObj->protocol_name, protocol_name) == 0)
90 return YES;
91 else if (protocol_list)
92 {
93 int i;
94
95 for (i = 0; i < protocol_list->count; i++)
96 {
97 Protocol *p = protocol_list->list[i];
98
99 if (strcmp(aProtocolObj->protocol_name, p->protocol_name) == 0)
100 return YES;
101
102 if ([p conformsTo:aProtocolObj])
103 return YES;
104 }
105 return NO;
106 }
107 else
108 return NO;
109 }
110
111 - (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSel
112 {
113 struct objc_method_description *m = lookup_method(instance_methods, aSel);
114
115 if (!m && protocol_list)
116 m = lookup_instance_method(protocol_list, aSel);
117
118 return m;
119 }
120
121 - (struct objc_method_description *) descriptionForClassMethod:(SEL)aSel
122 {
123 struct objc_method_description *m = lookup_method(class_methods, aSel);
124
125 if (!m && protocol_list)
126 m = lookup_class_method(protocol_list, aSel);
127
128 return m;
129 }
130
131 - (const char *)name
132 {
133 return protocol_name;
134 }
135
136 - (BOOL)isEqual:other
137 {
138 return [other isKindOf:[Protocol class]] && [self conformsTo: other] && [other conformsTo: self];
139 }
140
141 - (unsigned int)hash
142 {
143 return 23;
144 }
145
146 static
147 struct objc_method_description *
148 lookup_method(struct objc_method_description_list *mlist, SEL aSel)
149 {
150 if (mlist)
151 {
152 int i;
153 for (i = 0; i < mlist->count; i++)
154 if (mlist->list[i].name == aSel)
155 return mlist->list+i;
156 }
157 return 0;
158 }
159
160 static
161 struct objc_method_description *
162 lookup_instance_method(struct objc_protocol_list *plist, SEL aSel)
163 {
164 int i;
165 struct objc_method_description *m = 0;
166
167 for (i = 0; i < plist->count; i++)
168 {
169 if (plist->list[i]->instance_methods)
170 m = lookup_method(plist->list[i]->instance_methods, aSel);
171
172 /* depth first search */
173 if (!m && plist->list[i]->protocol_list)
174 m = lookup_instance_method(plist->list[i]->protocol_list, aSel);
175
176 if (m)
177 return m;
178 }
179 return 0;
180 }
181
182 static
183 struct objc_method_description *
184 lookup_class_method(struct objc_protocol_list *plist, SEL aSel)
185 {
186 int i;
187 struct objc_method_description *m = 0;
188
189 for (i = 0; i < plist->count; i++)
190 {
191 if (plist->list[i]->class_methods)
192 m = lookup_method(plist->list[i]->class_methods, aSel);
193
194 /* depth first search */
195 if (!m && plist->list[i]->protocol_list)
196 m = lookup_class_method(plist->list[i]->protocol_list, aSel);
197
198 if (m)
199 return m;
200 }
201 return 0;
202 }
203
204 @end