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 package com.sun.tools.javac.comp;
27
28 import java.util.HashSet;
29 import java.util.Set;
30 import java.util.function.BiConsumer;
31
32 import javax.tools.JavaFileObject;
33
34 import com.sun.tools.javac.code.*;
35 import com.sun.tools.javac.code.Lint.LintCategory;
36 import com.sun.tools.javac.code.Scope.ImportFilter;
37 import com.sun.tools.javac.code.Scope.ImportScope;
38 import com.sun.tools.javac.code.Scope.NamedImportScope;
39 import com.sun.tools.javac.code.Scope.StarImportScope;
40 import com.sun.tools.javac.code.Scope.WriteableScope;
41 import com.sun.tools.javac.code.Source.Feature;
42 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
43 import com.sun.tools.javac.parser.Parser;
44 import com.sun.tools.javac.parser.ParserFactory;
45 import com.sun.tools.javac.tree.*;
46 import com.sun.tools.javac.util.*;
47 import com.sun.tools.javac.util.DefinedBy.Api;
48
49 import com.sun.tools.javac.code.Symbol.*;
50 import com.sun.tools.javac.code.Type.*;
51 import com.sun.tools.javac.resources.CompilerProperties.Errors;
52 import com.sun.tools.javac.tree.JCTree.*;
53
54 import static com.sun.tools.javac.code.Flags.*;
55 import static com.sun.tools.javac.code.Flags.ANNOTATION;
56 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
57 import static com.sun.tools.javac.code.Kinds.Kind.*;
58 import static com.sun.tools.javac.code.TypeTag.CLASS;
59 import static com.sun.tools.javac.code.TypeTag.ERROR;
60 import com.sun.tools.javac.resources.CompilerProperties.Fragments;
61
62 import static com.sun.tools.javac.code.TypeTag.*;
63 import static com.sun.tools.javac.tree.JCTree.Tag.*;
64
65 import com.sun.tools.javac.util.Dependencies.CompletionCause;
66 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
67 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
68
69 /** This is the second phase of Enter, in which classes are completed
70 * by resolving their headers and entering their members in the into
71 * the class scope. See Enter for an overall overview.
72 *
73 * This class uses internal phases to process the classes. When a phase
74 * processes classes, the lower phases are not invoked until all classes
75 * pass through the current phase. Note that it is possible that upper phases
76 * are run due to recursive completion. The internal phases are:
77 * - ImportPhase: shallow pass through imports, adds information about imports
78 * the NamedImportScope and StarImportScope, but avoids queries
79 * about class hierarchy.
80 * - HierarchyPhase: resolves the supertypes of the given class. Does not handle
553 }
554
555 protected Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) {
556 WriteableScope baseScope = WriteableScope.create(tree.sym);
557 //import already entered local classes into base scope
558 for (Symbol sym : env.outer.info.scope.getSymbols(NON_RECURSIVE)) {
559 if (sym.isDirectlyOrIndirectlyLocal()) {
560 baseScope.enter(sym);
561 }
562 }
563 //import current type-parameters into base scope
564 if (tree.typarams != null)
565 for (List<JCTypeParameter> typarams = tree.typarams;
566 typarams.nonEmpty();
567 typarams = typarams.tail)
568 baseScope.enter(typarams.head.type.tsym);
569 Env<AttrContext> outer = env.outer; // the base clause can't see members of this class
570 Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(baseScope));
571 localEnv.baseClause = true;
572 localEnv.outer = outer;
573 localEnv.info.isSelfCall = false;
574 return localEnv;
575 }
576
577 /** Generate a base clause for an enum type.
578 * @param pos The position for trees and diagnostics, if any
579 * @param c The class symbol of the enum
580 */
581 protected JCExpression enumBase(int pos, ClassSymbol c) {
582 JCExpression result = make.at(pos).
583 TypeApply(make.QualIdent(syms.enumSym),
584 List.of(make.Type(c.type)));
585 return result;
586 }
587
588 /** Generate a base clause for a record type.
589 * @param pos The position for trees and diagnostics, if any
590 * @param c The class symbol of the record
591 */
592 protected JCExpression recordBase(int pos, ClassSymbol c) {
593 JCExpression result = make.at(pos).
1123 (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
1124 addEnumMembers(tree, env);
1125 }
1126 boolean isRecord = (tree.sym.flags_field & RECORD) != 0;
1127 List<JCTree> alreadyEntered = null;
1128 if (isRecord) {
1129 alreadyEntered = List.convert(JCTree.class, TreeInfo.recordFields(tree));
1130 alreadyEntered = alreadyEntered.prependList(tree.defs.stream()
1131 .filter(t -> TreeInfo.isConstructor(t) && t != defaultConstructor).collect(List.collector()));
1132 }
1133 List<JCTree> defsToEnter = isRecord ?
1134 tree.defs.diff(alreadyEntered) : tree.defs;
1135 memberEnter.memberEnter(defsToEnter, env);
1136 if (isRecord) {
1137 addRecordMembersIfNeeded(tree, env);
1138 }
1139 if (tree.sym.isAnnotationType()) {
1140 Assert.check(tree.sym.isCompleted());
1141 tree.sym.setAnnotationTypeMetadata(new AnnotationTypeMetadata(tree.sym, annotate.annotationTypeSourceCompleter()));
1142 }
1143 }
1144
1145 private void addAccessor(JCVariableDecl tree, Env<AttrContext> env) {
1146 MethodSymbol implSym = lookupMethod(env.enclClass.sym, tree.sym.name, List.nil());
1147 RecordComponent rec = ((ClassSymbol) tree.sym.owner).getRecordComponent(tree.sym);
1148 if (implSym == null || (implSym.flags_field & GENERATED_MEMBER) != 0) {
1149 /* here we are pushing the annotations present in the corresponding field down to the accessor
1150 * it could be that some of those annotations are not applicable to the accessor, they will be striped
1151 * away later at Check::validateAnnotation
1152 */
1153 TreeCopier<JCTree> tc = new TreeCopier<JCTree>(make.at(tree.pos));
1154 List<JCAnnotation> originalAnnos = rec.getOriginalAnnos().isEmpty() ?
1155 rec.getOriginalAnnos() :
1156 tc.copy(rec.getOriginalAnnos());
1157 JCVariableDecl recordField = TreeInfo.recordFields((JCClassDecl) env.tree).stream().filter(rf -> rf.name == tree.name).findAny().get();
1158 JCMethodDecl getter = make.at(tree.pos).
1159 MethodDef(
1160 make.Modifiers(PUBLIC | Flags.GENERATED_MEMBER, originalAnnos),
1161 tree.sym.name,
1162 /* we need to special case for the case when the user declared the type as an ident
|
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 package com.sun.tools.javac.comp;
27
28 import java.util.HashSet;
29 import java.util.Set;
30 import java.util.function.BiConsumer;
31
32 import javax.tools.JavaFileObject;
33
34 import com.sun.tools.javac.code.*;
35 import com.sun.tools.javac.code.Lint.LintCategory;
36 import com.sun.tools.javac.code.Scope.ImportFilter;
37 import com.sun.tools.javac.code.Scope.NamedImportScope;
38 import com.sun.tools.javac.code.Scope.StarImportScope;
39 import com.sun.tools.javac.code.Scope.WriteableScope;
40 import com.sun.tools.javac.code.Source.Feature;
41 import com.sun.tools.javac.comp.Annotate.AnnotationTypeMetadata;
42 import com.sun.tools.javac.parser.Parser;
43 import com.sun.tools.javac.parser.ParserFactory;
44 import com.sun.tools.javac.tree.*;
45 import com.sun.tools.javac.util.*;
46 import com.sun.tools.javac.util.DefinedBy.Api;
47
48 import com.sun.tools.javac.code.Symbol.*;
49 import com.sun.tools.javac.code.Type.*;
50 import com.sun.tools.javac.resources.CompilerProperties.Errors;
51 import com.sun.tools.javac.tree.JCTree.*;
52
53 import static com.sun.tools.javac.code.Flags.*;
54 import static com.sun.tools.javac.code.Flags.ANNOTATION;
55 import static com.sun.tools.javac.code.Flags.SYNCHRONIZED;
56 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
57 import static com.sun.tools.javac.code.Kinds.Kind.*;
58 import static com.sun.tools.javac.code.TypeTag.CLASS;
59 import static com.sun.tools.javac.code.TypeTag.ERROR;
60
61 import static com.sun.tools.javac.code.TypeTag.*;
62 import static com.sun.tools.javac.tree.JCTree.Tag.*;
63
64 import com.sun.tools.javac.util.Dependencies.CompletionCause;
65 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
66 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
67
68 /** This is the second phase of Enter, in which classes are completed
69 * by resolving their headers and entering their members in the into
70 * the class scope. See Enter for an overall overview.
71 *
72 * This class uses internal phases to process the classes. When a phase
73 * processes classes, the lower phases are not invoked until all classes
74 * pass through the current phase. Note that it is possible that upper phases
75 * are run due to recursive completion. The internal phases are:
76 * - ImportPhase: shallow pass through imports, adds information about imports
77 * the NamedImportScope and StarImportScope, but avoids queries
78 * about class hierarchy.
79 * - HierarchyPhase: resolves the supertypes of the given class. Does not handle
552 }
553
554 protected Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) {
555 WriteableScope baseScope = WriteableScope.create(tree.sym);
556 //import already entered local classes into base scope
557 for (Symbol sym : env.outer.info.scope.getSymbols(NON_RECURSIVE)) {
558 if (sym.isDirectlyOrIndirectlyLocal()) {
559 baseScope.enter(sym);
560 }
561 }
562 //import current type-parameters into base scope
563 if (tree.typarams != null)
564 for (List<JCTypeParameter> typarams = tree.typarams;
565 typarams.nonEmpty();
566 typarams = typarams.tail)
567 baseScope.enter(typarams.head.type.tsym);
568 Env<AttrContext> outer = env.outer; // the base clause can't see members of this class
569 Env<AttrContext> localEnv = outer.dup(tree, outer.info.dup(baseScope));
570 localEnv.baseClause = true;
571 localEnv.outer = outer;
572 return localEnv;
573 }
574
575 /** Generate a base clause for an enum type.
576 * @param pos The position for trees and diagnostics, if any
577 * @param c The class symbol of the enum
578 */
579 protected JCExpression enumBase(int pos, ClassSymbol c) {
580 JCExpression result = make.at(pos).
581 TypeApply(make.QualIdent(syms.enumSym),
582 List.of(make.Type(c.type)));
583 return result;
584 }
585
586 /** Generate a base clause for a record type.
587 * @param pos The position for trees and diagnostics, if any
588 * @param c The class symbol of the record
589 */
590 protected JCExpression recordBase(int pos, ClassSymbol c) {
591 JCExpression result = make.at(pos).
1121 (types.supertype(tree.sym.type).tsym.flags() & Flags.ENUM) == 0) {
1122 addEnumMembers(tree, env);
1123 }
1124 boolean isRecord = (tree.sym.flags_field & RECORD) != 0;
1125 List<JCTree> alreadyEntered = null;
1126 if (isRecord) {
1127 alreadyEntered = List.convert(JCTree.class, TreeInfo.recordFields(tree));
1128 alreadyEntered = alreadyEntered.prependList(tree.defs.stream()
1129 .filter(t -> TreeInfo.isConstructor(t) && t != defaultConstructor).collect(List.collector()));
1130 }
1131 List<JCTree> defsToEnter = isRecord ?
1132 tree.defs.diff(alreadyEntered) : tree.defs;
1133 memberEnter.memberEnter(defsToEnter, env);
1134 if (isRecord) {
1135 addRecordMembersIfNeeded(tree, env);
1136 }
1137 if (tree.sym.isAnnotationType()) {
1138 Assert.check(tree.sym.isCompleted());
1139 tree.sym.setAnnotationTypeMetadata(new AnnotationTypeMetadata(tree.sym, annotate.annotationTypeSourceCompleter()));
1140 }
1141
1142 if ((tree.sym.flags() & (INTERFACE | VALUE_CLASS)) == 0) {
1143 tree.sym.flags_field |= IDENTITY_TYPE;
1144 }
1145 }
1146
1147 private void addAccessor(JCVariableDecl tree, Env<AttrContext> env) {
1148 MethodSymbol implSym = lookupMethod(env.enclClass.sym, tree.sym.name, List.nil());
1149 RecordComponent rec = ((ClassSymbol) tree.sym.owner).getRecordComponent(tree.sym);
1150 if (implSym == null || (implSym.flags_field & GENERATED_MEMBER) != 0) {
1151 /* here we are pushing the annotations present in the corresponding field down to the accessor
1152 * it could be that some of those annotations are not applicable to the accessor, they will be striped
1153 * away later at Check::validateAnnotation
1154 */
1155 TreeCopier<JCTree> tc = new TreeCopier<JCTree>(make.at(tree.pos));
1156 List<JCAnnotation> originalAnnos = rec.getOriginalAnnos().isEmpty() ?
1157 rec.getOriginalAnnos() :
1158 tc.copy(rec.getOriginalAnnos());
1159 JCVariableDecl recordField = TreeInfo.recordFields((JCClassDecl) env.tree).stream().filter(rf -> rf.name == tree.name).findAny().get();
1160 JCMethodDecl getter = make.at(tree.pos).
1161 MethodDef(
1162 make.Modifiers(PUBLIC | Flags.GENERATED_MEMBER, originalAnnos),
1163 tree.sym.name,
1164 /* we need to special case for the case when the user declared the type as an ident
|