+CYIdentifierFlags *CYScope::Lookup(CYContext &context, CYIdentifier *identifier) {
+ return Lookup(context, identifier->Word());
+}
+
+CYIdentifierFlags *CYScope::Declare(CYContext &context, CYIdentifier *identifier, CYIdentifierKind kind) {
+ _assert(identifier->next_ == NULL || identifier->next_ == identifier);
+
+ CYIdentifierFlags *existing(Lookup(context, identifier));
+ if (existing == NULL)
+ internal_ = $ CYIdentifierFlags(identifier, kind, internal_);
+ ++internal_->count_;
+ if (existing == NULL)
+ return internal_;
+
+ if (kind == CYIdentifierGlobal);
+ else if (existing->kind_ == CYIdentifierGlobal || existing->kind_ == CYIdentifierMagic)
+ existing->kind_ = kind;
+ else if (existing->kind_ == CYIdentifierLexical || kind == CYIdentifierLexical)
+ _assert(false); // XXX: throw new SyntaxError()
+
+ 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<CYBindings> bindings;
+
+ CYForEach (i, internal_)
+ if (i->kind_ == CYIdentifierVariable)
+ bindings
+ ->* $ CYBindings($ CYBinding(i->identifier_));
+
+ if (bindings) {
+ CYVar *var($ CYVar(bindings));
+ 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;