1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2014 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
*CYComprehension::Parameters(CYContext
&context
) const { $
T(NULL
)
173 CYFunctionParameter
*next(next_
->Parameters(context
));
174 if (CYFunctionParameter
*parameter
= Parameter(context
)) {
175 parameter
->SetNext(next
);
181 CYStatement
*CYComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
182 return next_
== NULL
? statement
: next_
->Replace(context
, statement
);
185 CYExpression
*CYCondition::Replace(CYContext
&context
) {
186 context
.Replace(test_
);
187 context
.Replace(true_
);
188 context
.Replace(false_
);
192 void CYContext::NonLocal(CYStatement
*&statements
) {
193 CYContext
&context(*this);
195 if (nextlocal_
!= NULL
&& nextlocal_
->identifier_
!= NULL
) {
196 CYIdentifier
*cye($
I("$cye")->Replace(context
));
197 CYIdentifier
*unique(nextlocal_
->identifier_
->Replace(context
));
199 CYStatement
*declare(
200 $
CYVar($
L1($
CYDeclaration(unique
, $
CYObject()))));
202 cy::Syntax::Catch
*rescue(
203 $
cy::Syntax::Catch(cye
, $$
->*
204 $
CYIf($
CYIdentical($
M($
V(cye
), $
S("$cyk")), $
V(unique
)), $$
->*
205 $
CYReturn($
M($
V(cye
), $
S("$cyv"))))->*
206 $
cy::Syntax::Throw($
V(cye
))));
208 context
.Replace(declare
);
209 rescue
->Replace(context
);
213 $
cy::Syntax::Try(statements
, rescue
, NULL
);
217 CYIdentifier
*CYContext::Unique() {
218 return $
CYIdentifier($pool
.strcat("$cy", $pool
.itoa(unique_
++), NULL
));
221 CYStatement
*CYContinue::Replace(CYContext
&context
) {
225 CYStatement
*CYDebugger::Replace(CYContext
&context
) {
229 CYAssignment
*CYDeclaration::Assignment(CYContext
&context
) {
230 if (initialiser_
== NULL
)
233 CYAssignment
*value($
CYAssign(Variable(context
), initialiser_
));
238 CYVariable
*CYDeclaration::Variable(CYContext
&context
) {
239 return $
V(identifier_
);
242 CYStatement
*CYDeclaration::ForEachIn(CYContext
&context
, CYExpression
*value
) {
243 return $
CYVar($
L1($
CYDeclaration(identifier_
, value
)));
246 CYExpression
*CYDeclaration::Replace(CYContext
&context
) {
247 context
.Replace(identifier_
);
248 context
.scope_
->Declare(context
, identifier_
, CYIdentifierVariable
);
249 return Variable(context
);
252 void CYDeclarations::Replace(CYContext
&context
) { $
T()
253 declaration_
->Replace(context
);
254 next_
->Replace(context
);
257 CYProperty
*CYDeclarations::Property(CYContext
&context
) { $
T(NULL
)
258 return $
CYProperty(declaration_
->identifier_
, declaration_
->initialiser_
, next_
->Property(context
));
261 CYFunctionParameter
*CYDeclarations::Parameter(CYContext
&context
) { $
T(NULL
)
262 return $
CYFunctionParameter($
CYDeclaration(declaration_
->identifier_
), next_
->Parameter(context
));
265 CYArgument
*CYDeclarations::Argument(CYContext
&context
) { $
T(NULL
)
266 return $
CYArgument(declaration_
->initialiser_
, next_
->Argument(context
));
269 CYCompound
*CYDeclarations::Compound(CYContext
&context
) { $
T(NULL
)
270 CYCompound
*compound(next_
->Compound(context
));
271 if (CYAssignment
*assignment
= declaration_
->Assignment(context
))
272 compound
= $
CYCompound(assignment
, compound
);
276 CYExpression
*CYDirectMember::Replace(CYContext
&context
) {
277 context
.Replace(object_
);
278 context
.Replace(property_
);
282 CYStatement
*CYDoWhile::Replace(CYContext
&context
) {
283 context
.Replace(test_
);
284 context
.Replace(code_
);
288 void CYElement::Replace(CYContext
&context
) { $
T()
289 context
.Replace(value_
);
290 next_
->Replace(context
);
293 CYStatement
*CYEmpty::Replace(CYContext
&context
) {
297 CYExpression
*CYEncodedType::Replace(CYContext
&context
) {
298 return typed_
->Replace(context
);
301 CYStatement
*CYExpress::Replace(CYContext
&context
) {
302 while (CYExpress
*express
= dynamic_cast<CYExpress
*>(next_
)) {
303 expression_
= $
CYCompound(expression_
, express
->expression_
);
304 SetNext(express
->next_
);
307 context
.Replace(expression_
);
308 if (expression_
== NULL
)
314 CYExpression
*CYExpression::AddArgument(CYContext
&context
, CYExpression
*value
) {
315 return $
C1(this, value
);
318 CYExpression
*CYExpression::ClassName(CYContext
&context
, bool object
) {
322 CYStatement
*CYExpression::ForEachIn(CYContext
&context
, CYExpression
*value
) {
323 return $
E($
CYAssign(this, value
));
326 CYAssignment
*CYExpression::Assignment(CYContext
&context
) {
330 CYStatement
*CYExternal::Replace(CYContext
&context
) {
331 return $
E($
CYAssign($
V(typed_
->identifier_
), $
C1(typed_
->Replace(context
), $
C2($
V("dlsym"), $
V("RTLD_DEFAULT"), $
S(typed_
->identifier_
->Word())))));
334 CYNumber
*CYFalse::Number(CYContext
&context
) {
338 CYString
*CYFalse::String(CYContext
&context
) {
342 CYExpression
*CYFatArrow::Replace(CYContext
&context
) {
343 CYFunctionExpression
*function($
CYFunctionExpression(NULL
, parameters_
, code_
));
344 function
->this_
.SetNext(context
.this_
);
348 void CYFinally::Replace(CYContext
&context
) { $
T()
349 code_
.Replace(context
);
352 CYStatement
*CYFor::Replace(CYContext
&context
) {
353 context
.Replace(initialiser_
);
354 context
.Replace(test_
);
355 context
.Replace(increment_
);
356 context
.Replace(code_
);
360 CYCompound
*CYForDeclarations::Replace(CYContext
&context
) {
361 declarations_
->Replace(context
);
362 return declarations_
->Compound(context
);
365 // XXX: this still feels highly suboptimal
366 CYStatement
*CYForIn::Replace(CYContext
&context
) {
367 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
368 return $
CYBlock($$
->*
373 context
.Replace(initialiser_
);
374 context
.Replace(set_
);
375 context
.Replace(code_
);
379 CYFunctionParameter
*CYForInComprehension::Parameter(CYContext
&context
) const {
380 return $
CYFunctionParameter($
CYDeclaration(name_
));
383 CYStatement
*CYForInComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
384 return $
CYForIn($
V(name_
), set_
, CYComprehension::Replace(context
, statement
));
387 CYStatement
*CYForOf::Replace(CYContext
&context
) {
388 if (CYAssignment
*assignment
= initialiser_
->Assignment(context
))
389 return $
CYBlock($$
->*
394 CYIdentifier
*cys($
I("$cys")), *cyt($
I("$cyt"));
396 return $
CYLetStatement($
L2($
CYDeclaration(cys
, set_
), $
CYDeclaration(cyt
)), $$
->*
397 $
CYForIn($
V(cyt
), $
V(cys
), $
CYBlock($$
->*
398 initialiser_
->ForEachIn(context
, $
M($
V(cys
), $
V(cyt
)))->*
404 CYFunctionParameter
*CYForOfComprehension::Parameter(CYContext
&context
) const {
405 return $
CYFunctionParameter($
CYDeclaration(name_
));
408 CYStatement
*CYForOfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
409 CYIdentifier
*cys($
I("$cys"));
411 return $
E($
C0($
F(NULL
, $
P1($
L("$cys")), $$
->*
412 $
E($
CYAssign($
V(cys
), set_
))->*
413 $
CYForIn($
V(name_
), $
V(cys
), $
CYBlock($$
->*
414 $
E($
CYAssign($
V(name_
), $
M($
V(cys
), $
V(name_
))))->*
415 CYComprehension::Replace(context
, statement
)
420 void CYFunction::Inject(CYContext
&context
) {
421 context
.Replace(name_
);
422 context
.scope_
->Declare(context
, name_
, CYIdentifierOther
);
425 void CYFunction::Replace_(CYContext
&context
, bool outer
) {
429 CYThisScope
*_this(context
.this_
);
430 context
.this_
= CYGetLast(&this_
);
432 CYNonLocal
*nonlocal(context
.nonlocal_
);
433 CYNonLocal
*nextlocal(context
.nextlocal_
);
436 if (nonlocal_
!= NULL
) {
438 context
.nonlocal_
= nonlocal_
;
441 nonlocal_
= $
CYNonLocal();
442 context
.nextlocal_
= nonlocal_
;
445 CYScope
scope(!localize
, context
, code_
.statements_
);
447 if (!outer
&& name_
!= NULL
)
450 parameters_
->Replace(context
, code_
);
451 code_
.Replace(context
);
453 if (CYIdentifier
*identifier
= this_
.identifier_
)
454 code_
.statements_
= $$
->*
455 $
CYVar($
L1($
CYDeclaration(identifier
, $
CYThis())))->*
459 context
.NonLocal(code_
.statements_
);
461 context
.nextlocal_
= nextlocal
;
462 context
.nonlocal_
= nonlocal
;
464 context
.this_
= _this
;
469 CYExpression
*CYFunctionExpression::Replace(CYContext
&context
) {
470 Replace_(context
, false);
474 void CYFunctionParameter::Replace(CYContext
&context
, CYBlock
&code
) { $
T()
475 CYAssignment
*assignment(initialiser_
->Assignment(context
));
476 context
.Replace(initialiser_
);
478 next_
->Replace(context
, code
);
480 if (assignment
!= NULL
)
481 // XXX: this cast is quite incorrect
482 code
.AddPrev($
CYIf($
CYIdentical($
CYTypeOf(dynamic_cast<CYExpression
*>(initialiser_
)), $
S("undefined")), $$
->*
487 CYStatement
*CYFunctionStatement::Replace(CYContext
&context
) {
488 Replace_(context
, true);
492 CYIdentifier
*CYIdentifier::Replace(CYContext
&context
) {
493 if (replace_
!= NULL
&& replace_
!= this)
494 return replace_
->Replace(context
);
495 replace_
= context
.scope_
->Lookup(context
, this);
499 CYStatement
*CYIf::Replace(CYContext
&context
) {
500 context
.Replace(test_
);
501 context
.Replace(true_
);
502 context
.Replace(false_
);
506 CYFunctionParameter
*CYIfComprehension::Parameter(CYContext
&context
) const {
510 CYStatement
*CYIfComprehension::Replace(CYContext
&context
, CYStatement
*statement
) const {
511 return $
CYIf(test_
, CYComprehension::Replace(context
, statement
));
514 CYStatement
*CYImport::Replace(CYContext
&context
) {
515 return $
CYVar($
L1($
L(module_
->part_
->Word(), $
C1($
V("require"), module_
->Replace(context
, "/")))));
518 CYExpression
*CYIndirect::Replace(CYContext
&context
) {
519 return $
M(rhs_
, $
S("$cyi"));
522 CYExpression
*CYIndirectMember::Replace(CYContext
&context
) {
523 return $
M($
CYIndirect(object_
), property_
);
526 CYExpression
*CYInfix::Replace(CYContext
&context
) {
527 context
.Replace(lhs_
);
528 context
.Replace(rhs_
);
532 CYStatement
*CYLabel::Replace(CYContext
&context
) {
533 context
.Replace(statement_
);
537 CYExpression
*CYLambda::Replace(CYContext
&context
) {
538 return $
N2($
V("Functor"), $
CYFunctionExpression(NULL
, parameters_
->Parameters(context
), statements_
), parameters_
->TypeSignature(context
, typed_
->Replace(context
)));
541 CYStatement
*CYLetStatement::Replace(CYContext
&context
) {
542 return $
E($
CYCall(CYNonLocalize(context
, $
CYFunctionExpression(NULL
, declarations_
->Parameter(context
), code_
)), declarations_
->Argument(context
)));
545 CYString
*CYModule::Replace(CYContext
&context
, const char *separator
) const {
547 return $
CYString(part_
);
548 return $
CYString($pool
.strcat(next_
->Replace(context
, separator
)->Value(), separator
, part_
->Word(), NULL
));
551 CYExpression
*CYMultiply::Replace(CYContext
&context
) {
552 CYInfix::Replace(context
);
554 if (CYNumber
*lhn
= lhs_
->Number(context
))
555 if (CYNumber
*rhn
= rhs_
->Number(context
))
556 return $
D(lhn
->Value() * rhn
->Value());
564 CYExpression
*New::AddArgument(CYContext
&context
, CYExpression
*value
) {
565 CYSetLast(arguments_
) = $
CYArgument(value
);
569 CYExpression
*New::Replace(CYContext
&context
) {
570 context
.Replace(constructor_
);
571 arguments_
->Replace(context
);
577 CYNumber
*CYNull::Number(CYContext
&context
) {
581 CYString
*CYNull::String(CYContext
&context
) {
585 CYNumber
*CYNumber::Number(CYContext
&context
) {
589 CYString
*CYNumber::String(CYContext
&context
) {
590 // XXX: there is a precise algorithm for this
591 return $
S($pool
.sprintf(24, "%.17g", Value()));
594 CYExpression
*CYObject::Replace(CYContext
&context
) {
595 properties_
->Replace(context
);
599 CYExpression
*CYPostfix::Replace(CYContext
&context
) {
600 context
.Replace(lhs_
);
604 CYExpression
*CYPrefix::Replace(CYContext
&context
) {
605 context
.Replace(rhs_
);
609 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
610 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
611 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
614 struct IdentifierUsageLess
:
615 std::binary_function
<CYIdentifier
*, CYIdentifier
*, bool>
617 _finline
bool operator ()(CYIdentifier
*lhs
, CYIdentifier
*rhs
) const {
618 if (lhs
->usage_
!= rhs
->usage_
)
619 return lhs
->usage_
> rhs
->usage_
;
624 typedef std::set
<CYIdentifier
*, IdentifierUsageLess
> IdentifierUsages
;
627 void CYProgram::Replace(CYContext
&context
) {
628 CYScope
scope(true, context
, statements_
);
630 context
.nextlocal_
= $
CYNonLocal();
631 context
.ReplaceAll(statements_
);
632 context
.NonLocal(statements_
);
638 CYCStringSet external
;
639 for (CYIdentifierValueSet::const_iterator
i(scope
.identifiers_
.begin()); i
!= scope
.identifiers_
.end(); ++i
)
640 external
.insert((*i
)->Word());
642 IdentifierUsages usages
;
644 if (offset
< context
.rename_
.size())
645 CYForEach (i
, context
.rename_
[offset
].identifier_
)
648 // XXX: totalling the probable occurrences and sorting by them would improve the result
649 for (CYIdentifierUsageVector::const_iterator
i(context
.rename_
.begin()); i
!= context
.rename_
.end(); ++i
, ++offset
) {
650 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
654 if (context
.options_
.verbose_
)
655 name
= $pool
.strcat("$", $pool
.itoa(offset
), NULL
);
661 unsigned position(7), local(offset
+ 1);
664 unsigned index(local
% (sizeof(MappingSet
) - 1));
665 local
/= sizeof(MappingSet
) - 1;
666 id
[--position
] = MappingSet
[index
];
667 } while (local
!= 0);
669 if (external
.find(id
+ position
) != external
.end()) {
674 name
= $pool
.strmemdup(id
+ position
, 7 - position
);
675 // XXX: at some point, this could become a keyword
678 CYForEach (identifier
, i
->identifier_
)
679 identifier
->Set(name
);
683 void CYProperty::Replace(CYContext
&context
) { $
T()
684 context
.Replace(value_
);
685 next_
->Replace(context
);
690 CYStatement
*CYReturn::Replace(CYContext
&context
) {
691 if (context
.nonlocal_
!= NULL
) {
692 CYProperty
*value(value_
== NULL
? NULL
: $
CYProperty($
S("$cyv"), value_
));
693 return $
cy::Syntax::Throw($
CYObject(
694 $
CYProperty($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
)
698 context
.Replace(value_
);
702 CYExpression
*CYRubyBlock::Replace(CYContext
&context
) {
703 // XXX: this needs to do something much more epic to handle return
704 return call_
->AddArgument(context
, proc_
->Replace(context
));
707 CYExpression
*CYRubyProc::Replace(CYContext
&context
) {
708 return CYNonLocalize(context
, $
CYFunctionExpression(NULL
, parameters_
, code_
));
711 CYScope::CYScope(bool transparent
, CYContext
&context
, CYStatement
*&statements
) :
712 transparent_(transparent
),
714 statements_(statements
),
715 parent_(context
.scope_
)
717 context_
.scope_
= this;
720 CYScope::~CYScope() {
723 void CYScope::Close() {
724 context_
.scope_
= parent_
;
725 Scope(context_
, statements_
);
728 void CYScope::Declare(CYContext
&context
, CYIdentifier
*identifier
, CYIdentifierFlags flags
) {
729 if (!transparent_
|| flags
== CYIdentifierArgument
|| flags
== CYIdentifierCatch
)
730 internal_
.insert(CYIdentifierAddressFlagsMap::value_type(identifier
, flags
));
731 else if (parent_
!= NULL
)
732 parent_
->Declare(context
, identifier
, flags
);
735 CYIdentifier
*CYScope::Lookup(CYContext
&context
, CYIdentifier
*identifier
) {
736 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
737 return *insert
.first
;
740 void CYScope::Merge(CYContext
&context
, CYIdentifier
*identifier
) {
741 std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
));
742 if (!insert
.second
) {
743 if ((*insert
.first
)->offset_
< identifier
->offset_
)
744 (*insert
.first
)->offset_
= identifier
->offset_
;
745 identifier
->replace_
= *insert
.first
;
746 (*insert
.first
)->usage_
+= identifier
->usage_
+ 1;
751 struct IdentifierOffset
{
753 CYIdentifierFlags flags_
;
755 CYIdentifier
*identifier_
;
757 IdentifierOffset(CYIdentifier
*identifier
, CYIdentifierFlags flags
) :
758 offset_(identifier
->offset_
),
760 usage_(identifier
->usage_
),
761 identifier_(identifier
)
766 struct IdentifierOffsetLess
:
767 std::binary_function
<const IdentifierOffset
&, const IdentifierOffset
&, bool>
769 _finline
bool operator ()(const IdentifierOffset
&lhs
, const IdentifierOffset
&rhs
) const {
770 if (lhs
.offset_
!= rhs
.offset_
)
771 return lhs
.offset_
< rhs
.offset_
;
772 if (lhs
.flags_
!= rhs
.flags_
)
773 return lhs
.flags_
< rhs
.flags_
;
774 /*if (lhs.usage_ != rhs.usage_)
775 return lhs.usage_ < rhs.usage_;*/
776 return lhs
.identifier_
< rhs
.identifier_
;
780 typedef std::set
<IdentifierOffset
, IdentifierOffsetLess
> IdentifierOffsets
;
783 void CYScope::Scope(CYContext
&context
, CYStatement
*&statements
) {
787 CYDeclarations
*last(NULL
), *curr(NULL
);
789 IdentifierOffsets offsets
;
791 for (CYIdentifierAddressFlagsMap::const_iterator
i(internal_
.begin()); i
!= internal_
.end(); ++i
)
792 if (i
->second
!= CYIdentifierMagic
)
793 offsets
.insert(IdentifierOffset(i
->first
, i
->second
));
797 for (IdentifierOffsets::const_iterator
i(offsets
.begin()); i
!= offsets
.end(); ++i
) {
798 if (i
->flags_
== CYIdentifierVariable
) {
799 CYDeclarations
*next($
CYDeclarations($
CYDeclaration(i
->identifier_
)));
807 if (offset
< i
->offset_
)
809 if (context
.rename_
.size() <= offset
)
810 context
.rename_
.resize(offset
+ 1);
812 CYIdentifierUsage
&rename(context
.rename_
[offset
++]);
813 i
->identifier_
->SetNext(rename
.identifier_
);
814 rename
.identifier_
= i
->identifier_
;
815 rename
.usage_
+= i
->identifier_
->usage_
+ 1;
819 CYVar
*var($
CYVar(last
));
820 var
->SetNext(statements
);
824 for (CYIdentifierValueSet::const_iterator
i(identifiers_
.begin()); i
!= identifiers_
.end(); ++i
)
825 if (internal_
.find(*i
) == internal_
.end()) {
826 //std::cout << *i << '=' << offset << std::endl;
827 if ((*i
)->offset_
< offset
)
828 (*i
)->offset_
= offset
;
829 parent_
->Merge(context
, *i
);
833 CYString
*CYString::Concat(CYContext
&context
, CYString
*rhs
) const {
834 size_t size(size_
+ rhs
->size_
);
835 char *value($
char[size
+ 1]);
836 memcpy(value
, value_
, size_
);
837 memcpy(value
+ size_
, rhs
->value_
, rhs
->size_
);
839 return $
S(value
, size
);
842 CYNumber
*CYString::Number(CYContext
&context
) {
843 // XXX: there is a precise algorithm for this
847 CYString
*CYString::String(CYContext
&context
) {
851 CYStatement
*CYSwitch::Replace(CYContext
&context
) {
852 context
.Replace(value_
);
853 clauses_
->Replace(context
);
857 CYExpression
*CYThis::Replace(CYContext
&context
) {
858 if (context
.this_
!= NULL
)
859 return $
V(context
.this_
->Identifier(context
));
866 CYStatement
*Throw::Replace(CYContext
&context
) {
867 context
.Replace(value_
);
873 CYExpression
*CYTrivial::Replace(CYContext
&context
) {
877 CYNumber
*CYTrue::Number(CYContext
&context
) {
881 CYString
*CYTrue::String(CYContext
&context
) {
888 CYStatement
*Try::Replace(CYContext
&context
) {
889 code_
.Replace(context
);
890 catch_
->Replace(context
);
891 finally_
->Replace(context
);
897 CYExpression
*CYTypeArrayOf::Replace_(CYContext
&context
, CYExpression
*type
) {
898 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("arrayOf")), $
CYArgument(size_
)));
901 CYExpression
*CYTypeBlockWith::Replace_(CYContext
&context
, CYExpression
*type
) {
902 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("blockWith")), parameters_
->Argument(context
)));
905 CYExpression
*CYTypeConstant::Replace_(CYContext
&context
, CYExpression
*type
) {
906 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("constant"))));
909 CYStatement
*CYTypeDefinition::Replace(CYContext
&context
) {
910 return $
E($
CYAssign($
V(typed_
->identifier_
), typed_
->Replace(context
)));
913 CYExpression
*CYTypeError::Replace(CYContext
&context
) {
918 CYExpression
*CYTypeModifier::Replace(CYContext
&context
, CYExpression
*type
) { $
T(type
)
919 return Replace_(context
, type
);
922 CYExpression
*CYTypeFunctionWith::Replace_(CYContext
&context
, CYExpression
*type
) {
923 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("functionWith")), parameters_
->Argument(context
)));
926 CYExpression
*CYTypeLong::Replace(CYContext
&context
) {
927 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("long")));
930 CYExpression
*CYTypePointerTo::Replace_(CYContext
&context
, CYExpression
*type
) {
931 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("pointerTo"))));
934 CYExpression
*CYTypeShort::Replace(CYContext
&context
) {
935 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("short")));
938 CYExpression
*CYTypeSigned::Replace(CYContext
&context
) {
939 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("signed")));
942 CYExpression
*CYTypeUnsigned::Replace(CYContext
&context
) {
943 return $
CYCall($
CYDirectMember(specifier_
->Replace(context
), $
CYString("unsigned")));
946 CYExpression
*CYTypeVariable::Replace(CYContext
&context
) {
950 CYExpression
*CYTypeVoid::Replace(CYContext
&context
) {
951 return $
N1($
V("Type"), $
CYString("v"));
954 CYExpression
*CYTypeVolatile::Replace_(CYContext
&context
, CYExpression
*type
) {
955 return next_
->Replace(context
, $
CYCall($
CYDirectMember(type
, $
CYString("volatile"))));
958 CYExpression
*CYTypedIdentifier::Replace(CYContext
&context
) {
959 return modifier_
->Replace(context
, specifier_
->Replace(context
));
962 CYTypeFunctionWith
*CYTypedIdentifier::Function() {
963 CYTypeModifier
**modifier(&modifier_
);
964 if (*modifier
== NULL
)
966 while ((*modifier
)->next_
!= NULL
)
967 modifier
= &(*modifier
)->next_
;
968 CYTypeFunctionWith
*function((*modifier
)->Function());
969 if (function
== NULL
)
975 CYArgument
*CYTypedParameter::Argument(CYContext
&context
) { $
T(NULL
)
976 return $
CYArgument(typed_
->Replace(context
), next_
->Argument(context
));
979 CYFunctionParameter
*CYTypedParameter::Parameters(CYContext
&context
) { $
T(NULL
)
980 return $
CYFunctionParameter($
CYDeclaration(typed_
->identifier_
?: context
.Unique()), next_
->Parameters(context
));
983 CYExpression
*CYTypedParameter::TypeSignature(CYContext
&context
, CYExpression
*prefix
) { $
T(prefix
)
984 return next_
->TypeSignature(context
, $
CYAdd(prefix
, typed_
->Replace(context
)));
987 CYStatement
*CYVar::Replace(CYContext
&context
) {
988 declarations_
->Replace(context
);
989 if (CYCompound
*compound
= declarations_
->Compound(context
))
994 CYExpression
*CYVariable::Replace(CYContext
&context
) {
995 context
.Replace(name_
);
999 CYStatement
*CYWhile::Replace(CYContext
&context
) {
1000 context
.Replace(test_
);
1001 context
.Replace(code_
);
1005 CYStatement
*CYWith::Replace(CYContext
&context
) {
1006 context
.Replace(scope_
);
1007 context
.Replace(code_
);
1011 CYExpression
*CYWord::ClassName(CYContext
&context
, bool object
) {
1012 CYString
*name($
S(this));
1014 return $
C1($
V("objc_getClass"), name
);