+ return existing;
+}
+
+void CYScope::Merge(CYContext &context, const CYIdentifierFlags *flags) {
+ _assert(flags->identifier_->next_ == flags->identifier_);
+ CYIdentifierFlags *existing(Declare(context, flags->identifier_, flags->kind_));
+ flags->identifier_->next_ = existing->identifier_;
+
+ existing->count_ += flags->count_;
+ if (existing->offset_ < flags->offset_)
+ existing->offset_ = flags->offset_;
+}
+
+void CYScope::Close(CYContext &context, CYStatement *&statements) {
+ Close(context);
+
+ CYList<CYDeclarations> declarations;
+
+ CYForEach (i, internal_)
+ if (i->kind_ == CYIdentifierVariable)
+ declarations
+ ->* $ CYDeclarations($ CYDeclaration(i->identifier_));
+
+ if (declarations) {
+ CYVar *var($ CYVar(declarations));
+ var->SetNext(statements);
+ statements = var;
+ }
+}
+
+void CYScope::Close(CYContext &context) {
+ context.scope_ = parent_;
+
+ CYForEach (i, internal_) {
+ _assert(i->identifier_->next_ == i->identifier_);
+ switch (i->kind_) {
+ case CYIdentifierArgument: {
+ _assert(!transparent_);
+ } break;
+
+ case CYIdentifierLexical: {
+ if (!damaged_) {
+ CYIdentifier *replace(context.Unique());
+ replace->next_ = replace;
+ i->identifier_->next_ = replace;
+ i->identifier_ = replace;
+ }
+
+ if (!transparent_)
+ i->kind_ = CYIdentifierVariable;
+ else
+ parent_->Declare(context, i->identifier_, CYIdentifierVariable);
+ } break;
+
+ case CYIdentifierVariable: {
+ if (transparent_) {
+ parent_->Declare(context, i->identifier_, i->kind_);
+ i->kind_ = CYIdentifierGlobal;
+ }
+ } break;
+ default:; } }
+
+ if (damaged_)
+ return;
+
+ typedef std::multimap<unsigned, CYIdentifier *> CYIdentifierOffsetMap;
+ CYIdentifierOffsetMap offsets;
+
+ CYForEach (i, internal_) {
+ _assert(i->identifier_->next_ == i->identifier_);
+ switch (i->kind_) {
+ case CYIdentifierArgument:
+ case CYIdentifierVariable:
+ offsets.insert(CYIdentifierOffsetMap::value_type(i->offset_, i->identifier_));
+ break;
+ default:; } }
+
+ unsigned offset(0);
+
+ for (CYIdentifierOffsetMap::const_iterator i(offsets.begin()); i != offsets.end(); ++i) {
+ if (offset < i->first)
+ offset = i->first;
+ CYIdentifier *identifier(i->second);
+
+ if (offset >= context.replace_.size())
+ context.replace_.resize(offset + 1, NULL);
+ CYIdentifier *&replace(context.replace_[offset++]);
+
+ if (replace == NULL)
+ replace = identifier;
+ else {
+ _assert(replace->next_ == replace);
+ identifier->next_ = replace;
+ }
+ }
+
+ if (parent_ == NULL)
+ return;
+
+ CYForEach (i, internal_) {
+ switch (i->kind_) {
+ case CYIdentifierGlobal: {
+ if (i->offset_ < offset)
+ i->offset_ = offset;
+ parent_->Merge(context, i);
+ } break;
+ default:; } }
+}
+
+CYElementValue *CYSpan::Replace(CYContext &context) { $T(NULL)
+ return $ CYElementValue(expression_, $ CYElementValue(string_, next_->Replace(context)));
+}
+
+CYStatement *CYStatement::Return() {
+ return this;