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