2005-01-26 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / mbas / const.cs
index aba1d6b531b6a2893bbee1bdb9fcb1aed42d0079..09282a89d9dc61b2bae309b39a44477773a03145 100644 (file)
@@ -17,7 +17,8 @@
 //     function aborts because it requires its argument to be of the same type
 //
 
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
+
 
        using System;
        using System.Reflection;
@@ -25,30 +26,36 @@ namespace Mono.CSharp {
        using System.Collections;
 
        public class Const : MemberCore {
-               public readonly string ConstantType;
+               public Expression ConstantType;
                public Expression Expr;
-               public Attributes  OptAttributes;
                public FieldBuilder FieldBuilder;
 
                object ConstantValue = null;
                Type type;
 
+               bool in_transit = false;
+
                public const int AllowedModifiers =
-                       Modifiers.NEW |
                        Modifiers.PUBLIC |
                        Modifiers.PROTECTED |
                        Modifiers.INTERNAL |
-                       Modifiers.PRIVATE;
+                       Modifiers.PRIVATE |
+                       Modifiers.SHADOWS;
 
-               public Const (string constant_type, string name, Expression expr, int mod_flags,
+               public Const (Expression constant_type, string name, Expression expr, int mod_flags,
                              Attributes attrs, Location loc)
-                       : base (name, loc)
+                       : base (name, attrs, loc)
                {
                        ConstantType = constant_type;
                        Name = name;
                        Expr = expr;
                        ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE, loc);
-                       OptAttributes = attrs;
+               }
+
+               public override AttributeTargets AttributeTargets {
+                       get {
+                               return AttributeTargets.Field;
+                       }
                }
 
                public FieldAttributes FieldAttr {
@@ -75,7 +82,7 @@ namespace Mono.CSharp {
                /// </summary>
                public override bool Define (TypeContainer parent)
                {
-                       type = RootContext.LookupType (parent, ConstantType, true, Location);
+                       type = parent.ResolveType (ConstantType, false, Location);
 
                        if (type == null)
                                return false;
@@ -83,24 +90,75 @@ namespace Mono.CSharp {
                        if (!TypeManager.IsBuiltinType (type) &&
                            (!type.IsSubclassOf (TypeManager.enum_type))) {
                                Report.Error (
-                                       -3, Location,
+                                       30424, Location,
                                        "Constant type is not valid (only system types are allowed)");
                                return false;
                        }
 
+                       // if no type is declared expicitely 
+                       // set the expression type as the type of constant
+                       if (type == TypeManager.object_type){
+                               if (Expr is IntLiteral)
+                                       type = TypeManager.int32_type;
+                               else if (Expr is UIntLiteral)
+                                       type = TypeManager.uint32_type;
+                               else if (Expr is LongLiteral)
+                                       type = TypeManager.int64_type;
+                               else if (Expr is ULongLiteral)
+                                       type = TypeManager.uint64_type;
+                               else if (Expr is FloatLiteral)
+                                       type = TypeManager.float_type;
+                               else if (Expr is DoubleLiteral)
+                                       type = TypeManager.double_type;
+                               else if (Expr is StringLiteral)
+                                       type = TypeManager.string_type;
+                               else if (Expr is ShortLiteral)
+                                       type = TypeManager.short_type;
+                               else if (Expr is UShortConstant)
+                                       type = TypeManager.ushort_type;
+                               else if (Expr is SByteConstant)
+                                       type = TypeManager.sbyte_type;
+                               else if (Expr is ByteConstant)
+                                       type = TypeManager.byte_type;
+                               else if (Expr is CharConstant)
+                                       type = TypeManager.char_type;
+                               else if (Expr is BoolConstant)
+                                       type = TypeManager.bool_type;
+                               else if (Expr is DateConstant)
+                                       type = TypeManager.date_type;
+                       }
                        Type ptype = parent.TypeBuilder.BaseType;
 
                        if (ptype != null) {
-                               MemberInfo [] mi = TypeContainer.FindMembers (
+                               MemberList list = TypeContainer.FindMembers (
                                        ptype, MemberTypes.Field, BindingFlags.Public,
                                        Type.FilterName, Name);
                                
-                               if (mi == null || mi.Length == 0)
-                                       if ((ModFlags & Modifiers.NEW) != 0)
-                                               WarningNotHiding (parent);
+                               if ((list.Count > 0) && ((ModFlags & Modifiers.SHADOWS) == 0))
+                                       Report.Warning (
+                                               40004, 2, Location, 
+                                               "Const '" + Name + "' should be declared " +
+                                               "Shadows since the base type '" + ptype.Name + 
+                                               "' has a Const with same name");
+                               if (list.Count == 0) {
+                                       // if a member of module is not inherited from Object class
+                                       // can not be declared protected
+                                       if ((parent is Module) && ((ModFlags & Modifiers.PROTECTED) != 0))
+                                               Report.Error (30593, Location,
+                                                       "'Const' inside a 'Module' can not be " +
+                                                       "declared as 'Protected'");
 
-                       } else if ((ModFlags & Modifiers.NEW) != 0)
-                               WarningNotHiding (parent);
+                                       /*if ((ModFlags & Modifiers.NEW) != 0)
+                                               WarningNotHiding (parent);*/
+                               }
+                       } 
+                       /*else if ((ModFlags & Modifiers.NEW) != 0)
+                               WarningNotHiding (parent);*/
+
+                       if ((parent is Struct) && ((ModFlags & Modifiers.PROTECTED) != 0))
+                               Report.Error (30435, Location,
+                                       "'Const' inside a 'Structure' can not be " +
+                                       "declared as 'Protected'");
 
                        FieldBuilder = parent.TypeBuilder.DefineField (Name, type, FieldAttr);
 
@@ -118,16 +176,39 @@ namespace Mono.CSharp {
                        if (ConstantValue != null)
                                return ConstantValue;
 
+                       if (in_transit) {
+                               Report.Error (110, Location,
+                                             "The evaluation of the constant value for `" +
+                                             Name + "' involves a circular definition.");
+                               return null;
+                       }
+
+                       in_transit = true;
+                       int errors = Report.Errors;
+
                        Expr = Expr.Resolve (ec);
 
+                       in_transit = false;
+
                        if (Expr == null) {
-                               Report.Error (150, Location, "A constant value is expected");
+                               if (errors == Report.Errors)
+                                       Report.Error (30059, Location, "A constant value is expected");
                                return null;
                        }
 
                        if (!(Expr is Constant)) {
-                               Report.Error (150, Location, "A constant value is expected");
-                               return null;
+                               UnCheckedExpr un_expr = Expr as UnCheckedExpr;
+                               CheckedExpr ch_expr = Expr as CheckedExpr;
+
+                               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 
+                               {
+                                       Report.Error (30059, Location, "A constant value is expected");
+                                       return null;
+                               }
                        }
 
                        ConstantValue = ((Constant) Expr).GetValue ();
@@ -187,7 +268,7 @@ namespace Mono.CSharp {
 
                        if (!TypeManager.RegisterFieldValue (FieldBuilder, ConstantValue))
                                return null;
-
+                       
                        return ConstantValue;
                }
                
@@ -202,7 +283,11 @@ namespace Mono.CSharp {
                        
                        return;
                }
+               
+               public override void ApplyAttributeBuilder (Attribute a, CustomAttributeBuilder cb)
+               {
+                       FieldBuilder.SetCustomAttribute (cb);
+               }
+               
        }
 }
-
-