langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java
author jlahoda
Mon, 19 Jun 2017 05:56:49 +0200
changeset 45685 95526afc4e20
parent 45682 fc3b228b9e2a
child 45744 db6aedca2c8c
permissions -rw-r--r--
8182162: Remove -XD-Xmodule Summary: Removing the undocumented -XD-Xmodule: option. Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
43267
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
     2
 * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
36526
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;
43270
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
    44
import java.util.stream.Collectors;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.stream.Stream;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import javax.lang.model.SourceVersion;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import javax.tools.JavaFileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import javax.tools.JavaFileManager.Location;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import javax.tools.JavaFileObject;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import javax.tools.JavaFileObject.Kind;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import javax.tools.StandardLocation;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    54
import com.sun.source.tree.ModuleTree.ModuleKind;
39361
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
    55
import com.sun.tools.javac.code.ClassFinder;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    56
import com.sun.tools.javac.code.DeferredLintHandler;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import com.sun.tools.javac.code.Directive;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
import com.sun.tools.javac.code.Directive.ExportsDirective;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    59
import com.sun.tools.javac.code.Directive.ExportsFlag;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    60
import com.sun.tools.javac.code.Directive.OpensDirective;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    61
import com.sun.tools.javac.code.Directive.OpensFlag;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
import com.sun.tools.javac.code.Directive.RequiresDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
import com.sun.tools.javac.code.Directive.RequiresFlag;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
import com.sun.tools.javac.code.Directive.UsesDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
import com.sun.tools.javac.code.Flags;
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
    66
import com.sun.tools.javac.code.Lint.LintCategory;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
import com.sun.tools.javac.code.ModuleFinder;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
import com.sun.tools.javac.code.Source;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
import com.sun.tools.javac.code.Symbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
import com.sun.tools.javac.code.Symbol.ClassSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
import com.sun.tools.javac.code.Symbol.Completer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
import com.sun.tools.javac.code.Symbol.CompletionFailure;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
import com.sun.tools.javac.code.Symbol.MethodSymbol;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    74
import com.sun.tools.javac.code.Symbol.ModuleFlags;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
import com.sun.tools.javac.code.Symbol.PackageSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
import com.sun.tools.javac.code.Symtab;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
import com.sun.tools.javac.code.Type;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
    79
import com.sun.tools.javac.code.Types;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
import com.sun.tools.javac.jvm.ClassWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
import com.sun.tools.javac.jvm.JNIWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
import com.sun.tools.javac.main.Option;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
import com.sun.tools.javac.resources.CompilerProperties.Errors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
import com.sun.tools.javac.tree.JCTree;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    87
import com.sun.tools.javac.tree.JCTree.JCDirective;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
import com.sun.tools.javac.tree.JCTree.JCExports;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
import com.sun.tools.javac.tree.JCTree.JCExpression;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    91
import com.sun.tools.javac.tree.JCTree.JCOpens;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
import com.sun.tools.javac.tree.JCTree.JCProvides;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
import com.sun.tools.javac.tree.JCTree.JCRequires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
import com.sun.tools.javac.tree.JCTree.JCUses;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    95
import com.sun.tools.javac.tree.JCTree.Tag;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
import com.sun.tools.javac.tree.TreeInfo;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    97
import com.sun.tools.javac.util.Abort;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
import com.sun.tools.javac.util.Assert;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
import com.sun.tools.javac.util.Context;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
import com.sun.tools.javac.util.JCDiagnostic;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
import com.sun.tools.javac.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
import com.sun.tools.javac.util.ListBuffer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
import com.sun.tools.javac.util.Log;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
import com.sun.tools.javac.util.Name;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
import com.sun.tools.javac.util.Names;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
import com.sun.tools.javac.util.Options;
39361
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   108
import com.sun.tools.javac.util.Position;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   110
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
   111
import static com.sun.tools.javac.code.Flags.ENUM;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
import static com.sun.tools.javac.code.Flags.PUBLIC;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   113
import static com.sun.tools.javac.code.Flags.UNATTRIBUTED;
44290
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
   114
import com.sun.tools.javac.code.Kinds;
42841
1768507dae6b 8171412: tools/javac/modules/AddLimitMods.java failed with "error: module not found"
jjg
parents: 42827
diff changeset
   115
import static com.sun.tools.javac.code.Kinds.Kind.ERR;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   116
import static com.sun.tools.javac.code.Kinds.Kind.MDL;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   117
import static com.sun.tools.javac.code.Kinds.Kind.MTH;
43270
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
   118
import com.sun.tools.javac.code.Symbol.ModuleResolutionFlags;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   119
import static com.sun.tools.javac.code.TypeTag.CLASS;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
/**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
 *  TODO: fill in
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
 *  <p><b>This is NOT part of any supported API.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
 *  If you write code that depends on this, you do so at your own risk.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
 *  This code and its internal interfaces are subject to change or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
 *  deletion without notice.</b>
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
public class Modules extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
    private static final String ALL_SYSTEM = "ALL-SYSTEM";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
    private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
    private final Log log;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   134
    private final Names names;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
    private final Symtab syms;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
    private final Attr attr;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   137
    private final Check chk;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   138
    private final DeferredLintHandler deferredLintHandler;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    private final TypeEnvs typeEnvs;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   140
    private final Types types;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   141
    private final JavaFileManager fileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    private final ModuleFinder moduleFinder;
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   143
    private final Source source;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    private final boolean allowModules;
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
   145
    private final boolean allowAccessIntoSystem;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
    public final boolean multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   149
    private final Name java_se;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   150
    private final Name java_;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   151
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
    ModuleSymbol defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   153
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
    private final String addExportsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   155
    private Map<ModuleSymbol, Set<ExportsDirective>> addExports;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    private final String addReadsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   157
    private Map<ModuleSymbol, Set<RequiresDirective>> addReads;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   158
    private final String addModsOpt;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   159
    private final Set<String> extraAddMods = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   160
    private final String limitModsOpt;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   161
    private final Set<String> extraLimitMods = new HashSet<>();
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   162
    private final String moduleVersionOpt;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   163
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   164
    private final boolean lintOptions;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   165
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   166
    private Set<ModuleSymbol> rootModules = null;
43579
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
   167
    private final Set<ModuleSymbol> warnedMissing = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   168
44290
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
   169
    public PackageNameFinder findPackageInFile;
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
   170
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
    public static Modules instance(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
        Modules instance = context.get(Modules.class);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   173
        if (instance == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            instance = new Modules(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        return instance;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   176
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
    protected Modules(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        context.put(Modules.class, this);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   180
        log = Log.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   181
        names = Names.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        syms = Symtab.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        attr = Attr.instance(context);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   184
        chk = Check.instance(context);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   185
        deferredLintHandler = DeferredLintHandler.instance(context);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   186
        typeEnvs = TypeEnvs.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   187
        moduleFinder = ModuleFinder.instance(context);
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   188
        types = Types.instance(context);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        fileManager = context.get(JavaFileManager.class);
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   190
        source = Source.instance(context);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   191
        allowModules = source.allowModules();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        Options options = Options.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   193
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
   194
        allowAccessIntoSystem = options.isUnset(Option.RELEASE);
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   195
        lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   196
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        ClassWriter classWriter = ClassWriter.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   199
        classWriter.multiModuleMode = multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   200
        JNIWriter jniWriter = JNIWriter.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        jniWriter.multiModuleMode = multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   202
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   203
        java_se = names.fromString("java.se");
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   204
        java_ = names.fromString("java.");
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   205
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   206
        addExportsOpt = options.get(Option.ADD_EXPORTS);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   207
        addReadsOpt = options.get(Option.ADD_READS);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   208
        addModsOpt = options.get(Option.ADD_MODULES);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   209
        limitModsOpt = options.get(Option.LIMIT_MODULES);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   210
        moduleVersionOpt = options.get(Option.MODULE_VERSION);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   211
    }
44812
1d9c041728a6 8178012: Finish removal of -Xmodule:
jlahoda
parents: 44576
diff changeset
   212
    //where
1d9c041728a6 8178012: Finish removal of -Xmodule:
jlahoda
parents: 44576
diff changeset
   213
        private static final String XMODULES_PREFIX = "-Xmodule:";
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   214
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   215
    int depth = -1;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   216
    private void dprintln(String msg) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        for (int i = 0; i < depth; i++)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   218
            System.err.print("  ");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   219
        System.err.println(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   220
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   221
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   222
    public void addExtraAddModules(String... extras) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   223
        extraAddMods.addAll(Arrays.asList(extras));
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   224
    }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   225
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   226
    public void addExtraLimitModules(String... extras) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   227
        extraLimitMods.addAll(Arrays.asList(extras));
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   228
    }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   229
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   230
    boolean inInitModules;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   231
    public void initModules(List<JCCompilationUnit> trees) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   232
        Assert.check(!inInitModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   233
        try {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   234
            inInitModules = true;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   235
            Assert.checkNull(rootModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   236
            enter(trees, modules -> {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   237
                Assert.checkNull(rootModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   238
                Assert.checkNull(allModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   239
                this.rootModules = modules;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   240
                setupAllModules(); //initialize the module graph
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   241
                Assert.checkNonNull(allModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   242
                inInitModules = false;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   243
            }, null);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   244
        } finally {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   245
            inInitModules = false;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   246
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   247
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   248
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   249
    public boolean enter(List<JCCompilationUnit> trees, ClassSymbol c) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   250
        Assert.check(rootModules != null || inInitModules || !allowModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   251
        return enter(trees, modules -> {}, c);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   252
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   253
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   254
    private boolean enter(List<JCCompilationUnit> trees, Consumer<Set<ModuleSymbol>> init, ClassSymbol c) {
38619
27c0007bb28d 8152785: Remove javac -XDnoModules
vromero
parents: 37854
diff changeset
   255
        if (!allowModules) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   256
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                tree.modle = syms.noModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   258
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
            defaultModule = syms.noModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            return true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   261
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   262
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   263
        int startErrors = log.nerrors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   265
        depth++;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   266
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   267
            // scan trees for module defs
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   268
            Set<ModuleSymbol> roots = enterModules(trees, c);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   269
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42823
diff changeset
   270
            setCompilationUnitModules(trees, roots, c);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   271
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   272
            init.accept(roots);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   273
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   274
            for (ModuleSymbol msym: roots) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                msym.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   276
            }
39361
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   277
        } catch (CompletionFailure ex) {
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   278
            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
   279
            if (ex instanceof ClassFinder.BadClassFile) throw new Abort();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   280
        } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   281
            depth--;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   282
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   283
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   284
        return (log.nerrors == startErrors);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   285
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   287
    public Completer getCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   288
        return mainCompleter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   289
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   290
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   291
    public ModuleSymbol getDefaultModule() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   292
        return defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   293
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   294
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   295
    public boolean modulesInitialized() {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   296
        return allModules != null;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   297
    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   298
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   299
    private Set<ModuleSymbol> enterModules(List<JCCompilationUnit> trees, ClassSymbol c) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   300
        Set<ModuleSymbol> modules = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   301
        for (JCCompilationUnit tree : trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   302
            JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   303
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                enterModule(tree, c, modules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   305
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   306
                log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   308
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   309
        return modules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   310
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   311
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   312
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   313
    private void enterModule(JCCompilationUnit toplevel, ClassSymbol c, Set<ModuleSymbol> modules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   314
        boolean isModuleInfo = toplevel.sourcefile.isNameCompatible("module-info", Kind.SOURCE);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   315
        boolean isModuleDecl = toplevel.getModuleDecl() != null;
41999
dbf796210eff 8168312: javac throws NPE if annotation processor is specified and module is declared in a file named arbitrarily
sadayapalam
parents: 41942
diff changeset
   316
        if (isModuleDecl) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   317
            JCModuleDecl decl = toplevel.getModuleDecl();
41999
dbf796210eff 8168312: javac throws NPE if annotation processor is specified and module is declared in a file named arbitrarily
sadayapalam
parents: 41942
diff changeset
   318
            if (!isModuleInfo) {
dbf796210eff 8168312: javac throws NPE if annotation processor is specified and module is declared in a file named arbitrarily
sadayapalam
parents: 41942
diff changeset
   319
                log.error(decl.pos(), Errors.ModuleDeclSbInModuleInfoJava);
dbf796210eff 8168312: javac throws NPE if annotation processor is specified and module is declared in a file named arbitrarily
sadayapalam
parents: 41942
diff changeset
   320
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   321
            Name name = TreeInfo.fullName(decl.qualId);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   322
            ModuleSymbol sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   323
            if (c != null) {
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   324
                sym = (ModuleSymbol) c.owner;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   325
                Assert.checkNonNull(sym.name);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   326
                Name treeName = TreeInfo.fullName(decl.qualId);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   327
                if (sym.name != treeName) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   328
                    log.error(decl.pos(), Errors.ModuleNameMismatch(name, sym.name));
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   329
                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   330
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                sym = syms.enterModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   332
                if (sym.module_info.sourcefile != null && sym.module_info.sourcefile != toplevel.sourcefile) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   333
                    log.error(decl.pos(), Errors.DuplicateModule(sym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   334
                    return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   335
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   336
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            sym.completer = getSourceCompleter(toplevel);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   338
            sym.module_info.sourcefile = toplevel.sourcefile;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   339
            decl.sym = sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   340
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   341
            if (multiModuleMode || modules.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   342
                modules.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   343
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   344
                log.error(toplevel.pos(), Errors.TooManyModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   345
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   346
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   347
            Env<AttrContext> provisionalEnv = new Env<>(decl, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   348
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   349
            provisionalEnv.toplevel = toplevel;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   350
            typeEnvs.put(sym, provisionalEnv);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   351
        } else if (isModuleInfo) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   352
            if (multiModuleMode) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   353
                JCTree tree = toplevel.defs.isEmpty() ? toplevel : toplevel.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   354
                log.error(tree.pos(), Errors.ExpectedModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   355
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   357
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   358
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42823
diff changeset
   359
    private void setCompilationUnitModules(List<JCCompilationUnit> trees, Set<ModuleSymbol> rootModules, ClassSymbol c) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   360
        // update the module for each compilation unit
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   361
        if (multiModuleMode) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   362
            checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   363
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   364
                if (tree.defs.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   365
                    tree.modle = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   366
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   367
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   368
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   369
                JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   370
                try {
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   371
                    Location msplocn = getModuleLocation(tree);
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   372
                    Location plocn = fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH) ?
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   373
                            fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH,
44063
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   374
                                                             tree.sourcefile) :
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   375
                            null;
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   376
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   377
                    if (plocn != null) {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   378
                        Name name = names.fromString(fileManager.inferModuleName(plocn));
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   379
                        ModuleSymbol msym = moduleFinder.findModule(name);
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   380
                        tree.modle = msym;
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   381
                        rootModules.add(msym);
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   382
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   383
                        if (msplocn != null) {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   384
                            Name mspname = names.fromString(fileManager.inferModuleName(msplocn));
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   385
                            if (name != mspname) {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   386
                                log.error(tree.pos(), Errors.FilePatchedAndMsp(name, mspname));
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   387
                            }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   388
                        }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   389
                    } else if (msplocn != null) {
44063
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   390
                        if (tree.getModuleDecl() != null) {
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   391
                            JavaFileObject canonical =
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   392
                                    fileManager.getJavaFileForInput(msplocn, "module-info", Kind.SOURCE);
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   393
                            if (canonical == null || !fileManager.isSameFile(canonical, tree.sourcefile)) {
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   394
                                log.error(tree.pos(), Errors.ModuleNotFoundOnModuleSourcePath);
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   395
                            }
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   396
                        }
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   397
                        Name name = names.fromString(fileManager.inferModuleName(msplocn));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   398
                        ModuleSymbol msym;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   399
                        JCModuleDecl decl = tree.getModuleDecl();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   400
                        if (decl != null) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                            msym = decl.sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   402
                            if (msym.name != name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                                log.error(decl.qualId, Errors.ModuleNameMismatch(msym.name, name));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   404
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   405
                        } else {
43267
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   406
                            if (tree.getPackage() == null) {
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   407
                                log.error(tree.pos(), Errors.UnnamedPkgNotAllowedNamedModules);
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   408
                            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   409
                            msym = syms.enterModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   410
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   411
                        if (msym.sourceLocation == null) {
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   412
                            msym.sourceLocation = msplocn;
44289
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   413
                            if (fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH)) {
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   414
                                msym.patchLocation = fileManager.getLocationForModule(
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   415
                                        StandardLocation.PATCH_MODULE_PATH, msym.name.toString());
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   416
                            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   417
                            if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
44289
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   418
                                Location outputLocn = fileManager.getLocationForModule(
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   419
                                        StandardLocation.CLASS_OUTPUT, msym.name.toString());
44289
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   420
                                if (msym.patchLocation == null) {
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   421
                                    msym.classLocation = outputLocn;
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   422
                                } else {
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   423
                                    msym.patchOutputLocation = outputLocn;
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   424
                                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   425
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   426
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   427
                        tree.modle = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   428
                        rootModules.add(msym);
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42823
diff changeset
   429
                    } else if (c != null && c.packge().modle == syms.unnamedModule) {
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42823
diff changeset
   430
                        tree.modle = syms.unnamedModule;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   431
                    } else {
43267
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   432
                        if (tree.getModuleDecl() != null) {
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   433
                            log.error(tree.pos(), Errors.ModuleNotFoundOnModuleSourcePath);
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   434
                        } else {
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   435
                            log.error(tree.pos(), Errors.NotInModuleOnModuleSourcePath);
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   436
                        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   437
                        tree.modle = syms.errModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   438
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   439
                } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   440
                    throw new Error(e); // FIXME
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   441
                } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   442
                    log.useSource(prev);
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
            if (syms.unnamedModule.sourceLocation == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   446
                syms.unnamedModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   447
                syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   448
                syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   449
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   450
            defaultModule = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   451
        } else {
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   452
            ModuleSymbol module = null;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   453
            if (defaultModule == null) {
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   454
                String moduleOverride = singleModuleOverride(trees);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   455
                switch (rootModules.size()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   456
                    case 0:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   457
                        defaultModule = moduleFinder.findSingleModule();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   458
                        if (defaultModule == syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   459
                            if (moduleOverride != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   460
                                checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   461
                                defaultModule = moduleFinder.findModule(names.fromString(moduleOverride));
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   462
                                defaultModule.patchOutputLocation = StandardLocation.CLASS_OUTPUT;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   463
                            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   464
                                // Question: why not do findAllModules and initVisiblePackages here?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   465
                                // i.e. body of unnamedModuleCompleter
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   466
                                defaultModule.completer = getUnnamedModuleCompleter();
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   467
                                defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   468
                                defaultModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   469
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   470
                        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   471
                            checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   472
                            defaultModule.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   473
                            // Question: why not do completeModule here?
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
   474
                            defaultModule.completer = sym -> completeModule((ModuleSymbol) sym);
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   475
                            defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   476
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   477
                        rootModules.add(defaultModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   478
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   479
                    case 1:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   480
                        checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   481
                        defaultModule = rootModules.iterator().next();
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   482
                        defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
44289
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   483
                        if (fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH)) {
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   484
                            try {
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   485
                                defaultModule.patchLocation = fileManager.getLocationForModule(
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   486
                                        StandardLocation.PATCH_MODULE_PATH, defaultModule.name.toString());
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   487
                            } catch (IOException ex) {
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   488
                                throw new Error(ex);
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   489
                            }
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   490
                        }
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   491
                        if (defaultModule.patchLocation == null) {
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   492
                            defaultModule.classLocation = StandardLocation.CLASS_OUTPUT;
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   493
                        } else {
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   494
                            defaultModule.patchOutputLocation = StandardLocation.CLASS_OUTPUT;
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   495
                        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   496
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   497
                    default:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   498
                        Assert.error("too many modules");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   499
                }
44289
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   500
            } else if (rootModules.size() == 1) {
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   501
                module = rootModules.iterator().next();
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   502
                module.complete();
842ccb558d7d 8175057: module-info on patch path should not produce an error
jlahoda
parents: 44063
diff changeset
   503
                module.completer = sym -> completeModule((ModuleSymbol) sym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   504
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   505
                Assert.check(rootModules.isEmpty());
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   506
                String moduleOverride = singleModuleOverride(trees);
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   507
                if (moduleOverride != null) {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   508
                    module = moduleFinder.findModule(names.fromString(moduleOverride));
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   509
                } else {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   510
                    module = defaultModule;
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   511
                }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   512
                rootModules.add(module);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   513
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   514
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   515
            if (defaultModule != syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   516
                syms.unnamedModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   517
                syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   518
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   519
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   520
            if (module == null) {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   521
                module = defaultModule;
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   522
            }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   523
44822
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   524
            for (JCCompilationUnit tree : trees) {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   525
                if (defaultModule != syms.unnamedModule
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   526
                        && defaultModule.sourceLocation == StandardLocation.SOURCE_PATH
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   527
                        && fileManager.hasLocation(StandardLocation.SOURCE_PATH)) {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   528
                    checkSourceLocation(tree, module);
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   529
                }
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   530
                tree.modle = module;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   531
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   532
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   533
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   534
44822
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   535
    private void checkSourceLocation(JCCompilationUnit tree, ModuleSymbol msym) {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   536
        try {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   537
            JavaFileObject fo = tree.sourcefile;
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   538
            if (fileManager.contains(msym.sourceLocation, fo)) {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   539
                return;
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   540
            }
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   541
            if (msym.patchLocation != null && fileManager.contains(msym.patchLocation, fo)) {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   542
                return;
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   543
            }
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   544
            if (fileManager.hasLocation(StandardLocation.SOURCE_OUTPUT)) {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   545
                if (fileManager.contains(StandardLocation.SOURCE_OUTPUT, fo)) {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   546
                    return;
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   547
                }
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   548
            } else {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   549
                if (fileManager.contains(StandardLocation.CLASS_OUTPUT, fo)) {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   550
                    return;
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   551
                }
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   552
            }
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   553
        } catch (IOException e) {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   554
            throw new Error(e);
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   555
        }
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   556
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   557
        JavaFileObject prev = log.useSource(tree.sourcefile);
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   558
        try {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   559
            log.error(tree.pos(), "file.sb.on.source.or.patch.path.for.module");
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   560
        } finally {
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   561
            log.useSource(prev);
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   562
        }
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   563
    }
2f24758e7ae0 8176327: javac produces wrong module-info
jjg
parents: 44812
diff changeset
   564
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   565
    private String singleModuleOverride(List<JCCompilationUnit> trees) {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   566
        if (!fileManager.hasLocation(StandardLocation.PATCH_MODULE_PATH)) {
45685
95526afc4e20 8182162: Remove -XD-Xmodule
jlahoda
parents: 45682
diff changeset
   567
            return null;
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   568
        }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   569
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   570
        Set<String> override = new LinkedHashSet<>();
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   571
        for (JCCompilationUnit tree : trees) {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   572
            JavaFileObject fo = tree.sourcefile;
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   573
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   574
            try {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   575
                Location loc =
44063
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   576
                        fileManager.getLocationForModule(StandardLocation.PATCH_MODULE_PATH, fo);
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   577
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   578
                if (loc != null) {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   579
                    override.add(fileManager.inferModuleName(loc));
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   580
                }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   581
            } catch (IOException ex) {
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   582
                throw new Error(ex);
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   583
            }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   584
        }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   585
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   586
        switch (override.size()) {
45685
95526afc4e20 8182162: Remove -XD-Xmodule
jlahoda
parents: 45682
diff changeset
   587
            case 0: return null;
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   588
            case 1: return override.iterator().next();
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   589
            default:
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   590
                log.error(Errors.TooManyPatchedModules(override));
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   591
                return null;
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   592
        }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   593
    }
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
   594
43267
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   595
    /**
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   596
     * Determine the location for the module on the module source path
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   597
     * or source output directory which contains a given CompilationUnit.
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   598
     * If the source output directory is unset, the class output directory
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   599
     * will be checked instead.
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   600
     * {@code null} is returned if no such module can be found.
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   601
     * @param tree the compilation unit tree
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   602
     * @return the location for the enclosing module
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   603
     * @throws IOException if there is a problem while searching for the module.
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   604
     */
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   605
    private Location getModuleLocation(JCCompilationUnit tree) throws IOException {
43267
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   606
        JavaFileObject fo = tree.sourcefile;
81e70bde1b96 8165102: incorrect message from javac
jjg
parents: 43138
diff changeset
   607
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   608
        Location loc =
44063
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   609
                fileManager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH, fo);
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   610
        if (loc == null) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   611
            Location sourceOutput = fileManager.hasLocation(StandardLocation.SOURCE_OUTPUT) ?
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   612
                    StandardLocation.SOURCE_OUTPUT : StandardLocation.CLASS_OUTPUT;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   613
            loc =
44063
5f0cf4126949 8175560: Drop String pkgName from javax.tools.JavaFileManager.getLocationForModule(Location location, JavaFileObject fo, String pkgName)
jlahoda
parents: 43865
diff changeset
   614
                fileManager.getLocationForModule(sourceOutput, fo);
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   615
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   616
        return loc;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   617
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   618
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   619
    private void checkNoAllModulePath() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   620
        if (addModsOpt != null && Arrays.asList(addModsOpt.split(",")).contains(ALL_MODULE_PATH)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   621
            log.error(Errors.AddmodsAllModulePathInvalid);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   622
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   623
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   624
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   625
    private final Completer mainCompleter = new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   626
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   627
        public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   628
            ModuleSymbol msym = moduleFinder.findModule((ModuleSymbol) sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   629
42841
1768507dae6b 8171412: tools/javac/modules/AddLimitMods.java failed with "error: module not found"
jjg
parents: 42827
diff changeset
   630
            if (msym.kind == ERR) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   631
                //make sure the module is initialized:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   632
                msym.directives = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   633
                msym.exports = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   634
                msym.provides = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   635
                msym.requires = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   636
                msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   637
            } 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
   638
                setupAutomaticModule(msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   639
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   640
                msym.module_info.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   641
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   642
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   643
            // If module-info comes from a .java file, the underlying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   644
            // call of classFinder.fillIn will have called through the
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   645
            // source completer, to Enter, and then to Modules.enter,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   646
            // which will call completeModule.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   647
            // But, if module-info comes from a .class file, the underlying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   648
            // call of classFinder.fillIn will just call ClassReader to read
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   649
            // the .class file, and so we call completeModule here.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   650
            if (msym.module_info.classfile == null || msym.module_info.classfile.getKind() == Kind.CLASS) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   651
                completeModule(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   652
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   653
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   654
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   655
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   656
        public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   657
            return "mainCompleter";
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
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   661
    private void setupAutomaticModule(ModuleSymbol msym) throws CompletionFailure {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   662
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   663
            ListBuffer<Directive> directives = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   664
            ListBuffer<ExportsDirective> exports = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   665
            Set<String> seenPackages = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   666
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   667
            for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   668
                String binName = fileManager.inferBinaryName(msym.classLocation, clazz);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   669
                String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package????
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   670
                if (seenPackages.add(pack)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   671
                    ExportsDirective d = new ExportsDirective(syms.enterPackage(msym, names.fromString(pack)), null);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   672
                    //TODO: opens?
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   673
                    directives.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   674
                    exports.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   675
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   676
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   677
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   678
            msym.exports = exports.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   679
            msym.provides = List.nil();
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   680
            msym.requires = List.nil();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   681
            msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   682
            msym.directives = directives.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   683
            msym.flags_field |= Flags.ACYCLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   684
        } catch (IOException ex) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   685
            throw new IllegalStateException(ex);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   686
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   687
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   688
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   689
    private void completeAutomaticModule(ModuleSymbol msym) throws CompletionFailure {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   690
        ListBuffer<Directive> directives = new ListBuffer<>();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   691
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   692
        directives.addAll(msym.directives);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   693
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   694
        ListBuffer<RequiresDirective> requires = new ListBuffer<>();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   695
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   696
        for (ModuleSymbol ms : allModules()) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   697
            if (ms == syms.unnamedModule || ms == msym)
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   698
                continue;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   699
            Set<RequiresFlag> flags = (ms.flags_field & Flags.AUTOMATIC_MODULE) != 0 ?
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   700
                    EnumSet.of(RequiresFlag.TRANSITIVE) : EnumSet.noneOf(RequiresFlag.class);
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   701
            RequiresDirective d = new RequiresDirective(ms, flags);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   702
            directives.add(d);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   703
            requires.add(d);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   704
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   705
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   706
        RequiresDirective requiresUnnamed = new RequiresDirective(syms.unnamedModule);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   707
        directives.add(requiresUnnamed);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   708
        requires.add(requiresUnnamed);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   709
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   710
        msym.requires = requires.toList();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   711
        msym.directives = directives.toList();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   712
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   713
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   714
    private Completer getSourceCompleter(JCCompilationUnit tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   715
        return new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   716
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   717
            public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   718
                ModuleSymbol msym = (ModuleSymbol) sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   719
                msym.flags_field |= UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   720
                ModuleVisitor v = new ModuleVisitor();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   721
                JavaFileObject prev = log.useSource(tree.sourcefile);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   722
                JCModuleDecl moduleDecl = tree.getModuleDecl();
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   723
                DiagnosticPosition prevLintPos = deferredLintHandler.setPos(moduleDecl.pos());
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   724
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   725
                try {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   726
                    moduleDecl.accept(v);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   727
                    completeModule(msym);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   728
                    checkCyclicDependencies(moduleDecl);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   729
                } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   730
                    log.useSource(prev);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   731
                    deferredLintHandler.setPos(prevLintPos);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   732
                    msym.flags_field &= ~UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   733
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   734
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   735
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   736
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   737
            public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   738
                return "SourceCompleter: " + tree.sourcefile.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   739
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   740
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   741
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   742
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   743
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   744
    public boolean isRootModule(ModuleSymbol module) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   745
        Assert.checkNonNull(rootModules);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   746
        return rootModules.contains(module);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   747
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   748
44291
e1b620ac6c98 8175119: Need to specify module of types created by Filer.createSourceFile/Filer.createClassFile?
jlahoda
parents: 44290
diff changeset
   749
    public Set<ModuleSymbol> getRootModules() {
e1b620ac6c98 8175119: Need to specify module of types created by Filer.createSourceFile/Filer.createClassFile?
jlahoda
parents: 44290
diff changeset
   750
        Assert.checkNonNull(rootModules);
e1b620ac6c98 8175119: Need to specify module of types created by Filer.createSourceFile/Filer.createClassFile?
jlahoda
parents: 44290
diff changeset
   751
        return rootModules;
e1b620ac6c98 8175119: Need to specify module of types created by Filer.createSourceFile/Filer.createClassFile?
jlahoda
parents: 44290
diff changeset
   752
    }
e1b620ac6c98 8175119: Need to specify module of types created by Filer.createSourceFile/Filer.createClassFile?
jlahoda
parents: 44290
diff changeset
   753
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   754
    class ModuleVisitor extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   755
        private ModuleSymbol sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   756
        private final Set<ModuleSymbol> allRequires = new HashSet<>();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   757
        private final Map<PackageSymbol,List<ExportsDirective>> allExports = new HashMap<>();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   758
        private final Map<PackageSymbol,List<OpensDirective>> allOpens = new HashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   759
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   760
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   761
        public void visitModuleDef(JCModuleDecl tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   762
            sym = Assert.checkNonNull(tree.sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   763
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   764
            if (tree.getModuleType() == ModuleKind.OPEN) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   765
                sym.flags.add(ModuleFlags.OPEN);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   766
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   767
            sym.flags_field |= (tree.mods.flags & Flags.DEPRECATED);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   768
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   769
            sym.requires = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   770
            sym.exports = List.nil();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   771
            sym.opens = List.nil();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   772
            tree.directives.forEach(t -> t.accept(this));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   773
            sym.requires = sym.requires.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   774
            sym.exports = sym.exports.reverse();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   775
            sym.opens = sym.opens.reverse();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   776
            ensureJavaBase();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   777
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   778
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   779
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   780
        public void visitRequires(JCRequires tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   781
            ModuleSymbol msym = lookupModule(tree.moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   782
            if (msym.kind != MDL) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   783
                log.error(tree.moduleName.pos(), Errors.ModuleNotFound(msym));
43579
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
   784
                warnedMissing.add(msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   785
            } else if (allRequires.contains(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   786
                log.error(tree.moduleName.pos(), Errors.DuplicateRequires(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   787
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   788
                allRequires.add(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   789
                Set<RequiresFlag> flags = EnumSet.noneOf(RequiresFlag.class);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   790
                if (tree.isTransitive)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   791
                    flags.add(RequiresFlag.TRANSITIVE);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   792
                if (tree.isStaticPhase)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   793
                    flags.add(RequiresFlag.STATIC_PHASE);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   794
                RequiresDirective d = new RequiresDirective(msym, flags);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   795
                tree.directive = d;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   796
                sym.requires = sym.requires.prepend(d);
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
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   800
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   801
        public void visitExports(JCExports tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   802
            Name name = TreeInfo.fullName(tree.qualid);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   803
            PackageSymbol packge = syms.enterPackage(sym, name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   804
            attr.setPackageSymbols(tree.qualid, packge);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   805
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   806
            List<ExportsDirective> exportsForPackage = allExports.computeIfAbsent(packge, p -> List.nil());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   807
            for (ExportsDirective d : exportsForPackage) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   808
                reportExportsConflict(tree, packge);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   809
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   810
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   811
            List<ModuleSymbol> toModules = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   812
            if (tree.moduleNames != null) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   813
                Set<ModuleSymbol> to = new LinkedHashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   814
                for (JCExpression n: tree.moduleNames) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   815
                    ModuleSymbol msym = lookupModule(n);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   816
                    chk.checkModuleExists(n.pos(), msym);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   817
                    for (ExportsDirective d : exportsForPackage) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   818
                        checkDuplicateExportsToModule(n, msym, d);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   819
                    }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   820
                    if (!to.add(msym)) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   821
                        reportExportsConflictToModule(n, msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   822
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   823
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   824
                toModules = List.from(to);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   825
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   826
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   827
            if (toModules == null || !toModules.isEmpty()) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   828
                Set<ExportsFlag> flags = EnumSet.noneOf(ExportsFlag.class);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   829
                ExportsDirective d = new ExportsDirective(packge, toModules, flags);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   830
                sym.exports = sym.exports.prepend(d);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   831
                tree.directive = d;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   832
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   833
                allExports.put(packge, exportsForPackage.prepend(d));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   834
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   835
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   836
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   837
        private void reportExportsConflict(JCExports tree, PackageSymbol packge) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   838
            log.error(tree.qualid.pos(), Errors.ConflictingExports(packge));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   839
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   840
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   841
        private void checkDuplicateExportsToModule(JCExpression name, ModuleSymbol msym,
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   842
                ExportsDirective d) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   843
            if (d.modules != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   844
                for (ModuleSymbol other : d.modules) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   845
                    if (msym == other) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   846
                        reportExportsConflictToModule(name, msym);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   847
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   848
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   849
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   850
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   851
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   852
        private void reportExportsConflictToModule(JCExpression name, ModuleSymbol msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   853
            log.error(name.pos(), Errors.ConflictingExportsToModule(msym));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   854
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   855
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   856
        @Override
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   857
        public void visitOpens(JCOpens tree) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   858
            Name name = TreeInfo.fullName(tree.qualid);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   859
            PackageSymbol packge = syms.enterPackage(sym, name);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   860
            attr.setPackageSymbols(tree.qualid, packge);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   861
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   862
            if (sym.flags.contains(ModuleFlags.OPEN)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   863
                log.error(tree.pos(), Errors.NoOpensUnlessStrong);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   864
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   865
            List<OpensDirective> opensForPackage = allOpens.computeIfAbsent(packge, p -> List.nil());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   866
            for (OpensDirective d : opensForPackage) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   867
                reportOpensConflict(tree, packge);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   868
            }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   869
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   870
            List<ModuleSymbol> toModules = null;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   871
            if (tree.moduleNames != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   872
                Set<ModuleSymbol> to = new LinkedHashSet<>();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   873
                for (JCExpression n: tree.moduleNames) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   874
                    ModuleSymbol msym = lookupModule(n);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   875
                    chk.checkModuleExists(n.pos(), msym);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   876
                    for (OpensDirective d : opensForPackage) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   877
                        checkDuplicateOpensToModule(n, msym, d);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   878
                    }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   879
                    if (!to.add(msym)) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   880
                        reportOpensConflictToModule(n, msym);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   881
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   882
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   883
                toModules = List.from(to);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   884
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   885
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   886
            if (toModules == null || !toModules.isEmpty()) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   887
                Set<OpensFlag> flags = EnumSet.noneOf(OpensFlag.class);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   888
                OpensDirective d = new OpensDirective(packge, toModules, flags);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   889
                sym.opens = sym.opens.prepend(d);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   890
                tree.directive = d;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   891
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   892
                allOpens.put(packge, opensForPackage.prepend(d));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   893
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   894
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   895
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   896
        private void reportOpensConflict(JCOpens tree, PackageSymbol packge) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   897
            log.error(tree.qualid.pos(), Errors.ConflictingOpens(packge));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   898
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   899
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   900
        private void checkDuplicateOpensToModule(JCExpression name, ModuleSymbol msym,
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   901
                OpensDirective d) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   902
            if (d.modules != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   903
                for (ModuleSymbol other : d.modules) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   904
                    if (msym == other) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   905
                        reportOpensConflictToModule(name, msym);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   906
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   907
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   908
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   909
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   910
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   911
        private void reportOpensConflictToModule(JCExpression name, ModuleSymbol msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   912
            log.error(name.pos(), Errors.ConflictingOpensToModule(msym));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   913
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   914
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   915
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   916
        public void visitProvides(JCProvides tree) { }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   917
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   918
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   919
        public void visitUses(JCUses tree) { }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   920
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   921
        private void ensureJavaBase() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   922
            if (sym.name == names.java_base)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   923
                return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   924
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   925
            for (RequiresDirective d: sym.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   926
                if (d.module.name == names.java_base)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   927
                    return;
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
            ModuleSymbol java_base = syms.enterModule(names.java_base);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   931
            Directive.RequiresDirective d =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   932
                    new Directive.RequiresDirective(java_base,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   933
                            EnumSet.of(Directive.RequiresFlag.MANDATED));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   934
            sym.requires = sym.requires.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   935
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   936
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   937
        private ModuleSymbol lookupModule(JCExpression moduleName) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   938
            Name name = TreeInfo.fullName(moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   939
            ModuleSymbol msym = moduleFinder.findModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   940
            TreeInfo.setSymbol(moduleName, msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   941
            return msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   942
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   943
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   944
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   945
    public Completer getUsesProvidesCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   946
        return sym -> {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   947
            ModuleSymbol msym = (ModuleSymbol) sym;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   948
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   949
            msym.complete();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   950
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   951
            Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   952
            UsesProvidesVisitor v = new UsesProvidesVisitor(msym, env);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   953
            JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   954
            JCModuleDecl decl = env.toplevel.getModuleDecl();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   955
            DiagnosticPosition prevLintPos = deferredLintHandler.setPos(decl.pos());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   956
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   957
            try {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   958
                decl.accept(v);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   959
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   960
                log.useSource(prev);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   961
                deferredLintHandler.setPos(prevLintPos);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   962
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   963
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   964
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   965
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   966
    class UsesProvidesVisitor extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   967
        private final ModuleSymbol msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   968
        private final Env<AttrContext> env;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   969
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   970
        private final Set<ClassSymbol> allUses = new HashSet<>();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   971
        private final Map<ClassSymbol, Set<ClassSymbol>> allProvides = new HashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   972
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   973
        public UsesProvidesVisitor(ModuleSymbol msym, Env<AttrContext> env) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   974
            this.msym = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   975
            this.env = env;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   976
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   977
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   978
        @Override @SuppressWarnings("unchecked")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   979
        public void visitModuleDef(JCModuleDecl tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   980
            msym.directives = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   981
            msym.provides = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   982
            msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   983
            tree.directives.forEach(t -> t.accept(this));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   984
            msym.directives = msym.directives.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   985
            msym.provides = msym.provides.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   986
            msym.uses = msym.uses.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   987
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   988
            if (msym.requires.nonEmpty() && msym.requires.head.flags.contains(RequiresFlag.MANDATED))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   989
                msym.directives = msym.directives.prepend(msym.requires.head);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   990
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   991
            msym.directives = msym.directives.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   992
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   993
            checkForCorrectness();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   994
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   995
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   996
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   997
        public void visitExports(JCExports tree) {
44290
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
   998
            Iterable<Symbol> packageContent = tree.directive.packge.members().getSymbols();
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
   999
            List<JavaFileObject> filesToCheck = List.nil();
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1000
            boolean packageNotEmpty = false;
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1001
            for (Symbol sym : packageContent) {
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1002
                if (sym.kind != Kinds.Kind.TYP)
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1003
                    continue;
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1004
                ClassSymbol csym = (ClassSymbol) sym;
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1005
                if (sym.completer.isTerminal() ||
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1006
                    csym.classfile.getKind() == Kind.CLASS) {
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1007
                    packageNotEmpty = true;
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1008
                    filesToCheck = List.nil();
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1009
                    break;
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1010
                }
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1011
                if (csym.classfile.getKind() == Kind.SOURCE) {
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1012
                    filesToCheck = filesToCheck.prepend(csym.classfile);
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1013
                }
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1014
            }
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1015
            for (JavaFileObject jfo : filesToCheck) {
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1016
                if (findPackageInFile.findPackageNameOf(jfo) == tree.directive.packge.fullname) {
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1017
                    packageNotEmpty = true;
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1018
                    break;
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1019
                }
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1020
            }
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1021
            if (!packageNotEmpty) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1022
                log.error(tree.qualid.pos(), Errors.PackageEmptyOrNotFound(tree.directive.packge));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1023
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1024
            msym.directives = msym.directives.prepend(tree.directive);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1025
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1026
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1027
        @Override
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1028
        public void visitOpens(JCOpens tree) {
43588
6a0e0ea0e93b 8172901: javac: 'opens' statement cannot specify non observable package
jjg
parents: 43579
diff changeset
  1029
            chk.checkPackageExistsForOpens(tree.qualid, tree.directive.packge);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1030
            msym.directives = msym.directives.prepend(tree.directive);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1031
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1032
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1033
        MethodSymbol noArgsConstructor(ClassSymbol tsym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1034
            for (Symbol sym : tsym.members().getSymbolsByName(names.init)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1035
                MethodSymbol mSym = (MethodSymbol)sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1036
                if (mSym.params().isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1037
                    return mSym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1038
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1039
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1040
            return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1041
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1042
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1043
        MethodSymbol factoryMethod(ClassSymbol tsym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1044
            for (Symbol sym : tsym.members().getSymbolsByName(names.provider, sym -> sym.kind == MTH)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1045
                MethodSymbol mSym = (MethodSymbol)sym;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1046
                if (mSym.isStatic() && (mSym.flags() & Flags.PUBLIC) != 0 && mSym.params().isEmpty()) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1047
                    return mSym;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1048
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1049
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1050
            return null;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1051
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1052
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1053
        Map<Directive.ProvidesDirective, JCProvides> directiveToTreeMap = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1054
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1055
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1056
        public void visitProvides(JCProvides tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1057
            Type st = attr.attribType(tree.serviceName, env, syms.objectType);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1058
            ClassSymbol service = (ClassSymbol) st.tsym;
43577
1a1b1242f7aa 8172807: Javac doesn't report errors on duplicate provides with different service implementations
jjg
parents: 43271
diff changeset
  1059
            if (allProvides.containsKey(service)) {
1a1b1242f7aa 8172807: Javac doesn't report errors on duplicate provides with different service implementations
jjg
parents: 43271
diff changeset
  1060
                log.error(tree.serviceName.pos(), Errors.RepeatedProvidesForService(service));
1a1b1242f7aa 8172807: Javac doesn't report errors on duplicate provides with different service implementations
jjg
parents: 43271
diff changeset
  1061
            }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1062
            ListBuffer<ClassSymbol> impls = new ListBuffer<>();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1063
            for (JCExpression implName : tree.implNames) {
43865
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1064
                Type it;
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1065
                boolean prevVisitingServiceImplementation = env.info.visitingServiceImplementation;
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1066
                try {
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1067
                    env.info.visitingServiceImplementation = true;
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1068
                    it = attr.attribType(implName, env, syms.objectType);
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1069
                } finally {
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1070
                    env.info.visitingServiceImplementation = prevVisitingServiceImplementation;
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1071
                }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1072
                ClassSymbol impl = (ClassSymbol) it.tsym;
43865
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1073
                if ((impl.flags_field & PUBLIC) == 0) {
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1074
                    log.error(implName.pos(), Errors.NotDefPublic(impl, impl.location()));
532c50fea155 8174243: incorrect error message for nested service provider
vromero
parents: 43772
diff changeset
  1075
                }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1076
                //find provider factory:
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1077
                MethodSymbol factory = factoryMethod(impl);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1078
                if (factory != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1079
                    Type returnType = factory.type.getReturnType();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1080
                    if (!types.isSubtype(returnType, st)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1081
                        log.error(implName.pos(), Errors.ServiceImplementationProviderReturnMustBeSubtypeOfServiceInterface);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1082
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1083
                } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1084
                    if (!types.isSubtype(it, st)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1085
                        log.error(implName.pos(), Errors.ServiceImplementationMustBeSubtypeOfServiceInterface);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1086
                    } else if ((impl.flags() & ABSTRACT) != 0) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1087
                        log.error(implName.pos(), Errors.ServiceImplementationIsAbstract(impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1088
                    } else if (impl.isInner()) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1089
                        log.error(implName.pos(), Errors.ServiceImplementationIsInner(impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1090
                    } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1091
                        MethodSymbol constr = noArgsConstructor(impl);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1092
                        if (constr == null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1093
                            log.error(implName.pos(), Errors.ServiceImplementationDoesntHaveANoArgsConstructor(impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1094
                        } else if ((constr.flags() & PUBLIC) == 0) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1095
                            log.error(implName.pos(), Errors.ServiceImplementationNoArgsConstructorNotPublic(impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1096
                        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1097
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1098
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1099
                if (it.hasTag(CLASS)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1100
                    if (allProvides.computeIfAbsent(service, s -> new HashSet<>()).add(impl)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1101
                        impls.append(impl);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1102
                    } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1103
                        log.error(implName.pos(), Errors.DuplicateProvides(service, impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1104
                    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1105
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1106
            }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1107
            if (st.hasTag(CLASS) && !impls.isEmpty()) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1108
                Directive.ProvidesDirective d = new Directive.ProvidesDirective(service, impls.toList());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1109
                msym.provides = msym.provides.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1110
                msym.directives = msym.directives.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1111
                directiveToTreeMap.put(d, tree);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1112
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1113
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1114
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1115
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1116
        public void visitRequires(JCRequires tree) {
43271
ce89609dde7c 8172809: Error compiling javafx modules after fix for JDK-8169197
jlahoda
parents: 43270
diff changeset
  1117
            if (tree.directive != null && allModules().contains(tree.directive.module)) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1118
                chk.checkDeprecated(tree.moduleName.pos(), msym, tree.directive.module);
44576
9e18c9ce29e7 8178011: Automatic module warnings
jlahoda
parents: 44573
diff changeset
  1119
                chk.checkModuleRequires(tree.moduleName.pos(), tree.directive);
39915
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
  1120
                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
  1121
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1122
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1123
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1124
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1125
        public void visitUses(JCUses tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1126
            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
  1127
            Symbol sym = TreeInfo.symbol(tree.qualid);
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
  1128
            if ((sym.flags() & ENUM) != 0) {
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
  1129
                log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym));
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
  1130
            } else if (st.hasTag(CLASS)) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1131
                ClassSymbol service = (ClassSymbol) st.tsym;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1132
                if (allUses.add(service)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1133
                    Directive.UsesDirective d = new Directive.UsesDirective(service);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1134
                    msym.uses = msym.uses.prepend(d);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1135
                    msym.directives = msym.directives.prepend(d);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1136
                } else {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1137
                    log.error(tree.pos(), Errors.DuplicateUses(service));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1138
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1139
            }
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 checkForCorrectness() {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1143
            for (Directive.ProvidesDirective provides : msym.provides) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1144
                JCProvides tree = directiveToTreeMap.get(provides);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1145
                for (ClassSymbol impl : provides.impls) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1146
                    /* The implementation must be defined in the same module as the provides directive
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1147
                     * (else, error)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1148
                     */
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1149
                    PackageSymbol implementationDefiningPackage = impl.packge();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1150
                    if (implementationDefiningPackage.modle != msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1151
                        // TODO: should use tree for the implentation name, not the entire provides tree
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1152
                        // TODO: should improve error message to identify the implementation type
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1153
                        log.error(tree.pos(), Errors.ServiceImplementationNotInRightModule(implementationDefiningPackage.modle));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1154
                    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1155
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1156
                    /* There is no inherent requirement that module that provides a service should actually
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1157
                     * use it itself. However, it is a pointless declaration if the service package is not
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1158
                     * exported and there is no uses for the service.
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1159
                     */
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1160
                    PackageSymbol interfaceDeclaringPackage = provides.service.packge();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1161
                    boolean isInterfaceDeclaredInCurrentModule = interfaceDeclaringPackage.modle == msym;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1162
                    boolean isInterfaceExportedFromAReadableModule =
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1163
                            msym.visiblePackages.get(interfaceDeclaringPackage.fullname) == interfaceDeclaringPackage;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1164
                    if (isInterfaceDeclaredInCurrentModule && !isInterfaceExportedFromAReadableModule) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1165
                        // ok the interface is declared in this module. Let's check if it's exported
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1166
                        boolean warn = true;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1167
                        for (ExportsDirective export : msym.exports) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1168
                            if (interfaceDeclaringPackage == export.packge) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1169
                                warn = false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1170
                                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1171
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1172
                        }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1173
                        if (warn) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1174
                            for (UsesDirective uses : msym.uses) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1175
                                if (provides.service == uses.service) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1176
                                    warn = false;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1177
                                    break;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1178
                                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1179
                            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1180
                        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1181
                        if (warn) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1182
                            log.warning(tree.pos(), Warnings.ServiceProvidedButNotExportedOrUsed(provides.service));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1183
                        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1184
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1185
                }
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
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1190
    private Set<ModuleSymbol> allModules;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1191
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1192
    public Set<ModuleSymbol> allModules() {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1193
        Assert.checkNonNull(allModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1194
        return allModules;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1195
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1196
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
  1197
    private void setupAllModules() {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1198
        Assert.checkNonNull(rootModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1199
        Assert.checkNull(allModules);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1200
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1201
        Set<ModuleSymbol> observable;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1202
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1203
        if (limitModsOpt == null && extraLimitMods.isEmpty()) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1204
            observable = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1205
        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1206
            Set<ModuleSymbol> limitMods = new HashSet<>();
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1207
            if (limitModsOpt != null) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1208
                for (String limit : limitModsOpt.split(",")) {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1209
                    if (!isValidName(limit))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1210
                        continue;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1211
                    limitMods.add(syms.enterModule(names.fromString(limit)));
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1212
                }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1213
            }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1214
            for (String limit : extraLimitMods) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1215
                limitMods.add(syms.enterModule(names.fromString(limit)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1216
            }
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1217
            observable = computeTransitiveClosure(limitMods, rootModules, null);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1218
            observable.addAll(rootModules);
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1219
            if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1220
                for (ModuleSymbol msym : limitMods) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1221
                    if (!observable.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1222
                        log.warning(LintCategory.OPTIONS,
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1223
                                Warnings.ModuleForOptionNotFound(Option.LIMIT_MODULES, msym));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1224
                    }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1225
                }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1226
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1227
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1228
42841
1768507dae6b 8171412: tools/javac/modules/AddLimitMods.java failed with "error: module not found"
jjg
parents: 42827
diff changeset
  1229
        Predicate<ModuleSymbol> observablePred = sym ->
1768507dae6b 8171412: tools/javac/modules/AddLimitMods.java failed with "error: module not found"
jjg
parents: 42827
diff changeset
  1230
             (observable == null) ? (moduleFinder.findModule(sym).kind != ERR) : observable.contains(sym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1231
        Predicate<ModuleSymbol> systemModulePred = sym -> (sym.flags() & Flags.SYSTEM_MODULE) != 0;
43270
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1232
        Predicate<ModuleSymbol> noIncubatorPred = sym -> {
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1233
            sym.complete();
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1234
            return !sym.resolutionFlags.contains(ModuleResolutionFlags.DO_NOT_RESOLVE_BY_DEFAULT);
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1235
        };
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1236
        Set<ModuleSymbol> enabledRoot = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1237
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1238
        if (rootModules.contains(syms.unnamedModule)) {
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1239
            ModuleSymbol javaSE = syms.getModule(java_se);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1240
            Predicate<ModuleSymbol> jdkModulePred;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1241
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1242
            if (javaSE != null && (observable == null || observable.contains(javaSE))) {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1243
                jdkModulePred = sym -> {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1244
                    sym.complete();
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1245
                    return   !sym.name.startsWith(java_)
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1246
                           && sym.exports.stream().anyMatch(e -> e.modules == null);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1247
                };
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1248
                enabledRoot.add(javaSE);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1249
            } else {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1250
                jdkModulePred = sym -> true;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1251
            }
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1252
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1253
            for (ModuleSymbol sym : new HashSet<>(syms.getAllModules())) {
43270
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1254
                if (systemModulePred.test(sym) && observablePred.test(sym) && jdkModulePred.test(sym) && noIncubatorPred.test(sym)) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1255
                    enabledRoot.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1256
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1257
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1258
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1259
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1260
        enabledRoot.addAll(rootModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1261
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1262
        if (addModsOpt != null || !extraAddMods.isEmpty()) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1263
            Set<String> fullAddMods = new HashSet<>();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1264
            fullAddMods.addAll(extraAddMods);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1265
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1266
            if (addModsOpt != null) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1267
                fullAddMods.addAll(Arrays.asList(addModsOpt.split(",")));
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1268
            }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1269
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1270
            for (String added : fullAddMods) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1271
                Stream<ModuleSymbol> modules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1272
                switch (added) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1273
                    case ALL_SYSTEM:
43270
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1274
                        modules = new HashSet<>(syms.getAllModules())
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1275
                                .stream()
44573
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1276
                                .filter(systemModulePred.and(observablePred));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1277
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1278
                    case ALL_MODULE_PATH:
43270
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1279
                        modules = new HashSet<>(syms.getAllModules())
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1280
                                .stream()
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1281
                                .filter(systemModulePred.negate().and(observablePred));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1282
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1283
                    default:
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1284
                        if (!isValidName(added))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1285
                            continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1286
                        modules = Stream.of(syms.enterModule(names.fromString(added)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1287
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1288
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1289
                modules.forEach(sym -> {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1290
                    enabledRoot.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1291
                    if (observable != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1292
                        observable.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1293
                });
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1294
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1295
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1296
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1297
        Set<ModuleSymbol> result = computeTransitiveClosure(enabledRoot, rootModules, observable);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1298
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1299
        result.add(syms.unnamedModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1300
44573
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1301
        boolean hasAutomatic = result.stream().anyMatch(IS_AUTOMATIC);
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1302
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1303
        if (hasAutomatic) {
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1304
            syms.getAllModules()
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1305
                .stream()
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1306
                .filter(IS_AUTOMATIC)
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1307
                .forEach(result::add);
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1308
        }
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1309
43270
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1310
        String incubatingModules = result.stream()
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1311
                .filter(msym -> msym.resolutionFlags.contains(ModuleResolutionFlags.WARN_INCUBATING))
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1312
                .map(msym -> msym.name.toString())
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1313
                .collect(Collectors.joining(","));
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1314
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1315
        if (!incubatingModules.isEmpty()) {
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1316
            log.warning(Warnings.IncubatingModules(incubatingModules));
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1317
        }
de9a02e20567 8171177: Compiler should issue a warning for incubating modules that are resolved
jlahoda
parents: 43267
diff changeset
  1318
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1319
        allModules = result;
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1320
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1321
        //add module versions from options, if any:
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1322
        if (moduleVersionOpt != null) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1323
            Name version = names.fromString(moduleVersionOpt);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1324
            rootModules.forEach(m -> m.version = version);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1325
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1326
    }
44573
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1327
    //where:
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1328
        private static final Predicate<ModuleSymbol> IS_AUTOMATIC =
245bb4e6f983 8177530: Module system implementation refresh (4/2017)
alanb
parents: 44291
diff changeset
  1329
                m -> (m.flags_field & Flags.AUTOMATIC_MODULE) != 0;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1330
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1331
    public boolean isInModuleGraph(ModuleSymbol msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1332
        return allModules == null || allModules.contains(msym);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1333
    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1334
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1335
    private Set<ModuleSymbol> computeTransitiveClosure(Set<? extends ModuleSymbol> base,
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1336
                                                       Set<? extends ModuleSymbol> rootModules,
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1337
                                                       Set<ModuleSymbol> observable) {
43579
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1338
        List<ModuleSymbol> primaryTodo = List.nil();
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1339
        List<ModuleSymbol> secondaryTodo = List.nil();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1340
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1341
        for (ModuleSymbol ms : base) {
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1342
            if (rootModules.contains(ms)) {
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1343
                primaryTodo = primaryTodo.prepend(ms);
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1344
            } else {
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1345
                secondaryTodo = secondaryTodo.prepend(ms);
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1346
            }
36526
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
        Set<ModuleSymbol> result = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1350
        result.add(syms.java_base);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1351
43579
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1352
        while (primaryTodo.nonEmpty() || secondaryTodo.nonEmpty()) {
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1353
            ModuleSymbol current;
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1354
            boolean isPrimaryTodo;
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1355
            if (primaryTodo.nonEmpty()) {
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1356
                current = primaryTodo.head;
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1357
                primaryTodo = primaryTodo.tail;
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1358
                isPrimaryTodo = true;
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1359
            } else {
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1360
                current = secondaryTodo.head;
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1361
                secondaryTodo = secondaryTodo.tail;
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1362
                isPrimaryTodo = false;
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1363
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1364
            if (observable != null && !observable.contains(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1365
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1366
            if (!result.add(current) || current == syms.unnamedModule || ((current.flags_field & Flags.AUTOMATIC_MODULE) != 0))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1367
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1368
            current.complete();
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1369
            if (current.kind == ERR && (isPrimaryTodo || base.contains(current)) && warnedMissing.add(current)) {
43579
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1370
                log.error(Errors.ModuleNotFound(current));
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1371
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1372
            for (RequiresDirective rd : current.requires) {
43579
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1373
                if (rd.module == syms.java_base) continue;
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1374
                if ((rd.isTransitive() && isPrimaryTodo) || rootModules.contains(current)) {
43579
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1375
                    primaryTodo = primaryTodo.prepend(rd.module);
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1376
                } else {
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1377
                    secondaryTodo = secondaryTodo.prepend(rd.module);
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1378
                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1379
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1380
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1381
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1382
        return result;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1383
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1384
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1385
    public ModuleSymbol getObservableModule(Name name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1386
        ModuleSymbol mod = syms.getModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1387
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1388
        if (allModules().contains(mod)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1389
            return mod;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1390
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1391
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1392
        return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1393
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1394
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1395
    private Completer getUnnamedModuleCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1396
        moduleFinder.findAllModules();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1397
        return new Symbol.Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1398
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1399
            public void complete(Symbol sym) throws CompletionFailure {
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1400
                if (inInitModules) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1401
                    sym.completer = this;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1402
                    return ;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1403
                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1404
                ModuleSymbol msym = (ModuleSymbol) sym;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1405
                Set<ModuleSymbol> allModules = new HashSet<>(allModules());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1406
                allModules.remove(syms.unnamedModule);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1407
                for (ModuleSymbol m : allModules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1408
                    m.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1409
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1410
                initVisiblePackages(msym, allModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1411
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1412
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1413
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1414
            public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1415
                return "unnamedModule Completer";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1416
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1417
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1418
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1419
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1420
    private final Map<ModuleSymbol, Set<ModuleSymbol>> requiresTransitiveCache = new HashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1421
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1422
    private void completeModule(ModuleSymbol msym) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1423
        if (inInitModules) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1424
            msym.completer = sym -> completeModule(msym);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1425
            return ;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1426
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1427
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1428
        if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1429
            completeAutomaticModule(msym);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1430
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1431
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1432
        Assert.checkNonNull(msym.requires);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1433
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1434
        initAddReads();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1435
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1436
        msym.requires = msym.requires.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1437
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1438
        List<RequiresDirective> requires = msym.requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1439
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1440
        while (requires.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1441
            if (!allModules().contains(requires.head.module)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1442
                Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1443
                if (env != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1444
                    JavaFileObject origSource = log.useSource(env.toplevel.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1445
                    try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1446
                        log.error(/*XXX*/env.tree, Errors.ModuleNotFound(requires.head.module));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1447
                    } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1448
                        log.useSource(origSource);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1449
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1450
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1451
                    Assert.check((msym.flags() & Flags.AUTOMATIC_MODULE) == 0);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1452
                }
43271
ce89609dde7c 8172809: Error compiling javafx modules after fix for JDK-8169197
jlahoda
parents: 43270
diff changeset
  1453
                msym.requires = List.filter(msym.requires, requires.head);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1454
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1455
            requires = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1456
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1457
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1458
        Set<ModuleSymbol> readable = new LinkedHashSet<>();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1459
        Set<ModuleSymbol> requiresTransitive = new HashSet<>();
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1460
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1461
        for (RequiresDirective d : msym.requires) {
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1462
            d.module.complete();
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1463
            readable.add(d.module);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1464
            Set<ModuleSymbol> s = retrieveRequiresTransitive(d.module);
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1465
            Assert.checkNonNull(s, () -> "no entry in cache for " + d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1466
            readable.addAll(s);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1467
            if (d.flags.contains(RequiresFlag.TRANSITIVE)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1468
                requiresTransitive.add(d.module);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1469
                requiresTransitive.addAll(s);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1470
            }
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1471
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1472
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1473
        requiresTransitiveCache.put(msym, requiresTransitive);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1474
        initVisiblePackages(msym, readable);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1475
        for (ExportsDirective d: msym.exports) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1476
            if (d.packge != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1477
                d.packge.modle = msym;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1478
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1479
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1480
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1481
        if (!allowAccessIntoSystem && (msym.flags() & Flags.SYSTEM_MODULE) != 0 &&
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1482
            msym.patchLocation != null) {
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1483
            log.error(Errors.PatchModuleWithRelease(msym));
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1484
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1485
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1486
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1487
    private Set<ModuleSymbol> retrieveRequiresTransitive(ModuleSymbol msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1488
        Set<ModuleSymbol> requiresTransitive = requiresTransitiveCache.get(msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1489
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1490
        if (requiresTransitive == null) {
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40604
diff changeset
  1491
            //the module graph may contain cycles involving automatic modules or --add-reads edges
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1492
            requiresTransitive = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1493
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1494
            Set<ModuleSymbol> seen = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1495
            List<ModuleSymbol> todo = List.of(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1496
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1497
            while (todo.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1498
                ModuleSymbol current = todo.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1499
                todo = todo.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1500
                if (!seen.add(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1501
                    continue;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1502
                requiresTransitive.add(current);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1503
                current.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1504
                Iterable<? extends RequiresDirective> requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1505
                if (current != syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1506
                    Assert.checkNonNull(current.requires, () -> current + ".requires == null; " + msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1507
                    requires = current.requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1508
                    for (RequiresDirective rd : requires) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1509
                        if (rd.isTransitive())
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1510
                            todo = todo.prepend(rd.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1511
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1512
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1513
                    for (ModuleSymbol mod : allModules()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1514
                        todo = todo.prepend(mod);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1515
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1516
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1517
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1518
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1519
            requiresTransitive.remove(msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1520
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1521
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1522
        return requiresTransitive;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1523
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1524
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1525
    private void initVisiblePackages(ModuleSymbol msym, Collection<ModuleSymbol> readable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1526
        initAddExports();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1527
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1528
        msym.visiblePackages = new LinkedHashMap<>();
43138
680d378b9d64 8169197: Improve error reporting for compiling against unexported package
jlahoda
parents: 42841
diff changeset
  1529
        msym.readModules = new HashSet<>(readable);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1530
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1531
        Map<Name, ModuleSymbol> seen = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1532
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1533
        for (ModuleSymbol rm : readable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1534
            if (rm == syms.unnamedModule)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1535
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1536
            addVisiblePackages(msym, seen, rm, rm.exports);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1537
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1538
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1539
        addExports.forEach((exportsFrom, exports) -> {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1540
            addVisiblePackages(msym, seen, exportsFrom, exports);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1541
        });
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1542
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1543
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1544
    private void addVisiblePackages(ModuleSymbol msym,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1545
                                    Map<Name, ModuleSymbol> seenPackages,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1546
                                    ModuleSymbol exportsFrom,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1547
                                    Collection<ExportsDirective> exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1548
        for (ExportsDirective d : exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1549
            if (d.modules == null || d.modules.contains(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1550
                Name packageName = d.packge.fullname;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1551
                ModuleSymbol previousModule = seenPackages.get(packageName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1552
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1553
                if (previousModule != null && previousModule != exportsFrom) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1554
                    Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1555
                    JavaFileObject origSource = env != null ? log.useSource(env.toplevel.sourcefile)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1556
                                                            : null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1557
                    DiagnosticPosition pos = env != null ? env.tree.pos() : null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1558
                    try {
45682
fc3b228b9e2a 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45156
diff changeset
  1559
                        if (msym.isUnnamed()) {
fc3b228b9e2a 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45156
diff changeset
  1560
                            log.error(pos, Errors.PackageClashFromRequiresInUnnamed(packageName,
fc3b228b9e2a 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45156
diff changeset
  1561
                                                                                    previousModule, exportsFrom));
fc3b228b9e2a 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45156
diff changeset
  1562
                        } else {
fc3b228b9e2a 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45156
diff changeset
  1563
                            log.error(pos, Errors.PackageClashFromRequires(msym, packageName,
fc3b228b9e2a 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45156
diff changeset
  1564
                                                                           previousModule, exportsFrom));
fc3b228b9e2a 8181087: Module system implementation refresh (6/2017)
alanb
parents: 45156
diff changeset
  1565
                        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1566
                    } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1567
                        if (env != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1568
                            log.useSource(origSource);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1569
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1570
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1571
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1572
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1573
                seenPackages.put(packageName, exportsFrom);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1574
                msym.visiblePackages.put(d.packge.fullname, d.packge);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1575
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1576
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1577
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1578
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1579
    private void initAddExports() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1580
        if (addExports != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1581
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1582
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1583
        addExports = new LinkedHashMap<>();
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1584
        Set<ModuleSymbol> unknownModules = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1585
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1586
        if (addExportsOpt == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1587
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1588
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1589
        Pattern ep = Pattern.compile("([^/]+)/([^=]+)=(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1590
        for (String s: addExportsOpt.split("\0+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1591
            if (s.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1592
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1593
            Matcher em = ep.matcher(s);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1594
            if (!em.matches()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1595
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1596
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1597
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1598
            // Terminology comes from
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40604
diff changeset
  1599
            //  --add-exports module/package=target,...
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1600
            // Compare to
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1601
            //  module module { exports package to target, ... }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1602
            String moduleName = em.group(1);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1603
            String packageName = em.group(2);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1604
            String targetNames = em.group(3);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1605
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1606
            if (!isValidName(moduleName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1607
                continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1608
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1609
            ModuleSymbol msym = syms.enterModule(names.fromString(moduleName));
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1610
            if (!isKnownModule(msym, unknownModules))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1611
                continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1612
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1613
            if (!isValidName(packageName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1614
                continue;
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1615
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1616
            if (!allowAccessIntoSystem && (msym.flags() & Flags.SYSTEM_MODULE) != 0) {
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1617
                log.error(Errors.AddExportsWithRelease(msym));
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1618
                continue;
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1619
            }
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1620
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1621
            PackageSymbol p = syms.enterPackage(msym, names.fromString(packageName));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1622
            p.modle = msym;  // TODO: do we need this?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1623
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1624
            List<ModuleSymbol> targetModules = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1625
            for (String toModule : targetNames.split("[ ,]+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1626
                ModuleSymbol m;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1627
                if (toModule.equals("ALL-UNNAMED")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1628
                    m = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1629
                } else {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1630
                    if (!isValidName(toModule))
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1631
                        continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1632
                    m = syms.enterModule(names.fromString(toModule));
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1633
                    if (!isKnownModule(m, unknownModules))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1634
                        continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1635
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1636
                targetModules = targetModules.prepend(m);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1637
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1638
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1639
            Set<ExportsDirective> extra = addExports.computeIfAbsent(msym, _x -> new LinkedHashSet<>());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1640
            ExportsDirective d = new ExportsDirective(p, targetModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1641
            extra.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1642
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1643
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1644
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1645
    private boolean isKnownModule(ModuleSymbol msym, Set<ModuleSymbol> unknownModules) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1646
        if (allModules.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1647
            return true;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1648
        }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1649
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1650
        if (!unknownModules.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1651
            if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1652
                log.warning(LintCategory.OPTIONS,
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1653
                        Warnings.ModuleForOptionNotFound(Option.ADD_EXPORTS, msym));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1654
            }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1655
            unknownModules.add(msym);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1656
        }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1657
        return false;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1658
    }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1659
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1660
    private void initAddReads() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1661
        if (addReads != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1662
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1663
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1664
        addReads = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1665
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1666
        if (addReadsOpt == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1667
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1668
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1669
        Pattern rp = Pattern.compile("([^=]+)=(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1670
        for (String s : addReadsOpt.split("\0+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1671
            if (s.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1672
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1673
            Matcher rm = rp.matcher(s);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1674
            if (!rm.matches()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1675
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1676
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1677
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1678
            // Terminology comes from
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1679
            //  --add-reads source-module=target-module,...
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1680
            // Compare to
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1681
            //  module source-module { requires target-module; ... }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1682
            String sourceName = rm.group(1);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1683
            String targetNames = rm.group(2);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1684
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1685
            if (!isValidName(sourceName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1686
                continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1687
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1688
            ModuleSymbol msym = syms.enterModule(names.fromString(sourceName));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1689
            if (!allModules.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1690
                if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1691
                    log.warning(Warnings.ModuleForOptionNotFound(Option.ADD_READS, msym));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1692
                }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1693
                continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1694
            }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1695
45156
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1696
            if (!allowAccessIntoSystem && (msym.flags() & Flags.SYSTEM_MODULE) != 0) {
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1697
                log.error(Errors.AddReadsWithRelease(msym));
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1698
                continue;
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1699
            }
001f73134346 8178152: Handling of incubating modules, the jdk.unsupported module and --add-exports with --release <current>
jlahoda
parents: 44822
diff changeset
  1700
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1701
            for (String targetName : targetNames.split("[ ,]+", -1)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1702
                ModuleSymbol targetModule;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1703
                if (targetName.equals("ALL-UNNAMED")) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1704
                    targetModule = syms.unnamedModule;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1705
                } else {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1706
                    if (!isValidName(targetName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1707
                        continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1708
                    targetModule = syms.enterModule(names.fromString(targetName));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1709
                    if (!allModules.contains(targetModule)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1710
                        if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1711
                            log.warning(LintCategory.OPTIONS, Warnings.ModuleForOptionNotFound(Option.ADD_READS, targetModule));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1712
                        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1713
                        continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1714
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1715
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1716
                addReads.computeIfAbsent(msym, m -> new HashSet<>())
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1717
                        .add(new RequiresDirective(targetModule, EnumSet.of(RequiresFlag.EXTRA)));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1718
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1719
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1720
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1721
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1722
    private void checkCyclicDependencies(JCModuleDecl mod) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1723
        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
  1724
            JCRequires rd;
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
  1725
            if (!d.hasTag(Tag.REQUIRES) || (rd = (JCRequires) d).directive == null)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1726
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1727
            Set<ModuleSymbol> nonSyntheticDeps = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1728
            List<ModuleSymbol> queue = List.of(rd.directive.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1729
            while (queue.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1730
                ModuleSymbol current = queue.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1731
                queue = queue.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1732
                if (!nonSyntheticDeps.add(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1733
                    continue;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1734
                current.complete();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1735
                if ((current.flags() & Flags.ACYCLIC) != 0)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1736
                    continue;
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1737
                Assert.checkNonNull(current.requires, current::toString);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1738
                for (RequiresDirective dep : current.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1739
                    if (!dep.flags.contains(RequiresFlag.EXTRA))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1740
                        queue = queue.prepend(dep.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1741
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1742
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1743
            if (nonSyntheticDeps.contains(mod.sym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1744
                log.error(rd.moduleName.pos(), Errors.CyclicRequires(rd.directive.module));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1745
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1746
            mod.sym.flags_field |= Flags.ACYCLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1747
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1748
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1749
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1750
    private boolean isValidName(CharSequence name) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1751
        return SourceVersion.isName(name, Source.toSourceVersion(source));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1752
    }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1753
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1754
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1755
    private String toString(ModuleSymbol msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1756
        return msym.name + "["
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1757
                + "kind:" + msym.kind + ";"
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1758
                + "locn:" + toString(msym.sourceLocation) + "," + toString(msym.classLocation) + ";"
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1759
                + "info:" + toString(msym.module_info.sourcefile) + ","
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1760
                            + toString(msym.module_info.classfile) + ","
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1761
                            + msym.module_info.completer
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1762
                + "]";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1763
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1764
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1765
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1766
    String toString(Location locn) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1767
        return (locn == null) ? "--" : locn.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1768
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1769
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1770
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1771
    String toString(JavaFileObject fo) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1772
        return (fo == null) ? "--" : fo.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1773
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1774
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1775
    public void newRound() {
43772
4e5350b7be75 8173777: Merge javac -Xmodule into javac--patch-module
jlahoda
parents: 43588
diff changeset
  1776
        allModules = null;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1777
        rootModules = null;
43579
0d00f3ea77bc 8172240: javac should not need the transitive closure to compile a module
vromero
parents: 43577
diff changeset
  1778
        warnedMissing.clear();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1779
    }
44290
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1780
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1781
    public interface PackageNameFinder {
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1782
        public Name findPackageNameOf(JavaFileObject jfo);
202973b2d1ae 8176045: No compile error when a package is not declared
jlahoda
parents: 44289
diff changeset
  1783
    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1784
}