PageRenderTime 94ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/boo/src/Boo.Lang.Compiler/CompilerPipeline.cs

https://github.com/w4x/boolangstudio
C# | 309 lines | 239 code | 41 blank | 29 comment | 31 complexity | d3aafc4289baba6559ae0f02774f2d5b MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Rodrigo B. de Oliveira nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. namespace Boo.Lang.Compiler
  29. {
  30. using System;
  31. using System.Reflection;
  32. public class CompilerStepEventArgs : EventArgs
  33. {
  34. public CompilerContext Context;
  35. public ICompilerStep Step;
  36. public CompilerStepEventArgs(CompilerContext context, ICompilerStep step)
  37. {
  38. this.Context = context;
  39. this.Step = step;
  40. }
  41. }
  42. public delegate void CompilerStepEventHandler(object sender, CompilerStepEventArgs args);
  43. /// <summary>
  44. /// A ordered set of <see cref="ICompilerStep"/> implementations
  45. /// that should be executed in sequence.
  46. /// </summary>
  47. public class CompilerPipeline
  48. {
  49. public event CompilerStepEventHandler BeforeStep;
  50. public event CompilerStepEventHandler AfterStep;
  51. public static CompilerPipeline GetPipeline(string name)
  52. {
  53. if (null == name) throw new ArgumentNullException("name");
  54. switch (name)
  55. {
  56. case "parse": return new Pipelines.Parse();
  57. case "compile": return new Pipelines.Compile();
  58. case "run": return new Pipelines.Run();
  59. case "default": return new Pipelines.CompileToFile();
  60. case "verify": return new Pipelines.CompileToFileAndVerify();
  61. case "roundtrip": return new Pipelines.ParseAndPrint();
  62. case "boo": return new Pipelines.CompileToBoo();
  63. case "ast": return new Pipelines.ParseAndPrintAst();
  64. case "xml": return new Pipelines.ParseAndPrintXml();
  65. case "checkforerrors": return new Pipelines.CheckForErrors();
  66. case "dumpreferences":
  67. {
  68. CompilerPipeline pipeline = new Pipelines.CompileToBoo();
  69. pipeline.Add(new Boo.Lang.Compiler.Steps.DumpReferences());
  70. return pipeline;
  71. }
  72. }
  73. return LoadCustomPipeline(name);
  74. }
  75. private static CompilerPipeline LoadCustomPipeline(string typeName)
  76. {
  77. if (typeName.IndexOf(',') < 0)
  78. throw new ArgumentException(Boo.Lang.ResourceManager.Format("BooC.InvalidPipeline", typeName));
  79. return (CompilerPipeline)Activator.CreateInstance(FindPipelineType(typeName));
  80. }
  81. private static System.Type FindPipelineType(string typeName)
  82. {
  83. Assembly loaded = FindLoadedAssembly(AssemblySimpleNameFromFullTypeName(typeName));
  84. if (null != loaded) return loaded.GetType(SimpleTypeNameFromFullTypeName(typeName));
  85. return Type.GetType(typeName, true);
  86. }
  87. private static string SimpleTypeNameFromFullTypeName(string name)
  88. {
  89. return name.Split(',')[0].Trim();
  90. }
  91. private static string AssemblySimpleNameFromFullTypeName(string name)
  92. {
  93. return name.Split(',')[1].Trim();
  94. }
  95. private static Assembly FindLoadedAssembly(string assemblyName)
  96. {
  97. foreach (Assembly loaded in AppDomain.CurrentDomain.GetAssemblies())
  98. {
  99. if (loaded.GetName().Name == assemblyName) return loaded;
  100. }
  101. return null;
  102. }
  103. protected Boo.Lang.List _items;
  104. protected bool _breakOnErrors;
  105. public CompilerPipeline()
  106. {
  107. _items = new Boo.Lang.List();
  108. _breakOnErrors = true;
  109. }
  110. public bool BreakOnErrors
  111. {
  112. get
  113. {
  114. return _breakOnErrors;
  115. }
  116. set
  117. {
  118. _breakOnErrors = value;
  119. }
  120. }
  121. public CompilerPipeline Add(ICompilerStep step)
  122. {
  123. if (null == step)
  124. {
  125. throw new ArgumentNullException("step");
  126. }
  127. _items.Add(step);
  128. return this;
  129. }
  130. public CompilerPipeline RemoveAt(int index)
  131. {
  132. _items.RemoveAt(index);
  133. return this;
  134. }
  135. public CompilerPipeline Insert(int index, ICompilerStep step)
  136. {
  137. if (null == step)
  138. {
  139. throw new ArgumentNullException("step");
  140. }
  141. _items.Insert(index, step);
  142. return this;
  143. }
  144. public CompilerPipeline InsertAfter(Type stepExactType, ICompilerStep step)
  145. {
  146. return Insert(Find(stepExactType)+1, step);
  147. }
  148. public CompilerPipeline InsertBefore(Type stepExactType, ICompilerStep step)
  149. {
  150. return Insert(Find(stepExactType)-1, step);
  151. }
  152. public CompilerPipeline Replace(Type stepExactType, ICompilerStep step)
  153. {
  154. if (null == step)
  155. {
  156. throw new ArgumentNullException("step");
  157. }
  158. int index = Find(stepExactType);
  159. if (-1 == index)
  160. {
  161. throw new ArgumentException("stepExactType");
  162. }
  163. _items[index] = step;
  164. return this;
  165. }
  166. public int Find(Type stepExactType)
  167. {
  168. if (null == stepExactType)
  169. {
  170. throw new ArgumentNullException("stepExactType");
  171. }
  172. for (int i=0; i<_items.Count; ++i)
  173. {
  174. if (_items[i].GetType() == stepExactType)
  175. {
  176. return i;
  177. }
  178. }
  179. return -1;
  180. }
  181. public ICompilerStep Get(Type stepExactType)
  182. {
  183. int index = Find(stepExactType);
  184. if (-1 == index) return null;
  185. return (ICompilerStep)_items[index];
  186. }
  187. public int Count
  188. {
  189. get
  190. {
  191. return _items.Count;
  192. }
  193. }
  194. public ICompilerStep this[int index]
  195. {
  196. get
  197. {
  198. return (ICompilerStep)_items[index];
  199. }
  200. set
  201. {
  202. if (null == value)
  203. {
  204. throw new ArgumentNullException("value");
  205. }
  206. _items[index] = value;
  207. }
  208. }
  209. virtual public void Clear()
  210. {
  211. _items.Clear();
  212. }
  213. virtual protected void OnBeforeStep(CompilerContext context, ICompilerStep step)
  214. {
  215. if (null != BeforeStep)
  216. {
  217. BeforeStep(this, new CompilerStepEventArgs(context, step));
  218. }
  219. }
  220. virtual protected void OnAfterStep(CompilerContext context, ICompilerStep step)
  221. {
  222. if (null != AfterStep)
  223. {
  224. AfterStep(this, new CompilerStepEventArgs(context, step));
  225. }
  226. }
  227. virtual protected void Prepare(CompilerContext context)
  228. {
  229. }
  230. virtual public void Run(CompilerContext context)
  231. {
  232. Prepare(context);
  233. foreach (ICompilerStep step in _items)
  234. {
  235. RunStep(context, step);
  236. if (_breakOnErrors && context.Errors.Count > 0)
  237. {
  238. break;
  239. }
  240. }
  241. foreach (ICompilerStep step in _items)
  242. {
  243. step.Dispose();
  244. }
  245. }
  246. protected void RunStep(CompilerContext context, ICompilerStep step)
  247. {
  248. OnBeforeStep(context, step);
  249. step.Initialize(context);
  250. try
  251. {
  252. step.Run();
  253. }
  254. catch (Boo.Lang.Compiler.CompilerError error)
  255. {
  256. context.Errors.Add(error);
  257. }
  258. catch (System.Exception x)
  259. {
  260. context.Errors.Add(CompilerErrorFactory.StepExecutionError(x, step));
  261. }
  262. finally
  263. {
  264. OnAfterStep(context, step);
  265. }
  266. }
  267. }
  268. }