PR rtl-optimization/55547
[official-gcc.git] / libobjc / objects.c
blobbdbe1dd58bf2e7c6824b36145475c1312eea220f
1 /* GNU Objective C Runtime class related functions
2 Copyright (C) 1993, 1995, 1996, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 3, or (at your option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "objc-private/common.h"
27 #include "objc/runtime.h"
28 #include "objc/thr.h" /* Required by objc-private/runtime.h. */
29 #include "objc-private/module-abi-8.h" /* For CLS_ISCLASS and similar. */
30 #include "objc-private/runtime.h" /* the kitchen sink */
32 #include <string.h> /* For memcpy() */
34 #if OBJC_WITH_GC
35 # include <gc.h>
36 # include <gc_typed.h>
37 #endif
39 /* FIXME: The semantics of extraBytes are not really clear. */
40 inline
42 class_createInstance (Class class, size_t extraBytes)
44 id new = nil;
46 #if OBJC_WITH_GC
47 if (CLS_ISCLASS (class))
48 new = (id) GC_malloc_explicitly_typed (class->instance_size + extraBytes,
49 (GC_descr)class->gc_object_type);
50 #else
51 if (CLS_ISCLASS (class))
52 new = (id) objc_calloc (class->instance_size + extraBytes, 1);
53 #endif
55 if (new != nil)
57 /* There is no need to zero the memory, since both
58 GC_malloc_explicitly_typed and objc_calloc return zeroed
59 memory. */
60 new->class_pointer = class;
63 /* TODO: Invoke C++ constructors on all appropriate C++ instance
64 variables of the new object. */
66 return new;
69 /* Traditional GNU Objective-C Runtime API. */
71 object_copy (id object, size_t extraBytes)
73 if ((object != nil) && CLS_ISCLASS (object->class_pointer))
75 /* TODO: How should it work with C++ constructors ? */
76 id copy = class_createInstance (object->class_pointer, extraBytes);
77 memcpy (copy, object, object->class_pointer->instance_size + extraBytes);
78 return copy;
80 else
81 return nil;
85 object_dispose (id object)
87 if ((object != nil) && CLS_ISCLASS (object->class_pointer))
89 /* TODO: Invoke C++ destructors on all appropriate C++ instance
90 variables. But what happens with the garbage collector ?
91 Would object_dispose() be ever called in that case ? */
93 objc_free (object);
95 return nil;
98 const char *
99 object_getClassName (id object)
101 if (object != nil)
102 return object->class_pointer->name;
103 else
104 return "Nil";
107 Class
108 object_setClass (id object, Class class_)
110 if (object == nil)
111 return Nil;
112 else
114 Class old_class = object->class_pointer;
116 object->class_pointer = class_;
117 return old_class;