+ return this;
+}
+
+void CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierFlags flags) {
+ internal_.insert(CYIdentifierAddressFlagsMap::value_type(identifier, flags));
+}
+
+CYIdentifier *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
+ std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
+ return *insert.first;
+}
+
+void CYScope::Merge(CYContext &context, CYIdentifier *identifier) {
+ std::pair<CYIdentifierValueSet::iterator, bool> insert(identifiers_.insert(identifier));
+ if (!insert.second) {
+ if ((*insert.first)->offset_ < identifier->offset_)
+ (*insert.first)->offset_ = identifier->offset_;
+ identifier->replace_ = *insert.first;
+ (*insert.first)->usage_ += identifier->usage_ + 1;
+ }
+}
+
+namespace {
+ struct IdentifierOffset {
+ size_t offset_;
+ CYIdentifierFlags flags_;
+ size_t usage_;
+ CYIdentifier *identifier_;
+
+ IdentifierOffset(CYIdentifier *identifier, CYIdentifierFlags flags) :
+ offset_(identifier->offset_),
+ flags_(flags),
+ usage_(identifier->usage_),
+ identifier_(identifier)
+ {
+ }
+ };
+
+ struct IdentifierOffsetLess :
+ std::binary_function<const IdentifierOffset &, const IdentifierOffset &, bool>
+ {
+ _finline bool operator ()(const IdentifierOffset &lhs, const IdentifierOffset &rhs) const {
+ if (lhs.offset_ != rhs.offset_)
+ return lhs.offset_ < rhs.offset_;
+ if (lhs.flags_ != rhs.flags_)
+ return lhs.flags_ < rhs.flags_;
+ /*if (lhs.usage_ != rhs.usage_)
+ return lhs.usage_ < rhs.usage_;*/
+ return lhs.identifier_ < rhs.identifier_;
+ }
+ };
+
+ typedef std::set<IdentifierOffset, IdentifierOffsetLess> IdentifierOffsets;
+}
+
+void CYScope::Scope(CYContext &context, CYStatement *&statements) {
+ if (parent_ == NULL)
+ return;
+
+ CYDeclarations *last(NULL), *curr(NULL);
+
+ IdentifierOffsets offsets;
+
+ for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i)
+ if (i->second != CYIdentifierMagic)
+ offsets.insert(IdentifierOffset(i->first, i->second));
+
+ size_t offset(0);
+
+ for (IdentifierOffsets::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
+ if (i->flags_ == CYIdentifierVariable) {
+ CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->identifier_)));
+ if (last == NULL)
+ last = next;
+ if (curr != NULL)
+ curr->SetNext(next);
+ curr = next;
+ }
+
+ if (offset < i->offset_)
+ offset = i->offset_;
+ if (context.rename_.size() <= offset)
+ context.rename_.resize(offset + 1);
+
+ CYIdentifierUsage &rename(context.rename_[offset++]);
+ i->identifier_->SetNext(rename.identifier_);
+ rename.identifier_ = i->identifier_;
+ rename.usage_ += i->identifier_->usage_ + 1;
+ }
+
+ if (last != NULL) {
+ CYVar *var($ CYVar(last));
+ var->SetNext(statements);
+ statements = var;
+ }
+
+ for (CYIdentifierValueSet::const_iterator i(identifiers_.begin()); i != identifiers_.end(); ++i)
+ if (internal_.find(*i) == internal_.end()) {
+ //std::cout << *i << '=' << offset << std::endl;
+ if ((*i)->offset_ < offset)
+ (*i)->offset_ = offset;
+ parent_->Merge(context, *i);
+ }
+}
+
+CYStatement *CYStatement::Collapse(CYContext &context) {
+ return this;