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