All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.aya.concrete.resolve.context.PhysicalModuleContext Maven / Gradle / Ivy

There is a newer version: 0.33.0
Show newest version
// Copyright (c) 2020-2021 Yinsen (Tesla) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.concrete.resolve.context;

import kala.collection.Seq;
import kala.collection.immutable.ImmutableSeq;
import kala.collection.mutable.MutableHashMap;
import kala.collection.mutable.MutableMap;
import org.aya.api.ref.Var;
import org.aya.concrete.resolve.error.DuplicateExportError;
import org.aya.concrete.stmt.Stmt;
import org.aya.util.error.SourcePos;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author re-xyr
 */
public non-sealed class PhysicalModuleContext implements ModuleContext {
  public final @NotNull Context parent;
  public final @NotNull MutableMap, Var>> definitions = MutableHashMap.create();
  public final @NotNull MutableMap, MutableMap> modules = MutableHashMap.of(TOP_LEVEL_MOD_NAME, MutableHashMap.create());
  public final @NotNull MutableMap, MutableMap> exports = MutableHashMap.of(TOP_LEVEL_MOD_NAME, MutableHashMap.create());

  private final @NotNull ImmutableSeq moduleName;

  @Override
  public @NotNull ImmutableSeq moduleName() {
    return moduleName;
  }

  private @Nullable NoExportContext exampleContext;

  public PhysicalModuleContext(@NotNull Context parent, @NotNull ImmutableSeq moduleName) {
    this.parent = parent;
    this.moduleName = moduleName;
  }

  @Override public void importModule(
    @NotNull Stmt.Accessibility accessibility,
    @NotNull SourcePos sourcePos,
    ImmutableSeq componentName,
    MutableMap mod
  ) {
    ModuleContext.super.importModule(accessibility, sourcePos, componentName, mod);
    if (accessibility == Stmt.Accessibility.Public) exports.set(componentName, mod);
  }

  @Override public void addGlobal(
    @NotNull ImmutableSeq modName,
    @NotNull String name,
    @NotNull Stmt.Accessibility accessibility,
    @NotNull Var ref,
    @NotNull SourcePos sourcePos
  ) {
    ModuleContext.super.addGlobal(modName, name, accessibility, ref, sourcePos);
    if (accessibility == Stmt.Accessibility.Public) {
      if (exports.get(TOP_LEVEL_MOD_NAME).containsKey(name)) {
        reportAndThrow(new DuplicateExportError(name, sourcePos));
      } else exports.get(TOP_LEVEL_MOD_NAME).set(name, ref);
    }
  }

  public @NotNull NoExportContext exampleContext() {
    if (exampleContext == null) exampleContext = new NoExportContext(this);
    return exampleContext;
  }

  @Override public @NotNull Context parent() {
    return parent;
  }

  @Override public @NotNull MutableMap, Var>> definitions() {
    return definitions;
  }

  @Override public @NotNull MutableMap, MutableMap> modules() {
    return modules;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy