]> git.saurik.com Git - cycript.git/blob - sig/copy.cpp
Replace sig::Primitive with full object hierarchy.
[cycript.git] / sig / copy.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
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
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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/>.
19 **/
20 /* }}} */
21
22 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE
24 #endif
25
26 #include "Pooling.hpp"
27 #include "sig/parse.hpp"
28
29 namespace sig {
30
31 void Copy(CYPool &pool, Element &lhs, const Element &rhs) {
32 lhs.name = pool.strdup(rhs.name);
33 if (rhs.type == NULL)
34 lhs.type = NULL;
35 else
36 lhs.type = rhs.type->Copy(pool);
37 lhs.offset = rhs.offset;
38 }
39
40 void Copy(CYPool &pool, Signature &lhs, const Signature &rhs) {
41 size_t count(rhs.count);
42 lhs.count = count;
43 lhs.elements = new(pool) Element[count];
44 for (size_t index(0); index != count; ++index)
45 Copy(pool, lhs.elements[index], rhs.elements[index]);
46 }
47
48 Void *Void::Copy(CYPool &pool, const char *name) const {
49 return new(pool) Void();
50 }
51
52 Unknown *Unknown::Copy(CYPool &pool, const char *name) const {
53 return new(pool) Unknown();
54 }
55
56 String *String::Copy(CYPool &pool, const char *name) const {
57 return new(pool) String();
58 }
59
60 Meta *Meta::Copy(CYPool &pool, const char *name) const {
61 return new(pool) Meta();
62 }
63
64 Selector *Selector::Copy(CYPool &pool, const char *name) const {
65 return new(pool) Selector();
66 }
67
68 Bits *Bits::Copy(CYPool &pool, const char *name) const {
69 return new(pool) Bits(size);
70 }
71
72 Pointer *Pointer::Copy(CYPool &pool, const char *name) const {
73 return new(pool) Pointer(*type.Copy(pool));
74 }
75
76 Array *Array::Copy(CYPool &pool, const char *name) const {
77 return new(pool) Array(*type.Copy(pool), size);
78 }
79
80 Object *Object::Copy(CYPool &pool, const char *name) const {
81 Object *copy(new(pool) Object(pool.strdup(name)));
82 copy->name = name;
83 return copy;
84 }
85
86 Aggregate *Aggregate::Copy(CYPool &pool, const char *name) const {
87 Aggregate *copy(new(pool) Aggregate(overlap, name));
88 sig::Copy(pool, copy->signature, signature);
89 copy->name = name;
90 return copy;
91 }
92
93 Function *Function::Copy(CYPool &pool, const char *name) const {
94 Function *copy(new(pool) Function());
95 sig::Copy(pool, copy->signature, signature);
96 return copy;
97 }
98
99 Block *Block::Copy(CYPool &pool, const char *name) const {
100 Block *copy(new(pool) Block());
101 sig::Copy(pool, copy->signature, signature);
102 return copy;
103 }
104
105 void Copy(CYPool &pool, ffi_type &lhs, ffi_type &rhs) {
106 lhs.size = rhs.size;
107 lhs.alignment = rhs.alignment;
108 lhs.type = rhs.type;
109 if (rhs.elements == NULL)
110 lhs.elements = NULL;
111 else {
112 size_t count(0);
113 while (rhs.elements[count] != NULL)
114 ++count;
115
116 lhs.elements = new(pool) ffi_type *[count + 1];
117 lhs.elements[count] = NULL;
118
119 for (size_t index(0); index != count; ++index) {
120 // XXX: if these are libffi native then you can just take them
121 ffi_type *ffi(new(pool) ffi_type);
122 lhs.elements[index] = ffi;
123 sig::Copy(pool, *ffi, *rhs.elements[index]);
124 }
125 }
126 }
127
128 const char *Type::GetName() const {
129 return NULL;
130 }
131
132 const char *Object::GetName() const {
133 return name;
134 }
135
136 const char *Aggregate::GetName() const {
137 return name;
138 }
139
140 }