]> git.saurik.com Git - cycript.git/blame - ObjectiveC/Replace.cpp
Add static size and alignment properties to Type.
[cycript.git] / ObjectiveC / Replace.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
8d7447c1 2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
b53b30c1
JF
3*/
4
b3378a02 5/* GNU Lesser General Public License, Version 3 {{{ */
b53b30c1 6/*
b3378a02
JF
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.
b53b30c1 11 *
b3378a02
JF
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.
b53b30c1 16 *
b3378a02
JF
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**/
b53b30c1
JF
20/* }}} */
21
4de0686f 22#include "Replace.hpp"
3c1c3635 23#include "ObjectiveC/Syntax.hpp"
4de0686f 24
4de0686f
JF
25#include <sstream>
26
9a7c375c
JF
27static 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)
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
4de0686f
JF
45CYStatement *CYCategory::Replace(CYContext &context) {
46 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
47
b639f46d 48 return $E($C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->*
4de0686f
JF
49 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
50 $E($ CYAssign(cyc, cys))->*
725075ae 51 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
4de0686f
JF
52 messages_->Replace(context, true)
53 ), name_->ClassName(context, true)));
54}
55
56CYExpression *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
c8a0500b 61 return $C1($F(NULL, $P6($L("$cys"), $L("$cyp"), $L("$cyc"), $L("$cyn"), $L("$cyt"), $L("$cym")), $$->*
4de0686f
JF
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)))->*
64b8d29f 65 protocols_->Replace(context)->*
4de0686f
JF
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
73CYExpression *CYClassExpression::Replace(CYContext &context) {
74 return Replace_(context);
75}
76
77CYStatement *CYClassStatement::Replace(CYContext &context) {
78 return $E(Replace_(context));
79}
80
81CYStatement *CYField::Replace(CYContext &context) const {
82 return NULL;
83}
84
1ba6903e
JF
85CYStatement *CYImport::Replace(CYContext &context) {
86 return this;
87}
88
4de0686f
JF
89CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
90 CYVariable *cyn($V("$cyn"));
91 CYVariable *cyt($V("$cyt"));
cacd1a88
JF
92 CYVariable *self($V("self"));
93 CYVariable *_class($V(instance_ ? "$cys" : "$cyp"));
4de0686f 94
9a7c375c
JF
95 CYExpression *type(TypeSignature(context) ?: $C1($M(cyn, $S("type")), _class));
96
4de0686f
JF
97 return $ CYBlock($$->*
98 next_->Replace(context, replace)->*
99 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
9a7c375c 100 $E($ CYAssign(cyt, type))->*
4de0686f
JF
101 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
102 $V(instance_ ? "$cyc" : "$cym"),
103 cyn,
c8a0500b
JF
104 $N2($V("Functor"), $F(NULL, $P2($L("self"), $L("_cmd"), parameters_->Parameters(context)), $$->*
105 $ CYVar($L1($L("$cyr", $N2($V("Super"), self, _class))))->*
cacd1a88 106 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self))
4de0686f
JF
107 ), cyt),
108 cyt
109 ))
110 );
111}
112
9a7c375c
JF
113CYExpression *CYMessage::TypeSignature(CYContext &context) const {
114 return MessageType(context, type_, parameters_, $S("@:"));
115}
116
4de0686f
JF
117CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
118 CYFunctionParameter *next(next_->Parameters(context));
c8a0500b 119 return name_ == NULL ? next : $ CYFunctionParameter($ CYDeclaration(name_), next);
4de0686f
JF
120}
121
122CYSelector *CYMessageParameter::Selector(CYContext &context) const {
123 return $ CYSelector(SelectorPart(context));
124}
125
126CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
127 CYSelectorPart *next(next_->SelectorPart(context));
128 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
129}
130
9a7c375c
JF
131CYExpression *CYMessageParameter::TypeSignature(CYContext &context) const {
132 return MessageType(context, type_, next_);
133}
134
f2f0d1d1
JF
135CYExpression *CYBox::Replace(CYContext &context) {
136 return $C1($M($V("Instance"), $S("box")), value_);
137}
138
64b8d29f
JF
139CYStatement *CYProtocol::Replace(CYContext &context) const { $T(NULL)
140 return $ CYBlock($$->*
141 next_->Replace(context)->*
142 $E($C2($V("class_addProtocol"),
143 $V("$cyc"), name_
144 ))
145 );
146}
147
4de0686f
JF
148CYExpression *CYSelector::Replace(CYContext &context) {
149 return $N1($V("Selector"), name_->Replace(context));
150}
151
152CYString *CYSelectorPart::Replace(CYContext &context) {
153 std::ostringstream str;
c2c9f509 154 CYForEach (part, this) {
4de0686f 155 if (part->name_ != NULL)
69688ca1 156 str << part->name_->Word();
4de0686f
JF
157 if (part->value_)
158 str << ':';
159 }
2eb8215d 160 return $S(apr_pstrdup($pool, str.str().c_str()));
4de0686f
JF
161}
162
cacd1a88 163CYExpression *CYSendDirect::Replace(CYContext &context) {
4de0686f
JF
164 std::ostringstream name;
165 CYArgument **argument(&arguments_);
2385c806 166 CYSelectorPart *selector(NULL), *current(NULL);
4de0686f
JF
167
168 while (*argument != NULL) {
169 if ((*argument)->name_ != NULL) {
2385c806
JF
170 CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL));
171 if (selector == NULL)
172 selector = part;
173 if (current != NULL)
174 current->SetNext(part);
175 current = part;
4de0686f 176 (*argument)->name_ = NULL;
4de0686f
JF
177 }
178
179 if ((*argument)->value_ == NULL)
180 *argument = (*argument)->next_;
181 else
182 argument = &(*argument)->next_;
183 }
184
2385c806 185 return $C2($V("objc_msgSend"), self_, ($ CYSelector(selector))->Replace(context), arguments_);
4de0686f 186}
cacd1a88
JF
187
188CYExpression *CYSendSuper::Replace(CYContext &context) {
57a65431 189 return $ CYSendDirect($V("$cyr"), arguments_);
cacd1a88 190}