Files
mono/mcs/class/System.Web/System.Web.Compilation/BaseCompiler.cs
T
Gonzalo Paniagua Javier e80814a23c 2003-04-30 Gonzalo Paniagua Javier <[email protected]>
* AspComponentFoundry.cs: simplified it a lot by using Type and Assembly
	instead of their names.

	* AspElements.cs: removed. No longer needed.

	* AspGenerator.cs: this file is now in charge of interfacing between
	the parser and the compiler. It manages the creation of the
	ControlBuilder tree and the compilation of the CodeDOM tree.

	* AspParser.cs: tag handling is simpler now.  Instead of a whole bunch
	of different Types, tags are just and id and a set of attributes.
	Implement ILocation interface.

	* AspTokenizer.cs: added a few methods to help the parser implementing
	ILocation.

	* BaseCompiler.cs: handles the portions of the CodeDOM tree that are
	common to appliaction, page and user control, including the actual
	compilation and error handling.

	* CSCompiler.cs: compiles C# files using CodeDOM interfaces.

	* CachingCompiler.cs: simplified to use the new interfaces.

	* CompilationException.cs: it's now using CompilationResult to report
	errors.

	* CompilationResult.cs: Removed file.

	* Directive.cs: to check for the validity of a directive.

	* GlobalAsaxCompiler.cs: simplified a lot, as most of the work is done
	in BaseCompiler.

	* ILocation.cs: interface used to now the exact place where a parse
	error happens.

	* PageCompiler.cs: generates a couple of methods that are only used in
	pages.

	* ParseException.cs: use the ILocation interface.

	* TagAttributes.cs: handles the attributes of the tags parsed.

	* TagType.cs: an enum for the different kinds of tags.

	* TemplateControlCompiler.cs: this is the one that does most of the
	conversion from teh ControlBuilder tree into a CodeDOM tree.

	* UserControlCompiler.cs: simplified as most of the work is done in
	its base classes.

	* WebServiceCompiler.cs: dummy.

svn path=/trunk/mcs/; revision=14140
2003-04-30 04:56:01 +00:00

185 lines
4.8 KiB
C#

//
// System.Web.Compilation.BaseCompiler
//
// Authors:
// Gonzalo Paniagua Javier ([email protected])
//
// (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
//
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Reflection;
using System.Text;
using System.Web.UI;
//temp:
using Microsoft.CSharp;
using System.IO;
namespace System.Web.Compilation
{
abstract class BaseCompiler
{
TemplateParser parser;
CodeDomProvider provider;
ICodeCompiler compiler;
CodeCompileUnit unit;
CodeNamespace mainNS;
CompilerParameters compilerParameters;
protected CodeTypeDeclaration mainClass;
protected CodeTypeReferenceExpression mainClassExpr;
protected static CodeThisReferenceExpression thisRef = new CodeThisReferenceExpression ();
protected BaseCompiler (TemplateParser parser)
{
compilerParameters = new CompilerParameters ();
this.parser = parser;
}
void Init ()
{
unit = new CodeCompileUnit ();
mainNS = new CodeNamespace ("ASP");
unit.Namespaces.Add (mainNS);
mainClass = new CodeTypeDeclaration (parser.ClassName);
mainClass.TypeAttributes = TypeAttributes.Public;
mainNS.Types.Add (mainClass);
mainClass.BaseTypes.Add (new CodeTypeReference (parser.BaseType.FullName));
mainClassExpr = new CodeTypeReferenceExpression ("ASP." + parser.ClassName);
foreach (object o in parser.Imports) {
if (o is string)
mainNS.Imports.Add (new CodeNamespaceImport ((string) o));
}
if (parser.Assemblies != null) {
foreach (object o in parser.Assemblies) {
if (o is string)
unit.ReferencedAssemblies.Add ((string) o);
}
}
AddInterfaces ();
CreateStaticFields ();
AddScripts ();
CreateConstructor (null, null);
}
void BuildTree ()
{
Init ();
CreateMethods ();
}
protected virtual void CreateStaticFields ()
{
CodeMemberField fld = new CodeMemberField (typeof (bool), "__intialized");
fld.Attributes = MemberAttributes.Private | MemberAttributes.Static;
fld.InitExpression = new CodePrimitiveExpression (false);
mainClass.Members.Add (fld);
}
protected virtual void CreateConstructor (CodeStatementCollection localVars, CodeStatementCollection trueStmt)
{
CodeConstructor ctor = new CodeConstructor ();
ctor.Attributes = MemberAttributes.Public;
mainClass.Members.Add (ctor);
if (localVars != null)
ctor.Statements.AddRange (localVars);
CodeTypeReferenceExpression r = new CodeTypeReferenceExpression (mainNS.Name + "." + mainClass.Name);
CodeFieldReferenceExpression intialized = new CodeFieldReferenceExpression (r, "__intialized");
CodeBinaryOperatorExpression bin = new CodeBinaryOperatorExpression (intialized,
CodeBinaryOperatorType.ValueEquality,
new CodePrimitiveExpression (false));
CodeAssignStatement assign = new CodeAssignStatement (intialized,
new CodePrimitiveExpression (true));
CodeConditionStatement cond = new CodeConditionStatement (bin, assign);
if (trueStmt != null)
cond.TrueStatements.AddRange (trueStmt);
ctor.Statements.Add (cond);
}
void AddScripts ()
{
if (parser.Scripts == null || parser.Scripts.Count == 0)
return;
foreach (object o in parser.Scripts) {
if (o is string)
mainClass.Members.Add (new CodeSnippetTypeMember ((string) o));
}
}
protected virtual void CreateMethods ()
{
}
protected virtual void AddInterfaces ()
{
if (parser.Interfaces == null)
return;
foreach (object o in parser.Interfaces) {
if (o is string)
mainClass.BaseTypes.Add (new CodeTypeReference ((string) o));
}
}
protected virtual void ProcessObjectTag (ObjectTagBuilder tag)
{
}
void CheckCompilerErrors (CompilerResults results)
{
if (results.NativeCompilerReturnValue == 0)
return;
StringWriter writer = new StringWriter();
provider.CreateGenerator().GenerateCodeFromCompileUnit (unit, writer, null);
StringBuilder sb = new StringBuilder ();
foreach (CompilerError error in results.Errors) {
sb.Append (error);
sb.Append ('\n');
}
throw new CompilationException (parser.InputFile, sb.ToString (), writer.ToString ());
}
public virtual Type GetCompiledType ()
{
//TODO: get the compiler and default options from system.web/compileroptions
provider = new CSharpCodeProvider ();
compiler = provider.CreateCompiler ();
BuildTree ();
CompilerResults results = CachingCompiler.Compile (this);
CheckCompilerErrors (results);
return results.CompiledAssembly.GetType (mainClassExpr.Type.BaseType, true);
}
internal CompilerParameters CompilerParameters {
get { return compilerParameters; }
}
internal CodeCompileUnit Unit {
get { return unit; }
}
internal ICodeCompiler Compiler {
get { return compiler; }
}
internal TemplateParser Parser {
get { return parser; }
}
}
}