langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java
author mcimadamore
Mon, 07 Nov 2016 11:16:23 +0000
changeset 41942 260c724e5614
parent 41938 8e66bf10fcec
child 41999 dbf796210eff
permissions -rw-r--r--
8168854: javac erroneously reject a a service interface inner class in a provides clause Summary: Java SE 9 proposed spec changes do not explicitly forbid a service interface to be an inner class - javac should follow 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.Set;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
    40
import java.util.function.Consumer;
36526
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;
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
    61
import com.sun.tools.javac.code.Lint.LintCategory;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
import com.sun.tools.javac.code.ModuleFinder;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
import com.sun.tools.javac.code.Source;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
import com.sun.tools.javac.code.Symbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
import com.sun.tools.javac.code.Symbol.ClassSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
import com.sun.tools.javac.code.Symbol.Completer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
import com.sun.tools.javac.code.Symbol.CompletionFailure;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
import com.sun.tools.javac.code.Symbol.MethodSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
import com.sun.tools.javac.code.Symbol.PackageSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
import com.sun.tools.javac.code.Symtab;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
import com.sun.tools.javac.code.Type;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
    73
import com.sun.tools.javac.code.Types;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
import com.sun.tools.javac.jvm.ClassWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
import com.sun.tools.javac.jvm.JNIWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
import com.sun.tools.javac.main.Option;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
import com.sun.tools.javac.resources.CompilerProperties.Errors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
import com.sun.tools.javac.tree.JCTree;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
import com.sun.tools.javac.tree.JCTree.JCExports;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
import com.sun.tools.javac.tree.JCTree.JCExpression;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
import com.sun.tools.javac.tree.JCTree.JCPackageDecl;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
import com.sun.tools.javac.tree.JCTree.JCProvides;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
import com.sun.tools.javac.tree.JCTree.JCRequires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
import com.sun.tools.javac.tree.JCTree.JCUses;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
import com.sun.tools.javac.tree.TreeInfo;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
import com.sun.tools.javac.util.Assert;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
import com.sun.tools.javac.util.Context;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    91
import com.sun.tools.javac.util.JCDiagnostic;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
import com.sun.tools.javac.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
import com.sun.tools.javac.util.ListBuffer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
import com.sun.tools.javac.util.Log;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
import com.sun.tools.javac.util.Name;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
import com.sun.tools.javac.util.Names;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
import com.sun.tools.javac.util.Options;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
import static com.sun.tools.javac.code.Flags.UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
import static com.sun.tools.javac.code.Kinds.Kind.MDL;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
import static com.sun.tools.javac.code.TypeTag.CLASS;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
import com.sun.tools.javac.tree.JCTree.JCDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
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
   106
import com.sun.tools.javac.util.Abort;
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   107
import com.sun.tools.javac.util.Position;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
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
   110
import static com.sun.tools.javac.code.Flags.ENUM;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
import static com.sun.tools.javac.code.Flags.PUBLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
import static com.sun.tools.javac.tree.JCTree.Tag.MODULEDEF;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
/**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   115
 *  TODO: fill in
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   116
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   117
 *  <p><b>This is NOT part of any supported API.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
 *  If you write code that depends on this, you do so at your own risk.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
 *  This code and its internal interfaces are subject to change or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
 *  deletion without notice.</b>
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
public class Modules extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
    private static final String ALL_SYSTEM = "ALL-SYSTEM";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
    private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
    private final Log log;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
    private final Names names;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    private final Symtab syms;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
    private final Attr attr;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
    private final TypeEnvs typeEnvs;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   131
    private final Types types;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
    private final JavaFileManager fileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
    private final ModuleFinder moduleFinder;
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   134
    private final Source source;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    private final boolean allowModules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    public final boolean multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   138
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    private final String moduleOverride;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   140
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   141
    private final Name java_se;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   142
    private final Name java_;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   143
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    ModuleSymbol defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   145
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    private final String addExportsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
    private Map<ModuleSymbol, Set<ExportsDirective>> addExports;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
    private final String addReadsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   149
    private Map<ModuleSymbol, Set<RequiresDirective>> addReads;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   150
    private final String addModsOpt;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   151
    private final Set<String> extraAddMods = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
    private final String limitModsOpt;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   153
    private final Set<String> extraLimitMods = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   155
    private final boolean lintOptions;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   156
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   157
    private Set<ModuleSymbol> rootModules = null;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   158
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   159
    public static Modules instance(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        Modules instance = context.get(Modules.class);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   161
        if (instance == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   162
            instance = new Modules(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   163
        return instance;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   164
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   165
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   166
    protected Modules(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   167
        context.put(Modules.class, this);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        log = Log.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        names = Names.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
        syms = Symtab.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        attr = Attr.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        typeEnvs = TypeEnvs.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        moduleFinder = ModuleFinder.instance(context);
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   174
        types = Types.instance(context);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        fileManager = context.get(JavaFileManager.class);
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   176
        source = Source.instance(context);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   177
        allowModules = source.allowModules();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        Options options = Options.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   180
        lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   181
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        moduleOverride = options.get(Option.XMODULE);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        ClassWriter classWriter = ClassWriter.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        classWriter.multiModuleMode = multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        JNIWriter jniWriter = JNIWriter.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        jniWriter.multiModuleMode = multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   190
        java_se = names.fromString("java.se");
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   191
        java_ = names.fromString("java.");
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   192
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   193
        addExportsOpt = options.get(Option.ADD_EXPORTS);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   194
        addReadsOpt = options.get(Option.ADD_READS);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   195
        addModsOpt = options.get(Option.ADD_MODULES);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   196
        limitModsOpt = options.get(Option.LIMIT_MODULES);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   198
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   199
    int depth = -1;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   200
    private void dprintln(String msg) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        for (int i = 0; i < depth; i++)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   202
            System.err.print("  ");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        System.err.println(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   204
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   205
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   206
    public void addExtraAddModules(String... extras) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   207
        extraAddMods.addAll(Arrays.asList(extras));
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   208
    }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   209
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   210
    public void addExtraLimitModules(String... extras) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   211
        extraLimitMods.addAll(Arrays.asList(extras));
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   212
    }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   213
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   214
    boolean inInitModules;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   215
    public void initModules(List<JCCompilationUnit> trees) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   216
        Assert.check(!inInitModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   217
        try {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   218
            inInitModules = true;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   219
            Assert.checkNull(rootModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   220
            enter(trees, modules -> {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   221
                Assert.checkNull(rootModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   222
                Assert.checkNull(allModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   223
                this.rootModules = modules;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   224
                setupAllModules(); //initialize the module graph
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   225
                Assert.checkNonNull(allModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   226
                inInitModules = false;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   227
            }, null);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   228
        } finally {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   229
            inInitModules = false;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   230
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   231
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   232
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   233
    public boolean enter(List<JCCompilationUnit> trees, ClassSymbol c) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   234
        Assert.check(rootModules != null || inInitModules || !allowModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   235
        return enter(trees, modules -> {}, c);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   236
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   237
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   238
    private boolean enter(List<JCCompilationUnit> trees, Consumer<Set<ModuleSymbol>> init, ClassSymbol c) {
38619
27c0007bb28d 8152785: Remove javac -XDnoModules
vromero
parents: 37854
diff changeset
   239
        if (!allowModules) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   241
                tree.modle = syms.noModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   242
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   243
            defaultModule = syms.noModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            return true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   246
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   247
        int startErrors = log.nerrors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   248
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   249
        depth++;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   251
            // scan trees for module defs
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   252
            Set<ModuleSymbol> roots = enterModules(trees, c);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   253
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   254
            setCompilationUnitModules(trees, roots);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   255
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   256
            init.accept(roots);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   257
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   258
            for (ModuleSymbol msym: roots) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
                msym.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            }
39361
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   261
        } catch (CompletionFailure ex) {
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   262
            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
   263
            if (ex instanceof ClassFinder.BadClassFile) throw new Abort();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
        } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   265
            depth--;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   266
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   267
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        return (log.nerrors == startErrors);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   269
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   270
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   271
    public Completer getCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   272
        return mainCompleter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   273
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   274
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   275
    public ModuleSymbol getDefaultModule() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   276
        return defaultModule;
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
    private Set<ModuleSymbol> enterModules(List<JCCompilationUnit> trees, ClassSymbol c) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   280
        Set<ModuleSymbol> modules = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   281
        for (JCCompilationUnit tree : trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   282
            JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   283
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   284
                enterModule(tree, c, modules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
                log.useSource(prev);
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
        return modules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   290
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   291
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   292
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   293
    private void enterModule(JCCompilationUnit toplevel, ClassSymbol c, Set<ModuleSymbol> modules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        boolean isModuleInfo = toplevel.sourcefile.isNameCompatible("module-info", Kind.SOURCE);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   295
        boolean isModuleDecl = toplevel.defs.nonEmpty() && toplevel.defs.head.hasTag(MODULEDEF);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   296
        if (isModuleInfo && isModuleDecl) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            JCModuleDecl decl = (JCModuleDecl) toplevel.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            Name name = TreeInfo.fullName(decl.qualId);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            ModuleSymbol sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   300
            if (c != null) {
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   301
                sym = (ModuleSymbol) c.owner;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   302
                Assert.checkNonNull(sym.name);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   303
                Name treeName = TreeInfo.fullName(decl.qualId);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   304
                if (sym.name != treeName) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   305
                    log.error(decl.pos(), Errors.ModuleNameMismatch(name, sym.name));
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   306
                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   308
                sym = syms.enterModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                if (sym.module_info.sourcefile != null && sym.module_info.sourcefile != toplevel.sourcefile) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                    log.error(decl.pos(), Errors.DuplicateModule(sym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   311
                    return;
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
            sym.completer = getSourceCompleter(toplevel);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   315
            sym.module_info.sourcefile = toplevel.sourcefile;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   316
            decl.sym = sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   317
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            if (multiModuleMode || modules.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   319
                modules.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   320
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   321
                log.error(toplevel.pos(), Errors.TooManyModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   322
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   323
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   324
            Env<AttrContext> provisionalEnv = new Env<>(decl, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   325
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   326
            provisionalEnv.toplevel = toplevel;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   327
            typeEnvs.put(sym, provisionalEnv);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   328
        } else if (isModuleInfo) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   329
            if (multiModuleMode) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                JCTree tree = toplevel.defs.isEmpty() ? toplevel : toplevel.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                log.error(tree.pos(), Errors.ExpectedModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   332
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   333
        } else if (isModuleDecl) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   334
            JCTree tree = toplevel.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   335
            log.error(tree.pos(), Errors.ModuleDeclSbInModuleInfoJava);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   336
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   337
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   338
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   339
    private void setCompilationUnitModules(List<JCCompilationUnit> trees, Set<ModuleSymbol> rootModules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   340
        // update the module for each compilation unit
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   341
        if (multiModuleMode) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   342
            checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   343
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   344
                if (tree.defs.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   345
                    tree.modle = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   346
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   347
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   348
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   349
                JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   350
                try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   351
                    Location locn = getModuleLocation(tree);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   352
                    if (locn != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   353
                        Name name = names.fromString(fileManager.inferModuleName(locn));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   354
                        ModuleSymbol msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   355
                        if (tree.defs.head.hasTag(MODULEDEF)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   356
                            JCModuleDecl decl = (JCModuleDecl) tree.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   357
                            msym = decl.sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   358
                            if (msym.name != name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   359
                                log.error(decl.qualId, Errors.ModuleNameMismatch(msym.name, name));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   360
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   361
                        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   362
                            msym = syms.enterModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   363
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   364
                        if (msym.sourceLocation == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   365
                            msym.sourceLocation = locn;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   366
                            if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   367
                                msym.classLocation = fileManager.getModuleLocation(
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   368
                                        StandardLocation.CLASS_OUTPUT, msym.name.toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   369
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   370
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   371
                        tree.modle = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   372
                        rootModules.add(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   373
                    } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   374
                        log.error(tree.pos(), Errors.UnnamedPkgNotAllowedNamedModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   375
                        tree.modle = syms.errModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   376
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   377
                } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   378
                    throw new Error(e); // FIXME
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   379
                } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   380
                    log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   381
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   382
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   383
            if (syms.unnamedModule.sourceLocation == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   384
                syms.unnamedModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   386
                syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   387
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   388
            defaultModule = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   389
        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   390
            if (defaultModule == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                switch (rootModules.size()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   392
                    case 0:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   393
                        defaultModule = moduleFinder.findSingleModule();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                        if (defaultModule == syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                            if (moduleOverride != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                                checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   397
                                defaultModule = moduleFinder.findModule(names.fromString(moduleOverride));
40768
8b6a878d8773 8165008: javac -Xmodule compiles the module in a way that reads the unnamed module
jlahoda
parents: 40762
diff changeset
   398
                                defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   399
                            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   400
                                // Question: why not do findAllModules and initVisiblePackages here?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                                // i.e. body of unnamedModuleCompleter
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   402
                                defaultModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                                defaultModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   404
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   405
                        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   406
                            checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleClasspath);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   407
                            checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   408
                            defaultModule.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   409
                            // Question: why not do completeModule here?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   410
                            defaultModule.completer = new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   411
                                @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   412
                                public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   413
                                    completeModule((ModuleSymbol) sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   414
                                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   415
                            };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   416
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   417
                        rootModules.add(defaultModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   418
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   419
                    case 1:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   420
                        checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleSourcepath);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   421
                        checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   422
                        defaultModule = rootModules.iterator().next();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   423
                        defaultModule.classLocation = StandardLocation.CLASS_OUTPUT;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   424
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   425
                    default:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   426
                        Assert.error("too many modules");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   427
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   428
                defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   429
            } else if (rootModules.size() == 1 && defaultModule == rootModules.iterator().next()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   430
                defaultModule.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   431
                defaultModule.completer = sym -> completeModule((ModuleSymbol) sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   432
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   433
                Assert.check(rootModules.isEmpty());
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   434
                rootModules.add(defaultModule);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   435
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   436
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   437
            if (defaultModule != syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   438
                syms.unnamedModule.completer = getUnnamedModuleCompleter();
40768
8b6a878d8773 8165008: javac -Xmodule compiles the module in a way that reads the unnamed module
jlahoda
parents: 40762
diff changeset
   439
                if (moduleOverride == null) {
8b6a878d8773 8165008: javac -Xmodule compiles the module in a way that reads the unnamed module
jlahoda
parents: 40762
diff changeset
   440
                    syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH;
8b6a878d8773 8165008: javac -Xmodule compiles the module in a way that reads the unnamed module
jlahoda
parents: 40762
diff changeset
   441
                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   442
                syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
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
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   446
                tree.modle = defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   447
            }
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 Location getModuleLocation(JCCompilationUnit tree) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   452
        switch (tree.defs.head.getTag()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   453
            case MODULEDEF:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   454
                return getModuleLocation(tree.sourcefile, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   455
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   456
            case PACKAGEDEF:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   457
                JCPackageDecl pkg = (JCPackageDecl) tree.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   458
                return getModuleLocation(tree.sourcefile, TreeInfo.fullName(pkg.pid));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   459
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   460
            default:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   461
                // code in unnamed module
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   462
                return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   463
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   464
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   465
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   466
    private Location getModuleLocation(JavaFileObject fo, Name pkgName) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   467
        // For now, just check module source path.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   468
        // We may want to check source path as well.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   469
        return fileManager.getModuleLocation(StandardLocation.MODULE_SOURCE_PATH,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   470
                fo, (pkgName == null) ? null : pkgName.toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   471
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   472
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   473
    private void checkSpecifiedModule(List<JCCompilationUnit> trees, JCDiagnostic.Error error) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   474
        if (moduleOverride != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   475
            JavaFileObject prev = log.useSource(trees.head.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   476
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   477
                log.error(trees.head.pos(), error);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   478
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   479
                log.useSource(prev);
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
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   483
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   484
    private void checkNoAllModulePath() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   485
        if (addModsOpt != null && Arrays.asList(addModsOpt.split(",")).contains(ALL_MODULE_PATH)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   486
            log.error(Errors.AddmodsAllModulePathInvalid);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   487
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   488
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   489
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   490
    private final Completer mainCompleter = new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   491
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   492
        public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   493
            ModuleSymbol msym = moduleFinder.findModule((ModuleSymbol) sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   494
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   495
            if (msym.kind == Kinds.Kind.ERR) {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   496
                log.error(Errors.ModuleNotFound(msym));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                //make sure the module is initialized:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   498
                msym.directives = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   499
                msym.exports = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   500
                msym.provides = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   501
                msym.requires = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   502
                msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   503
            } else if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   504
                setupAutomaticModule(msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   505
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   506
                msym.module_info.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   507
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   508
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   509
            // If module-info comes from a .java file, the underlying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   510
            // call of classFinder.fillIn will have called through the
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   511
            // source completer, to Enter, and then to Modules.enter,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   512
            // which will call completeModule.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   513
            // But, if module-info comes from a .class file, the underlying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   514
            // call of classFinder.fillIn will just call ClassReader to read
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   515
            // the .class file, and so we call completeModule here.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   516
            if (msym.module_info.classfile == null || msym.module_info.classfile.getKind() == Kind.CLASS) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   517
                completeModule(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   518
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   519
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   520
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   521
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   522
        public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   523
            return "mainCompleter";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   524
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   525
    };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   526
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   527
    private void setupAutomaticModule(ModuleSymbol msym) throws CompletionFailure {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   528
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   529
            ListBuffer<Directive> directives = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   530
            ListBuffer<ExportsDirective> exports = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   531
            Set<String> seenPackages = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   532
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   533
            for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   534
                String binName = fileManager.inferBinaryName(msym.classLocation, clazz);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   535
                String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package????
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   536
                if (seenPackages.add(pack)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   537
                    ExportsDirective d = new ExportsDirective(syms.enterPackage(msym, names.fromString(pack)), null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   538
                    directives.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   539
                    exports.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   540
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   541
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   542
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   543
            msym.exports = exports.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   544
            msym.provides = List.nil();
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   545
            msym.requires = List.nil();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   547
            msym.directives = directives.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   548
            msym.flags_field |= Flags.ACYCLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   549
        } catch (IOException ex) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   550
            throw new IllegalStateException(ex);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   551
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   552
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   553
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   554
    private void completeAutomaticModule(ModuleSymbol msym) throws CompletionFailure {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   555
        ListBuffer<Directive> directives = new ListBuffer<>();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   556
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   557
        directives.addAll(msym.directives);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   558
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   559
        ListBuffer<RequiresDirective> requires = new ListBuffer<>();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   560
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   561
        for (ModuleSymbol ms : allModules()) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   562
            if (ms == syms.unnamedModule || ms == msym)
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   563
                continue;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   564
            Set<RequiresFlag> flags = (ms.flags_field & Flags.AUTOMATIC_MODULE) != 0 ?
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   565
                    EnumSet.of(RequiresFlag.PUBLIC) : EnumSet.noneOf(RequiresFlag.class);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   566
            RequiresDirective d = new RequiresDirective(ms, flags);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   567
            directives.add(d);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   568
            requires.add(d);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   569
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   570
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   571
        RequiresDirective requiresUnnamed = new RequiresDirective(syms.unnamedModule);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   572
        directives.add(requiresUnnamed);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   573
        requires.add(requiresUnnamed);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   574
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   575
        msym.requires = requires.toList();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   576
        msym.directives = directives.toList();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   577
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   578
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   579
    private Completer getSourceCompleter(JCCompilationUnit tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   580
        return new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   581
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   582
            public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   583
                ModuleSymbol msym = (ModuleSymbol) sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   584
                msym.flags_field |= UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   585
                ModuleVisitor v = new ModuleVisitor();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   586
                JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   587
                try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   588
                    tree.defs.head.accept(v);
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   589
                    checkCyclicDependencies((JCModuleDecl) tree.defs.head);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   590
                    completeModule(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   591
                } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   592
                    log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   593
                    msym.flags_field &= ~UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   594
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   595
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   596
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   597
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   598
            public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   599
                return "SourceCompleter: " + tree.sourcefile.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   600
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   601
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
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   605
    class ModuleVisitor extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   606
        private ModuleSymbol sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   607
        private final Set<ModuleSymbol> allRequires = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   608
        private final Set<PackageSymbol> allExports = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   609
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   610
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   611
        public void visitModuleDef(JCModuleDecl tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   612
            sym = Assert.checkNonNull(tree.sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   613
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   614
            sym.requires = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   615
            sym.exports = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   616
            tree.directives.forEach(t -> t.accept(this));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   617
            sym.requires = sym.requires.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   618
            sym.exports = sym.exports.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   619
            ensureJavaBase();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   620
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   621
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   622
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   623
        public void visitRequires(JCRequires tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   624
            ModuleSymbol msym = lookupModule(tree.moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   625
            if (msym.kind != MDL) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   626
                log.error(tree.moduleName.pos(), Errors.ModuleNotFound(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   627
            } else if (allRequires.contains(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   628
                log.error(tree.moduleName.pos(), Errors.DuplicateRequires(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   629
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   630
                allRequires.add(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   631
                Set<RequiresFlag> flags = EnumSet.noneOf(RequiresFlag.class);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   632
                if (tree.isPublic)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   633
                    flags.add(RequiresFlag.PUBLIC);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   634
                RequiresDirective d = new RequiresDirective(msym, flags);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   635
                tree.directive = d;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                sym.requires = sym.requires.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   637
            }
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
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   641
        public void visitExports(JCExports tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   642
            Name name = TreeInfo.fullName(tree.qualid);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   643
            PackageSymbol packge = syms.enterPackage(sym, name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   644
            attr.setPackageSymbols(tree.qualid, packge);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   645
            if (!allExports.add(packge)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   646
                log.error(tree.qualid.pos(), Errors.DuplicateExports(packge));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   647
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   648
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   649
            List<ModuleSymbol> toModules = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   650
            if (tree.moduleNames != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   651
                Set<ModuleSymbol> to = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   652
                for (JCExpression n: tree.moduleNames) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   653
                    ModuleSymbol msym = lookupModule(n);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   654
                    if (msym.kind != MDL) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   655
                        log.error(n.pos(), Errors.ModuleNotFound(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   656
                    } else if (!to.add(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   657
                        log.error(n.pos(), Errors.DuplicateExports(msym));
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
                toModules = List.from(to);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   661
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   662
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   663
            if (toModules == null || !toModules.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   664
                ExportsDirective d = new ExportsDirective(packge, toModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   665
                tree.directive = d;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   666
                sym.exports = sym.exports.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   667
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   668
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   669
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   670
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   671
        public void visitProvides(JCProvides tree) { }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   672
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   673
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   674
        public void visitUses(JCUses tree) { }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   675
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   676
        private void ensureJavaBase() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   677
            if (sym.name == names.java_base)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   678
                return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   679
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   680
            for (RequiresDirective d: sym.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   681
                if (d.module.name == names.java_base)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   682
                    return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   683
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   684
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   685
            ModuleSymbol java_base = syms.enterModule(names.java_base);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   686
            Directive.RequiresDirective d =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   687
                    new Directive.RequiresDirective(java_base,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   688
                            EnumSet.of(Directive.RequiresFlag.MANDATED));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   689
            sym.requires = sym.requires.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   690
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   691
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   692
        private ModuleSymbol lookupModule(JCExpression moduleName) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   693
            Name name = TreeInfo.fullName(moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   694
            ModuleSymbol msym = moduleFinder.findModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   695
            TreeInfo.setSymbol(moduleName, msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   696
            return msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   697
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   698
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   699
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   700
    public Completer getUsesProvidesCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   701
        return sym -> {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   702
            ModuleSymbol msym = (ModuleSymbol) sym;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   703
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   704
            msym.complete();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   705
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   706
            Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   707
            UsesProvidesVisitor v = new UsesProvidesVisitor(msym, env);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   708
            JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   709
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   710
                env.toplevel.defs.head.accept(v);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   711
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   712
                log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   713
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   714
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   715
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   716
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   717
    class UsesProvidesVisitor extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   718
        private final ModuleSymbol msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   719
        private final Env<AttrContext> env;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   720
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   721
        private final Set<Directive.UsesDirective> allUses = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   722
        private final Set<Directive.ProvidesDirective> allProvides = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   723
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   724
        public UsesProvidesVisitor(ModuleSymbol msym, Env<AttrContext> env) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   725
            this.msym = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   726
            this.env = env;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   727
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   728
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   729
        @Override @SuppressWarnings("unchecked")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   730
        public void visitModuleDef(JCModuleDecl tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   731
            msym.directives = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   732
            msym.provides = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   733
            msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   734
            tree.directives.forEach(t -> t.accept(this));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   735
            msym.directives = msym.directives.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   736
            msym.provides = msym.provides.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   737
            msym.uses = msym.uses.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   738
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   739
            if (msym.requires.nonEmpty() && msym.requires.head.flags.contains(RequiresFlag.MANDATED))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   740
                msym.directives = msym.directives.prepend(msym.requires.head);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   741
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   742
            msym.directives = msym.directives.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   743
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   744
            checkForCorrectness();
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
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   748
        public void visitExports(JCExports tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   749
            if (tree.directive.packge.members().isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   750
                log.error(tree.qualid.pos(), Errors.PackageEmptyOrNotFound(tree.directive.packge));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   751
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   752
            msym.directives = msym.directives.prepend(tree.directive);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   753
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   754
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   755
        MethodSymbol noArgsConstructor(ClassSymbol tsym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   756
            for (Symbol sym : tsym.members().getSymbolsByName(names.init)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   757
                MethodSymbol mSym = (MethodSymbol)sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   758
                if (mSym.params().isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   759
                    return mSym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   760
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   761
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   762
            return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   763
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   764
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   765
        Map<Directive.ProvidesDirective, JCProvides> directiveToTreeMap = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   766
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   767
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   768
        public void visitProvides(JCProvides tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   769
            Type st = attr.attribType(tree.serviceName, env, syms.objectType);
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   770
            Type it = attr.attribType(tree.implName, env, syms.objectType);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   771
            ClassSymbol service = (ClassSymbol) st.tsym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   772
            ClassSymbol impl = (ClassSymbol) it.tsym;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   773
            if (!types.isSubtype(it, st)) {
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   774
                log.error(tree.implName.pos(), Errors.ServiceImplementationMustBeSubtypeOfServiceInterface);
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   775
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   776
            if ((impl.flags() & ABSTRACT) != 0) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   777
                log.error(tree.implName.pos(), Errors.ServiceImplementationIsAbstract(impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   778
            } else if (impl.isInner()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   779
                log.error(tree.implName.pos(), Errors.ServiceImplementationIsInner(impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   780
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   781
                MethodSymbol constr = noArgsConstructor(impl);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   782
                if (constr == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   783
                    log.error(tree.implName.pos(), Errors.ServiceImplementationDoesntHaveANoArgsConstructor(impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   784
                } else if ((constr.flags() & PUBLIC) == 0) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   785
                    log.error(tree.implName.pos(), Errors.ServiceImplementationNoArgsConstructorNotPublic(impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   786
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   787
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   788
            if (st.hasTag(CLASS) && it.hasTag(CLASS)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   789
                Directive.ProvidesDirective d = new Directive.ProvidesDirective(service, impl);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   790
                if (!allProvides.add(d)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   791
                    log.error(tree.pos(), Errors.DuplicateProvides(service, impl));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   792
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   793
                msym.provides = msym.provides.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   794
                msym.directives = msym.directives.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   795
                directiveToTreeMap.put(d, tree);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   796
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   797
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   798
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   799
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   800
        public void visitRequires(JCRequires tree) {
39915
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
   801
            if (tree.directive != null) {
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
   802
                msym.directives = msym.directives.prepend(tree.directive);
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
   803
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   804
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   805
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   806
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   807
        public void visitUses(JCUses tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   808
            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
   809
            Symbol sym = TreeInfo.symbol(tree.qualid);
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   810
            if ((sym.flags() & ENUM) != 0) {
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   811
                log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym));
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   812
            } else if (st.hasTag(CLASS)) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   813
                ClassSymbol service = (ClassSymbol) st.tsym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   814
                Directive.UsesDirective d = new Directive.UsesDirective(service);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   815
                if (!allUses.add(d)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   816
                    log.error(tree.pos(), Errors.DuplicateUses(service));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   817
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   818
                msym.uses = msym.uses.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   819
                msym.directives = msym.directives.prepend(d);
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
        private void checkForCorrectness() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   824
            for (Directive.ProvidesDirective provides : allProvides) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   825
                JCProvides tree = directiveToTreeMap.get(provides);
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   826
                /* 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
   827
                 * (else, error)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   828
                 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   829
                PackageSymbol implementationDefiningPackage = provides.impl.packge();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   830
                if (implementationDefiningPackage.modle != msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   831
                    log.error(tree.pos(), Errors.ServiceImplementationNotInRightModule(implementationDefiningPackage.modle));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   832
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   833
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   834
                /* 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
   835
                 * 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
   836
                 * exported and there is no uses for the service.
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   837
                 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   838
                PackageSymbol interfaceDeclaringPackage = provides.service.packge();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   839
                boolean isInterfaceDeclaredInCurrentModule = interfaceDeclaringPackage.modle == msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   840
                boolean isInterfaceExportedFromAReadableModule =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   841
                        msym.visiblePackages.get(interfaceDeclaringPackage.fullname) == interfaceDeclaringPackage;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   842
                if (isInterfaceDeclaredInCurrentModule && !isInterfaceExportedFromAReadableModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   843
                    // ok the interface is declared in this module. Let's check if it's exported
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   844
                    boolean warn = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   845
                    for (ExportsDirective export : msym.exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   846
                        if (interfaceDeclaringPackage == export.packge) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   847
                            warn = false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   848
                            break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   849
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   850
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   851
                    if (warn) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   852
                        for (UsesDirective uses : msym.uses) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   853
                            if (provides.service == uses.service) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   854
                                warn = false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   855
                                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   856
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   857
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   858
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   859
                    if (warn) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   860
                        log.warning(tree.pos(), Warnings.ServiceProvidedButNotExportedOrUsed(provides.service));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   861
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   862
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   863
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   864
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   865
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   866
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   867
    private Set<ModuleSymbol> allModules;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   868
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   869
    public Set<ModuleSymbol> allModules() {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   870
        Assert.checkNonNull(allModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   871
        return allModules;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   872
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   873
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   874
    private void setupAllModules() {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   875
        Assert.checkNonNull(rootModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   876
        Assert.checkNull(allModules);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   877
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   878
        Set<ModuleSymbol> observable;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   879
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   880
        if (limitModsOpt == null && extraLimitMods.isEmpty()) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   881
            observable = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   882
        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   883
            Set<ModuleSymbol> limitMods = new HashSet<>();
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   884
            if (limitModsOpt != null) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   885
                for (String limit : limitModsOpt.split(",")) {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   886
                    if (!isValidName(limit))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   887
                        continue;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   888
                    limitMods.add(syms.enterModule(names.fromString(limit)));
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   889
                }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   890
            }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   891
            for (String limit : extraLimitMods) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   892
                limitMods.add(syms.enterModule(names.fromString(limit)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   893
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   894
            observable = computeTransitiveClosure(limitMods, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   895
            observable.addAll(rootModules);
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   896
            if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   897
                for (ModuleSymbol msym : limitMods) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   898
                    if (!observable.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   899
                        log.warning(LintCategory.OPTIONS,
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   900
                                Warnings.ModuleForOptionNotFound(Option.LIMIT_MODULES, msym));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   901
                    }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   902
                }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   903
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   904
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   905
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   906
        Predicate<ModuleSymbol> observablePred = sym -> observable == null || observable.contains(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   907
        Predicate<ModuleSymbol> systemModulePred = sym -> (sym.flags() & Flags.SYSTEM_MODULE) != 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   908
        Set<ModuleSymbol> enabledRoot = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   909
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   910
        if (rootModules.contains(syms.unnamedModule)) {
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   911
            ModuleSymbol javaSE = syms.getModule(java_se);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   912
            Predicate<ModuleSymbol> jdkModulePred;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   913
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   914
            if (javaSE != null && (observable == null || observable.contains(javaSE))) {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   915
                jdkModulePred = sym -> {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   916
                    sym.complete();
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   917
                    return   !sym.name.startsWith(java_)
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   918
                           && sym.exports.stream().anyMatch(e -> e.modules == null);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   919
                };
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   920
                enabledRoot.add(javaSE);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   921
            } else {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   922
                jdkModulePred = sym -> true;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   923
            }
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   924
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   925
            for (ModuleSymbol sym : new HashSet<>(syms.getAllModules())) {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   926
                if (systemModulePred.test(sym) && observablePred.test(sym) && jdkModulePred.test(sym)) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   927
                    enabledRoot.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   928
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   929
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   930
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   931
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   932
        enabledRoot.addAll(rootModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   933
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   934
        if (addModsOpt != null || !extraAddMods.isEmpty()) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   935
            Set<String> fullAddMods = new HashSet<>();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   936
            fullAddMods.addAll(extraAddMods);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   937
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   938
            if (addModsOpt != null) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   939
                fullAddMods.addAll(Arrays.asList(addModsOpt.split(",")));
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   940
            }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   941
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   942
            for (String added : fullAddMods) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   943
                Stream<ModuleSymbol> modules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   944
                switch (added) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   945
                    case ALL_SYSTEM:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   946
                        modules = syms.getAllModules()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   947
                                      .stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   948
                                      .filter(systemModulePred.and(observablePred));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   949
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   950
                    case ALL_MODULE_PATH:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   951
                        modules = syms.getAllModules()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   952
                                      .stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   953
                                      .filter(systemModulePred.negate().and(observablePred));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   954
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   955
                    default:
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   956
                        if (!isValidName(added))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   957
                            continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   958
                        modules = Stream.of(syms.enterModule(names.fromString(added)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   959
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   960
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   961
                modules.forEach(sym -> {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   962
                    enabledRoot.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   963
                    if (observable != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   964
                        observable.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   965
                });
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   966
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   967
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   968
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   969
        Set<ModuleSymbol> result = computeTransitiveClosure(enabledRoot, observable);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   970
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   971
        result.add(syms.unnamedModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   972
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   973
        allModules = result;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   974
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   975
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   976
    private Set<ModuleSymbol> computeTransitiveClosure(Iterable<? extends ModuleSymbol> base, Set<ModuleSymbol> observable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   977
        List<ModuleSymbol> todo = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   978
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   979
        for (ModuleSymbol ms : base) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   980
            todo = todo.prepend(ms);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   981
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   982
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   983
        Set<ModuleSymbol> result = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   984
        result.add(syms.java_base);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   985
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   986
        while (todo.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   987
            ModuleSymbol current = todo.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   988
            todo = todo.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   989
            if (observable != null && !observable.contains(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   990
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   991
            if (!result.add(current) || current == syms.unnamedModule || ((current.flags_field & Flags.AUTOMATIC_MODULE) != 0))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   992
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   993
            current.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   994
            for (RequiresDirective rd : current.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   995
                todo = todo.prepend(rd.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   996
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   997
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   998
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   999
        return result;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1000
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1001
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1002
    public ModuleSymbol getObservableModule(Name name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1003
        ModuleSymbol mod = syms.getModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1004
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1005
        if (allModules().contains(mod)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1006
            return mod;
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
        return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1010
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1011
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1012
    private Completer getUnnamedModuleCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1013
        moduleFinder.findAllModules();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1014
        return new Symbol.Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1015
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1016
            public void complete(Symbol sym) throws CompletionFailure {
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1017
                if (inInitModules) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1018
                    sym.completer = this;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1019
                    return ;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1020
                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1021
                ModuleSymbol msym = (ModuleSymbol) sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1022
                Set<ModuleSymbol> allModules = allModules();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1023
                for (ModuleSymbol m : allModules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1024
                    m.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1025
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1026
                initVisiblePackages(msym, allModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1027
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1028
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1029
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1030
            public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1031
                return "unnamedModule Completer";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1032
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1033
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1034
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1035
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1036
    private final Map<ModuleSymbol, Set<ModuleSymbol>> requiresPublicCache = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1037
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1038
    private void completeModule(ModuleSymbol msym) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1039
        if (inInitModules) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1040
            msym.completer = sym -> completeModule(msym);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1041
            return ;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1042
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1043
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1044
        if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1045
            completeAutomaticModule(msym);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1046
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1047
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1048
        Assert.checkNonNull(msym.requires);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1049
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1050
        initAddReads();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1051
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1052
        msym.requires = msym.requires.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1053
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1054
        List<RequiresDirective> requires = msym.requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1055
        List<RequiresDirective> previous = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1056
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1057
        while (requires.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1058
            if (!allModules().contains(requires.head.module)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1059
                Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1060
                if (env != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1061
                    JavaFileObject origSource = log.useSource(env.toplevel.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1062
                    try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1063
                        log.error(/*XXX*/env.tree, Errors.ModuleNotFound(requires.head.module));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1064
                    } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1065
                        log.useSource(origSource);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1066
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1067
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1068
                    Assert.check((msym.flags() & Flags.AUTOMATIC_MODULE) == 0);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1069
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1070
                if (previous != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1071
                    previous.tail = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1072
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1073
                    msym.requires.tail = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1074
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1075
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1076
                previous = requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1077
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1078
            requires = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1079
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1080
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1081
        Set<ModuleSymbol> readable = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1082
        Set<ModuleSymbol> requiresPublic = new HashSet<>();
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1083
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1084
        for (RequiresDirective d : msym.requires) {
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1085
            d.module.complete();
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1086
            readable.add(d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1087
            Set<ModuleSymbol> s = retrieveRequiresPublic(d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1088
            Assert.checkNonNull(s, () -> "no entry in cache for " + d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1089
            readable.addAll(s);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1090
            if (d.flags.contains(RequiresFlag.PUBLIC)) {
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1091
                requiresPublic.add(d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1092
                requiresPublic.addAll(s);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1093
            }
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1094
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1095
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1096
        requiresPublicCache.put(msym, requiresPublic);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1097
        initVisiblePackages(msym, readable);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1098
        for (ExportsDirective d: msym.exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1099
            d.packge.modle = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1100
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1101
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1102
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1103
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1104
    private Set<ModuleSymbol> retrieveRequiresPublic(ModuleSymbol msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1105
        Set<ModuleSymbol> requiresPublic = requiresPublicCache.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1106
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1107
        if (requiresPublic == null) {
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40604
diff changeset
  1108
            //the module graph may contain cycles involving automatic modules or --add-reads edges
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1109
            requiresPublic = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1110
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1111
            Set<ModuleSymbol> seen = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1112
            List<ModuleSymbol> todo = List.of(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1113
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1114
            while (todo.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1115
                ModuleSymbol current = todo.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1116
                todo = todo.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1117
                if (!seen.add(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1118
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1119
                requiresPublic.add(current);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1120
                current.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1121
                Iterable<? extends RequiresDirective> requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1122
                if (current != syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1123
                    Assert.checkNonNull(current.requires, () -> current + ".requires == null; " + msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1124
                    requires = current.requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1125
                    for (RequiresDirective rd : requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1126
                        if (rd.isPublic())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1127
                            todo = todo.prepend(rd.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1128
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1129
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1130
                    for (ModuleSymbol mod : allModules()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1131
                        todo = todo.prepend(mod);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1132
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1133
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1134
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1135
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1136
            requiresPublic.remove(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1137
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1138
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1139
        return requiresPublic;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1140
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1141
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1142
    private void initVisiblePackages(ModuleSymbol msym, Collection<ModuleSymbol> readable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1143
        initAddExports();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1144
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1145
        msym.visiblePackages = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1146
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1147
        Map<Name, ModuleSymbol> seen = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1148
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1149
        for (ModuleSymbol rm : readable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1150
            if (rm == syms.unnamedModule)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1151
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1152
            addVisiblePackages(msym, seen, rm, rm.exports);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1153
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1154
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1155
        addExports.forEach((exportsFrom, exports) -> {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1156
            addVisiblePackages(msym, seen, exportsFrom, exports);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1157
        });
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1158
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1159
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1160
    private void addVisiblePackages(ModuleSymbol msym,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1161
                                    Map<Name, ModuleSymbol> seenPackages,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1162
                                    ModuleSymbol exportsFrom,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1163
                                    Collection<ExportsDirective> exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1164
        for (ExportsDirective d : exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1165
            if (d.modules == null || d.modules.contains(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1166
                Name packageName = d.packge.fullname;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1167
                ModuleSymbol previousModule = seenPackages.get(packageName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1168
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1169
                if (previousModule != null && previousModule != exportsFrom) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1170
                    Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1171
                    JavaFileObject origSource = env != null ? log.useSource(env.toplevel.sourcefile)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1172
                                                            : null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1173
                    DiagnosticPosition pos = env != null ? env.tree.pos() : null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1174
                    try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1175
                        log.error(pos, Errors.PackageClashFromRequires(msym, packageName,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1176
                                                                      previousModule, exportsFrom));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1177
                    } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1178
                        if (env != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1179
                            log.useSource(origSource);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1180
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1181
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1182
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1183
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1184
                seenPackages.put(packageName, exportsFrom);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1185
                msym.visiblePackages.put(d.packge.fullname, d.packge);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1186
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1187
        }
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
    private void initAddExports() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1191
        if (addExports != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1192
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1193
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1194
        addExports = new LinkedHashMap<>();
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1195
        Set<ModuleSymbol> unknownModules = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1196
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1197
        if (addExportsOpt == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1198
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1199
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1200
        Pattern ep = Pattern.compile("([^/]+)/([^=]+)=(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1201
        for (String s: addExportsOpt.split("\0+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1202
            if (s.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1203
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1204
            Matcher em = ep.matcher(s);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1205
            if (!em.matches()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1206
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1207
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1208
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1209
            // Terminology comes from
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40604
diff changeset
  1210
            //  --add-exports module/package=target,...
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1211
            // Compare to
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1212
            //  module module { exports package to target, ... }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1213
            String moduleName = em.group(1);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1214
            String packageName = em.group(2);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1215
            String targetNames = em.group(3);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1216
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1217
            if (!isValidName(moduleName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1218
                continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1219
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1220
            ModuleSymbol msym = syms.enterModule(names.fromString(moduleName));
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1221
            if (!isKnownModule(msym, unknownModules))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1222
                continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1223
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1224
            if (!isValidName(packageName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1225
                continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1226
            PackageSymbol p = syms.enterPackage(msym, names.fromString(packageName));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1227
            p.modle = msym;  // TODO: do we need this?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1228
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1229
            List<ModuleSymbol> targetModules = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1230
            for (String toModule : targetNames.split("[ ,]+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1231
                ModuleSymbol m;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1232
                if (toModule.equals("ALL-UNNAMED")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1233
                    m = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1234
                } else {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1235
                    if (!isValidName(toModule))
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1236
                        continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1237
                    m = syms.enterModule(names.fromString(toModule));
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1238
                    if (!isKnownModule(m, unknownModules))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1239
                        continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1240
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1241
                targetModules = targetModules.prepend(m);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1242
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1243
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1244
            Set<ExportsDirective> extra = addExports.computeIfAbsent(msym, _x -> new LinkedHashSet<>());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1245
            ExportsDirective d = new ExportsDirective(p, targetModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1246
            extra.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1247
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1248
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1249
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1250
    private boolean isKnownModule(ModuleSymbol msym, Set<ModuleSymbol> unknownModules) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1251
        if (allModules.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1252
            return true;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1253
        }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1254
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1255
        if (!unknownModules.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1256
            if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1257
                log.warning(LintCategory.OPTIONS,
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1258
                        Warnings.ModuleForOptionNotFound(Option.ADD_EXPORTS, msym));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1259
            }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1260
            unknownModules.add(msym);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1261
        }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1262
        return false;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1263
    }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1264
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1265
    private void initAddReads() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1266
        if (addReads != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1267
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1268
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1269
        addReads = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1270
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1271
        if (addReadsOpt == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1272
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1273
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1274
        Pattern rp = Pattern.compile("([^=]+)=(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1275
        for (String s : addReadsOpt.split("\0+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1276
            if (s.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1277
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1278
            Matcher rm = rp.matcher(s);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1279
            if (!rm.matches()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1280
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1281
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1282
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1283
            // Terminology comes from
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1284
            //  --add-reads source-module=target-module,...
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1285
            // Compare to
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1286
            //  module source-module { requires target-module; ... }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1287
            String sourceName = rm.group(1);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1288
            String targetNames = rm.group(2);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1289
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1290
            if (!isValidName(sourceName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1291
                continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1292
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1293
            ModuleSymbol msym = syms.enterModule(names.fromString(sourceName));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1294
            if (!allModules.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1295
                if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1296
                    log.warning(Warnings.ModuleForOptionNotFound(Option.ADD_READS, msym));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1297
                }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1298
                continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1299
            }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1300
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1301
            for (String targetName : targetNames.split("[ ,]+", -1)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1302
                ModuleSymbol targetModule;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1303
                if (targetName.equals("ALL-UNNAMED")) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1304
                    targetModule = syms.unnamedModule;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1305
                } else {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1306
                    if (!isValidName(targetName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1307
                        continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1308
                    targetModule = syms.enterModule(names.fromString(targetName));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1309
                    if (!allModules.contains(targetModule)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1310
                        if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1311
                            log.warning(LintCategory.OPTIONS, Warnings.ModuleForOptionNotFound(Option.ADD_READS, targetModule));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1312
                        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1313
                        continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1314
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1315
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1316
                addReads.computeIfAbsent(msym, m -> new HashSet<>())
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1317
                        .add(new RequiresDirective(targetModule, EnumSet.of(RequiresFlag.EXTRA)));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1318
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1319
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1320
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1321
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1322
    private void checkCyclicDependencies(JCModuleDecl mod) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1323
        for (JCDirective d : mod.directives) {
39915
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
  1324
            JCRequires rd;
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
  1325
            if (!d.hasTag(Tag.REQUIRES) || (rd = (JCRequires) d).directive == null)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1326
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1327
            Set<ModuleSymbol> nonSyntheticDeps = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1328
            List<ModuleSymbol> queue = List.of(rd.directive.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1329
            while (queue.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1330
                ModuleSymbol current = queue.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1331
                queue = queue.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1332
                if (!nonSyntheticDeps.add(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1333
                    continue;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1334
                current.complete();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1335
                if ((current.flags() & Flags.ACYCLIC) != 0)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1336
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1337
                Assert.checkNonNull(current.requires, () -> current.toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1338
                for (RequiresDirective dep : current.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1339
                    if (!dep.flags.contains(RequiresFlag.EXTRA))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1340
                        queue = queue.prepend(dep.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1341
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1342
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1343
            if (nonSyntheticDeps.contains(mod.sym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1344
                log.error(rd.moduleName.pos(), Errors.CyclicRequires(rd.directive.module));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1345
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1346
            mod.sym.flags_field |= Flags.ACYCLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1347
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1348
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1349
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1350
    private boolean isValidName(CharSequence name) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1351
        return SourceVersion.isName(name, Source.toSourceVersion(source));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1352
    }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1353
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1354
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1355
    private String toString(ModuleSymbol msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1356
        return msym.name + "["
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1357
                + "kind:" + msym.kind + ";"
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1358
                + "locn:" + toString(msym.sourceLocation) + "," + toString(msym.classLocation) + ";"
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1359
                + "info:" + toString(msym.module_info.sourcefile) + ","
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1360
                            + toString(msym.module_info.classfile) + ","
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1361
                            + msym.module_info.completer
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1362
                + "]";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1363
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1364
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1365
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1366
    String toString(Location locn) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1367
        return (locn == null) ? "--" : locn.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1368
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1369
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1370
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1371
    String toString(JavaFileObject fo) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1372
        return (fo == null) ? "--" : fo.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1373
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1374
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1375
    public void newRound() {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1376
        rootModules = null;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1377
        allModules = null;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1378
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1379
}