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 CYExpression
*CYAdd::Replace(CYContext
&context
) {
33 CYInfix::Replace(context
);
35 CYString
*lhs(dynamic_cast<CYString
*>(lhs_
));
36 CYString
*rhs(dynamic_cast<CYString
*>(rhs_
));
38 if (lhs
!= NULL
|| rhs
!= NULL
) {
40 lhs
= lhs_
->String(context
);
43 } else if (rhs
== NULL
) {
44 rhs
= rhs_
->String(context
);
49 return lhs
->Concat(context
, rhs
);
52 if (CYNumber
*lhn
= lhs_
->Number(context
))
53 if (CYNumber
*rhn
= rhs_
->Number(context
))
54 return $
D(lhn
->Value() + rhn
->Value());
59 CYExpression
*CYAddressOf::Replace(CYContext
&context
) {
60 return $
C0($
M(rhs_
, $
S("$cya")));
63 CYArgument
*CYArgument::Replace(CYContext
&context
) { $
T(NULL
)
64 context
.Replace(value_
);
65 next_
= next_
->Replace(context
);
77 CYExpression
*CYArray::Replace(CYContext
&context
) {
78 elements_
->Replace(context
);
82 CYExpression
*CYArrayComprehension::Replace(CYContext
&context
) {
83 CYVariable
*cyv($
V("$cyv"));
85 return $
C0($
F(NULL
, $
P1($
L("$cyv"), comprehensions_
->Parameters(context
)), $$
->*
86 $
E($
CYAssign(cyv
, $
CYArray()))->*
87 comprehensions_
->Replace(context
, $
E($
C1($
M(cyv
, $
S("push")), expression_
)))->*
92 CYExpression
*CYAssignment::Replace(CYContext
&context
) {
93 context
.Replace(lhs_
);
94 context
.Replace(rhs_
);
98 CYStatement
*CYBlock::Replace(CYContext
&context
) {
99 context
.ReplaceAll(statements_
);
100 if (statements_
== NULL
)
105 CYStatement
*CYBreak::Replace(CYContext
&context
) {
109 CYExpression
*CYCall::AddArgument(CYContext
&context
, CYExpression
*value
) {
110 CYArgument
**argument(&arguments_
);
111 while (*argument
!= NULL
)
112 argument
= &(*argument
)->next_
;
113 *argument
= $
CYArgument(value
);
117 CYExpression
*CYCall::Replace(CYContext
&context
) {
118 context
.Replace(function_
);
119 arguments_
->Replace(context
);
126 void Catch::Replace(CYContext
&context
) { $
T()
127 CYScope
scope(true, context
, code_
.statements_
);
129 context
.Replace(name_
);
130 context
.scope_
->Declare(context
, name_
, CYIdentifierCatch
);
132 code_
.Replace(context
);
138 void CYClause::Replace(CYContext
&context
) { $
T()
139 context
.Replace(case_
);
140 context
.ReplaceAll(statements_
);
141 next_
->Replace(context
);
144 CYStatement
*CYComment::Replace(CYContext
&context
) {
148 CYExpression
*CYCompound::Replace(CYContext
&context
) {
152 context
.Replace(expression_
);
153 context
.Replace(next_
);
155 if (CYCompound
*compound
= dynamic_cast<CYCompound
*>(expression_
)) {
156 expression_
= compound
->expression_
;
157 compound
->expression_
= compound
->next_
;
158 compound
->next_
= next_
;
165 CYExpression
*CYCompound::Primitive(CYContext
&context
) {
166 CYExpression
*expression(expression_
);
167 if (expression
== NULL
|| next_
!= NULL
)
169 return expression
->Primitive(context
);
172 CYFunctionParameter
*CYCompound::Parameters() const {
173 CYFunctionParameter
*next
;
177 next
= next_
->Parameters();
182 CYFunctionParameter
*parameter(expression_
->Parameter());
183 if (parameter
== NULL
)
186 parameter
->SetNext(next
);
190 CYFunctionParameter
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
191 CYFunctionParameter
*next(next_
->Parameters(context
));
192 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
193 parameter
->SetNext(next
);
199 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
200 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
203 CYExpression
*CYCondition::Replace(CYContext
&context
) {
204 context
.Replace(test_
);
205 context
.Replace(true_
);
206 context
.Replace(false_
);
210 void CYContext::NonLocal(CYStatement
*&statements
) {
211 CYContext
&context(*this);
213 if (nextlocal_
!= NULL
&& nextlocal_
->identifier_
!= NULL
) {
214 CYIdentifier
*cye($
I("$cye")->Replace(context
));
215 CYIdentifier
*unique(nextlocal_
->identifier_
->Replace(context
));
217 CYStatement
*declare(
218 $
CYVar($
L1($
CYDeclaration(unique
, $
CYObject()))));
220 cy::Syntax::Catch
*rescue(
221 $
cy::Syntax::Catch(cye
, $$
->*
222 $
CYIf($
CYIdentical($
M($
V(cye
), $
S("$cyk")), $
V(unique
)), $$
->*
223 $
CYReturn($
M($
V(cye
), $
S("$cyv"))))->*
224 $
cy::Syntax::Throw($
V(cye
))));
226 context
.Replace(declare
);
227 rescue
->Replace(context
);
231 $
cy::Syntax::Try(statements
, rescue
, NULL
);
235 CYIdentifier
*CYContext::Unique() {
236 return $
CYIdentifier($pool
.strcat("$cy", $pool
.itoa(unique_
++), NULL
));
239 CYStatement
*CYContinue::Replace(CYContext
&context
) {
243 CYStatement
*CYDebugger::Replace(CYContext
&context
) {
247 CYAssignment
*CYDeclaration::Assignment(CYContext
&context
) {
248 if (initialiser_
== NULL
)
251 CYAssignment
*value($
CYAssign(Variable(context
), initialiser_
));
256 CYVariable
*CYDeclaration::Variable(CYContext
&context
) {
257 return $
V(identifier_
);
260 CYStatement
*CYDeclaration::ForEachIn(CYContext
&context
, CYExpression
*value
) {
261 return $
CYVar($
L1($
CYDeclaration(identifier_
, value
)));
264 CYExpression
*CYDeclaration::Replace(CYContext
&context
) {
265 context
.Replace(identifier_
);
266 context
.scope_
->Declare(context
, identifier_
, CYIdentifierVariable
);
267 return Variable(context
);
270 void CYDeclarations::Replace(CYContext
&context
) { $
T()
271 declaration_
->Replace(context
);
272 next_
->Replace(context
);
275 CYProperty
*CYDeclarations::Property(CYContext
&context
) { $
T(NULL
)
276 return $
CYProperty(declaration_
->identifier_
, declaration_
->initialiser_
, next_
->Property(context
));
279 CYFunctionParameter
*CYDeclarations::Parameter(CYContext
&context
) { $
T(NULL
)
280 return $
CYFunctionParameter($
CYDeclaration(declaration_
->identifier_
), next_
->Parameter(context
));
283 CYArgument
*CYDeclarations::Argument(CYContext
&context
) { $
T(NULL
)
284 return $
CYArgument(declaration_
->initialiser_
, next_
->Argument(context
));
287 CYCompound
*CYDeclarations::Compound(CYContext
&context
) { $
T(NULL
)
288 CYCompound
*compound(next_
->Compound(context
));
289 if (CYAssignment
*assignment
= declaration_
->Assignment(context
))
290 compound
= $
CYCompound(assignment
, compound
);
294 CYExpression
*CYDirectMember::Replace(CYContext
&context
) {
295 context
.Replace(object_
);
296 context
.Replace(property_
);
300 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
301 context
.Replace(test_
);
302 context
.Replace(code_
);
306 void CYElement::Replace(CYContext
&context
) { $
T()
307 context
.Replace(value_
);
308 next_
->Replace(context
);
311 CYStatement
*CYEmpty::Replace(CYContext
&context
) {
315 CYExpression
*CYEncodedType::Replace(CYContext
&context
) {
316 return typed_
->Replace(context
);
319 CYStatement
*CYExpress::Replace(CYContext
&context
) {
320 while (CYExpress
*express
= dynamic_cast<CYExpress
*>(next_
)) {
321 expression_
= $
CYCompound(expression_
, express
->expression_
);
322 SetNext(express
->next_
);
325 context
.Replace(expression_
);
326 if (expression_
== NULL
)
332 CYExpression
*CYExpression::AddArgument(CYContext
&context
, CYExpression
*value
) {
333 return $
C1(this, value
);
336 CYExpression
*CYExpression::ClassName(CYContext
&context
, bool object
) {
340 CYStatement
*CYExpression::ForEachIn(CYContext
&context
, CYExpression
*value
) {
341 return $
E($
CYAssign(this, value
));
344 CYAssignment
*CYExpression::Assignment(CYContext
&context
) {
348 CYFunctionParameter
*CYExpression::Parameter() const {
352 CYFunctionParameter
*CYExpression::Parameters() const {
356 CYStatement
*CYExternal::Replace(CYContext
&context
) {
357 return $
E($
CYAssign($
V(typed_
->identifier_
), $
C1(typed_
->Replace(context
), $
C2($
V("dlsym"), $
V("RTLD_DEFAULT"), $
S(typed_
->identifier_
->Word())))));
360 CYNumber
*CYFalse::Number(CYContext
&context
) {
364 CYString
*CYFalse::String(CYContext
&context
) {
368 CYExpression
*CYFatArrow::Replace(CYContext
&context
) {
369 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
370 function
->this_
.SetNext(context
.this_
);
374 void CYFinally::Replace(CYContext
&context
) { $
T()
375 code_
.Replace(context
);
378 CYStatement
*CYFor::Replace(CYContext
&context
) {
379 context
.Replace(initialiser_
);
380 context
.Replace(test_
);
381 context
.Replace(increment_
);
382 context
.Replace(code_
);
386 CYCompound
*CYForDeclarations::Replace(CYContext
&context
) {
387 declarations_
->Replace(context
);
388 return declarations_
->Compound(context
);
391 // XXX: this still feels highly suboptimal
392 CYStatement
*CYForIn::Replace(CYContext
&context
) {
393 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
394 return $
CYBlock($$
->*
399 context
.Replace(initialiser_
);
400 context
.Replace(set_
);
401 context
.Replace(code_
);
405 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
406 return $
CYFunctionParameter($
CYDeclaration(name_
));
409 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
410 return $
CYForIn($
V(name_
), set_
, CYComprehension::Replace(context
, statement
));
413 CYStatement
*CYForOf::Replace(CYContext
&context
) {
414 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
415 return $
CYBlock($$
->*
420 CYIdentifier
*cys($
I("$cys")), *cyt($
I("$cyt"));
422 return $
CYLetStatement($
L2($
CYDeclaration(cys
, set_
), $
CYDeclaration(cyt
)), $$
->*
423 $
CYForIn($
V(cyt
), $
V(cys
), $
CYBlock($$
->*
424 initialiser_
->ForEachIn(context
, $
M($
V(cys
), $
V(cyt
)))->*
430 CYFunctionParameter
*CYForOfComprehension::Parameter(CYContext
&context
) const {
431 return $
CYFunctionParameter($
CYDeclaration(name_
));
434 CYStatement
*CYForOfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
435 CYIdentifier
*cys($
I("$cys"));
437 return $
E($
C0($
F(NULL
, $
P1($
L("$cys")), $$
->*
438 $
E($
CYAssign($
V(cys
), set_
))->*
439 $
CYForIn($
V(name_
), $
V(cys
), $
CYBlock($$
->*
440 $
E($
CYAssign($
V(name_
), $
M($
V(cys
), $
V(name_
))))->*
441 CYComprehension::Replace(context
, statement
)
446 void CYFunction::Inject(CYContext
&context
) {
447 context
.Replace(name_
);
448 context
.scope_
->Declare(context
, name_
, CYIdentifierOther
);
451 void CYFunction::Replace_(CYContext
&context
, bool outer
) {
455 CYThisScope
*_this(context
.this_
);
456 context
.this_
= CYGetLast(&this_
);
458 CYNonLocal
*nonlocal(context
.nonlocal_
);
459 CYNonLocal
*nextlocal(context
.nextlocal_
);
462 if (nonlocal_
!= NULL
) {
464 context
.nonlocal_
= nonlocal_
;
467 nonlocal_
= $
CYNonLocal();
468 context
.nextlocal_
= nonlocal_
;
471 CYScope
scope(!localize
, context
, code_
.statements_
);
473 if (!outer
&& name_
!= NULL
)
476 parameters_
->Replace(context
, code_
);
477 code_
.Replace(context
);
479 if (CYIdentifier
*identifier
= this_
.identifier_
)
480 code_
.statements_
= $$
->*
481 $
CYVar($
L1($
CYDeclaration(identifier
, $
CYThis())))->*
485 context
.NonLocal(code_
.statements_
);
487 context
.nextlocal_
= nextlocal
;
488 context
.nonlocal_
= nonlocal
;
490 context
.this_
= _this
;
495 CYExpression
*CYFunctionExpression::Replace(CYContext
&context
) {
496 Replace_(context
, false);
500 void CYFunctionParameter::Replace(CYContext
&context
, CYBlock
&code
) { $
T()
501 CYAssignment
*assignment(initialiser_
->Assignment(context
));
502 context
.Replace(initialiser_
);
504 next_
->Replace(context
, code
);
506 if (assignment
!= NULL
)
507 // XXX: this cast is quite incorrect
508 code
.AddPrev($
CYIf($
CYIdentical($
CYTypeOf(dynamic_cast<CYExpression
*>(initialiser_
)), $
S("undefined")), $$
->*
513 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
514 Replace_(context
, true);
518 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
) {
519 if (replace_
!= NULL
&& replace_
!= this)
520 return replace_
->Replace(context
);
521 replace_
= context
.scope_
->Lookup(context
, this);
525 CYStatement
*CYIf::Replace(CYContext
&context
) {
526 context
.Replace(test_
);
527 context
.Replace(true_
);
528 context
.Replace(false_
);
532 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
536 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
537 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
540 CYStatement
*CYImport::Replace(CYContext
&context
) {
541 return $
CYVar($
L1($
L(module_
->part_
->Word(), $
C1($
V("require"), module_
->Replace(context
, "/")))));
544 CYExpression
*CYIndirect::Replace(CYContext
&context
) {
545 return $
M(rhs_
, $
S("$cyi"));
548 CYExpression
*CYIndirectMember::Replace(CYContext
&context
) {
549 return $
M($
CYIndirect(object_
), property_
);
552 CYExpression
*CYInfix::Replace(CYContext
&context
) {
553 context
.Replace(lhs_
);
554 context
.Replace(rhs_
);
558 CYStatement
*CYLabel::Replace(CYContext
&context
) {
559 context
.Replace(statement_
);
563 CYExpression
*CYLambda::Replace(CYContext
&context
) {
564 return $
N2($
V("Functor"), $
CYFunctionExpression(NULL
, parameters_
->Parameters(context
), statements_
), parameters_
->TypeSignature(context
, typed_
->Replace(context
)));
567 CYStatement
*CYLetStatement::Replace(CYContext
&context
) {
568 return $
E($
CYCall(CYNonLocalize(context
, $
CYFunctionExpression(NULL
, declarations_
->Parameter(context
), code_
)), declarations_
->Argument(context
)));
571 CYString
*CYModule::Replace(CYContext
&context
, const char *separator
) const {
573 return $
CYString(part_
);
574 return $
CYString($pool
.strcat(next_
->Replace(context
, separator
)->Value(), separator
, part_
->Word(), NULL
));
577 CYExpression
*CYMultiply::Replace(CYContext
&context
) {
578 CYInfix::Replace(context
);
580 if (CYNumber
*lhn
= lhs_
->Number(context
))
581 if (CYNumber
*rhn
= rhs_
->Number(context
))
582 return $
D(lhn
->Value() * rhn
->Value());
590 CYExpression
*New::AddArgument(CYContext
&context
, CYExpression
*value
) {
591 CYSetLast(arguments_
) = $
CYArgument(value
);
595 CYExpression
*New::Replace(CYContext
&context
) {
596 context
.Replace(constructor_
);
597 arguments_
->Replace(context
);
603 CYNumber
*CYNull::Number(CYContext
&context
) {
607 CYString
*CYNull::String(CYContext
&context
) {
611 CYNumber
*CYNumber::Number(CYContext
&context
) {
615 CYString
*CYNumber::String(CYContext
&context
) {
616 // XXX: there is a precise algorithm for this
617 return $
S($pool
.sprintf(24, "%.17g", Value()));
620 CYExpression
*CYObject::Replace(CYContext
&context
) {
621 properties_
->Replace(context
);
625 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
626 context
.Replace(lhs_
);
630 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
631 context
.Replace(rhs_
);
635 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
636 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
637 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
640 struct IdentifierUsageLess
:
641 std::binary_function
<CYIdentifier
*, CYIdentifier
*, bool>
643 _finline
bool operator ()(CYIdentifier
*lhs
, CYIdentifier
*rhs
) const {
644 if (lhs
->usage_
!= rhs
->usage_
)
645 return lhs
->usage_
> rhs
->usage_
;
650 typedef std::set
<CYIdentifier
*, IdentifierUsageLess
> IdentifierUsages
;
653 void CYProgram::Replace(CYContext
&context
) {
654 CYScope
scope(true, context
, statements_
);
656 context
.nextlocal_
= $
CYNonLocal();
657 context
.ReplaceAll(statements_
);
658 context
.NonLocal(statements_
);
664 CYCStringSet external
;
665 for (CYIdentifierValueSet::const_iterator
i(scope
.identifiers_
.begin()); i
!= scope
.identifiers_
.end(); ++i
)
666 external
.insert((*i
)->Word());
668 IdentifierUsages usages
;
670 if (offset
< context
.rename_
.size())
671 CYForEach (i
, context
.rename_
[offset
].identifier_
)
674 // XXX: totalling the probable occurrences and sorting by them would improve the result
675 for (CYIdentifierUsageVector::const_iterator
i(context
.rename_
.begin()); i
!= context
.rename_
.end(); ++i
, ++offset
) {
676 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
680 if (context
.options_
.verbose_
)
681 name
= $pool
.strcat("$", $pool
.itoa(offset
), NULL
);
687 unsigned position(7), local(offset
+ 1);
690 unsigned index(local
% (sizeof(MappingSet
) - 1));
691 local
/= sizeof(MappingSet
) - 1;
692 id
[--position
] = MappingSet
[index
];
693 } while (local
!= 0);
695 if (external
.find(id
+ position
) != external
.end()) {
700 name
= $pool
.strmemdup(id
+ position
, 7 - position
);
701 // XXX: at some point, this could become a keyword
704 CYForEach (identifier
, i
->identifier_
)
705 identifier
->Set(name
);
709 void CYProperty::Replace(CYContext
&context
) { $
T()
710 context
.Replace(value_
);
711 next_
->Replace(context
);
716 CYStatement
*CYReturn::Replace(CYContext
&context
) {
717 if (context
.nonlocal_
!= NULL
) {
718 CYProperty
*value(value_
== NULL
? NULL
: $
CYProperty($
S("$cyv"), value_
));
719 return $
cy::Syntax::Throw($
CYObject(
720 $
CYProperty($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
)
724 context
.Replace(value_
);
728 CYExpression
*CYRubyBlock::Replace(CYContext
&context
) {
729 // XXX: this needs to do something much more epic to handle return
730 return call_
->AddArgument(context
, proc_
->Replace(context
));
733 CYExpression
*CYRubyProc::Replace(CYContext
&context
) {
734 return CYNonLocalize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
));
737 CYScope::CYScope(bool transparent
, CYContext
&context
, CYStatement
*&statements
) :
738 transparent_(transparent
),
740 statements_(statements
),
741 parent_(context
.scope_
)
743 context_
.scope_
= this;
746 CYScope::~CYScope() {
749 void CYScope::Close() {
750 context_
.scope_
= parent_
;
751 Scope(context_
, statements_
);
754 void CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierFlags flags
) {
755 if (!transparent_
|| flags
== CYIdentifierArgument
|| flags
== CYIdentifierCatch
)
756 internal_
.insert(CYIdentifierAddressFlagsMap::value_type(identifier
, flags
));
757 else if (parent_
!= NULL
)
758 parent_
->Declare(context
, identifier
, flags
);
761 CYIdentifier
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
762 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
763 return *insert
.first
;
766 void CYScope::Merge(CYContext
&context
, CYIdentifier
*identifier
) {
767 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
768 if (!insert
.second
) {
769 if ((*insert
.first
)->offset_
< identifier
->offset_
)
770 (*insert
.first
)->offset_
= identifier
->offset_
;
771 identifier
->replace_
= *insert
.first
;
772 (*insert
.first
)->usage_
+= identifier
->usage_
+ 1;
777 struct IdentifierOffset
{
779 CYIdentifierFlags flags_
;
781 CYIdentifier
*identifier_
;
783 IdentifierOffset(CYIdentifier
*identifier
, CYIdentifierFlags flags
) :
784 offset_(identifier
->offset_
),
786 usage_(identifier
->usage_
),
787 identifier_(identifier
)
792 struct IdentifierOffsetLess
:
793 std::binary_function
<const IdentifierOffset
&, const IdentifierOffset
&, bool>
795 _finline
bool operator ()(const IdentifierOffset
&lhs
, const IdentifierOffset
&rhs
) const {
796 if (lhs
.offset_
!= rhs
.offset_
)
797 return lhs
.offset_
< rhs
.offset_
;
798 if (lhs
.flags_
!= rhs
.flags_
)
799 return lhs
.flags_
< rhs
.flags_
;
800 /*if (lhs.usage_ != rhs.usage_)
801 return lhs.usage_ < rhs.usage_;*/
802 return lhs
.identifier_
< rhs
.identifier_
;
806 typedef std::set
<IdentifierOffset
, IdentifierOffsetLess
> IdentifierOffsets
;
809 void CYScope::Scope(CYContext
&context
, CYStatement
*&statements
) {
813 CYDeclarations
*last(NULL
), *curr(NULL
);
815 IdentifierOffsets offsets
;
817 for (CYIdentifierAddressFlagsMap::const_iterator
i(internal_
.begin()); i
!= internal_
.end(); ++i
)
818 if (i
->second
!= CYIdentifierMagic
)
819 offsets
.insert(IdentifierOffset(i
->first
, i
->second
));
823 for (IdentifierOffsets::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
824 if (i
->flags_
== CYIdentifierVariable
) {
825 CYDeclarations
*next($
CYDeclarations($
CYDeclaration(i
->identifier_
)));
833 if (offset
< i
->offset_
)
835 if (context
.rename_
.size() <= offset
)
836 context
.rename_
.resize(offset
+ 1);
838 CYIdentifierUsage
&rename(context
.rename_
[offset
++]);
839 i
->identifier_
->SetNext(rename
.identifier_
);
840 rename
.identifier_
= i
->identifier_
;
841 rename
.usage_
+= i
->identifier_
->usage_
+ 1;
845 CYVar
*var($
CYVar(last
));
846 var
->SetNext(statements
);
850 for (CYIdentifierValueSet::const_iterator
i(identifiers_
.begin()); i
!= identifiers_
.end(); ++i
)
851 if (internal_
.find(*i
) == internal_
.end()) {
852 //std::cout << *i << '=' << offset << std::endl;
853 if ((*i
)->offset_
< offset
)
854 (*i
)->offset_
= offset
;
855 parent_
->Merge(context
, *i
);
859 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
860 size_t size(size_
+ rhs
->size_
);
861 char *value($
char[size
+ 1]);
862 memcpy(value
, value_
, size_
);
863 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
865 return $
S(value
, size
);
868 CYNumber
*CYString::Number(CYContext
&context
) {
869 // XXX: there is a precise algorithm for this
873 CYString
*CYString::String(CYContext
&context
) {
877 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
878 context
.Replace(value_
);
879 clauses_
->Replace(context
);
883 CYExpression
*CYThis::Replace(CYContext
&context
) {
884 if (context
.this_
!= NULL
)
885 return $
V(context
.this_
->Identifier(context
));
892 CYStatement
*Throw::Replace(CYContext
&context
) {
893 context
.Replace(value_
);
899 CYExpression
*CYTrivial::Replace(CYContext
&context
) {
903 CYNumber
*CYTrue::Number(CYContext
&context
) {
907 CYString
*CYTrue::String(CYContext
&context
) {
914 CYStatement
*Try::Replace(CYContext
&context
) {
915 code_
.Replace(context
);
916 catch_
->Replace(context
);
917 finally_
->Replace(context
);
923 CYExpression
*CYTypeArrayOf::Replace_(CYContext
&context
, CYExpression
*type
) {
924 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("arrayOf")), $
CYArgument(size_
)));
927 CYExpression
*CYTypeBlockWith::Replace_(CYContext
&context
, CYExpression
*type
) {
928 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("blockWith")), parameters_
->Argument(context
)));
931 CYExpression
*CYTypeConstant::Replace_(CYContext
&context
, CYExpression
*type
) {
932 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("constant"))));
935 CYStatement
*CYTypeDefinition::Replace(CYContext
&context
) {
936 return $
E($
CYAssign($
V(typed_
->identifier_
), typed_
->Replace(context
)));
939 CYExpression
*CYTypeError::Replace(CYContext
&context
) {
944 CYExpression
*CYTypeModifier::Replace(CYContext
&context
, CYExpression
*type
) { $
T(type
)
945 return Replace_(context
, type
);
948 CYExpression
*CYTypeFunctionWith::Replace_(CYContext
&context
, CYExpression
*type
) {
949 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("functionWith")), parameters_
->Argument(context
)));
952 CYExpression
*CYTypeLong::Replace(CYContext
&context
) {
953 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("long")));
956 CYExpression
*CYTypePointerTo::Replace_(CYContext
&context
, CYExpression
*type
) {
957 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("pointerTo"))));
960 CYExpression
*CYTypeShort::Replace(CYContext
&context
) {
961 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("short")));
964 CYExpression
*CYTypeSigned::Replace(CYContext
&context
) {
965 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("signed")));
968 CYExpression
*CYTypeUnsigned::Replace(CYContext
&context
) {
969 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("unsigned")));
972 CYExpression
*CYTypeVariable::Replace(CYContext
&context
) {
976 CYExpression
*CYTypeVoid::Replace(CYContext
&context
) {
977 return $
N1($
V("Type"), $
CYString("v"));
980 CYExpression
*CYTypeVolatile::Replace_(CYContext
&context
, CYExpression
*type
) {
981 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("volatile"))));
984 CYExpression
*CYTypedIdentifier::Replace(CYContext
&context
) {
985 return modifier_
->Replace(context
, specifier_
->Replace(context
));
988 CYTypeFunctionWith
*CYTypedIdentifier::Function() {
989 CYTypeModifier
**modifier(&modifier_
);
990 if (*modifier
== NULL
)
992 while ((*modifier
)->next_
!= NULL
)
993 modifier
= &(*modifier
)->next_
;
994 CYTypeFunctionWith
*function((*modifier
)->Function());
995 if (function
== NULL
)
1001 CYArgument
*CYTypedParameter::Argument(CYContext
&context
) { $
T(NULL
)
1002 return $
CYArgument(typed_
->Replace(context
), next_
->Argument(context
));
1005 CYFunctionParameter
*CYTypedParameter::Parameters(CYContext
&context
) { $
T(NULL
)
1006 return $
CYFunctionParameter($
CYDeclaration(typed_
->identifier_
?: context
.Unique()), next_
->Parameters(context
));
1009 CYExpression
*CYTypedParameter::TypeSignature(CYContext
&context
, CYExpression
*prefix
) { $
T(prefix
)
1010 return next_
->TypeSignature(context
, $
CYAdd(prefix
, typed_
->Replace(context
)));
1013 CYStatement
*CYVar::Replace(CYContext
&context
) {
1014 declarations_
->Replace(context
);
1015 if (CYCompound
*compound
= declarations_
->Compound(context
))
1016 return $
E(compound
);
1020 CYExpression
*CYVariable::Replace(CYContext
&context
) {
1021 context
.Replace(name_
);
1025 CYFunctionParameter
*CYVariable::Parameter() const {
1026 return $
CYFunctionParameter($
CYDeclaration(name_
));
1029 CYStatement
*CYWhile::Replace(CYContext
&context
) {
1030 context
.Replace(test_
);
1031 context
.Replace(code_
);
1035 CYStatement
*CYWith::Replace(CYContext
&context
) {
1036 context
.Replace(scope_
);
1037 context
.Replace(code_
);
1041 CYExpression
*CYWord::ClassName(CYContext
&context
, bool object
) {
1042 CYString
*name($
S(this));
1044 return $
C1($
V("objc_getClass"), name
);