Skip to content

8362885: A more formal way to mark javac's Flags that belong to a specific Symbol type only #26452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion make/ToolsLangtools.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $(eval $(call SetupJavaCompilation, BUILD_TOOLS_LANGTOOLS, \
COMPILER := bootjdk, \
TARGET_RELEASE := $(TARGET_RELEASE_BOOTJDK), \
SRC := $(TOPDIR)/make/langtools/tools, \
INCLUDES := compileproperties propertiesparser, \
INCLUDES := compileproperties flagsgenerator propertiesparser, \
COPY := .properties, \
BIN := $(BUILDTOOLS_OUTPUTDIR)/langtools_tools_classes, \
))
Expand Down
185 changes: 185 additions & 0 deletions make/langtools/tools/flagsgenerator/FlagsGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package flagsgenerator;

import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TreePath;
import com.sun.source.util.Trees;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementFilter;
import javax.tools.ToolProvider;

public class FlagsGenerator {
public static void main(String... args) throws IOException {
var compiler = ToolProvider.getSystemJavaCompiler();

try (var fm = compiler.getStandardFileManager(null, null, null)) {
JavacTask task = (JavacTask) compiler.getTask(null, null, d -> {}, null, null, fm.getJavaFileObjects(args[0]));
Trees trees = Trees.instance(task);
CompilationUnitTree cut = task.parse().iterator().next();

task.analyze();

TypeElement clazz = (TypeElement) trees.getElement(new TreePath(new TreePath(cut), cut.getTypeDecls().get(0)));
Map<Integer, List<String>> flag2Names = new TreeMap<>();
Map<FlagTarget, Map<Integer, List<String>>> target2FlagBit2Fields = new HashMap<>();
Map<String, String> customToString = new HashMap<>();
Set<String> noToString = new HashSet<>();

for (VariableElement field : ElementFilter.fieldsIn(clazz.getEnclosedElements())) {
String flagName = field.getSimpleName().toString();
for (AnnotationMirror am : field.getAnnotationMirrors()) {
switch (am.getAnnotationType().toString()) {
case "com.sun.tools.javac.code.Flags.Use" -> {
long flagValue = ((Number) field.getConstantValue()).longValue();
int flagBit = 63 - Long.numberOfLeadingZeros(flagValue);

flag2Names.computeIfAbsent(flagBit, _ -> new ArrayList<>())
.add(flagName);

List<?> originalTargets = (List<?>) valueOfValueAttribute(am);
originalTargets.stream()
.map(value -> FlagTarget.valueOf(value.toString()))
.forEach(target -> target2FlagBit2Fields.computeIfAbsent(target, _ -> new HashMap<>())
.computeIfAbsent(flagBit, _ -> new ArrayList<>())
.add(flagName));
}
case "com.sun.tools.javac.code.Flags.CustomToStringValue" -> {
customToString.put(flagName, (String) valueOfValueAttribute(am));
}
case "com.sun.tools.javac.code.Flags.NoToStringValue" -> {
noToString.add(flagName);
}
}
}
}

//verify there are no flag overlaps:
for (Entry<FlagTarget, Map<Integer, List<String>>> targetAndFlag : target2FlagBit2Fields.entrySet()) {
for (Entry<Integer, List<String>> flagAndFields : targetAndFlag.getValue().entrySet()) {
if (flagAndFields.getValue().size() > 1) {
throw new AssertionError("duplicate flag for target: " + targetAndFlag.getKey() +
", flag: " + flagAndFields.getKey() +
", flags fields: " + flagAndFields.getValue());
}
}
}

String masks = Arrays.stream(FlagTarget.values())
.map(target -> " public static final long MASK_" + target.name() + "_FLAGS = " + getMask(target2FlagBit2Fields, target) + ";")
.collect(Collectors.joining("\n"));

try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(Paths.get(args[1])))) {
out.println("""
package com.sun.tools.javac.code;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a license header for generated sources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CompilerProperties and other files that are generated using similar means don't have license headers, so I assume we don't need that.

import com.sun.tools.javac.util.Assert;
public enum FlagsEnum {
""");
for (Entry<Integer, List<String>> e : flag2Names.entrySet()) {
String constantName = e.getValue().stream().collect(Collectors.joining("_OR_"));
String toString = e.getValue()
.stream()
.filter(n -> !noToString.contains(n))
.map(n -> customToString.getOrDefault(n, n.toLowerCase(Locale.US)))
.collect(Collectors.joining(" or "));
out.println(" " + constantName + "(1L<<" + e.getKey() + ", \"" + toString + "\"),");
}
out.println("""
;
${masks}
private final long value;
private final String toString;
private FlagsEnum(long value, String toString) {
this.value = value;
this.toString = toString;
}
public long value() {
return value;
}
public String toString() {
return toString;
}
public static void assertNoUnexpectedFlags(long flags, long mask) {
Assert.check((flags & ~mask) == 0,
() -> "Unexpected flags: 0x" + Long.toHexString(flags & ~mask) + "L (" +
Flags.asFlagSet(flags & ~mask) + ")");
}
}
""".replace("${masks}", masks));
}
}
}

private static String getMask(Map<FlagTarget, Map<Integer, List<String>>> target2FlagBit2Fields,
FlagTarget target) {
long mask = target2FlagBit2Fields.get(target)
.keySet()
.stream()
.mapToLong(bit -> 1L << bit)
.reduce(0, (l, r) -> l | r);

return "0x" + Long.toHexString(mask) + "L";
}

private static Object valueOfValueAttribute(AnnotationMirror am) {
return am.getElementValues()
.values()
.iterator()
.next()
.getValue();
}

public enum FlagTarget {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just flyby comment: symbol kind is an enum, so in principle we could use constants such as MTH, VAR, TYP as targets, if we wanted to connect the decls more with the actual code. But I like your approach as well (as symbol kind has also a lot of other more dubious stuff/noise)

BLOCK,
CLASS,
METHOD,
MODULE,
PACKAGE,
TYPE_VAR,
VARIABLE;
}
}
4 changes: 2 additions & 2 deletions make/langtools/tools/propertiesparser/parser/MessageType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -76,7 +76,7 @@ public enum SimpleType implements MessageType {
ANNOTATION("annotation", "Compound", "com.sun.tools.javac.code.Attribute"),
BOOLEAN("boolean", "boolean", null),
COLLECTION("collection", "Collection", "java.util"),
FLAG("flag", "Flag", "com.sun.tools.javac.code.Flags"),
FLAG("flag", "FlagsEnum", "com.sun.tools.javac.code"),
FRAGMENT("fragment", "Fragment", null),
DIAGNOSTIC("diagnostic", "JCDiagnostic", "com.sun.tools.javac.util"),
MODIFIER("modifier", "Modifier", "javax.lang.model.element"),
Expand Down
35 changes: 30 additions & 5 deletions make/modules/jdk.compiler/Gensrc.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ $(eval $(call SetupCompileProperties, COMPILE_PROPERTIES, \

TARGETS += $(COMPILE_PROPERTIES)

################################################################################
#
# Compile properties files into enum-like classes using the propertiesparser tool
#

# To avoid reevaluating the compilation setup for the tools each time this file
# is included, the following trick is used to be able to declare a dependency on
# the built tools.
BUILD_TOOLS_LANGTOOLS := $(call SetupJavaCompilationCompileTarget, \
BUILD_TOOLS_LANGTOOLS, $(BUILDTOOLS_OUTPUTDIR)/langtools_tools_classes)

################################################################################
#
# Compile properties files into enum-like classes using the propertiesparser tool
#

TOOL_PARSEPROPERTIES_CMD := $(JAVA_SMALL) -cp $(BUILDTOOLS_OUTPUTDIR)/langtools_tools_classes \
propertiesparser.PropertiesParser

Expand All @@ -76,3 +76,28 @@ $(eval $(call SetupExecute, PARSEPROPERTIES, \
TARGETS += $(PARSEPROPERTIES)

################################################################################

################################################################################
#
# Generate FlagsEnum from Flags constants:
#

TOOL_FLAGSGENERATOR_CMD := $(JAVA_SMALL) -cp $(BUILDTOOLS_OUTPUTDIR)/langtools_tools_classes \
flagsgenerator.FlagsGenerator

FLAGS_SRC := \
$(MODULE_SRC)/share/classes/com/sun/tools/javac/code/Flags.java

FLAGS_OUT := \
$(SUPPORT_OUTPUTDIR)/gensrc/$(MODULE)/com/sun/tools/javac/code/FlagsEnum.java

$(eval $(call SetupExecute, FLAGSGENERATOR, \
WARN := Generating FlagsEnum, \
DEPS := $(FLAGS_SRC) $(BUILD_TOOLS_LANGTOOLS), \
OUTPUT_FILE := $(FLAGS_OUT), \
COMMAND := $(TOOL_FLAGSGENERATOR_CMD) $(FLAGS_SRC) $(FLAGS_OUT), \
))

TARGETS += $(FLAGSGENERATOR)

################################################################################
Loading