]> git.saurik.com Git - cycript.git/blob - sig/parse.cpp
Cycript no longer uses the systems' libffi.dylib.
[cycript.git] / sig / parse.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "sig/parse.hpp"
23 #include "Error.hpp"
24
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cstring>
28
29 namespace sig {
30
31 void Parse_(CYPool &pool, struct Signature *signature, const char **name, char eos, Callback callback);
32 struct Type *Parse_(CYPool &pool, const char **name, char eos, bool named, Callback callback);
33
34
35 /* XXX: I really screwed up this time */
36 void *prealloc_(CYPool &pool, void *odata, size_t osize, size_t nsize) {
37 void *ndata(pool.malloc<void>(nsize));
38 memcpy(ndata, odata, osize);
39 return ndata;
40 }
41
42 void Parse_(CYPool &pool, struct Signature *signature, const char **name, char eos, Callback callback) {
43 _assert(*name != NULL);
44
45 // XXX: this is just a stupid check :(
46 bool named(**name == '"');
47
48 signature->elements = NULL;
49 signature->count = 0;
50
51 for (;;) {
52 if (**name == eos) {
53 ++*name;
54 return;
55 }
56
57 signature->elements = (struct Element *) prealloc_(pool, signature->elements, signature->count * sizeof(struct Element), (signature->count + 1) * sizeof(struct Element));
58 _assert(signature->elements != NULL);
59
60 struct Element *element = &signature->elements[signature->count++];
61
62 if (**name != '"')
63 element->name = NULL;
64 else {
65 const char *quote = strchr(++*name, '"');
66 element->name = pool.strmemdup(*name, quote - *name);
67 *name = quote + 1;
68 }
69
70 element->type = Parse_(pool, name, eos, named, callback);
71
72 if (**name < '0' || **name > '9')
73 element->offset = _not(size_t);
74 else {
75 element->offset = 0;
76
77 do
78 element->offset = element->offset * 10 + (*(*name)++ - '0');
79 while (**name >= '0' && **name <= '9');
80 }
81 }
82 }
83
84 Type *Parse_(CYPool &pool, const char **name, char eos, bool named, Callback callback) {
85 char next = *(*name)++;
86 if (next == '?')
87 return NULL;
88
89 Type *type(new(pool) Type());
90 _assert(type != NULL);
91 memset(type, 0, sizeof(Type));
92
93 parse:
94 switch (next) {
95 case '#': type->primitive = typename_P; break;
96
97 case '(':
98 if (type->data.signature.count < 2)
99 type->primitive = struct_P;
100 else
101 type->primitive = union_P;
102 next = ')';
103 goto aggregate;
104
105 case '*': type->primitive = string_P; break;
106 case ':': type->primitive = selector_P; break;
107
108 case '@': {
109 char next(**name);
110
111 if (next == '?') {
112 type->primitive = block_P;
113 ++*name;
114 } else {
115 type->primitive = object_P;
116
117 if (next == '"') {
118 const char *quote = strchr(*name + 1, '"');
119 if (quote == NULL) {
120 printf("unterminated specific id type {%s}\n", *name - 10);
121 _assert(false);
122 } else if (!named || quote[1] == eos || quote[1] == '"') {
123 type->name = pool.strmemdup(*name + 1, quote - *name - 1);
124 *name = quote + 1;
125 }
126 }
127 }
128
129 } break;
130
131 case 'B': type->primitive = boolean_P; break;
132 case 'C': type->primitive = uchar_P; break;
133 case 'I': type->primitive = uint_P; break;
134 case 'L': type->primitive = ulong_P; break;
135 case 'Q': type->primitive = ulonglong_P; break;
136 case 'S': type->primitive = ushort_P; break;
137
138 case '[':
139 type->primitive = array_P;
140 type->data.data.size = strtoul(*name, (char **) name, 10);
141 type->data.data.type = Parse_(pool, name, eos, false, callback);
142 if (**name != ']') {
143 printf("']' != \"%s\"\n", *name);
144 _assert(false);
145 }
146 ++*name;
147 break;
148
149 case '^':
150 type->primitive = pointer_P;
151 if (**name == '"') {
152 type->data.data.type = NULL;
153 } else {
154 type->data.data.type = Parse_(pool, name, eos, named, callback);
155 sig::Type *&target(type->data.data.type);
156 if (target != NULL && target->primitive == void_P)
157 target = NULL;
158 }
159 break;
160
161 case 'b':
162 type->primitive = bit_P;
163 type->data.data.size = strtoul(*name, (char **) name, 10);
164 break;
165
166 case 'c': type->primitive = char_P; break;
167 case 'd': type->primitive = double_P; break;
168 case 'f': type->primitive = float_P; break;
169 case 'i': type->primitive = int_P; break;
170 case 'l': type->primitive = long_P; break;
171 case 'q': type->primitive = longlong_P; break;
172 case 's': type->primitive = short_P; break;
173 case 'v': type->primitive = void_P; break;
174
175 case '{':
176 type->primitive = struct_P;
177 next = '}';
178 goto aggregate;
179
180 aggregate: {
181 char end = next;
182 const char *begin = *name;
183 do next = *(*name)++;
184 while (
185 next != '=' &&
186 next != '}'
187 );
188 size_t length = *name - begin - 1;
189 if (strncmp(begin, "?", length) != 0)
190 type->name = (char *) pool.strmemdup(begin, length);
191 else
192 type->name = NULL;
193
194 // XXX: this types thing is a throwback to JocStrap
195
196 if (next == '=')
197 Parse_(pool, &type->data.signature, name, end, callback);
198 } break;
199
200 case 'N': type->flags |= JOC_TYPE_INOUT; goto next;
201 case 'n': type->flags |= JOC_TYPE_IN; goto next;
202 case 'O': type->flags |= JOC_TYPE_BYCOPY; goto next;
203 case 'o': type->flags |= JOC_TYPE_OUT; goto next;
204 case 'R': type->flags |= JOC_TYPE_BYREF; goto next;
205 case 'r': type->flags |= JOC_TYPE_CONST; goto next;
206 case 'V': type->flags |= JOC_TYPE_ONEWAY; goto next;
207
208 next:
209 next = *(*name)++;
210 goto parse;
211 break;
212
213 default:
214 printf("invalid type character: '%c' {%s}\n", next, *name - 10);
215 _assert(false);
216 }
217
218 if (callback != NULL)
219 (*callback)(pool, type);
220
221 return type;
222 }
223
224 void Parse(CYPool &pool, struct Signature *signature, const char *name, Callback callback) {
225 const char *temp = name;
226 Parse_(pool, signature, &temp, '\0', callback);
227 _assert(temp[-1] == '\0');
228 }
229
230 const char *Unparse(CYPool &pool, struct Signature *signature) {
231 const char *value = "";
232 size_t offset;
233
234 for (offset = 0; offset != signature->count; ++offset) {
235 const char *type = Unparse(pool, signature->elements[offset].type);
236 value = pool.strcat(value, type, NULL);
237 }
238
239 return value;
240 }
241
242 const char *Unparse_(CYPool &pool, struct Type *type) {
243 switch (type->primitive) {
244 case typename_P: return "#";
245 case union_P: return pool.strcat("(", Unparse(pool, &type->data.signature), ")", NULL);
246 case string_P: return "*";
247 case selector_P: return ":";
248 case block_P: return "@?";
249 case object_P: return type->name == NULL ? "@" : pool.strcat("@\"", type->name, "\"", NULL);
250 case boolean_P: return "B";
251 case uchar_P: return "C";
252 case uint_P: return "I";
253 case ulong_P: return "L";
254 case ulonglong_P: return "Q";
255 case ushort_P: return "S";
256
257 case array_P: {
258 const char *value = Unparse(pool, type->data.data.type);
259 return pool.strcat("[", pool.itoa(type->data.data.size), value, "]", NULL);
260 } break;
261
262 case pointer_P: return pool.strcat("^", type->data.data.type == NULL ? "v" : Unparse(pool, type->data.data.type), NULL);
263 case bit_P: return pool.strcat("b", pool.itoa(type->data.data.size), NULL);
264 case char_P: return "c";
265 case double_P: return "d";
266 case float_P: return "f";
267 case int_P: return "i";
268 case long_P: return "l";
269 case longlong_P: return "q";
270 case short_P: return "s";
271 case void_P: return "v";
272 case struct_P: return pool.strcat("{", type->name == NULL ? "?" : type->name, "=", Unparse(pool, &type->data.signature), "}", NULL);
273 }
274
275 _assert(false);
276 return NULL;
277 }
278
279 const char *Unparse(CYPool &pool, struct Type *type) {
280 if (type == NULL)
281 return "?";
282
283 const char *base(Unparse_(pool, type));
284 if (type->flags == 0)
285 return base;
286
287 #define iovec_(base, size) \
288 (struct iovec) {const_cast<char *>(base), size}
289
290 size_t size(strlen(base));
291 char buffer[7 + size];
292 size_t offset(0);
293
294 if ((type->flags & JOC_TYPE_INOUT) != 0)
295 buffer[offset++] = 'N';
296 if ((type->flags & JOC_TYPE_IN) != 0)
297 buffer[offset++] = 'n';
298 if ((type->flags & JOC_TYPE_BYCOPY) != 0)
299 buffer[offset++] = 'O';
300 if ((type->flags & JOC_TYPE_OUT) != 0)
301 buffer[offset++] = 'o';
302 if ((type->flags & JOC_TYPE_BYREF) != 0)
303 buffer[offset++] = 'R';
304 if ((type->flags & JOC_TYPE_CONST) != 0)
305 buffer[offset++] = 'r';
306 if ((type->flags & JOC_TYPE_ONEWAY) != 0)
307 buffer[offset++] = 'V';
308
309 memcpy(buffer + offset, base, size);
310 return pool.strmemdup(buffer, offset + size);
311 }
312
313 }