+ 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<CYIdentifierAddressSet::iterator, bool> insert(identifiers_.insert(identifier));
+ if (!insert.second) {
+ if ((*insert.first)->offset_ < identifier->offset_)
+ (*insert.first)->offset_ = identifier->offset_;
+ identifier->replace_ = *insert.first;
+ }
+}
+
+void CYScope::Scope(CYContext &context, CYStatement *&statements) {
+ CYDeclarations *last(NULL), *curr(NULL);
+ CYProgram *program(context.program_);
+
+ typedef std::multimap<size_t, CYIdentifier *> IdentifierOffsetMap;
+ IdentifierOffsetMap offsetted;
+
+ // XXX: we don't want to do this in order, we want to sort it by probable occurrence
+ for (CYIdentifierAddressFlagsMap::const_iterator i(internal_.begin()); i != internal_.end(); ++i) {
+ if (program != NULL && i->second != CYIdentifierMagic)
+ offsetted.insert(IdentifierOffsetMap::value_type(i->first->offset_, i->first));
+ if (i->second == CYIdentifierVariable) {
+ CYDeclarations *next($ CYDeclarations($ CYDeclaration(i->first)));
+ if (last == NULL)
+ last = next;
+ if (curr != NULL)
+ curr->SetNext(next);
+ curr = next;
+ }
+ }
+
+ size_t offset(0);
+
+ for (IdentifierOffsetMap::const_iterator i(offsetted.begin()); i != offsetted.end(); ++i) {
+ if (offset < i->first)
+ offset = i->first;
+ if (program->rename_.size() <= offset)
+ program->rename_.resize(offset + 1);
+ CYIdentifier *&identifier(program->rename_[offset++]);
+ i->second->SetNext(identifier);
+ identifier = i->second;
+ }
+
+ 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()) {
+ if ((*i)->offset_ < offset)
+ (*i)->offset_ = offset;
+ parent_->Merge(context, *i);
+ }
+}
+
+CYStatement *CYStatement::Collapse(CYContext &context) {
+ return this;