Files
mono/mcs/class/System.Web/System.Web.Compilation/BaseCompiler.cs
T
Gonzalo Paniagua Javier a3e7d18173 2003-05-01 Gonzalo Paniagua Javier <[email protected]>
* System.Web/HtmlizedException.cs: added more virtual methods.

	* System.Web/HttpException.cs:  some work on the output when there's
	a source file present.

	* System.Web.Compilation/AspGenerator.cs: copy the location before
	setting the value for the control builders.

	* System.Web.Compilation/BaseCompiler.cs: changed parameters for
	CompilationException.

	* System.Web.Compilation/CompilationException.cs: it takes now line
	numbers and error descriptions from the CompilerErrorCollection.

	* System.Web.Compilation/Location.cs: used when a copy of an ILocation
	is needed.

	* System.Web.Compilation/ParseException.cs: implemented new methods
	to provide line numbers and souce file.

	* System.Web.Compilation/TemplateControlCompiler.cs: throw a
	ParseException where appropiate.

	* System.Web.UI/BaseParser.cs: Location property is now here. Added a
	couple of convenience methods to throw a ParseException.

	* System.Web.UI/TemplateParser.cs:
	* System.Web.UI/TemplateControlParser.cs:
	* System.Web.UI/PageParser.cs: throw ParseException where appropiate.

svn path=/trunk/mcs/; revision=14191
2003-05-01 19:34:32 +00:00

180 lines
4.7 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);
throw new CompilationException (parser.InputFile, results.Errors, 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; }
}
}
}