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