Ver Fonte

2001-08-26 Miguel de Icaza <[email protected]>

	* typemanager.cs (InitCoreTypes): Initialize the various core
	types after we have populated the type manager with the user
	defined types (this distinction will be important later while
	compiling corlib.dll)

	* expression.cs, literal.cs, assign.cs, constant.cs: Started work
	on Expression Classification.  Now all expressions have a method
	`Resolve' and a method `Emit'.

	* codegen.cs, cs-parser.jay: Fixed the bug that stopped code
	generation from working.     Also add some temporary debugging
	code.

svn path=/trunk/mcs/; revision=632
Miguel de Icaza há 24 anos atrás
pai
commit
2ca4ca638d
9 ficheiros alterados com 375 adições e 96 exclusões
  1. 15 0
      mcs/mcs/ChangeLog
  2. 12 0
      mcs/mcs/assign.cs
  3. 32 1
      mcs/mcs/codegen.cs
  4. 13 5
      mcs/mcs/constant.cs
  5. 1 1
      mcs/mcs/cs-parser.jay
  6. 11 0
      mcs/mcs/driver.cs
  7. 177 86
      mcs/mcs/expression.cs
  8. 84 2
      mcs/mcs/literal.cs
  9. 30 1
      mcs/mcs/typemanager.cs

+ 15 - 0
mcs/mcs/ChangeLog

@@ -1,3 +1,18 @@
+2001-08-26  Miguel de Icaza  <[email protected]>
+
+	* typemanager.cs (InitCoreTypes): Initialize the various core
+	types after we have populated the type manager with the user
+	defined types (this distinction will be important later while
+	compiling corlib.dll)
+
+	* expression.cs, literal.cs, assign.cs, constant.cs: Started work
+	on Expression Classification.  Now all expressions have a method
+	`Resolve' and a method `Emit'.
+
+	* codegen.cs, cs-parser.jay: Fixed the bug that stopped code
+	generation from working.     Also add some temporary debugging
+	code. 
+	
 2001-08-24  Miguel de Icaza  <[email protected]>
 
 	* codegen.cs: Lots of code generation pieces.  This is only the

+ 12 - 0
mcs/mcs/assign.cs

@@ -36,5 +36,17 @@ namespace CIR {
 				source = value;
 			}
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 }
+
+
+

+ 32 - 1
mcs/mcs/codegen.cs

@@ -78,8 +78,29 @@ namespace CIR {
 			check_state = false;
 		}
 
+		public bool ConvertTo (Type target, Type source, bool verbose)
+		{
+			if (target == source)
+				return true;
+
+			if (verbose)
+				parent.RootContext.Report.Error (
+					31, "Can not convert to type bool");
+			
+			return false;
+		}
+		
 		public void EmitBoolExpression (Expression e)
 		{
+			e.Resolve (parent);
+			if (!ConvertTo (TypeManager.bool_type, e.Type, false)){
+				parent.RootContext.Report.Error (
+					31, "Can not convert the expression to a boolean");
+				return;
+			}
+			
+			e.Emit (this);
+			
 			//
 			// Sample: for now we just load true on the stack
 			//
@@ -194,6 +215,8 @@ namespace CIR {
 		
 		void EmitStatement (Statement s)
 		{
+			Console.WriteLine ("Emitting statement of type" + s.GetType ().ToString ());
+			
 			if (s is If)
 				EmitIf ((If) s);
 			else if (s is Do)
@@ -210,18 +233,26 @@ namespace CIR {
 				EmitChecked ((Checked) s);
 			else if (s is Unchecked)
 				EmitUnChecked ((Unchecked) s);
+			else if (s is Block)
+				EmitBlock ((Block) s);
+			else {
+				Console.WriteLine ("Unhandled Statement type: " +
+						   s.GetType ().ToString ());
+			}
 		}
 
 		void EmitBlock (Block block)
 		{
-			foreach (Statement s in block.Statements)
+			foreach (Statement s in block.Statements){
 				EmitStatement (s);
+			}
 		}
 		
 		public void EmitTopBlock (Block block)
 		{
 			block.EmitMeta (parent, ig, block);
 
+			Console.WriteLine ("Emitting Top Block");
 			EmitBlock (block);
 
 			ig.Emit (OpCodes.Ret);

+ 13 - 5
mcs/mcs/constant.cs

@@ -7,7 +7,7 @@ namespace CIR {
 	public class Constant : Expression {
 		string     name;
 		Expression expr;
-		string     type;
+		string     constant_type;
 		int        mod_flags;
 
 		public const int AllowedModifiers =
@@ -17,9 +17,9 @@ namespace CIR {
 			Modifiers.INTERNAL |
 			Modifiers.PRIVATE;
 
-		public Constant (string type, string name, Expression expr, int mod_flags)
+		public Constant (string constant_type, string name, Expression expr, int mod_flags)
 		{
-			this.type = type;
+			this.constant_type = constant_type;
 			this.name = name;
 			this.expr = expr;
 			this.mod_flags = Modifiers.Check (AllowedModifiers, mod_flags, Modifiers.PRIVATE);
@@ -33,7 +33,7 @@ namespace CIR {
 
 		public string ConstantType {
 			get {
-				return type;
+				return constant_type;
 			}
 		}
 
@@ -49,6 +49,14 @@ namespace CIR {
 			}
 		}
 
+		public override void Resolve (TypeContainer tc)
+		{
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
+		       
 		// <summary>
 		//   Defines the constant in the @parent
 		// </summary>
@@ -58,7 +66,7 @@ namespace CIR {
 			TypeCode tc;
 			Type t;
 			
-			t = rc.LookupType (parent, type);
+			t = rc.LookupType (parent, constant_type);
 			if (t == null)
 				return;
 

+ 1 - 1
mcs/mcs/cs-parser.jay

@@ -2057,7 +2057,7 @@ statement
 	;
 
 embedded_statement
-	: block				{ current_block.AddStatement ((Statement) $1); }
+	: block
 	| empty_statement
         | expression_statement
 	| selection_statement

+ 11 - 0
mcs/mcs/driver.cs

@@ -293,6 +293,17 @@ namespace CIR
 			//
 			context.ResolveTree ();
 			context.PopulateTypes ();
+
+			//
+			// Before emitting, we need to get the core
+			// types emitted from the user defined types
+			// or from the system ones.
+			//
+			context.TypeManager.InitCoreTypes ();
+
+			//
+			// The code generator
+			//
 			context.EmitCode ();
 			
 			if (context.Report.Errors > 0){

+ 177 - 86
mcs/mcs/expression.cs

@@ -10,49 +10,61 @@ namespace CIR {
 	using System.Collections;
 	using System.Diagnostics;
 	using System;
-	
-	public abstract class Expression {
-		string type;
-		bool is_lvalue;
 
-		public string Type {
+	// <remarks>
+	//   The ExprClass class contains the is used to pass the 
+	//   classification of an expression (value, variable, namespace,
+	//   type, method group, property access, event access, indexer access,
+	//   nothing).
+	// </remarks>
+	public enum ExprClass {
+		Invalid,
+		
+		Value, Variable, Namespace, Type,
+		MethodGroup, PropertyAccess,
+		EventAccess, IndexerAccess, Nothing, 
+	}
+
+	// <remarks>
+	//   Base class for expressions
+	// </remarks>
+	public abstract class Expression {
+		protected ExprClass eclass;
+		protected Type      type;
+		
+		public Type Type {
 			get {
 				return type;
 			}
+
+			set {
+				type = value;
+			}
 		}
 
-		public bool IsLValue {
+		public ExprClass ExprClass {
 			get {
-				return is_lvalue;
+				return eclass;
 			}
 
 			set {
-				is_lvalue = value;
+				eclass = value;
 			}
 		}
 
-		public virtual bool IsLiteral {
-			get {
-				return false;
-			}
-		}
+		public abstract void Resolve (TypeContainer tc);
+		public abstract void Emit    (EmitContext ec);
 		
 		// <summary>
 		//   Protected constructor.  Only derivate types should
 		//   be able to be created
 		// </summary>
-		// protected Type (
 
 		protected Expression ()
 		{
+			eclass = ExprClass.Invalid;
 			type = null;
 		}
-		
-		static bool EvaluateType (Expression expr, out string setType)
-		{
-			setType = null;
-			return true;
-		}
 	}
 
 	public class Unary : Expression {
@@ -90,10 +102,19 @@ namespace CIR {
 				oper = value;
 			}
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class Probe : Expression {
-		string type;
+		string probe_type;
 		Expression expr;
 		Operator oper;
 
@@ -101,10 +122,10 @@ namespace CIR {
 			Is, As
 		}
 		
-		public Probe (Operator oper, Expression expr, string type)
+		public Probe (Operator oper, Expression expr, string probe_type)
 		{
 			this.oper = oper;
-			this.type = type;
+			this.probe_type = probe_type;
 			this.expr = expr;
 		}
 
@@ -122,25 +143,33 @@ namespace CIR {
 
 		public string ProbeType {
 			get {
-				return type;
+				return probe_type;
 			}
 		}
-			       
+
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 	
 	public class Cast : Expression {
-		string type;
+		string target_type;
 		Expression expr;
 		
-		public Cast (string type, Expression expr)
+		public Cast (string cast_type, Expression expr)
 		{
-			this.type = type;
+			this.target_type = target_type;
 			this.expr = expr;
 		}
 
 		public string TargetType {
 			get {
-				return type;
+				return target_type;
 			}
 		}
 
@@ -152,6 +181,15 @@ namespace CIR {
 				expr = value;
 			}
 		}
+		
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class Binary : Expression {
@@ -204,6 +242,14 @@ namespace CIR {
 				right = value;
 			}
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class Conditional : Expression {
@@ -233,6 +279,15 @@ namespace CIR {
 				return falseExpr;
 			}
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class SimpleName : Expression {
@@ -248,6 +303,15 @@ namespace CIR {
 				return name;
 			}
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 	
 	public class LocalVariableReference : Expression {
@@ -271,6 +335,15 @@ namespace CIR {
 				return name;
 			}
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class ParameterReference : Expression {
@@ -288,6 +361,15 @@ namespace CIR {
 				return name;
 			}
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 	
 	// <summary>
@@ -300,13 +382,13 @@ namespace CIR {
 			Out
 		};
 
-		AType type;
+		public readonly AType Type;
 		Expression expr;
 
 		public Argument (Expression expr, AType type)
 		{
 			this.expr = expr;
-			this.type = type;
+			this.Type = type;
 		}
 
 		public Expression Expr {
@@ -314,19 +396,13 @@ namespace CIR {
 				return expr;
 			}
 		}
-
-		public AType Type {
-			get {
-				return type;
-			}
-		}
 	}
 
 	// <summary>
 	//   Invocation of methods or delegates.
 	// </summary>
 	public class Invocation : Expression {
-		ArrayList arguments;
+		public readonly ArrayList Arguments;
 		Expression expr;
 
 		//
@@ -339,7 +415,7 @@ namespace CIR {
 		public Invocation (Expression expr, ArrayList arguments)
 		{
 			this.expr = expr;
-			this.arguments = arguments;
+			Arguments = arguments;
 		}
 
 		public Expression Expr {
@@ -348,77 +424,91 @@ namespace CIR {
 			}
 		}
 
-		public ArrayList Arguments {
-			get {
-				return arguments;
-			}
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
 		}
 	}
 
 	public class New : Expression {
-		ArrayList arguments;
-		string requested_type;
+		public readonly ArrayList Arguments;
+		public readonly string    RequestedType;
 
 		public New (string requested_type, ArrayList arguments)
 		{
-			this.requested_type = requested_type;
-			this.arguments = arguments;
+			RequestedType = requested_type;
+			Arguments = arguments;
 		}
 		
-		public ArrayList Arguments{
-			get {
-				return arguments;
-			}
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
 		}
-		
-		public string RequestedType {
-			get {
-				return requested_type;
-			}
+
+		public override void Emit (EmitContext ec)
+		{
 		}
 	}
 
 	public class This : Expression {
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class TypeOf : Expression {
-		string queried_type;
+		public readonly string QueriedType;
 		
 		public TypeOf (string queried_type)
 		{
-			this.queried_type = queried_type;
+			QueriedType = queried_type;
 		}
 
-		public string QueriedType {
-			get {
-				return queried_type;
-			}
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
 		}
 	}
 
 	public class SizeOf : Expression {
-		string queried_type;
+		public readonly string QueriedType;
 		
 		public SizeOf (string queried_type)
 		{
-			this.queried_type = queried_type;
+			this.QueriedType = queried_type;
 		}
 
-		public string QueriedType {
-			get {
-				return queried_type;
-			}
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
 		}
 	}
 
 	public class MemberAccess : Expression {
+		public readonly string Identifier;
 		Expression expr;
-		string id;
 		
 		public MemberAccess (Expression expr, string id)
 		{
 			this.expr = expr;
-			this.id = id;
+			Identifier = id;
 		}
 
 		public Expression Expr {
@@ -426,35 +516,36 @@ namespace CIR {
 				return expr;
 			}
 		}
+		
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
+		}
 
-		public string Identifier {
-			get {
-				return id;
-			}
+		public override void Emit (EmitContext ec)
+		{
 		}
+
 	}
 
 	public class BuiltinTypeAccess : Expression {
-		string access_base;
-		string method;
+		public readonly string AccessBase;
+		public readonly string Method;
 		
 		public BuiltinTypeAccess (string type, string method)
 		{
 			System.Console.WriteLine ("DUDE! This type should be fully resolved!");
-			this.access_base = access_base;
-			this.method = method;
+			AccessBase = type;
+			Method = method;
 		}
 
-		public string AccessBase {
-			get {
-				return access_base;
-			}
+		public override void Resolve (TypeContainer tc)
+		{
+			// FIXME: Implement;
 		}
 
-		public string Method {
-			get {
-				return method;
-			}
+		public override void Emit (EmitContext ec)
+		{
 		}
 	}
 }

+ 84 - 2
mcs/mcs/literal.cs

@@ -7,9 +7,10 @@
 // (C) 2001 Ximian, Inc.
 //
 
+using System;
+
 namespace CIR {
 	public abstract class Literal : Expression {
-
 		// <summary>
 		//   This is different from ToString in that ToString
 		//   is supposed to be there for debugging purposes,
@@ -59,14 +60,25 @@ namespace CIR {
 		public NullLiteral ()
 		{
 		}
-
+		
 		override public string AsString ()
 		{
 			return "null";
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			eclass = ExprClass.Value;
+			type = TypeManager.object_type;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class BoolLiteral : Literal {
+		static Type bool_type = Type.GetType ("System.Bool");
 		bool val;
 		
 		public BoolLiteral (bool val)
@@ -78,6 +90,16 @@ namespace CIR {
 		{
 			return val ? "true" : "false";
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			eclass = ExprClass.Value;
+			type = bool_type;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class CharLiteral : Literal {
@@ -92,6 +114,16 @@ namespace CIR {
 		{
 			return "\"" + descape (c) + "\"";
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			eclass = ExprClass.Value;
+			type = TypeManager.char_type;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class IntLiteral : Literal {
@@ -106,6 +138,16 @@ namespace CIR {
 		{
 			return i.ToString ();
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			eclass = ExprClass.Value;
+			type = TypeManager.int32_type;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class FloatLiteral : Literal {
@@ -120,6 +162,16 @@ namespace CIR {
 		{
 			return f.ToString ();
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			eclass = ExprClass.Value;
+			type = TypeManager.float_type;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class DoubleLiteral : Literal {
@@ -134,6 +186,16 @@ namespace CIR {
 		{
 			return d.ToString ();
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			eclass = ExprClass.Value;
+			type = TypeManager.double_type;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class DecimalLiteral : Literal {
@@ -148,6 +210,16 @@ namespace CIR {
 		{
 			return d.ToString ();
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			eclass = ExprClass.Value;
+			type = TypeManager.decimal_type;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 
 	public class StringLiteral : Literal {
@@ -163,5 +235,15 @@ namespace CIR {
 		{
 			return "\"" + s + "\"";
 		}
+
+		public override void Resolve (TypeContainer tc)
+		{
+			eclass = ExprClass.Value;
+			type = TypeManager.string_type;
+		}
+
+		public override void Emit (EmitContext ec)
+		{
+		}
 	}
 }

+ 30 - 1
mcs/mcs/typemanager.cs

@@ -15,7 +15,17 @@ using System.Reflection;
 using System.Reflection.Emit;
 
 public class TypeManager {
-
+	static public Type object_type;
+	static public Type string_type;
+	static public Type int32_type;
+	static public Type uint32_type;
+	static public Type float_type;
+	static public Type double_type;
+	static public Type char_type;
+	static public Type short_type;
+	static public Type decimal_type;
+	static public Type bool_type;
+	
 	// <remarks>
 	//   Holds the Array of Assemblies that have been loaded
 	//   (either because it is the default or the user used the
@@ -82,6 +92,25 @@ public class TypeManager {
 		return null;
 	}
 
+	// <remarks>
+	//   The types have to be initialized after the initial
+	//   population of the type has happened (for example, to
+	//   bootstrap the corlib.dll
+	// </remarks>
+	public void InitCoreTypes ()
+	{
+		object_type  = LookupType ("System.Object");
+		string_type  = LookupType ("System.String");
+		int32_type   = LookupType ("System.Int32");
+		uint32_type  = LookupType ("System.UInt32"); 
+		float_type   = LookupType ("System.Single");
+		double_type  = LookupType ("System.Double");
+		char_type    = LookupType ("System.Char");
+		short_type   = LookupType ("System.Short");
+		decimal_type = LookupType ("System.Decimal");
+		bool_type    = LookupType ("System.Bool");
+	}
+	
 	// <summary>
 	//   Returns the User Defined Types
 	// </summary>