]>
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 | ||
20052ff7 JF |
22 | #include <sstream> |
23 | ||
4de0686f | 24 | #include "Replace.hpp" |
4de0686f | 25 | |
20052ff7 | 26 | #include "ObjectiveC/Syntax.hpp" |
4de0686f | 27 | |
57d55714 | 28 | static CYExpression *MessageType(CYContext &context, CYTypedIdentifier *type, CYMessageParameter *next, CYExpression *extra = NULL) { |
57d55714 | 29 | CYExpression *left($C0($M(type->Replace(context), $S("toString")))); |
9a7c375c JF |
30 | if (extra != NULL) |
31 | left = $ CYAdd(left, extra); | |
32 | ||
104cc5f5 | 33 | if (next == NULL || next->type_ == NULL) |
9a7c375c JF |
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 | ||
4de0686f JF |
43 | CYStatement *CYCategory::Replace(CYContext &context) { |
44 | CYVariable *cyc($V("$cyc")), *cys($V("$cys")); | |
45 | ||
09fc3efb | 46 | return $E($C1($F(NULL, $P6($B($I("$cys")), $B($I("$cyp")), $B($I("$cyc")), $B($I("$cyn")), $B($I("$cyt")), $B($I("$cym"))), $$->* |
4de0686f JF |
47 | $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->* |
48 | $E($ CYAssign(cyc, cys))->* | |
725075ae | 49 | $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->* |
4de0686f | 50 | messages_->Replace(context, true) |
c5b15840 | 51 | ), $C1($V("objc_getClass"), $S(name_)))); |
4de0686f JF |
52 | } |
53 | ||
c5b15840 | 54 | CYStatement *CYImplementation::Replace(CYContext &context) { |
4de0686f JF |
55 | CYVariable *cyc($V("$cyc")), *cys($V("$cys")); |
56 | ||
09fc3efb | 57 | return $E($C1($F(NULL, $P6($B($I("$cys")), $B($I("$cyp")), $B($I("$cyc")), $B($I("$cyn")), $B($I("$cyt")), $B($I("$cym"))), $$->* |
4de0686f | 58 | $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->* |
c5b15840 | 59 | $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, $S(name_), $D(0))))->* |
4de0686f | 60 | $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->* |
64b8d29f | 61 | protocols_->Replace(context)->* |
4de0686f JF |
62 | fields_->Replace(context)->* |
63 | messages_->Replace(context, false)->* | |
64 | $E($C1($V("objc_registerClassPair"), cyc))->* | |
65 | $ CYReturn(cyc) | |
09fc3efb | 66 | ), extends_ == NULL ? $ CYNull() : extends_)); |
4de0686f JF |
67 | } |
68 | ||
c5b15840 | 69 | CYStatement *CYImplementationField::Replace(CYContext &context) const { $T(NULL) |
cfd73c6d JF |
70 | CYVariable *cyn($V("$cyn")); |
71 | CYVariable *cyt($V("$cyt")); | |
72 | ||
d2f6e642 | 73 | CYExpression *type($C0($M(typed_->Replace(context), $S("toString")))); |
cfd73c6d JF |
74 | |
75 | return $ CYBlock($$->* | |
cfd73c6d JF |
76 | $E($ CYAssign(cyt, type))->* |
77 | $E($ CYAssign(cyn, $N1($V("Type"), cyt)))->* | |
17f01b7a JF |
78 | $E($C5($V("class_addIvar"), $V("$cyc"), $S(typed_->identifier_->Word()), $M(cyn, $S("size")), $M(cyn, $S("alignment")), cyt))->* |
79 | next_->Replace(context) | |
cfd73c6d | 80 | ); |
4de0686f JF |
81 | } |
82 | ||
7085e1ab | 83 | CYTarget *CYInstanceLiteral::Replace(CYContext &context) { |
61769f4f JF |
84 | return $N1($V("Instance"), number_); |
85 | } | |
86 | ||
4de0686f JF |
87 | CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL) |
88 | CYVariable *cyn($V("$cyn")); | |
89 | CYVariable *cyt($V("$cyt")); | |
cacd1a88 JF |
90 | CYVariable *self($V("self")); |
91 | CYVariable *_class($V(instance_ ? "$cys" : "$cyp")); | |
4de0686f JF |
92 | |
93 | return $ CYBlock($$->* | |
94 | next_->Replace(context, replace)->* | |
95 | $E($ CYAssign(cyn, parameters_->Selector(context)))->* | |
104cc5f5 | 96 | $E($ CYAssign(cyt, TypeSignature(context)))->* |
4de0686f JF |
97 | $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"), |
98 | $V(instance_ ? "$cyc" : "$cym"), | |
99 | cyn, | |
09fc3efb JF |
100 | $N2($V("Functor"), $F(NULL, $P2($B($I("self")), $B($I("_cmd")), parameters_->Parameters(context)), $$->* |
101 | $ CYVar($B1($B($I("$cyr"), $N2($V("objc_super"), self, _class))))->* | |
b0385401 | 102 | $ CYReturn($C1($M($F(NULL, NULL, code_.code_), $S("call")), self)) |
4de0686f JF |
103 | ), cyt), |
104 | cyt | |
105 | )) | |
106 | ); | |
107 | } | |
108 | ||
9a7c375c JF |
109 | CYExpression *CYMessage::TypeSignature(CYContext &context) const { |
110 | return MessageType(context, type_, parameters_, $S("@:")); | |
111 | } | |
112 | ||
4de0686f JF |
113 | CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL) |
114 | CYFunctionParameter *next(next_->Parameters(context)); | |
09fc3efb | 115 | return type_ == NULL ? next : $ CYFunctionParameter($B(type_->identifier_), next); |
4de0686f JF |
116 | } |
117 | ||
118 | CYSelector *CYMessageParameter::Selector(CYContext &context) const { | |
119 | return $ CYSelector(SelectorPart(context)); | |
120 | } | |
121 | ||
122 | CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL) | |
123 | CYSelectorPart *next(next_->SelectorPart(context)); | |
09fc3efb | 124 | return name_ == NULL ? next : $ CYSelectorPart(name_, type_ != NULL, next); |
4de0686f JF |
125 | } |
126 | ||
9a7c375c JF |
127 | CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const { |
128 | return MessageType(context, type_, next_); | |
129 | } | |
130 | ||
7085e1ab | 131 | CYTarget *CYBox::Replace(CYContext &context) { |
f2f0d1d1 JF |
132 | return $C1($M($V("Instance"), $S("box")), value_); |
133 | } | |
134 | ||
5a6c975a JF |
135 | CYTarget *CYObjCArray::Replace(CYContext &context) { |
136 | size_t count(0); | |
137 | CYForEach (element, elements_) | |
138 | ++count; | |
139 | return $ CYSendDirect($V("NSArray"), $C_($ CYWord("arrayWithObjects"), $ CYArray(elements_), $C_($ CYWord("count"), $D(count)))); | |
140 | } | |
141 | ||
142 | CYTarget *CYObjCDictionary::Replace(CYContext &context) { | |
143 | CYList<CYElement> keys; | |
144 | CYList<CYElement> values; | |
145 | size_t count(0); | |
146 | ||
147 | CYForEach (pair, pairs_) { | |
148 | keys->*$ CYElementValue(pair->key_); | |
149 | values->*$ CYElementValue(pair->value_); | |
150 | ++count; | |
151 | } | |
152 | ||
153 | return $ CYSendDirect($V("NSDictionary"), $C_($ CYWord("dictionaryWithObjects"), $ CYArray(values), $C_($ CYWord("forKeys"), $ CYArray(keys), $C_($ CYWord("count"), $D(count))))); | |
154 | } | |
155 | ||
7085e1ab | 156 | CYTarget *CYObjCBlock::Replace(CYContext &context) { |
64a505ff | 157 | return $C1($ CYTypeExpression(($ CYTypedIdentifier(*typed_))->Modify($ CYTypeBlockWith(parameters_))), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_)); |
56e02e5b JF |
158 | } |
159 | ||
64b8d29f JF |
160 | CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL) |
161 | return $ CYBlock($$->* | |
162 | next_->Replace(context)->* | |
163 | $E($C2($V("class_addProtocol"), | |
164 | $V("$cyc"), name_ | |
165 | )) | |
166 | ); | |
167 | } | |
168 | ||
7085e1ab | 169 | CYTarget *CYSelector::Replace(CYContext &context) { |
09fc3efb | 170 | return $C1($V("sel_registerName"), parts_->Replace(context)); |
4de0686f JF |
171 | } |
172 | ||
173 | CYString *CYSelectorPart::Replace(CYContext &context) { | |
174 | std::ostringstream str; | |
c2c9f509 | 175 | CYForEach (part, this) { |
4de0686f | 176 | if (part->name_ != NULL) |
69688ca1 | 177 | str << part->name_->Word(); |
4de0686f JF |
178 | if (part->value_) |
179 | str << ':'; | |
180 | } | |
b799113b | 181 | return $S($pool.strdup(str.str().c_str())); |
4de0686f JF |
182 | } |
183 | ||
7085e1ab | 184 | CYTarget *CYSendDirect::Replace(CYContext &context) { |
4de0686f JF |
185 | std::ostringstream name; |
186 | CYArgument **argument(&arguments_); | |
2385c806 | 187 | CYSelectorPart *selector(NULL), *current(NULL); |
4de0686f JF |
188 | |
189 | while (*argument != NULL) { | |
190 | if ((*argument)->name_ != NULL) { | |
2385c806 JF |
191 | CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL)); |
192 | if (selector == NULL) | |
193 | selector = part; | |
194 | if (current != NULL) | |
195 | current->SetNext(part); | |
196 | current = part; | |
4de0686f | 197 | (*argument)->name_ = NULL; |
4de0686f JF |
198 | } |
199 | ||
200 | if ((*argument)->value_ == NULL) | |
201 | *argument = (*argument)->next_; | |
202 | else | |
203 | argument = &(*argument)->next_; | |
204 | } | |
205 | ||
dbf9d389 | 206 | return $C2($V("objc_msgSend"), self_, selector->Replace(context), arguments_); |
4de0686f | 207 | } |
cacd1a88 | 208 | |
7085e1ab | 209 | CYTarget *CYSendSuper::Replace(CYContext &context) { |
57a65431 | 210 | return $ CYSendDirect($V("$cyr"), arguments_); |
cacd1a88 | 211 | } |