]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c1d3e52e | 2 | * Copyright (C) 2009-2015 Jay Freeman (saurik) |
b53b30c1 JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
b53b30c1 | 6 | /* |
f95d2598 JF |
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 | |
c15969fd | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f95d2598 JF |
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/>. | |
b3378a02 | 19 | **/ |
b53b30c1 JF |
20 | /* }}} */ |
21 | ||
4de0686f | 22 | #include "Replace.hpp" |
3c1c3635 | 23 | #include "ObjectiveC/Syntax.hpp" |
4de0686f | 24 | |
4de0686f JF |
25 | #include <sstream> |
26 | ||
57d55714 | 27 | static CYExpression *MessageType(CYContext &context, CYTypedIdentifier *type, CYMessageParameter *next, CYExpression *extra = NULL) { |
57d55714 | 28 | CYExpression *left($C0($M(type->Replace(context), $S("toString")))); |
9a7c375c JF |
29 | if (extra != NULL) |
30 | left = $ CYAdd(left, extra); | |
31 | ||
104cc5f5 | 32 | if (next == NULL || next->type_ == NULL) |
9a7c375c JF |
33 | return left; |
34 | ||
35 | CYExpression *right(next->TypeSignature(context)); | |
36 | if (right == NULL) | |
37 | return NULL; | |
38 | ||
39 | return $ CYAdd(left, right); | |
40 | } | |
41 | ||
4de0686f JF |
42 | CYStatement *CYCategory::Replace(CYContext &context) { |
43 | CYVariable *cyc($V("$cyc")), *cys($V("$cys")); | |
44 | ||
b639f46d | 45 | return $E($C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->* |
4de0686f JF |
46 | $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->* |
47 | $E($ CYAssign(cyc, cys))->* | |
725075ae | 48 | $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->* |
4de0686f JF |
49 | messages_->Replace(context, true) |
50 | ), name_->ClassName(context, true))); | |
51 | } | |
52 | ||
53 | CYExpression *CYClass::Replace_(CYContext &context) { | |
54 | CYVariable *cyc($V("$cyc")), *cys($V("$cys")); | |
55 | ||
56 | CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$"))); | |
57 | ||
c8a0500b | 58 | return $C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->* |
4de0686f JF |
59 | $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->* |
60 | $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->* | |
61 | $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->* | |
64b8d29f | 62 | protocols_->Replace(context)->* |
4de0686f JF |
63 | fields_->Replace(context)->* |
64 | messages_->Replace(context, false)->* | |
65 | $E($C1($V("objc_registerClassPair"), cyc))->* | |
66 | $ CYReturn(cyc) | |
67 | ), super_ == NULL ? $ CYNull() : super_); | |
68 | } | |
69 | ||
70 | CYExpression *CYClassExpression::Replace(CYContext &context) { | |
71 | return Replace_(context); | |
72 | } | |
73 | ||
74 | CYStatement *CYClassStatement::Replace(CYContext &context) { | |
75 | return $E(Replace_(context)); | |
76 | } | |
77 | ||
b6a67580 | 78 | CYStatement *CYClassField::Replace(CYContext &context) const { $T(NULL) |
cfd73c6d JF |
79 | CYVariable *cyn($V("$cyn")); |
80 | CYVariable *cyt($V("$cyt")); | |
81 | ||
d2f6e642 | 82 | CYExpression *type($C0($M(typed_->Replace(context), $S("toString")))); |
cfd73c6d JF |
83 | |
84 | return $ CYBlock($$->* | |
cfd73c6d JF |
85 | $E($ CYAssign(cyt, type))->* |
86 | $E($ CYAssign(cyn, $N1($V("Type"), cyt)))->* | |
17f01b7a JF |
87 | $E($C5($V("class_addIvar"), $V("$cyc"), $S(typed_->identifier_->Word()), $M(cyn, $S("size")), $M(cyn, $S("alignment")), cyt))->* |
88 | next_->Replace(context) | |
cfd73c6d | 89 | ); |
4de0686f JF |
90 | } |
91 | ||
61769f4f JF |
92 | CYExpression *CYInstanceLiteral::Replace(CYContext &context) { |
93 | return $N1($V("Instance"), number_); | |
94 | } | |
95 | ||
4de0686f JF |
96 | CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL) |
97 | CYVariable *cyn($V("$cyn")); | |
98 | CYVariable *cyt($V("$cyt")); | |
cacd1a88 JF |
99 | CYVariable *self($V("self")); |
100 | CYVariable *_class($V(instance_ ? "$cys" : "$cyp")); | |
4de0686f JF |
101 | |
102 | return $ CYBlock($$->* | |
103 | next_->Replace(context, replace)->* | |
104 | $E($ CYAssign(cyn, parameters_->Selector(context)))->* | |
104cc5f5 | 105 | $E($ CYAssign(cyt, TypeSignature(context)))->* |
4de0686f JF |
106 | $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"), |
107 | $V(instance_ ? "$cyc" : "$cym"), | |
108 | cyn, | |
c8a0500b | 109 | $N2($V("Functor"), $F(NULL, $P2($L("self"), $L("_cmd"), parameters_->Parameters(context)), $$->* |
7a3459ce | 110 | $ CYVar($L1($L("$cyr", $N2($V("objc_super"), self, _class))))->* |
b0385401 | 111 | $ CYReturn($C1($M($F(NULL, NULL, code_.code_), $S("call")), self)) |
4de0686f JF |
112 | ), cyt), |
113 | cyt | |
114 | )) | |
115 | ); | |
116 | } | |
117 | ||
9a7c375c JF |
118 | CYExpression *CYMessage::TypeSignature(CYContext &context) const { |
119 | return MessageType(context, type_, parameters_, $S("@:")); | |
120 | } | |
121 | ||
4de0686f JF |
122 | CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL) |
123 | CYFunctionParameter *next(next_->Parameters(context)); | |
104cc5f5 | 124 | return type_ == NULL ? next : $ CYFunctionParameter($ CYDeclaration(type_->identifier_), next); |
4de0686f JF |
125 | } |
126 | ||
127 | CYSelector *CYMessageParameter::Selector(CYContext &context) const { | |
128 | return $ CYSelector(SelectorPart(context)); | |
129 | } | |
130 | ||
131 | CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL) | |
132 | CYSelectorPart *next(next_->SelectorPart(context)); | |
104cc5f5 | 133 | return tag_ == NULL ? next : $ CYSelectorPart(tag_, type_ != NULL, next); |
4de0686f JF |
134 | } |
135 | ||
9a7c375c JF |
136 | CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const { |
137 | return MessageType(context, type_, next_); | |
138 | } | |
139 | ||
f2f0d1d1 JF |
140 | CYExpression *CYBox::Replace(CYContext &context) { |
141 | return $C1($M($V("Instance"), $S("box")), value_); | |
142 | } | |
143 | ||
56e02e5b | 144 | CYExpression *CYObjCBlock::Replace(CYContext &context) { |
b0385401 | 145 | return $C1($ CYEncodedType(($ CYTypedIdentifier(*typed_))->Modify($ CYTypeBlockWith(parameters_))), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_)); |
56e02e5b JF |
146 | } |
147 | ||
64b8d29f JF |
148 | CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL) |
149 | return $ CYBlock($$->* | |
150 | next_->Replace(context)->* | |
151 | $E($C2($V("class_addProtocol"), | |
152 | $V("$cyc"), name_ | |
153 | )) | |
154 | ); | |
155 | } | |
156 | ||
4de0686f | 157 | CYExpression *CYSelector::Replace(CYContext &context) { |
575f1f1b | 158 | return $C1($V("sel_registerName"), name_->Replace(context)); |
4de0686f JF |
159 | } |
160 | ||
161 | CYString *CYSelectorPart::Replace(CYContext &context) { | |
162 | std::ostringstream str; | |
c2c9f509 | 163 | CYForEach (part, this) { |
4de0686f | 164 | if (part->name_ != NULL) |
69688ca1 | 165 | str << part->name_->Word(); |
4de0686f JF |
166 | if (part->value_) |
167 | str << ':'; | |
168 | } | |
b799113b | 169 | return $S($pool.strdup(str.str().c_str())); |
4de0686f JF |
170 | } |
171 | ||
cacd1a88 | 172 | CYExpression *CYSendDirect::Replace(CYContext &context) { |
4de0686f JF |
173 | std::ostringstream name; |
174 | CYArgument **argument(&arguments_); | |
2385c806 | 175 | CYSelectorPart *selector(NULL), *current(NULL); |
4de0686f JF |
176 | |
177 | while (*argument != NULL) { | |
178 | if ((*argument)->name_ != NULL) { | |
2385c806 JF |
179 | CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL)); |
180 | if (selector == NULL) | |
181 | selector = part; | |
182 | if (current != NULL) | |
183 | current->SetNext(part); | |
184 | current = part; | |
4de0686f | 185 | (*argument)->name_ = NULL; |
4de0686f JF |
186 | } |
187 | ||
188 | if ((*argument)->value_ == NULL) | |
189 | *argument = (*argument)->next_; | |
190 | else | |
191 | argument = &(*argument)->next_; | |
192 | } | |
193 | ||
dbf9d389 | 194 | return $C2($V("objc_msgSend"), self_, selector->Replace(context), arguments_); |
4de0686f | 195 | } |
cacd1a88 JF |
196 | |
197 | CYExpression *CYSendSuper::Replace(CYContext &context) { | |
57a65431 | 198 | return $ CYSendDirect($V("$cyr"), arguments_); |
cacd1a88 | 199 | } |