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 {
55 void CYMessage::Output(CYOutput &out, bool replace) const {
56 out << (instance_ ? '-' : '+');
58 for (CYMessageParameter *parameter(parameters_); parameter != NULL; parameter = parameter->next_)
59 if (parameter->tag_ != NULL) {
60 out << ' ' << *parameter->tag_;
61 if (parameter->name_ != NULL)
62 out << ':' << *parameter->name_;
68 void CYSelector::Output(CYOutput &out, CYFlags flags) const {
69 out << "@selector" << '(' << name_ << ')';
72 void CYSelectorPart::Output(CYOutput &out) const {
79 void CYSend::Output(CYOutput &out, CYFlags flags) const {
82 self_->Output(out, CYPA, CYNoFlags);
84 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
85 if (argument->name_ != NULL) {
86 out << ' ' << *argument->name_;
87 if (argument->value_ != NULL)
88 out << ':' << *argument->value_;
94 CYStatement *CYCategory::Replace(CYContext &context) {
95 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
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)));
104 CYExpression *CYClass::Replace_(CYContext &context) {
105 CYVariable *cyc($V("$cyc")), *cys($V("$cys"));
107 CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$")));
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))->*
117 ), super_ == NULL ? $ CYNull() : super_);
120 CYExpression *CYClassExpression::Replace(CYContext &context) {
121 return Replace_(context);
124 CYStatement *CYClassStatement::Replace(CYContext &context) {
125 return $E(Replace_(context));
128 CYStatement *CYField::Replace(CYContext &context) const {
132 CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL)
133 CYVariable *cyn($V("$cyn"));
134 CYVariable *cyt($V("$cyt"));
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"),
143 $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->*
144 $ CYReturn($C1($M($F(NULL, NULL, code_), $S("call")), $V("self")))
151 CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL)
152 CYFunctionParameter *next(next_->Parameters(context));
153 return name_ == NULL ? next : $ CYFunctionParameter(name_, next);
156 CYSelector *CYMessageParameter::Selector(CYContext &context) const {
157 return $ CYSelector(SelectorPart(context));
160 CYSelectorPart *CYMessageParameter::SelectorPart(CYContext &context) const { $T(NULL)
161 CYSelectorPart *next(next_->SelectorPart(context));
162 return tag_ == NULL ? next : $ CYSelectorPart(tag_, name_ != NULL, next);
165 CYExpression *CYSelector::Replace(CYContext &context) {
166 return $N1($V("Selector"), name_->Replace(context));
169 CYString *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();
177 return $S(apr_pstrdup(context.pool_, str.str().c_str()));
180 CYExpression *CYSend::Replace(CYContext &context) {
181 std::ostringstream name;
182 CYArgument **argument(&arguments_);
184 while (*argument != NULL) {
185 if ((*argument)->name_ != NULL) {
186 name << *(*argument)->name_;
187 (*argument)->name_ = NULL;
188 if ((*argument)->value_ != NULL)
192 if ((*argument)->value_ == NULL)
193 *argument = (*argument)->next_;
195 argument = &(*argument)->next_;
198 SEL sel(sel_registerName(name.str().c_str()));
199 double address(static_cast<double>(reinterpret_cast<uintptr_t>(sel)));
201 return $C2($V("objc_msgSend"), self_, $D(address), arguments_);