TemplateScope.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package kawa.lang;
  2. import gnu.bytecode.ClassType;
  3. import gnu.expr.*;
  4. import java.io.*;
  5. /** A scope created when expanding a SyntaxTemplate.
  6. * This is used to ensure proper "hygiene". */
  7. public class TemplateScope extends LetExp implements Externalizable
  8. {
  9. /** The module instance containing the defining macro.
  10. * If we're expanding a macro imported from some external module,
  11. * the macro's template(s) may expand to references to declarations in
  12. * that external module. If the module is non-static, we may need a
  13. * context instance to access those declarations; we inherit the context
  14. * instance from the declaration bound to the imported macro.
  15. * This is used to setContextDecl() of such references. */
  16. Declaration macroContext;
  17. /** See Translator#currentMacroMark. */
  18. Object macroMark;
  19. private Syntax syntax; // Only used for debugging
  20. public TemplateScope ()
  21. {
  22. }
  23. public TemplateScope (ScopeExp outer)
  24. {
  25. this.setOuter(outer);
  26. }
  27. public static TemplateScope make ()
  28. {
  29. return make((Translator) Compilation.getCurrent(), null);
  30. }
  31. public static TemplateScope make (Translator tr, ScopeExp savedScope) {
  32. TemplateScope templateScope = new TemplateScope(savedScope);
  33. if (tr != null) {
  34. templateScope.macroMark = tr.currentMacroMark;
  35. Syntax curSyntax = tr.getCurrentSyntax();
  36. if (curSyntax instanceof Macro) {
  37. templateScope.macroContext = tr.macroContext;
  38. if (savedScope == null)
  39. templateScope.setOuter(((Macro) curSyntax).getCapturedScope());
  40. }
  41. templateScope.syntax = curSyntax;
  42. }
  43. return templateScope;
  44. }
  45. public static TemplateScope make(ModuleExp module, String mname) {
  46. TemplateScope templateScope = new TemplateScope();
  47. templateScope.setOuter(module);
  48. return templateScope;
  49. }
  50. void init(Macro macro) {
  51. setOuter(macro.getCapturedScope());
  52. macroContext = getOuter().lookup(macro.getName());
  53. syntax = macro;
  54. macroMark = macro;
  55. }
  56. public static TemplateScope make(String moduleClassName) {
  57. TemplateScope templateScope = new TemplateScope();
  58. templateScope.setOuter(moduleClassName);
  59. return templateScope;
  60. }
  61. void setOuter(String moduleClassName) {
  62. setOuter(ModuleInfo.find(ClassType.make(moduleClassName)).getModuleExp());
  63. }
  64. public String toString() { return super.toString()+"(for "+syntax+")"; }
  65. public void writeExternal(ObjectOutput out) throws IOException
  66. {
  67. String moduleClassName = null;
  68. if (getOuter() instanceof ModuleExp) {
  69. ClassType moduleClass = ((ModuleExp) getOuter()).getClassType();
  70. if (moduleClass != null)
  71. moduleClassName = moduleClass.getName();
  72. }
  73. out.writeObject(moduleClassName);
  74. }
  75. public void readExternal(ObjectInput in)
  76. throws IOException, ClassNotFoundException
  77. {
  78. in.readObject(); // ignore, for now
  79. }
  80. /* FUTURE
  81. // This may be a cleaner way to implement hygiene renaming.
  82. // Instead of alias declaration use alias symbols.
  83. // When NameLookup.lookup fails for an alias symbol, we seach for
  84. // the original symbol, but starting withe tscope.getOuter().
  85. // Another complication is dealing with quoted symbols (use the unaliased).
  86. // Note there may be multiple levels of aliasing in case of a macro
  87. // generating a macro.
  88. AliasNamespace aliasNamespaces;
  89. public Symbol getAliasSymbol(Symbol symbol) {
  90. Namespace ns = symbol.getNamespace();
  91. if (ns == null)
  92. return symbol;
  93. return getAliasNamespace(ns).getSymbol(symbol.getLocalPart());
  94. }
  95. AliasNamespace getAliasNamespace(Namespace baseNamespace) {
  96. for (AliasNamespace ns = aliasNamespaces; ns != null; ns = ns.next)
  97. if (ns.baseNamespace == baseNamespace)
  98. return ns;
  99. AliasNamespace ns = new AliasNamespace(baseNamespace, this);
  100. ns.next = aliasNamespaces;
  101. aliasNamespaces = ns;
  102. return ns;
  103. }
  104. public static class AliasNamespace extends Namespace {
  105. public Namespace baseNamespace;
  106. AliasNamespace next;
  107. public TemplateScope tscope;
  108. public AliasNamespace(Namespace base, TemplateScope tscope) {
  109. this.prefix = base.getPrefix();
  110. this.setName(base.getName());
  111. this.tscope = tscope;
  112. }
  113. }
  114. */
  115. }