]> git.saurik.com Git - cycript.git/blob - Replace.cpp
Pass var declarations through non-local functions.
[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 void CYArgument::Replace(CYContext &context) { $T()
67 context.Replace(value_);
68 next_->Replace(context);
69 }
70
71 CYExpression *CYArray::Replace(CYContext &context) {
72 elements_->Replace(context);
73 return this;
74 }
75
76 CYExpression *CYArrayComprehension::Replace(CYContext &context) {
77 CYVariable *cyv($V("$cyv"));
78
79 return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->*
80 $E($ CYAssign(cyv, $ CYArray()))->*
81 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
82 $ CYReturn(cyv)
83 ));
84 }
85
86 CYExpression *CYAssignment::Replace(CYContext &context) {
87 context.Replace(lhs_);
88 context.Replace(rhs_);
89 return this;
90 }
91
92 CYStatement *CYBlock::Replace(CYContext &context) {
93 context.ReplaceAll(statements_);
94 if (statements_ == NULL)
95 return $ CYEmpty();
96 return this;
97 }
98
99 CYStatement *CYBreak::Replace(CYContext &context) {
100 return this;
101 }
102
103 CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
104 CYArgument **argument(&arguments_);
105 while (*argument != NULL)
106 argument = &(*argument)->next_;
107 *argument = $ CYArgument(value);
108 return this;
109 }
110
111 CYExpression *CYCall::Replace(CYContext &context) {
112 context.Replace(function_);
113 arguments_->Replace(context);
114 return this;
115 }
116
117 namespace cy {
118 namespace Syntax {
119
120 void Catch::Replace(CYContext &context) { $T()
121 CYScope scope(true, context, code_.statements_);
122
123 context.Replace(name_);
124 context.scope_->Declare(context, name_, CYIdentifierCatch);
125
126 code_.Replace(context);
127 scope.Close();
128 }
129
130 } }
131
132 void CYClause::Replace(CYContext &context) { $T()
133 context.Replace(case_);
134 context.ReplaceAll(statements_);
135 next_->Replace(context);
136 }
137
138 CYStatement *CYComment::Replace(CYContext &context) {
139 return this;
140 }
141
142 CYExpression *CYCompound::Replace(CYContext &context) {
143 context.ReplaceAll(expressions_);
144 if (expressions_ == NULL)
145 return NULL;
146 return this;
147 }
148
149 CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
150 CYFunctionParameter *next(next_->Parameters(context));
151 if (CYFunctionParameter *parameter = Parameter(context)) {
152 parameter->SetNext(next);
153 return parameter;
154 } else
155 return next;
156 }
157
158 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
159 return next_ == NULL ? statement : next_->Replace(context, statement);
160 }
161
162 CYExpression *CYCondition::Replace(CYContext &context) {
163 context.Replace(test_);
164 context.Replace(true_);
165 context.Replace(false_);
166 return this;
167 }
168
169 void CYContext::NonLocal(CYStatement *&statements) {
170 CYContext &context(*this);
171
172 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
173 CYIdentifier *cye($I("$cye")->Replace(context));
174 CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
175
176 CYStatement *declare(
177 $ CYVar($L1($L(unique, $ CYObject()))));
178
179 cy::Syntax::Catch *rescue(
180 $ cy::Syntax::Catch(cye, $$->*
181 $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->*
182 $ CYReturn($M($V(cye), $S("$cyv"))))->*
183 $ cy::Syntax::Throw($V(cye))));
184
185 context.Replace(declare);
186 rescue->Replace(context);
187
188 statements = $$->*
189 declare->*
190 $ cy::Syntax::Try(statements, rescue, NULL);
191 }
192 }
193
194 CYIdentifier *CYContext::Unique() {
195 return $ CYIdentifier(apr_psprintf($pool, "$cy%u", unique_++));
196 }
197
198 CYStatement *CYContinue::Replace(CYContext &context) {
199 return this;
200 }
201
202 CYAssignment *CYDeclaration::Assignment(CYContext &context) {
203 if (initialiser_ == NULL)
204 return NULL;
205
206 CYAssignment *value($ CYAssign(Variable(context), initialiser_));
207 initialiser_ = NULL;
208 return value;
209 }
210
211 CYVariable *CYDeclaration::Variable(CYContext &context) {
212 return $V(identifier_);
213 }
214
215 CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
216 return $ CYVar($L1($L(identifier_, value)));
217 }
218
219 CYExpression *CYDeclaration::Replace(CYContext &context) {
220 context.Replace(identifier_);
221 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
222 return Variable(context);
223 }
224
225 void CYDeclarations::Replace(CYContext &context) { $T()
226 declaration_->Replace(context);
227 next_->Replace(context);
228 }
229
230 CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
231 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context));
232 }
233
234 CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
235 return $ CYFunctionParameter(declaration_->identifier_, next_->Parameter(context));
236 }
237
238 CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
239 return $ CYArgument(declaration_->initialiser_ ?: $U, next_->Argument(context));
240 }
241
242 CYCompound *CYDeclarations::Compound(CYContext &context) { $T(NULL)
243 CYCompound *compound(next_->Compound(context) ?: $ CYCompound());
244 if (CYAssignment *assignment = declaration_->Assignment(context))
245 compound->AddPrev(assignment);
246 return compound;
247 }
248
249 CYExpression *CYDirectMember::Replace(CYContext &context) {
250 context.Replace(object_);
251 context.Replace(property_);
252 return this;
253 }
254
255 CYStatement *CYDoWhile::Replace(CYContext &context) {
256 context.Replace(test_);
257 context.Replace(code_);
258 return this;
259 }
260
261 void CYElement::Replace(CYContext &context) { $T()
262 context.Replace(value_);
263 next_->Replace(context);
264 }
265
266 CYStatement *CYEmpty::Replace(CYContext &context) {
267 return NULL;
268 }
269
270 CYStatement *CYExpress::Replace(CYContext &context) {
271 while (CYExpress *express = dynamic_cast<CYExpress *>(next_)) {
272 CYCompound *compound(dynamic_cast<CYCompound *>(express->expression_));
273 if (compound == NULL)
274 compound = $ CYCompound(express->expression_);
275 compound->AddPrev(expression_);
276 expression_ = compound;
277 SetNext(express->next_);
278 }
279
280 context.Replace(expression_);
281 if (expression_ == NULL)
282 return $ CYEmpty();
283
284 return this;
285 }
286
287 CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
288 return $C1(this, value);
289 }
290
291 CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
292 return this;
293 }
294
295 CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
296 return $E($ CYAssign(this, value));
297 }
298
299 CYAssignment *CYExpression::Assignment(CYContext &context) {
300 return NULL;
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 CYCompound *CYForDeclarations::Replace(CYContext &context) {
324 declarations_->Replace(context);
325 return declarations_->Compound(context);
326 }
327
328 // XXX: this still feels highly suboptimal
329 CYStatement *CYForIn::Replace(CYContext &context) {
330 if (CYAssignment *assignment = initialiser_->Assignment(context))
331 return $ CYBlock($$->*
332 $E(assignment)->*
333 this
334 );
335
336 context.Replace(initialiser_);
337 context.Replace(set_);
338 context.Replace(code_);
339 return this;
340 }
341
342 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
343 return $ CYFunctionParameter(name_);
344 }
345
346 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
347 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
348 }
349
350 CYStatement *CYForEachIn::Replace(CYContext &context) {
351 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
352
353 return $ CYLet($L2($L(cys, set_), $L(cyt)), $$->*
354 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
355 initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))->*
356 code_
357 ))
358 );
359 }
360
361 CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
362 return $ CYFunctionParameter(name_);
363 }
364
365 CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
366 CYIdentifier *cys($I("cys"));
367
368 return $E($C0($F(NULL, $P1("$cys"), $$->*
369 $E($ CYAssign($V(cys), set_))->*
370 $ CYForIn($V(name_), $V(cys), $ CYBlock($$->*
371 $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->*
372 CYComprehension::Replace(context, statement)
373 ))
374 )));
375 }
376
377 void CYFunction::Inject(CYContext &context) {
378 context.Replace(name_);
379 context.scope_->Declare(context, name_, CYIdentifierOther);
380 }
381
382 void CYFunction::Replace_(CYContext &context, bool outer) {
383 if (outer)
384 Inject(context);
385
386 CYNonLocal *nonlocal(context.nonlocal_);
387 CYNonLocal *nextlocal(context.nextlocal_);
388
389 bool localize;
390 if (nonlocal_ != NULL) {
391 localize = false;
392 context.nonlocal_ = nonlocal_;
393 } else {
394 localize = true;
395 nonlocal_ = $ CYNonLocal();
396 context.nextlocal_ = nonlocal_;
397 }
398
399 CYScope scope(!localize, context, code_.statements_);
400
401 if (!outer && name_ != NULL)
402 Inject(context);
403
404 if (parameters_ != NULL)
405 parameters_ = parameters_->Replace(context, code_);
406
407 code_.Replace(context);
408
409 if (localize)
410 context.NonLocal(code_.statements_);
411
412 context.nextlocal_ = nextlocal;
413 context.nonlocal_ = nonlocal;
414
415 scope.Close();
416 }
417
418 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
419 Replace_(context, false);
420 return this;
421 }
422
423 CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) {
424 context.Replace(name_);
425 context.scope_->Declare(context, name_, CYIdentifierArgument);
426 if (next_ != NULL)
427 next_ = next_->Replace(context, code);
428 return this;
429 }
430
431 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
432 Replace_(context, true);
433 return this;
434 }
435
436 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
437 if (replace_ != NULL && replace_ != this)
438 return replace_->Replace(context);
439 replace_ = context.scope_->Lookup(context, this);
440 return replace_;
441 }
442
443 CYStatement *CYIf::Replace(CYContext &context) {
444 context.Replace(test_);
445 context.Replace(true_);
446 context.Replace(false_);
447 return this;
448 }
449
450 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
451 return NULL;
452 }
453
454 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
455 return $ CYIf(test_, CYComprehension::Replace(context, statement));
456 }
457
458 CYExpression *CYIndirect::Replace(CYContext &context) {
459 return $M(rhs_, $S("$cyi"));
460 }
461
462 CYExpression *CYIndirectMember::Replace(CYContext &context) {
463 return $M($ CYIndirect(object_), property_);
464 }
465
466 CYExpression *CYInfix::Replace(CYContext &context) {
467 context.Replace(lhs_);
468 context.Replace(rhs_);
469 return this;
470 }
471
472 CYStatement *CYLabel::Replace(CYContext &context) {
473 context.Replace(statement_);
474 return this;
475 }
476
477 CYStatement *CYLet::Replace(CYContext &context) {
478 return $E($ CYCall(CYNonLocalize(context, $ CYFunctionExpression(NULL, declarations_->Parameter(context), code_)), declarations_->Argument(context)));
479 }
480
481 namespace cy {
482 namespace Syntax {
483
484 CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
485 CYSetLast(arguments_, $ CYArgument(value));
486 return this;
487 }
488
489 CYExpression *New::Replace(CYContext &context) {
490 context.Replace(constructor_);
491 arguments_->Replace(context);
492 return this;
493 }
494
495 } }
496
497 CYNumber *CYNull::Number(CYContext &context) {
498 return $D(0);
499 }
500
501 CYString *CYNull::String(CYContext &context) {
502 return $S("null");
503 }
504
505 CYNumber *CYNumber::Number(CYContext &context) {
506 return this;
507 }
508
509 CYString *CYNumber::String(CYContext &context) {
510 // XXX: there is a precise algorithm for this
511 return $S(apr_psprintf($pool, "%.17g", Value()));
512 }
513
514 CYExpression *CYObject::Replace(CYContext &context) {
515 properties_->Replace(context);
516 return this;
517 }
518
519 CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) {
520 CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_));
521 parameter = parameter->Replace(context, code);
522 context.Replace(initializer_);
523
524 CYVariable *name($V(name_));
525 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->*
526 $E($ CYAssign(name, initializer_))
527 ));
528
529 return parameter;
530 }
531
532 CYExpression *CYPostfix::Replace(CYContext &context) {
533 context.Replace(lhs_);
534 return this;
535 }
536
537 CYExpression *CYPrefix::Replace(CYContext &context) {
538 context.Replace(rhs_);
539 return this;
540 }
541
542 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
543 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
544 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
545
546 namespace {
547 struct IdentifierUsageLess :
548 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
549 {
550 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
551 if (lhs->usage_ != rhs->usage_)
552 return lhs->usage_ > rhs->usage_;
553 return lhs < rhs;
554 }
555 };
556
557 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
558 }
559
560 void CYProgram::Replace(CYContext &context) {
561 CYScope scope(true, context, statements_);
562
563 context.nextlocal_ = $ CYNonLocal();
564 context.ReplaceAll(statements_);
565 context.NonLocal(statements_);
566
567 scope.Close();
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 CYForEach (i, context.rename_[offset].identifier_)
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($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($pool, id + position, 7 - position);
608 // XXX: at some point, this could become a keyword
609 }
610
611 CYForEach (identifier, i->identifier_)
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"), $V(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 return CYNonLocalize(context, $ CYFunctionExpression(NULL, parameters_, code_));
640 }
641
642 CYScope::CYScope(bool transparent, CYContext &context, CYStatement *&statements) :
643 transparent_(transparent),
644 context_(context),
645 statements_(statements),
646 parent_(context.scope_)
647 {
648 context_.scope_ = this;
649 }
650
651 CYScope::~CYScope() {
652 }
653
654 void CYScope::Close() {
655 context_.scope_ = parent_;
656 Scope(context_, statements_);
657 }
658
659 void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
660 if (!transparent_ || flags == CYIdentifierArgument || flags == CYIdentifierCatch)
661 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
662 else if (parent_ != NULL)
663 parent_->Declare(context, identifier, flags);
664 }
665
666 CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
667 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
668 return *insert.first;
669 }
670
671 void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
672 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
673 if (!insert.second) {
674 if ((*insert.first)->offset_ < identifier->offset_)
675 (*insert.first)->offset_ = identifier->offset_;
676 identifier->replace_ = *insert.first;
677 (*insert.first)->usage_ += identifier->usage_ + 1;
678 }
679 }
680
681 namespace {
682 struct IdentifierOffset {
683 size_t offset_;
684 CYIdentifierFlags flags_;
685 size_t usage_;
686 CYIdentifier *identifier_;
687
688 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
689 offset_(identifier->offset_),
690 flags_(flags),
691 usage_(identifier->usage_),
692 identifier_(identifier)
693 {
694 }
695 };
696
697 struct IdentifierOffsetLess :
698 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
699 {
700 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
701 if (lhs.offset_ != rhs.offset_)
702 return lhs.offset_ < rhs.offset_;
703 if (lhs.flags_ != rhs.flags_)
704 return lhs.flags_ < rhs.flags_;
705 /*if (lhs.usage_ != rhs.usage_)
706 return lhs.usage_ < rhs.usage_;*/
707 return lhs.identifier_ < rhs.identifier_;
708 }
709 };
710
711 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
712 }
713
714 void CYScope::Scope(CYContext &context, CYStatement *&statements) {
715 if (parent_ == NULL)
716 return;
717
718 CYDeclarations *last(NULL), *curr(NULL);
719
720 IdentifierOffsets offsets;
721
722 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
723 if (i->second != CYIdentifierMagic)
724 offsets.insert(IdentifierOffset(i->first, i->second));
725
726 size_t offset(0);
727
728 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
729 if (i->flags_ == CYIdentifierVariable) {
730 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
731 if (last == NULL)
732 last = next;
733 if (curr != NULL)
734 curr->SetNext(next);
735 curr = next;
736 }
737
738 if (offset < i->offset_)
739 offset = i->offset_;
740 if (context.rename_.size() <= offset)
741 context.rename_.resize(offset + 1);
742
743 CYIdentifierUsage &rename(context.rename_[offset++]);
744 i->identifier_->SetNext(rename.identifier_);
745 rename.identifier_ = i->identifier_;
746 rename.usage_ += i->identifier_->usage_ + 1;
747 }
748
749 if (last != NULL) {
750 CYVar *var($ CYVar(last));
751 var->SetNext(statements);
752 statements = var;
753 }
754
755 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
756 if (internal_.find(*i) == internal_.end()) {
757 //std::cout << *i << '=' << offset << std::endl;
758 if ((*i)->offset_ < offset)
759 (*i)->offset_ = offset;
760 parent_->Merge(context, *i);
761 }
762 }
763
764 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
765 size_t size(size_ + rhs->size_);
766 char *value($ char[size + 1]);
767 memcpy(value, value_, size_);
768 memcpy(value + size_, rhs->value_, rhs->size_);
769 value[size] = '\0';
770 return $S(value, size);
771 }
772
773 CYNumber *CYString::Number(CYContext &context) {
774 // XXX: there is a precise algorithm for this
775 return NULL;
776 }
777
778 CYString *CYString::String(CYContext &context) {
779 return this;
780 }
781
782 CYStatement *CYSwitch::Replace(CYContext &context) {
783 context.Replace(value_);
784 clauses_->Replace(context);
785 return this;
786 }
787
788 CYExpression *CYThis::Replace(CYContext &context) {
789 return this;
790 }
791
792 namespace cy {
793 namespace Syntax {
794
795 CYStatement *Throw::Replace(CYContext &context) {
796 context.Replace(value_);
797 return this;
798 }
799
800 } }
801
802 CYExpression *CYTrivial::Replace(CYContext &context) {
803 return this;
804 }
805
806 CYNumber *CYTrue::Number(CYContext &context) {
807 return $D(1);
808 }
809
810 CYString *CYTrue::String(CYContext &context) {
811 return $S("true");
812 }
813
814 namespace cy {
815 namespace Syntax {
816
817 CYStatement *Try::Replace(CYContext &context) {
818 code_.Replace(context);
819 catch_->Replace(context);
820 finally_->Replace(context);
821 return this;
822 }
823
824 } }
825
826 CYStatement *CYVar::Replace(CYContext &context) {
827 declarations_->Replace(context);
828 return $E(declarations_->Compound(context));
829 }
830
831 CYExpression *CYVariable::Replace(CYContext &context) {
832 context.Replace(name_);
833 return this;
834 }
835
836 CYStatement *CYWhile::Replace(CYContext &context) {
837 context.Replace(test_);
838 context.Replace(code_);
839 return this;
840 }
841
842 CYStatement *CYWith::Replace(CYContext &context) {
843 context.Replace(scope_);
844 context.Replace(code_);
845 return this;
846 }
847
848 CYExpression *CYWord::ClassName(CYContext &context, bool object) {
849 CYString *name($S(this));
850 if (object)
851 return $C1($V("objc_getClass"), name);
852 else
853 return name;
854 }