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