1#import <Foundation/Foundation.h>
2
3int side_effect = 0;
4
5NSString *str = @"some string";
6
7const char *directCallConflictingName() {
8  return "wrong function";
9}
10
11@interface Foo : NSObject {
12  int instance_var;
13}
14-(int) entryPoint;
15@end
16
17@implementation Foo
18-(int) entryPoint
19{
20  // Try calling directly with self. Same as in the main method otherwise.
21  return 0; //%self.expect("expr [self directCallNoArgs]", substrs=["called directCallNoArgs"])
22            //%self.expect("expr [self directCallArgs: 1111]", substrs=["= 2345"])
23            //%self.expect("expr side_effect = 0; [self directCallVoidReturn]; side_effect", substrs=["= 4321"])
24            //%self.expect("expr [self directCallNSStringArg: str]", substrs=['@"some string"'])
25            //%self.expect("expr [self directCallIdArg: (id)str]", substrs=['@"some string appendix"'])
26            //%self.expect("expr [self directCallConflictingName]", substrs=["correct function"])
27            //%self.expect("expr [self directCallWithCategory]", substrs=["called function with category"])
28}
29
30// Declare several objc_direct functions we can test.
31-(const char *) directCallNoArgs __attribute__((objc_direct))
32{
33  return "called directCallNoArgs";
34}
35
36-(void) directCallVoidReturn __attribute__((objc_direct))
37{
38  side_effect = 4321;
39}
40
41-(int) directCallArgs:(int)i __attribute__((objc_direct))
42{
43  // Use the arg in some way to make sure that gets passed correctly.
44  return i + 1234;
45}
46
47-(NSString *) directCallNSStringArg:(NSString *)str __attribute__((objc_direct))
48{
49  return str;
50}
51
52-(NSString *) directCallIdArg:(id)param __attribute__((objc_direct))
53{
54  return [param stringByAppendingString:@" appendix"];
55}
56
57// We have another function with the same name above. Make sure this doesn't influence
58// what we call.
59-(const char *) directCallConflictingName  __attribute__((objc_direct))
60{
61  return "correct function";
62}
63@end
64
65
66@interface Foo (Cat)
67@end
68
69@implementation Foo (Cat)
70-(const char *) directCallWithCategory  __attribute__((objc_direct))
71{
72  return "called function with category";
73}
74@end
75
76int main()
77{
78  Foo *foo = [[Foo alloc] init];
79  [foo directCallNoArgs];
80  [foo directCallArgs: 1];
81  [foo directCallVoidReturn];
82  [foo directCallNSStringArg: str];
83  [foo directCallIdArg: (id)str];
84  [foo entryPoint];  //%self.expect("expr [foo directCallNoArgs]", substrs=["called directCallNoArgs"])
85                     //%self.expect("expr [foo directCallArgs: 1111]", substrs=["= 2345"])
86                     //%self.expect("expr side_effect = 0; [foo directCallVoidReturn]; side_effect", substrs=["= 4321"])
87                     //%self.expect("expr [foo directCallNSStringArg: str]", substrs=['@"some string"'])
88                     //%self.expect("expr [foo directCallIdArg: (id)str]", substrs=['@"some string appendix"'])
89                     //%self.expect("expr [foo directCallConflictingName]", substrs=["correct function"])
90                     //%self.expect("expr [foo directCallWithCategory]", substrs=["called function with category"])
91  return 0;
92}
93