]> git.saurik.com Git - cycript.git/blob - Replace.cpp
5be4e01b718f413f6f9bae136a6cdcb3e6e4ed35
[cycript.git] / Replace.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
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.
11 *
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.
16 *
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/>.
19 **/
20 /* }}} */
21
22 #include "Parser.hpp"
23 #include "Replace.hpp"
24
25 #include <iomanip>
26
27 CYFunctionExpression *CYNonLocalize(CYContext &context, CYFunctionExpression *function) {
28 function->nonlocal_ = context.nextlocal_;
29 return function;
30 }
31
32 CYExpression *CYAdd::Replace(CYContext &context) {
33 CYInfix::Replace(context);
34
35 CYExpression *lhp(lhs_->Primitive(context));
36 CYExpression *rhp(rhs_->Primitive(context));
37
38 CYString *lhs(dynamic_cast<CYString *>(lhp));
39 CYString *rhs(dynamic_cast<CYString *>(rhp));
40
41 if (lhs != NULL || rhs != NULL) {
42 if (lhs == NULL) {
43 lhs = lhp->String(context);
44 if (lhs == NULL)
45 return this;
46 } else if (rhs == NULL) {
47 rhs = rhp->String(context);
48 if (rhs == NULL)
49 return this;
50 }
51
52 return lhs->Concat(context, rhs);
53 }
54
55 if (CYNumber *lhn = lhp->Number(context))
56 if (CYNumber *rhn = rhp->Number(context))
57 return $D(lhn->Value() + rhn->Value());
58
59 return this;
60 }
61
62 CYExpression *CYAddressOf::Replace(CYContext &context) {
63 return $C0($M(rhs_, $S("$cya")));
64 }
65
66 CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
67 context.Replace(value_);
68 next_ = next_->Replace(context);
69
70 if (value_ == NULL) {
71 if (next_ == NULL)
72 return NULL;
73 else
74 value_ = $U;
75 }
76
77 return this;
78 }
79
80 CYExpression *CYArray::Replace(CYContext &context) {
81 elements_->Replace(context);
82 return this;
83 }
84
85 CYExpression *CYArrayComprehension::Replace(CYContext &context) {
86 CYVariable *cyv($V("$cyv"));
87
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_)))->*
91 $ CYReturn(cyv)
92 ));
93 }
94
95 CYExpression *CYAssignment::Replace(CYContext &context) {
96 context.Replace(lhs_);
97 context.Replace(rhs_);
98 return this;
99 }
100
101 CYStatement *CYBlock::Replace(CYContext &context) {
102 context.ReplaceAll(statements_);
103 if (statements_ == NULL)
104 return $ CYEmpty();
105 return this;
106 }
107
108 CYStatement *CYBreak::Replace(CYContext &context) {
109 return this;
110 }
111
112 CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
113 CYArgument **argument(&arguments_);
114 while (*argument != NULL)
115 argument = &(*argument)->next_;
116 *argument = $ CYArgument(value);
117 return this;
118 }
119
120 CYExpression *CYCall::Replace(CYContext &context) {
121 context.Replace(function_);
122 arguments_->Replace(context);
123 return this;
124 }
125
126 namespace cy {
127 namespace Syntax {
128
129 void Catch::Replace(CYContext &context) { $T()
130 CYScope scope(true, context, code_.statements_);
131
132 context.Replace(name_);
133 context.scope_->Declare(context, name_, CYIdentifierCatch);
134
135 code_.Replace(context);
136 scope.Close();
137 }
138
139 } }
140
141 void CYClause::Replace(CYContext &context) { $T()
142 context.Replace(case_);
143 context.ReplaceAll(statements_);
144 next_->Replace(context);
145 }
146
147 CYStatement *CYComment::Replace(CYContext &context) {
148 return this;
149 }
150
151 CYExpression *CYCompound::Replace(CYContext &context) {
152 context.ReplaceAll(expressions_);
153 if (expressions_ == NULL)
154 return NULL;
155 return this;
156 }
157
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);
162 return parameter;
163 } else
164 return next;
165 }
166
167 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
168 return next_ == NULL ? statement : next_->Replace(context, statement);
169 }
170
171 CYExpression *CYCondition::Replace(CYContext &context) {
172 context.Replace(test_);
173 context.Replace(true_);
174 context.Replace(false_);
175 return this;
176 }
177
178 void CYContext::NonLocal(CYStatement *&statements) {
179 CYContext &context(*this);
180
181 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
182 CYIdentifier *cye($I("$cye")->Replace(context));
183 CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
184
185 CYStatement *declare(
186 $ CYVar($L1($L(unique, $ CYObject()))));
187
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))));
193
194 context.Replace(declare);
195 rescue->Replace(context);
196
197 statements = $$->*
198 declare->*
199 $ cy::Syntax::Try(statements, rescue, NULL);
200 }
201 }
202
203 CYIdentifier *CYContext::Unique() {
204 return $ CYIdentifier(apr_psprintf($pool, "$cy%u", unique_++));
205 }
206
207 CYStatement *CYContinue::Replace(CYContext &context) {
208 return this;
209 }
210
211 CYAssignment *CYDeclaration::Assignment(CYContext &context) {
212 if (initialiser_ == NULL)
213 return NULL;
214
215 CYAssignment *value($ CYAssign(Variable(context), initialiser_));
216 initialiser_ = NULL;
217 return value;
218 }
219
220 CYVariable *CYDeclaration::Variable(CYContext &context) {
221 return $V(identifier_);
222 }
223
224 CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
225 return $ CYVar($L1($L(identifier_, value)));
226 }
227
228 CYExpression *CYDeclaration::Replace(CYContext &context) {
229 context.Replace(identifier_);
230 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
231 return Variable(context);
232 }
233
234 void CYDeclarations::Replace(CYContext &context) { $T()
235 declaration_->Replace(context);
236 next_->Replace(context);
237 }
238
239 CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
240 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_, next_->Property(context));
241 }
242
243 CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
244 return $ CYFunctionParameter(declaration_->identifier_, next_->Parameter(context));
245 }
246
247 CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
248 return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
249 }
250
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);
255 return compound;
256 }
257
258 CYExpression *CYDirectMember::Replace(CYContext &context) {
259 context.Replace(object_);
260 context.Replace(property_);
261 return this;
262 }
263
264 CYStatement *CYDoWhile::Replace(CYContext &context) {
265 context.Replace(test_);
266 context.Replace(code_);
267 return this;
268 }
269
270 void CYElement::Replace(CYContext &context) { $T()
271 context.Replace(value_);
272 next_->Replace(context);
273 }
274
275 CYStatement *CYEmpty::Replace(CYContext &context) {
276 return NULL;
277 }
278
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_);
287 }
288
289 context.Replace(expression_);
290 if (expression_ == NULL)
291 return $ CYEmpty();
292
293 return this;
294 }
295
296 CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
297 return $C1(this, value);
298 }
299
300 CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
301 return this;
302 }
303
304 CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
305 return $E($ CYAssign(this, value));
306 }
307
308 CYAssignment *CYExpression::Assignment(CYContext &context) {
309 return NULL;
310 }
311
312 CYNumber *CYFalse::Number(CYContext &context) {
313 return $D(0);
314 }
315
316 CYString *CYFalse::String(CYContext &context) {
317 return $S("false");
318 }
319
320 void CYFinally::Replace(CYContext &context) { $T()
321 code_.Replace(context);
322 }
323
324 CYStatement *CYFor::Replace(CYContext &context) {
325 context.Replace(initialiser_);
326 context.Replace(test_);
327 context.Replace(increment_);
328 context.Replace(code_);
329 return this;
330 }
331
332 CYCompound *CYForDeclarations::Replace(CYContext &context) {
333 declarations_->Replace(context);
334 return declarations_->Compound(context);
335 }
336
337 // XXX: this still feels highly suboptimal
338 CYStatement *CYForIn::Replace(CYContext &context) {
339 if (CYAssignment *assignment = initialiser_->Assignment(context))
340 return $ CYBlock($$->*
341 $E(assignment)->*
342 this
343 );
344
345 context.Replace(initialiser_);
346 context.Replace(set_);
347 context.Replace(code_);
348 return this;
349 }
350
351 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
352 return $ CYFunctionParameter(name_);
353 }
354
355 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
356 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
357 }
358
359 CYStatement *CYForEachIn::Replace(CYContext &context) {
360 if (CYAssignment *assignment = initialiser_->Assignment(context))
361 return $ CYBlock($$->*
362 $E(assignment)->*
363 this
364 );
365
366 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
367
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)))->*
371 code_
372 ))
373 );
374 }
375
376 CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
377 return $ CYFunctionParameter(name_);
378 }
379
380 CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
381 CYIdentifier *cys($I("cys"));
382
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)
388 ))
389 )));
390 }
391
392 void CYFunction::Inject(CYContext &context) {
393 context.Replace(name_);
394 context.scope_->Declare(context, name_, CYIdentifierOther);
395 }
396
397 void CYFunction::Replace_(CYContext &context, bool outer) {
398 if (outer)
399 Inject(context);
400
401 CYNonLocal *nonlocal(context.nonlocal_);
402 CYNonLocal *nextlocal(context.nextlocal_);
403
404 bool localize;
405 if (nonlocal_ != NULL) {
406 localize = false;
407 context.nonlocal_ = nonlocal_;
408 } else {
409 localize = true;
410 nonlocal_ = $ CYNonLocal();
411 context.nextlocal_ = nonlocal_;
412 }
413
414 CYScope scope(!localize, context, code_.statements_);
415
416 if (!outer && name_ != NULL)
417 Inject(context);
418
419 if (parameters_ != NULL)
420 parameters_ = parameters_->Replace(context, code_);
421
422 code_.Replace(context);
423
424 if (localize)
425 context.NonLocal(code_.statements_);
426
427 context.nextlocal_ = nextlocal;
428 context.nonlocal_ = nonlocal;
429
430 scope.Close();
431 }
432
433 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
434 Replace_(context, false);
435 return this;
436 }
437
438 CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) {
439 context.Replace(name_);
440 context.scope_->Declare(context, name_, CYIdentifierArgument);
441 if (next_ != NULL)
442 next_ = next_->Replace(context, code);
443 return this;
444 }
445
446 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
447 Replace_(context, true);
448 return this;
449 }
450
451 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
452 if (replace_ != NULL && replace_ != this)
453 return replace_->Replace(context);
454 replace_ = context.scope_->Lookup(context, this);
455 return replace_;
456 }
457
458 CYStatement *CYIf::Replace(CYContext &context) {
459 context.Replace(test_);
460 context.Replace(true_);
461 context.Replace(false_);
462 return this;
463 }
464
465 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
466 return NULL;
467 }
468
469 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
470 return $ CYIf(test_, CYComprehension::Replace(context, statement));
471 }
472
473 CYExpression *CYIndirect::Replace(CYContext &context) {
474 return $M(rhs_, $S("$cyi"));
475 }
476
477 CYExpression *CYIndirectMember::Replace(CYContext &context) {
478 return $M($ CYIndirect(object_), property_);
479 }
480
481 CYExpression *CYInfix::Replace(CYContext &context) {
482 context.Replace(lhs_);
483 context.Replace(rhs_);
484 return this;
485 }
486
487 CYStatement *CYLabel::Replace(CYContext &context) {
488 context.Replace(statement_);
489 return this;
490 }
491
492 CYStatement *CYLet::Replace(CYContext &context) {
493 return $E($ CYCall(CYNonLocalize(context, $ CYFunctionExpression(NULL, declarations_->Parameter(context), code_)), declarations_->Argument(context)));
494 }
495
496 namespace cy {
497 namespace Syntax {
498
499 CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
500 CYSetLast(arguments_, $ CYArgument(value));
501 return this;
502 }
503
504 CYExpression *New::Replace(CYContext &context) {
505 context.Replace(constructor_);
506 arguments_->Replace(context);
507 return this;
508 }
509
510 } }
511
512 CYNumber *CYNull::Number(CYContext &context) {
513 return $D(0);
514 }
515
516 CYString *CYNull::String(CYContext &context) {
517 return $S("null");
518 }
519
520 CYNumber *CYNumber::Number(CYContext &context) {
521 return this;
522 }
523
524 CYString *CYNumber::String(CYContext &context) {
525 // XXX: there is a precise algorithm for this
526 return $S(apr_psprintf($pool, "%.17g", Value()));
527 }
528
529 CYExpression *CYObject::Replace(CYContext &context) {
530 properties_->Replace(context);
531 return this;
532 }
533
534 CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) {
535 CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_));
536 parameter = parameter->Replace(context, code);
537 context.Replace(initializer_);
538
539 CYVariable *name($V(name_));
540 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->*
541 $E($ CYAssign(name, initializer_))
542 ));
543
544 return parameter;
545 }
546
547 CYExpression *CYPostfix::Replace(CYContext &context) {
548 context.Replace(lhs_);
549 return this;
550 }
551
552 CYExpression *CYPrefix::Replace(CYContext &context) {
553 context.Replace(rhs_);
554 return this;
555 }
556
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$_"
560
561 namespace {
562 struct IdentifierUsageLess :
563 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
564 {
565 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
566 if (lhs->usage_ != rhs->usage_)
567 return lhs->usage_ > rhs->usage_;
568 return lhs < rhs;
569 }
570 };
571
572 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
573 }
574
575 void CYProgram::Replace(CYContext &context) {
576 CYScope scope(true, context, statements_);
577
578 context.nextlocal_ = $ CYNonLocal();
579 context.ReplaceAll(statements_);
580 context.NonLocal(statements_);
581
582 scope.Close();
583
584 size_t offset(0);
585
586 CYCStringSet external;
587 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
588 external.insert((*i)->Word());
589
590 IdentifierUsages usages;
591
592 if (offset < context.rename_.size())
593 CYForEach (i, context.rename_[offset].identifier_)
594 usages.insert(i);
595
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;
599
600 const char *name;
601
602 if (context.options_.verbose_)
603 name = apr_psprintf($pool, "$%"APR_SIZE_T_FMT"", offset);
604 else {
605 char id[8];
606 id[7] = '\0';
607
608 id:
609 unsigned position(7), local(offset + 1);
610
611 do {
612 unsigned index(local % (sizeof(MappingSet) - 1));
613 local /= sizeof(MappingSet) - 1;
614 id[--position] = MappingSet[index];
615 } while (local != 0);
616
617 if (external.find(id + position) != external.end()) {
618 ++offset;
619 goto id;
620 }
621
622 name = apr_pstrmemdup($pool, id + position, 7 - position);
623 // XXX: at some point, this could become a keyword
624 }
625
626 CYForEach (identifier, i->identifier_)
627 identifier->Set(name);
628 }
629 }
630
631 void CYProperty::Replace(CYContext &context) { $T()
632 context.Replace(value_);
633 next_->Replace(context);
634 if (value_ == NULL)
635 value_ = $U;
636 }
637
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)
643 ));
644 }
645
646 context.Replace(value_);
647 return this;
648 }
649
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));
653 }
654
655 CYExpression *CYRubyProc::Replace(CYContext &context) {
656 return CYNonLocalize(context, $ CYFunctionExpression(NULL, parameters_, code_));
657 }
658
659 CYScope::CYScope(bool transparent, CYContext &context, CYStatement *&statements) :
660 transparent_(transparent),
661 context_(context),
662 statements_(statements),
663 parent_(context.scope_)
664 {
665 context_.scope_ = this;
666 }
667
668 CYScope::~CYScope() {
669 }
670
671 void CYScope::Close() {
672 context_.scope_ = parent_;
673 Scope(context_, statements_);
674 }
675
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);
681 }
682
683 CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
684 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
685 return *insert.first;
686 }
687
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;
695 }
696 }
697
698 namespace {
699 struct IdentifierOffset {
700 size_t offset_;
701 CYIdentifierFlags flags_;
702 size_t usage_;
703 CYIdentifier *identifier_;
704
705 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
706 offset_(identifier->offset_),
707 flags_(flags),
708 usage_(identifier->usage_),
709 identifier_(identifier)
710 {
711 }
712 };
713
714 struct IdentifierOffsetLess :
715 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
716 {
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_;
725 }
726 };
727
728 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
729 }
730
731 void CYScope::Scope(CYContext &context, CYStatement *&statements) {
732 if (parent_ == NULL)
733 return;
734
735 CYDeclarations *last(NULL), *curr(NULL);
736
737 IdentifierOffsets offsets;
738
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));
742
743 size_t offset(0);
744
745 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
746 if (i->flags_ == CYIdentifierVariable) {
747 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
748 if (last == NULL)
749 last = next;
750 if (curr != NULL)
751 curr->SetNext(next);
752 curr = next;
753 }
754
755 if (offset < i->offset_)
756 offset = i->offset_;
757 if (context.rename_.size() <= offset)
758 context.rename_.resize(offset + 1);
759
760 CYIdentifierUsage &rename(context.rename_[offset++]);
761 i->identifier_->SetNext(rename.identifier_);
762 rename.identifier_ = i->identifier_;
763 rename.usage_ += i->identifier_->usage_ + 1;
764 }
765
766 if (last != NULL) {
767 CYVar *var($ CYVar(last));
768 var->SetNext(statements);
769 statements = var;
770 }
771
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);
778 }
779 }
780
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_);
786 value[size] = '\0';
787 return $S(value, size);
788 }
789
790 CYNumber *CYString::Number(CYContext &context) {
791 // XXX: there is a precise algorithm for this
792 return NULL;
793 }
794
795 CYString *CYString::String(CYContext &context) {
796 return this;
797 }
798
799 CYStatement *CYSwitch::Replace(CYContext &context) {
800 context.Replace(value_);
801 clauses_->Replace(context);
802 return this;
803 }
804
805 CYExpression *CYThis::Replace(CYContext &context) {
806 return this;
807 }
808
809 namespace cy {
810 namespace Syntax {
811
812 CYStatement *Throw::Replace(CYContext &context) {
813 context.Replace(value_);
814 return this;
815 }
816
817 } }
818
819 CYExpression *CYTrivial::Replace(CYContext &context) {
820 return this;
821 }
822
823 CYNumber *CYTrue::Number(CYContext &context) {
824 return $D(1);
825 }
826
827 CYString *CYTrue::String(CYContext &context) {
828 return $S("true");
829 }
830
831 namespace cy {
832 namespace Syntax {
833
834 CYStatement *Try::Replace(CYContext &context) {
835 code_.Replace(context);
836 catch_->Replace(context);
837 finally_->Replace(context);
838 return this;
839 }
840
841 } }
842
843 CYStatement *CYVar::Replace(CYContext &context) {
844 declarations_->Replace(context);
845 return $E(declarations_->Compound(context));
846 }
847
848 CYExpression *CYVariable::Replace(CYContext &context) {
849 context.Replace(name_);
850 return this;
851 }
852
853 CYStatement *CYWhile::Replace(CYContext &context) {
854 context.Replace(test_);
855 context.Replace(code_);
856 return this;
857 }
858
859 CYStatement *CYWith::Replace(CYContext &context) {
860 context.Replace(scope_);
861 context.Replace(code_);
862 return this;
863 }
864
865 CYExpression *CYWord::ClassName(CYContext &context, bool object) {
866 CYString *name($S(this));
867 if (object)
868 return $C1($V("objc_getClass"), name);
869 else
870 return name;
871 }