]> git.ipfire.org Git - thirdparty/gcc.git/blob - libobjc/Protocol.m
In gcc/: 2010-10-06 Nicola Pero <nicola.pero@meta-innovation.com>
[thirdparty/gcc.git] / libobjc / Protocol.m
1 /* This file contains the implementation of class Protocol.
2 Copyright (C) 1993, 2004, 2009 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
24
25 #include "objc-private/common.h"
26 #include "objc/Protocol.h"
27 #include "objc/objc-api.h"
28
29 /* Method description list */
30 struct objc_method_description_list {
31 int count;
32 struct objc_method_description list[1];
33 };
34
35
36 @implementation Protocol
37 {
38 @private
39 char *protocol_name;
40 struct objc_protocol_list *protocol_list;
41 struct objc_method_description_list *instance_methods, *class_methods;
42 }
43
44 /* Obtaining attributes intrinsic to the protocol */
45
46 - (const char *)name
47 {
48 return protocol_name;
49 }
50
51 /* Testing protocol conformance */
52
53 - (BOOL) conformsTo: (Protocol *)aProtocolObject
54 {
55 size_t i;
56 struct objc_protocol_list* proto_list;
57
58 if (aProtocolObject == nil)
59 return NO;
60
61 if (!strcmp(aProtocolObject->protocol_name, self->protocol_name))
62 return YES;
63
64 for (proto_list = protocol_list; proto_list; proto_list = proto_list->next)
65 {
66 for (i=0; i < proto_list->count; i++)
67 {
68 if ([proto_list->list[i] conformsTo: aProtocolObject])
69 return YES;
70 }
71 }
72
73 return NO;
74 }
75
76 /* Looking up information specific to a protocol */
77
78 - (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSel
79 {
80 int i;
81 struct objc_protocol_list* proto_list;
82 const char* name = sel_get_name (aSel);
83 struct objc_method_description *result;
84
85 if (instance_methods)
86 for (i = 0; i < instance_methods->count; i++)
87 {
88 if (!strcmp ((char*)instance_methods->list[i].name, name))
89 return &(instance_methods->list[i]);
90 }
91
92 for (proto_list = protocol_list; proto_list; proto_list = proto_list->next)
93 {
94 size_t j;
95 for (j=0; j < proto_list->count; j++)
96 {
97 if ((result = [proto_list->list[j]
98 descriptionForInstanceMethod: aSel]))
99 return result;
100 }
101 }
102
103 return NULL;
104 }
105
106 - (struct objc_method_description *) descriptionForClassMethod:(SEL)aSel;
107 {
108 int i;
109 struct objc_protocol_list* proto_list;
110 const char* name = sel_get_name (aSel);
111 struct objc_method_description *result;
112
113 if (class_methods)
114 for (i = 0; i < class_methods->count; i++)
115 {
116 if (!strcmp ((char*)class_methods->list[i].name, name))
117 return &(class_methods->list[i]);
118 }
119
120 for (proto_list = protocol_list; proto_list; proto_list = proto_list->next)
121 {
122 size_t j;
123 for (j=0; j < proto_list->count; j++)
124 {
125 if ((result = [proto_list->list[j]
126 descriptionForClassMethod: aSel]))
127 return result;
128 }
129 }
130
131 return NULL;
132 }
133
134 - (unsigned) hash
135 {
136 /* Compute a hash of the protocol_name; use the same hash algorithm
137 * that we use for class names; protocol names and class names are
138 * somewhat similar types of string spaces.
139 */
140 int hash = 0, index;
141
142 for (index = 0; protocol_name[index] != '\0'; index++)
143 {
144 hash = (hash << 4) ^ (hash >> 28) ^ protocol_name[index];
145 }
146
147 hash = (hash ^ (hash >> 10) ^ (hash >> 20));
148
149 return hash;
150 }
151
152 /*
153 * Equality between formal protocols is only formal (nothing to do
154 * with actually checking the list of methods they have!). Two formal
155 * Protocols are equal if and only if they have the same name.
156 *
157 * Please note (for comparisons with other implementations) that
158 * checking the names is equivalent to checking that Protocol A
159 * conforms to Protocol B and Protocol B conforms to Protocol A,
160 * because this happens iff they have the same name. If they have
161 * different names, A conforms to B if and only if A includes B, but
162 * the situation where A includes B and B includes A is a circular
163 * dependency between Protocols which is forbidden by the compiler, so
164 * A conforms to B and B conforms to A with A and B having different
165 * names is an impossible case.
166 */
167 - (BOOL) isEqual: (id)obj
168 {
169 if (obj == self)
170 return YES;
171
172 if ([obj isKindOf: [Protocol class]])
173 {
174 if (strcmp (protocol_name, ((Protocol *)obj)->protocol_name) == 0)
175 return YES;
176 }
177
178 return NO;
179 }
180 @end
181