BaseCompiler.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // System.Web.Compilation.BaseCompiler
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (c) Copyright 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. using System;
  10. using System.CodeDom;
  11. using System.CodeDom.Compiler;
  12. using System.Collections;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Web.UI;
  16. using System.Web.Configuration;
  17. using System.IO;
  18. namespace System.Web.Compilation
  19. {
  20. abstract class BaseCompiler
  21. {
  22. static string dynamicBase = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
  23. TemplateParser parser;
  24. CodeDomProvider provider;
  25. ICodeCompiler compiler;
  26. CodeCompileUnit unit;
  27. CodeNamespace mainNS;
  28. CompilerParameters compilerParameters;
  29. protected CodeTypeDeclaration mainClass;
  30. protected CodeTypeReferenceExpression mainClassExpr;
  31. protected static CodeThisReferenceExpression thisRef = new CodeThisReferenceExpression ();
  32. protected BaseCompiler (TemplateParser parser)
  33. {
  34. compilerParameters = new CompilerParameters ();
  35. this.parser = parser;
  36. }
  37. void Init ()
  38. {
  39. unit = new CodeCompileUnit ();
  40. mainNS = new CodeNamespace ("ASP");
  41. unit.Namespaces.Add (mainNS);
  42. mainClass = new CodeTypeDeclaration (parser.ClassName);
  43. mainClass.TypeAttributes = TypeAttributes.Public;
  44. mainNS.Types.Add (mainClass);
  45. mainClass.BaseTypes.Add (new CodeTypeReference (parser.BaseType.FullName));
  46. mainClassExpr = new CodeTypeReferenceExpression ("ASP." + parser.ClassName);
  47. foreach (object o in parser.Imports) {
  48. if (o is string)
  49. mainNS.Imports.Add (new CodeNamespaceImport ((string) o));
  50. }
  51. if (parser.Assemblies != null) {
  52. foreach (object o in parser.Assemblies) {
  53. if (o is string)
  54. unit.ReferencedAssemblies.Add ((string) o);
  55. }
  56. }
  57. AddInterfaces ();
  58. AddClassAttributes ();
  59. CreateStaticFields ();
  60. AddApplicationAndSessionObjects ();
  61. AddScripts ();
  62. CreateConstructor (null, null);
  63. }
  64. protected virtual void CreateStaticFields ()
  65. {
  66. CodeMemberField fld = new CodeMemberField (typeof (bool), "__intialized");
  67. fld.Attributes = MemberAttributes.Private | MemberAttributes.Static;
  68. fld.InitExpression = new CodePrimitiveExpression (false);
  69. mainClass.Members.Add (fld);
  70. }
  71. protected virtual void CreateConstructor (CodeStatementCollection localVars,
  72. CodeStatementCollection trueStmt)
  73. {
  74. CodeConstructor ctor = new CodeConstructor ();
  75. ctor.Attributes = MemberAttributes.Public;
  76. mainClass.Members.Add (ctor);
  77. if (localVars != null)
  78. ctor.Statements.AddRange (localVars);
  79. CodeTypeReferenceExpression r;
  80. r = new CodeTypeReferenceExpression (mainNS.Name + "." + mainClass.Name);
  81. CodeFieldReferenceExpression intialized;
  82. intialized = new CodeFieldReferenceExpression (r, "__intialized");
  83. CodeBinaryOperatorExpression bin;
  84. bin = new CodeBinaryOperatorExpression (intialized,
  85. CodeBinaryOperatorType.ValueEquality,
  86. new CodePrimitiveExpression (false));
  87. CodeAssignStatement assign = new CodeAssignStatement (intialized,
  88. new CodePrimitiveExpression (true));
  89. CodeConditionStatement cond = new CodeConditionStatement (bin, assign);
  90. if (trueStmt != null)
  91. cond.TrueStatements.AddRange (trueStmt);
  92. ctor.Statements.Add (cond);
  93. }
  94. void AddScripts ()
  95. {
  96. if (parser.Scripts == null || parser.Scripts.Count == 0)
  97. return;
  98. foreach (object o in parser.Scripts) {
  99. if (o is string)
  100. mainClass.Members.Add (new CodeSnippetTypeMember ((string) o));
  101. }
  102. }
  103. protected virtual void CreateMethods ()
  104. {
  105. }
  106. protected virtual void AddInterfaces ()
  107. {
  108. if (parser.Interfaces == null)
  109. return;
  110. foreach (object o in parser.Interfaces) {
  111. if (o is string)
  112. mainClass.BaseTypes.Add (new CodeTypeReference ((string) o));
  113. }
  114. }
  115. protected virtual void AddClassAttributes ()
  116. {
  117. }
  118. protected virtual void AddApplicationAndSessionObjects ()
  119. {
  120. }
  121. /* Utility methods for <object> stuff */
  122. protected void CreateApplicationOrSessionPropertyForObject (Type type,
  123. string propName,
  124. bool isApplication,
  125. bool isPublic)
  126. {
  127. /* if isApplication this generates (the 'cachedapp' field is created earlier):
  128. private MyNS.MyClass app {
  129. get {
  130. if ((this.cachedapp == null)) {
  131. this.cachedapp = ((MyNS.MyClass)
  132. (this.Application.StaticObjects.GetObject("app")));
  133. }
  134. return this.cachedapp;
  135. }
  136. }
  137. else, this is for Session:
  138. private MyNS.MyClass ses {
  139. get {
  140. return ((MyNS.MyClass) (this.Session.StaticObjects.GetObject("ses")));
  141. }
  142. }
  143. */
  144. CodeExpression result = null;
  145. CodeMemberProperty prop = new CodeMemberProperty ();
  146. prop.Type = new CodeTypeReference (type);
  147. prop.Name = propName;
  148. if (isPublic)
  149. prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  150. else
  151. prop.Attributes = MemberAttributes.Private | MemberAttributes.Final;
  152. CodePropertyReferenceExpression p1;
  153. if (isApplication)
  154. p1 = new CodePropertyReferenceExpression (thisRef, "Application");
  155. else
  156. p1 = new CodePropertyReferenceExpression (thisRef, "Session");
  157. CodePropertyReferenceExpression p2;
  158. p2 = new CodePropertyReferenceExpression (p1, "StaticObjects");
  159. CodeMethodReferenceExpression getobject;
  160. getobject = new CodeMethodReferenceExpression (p2, "GetObject");
  161. CodeMethodInvokeExpression invoker;
  162. invoker = new CodeMethodInvokeExpression (getobject,
  163. new CodePrimitiveExpression (propName));
  164. CodeCastExpression cast = new CodeCastExpression (prop.Type, invoker);
  165. if (isApplication) {
  166. CodeFieldReferenceExpression field;
  167. field = new CodeFieldReferenceExpression (thisRef, "cached" + propName);
  168. CodeConditionStatement stmt = new CodeConditionStatement();
  169. stmt.Condition = new CodeBinaryOperatorExpression (field,
  170. CodeBinaryOperatorType.IdentityEquality,
  171. new CodePrimitiveExpression (null));
  172. CodeAssignStatement assign = new CodeAssignStatement ();
  173. assign.Left = field;
  174. assign.Right = cast;
  175. stmt.TrueStatements.Add (assign);
  176. prop.GetStatements.Add (stmt);
  177. result = field;
  178. } else {
  179. result = cast;
  180. }
  181. prop.GetStatements.Add (new CodeMethodReturnStatement (result));
  182. mainClass.Members.Add (prop);
  183. }
  184. protected string CreateFieldForObject (Type type, string name)
  185. {
  186. string fieldName = "cached" + name;
  187. CodeMemberField f = new CodeMemberField (type, fieldName);
  188. f.Attributes = MemberAttributes.Private;
  189. mainClass.Members.Add (f);
  190. return fieldName;
  191. }
  192. protected void CreatePropertyForObject (Type type, string propName, string fieldName, bool isPublic)
  193. {
  194. CodeFieldReferenceExpression field = new CodeFieldReferenceExpression (thisRef, fieldName);
  195. CodeMemberProperty prop = new CodeMemberProperty ();
  196. prop.Type = new CodeTypeReference (type);
  197. prop.Name = propName;
  198. if (isPublic)
  199. prop.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  200. else
  201. prop.Attributes = MemberAttributes.Private | MemberAttributes.Final;
  202. CodeConditionStatement stmt = new CodeConditionStatement();
  203. stmt.Condition = new CodeBinaryOperatorExpression (field,
  204. CodeBinaryOperatorType.IdentityEquality,
  205. new CodePrimitiveExpression (null));
  206. CodeObjectCreateExpression create = new CodeObjectCreateExpression (prop.Type);
  207. stmt.TrueStatements.Add (new CodeAssignStatement (field, create));
  208. prop.GetStatements.Add (stmt);
  209. prop.GetStatements.Add (new CodeMethodReturnStatement (field));
  210. mainClass.Members.Add (prop);
  211. }
  212. /******/
  213. void CheckCompilerErrors (CompilerResults results)
  214. {
  215. if (results.NativeCompilerReturnValue == 0)
  216. return;
  217. StringWriter writer = new StringWriter();
  218. provider.CreateGenerator().GenerateCodeFromCompileUnit (unit, writer, null);
  219. throw new CompilationException (parser.InputFile, results.Errors, writer.ToString ());
  220. }
  221. public virtual Type GetCompiledType ()
  222. {
  223. Init ();
  224. CompilerResults results = (CompilerResults) HttpRuntime.Cache [parser.InputFile];
  225. if (results != null) {
  226. Assembly a = results.CompiledAssembly;
  227. if (a != null)
  228. return a.GetType (mainClassExpr.Type.BaseType, true);
  229. }
  230. string lang = parser.Language;
  231. CompilationConfiguration config;
  232. config = CompilationConfiguration.GetInstance (parser.Context);
  233. provider = config.GetProvider (lang);
  234. if (provider == null)
  235. throw new HttpException ("Configuration error. Language not supported: " +
  236. lang, 500);
  237. compiler = provider.CreateCompiler ();
  238. CreateMethods ();
  239. compilerParameters.IncludeDebugInformation = parser.Debug;
  240. compilerParameters.CompilerOptions = config.GetCompilerOptions (lang) + " " +
  241. parser.CompilerOptions;
  242. compilerParameters.WarningLevel = config.GetWarningLevel (lang);
  243. TempFileCollection tempcoll = new TempFileCollection (config.TempDirectory);
  244. compilerParameters.TempFiles = tempcoll;
  245. string dllfilename = Path.GetFileName (tempcoll.BasePath) + ".dll";
  246. if (!Directory.Exists (dynamicBase))
  247. Directory.CreateDirectory (dynamicBase);
  248. compilerParameters.OutputAssembly = Path.Combine (dynamicBase, dllfilename);
  249. results = CachingCompiler.Compile (this);
  250. CheckCompilerErrors (results);
  251. if (results.CompiledAssembly == null)
  252. throw new CompilationException (parser.InputFile, results.Errors,
  253. "No assembly returned after compilation!?");
  254. return results.CompiledAssembly.GetType (mainClassExpr.Type.BaseType, true);
  255. }
  256. internal CompilerParameters CompilerParameters {
  257. get { return compilerParameters; }
  258. }
  259. internal CodeCompileUnit Unit {
  260. get { return unit; }
  261. }
  262. internal virtual ICodeCompiler Compiler {
  263. get { return compiler; }
  264. }
  265. internal TemplateParser Parser {
  266. get { return parser; }
  267. }
  268. }
  269. }