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

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

https://github.com/boo/boo-lang
C# | 125 lines | 84 code | 16 blank | 25 comment | 7 complexity | 56016cffefde9f3a41ea8290775eb4f7 MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2003, 2004, 2005 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.Ast;
  30. using Boo.Lang.Compiler.MetaProgramming;
  31. using Boo.Lang.Compiler.TypeSystem;
  32. namespace Boo.Lang.Compiler.Steps
  33. {
  34. class MacroCompiler : AbstractCompilerComponent
  35. {
  36. private static readonly object CachedTypeAnnotation = new object();
  37. public MacroCompiler(CompilerContext context)
  38. {
  39. Initialize(context);
  40. }
  41. public Type Compile(InternalClass macro)
  42. {
  43. return Compile(macro.TypeDefinition);
  44. }
  45. private Type Compile(TypeDefinition node)
  46. {
  47. Type type = CachedType(node);
  48. if (type != null) return type;
  49. if (AlreadyCompiled(node)) return null;
  50. Type compiledType = RunCompiler(node);
  51. CacheType(node, compiledType);
  52. return compiledType;
  53. }
  54. private Type RunCompiler(TypeDefinition node)
  55. {
  56. TraceInfo("Compiling macro '{0}'", node.FullName);
  57. CompilerContext result = Compilation.compile_(ModuleFor(node));
  58. if (0 == result.Errors.Count)
  59. {
  60. TraceInfo("Macro '{0}' successfully compiled to '{1}'", node.FullName, result.GeneratedAssembly);
  61. return result.GeneratedAssembly.GetType(node.FullName);
  62. }
  63. ReportErrors(result.Errors);
  64. return null;
  65. }
  66. private void TraceInfo(string format, params object[] args)
  67. {
  68. Context.TraceInfo(format, args);
  69. }
  70. private Module ModuleFor(TypeDefinition node)
  71. {
  72. Module m = new Module();
  73. m.Namespace = ClearClone(node.EnclosingModule.Namespace);
  74. m.Name = node.Name;
  75. foreach (Import i in node.EnclosingModule.Imports)
  76. {
  77. m.Imports.Add(ClearClone(i));
  78. }
  79. m.Members.Add(ClearClone(node));
  80. return m;
  81. }
  82. private T ClearClone<T>(T node) where T: Node
  83. {
  84. if (node == null) return null;
  85. T clone = (T)node.CloneNode();
  86. clone.ClearTypeSystemBindings();
  87. return clone;
  88. }
  89. private void ReportErrors(CompilerErrorCollection errors)
  90. {
  91. foreach (CompilerError e in errors)
  92. {
  93. this.Errors.Add(e);
  94. }
  95. }
  96. private static void CacheType(TypeDefinition node, Type type)
  97. {
  98. node[CachedTypeAnnotation] = type;
  99. }
  100. private static Type CachedType(TypeDefinition node)
  101. {
  102. return node[CachedTypeAnnotation] as System.Type;
  103. }
  104. private static bool AlreadyCompiled(TypeDefinition node)
  105. {
  106. return node.ContainsAnnotation(CachedTypeAnnotation);
  107. }
  108. }
  109. }