X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/e013809d7a06e37dcbf21c340febecda46ba0caf..c30687a7155e8c96310eaddd8213f6d3a69cab0e:/Replace.cpp?ds=sidebyside diff --git a/Replace.cpp b/Replace.cpp index b54bfe3..c06c2a5 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -338,8 +338,9 @@ void CYFunction::Replace_(CYContext &context, bool outer) { if (outer) Inject(context); - parent_ = context.scope_; - context.scope_ = this; + CYScope scope; + scope.parent_ = context.scope_; + context.scope_ = &scope; if (!outer && name_ != NULL) Inject(context); @@ -347,8 +348,8 @@ void CYFunction::Replace_(CYContext &context, bool outer) { parameters_->Replace(context); code_.Replace(context); - context.scope_ = parent_; - Scope(context, code_.statements_); + context.scope_ = scope.parent_; + scope.Scope(context, code_.statements_); } CYExpression *CYFunctionExpression::Replace(CYContext &context) { @@ -368,11 +369,9 @@ CYStatement *CYFunctionStatement::Replace(CYContext &context) { } CYIdentifier *CYIdentifier::Replace(CYContext &context) { - if (replace_ == NULL) { - replace_ = context.scope_->Lookup(context, this); - ++replace_->usage_; - } else if (replace_ != this) + if (replace_ != NULL && replace_ != this) return replace_->Replace(context); + replace_ = context.scope_->Lookup(context, this); return replace_; } @@ -478,32 +477,27 @@ namespace { } void CYProgram::Replace(CYContext &context) { - parent_ = context.scope_; - CYProgram *program(context.program_); - - context.scope_ = this; - context.program_ = this; - + CYScope scope; + scope.parent_ = context.scope_; + context.scope_ = &scope; statements_ = statements_->ReplaceAll(context); - - context.scope_ = parent_; - context.program_ = program; - Scope(context, statements_); + context.scope_ = scope.parent_; + scope.Scope(context, statements_); size_t offset(0); CYCStringSet external; - for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i) + for (CYIdentifierValueSet::const_iterator i(scope.identifiers_.begin()); i != scope.identifiers_.end(); ++i) external.insert((*i)->Word()); IdentifierUsages usages; - if (offset < rename_.size()) - for (CYIdentifier *i(rename_[offset].identifier_); i != NULL; i = i->next_) - usages.insert(i); + if (offset < context.rename_.size()) + for (CYIdentifier *i(context.rename_[offset].identifier_); i != NULL; i = i->next_) + usages.insert(i); // XXX: totalling the probable occurrences and sorting by them would improve the result - for (CYIdentifierUsageVector::const_iterator i(rename_.begin()); i != rename_.end(); ++i, ++offset) { + for (CYIdentifierUsageVector::const_iterator i(context.rename_.begin()); i != context.rename_.end(); ++i, ++offset) { //std::cout << *i << ":" << (*i)->offset_ << std::endl; const char *name; @@ -591,7 +585,7 @@ namespace { if (lhs.flags_ != rhs.flags_) return lhs.flags_ < rhs.flags_; /*if (lhs.usage_ != rhs.usage_) - return lhs.usage_ > rhs.usage_;*/ + return lhs.usage_ < rhs.usage_;*/ return lhs.identifier_ < rhs.identifier_; } }; @@ -600,13 +594,15 @@ namespace { } void CYScope::Scope(CYContext &context, CYStatement *&statements) { + if (parent_ == NULL) + return; + CYDeclarations *last(NULL), *curr(NULL); - CYProgram *program(context.program_); IdentifierOffsets offsets; for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) - if (program != NULL && i->second != CYIdentifierMagic) + if (i->second != CYIdentifierMagic) offsets.insert(IdentifierOffset(i->first, i->second)); size_t offset(0); @@ -623,10 +619,10 @@ void CYScope::Scope(CYContext &context, CYStatement *&statements) { if (offset < i->offset_) offset = i->offset_; - if (program->rename_.size() <= offset) - program->rename_.resize(offset + 1); + if (context.rename_.size() <= offset) + context.rename_.resize(offset + 1); - CYIdentifierUsage &rename(program->rename_[offset++]); + CYIdentifierUsage &rename(context.rename_[offset++]); i->identifier_->SetNext(rename.identifier_); rename.identifier_ = i->identifier_; rename.usage_ += i->identifier_->usage_ + 1;