Welcome to mirror list, hosted at ThFree Co, Russian Federation.

const.cs « mbas « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aba1d6b531b6a2893bbee1bdb9fcb1aed42d0079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// const.cs: Constant declarations.
//
// Author:
//   Miguel de Icaza (miguel@ximian.com)
//
// (C) 2001 Ximian, Inc.
//
//

//
// This is needed because the following situation arises:
//
//     The FieldBuilder is declared with the real type for an enumeration
//
//     When we attempt to set the value for the constant, the FieldBuilder.SetConstant
//     function aborts because it requires its argument to be of the same type
//

namespace Mono.CSharp {

	using System;
	using System.Reflection;
	using System.Reflection.Emit;
	using System.Collections;

	public class Const : MemberCore {
		public readonly string ConstantType;
		public Expression Expr;
		public Attributes  OptAttributes;
		public FieldBuilder FieldBuilder;

		object ConstantValue = null;
		Type type;

		public const int AllowedModifiers =
			Modifiers.NEW |
			Modifiers.PUBLIC |
			Modifiers.PROTECTED |
			Modifiers.INTERNAL |
			Modifiers.PRIVATE;

		public Const (string constant_type, string name, Expression expr, int mod_flags,
			      Attributes attrs, Location loc)
			: base (name, loc)
		{
			ConstantType = constant_type;
			Name = name;
			Expr = expr;
			ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE, loc);
			OptAttributes = attrs;
		}

		public FieldAttributes FieldAttr {
			get {
				return FieldAttributes.Literal | FieldAttributes.Static |
					Modifiers.FieldAttr (ModFlags) ;
			}
		}

#if DEBUG
		void dump_tree (Type t)
		{
			Console.WriteLine ("Dumping hierarchy");
			while (t != null){
				Console.WriteLine ("   " + t.FullName + " " +
					(t.GetType ().IsEnum ? "yes" : "no"));
				t = t.BaseType;
			}
		}
#endif

		/// <summary>
		///   Defines the constant in the @parent
		/// </summary>
		public override bool Define (TypeContainer parent)
		{
			type = RootContext.LookupType (parent, ConstantType, true, Location);

			if (type == null)
				return false;

			if (!TypeManager.IsBuiltinType (type) &&
			    (!type.IsSubclassOf (TypeManager.enum_type))) {
				Report.Error (
					-3, Location,
					"Constant type is not valid (only system types are allowed)");
				return false;
			}

			Type ptype = parent.TypeBuilder.BaseType;

			if (ptype != null) {
				MemberInfo [] mi = TypeContainer.FindMembers (
					ptype, MemberTypes.Field, BindingFlags.Public,
					Type.FilterName, Name);
				
				if (mi == null || mi.Length == 0)
					if ((ModFlags & Modifiers.NEW) != 0)
						WarningNotHiding (parent);

			} else if ((ModFlags & Modifiers.NEW) != 0)
				WarningNotHiding (parent);

			FieldBuilder = parent.TypeBuilder.DefineField (Name, type, FieldAttr);

			TypeManager.RegisterConstant (FieldBuilder, this);

			return true;
		}

		/// <summary>
		///  Looks up the value of a constant field. Defines it if it hasn't
		///  already been. Similar to LookupEnumValue in spirit.
		/// </summary>
		public object LookupConstantValue (EmitContext ec)
		{
			if (ConstantValue != null)
				return ConstantValue;

			Expr = Expr.Resolve (ec);

			if (Expr == null) {
				Report.Error (150, Location, "A constant value is expected");
				return null;
			}

			if (!(Expr is Constant)) {
				Report.Error (150, Location, "A constant value is expected");
				return null;
			}

			ConstantValue = ((Constant) Expr).GetValue ();

			if (type != Expr.Type) {
				try {
					ConstantValue = TypeManager.ChangeType (ConstantValue, type);
				} catch {
					Expression.Error_CannotConvertImplicit (Location, Expr.Type, type);
					return null;
				}

				if (type == TypeManager.int32_type)
					Expr = new IntConstant ((int) ConstantValue);
				else if (type == TypeManager.uint32_type)
					Expr = new UIntConstant ((uint) ConstantValue);
				else if (type == TypeManager.int64_type)
					Expr = new LongConstant ((long) ConstantValue);
				else if (type == TypeManager.uint64_type)
					Expr = new ULongConstant ((ulong) ConstantValue);
				else if (type == TypeManager.float_type)
					Expr = new FloatConstant ((float) ConstantValue);
				else if (type == TypeManager.double_type)
					Expr = new DoubleConstant ((double) ConstantValue);
				else if (type == TypeManager.string_type)
					Expr = new StringConstant ((string) ConstantValue);
				else if (type == TypeManager.short_type)
					Expr = new ShortConstant ((short) ConstantValue);
				else if (type == TypeManager.ushort_type)
					Expr = new UShortConstant ((ushort) ConstantValue);
				else if (type == TypeManager.sbyte_type)
					Expr = new SByteConstant ((sbyte) ConstantValue);
				else if (type == TypeManager.byte_type)
					Expr = new ByteConstant ((byte) ConstantValue);
				else if (type == TypeManager.char_type)
					Expr = new CharConstant ((char) ConstantValue);
				else if (type == TypeManager.bool_type)
					Expr = new BoolConstant ((bool) ConstantValue);
			}

			if (type.IsEnum){
				//
				// This sadly does not work for our user-defined enumerations types ;-(
				//
				try {
					ConstantValue = System.Enum.ToObject (
						type, ConstantValue);
				} catch (ArgumentException){
					Report.Error (
						-16, Location,
						".NET SDK 1.0 does not permit to create the constant "+
						" field from a user-defined enumeration");
				}
			}

			FieldBuilder.SetConstant (ConstantValue);

			if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
				return null;

			return ConstantValue;
		}
		
		
		/// <summary>
		///  Emits the field value by evaluating the expression
		/// </summary>
		public void EmitConstant (TypeContainer parent)
		{
			EmitContext ec = new EmitContext (parent, Location, null, type, ModFlags);
			LookupConstantValue (ec);
			
			return;
		}
	}
}