]> git.saurik.com Git - cycript.git/blame - Output.cpp
Implemented ivar access, added some general runtime reflection hierarchy, changed...
[cycript.git] / Output.cpp
CommitLineData
5999c315
JF
1#include "Parser.hpp"
2
3#include <iostream>
4#include <iomanip>
5
4afefdd9
JF
6#include <objc/runtime.h>
7#include <sstream>
8
b09da87b
JF
9_finline CYFlags operator ~(CYFlags rhs) {
10 return static_cast<CYFlags>(~static_cast<unsigned>(rhs));
11}
12
13_finline CYFlags operator &(CYFlags lhs, CYFlags rhs) {
14 return static_cast<CYFlags>(static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs));
15}
16
17_finline CYFlags operator |(CYFlags lhs, CYFlags rhs) {
18 return static_cast<CYFlags>(static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs));
19}
20
21_finline CYFlags &operator |=(CYFlags &lhs, CYFlags rhs) {
22 return lhs = lhs | rhs;
23}
24
25_finline CYFlags CYLeft(CYFlags flags) {
26 return flags & ~CYNoTrailer;
27}
28
29_finline CYFlags CYCenter(CYFlags flags) {
30 return flags & CYNoIn;
31}
32
33_finline CYFlags CYRight(CYFlags flags) {
34 return flags & (CYNoIn | CYNoTrailer);
35}
36
37bool CYFalse::Value() const {
38 return false;
39}
40
41bool CYTrue::Value() const {
42 return true;
43}
44
d35a3b07
JF
45#define CYPA 16
46
b09da87b
JF
47void CYAddressOf::Output(std::ostream &out, CYFlags flags) const {
48 rhs_->Output(out, 1, CYLeft(flags));
283e7e33 49 out << ".addressOf()";
5999c315
JF
50}
51
d35a3b07
JF
52void CYArgument::Output(std::ostream &out) const {
53 if (name_ != NULL) {
5999c315
JF
54 out << *name_;
55 if (value_ != NULL)
56 out << ":";
57 }
d35a3b07 58 if (value_ != NULL)
b09da87b 59 value_->Output(out, CYPA, CYNoFlags);
5999c315 60 if (next_ != NULL) {
d35a3b07
JF
61 if (next_->name_ == NULL)
62 out << ',';
63 else
64 out << ' ';
65 next_->Output(out);
5999c315
JF
66 }
67}
68
b09da87b 69void CYArray::Output(std::ostream &out, CYFlags flags) const {
5befe15e
JF
70 out << '[';
71 if (elements_ != NULL)
72 elements_->Output(out);
73 out << ']';
74}
75
b09da87b
JF
76void CYAssignment::Output(std::ostream &out, CYFlags flags) const {
77 lhs_->Output(out, Precedence() - 1, CYLeft(flags));
d35a3b07 78 out << Operator();
b09da87b 79 rhs_->Output(out, Precedence(), CYRight(flags));
d35a3b07
JF
80}
81
b09da87b
JF
82void CYBoolean::Output(std::ostream &out, CYFlags flags) const {
83 if ((flags & CYNoLeader) != 0)
84 out << ' ';
5999c315 85 out << (Value() ? "true" : "false");
b09da87b
JF
86 if ((flags & CYNoTrailer) != 0)
87 out << ' ';
5999c315
JF
88}
89
90void CYBreak::Output(std::ostream &out) const {
91 out << "break";
92 if (label_ != NULL)
93 out << ' ' << *label_;
94 out << ';';
95}
96
b09da87b
JF
97void CYCall::Output(std::ostream &out, CYFlags flags) const {
98 function_->Output(out, Precedence(), CYLeft(flags));
d35a3b07 99 out << '(';
5999c315 100 if (arguments_ != NULL)
d35a3b07 101 arguments_->Output(out);
5999c315
JF
102 out << ')';
103}
104
105void CYCatch::Output(std::ostream &out) const {
106 out << "catch(" << *name_ << ')';
107 code_->Output(out, true);
108}
109
e5bc40db
JF
110void CYCategory::Output(std::ostream &out) const {
111 out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
112 out << "$cyp=object_getClass($cys);";
113 out << "$cyc=$cys;";
114 if (messages_ != NULL)
115 messages_->Output(out, true);
116 out << "})(";
117 name_->ClassName(out);
118 out << ");";
119}
120
b09da87b 121void CYClass::Output(std::ostream &out) const {
e5bc40db
JF
122 out << "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
123 out << "$cyp=object_getClass($cys);";
b09da87b
JF
124 out << "$cyc=objc_allocateClassPair($cys,\"" << *name_ << "\",0);";
125 out << "$cym=object_getClass($cyc);";
126 if (fields_ != NULL)
127 fields_->Output(out);
128 if (messages_ != NULL)
e5bc40db 129 messages_->Output(out, false);
b09da87b
JF
130 out << "objc_registerClassPair($cyc);";
131 out << "})(";
132 if (super_ != NULL)
133 super_->Output(out, CYPA, CYNoFlags);
134 else
135 out << "null";
136 out << ");";
137}
138
e5bc40db
JF
139void CYCompound::Output(std::ostream &out, CYFlags flags) const {
140 if (CYExpression *expression = expressions_)
141 if (CYExpression *next = expression->next_) {
142 expression->Output(out, CYLeft(flags));
143 CYFlags center(CYCenter(flags));
144 while (next != NULL) {
145 expression = next;
146 out << ',';
147 next = expression->next_;
148 CYFlags right(next != NULL ? center : CYRight(flags));
149 expression->Output(out, right);
150 }
151 } else
152 expression->Output(out, flags);
153}
154
b09da87b
JF
155void CYCondition::Output(std::ostream &out, CYFlags flags) const {
156 test_->Output(out, Precedence() - 1, CYLeft(flags));
d35a3b07 157 out << '?';
5999c315 158 if (true_ != NULL)
b09da87b 159 true_->Output(out, CYPA, CYNoFlags);
d35a3b07 160 out << ':';
b09da87b 161 false_->Output(out, CYPA, CYRight(flags));
5999c315
JF
162}
163
164void CYContinue::Output(std::ostream &out) const {
165 out << "continue";
166 if (label_ != NULL)
167 out << ' ' << *label_;
168 out << ';';
169}
170
171void CYClause::Output(std::ostream &out) const {
d35a3b07
JF
172 if (case_ != NULL) {
173 out << "case";
b09da87b 174 case_->Output(out, CYNoFlags);
d35a3b07 175 } else
5999c315
JF
176 out << "default";
177 out << ':';
178 if (code_ != NULL)
179 code_->Output(out, false);
180 out << *next_;
181}
182
b09da87b 183// XXX: deal with NoIn
5999c315
JF
184void CYDeclaration::Part(std::ostream &out) const {
185 out << "var ";
186 Output(out);
187}
188
189void CYDeclaration::Output(std::ostream &out) const {
190 out << *identifier_;
d35a3b07
JF
191 if (initialiser_ != NULL) {
192 out << '=';
b09da87b 193 initialiser_->Output(out, CYPA, CYNoFlags);
d35a3b07 194 }
5999c315
JF
195}
196
b09da87b 197// XXX: deal with NoIn
5999c315
JF
198void CYDeclarations::Part(std::ostream &out) const {
199 out << "var ";
d35a3b07 200
5999c315 201 const CYDeclarations *declaration(this);
d35a3b07
JF
202 output:
203 out << *declaration->declaration_;
204 declaration = declaration->next_;
205
206 if (declaration != NULL) {
207 out << ',';
208 goto output;
209 }
5999c315
JF
210}
211
212void CYDeclarations::Output(std::ostream &out) const {
213 Part(out);
214 out << ';';
215}
216
217void CYDoWhile::Output(std::ostream &out) const {
b09da87b 218 // XXX: extra space character!
5999c315
JF
219 out << "do ";
220 code_->Output(out, false);
d35a3b07 221 out << "while(";
b09da87b
JF
222 test_->Output(out, CYNoFlags);
223 out << ')';
5999c315
JF
224}
225
5befe15e 226void CYElement::Output(std::ostream &out) const {
5999c315 227 if (value_ != NULL)
b09da87b 228 value_->Output(out, CYPA, CYNoFlags);
5befe15e 229 if (next_ != NULL || value_ == NULL)
5999c315 230 out << ',';
5befe15e
JF
231 if (next_ != NULL)
232 next_->Output(out);
5999c315
JF
233}
234
235void CYEmpty::Output(std::ostream &out) const {
236 out << ';';
237}
238
239void CYEmpty::Output(std::ostream &out, bool block) const {
240 if (next_ != NULL)
241 CYSource::Output(out, block);
242 else
243 out << "{}";
244}
245
246void CYExpress::Output(std::ostream &out) const {
b09da87b 247 expression_->Output(out, CYNoFunction | CYNoBrace);
5999c315
JF
248 out << ';';
249}
250
e5bc40db
JF
251void CYExpression::ClassName(std::ostream &out) const {
252 Output(out, CYPA, CYNoFlags);
253}
254
5999c315 255void CYExpression::Part(std::ostream &out) const {
d35a3b07 256 // XXX: this should handle LeftHandSideExpression
b09da87b 257 Output(out, CYNoIn);
d35a3b07
JF
258}
259
b09da87b
JF
260void CYExpression::Output(std::ostream &out, unsigned precedence, CYFlags flags) const {
261 if (precedence < Precedence()) {
5999c315 262 out << '(';
b09da87b 263 Output(out, CYNoFlags);
5999c315 264 out << ')';
b09da87b
JF
265 } else
266 Output(out, flags);
267}
268
269void CYField::Output(std::ostream &out) const {
270 // XXX: implement!
5999c315
JF
271}
272
273void CYFor::Output(std::ostream &out) const {
274 out << "for(";
275 if (initialiser_ != NULL)
276 initialiser_->Part(out);
277 out << ';';
278 if (test_ != NULL)
b09da87b 279 test_->Output(out, CYNoFlags);
5999c315
JF
280 out << ';';
281 if (increment_ != NULL)
b09da87b 282 increment_->Output(out, CYNoFlags);
5999c315
JF
283 out << ')';
284 code_->Output(out, false);
285}
286
287void CYForIn::Output(std::ostream &out) const {
288 out << "for(";
289 initialiser_->Part(out);
b09da87b
JF
290 // XXX: deal with this space character!
291 out << ' ';
292 out << "in";
293 set_->Output(out, CYNoLeader);
5999c315
JF
294 out << ')';
295 code_->Output(out, false);
296}
297
298void CYFunction::Output(std::ostream &out) const {
b09da87b
JF
299 CYLambda::Output(out, CYNoFlags);
300}
301
302void CYFunctionParameter::Output(std::ostream &out) const {
303 out << *name_;
304 if (next_ != NULL) {
305 out << ',';
306 out << *next_;
307 }
5999c315
JF
308}
309
310void CYIf::Output(std::ostream &out) const {
d35a3b07 311 out << "if(";
b09da87b 312 test_->Output(out, CYNoFlags);
d35a3b07 313 out << ')';
5999c315
JF
314 true_->Output(out, true);
315 if (false_ != NULL) {
316 out << "else ";
317 false_->Output(out, false);
318 }
319}
320
b09da87b
JF
321void CYIndirect::Output(std::ostream &out, CYFlags flags) const {
322 rhs_->Output(out, 1, CYLeft(flags));
d35a3b07 323 out << "[0]";
5999c315
JF
324}
325
b09da87b
JF
326void CYInfix::Output(std::ostream &out, CYFlags flags) const {
327 const char *name(Operator());
328 bool protect((flags & CYNoIn) != 0 && strcmp(name, "in"));
329 if (protect)
330 out << '(';
331 bool alphabetic(Alphabetic());
332 CYFlags left(protect ? CYNoFlags : CYLeft(flags));
333 if (alphabetic)
334 left |= CYNoTrailer;
335 lhs_->Output(out, Precedence(), left);
336 out << name;
337 CYFlags right(protect ? CYNoFlags : CYRight(flags));
338 if (alphabetic)
339 right |= CYNoLeader;
340 rhs_->Output(out, Precedence() - 1, right);
341 if (protect)
342 out << ')';
5999c315
JF
343}
344
b09da87b
JF
345void CYLambda::Output(std::ostream &out, CYFlags flags) const {
346 bool protect((flags & CYNoFunction) != 0);
347 if (protect)
348 out << '(';
5999c315
JF
349 out << "function";
350 if (name_ != NULL)
351 out << ' ' << *name_;
352 out << '(';
353 if (parameters_ != NULL)
354 out << *parameters_;
b09da87b
JF
355 out << "){";
356 if (body_ != NULL)
357 body_->Show(out);
358 out << '}';
359 if (protect)
360 out << ')';
5999c315
JF
361}
362
b09da87b
JF
363void CYMember::Output(std::ostream &out, CYFlags flags) const {
364 object_->Output(out, Precedence(), CYLeft(flags));
dea834b0
JF
365 if (const char *word = property_->Word())
366 out << '.' << word;
367 else {
368 out << '[';
b09da87b 369 property_->Output(out, CYNoFlags);
dea834b0
JF
370 out << ']';
371 }
5999c315
JF
372}
373
e5bc40db 374void CYMessage::Output(std::ostream &out, bool replace) const {
4e8c99fb 375 if (next_ != NULL)
e5bc40db 376 next_->Output(out, replace);
b09da87b
JF
377 out << "$cyn=new Selector(\"";
378 for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
379 if (parameter->tag_ != NULL) {
380 out << *parameter->tag_;
381 if (parameter->name_ != NULL)
5999c315
JF
382 out << ':';
383 }
b09da87b 384 out << "\");";
e5bc40db
JF
385 out << "$cyt=$cyn.type($cy" << (instance_ ? 's' : 'p') << ");";
386 out << "class_" << (replace ? "replace" : "add") << "Method($cy" << (instance_ ? 'c' : 'm') << ",$cyn,";
478d4ed0 387 out << "new Functor(function(self,_cmd";
b09da87b 388 for (CYMessageParameter *parameter(parameter_); parameter != NULL; parameter = parameter->next_)
478d4ed0
JF
389 if (parameter->name_ != NULL)
390 out << ',' << *parameter->name_;
391 out << "){return function(){";
b09da87b
JF
392 if (body_ != NULL)
393 body_->Show(out);
478d4ed0 394 out << "}.call(self);},$cyt),$cyt);";
5999c315
JF
395}
396
b09da87b 397void CYNew::Output(std::ostream &out, CYFlags flags) const {
bce8339b
JF
398 if ((flags & CYNoLeader) != 0)
399 out << ' ';
d35a3b07 400 out << "new";
b09da87b 401 constructor_->Output(out, Precedence(), CYCenter(flags) | CYNoLeader);
d35a3b07 402 out << '(';
5999c315 403 if (arguments_ != NULL)
d35a3b07 404 arguments_->Output(out);
5999c315
JF
405 out << ')';
406}
407
b09da87b
JF
408void CYNull::Output(std::ostream &out, CYFlags flags) const {
409 if ((flags & CYNoLeader) != 0)
410 out << ' ';
5999c315 411 CYWord::Output(out);
b09da87b
JF
412 if ((flags & CYNoTrailer) != 0)
413 out << ' ';
5999c315
JF
414}
415
b09da87b
JF
416void CYNumber::Output(std::ostream &out, CYFlags flags) const {
417 if ((flags & CYNoLeader) != 0)
418 out << ' ';
18401062
JF
419 // XXX: decide on correct precision
420 out << std::setprecision(9) << Value();
b09da87b
JF
421 if ((flags & CYNoTrailer) != 0)
422 out << ' ';
5999c315
JF
423}
424
e5bc40db
JF
425void CYNumber::PropertyName(std::ostream &out) const {
426 Output(out);
427}
428
b09da87b
JF
429void CYObject::Output(std::ostream &out, CYFlags flags) const {
430 bool protect((flags & CYNoBrace) != 0);
431 if (protect)
432 out << '(';
693d501b
JF
433 out << '{';
434 if (property_ != NULL)
435 property_->Output(out);
436 out << '}';
b09da87b
JF
437 if (protect)
438 out << ')';
693d501b
JF
439}
440
b09da87b
JF
441void CYPostfix::Output(std::ostream &out, CYFlags flags) const {
442 lhs_->Output(out, Precedence(), CYLeft(flags));
d35a3b07 443 out << Operator();
5999c315
JF
444}
445
b09da87b
JF
446void CYPrefix::Output(std::ostream &out, CYFlags flags) const {
447 bool alphabetic(Alphabetic());
d35a3b07 448 out << Operator();
b09da87b
JF
449 CYFlags right(CYRight(flags));
450 if (alphabetic)
451 right |= CYNoLeader;
452 rhs_->Output(out, Precedence(), right);
5999c315
JF
453}
454
693d501b 455void CYProperty::Output(std::ostream &out) const {
e5bc40db 456 name_->PropertyName(out);
579ed526 457 out << ':';
b09da87b 458 value_->Output(out, CYPA, CYNoFlags);
5999c315
JF
459 if (next_ != NULL) {
460 out << ',';
693d501b 461 next_->Output(out);
5999c315 462 }
5999c315
JF
463}
464
465void CYReturn::Output(std::ostream &out) const {
466 out << "return";
b09da87b
JF
467 if (value_ != NULL)
468 value_->Output(out, CYNoLeader);
5999c315
JF
469 out << ';';
470}
471
b09da87b 472void CYSelector::Output(std::ostream &out, CYFlags flags) const {
bce8339b
JF
473 if ((flags & CYNoLeader) != 0)
474 out << ' ';
b09da87b 475 out << "new Selector(\"";
62014ea9
JF
476 if (name_ != NULL)
477 name_->Output(out);
dea834b0 478 out << "\")";
e7ed5354
JF
479}
480
62014ea9
JF
481void CYSelectorPart::Output(std::ostream &out) const {
482 if (name_ != NULL)
483 out << *name_;
484 if (value_)
485 out << ':';
486 if (next_ != NULL)
487 next_->Output(out);
488}
489
b09da87b 490void CYSend::Output(std::ostream &out, CYFlags flags) const {
c239b9f8
JF
491 if ((flags & CYNoLeader) != 0)
492 out << ' ';
b09da87b
JF
493 out << "objc_msgSend(";
494 self_->Output(out, CYPA, CYNoFlags);
4afefdd9
JF
495 out << ",";
496 std::ostringstream name;
b09da87b
JF
497 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
498 if (argument->name_ != NULL) {
4afefdd9 499 name << *argument->name_;
b09da87b 500 if (argument->value_ != NULL)
4afefdd9 501 name << ':';
b09da87b 502 }
4afefdd9 503 out << reinterpret_cast<void *>(sel_registerName(name.str().c_str()));
b09da87b
JF
504 for (CYArgument *argument(arguments_); argument != NULL; argument = argument->next_)
505 if (argument->value_ != NULL) {
506 out << ",";
507 argument->value_->Output(out, CYPA, CYNoFlags);
508 }
509 out << ')';
510}
511
b1ff2d78 512void CYSource::Show(std::ostream &out) const {
5999c315 513 for (const CYSource *next(this); next != NULL; next = next->next_)
b09da87b 514 next->Output(out);
5999c315
JF
515}
516
517void CYSource::Output(std::ostream &out, bool block) const {
518 if (!block && next_ == NULL)
519 Output(out);
520 else {
521 out << '{';
b1ff2d78 522 Show(out);
5999c315
JF
523 out << '}';
524 }
525}
526
b09da87b 527void CYString::Output(std::ostream &out, CYFlags flags) const {
b4aa79af
JF
528 unsigned quot(0), apos(0);
529 for (const char *value(value_), *end(value_ + size_); value != end; ++value)
530 if (*value == '"')
531 ++quot;
532 else if (*value == '\'')
533 ++apos;
534
535 bool single(quot > apos);
536
537 out << (single ? '\'' : '"');
5999c315
JF
538 for (const char *value(value_), *end(value_ + size_); value != end; ++value)
539 switch (*value) {
5999c315
JF
540 case '\\': out << "\\\\"; break;
541 case '\b': out << "\\b"; break;
542 case '\f': out << "\\f"; break;
543 case '\n': out << "\\n"; break;
544 case '\r': out << "\\r"; break;
545 case '\t': out << "\\t"; break;
546 case '\v': out << "\\v"; break;
547
b4aa79af
JF
548 case '"':
549 if (!single)
550 out << "\\\"";
551 else goto simple;
552 break;
553
554 case '\'':
555 if (single)
556 out << "\\'";
557 else goto simple;
558 break;
559
5999c315
JF
560 default:
561 if (*value < 0x20 || *value >= 0x7f)
562 out << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value);
b4aa79af 563 else simple:
5999c315
JF
564 out << *value;
565 }
b4aa79af 566 out << (single ? '\'' : '"');
5999c315
JF
567}
568
e5bc40db
JF
569void CYString::PropertyName(std::ostream &out) const {
570 if (const char *word = Word())
571 out << word;
572 else
573 Output(out);
574}
575
5999c315 576void CYSwitch::Output(std::ostream &out) const {
d35a3b07 577 out << "switch(";
b09da87b 578 value_->Output(out, CYNoFlags);
d35a3b07 579 out << "){";
5999c315
JF
580 if (clauses_ != NULL)
581 out << *clauses_;
582 out << '}';
583}
584
b09da87b
JF
585void CYThis::Output(std::ostream &out, CYFlags flags) const {
586 if ((flags & CYNoLeader) != 0)
587 out << ' ';
5999c315 588 CYWord::Output(out);
b09da87b
JF
589 if ((flags & CYNoTrailer) != 0)
590 out << ' ';
5999c315
JF
591}
592
593void CYThrow::Output(std::ostream &out) const {
d35a3b07 594 out << "throw";
b09da87b
JF
595 if (value_ != NULL)
596 value_->Output(out, CYNoLeader);
5999c315
JF
597 out << ';';
598}
599
600void CYTry::Output(std::ostream &out) const {
601 out << "try";
602 try_->Output(out, true);
603 if (catch_ != NULL)
604 out << catch_;
605 if (finally_ != NULL) {
606 out << "finally";
607 finally_->Output(out, true);
608 }
609}
610
b09da87b 611void CYVariable::Output(std::ostream &out, CYFlags flags) const {
478d4ed0
JF
612 if ((flags & CYNoLeader) != 0)
613 out << ' ';
5999c315 614 out << *name_;
478d4ed0
JF
615 if ((flags & CYNoTrailer) != 0)
616 out << ' ';
5999c315
JF
617}
618
619void CYWhile::Output(std::ostream &out) const {
d35a3b07 620 out << "while(";
b09da87b 621 test_->Output(out, CYNoFlags);
d35a3b07 622 out << ')';
5999c315
JF
623 code_->Output(out, false);
624}
625
626void CYWith::Output(std::ostream &out) const {
d35a3b07 627 out << "with(";
b09da87b 628 scope_->Output(out, CYNoFlags);
d35a3b07 629 out << ')';
5999c315
JF
630 code_->Output(out, false);
631}
632
e5bc40db
JF
633void CYWord::ClassName(std::ostream &out) const {
634 out << "objc_getClass(\"" << Value() << "\")";
635}
636
5999c315
JF
637void CYWord::Output(std::ostream &out) const {
638 out << Value();
639}
e5bc40db
JF
640
641void CYWord::PropertyName(std::ostream &out) const {
642 Output(out);
643}