-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
typemanager.cs
3625 lines (3053 loc) · 102 KB
/
typemanager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// typemanager.cs: C# type manager
//
// Author: Miguel de Icaza (miguel@gnu.org)
// Ravi Pratap (ravi@ximian.com)
// Marek Safar (marek.safar@seznam.cz)
//
// 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_SPEEDUP
using 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 {
#if GMCS_SOURCE
partial
#endif
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 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 uintptr_type;
static public Type monitor_type;
static public Type interlocked_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 extension_attribute_type;
static public Type default_parameter_value_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;
static internal Type internals_visible_attr_type;
static internal Type type_forwarder_attr_type;
#endif
//
// 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_isinterned_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 MethodInfo int_interlocked_compare_exchange;
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;
#if NET_2_0
/// C# 2.0
static internal CustomAttributeBuilder compiler_generated_attr;
static internal ConstructorInfo fixed_buffer_attr_ctor;
#endif
/// C# 3.0
static internal CustomAttributeBuilder extension_attribute_attr;
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 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 propertybuilder_to_property;
static Hashtable fields;
static Hashtable events;
#if GMCS_SOURCE
static PtrHashtable assembly_internals_vis_attrs;
#endif
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;
builder_to_type_param = null;
indexer_arguments = null;
method_params = null;
builder_to_method = null;
fields = null;
events = null;
type_hash = null;
propertybuilder_to_property = null;
#if GMCS_SOURCE
assembly_internals_vis_attrs = null;
#endif
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 = GetParameterData ((MethodBase) mi).Types;
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 ();
builder_to_type_param = new PtrHashtable ();
method_params = new PtrHashtable ();
method_overrides = new PtrHashtable ();
indexer_arguments = new PtrHashtable ();
builder_to_ifaces = new PtrHashtable ();
fieldbuilders_to_fields = new Hashtable ();
propertybuilder_to_property = new Hashtable ();
fields = new Hashtable ();
type_hash = new DoubleHash ();
#if GMCS_SOURCE
assembly_internals_vis_attrs = new PtrHashtable ();
#endif
// to uncover regressions
AllClsTopLevelTypes = null;
}
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);
method_params.Add (builder, method.ParameterInfo);
}
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)
return container.MemberCache;
}
#if GMCS_SOURCE
if (t is GenericTypeParameterBuilder) {
IMemberContainer container = builder_to_type_param [t] as IMemberContainer;
if (container != null)
return container.MemberCache;
}
#endif
return TypeHandle.GetMemberCache (t);
}
public static MemberCache LookupBaseInterfacesCache (Type t)
{
Type [] ifaces = GetInterfaces (t);
if (ifaces != null && ifaces.Length == 1)
return LookupMemberCache (ifaces [0]);
// TODO: the builder_to_member_cache should be indexed by 'ifaces', not 't'
MemberCache cache = builder_to_member_cache [t] as MemberCache;
if (cache != null)
return cache;
cache = new MemberCache (ifaces);
builder_to_member_cache.Add (t, cache);
return cache;
}
public static TypeContainer LookupInterface (Type t)
{
TypeContainer tc = (TypeContainer) builder_to_declspace [t];
if ((tc == null) || (tc.Kind != Kind.Interface))
return null;
return tc;
}
public static Delegate LookupDelegate (Type t)
{
return builder_to_declspace [t] as Delegate;
}
public static Class LookupClass (Type t)
{
return (Class) builder_to_declspace [t];
}
//
// We use this hash for multiple kinds of constructed types:
//
// (T, "&") Given T, get T &
// (T, "*") Given T, get T *
// (T, "[]") Given T and a array dimension, get T []
// (T, X) Given a type T and a simple name X, get the type T+X
//
// Accessibility tests, if necessary, should be done by the user
//
static DoubleHash type_hash = new DoubleHash ();
//
// Gets the reference to T version of the Type (T&)
//
public static Type GetReferenceType (Type t)
{
#if GMCS_SOURCE
return t.MakeByRefType ();
#else
return GetConstructedType (t, "&");
#endif
}
//
// Gets the pointer to T version of the Type (T*)
//
public static Type GetPointerType (Type t)
{
return GetConstructedType (t, "*");
}
public static Type GetConstructedType (Type t, string dim)
{
object ret = null;
if (type_hash.Lookup (t, dim, out ret))
return (Type) ret;
ret = t.Module.GetType (t.ToString () + dim);
if (ret != null) {
type_hash.Insert (t, dim, ret);
return (Type) ret;
}
if (dim == "&") {
ret = GetReferenceType (t);
type_hash.Insert (t, dim, ret);
return (Type) ret;
}
#if GMCS_SOURCE
if (t.IsGenericParameter || t.IsGenericType) {
int pos = 0;
Type result = t;
while ((pos < dim.Length) && (dim [pos] == '[')) {
pos++;
if (dim [pos] == ']') {
result = result.MakeArrayType ();
pos++;
if (pos < dim.Length)
continue;
type_hash.Insert (t, dim, result);
return result;
}
int rank = 0;
while (dim [pos] == ',') {
pos++; rank++;
}
if ((dim [pos] != ']') || (pos != dim.Length-1))
break;
result = result.MakeArrayType (rank + 1);
type_hash.Insert (t, dim, result);
return result;
}
}
#endif
type_hash.Insert (t, dim, null);
return null;
}
public static Type GetNestedType (Type t, string name)
{
object ret = null;
if (!type_hash.Lookup (t, name, out ret)) {
ret = t.GetNestedType (name,
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
type_hash.Insert (t, name, ret);
}
return (Type) ret;
}
/// <summary>
/// Fills static table with exported types from all referenced assemblies.
/// This information is required for CLS Compliance tests.
/// </summary>
public static void LoadAllImportedTypes ()
{
AllClsTopLevelTypes = new Hashtable (1500);
foreach (Assembly a in RootNamespace.Global.Assemblies) {
foreach (Type t in a.GetExportedTypes ()) {
AllClsTopLevelTypes [t.FullName.ToLower (System.Globalization.CultureInfo.InvariantCulture)] = null;
}
}
}
public static bool NamespaceClash (string name, Location loc)
{
if (! RootNamespace.Global.IsNamespace (name))
return false;
Report.Error (519, loc, String.Format ("`{0}' clashes with a predefined namespace", name));
return true;
}
/// <summary>
/// Returns the C# name of a type if possible, or the full type name otherwise
/// </summary>
static public string CSharpName (Type t)
{
if (t == typeof(NullType))
return "null";
string name = GetFullName (t);
return Regex.Replace (name,
@"^System\." +
@"(Int32|UInt32|Int16|UInt16|Int64|UInt64|" +
@"Single|Double|Char|Decimal|Byte|SByte|Object|" +
@"Boolean|String|Void|Null)" +
@"(\W+|\b)",
new MatchEvaluator (CSharpNameMatch)).Replace ('+', '.');
}
static public string CSharpName (Type[] types)
{
StringBuilder sb = new StringBuilder ();
foreach (Type t in types) {
sb.Append (CSharpName (t));
sb.Append (',');
}
sb.Remove (sb.Length - 1, 1);
return sb.ToString ();
}
static String CSharpNameMatch (Match match)
{
string s = match.Groups [1].Captures [0].Value;
return s.ToLower ().
Replace ("int32", "int").
Replace ("uint32", "uint").
Replace ("int16", "short").
Replace ("uint16", "ushort").
Replace ("int64", "long").
Replace ("uint64", "ulong").
Replace ("single", "float").
Replace ("boolean", "bool")
+ match.Groups [2].Captures [0].Value;
}
// Used for error reporting to show symbolic name instead of underlying value
public static string CSharpEnumValue (Type t, object value)
{
t = DropGenericTypeArguments (t);
Enum e = LookupDeclSpace (t) as Enum;
if (e == null)
return System.Enum.GetName (t, value);
return e.GetDefinition (value).GetSignatureForError ();
}
/// <summary>
/// Returns the signature of the method with full namespace classification
/// </summary>
static public string GetFullNameSignature (MemberInfo mi)
{
PropertyInfo pi = mi as PropertyInfo;
if (pi != null) {
MethodBase pmi = pi.GetGetMethod (true);
if (pmi == null)
pmi = pi.GetSetMethod (true);
if (GetParameterData (pmi).Count > 0)
mi = pmi;
}
return (mi is MethodBase)
? CSharpSignature (mi as MethodBase)
: CSharpName (mi.DeclaringType) + '.' + mi.Name;
}
#if GMCS_SOURCE
private static int GetFullName (Type t, StringBuilder sb)
{
int pos = 0;
if (!t.IsGenericType) {
sb.Append (t.FullName);
return 0;
}
if (t.DeclaringType != null) {
pos = GetFullName (t.DeclaringType, sb);
sb.Append ('.');
} else if (t.Namespace != null && t.Namespace.Length != 0) {
sb.Append (t.Namespace);
sb.Append ('.');
}
sb.Append (RemoveGenericArity (t.Name));
Type[] this_args = GetTypeArguments (t);
if (this_args.Length < pos)
throw new InternalErrorException (
"Enclosing class " + t.DeclaringType + " has more type arguments than " + t);
if (this_args.Length == pos)
return pos;
sb.Append ('<');
for (;;) {
sb.Append (CSharpName (this_args [pos++]));
if (pos == this_args.Length)
break;
sb.Append (',');
}
sb.Append ('>');
return pos;
}
static string GetFullName (Type t)
{
if (t.IsArray) {
string dimension = t.Name.Substring (t.Name.LastIndexOf ('['));
return GetFullName (t.GetElementType ()) + dimension;
}
if (IsNullableType (t) && !t.IsGenericTypeDefinition) {
t = GetTypeArguments (t)[0];
return CSharpName (t) + "?";
}
if (t.IsGenericParameter)
return t.Name;
if (!t.IsGenericType)
return t.FullName;
StringBuilder sb = new StringBuilder ();
int pos = GetFullName (t, sb);
if (pos <= 0)
throw new InternalErrorException ("Generic Type " + t + " doesn't have type arguments");
return sb.ToString ();
}
#else
public static string GetFullName (Type t)
{
return t.FullName;
}
#endif
public static string RemoveGenericArity (string from)
{
int i = from.IndexOf ('`');
if (i > 0)
return from.Substring (0, i);
return from;
}
/// <summary>
/// When we need to report accessors as well
/// </summary>
static public string CSharpSignature (MethodBase mb)
{
return CSharpSignature (mb, false);
}
/// <summary>
/// Returns the signature of the method
/// </summary>
static public string CSharpSignature (MethodBase mb, bool show_accessor)
{
StringBuilder sig = new StringBuilder (CSharpName (mb.DeclaringType));
sig.Append ('.');
ParameterData iparams = GetParameterData (mb);
string parameters = iparams.GetSignatureForError ();
int accessor_end = 0;
if (!mb.IsConstructor && TypeManager.IsSpecialMethod (mb)) {
Operator.OpType ot = Operator.GetOperatorType (mb.Name);
if (ot != Operator.OpType.TOP) {
sig.Append ("operator ");
sig.Append (Operator.GetName (ot));
sig.Append (parameters);
return sig.ToString ();
}
bool is_getter = mb.Name.StartsWith ("get_");
bool is_setter = mb.Name.StartsWith ("set_");
if (is_getter || is_setter || mb.Name.StartsWith ("add_")) {
accessor_end = 3;
} else if (mb.Name.StartsWith ("remove_")) {
accessor_end = 6;
}
// Is indexer
if (iparams.Count > (is_getter ? 0 : 1)) {
sig.Append ("this[");
if (is_getter)
sig.Append (parameters.Substring (1, parameters.Length - 2));
else
sig.Append (parameters.Substring (1, parameters.LastIndexOf (',') - 1));
sig.Append (']');
} else {
sig.Append (mb.Name.Substring (accessor_end + 1));
}
} else {
if (mb.Name == ".ctor")
sig.Append (RemoveGenericArity (mb.DeclaringType.Name));
else {
sig.Append (mb.Name);
#if GMCS_SOURCE
if (TypeManager.IsGenericMethod (mb)) {
Type[] args = mb.GetGenericArguments ();
sig.Append ('<');
for (int i = 0; i < args.Length; i++) {
if (i > 0)
sig.Append (',');
sig.Append (args [i].Name);
}
sig.Append ('>');
}
#endif
}
sig.Append (parameters);
}
if (show_accessor && accessor_end > 0) {
sig.Append ('.');
sig.Append (mb.Name.Substring (0, accessor_end));
}
return sig.ToString ();
}
public static string GetMethodName (MethodInfo m)
{
#if GMCS_SOURCE
if (!IsGenericMethodDefinition (m) && !IsGenericMethod (m))
return m.Name;
return MemberName.MakeName (m.Name, m.GetGenericArguments ().Length);
#else
return m.Name;
#endif
}
static public string CSharpSignature (EventInfo ei)
{
return CSharpName (ei.DeclaringType) + "." + ei.Name;
}
/// <summary>
/// Looks up a type, and aborts if it is not found. This is used
/// by types required by the compiler
/// </summary>
static Type CoreLookupType (string namespaceName, string name)
{
return CoreLookupType (namespaceName, name, false);
}
static Type CoreLookupType (string ns_name, string name, bool mayFail)
{
Namespace ns = RootNamespace.Global.GetNamespace (ns_name, true);
FullNamedExpression fne = ns.Lookup (RootContext.ToplevelTypes, name, Location.Null);
Type t = fne == null ? null : fne.Type;
if (t == null) {
if (!mayFail)
Report.Error (518, "The predefined type `" + name + "' is not defined or imported");
return null;
}
AttributeTester.RegisterNonObsoleteType (t);
return t;
}
/// <summary>
/// Returns the MethodInfo for a method named `name' defined
/// in type `t' which takes arguments of types `args'
/// </summary>
static MethodInfo GetMethod (Type t, string name, Type [] args, bool is_private, bool report_errors)
{
MemberList list;
Signature sig;
BindingFlags flags = instance_and_static | BindingFlags.Public;
sig.name = name;
sig.args = args;
if (is_private)
flags |= BindingFlags.NonPublic;
list = FindMembers (t, MemberTypes.Method, flags, signature_filter, sig);
if (list.Count == 0) {
if (report_errors)
Report.Error (-19, "Can not find the core function `" + name + "'");
return null;
}
MethodInfo mi = list [0] as MethodInfo;
if (mi == null) {
if (report_errors)
Report.Error (-19, "Can not find the core function `" + name + "'");
return null;
}
return mi;
}
static MethodInfo GetMethod (Type t, string name, Type [] args, bool report_errors)
{
return GetMethod (t, name, args, false, report_errors);
}
public static MethodInfo GetMethod (Type t, string name, Type [] args)
{
return GetMethod (t, name, args, true);
}
/// <summary>
/// Returns the PropertyInfo for a property named `name' defined
/// in type `t'
/// </summary>
public static PropertyInfo GetProperty (Type t, string name)
{
MemberList list = FindMembers (t, MemberTypes.Property, BindingFlags.Public |
BindingFlags.Instance, Type.FilterName, name);
if (list.Count == 0) {
Report.Error (-19, "Can not find the core property `" + name + "'");
return null;
}
PropertyInfo pi = list [0] as PropertyInfo;
if (pi == null) {
Report.Error (-19, "Can not find the core function `" + name + "'");
return null;
}
return pi;
}
/// <summary>
/// Returns the ConstructorInfo for "args"
/// </summary>
public static ConstructorInfo GetConstructor (Type t, Type [] args)
{
MemberList list;
Signature sig;
sig.name = ".ctor";
sig.args = args;
if (t == null)
throw new InternalErrorException ("Core types haven't been initialized yet?");
list = FindMembers (t, MemberTypes.Constructor,
instance_and_static | BindingFlags.Public | BindingFlags.DeclaredOnly,
signature_filter, sig);
if (list.Count == 0){
Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
return null;
}
ConstructorInfo ci = list [0] as ConstructorInfo;
if (ci == null){
Report.Error (-19, "Can not find the core constructor for type `" + t.Name + "'");
return null;
}
return ci;
}
public static void InitEnumUnderlyingTypes ()
{
int32_type = CoreLookupType ("System", "Int32");
int64_type = CoreLookupType ("System", "Int64");
uint32_type = CoreLookupType ("System", "UInt32");
uint64_type = CoreLookupType ("System", "UInt64");
byte_type = CoreLookupType ("System", "Byte");
sbyte_type = CoreLookupType ("System", "SByte");
short_type = CoreLookupType ("System", "Int16");
ushort_type = CoreLookupType ("System", "UInt16");
ienumerator_type = CoreLookupType ("System.Collections", "IEnumerator");
ienumerable_type = CoreLookupType ("System.Collections", "IEnumerable");
idisposable_type = CoreLookupType ("System", "IDisposable");
#if GMCS_SOURCE
InitGenericCoreTypes ();