]> git.saurik.com Git - cycript.git/blob - Replace.cpp
250b5550cfcf4ff3ddc040588d765999787b7059
[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 (nonlocal_->identifier_ != NULL) {
179 CYVariable *cye($V("$cye"));
180 CYVariable *unique($ CYVariable(nonlocal_->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 bool localize;
380 if (nonlocal_ != NULL)
381 localize = false;
382 else {
383 localize = true;
384 nonlocal_ = $ CYNonLocal();
385 }
386
387 CYNonLocal *nonlocal(context.nonlocal_);
388 context.nonlocal_ = nonlocal_;
389
390 if (!outer && name_ != NULL)
391 Inject(context);
392
393 if (parameters_ != NULL)
394 parameters_ = parameters_->Replace(context, code_);
395 code_.Replace(context);
396
397 if (localize)
398 context.NonLocal(code_.statements_);
399 context.nonlocal_ = nonlocal;
400
401 context.scope_ = scope.parent_;
402 scope.Scope(context, code_.statements_);
403 }
404
405 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
406 Replace_(context, false);
407 return this;
408 }
409
410 CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) {
411 name_ = name_->Replace(context);
412 context.scope_->Declare(context, name_, CYIdentifierArgument);
413 if (next_ != NULL)
414 next_ = next_->Replace(context, code);
415 return this;
416 }
417
418 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
419 Replace_(context, true);
420 return this;
421 }
422
423 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
424 if (replace_ != NULL && replace_ != this)
425 return replace_->Replace(context);
426 replace_ = context.scope_->Lookup(context, this);
427 return replace_;
428 }
429
430 CYStatement *CYIf::Replace(CYContext &context) {
431 context.Replace(test_);
432 context.Replace(true_);
433 context.Replace(false_);
434 return this;
435 }
436
437 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
438 return NULL;
439 }
440
441 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
442 return $ CYIf(test_, CYComprehension::Replace(context, statement));
443 }
444
445 CYExpression *CYIndirect::Replace(CYContext &context) {
446 CYPrefix::Replace(context);
447 return $M(rhs_, $S("$cyi"));
448 }
449
450 CYExpression *CYIndirectMember::Replace(CYContext &context) {
451 Replace_(context);
452 return $M($ CYIndirect(object_), property_);
453 }
454
455 CYExpression *CYInfix::Replace(CYContext &context) {
456 context.Replace(lhs_);
457 context.Replace(rhs_);
458 return this;
459 }
460
461 CYStatement *CYLabel::Replace(CYContext &context) {
462 context.Replace(statement_);
463 return this;
464 }
465
466 CYStatement *CYLet::Replace(CYContext &context) {
467 return $ CYWith($ CYObject(declarations_->Property(context)), &code_);
468 }
469
470 void CYMember::Replace_(CYContext &context) {
471 context.Replace(object_);
472 context.Replace(property_);
473 }
474
475 CYExpression *CYNew::AddArgument(CYContext &context, CYExpression *value) {
476 CYArgument **argument(&arguments_);
477 while (*argument != NULL)
478 argument = &(*argument)->next_;
479 *argument = $ CYArgument(value);
480 return this;
481 }
482
483 CYExpression *CYNew::Replace(CYContext &context) {
484 context.Replace(constructor_);
485 arguments_->Replace(context);
486 return this;
487 }
488
489 CYNumber *CYNull::Number(CYContext &context) {
490 return $D(0);
491 }
492
493 CYString *CYNull::String(CYContext &context) {
494 return $S("null");
495 }
496
497 CYNumber *CYNumber::Number(CYContext &context) {
498 return this;
499 }
500
501 CYString *CYNumber::String(CYContext &context) {
502 // XXX: there is a precise algorithm for this
503 return $S(apr_psprintf(context.pool_, "%.17g", Value()));
504 }
505
506 CYExpression *CYObject::Replace(CYContext &context) {
507 properties_->Replace(context);
508 return this;
509 }
510
511 CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) {
512 CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_));
513 parameter = parameter->Replace(context, code);
514 initializer_ = initializer_->Replace(context);
515
516 CYVariable *name($ CYVariable(name_));
517 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->*
518 $E($ CYAssign(name, initializer_))
519 ));
520
521 return parameter;
522 }
523
524 CYExpression *CYPostfix::Replace(CYContext &context) {
525 context.Replace(lhs_);
526 return this;
527 }
528
529 CYExpression *CYPrefix::Replace(CYContext &context) {
530 context.Replace(rhs_);
531 return this;
532 }
533
534 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
535 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
536 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
537
538 namespace {
539 struct IdentifierUsageLess :
540 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
541 {
542 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
543 if (lhs->usage_ != rhs->usage_)
544 return lhs->usage_ > rhs->usage_;
545 return lhs < rhs;
546 }
547 };
548
549 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
550 }
551
552 void CYProgram::Replace(CYContext &context) {
553 CYScope scope;
554 scope.parent_ = context.scope_;
555 context.scope_ = &scope;
556
557 context.nonlocal_ = $ CYNonLocal();
558 statements_ = statements_->ReplaceAll(context);
559 context.NonLocal(statements_);
560
561 context.scope_ = scope.parent_;
562 scope.Scope(context, statements_);
563
564 size_t offset(0);
565
566 CYCStringSet external;
567 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
568 external.insert((*i)->Word());
569
570 IdentifierUsages usages;
571
572 if (offset < context.rename_.size())
573 for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_)
574 usages.insert(i);
575
576 // XXX: totalling the probable occurrences and sorting by them would improve the result
577 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
578 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
579
580 const char *name;
581
582 if (context.options_.verbose_)
583 name = apr_psprintf(context.pool_, "$%"APR_SIZE_T_FMT"", offset);
584 else {
585 char id[8];
586 id[7] = '\0';
587
588 id:
589 unsigned position(7), local(offset + 1);
590
591 do {
592 unsigned index(local % (sizeof(MappingSet) - 1));
593 local /= sizeof(MappingSet) - 1;
594 id[--position] = MappingSet[index];
595 } while (local != 0);
596
597 if (external.find(id + position) != external.end()) {
598 ++offset;
599 goto id;
600 }
601
602 name = apr_pstrmemdup(context.pool_, id + position, 7 - position);
603 // XXX: at some point, this could become a keyword
604 }
605
606 for (CYIdentifier *identifier(i->identifier_); identifier != NULL; identifier = identifier->next_)
607 identifier->Set(name);
608 }
609 }
610
611 void CYProperty::Replace(CYContext &context) { $T()
612 context.Replace(value_);
613 next_->Replace(context);
614 }
615
616 CYStatement *CYReturn::Replace(CYContext &context) {
617 if (context.nonlocal_ != NULL) {
618 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
619 return $ cy::Syntax::Throw($ CYObject(
620 $ CYProperty($S("$cyk"), $ CYVariable(context.nonlocal_->Target(context)), value)
621 ));
622 }
623
624 context.Replace(value_);
625 return this;
626 }
627
628 CYExpression *CYRubyBlock::Replace(CYContext &context) {
629 // XXX: this needs to do something much more epic to handle return
630 return call_->AddArgument(context, proc_->Replace(context));
631 }
632
633 CYExpression *CYRubyProc::Replace(CYContext &context) {
634 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
635 function->nonlocal_ = context.nonlocal_;
636 return function;
637 }
638
639 void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
640 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
641 }
642
643 CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
644 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
645 return *insert.first;
646 }
647
648 void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
649 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
650 if (!insert.second) {
651 if ((*insert.first)->offset_ < identifier->offset_)
652 (*insert.first)->offset_ = identifier->offset_;
653 identifier->replace_ = *insert.first;
654 (*insert.first)->usage_ += identifier->usage_ + 1;
655 }
656 }
657
658 namespace {
659 struct IdentifierOffset {
660 size_t offset_;
661 CYIdentifierFlags flags_;
662 size_t usage_;
663 CYIdentifier *identifier_;
664
665 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
666 offset_(identifier->offset_),
667 flags_(flags),
668 usage_(identifier->usage_),
669 identifier_(identifier)
670 {
671 }
672 };
673
674 struct IdentifierOffsetLess :
675 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
676 {
677 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
678 if (lhs.offset_ != rhs.offset_)
679 return lhs.offset_ < rhs.offset_;
680 if (lhs.flags_ != rhs.flags_)
681 return lhs.flags_ < rhs.flags_;
682 /*if (lhs.usage_ != rhs.usage_)
683 return lhs.usage_ < rhs.usage_;*/
684 return lhs.identifier_ < rhs.identifier_;
685 }
686 };
687
688 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
689 }
690
691 void CYScope::Scope(CYContext &context, CYStatement *&statements) {
692 if (parent_ == NULL)
693 return;
694
695 CYDeclarations *last(NULL), *curr(NULL);
696
697 IdentifierOffsets offsets;
698
699 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
700 if (i->second != CYIdentifierMagic)
701 offsets.insert(IdentifierOffset(i->first, i->second));
702
703 size_t offset(0);
704
705 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
706 if (i->flags_ == CYIdentifierVariable) {
707 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
708 if (last == NULL)
709 last = next;
710 if (curr != NULL)
711 curr->SetNext(next);
712 curr = next;
713 }
714
715 if (offset < i->offset_)
716 offset = i->offset_;
717 if (context.rename_.size() <= offset)
718 context.rename_.resize(offset + 1);
719
720 CYIdentifierUsage &rename(context.rename_[offset++]);
721 i->identifier_->SetNext(rename.identifier_);
722 rename.identifier_ = i->identifier_;
723 rename.usage_ += i->identifier_->usage_ + 1;
724 }
725
726 if (last != NULL) {
727 CYVar *var($ CYVar(last));
728 var->SetNext(statements);
729 statements = var;
730 }
731
732 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
733 if (internal_.find(*i) == internal_.end()) {
734 //std::cout << *i << '=' << offset << std::endl;
735 if ((*i)->offset_ < offset)
736 (*i)->offset_ = offset;
737 parent_->Merge(context, *i);
738 }
739 }
740
741 CYStatement *CYStatement::Collapse(CYContext &context) {
742 return this;
743 }
744
745 CYStatement *CYStatement::ReplaceAll(CYContext &context) { $T(NULL)
746 CYStatement *replace(this);
747 context.Replace(replace);
748 replace->SetNext(next_->ReplaceAll(context));
749 return replace->Collapse(context);
750 }
751
752 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
753 size_t size(size_ + rhs->size_);
754 char *value(new(context.pool_) char[size + 1]);
755 memcpy(value, value_, size_);
756 memcpy(value + size_, rhs->value_, rhs->size_);
757 value[size] = '\0';
758 return $S(value, size);
759 }
760
761 CYNumber *CYString::Number(CYContext &context) {
762 // XXX: there is a precise algorithm for this
763 return NULL;
764 }
765
766 CYString *CYString::String(CYContext &context) {
767 return this;
768 }
769
770 CYStatement *CYSwitch::Replace(CYContext &context) {
771 context.Replace(value_);
772 clauses_->Replace(context);
773 return this;
774 }
775
776 CYExpression *CYThis::Replace(CYContext &context) {
777 return this;
778 }
779
780 namespace cy {
781 namespace Syntax {
782
783 CYStatement *Throw::Replace(CYContext &context) {
784 context.Replace(value_);
785 return this;
786 }
787
788 } }
789
790 CYExpression *CYTrivial::Replace(CYContext &context) {
791 return this;
792 }
793
794 CYNumber *CYTrue::Number(CYContext &context) {
795 return $D(1);
796 }
797
798 CYString *CYTrue::String(CYContext &context) {
799 return $S("true");
800 }
801
802 namespace cy {
803 namespace Syntax {
804
805 CYStatement *Try::Replace(CYContext &context) {
806 code_.Replace(context);
807 catch_->Replace(context);
808 finally_->Replace(context);
809 return this;
810 }
811
812 } }
813
814 CYStatement *CYVar::Replace(CYContext &context) {
815 return $E(declarations_->Replace(context));
816 }
817
818 CYExpression *CYVariable::Replace(CYContext &context) {
819 name_ = name_->Replace(context);
820 return this;
821 }
822
823 CYStatement *CYWhile::Replace(CYContext &context) {
824 context.Replace(test_);
825 context.Replace(code_);
826 return this;
827 }
828
829 CYStatement *CYWith::Replace(CYContext &context) {
830 context.Replace(scope_);
831 context.Replace(code_);
832 return this;
833 }
834
835 CYExpression *CYWord::ClassName(CYContext &context, bool object) {
836 CYString *name($S(this));
837 if (object)
838 return $C1($V("objc_getClass"), name);
839 else
840 return name;
841 }