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