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