xref: /llvm-project/lldb/test/API/lang/objc/blocks/ivars-in-blocks.m (revision d1a1798e51a30fbf537e9fd8931a49b504f37a25)
1#import "ivars-in-blocks.h"
2#import <stdio.h>
3
4typedef int (^my_block_ptr_type) (int);
5
6@interface IAmBlocky()
7{
8  int _hidden_ivar;
9  my_block_ptr_type _block_ptr;
10}
11
12@end
13
14@implementation IAmBlocky
15
16+ (int) addend
17{
18  return 3;
19}
20
21+ (void) classMethod
22{
23  int (^my_block)(int) = ^(int foo)
24  {
25    int ret = foo + [self addend];
26    return ret; // Break here inside the class method block.
27  };
28  printf("%d\n", my_block(2));
29}
30
31- (void) makeBlockPtr;
32{
33  _block_ptr = ^(int inval)
34  {
35    _hidden_ivar += inval;
36    return blocky_ivar * inval; // Break here inside the block.
37  };
38}
39
40- (IAmBlocky *) init
41{
42  blocky_ivar = 10;
43  _hidden_ivar = 20;
44  // Interesting...  Apparently you can't make a block in your init method.  This crashes...
45  // [self makeBlockPtr];
46  return self;
47}
48
49- (int) callABlock: (int) block_value
50{
51  if (_block_ptr == NULL)
52    [self makeBlockPtr];
53  int ret = _block_ptr (block_value);
54  [IAmBlocky classMethod];
55  return ret;
56}
57@end
58
59