| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- //
- // System.Web.Compilation.BaseCompiler
- //
- // Authors:
- // Gonzalo Paniagua Javier ([email protected])
- //
- // (c) Copyright 2002,2003 Ximian, Inc (http://www.ximian.com)
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using System.Collections;
- using System.Collections.Specialized;
- using System.Reflection;
- using System.Text;
- using System.Web.UI;
- using System.Web.Configuration;
- using System.IO;
- namespace System.Web.Compilation
- {
- abstract class BaseCompiler
- {
- #if NET_2_0
- static BindingFlags replaceableFlags = BindingFlags.Public | BindingFlags.NonPublic |
- BindingFlags.Instance;
- #endif
- TemplateParser parser;
- CodeDomProvider provider;
- ICodeCompiler compiler;
- CodeCompileUnit unit;
- CodeNamespace mainNS;
- CompilerParameters compilerParameters;
- #if NET_2_0
- bool isRebuilding = false;
- protected Hashtable partialNameOverride = new Hashtable();
- protected CodeTypeDeclaration partialClass;
- protected CodeTypeReferenceExpression partialClassExpr;
- #endif
- protected CodeTypeDeclaration mainClass;
- protected CodeTypeReferenceExpression mainClassExpr;
- protected static CodeThisReferenceExpression thisRef = new CodeThisReferenceExpression ();
- protected BaseCompiler (TemplateParser parser)
- {
- this.parser = parser;
- }
- void Init ()
- {
- unit = new CodeCompileUnit ();
- #if NET_2_0
- if (parser.IsPartial) {
- string partialns = null;
- string partialclasstype = parser.PartialClassName;
- int partialdot = partialclasstype.LastIndexOf ('.');
- if (partialdot != -1) {
- partialns = partialclasstype.Substring (0, partialdot);
- partialclasstype = partialclasstype.Substring (partialdot + 1);
- }
-
- CodeNamespace partialNS = new CodeNamespace (partialns);
- partialClass = new CodeTypeDeclaration (partialclasstype);
- partialClass.IsPartial = true;
- partialClassExpr = new CodeTypeReferenceExpression (parser.PartialClassName);
-
- unit.Namespaces.Add (partialNS);
- partialClass.TypeAttributes = TypeAttributes.Public;
- partialNS.Types.Add (partialClass);
- }
- #endif
- string mainclasstype = parser.ClassName;
- string mainns = "ASP";
- #if NET_2_0
- int maindot = mainclasstype.LastIndexOf ('.');
- if (maindot != -1) {
- mainns = mainclasstype.Substring (0, maindot);
- mainclasstype = mainclasstype.Substring (maindot + 1);
- }
- #endif
- mainNS = new CodeNamespace (mainns);
- mainClass = new CodeTypeDeclaration (mainclasstype);
- CodeTypeReference baseTypeRef;
- #if NET_2_0
- if (partialClass != null) {
- baseTypeRef = new CodeTypeReference (parser.PartialClassName);
- baseTypeRef.Options |= CodeTypeReferenceOptions.GlobalReference;
- } else {
- baseTypeRef = new CodeTypeReference (parser.BaseType.FullName);
- if (parser.BaseTypeIsGlobal)
- baseTypeRef.Options |= CodeTypeReferenceOptions.GlobalReference;
- }
- #else
- baseTypeRef = new CodeTypeReference (parser.BaseType.FullName);
- #endif
- mainClass.BaseTypes.Add (baseTypeRef);
- mainClassExpr = new CodeTypeReferenceExpression (mainns + "." + mainclasstype);
- unit.Namespaces.Add (mainNS);
- mainClass.TypeAttributes = TypeAttributes.Public;
- mainNS.Types.Add (mainClass);
- foreach (object o in parser.Imports) {
- if (o is string)
- mainNS.Imports.Add (new CodeNamespaceImport ((string) o));
- }
- // StringCollection.Contains has O(n) complexity, but
- // considering the number of comparisons we make on
- // average and the fact that using an intermediate array
- // would be even more costly, this is fine here.
- StringCollection refAsm = unit.ReferencedAssemblies;
- string asmName;
- if (parser.Assemblies != null) {
- foreach (object o in parser.Assemblies) {
- asmName = o as string;
- if (asmName != null && !refAsm.Contains (asmName))
- refAsm.Add (asmName);
- }
- }
- #if NET_2_0
- ArrayList al = WebConfigurationManager.ExtraAssemblies;
- if (al != null && al.Count > 0) {
- foreach (object o in al) {
- asmName = o as string;
- if (asmName != null && !refAsm.Contains (asmName))
- refAsm.Add (asmName);
- }
- }
- IList list = BuildManager.CodeAssemblies;
- if (list != null && list.Count > 0) {
- Assembly asm;
- foreach (object o in list) {
- asm = o as Assembly;
- if (o == null)
- continue;
- asmName = asm.Location;
- if (asmName != null && !refAsm.Contains (asmName))
- refAsm.Add (asmName);
- }
- }
- #endif
- // Late-bound generators specifics (as for MonoBASIC/VB.NET)
- unit.UserData["RequireVariableDeclaration"] = parser.ExplicitOn;
- unit.UserData["AllowLateBound"] = !parser.StrictOn;
-
- AddInterfaces ();
- AddClassAttributes ();
- CreateStaticFields ();
- AddApplicationAndSessionObjects ();
- AddScripts ();
- CreateMethods ();
- CreateConstructor (null, null);
- }
- protected virtual void CreateStaticFields ()
- {
- CodeMemberField fld = new CodeMemberField (typeof (bool), "__initialized");
- fld.Attributes = MemberAttributes.Private | MemberAttributes.Static;
- fld.InitExpression = new CodePrimitiveExpression (false);
- mainClass.Members.Add (fld);
- }
- #if NET_2_0
- void AssignAppRelativeVirtualPath (CodeConstructor ctor)
- {
- Type baseType = parser.CodeFileBaseClassType;
- if (baseType == null)
- baseType = parser.BaseType;
- if (baseType == null)
- return;
- if (!baseType.IsSubclassOf (typeof (System.Web.UI.TemplateControl)))
- return;
- string arvp = Path.Combine (parser.BaseVirtualDir, Path.GetFileName (parser.InputFile));
- if (VirtualPathUtility.IsAbsolute (arvp))
- arvp = "~" + arvp;
- CodeTypeReference baseTypeRef = new CodeTypeReference (baseType.FullName);
- if (parser.BaseTypeIsGlobal)
- baseTypeRef.Options |= CodeTypeReferenceOptions.GlobalReference;
-
- CodeExpression cast = new CodeCastExpression (baseTypeRef, new CodeThisReferenceExpression ());
- CodePropertyReferenceExpression arvpProp = new CodePropertyReferenceExpression (cast, "AppRelativeVirtualPath");
- CodeAssignStatement arvpAssign = new CodeAssignStatement ();
- arvpAssign.Left = arvpProp;
- arvpAssign.Right = new CodePrimitiveExpression (VirtualPathUtility.RemoveTrailingSlash (arvp));
- ctor.Statements.Add (arvpAssign);
- }
- #endif
-
- protected virtual void CreateConstructor (CodeStatementCollection localVars,
- CodeStatementCollection trueStmt)
- {
- CodeConstructor ctor = new CodeConstructor ();
- ctor.Attributes = MemberAttributes.Public;
- mainClass.Members.Add (ctor);
- #if NET_2_0
- AssignAppRelativeVirtualPath (ctor);
- #endif
- if (localVars != null)
- ctor.Statements.AddRange (localVars);
- CodeTypeReferenceExpression r;
- #if NET_2_0
- if (parser.IsPartial)
- r = new CodeTypeReferenceExpression (mainClass.Name);
- else
- #endif
- r = new CodeTypeReferenceExpression (mainNS.Name + "." + mainClass.Name);
- CodeFieldReferenceExpression initialized;
- initialized = new CodeFieldReferenceExpression (r, "__initialized");
-
- CodeBinaryOperatorExpression bin;
- bin = new CodeBinaryOperatorExpression (initialized,
- CodeBinaryOperatorType.ValueEquality,
- new CodePrimitiveExpression (false));
- CodeAssignStatement assign = new CodeAssignStatement (initialized,
- 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 internal virtual void CreateMethods ()
- {
- }
- #if NET_2_0
- void InternalCreatePageProperty (string retType, string name, string contextProperty)
- {
- CodeMemberProperty property = new CodeMemberProperty ();
- property.Name = name;
- property.Type = new CodeTypeReference (retType);
- property.Attributes = MemberAttributes.Family | MemberAttributes.Final;
- CodeMethodReturnStatement ret = new CodeMethodReturnStatement ();
- CodeCastExpression cast = new CodeCastExpression ();
- ret.Expression = cast;
-
- CodePropertyReferenceExpression refexp = new CodePropertyReferenceExpression ();
- refexp.TargetObject = new CodePropertyReferenceExpression (new CodeThisReferenceExpression (), "Context");
- refexp.PropertyName = contextProperty;
-
- cast.TargetType = new CodeTypeReference (retType);
- cast.Expression = refexp;
-
- property.GetStatements.Add (ret);
- if (partialClass == null)
- mainClass.Members.Add (property);
- else
- partialClass.Members.Add (property);
- }
-
- protected void CreateProfileProperty ()
- {
- string retType;
- if (AppCodeCompiler.HaveCustomProfile (WebConfigurationManager.GetSection ("system.web/profile") as ProfileSection))
- retType = "ProfileCommon";
- else
- retType = "System.Web.Profile.DefaultProfile";
- InternalCreatePageProperty (retType, "Profile", "Profile");
- }
- #endif
-
- 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 AddClassAttributes ()
- {
- }
-
- protected virtual void AddApplicationAndSessionObjects ()
- {
- }
- /* Utility methods for <object> stuff */
- protected void CreateApplicationOrSessionPropertyForObject (Type type,
- string propName,
- bool isApplication,
- bool isPublic)
- {
- /* if isApplication this generates (the 'cachedapp' field is created earlier):
- private MyNS.MyClass app {
- get {
- if ((this.cachedapp == null)) {
- this.cachedapp = ((MyNS.MyClass)
- (this.Application.StaticObjects.GetObject("app")));
- }
- return this.cachedapp;
- }
- }
- else, this is for Session:
- private MyNS.MyClass ses {
- get {
- return ((MyNS.MyClass) (this.Session.StaticObjects.GetObject("ses")));
- }
- }
- */
- CodeExpression result = null;
- CodeMemberProperty prop = new CodeMemberProperty ();
- prop.Type = new CodeTypeReference (type);
- prop.Name = propName;
- if (isPublic)
- prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
- else
- prop.Attributes = MemberAttributes.Private | MemberAttributes.Final;
- CodePropertyReferenceExpression p1;
- if (isApplication)
- p1 = new CodePropertyReferenceExpression (thisRef, "Application");
- else
- p1 = new CodePropertyReferenceExpression (thisRef, "Session");
- CodePropertyReferenceExpression p2;
- p2 = new CodePropertyReferenceExpression (p1, "StaticObjects");
- CodeMethodReferenceExpression getobject;
- getobject = new CodeMethodReferenceExpression (p2, "GetObject");
- CodeMethodInvokeExpression invoker;
- invoker = new CodeMethodInvokeExpression (getobject,
- new CodePrimitiveExpression (propName));
- CodeCastExpression cast = new CodeCastExpression (prop.Type, invoker);
- if (isApplication) {
- CodeFieldReferenceExpression field;
- field = new CodeFieldReferenceExpression (thisRef, "cached" + propName);
- CodeConditionStatement stmt = new CodeConditionStatement();
- stmt.Condition = new CodeBinaryOperatorExpression (field,
- CodeBinaryOperatorType.IdentityEquality,
- new CodePrimitiveExpression (null));
- CodeAssignStatement assign = new CodeAssignStatement ();
- assign.Left = field;
- assign.Right = cast;
- stmt.TrueStatements.Add (assign);
- prop.GetStatements.Add (stmt);
- result = field;
- } else {
- result = cast;
- }
-
- prop.GetStatements.Add (new CodeMethodReturnStatement (result));
- mainClass.Members.Add (prop);
- }
- protected string CreateFieldForObject (Type type, string name)
- {
- string fieldName = "cached" + name;
- CodeMemberField f = new CodeMemberField (type, fieldName);
- f.Attributes = MemberAttributes.Private;
- mainClass.Members.Add (f);
- return fieldName;
- }
- protected void CreatePropertyForObject (Type type, string propName, string fieldName, bool isPublic)
- {
- CodeFieldReferenceExpression field = new CodeFieldReferenceExpression (thisRef, fieldName);
- CodeMemberProperty prop = new CodeMemberProperty ();
- prop.Type = new CodeTypeReference (type);
- prop.Name = propName;
- if (isPublic)
- prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
- else
- prop.Attributes = MemberAttributes.Private | MemberAttributes.Final;
- CodeConditionStatement stmt = new CodeConditionStatement();
- stmt.Condition = new CodeBinaryOperatorExpression (field,
- CodeBinaryOperatorType.IdentityEquality,
- new CodePrimitiveExpression (null));
- CodeObjectCreateExpression create = new CodeObjectCreateExpression (prop.Type);
- stmt.TrueStatements.Add (new CodeAssignStatement (field, create));
- prop.GetStatements.Add (stmt);
- prop.GetStatements.Add (new CodeMethodReturnStatement (field));
- mainClass.Members.Add (prop);
- }
- /******/
- void CheckCompilerErrors (CompilerResults results)
- {
- if (results.NativeCompilerReturnValue == 0)
- return;
- string fileText = null;
- CompilerErrorCollection errors = results.Errors;
- CompilerError ce = (errors != null && errors.Count > 0) ? errors [0] : null;
- string inFile = (ce != null) ? ce.FileName : null;
-
- if (inFile != null && File.Exists (inFile)) {
- using (StreamReader sr = File.OpenText (inFile)) {
- fileText = sr.ReadToEnd ();
- }
- } else {
- StringWriter writer = new StringWriter();
- provider.CreateGenerator().GenerateCodeFromCompileUnit (unit, writer, null);
- fileText = writer.ToString ();
- }
- throw new CompilationException (parser.InputFile, errors, fileText);
- }
- protected string DynamicDir ()
- {
- return AppDomain.CurrentDomain.SetupInformation.DynamicBase;
- }
- internal static CodeDomProvider CreateProvider (string lang, out string compilerOptions, out int warningLevel, out string tempdir)
- {
- return CreateProvider (HttpContext.Current, lang, out compilerOptions, out warningLevel, out tempdir);
- }
-
- internal static CodeDomProvider CreateProvider (HttpContext context, string lang, out string compilerOptions, out int warningLevel, out string tempdir)
- {
- CodeDomProvider ret = null;
-
- #if NET_2_0
- CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
- Compiler comp = config.Compilers[lang];
-
- if (comp == null) {
- CompilerInfo info = CodeDomProvider.GetCompilerInfo (lang);
- if (info != null && info.IsCodeDomProviderTypeValid) {
- ret = info.CreateProvider ();
- CompilerParameters cp = info.CreateDefaultCompilerParameters ();
- compilerOptions = cp.CompilerOptions;
- warningLevel = cp.WarningLevel;
- } else {
- compilerOptions = String.Empty;
- warningLevel = 2;
- }
- } else {
- Type t = HttpApplication.LoadType (comp.Type, true);
- ret = Activator.CreateInstance (t) as CodeDomProvider;
- compilerOptions = comp.CompilerOptions;
- warningLevel = comp.WarningLevel;
- }
- #else
- CompilationConfiguration config;
- config = CompilationConfiguration.GetInstance (context);
- ret = config.GetProvider (lang);
- compilerOptions = config.GetCompilerOptions (lang);
- warningLevel = config.GetWarningLevel (lang);
- #endif
- tempdir = config.TempDirectory;
- return ret;
- }
-
- [MonoTODO ("find out how to extract the warningLevel and compilerOptions in the <system.codedom> case")]
- public virtual Type GetCompiledType ()
- {
- Type type = CachingCompiler.GetTypeFromCache (parser.InputFile);
- if (type != null)
- return type;
- Init ();
- string lang = parser.Language;
- string tempdir;
- string compilerOptions;
- int warningLevel;
- Provider = CreateProvider (parser.Context, lang, out compilerOptions, out warningLevel, out tempdir);
- if (Provider == null)
- throw new HttpException ("Configuration error. Language not supported: " +
- lang, 500);
- #if !NET_2_0
- compiler = provider.CreateCompiler ();
- #endif
- CompilerParameters parameters = CompilerParameters;
- parameters.IncludeDebugInformation = parser.Debug;
- parameters.CompilerOptions = compilerOptions + " " + parser.CompilerOptions;
- parameters.WarningLevel = warningLevel;
-
- bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
- if (tempdir == null || tempdir == "")
- tempdir = DynamicDir ();
-
- TempFileCollection tempcoll = new TempFileCollection (tempdir, keepFiles);
- parameters.TempFiles = tempcoll;
- string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
- parameters.OutputAssembly = Path.Combine (DynamicDir (), dllfilename);
- CompilerResults results = CachingCompiler.Compile (this);
- CheckCompilerErrors (results);
- Assembly assembly = results.CompiledAssembly;
- if (assembly == null) {
- if (!File.Exists (parameters.OutputAssembly)) {
- results.TempFiles.Delete ();
- throw new CompilationException (parser.InputFile, results.Errors,
- "No assembly returned after compilation!?");
- }
- assembly = Assembly.LoadFrom (parameters.OutputAssembly);
- }
- results.TempFiles.Delete ();
- Type mainClassType = assembly.GetType (mainClassExpr.Type.BaseType, true);
- #if NET_2_0
- if (parser.IsPartial) {
- // With the partial classes, we need to make sure we
- // don't have any methods that should have not been
- // created (because they are accessible from the base
- // types). We cannot do this normally because the
- // codebehind file is actually a partial class and we
- // have no way of identifying the partial class' base
- // type until now.
- if (!isRebuilding && CheckPartialBaseType (mainClassType)) {
- isRebuilding = true;
- parser.RootBuilder.ResetState ();
- return GetCompiledType ();
- }
- }
- #endif
- return mainClassType;
- }
- #if NET_2_0
- internal bool IsRebuildingPartial
- {
- get { return isRebuilding; }
- }
- internal bool CheckPartialBaseType (Type type)
- {
- // Get the base type. If we don't have any (bad thing), we
- // don't need to replace ourselves. Also check for the
- // core file, since that won't have any either.
- Type baseType = type.BaseType;
- if (baseType == null || baseType == typeof(System.Web.UI.Page))
- return false;
- bool rebuild = false;
- if (CheckPartialBaseFields (type, baseType))
- rebuild = true;
- if (CheckPartialBaseProperties (type, baseType))
- rebuild = true;
- return rebuild;
- }
- internal bool CheckPartialBaseFields (Type type, Type baseType)
- {
- bool rebuild = false;
- foreach (FieldInfo baseInfo in baseType.GetFields (replaceableFlags)) {
- if (baseInfo.IsPrivate)
- continue;
- FieldInfo typeInfo = type.GetField (baseInfo.Name, replaceableFlags);
- if (typeInfo != null && typeInfo.DeclaringType == type) {
- partialNameOverride [typeInfo.Name] = true;
- rebuild = true;
- }
- }
- return rebuild;
- }
- internal bool CheckPartialBaseProperties (Type type, Type baseType)
- {
- bool rebuild = false;
- foreach (PropertyInfo baseInfo in baseType.GetProperties ()) {
- PropertyInfo typeInfo = type.GetProperty (baseInfo.Name);
- if (typeInfo != null && typeInfo.DeclaringType == type) {
- partialNameOverride [typeInfo.Name] = true;
- rebuild = true;
- }
- }
- return rebuild;
- }
- #endif
- internal CodeDomProvider Provider {
- get { return provider; }
- set { provider = value; }
- }
- internal ICodeCompiler Compiler {
- get { return compiler; }
- set { compiler = value; }
- }
- internal CompilerParameters CompilerParameters {
- get {
- if (compilerParameters == null)
- compilerParameters = new CompilerParameters ();
-
- return compilerParameters;
- }
-
- set { compilerParameters = value; }
- }
- internal CodeCompileUnit CompileUnit {
- get { return unit; }
- }
- internal TemplateParser Parser {
- get { return parser; }
- }
- }
- }
|