]>
git.saurik.com Git - cycript.git/blob - Output.cpp
6 #include <objc/runtime.h>
9 _finline CYFlags
operator ~(CYFlags rhs
) {
10 return static_cast<CYFlags
>(~static_cast<unsigned>(rhs
));
13 _finline CYFlags
operator &(CYFlags lhs
, CYFlags rhs
) {
14 return static_cast<CYFlags
>(static_cast<unsigned>(lhs
) & static_cast<unsigned>(rhs
));
17 _finline CYFlags
operator |(CYFlags lhs
, CYFlags rhs
) {
18 return static_cast<CYFlags
>(static_cast<unsigned>(lhs
) | static_cast<unsigned>(rhs
));
21 _finline CYFlags
&operator |=(CYFlags
&lhs
, CYFlags rhs
) {
22 return lhs
= lhs
| rhs
;
25 _finline CYFlags
CYLeft(CYFlags flags
) {
26 return flags
& ~CYNoTrailer
;
29 _finline CYFlags
CYCenter(CYFlags flags
) {
30 return flags
& CYNoIn
;
33 _finline CYFlags
CYRight(CYFlags flags
) {
34 return flags
& (CYNoIn
| CYNoTrailer
);
37 bool CYFalse::Value() const {
41 bool CYTrue::Value() const {
47 void CYAddressOf::Output(std::ostream
&out
, CYFlags flags
) const {
48 rhs_
->Output(out
, 1, CYLeft(flags
));
49 out
<< ".addressOf()";
52 void CYArgument::Output(std::ostream
&out
) const {
59 value_
->Output(out
, CYPA
, CYNoFlags
);
61 if (next_
->name_
== NULL
)
69 void CYArray::Output(std::ostream
&out
, CYFlags flags
) const {
71 if (elements_
!= NULL
)
72 elements_
->Output(out
);
76 void CYAssignment::Output(std::ostream
&out
, CYFlags flags
) const {
77 lhs_
->Output(out
, Precedence() - 1, CYLeft(flags
));
79 rhs_
->Output(out
, Precedence(), CYRight(flags
));
82 void CYBoolean::Output(std::ostream
&out
, CYFlags flags
) const {
83 if ((flags
& CYNoLeader
) != 0)
85 out
<< (Value() ? "true" : "false");
86 if ((flags
& CYNoTrailer
) != 0)
90 void CYBreak::Output(std::ostream
&out
) const {
93 out
<< ' ' << *label_
;
97 void CYCall::Output(std::ostream
&out
, CYFlags flags
) const {
98 function_
->Output(out
, Precedence(), CYLeft(flags
));
100 if (arguments_
!= NULL
)
101 arguments_
->Output(out
);
105 void CYCatch::Output(std::ostream
&out
) const {
106 out
<< "catch(" << *name_
<< ')';
107 code_
->Output(out
, true);
110 void CYCategory::Output(std::ostream
&out
) const {
111 out
<< "(function($cys,$cyp,$cyc,$cyn,$cyt){";
112 out
<< "$cyp=object_getClass($cys);";
114 if (messages_
!= NULL
)
115 messages_
->Output(out
, true);
117 name_
->ClassName(out
);
121 void CYClass::Output(std::ostream
&out
) const {
122 out
<< "(function($cys,$cyp,$cyc,$cyn,$cyt,$cym){";
123 out
<< "$cyp=object_getClass($cys);";
124 out
<< "$cyc=objc_allocateClassPair($cys,\"" << *name_
<< "\",0);";
125 out
<< "$cym=object_getClass($cyc);";
127 fields_
->Output(out
);
128 if (messages_
!= NULL
)
129 messages_
->Output(out
, false);
130 out
<< "objc_registerClassPair($cyc);";
133 super_
->Output(out
, CYPA
, CYNoFlags
);
139 void 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
) {
147 next
= expression
->next_
;
148 CYFlags
right(next
!= NULL
? center
: CYRight(flags
));
149 expression
->Output(out
, right
);
152 expression
->Output(out
, flags
);
155 void CYCondition::Output(std::ostream
&out
, CYFlags flags
) const {
156 test_
->Output(out
, Precedence() - 1, CYLeft(flags
));
159 true_
->Output(out
, CYPA
, CYNoFlags
);
161 false_
->Output(out
, CYPA
, CYRight(flags
));
164 void CYContinue::Output(std::ostream
&out
) const {
167 out
<< ' ' << *label_
;
171 void CYClause::Output(std::ostream
&out
) const {
174 case_
->Output(out
, CYNoFlags
);
179 code_
->Output(out
, false);
183 // XXX: deal with NoIn
184 void CYDeclaration::Part(std::ostream
&out
) const {
189 void CYDeclaration::Output(std::ostream
&out
) const {
191 if (initialiser_
!= NULL
) {
193 initialiser_
->Output(out
, CYPA
, CYNoFlags
);
197 // XXX: deal with NoIn
198 void CYDeclarations::Part(std::ostream
&out
) const {
201 const CYDeclarations
*declaration(this);
203 out
<< *declaration
->declaration_
;
204 declaration
= declaration
->next_
;
206 if (declaration
!= NULL
) {
212 void CYDeclarations::Output(std::ostream
&out
) const {
217 void CYDoWhile::Output(std::ostream
&out
) const {
218 // XXX: extra space character!
220 code_
->Output(out
, false);
222 test_
->Output(out
, CYNoFlags
);
226 void CYElement::Output(std::ostream
&out
) const {
228 value_
->Output(out
, CYPA
, CYNoFlags
);
229 if (next_
!= NULL
|| value_
== NULL
)
235 void CYEmpty::Output(std::ostream
&out
) const {
239 void CYEmpty::Output(std::ostream
&out
, bool block
) const {
241 CYSource::Output(out
, block
);
246 void CYExpress::Output(std::ostream
&out
) const {
247 expression_
->Output(out
, CYNoFunction
| CYNoBrace
);
251 void CYExpression::ClassName(std::ostream
&out
) const {
252 Output(out
, CYPA
, CYNoFlags
);
255 void CYExpression::Part(std::ostream
&out
) const {
256 // XXX: this should handle LeftHandSideExpression
260 void CYExpression::Output(std::ostream
&out
, unsigned precedence
, CYFlags flags
) const {
261 if (precedence
< Precedence()) {
263 Output(out
, CYNoFlags
);
269 void CYField::Output(std::ostream
&out
) const {
273 void CYFor::Output(std::ostream
&out
) const {
275 if (initialiser_
!= NULL
)
276 initialiser_
->Part(out
);
279 test_
->Output(out
, CYNoFlags
);
281 if (increment_
!= NULL
)
282 increment_
->Output(out
, CYNoFlags
);
284 code_
->Output(out
, false);
287 void CYForIn::Output(std::ostream
&out
) const {
289 initialiser_
->Part(out
);
290 // XXX: deal with this space character!
293 set_
->Output(out
, CYNoLeader
);
295 code_
->Output(out
, false);
298 void CYFunction::Output(std::ostream
&out
) const {
299 CYLambda::Output(out
, CYNoFlags
);
302 void CYFunctionParameter::Output(std::ostream
&out
) const {
310 void CYIf::Output(std::ostream
&out
) const {
312 test_
->Output(out
, CYNoFlags
);
314 true_
->Output(out
, true);
315 if (false_
!= NULL
) {
317 false_
->Output(out
, false);
321 void CYIndirect::Output(std::ostream
&out
, CYFlags flags
) const {
322 rhs_
->Output(out
, 1, CYLeft(flags
));
326 void CYInfix::Output(std::ostream
&out
, CYFlags flags
) const {
327 const char *name(Operator());
328 bool protect((flags
& CYNoIn
) != 0 && strcmp(name
, "in"));
331 bool alphabetic(Alphabetic());
332 CYFlags
left(protect
? CYNoFlags
: CYLeft(flags
));
335 lhs_
->Output(out
, Precedence(), left
);
337 CYFlags
right(protect
? CYNoFlags
: CYRight(flags
));
340 rhs_
->Output(out
, Precedence() - 1, right
);
345 void CYLambda::Output(std::ostream
&out
, CYFlags flags
) const {
346 bool protect((flags
& CYNoFunction
) != 0);
351 out
<< ' ' << *name_
;
353 if (parameters_
!= NULL
)
363 void CYMember::Output(std::ostream
&out
, CYFlags flags
) const {
364 object_
->Output(out
, Precedence(), CYLeft(flags
));
365 if (const char *word
= property_
->Word())
369 property_
->Output(out
, CYNoFlags
);
374 void CYMessage::Output(std::ostream
&out
, bool replace
) const {
376 next_
->Output(out
, replace
);
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
)
385 out
<< "$cyt=$cyn.type($cy" << (instance_
? 's' : 'p') << ");";
386 out
<< "class_" << (replace
? "replace" : "add") << "Method($cy" << (instance_
? 'c' : 'm') << ",$cyn,";
387 out
<< "new Functor(function(self,_cmd";
388 for (CYMessageParameter
*parameter(parameter_
); parameter
!= NULL
; parameter
= parameter
->next_
)
389 if (parameter
->name_
!= NULL
)
390 out
<< ',' << *parameter
->name_
;
391 out
<< "){return function(){";
394 out
<< "}.call(self);},$cyt),$cyt);";
397 void CYNew::Output(std::ostream
&out
, CYFlags flags
) const {
398 if ((flags
& CYNoLeader
) != 0)
401 constructor_
->Output(out
, Precedence(), CYCenter(flags
) | CYNoLeader
);
403 if (arguments_
!= NULL
)
404 arguments_
->Output(out
);
408 void CYNull::Output(std::ostream
&out
, CYFlags flags
) const {
409 if ((flags
& CYNoLeader
) != 0)
412 if ((flags
& CYNoTrailer
) != 0)
416 void CYNumber::Output(std::ostream
&out
, CYFlags flags
) const {
417 if ((flags
& CYNoLeader
) != 0)
419 // XXX: decide on correct precision
420 out
<< std::setprecision(9) << Value();
421 if ((flags
& CYNoTrailer
) != 0)
425 void CYNumber::PropertyName(std::ostream
&out
) const {
429 void CYObject::Output(std::ostream
&out
, CYFlags flags
) const {
430 bool protect((flags
& CYNoBrace
) != 0);
434 if (property_
!= NULL
)
435 property_
->Output(out
);
441 void CYPostfix::Output(std::ostream
&out
, CYFlags flags
) const {
442 lhs_
->Output(out
, Precedence(), CYLeft(flags
));
446 void CYPrefix::Output(std::ostream
&out
, CYFlags flags
) const {
447 bool alphabetic(Alphabetic());
449 CYFlags
right(CYRight(flags
));
452 rhs_
->Output(out
, Precedence(), right
);
455 void CYProperty::Output(std::ostream
&out
) const {
456 name_
->PropertyName(out
);
458 value_
->Output(out
, CYPA
, CYNoFlags
);
465 void CYReturn::Output(std::ostream
&out
) const {
468 value_
->Output(out
, CYNoLeader
);
472 void CYSelector::Output(std::ostream
&out
, CYFlags flags
) const {
473 if ((flags
& CYNoLeader
) != 0)
475 out
<< "new Selector(\"";
481 void CYSelectorPart::Output(std::ostream
&out
) const {
490 void CYSend::Output(std::ostream
&out
, CYFlags flags
) const {
491 if ((flags
& CYNoLeader
) != 0)
493 out
<< "objc_msgSend(";
494 self_
->Output(out
, CYPA
, CYNoFlags
);
496 std::ostringstream name
;
497 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
498 if (argument
->name_
!= NULL
) {
499 name
<< *argument
->name_
;
500 if (argument
->value_
!= NULL
)
503 out
<< reinterpret_cast<void *>(sel_registerName(name
.str().c_str()));
504 for (CYArgument
*argument(arguments_
); argument
!= NULL
; argument
= argument
->next_
)
505 if (argument
->value_
!= NULL
) {
507 argument
->value_
->Output(out
, CYPA
, CYNoFlags
);
512 void CYSource::Show(std::ostream
&out
) const {
513 for (const CYSource
*next(this); next
!= NULL
; next
= next
->next_
)
517 void CYSource::Output(std::ostream
&out
, bool block
) const {
518 if (!block
&& next_
== NULL
)
527 void CYString::Output(std::ostream
&out
, CYFlags flags
) const {
528 unsigned quot(0), apos(0);
529 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
532 else if (*value
== '\'')
535 bool single(quot
> apos
);
537 out
<< (single
? '\'' : '"');
538 for (const char *value(value_
), *end(value_
+ size_
); value
!= end
; ++value
)
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;
561 if (*value
< 0x20 || *value
>= 0x7f)
562 out
<< "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value
);
566 out
<< (single
? '\'' : '"');
569 void CYString::PropertyName(std::ostream
&out
) const {
570 if (const char *word
= Word())
576 void CYSwitch::Output(std::ostream
&out
) const {
578 value_
->Output(out
, CYNoFlags
);
580 if (clauses_
!= NULL
)
585 void CYThis::Output(std::ostream
&out
, CYFlags flags
) const {
586 if ((flags
& CYNoLeader
) != 0)
589 if ((flags
& CYNoTrailer
) != 0)
593 void CYThrow::Output(std::ostream
&out
) const {
596 value_
->Output(out
, CYNoLeader
);
600 void CYTry::Output(std::ostream
&out
) const {
602 try_
->Output(out
, true);
605 if (finally_
!= NULL
) {
607 finally_
->Output(out
, true);
611 void CYVariable::Output(std::ostream
&out
, CYFlags flags
) const {
612 if ((flags
& CYNoLeader
) != 0)
615 if ((flags
& CYNoTrailer
) != 0)
619 void CYWhile::Output(std::ostream
&out
) const {
621 test_
->Output(out
, CYNoFlags
);
623 code_
->Output(out
, false);
626 void CYWith::Output(std::ostream
&out
) const {
628 scope_
->Output(out
, CYNoFlags
);
630 code_
->Output(out
, false);
633 void CYWord::ClassName(std::ostream
&out
) const {
634 out
<< "objc_getClass(\"" << Value() << "\")";
637 void CYWord::Output(std::ostream
&out
) const {
641 void CYWord::PropertyName(std::ostream
&out
) const {