]> git.saurik.com Git - cycript.git/blame - Replace.cpp
Do not check against classes that are not instanced.
[cycript.git] / Replace.cpp
CommitLineData
b3378a02
JF
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
4644480a
JF
3*/
4
b3378a02 5/* GNU Lesser General Public License, Version 3 {{{ */
4644480a 6/*
b3378a02
JF
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
4644480a 11 *
b3378a02
JF
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
4644480a 16 *
b3378a02
JF
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19**/
4644480a
JF
20/* }}} */
21
3b52fd1a 22#include "Parser.hpp"
6a981250 23#include "Replace.hpp"
3b52fd1a 24
3b52fd1a
JF
25#include <iomanip>
26
4644480a
JF
27CYExpression *CYAdd::Replace(CYContext &context) {
28 CYInfix::Replace(context);
29
30 CYExpression *lhp(lhs_->Primitive(context));
31 CYExpression *rhp(rhs_->Primitive(context));
32
33 CYString *lhs(dynamic_cast<CYString *>(lhp));
34 CYString *rhs(dynamic_cast<CYString *>(rhp));
35
36 if (lhs != NULL || rhs != NULL) {
37 if (lhs == NULL) {
38 lhs = lhp->String(context);
39 if (lhs == NULL)
029bc65b 40 return this;
4644480a
JF
41 } else if (rhs == NULL) {
42 rhs = rhp->String(context);
43 if (rhs == NULL)
029bc65b 44 return this;
4644480a
JF
45 }
46
47 return lhs->Concat(context, rhs);
48 }
49
50 if (CYNumber *lhn = lhp->Number(context))
51 if (CYNumber *rhn = rhp->Number(context))
52 return $D(lhn->Value() + rhn->Value());
53
029bc65b 54 return this;
4644480a
JF
55}
56
3b52fd1a
JF
57CYExpression *CYAddressOf::Replace(CYContext &context) {
58 CYPrefix::Replace(context);
59 return $C0($M(rhs_, $S("$cya")));
60}
61
62void CYArgument::Replace(CYContext &context) { $T()
63 context.Replace(value_);
64 next_->Replace(context);
65}
66
67CYExpression *CYArray::Replace(CYContext &context) {
68 elements_->Replace(context);
029bc65b 69 return this;
3b52fd1a
JF
70}
71
72CYExpression *CYArrayComprehension::Replace(CYContext &context) {
73 CYVariable *cyv($V("$cyv"));
74
75 return $C0($F(NULL, $P1("$cyv", comprehensions_->Parameters(context)), $$->*
76 $E($ CYAssign(cyv, $ CYArray()))->*
77 comprehensions_->Replace(context, $E($C1($M(cyv, $S("push")), expression_)))->*
78 $ CYReturn(cyv)
79 ));
80}
81
82CYExpression *CYAssignment::Replace(CYContext &context) {
83 context.Replace(lhs_);
84 context.Replace(rhs_);
029bc65b 85 return this;
3b52fd1a
JF
86}
87
88CYStatement *CYBlock::Replace(CYContext &context) {
cde20a5a 89 context.ReplaceAll(statements_);
029bc65b
JF
90 if (statements_ == NULL)
91 return $ CYEmpty();
92 return this;
3b52fd1a
JF
93}
94
95CYStatement *CYBreak::Replace(CYContext &context) {
029bc65b 96 return this;
3b52fd1a
JF
97}
98
6c093cce
JF
99CYExpression *CYCall::AddArgument(CYContext &context, CYExpression *value) {
100 CYArgument **argument(&arguments_);
101 while (*argument != NULL)
102 argument = &(*argument)->next_;
103 *argument = $ CYArgument(value);
104 return this;
105}
106
3b52fd1a
JF
107CYExpression *CYCall::Replace(CYContext &context) {
108 context.Replace(function_);
109 arguments_->Replace(context);
029bc65b 110 return this;
3b52fd1a
JF
111}
112
37954781
JF
113namespace cy {
114namespace Syntax {
115
116void Catch::Replace(CYContext &context) { $T()
daf22a65
JF
117 CYScope scope(CYScopeCatch, context, code_.statements_);
118
119 context.Replace(name_);
120 context.scope_->Declare(context, name_, CYIdentifierCatch);
121
3b52fd1a 122 code_.Replace(context);
daf22a65 123 scope.Close();
3b52fd1a
JF
124}
125
37954781
JF
126} }
127
3b52fd1a
JF
128void CYClause::Replace(CYContext &context) { $T()
129 context.Replace(case_);
cde20a5a 130 context.ReplaceAll(statements_);
3b52fd1a
JF
131 next_->Replace(context);
132}
133
320ce753 134CYStatement *CYComment::Replace(CYContext &context) {
029bc65b 135 return this;
320ce753
JF
136}
137
3b52fd1a 138CYExpression *CYCompound::Replace(CYContext &context) {
cde20a5a 139 context.ReplaceAll(expressions_);
9efd8b03 140 return this;
3b52fd1a
JF
141}
142
143CYFunctionParameter *CYComprehension::Parameters(CYContext &context) const { $T(NULL)
144 CYFunctionParameter *next(next_->Parameters(context));
145 if (CYFunctionParameter *parameter = Parameter(context)) {
146 parameter->SetNext(next);
147 return parameter;
148 } else
149 return next;
150}
151
152CYStatement *CYComprehension::Replace(CYContext &context, CYStatement *statement) const {
153 return next_ == NULL ? statement : next_->Replace(context, statement);
154}
155
156CYExpression *CYCondition::Replace(CYContext &context) {
157 context.Replace(test_);
158 context.Replace(true_);
159 context.Replace(false_);
029bc65b 160 return this;
3b52fd1a
JF
161}
162
ab2aa221
JF
163void CYContext::NonLocal(CYStatement *&statements) {
164 CYContext &context(*this);
165
06293152 166 if (nextlocal_ != NULL && nextlocal_->identifier_ != NULL) {
daf22a65
JF
167 CYIdentifier *cye($I("$cye")->Replace(context));
168 CYIdentifier *unique(nextlocal_->identifier_->Replace(context));
169
170 CYStatement *declare(
171 $ CYVar($L1($L(unique, $ CYObject()))));
172
173 cy::Syntax::Catch *rescue(
174 $ cy::Syntax::Catch(cye, $$->*
4e58e244
JF
175 $ CYIf($ CYIdentical($M($V(cye), $S("$cyk")), $V(unique)), $$->*
176 $ CYReturn($M($V(cye), $S("$cyv"))))->*
177 $ cy::Syntax::Throw($V(cye))));
daf22a65 178
577bfbfa 179 context.Replace(declare);
daf22a65 180 rescue->Replace(context);
ab2aa221
JF
181
182 statements = $$->*
daf22a65
JF
183 declare->*
184 $ cy::Syntax::Try(statements, rescue, NULL);
ab2aa221
JF
185 }
186}
187
188CYIdentifier *CYContext::Unique() {
2eb8215d 189 return $ CYIdentifier(apr_psprintf($pool, "$cy%u", unique_++));
ab2aa221
JF
190}
191
3b52fd1a 192CYStatement *CYContinue::Replace(CYContext &context) {
029bc65b
JF
193 return this;
194}
195
196CYAssignment *CYDeclaration::Assignment(CYContext &context) {
197 CYExpression *variable(Replace(context));
198 return initialiser_ == NULL ? NULL : $ CYAssign(variable, initialiser_);
3b52fd1a
JF
199}
200
201CYExpression *CYDeclaration::ForEachIn(CYContext &context) {
4e58e244 202 return $V(identifier_);
3b52fd1a
JF
203}
204
029bc65b 205CYExpression *CYDeclaration::Replace(CYContext &context) {
a86e34d0
JF
206 context.Replace(identifier_);
207 context.scope_->Declare(context, identifier_, CYIdentifierVariable);
4e58e244 208 return $V(identifier_);
3b52fd1a
JF
209}
210
550ee46a
JF
211CYProperty *CYDeclarations::Property(CYContext &context) { $T(NULL)
212 return $ CYProperty(declaration_->identifier_, declaration_->initialiser_ ?: $U, next_->Property(context));
213}
214
029bc65b 215CYCompound *CYDeclarations::Replace(CYContext &context) {
aeb295b7 216 CYCompound *compound(next_ == NULL ? $ CYCompound() : next_->Replace(context));
029bc65b
JF
217 if (CYAssignment *assignment = declaration_->Assignment(context))
218 compound->AddPrev(assignment);
219 return compound;
3b52fd1a
JF
220}
221
222CYExpression *CYDirectMember::Replace(CYContext &context) {
223 Replace_(context);
029bc65b 224 return this;
3b52fd1a
JF
225}
226
227CYStatement *CYDoWhile::Replace(CYContext &context) {
228 context.Replace(test_);
229 context.Replace(code_);
029bc65b 230 return this;
3b52fd1a
JF
231}
232
233void CYElement::Replace(CYContext &context) { $T()
234 context.Replace(value_);
235 next_->Replace(context);
236}
237
029bc65b
JF
238CYStatement *CYEmpty::Collapse(CYContext &context) {
239 return next_;
240}
241
3b52fd1a 242CYStatement *CYEmpty::Replace(CYContext &context) {
029bc65b
JF
243 return this;
244}
245
246CYStatement *CYExpress::Collapse(CYContext &context) {
247 if (CYExpress *express = dynamic_cast<CYExpress *>(next_)) {
248 CYCompound *next(dynamic_cast<CYCompound *>(express->expression_));
249 if (next == NULL)
250 next = $ CYCompound(express->expression_);
251 next->AddPrev(expression_);
252 expression_ = next;
253 SetNext(express->next_);
254 }
255
256 return this;
3b52fd1a
JF
257}
258
259CYStatement *CYExpress::Replace(CYContext &context) {
260 context.Replace(expression_);
029bc65b
JF
261 if (expression_ == NULL)
262 return $ CYEmpty();
263 return this;
3b52fd1a
JF
264}
265
6c093cce
JF
266CYExpression *CYExpression::AddArgument(CYContext &context, CYExpression *value) {
267 return $C1(this, value);
268}
269
3b52fd1a
JF
270CYExpression *CYExpression::ClassName(CYContext &context, bool object) {
271 return this;
272}
273
274CYExpression *CYExpression::ForEachIn(CYContext &context) {
275 return this;
276}
277
4644480a
JF
278CYNumber *CYFalse::Number(CYContext &context) {
279 return $D(0);
280}
281
282CYString *CYFalse::String(CYContext &context) {
283 return $S("false");
284}
285
3b52fd1a
JF
286void CYFinally::Replace(CYContext &context) { $T()
287 code_.Replace(context);
288}
289
290CYStatement *CYFor::Replace(CYContext &context) {
029bc65b 291 context.Replace(initialiser_);
3b52fd1a
JF
292 context.Replace(test_);
293 context.Replace(increment_);
294 context.Replace(code_);
029bc65b 295 return this;
3b52fd1a
JF
296}
297
298CYStatement *CYForIn::Replace(CYContext &context) {
029bc65b
JF
299 // XXX: this actually might need a prefix statement
300 context.Replace(initialiser_);
3b52fd1a
JF
301 context.Replace(set_);
302 context.Replace(code_);
029bc65b 303 return this;
3b52fd1a
JF
304}
305
306CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const {
307 return $ CYFunctionParameter(name_);
308}
309
310CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const {
4e58e244 311 return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement));
3b52fd1a
JF
312}
313
314CYStatement *CYForEachIn::Replace(CYContext &context) {
2eb8215d 315 CYIdentifier *cys($I("$cys")), *cyt($I("$cyt"));
3b52fd1a 316
2eb8215d
JF
317 return $ CYLet($L2($L(cys, set_), $L(cyt)), $$->*
318 $ CYForIn($V(cyt), $V(cys), $ CYBlock($$->*
319 $E($ CYAssign(initialiser_->ForEachIn(context), $M($V(cys), $V(cyt))))->*
3b52fd1a
JF
320 code_
321 ))
550ee46a 322 );
3b52fd1a
JF
323}
324
325CYFunctionParameter *CYForEachInComprehension::Parameter(CYContext &context) const {
326 return $ CYFunctionParameter(name_);
327}
328
329CYStatement *CYForEachInComprehension::Replace(CYContext &context, CYStatement *statement) const {
2eb8215d 330 CYIdentifier *cys($I("cys"));
3b52fd1a
JF
331
332 return $E($C0($F(NULL, $P1("$cys"), $$->*
2eb8215d
JF
333 $E($ CYAssign($V(cys), set_))->*
334 $ CYForIn($V(name_), $V(cys), $ CYBlock($$->*
335 $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->*
3b52fd1a
JF
336 CYComprehension::Replace(context, statement)
337 ))
338 )));
339}
340
14ec9e00 341void CYFunction::Inject(CYContext &context) {
6a981250 342 context.Replace(name_);
a86e34d0 343 context.scope_->Declare(context, name_, CYIdentifierOther);
14ec9e00
JF
344}
345
346void CYFunction::Replace_(CYContext &context, bool outer) {
347 if (outer)
348 Inject(context);
349
daf22a65 350 CYScope scope(CYScopeFunction, context, code_.statements_);
029bc65b 351
06293152
JF
352 CYNonLocal *nonlocal(context.nonlocal_);
353 CYNonLocal *nextlocal(context.nextlocal_);
354
ab2aa221 355 bool localize;
06293152 356 if (nonlocal_ != NULL) {
ab2aa221 357 localize = false;
06293152
JF
358 context.nonlocal_ = nonlocal_;
359 } else {
ab2aa221
JF
360 localize = true;
361 nonlocal_ = $ CYNonLocal();
06293152 362 context.nextlocal_ = nonlocal_;
ab2aa221
JF
363 }
364
14ec9e00
JF
365 if (!outer && name_ != NULL)
366 Inject(context);
367
6c093cce
JF
368 if (parameters_ != NULL)
369 parameters_ = parameters_->Replace(context, code_);
daf22a65 370
3b52fd1a 371 code_.Replace(context);
029bc65b 372
ab2aa221
JF
373 if (localize)
374 context.NonLocal(code_.statements_);
06293152
JF
375
376 context.nextlocal_ = nextlocal;
ab2aa221
JF
377 context.nonlocal_ = nonlocal;
378
daf22a65 379 scope.Close();
3b52fd1a
JF
380}
381
382CYExpression *CYFunctionExpression::Replace(CYContext &context) {
14ec9e00 383 Replace_(context, false);
029bc65b
JF
384 return this;
385}
386
4e11a430 387CYFunctionParameter *CYFunctionParameter::Replace(CYContext &context, CYBlock &code) {
577bfbfa 388 context.Replace(name_);
a86e34d0 389 context.scope_->Declare(context, name_, CYIdentifierArgument);
4e11a430
JF
390 if (next_ != NULL)
391 next_ = next_->Replace(context, code);
392 return this;
3b52fd1a
JF
393}
394
395CYStatement *CYFunctionStatement::Replace(CYContext &context) {
14ec9e00 396 Replace_(context, true);
029bc65b
JF
397 return this;
398}
399
400CYIdentifier *CYIdentifier::Replace(CYContext &context) {
a846a8cd 401 if (replace_ != NULL && replace_ != this)
de9fc71b 402 return replace_->Replace(context);
a846a8cd 403 replace_ = context.scope_->Lookup(context, this);
a86e34d0 404 return replace_;
3b52fd1a
JF
405}
406
407CYStatement *CYIf::Replace(CYContext &context) {
408 context.Replace(test_);
409 context.Replace(true_);
410 context.Replace(false_);
029bc65b 411 return this;
3b52fd1a
JF
412}
413
414CYFunctionParameter *CYIfComprehension::Parameter(CYContext &context) const {
415 return NULL;
416}
417
418CYStatement *CYIfComprehension::Replace(CYContext &context, CYStatement *statement) const {
419 return $ CYIf(test_, CYComprehension::Replace(context, statement));
420}
421
422CYExpression *CYIndirect::Replace(CYContext &context) {
423 CYPrefix::Replace(context);
424 return $M(rhs_, $S("$cyi"));
425}
426
427CYExpression *CYIndirectMember::Replace(CYContext &context) {
428 Replace_(context);
429 return $M($ CYIndirect(object_), property_);
430}
431
432CYExpression *CYInfix::Replace(CYContext &context) {
433 context.Replace(lhs_);
434 context.Replace(rhs_);
029bc65b 435 return this;
3b52fd1a
JF
436}
437
438CYStatement *CYLabel::Replace(CYContext &context) {
439 context.Replace(statement_);
029bc65b 440 return this;
3b52fd1a
JF
441}
442
550ee46a
JF
443CYStatement *CYLet::Replace(CYContext &context) {
444 return $ CYWith($ CYObject(declarations_->Property(context)), &code_);
445}
446
3b52fd1a
JF
447void CYMember::Replace_(CYContext &context) {
448 context.Replace(object_);
449 context.Replace(property_);
450}
451
2eb8215d
JF
452namespace cy {
453namespace Syntax {
454
455CYExpression *New::AddArgument(CYContext &context, CYExpression *value) {
359e8b82 456 CYSetLast(arguments_, $ CYArgument(value));
6c093cce
JF
457 return this;
458}
459
2eb8215d 460CYExpression *New::Replace(CYContext &context) {
3b52fd1a
JF
461 context.Replace(constructor_);
462 arguments_->Replace(context);
029bc65b 463 return this;
3b52fd1a
JF
464}
465
2eb8215d
JF
466} }
467
4644480a
JF
468CYNumber *CYNull::Number(CYContext &context) {
469 return $D(0);
470}
471
472CYString *CYNull::String(CYContext &context) {
473 return $S("null");
474}
475
476CYNumber *CYNumber::Number(CYContext &context) {
477 return this;
478}
479
480CYString *CYNumber::String(CYContext &context) {
481 // XXX: there is a precise algorithm for this
2eb8215d 482 return $S(apr_psprintf($pool, "%.17g", Value()));
4644480a
JF
483}
484
3b52fd1a
JF
485CYExpression *CYObject::Replace(CYContext &context) {
486 properties_->Replace(context);
029bc65b 487 return this;
3b52fd1a
JF
488}
489
4e11a430
JF
490CYFunctionParameter *CYOptionalFunctionParameter::Replace(CYContext &context, CYBlock &code) {
491 CYFunctionParameter *parameter($ CYFunctionParameter(name_, next_));
492 parameter = parameter->Replace(context, code);
577bfbfa 493 context.Replace(initializer_);
4e11a430 494
4e58e244 495 CYVariable *name($V(name_));
4e11a430
JF
496 code.AddPrev($ CYIf($ CYIdentical($ CYTypeOf(name), $S("undefined")), $$->*
497 $E($ CYAssign(name, initializer_))
498 ));
499
500 return parameter;
501}
502
3b52fd1a
JF
503CYExpression *CYPostfix::Replace(CYContext &context) {
504 context.Replace(lhs_);
029bc65b 505 return this;
3b52fd1a
JF
506}
507
508CYExpression *CYPrefix::Replace(CYContext &context) {
509 context.Replace(rhs_);
029bc65b 510 return this;
3b52fd1a
JF
511}
512
10711823 513// XXX: this is evil evil black magic. don't ask, don't tell... don't believe!
45d0c928 514#define MappingSet "0etnirsoalfucdphmgyvbxTwSNECAFjDLkMOIBPqzRH$_WXUVGYKQJZ"
10711823 515//#define MappingSet "0abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"
45d0c928 516
e013809d
JF
517namespace {
518 struct IdentifierUsageLess :
519 std::binary_function<CYIdentifier *, CYIdentifier *, bool>
520 {
521 _finline bool operator ()(CYIdentifier *lhs, CYIdentifier *rhs) const {
522 if (lhs->usage_ != rhs->usage_)
523 return lhs->usage_ > rhs->usage_;
524 return lhs < rhs;
525 }
526 };
527
528 typedef std::set<CYIdentifier *, IdentifierUsageLess> IdentifierUsages;
529}
530
3b52fd1a 531void CYProgram::Replace(CYContext &context) {
daf22a65 532 CYScope scope(CYScopeProgram, context, statements_);
ab2aa221 533
06293152 534 context.nextlocal_ = $ CYNonLocal();
cde20a5a 535 context.ReplaceAll(statements_);
ab2aa221
JF
536 context.NonLocal(statements_);
537
daf22a65 538 scope.Close();
14ec9e00
JF
539
540 size_t offset(0);
541
6a981250 542 CYCStringSet external;
a846a8cd 543 for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i)
6a981250
JF
544 external.insert((*i)->Word());
545
e013809d
JF
546 IdentifierUsages usages;
547
a846a8cd 548 if (offset < context.rename_.size())
c2c9f509
JF
549 CYForEach (i, context.rename_[offset].identifier_)
550 usages.insert(i);
e013809d 551
14ec9e00 552 // XXX: totalling the probable occurrences and sorting by them would improve the result
a846a8cd 553 for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) {
de9fc71b
JF
554 //std::cout << *i << ":" << (*i)->offset_ << std::endl;
555
14ec9e00
JF
556 const char *name;
557
558 if (context.options_.verbose_)
2eb8215d 559 name = apr_psprintf($pool, "$%"APR_SIZE_T_FMT"", offset);
14ec9e00
JF
560 else {
561 char id[8];
562 id[7] = '\0';
563
564 id:
565 unsigned position(7), local(offset + 1);
566
567 do {
45d0c928
JF
568 unsigned index(local % (sizeof(MappingSet) - 1));
569 local /= sizeof(MappingSet) - 1;
570 id[--position] = MappingSet[index];
14ec9e00
JF
571 } while (local != 0);
572
6a981250 573 if (external.find(id + position) != external.end()) {
14ec9e00
JF
574 ++offset;
575 goto id;
576 }
577
2eb8215d 578 name = apr_pstrmemdup($pool, id + position, 7 - position);
14ec9e00
JF
579 // XXX: at some point, this could become a keyword
580 }
581
c2c9f509 582 CYForEach (identifier, i->identifier_)
14ec9e00
JF
583 identifier->Set(name);
584 }
3b52fd1a
JF
585}
586
587void CYProperty::Replace(CYContext &context) { $T()
588 context.Replace(value_);
589 next_->Replace(context);
590}
591
592CYStatement *CYReturn::Replace(CYContext &context) {
ab2aa221
JF
593 if (context.nonlocal_ != NULL) {
594 CYProperty *value(value_ == NULL ? NULL : $ CYProperty($S("$cyv"), value_));
595 return $ cy::Syntax::Throw($ CYObject(
4e58e244 596 $ CYProperty($S("$cyk"), $V(context.nonlocal_->Target(context)), value)
ab2aa221
JF
597 ));
598 }
599
3b52fd1a 600 context.Replace(value_);
029bc65b
JF
601 return this;
602}
603
6c093cce
JF
604CYExpression *CYRubyBlock::Replace(CYContext &context) {
605 // XXX: this needs to do something much more epic to handle return
606 return call_->AddArgument(context, proc_->Replace(context));
607}
608
609CYExpression *CYRubyProc::Replace(CYContext &context) {
ab2aa221 610 CYFunctionExpression *function($ CYFunctionExpression(NULL, parameters_, code_));
06293152 611 function->nonlocal_ = context.nextlocal_;
ab2aa221 612 return function;
6c093cce
JF
613}
614
daf22a65
JF
615CYScope::CYScope(CYScopeType type, CYContext &context, CYStatement *&statements) :
616 type_(type),
617 context_(context),
618 statements_(statements),
619 parent_(context.scope_)
620{
621 context_.scope_ = this;
622}
623
e4676127
JF
624CYScope::~CYScope() {
625}
626
daf22a65
JF
627void CYScope::Close() {
628 context_.scope_ = parent_;
629 Scope(context_, statements_);
630}
631
a86e34d0 632void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
daf22a65
JF
633 if (type_ == CYScopeCatch && flags != CYIdentifierCatch)
634 parent_->Declare(context, identifier, flags);
635 else
636 internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
a86e34d0
JF
637}
638
639CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
640 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
641 return *insert.first;
642}
643
6a981250 644void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
10711823 645 std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
6a981250
JF
646 if (!insert.second) {
647 if ((*insert.first)->offset_ < identifier->offset_)
648 (*insert.first)->offset_ = identifier->offset_;
649 identifier->replace_ = *insert.first;
e013809d 650 (*insert.first)->usage_ += identifier->usage_ + 1;
029bc65b
JF
651 }
652}
653
10711823
JF
654namespace {
655 struct IdentifierOffset {
656 size_t offset_;
657 CYIdentifierFlags flags_;
e013809d 658 size_t usage_;
10711823
JF
659 CYIdentifier *identifier_;
660
e013809d
JF
661 IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
662 offset_(identifier->offset_),
10711823 663 flags_(flags),
e013809d 664 usage_(identifier->usage_),
10711823
JF
665 identifier_(identifier)
666 {
667 }
668 };
669
670 struct IdentifierOffsetLess :
671 std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
672 {
673 _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
674 if (lhs.offset_ != rhs.offset_)
675 return lhs.offset_ < rhs.offset_;
676 if (lhs.flags_ != rhs.flags_)
677 return lhs.flags_ < rhs.flags_;
e013809d 678 /*if (lhs.usage_ != rhs.usage_)
a846a8cd 679 return lhs.usage_ < rhs.usage_;*/
10711823
JF
680 return lhs.identifier_ < rhs.identifier_;
681 }
682 };
683
684 typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
685}
686
029bc65b 687void CYScope::Scope(CYContext &context, CYStatement *&statements) {
2c81c6df
JF
688 if (parent_ == NULL)
689 return;
690
029bc65b 691 CYDeclarations *last(NULL), *curr(NULL);
14ec9e00 692
10711823 693 IdentifierOffsets offsets;
6a981250 694
10711823 695 for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
a846a8cd 696 if (i->second != CYIdentifierMagic)
e013809d 697 offsets.insert(IdentifierOffset(i->first, i->second));
10711823
JF
698
699 size_t offset(0);
700
701 for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
702 if (i->flags_ == CYIdentifierVariable) {
703 CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
029bc65b
JF
704 if (last == NULL)
705 last = next;
706 if (curr != NULL)
707 curr->SetNext(next);
708 curr = next;
709 }
029bc65b 710
10711823
JF
711 if (offset < i->offset_)
712 offset = i->offset_;
a846a8cd
JF
713 if (context.rename_.size() <= offset)
714 context.rename_.resize(offset + 1);
10711823 715
a846a8cd 716 CYIdentifierUsage &rename(context.rename_[offset++]);
e013809d
JF
717 i->identifier_->SetNext(rename.identifier_);
718 rename.identifier_ = i->identifier_;
719 rename.usage_ += i->identifier_->usage_ + 1;
6a981250
JF
720 }
721
029bc65b
JF
722 if (last != NULL) {
723 CYVar *var($ CYVar(last));
724 var->SetNext(statements);
725 statements = var;
726 }
727
6a981250
JF
728 for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
729 if (internal_.find(*i) == internal_.end()) {
de9fc71b 730 //std::cout << *i << '=' << offset << std::endl;
6a981250
JF
731 if ((*i)->offset_ < offset)
732 (*i)->offset_ = offset;
733 parent_->Merge(context, *i);
734 }
029bc65b
JF
735}
736
737CYStatement *CYStatement::Collapse(CYContext &context) {
738 return this;
3b52fd1a
JF
739}
740
4644480a
JF
741CYString *CYString::Concat(CYContext &context, CYString *rhs) const {
742 size_t size(size_ + rhs->size_);
2eb8215d 743 char *value($ char[size + 1]);
4644480a
JF
744 memcpy(value, value_, size_);
745 memcpy(value + size_, rhs->value_, rhs->size_);
746 value[size] = '\0';
a14eb702 747 return $S(value, size);
4644480a
JF
748}
749
750CYNumber *CYString::Number(CYContext &context) {
751 // XXX: there is a precise algorithm for this
752 return NULL;
753}
754
755CYString *CYString::String(CYContext &context) {
756 return this;
757}
758
3b52fd1a
JF
759CYStatement *CYSwitch::Replace(CYContext &context) {
760 context.Replace(value_);
761 clauses_->Replace(context);
029bc65b 762 return this;
3b52fd1a
JF
763}
764
765CYExpression *CYThis::Replace(CYContext &context) {
029bc65b 766 return this;
3b52fd1a
JF
767}
768
37954781
JF
769namespace cy {
770namespace Syntax {
771
772CYStatement *Throw::Replace(CYContext &context) {
3b52fd1a 773 context.Replace(value_);
029bc65b 774 return this;
3b52fd1a
JF
775}
776
37954781
JF
777} }
778
3b52fd1a 779CYExpression *CYTrivial::Replace(CYContext &context) {
029bc65b 780 return this;
3b52fd1a
JF
781}
782
4644480a
JF
783CYNumber *CYTrue::Number(CYContext &context) {
784 return $D(1);
785}
786
787CYString *CYTrue::String(CYContext &context) {
788 return $S("true");
789}
790
37954781
JF
791namespace cy {
792namespace Syntax {
793
794CYStatement *Try::Replace(CYContext &context) {
3b52fd1a
JF
795 code_.Replace(context);
796 catch_->Replace(context);
797 finally_->Replace(context);
029bc65b 798 return this;
3b52fd1a
JF
799}
800
37954781
JF
801} }
802
3b52fd1a 803CYStatement *CYVar::Replace(CYContext &context) {
029bc65b 804 return $E(declarations_->Replace(context));
3b52fd1a
JF
805}
806
807CYExpression *CYVariable::Replace(CYContext &context) {
577bfbfa 808 context.Replace(name_);
029bc65b 809 return this;
3b52fd1a
JF
810}
811
812CYStatement *CYWhile::Replace(CYContext &context) {
813 context.Replace(test_);
814 context.Replace(code_);
029bc65b 815 return this;
3b52fd1a
JF
816}
817
818CYStatement *CYWith::Replace(CYContext &context) {
819 context.Replace(scope_);
820 context.Replace(code_);
029bc65b 821 return this;
3b52fd1a
JF
822}
823
824CYExpression *CYWord::ClassName(CYContext &context, bool object) {
825 CYString *name($S(this));
826 if (object)
827 return $C1($V("objc_getClass"), name);
828 else
829 return name;
830}