File: decl.cs

package info (click to toggle)
nrefactory 5.3.0%2B20130718.73b6d0f-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 15,684 kB
  • ctags: 37,257
  • sloc: cs: 296,018; makefile: 24; ansic: 7; sh: 2
file content (1292 lines) | stat: -rw-r--r-- 33,850 bytes parent folder | download | duplicates (4)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
//
// decl.cs: Declaration base class for structs, classes, enums and interfaces.
//
// Author: Miguel de Icaza (miguel@gnu.org)
//         Marek Safar (marek.safar@seznam.cz)
//
// Dual licensed under the terms of the MIT X11 or GNU GPL
//
// Copyright 2001 Ximian, Inc (http://www.ximian.com)
// Copyright 2004-2008 Novell, Inc
// Copyright 2011 Xamarin Inc
//
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Mono.CompilerServices.SymbolWriter;

#if NET_2_1
using XmlElement = System.Object;
#else
using System.Xml;
#endif

#if STATIC
using IKVM.Reflection;
using IKVM.Reflection.Emit;
#else
using System.Reflection;
using System.Reflection.Emit;
#endif

namespace Mono.CSharp {

	//
	// Better name would be DottenName
	//
	[DebuggerDisplay ("{GetSignatureForError()}")]
	public class MemberName
	{
		public static readonly MemberName Null = new MemberName ("");

		public readonly string Name;
		public TypeParameters TypeParameters;
		public readonly FullNamedExpression ExplicitInterface;
		public readonly Location Location;

		public readonly MemberName Left;

		public MemberName (string name)
			: this (name, Location.Null)
		{ }

		public MemberName (string name, Location loc)
			: this (null, name, loc)
		{ }

		public MemberName (string name, TypeParameters tparams, Location loc)
		{
			this.Name = name;
			this.Location = loc;

			this.TypeParameters = tparams;
		}

		public MemberName (string name, TypeParameters tparams, FullNamedExpression explicitInterface, Location loc)
			: this (name, tparams, loc)
		{
			this.ExplicitInterface = explicitInterface;
		}

		public MemberName (MemberName left, string name, Location loc)
		{
			this.Name = name;
			this.Location = loc;
			this.Left = left;
		}

		public MemberName (MemberName left, string name, FullNamedExpression explicitInterface, Location loc)
			: this (left, name, loc)
		{
			this.ExplicitInterface = explicitInterface;
		}

		public MemberName (MemberName left, MemberName right)
		{
			this.Name = right.Name;
			this.Location = right.Location;
			this.TypeParameters = right.TypeParameters;
			this.Left = left;
		}

		public int Arity {
			get {
				return TypeParameters == null ? 0 : TypeParameters.Count;
			}
		}

		public bool IsGeneric {
			get {
				return TypeParameters != null;
			}
		}

		public string Basename {
			get {
				if (TypeParameters != null)
					return MakeName (Name, TypeParameters);
				return Name;
			}
		}

		public void CreateMetadataName (StringBuilder sb)
		{
			if (Left != null)
				Left.CreateMetadataName (sb);

			if (sb.Length != 0) {
				sb.Append (".");
			}

			sb.Append (Basename);
		}

		public string GetSignatureForDocumentation ()
		{
			var s = Basename;

			if (ExplicitInterface != null)
				s = ExplicitInterface.GetSignatureForError () + "." + s;

			if (Left == null)
				return s;

			return Left.GetSignatureForDocumentation () + "." + s;
		}

		public string GetSignatureForError ()
		{
			string s = TypeParameters == null ? null : "<" + TypeParameters.GetSignatureForError () + ">";
			s = Name + s;

			if (ExplicitInterface != null)
				s = ExplicitInterface.GetSignatureForError () + "." + s;

			if (Left == null)
				return s;

			return Left.GetSignatureForError () + "." + s;
		}

		public override bool Equals (object other)
		{
			return Equals (other as MemberName);
		}

		public bool Equals (MemberName other)
		{
			if (this == other)
				return true;
			if (other == null || Name != other.Name)
				return false;

			if ((TypeParameters != null) &&
			    (other.TypeParameters == null || TypeParameters.Count != other.TypeParameters.Count))
				return false;

			if ((TypeParameters == null) && (other.TypeParameters != null))
				return false;

			if (Left == null)
				return other.Left == null;

			return Left.Equals (other.Left);
		}

		public override int GetHashCode ()
		{
			int hash = Name.GetHashCode ();
			for (MemberName n = Left; n != null; n = n.Left)
				hash ^= n.Name.GetHashCode ();

			if (TypeParameters != null)
				hash ^= TypeParameters.Count << 5;

			return hash & 0x7FFFFFFF;
		}

		public static string MakeName (string name, TypeParameters args)
		{
			if (args == null)
				return name;

			return name + "`" + args.Count;
		}

		public static string MakeName (string name, int count)
		{
			return name + "`" + count;
		}
	}

	public class SimpleMemberName
	{
		public string Value;
		public Location Location;

		public SimpleMemberName (string name, Location loc)
		{
			this.Value = name;
			this.Location = loc;
		}
	}

	/// <summary>
	///   Base representation for members.  This is used to keep track
	///   of Name, Location and Modifier flags, and handling Attributes.
	/// </summary>
	[System.Diagnostics.DebuggerDisplay ("{GetSignatureForError()}")]
	public abstract class MemberCore : Attributable, IMemberContext, IMemberDefinition
	{
		string IMemberDefinition.Name {
			get {
				return member_name.Name;
			}
		}

                // Is not readonly because of IndexerName attribute
		private MemberName member_name;
		public MemberName MemberName {
			get { return member_name; }
		}

		/// <summary>
		///   Modifier flags that the user specified in the source code
		/// </summary>
		private Modifiers mod_flags;
		public Modifiers ModFlags {
			set {
				mod_flags = value;
				if ((value & Modifiers.COMPILER_GENERATED) != 0)
					caching_flags = Flags.IsUsed | Flags.IsAssigned;
			}
			get {
				return mod_flags;
			}
		}

		public virtual ModuleContainer Module {
			get {
				return Parent.Module;
			}
		}

		public /*readonly*/ TypeContainer Parent;

		/// <summary>
		///   Location where this declaration happens
		/// </summary>
		public Location Location {
			get { return member_name.Location; }
		}

		/// <summary>
		///   XML documentation comment
		/// </summary>
		protected string comment;

		/// <summary>
		///   Represents header string for documentation comment 
		///   for each member types.
		/// </summary>
		public abstract string DocCommentHeader { get; }

		[Flags]
		public enum Flags {
			Obsolete_Undetected = 1,		// Obsolete attribute has not been detected yet
			Obsolete = 1 << 1,			// Type has obsolete attribute
			ClsCompliance_Undetected = 1 << 2,	// CLS Compliance has not been detected yet
			ClsCompliant = 1 << 3,			// Type is CLS Compliant
			CloseTypeCreated = 1 << 4,		// Tracks whether we have Closed the type
			HasCompliantAttribute_Undetected = 1 << 5,	// Presence of CLSCompliantAttribute has not been detected
			HasClsCompliantAttribute = 1 << 6,			// Type has CLSCompliantAttribute
			ClsCompliantAttributeFalse = 1 << 7,			// Member has CLSCompliant(false)
			Excluded_Undetected = 1 << 8,		// Conditional attribute has not been detected yet
			Excluded = 1 << 9,					// Method is conditional
			MethodOverloadsExist = 1 << 10,		// Test for duplication must be performed
			IsUsed = 1 << 11,
			IsAssigned = 1 << 12,				// Field is assigned
			HasExplicitLayout	= 1 << 13,
			PartialDefinitionExists	= 1 << 14,	// Set when corresponding partial method definition exists
			HasStructLayout	= 1 << 15,			// Has StructLayoutAttribute
			HasInstanceConstructor = 1 << 16,
			HasUserOperators = 1 << 17,
			CanBeReused = 1 << 18,
			InterfacesExpanded = 1 << 19
		}

		/// <summary>
		///   MemberCore flags at first detected then cached
		/// </summary>
		internal Flags caching_flags;

		public MemberCore (TypeContainer parent, MemberName name, Attributes attrs)
		{
			this.Parent = parent;
			member_name = name;
			caching_flags = Flags.Obsolete_Undetected | Flags.ClsCompliance_Undetected | Flags.HasCompliantAttribute_Undetected | Flags.Excluded_Undetected;
			AddAttributes (attrs, this);
		}

		protected virtual void SetMemberName (MemberName new_name)
		{
			member_name = new_name;
		}

		public virtual void Accept (StructuralVisitor visitor)
		{
			visitor.Visit (this);
		}

		protected bool CheckAbstractAndExtern (bool has_block)
		{
			if (Parent.PartialContainer.Kind == MemberKind.Interface)
				return true;

			if (has_block) {
				if ((ModFlags & Modifiers.EXTERN) != 0) {
					Report.Error (179, Location, "`{0}' cannot declare a body because it is marked extern",
						GetSignatureForError ());
					return false;
				}

				if ((ModFlags & Modifiers.ABSTRACT) != 0) {
					Report.Error (500, Location, "`{0}' cannot declare a body because it is marked abstract",
						GetSignatureForError ());
					return false;
				}
			} else {
				if ((ModFlags & (Modifiers.ABSTRACT | Modifiers.EXTERN | Modifiers.PARTIAL)) == 0 && !(Parent is Delegate)) {
					if (Compiler.Settings.Version >= LanguageVersion.V_3) {
						Property.PropertyMethod pm = this as Property.PropertyMethod;
						if (pm is Indexer.GetIndexerMethod || pm is Indexer.SetIndexerMethod)
							pm = null;

						if (pm != null && pm.Property.AccessorSecond == null) {
							Report.Error (840, Location,
								"`{0}' must have a body because it is not marked abstract or extern. The property can be automatically implemented when you define both accessors",
								GetSignatureForError ());
							return false;
						}
					}

					Report.Error (501, Location, "`{0}' must have a body because it is not marked abstract, extern, or partial",
					              GetSignatureForError ());
					return false;
				}
			}

			return true;
		}

		protected void CheckProtectedModifier ()
		{
			if ((ModFlags & Modifiers.PROTECTED) == 0)
				return;

			if (Parent.PartialContainer.Kind == MemberKind.Struct) {
				Report.Error (666, Location, "`{0}': Structs cannot contain protected members",
					GetSignatureForError ());
				return;
			}

			if ((Parent.ModFlags & Modifiers.STATIC) != 0) {
				Report.Error (1057, Location, "`{0}': Static classes cannot contain protected members",
					GetSignatureForError ());
				return;
			}

			if ((Parent.ModFlags & Modifiers.SEALED) != 0 && (ModFlags & Modifiers.OVERRIDE) == 0 &&
				!(this is Destructor)) {
				Report.Warning (628, 4, Location, "`{0}': new protected member declared in sealed class",
					GetSignatureForError ());
				return;
			}
		}

		public abstract bool Define ();

		public virtual string DocComment {
			get {
				return comment;
			}
			set {
				comment = value;
			}
		}

		// 
		// Returns full member name for error message
		//
		public virtual string GetSignatureForError ()
		{
			var parent = Parent.GetSignatureForError ();
			if (parent == null)
				return member_name.GetSignatureForError ();

			return parent + "." + member_name.GetSignatureForError ();
		}

		/// <summary>
		/// Base Emit method. This is also entry point for CLS-Compliant verification.
		/// </summary>
		public virtual void Emit ()
		{
			if (!Compiler.Settings.VerifyClsCompliance)
				return;

			VerifyClsCompliance ();
		}

		public bool IsAvailableForReuse {
			get {
				return (caching_flags & Flags.CanBeReused) != 0;
			}
			set {
				caching_flags = value ? (caching_flags | Flags.CanBeReused) : (caching_flags & ~Flags.CanBeReused);
			}
		}

		public bool IsCompilerGenerated {
			get	{
				if ((mod_flags & Modifiers.COMPILER_GENERATED) != 0)
					return true;

				return Parent == null ? false : Parent.IsCompilerGenerated;
			}
		}

		public bool IsImported {
			get {
				return false;
			}
		}

		public virtual bool IsUsed {
			get {
				return (caching_flags & Flags.IsUsed) != 0;
			}
		}

		protected Report Report {
			get {
				return Compiler.Report;
			}
		}

		public void SetIsUsed ()
		{
			caching_flags |= Flags.IsUsed;
		}

		public void SetIsAssigned ()
		{
			caching_flags |= Flags.IsAssigned;
		}

		public virtual void SetConstraints (List<Constraints> constraints_list)
		{
			var tparams = member_name.TypeParameters;
			if (tparams == null) {
				Report.Error (80, Location, "Constraints are not allowed on non-generic declarations");
				return;
			}

			foreach (var c in constraints_list) {
				var tp = tparams.Find (c.TypeParameter.Value);
				if (tp == null) {
					Report.Error (699, c.Location, "`{0}': A constraint references nonexistent type parameter `{1}'",
						GetSignatureForError (), c.TypeParameter.Value);
					continue;
				}

				tp.Constraints = c;
			}
		}

		/// <summary>
		/// Returns instance of ObsoleteAttribute for this MemberCore
		/// </summary>
		public virtual ObsoleteAttribute GetAttributeObsolete ()
		{
			if ((caching_flags & (Flags.Obsolete_Undetected | Flags.Obsolete)) == 0)
				return null;

			caching_flags &= ~Flags.Obsolete_Undetected;

			if (OptAttributes == null)
				return null;

			Attribute obsolete_attr = OptAttributes.Search (Module.PredefinedAttributes.Obsolete);
			if (obsolete_attr == null)
				return null;

			caching_flags |= Flags.Obsolete;

			ObsoleteAttribute obsolete = obsolete_attr.GetObsoleteAttribute ();
			if (obsolete == null)
				return null;

			return obsolete;
		}

		/// <summary>
		/// Checks for ObsoleteAttribute presence. It's used for testing of all non-types elements
		/// </summary>
		public virtual void CheckObsoleteness (Location loc)
		{
			ObsoleteAttribute oa = GetAttributeObsolete ();
			if (oa != null)
				AttributeTester.Report_ObsoleteMessage (oa, GetSignatureForError (), loc, Report);
		}

		//
		// Checks whether the type P is as accessible as this member
		//
		public bool IsAccessibleAs (TypeSpec p)
		{
			//
			// if M is private, its accessibility is the same as this declspace.
			// we already know that P is accessible to T before this method, so we
			// may return true.
			//
			if ((mod_flags & Modifiers.PRIVATE) != 0)
				return true;

			while (TypeManager.HasElementType (p))
				p = TypeManager.GetElementType (p);

			if (p.IsGenericParameter)
				return true;

			for (TypeSpec p_parent; p != null; p = p_parent) {
				p_parent = p.DeclaringType;

				if (p.IsGeneric) {
					foreach (TypeSpec t in p.TypeArguments) {
						if (!IsAccessibleAs (t))
							return false;
					}
				}

				var pAccess = p.Modifiers & Modifiers.AccessibilityMask;
				if (pAccess == Modifiers.PUBLIC)
					continue;

				bool same_access_restrictions = false;
				for (MemberCore mc = this; !same_access_restrictions && mc != null && mc.Parent != null; mc = mc.Parent) {
					var al = mc.ModFlags & Modifiers.AccessibilityMask;
					switch (pAccess) {
					case Modifiers.INTERNAL:
						if (al == Modifiers.PRIVATE || al == Modifiers.INTERNAL)
							same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
						
						break;

					case Modifiers.PROTECTED:
						if (al == Modifiers.PROTECTED) {
							same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent);
							break;
						}

						if (al == Modifiers.PRIVATE) {
							//
							// When type is private and any of its parents derives from
							// protected type then the type is accessible
							//
							while (mc.Parent != null && mc.Parent.PartialContainer != null) {
								if (mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent))
									same_access_restrictions = true;
								mc = mc.Parent; 
							}
						}
						
						break;

					case Modifiers.PROTECTED | Modifiers.INTERNAL:
						if (al == Modifiers.INTERNAL)
							same_access_restrictions = p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
						else if (al == (Modifiers.PROTECTED | Modifiers.INTERNAL))
							same_access_restrictions = mc.Parent.PartialContainer.IsBaseTypeDefinition (p_parent) && p.MemberDefinition.IsInternalAsPublic (mc.Module.DeclaringAssembly);
						else
							goto case Modifiers.PROTECTED;

						break;

					case Modifiers.PRIVATE:
						//
						// Both are private and share same parent
						//
						if (al == Modifiers.PRIVATE) {
							var decl = mc.Parent;
							do {
								same_access_restrictions = decl.CurrentType.MemberDefinition == p_parent.MemberDefinition;
							} while (!same_access_restrictions && !decl.PartialContainer.IsTopLevel && (decl = decl.Parent) != null);
						}
						
						break;
						
					default:
						throw new InternalErrorException (al.ToString ());
					}
				}
				
				if (!same_access_restrictions)
					return false;
			}

			return true;
		}

		/// <summary>
		/// Analyze whether CLS-Compliant verification must be execute for this MemberCore.
		/// </summary>
		public override bool IsClsComplianceRequired ()
		{
			if ((caching_flags & Flags.ClsCompliance_Undetected) == 0)
				return (caching_flags & Flags.ClsCompliant) != 0;

			caching_flags &= ~Flags.ClsCompliance_Undetected;

			if (HasClsCompliantAttribute) {
				if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0)
					return false;

				caching_flags |= Flags.ClsCompliant;
				return true;
			}

			if (Parent.IsClsComplianceRequired ()) {
				caching_flags |= Flags.ClsCompliant;
				return true;
			}

			return false;
		}

		public virtual string[] ConditionalConditions ()
		{
			return null;
		}

		/// <summary>
		/// Returns true when MemberCore is exposed from assembly.
		/// </summary>
		public bool IsExposedFromAssembly ()
		{
			if ((ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
				return this is NamespaceContainer;
			
			var parentContainer = Parent.PartialContainer;
			while (parentContainer != null) {
				if ((parentContainer.ModFlags & (Modifiers.PUBLIC | Modifiers.PROTECTED)) == 0)
					return false;

				parentContainer = parentContainer.Parent.PartialContainer;
			}

			return true;
		}

		//
		// Does extension methods look up to find a method which matches name and extensionType.
		// Search starts from this namespace and continues hierarchically up to top level.
		//
		public ExtensionMethodCandidates LookupExtensionMethod (TypeSpec extensionType, string name, int arity)
		{
			var m = Parent;
			do {
				var ns = m as NamespaceContainer;
				if (ns != null)
					return ns.LookupExtensionMethod (this, extensionType, name, arity, 0);

				m = m.Parent;
			} while (m != null);

			return null;
		}

		public virtual FullNamedExpression LookupNamespaceAlias (string name)
		{
			return Parent.LookupNamespaceAlias (name);
		}

		public virtual FullNamedExpression LookupNamespaceOrType (string name, int arity, LookupMode mode, Location loc)
		{
			return Parent.LookupNamespaceOrType (name, arity, mode, loc);
		}

		/// <summary>
		/// Goes through class hierarchy and gets value of first found CLSCompliantAttribute.
		/// If no is attribute exists then assembly CLSCompliantAttribute is returned.
		/// </summary>
		public bool? CLSAttributeValue {
			get {
				if ((caching_flags & Flags.HasCompliantAttribute_Undetected) == 0) {
					if ((caching_flags & Flags.HasClsCompliantAttribute) == 0)
						return null;

					return (caching_flags & Flags.ClsCompliantAttributeFalse) == 0;
				}

				caching_flags &= ~Flags.HasCompliantAttribute_Undetected;

				if (OptAttributes != null) {
					Attribute cls_attribute = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
					if (cls_attribute != null) {
						caching_flags |= Flags.HasClsCompliantAttribute;
						if (cls_attribute.GetClsCompliantAttributeValue ())
							return true;

						caching_flags |= Flags.ClsCompliantAttributeFalse;
						return false;
					}
				}

				return null;
			}
		}

		/// <summary>
		/// Returns true if MemberCore is explicitly marked with CLSCompliantAttribute
		/// </summary>
		protected bool HasClsCompliantAttribute {
			get {
				return CLSAttributeValue.HasValue;
			}
		}

		/// <summary>
		/// Returns true when a member supports multiple overloads (methods, indexers, etc)
		/// </summary>
		public virtual bool EnableOverloadChecks (MemberCore overload)
		{
			return false;
		}

		/// <summary>
		/// The main virtual method for CLS-Compliant verifications.
		/// The method returns true if member is CLS-Compliant and false if member is not
		/// CLS-Compliant which means that CLS-Compliant tests are not necessary. A descendants override it
		/// and add their extra verifications.
		/// </summary>
		protected virtual bool VerifyClsCompliance ()
		{
			if (HasClsCompliantAttribute) {
				if (!Module.DeclaringAssembly.HasCLSCompliantAttribute) {
					Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
					if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
						Report.Warning (3021, 2, a.Location,
							"`{0}' does not need a CLSCompliant attribute because the assembly is not marked as CLS-compliant",
							GetSignatureForError ());
					} else {
						Report.Warning (3014, 1, a.Location,
							"`{0}' cannot be marked as CLS-compliant because the assembly is not marked as CLS-compliant",
							GetSignatureForError ());
					}
					return false;
				}

				if (!IsExposedFromAssembly ()) {
					Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
					Report.Warning (3019, 2, a.Location, "CLS compliance checking will not be performed on `{0}' because it is not visible from outside this assembly", GetSignatureForError ());
					return false;
				}

				if ((caching_flags & Flags.ClsCompliantAttributeFalse) != 0) {
					if (Parent is Interface && Parent.IsClsComplianceRequired ()) {
						Report.Warning (3010, 1, Location, "`{0}': CLS-compliant interfaces must have only CLS-compliant members", GetSignatureForError ());
					} else if (Parent.Kind == MemberKind.Class && (ModFlags & Modifiers.ABSTRACT) != 0 && Parent.IsClsComplianceRequired ()) {
						Report.Warning (3011, 1, Location, "`{0}': only CLS-compliant members can be abstract", GetSignatureForError ());
					}

					return false;
				}

				if (Parent.Kind != MemberKind.Namespace && Parent.Kind != 0 && !Parent.IsClsComplianceRequired ()) {
					Attribute a = OptAttributes.Search (Module.PredefinedAttributes.CLSCompliant);
					Report.Warning (3018, 1, a.Location, "`{0}' cannot be marked as CLS-compliant because it is a member of non CLS-compliant type `{1}'",
						GetSignatureForError (), Parent.GetSignatureForError ());
					return false;
				}
			} else {
				if (!IsExposedFromAssembly ())
					return false;

				if (!Parent.IsClsComplianceRequired ())
					return false;
			}

			if (member_name.Name [0] == '_') {
				Warning_IdentifierNotCompliant ();
			}

			if (member_name.TypeParameters != null)
				member_name.TypeParameters.VerifyClsCompliance ();

			return true;
		}

		protected void Warning_IdentifierNotCompliant ()
		{
			Report.Warning (3008, 1, MemberName.Location, "Identifier `{0}' is not CLS-compliant", GetSignatureForError ());
		}

		public virtual string GetCallerMemberName ()
		{
			return MemberName.Name;
		}

		//
		// Returns a string that represents the signature for this 
		// member which should be used in XML documentation.
		//
		public abstract string GetSignatureForDocumentation ();

		public virtual void GetCompletionStartingWith (string prefix, List<string> results)
		{
			Parent.GetCompletionStartingWith (prefix, results);
		}

		//
		// Generates xml doc comments (if any), and if required,
		// handle warning report.
		//
		internal virtual void GenerateDocComment (DocumentationBuilder builder)
		{
			if (DocComment == null) {
				if (IsExposedFromAssembly ()) {
					Constructor c = this as Constructor;
					if (c == null || !c.IsDefault ())
						Report.Warning (1591, 4, Location,
							"Missing XML comment for publicly visible type or member `{0}'", GetSignatureForError ());
				}

				return;
			}

			try {
				builder.GenerateDocumentationForMember (this);
			} catch (Exception e) {
				throw new InternalErrorException (this, e);
			}
		}

		public virtual void WriteDebugSymbol (MonoSymbolFile file)
		{
		}

		#region IMemberContext Members

		public virtual CompilerContext Compiler {
			get {
				return Module.Compiler;
			}
		}

		public virtual TypeSpec CurrentType {
			get { return Parent.CurrentType; }
		}

		public MemberCore CurrentMemberDefinition {
			get { return this; }
		}

		public virtual TypeParameters CurrentTypeParameters {
			get { return null; }
		}

		public bool IsObsolete {
			get {
				if (GetAttributeObsolete () != null)
					return true;

				return Parent == null ? false : Parent.IsObsolete;
			}
		}

		public bool IsUnsafe {
			get {
				if ((ModFlags & Modifiers.UNSAFE) != 0)
					return true;

				return Parent == null ? false : Parent.IsUnsafe;
			}
		}

		public bool IsStatic {
			get {
				return (ModFlags & Modifiers.STATIC) != 0;
			}
		}

		#endregion
	}

	//
	// Base member specification. A member specification contains
	// member details which can alter in the context (e.g. generic instances)
	//
	public abstract class MemberSpec
	{
		[Flags]
		public enum StateFlags
		{
			Obsolete_Undetected = 1,	// Obsolete attribute has not been detected yet
			Obsolete = 1 << 1,			// Member has obsolete attribute
			CLSCompliant_Undetected = 1 << 2,	// CLSCompliant attribute has not been detected yet
			CLSCompliant = 1 << 3,		// Member is CLS Compliant
			MissingDependency_Undetected = 1 << 4,
			MissingDependency = 1 << 5,
			HasDynamicElement = 1 << 6,
			ConstraintsChecked = 1 << 7,

			IsAccessor = 1 << 9,		// Method is an accessor
			IsGeneric = 1 << 10,		// Member contains type arguments

			PendingMetaInflate = 1 << 12,
			PendingMakeMethod = 1 << 13,
			PendingMemberCacheMembers = 1 << 14,
			PendingBaseTypeInflate = 1 << 15,
			InterfacesExpanded = 1 << 16,
			IsNotCSharpCompatible = 1 << 17,
			SpecialRuntimeType = 1 << 18,
			InflatedExpressionType = 1 << 19,
			InflatedNullableType = 1 << 20,
			GenericIterateInterface = 1 << 21,
			GenericTask = 1 << 22,
			InterfacesImported = 1 << 23,
		}

		//
		// Some flags can be copied directly from other member
		//
		protected const StateFlags SharedStateFlags =
			StateFlags.CLSCompliant | StateFlags.CLSCompliant_Undetected |
			StateFlags.Obsolete | StateFlags.Obsolete_Undetected |
			StateFlags.MissingDependency | StateFlags.MissingDependency_Undetected |
			StateFlags.HasDynamicElement;

		protected Modifiers modifiers;
		public StateFlags state;
		protected IMemberDefinition definition;
		public readonly MemberKind Kind;
		protected TypeSpec declaringType;

#if DEBUG
		static int counter;
		public int ID = counter++;
#endif

		protected MemberSpec (MemberKind kind, TypeSpec declaringType, IMemberDefinition definition, Modifiers modifiers)
		{
			this.Kind = kind;
			this.declaringType = declaringType;
			this.definition = definition;
			this.modifiers = modifiers;

			if (kind == MemberKind.MissingType)
				state = StateFlags.MissingDependency;
			else
				state = StateFlags.Obsolete_Undetected | StateFlags.CLSCompliant_Undetected | StateFlags.MissingDependency_Undetected;
		}

		#region Properties

		public virtual int Arity {
			get {
				return 0;
			}
		}

		public TypeSpec DeclaringType {
			get {
				return declaringType;
			}
			set {
				declaringType = value;
			}
		}

		public IMemberDefinition MemberDefinition {
			get {
				return definition;
			}
		}

		public Modifiers Modifiers {
			get {
				return modifiers;
			}
			set {
				modifiers = value;
			}
		}
		
		public virtual string Name {
			get {
				return definition.Name;
			}
		}

		public bool IsAbstract {
			get { return (modifiers & Modifiers.ABSTRACT) != 0; }
		}

		public bool IsAccessor {
			get {
				return (state & StateFlags.IsAccessor) != 0;
			}
			set {
				state = value ? state | StateFlags.IsAccessor : state & ~StateFlags.IsAccessor;
			}
		}

		//
		// Return true when this member is a generic in C# terms
		// A nested non-generic type of generic type will return false
		//
		public bool IsGeneric {
			get {
				return (state & StateFlags.IsGeneric) != 0;
			}
			set {
				state = value ? state | StateFlags.IsGeneric : state & ~StateFlags.IsGeneric;
			}
		}

		//
		// Returns true for imported members which are not compatible with C# language
		//
		public bool IsNotCSharpCompatible {
			get {
				return (state & StateFlags.IsNotCSharpCompatible) != 0;
			}
			set {
				state = value ? state | StateFlags.IsNotCSharpCompatible : state & ~StateFlags.IsNotCSharpCompatible;
			}
		}

		public bool IsPrivate {
			get { return (modifiers & Modifiers.PRIVATE) != 0; }
		}

		public bool IsPublic {
			get { return (modifiers & Modifiers.PUBLIC) != 0; }
		}

		public bool IsStatic {
			get { 
				return (modifiers & Modifiers.STATIC) != 0;
			}
		}

		#endregion

		public virtual ObsoleteAttribute GetAttributeObsolete ()
		{
			if ((state & (StateFlags.Obsolete | StateFlags.Obsolete_Undetected)) == 0)
				return null;

			state &= ~StateFlags.Obsolete_Undetected;

			var oa = definition.GetAttributeObsolete ();
			if (oa != null)
				state |= StateFlags.Obsolete;

			return oa;
		}

		//
		// Returns a list of missing dependencies of this member. The list
		// will contain types only but it can have numerous values for members
		// like methods where both return type and all parameters are checked
		//
		public List<TypeSpec> GetMissingDependencies ()
		{
			if ((state & (StateFlags.MissingDependency | StateFlags.MissingDependency_Undetected)) == 0)
				return null;

			state &= ~StateFlags.MissingDependency_Undetected;

			var imported = definition as ImportedDefinition;
			List<TypeSpec> missing;
			if (imported != null) {
				missing = ResolveMissingDependencies ();
			} else if (this is ElementTypeSpec) {
				missing = ((ElementTypeSpec) this).Element.GetMissingDependencies ();
			} else {
				missing = null;
			}

			if (missing != null) {
				state |= StateFlags.MissingDependency;
			}

			return missing;
		}

		public abstract List<TypeSpec> ResolveMissingDependencies ();

		protected virtual bool IsNotCLSCompliant (out bool attrValue)
		{
			var cls = MemberDefinition.CLSAttributeValue;
			attrValue = cls ?? false;
			return cls == false;
		}

		public virtual string GetSignatureForDocumentation ()
		{
			return DeclaringType.GetSignatureForDocumentation () + "." + Name;
		}

		public virtual string GetSignatureForError ()
		{
			var bf = MemberDefinition as Property.BackingField;
			string name;
			if (bf == null) {
				name = Name;
			} else {
				name = bf.OriginalProperty.MemberName.Name;
			}

			return DeclaringType.GetSignatureForError () + "." + name;
		}

		public virtual MemberSpec InflateMember (TypeParameterInflator inflator)
		{
			var inflated = (MemberSpec) MemberwiseClone ();
			inflated.declaringType = inflator.TypeInstance;
			if (DeclaringType.IsGenericOrParentIsGeneric)
				inflated.state |= StateFlags.PendingMetaInflate;
#if DEBUG
			inflated.ID += 1000000;
#endif
			return inflated;
		}

		//
		// Is this member accessible from invocation context
		//
		public bool IsAccessible (IMemberContext ctx)
		{
			var ma = Modifiers & Modifiers.AccessibilityMask;
			if (ma == Modifiers.PUBLIC)
				return true;

			var parentType = /* this as TypeSpec ?? */ DeclaringType;
			var ctype = ctx.CurrentType;

			if (ma == Modifiers.PRIVATE) {
				if (ctype == null)
					return false;
				//
				// It's only accessible to the current class or children
				//
				if (parentType.MemberDefinition == ctype.MemberDefinition)
					return true;

				return TypeManager.IsNestedChildOf (ctype, parentType.MemberDefinition);
			}

			if ((ma & Modifiers.INTERNAL) != 0) {
				bool b;
				var assembly = ctype == null ? ctx.Module.DeclaringAssembly : ctype.MemberDefinition.DeclaringAssembly;

				if (parentType == null) {
					b = ((ITypeDefinition) MemberDefinition).IsInternalAsPublic (assembly);
				} else {
					b = DeclaringType.MemberDefinition.IsInternalAsPublic (assembly);
				}

				if (b || ma == Modifiers.INTERNAL)
					return b;
			}

			//
			// Checks whether `ctype' is a subclass or nested child of `parentType'.
			//
			while (ctype != null) {
				if (TypeManager.IsFamilyAccessible (ctype, parentType))
					return true;

				// Handle nested types.
				ctype = ctype.DeclaringType;	// TODO: Untested ???
			}

			return false;
		}

		//
		// Returns member CLS compliance based on full member hierarchy
		//
		public bool IsCLSCompliant ()
		{
			if ((state & StateFlags.CLSCompliant_Undetected) != 0) {
				state &= ~StateFlags.CLSCompliant_Undetected;

				bool compliant;
				if (IsNotCLSCompliant (out compliant))
					return false;

				if (!compliant) {
					if (DeclaringType != null) {
						compliant = DeclaringType.IsCLSCompliant ();
					} else {
						compliant = ((ITypeDefinition) MemberDefinition).DeclaringAssembly.IsCLSCompliant;
					}
				}

				if (compliant)
					state |= StateFlags.CLSCompliant;
			}

			return (state & StateFlags.CLSCompliant) != 0;
		}

		public bool IsConditionallyExcluded (IMemberContext ctx, Location loc)
		{
			if ((Kind & (MemberKind.Class | MemberKind.Method)) == 0)
				return false;

			var conditions = MemberDefinition.ConditionalConditions ();
			if (conditions == null)
				return false;

			var m = ctx.CurrentMemberDefinition;
			CompilationSourceFile unit = null;
			while (m != null && unit == null) {
				unit = m as CompilationSourceFile;
				m = m.Parent;
			}

			if (unit != null) {
				foreach (var condition in conditions) {
					if (unit.IsConditionalDefined (condition))
						return false;
				}
			}

			return true;
		}

		public override string ToString ()
		{
			return GetSignatureForError ();
		}
	}

	//
	// Member details which are same between all member
	// specifications
	//
	public interface IMemberDefinition
	{
		bool? CLSAttributeValue { get; }
		string Name { get; }
		bool IsImported { get; }

		string[] ConditionalConditions ();
		ObsoleteAttribute GetAttributeObsolete ();
		void SetIsAssigned ();
		void SetIsUsed ();
	}

	public interface IMethodDefinition : IMemberDefinition
	{
		MethodBase Metadata { get; }
	}

	public interface IParametersMember : IInterfaceMemberSpec
	{
		AParametersCollection Parameters { get; }
	}

	public interface IInterfaceMemberSpec
	{
		TypeSpec MemberType { get; }
	}
}