More work
authorMiguel de Icaza <miguel@gnome.org>
Wed, 15 Aug 2001 19:56:19 +0000 (19:56 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Wed, 15 Aug 2001 19:56:19 +0000 (19:56 -0000)
svn path=/trunk/mcs/; revision=477

mcs/mcs/ChangeLog
mcs/mcs/TODO
mcs/mcs/class.cs
mcs/mcs/constant.cs
mcs/mcs/cs-parser.jay
mcs/mcs/decl.cs
mcs/mcs/driver.cs
mcs/mcs/interface.cs
mcs/mcs/parameter.cs
mcs/mcs/rootcontext.cs
mcs/mcs/tree.cs

index 8d5eca37c9bc2ae1ca93f4eb27eadc09703cce62..e6cf61985642150f07ca2f595f34ff6dcae99e0a 100755 (executable)
@@ -1,3 +1,7 @@
+2001-08-13  Miguel de Icaza  <miguel@ximian.com>
+
+       * class.cs: Put back walking code for type containers.
+
 2001-08-11  Miguel de Icaza  <miguel@ximian.com>
 
        * class.cs (MakeConstant): Code to define constants.
index ebc4536ae0bc6889f80f54870efb58968a76d49b..21353232d060cf736b877df45e8cf71e181ea30b 100644 (file)
@@ -1,17 +1,29 @@
-* Namespaces.
+* Ordering
 
-       Currently the code can not resolve identifiers in other
-       namespaces.
+       First define methods and operator overloads.
 
-       namespace A {
-               class X : B {
-               }
-       }
+       Do enumerations and constants.
 
-       namespace A {
-               class B {
-               }
-       }
+       Process function bodies
+
+       Can a constant_expression invoke overloaded operators?
+       Explicit user-defined conversions?
+
+* Tokenizer
+
+       Propagate file/location to upper layers
+
+* Error handling
+
+       Normalize, and use Tokenizer location 
+
+* Enumerations
+
+       Currently I am not resolving enumerations.
+
+       Either I track them with `RecordEnum' as I do with classes,
+       structs and interfaces or I rewrite the code to visit type
+       containers and `walk' the enums with this process. 
 
 * Known problems:
 
@@ -33,3 +45,5 @@
                Array r = (string []) object
 
        Wont be parsed.
+
+  
\ No newline at end of file
index b4ec96bab50ab98c5f023c078973e9810d198606..d68f1565b0716ea5d312238c5b09ba97be92b2e0 100755 (executable)
@@ -42,11 +42,17 @@ namespace CIR {
                TypeContainer parent;
                ArrayList type_bases;
 
-               public TypeContainer (TypeContainer parent, string name) : base (name)
+               //
+               // This behaves like a property ;-)
+               //
+               readonly public RootContext RootContext;
+
+               public TypeContainer (RootContext rc, TypeContainer parent, string name) : base (name)
                {
                        types = new Hashtable ();
                        this.parent = parent;
-
+                       RootContext = rc;
+                       
                        string n;
                        if (parent == null)
                                n = "";
@@ -300,7 +306,7 @@ namespace CIR {
                                my_namespace = value;
                        }
                }
-               
+
                //
                // The Toplevel is `root_types' which is a containerfor all
                // types defined, hence the non-obviios parent.parent.
@@ -355,27 +361,45 @@ namespace CIR {
                        }
                }
 
-               void MakeConstant (RootContext rc, Constant c)
-               {
-                       FieldBuilder fb;
-
-                       fb = Definition.DefineField (c.Name,
-                                                    rc.LookupType (this, c.ConstantType),
-                                                    c.FieldAttr);
-
-                       
-               }
-               
                //
                // Populates our TypeBuilder with fields and methods
                //
                public void Populate (RootContext rc)
                {
                        if (Constants != null){
-                               foreach (DictionaryEntry cde in Constants)
-                                       MakeConstant (rc, (Constant) cde.Value);
+                               foreach (DictionaryEntry cde in Constants){
+                                       Constant c = (Constant) cde.Value;
+
+                                       c.MakeConstant (rc, this);
+                               }
+                       }
+               }
+
+               public delegate void ExamineType (TypeContainer container, object cback_data);
+
+               void WalkTypesAt (TypeContainer root, ExamineType visit, object cback_data)
+               {
+                       if (root == null)
+                               return;
+
+                       foreach (DictionaryEntry de in root.Types){
+                               TypeContainer type = (TypeContainer) de.Value;
+
+                               visit (type, cback_data);
+                               WalkTypesAt (type, visit, cback_data);
                        }
                }
+
+               public void WalkTypes (ExamineType visit, object cback)
+               {
+                       WalkTypesAt (this, visit, cback);
+               }
+
+               public Type LookupType (string name, bool silent)
+               {
+                       Console.WriteLine ("Is type container null? " + (this == null).ToString ());
+                       return RootContext.LookupType (this, name, silent);
+               }
        }
 
        public class Class : TypeContainer {
@@ -391,8 +415,8 @@ namespace CIR {
                        Modifiers.ABSTRACT |
                        Modifiers.SEALED;
 
-               public Class (TypeContainer parent, string name, int mod)
-                       : base (parent, name)
+               public Class (RootContext rc, TypeContainer parent, string name, int mod)
+                       : base (rc, parent, name)
                {
                        int accmods;
 
@@ -426,8 +450,8 @@ namespace CIR {
                        Modifiers.INTERNAL |
                        Modifiers.PRIVATE;
 
-               public Struct (TypeContainer parent, string name, int mod)
-                       : base (parent, name)
+               public Struct (RootContext rc, TypeContainer parent, string name, int mod)
+                       : base (rc, parent, name)
                {
                        int accmods;
                        
index 6979df07c9bc1b4e84de9f1a51c45a96e1bb745a..c4a3527535b88a4d2bcb70ce07d2072716df02c6 100755 (executable)
@@ -2,6 +2,7 @@ namespace CIR {
 
        using System;
        using System.Reflection;
+       using System.Reflection.Emit;
        
        public class Constant : Expression {
                string     name;
@@ -47,6 +48,44 @@ namespace CIR {
                                return FieldAttributes.Literal | Modifiers.Map (mod_flags) ;
                        }
                }
+
+               // <summary>
+               //   Defines the constant in the @parent
+               // </summary>
+               public void MakeConstant (RootContext rc, TypeContainer parent)
+               {
+                       FieldBuilder fb;
+                       TypeCode tc;
+                       Type t;
+                       
+                       t = rc.LookupType (parent, type);
+                       if (t == null)
+                               return;
+
+                       tc = System.Type.GetTypeCode (t);
+                       
+                       if ((tc == TypeCode.SByte)  || (tc == TypeCode.Byte)   ||
+                           (tc == TypeCode.Int16)  || (tc == TypeCode.UInt16) ||
+                           (tc == TypeCode.Int32)  || (tc == TypeCode.Int64)  ||
+                           (tc == TypeCode.UInt32) || (tc == TypeCode.UInt64)) {
+                               
+                       } else if ((tc == TypeCode.Double) || (tc == TypeCode.Single)) {
+
+                       } else if (tc == TypeCode.Char) {
+                       } else if (tc == TypeCode.Decimal) {
+
+                       } else if (t.IsSubclassOf (typeof (System.String))) {
+
+                       } else if (t.IsSubclassOf (typeof (System.Enum))) {
+
+                       } else {
+                               rc.Report.Error (-3, "Constant type is not valid (only system types are allowed");
+                               return;
+                       }
+
+                       fb = parent.TypeBuilder.DefineField (name, t, FieldAttr);
+                       
+               }
        }
 }
 
index 13a90ce4a062d64b84018178f26edf573810894d..0516c2c067e58e0ea9fad293103130e78540a029 100755 (executable)
@@ -70,7 +70,8 @@ namespace CIR
                // <summary>\r
                //   Used to record all types defined\r
                // </summary>\r
-               CIR.Tree tree;\r
+               Tree tree;\r
+               RootContext rc;\r
 \r
                // Name of the file we are parsing\r
                public string name;\r
@@ -482,7 +483,7 @@ struct_declaration
                Struct new_struct;\r
                string full_struct_name = MakeName ((string) $4);\r
 \r
-               new_struct = new Struct (current_container, full_struct_name, (int) $2);\r
+               new_struct = new Struct (rc, current_container, full_struct_name, (int) $2);\r
                current_container = new_struct;\r
                current_container.Namespace = current_namespace;\r
                tree.RecordStruct (full_struct_name, new_struct);\r
@@ -1923,7 +1924,7 @@ class_declaration
                Class new_class;\r
                string full_class_name = MakeName ((string) $4);\r
 \r
-               new_class = new Class (current_container, full_class_name, (int) $2);\r
+               new_class = new Class (rc, current_container, full_class_name, (int) $2);\r
                current_container = new_class;\r
                current_container.Namespace = current_namespace;\r
                tree.RecordClass (full_class_name, new_class);\r
@@ -2722,10 +2723,11 @@ void note (string s)
 \r
 Tokenizer lexer;\r
 \r
-public CSharpParser(CIR.Tree tree, string name, System.IO.Stream input) \r
+public CSharpParser(RootContext rc, string name, System.IO.Stream input) \r
 {\r
        current_namespace = new Namespace (null, "");\r
-       this.tree = tree;\r
+       this.rc   = rc;\r
+       this.tree = rc.Tree;\r
        this.name = name;\r
        this.input = input;\r
        current_container = tree.Types;\r
index 332a5f2b77cdfdd2932b318ee405ebd46225fbed..2f09a1ee3160d8f8d1c08026a9aefa2dbfe4638a 100755 (executable)
@@ -129,7 +129,7 @@ namespace CIR {
                        }
                }
 
-               public TypeBuilder Definition {
+               public TypeBuilder TypeBuilder {
                        get {
                                return definition;
                        }
index 8b9851c60f074412ace45de2b2fe1a7a0654c3dd..e06b56007fe8dc889c13e2e2d262f97bf1e71710 100755 (executable)
@@ -37,7 +37,7 @@ namespace CIR
 \r
                int error_count = 0;\r
 \r
-               public int parse (Tree context, string input_file)\r
+               public int parse (string input_file)\r
                {\r
                        CSharpParser parser;\r
                        System.IO.Stream input;\r
@@ -224,7 +224,7 @@ namespace CIR
                                        continue;\r
                                }\r
 \r
-                               errors += parse (context.Tree, arg);\r
+                               errors += parse (arg);\r
                        }\r
 \r
                        //\r
index 17df3efb16c95f80aed14ed4451acc8ff6514caf..5f2f434ece9794ae46be4b6fb8d03855933d0a08 100755 (executable)
@@ -26,6 +26,8 @@ namespace CIR {
                Hashtable defined_events;
                Hashtable defined_properties;
 
+               TypeContainer parent;
+               
                // These will happen after the semantic analysis
                
                // Hashtable defined_indexers;
@@ -49,6 +51,7 @@ namespace CIR {
                        defined_properties = new Hashtable ();
 
                        this.mod_flags = Modifiers.Check (AllowedModifiers, mod, Modifiers.PUBLIC);
+                       this.parent = parent;
                }
 
                public AdditionResult AddMethod (InterfaceMethod imethod)
@@ -141,6 +144,66 @@ namespace CIR {
                                bases = value;
                        }
                }
+
+               void Error111 (InterfaceMethod im)
+               {
+                       parent.RootContext.Report.Error (
+                               111,
+                               "Interface `" + Name + "' already contains a definition with the " +
+                               "same return value and paramenter types for method `" + im.Name + "'");
+               }
+               
+               void PopulateMethods ()
+               {
+                       foreach (InterfaceMethod im in defined_method_list){
+                               Type ReturnType = parent.LookupType (im.ReturnType, true);
+                       
+                               TypeBuilder.DefineMethod (
+                                       im.Name, MethodAttributes.Public,
+                                       ReturnType, im.ParameterTypes (parent));
+                       }
+               }
+
+               // <summary>
+               //   Performs the semantic analysis for all the interface members
+               //   that were declared
+               // </summary>
+               bool SemanticAnalysis ()
+               {
+                       Hashtable methods = new Hashtable ();
+
+                       //
+                       // First check that all methods with the same name
+                       // have a different signature.
+                       //
+                       foreach (InterfaceMethod im in defined_method_list){
+                               string sig = im.GetSignature (parent);
+
+                               //
+                               // If there was an undefined Type on the signatures
+                               // 
+                               if (sig == null)
+                                       continue;
+
+                               if (methods [sig] != null){
+                                       Error111 (im);
+                                       return false;
+                               }
+                       }
+
+                       return true;
+               }
+
+               // <summary>
+               //   Performs semantic analysis, and then generates the IL interfaces
+               // </summary>
+               public void Populate ()
+               {
+                       if (!SemanticAnalysis ())
+                               return;
+                       
+                       PopulateMethods ();
+               }
        }
 
        public class InterfaceMemberBase {
@@ -222,35 +285,37 @@ namespace CIR {
        }
        
        public class InterfaceMethod : InterfaceMemberBase {
-               string return_type;
-               bool is_new;
-               Parameters args;
+               public readonly string     ReturnType;
+               public readonly bool       IsNew;
+               public readonly Parameters Parameters;
                
                public InterfaceMethod (string return_type, string name, bool is_new, Parameters args)
                        : base (name)
                {
-                       this.return_type = return_type;
-                       this.is_new = is_new;
-                       this.args = args;
+                       this.ReturnType = return_type;
+                       this.IsNew = is_new;
+                       this.Parameters = args;
                }
 
-               public string ReturnType {
-                       get {
-                               return return_type;
-                       }
-               }
+               // <summary>
+               //   Returns the signature for this interface method
+               // </summary>
+               public string GetSignature (TypeContainer tc)
+               {
+                       Type ret = tc.LookupType (ReturnType, false);
+                       string args = Parameters.GetSignature (tc);
 
-               public bool IsNew {
-                       get {
-                               return is_new;
-                       }
+                       if ((ret == null) || (args == null))
+                               return null;
+                       
+                       return (IsNew ? "new-" : "") + ret.FullName + "(" + args + ")";
                }
 
-               public Parameters Parameters {
-                       get {
-                               return args;
-                       }
+               public Type [] ParameterTypes (TypeContainer tc)
+               {
+                       return Parameters.GetTypes (tc);
                }
+
        }
 
        public class InterfaceIndexer : InterfaceMemberBase {
index 7a656e3760e38067a0cbb5069e98f0ffc6fd5525..9730598c10597c94a5c2b6a3fbde96bd82e9cee3 100755 (executable)
@@ -18,9 +18,8 @@
 namespace CIR {
 
        using System;
-       
-       public class Parameter {
 
+       public class Parameter {
                public enum Modifier {
                        NONE,
                        REF,
@@ -28,20 +27,20 @@ namespace CIR {
                        PARAMS,
                }
 
-               string   type;
-               string   name;
-               Modifier mod;
+               public readonly string   Type;
+               public readonly string   Name;
+               public readonly Modifier ModFlags;
                
                public Parameter (string type, string name, Modifier mod)
                {
-                       this.name = name;
-                       this.mod = mod;
-                       this.type = type;
+                       Name = name;
+                       ModFlags = mod;
+                       Type = type = type;
                }
 
                string ModSignature ()
                {
-                       switch (mod){
+                       switch (ModFlags){
                        case Modifier.NONE:
                                return "";
                        case Modifier.REF:
@@ -54,29 +53,19 @@ namespace CIR {
                        // This should not happen.
                        return (string) null;
                }
-               
-               public string Signature {
-                       get {
-                               return ModSignature (); // TYPEFIX: + Type.GetTypeEncoding (typeref.Type);
-                       }
-               }
-               
-               public string Type {
-                       get {
-                               return type;
-                       }
-               }
 
-               public string Name {
-                       get {
-                               return name;
-                       }
-               }
+               // <summary>
+               //   Returns the signature for this parameter evaluating it on the
+               //   @tc context
+               // </summary>
+               public string GetSignature (TypeContainer tc)
+               {
+                       Type t = tc.LookupType (Type, false);
 
-               public Modifier ModFlags {
-                       get {
-                               return mod;
-                       }
+                       if (t == null)
+                               return "";
+                       
+                       return ModSignature () + t.FullName;
                }
        }
 
@@ -91,44 +80,55 @@ namespace CIR {
                        this.array_parameter = array_parameter;
                }
 
-               void compute_signature ()
-               {
-                       signature = "";
-                       
-                       if (fixed_parameters != null){
-                               for (int i = 0; i < fixed_parameters.Count; i++){
-                                       Parameter par = (Parameter) fixed_parameters [i];
-                                       
-                                       signature += par.Signature;
-                               }
-                       }
-
-                       //
-                       // Note: as per the spec, the `params' arguments (array_parameter)
-                       // are not used in the signature computation for a method
-                       //
-               }
-               
+               // <summary>
+               //   Returns the fixed parameters element
+               // </summary>
                public ParameterCollection FixedParameters {
                        get {
                                return fixed_parameters;
                        }
                }
 
+               // <summary>
+               //   Returns the array parameter.
+               // </summary>
                public Parameter ArrayParameter {
                        get {
                                return array_parameter;
                        }
                }
 
-               // Removed for now, as we can not compute this at
-               // boot strap.
-               public string Signature {
-                       get {
-                               return signature;
+               public void ComputeSignature (TypeContainer tc)
+               {
+                       signature = "";
+                       if (fixed_parameters != null){
+                               for (int i = 0; i < fixed_parameters.Count; i++){
+                                       Parameter par = (Parameter) fixed_parameters [i];
+                                       
+                                       signature += par.GetSignature (tc);
+                               }
                        }
+                       //
+                       // Note: as per the spec, the `params' arguments (array_parameter)
+                       // are not used in the signature computation for a method
+                       //
                }
 
+               // <summary>
+               //    Returns the signature of the Parameters evaluated in
+               //    the @tc environment
+               // </summary>
+               public string GetSignature (TypeContainer tc)
+               {
+                       if (signature == null)
+                               ComputeSignature (tc);
+                       
+                       return signature;
+               }
+               
+               // <summary>
+               //    Returns the paramenter information based on the name
+               // </summary>
                public Parameter GetParameterByName (string name)
                {
                        if (fixed_parameters == null)
@@ -137,9 +137,30 @@ namespace CIR {
                        foreach (Parameter par in fixed_parameters)
                                if (par.Name == name)
                                        return par;
+
                        return null;
                }
+
+               // <summary>
+               //   Returns the argument types as an array
+               // </summary>
+               public Type [] GetTypes (TypeContainer tc)
+               {
+                       int extra = (array_parameter != null) ? 1 : 0;
+                       Type [] types = new Type [fixed_parameters.Count + extra];
+                       int i = 0;
+                       
+                       foreach (Parameter p in fixed_parameters){
+                               types [i++] = tc.LookupType (p.Name, false);
+                       }
+
+                       if (extra > 0)
+                               types [i] = Type.GetType ("System.Object");
+
+                       return types;
+               }
        }
 }
                
        
+
index e555911e793c0e291388120640a8ea8da39cf279..aec37b6a3b018679c24930b730f9978921dc1649 100755 (executable)
@@ -58,7 +58,7 @@ namespace CIR {
                
                public RootContext ()
                {
-                       tree = new Tree ();
+                       tree = new Tree (null);
                        type_manager = new TypeManager ();
                        report = new Report ();
 
@@ -190,7 +190,7 @@ namespace CIR {
                //
                TypeBuilder CreateInterface (Interface iface)
                {
-                       TypeBuilder tb = iface.Definition;
+                       TypeBuilder tb = iface.TypeBuilder;
                        Type [] ifaces;
                        string name;
                        bool error;
@@ -216,7 +216,7 @@ namespace CIR {
                                            TypeAttributes.Abstract,
                                            null,   // Parent Type
                                            ifaces);
-                       iface.Definition = tb;
+                       iface.TypeBuilder = tb;
 
                        type_manager.AddUserType (name, tb);
 
@@ -427,7 +427,7 @@ namespace CIR {
                //
                TypeBuilder CreateType (TypeContainer tc, bool is_class)
                {
-                       TypeBuilder tb = tc.Definition;
+                       TypeBuilder tb = tc.TypeBuilder;
                        Type parent;
                        Type [] ifaces;
                        bool error;
@@ -452,7 +452,7 @@ namespace CIR {
                                            parent,
                                            ifaces);
 
-                       tc.Definition = tb;
+                       tc.TypeBuilder = tb;
                        type_manager.AddUserType (name, tb);
                        tc.InTransit = false;
                        
@@ -554,11 +554,6 @@ namespace CIR {
                        return LookupType (tc, name, true);
                }
                
-               public void PopulateInterface (Interface ifacex)
-               {
-                       
-               }
-
                // <summary>
                //   Populates the structs and classes with fields and methods
                // </summary>
@@ -569,9 +564,13 @@ namespace CIR {
                {
                        Hashtable ifaces, classes;
                        
-                       if ((ifaces = tree.Interfaces) != null)
-                               foreach (DictionaryEntry de in ifaces)
-                                       PopulateInterface ((Interface) de.Value);
+                       if ((ifaces = tree.Interfaces) != null){
+                               foreach (DictionaryEntry de in ifaces){
+                                       Interface iface = (Interface) de.Value;
+
+                                       iface.Populate ();
+                               }
+                       }
 
                        if ((classes = tree.Classes) != null){
                                foreach (DictionaryEntry de in classes){
@@ -580,9 +579,8 @@ namespace CIR {
                                        tc.Populate (this);
                                }
                        }
-                               
                }
-               
+
                // <summary>
                //   Compiling against Standard Libraries property.
                // </summary>
index 394e8389c7920f37548062f25ff1d336fc288792..7b651dbc14c6b57a40d8c7de81a1654e99d966e0 100755 (executable)
@@ -48,9 +48,12 @@ namespace CIR
                // </summary>
                Hashtable classes;
 
-               public Tree ()
+               RootContext rc;
+               
+               public Tree (RootContext rc)
                {
-                       root_types = new TypeContainer (null, "");
+                       root_types = new TypeContainer (rc, null, "");
+                       this.rc = rc;
                }