]>
git.saurik.com Git - cycript.git/blob - Replace.cpp
6 #include <objc/runtime.h>
17 ($ CYFunctionExpression(args))
19 ($ CYIdentifier(args))
21 ($ CYDirectMember(args))
23 ($ CYFunctionParameter(args))
27 ($ CYVariable($I(name)))
35 #define $P1(arg0, args...) \
37 #define $P2(arg0, arg1, args...) \
38 $P($I(arg0), $P1(arg1, ##args))
39 #define $P3(arg0, arg1, arg2, args...) \
40 $P($I(arg0), $P2(arg1, arg2, ##args))
41 #define $P4(arg0, arg1, arg2, arg3, args...) \
42 $P($I(arg0), $P3(arg1, arg2, arg3, ##args))
43 #define $P5(arg0, arg1, arg2, arg3, arg4, args...) \
44 $P($I(arg0), $P4(arg1, arg2, arg3, arg4, ##args))
45 #define $P6(arg0, arg1, arg2, arg3, arg4, arg5, args...) \
46 $P($I(arg0), $P5(arg1, arg2, arg3, arg4, arg5, ##args))
50 #define $C_(args...) \
55 #define $C1_(arg0, args...) \
57 #define $C2_(arg0, arg1, args...) \
58 $C_(arg0, $C1_(arg1, ##args))
59 #define $C3_(arg0, arg1, arg2, args...) \
60 $C_(arg0, $C2_(arg1, arg2, ##args))
61 #define $C4_(arg0, arg1, arg2, arg3, args...) \
62 $C_(arg0, $C3_(arg1, arg2, arg3, ##args))
63 #define $C5_(arg0, arg1, arg2, arg3, arg4, args...) \
64 $C_(arg0, $C4_(arg1, arg2, arg3, arg4, ##args))
65 #define $C6_(arg0, arg1, arg2, arg3, arg4, arg5, args...) \
66 $C_(arg0, $C5_(arg1, arg2, arg3, arg4, arg5, ##args))
68 #define $C0(func, args...) \
70 #define $C1(func, args...) \
72 #define $C2(func, args...) \
74 #define $C3(func, args...) \
76 #define $C4(func, args...) \
78 #define $C5(func, args...) \
81 #define $N0(func, args...) \
83 #define $N1(func, args...) \
85 #define $N2(func, args...) \
87 #define $N3(func, args...) \
89 #define $N4(func, args...) \
91 #define $N5(func, args...) \
94 CYExpression
*CYAddressOf::Replace(CYContext
&context
) {
95 CYPrefix::Replace(context
);
96 return $
C0($
M(rhs_
, $
S("$cya")));
99 void CYArgument::Replace(CYContext
&context
) { $
T()
100 context
.Replace(value_
);
101 next_
->Replace(context
);
104 CYExpression
*CYArray::Replace(CYContext
&context
) {
105 elements_
->Replace(context
);
109 CYExpression
*CYArrayComprehension::Replace(CYContext
&context
) {
110 CYVariable
*cyv($
V("$cyv"));
112 return $
C0($
F(NULL
, $
P1("$cyv", comprehensions_
->Parameters(context
)), $$
->*
113 $
E($
CYAssign(cyv
, $
CYArray()))->*
114 comprehensions_
->Replace(context
, $
E($
C1($
M(cyv
, $
S("push")), expression_
)))->*
119 CYExpression
*CYAssignment::Replace(CYContext
&context
) {
120 context
.Replace(lhs_
);
121 context
.Replace(rhs_
);
125 CYStatement
*CYBlock::Replace(CYContext
&context
) {
126 statements_
= statements_
->ReplaceAll(context
);
130 CYStatement
*CYBreak::Replace(CYContext
&context
) {
134 CYExpression
*CYCall::Replace(CYContext
&context
) {
135 context
.Replace(function_
);
136 arguments_
->Replace(context
);
140 void CYCatch::Replace(CYContext
&context
) { $
T()
141 code_
.Replace(context
);
144 CYStatement
*CYCategory::Replace(CYContext
&context
) {
145 CYVariable
*cyc($
V("$cyc")), *cys($
V("$cys"));
147 return $
E($
C1($
F(NULL
, $
P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$
->*
148 $
E($
CYAssign($
V("$cyp"), $
C1($
V("object_getClass"), cys
)))->*
149 $
E($
CYAssign(cyc
, cys
))->*
150 messages_
->Replace(context
, true)
151 ), name_
->ClassName(context
, true)));
154 CYExpression
*CYClass::Replace_(CYContext
&context
) {
155 CYVariable
*cyc($
V("$cyc")), *cys($
V("$cys"));
157 CYExpression
*name(name_
!= NULL
? name_
->ClassName(context
, false) : $
C1($
V("$cyq"), $
S("CY$")));
159 return $
C1($
F(NULL
, $
P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$
->*
160 $
E($
CYAssign($
V("$cyp"), $
C1($
V("object_getClass"), cys
)))->*
161 $
E($
CYAssign(cyc
, $
C3($
V("objc_allocateClassPair"), cys
, name
, $
D(0))))->*
162 $
E($
CYAssign($
V("$cym"), $
C1($
V("object_getClass"), cyc
)))->*
163 fields_
->Replace(context
)->*
164 messages_
->Replace(context
, false)->*
165 $
E($
C1($
V("objc_registerClassPair"), cyc
))->*
167 ), super_
== NULL
? $
CYNull() : super_
);
170 CYExpression
*CYClassExpression::Replace(CYContext
&context
) {
171 return Replace_(context
);
174 CYStatement
*CYClassStatement::Replace(CYContext
&context
) {
175 return $
E(Replace_(context
));
178 void CYClause::Replace(CYContext
&context
) { $
T()
179 context
.Replace(case_
);
180 statements_
= statements_
->ReplaceAll(context
);
181 next_
->Replace(context
);
184 CYExpression
*CYCompound::Replace(CYContext
&context
) {
185 expressions_
= expressions_
->ReplaceAll(context
);
189 CYFunctionParameter
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
190 CYFunctionParameter
*next(next_
->Parameters(context
));
191 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
192 parameter
->SetNext(next
);
198 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
199 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
202 CYExpression
*CYCondition::Replace(CYContext
&context
) {
203 context
.Replace(test_
);
204 context
.Replace(true_
);
205 context
.Replace(false_
);
209 CYStatement
*CYContinue::Replace(CYContext
&context
) {
213 CYExpression
*CYDeclaration::ForEachIn(CYContext
&context
) {
214 return $
CYVariable(identifier_
);
217 void CYDeclaration::Replace(CYContext
&context
) {
218 context
.Replace(initialiser_
);
221 void CYDeclarations::Replace(CYContext
&context
) { $
T()
222 declaration_
->Replace(context
);
223 next_
->Replace(context
);
226 CYExpression
*CYDirectMember::Replace(CYContext
&context
) {
231 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
232 context
.Replace(test_
);
233 context
.Replace(code_
);
237 void CYElement::Replace(CYContext
&context
) { $
T()
238 context
.Replace(value_
);
239 next_
->Replace(context
);
242 CYStatement
*CYEmpty::Replace(CYContext
&context
) {
246 CYStatement
*CYExpress::Replace(CYContext
&context
) {
247 context
.Replace(expression_
);
251 CYExpression
*CYExpression::ClassName(CYContext
&context
, bool object
) {
255 CYExpression
*CYExpression::ForEachIn(CYContext
&context
) {
259 CYExpression
*CYExpression::ReplaceAll(CYContext
&context
) { $
T(NULL
)
260 CYExpression
*replace(this);
261 context
.Replace(replace
);
263 if (CYExpression
*next
= next_
->ReplaceAll(context
))
264 replace
->SetNext(next
);
266 replace
->SetNext(next_
);
271 CYStatement
*CYField::Replace(CYContext
&context
) const {
275 void CYFinally::Replace(CYContext
&context
) { $
T()
276 code_
.Replace(context
);
279 CYStatement
*CYFor::Replace(CYContext
&context
) {
281 context
.Replace(test_
);
282 context
.Replace(increment_
);
283 context
.Replace(code_
);
287 CYStatement
*CYForIn::Replace(CYContext
&context
) {
289 context
.Replace(set_
);
290 context
.Replace(code_
);
294 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
295 return $
CYFunctionParameter(name_
);
298 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
299 return $
CYForIn($
CYVariable(name_
), set_
, CYComprehension::Replace(context
, statement
));
302 CYStatement
*CYForEachIn::Replace(CYContext
&context
) {
303 CYVariable
*cys($
V("$cys")), *cyt($
V("$cyt"));
305 return $
CYWith($
CYObject($
CYProperty($
S("$cys"), $
D(0), $
CYProperty($
S("$cyt"), $
D(0)))), $
CYBlock($$
->*
306 $
E($
CYAssign(cys
, set_
))->*
307 $
CYForIn(cyt
, cys
, $
CYBlock($$
->*
308 $
E($
CYAssign(initialiser_
->ForEachIn(context
), $
M(cys
, cyt
)))->*
314 CYFunctionParameter
*CYForEachInComprehension::Parameter(CYContext
&context
) const {
315 return $
CYFunctionParameter(name_
);
318 CYStatement
*CYForEachInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
319 CYVariable
*cys($
V("$cys")), *name($
CYVariable(name_
));
321 return $
E($
C0($
F(NULL
, $
P1("$cys"), $$
->*
322 $
E($
CYAssign(cys
, set_
))->*
323 $
CYForIn(name
, cys
, $
CYBlock($$
->*
324 $
E($
CYAssign(name
, $
M(cys
, name
)))->*
325 CYComprehension::Replace(context
, statement
)
330 void CYFunction::Replace_(CYContext
&context
) {
331 code_
.Replace(context
);
334 CYExpression
*CYFunctionExpression::Replace(CYContext
&context
) {
339 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
344 CYStatement
*CYIf::Replace(CYContext
&context
) {
345 context
.Replace(test_
);
346 context
.Replace(true_
);
347 context
.Replace(false_
);
351 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
355 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
356 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
359 CYExpression
*CYIndirect::Replace(CYContext
&context
) {
360 CYPrefix::Replace(context
);
361 return $
M(rhs_
, $
S("$cyi"));
364 CYExpression
*CYIndirectMember::Replace(CYContext
&context
) {
366 return $
M($
CYIndirect(object_
), property_
);
369 CYExpression
*CYInfix::Replace(CYContext
&context
) {
370 context
.Replace(lhs_
);
371 context
.Replace(rhs_
);
375 CYStatement
*CYLabel::Replace(CYContext
&context
) {
376 context
.Replace(statement_
);
380 void CYMember::Replace_(CYContext
&context
) {
381 context
.Replace(object_
);
382 context
.Replace(property_
);
385 CYStatement
*CYMessage::Replace(CYContext
&context
, bool replace
) const { $
T(NULL
)
386 CYVariable
*cyn($
V("$cyn"));
387 CYVariable
*cyt($
V("$cyt"));
389 return $
CYBlock($$
->*
390 next_
->Replace(context
, replace
)->*
391 $
E($
CYAssign(cyn
, parameters_
->Selector(context
)))->*
392 $
E($
CYAssign(cyt
, $
C1($
M(cyn
, $
S("type")), $
V(instance_
? "$cys" : "$cyp"))))->*
393 $
E($
C4($
V(replace
? "class_replaceMethod" : "class_addMethod"),
394 $
V(instance_
? "$cyc" : "$cym"),
396 $
N2($
V("Functor"), $
F(NULL
, $
P2("self", "_cmd", parameters_
->Parameters(context
)), $$
->*
397 $
CYReturn($
C1($
M($
F(NULL
, NULL
, statements_
), $
S("call")), $
V("self")))
404 CYFunctionParameter
*CYMessageParameter::Parameters(CYContext
&context
) const { $
T(NULL
)
405 CYFunctionParameter
*next(next_
->Parameters(context
));
406 return name_
== NULL
? next
: $
CYFunctionParameter(name_
, next
);
409 CYSelector
*CYMessageParameter::Selector(CYContext
&context
) const {
410 return $
CYSelector(SelectorPart(context
));
413 CYSelectorPart
*CYMessageParameter::SelectorPart(CYContext
&context
) const { $
T(NULL
)
414 CYSelectorPart
*next(next_
->SelectorPart(context
));
415 return tag_
== NULL
? next
: $
CYSelectorPart(tag_
, name_
!= NULL
, next
);
418 CYExpression
*CYNew::Replace(CYContext
&context
) {
419 context
.Replace(constructor_
);
420 arguments_
->Replace(context
);
424 CYExpression
*CYObject::Replace(CYContext
&context
) {
425 properties_
->Replace(context
);
429 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
430 context
.Replace(lhs_
);
434 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
435 context
.Replace(rhs_
);
439 void CYProgram::Replace(CYContext
&context
) {
440 statements_
= statements_
->ReplaceAll(context
);
443 void CYProperty::Replace(CYContext
&context
) { $
T()
444 context
.Replace(value_
);
445 next_
->Replace(context
);
448 CYStatement
*CYReturn::Replace(CYContext
&context
) {
449 context
.Replace(value_
);
453 CYExpression
*CYSelector::Replace(CYContext
&context
) {
454 return $
N1($
V("Selector"), name_
->Replace(context
));
457 CYExpression
*CYSend::Replace(CYContext
&context
) {
458 std::ostringstream name
;
459 CYArgument
**argument(&arguments_
);
461 while (*argument
!= NULL
) {
462 if ((*argument
)->name_
!= NULL
) {
463 name
<< *(*argument
)->name_
;
464 (*argument
)->name_
= NULL
;
465 if ((*argument
)->value_
!= NULL
)
469 if ((*argument
)->value_
== NULL
)
470 *argument
= (*argument
)->next_
;
472 argument
= &(*argument
)->next_
;
475 SEL
sel(sel_registerName(name
.str().c_str()));
476 double address(static_cast<double>(reinterpret_cast<uintptr_t>(sel
)));
478 return $
C2($
V("objc_msgSend"), self_
, $
D(address
), arguments_
);
481 CYString
*CYSelectorPart::Replace(CYContext
&context
) {
482 std::ostringstream str
;
483 for (const CYSelectorPart
*part(this); part
!= NULL
; part
= part
->next_
) {
484 if (part
->name_
!= NULL
)
485 str
<< part
->name_
->Value();
489 return $
S(apr_pstrdup(context
.pool_
, str
.str().c_str()));
492 CYStatement
*CYStatement::ReplaceAll(CYContext
&context
) { $
T(NULL
)
493 CYStatement
*replace(this);
494 context
.Replace(replace
);
496 if (CYStatement
*next
= next_
->ReplaceAll(context
))
497 replace
->SetNext(next
);
499 replace
->SetNext(next_
);
504 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
505 context
.Replace(value_
);
506 clauses_
->Replace(context
);
510 CYExpression
*CYThis::Replace(CYContext
&context
) {
514 CYStatement
*CYThrow::Replace(CYContext
&context
) {
515 context
.Replace(value_
);
519 CYExpression
*CYTrivial::Replace(CYContext
&context
) {
523 CYStatement
*CYTry::Replace(CYContext
&context
) {
524 code_
.Replace(context
);
525 catch_
->Replace(context
);
526 finally_
->Replace(context
);
530 CYStatement
*CYVar::Replace(CYContext
&context
) {
531 declarations_
->Replace(context
);
535 CYExpression
*CYVariable::Replace(CYContext
&context
) {
539 CYStatement
*CYWhile::Replace(CYContext
&context
) {
540 context
.Replace(test_
);
541 context
.Replace(code_
);
545 CYStatement
*CYWith::Replace(CYContext
&context
) {
546 context
.Replace(scope_
);
547 context
.Replace(code_
);
551 CYExpression
*CYWord::ClassName(CYContext
&context
, bool object
) {
552 CYString
*name($
S(this));
554 return $
C1($
V("objc_getClass"), name
);