2 // System.Web.Compilation.BaseCompiler
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (c) Copyright 2002,2003 Ximian, Inc (http://www.ximian.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.CodeDom.Compiler;
34 using System.Collections;
35 using System.Reflection;
38 using System.Web.Configuration;
41 namespace System.Web.Compilation
43 abstract class BaseCompiler
46 static BindingFlags replaceableFlags = BindingFlags.Public | BindingFlags.NonPublic |
47 BindingFlags.Instance;
50 TemplateParser parser;
51 CodeDomProvider provider;
52 ICodeCompiler compiler;
55 CompilerParameters compilerParameters;
57 bool isRebuilding = false;
58 protected Hashtable partialNameOverride = new Hashtable();
60 protected CodeTypeDeclaration mainClass;
61 protected CodeTypeReferenceExpression mainClassExpr;
62 protected static CodeThisReferenceExpression thisRef = new CodeThisReferenceExpression ();
64 protected BaseCompiler (TemplateParser parser)
66 compilerParameters = new CompilerParameters ();
72 unit = new CodeCompileUnit ();
74 if (parser.IsPartial) {
76 string classtype = parser.PartialClassName;
78 if (classtype.Contains (".")) {
79 int dot = classtype.LastIndexOf (".");
80 ns = classtype.Substring (0, dot);
81 classtype = classtype.Substring (dot + 1);
84 mainNS = new CodeNamespace (ns);
85 mainClass = new CodeTypeDeclaration (classtype);
86 mainClass.IsPartial = true;
87 mainClassExpr = new CodeTypeReferenceExpression (parser.PartialClassName);
90 mainNS = new CodeNamespace ("ASP");
91 mainClass = new CodeTypeDeclaration (parser.ClassName);
92 CodeTypeReference baseTypeRef = new CodeTypeReference (parser.BaseType.FullName);
93 if (parser.BaseTypeIsGlobal)
94 baseTypeRef.Options |= CodeTypeReferenceOptions.GlobalReference;
95 mainClass.BaseTypes.Add (baseTypeRef);
96 mainClassExpr = new CodeTypeReferenceExpression ("ASP." + parser.ClassName);
100 unit.Namespaces.Add (mainNS);
101 mainClass.TypeAttributes = TypeAttributes.Public;
102 mainNS.Types.Add (mainClass);
104 foreach (object o in parser.Imports) {
106 mainNS.Imports.Add (new CodeNamespaceImport ((string) o));
109 if (parser.Assemblies != null) {
110 foreach (object o in parser.Assemblies) {
112 unit.ReferencedAssemblies.Add ((string) o);
117 ArrayList al = WebConfigurationManager.ExtraAssemblies;
118 if (al != null && al.Count > 0) {
119 foreach (object o in al)
121 unit.ReferencedAssemblies.Add ((string) o);
124 IList list = BuildManager.CodeAssemblies;
125 if (list != null && list.Count > 0) {
126 foreach (object o in list)
128 unit.ReferencedAssemblies.Add ((string) o);
131 // Late-bound generators specifics (as for MonoBASIC/VB.NET)
132 unit.UserData["RequireVariableDeclaration"] = parser.ExplicitOn;
133 unit.UserData["AllowLateBound"] = !parser.StrictOn;
136 AddClassAttributes ();
137 CreateStaticFields ();
138 AddApplicationAndSessionObjects ();
141 CreateConstructor (null, null);
145 internal CodeDomProvider Provider {
146 get { return provider; }
149 internal CodeCompileUnit CompileUnit {
153 protected virtual void CreateStaticFields ()
155 CodeMemberField fld = new CodeMemberField (typeof (bool), "__initialized");
156 fld.Attributes = MemberAttributes.Private | MemberAttributes.Static;
157 fld.InitExpression = new CodePrimitiveExpression (false);
158 mainClass.Members.Add (fld);
161 protected virtual void CreateConstructor (CodeStatementCollection localVars,
162 CodeStatementCollection trueStmt)
164 CodeConstructor ctor = new CodeConstructor ();
165 ctor.Attributes = MemberAttributes.Public;
166 mainClass.Members.Add (ctor);
168 if (localVars != null)
169 ctor.Statements.AddRange (localVars);
171 CodeTypeReferenceExpression r;
173 if (parser.IsPartial)
174 r = new CodeTypeReferenceExpression (mainClass.Name);
177 r = new CodeTypeReferenceExpression (mainNS.Name + "." + mainClass.Name);
178 CodeFieldReferenceExpression initialized;
179 initialized = new CodeFieldReferenceExpression (r, "__initialized");
181 CodeBinaryOperatorExpression bin;
182 bin = new CodeBinaryOperatorExpression (initialized,
183 CodeBinaryOperatorType.ValueEquality,
184 new CodePrimitiveExpression (false));
186 CodeAssignStatement assign = new CodeAssignStatement (initialized,
187 new CodePrimitiveExpression (true));
189 CodeConditionStatement cond = new CodeConditionStatement (bin, assign);
190 if (trueStmt != null)
191 cond.TrueStatements.AddRange (trueStmt);
193 ctor.Statements.Add (cond);
198 if (parser.Scripts == null || parser.Scripts.Count == 0)
201 foreach (object o in parser.Scripts) {
203 mainClass.Members.Add (new CodeSnippetTypeMember ((string) o));
207 protected internal virtual void CreateMethods ()
211 protected virtual void AddInterfaces ()
213 if (parser.Interfaces == null)
216 foreach (object o in parser.Interfaces) {
218 mainClass.BaseTypes.Add (new CodeTypeReference ((string) o));
222 protected virtual void AddClassAttributes ()
226 protected virtual void AddApplicationAndSessionObjects ()
230 /* Utility methods for <object> stuff */
231 protected void CreateApplicationOrSessionPropertyForObject (Type type,
236 /* if isApplication this generates (the 'cachedapp' field is created earlier):
237 private MyNS.MyClass app {
239 if ((this.cachedapp == null)) {
240 this.cachedapp = ((MyNS.MyClass)
241 (this.Application.StaticObjects.GetObject("app")));
243 return this.cachedapp;
247 else, this is for Session:
248 private MyNS.MyClass ses {
250 return ((MyNS.MyClass) (this.Session.StaticObjects.GetObject("ses")));
256 CodeExpression result = null;
258 CodeMemberProperty prop = new CodeMemberProperty ();
259 prop.Type = new CodeTypeReference (type);
260 prop.Name = propName;
262 prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
264 prop.Attributes = MemberAttributes.Private | MemberAttributes.Final;
266 CodePropertyReferenceExpression p1;
268 p1 = new CodePropertyReferenceExpression (thisRef, "Application");
270 p1 = new CodePropertyReferenceExpression (thisRef, "Session");
272 CodePropertyReferenceExpression p2;
273 p2 = new CodePropertyReferenceExpression (p1, "StaticObjects");
275 CodeMethodReferenceExpression getobject;
276 getobject = new CodeMethodReferenceExpression (p2, "GetObject");
278 CodeMethodInvokeExpression invoker;
279 invoker = new CodeMethodInvokeExpression (getobject,
280 new CodePrimitiveExpression (propName));
282 CodeCastExpression cast = new CodeCastExpression (prop.Type, invoker);
285 CodeFieldReferenceExpression field;
286 field = new CodeFieldReferenceExpression (thisRef, "cached" + propName);
288 CodeConditionStatement stmt = new CodeConditionStatement();
289 stmt.Condition = new CodeBinaryOperatorExpression (field,
290 CodeBinaryOperatorType.IdentityEquality,
291 new CodePrimitiveExpression (null));
293 CodeAssignStatement assign = new CodeAssignStatement ();
296 stmt.TrueStatements.Add (assign);
297 prop.GetStatements.Add (stmt);
303 prop.GetStatements.Add (new CodeMethodReturnStatement (result));
304 mainClass.Members.Add (prop);
307 protected string CreateFieldForObject (Type type, string name)
309 string fieldName = "cached" + name;
310 CodeMemberField f = new CodeMemberField (type, fieldName);
311 f.Attributes = MemberAttributes.Private;
312 mainClass.Members.Add (f);
316 protected void CreatePropertyForObject (Type type, string propName, string fieldName, bool isPublic)
318 CodeFieldReferenceExpression field = new CodeFieldReferenceExpression (thisRef, fieldName);
319 CodeMemberProperty prop = new CodeMemberProperty ();
320 prop.Type = new CodeTypeReference (type);
321 prop.Name = propName;
323 prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
325 prop.Attributes = MemberAttributes.Private | MemberAttributes.Final;
327 CodeConditionStatement stmt = new CodeConditionStatement();
328 stmt.Condition = new CodeBinaryOperatorExpression (field,
329 CodeBinaryOperatorType.IdentityEquality,
330 new CodePrimitiveExpression (null));
332 CodeObjectCreateExpression create = new CodeObjectCreateExpression (prop.Type);
333 stmt.TrueStatements.Add (new CodeAssignStatement (field, create));
334 prop.GetStatements.Add (stmt);
335 prop.GetStatements.Add (new CodeMethodReturnStatement (field));
337 mainClass.Members.Add (prop);
341 void CheckCompilerErrors (CompilerResults results)
343 if (results.NativeCompilerReturnValue == 0)
346 StringWriter writer = new StringWriter();
347 provider.CreateGenerator().GenerateCodeFromCompileUnit (unit, writer, null);
348 throw new CompilationException (parser.InputFile, results.Errors, writer.ToString ());
351 protected string DynamicDir ()
353 return AppDomain.CurrentDomain.SetupInformation.DynamicBase;
356 [MonoTODO ("find out how to extract the warningLevel and compilerOptions in the <system.codedom> case")]
357 public virtual Type GetCompiledType ()
359 Type type = CachingCompiler.GetTypeFromCache (parser.InputFile);
364 string lang = parser.Language;
366 CompilationSection config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
367 Compiler comp = config.Compilers[lang];
369 string compilerOptions = "";
370 int warningLevel = 0;
373 CompilerInfo info = CodeDomProvider.GetCompilerInfo (lang);
374 if (info != null && info.IsCodeDomProviderTypeValid)
375 provider = info.CreateProvider ();
377 // XXX there's no way to get
378 // warningLevel or compilerOptions out
379 // of the provider.. they're in the
380 // configuration section, though.
383 Type t = Type.GetType (comp.Type, true);
384 provider = Activator.CreateInstance (t) as CodeDomProvider;
386 compilerOptions = comp.CompilerOptions;
387 warningLevel = comp.WarningLevel;
391 CompilationConfiguration config;
393 config = CompilationConfiguration.GetInstance (parser.Context);
394 provider = config.GetProvider (lang);
396 string compilerOptions = config.GetCompilerOptions (lang);
397 int warningLevel = config.GetWarningLevel (lang);
399 if (provider == null)
400 throw new HttpException ("Configuration error. Language not supported: " +
403 compiler = provider.CreateCompiler ();
405 compilerParameters.IncludeDebugInformation = parser.Debug;
406 compilerParameters.CompilerOptions = compilerOptions + " " + parser.CompilerOptions;
408 compilerParameters.WarningLevel = warningLevel;
409 bool keepFiles = (Environment.GetEnvironmentVariable ("MONO_ASPNET_NODELETE") != null);
411 string tempdir = config.TempDirectory;
412 if (tempdir == null || tempdir == "")
413 tempdir = DynamicDir ();
415 TempFileCollection tempcoll = new TempFileCollection (tempdir, keepFiles);
416 compilerParameters.TempFiles = tempcoll;
417 string dllfilename = Path.GetFileName (tempcoll.AddExtension ("dll", true));
418 compilerParameters.OutputAssembly = Path.Combine (DynamicDir (), dllfilename);
420 CompilerResults results = CachingCompiler.Compile (this);
421 CheckCompilerErrors (results);
422 Assembly assembly = results.CompiledAssembly;
423 if (assembly == null) {
424 if (!File.Exists (compilerParameters.OutputAssembly)) {
425 results.TempFiles.Delete ();
426 throw new CompilationException (parser.InputFile, results.Errors,
427 "No assembly returned after compilation!?");
430 assembly = Assembly.LoadFrom (compilerParameters.OutputAssembly);
433 results.TempFiles.Delete ();
434 Type mainClassType = assembly.GetType (mainClassExpr.Type.BaseType, true);
437 if (parser.IsPartial) {
438 // With the partial classes, we need to make sure we
439 // don't have any methods that should have not been
440 // created (because they are accessible from the base
441 // types). We cannot do this normally because the
442 // codebehind file is actually a partial class and we
443 // have no way of identifying the partial class' base
445 if (!isRebuilding && CheckPartialBaseType (mainClassType)) {
447 parser.RootBuilder.ResetState ();
448 return GetCompiledType ();
453 return mainClassType;
457 internal bool IsRebuildingPartial
459 get { return isRebuilding; }
462 internal bool CheckPartialBaseType (Type type)
464 // Get the base type. If we don't have any (bad thing), we
465 // don't need to replace ourselves. Also check for the
466 // core file, since that won't have any either.
467 Type baseType = type.BaseType;
468 if (baseType == null || baseType == typeof(System.Web.UI.Page))
471 bool rebuild = false;
473 if (CheckPartialBaseFields (type, baseType))
476 if (CheckPartialBaseProperties (type, baseType))
482 internal bool CheckPartialBaseFields (Type type, Type baseType)
484 bool rebuild = false;
486 foreach (FieldInfo baseInfo in baseType.GetFields (replaceableFlags)) {
487 if (baseInfo.IsPrivate)
490 FieldInfo typeInfo = type.GetField (baseInfo.Name, replaceableFlags);
492 if (typeInfo != null && typeInfo.DeclaringType == type) {
493 partialNameOverride [typeInfo.Name] = true;
501 internal bool CheckPartialBaseProperties (Type type, Type baseType)
503 bool rebuild = false;
505 foreach (PropertyInfo baseInfo in baseType.GetProperties ()) {
506 PropertyInfo typeInfo = type.GetProperty (baseInfo.Name);
508 if (typeInfo != null && typeInfo.DeclaringType == type) {
509 partialNameOverride [typeInfo.Name] = true;
518 internal CompilerParameters CompilerParameters {
519 get { return compilerParameters; }
522 internal CodeCompileUnit Unit {
526 internal virtual ICodeCompiler Compiler {
527 get { return compiler; }
530 internal TemplateParser Parser {
531 get { return parser; }