a6b8dfd41393975de9270477eb4cf9d0817a7451
[mono.git] / mcs / mcs / enum.cs
1 //
2 // enum.cs: Enum handling.
3 //
4 // Author: Miguel de Icaza (miguel@gnu.org)
5 //
6 // Licensed under the terms of the GNU GPL
7 //
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
9 //
10
11 using System;
12 using System.Collections;
13 using System.Reflection;
14 using System.Reflection.Emit;
15
16 namespace CIR {
17
18         public class Enum : DeclSpace {
19
20                 ArrayList ordered_enums;
21                 string type;
22                 string name;
23                 int mod_flags;
24                 public EnumBuilder EnumBuilder;
25                 public Attributes  OptAttributes;
26                 
27                 public const int AllowedModifiers =
28                         Modifiers.NEW |
29                         Modifiers.PUBLIC |
30                         Modifiers.PROTECTED |
31                         Modifiers.INTERNAL |
32                         Modifiers.PRIVATE;
33
34                 public Enum (string type, int mod_flags, string name, Attributes attrs) : base (name)
35                 {
36                         this.type = type;
37                         this.name = name;
38                         this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC);
39                         OptAttributes = attrs;
40                         ordered_enums = new ArrayList ();
41                 }
42
43                 // <summary>
44                 //   Adds @name to the enumeration space, with @expr
45                 //   being its definition.  
46                 // </summary>
47                 public AdditionResult AddEnumMember (string name, Expression expr)
48                 {
49                         if (defined_names.Contains (name))
50                                 return AdditionResult.NameExists;
51
52                         DefineName (name, expr);
53
54                         ordered_enums.Add (name);
55                         return AdditionResult.Success;
56                 }
57
58                 public void Define (TypeContainer parent)
59                 {
60                         TypeAttributes attr = Modifiers.TypeAttr (ModFlags, parent);
61
62                         Type t = System.Type.GetType (type);
63
64                         EnumBuilder = parent.RootContext.CodeGen.ModuleBuilder.DefineEnum (name, attr, t);
65                 }
66
67                 public string Type {
68                         get {
69                                 return type;
70                         }
71                 }
72
73                 public ArrayList ValueNames {
74                         get {
75                                 return ordered_enums;
76                         }
77                 }
78
79                 public int ModFlags {
80                         get {
81                                 return mod_flags;
82                         }
83                 }
84                 
85                 // indexer
86                 public Expression this [string name] {
87                         get {
88                                 return (Expression) defined_names [name];
89                         }
90                 }
91         }
92 }