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