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