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