1 /* Cycript - Optimizing JavaScript Compiler/Runtime 
   2  * Copyright (C) 2009-2010  Jay Freeman (saurik) 
   5 /* GNU Lesser General Public License, Version 3 {{{ */ 
   7  * Cycript is free software: you can redistribute it and/or modify it under 
   8  * the terms of the GNU Lesser General Public License as published by the 
   9  * Free Software Foundation, either version 3 of the License, or (at your 
  10  * option) any later version. 
  12  * Cycript is distributed in the hope that it will be useful, but WITHOUT 
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
  15  * License for more details. 
  17  * You should have received a copy of the GNU Lesser General Public License 
  18  * along with Cycript.  If not, see <http://www.gnu.org/licenses/>. 
  23 #include "Replace.hpp" 
  27 CYFunctionExpression 
*CYNonLocalize(CYContext 
&context
, CYFunctionExpression 
*function
) { 
  28     function
->nonlocal_ 
= context
.nextlocal_
; 
  32 CYExpression 
*CYAdd::Replace(CYContext 
&context
) { 
  33     CYInfix::Replace(context
); 
  35     CYExpression 
*lhp(lhs_
->Primitive(context
)); 
  36     CYExpression 
*rhp(rhs_
->Primitive(context
)); 
  38     CYString 
*lhs(dynamic_cast<CYString 
*>(lhp
)); 
  39     CYString 
*rhs(dynamic_cast<CYString 
*>(rhp
)); 
  41     if (lhs 
!= NULL 
|| rhs 
!= NULL
) { 
  43             lhs 
= lhp
->String(context
); 
  46         } else if (rhs 
== NULL
) { 
  47             rhs 
= rhp
->String(context
); 
  52         return lhs
->Concat(context
, rhs
); 
  55     if (CYNumber 
*lhn 
= lhp
->Number(context
)) 
  56         if (CYNumber 
*rhn 
= rhp
->Number(context
)) 
  57             return $
D(lhn
->Value() + rhn
->Value()); 
  62 CYExpression 
*CYAddressOf::Replace(CYContext 
&context
) { 
  63     return $
C0($
M(rhs_
, $
S("$cya"))); 
  66 CYArgument 
*CYArgument::Replace(CYContext 
&context
) { $
T(NULL
) 
  67     context
.Replace(value_
); 
  68     next_ 
= next_
->Replace(context
); 
  80 CYExpression 
*CYArray::Replace(CYContext 
&context
) { 
  81     elements_
->Replace(context
); 
  85 CYExpression 
*CYArrayComprehension::Replace(CYContext 
&context
) { 
  86     CYVariable 
*cyv($
V("$cyv")); 
  88     return $
C0($
F(NULL
, $
P1("$cyv", comprehensions_
->Parameters(context
)), $$
->* 
  89         $
E($ 
CYAssign(cyv
, $ 
CYArray()))->* 
  90         comprehensions_
->Replace(context
, $
E($
C1($
M(cyv
, $
S("push")), expression_
)))->* 
  95 CYExpression 
*CYAssignment::Replace(CYContext 
&context
) { 
  96     context
.Replace(lhs_
); 
  97     context
.Replace(rhs_
); 
 101 CYStatement 
*CYBlock::Replace(CYContext 
&context
) { 
 102     context
.ReplaceAll(statements_
); 
 103     if (statements_ 
== NULL
) 
 108 CYStatement 
*CYBreak::Replace(CYContext 
&context
) { 
 112 CYExpression 
*CYCall::AddArgument(CYContext 
&context
, CYExpression 
*value
) { 
 113     CYArgument 
**argument(&arguments_
); 
 114     while (*argument 
!= NULL
) 
 115         argument 
= &(*argument
)->next_
; 
 116     *argument 
= $ 
CYArgument(value
); 
 120 CYExpression 
*CYCall::Replace(CYContext 
&context
) { 
 121     context
.Replace(function_
); 
 122     arguments_
->Replace(context
); 
 129 void Catch::Replace(CYContext 
&context
) { $
T() 
 130     CYScope 
scope(true, context
, code_
.statements_
); 
 132     context
.Replace(name_
); 
 133     context
.scope_
->Declare(context
, name_
, CYIdentifierCatch
); 
 135     code_
.Replace(context
); 
 141 void CYClause::Replace(CYContext 
&context
) { $
T() 
 142     context
.Replace(case_
); 
 143     context
.ReplaceAll(statements_
); 
 144     next_
->Replace(context
); 
 147 CYStatement 
*CYComment::Replace(CYContext 
&context
) { 
 151 CYExpression 
*CYCompound::Replace(CYContext 
&context
) { 
 152     context
.ReplaceAll(expressions_
); 
 153     if (expressions_ 
== NULL
) 
 158 CYFunctionParameter 
*CYComprehension::Parameters(CYContext 
&context
) const { $
T(NULL
) 
 159     CYFunctionParameter 
*next(next_
->Parameters(context
)); 
 160     if (CYFunctionParameter 
*parameter 
= Parameter(context
)) { 
 161         parameter
->SetNext(next
); 
 167 CYStatement 
*CYComprehension::Replace(CYContext 
&context
, CYStatement 
*statement
) const { 
 168     return next_ 
== NULL 
? statement 
: next_
->Replace(context
, statement
); 
 171 CYExpression 
*CYCondition::Replace(CYContext 
&context
) { 
 172     context
.Replace(test_
); 
 173     context
.Replace(true_
); 
 174     context
.Replace(false_
); 
 178 void CYContext::NonLocal(CYStatement 
*&statements
) { 
 179     CYContext 
&context(*this); 
 181     if (nextlocal_ 
!= NULL 
&& nextlocal_
->identifier_ 
!= NULL
) { 
 182         CYIdentifier 
*cye($
I("$cye")->Replace(context
)); 
 183         CYIdentifier 
*unique(nextlocal_
->identifier_
->Replace(context
)); 
 185         CYStatement 
*declare( 
 186             $ 
CYVar($
L1($
L(unique
, $ 
CYObject())))); 
 188         cy::Syntax::Catch 
*rescue( 
 189             $ 
cy::Syntax::Catch(cye
, $$
->* 
 190                 $ 
CYIf($ 
CYIdentical($
M($
V(cye
), $
S("$cyk")), $
V(unique
)), $$
->* 
 191                     $ 
CYReturn($
M($
V(cye
), $
S("$cyv"))))->* 
 192                 $ 
cy::Syntax::Throw($
V(cye
)))); 
 194         context
.Replace(declare
); 
 195         rescue
->Replace(context
); 
 199             $ 
cy::Syntax::Try(statements
, rescue
, NULL
); 
 203 CYIdentifier 
*CYContext::Unique() { 
 204     return $ 
CYIdentifier(apr_psprintf($pool
, "$cy%u", unique_
++)); 
 207 CYStatement 
*CYContinue::Replace(CYContext 
&context
) { 
 211 CYAssignment 
*CYDeclaration::Assignment(CYContext 
&context
) { 
 212     if (initialiser_ 
== NULL
) 
 215     CYAssignment 
*value($ 
CYAssign(Variable(context
), initialiser_
)); 
 220 CYVariable 
*CYDeclaration::Variable(CYContext 
&context
) { 
 221     return $
V(identifier_
); 
 224 CYStatement 
*CYDeclaration::ForEachIn(CYContext 
&context
, CYExpression 
*value
) { 
 225     return $ 
CYVar($
L1($
L(identifier_
, value
))); 
 228 CYExpression 
*CYDeclaration::Replace(CYContext 
&context
) { 
 229     context
.Replace(identifier_
); 
 230     context
.scope_
->Declare(context
, identifier_
, CYIdentifierVariable
); 
 231     return Variable(context
); 
 234 void CYDeclarations::Replace(CYContext 
&context
) { $
T() 
 235     declaration_
->Replace(context
); 
 236     next_
->Replace(context
); 
 239 CYProperty 
*CYDeclarations::Property(CYContext 
&context
) { $
T(NULL
) 
 240     return $ 
CYProperty(declaration_
->identifier_
, declaration_
->initialiser_
, next_
->Property(context
)); 
 243 CYFunctionParameter 
*CYDeclarations::Parameter(CYContext 
&context
) { $
T(NULL
) 
 244     return $ 
CYFunctionParameter(declaration_
->identifier_
, next_
->Parameter(context
)); 
 247 CYArgument 
*CYDeclarations::Argument(CYContext 
&context
) { $
T(NULL
) 
 248     return $ 
CYArgument(declaration_
->initialiser_
, next_
->Argument(context
)); 
 251 CYCompound 
*CYDeclarations::Compound(CYContext 
&context
) { $
T(NULL
) 
 252     CYCompound 
*compound(next_
->Compound(context
) ?: $ 
CYCompound()); 
 253     if (CYAssignment 
*assignment 
= declaration_
->Assignment(context
)) 
 254         compound
->AddPrev(assignment
); 
 258 CYExpression 
*CYDirectMember::Replace(CYContext 
&context
) { 
 259     context
.Replace(object_
); 
 260     context
.Replace(property_
); 
 264 CYStatement 
*CYDoWhile::Replace(CYContext 
&context
) { 
 265     context
.Replace(test_
); 
 266     context
.Replace(code_
); 
 270 void CYElement::Replace(CYContext 
&context
) { $
T() 
 271     context
.Replace(value_
); 
 272     next_
->Replace(context
); 
 275 CYStatement 
*CYEmpty::Replace(CYContext 
&context
) { 
 279 CYStatement 
*CYExpress::Replace(CYContext 
&context
) { 
 280     while (CYExpress 
*express 
= dynamic_cast<CYExpress 
*>(next_
)) { 
 281         CYCompound 
*compound(dynamic_cast<CYCompound 
*>(express
->expression_
)); 
 282         if (compound 
== NULL
) 
 283             compound 
= $ 
CYCompound(express
->expression_
); 
 284         compound
->AddPrev(expression_
); 
 285         expression_ 
= compound
; 
 286         SetNext(express
->next_
); 
 289     context
.Replace(expression_
); 
 290     if (expression_ 
== NULL
) 
 296 CYExpression 
*CYExpression::AddArgument(CYContext 
&context
, CYExpression 
*value
) { 
 297     return $
C1(this, value
); 
 300 CYExpression 
*CYExpression::ClassName(CYContext 
&context
, bool object
) { 
 304 CYStatement 
*CYExpression::ForEachIn(CYContext 
&context
, CYExpression 
*value
) { 
 305     return $
E($ 
CYAssign(this, value
)); 
 308 CYAssignment 
*CYExpression::Assignment(CYContext 
&context
) { 
 312 CYNumber 
*CYFalse::Number(CYContext 
&context
) { 
 316 CYString 
*CYFalse::String(CYContext 
&context
) { 
 320 void CYFinally::Replace(CYContext 
&context
) { $
T() 
 321     code_
.Replace(context
); 
 324 CYStatement 
*CYFor::Replace(CYContext 
&context
) { 
 325     context
.Replace(initialiser_
); 
 326     context
.Replace(test_
); 
 327     context
.Replace(increment_
); 
 328     context
.Replace(code_
); 
 332 CYCompound 
*CYForDeclarations::Replace(CYContext 
&context
) { 
 333     declarations_
->Replace(context
); 
 334     return declarations_
->Compound(context
); 
 337 // XXX: this still feels highly suboptimal 
 338 CYStatement 
*CYForIn::Replace(CYContext 
&context
) { 
 339     if (CYAssignment 
*assignment 
= initialiser_
->Assignment(context
)) 
 340         return $ 
CYBlock($$
->* 
 345     context
.Replace(initialiser_
); 
 346     context
.Replace(set_
); 
 347     context
.Replace(code_
); 
 351 CYFunctionParameter 
*CYForInComprehension::Parameter(CYContext 
&context
) const { 
 352     return $ 
CYFunctionParameter(name_
); 
 355 CYStatement 
*CYForInComprehension::Replace(CYContext 
&context
, CYStatement 
*statement
) const { 
 356     return $ 
CYForIn($
V(name_
), set_
, CYComprehension::Replace(context
, statement
)); 
 359 CYStatement 
*CYForEachIn::Replace(CYContext 
&context
) { 
 360     if (CYAssignment 
*assignment 
= initialiser_
->Assignment(context
)) 
 361         return $ 
CYBlock($$
->* 
 366     CYIdentifier 
*cys($
I("$cys")), *cyt($
I("$cyt")); 
 368     return $ 
CYLet($
L2($
L(cys
, set_
), $
L(cyt
)), $$
->* 
 369         $ 
CYForIn($
V(cyt
), $
V(cys
), $ 
CYBlock($$
->* 
 370             initialiser_
->ForEachIn(context
, $
M($
V(cys
), $
V(cyt
)))->* 
 376 CYFunctionParameter 
*CYForEachInComprehension::Parameter(CYContext 
&context
) const { 
 377     return $ 
CYFunctionParameter(name_
); 
 380 CYStatement 
*CYForEachInComprehension::Replace(CYContext 
&context
, CYStatement 
*statement
) const { 
 381     CYIdentifier 
*cys($
I("cys")); 
 383     return $
E($
C0($
F(NULL
, $
P1("$cys"), $$
->* 
 384         $
E($ 
CYAssign($
V(cys
), set_
))->* 
 385         $ 
CYForIn($
V(name_
), $
V(cys
), $ 
CYBlock($$
->* 
 386             $
E($ 
CYAssign($
V(name_
), $
M($
V(cys
), $
V(name_
))))->* 
 387             CYComprehension::Replace(context
, statement
) 
 392 void CYFunction::Inject(CYContext 
&context
) { 
 393     context
.Replace(name_
); 
 394     context
.scope_
->Declare(context
, name_
, CYIdentifierOther
); 
 397 void CYFunction::Replace_(CYContext 
&context
, bool outer
) { 
 401     CYNonLocal 
*nonlocal(context
.nonlocal_
); 
 402     CYNonLocal 
*nextlocal(context
.nextlocal_
); 
 405     if (nonlocal_ 
!= NULL
) { 
 407         context
.nonlocal_ 
= nonlocal_
; 
 410         nonlocal_ 
= $ 
CYNonLocal(); 
 411         context
.nextlocal_ 
= nonlocal_
; 
 414     CYScope 
scope(!localize
, context
, code_
.statements_
); 
 416     if (!outer 
&& name_ 
!= NULL
) 
 419     if (parameters_ 
!= NULL
) 
 420         parameters_ 
= parameters_
->Replace(context
, code_
); 
 422     code_
.Replace(context
); 
 425         context
.NonLocal(code_
.statements_
); 
 427     context
.nextlocal_ 
= nextlocal
; 
 428     context
.nonlocal_ 
= nonlocal
; 
 433 CYExpression 
*CYFunctionExpression::Replace(CYContext 
&context
) { 
 434     Replace_(context
, false); 
 438 CYFunctionParameter 
*CYFunctionParameter::Replace(CYContext 
&context
, CYBlock 
&code
) { 
 439     context
.Replace(name_
); 
 440     context
.scope_
->Declare(context
, name_
, CYIdentifierArgument
); 
 442         next_ 
= next_
->Replace(context
, code
); 
 446 CYStatement 
*CYFunctionStatement::Replace(CYContext 
&context
) { 
 447     Replace_(context
, true); 
 451 CYIdentifier 
*CYIdentifier::Replace(CYContext 
&context
) { 
 452     if (replace_ 
!= NULL 
&& replace_ 
!= this) 
 453         return replace_
->Replace(context
); 
 454     replace_ 
= context
.scope_
->Lookup(context
, this); 
 458 CYStatement 
*CYIf::Replace(CYContext 
&context
) { 
 459     context
.Replace(test_
); 
 460     context
.Replace(true_
); 
 461     context
.Replace(false_
); 
 465 CYFunctionParameter 
*CYIfComprehension::Parameter(CYContext 
&context
) const { 
 469 CYStatement 
*CYIfComprehension::Replace(CYContext 
&context
, CYStatement 
*statement
) const { 
 470     return $ 
CYIf(test_
, CYComprehension::Replace(context
, statement
)); 
 473 CYExpression 
*CYIndirect::Replace(CYContext 
&context
) { 
 474     return $
M(rhs_
, $
S("$cyi")); 
 477 CYExpression 
*CYIndirectMember::Replace(CYContext 
&context
) { 
 478     return $
M($ 
CYIndirect(object_
), property_
); 
 481 CYExpression 
*CYInfix::Replace(CYContext 
&context
) { 
 482     context
.Replace(lhs_
); 
 483     context
.Replace(rhs_
); 
 487 CYStatement 
*CYLabel::Replace(CYContext 
&context
) { 
 488     context
.Replace(statement_
); 
 492 CYStatement 
*CYLet::Replace(CYContext 
&context
) { 
 493     return $
E($ 
CYCall(CYNonLocalize(context
, $ 
CYFunctionExpression(NULL
, declarations_
->Parameter(context
), code_
)), declarations_
->Argument(context
))); 
 499 CYExpression 
*New::AddArgument(CYContext 
&context
, CYExpression 
*value
) { 
 500     CYSetLast(arguments_
, $ 
CYArgument(value
)); 
 504 CYExpression 
*New::Replace(CYContext 
&context
) { 
 505     context
.Replace(constructor_
); 
 506     arguments_
->Replace(context
); 
 512 CYNumber 
*CYNull::Number(CYContext 
&context
) { 
 516 CYString 
*CYNull::String(CYContext 
&context
) { 
 520 CYNumber 
*CYNumber::Number(CYContext 
&context
) { 
 524 CYString 
*CYNumber::String(CYContext 
&context
) { 
 525     // XXX: there is a precise algorithm for this 
 526     return $
S(apr_psprintf($pool
, "%.17g", Value())); 
 529 CYExpression 
*CYObject::Replace(CYContext 
&context
) { 
 530     properties_
->Replace(context
); 
 534 CYFunctionParameter 
*CYOptionalFunctionParameter::Replace(CYContext 
&context
, CYBlock 
&code
) { 
 535     CYFunctionParameter 
*parameter($ 
CYFunctionParameter(name_
, next_
)); 
 536     parameter 
= parameter
->Replace(context
, code
); 
 537     context
.Replace(initializer_
); 
 539     CYVariable 
*name($
V(name_
)); 
 540     code
.AddPrev($ 
CYIf($ 
CYIdentical($ 
CYTypeOf(name
), $
S("undefined")), $$
->* 
 541         $
E($ 
CYAssign(name
, initializer_
)) 
 547 CYExpression 
*CYPostfix::Replace(CYContext 
&context
) { 
 548     context
.Replace(lhs_
); 
 552 CYExpression 
*CYPrefix::Replace(CYContext 
&context
) { 
 553     context
.Replace(rhs_
); 
 557 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe! 
 558 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ" 
 559 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_" 
 562     struct IdentifierUsageLess 
: 
 563         std::binary_function
<CYIdentifier 
*, CYIdentifier 
*, bool> 
 565         _finline 
bool operator ()(CYIdentifier 
*lhs
, CYIdentifier 
*rhs
) const { 
 566             if (lhs
->usage_ 
!= rhs
->usage_
) 
 567                 return lhs
->usage_ 
> rhs
->usage_
; 
 572     typedef std::set
<CYIdentifier 
*, IdentifierUsageLess
> IdentifierUsages
; 
 575 void CYProgram::Replace(CYContext 
&context
) { 
 576     CYScope 
scope(true, context
, statements_
); 
 578     context
.nextlocal_ 
= $ 
CYNonLocal(); 
 579     context
.ReplaceAll(statements_
); 
 580     context
.NonLocal(statements_
); 
 586     CYCStringSet external
; 
 587     for (CYIdentifierValueSet::const_iterator 
i(scope
.identifiers_
.begin()); i 
!= scope
.identifiers_
.end(); ++i
) 
 588         external
.insert((*i
)->Word()); 
 590     IdentifierUsages usages
; 
 592     if (offset 
< context
.rename_
.size()) 
 593         CYForEach (i
, context
.rename_
[offset
].identifier_
) 
 596     // XXX: totalling the probable occurrences and sorting by them would improve the result 
 597     for (CYIdentifierUsageVector::const_iterator 
i(context
.rename_
.begin()); i 
!= context
.rename_
.end(); ++i
, ++offset
) { 
 598         //std::cout << *i << ":" << (*i)->offset_ << std::endl; 
 602         if (context
.options_
.verbose_
) 
 603             name 
= apr_psprintf($pool
, "$%"APR_SIZE_T_FMT
"", offset
); 
 609             unsigned position(7), local(offset 
+ 1); 
 612                 unsigned index(local 
% (sizeof(MappingSet
) - 1)); 
 613                 local 
/= sizeof(MappingSet
) - 1; 
 614                 id
[--position
] = MappingSet
[index
]; 
 615             } while (local 
!= 0); 
 617             if (external
.find(id 
+ position
) != external
.end()) { 
 622             name 
= apr_pstrmemdup($pool
, id 
+ position
, 7 - position
); 
 623             // XXX: at some point, this could become a keyword 
 626         CYForEach (identifier
, i
->identifier_
) 
 627             identifier
->Set(name
); 
 631 void CYProperty::Replace(CYContext 
&context
) { $
T() 
 632     context
.Replace(value_
); 
 633     next_
->Replace(context
); 
 638 CYStatement 
*CYReturn::Replace(CYContext 
&context
) { 
 639     if (context
.nonlocal_ 
!= NULL
) { 
 640         CYProperty 
*value(value_ 
== NULL 
? NULL 
: $ 
CYProperty($
S("$cyv"), value_
)); 
 641         return $ 
cy::Syntax::Throw($ 
CYObject( 
 642             $ 
CYProperty($
S("$cyk"), $
V(context
.nonlocal_
->Target(context
)), value
) 
 646     context
.Replace(value_
); 
 650 CYExpression 
*CYRubyBlock::Replace(CYContext 
&context
) { 
 651     // XXX: this needs to do something much more epic to handle return 
 652     return call_
->AddArgument(context
, proc_
->Replace(context
)); 
 655 CYExpression 
*CYRubyProc::Replace(CYContext 
&context
) { 
 656     return CYNonLocalize(context
, $ 
CYFunctionExpression(NULL
, parameters_
, code_
)); 
 659 CYScope::CYScope(bool transparent
, CYContext 
&context
, CYStatement 
*&statements
) : 
 660     transparent_(transparent
), 
 662     statements_(statements
), 
 663     parent_(context
.scope_
) 
 665     context_
.scope_ 
= this; 
 668 CYScope::~CYScope() { 
 671 void CYScope::Close() { 
 672     context_
.scope_ 
= parent_
; 
 673     Scope(context_
, statements_
); 
 676 void CYScope::Declare(CYContext 
&context
, CYIdentifier 
*identifier
, CYIdentifierFlags flags
) { 
 677     if (!transparent_ 
|| flags 
== CYIdentifierArgument 
|| flags 
== CYIdentifierCatch
) 
 678         internal_
.insert(CYIdentifierAddressFlagsMap::value_type(identifier
, flags
)); 
 679     else if (parent_ 
!= NULL
) 
 680         parent_
->Declare(context
, identifier
, flags
); 
 683 CYIdentifier 
*CYScope::Lookup(CYContext 
&context
, CYIdentifier 
*identifier
) { 
 684     std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
)); 
 685     return *insert
.first
; 
 688 void CYScope::Merge(CYContext 
&context
, CYIdentifier 
*identifier
) { 
 689     std::pair
<CYIdentifierValueSet::iterator
, bool> insert(identifiers_
.insert(identifier
)); 
 690     if (!insert
.second
) { 
 691         if ((*insert
.first
)->offset_ 
< identifier
->offset_
) 
 692             (*insert
.first
)->offset_ 
= identifier
->offset_
; 
 693         identifier
->replace_ 
= *insert
.first
; 
 694         (*insert
.first
)->usage_ 
+= identifier
->usage_ 
+ 1; 
 699     struct IdentifierOffset 
{ 
 701         CYIdentifierFlags flags_
; 
 703         CYIdentifier 
*identifier_
; 
 705         IdentifierOffset(CYIdentifier 
*identifier
, CYIdentifierFlags flags
) : 
 706             offset_(identifier
->offset_
), 
 708             usage_(identifier
->usage_
), 
 709             identifier_(identifier
) 
 714     struct IdentifierOffsetLess 
: 
 715         std::binary_function
<const IdentifierOffset 
&, const IdentifierOffset 
&, bool> 
 717         _finline 
bool operator ()(const IdentifierOffset 
&lhs
, const IdentifierOffset 
&rhs
) const { 
 718             if (lhs
.offset_ 
!= rhs
.offset_
) 
 719                 return lhs
.offset_ 
< rhs
.offset_
; 
 720             if (lhs
.flags_ 
!= rhs
.flags_
) 
 721                 return lhs
.flags_ 
< rhs
.flags_
; 
 722             /*if (lhs.usage_ != rhs.usage_) 
 723                 return lhs.usage_ < rhs.usage_;*/ 
 724             return lhs
.identifier_ 
< rhs
.identifier_
; 
 728     typedef std::set
<IdentifierOffset
, IdentifierOffsetLess
> IdentifierOffsets
; 
 731 void CYScope::Scope(CYContext 
&context
, CYStatement 
*&statements
) { 
 735     CYDeclarations 
*last(NULL
), *curr(NULL
); 
 737     IdentifierOffsets offsets
; 
 739     for (CYIdentifierAddressFlagsMap::const_iterator 
i(internal_
.begin()); i 
!= internal_
.end(); ++i
) 
 740         if (i
->second 
!= CYIdentifierMagic
) 
 741             offsets
.insert(IdentifierOffset(i
->first
, i
->second
)); 
 745     for (IdentifierOffsets::const_iterator 
i(offsets
.begin()); i 
!= offsets
.end(); ++i
) { 
 746         if (i
->flags_ 
== CYIdentifierVariable
) { 
 747             CYDeclarations 
*next($ 
CYDeclarations($ 
CYDeclaration(i
->identifier_
))); 
 755         if (offset 
< i
->offset_
) 
 757         if (context
.rename_
.size() <= offset
) 
 758             context
.rename_
.resize(offset 
+ 1); 
 760         CYIdentifierUsage 
&rename(context
.rename_
[offset
++]); 
 761         i
->identifier_
->SetNext(rename
.identifier_
); 
 762         rename
.identifier_ 
= i
->identifier_
; 
 763         rename
.usage_ 
+= i
->identifier_
->usage_ 
+ 1; 
 767         CYVar 
*var($ 
CYVar(last
)); 
 768         var
->SetNext(statements
); 
 772     for (CYIdentifierValueSet::const_iterator 
i(identifiers_
.begin()); i 
!= identifiers_
.end(); ++i
) 
 773         if (internal_
.find(*i
) == internal_
.end()) { 
 774             //std::cout << *i << '=' << offset << std::endl; 
 775             if ((*i
)->offset_ 
< offset
) 
 776                 (*i
)->offset_ 
= offset
; 
 777             parent_
->Merge(context
, *i
); 
 781 CYString 
*CYString::Concat(CYContext 
&context
, CYString 
*rhs
) const { 
 782     size_t size(size_ 
+ rhs
->size_
); 
 783     char *value($ 
char[size 
+ 1]); 
 784     memcpy(value
, value_
, size_
); 
 785     memcpy(value 
+ size_
, rhs
->value_
, rhs
->size_
); 
 787     return $
S(value
, size
); 
 790 CYNumber 
*CYString::Number(CYContext 
&context
) { 
 791     // XXX: there is a precise algorithm for this 
 795 CYString 
*CYString::String(CYContext 
&context
) { 
 799 CYStatement 
*CYSwitch::Replace(CYContext 
&context
) { 
 800     context
.Replace(value_
); 
 801     clauses_
->Replace(context
); 
 805 CYExpression 
*CYThis::Replace(CYContext 
&context
) { 
 812 CYStatement 
*Throw::Replace(CYContext 
&context
) { 
 813     context
.Replace(value_
); 
 819 CYExpression 
*CYTrivial::Replace(CYContext 
&context
) { 
 823 CYNumber 
*CYTrue::Number(CYContext 
&context
) { 
 827 CYString 
*CYTrue::String(CYContext 
&context
) { 
 834 CYStatement 
*Try::Replace(CYContext 
&context
) { 
 835     code_
.Replace(context
); 
 836     catch_
->Replace(context
); 
 837     finally_
->Replace(context
); 
 843 CYStatement 
*CYVar::Replace(CYContext 
&context
) { 
 844     declarations_
->Replace(context
); 
 845     return $
E(declarations_
->Compound(context
)); 
 848 CYExpression 
*CYVariable::Replace(CYContext 
&context
) { 
 849     context
.Replace(name_
); 
 853 CYStatement 
*CYWhile::Replace(CYContext 
&context
) { 
 854     context
.Replace(test_
); 
 855     context
.Replace(code_
); 
 859 CYStatement 
*CYWith::Replace(CYContext 
&context
) { 
 860     context
.Replace(scope_
); 
 861     context
.Replace(code_
); 
 865 CYExpression 
*CYWord::ClassName(CYContext 
&context
, bool object
) { 
 866     CYString 
*name($
S(this)); 
 868         return $
C1($
V("objc_getClass"), name
);