1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <Foundation/NSMethodSignature.h>
10 #import <Foundation/NSArray.h>
11 #import <Foundation/NSMapTable.h>
12 #import <Foundation/NSRaise.h>
13 #import <Foundation/NSStringHashing.h>
14 #import <Foundation/NSCoder.h>
17 @implementation NSMethodSignature
19 -initWithTypes:(const char *)typesCString {
20 const char *next,*last;
21 NSUInteger size,align;
23 size_t typesCStringLength=strlen(typesCString);
24 char *types[typesCStringLength]; // at most strlen arguments
26 // not guaranteed that typesCString is static
27 _typesCString=NSZoneMalloc(NULL,typesCStringLength+1);
28 strcpy(_typesCString,typesCString);
29 next=last=_typesCString;
33 while((next=NSGetSizeAndAlignment(next,&size,&align))!=last){
34 NSUInteger length=next-last;
35 char *nextCString=NSZoneMalloc(NULL,length+1);
37 strncpy(nextCString,last,length);
38 nextCString[length]='\0';
41 _returnType=nextCString;
43 types[_numberOfArguments]=nextCString;
49 while((*next>='0' && *next<='9') || *next=='+' || *next=='-' || *next=='?')
58 if(_numberOfArguments){
59 _types=NSZoneMalloc(NULL,_numberOfArguments*sizeof(char *));
63 for(i=0;i<_numberOfArguments;i++)
71 NSZoneFree(NULL,_typesCString);
74 NSZoneFree(NULL,_returnType);
78 for(i=0;i<_numberOfArguments;i++)
79 NSZoneFree(NULL,_types[i]);
80 NSZoneFree(NULL,_types);
83 NSDeallocateObject(self);
88 +(NSMethodSignature *)signatureWithObjCTypes:(const char *)typesCString {
89 return [[[NSMethodSignature allocWithZone:NULL] initWithTypes:typesCString] autorelease];
92 -(NSString *)description {
93 return [NSString stringWithFormat:@"<NSMethodSignature: -(%s)%s>",_returnType,_typesCString];
97 return NSStringHashZeroTerminatedASCII(_typesCString);
100 -(BOOL)isEqual:otherObject {
101 if(self==otherObject)
104 if([otherObject isKindOfClass:[NSMethodSignature class]]){
105 NSMethodSignature *other=otherObject;
107 return (strcmp(_typesCString,other->_typesCString)==0)?YES:NO;
114 return (_returnType!=NULL && _returnType[0]=='V');
117 -(NSUInteger)frameLength {
121 for(i=0;i<_numberOfArguments;i++){
123 NSUInteger naturalSize;
124 NSUInteger promotedSize;
126 NSGetSizeAndAlignment(_types[i],&naturalSize,&align);
127 promotedSize=((naturalSize+sizeof(long)-1)/sizeof(long))*sizeof(long);
129 result+=promotedSize;
134 -(NSUInteger)methodReturnLength {
135 NSUInteger size,align;
137 NSGetSizeAndAlignment(_returnType,&size,&align);
142 -(const char *)methodReturnType {
146 -(NSUInteger)numberOfArguments {
147 return _numberOfArguments;
150 -(const char *)getArgumentTypeAtIndex:(NSUInteger)index {
151 if(index>=_numberOfArguments){
152 [NSException raise:NSInvalidArgumentException format:@"index (%d) is beyond number of arguments (%d)",index,_numberOfArguments];
156 return _types[index];