2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[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 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2003-2008 Novell, Inc.
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.Security; 
22 using System.Security.Permissions;
23 using System.Text;
24 using System.IO;
25
26 namespace Mono.CSharp {
27
28         /// <summary>
29         ///   Base class for objects that can have Attributes applied to them.
30         /// </summary>
31         public abstract class Attributable {
32                 /// <summary>
33                 ///   Attributes for this type
34                 /// </summary>
35                 protected Attributes attributes;
36
37                 public Attributable (Attributes attrs)
38                 {
39                         if (attrs != null)
40                                 OptAttributes = attrs;
41                 }
42
43                 public Attributes OptAttributes 
44                 {
45                         get {
46                                 return attributes;
47                         }
48                         set {
49                                 attributes = value;
50
51                                 if (attributes != null) {
52                                         attributes.AttachTo (this);
53                                 }
54                         }
55                 }
56
57                 /// <summary>
58                 /// Use member-specific procedure to apply attribute @a in @cb to the entity being built in @builder
59                 /// </summary>
60                 public abstract void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb, PredefinedAttributes pa);
61
62                 /// <summary>
63                 /// Returns one AttributeTarget for this element.
64                 /// </summary>
65                 public abstract AttributeTargets AttributeTargets { get; }
66
67                 public abstract IResolveContext ResolveContext { get; }
68
69                 public abstract bool IsClsComplianceRequired ();
70
71                 /// <summary>
72                 /// Gets list of valid attribute targets for explicit target declaration.
73                 /// The first array item is default target. Don't break this rule.
74                 /// </summary>
75                 public abstract string[] ValidAttributeTargets { get; }
76         };
77
78         public class Attribute : Expression
79         {
80                 //
81                 // Wraps owner resolve context, the owner is an attribute
82                 // parent and the rest is exactly same
83                 //
84                 struct AttributeResolveContext : IResolveContext
85                 {
86                         readonly IResolveContext rc;
87
88                         public AttributeResolveContext (IResolveContext rc)
89                         {
90                                 this.rc = rc;
91                         }
92
93                         #region IResolveContext Members
94
95                         public DeclSpace DeclContainer {
96                                 get {
97                                         DeclSpace ds = rc as DeclSpace;
98                                         if (ds == null)
99                                                 return rc.DeclContainer;
100
101                                         return ds;
102                                 }
103                         }
104
105                         public bool IsInObsoleteScope {
106                                 get { return rc.IsInObsoleteScope; }
107                         }
108
109                         public bool IsInUnsafeScope {
110                                 get { return rc.IsInUnsafeScope; }
111                         }
112
113                         public DeclSpace GenericDeclContainer {
114                                 get { return rc.GenericDeclContainer; }
115                         }
116
117                         #endregion
118                 }
119
120                 public readonly string ExplicitTarget;
121                 public AttributeTargets Target;
122
123                 // TODO: remove this member
124                 public readonly string    Name;
125                 public readonly Expression LeftExpr;
126                 public readonly string Identifier;
127
128                 Arguments PosArguments;
129                 Arguments NamedArguments;
130
131                 bool resolve_error;
132                 readonly bool nameEscaped;
133
134                 // It can contain more onwers when the attribute is applied to multiple fiels.
135                 protected Attributable[] owners;
136
137                 static readonly AttributeUsageAttribute DefaultUsageAttribute = new AttributeUsageAttribute (AttributeTargets.All);
138                 static Assembly orig_sec_assembly;
139                 public static readonly object[] EmptyObject = new object [0];
140
141                 // non-null if named args present after Resolve () is called
142                 PropertyInfo [] prop_info_arr;
143                 FieldInfo [] field_info_arr;
144                 object [] field_values_arr;
145                 object [] prop_values_arr;
146                 object [] pos_values;
147
148                 static PtrHashtable usage_attr_cache;
149                 // Cache for parameter-less attributes
150                 static PtrHashtable att_cache;
151
152                 public Attribute (string target, Expression left_expr, string identifier, Arguments[] args, Location loc, bool nameEscaped)
153                 {
154                         LeftExpr = left_expr;
155                         Identifier = identifier;
156                         Name = LeftExpr == null ? identifier : LeftExpr + "." + identifier;
157                         if (args != null) {
158                                 PosArguments = args [0];
159                                 NamedArguments = args [1];                              
160                         }
161                         this.loc = loc;
162                         ExplicitTarget = target;
163                         this.nameEscaped = nameEscaped;
164                 }
165
166                 public Attribute Clone ()
167                 {
168                         Attribute a = new Attribute (ExplicitTarget, LeftExpr, Identifier, null, loc, nameEscaped);
169                         a.PosArguments = PosArguments;
170                         a.NamedArguments = NamedArguments;
171                         return a;
172                 }
173
174                 static Attribute ()
175                 {
176                         Reset ();
177                 }
178
179                 public static void Reset ()
180                 {
181                         usage_attr_cache = new PtrHashtable ();
182                         att_cache = new PtrHashtable ();
183                 }
184
185                 public virtual void AttachTo (Attributable owner)
186                 {
187                         if (this.owners == null) {
188                                 this.owners = new Attributable[1] { owner };
189                                 return;
190                         }
191
192                         // When the same attribute is attached to multiple fiels
193                         // we use this extra_owners as a list of owners. The attribute
194                         // then can be removed because will be emitted when first owner
195                         // is served
196                         Attributable[] new_array = new Attributable [this.owners.Length + 1];
197                         owners.CopyTo (new_array, 0);
198                         new_array [owners.Length] = owner;
199                         this.owners = new_array;
200                         owner.OptAttributes = null;
201                 }
202
203                 void Error_InvalidNamedArgument (NamedArgument name)
204                 {
205                         Report.Error (617, name.Name.Location, "`{0}' is not a valid named attribute argument. Named attribute arguments " +
206                                       "must be fields which are not readonly, static, const or read-write properties which are " +
207                                       "public and not static",
208                               name.Name.Value);
209                 }
210
211                 void Error_InvalidNamedArgumentType (NamedArgument name)
212                 {
213                         Report.Error (655, name.Name.Location,
214                                 "`{0}' is not a valid named attribute argument because it is not a valid attribute parameter type",
215                                 name.Name.Value);
216                 }
217
218                 public static void Error_AttributeArgumentNotValid (Location loc)
219                 {
220                         Report.Error (182, loc,
221                                       "An attribute argument must be a constant expression, typeof " +
222                                       "expression or array creation expression");
223                 }
224                 
225                 static void Error_TypeParameterInAttribute (Location loc)
226                 {
227                         Report.Error (
228                                 -202, loc, "Can not use a type parameter in an attribute");
229                 }
230
231                 public void Error_MissingGuidAttribute ()
232                 {
233                         Report.Error (596, Location, "The Guid attribute must be specified with the ComImport attribute");
234                 }
235
236                 public void Error_MisusedExtensionAttribute ()
237                 {
238                         Report.Error (1112, Location, "Do not use `{0}' directly. Use parameter modifier `this' instead", GetSignatureForError ());
239                 }
240
241                 /// <summary>
242                 /// This is rather hack. We report many emit attribute error with same error to be compatible with
243                 /// csc. But because csc has to report them this way because error came from ilasm we needn't.
244                 /// </summary>
245                 public void Error_AttributeEmitError (string inner)
246                 {
247                         Report.Error (647, Location, "Error during emitting `{0}' attribute. The reason is `{1}'",
248                                       TypeManager.CSharpName (Type), inner);
249                 }
250
251                 public void Error_InvalidSecurityParent ()
252                 {
253                         Error_AttributeEmitError ("it is attached to invalid parent");
254                 }
255
256                 Attributable Owner {
257                         get {
258                                 return owners [0];
259                         }
260                 }
261
262                 protected virtual TypeExpr ResolveAsTypeTerminal (Expression expr, IResolveContext ec, bool silent)
263                 {
264                         return expr.ResolveAsTypeTerminal (ec, silent);
265                 }
266
267                 Type ResolvePossibleAttributeType (string name, bool silent, ref bool is_attr)
268                 {
269                         IResolveContext rc = new AttributeResolveContext (Owner.ResolveContext);
270
271                         TypeExpr te;
272                         if (LeftExpr == null) {
273                                 te = ResolveAsTypeTerminal (new SimpleName (name, Location), rc, silent);
274                         } else {
275                                 te = ResolveAsTypeTerminal (new MemberAccess (LeftExpr, name), rc, silent);
276                         }
277
278                         if (te == null)
279                                 return null;
280
281                         Type t = te.Type;
282                         if (TypeManager.IsSubclassOf (t, TypeManager.attribute_type)) {
283                                 is_attr = true;
284                         } else if (!silent) {
285                                 Report.SymbolRelatedToPreviousError (t);
286                                 Report.Error (616, Location, "`{0}': is not an attribute class", TypeManager.CSharpName (t));
287                         }
288                         return t;
289                 }
290
291                 /// <summary>
292                 ///   Tries to resolve the type of the attribute. Flags an error if it can't, and complain is true.
293                 /// </summary>
294                 void ResolveAttributeType ()
295                 {
296                         bool t1_is_attr = false;
297                         Type t1 = ResolvePossibleAttributeType (Identifier, true, ref t1_is_attr);
298
299                         bool t2_is_attr = false;
300                         Type t2 = nameEscaped ? null :
301                                 ResolvePossibleAttributeType (Identifier + "Attribute", true, ref t2_is_attr);
302
303                         if (t1_is_attr && t2_is_attr) {
304                                 Report.Error (1614, Location, "`{0}' is ambiguous between `{0}' and `{0}Attribute'. " +
305                                               "Use either `@{0}' or `{0}Attribute'", GetSignatureForError ());
306                                 resolve_error = true;
307                                 return;
308                         }
309
310                         if (t1_is_attr) {
311                                 Type = t1;
312                                 return;
313                         }
314
315                         if (t2_is_attr) {
316                                 Type = t2;
317                                 return;
318                         }
319
320                         if (t1 == null && t2 == null)
321                                 ResolvePossibleAttributeType (Identifier, false, ref t1_is_attr);
322                         if (t1 != null)
323                                 ResolvePossibleAttributeType (Identifier, false, ref t1_is_attr);
324                         if (t2 != null)
325                                 ResolvePossibleAttributeType (Identifier + "Attribute", false, ref t2_is_attr);
326
327                         resolve_error = true;
328                 }
329
330                 public virtual Type ResolveType ()
331                 {
332                         if (Type == null && !resolve_error)
333                                 ResolveAttributeType ();
334                         return Type;
335                 }
336
337                 public override string GetSignatureForError ()
338                 {
339                         if (Type != null)
340                                 return TypeManager.CSharpName (Type);
341
342                         return LeftExpr == null ? Identifier : LeftExpr.GetSignatureForError () + "." + Identifier;
343                 }
344
345                 public bool HasSecurityAttribute {
346                         get {
347                                 PredefinedAttribute pa = PredefinedAttributes.Get.Security;
348                                 return pa.IsDefined && TypeManager.IsSubclassOf (type, pa.Type);
349                         }
350                 }
351
352                 public bool IsValidSecurityAttribute ()
353                 {
354                         return HasSecurityAttribute && IsSecurityActionValid (false);
355                 }
356
357                 static bool IsValidArgumentType (Type t)
358                 {
359                         if (t.IsArray)
360                                 t = TypeManager.GetElementType (t);
361
362                         return t == TypeManager.string_type ||
363                                 TypeManager.IsPrimitiveType (t) ||
364                                 TypeManager.IsEnumType (t) ||
365                                 t == TypeManager.object_type ||
366                                 t == TypeManager.type_type;
367                 }
368
369                 void ApplyModuleCharSet ()
370                 {
371                         if (Type != PredefinedAttributes.Get.DllImport)
372                                 return;
373
374                         if (!RootContext.ToplevelTypes.HasDefaultCharSet)
375                                 return;
376
377                         const string CharSetEnumMember = "CharSet";
378                         if (NamedArguments == null) {
379                                 NamedArguments = new Arguments (1);
380                         } else {
381                                 foreach (NamedArgument a in NamedArguments) {
382                                         if (a.Name.Value == CharSetEnumMember)
383                                                 return;
384                                 }
385                         }
386                         
387                         NamedArguments.Add (new NamedArgument (new LocatedToken (loc, CharSetEnumMember),
388                                 Constant.CreateConstant (typeof (CharSet), RootContext.ToplevelTypes.DefaultCharSet, Location)));
389                 }
390
391                 public CustomAttributeBuilder Resolve ()
392                 {
393                         if (resolve_error)
394                                 return null;
395
396                         resolve_error = true;
397
398                         if (Type == null) {
399                                 ResolveAttributeType ();
400                                 if (Type == null)
401                                         return null;
402                         }
403
404                         if (Type.IsAbstract) {
405                                 Report.Error (653, Location, "Cannot apply attribute class `{0}' because it is abstract", GetSignatureForError ());
406                                 return null;
407                         }
408
409                         ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (Type);
410                         if (obsolete_attr != null) {
411                                 AttributeTester.Report_ObsoleteMessage (obsolete_attr, TypeManager.CSharpName (Type), Location);
412                         }
413
414                         if (PosArguments == null && NamedArguments == null) {
415                                 object o = att_cache [Type];
416                                 if (o != null) {
417                                         resolve_error = false;
418                                         return (CustomAttributeBuilder)o;
419                                 }
420                         }
421
422                         Attributable owner = Owner;
423                         DeclSpace ds = owner.ResolveContext as DeclSpace;
424                         if (ds == null)
425                                 ds = owner.ResolveContext.DeclContainer;
426                         
427                         EmitContext ec = new EmitContext (owner.ResolveContext, ds, ds,
428                                 Location, null, typeof (Attribute), ds.ModFlags, false);
429                         ec.IsAnonymousMethodAllowed = false;
430
431                         ConstructorInfo ctor = ResolveConstructor (ec);
432                         if (ctor == null) {
433                                 if (Type is TypeBuilder && 
434                                     TypeManager.LookupDeclSpace (Type).MemberCache == null)
435                                         // The attribute type has been DefineType'd, but not Defined.  Let's not treat it as an error.
436                                         // It'll be resolved again when the attached-to entity is emitted.
437                                         resolve_error = false;
438                                 return null;
439                         }
440
441                         ApplyModuleCharSet ();
442
443                         CustomAttributeBuilder cb;
444                         try {
445                                 // SRE does not allow private ctor but we want to report all source code errors
446                                 if (ctor.IsPrivate)
447                                         return null;
448
449                                 if (NamedArguments == null) {
450                                         cb = new CustomAttributeBuilder (ctor, pos_values);
451
452                                         if (pos_values.Length == 0)
453                                                 att_cache.Add (Type, cb);
454
455                                         resolve_error = false;
456                                         return cb;
457                                 }
458
459                                 if (!ResolveNamedArguments (ec)) {
460                                         return null;
461                                 }
462
463                                 cb = new CustomAttributeBuilder (ctor, pos_values,
464                                                 prop_info_arr, prop_values_arr,
465                                                 field_info_arr, field_values_arr);
466
467                                 resolve_error = false;
468                                 return cb;
469                         }
470                         catch (Exception) {
471                                 Error_AttributeArgumentNotValid (Location);
472                                 return null;
473                         }
474                 }
475
476                 protected virtual ConstructorInfo ResolveConstructor (EmitContext ec)
477                 {
478                         if (PosArguments != null)
479                                 PosArguments.Resolve (ec);
480
481                         MethodGroupExpr mg = MemberLookupFinal (ec, ec.ContainerType,
482                                 Type, ConstructorInfo.ConstructorName, MemberTypes.Constructor,
483                                 BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly,
484                                 Location) as MethodGroupExpr;
485
486                         if (mg == null)
487                                 throw new NotImplementedException ();
488
489                         mg = mg.OverloadResolve (ec, ref PosArguments, false, Location);
490                         if (mg == null)
491                                 return null;
492                         
493                         ConstructorInfo constructor = (ConstructorInfo)mg;
494                         if (PosArguments == null) {
495                                 pos_values = EmptyObject;
496                                 return constructor;
497                         }
498
499                         AParametersCollection pd = TypeManager.GetParameterData (constructor);
500
501                         if (!PosArguments.GetAttributableValue (ec, out pos_values))
502                                 return null;
503
504                         // Here we do the checks which should be done by corlib or by runtime.
505                         // However Zoltan doesn't like it and every Mono compiler has to do it again.
506                         PredefinedAttributes pa = PredefinedAttributes.Get;
507                         if (Type == pa.Guid) {
508                                 try {
509                                         new Guid ((string)pos_values [0]);
510                                 }
511                                 catch (Exception e) {
512                                         Error_AttributeEmitError (e.Message);
513                                         return null;
514                                 }
515                         }
516
517                         if (Type == pa.AttributeUsage && (int)pos_values [0] == 0) {
518                                 Report.Error (591, Location, "Invalid value for argument to `System.AttributeUsage' attribute");
519                                 return null;
520                         }
521
522                         if (Type == pa.IndexerName || Type == pa.Conditional) {
523                                 string v = pos_values [0] as string;
524                                 if (!Tokenizer.IsValidIdentifier (v) || Tokenizer.IsKeyword (v)) {
525                                         Report.Error (633, PosArguments [0].Expr.Location,
526                                                 "The argument to the `{0}' attribute must be a valid identifier", GetSignatureForError ());
527                                         return null;
528                                 }
529                         }
530
531                         if (Type == pa.MethodImpl && pos_values.Length == 1 &&
532                                 pd.Types [0] == TypeManager.short_type &&
533                                 !System.Enum.IsDefined (typeof (MethodImplOptions), pos_values [0].ToString ())) {
534                                 Error_AttributeEmitError ("Incorrect argument value.");
535                                 return null;
536                         }
537
538                         return constructor;
539                 }
540
541                 protected virtual bool ResolveNamedArguments (EmitContext ec)
542                 {
543                         int named_arg_count = NamedArguments.Count;
544
545                         ArrayList field_infos = new ArrayList (named_arg_count);
546                         ArrayList prop_infos  = new ArrayList (named_arg_count);
547                         ArrayList field_values = new ArrayList (named_arg_count);
548                         ArrayList prop_values = new ArrayList (named_arg_count);
549
550                         ArrayList seen_names = new ArrayList (named_arg_count);
551                         
552                         foreach (NamedArgument a in NamedArguments) {
553                                 string name = a.Name.Value;
554                                 if (seen_names.Contains (name)) {
555                                         Report.Error (643, a.Name.Location, "Duplicate named attribute `{0}' argument", name);
556                                         continue;
557                                 }                       
558         
559                                 seen_names.Add (name);
560
561                                 a.Resolve (ec);
562
563                                 Expression member = Expression.MemberLookup (
564                                         ec.ContainerType, Type, name,
565                                         MemberTypes.All,
566                                         BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static,
567                                         Location);
568
569                                 if (member == null) {
570                                         member = Expression.MemberLookup (ec.ContainerType, Type, name,
571                                                 MemberTypes.All, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static,
572                                                 Location);
573
574                                         if (member != null) {
575                                                 Report.SymbolRelatedToPreviousError (member.Type);
576                                                 Expression.ErrorIsInaccesible (Location, member.GetSignatureForError ());
577                                                 return false;
578                                         }
579                                 }
580
581                                 if (member == null){
582                                         Expression.Error_TypeDoesNotContainDefinition (Location, Type, name);
583                                         return false;
584                                 }
585                                 
586                                 if (!(member is PropertyExpr || member is FieldExpr)) {
587                                         Error_InvalidNamedArgument (a);
588                                         return false;
589                                 }
590
591                                 if (a.Expr is TypeParameterExpr){
592                                         Error_TypeParameterInAttribute (Location);
593                                         return false;
594                                 }
595
596                                 ObsoleteAttribute obsolete_attr;
597
598                                 if (member is PropertyExpr) {
599                                         PropertyInfo pi = ((PropertyExpr) member).PropertyInfo;
600
601                                         if (!pi.CanWrite || !pi.CanRead || pi.GetGetMethod ().IsStatic) {
602                                                 Report.SymbolRelatedToPreviousError (pi);
603                                                 Error_InvalidNamedArgument (a);
604                                                 return false;
605                                         }
606
607                                         if (!IsValidArgumentType (member.Type)) {
608                                                 Report.SymbolRelatedToPreviousError (pi);
609                                                 Error_InvalidNamedArgumentType (a);
610                                                 return false;
611                                         }
612
613                                         object value;
614                                         if (!a.Expr.GetAttributableValue (ec, member.Type, out value))
615                                                 return false;
616
617                                         PropertyBase pb = TypeManager.GetProperty (pi);
618                                         if (pb != null)
619                                                 obsolete_attr = pb.GetObsoleteAttribute ();
620                                         else
621                                                 obsolete_attr = AttributeTester.GetMemberObsoleteAttribute (pi);
622
623                                         prop_values.Add (value);
624                                         prop_infos.Add (pi);
625                                         
626                                 } else {
627                                         FieldInfo fi = ((FieldExpr) member).FieldInfo;
628
629                                         if (fi.IsInitOnly || fi.IsStatic) {
630                                                 Error_InvalidNamedArgument (a);
631                                                 return false;
632                                         }
633
634                                         if (!IsValidArgumentType (member.Type)) {
635                                                 Report.SymbolRelatedToPreviousError (fi);
636                                                 Error_InvalidNamedArgumentType (a);
637                                                 return false;
638                                         }
639
640                                         object value;
641                                         if (!a.Expr.GetAttributableValue (ec, member.Type, out value))
642                                                 return false;
643
644                                         FieldBase fb = TypeManager.GetField (fi);
645                                         if (fb != null)
646                                                 obsolete_attr = fb.GetObsoleteAttribute ();
647                                         else
648                                                 obsolete_attr = AttributeTester.GetMemberObsoleteAttribute (fi);
649
650                                         field_values.Add (value);                                       
651                                         field_infos.Add (fi);
652                                 }
653
654                                 if (obsolete_attr != null && !Owner.ResolveContext.IsInObsoleteScope)
655                                         AttributeTester.Report_ObsoleteMessage (obsolete_attr, member.GetSignatureForError (), member.Location);
656                         }
657
658                         prop_info_arr = new PropertyInfo [prop_infos.Count];
659                         field_info_arr = new FieldInfo [field_infos.Count];
660                         field_values_arr = new object [field_values.Count];
661                         prop_values_arr = new object [prop_values.Count];
662
663                         field_infos.CopyTo  (field_info_arr, 0);
664                         field_values.CopyTo (field_values_arr, 0);
665
666                         prop_values.CopyTo  (prop_values_arr, 0);
667                         prop_infos.CopyTo   (prop_info_arr, 0);
668
669                         return true;
670                 }
671
672                 /// <summary>
673                 ///   Get a string containing a list of valid targets for the attribute 'attr'
674                 /// </summary>
675                 public string GetValidTargets ()
676                 {
677                         StringBuilder sb = new StringBuilder ();
678                         AttributeTargets targets = GetAttributeUsage (Type).ValidOn;
679
680                         if ((targets & AttributeTargets.Assembly) != 0)
681                                 sb.Append ("assembly, ");
682
683                         if ((targets & AttributeTargets.Module) != 0)
684                                 sb.Append ("module, ");
685
686                         if ((targets & AttributeTargets.Class) != 0)
687                                 sb.Append ("class, ");
688
689                         if ((targets & AttributeTargets.Struct) != 0)
690                                 sb.Append ("struct, ");
691
692                         if ((targets & AttributeTargets.Enum) != 0)
693                                 sb.Append ("enum, ");
694
695                         if ((targets & AttributeTargets.Constructor) != 0)
696                                 sb.Append ("constructor, ");
697
698                         if ((targets & AttributeTargets.Method) != 0)
699                                 sb.Append ("method, ");
700
701                         if ((targets & AttributeTargets.Property) != 0)
702                                 sb.Append ("property, indexer, ");
703
704                         if ((targets & AttributeTargets.Field) != 0)
705                                 sb.Append ("field, ");
706
707                         if ((targets & AttributeTargets.Event) != 0)
708                                 sb.Append ("event, ");
709
710                         if ((targets & AttributeTargets.Interface) != 0)
711                                 sb.Append ("interface, ");
712
713                         if ((targets & AttributeTargets.Parameter) != 0)
714                                 sb.Append ("parameter, ");
715
716                         if ((targets & AttributeTargets.Delegate) != 0)
717                                 sb.Append ("delegate, ");
718
719                         if ((targets & AttributeTargets.ReturnValue) != 0)
720                                 sb.Append ("return, ");
721
722 #if NET_2_0
723                         if ((targets & AttributeTargets.GenericParameter) != 0)
724                                 sb.Append ("type parameter, ");
725 #endif                  
726                         return sb.Remove (sb.Length - 2, 2).ToString ();
727                 }
728
729                 /// <summary>
730                 /// Returns AttributeUsage attribute based on types hierarchy
731                 /// </summary>
732                 static AttributeUsageAttribute GetAttributeUsage (Type type)
733                 {
734                         AttributeUsageAttribute ua = usage_attr_cache [type] as AttributeUsageAttribute;
735                         if (ua != null)
736                                 return ua;
737
738                         Class attr_class = TypeManager.LookupClass (type);
739                         PredefinedAttribute pa = PredefinedAttributes.Get.AttributeUsage;
740
741                         if (attr_class == null) {
742                                 if (!pa.IsDefined)
743                                         return new AttributeUsageAttribute (0);
744
745                                 object[] usage_attr = type.GetCustomAttributes (pa.Type, true);
746                                 ua = (AttributeUsageAttribute)usage_attr [0];
747                                 usage_attr_cache.Add (type, ua);
748                                 return ua;
749                         }
750
751                         Attribute a = null;
752                         if (attr_class.OptAttributes != null)
753                                 a = attr_class.OptAttributes.Search (pa);
754
755                         if (a == null) {
756                                 if (attr_class.TypeBuilder.BaseType != TypeManager.attribute_type)
757                                         ua = GetAttributeUsage (attr_class.TypeBuilder.BaseType);
758                                 else
759                                         ua = DefaultUsageAttribute;
760                         } else {
761                                 ua = a.GetAttributeUsageAttribute ();
762                         }
763
764                         usage_attr_cache.Add (type, ua);
765                         return ua;
766                 }
767
768                 AttributeUsageAttribute GetAttributeUsageAttribute ()
769                 {
770                         if (pos_values == null)
771                                 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
772                                 // But because a lot of attribute class code must be rewritten will be better to wait...
773                                 Resolve ();
774
775                         if (resolve_error)
776                                 return DefaultUsageAttribute;
777
778                         AttributeUsageAttribute usage_attribute = new AttributeUsageAttribute ((AttributeTargets)pos_values [0]);
779
780                         object field = GetPropertyValue ("AllowMultiple");
781                         if (field != null)
782                                 usage_attribute.AllowMultiple = (bool)field;
783
784                         field = GetPropertyValue ("Inherited");
785                         if (field != null)
786                                 usage_attribute.Inherited = (bool)field;
787
788                         return usage_attribute;
789                 }
790
791                 /// <summary>
792                 /// Returns custom name of indexer
793                 /// </summary>
794                 public string GetIndexerAttributeValue ()
795                 {
796                         if (pos_values == null)
797                                 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
798                                 // But because a lot of attribute class code must be rewritten will be better to wait...
799                                 Resolve ();
800
801                         if (resolve_error)
802                                 return null;
803
804                         return pos_values [0] as string;
805                 }
806
807                 /// <summary>
808                 /// Returns condition of ConditionalAttribute
809                 /// </summary>
810                 public string GetConditionalAttributeValue ()
811                 {
812                         if (pos_values == null)
813                                 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
814                                 // But because a lot of attribute class code must be rewritten will be better to wait...
815                                 Resolve ();
816
817                         if (resolve_error)
818                                 return null;
819
820                         return (string)pos_values [0];
821                 }
822
823                 /// <summary>
824                 /// Creates the instance of ObsoleteAttribute from this attribute instance
825                 /// </summary>
826                 public ObsoleteAttribute GetObsoleteAttribute ()
827                 {
828                         if (pos_values == null)
829                                 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
830                                 // But because a lot of attribute class code must be rewritten will be better to wait...
831                                 Resolve ();
832
833                         if (resolve_error)
834                                 return null;
835
836                         if (pos_values == null || pos_values.Length == 0)
837                                 return new ObsoleteAttribute ();
838
839                         if (pos_values.Length == 1)
840                                 return new ObsoleteAttribute ((string)pos_values [0]);
841
842                         return new ObsoleteAttribute ((string)pos_values [0], (bool)pos_values [1]);
843                 }
844
845                 /// <summary>
846                 /// Returns value of CLSCompliantAttribute contructor parameter but because the method can be called
847                 /// before ApplyAttribute. We need to resolve the arguments.
848                 /// This situation occurs when class deps is differs from Emit order.  
849                 /// </summary>
850                 public bool GetClsCompliantAttributeValue ()
851                 {
852                         if (pos_values == null)
853                                 // TODO: It is not neccessary to call whole Resolve (ApplyAttribute does it now) we need only ctor args.
854                                 // But because a lot of attribute class code must be rewritten will be better to wait...
855                                 Resolve ();
856
857                         if (resolve_error)
858                                 return false;
859
860                         return (bool)pos_values [0];
861                 }
862
863                 public Type GetCoClassAttributeValue ()
864                 {
865                         if (pos_values == null)
866                                 Resolve ();
867
868                         if (resolve_error)
869                                 return null;
870
871                         return (Type)pos_values [0];
872                 }
873
874                 public bool CheckTarget ()
875                 {
876                         string[] valid_targets = Owner.ValidAttributeTargets;
877                         if (ExplicitTarget == null || ExplicitTarget == valid_targets [0]) {
878                                 Target = Owner.AttributeTargets;
879                                 return true;
880                         }
881
882                         // TODO: we can skip the first item
883                         if (((IList) valid_targets).Contains (ExplicitTarget)) {
884                                 switch (ExplicitTarget) {
885                                 case "return": Target = AttributeTargets.ReturnValue; return true;
886                                 case "param": Target = AttributeTargets.Parameter; return true;
887                                 case "field": Target = AttributeTargets.Field; return true;
888                                 case "method": Target = AttributeTargets.Method; return true;
889                                 case "property": Target = AttributeTargets.Property; return true;
890                                 }
891                                 throw new InternalErrorException ("Unknown explicit target: " + ExplicitTarget);
892                         }
893                                 
894                         StringBuilder sb = new StringBuilder ();
895                         foreach (string s in valid_targets) {
896                                 sb.Append (s);
897                                 sb.Append (", ");
898                         }
899                         sb.Remove (sb.Length - 2, 2);
900                         Report.Error (657, Location, "`{0}' is not a valid attribute location for this declaration. " +
901                                 "Valid attribute locations for this declaration are `{1}'", ExplicitTarget, sb.ToString ());
902                         return false;
903                 }
904
905                 /// <summary>
906                 /// Tests permitted SecurityAction for assembly or other types
907                 /// </summary>
908                 protected virtual bool IsSecurityActionValid (bool for_assembly)
909                 {
910                         SecurityAction action = GetSecurityActionValue ();
911
912                         switch (action) {
913                         case SecurityAction.Demand:
914                         case SecurityAction.Assert:
915                         case SecurityAction.Deny:
916                         case SecurityAction.PermitOnly:
917                         case SecurityAction.LinkDemand:
918                         case SecurityAction.InheritanceDemand:
919                                 if (!for_assembly)
920                                         return true;
921                                 break;
922
923                         case SecurityAction.RequestMinimum:
924                         case SecurityAction.RequestOptional:
925                         case SecurityAction.RequestRefuse:
926                                 if (for_assembly)
927                                         return true;
928                                 break;
929
930                         default:
931                                 Error_AttributeEmitError ("SecurityAction is out of range");
932                                 return false;
933                         }
934
935                         Error_AttributeEmitError (String.Concat ("SecurityAction `", action, "' is not valid for this declaration"));
936                         return false;
937                 }
938
939                 System.Security.Permissions.SecurityAction GetSecurityActionValue ()
940                 {
941                         return (SecurityAction)pos_values [0];
942                 }
943
944                 /// <summary>
945                 /// Creates instance of SecurityAttribute class and add result of CreatePermission method to permission table.
946                 /// </summary>
947                 /// <returns></returns>
948                 public void ExtractSecurityPermissionSet (ListDictionary permissions)
949                 {
950                         Type orig_assembly_type = null;
951
952                         if (TypeManager.LookupDeclSpace (Type) != null) {
953                                 if (!RootContext.StdLib) {
954                                         orig_assembly_type = Type.GetType (Type.FullName);
955                                 } else {
956                                         string orig_version_path = Environment.GetEnvironmentVariable ("__SECURITY_BOOTSTRAP_DB");
957                                         if (orig_version_path == null) {
958                                                 Error_AttributeEmitError ("security custom attributes can not be referenced from defining assembly");
959                                                 return;
960                                         }
961
962                                         if (orig_sec_assembly == null) {
963                                                 string file = Path.Combine (orig_version_path, Driver.OutputFile);
964                                                 orig_sec_assembly = Assembly.LoadFile (file);
965                                         }
966
967                                         orig_assembly_type = orig_sec_assembly.GetType (Type.FullName, true);
968                                         if (orig_assembly_type == null) {
969                                                 Report.Warning (-112, 1, Location, "Self-referenced security attribute `{0}' " +
970                                                                 "was not found in previous version of assembly");
971                                                 return;
972                                         }
973                                 }
974                         }
975
976                         SecurityAttribute sa;
977                         // For all non-selfreferencing security attributes we can avoid all hacks
978                         if (orig_assembly_type == null) {
979                                 sa = (SecurityAttribute) Activator.CreateInstance (Type, pos_values);
980
981                                 if (prop_info_arr != null) {
982                                         for (int i = 0; i < prop_info_arr.Length; ++i) {
983                                                 PropertyInfo pi = prop_info_arr [i];
984                                                 pi.SetValue (sa, prop_values_arr [i], null);
985                                         }
986                                 }
987                         } else {
988                                 // HACK: All security attributes have same ctor syntax
989                                 sa = (SecurityAttribute) Activator.CreateInstance (orig_assembly_type, new object[] { GetSecurityActionValue () } );
990
991                                 // All types are from newly created assembly but for invocation with old one we need to convert them
992                                 if (prop_info_arr != null) {
993                                         for (int i = 0; i < prop_info_arr.Length; ++i) {
994                                                 PropertyInfo emited_pi = prop_info_arr [i];
995                                                 // FIXME: We are missing return type filter
996                                                 // TODO: pi can be null
997                                                 PropertyInfo pi = orig_assembly_type.GetProperty (emited_pi.Name);
998
999                                                 object old_instance = TypeManager.IsEnumType (pi.PropertyType) ?
1000                                                         System.Enum.ToObject (pi.PropertyType, prop_values_arr [i]) :
1001                                                         prop_values_arr [i];
1002
1003                                                 pi.SetValue (sa, old_instance, null);
1004                                         }
1005                                 }
1006                         }
1007
1008                         IPermission perm;
1009                         perm = sa.CreatePermission ();
1010                         SecurityAction action = GetSecurityActionValue ();
1011
1012                         // IS is correct because for corlib we are using an instance from old corlib
1013                         if (!(perm is System.Security.CodeAccessPermission)) {
1014                                 switch (action) {
1015                                 case SecurityAction.Demand:
1016                                         action = (SecurityAction)13;
1017                                         break;
1018                                 case SecurityAction.LinkDemand:
1019                                         action = (SecurityAction)14;
1020                                         break;
1021                                 case SecurityAction.InheritanceDemand:
1022                                         action = (SecurityAction)15;
1023                                         break;
1024                                 }
1025                         }
1026
1027                         PermissionSet ps = (PermissionSet)permissions [action];
1028                         if (ps == null) {
1029                                 if (sa is PermissionSetAttribute)
1030                                         ps = new PermissionSet (sa.Unrestricted ? PermissionState.Unrestricted : PermissionState.None);
1031                                 else
1032                                         ps = new PermissionSet (PermissionState.None);
1033
1034                                 permissions.Add (action, ps);
1035                         } else if (!ps.IsUnrestricted () && (sa is PermissionSetAttribute) && sa.Unrestricted) {
1036                                 ps = ps.Union (new PermissionSet (PermissionState.Unrestricted));
1037                                 permissions [action] = ps;
1038                         }
1039                         ps.AddPermission (perm);
1040                 }
1041
1042                 public object GetPropertyValue (string name)
1043                 {
1044                         if (prop_info_arr == null)
1045                                 return null;
1046
1047                         for (int i = 0; i < prop_info_arr.Length; ++i) {
1048                                 if (prop_info_arr [i].Name == name)
1049                                         return prop_values_arr [i];
1050                         }
1051
1052                         return null;
1053                 }
1054
1055                 //
1056                 // Theoretically, we can get rid of this, since FieldBuilder.SetCustomAttribute()
1057                 // and ParameterBuilder.SetCustomAttribute() are supposed to handle this attribute.
1058                 // However, we can't, since it appears that the .NET 1.1 SRE hangs when given a MarshalAsAttribute.
1059                 //
1060 #if !NET_2_0
1061                 public UnmanagedMarshal GetMarshal (Attributable attr)
1062                 {
1063                         UnmanagedType UnmanagedType;
1064                         if (!RootContext.StdLib || pos_values [0].GetType () != typeof (UnmanagedType))
1065                                 UnmanagedType = (UnmanagedType) System.Enum.ToObject (typeof (UnmanagedType), pos_values [0]);
1066                         else
1067                                 UnmanagedType = (UnmanagedType) pos_values [0];
1068
1069                         object value = GetFieldValue ("SizeParamIndex");
1070                         if (value != null && UnmanagedType != UnmanagedType.LPArray) {
1071                                 Error_AttributeEmitError ("SizeParamIndex field is not valid for the specified unmanaged type");
1072                                 return null;
1073                         }
1074
1075                         object o = GetFieldValue ("ArraySubType");
1076                         UnmanagedType array_sub_type = o == null ? (UnmanagedType) 0x50 /* NATIVE_MAX */ : (UnmanagedType) o;
1077
1078                         switch (UnmanagedType) {
1079                         case UnmanagedType.CustomMarshaler: {
1080                                 MethodInfo define_custom = typeof (UnmanagedMarshal).GetMethod ("DefineCustom",
1081                                         BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1082                                 if (define_custom == null) {
1083                                         Report.RuntimeMissingSupport (Location, "set marshal info");
1084                                         return null;
1085                                 }
1086                                 
1087                                 object [] args = new object [4];
1088                                 args [0] = GetFieldValue ("MarshalTypeRef");
1089                                 args [1] = GetFieldValue ("MarshalCookie");
1090                                 args [2] = GetFieldValue ("MarshalType");
1091                                 args [3] = Guid.Empty;
1092                                 return (UnmanagedMarshal) define_custom.Invoke (null, args);
1093                         }
1094                         case UnmanagedType.LPArray: {
1095                                 object size_const = GetFieldValue ("SizeConst");
1096                                 object size_param_index = GetFieldValue ("SizeParamIndex");
1097
1098                                 if ((size_const != null) || (size_param_index != null)) {
1099                                         MethodInfo define_array = typeof (UnmanagedMarshal).GetMethod ("DefineLPArrayInternal",
1100                                                 BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
1101                                         if (define_array == null) {
1102                                                 Report.RuntimeMissingSupport (Location, "set marshal info");
1103                                                 return null;
1104                                         }
1105                                 
1106                                         object [] args = new object [3];
1107                                         args [0] = array_sub_type;
1108                                         args [1] = size_const == null ? -1 : size_const;
1109                                         args [2] = size_param_index == null ? -1 : size_param_index;
1110                                         return (UnmanagedMarshal) define_array.Invoke (null, args);
1111                                 }
1112                                 else
1113                                         return UnmanagedMarshal.DefineLPArray (array_sub_type);
1114                         }
1115                         case UnmanagedType.SafeArray:
1116                                 return UnmanagedMarshal.DefineSafeArray (array_sub_type);
1117
1118                         case UnmanagedType.ByValArray:
1119                                 FieldBase fm = attr as FieldBase;
1120                                 if (fm == null) {
1121                                         Error_AttributeEmitError ("Specified unmanaged type is only valid on fields");
1122                                         return null;
1123                                 }
1124                                 return UnmanagedMarshal.DefineByValArray ((int) GetFieldValue ("SizeConst"));
1125
1126                         case UnmanagedType.ByValTStr:
1127                                 return UnmanagedMarshal.DefineByValTStr ((int) GetFieldValue ("SizeConst"));
1128
1129                         default:
1130                                 return UnmanagedMarshal.DefineUnmanagedMarshal (UnmanagedType);
1131                         }
1132                 }
1133
1134                 object GetFieldValue (string name)
1135                 {
1136                         int i;
1137                         if (field_info_arr == null)
1138                                 return null;
1139                         i = 0;
1140                         foreach (FieldInfo fi in field_info_arr) {
1141                                 if (fi.Name == name)
1142                                         return GetValue (field_values_arr [i]);
1143                                 i++;
1144                         }
1145                         return null;
1146                 }
1147
1148                 static object GetValue (object value)
1149                 {
1150                         if (value is EnumConstant)
1151                                 return ((EnumConstant) value).GetValue ();
1152                         else
1153                                 return value;                           
1154                 }
1155                 
1156 #endif
1157
1158                 public CharSet GetCharSetValue ()
1159                 {
1160                         return (CharSet)System.Enum.Parse (typeof (CharSet), pos_values [0].ToString ());
1161                 }
1162
1163                 public bool HasField (string fieldName)
1164                 {
1165                         if (field_info_arr == null)
1166                                 return false;
1167
1168                         foreach (FieldInfo fi in field_info_arr) {
1169                                 if (fi.Name == fieldName)
1170                                         return true;
1171                         }
1172
1173                         return false;
1174                 }
1175
1176                 public bool IsInternalMethodImplAttribute {
1177                         get {
1178                                 if (Type != PredefinedAttributes.Get.MethodImpl)
1179                                         return false;
1180
1181                                 MethodImplOptions options;
1182                                 if (pos_values[0].GetType () != typeof (MethodImplOptions))
1183                                         options = (MethodImplOptions)System.Enum.ToObject (typeof (MethodImplOptions), pos_values[0]);
1184                                 else
1185                                         options = (MethodImplOptions)pos_values[0];
1186
1187                                 return (options & MethodImplOptions.InternalCall) != 0;
1188                         }
1189                 }
1190
1191                 public LayoutKind GetLayoutKindValue ()
1192                 {
1193                         if (!RootContext.StdLib || pos_values [0].GetType () != typeof (LayoutKind))
1194                                 return (LayoutKind)System.Enum.ToObject (typeof (LayoutKind), pos_values [0]);
1195
1196                         return (LayoutKind)pos_values [0];
1197                 }
1198
1199                 public object GetParameterDefaultValue ()
1200                 {
1201                         return pos_values [0];
1202                 }
1203
1204                 public override bool Equals (object obj)
1205                 {
1206                         Attribute a = obj as Attribute;
1207                         if (a == null)
1208                                 return false;
1209
1210                         return Type == a.Type && Target == a.Target;
1211                 }
1212
1213                 public override int GetHashCode ()
1214                 {
1215                         return base.GetHashCode ();
1216                 }
1217
1218                 /// <summary>
1219                 /// Emit attribute for Attributable symbol
1220                 /// </summary>
1221                 public void Emit (ListDictionary allEmitted)
1222                 {
1223                         CustomAttributeBuilder cb = Resolve ();
1224                         if (cb == null)
1225                                 return;
1226
1227                         AttributeUsageAttribute usage_attr = GetAttributeUsage (Type);
1228                         if ((usage_attr.ValidOn & Target) == 0) {
1229                                 Report.Error (592, Location, "The attribute `{0}' is not valid on this declaration type. " +
1230                                               "It is valid on `{1}' declarations only",
1231                                         GetSignatureForError (), GetValidTargets ());
1232                                 return;
1233                         }
1234
1235                         try {
1236                                 foreach (Attributable owner in owners)
1237                                         owner.ApplyAttributeBuilder (this, cb, PredefinedAttributes.Get);
1238                         }
1239                         catch (Exception e) {
1240                                 Error_AttributeEmitError (e.Message);
1241                                 return;
1242                         }
1243
1244                         if (!usage_attr.AllowMultiple && allEmitted != null) {
1245                                 if (allEmitted.Contains (this)) {
1246                                         ArrayList a = allEmitted [this] as ArrayList;
1247                                         if (a == null) {
1248                                                 a = new ArrayList (2);
1249                                                 allEmitted [this] = a;
1250                                         }
1251                                         a.Add (this);
1252                                 } else {
1253                                         allEmitted.Add (this, null);
1254                                 }
1255                         }
1256
1257                         if (!RootContext.VerifyClsCompliance)
1258                                 return;
1259
1260                         // Here we are testing attribute arguments for array usage (error 3016)
1261                         if (Owner.IsClsComplianceRequired ()) {
1262                                 if (PosArguments != null)
1263                                         PosArguments.CheckArrayAsAttribute ();
1264                         
1265                                 if (NamedArguments == null)
1266                                         return;
1267
1268                                 NamedArguments.CheckArrayAsAttribute ();
1269                         }
1270                 }
1271
1272                 private Expression GetValue () 
1273                 {
1274                         if (PosArguments == null || PosArguments.Count < 1)
1275                                 return null;
1276
1277                         return PosArguments [0].Expr;
1278                 }
1279
1280                 public string GetString () 
1281                 {
1282                         Expression e = GetValue ();
1283                         if (e is StringConstant)
1284                                 return ((StringConstant)e).Value;
1285                         return null;
1286                 }
1287
1288                 public bool GetBoolean () 
1289                 {
1290                         Expression e = GetValue ();
1291                         if (e is BoolConstant)
1292                                 return ((BoolConstant)e).Value;
1293                         return false;
1294                 }
1295
1296                 public Type GetArgumentType ()
1297                 {
1298                         TypeOf e = GetValue () as TypeOf;
1299                         if (e == null)
1300                                 return null;
1301                         return e.TypeArgument;
1302                 }
1303
1304                 public override Expression CreateExpressionTree (EmitContext ec)
1305                 {
1306                         throw new NotSupportedException ("ET");
1307                 }
1308
1309                 public override Expression DoResolve (EmitContext ec)
1310                 {
1311                         throw new NotImplementedException ();
1312                 }
1313
1314                 public override void Emit (EmitContext ec)
1315                 {
1316                         throw new NotImplementedException ();
1317                 }
1318         }
1319         
1320
1321         /// <summary>
1322         /// For global attributes (assembly, module) we need special handling.
1323         /// Attributes can be located in the several files
1324         /// </summary>
1325         public class GlobalAttribute : Attribute
1326         {
1327                 public readonly NamespaceEntry ns;
1328
1329                 public GlobalAttribute (NamespaceEntry ns, string target, 
1330                                         Expression left_expr, string identifier, Arguments[] args, Location loc, bool nameEscaped):
1331                         base (target, left_expr, identifier, args, loc, nameEscaped)
1332                 {
1333                         this.ns = ns;
1334                         this.owners = new Attributable[1];
1335                 }
1336                 
1337                 public override void AttachTo (Attributable owner)
1338                 {
1339                         if (ExplicitTarget == "assembly") {
1340                                 owners [0] = CodeGen.Assembly;
1341                                 return;
1342                         }
1343                         if (ExplicitTarget == "module") {
1344                                 owners [0] = RootContext.ToplevelTypes;
1345                                 return;
1346                         }
1347                         throw new NotImplementedException ("Unknown global explicit target " + ExplicitTarget);
1348                 }
1349
1350                 void Enter ()
1351                 {
1352                         // RootContext.ToplevelTypes has a single NamespaceEntry which gets overwritten
1353                         // each time a new file is parsed.  However, we need to use the NamespaceEntry
1354                         // in effect where the attribute was used.  Since code elsewhere cannot assume
1355                         // that the NamespaceEntry is right, just overwrite it.
1356                         //
1357                         // Precondition: RootContext.ToplevelTypes == null
1358
1359                         if (RootContext.ToplevelTypes.NamespaceEntry != null)
1360                                 throw new InternalErrorException (Location + " non-null NamespaceEntry");
1361
1362                         RootContext.ToplevelTypes.NamespaceEntry = ns;
1363                 }
1364
1365                 protected override bool IsSecurityActionValid (bool for_assembly)
1366                 {
1367                         return base.IsSecurityActionValid (true);
1368                 }
1369
1370                 void Leave ()
1371                 {
1372                         RootContext.ToplevelTypes.NamespaceEntry = null;
1373                 }
1374
1375                 protected override TypeExpr ResolveAsTypeTerminal (Expression expr, IResolveContext ec, bool silent)
1376                 {
1377                         try {
1378                                 Enter ();
1379                                 return base.ResolveAsTypeTerminal (expr, ec, silent);
1380                         }
1381                         finally {
1382                                 Leave ();
1383                         }
1384                 }
1385
1386                 protected override ConstructorInfo ResolveConstructor (EmitContext ec)
1387                 {
1388                         try {
1389                                 Enter ();
1390                                 return base.ResolveConstructor (ec);
1391                         }
1392                         finally {
1393                                 Leave ();
1394                         }
1395                 }
1396
1397                 protected override bool ResolveNamedArguments (EmitContext ec)
1398                 {
1399                         try {
1400                                 Enter ();
1401                                 return base.ResolveNamedArguments (ec);
1402                         }
1403                         finally {
1404                                 Leave ();
1405                         }
1406                 }
1407         }
1408
1409         public class Attributes {
1410                 public readonly ArrayList Attrs;
1411
1412                 public Attributes (Attribute a)
1413                 {
1414                         Attrs = new ArrayList ();
1415                         Attrs.Add (a);
1416                 }
1417
1418                 public Attributes (ArrayList attrs)
1419                 {
1420                         Attrs = attrs;
1421                 }
1422
1423                 public void AddAttributes (ArrayList attrs)
1424                 {
1425                         Attrs.AddRange (attrs);
1426                 }
1427
1428                 public void AttachTo (Attributable attributable)
1429                 {
1430                         foreach (Attribute a in Attrs)
1431                                 a.AttachTo (attributable);
1432                 }
1433
1434                 public Attributes Clone ()
1435                 {
1436                         ArrayList al = new ArrayList (Attrs.Count);
1437                         foreach (Attribute a in Attrs)
1438                                 al.Add (a.Clone ());
1439
1440                         return new Attributes (al);
1441                 }
1442
1443                 /// <summary>
1444                 /// Checks whether attribute target is valid for the current element
1445                 /// </summary>
1446                 public bool CheckTargets ()
1447                 {
1448                         foreach (Attribute a in Attrs) {
1449                                 if (!a.CheckTarget ())
1450                                         return false;
1451                         }
1452                         return true;
1453                 }
1454
1455                 public Attribute Search (PredefinedAttribute t)
1456                 {
1457                         foreach (Attribute a in Attrs) {
1458                                 if (a.ResolveType () == t)
1459                                         return a;
1460                         }
1461                         return null;
1462                 }
1463
1464                 /// <summary>
1465                 /// Returns all attributes of type 't'. Use it when attribute is AllowMultiple = true
1466                 /// </summary>
1467                 public Attribute[] SearchMulti (PredefinedAttribute t)
1468                 {
1469                         ArrayList ar = null;
1470
1471                         foreach (Attribute a in Attrs) {
1472                                 if (a.ResolveType () == t) {
1473                                         if (ar == null)
1474                                                 ar = new ArrayList ();
1475                                         ar.Add (a);
1476                                 }
1477                         }
1478
1479                         return ar == null ? null : ar.ToArray (typeof (Attribute)) as Attribute[];
1480                 }
1481
1482                 public void Emit ()
1483                 {
1484                         CheckTargets ();
1485
1486                         ListDictionary ld = Attrs.Count > 1 ? new ListDictionary () : null;
1487
1488                         foreach (Attribute a in Attrs)
1489                                 a.Emit (ld);
1490
1491                         if (ld == null || ld.Count == 0)
1492                                 return;
1493
1494                         foreach (DictionaryEntry d in ld) {
1495                                 if (d.Value == null)
1496                                         continue;
1497
1498                                 foreach (Attribute collision in (ArrayList)d.Value)
1499                                         Report.SymbolRelatedToPreviousError (collision.Location, "");
1500
1501                                 Attribute a = (Attribute)d.Key;
1502                                 Report.Error (579, a.Location, "The attribute `{0}' cannot be applied multiple times",
1503                                         a.GetSignatureForError ());
1504                         }
1505                 }
1506
1507                 public bool Contains (PredefinedAttribute t)
1508                 {
1509                         return Search (t) != null;
1510                 }
1511         }
1512
1513         /// <summary>
1514         /// Helper class for attribute verification routine.
1515         /// </summary>
1516         sealed class AttributeTester
1517         {
1518                 static PtrHashtable analyzed_types;
1519                 static PtrHashtable analyzed_types_obsolete;
1520                 static PtrHashtable analyzed_member_obsolete;
1521                 static PtrHashtable analyzed_method_excluded;
1522                 static PtrHashtable fixed_buffer_cache;
1523
1524                 static object TRUE = new object ();
1525                 static object FALSE = new object ();
1526
1527                 static AttributeTester ()
1528                 {
1529                         Reset ();
1530                 }
1531
1532                 private AttributeTester ()
1533                 {
1534                 }
1535
1536                 public static void Reset ()
1537                 {
1538                         analyzed_types = new PtrHashtable ();
1539                         analyzed_types_obsolete = new PtrHashtable ();
1540                         analyzed_member_obsolete = new PtrHashtable ();
1541                         analyzed_method_excluded = new PtrHashtable ();
1542                         fixed_buffer_cache = new PtrHashtable ();
1543                 }
1544
1545                 public enum Result {
1546                         Ok,
1547                         RefOutArrayError,
1548                         ArrayArrayError
1549                 }
1550
1551                 /// <summary>
1552                 /// Returns true if parameters of two compared methods are CLS-Compliant.
1553                 /// It tests differing only in ref or out, or in array rank.
1554                 /// </summary>
1555                 public static Result AreOverloadedMethodParamsClsCompliant (AParametersCollection pa, AParametersCollection pb) 
1556                 {
1557                         Type [] types_a = pa.Types;
1558                         Type [] types_b = pb.Types;
1559                         if (types_a == null || types_b == null)
1560                                 return Result.Ok;
1561
1562                         if (types_a.Length != types_b.Length)
1563                                 return Result.Ok;
1564
1565                         Result result = Result.Ok;
1566                         for (int i = 0; i < types_b.Length; ++i) {
1567                                 Type aType = types_a [i];
1568                                 Type bType = types_b [i];
1569
1570                                 if (aType.IsArray && bType.IsArray) {
1571                                         Type a_el_type = TypeManager.GetElementType (aType);
1572                                         Type b_el_type = TypeManager.GetElementType (bType);
1573                                         if (aType.GetArrayRank () != bType.GetArrayRank () && a_el_type == b_el_type) {
1574                                                 result = Result.RefOutArrayError;
1575                                                 continue;
1576                                         }
1577
1578                                         if (a_el_type.IsArray || b_el_type.IsArray) {
1579                                                 result = Result.ArrayArrayError;
1580                                                 continue;
1581                                         }
1582                                 }
1583
1584                                 if (aType != bType)
1585                                         return Result.Ok;
1586
1587                                 const Parameter.Modifier out_ref_mod = (Parameter.Modifier.OUTMASK | Parameter.Modifier.REFMASK);
1588                                 if ((pa.FixedParameters[i].ModFlags & out_ref_mod) != (pb.FixedParameters[i].ModFlags & out_ref_mod))
1589                                         result = Result.RefOutArrayError;
1590                         }
1591                         return result;
1592                 }
1593
1594                 /// <summary>
1595                 /// This method tests the CLS compliance of external types. It doesn't test type visibility.
1596                 /// </summary>
1597                 public static bool IsClsCompliant (Type type) 
1598                 {
1599                         if (type == null)
1600                                 return true;
1601
1602                         object type_compliance = analyzed_types[type];
1603                         if (type_compliance != null)
1604                                 return type_compliance == TRUE;
1605
1606                         if (type.IsPointer) {
1607                                 analyzed_types.Add (type, FALSE);
1608                                 return false;
1609                         }
1610
1611                         bool result;
1612                         if (type.IsArray) {
1613                                 result = IsClsCompliant (TypeManager.GetElementType (type));
1614                         } else if (TypeManager.IsNullableType (type)) {
1615                                 result = IsClsCompliant (TypeManager.GetTypeArguments (type) [0]);
1616                         } else {
1617                                 result = AnalyzeTypeCompliance (type);
1618                         }
1619                         analyzed_types.Add (type, result ? TRUE : FALSE);
1620                         return result;
1621                 }        
1622         
1623                 /// <summary>
1624                 /// Returns IFixedBuffer implementation if field is fixed buffer else null.
1625                 /// </summary>
1626                 public static IFixedBuffer GetFixedBuffer (FieldInfo fi)
1627                 {
1628                         // Fixed buffer helper type is generated as value type
1629                         if (TypeManager.IsReferenceType (fi.FieldType))
1630                                 return null;
1631
1632                         FieldBase fb = TypeManager.GetField (fi);
1633                         if (fb != null) {
1634                                 return fb as IFixedBuffer;
1635                         }
1636                         
1637                         if (TypeManager.GetConstant (fi) != null)
1638                                 return null;
1639
1640                         object o = fixed_buffer_cache [fi];
1641                         if (o == null) {
1642                                 PredefinedAttribute pa = PredefinedAttributes.Get.FixedBuffer;
1643                                 if (!pa.IsDefined)
1644                                         return null;
1645
1646                                 if (!fi.IsDefined (pa.Type, false)) {
1647                                         fixed_buffer_cache.Add (fi, FALSE);
1648                                         return null;
1649                                 }
1650                                 
1651                                 IFixedBuffer iff = new FixedFieldExternal (fi);
1652                                 fixed_buffer_cache.Add (fi, iff);
1653                                 return iff;
1654                         }
1655
1656                         if (o == FALSE)
1657                                 return null;
1658
1659                         return (IFixedBuffer)o;
1660                 }
1661
1662                 public static void VerifyModulesClsCompliance ()
1663                 {
1664                         Module[] modules = GlobalRootNamespace.Instance.Modules;
1665                         if (modules == null)
1666                                 return;
1667
1668                         // The first module is generated assembly
1669                         for (int i = 1; i < modules.Length; ++i) {
1670                                 Module module = modules [i];
1671                                 if (!GetClsCompliantAttributeValue (module, null)) {
1672                                         Report.Error (3013, "Added modules must be marked with the CLSCompliant attribute " +
1673                                                       "to match the assembly", module.Name);
1674                                         return;
1675                                 }
1676                         }
1677                 }
1678
1679                 public static Type GetImportedIgnoreCaseClsType (string name)
1680                 {
1681                         foreach (Assembly a in GlobalRootNamespace.Instance.Assemblies) {
1682                                 Type t = a.GetType (name, false, true);
1683                                 if (t == null)
1684                                         continue;
1685
1686                                 if (IsClsCompliant (t))
1687                                         return t;
1688                         }
1689                         return null;
1690                 }
1691
1692                 static bool GetClsCompliantAttributeValue (ICustomAttributeProvider attribute_provider, Assembly a) 
1693                 {
1694                         PredefinedAttribute pa = PredefinedAttributes.Get.CLSCompliant;
1695                         if (!pa.IsDefined)
1696                                 return false;
1697
1698                         object[] cls_attr = attribute_provider.GetCustomAttributes (pa.Type, false);
1699                         if (cls_attr.Length == 0) {
1700                                 if (a == null)
1701                                         return false;
1702
1703                                 return GetClsCompliantAttributeValue (a, null);
1704                         }
1705                         
1706                         return ((CLSCompliantAttribute)cls_attr [0]).IsCompliant;
1707                 }
1708
1709                 static bool AnalyzeTypeCompliance (Type type)
1710                 {
1711                         type = TypeManager.DropGenericTypeArguments (type);
1712                         DeclSpace ds = TypeManager.LookupDeclSpace (type);
1713                         if (ds != null) {
1714                                 return ds.IsClsComplianceRequired ();
1715                         }
1716
1717                         if (TypeManager.IsGenericParameter (type))
1718                                 return true;
1719
1720                         return GetClsCompliantAttributeValue (type, type.Assembly);
1721                 }
1722
1723                 /// <summary>
1724                 /// Returns instance of ObsoleteAttribute when type is obsolete
1725                 /// </summary>
1726                 public static ObsoleteAttribute GetObsoleteAttribute (Type type)
1727                 {
1728                         object type_obsolete = analyzed_types_obsolete [type];
1729                         if (type_obsolete == FALSE)
1730                                 return null;
1731
1732                         if (type_obsolete != null)
1733                                 return (ObsoleteAttribute)type_obsolete;
1734
1735                         ObsoleteAttribute result = null;
1736                         if (TypeManager.HasElementType (type)) {
1737                                 result = GetObsoleteAttribute (TypeManager.GetElementType (type));
1738                         } else if (TypeManager.IsGenericParameter (type))
1739                                 result = null;  // TODO: throw new NotSupportedException ()
1740                         else if (TypeManager.IsGenericType (type) && !TypeManager.IsGenericTypeDefinition (type)) {
1741                                 return GetObsoleteAttribute (TypeManager.DropGenericTypeArguments (type));
1742                         } else {
1743                                 DeclSpace type_ds = TypeManager.LookupDeclSpace (type);
1744
1745                                 // Type is external, we can get attribute directly
1746                                 if (type_ds == null) {
1747                                         PredefinedAttribute pa = PredefinedAttributes.Get.Obsolete;
1748                                         if (pa.IsDefined) {
1749                                                 object[] attribute = type.GetCustomAttributes (pa.Type, false);
1750                                                 if (attribute.Length == 1)
1751                                                         result = (ObsoleteAttribute) attribute[0];
1752                                         }
1753                                 } else {
1754                                         result = type_ds.GetObsoleteAttribute ();
1755                                 }
1756                         }
1757
1758                         // Cannot use .Add because of corlib bootstrap
1759                         analyzed_types_obsolete [type] = result == null ? FALSE : result;
1760                         return result;
1761                 }
1762
1763                 /// <summary>
1764                 /// Returns instance of ObsoleteAttribute when method is obsolete
1765                 /// </summary>
1766                 public static ObsoleteAttribute GetMethodObsoleteAttribute (MethodBase mb)
1767                 {
1768                         IMethodData mc = TypeManager.GetMethod (mb);
1769                         if (mc != null) 
1770                                 return mc.GetObsoleteAttribute ();
1771
1772                         // compiler generated methods are not registered by AddMethod
1773                         if (mb.DeclaringType is TypeBuilder)
1774                                 return null;
1775
1776                         MemberInfo mi = TypeManager.GetPropertyFromAccessor (mb);
1777                         if (mi != null)
1778                                 return GetMemberObsoleteAttribute (mi);
1779
1780                         mi = TypeManager.GetEventFromAccessor (mb);
1781                         if (mi != null)
1782                                 return GetMemberObsoleteAttribute (mi);
1783
1784                         return GetMemberObsoleteAttribute (mb);
1785                 }
1786
1787                 /// <summary>
1788                 /// Returns instance of ObsoleteAttribute when member is obsolete
1789                 /// </summary>
1790                 public static ObsoleteAttribute GetMemberObsoleteAttribute (MemberInfo mi)
1791                 {
1792                         object type_obsolete = analyzed_member_obsolete [mi];
1793                         if (type_obsolete == FALSE)
1794                                 return null;
1795
1796                         if (type_obsolete != null)
1797                                 return (ObsoleteAttribute)type_obsolete;
1798
1799                         if ((mi.DeclaringType is TypeBuilder) || TypeManager.IsGenericType (mi.DeclaringType))
1800                                 return null;
1801
1802                         PredefinedAttribute pa = PredefinedAttributes.Get.Obsolete;
1803                         if (!pa.IsDefined)
1804                                 return null;
1805
1806                         ObsoleteAttribute oa = System.Attribute.GetCustomAttribute (mi, pa.Type, false)
1807                                 as ObsoleteAttribute;
1808                         analyzed_member_obsolete.Add (mi, oa == null ? FALSE : oa);
1809                         return oa;
1810                 }
1811
1812                 /// <summary>
1813                 /// Common method for Obsolete error/warning reporting.
1814                 /// </summary>
1815                 public static void Report_ObsoleteMessage (ObsoleteAttribute oa, string member, Location loc)
1816                 {
1817                         if (oa.IsError) {
1818                                 Report.Error (619, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1819                                 return;
1820                         }
1821
1822                         if (oa.Message == null || oa.Message.Length == 0) {
1823                                 Report.Warning (612, 1, loc, "`{0}' is obsolete", member);
1824                                 return;
1825                         }
1826                         Report.Warning (618, 2, loc, "`{0}' is obsolete: `{1}'", member, oa.Message);
1827                 }
1828
1829                 public static bool IsConditionalMethodExcluded (MethodBase mb, Location loc)
1830                 {
1831                         object excluded = analyzed_method_excluded [mb];
1832                         if (excluded != null)
1833                                 return excluded == TRUE ? true : false;
1834
1835                         PredefinedAttribute pa = PredefinedAttributes.Get.Conditional;
1836                         if (!pa.IsDefined)
1837                                 return false;
1838
1839                         ConditionalAttribute[] attrs = mb.GetCustomAttributes (pa.Type, true)
1840                                 as ConditionalAttribute[];
1841                         if (attrs.Length == 0) {
1842                                 analyzed_method_excluded.Add (mb, FALSE);
1843                                 return false;
1844                         }
1845
1846                         foreach (ConditionalAttribute a in attrs) {
1847                                 if (loc.CompilationUnit.IsConditionalDefined (a.ConditionString)) {
1848                                         analyzed_method_excluded.Add (mb, FALSE);
1849                                         return false;
1850                                 }
1851                         }
1852
1853                         analyzed_method_excluded.Add (mb, TRUE);
1854                         return true;
1855                 }
1856
1857                 /// <summary>
1858                 /// Analyzes class whether it has attribute which has ConditionalAttribute
1859                 /// and its condition is not defined.
1860                 /// </summary>
1861                 public static bool IsAttributeExcluded (Type type, Location loc)
1862                 {
1863                         if (!type.IsClass)
1864                                 return false;
1865
1866                         Class class_decl = TypeManager.LookupDeclSpace (type) as Class;
1867
1868                         // TODO: add caching
1869                         // TODO: merge all Type bases attribute caching to one cache to save memory
1870                         PredefinedAttribute pa = PredefinedAttributes.Get.Conditional;
1871                         if (class_decl == null && pa.IsDefined) {
1872                                 object[] attributes = type.GetCustomAttributes (pa.Type, false);
1873                                 foreach (ConditionalAttribute ca in attributes) {
1874                                         if (loc.CompilationUnit.IsConditionalDefined (ca.ConditionString))
1875                                                 return false;
1876                                 }
1877                                 return attributes.Length > 0;
1878                         }
1879
1880                         return class_decl.IsExcluded ();
1881                 }
1882
1883                 public static Type GetCoClassAttribute (Type type)
1884                 {
1885                         TypeContainer tc = TypeManager.LookupInterface (type);
1886                         PredefinedAttribute pa = PredefinedAttributes.Get.CoClass;
1887                         if (tc == null) {
1888                                 if (!pa.IsDefined)
1889                                         return null;
1890
1891                                 object[] o = type.GetCustomAttributes (pa.Type, false);
1892                                 if (o.Length < 1)
1893                                         return null;
1894                                 return ((System.Runtime.InteropServices.CoClassAttribute)o[0]).CoClass;
1895                         }
1896
1897                         if (tc.OptAttributes == null)
1898                                 return null;
1899
1900                         Attribute a = tc.OptAttributes.Search (pa);
1901                         if (a == null)
1902                                 return null;
1903
1904                         return a.GetCoClassAttributeValue ();
1905                 }
1906         }
1907
1908         public class PredefinedAttributes
1909         {
1910                 // Core types
1911                 public readonly PredefinedAttribute ParamArray;
1912                 public readonly PredefinedAttribute Out;
1913
1914                 // Optional types
1915                 public readonly PredefinedAttribute Obsolete;
1916                 public readonly PredefinedAttribute DllImport;
1917                 public readonly PredefinedAttribute MethodImpl;
1918                 public readonly PredefinedAttribute MarshalAs;
1919                 public readonly PredefinedAttribute In;
1920                 public readonly PredefinedAttribute IndexerName;
1921                 public readonly PredefinedAttribute Conditional;
1922                 public readonly PredefinedAttribute CLSCompliant;
1923                 public readonly PredefinedAttribute Security;
1924                 public readonly PredefinedAttribute Required;
1925                 public readonly PredefinedAttribute Guid;
1926                 public readonly PredefinedAttribute AssemblyCulture;
1927                 public readonly PredefinedAttribute AssemblyVersion;
1928                 public readonly PredefinedAttribute ComImport;
1929                 public readonly PredefinedAttribute CoClass;
1930                 public readonly PredefinedAttribute AttributeUsage;
1931                 public readonly PredefinedAttribute DefaultParameterValue;
1932                 public readonly PredefinedAttribute OptionalParameter;
1933
1934                 // New in .NET 2.0
1935                 public readonly PredefinedAttribute DefaultCharset;
1936                 public readonly PredefinedAttribute TypeForwarder;
1937                 public readonly PredefinedAttribute FixedBuffer;
1938                 public readonly PredefinedAttribute CompilerGenerated;
1939                 public readonly PredefinedAttribute InternalsVisibleTo;
1940                 public readonly PredefinedAttribute RuntimeCompatibility;
1941                 public readonly PredefinedAttribute DebuggerHidden;
1942                 public readonly PredefinedAttribute UnsafeValueType;
1943
1944                 // New in .NET 3.5
1945                 public readonly PredefinedAttribute Extension;
1946
1947                 //
1948                 // Optional types which are used as types and for member lookup
1949                 //
1950                 public readonly PredefinedAttribute DefaultMember;
1951                 public readonly PredefinedAttribute DecimalConstant;
1952                 public readonly PredefinedAttribute StructLayout;
1953                 public readonly PredefinedAttribute FieldOffset;
1954
1955                 public static PredefinedAttributes Get = new PredefinedAttributes ();
1956
1957                 private PredefinedAttributes ()
1958                 {
1959                         ParamArray = new PredefinedAttribute ("System", "ParamArrayAttribute");
1960                         Out = new PredefinedAttribute ("System.Runtime.InteropServices", "OutAttribute");
1961
1962                         Obsolete = new PredefinedAttribute ("System", "ObsoleteAttribute");
1963                         DllImport = new PredefinedAttribute ("System.Runtime.InteropServices", "DllImportAttribute");
1964                         MethodImpl = new PredefinedAttribute ("System.Runtime.CompilerServices", "MethodImplAttribute");
1965                         MarshalAs = new PredefinedAttribute ("System.Runtime.InteropServices", "MarshalAsAttribute");
1966                         In = new PredefinedAttribute ("System.Runtime.InteropServices", "InAttribute");
1967                         IndexerName = new PredefinedAttribute ("System.Runtime.CompilerServices", "IndexerNameAttribute");
1968                         Conditional = new PredefinedAttribute ("System.Diagnostics", "ConditionalAttribute");
1969                         CLSCompliant = new PredefinedAttribute ("System", "CLSCompliantAttribute");
1970                         Security = new PredefinedAttribute ("System.Security.Permissions", "SecurityAttribute");
1971                         Required = new PredefinedAttribute ("System.Runtime.CompilerServices", "RequiredAttributeAttribute");
1972                         Guid = new PredefinedAttribute ("System.Runtime.InteropServices", "GuidAttribute");
1973                         AssemblyCulture = new PredefinedAttribute ("System.Reflection", "AssemblyCultureAttribute");
1974                         AssemblyVersion = new PredefinedAttribute ("System.Reflection", "AssemblyVersionAttribute");
1975                         ComImport = new PredefinedAttribute ("System.Runtime.InteropServices", "ComImportAttribute");
1976                         CoClass = new PredefinedAttribute ("System.Runtime.InteropServices", "CoClassAttribute");
1977                         AttributeUsage = new PredefinedAttribute ("System", "AttributeUsageAttribute");
1978                         DefaultParameterValue = new PredefinedAttribute ("System.Runtime.InteropServices", "DefaultParameterValueAttribute");
1979                         OptionalParameter = new PredefinedAttribute ("System.Runtime.InteropServices", "OptionalAttribute");
1980
1981                         DefaultCharset = new PredefinedAttribute ("System.Runtime.InteropServices", "DefaultCharSetAttribute");
1982                         TypeForwarder = new PredefinedAttribute ("System.Runtime.CompilerServices", "TypeForwardedToAttribute");
1983                         FixedBuffer = new PredefinedAttribute ("System.Runtime.CompilerServices", "FixedBufferAttribute");
1984                         CompilerGenerated = new PredefinedAttribute ("System.Runtime.CompilerServices", "CompilerGeneratedAttribute");
1985                         InternalsVisibleTo = new PredefinedAttribute ("System.Runtime.CompilerServices", "InternalsVisibleToAttribute");
1986                         RuntimeCompatibility = new PredefinedAttribute ("System.Runtime.CompilerServices", "RuntimeCompatibilityAttribute");
1987                         DebuggerHidden = new PredefinedAttribute ("System.Diagnostics", "DebuggerHiddenAttribute");
1988                         UnsafeValueType = new PredefinedAttribute ("System.Runtime.CompilerServices", "UnsafeValueTypeAttribute");
1989
1990                         Extension = new PredefinedAttribute ("System.Runtime.CompilerServices", "ExtensionAttribute");
1991
1992                         DefaultMember = new PredefinedAttribute ("System.Reflection", "DefaultMemberAttribute");
1993                         DecimalConstant = new PredefinedAttribute ("System.Runtime.CompilerServices", "DecimalConstantAttribute");
1994                         StructLayout = new PredefinedAttribute ("System.Runtime.InteropServices", "StructLayoutAttribute");
1995                         FieldOffset = new PredefinedAttribute ("System.Runtime.InteropServices", "FieldOffsetAttribute");
1996                 }
1997
1998                 public void Initialize ()
1999                 {
2000                         foreach (FieldInfo fi in GetType ().GetFields (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
2001                                 ((PredefinedAttribute) fi.GetValue (this)).Resolve (true);
2002                         }
2003                 }
2004
2005                 public static void Reset ()
2006                 {
2007                         Get = new PredefinedAttributes ();
2008                 }
2009         }
2010
2011         public class PredefinedAttribute
2012         {
2013                 Type type;
2014                 CustomAttributeBuilder cab;
2015                 ConstructorInfo ctor;
2016                 readonly string ns, name;
2017
2018                 public PredefinedAttribute (string ns, string name)
2019                 {
2020                         this.ns = ns;
2021                         this.name = name;
2022                 }
2023
2024                 public static bool operator == (Type type, PredefinedAttribute pa)
2025                 {
2026                         return type == pa.type;
2027                 }
2028
2029                 public static bool operator != (Type type, PredefinedAttribute pa)
2030                 {
2031                         return type != pa.type;
2032                 }
2033
2034                 public ConstructorInfo Constructor {
2035                         get { return ctor; }
2036                 }
2037
2038                 public override int GetHashCode ()
2039                 {
2040                         return base.GetHashCode ();
2041                 }
2042
2043                 public override bool Equals (object obj)
2044                 {
2045                         throw new NotSupportedException ();
2046                 }
2047
2048                 public void EmitAttribute (ConstructorBuilder builder)
2049                 {
2050                         if (ResolveBuilder ())
2051                                 builder.SetCustomAttribute (cab);
2052                 }
2053
2054                 public void EmitAttribute (MethodBuilder builder)
2055                 {
2056                         if (ResolveBuilder ())
2057                                 builder.SetCustomAttribute (cab);
2058                 }
2059
2060                 public void EmitAttribute (FieldBuilder builder)
2061                 {
2062                         if (ResolveBuilder ())
2063                                 builder.SetCustomAttribute (cab);
2064                 }
2065
2066                 public void EmitAttribute (TypeBuilder builder)
2067                 {
2068                         if (ResolveBuilder ())
2069                                 builder.SetCustomAttribute (cab);
2070                 }
2071
2072                 public void EmitAttribute (AssemblyBuilder builder)
2073                 {
2074                         if (ResolveBuilder ())
2075                                 builder.SetCustomAttribute (cab);
2076                 }
2077
2078                 public void EmitAttribute (ParameterBuilder builder, Location loc)
2079                 {
2080                         if (ResolveBuilder ())
2081                                 builder.SetCustomAttribute (cab);
2082                 }
2083
2084                 public bool IsDefined {
2085                         get { return type != null && type != typeof (PredefinedAttribute); }
2086                 }
2087
2088                 public bool Resolve (bool canFail)
2089                 {
2090                         if (type != null) {
2091                                 if (IsDefined)
2092                                         return true;
2093                                 if (canFail)
2094                                         return false;
2095                         }
2096
2097                         type = TypeManager.CoreLookupType (ns, name, Kind.Class, !canFail);
2098                         if (type == null) {
2099                                 type = typeof (PredefinedAttribute);
2100                                 return false;
2101                         }
2102
2103                         return true;
2104                 }
2105
2106                 bool ResolveBuilder ()
2107                 {
2108                         if (cab != null)
2109                                 return true;
2110
2111                         //
2112                         // Handle all parameter-less attributes as optional
2113                         //
2114                         if (!Resolve (true))
2115                                 return false;
2116
2117                         ConstructorInfo ci = TypeManager.GetPredefinedConstructor (type, Location.Null, Type.EmptyTypes);
2118                         if (ci == null)
2119                                 return false;
2120
2121                         cab = new CustomAttributeBuilder (ci, new object[0]);
2122                         return true;
2123                 }
2124
2125                 public bool ResolveConstructor (Location loc, params Type[] argType)
2126                 {
2127                         if (ctor != null)
2128                                 throw new InternalErrorException ("Predefined ctor redefined");
2129
2130                         if (!Resolve (false))
2131                                 return false;
2132
2133                         ctor = TypeManager.GetPredefinedConstructor (type, loc, argType);
2134                         return ctor != null;
2135                 }
2136
2137                 public Type Type {
2138                         get { return type; }
2139                 }
2140         }
2141 }