-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathblocks.c
81 lines (61 loc) · 2.13 KB
/
blocks.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Compilable with:
// clang -fblocks -c blocks.c
void (*functionPtrReturningVoidWithVoidArgument)(void);
int (*functionPtrReturningIntWithIntAndCharArguments)(int, char);
void (*arrayOfTenFunctionPtrsReturningVoidWithIntArgument[10])(int);
void (^blockReturningVoidWithVoidArgument)(void);
int (^blockReturningIntWithIntAndCharArguments)(int, char);
void (^arrayOfTenBlocksReturningVoidWithIntArgument[10])(int);
int x;
void f(void) {
blockReturningVoidWithVoidArgument
= ^ void (void) { x = 1; };
blockReturningVoidWithVoidArgument
= ^ (void) { x = 1; };
blockReturningVoidWithVoidArgument
= ^ { x = 1; };
// Has an implicit "return;" so result type is void
void (^b1)(int, char)
= ^ (int y, char z) { x = 1; };
// Has no implicit "return;" but result type still void
void (^b2)(int, char)
= ^ (int y, char z) { x = 1; while (1) { } };
// Explicit return so result type double
double (^b3)(int, char)
= ^ (int y, char z) { x = 1; return 0.5; };
// Explicit return so result type double
double (^b4)(int, char)
= ^ (int y, char z) { x = 1; if (1) { return 0.5; }; while (1) { } };
// Also has an explicit return type
char (^b5)(int, char)
= ^ char (int y, char z) { x = 1; return 'c'; };
// Also has an explicit return type
const char * (^b6)(int, char)
= ^ const char * (int y, char z) { x = 1; return "str"; };
// Also has an explicit return type
const char * const * (^b7)(int, char)
= ^ const char * const * (int y, char z) { x = 1; while (1) { }; };
char c;
double d;
b1(5, 'a');
b2(5, 'a');
d = b3(5, 'a');
d = b4(5, 'a');
c = b5(5, 'a');
}
int (^fn)() = ^{
return 4;
};
void g(void) {
// Implicit parameter list
int(^b1)(void) = ^ int { return 4; };
// Exotic function pointer case
typedef int (*pointerToFunctionThatReturnsIntWithCharArg)(char);
pointerToFunctionThatReturnsIntWithCharArg functionPointer = 0;
pointerToFunctionThatReturnsIntWithCharArg (^b2)(float) = ^int(*(float x))(char) { return functionPointer; };
}
void h(void) {
typedef void(^B)(void);
(B)^{};
(B)^{};
}