18c1441f8SAlexey Samsonov //
2*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
4*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
58c1441f8SAlexey Samsonov
68c1441f8SAlexey Samsonov /*
78c1441f8SAlexey Samsonov * objectassign.c
88c1441f8SAlexey Samsonov * testObjects
98c1441f8SAlexey Samsonov *
108c1441f8SAlexey Samsonov * Created by Blaine Garst on 10/28/08.
118c1441f8SAlexey Samsonov *
128c1441f8SAlexey Samsonov * This just tests that the compiler is issuing the proper helper routines
138c1441f8SAlexey Samsonov * CONFIG C rdar://6175959
148c1441f8SAlexey Samsonov */
158c1441f8SAlexey Samsonov
168c1441f8SAlexey Samsonov
178c1441f8SAlexey Samsonov #include <stdio.h>
188c1441f8SAlexey Samsonov #include <Block_private.h>
198c1441f8SAlexey Samsonov
208c1441f8SAlexey Samsonov
218c1441f8SAlexey Samsonov int AssignCalled = 0;
228c1441f8SAlexey Samsonov int DisposeCalled = 0;
238c1441f8SAlexey Samsonov
248c1441f8SAlexey Samsonov // local copy instead of libSystem.B.dylib copy
_Block_object_assign(void * destAddr,const void * object,const int isWeak)258c1441f8SAlexey Samsonov void _Block_object_assign(void *destAddr, const void *object, const int isWeak) {
268c1441f8SAlexey Samsonov //printf("_Block_object_assign(%p, %p, %d) called\n", destAddr, object, isWeak);
278c1441f8SAlexey Samsonov AssignCalled = 1;
288c1441f8SAlexey Samsonov }
298c1441f8SAlexey Samsonov
_Block_object_dispose(const void * object,const int isWeak)308c1441f8SAlexey Samsonov void _Block_object_dispose(const void *object, const int isWeak) {
318c1441f8SAlexey Samsonov //printf("_Block_object_dispose(%p, %d) called\n", object, isWeak);
328c1441f8SAlexey Samsonov DisposeCalled = 1;
338c1441f8SAlexey Samsonov }
348c1441f8SAlexey Samsonov
358c1441f8SAlexey Samsonov struct MyStruct {
368c1441f8SAlexey Samsonov long isa;
378c1441f8SAlexey Samsonov long field;
388c1441f8SAlexey Samsonov };
398c1441f8SAlexey Samsonov
408c1441f8SAlexey Samsonov typedef struct MyStruct *__attribute__((NSObject)) MyStruct_t;
418c1441f8SAlexey Samsonov
main(int argc,char * argv[])428c1441f8SAlexey Samsonov int main(int argc, char *argv[]) {
438c1441f8SAlexey Samsonov if (__APPLE_CC__ < 5627) {
448c1441f8SAlexey Samsonov printf("need compiler version %d, have %d\n", 5627, __APPLE_CC__);
458c1441f8SAlexey Samsonov return 0;
468c1441f8SAlexey Samsonov }
478c1441f8SAlexey Samsonov // create a block
488c1441f8SAlexey Samsonov struct MyStruct X;
498c1441f8SAlexey Samsonov MyStruct_t xp = (MyStruct_t)&X;
508c1441f8SAlexey Samsonov xp->field = 10;
518c1441f8SAlexey Samsonov void (^myBlock)(void) = ^{ printf("field is %ld\n", xp->field); };
528c1441f8SAlexey Samsonov // should be a copy helper generated with a calls to above routines
538c1441f8SAlexey Samsonov // Lets find out!
548c1441f8SAlexey Samsonov struct Block_layout *bl = (struct Block_layout *)(void *)myBlock;
558c1441f8SAlexey Samsonov if ((bl->flags & BLOCK_HAS_COPY_DISPOSE) != BLOCK_HAS_COPY_DISPOSE) {
568c1441f8SAlexey Samsonov printf("no copy dispose!!!!\n");
578c1441f8SAlexey Samsonov return 1;
588c1441f8SAlexey Samsonov }
598c1441f8SAlexey Samsonov // call helper routines directly. These will, in turn, we hope, call the stubs above
608c1441f8SAlexey Samsonov long destBuffer[256];
618c1441f8SAlexey Samsonov //printf("destbuffer is at %p, block at %p\n", destBuffer, (void *)bl);
628c1441f8SAlexey Samsonov //printf("dump is %s\n", _Block_dump(myBlock));
638c1441f8SAlexey Samsonov bl->descriptor->copy(destBuffer, bl);
648c1441f8SAlexey Samsonov bl->descriptor->dispose(bl);
658c1441f8SAlexey Samsonov if (AssignCalled == 0) {
668c1441f8SAlexey Samsonov printf("did not call assign helper!\n");
678c1441f8SAlexey Samsonov return 1;
688c1441f8SAlexey Samsonov }
698c1441f8SAlexey Samsonov if (DisposeCalled == 0) {
708c1441f8SAlexey Samsonov printf("did not call dispose helper\n");
718c1441f8SAlexey Samsonov return 1;
728c1441f8SAlexey Samsonov }
738c1441f8SAlexey Samsonov printf("%s: Success!\n", argv[0]);
748c1441f8SAlexey Samsonov return 0;
758c1441f8SAlexey Samsonov }
76