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