]> git.saurik.com Git - cycript.git/blob - ObjectiveC/Replace.cpp
I lost a $ on $cyr at some point during the super transformation.
[cycript.git] / ObjectiveC / Replace.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "Replace.hpp"
23 #include "ObjectiveC/Syntax.hpp"
24
25 #include <sstream>
26
27 CYStatement *CYCategory::Replace(CYContext &context) {
28 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
29
30 return $E($C1($F(NULL, $P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$->*
31 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
32 $E($ CYAssign(cyc, cys))->*
33 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
34 messages_->Replace(context, true)
35 ), name_->ClassName(context, true)));
36 }
37
38 CYExpression *CYClass::Replace_(CYContext &context) {
39 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
40
41 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
42
43 return $C1($F(NULL, $P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$->*
44 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
45 $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->*
46 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
47 protocols_->Replace(context)->*
48 fields_->Replace(context)->*
49 messages_->Replace(context, false)->*
50 $E($C1($V("objc_registerClassPair"), cyc))->*
51 $ CYReturn(cyc)
52 ), super_ == NULL ? $ CYNull() : super_);
53 }
54
55 CYExpression *CYClassExpression::Replace(CYContext &context) {
56 return Replace_(context);
57 }
58
59 CYStatement *CYClassStatement::Replace(CYContext &context) {
60 return $E(Replace_(context));
61 }
62
63 CYStatement *CYField::Replace(CYContext &context) const {
64 return NULL;
65 }
66
67 CYStatement *CYImport::Replace(CYContext &context) {
68 return this;
69 }
70
71 CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
72 CYVariable *cyn($V("$cyn"));
73 CYVariable *cyt($V("$cyt"));
74 CYVariable *self($V("self"));
75 CYVariable *_class($V(instance_ ? "$cys" : "$cyp"));
76
77 return $ CYBlock($$->*
78 next_->Replace(context, replace)->*
79 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
80 $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), _class)))->*
81 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
82 $V(instance_ ? "$cyc" : "$cym"),
83 cyn,
84 $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->*
85 $ CYVar($L1($L($I("$cyr"), $N2($V("Super"), self, _class))))->*
86 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self))
87 ), cyt),
88 cyt
89 ))
90 );
91 }
92
93 CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
94 CYFunctionParameter *next(next_->Parameters(context));
95 return name_ == NULL ? next : $ CYFunctionParameter(name_, next);
96 }
97
98 CYSelector *CYMessageParameter::Selector(CYContext &context) const {
99 return $ CYSelector(SelectorPart(context));
100 }
101
102 CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
103 CYSelectorPart *next(next_->SelectorPart(context));
104 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
105 }
106
107 CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL)
108 return $ CYBlock($$->*
109 next_->Replace(context)->*
110 $E($C2($V("class_addProtocol"),
111 $V("$cyc"), name_
112 ))
113 );
114 }
115
116 CYExpression *CYSelector::Replace(CYContext &context) {
117 return $N1($V("Selector"), name_->Replace(context));
118 }
119
120 CYString *CYSelectorPart::Replace(CYContext &context) {
121 std::ostringstream str;
122 CYForEach (part, this) {
123 if (part->name_ != NULL)
124 str << part->name_->Word();
125 if (part->value_)
126 str << ':';
127 }
128 return $S(apr_pstrdup($pool, str.str().c_str()));
129 }
130
131 CYExpression *CYSendDirect::Replace(CYContext &context) {
132 std::ostringstream name;
133 CYArgument **argument(&arguments_);
134 CYSelectorPart *selector(NULL), *current(NULL);
135
136 while (*argument != NULL) {
137 if ((*argument)->name_ != NULL) {
138 CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL));
139 if (selector == NULL)
140 selector = part;
141 if (current != NULL)
142 current->SetNext(part);
143 current = part;
144 (*argument)->name_ = NULL;
145 }
146
147 if ((*argument)->value_ == NULL)
148 *argument = (*argument)->next_;
149 else
150 argument = &(*argument)->next_;
151 }
152
153 return $C2($V("objc_msgSend"), self_, ($ CYSelector(selector))->Replace(context), arguments_);
154 }
155
156 CYExpression *CYSendSuper::Replace(CYContext &context) {
157 return $ CYSendDirect($V("$cyr"), arguments_);
158 }