]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Optimizing JavaScript Compiler/Runtime | |
2 | * Copyright (C) 2009-2012 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 | static 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 | ||
35 | if (next == NULL || next->name_ == NULL) | |
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 | ||
45 | CYStatement *CYCategory::Replace(CYContext &context) { | |
46 | CYVariable *cyc($V("$cyc")), *cys($V("$cys")); | |
47 | ||
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))); | |
54 | } | |
55 | ||
56 | CYExpression *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 | ||
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))->* | |
69 | $ CYReturn(cyc) | |
70 | ), super_ == NULL ? $ CYNull() : super_); | |
71 | } | |
72 | ||
73 | CYExpression *CYClassExpression::Replace(CYContext &context) { | |
74 | return Replace_(context); | |
75 | } | |
76 | ||
77 | CYStatement *CYClassStatement::Replace(CYContext &context) { | |
78 | return $E(Replace_(context)); | |
79 | } | |
80 | ||
81 | CYExpression *CYTypeArrayOf::Replace(CYContext &context) { | |
82 | return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("arrayOf")), $ CYArgument($ CYNumber(size_))); | |
83 | } | |
84 | ||
85 | CYExpression *CYTypeConstant::Replace(CYContext &context) { | |
86 | return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("constant"))); | |
87 | } | |
88 | ||
89 | CYExpression *CYTypePointerTo::Replace(CYContext &context) { | |
90 | return $ CYCall($ CYDirectMember(next_->Replace(context), $ CYString("pointerTo"))); | |
91 | } | |
92 | ||
93 | CYExpression *CYTypeVariable::Replace(CYContext &context) { | |
94 | return expression_; | |
95 | } | |
96 | ||
97 | CYExpression *CYEncodedType::Replace(CYContext &context) { | |
98 | return type_->Replace(context); | |
99 | } | |
100 | ||
101 | CYStatement *CYField::Replace(CYContext &context) const { $T(NULL) | |
102 | CYVariable *cyn($V("$cyn")); | |
103 | CYVariable *cyt($V("$cyt")); | |
104 | ||
105 | CYExpression *type($C0($M(type_, $S("toString")))); | |
106 | ||
107 | return $ CYBlock($$->* | |
108 | next_->Replace(context)->* | |
109 | $E($ CYAssign(cyt, type))->* | |
110 | $E($ CYAssign(cyn, $N1($V("Type"), cyt)))->* | |
111 | $E($C5($V("class_addIvar"), $V("$cyc"), $S(name_->Word()), $M(cyn, $S("size")), $M(cyn, $S("alignment")), cyt)) | |
112 | ); | |
113 | } | |
114 | ||
115 | CYStatement *CYImport::Replace(CYContext &context) { | |
116 | return this; | |
117 | } | |
118 | ||
119 | CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL) | |
120 | CYVariable *cyn($V("$cyn")); | |
121 | CYVariable *cyt($V("$cyt")); | |
122 | CYVariable *self($V("self")); | |
123 | CYVariable *_class($V(instance_ ? "$cys" : "$cyp")); | |
124 | ||
125 | CYExpression *type(TypeSignature(context) ?: $C1($M(cyn, $S("type")), _class)); | |
126 | ||
127 | return $ CYBlock($$->* | |
128 | next_->Replace(context, replace)->* | |
129 | $E($ CYAssign(cyn, parameters_->Selector(context)))->* | |
130 | $E($ CYAssign(cyt, type))->* | |
131 | $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"), | |
132 | $V(instance_ ? "$cyc" : "$cym"), | |
133 | cyn, | |
134 | $N2($V("Functor"), $F(NULL, $P2($L("self"), $L("_cmd"), parameters_->Parameters(context)), $$->* | |
135 | $ CYVar($L1($L("$cyr", $N2($V("Super"), self, _class))))->* | |
136 | $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self)) | |
137 | ), cyt), | |
138 | cyt | |
139 | )) | |
140 | ); | |
141 | } | |
142 | ||
143 | CYExpression *CYMessage::TypeSignature(CYContext &context) const { | |
144 | return MessageType(context, type_, parameters_, $S("@:")); | |
145 | } | |
146 | ||
147 | CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL) | |
148 | CYFunctionParameter *next(next_->Parameters(context)); | |
149 | return name_ == NULL ? next : $ CYFunctionParameter($ CYDeclaration(name_), next); | |
150 | } | |
151 | ||
152 | CYSelector *CYMessageParameter::Selector(CYContext &context) const { | |
153 | return $ CYSelector(SelectorPart(context)); | |
154 | } | |
155 | ||
156 | CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL) | |
157 | CYSelectorPart *next(next_->SelectorPart(context)); | |
158 | return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next); | |
159 | } | |
160 | ||
161 | CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const { | |
162 | return MessageType(context, type_, next_); | |
163 | } | |
164 | ||
165 | CYExpression *CYBox::Replace(CYContext &context) { | |
166 | return $C1($M($V("Instance"), $S("box")), value_); | |
167 | } | |
168 | ||
169 | CYExpression *CYObjCBlock::Replace(CYContext &context) { | |
170 | return $N2($V("Functor"), $ CYFunctionExpression(NULL, $ CYFunctionParameter($ CYDeclaration($ CYIdentifier("$cyt")), parameters_->Parameters(context)), statements_), parameters_->TypeSignature(context, $ CYAdd(type_->Replace(context), $ CYString("@")))); | |
171 | } | |
172 | ||
173 | CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL) | |
174 | return $ CYBlock($$->* | |
175 | next_->Replace(context)->* | |
176 | $E($C2($V("class_addProtocol"), | |
177 | $V("$cyc"), name_ | |
178 | )) | |
179 | ); | |
180 | } | |
181 | ||
182 | CYExpression *CYSelector::Replace(CYContext &context) { | |
183 | return $C1($V("sel_registerName"), name_->Replace(context)); | |
184 | } | |
185 | ||
186 | CYString *CYSelectorPart::Replace(CYContext &context) { | |
187 | std::ostringstream str; | |
188 | CYForEach (part, this) { | |
189 | if (part->name_ != NULL) | |
190 | str << part->name_->Word(); | |
191 | if (part->value_) | |
192 | str << ':'; | |
193 | } | |
194 | return $S(apr_pstrdup($pool, str.str().c_str())); | |
195 | } | |
196 | ||
197 | CYExpression *CYSendDirect::Replace(CYContext &context) { | |
198 | std::ostringstream name; | |
199 | CYArgument **argument(&arguments_); | |
200 | CYSelectorPart *selector(NULL), *current(NULL); | |
201 | ||
202 | while (*argument != NULL) { | |
203 | if ((*argument)->name_ != NULL) { | |
204 | CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL)); | |
205 | if (selector == NULL) | |
206 | selector = part; | |
207 | if (current != NULL) | |
208 | current->SetNext(part); | |
209 | current = part; | |
210 | (*argument)->name_ = NULL; | |
211 | } | |
212 | ||
213 | if ((*argument)->value_ == NULL) | |
214 | *argument = (*argument)->next_; | |
215 | else | |
216 | argument = &(*argument)->next_; | |
217 | } | |
218 | ||
219 | return $C2($V("objc_msgSend"), self_, ($ CYSelector(selector))->Replace(context), arguments_); | |
220 | } | |
221 | ||
222 | CYExpression *CYSendSuper::Replace(CYContext &context) { | |
223 | return $ CYSendDirect($V("$cyr"), arguments_); | |
224 | } | |
225 | ||
226 | CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL) | |
227 | return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_), next_->Parameters(context)); | |
228 | } | |
229 | ||
230 | CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix) | |
231 | return next_->TypeSignature(context, $ CYAdd(prefix, typed_->type_->Replace(context))); | |
232 | } |