| 1 |
14 |
jlechner |
/* This file contains the implementation of class Protocol.
|
| 2 |
|
|
Copyright (C) 1993, 2004 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 2, 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 |
|
|
You should have received a copy of the GNU General Public License
|
| 17 |
|
|
along with GCC; see the file COPYING. If not, write to
|
| 18 |
|
|
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
|
| 19 |
|
|
Boston, MA 02110-1301, USA. */
|
| 20 |
|
|
|
| 21 |
|
|
/* As a special exception, if you link this library with files
|
| 22 |
|
|
compiled with GCC to produce an executable, this does not cause
|
| 23 |
|
|
the resulting executable to be covered by the GNU General Public License.
|
| 24 |
|
|
This exception does not however invalidate any other reasons why
|
| 25 |
|
|
the executable file might be covered by the GNU General Public License. */
|
| 26 |
|
|
|
| 27 |
|
|
#include "objc/Protocol.h"
|
| 28 |
|
|
#include "objc/objc-api.h"
|
| 29 |
|
|
|
| 30 |
|
|
/* Method description list */
|
| 31 |
|
|
struct objc_method_description_list {
|
| 32 |
|
|
int count;
|
| 33 |
|
|
struct objc_method_description list[1];
|
| 34 |
|
|
};
|
| 35 |
|
|
|
| 36 |
|
|
|
| 37 |
|
|
@implementation Protocol
|
| 38 |
|
|
{
|
| 39 |
|
|
@private
|
| 40 |
|
|
char *protocol_name;
|
| 41 |
|
|
struct objc_protocol_list *protocol_list;
|
| 42 |
|
|
struct objc_method_description_list *instance_methods, *class_methods;
|
| 43 |
|
|
}
|
| 44 |
|
|
|
| 45 |
|
|
/* Obtaining attributes intrinsic to the protocol */
|
| 46 |
|
|
|
| 47 |
|
|
- (const char *)name
|
| 48 |
|
|
{
|
| 49 |
|
|
return protocol_name;
|
| 50 |
|
|
}
|
| 51 |
|
|
|
| 52 |
|
|
/* Testing protocol conformance */
|
| 53 |
|
|
|
| 54 |
|
|
- (BOOL) conformsTo: (Protocol *)aProtocolObject
|
| 55 |
|
|
{
|
| 56 |
|
|
size_t i;
|
| 57 |
|
|
struct objc_protocol_list* proto_list;
|
| 58 |
|
|
|
| 59 |
|
|
if (aProtocolObject == nil)
|
| 60 |
|
|
return NO;
|
| 61 |
|
|
|
| 62 |
|
|
if (!strcmp(aProtocolObject->protocol_name, self->protocol_name))
|
| 63 |
|
|
return YES;
|
| 64 |
|
|
|
| 65 |
|
|
for (proto_list = protocol_list; proto_list; proto_list = proto_list->next)
|
| 66 |
|
|
{
|
| 67 |
|
|
for (i=0; i < proto_list->count; i++)
|
| 68 |
|
|
{
|
| 69 |
|
|
if ([proto_list->list[i] conformsTo: aProtocolObject])
|
| 70 |
|
|
return YES;
|
| 71 |
|
|
}
|
| 72 |
|
|
}
|
| 73 |
|
|
|
| 74 |
|
|
return NO;
|
| 75 |
|
|
}
|
| 76 |
|
|
|
| 77 |
|
|
/* Looking up information specific to a protocol */
|
| 78 |
|
|
|
| 79 |
|
|
- (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSel
|
| 80 |
|
|
{
|
| 81 |
|
|
int i;
|
| 82 |
|
|
struct objc_protocol_list* proto_list;
|
| 83 |
|
|
const char* name = sel_get_name (aSel);
|
| 84 |
|
|
struct objc_method_description *result;
|
| 85 |
|
|
|
| 86 |
|
|
if (instance_methods)
|
| 87 |
|
|
for (i = 0; i < instance_methods->count; i++)
|
| 88 |
|
|
{
|
| 89 |
|
|
if (!strcmp ((char*)instance_methods->list[i].name, name))
|
| 90 |
|
|
return &(instance_methods->list[i]);
|
| 91 |
|
|
}
|
| 92 |
|
|
|
| 93 |
|
|
for (proto_list = protocol_list; proto_list; proto_list = proto_list->next)
|
| 94 |
|
|
{
|
| 95 |
|
|
size_t j;
|
| 96 |
|
|
for (j=0; j < proto_list->count; j++)
|
| 97 |
|
|
{
|
| 98 |
|
|
if ((result = [proto_list->list[j]
|
| 99 |
|
|
descriptionForInstanceMethod: aSel]))
|
| 100 |
|
|
return result;
|
| 101 |
|
|
}
|
| 102 |
|
|
}
|
| 103 |
|
|
|
| 104 |
|
|
return NULL;
|
| 105 |
|
|
}
|
| 106 |
|
|
|
| 107 |
|
|
- (struct objc_method_description *) descriptionForClassMethod:(SEL)aSel;
|
| 108 |
|
|
{
|
| 109 |
|
|
int i;
|
| 110 |
|
|
struct objc_protocol_list* proto_list;
|
| 111 |
|
|
const char* name = sel_get_name (aSel);
|
| 112 |
|
|
struct objc_method_description *result;
|
| 113 |
|
|
|
| 114 |
|
|
if (class_methods)
|
| 115 |
|
|
for (i = 0; i < class_methods->count; i++)
|
| 116 |
|
|
{
|
| 117 |
|
|
if (!strcmp ((char*)class_methods->list[i].name, name))
|
| 118 |
|
|
return &(class_methods->list[i]);
|
| 119 |
|
|
}
|
| 120 |
|
|
|
| 121 |
|
|
for (proto_list = protocol_list; proto_list; proto_list = proto_list->next)
|
| 122 |
|
|
{
|
| 123 |
|
|
size_t j;
|
| 124 |
|
|
for (j=0; j < proto_list->count; j++)
|
| 125 |
|
|
{
|
| 126 |
|
|
if ((result = [proto_list->list[j]
|
| 127 |
|
|
descriptionForClassMethod: aSel]))
|
| 128 |
|
|
return result;
|
| 129 |
|
|
}
|
| 130 |
|
|
}
|
| 131 |
|
|
|
| 132 |
|
|
return NULL;
|
| 133 |
|
|
}
|
| 134 |
|
|
|
| 135 |
|
|
- (unsigned) hash
|
| 136 |
|
|
{
|
| 137 |
|
|
/* Compute a hash of the protocol_name; use the same hash algorithm
|
| 138 |
|
|
* that we use for class names; protocol names and class names are
|
| 139 |
|
|
* somewhat similar types of string spaces.
|
| 140 |
|
|
*/
|
| 141 |
|
|
int hash = 0, index;
|
| 142 |
|
|
|
| 143 |
|
|
for (index = 0; protocol_name[index] != '\0'; index++)
|
| 144 |
|
|
{
|
| 145 |
|
|
hash = (hash << 4) ^ (hash >> 28) ^ protocol_name[index];
|
| 146 |
|
|
}
|
| 147 |
|
|
|
| 148 |
|
|
hash = (hash ^ (hash >> 10) ^ (hash >> 20));
|
| 149 |
|
|
|
| 150 |
|
|
return hash;
|
| 151 |
|
|
}
|
| 152 |
|
|
|
| 153 |
|
|
/*
|
| 154 |
|
|
* Equality between formal protocols is only formal (nothing to do
|
| 155 |
|
|
* with actually checking the list of methods they have!). Two formal
|
| 156 |
|
|
* Protocols are equal if and only if they have the same name.
|
| 157 |
|
|
*
|
| 158 |
|
|
* Please note (for comparisons with other implementations) that
|
| 159 |
|
|
* checking the names is equivalent to checking that Protocol A
|
| 160 |
|
|
* conforms to Protocol B and Protocol B conforms to Protocol A,
|
| 161 |
|
|
* because this happens iff they have the same name. If they have
|
| 162 |
|
|
* different names, A conforms to B if and only if A includes B, but
|
| 163 |
|
|
* the situation where A includes B and B includes A is a circular
|
| 164 |
|
|
* dependency between Protocols which is forbidden by the compiler, so
|
| 165 |
|
|
* A conforms to B and B conforms to A with A and B having different
|
| 166 |
|
|
* names is an impossible case.
|
| 167 |
|
|
*/
|
| 168 |
|
|
- (BOOL) isEqual: (id)obj
|
| 169 |
|
|
{
|
| 170 |
|
|
if (obj == self)
|
| 171 |
|
|
return YES;
|
| 172 |
|
|
|
| 173 |
|
|
if ([obj isKindOf: [Protocol class]])
|
| 174 |
|
|
{
|
| 175 |
|
|
if (strcmp (protocol_name, ((Protocol *)obj)->protocol_name) == 0)
|
| 176 |
|
|
return YES;
|
| 177 |
|
|
}
|
| 178 |
|
|
|
| 179 |
|
|
return NO;
|
| 180 |
|
|
}
|
| 181 |
|
|
@end
|
| 182 |
|
|
|