📄 typemanager.cs
字号:
//// typemanager.cs: C# type manager//// Author: Miguel de Icaza (miguel@gnu.org)// Ravi Pratap (ravi@ximian.com)//// Licensed under the terms of the GNU GPL//// (C) 2001 Ximian, Inc (http://www.ximian.com)//////// We will eventually remove the SIMPLE_SPEEDUP, and should never change // the behavior of the compilation. This can be removed if we rework// the code to get a list of namespaces available.//#define SIMPLE_SPEEDUPusing System;using System.IO;using System.Globalization;using System.Collections;using System.Reflection;using System.Reflection.Emit;using System.Text;using System.Text.RegularExpressions;using System.Runtime.CompilerServices;using System.Diagnostics;namespace Mono.CSharp {public class TypeManager { // // A list of core types that the compiler requires or uses // static public Type object_type; static public Type value_type; static public Type string_type; static public Type int32_type; static public Type uint32_type; static public Type int64_type; static public Type uint64_type; static public Type float_type; static public Type double_type; static public Type char_type; static public Type char_ptr_type; static public Type short_type; static public Type decimal_type; static public Type bool_type; static public Type sbyte_type; static public Type byte_type; static public Type ushort_type; static public Type enum_type; static public Type delegate_type; static public Type multicast_delegate_type; static public Type void_type; static public Type null_type; static public Type enumeration_type; static public Type array_type; static public Type runtime_handle_type; static public Type icloneable_type; static public Type type_type; static public Type ienumerator_type; static public Type ienumerable_type; static public Type idisposable_type; static public Type iconvertible_type; static public Type default_member_type; static public Type iasyncresult_type; static public Type asynccallback_type; static public Type intptr_type; static public Type monitor_type; static public Type runtime_field_handle_type; static public Type runtime_argument_handle_type; static public Type attribute_type; static public Type attribute_usage_type; static public Type decimal_constant_attribute_type; static public Type dllimport_type; static public Type unverifiable_code_type; static public Type methodimpl_attr_type; static public Type marshal_as_attr_type; static public Type param_array_type; static public Type void_ptr_type; static public Type indexer_name_type; static public Type exception_type; static public Type invalid_operation_exception_type; static public Type not_supported_exception_type; static public Type obsolete_attribute_type; static public Type conditional_attribute_type; static public Type in_attribute_type; static public Type out_attribute_type; static public Type anonymous_method_type; static public Type cls_compliant_attribute_type; static public Type typed_reference_type; static public Type arg_iterator_type; static public Type mbr_type; static public Type struct_layout_attribute_type; static public Type field_offset_attribute_type; static public Type security_attr_type; static public Type required_attr_type; static public Type guid_attr_type; static public Type assembly_culture_attribute_type; static public Type coclass_attr_type; static public Type comimport_attr_type; /// /// .NET 2.0 ///#if NET_2_0 static internal Type runtime_compatibility_attr_type; static internal Type compiler_generated_attr_type; static internal Type fixed_buffer_attr_type; static internal Type default_charset_type;#endif // // An empty array of types // static public Type [] NoTypes; static public TypeExpr [] NoTypeExprs; // // Expressions representing the internal types. Used during declaration // definition. // static public TypeExpr system_object_expr, system_string_expr; static public TypeExpr system_boolean_expr, system_decimal_expr; static public TypeExpr system_single_expr, system_double_expr; static public TypeExpr system_sbyte_expr, system_byte_expr; static public TypeExpr system_int16_expr, system_uint16_expr; static public TypeExpr system_int32_expr, system_uint32_expr; static public TypeExpr system_int64_expr, system_uint64_expr; static public TypeExpr system_char_expr, system_void_expr; static public TypeExpr system_asynccallback_expr; static public TypeExpr system_iasyncresult_expr; static public TypeExpr system_valuetype_expr; static public TypeExpr system_intptr_expr; // // This is only used when compiling corlib // static public Type system_int32_type; static public Type system_array_type; static public Type system_type_type; static public Type system_assemblybuilder_type; static public MethodInfo system_int_array_get_length; static public MethodInfo system_int_array_get_rank; static public MethodInfo system_object_array_clone; static public MethodInfo system_int_array_get_length_int; static public MethodInfo system_int_array_get_lower_bound_int; static public MethodInfo system_int_array_get_upper_bound_int; static public MethodInfo system_void_array_copyto_array_int; // // Internal, not really used outside // static Type runtime_helpers_type; // // These methods are called by code generated by the compiler // static public MethodInfo string_concat_string_string; static public MethodInfo string_concat_string_string_string; static public MethodInfo string_concat_string_string_string_string; static public MethodInfo string_concat_string_dot_dot_dot; static public MethodInfo string_concat_object_object; static public MethodInfo string_concat_object_object_object; static public MethodInfo string_concat_object_dot_dot_dot; static public MethodInfo string_isinterneted_string; static public MethodInfo system_type_get_type_from_handle; static public MethodInfo bool_movenext_void; static public MethodInfo ienumerable_getenumerator_void; static public MethodInfo void_reset_void; static public MethodInfo void_dispose_void; static public MethodInfo void_monitor_enter_object; static public MethodInfo void_monitor_exit_object; static public MethodInfo void_initializearray_array_fieldhandle; static public MethodInfo int_getlength_int; static public MethodInfo delegate_combine_delegate_delegate; static public MethodInfo delegate_remove_delegate_delegate; static public MethodInfo int_get_offset_to_string_data; static public MethodInfo int_array_get_length; static public MethodInfo int_array_get_rank; static public MethodInfo object_array_clone; static public MethodInfo int_array_get_length_int; static public MethodInfo int_array_get_lower_bound_int; static public MethodInfo int_array_get_upper_bound_int; static public MethodInfo void_array_copyto_array_int; static public PropertyInfo ienumerator_getcurrent; // // The attribute constructors. // static public ConstructorInfo object_ctor; static public ConstructorInfo cons_param_array_attribute; static public ConstructorInfo void_decimal_ctor_five_args; static public ConstructorInfo void_decimal_ctor_int_arg; static public ConstructorInfo unverifiable_code_ctor; static public ConstructorInfo default_member_ctor; static public ConstructorInfo decimal_constant_attribute_ctor; static internal ConstructorInfo struct_layout_attribute_ctor; static public ConstructorInfo field_offset_attribute_ctor; /// /// A new in C# 2.0 /// #if NET_2_0 static internal CustomAttributeBuilder compiler_generated_attr; static internal ConstructorInfo fixed_buffer_attr_ctor;#endif static PtrHashtable builder_to_declspace; static PtrHashtable builder_to_member_cache; // <remarks> // Tracks the interfaces implemented by typebuilders. We only // enter those who do implement or or more interfaces // </remarks> static PtrHashtable builder_to_ifaces; // <remarks> // Maps MethodBase.RuntimeTypeHandle to a Type array that contains // the arguments to the method // </remarks> static Hashtable method_arguments; // <remarks> // Maps PropertyBuilder to a Type array that contains // the arguments to the indexer // </remarks> static Hashtable indexer_arguments; // <remarks> // Maps a MethodBase to its ParameterData (either InternalParameters or ReflectionParameters) // <remarks> static Hashtable method_params; // <remarks> // A hash table from override methods to their base virtual method. // <remarks> static Hashtable method_overrides; // <remarks> // Keeps track of methods // </remarks> static Hashtable builder_to_method; // <remarks> // Contains all public types from referenced assemblies. // This member is used only if CLS Compliance verification is required. // </remarks> public static Hashtable AllClsTopLevelTypes; static Hashtable fieldbuilders_to_fields; static Hashtable fields; struct Signature { public string name; public Type [] args; } public static void CleanUp () { // Lets get everything clean so that we can collect before generating code builder_to_declspace = null; builder_to_member_cache = null; builder_to_ifaces = null; method_arguments = null; indexer_arguments = null; method_params = null; builder_to_method = null; fields = null; fieldbuilders_to_fields = null; events = null; priv_fields_events = null; type_hash = null; TypeHandle.CleanUp (); } /// <summary> /// A filter for Findmembers that uses the Signature object to /// extract objects /// </summary> static bool SignatureFilter (MemberInfo mi, object criteria) { Signature sig = (Signature) criteria; if (!(mi is MethodBase)) return false; if (mi.Name != sig.name) return false; int count = sig.args.Length; if (mi is MethodBuilder || mi is ConstructorBuilder){ Type [] candidate_args = GetArgumentTypes ((MethodBase) mi); if (candidate_args.Length != count) return false; for (int i = 0; i < count; i++) if (candidate_args [i] != sig.args [i]) return false; return true; } else { ParameterInfo [] pars = ((MethodBase) mi).GetParameters (); if (pars.Length != count) return false; for (int i = 0; i < count; i++) if (pars [i].ParameterType != sig.args [i]) return false; return true; } } // A delegate that points to the filter above. static MemberFilter signature_filter; // // These are expressions that represent some of the internal data types, used // elsewhere // static void InitExpressionTypes () { system_object_expr = new TypeLookupExpression ("System.Object"); system_string_expr = new TypeLookupExpression ("System.String"); system_boolean_expr = new TypeLookupExpression ("System.Boolean"); system_decimal_expr = new TypeLookupExpression ("System.Decimal"); system_single_expr = new TypeLookupExpression ("System.Single"); system_double_expr = new TypeLookupExpression ("System.Double"); system_sbyte_expr = new TypeLookupExpression ("System.SByte"); system_byte_expr = new TypeLookupExpression ("System.Byte"); system_int16_expr = new TypeLookupExpression ("System.Int16"); system_uint16_expr = new TypeLookupExpression ("System.UInt16"); system_int32_expr = new TypeLookupExpression ("System.Int32"); system_uint32_expr = new TypeLookupExpression ("System.UInt32"); system_int64_expr = new TypeLookupExpression ("System.Int64"); system_uint64_expr = new TypeLookupExpression ("System.UInt64"); system_char_expr = new TypeLookupExpression ("System.Char"); system_void_expr = new TypeLookupExpression ("System.Void"); system_asynccallback_expr = new TypeLookupExpression ("System.AsyncCallback"); system_iasyncresult_expr = new TypeLookupExpression ("System.IAsyncResult"); system_valuetype_expr = new TypeLookupExpression ("System.ValueType"); system_intptr_expr = new TypeLookupExpression ("System.IntPtr"); } static TypeManager () { Reset (); signature_filter = new MemberFilter (SignatureFilter); InitExpressionTypes (); } static public void Reset () { builder_to_declspace = new PtrHashtable (); builder_to_member_cache = new PtrHashtable (); builder_to_method = new PtrHashtable (); method_arguments = new PtrHashtable (); method_params = new PtrHashtable (); method_overrides = new PtrHashtable (); indexer_arguments = new PtrHashtable (); builder_to_ifaces = new PtrHashtable (); NoTypes = new Type [0]; NoTypeExprs = new TypeExpr [0]; fieldbuilders_to_fields = new Hashtable (); fields = new Hashtable (); type_hash = new DoubleHash (); } public static void AddUserType (DeclSpace ds) { builder_to_declspace.Add (ds.TypeBuilder, ds); } // // This entry point is used by types that we define under the covers // public static void RegisterBuilder (Type tb, Type [] ifaces) { if (ifaces != null) builder_to_ifaces [tb] = ifaces; } public static void AddMethod (MethodBase builder, IMethodData method) { builder_to_method.Add (builder, method); } public static IMethodData GetMethod (MethodBase builder) { return (IMethodData) builder_to_method [builder]; } /// <summary> /// Returns the DeclSpace whose Type is `t' or null if there is no /// DeclSpace for `t' (ie, the Type comes from a library) /// </summary> public static DeclSpace LookupDeclSpace (Type t) { return builder_to_declspace [t] as DeclSpace; } /// <summary> /// Returns the TypeContainer whose Type is `t' or null if there is no /// TypeContainer for `t' (ie, the Type comes from a library) /// </summary> public static TypeContainer LookupTypeContainer (Type t) { return builder_to_declspace [t] as TypeContainer; } public static MemberCache LookupMemberCache (Type t) { if (t is TypeBuilder) { IMemberContainer container = builder_to_declspace [t] as IMemberContainer; if (container != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -