new error test
[mono.git] / mcs / gmcs / attribute.cs
1 //
2 // attribute.cs: Attribute Handler
3 //
4 // Author: Ravi Pratap (ravi@ximian.com)
5 //         Marek Safar (marek.safar@seznam.cz)
6 //
7 // Licensed under the terms of the GNU GPL
8 //
9 // (C) 2001 Ximian, Inc (http://www.ximian.com)
10 //
11 //
12
13 using System;
14 using System.Diagnostics;
15 using System.Collections;
16 using System.Collections.Specialized;
17 using System.Reflection;
18 using System.Reflection.Emit;
19 using System.Runtime.InteropServices;
20 using System.Runtime.CompilerServices;
21 using System.Text;
22
23 namespace Mono.CSharp {
24
25         /// <summary>
26         ///   Base class for objects that can have Attributes applied to them.
27         /// </summary>
28         public abstract class Attributable {
29                 /// <summary>
30                 ///   Attributes for this type
31                 /// </summary>
32                 Attributes attributes;
33
34                 public Attributable(Attributes attrs)
35                 {
36                         attributes = attrs;
37                         if (attributes != null)
38                                 attributes.CheckTargets (ValidAttributeTargets);
39                 }
40
41                 public Attributes OptAttributes 
42                 {
43                         get {
44                                 return attributes;
45                         }
46                         set {
47                                 attributes = value;
48                                 if (attributes != null)
49                                         attributes.CheckTargets (ValidAttributeTargets);
50                         }
51                 }
52
53                 /// <summary>
54                 /// Use member-specific procedure to apply attribute @a in @cb to the entity being built in @builder
55                 /// </summary>
56                 public abstract void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb);
57
58                 /// <summary>
59                 /// Returns combination of enabled AttributeTargets
60                 /// </summary>
61                 public abstract AttributeTargets AttributeTargets { get; }
62
63                 public abstract bool IsClsCompliaceRequired (DeclSpace ds);
64
65                 /// <summary>
66                 /// Gets list of valid attribute targets for explicit target declaration.
67                 /// The first array item is default target. Don't break this rule.
68                 /// </summary>
69                 protected abstract string[] ValidAttributeTargets { get; }
70         };
71
72         public class Attribute {
73                 public string Target;
74                 public readonly string    Name;
75                 public readonly ArrayList Arguments;
76
77                 public readonly Location Location;
78
79                 public Type Type;
80                 
81                 // Is non-null if type is AttributeUsageAttribute
82                 AttributeUsageAttribute usage_attribute;
83
84                 public AttributeUsageAttribute UsageAttribute {
85                         get {
86                                 return usage_attribute;
87                         }
88                 }
89                 
90                 MethodImplOptions ImplOptions;
91                 UnmanagedType     UnmanagedType;
92                 CustomAttributeBuilder cb;
93         
94                 // non-null if named args present after Resolve () is called
95                 PropertyInfo [] prop_info_arr;
96                 FieldInfo [] field_info_arr;
97                 object [] field_values_arr;
98                 object [] prop_values_arr;
99                 object [] pos_values;
100
101                 static PtrHashtable usage_attr_cache = new PtrHashtable ();
102                 
103                 public Attribute (string target, string name, ArrayList args, Location loc)
104                 {
105                         Name = name;
106                         Arguments = args;
107                         Location = loc;
108                         Target = target;
109                 }
110
111                 void Error_InvalidNamedArgument (string name)
112                 {
113                         Report.Error (617, Location, "'" + name + "' is not a valid named attribute " +
114                                       "argument. Named attribute arguments must be fields which are not " +
115                                       "readonly, static or const, or properties with a set accessor which "+
116                                       "are not static.");
117                 }
118
119                 static void Error_AttributeArgumentNotValid (Location loc)
120                 {
121                         Report.Error (182, loc,
122                                       "An attribute argument must be a constant expression, typeof " +
123                                       "expression or array creation expression");
124                 }
125
126                 static void Error_TypeParameterInAttribute (Location loc)
127                 {
128                         Report.Error (
129                                 -202, loc, "Can not use a type parameter in an attribute");
130                 }
131
132                 void Error_AttributeConstructorMismatch ()
133                 {
134                         Report.Error (-6, Location,
135                                       "Could not find a constructor for this argument list.");
136                 }
137
138                 /// <summary>
139                 ///   Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
140                 /// </summary>
141                 protected virtual Type CheckAttributeType (EmitContext ec, bool complain)
142                 {
143                         TypeExpr t1 = RootContext.LookupType (ec.DeclSpace, Name, true, Location);
144                         // FIXME: Shouldn't do this for quoted attributes: [@A]
145                         TypeExpr t2 = RootContext.LookupType (ec.DeclSpace, Name + "Attribute", true, Location);
146
147                         String err0616 = null;
148                         if (t1 != null && ! t1.IsAttribute) {
149                                 t1 = null;
150                                 err0616 = "'" + Name + "': is not an attribute class";
151                         }
152                         if (t2 != null && ! t2.IsAttribute) {
153                                 t2 = null;
154                                 err0616 = (err0616 != null) 
155                                         ? "Neither '" + Name + "' nor '" + Name + "Attribute' is an attribute class"
156                                         : "'" + Name + "Attribute': is not an attribute class";
157                         }
158
159                         if (t1 != null && t2 != null) {
160                                 Report.Error(1614, Location, "'" + Name + "': is ambiguous; " 
161                                              + " use either '@" + Name + "' or '" + Name + "Attribute'");
162                                 return null;
163                         }
164                         if (t1 != null)
165                                 return t1.ResolveType (ec);
166                         if (t2 != null)
167                                 return t2.ResolveType (ec);
168                         if (err0616 != null) {
169                                 Report.Error (616, Location, err0616);
170                                 return null;
171                         }
172
173                         if (complain)
174                                 Report.Error (246, Location, 
175                                               "Could not find attribute '" + Name 
176                                               + "' (are you missing a using directive or an assembly reference ?)");
177                         return null;
178                 }
179
180                 public Type ResolveType (EmitContext ec, bool complain)
181                 {
182                         if (Type == null)
183                                 Type = CheckAttributeType (ec, complain);
184                         return Type;
185                 }
186
187                 /// <summary>
188                 ///   Validates the guid string
189                 /// </summary>
190                 bool ValidateGuid (string guid)
191                 {
192                         try {
193                                 new Guid (guid);
194                                 return true;
195                         } catch {
196                                 Report.Error (647, Location, "Format of GUID is invalid: " + guid);
197                                 return false;
198                         }
199                 }
200
201                 string GetFullMemberName (string member)
202                 {
203                         return Type.FullName + '.' + member;
204                 }
205
206                 //
207                 // Given an expression, if the expression is a valid attribute-argument-expression
208                 // returns an object that can be used to encode it, or null on failure.
209                 //
210                 public static bool GetAttributeArgumentExpression (Expression e, Location loc, out object result)
211                 {
212                         if (e is Constant) {
213                                 result = ((Constant) e).GetValue ();
214                                 return true;
215                         } else if (e is TypeOf) {
216                                 result = ((TypeOf) e).TypeArg;
217                                 return true;
218                         } else if (e is ArrayCreation){
219                                 result =  ((ArrayCreation) e).EncodeAsAttribute ();
220                                 if (result != null)
221                                         return true;
222                         } else if (e is EmptyCast) {
223                                 result = e;
224                                 if (((EmptyCast) e).Child is Constant) {
225                                         result = ((Constant) ((EmptyCast)e).Child).GetValue();
226                                 }
227                                 return true;
228                         }
229
230                         result = null;
231                         Error_AttributeArgumentNotValid (loc);
232                         return false;
233                 }
234                 
235                 public CustomAttributeBuilder Resolve (EmitContext ec)
236                 {
237                         Type oldType = Type;
238                         
239                         // Sanity check.
240                         Type = CheckAttributeType (ec, true);
241                         if (oldType == null && Type == null)
242                                 return null;
243                         if (oldType != null && oldType != Type) {
244                                 Report.Error (-6, Location,
245                                               "Attribute {0} resolved to different types at different times: {1} vs. {2}",
246                                               Name, oldType, Type);
247                                 return null;
248                         }
249
250                         bool MethodImplAttr = false;
251                         bool MarshalAsAttr = false;
252                         bool GuidAttr = false;
253                         bool usage_attr = false;
254
255                         bool DoCompares = true;
256
257                         //
258                         // If we are a certain special attribute, we
259                         // set the information accordingly
260                         //
261                         
262                         if (Type == TypeManager.attribute_usage_type)
263                                 usage_attr = true;
264                         else if (Type == TypeManager.methodimpl_attr_type)
265                                 MethodImplAttr = true;
266                         else if (Type == TypeManager.marshal_as_attr_type)
267                                 MarshalAsAttr = true;
268                         else if (Type == TypeManager.guid_attr_type)
269                                 GuidAttr = true;
270                         else
271                                 DoCompares = false;
272
273                         // Now we extract the positional and named arguments
274                         
275                         ArrayList pos_args = new ArrayList ();
276                         ArrayList named_args = new ArrayList ();
277                         int pos_arg_count = 0;
278                         
279                         if (Arguments != null) {
280                                 pos_args = (ArrayList) Arguments [0];
281                                 if (pos_args != null)
282                                         pos_arg_count = pos_args.Count;
283                                 if (Arguments.Count > 1)
284                                         named_args = (ArrayList) Arguments [1];
285                         }
286
287                         pos_values = new object [pos_arg_count];
288
289                         //
290                         // First process positional arguments 
291                         //
292
293                         int i;
294                         for (i = 0; i < pos_arg_count; i++) {
295                                 Argument a = (Argument) pos_args [i];
296                                 Expression e;
297
298                                 if (!a.Resolve (ec, Location))
299                                         return null;
300
301                                 e = a.Expr;
302
303                                 object val;
304                                 if (!GetAttributeArgumentExpression (e, Location, out val))
305                                         return null;
306                                 
307                                 pos_values [i] = val;
308                                 if (DoCompares){
309                                         if (usage_attr)
310                                                 usage_attribute = new AttributeUsageAttribute ((AttributeTargets) pos_values [0]);
311                                         else if (MethodImplAttr)
312                                                 this.ImplOptions = (MethodImplOptions) pos_values [0];
313                                         else if (GuidAttr){
314                                                 //
315                                                 // we will later check the validity of the type
316                                                 //
317                                                 if (pos_values [0] is string){
318                                                         if (!ValidateGuid ((string) pos_values [0]))
319                                                                 return null;
320                                                 }
321                                                 
322                                         } else if (MarshalAsAttr)
323                                                 this.UnmanagedType =
324                                                 (System.Runtime.InteropServices.UnmanagedType) pos_values [0];
325                                 }
326                         }
327
328                         //
329                         // Now process named arguments
330                         //
331
332                         ArrayList field_infos = null;
333                         ArrayList prop_infos  = null;
334                         ArrayList field_values = null;
335                         ArrayList prop_values = null;
336
337                         if (named_args.Count > 0) {
338                                 field_infos = new ArrayList ();
339                                 prop_infos  = new ArrayList ();
340                                 field_values = new ArrayList ();
341                                 prop_values = new ArrayList ();
342                         }
343
344                         Hashtable seen_names = new Hashtable();
345                         
346                         for (i = 0; i < named_args.Count; i++) {
347                                 DictionaryEntry de = (DictionaryEntry) named_args [i];
348                                 string member_name = (string) de.Key;
349                                 Argument a  = (Argument) de.Value;
350                                 Expression e;
351
352                                 if (seen_names.Contains(member_name)) {
353                                         Report.Error(643, Location, "'" + member_name + "' duplicate named attribute argument");
354                                         return null;
355                                 }                               
356                                 seen_names.Add(member_name, 1);
357                                 
358                                 if (!a.Resolve (ec, Location))
359                                         return null;
360
361                                 Expression member = Expression.MemberLookup (
362                                         ec, Type, member_name,
363                                         MemberTypes.Field | MemberTypes.Property,
364                                         BindingFlags.Public | BindingFlags.Instance,
365                                         Location);
366
367                                 if (member == null) {
368                                         member = Expression.MemberLookup (ec, Type, member_name,
369                                                 MemberTypes.Field | MemberTypes.Property, BindingFlags.NonPublic | BindingFlags.Instance,
370                                                 Location);
371
372                                         if (member != null) {
373                                                 Report.Error_T (122, Location, GetFullMemberName (member_name));
374                                                 return null;
375                                         }
376                                 }
377
378                                 if (member == null || !(member is PropertyExpr || member is FieldExpr)) {
379                                         Error_InvalidNamedArgument (member_name);
380                                         return null;
381                                 }
382
383                                 e = a.Expr;
384                                 if (e is TypeParameterExpr){
385                                         Error_TypeParameterInAttribute (Location);
386                                         return null;
387                                 }
388                                         
389                                 if (member is PropertyExpr) {
390                                         PropertyExpr pe = (PropertyExpr) member;
391                                         PropertyInfo pi = pe.PropertyInfo;
392
393                                         if (!pi.CanWrite) {
394                                                 Error_InvalidNamedArgument (member_name);
395                                                 return null;
396                                         }
397
398                                         Constant c = e as Constant;
399                                         if (c != null) {
400                                                 if (c.Type != pi.PropertyType) {
401                                                         c = Const.ChangeType (Location, c, pi.PropertyType);
402                                                         if (c == null)
403                                                                 return null;
404                                                 }
405                                                 
406                                                 object o = c.GetValue ();
407                                                 prop_values.Add (o);
408                                                 
409                                                 if (usage_attribute != null) {
410                                                         if (member_name == "AllowMultiple")
411                                                                 usage_attribute.AllowMultiple = (bool) o;
412                                                         if (member_name == "Inherited")
413                                                                 usage_attribute.Inherited = (bool) o;
414                                                 }
415                                                 
416                                         } else if (e is TypeOf) {
417                                                 prop_values.Add (((TypeOf) e).TypeArg);
418                                         } else if (e is ArrayCreation) {
419                                                 prop_values.Add (((ArrayCreation) e).EncodeAsAttribute());
420                                         } else {
421                                                 Error_AttributeArgumentNotValid (Location);
422                                                 return null;
423                                         }
424                                         
425                                         prop_infos.Add (pi);
426                                         
427                                 } else if (member is FieldExpr) {
428                                         FieldExpr fe = (FieldExpr) member;
429                                         FieldInfo fi = fe.FieldInfo;
430
431                                         if (fi.IsInitOnly) {
432                                                 Error_InvalidNamedArgument (member_name);
433                                                 return null;
434                                         }
435
436                                         //
437                                         // Handle charset here, and set the TypeAttributes
438                                         
439                                         Constant c = e as Constant;
440                                         if (c != null) {
441                                                 if (c.Type != fi.FieldType){
442                                                         c = Const.ChangeType (Location, c, fi.FieldType);
443                                                         if (c == null)
444                                                                 return null;
445                                                 } 
446                                                 
447                                                 object value = c.GetValue ();
448                                                 field_values.Add (value);
449                                         } else if (e is TypeOf) {
450                                                 field_values.Add (((TypeOf) e).TypeArg);
451                                         } else {
452                                                 Error_AttributeArgumentNotValid (Location);
453                                                 return null;
454                                         }
455                                         
456                                         field_infos.Add (fi);
457                                 }
458                         }
459
460                         Expression mg = Expression.MemberLookup (
461                                 ec, Type, ".ctor", MemberTypes.Constructor,
462                                 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
463                                 Location);
464
465                         if (mg == null) {
466                                 Error_AttributeConstructorMismatch ();
467                                 return null;
468                         }
469
470                         MethodBase constructor = Invocation.OverloadResolve (
471                                 ec, (MethodGroupExpr) mg, pos_args, false, Location);
472
473                         if (constructor == null) {
474                                 Error_AttributeConstructorMismatch ();
475                                 return null;
476                         }
477
478                         //
479                         // Now we perform some checks on the positional args as they
480                         // cannot be null for a constructor which expects a parameter
481                         // of type object
482                         //
483
484                         ParameterData pd = Invocation.GetParameterData (constructor);
485
486                         int group_in_params_array = Int32.MaxValue;
487                         int pc = pd.Count;
488                         if (pc > 0 && pd.ParameterModifier (pc-1) == Parameter.Modifier.PARAMS)
489                                 group_in_params_array = pc-1;
490
491                         for (int j = 0; j < pos_arg_count; ++j) {
492                                 Argument a = (Argument) pos_args [j];
493                                 
494                                 if (a.Expr is NullLiteral && pd.ParameterType (j) == TypeManager.object_type) {
495                                         Error_AttributeArgumentNotValid (Location);
496                                         return null;
497                                 }
498
499                                 if (j < group_in_params_array)
500                                         continue;
501                                 
502                                 if (j == group_in_params_array){
503                                         object v = pos_values [j];
504                                         int count = pos_arg_count - j;
505
506                                         object [] array = new object [count];
507                                         pos_values [j] = array;
508                                         array [0] = v;
509                                 } else {
510                                         object [] array = (object []) pos_values [group_in_params_array];
511
512                                         array [j - group_in_params_array] = pos_values [j];
513                                 }
514                         }
515
516                         //
517                         // Adjust the size of the pos_values if it had params
518                         //
519                         if (group_in_params_array != Int32.MaxValue){
520                                 int argc = group_in_params_array+1;
521                                 object [] new_pos_values = new object [argc];
522
523                                 for (int p = 0; p < argc; p++)
524                                         new_pos_values [p] = pos_values [p];
525                                 pos_values = new_pos_values;
526                         }
527
528                         try {
529                                 if (named_args.Count > 0) {
530                                         prop_info_arr = new PropertyInfo [prop_infos.Count];
531                                         field_info_arr = new FieldInfo [field_infos.Count];
532                                         field_values_arr = new object [field_values.Count];
533                                         prop_values_arr = new object [prop_values.Count];
534
535                                         field_infos.CopyTo  (field_info_arr, 0);
536                                         field_values.CopyTo (field_values_arr, 0);
537
538                                         prop_values.CopyTo  (prop_values_arr, 0);
539                                         prop_infos.CopyTo   (prop_info_arr, 0);
540
541                                         cb = new CustomAttributeBuilder (
542                                                 (ConstructorInfo) constructor, pos_values,
543                                                 prop_info_arr, prop_values_arr,
544                                                 field_info_arr, field_values_arr);
545                                 }
546                                 else
547                                         cb = new CustomAttributeBuilder (
548                                                 (ConstructorInfo) constructor, pos_values);
549                         } catch (NullReferenceException) {
550                                 // 
551                                 // Don't know what to do here
552                                 //
553                                 Report.Warning (
554                                         -101, Location, "NullReferenceException while trying to create attribute." +
555                                         "Something's wrong!");
556                         } catch (Exception e) {
557                                 //
558                                 // Sample:
559                                 // using System.ComponentModel;
560                                 // [DefaultValue (CollectionChangeAction.Add)]
561                                 // class X { static void Main () {} }
562                                 //
563                                 Report.Warning (
564                                         -23, Location, "The compiler can not encode this attribute in .NET due to a bug in the .NET runtime. Try the Mono runtime. The exception was: " + e.Message);
565                         }
566                         
567                         return cb;
568                 }
569
570                 /// <summary>
571                 ///   Get a string containing a list of valid targets for the attribute 'attr'
572                 /// </summary>
573                 static string GetValidTargets (Attribute attr)
574                 {
575                         StringBuilder sb = new StringBuilder ();
576                         AttributeTargets targets = attr.GetAttributeUsage ().ValidOn;
577
578                         if ((targets & AttributeTargets.Assembly) != 0)
579                                 sb.Append ("'assembly' ");
580
581                         if ((targets & AttributeTargets.Class) != 0)
582                                 sb.Append ("'class' ");
583
584                         if ((targets & AttributeTargets.Constructor) != 0)
585                                 sb.Append ("'constructor' ");
586
587                         if ((targets & AttributeTargets.Delegate) != 0)
588                                 sb.Append ("'delegate' ");
589
590                         if ((targets & AttributeTargets.Enum) != 0)
591                                 sb.Append ("'enum' ");
592
593                         if ((targets & AttributeTargets.Event) != 0)
594                                 sb.Append ("'event' ");
595
596                         if ((targets & AttributeTargets.Field) != 0)
597                                 sb.Append ("'field' ");
598
599                         if ((targets & AttributeTargets.Interface) != 0)
600                                 sb.Append ("'interface' ");
601
602                         if ((targets & AttributeTargets.Method) != 0)
603                                 sb.Append ("'method' ");
604
605                         if ((targets & AttributeTargets.Module) != 0)
606                                 sb.Append ("'module' ");
607
608                         if ((targets & AttributeTargets.Parameter) != 0)
609                                 sb.Append ("'parameter' ");
610
611                         if ((targets & AttributeTargets.Property) != 0)
612                                 sb.Append ("'property' ");
613
614                         if ((targets & AttributeTargets.ReturnValue) != 0)
615                                 sb.Append ("'return' ");
616
617                         if ((targets & AttributeTargets.Struct) != 0)
618                                 sb.Append ("'struct' ");
619
620                         return sb.ToString ();
621
622                 }
623
624                 public static void Error_AttributeNotValidForElement (Attribute a, Location loc)
625                 {
626                         Report.Error (
627                                 592, loc, "Attribute '" + a.Name +
628                                 "' is not valid on this declaration type. " +
629                                 "It is valid on " + GetValidTargets (a) + "declarations only.");
630                 }
631
632
633                 /// <summary>
634                 /// Returns AttributeUsage attribute for this type
635                 /// </summary>
636                 public AttributeUsageAttribute GetAttributeUsage ()
637                 {
638                         AttributeUsageAttribute ua = usage_attr_cache [Type] as AttributeUsageAttribute;
639                         if (ua != null)
640                                 return ua;
641
642                         Class attr_class = TypeManager.LookupClass (Type);
643
644                         if (attr_class == null) {
645                                 object[] usage_attr = Type.GetCustomAttributes (TypeManager.attribute_usage_type, true);
646                                 ua = (AttributeUsageAttribute)usage_attr [0];
647                                 usage_attr_cache.Add (Type, ua);
648                                 return ua;
649                         }
650                 
651                         return attr_class.AttributeUsage;
652                 }
653
654
655                 //
656                 // This pulls the condition name out of a Conditional attribute
657                 //
658                 public string Conditional_GetConditionName ()
659                 {
660                         //
661                         // So we have a Conditional, pull the data out.
662                         //
663                         if (Arguments == null || Arguments [0] == null){
664                                 Error_AttributeConstructorMismatch ();
665                                 return null;
666                         }
667
668                         ArrayList pos_args = (ArrayList) Arguments [0];
669                         if (pos_args.Count != 1){
670                                 Error_AttributeConstructorMismatch ();
671                                 return null;
672                         }
673
674                         Argument arg = (Argument) pos_args [0]; 
675                         if (!(arg.Expr is StringConstant)){
676                                 Error_AttributeConstructorMismatch ();
677                                 return null;
678                         }
679
680                         return ((StringConstant) arg.Expr).Value;
681                 }
682
683                 public string IndexerName_GetIndexerName (EmitContext ec)
684                 {
685                         if (Arguments == null || Arguments [0] == null){
686                                 Error_AttributeConstructorMismatch ();
687                                 return null;
688                         }
689                         ArrayList pos_args = (ArrayList) Arguments [0];
690                         if (pos_args.Count != 1) {
691                                 Error_AttributeConstructorMismatch ();
692                                 return null;
693                         }
694                         
695                         Argument arg = (Argument) pos_args [0];
696                         if (!arg.Resolve (ec, Location))
697                                 return null;
698                         
699                         StringConstant sc = arg.Expr as StringConstant;
700                         if (sc == null){
701                                 Error_AttributeConstructorMismatch ();
702                                 return null;
703                         }
704                         
705                         return sc.Value;
706                 }
707
708                 /// <summary>
709                 /// Returns condition of ConditionalAttribute
710                 /// </summary>
711                 public string GetConditionalAttributeValue (DeclSpace ds)
712                 {
713                         if (pos_values == null) {
714                                 EmitContext ec = new EmitContext (ds, ds, Location, null, null, 0, false);
715
716                                 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
717                                 // But because a lot of attribute class code must be rewritten will be better to wait...
718                                 Resolve (ec);
719                         }
720
721                         // Some error occurred
722                         if (pos_values [0] == null)
723                                 return null;
724
725                         return (string)pos_values [0];
726                 }
727
728                 /// <summary>
729                 /// Creates the instance of ObsoleteAttribute from this attribute instance
730                 /// </summary>
731                 public ObsoleteAttribute GetObsoleteAttribute (DeclSpace ds)
732                 {
733                         if (pos_values == null) {
734                                 EmitContext ec = new EmitContext (ds, ds, Location, null, null, 0, false);
735
736                                 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
737                                 // But because a lot of attribute class code must be rewritten will be better to wait...
738                                 Resolve (ec);
739                         }
740
741                         // Some error occurred
742                         if (pos_values == null)
743                                 return null;
744
745                         if (pos_values.Length == 0)
746                                 return new ObsoleteAttribute ();
747
748                         if (pos_values.Length == 1)
749                                 return new ObsoleteAttribute ((string)pos_values [0]);
750
751                         return new ObsoleteAttribute ((string)pos_values [0], (bool)pos_values [1]);
752                 }
753
754                 /// <summary>
755                 /// Returns value of CLSCompliantAttribute contructor parameter but because the method can be called
756                 /// before ApplyAttribute. We need to resolve the arguments.
757                 /// This situation occurs when class deps is differs from Emit order.  
758                 /// </summary>
759                 public bool GetClsCompliantAttributeValue (DeclSpace ds)
760                 {
761                         if (pos_values == null) {
762                                 EmitContext ec = new EmitContext (ds, ds, Location, null, null, 0, false);
763
764                                 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
765                                 // But because a lot of attribute class code must be rewritten will be better to wait...
766                                 Resolve (ec);
767                         }
768
769                         // Some error occurred
770                         if (pos_values [0] == null)
771                                 return false;
772
773                         return (bool)pos_values [0];
774                 }
775
776                 public object GetPositionalValue (int i)
777                 {
778                         return (pos_values == null) ? null : pos_values[i];
779                 }
780
781                 object GetFieldValue (string name)
782                 {
783                         int i;
784                         if (field_info_arr == null)
785                                 return null;
786                         i = 0;
787                         foreach (FieldInfo fi in field_info_arr) {
788                                 if (fi.Name == name)
789                                         return field_values_arr [i];
790                                 i++;
791                         }
792                         return null;
793                 }
794
795                 public UnmanagedMarshal GetMarshal ()
796                 {
797                         object o = GetFieldValue ("ArraySubType");
798                         UnmanagedType array_sub_type = o == null ? UnmanagedType.I4 : (UnmanagedType) o;
799                         
800                         switch (UnmanagedType) {
801                         case UnmanagedType.CustomMarshaler:
802                                 MethodInfo define_custom = typeof (UnmanagedMarshal).GetMethod ("DefineCustom",
803                                                                        BindingFlags.Static | BindingFlags.Public);
804                                 if (define_custom == null)
805                                         return null;
806                                 
807                                 object [] args = new object [4];
808                                 args [0] = GetFieldValue ("MarshalTypeRef");
809                                 args [1] = GetFieldValue ("MarshalCookie");
810                                 args [2] = GetFieldValue ("MarshalType");
811                                 args [3] = Guid.Empty;
812                                 return (UnmanagedMarshal) define_custom.Invoke (null, args);
813                                 
814                         case UnmanagedType.LPArray:                             
815                                 return UnmanagedMarshal.DefineLPArray (array_sub_type);
816                         
817                         case UnmanagedType.SafeArray:
818                                 return UnmanagedMarshal.DefineSafeArray (array_sub_type);
819                         
820                         case UnmanagedType.ByValArray:
821                                 return UnmanagedMarshal.DefineByValArray ((int) GetFieldValue ("SizeConst"));
822                         
823                         case UnmanagedType.ByValTStr:
824                                 return UnmanagedMarshal.DefineByValTStr ((int) GetFieldValue ("SizeConst"));
825                         
826                         default:
827                                 return UnmanagedMarshal.DefineUnmanagedMarshal (UnmanagedType);
828                         }
829                 }
830
831                 public bool IsInternalCall
832                 {
833                         get { return ImplOptions == MethodImplOptions.InternalCall; }
834                 }
835
836                 /// <summary>
837                 /// Emit attribute for Attributable symbol
838                 /// </summary>
839                 public void Emit (EmitContext ec, Attributable ias, ListDictionary emitted_attr)
840                 {
841                         CustomAttributeBuilder cb = Resolve (ec);
842                         if (cb == null)
843                                 return;
844
845                         AttributeUsageAttribute usage_attr = GetAttributeUsage ();
846                         if ((usage_attr.ValidOn & ias.AttributeTargets) == 0) {
847                                         Error_AttributeNotValidForElement (this, Location);
848                                 return;
849                         }
850
851                         ias.ApplyAttributeBuilder (this, cb);
852
853                         if (!usage_attr.AllowMultiple && emitted_attr.Contains (Target)) {
854                                 Report.Error (579, Location, "Duplicate '" + Name + "' attribute");
855                         }
856
857                         emitted_attr [Type] = Target;
858
859                         // Here we are testing attribute arguments for array usage (error 3016)
860                         if (ias.IsClsCompliaceRequired (ec.DeclSpace)) {
861                                 if (Arguments == null)
862                                         return;
863
864                                 ArrayList pos_args = (ArrayList) Arguments [0];
865                                 if (pos_args != null) {
866                                         foreach (Argument arg in pos_args) { 
867                                                 // Type is undefined (was error 246)
868                                                 if (arg.Type == null)
869                                                         return;
870
871                                                 if (arg.Type.IsArray) {
872                                                         Report.Error_T (3016, Location);
873                                                         return;
874                                                 }
875                                         }
876                                 }
877                         
878                                 if (Arguments.Count < 2)
879                                         return;
880                         
881                                 ArrayList named_args = (ArrayList) Arguments [1];
882                                 foreach (DictionaryEntry de in named_args) {
883                                         Argument arg  = (Argument) de.Value;
884
885                                         // Type is undefined (was error 246)
886                                         if (arg.Type == null)
887                                                 return;
888
889                                         if (arg.Type.IsArray) {
890                                                 Report.Error_T (3016, Location);
891                                                 return;
892                                         }
893                                 }
894                         }
895                 }
896
897                 public object GetValue (EmitContext ec, Constant c, Type target)
898                 {
899                         if (Convert.ImplicitConversionExists (ec, c, target))
900                                 return c.GetValue ();
901
902                         Convert.Error_CannotImplicitConversion (Location, c.Type, target);
903                         return null;
904                 }
905                 
906                 public MethodBuilder DefinePInvokeMethod (EmitContext ec, TypeBuilder builder, string name,
907                                                           MethodAttributes flags, Type ret_type, Type [] param_types)
908                 {
909                         //
910                         // We extract from the attribute the information we need 
911                         //
912
913                         if (Arguments == null) {
914                                 Console.WriteLine ("Internal error : this is not supposed to happen !");
915                                 return null;
916                         }
917
918                         ResolveType (ec, true);
919                         if (Type == null)
920                                 return null;
921                         
922                         ArrayList named_args = new ArrayList ();
923                         
924                         ArrayList pos_args = (ArrayList) Arguments [0];
925                         if (Arguments.Count > 1)
926                                 named_args = (ArrayList) Arguments [1];
927                         
928
929                         string dll_name = null;
930                         
931                         Argument tmp = (Argument) pos_args [0];
932
933                         if (!tmp.Resolve (ec, Location))
934                                 return null;
935                         
936                         if (tmp.Expr is Constant)
937                                 dll_name = (string) ((Constant) tmp.Expr).GetValue ();
938                         else { 
939                                 Error_AttributeArgumentNotValid (Location);
940                                 return null;
941                         }
942
943                         // Now we process the named arguments
944                         CallingConvention cc = CallingConvention.Winapi;
945                         CharSet charset = CharSet.Ansi;
946                         bool preserve_sig = true;
947 #if FIXME
948                         bool exact_spelling = false;
949 #endif
950                         bool set_last_err = false;
951                         string entry_point = null;
952
953                         for (int i = 0; i < named_args.Count; i++) {
954
955                                 DictionaryEntry de = (DictionaryEntry) named_args [i];
956
957                                 string member_name = (string) de.Key;
958                                 Argument a  = (Argument) de.Value;
959
960                                 if (!a.Resolve (ec, Location))
961                                         return null;
962
963                                 Expression member = Expression.MemberLookup (
964                                         ec, Type, member_name, 
965                                         MemberTypes.Field | MemberTypes.Property,
966                                         BindingFlags.Public | BindingFlags.Instance,
967                                         Location);
968
969                                 if (member == null || !(member is FieldExpr)) {
970                                         Error_InvalidNamedArgument (member_name);
971                                         return null;
972                                 }
973
974                                 if (member is FieldExpr) {
975                                         FieldExpr fe = (FieldExpr) member;
976                                         FieldInfo fi = fe.FieldInfo;
977
978                                         if (fi.IsInitOnly) {
979                                                 Error_InvalidNamedArgument (member_name);
980                                                 return null;
981                                         }
982
983                                         if (a.Expr is Constant) {
984                                                 Constant c = (Constant) a.Expr;
985
986                                                 try {
987                                                         if (member_name == "CallingConvention"){
988                                                                 object val = GetValue (ec, c, typeof (CallingConvention));
989                                                                 if (val == null)
990                                                                         return null;
991                                                                 cc = (CallingConvention) val;
992                                                         } else if (member_name == "CharSet"){
993                                                                 charset = (CharSet) c.GetValue ();
994                                                         } else if (member_name == "EntryPoint")
995                                                                 entry_point = (string) c.GetValue ();
996                                                         else if (member_name == "SetLastError")
997                                                                 set_last_err = (bool) c.GetValue ();
998 #if FIXME
999                                                         else if (member_name == "ExactSpelling")
1000                                                                 exact_spelling = (bool) c.GetValue ();
1001 #endif
1002                                                         else if (member_name == "PreserveSig")
1003                                                                 preserve_sig = (bool) c.GetValue ();
1004                                                 } catch (InvalidCastException){
1005                                                         Error_InvalidNamedArgument (member_name);
1006                                                         Error_AttributeArgumentNotValid (Location);
1007                                                 }
1008                                         } else { 
1009                                                 Error_AttributeArgumentNotValid (Location);
1010                                                 return null;
1011                                         }
1012                                         
1013                                 }
1014                         }
1015
1016                         if (entry_point == null)
1017                                 entry_point = name;
1018                         if (set_last_err)
1019                                 charset = (CharSet)((int)charset | 0x40);
1020                         
1021                         MethodBuilder mb = builder.DefinePInvokeMethod (
1022                                 name, dll_name, entry_point, flags | MethodAttributes.HideBySig,
1023                                 CallingConventions.Standard,
1024                                 ret_type,
1025                                 param_types,
1026                                 cc,
1027                                 charset);
1028
1029                         if (preserve_sig)
1030                                 mb.SetImplementationFlags (MethodImplAttributes.PreserveSig);
1031                         
1032                         return mb;
1033                 }
1034
1035                 private Expression GetValue () 
1036                 {
1037                         if ((Arguments == null) || (Arguments.Count < 1))
1038                                 return null;
1039                         ArrayList al = (ArrayList) Arguments [0];
1040                         if ((al == null) || (al.Count < 1))
1041                                 return null;
1042                         Argument arg = (Argument) al [0];
1043                         if ((arg == null) || (arg.Expr == null))
1044                                 return null;
1045                         return arg.Expr;
1046                 }
1047
1048                 public string GetString () 
1049                 {
1050                         Expression e = GetValue ();
1051                         if (e is StringLiteral)
1052                                 return (e as StringLiteral).Value;
1053                         return null;
1054                 }
1055
1056                 public bool GetBoolean () 
1057                 {
1058                         Expression e = GetValue ();
1059                         if (e is BoolLiteral)
1060                                 return (e as BoolLiteral).Value;
1061                         return false;
1062                 }
1063         }
1064         
1065
1066         /// <summary>
1067         /// For global attributes (assembly, module) we need special handling.
1068         /// Attributes can be located in the several files
1069         /// </summary>
1070         public class GlobalAttribute: Attribute
1071         {
1072                 public readonly NamespaceEntry ns;
1073
1074                 public GlobalAttribute (TypeContainer container, string target, string name, ArrayList args, Location loc):
1075                         base (target, name, args, loc)
1076                 {
1077                         ns = container.NamespaceEntry;
1078                 }
1079
1080                 protected override Type CheckAttributeType (EmitContext ec, bool complain)
1081                 {
1082                         NamespaceEntry old = ec.DeclSpace.NamespaceEntry;
1083                         if (old == null || old.NS == null || old.NS == Namespace.Root) 
1084                                 ec.DeclSpace.NamespaceEntry = ns;
1085                         return base.CheckAttributeType (ec, complain);
1086                 }
1087         }
1088
1089         public class Attributes {
1090                 public ArrayList Attrs;
1091
1092                 public Attributes (Attribute a)
1093                 {
1094                         Attrs = new ArrayList ();
1095                         Attrs.Add (a);
1096                 }
1097
1098                 public Attributes (ArrayList attrs)
1099                 {
1100                         Attrs = attrs;
1101                 }
1102
1103                 public void AddAttributes (ArrayList attrs)
1104                 {
1105                         Attrs.AddRange (attrs);
1106                 }
1107
1108                 /// <summary>
1109                 /// Checks whether attribute target is valid for the current element
1110                 /// </summary>
1111                 public void CheckTargets (string[] possible_targets)
1112                 {
1113                         foreach (Attribute a in Attrs) {
1114                                 if (a.Target == null) {
1115                                         a.Target = possible_targets [0];
1116                                         continue;
1117                                 }
1118
1119                                 if (((IList) possible_targets).Contains (a.Target))
1120                                         continue;
1121
1122                                 StringBuilder sb = new StringBuilder ();
1123                                 foreach (string s in possible_targets) {
1124                                         sb.Append (s);
1125                                         sb.Append (", ");
1126                                 }
1127                                 sb.Remove (sb.Length - 2, 2);
1128                                 Report.Error_T (657, a.Location, a.Target, sb.ToString ());
1129                         }
1130                 }
1131
1132                 private Attribute Search (Type t, EmitContext ec, bool complain)
1133                 {
1134                         foreach (Attribute a in Attrs) {
1135                                 if (a.ResolveType (ec, false) == t)
1136                                         return a;
1137                         }
1138                         return null;
1139                 }
1140
1141                 public Attribute Search (Type t, EmitContext ec)
1142                 {
1143                         return Search (t, ec, true);
1144                 }
1145
1146                 /// <summary>
1147                 /// Returns all attributes of type 't'. Use it when attribute is AllowMultiple = true
1148                 /// </summary>
1149                 public Attribute[] SearchMulti (Type t, EmitContext ec)
1150                 {
1151                         ArrayList ar = null;
1152
1153                         foreach (Attribute a in Attrs) {
1154                                 if (a.ResolveType (ec, false) == t) {
1155                                         if (ar == null)
1156                                                 ar = new ArrayList ();
1157                                         ar.Add (a);
1158                                 }
1159                         }
1160
1161                         return ar == null ? null : ar.ToArray (typeof (Attribute)) as Attribute[];
1162                 }
1163
1164                 public void Emit (EmitContext ec, Attributable ias)
1165                 {
1166                         ListDictionary ld = new ListDictionary ();
1167
1168                         foreach (Attribute a in Attrs)
1169                                 a.Emit (ec, ias, ld);
1170                 }
1171
1172                 public bool Contains (Type t, EmitContext ec)
1173                 {
1174                         return Search (t, ec) != null;
1175                 }
1176
1177                 public Attribute GetClsCompliantAttribute (EmitContext ec)
1178                 {
1179                         return Search (TypeManager.cls_compliant_attribute_type, ec, false);
1180                 }
1181
1182                 /// <summary>
1183                 /// Pulls the IndexerName attribute from an Indexer if it exists.
1184                 /// </summary>
1185                 public string ScanForIndexerName (EmitContext ec)
1186                 {
1187                         Attribute a = Search (TypeManager.indexer_name_type, ec);
1188                         if (a == null)
1189                                 return null;
1190
1191                         // Remove the attribute from the list
1192                         //TODO: It is very close to hack and it can crash here
1193                         Attrs.Remove (a);
1194
1195                         return a.IndexerName_GetIndexerName (ec);
1196                 }
1197
1198         }
1199
1200         /// <summary>
1201         /// Helper class for attribute verification routine.
1202         /// </summary>
1203         sealed class AttributeTester
1204         {
1205                 static PtrHashtable analyzed_types = new PtrHashtable ();
1206                 static PtrHashtable analyzed_types_obsolete = new PtrHashtable ();
1207                 static PtrHashtable analyzed_member_obsolete = new PtrHashtable ();
1208                 static PtrHashtable analyzed_method_excluded = new PtrHashtable ();
1209
1210                 private AttributeTester ()
1211                 {
1212                 }
1213
1214                 /// <summary>
1215                 /// Returns true if parameters of two compared methods are CLS-Compliant.
1216                 /// It tests differing only in ref or out, or in array rank.
1217                 /// </summary>
1218                 public static bool AreOverloadedMethodParamsClsCompliant (Type[] types_a, Type[] types_b) 
1219                 {
1220                         if (types_a == null || types_b == null)
1221                                 return true;
1222
1223                         if (types_a.Length != types_b.Length)
1224                                 return true;
1225
1226                         for (int i = 0; i < types_b.Length; ++i) {
1227                                 Type aType = types_a [i];
1228                                 Type bType = types_b [i];
1229
1230                                 if (aType.IsArray && bType.IsArray && aType.GetArrayRank () != bType.GetArrayRank () && aType.GetElementType () == bType.GetElementType ()) {
1231                                         return false;
1232                                 }
1233
1234                                 Type aBaseType = aType;
1235                                 bool is_either_ref_or_out = false;
1236
1237                                 if (aType.IsByRef || aType.IsPointer) {
1238                                         aBaseType = aType.GetElementType ();
1239                                         is_either_ref_or_out = true;
1240                                 }
1241
1242                                 Type bBaseType = bType;
1243                                 if (bType.IsByRef || bType.IsPointer) 
1244                                 {
1245                                         bBaseType = bType.GetElementType ();
1246                                         is_either_ref_or_out = !is_either_ref_or_out;
1247                                 }
1248
1249                                 if (aBaseType != bBaseType)
1250                                         continue;
1251
1252                                 if (is_either_ref_or_out)
1253                                         return false;
1254                         }
1255                         return true;
1256                 }
1257
1258                 /// <summary>
1259                 /// Goes through all parameters and test if they are CLS-Compliant.
1260                 /// </summary>
1261                 public static bool AreParametersCompliant (Parameter[] fixedParameters, Location loc)
1262                 {
1263                         if (fixedParameters == null)
1264                                 return true;
1265
1266                         foreach (Parameter arg in fixedParameters) {
1267                                 if (!AttributeTester.IsClsCompliant (arg.ParameterType)) {
1268                                         Report.Error_T (3001, loc, arg.GetSignatureForError ());
1269                                         return false;
1270                                 }
1271                         }
1272                         return true;
1273                 }
1274
1275
1276                 /// <summary>
1277                 /// This method tests the CLS compliance of external types. It doesn't test type visibility.
1278                 /// </summary>
1279                 public static bool IsClsCompliant (Type type) 
1280                 {
1281                         if (type == null)
1282                                 return true;
1283
1284                         object type_compliance = analyzed_types[type];
1285                         if (type_compliance != null)
1286                                 return type_compliance == TRUE;
1287
1288                         if (type.IsPointer) {
1289                                 analyzed_types.Add (type, null);
1290                                 return false;
1291                         }
1292
1293                         bool result;
1294                         if (type.IsArray || type.IsByRef)       {
1295                                 result = IsClsCompliant (TypeManager.GetElementType (type));
1296                         } else {
1297                                 result = AnalyzeTypeCompliance (type);
1298                         }
1299                         analyzed_types.Add (type, result ? TRUE : FALSE);
1300                         return result;
1301                 }                
1302
1303                 static object TRUE = new object ();
1304                 static object FALSE = new object ();
1305
1306                 /// <summary>
1307                 /// Non-hierarchical CLS Compliance analyzer
1308                 /// </summary>
1309                 public static bool IsComplianceRequired (MemberInfo mi, DeclSpace ds)
1310                 {
1311                         DeclSpace temp_ds = TypeManager.LookupDeclSpace (mi.DeclaringType);
1312
1313                         // Type is external, we can get attribute directly
1314                         if (temp_ds == null) {
1315                                 object[] cls_attribute = mi.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
1316                                 return (cls_attribute.Length == 1 && ((CLSCompliantAttribute)cls_attribute[0]).IsCompliant);
1317                         }
1318
1319                         string tmp_name;
1320                         // Interface doesn't store full name
1321                         if (temp_ds is Interface)
1322                                 tmp_name = mi.Name;
1323                         else
1324                                 tmp_name = String.Concat (temp_ds.Name, ".", mi.Name);
1325
1326                         MemberCore mc = temp_ds.GetDefinition (tmp_name) as MemberCore;
1327                         return mc.IsClsCompliaceRequired (ds);
1328                 }
1329
1330                 public static void VerifyModulesClsCompliance ()
1331                 {
1332                         Module[] modules = TypeManager.Modules;
1333                         if (modules == null)
1334                                 return;
1335
1336                         // The first module is generated assembly
1337                         for (int i = 1; i < modules.Length; ++i) {
1338                                 Module module = modules [i];
1339                                 if (!IsClsCompliant (module)) {
1340                                         Report.Error_T (3013, module.Name);
1341                                         return;
1342                                 }
1343                         }
1344                 }
1345
1346                 static bool IsClsCompliant (ICustomAttributeProvider attribute_provider) 
1347                 {
1348                         object[] CompliantAttribute = attribute_provider.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
1349                         if (CompliantAttribute.Length == 0)
1350                                 return false;
1351
1352                         return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
1353                 }
1354
1355                 static bool AnalyzeTypeCompliance (Type type)
1356                 {
1357                         DeclSpace ds = TypeManager.LookupDeclSpace (type);
1358                         if (ds != null) {
1359                                 return ds.IsClsCompliaceRequired (ds.Parent);
1360                         }
1361
1362                         if (type.IsGenericParameter)
1363                                 return false;
1364
1365                         object[] CompliantAttribute = type.GetCustomAttributes (TypeManager.cls_compliant_attribute_type, false);
1366                         if (CompliantAttribute.Length == 0) 
1367                                 return IsClsCompliant (type.Assembly);
1368
1369                         return ((CLSCompliantAttribute)CompliantAttribute[0]).IsCompliant;
1370                 }
1371
1372                 /// <summary>
1373                 /// Returns instance of ObsoleteAttribute when type is obsolete
1374                 /// </summary>
1375                 public static ObsoleteAttribute GetObsoleteAttribute (Type type)
1376                 {
1377                         object type_obsolete = analyzed_types_obsolete [type];
1378                         if (type_obsolete == FALSE)
1379                                 return null;
1380
1381                         if (type_obsolete != null)
1382                                 return (ObsoleteAttribute)type_obsolete;
1383
1384                         ObsoleteAttribute result = null;
1385                         if (type.IsByRef || type.IsArray || type.IsPointer) {
1386                                 result = GetObsoleteAttribute (TypeManager.GetElementType (type));
1387                         } else if (type.IsGenericParameter || type.IsGenericInstance)
1388                                 return null;
1389                         else {
1390                                 DeclSpace type_ds = TypeManager.LookupDeclSpace (type);
1391
1392                                 // Type is external, we can get attribute directly
1393                                 if (type_ds == null) {
1394                                         object[] attribute = type.GetCustomAttributes (TypeManager.obsolete_attribute_type, false);
1395                                         if (attribute.Length == 1)
1396                                                 result = (ObsoleteAttribute)attribute [0];
1397                                 } else {
1398                                         result = type_ds.GetObsoleteAttribute (type_ds);
1399                                 }
1400                         }
1401
1402                         analyzed_types_obsolete.Add (type, result == null ? FALSE : result);
1403                         return result;
1404                 }
1405
1406                 /// <summary>
1407                 /// Returns instance of ObsoleteAttribute when method is obsolete
1408                 /// </summary>
1409                 public static ObsoleteAttribute GetMethodObsoleteAttribute (MethodBase mb)
1410                 {
1411                         IMethodData mc = TypeManager.GetMethod (mb);
1412                         if (mc != null) 
1413                                 return mc.GetObsoleteAttribute ();
1414
1415                         return GetMemberObsoleteAttribute (mb);
1416                 }
1417
1418                 /// <summary>
1419                 /// Returns instance of ObsoleteAttribute when member is obsolete
1420                 /// </summary>
1421                 public static ObsoleteAttribute GetMemberObsoleteAttribute (MemberInfo mi)
1422                 {
1423                         object type_obsolete = analyzed_member_obsolete [mi];
1424                         if (type_obsolete == FALSE)
1425                                 return null;
1426
1427                         if (type_obsolete != null)
1428                                 return (ObsoleteAttribute)type_obsolete;
1429
1430                         if ((mi.DeclaringType is TypeBuilder) || mi.DeclaringType.IsGenericInstance)
1431                                 return null;
1432
1433                         ObsoleteAttribute oa = System.Attribute.GetCustomAttribute (mi, TypeManager.obsolete_attribute_type, false) as ObsoleteAttribute;
1434                         analyzed_member_obsolete.Add (mi, oa == null ? FALSE : oa);
1435                         return oa;
1436                 }
1437
1438                 /// <summary>
1439                 /// Common method for Obsolete error/warning reporting.
1440                 /// </summary>
1441                 public static void Report_ObsoleteMessage (ObsoleteAttribute oa, string member, Location loc)
1442                 {
1443                         if (oa.IsError) {
1444                                 Report.Error_T (619, loc, member, oa.Message);
1445                                 return;
1446                         }
1447
1448                         if (oa.Message == null) {
1449                                 Report.Warning_T (612, loc, member);
1450                                 return;
1451                         }
1452                         Report.Warning_T (618, loc, member, oa.Message);
1453                 }
1454
1455                 public static bool IsConditionalMethodExcluded (MethodBase mb)
1456                 {
1457                         object excluded = analyzed_method_excluded [mb];
1458                         if (excluded != null)
1459                                 return excluded == TRUE ? true : false;
1460
1461                         if (mb.Mono_IsInflatedMethod)
1462                                 return false;
1463                         
1464                         ConditionalAttribute[] attrs = mb.GetCustomAttributes (TypeManager.conditional_attribute_type, true) as ConditionalAttribute[];
1465                         if (attrs.Length == 0) {
1466                                 analyzed_method_excluded.Add (mb, FALSE);
1467                                 return false;
1468                         }
1469
1470                         foreach (ConditionalAttribute a in attrs) {
1471                                 if (RootContext.AllDefines.Contains (a.ConditionString)) {
1472                                         analyzed_method_excluded.Add (mb, FALSE);
1473                                         return false;
1474                                 }
1475                         }
1476                         analyzed_method_excluded.Add (mb, TRUE);
1477                         return true;
1478                 }
1479         }
1480 }