]>
git.saurik.com Git - cycript.git/blob - ObjectiveC/Replace.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #include "Replace.hpp"
23 #include "ObjectiveC/Syntax.hpp"
27 static CYExpression
*MessageType(CYContext
&context
, CYExpression
*type
, CYMessageParameter
*next
, CYExpression
*extra
= NULL
) {
31 CYExpression
*left($
C0($
M(type
, $
S("toString"))));
33 left
= $
CYAdd(left
, extra
);
35 if (next
== NULL
|| next
->name_
== NULL
)
38 CYExpression
*right(next
->TypeSignature(context
));
42 return $
CYAdd(left
, right
);
45 CYStatement
*CYCategory::Replace(CYContext
&context
) {
46 CYVariable
*cyc($
V("$cyc")), *cys($
V("$cys"));
48 return $
E($
C1($
F(NULL
, $
P6($
L("$cys"), $
L("$cyp"), $
L("$cyc"), $
L("$cyn"), $
L("$cyt"), $
L("$cym")), $$
->*
49 $
E($
CYAssign($
V("$cyp"), $
C1($
V("object_getClass"), cys
)))->*
50 $
E($
CYAssign(cyc
, cys
))->*
51 $
E($
CYAssign($
V("$cym"), $
C1($
V("object_getClass"), cyc
)))->*
52 messages_
->Replace(context
, true)
53 ), name_
->ClassName(context
, true)));
56 CYExpression
*CYClass::Replace_(CYContext
&context
) {
57 CYVariable
*cyc($
V("$cyc")), *cys($
V("$cys"));
59 CYExpression
*name(name_
!= NULL
? name_
->ClassName(context
, false) : $
C1($
V("$cyq"), $
S("CY$")));
61 return $
C1($
F(NULL
, $
P6($
L("$cys"), $
L("$cyp"), $
L("$cyc"), $
L("$cyn"), $
L("$cyt"), $
L("$cym")), $$
->*
62 $
E($
CYAssign($
V("$cyp"), $
C1($
V("object_getClass"), cys
)))->*
63 $
E($
CYAssign(cyc
, $
C3($
V("objc_allocateClassPair"), cys
, name
, $
D(0))))->*
64 $
E($
CYAssign($
V("$cym"), $
C1($
V("object_getClass"), cyc
)))->*
65 protocols_
->Replace(context
)->*
66 fields_
->Replace(context
)->*
67 messages_
->Replace(context
, false)->*
68 $
E($
C1($
V("objc_registerClassPair"), cyc
))->*
70 ), super_
== NULL
? $
CYNull() : super_
);
73 CYExpression
*CYClassExpression::Replace(CYContext
&context
) {
74 return Replace_(context
);
77 CYStatement
*CYClassStatement::Replace(CYContext
&context
) {
78 return $
E(Replace_(context
));
81 CYExpression
*CYEncodedPart::Replace(CYContext
&context
, CYExpression
*base
) { $
T(base
)
82 return next_
->Replace(context
, $
CYCall($
CYDirectMember(base
, $
CYString(name_
)), arguments_
));
85 CYExpression
*CYEncodedType::Replace(CYContext
&context
) {
86 return parts_
->Replace(context
, base_
);
89 CYStatement
*CYField::Replace(CYContext
&context
) const { $
T(NULL
)
90 CYVariable
*cyn($
V("$cyn"));
91 CYVariable
*cyt($
V("$cyt"));
93 CYExpression
*type($
C0($
M(type_
, $
S("toString"))));
95 return $
CYBlock($$
->*
96 next_
->Replace(context
)->*
97 $
E($
CYAssign(cyt
, type
))->*
98 $
E($
CYAssign(cyn
, $
N1($
V("Type"), cyt
)))->*
99 $
E($
C5($
V("class_addIvar"), $
V("$cyc"), $
S(name_
->Word()), $
M(cyn
, $
S("size")), $
M(cyn
, $
S("alignment")), cyt
))
103 CYStatement
*CYImport::Replace(CYContext
&context
) {
107 CYStatement
*CYMessage::Replace(CYContext
&context
, bool replace
) const { $
T(NULL
)
108 CYVariable
*cyn($
V("$cyn"));
109 CYVariable
*cyt($
V("$cyt"));
110 CYVariable
*self($
V("self"));
111 CYVariable
*_class($
V(instance_
? "$cys" : "$cyp"));
113 CYExpression
*type(TypeSignature(context
) ?: $
C1($
M(cyn
, $
S("type")), _class
));
115 return $
CYBlock($$
->*
116 next_
->Replace(context
, replace
)->*
117 $
E($
CYAssign(cyn
, parameters_
->Selector(context
)))->*
118 $
E($
CYAssign(cyt
, type
))->*
119 $
E($
C4($
V(replace
? "class_replaceMethod" : "class_addMethod"),
120 $
V(instance_
? "$cyc" : "$cym"),
122 $
N2($
V("Functor"), $
F(NULL
, $
P2($
L("self"), $
L("_cmd"), parameters_
->Parameters(context
)), $$
->*
123 $
CYVar($
L1($
L("$cyr", $
N2($
V("Super"), self
, _class
))))->*
124 $
CYReturn($
C1($
M($
F(NULL
, NULL
, code_
), $
S("call")), self
))
131 CYExpression
*CYMessage::TypeSignature(CYContext
&context
) const {
132 return MessageType(context
, type_
, parameters_
, $
S("@:"));
135 CYFunctionParameter
*CYMessageParameter::Parameters(CYContext
&context
) const { $
T(NULL
)
136 CYFunctionParameter
*next(next_
->Parameters(context
));
137 return name_
== NULL
? next
: $
CYFunctionParameter($
CYDeclaration(name_
), next
);
140 CYSelector
*CYMessageParameter::Selector(CYContext
&context
) const {
141 return $
CYSelector(SelectorPart(context
));
144 CYSelectorPart
*CYMessageParameter::SelectorPart(CYContext
&context
) const { $
T(NULL
)
145 CYSelectorPart
*next(next_
->SelectorPart(context
));
146 return tag_
== NULL
? next
: $
CYSelectorPart(tag_
, name_
!= NULL
, next
);
149 CYExpression
*CYMessageParameter::TypeSignature(CYContext
&context
) const {
150 return MessageType(context
, type_
, next_
);
153 CYExpression
*CYBox::Replace(CYContext
&context
) {
154 return $
C1($
M($
V("Instance"), $
S("box")), value_
);
157 CYStatement
*CYProtocol::Replace(CYContext
&context
) const { $
T(NULL
)
158 return $
CYBlock($$
->*
159 next_
->Replace(context
)->*
160 $
E($
C2($
V("class_addProtocol"),
166 CYExpression
*CYSelector::Replace(CYContext
&context
) {
167 return $
C1($
V("sel_registerName"), name_
->Replace(context
));
170 CYString
*CYSelectorPart::Replace(CYContext
&context
) {
171 std::ostringstream str
;
172 CYForEach (part
, this) {
173 if (part
->name_
!= NULL
)
174 str
<< part
->name_
->Word();
178 return $
S(apr_pstrdup($pool
, str
.str().c_str()));
181 CYExpression
*CYSendDirect::Replace(CYContext
&context
) {
182 std::ostringstream name
;
183 CYArgument
**argument(&arguments_
);
184 CYSelectorPart
*selector(NULL
), *current(NULL
);
186 while (*argument
!= NULL
) {
187 if ((*argument
)->name_
!= NULL
) {
188 CYSelectorPart
*part($
CYSelectorPart((*argument
)->name_
, (*argument
)->value_
!= NULL
));
189 if (selector
== NULL
)
192 current
->SetNext(part
);
194 (*argument
)->name_
= NULL
;
197 if ((*argument
)->value_
== NULL
)
198 *argument
= (*argument
)->next_
;
200 argument
= &(*argument
)->next_
;
203 return $
C2($
V("objc_msgSend"), self_
, ($
CYSelector(selector
))->Replace(context
), arguments_
);
206 CYExpression
*CYSendSuper::Replace(CYContext
&context
) {
207 return $
CYSendDirect($
V("$cyr"), arguments_
);