PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

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