1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
23 #include "Replace.hpp"
27 CYFunctionExpression
*CYNonLocalize(CYContext
&context
, CYFunctionExpression
*function
) {
28 function
->nonlocal_
= context
.nextlocal_
;
32 CYExpression
*CYAdd::Replace(CYContext
&context
) {
33 CYInfix::Replace(context
);
35 CYExpression
*lhp(lhs_
->Primitive(context
));
36 CYExpression
*rhp(rhs_
->Primitive(context
));
38 CYString
*lhs(dynamic_cast<CYString
*>(lhp
));
39 CYString
*rhs(dynamic_cast<CYString
*>(rhp
));
41 if (lhs
!= NULL
|| rhs
!= NULL
) {
43 lhs
= lhp
->String(context
);
46 } else if (rhs
== NULL
) {
47 rhs
= rhp
->String(context
);
52 return lhs
->Concat(context
, rhs
);
55 if (CYNumber
*lhn
= lhp
->Number(context
))
56 if (CYNumber
*rhn
= rhp
->Number(context
))
57 return $
D(lhn
->Value() + rhn
->Value());
62 CYExpression
*CYAddressOf::Replace(CYContext
&context
) {
63 return $
C0($
M(rhs_
, $
S("$cya")));
66 CYArgument
*CYArgument::Replace(CYContext
&context
) { $
T(NULL
)
67 context
.Replace(value_
);
68 next_
= next_
->Replace(context
);
80 CYExpression
*CYArray::Replace(CYContext
&context
) {
81 elements_
->Replace(context
);
85 CYExpression
*CYArrayComprehension::Replace(CYContext
&context
) {
86 CYVariable
*cyv($
V("$cyv"));
88 return $
C0($
F(NULL
, $
P1($
L("$cyv"), comprehensions_
->Parameters(context
)), $$
->*
89 $
E($
CYAssign(cyv
, $
CYArray()))->*
90 comprehensions_
->Replace(context
, $
E($
C1($
M(cyv
, $
S("push")), expression_
)))->*
95 CYExpression
*CYAssignment::Replace(CYContext
&context
) {
96 context
.Replace(lhs_
);
97 context
.Replace(rhs_
);
101 CYStatement
*CYBlock::Replace(CYContext
&context
) {
102 context
.ReplaceAll(statements_
);
103 if (statements_
== NULL
)
108 CYStatement
*CYBreak::Replace(CYContext
&context
) {
112 CYExpression
*CYCall::AddArgument(CYContext
&context
, CYExpression
*value
) {
113 CYArgument
**argument(&arguments_
);
114 while (*argument
!= NULL
)
115 argument
= &(*argument
)->next_
;
116 *argument
= $
CYArgument(value
);
120 CYExpression
*CYCall::Replace(CYContext
&context
) {
121 context
.Replace(function_
);
122 arguments_
->Replace(context
);
129 void Catch::Replace(CYContext
&context
) { $
T()
130 CYScope
scope(true, context
, code_
.statements_
);
132 context
.Replace(name_
);
133 context
.scope_
->Declare(context
, name_
, CYIdentifierCatch
);
135 code_
.Replace(context
);
141 void CYClause::Replace(CYContext
&context
) { $
T()
142 context
.Replace(case_
);
143 context
.ReplaceAll(statements_
);
144 next_
->Replace(context
);
147 CYStatement
*CYComment::Replace(CYContext
&context
) {
151 CYExpression
*CYCompound::Replace(CYContext
&context
) {
152 context
.ReplaceAll(expressions_
);
153 if (expressions_
== NULL
)
158 CYExpression
*CYCompound::Primitive(CYContext
&context
) {
159 CYExpression
*expression(expressions_
);
160 if (expression
== NULL
)
162 while (expression
->next_
!= NULL
)
163 expression
= expression
->next_
;
164 return expression
->Primitive(context
);
167 CYFunctionParameter
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
168 CYFunctionParameter
*next(next_
->Parameters(context
));
169 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
170 parameter
->SetNext(next
);
176 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
177 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
180 CYExpression
*CYCondition::Replace(CYContext
&context
) {
181 context
.Replace(test_
);
182 context
.Replace(true_
);
183 context
.Replace(false_
);
187 void CYContext::NonLocal(CYStatement
*&statements
) {
188 CYContext
&context(*this);
190 if (nextlocal_
!= NULL
&& nextlocal_
->identifier_
!= NULL
) {
191 CYIdentifier
*cye($
I("$cye")->Replace(context
));
192 CYIdentifier
*unique(nextlocal_
->identifier_
->Replace(context
));
194 CYStatement
*declare(
195 $
CYVar($
L1($
CYDeclaration(unique
, $
CYObject()))));
197 cy::Syntax::Catch
*rescue(
198 $
cy::Syntax::Catch(cye
, $$
->*
199 $
CYIf($
CYIdentical($
M($
V(cye
), $
S("$cyk")), $
V(unique
)), $$
->*
200 $
CYReturn($
M($
V(cye
), $
S("$cyv"))))->*
201 $
cy::Syntax::Throw($
V(cye
))));
203 context
.Replace(declare
);
204 rescue
->Replace(context
);
208 $
cy::Syntax::Try(statements
, rescue
, NULL
);
212 CYIdentifier
*CYContext::Unique() {
213 return $
CYIdentifier($pool
.strcat("$cy", $pool
.itoa(unique_
++), NULL
));
216 CYStatement
*CYContinue::Replace(CYContext
&context
) {
220 CYStatement
*CYDebugger::Replace(CYContext
&context
) {
224 CYAssignment
*CYDeclaration::Assignment(CYContext
&context
) {
225 if (initialiser_
== NULL
)
228 CYAssignment
*value($
CYAssign(Variable(context
), initialiser_
));
233 CYVariable
*CYDeclaration::Variable(CYContext
&context
) {
234 return $
V(identifier_
);
237 CYStatement
*CYDeclaration::ForEachIn(CYContext
&context
, CYExpression
*value
) {
238 return $
CYVar($
L1($
CYDeclaration(identifier_
, value
)));
241 CYExpression
*CYDeclaration::Replace(CYContext
&context
) {
242 context
.Replace(identifier_
);
243 context
.scope_
->Declare(context
, identifier_
, CYIdentifierVariable
);
244 return Variable(context
);
247 void CYDeclarations::Replace(CYContext
&context
) { $
T()
248 declaration_
->Replace(context
);
249 next_
->Replace(context
);
252 CYProperty
*CYDeclarations::Property(CYContext
&context
) { $
T(NULL
)
253 return $
CYProperty(declaration_
->identifier_
, declaration_
->initialiser_
, next_
->Property(context
));
256 CYFunctionParameter
*CYDeclarations::Parameter(CYContext
&context
) { $
T(NULL
)
257 return $
CYFunctionParameter($
CYDeclaration(declaration_
->identifier_
), next_
->Parameter(context
));
260 CYArgument
*CYDeclarations::Argument(CYContext
&context
) { $
T(NULL
)
261 return $
CYArgument(declaration_
->initialiser_
, next_
->Argument(context
));
264 CYCompound
*CYDeclarations::Compound(CYContext
&context
) { $
T(NULL
)
265 CYCompound
*compound(next_
->Compound(context
) ?: $
CYCompound());
266 if (CYAssignment
*assignment
= declaration_
->Assignment(context
))
267 compound
->AddPrev(assignment
);
271 CYExpression
*CYDirectMember::Replace(CYContext
&context
) {
272 context
.Replace(object_
);
273 context
.Replace(property_
);
277 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
278 context
.Replace(test_
);
279 context
.Replace(code_
);
283 void CYElement::Replace(CYContext
&context
) { $
T()
284 context
.Replace(value_
);
285 next_
->Replace(context
);
288 CYStatement
*CYEmpty::Replace(CYContext
&context
) {
292 CYExpression
*CYEncodedType::Replace(CYContext
&context
) {
293 return typed_
->Replace(context
);
296 CYStatement
*CYExpress::Replace(CYContext
&context
) {
297 while (CYExpress
*express
= dynamic_cast<CYExpress
*>(next_
)) {
298 CYCompound
*compound(dynamic_cast<CYCompound
*>(express
->expression_
));
299 if (compound
== NULL
)
300 compound
= $
CYCompound(express
->expression_
);
301 compound
->AddPrev(expression_
);
302 expression_
= compound
;
303 SetNext(express
->next_
);
306 context
.Replace(expression_
);
307 if (expression_
== NULL
)
313 CYExpression
*CYExpression::AddArgument(CYContext
&context
, CYExpression
*value
) {
314 return $
C1(this, value
);
317 CYExpression
*CYExpression::ClassName(CYContext
&context
, bool object
) {
321 CYStatement
*CYExpression::ForEachIn(CYContext
&context
, CYExpression
*value
) {
322 return $
E($
CYAssign(this, value
));
325 CYAssignment
*CYExpression::Assignment(CYContext
&context
) {
329 CYNumber
*CYFalse::Number(CYContext
&context
) {
333 CYString
*CYFalse::String(CYContext
&context
) {
337 CYExpression
*CYFatArrow::Replace(CYContext
&context
) {
338 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
339 function
->this_
.SetNext(context
.this_
);
343 void CYFinally::Replace(CYContext
&context
) { $
T()
344 code_
.Replace(context
);
347 CYStatement
*CYFor::Replace(CYContext
&context
) {
348 context
.Replace(initialiser_
);
349 context
.Replace(test_
);
350 context
.Replace(increment_
);
351 context
.Replace(code_
);
355 CYCompound
*CYForDeclarations::Replace(CYContext
&context
) {
356 declarations_
->Replace(context
);
357 return declarations_
->Compound(context
);
360 // XXX: this still feels highly suboptimal
361 CYStatement
*CYForIn::Replace(CYContext
&context
) {
362 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
363 return $
CYBlock($$
->*
368 context
.Replace(initialiser_
);
369 context
.Replace(set_
);
370 context
.Replace(code_
);
374 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
375 return $
CYFunctionParameter($
CYDeclaration(name_
));
378 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
379 return $
CYForIn($
V(name_
), set_
, CYComprehension::Replace(context
, statement
));
382 CYStatement
*CYForOf::Replace(CYContext
&context
) {
383 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
384 return $
CYBlock($$
->*
389 CYIdentifier
*cys($
I("$cys")), *cyt($
I("$cyt"));
391 return $
CYLetStatement($
L2($
CYDeclaration(cys
, set_
), $
CYDeclaration(cyt
)), $$
->*
392 $
CYForIn($
V(cyt
), $
V(cys
), $
CYBlock($$
->*
393 initialiser_
->ForEachIn(context
, $
M($
V(cys
), $
V(cyt
)))->*
399 CYFunctionParameter
*CYForOfComprehension::Parameter(CYContext
&context
) const {
400 return $
CYFunctionParameter($
CYDeclaration(name_
));
403 CYStatement
*CYForOfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
404 CYIdentifier
*cys($
I("cys"));
406 return $
E($
C0($
F(NULL
, $
P1($
L("$cys")), $$
->*
407 $
E($
CYAssign($
V(cys
), set_
))->*
408 $
CYForIn($
V(name_
), $
V(cys
), $
CYBlock($$
->*
409 $
E($
CYAssign($
V(name_
), $
M($
V(cys
), $
V(name_
))))->*
410 CYComprehension::Replace(context
, statement
)
415 void CYFunction::Inject(CYContext
&context
) {
416 context
.Replace(name_
);
417 context
.scope_
->Declare(context
, name_
, CYIdentifierOther
);
420 void CYFunction::Replace_(CYContext
&context
, bool outer
) {
424 CYThisScope
*_this(context
.this_
);
425 context
.this_
= CYGetLast(&this_
);
427 CYNonLocal
*nonlocal(context
.nonlocal_
);
428 CYNonLocal
*nextlocal(context
.nextlocal_
);
431 if (nonlocal_
!= NULL
) {
433 context
.nonlocal_
= nonlocal_
;
436 nonlocal_
= $
CYNonLocal();
437 context
.nextlocal_
= nonlocal_
;
440 CYScope
scope(!localize
, context
, code_
.statements_
);
442 if (!outer
&& name_
!= NULL
)
445 parameters_
->Replace(context
, code_
);
446 code_
.Replace(context
);
448 if (CYIdentifier
*identifier
= this_
.identifier_
)
449 code_
.statements_
= $$
->*
450 $
CYVar($
L1($
CYDeclaration(identifier
, $
CYThis())))->*
454 context
.NonLocal(code_
.statements_
);
456 context
.nextlocal_
= nextlocal
;
457 context
.nonlocal_
= nonlocal
;
459 context
.this_
= _this
;
464 CYExpression
*CYFunctionExpression::Replace(CYContext
&context
) {
465 Replace_(context
, false);
469 void CYFunctionParameter::Replace(CYContext
&context
, CYBlock
&code
) { $
T()
470 CYAssignment
*assignment(initialiser_
->Assignment(context
));
471 context
.Replace(initialiser_
);
473 next_
->Replace(context
, code
);
475 if (assignment
!= NULL
)
476 // XXX: this cast is quite incorrect
477 code
.AddPrev($
CYIf($
CYIdentical($
CYTypeOf(dynamic_cast<CYExpression
*>(initialiser_
)), $
S("undefined")), $$
->*
482 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
483 Replace_(context
, true);
487 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
) {
488 if (replace_
!= NULL
&& replace_
!= this)
489 return replace_
->Replace(context
);
490 replace_
= context
.scope_
->Lookup(context
, this);
494 CYStatement
*CYIf::Replace(CYContext
&context
) {
495 context
.Replace(test_
);
496 context
.Replace(true_
);
497 context
.Replace(false_
);
501 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
505 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
506 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
509 CYExpression
*CYIndirect::Replace(CYContext
&context
) {
510 return $
M(rhs_
, $
S("$cyi"));
513 CYExpression
*CYIndirectMember::Replace(CYContext
&context
) {
514 return $
M($
CYIndirect(object_
), property_
);
517 CYExpression
*CYInfix::Replace(CYContext
&context
) {
518 context
.Replace(lhs_
);
519 context
.Replace(rhs_
);
523 CYStatement
*CYLabel::Replace(CYContext
&context
) {
524 context
.Replace(statement_
);
528 CYExpression
*CYLambda::Replace(CYContext
&context
) {
529 return $
N2($
V("Functor"), $
CYFunctionExpression(NULL
, parameters_
->Parameters(context
), statements_
), parameters_
->TypeSignature(context
, typed_
->Replace(context
)));
532 CYStatement
*CYLetStatement::Replace(CYContext
&context
) {
533 return $
E($
CYCall(CYNonLocalize(context
, $
CYFunctionExpression(NULL
, declarations_
->Parameter(context
), code_
)), declarations_
->Argument(context
)));
539 CYExpression
*New::AddArgument(CYContext
&context
, CYExpression
*value
) {
540 CYSetLast(arguments_
) = $
CYArgument(value
);
544 CYExpression
*New::Replace(CYContext
&context
) {
545 context
.Replace(constructor_
);
546 arguments_
->Replace(context
);
552 CYNumber
*CYNull::Number(CYContext
&context
) {
556 CYString
*CYNull::String(CYContext
&context
) {
560 CYNumber
*CYNumber::Number(CYContext
&context
) {
564 CYString
*CYNumber::String(CYContext
&context
) {
565 // XXX: there is a precise algorithm for this
566 return $
S($pool
.sprintf(24, "%.17g", Value()));
569 CYExpression
*CYObject::Replace(CYContext
&context
) {
570 properties_
->Replace(context
);
574 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
575 context
.Replace(lhs_
);
579 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
580 context
.Replace(rhs_
);
584 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
585 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
586 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
589 struct IdentifierUsageLess
:
590 std::binary_function
<CYIdentifier
*, CYIdentifier
*, bool>
592 _finline
bool operator ()(CYIdentifier
*lhs
, CYIdentifier
*rhs
) const {
593 if (lhs
->usage_
!= rhs
->usage_
)
594 return lhs
->usage_
> rhs
->usage_
;
599 typedef std::set
<CYIdentifier
*, IdentifierUsageLess
> IdentifierUsages
;
602 void CYProgram::Replace(CYContext
&context
) {
603 CYScope
scope(true, context
, statements_
);
605 context
.nextlocal_
= $
CYNonLocal();
606 context
.ReplaceAll(statements_
);
607 context
.NonLocal(statements_
);
613 CYCStringSet external
;
614 for (CYIdentifierValueSet::const_iterator
i(scope
.identifiers_
.begin()); i
!= scope
.identifiers_
.end(); ++i
)
615 external
.insert((*i
)->Word());
617 IdentifierUsages usages
;
619 if (offset
< context
.rename_
.size())
620 CYForEach (i
, context
.rename_
[offset
].identifier_
)
623 // XXX: totalling the probable occurrences and sorting by them would improve the result
624 for (CYIdentifierUsageVector::const_iterator
i(context
.rename_
.begin()); i
!= context
.rename_
.end(); ++i
, ++offset
) {
625 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
629 if (context
.options_
.verbose_
)
630 name
= $pool
.strcat("$", $pool
.itoa(offset
), NULL
);
636 unsigned position(7), local(offset
+ 1);
639 unsigned index(local
% (sizeof(MappingSet
) - 1));
640 local
/= sizeof(MappingSet
) - 1;
641 id
[--position
] = MappingSet
[index
];
642 } while (local
!= 0);
644 if (external
.find(id
+ position
) != external
.end()) {
649 name
= $pool
.strmemdup(id
+ position
, 7 - position
);
650 // XXX: at some point, this could become a keyword
653 CYForEach (identifier
, i
->identifier_
)
654 identifier
->Set(name
);
658 void CYProperty::Replace(CYContext
&context
) { $
T()
659 context
.Replace(value_
);
660 next_
->Replace(context
);
665 CYStatement
*CYReturn::Replace(CYContext
&context
) {
666 if (context
.nonlocal_
!= NULL
) {
667 CYProperty
*value(value_
== NULL
? NULL
: $
CYProperty($
S("$cyv"), value_
));
668 return $
cy::Syntax::Throw($
CYObject(
669 $
CYProperty($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
)
673 context
.Replace(value_
);
677 CYExpression
*CYRubyBlock::Replace(CYContext
&context
) {
678 // XXX: this needs to do something much more epic to handle return
679 return call_
->AddArgument(context
, proc_
->Replace(context
));
682 CYExpression
*CYRubyProc::Replace(CYContext
&context
) {
683 return CYNonLocalize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
));
686 CYScope::CYScope(bool transparent
, CYContext
&context
, CYStatement
*&statements
) :
687 transparent_(transparent
),
689 statements_(statements
),
690 parent_(context
.scope_
)
692 context_
.scope_
= this;
695 CYScope::~CYScope() {
698 void CYScope::Close() {
699 context_
.scope_
= parent_
;
700 Scope(context_
, statements_
);
703 void CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierFlags flags
) {
704 if (!transparent_
|| flags
== CYIdentifierArgument
|| flags
== CYIdentifierCatch
)
705 internal_
.insert(CYIdentifierAddressFlagsMap::value_type(identifier
, flags
));
706 else if (parent_
!= NULL
)
707 parent_
->Declare(context
, identifier
, flags
);
710 CYIdentifier
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
711 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
712 return *insert
.first
;
715 void CYScope::Merge(CYContext
&context
, CYIdentifier
*identifier
) {
716 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
717 if (!insert
.second
) {
718 if ((*insert
.first
)->offset_
< identifier
->offset_
)
719 (*insert
.first
)->offset_
= identifier
->offset_
;
720 identifier
->replace_
= *insert
.first
;
721 (*insert
.first
)->usage_
+= identifier
->usage_
+ 1;
726 struct IdentifierOffset
{
728 CYIdentifierFlags flags_
;
730 CYIdentifier
*identifier_
;
732 IdentifierOffset(CYIdentifier
*identifier
, CYIdentifierFlags flags
) :
733 offset_(identifier
->offset_
),
735 usage_(identifier
->usage_
),
736 identifier_(identifier
)
741 struct IdentifierOffsetLess
:
742 std::binary_function
<const IdentifierOffset
&, const IdentifierOffset
&, bool>
744 _finline
bool operator ()(const IdentifierOffset
&lhs
, const IdentifierOffset
&rhs
) const {
745 if (lhs
.offset_
!= rhs
.offset_
)
746 return lhs
.offset_
< rhs
.offset_
;
747 if (lhs
.flags_
!= rhs
.flags_
)
748 return lhs
.flags_
< rhs
.flags_
;
749 /*if (lhs.usage_ != rhs.usage_)
750 return lhs.usage_ < rhs.usage_;*/
751 return lhs
.identifier_
< rhs
.identifier_
;
755 typedef std::set
<IdentifierOffset
, IdentifierOffsetLess
> IdentifierOffsets
;
758 void CYScope::Scope(CYContext
&context
, CYStatement
*&statements
) {
762 CYDeclarations
*last(NULL
), *curr(NULL
);
764 IdentifierOffsets offsets
;
766 for (CYIdentifierAddressFlagsMap::const_iterator
i(internal_
.begin()); i
!= internal_
.end(); ++i
)
767 if (i
->second
!= CYIdentifierMagic
)
768 offsets
.insert(IdentifierOffset(i
->first
, i
->second
));
772 for (IdentifierOffsets::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
773 if (i
->flags_
== CYIdentifierVariable
) {
774 CYDeclarations
*next($
CYDeclarations($
CYDeclaration(i
->identifier_
)));
782 if (offset
< i
->offset_
)
784 if (context
.rename_
.size() <= offset
)
785 context
.rename_
.resize(offset
+ 1);
787 CYIdentifierUsage
&rename(context
.rename_
[offset
++]);
788 i
->identifier_
->SetNext(rename
.identifier_
);
789 rename
.identifier_
= i
->identifier_
;
790 rename
.usage_
+= i
->identifier_
->usage_
+ 1;
794 CYVar
*var($
CYVar(last
));
795 var
->SetNext(statements
);
799 for (CYIdentifierValueSet::const_iterator
i(identifiers_
.begin()); i
!= identifiers_
.end(); ++i
)
800 if (internal_
.find(*i
) == internal_
.end()) {
801 //std::cout << *i << '=' << offset << std::endl;
802 if ((*i
)->offset_
< offset
)
803 (*i
)->offset_
= offset
;
804 parent_
->Merge(context
, *i
);
808 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
809 size_t size(size_
+ rhs
->size_
);
810 char *value($
char[size
+ 1]);
811 memcpy(value
, value_
, size_
);
812 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
814 return $
S(value
, size
);
817 CYNumber
*CYString::Number(CYContext
&context
) {
818 // XXX: there is a precise algorithm for this
822 CYString
*CYString::String(CYContext
&context
) {
826 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
827 context
.Replace(value_
);
828 clauses_
->Replace(context
);
832 CYExpression
*CYThis::Replace(CYContext
&context
) {
833 if (context
.this_
!= NULL
)
834 return $
V(context
.this_
->Identifier(context
));
841 CYStatement
*Throw::Replace(CYContext
&context
) {
842 context
.Replace(value_
);
848 CYExpression
*CYTrivial::Replace(CYContext
&context
) {
852 CYNumber
*CYTrue::Number(CYContext
&context
) {
856 CYString
*CYTrue::String(CYContext
&context
) {
863 CYStatement
*Try::Replace(CYContext
&context
) {
864 code_
.Replace(context
);
865 catch_
->Replace(context
);
866 finally_
->Replace(context
);
872 CYExpression
*CYTypeArrayOf::Replace_(CYContext
&context
, CYExpression
*type
) {
873 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("arrayOf")), $
CYArgument(size_
)));
876 CYExpression
*CYTypeConstant::Replace_(CYContext
&context
, CYExpression
*type
) {
877 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("constant"))));
880 CYStatement
*CYTypeDefinition::Replace(CYContext
&context
) {
881 return $
E($
CYAssign($
V(typed_
->identifier_
), typed_
->Replace(context
)));
884 CYExpression
*CYTypeModifier::Replace(CYContext
&context
, CYExpression
*type
) { $
T(type
)
885 return Replace_(context
, type
);
888 CYExpression
*CYTypeFunctionWith::Replace_(CYContext
&context
, CYExpression
*type
) {
889 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("functionWith")), parameters_
->Argument(context
)));
892 CYExpression
*CYTypePointerTo::Replace_(CYContext
&context
, CYExpression
*type
) {
893 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("pointerTo"))));
896 CYExpression
*CYTypeVolatile::Replace_(CYContext
&context
, CYExpression
*type
) {
897 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("volatile"))));
900 CYExpression
*CYTypedIdentifier::Replace(CYContext
&context
) {
901 return modifier_
->Replace(context
, type_
);
904 CYArgument
*CYTypedParameter::Argument(CYContext
&context
) { $
T(NULL
)
905 return $
CYArgument(typed_
->Replace(context
), next_
->Argument(context
));
908 CYFunctionParameter
*CYTypedParameter::Parameters(CYContext
&context
) { $
T(NULL
)
909 return $
CYFunctionParameter($
CYDeclaration(typed_
->identifier_
?: context
.Unique()), next_
->Parameters(context
));
912 CYExpression
*CYTypedParameter::TypeSignature(CYContext
&context
, CYExpression
*prefix
) { $
T(prefix
)
913 return next_
->TypeSignature(context
, $
CYAdd(prefix
, typed_
->type_
->Replace(context
)));
916 CYStatement
*CYVar::Replace(CYContext
&context
) {
917 declarations_
->Replace(context
);
918 return $
E(declarations_
->Compound(context
));
921 CYExpression
*CYVariable::Replace(CYContext
&context
) {
922 context
.Replace(name_
);
926 CYStatement
*CYWhile::Replace(CYContext
&context
) {
927 context
.Replace(test_
);
928 context
.Replace(code_
);
932 CYStatement
*CYWith::Replace(CYContext
&context
) {
933 context
.Replace(scope_
);
934 context
.Replace(code_
);
938 CYExpression
*CYWord::ClassName(CYContext
&context
, bool object
) {
939 CYString
*name($
S(this));
941 return $
C1($
V("objc_getClass"), name
);