1 /* Cycript - Optimizing JavaScript Compiler/Runtime
 
   2  * Copyright (C) 2009-2010  Jay Freeman (saurik)
 
   5 /* GNU Lesser General Public License, Version 3 {{{ */
 
   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.
 
  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.
 
  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/>.
 
  22 #include "Replace.hpp"
 
  23 #include "ObjectiveC/Syntax.hpp"
 
  25 #include <Foundation/Foundation.h>
 
  28 void CYCategory::Output(CYOutput &out, CYFlags flags) const {
 
  29     out << "(function($cys,$cyp,$cyc,$cyn,$cyt){";
 
  30     out << "$cyp=object_getClass($cys);";
 
  32     if (messages_ != NULL)
 
  33         messages_->Output(out, true);
 
  35     name_->ClassName(out, true);
 
  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,";
 
  46         name_->ClassName(out, false);
 
  48         out << "$cyq(\"CY$\")";
 
  50     out << "$cym=object_getClass($cyc);";
 
  53     if (messages_ != NULL)
 
  54         messages_->Output(out, false);
 
  55     if (protocols_ != NULL) {
 
  60     out << "objc_registerClassPair($cyc);";
 
  61     out << "return $cyc;";
 
  64         super_->Output(out, CYAssign::Precedence_, CYNoFlags);
 
  70 void CYClassExpression::Output(CYOutput &out, CYFlags flags) const {
 
  71     CYClass::Output(out, flags);
 
  74 void CYClassStatement::Output(CYOutput &out, CYFlags flags) const {
 
  75     CYClass::Output(out, flags);
 
  78 void CYField::Output(CYOutput &out) const {
 
  81 void CYImport::Output(CYOutput &out, CYFlags flags) const {
 
  85 void CYMessage::Output(CYOutput &out, bool replace) const {
 
  86     out << (instance_ ? '-' : '+');
 
  88     CYForEach (parameter, parameters_)
 
  89         if (parameter->tag_ != NULL) {
 
  90             out << ' ' << *parameter->tag_;
 
  91             if (parameter->name_ != NULL)
 
  92                 out << ':' << *parameter->name_;
 
  98 void CYBox::Output(CYOutput &out, CYFlags flags) const {
 
 100     value_->Output(out, Precedence(), CYRight(flags));
 
 103 void CYProtocol::Output(CYOutput &out) const {
 
 104     name_->Output(out, CYAssign::Precedence_, CYNoFlags);
 
 106         out << ',' << ' ' << *next_;
 
 109 void CYSelector::Output(CYOutput &out, CYFlags flags) const {
 
 110     out << "@selector" << '(' << name_ << ')';
 
 113 void CYSelectorPart::Output(CYOutput &out) const {
 
 120 void CYSend::Output(CYOutput &out, CYFlags flags) const {
 
 121     CYForEach (argument, arguments_)
 
 122         if (argument->name_ != NULL) {
 
 123             out << ' ' << *argument->name_;
 
 124             if (argument->value_ != NULL)
 
 125                 out << ':' << *argument->value_;
 
 129 void CYSendDirect::Output(CYOutput &out, CYFlags flags) const {
 
 131     self_->Output(out, CYAssign::Precedence_, CYNoFlags);
 
 132     CYSend::Output(out, flags);
 
 136 void CYSendSuper::Output(CYOutput &out, CYFlags flags) const {
 
 137     out << '[' << "super";
 
 138     CYSend::Output(out, flags);