186d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst//
286d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst//                     The LLVM Compiler Infrastructure
386d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst//
486d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst// This file is distributed under the University of Illinois Open Source
586d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst// License. See LICENSE.TXT for details.
686d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
786d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst// CONFIG open rdar://6416474
886d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst// was  rdar://5847976
986d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst// was  rdar://6348320
1086d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
1186d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst#include <stdio.h>
1286d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst#include <Block.h>
1386d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
1486d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garstint verbose = 0;
1586d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
1686d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garstint main(int argc, char* argv[]) {
1786d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
1886d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst        if (argc > 1) verbose = 1;
1986d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
2086d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	__block void (^recursive_local_block)(int);
2186d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
2286d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	if (verbose) printf("recursive_local_block is a local recursive block\n");
2386d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	recursive_local_block = ^(int i) {
2486d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		if (verbose) printf("%d\n", i);
2586d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		if (i > 0) {
2686d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst			recursive_local_block(i - 1);
2786d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		}
2886d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst    };
2986d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
3086d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	if (verbose) printf("recursive_local_block's address is %p, running it:\n", (void*)recursive_local_block);
3186d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	recursive_local_block(5);
3286d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
3386d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	if (verbose) printf("Creating other_local_block: a local block that calls recursive_local_block\n");
3486d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
3586d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	void (^other_local_block)(int) = ^(int i) {
3686d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		if (verbose) printf("other_local_block running\n");
3786d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		recursive_local_block(i);
3886d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst    };
3986d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
4086d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	if (verbose) printf("other_local_block's address is %p, running it:\n", (void*)other_local_block);
4186d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
4286d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	other_local_block(5);
4386d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
4486d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst#if __APPLE_CC__ >= 5627
4586d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	if (verbose) printf("Creating other_copied_block: a Block_copy of a block that will call recursive_local_block\n");
4686d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
4786d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	void (^other_copied_block)(int) = Block_copy(^(int i) {
4886d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		if (verbose) printf("other_copied_block running\n");
4986d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		recursive_local_block(i);
5086d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst    });
5186d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
5286d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	if (verbose) printf("other_copied_block's address is %p, running it:\n", (void*)other_copied_block);
5386d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
5486d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	other_copied_block(5);
5586d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst#endif
5686d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
5786d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	__block void (^recursive_copy_block)(int);
5886d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
5986d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	if (verbose) printf("Creating recursive_copy_block: a Block_copy of a block that will call recursive_copy_block recursively\n");
6086d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
6186d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	recursive_copy_block = Block_copy(^(int i) {
6286d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		if (verbose) printf("%d\n", i);
6386d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		if (i > 0) {
6486d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst			recursive_copy_block(i - 1);
6586d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst		}
6686d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst    });
6786d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
6886d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	if (verbose) printf("recursive_copy_block's address is %p, running it:\n", (void*)recursive_copy_block);
6986d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
7086d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	recursive_copy_block(5);
7186d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst
7286d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst        printf("%s: Success\n", argv[0]);
7386d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst	return 0;
7486d0ba4286ec42dcd055bd22434c7f40009834bdBlaine Garst}
75