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(code_
);
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
);
129 context
.Replace(name_
);
130 context
.scope_
->Declare(context
, name_
, CYIdentifierCatch
);
132 context
.ReplaceAll(code_
);
133 scope
.Close(context
, code_
);
138 void CYClause::Replace(CYContext
&context
) { $
T()
139 context
.Replace(case_
);
140 context
.ReplaceAll(code_
);
141 next_
->Replace(context
);
144 CYStatement
*CYComment::Replace(CYContext
&context
) {
148 CYExpression
*CYCompound::Replace(CYContext
&context
) {
149 context
.Replace(expression_
);
150 context
.Replace(next_
);
152 if (CYCompound
*compound
= dynamic_cast<CYCompound
*>(expression_
)) {
153 expression_
= compound
->expression_
;
154 compound
->expression_
= compound
->next_
;
155 compound
->next_
= next_
;
162 CYFunctionParameter
*CYCompound::Parameter() const {
163 CYFunctionParameter
*next(next_
->Parameter());
167 CYFunctionParameter
*parameter(expression_
->Parameter());
168 if (parameter
== NULL
)
171 parameter
->SetNext(next
);
175 CYFunctionParameter
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
176 CYFunctionParameter
*next(next_
->Parameters(context
));
177 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
178 parameter
->SetNext(next
);
184 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
185 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
188 CYExpression
*CYCondition::Replace(CYContext
&context
) {
189 context
.Replace(test_
);
190 context
.Replace(true_
);
191 context
.Replace(false_
);
195 void CYContext::NonLocal(CYStatement
*&statements
) {
196 CYContext
&context(*this);
198 if (nextlocal_
!= NULL
&& nextlocal_
->identifier_
!= NULL
) {
199 CYIdentifier
*cye($
I("$cye")->Replace(context
));
200 CYIdentifier
*unique(nextlocal_
->identifier_
->Replace(context
));
202 CYStatement
*declare(
203 $
CYVar($
L1($
CYDeclaration(unique
, $
CYObject()))));
205 cy::Syntax::Catch
*rescue(
206 $
cy::Syntax::Catch(cye
, $$
->*
207 $
CYIf($
CYIdentical($
M($
V(cye
), $
S("$cyk")), $
V(unique
)), $$
->*
208 $
CYReturn($
M($
V(cye
), $
S("$cyv"))))->*
209 $
cy::Syntax::Throw($
V(cye
))));
211 // XXX: I don't understand any of this
212 context
.Replace(declare
);
213 rescue
->Replace(context
);
217 $
cy::Syntax::Try(statements
, rescue
, NULL
);
221 CYIdentifier
*CYContext::Unique() {
222 return $
CYIdentifier($pool
.strcat("$cy", $pool
.itoa(unique_
++), NULL
));
225 CYStatement
*CYContinue::Replace(CYContext
&context
) {
229 CYStatement
*CYDebugger::Replace(CYContext
&context
) {
233 CYAssignment
*CYDeclaration::Assignment(CYContext
&context
) {
234 if (initialiser_
== NULL
)
237 CYAssignment
*value($
CYAssign(Variable(context
), initialiser_
));
242 CYVariable
*CYDeclaration::Variable(CYContext
&context
) {
243 return $
V(identifier_
);
246 CYStatement
*CYDeclaration::ForEachIn(CYContext
&context
, CYExpression
*value
) {
247 return $
CYVar($
L1($
CYDeclaration(identifier_
, value
)));
250 CYExpression
*CYDeclaration::Replace(CYContext
&context
) {
251 context
.Replace(identifier_
);
252 context
.scope_
->Declare(context
, identifier_
, CYIdentifierVariable
);
253 return Variable(context
);
256 void CYDeclarations::Replace(CYContext
&context
) { $
T()
257 declaration_
->Replace(context
);
258 next_
->Replace(context
);
261 CYProperty
*CYDeclarations::Property(CYContext
&context
) { $
T(NULL
)
262 return $
CYProperty(declaration_
->identifier_
, declaration_
->initialiser_
, next_
->Property(context
));
265 CYFunctionParameter
*CYDeclarations::Parameter(CYContext
&context
) { $
T(NULL
)
266 return $
CYFunctionParameter($
CYDeclaration(declaration_
->identifier_
), next_
->Parameter(context
));
269 CYArgument
*CYDeclarations::Argument(CYContext
&context
) { $
T(NULL
)
270 return $
CYArgument(declaration_
->initialiser_
, next_
->Argument(context
));
273 CYExpression
*CYDeclarations::Expression(CYContext
&context
) { $
T(NULL
)
274 CYExpression
*compound(next_
->Expression(context
));
275 if (CYAssignment
*assignment
= declaration_
->Assignment(context
))
276 if (compound
== NULL
)
277 compound
= assignment
;
279 compound
= $
CYCompound(assignment
, compound
);
283 CYExpression
*CYDirectMember::Replace(CYContext
&context
) {
284 context
.Replace(object_
);
285 context
.Replace(property_
);
289 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
290 context
.Replace(test_
);
291 context
.ReplaceAll(code_
);
295 void CYElement::Replace(CYContext
&context
) { $
T()
296 context
.Replace(value_
);
297 next_
->Replace(context
);
300 CYStatement
*CYEmpty::Replace(CYContext
&context
) {
304 CYExpression
*CYEncodedType::Replace(CYContext
&context
) {
305 return typed_
->Replace(context
);
308 CYStatement
*CYExpress::Replace(CYContext
&context
) {
309 context
.Replace(expression_
);
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 CYFunctionParameter
*CYExpression::Parameter() const {
333 CYStatement
*CYExternal::Replace(CYContext
&context
) {
334 return $
E($
CYAssign($
V(typed_
->identifier_
), $
C1(typed_
->Replace(context
), $
C2($
V("dlsym"), $
V("RTLD_DEFAULT"), $
S(typed_
->identifier_
->Word())))));
337 CYNumber
*CYFalse::Number(CYContext
&context
) {
341 CYString
*CYFalse::String(CYContext
&context
) {
345 CYExpression
*CYFatArrow::Replace(CYContext
&context
) {
346 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
347 function
->this_
.SetNext(context
.this_
);
351 void CYFinally::Replace(CYContext
&context
) { $
T()
352 context
.ReplaceAll(code_
);
355 CYStatement
*CYFor::Replace(CYContext
&context
) {
356 context
.Replace(initialiser_
);
357 context
.Replace(test_
);
358 context
.Replace(increment_
);
359 context
.ReplaceAll(code_
);
363 CYExpression
*CYForDeclarations::Replace(CYContext
&context
) {
364 declarations_
->Replace(context
);
365 return declarations_
->Expression(context
);
368 // XXX: this still feels highly suboptimal
369 CYStatement
*CYForIn::Replace(CYContext
&context
) {
370 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
371 return $
CYBlock($$
->*
376 context
.Replace(initialiser_
);
377 context
.Replace(set_
);
378 context
.ReplaceAll(code_
);
382 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
383 return $
CYFunctionParameter($
CYDeclaration(name_
));
386 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
387 return $
CYForIn($
V(name_
), set_
, CYComprehension::Replace(context
, statement
));
390 CYStatement
*CYForOf::Replace(CYContext
&context
) {
391 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
392 return $
CYBlock($$
->*
397 CYIdentifier
*cys($
I("$cys")), *cyt($
I("$cyt"));
399 return $
CYLetStatement($
L2($
CYDeclaration(cys
, set_
), $
CYDeclaration(cyt
)), $$
->*
400 $
CYForIn($
V(cyt
), $
V(cys
), $
CYBlock($$
->*
401 initialiser_
->ForEachIn(context
, $
M($
V(cys
), $
V(cyt
)))->*
407 CYFunctionParameter
*CYForOfComprehension::Parameter(CYContext
&context
) const {
408 return $
CYFunctionParameter($
CYDeclaration(name_
));
411 CYStatement
*CYForOfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
412 CYIdentifier
*cys($
I("$cys"));
414 return $
E($
C0($
F(NULL
, $
P1($
L("$cys")), $$
->*
415 $
E($
CYAssign($
V(cys
), set_
))->*
416 $
CYForIn($
V(name_
), $
V(cys
), $
CYBlock($$
->*
417 $
E($
CYAssign($
V(name_
), $
M($
V(cys
), $
V(name_
))))->*
418 CYComprehension::Replace(context
, statement
)
423 void CYFunction::Inject(CYContext
&context
) {
424 context
.Replace(name_
);
425 context
.scope_
->Declare(context
, name_
, CYIdentifierOther
);
428 void CYFunction::Replace_(CYContext
&context
, bool outer
) {
432 CYThisScope
*_this(context
.this_
);
433 context
.this_
= CYGetLast(&this_
);
435 CYNonLocal
*nonlocal(context
.nonlocal_
);
436 CYNonLocal
*nextlocal(context
.nextlocal_
);
439 if (nonlocal_
!= NULL
) {
441 context
.nonlocal_
= nonlocal_
;
444 nonlocal_
= $
CYNonLocal();
445 context
.nextlocal_
= nonlocal_
;
448 CYScope
scope(!localize
, context
);
450 if (!outer
&& name_
!= NULL
)
453 parameters_
->Replace(context
, code_
);
454 context
.ReplaceAll(code_
);
456 if (CYIdentifier
*identifier
= this_
.identifier_
)
458 $
CYVar($
L1($
CYDeclaration(identifier
, $
CYThis())))->*
462 context
.NonLocal(code_
);
464 context
.nextlocal_
= nextlocal
;
465 context
.nonlocal_
= nonlocal
;
467 context
.this_
= _this
;
469 scope
.Close(context
, code_
);
472 CYExpression
*CYFunctionExpression::Replace(CYContext
&context
) {
473 Replace_(context
, false);
477 void CYFunctionParameter::Replace(CYContext
&context
, CYStatement
*&statements
) { $
T()
478 CYAssignment
*assignment(initialiser_
->Assignment(context
));
479 context
.Replace(initialiser_
);
481 next_
->Replace(context
, statements
);
483 if (assignment
!= NULL
)
485 // XXX: this cast is quite incorrect
486 $
CYIf($
CYIdentical($
CYTypeOf(dynamic_cast<CYExpression
*>(initialiser_
)), $
S("undefined")), $$
->*
492 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
493 Replace_(context
, true);
497 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
) {
498 if (replace_
!= NULL
&& replace_
!= this)
499 return replace_
->Replace(context
);
500 replace_
= context
.scope_
->Lookup(context
, this);
504 CYStatement
*CYIf::Replace(CYContext
&context
) {
505 context
.Replace(test_
);
506 context
.ReplaceAll(true_
);
507 context
.ReplaceAll(false_
);
511 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
515 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
516 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
519 CYStatement
*CYImport::Replace(CYContext
&context
) {
520 return $
CYVar($
L1($
L(module_
->part_
->Word(), $
C1($
V("require"), module_
->Replace(context
, "/")))));
523 CYExpression
*CYIndirect::Replace(CYContext
&context
) {
524 return $
M(rhs_
, $
S("$cyi"));
527 CYExpression
*CYIndirectMember::Replace(CYContext
&context
) {
528 return $
M($
CYIndirect(object_
), property_
);
531 CYExpression
*CYInfix::Replace(CYContext
&context
) {
532 context
.Replace(lhs_
);
533 context
.Replace(rhs_
);
537 CYStatement
*CYLabel::Replace(CYContext
&context
) {
538 context
.Replace(statement_
);
542 CYExpression
*CYLambda::Replace(CYContext
&context
) {
543 return $
N2($
V("Functor"), $
CYFunctionExpression(NULL
, parameters_
->Parameters(context
), code_
), parameters_
->TypeSignature(context
, typed_
->Replace(context
)));
546 CYStatement
*CYLetStatement::Replace(CYContext
&context
) {
547 return $
E($
CYCall(CYNonLocalize(context
, $
CYFunctionExpression(NULL
, declarations_
->Parameter(context
), code_
)), declarations_
->Argument(context
)));
550 CYString
*CYModule::Replace(CYContext
&context
, const char *separator
) const {
552 return $
CYString(part_
);
553 return $
CYString($pool
.strcat(next_
->Replace(context
, separator
)->Value(), separator
, part_
->Word(), NULL
));
556 CYExpression
*CYMultiply::Replace(CYContext
&context
) {
557 CYInfix::Replace(context
);
559 if (CYNumber
*lhn
= lhs_
->Number(context
))
560 if (CYNumber
*rhn
= rhs_
->Number(context
))
561 return $
D(lhn
->Value() * rhn
->Value());
569 CYExpression
*New::AddArgument(CYContext
&context
, CYExpression
*value
) {
570 CYSetLast(arguments_
) = $
CYArgument(value
);
574 CYExpression
*New::Replace(CYContext
&context
) {
575 context
.Replace(constructor_
);
576 arguments_
->Replace(context
);
582 CYNumber
*CYNull::Number(CYContext
&context
) {
586 CYString
*CYNull::String(CYContext
&context
) {
590 CYNumber
*CYNumber::Number(CYContext
&context
) {
594 CYString
*CYNumber::String(CYContext
&context
) {
595 // XXX: there is a precise algorithm for this
596 return $
S($pool
.sprintf(24, "%.17g", Value()));
599 CYExpression
*CYObject::Replace(CYContext
&context
) {
600 properties_
->Replace(context
);
604 CYExpression
*CYParenthetical::Replace(CYContext
&context
) {
608 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
609 context
.Replace(lhs_
);
613 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
614 context
.Replace(rhs_
);
618 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
619 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
620 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
623 struct IdentifierUsageLess
:
624 std::binary_function
<CYIdentifier
*, CYIdentifier
*, bool>
626 _finline
bool operator ()(CYIdentifier
*lhs
, CYIdentifier
*rhs
) const {
627 if (lhs
->usage_
!= rhs
->usage_
)
628 return lhs
->usage_
> rhs
->usage_
;
633 typedef std::set
<CYIdentifier
*, IdentifierUsageLess
> IdentifierUsages
;
636 void CYProgram::Replace(CYContext
&context
) {
637 CYScope
scope(true, context
);
639 context
.nextlocal_
= $
CYNonLocal();
640 context
.ReplaceAll(code_
);
641 context
.NonLocal(code_
);
643 scope
.Close(context
, code_
);
647 CYCStringSet external
;
648 for (CYIdentifierValueSet::const_iterator
i(scope
.identifiers_
.begin()); i
!= scope
.identifiers_
.end(); ++i
)
649 external
.insert((*i
)->Word());
651 IdentifierUsages usages
;
653 if (offset
< context
.rename_
.size())
654 CYForEach (i
, context
.rename_
[offset
].identifier_
)
657 // XXX: totalling the probable occurrences and sorting by them would improve the result
658 for (CYIdentifierUsageVector::const_iterator
i(context
.rename_
.begin()); i
!= context
.rename_
.end(); ++i
, ++offset
) {
659 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
663 if (context
.options_
.verbose_
)
664 name
= $pool
.strcat("$", $pool
.itoa(offset
), NULL
);
670 unsigned position(7), local(offset
+ 1);
673 unsigned index(local
% (sizeof(MappingSet
) - 1));
674 local
/= sizeof(MappingSet
) - 1;
675 id
[--position
] = MappingSet
[index
];
676 } while (local
!= 0);
678 if (external
.find(id
+ position
) != external
.end()) {
683 name
= $pool
.strmemdup(id
+ position
, 7 - position
);
684 // XXX: at some point, this could become a keyword
687 CYForEach (identifier
, i
->identifier_
)
688 identifier
->Set(name
);
692 void CYProperty::Replace(CYContext
&context
) { $
T()
693 context
.Replace(value_
);
694 next_
->Replace(context
);
699 CYStatement
*CYReturn::Replace(CYContext
&context
) {
700 if (context
.nonlocal_
!= NULL
) {
701 CYProperty
*value(value_
== NULL
? NULL
: $
CYProperty($
S("$cyv"), value_
));
702 return $
cy::Syntax::Throw($
CYObject(
703 $
CYProperty($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
)
707 context
.Replace(value_
);
711 CYExpression
*CYRubyBlock::Replace(CYContext
&context
) {
712 // XXX: this needs to do something much more epic to handle return
713 return call_
->AddArgument(context
, proc_
->Replace(context
));
716 CYExpression
*CYRubyProc::Replace(CYContext
&context
) {
717 return CYNonLocalize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
));
720 CYScope::CYScope(bool transparent
, CYContext
&context
) :
721 transparent_(transparent
),
722 parent_(context
.scope_
)
724 context
.scope_
= this;
727 void CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierFlags flags
) {
728 if (!transparent_
|| flags
== CYIdentifierArgument
|| flags
== CYIdentifierCatch
)
729 internal_
.insert(CYIdentifierAddressFlagsMap::value_type(identifier
, flags
));
730 else if (parent_
!= NULL
)
731 parent_
->Declare(context
, identifier
, flags
);
734 CYIdentifier
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
735 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
736 return *insert
.first
;
739 void CYScope::Merge(CYContext
&context
, CYIdentifier
*identifier
) {
740 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
741 if (!insert
.second
) {
742 if ((*insert
.first
)->offset_
< identifier
->offset_
)
743 (*insert
.first
)->offset_
= identifier
->offset_
;
744 identifier
->replace_
= *insert
.first
;
745 (*insert
.first
)->usage_
+= identifier
->usage_
+ 1;
750 struct IdentifierOffset
{
752 CYIdentifierFlags flags_
;
754 CYIdentifier
*identifier_
;
756 IdentifierOffset(CYIdentifier
*identifier
, CYIdentifierFlags flags
) :
757 offset_(identifier
->offset_
),
759 usage_(identifier
->usage_
),
760 identifier_(identifier
)
765 struct IdentifierOffsetLess
:
766 std::binary_function
<const IdentifierOffset
&, const IdentifierOffset
&, bool>
768 _finline
bool operator ()(const IdentifierOffset
&lhs
, const IdentifierOffset
&rhs
) const {
769 if (lhs
.offset_
!= rhs
.offset_
)
770 return lhs
.offset_
< rhs
.offset_
;
771 if (lhs
.flags_
!= rhs
.flags_
)
772 return lhs
.flags_
< rhs
.flags_
;
773 /*if (lhs.usage_ != rhs.usage_)
774 return lhs.usage_ < rhs.usage_;*/
775 return lhs
.identifier_
< rhs
.identifier_
;
779 typedef std::set
<IdentifierOffset
, IdentifierOffsetLess
> IdentifierOffsets
;
782 void CYScope::Close(CYContext
&context
, CYStatement
*&statements
) {
783 context
.scope_
= parent_
;
788 CYDeclarations
*last(NULL
), *curr(NULL
);
790 IdentifierOffsets offsets
;
792 for (CYIdentifierAddressFlagsMap::const_iterator
i(internal_
.begin()); i
!= internal_
.end(); ++i
)
793 if (i
->second
!= CYIdentifierMagic
)
794 offsets
.insert(IdentifierOffset(i
->first
, i
->second
));
798 for (IdentifierOffsets::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
799 if (i
->flags_
== CYIdentifierVariable
) {
800 CYDeclarations
*next($
CYDeclarations($
CYDeclaration(i
->identifier_
)));
808 if (offset
< i
->offset_
)
810 if (context
.rename_
.size() <= offset
)
811 context
.rename_
.resize(offset
+ 1);
813 CYIdentifierUsage
&rename(context
.rename_
[offset
++]);
814 i
->identifier_
->SetNext(rename
.identifier_
);
815 rename
.identifier_
= i
->identifier_
;
816 rename
.usage_
+= i
->identifier_
->usage_
+ 1;
820 CYVar
*var($
CYVar(last
));
821 var
->SetNext(statements
);
825 for (CYIdentifierValueSet::const_iterator
i(identifiers_
.begin()); i
!= identifiers_
.end(); ++i
)
826 if (internal_
.find(*i
) == internal_
.end()) {
827 //std::cout << *i << '=' << offset << std::endl;
828 if ((*i
)->offset_
< offset
)
829 (*i
)->offset_
= offset
;
830 parent_
->Merge(context
, *i
);
834 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
835 size_t size(size_
+ rhs
->size_
);
836 char *value($
char[size
+ 1]);
837 memcpy(value
, value_
, size_
);
838 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
840 return $
S(value
, size
);
843 CYNumber
*CYString::Number(CYContext
&context
) {
844 // XXX: there is a precise algorithm for this
848 CYString
*CYString::String(CYContext
&context
) {
852 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
853 context
.Replace(value_
);
854 clauses_
->Replace(context
);
858 CYExpression
*CYThis::Replace(CYContext
&context
) {
859 if (context
.this_
!= NULL
)
860 return $
V(context
.this_
->Identifier(context
));
867 CYStatement
*Throw::Replace(CYContext
&context
) {
868 context
.Replace(value_
);
874 CYExpression
*CYTrivial::Replace(CYContext
&context
) {
878 CYNumber
*CYTrue::Number(CYContext
&context
) {
882 CYString
*CYTrue::String(CYContext
&context
) {
889 CYStatement
*Try::Replace(CYContext
&context
) {
890 context
.ReplaceAll(code_
);
891 catch_
->Replace(context
);
892 finally_
->Replace(context
);
898 CYExpression
*CYTypeArrayOf::Replace_(CYContext
&context
, CYExpression
*type
) {
899 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("arrayOf")), $
CYArgument(size_
)));
902 CYExpression
*CYTypeBlockWith::Replace_(CYContext
&context
, CYExpression
*type
) {
903 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("blockWith")), parameters_
->Argument(context
)));
906 CYExpression
*CYTypeConstant::Replace_(CYContext
&context
, CYExpression
*type
) {
907 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("constant"))));
910 CYStatement
*CYTypeDefinition::Replace(CYContext
&context
) {
911 return $
E($
CYAssign($
V(typed_
->identifier_
), typed_
->Replace(context
)));
914 CYExpression
*CYTypeError::Replace(CYContext
&context
) {
919 CYExpression
*CYTypeModifier::Replace(CYContext
&context
, CYExpression
*type
) { $
T(type
)
920 return Replace_(context
, type
);
923 CYExpression
*CYTypeFunctionWith::Replace_(CYContext
&context
, CYExpression
*type
) {
924 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("functionWith")), parameters_
->Argument(context
)));
927 CYExpression
*CYTypeLong::Replace(CYContext
&context
) {
928 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("long")));
931 CYExpression
*CYTypePointerTo::Replace_(CYContext
&context
, CYExpression
*type
) {
932 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("pointerTo"))));
935 CYExpression
*CYTypeShort::Replace(CYContext
&context
) {
936 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("short")));
939 CYExpression
*CYTypeSigned::Replace(CYContext
&context
) {
940 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("signed")));
943 CYExpression
*CYTypeUnsigned::Replace(CYContext
&context
) {
944 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("unsigned")));
947 CYExpression
*CYTypeVariable::Replace(CYContext
&context
) {
951 CYExpression
*CYTypeVoid::Replace(CYContext
&context
) {
952 return $
N1($
V("Type"), $
CYString("v"));
955 CYExpression
*CYTypeVolatile::Replace_(CYContext
&context
, CYExpression
*type
) {
956 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("volatile"))));
959 CYExpression
*CYTypedIdentifier::Replace(CYContext
&context
) {
960 return modifier_
->Replace(context
, specifier_
->Replace(context
));
963 CYTypeFunctionWith
*CYTypedIdentifier::Function() {
964 CYTypeModifier
**modifier(&modifier_
);
965 if (*modifier
== NULL
)
967 while ((*modifier
)->next_
!= NULL
)
968 modifier
= &(*modifier
)->next_
;
969 CYTypeFunctionWith
*function((*modifier
)->Function());
970 if (function
== NULL
)
976 CYArgument
*CYTypedParameter::Argument(CYContext
&context
) { $
T(NULL
)
977 return $
CYArgument(typed_
->Replace(context
), next_
->Argument(context
));
980 CYFunctionParameter
*CYTypedParameter::Parameters(CYContext
&context
) { $
T(NULL
)
981 return $
CYFunctionParameter($
CYDeclaration(typed_
->identifier_
?: context
.Unique()), next_
->Parameters(context
));
984 CYExpression
*CYTypedParameter::TypeSignature(CYContext
&context
, CYExpression
*prefix
) { $
T(prefix
)
985 return next_
->TypeSignature(context
, $
CYAdd(prefix
, typed_
->Replace(context
)));
988 CYStatement
*CYVar::Replace(CYContext
&context
) {
989 declarations_
->Replace(context
);
990 if (CYExpression
*expression
= declarations_
->Expression(context
))
991 return $
E(expression
);
995 CYExpression
*CYVariable::Replace(CYContext
&context
) {
996 context
.Replace(name_
);
1000 CYFunctionParameter
*CYVariable::Parameter() const {
1001 return $
CYFunctionParameter($
CYDeclaration(name_
));
1004 CYStatement
*CYWhile::Replace(CYContext
&context
) {
1005 context
.Replace(test_
);
1006 context
.ReplaceAll(code_
);
1010 CYStatement
*CYWith::Replace(CYContext
&context
) {
1011 context
.Replace(scope_
);
1012 context
.ReplaceAll(code_
);
1016 CYExpression
*CYWord::ClassName(CYContext
&context
, bool object
) {
1017 CYString
*name($
S(this));
1019 return $
C1($
V("objc_getClass"), name
);