1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.api.tree;
  26 
  27 import java.util.List;
  28 import java.util.stream.Collectors;
  29 import jdk.nashorn.internal.ir.FunctionNode;
  30 import jdk.nashorn.internal.ir.IdentNode;
  31 import jdk.nashorn.internal.ir.Module;
  32 import static jdk.nashorn.api.tree.ExportEntryTreeImpl.createExportList;
  33 import static jdk.nashorn.api.tree.ImportEntryTreeImpl.createImportList;
  34 
  35 final class ModuleTreeImpl extends TreeImpl implements ModuleTree {
  36 
  37     private final Module mod;
  38     private final List<? extends ImportEntryTree> imports;
  39     private final List<? extends ExportEntryTree> localExports;
  40     private final List<? extends ExportEntryTree> indirectExports;
  41     private final List<? extends ExportEntryTree> starExports;
  42 
  43     private ModuleTreeImpl(final FunctionNode func,
  44             final List<? extends ImportEntryTree> imports,
  45             final List<? extends ExportEntryTree> localExports,
  46             final List<? extends ExportEntryTree> indirectExports,
  47             final List<? extends ExportEntryTree> starExports) {
  48         super(func);
  49         assert func.getKind() == FunctionNode.Kind.MODULE : "module function node expected";
  50         this.mod = func.getModule();
  51         this.imports = imports;
  52         this.localExports = localExports;
  53         this.indirectExports = indirectExports;
  54         this.starExports = starExports;
  55     }
  56 
  57     static ModuleTreeImpl create(final FunctionNode func) {
  58         final Module mod = func.getModule();
  59         return new ModuleTreeImpl(func,
  60             createImportList(mod.getImportEntries()),
  61             createExportList(mod.getLocalExportEntries()),
  62             createExportList(mod.getIndirectExportEntries()),
  63             createExportList(mod.getStarExportEntries()));
  64     }
  65 
  66     @Override
  67     public Kind getKind() {
  68         return Tree.Kind.MODULE;
  69     }
  70 
  71     @Override
  72     public List<? extends ImportEntryTree> getImportEntries() {
  73         return imports;
  74     }
  75 
  76     @Override
  77     public List<? extends ExportEntryTree> getLocalExportEntries() {
  78         return localExports;
  79     }
  80 
  81     @Override
  82     public List<? extends ExportEntryTree> getIndirectExportEntries() {
  83         return indirectExports;
  84     }
  85 
  86     @Override
  87     public List<? extends ExportEntryTree> getStarExportEntries() {
  88         return starExports;
  89     }
  90 
  91     @Override
  92     public <R,D> R accept(final TreeVisitor<R,D> visitor, final D data) {
  93         return visitor.visitModule(this, data);
  94     }
  95 
  96     static IdentifierTree identOrNull(final IdentNode node) {
  97         return node != null? new IdentifierTreeImpl(node) : null;
  98     }
  99 }