]> git.saurik.com Git - cycript.git/blame - ObjectiveC.mm
Fixed the ::Class bug and attempted to further abstract GNUstep.
[cycript.git] / ObjectiveC.mm
CommitLineData
4de0686f
JF
1#include "Replace.hpp"
2#include "ObjectiveC.hpp"
3
4#include <objc/runtime.h>
5#include <sstream>
6
7void CYCategory::Output(CYOutput &out, CYFlags flags) const {
8 out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
9 out << "$cyp=object_getClass($cys);";
10 out << "$cyc=$cys;";
11 if (messages_ != NULL)
12 messages_->Output(out, true);
13 out << "})(";
14 name_->ClassName(out, true);
15 out << ')';
16 out << ';';
17}
18
19void CYClass::Output(CYOutput &out, CYFlags flags) const {
20 // XXX: I don't necc. need the ()s
21 out << "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
22 out << "$cyp=object_getClass($cys);";
23 out << "$cyc=objc_allocateClassPair($cys,";
24 if (name_ != NULL)
25 name_->ClassName(out, false);
26 else
27 out << "$cyq(\"CY$\")";
28 out << ",0);";
29 out << "$cym=object_getClass($cyc);";
30 if (fields_ != NULL)
31 fields_->Output(out);
32 if (messages_ != NULL)
33 messages_->Output(out, false);
34 out << "objc_registerClassPair($cyc);";
35 out << "return $cyc;";
36 out << "}(";
37 if (super_ != NULL)
38 super_->Output(out, CYPA, CYNoFlags);
39 else
40 out << "null";
41 out << "))";
42}
43
44void CYClassExpression::Output(CYOutput &out, CYFlags flags) const {
45 CYClass::Output(out, flags);
46}
47
48void CYClassStatement::Output(CYOutput &out, CYFlags flags) const {
49 CYClass::Output(out, flags);
50}
51
52void CYField::Output(CYOutput &out) const {
4de0686f
JF
53}
54
55void CYMessage::Output(CYOutput &out, bool replace) const {
4644480a
JF
56 out << (instance_ ? '-' : '+');
57
4de0686f
JF
58 for (CYMessageParameter *parameter(parameters_); parameter != NULL; parameter = parameter->next_)
59 if (parameter->tag_ != NULL) {
4644480a 60 out << ' ' << *parameter->tag_;
4de0686f 61 if (parameter->name_ != NULL)
4644480a 62 out << ':' << *parameter->name_;
4de0686f 63 }
4644480a
JF
64
65 out << code_;
4de0686f
JF
66}
67
68void CYSelector::Output(CYOutput &out, CYFlags flags) const {
69 out << "@selector" << '(' << name_ << ')';
70}
71
72void CYSelectorPart::Output(CYOutput &out) const {
73 out << name_;
74 if (value_)
75 out << ':';
76 out << next_;
77}
78
79void CYSend::Output(CYOutput &out, CYFlags flags) const {
80 out << '[';
4644480a 81
4de0686f 82 self_->Output(out, CYPA, CYNoFlags);
4de0686f 83
4de0686f
JF
84 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
85 if (argument->name_ != NULL) {
4644480a 86 out << ' ' << *argument->name_;
4de0686f 87 if (argument->value_ != NULL)
4644480a 88 out << ':' << *argument->value_;
4de0686f
JF
89 }
90
4644480a 91 out << ']';
4de0686f
JF
92}
93
94CYStatement *CYCategory::Replace(CYContext &context) {
95 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
96
97 return $E($C1($F(NULL, $P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$->*
98 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
99 $E($ CYAssign(cyc, cys))->*
100 messages_->Replace(context, true)
101 ), name_->ClassName(context, true)));
102}
103
104CYExpression *CYClass::Replace_(CYContext &context) {
105 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
106
107 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
108
109 return $C1($F(NULL, $P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$->*
110 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
111 $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->*
112 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
113 fields_->Replace(context)->*
114 messages_->Replace(context, false)->*
115 $E($C1($V("objc_registerClassPair"), cyc))->*
116 $ CYReturn(cyc)
117 ), super_ == NULL ? $ CYNull() : super_);
118}
119
120CYExpression *CYClassExpression::Replace(CYContext &context) {
121 return Replace_(context);
122}
123
124CYStatement *CYClassStatement::Replace(CYContext &context) {
125 return $E(Replace_(context));
126}
127
128CYStatement *CYField::Replace(CYContext &context) const {
129 return NULL;
130}
131
132CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
133 CYVariable *cyn($V("$cyn"));
134 CYVariable *cyt($V("$cyt"));
135
136 return $ CYBlock($$->*
137 next_->Replace(context, replace)->*
138 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
139 $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), $V(instance_ ? "$cys" : "$cyp"))))->*
140 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
141 $V(instance_ ? "$cyc" : "$cym"),
142 cyn,
143 $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->*
4644480a 144 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), $V("self")))
4de0686f
JF
145 ), cyt),
146 cyt
147 ))
148 );
149}
150
151CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
152 CYFunctionParameter *next(next_->Parameters(context));
153 return name_ == NULL ? next : $ CYFunctionParameter(name_, next);
154}
155
156CYSelector *CYMessageParameter::Selector(CYContext &context) const {
157 return $ CYSelector(SelectorPart(context));
158}
159
160CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
161 CYSelectorPart *next(next_->SelectorPart(context));
162 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
163}
164
165CYExpression *CYSelector::Replace(CYContext &context) {
166 return $N1($V("Selector"), name_->Replace(context));
167}
168
169CYString *CYSelectorPart::Replace(CYContext &context) {
170 std::ostringstream str;
171 for (const CYSelectorPart *part(this); part != NULL; part = part->next_) {
172 if (part->name_ != NULL)
173 str << part->name_->Value();
174 if (part->value_)
175 str << ':';
176 }
177 return $S(apr_pstrdup(context.pool_, str.str().c_str()));
178}
179
180CYExpression *CYSend::Replace(CYContext &context) {
181 std::ostringstream name;
182 CYArgument **argument(&arguments_);
183
184 while (*argument != NULL) {
185 if ((*argument)->name_ != NULL) {
186 name << *(*argument)->name_;
187 (*argument)->name_ = NULL;
188 if ((*argument)->value_ != NULL)
189 name << ':';
190 }
191
192 if ((*argument)->value_ == NULL)
193 *argument = (*argument)->next_;
194 else
195 argument = &(*argument)->next_;
196 }
197
198 SEL sel(sel_registerName(name.str().c_str()));
199 double address(static_cast<double>(reinterpret_cast<uintptr_t>(sel)));
200
201 return $C2($V("objc_msgSend"), self_, $D(address), arguments_);
202}