18437d9bdb37bbb568c9f31881548a1caff455ea
[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, Location l)
35                         : base (name, l)
36                 {
37                         this.type = type;
38                         this.name = name;
39                         this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PUBLIC);
40                         OptAttributes = attrs;
41                         ordered_enums = new ArrayList ();
42                 }
43
44                 // <summary>
45                 //   Adds @name to the enumeration space, with @expr
46                 //   being its definition.  
47                 // </summary>
48                 public AdditionResult AddEnumMember (string name, Expression expr)
49                 {
50                         if (defined_names.Contains (name))
51                                 return AdditionResult.NameExists;
52
53                         DefineName (name, expr);
54
55                         ordered_enums.Add (name);
56                         return AdditionResult.Success;
57                 }
58
59                 public void Define (TypeContainer parent)
60                 {
61                         TypeAttributes attr = Modifiers.TypeAttr (ModFlags, parent);
62
63                         Type t = parent.LookupType (type, false);
64
65                         EnumBuilder = parent.RootContext.CodeGen.ModuleBuilder.DefineEnum (name, attr, t);
66                 }
67
68                 public string Type {
69                         get {
70                                 return type;
71                         }
72                 }
73
74                 public ArrayList ValueNames {
75                         get {
76                                 return ordered_enums;
77                         }
78                 }
79
80                 public int ModFlags {
81                         get {
82                                 return mod_flags;
83                         }
84                 }
85                 
86                 // indexer
87                 public Expression this [string name] {
88                         get {
89                                 return (Expression) defined_names [name];
90                         }
91                 }
92         }
93 }