Skip to content

Commit 6749c62

Browse files
committed
8358772: Template-Framework Library: Primitive Types
Reviewed-by: mhaessig, chagedorn
1 parent 9aeacf2 commit 6749c62

File tree

7 files changed

+585
-44
lines changed

7 files changed

+585
-44
lines changed

test/hotspot/jtreg/compiler/lib/template_framework/Hook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public record Hook(String name) {
7575
* @return A {@link Token} that captures the anchoring of the scope and the list of validated {@link Token}s.
7676
*/
7777
public Token anchor(Object... tokens) {
78-
return new HookAnchorToken(this, Token.parse(tokens));
78+
return new HookAnchorToken(this, TokenParser.parse(tokens));
7979
}
8080

8181
/**

test/hotspot/jtreg/compiler/lib/template_framework/Template.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ static <T1, T2, T3> Template.ThreeArgs<T1, T2, T3> make(String arg1Name, String
615615
* @throws IllegalArgumentException if the list of tokens contains an unexpected object.
616616
*/
617617
static TemplateBody body(Object... tokens) {
618-
return new TemplateBody(Token.parse(tokens));
618+
return new TemplateBody(TokenParser.parse(tokens));
619619
}
620620

621621
/**

test/hotspot/jtreg/compiler/lib/template_framework/Token.java

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,11 @@
2323

2424
package compiler.lib.template_framework;
2525

26-
import java.util.Arrays;
27-
import java.util.ArrayList;
28-
import java.util.List;
29-
3026
/**
3127
* The {@link Template#body} and {@link Hook#anchor} are given a list of tokens, which are either
32-
* {@link Token}s or {@link String}s or some permitted boxed primitives. These are then parsed
33-
* and all non-{@link Token}s are converted to {@link StringToken}s. The parsing also flattens
34-
* {@link List}s.
28+
* {@link Token}s or {@link String}s or some permitted boxed primitives.
3529
*/
36-
sealed interface Token permits StringToken,
30+
public sealed interface Token permits StringToken,
3731
TemplateToken,
3832
TemplateToken.ZeroArgs,
3933
TemplateToken.OneArg,
@@ -42,37 +36,4 @@ sealed interface Token permits StringToken,
4236
HookAnchorToken,
4337
HookInsertToken,
4438
AddNameToken,
45-
NothingToken
46-
{
47-
static List<Token> parse(Object[] objects) {
48-
if (objects == null) {
49-
throw new IllegalArgumentException("Unexpected tokens: null");
50-
}
51-
List<Token> outputList = new ArrayList<>();
52-
parseToken(Arrays.asList(objects), outputList);
53-
return outputList;
54-
}
55-
56-
private static void parseList(List<?> inputList, List<Token> outputList) {
57-
for (Object o : inputList) {
58-
parseToken(o, outputList);
59-
}
60-
}
61-
62-
private static void parseToken(Object o, List<Token> outputList) {
63-
if (o == null) {
64-
throw new IllegalArgumentException("Unexpected token: null");
65-
}
66-
switch (o) {
67-
case Token t -> outputList.add(t);
68-
case String s -> outputList.add(new StringToken(Renderer.format(s)));
69-
case Integer s -> outputList.add(new StringToken(Renderer.format(s)));
70-
case Long s -> outputList.add(new StringToken(Renderer.format(s)));
71-
case Double s -> outputList.add(new StringToken(Renderer.format(s)));
72-
case Float s -> outputList.add(new StringToken(Renderer.format(s)));
73-
case Boolean s -> outputList.add(new StringToken(Renderer.format(s)));
74-
case List<?> l -> parseList(l, outputList);
75-
default -> throw new IllegalArgumentException("Unexpected token: " + o);
76-
}
77-
}
78-
}
39+
NothingToken {}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2025, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package compiler.lib.template_framework;
25+
26+
import java.util.Arrays;
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
30+
/**
31+
* Helper class for {@link Token}, to keep the parsing methods package private.
32+
*
33+
* <p>
34+
* The {@link Template#body} and {@link Hook#anchor} are given a list of tokens, which are either
35+
* {@link Token}s or {@link String}s or some permitted boxed primitives. These are then parsed
36+
* and all non-{@link Token}s are converted to {@link StringToken}s. The parsing also flattens
37+
* {@link List}s.
38+
*/
39+
final class TokenParser {
40+
static List<Token> parse(Object[] objects) {
41+
if (objects == null) {
42+
throw new IllegalArgumentException("Unexpected tokens: null");
43+
}
44+
List<Token> outputList = new ArrayList<>();
45+
parseToken(Arrays.asList(objects), outputList);
46+
return outputList;
47+
}
48+
49+
private static void parseList(List<?> inputList, List<Token> outputList) {
50+
for (Object o : inputList) {
51+
parseToken(o, outputList);
52+
}
53+
}
54+
55+
private static void parseToken(Object o, List<Token> outputList) {
56+
if (o == null) {
57+
throw new IllegalArgumentException("Unexpected token: null");
58+
}
59+
switch (o) {
60+
case Token t -> outputList.add(t);
61+
case String s -> outputList.add(new StringToken(Renderer.format(s)));
62+
case Integer s -> outputList.add(new StringToken(Renderer.format(s)));
63+
case Long s -> outputList.add(new StringToken(Renderer.format(s)));
64+
case Double s -> outputList.add(new StringToken(Renderer.format(s)));
65+
case Float s -> outputList.add(new StringToken(Renderer.format(s)));
66+
case Boolean s -> outputList.add(new StringToken(Renderer.format(s)));
67+
case List<?> l -> parseList(l, outputList);
68+
default -> throw new IllegalArgumentException("Unexpected token: " + o);
69+
}
70+
}
71+
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2025, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
package compiler.lib.template_framework.library;
25+
26+
import java.util.List;
27+
28+
import compiler.lib.template_framework.DataName;
29+
import compiler.lib.template_framework.Template;
30+
31+
/**
32+
* The {@link CodeGenerationDataNameType} extends the {@link DataName.Type} with
33+
* additional functionality for code generation. These types with their extended
34+
* functionality can be used with many other code generation facilities in the
35+
* library, such as generating random {@code Expression}s.
36+
*/
37+
public interface CodeGenerationDataNameType extends DataName.Type {
38+
39+
/**
40+
* This method provides a random constant value for the type, which can
41+
* be used as a token inside a {@link Template}.
42+
*
43+
* @return A random constant value.
44+
*/
45+
Object con();
46+
47+
/**
48+
* The byte {@link PrimitiveType}.
49+
*
50+
* @return The byte {@link PrimitiveType}.
51+
*/
52+
static PrimitiveType bytes() { return PrimitiveType.BYTES; }
53+
54+
/**
55+
* The short {@link PrimitiveType}.
56+
*
57+
* @return The short {@link PrimitiveType}.
58+
*/
59+
static PrimitiveType shorts() { return PrimitiveType.SHORTS; }
60+
61+
/**
62+
* The char {@link PrimitiveType}.
63+
*
64+
* @return The char {@link PrimitiveType}.
65+
*/
66+
static PrimitiveType chars() { return PrimitiveType.CHARS; }
67+
68+
/**
69+
* The int {@link PrimitiveType}.
70+
*
71+
* @return The int {@link PrimitiveType}.
72+
*/
73+
static PrimitiveType ints() { return PrimitiveType.INTS; }
74+
75+
/**
76+
* The long {@link PrimitiveType}.
77+
*
78+
* @return The long {@link PrimitiveType}.
79+
*/
80+
static PrimitiveType longs() { return PrimitiveType.LONGS; }
81+
82+
/**
83+
* The float {@link PrimitiveType}.
84+
*
85+
* @return The float {@link PrimitiveType}.
86+
*/
87+
static PrimitiveType floats() { return PrimitiveType.FLOATS; }
88+
89+
/**
90+
* The double {@link PrimitiveType}.
91+
*
92+
* @return The double {@link PrimitiveType}.
93+
*/
94+
static PrimitiveType doubles() { return PrimitiveType.DOUBLES; }
95+
96+
/**
97+
* The boolean {@link PrimitiveType}.
98+
*
99+
* @return The boolean {@link PrimitiveType}.
100+
*/
101+
static PrimitiveType booleans() { return PrimitiveType.BOOLEANS; }
102+
103+
/**
104+
* List of all {@link PrimitiveType}s.
105+
*/
106+
List<PrimitiveType> PRIMITIVE_TYPES = List.of(
107+
bytes(),
108+
chars(),
109+
shorts(),
110+
ints(),
111+
longs(),
112+
floats(),
113+
doubles(),
114+
booleans()
115+
);
116+
117+
/**
118+
* List of all integral {@link PrimitiveType}s (byte, char, short, int, long).
119+
*/
120+
List<PrimitiveType> INTEGRAL_TYPES = List.of(
121+
bytes(),
122+
chars(),
123+
shorts(),
124+
ints(),
125+
longs()
126+
);
127+
128+
/**
129+
* List of all subword {@link PrimitiveType}s (byte, char, short).
130+
*/
131+
List<PrimitiveType> SUBWORD_TYPES = List.of(
132+
bytes(),
133+
chars(),
134+
shorts()
135+
);
136+
137+
/**
138+
* List of all floating {@link PrimitiveType}s (float, double).
139+
*/
140+
List<PrimitiveType> FLOATING_TYPES = List.of(
141+
floats(),
142+
doubles()
143+
);
144+
145+
/**
146+
* List of all integral and floating {@link PrimitiveType}s.
147+
*/
148+
List<PrimitiveType> INTEGRAL_AND_FLOATING_TYPES = List.of(
149+
bytes(),
150+
chars(),
151+
shorts(),
152+
ints(),
153+
longs(),
154+
floats(),
155+
doubles()
156+
);
157+
}

0 commit comments

Comments
 (0)