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