]>
Commit | Line | Data |
---|---|---|
4de0686f JF |
1 | #include "Replace.hpp" |
2 | #include "ObjectiveC.hpp" | |
3 | ||
4 | #include <objc/runtime.h> | |
5 | #include <sstream> | |
6 | ||
7 | void 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 | ||
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,"; | |
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 | ||
44 | void CYClassExpression::Output(CYOutput &out, CYFlags flags) const { | |
45 | CYClass::Output(out, flags); | |
46 | } | |
47 | ||
48 | void CYClassStatement::Output(CYOutput &out, CYFlags flags) const { | |
49 | CYClass::Output(out, flags); | |
50 | } | |
51 | ||
52 | void CYField::Output(CYOutput &out) const { | |
53 | // XXX: implement! | |
54 | } | |
55 | ||
56 | void CYMessage::Output(CYOutput &out, bool replace) const { | |
57 | if (next_ != NULL) | |
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) | |
64 | out << ':'; | |
65 | } | |
66 | out << "\");"; | |
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);"; | |
77 | } | |
78 | ||
79 | void CYSelector::Output(CYOutput &out, CYFlags flags) const { | |
80 | out << "@selector" << '(' << name_ << ')'; | |
81 | } | |
82 | ||
83 | void CYSelectorPart::Output(CYOutput &out) const { | |
84 | out << name_; | |
85 | if (value_) | |
86 | out << ':'; | |
87 | out << next_; | |
88 | } | |
89 | ||
90 | void CYSend::Output(CYOutput &out, CYFlags flags) const { | |
91 | out << '['; | |
92 | self_->Output(out, CYPA, CYNoFlags); | |
93 | out << ']'; | |
94 | ||
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) | |
100 | name << ':'; | |
101 | } | |
102 | ||
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) { | |
106 | out << ','; | |
107 | argument->value_->Output(out, CYPA, CYNoFlags); | |
108 | } | |
109 | out << ')'; | |
110 | } | |
111 | ||
112 | CYStatement *CYCategory::Replace(CYContext &context) { | |
113 | CYVariable *cyc($V("$cyc")), *cys($V("$cys")); | |
114 | ||
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))); | |
120 | } | |
121 | ||
122 | CYExpression *CYClass::Replace_(CYContext &context) { | |
123 | CYVariable *cyc($V("$cyc")), *cys($V("$cys")); | |
124 | ||
125 | CYExpression *name(name_ != NULL ? name_->ClassName(context, false) : $C1($V("$cyq"), $S("CY$"))); | |
126 | ||
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))->* | |
134 | $ CYReturn(cyc) | |
135 | ), super_ == NULL ? $ CYNull() : super_); | |
136 | } | |
137 | ||
138 | CYExpression *CYClassExpression::Replace(CYContext &context) { | |
139 | return Replace_(context); | |
140 | } | |
141 | ||
142 | CYStatement *CYClassStatement::Replace(CYContext &context) { | |
143 | return $E(Replace_(context)); | |
144 | } | |
145 | ||
146 | CYStatement *CYField::Replace(CYContext &context) const { | |
147 | return NULL; | |
148 | } | |
149 | ||
150 | CYStatement *CYMessage::Replace(CYContext &context, bool replace) const { $T(NULL) | |
151 | CYVariable *cyn($V("$cyn")); | |
152 | CYVariable *cyt($V("$cyt")); | |
153 | ||
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"), | |
160 | cyn, | |
161 | $N2($V("Functor"), $F(NULL, $P2("self", "_cmd", parameters_->Parameters(context)), $$->* | |
162 | $ CYReturn($C1($M($F(NULL, NULL, statements_), $S("call")), $V("self"))) | |
163 | ), cyt), | |
164 | cyt | |
165 | )) | |
166 | ); | |
167 | } | |
168 | ||
169 | CYFunctionParameter *CYMessageParameter::Parameters(CYContext &context) const { $T(NULL) | |
170 | CYFunctionParameter *next(next_->Parameters(context)); | |
171 | return name_ == NULL ? next : $ CYFunctionParameter(name_, next); | |
172 | } | |
173 | ||
174 | CYSelector *CYMessageParameter::Selector(CYContext &context) const { | |
175 | return $ CYSelector(SelectorPart(context)); | |
176 | } | |
177 | ||
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); | |
181 | } | |
182 | ||
183 | CYExpression *CYSelector::Replace(CYContext &context) { | |
184 | return $N1($V("Selector"), name_->Replace(context)); | |
185 | } | |
186 | ||
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(); | |
192 | if (part->value_) | |
193 | str << ':'; | |
194 | } | |
195 | return $S(apr_pstrdup(context.pool_, str.str().c_str())); | |
196 | } | |
197 | ||
198 | CYExpression *CYSend::Replace(CYContext &context) { | |
199 | std::ostringstream name; | |
200 | CYArgument **argument(&arguments_); | |
201 | ||
202 | while (*argument != NULL) { | |
203 | if ((*argument)->name_ != NULL) { | |
204 | name << *(*argument)->name_; | |
205 | (*argument)->name_ = NULL; | |
206 | if ((*argument)->value_ != NULL) | |
207 | name << ':'; | |
208 | } | |
209 | ||
210 | if ((*argument)->value_ == NULL) | |
211 | *argument = (*argument)->next_; | |
212 | else | |
213 | argument = &(*argument)->next_; | |
214 | } | |
215 | ||
216 | SEL sel(sel_registerName(name.str().c_str())); | |
217 | double address(static_cast<double>(reinterpret_cast<uintptr_t>(sel))); | |
218 | ||
219 | return $C2($V("objc_msgSend"), self_, $D(address), arguments_); | |
220 | } |