]> git.saurik.com Git - cycript.git/blob - ObjectiveC/Output.mm
Factor common code out of FFI closure adapters.
[cycript.git] / ObjectiveC / Output.mm
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "Replace.hpp"
23 #include "ObjectiveC/Syntax.hpp"
24
25 #include <Foundation/Foundation.h>
26 #include <sstream>
27
28 void CYCategory::Output(CYOutput &out, CYFlags flags) const {
29 out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
30 out << "$cyp=object_getClass($cys);";
31 out << "$cyc=$cys;";
32 if (messages_ != NULL)
33 messages_->Output(out, true);
34 out << "})(";
35 name_->ClassName(out, true);
36 out << ')';
37 out << ';';
38 }
39
40 void CYClass::Output(CYOutput &out, CYFlags flags) const {
41 // XXX: I don't necc. need the ()s
42 out << "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
43 out << "$cyp=object_getClass($cys);";
44 out << "$cyc=objc_allocateClassPair($cys,";
45 if (name_ != NULL)
46 name_->ClassName(out, false);
47 else
48 out << "$cyq(\"CY$\")";
49 out << ",0);";
50 out << "$cym=object_getClass($cyc);";
51 if (fields_ != NULL)
52 fields_->Output(out);
53 if (messages_ != NULL)
54 messages_->Output(out, false);
55 if (protocols_ != NULL) {
56 out << '<';
57 out << *protocols_;
58 out << '>';
59 }
60 out << "objc_registerClassPair($cyc);";
61 out << "return $cyc;";
62 out << "}(";
63 if (super_ != NULL)
64 super_->Output(out, CYAssign::Precedence_, CYNoFlags);
65 else
66 out << "null";
67 out << "))";
68 }
69
70 void CYClassExpression::Output(CYOutput &out, CYFlags flags) const {
71 CYClass::Output(out, flags);
72 }
73
74 void CYClassStatement::Output(CYOutput &out, CYFlags flags) const {
75 CYClass::Output(out, flags);
76 }
77
78 void CYEncodedType::Output(CYOutput &out, CYFlags flags) const {
79 out << "@encode(";
80 // XXX: this is seriously wrong
81 out << ")";
82 }
83
84 void CYField::Output(CYOutput &out) const {
85 }
86
87 void CYImport::Output(CYOutput &out, CYFlags flags) const {
88 out << "@import";
89 }
90
91 void CYMessage::Output(CYOutput &out, bool replace) const {
92 out << (instance_ ? '-' : '+');
93
94 CYForEach (parameter, parameters_)
95 if (parameter->tag_ != NULL) {
96 out << ' ' << *parameter->tag_;
97 if (parameter->name_ != NULL)
98 out << ':' << *parameter->name_;
99 }
100
101 out << code_;
102 }
103
104 void CYBox::Output(CYOutput &out, CYFlags flags) const {
105 out << '@';
106 value_->Output(out, Precedence(), CYRight(flags));
107 }
108
109 void CYObjCBlock::Output(CYOutput &out, CYFlags flags) const {
110 // XXX: this is seriously wrong
111 out << "^(";
112 out << ")";
113 out << "{";
114 out << "}";
115 }
116
117 void CYProtocol::Output(CYOutput &out) const {
118 name_->Output(out, CYAssign::Precedence_, CYNoFlags);
119 if (next_ != NULL)
120 out << ',' << ' ' << *next_;
121 }
122
123 void CYSelector::Output(CYOutput &out, CYFlags flags) const {
124 out << "@selector" << '(' << name_ << ')';
125 }
126
127 void CYSelectorPart::Output(CYOutput &out) const {
128 out << name_;
129 if (value_)
130 out << ':';
131 out << next_;
132 }
133
134 void CYSend::Output(CYOutput &out, CYFlags flags) const {
135 CYForEach (argument, arguments_)
136 if (argument->name_ != NULL) {
137 out << ' ' << *argument->name_;
138 if (argument->value_ != NULL)
139 out << ':' << *argument->value_;
140 }
141 }
142
143 void CYSendDirect::Output(CYOutput &out, CYFlags flags) const {
144 out << '[';
145 self_->Output(out, CYAssign::Precedence_, CYNoFlags);
146 CYSend::Output(out, flags);
147 out << ']';
148 }
149
150 void CYSendSuper::Output(CYOutput &out, CYFlags flags) const {
151 out << '[' << "super";
152 CYSend::Output(out, flags);
153 out << ']';
154 }