]> git.saurik.com Git - cycript.git/blame - ObjectiveC/Replace.cpp
Objective-C class fields must be TypeIdentifiers.
[cycript.git] / ObjectiveC / Replace.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c15969fd 2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
b53b30c1
JF
3*/
4
c15969fd 5/* GNU General Public License, Version 3 {{{ */
b53b30c1 6/*
c15969fd
JF
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
b53b30c1 11 *
c15969fd
JF
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
b53b30c1 16 *
c15969fd 17 * You should have received a copy of the GNU General Public License
b3378a02
JF
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19**/
b53b30c1
JF
20/* }}} */
21
4de0686f 22#include "Replace.hpp"
3c1c3635 23#include "ObjectiveC/Syntax.hpp"
4de0686f 24
4de0686f
JF
25#include <sstream>
26
9a7c375c
JF
27static CYExpression *MessageType(CYContext &context, CYExpression *type, CYMessageParameter *next, CYExpression *extra = NULL) {
28 if (type == NULL)
29 return NULL;
30
31 CYExpression *left($C0($M(type, $S("toString"))));
32 if (extra != NULL)
33 left = $ CYAdd(left, extra);
34
cfd73c6d 35 if (next == NULL || next->name_ == NULL)
9a7c375c
JF
36 return left;
37
38 CYExpression *right(next->TypeSignature(context));
39 if (right == NULL)
40 return NULL;
41
42 return $ CYAdd(left, right);
43}
44
4de0686f
JF
45CYStatement *CYCategory::Replace(CYContext &context) {
46 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
47
b639f46d 48 return $E($C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->*
4de0686f
JF
49 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
50 $E($ CYAssign(cyc, cys))->*
725075ae 51 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
4de0686f
JF
52 messages_->Replace(context, true)
53 ), name_->ClassName(context, true)));
54}
55
56CYExpression *CYClass::Replace_(CYContext &context) {
57 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
58
59 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
60
c8a0500b 61 return $C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->*
4de0686f
JF
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)))->*
64b8d29f 65 protocols_->Replace(context)->*
4de0686f
JF
66 fields_->Replace(context)->*
67 messages_->Replace(context, false)->*
68 $E($C1($V("objc_registerClassPair"), cyc))->*
69 $ CYReturn(cyc)
70 ), super_ == NULL ? $ CYNull() : super_);
71}
72
73CYExpression *CYClassExpression::Replace(CYContext &context) {
74 return Replace_(context);
75}
76
77CYStatement *CYClassStatement::Replace(CYContext &context) {
78 return $E(Replace_(context));
79}
80
cfd73c6d
JF
81CYStatement *CYField::Replace(CYContext &context) const { $T(NULL)
82 CYVariable *cyn($V("$cyn"));
83 CYVariable *cyt($V("$cyt"));
84
d2f6e642 85 CYExpression *type($C0($M(typed_->Replace(context), $S("toString"))));
cfd73c6d
JF
86
87 return $ CYBlock($$->*
88 next_->Replace(context)->*
89 $E($ CYAssign(cyt, type))->*
90 $E($ CYAssign(cyn, $N1($V("Type"), cyt)))->*
d2f6e642 91 $E($C5($V("class_addIvar"), $V("$cyc"), $S(typed_->identifier_->Word()), $M(cyn, $S("size")), $M(cyn, $S("alignment")), cyt))
cfd73c6d 92 );
4de0686f
JF
93}
94
1ba6903e 95CYStatement *CYImport::Replace(CYContext &context) {
417dcc12 96 return $ CYVar($L1($L(module_->part_->Word(), $C1($V("require"), module_->Replace(context, "/")))));
1ba6903e
JF
97}
98
61769f4f
JF
99CYExpression *CYInstanceLiteral::Replace(CYContext &context) {
100 return $N1($V("Instance"), number_);
101}
102
4de0686f
JF
103CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
104 CYVariable *cyn($V("$cyn"));
105 CYVariable *cyt($V("$cyt"));
cacd1a88
JF
106 CYVariable *self($V("self"));
107 CYVariable *_class($V(instance_ ? "$cys" : "$cyp"));
4de0686f 108
9a7c375c
JF
109 CYExpression *type(TypeSignature(context) ?: $C1($M(cyn, $S("type")), _class));
110
4de0686f
JF
111 return $ CYBlock($$->*
112 next_->Replace(context, replace)->*
113 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
9a7c375c 114 $E($ CYAssign(cyt, type))->*
4de0686f
JF
115 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
116 $V(instance_ ? "$cyc" : "$cym"),
117 cyn,
c8a0500b 118 $N2($V("Functor"), $F(NULL, $P2($L("self"), $L("_cmd"), parameters_->Parameters(context)), $$->*
7a3459ce 119 $ CYVar($L1($L("$cyr", $N2($V("objc_super"), self, _class))))->*
cacd1a88 120 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self))
4de0686f
JF
121 ), cyt),
122 cyt
123 ))
124 );
125}
126
9a7c375c
JF
127CYExpression *CYMessage::TypeSignature(CYContext &context) const {
128 return MessageType(context, type_, parameters_, $S("@:"));
129}
130
4de0686f
JF
131CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
132 CYFunctionParameter *next(next_->Parameters(context));
c8a0500b 133 return name_ == NULL ? next : $ CYFunctionParameter($ CYDeclaration(name_), next);
4de0686f
JF
134}
135
136CYSelector *CYMessageParameter::Selector(CYContext &context) const {
137 return $ CYSelector(SelectorPart(context));
138}
139
140CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
141 CYSelectorPart *next(next_->SelectorPart(context));
142 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
143}
144
9a7c375c
JF
145CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const {
146 return MessageType(context, type_, next_);
147}
148
417dcc12
JF
149CYString *CYModule::Replace(CYContext &context, const char *separator) const {
150 if (next_ == NULL)
151 return $ CYString(part_);
152 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
153}
154
f2f0d1d1
JF
155CYExpression *CYBox::Replace(CYContext &context) {
156 return $C1($M($V("Instance"), $S("box")), value_);
157}
158
56e02e5b 159CYExpression *CYObjCBlock::Replace(CYContext &context) {
9fe34350 160 return $C1($ CYEncodedType(($ CYTypedIdentifier(*typed_))->Modify($ CYTypeBlockWith(parameters_))), $ CYFunctionExpression(NULL, parameters_->Parameters(context), statements_));
56e02e5b
JF
161}
162
64b8d29f
JF
163CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL)
164 return $ CYBlock($$->*
165 next_->Replace(context)->*
166 $E($C2($V("class_addProtocol"),
167 $V("$cyc"), name_
168 ))
169 );
170}
171
4de0686f 172CYExpression *CYSelector::Replace(CYContext &context) {
575f1f1b 173 return $C1($V("sel_registerName"), name_->Replace(context));
4de0686f
JF
174}
175
176CYString *CYSelectorPart::Replace(CYContext &context) {
177 std::ostringstream str;
c2c9f509 178 CYForEach (part, this) {
4de0686f 179 if (part->name_ != NULL)
69688ca1 180 str << part->name_->Word();
4de0686f
JF
181 if (part->value_)
182 str << ':';
183 }
b799113b 184 return $S($pool.strdup(str.str().c_str()));
4de0686f
JF
185}
186
cacd1a88 187CYExpression *CYSendDirect::Replace(CYContext &context) {
4de0686f
JF
188 std::ostringstream name;
189 CYArgument **argument(&arguments_);
2385c806 190 CYSelectorPart *selector(NULL), *current(NULL);
4de0686f
JF
191
192 while (*argument != NULL) {
193 if ((*argument)->name_ != NULL) {
2385c806
JF
194 CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL));
195 if (selector == NULL)
196 selector = part;
197 if (current != NULL)
198 current->SetNext(part);
199 current = part;
4de0686f 200 (*argument)->name_ = NULL;
4de0686f
JF
201 }
202
203 if ((*argument)->value_ == NULL)
204 *argument = (*argument)->next_;
205 else
206 argument = &(*argument)->next_;
207 }
208
2385c806 209 return $C2($V("objc_msgSend"), self_, ($ CYSelector(selector))->Replace(context), arguments_);
4de0686f 210}
cacd1a88
JF
211
212CYExpression *CYSendSuper::Replace(CYContext &context) {
57a65431 213 return $ CYSendDirect($V("$cyr"), arguments_);
cacd1a88 214}