PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Boo.Lang.Compiler/Steps/AbstractVisitorCompilerStep.cs

https://github.com/boo/boo-lang
C# | 229 lines | 170 code | 33 blank | 26 comment | 5 complexity | 0b2a8820168f4f156dee7a5abfdaa273 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 Boo.Lang.Compiler;
  30. using Boo.Lang.Compiler.Ast;
  31. using Boo.Lang.Compiler.TypeSystem;
  32. namespace Boo.Lang.Compiler.Steps
  33. {
  34. public abstract class AbstractVisitorCompilerStep : Boo.Lang.Compiler.Ast.DepthFirstVisitor, ICompilerStep
  35. {
  36. protected CompilerContext _context;
  37. protected AbstractVisitorCompilerStep()
  38. {
  39. }
  40. protected CompilerContext Context
  41. {
  42. get
  43. {
  44. return _context;
  45. }
  46. }
  47. protected BooCodeBuilder CodeBuilder
  48. {
  49. get
  50. {
  51. return _context.CodeBuilder;
  52. }
  53. }
  54. protected Boo.Lang.Compiler.Ast.CompileUnit CompileUnit
  55. {
  56. get
  57. {
  58. return _context.CompileUnit;
  59. }
  60. }
  61. protected NameResolutionService NameResolutionService
  62. {
  63. get
  64. {
  65. return _context.NameResolutionService;
  66. }
  67. }
  68. protected CompilerParameters Parameters
  69. {
  70. get
  71. {
  72. return _context.Parameters;
  73. }
  74. }
  75. protected CompilerErrorCollection Errors
  76. {
  77. get
  78. {
  79. return _context.Errors;
  80. }
  81. }
  82. protected CompilerWarningCollection Warnings
  83. {
  84. get
  85. {
  86. return _context.Warnings;
  87. }
  88. }
  89. protected TypeSystemServices TypeSystemServices
  90. {
  91. get
  92. {
  93. return _context.TypeSystemServices;
  94. }
  95. }
  96. public override void OnQuasiquoteExpression(Boo.Lang.Compiler.Ast.QuasiquoteExpression node)
  97. {
  98. // ignore quasi-quotes
  99. }
  100. override protected void OnError(Node node, Exception error)
  101. {
  102. _context.TraceError("{0}: Internal compiler error on node '{2}': {1}", node.LexicalInfo, error, node);
  103. base.OnError(node, error);
  104. }
  105. protected void Error(Expression node, CompilerError error)
  106. {
  107. Error(node);
  108. Errors.Add(error);
  109. }
  110. protected void Error(CompilerError error)
  111. {
  112. Errors.Add(error);
  113. }
  114. protected void Error(Expression node)
  115. {
  116. node.ExpressionType = TypeSystemServices.ErrorEntity;
  117. }
  118. protected void Bind(Node node, IEntity tag)
  119. {
  120. _context.TraceVerbose("{0}: Node '{1}' bound to '{2}'.", node.LexicalInfo, node, tag);
  121. node.Entity = tag;
  122. }
  123. public IEntity GetEntity(Node node)
  124. {
  125. return TypeSystemServices.GetEntity(node);
  126. }
  127. public IMethod GetEntity(Method node)
  128. {
  129. return (IMethod)TypeSystemServices.GetEntity(node);
  130. }
  131. public IProperty GetEntity(Property node)
  132. {
  133. return (IProperty)TypeSystemServices.GetEntity(node);
  134. }
  135. protected void BindExpressionType(Expression node, IType type)
  136. {
  137. _context.TraceVerbose("{0}: Type of expression '{1}' bound to '{2}'.", node.LexicalInfo, node, type);
  138. node.ExpressionType = type;
  139. }
  140. protected IType GetConcreteExpressionType(Expression expression)
  141. {
  142. return TypeSystemServices.GetConcreteExpressionType(expression);
  143. }
  144. protected virtual IType GetExpressionType(Expression node)
  145. {
  146. return TypeSystemServices.GetExpressionType(node);
  147. }
  148. public IType GetType(Node node)
  149. {
  150. return TypeSystemServices.GetType(node);
  151. }
  152. public InternalLocal GetInternalLocal(Node local)
  153. {
  154. return (InternalLocal)GetEntity(local);
  155. }
  156. protected void NotImplemented(Node node, string feature)
  157. {
  158. throw CompilerErrorFactory.NotImplemented(node, feature);
  159. }
  160. public virtual void Initialize(CompilerContext context)
  161. {
  162. if (null == context)
  163. {
  164. throw new ArgumentNullException("context");
  165. }
  166. _context = context;
  167. }
  168. public abstract void Run();
  169. public virtual void Dispose()
  170. {
  171. _context = null;
  172. }
  173. private readonly object VisitedAnnotationKey = new object();
  174. protected void MarkVisited(Node node)
  175. {
  176. node[VisitedAnnotationKey] = VisitedAnnotationKey;
  177. _context.TraceInfo("{0}: node '{1}' mark visited.", node.LexicalInfo, node);
  178. }
  179. protected virtual void EnsureRelatedNodeWasVisited(Node sourceNode, IEntity entity)
  180. {
  181. IInternalEntity internalEntity = entity as IInternalEntity;
  182. if (null != internalEntity)
  183. {
  184. Node node = internalEntity.Node;
  185. if (!WasVisited(node))
  186. {
  187. Visit(node);
  188. }
  189. }
  190. }
  191. protected bool WasVisited(Node node)
  192. {
  193. return node.ContainsAnnotation(VisitedAnnotationKey);
  194. }
  195. }
  196. }