]> git.saurik.com Git - cycript.git/blob - Replace.cpp
ef481e6252d46757d85be9c81f35d1e1c3040818
[cycript.git] / Replace.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <iomanip>
23
24 #include "Replace.hpp"
25 #include "Syntax.hpp"
26
27 CYFunctionExpression *CYNonLocalize(CYContext &context, CYFunctionExpression *function) {
28 function->nonlocal_ = context.nextlocal_;
29 return function;
30 }
31
32 CYFunctionExpression *CYSuperize(CYContext &context, CYFunctionExpression *function) {
33 function->super_ = context.super_;
34 return function;
35 }
36
37 CYStatement *CYDefineProperty(CYExpression *object, CYExpression *name, bool configurable, bool enumerable, CYProperty *descriptor) {
38 return $E($C3($M($V("Object"), $S("defineProperty")), object, name, $ CYObject(CYList<CYProperty>()
39 ->* (configurable ? $ CYPropertyValue($S("configurable"), $ CYTrue()) : NULL)
40 ->* (enumerable ? $ CYPropertyValue($S("enumerable"), $ CYTrue()) : NULL)
41 ->* descriptor)));
42 }
43
44 static void CYImplicitReturn(CYStatement *&code) {
45 if (CYStatement *&last = CYGetLast(code))
46 last = last->Return();
47 }
48
49 CYExpression *CYAdd::Replace(CYContext &context) {
50 CYInfix::Replace(context);
51
52 CYString *lhs(dynamic_cast<CYString *>(lhs_));
53 CYString *rhs(dynamic_cast<CYString *>(rhs_));
54
55 if (lhs != NULL || rhs != NULL) {
56 if (lhs == NULL) {
57 lhs = lhs_->String(context);
58 if (lhs == NULL)
59 return this;
60 } else if (rhs == NULL) {
61 rhs = rhs_->String(context);
62 if (rhs == NULL)
63 return this;
64 }
65
66 return lhs->Concat(context, rhs);
67 }
68
69 if (CYNumber *lhn = lhs_->Number(context))
70 if (CYNumber *rhn = rhs_->Number(context))
71 return $D(lhn->Value() + rhn->Value());
72
73 return this;
74 }
75
76 CYExpression *CYAddressOf::Replace(CYContext &context) {
77 return $C0($M(rhs_, $S("$cya")));
78 }
79
80 CYArgument *CYArgument::Replace(CYContext &context) { $T(NULL)
81 context.Replace(value_);
82 next_ = next_->Replace(context);
83
84 if (value_ == NULL) {
85 if (next_ == NULL)
86 return NULL;
87 else
88 value_ = $U;
89 }
90
91 return this;
92 }
93
94 CYExpression *CYArray::Replace(CYContext &context) {
95 if (elements_ != NULL)
96 elements_->Replace(context);
97 return this;
98 }
99
100 CYExpression *CYArrayComprehension::Replace(CYContext &context) {
101 CYVariable *cyv($V("$cyv"));
102
103 return $C0($F(NULL, $P1($L($I("$cyv")), comprehensions_->Parameters(context)), $$->*
104 $E($ CYAssign(cyv, $ CYArray()))->*
105 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
106 $ CYReturn(cyv)
107 ));
108 }
109
110 CYExpression *CYAssignment::Replace(CYContext &context) {
111 context.Replace(lhs_);
112 context.Replace(rhs_);
113 return this;
114 }
115
116 CYStatement *CYBlock::Return() {
117 CYImplicitReturn(code_);
118 return this;
119 }
120
121 CYStatement *CYBlock::Replace(CYContext &context) {
122 context.ReplaceAll(code_);
123 if (code_ == NULL)
124 return $ CYEmpty();
125 return this;
126 }
127
128 CYStatement *CYBreak::Replace(CYContext &context) {
129 return this;
130 }
131
132 CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
133 CYArgument **argument(&arguments_);
134 while (*argument != NULL)
135 argument = &(*argument)->next_;
136 *argument = $ CYArgument(value);
137 return this;
138 }
139
140 CYExpression *CYCall::Replace(CYContext &context) {
141 context.Replace(function_);
142 arguments_->Replace(context);
143 return this;
144 }
145
146 namespace cy {
147 namespace Syntax {
148
149 void Catch::Replace(CYContext &context) { $T()
150 CYScope scope(true, context);
151
152 context.Replace(name_);
153 context.scope_->Declare(context, name_, CYIdentifierCatch);
154
155 context.ReplaceAll(code_);
156 scope.Close(context, code_);
157 }
158
159 } }
160
161 CYExpression *CYClassExpression::Replace(CYContext &context) {
162 CYBuilder builder;
163
164 CYIdentifier *super(context.Unique());
165
166 CYIdentifier *old(context.super_);
167 context.super_ = super;
168
169 CYIdentifier *constructor(context.Unique());
170 CYForEach (member, tail_->static_)
171 member->Replace(context, builder, $V(constructor), true);
172
173 CYIdentifier *prototype(context.Unique());
174 CYForEach (member, tail_->instance_)
175 member->Replace(context, builder, $V(prototype), true);
176
177 if (tail_->constructor_ == NULL)
178 tail_->constructor_ = $ CYFunctionExpression(NULL, NULL, NULL);
179 tail_->constructor_ = CYSuperize(context, tail_->constructor_);
180
181 context.super_ = old;
182
183 return $C1($ CYFunctionExpression(NULL, $P($L(super)), $$
184 ->* $ CYVar($L1($L(constructor, tail_->constructor_)))
185 ->* $ CYVar($L1($L(prototype, $ CYFunctionExpression(NULL, NULL, NULL))))
186 ->* $E($ CYAssign($M($V(prototype), $S("prototype")), $M($V(super), $S("prototype"))))
187 ->* $E($ CYAssign($V(prototype), $N($V(prototype))))
188 ->* CYDefineProperty($V(prototype), $S("constructor"), false, false, $ CYPropertyValue($S("value"), $V(constructor)))
189 ->* $ CYVar(builder.declarations_)
190 ->* builder.statements_
191 ->* CYDefineProperty($V(constructor), $S("prototype"), false, false, $ CYPropertyValue($S("value"), $V(prototype)))
192 ->* $ CYReturn($V(constructor))
193 ), tail_->extends_ ?: $V($I("Object")));
194 }
195
196 CYStatement *CYClassStatement::Replace(CYContext &context) {
197 return $ CYVar($L1($L(name_, $ CYClassExpression(name_, tail_))));
198 }
199
200 void CYClause::Replace(CYContext &context) { $T()
201 context.Replace(case_);
202 context.ReplaceAll(code_);
203 next_->Replace(context);
204 }
205
206 CYExpression *CYCompound::Replace(CYContext &context) {
207 context.Replace(expression_);
208 context.Replace(next_);
209
210 if (CYCompound *compound = dynamic_cast<CYCompound *>(expression_)) {
211 expression_ = compound->expression_;
212 compound->expression_ = compound->next_;
213 compound->next_ = next_;
214 next_ = compound;
215 }
216
217 return this;
218 }
219
220 CYFunctionParameter *CYCompound::Parameter() const {
221 CYFunctionParameter *next(next_->Parameter());
222 if (next == NULL)
223 return NULL;
224
225 CYFunctionParameter *parameter(expression_->Parameter());
226 if (parameter == NULL)
227 return NULL;
228
229 parameter->SetNext(next);
230 return parameter;
231 }
232
233 CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
234 CYFunctionParameter *next(next_->Parameters(context));
235 if (CYFunctionParameter *parameter = Parameter(context)) {
236 parameter->SetNext(next);
237 return parameter;
238 } else
239 return next;
240 }
241
242 CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
243 return next_ == NULL ? statement : next_->Replace(context, statement);
244 }
245
246 CYExpression *CYComputed::PropertyName(CYContext &context) {
247 return expression_;
248 }
249
250 CYExpression *CYCondition::Replace(CYContext &context) {
251 context.Replace(test_);
252 context.Replace(true_);
253 context.Replace(false_);
254 return this;
255 }
256
257 void CYContext::NonLocal(CYStatement *&statements) {
258 CYContext &context(*this);
259
260 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
261 CYIdentifier *cye($I("$cye")->Replace(context));
262 CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
263
264 CYStatement *declare(
265 $ CYVar($L1($L(unique, $ CYObject()))));
266
267 cy::Syntax::Catch *rescue(
268 $ cy::Syntax::Catch(cye, $$->*
269 $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->*
270 $ CYReturn($M($V(cye), $S("$cyv"))))->*
271 $ cy::Syntax::Throw($V(cye))));
272
273 // XXX: I don't understand any of this
274 context.Replace(declare);
275 rescue->Replace(context);
276
277 statements = $$->*
278 declare->*
279 $ cy::Syntax::Try(statements, rescue, NULL);
280 }
281 }
282
283 CYIdentifier *CYContext::Unique() {
284 return $ CYIdentifier($pool.strcat("$cy", $pool.itoa(unique_++), NULL));
285 }
286
287 CYStatement *CYContinue::Replace(CYContext &context) {
288 return this;
289 }
290
291 CYStatement *CYDebugger::Replace(CYContext &context) {
292 return this;
293 }
294
295 CYAssignment *CYDeclaration::Assignment(CYContext &context) {
296 if (initialiser_ == NULL)
297 return NULL;
298
299 CYAssignment *value($ CYAssign(Variable(context), initialiser_));
300 initialiser_ = NULL;
301 return value;
302 }
303
304 CYVariable *CYDeclaration::Variable(CYContext &context) {
305 return $V(identifier_);
306 }
307
308 CYStatement *CYDeclaration::ForEachIn(CYContext &context, CYExpression *value) {
309 return $ CYVar($L1($ CYDeclaration(identifier_, value)));
310 }
311
312 CYExpression *CYDeclaration::Replace(CYContext &context) {
313 context.Replace(identifier_);
314 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
315 return Variable(context);
316 }
317
318 void CYDeclarations::Replace(CYContext &context) { $T()
319 declaration_->Replace(context);
320 next_->Replace(context);
321 }
322
323 CYFunctionParameter *CYDeclarations::Parameter(CYContext &context) { $T(NULL)
324 return $ CYFunctionParameter($ CYDeclaration(declaration_->identifier_), next_->Parameter(context));
325 }
326
327 CYArgument *CYDeclarations::Argument(CYContext &context) { $T(NULL)
328 return $ CYArgument(declaration_->initialiser_, next_->Argument(context));
329 }
330
331 CYExpression *CYDeclarations::Expression(CYContext &context) { $T(NULL)
332 CYExpression *compound(next_->Expression(context));
333 if (CYAssignment *assignment = declaration_->Assignment(context))
334 if (compound == NULL)
335 compound = assignment;
336 else
337 compound = $ CYCompound(assignment, compound);
338 return compound;
339 }
340
341 CYExpression *CYDirectMember::Replace(CYContext &context) {
342 context.Replace(object_);
343 context.Replace(property_);
344 return this;
345 }
346
347 CYStatement *CYDoWhile::Replace(CYContext &context) {
348 context.Replace(test_);
349 context.ReplaceAll(code_);
350 return this;
351 }
352
353 void CYElementSpread::Replace(CYContext &context) {
354 context.Replace(value_);
355 }
356
357 void CYElementValue::Replace(CYContext &context) {
358 context.Replace(value_);
359 if (next_ != NULL)
360 next_->Replace(context);
361 }
362
363 CYStatement *CYEmpty::Replace(CYContext &context) {
364 return NULL;
365 }
366
367 CYExpression *CYEncodedType::Replace(CYContext &context) {
368 return typed_->Replace(context);
369 }
370
371 CYStatement *CYExpress::Return() {
372 return $ CYReturn(expression_);
373 }
374
375 CYStatement *CYExpress::Replace(CYContext &context) {
376 context.Replace(expression_);
377 return this;
378 }
379
380 CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
381 return $C1(this, value);
382 }
383
384 CYStatement *CYExpression::ForEachIn(CYContext &context, CYExpression *value) {
385 return $E($ CYAssign(this, value));
386 }
387
388 CYAssignment *CYExpression::Assignment(CYContext &context) {
389 return NULL;
390 }
391
392 CYFunctionParameter *CYExpression::Parameter() const {
393 return NULL;
394 }
395
396 CYStatement *CYExternal::Replace(CYContext &context) {
397 return $E($ CYAssign($V(typed_->identifier_), $C1(typed_->Replace(context), $C2($V("dlsym"), $V("RTLD_DEFAULT"), $S(typed_->identifier_->Word())))));
398 }
399
400 CYNumber *CYFalse::Number(CYContext &context) {
401 return $D(0);
402 }
403
404 CYString *CYFalse::String(CYContext &context) {
405 return $S("false");
406 }
407
408 CYExpression *CYFatArrow::Replace(CYContext &context) {
409 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
410 function->this_.SetNext(context.this_);
411 return function;
412 }
413
414 void CYFinally::Replace(CYContext &context) { $T()
415 context.ReplaceAll(code_);
416 }
417
418 CYStatement *CYFor::Replace(CYContext &context) {
419 context.Replace(initialiser_);
420 context.Replace(test_);
421 context.Replace(increment_);
422 context.ReplaceAll(code_);
423 return this;
424 }
425
426 CYExpression *CYForDeclarations::Replace(CYContext &context) {
427 declarations_->Replace(context);
428 return declarations_->Expression(context);
429 }
430
431 // XXX: this still feels highly suboptimal
432 CYStatement *CYForIn::Replace(CYContext &context) {
433 if (CYAssignment *assignment = initialiser_->Assignment(context))
434 return $ CYBlock($$->*
435 $E(assignment)->*
436 this
437 );
438
439 context.Replace(initialiser_);
440 context.Replace(set_);
441 context.ReplaceAll(code_);
442 return this;
443 }
444
445 CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
446 return $ CYFunctionParameter(declaration_);
447 }
448
449 CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
450 return $ CYForIn(declaration_->Variable(context), set_, CYComprehension::Replace(context, statement));
451 }
452
453 CYStatement *CYForOf::Replace(CYContext &context) {
454 if (CYAssignment *assignment = initialiser_->Assignment(context))
455 return $ CYBlock($$->*
456 $E(assignment)->*
457 this
458 );
459
460 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
461
462 return $ CYLetStatement($L2($ CYDeclaration(cys, set_), $ CYDeclaration(cyt)), $$->*
463 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
464 initialiser_->ForEachIn(context, $M($V(cys), $V(cyt)))->*
465 code_
466 ))
467 );
468 }
469
470 CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const {
471 return $ CYFunctionParameter(declaration_);
472 }
473
474 CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const {
475 CYIdentifier *cys($I("$cys"));
476
477 return $E($C0($F(NULL, $P1($L($I("$cys"))), $$->*
478 $E($ CYAssign($V(cys), set_))->*
479 $ CYForIn(declaration_->Variable(context), $V(cys), $ CYBlock($$->*
480 $E($ CYAssign(declaration_->Variable(context), $M($V(cys), declaration_->Variable(context))))->*
481 CYComprehension::Replace(context, statement)
482 ))
483 )));
484 }
485
486 void CYFunction::Replace(CYContext &context) {
487 CYThisScope *_this(context.this_);
488 context.this_ = &this_;
489 context.this_ = CYGetLast(context.this_);
490
491 CYIdentifier *super(context.super_);
492 context.super_ = super_;
493
494 CYNonLocal *nonlocal(context.nonlocal_);
495 CYNonLocal *nextlocal(context.nextlocal_);
496
497 bool localize;
498 if (nonlocal_ != NULL) {
499 localize = false;
500 context.nonlocal_ = nonlocal_;
501 } else {
502 localize = true;
503 nonlocal_ = $ CYNonLocal();
504 context.nextlocal_ = nonlocal_;
505 }
506
507 CYScope scope(!localize, context);
508
509 parameters_->Replace(context, code_);
510
511 context.ReplaceAll(code_);
512
513 if (implicit_)
514 CYImplicitReturn(code_);
515
516 if (CYIdentifier *identifier = this_.identifier_) {
517 context.scope_->Declare(context, identifier, CYIdentifierVariable);
518 code_ = $$
519 ->*$E($ CYAssign($V(identifier), $ CYThis()))
520 ->*code_
521 ;
522 }
523
524 if (localize)
525 context.NonLocal(code_);
526
527 context.nextlocal_ = nextlocal;
528 context.nonlocal_ = nonlocal;
529
530 context.super_ = super;
531 context.this_ = _this;
532
533 scope.Close(context, code_);
534 }
535
536 CYExpression *CYFunctionExpression::Replace(CYContext &context) {
537 CYScope scope(false, context);
538 if (name_ != NULL)
539 context.scope_->Declare(context, name_, CYIdentifierOther);
540 CYFunction::Replace(context);
541 scope.Close(context, code_);
542 return this;
543 }
544
545 void CYFunctionParameter::Replace(CYContext &context, CYStatement *&statements) { $T()
546 CYAssignment *assignment(initialiser_->Assignment(context));
547 context.Replace(initialiser_);
548
549 next_->Replace(context, statements);
550
551 if (assignment != NULL)
552 statements = $$->*
553 // XXX: this cast is quite incorrect
554 $ CYIf($ CYIdentical($ CYTypeOf(dynamic_cast<CYExpression *>(initialiser_)), $S("undefined")), $$->*
555 $E(assignment)
556 )->*
557 statements;
558 }
559
560 CYStatement *CYFunctionStatement::Replace(CYContext &context) {
561 context.scope_->Declare(context, name_, CYIdentifierOther);
562 CYFunction::Replace(context);
563 return this;
564 }
565
566 CYIdentifier *CYIdentifier::Replace(CYContext &context) {
567 if (replace_ != NULL && replace_ != this)
568 return replace_->Replace(context);
569 replace_ = context.scope_->Lookup(context, this);
570 return replace_;
571 }
572
573 CYStatement *CYIf::Return() {
574 CYImplicitReturn(true_);
575 CYImplicitReturn(false_);
576 return this;
577 }
578
579 CYStatement *CYIf::Replace(CYContext &context) {
580 context.Replace(test_);
581 context.ReplaceAll(true_);
582 context.ReplaceAll(false_);
583 return this;
584 }
585
586 CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
587 return NULL;
588 }
589
590 CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
591 return $ CYIf(test_, CYComprehension::Replace(context, statement));
592 }
593
594 CYStatement *CYImport::Replace(CYContext &context) {
595 return $ CYVar($L1($L($I(module_->part_->Word()), $C1($V("require"), module_->Replace(context, "/")))));
596 }
597
598 CYExpression *CYIndirect::Replace(CYContext &context) {
599 return $M(rhs_, $S("$cyi"));
600 }
601
602 CYExpression *CYIndirectMember::Replace(CYContext &context) {
603 return $M($ CYIndirect(object_), property_);
604 }
605
606 CYExpression *CYInfix::Replace(CYContext &context) {
607 context.Replace(lhs_);
608 context.Replace(rhs_);
609 return this;
610 }
611
612 CYStatement *CYLabel::Replace(CYContext &context) {
613 context.Replace(statement_);
614 return this;
615 }
616
617 CYExpression *CYLambda::Replace(CYContext &context) {
618 return $N2($V("Functor"), $ CYFunctionExpression(NULL, parameters_->Parameters(context), code_), parameters_->TypeSignature(context, typed_->Replace(context)));
619 }
620
621 CYStatement *CYLetStatement::Replace(CYContext &context) {
622 return $E($ CYCall(CYNonLocalize(context, $ CYFunctionExpression(NULL, declarations_->Parameter(context), code_)), declarations_->Argument(context)));
623 }
624
625 CYFunctionExpression *CYMethod::Constructor() {
626 return NULL;
627 }
628
629 void CYMethod::Replace(CYContext &context) {
630 CYFunction::Replace(context);
631 }
632
633 CYString *CYModule::Replace(CYContext &context, const char *separator) const {
634 if (next_ == NULL)
635 return $ CYString(part_);
636 return $ CYString($pool.strcat(next_->Replace(context, separator)->Value(), separator, part_->Word(), NULL));
637 }
638
639 CYExpression *CYMultiply::Replace(CYContext &context) {
640 CYInfix::Replace(context);
641
642 if (CYNumber *lhn = lhs_->Number(context))
643 if (CYNumber *rhn = rhs_->Number(context))
644 return $D(lhn->Value() * rhn->Value());
645
646 return this;
647 }
648
649 namespace cy {
650 namespace Syntax {
651
652 CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
653 CYSetLast(arguments_) = $ CYArgument(value);
654 return this;
655 }
656
657 CYExpression *New::Replace(CYContext &context) {
658 context.Replace(constructor_);
659 arguments_->Replace(context);
660 return this;
661 }
662
663 } }
664
665 CYNumber *CYNull::Number(CYContext &context) {
666 return $D(0);
667 }
668
669 CYString *CYNull::String(CYContext &context) {
670 return $S("null");
671 }
672
673 CYNumber *CYNumber::Number(CYContext &context) {
674 return this;
675 }
676
677 CYString *CYNumber::String(CYContext &context) {
678 // XXX: there is a precise algorithm for this
679 return $S($pool.sprintf(24, "%.17g", Value()));
680 }
681
682 CYExpression *CYNumber::PropertyName(CYContext &context) {
683 return String(context);
684 }
685
686 CYExpression *CYObject::Replace(CYContext &context) {
687 CYBuilder builder;
688 if (properties_ != NULL)
689 properties_ = properties_->ReplaceAll(context, builder, $ CYThis(), false);
690
691 if (builder) {
692 return $C1($M($ CYFunctionExpression(NULL, builder.declarations_->Parameter(context),
693 builder.statements_->*
694 $ CYReturn($ CYThis())
695 ), $S("call")), this, builder.declarations_->Argument(context));
696 }
697
698 CYForEach (property, properties_)
699 property->Replace(context);
700 return this;
701 }
702
703 CYExpression *CYParenthetical::Replace(CYContext &context) {
704 return expression_;
705 }
706
707 CYExpression *CYPostfix::Replace(CYContext &context) {
708 context.Replace(lhs_);
709 return this;
710 }
711
712 CYExpression *CYPrefix::Replace(CYContext &context) {
713 context.Replace(rhs_);
714 return this;
715 }
716
717 CYProperty *CYProperty::ReplaceAll(CYContext &context, CYBuilder &builder, CYExpression *self, bool update) {
718 update |= name_->Computed();
719 if (update)
720 Replace(context, builder, self, false);
721 if (next_ != NULL)
722 next_ = next_->ReplaceAll(context, builder, self, update);
723 return update ? next_ : this;
724 }
725
726 void CYProperty::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, bool protect) {
727 CYExpression *name(name_->PropertyName(context));
728 if (name_->Computed()) {
729 CYIdentifier *unique(context.Unique());
730 builder.declarations_->*$L1($L(unique, name));
731 name = $V(unique);
732 }
733
734 Replace(context, builder, self, name, protect);
735 }
736
737 void CYPropertyGetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
738 CYIdentifier *unique(context.Unique());
739 builder.declarations_
740 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
741 builder.statements_
742 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("get"), $V(unique)));
743 }
744
745 CYFunctionExpression *CYPropertyMethod::Constructor() {
746 return name_->Constructor() ? $ CYFunctionExpression(NULL, parameters_, code_) : NULL;
747 }
748
749 void CYPropertyMethod::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
750 CYIdentifier *unique(context.Unique());
751 builder.declarations_
752 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
753 builder.statements_
754 ->* (!protect ? $E($ CYAssign($M(self, name), $V(unique))) :
755 CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("value"), $V(unique), $ CYPropertyValue($S("writable"), $ CYTrue()))));
756 }
757
758 void CYPropertySetter::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
759 CYIdentifier *unique(context.Unique());
760 builder.declarations_
761 ->* $L1($L(unique, CYSuperize(context, $ CYFunctionExpression(NULL, parameters_, code_))));
762 builder.statements_
763 ->* CYDefineProperty(self, name, true, !protect, $ CYPropertyValue($S("set"), $V(unique)));
764 }
765
766 void CYPropertyValue::Replace(CYContext &context, CYBuilder &builder, CYExpression *self, CYExpression *name, bool protect) {
767 _assert(!protect);
768 CYIdentifier *unique(context.Unique());
769 builder.declarations_
770 ->* $L1($L(unique, value_));
771 builder.statements_
772 ->* $E($ CYAssign($M(self, name), $V(unique)));
773 }
774
775 void CYPropertyValue::Replace(CYContext &context) {
776 context.Replace(value_);
777 }
778
779 // XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
780 #define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
781 //#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
782
783 namespace {
784 struct IdentifierUsageLess :
785 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
786 {
787 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
788 if (lhs->usage_ != rhs->usage_)
789 return lhs->usage_ > rhs->usage_;
790 return lhs < rhs;
791 }
792 };
793
794 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
795 }
796
797 void CYScript::Replace(CYContext &context) {
798 CYScope scope(true, context);
799
800 context.nextlocal_ = $ CYNonLocal();
801 context.ReplaceAll(code_);
802 context.NonLocal(code_);
803
804 scope.Close(context, code_);
805
806 size_t offset(0);
807
808 CYCStringSet external;
809 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
810 external.insert((*i)->Word());
811
812 IdentifierUsages usages;
813
814 if (offset < context.rename_.size())
815 CYForEach (i, context.rename_[offset].identifier_)
816 usages.insert(i);
817
818 // XXX: totalling the probable occurrences and sorting by them would improve the result
819 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
820 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
821
822 const char *name;
823
824 if (context.options_.verbose_)
825 name = $pool.strcat("$", $pool.itoa(offset), NULL);
826 else {
827 char id[8];
828 id[7] = '\0';
829
830 id:
831 unsigned position(7), local(offset + 1);
832
833 do {
834 unsigned index(local % (sizeof(MappingSet) - 1));
835 local /= sizeof(MappingSet) - 1;
836 id[--position] = MappingSet[index];
837 } while (local != 0);
838
839 if (external.find(id + position) != external.end()) {
840 ++offset;
841 goto id;
842 }
843
844 name = $pool.strmemdup(id + position, 7 - position);
845 // XXX: at some point, this could become a keyword
846 }
847
848 CYForEach (identifier, i->identifier_)
849 identifier->Set(name);
850 }
851 }
852
853 CYStatement *CYReturn::Replace(CYContext &context) {
854 if (context.nonlocal_ != NULL) {
855 CYProperty *value(value_ == NULL ? NULL : $ CYPropertyValue($S("$cyv"), value_));
856 return $ cy::Syntax::Throw($ CYObject(
857 $ CYPropertyValue($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
858 ));
859 }
860
861 context.Replace(value_);
862 return this;
863 }
864
865 CYExpression *CYRubyBlock::Replace(CYContext &context) {
866 return call_->AddArgument(context, proc_->Replace(context));
867 }
868
869 CYExpression *CYRubyBlock::AddArgument(CYContext &context, CYExpression *value) {
870 return Replace(context)->AddArgument(context, value);
871 }
872
873 CYExpression *CYRubyProc::Replace(CYContext &context) {
874 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
875 function = CYNonLocalize(context, function);
876 function->implicit_ = true;
877 return function;
878 }
879
880 CYScope::CYScope(bool transparent, CYContext &context) :
881 transparent_(transparent),
882 parent_(context.scope_)
883 {
884 context.scope_ = this;
885 }
886
887 void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
888 if (!transparent_ || flags == CYIdentifierArgument || flags == CYIdentifierCatch)
889 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
890 else if (parent_ != NULL)
891 parent_->Declare(context, identifier, flags);
892 }
893
894 CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
895 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
896 return *insert.first;
897 }
898
899 void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
900 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
901 if (!insert.second) {
902 if ((*insert.first)->offset_ < identifier->offset_)
903 (*insert.first)->offset_ = identifier->offset_;
904 identifier->replace_ = *insert.first;
905 (*insert.first)->usage_ += identifier->usage_ + 1;
906 }
907 }
908
909 namespace {
910 struct IdentifierOffset {
911 size_t offset_;
912 CYIdentifierFlags flags_;
913 size_t usage_;
914 CYIdentifier *identifier_;
915
916 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
917 offset_(identifier->offset_),
918 flags_(flags),
919 usage_(identifier->usage_),
920 identifier_(identifier)
921 {
922 }
923 };
924
925 struct IdentifierOffsetLess :
926 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
927 {
928 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
929 if (lhs.offset_ != rhs.offset_)
930 return lhs.offset_ < rhs.offset_;
931 if (lhs.flags_ != rhs.flags_)
932 return lhs.flags_ < rhs.flags_;
933 /*if (lhs.usage_ != rhs.usage_)
934 return lhs.usage_ < rhs.usage_;*/
935 return lhs.identifier_ < rhs.identifier_;
936 }
937 };
938
939 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
940 }
941
942 void CYScope::Close(CYContext &context, CYStatement *&statements) {
943 context.scope_ = parent_;
944
945 if (parent_ == NULL)
946 return;
947
948 CYDeclarations *last(NULL), *curr(NULL);
949
950 IdentifierOffsets offsets;
951
952 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
953 if (i->second != CYIdentifierMagic)
954 offsets.insert(IdentifierOffset(i->first, i->second));
955
956 size_t offset(0);
957
958 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
959 if (i->flags_ == CYIdentifierVariable) {
960 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
961 if (last == NULL)
962 last = next;
963 if (curr != NULL)
964 curr->SetNext(next);
965 curr = next;
966 }
967
968 if (offset < i->offset_)
969 offset = i->offset_;
970 if (context.rename_.size() <= offset)
971 context.rename_.resize(offset + 1);
972
973 CYIdentifierUsage &rename(context.rename_[offset++]);
974 i->identifier_->SetNext(rename.identifier_);
975 rename.identifier_ = i->identifier_;
976 rename.usage_ += i->identifier_->usage_ + 1;
977 }
978
979 if (last != NULL) {
980 CYVar *var($ CYVar(last));
981 var->SetNext(statements);
982 statements = var;
983 }
984
985 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
986 if (internal_.find(*i) == internal_.end()) {
987 //std::cout << *i << '=' << offset << std::endl;
988 if ((*i)->offset_ < offset)
989 (*i)->offset_ = offset;
990 parent_->Merge(context, *i);
991 }
992 }
993
994 CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
995 return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
996 }
997
998 CYStatement *CYStatement::Return() {
999 return this;
1000 }
1001
1002 CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
1003 size_t size(size_ + rhs->size_);
1004 char *value($ char[size + 1]);
1005 memcpy(value, value_, size_);
1006 memcpy(value + size_, rhs->value_, rhs->size_);
1007 value[size] = '\0';
1008 return $S(value, size);
1009 }
1010
1011 CYNumber *CYString::Number(CYContext &context) {
1012 // XXX: there is a precise algorithm for this
1013 return NULL;
1014 }
1015
1016 CYExpression *CYString::PropertyName(CYContext &context) {
1017 return this;
1018 }
1019
1020 CYString *CYString::String(CYContext &context) {
1021 return this;
1022 }
1023
1024 CYExpression *CYSuperAccess::Replace(CYContext &context) {
1025 return $C1($M($M($M($V(context.super_), $S("prototype")), property_), $S("bind")), $ CYThis());
1026 }
1027
1028 CYExpression *CYSuperCall::Replace(CYContext &context) {
1029 return $C($C1($M($V(context.super_), $S("bind")), $ CYThis()), arguments_);
1030 }
1031
1032 CYStatement *CYSwitch::Replace(CYContext &context) {
1033 context.Replace(value_);
1034 clauses_->Replace(context);
1035 return this;
1036 }
1037
1038 CYExpression *CYTemplate::Replace(CYContext &context) {
1039 return $C2($M($M($M($V("String"), $S("prototype")), $S("concat")), $S("apply")), $S(""), $ CYArray($ CYElementValue(string_, spans_->Replace(context))));
1040 }
1041
1042 CYExpression *CYThis::Replace(CYContext &context) {
1043 if (context.this_ != NULL)
1044 return $V(context.this_->Identifier(context));
1045 return this;
1046 }
1047
1048 namespace cy {
1049 namespace Syntax {
1050
1051 CYStatement *Throw::Replace(CYContext &context) {
1052 context.Replace(value_);
1053 return this;
1054 }
1055
1056 } }
1057
1058 CYExpression *CYTrivial::Replace(CYContext &context) {
1059 return this;
1060 }
1061
1062 CYNumber *CYTrue::Number(CYContext &context) {
1063 return $D(1);
1064 }
1065
1066 CYString *CYTrue::String(CYContext &context) {
1067 return $S("true");
1068 }
1069
1070 namespace cy {
1071 namespace Syntax {
1072
1073 CYStatement *Try::Replace(CYContext &context) {
1074 context.ReplaceAll(code_);
1075 catch_->Replace(context);
1076 finally_->Replace(context);
1077 return this;
1078 }
1079
1080 } }
1081
1082 CYExpression *CYTypeArrayOf::Replace_(CYContext &context, CYExpression *type) {
1083 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("arrayOf")), $ CYArgument(size_)));
1084 }
1085
1086 CYExpression *CYTypeBlockWith::Replace_(CYContext &context, CYExpression *type) {
1087 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("blockWith")), parameters_->Argument(context)));
1088 }
1089
1090 CYExpression *CYTypeConstant::Replace_(CYContext &context, CYExpression *type) {
1091 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("constant"))));
1092 }
1093
1094 CYStatement *CYTypeDefinition::Replace(CYContext &context) {
1095 return $E($ CYAssign($V(typed_->identifier_), typed_->Replace(context)));
1096 }
1097
1098 CYExpression *CYTypeError::Replace(CYContext &context) {
1099 _assert(false);
1100 return NULL;
1101 }
1102
1103 CYExpression *CYTypeModifier::Replace(CYContext &context, CYExpression *type) { $T(type)
1104 return Replace_(context, type);
1105 }
1106
1107 CYExpression *CYTypeFunctionWith::Replace_(CYContext &context, CYExpression *type) {
1108 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("functionWith")), parameters_->Argument(context)));
1109 }
1110
1111 CYExpression *CYTypeLong::Replace(CYContext &context) {
1112 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("long")));
1113 }
1114
1115 CYExpression *CYTypePointerTo::Replace_(CYContext &context, CYExpression *type) {
1116 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("pointerTo"))));
1117 }
1118
1119 CYExpression *CYTypeShort::Replace(CYContext &context) {
1120 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("short")));
1121 }
1122
1123 CYExpression *CYTypeSigned::Replace(CYContext &context) {
1124 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("signed")));
1125 }
1126
1127 CYExpression *CYTypeUnsigned::Replace(CYContext &context) {
1128 return $ CYCall($ CYDirectMember(specifier_->Replace(context), $ CYString("unsigned")));
1129 }
1130
1131 CYExpression *CYTypeVariable::Replace(CYContext &context) {
1132 return $V(name_);
1133 }
1134
1135 CYExpression *CYTypeVoid::Replace(CYContext &context) {
1136 return $N1($V("Type"), $ CYString("v"));
1137 }
1138
1139 CYExpression *CYTypeVolatile::Replace_(CYContext &context, CYExpression *type) {
1140 return next_->Replace(context, $ CYCall($ CYDirectMember(type, $ CYString("volatile"))));
1141 }
1142
1143 CYExpression *CYTypedIdentifier::Replace(CYContext &context) {
1144 return modifier_->Replace(context, specifier_->Replace(context));
1145 }
1146
1147 CYTypeFunctionWith *CYTypedIdentifier::Function() {
1148 CYTypeModifier **modifier(&modifier_);
1149 if (*modifier == NULL)
1150 return NULL;
1151 while ((*modifier)->next_ != NULL)
1152 modifier = &(*modifier)->next_;
1153 CYTypeFunctionWith *function((*modifier)->Function());
1154 if (function == NULL)
1155 return NULL;
1156 *modifier = NULL;
1157 return function;
1158 }
1159
1160 CYArgument *CYTypedParameter::Argument(CYContext &context) { $T(NULL)
1161 return $ CYArgument(typed_->Replace(context), next_->Argument(context));
1162 }
1163
1164 CYFunctionParameter *CYTypedParameter::Parameters(CYContext &context) { $T(NULL)
1165 return $ CYFunctionParameter($ CYDeclaration(typed_->identifier_ ?: context.Unique()), next_->Parameters(context));
1166 }
1167
1168 CYExpression *CYTypedParameter::TypeSignature(CYContext &context, CYExpression *prefix) { $T(prefix)
1169 return next_->TypeSignature(context, $ CYAdd(prefix, typed_->Replace(context)));
1170 }
1171
1172 CYStatement *CYVar::Replace(CYContext &context) {
1173 declarations_->Replace(context);
1174 if (CYExpression *expression = declarations_->Expression(context))
1175 return $E(expression);
1176 return $ CYEmpty();
1177 }
1178
1179 CYExpression *CYVariable::Replace(CYContext &context) {
1180 context.Replace(name_);
1181 return this;
1182 }
1183
1184 CYFunctionParameter *CYVariable::Parameter() const {
1185 return $ CYFunctionParameter($ CYDeclaration(name_));
1186 }
1187
1188 CYStatement *CYWhile::Replace(CYContext &context) {
1189 context.Replace(test_);
1190 context.ReplaceAll(code_);
1191 return this;
1192 }
1193
1194 CYStatement *CYWith::Replace(CYContext &context) {
1195 context.Replace(scope_);
1196 context.ReplaceAll(code_);
1197 return this;
1198 }
1199
1200 CYExpression *CYWord::PropertyName(CYContext &context) {
1201 return $S(this);
1202 }