]> git.saurik.com Git - cycript.git/blob - Replace.cpp
9cbd30fbbe86c958e67a6dfb5401bcb3dc24cbd8
[cycript.git] / Replace.cpp
1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /* }}} */
39
40 #include "Parser.hpp"
41 #include "Replace.hpp"
42
43 #include <iomanip>
44
45 CYExpression *CYAdd::Replace(CYContext &context) {
46 CYInfix::Replace(context);
47
48 CYExpression *lhp(lhs_->Primitive(context));
49 CYExpression *rhp(rhs_->Primitive(context));
50
51 CYString *lhs(dynamic_cast<CYString *>(lhp));
52 CYString *rhs(dynamic_cast<CYString *>(rhp));
53
54 if (lhs != NULL || rhs != NULL) {
55 if (lhs == NULL) {
56 lhs = lhp->String(context);
57 if (lhs == NULL)
58 return this;
59 } else if (rhs == NULL) {
60 rhs = rhp->String(context);
61 if (rhs == NULL)
62 return this;
63 }
64
65 return lhs->Concat(context, rhs);
66 }
67
68 if (CYNumber *lhn = lhp->Number(context))
69 if (CYNumber *rhn = rhp->Number(context))
70 return $D(lhn->Value() + rhn->Value());
71
72 return this;
73 }
74
75 CYExpression *CYAddressOf::Replace(CYContext &context) {
76 CYPrefix::Replace(context);
77 return $C0($M(rhs_, $S("$cya")));
78 }
79
80 void CYArgument::Replace(CYContext &context) { $T()
81 context.Replace(value_);
82 next_->Replace(context);
83 }
84
85 CYExpression *CYArray::Replace(CYContext &context) {
86 elements_->Replace(context);
87 return this;
88 }
89
90 CYExpression *CYArrayComprehension::Replace(CYContext &context) {
91 CYVariable *cyv($V("$cyv"));
92
93 return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->*
94 $E($ CYAssign(cyv, $ CYArray()))->*
95 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
96 $ CYReturn(cyv)
97 ));
98 }
99
100 CYExpression *CYAssignment::Replace(CYContext &context) {
101 context.Replace(lhs_);
102 context.Replace(rhs_);
103 return this;
104 }
105
106 CYStatement *CYBlock::Replace(CYContext &context) {
107 statements_ = statements_->ReplaceAll(context);
108 if (statements_ == NULL)
109 return $ CYEmpty();
110 return this;
111 }
112
113 CYStatement *CYBreak::Replace(CYContext &context) {
114 return this;
115 }
116
117 CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
118 CYArgument **argument(&arguments_);
119 while (*argument != NULL)
120 argument = &(*argument)->next_;
121 *argument = $ CYArgument(value);
122 return this;
123 }
124
125 CYExpression *CYCall::Replace(CYContext &context) {
126 context.Replace(function_);
127 arguments_->Replace(context);
128 return this;
129 }
130
131 namespace cy {
132 namespace Syntax {
133
134 void Catch::Replace(CYContext &context) { $T()
135 code_.Replace(context);
136 }
137
138 } }
139
140 void CYClause::Replace(CYContext &context) { $T()
141 context.Replace(case_);
142 statements_ = statements_->ReplaceAll(context);
143 next_->Replace(context);
144 }
145
146 CYStatement *CYComment::Replace(CYContext &context) {
147 return this;
148 }
149
150 CYExpression *CYCompound::Replace(CYContext &context) {
151 expressions_ = expressions_->ReplaceAll(context);
152 return expressions_ == NULL ? NULL : this;
153 }
154
155 CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
156 CYFunctionParameter *next(next_->Parameters(context));
157 if (CYFunctionParameter *parameter = Parameter(context)) {
158 parameter->SetNext(next);
159 return parameter;
160 } else
161 return next;
162 }
163
164 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
165 return next_ == NULL ? statement : next_->Replace(context, statement);
166 }
167
168 CYExpression *CYCondition::Replace(CYContext &context) {
169 context.Replace(test_);
170 context.Replace(true_);
171 context.Replace(false_);
172 return this;
173 }
174
175 void CYContext::NonLocal(CYStatement *&statements) {
176 CYContext &context(*this);
177
178 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
179 CYVariable *cye($V("$cye"));
180 CYVariable *unique($ CYVariable(nextlocal_->identifier_));
181
182 statements = $$->*
183 $E($ CYAssign(unique, $ CYObject()))->*
184 $ cy::Syntax::Try(statements, $ cy::Syntax::Catch(cye->name_, $$->*
185 $ CYIf($ CYIdentical($M(cye, $S("$cyk")), unique), $$->*
186 $ CYReturn($M(cye, $S("$cyv"))))->*
187 $ cy::Syntax::Throw(cye)
188 ), NULL);
189 }
190 }
191
192 CYIdentifier *CYContext::Unique() {
193 CYContext &context(*this);
194 return $ CYIdentifier(apr_psprintf(pool_, "$cy%u", unique_++));
195 }
196
197 CYStatement *CYContinue::Replace(CYContext &context) {
198 return this;
199 }
200
201 CYAssignment *CYDeclaration::Assignment(CYContext &context) {
202 CYExpression *variable(Replace(context));
203 return initialiser_ == NULL ? NULL : $ CYAssign(variable, initialiser_);
204 }
205
206 CYExpression *CYDeclaration::ForEachIn(CYContext &context) {
207 return $ CYVariable(identifier_);
208 }
209
210 CYExpression *CYDeclaration::Replace(CYContext &context) {
211 context.Replace(identifier_);
212 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
213 return $ CYVariable(identifier_);
214 }
215
216 CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
217 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context));
218 }
219
220 CYCompound *CYDeclarations::Replace(CYContext &context) {
221 CYCompound *compound;
222 if (next_ == NULL) compound:
223 compound = $ CYCompound();
224 else {
225 compound = next_->Replace(context);
226 if (compound == NULL)
227 goto compound;
228 }
229
230 if (CYAssignment *assignment = declaration_->Assignment(context))
231 compound->AddPrev(assignment);
232 return compound;
233 }
234
235 CYExpression *CYDirectMember::Replace(CYContext &context) {
236 Replace_(context);
237 return this;
238 }
239
240 CYStatement *CYDoWhile::Replace(CYContext &context) {
241 context.Replace(test_);
242 context.Replace(code_);
243 return this;
244 }
245
246 void CYElement::Replace(CYContext &context) { $T()
247 context.Replace(value_);
248 next_->Replace(context);
249 }
250
251 CYStatement *CYEmpty::Collapse(CYContext &context) {
252 return next_;
253 }
254
255 CYStatement *CYEmpty::Replace(CYContext &context) {
256 return this;
257 }
258
259 CYStatement *CYExpress::Collapse(CYContext &context) {
260 if (CYExpress *express = dynamic_cast<CYExpress *>(next_)) {
261 CYCompound *next(dynamic_cast<CYCompound *>(express->expression_));
262 if (next == NULL)
263 next = $ CYCompound(express->expression_);
264 next->AddPrev(expression_);
265 expression_ = next;
266 SetNext(express->next_);
267 }
268
269 return this;
270 }
271
272 CYStatement *CYExpress::Replace(CYContext &context) {
273 context.Replace(expression_);
274 if (expression_ == NULL)
275 return $ CYEmpty();
276 return this;
277 }
278
279 CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
280 return $C1(this, value);
281 }
282
283 CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
284 return this;
285 }
286
287 CYExpression *CYExpression::ForEachIn(CYContext &context) {
288 return this;
289 }
290
291 CYExpression *CYExpression::ReplaceAll(CYContext &context) { $T(NULL)
292 CYExpression *replace(this);
293 context.Replace(replace);
294
295 if (CYExpression *next = next_->ReplaceAll(context))
296 replace->SetNext(next);
297 else
298 replace->SetNext(next_);
299
300 return replace;
301 }
302
303 CYNumber *CYFalse::Number(CYContext &context) {
304 return $D(0);
305 }
306
307 CYString *CYFalse::String(CYContext &context) {
308 return $S("false");
309 }
310
311 void CYFinally::Replace(CYContext &context) { $T()
312 code_.Replace(context);
313 }
314
315 CYStatement *CYFor::Replace(CYContext &context) {
316 context.Replace(initialiser_);
317 context.Replace(test_);
318 context.Replace(increment_);
319 context.Replace(code_);
320 return this;
321 }
322
323 CYStatement *CYForIn::Replace(CYContext &context) {
324 // XXX: this actually might need a prefix statement
325 context.Replace(initialiser_);
326 context.Replace(set_);
327 context.Replace(code_);
328 return this;
329 }
330
331 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
332 return $ CYFunctionParameter(name_);
333 }
334
335 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
336 return $ CYForIn($ CYVariable(name_), set_, CYComprehension::Replace(context, statement));
337 }
338
339 CYStatement *CYForEachIn::Replace(CYContext &context) {
340 CYVariable *cys($V("$cys")), *cyt($V("$cyt"));
341
342 return $ CYLet($L2($L($I("$cys"), set_), $L($I("$cyt"))), $$->*
343 $ CYForIn(cyt, cys, $ CYBlock($$->*
344 $E($ CYAssign(initialiser_->ForEachIn(context), $M(cys, cyt)))->*
345 code_
346 ))
347 );
348 }
349
350 CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
351 return $ CYFunctionParameter(name_);
352 }
353
354 CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
355 CYVariable *cys($V("$cys")), *name($ CYVariable(name_));
356
357 return $E($C0($F(NULL, $P1("$cys"), $$->*
358 $E($ CYAssign(cys, set_))->*
359 $ CYForIn(name, cys, $ CYBlock($$->*
360 $E($ CYAssign(name, $M(cys, name)))->*
361 CYComprehension::Replace(context, statement)
362 ))
363 )));
364 }
365
366 void CYFunction::Inject(CYContext &context) {
367 context.Replace(name_);
368 context.scope_->Declare(context, name_, CYIdentifierOther);
369 }
370
371 void CYFunction::Replace_(CYContext &context, bool outer) {
372 if (outer)
373 Inject(context);
374
375 CYScope scope;
376 scope.parent_ = context.scope_;
377 context.scope_ = &scope;
378
379 CYNonLocal *nonlocal(context.nonlocal_);
380 CYNonLocal *nextlocal(context.nextlocal_);
381
382 bool localize;
383 if (nonlocal_ != NULL) {
384 localize = false;
385 context.nonlocal_ = nonlocal_;
386 } else {
387 localize = true;
388 nonlocal_ = $ CYNonLocal();
389 context.nextlocal_ = nonlocal_;
390 }
391
392 if (!outer && name_ != NULL)
393 Inject(context);
394
395 if (parameters_ != NULL)
396 parameters_ = parameters_->Replace(context, code_);
397 code_.Replace(context);
398
399 if (localize)
400 context.NonLocal(code_.statements_);
401
402 context.nextlocal_ = nextlocal;
403 context.nonlocal_ = nonlocal;
404
405 context.scope_ = scope.parent_;
406 scope.Scope(context, code_.statements_);
407 }
408
409 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
410 Replace_(context, false);
411 return this;
412 }
413
414 CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) {
415 name_ = name_->Replace(context);
416 context.scope_->Declare(context, name_, CYIdentifierArgument);
417 if (next_ != NULL)
418 next_ = next_->Replace(context, code);
419 return this;
420 }
421
422 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
423 Replace_(context, true);
424 return this;
425 }
426
427 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
428 if (replace_ != NULL && replace_ != this)
429 return replace_->Replace(context);
430 replace_ = context.scope_->Lookup(context, this);
431 return replace_;
432 }
433
434 CYStatement *CYIf::Replace(CYContext &context) {
435 context.Replace(test_);
436 context.Replace(true_);
437 context.Replace(false_);
438 return this;
439 }
440
441 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
442 return NULL;
443 }
444
445 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
446 return $ CYIf(test_, CYComprehension::Replace(context, statement));
447 }
448
449 CYExpression *CYIndirect::Replace(CYContext &context) {
450 CYPrefix::Replace(context);
451 return $M(rhs_, $S("$cyi"));
452 }
453
454 CYExpression *CYIndirectMember::Replace(CYContext &context) {
455 Replace_(context);
456 return $M($ CYIndirect(object_), property_);
457 }
458
459 CYExpression *CYInfix::Replace(CYContext &context) {
460 context.Replace(lhs_);
461 context.Replace(rhs_);
462 return this;
463 }
464
465 CYStatement *CYLabel::Replace(CYContext &context) {
466 context.Replace(statement_);
467 return this;
468 }
469
470 CYStatement *CYLet::Replace(CYContext &context) {
471 return $ CYWith($ CYObject(declarations_->Property(context)), &code_);
472 }
473
474 void CYMember::Replace_(CYContext &context) {
475 context.Replace(object_);
476 context.Replace(property_);
477 }
478
479 CYExpression *CYNew::AddArgument(CYContext &context, CYExpression *value) {
480 CYArgument **argument(&arguments_);
481 while (*argument != NULL)
482 argument = &(*argument)->next_;
483 *argument = $ CYArgument(value);
484 return this;
485 }
486
487 CYExpression *CYNew::Replace(CYContext &context) {
488 context.Replace(constructor_);
489 arguments_->Replace(context);
490 return this;
491 }
492
493 CYNumber *CYNull::Number(CYContext &context) {
494 return $D(0);
495 }
496
497 CYString *CYNull::String(CYContext &context) {
498 return $S("null");
499 }
500
501 CYNumber *CYNumber::Number(CYContext &context) {
502 return this;
503 }
504
505 CYString *CYNumber::String(CYContext &context) {
506 // XXX: there is a precise algorithm for this
507 return $S(apr_psprintf(context.pool_, "%.17g", Value()));
508 }
509
510 CYExpression *CYObject::Replace(CYContext &context) {
511 properties_->Replace(context);
512 return this;
513 }
514
515 CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) {
516 CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_));
517 parameter = parameter->Replace(context, code);
518 initializer_ = initializer_->Replace(context);
519
520 CYVariable *name($ CYVariable(name_));
521 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->*
522 $E($ CYAssign(name, initializer_))
523 ));
524
525 return parameter;
526 }
527
528 CYExpression *CYPostfix::Replace(CYContext &context) {
529 context.Replace(lhs_);
530 return this;
531 }
532
533 CYExpression *CYPrefix::Replace(CYContext &context) {
534 context.Replace(rhs_);
535 return this;
536 }
537
538 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
539 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
540 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
541
542 namespace {
543 struct IdentifierUsageLess :
544 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
545 {
546 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
547 if (lhs->usage_ != rhs->usage_)
548 return lhs->usage_ > rhs->usage_;
549 return lhs < rhs;
550 }
551 };
552
553 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
554 }
555
556 void CYProgram::Replace(CYContext &context) {
557 CYScope scope;
558 scope.parent_ = context.scope_;
559 context.scope_ = &scope;
560
561 context.nextlocal_ = $ CYNonLocal();
562
563 statements_ = statements_->ReplaceAll(context);
564 context.NonLocal(statements_);
565
566 context.scope_ = scope.parent_;
567 scope.Scope(context, statements_);
568
569 size_t offset(0);
570
571 CYCStringSet external;
572 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
573 external.insert((*i)->Word());
574
575 IdentifierUsages usages;
576
577 if (offset < context.rename_.size())
578 for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_)
579 usages.insert(i);
580
581 // XXX: totalling the probable occurrences and sorting by them would improve the result
582 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
583 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
584
585 const char *name;
586
587 if (context.options_.verbose_)
588 name = apr_psprintf(context.pool_, "$%"APR_SIZE_T_FMT"", offset);
589 else {
590 char id[8];
591 id[7] = '\0';
592
593 id:
594 unsigned position(7), local(offset + 1);
595
596 do {
597 unsigned index(local % (sizeof(MappingSet) - 1));
598 local /= sizeof(MappingSet) - 1;
599 id[--position] = MappingSet[index];
600 } while (local != 0);
601
602 if (external.find(id + position) != external.end()) {
603 ++offset;
604 goto id;
605 }
606
607 name = apr_pstrmemdup(context.pool_, id + position, 7 - position);
608 // XXX: at some point, this could become a keyword
609 }
610
611 for (CYIdentifier *identifier(i->identifier_); identifier != NULL; identifier = identifier->next_)
612 identifier->Set(name);
613 }
614 }
615
616 void CYProperty::Replace(CYContext &context) { $T()
617 context.Replace(value_);
618 next_->Replace(context);
619 }
620
621 CYStatement *CYReturn::Replace(CYContext &context) {
622 if (context.nonlocal_ != NULL) {
623 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
624 return $ cy::Syntax::Throw($ CYObject(
625 $ CYProperty($S("$cyk"), $ CYVariable(context.nonlocal_->Target(context)), value)
626 ));
627 }
628
629 context.Replace(value_);
630 return this;
631 }
632
633 CYExpression *CYRubyBlock::Replace(CYContext &context) {
634 // XXX: this needs to do something much more epic to handle return
635 return call_->AddArgument(context, proc_->Replace(context));
636 }
637
638 CYExpression *CYRubyProc::Replace(CYContext &context) {
639 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
640 function->nonlocal_ = context.nextlocal_;
641 return function;
642 }
643
644 void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
645 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
646 }
647
648 CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
649 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
650 return *insert.first;
651 }
652
653 void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
654 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
655 if (!insert.second) {
656 if ((*insert.first)->offset_ < identifier->offset_)
657 (*insert.first)->offset_ = identifier->offset_;
658 identifier->replace_ = *insert.first;
659 (*insert.first)->usage_ += identifier->usage_ + 1;
660 }
661 }
662
663 namespace {
664 struct IdentifierOffset {
665 size_t offset_;
666 CYIdentifierFlags flags_;
667 size_t usage_;
668 CYIdentifier *identifier_;
669
670 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
671 offset_(identifier->offset_),
672 flags_(flags),
673 usage_(identifier->usage_),
674 identifier_(identifier)
675 {
676 }
677 };
678
679 struct IdentifierOffsetLess :
680 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
681 {
682 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
683 if (lhs.offset_ != rhs.offset_)
684 return lhs.offset_ < rhs.offset_;
685 if (lhs.flags_ != rhs.flags_)
686 return lhs.flags_ < rhs.flags_;
687 /*if (lhs.usage_ != rhs.usage_)
688 return lhs.usage_ < rhs.usage_;*/
689 return lhs.identifier_ < rhs.identifier_;
690 }
691 };
692
693 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
694 }
695
696 void CYScope::Scope(CYContext &context, CYStatement *&statements) {
697 if (parent_ == NULL)
698 return;
699
700 CYDeclarations *last(NULL), *curr(NULL);
701
702 IdentifierOffsets offsets;
703
704 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
705 if (i->second != CYIdentifierMagic)
706 offsets.insert(IdentifierOffset(i->first, i->second));
707
708 size_t offset(0);
709
710 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
711 if (i->flags_ == CYIdentifierVariable) {
712 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
713 if (last == NULL)
714 last = next;
715 if (curr != NULL)
716 curr->SetNext(next);
717 curr = next;
718 }
719
720 if (offset < i->offset_)
721 offset = i->offset_;
722 if (context.rename_.size() <= offset)
723 context.rename_.resize(offset + 1);
724
725 CYIdentifierUsage &rename(context.rename_[offset++]);
726 i->identifier_->SetNext(rename.identifier_);
727 rename.identifier_ = i->identifier_;
728 rename.usage_ += i->identifier_->usage_ + 1;
729 }
730
731 if (last != NULL) {
732 CYVar *var($ CYVar(last));
733 var->SetNext(statements);
734 statements = var;
735 }
736
737 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
738 if (internal_.find(*i) == internal_.end()) {
739 //std::cout << *i << '=' << offset << std::endl;
740 if ((*i)->offset_ < offset)
741 (*i)->offset_ = offset;
742 parent_->Merge(context, *i);
743 }
744 }
745
746 CYStatement *CYStatement::Collapse(CYContext &context) {
747 return this;
748 }
749
750 CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL)
751 CYStatement *replace(this);
752 context.Replace(replace);
753 replace->SetNext(next_->ReplaceAll(context));
754 return replace->Collapse(context);
755 }
756
757 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
758 size_t size(size_ + rhs->size_);
759 char *value(new(context.pool_) char[size + 1]);
760 memcpy(value, value_, size_);
761 memcpy(value + size_, rhs->value_, rhs->size_);
762 value[size] = '\0';
763 return $S(value, size);
764 }
765
766 CYNumber *CYString::Number(CYContext &context) {
767 // XXX: there is a precise algorithm for this
768 return NULL;
769 }
770
771 CYString *CYString::String(CYContext &context) {
772 return this;
773 }
774
775 CYStatement *CYSwitch::Replace(CYContext &context) {
776 context.Replace(value_);
777 clauses_->Replace(context);
778 return this;
779 }
780
781 CYExpression *CYThis::Replace(CYContext &context) {
782 return this;
783 }
784
785 namespace cy {
786 namespace Syntax {
787
788 CYStatement *Throw::Replace(CYContext &context) {
789 context.Replace(value_);
790 return this;
791 }
792
793 } }
794
795 CYExpression *CYTrivial::Replace(CYContext &context) {
796 return this;
797 }
798
799 CYNumber *CYTrue::Number(CYContext &context) {
800 return $D(1);
801 }
802
803 CYString *CYTrue::String(CYContext &context) {
804 return $S("true");
805 }
806
807 namespace cy {
808 namespace Syntax {
809
810 CYStatement *Try::Replace(CYContext &context) {
811 code_.Replace(context);
812 catch_->Replace(context);
813 finally_->Replace(context);
814 return this;
815 }
816
817 } }
818
819 CYStatement *CYVar::Replace(CYContext &context) {
820 return $E(declarations_->Replace(context));
821 }
822
823 CYExpression *CYVariable::Replace(CYContext &context) {
824 name_ = name_->Replace(context);
825 return this;
826 }
827
828 CYStatement *CYWhile::Replace(CYContext &context) {
829 context.Replace(test_);
830 context.Replace(code_);
831 return this;
832 }
833
834 CYStatement *CYWith::Replace(CYContext &context) {
835 context.Replace(scope_);
836 context.Replace(code_);
837 return this;
838 }
839
840 CYExpression *CYWord::ClassName(CYContext &context, bool object) {
841 CYString *name($S(this));
842 if (object)
843 return $C1($V("objc_getClass"), name);
844 else
845 return name;
846 }