PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Boo.Lang.Compiler/CompilerContext.cs

https://github.com/boo/boo-lang
C# | 322 lines | 235 code | 54 blank | 33 comment | 28 complexity | 12b4d992dd228b0a830e54e79013b419 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. using System;
  29. using System.Diagnostics;
  30. using Boo.Lang;
  31. using Boo.Lang.Compiler.Ast;
  32. using Assembly = System.Reflection.Assembly;
  33. namespace Boo.Lang.Compiler
  34. {
  35. /// <summary>
  36. /// boo compilation context.
  37. /// </summary>
  38. public class CompilerContext
  39. {
  40. public static CompilerContext Current
  41. {
  42. get { return _current != null ? _current.Value : null; }
  43. }
  44. [ThreadStatic] private static DynamicVariable<CompilerContext> _current;
  45. protected CompilerParameters _parameters;
  46. protected CompileUnit _unit;
  47. protected AssemblyCollection _assemblyReferences;
  48. protected CompilerErrorCollection _errors;
  49. protected CompilerWarningCollection _warnings;
  50. protected TypeSystem.TypeSystemServices _typeSystemServices;
  51. protected readonly TypeSystem.NameResolutionService _nameResolutionService;
  52. protected TraceSwitch _traceSwitch;
  53. protected int _localIndex;
  54. protected Assembly _generatedAssembly;
  55. protected string _generatedAssemblyFileName;
  56. protected Hash _properties;
  57. public CompilerContext() : this(new CompileUnit())
  58. {
  59. }
  60. public CompilerContext(CompileUnit unit) : this(new CompilerParameters(), unit)
  61. {
  62. }
  63. public CompilerContext(bool stdlib) : this(new CompilerParameters(stdlib), new CompileUnit())
  64. {
  65. }
  66. public CompilerContext(CompilerParameters options, CompileUnit unit)
  67. {
  68. if (null == options) throw new ArgumentNullException("options");
  69. if (null == unit) throw new ArgumentNullException("unit");
  70. _unit = unit;
  71. _errors = new CompilerErrorCollection();
  72. _warnings = new CompilerWarningCollection();
  73. _assemblyReferences = options.References;
  74. _parameters = options;
  75. if (_parameters.Debug && !_parameters.Defines.ContainsKey("DEBUG"))
  76. _parameters.Defines.Add("DEBUG", null);
  77. _nameResolutionService = new TypeSystem.NameResolutionService(this);
  78. _properties = new Hash();
  79. }
  80. public Hash Properties
  81. {
  82. get { return _properties; }
  83. }
  84. public string GeneratedAssemblyFileName
  85. {
  86. get { return _generatedAssemblyFileName; }
  87. set
  88. {
  89. if (null == value || 0 == value.Length)
  90. {
  91. throw new ArgumentException("GeneratedAssemblyFileName");
  92. }
  93. _generatedAssemblyFileName = value;
  94. }
  95. }
  96. public object this[object key]
  97. {
  98. get { return _properties[key]; }
  99. set { _properties[key] = value; }
  100. }
  101. public CompilerParameters Parameters
  102. {
  103. get { return _parameters; }
  104. }
  105. public AssemblyCollection References
  106. {
  107. get { return _assemblyReferences; }
  108. }
  109. public CompilerErrorCollection Errors
  110. {
  111. get { return _errors; }
  112. }
  113. public CompilerWarningCollection Warnings
  114. {
  115. get { return _warnings; }
  116. }
  117. public CompileUnit CompileUnit
  118. {
  119. get { return _unit; }
  120. }
  121. public TypeSystem.TypeSystemServices TypeSystemServices
  122. {
  123. get { return _typeSystemServices; }
  124. set
  125. {
  126. if (null == value) throw new ArgumentNullException("TypeSystemServices");
  127. _typeSystemServices = value;
  128. }
  129. }
  130. public TypeSystem.BooCodeBuilder CodeBuilder
  131. {
  132. get { return _typeSystemServices.CodeBuilder; }
  133. }
  134. public TypeSystem.NameResolutionService NameResolutionService
  135. {
  136. get { return _nameResolutionService; }
  137. }
  138. public Assembly GeneratedAssembly
  139. {
  140. get { return _generatedAssembly; }
  141. set { _generatedAssembly = value; }
  142. }
  143. public int AllocIndex()
  144. {
  145. return ++_localIndex;
  146. }
  147. [Conditional("TRACE")]
  148. public void TraceEnter(string format, object param)
  149. {
  150. if (_parameters.TraceInfo)
  151. {
  152. Trace.WriteLine(string.Format(format, param));
  153. ++Trace.IndentLevel;
  154. }
  155. }
  156. [Conditional("TRACE")]
  157. public void TraceLeave(string format, object param)
  158. {
  159. if (_parameters.TraceInfo)
  160. {
  161. --Trace.IndentLevel;
  162. Trace.WriteLine(string.Format(format, param));
  163. }
  164. }
  165. [Conditional("TRACE")]
  166. public void TraceInfo(string format, params object[] args)
  167. {
  168. if (_parameters.TraceInfo)
  169. {
  170. Trace.WriteLine(string.Format(format, args));
  171. }
  172. }
  173. [Conditional("TRACE")]
  174. public void TraceInfo(string message)
  175. {
  176. if (_parameters.TraceInfo)
  177. {
  178. Trace.WriteLine(message);
  179. }
  180. }
  181. [Conditional("TRACE")]
  182. public void TraceWarning(string message)
  183. {
  184. if (_parameters.TraceWarning)
  185. {
  186. Trace.WriteLine(message);
  187. }
  188. }
  189. [Conditional("TRACE")]
  190. public void TraceWarning(string message, params object[] args)
  191. {
  192. if (_parameters.TraceWarning)
  193. {
  194. Trace.WriteLine(string.Format(message, args));
  195. }
  196. }
  197. [Conditional("TRACE")]
  198. public void TraceVerbose(string format, params object[] args)
  199. {
  200. if (_parameters.TraceVerbose)
  201. {
  202. Trace.WriteLine(string.Format(format, args));
  203. }
  204. }
  205. [Conditional("TRACE")]
  206. public void TraceVerbose(string format, object param1, object param2)
  207. {
  208. if (_parameters.TraceVerbose)
  209. {
  210. Trace.WriteLine(string.Format(format, param1, param2));
  211. }
  212. }
  213. [Conditional("TRACE")]
  214. public void TraceVerbose(string format, object param1, object param2, object param3)
  215. {
  216. if (_parameters.TraceVerbose)
  217. {
  218. Trace.WriteLine(string.Format(format, param1, param2, param3));
  219. }
  220. }
  221. [Conditional("TRACE")]
  222. public void TraceVerbose(string format, object param)
  223. {
  224. if (_parameters.TraceVerbose)
  225. {
  226. Trace.WriteLine(string.Format(format, param));
  227. }
  228. }
  229. [Conditional("TRACE")]
  230. public void TraceVerbose(string message)
  231. {
  232. if (_parameters.TraceVerbose)
  233. {
  234. Trace.WriteLine(message);
  235. }
  236. }
  237. [Conditional("TRACE")]
  238. public void TraceError(string message, params object[] args)
  239. {
  240. if (_parameters.TraceError)
  241. {
  242. Trace.WriteLine(string.Format(message, args));
  243. }
  244. }
  245. [Conditional("TRACE")]
  246. public void TraceError(Exception x)
  247. {
  248. if (_parameters.TraceError)
  249. {
  250. Trace.WriteLine(x);
  251. }
  252. }
  253. /// <summary>
  254. /// Runs the given action with this context ensuring CompilerContext.Current
  255. /// returns the right context.
  256. /// </summary>
  257. /// <param name="action"></param>
  258. public void Run(System.Action<CompilerContext> action)
  259. {
  260. CurrentVariable().With(this, action);
  261. }
  262. private static DynamicVariable<CompilerContext> CurrentVariable()
  263. {
  264. if (null == _current) _current = new DynamicVariable<CompilerContext>();
  265. return _current;
  266. }
  267. }
  268. }