/
class.cs
3901 lines (3207 loc) · 107 KB
/
class.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
//
// class.cs: Class and Struct handlers
//
// Authors: Miguel de Icaza (miguel@gnu.org)
// Martin Baulig (martin@ximian.com)
// Marek Safar (marek.safar@gmail.com)
//
// Dual licensed under the terms of the MIT X11 or GNU GPL
//
// Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
// Copyright 2004-2011 Novell, Inc
// Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
//
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Diagnostics;
using Mono.CompilerServices.SymbolWriter;
#if MOBILE
using XmlElement = System.Object;
#endif
#if STATIC
using SecurityType = System.Collections.Generic.List<IKVM.Reflection.Emit.CustomAttributeBuilder>;
using IKVM.Reflection;
using IKVM.Reflection.Emit;
#else
using SecurityType = System.Collections.Generic.Dictionary<System.Security.Permissions.SecurityAction, System.Security.PermissionSet>;
using System.Reflection;
using System.Reflection.Emit;
#endif
namespace Mono.CSharp
{
//
// General types container, used as a base class for all constructs which can hold types
//
public abstract class TypeContainer : MemberCore
{
public readonly MemberKind Kind;
protected List<TypeContainer> containers;
TypeDefinition main_container;
protected Dictionary<string, MemberCore> defined_names;
protected bool is_defined;
public int CounterAnonymousMethods { get; set; }
public int CounterAnonymousContainers { get; set; }
public int CounterSwitchTypes { get; set; }
protected TypeContainer (TypeContainer parent, MemberName name, Attributes attrs, MemberKind kind)
: base (parent, name, attrs)
{
this.Kind = kind;
defined_names = new Dictionary<string, MemberCore> ();
}
public override TypeSpec CurrentType {
get {
return null;
}
}
public Dictionary<string, MemberCore> DefinedNames {
get {
return defined_names;
}
}
public TypeDefinition PartialContainer {
get {
return main_container;
}
protected set {
main_container = value;
}
}
public IList<TypeContainer> Containers {
get {
return containers;
}
}
//
// Any unattached attributes during parsing get added here. Used
// by FULL_AST mode
//
public Attributes UnattachedAttributes {
get; set;
}
public void AddCompilerGeneratedClass (CompilerGeneratedContainer c)
{
AddTypeContainerMember (c);
}
public virtual void AddPartial (TypeDefinition next_part)
{
MemberCore mc;
(PartialContainer ?? this).defined_names.TryGetValue (next_part.MemberName.Basename, out mc);
AddPartial (next_part, mc as TypeDefinition);
}
protected void AddPartial (TypeDefinition next_part, TypeDefinition existing)
{
next_part.ModFlags |= Modifiers.PARTIAL;
if (existing == null) {
AddTypeContainer (next_part);
return;
}
if ((existing.ModFlags & Modifiers.PARTIAL) == 0) {
if (existing.Kind != next_part.Kind) {
AddTypeContainer (next_part);
} else {
Report.SymbolRelatedToPreviousError (next_part);
Error_MissingPartialModifier (existing);
}
return;
}
if (existing.Kind != next_part.Kind) {
Report.SymbolRelatedToPreviousError (existing);
Report.Error (261, next_part.Location,
"Partial declarations of `{0}' must be all classes, all structs or all interfaces",
next_part.GetSignatureForError ());
}
if ((existing.ModFlags & Modifiers.AccessibilityMask) != (next_part.ModFlags & Modifiers.AccessibilityMask) &&
((existing.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFIER) == 0 &&
(next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFIER) == 0)) {
Report.SymbolRelatedToPreviousError (existing);
Report.Error (262, next_part.Location,
"Partial declarations of `{0}' have conflicting accessibility modifiers",
next_part.GetSignatureForError ());
}
var tc_names = existing.CurrentTypeParameters;
if (tc_names != null) {
for (int i = 0; i < tc_names.Count; ++i) {
var tp = next_part.MemberName.TypeParameters[i];
if (tc_names[i].MemberName.Name != tp.MemberName.Name) {
Report.SymbolRelatedToPreviousError (existing.Location, "");
Report.Error (264, next_part.Location, "Partial declarations of `{0}' must have the same type parameter names in the same order",
next_part.GetSignatureForError ());
break;
}
if (tc_names[i].Variance != tp.Variance) {
Report.SymbolRelatedToPreviousError (existing.Location, "");
Report.Error (1067, next_part.Location, "Partial declarations of `{0}' must have the same type parameter variance modifiers",
next_part.GetSignatureForError ());
break;
}
}
}
if ((next_part.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFIER) != 0) {
existing.ModFlags |= next_part.ModFlags & ~(Modifiers.DEFAULT_ACCESS_MODIFIER | Modifiers.AccessibilityMask);
} else if ((existing.ModFlags & Modifiers.DEFAULT_ACCESS_MODIFIER) != 0) {
existing.ModFlags &= ~(Modifiers.DEFAULT_ACCESS_MODIFIER | Modifiers.AccessibilityMask);
existing.ModFlags |= next_part.ModFlags;
} else {
existing.ModFlags |= next_part.ModFlags;
}
existing.Definition.Modifiers = existing.ModFlags;
if (next_part.attributes != null) {
if (existing.attributes == null)
existing.attributes = next_part.attributes;
else
existing.attributes.AddAttributes (next_part.attributes.Attrs);
}
next_part.PartialContainer = existing;
existing.AddPartialPart (next_part);
AddTypeContainerMember (next_part);
}
public virtual void AddTypeContainer (TypeContainer tc)
{
AddTypeContainerMember (tc);
var tparams = tc.MemberName.TypeParameters;
if (tparams != null && tc.PartialContainer != null) {
var td = (TypeDefinition) tc;
for (int i = 0; i < tparams.Count; ++i) {
var tp = tparams[i];
if (tp.MemberName == null)
continue;
td.AddNameToContainer (tp, tp.Name);
}
}
}
protected virtual void AddTypeContainerMember (TypeContainer tc)
{
containers.Add (tc);
}
public virtual void CloseContainer ()
{
if (containers != null) {
foreach (TypeContainer tc in containers) {
tc.CloseContainer ();
}
}
}
public virtual void CreateMetadataName (StringBuilder sb)
{
if (Parent != null && Parent.MemberName != null)
Parent.CreateMetadataName (sb);
MemberName.CreateMetadataName (sb);
}
public virtual bool CreateContainer ()
{
if (containers != null) {
foreach (TypeContainer tc in containers) {
tc.CreateContainer ();
}
}
return true;
}
public override bool Define ()
{
if (containers != null) {
foreach (TypeContainer tc in containers) {
tc.Define ();
}
}
// Release cache used by parser only
if (Module.Evaluator == null) {
defined_names = null;
} else {
defined_names.Clear ();
}
return true;
}
public virtual void PrepareEmit ()
{
if (containers != null) {
foreach (var t in containers) {
try {
t.PrepareEmit ();
} catch (Exception e) {
if (MemberName == MemberName.Null)
throw;
throw new InternalErrorException (t, e);
}
}
}
}
public virtual bool DefineContainer ()
{
if (is_defined)
return true;
is_defined = true;
DoDefineContainer ();
if (containers != null) {
foreach (TypeContainer tc in containers) {
try {
tc.DefineContainer ();
} catch (Exception e) {
if (MemberName == MemberName.Null)
throw;
throw new InternalErrorException (tc, e);
}
}
if (PartialContainer != null && PartialContainer != this) {
containers = null;
}
}
return true;
}
public virtual void ExpandBaseInterfaces ()
{
if (containers != null) {
foreach (TypeContainer tc in containers) {
tc.ExpandBaseInterfaces ();
}
}
}
protected virtual void DefineNamespace ()
{
if (containers != null) {
foreach (var tc in containers) {
try {
tc.DefineNamespace ();
} catch (Exception e) {
throw new InternalErrorException (tc, e);
}
}
}
}
protected virtual void DoDefineContainer ()
{
}
public virtual void EmitContainer ()
{
if (containers != null) {
for (int i = 0; i < containers.Count; ++i)
containers[i].EmitContainer ();
}
}
protected void Error_MissingPartialModifier (MemberCore type)
{
Report.Error (260, type.Location,
"Missing partial modifier on declaration of type `{0}'. Another partial declaration of this type exists",
type.GetSignatureForError ());
}
public override string GetSignatureForDocumentation ()
{
if (Parent != null && Parent.MemberName != null)
return Parent.GetSignatureForDocumentation () + "." + MemberName.GetSignatureForDocumentation ();
return MemberName.GetSignatureForDocumentation ();
}
public override string GetSignatureForError ()
{
if (Parent != null && Parent.MemberName != null)
return Parent.GetSignatureForError () + "." + MemberName.GetSignatureForError ();
return MemberName.GetSignatureForError ();
}
public virtual string GetSignatureForMetadata ()
{
var sb = new StringBuilder ();
CreateMetadataName (sb);
return sb.ToString ();
}
public virtual void RemoveContainer (TypeContainer cont)
{
if (containers != null)
containers.Remove (cont);
var tc = Parent == Module ? Module : this;
tc.defined_names.Remove (cont.MemberName.Basename);
}
public virtual void VerifyMembers ()
{
if (containers != null) {
foreach (TypeContainer tc in containers)
tc.VerifyMembers ();
}
}
public override void WriteDebugSymbol (MonoSymbolFile file)
{
if (containers != null) {
foreach (TypeContainer tc in containers) {
tc.WriteDebugSymbol (file);
}
}
}
}
public abstract class TypeDefinition : TypeContainer, ITypeDefinition
{
//
// Different context is needed when resolving type container base
// types. Type names come from the parent scope but type parameter
// names from the container scope.
//
public struct BaseContext : IMemberContext
{
TypeContainer tc;
public BaseContext (TypeContainer tc)
{
this.tc = tc;
}
#region IMemberContext Members
public CompilerContext Compiler {
get { return tc.Compiler; }
}
public TypeSpec CurrentType {
get { return tc.PartialContainer.CurrentType; }
}
public TypeParameters CurrentTypeParameters {
get { return tc.PartialContainer.CurrentTypeParameters; }
}
public MemberCore CurrentMemberDefinition {
get { return tc; }
}
public bool IsObsolete {
get { return tc.IsObsolete; }
}
public bool IsUnsafe {
get { return tc.IsUnsafe; }
}
public bool IsStatic {
get { return tc.IsStatic; }
}
public ModuleContainer Module {
get { return tc.Module; }
}
public string GetSignatureForError ()
{
return tc.GetSignatureForError ();
}
public ExtensionMethodCandidates LookupExtensionMethod (string name, int arity)
{
return null;
}
public FullNamedExpression LookupNamespaceAlias (string name)
{
return tc.Parent.LookupNamespaceAlias (name);
}
public FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
{
if (arity == 0) {
var tp = CurrentTypeParameters;
if (tp != null) {
TypeParameter t = tp.Find (name);
if (t != null)
return new TypeParameterExpr (t, loc);
}
}
return tc.Parent.LookupNamespaceOrType (name, arity, mode, loc);
}
#endregion
}
[Flags]
enum CachedMethods
{
Equals = 1,
GetHashCode = 1 << 1,
HasStaticFieldInitializer = 1 << 2
}
readonly List<MemberCore> members;
// Holds a list of fields that have initializers
protected List<FieldInitializer> initialized_fields;
// Holds a list of static fields that have initializers
protected List<FieldInitializer> initialized_static_fields;
Dictionary<MethodSpec, Method> hoisted_base_call_proxies;
Dictionary<string, FullNamedExpression> Cache = new Dictionary<string, FullNamedExpression> ();
//
// Points to the first non-static field added to the container.
//
// This is an arbitrary choice. We are interested in looking at _some_ non-static field,
// and the first one's as good as any.
//
protected FieldBase first_nonstatic_field;
//
// This one is computed after we can distinguish interfaces
// from classes from the arraylist `type_bases'
//
protected TypeSpec base_type;
FullNamedExpression base_type_expr; // TODO: It's temporary variable
protected TypeSpec[] iface_exprs;
protected List<FullNamedExpression> type_bases;
// Partial parts for classes only
List<TypeDefinition> class_partial_parts;
TypeDefinition InTransit;
public TypeBuilder TypeBuilder;
GenericTypeParameterBuilder[] all_tp_builders;
//
// All recursive type parameters put together sharing same
// TypeParameter instances
//
TypeParameters all_type_parameters;
public const string DefaultIndexerName = "Item";
bool has_normal_indexers;
string indexer_name;
bool error;
bool members_defined;
bool members_defined_ok;
protected bool has_static_constructor;
private CachedMethods cached_method;
protected TypeSpec spec;
TypeSpec current_type;
public int DynamicSitesCounter;
public int AnonymousMethodsCounter;
public int MethodGroupsCounter;
static readonly string[] attribute_targets = new [] { "type" };
static readonly string[] attribute_targets_primary = new [] { "type", "method" };
/// <remarks>
/// The pending methods that need to be implemented
// (interfaces or abstract methods)
/// </remarks>
PendingImplementation pending;
protected TypeDefinition (TypeContainer parent, MemberName name, Attributes attrs, MemberKind kind)
: base (parent, name, attrs, kind)
{
PartialContainer = this;
members = new List<MemberCore> ();
}
#region Properties
public List<FullNamedExpression> BaseTypeExpressions {
get {
return type_bases;
}
}
public override TypeSpec CurrentType {
get {
if (current_type == null) {
if (IsGenericOrParentIsGeneric) {
//
// Switch to inflated version as it's used by all expressions
//
var targs = CurrentTypeParameters == null ? TypeSpec.EmptyTypes : CurrentTypeParameters.Types;
current_type = spec.MakeGenericType (this, targs);
} else {
current_type = spec;
}
}
return current_type;
}
}
public override TypeParameters CurrentTypeParameters {
get {
return PartialContainer.MemberName.TypeParameters;
}
}
int CurrentTypeParametersStartIndex {
get {
int total = all_tp_builders.Length;
if (CurrentTypeParameters != null) {
return total - CurrentTypeParameters.Count;
}
return total;
}
}
public virtual AssemblyDefinition DeclaringAssembly {
get {
return Module.DeclaringAssembly;
}
}
IAssemblyDefinition ITypeDefinition.DeclaringAssembly {
get {
return Module.DeclaringAssembly;
}
}
public TypeSpec Definition {
get {
return spec;
}
}
public bool HasMembersDefined {
get {
return members_defined;
}
}
public bool HasInstanceConstructor {
get {
return (caching_flags & Flags.HasInstanceConstructor) != 0;
}
set {
caching_flags |= Flags.HasInstanceConstructor;
}
}
// Indicated whether container has StructLayout attribute set Explicit
public bool HasExplicitLayout {
get { return (caching_flags & Flags.HasExplicitLayout) != 0; }
set { caching_flags |= Flags.HasExplicitLayout; }
}
public bool HasOperators {
get {
return (caching_flags & Flags.HasUserOperators) != 0;
}
set {
caching_flags |= Flags.HasUserOperators;
}
}
public bool HasStructLayout {
get { return (caching_flags & Flags.HasStructLayout) != 0; }
set { caching_flags |= Flags.HasStructLayout; }
}
public TypeSpec[] Interfaces {
get {
return iface_exprs;
}
}
public bool IsGenericOrParentIsGeneric {
get {
return all_type_parameters != null;
}
}
public bool IsTopLevel {
get {
return !(Parent is TypeDefinition);
}
}
public bool IsPartial {
get {
return (ModFlags & Modifiers.PARTIAL) != 0;
}
}
bool ITypeDefinition.IsTypeForwarder {
get {
return false;
}
}
bool ITypeDefinition.IsCyclicTypeForwarder {
get {
return false;
}
}
//
// Returns true for secondary partial containers
//
bool IsPartialPart {
get {
return PartialContainer != this;
}
}
public MemberCache MemberCache {
get {
return spec.MemberCache;
}
}
public List<MemberCore> Members {
get {
return members;
}
}
string ITypeDefinition.Namespace {
get {
var p = Parent;
while (p.Kind != MemberKind.Namespace)
p = p.Parent;
return p.MemberName == null ? null : p.GetSignatureForError ();
}
}
public ParametersCompiled PrimaryConstructorParameters { get; set; }
public Arguments PrimaryConstructorBaseArguments { get; set; }
public Location PrimaryConstructorBaseArgumentsStart { get; set; }
public TypeParameters TypeParametersAll {
get {
return all_type_parameters;
}
}
public override string[] ValidAttributeTargets {
get {
return PrimaryConstructorParameters != null ? attribute_targets_primary : attribute_targets;
}
}
#endregion
public override void Accept (StructuralVisitor visitor)
{
visitor.Visit (this);
}
public void AddMember (MemberCore symbol)
{
if (symbol.MemberName.ExplicitInterface != null) {
if (!(Kind == MemberKind.Class || Kind == MemberKind.Struct)) {
Report.Error (541, symbol.Location,
"`{0}': explicit interface declaration can only be declared in a class or struct",
symbol.GetSignatureForError ());
}
}
AddNameToContainer (symbol, symbol.MemberName.Name);
members.Add (symbol);
}
public override void AddTypeContainer (TypeContainer tc)
{
AddNameToContainer (tc, tc.MemberName.Basename);
base.AddTypeContainer (tc);
}
protected override void AddTypeContainerMember (TypeContainer tc)
{
members.Add (tc);
if (containers == null)
containers = new List<TypeContainer> ();
base.AddTypeContainerMember (tc);
}
//
// Adds the member to defined_names table. It tests for duplications and enclosing name conflicts
//
public virtual void AddNameToContainer (MemberCore symbol, string name)
{
if (((ModFlags | symbol.ModFlags) & Modifiers.COMPILER_GENERATED) != 0)
return;
MemberCore mc;
if (!PartialContainer.defined_names.TryGetValue (name, out mc)) {
PartialContainer.defined_names.Add (name, symbol);
return;
}
if (symbol.EnableOverloadChecks (mc))
return;
InterfaceMemberBase im = mc as InterfaceMemberBase;
if (im != null && im.IsExplicitImpl)
return;
Report.SymbolRelatedToPreviousError (mc);
if ((mc.ModFlags & Modifiers.PARTIAL) != 0 && (symbol is ClassOrStruct || symbol is Interface)) {
Error_MissingPartialModifier (symbol);
return;
}
if (symbol is TypeParameter) {
Report.Error (692, symbol.Location,
"Duplicate type parameter `{0}'", symbol.GetSignatureForError ());
} else {
Report.Error (102, symbol.Location,
"The type `{0}' already contains a definition for `{1}'",
GetSignatureForError (), name);
}
return;
}
public void AddConstructor (Constructor c)
{
AddConstructor (c, false);
}
public void AddConstructor (Constructor c, bool isDefault)
{
bool is_static = (c.ModFlags & Modifiers.STATIC) != 0;
if (!isDefault)
AddNameToContainer (c, is_static ? Constructor.TypeConstructorName : Constructor.ConstructorName);
if (is_static && c.ParameterInfo.IsEmpty) {
PartialContainer.has_static_constructor = true;
} else {
PartialContainer.HasInstanceConstructor = true;
}
members.Add (c);
}
public bool AddField (FieldBase field)
{
AddMember (field);
if ((field.ModFlags & Modifiers.STATIC) != 0)
return true;
var first_field = PartialContainer.first_nonstatic_field;
if (first_field == null) {
PartialContainer.first_nonstatic_field = field;
return true;
}
if (Kind == MemberKind.Struct && first_field.Parent != field.Parent) {
Report.SymbolRelatedToPreviousError (first_field.Parent);
Report.Warning (282, 3, field.Location,
"struct instance field `{0}' found in different declaration from instance field `{1}'",
field.GetSignatureForError (), first_field.GetSignatureForError ());
}
return true;
}
/// <summary>
/// Indexer has special handling in constrast to other AddXXX because the name can be driven by IndexerNameAttribute
/// </summary>
public void AddIndexer (Indexer i)
{
members.Add (i);
}
public void AddOperator (Operator op)
{
PartialContainer.HasOperators = true;
AddMember (op);
}
public void AddPartialPart (TypeDefinition part)
{
if (Kind != MemberKind.Class)
return;
if (class_partial_parts == null)
class_partial_parts = new List<TypeDefinition> ();
class_partial_parts.Add (part);
}
public override void ApplyAttributeBuilder (Attribute a, MethodSpec ctor, byte[] cdata, PredefinedAttributes pa)
{
if (a.Target == AttributeTargets.Method) {
foreach (var m in members) {
var c = m as Constructor;
if (c == null)
continue;
if (c.IsPrimaryConstructor) {
c.ApplyAttributeBuilder (a, ctor, cdata, pa);
return;
}
}
throw new InternalErrorException ();
}
if (has_normal_indexers && a.Type == pa.DefaultMember) {
Report.Error (646, a.Location, "Cannot specify the `DefaultMember' attribute on type containing an indexer");
return;
}
if (a.Type == pa.Required) {
Report.Error (1608, a.Location, "The RequiredAttribute attribute is not permitted on C# types");
return;
}
TypeBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), cdata);
}
public override AttributeTargets AttributeTargets {
get {
throw new NotSupportedException ();
}
}
public TypeSpec BaseType {
get {
return spec.BaseType;
}
}
protected virtual TypeAttributes TypeAttr {
get {
return ModifiersExtensions.TypeAttr (ModFlags, IsTopLevel);
}
}
public int TypeParametersCount {
get {
return MemberName.Arity;
}
}
TypeParameterSpec[] ITypeDefinition.TypeParameters {
get {
var ctp = PartialContainer.CurrentTypeParameters;
return ctp == null ? TypeParameterSpec.EmptyTypes : ctp.Types;
}
}
public string GetAttributeDefaultMember ()
{
return indexer_name ?? DefaultIndexerName;
}
public bool IsComImport {
get {
if (OptAttributes == null)
return false;
return OptAttributes.Contains (Module.PredefinedAttributes.ComImport);
}
}
public void RegisterFieldForInitialization (MemberCore field, FieldInitializer expression)
{
if (IsPartialPart)
PartialContainer.RegisterFieldForInitialization (field, expression);
if ((field.ModFlags & Modifiers.STATIC) != 0){
if (initialized_static_fields == null) {
HasStaticFieldInitializer = true;
initialized_static_fields = new List<FieldInitializer> (4);
}
initialized_static_fields.Add (expression);
} else {
if (Kind == MemberKind.Struct) {
if (Compiler.Settings.Version != LanguageVersion.Experimental) {
Report.Error (573, expression.Location, "'{0}': Structs cannot have instance property or field initializers",
GetSignatureForError ());
}
}
if (initialized_fields == null)
initialized_fields = new List<FieldInitializer> (4);
initialized_fields.Add (expression);
}
}
public void ResolveFieldInitializers (BlockContext ec)
{
Debug.Assert (!IsPartialPart);
if (ec.IsStatic) {
if (initialized_static_fields == null)
return;