]> git.saurik.com Git - cycript.git/blob - ObjectiveC/Output.cpp
Also use CXType walker to for function prototypes.
[cycript.git] / ObjectiveC / Output.cpp
1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <sstream>
23
24 #include "Replace.hpp"
25
26 #include "ObjectiveC/Syntax.hpp"
27
28 void CYCategory::Output(CYOutput &out, CYFlags flags) const {
29 out << "@implementation" << ' ' << *name_ << ' ' << '(' << ')' << '\n';
30 ++out.indent_;
31
32 CYForEach (message, messages_) {
33 message->Output(out);
34 out << '\n';
35 }
36
37 --out.indent_;
38 out << "@end";
39 }
40
41 void CYImplementation::Output(CYOutput &out, CYFlags flags) const {
42 out << "@implementation" << ' ' << *name_ << '\n';
43 ++out.indent_;
44
45 // XXX: implement
46
47 --out.indent_;
48 out << "@end";
49 }
50
51 void CYImplementationField::Output(CYOutput &out) const {
52 type_->Output(out, name_);
53 out.Terminate();
54 out << '\n';
55 }
56
57 void CYInstanceLiteral::Output(CYOutput &out, CYFlags flags) const {
58 out << '#';
59 number_->Output(out, CYRight(flags));
60 }
61
62 void CYMessage::Output(CYOutput &out) const {
63 out << (instance_ ? '-' : '+');
64
65 CYForEach (parameter, parameters_)
66 if (parameter->name_ != NULL) {
67 out << ' ' << *parameter->name_;
68 // XXX: this is off somehow
69 if (parameter->identifier_ != NULL) {
70 out << ':';
71 if (parameter->type_ != NULL)
72 out << '(' << *parameter->type_ << ')';
73 out << *parameter->identifier_;
74 }
75 }
76
77 out << code_;
78 }
79
80 void CYBox::Output(CYOutput &out, CYFlags flags) const {
81 out << '@';
82 value_->Output(out, Precedence(), CYRight(flags));
83 }
84
85 void CYObjCArray::Output(CYOutput &out, CYFlags flags) const {
86 out << '@' << '[' << elements_ << ']';
87 }
88
89 void CYObjCDictionary::Output(CYOutput &out, CYFlags flags) const {
90 unsigned count(0);
91 CYForEach (pair, pairs_)
92 ++count;
93 bool large(count > 8);
94
95 out << '@' << '{';
96 if (large) {
97 out << '\n';
98 ++out.indent_;
99 }
100
101 bool comma(false);
102 CYForEach (pair, pairs_) {
103 if (!comma)
104 comma = true;
105 else {
106 out << ',';
107 if (large)
108 out << '\n';
109 else
110 out << ' ';
111 }
112
113 if (large)
114 out << '\t';
115
116 pair->key_->Output(out, CYAssign::Precedence_, CYNoFlags);
117 out << ':' << ' ';
118 pair->value_->Output(out, CYAssign::Precedence_, CYNoFlags);
119 }
120
121 if (large && out.pretty_)
122 out << ',';
123
124 if (large) {
125 out << '\n';
126 --out.indent_;
127 }
128
129 out << '\t' << '}';
130 }
131
132 void CYObjCBlock::Output(CYOutput &out, CYFlags flags) const {
133 out << '^' << ' ' << *typed_ << ' ' << '(';
134
135 bool comma(false);
136 CYForEach (parameter, parameters_) {
137 if (comma)
138 out << ',' << ' ';
139 else
140 comma = true;
141 parameter->type_->Output(out, parameter->name_);
142 }
143
144 out << ')' << ' ' << '{' << '\n';
145 ++out.indent_;
146 out << code_;
147 --out.indent_;
148 out << '\t' << '}';
149 }
150
151 void CYProtocol::Output(CYOutput &out) const {
152 name_->Output(out, CYAssign::Precedence_, CYNoFlags);
153 if (next_ != NULL)
154 out << ',' << ' ' << *next_;
155 }
156
157 void CYSelector::Output(CYOutput &out, CYFlags flags) const {
158 out << "@selector" << '(' << parts_ << ')';
159 }
160
161 void CYSelectorPart::Output(CYOutput &out) const {
162 out << name_;
163 if (value_)
164 out << ':';
165 out << next_;
166 }
167
168 void CYSend::Output(CYOutput &out, CYFlags flags) const {
169 CYForEach (argument, arguments_)
170 if (argument->name_ != NULL) {
171 out << ' ' << *argument->name_;
172 if (argument->value_ != NULL)
173 out << ':' << *argument->value_;
174 }
175 }
176
177 void CYSendDirect::Output(CYOutput &out, CYFlags flags) const {
178 out << '[';
179 self_->Output(out, CYAssign::Precedence_, CYNoFlags);
180 CYSend::Output(out, flags);
181 out << ']';
182 }
183
184 void CYSendSuper::Output(CYOutput &out, CYFlags flags) const {
185 out << '[' << "super";
186 CYSend::Output(out, flags);
187 out << ']';
188 }