Inheritance: MetaMetadataField
        protected override void CustomizeFieldDescriptor(SimplTypesScope metadataTScope, MetadataFieldDescriptorProxy fdProxy)
        {
            base.CustomizeFieldDescriptor(metadataTScope, fdProxy);

            MetaMetadata thisMmd = TypeMmd;

            if (thisMmd == null)
            {
                return;
            }

            MetaMetadataNestedField inheritedField = (MetaMetadataNestedField)SuperField;

            if (inheritedField != null)
            {
                MetaMetadata superMmd = inheritedField.TypeMmd;
                if (thisMmd == superMmd || thisMmd.IsDerivedFrom(superMmd))
                {
                    MetadataClassDescriptor elementMetadataCD = thisMmd.GetMetadataClassDescriptor(metadataTScope);
                    if (elementMetadataCD != null)
                    {
                        fdProxy.SetElementClassDescriptor(elementMetadataCD);
                    }
                    else
                    {
                        Debug.WriteLineIf(BigSemanticsSettings.DebugLevel > 5, "can't bind FieldDescriptor because metadata class does not exist for: " + thisMmd.ToString());
                    }
                }
                else
                {
                    throw new MetaMetadataException("incompatible types: " + inheritedField + " => " + this);
                }
            }
        }
        private void InheritFieldFinished(MetaMetadataNestedField sender, EventArgs e)
        {
            MetaMetadataCompositeField inheritedField = (MetaMetadataCompositeField)_waitingToInheritFrom.Pop();

            //InheritFromCompositeField(inheritedField, Repository);

            if (_waitingToInheritFrom.Count == 0)
            {
                FinishInheritance();
            }
        }
        private void InheritMetaMetadataFinished(MetaMetadataNestedField sender, EventArgs e)
        {
            MetaMetadata       inheritedMmd       = (MetaMetadata)_waitingToInheritFrom.Pop();
            InheritanceHandler inheritanceHandler = _waitingToInheritFromInheritanceHandler.Pop();

            InheritFromTopLevelMetaMetadata(inheritedMmd, Repository, inheritanceHandler);

            InheritFromSuperField(Repository, inheritanceHandler);

            if (_waitingToInheritFrom.Count == 0)
            {
                FinishInheritance();
            }
        }
        public void AddInheritanceFinishHandler(MetaMetadataNestedField inheritingField, InheritFinishedEventHandler eventHandler, InheritanceHandler inheritanceHandler)
        {
            if (_waitingToInheritFrom == null)
            {
                _waitingToInheritFrom = new Stack <MetaMetadataNestedField>();
            }

            _waitingToInheritFrom.Push(inheritingField);

            //adding the InheritanceHandler for this inheritingField
            if (_waitingToInheritFromInheritanceHandler == null)
            {
                _waitingToInheritFromInheritanceHandler = new Stack <InheritanceHandler>();
            }

            _waitingToInheritFromInheritanceHandler.Push(inheritanceHandler);

            inheritingField.InheritFinished += eventHandler;
        }
        private void InheritMetaMetadataFinished(MetaMetadataNestedField sender, EventArgs e)
        {
            MetaMetadata inheritedMmd = (MetaMetadata) _waitingToInheritFrom.Pop();
            InheritanceHandler inheritanceHandler = _waitingToInheritFromInheritanceHandler.Pop();

            InheritFromTopLevelMetaMetadata(inheritedMmd, Repository, inheritanceHandler);

            InheritFromSuperField(Repository, inheritanceHandler);

            if (_waitingToInheritFrom.Count == 0)
                FinishInheritance();
        }
        private void InheritFieldFinished(MetaMetadataNestedField sender, EventArgs e)
        {
            MetaMetadataCompositeField inheritedField = (MetaMetadataCompositeField) _waitingToInheritFrom.Pop();
            //InheritFromCompositeField(inheritedField, Repository);

            if (_waitingToInheritFrom.Count == 0)
                FinishInheritance();
        }
        public void AddInheritanceFinishHandler(MetaMetadataNestedField inheritingField, InheritFinishedEventHandler eventHandler, InheritanceHandler inheritanceHandler)
        {
            if (_waitingToInheritFrom == null)
                _waitingToInheritFrom = new Stack<MetaMetadataNestedField>();

            _waitingToInheritFrom.Push(inheritingField);

            //adding the InheritanceHandler for this inheritingField
            if (_waitingToInheritFromInheritanceHandler == null)
                _waitingToInheritFromInheritanceHandler = new Stack<InheritanceHandler>();

            _waitingToInheritFromInheritanceHandler.Push(inheritanceHandler);

            inheritingField.InheritFinished += eventHandler;
        }
        protected virtual void InheritFrom(MetaMetadataRepository repository,
                                           MetaMetadataCompositeField inheritedStructure,
                                           InheritanceHandler inheritanceHandler)
        {
            // init nested fields inside this
            var subfields = Kids.Values;

            foreach (MetaMetadataField f in subfields)
            {
                if (f is MetaMetadataNestedField)
                {
                    f.Repository = (repository);
                    MetaMetadataNestedField nested = (MetaMetadataNestedField)f;
                    if (nested.PackageName == null)
                    {
                        nested.PackageName = PackageName;
                    }
                    nested.Scope = Scope;
                }
            }

            // inherit fields with attributes from inheritedStructure
            // if inheritedStructure == null, this must be the root meta-metadata
            if (inheritedStructure != null)
            {
                var inheritedStructSubfields = inheritedStructure.Kids.Values;
                foreach (MetaMetadataField field in inheritedStructSubfields)
                {
                    if (field is MetaMetadataNestedField)
                    {
                        ((MetaMetadataNestedField)field).InheritMetaMetadata(inheritanceHandler);
                    }
                    string            fieldName = field.Name;
                    MetaMetadataField fieldLocal;
                    kids.TryGetValue(fieldName, out fieldLocal);

                    if (fieldLocal == null && inheritanceHandler.IsUsingGenerics(field))
                    {
                        // if the super field is using generics, we will need to re-evaluate generic type vars
                        fieldLocal = (MetaMetadataField)Activator.CreateInstance(field.GetType());

                        //Prepare Child Field For Inheritance
                        fieldLocal.Repository = (repository);
                        if (fieldLocal is MetaMetadataNestedField)
                        {
                            MetaMetadataNestedField nested = (MetaMetadataNestedField)fieldLocal;
                            if (nested.PackageName == null)
                            {
                                nested.PackageName = PackageName;
                            }
                            nested.Scope = Scope;
                        }
                    }
                    if (fieldLocal != null)
                    {
                        Debug.WriteLine("inheriting field: " + fieldLocal + " <= " + field);
                        if (field.GetType() != fieldLocal.GetType())
                        {
                            Debug.WriteLine("local field " + fieldLocal + " hides field " + fieldLocal +
                                            " with the same name in super mmd type!");
                        }
                        // debug("inheriting field " + fieldLocal + " from " + field);
                        if (field != fieldLocal)
                        {
                            fieldLocal.SuperField = field;
                        }
                        fieldLocal.DeclaringMmd = field.DeclaringMmd;
                        fieldLocal.InheritAttributes(field);
                        if (fieldLocal is MetaMetadataNestedField)
                        {
                            ((MetaMetadataNestedField)fieldLocal).PackageName =
                                ((MetaMetadataNestedField)field).PackageName;
                        }
                    }
                }
            }

            // recursively call inheritMetaMetadata() on nested fields
            foreach (MetaMetadataField f in subfields)
            {
                // a new field is defined inside this mmd
                if (f.DeclaringMmd == this && f.SuperField == null)
                {
                    SetNewMetadataClass(true);
                }

                // recursively call this method on nested fields
                f.Repository = repository;
                if (f is MetaMetadataNestedField)
                {
                    MetaMetadataNestedField f1 = (MetaMetadataNestedField)f;
                    f1.InheritMetaMetadata(inheritanceHandler);
                    if (f1.IsNewMetadataClass())
                    {
                        SetNewMetadataClass(true);
                    }

                    MetaMetadataNestedField f0 = (MetaMetadataNestedField)f.SuperField;
                    if (f0 != null && f0.GetTypeName() != f1.GetTypeName())
                    {
                        // inherited field w changing base type (polymorphic case)
                        f1.InheritMetaMetadata(inheritanceHandler);
                        MetaMetadata mmd0 = f0.TypeMmd;
                        MetaMetadata mmd1 = f1.TypeMmd;
                        if (mmd1.IsDerivedFrom(mmd0))
                        {
                            SetNewMetadataClass(true);
                        }
                        else
                        {
                            throw new MetaMetadataException("incompatible types: " + mmd0 + " => " + mmd1);
                        }
                    }
                }
            }

            // clone fields only declared in inheritedStructure.
            // must clone them after recursively calling inheritMetaMetadata(), so that their nested
            // structures (which may be inherited too) can be cloned.
            if (inheritedStructure != null)
            {
                var inheritedStructSubfields = inheritedStructure.Kids.Values;
                foreach (MetaMetadataField field in inheritedStructSubfields)
                {
                    string            fieldName = field.Name;
                    MetaMetadataField fieldLocal;
                    kids.TryGetValue(fieldName, out fieldLocal);

                    if (fieldLocal == null)
                    {
                        //					MetaMetadataField clonedField = (MetaMetadataField) field.clone();
                        //					clonedField.setParent(this);
                        //					this.getChildMetaMetadata().put(fieldName, clonedField);
                        kids.Put(fieldName, field);
                    }
                }
            }
        }
        public MetaMetadata ResolveMmdName(String mmdName, NameType[] nameType)
        {
            if (mmdName == null)
            {
                return(null);
            }
            Object            resultObj = null;
            MetaMetadata      result    = null;
            MetaMetadataField field     = mmStack.Peek();

            if (nameType != null && nameType.Length > 0)
            {
                nameType[0] = NameType.NONE;
            }

            // step 1: try to resolve the name as a concrete meta-metadata name, using the mmdScope.
            if (field is MetaMetadataNestedField)
            {
                MetaMetadataNestedField nested = (MetaMetadataNestedField)field;
                nested.Scope.TryGetValue(mmdName, out resultObj);
                result = (MetaMetadata)resultObj;
                if (result != null)
                {
                    if (nameType != null && nameType.Length > 0)
                    {
                        nameType[0] = NameType.MMD;
                    }
                }
            }

            // step 2: if step 1 failed, try to use it as a generic type var name
            if (result == null && mmdName.ToUpper().Equals(mmdName))
            {
                List <Object> gtvScopes = scopeStack.Peek().GetAll(GENERIC_TYPE_VAR_SCOPE);
                foreach (MmdGenericTypeVarScope gtvScope_object in gtvScopes)
                {
                    if (!(gtvScope_object is MmdGenericTypeVarScope))
                    {
                        throw new MetaMetadataException("Object is not instance of MmdGenericTypeVarScope");
                    }
                    MmdGenericTypeVarScope gtvScope = gtvScope_object;

                    MmdGenericTypeVar gtv = gtvScope.Get(mmdName);
                    if (gtv != null)
                    {
                        if (gtv.Arg != null)
                        {
                            result = ResolveMmdName(gtv.Arg);
                        }
                        else if (gtv.ExtendsAttribute != null)
                        {
                            result = ResolveMmdName(gtv.ExtendsAttribute);
                        }
                        // TODO superAttribute?
                    }
                }
                if (result != null)
                {
                    if (nameType != null && nameType.Length > 0)
                    {
                        nameType[0] = NameType.GENERIC;
                    }
                }
            }

            return(result);
        }
        /**
         * bind metadata field descriptors to sub-fields of this nested field, with field names as keys,
         * but without mixins field.
         * <p>
         * sub-fields that lack corresponding field descriptors will be removed from this nested field.
         * <p>
         * note that this field no longer uses a boolean flag to prevent multiple invocation. this should
         * have been done by the bindClassDescriptor() method.
         *
         * @param metadataTScope
         *          the translation scope of (generated) metadata classes.
         * @param metadataClassDescriptor
         *          the metadata class descriptor where field descriptors can be found.
         */

        protected void BindMetadataFieldDescriptors(SimplTypesScope metadataTScope,
                                                    MetadataClassDescriptor metadataClassDescriptorToBind)
        {
            bool needCloneKids = false;

            // check if the class's base class is genereic typed, and make sure itself is not generic.
            MetadataClassDescriptor metadataCd = MetadataClassDescriptor;
            MetaMetadata            baseMmd    = TypeMmd;

            while (baseMmd != null && metadataCd != null)
            {
                if (metadataCd.GetGenericTypeVars().Count == 0 && baseMmd.GenericTypeVars != null)
                {
                    needCloneKids = true;
                    break;
                }
                metadataCd = baseMmd.MetadataClassDescriptor;
                baseMmd    = baseMmd.TypeMmd;
            }

            if (needCloneKids)
            {
                DictionaryList <string, MetaMetadataField> clonedKids = new DictionaryList <string, MetaMetadataField>();
                foreach (KeyValuePair <string, MetaMetadataField> entry in Kids)
                {
                    string            key   = entry.Key;
                    MetaMetadataField field = entry.Value;

                    // look up to see if the field is declared in a generic typed class. If not, it does not need to clone it.
                    MetaMetadata declaringMmd = field.DeclaringMmd;
                    if (declaringMmd != null && declaringMmd.GenericTypeVars != null && declaringMmd.IsGenericMetadata)
                    {
                        // clone the field
                        field = field.Clone();
                        // remove the inherited field descriptor
                        field.MetadataFieldDescriptor = null;
                    }
                    clonedKids.Put(key, field);
                }
                Kids = clonedKids;
            }


            // copy the kids collection first to prevent modification to the collection during iteration (which may invalidate the iterator).
            List <MetaMetadataField> fields = new List <MetaMetadataField>(Kids.Values);

            foreach (MetaMetadataField thatChild in fields)
            {
                // look up by field name and bind
                MetadataFieldDescriptor metadataFd = thatChild.BindMetadataFieldDescriptor(metadataTScope, metadataClassDescriptorToBind);
                if (metadataFd == null)
                {
                    Debug.WriteLineIf(BigSemanticsSettings.DebugLevel > 5, "Cannot bind metadata field descriptor for " + thatChild);
                    Kids.Remove(thatChild.Name);
                    continue;
                }

                // set defininig mmdfield

                // process hide and shadows
                var isImage = thatChild is MetaMetadataCompositeField && "image".Equals(((MetaMetadataCompositeField)thatChild).Type);
                HashSet <String> nonDisplayedFieldNames = NonDisplayedFieldNames;
                if (thatChild.Hide && !isImage)
                {
                    nonDisplayedFieldNames.Add(thatChild.Name);
                }
                if (thatChild.Shadows != null)
                {
                    nonDisplayedFieldNames.Add(thatChild.Shadows);
                }

                // recursively process sub-fields
                Int32 fieldType = metadataFd.FdType;
                if (fieldType == FieldTypes.CompositeElement || fieldType == FieldTypes.CollectionElement)
                {
                    // bind class descriptor for nested sub-fields
                    MetaMetadataNestedField nested = (MetaMetadataNestedField)thatChild;
                    MetadataFieldDescriptor fd     = nested.MetadataFieldDescriptor;
                    if (fd.IsPolymorphic)
                    {
                        Debug.WriteLineIf(BigSemanticsSettings.DebugLevel > 5, "Polymorphic field: " + nested + ", not binding an element class descriptor.");
                    }
                    else
                    {
                        MetadataClassDescriptor elementClassDescriptor =
                            ((MetaMetadataNestedField)thatChild).BindMetadataClassDescriptor(metadataTScope);
                        if (elementClassDescriptor != null)
                        {
                            MetaMetadata mmdForThatChild = nested.TypeMmd;
                            if (mmdForThatChild != null && mmdForThatChild.MetadataClassDescriptor == null)
                            {
                                //							mmdForThatChild.setMetadataClassDescriptor(elementClassDescriptor);
                                mmdForThatChild.BindMetadataClassDescriptor(metadataTScope);
                            }
                        }
                        else
                        {
                            Debug.WriteLineIf(BigSemanticsSettings.DebugLevel > 5, "Cannot determine elementClassDescriptor for " + thatChild);
                            Kids.Remove(thatChild.Name);
                        }
                    }
                }

                if (this is MetaMetadata)
                {
                    MetaMetadata mmd       = (MetaMetadata)this;
                    String       naturalId = thatChild.AsNaturalId;
                    if (naturalId != null)
                    {
                        mmd.NaturalIds.Put(naturalId, thatChild);
                    }
                }
            }
        }