langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Modules.java
author jjg
Tue, 20 Dec 2016 05:50:39 -0800
changeset 42841 1768507dae6b
parent 42827 36468b5fa7f4
child 43138 680d378b9d64
permissions -rw-r--r--
8171412: tools/javac/modules/AddLimitMods.java failed with "error: module not found" Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    25
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
package com.sun.tools.javac.comp;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.Arrays;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.Collection;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.Collections;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.EnumSet;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.HashMap;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.HashSet;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.LinkedHashMap;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.util.LinkedHashSet;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.Map;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.Set;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
    40
import java.util.function.Consumer;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.function.Predicate;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.regex.Matcher;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.regex.Pattern;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.stream.Stream;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import javax.lang.model.SourceVersion;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import javax.tools.JavaFileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import javax.tools.JavaFileManager.Location;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import javax.tools.JavaFileObject;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import javax.tools.JavaFileObject.Kind;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import javax.tools.StandardLocation;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    53
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
    54
import com.sun.tools.javac.code.ClassFinder;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    55
import com.sun.tools.javac.code.DeferredLintHandler;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import com.sun.tools.javac.code.Directive;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import com.sun.tools.javac.code.Directive.ExportsDirective;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    58
import com.sun.tools.javac.code.Directive.ExportsFlag;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    59
import com.sun.tools.javac.code.Directive.OpensDirective;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    60
import com.sun.tools.javac.code.Directive.OpensFlag;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
import com.sun.tools.javac.code.Directive.RequiresDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
import com.sun.tools.javac.code.Directive.RequiresFlag;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
import com.sun.tools.javac.code.Directive.UsesDirective;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
import com.sun.tools.javac.code.Flags;
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
    65
import com.sun.tools.javac.code.Lint.LintCategory;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
import com.sun.tools.javac.code.ModuleFinder;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
import com.sun.tools.javac.code.Source;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
import com.sun.tools.javac.code.Symbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
import com.sun.tools.javac.code.Symbol.ClassSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
import com.sun.tools.javac.code.Symbol.Completer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
import com.sun.tools.javac.code.Symbol.CompletionFailure;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
import com.sun.tools.javac.code.Symbol.MethodSymbol;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    73
import com.sun.tools.javac.code.Symbol.ModuleFlags;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
import com.sun.tools.javac.code.Symbol.PackageSymbol;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
import com.sun.tools.javac.code.Symtab;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
import com.sun.tools.javac.code.Type;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
    78
import com.sun.tools.javac.code.Types;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
import com.sun.tools.javac.jvm.ClassWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
import com.sun.tools.javac.jvm.JNIWriter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
import com.sun.tools.javac.main.Option;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
import com.sun.tools.javac.resources.CompilerProperties.Errors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
import com.sun.tools.javac.tree.JCTree;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    86
import com.sun.tools.javac.tree.JCTree.JCDirective;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
import com.sun.tools.javac.tree.JCTree.JCExports;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
import com.sun.tools.javac.tree.JCTree.JCExpression;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
    90
import com.sun.tools.javac.tree.JCTree.JCOpens;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    91
import com.sun.tools.javac.tree.JCTree.JCPackageDecl;
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;
42841
1768507dae6b 8171412: tools/javac/modules/AddLimitMods.java failed with "error: module not found"
jjg
parents: 42827
diff changeset
   114
import static com.sun.tools.javac.code.Kinds.Kind.ERR;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   115
import static com.sun.tools.javac.code.Kinds.Kind.MDL;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   116
import static com.sun.tools.javac.code.Kinds.Kind.MTH;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   117
import static com.sun.tools.javac.code.TypeTag.CLASS;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
/**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
 *  TODO: fill in
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
 *  <p><b>This is NOT part of any supported API.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
 *  If you write code that depends on this, you do so at your own risk.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
 *  This code and its internal interfaces are subject to change or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
 *  deletion without notice.</b>
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
public class Modules extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
    private static final String ALL_SYSTEM = "ALL-SYSTEM";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
    private static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
    private final Log log;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
    private final Names names;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
    private final Symtab syms;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   134
    private final Attr attr;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   135
    private final Check chk;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   136
    private final DeferredLintHandler deferredLintHandler;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
    private final TypeEnvs typeEnvs;
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   138
    private final Types types;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
    private final JavaFileManager fileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   140
    private final ModuleFinder moduleFinder;
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   141
    private final Source source;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   142
    private final boolean allowModules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   143
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
    public final boolean multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   145
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    private final String moduleOverride;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   148
    private final Name java_se;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   149
    private final Name java_;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   150
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   151
    ModuleSymbol defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   153
    private final String addExportsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
    private Map<ModuleSymbol, Set<ExportsDirective>> addExports;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   155
    private final String addReadsOpt;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   156
    private Map<ModuleSymbol, Set<RequiresDirective>> addReads;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   157
    private final String addModsOpt;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   158
    private final Set<String> extraAddMods = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   159
    private final String limitModsOpt;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   160
    private final Set<String> extraLimitMods = new HashSet<>();
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   161
    private final String moduleVersionOpt;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   162
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   163
    private final boolean lintOptions;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   164
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   165
    private Set<ModuleSymbol> rootModules = null;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   166
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   167
    public static Modules instance(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        Modules instance = context.get(Modules.class);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   169
        if (instance == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
            instance = new Modules(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
        return instance;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   173
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   174
    protected Modules(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        context.put(Modules.class, this);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   176
        log = Log.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
        names = Names.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        syms = Symtab.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        attr = Attr.instance(context);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   180
        chk = Check.instance(context);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   181
        deferredLintHandler = DeferredLintHandler.instance(context);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
        typeEnvs = TypeEnvs.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        moduleFinder = ModuleFinder.instance(context);
38827
884d32899770 8152062: obscure error message for bad 'provides'
vromero
parents: 38619
diff changeset
   184
        types = Types.instance(context);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
        fileManager = context.get(JavaFileManager.class);
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   186
        source = Source.instance(context);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   187
        allowModules = source.allowModules();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   188
        Options options = Options.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   190
        lintOptions = options.isUnset(Option.XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   191
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   192
        moduleOverride = options.get(Option.XMODULE);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   193
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   194
        multiModuleMode = fileManager.hasLocation(StandardLocation.MODULE_SOURCE_PATH);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   195
        ClassWriter classWriter = ClassWriter.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   196
        classWriter.multiModuleMode = multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        JNIWriter jniWriter = JNIWriter.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   198
        jniWriter.multiModuleMode = multiModuleMode;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   199
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   200
        java_se = names.fromString("java.se");
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   201
        java_ = names.fromString("java.");
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
   202
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   203
        addExportsOpt = options.get(Option.ADD_EXPORTS);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   204
        addReadsOpt = options.get(Option.ADD_READS);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   205
        addModsOpt = options.get(Option.ADD_MODULES);
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 39917
diff changeset
   206
        limitModsOpt = options.get(Option.LIMIT_MODULES);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   207
        moduleVersionOpt = options.get(Option.MODULE_VERSION);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   208
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   209
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   210
    int depth = -1;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   211
    private void dprintln(String msg) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   212
        for (int i = 0; i < depth; i++)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   213
            System.err.print("  ");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        System.err.println(msg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   215
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   216
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   217
    public void addExtraAddModules(String... extras) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   218
        extraAddMods.addAll(Arrays.asList(extras));
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   219
    }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   220
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   221
    public void addExtraLimitModules(String... extras) {
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   222
        extraLimitMods.addAll(Arrays.asList(extras));
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   223
    }
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   224
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   225
    boolean inInitModules;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   226
    public void initModules(List<JCCompilationUnit> trees) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   227
        Assert.check(!inInitModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   228
        try {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   229
            inInitModules = true;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   230
            Assert.checkNull(rootModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   231
            enter(trees, modules -> {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   232
                Assert.checkNull(rootModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   233
                Assert.checkNull(allModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   234
                this.rootModules = modules;
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
   235
                setupAllModules(); //initialize the module graph
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   236
                Assert.checkNonNull(allModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   237
                inInitModules = false;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   238
            }, null);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   239
        } finally {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   240
            inInitModules = false;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   241
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   242
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   243
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   244
    public boolean enter(List<JCCompilationUnit> trees, ClassSymbol c) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   245
        Assert.check(rootModules != null || inInitModules || !allowModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   246
        return enter(trees, modules -> {}, c);
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
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   249
    private boolean enter(List<JCCompilationUnit> trees, Consumer<Set<ModuleSymbol>> init, ClassSymbol c) {
38619
27c0007bb28d 8152785: Remove javac -XDnoModules
vromero
parents: 37854
diff changeset
   250
        if (!allowModules) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   251
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   252
                tree.modle = syms.noModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   253
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   254
            defaultModule = syms.noModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   255
            return true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   256
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   257
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   258
        int startErrors = log.nerrors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
        depth++;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   261
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   262
            // scan trees for module defs
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   263
            Set<ModuleSymbol> roots = enterModules(trees, c);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42823
diff changeset
   265
            setCompilationUnitModules(trees, roots, c);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   266
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   267
            init.accept(roots);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   268
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   269
            for (ModuleSymbol msym: roots) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                msym.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   271
            }
39361
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   272
        } catch (CompletionFailure ex) {
cd0aca7db174 8159439: javac throws NPE with Module attribute and super_class != 0
vromero
parents: 38827
diff changeset
   273
            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
   274
            if (ex instanceof ClassFinder.BadClassFile) throw new Abort();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   275
        } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   276
            depth--;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   277
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   278
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   279
        return (log.nerrors == startErrors);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   280
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   281
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   282
    public Completer getCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   283
        return mainCompleter;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   284
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   285
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
    public ModuleSymbol getDefaultModule() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   287
        return defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   288
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   289
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   290
    public boolean modulesInitialized() {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   291
        return allModules != null;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   292
    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   293
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   294
    private Set<ModuleSymbol> enterModules(List<JCCompilationUnit> trees, ClassSymbol c) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   295
        Set<ModuleSymbol> modules = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   296
        for (JCCompilationUnit tree : trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   299
                enterModule(tree, c, modules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   300
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   301
                log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   302
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   303
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   304
        return modules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   305
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   306
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   307
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   308
    private void enterModule(JCCompilationUnit toplevel, ClassSymbol c, Set<ModuleSymbol> modules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   309
        boolean isModuleInfo = toplevel.sourcefile.isNameCompatible("module-info", Kind.SOURCE);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   310
        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
   311
        if (isModuleDecl) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   312
            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
   313
            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
   314
                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
   315
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   316
            Name name = TreeInfo.fullName(decl.qualId);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   317
            ModuleSymbol sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   318
            if (c != null) {
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   319
                sym = (ModuleSymbol) c.owner;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   320
                Assert.checkNonNull(sym.name);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   321
                Name treeName = TreeInfo.fullName(decl.qualId);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   322
                if (sym.name != treeName) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   323
                    log.error(decl.pos(), Errors.ModuleNameMismatch(name, sym.name));
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
   324
                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   325
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   326
                sym = syms.enterModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   327
                if (sym.module_info.sourcefile != null && sym.module_info.sourcefile != toplevel.sourcefile) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   328
                    log.error(decl.pos(), Errors.DuplicateModule(sym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   329
                    return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   331
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   332
            sym.completer = getSourceCompleter(toplevel);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   333
            sym.module_info.sourcefile = toplevel.sourcefile;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   334
            decl.sym = sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   335
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   336
            if (multiModuleMode || modules.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   337
                modules.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   338
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   339
                log.error(toplevel.pos(), Errors.TooManyModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   340
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   341
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   342
            Env<AttrContext> provisionalEnv = new Env<>(decl, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   343
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   344
            provisionalEnv.toplevel = toplevel;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   345
            typeEnvs.put(sym, provisionalEnv);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   346
        } else if (isModuleInfo) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   347
            if (multiModuleMode) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   348
                JCTree tree = toplevel.defs.isEmpty() ? toplevel : toplevel.defs.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   349
                log.error(tree.pos(), Errors.ExpectedModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   350
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   351
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   352
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   353
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42823
diff changeset
   354
    private void setCompilationUnitModules(List<JCCompilationUnit> trees, Set<ModuleSymbol> rootModules, ClassSymbol c) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   355
        // update the module for each compilation unit
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        if (multiModuleMode) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   357
            checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   358
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   359
                if (tree.defs.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   360
                    tree.modle = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   361
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   362
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   363
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   364
                JavaFileObject prev = log.useSource(tree.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   365
                try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   366
                    Location locn = getModuleLocation(tree);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   367
                    if (locn != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   368
                        Name name = names.fromString(fileManager.inferModuleName(locn));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   369
                        ModuleSymbol msym;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   370
                        JCModuleDecl decl = tree.getModuleDecl();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   371
                        if (decl != null) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   372
                            msym = decl.sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   373
                            if (msym.name != name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   374
                                log.error(decl.qualId, Errors.ModuleNameMismatch(msym.name, name));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   375
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   376
                        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   377
                            msym = syms.enterModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   378
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   379
                        if (msym.sourceLocation == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   380
                            msym.sourceLocation = locn;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   381
                            if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41999
diff changeset
   382
                                msym.classLocation = fileManager.getLocationForModule(
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   383
                                        StandardLocation.CLASS_OUTPUT, msym.name.toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   384
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   385
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   386
                        tree.modle = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   387
                        rootModules.add(msym);
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42823
diff changeset
   388
                    } else if (c != null && c.packge().modle == syms.unnamedModule) {
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42823
diff changeset
   389
                        tree.modle = syms.unnamedModule;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   390
                    } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   391
                        log.error(tree.pos(), Errors.UnnamedPkgNotAllowedNamedModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   392
                        tree.modle = syms.errModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   393
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   394
                } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   395
                    throw new Error(e); // FIXME
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   397
                    log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   398
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   399
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   400
            if (syms.unnamedModule.sourceLocation == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   401
                syms.unnamedModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   402
                syms.unnamedModule.sourceLocation = StandardLocation.SOURCE_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   403
                syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   404
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   405
            defaultModule = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   406
        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   407
            if (defaultModule == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   408
                switch (rootModules.size()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   409
                    case 0:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   410
                        defaultModule = moduleFinder.findSingleModule();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   411
                        if (defaultModule == syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   412
                            if (moduleOverride != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   413
                                checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   414
                                defaultModule = moduleFinder.findModule(names.fromString(moduleOverride));
40768
8b6a878d8773 8165008: javac -Xmodule compiles the module in a way that reads the unnamed module
jlahoda
parents: 40762
diff changeset
   415
                                defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   416
                            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   417
                                // Question: why not do findAllModules and initVisiblePackages here?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   418
                                // i.e. body of unnamedModuleCompleter
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   419
                                defaultModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   420
                                defaultModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   421
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   422
                        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   423
                            checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleClasspath);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   424
                            checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   425
                            defaultModule.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   426
                            // Question: why not do completeModule here?
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
   427
                            defaultModule.completer = sym -> completeModule((ModuleSymbol) sym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   428
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   429
                        rootModules.add(defaultModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   430
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   431
                    case 1:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   432
                        checkSpecifiedModule(trees, Errors.ModuleInfoWithXmoduleSourcepath);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   433
                        checkNoAllModulePath();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   434
                        defaultModule = rootModules.iterator().next();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   435
                        defaultModule.classLocation = StandardLocation.CLASS_OUTPUT;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   436
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   437
                    default:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   438
                        Assert.error("too many modules");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   439
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   440
                defaultModule.sourceLocation = StandardLocation.SOURCE_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            } else if (rootModules.size() == 1 && defaultModule == rootModules.iterator().next()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   442
                defaultModule.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   443
                defaultModule.completer = sym -> completeModule((ModuleSymbol) sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   444
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   445
                Assert.check(rootModules.isEmpty());
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   446
                rootModules.add(defaultModule);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   447
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   448
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   449
            if (defaultModule != syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   450
                syms.unnamedModule.completer = getUnnamedModuleCompleter();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   451
                syms.unnamedModule.classLocation = StandardLocation.CLASS_PATH;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   452
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   453
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   454
            for (JCCompilationUnit tree: trees) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   455
                tree.modle = defaultModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   456
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   457
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   458
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   459
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   460
    private Location getModuleLocation(JCCompilationUnit tree) throws IOException {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   461
        if (tree.getModuleDecl() != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   462
            return getModuleLocation(tree.sourcefile, null);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   463
        } else if (tree.getPackage() != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   464
            JCPackageDecl pkg = tree.getPackage();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   465
            return getModuleLocation(tree.sourcefile, TreeInfo.fullName(pkg.pid));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   466
        } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   467
            // code in unnamed module
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   468
            return null;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   469
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   470
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   471
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   472
    private Location getModuleLocation(JavaFileObject fo, Name pkgName) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   473
        // For now, just check module source path.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   474
        // We may want to check source path as well.
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   475
        Location loc =
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   476
                fileManager.getLocationForModule(StandardLocation.MODULE_SOURCE_PATH,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   477
                                                 fo, (pkgName == null) ? null : pkgName.toString());
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   478
        if (loc == null) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   479
            Location sourceOutput = fileManager.hasLocation(StandardLocation.SOURCE_OUTPUT) ?
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   480
                    StandardLocation.SOURCE_OUTPUT : StandardLocation.CLASS_OUTPUT;
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   481
            loc =
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   482
                fileManager.getLocationForModule(sourceOutput,
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   483
                                                 fo, (pkgName == null) ? null : pkgName.toString());
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   484
        }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   485
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   486
        return loc;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   487
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   488
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   489
    private void checkSpecifiedModule(List<JCCompilationUnit> trees, JCDiagnostic.Error error) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   490
        if (moduleOverride != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   491
            JavaFileObject prev = log.useSource(trees.head.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   492
            try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   493
                log.error(trees.head.pos(), error);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   494
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   495
                log.useSource(prev);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   496
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   497
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   498
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   499
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   500
    private void checkNoAllModulePath() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   501
        if (addModsOpt != null && Arrays.asList(addModsOpt.split(",")).contains(ALL_MODULE_PATH)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   502
            log.error(Errors.AddmodsAllModulePathInvalid);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   503
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   504
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   505
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   506
    private final Completer mainCompleter = new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   507
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   508
        public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   509
            ModuleSymbol msym = moduleFinder.findModule((ModuleSymbol) sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   510
42841
1768507dae6b 8171412: tools/javac/modules/AddLimitMods.java failed with "error: module not found"
jjg
parents: 42827
diff changeset
   511
            if (msym.kind == ERR) {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
   512
                log.error(Errors.ModuleNotFound(msym));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   513
                //make sure the module is initialized:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   514
                msym.directives = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   515
                msym.exports = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   516
                msym.provides = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   517
                msym.requires = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   518
                msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   519
            } 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
   520
                setupAutomaticModule(msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   521
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   522
                msym.module_info.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   523
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   524
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   525
            // If module-info comes from a .java file, the underlying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   526
            // call of classFinder.fillIn will have called through the
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   527
            // source completer, to Enter, and then to Modules.enter,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   528
            // which will call completeModule.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   529
            // But, if module-info comes from a .class file, the underlying
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   530
            // call of classFinder.fillIn will just call ClassReader to read
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   531
            // the .class file, and so we call completeModule here.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   532
            if (msym.module_info.classfile == null || msym.module_info.classfile.getKind() == Kind.CLASS) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   533
                completeModule(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   534
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   535
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   536
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   537
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   538
        public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   539
            return "mainCompleter";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   540
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   541
    };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   542
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   543
    private void setupAutomaticModule(ModuleSymbol msym) throws CompletionFailure {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   544
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   545
            ListBuffer<Directive> directives = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   546
            ListBuffer<ExportsDirective> exports = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   547
            Set<String> seenPackages = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   548
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   549
            for (JavaFileObject clazz : fileManager.list(msym.classLocation, "", EnumSet.of(Kind.CLASS), true)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   550
                String binName = fileManager.inferBinaryName(msym.classLocation, clazz);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   551
                String pack = binName.lastIndexOf('.') != (-1) ? binName.substring(0, binName.lastIndexOf('.')) : ""; //unnamed package????
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   552
                if (seenPackages.add(pack)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   553
                    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
   554
                    //TODO: opens?
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   555
                    directives.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   556
                    exports.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   557
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   558
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   559
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   560
            msym.exports = exports.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   561
            msym.provides = List.nil();
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   562
            msym.requires = List.nil();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   563
            msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   564
            msym.directives = directives.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   565
            msym.flags_field |= Flags.ACYCLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   566
        } catch (IOException ex) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   567
            throw new IllegalStateException(ex);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   568
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   569
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   570
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   571
    private void completeAutomaticModule(ModuleSymbol msym) throws CompletionFailure {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   572
        ListBuffer<Directive> directives = new ListBuffer<>();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   573
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   574
        directives.addAll(msym.directives);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   575
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   576
        ListBuffer<RequiresDirective> requires = new ListBuffer<>();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   577
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   578
        for (ModuleSymbol ms : allModules()) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   579
            if (ms == syms.unnamedModule || ms == msym)
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   580
                continue;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   581
            Set<RequiresFlag> flags = (ms.flags_field & Flags.AUTOMATIC_MODULE) != 0 ?
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   582
                    EnumSet.of(RequiresFlag.TRANSITIVE) : EnumSet.noneOf(RequiresFlag.class);
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   583
            RequiresDirective d = new RequiresDirective(ms, flags);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   584
            directives.add(d);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   585
            requires.add(d);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   586
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   587
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   588
        RequiresDirective requiresUnnamed = new RequiresDirective(syms.unnamedModule);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   589
        directives.add(requiresUnnamed);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   590
        requires.add(requiresUnnamed);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   591
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   592
        msym.requires = requires.toList();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   593
        msym.directives = directives.toList();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   594
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   595
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   596
    private Completer getSourceCompleter(JCCompilationUnit tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   597
        return new Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   598
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   599
            public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   600
                ModuleSymbol msym = (ModuleSymbol) sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   601
                msym.flags_field |= UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   602
                ModuleVisitor v = new ModuleVisitor();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   603
                JavaFileObject prev = log.useSource(tree.sourcefile);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   604
                JCModuleDecl moduleDecl = tree.getModuleDecl();
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   605
                DiagnosticPosition prevLintPos = deferredLintHandler.setPos(moduleDecl.pos());
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   606
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   607
                try {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   608
                    moduleDecl.accept(v);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   609
                    completeModule(msym);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   610
                    checkCyclicDependencies(moduleDecl);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   611
                } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   612
                    log.useSource(prev);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   613
                    deferredLintHandler.setPos(prevLintPos);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   614
                    msym.flags_field &= ~UNATTRIBUTED;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   615
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   616
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   617
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   618
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   619
            public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   620
                return "SourceCompleter: " + tree.sourcefile.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   621
            }
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
42815
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   626
    public boolean isRootModule(ModuleSymbol module) {
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   627
        Assert.checkNonNull(rootModules);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   628
        return rootModules.contains(module);
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   629
    }
050370edaade 8072988: Update javax.annotation.processing for modules
jlahoda
parents: 42408
diff changeset
   630
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   631
    class ModuleVisitor extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   632
        private ModuleSymbol sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   633
        private final Set<ModuleSymbol> allRequires = new HashSet<>();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   634
        private final Map<PackageSymbol,List<ExportsDirective>> allExports = new HashMap<>();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   635
        private final Map<PackageSymbol,List<OpensDirective>> allOpens = new HashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   636
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   637
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   638
        public void visitModuleDef(JCModuleDecl tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   639
            sym = Assert.checkNonNull(tree.sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   640
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   641
            if (tree.getModuleType() == ModuleKind.OPEN) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   642
                sym.flags.add(ModuleFlags.OPEN);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   643
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   644
            sym.flags_field |= (tree.mods.flags & Flags.DEPRECATED);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   645
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   646
            sym.requires = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   647
            sym.exports = List.nil();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   648
            sym.opens = List.nil();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   649
            tree.directives.forEach(t -> t.accept(this));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   650
            sym.requires = sym.requires.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   651
            sym.exports = sym.exports.reverse();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   652
            sym.opens = sym.opens.reverse();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   653
            ensureJavaBase();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   654
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   655
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   656
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   657
        public void visitRequires(JCRequires tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   658
            ModuleSymbol msym = lookupModule(tree.moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   659
            if (msym.kind != MDL) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   660
                log.error(tree.moduleName.pos(), Errors.ModuleNotFound(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   661
            } else if (allRequires.contains(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   662
                log.error(tree.moduleName.pos(), Errors.DuplicateRequires(msym));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   663
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   664
                allRequires.add(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   665
                Set<RequiresFlag> flags = EnumSet.noneOf(RequiresFlag.class);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   666
                if (tree.isTransitive)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   667
                    flags.add(RequiresFlag.TRANSITIVE);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   668
                if (tree.isStaticPhase)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   669
                    flags.add(RequiresFlag.STATIC_PHASE);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   670
                RequiresDirective d = new RequiresDirective(msym, flags);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   671
                tree.directive = d;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   672
                sym.requires = sym.requires.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   673
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   674
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   675
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   676
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   677
        public void visitExports(JCExports tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   678
            Name name = TreeInfo.fullName(tree.qualid);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   679
            PackageSymbol packge = syms.enterPackage(sym, name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   680
            attr.setPackageSymbols(tree.qualid, packge);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   681
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   682
            if (tree.hasTag(Tag.OPENS) && sym.flags.contains(ModuleFlags.OPEN)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   683
                log.error(tree.pos(), Errors.NoOpensUnlessStrong);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   684
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   685
            List<ExportsDirective> exportsForPackage = allExports.computeIfAbsent(packge, p -> List.nil());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   686
            for (ExportsDirective d : exportsForPackage) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   687
                reportExportsConflict(tree, packge);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   688
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   689
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   690
            List<ModuleSymbol> toModules = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   691
            if (tree.moduleNames != null) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   692
                Set<ModuleSymbol> to = new LinkedHashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   693
                for (JCExpression n: tree.moduleNames) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   694
                    ModuleSymbol msym = lookupModule(n);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   695
                    chk.checkModuleExists(n.pos(), msym);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   696
                    for (ExportsDirective d : exportsForPackage) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   697
                        checkDuplicateExportsToModule(n, msym, d);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   698
                    }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   699
                    if (!to.add(msym)) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   700
                        reportExportsConflictToModule(n, msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   701
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   702
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   703
                toModules = List.from(to);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   704
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   705
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   706
            if (toModules == null || !toModules.isEmpty()) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   707
                Set<ExportsFlag> flags = EnumSet.noneOf(ExportsFlag.class);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   708
                ExportsDirective d = new ExportsDirective(packge, toModules, flags);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   709
                sym.exports = sym.exports.prepend(d);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   710
                tree.directive = d;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   711
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   712
                allExports.put(packge, exportsForPackage.prepend(d));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   713
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   714
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   715
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   716
        private void reportExportsConflict(JCExports tree, PackageSymbol packge) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   717
            log.error(tree.qualid.pos(), Errors.ConflictingExports(packge));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   718
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   719
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   720
        private void checkDuplicateExportsToModule(JCExpression name, ModuleSymbol msym,
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   721
                ExportsDirective d) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   722
            if (d.modules != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   723
                for (ModuleSymbol other : d.modules) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   724
                    if (msym == other) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   725
                        reportExportsConflictToModule(name, msym);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   726
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   727
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   728
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   729
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   730
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   731
        private void reportExportsConflictToModule(JCExpression name, ModuleSymbol msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   732
            log.error(name.pos(), Errors.ConflictingExportsToModule(msym));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   733
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   734
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   735
        @Override
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   736
        public void visitOpens(JCOpens tree) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   737
            Name name = TreeInfo.fullName(tree.qualid);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   738
            PackageSymbol packge = syms.enterPackage(sym, name);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   739
            attr.setPackageSymbols(tree.qualid, packge);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   740
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   741
            if (sym.flags.contains(ModuleFlags.OPEN)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   742
                log.error(tree.pos(), Errors.NoOpensUnlessStrong);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   743
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   744
            List<OpensDirective> opensForPackage = allOpens.computeIfAbsent(packge, p -> List.nil());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   745
            for (OpensDirective d : opensForPackage) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   746
                reportOpensConflict(tree, packge);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   747
            }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   748
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   749
            List<ModuleSymbol> toModules = null;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   750
            if (tree.moduleNames != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   751
                Set<ModuleSymbol> to = new LinkedHashSet<>();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   752
                for (JCExpression n: tree.moduleNames) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   753
                    ModuleSymbol msym = lookupModule(n);
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   754
                    chk.checkModuleExists(n.pos(), msym);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   755
                    for (OpensDirective d : opensForPackage) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   756
                        checkDuplicateOpensToModule(n, msym, d);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   757
                    }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   758
                    if (!to.add(msym)) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
   759
                        reportOpensConflictToModule(n, msym);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   760
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   761
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   762
                toModules = List.from(to);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   763
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   764
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   765
            if (toModules == null || !toModules.isEmpty()) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   766
                Set<OpensFlag> flags = EnumSet.noneOf(OpensFlag.class);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   767
                OpensDirective d = new OpensDirective(packge, toModules, flags);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   768
                sym.opens = sym.opens.prepend(d);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   769
                tree.directive = d;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   770
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   771
                allOpens.put(packge, opensForPackage.prepend(d));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   772
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   773
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   774
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   775
        private void reportOpensConflict(JCOpens tree, PackageSymbol packge) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   776
            log.error(tree.qualid.pos(), Errors.ConflictingOpens(packge));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   777
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   778
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   779
        private void checkDuplicateOpensToModule(JCExpression name, ModuleSymbol msym,
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   780
                OpensDirective d) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   781
            if (d.modules != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   782
                for (ModuleSymbol other : d.modules) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   783
                    if (msym == other) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   784
                        reportOpensConflictToModule(name, msym);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   785
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   786
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   787
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   788
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   789
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   790
        private void reportOpensConflictToModule(JCExpression name, ModuleSymbol msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   791
            log.error(name.pos(), Errors.ConflictingOpensToModule(msym));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   792
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   793
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   794
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   795
        public void visitProvides(JCProvides tree) { }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   796
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   797
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   798
        public void visitUses(JCUses tree) { }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   799
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   800
        private void ensureJavaBase() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   801
            if (sym.name == names.java_base)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   802
                return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   803
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   804
            for (RequiresDirective d: sym.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   805
                if (d.module.name == names.java_base)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   806
                    return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   807
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   808
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   809
            ModuleSymbol java_base = syms.enterModule(names.java_base);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   810
            Directive.RequiresDirective d =
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   811
                    new Directive.RequiresDirective(java_base,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   812
                            EnumSet.of(Directive.RequiresFlag.MANDATED));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   813
            sym.requires = sym.requires.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   814
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   815
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   816
        private ModuleSymbol lookupModule(JCExpression moduleName) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   817
            Name name = TreeInfo.fullName(moduleName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   818
            ModuleSymbol msym = moduleFinder.findModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   819
            TreeInfo.setSymbol(moduleName, msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   820
            return msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   821
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   822
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   823
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   824
    public Completer getUsesProvidesCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   825
        return sym -> {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   826
            ModuleSymbol msym = (ModuleSymbol) sym;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   827
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   828
            msym.complete();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
   829
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   830
            Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   831
            UsesProvidesVisitor v = new UsesProvidesVisitor(msym, env);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   832
            JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   833
            JCModuleDecl decl = env.toplevel.getModuleDecl();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   834
            DiagnosticPosition prevLintPos = deferredLintHandler.setPos(decl.pos());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   835
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   836
            try {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   837
                decl.accept(v);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   838
            } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   839
                log.useSource(prev);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   840
                deferredLintHandler.setPos(prevLintPos);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   841
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   842
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   843
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   844
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   845
    class UsesProvidesVisitor extends JCTree.Visitor {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   846
        private final ModuleSymbol msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   847
        private final Env<AttrContext> env;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   848
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   849
        private final Set<ClassSymbol> allUses = new HashSet<>();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   850
        private final Map<ClassSymbol, Set<ClassSymbol>> allProvides = new HashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   851
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   852
        public UsesProvidesVisitor(ModuleSymbol msym, Env<AttrContext> env) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   853
            this.msym = msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   854
            this.env = env;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   855
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   856
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   857
        @Override @SuppressWarnings("unchecked")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   858
        public void visitModuleDef(JCModuleDecl tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   859
            msym.directives = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   860
            msym.provides = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   861
            msym.uses = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   862
            tree.directives.forEach(t -> t.accept(this));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   863
            msym.directives = msym.directives.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   864
            msym.provides = msym.provides.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   865
            msym.uses = msym.uses.reverse();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   866
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   867
            if (msym.requires.nonEmpty() && msym.requires.head.flags.contains(RequiresFlag.MANDATED))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   868
                msym.directives = msym.directives.prepend(msym.requires.head);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   869
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   870
            msym.directives = msym.directives.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   871
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   872
            checkForCorrectness();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   873
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   874
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   875
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   876
        public void visitExports(JCExports tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   877
            if (tree.directive.packge.members().isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   878
                log.error(tree.qualid.pos(), Errors.PackageEmptyOrNotFound(tree.directive.packge));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   879
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   880
            msym.directives = msym.directives.prepend(tree.directive);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   881
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   882
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   883
        @Override
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   884
        public void visitOpens(JCOpens tree) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   885
            if (tree.directive.packge.members().isEmpty() &&
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   886
                ((tree.directive.packge.flags() & Flags.HAS_RESOURCE) == 0)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   887
                log.error(tree.qualid.pos(), Errors.PackageEmptyOrNotFound(tree.directive.packge));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   888
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   889
            msym.directives = msym.directives.prepend(tree.directive);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   890
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   891
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   892
        MethodSymbol noArgsConstructor(ClassSymbol tsym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   893
            for (Symbol sym : tsym.members().getSymbolsByName(names.init)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   894
                MethodSymbol mSym = (MethodSymbol)sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   895
                if (mSym.params().isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   896
                    return mSym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   897
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   898
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   899
            return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   900
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   901
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   902
        MethodSymbol factoryMethod(ClassSymbol tsym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   903
            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
   904
                MethodSymbol mSym = (MethodSymbol)sym;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   905
                if (mSym.isStatic() && (mSym.flags() & Flags.PUBLIC) != 0 && mSym.params().isEmpty()) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   906
                    return mSym;
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
            return null;
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
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   912
        Map<Directive.ProvidesDirective, JCProvides> directiveToTreeMap = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   913
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   914
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   915
        public void visitProvides(JCProvides tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   916
            Type st = attr.attribType(tree.serviceName, env, syms.objectType);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   917
            ClassSymbol service = (ClassSymbol) st.tsym;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   918
            ListBuffer<ClassSymbol> impls = new ListBuffer<>();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   919
            for (JCExpression implName : tree.implNames) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   920
                Type it = attr.attribType(implName, env, syms.objectType);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   921
                ClassSymbol impl = (ClassSymbol) it.tsym;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   922
                //find provider factory:
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   923
                MethodSymbol factory = factoryMethod(impl);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   924
                if (factory != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   925
                    Type returnType = factory.type.getReturnType();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   926
                    if (!types.isSubtype(returnType, st)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   927
                        log.error(implName.pos(), Errors.ServiceImplementationProviderReturnMustBeSubtypeOfServiceInterface);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   928
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   929
                } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   930
                    if (!types.isSubtype(it, st)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   931
                        log.error(implName.pos(), Errors.ServiceImplementationMustBeSubtypeOfServiceInterface);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   932
                    } else if ((impl.flags() & ABSTRACT) != 0) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   933
                        log.error(implName.pos(), Errors.ServiceImplementationIsAbstract(impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   934
                    } else if (impl.isInner()) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   935
                        log.error(implName.pos(), Errors.ServiceImplementationIsInner(impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   936
                    } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   937
                        MethodSymbol constr = noArgsConstructor(impl);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   938
                        if (constr == null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   939
                            log.error(implName.pos(), Errors.ServiceImplementationDoesntHaveANoArgsConstructor(impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   940
                        } else if ((constr.flags() & PUBLIC) == 0) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   941
                            log.error(implName.pos(), Errors.ServiceImplementationNoArgsConstructorNotPublic(impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   942
                        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   943
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   944
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   945
                if (it.hasTag(CLASS)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   946
                    // For now, we just check the pair (service-type, impl-type) is unique
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   947
                    // TODO, check only one provides per service type as well
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   948
                    if (allProvides.computeIfAbsent(service, s -> new HashSet<>()).add(impl)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   949
                        impls.append(impl);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   950
                    } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   951
                        log.error(implName.pos(), Errors.DuplicateProvides(service, impl));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   952
                    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   953
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   954
            }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   955
            if (st.hasTag(CLASS) && !impls.isEmpty()) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   956
                Directive.ProvidesDirective d = new Directive.ProvidesDirective(service, impls.toList());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   957
                msym.provides = msym.provides.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   958
                msym.directives = msym.directives.prepend(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   959
                directiveToTreeMap.put(d, tree);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   960
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   961
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   962
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   963
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   964
        public void visitRequires(JCRequires tree) {
39915
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
   965
            if (tree.directive != null) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   966
                chk.checkDeprecated(tree.moduleName.pos(), msym, tree.directive.module);
39915
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
   967
                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
   968
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   969
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   970
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   971
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   972
        public void visitUses(JCUses tree) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   973
            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
   974
            Symbol sym = TreeInfo.symbol(tree.qualid);
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   975
            if ((sym.flags() & ENUM) != 0) {
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   976
                log.error(tree.qualid.pos(), Errors.ServiceDefinitionIsEnum(st.tsym));
a76a06106d02 8153268: javac accepts enums being referenced by 'uses' statement
vromero
parents: 37848
diff changeset
   977
            } else if (st.hasTag(CLASS)) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   978
                ClassSymbol service = (ClassSymbol) st.tsym;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   979
                if (allUses.add(service)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   980
                    Directive.UsesDirective d = new Directive.UsesDirective(service);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   981
                    msym.uses = msym.uses.prepend(d);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   982
                    msym.directives = msym.directives.prepend(d);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   983
                } else {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   984
                    log.error(tree.pos(), Errors.DuplicateUses(service));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   985
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   986
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   987
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   988
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   989
        private void checkForCorrectness() {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   990
            for (Directive.ProvidesDirective provides : msym.provides) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   991
                JCProvides tree = directiveToTreeMap.get(provides);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   992
                for (ClassSymbol impl : provides.impls) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   993
                    /* 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
   994
                     * (else, error)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   995
                     */
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   996
                    PackageSymbol implementationDefiningPackage = impl.packge();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   997
                    if (implementationDefiningPackage.modle != msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
   998
                        // 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
   999
                        // TODO: should improve error message to identify the implementation type
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1000
                        log.error(tree.pos(), Errors.ServiceImplementationNotInRightModule(implementationDefiningPackage.modle));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1001
                    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1002
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1003
                    /* 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
  1004
                     * 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
  1005
                     * exported and there is no uses for the service.
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1006
                     */
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1007
                    PackageSymbol interfaceDeclaringPackage = provides.service.packge();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1008
                    boolean isInterfaceDeclaredInCurrentModule = interfaceDeclaringPackage.modle == msym;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1009
                    boolean isInterfaceExportedFromAReadableModule =
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1010
                            msym.visiblePackages.get(interfaceDeclaringPackage.fullname) == interfaceDeclaringPackage;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1011
                    if (isInterfaceDeclaredInCurrentModule && !isInterfaceExportedFromAReadableModule) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1012
                        // 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
  1013
                        boolean warn = true;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1014
                        for (ExportsDirective export : msym.exports) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1015
                            if (interfaceDeclaringPackage == export.packge) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1016
                                warn = false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1017
                                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1018
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1019
                        }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1020
                        if (warn) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1021
                            for (UsesDirective uses : msym.uses) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1022
                                if (provides.service == uses.service) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1023
                                    warn = false;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1024
                                    break;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1025
                                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1026
                            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1027
                        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1028
                        if (warn) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1029
                            log.warning(tree.pos(), Warnings.ServiceProvidedButNotExportedOrUsed(provides.service));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1030
                        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1031
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1032
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1033
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1034
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1035
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1036
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1037
    private Set<ModuleSymbol> allModules;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1038
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1039
    public Set<ModuleSymbol> allModules() {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1040
        Assert.checkNonNull(allModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1041
        return allModules;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1042
    }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1043
40311
bb76098875c8 8133884: javac moduleName/className and moduleName/packageName options
jlahoda
parents: 40308
diff changeset
  1044
    private void setupAllModules() {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1045
        Assert.checkNonNull(rootModules);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1046
        Assert.checkNull(allModules);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1047
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1048
        Set<ModuleSymbol> observable;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1049
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1050
        if (limitModsOpt == null && extraLimitMods.isEmpty()) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1051
            observable = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1052
        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1053
            Set<ModuleSymbol> limitMods = new HashSet<>();
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1054
            if (limitModsOpt != null) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1055
                for (String limit : limitModsOpt.split(",")) {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1056
                    if (!isValidName(limit))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1057
                        continue;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1058
                    limitMods.add(syms.enterModule(names.fromString(limit)));
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1059
                }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1060
            }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1061
            for (String limit : extraLimitMods) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1062
                limitMods.add(syms.enterModule(names.fromString(limit)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1063
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1064
            observable = computeTransitiveClosure(limitMods, null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1065
            observable.addAll(rootModules);
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1066
            if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1067
                for (ModuleSymbol msym : limitMods) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1068
                    if (!observable.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1069
                        log.warning(LintCategory.OPTIONS,
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1070
                                Warnings.ModuleForOptionNotFound(Option.LIMIT_MODULES, msym));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1071
                    }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1072
                }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1073
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1074
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1075
42841
1768507dae6b 8171412: tools/javac/modules/AddLimitMods.java failed with "error: module not found"
jjg
parents: 42827
diff changeset
  1076
        Predicate<ModuleSymbol> observablePred = sym ->
1768507dae6b 8171412: tools/javac/modules/AddLimitMods.java failed with "error: module not found"
jjg
parents: 42827
diff changeset
  1077
             (observable == null) ? (moduleFinder.findModule(sym).kind != ERR) : observable.contains(sym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1078
        Predicate<ModuleSymbol> systemModulePred = sym -> (sym.flags() & Flags.SYSTEM_MODULE) != 0;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1079
        Set<ModuleSymbol> enabledRoot = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1080
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1081
        if (rootModules.contains(syms.unnamedModule)) {
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1082
            ModuleSymbol javaSE = syms.getModule(java_se);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1083
            Predicate<ModuleSymbol> jdkModulePred;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1084
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1085
            if (javaSE != null && (observable == null || observable.contains(javaSE))) {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1086
                jdkModulePred = sym -> {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1087
                    sym.complete();
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1088
                    return   !sym.name.startsWith(java_)
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1089
                           && sym.exports.stream().anyMatch(e -> e.modules == null);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1090
                };
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1091
                enabledRoot.add(javaSE);
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1092
            } else {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1093
                jdkModulePred = sym -> true;
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1094
            }
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1095
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1096
            for (ModuleSymbol sym : new HashSet<>(syms.getAllModules())) {
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36526
diff changeset
  1097
                if (systemModulePred.test(sym) && observablePred.test(sym) && jdkModulePred.test(sym)) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1098
                    enabledRoot.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1099
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1100
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1101
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1102
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1103
        enabledRoot.addAll(rootModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1104
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1105
        if (addModsOpt != null || !extraAddMods.isEmpty()) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1106
            Set<String> fullAddMods = new HashSet<>();
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1107
            fullAddMods.addAll(extraAddMods);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1108
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1109
            if (addModsOpt != null) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1110
                fullAddMods.addAll(Arrays.asList(addModsOpt.split(",")));
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1111
            }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1112
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1113
            for (String added : fullAddMods) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1114
                Stream<ModuleSymbol> modules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1115
                switch (added) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1116
                    case ALL_SYSTEM:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1117
                        modules = syms.getAllModules()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1118
                                      .stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1119
                                      .filter(systemModulePred.and(observablePred));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1120
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1121
                    case ALL_MODULE_PATH:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1122
                        modules = syms.getAllModules()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1123
                                      .stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1124
                                      .filter(systemModulePred.negate().and(observablePred));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1125
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1126
                    default:
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1127
                        if (!isValidName(added))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1128
                            continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1129
                        modules = Stream.of(syms.enterModule(names.fromString(added)));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1130
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1131
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1132
                modules.forEach(sym -> {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1133
                    enabledRoot.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1134
                    if (observable != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1135
                        observable.add(sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1136
                });
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1137
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1138
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1139
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1140
        Set<ModuleSymbol> result = computeTransitiveClosure(enabledRoot, observable);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1141
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1142
        result.add(syms.unnamedModule);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1143
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1144
        allModules = result;
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1145
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1146
        //add module versions from options, if any:
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1147
        if (moduleVersionOpt != null) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1148
            Name version = names.fromString(moduleVersionOpt);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1149
            rootModules.forEach(m -> m.version = version);
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42408
diff changeset
  1150
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1151
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1152
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1153
    public boolean isInModuleGraph(ModuleSymbol msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1154
        return allModules == null || allModules.contains(msym);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1155
    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1156
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1157
    private Set<ModuleSymbol> computeTransitiveClosure(Iterable<? extends ModuleSymbol> base, Set<ModuleSymbol> observable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1158
        List<ModuleSymbol> todo = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1159
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1160
        for (ModuleSymbol ms : base) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1161
            todo = todo.prepend(ms);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1162
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1163
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1164
        Set<ModuleSymbol> result = new LinkedHashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1165
        result.add(syms.java_base);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1166
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1167
        while (todo.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1168
            ModuleSymbol current = todo.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1169
            todo = todo.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1170
            if (observable != null && !observable.contains(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1171
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1172
            if (!result.add(current) || current == syms.unnamedModule || ((current.flags_field & Flags.AUTOMATIC_MODULE) != 0))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1173
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1174
            current.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1175
            for (RequiresDirective rd : current.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1176
                todo = todo.prepend(rd.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1177
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1178
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1179
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1180
        return result;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1181
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1182
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1183
    public ModuleSymbol getObservableModule(Name name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1184
        ModuleSymbol mod = syms.getModule(name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1185
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1186
        if (allModules().contains(mod)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1187
            return mod;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1188
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1189
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1190
        return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1191
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1192
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1193
    private Completer getUnnamedModuleCompleter() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1194
        moduleFinder.findAllModules();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1195
        return new Symbol.Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1196
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1197
            public void complete(Symbol sym) throws CompletionFailure {
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1198
                if (inInitModules) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1199
                    sym.completer = this;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1200
                    return ;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 40768
diff changeset
  1201
                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1202
                ModuleSymbol msym = (ModuleSymbol) sym;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1203
                Set<ModuleSymbol> allModules = new HashSet<>(allModules());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1204
                allModules.remove(syms.unnamedModule);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1205
                for (ModuleSymbol m : allModules) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1206
                    m.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1207
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1208
                initVisiblePackages(msym, allModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1209
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1210
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1211
            @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1212
            public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1213
                return "unnamedModule Completer";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1214
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1215
        };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1216
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1217
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1218
    private final Map<ModuleSymbol, Set<ModuleSymbol>> requiresTransitiveCache = new HashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1219
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1220
    private void completeModule(ModuleSymbol msym) {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1221
        if (inInitModules) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1222
            msym.completer = sym -> completeModule(msym);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1223
            return ;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1224
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1225
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1226
        if ((msym.flags_field & Flags.AUTOMATIC_MODULE) != 0) {
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1227
            completeAutomaticModule(msym);
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1228
        }
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1229
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1230
        Assert.checkNonNull(msym.requires);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1231
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1232
        initAddReads();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1233
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1234
        msym.requires = msym.requires.appendList(List.from(addReads.getOrDefault(msym, Collections.emptySet())));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1235
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1236
        List<RequiresDirective> requires = msym.requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1237
        List<RequiresDirective> previous = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1238
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1239
        while (requires.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1240
            if (!allModules().contains(requires.head.module)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1241
                Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1242
                if (env != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1243
                    JavaFileObject origSource = log.useSource(env.toplevel.sourcefile);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1244
                    try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1245
                        log.error(/*XXX*/env.tree, Errors.ModuleNotFound(requires.head.module));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1246
                    } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1247
                        log.useSource(origSource);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1248
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1249
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1250
                    Assert.check((msym.flags() & Flags.AUTOMATIC_MODULE) == 0);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1251
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1252
                if (previous != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1253
                    previous.tail = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1254
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1255
                    msym.requires.tail = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1256
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1257
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1258
                previous = requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1259
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1260
            requires = requires.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1261
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1262
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1263
        Set<ModuleSymbol> readable = new LinkedHashSet<>();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1264
        Set<ModuleSymbol> requiresTransitive = new HashSet<>();
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1265
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1266
        for (RequiresDirective d : msym.requires) {
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1267
            d.module.complete();
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1268
            readable.add(d.module);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1269
            Set<ModuleSymbol> s = retrieveRequiresTransitive(d.module);
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1270
            Assert.checkNonNull(s, () -> "no entry in cache for " + d.module);
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1271
            readable.addAll(s);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1272
            if (d.flags.contains(RequiresFlag.TRANSITIVE)) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1273
                requiresTransitive.add(d.module);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1274
                requiresTransitive.addAll(s);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1275
            }
39366
8bf5fe72ca88 8155026: javac grants implied readability to explicit modules
jlahoda
parents: 39361
diff changeset
  1276
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1277
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1278
        requiresTransitiveCache.put(msym, requiresTransitive);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1279
        initVisiblePackages(msym, readable);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1280
        for (ExportsDirective d: msym.exports) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1281
            if (d.packge != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1282
                d.packge.modle = msym;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1283
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1284
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1285
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1286
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1287
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1288
    private Set<ModuleSymbol> retrieveRequiresTransitive(ModuleSymbol msym) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1289
        Set<ModuleSymbol> requiresTransitive = requiresTransitiveCache.get(msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1290
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1291
        if (requiresTransitive == null) {
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40604
diff changeset
  1292
            //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
  1293
            requiresTransitive = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1294
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1295
            Set<ModuleSymbol> seen = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1296
            List<ModuleSymbol> todo = List.of(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1297
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1298
            while (todo.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1299
                ModuleSymbol current = todo.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1300
                todo = todo.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1301
                if (!seen.add(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1302
                    continue;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1303
                requiresTransitive.add(current);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1304
                current.complete();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1305
                Iterable<? extends RequiresDirective> requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1306
                if (current != syms.unnamedModule) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1307
                    Assert.checkNonNull(current.requires, () -> current + ".requires == null; " + msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1308
                    requires = current.requires;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1309
                    for (RequiresDirective rd : requires) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1310
                        if (rd.isTransitive())
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1311
                            todo = todo.prepend(rd.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1312
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1313
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1314
                    for (ModuleSymbol mod : allModules()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1315
                        todo = todo.prepend(mod);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1316
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1317
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1318
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1319
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1320
            requiresTransitive.remove(msym);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1321
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1322
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41999
diff changeset
  1323
        return requiresTransitive;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1324
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1325
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1326
    private void initVisiblePackages(ModuleSymbol msym, Collection<ModuleSymbol> readable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1327
        initAddExports();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1328
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1329
        msym.visiblePackages = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1330
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1331
        Map<Name, ModuleSymbol> seen = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1332
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1333
        for (ModuleSymbol rm : readable) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1334
            if (rm == syms.unnamedModule)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1335
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1336
            addVisiblePackages(msym, seen, rm, rm.exports);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1337
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1338
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1339
        addExports.forEach((exportsFrom, exports) -> {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1340
            addVisiblePackages(msym, seen, exportsFrom, exports);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1341
        });
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1342
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1343
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1344
    private void addVisiblePackages(ModuleSymbol msym,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1345
                                    Map<Name, ModuleSymbol> seenPackages,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1346
                                    ModuleSymbol exportsFrom,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1347
                                    Collection<ExportsDirective> exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1348
        for (ExportsDirective d : exports) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1349
            if (d.modules == null || d.modules.contains(msym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1350
                Name packageName = d.packge.fullname;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1351
                ModuleSymbol previousModule = seenPackages.get(packageName);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1352
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1353
                if (previousModule != null && previousModule != exportsFrom) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1354
                    Env<AttrContext> env = typeEnvs.get(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1355
                    JavaFileObject origSource = env != null ? log.useSource(env.toplevel.sourcefile)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1356
                                                            : null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1357
                    DiagnosticPosition pos = env != null ? env.tree.pos() : null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1358
                    try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1359
                        log.error(pos, Errors.PackageClashFromRequires(msym, packageName,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1360
                                                                      previousModule, exportsFrom));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1361
                    } finally {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1362
                        if (env != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1363
                            log.useSource(origSource);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1364
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1365
                    continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1366
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1367
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1368
                seenPackages.put(packageName, exportsFrom);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1369
                msym.visiblePackages.put(d.packge.fullname, d.packge);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1370
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1371
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1372
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1373
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1374
    private void initAddExports() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1375
        if (addExports != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1376
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1377
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1378
        addExports = new LinkedHashMap<>();
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1379
        Set<ModuleSymbol> unknownModules = new HashSet<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1380
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1381
        if (addExportsOpt == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1382
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1383
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1384
        Pattern ep = Pattern.compile("([^/]+)/([^=]+)=(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1385
        for (String s: addExportsOpt.split("\0+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1386
            if (s.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1387
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1388
            Matcher em = ep.matcher(s);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1389
            if (!em.matches()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1390
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1391
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1392
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1393
            // Terminology comes from
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40604
diff changeset
  1394
            //  --add-exports module/package=target,...
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1395
            // Compare to
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1396
            //  module module { exports package to target, ... }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1397
            String moduleName = em.group(1);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1398
            String packageName = em.group(2);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1399
            String targetNames = em.group(3);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1400
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1401
            if (!isValidName(moduleName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1402
                continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1403
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1404
            ModuleSymbol msym = syms.enterModule(names.fromString(moduleName));
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1405
            if (!isKnownModule(msym, unknownModules))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1406
                continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1407
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1408
            if (!isValidName(packageName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1409
                continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1410
            PackageSymbol p = syms.enterPackage(msym, names.fromString(packageName));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1411
            p.modle = msym;  // TODO: do we need this?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1412
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1413
            List<ModuleSymbol> targetModules = List.nil();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1414
            for (String toModule : targetNames.split("[ ,]+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1415
                ModuleSymbol m;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1416
                if (toModule.equals("ALL-UNNAMED")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1417
                    m = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1418
                } else {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1419
                    if (!isValidName(toModule))
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1420
                        continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1421
                    m = syms.enterModule(names.fromString(toModule));
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1422
                    if (!isKnownModule(m, unknownModules))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1423
                        continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1424
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1425
                targetModules = targetModules.prepend(m);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1426
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1427
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1428
            Set<ExportsDirective> extra = addExports.computeIfAbsent(msym, _x -> new LinkedHashSet<>());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1429
            ExportsDirective d = new ExportsDirective(p, targetModules);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1430
            extra.add(d);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1431
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1432
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1433
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1434
    private boolean isKnownModule(ModuleSymbol msym, Set<ModuleSymbol> unknownModules) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1435
        if (allModules.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1436
            return true;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1437
        }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1438
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1439
        if (!unknownModules.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1440
            if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1441
                log.warning(LintCategory.OPTIONS,
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1442
                        Warnings.ModuleForOptionNotFound(Option.ADD_EXPORTS, msym));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1443
            }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1444
            unknownModules.add(msym);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1445
        }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1446
        return false;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1447
    }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1448
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1449
    private void initAddReads() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1450
        if (addReads != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1451
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1452
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1453
        addReads = new LinkedHashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1454
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1455
        if (addReadsOpt == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1456
            return;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1457
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1458
        Pattern rp = Pattern.compile("([^=]+)=(.*)");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1459
        for (String s : addReadsOpt.split("\0+")) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1460
            if (s.isEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1461
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1462
            Matcher rm = rp.matcher(s);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1463
            if (!rm.matches()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1464
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1465
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1466
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1467
            // Terminology comes from
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1468
            //  --add-reads source-module=target-module,...
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1469
            // Compare to
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1470
            //  module source-module { requires target-module; ... }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1471
            String sourceName = rm.group(1);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1472
            String targetNames = rm.group(2);
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1473
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1474
            if (!isValidName(sourceName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1475
                continue;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1476
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1477
            ModuleSymbol msym = syms.enterModule(names.fromString(sourceName));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1478
            if (!allModules.contains(msym)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1479
                if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1480
                    log.warning(Warnings.ModuleForOptionNotFound(Option.ADD_READS, msym));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1481
                }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1482
                continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1483
            }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1484
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1485
            for (String targetName : targetNames.split("[ ,]+", -1)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1486
                ModuleSymbol targetModule;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1487
                if (targetName.equals("ALL-UNNAMED")) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1488
                    targetModule = syms.unnamedModule;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1489
                } else {
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1490
                    if (!isValidName(targetName))
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1491
                        continue;
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1492
                    targetModule = syms.enterModule(names.fromString(targetName));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1493
                    if (!allModules.contains(targetModule)) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1494
                        if (lintOptions) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1495
                            log.warning(LintCategory.OPTIONS, Warnings.ModuleForOptionNotFound(Option.ADD_READS, targetModule));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1496
                        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1497
                        continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1498
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1499
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1500
                addReads.computeIfAbsent(msym, m -> new HashSet<>())
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1501
                        .add(new RequiresDirective(targetModule, EnumSet.of(RequiresFlag.EXTRA)));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1502
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1503
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1504
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1505
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1506
    private void checkCyclicDependencies(JCModuleDecl mod) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1507
        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
  1508
            JCRequires rd;
5d1e0740e709 8158224: NullPointerException in com.sun.tools.javac.comp.Modules.checkCyclicDependencies when module missing
sadayapalam
parents: 39366
diff changeset
  1509
            if (!d.hasTag(Tag.REQUIRES) || (rd = (JCRequires) d).directive == null)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1510
                continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1511
            Set<ModuleSymbol> nonSyntheticDeps = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1512
            List<ModuleSymbol> queue = List.of(rd.directive.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1513
            while (queue.nonEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1514
                ModuleSymbol current = queue.head;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1515
                queue = queue.tail;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1516
                if (!nonSyntheticDeps.add(current))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1517
                    continue;
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1518
                current.complete();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1519
                if ((current.flags() & Flags.ACYCLIC) != 0)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1520
                    continue;
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42824
diff changeset
  1521
                Assert.checkNonNull(current.requires, current::toString);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1522
                for (RequiresDirective dep : current.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1523
                    if (!dep.flags.contains(RequiresFlag.EXTRA))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1524
                        queue = queue.prepend(dep.module);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1525
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1526
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1527
            if (nonSyntheticDeps.contains(mod.sym)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1528
                log.error(rd.moduleName.pos(), Errors.CyclicRequires(rd.directive.module));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1529
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1530
            mod.sym.flags_field |= Flags.ACYCLIC;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1531
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1532
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1533
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1534
    private boolean isValidName(CharSequence name) {
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1535
        return SourceVersion.isName(name, Source.toSourceVersion(source));
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1536
    }
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 41932
diff changeset
  1537
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1538
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1539
    private String toString(ModuleSymbol msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1540
        return msym.name + "["
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1541
                + "kind:" + msym.kind + ";"
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1542
                + "locn:" + toString(msym.sourceLocation) + "," + toString(msym.classLocation) + ";"
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1543
                + "info:" + toString(msym.module_info.sourcefile) + ","
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1544
                            + toString(msym.module_info.classfile) + ","
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1545
                            + msym.module_info.completer
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1546
                + "]";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1547
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1548
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1549
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1550
    String toString(Location locn) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1551
        return (locn == null) ? "--" : locn.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1552
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1553
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1554
    // DEBUG
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1555
    String toString(JavaFileObject fo) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1556
        return (fo == null) ? "--" : fo.getName();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1557
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1558
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1559
    public void newRound() {
39917
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1560
        rootModules = null;
c51a8950f278 8154705: invalid use of ALL-MODULE-PATH causes crash
ksrini
parents: 39915
diff changeset
  1561
        allModules = null;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1562
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
  1563
}