summaryrefslogtreecommitdiffstats
path: root/test/Sema/block-literal.c
blob: 19d476fc294f16ab269c01c6f233328161f2ccc3 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// RUN: clang-cc -fsyntax-only %s -verify -fblocks

void I( void (^)(void));
void (^noop)(void);

void nothing();
int printf(const char*, ...);

typedef void (^T) (void);

void takeclosure(T);
int takeintint(int (^C)(int)) { return C(4); }

T somefunction() {
	if (^{ })
	  nothing();

	noop = ^{};

	noop = ^{printf("\nClosure\n"); };

	I(^{ });

	return ^{printf("\nClosure\n"); };
}
void test2() {
	int x = 4;

	takeclosure(^{ printf("%d\n", x); });

  while (1) {
	  takeclosure(^{ 
      break;  // expected-error {{'break' statement not in loop or switch statement}}
	    continue; // expected-error {{'continue' statement not in loop statement}}
	    while(1) break;  // ok
      goto foo; // expected-error {{goto not allowed}}
    });
    break;
	}

foo:
	takeclosure(^{ x = 4; });  // expected-error {{variable is not assignable (missing __block type specifier)}}
  __block y = 7;    // expected-warning {{type specifier missing, defaults to 'int'}}
  takeclosure(^{ y = 8; });
}


void (^test3())(void) { 
  return ^{};
}

void test4() {
  void (^noop)(void) = ^{};
  void (*noop2)() = 0;
}

void myfunc(int (^block)(int)) {}

void myfunc3(const int *x);

void test5() {
    int a;

    myfunc(^(int abcd) {
        myfunc3(&a);
        return 1;
    });
}

void *X;

void test_arguments() {
  int y;
  int (^c)(char);
  (1 ? c : 0)('x');
  (1 ? 0 : c)('x');

  (1 ? c : c)('x');
}

static int global_x = 10;
void (^global_block)(void) = ^{ printf("global x is %d\n", global_x); };

typedef void (^void_block_t)(void);

static const void_block_t myBlock = ^{ };

static const void_block_t myBlock2 = ^ void(void) { }; 

#if 0
// Old syntax. FIXME: convert/test.
void test_byref() {
  int i;
  
  X = ^{| g |};  // error {{use of undeclared identifier 'g'}}

  X = ^{| i,i,i | };

  X = ^{|i| i = 0; };

}

// TODO: global closures someday.
void *A = ^{};
void *B = ^(int){ A = 0; };


// Closures can not take return types at this point.
void test_retvals() {
  // Explicit return value.
  ^int{};   // error {{closure with explicit return type requires argument list}}
  X = ^void(){};

  // Optional specification of return type.
  X = ^char{ return 'x'; };  // error {{closure with explicit return type requires argument list}}

  X = ^/*missing declspec*/ *() { return (void*)0; };
  X = ^void*() { return (void*)0; };
  
  //X = ^char(short c){ if (c) return c; else return (int)4; };
  
}

#endif
OpenPOWER on IntegriCloud