1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
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.
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/>.
23 #include "Replace.hpp"
27 CYFunctionExpression
*CYNonLocalize(CYContext
&context
, CYFunctionExpression
*function
) {
28 function
->nonlocal_
= context
.nextlocal_
;
32 static void CYImplicitReturn(CYStatement
*&code
) {
33 if (CYStatement
*&last
= CYGetLast(code
))
34 last
= last
->Return();
37 CYExpression
*CYAdd::Replace(CYContext
&context
) {
38 CYInfix::Replace(context
);
40 CYString
*lhs(dynamic_cast<CYString
*>(lhs_
));
41 CYString
*rhs(dynamic_cast<CYString
*>(rhs_
));
43 if (lhs
!= NULL
|| rhs
!= NULL
) {
45 lhs
= lhs_
->String(context
);
48 } else if (rhs
== NULL
) {
49 rhs
= rhs_
->String(context
);
54 return lhs
->Concat(context
, rhs
);
57 if (CYNumber
*lhn
= lhs_
->Number(context
))
58 if (CYNumber
*rhn
= rhs_
->Number(context
))
59 return $
D(lhn
->Value() + rhn
->Value());
64 CYExpression
*CYAddressOf::Replace(CYContext
&context
) {
65 return $
C0($
M(rhs_
, $
S("$cya")));
68 CYArgument
*CYArgument::Replace(CYContext
&context
) { $
T(NULL
)
69 context
.Replace(value_
);
70 next_
= next_
->Replace(context
);
82 CYExpression
*CYArray::Replace(CYContext
&context
) {
83 elements_
->Replace(context
);
87 CYExpression
*CYArrayComprehension::Replace(CYContext
&context
) {
88 CYVariable
*cyv($
V("$cyv"));
90 return $
C0($
F(NULL
, $
P1($
L("$cyv"), comprehensions_
->Parameters(context
)), $$
->*
91 $
E($
CYAssign(cyv
, $
CYArray()))->*
92 comprehensions_
->Replace(context
, $
E($
C1($
M(cyv
, $
S("push")), expression_
)))->*
97 CYExpression
*CYAssignment::Replace(CYContext
&context
) {
98 context
.Replace(lhs_
);
99 context
.Replace(rhs_
);
103 CYStatement
*CYBlock::Return() {
104 CYImplicitReturn(code_
);
108 CYStatement
*CYBlock::Replace(CYContext
&context
) {
109 context
.ReplaceAll(code_
);
115 CYStatement
*CYBreak::Replace(CYContext
&context
) {
119 CYExpression
*CYCall::AddArgument(CYContext
&context
, CYExpression
*value
) {
120 CYArgument
**argument(&arguments_
);
121 while (*argument
!= NULL
)
122 argument
= &(*argument
)->next_
;
123 *argument
= $
CYArgument(value
);
127 CYExpression
*CYCall::Replace(CYContext
&context
) {
128 context
.Replace(function_
);
129 arguments_
->Replace(context
);
136 void Catch::Replace(CYContext
&context
) { $
T()
137 CYScope
scope(true, context
);
139 context
.Replace(name_
);
140 context
.scope_
->Declare(context
, name_
, CYIdentifierCatch
);
142 context
.ReplaceAll(code_
);
143 scope
.Close(context
, code_
);
148 void CYClause::Replace(CYContext
&context
) { $
T()
149 context
.Replace(case_
);
150 context
.ReplaceAll(code_
);
151 next_
->Replace(context
);
154 CYExpression
*CYCompound::Replace(CYContext
&context
) {
155 context
.Replace(expression_
);
156 context
.Replace(next_
);
158 if (CYCompound
*compound
= dynamic_cast<CYCompound
*>(expression_
)) {
159 expression_
= compound
->expression_
;
160 compound
->expression_
= compound
->next_
;
161 compound
->next_
= next_
;
168 CYFunctionParameter
*CYCompound::Parameter() const {
169 CYFunctionParameter
*next(next_
->Parameter());
173 CYFunctionParameter
*parameter(expression_
->Parameter());
174 if (parameter
== NULL
)
177 parameter
->SetNext(next
);
181 CYFunctionParameter
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
182 CYFunctionParameter
*next(next_
->Parameters(context
));
183 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
184 parameter
->SetNext(next
);
190 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
191 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
194 CYExpression
*CYCondition::Replace(CYContext
&context
) {
195 context
.Replace(test_
);
196 context
.Replace(true_
);
197 context
.Replace(false_
);
201 void CYContext::NonLocal(CYStatement
*&statements
) {
202 CYContext
&context(*this);
204 if (nextlocal_
!= NULL
&& nextlocal_
->identifier_
!= NULL
) {
205 CYIdentifier
*cye($
I("$cye")->Replace(context
));
206 CYIdentifier
*unique(nextlocal_
->identifier_
->Replace(context
));
208 CYStatement
*declare(
209 $
CYVar($
L1($
CYDeclaration(unique
, $
CYObject()))));
211 cy::Syntax::Catch
*rescue(
212 $
cy::Syntax::Catch(cye
, $$
->*
213 $
CYIf($
CYIdentical($
M($
V(cye
), $
S("$cyk")), $
V(unique
)), $$
->*
214 $
CYReturn($
M($
V(cye
), $
S("$cyv"))))->*
215 $
cy::Syntax::Throw($
V(cye
))));
217 // XXX: I don't understand any of this
218 context
.Replace(declare
);
219 rescue
->Replace(context
);
223 $
cy::Syntax::Try(statements
, rescue
, NULL
);
227 CYIdentifier
*CYContext::Unique() {
228 return $
CYIdentifier($pool
.strcat("$cy", $pool
.itoa(unique_
++), NULL
));
231 CYStatement
*CYContinue::Replace(CYContext
&context
) {
235 CYStatement
*CYDebugger::Replace(CYContext
&context
) {
239 CYAssignment
*CYDeclaration::Assignment(CYContext
&context
) {
240 if (initialiser_
== NULL
)
243 CYAssignment
*value($
CYAssign(Variable(context
), initialiser_
));
248 CYVariable
*CYDeclaration::Variable(CYContext
&context
) {
249 return $
V(identifier_
);
252 CYStatement
*CYDeclaration::ForEachIn(CYContext
&context
, CYExpression
*value
) {
253 return $
CYVar($
L1($
CYDeclaration(identifier_
, value
)));
256 CYExpression
*CYDeclaration::Replace(CYContext
&context
) {
257 context
.Replace(identifier_
);
258 context
.scope_
->Declare(context
, identifier_
, CYIdentifierVariable
);
259 return Variable(context
);
262 void CYDeclarations::Replace(CYContext
&context
) { $
T()
263 declaration_
->Replace(context
);
264 next_
->Replace(context
);
267 CYProperty
*CYDeclarations::Property(CYContext
&context
) { $
T(NULL
)
268 return $
CYProperty(declaration_
->identifier_
, declaration_
->initialiser_
, next_
->Property(context
));
271 CYFunctionParameter
*CYDeclarations::Parameter(CYContext
&context
) { $
T(NULL
)
272 return $
CYFunctionParameter($
CYDeclaration(declaration_
->identifier_
), next_
->Parameter(context
));
275 CYArgument
*CYDeclarations::Argument(CYContext
&context
) { $
T(NULL
)
276 return $
CYArgument(declaration_
->initialiser_
, next_
->Argument(context
));
279 CYExpression
*CYDeclarations::Expression(CYContext
&context
) { $
T(NULL
)
280 CYExpression
*compound(next_
->Expression(context
));
281 if (CYAssignment
*assignment
= declaration_
->Assignment(context
))
282 if (compound
== NULL
)
283 compound
= assignment
;
285 compound
= $
CYCompound(assignment
, compound
);
289 CYExpression
*CYDirectMember::Replace(CYContext
&context
) {
290 context
.Replace(object_
);
291 context
.Replace(property_
);
295 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
296 context
.Replace(test_
);
297 context
.ReplaceAll(code_
);
301 void CYElement::Replace(CYContext
&context
) { $
T()
302 context
.Replace(value_
);
303 next_
->Replace(context
);
306 CYStatement
*CYEmpty::Replace(CYContext
&context
) {
310 CYExpression
*CYEncodedType::Replace(CYContext
&context
) {
311 return typed_
->Replace(context
);
314 CYStatement
*CYExpress::Return() {
315 return $
CYReturn(expression_
);
318 CYStatement
*CYExpress::Replace(CYContext
&context
) {
319 context
.Replace(expression_
);
323 CYExpression
*CYExpression::AddArgument(CYContext
&context
, CYExpression
*value
) {
324 return $
C1(this, value
);
327 CYExpression
*CYExpression::ClassName(CYContext
&context
, bool object
) {
331 CYStatement
*CYExpression::ForEachIn(CYContext
&context
, CYExpression
*value
) {
332 return $
E($
CYAssign(this, value
));
335 CYAssignment
*CYExpression::Assignment(CYContext
&context
) {
339 CYFunctionParameter
*CYExpression::Parameter() const {
343 CYStatement
*CYExternal::Replace(CYContext
&context
) {
344 return $
E($
CYAssign($
V(typed_
->identifier_
), $
C1(typed_
->Replace(context
), $
C2($
V("dlsym"), $
V("RTLD_DEFAULT"), $
S(typed_
->identifier_
->Word())))));
347 CYNumber
*CYFalse::Number(CYContext
&context
) {
351 CYString
*CYFalse::String(CYContext
&context
) {
355 CYExpression
*CYFatArrow::Replace(CYContext
&context
) {
356 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
357 function
->this_
.SetNext(context
.this_
);
361 void CYFinally::Replace(CYContext
&context
) { $
T()
362 context
.ReplaceAll(code_
);
365 CYStatement
*CYFor::Replace(CYContext
&context
) {
366 context
.Replace(initialiser_
);
367 context
.Replace(test_
);
368 context
.Replace(increment_
);
369 context
.ReplaceAll(code_
);
373 CYExpression
*CYForDeclarations::Replace(CYContext
&context
) {
374 declarations_
->Replace(context
);
375 return declarations_
->Expression(context
);
378 // XXX: this still feels highly suboptimal
379 CYStatement
*CYForIn::Replace(CYContext
&context
) {
380 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
381 return $
CYBlock($$
->*
386 context
.Replace(initialiser_
);
387 context
.Replace(set_
);
388 context
.ReplaceAll(code_
);
392 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
393 return $
CYFunctionParameter($
CYDeclaration(name_
));
396 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
397 return $
CYForIn($
V(name_
), set_
, CYComprehension::Replace(context
, statement
));
400 CYStatement
*CYForOf::Replace(CYContext
&context
) {
401 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
402 return $
CYBlock($$
->*
407 CYIdentifier
*cys($
I("$cys")), *cyt($
I("$cyt"));
409 return $
CYLetStatement($
L2($
CYDeclaration(cys
, set_
), $
CYDeclaration(cyt
)), $$
->*
410 $
CYForIn($
V(cyt
), $
V(cys
), $
CYBlock($$
->*
411 initialiser_
->ForEachIn(context
, $
M($
V(cys
), $
V(cyt
)))->*
417 CYFunctionParameter
*CYForOfComprehension::Parameter(CYContext
&context
) const {
418 return $
CYFunctionParameter($
CYDeclaration(name_
));
421 CYStatement
*CYForOfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
422 CYIdentifier
*cys($
I("$cys"));
424 return $
E($
C0($
F(NULL
, $
P1($
L("$cys")), $$
->*
425 $
E($
CYAssign($
V(cys
), set_
))->*
426 $
CYForIn($
V(name_
), $
V(cys
), $
CYBlock($$
->*
427 $
E($
CYAssign($
V(name_
), $
M($
V(cys
), $
V(name_
))))->*
428 CYComprehension::Replace(context
, statement
)
433 void CYFunction::Inject(CYContext
&context
) {
434 context
.Replace(name_
);
435 context
.scope_
->Declare(context
, name_
, CYIdentifierOther
);
438 void CYFunction::Replace_(CYContext
&context
, bool outer
) {
442 CYThisScope
*_this(context
.this_
);
443 context
.this_
= &this_
;
444 context
.this_
= CYGetLast(context
.this_
);
446 CYNonLocal
*nonlocal(context
.nonlocal_
);
447 CYNonLocal
*nextlocal(context
.nextlocal_
);
450 if (nonlocal_
!= NULL
) {
452 context
.nonlocal_
= nonlocal_
;
455 nonlocal_
= $
CYNonLocal();
456 context
.nextlocal_
= nonlocal_
;
459 CYScope
scope(!localize
, context
);
461 if (!outer
&& name_
!= NULL
)
464 parameters_
->Replace(context
, code_
);
465 context
.ReplaceAll(code_
);
468 CYImplicitReturn(code_
);
470 if (CYIdentifier
*identifier
= this_
.identifier_
)
472 $
CYVar($
L1($
CYDeclaration(identifier
, $
CYThis())))->*
476 context
.NonLocal(code_
);
478 context
.nextlocal_
= nextlocal
;
479 context
.nonlocal_
= nonlocal
;
481 context
.this_
= _this
;
483 scope
.Close(context
, code_
);
486 CYExpression
*CYFunctionExpression::Replace(CYContext
&context
) {
487 Replace_(context
, false);
491 void CYFunctionParameter::Replace(CYContext
&context
, CYStatement
*&statements
) { $
T()
492 CYAssignment
*assignment(initialiser_
->Assignment(context
));
493 context
.Replace(initialiser_
);
495 next_
->Replace(context
, statements
);
497 if (assignment
!= NULL
)
499 // XXX: this cast is quite incorrect
500 $
CYIf($
CYIdentical($
CYTypeOf(dynamic_cast<CYExpression
*>(initialiser_
)), $
S("undefined")), $$
->*
506 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
507 Replace_(context
, true);
511 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
) {
512 if (replace_
!= NULL
&& replace_
!= this)
513 return replace_
->Replace(context
);
514 replace_
= context
.scope_
->Lookup(context
, this);
518 CYStatement
*CYIf::Return() {
519 CYImplicitReturn(true_
);
520 CYImplicitReturn(false_
);
524 CYStatement
*CYIf::Replace(CYContext
&context
) {
525 context
.Replace(test_
);
526 context
.ReplaceAll(true_
);
527 context
.ReplaceAll(false_
);
531 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
535 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
536 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
539 CYStatement
*CYImport::Replace(CYContext
&context
) {
540 return $
CYVar($
L1($
L(module_
->part_
->Word(), $
C1($
V("require"), module_
->Replace(context
, "/")))));
543 CYExpression
*CYIndirect::Replace(CYContext
&context
) {
544 return $
M(rhs_
, $
S("$cyi"));
547 CYExpression
*CYIndirectMember::Replace(CYContext
&context
) {
548 return $
M($
CYIndirect(object_
), property_
);
551 CYExpression
*CYInfix::Replace(CYContext
&context
) {
552 context
.Replace(lhs_
);
553 context
.Replace(rhs_
);
557 CYStatement
*CYLabel::Replace(CYContext
&context
) {
558 context
.Replace(statement_
);
562 CYExpression
*CYLambda::Replace(CYContext
&context
) {
563 return $
N2($
V("Functor"), $
CYFunctionExpression(NULL
, parameters_
->Parameters(context
), code_
), parameters_
->TypeSignature(context
, typed_
->Replace(context
)));
566 CYStatement
*CYLetStatement::Replace(CYContext
&context
) {
567 return $
E($
CYCall(CYNonLocalize(context
, $
CYFunctionExpression(NULL
, declarations_
->Parameter(context
), code_
)), declarations_
->Argument(context
)));
570 CYString
*CYModule::Replace(CYContext
&context
, const char *separator
) const {
572 return $
CYString(part_
);
573 return $
CYString($pool
.strcat(next_
->Replace(context
, separator
)->Value(), separator
, part_
->Word(), NULL
));
576 CYExpression
*CYMultiply::Replace(CYContext
&context
) {
577 CYInfix::Replace(context
);
579 if (CYNumber
*lhn
= lhs_
->Number(context
))
580 if (CYNumber
*rhn
= rhs_
->Number(context
))
581 return $
D(lhn
->Value() * rhn
->Value());
589 CYExpression
*New::AddArgument(CYContext
&context
, CYExpression
*value
) {
590 CYSetLast(arguments_
) = $
CYArgument(value
);
594 CYExpression
*New::Replace(CYContext
&context
) {
595 context
.Replace(constructor_
);
596 arguments_
->Replace(context
);
602 CYNumber
*CYNull::Number(CYContext
&context
) {
606 CYString
*CYNull::String(CYContext
&context
) {
610 CYNumber
*CYNumber::Number(CYContext
&context
) {
614 CYString
*CYNumber::String(CYContext
&context
) {
615 // XXX: there is a precise algorithm for this
616 return $
S($pool
.sprintf(24, "%.17g", Value()));
619 CYExpression
*CYObject::Replace(CYContext
&context
) {
620 properties_
->Replace(context
);
624 CYExpression
*CYParenthetical::Replace(CYContext
&context
) {
628 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
629 context
.Replace(lhs_
);
633 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
634 context
.Replace(rhs_
);
638 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
639 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
640 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
643 struct IdentifierUsageLess
:
644 std::binary_function
<CYIdentifier
*, CYIdentifier
*, bool>
646 _finline
bool operator ()(CYIdentifier
*lhs
, CYIdentifier
*rhs
) const {
647 if (lhs
->usage_
!= rhs
->usage_
)
648 return lhs
->usage_
> rhs
->usage_
;
653 typedef std::set
<CYIdentifier
*, IdentifierUsageLess
> IdentifierUsages
;
656 void CYScript::Replace(CYContext
&context
) {
657 CYScope
scope(true, context
);
659 context
.nextlocal_
= $
CYNonLocal();
660 context
.ReplaceAll(code_
);
661 context
.NonLocal(code_
);
663 scope
.Close(context
, code_
);
667 CYCStringSet external
;
668 for (CYIdentifierValueSet::const_iterator
i(scope
.identifiers_
.begin()); i
!= scope
.identifiers_
.end(); ++i
)
669 external
.insert((*i
)->Word());
671 IdentifierUsages usages
;
673 if (offset
< context
.rename_
.size())
674 CYForEach (i
, context
.rename_
[offset
].identifier_
)
677 // XXX: totalling the probable occurrences and sorting by them would improve the result
678 for (CYIdentifierUsageVector::const_iterator
i(context
.rename_
.begin()); i
!= context
.rename_
.end(); ++i
, ++offset
) {
679 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
683 if (context
.options_
.verbose_
)
684 name
= $pool
.strcat("$", $pool
.itoa(offset
), NULL
);
690 unsigned position(7), local(offset
+ 1);
693 unsigned index(local
% (sizeof(MappingSet
) - 1));
694 local
/= sizeof(MappingSet
) - 1;
695 id
[--position
] = MappingSet
[index
];
696 } while (local
!= 0);
698 if (external
.find(id
+ position
) != external
.end()) {
703 name
= $pool
.strmemdup(id
+ position
, 7 - position
);
704 // XXX: at some point, this could become a keyword
707 CYForEach (identifier
, i
->identifier_
)
708 identifier
->Set(name
);
712 void CYProperty::Replace(CYContext
&context
) { $
T()
713 context
.Replace(value_
);
714 next_
->Replace(context
);
719 CYStatement
*CYReturn::Replace(CYContext
&context
) {
720 if (context
.nonlocal_
!= NULL
) {
721 CYProperty
*value(value_
== NULL
? NULL
: $
CYProperty($
S("$cyv"), value_
));
722 return $
cy::Syntax::Throw($
CYObject(
723 $
CYProperty($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
)
727 context
.Replace(value_
);
731 CYExpression
*CYRubyBlock::Replace(CYContext
&context
) {
732 return call_
->AddArgument(context
, proc_
->Replace(context
));
735 CYExpression
*CYRubyProc::Replace(CYContext
&context
) {
736 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
737 function
= CYNonLocalize(context
, function
);
738 function
->implicit_
= true;
742 CYScope::CYScope(bool transparent
, CYContext
&context
) :
743 transparent_(transparent
),
744 parent_(context
.scope_
)
746 context
.scope_
= this;
749 void CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierFlags flags
) {
750 if (!transparent_
|| flags
== CYIdentifierArgument
|| flags
== CYIdentifierCatch
)
751 internal_
.insert(CYIdentifierAddressFlagsMap::value_type(identifier
, flags
));
752 else if (parent_
!= NULL
)
753 parent_
->Declare(context
, identifier
, flags
);
756 CYIdentifier
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
757 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
758 return *insert
.first
;
761 void CYScope::Merge(CYContext
&context
, CYIdentifier
*identifier
) {
762 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
763 if (!insert
.second
) {
764 if ((*insert
.first
)->offset_
< identifier
->offset_
)
765 (*insert
.first
)->offset_
= identifier
->offset_
;
766 identifier
->replace_
= *insert
.first
;
767 (*insert
.first
)->usage_
+= identifier
->usage_
+ 1;
772 struct IdentifierOffset
{
774 CYIdentifierFlags flags_
;
776 CYIdentifier
*identifier_
;
778 IdentifierOffset(CYIdentifier
*identifier
, CYIdentifierFlags flags
) :
779 offset_(identifier
->offset_
),
781 usage_(identifier
->usage_
),
782 identifier_(identifier
)
787 struct IdentifierOffsetLess
:
788 std::binary_function
<const IdentifierOffset
&, const IdentifierOffset
&, bool>
790 _finline
bool operator ()(const IdentifierOffset
&lhs
, const IdentifierOffset
&rhs
) const {
791 if (lhs
.offset_
!= rhs
.offset_
)
792 return lhs
.offset_
< rhs
.offset_
;
793 if (lhs
.flags_
!= rhs
.flags_
)
794 return lhs
.flags_
< rhs
.flags_
;
795 /*if (lhs.usage_ != rhs.usage_)
796 return lhs.usage_ < rhs.usage_;*/
797 return lhs
.identifier_
< rhs
.identifier_
;
801 typedef std::set
<IdentifierOffset
, IdentifierOffsetLess
> IdentifierOffsets
;
804 void CYScope::Close(CYContext
&context
, CYStatement
*&statements
) {
805 context
.scope_
= parent_
;
810 CYDeclarations
*last(NULL
), *curr(NULL
);
812 IdentifierOffsets offsets
;
814 for (CYIdentifierAddressFlagsMap::const_iterator
i(internal_
.begin()); i
!= internal_
.end(); ++i
)
815 if (i
->second
!= CYIdentifierMagic
)
816 offsets
.insert(IdentifierOffset(i
->first
, i
->second
));
820 for (IdentifierOffsets::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
821 if (i
->flags_
== CYIdentifierVariable
) {
822 CYDeclarations
*next($
CYDeclarations($
CYDeclaration(i
->identifier_
)));
830 if (offset
< i
->offset_
)
832 if (context
.rename_
.size() <= offset
)
833 context
.rename_
.resize(offset
+ 1);
835 CYIdentifierUsage
&rename(context
.rename_
[offset
++]);
836 i
->identifier_
->SetNext(rename
.identifier_
);
837 rename
.identifier_
= i
->identifier_
;
838 rename
.usage_
+= i
->identifier_
->usage_
+ 1;
842 CYVar
*var($
CYVar(last
));
843 var
->SetNext(statements
);
847 for (CYIdentifierValueSet::const_iterator
i(identifiers_
.begin()); i
!= identifiers_
.end(); ++i
)
848 if (internal_
.find(*i
) == internal_
.end()) {
849 //std::cout << *i << '=' << offset << std::endl;
850 if ((*i
)->offset_
< offset
)
851 (*i
)->offset_
= offset
;
852 parent_
->Merge(context
, *i
);
856 CYElement
*CYSpan::Replace(CYContext
&context
) { $
T(NULL
)
857 return $
CYElement(expression_
, $
CYElement(string_
, next_
->Replace(context
)));
860 CYStatement
*CYStatement::Return() {
864 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
865 size_t size(size_
+ rhs
->size_
);
866 char *value($
char[size
+ 1]);
867 memcpy(value
, value_
, size_
);
868 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
870 return $
S(value
, size
);
873 CYNumber
*CYString::Number(CYContext
&context
) {
874 // XXX: there is a precise algorithm for this
878 CYString
*CYString::String(CYContext
&context
) {
882 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
883 context
.Replace(value_
);
884 clauses_
->Replace(context
);
888 CYExpression
*CYTemplate::Replace(CYContext
&context
) {
889 return $
C2($
M($
M($
M($
V("String"), $
S("prototype")), $
S("concat")), $
S("apply")), $
S(""), $
CYArray($
CYElement(string_
, spans_
->Replace(context
))));
892 CYExpression
*CYThis::Replace(CYContext
&context
) {
893 if (context
.this_
!= NULL
)
894 return $
V(context
.this_
->Identifier(context
));
901 CYStatement
*Throw::Replace(CYContext
&context
) {
902 context
.Replace(value_
);
908 CYExpression
*CYTrivial::Replace(CYContext
&context
) {
912 CYNumber
*CYTrue::Number(CYContext
&context
) {
916 CYString
*CYTrue::String(CYContext
&context
) {
923 CYStatement
*Try::Replace(CYContext
&context
) {
924 context
.ReplaceAll(code_
);
925 catch_
->Replace(context
);
926 finally_
->Replace(context
);
932 CYExpression
*CYTypeArrayOf::Replace_(CYContext
&context
, CYExpression
*type
) {
933 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("arrayOf")), $
CYArgument(size_
)));
936 CYExpression
*CYTypeBlockWith::Replace_(CYContext
&context
, CYExpression
*type
) {
937 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("blockWith")), parameters_
->Argument(context
)));
940 CYExpression
*CYTypeConstant::Replace_(CYContext
&context
, CYExpression
*type
) {
941 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("constant"))));
944 CYStatement
*CYTypeDefinition::Replace(CYContext
&context
) {
945 return $
E($
CYAssign($
V(typed_
->identifier_
), typed_
->Replace(context
)));
948 CYExpression
*CYTypeError::Replace(CYContext
&context
) {
953 CYExpression
*CYTypeModifier::Replace(CYContext
&context
, CYExpression
*type
) { $
T(type
)
954 return Replace_(context
, type
);
957 CYExpression
*CYTypeFunctionWith::Replace_(CYContext
&context
, CYExpression
*type
) {
958 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("functionWith")), parameters_
->Argument(context
)));
961 CYExpression
*CYTypeLong::Replace(CYContext
&context
) {
962 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("long")));
965 CYExpression
*CYTypePointerTo::Replace_(CYContext
&context
, CYExpression
*type
) {
966 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("pointerTo"))));
969 CYExpression
*CYTypeShort::Replace(CYContext
&context
) {
970 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("short")));
973 CYExpression
*CYTypeSigned::Replace(CYContext
&context
) {
974 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("signed")));
977 CYExpression
*CYTypeUnsigned::Replace(CYContext
&context
) {
978 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("unsigned")));
981 CYExpression
*CYTypeVariable::Replace(CYContext
&context
) {
985 CYExpression
*CYTypeVoid::Replace(CYContext
&context
) {
986 return $
N1($
V("Type"), $
CYString("v"));
989 CYExpression
*CYTypeVolatile::Replace_(CYContext
&context
, CYExpression
*type
) {
990 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("volatile"))));
993 CYExpression
*CYTypedIdentifier::Replace(CYContext
&context
) {
994 return modifier_
->Replace(context
, specifier_
->Replace(context
));
997 CYTypeFunctionWith
*CYTypedIdentifier::Function() {
998 CYTypeModifier
**modifier(&modifier_
);
999 if (*modifier
== NULL
)
1001 while ((*modifier
)->next_
!= NULL
)
1002 modifier
= &(*modifier
)->next_
;
1003 CYTypeFunctionWith
*function((*modifier
)->Function());
1004 if (function
== NULL
)
1010 CYArgument
*CYTypedParameter::Argument(CYContext
&context
) { $
T(NULL
)
1011 return $
CYArgument(typed_
->Replace(context
), next_
->Argument(context
));
1014 CYFunctionParameter
*CYTypedParameter::Parameters(CYContext
&context
) { $
T(NULL
)
1015 return $
CYFunctionParameter($
CYDeclaration(typed_
->identifier_
?: context
.Unique()), next_
->Parameters(context
));
1018 CYExpression
*CYTypedParameter::TypeSignature(CYContext
&context
, CYExpression
*prefix
) { $
T(prefix
)
1019 return next_
->TypeSignature(context
, $
CYAdd(prefix
, typed_
->Replace(context
)));
1022 CYStatement
*CYVar::Replace(CYContext
&context
) {
1023 declarations_
->Replace(context
);
1024 if (CYExpression
*expression
= declarations_
->Expression(context
))
1025 return $
E(expression
);
1029 CYExpression
*CYVariable::Replace(CYContext
&context
) {
1030 context
.Replace(name_
);
1034 CYFunctionParameter
*CYVariable::Parameter() const {
1035 return $
CYFunctionParameter($
CYDeclaration(name_
));
1038 CYStatement
*CYWhile::Replace(CYContext
&context
) {
1039 context
.Replace(test_
);
1040 context
.ReplaceAll(code_
);
1044 CYStatement
*CYWith::Replace(CYContext
&context
) {
1045 context
.Replace(scope_
);
1046 context
.ReplaceAll(code_
);
1050 CYExpression
*CYWord::ClassName(CYContext
&context
, bool object
) {
1051 CYString
*name($
S(this));
1053 return $
C1($
V("objc_getClass"), name
);