langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java
author jlahoda
Tue, 28 Jun 2016 13:33:04 +0200
changeset 39366 8bf5fe72ca88
parent 39361 cd0aca7db174
child 39915 5d1e0740e709
permissions -rw-r--r--
8155026: javac grants implied readability to explicit modules Summary: Automatic modules should not 'requires public' ordinary named modules Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    25
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
package com.sun.tools.javac.comp;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.Arrays;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.Collection;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Collections;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.EnumSet;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.HashMap;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.HashSet;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.LinkedHashMap;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.LinkedHashSet;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Map;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Map.Entry;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.Set;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.function.Predicate;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.regex.Matcher;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.regex.Pattern;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.stream.Stream;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import javax.lang.model.SourceVersion;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import javax.tools.JavaFileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import javax.tools.JavaFileManager.Location;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import javax.tools.JavaFileObject;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import javax.tools.JavaFileObject.Kind;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import javax.tools.StandardLocation;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
39361
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
    53
import com.sun.tools.javac.code.ClassFinder;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import com.sun.tools.javac.code.Directive;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import com.sun.tools.javac.code.Directive.ExportsDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import com.sun.tools.javac.code.Directive.RequiresDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import com.sun.tools.javac.code.Directive.RequiresFlag;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import com.sun.tools.javac.code.Directive.UsesDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import com.sun.tools.javac.code.Flags;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    60
import com.sun.tools.javac.code.Kinds;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
import com.sun.tools.javac.code.ModuleFinder;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
import com.sun.tools.javac.code.Source;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
import com.sun.tools.javac.code.Symbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
import com.sun.tools.javac.code.Symbol.ClassSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
import com.sun.tools.javac.code.Symbol.Completer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
import com.sun.tools.javac.code.Symbol.CompletionFailure;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
import com.sun.tools.javac.code.Symbol.MethodSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
import com.sun.tools.javac.code.Symbol.PackageSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
import com.sun.tools.javac.code.Symtab;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
import com.sun.tools.javac.code.Type;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
    72
import com.sun.tools.javac.code.Types;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
import com.sun.tools.javac.jvm.ClassWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
import com.sun.tools.javac.jvm.JNIWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
import com.sun.tools.javac.main.Option;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
import com.sun.tools.javac.resources.CompilerProperties.Errors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
import com.sun.tools.javac.tree.JCTree;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
import com.sun.tools.javac.tree.JCTree.JCExports;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
import com.sun.tools.javac.tree.JCTree.JCExpression;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
import com.sun.tools.javac.tree.JCTree.JCPackageDecl;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
import com.sun.tools.javac.tree.JCTree.JCProvides;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
import com.sun.tools.javac.tree.JCTree.JCRequires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
import com.sun.tools.javac.tree.JCTree.JCUses;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
import com.sun.tools.javac.tree.TreeInfo;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
import com.sun.tools.javac.util.Assert;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
import com.sun.tools.javac.util.Context;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
import com.sun.tools.javac.util.JCDiagnostic;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    91
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
import com.sun.tools.javac.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
import com.sun.tools.javac.util.ListBuffer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
import com.sun.tools.javac.util.Log;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
import com.sun.tools.javac.util.Name;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
import com.sun.tools.javac.util.Names;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
import com.sun.tools.javac.util.Options;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
import static com.sun.tools.javac.code.Flags.UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
import static com.sun.tools.javac.code.Kinds.Kind.MDL;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
import static com.sun.tools.javac.code.TypeTag.CLASS;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
import com.sun.tools.javac.tree.JCTree.JCDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
import com.sun.tools.javac.tree.JCTree.Tag;
39361
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   105
import com.sun.tools.javac.util.Abort;
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   106
import com.sun.tools.javac.util.Position;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
import static com.sun.tools.javac.code.Flags.ABSTRACT;
37854
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   109
import static com.sun.tools.javac.code.Flags.ENUM;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   110
import static com.sun.tools.javac.code.Flags.PUBLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
import static com.sun.tools.javac.tree.JCTree.Tag.MODULEDEF;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
/**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
 *  TODO: fill in
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   115
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   116
 *  <p><b>This is NOT part of any supported API.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   117
 *  If you write code that depends on this, you do so at your own risk.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
 *  This code and its internal interfaces are subject to change or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
 *  deletion without notice.</b>
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
public class Modules extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
    private static final String ALL_SYSTEM = "ALL-SYSTEM";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
    private final Log log;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    private final Names names;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    private final Symtab syms;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    private final Attr attr;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
    private final TypeEnvs typeEnvs;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   130
    private final Types types;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
    private final JavaFileManager fileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
    private final ModuleFinder moduleFinder;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
    private final boolean allowModules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   134
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    public final boolean multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    private final String moduleOverride;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   138
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   139
    private final Name java_se;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   140
    private final Name java_;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   141
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    ModuleSymbol defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   143
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    private final String addExportsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   145
    private Map<ModuleSymbol, Set<ExportsDirective>> addExports;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    private final String addReadsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
    private Map<ModuleSymbol, Set<RequiresDirective>> addReads;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
    private final String addModsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   149
    private final String limitModsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   150
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   151
    private Set<ModuleSymbol> rootModules = Collections.emptySet();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    public static Modules instance(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
        Modules instance = context.get(Modules.class);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   155
        if (instance == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            instance = new Modules(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        return instance;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   158
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   159
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   160
    protected Modules(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        context.put(Modules.class, this);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   162
        log = Log.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        names = Names.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        syms = Symtab.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   165
        attr = Attr.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   166
        typeEnvs = TypeEnvs.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        moduleFinder = ModuleFinder.instance(context);
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   168
        types = Types.instance(context);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        fileManager = context.get(JavaFileManager.class);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        allowModules = Source.instance(context).allowModules();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        Options options = Options.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        moduleOverride = options.get(Option.XMODULE);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   174
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        ClassWriter classWriter = ClassWriter.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        classWriter.multiModuleMode = multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        JNIWriter jniWriter = JNIWriter.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        jniWriter.multiModuleMode = multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   180
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   181
        java_se = names.fromString("java.se");
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   182
        java_ = names.fromString("java.");
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   183
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        addExportsOpt = options.get(Option.XADDEXPORTS);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        addReadsOpt = options.get(Option.XADDREADS);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        addModsOpt = options.get(Option.ADDMODS);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        limitModsOpt = options.get(Option.LIMITMODS);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   188
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   190
    int depth = -1;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   191
    private void dprintln(String msg) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        for (int i = 0; i < depth; i++)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   193
            System.err.print("  ");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        System.err.println(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   195
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   196
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
    public boolean enter(List<JCCompilationUnit> trees, ClassSymbol c) {
38619
27c0007bb28d 8152785: Remove javac -XDnoModules
vromero
parents: 37854
diff changeset
   198
        if (!allowModules) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   199
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                tree.modle = syms.noModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   201
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            defaultModule = syms.noModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   203
            return true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   204
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   205
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   206
        int startErrors = log.nerrors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   207
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   208
        depth++;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   210
            // scan trees for module defs
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   211
            Set<ModuleSymbol> roots = enterModules(trees, c);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   212
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   213
            setCompilationUnitModules(trees, roots);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   214
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   215
            if (!roots.isEmpty() && this.rootModules.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   216
                this.rootModules = roots;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   217
                allModules(); //ensure errors reported
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   218
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   219
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   220
            for (ModuleSymbol msym: roots) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   221
                msym.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   222
            }
39361
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   223
        } catch (CompletionFailure ex) {
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   224
            log.error(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE, Position.NOPOS, "cant.access", ex.sym, ex.getDetailValue());
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   225
            if (ex instanceof ClassFinder.BadClassFile) throw new Abort();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   226
        } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   227
            depth--;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   229
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        return (log.nerrors == startErrors);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   231
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   232
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   233
    public Completer getCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   234
        return mainCompleter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   235
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   236
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   237
    public ModuleSymbol getDefaultModule() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   238
        return defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   239
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   240
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   241
    private Set<ModuleSymbol> enterModules(List<JCCompilationUnit> trees, ClassSymbol c) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        Set<ModuleSymbol> modules = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        for (JCCompilationUnit tree : trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   246
                enterModule(tree, c, modules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   247
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   248
                log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   249
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   251
        return modules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   252
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   253
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   254
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   255
    private void enterModule(JCCompilationUnit toplevel, ClassSymbol c, Set<ModuleSymbol> modules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   256
        boolean isModuleInfo = toplevel.sourcefile.isNameCompatible("module-info", Kind.SOURCE);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   257
        boolean isModuleDecl = toplevel.defs.nonEmpty() && toplevel.defs.head.hasTag(MODULEDEF);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        if (isModuleInfo && isModuleDecl) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
            JCModuleDecl decl = (JCModuleDecl) toplevel.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            Name name = TreeInfo.fullName(decl.qualId);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   261
            ModuleSymbol sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   262
            if (c != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   263
               sym = (ModuleSymbol) c.owner;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
               if (sym.name == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   265
                   //ModuleFinder.findSingleModule creates a stub of a ModuleSymbol without a name,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                   //fill the name here after the module-info.java has been parsed
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                   //also enter the ModuleSymbol among modules:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                   syms.enterModule(sym, name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   269
               } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                   // TODO: validate name
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   271
               }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   272
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   273
                sym = syms.enterModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                if (sym.module_info.sourcefile != null && sym.module_info.sourcefile != toplevel.sourcefile) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                    log.error(decl.pos(), Errors.DuplicateModule(sym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   276
                    return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   277
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   278
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   279
            sym.completer = getSourceCompleter(toplevel);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   280
            sym.module_info.sourcefile = toplevel.sourcefile;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   281
            decl.sym = sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   282
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   283
            if (multiModuleMode || modules.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   284
                modules.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
                log.error(toplevel.pos(), Errors.TooManyModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   287
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   288
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   289
            Env<AttrContext> provisionalEnv = new Env<>(decl, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   290
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   291
            provisionalEnv.toplevel = toplevel;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   292
            typeEnvs.put(sym, provisionalEnv);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   293
        } else if (isModuleInfo) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   294
            if (multiModuleMode) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   295
                JCTree tree = toplevel.defs.isEmpty() ? toplevel : toplevel.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   296
                log.error(tree.pos(), Errors.ExpectedModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   298
        } else if (isModuleDecl) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            JCTree tree = toplevel.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   300
            log.error(tree.pos(), Errors.ModuleDeclSbInModuleInfoJava);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   301
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   302
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   303
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   304
    private void setCompilationUnitModules(List<JCCompilationUnit> trees, Set<ModuleSymbol> rootModules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   305
        // update the module for each compilation unit
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   306
        if (multiModuleMode) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   308
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                if (tree.defs.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                    tree.modle = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   312
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   313
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   314
                JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   315
                try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   316
                    Location locn = getModuleLocation(tree);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   317
                    if (locn != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   318
                        Name name = names.fromString(fileManager.inferModuleName(locn));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   319
                        ModuleSymbol msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   320
                        if (tree.defs.head.hasTag(MODULEDEF)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   321
                            JCModuleDecl decl = (JCModuleDecl) tree.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   322
                            msym = decl.sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   323
                            if (msym.name != name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   324
                                log.error(decl.qualId, Errors.ModuleNameMismatch(msym.name, name));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   325
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   326
                        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   327
                            msym = syms.enterModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   328
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   329
                        if (msym.sourceLocation == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                            msym.sourceLocation = locn;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                            if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   332
                                msym.classLocation = fileManager.getModuleLocation(
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   333
                                        StandardLocation.CLASS_OUTPUT, msym.name.toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   334
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   335
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   336
                        tree.modle = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   337
                        rootModules.add(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   338
                    } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   339
                        log.error(tree.pos(), Errors.UnnamedPkgNotAllowedNamedModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   340
                        tree.modle = syms.errModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   341
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   342
                } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   343
                    throw new Error(e); // FIXME
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   344
                } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   345
                    log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   346
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   347
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   348
            if (syms.unnamedModule.sourceLocation == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   349
                syms.unnamedModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   350
                syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   351
                syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   352
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   353
            defaultModule = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   354
        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   355
            if (defaultModule == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   356
                switch (rootModules.size()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   357
                    case 0:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   358
                        defaultModule = moduleFinder.findSingleModule();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   359
                        if (defaultModule == syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   360
                            if (moduleOverride != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   361
                                checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   362
                                defaultModule = moduleFinder.findModule(names.fromString(moduleOverride));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   363
                            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   364
                                // Question: why not do findAllModules and initVisiblePackages here?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   365
                                // i.e. body of unnamedModuleCompleter
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   366
                                defaultModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   367
                                defaultModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   368
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   369
                        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   370
                            checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleClasspath);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   371
                            checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   372
                            defaultModule.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   373
                            // Question: why not do completeModule here?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   374
                            defaultModule.completer = new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   375
                                @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   376
                                public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   377
                                    completeModule((ModuleSymbol) sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   378
                                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   379
                            };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   380
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   381
                        rootModules.add(defaultModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   382
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   383
                    case 1:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   384
                        checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleSourcepath);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                        checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   386
                        defaultModule = rootModules.iterator().next();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   387
                        defaultModule.classLocation = StandardLocation.CLASS_OUTPUT;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   388
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   389
                    default:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   390
                        Assert.error("too many modules");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   392
                defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   393
            } else if (rootModules.size() == 1 && defaultModule == rootModules.iterator().next()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                defaultModule.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                defaultModule.completer = sym -> completeModule((ModuleSymbol) sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   396
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   397
                Assert.check(rootModules.isEmpty());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   398
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   399
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   400
            if (defaultModule != syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                syms.unnamedModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   402
                syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   404
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   405
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   406
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   407
                tree.modle = defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   408
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   409
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   410
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   411
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   412
    private Location getModuleLocation(JCCompilationUnit tree) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   413
        switch (tree.defs.head.getTag()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   414
            case MODULEDEF:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   415
                return getModuleLocation(tree.sourcefile, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   416
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   417
            case PACKAGEDEF:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   418
                JCPackageDecl pkg = (JCPackageDecl) tree.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   419
                return getModuleLocation(tree.sourcefile, TreeInfo.fullName(pkg.pid));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   420
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   421
            default:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   422
                // code in unnamed module
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   423
                return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   424
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   425
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   426
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   427
    private Location getModuleLocation(JavaFileObject fo, Name pkgName) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   428
        // For now, just check module source path.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   429
        // We may want to check source path as well.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   430
        return fileManager.getModuleLocation(StandardLocation.MODULE_SOURCE_PATH,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   431
                fo, (pkgName == null) ? null : pkgName.toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   432
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   433
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   434
    private void checkSpecifiedModule(List<JCCompilationUnit> trees, JCDiagnostic.Error error) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   435
        if (moduleOverride != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   436
            JavaFileObject prev = log.useSource(trees.head.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   437
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   438
                log.error(trees.head.pos(), error);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   439
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   440
                log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   442
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   443
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   444
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   445
    private void checkNoAllModulePath() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   446
        if (addModsOpt != null && Arrays.asList(addModsOpt.split(",")).contains(ALL_MODULE_PATH)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   447
            log.error(Errors.AddmodsAllModulePathInvalid);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   448
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   449
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   450
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   451
    private final Completer mainCompleter = new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   452
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   453
        public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   454
            ModuleSymbol msym = moduleFinder.findModule((ModuleSymbol) sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   455
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   456
            if (msym.kind == Kinds.Kind.ERR) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   457
                log.error(Errors.CantFindModule(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   458
                //make sure the module is initialized:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   459
                msym.directives = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   460
                msym.exports = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   461
                msym.provides = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   462
                msym.requires = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   463
                msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   464
            } else if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   465
                completeAutomaticModule(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   466
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   467
                msym.module_info.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   468
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   469
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   470
            // If module-info comes from a .java file, the underlying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   471
            // call of classFinder.fillIn will have called through the
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   472
            // source completer, to Enter, and then to Modules.enter,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   473
            // which will call completeModule.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   474
            // But, if module-info comes from a .class file, the underlying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   475
            // call of classFinder.fillIn will just call ClassReader to read
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   476
            // the .class file, and so we call completeModule here.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   477
            if (msym.module_info.classfile == null || msym.module_info.classfile.getKind() == Kind.CLASS) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   478
                completeModule(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   479
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   480
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   481
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   482
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   483
        public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   484
            return "mainCompleter";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   485
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   486
    };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   487
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   488
    private void completeAutomaticModule(ModuleSymbol msym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   489
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   490
            ListBuffer<Directive> directives = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   491
            ListBuffer<ExportsDirective> exports = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   492
            Set<String> seenPackages = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   493
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   494
            for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                String binName = fileManager.inferBinaryName(msym.classLocation, clazz);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   496
                String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package????
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                if (seenPackages.add(pack)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   498
                    ExportsDirective d = new ExportsDirective(syms.enterPackage(msym, names.fromString(pack)), null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   499
                    directives.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   500
                    exports.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   501
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   502
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   503
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   504
            ListBuffer<RequiresDirective> requires = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   505
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   506
            //ensure all modules are found:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   507
            moduleFinder.findAllModules();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   508
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   509
            for (ModuleSymbol ms : allModules()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   510
                if (ms == syms.unnamedModule || ms == msym)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   511
                    continue;
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
   512
                Set<RequiresFlag> flags = (ms.flags_field & Flags.AUTOMATIC_MODULE) != 0 ?
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
   513
                        EnumSet.of(RequiresFlag.PUBLIC) : EnumSet.noneOf(RequiresFlag.class);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
   514
                RequiresDirective d = new RequiresDirective(ms, flags);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   515
                directives.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   516
                requires.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   517
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   518
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   519
            RequiresDirective requiresUnnamed = new RequiresDirective(syms.unnamedModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   520
            directives.add(requiresUnnamed);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   521
            requires.add(requiresUnnamed);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   522
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   523
            msym.exports = exports.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   524
            msym.provides = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            msym.requires = requires.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   527
            msym.directives = directives.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   528
            msym.flags_field |= Flags.ACYCLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   529
        } catch (IOException ex) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   530
            throw new IllegalStateException(ex);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   531
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   532
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   533
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   534
    private Completer getSourceCompleter(JCCompilationUnit tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   535
        return new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   536
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   537
            public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   538
                ModuleSymbol msym = (ModuleSymbol) sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   539
                msym.flags_field |= UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   540
                ModuleVisitor v = new ModuleVisitor();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   541
                JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   542
                try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   543
                    tree.defs.head.accept(v);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   544
                    completeModule(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   545
                    checkCyclicDependencies((JCModuleDecl) tree.defs.head);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   546
                } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   547
                    log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   548
                    msym.flags_field &= ~UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   549
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   550
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   551
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   552
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   553
            public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   554
                return "SourceCompleter: " + tree.sourcefile.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   555
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   556
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   557
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   558
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   559
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   560
    class ModuleVisitor extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   561
        private ModuleSymbol sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   562
        private final Set<ModuleSymbol> allRequires = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   563
        private final Set<PackageSymbol> allExports = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   564
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   565
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   566
        public void visitModuleDef(JCModuleDecl tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   567
            sym = Assert.checkNonNull(tree.sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   568
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   569
            sym.requires = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   570
            sym.exports = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   571
            tree.directives.forEach(t -> t.accept(this));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   572
            sym.requires = sym.requires.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   573
            sym.exports = sym.exports.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   574
            ensureJavaBase();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   575
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   576
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   577
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   578
        public void visitRequires(JCRequires tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   579
            ModuleSymbol msym = lookupModule(tree.moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   580
            if (msym.kind != MDL) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   581
                log.error(tree.moduleName.pos(), Errors.ModuleNotFound(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   582
            } else if (allRequires.contains(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   583
                log.error(tree.moduleName.pos(), Errors.DuplicateRequires(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   584
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   585
                allRequires.add(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   586
                Set<RequiresFlag> flags = EnumSet.noneOf(RequiresFlag.class);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   587
                if (tree.isPublic)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   588
                    flags.add(RequiresFlag.PUBLIC);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   589
                RequiresDirective d = new RequiresDirective(msym, flags);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   590
                tree.directive = d;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   591
                sym.requires = sym.requires.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   592
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   593
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   594
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   595
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   596
        public void visitExports(JCExports tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   597
            Name name = TreeInfo.fullName(tree.qualid);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   598
            PackageSymbol packge = syms.enterPackage(sym, name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   599
            attr.setPackageSymbols(tree.qualid, packge);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   600
            if (!allExports.add(packge)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   601
                log.error(tree.qualid.pos(), Errors.DuplicateExports(packge));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   602
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   603
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   604
            List<ModuleSymbol> toModules = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   605
            if (tree.moduleNames != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   606
                Set<ModuleSymbol> to = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   607
                for (JCExpression n: tree.moduleNames) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   608
                    ModuleSymbol msym = lookupModule(n);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   609
                    if (msym.kind != MDL) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   610
                        log.error(n.pos(), Errors.ModuleNotFound(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   611
                    } else if (!to.add(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   612
                        log.error(n.pos(), Errors.DuplicateExports(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   613
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   614
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   615
                toModules = List.from(to);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   616
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   617
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   618
            if (toModules == null || !toModules.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   619
                ExportsDirective d = new ExportsDirective(packge, toModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   620
                tree.directive = d;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   621
                sym.exports = sym.exports.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   622
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   623
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   624
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   625
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   626
        public void visitProvides(JCProvides tree) { }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   627
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   628
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   629
        public void visitUses(JCUses tree) { }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   630
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   631
        private void ensureJavaBase() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   632
            if (sym.name == names.java_base)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   633
                return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   634
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   635
            for (RequiresDirective d: sym.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                if (d.module.name == names.java_base)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   637
                    return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   638
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   639
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   640
            ModuleSymbol java_base = syms.enterModule(names.java_base);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   641
            Directive.RequiresDirective d =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   642
                    new Directive.RequiresDirective(java_base,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   643
                            EnumSet.of(Directive.RequiresFlag.MANDATED));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   644
            sym.requires = sym.requires.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   645
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   646
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   647
        private ModuleSymbol lookupModule(JCExpression moduleName) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   648
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   649
            Name name = TreeInfo.fullName(moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   650
            ModuleSymbol msym = moduleFinder.findModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   651
            TreeInfo.setSymbol(moduleName, msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   652
            return msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   653
            } catch (Throwable t) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   654
                System.err.println("Module " + sym + "; lookup export " + moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   655
                throw t;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   656
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   657
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   658
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   659
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   660
    public Completer getUsesProvidesCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   661
        return sym -> {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   662
            ModuleSymbol msym = (ModuleSymbol) sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   663
            Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   664
            UsesProvidesVisitor v = new UsesProvidesVisitor(msym, env);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   665
            JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   666
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   667
                env.toplevel.defs.head.accept(v);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   668
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   669
                log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   670
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   671
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   672
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   673
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   674
    class UsesProvidesVisitor extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   675
        private final ModuleSymbol msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   676
        private final Env<AttrContext> env;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   677
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   678
        private final Set<Directive.UsesDirective> allUses = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   679
        private final Set<Directive.ProvidesDirective> allProvides = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   680
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   681
        public UsesProvidesVisitor(ModuleSymbol msym, Env<AttrContext> env) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   682
            this.msym = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   683
            this.env = env;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   684
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   685
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   686
        @Override @SuppressWarnings("unchecked")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   687
        public void visitModuleDef(JCModuleDecl tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   688
            msym.directives = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   689
            msym.provides = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   690
            msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   691
            tree.directives.forEach(t -> t.accept(this));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   692
            msym.directives = msym.directives.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   693
            msym.provides = msym.provides.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   694
            msym.uses = msym.uses.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   695
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   696
            if (msym.requires.nonEmpty() && msym.requires.head.flags.contains(RequiresFlag.MANDATED))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   697
                msym.directives = msym.directives.prepend(msym.requires.head);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   698
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   699
            msym.directives = msym.directives.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   700
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   701
            checkForCorrectness();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   702
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   703
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   704
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   705
        public void visitExports(JCExports tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   706
            if (tree.directive.packge.members().isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   707
                log.error(tree.qualid.pos(), Errors.PackageEmptyOrNotFound(tree.directive.packge));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   708
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   709
            msym.directives = msym.directives.prepend(tree.directive);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   710
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   711
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   712
        MethodSymbol noArgsConstructor(ClassSymbol tsym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   713
            for (Symbol sym : tsym.members().getSymbolsByName(names.init)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   714
                MethodSymbol mSym = (MethodSymbol)sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   715
                if (mSym.params().isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   716
                    return mSym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   717
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   718
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   719
            return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   720
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   721
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   722
        Map<Directive.ProvidesDirective, JCProvides> directiveToTreeMap = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   723
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   724
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   725
        public void visitProvides(JCProvides tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   726
            Type st = attr.attribType(tree.serviceName, env, syms.objectType);
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   727
            Type it = attr.attribType(tree.implName, env, syms.objectType);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   728
            ClassSymbol service = (ClassSymbol) st.tsym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   729
            ClassSymbol impl = (ClassSymbol) it.tsym;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   730
            if (!types.isSubtype(it, st)) {
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   731
                log.error(tree.implName.pos(), Errors.ServiceImplementationMustBeSubtypeOfServiceInterface);
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   732
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   733
            if ((impl.flags() & ABSTRACT) != 0) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   734
                log.error(tree.implName.pos(), Errors.ServiceImplementationIsAbstract(impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   735
            } else if (impl.isInner()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   736
                log.error(tree.implName.pos(), Errors.ServiceImplementationIsInner(impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   737
            } else if (service.isInner()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   738
                log.error(tree.serviceName.pos(), Errors.ServiceDefinitionIsInner(service));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   739
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   740
                MethodSymbol constr = noArgsConstructor(impl);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   741
                if (constr == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   742
                    log.error(tree.implName.pos(), Errors.ServiceImplementationDoesntHaveANoArgsConstructor(impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   743
                } else if ((constr.flags() & PUBLIC) == 0) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   744
                    log.error(tree.implName.pos(), Errors.ServiceImplementationNoArgsConstructorNotPublic(impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   745
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   746
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   747
            if (st.hasTag(CLASS) && it.hasTag(CLASS)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   748
                Directive.ProvidesDirective d = new Directive.ProvidesDirective(service, impl);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   749
                if (!allProvides.add(d)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   750
                    log.error(tree.pos(), Errors.DuplicateProvides(service, impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   751
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   752
                msym.provides = msym.provides.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   753
                msym.directives = msym.directives.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   754
                directiveToTreeMap.put(d, tree);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   755
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   756
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   757
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   758
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   759
        public void visitRequires(JCRequires tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   760
            msym.directives = msym.directives.prepend(tree.directive);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   761
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   762
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   763
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   764
        public void visitUses(JCUses tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   765
            Type st = attr.attribType(tree.qualid, env, syms.objectType);
37854
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   766
            Symbol sym = TreeInfo.symbol(tree.qualid);
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   767
            if ((sym.flags() & ENUM) != 0) {
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   768
                log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym));
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   769
            } else if (st.hasTag(CLASS)) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   770
                ClassSymbol service = (ClassSymbol) st.tsym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   771
                Directive.UsesDirective d = new Directive.UsesDirective(service);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   772
                if (!allUses.add(d)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   773
                    log.error(tree.pos(), Errors.DuplicateUses(service));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   774
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   775
                msym.uses = msym.uses.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   776
                msym.directives = msym.directives.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   777
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   778
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   779
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   780
        private void checkForCorrectness() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   781
            for (Directive.ProvidesDirective provides : allProvides) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   782
                JCProvides tree = directiveToTreeMap.get(provides);
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   783
                /* The implementation must be defined in the same module as the provides directive
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   784
                 * (else, error)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   785
                 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   786
                PackageSymbol implementationDefiningPackage = provides.impl.packge();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   787
                if (implementationDefiningPackage.modle != msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   788
                    log.error(tree.pos(), Errors.ServiceImplementationNotInRightModule(implementationDefiningPackage.modle));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   789
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   790
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   791
                /* There is no inherent requirement that module that provides a service should actually
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   792
                 * use it itself. However, it is a pointless declaration if the service package is not
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   793
                 * exported and there is no uses for the service.
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   794
                 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   795
                PackageSymbol interfaceDeclaringPackage = provides.service.packge();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   796
                boolean isInterfaceDeclaredInCurrentModule = interfaceDeclaringPackage.modle == msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   797
                boolean isInterfaceExportedFromAReadableModule =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   798
                        msym.visiblePackages.get(interfaceDeclaringPackage.fullname) == interfaceDeclaringPackage;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   799
                if (isInterfaceDeclaredInCurrentModule && !isInterfaceExportedFromAReadableModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   800
                    // ok the interface is declared in this module. Let's check if it's exported
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   801
                    boolean warn = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   802
                    for (ExportsDirective export : msym.exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   803
                        if (interfaceDeclaringPackage == export.packge) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   804
                            warn = false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   805
                            break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   806
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   807
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   808
                    if (warn) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   809
                        for (UsesDirective uses : msym.uses) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   810
                            if (provides.service == uses.service) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   811
                                warn = false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   812
                                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   813
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   814
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   815
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   816
                    if (warn) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   817
                        log.warning(tree.pos(), Warnings.ServiceProvidedButNotExportedOrUsed(provides.service));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   818
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   819
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   820
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   821
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   822
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   823
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   824
    private Set<ModuleSymbol> allModulesCache;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   825
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   826
    private Set<ModuleSymbol> allModules() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   827
        if (allModulesCache != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   828
            return allModulesCache;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   829
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   830
        Set<ModuleSymbol> observable;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   831
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   832
        if (limitModsOpt == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   833
            observable = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   834
        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   835
            Set<ModuleSymbol> limitMods = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   836
            for (String limit : limitModsOpt.split(",")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   837
                limitMods.add(syms.enterModule(names.fromString(limit)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   838
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   839
            observable = computeTransitiveClosure(limitMods, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   840
            observable.addAll(rootModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   841
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   842
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   843
        Predicate<ModuleSymbol> observablePred = sym -> observable == null || observable.contains(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   844
        Predicate<ModuleSymbol> systemModulePred = sym -> (sym.flags() & Flags.SYSTEM_MODULE) != 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   845
        Set<ModuleSymbol> enabledRoot = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   846
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   847
        if (rootModules.contains(syms.unnamedModule)) {
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   848
            ModuleSymbol javaSE = syms.getModule(java_se);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   849
            Predicate<ModuleSymbol> jdkModulePred;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   850
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   851
            if (javaSE != null && (observable == null || observable.contains(javaSE))) {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   852
                jdkModulePred = sym -> {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   853
                    sym.complete();
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   854
                    return   !sym.name.startsWith(java_)
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   855
                           && sym.exports.stream().anyMatch(e -> e.modules == null);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   856
                };
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   857
                enabledRoot.add(javaSE);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   858
            } else {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   859
                jdkModulePred = sym -> true;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   860
            }
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   861
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   862
            for (ModuleSymbol sym : new HashSet<>(syms.getAllModules())) {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   863
                if (systemModulePred.test(sym) && observablePred.test(sym) && jdkModulePred.test(sym)) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   864
                    enabledRoot.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   865
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   866
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   867
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   868
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   869
        enabledRoot.addAll(rootModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   870
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   871
        if (addModsOpt != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   872
            for (String added : addModsOpt.split(",")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   873
                Stream<ModuleSymbol> modules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   874
                switch (added) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   875
                    case ALL_SYSTEM:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   876
                        modules = syms.getAllModules()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   877
                                      .stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   878
                                      .filter(systemModulePred.and(observablePred));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   879
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   880
                    case ALL_MODULE_PATH:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   881
                        modules = syms.getAllModules()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   882
                                      .stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   883
                                      .filter(systemModulePred.negate().and(observablePred));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   884
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   885
                    default:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   886
                        modules = Stream.of(syms.enterModule(names.fromString(added)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   887
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   888
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   889
                modules.forEach(sym -> {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   890
                    enabledRoot.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   891
                    if (observable != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   892
                        observable.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   893
                });
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   894
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   895
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   896
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   897
        Set<ModuleSymbol> result = computeTransitiveClosure(enabledRoot, observable);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   898
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   899
        result.add(syms.unnamedModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   900
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   901
        if (!rootModules.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   902
            allModulesCache = result;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   903
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   904
        return result;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   905
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   906
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   907
    public void enableAllModules() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   908
        allModulesCache = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   909
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   910
        moduleFinder.findAllModules();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   911
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   912
        for (ModuleSymbol msym : syms.getAllModules()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   913
            allModulesCache.add(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   914
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   915
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   916
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   917
    private Set<ModuleSymbol> computeTransitiveClosure(Iterable<? extends ModuleSymbol> base, Set<ModuleSymbol> observable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   918
        List<ModuleSymbol> todo = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   919
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   920
        for (ModuleSymbol ms : base) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   921
            todo = todo.prepend(ms);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   922
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   923
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   924
        Set<ModuleSymbol> result = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   925
        result.add(syms.java_base);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   926
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   927
        while (todo.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   928
            ModuleSymbol current = todo.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   929
            todo = todo.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   930
            if (observable != null && !observable.contains(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   931
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   932
            if (!result.add(current) || current == syms.unnamedModule || ((current.flags_field & Flags.AUTOMATIC_MODULE) != 0))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   933
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   934
            current.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   935
            for (RequiresDirective rd : current.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   936
                todo = todo.prepend(rd.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   937
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   938
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   939
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   940
        return result;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   941
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   942
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   943
    public ModuleSymbol getObservableModule(Name name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   944
        ModuleSymbol mod = syms.getModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   945
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   946
        if (allModules().contains(mod)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   947
            return mod;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   948
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   949
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   950
        return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   951
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   952
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   953
    private Completer getUnnamedModuleCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   954
        moduleFinder.findAllModules();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   955
        return new Symbol.Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   956
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   957
            public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   958
                ModuleSymbol msym = (ModuleSymbol) sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   959
                Set<ModuleSymbol> allModules = allModules();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   960
                for (ModuleSymbol m : allModules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   961
                    m.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   962
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   963
                initVisiblePackages(msym, allModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   964
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   965
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   966
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   967
            public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   968
                return "unnamedModule Completer";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   969
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   970
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   971
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   972
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   973
    private final Map<ModuleSymbol, Set<ModuleSymbol>> requiresPublicCache = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   974
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   975
    private void completeModule(ModuleSymbol msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   976
        Assert.checkNonNull(msym.requires);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   977
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   978
        initAddReads();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   979
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   980
        msym.requires = msym.requires.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   981
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   982
        List<RequiresDirective> requires = msym.requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   983
        List<RequiresDirective> previous = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   984
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   985
        while (requires.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   986
            if (!allModules().contains(requires.head.module)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   987
                Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   988
                if (env != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   989
                    JavaFileObject origSource = log.useSource(env.toplevel.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   990
                    try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   991
                        log.error(/*XXX*/env.tree, Errors.ModuleNotFound(requires.head.module));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   992
                    } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   993
                        log.useSource(origSource);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   994
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   995
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   996
                    Assert.check((msym.flags() & Flags.AUTOMATIC_MODULE) == 0);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   997
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   998
                if (previous != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   999
                    previous.tail = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1000
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1001
                    msym.requires.tail = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1002
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1003
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1004
                previous = requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1005
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1006
            requires = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1007
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1008
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1009
        Set<ModuleSymbol> readable = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1010
        Set<ModuleSymbol> requiresPublic = new HashSet<>();
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1011
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1012
        for (RequiresDirective d : msym.requires) {
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1013
            d.module.complete();
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1014
            readable.add(d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1015
            Set<ModuleSymbol> s = retrieveRequiresPublic(d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1016
            Assert.checkNonNull(s, () -> "no entry in cache for " + d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1017
            readable.addAll(s);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1018
            if (d.flags.contains(RequiresFlag.PUBLIC)) {
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1019
                requiresPublic.add(d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1020
                requiresPublic.addAll(s);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1021
            }
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1022
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1023
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1024
        requiresPublicCache.put(msym, requiresPublic);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1025
        initVisiblePackages(msym, readable);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1026
        for (ExportsDirective d: msym.exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1027
            d.packge.modle = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1028
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1029
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1030
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1031
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1032
    private Set<ModuleSymbol> retrieveRequiresPublic(ModuleSymbol msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1033
        Set<ModuleSymbol> requiresPublic = requiresPublicCache.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1034
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1035
        if (requiresPublic == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1036
            //the module graph may contain cycles involving automatic modules or -XaddReads edges
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1037
            requiresPublic = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1038
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1039
            Set<ModuleSymbol> seen = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1040
            List<ModuleSymbol> todo = List.of(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1041
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1042
            while (todo.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1043
                ModuleSymbol current = todo.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1044
                todo = todo.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1045
                if (!seen.add(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1046
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1047
                requiresPublic.add(current);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1048
                current.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1049
                Iterable<? extends RequiresDirective> requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1050
                if (current != syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1051
                    Assert.checkNonNull(current.requires, () -> current + ".requires == null; " + msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1052
                    requires = current.requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1053
                    for (RequiresDirective rd : requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1054
                        if (rd.isPublic())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1055
                            todo = todo.prepend(rd.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1056
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1057
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1058
                    for (ModuleSymbol mod : allModules()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1059
                        todo = todo.prepend(mod);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1060
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1061
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1062
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1063
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1064
            requiresPublic.remove(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1065
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1066
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1067
        return requiresPublic;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1068
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1069
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1070
    private void initVisiblePackages(ModuleSymbol msym, Collection<ModuleSymbol> readable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1071
        initAddExports();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1072
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1073
        msym.visiblePackages = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1074
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1075
        Map<Name, ModuleSymbol> seen = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1076
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1077
        for (ModuleSymbol rm : readable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1078
            if (rm == syms.unnamedModule)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1079
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1080
            addVisiblePackages(msym, seen, rm, rm.exports);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1081
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1082
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1083
        for (Entry<ModuleSymbol, Set<ExportsDirective>> addExportsEntry : addExports.entrySet())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1084
            addVisiblePackages(msym, seen, addExportsEntry.getKey(), addExportsEntry.getValue());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1085
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1086
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1087
    private void addVisiblePackages(ModuleSymbol msym,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1088
                                    Map<Name, ModuleSymbol> seenPackages,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1089
                                    ModuleSymbol exportsFrom,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1090
                                    Collection<ExportsDirective> exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1091
        for (ExportsDirective d : exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1092
            if (d.modules == null || d.modules.contains(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1093
                Name packageName = d.packge.fullname;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1094
                ModuleSymbol previousModule = seenPackages.get(packageName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1095
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1096
                if (previousModule != null && previousModule != exportsFrom) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1097
                    Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1098
                    JavaFileObject origSource = env != null ? log.useSource(env.toplevel.sourcefile)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1099
                                                            : null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1100
                    DiagnosticPosition pos = env != null ? env.tree.pos() : null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1101
                    try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1102
                        log.error(pos, Errors.PackageClashFromRequires(msym, packageName,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1103
                                                                      previousModule, exportsFrom));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1104
                    } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1105
                        if (env != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1106
                            log.useSource(origSource);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1107
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1108
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1109
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1110
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1111
                seenPackages.put(packageName, exportsFrom);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1112
                msym.visiblePackages.put(d.packge.fullname, d.packge);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1113
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1114
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1115
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1116
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1117
    private void initAddExports() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1118
        if (addExports != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1119
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1120
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1121
        addExports = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1122
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1123
        if (addExportsOpt == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1124
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1125
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1126
//        System.err.println("Modules.addExports:\n   " + addExportsOpt.replace("\0", "\n   "));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1127
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1128
        Pattern ep = Pattern.compile("([^/]+)/([^=]+)=(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1129
        for (String s: addExportsOpt.split("\0+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1130
            if (s.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1131
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1132
            Matcher em = ep.matcher(s);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1133
            if (!em.matches()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1134
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1135
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1136
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1137
            // Terminology comes from
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1138
            //  -XaddExports:module/package=target,...
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1139
            // Compare to
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1140
            //  module module { exports package to target, ... }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1141
            String moduleName = em.group(1);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1142
            String packageName = em.group(2);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1143
            String targetNames = em.group(3);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1144
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1145
            ModuleSymbol msym = syms.enterModule(names.fromString(moduleName));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1146
            PackageSymbol p = syms.enterPackage(msym, names.fromString(packageName));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1147
            p.modle = msym;  // TODO: do we need this?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1148
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1149
            List<ModuleSymbol> targetModules = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1150
            for (String toModule : targetNames.split("[ ,]+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1151
                ModuleSymbol m;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1152
                if (toModule.equals("ALL-UNNAMED")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1153
                    m = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1154
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1155
                    if (!SourceVersion.isName(toModule)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1156
                        // TODO: error: invalid module name
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1157
                        continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1158
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1159
                    m = syms.enterModule(names.fromString(toModule));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1160
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1161
                targetModules = targetModules.prepend(m);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1162
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1163
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1164
            Set<ExportsDirective> extra = addExports.computeIfAbsent(msym, _x -> new LinkedHashSet<>());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1165
            ExportsDirective d = new ExportsDirective(p, targetModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1166
            extra.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1167
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1168
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1169
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1170
    private void initAddReads() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1171
        if (addReads != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1172
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1173
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1174
        addReads = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1175
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1176
        if (addReadsOpt == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1177
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1178
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1179
//        System.err.println("Modules.addReads:\n   " + addReadsOpt.replace("\0", "\n   "));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1180
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1181
        Pattern rp = Pattern.compile("([^=]+)=(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1182
        for (String s : addReadsOpt.split("\0+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1183
            if (s.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1184
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1185
            Matcher rm = rp.matcher(s);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1186
            if (!rm.matches()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1187
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1188
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1189
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1190
            // Terminology comes from
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1191
            //  -XaddReads:target-module=source-module,...
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1192
            // Compare to
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1193
            //  module target-module { requires source-module; ... }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1194
            String targetName = rm.group(1);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1195
            String sources = rm.group(2);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1196
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1197
            ModuleSymbol msym = syms.enterModule(names.fromString(targetName));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1198
            for (String source : sources.split("[ ,]+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1199
                ModuleSymbol sourceModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1200
                if (source.equals("ALL-UNNAMED")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1201
                    sourceModule = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1202
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1203
                    if (!SourceVersion.isName(source)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1204
                        // TODO: error: invalid module name
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1205
                        continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1206
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1207
                    sourceModule = syms.enterModule(names.fromString(source));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1208
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1209
                addReads.computeIfAbsent(msym, m -> new HashSet<>())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1210
                        .add(new RequiresDirective(sourceModule, EnumSet.of(RequiresFlag.EXTRA)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1211
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1212
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1213
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1214
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1215
    private void checkCyclicDependencies(JCModuleDecl mod) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1216
        for (JCDirective d : mod.directives) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1217
            if (!d.hasTag(Tag.REQUIRES))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1218
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1219
            JCRequires rd = (JCRequires) d;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1220
            Set<ModuleSymbol> nonSyntheticDeps = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1221
            List<ModuleSymbol> queue = List.of(rd.directive.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1222
            while (queue.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1223
                ModuleSymbol current = queue.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1224
                queue = queue.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1225
                if (!nonSyntheticDeps.add(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1226
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1227
                if ((current.flags() & Flags.ACYCLIC) != 0)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1228
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1229
                current.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1230
                Assert.checkNonNull(current.requires, () -> current.toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1231
                for (RequiresDirective dep : current.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1232
                    if (!dep.flags.contains(RequiresFlag.EXTRA))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1233
                        queue = queue.prepend(dep.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1234
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1235
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1236
            if (nonSyntheticDeps.contains(mod.sym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1237
                log.error(rd.moduleName.pos(), Errors.CyclicRequires(rd.directive.module));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1238
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1239
            mod.sym.flags_field |= Flags.ACYCLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1240
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1241
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1242
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1243
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1244
    private String toString(ModuleSymbol msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1245
        return msym.name + "["
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1246
                + "kind:" + msym.kind + ";"
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1247
                + "locn:" + toString(msym.sourceLocation) + "," + toString(msym.classLocation) + ";"
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1248
                + "info:" + toString(msym.module_info.sourcefile) + ","
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1249
                            + toString(msym.module_info.classfile) + ","
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1250
                            + msym.module_info.completer
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1251
                + "]";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1252
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1253
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1254
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1255
    String toString(Location locn) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1256
        return (locn == null) ? "--" : locn.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1257
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1258
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1259
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1260
    String toString(JavaFileObject fo) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1261
        return (fo == null) ? "--" : fo.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1262
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1263
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1264
    public void newRound() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1265
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1266
}