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