2005-04-13 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / mcs / codegen.cs
1 //
2 // codegen.cs: The code generator
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) 2001, 2002, 2003 Ximian, Inc.
8 // (C) 2004 Novell, Inc.
9 //
10 //#define PRODUCTION
11 using System;
12 using System.IO;
13 using System.Collections;
14 using System.Collections.Specialized;
15 using System.Reflection;
16 using System.Reflection.Emit;
17 using System.Runtime.InteropServices;
18 using System.Security;
19 using System.Security.Cryptography;
20 using System.Security.Permissions;
21
22 using Mono.Security.Cryptography;
23
24 namespace Mono.CSharp {
25
26         /// <summary>
27         ///    Code generator class.
28         /// </summary>
29         public class CodeGen {
30                 static AppDomain current_domain;
31                 static public SymbolWriter SymbolWriter;
32
33                 public static AssemblyClass Assembly;
34                 public static ModuleClass Module;
35
36                 static CodeGen ()
37                 {
38                         Reset ();
39                 }
40
41                 public static void Reset ()
42                 {
43                         Assembly = new AssemblyClass ();
44                         Module = new ModuleClass (RootContext.Unsafe);
45                 }
46
47                 public static string Basename (string name)
48                 {
49                         int pos = name.LastIndexOf ('/');
50
51                         if (pos != -1)
52                                 return name.Substring (pos + 1);
53
54                         pos = name.LastIndexOf ('\\');
55                         if (pos != -1)
56                                 return name.Substring (pos + 1);
57
58                         return name;
59                 }
60
61                 public static string Dirname (string name)
62                 {
63                         int pos = name.LastIndexOf ('/');
64
65                         if (pos != -1)
66                                 return name.Substring (0, pos);
67
68                         pos = name.LastIndexOf ('\\');
69                         if (pos != -1)
70                                 return name.Substring (0, pos);
71
72                         return ".";
73                 }
74
75                 static public string FileName;
76
77                 //
78                 // Initializes the symbol writer
79                 //
80                 static void InitializeSymbolWriter (string filename)
81                 {
82                         SymbolWriter = SymbolWriter.GetSymbolWriter (Module.Builder, filename);
83
84                         //
85                         // If we got an ISymbolWriter instance, initialize it.
86                         //
87                         if (SymbolWriter == null) {
88                                 Report.Warning (
89                                         -18, "Could not find the symbol writer assembly (Mono.CompilerServices.SymbolWriter.dll). This is normally an installation problem. Please make sure to compile and install the mcs/class/Mono.CompilerServices.SymbolWriter directory.");
90                                 return;
91                         }
92                 }
93
94                 //
95                 // Initializes the code generator variables
96                 //
97                 static public bool Init (string name, string output, bool want_debugging_support)
98                 {
99                         FileName = output;
100                         AssemblyName an = Assembly.GetAssemblyName (name, output);
101                         if (an == null)
102                                 return false;
103
104                         if (an.KeyPair != null) {
105                                 // If we are going to strong name our assembly make
106                                 // sure all its refs are strong named
107                                 foreach (Assembly a in TypeManager.GetAssemblies ()) {
108                                         AssemblyName ref_name = a.GetName ();
109                                         byte [] b = ref_name.GetPublicKeyToken ();
110                                         if (b == null || b.Length == 0) {
111                                                 Report.Warning (1577, "Assembly generation failed " +
112                                                                 "-- Referenced assembly '" +
113                                                                 ref_name.Name +
114                                                                 "' does not have a strong name.");
115                                                 //Environment.Exit (1);
116                                         }
117                                 }
118                         }
119                         
120                         current_domain = AppDomain.CurrentDomain;
121
122                         try {
123                                 Assembly.Builder = current_domain.DefineDynamicAssembly (an,
124                                         AssemblyBuilderAccess.Save, Dirname (name));
125                         }
126                         catch (ArgumentException) {
127                                 // specified key may not be exportable outside it's container
128                                 if (RootContext.StrongNameKeyContainer != null) {
129                                         Report.Error (1548, "Could not access the key inside the container `" +
130                                                 RootContext.StrongNameKeyContainer + "'.");
131                                         Environment.Exit (1);
132                                 }
133                                 return false;
134                         }
135                         catch (CryptographicException) {
136                                 if ((RootContext.StrongNameKeyContainer != null) || (RootContext.StrongNameKeyFile != null)) {
137                                         Report.Error (1548, "Could not use the specified key to strongname the assembly.");
138                                         Environment.Exit (1);
139                                 }
140                                 return false;
141                         }
142
143                         //
144                         // Pass a path-less name to DefineDynamicModule.  Wonder how
145                         // this copes with output in different directories then.
146                         // FIXME: figure out how this copes with --output /tmp/blah
147                         //
148                         // If the third argument is true, the ModuleBuilder will dynamically
149                         // load the default symbol writer.
150                         //
151                         Module.Builder = Assembly.Builder.DefineDynamicModule (
152                                 Basename (name), Basename (output), false);
153
154                         if (want_debugging_support)
155                                 InitializeSymbolWriter (output);
156
157                         return true;
158                 }
159
160                 static public void Save (string name)
161                 {
162                         try {
163                                 Assembly.Builder.Save (Basename (name));
164                         }
165                         catch (COMException) {
166                                 if ((RootContext.StrongNameKeyFile == null) || (!RootContext.StrongNameDelaySign))
167                                         throw;
168
169                                 // FIXME: it seems Microsoft AssemblyBuilder doesn't like to delay sign assemblies 
170                                 Report.Error (1548, "Couldn't delay-sign the assembly with the '" +
171                                         RootContext.StrongNameKeyFile +
172                                         "', Use MCS with the Mono runtime or CSC to compile this assembly.");
173                         }
174                         catch (System.IO.IOException io) {
175                                 Report.Error (16, "Could not write to file `"+name+"', cause: " + io.Message);
176                         }
177                         catch (System.UnauthorizedAccessException ua) {
178                                 Report.Error (16, "Could not write to file `"+name+"', cause: " + ua.Message);
179                         }
180
181                         if (SymbolWriter != null)
182                                 SymbolWriter.WriteSymbolFile ();
183                 }
184         }
185
186         //
187         // Provides "local" store across code that can yield: locals
188         // or fields, notice that this should not be used by anonymous
189         // methods to create local storage, those only require
190         // variable mapping.
191         //
192         public class VariableStorage {
193                 FieldBuilder fb;
194                 LocalBuilder local;
195                 
196                 static int count;
197                 
198                 public VariableStorage (EmitContext ec, Type t)
199                 {
200                         count++;
201                         if (ec.InIterator)
202                                 fb = ec.CurrentIterator.MapVariable ("s_", count.ToString (), t);
203                         else
204                                 local = ec.ig.DeclareLocal (t);
205                 }
206
207                 public void EmitThis (ILGenerator ig)
208                 {
209                         if (fb != null)
210                                 ig.Emit (OpCodes.Ldarg_0);
211                 }
212
213                 public void EmitStore (ILGenerator ig)
214                 {
215                         if (fb == null)
216                                 ig.Emit (OpCodes.Stloc, local);
217                         else
218                                 ig.Emit (OpCodes.Stfld, fb);
219                 }
220
221                 public void EmitLoad (ILGenerator ig)
222                 {
223                         if (fb == null)
224                                 ig.Emit (OpCodes.Ldloc, local);
225                         else 
226                                 ig.Emit (OpCodes.Ldfld, fb);
227                 }
228
229                 public void EmitLoadAddress (ILGenerator ig)
230                 {
231                         if (fb == null)
232                                 ig.Emit (OpCodes.Ldloca, local);
233                         else 
234                                 ig.Emit (OpCodes.Ldflda, fb);
235                 }
236                 
237                 public void EmitCall (ILGenerator ig, MethodInfo mi)
238                 {
239                         // FIXME : we should handle a call like tostring
240                         // here, where boxing is needed. However, we will
241                         // never encounter that with the current usage.
242                         
243                         bool value_type_call;
244                         EmitThis (ig);
245                         if (fb == null) {
246                                 value_type_call = local.LocalType.IsValueType;
247                                 
248                                 if (value_type_call)
249                                         ig.Emit (OpCodes.Ldloca, local);
250                                 else
251                                         ig.Emit (OpCodes.Ldloc, local);
252                         } else {
253                                 value_type_call = fb.FieldType.IsValueType;
254                                 
255                                 if (value_type_call)
256                                         ig.Emit (OpCodes.Ldflda, fb);
257                                 else
258                                         ig.Emit (OpCodes.Ldfld, fb);
259                         }
260                         
261                         ig.Emit (value_type_call ? OpCodes.Call : OpCodes.Callvirt, mi);
262                 }
263         }
264
265         /// <summary>
266         ///   An Emit Context is created for each body of code (from methods,
267         ///   properties bodies, indexer bodies or constructor bodies)
268         /// </summary>
269         public class EmitContext {
270                 public DeclSpace DeclSpace;
271                 public DeclSpace TypeContainer;
272                 public ILGenerator   ig;
273
274                 /// <summary>
275                 ///   This variable tracks the `checked' state of the compilation,
276                 ///   it controls whether we should generate code that does overflow
277                 ///   checking, or if we generate code that ignores overflows.
278                 ///
279                 ///   The default setting comes from the command line option to generate
280                 ///   checked or unchecked code plus any source code changes using the
281                 ///   checked/unchecked statements or expressions.   Contrast this with
282                 ///   the ConstantCheckState flag.
283                 /// </summary>
284                 
285                 public bool CheckState;
286
287                 /// <summary>
288                 ///   The constant check state is always set to `true' and cant be changed
289                 ///   from the command line.  The source code can change this setting with
290                 ///   the `checked' and `unchecked' statements and expressions. 
291                 /// </summary>
292                 public bool ConstantCheckState;
293
294                 /// <summary>
295                 ///   Whether we are emitting code inside a static or instance method
296                 /// </summary>
297                 public bool IsStatic;
298
299                 /// <summary>
300                 ///   Whether the actual created method is static or instance method.
301                 ///   Althoug the method might be declared as `static', if an anonymous
302                 ///   method is involved, we might turn this into an instance method.
303                 ///
304                 ///   So this reflects the low-level staticness of the method, while
305                 ///   IsStatic represents the semantic, high-level staticness.
306                 /// </summary>
307                 public bool MethodIsStatic;
308
309                 /// <summary>
310                 ///   Whether we are emitting a field initializer
311                 /// </summary>
312                 public bool IsFieldInitializer;
313
314                 /// <summary>
315                 ///   The value that is allowed to be returned or NULL if there is no
316                 ///   return type.
317                 /// </summary>
318                 public Type ReturnType;
319
320                 /// <summary>
321                 ///   Points to the Type (extracted from the TypeContainer) that
322                 ///   declares this body of code
323                 /// </summary>
324                 public Type ContainerType;
325                 
326                 /// <summary>
327                 ///   Whether this is generating code for a constructor
328                 /// </summary>
329                 public bool IsConstructor;
330
331                 /// <summary>
332                 ///   Whether we're control flow analysis enabled
333                 /// </summary>
334                 public bool DoFlowAnalysis;
335                 
336                 /// <summary>
337                 ///   Keeps track of the Type to LocalBuilder temporary storage created
338                 ///   to store structures (used to compute the address of the structure
339                 ///   value on structure method invocations)
340                 /// </summary>
341                 public Hashtable temporary_storage;
342
343                 public Block CurrentBlock;
344
345                 public int CurrentFile;
346
347                 /// <summary>
348                 ///   The location where we store the return value.
349                 /// </summary>
350                 LocalBuilder return_value;
351
352                 /// <summary>
353                 ///   The location where return has to jump to return the
354                 ///   value
355                 /// </summary>
356                 public Label ReturnLabel;
357
358                 /// <summary>
359                 ///   If we already defined the ReturnLabel
360                 /// </summary>
361                 public bool HasReturnLabel;
362
363                 /// <summary>
364                 ///   Whether we are inside an iterator block.
365                 /// </summary>
366                 public bool InIterator;
367
368                 public bool IsLastStatement;
369
370                 /// <summary>
371                 ///   Whether remapping of locals, parameters and fields is turned on.
372                 ///   Used by iterators and anonymous methods.
373                 /// </summary>
374                 public bool RemapToProxy;
375
376                 /// <summary>
377                 ///  Whether we are inside an unsafe block
378                 /// </summary>
379                 public bool InUnsafe;
380
381                 /// <summary>
382                 ///  Whether we are in a `fixed' initialization
383                 /// </summary>
384                 public bool InFixedInitializer;
385
386                 public bool InCatch;
387                 public bool InFinally;
388
389                 /// <summary>
390                 ///  Whether we are inside an anonymous method.
391                 /// </summary>
392                 public AnonymousMethod CurrentAnonymousMethod;
393                 
394                 /// <summary>
395                 ///   Location for this EmitContext
396                 /// </summary>
397                 public Location loc;
398
399                 /// <summary>
400                 ///   Inside an enum definition, we do not resolve enumeration values
401                 ///   to their enumerations, but rather to the underlying type/value
402                 ///   This is so EnumVal + EnumValB can be evaluated.
403                 ///
404                 ///   There is no "E operator + (E x, E y)", so during an enum evaluation
405                 ///   we relax the rules
406                 /// </summary>
407                 public bool InEnumContext;
408
409                 /// <summary>
410                 ///   Anonymous methods can capture local variables and fields,
411                 ///   this object tracks it.  It is copied from the TopLevelBlock
412                 ///   field.
413                 /// </summary>
414                 public CaptureContext capture_context;
415
416                 /// <summary>
417                 /// Trace when method is called and is obsolete then this member suppress message
418                 /// when call is inside next [Obsolete] method or type.
419                 /// </summary>
420                 public bool TestObsoleteMethodUsage = true;
421
422                 /// <summary>
423                 ///    The current iterator
424                 /// </summary>
425                 public Iterator CurrentIterator;
426
427                 /// <summary>
428                 ///    Whether we are in the resolving stage or not
429                 /// </summary>
430                 enum Phase {
431                         Created,
432                         Resolving,
433                         Emitting
434                 }
435                 
436                 Phase current_phase;
437                 FlowBranching current_flow_branching;
438                 
439                 public EmitContext (DeclSpace parent, DeclSpace ds, Location l, ILGenerator ig,
440                                     Type return_type, int code_flags, bool is_constructor)
441                 {
442                         this.ig = ig;
443
444                         TypeContainer = parent;
445                         DeclSpace = ds;
446                         CheckState = RootContext.Checked;
447                         ConstantCheckState = true;
448
449                         IsStatic = (code_flags & Modifiers.STATIC) != 0;
450                         MethodIsStatic = IsStatic;
451                         InIterator = (code_flags & Modifiers.METHOD_YIELDS) != 0;
452                         RemapToProxy = InIterator;
453                         ReturnType = return_type;
454                         IsConstructor = is_constructor;
455                         CurrentBlock = null;
456                         CurrentFile = 0;
457                         current_phase = Phase.Created;
458                         
459                         if (parent != null){
460                                 // Can only be null for the ResolveType contexts.
461                                 ContainerType = parent.TypeBuilder;
462                                 if (parent.UnsafeContext)
463                                         InUnsafe = true;
464                                 else
465                                         InUnsafe = (code_flags & Modifiers.UNSAFE) != 0;
466                         }
467                         loc = l;
468
469                         if (ReturnType == TypeManager.void_type)
470                                 ReturnType = null;
471                 }
472
473                 public EmitContext (TypeContainer tc, Location l, ILGenerator ig,
474                                     Type return_type, int code_flags, bool is_constructor)
475                         : this (tc, tc, l, ig, return_type, code_flags, is_constructor)
476                 {
477                 }
478
479                 public EmitContext (TypeContainer tc, Location l, ILGenerator ig,
480                                     Type return_type, int code_flags)
481                         : this (tc, tc, l, ig, return_type, code_flags, false)
482                 {
483                 }
484
485                 public FlowBranching CurrentBranching {
486                         get {
487                                 return current_flow_branching;
488                         }
489                 }
490
491                 public bool HaveCaptureInfo {
492                         get {
493                                 return capture_context != null;
494                         }
495                 }
496
497                 // <summary>
498                 //   Starts a new code branching.  This inherits the state of all local
499                 //   variables and parameters from the current branching.
500                 // </summary>
501                 public FlowBranching StartFlowBranching (FlowBranching.BranchingType type, Location loc)
502                 {
503                         current_flow_branching = FlowBranching.CreateBranching (CurrentBranching, type, null, loc);
504                         return current_flow_branching;
505                 }
506
507                 // <summary>
508                 //   Starts a new code branching for block `block'.
509                 // </summary>
510                 public FlowBranching StartFlowBranching (Block block)
511                 {
512                         FlowBranching.BranchingType type;
513
514                         if (CurrentBranching.Type == FlowBranching.BranchingType.Switch)
515                                 type = FlowBranching.BranchingType.SwitchSection;
516                         else
517                                 type = FlowBranching.BranchingType.Block;
518
519                         current_flow_branching = FlowBranching.CreateBranching (CurrentBranching, type, block, block.StartLocation);
520                         return current_flow_branching;
521                 }
522
523                 public FlowBranchingException StartFlowBranching (ExceptionStatement stmt)
524                 {
525                         FlowBranchingException branching = new FlowBranchingException (
526                                 CurrentBranching, stmt);
527                         current_flow_branching = branching;
528                         return branching;
529                 }
530
531                 // <summary>
532                 //   Ends a code branching.  Merges the state of locals and parameters
533                 //   from all the children of the ending branching.
534                 // </summary>
535                 public FlowBranching.UsageVector DoEndFlowBranching ()
536                 {
537                         FlowBranching old = current_flow_branching;
538                         current_flow_branching = current_flow_branching.Parent;
539
540                         return current_flow_branching.MergeChild (old);
541                 }
542
543                 // <summary>
544                 //   Ends a code branching.  Merges the state of locals and parameters
545                 //   from all the children of the ending branching.
546                 // </summary>
547                 public FlowBranching.Reachability EndFlowBranching ()
548                 {
549                         FlowBranching.UsageVector vector = DoEndFlowBranching ();
550
551                         return vector.Reachability;
552                 }
553
554                 // <summary>
555                 //   Kills the current code branching.  This throws away any changed state
556                 //   information and should only be used in case of an error.
557                 // </summary>
558                 public void KillFlowBranching ()
559                 {
560                         current_flow_branching = current_flow_branching.Parent;
561                 }
562
563                 public void CaptureVariable (LocalInfo li)
564                 {
565                         capture_context.AddLocal (CurrentAnonymousMethod, li);
566                         li.IsCaptured = true;
567                 }
568
569                 public void CaptureParameter (string name, Type t, int idx)
570                 {
571                         capture_context.AddParameter (this, CurrentAnonymousMethod, name, t, idx);
572                 }
573
574                 public void CaptureThis ()
575                 {
576                         capture_context.CaptureThis ();
577                 }
578                 
579                 
580                 //
581                 // Use to register a field as captured
582                 //
583                 public void CaptureField (FieldExpr fe)
584                 {
585                         capture_context.AddField (fe);
586                 }
587
588                 //
589                 // Whether anonymous methods have captured variables
590                 //
591                 public bool HaveCapturedVariables ()
592                 {
593                         if (capture_context != null)
594                                 return capture_context.HaveCapturedVariables;
595                         return false;
596                 }
597
598                 //
599                 // Whether anonymous methods have captured fields or this.
600                 //
601                 public bool HaveCapturedFields ()
602                 {
603                         if (capture_context != null)
604                                 return capture_context.HaveCapturedFields;
605                         return false;
606                 }
607
608                 //
609                 // Emits the instance pointer for the host method
610                 //
611                 public void EmitMethodHostInstance (EmitContext target, AnonymousMethod am)
612                 {
613                         if (capture_context != null)
614                                 capture_context.EmitMethodHostInstance (target, am);
615                         else if (IsStatic)
616                                 target.ig.Emit (OpCodes.Ldnull);
617                         else
618                                 target.ig.Emit (OpCodes.Ldarg_0);
619                 }
620
621                 //
622                 // Returns whether the `local' variable has been captured by an anonymous
623                 // method
624                 //
625                 public bool IsCaptured (LocalInfo local)
626                 {
627                         return capture_context.IsCaptured (local);
628                 }
629
630                 public bool IsParameterCaptured (string name)
631                 {
632                         if (capture_context != null)
633                                 return capture_context.IsParameterCaptured (name);
634                         return false;
635                 }
636                 
637                 public void EmitMeta (ToplevelBlock b, InternalParameters ip)
638                 {
639                         if (capture_context != null)
640                                 capture_context.EmitAnonymousHelperClasses (this);
641                         b.EmitMeta (this);
642
643                         if (HasReturnLabel)
644                                 ReturnLabel = ig.DefineLabel ();
645                 }
646
647                 //
648                 // Here until we can fix the problem with Mono.CSharp.Switch, which
649                 // currently can not cope with ig == null during resolve (which must
650                 // be fixed for switch statements to work on anonymous methods).
651                 //
652                 public void EmitTopBlock (ToplevelBlock block, InternalParameters ip, Location loc)
653                 {
654                         if (block == null)
655                                 return;
656                         
657                         bool unreachable;
658                         
659                         if (ResolveTopBlock (null, block, ip, loc, out unreachable)){
660                                 EmitMeta (block, ip);
661
662                                 current_phase = Phase.Emitting;
663                                 EmitResolvedTopBlock (block, unreachable);
664                         }
665                 }
666
667                 public bool ResolveTopBlock (EmitContext anonymous_method_host, ToplevelBlock block,
668                                              InternalParameters ip, Location loc, out bool unreachable)
669                 {
670                         current_phase = Phase.Resolving;
671                         
672                         unreachable = false;
673
674                         capture_context = block.CaptureContext;
675                         
676                         if (!Location.IsNull (loc))
677                                 CurrentFile = loc.File;
678
679 #if PRODUCTION
680                         try {
681 #endif
682                                 int errors = Report.Errors;
683
684                                 block.ResolveMeta (block, this, ip);
685                                 if (Report.Errors != errors)
686                                         return false;
687
688                                         bool old_do_flow_analysis = DoFlowAnalysis;
689                                         DoFlowAnalysis = true;
690
691                                         if (anonymous_method_host != null)
692                                                 current_flow_branching = FlowBranching.CreateBranching (
693                                                 anonymous_method_host.CurrentBranching, FlowBranching.BranchingType.Block,
694                                                 block, loc);
695                                         else 
696                                                 current_flow_branching = FlowBranching.CreateBranching (
697                                                         null, FlowBranching.BranchingType.Block, block, loc);
698
699                                         if (!block.Resolve (this)) {
700                                                 current_flow_branching = null;
701                                                 DoFlowAnalysis = old_do_flow_analysis;
702                                                 return false;
703                                         }
704
705                                         FlowBranching.Reachability reachability = current_flow_branching.MergeTopBlock ();
706                                         current_flow_branching = null;
707                                         
708                                         DoFlowAnalysis = old_do_flow_analysis;
709
710                                         if (reachability.AlwaysReturns ||
711                                             reachability.AlwaysThrows ||
712                                             reachability.IsUnreachable)
713                                                 unreachable = true;
714 #if PRODUCTION
715                         } catch (Exception e) {
716                                         Console.WriteLine ("Exception caught by the compiler while compiling:");
717                                         Console.WriteLine ("   Block that caused the problem begin at: " + loc);
718                                         
719                                         if (CurrentBlock != null){
720                                                 Console.WriteLine ("                     Block being compiled: [{0},{1}]",
721                                                                    CurrentBlock.StartLocation, CurrentBlock.EndLocation);
722                                         }
723                                         Console.WriteLine (e.GetType ().FullName + ": " + e.Message);
724                                         throw;
725                         }
726 #endif
727
728                         if (ReturnType != null && !unreachable){
729                                 if (!InIterator){
730                                         if (CurrentAnonymousMethod != null){
731                                                 Report.Error (1643, loc, "Not all code paths return a value in anonymous method of type `{0}'",
732                                                               CurrentAnonymousMethod.Type);
733                                         } else {
734                                                 Report.Error (161, loc, "Not all code paths return a value");
735                                         }
736                                         
737                                         return false;
738                                 }
739                         }
740                         block.CompleteContexts ();
741
742                         return true;
743                 }
744
745                 public void EmitResolvedTopBlock (ToplevelBlock block, bool unreachable)
746                 {
747                         if (block != null)
748                                 block.Emit (this);
749                         
750                         if (HasReturnLabel)
751                                 ig.MarkLabel (ReturnLabel);
752                         
753                         if (return_value != null){
754                                 ig.Emit (OpCodes.Ldloc, return_value);
755                                 ig.Emit (OpCodes.Ret);
756                         } else {
757                                 //
758                                 // If `HasReturnLabel' is set, then we already emitted a
759                                 // jump to the end of the method, so we must emit a `ret'
760                                 // there.
761                                 //
762                                 // Unfortunately, System.Reflection.Emit automatically emits
763                                 // a leave to the end of a finally block.  This is a problem
764                                 // if no code is following the try/finally block since we may
765                                 // jump to a point after the end of the method.
766                                 // As a workaround, we're always creating a return label in
767                                 // this case.
768                                 //
769
770                                 if ((block != null) && block.IsDestructor) {
771                                         // Nothing to do; S.R.E automatically emits a leave.
772                                 } else if (HasReturnLabel || (!unreachable && !InIterator)) {
773                                         if (ReturnType != null)
774                                                 ig.Emit (OpCodes.Ldloc, TemporaryReturn ());
775                                         ig.Emit (OpCodes.Ret);
776                                 }
777                         }
778
779                         //
780                         // Close pending helper classes if we are the toplevel
781                         //
782                         if (capture_context != null && capture_context.ParentToplevel == null)
783                                 capture_context.CloseAnonymousHelperClasses ();
784                 }
785
786                 /// <summary>
787                 ///   This is called immediately before emitting an IL opcode to tell the symbol
788                 ///   writer to which source line this opcode belongs.
789                 /// </summary>
790                 public void Mark (Location loc, bool check_file)
791                 {
792                         if ((CodeGen.SymbolWriter == null) || Location.IsNull (loc))
793                                 return;
794
795                         if (check_file && (CurrentFile != loc.File))
796                                 return;
797
798                         CodeGen.SymbolWriter.MarkSequencePoint (ig, loc.Row, 0);
799                 }
800
801                 public void DefineLocalVariable (string name, LocalBuilder builder)
802                 {
803                         if (CodeGen.SymbolWriter == null)
804                                 return;
805
806                         CodeGen.SymbolWriter.DefineLocalVariable (name, builder);
807                 }
808
809                 public void BeginScope ()
810                 {
811                         ig.BeginScope();
812
813                         if (CodeGen.SymbolWriter != null)
814                                 CodeGen.SymbolWriter.OpenScope(ig);
815                 }
816
817                 public void EndScope ()
818                 {
819                         ig.EndScope();
820
821                         if (CodeGen.SymbolWriter != null)
822                                 CodeGen.SymbolWriter.CloseScope(ig);
823                 }
824
825                 /// <summary>
826                 ///   Returns a temporary storage for a variable of type t as 
827                 ///   a local variable in the current body.
828                 /// </summary>
829                 public LocalBuilder GetTemporaryLocal (Type t)
830                 {
831                         LocalBuilder location = null;
832                         
833                         if (temporary_storage != null){
834                                 object o = temporary_storage [t];
835                                 if (o != null){
836                                         if (o is ArrayList){
837                                                 ArrayList al = (ArrayList) o;
838                                                 
839                                                 for (int i = 0; i < al.Count; i++){
840                                                         if (al [i] != null){
841                                                                 location = (LocalBuilder) al [i];
842                                                                 al [i] = null;
843                                                                 break;
844                                                         }
845                                                 }
846                                         } else
847                                                 location = (LocalBuilder) o;
848                                         if (location != null)
849                                                 return location;
850                                 }
851                         }
852                         
853                         return ig.DeclareLocal (t);
854                 }
855
856                 public void FreeTemporaryLocal (LocalBuilder b, Type t)
857                 {
858                         if (temporary_storage == null){
859                                 temporary_storage = new Hashtable ();
860                                 temporary_storage [t] = b;
861                                 return;
862                         }
863                         object o = temporary_storage [t];
864                         if (o == null){
865                                 temporary_storage [t] = b;
866                                 return;
867                         }
868                         if (o is ArrayList){
869                                 ArrayList al = (ArrayList) o;
870                                 for (int i = 0; i < al.Count; i++){
871                                         if (al [i] == null){
872                                                 al [i] = b;
873                                                 return;
874                                         }
875                                 }
876                                 al.Add (b);
877                                 return;
878                         }
879                         ArrayList replacement = new ArrayList ();
880                         replacement.Add (o);
881                         temporary_storage.Remove (t);
882                         temporary_storage [t] = replacement;
883                 }
884
885                 /// <summary>
886                 ///   Current loop begin and end labels.
887                 /// </summary>
888                 public Label LoopBegin, LoopEnd;
889
890                 /// <summary>
891                 ///   Default target in a switch statement.   Only valid if
892                 ///   InSwitch is true
893                 /// </summary>
894                 public Label DefaultTarget;
895
896                 /// <summary>
897                 ///   If this is non-null, points to the current switch statement
898                 /// </summary>
899                 public Switch Switch;
900
901                 /// <summary>
902                 ///   ReturnValue creates on demand the LocalBuilder for the
903                 ///   return value from the function.  By default this is not
904                 ///   used.  This is only required when returns are found inside
905                 ///   Try or Catch statements.
906                 ///
907                 ///   This method is typically invoked from the Emit phase, so
908                 ///   we allow the creation of a return label if it was not
909                 ///   requested during the resolution phase.   Could be cleaned
910                 ///   up, but it would replicate a lot of logic in the Emit phase
911                 ///   of the code that uses it.
912                 /// </summary>
913                 public LocalBuilder TemporaryReturn ()
914                 {
915                         if (return_value == null){
916                                 return_value = ig.DeclareLocal (ReturnType);
917                                 if (!HasReturnLabel){
918                                         ReturnLabel = ig.DefineLabel ();
919                                         HasReturnLabel = true;
920                                 }
921                         }
922
923                         return return_value;
924                 }
925
926                 /// <summary>
927                 ///   This method is used during the Resolution phase to flag the
928                 ///   need to define the ReturnLabel
929                 /// </summary>
930                 public void NeedReturnLabel ()
931                 {
932                         if (current_phase != Phase.Resolving){
933                                 //
934                                 // The reason is that the `ReturnLabel' is declared between
935                                 // resolution and emission
936                                 // 
937                                 throw new Exception ("NeedReturnLabel called from Emit phase, should only be called during Resolve");
938                         }
939                         
940                         if (!InIterator && !HasReturnLabel) 
941                                 HasReturnLabel = true;
942                 }
943
944                 //
945                 // Creates a field `name' with the type `t' on the proxy class
946                 //
947                 public FieldBuilder MapVariable (string name, Type t)
948                 {
949                         if (InIterator)
950                                 return CurrentIterator.MapVariable ("v_", name, t);
951
952                         throw new Exception ("MapVariable for an unknown state");
953                 }
954
955                 public Expression RemapParameter (int idx)
956                 {
957                         FieldExpr fe = new FieldExprNoAddress (CurrentIterator.parameter_fields [idx].FieldBuilder, loc);
958                         fe.InstanceExpression = new ProxyInstance ();
959                         return fe.DoResolve (this);
960                 }
961
962                 public Expression RemapParameterLValue (int idx, Expression right_side)
963                 {
964                         FieldExpr fe = new FieldExprNoAddress (CurrentIterator.parameter_fields [idx].FieldBuilder, loc);
965                         fe.InstanceExpression = new ProxyInstance ();
966                         return fe.DoResolveLValue (this, right_side);
967                 }
968                 
969                 //
970                 // Emits the proper object to address fields on a remapped
971                 // variable/parameter to field in anonymous-method/iterator proxy classes.
972                 //
973                 public void EmitThis ()
974                 {
975                         ig.Emit (OpCodes.Ldarg_0);
976                         if (InIterator){
977                                 if (!IsStatic){
978                                         FieldBuilder this_field = CurrentIterator.this_field.FieldBuilder;
979                                         if (TypeManager.IsValueType (this_field.FieldType))
980                                                 ig.Emit (OpCodes.Ldflda, this_field);
981                                         else
982                                                 ig.Emit (OpCodes.Ldfld, this_field);
983                                 } 
984                         } else if (capture_context != null && CurrentAnonymousMethod != null){
985                                 ScopeInfo si = CurrentAnonymousMethod.Scope;
986                                 while (si != null){
987                                         if (si.ParentLink != null)
988                                                 ig.Emit (OpCodes.Ldfld, si.ParentLink);
989                                         if (si.THIS != null){
990                                                 ig.Emit (OpCodes.Ldfld, si.THIS);
991                                                 break;
992                                         }
993                                         si = si.ParentScope;
994                                 }
995                         } 
996                 }
997
998                 //
999                 // Emits the code necessary to load the instance required
1000                 // to access the captured LocalInfo
1001                 //
1002                 public void EmitCapturedVariableInstance (LocalInfo li)
1003                 {
1004                         if (RemapToProxy){
1005                                 ig.Emit (OpCodes.Ldarg_0);
1006                                 return;
1007                         }
1008                         
1009                         if (capture_context == null)
1010                                 throw new Exception ("Calling EmitCapturedContext when there is no capture_context");
1011                         
1012                         capture_context.EmitCapturedVariableInstance (this, li, CurrentAnonymousMethod);
1013                 }
1014
1015                 public void EmitParameter (string name)
1016                 {
1017                         capture_context.EmitParameter (this, name);
1018                 }
1019
1020                 public void EmitAssignParameter (string name, Expression source, bool leave_copy, bool prepare_for_load)
1021                 {
1022                         capture_context.EmitAssignParameter (this, name, source, leave_copy, prepare_for_load);
1023                 }
1024
1025                 public void EmitAddressOfParameter (string name)
1026                 {
1027                         capture_context.EmitAddressOfParameter (this, name);
1028                 }
1029                 
1030                 public Expression GetThis (Location loc)
1031                 {
1032                         This my_this;
1033                         if (CurrentBlock != null)
1034                                 my_this = new This (CurrentBlock, loc);
1035                         else
1036                                 my_this = new This (loc);
1037
1038                         if (!my_this.ResolveBase (this))
1039                                 my_this = null;
1040
1041                         return my_this;
1042                 }
1043         }
1044
1045
1046         public abstract class CommonAssemblyModulClass: Attributable {
1047                 protected CommonAssemblyModulClass ():
1048                         base (null)
1049                 {
1050                 }
1051
1052                 public void AddAttributes (ArrayList attrs)
1053                 {
1054                         if (OptAttributes == null) {
1055                                 OptAttributes = new Attributes (attrs);
1056                                 return;
1057                         }
1058                         OptAttributes.AddAttributes (attrs);
1059                 }
1060
1061                 public virtual void Emit (TypeContainer tc) 
1062                 {
1063                         if (OptAttributes == null)
1064                                 return;
1065
1066                         EmitContext ec = new EmitContext (tc, Mono.CSharp.Location.Null, null, null, 0, false);
1067                         OptAttributes.Emit (ec, this);
1068                 }
1069                 
1070                 protected Attribute ResolveAttribute (Type a_type)
1071                 {
1072                         if (OptAttributes == null)
1073                                 return null;
1074
1075                         // Ensure that we only have GlobalAttributes, since the Search below isn't safe with other types.
1076                         if (!OptAttributes.CheckTargets (this))
1077                                 return null;
1078
1079                         EmitContext temp_ec = new EmitContext (RootContext.Tree.Types, Mono.CSharp.Location.Null, null, null, 0, false);
1080                         Attribute a = OptAttributes.Search (a_type, temp_ec);
1081                         if (a != null) {
1082                                 a.Resolve (temp_ec);
1083                         }
1084                         return a;
1085                 }
1086         }
1087                 
1088         public class AssemblyClass: CommonAssemblyModulClass {
1089                 // TODO: make it private and move all builder based methods here
1090                 public AssemblyBuilder Builder;
1091                 bool is_cls_compliant;
1092                 public Attribute ClsCompliantAttribute;
1093
1094                 ListDictionary declarative_security;
1095
1096                 static string[] attribute_targets = new string [] { "assembly" };
1097
1098                 public AssemblyClass (): base ()
1099                 {
1100                         is_cls_compliant = false;
1101                 }
1102
1103                 public bool IsClsCompliant {
1104                         get {
1105                                 return is_cls_compliant;
1106                         }
1107                 }
1108
1109                 public override AttributeTargets AttributeTargets {
1110                         get {
1111                                 return AttributeTargets.Assembly;
1112                         }
1113                 }
1114
1115                 public override bool IsClsCompliaceRequired(DeclSpace ds)
1116                 {
1117                         return is_cls_compliant;
1118                 }
1119
1120                 public void ResolveClsCompliance ()
1121                 {
1122                         ClsCompliantAttribute = ResolveAttribute (TypeManager.cls_compliant_attribute_type);
1123                         if (ClsCompliantAttribute == null)
1124                                 return;
1125
1126                         is_cls_compliant = ClsCompliantAttribute.GetClsCompliantAttributeValue (null);
1127                 }
1128
1129                 // fix bug #56621
1130                 private void SetPublicKey (AssemblyName an, byte[] strongNameBlob) 
1131                 {
1132                         try {
1133                                 // check for possible ECMA key
1134                                 if (strongNameBlob.Length == 16) {
1135                                         // will be rejected if not "the" ECMA key
1136                                         an.SetPublicKey (strongNameBlob);
1137                                 }
1138                                 else {
1139                                         // take it, with or without, a private key
1140                                         RSA rsa = CryptoConvert.FromCapiKeyBlob (strongNameBlob);
1141                                         // and make sure we only feed the public part to Sys.Ref
1142                                         byte[] publickey = CryptoConvert.ToCapiPublicKeyBlob (rsa);
1143                                         
1144                                         // AssemblyName.SetPublicKey requires an additional header
1145                                         byte[] publicKeyHeader = new byte [12] { 0x00, 0x24, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00 };
1146
1147                                         byte[] encodedPublicKey = new byte [12 + publickey.Length];
1148                                         Buffer.BlockCopy (publicKeyHeader, 0, encodedPublicKey, 0, 12);
1149                                         Buffer.BlockCopy (publickey, 0, encodedPublicKey, 12, publickey.Length);
1150                                         an.SetPublicKey (encodedPublicKey);
1151                                 }
1152                         }
1153                         catch (Exception) {
1154                                 Report.Error (1548, "Could not strongname the assembly. File `" +
1155                                         RootContext.StrongNameKeyFile + "' incorrectly encoded.");
1156                                 Environment.Exit (1);
1157                         }
1158                 }
1159
1160                 // TODO: rewrite this code (to kill N bugs and make it faster) and use standard ApplyAttribute way.
1161                 public AssemblyName GetAssemblyName (string name, string output) 
1162                 {
1163                         if (OptAttributes != null) {
1164                                 foreach (Attribute a in OptAttributes.Attrs) {
1165                                         // cannot rely on any resolve-based members before you call Resolve
1166                                         if (a.ExplicitTarget == null || a.ExplicitTarget != "assembly")
1167                                                 continue;
1168
1169                                         // TODO: This code is buggy: comparing Attribute name without resolving it is wrong.
1170                                         //       However, this is invoked by CodeGen.Init, at which time none of the namespaces
1171                                         //       are loaded yet.
1172                                         switch (a.Name) {
1173                                                 case "AssemblyKeyFile":
1174                                                 case "AssemblyKeyFileAttribute":
1175                                                 case "System.Reflection.AssemblyKeyFileAttribute":
1176                                                         if (RootContext.StrongNameKeyFile != null) {
1177                                                                 Report.SymbolRelatedToPreviousError (a.Location, a.Name);
1178                                                                 Report.Warning (1616, "Compiler option '{0}' overrides '{1}' given in source", "keyfile", "System.Reflection.AssemblyKeyFileAttribute");
1179                                                         }
1180                                                         else {
1181                                                                 string value = a.GetString ();
1182                                                                 if (value != String.Empty)
1183                                                                         RootContext.StrongNameKeyFile = value;
1184                                                         }
1185                                                         break;
1186                                                 case "AssemblyKeyName":
1187                                                 case "AssemblyKeyNameAttribute":
1188                                                 case "System.Reflection.AssemblyKeyNameAttribute":
1189                                                         if (RootContext.StrongNameKeyContainer != null) {
1190                                                                 Report.SymbolRelatedToPreviousError (a.Location, a.Name);
1191                                                                 Report.Warning (1616, "keycontainer", "Compiler option '{0}' overrides '{1}' given in source", "System.Reflection.AssemblyKeyNameAttribute");
1192                                                         }
1193                                                         else {
1194                                                                 string value = a.GetString ();
1195                                                                 if (value != String.Empty)
1196                                                                         RootContext.StrongNameKeyContainer = value;
1197                                                         }
1198                                                         break;
1199                                                 case "AssemblyDelaySign":
1200                                                 case "AssemblyDelaySignAttribute":
1201                                                 case "System.Reflection.AssemblyDelaySignAttribute":
1202                                                         RootContext.StrongNameDelaySign = a.GetBoolean ();
1203                                                         break;
1204                                         }
1205                                 }
1206                         }
1207
1208                         AssemblyName an = new AssemblyName ();
1209                         an.Name = Path.GetFileNameWithoutExtension (name);
1210
1211                         // note: delay doesn't apply when using a key container
1212                         if (RootContext.StrongNameKeyContainer != null) {
1213                                 an.KeyPair = new StrongNameKeyPair (RootContext.StrongNameKeyContainer);
1214                                 return an;
1215                         }
1216
1217                         // strongname is optional
1218                         if (RootContext.StrongNameKeyFile == null)
1219                                 return an;
1220
1221                         string AssemblyDir = Path.GetDirectoryName (output);
1222
1223                         // the StrongName key file may be relative to (a) the compiled
1224                         // file or (b) to the output assembly. See bugzilla #55320
1225                         // http://bugzilla.ximian.com/show_bug.cgi?id=55320
1226
1227                         // (a) relative to the compiled file
1228                         string filename = Path.GetFullPath (RootContext.StrongNameKeyFile);
1229                         bool exist = File.Exists (filename);
1230                         if ((!exist) && (AssemblyDir != null) && (AssemblyDir != String.Empty)) {
1231                                 // (b) relative to the outputed assembly
1232                                 filename = Path.GetFullPath (Path.Combine (AssemblyDir, RootContext.StrongNameKeyFile));
1233                                 exist = File.Exists (filename);
1234                         }
1235
1236                         if (exist) {
1237                                 using (FileStream fs = new FileStream (filename, FileMode.Open, FileAccess.Read)) {
1238                                         byte[] snkeypair = new byte [fs.Length];
1239                                         fs.Read (snkeypair, 0, snkeypair.Length);
1240
1241                                         if (RootContext.StrongNameDelaySign) {
1242                                                 // delayed signing - DO NOT include private key
1243                                                 SetPublicKey (an, snkeypair);
1244                                         }
1245                                         else {
1246                                                 // no delay so we make sure we have the private key
1247                                                 try {
1248                                                         CryptoConvert.FromCapiPrivateKeyBlob (snkeypair);
1249                                                         an.KeyPair = new StrongNameKeyPair (snkeypair);
1250                                                 }
1251                                                 catch (CryptographicException) {
1252                                                         if (snkeypair.Length == 16) {
1253                                                                 // error # is different for ECMA key
1254                                                                 Report.Error (1606, "Could not strongname the assembly. " + 
1255                                                                         "ECMA key can only be used to delay-sign assemblies");
1256                                                         }
1257                                                         else {
1258                                                                 Report.Error (1548, "Could not strongname the assembly. File `" +
1259                                                                         RootContext.StrongNameKeyFile +
1260                                                                         "' doesn't have a private key.");
1261                                                         }
1262                                                         return null;
1263                                                 }
1264                                         }
1265                                 }
1266                         }
1267                         else {
1268                                 Report.Error (1548, "Could not strongname the assembly. File `" +
1269                                         RootContext.StrongNameKeyFile + "' not found.");
1270                                 return null;
1271                         }
1272                         return an;
1273                 }
1274
1275                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder customBuilder)
1276                 {
1277                         if (a.Type.IsSubclassOf (TypeManager.security_attr_type) && a.CheckSecurityActionValidity (true)) {
1278                                 if (declarative_security == null)
1279                                         declarative_security = new ListDictionary ();
1280
1281                                 a.ExtractSecurityPermissionSet (declarative_security);
1282                                 return;
1283                         }
1284
1285                         Builder.SetCustomAttribute (customBuilder);
1286                 }
1287
1288                 public override void Emit (TypeContainer tc)
1289                 {
1290                         base.Emit (tc);
1291
1292                         if (declarative_security != null) {
1293
1294                                 MethodInfo add_permission = typeof (AssemblyBuilder).GetMethod ("AddPermissionRequests", BindingFlags.Instance | BindingFlags.NonPublic);
1295                                 object builder_instance = Builder;
1296
1297                                 try {
1298                                         // Microsoft runtime hacking
1299                                         if (add_permission == null) {
1300                                                 Type assembly_builder = typeof (AssemblyBuilder).Assembly.GetType ("System.Reflection.Emit.AssemblyBuilderData");
1301                                                 add_permission = assembly_builder.GetMethod ("AddPermissionRequests", BindingFlags.Instance | BindingFlags.NonPublic);
1302
1303                                                 FieldInfo fi = typeof (AssemblyBuilder).GetField ("m_assemblyData", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField);
1304                                                 builder_instance = fi.GetValue (Builder);
1305                                         }
1306
1307                                         object[] args = new object [] { declarative_security [SecurityAction.RequestMinimum],
1308                                                                                                   declarative_security [SecurityAction.RequestOptional],
1309                                                                                                   declarative_security [SecurityAction.RequestRefuse] };
1310                                         add_permission.Invoke (builder_instance, args);
1311                                 }
1312                                 catch {
1313                                         Report.RuntimeMissingSupport (Location.Null, "assembly permission setting");
1314                                 }
1315                         }
1316                 }
1317
1318                 public override string[] ValidAttributeTargets {
1319                         get {
1320                                 return attribute_targets;
1321                         }
1322                 }
1323         }
1324
1325         public class ModuleClass: CommonAssemblyModulClass {
1326                 // TODO: make it private and move all builder based methods here
1327                 public ModuleBuilder Builder;
1328                 bool m_module_is_unsafe;
1329
1330                 public CharSet DefaultCharSet = CharSet.Ansi;
1331                 public TypeAttributes DefaultCharSetType = TypeAttributes.AnsiClass;
1332
1333                 static string[] attribute_targets = new string [] { "module" };
1334
1335                 public ModuleClass (bool is_unsafe)
1336                 {
1337                         m_module_is_unsafe = is_unsafe;
1338                 }
1339
1340                 public override AttributeTargets AttributeTargets {
1341                         get {
1342                                 return AttributeTargets.Module;
1343                         }
1344                 }
1345
1346                 public override bool IsClsCompliaceRequired(DeclSpace ds)
1347                 {
1348                         return CodeGen.Assembly.IsClsCompliant;
1349                 }
1350
1351                 public override void Emit (TypeContainer tc) 
1352                 {
1353                         base.Emit (tc);
1354
1355                         if (!m_module_is_unsafe)
1356                                 return;
1357
1358                         if (TypeManager.unverifiable_code_ctor == null) {
1359                                 Console.WriteLine ("Internal error ! Cannot set unverifiable code attribute.");
1360                                 return;
1361                         }
1362                                 
1363                         Builder.SetCustomAttribute (new CustomAttributeBuilder (TypeManager.unverifiable_code_ctor, new object [0]));
1364                 }
1365                 
1366                 public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder customBuilder)
1367                 {
1368                         if (a.Type == TypeManager.cls_compliant_attribute_type) {
1369                                 if (CodeGen.Assembly.ClsCompliantAttribute == null) {
1370                                         Report.Warning (3012, a.Location, "You must specify the CLSCompliant attribute on the assembly, not the module, to enable CLS compliance checking");
1371                                 }
1372                                 else if (CodeGen.Assembly.IsClsCompliant != a.GetBoolean ()) {
1373                                         Report.SymbolRelatedToPreviousError (CodeGen.Assembly.ClsCompliantAttribute.Location, CodeGen.Assembly.ClsCompliantAttribute.Name);
1374                                         Report.Error (3017, a.Location, "You cannot specify the CLSCompliant attribute on a module that differs from the CLSCompliant attribute on the assembly");
1375                                         return;
1376                                 }
1377                         }
1378
1379                         Builder.SetCustomAttribute (customBuilder);
1380                 }
1381
1382                 /// <summary>
1383                 /// It is called very early therefore can resolve only predefined attributes
1384                 /// </summary>
1385                 public void ResolveAttributes ()
1386                 {
1387 #if NET_2_0
1388                         Attribute a = ResolveAttribute (TypeManager.default_charset_type);
1389                         if (a != null) {
1390                                 DefaultCharSet = a.GetCharSetValue ();
1391                                 switch (DefaultCharSet) {
1392                                         case CharSet.Ansi:
1393                                         case CharSet.None:
1394                                                 break;
1395                                         case CharSet.Auto:
1396                                                 DefaultCharSetType = TypeAttributes.AutoClass;
1397                                                 break;
1398                                         case CharSet.Unicode:
1399                                                 DefaultCharSetType = TypeAttributes.UnicodeClass;
1400                                                 break;
1401                                         default:
1402                                                 Report.Error (1724, a.Location, "Value specified for the argument to 'System.Runtime.InteropServices.DefaultCharSetAttribute' is not valid");
1403                                                 break;
1404                                 }
1405                         }
1406 #endif
1407                 }
1408
1409                 public override string[] ValidAttributeTargets {
1410                         get {
1411                                 return attribute_targets;
1412                         }
1413                 }
1414         }
1415 }