]> git.saurik.com Git - cycript.git/blob - ObjectiveC/Replace.cpp
Flex drove me crazy with "you just got jammed" :/.
[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 "Replace.hpp"
23 #include "ObjectiveC/Syntax.hpp"
24
25 #include <sstream>
26
27 static CYExpression *MessageType(CYContext &context, CYTypedIdentifier *type, CYMessageParameter *next, CYExpression *extra = NULL) {
28 CYExpression *left($C0($M(type->Replace(context), $S("toString"))));
29 if (extra != NULL)
30 left = $ CYAdd(left, extra);
31
32 if (next == NULL || next->type_ == NULL)
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
42 CYStatement *CYCategory::Replace(CYContext &context) {
43 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
44
45 return $E($C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->*
46 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
47 $E($ CYAssign(cyc, cys))->*
48 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
49 messages_->Replace(context, true)
50 ), name_->ClassName(context, true)));
51 }
52
53 CYStatement *CYClassStatement::Replace(CYContext &context) {
54 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
55
56 CYExpression *name(name_->ClassName(context, false));
57
58 return $E($C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->*
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)))->*
62 protocols_->Replace(context)->*
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 CYStatement *CYClassField::Replace(CYContext &context) const { $T(NULL)
71 CYVariable *cyn($V("$cyn"));
72 CYVariable *cyt($V("$cyt"));
73
74 CYExpression *type($C0($M(typed_->Replace(context), $S("toString"))));
75
76 return $ CYBlock($$->*
77 $E($ CYAssign(cyt, type))->*
78 $E($ CYAssign(cyn, $N1($V("Type"), cyt)))->*
79 $E($C5($V("class_addIvar"), $V("$cyc"), $S(typed_->identifier_->Word()), $M(cyn, $S("size")), $M(cyn, $S("alignment")), cyt))->*
80 next_->Replace(context)
81 );
82 }
83
84 CYExpression *CYInstanceLiteral::Replace(CYContext &context) {
85 return $N1($V("Instance"), number_);
86 }
87
88 CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
89 CYVariable *cyn($V("$cyn"));
90 CYVariable *cyt($V("$cyt"));
91 CYVariable *self($V("self"));
92 CYVariable *_class($V(instance_ ? "$cys" : "$cyp"));
93
94 return $ CYBlock($$->*
95 next_->Replace(context, replace)->*
96 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
97 $E($ CYAssign(cyt, TypeSignature(context)))->*
98 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
99 $V(instance_ ? "$cyc" : "$cym"),
100 cyn,
101 $N2($V("Functor"), $F(NULL, $P2($L("self"), $L("_cmd"), parameters_->Parameters(context)), $$->*
102 $ CYVar($L1($L("$cyr", $N2($V("objc_super"), self, _class))))->*
103 $ CYReturn($C1($M($F(NULL, NULL, code_.code_), $S("call")), self))
104 ), cyt),
105 cyt
106 ))
107 );
108 }
109
110 CYExpression *CYMessage::TypeSignature(CYContext &context) const {
111 return MessageType(context, type_, parameters_, $S("@:"));
112 }
113
114 CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
115 CYFunctionParameter *next(next_->Parameters(context));
116 return type_ == NULL ? next : $ CYFunctionParameter($ CYDeclaration(type_->identifier_), next);
117 }
118
119 CYSelector *CYMessageParameter::Selector(CYContext &context) const {
120 return $ CYSelector(SelectorPart(context));
121 }
122
123 CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
124 CYSelectorPart *next(next_->SelectorPart(context));
125 return tag_ == NULL ? next : $ CYSelectorPart(tag_, type_ != NULL, next);
126 }
127
128 CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const {
129 return MessageType(context, type_, next_);
130 }
131
132 CYExpression *CYBox::Replace(CYContext &context) {
133 return $C1($M($V("Instance"), $S("box")), value_);
134 }
135
136 CYExpression *CYObjCBlock::Replace(CYContext &context) {
137 return $C1($ CYEncodedType(($ CYTypedIdentifier(*typed_))->Modify($ CYTypeBlockWith(parameters_))), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_));
138 }
139
140 CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL)
141 return $ CYBlock($$->*
142 next_->Replace(context)->*
143 $E($C2($V("class_addProtocol"),
144 $V("$cyc"), name_
145 ))
146 );
147 }
148
149 CYExpression *CYSelector::Replace(CYContext &context) {
150 return $C1($V("sel_registerName"), name_->Replace(context));
151 }
152
153 CYString *CYSelectorPart::Replace(CYContext &context) {
154 std::ostringstream str;
155 CYForEach (part, this) {
156 if (part->name_ != NULL)
157 str << part->name_->Word();
158 if (part->value_)
159 str << ':';
160 }
161 return $S($pool.strdup(str.str().c_str()));
162 }
163
164 CYExpression *CYSendDirect::Replace(CYContext &context) {
165 std::ostringstream name;
166 CYArgument **argument(&arguments_);
167 CYSelectorPart *selector(NULL), *current(NULL);
168
169 while (*argument != NULL) {
170 if ((*argument)->name_ != NULL) {
171 CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL));
172 if (selector == NULL)
173 selector = part;
174 if (current != NULL)
175 current->SetNext(part);
176 current = part;
177 (*argument)->name_ = NULL;
178 }
179
180 if ((*argument)->value_ == NULL)
181 *argument = (*argument)->next_;
182 else
183 argument = &(*argument)->next_;
184 }
185
186 return $C2($V("objc_msgSend"), self_, selector->Replace(context), arguments_);
187 }
188
189 CYExpression *CYSendSuper::Replace(CYContext &context) {
190 return $ CYSendDirect($V("$cyr"), arguments_);
191 }