]> git.saurik.com Git - cycript.git/blob - ObjectiveC/Replace.mm
Implemented the symbol sorting optimization, for optimal gzip compression.
[cycript.git] / ObjectiveC / Replace.mm
1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /* }}} */
39
40 #include "Replace.hpp"
41 #include "Context.hpp"
42 #include "ObjectiveC/Syntax.hpp"
43
44 #include <Foundation/Foundation.h>
45 #include <sstream>
46
47 CYStatement *CYCategory::Replace(CYContext &context) {
48 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
49
50 return $E($C1($F(NULL, $P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$->*
51 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
52 $E($ CYAssign(cyc, cys))->*
53 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
54 messages_->Replace(context, true)
55 ), name_->ClassName(context, true)));
56 }
57
58 CYExpression *CYClass::Replace_(CYContext &context) {
59 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
60
61 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
62
63 return $C1($F(NULL, $P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$->*
64 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
65 $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->*
66 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
67 fields_->Replace(context)->*
68 messages_->Replace(context, false)->*
69 $E($C1($V("objc_registerClassPair"), cyc))->*
70 $ CYReturn(cyc)
71 ), super_ == NULL ? $ CYNull() : super_);
72 }
73
74 CYExpression *CYClassExpression::Replace(CYContext &context) {
75 return Replace_(context);
76 }
77
78 CYStatement *CYClassStatement::Replace(CYContext &context) {
79 return $E(Replace_(context));
80 }
81
82 CYStatement *CYField::Replace(CYContext &context) const {
83 return NULL;
84 }
85
86 CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
87 CYVariable *cyn($V("$cyn"));
88 CYVariable *cyt($V("$cyt"));
89 CYVariable *self($V("self"));
90 CYVariable *_class($V(instance_ ? "$cys" : "$cyp"));
91
92 return $ CYBlock($$->*
93 next_->Replace(context, replace)->*
94 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
95 $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), _class)))->*
96 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
97 $V(instance_ ? "$cyc" : "$cym"),
98 cyn,
99 $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->*
100 $ CYVar($L1($L($I("$cyr"), $N2($V("Super"), self, _class))))->*
101 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), self))
102 ), cyt),
103 cyt
104 ))
105 );
106 }
107
108 CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
109 CYFunctionParameter *next(next_->Parameters(context));
110 return name_ == NULL ? next : $ CYFunctionParameter(name_, next);
111 }
112
113 CYSelector *CYMessageParameter::Selector(CYContext &context) const {
114 return $ CYSelector(SelectorPart(context));
115 }
116
117 CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
118 CYSelectorPart *next(next_->SelectorPart(context));
119 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
120 }
121
122 CYExpression *CYSelector::Replace(CYContext &context) {
123 return $N1($V("Selector"), name_->Replace(context));
124 }
125
126 CYString *CYSelectorPart::Replace(CYContext &context) {
127 std::ostringstream str;
128 for (const CYSelectorPart *part(this); part != NULL; part = part->next_) {
129 if (part->name_ != NULL)
130 str << part->name_->Word();
131 if (part->value_)
132 str << ':';
133 }
134 return $S(apr_pstrdup(context.pool_, str.str().c_str()));
135 }
136
137 CYExpression *CYSendDirect::Replace(CYContext &context) {
138 std::ostringstream name;
139 CYArgument **argument(&arguments_);
140 CYSelectorPart *selector(NULL), *current(NULL);
141
142 while (*argument != NULL) {
143 if ((*argument)->name_ != NULL) {
144 CYSelectorPart *part($ CYSelectorPart((*argument)->name_, (*argument)->value_ != NULL));
145 if (selector == NULL)
146 selector = part;
147 if (current != NULL)
148 current->SetNext(part);
149 current = part;
150 (*argument)->name_ = NULL;
151 }
152
153 if ((*argument)->value_ == NULL)
154 *argument = (*argument)->next_;
155 else
156 argument = &(*argument)->next_;
157 }
158
159 return $C2($V("objc_msgSend"), self_, ($ CYSelector(selector))->Replace(context), arguments_);
160 }
161
162 CYExpression *CYSendSuper::Replace(CYContext &context) {
163 return $ CYSendDirect($V("$cyr"), arguments_);
164 }