Skip to content
Closed
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
161 changes: 161 additions & 0 deletions make/langtools/tools/flagsgenerator/FlagsGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* 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.EnumMap;
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 EnumMap<>(FlagTarget.class);
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));
Copy link
Member

Choose a reason for hiding this comment

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

Instead of a dedicated loop to verify no overlaps, we can make the nested map Map<Integer, String> and fail fast whenever we detect a conflict, like:

var oldFlagName = target2FlagBit2Fields.computeIfAbsent(target, _ -> new HashMap<>()).put(flagBit, flagName);
if (oldFlagName != null) {
    // Fail fast code
}

I personally don't see a reason to collect all conflicting fields for a flag if any of these conflicts already causes a failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its unlikely to be common in the long run, but there may be conflicts among more than two fields. Reporting all at once (rather than reporting one conflict, and after resolving reporting another) seems nicer.

}
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());
}
}
}

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.


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("""
;

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;
}
}
""");
}
}
}

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

private enum FlagTarget {
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
33 changes: 28 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,26 @@ $(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