+2001-07-17 Miguel de Icaza <miguel@ximian.com>
+
+ * tree.cs: moved IGenerator interface and renamed it to ITreeDump
+ and put it here.
+
+ Get rid of old crufty code.
+
+ * rootcontext.cs: Use this to keep track of the parsed
+ representation and the defined types available to the program.
+
+ * gen-treedump.cs: adjust for new convention.
+
+ * type.cs: Split out the type manager, and the assembly builder
+ from here.
+
+ * typemanager.cs: the type manager will live here now.
+
+ * cil-codegen.cs: And the code generator here.
+
2001-07-14 Sean MacIsaac <macisaac@ximian.com>
* makefile: Fixed up for easy making.
--- /dev/null
+//
+// cil-codegen.cs: The CIL code generation interface
+//
+// Author:
+// Miguel de Icaza (miguel@ximian.com)
+//
+// (C) 2001 Ximian, Inc.
+//
+
+using System;
+using System.Reflection;
+using System.Reflection.Emit;
+
+public class CilCodeGen {
+ AppDomain current_domain;
+ AssemblyBuilder assembly_builder;
+ ModuleBuilder module_builder;
+
+ public CilCodeGen (string name, string output)
+ {
+ AssemblyName an;
+
+ an = new AssemblyName ();
+ an.Name = "AssemblyName";
+ current_domain = AppDomain.CurrentDomain;
+ assembly_builder = current_domain.DefineDynamicAssembly (
+ an, AssemblyBuilderAccess.RunAndSave);
+
+ module_builder = assembly_builder.DefineDynamicModule (name, output);
+ }
+
+ public AssemblyBuilder AssemblyBuilder {
+ get {
+ return assembly_builder;
+ }
+ }
+
+ public ModuleBuilder ModuleBuilder {
+ get {
+ return module_builder;
+ }
+ }
+
+}
return 0;
}
- override public Type Define (Tree tree)
- {
- return null;
- }
-
public delegate void VisitContainer (TypeContainer container, object cback_data);
void VisitTypesAt (TypeContainer root, VisitContainer visit, object cback)
in_transit = value;
}
}
-
- abstract public Type Define (Tree tree);
}
}
using CSC;\r
\r
/// <summary>\r
- /// Summary description for Class1.\r
+ /// The compiler driver.\r
/// </summary>\r
public class Driver\r
{\r
// Lookup paths\r
ArrayList link_paths;\r
\r
- // Our parser context.\r
- Tree context;\r
- \r
+ RootContext context; \r
+\r
bool yacc_verbose = false;\r
\r
int error_count = 0;\r
\r
}\r
\r
- public IGenerator lookup_output (string name)\r
+ public ITreeDump lookup_dumper (string name)\r
{\r
if (name == "tree")\r
return new Generator.TreeDump ();\r
- if (name == "il")\r
- return new MSIL.Generator ();\r
+ \r
+ // if (name == "il")\r
+ // return new MSIL.Generator ();\r
\r
return null;\r
}\r
return 1;\r
}\r
\r
- context.AddAssembly (a);\r
+ context.TypeManager.AddAssembly (a);\r
}\r
return 0;\r
}\r
- \r
+\r
+ // <summary>\r
+ // Loads all assemblies referenced on the command line\r
+ // </summary>\r
public int LoadReferences ()\r
{\r
int errors = 0;\r
return errors;\r
}\r
\r
- \r
+ // <summary>\r
+ // Parses the arguments, and drives the compilation\r
+ // process.\r
+ //\r
+ // TODO: Mostly structured to debug the compiler\r
+ // now, needs to be turned into a real driver soon.\r
+ // </summary>\r
public Driver (string [] args)\r
{\r
Stream output_stream = Console.OpenStandardOutput ();\r
- IGenerator generator = null;\r
+ ITreeDump generator = null;\r
int errors = 0, i;\r
\r
- context = new Tree ();\r
+ context = new RootContext ();\r
references = new ArrayList ();\r
link_paths = new ArrayList ();\r
\r
}\r
\r
if (arg.StartsWith ("-t")){\r
- generator = lookup_output (args [++i]);\r
+ generator = lookup_dumper (args [++i]);\r
continue;\r
}\r
\r
continue;\r
}\r
\r
- errors += parse (context, arg);\r
+ errors += parse (context.Tree, arg);\r
}\r
if (errors > 0)\r
error ("// Parsing failed");\r
notice ("// Assemblies loaded");\r
\r
\r
- errors += context.BuilderInit ("Module", "Module.exe");\r
-\r
//\r
- // Name resolution on the tree.\r
+ // Dumping the parsed tree.\r
//\r
- errors += context.Resolve ();\r
- \r
- //\r
- // Code generation from the tree\r
+ // This code generation interface is only here\r
+ // for debugging purposes, it will change as we\r
+ // move to the new tree. \r
//\r
if (generator != null){\r
StreamWriter output = new StreamWriter (output_stream);\r
\r
- errors += generator.GenerateFromTree (context, output);\r
+ errors += generator.Dump (context.Tree, output);\r
\r
if (errors > 0)\r
error ("// Compilation failed");\r
\r
output.Flush ();\r
output.Close ();\r
- }\r
+ } \r
\r
error_count = errors;\r
}\r
return (Expression) defined_names [name];
}
}
-
- public override Type Define (Tree tree)
- {
- return null;
- }
}
}
namespace MSIL {
- public class Generator : CIR.IGenerator {
+ public class Generator : CIR.ITreeDump {
StreamWriter o;
int indent = 0;
namespace Generator {
- public class TreeDump : CIR.IGenerator {
+ public class TreeDump : CIR.ITreeDump {
StreamWriter o;
int indent;
bool indented;
}
}
- public int GenerateFromTree (Tree tree, StreamWriter output)
+ public int Dump (Tree tree, StreamWriter output)
{
this.o = output;
bases = value;
}
}
-
- Type def;
- bool in_transit = false;
- public override Type Define (Tree tree)
- {
- if (in_transit){
- CSC.CSharpParser.error (529, "There is a cycle in interface " + Name);
- return null;
- }
- in_transit = true;
-
- ModuleBuilder mb = tree.ModuleBuilder;
-
- if (bases != null){
- // FIXME: Go over each one of the TypeRefs
- // in Bases and resolve.
- Console.WriteLine ("Base interfaces not yet supported");
- return null;
- }
-
- TypeAttributes attr = TypeAttributes.Interface;
-
- attr |= Modifiers.TypeAttr (mod_flags);
-
- def = mb.DefineType (Name, attr);
-
- in_transit = false;
-
- return def;
- }
}
public class InterfaceMemberBase {
assign.cs \
driver.cs $(COMMON_SOURCES) \
class.cs \
+ cil-codegen.cs \
constant.cs \
decl.cs \
enum.cs \
expression.cs \
- generator.cs \
- gen-il.cs \
gen-treedump.cs \
interface.cs \
literal.cs \
namespace.cs \
parameter.cs \
parameterCollection.cs \
+ rootcontext.cs \
statement.cs \
statementCollection.cs \
- type.cs
+ type.cs \
+ typemanager.cs
TEST_TOKENIZER_SOURCES = test-token.cs $(COMMON_SOURCES)
--- /dev/null
+//
+// rootcontext.cs: keeps track of our tree representation, and assemblies loaded.
+//
+// Author: Miguel de Icaza (miguel@gnu.org)
+//
+// Licensed under the terms of the GNU GPL
+//
+// (C) 2001 Ximian, Inc (http://www.ximian.com)
+
+namespace CIR {
+
+ public class RootContext {
+
+ //
+ // Contains the parsed tree
+ //
+ Tree tree;
+
+ //
+ // Contains loaded assemblies and our generated code as we go.
+ //
+ TypeManager type_manager;
+
+ public RootContext ()
+ {
+ tree = new Tree ();
+ type_manager = new TypeManager ();
+ }
+
+ public TypeManager TypeManager {
+ get {
+ return type_manager;
+ }
+ }
+
+ public Tree Tree {
+ get {
+ return tree;
+ }
+ }
+ }
+}
+
using System.Collections;
using System.Reflection;
using System.Reflection.Emit;
+using System.IO;
namespace CIR
{
+ public interface ITreeDump {
+ int Dump (Tree tree, StreamWriter output);
+ void ParseOptions (string options);
+ }
+
// <summary>
- // A storage for temporary IL trees
+ //
+ // We store here all the toplevel types that we have parsed,
+ // this is the root of all information we have parsed.
+ //
// </summary>
public class Tree {
TypeContainer root_types;
- // <summary>
- // Holds the Array of Assemblies that have been loaded
- // (either because it is the default or the user used the
- // -r command line option)
- // </summary>
- ArrayList assemblies;
-
- // <summary>
- // This is used to map defined FQN to Types
- // </summary>
- Hashtable types;
-
// <summary>
// This maps source defined types that are defined
// in the source code to their object holders (Class, Struct,
// </summary>
Hashtable source_types;
- AppDomain current_domain;
- AssemblyBuilder assembly_builder;
- ModuleBuilder module_builder;
-
public Tree ()
{
root_types = new TypeContainer (null, "");
- assemblies = new ArrayList ();
- types = new Hashtable ();
source_types = new Hashtable ();
}
- public int BuilderInit (string name, string output)
- {
- AssemblyName an;
-
- an = new AssemblyName ();
- an.Name = "AssemblyName";
- current_domain = AppDomain.CurrentDomain;
- assembly_builder = current_domain.DefineDynamicAssembly (
- an, AssemblyBuilderAccess.RunAndSave);
-
- module_builder = assembly_builder.DefineDynamicModule (name, output);
-
- return 0;
- }
-
- public AssemblyBuilder AssemblyBuilder {
- get {
- return assembly_builder;
- }
- }
-
- public ModuleBuilder ModuleBuilder {
- get {
- return module_builder;
- }
- }
public void RecordType (string name, DeclSpace decl)
{
}
}
- public int ResolveTypeContainerTypes (TypeContainer type)
- {
- return 0;
- }
-
- public int ResolveNames (TypeContainer types)
- {
- int errors = 0;
-
- if (types == null)
- return 0;
-
- foreach (DictionaryEntry de in types.Types){
- TypeContainer type = (TypeContainer) de.Value;
-
- errors += ResolveTypeContainerTypes (type);
- }
-
- return errors;
- }
-
- public Type ResolveType (string name)
- {
- Type t = (Type) types [name];
-
- if (t != null)
- return t;
-
- DeclSpace decl;
- decl = (DeclSpace) source_types [name];
-
- // FIXME: handle using here.
- if (decl == null){
- CSC.CSharpParser.error (234, "The type or namespace name '" + name
- + "' does not exist in the current space");
- return null;
- }
-
- return decl.Define (this);
- }
-
- int iscan_errors = 0;
- void interface_scan (TypeContainer container, object data)
- {
- foreach (Interface iface in container.Interfaces){
- Type t = ResolveType (iface.Name);
-
- if (t != null){
- CSC.CSharpParser.error (101, "There is already a definition for " + iface.Name);
- iscan_errors++;
- }
- iface.Define (this);
- }
- }
-
- public int ResolveInterfaces (TypeContainer type)
- {
- TypeContainer.VisitContainer iscanner;
-
- iscanner = new TypeContainer.VisitContainer (interface_scan);
- type.VisitTypes (iscanner, this);
- return iscan_errors;
- }
-
- public int ResolveTypeContainerParents (TypeContainer type)
- {
- return type.ResolveParents (this);
- }
-
- public int Resolve ()
- {
- int errors = 0;
-
- errors += ResolveInterfaces (root_types);
- errors += ResolveTypeContainerParents (root_types);
- return 0;
- }
-
- public void AddAssembly (Assembly a)
- {
- assemblies.Add (a);
- foreach (Type t in a.GetExportedTypes ()){
- types.Add (t.FullName, t);
- }
- }
}
}
--- /dev/null
+//
+// typegen.cs: type generation
+//
+// Author: Miguel de Icaza (miguel@gnu.org)
+//
+// Licensed under the terms of the GNU GPL
+//
+// (C) 2001 Ximian, Inc (http://www.ximian.com)
+//
+//
+
+using System;
+using System.Collections;
+using System.Reflection;
+using System.Reflection.Emit;
+
+public class TypeManager {
+
+ // <summary>
+ // Holds the Array of Assemblies that have been loaded
+ // (either because it is the default or the user used the
+ // -r command line option)
+ // </summary>
+ ArrayList assemblies;
+
+ // <summary>
+ // This is used to map defined FQN to Types
+ // </summary>
+ Hashtable types;
+
+ public TypeManager ()
+ {
+ assemblies = new ArrayList ();
+ types = new Hashtable ();
+ }
+
+ // <summary>
+ // Registers a single type with the Type Manager. This is
+ // an interface for our type builder.
+ // </summary>
+ public void AddType (string name, Type t)
+ {
+ types.Add (t.FullName, t);
+ }
+
+ // <summary>
+ // Registers an assembly to load types from.
+ // </summary>
+ public void AddAssembly (Assembly a)
+ {
+ assemblies.Add (a);
+ foreach (Type t in a.GetExportedTypes ()){
+ AddType (t.FullName, t);
+ }
+ }
+
+ // <summary>
+ // Returns the Type associated with @name
+ // </summary>
+ public Type LookupType (string name)
+ {
+ Type t = (Type) types [name];
+
+ return t;
+ }
+}
+