2 #include "ObjectiveC.hpp"
4 #include <objc/runtime.h>
7 void CYCategory::Output(CYOutput &out, CYFlags flags) const {
8 out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
9 out << "$cyp=object_getClass($cys);";
11 if (messages_ != NULL)
12 messages_->Output(out, true);
14 name_->ClassName(out, true);
19 void 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,";
25 name_->ClassName(out, false);
27 out << "$cyq(\"CY$\")";
29 out << "$cym=object_getClass($cyc);";
32 if (messages_ != NULL)
33 messages_->Output(out, false);
34 out << "objc_registerClassPair($cyc);";
35 out << "return $cyc;";
38 super_->Output(out, CYPA, CYNoFlags);
44 void CYClassExpression::Output(CYOutput &out, CYFlags flags) const {
45 CYClass::Output(out, flags);
48 void CYClassStatement::Output(CYOutput &out, CYFlags flags) const {
49 CYClass::Output(out, flags);
52 void CYField::Output(CYOutput &out) const {
56 void CYMessage::Output(CYOutput &out, bool replace) const {
58 next_->Output(out, replace);
59 out << "$cyn=new Selector(\"";
60 for (CYMessageParameter *parameter(parameters_); parameter != NULL; parameter = parameter->next_)
61 if (parameter->tag_ != NULL) {
62 out << *parameter->tag_;
63 if (parameter->name_ != NULL)
67 out << "$cyt=$cyn.type($cy" << (instance_ ? 's' : 'p') << ')' << ';';
68 out << (replace ? "class_replaceMethod" : "class_addMethod") << '(' << (instance_ ? "$cyc" : "$cym") << ',' << "$cyn" << ',';
69 out << "new Functor(function(self,_cmd";
70 for (CYMessageParameter *parameter(parameters_); parameter != NULL; parameter = parameter->next_)
71 if (parameter->name_ != NULL)
72 out << ',' << *parameter->name_;
73 out << "){return function(){";
74 if (statements_ != NULL)
75 statements_->Multiple(out);
76 out << "}.call(self);},$cyt),$cyt);";
79 void CYSelector::Output(CYOutput &out, CYFlags flags) const {
80 out << "@selector" << '(' << name_ << ')';
83 void CYSelectorPart::Output(CYOutput &out) const {
90 void CYSend::Output(CYOutput &out, CYFlags flags) const {
92 self_->Output(out, CYPA, CYNoFlags);
95 std::ostringstream name;
96 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
97 if (argument->name_ != NULL) {
98 name << *argument->name_;
99 if (argument->value_ != NULL)
103 out.out_ << reinterpret_cast<void *>(sel_registerName(name.str().c_str()));
104 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
105 if (argument->value_ != NULL) {
107 argument->value_->Output(out, CYPA, CYNoFlags);
112 CYStatement *CYCategory::Replace(CYContext &context) {
113 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
115 return $E($C1($F(NULL, $P5("$cys", "$cyp", "$cyc", "$cyn", "$cyt"), $$->*
116 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
117 $E($ CYAssign(cyc, cys))->*
118 messages_->Replace(context, true)
119 ), name_->ClassName(context, true)));
122 CYExpression *CYClass::Replace_(CYContext &context) {
123 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
125 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
127 return $C1($F(NULL, $P6("$cys", "$cyp", "$cyc", "$cyn", "$cyt", "$cym"), $$->*
128 $E($ CYAssign($V("$cyp"), $C1($V("object_getClass"), cys)))->*
129 $E($ CYAssign(cyc, $C3($V("objc_allocateClassPair"), cys, name, $D(0))))->*
130 $E($ CYAssign($V("$cym"), $C1($V("object_getClass"), cyc)))->*
131 fields_->Replace(context)->*
132 messages_->Replace(context, false)->*
133 $E($C1($V("objc_registerClassPair"), cyc))->*
135 ), super_ == NULL ? $ CYNull() : super_);
138 CYExpression *CYClassExpression::Replace(CYContext &context) {
139 return Replace_(context);
142 CYStatement *CYClassStatement::Replace(CYContext &context) {
143 return $E(Replace_(context));
146 CYStatement *CYField::Replace(CYContext &context) const {
150 CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
151 CYVariable *cyn($V("$cyn"));
152 CYVariable *cyt($V("$cyt"));
154 return $ CYBlock($$->*
155 next_->Replace(context, replace)->*
156 $E($ CYAssign(cyn, parameters_->Selector(context)))->*
157 $E($ CYAssign(cyt, $C1($M(cyn, $S("type")), $V(instance_ ? "$cys" : "$cyp"))))->*
158 $E($C4($V(replace ? "class_replaceMethod" : "class_addMethod"),
159 $V(instance_ ? "$cyc" : "$cym"),
161 $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->*
162 $ CYReturn($C1($M($F(NULL, NULL, statements_), $S("call")), $V("self")))
169 CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
170 CYFunctionParameter *next(next_->Parameters(context));
171 return name_ == NULL ? next : $ CYFunctionParameter(name_, next);
174 CYSelector *CYMessageParameter::Selector(CYContext &context) const {
175 return $ CYSelector(SelectorPart(context));
178 CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
179 CYSelectorPart *next(next_->SelectorPart(context));
180 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
183 CYExpression *CYSelector::Replace(CYContext &context) {
184 return $N1($V("Selector"), name_->Replace(context));
187 CYString *CYSelectorPart::Replace(CYContext &context) {
188 std::ostringstream str;
189 for (const CYSelectorPart *part(this); part != NULL; part = part->next_) {
190 if (part->name_ != NULL)
191 str << part->name_->Value();
195 return $S(apr_pstrdup(context.pool_, str.str().c_str()));
198 CYExpression *CYSend::Replace(CYContext &context) {
199 std::ostringstream name;
200 CYArgument **argument(&arguments_);
202 while (*argument != NULL) {
203 if ((*argument)->name_ != NULL) {
204 name << *(*argument)->name_;
205 (*argument)->name_ = NULL;
206 if ((*argument)->value_ != NULL)
210 if ((*argument)->value_ == NULL)
211 *argument = (*argument)->next_;
213 argument = &(*argument)->next_;
216 SEL sel(sel_registerName(name.str().c_str()));
217 double address(static_cast<double>(reinterpret_cast<uintptr_t>(sel)));
219 return $C2($V("objc_msgSend"), self_, $D(address), arguments_);