5a21b1350711acd289fd26f75de5359e43c8b3df
[mono.git] / mcs / mcs / attribute.cs
1 //\r
2 // attribute.cs: Attribute Handler\r
3 //\r
4 // Author: Ravi Pratap (ravi@ximian.com)\r
5 //\r
6 // Licensed under the terms of the GNU GPL\r
7 //\r
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)\r
9 //\r
10 //\r
11 \r
12 using System;\r
13 using System.Diagnostics;\r
14 using System.Collections;\r
15 using System.Reflection;\r
16 using System.Reflection.Emit;\r
17 using System.Runtime.InteropServices;\r
18 using System.Runtime.CompilerServices;\r
19 using System.Text;\r
20 \r
21 namespace Mono.CSharp {\r
22 \r
23         public class Attribute {\r
24                 public readonly string    Name;\r
25                 public readonly ArrayList Arguments;\r
26 \r
27                 Location Location;\r
28 \r
29                 public Type Type;\r
30                 \r
31                 //\r
32                 // The following are only meaningful when the attribute\r
33                 // being emitted is one of the builtin ones\r
34                 //\r
35                 AttributeTargets Targets;\r
36                 bool AllowMultiple;\r
37                 bool Inherited;\r
38 \r
39                 bool UsageAttr = false;\r
40                 \r
41                 MethodImplOptions ImplOptions;\r
42                 UnmanagedType     UnmanagedType;\r
43                 CustomAttributeBuilder cb;\r
44                 \r
45                 public Attribute (string name, ArrayList args, Location loc)\r
46                 {\r
47                         Name = name;\r
48                         Arguments = args;\r
49                         Location = loc;\r
50                 }\r
51 \r
52                 void Error_InvalidNamedArgument (string name)\r
53                 {\r
54                         Report.Error (617, Location, "'" + name + "' is not a valid named attribute " +\r
55                                       "argument. Named attribute arguments must be fields which are not " +\r
56                                       "readonly, static or const, or properties with a set accessor which "+\r
57                                       "are not static.");\r
58                 }\r
59 \r
60                 void Error_AttributeArgumentNotValid ()\r
61                 {\r
62                         Report.Error (182, Location,\r
63                                       "An attribute argument must be a constant expression, typeof " +\r
64                                       "expression or array creation expression");\r
65                 }\r
66 \r
67                 static void Error_AttributeConstructorMismatch (Location loc)\r
68                 {\r
69                         Report.Error (\r
70                                         -6, loc,\r
71                                         "Could not find a constructor for this argument list.");\r
72                 }\r
73                 \r
74                 private Type CheckAttributeType (EmitContext ec) {\r
75                         Type t;\r
76                         bool isattributeclass = true;\r
77                         \r
78                         t = RootContext.LookupType (ec.DeclSpace, Name, true, Location);\r
79                         if (t != null) {\r
80                                 isattributeclass = t.IsSubclassOf (TypeManager.attribute_type);\r
81                                 if (isattributeclass)\r
82                                         return t;\r
83                         }\r
84                         t = RootContext.LookupType (ec.DeclSpace, Name + "Attribute", true, Location);\r
85                         if (t != null) {\r
86                                 if (t.IsSubclassOf (TypeManager.attribute_type))\r
87                                         return t;\r
88                         }\r
89                         if (!isattributeclass) {\r
90                                 Report.Error (616, Location, "'" + Name + "': is not an attribute class");\r
91                                 return null;\r
92                         }\r
93                         if (t != null) {\r
94                                 Report.Error (616, Location, "'" + Name + "Attribute': is not an attribute class");\r
95                                 return null;\r
96                         }\r
97                         Report.Error (\r
98                                 246, Location, "Could not find attribute '" + Name + "' (are you" +\r
99                                 " missing a using directive or an assembly reference ?)");\r
100                         return null;\r
101                 }\r
102 \r
103                 public Type ResolveType (EmitContext ec)\r
104                 {\r
105                         Type = CheckAttributeType (ec);\r
106                         return Type;\r
107                 }\r
108 \r
109                 \r
110                 public CustomAttributeBuilder Resolve (EmitContext ec)\r
111                 {\r
112                         if (Type == null)\r
113                                 Type = CheckAttributeType (ec);\r
114                         if (Type == null)\r
115                                 return null;\r
116 \r
117                         bool MethodImplAttr = false;\r
118                         bool MarshalAsAttr = false;\r
119 \r
120                         UsageAttr = false;\r
121 \r
122                         if (Type == TypeManager.attribute_usage_type)\r
123                                 UsageAttr = true;\r
124                         if (Type == TypeManager.methodimpl_attr_type)\r
125                                 MethodImplAttr = true;\r
126                         if (Type == TypeManager.marshal_as_attr_type)\r
127                                 MarshalAsAttr = true;\r
128 \r
129                         // Now we extract the positional and named arguments\r
130                         \r
131                         ArrayList pos_args = new ArrayList ();\r
132                         ArrayList named_args = new ArrayList ();\r
133                         int pos_arg_count = 0;\r
134                         \r
135                         if (Arguments != null) {\r
136                                 pos_args = (ArrayList) Arguments [0];\r
137                                 if (pos_args != null)\r
138                                         pos_arg_count = pos_args.Count;\r
139                                 if (Arguments.Count > 1)\r
140                                         named_args = (ArrayList) Arguments [1];\r
141                         }\r
142 \r
143                         object [] pos_values = new object [pos_arg_count];\r
144 \r
145                         //\r
146                         // First process positional arguments \r
147                         //\r
148 \r
149                         int i;\r
150                         for (i = 0; i < pos_arg_count; i++) {\r
151                                 Argument a = (Argument) pos_args [i];\r
152                                 Expression e;\r
153 \r
154                                 if (!a.Resolve (ec, Location))\r
155                                         return null;\r
156 \r
157                                 e = a.Expr;\r
158                                 if (e is Constant) {\r
159                                         pos_values [i] = ((Constant) e).GetValue ();\r
160                                 } else if (e is TypeOf) {\r
161                                         pos_values [i] = ((TypeOf) e).TypeArg;\r
162                                 } else {\r
163                                         Console.WriteLine ("Foo " + e);\r
164                                         Error_AttributeArgumentNotValid ();\r
165                                         return null;\r
166                                 }\r
167                                 \r
168                                 if (UsageAttr)\r
169                                         this.Targets = (AttributeTargets) pos_values [0];\r
170                                 \r
171                                 if (MethodImplAttr)\r
172                                         this.ImplOptions = (MethodImplOptions) pos_values [0];\r
173                                 \r
174                                 if (MarshalAsAttr)\r
175                                         this.UnmanagedType =\r
176                                         (System.Runtime.InteropServices.UnmanagedType) pos_values [0];\r
177                         }\r
178 \r
179                         //\r
180                         // Now process named arguments\r
181                         //\r
182 \r
183                         ArrayList field_infos = new ArrayList ();\r
184                         ArrayList prop_infos  = new ArrayList ();\r
185                         ArrayList field_values = new ArrayList ();\r
186                         ArrayList prop_values = new ArrayList ();\r
187                         \r
188                         for (i = 0; i < named_args.Count; i++) {\r
189                                 DictionaryEntry de = (DictionaryEntry) named_args [i];\r
190                                 string member_name = (string) de.Key;\r
191                                 Argument a  = (Argument) de.Value;\r
192                                 Expression e;\r
193                                 \r
194                                 if (!a.Resolve (ec, Location))\r
195                                         return null;\r
196 \r
197                                 Expression member = Expression.MemberLookup (\r
198                                         ec, Type, member_name,\r
199                                         MemberTypes.Field | MemberTypes.Property,\r
200                                         BindingFlags.Public | BindingFlags.Instance,\r
201                                         Location);\r
202 \r
203                                 if (member == null || !(member is PropertyExpr || member is FieldExpr)) {\r
204                                         Error_InvalidNamedArgument (member_name);\r
205                                         return null;\r
206                                 }\r
207 \r
208                                 e = a.Expr;\r
209                                 if (member is PropertyExpr) {\r
210                                         PropertyExpr pe = (PropertyExpr) member;\r
211                                         PropertyInfo pi = pe.PropertyInfo;\r
212 \r
213                                         if (!pi.CanWrite) {\r
214                                                 Error_InvalidNamedArgument (member_name);\r
215                                                 return null;\r
216                                         }\r
217 \r
218                                         if (e is Constant) {\r
219                                                 object o = ((Constant) e).GetValue ();\r
220                                                 prop_values.Add (o);\r
221                                                 \r
222                                                 if (UsageAttr) {\r
223                                                         if (member_name == "AllowMultiple")\r
224                                                                 this.AllowMultiple = (bool) o;\r
225                                                         if (member_name == "Inherited")\r
226                                                                 this.Inherited = (bool) o;\r
227                                                 }\r
228                                                 \r
229                                         } else if (e is TypeOf) {\r
230                                                 prop_values.Add (((TypeOf) e).TypeArg);\r
231                                         } else {\r
232                                                 Error_AttributeArgumentNotValid ();\r
233                                                 return null;\r
234                                         }\r
235                                         \r
236                                         prop_infos.Add (pi);\r
237                                         \r
238                                 } else if (member is FieldExpr) {\r
239                                         FieldExpr fe = (FieldExpr) member;\r
240                                         FieldInfo fi = fe.FieldInfo;\r
241 \r
242                                         if (fi.IsInitOnly) {\r
243                                                 Error_InvalidNamedArgument (member_name);\r
244                                                 return null;\r
245                                         }\r
246 \r
247                                         //\r
248                                         // Handle charset here, and set the TypeAttributes\r
249                                         \r
250                                         if (e is Constant){\r
251                                                 object value = ((Constant) e).GetValue ();\r
252                                                 \r
253                                                 field_values.Add (value);\r
254                                         } else if (e is TypeOf) {\r
255                                                 field_values.Add (((TypeOf) e).TypeArg);\r
256                                         } else {\r
257                                                 Error_AttributeArgumentNotValid ();\r
258                                                 return null;\r
259                                         }\r
260                                         \r
261                                         field_infos.Add (fi);\r
262                                 }\r
263                         }\r
264 \r
265                         Expression mg = Expression.MemberLookup (\r
266                                 ec, Type, ".ctor", MemberTypes.Constructor,\r
267                                 BindingFlags.Public | BindingFlags.Instance, Location);\r
268 \r
269                         if (mg == null) {\r
270                                 Error_AttributeConstructorMismatch (Location);\r
271                                 return null;\r
272                         }\r
273 \r
274                         MethodBase constructor = Invocation.OverloadResolve (\r
275                                 ec, (MethodGroupExpr) mg, pos_args, Location);\r
276 \r
277                         if (constructor == null) {\r
278                                 Error_AttributeConstructorMismatch (Location);\r
279                                 return null;\r
280                         }\r
281                         \r
282                         PropertyInfo [] prop_info_arr = new PropertyInfo [prop_infos.Count];\r
283                         FieldInfo [] field_info_arr = new FieldInfo [field_infos.Count];\r
284                         object [] field_values_arr = new object [field_values.Count];\r
285                         object [] prop_values_arr = new object [prop_values.Count];\r
286 \r
287                         field_infos.CopyTo  (field_info_arr, 0);\r
288                         field_values.CopyTo (field_values_arr, 0);\r
289 \r
290                         prop_values.CopyTo  (prop_values_arr, 0);\r
291                         prop_infos.CopyTo   (prop_info_arr, 0);\r
292 \r
293                         try {\r
294                                 cb = new CustomAttributeBuilder (\r
295                                         (ConstructorInfo) constructor, pos_values,\r
296                                         prop_info_arr, prop_values_arr,\r
297                                         field_info_arr, field_values_arr); \r
298                         } catch (NullReferenceException) {\r
299                                 Report.Warning (\r
300                                         -23, Location,\r
301                                         "The compiler can not encode this attribute in the Mono runtime\n" +\r
302                                         "\tdue to a known bug in it.  We know about the problem and will\n" +\r
303                                         "\tfix it as soon as possible.");\r
304                         } catch {\r
305                                 //\r
306                                 // Sample:\r
307                                 // using System.ComponentModel;\r
308                                 // [DefaultValue (CollectionChangeAction.Add)]\r
309                                 // class X { static void Main () {} }\r
310                                 //\r
311                                 Report.Warning (\r
312                                         -23, Location,\r
313                                         "The compiler can not encode this attribute in .NET due to\n" +\r
314                                         "\ta bug in the .NET runtime.  Try the Mono runtime");\r
315                         }\r
316                         \r
317                         return cb;\r
318                 }\r
319 \r
320                 static string GetValidPlaces (Attribute attr)\r
321                 {\r
322                         StringBuilder sb = new StringBuilder ();\r
323                         AttributeTargets targets = 0;\r
324                         \r
325                         TypeContainer a = TypeManager.LookupAttr (attr.Type);\r
326 \r
327                         if (a == null) {\r
328                                 \r
329                                 System.Attribute [] attrs = null;\r
330                                 \r
331                                 try {\r
332                                         attrs = System.Attribute.GetCustomAttributes (attr.Type);\r
333                                         \r
334                                 } catch {\r
335                                         Report.Error (-20, attr.Location, "Cannot find attribute type " + attr.Name +\r
336                                                       " (maybe you forgot to set the usage using the" +\r
337                                                       " AttributeUsage attribute ?).");\r
338                                         return null;\r
339                                 }\r
340                                         \r
341                                 foreach (System.Attribute tmp in attrs)\r
342                                         if (tmp is AttributeUsageAttribute) {\r
343                                                 targets = ((AttributeUsageAttribute) tmp).ValidOn;\r
344                                                 break;\r
345                                         }\r
346                         } else\r
347                                 targets = a.Targets;\r
348 \r
349                         \r
350                         if ((targets & AttributeTargets.Assembly) != 0)\r
351                                 sb.Append ("'assembly' ");\r
352 \r
353                         if ((targets & AttributeTargets.Class) != 0)\r
354                                 sb.Append ("'class' ");\r
355 \r
356                         if ((targets & AttributeTargets.Constructor) != 0)\r
357                                 sb.Append ("'constructor' ");\r
358 \r
359                         if ((targets & AttributeTargets.Delegate) != 0)\r
360                                 sb.Append ("'delegate' ");\r
361 \r
362                         if ((targets & AttributeTargets.Enum) != 0)\r
363                                 sb.Append ("'enum' ");\r
364 \r
365                         if ((targets & AttributeTargets.Event) != 0)\r
366                                 sb.Append ("'event' ");\r
367 \r
368                         if ((targets & AttributeTargets.Field) != 0)\r
369                                 sb.Append ("'field' ");\r
370 \r
371                         if ((targets & AttributeTargets.Interface) != 0)\r
372                                 sb.Append ("'interface' ");\r
373 \r
374                         if ((targets & AttributeTargets.Method) != 0)\r
375                                 sb.Append ("'method' ");\r
376 \r
377                         if ((targets & AttributeTargets.Module) != 0)\r
378                                 sb.Append ("'module' ");\r
379 \r
380                         if ((targets & AttributeTargets.Parameter) != 0)\r
381                                 sb.Append ("'parameter' ");\r
382 \r
383                         if ((targets & AttributeTargets.Property) != 0)\r
384                                 sb.Append ("'property' ");\r
385 \r
386                         if ((targets & AttributeTargets.ReturnValue) != 0)\r
387                                 sb.Append ("'return value' ");\r
388 \r
389                         if ((targets & AttributeTargets.Struct) != 0)\r
390                                 sb.Append ("'struct' ");\r
391 \r
392                         return sb.ToString ();\r
393 \r
394                 }\r
395 \r
396                 public static void Error_AttributeNotValidForElement (Attribute a, Location loc)\r
397                 {\r
398                         Report.Error (\r
399                                 592, loc, "Attribute '" + a.Name +\r
400                                 "' is not valid on this declaration type. " +\r
401                                 "It is valid on " + GetValidPlaces (a) + "declarations only.");\r
402                 }\r
403 \r
404                 public static bool CheckAttribute (Attribute a, object element)\r
405                 {\r
406                         TypeContainer attr = TypeManager.LookupAttr (a.Type);\r
407                         AttributeTargets targets = 0;\r
408 \r
409                         \r
410                         if (attr == null) {\r
411 \r
412                                 System.Attribute [] attrs = null;\r
413                                 \r
414                                 try {\r
415                                         attrs = System.Attribute.GetCustomAttributes (a.Type);\r
416 \r
417                                 } catch {\r
418                                         Report.Error (-20, a.Location, "Cannot find attribute type " + a.Name +\r
419                                                       " (maybe you forgot to set the usage using the" +\r
420                                                       " AttributeUsage attribute ?).");\r
421                                         return false;\r
422                                 }\r
423                                         \r
424                                 foreach (System.Attribute tmp in attrs)\r
425                                         if (tmp is AttributeUsageAttribute) \r
426                                                 targets = ((AttributeUsageAttribute) tmp).ValidOn;\r
427                         } else\r
428                                 targets = attr.Targets;\r
429 \r
430                         if (element is Class) {\r
431                                 if ((targets & AttributeTargets.Class) != 0)\r
432                                         return true;\r
433                                 else\r
434                                         return false;\r
435                                 \r
436                         } else if (element is Struct) {\r
437                                 if ((targets & AttributeTargets.Struct) != 0)\r
438                                         return true;\r
439                                 else\r
440                                         return false;\r
441                         } else if (element is Constructor) {\r
442                                 if ((targets & AttributeTargets.Constructor) != 0)\r
443                                         return true;\r
444                                 else\r
445                                         return false;\r
446                         } else if (element is Delegate) {\r
447                                 if ((targets & AttributeTargets.Delegate) != 0)\r
448                                         return true;\r
449                                 else\r
450                                         return false;\r
451                         } else if (element is Enum) {\r
452                                 if ((targets & AttributeTargets.Enum) != 0)\r
453                                         return true;\r
454                                 else\r
455                                         return false;\r
456                         } else if (element is Event || element is InterfaceEvent) {\r
457                                 if ((targets & AttributeTargets.Event) != 0)\r
458                                         return true;\r
459                                 else\r
460                                         return false;\r
461                         } else if (element is Field || element is FieldBuilder) {\r
462                                 if ((targets & AttributeTargets.Field) != 0)\r
463                                         return true;\r
464                                 else\r
465                                         return false;\r
466                         } else if (element is Interface) {\r
467                                 if ((targets & AttributeTargets.Interface) != 0)\r
468                                         return true;\r
469                                 else\r
470                                         return false;\r
471                         } else if (element is Method || element is Operator || element is InterfaceMethod || element is Accessor) {\r
472                                 if ((targets & AttributeTargets.Method) != 0)\r
473                                         return true;\r
474                                 else\r
475                                         return false;\r
476                         } else if (element is ParameterBuilder) {\r
477                                 if ((targets & AttributeTargets.Parameter) != 0)\r
478                                         return true;\r
479                                 else\r
480                                         return false;\r
481                         } else if (element is Property || element is Indexer ||\r
482                                    element is InterfaceProperty || element is InterfaceIndexer) {\r
483                                 if ((targets & AttributeTargets.Property) != 0)\r
484                                         return true;\r
485                                 else\r
486                                         return false;\r
487                         } else if (element is AssemblyBuilder){\r
488                                 if ((targets & AttributeTargets.Assembly) != 0)\r
489                                         return true;\r
490                                 else\r
491                                         return false;\r
492                         }\r
493 \r
494                         return false;\r
495                 }\r
496 \r
497                 //\r
498                 // This method should be invoked to pull the IndexerName attribute from an\r
499                 // Indexer if it exists.\r
500                 //\r
501                 public static string ScanForIndexerName (EmitContext ec, Attributes opt_attrs)\r
502                 {\r
503                         if (opt_attrs == null)\r
504                                 return null;\r
505                         if (opt_attrs.AttributeSections == null)\r
506                                 return null;\r
507 \r
508                         foreach (AttributeSection asec in opt_attrs.AttributeSections) {\r
509                                 if (asec.Attributes == null)\r
510                                         continue;\r
511 \r
512                                 foreach (Attribute a in asec.Attributes){\r
513                                         if (a.ResolveType (ec) == null)\r
514                                                 return null;\r
515                                         \r
516                                         if (a.Type != TypeManager.indexer_name_type)\r
517                                                 continue;\r
518 \r
519                                         //\r
520                                         // So we have found an IndexerName, pull the data out.\r
521                                         //\r
522                                         if (a.Arguments == null || a.Arguments [0] == null){\r
523                                                 Error_AttributeConstructorMismatch (a.Location);\r
524                                                 return null;\r
525                                         }\r
526                                         ArrayList pos_args = (ArrayList) a.Arguments [0];\r
527                                         if (pos_args.Count == 0){\r
528                                                 Error_AttributeConstructorMismatch (a.Location);\r
529                                                 return null;\r
530                                         }\r
531                                         \r
532                                         Argument arg = (Argument) pos_args [0];\r
533                                         if (!arg.Resolve (ec, a.Location))\r
534                                                 return null;\r
535                                         \r
536                                         Expression e = arg.Expr;\r
537                                         if (!(e is StringConstant)){\r
538                                                 Error_AttributeConstructorMismatch (a.Location);\r
539                                                 return null;\r
540                                         }\r
541 \r
542                                         //\r
543                                         // Remove the attribute from the list\r
544                                         //\r
545                                         asec.Attributes.Remove (a);\r
546 \r
547                                         return (((StringConstant) e).Value);\r
548                                 }\r
549                         }\r
550                         return null;\r
551                 }\r
552 \r
553                 //\r
554                 // This pulls the condition name out of a Conditional attribute\r
555                 //\r
556                 public string Conditional_GetConditionName ()\r
557                 {\r
558                         //\r
559                         // So we have a Conditional, pull the data out.\r
560                         //\r
561                         if (Arguments == null || Arguments [0] == null){\r
562                                 Error_AttributeConstructorMismatch (Location);\r
563                                 return null;\r
564                         }\r
565 \r
566                         ArrayList pos_args = (ArrayList) Arguments [0];\r
567                         if (pos_args.Count != 1){\r
568                                 Error_AttributeConstructorMismatch (Location);\r
569                                 return null;\r
570                         }\r
571 \r
572                         Argument arg = (Argument) pos_args [0]; \r
573                         if (!(arg.Expr is StringConstant)){\r
574                                 Error_AttributeConstructorMismatch (Location);\r
575                                 return null;\r
576                         }\r
577 \r
578                         return ((StringConstant) arg.Expr).Value;\r
579                 }\r
580 \r
581                 //\r
582                 // This pulls the obsolete message and error flag out of an Obsolete attribute\r
583                 //\r
584                 public string Obsolete_GetObsoleteMessage (out bool is_error)\r
585                 {\r
586                         is_error = false;\r
587                         //\r
588                         // So we have an Obsolete, pull the data out.\r
589                         //\r
590                         if (Arguments == null || Arguments [0] == null)\r
591                                 return "";\r
592 \r
593                         ArrayList pos_args = (ArrayList) Arguments [0];\r
594                         if (pos_args.Count == 0)\r
595                                 return "";\r
596                         else if (pos_args.Count > 2){\r
597                                 Error_AttributeConstructorMismatch (Location);\r
598                                 return null;\r
599                         }\r
600 \r
601                         Argument arg = (Argument) pos_args [0]; \r
602                         if (!(arg.Expr is StringConstant)){\r
603                                 Error_AttributeConstructorMismatch (Location);\r
604                                 return null;\r
605                         }\r
606 \r
607                         if (pos_args.Count == 2){\r
608                                 Argument arg2 = (Argument) pos_args [1];\r
609                                 if (!(arg2.Expr is BoolConstant)){\r
610                                         Error_AttributeConstructorMismatch (Location);\r
611                                         return null;\r
612                                 }\r
613                                 is_error = ((BoolConstant) arg2.Expr).Value;\r
614                         }\r
615 \r
616                         return ((StringConstant) arg.Expr).Value;\r
617                 }\r
618 \r
619                 //\r
620                 // Applies the attributes to the `builder'.\r
621                 //\r
622                 public static void ApplyAttributes (EmitContext ec, object builder, object kind,\r
623                                                     Attributes opt_attrs, Location loc)\r
624                 {\r
625                         if (opt_attrs == null)\r
626                                 return;\r
627                         if (opt_attrs.AttributeSections == null)\r
628                                 return;\r
629 \r
630                         foreach (AttributeSection asec in opt_attrs.AttributeSections) {\r
631                                 if (asec.Attributes == null)\r
632                                         continue;\r
633 \r
634                                 if (asec.Target == "assembly" && !(builder is AssemblyBuilder))\r
635                                         continue;\r
636                                 \r
637                                 foreach (Attribute a in asec.Attributes) {\r
638                                         CustomAttributeBuilder cb = a.Resolve (ec);\r
639 \r
640                                         if (cb == null)\r
641                                                 continue;\r
642 \r
643                                         if (!(kind is TypeContainer))\r
644                                                 if (!CheckAttribute (a, kind)) {\r
645                                                         Error_AttributeNotValidForElement (a, loc);\r
646                                                         return;\r
647                                                 }\r
648 \r
649                                         if (kind is Method || kind is Operator || kind is InterfaceMethod ||\r
650                                             kind is Accessor) {\r
651                                                 if (a.Type == TypeManager.methodimpl_attr_type) {\r
652                                                         if (a.ImplOptions == MethodImplOptions.InternalCall)\r
653                                                                 ((MethodBuilder) builder).\r
654                                                                 SetImplementationFlags (\r
655                                                                         MethodImplAttributes.InternalCall |\r
656                                                                         MethodImplAttributes.Runtime);\r
657                                                 } else if (a.Type != TypeManager.dllimport_type){\r
658                                                         ((MethodBuilder) builder).SetCustomAttribute (cb);\r
659                                                 }\r
660                                         } else if (kind is Constructor) {\r
661                                                 ((ConstructorBuilder) builder).SetCustomAttribute (cb);\r
662                                         } else if (kind is Field) {\r
663                                                 ((FieldBuilder) builder).SetCustomAttribute (cb);\r
664                                         } else if (kind is Property || kind is Indexer ||\r
665                                                    kind is InterfaceProperty || kind is InterfaceIndexer) {\r
666                                                 ((PropertyBuilder) builder).SetCustomAttribute (cb);\r
667                                         } else if (kind is Event || kind is InterfaceEvent) {\r
668                                                 ((MyEventBuilder) builder).SetCustomAttribute (cb);\r
669                                         } else if (kind is ParameterBuilder) {\r
670 \r
671                                                 if (a.Type == TypeManager.marshal_as_attr_type) {\r
672                                                         UnmanagedMarshal marshal =\r
673                                                                 UnmanagedMarshal.DefineUnmanagedMarshal (a.UnmanagedType);\r
674                                                         \r
675                                                         ((ParameterBuilder) builder).SetMarshal (marshal);\r
676                                                 } else \r
677                                                         ((ParameterBuilder) builder).SetCustomAttribute (cb);\r
678                                                 \r
679                                         } else if (kind is Enum) {\r
680                                                 ((TypeBuilder) builder).SetCustomAttribute (cb); \r
681 \r
682                                         } else if (kind is TypeContainer) {\r
683                                                 TypeContainer tc = (TypeContainer) kind;\r
684                                                 \r
685                                                 if (a.UsageAttr) {\r
686                                                         tc.Targets = a.Targets;\r
687                                                         tc.AllowMultiple = a.AllowMultiple;\r
688                                                         tc.Inherited = a.Inherited;\r
689                                                         \r
690                                                 } else if (a.Type == TypeManager.default_member_type) {\r
691                                                         if (tc.Indexers != null) {\r
692                                                                 Report.Error (646, loc,\r
693                                                                       "Cannot specify the DefaultMember attribute on" +\r
694                                                                       " a type containing an indexer");\r
695                                                                 return;\r
696                                                         }\r
697 \r
698                                                 } else {\r
699                                                         if (!CheckAttribute (a, kind)) {\r
700                                                                 Error_AttributeNotValidForElement (a, loc);\r
701                                                                 return;\r
702                                                         }\r
703                                                 }\r
704 \r
705                                                 try {\r
706                                                         ((TypeBuilder) builder).SetCustomAttribute (cb);\r
707                                                 } catch (System.ArgumentException) {\r
708                                                         Report.Warning (\r
709                                                                 -21, loc,\r
710                                                 "The CharSet named property on StructLayout\n"+\r
711                                                 "\tdoes not work correctly on Microsoft.NET\n"+\r
712                                                 "\tYou might want to remove the CharSet declaration\n"+\r
713                                                 "\tor compile using the Mono runtime instead of the\n"+\r
714                                                 "\tMicrosoft .NET runtime");\r
715                                                 }\r
716                                                 \r
717                                         } else if (kind is Interface) {\r
718                                                 Interface iface = (Interface) kind;\r
719 \r
720                                                 if ((a.Type == TypeManager.default_member_type) &&\r
721                                                     (iface.InterfaceIndexers != null)) {\r
722                                                         Report.Error (\r
723                                                                 646, loc,\r
724                                                                 "Cannot specify the DefaultMember attribute on" +\r
725                                                                 " a type containing an indexer");\r
726                                                         return;\r
727                                                 }\r
728 \r
729                                                 if (!CheckAttribute (a, kind)) {\r
730                                                         Error_AttributeNotValidForElement (a, loc);\r
731                                                         return;\r
732                                                 }\r
733 \r
734                                                 ((TypeBuilder) builder).SetCustomAttribute (cb);\r
735                                         } else if (kind is AssemblyBuilder){\r
736                                                 ((AssemblyBuilder) builder).SetCustomAttribute (cb);\r
737                                         } else if (kind is ModuleBuilder) {\r
738                                                 ((ModuleBuilder) builder).SetCustomAttribute (cb);\r
739                                         } else if (kind is FieldBuilder) {\r
740                                                 ((FieldBuilder) builder).SetCustomAttribute (cb);\r
741                                         } else\r
742                                                 throw new Exception ("Unknown kind: " + kind);\r
743                                 }\r
744                         }\r
745                 }\r
746 \r
747                 public MethodBuilder DefinePInvokeMethod (EmitContext ec, TypeBuilder builder, string name,\r
748                                                           MethodAttributes flags, Type ret_type, Type [] param_types)\r
749                 {\r
750                         //\r
751                         // We extract from the attribute the information we need \r
752                         //\r
753 \r
754                         if (Arguments == null) {\r
755                                 Console.WriteLine ("Internal error : this is not supposed to happen !");\r
756                                 return null;\r
757                         }\r
758 \r
759                         Type = CheckAttributeType (ec);\r
760                         if (Type == null)\r
761                                 return null;\r
762                         \r
763                         ArrayList named_args = new ArrayList ();\r
764                         \r
765                         ArrayList pos_args = (ArrayList) Arguments [0];\r
766                         if (Arguments.Count > 1)\r
767                                 named_args = (ArrayList) Arguments [1];\r
768                         \r
769 \r
770                         string dll_name = null;\r
771                         \r
772                         Argument tmp = (Argument) pos_args [0];\r
773 \r
774                         if (!tmp.Resolve (ec, Location))\r
775                                 return null;\r
776                         \r
777                         if (tmp.Expr is Constant)\r
778                                 dll_name = (string) ((Constant) tmp.Expr).GetValue ();\r
779                         else { \r
780                                 Error_AttributeArgumentNotValid ();\r
781                                 return null;\r
782                         }\r
783 \r
784                         // Now we process the named arguments\r
785                         CallingConvention cc = CallingConvention.Winapi;\r
786                         CharSet charset = CharSet.Ansi;\r
787                         bool preserve_sig = true;\r
788                         bool exact_spelling = false;\r
789                         bool set_last_err = false;\r
790                         string entry_point = null;\r
791 \r
792                         for (int i = 0; i < named_args.Count; i++) {\r
793 \r
794                                 DictionaryEntry de = (DictionaryEntry) named_args [i];\r
795 \r
796                                 string member_name = (string) de.Key;\r
797                                 Argument a  = (Argument) de.Value;\r
798 \r
799                                 if (!a.Resolve (ec, Location))\r
800                                         return null;\r
801 \r
802                                 Expression member = Expression.MemberLookup (\r
803                                         ec, Type, member_name, \r
804                                         MemberTypes.Field | MemberTypes.Property,\r
805                                         BindingFlags.Public | BindingFlags.Instance,\r
806                                         Location);\r
807 \r
808                                 if (member == null || !(member is FieldExpr)) {\r
809                                         Error_InvalidNamedArgument (member_name);\r
810                                         return null;\r
811                                 }\r
812 \r
813                                 if (member is FieldExpr) {\r
814                                         FieldExpr fe = (FieldExpr) member;\r
815                                         FieldInfo fi = fe.FieldInfo;\r
816 \r
817                                         if (fi.IsInitOnly) {\r
818                                                 Error_InvalidNamedArgument (member_name);\r
819                                                 return null;\r
820                                         }\r
821 \r
822                                         if (a.Expr is Constant) {\r
823                                                 Constant c = (Constant) a.Expr;\r
824                                                 \r
825                                                 if (member_name == "CallingConvention")\r
826                                                         cc = (CallingConvention) c.GetValue ();\r
827                                                 else if (member_name == "CharSet")\r
828                                                         charset = (CharSet) c.GetValue ();\r
829                                                 else if (member_name == "EntryPoint")\r
830                                                         entry_point = (string) c.GetValue ();\r
831                                                 else if (member_name == "SetLastError")\r
832                                                         set_last_err = (bool) c.GetValue ();\r
833                                                 else if (member_name == "ExactSpelling")\r
834                                                         exact_spelling = (bool) c.GetValue ();\r
835                                                 else if (member_name == "PreserveSig")\r
836                                                         preserve_sig = (bool) c.GetValue ();\r
837                                         } else { \r
838                                                 Error_AttributeArgumentNotValid ();\r
839                                                 return null;\r
840                                         }\r
841                                         \r
842                                 }\r
843                         }\r
844 \r
845                         if (entry_point == null)\r
846                                 entry_point = name;\r
847                         \r
848                         MethodBuilder mb = builder.DefinePInvokeMethod (\r
849                                 name, dll_name, entry_point, flags | MethodAttributes.HideBySig,\r
850                                 CallingConventions.Standard,\r
851                                 ret_type,\r
852                                 param_types,\r
853                                 cc,\r
854                                 charset);\r
855 \r
856                         if (preserve_sig)\r
857                                 mb.SetImplementationFlags (MethodImplAttributes.PreserveSig);\r
858                         \r
859                         return mb;\r
860                 }\r
861                 \r
862         }\r
863         \r
864         public class AttributeSection {\r
865                 \r
866                 public readonly string    Target;\r
867                 public readonly ArrayList Attributes;\r
868                 \r
869                 public AttributeSection (string target, ArrayList attrs)\r
870                 {\r
871                         Target = target;\r
872                         Attributes = attrs;\r
873                 }\r
874                 \r
875         }\r
876 \r
877         public class Attributes {\r
878                 public ArrayList AttributeSections;\r
879                 public Location Location;\r
880 \r
881                 public Attributes (AttributeSection a, Location loc)\r
882                 {\r
883                         AttributeSections = new ArrayList ();\r
884                         AttributeSections.Add (a);\r
885 \r
886                 }\r
887 \r
888                 public void AddAttribute (AttributeSection a)\r
889                 {\r
890                         if (a != null)\r
891                                 AttributeSections.Add (a);\r
892                 }\r
893         }\r
894 }\r