]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Found the garbage collection bug from hell (classes apparently really need /something...
[cycript.git] / Replace.cpp
CommitLineData
4644480a
JF
1/* Cycript - Remove Execution Server and Disassembler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3*/
4
5/* Modified BSD License {{{ */
6/*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/* }}} */
39
3b52fd1a
JF
40#include "Parser.hpp"
41
3b52fd1a
JF
42#include <iomanip>
43
4de0686f 44#include "Replace.hpp"
3b52fd1a 45
4644480a
JF
46CYExpression *CYAdd::Replace(CYContext &context) {
47 CYInfix::Replace(context);
48
49 CYExpression *lhp(lhs_->Primitive(context));
50 CYExpression *rhp(rhs_->Primitive(context));
51
52 CYString *lhs(dynamic_cast<CYString *>(lhp));
53 CYString *rhs(dynamic_cast<CYString *>(rhp));
54
55 if (lhs != NULL || rhs != NULL) {
56 if (lhs == NULL) {
57 lhs = lhp->String(context);
58 if (lhs == NULL)
59 return NULL;
60 } else if (rhs == NULL) {
61 rhs = rhp->String(context);
62 if (rhs == NULL)
63 return NULL;
64 }
65
66 return lhs->Concat(context, rhs);
67 }
68
69 if (CYNumber *lhn = lhp->Number(context))
70 if (CYNumber *rhn = rhp->Number(context))
71 return $D(lhn->Value() + rhn->Value());
72
73 return NULL;
74}
75
3b52fd1a
JF
76CYExpression *CYAddressOf::Replace(CYContext &context) {
77 CYPrefix::Replace(context);
78 return $C0($M(rhs_, $S("$cya")));
79}
80
81void CYArgument::Replace(CYContext &context) { $T()
82 context.Replace(value_);
83 next_->Replace(context);
84}
85
86CYExpression *CYArray::Replace(CYContext &context) {
87 elements_->Replace(context);
88 return NULL;
89}
90
91CYExpression *CYArrayComprehension::Replace(CYContext &context) {
92 CYVariable *cyv($V("$cyv"));
93
94 return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->*
95 $E($ CYAssign(cyv, $ CYArray()))->*
96 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
97 $ CYReturn(cyv)
98 ));
99}
100
101CYExpression *CYAssignment::Replace(CYContext &context) {
102 context.Replace(lhs_);
103 context.Replace(rhs_);
104 return NULL;
105}
106
107CYStatement *CYBlock::Replace(CYContext &context) {
108 statements_ = statements_->ReplaceAll(context);
109 return NULL;
110}
111
112CYStatement *CYBreak::Replace(CYContext &context) {
113 return NULL;
114}
115
116CYExpression *CYCall::Replace(CYContext &context) {
117 context.Replace(function_);
118 arguments_->Replace(context);
119 return NULL;
120}
121
122void CYCatch::Replace(CYContext &context) { $T()
123 code_.Replace(context);
124}
125
3b52fd1a
JF
126void CYClause::Replace(CYContext &context) { $T()
127 context.Replace(case_);
128 statements_ = statements_->ReplaceAll(context);
129 next_->Replace(context);
130}
131
132CYExpression *CYCompound::Replace(CYContext &context) {
133 expressions_ = expressions_->ReplaceAll(context);
134 return NULL;
135}
136
137CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
138 CYFunctionParameter *next(next_->Parameters(context));
139 if (CYFunctionParameter *parameter = Parameter(context)) {
140 parameter->SetNext(next);
141 return parameter;
142 } else
143 return next;
144}
145
146CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
147 return next_ == NULL ? statement : next_->Replace(context, statement);
148}
149
150CYExpression *CYCondition::Replace(CYContext &context) {
151 context.Replace(test_);
152 context.Replace(true_);
153 context.Replace(false_);
154 return NULL;
155}
156
157CYStatement *CYContinue::Replace(CYContext &context) {
158 return NULL;
159}
160
161CYExpression *CYDeclaration::ForEachIn(CYContext &context) {
162 return $ CYVariable(identifier_);
163}
164
165void CYDeclaration::Replace(CYContext &context) {
166 context.Replace(initialiser_);
167}
168
169void CYDeclarations::Replace(CYContext &context) { $T()
170 declaration_->Replace(context);
171 next_->Replace(context);
172}
173
174CYExpression *CYDirectMember::Replace(CYContext &context) {
175 Replace_(context);
176 return NULL;
177}
178
179CYStatement *CYDoWhile::Replace(CYContext &context) {
180 context.Replace(test_);
181 context.Replace(code_);
182 return NULL;
183}
184
185void CYElement::Replace(CYContext &context) { $T()
186 context.Replace(value_);
187 next_->Replace(context);
188}
189
190CYStatement *CYEmpty::Replace(CYContext &context) {
191 return NULL;
192}
193
194CYStatement *CYExpress::Replace(CYContext &context) {
195 context.Replace(expression_);
196 return NULL;
197}
198
199CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
200 return this;
201}
202
203CYExpression *CYExpression::ForEachIn(CYContext &context) {
204 return this;
205}
206
207CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL)
208 CYExpression *replace(this);
209 context.Replace(replace);
210
211 if (CYExpression *next = next_->ReplaceAll(context))
212 replace->SetNext(next);
213 else
214 replace->SetNext(next_);
215
216 return replace;
217}
218
4644480a
JF
219CYNumber *CYFalse::Number(CYContext &context) {
220 return $D(0);
221}
222
223CYString *CYFalse::String(CYContext &context) {
224 return $S("false");
225}
226
3b52fd1a
JF
227void CYFinally::Replace(CYContext &context) { $T()
228 code_.Replace(context);
229}
230
231CYStatement *CYFor::Replace(CYContext &context) {
232 // XXX: initialiser_
233 context.Replace(test_);
234 context.Replace(increment_);
235 context.Replace(code_);
236 return NULL;
237}
238
239CYStatement *CYForIn::Replace(CYContext &context) {
240 // XXX: initialiser_
241 context.Replace(set_);
242 context.Replace(code_);
243 return NULL;
244}
245
246CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
247 return $ CYFunctionParameter(name_);
248}
249
250CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
251 return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement));
252}
253
254CYStatement *CYForEachIn::Replace(CYContext &context) {
255 CYVariable *cys($V("$cys")), *cyt($V("$cyt"));
256
257 return $ CYWith($ CYObject($ CYProperty($S("$cys"), $D(0), $ CYProperty($S("$cyt"), $D(0)))), $ CYBlock($$->*
258 $E($ CYAssign(cys, set_))->*
259 $ CYForIn(cyt, cys, $ CYBlock($$->*
260 $E($ CYAssign(initialiser_->ForEachIn(context), $M(cys, cyt)))->*
261 code_
262 ))
263 ));
264}
265
266CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
267 return $ CYFunctionParameter(name_);
268}
269
270CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
271 CYVariable *cys($V("$cys")), *name($ CYVariable(name_));
272
273 return $E($C0($F(NULL, $P1("$cys"), $$->*
274 $E($ CYAssign(cys, set_))->*
275 $ CYForIn(name, cys, $ CYBlock($$->*
276 $E($ CYAssign(name, $M(cys, name)))->*
277 CYComprehension::Replace(context, statement)
278 ))
279 )));
280}
281
282void CYFunction::Replace_(CYContext &context) {
283 code_.Replace(context);
284}
285
286CYExpression *CYFunctionExpression::Replace(CYContext &context) {
287 Replace_(context);
288 return NULL;
289}
290
291CYStatement *CYFunctionStatement::Replace(CYContext &context) {
292 Replace_(context);
293 return NULL;
294}
295
296CYStatement *CYIf::Replace(CYContext &context) {
297 context.Replace(test_);
298 context.Replace(true_);
299 context.Replace(false_);
300 return NULL;
301}
302
303CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
304 return NULL;
305}
306
307CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
308 return $ CYIf(test_, CYComprehension::Replace(context, statement));
309}
310
311CYExpression *CYIndirect::Replace(CYContext &context) {
312 CYPrefix::Replace(context);
313 return $M(rhs_, $S("$cyi"));
314}
315
316CYExpression *CYIndirectMember::Replace(CYContext &context) {
317 Replace_(context);
318 return $M($ CYIndirect(object_), property_);
319}
320
321CYExpression *CYInfix::Replace(CYContext &context) {
322 context.Replace(lhs_);
323 context.Replace(rhs_);
324 return NULL;
325}
326
327CYStatement *CYLabel::Replace(CYContext &context) {
328 context.Replace(statement_);
329 return NULL;
330}
331
332void CYMember::Replace_(CYContext &context) {
333 context.Replace(object_);
334 context.Replace(property_);
335}
336
3b52fd1a
JF
337CYExpression *CYNew::Replace(CYContext &context) {
338 context.Replace(constructor_);
339 arguments_->Replace(context);
340 return NULL;
341}
342
4644480a
JF
343CYNumber *CYNull::Number(CYContext &context) {
344 return $D(0);
345}
346
347CYString *CYNull::String(CYContext &context) {
348 return $S("null");
349}
350
351CYNumber *CYNumber::Number(CYContext &context) {
352 return this;
353}
354
355CYString *CYNumber::String(CYContext &context) {
356 // XXX: there is a precise algorithm for this
357 return $S(apr_psprintf(context.pool_, "%.17g", Value()));
358}
359
3b52fd1a
JF
360CYExpression *CYObject::Replace(CYContext &context) {
361 properties_->Replace(context);
362 return NULL;
363}
364
365CYExpression *CYPostfix::Replace(CYContext &context) {
366 context.Replace(lhs_);
367 return NULL;
368}
369
370CYExpression *CYPrefix::Replace(CYContext &context) {
371 context.Replace(rhs_);
372 return NULL;
373}
374
375void CYProgram::Replace(CYContext &context) {
376 statements_ = statements_->ReplaceAll(context);
377}
378
379void CYProperty::Replace(CYContext &context) { $T()
380 context.Replace(value_);
381 next_->Replace(context);
382}
383
384CYStatement *CYReturn::Replace(CYContext &context) {
385 context.Replace(value_);
386 return NULL;
387}
388
3b52fd1a
JF
389CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL)
390 CYStatement *replace(this);
391 context.Replace(replace);
392
393 if (CYStatement *next = next_->ReplaceAll(context))
394 replace->SetNext(next);
395 else
396 replace->SetNext(next_);
397
398 return replace;
399}
400
4644480a
JF
401CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
402 size_t size(size_ + rhs->size_);
403 char *value(new(context.pool_) char[size + 1]);
404 memcpy(value, value_, size_);
405 memcpy(value + size_, rhs->value_, rhs->size_);
406 value[size] = '\0';
407 return $S(value);
408}
409
410CYNumber *CYString::Number(CYContext &context) {
411 // XXX: there is a precise algorithm for this
412 return NULL;
413}
414
415CYString *CYString::String(CYContext &context) {
416 return this;
417}
418
3b52fd1a
JF
419CYStatement *CYSwitch::Replace(CYContext &context) {
420 context.Replace(value_);
421 clauses_->Replace(context);
422 return NULL;
423}
424
425CYExpression *CYThis::Replace(CYContext &context) {
426 return NULL;
427}
428
429CYStatement *CYThrow::Replace(CYContext &context) {
430 context.Replace(value_);
431 return NULL;
432}
433
434CYExpression *CYTrivial::Replace(CYContext &context) {
435 return NULL;
436}
437
4644480a
JF
438CYNumber *CYTrue::Number(CYContext &context) {
439 return $D(1);
440}
441
442CYString *CYTrue::String(CYContext &context) {
443 return $S("true");
444}
445
3b52fd1a
JF
446CYStatement *CYTry::Replace(CYContext &context) {
447 code_.Replace(context);
448 catch_->Replace(context);
449 finally_->Replace(context);
450 return NULL;
451}
452
453CYStatement *CYVar::Replace(CYContext &context) {
454 declarations_->Replace(context);
455 return NULL;
456}
457
458CYExpression *CYVariable::Replace(CYContext &context) {
459 return NULL;
460}
461
462CYStatement *CYWhile::Replace(CYContext &context) {
463 context.Replace(test_);
464 context.Replace(code_);
465 return NULL;
466}
467
468CYStatement *CYWith::Replace(CYContext &context) {
469 context.Replace(scope_);
470 context.Replace(code_);
471 return NULL;
472}
473
474CYExpression *CYWord::ClassName(CYContext &context, bool object) {
475 CYString *name($S(this));
476 if (object)
477 return $C1($V("objc_getClass"), name);
478 else
479 return name;
480}