NSMethodSignature+OCMAdditions.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2009-2015 Erik Doernenburg and contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. * not use these files except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. #import "NSMethodSignature+OCMAdditions.h"
  17. #import "OCMFunctions.h"
  18. #import <objc/runtime.h>
  19. @implementation NSMethodSignature(OCMAdditions)
  20. - (BOOL)usesSpecialStructureReturn
  21. {
  22. const char *types = OCMTypeWithoutQualifiers([self methodReturnType]);
  23. if((types == NULL) || (types[0] != '{'))
  24. return NO;
  25. /* In some cases structures are returned by ref. The rules are complex and depend on the
  26. architecture, see:
  27. http://sealiesoftware.com/blog/archive/2008/10/30/objc_explain_objc_msgSend_stret.html
  28. http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/LowLevelABI/000-Introduction/introduction.html
  29. https://github.com/atgreen/libffi/blob/master/src/x86/ffi64.c
  30. http://www.uclibc.org/docs/psABI-x86_64.pdf
  31. http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042e/IHI0042E_aapcs.pdf
  32. NSMethodSignature knows the details but has no API to return it, though it is in
  33. the debugDescription. Horribly kludgy.
  34. */
  35. NSRange range = [[self debugDescription] rangeOfString:@"is special struct return? YES"];
  36. return range.length > 0;
  37. }
  38. - (NSString *)fullTypeString
  39. {
  40. NSMutableString *typeString = [NSMutableString string];
  41. [typeString appendFormat:@"%s", [self methodReturnType]];
  42. for (NSUInteger i=0; i<[self numberOfArguments]; i++)
  43. [typeString appendFormat:@"%s", [self getArgumentTypeAtIndex:i]];
  44. return typeString;
  45. }
  46. - (const char *)fullObjCTypes
  47. {
  48. return [[self fullTypeString] UTF8String];
  49. }
  50. @end