]> git.saurik.com Git - cycript.git/blame - sig/copy.cpp
Try to push kJSClassAttributeNoAutomaticPrototype.
[cycript.git] / sig / copy.cpp
CommitLineData
7341eedb
JF
1/* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
3c1c3635
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
3c1c3635 6/*
f95d2598
JF
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b3378a02 19**/
3c1c3635
JF
20/* }}} */
21
22#ifndef _GNU_SOURCE
23#define _GNU_SOURCE
24#endif
25
3c1c3635
JF
26#include "Pooling.hpp"
27#include "sig/parse.hpp"
b128dfee 28
3c1c3635
JF
29namespace sig {
30
84aef9a7 31void Copy(CYPool &pool, Element &lhs, const Element &rhs) {
b799113b 32 lhs.name = pool.strdup(rhs.name);
1fdd7c7a
JF
33 _assert(rhs.type != NULL);
34 lhs.type = rhs.type->Copy(pool);
3c1c3635
JF
35 lhs.offset = rhs.offset;
36}
37
84aef9a7 38void Copy(CYPool &pool, Signature &lhs, const Signature &rhs) {
3c1c3635
JF
39 size_t count(rhs.count);
40 lhs.count = count;
1fdd7c7a
JF
41 if (count == _not(size_t))
42 lhs.elements = NULL;
43 else {
44 lhs.elements = new(pool) Element[count];
45 for (size_t index(0); index != count; ++index)
46 Copy(pool, lhs.elements[index], rhs.elements[index]);
47 }
3c1c3635
JF
48}
49
a109809e 50Void *Void::Copy(CYPool &pool, const char *rename) const {
0c172698 51 return Flag(new(pool) Void());
0559abf8 52}
3c1c3635 53
a109809e 54Unknown *Unknown::Copy(CYPool &pool, const char *rename) const {
0c172698 55 return Flag(new(pool) Unknown());
0559abf8 56}
3c1c3635 57
a109809e 58String *String::Copy(CYPool &pool, const char *rename) const {
0c172698 59 return Flag(new(pool) String());
0559abf8
JF
60}
61
e2ce853b 62#ifdef CY_OBJECTIVEC
a109809e 63Meta *Meta::Copy(CYPool &pool, const char *rename) const {
0c172698 64 return Flag(new(pool) Meta());
0559abf8
JF
65}
66
a109809e 67Selector *Selector::Copy(CYPool &pool, const char *rename) const {
0c172698 68 return Flag(new(pool) Selector());
0559abf8 69}
e2ce853b 70#endif
0559abf8 71
a109809e 72Bits *Bits::Copy(CYPool &pool, const char *rename) const {
0c172698 73 return Flag(new(pool) Bits(size));
0559abf8
JF
74}
75
a109809e 76Pointer *Pointer::Copy(CYPool &pool, const char *rename) const {
0c172698 77 return Flag(new(pool) Pointer(*type.Copy(pool)));
0559abf8
JF
78}
79
a109809e 80Array *Array::Copy(CYPool &pool, const char *rename) const {
0c172698 81 return Flag(new(pool) Array(*type.Copy(pool), size));
0559abf8
JF
82}
83
e2ce853b 84#ifdef CY_OBJECTIVEC
a109809e 85Object *Object::Copy(CYPool &pool, const char *rename) const {
0c172698 86 return Flag(new(pool) Object(pool.strdup(name)));
0559abf8 87}
e2ce853b 88#endif
0559abf8 89
aaa29c28
JF
90Enum *Enum::Copy(CYPool &pool, const char *rename) const {
91 if (rename == NULL)
92 rename = pool.strdup(name);
93 else if (rename[0] == '\0')
94 rename = NULL;
95 Enum *copy(new(pool) Enum(*type.Copy(pool), count, rename));
96 copy->constants = new(pool) Constant[count];
97 for (size_t i(0); i != count; ++i) {
98 copy->constants[i].name = pool.strdup(constants[i].name);
99 copy->constants[i].value = constants[i].value;
100 }
0c172698 101 return Flag(copy);
aaa29c28
JF
102}
103
a109809e 104Aggregate *Aggregate::Copy(CYPool &pool, const char *rename) const {
53cb77ff
JF
105 if (rename == NULL)
106 rename = pool.strdup(name);
107 else if (rename[0] == '\0')
108 rename = NULL;
109 Aggregate *copy(new(pool) Aggregate(overlap, rename));
0559abf8 110 sig::Copy(pool, copy->signature, signature);
0c172698 111 return Flag(copy);
0559abf8
JF
112}
113
a109809e 114Function *Function::Copy(CYPool &pool, const char *rename) const {
574d4720 115 Function *copy(new(pool) Function(variadic));
0559abf8 116 sig::Copy(pool, copy->signature, signature);
0c172698 117 return Flag(copy);
0559abf8
JF
118}
119
e2ce853b 120#ifdef CY_OBJECTIVEC
a109809e 121Block *Block::Copy(CYPool &pool, const char *rename) const {
0559abf8
JF
122 Block *copy(new(pool) Block());
123 sig::Copy(pool, copy->signature, signature);
0c172698 124 return Flag(copy);
3c1c3635 125}
e2ce853b 126#endif
3c1c3635 127
b799113b 128void Copy(CYPool &pool, ffi_type &lhs, ffi_type &rhs) {
3c1c3635
JF
129 lhs.size = rhs.size;
130 lhs.alignment = rhs.alignment;
131 lhs.type = rhs.type;
132 if (rhs.elements == NULL)
133 lhs.elements = NULL;
134 else {
135 size_t count(0);
136 while (rhs.elements[count] != NULL)
137 ++count;
138
139 lhs.elements = new(pool) ffi_type *[count + 1];
140 lhs.elements[count] = NULL;
141
142 for (size_t index(0); index != count; ++index) {
143 // XXX: if these are libffi native then you can just take them
144 ffi_type *ffi(new(pool) ffi_type);
145 lhs.elements[index] = ffi;
146 sig::Copy(pool, *ffi, *rhs.elements[index]);
147 }
148 }
149}
150
0559abf8
JF
151const char *Type::GetName() const {
152 return NULL;
153}
154
aaa29c28
JF
155const char *Enum::GetName() const {
156 return name;
157}
158
0559abf8
JF
159const char *Aggregate::GetName() const {
160 return name;
161}
162
3c1c3635 163}