Esempio n. 1
0
        public StructureType MergeStructureFields(StructureType str)
        {
            if (!HasCoincidentFields(str))
            {
                return(str);
            }
            StructureType strNew = new StructureType(str.Name, str.Size);

            strNew.IsSegment = str.IsSegment;
            UnionType ut     = new UnionType(null, null);
            int       offset = 0;
            string?   name   = null;

            foreach (StructureField f in str.Fields)
            {
                //$REVIEW: what if multiple fields have differing names?
                if (ut.Alternatives.Count == 0)
                {
                    offset = f.Offset;
                    name   = f.IsNameSet ? f.Name : null;
                    ut.Alternatives.Add(f.DataType);
                }
                else
                {
                    if (f.Offset == offset)
                    {
                        if (f.DataType is UnionType uf)
                        {
                            ut = unifier.UnifyUnions(ut, uf);
                        }
                        else
                        {
                            unifier.UnifyIntoUnion(ut, f.DataType);
                        }
                    }
                    else
                    {
                        strNew.Fields.Add(offset, ut.Simplify(), name);
                        offset = f.Offset;
                        name   = f.IsNameSet ? f.Name : null;
                        ut     = new UnionType(null, null);
                        ut.Alternatives.Add(f.DataType);
                    }
                }
            }
            if (ut.Alternatives.Count > 0)
            {
                strNew.Fields.Add(offset, ut.Simplify(), name);
            }

            var sfm = new StructureFieldMerger(strNew);

            strNew = sfm.Merge();
            return(strNew);
        }