]>
git.saurik.com Git - cycript.git/blob - sig/parse.cpp
e77da79bdc04053c6ad6f8347fc3dcdf98a327a4
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #include "sig/parse.hpp"
32 void Parse_(CYPool
&pool
, struct Signature
*signature
, const char **name
, char eos
, Callback callback
);
33 struct Type
*Parse_(CYPool
&pool
, const char **name
, char eos
, bool named
, Callback callback
);
36 /* XXX: I really screwed up this time */
37 void *prealloc_(CYPool
&pool
, void *odata
, size_t osize
, size_t nsize
) {
38 void *ndata(pool
.malloc
<void>(nsize
));
39 memcpy(ndata
, odata
, osize
);
43 void Parse_(CYPool
&pool
, struct Signature
*signature
, const char **name
, char eos
, Callback callback
) {
44 _assert(*name
!= NULL
);
46 // XXX: this is just a stupid check :(
47 bool named(**name
== '"');
49 signature
->elements
= NULL
;
58 signature
->elements
= (struct Element
*) prealloc_(pool
, signature
->elements
, signature
->count
* sizeof(struct Element
), (signature
->count
+ 1) * sizeof(struct Element
));
59 _assert(signature
->elements
!= NULL
);
61 struct Element
*element
= &signature
->elements
[signature
->count
++];
66 const char *quote
= strchr(++*name
, '"');
67 element
->name
= pool
.strmemdup(*name
, quote
- *name
);
71 element
->type
= Parse_(pool
, name
, eos
, named
, callback
);
73 if (**name
< '0' || **name
> '9')
74 element
->offset
= _not(size_t);
79 element
->offset
= element
->offset
* 10 + (*(*name
)++ - '0');
80 while (**name
>= '0' && **name
<= '9');
85 Type
*Parse_(CYPool
&pool
, const char **encoding
, char eos
, bool named
, Callback callback
) {
86 char next
= *(*encoding
)++;
93 case '?': type
= new(pool
) Unknown(); break;
94 case '#': type
= new(pool
) Meta(); break;
97 type
= new(pool
) Aggregate(true);
101 case '*': type
= new(pool
) String(); break;
102 case ':': type
= new(pool
) Selector(); break;
105 char next(**encoding
);
108 type
= new(pool
) Block();
115 const char *quote
= strchr(*encoding
+ 1, '"');
117 printf("unterminated specific id type {%s}\n", *encoding
- 10);
119 } else if (!named
|| quote
[1] == eos
|| quote
[1] == '"') {
120 name
= pool
.strmemdup(*encoding
+ 1, quote
- *encoding
- 1);
121 *encoding
= quote
+ 1;
127 type
= new(pool
) Object(name
);
132 case 'B': type
= new(pool
) Primitive
<bool>(); break;
133 case 'C': type
= new(pool
) Primitive
<unsigned char>(); break;
134 case 'I': type
= new(pool
) Primitive
<unsigned int>(); break;
135 case 'L': type
= new(pool
) Primitive
<unsigned long>(); break;
136 case 'Q': type
= new(pool
) Primitive
<unsigned long long>(); break;
137 case 'S': type
= new(pool
) Primitive
<unsigned short>(); break;
140 size_t size(strtoul(*encoding
, (char **) encoding
, 10));
141 type
= new(pool
) Array(*Parse_(pool
, encoding
, eos
, false, callback
), size
);
142 if (**encoding
!= ']') {
143 printf("']' != \"%s\"\n", *encoding
);
150 if (**encoding
== '"')
151 _assert(false); // XXX: why is this here?!?
153 type
= Parse_(pool
, encoding
, eos
, named
, callback
);
154 Aggregate
*aggregate(dynamic_cast<Aggregate
*>(type
));
155 if (aggregate
!= NULL
&& strcmp(aggregate
->name
, "_objc_class") == 0)
156 type
= new(pool
) Meta();
158 type
= new(pool
) Pointer(*type
);
163 type
= new(pool
) Bits(strtoul(*encoding
, (char **) encoding
, 10));
166 case 'c': type
= new(pool
) Primitive
<signed char>(); break;
167 case 'd': type
= new(pool
) Primitive
<double>(); break;
168 case 'f': type
= new(pool
) Primitive
<float>(); break;
169 case 'i': type
= new(pool
) Primitive
<signed int>(); break;
170 case 'l': type
= new(pool
) Primitive
<signed long>(); break;
171 case 'q': type
= new(pool
) Primitive
<signed long long>(); break;
172 case 's': type
= new(pool
) Primitive
<short>(); break;
173 case 'v': type
= new(pool
) Void(); break;
176 type
= new(pool
) Aggregate(false);
181 Aggregate
*aggregate(static_cast<Aggregate
*>(type
));
184 const char *begin
= *encoding
;
185 do next
= *(*encoding
)++;
190 size_t length
= *encoding
- begin
- 1;
191 if (strncmp(begin
, "?", length
) != 0)
192 aggregate
->name
= (char *) pool
.strmemdup(begin
, length
);
195 Parse_(pool
, &aggregate
->signature
, encoding
, end
, callback
);
197 // XXX: this is a hack to support trivial unions
198 if (aggregate
->signature
.count
<= 1)
199 aggregate
->overlap
= false;
201 if (callback
!= NULL
)
202 type
= (*callback
)(pool
, aggregate
);
205 case 'N': flags
|= JOC_TYPE_INOUT
; goto next
;
206 case 'n': flags
|= JOC_TYPE_IN
; goto next
;
207 case 'O': flags
|= JOC_TYPE_BYCOPY
; goto next
;
208 case 'o': flags
|= JOC_TYPE_OUT
; goto next
;
209 case 'R': flags
|= JOC_TYPE_BYREF
; goto next
;
210 case 'r': flags
|= JOC_TYPE_CONST
; goto next
;
211 case 'V': flags
|= JOC_TYPE_ONEWAY
; goto next
;
214 next
= *(*encoding
)++;
219 printf("invalid type character: '%c' {%s}\n", next
, *encoding
- 10);
228 void Parse(CYPool
&pool
, struct Signature
*signature
, const char *name
, Callback callback
) {
229 const char *temp
= name
;
230 Parse_(pool
, signature
, &temp
, '\0', callback
);
231 _assert(temp
[-1] == '\0');
234 const char *Unparse(CYPool
&pool
, const struct Signature
*signature
) {
235 const char *value
= "";
238 for (offset
= 0; offset
!= signature
->count
; ++offset
) {
239 const char *type
= Unparse(pool
, signature
->elements
[offset
].type
);
240 value
= pool
.strcat(value
, type
, NULL
);
247 const char *Primitive
<bool>::Encode(CYPool
&pool
) const {
252 const char *Primitive
<char>::Encode(CYPool
&pool
) const {
257 const char *Primitive
<double>::Encode(CYPool
&pool
) const {
262 const char *Primitive
<float>::Encode(CYPool
&pool
) const {
267 const char *Primitive
<signed char>::Encode(CYPool
&pool
) const {
272 const char *Primitive
<signed int>::Encode(CYPool
&pool
) const {
277 const char *Primitive
<signed long int>::Encode(CYPool
&pool
) const {
282 const char *Primitive
<signed long long int>::Encode(CYPool
&pool
) const {
287 const char *Primitive
<signed short int>::Encode(CYPool
&pool
) const {
292 const char *Primitive
<unsigned char>::Encode(CYPool
&pool
) const {
297 const char *Primitive
<unsigned int>::Encode(CYPool
&pool
) const {
302 const char *Primitive
<unsigned long int>::Encode(CYPool
&pool
) const {
307 const char *Primitive
<unsigned long long int>::Encode(CYPool
&pool
) const {
312 const char *Primitive
<unsigned short int>::Encode(CYPool
&pool
) const {
316 const char *Void::Encode(CYPool
&pool
) const {
320 const char *Unknown::Encode(CYPool
&pool
) const {
324 const char *String::Encode(CYPool
&pool
) const {
328 const char *Meta::Encode(CYPool
&pool
) const {
332 const char *Selector::Encode(CYPool
&pool
) const {
336 const char *Bits::Encode(CYPool
&pool
) const {
337 return pool
.strcat("b", pool
.itoa(size
), NULL
);
340 const char *Pointer::Encode(CYPool
&pool
) const {
341 return pool
.strcat("^", type
.Encode(pool
), NULL
);
344 const char *Array::Encode(CYPool
&pool
) const {
345 return pool
.strcat("[", pool
.itoa(size
), type
.Encode(pool
), "]", NULL
);
348 const char *Object::Encode(CYPool
&pool
) const {
349 return name
== NULL
? "@" : pool
.strcat("@\"", name
, "\"", NULL
);
352 const char *Aggregate::Encode(CYPool
&pool
) const {
353 return pool
.strcat(overlap
? "(" : "{", name
== NULL
? "?" : name
, "=", Unparse(pool
, &signature
), overlap
? ")" : "}", NULL
);
356 const char *Function::Encode(CYPool
&pool
) const {
360 const char *Block::Encode(CYPool
&pool
) const {
364 const char *Unparse(CYPool
&pool
, const struct Type
*type
) {
365 const char *base(type
->Encode(pool
));
366 if (type
->flags
== 0)
369 #define iovec_(base, size) \
370 (struct iovec) {const_cast<char *>(base), size}
372 size_t size(strlen(base
));
373 char buffer
[7 + size
];
376 if ((type
->flags
& JOC_TYPE_INOUT
) != 0)
377 buffer
[offset
++] = 'N';
378 if ((type
->flags
& JOC_TYPE_IN
) != 0)
379 buffer
[offset
++] = 'n';
380 if ((type
->flags
& JOC_TYPE_BYCOPY
) != 0)
381 buffer
[offset
++] = 'O';
382 if ((type
->flags
& JOC_TYPE_OUT
) != 0)
383 buffer
[offset
++] = 'o';
384 if ((type
->flags
& JOC_TYPE_BYREF
) != 0)
385 buffer
[offset
++] = 'R';
386 if ((type
->flags
& JOC_TYPE_CONST
) != 0)
387 buffer
[offset
++] = 'r';
388 if ((type
->flags
& JOC_TYPE_ONEWAY
) != 0)
389 buffer
[offset
++] = 'V';
391 memcpy(buffer
+ offset
, base
, size
);
392 return pool
.strmemdup(buffer
, offset
+ size
);