public void CopyTo(MemoryRegion dest)
 {
     CopyHelper.CopyTo(this, dest);
 }
        private MemberPrecidence LoadScatterElements(object owner, object conditional, List<MemoryMap> maps, ArrayList conditionalStack)
        {
            MemberPrecidence ret = MemberPrecidence.Unknown;

            List<If> ifs = GetCollectionFromObject(conditional, typeof(List<If>)) as List<If>;
            if (ifs != null && ifs.Count > 0)
            {
                LoadScatterConditional(owner, ifs, maps, conditionalStack);
            }
            List<IfDefined> ifdefs = GetCollectionFromObject(conditional, typeof(List<IfDefined>)) as List<IfDefined>;
            if (ifdefs != null)
            {
                LoadScatterConditional(owner, ifdefs, maps, conditionalStack);
            }
            List<IfNotDefined> ifndefs = GetCollectionFromObject(conditional, typeof(List<IfNotDefined>)) as List<IfNotDefined>;
            if (ifndefs != null)
            {
                LoadScatterConditional(owner, ifndefs, maps, conditionalStack);
            }

            List<LoadRegion> lrCollection = GetCollectionFromObject(conditional, typeof(List<LoadRegion>)) as List<LoadRegion>;
            if (lrCollection != null && lrCollection.Count > 0)
            {
                List<MemoryRegion> regionSet = GetCollectionFromObject(owner, typeof(List<MemoryRegion>)) as List<MemoryRegion>;

                ret = MemberPrecidence.LoadRegion;

                foreach (LoadRegion lr in lrCollection)
                {
                    MemoryRegion region = new MemoryRegion();
                    region.Name = lr.Name;
                    region.Address = lr.Base;
                    region.Options = lr.Options;
                    region.Size = lr.Size;
//                    region.Order = regionSet.Count;
                    region.Conditional = GetConditionalValue(conditional);

                    regionSet.Add(region);

                    ret = LoadScatterElements(region, lr, maps, conditionalStack);
                }
            }

            List<ExecRegion> erCollection = GetCollectionFromObject(conditional, typeof(List<ExecRegion>)) as List<ExecRegion>;
            if (erCollection != null && erCollection.Count > 0)
            {
                List<MemorySection> sectionSet = GetCollectionFromObject(owner, typeof(List<MemorySection>)) as List<MemorySection>;

                ret = MemberPrecidence.ExecRegion;

                foreach (ExecRegion er in erCollection)
                {
                    MemorySection section = new MemorySection();
                    section.Name        = er.Name;
                    section.Address     = er.Base;
                    section.Options     = er.Options;
                    section.Size        = er.Size;
                    section.Order       = (int)m_execRegionOrderMap[(owner as MemoryRegion).Name + ":" + er.Name];
                    section.Conditional = GetConditionalValue(conditional);

                    sectionSet.Add(section);

                    ret = LoadScatterElements(section, er, maps, conditionalStack);
                }
            }

            List<FileMapping> fileCollection = GetCollectionFromObject(conditional, typeof(List<FileMapping>)) as List<FileMapping>;
            if (fileCollection != null && fileCollection.Count > 0)
            {
                List<MemorySymbol> symSet = GetCollectionFromObject(owner, typeof(List<MemorySymbol>)) as List<MemorySymbol>;

                ret = MemberPrecidence.FileMapping;


                foreach (FileMapping fm in fileCollection)
                {
                    MemorySymbol sym = new MemorySymbol();
                    sym.Name       = fm.Name;
                    sym.Options    = fm.Options;
                    sym.Conditional = GetConditionalValue(conditional);

                    symSet.Add(sym);
                }
            }

            List<Set> setCollection = GetCollectionFromObject(conditional, typeof(List<Set>)) as List<Set>;
            if (setCollection != null && setCollection.Count > 0)
            {
                EnvVars envSets = GetObjectFromProperty(owner, typeof(EnvVars)) as EnvVars;

                if (ret < MemberPrecidence.SetMember)
                {
                    ret = MemberPrecidence.SetMember;
                }

                EnvVars envSet = null;

                // conditional belongs to the map if we have a load region in this conditional
                if(conditionalStack.Count == 0)
                {
                    envSet = FindMatchingSet(envSets, "Global");

                    if (envSet == null)
                    {
                        envSet = new EnvVars();
                        envSet.Name = "Global";
                        envSets.EnvVarsCollection.Add(envSet);
                    }
                }
                else
                {
                    for (int i = 0; i < conditionalStack.Count; i++)
                    {
                        object cond = conditionalStack[i];

                        if (i == (conditionalStack.Count-1) && owner is MemoryMap && ret >= MemberPrecidence.LoadRegion)
                        {
                            ((MemoryMap)owner).Name        = GetConditionalName(conditional);
                            ((MemoryMap)owner).Conditional = GetConditionalValue(conditional);
                        }
                        else
                        {
                            string name = GetConditionalName(cond);
                            string value = GetConditionalValue(cond);

                            name += "_" + value.Replace(" ", "_").Replace("\"", "");

                            envSet = FindMatchingSet(envSets, name);

                            if (envSet == null)
                            {
                                envSet = new EnvVars();
                                envSet.Name = name;
                                envSet.Conditional = value;

                                envSets.EnvVarsCollection.Add(envSet);
                            }

                            //envSets = envSet.EnvVarsCollection;
                        }
                    }
                }


                if ((int)ret < (int)MemberPrecidence.SetMember)
                {
                    ret = MemberPrecidence.SetMember;
                }

                foreach (Set set in GetCollectionFromObject(conditional, typeof(List<Set>)))
                {
                    EnvVar var = new EnvVar();
                    var.Name = set.Name;
                    var.Value = set.Value;

                    envSet.EnvVarCollection.Add(var);
                }
            }

            return ret;
        }
        private void addMemoryRegionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tn = treeViewInventory.SelectedNode;
            TreeNodeData tnd = tn.Tag as TreeNodeData;

            if (tnd != null)
            {
                MemoryRegion mr = new MemoryRegion();
                mr.Name = "<New Memory Region>";
//                mr.Order = tn.Nodes.Count;
                (tnd.Data as List<MemoryRegion>).Add(mr);
                TreeNode child = AddTreeElement(tn, mr.Name, mr, true, tnd.Data, tnd.FilePath);
                child.EnsureVisible();
                treeViewInventory.SelectedNode = child;
                child.BeginEdit();
            }
        }