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