2001-09-12 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / mcs / class.cs
index c40c359a2e88618c1ab61cc53f4155628d9fe53e..f05f3c0a3235859642fd452eaf38bc6387246957 100755 (executable)
@@ -90,7 +90,10 @@ namespace CIR {
                //
                // This behaves like a property ;-)
                //
-               readonly public RootContext RootContext;
+               public readonly RootContext RootContext;
+
+               // Attributes for this type
+               protected Attributes attributes;
 
                public TypeContainer (RootContext rc, TypeContainer parent, string name) : base (name)
                {
@@ -421,6 +424,30 @@ namespace CIR {
                        }
                }
 
+               public ArrayList Indexers {
+                       get {
+                               return indexers;
+                       }
+               }
+
+               public ArrayList Operators {
+                       get {
+                               return operators;
+                       }
+               }
+
+               public ArrayList Delegates {
+                       get {
+                               return delegates;
+                       }
+               }
+               
+               public Attributes OptAttributes {
+                       get {
+                               return attributes;
+                       }
+               }
+               
                public Namespace Namespace {
                        get {
                                return my_namespace;
@@ -603,6 +630,23 @@ namespace CIR {
                                foreach (Event e in Events)
                                        e.Define (this);
                        }
+
+                       if (Indexers != null) {
+                               foreach (Indexer i in Indexers)
+                                       i.Define (this);
+                       }
+
+                       if (Operators != null) {
+                               foreach (Operator o in Operators)
+                                       o.Define (this);
+                       }
+
+                       if (Delegates != null) {
+                               foreach (Delegate d in Delegates)
+                                       d.Define (this);
+                       }
+                       
+                       
                }
 
                //
@@ -648,6 +692,62 @@ namespace CIR {
                {
                        return RootContext.LookupType (this, name, silent);
                }
+
+               // <summary>
+               // This method returns the members of this type just like Type.FindMembers would
+               // Only, we need to use this for types which are _being_ defined because MS' brain
+               // dead implementation can't take care of that ;-)
+               // </summary>
+               public MemberInfo [] FindMembers (MemberTypes mt, BindingFlags bf, MemberFilter filter, object criteria)
+               {
+                       // FIXME : Need to actually take care of all the various
+                       // arguments being passed in but for now, we only bother with
+                       // the MemberTypes and criteria arguments.
+
+                       switch (mt) {
+
+                       case MemberTypes.All:
+
+                               break;
+
+                       case MemberTypes.Constructor:
+
+                               break;
+                               
+                       case MemberTypes.Custom:
+
+                               break;
+
+                       case MemberTypes.Event:
+
+                               break;
+
+                       case MemberTypes.Field:
+
+                               break;
+
+                       case MemberTypes.Method:
+
+                               break;
+                               
+                       case MemberTypes.NestedType:
+
+                               break;
+                               
+                       case MemberTypes.Property:
+
+                               break;
+                               
+                       case MemberTypes.TypeInfo:
+
+                               break;
+
+                       }
+
+                       return null;
+                       
+               }
+               
        }
 
        public class Class : TypeContainer {
@@ -663,7 +763,7 @@ namespace CIR {
                        Modifiers.ABSTRACT |
                        Modifiers.SEALED;
 
-               public Class (RootContext rc, TypeContainer parent, string name, int mod)
+               public Class (RootContext rc, TypeContainer parent, string name, int mod, Attributes attrs)
                        : base (rc, parent, name)
                {
                        int accmods;
@@ -674,6 +774,7 @@ namespace CIR {
                                accmods = Modifiers.PRIVATE;
                        
                        this.mod_flags = Modifiers.Check (AllowedModifiers, mod, accmods);
+                       this.attributes = attrs;
                }
 
                //
@@ -698,7 +799,7 @@ namespace CIR {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Struct (RootContext rc, TypeContainer parent, string name, int mod)
+               public Struct (RootContext rc, TypeContainer parent, string name, int mod, Attributes attrs)
                        : base (rc, parent, name)
                {
                        int accmods;
@@ -711,6 +812,8 @@ namespace CIR {
                        this.mod_flags = Modifiers.Check (AllowedModifiers, mod, accmods);
 
                        this.mod_flags |= Modifiers.SEALED;
+                       this.attributes = attrs;
+                       
                }
 
                //
@@ -734,16 +837,18 @@ namespace CIR {
                public readonly string     Name;
                public readonly int        ModFlags;
                public MethodBuilder MethodBuilder;
+               public readonly Attributes OptAttributes;
                
                Block block;
                
                // return_type can be "null" for VOID values.
-               public Method (string return_type, int mod, string name, Parameters parameters)
+               public Method (string return_type, int mod, string name, Parameters parameters, Attributes attrs)
                {
                        Name = name;
                        ReturnType = return_type;
                        Parameters = parameters;
                        ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE);
+                       OptAttributes = attrs;
                }
 
                // <summary>
@@ -830,6 +935,17 @@ namespace CIR {
                                GetCallingConvention (parent is Class),
                                ret_type, parameters);
 
+                       //
+                       // This is used to track the Entry Point,
+                       //
+                       // FIXME: Allow pluggable entry point, check arguments, etc.
+                       //
+                       if (Name == "Main"){
+                               if ((ModFlags & Modifiers.STATIC) != 0){
+                                       parent.RootContext.EntryPoint = MethodBuilder;
+                               }
+                       }
+                       
                        //
                        // Define each type attribute (in/out/ref) and
                        // the argument names.
@@ -864,6 +980,7 @@ namespace CIR {
                public readonly Object Initializer;
                public readonly string Name;
                public readonly int    ModFlags;
+               public readonly Attributes OptAttributes;
                
                // <summary>
                //   Modifiers allowed in a class declaration
@@ -877,12 +994,13 @@ namespace CIR {
                        Modifiers.STATIC |
                        Modifiers.READONLY;
 
-               public Field (string type, int mod, string name, Object expr_or_array_init)
+               public Field (string type, int mod, string name, Object expr_or_array_init, Attributes attrs)
                {
                        Type = type;
                        ModFlags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PRIVATE);
                        Name = name;
                        Initializer = expr_or_array_init;
+                       OptAttributes = attrs;
                }
        }
 
@@ -1036,6 +1154,7 @@ namespace CIR {
                public readonly int    ModFlags;
                public Block           Get, Set;
                public PropertyBuilder PropertyBuilder;
+               public Attributes OptAttributes;
                
                const int AllowedModifiers =
                        Modifiers.NEW |
@@ -1049,13 +1168,14 @@ namespace CIR {
                        Modifiers.ABSTRACT |
                        Modifiers.VIRTUAL;
                
-               public Property (string type, string name, int mod_flags, Block get_block, Block set_block)
+               public Property (string type, string name, int mod_flags, Block get_block, Block set_block, Attributes attrs)
                {
                        Type = type;
                        Name = name;
                        ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE);
                        Get = get_block;
                        Set = set_block;
+                       OptAttributes = attrs;
                }
 
                public void Define (TypeContainer parent)
@@ -1063,19 +1183,13 @@ namespace CIR {
 
                        MethodAttributes method_attr = Modifiers.MethodAttr(ModFlags);
                                        
-                       // FIXME - Right now using Modifiers.MethodAttributes
-                       // Guess we need to add a special method for ProprtyAttributes
-                       // in modifiers.cs - when we get final comments from miguel
-                       // abt why access modifiers like new internal etc are not handled
-                       // in modifiers.cs
-
                        // FIXME - how to handle PropertyAttributes.HasDefault
 
                        PropertyAttributes prop_attr = PropertyAttributes.RTSpecialName |
                                                       PropertyAttributes.SpecialName;
                
                
-                       Type tp = System.Type.GetType(Type);
+                       Type tp = parent.LookupType (Type, false);
                        Type [] prop_type = new Type [1];
                        prop_type [0] = tp;
 
@@ -1121,8 +1235,10 @@ namespace CIR {
                public readonly Block     Add;
                public readonly Block     Remove;
                public EventBuilder       EventBuilder;
-
-               public Event (string type, string name, Object init, int flags, Block add_block, Block rem_block)
+               public Attributes         OptAttributes;
+               
+               public Event (string type, string name, Object init, int flags, Block add_block, Block rem_block,
+                             Attributes attrs)
                {
                        Type = type;
                        Name = name;
@@ -1130,6 +1246,7 @@ namespace CIR {
                        ModFlags = Modifiers.Check (AllowedModifiers, flags, Modifiers.PRIVATE);  
                        Add = add_block;
                        Remove = rem_block;
+                       OptAttributes = attrs;
                }
 
                public void Define (TypeContainer parent)
@@ -1140,7 +1257,7 @@ namespace CIR {
                        
                        MethodBuilder mb;
 
-                       Type t = System.Type.GetType (Type);
+                       Type t = parent.LookupType (Type, false);
                        Type [] p_type = new Type [1];
                        p_type [0] = t;
                        
@@ -1180,9 +1297,13 @@ namespace CIR {
                public readonly int        ModFlags;
                public readonly Block      Get;
                public readonly Block      Set;
+               public Attributes          OptAttributes;
+               public MethodBuilder GetMethodBuilder;
+               public MethodBuilder SetMethodBuilder;
+               
 
                public Indexer (string type, string int_type, int flags, Parameters parms,
-                               Block get_block, Block set_block)
+                               Block get_block, Block set_block, Attributes attrs)
                {
 
                        Type = type;
@@ -1191,7 +1312,35 @@ namespace CIR {
                        FormalParameters = parms;
                        Get = get_block;
                        Set = set_block;
+                       OptAttributes = attrs;
+               }
+
+               public void Define (TypeContainer parent)
+               {
+                       MethodAttributes attr = Modifiers.MethodAttr (ModFlags);
+                       
+                       Type ret_type = parent.LookupType (Type, false);
+                       Type [] param_types = FormalParameters.GetParameterInfo (parent);
+
+                       GetMethodBuilder = parent.TypeBuilder.DefineMethod ("get_Item", attr, ret_type, param_types);
+                       SetMethodBuilder = parent.TypeBuilder.DefineMethod ("set_Item", attr, ret_type, param_types);
+                       
+                       Parameter [] p = FormalParameters.FixedParameters;
+
+                       if (p != null) {
+                               int i;
+                               
+                               for (i = 0; i < p.Length; ++i) {
+                                       GetMethodBuilder.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
+                                       SetMethodBuilder.DefineParameter (i + 1, p [i].Attributes, p [i].Name);
+                               }
+                               
+                               if (i != param_types.Length)
+                                       Console.WriteLine ("Implement type definition for params");
+                       }
+
                }
+               
        }
 
        public class Operator {
@@ -1243,9 +1392,11 @@ namespace CIR {
                public readonly string SecondArgName;
                public readonly int    ModFlags;
                public readonly Block  Block;
+               public Attributes      OptAttributes;
+               public MethodBuilder   OperatorMethodBuilder;
 
                public Operator (OpType type, string ret_type, int flags, string arg1type, string arg1name,
-                                string arg2type, string arg2name, Block block)
+                                string arg2type, string arg2name, Block block, Attributes attrs)
                {
                        OperatorType = type;
                        ReturnType = ret_type;
@@ -1255,45 +1406,32 @@ namespace CIR {
                        SecondArgType = arg2type;
                        SecondArgName = arg2name;
                        Block = block;
+                       OptAttributes = attrs;
                }
-                             
-               public static void CheckUnaryOperator (OpType op)
-               {
-                       switch (op) {
 
-                       case OpType.Bang : case OpType.Tilde : case OpType.Increment : case OpType.Decrement :
-                       case OpType.True : case OpType.False : case OpType.Plus : case OpType.Minus :
+               public void Define (TypeContainer parent)
+               {
+                       MethodAttributes attr = Modifiers.MethodAttr (ModFlags);
 
-                               break;
-                               
-                       default :
-                               // We have an error
-                               
-                               CSharpParser.error (1019, "Overloadable unary operator expected"); 
-                               break;
-                               
-                       }
-               }
+                       string name = "Operator" + OperatorType;
 
-               public static void CheckBinaryOperator (OpType op)
-               {
-                       switch (op) {
+                       Type ret_type = parent.LookupType (ReturnType, false);
 
-                       case OpType.Plus : case OpType.Minus : case OpType.Star : case OpType.Div :
-                       case OpType.Percent : case OpType.BitAnd : case OpType.BitOr : case OpType.Carret :
-                       case OpType.ShiftLeft : case OpType.ShiftRight : case OpType.Eq : case OpType.NotEq :
-                       case OpType.GreaterThan : case OpType.LesserThan : case OpType.GreaterOrEq :
-                       case OpType.LesserOrEq :
+                       Type [] param_types = new Type [2];
 
-                               break;
+                       param_types [0] = parent.LookupType (FirstArgType, false);
+                       if (SecondArgType != null)
+                               param_types [1] = parent.LookupType (SecondArgType, false);
+                       
+                       OperatorMethodBuilder = parent.TypeBuilder.DefineMethod (name, attr, ret_type, param_types);
 
-                       default :
+                       OperatorMethodBuilder.DefineParameter (1, ParameterAttributes.None, FirstArgName);
 
-                               CSharpParser.error (1020, "Overloadable binary operator expected");
-                               break;
-                       }
+                       if (SecondArgType != null)
+                               OperatorMethodBuilder.DefineParameter (2, ParameterAttributes.None, SecondArgName);
 
                }
+               
 
        }