2
0

BaseCompiler.cs 9.2 KB

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