* ILTokenizer.cs: Handle comments first, handle whitespace in hex
[mono.git] / mcs / mcs / const.cs
index 47cef70275950d7f5c2897ce2b9c11f382dbc94b..b3fb6ae24ddf38a2c80b118deebdb3a657e31af1 100755 (executable)
@@ -28,6 +28,7 @@ namespace Mono.CSharp {
                public Expression Expr;
                EmitContext const_ec;
 
+               bool resolved = false;
                object ConstantValue = null;
                Type type;
 
@@ -117,6 +118,9 @@ namespace Mono.CSharp {
                //
                public static Constant ChangeType (Location loc, Constant expr, Type type)
                {
+                       if (type == TypeManager.object_type)
+                               return expr;
+
                        bool fail;
 
                        // from the null type to any reference-type.
@@ -177,16 +181,19 @@ namespace Mono.CSharp {
                ///  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 ()
+               public bool LookupConstantValue (out object value)
                {
-                       if (ConstantValue != null)
-                               return ConstantValue;
+                       if (resolved) {
+                               value = ConstantValue;
+                               return true;
+                       }
 
                        if (in_transit) {
                                Report.Error (110, Location,
                                              "The evaluation of the constant value for `" +
                                              Name + "' involves a circular definition.");
-                               return null;
+                               value = null;
+                               return false;
                        }
 
                        in_transit = true;
@@ -195,9 +202,11 @@ namespace Mono.CSharp {
                        //
                        // We might have cleared Expr ourselves in a recursive definition
                        //
-                       if (Expr == null)
-                               return null;
-                       
+                       if (Expr == null){
+                               value = null;
+                               return false;
+                       }
+
                        Expr = Expr.Resolve (const_ec);
 
                        in_transit = false;
@@ -205,37 +214,49 @@ namespace Mono.CSharp {
                        if (Expr == null) {
                                if (errors == Report.Errors)
                                        Report.Error (150, Location, "A constant value is expected");
-                               return null;
+                               value = null;
+                               return false;
                        }
 
+                       Expression real_expr = Expr;
+
                        Constant ce = Expr as Constant;
                        if (ce == null){
                                UnCheckedExpr un_expr = Expr as UnCheckedExpr;
                                CheckedExpr ch_expr = Expr as CheckedExpr;
+                               EmptyCast ec_expr = Expr as EmptyCast;
 
                                if ((un_expr != null) && (un_expr.Expr is Constant))
                                        Expr = un_expr.Expr;
                                else if ((ch_expr != null) && (ch_expr.Expr is Constant))
                                        Expr = ch_expr.Expr;
+                               else if ((ec_expr != null) && (ec_expr.Child is Constant))
+                                       Expr = ec_expr.Child;
                                else if (Expr is ArrayCreation) {
                                        ArrayCreation ac = (ArrayCreation) Expr;
 
                                        Expr = ac.TurnIntoConstant ();
                                        if (Expr == null){
                                                Report.Error (150, Location, "A constant value is expected");
-                                               return null;
+                                               value = null;
+                                               return false;
                                        }
                                } else {
                                        if (errors == Report.Errors)
                                                Report.Error (150, Location, "A constant value is expected");
-                                       return null;
+                                       value = null;
+                                       return false;
                                }
+
+                               ce = Expr as Constant;
                        }
 
-                       if (type != ce.Type) {
+                       if (type != real_expr.Type) {
                                ce = ChangeType (Location, ce, type);
-                               if (ce == null)
-                                       return null;
+                               if (ce == null){
+                                       value = null;
+                                       return false;
+                               }
                                Expr = ce;
                        }
                        ConstantValue = ce.GetValue ();
@@ -258,9 +279,11 @@ namespace Mono.CSharp {
                        FieldBuilder.SetConstant (ConstantValue);
 
                        if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
-                               return null;
+                               throw new Exception ("Cannot register const value");
 
-                       return ConstantValue;
+                       value = ConstantValue;
+                       resolved = true;
+                       return true;
                }
                
                
@@ -269,7 +292,13 @@ namespace Mono.CSharp {
                /// </summary>
                public override void Emit (TypeContainer parent)
                {
-                       LookupConstantValue ();
+                       object value;
+                       LookupConstantValue (out value);
+
+                       if (OptAttributes != null) {
+                               OptAttributes.Emit (const_ec, this);
+                       }
+
                        base.Emit (parent);
                }
        }