From ab7d9dd49200d7cca75475b0eb2d86101d466ad0 Mon Sep 17 00:00:00 2001 From: samuraiGH <87191377+samuraiGH@users.noreply.github.com> Date: Thu, 9 Oct 2025 18:40:11 +0300 Subject: [PATCH] replacing Hashtable with HashSet and Dictionary (#3343) --- CodeCompletion/DomConverter.cs | 4 +- CodeCompletion/DomSyntaxTreeVisitor.cs | 4 +- Compiler/Compiler.cs | 7 +- Compiler/PCU/PCUReader.cs | 34 +-- Compiler/PCU/PCUWriter.cs | 53 ++-- Localization/StringResourcesLanguage.cs | 19 +- NETGenerator/Helpers.cs | 29 +- NETGenerator/NETGenegratorTools.cs | 68 ++--- Optimizer/OptimizerTools.cs | 15 +- ParserTools/XmlDocs.cs | 40 ++- TreeConverter/NetWrappers/NetHelper.cs | 271 +++++++++--------- TreeConverter/SystemLib/StaticSystemLib.cs | 29 +- .../TreeConversion/syntax_tree_visitor.cs | 16 +- TreeConverter/TreeRealization/programs.cs | 12 +- 14 files changed, 295 insertions(+), 306 deletions(-) diff --git a/CodeCompletion/DomConverter.cs b/CodeCompletion/DomConverter.cs index 351f7c38e..37a1ec6e3 100644 --- a/CodeCompletion/DomConverter.cs +++ b/CodeCompletion/DomConverter.cs @@ -42,7 +42,7 @@ namespace CodeCompletion // TODO: Требуется адаптировать к многоязычности EVA public static SymInfo[] standard_units; - private Hashtable cur_used_assemblies; + private HashSet cur_used_assemblies; public CompilationUnit unit; private void InitModules() @@ -1230,7 +1230,7 @@ namespace CodeCompletion return false; } - private Hashtable tmp_cur_used_assemblies; + private HashSet tmp_cur_used_assemblies; //kazhdaja programma mozhet imet svoj spisok podkluchaemyh sborok, //poetomu nado sohranat i vosstanavlivat tekushij kesh sborok diff --git a/CodeCompletion/DomSyntaxTreeVisitor.cs b/CodeCompletion/DomSyntaxTreeVisitor.cs index 5ba79fdb2..2e095230a 100644 --- a/CodeCompletion/DomSyntaxTreeVisitor.cs +++ b/CodeCompletion/DomSyntaxTreeVisitor.cs @@ -40,7 +40,7 @@ namespace CodeCompletion private static Compiler compiler; public static bool use_semantic_for_intellisense; private Dictionary method_call_cache = new Dictionary(); - public Hashtable cur_used_assemblies; + public HashSet cur_used_assemblies; public DomSyntaxTreeVisitor(DomConverter converter) { @@ -69,7 +69,7 @@ namespace CodeCompletion //throw e; //System.Diagnostics.Debug.WriteLine(e.StackTrace); } - cur_used_assemblies = (Hashtable)PascalABCCompiler.NetHelper.NetHelper.cur_used_assemblies.Clone(); + cur_used_assemblies = new HashSet(PascalABCCompiler.NetHelper.NetHelper.cur_used_assemblies); if (use_semantic_for_intellisense && !parse_only_interface) try { diff --git a/Compiler/Compiler.cs b/Compiler/Compiler.cs index 50bb6a1cf..8951dea16 100644 --- a/Compiler/Compiler.cs +++ b/Compiler/Compiler.cs @@ -156,7 +156,6 @@ using System.Linq; using System.Reflection; using Languages.Facade; -using SyntaxVisitors; namespace PascalABCCompiler { @@ -680,7 +679,7 @@ namespace PascalABCCompiler /// private List UnitsToCompileDelayedList = new List(); - public Hashtable RecompileList = new Hashtable(StringComparer.OrdinalIgnoreCase); + public HashSet RecompileList = new HashSet(StringComparer.OrdinalIgnoreCase); private CompilationUnit currentCompilationUnit = null; @@ -4005,7 +4004,7 @@ namespace PascalABCCompiler { if (UnitTable[Path.ChangeExtension(used_unit_fname, null)] != null) return true; need = true; - RecompileList[pcu_name] = pcu_name; + RecompileList.Add(pcu_name); } } if (need) return true; @@ -4027,7 +4026,7 @@ namespace PascalABCCompiler pr.AddAlreadyCompiledUnit(pcu_name2); return true; } - if (RecompileList.ContainsKey(pcu_name2)) + if (RecompileList.Contains(pcu_name2)) { return true; } diff --git a/Compiler/PCU/PCUReader.cs b/Compiler/PCU/PCUReader.cs index e9d5bbec9..97c2d94d5 100644 --- a/Compiler/PCU/PCUReader.cs +++ b/Compiler/PCU/PCUReader.cs @@ -2,13 +2,10 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.IO; -using System.Collections; using System.Collections.Generic; using System.Reflection; -using TreeConverter; using PascalABCCompiler.TreeConverter; using PascalABCCompiler.TreeRealization; -using Languages.Facade; namespace PascalABCCompiler.PCU { @@ -50,11 +47,11 @@ namespace PascalABCCompiler.PCU private Dictionary ext_members = new Dictionary(64);//таблица для связывания смещений с импортируемыми сущностями private Dictionary assemblies = new Dictionary(); private Dictionary class_names = new Dictionary(); - private Hashtable used_units = new Hashtable(); - public static Hashtable units = new Hashtable(StringComparer.OrdinalIgnoreCase); + private HashSet used_units = new HashSet(); + public static Dictionary units = new Dictionary(StringComparer.OrdinalIgnoreCase); public static List AllReaders = new List(); public bool need=false; - private Hashtable already_compiled = new Hashtable(); + private HashSet already_compiled = new HashSet(); private bool readDebugInfo=true; private int InitializationMethodOffset = 0; private int FinalizationMethodOffset = 0; @@ -183,12 +180,12 @@ namespace PascalABCCompiler.PCU public void AddAlreadyCompiledUnit(string name) { - already_compiled[name] = name; + already_compiled.Add(name); } public bool AlreadyCompiled(string name) { - return already_compiled[name] != null; + return already_compiled.Contains(name); } public static void CloseUnits() @@ -233,8 +230,7 @@ namespace PascalABCCompiler.PCU { if (id == -1) return this; string s = GetFullUnitName(pcu_file.incl_modules[id], pcu_file.languageCaseSensitive); - var pr = (PCUReader)units[s]; - if (pr == null) + if ( !units.TryGetValue(s, out var pr) ) { pr = new PCUReader(this); pr.GetCompilationUnit(s, this.readDebugInfo, pcu_file.languageCaseSensitive); @@ -257,8 +253,8 @@ namespace PascalABCCompiler.PCU this.FileName = FileName; this.readDebugInfo = readDebugInfo; unit_name = System.IO.Path.GetFileNameWithoutExtension(FileName); - PCUReader pr = (PCUReader)units[FileName]; - if (pr != null) return pr.unit; + if ( units.TryGetValue(FileName, out var pr) ) + return pr.unit; if (!File.Exists(FileName)) return null; //fs = new FileStream(file_name, FileMode.Open, FileAccess.Read); ms = new MemoryStream(File.ReadAllBytes(FileName)); @@ -361,7 +357,7 @@ namespace PascalABCCompiler.PCU if (comp.NeedRecompiled(FileName, pcu_file.incl_modules, this, pcu_file.languageCaseSensitive)) { //comp.RecompileList.Add(unit_name,unit_name); - comp.RecompileList[FileName] = FileName; + comp.RecompileList.Add(FileName); need = true; return need; } @@ -373,14 +369,13 @@ namespace PascalABCCompiler.PCU var used_unit_fname = comp.GetUnitFileName(Path.GetFileNameWithoutExtension(pcu_file.incl_modules[i]), pcu_file.incl_modules[i], dir, new SyntaxTree.SourceContext(0,0,0,0, FileName), pcu_file.languageCaseSensitive); if (Path.GetExtension(used_unit_fname) != comp.CompilerOptions.CompiledUnitExtension) return true; - PCUReader pr = (PCUReader)units[used_unit_fname]; - if (pr == null) + if ( !units.TryGetValue(used_unit_fname, out var pr) ); pr = new PCUReader(this); pr.AddAlreadyCompiledUnit(FileName); - if (used_units[used_unit_fname] == null) + if (!used_units.Contains(used_unit_fname)) { - used_units[used_unit_fname] = used_units; - if (already_compiled[used_unit_fname] == null) + used_units.Add(used_unit_fname); + if (!already_compiled.Contains(used_unit_fname)) { var sub_u = pr.GetCompilationUnit(used_unit_fname, this.readDebugInfo, pcu_file.languageCaseSensitive); if (sub_u == null) return true; @@ -2495,8 +2490,7 @@ namespace PascalABCCompiler.PCU //Добавляем подключенные модули for (int i = 0; i < uses_count; i++) { - PCUReader pcu_r = units[GetFullUnitName(pcu_file.incl_modules[i], pcu_file.languageCaseSensitive)] as PCUReader; - if (pcu_r != null) + if ( units.TryGetValue(GetFullUnitName(pcu_file.incl_modules[i], pcu_file.languageCaseSensitive), out var pcu_r) ); { top_scopes.Add(pcu_r.cun.scope); } diff --git a/Compiler/PCU/PCUWriter.cs b/Compiler/PCU/PCUWriter.cs index 81bd69d1b..d3398f7e4 100644 --- a/Compiler/PCU/PCUWriter.cs +++ b/Compiler/PCU/PCUWriter.cs @@ -255,7 +255,7 @@ namespace PascalABCCompiler.PCU //ivan private List dot_net_name_list = new List(); - private Hashtable tokens = new Hashtable(); + private Dictionary tokens = new Dictionary(); //ivan @@ -1202,8 +1202,10 @@ namespace PascalABCCompiler.PCU private int GetTokenForNetEntity(FieldInfo val) { int off=0; - object o = tokens[val]; - if (o != null) return (int)o; + + if (tokens.TryGetValue(val, out var o)) + return o; + DotNetNameRef dnnr = new DotNetNameRef(); dnnr.kind = DotNetKind.Field; dnnr.name = val.Name; @@ -1218,8 +1220,10 @@ namespace PascalABCCompiler.PCU { int off = 0; //if (tokens.TryGetValue(val, out off)) return off; - object o = tokens[val]; - if (o != null) return (int)o; + + if (tokens.TryGetValue(val, out var o)) + return o; + DotNetNameRef dnnr = new DotNetNameRef(); dnnr.kind = DotNetKind.Method; dnnr.name = val.Name; @@ -1255,8 +1259,10 @@ namespace PascalABCCompiler.PCU { int off = 0; //if (tokens.TryGetValue(val, out off)) return off; - object o = tokens[val]; - if (o != null) return (int)o; + + if (tokens.TryGetValue(val, out var o)) + return o; + DotNetNameRef dnnr = new DotNetNameRef(); dnnr.kind = DotNetKind.Constructor; dnnr.name = ".ctor"; @@ -1284,8 +1290,10 @@ namespace PascalABCCompiler.PCU { int off = 0; //if (tokens.TryGetValue(val, out off)) return off; - object o = tokens[val]; - if (o != null) return (int)o; + + if (tokens.TryGetValue(val, out var o)) + return o; + DotNetNameRef dnnr = new DotNetNameRef(); dnnr.kind = DotNetKind.Property; dnnr.name = val.Name; @@ -1314,8 +1322,10 @@ namespace PascalABCCompiler.PCU { int off = 0; //if (tokens.TryGetValue(val, out off)) return off; - object o = tokens[val]; - if (o != null) return (int)o; + + if (tokens.TryGetValue(val, out var o)) + return o; + DotNetNameRef dnnr = new DotNetNameRef(); dnnr.kind = DotNetKind.Type; if (val.FullName != null) @@ -1891,28 +1901,27 @@ namespace PascalABCCompiler.PCU return offset; } - private Hashtable var_positions = new Hashtable(); - private Hashtable const_positions = new Hashtable(); + private Dictionary> var_positions = new Dictionary>(); + private Dictionary> const_positions = new Dictionary>(); private void SaveVariableReferencePosition(namespace_variable nv) { - List offs = (List)var_positions[nv]; - if (offs == null) - { + if ( !var_positions.TryGetValue(nv, out var offs) ){ offs = new List(); var_positions[nv] = offs; } + offs.Add((int)bw.BaseStream.Position); } - + private void SaveConstantReferencePosition(namespace_constant_definition nv) { - List offs = (List)const_positions[nv]; - if (offs == null) + if ( !const_positions.TryGetValue(nv, out var offs) ) { offs = new List(); const_positions[nv] = offs; } + offs.Add((int)bw.BaseStream.Position); } @@ -1921,7 +1930,7 @@ namespace PascalABCCompiler.PCU int tmp = (int)bw.BaseStream.Position; foreach (namespace_variable nv in var_positions.Keys) { - List offs = (List)var_positions[nv]; + List offs = var_positions[nv]; byte is_def = 0; for (int i = 0; i < offs.Count; i++) { @@ -1931,13 +1940,13 @@ namespace PascalABCCompiler.PCU } bw.BaseStream.Position = tmp; } - + private void WriteConstantPositions() { int tmp = (int)bw.BaseStream.Position; foreach (namespace_constant_definition nv in const_positions.Keys) { - List offs = (List)const_positions[nv]; + List offs = const_positions[nv]; byte is_def = 0; for (int i = 0; i < offs.Count; i++) { diff --git a/Localization/StringResourcesLanguage.cs b/Localization/StringResourcesLanguage.cs index d47c4cf9e..cd7dedb19 100644 --- a/Localization/StringResourcesLanguage.cs +++ b/Localization/StringResourcesLanguage.cs @@ -2,8 +2,6 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Collections.Generic; -using System.Collections; -using System.Text; using System.IO; namespace PascalABCCompiler @@ -11,9 +9,9 @@ namespace PascalABCCompiler public class StringResourcesLanguage { - private static Hashtable AccessibleLanguagesHashtable = new Hashtable(StringComparer.OrdinalIgnoreCase); - private static Hashtable AccessibleTwoLetterISOHashtable = new Hashtable(StringComparer.OrdinalIgnoreCase); - private static Hashtable AccessibleLCIDHashtable = new Hashtable(StringComparer.OrdinalIgnoreCase); + private static Dictionary AccessibleLanguagesHashtable = new Dictionary(StringComparer.OrdinalIgnoreCase); + private static Dictionary AccessibleTwoLetterISOHashtable = new Dictionary(StringComparer.OrdinalIgnoreCase); + private static Dictionary AccessibleLCIDHashtable = new Dictionary(StringComparer.OrdinalIgnoreCase); private static List accessibleLanguages = new List(); private static string DefaultLanguage = null; private static string DefaultTwoLetterISO = null; @@ -119,11 +117,11 @@ namespace PascalABCCompiler get{return currentLanguageName;} set { - if (AccessibleLanguagesHashtable[value] == null) return; + if (!AccessibleLanguagesHashtable.ContainsKey(value)) return; currentLanguageName = value; - currentTwoLetterISO = AccessibleTwoLetterISOHashtable[value] as string; - currentLCID = AccessibleLCIDHashtable[currentTwoLetterISO] as string; - StringResources.ResDirectoryName = AccessibleLanguagesHashtable[currentLanguageName] as string; + currentTwoLetterISO = AccessibleTwoLetterISOHashtable[value]; + currentLCID = AccessibleLCIDHashtable[currentTwoLetterISO]; + StringResources.ResDirectoryName = AccessibleLanguagesHashtable[currentLanguageName]; StringResources.UpdateObjectsText(); } } @@ -146,7 +144,8 @@ namespace PascalABCCompiler public static string GetLCIDByTwoLetterISO(string iso) { - return AccessibleLCIDHashtable[iso] as string; + AccessibleLCIDHashtable.TryGetValue(iso, out var result); + return result; } } } diff --git a/NETGenerator/Helpers.cs b/NETGenerator/Helpers.cs index 0aacf2788..5acf4e385 100644 --- a/NETGenerator/Helpers.cs +++ b/NETGenerator/Helpers.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.Reflection.Emit; using System.Collections; using System.Linq; +using System.Collections.Generic; namespace PascalABCCompiler.NETGenerator { @@ -515,11 +516,11 @@ namespace PascalABCCompiler.NETGenerator { public class Helper { public Hashtable defs=new Hashtable(); - private Hashtable processing_types = new Hashtable(); + private HashSet processing_types = new HashSet(); private MethodInfo arr_mi=null; - private Hashtable pas_defs = new Hashtable(); - private Hashtable memoized_exprs = new Hashtable(); - private Hashtable dummy_methods = new Hashtable(); + private Dictionary pas_defs = new Dictionary(); + private Dictionary memoized_exprs = new Dictionary(); + private Dictionary dummy_methods = new Dictionary(); public Helper() {} @@ -528,9 +529,9 @@ namespace PascalABCCompiler.NETGenerator { dummy_methods[tb] = mb; } - public MethodBuilder GetDummyMethod(TypeBuilder tb) + public MethodBuilder GetDummyMethod(TypeBuilder tb) { - return dummy_methods[tb] as MethodBuilder; + return dummy_methods[tb]; } public void AddPascalTypeReference(ITypeNode tn, Type t) @@ -540,7 +541,8 @@ namespace PascalABCCompiler.NETGenerator { public Type GetPascalTypeReference(ITypeNode tn) { - return pas_defs[tn] as Type; + pas_defs.TryGetValue(tn, out var result); + return result; } public ConstInfo AddConstant(IConstantDefinitionNode cnst, FieldBuilder fb) @@ -872,14 +874,14 @@ namespace PascalABCCompiler.NETGenerator { return null; } - public void SetAsProcessing(ICommonTypeNode type) + public void SetAsProcessing(ICommonTypeNode type) { - processing_types[type] = true; + processing_types.Add(type); } public bool IsProcessing(ICommonTypeNode type) { - return processing_types[type] != null; + return processing_types.Contains(type); } public void LinkExpressionToLocalBuilder(IExpressionNode expr, LocalBuilder lb) @@ -889,7 +891,8 @@ namespace PascalABCCompiler.NETGenerator { public LocalBuilder GetLocalBuilderForExpression(IExpressionNode expr) { - return memoized_exprs[expr] as LocalBuilder; + memoized_exprs.TryGetValue(expr, out var result); + return result; } //получение типа @@ -1050,6 +1053,4 @@ namespace PascalABCCompiler.NETGenerator { return m; } } - - -} +} \ No newline at end of file diff --git a/NETGenerator/NETGenegratorTools.cs b/NETGenerator/NETGenegratorTools.cs index 59aa63b2f..8c050ac3c 100644 --- a/NETGenerator/NETGenegratorTools.cs +++ b/NETGenerator/NETGenegratorTools.cs @@ -2,9 +2,7 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using System.Collections.Generic; -using System.Text; using System.Reflection.Emit; -using System.Collections; using System.Reflection; using System.Runtime.InteropServices; @@ -19,7 +17,7 @@ namespace PascalABCCompiler.NETGenerator public static TypeInfo string_type; public static TypeInfo byte_type; public static Type ExceptionType = typeof(Exception); - public static Type VoidType = typeof(void); + public static Type VoidType = typeof(void); public static Type StringType = typeof(string); public static Type ObjectType = typeof(object); public static Type MonitorType = typeof(System.Threading.Monitor); @@ -46,7 +44,7 @@ namespace PascalABCCompiler.NETGenerator public static Type DoubleType = typeof(Double); public static Type GCHandleType = typeof(GCHandle); public static Type MarshalType = typeof(Marshal); - public static Type TypeType = typeof(Type); + public static Type TypeType = typeof(Type); public static Type ValueType = typeof(ValueType); public static Type IEnumerableType = typeof(System.Collections.IEnumerable); public static Type IEnumeratorType = typeof(System.Collections.IEnumerator); @@ -54,13 +52,13 @@ namespace PascalABCCompiler.NETGenerator public static Type IEnumerableGenericType = typeof(System.Collections.Generic.IEnumerable<>); public static Type IEnumeratorGenericType = typeof(System.Collections.Generic.IEnumerator<>); - private static Hashtable types; - private static Hashtable sizes; + private static HashSet types; + private static Dictionary sizes; public static MethodInfo ArrayCopyMethod; public static MethodInfo GetTypeFromHandleMethod; - public static MethodInfo ResizeMethod; + public static MethodInfo ResizeMethod; public static MethodInfo GCHandleFreeMethod; - public static MethodInfo StringNullOrEmptyMethod; + public static MethodInfo StringNullOrEmptyMethod; public static MethodInfo UnsizedArrayCreateMethodTemplate = null; public static MethodInfo GCHandleAlloc; public static MethodInfo GCHandleAllocPinned; @@ -86,34 +84,30 @@ namespace PascalABCCompiler.NETGenerator char_type = new TypeInfo(typeof(char)); string_type = new TypeInfo(typeof(string)); byte_type = new TypeInfo(typeof(byte)); - - types = new Hashtable(); - types[BoolType] = BoolType; - types[SByteType] = SByteType; - types[ByteType] = ByteType; - types[CharType] = CharType; - types[Int16Type] = Int16Type; - types[Int32Type] = Int32Type; - types[Int64Type] = Int64Type; - types[UInt16Type] = UInt16Type; - types[UInt32Type] = UInt32Type; - types[UInt64Type] = UInt64Type; - types[SingleType] = SingleType; - types[DoubleType] = DoubleType; - sizes = new Hashtable(); - sizes[BoolType] = sizeof(Boolean); - sizes[SByteType] = sizeof(SByte); - sizes[ByteType] = sizeof(Byte); - sizes[CharType] = sizeof(Char); - sizes[Int16Type] = sizeof(Int16); - sizes[Int32Type] = sizeof(Int32); - sizes[Int64Type] = sizeof(Int64); - sizes[UInt16Type] = sizeof(UInt16); - sizes[UInt32Type] = sizeof(UInt32); - sizes[UInt64Type] = sizeof(UInt64); - sizes[SingleType] = sizeof(Single); - sizes[DoubleType] = sizeof(Double); + types = new HashSet() + { + BoolType, SByteType, ByteType, CharType, + Int16Type, Int32Type, Int64Type, + UInt16Type, UInt32Type, UInt64Type, + SingleType, DoubleType + }; + + sizes = new Dictionary + { + [BoolType] = sizeof(Boolean), + [SByteType] = sizeof(SByte), + [ByteType] = sizeof(Byte), + [CharType] = sizeof(Char), + [Int16Type] = sizeof(Int16), + [Int32Type] = sizeof(Int32), + [Int64Type] = sizeof(Int64), + [UInt16Type] = sizeof(UInt16), + [UInt32Type] = sizeof(UInt32), + [UInt64Type] = sizeof(UInt64), + [SingleType] = sizeof(Single), + [DoubleType] = sizeof(Double) + }; //sizes[UIntPtr] = sizeof(UIntPtr); //types[TypeType] = TypeType; @@ -133,12 +127,12 @@ namespace PascalABCCompiler.NETGenerator public static bool IsStandType(Type t) { - return types[t] != null; + return types.Contains(t); } public static int GetPrimitiveTypeSize(Type PrimitiveType) { - return (int)sizes[PrimitiveType]; + return sizes[PrimitiveType]; } } diff --git a/Optimizer/OptimizerTools.cs b/Optimizer/OptimizerTools.cs index f99faadfa..0a3fb7816 100644 --- a/Optimizer/OptimizerTools.cs +++ b/Optimizer/OptimizerTools.cs @@ -1,8 +1,6 @@ // Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) -using System; using System.Collections.Generic; -using System.Text; using PascalABCCompiler.TreeRealization; using System.Collections; @@ -37,8 +35,8 @@ namespace PascalABCCompiler public class OptimizerHelper { private Hashtable ht = new Hashtable(); - private Hashtable warns = new Hashtable(); - private Hashtable ext_funcs = new Hashtable(); + private Dictionary> warns = new Dictionary>(); + private HashSet ext_funcs = new HashSet(); public void AddVariable(var_definition_node vdn) { @@ -76,8 +74,7 @@ namespace PascalABCCompiler public void AddTempWarning(var_definition_node vdn, CompilerWarningWithLocation cw) { - List lst = (List)warns[vdn]; - if (lst == null) + if ( !warns.TryGetValue(vdn, out var lst) ) { lst = new List(); warns[vdn] = lst; @@ -87,18 +84,18 @@ namespace PascalABCCompiler public void AddRealWarning(var_definition_node vdn, List cw) { - List lst = (List)warns[vdn]; + List lst = warns[vdn]; cw.AddRange(lst); } public void MarkAsExternal(common_function_node cfn) { - ext_funcs[cfn] = cfn; + ext_funcs.Add(cfn); } public bool IsExternal(common_function_node cfn) { - return ext_funcs[cfn] != null; + return ext_funcs.Contains(cfn); } } } \ No newline at end of file diff --git a/ParserTools/XmlDocs.cs b/ParserTools/XmlDocs.cs index 02ca9b773..7bd7cf7fe 100644 --- a/ParserTools/XmlDocs.cs +++ b/ParserTools/XmlDocs.cs @@ -1,25 +1,23 @@ // Copyright (c) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt) // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; -using System.Collections; using System.Collections.Generic; using System.IO; using System.Threading; using System.Xml; using System.Reflection; using System.Text; -using System.Globalization; namespace CodeCompletionTools { public partial class AssemblyDocCache { - private static Hashtable ht=new Hashtable(); + private static Dictionary ht = new Dictionary(); public static string Load(Assembly a, string path) { - if (ht[a] != null) return null; + if (!ht.ContainsKey(a)) return null; string dir; if (string.IsNullOrEmpty(a.Location)) dir = path; @@ -177,7 +175,7 @@ namespace CodeCompletionTools public static string GetDocumentation(Type t) { - XmlDoc xdoc = (XmlDoc)ht[t.Assembly]; + ht.TryGetValue(t.Assembly, out var xdoc); try { if (xdoc != null) @@ -195,7 +193,7 @@ namespace CodeCompletionTools public static string GetFullDocumentation(Type t) { - XmlDoc xdoc = (XmlDoc)ht[t.Assembly]; + ht.TryGetValue(t.Assembly, out var xdoc); try { if (xdoc != null) @@ -277,11 +275,11 @@ namespace CodeCompletionTools return sb.ToString(); } - public static string GetDocumentation(ConstructorInfo mi) - { + public static string GetDocumentation(ConstructorInfo mi) + { try { - XmlDoc xdoc = (XmlDoc)ht[mi.DeclaringType.Assembly]; + ht.TryGetValue(mi.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { string s = GetNormalHint(xdoc.GetDocumentation("M:" + mi.DeclaringType.FullName + ".#ctor" + GetParamNames(mi), false)); @@ -292,14 +290,14 @@ namespace CodeCompletionTools { } - return ""; - } + return ""; + } public static string GetFullDocumentation(ConstructorInfo mi) { try { - XmlDoc xdoc = (XmlDoc)ht[mi.DeclaringType.Assembly]; + ht.TryGetValue(mi.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { return xdoc.GetDocumentation("M:" + mi.DeclaringType.FullName + ".#ctor" + GetParamNames(mi), false); @@ -316,7 +314,7 @@ namespace CodeCompletionTools { try { - XmlDoc xdoc = (XmlDoc)ht[fi.DeclaringType.Assembly]; + ht.TryGetValue(fi.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { string s = GetNormalHint(xdoc.GetDocumentation("F:"+fi.DeclaringType.FullName+"."+fi.Name,false)); @@ -334,7 +332,7 @@ namespace CodeCompletionTools { try { - XmlDoc xdoc = (XmlDoc)ht[fi.DeclaringType.Assembly]; + ht.TryGetValue(fi.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { return xdoc.GetDocumentation("F:" + fi.DeclaringType.FullName + "." + fi.Name, false); @@ -351,7 +349,7 @@ namespace CodeCompletionTools { try { - XmlDoc xdoc = (XmlDoc)ht[pi.DeclaringType.Assembly]; + ht.TryGetValue(pi.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { string s = GetNormalHint(xdoc.GetDocumentation("P:"+pi.DeclaringType.FullName+"."+pi.Name,false)); @@ -369,7 +367,7 @@ namespace CodeCompletionTools { try { - XmlDoc xdoc = (XmlDoc)ht[pi.DeclaringType.Assembly]; + ht.TryGetValue(pi.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { return xdoc.GetDocumentation("P:" + pi.DeclaringType.FullName + "." + pi.Name, false); @@ -386,7 +384,7 @@ namespace CodeCompletionTools { try { - XmlDoc xdoc = (XmlDoc)ht[ei.DeclaringType.Assembly]; + ht.TryGetValue(ei.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { string s = GetNormalHint(xdoc.GetDocumentation("E:"+ei.DeclaringType.FullName+"."+ei.Name,false)); @@ -404,7 +402,7 @@ namespace CodeCompletionTools { try { - XmlDoc xdoc = (XmlDoc)ht[ei.DeclaringType.Assembly]; + ht.TryGetValue(ei.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { return xdoc.GetDocumentation("E:" + ei.DeclaringType.FullName + "." + ei.Name, false); @@ -429,7 +427,7 @@ namespace CodeCompletionTools { try { - XmlDoc xdoc = (XmlDoc)ht[mi.DeclaringType.Assembly]; + ht.TryGetValue(mi.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { string generic_add = GetGenericAddString(mi); @@ -448,7 +446,7 @@ namespace CodeCompletionTools { try { - XmlDoc xdoc = (XmlDoc)ht[mi.DeclaringType.Assembly]; + ht.TryGetValue(mi.DeclaringType.Assembly, out var xdoc); if (xdoc != null) { string generic_add = GetGenericAddString(mi); @@ -470,7 +468,7 @@ namespace CodeCompletionTools public static string GetDocumentation(Assembly a, string descr) { - XmlDoc xdoc = (XmlDoc)ht[a]; + ht.TryGetValue(a, out var xdoc); try { if (xdoc != null) diff --git a/TreeConverter/NetWrappers/NetHelper.cs b/TreeConverter/NetWrappers/NetHelper.cs index 8a3b057eb..992718281 100644 --- a/TreeConverter/NetWrappers/NetHelper.cs +++ b/TreeConverter/NetWrappers/NetHelper.cs @@ -6,7 +6,6 @@ using System.Collections; using System.Collections.Generic; using System.IO; using System.Reflection; -using System.Threading; using PascalABCCompiler.TreeConverter; using PascalABCCompiler.TreeRealization; @@ -286,9 +285,9 @@ namespace PascalABCCompiler.NetHelper } public static class NetHelper { - private static Hashtable namespaces; - private static Hashtable types; - private static Dictionary type_search_cache; + private static Dictionary namespaces; + private static Hashtable types; + private static Dictionary type_search_cache; private static Hashtable compiled_pascal_types; /*private static Hashtable methods; private static Hashtable properties; @@ -300,19 +299,19 @@ namespace PascalABCCompiler.NetHelper //private static Hashtable meth_nodes; private static Dictionary prop_nodes; private static Hashtable field_nodes; - private static Hashtable constr_nodes; - private static Hashtable stand_types; - private static Hashtable type_handles; - private static Hashtable method_handles; - private static Hashtable constr_handles; - private static Hashtable field_handles; - private static Hashtable assemblies; - private static Hashtable special_types; - private static Hashtable ns_types; + private static Dictionary constr_nodes; + private static HashSet stand_types; + private static Dictionary> type_handles; + private static Dictionary> method_handles; + private static Dictionary> constr_handles; + private static Dictionary> field_handles; + private static HashSet assemblies; + private static Dictionary special_types; + private static Dictionary ns_types; private static Type memberInfo; - private static Hashtable namespace_assemblies; - public static Hashtable cur_used_assemblies; - private static Hashtable cached_type_extensions; + private static Dictionary> namespace_assemblies; + public static HashSet cur_used_assemblies; + private static HashSet cached_type_extensions; public static Type DelegateType; public static Type EnumType; public static Type ArrayType; @@ -334,29 +333,28 @@ namespace PascalABCCompiler.NetHelper public static Dictionary> generics_names = new Dictionary>(); private static Dictionary member_cache = new Dictionary(); - public static void reset() - { + public static void reset() + { if (cur_used_assemblies == null) - cur_used_assemblies = new Hashtable(); + cur_used_assemblies = new HashSet(); cur_used_assemblies.Clear(); - cur_used_assemblies[typeof(string).Assembly] = typeof(string).Assembly; - cur_used_assemblies[typeof(Microsoft.CSharp.CSharpCodeProvider).Assembly] = typeof(Microsoft.CSharp.CSharpCodeProvider).Assembly; + cur_used_assemblies.Add(typeof(string).Assembly); + cur_used_assemblies.Add(typeof(Microsoft.CSharp.CSharpCodeProvider).Assembly); type_search_cache.Clear(); + } - } - - private static Hashtable ass_name_cache; - private static Hashtable file_dates; + private static Dictionary ass_name_cache; + private static Dictionary file_dates; private static Dictionary assm_full_paths; - public static bool IsAssemblyChanged(string name) + public static bool IsAssemblyChanged(string name) { if (name == null) return false; - Assembly a = ass_name_cache[name] as Assembly; + ass_name_cache.TryGetValue(name, out var a); if (a != null) { - if (System.IO.File.GetLastWriteTime(name) != (DateTime)file_dates[a]) + if (System.IO.File.GetLastWriteTime(name) != file_dates[a]) return true; return false; } @@ -367,12 +365,12 @@ namespace PascalABCCompiler.NetHelper public static Assembly LoadAssembly(string name, bool use_load_from = false) { if (name == null) return null; - Assembly a = ass_name_cache[name] as Assembly; + ass_name_cache.TryGetValue(name, out var a); if (a == null && name.IndexOf(System.IO.Path.DirectorySeparatorChar) == -1) - a = ass_name_cache[System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), name)] as Assembly; + ass_name_cache.TryGetValue(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), name), out a); if (a != null) { - if (System.IO.File.GetLastWriteTime(name) == (DateTime)file_dates[a]) + if (System.IO.File.GetLastWriteTime(name) == file_dates[a]) return a; namespace_assemblies.Remove(a); assemblies.Remove(a); @@ -433,24 +431,23 @@ namespace PascalABCCompiler.NetHelper } - public static Hashtable entry_types = new Hashtable(); + public static Dictionary> entry_types = new Dictionary>(); private static string curr_inited_assm_path = null; public static List init_namespaces(System.Reflection.Assembly _assembly) { - Assembly assembly = (Assembly)assemblies[_assembly]; + Assembly assembly = assemblies.Contains(_assembly) ? _assembly : null; List nss = new List(); - cur_used_assemblies[_assembly] = _assembly; + cur_used_assemblies.Add(_assembly); Type entry_type = null; - List unit_types = entry_types[_assembly] as List; - if (unit_types == null) + if ( !entry_types.TryGetValue(_assembly, out var unit_types) ) unit_types = new List(); if (assembly == null) { Type[] tarr = _assembly.GetTypes(); //Hashtable ns_ht = new Hashtable(CaseInsensitiveHashCodeProvider.Default,CaseInsensitiveComparer.Default); - Hashtable ns_ht = new Hashtable(StringComparer.CurrentCultureIgnoreCase); + var ns_ht = new HashSet(StringComparer.CurrentCultureIgnoreCase); foreach (Type t in tarr) { if (t.IsNotPublic) @@ -461,7 +458,7 @@ namespace PascalABCCompiler.NetHelper int pos = s.LastIndexOf('.'); //if (pos == -1) //if (namespaces[s] == null) ns_ht.Add(s,s); - ns_ht[s] = s; + ns_ht.Add(s); namespaces[s] = t; // SSM 17.05.19 Запретил uses Reflection: https://github.com/pascalabcnet/pascalabcnet/issues/1941 /*if (pos != -1) @@ -482,9 +479,9 @@ namespace PascalABCCompiler.NetHelper s = s.Substring(0, pos); pos = s.LastIndexOf('.'); //if (pos == -1) - if (namespaces[s] == null) + if (!namespaces.ContainsKey(s)) namespaces[s] = t; - ns_ht[s] = s; + ns_ht.Add(s); } } TypeInfo ti = new TypeInfo(t, t.FullName); @@ -747,7 +744,7 @@ namespace PascalABCCompiler.NetHelper } } namespace_assemblies[_assembly] = ns_ht; - assemblies[_assembly] = _assembly; + assemblies.Add(_assembly); } return unit_types; } @@ -856,13 +853,13 @@ namespace PascalABCCompiler.NetHelper public static bool NamespaceExists(string Namespace) { - Type t = (Type)namespaces[Namespace]; - - if (t != null && cur_used_assemblies.ContainsKey(t.Assembly)) return true; - foreach (Assembly a in namespace_assemblies.Keys) - if (cur_used_assemblies.ContainsKey(a) && (namespace_assemblies[a] as Hashtable).ContainsKey(Namespace)) + namespaces.TryGetValue(Namespace, out var t); + + if (t != null && cur_used_assemblies.Contains(t.Assembly)) return true; + foreach (Assembly a in namespace_assemblies.Keys) + if (cur_used_assemblies.Contains(a) && namespace_assemblies[a].Contains(Namespace)) return true; - return false; + return false; } static NetHelper() @@ -875,46 +872,46 @@ namespace PascalABCCompiler.NetHelper //types = new Hashtable(1024, CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default); types = new Hashtable(8096, StringComparer.CurrentCultureIgnoreCase); compiled_pascal_types = new Hashtable(1024, StringComparer.CurrentCultureIgnoreCase); - namespaces = new Hashtable(1024, StringComparer.CurrentCultureIgnoreCase); - ass_name_cache = new Hashtable(1024, StringComparer.CurrentCultureIgnoreCase); + namespaces = new Dictionary(1024, StringComparer.CurrentCultureIgnoreCase); + ass_name_cache = new Dictionary(1024, StringComparer.CurrentCultureIgnoreCase); assm_full_paths = new Dictionary(); //ass_name_cache = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default); - file_dates = new Hashtable(); + file_dates = new Dictionary(); //methods = new Hashtable(); //properties = new Hashtable(); members = new Dictionary>>(128); //fields = new Hashtable(); - assemblies = new Hashtable(); + assemblies = new HashSet(); //meth_nodes = new Hashtable(); prop_nodes = new Dictionary(); field_nodes = new Hashtable(); - constr_nodes = new Hashtable(); - stand_types = new Hashtable(); - type_handles = new Hashtable(); - method_handles = new Hashtable(); - constr_handles = new Hashtable(); - field_handles = new Hashtable(); - special_types = new Hashtable(); + constr_nodes = new Dictionary(); + stand_types = new HashSet(); + type_handles = new Dictionary>(); + method_handles = new Dictionary>(); + constr_handles = new Dictionary>(); + field_handles = new Dictionary>(); + special_types = new Dictionary(); type_search_cache = new Dictionary(300); - cached_type_extensions = new Hashtable(); - namespace_assemblies = new Hashtable(); - cur_used_assemblies = new Hashtable(); + cached_type_extensions = new HashSet(); + namespace_assemblies = new Dictionary>(); + cur_used_assemblies = new HashSet(); //ns_types = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default); - ns_types = new Hashtable(StringComparer.CurrentCultureIgnoreCase); + ns_types = new Dictionary(StringComparer.CurrentCultureIgnoreCase); memberInfo = typeof(MemberInfo); - stand_types[typeof(int)] = stand_types; - stand_types[typeof(byte)] = stand_types; - stand_types[typeof(bool)] = stand_types; - stand_types[typeof(sbyte)] = stand_types; - stand_types[typeof(short)] = stand_types; - stand_types[typeof(ushort)] = stand_types; - stand_types[typeof(uint)] = stand_types; - stand_types[typeof(long)] = stand_types; - stand_types[typeof(ulong)] = stand_types; - stand_types[typeof(char)] = stand_types; - stand_types[typeof(float)] = stand_types; - stand_types[typeof(double)] = stand_types; + stand_types.Add(typeof(int)); + stand_types.Add(typeof(byte)); + stand_types.Add(typeof(bool)); + stand_types.Add(typeof(sbyte)); + stand_types.Add(typeof(short)); + stand_types.Add(typeof(ushort)); + stand_types.Add(typeof(uint)); + stand_types.Add(typeof(long)); + stand_types.Add(typeof(ulong)); + stand_types.Add(typeof(char)); + stand_types.Add(typeof(float)); + stand_types.Add(typeof(double)); //stand_types[typeof(decimal)]=stand_types; //stand_types[NetHelper.void_ptr_type] = stand_types; DelegateType = typeof(Delegate); @@ -970,36 +967,35 @@ namespace PascalABCCompiler.NetHelper special_types[t.MetadataToken] = t; } - public static bool IsStandType(Type t) - { - if (stand_types[t] != null) return true; - return false; - } - - public static bool IsNetNamespace(string name, Type tt = null) - { + public static bool IsStandType(Type t) + { + return stand_types.Contains(t); + } + + public static bool IsNetNamespace(string name, Type tt = null) + { Type t = null; if (tt != null) t = tt; else - t = namespaces[name] as Type; - if (t != null && cur_used_assemblies.ContainsKey(t.Assembly)) return true; - foreach (Assembly a in namespace_assemblies.Keys) - if (cur_used_assemblies != null && cur_used_assemblies.ContainsKey(a) && (namespace_assemblies[a] as Hashtable).ContainsKey(name)) + namespaces.TryGetValue(name, out t); + if (t != null && cur_used_assemblies.Contains(t.Assembly)) return true; + foreach (Assembly a in namespace_assemblies.Keys) + if (cur_used_assemblies != null && cur_used_assemblies.Contains(a) && namespace_assemblies[a].Contains(name)) return true; - return false; - } + return false; + } public static bool IsNetNamespaceInAssembly(string name, Assembly a) { - if (cur_used_assemblies != null && cur_used_assemblies.ContainsKey(a) && (namespace_assemblies[a] as Hashtable).ContainsKey(name)) + if (cur_used_assemblies != null && cur_used_assemblies.Contains(a) && namespace_assemblies[a].Contains(name)) return true; return false; } public static bool IsNetNamespace(string name,PascalABCCompiler.TreeRealization.using_namespace_list _unar, out string full_ns) { - Type t = namespaces[name] as Type; + namespaces.TryGetValue(name, out var t); full_ns = name; if (t != null) { @@ -1023,8 +1019,7 @@ namespace PascalABCCompiler.NetHelper for (int i = 0; i < _unar.Count; i++) { string full_name = _unar[i].namespace_name + "." + name; - t = namespaces[full_name] as Type; - if (t != null) + if ( namespaces.TryGetValue(full_name, out t) ) { full_ns = full_name; return IsNetNamespace(full_ns, t); @@ -1051,7 +1046,7 @@ namespace PascalABCCompiler.NetHelper public static IEnumerable GetExtensionMethods(Type t) { List meths = null; - if (SystemCoreAssembly == null || !cur_used_assemblies.ContainsKey(SystemCoreAssembly)) + if (SystemCoreAssembly == null || !cur_used_assemblies.Contains(SystemCoreAssembly)) return new List(); if (type_extensions.TryGetValue(t, out meths) || t.IsGenericType && type_extensions.TryGetValue(t.GetGenericTypeDefinition(), out meths)) { @@ -1213,14 +1208,14 @@ namespace PascalABCCompiler.NetHelper public static string[] GetNamespaces(Assembly a) { - Hashtable ht = (Hashtable)namespace_assemblies[a]; + var ht = namespace_assemblies[a]; List lst = new List(); - foreach (string s in ht.Values) + foreach (string s in ht) //if (s.IndexOf('.') == -1) lst.Add(s); return lst.ToArray(); } - + public static void FormBaseInterfacesList(Type t, List interfaces) { if (interfaces.IndexOf(t) < 0) @@ -1297,7 +1292,7 @@ namespace PascalABCCompiler.NetHelper arrays_with_extension_methods[tmp_t] = tmp_t; if (tmp_t.IsGenericTypeDefinition) generics_with_extension_methods[tmp_t] = tmp_t; - cached_type_extensions[t] = t; + cached_type_extensions.Add(t); while (tmp_t != null) { List meths1 = null; @@ -1320,7 +1315,7 @@ namespace PascalABCCompiler.NetHelper all_meths.AddRange(meths3); foreach (MethodInfo mi in all_meths) { - if (cur_used_assemblies.ContainsKey(mi.DeclaringType.Assembly)) + if (cur_used_assemblies.Contains(mi.DeclaringType.Assembly)) { List al = null; string s = StringConstants.GetNETOperName(mi.Name); @@ -1348,7 +1343,7 @@ namespace PascalABCCompiler.NetHelper { foreach (MethodInfo mi in meths) { - if (cur_used_assemblies.ContainsKey(mi.DeclaringType.Assembly)) + if (cur_used_assemblies.Contains(mi.DeclaringType.Assembly)) { List al = null; if (!ht.TryGetValue(mi.Name, out al)) @@ -1931,7 +1926,7 @@ namespace PascalABCCompiler.NetHelper public static compiled_constructor_node GetConstructorNode(ConstructorInfo ci) { - return (compiled_constructor_node)constr_nodes[ci]; + return constr_nodes[ci]; } public static void AddConstructor(ConstructorInfo ci, compiled_constructor_node ccn) @@ -2051,7 +2046,7 @@ namespace PascalABCCompiler.NetHelper return null; List ti_list = fi.GetTypesByNamespaceList(_unar); foreach (TypeInfo ti in ti_list) - if (cur_used_assemblies.ContainsKey(ti.type.Assembly)) + if (cur_used_assemblies.Contains(ti.type.Assembly)) return ti.type; } object o = types[name]; @@ -2074,7 +2069,7 @@ namespace PascalABCCompiler.NetHelper { fi.type_infos.Add(new TypeNamespaceInfo(t, null)); } - if (cur_used_assemblies.ContainsKey(t.type.Assembly)) + if (cur_used_assemblies.Contains(t.type.Assembly)) return t.type; else return null; @@ -2092,7 +2087,7 @@ namespace PascalABCCompiler.NetHelper { fi.type_infos.Add(new TypeNamespaceInfo(t, _unar[i])); } - if (cur_used_assemblies.ContainsKey(t.type.Assembly)) + if (cur_used_assemblies.Contains(t.type.Assembly)) return t.type; else return null; @@ -2116,7 +2111,7 @@ namespace PascalABCCompiler.NetHelper { fi.type_infos.Add(new TypeNamespaceInfo(t, null)); } - if (cur_used_assemblies.ContainsKey(t.type.Assembly)) + if (cur_used_assemblies.Contains(t.type.Assembly)) return t.type; } for (int i = 0; i < _unar.Count; i++) @@ -2132,7 +2127,7 @@ namespace PascalABCCompiler.NetHelper { fi.type_infos.Add(new TypeNamespaceInfo(t, _unar[i])); } - if (cur_used_assemblies.ContainsKey(t.type.Assembly)) + if (cur_used_assemblies.Contains(t.type.Assembly)) return t.type; //else // return null; @@ -2152,10 +2147,10 @@ namespace PascalABCCompiler.NetHelper List founded_types = new List(); foreach (TypeInfo t in typs) { - if (cur_used_assemblies.ContainsKey(t.type.Assembly)) + if (cur_used_assemblies.Contains(t.type.Assembly)) founded_types.Add(t); } - if (founded_types.Count == 1 && cur_used_assemblies.ContainsKey(founded_types[0].type.Assembly)) + if (founded_types.Count == 1 && cur_used_assemblies.Contains(founded_types[0].type.Assembly)) return founded_types[0].type; else return null; @@ -2176,7 +2171,7 @@ namespace PascalABCCompiler.NetHelper //ivan added - runtime types adding Type t = Type.GetType(name, false, true); if (t == null) - foreach (Assembly a in assemblies.Values) + foreach (Assembly a in assemblies) { t = a.GetType(name, false, true); if (t != null) break; @@ -2210,8 +2205,8 @@ namespace PascalABCCompiler.NetHelper public static Type[] FindTypesInNamespace(string name) { List lst = new List(); - Type[] typs = (Type[])ns_types[name]; - if (typs != null) return typs; + if ( ns_types.TryGetValue(name, out var typs) ) + return typs; foreach (string s in types.Keys) { TypeInfo ti = types[s] as TypeInfo; @@ -2247,75 +2242,79 @@ namespace PascalABCCompiler.NetHelper return lst.ToArray(); } - private static Hashtable InitHandles(Assembly a) + private static Dictionary InitHandles(Assembly a) { Type[] types = a.GetTypes(); - Hashtable ht = new Hashtable(); + var ht = new Dictionary(); foreach (Type t in types) - ht[(int)t.MetadataToken] = t; + ht[t.MetadataToken] = t; type_handles[a] = ht; return ht; } - private static Hashtable InitMethodHandles(Type t) + private static Dictionary InitMethodHandles(Type t) { MethodInfo[] mis = t.GetMethods(); - Hashtable ht = new Hashtable(); + var ht = new Dictionary(); foreach (MethodInfo mi in mis) - ht[(int)mi.MetadataToken] = mi; + ht[mi.MetadataToken] = mi; method_handles[t] = ht; return ht; } - private static Hashtable InitConstructorHandles(Type t) + private static Dictionary InitConstructorHandles(Type t) { ConstructorInfo[] cis = t.GetConstructors(); - Hashtable ht = new Hashtable(); + var ht = new Dictionary(); foreach (ConstructorInfo ci in cis) - ht[(int)ci.MetadataToken] = ci; + ht[ci.MetadataToken] = ci; constr_handles[t] = ht; return ht; } - private static Hashtable InitFieldHandles(Type t) + private static Dictionary InitFieldHandles(Type t) { FieldInfo[] fis = t.GetFields(); - Hashtable ht = new Hashtable(); + var ht = new Dictionary(); foreach (FieldInfo fi in fis) - ht[(int)fi.MetadataToken] = fi; + ht[fi.MetadataToken] = fi; field_handles[t] = ht; return ht; } public static Type FindTypeByHandle(Assembly a, int handle) { - Hashtable ht = (Hashtable)type_handles[a]; - if (ht == null) ht = InitHandles(a); - Type t = (Type)ht[handle]; - if (t == null) - return (Type)special_types[handle]; + if (!type_handles.TryGetValue(a, out var ht)) + ht = InitHandles(a); + + if (!ht.TryGetValue(handle, out var t)) + t = special_types[handle]; + return t; } public static MethodInfo FindMethodByHandle(Type t, int handle) { - Hashtable ht = (Hashtable)method_handles[t]; - if (ht == null) ht = InitMethodHandles(t); - return (MethodInfo)ht[handle]; + if (!method_handles.TryGetValue(t, out var ht)) + ht = InitMethodHandles(t); + + return ht[handle]; } public static ConstructorInfo FindConstructorByHandle(Type t, int handle) { - Hashtable ht = (Hashtable)constr_handles[t]; - if (ht == null) ht = InitConstructorHandles(t); - return (ConstructorInfo)ht[handle]; + if (!constr_handles.TryGetValue(t, out var ht)) + ht = InitConstructorHandles(t); + + return ht[handle]; } public static FieldInfo FindFieldByHandle(Type t, int handle) { - Hashtable ht = (Hashtable)field_handles[t]; - if (ht == null) ht = InitFieldHandles(t); - return (FieldInfo)ht[handle]; + if (!field_handles.TryGetValue(t, out var ht)) + ht = InitFieldHandles(t); + + return ht[handle]; } public static void AddGenericInfo(Type t) diff --git a/TreeConverter/SystemLib/StaticSystemLib.cs b/TreeConverter/SystemLib/StaticSystemLib.cs index 7ba133fc7..f266f1f85 100644 --- a/TreeConverter/SystemLib/StaticSystemLib.cs +++ b/TreeConverter/SystemLib/StaticSystemLib.cs @@ -18,7 +18,6 @@ namespace PascalABCCompiler.SystemLibrary //TODO: Все базовые методы должны быть определены здесь как статические и использовать их отсюда, а не создавать их по два раза. public static class SystemLibrary { - private static compiled_type_node _bool_type; private static compiled_type_node _byte_type; private static compiled_type_node _sbyte_type; @@ -95,7 +94,7 @@ namespace PascalABCCompiler.SystemLibrary private static SymbolTable.TreeConverterSymbolTable _symtab; private static System.Collections.Generic.Dictionary ht = new System.Collections.Generic.Dictionary(); - private static System.Collections.Hashtable writable_in_typed_files_types = new System.Collections.Hashtable(); + private static HashSet writable_in_typed_files_types = new HashSet(); private static string_const_node _empty_string; @@ -2079,18 +2078,18 @@ namespace PascalABCCompiler.SystemLibrary // То есть, это было неправильное исправление writable_in_typed_files_types.Clear(); - writable_in_typed_files_types.Add(_bool_type, _bool_type); - writable_in_typed_files_types.Add(_byte_type, _byte_type); - writable_in_typed_files_types.Add(_sbyte_type, _sbyte_type); - writable_in_typed_files_types.Add(_short_type, _short_type); - writable_in_typed_files_types.Add(_ushort_type, _ushort_type); - writable_in_typed_files_types.Add(_integer_type, _integer_type); - writable_in_typed_files_types.Add(_uint_type, _uint_type); - writable_in_typed_files_types.Add(_int64_type, _int64_type); - writable_in_typed_files_types.Add(_uint64_type, _uint64_type); - writable_in_typed_files_types.Add(_double_type, _double_type); - writable_in_typed_files_types.Add(_char_type, _char_type); - writable_in_typed_files_types.Add(_float_type, _float_type); + writable_in_typed_files_types.Add(_bool_type); + writable_in_typed_files_types.Add(_byte_type); + writable_in_typed_files_types.Add(_sbyte_type); + writable_in_typed_files_types.Add(_short_type); + writable_in_typed_files_types.Add(_ushort_type); + writable_in_typed_files_types.Add(_integer_type); + writable_in_typed_files_types.Add(_uint_type); + writable_in_typed_files_types.Add(_int64_type); + writable_in_typed_files_types.Add(_uint64_type); + writable_in_typed_files_types.Add(_double_type); + writable_in_typed_files_types.Add(_char_type); + writable_in_typed_files_types.Add(_float_type); foreach (type_node tn in wait_add_ref_list) init_reference_type(tn); @@ -2120,7 +2119,7 @@ namespace PascalABCCompiler.SystemLibrary public static bool CanUseThisTypeForTypedFiles(type_node tn) { - return writable_in_typed_files_types[tn] != null; + return writable_in_typed_files_types.Contains(tn); } private static void add_std_convertions() diff --git a/TreeConverter/TreeConversion/syntax_tree_visitor.cs b/TreeConverter/TreeConversion/syntax_tree_visitor.cs index 0227063ea..101a0e59e 100644 --- a/TreeConverter/TreeConversion/syntax_tree_visitor.cs +++ b/TreeConverter/TreeConversion/syntax_tree_visitor.cs @@ -12761,7 +12761,7 @@ namespace PascalABCCompiler.TreeConverter { if (td.attributes != null) { - Hashtable ht = new Hashtable(); + var ht = new HashSet(); foreach (SyntaxTree.simple_attribute_list sal in td.attributes.attributes) for (int j = 0; j < sal.attributes.Count; j++) { @@ -12784,7 +12784,7 @@ namespace PascalABCCompiler.TreeConverter AddError(get_location(attr), "DUPLICATE_ATTRIBUTE_{0}_APPLICATION", tn.name); } else - ht[tn] = tn; + ht.Add(tn); SemanticTree.attribute_qualifier_kind qualifier = SemanticTree.attribute_qualifier_kind.none_kind; if (attr.qualifier != null) if (j == 0) @@ -12928,7 +12928,7 @@ namespace PascalABCCompiler.TreeConverter { if (cun.attributes != null) { - Hashtable ht = new Hashtable(); + var ht = new HashSet(); foreach (SyntaxTree.simple_attribute_list sal in un.attributes.attributes) for (int j = 0; j < sal.attributes.Count; j++) { @@ -12951,7 +12951,7 @@ namespace PascalABCCompiler.TreeConverter AddError(get_location(attr), "DUPLICATE_ATTRIBUTE_{0}_APPLICATION", tn.name); } else - ht[tn] = tn; + ht.Add(tn); SemanticTree.attribute_qualifier_kind qualifier = SemanticTree.attribute_qualifier_kind.none_kind; if (attr.qualifier != null) if (j == 0) @@ -13258,7 +13258,7 @@ namespace PascalABCCompiler.TreeConverter //Добавляет ограничители для параметра шаблона private void add_generic_eliminations(common_type_node param, List specificators) { - Hashtable used_interfs = new Hashtable(); + var used_interfs = new HashSet(); // System.Enum это класс, но его наследники - записи // Если указать "where T: Enum, record" - станет нельзя применять T=Enum, с ошибкой что Enum не запись // Но чтобы в секции where override метода можно было указать record без System.Enum @@ -13320,13 +13320,13 @@ namespace PascalABCCompiler.TreeConverter // param.is_class = true; if (spec_type.IsInterface) { - if (used_interfs[spec_type] != null) + if (used_interfs.Contains(spec_type)) { AddError(get_location(specificators[i]), "INTERFACE_{0}_ALREADY_ADDED_TO_IMPLEMENTING_LIST", spec_type.PrintableName); } else { - used_interfs.Add(spec_type, spec_type); + used_interfs.Add(spec_type); } //Добавляем интерфейс type_table.AddInterface(param, spec_type, get_location(specificators[i])); @@ -13358,7 +13358,7 @@ namespace PascalABCCompiler.TreeConverter { foreach (type_node tn in spec_type.ImplementingInterfaces) { - used_interfs.Add(tn, tn); + used_interfs.Add(tn); type_table.AddInterface(param, tn, get_location(specificators[i])); } } diff --git a/TreeConverter/TreeRealization/programs.cs b/TreeConverter/TreeRealization/programs.cs index dab87c685..1c2c05d6f 100644 --- a/TreeConverter/TreeRealization/programs.cs +++ b/TreeConverter/TreeRealization/programs.cs @@ -100,8 +100,8 @@ namespace PascalABCCompiler.TreeRealization common_namespace_node[] ret = new common_namespace_node[namespaces.Count]; namespaces.Values.CopyTo(ret, 0); return ret;*/ - System.Collections.Hashtable ht = new System.Collections.Hashtable(); - System.Collections.ArrayList al = new System.Collections.ArrayList(); + var ht = new Dictionary(); + var al = new List(); common_namespace_node main_cnn = null; foreach (common_unit_node un in units) { @@ -109,7 +109,7 @@ namespace PascalABCCompiler.TreeRealization { //if (cnn.namespace_full_name != "") //{ - if (ht[cnn.namespace_name] == null) + if (!ht.ContainsKey(cnn.namespace_name)) { al.Add(cnn); ht[cnn.namespace_name] = cnn; } @@ -160,15 +160,15 @@ namespace PascalABCCompiler.TreeRealization if (_namespaces == null) { _namespaces = get_units_namespaces(_units); - System.Collections.Hashtable ns_ht = new System.Collections.Hashtable(); + var ns_ht = new HashSet(); foreach (common_unit_node un in units) { foreach (string s in un.used_namespaces) { - if (!ns_ht.ContainsKey(s)) + if (!ns_ht.Contains(s)) { this._used_namespaces.Add(s); - ns_ht.Add(s,s); + ns_ht.Add(s); } } }