From d940541c65e2c60289246e32f64c278726107dae Mon Sep 17 00:00:00 2001 From: samuraiGH <87191377+samuraiGH@users.noreply.github.com> Date: Sat, 13 Jun 2026 09:40:55 +0300 Subject: [PATCH] refactoring (#3428) --- Compiler/Compiler.cs | 4 +- TreeConverter/NetWrappers/NetHelper.cs | 61 +++++++++---------- TreeConverter/OpenMP/OpenMP.cs | 6 +- .../InitializationDataForConverting.cs | 6 +- .../TreeConversion/compilation_context.cs | 15 +++-- .../convertion_data_and_alghoritms.cs | 4 +- .../TreeConversion/syntax_tree_visitor.cs | 6 +- TreeConverter/TreeRealization/generics.cs | 19 ++++-- 8 files changed, 64 insertions(+), 57 deletions(-) diff --git a/Compiler/Compiler.cs b/Compiler/Compiler.cs index c05175aab..f6a61b9ca 100644 --- a/Compiler/Compiler.cs +++ b/Compiler/Compiler.cs @@ -566,7 +566,7 @@ namespace PascalABCCompiler } } - private Hashtable BadNodesInSyntaxTree = new Hashtable(); + private HashSet BadNodesInSyntaxTree = new HashSet(); program_node semanticTree = null; public SemanticTree.IProgramNode SemanticTree @@ -3505,7 +3505,7 @@ namespace PascalABCCompiler currentUnit.syntax_error = errorsList[0] as SyntaxError; foreach (Error er in errorsList) if (er is SyntaxError && (er as SyntaxError).bad_node != null) - BadNodesInSyntaxTree[(er as SyntaxError).bad_node] = er; + BadNodesInSyntaxTree.Add((er as SyntaxError).bad_node); } } diff --git a/TreeConverter/NetWrappers/NetHelper.cs b/TreeConverter/NetWrappers/NetHelper.cs index 918440635..520e2adf8 100644 --- a/TreeConverter/NetWrappers/NetHelper.cs +++ b/TreeConverter/NetWrappers/NetHelper.cs @@ -2,7 +2,6 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; using PascalABCCompiler.SemanticTree; -using System.Collections; using System.Collections.Generic; using System.IO; using System.Reflection; @@ -286,9 +285,9 @@ namespace PascalABCCompiler.NetHelper public static class NetHelper { private static Dictionary namespaces; - private static Hashtable types; + private static Dictionary types; private static Dictionary type_search_cache; - private static Hashtable compiled_pascal_types; + private static Dictionary compiled_pascal_types; /*private static Hashtable methods; private static Hashtable properties; private static Hashtable fields;*/ @@ -298,7 +297,7 @@ namespace PascalABCCompiler.NetHelper private static Dictionary>> members; //private static Hashtable meth_nodes; private static Dictionary prop_nodes; - private static Hashtable field_nodes; + private static Dictionary field_nodes; private static Dictionary constr_nodes; private static HashSet stand_types; private static Dictionary> type_handles; @@ -487,7 +486,7 @@ namespace PascalABCCompiler.NetHelper TypeInfo ti = new TypeInfo(t, t.FullName); types[t.FullName] = ti; string short_name = t.Name; - object o2 = types[short_name]; + types.TryGetValue(short_name, out object o2); if (o2 == null) types[short_name] = ti; @@ -870,8 +869,8 @@ namespace PascalABCCompiler.NetHelper //interfaces = new Hashtable(); //\ssyy- //types = new Hashtable(1024, CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default); - types = new Hashtable(8096, StringComparer.CurrentCultureIgnoreCase); - compiled_pascal_types = new Hashtable(1024, StringComparer.CurrentCultureIgnoreCase); + types = new Dictionary(8096, StringComparer.CurrentCultureIgnoreCase); + compiled_pascal_types = new Dictionary(1024, StringComparer.CurrentCultureIgnoreCase); namespaces = new Dictionary(1024, StringComparer.CurrentCultureIgnoreCase); ass_name_cache = new Dictionary(1024, StringComparer.CurrentCultureIgnoreCase); assm_full_paths = new Dictionary(); @@ -884,7 +883,7 @@ namespace PascalABCCompiler.NetHelper assemblies = new HashSet(); //meth_nodes = new Hashtable(); prop_nodes = new Dictionary(); - field_nodes = new Hashtable(); + field_nodes = new Dictionary(); constr_nodes = new Dictionary(); stand_types = new HashSet(); type_handles = new Dictionary>(); @@ -1716,9 +1715,11 @@ namespace PascalABCCompiler.NetHelper public static compiled_variable_definition GetFieldNode(FieldInfo pi) { - compiled_variable_definition cpn = field_nodes[pi] as compiled_variable_definition; - if (cpn != null) + compiled_variable_definition cpn; + if ( field_nodes.TryGetValue(pi, out var existingNode) ) { + cpn = (compiled_variable_definition)existingNode; + if (cpn.type is compiled_type_node) return cpn; if (PascalABCCompiler.TreeConverter.compilation_context.instance != null && PascalABCCompiler.TreeConverter.compilation_context.instance.syntax_tree_visitor.CompiledUnit != null && PascalABCCompiler.TreeConverter.compilation_context.instance.converted_namespace != null) @@ -1840,12 +1841,13 @@ namespace PascalABCCompiler.NetHelper } public static compiled_class_constant_definition GetConstantFieldNode(FieldInfo fi) { - compiled_class_constant_definition cccd = field_nodes[fi] as compiled_class_constant_definition; - if (cccd != null) return cccd; + if ( field_nodes.TryGetValue(fi, out var existingNode) ) + return (compiled_class_constant_definition)existingNode; + constant_node cn = CreateConstantNode(fi.GetRawConstantValue()); if (cn == null) return null; - cccd = new compiled_class_constant_definition(fi, cn); + var cccd = new compiled_class_constant_definition(fi, cn); field_nodes[fi] = cccd; return cccd; } @@ -1865,12 +1867,10 @@ namespace PascalABCCompiler.NetHelper public static compiled_event GetEvent(EventInfo ei) { - compiled_event ce = field_nodes[ei] as compiled_event; - if (ce != null) - { - return ce; - } - ce = new compiled_event(ei); + if ( field_nodes.TryGetValue(ei, out var existingNode) ) + return (compiled_event)existingNode; + + var ce = new compiled_event(ei); field_nodes[ei] = ce; return ce; } @@ -1958,8 +1958,8 @@ namespace PascalABCCompiler.NetHelper if (ti != null) return ti.type; } - TypeInfo t = (TypeInfo)types[name]; - if (t != null) + + if (types.TryGetValue(name, out var temp) && temp is TypeInfo t) { fi = new FoundInfo(true, t); type_search_cache[name] = fi; @@ -1981,8 +1981,7 @@ namespace PascalABCCompiler.NetHelper public static template_class FindCompiledTemplateType(string name) { - object o = compiled_pascal_types[name]; - if (o != null) + if ( compiled_pascal_types.TryGetValue(name, out object o) ) { template_class tc = o as template_class; if (tc == null && PascalABCCompiler.TreeConverter.compilation_context.instance != null && PascalABCCompiler.TreeConverter.compilation_context.instance.syntax_tree_visitor.CompiledUnit != null && PascalABCCompiler.TreeConverter.compilation_context.instance.converted_namespace != null) @@ -2003,8 +2002,7 @@ namespace PascalABCCompiler.NetHelper public static type_node FindCompiledPascalType(string name) { - object o = compiled_pascal_types[name]; - if (o != null) + if ( compiled_pascal_types.TryGetValue(name, out object o) ) { type_node tn = o as type_node; if (tn == null && PascalABCCompiler.TreeConverter.compilation_context.instance != null && PascalABCCompiler.TreeConverter.compilation_context.instance.syntax_tree_visitor.CompiledUnit != null && PascalABCCompiler.TreeConverter.compilation_context.instance.converted_namespace != null) @@ -2062,7 +2060,7 @@ namespace PascalABCCompiler.NetHelper if (cur_used_assemblies.Contains(ti.type.Assembly)) return ti.type; } - object o = types[name]; + types.TryGetValue(name, out object o); if (o == null) { type_search_cache[name] = new FoundInfo(false); @@ -2151,7 +2149,7 @@ namespace PascalABCCompiler.NetHelper } for (int i = 0; i < _unar.Count; i++) { - o = types[_unar[i].namespace_name + "." + name]; + types.TryGetValue(_unar[i].namespace_name + "." + name, out o); if (o == null) continue; if (o is TypeInfo) @@ -2179,8 +2177,9 @@ namespace PascalABCCompiler.NetHelper public static Type FindTypeOrCreate(string name) { - TypeInfo ti = types[name] as TypeInfo; - if (ti != null /*&& cur_used_assemblies.ContainsKey(t.Assembly)*/) return ti.type; + types.TryGetValue(name, out var tempTI); + + if (tempTI is TypeInfo ti /*&& cur_used_assemblies.ContainsKey(t.Assembly)*/) return ti.type; //ivan added - runtime types adding Type t = Type.GetType(name, false, true); if (t == null) @@ -2222,8 +2221,8 @@ namespace PascalABCCompiler.NetHelper return typs; foreach (string s in types.Keys) { - TypeInfo ti = types[s] as TypeInfo; - if (ti != null) + types.TryGetValue(name, out var tempTI); + if (tempTI is TypeInfo ti) if (string.Compare(ti.type.Namespace,name, true) == 0) { lst.Add(ti.type); diff --git a/TreeConverter/OpenMP/OpenMP.cs b/TreeConverter/OpenMP/OpenMP.cs index fc9b51358..bde0e3a93 100644 --- a/TreeConverter/OpenMP/OpenMP.cs +++ b/TreeConverter/OpenMP/OpenMP.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 PascalABCCompiler.TreeRealization; -using System.Collections; namespace PascalABCCompiler.TreeConverter { @@ -104,7 +102,7 @@ namespace PascalABCCompiler.TreeConverter public List current_var_defs; public Stack block_stack; public statement_node_stack cycle_stack; - public Hashtable current_member_decls; + public Dictionary current_member_decls; public common_function_node_stack function_node_stack; public SymbolInfo current_last_created_function; public bool SemanticRulesThrowErrorWithoutSave; @@ -122,7 +120,7 @@ namespace PascalABCCompiler.TreeConverter cycle_stack = syntax_tree_visitor.context.CyclesStack; syntax_tree_visitor.context.CyclesStack = new statement_node_stack(); current_member_decls = syntax_tree_visitor.context.member_decls; - syntax_tree_visitor.context.member_decls = new Hashtable(); + syntax_tree_visitor.context.member_decls = new Dictionary(); function_node_stack = syntax_tree_visitor.context.func_stack; syntax_tree_visitor.context.func_stack = new common_function_node_stack(); current_last_created_function = syntax_tree_visitor.context.last_created_function; diff --git a/TreeConverter/TreeConversion/InitializationDataForConverting.cs b/TreeConverter/TreeConversion/InitializationDataForConverting.cs index 68e0a83f4..420c7bf00 100644 --- a/TreeConverter/TreeConversion/InitializationDataForConverting.cs +++ b/TreeConverter/TreeConversion/InitializationDataForConverting.cs @@ -12,7 +12,7 @@ namespace PascalABCCompiler.TreeConverter.TreeConversion { public readonly Errors.SyntaxError parserError; - public readonly Hashtable badNodes; + public readonly HashSet badNodes; public readonly TreeRealization.unit_node_list usedUnits; @@ -37,7 +37,7 @@ namespace PascalABCCompiler.TreeConverter.TreeConversion public InitializationDataForCompilingInterface( Errors.SyntaxError parserError, - Hashtable badNodes, + HashSet badNodes, TreeRealization.unit_node_list usedUnits, TreeRealization.using_namespace_list interfaceNamespaces, compilation_unit syntaxUnit, @@ -78,7 +78,7 @@ namespace PascalABCCompiler.TreeConverter.TreeConversion public InitializationDataForCompilingImplementation( Errors.SyntaxError parserError, - Hashtable badNodes, + HashSet badNodes, TreeRealization.unit_node_list usedUnits, TreeRealization.using_namespace_list interfaceNamespaces, TreeRealization.using_namespace_list implementationNamespaces, diff --git a/TreeConverter/TreeConversion/compilation_context.cs b/TreeConverter/TreeConversion/compilation_context.cs index ce8ae0663..4c70692af 100644 --- a/TreeConverter/TreeConversion/compilation_context.cs +++ b/TreeConverter/TreeConversion/compilation_context.cs @@ -4,7 +4,6 @@ using PascalABCCompiler.CoreUtils; using PascalABCCompiler.TreeRealization; using System; -using System.Collections; using System.Collections.Generic; using System.Linq; @@ -36,7 +35,7 @@ namespace PascalABCCompiler.TreeConverter public statement_node_stack cycles_stack; public int num_of_for_cycles; public SemanticTree.field_access_level _fal; - public System.Collections.Hashtable member_decls; + public Dictionary member_decls; public List types_predefined; public Stack type_stack; // Для вложенных типов public statement_list_stack stlist_stack; @@ -127,7 +126,7 @@ namespace PascalABCCompiler.TreeConverter _cycles_stack = new statement_node_stack(); _num_of_for_cycles = 0; _fal = SemanticTree.field_access_level.fal_private; - member_decls = new Hashtable(); + member_decls = new Dictionary(); _types_predefined = new List(); syntax_tree_visitor.ret.return_value(null); // _type_stack = new Stack(); @@ -250,7 +249,7 @@ namespace PascalABCCompiler.TreeConverter private static compilation_context _instance; private Dictionary> compiled_tc_cache = new Dictionary>(); - internal System.Collections.Hashtable member_decls = new System.Collections.Hashtable(); + internal Dictionary member_decls = new Dictionary(); internal bool namespace_converted = false; public compilation_context(convertion_data_and_alghoritms convertion_data_and_alghoritms, syntax_tree_visitor syntax_tree_visitor) @@ -430,12 +429,16 @@ namespace PascalABCCompiler.TreeConverter public common_function_node get_method_to_realize(SyntaxTree.declaration dc) { - return member_decls[dc] as common_function_node; + member_decls.TryGetValue(dc, out var result); + + return result as common_function_node; } public common_type_node get_type_to_realize(SyntaxTree.declaration dc) { - return member_decls[dc] as common_type_node; + member_decls.TryGetValue(dc, out var result); + + return result as common_type_node; } public void add_method_header(SyntaxTree.declaration dc, definition_node dn) diff --git a/TreeConverter/TreeConversion/convertion_data_and_alghoritms.cs b/TreeConverter/TreeConversion/convertion_data_and_alghoritms.cs index b8ae0ef8e..09fcf8d61 100644 --- a/TreeConverter/TreeConversion/convertion_data_and_alghoritms.cs +++ b/TreeConverter/TreeConversion/convertion_data_and_alghoritms.cs @@ -126,7 +126,7 @@ namespace PascalABCCompiler.TreeConverter _parser_error = value; } } - public System.Collections.Hashtable bad_nodes_in_syntax_tree; + public HashSet bad_nodes_in_syntax_tree; public SymbolTable.TreeConverterSymbolTable symbol_table { @@ -3330,7 +3330,7 @@ namespace PascalABCCompiler.TreeConverter { return; } - if (bad_nodes_in_syntax_tree[tn]!=null) + if ( bad_nodes_in_syntax_tree.Contains(tn) ) { AddError(new ParserError(_parser_error,_stv.get_location(tn))); } diff --git a/TreeConverter/TreeConversion/syntax_tree_visitor.cs b/TreeConverter/TreeConversion/syntax_tree_visitor.cs index a3fe1ca69..2394ee56a 100644 --- a/TreeConverter/TreeConversion/syntax_tree_visitor.cs +++ b/TreeConverter/TreeConversion/syntax_tree_visitor.cs @@ -717,7 +717,7 @@ namespace PascalABCCompiler.TreeConverter convertion_data_and_alghoritms.parser_error = value; } } - public System.Collections.Hashtable BadNodesInSyntaxTree + public HashSet BadNodesInSyntaxTree { get { @@ -19741,8 +19741,8 @@ namespace PascalABCCompiler.TreeConverter SemanticTree.field_access_level curr_fal = context.get_field_access_level(); List current_var_defs = context.var_defs; context.var_defs = new List(); - Hashtable current_member_decls = context.member_decls; - context.member_decls = new Hashtable(); + Dictionary current_member_decls = context.member_decls; + context.member_decls = new Dictionary(); //подменяем using-список using_namespace_list current_using_list = new using_namespace_list(); foreach (using_namespace un in using_list) diff --git a/TreeConverter/TreeRealization/generics.cs b/TreeConverter/TreeRealization/generics.cs index 29ad1b4e2..eedf9013a 100644 --- a/TreeConverter/TreeRealization/generics.cs +++ b/TreeConverter/TreeRealization/generics.cs @@ -232,14 +232,18 @@ namespace PascalABCCompiler.TreeRealization public static List all_function_instances = new List(); - public static Hashtable generic_instances = new Hashtable(); + public static Dictionary generic_instances = + new Dictionary(); public static syntax_tree_visitor visitor; public static List get_type_instances(type_node original_generic_type) { - List instances = generic_instances[original_generic_type] as List; - if (instances == null) + List instances; + + if ( generic_instances.TryGetValue(original_generic_type, out var temp) ) + instances = (List)temp; + else { instances = new List(); generic_instances.Add(original_generic_type, instances); @@ -249,14 +253,17 @@ namespace PascalABCCompiler.TreeRealization public static void remove_type_instances(type_node original_generic_type) { - if (generic_instances[original_generic_type] != null) + if (generic_instances.ContainsKey(original_generic_type)) generic_instances.Remove(original_generic_type); } public static List get_function_instances(function_node original_generic_function) { - List instances = generic_instances[original_generic_function] as List; - if (instances == null) + List instances; + + if ( generic_instances.TryGetValue(original_generic_function, out var temp) ) + instances = (List)temp; + else { instances = new List(); generic_instances.Add(original_generic_function, instances);