Skip to content

8352861: Template-Framework Library #23418

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

Closed
wants to merge 214 commits into from
Closed
Changes from all commits
Commits
Show all changes
214 commits
Select commit Hold shift + click to select a range
f9725b1
JDK-8344942
eme64 Feb 3, 2025
87e1b12
withArgs instead of use
eme64 Feb 3, 2025
c7d0828
more testing and refactoring
eme64 Feb 3, 2025
c93c193
more basic tests
eme64 Feb 3, 2025
6a4d875
test wip
eme64 Feb 3, 2025
09b41a1
more test wip
eme64 Feb 3, 2025
cea77c4
refactor and handle List
eme64 Feb 3, 2025
2af1609
wip Code render refactor wip
eme64 Feb 3, 2025
4e1fd4b
nested code rendering
eme64 Feb 3, 2025
7be8772
setting and findig hooks
eme64 Feb 4, 2025
3452290
STACK -> Frame.parent
eme64 Feb 4, 2025
e707204
move frame creation to recursive template use
eme64 Feb 4, 2025
f8151d6
implement HookUse
eme64 Feb 5, 2025
6905e84
nested hooks
eme64 Feb 5, 2025
16e8102
testHookWithNestedTemplates
eme64 Feb 5, 2025
704659c
wip names
eme64 Feb 5, 2025
ebc4297
list refactor
eme64 Feb 6, 2025
f5698b9
RenderInfo
eme64 Feb 6, 2025
6bfe918
refactor with TemplateFrame
eme64 Feb 6, 2025
fef2c1e
dollar with hooks
eme64 Feb 6, 2025
7be1d97
fix order of nested hook insertions
eme64 Feb 6, 2025
7abba9b
LetUse, hashtag replacements refactor, TemplateFrame refactor
eme64 Feb 6, 2025
179f768
let works inside whole template
eme64 Feb 7, 2025
e0982dc
simplify TemplateFrame
eme64 Feb 7, 2025
397f16e
Frame -> CodeFrame refactor
eme64 Feb 7, 2025
a5b4833
rm public for a field
eme64 Feb 7, 2025
61caf00
Token refactoring
eme64 Feb 7, 2025
f0c679a
InstantiatedTemplate -> TemplateBody
eme64 Feb 7, 2025
2b0e2a3
change from LetToken to Nothing
eme64 Feb 7, 2025
41bc62c
let with function
eme64 Feb 7, 2025
0bca0e6
let example
eme64 Feb 7, 2025
9b8e9f7
Renderer refactor
eme64 Feb 7, 2025
4be7d98
testSelector
eme64 Feb 7, 2025
3e2153d
wip fuel and done TemplateBinding
eme64 Feb 7, 2025
5a7d118
custom fuel cost with example
eme64 Feb 7, 2025
81beb03
imporve documentation
eme64 Feb 7, 2025
5daa95e
create nice constants for fuel
eme64 Feb 7, 2025
36fa57c
NameSet
eme64 Feb 7, 2025
124213a
testNames wip
eme64 Feb 10, 2025
b2e7281
testNames / defineName first example working
eme64 Feb 10, 2025
4698e5f
testNames2
eme64 Feb 10, 2025
6565068
testNames3
eme64 Feb 10, 2025
a8e925f
testListArgument
eme64 Feb 10, 2025
2ea3fa1
First negative test
eme64 Feb 10, 2025
b85a0af
more negative tests
eme64 Feb 10, 2025
da5ea0d
testFailingHook
eme64 Feb 10, 2025
a054485
testFailingSample
eme64 Feb 10, 2025
a73ee6c
testFailingHashtag
eme64 Feb 10, 2025
7f454fc
binding test
eme64 Feb 10, 2025
6e2b48a
body illegal argument examples
eme64 Feb 10, 2025
89c177a
fix token parsing and add tests
eme64 Feb 10, 2025
0c5eb27
TestManyTests and Library
eme64 Feb 10, 2025
0e58368
finish TestManyTests
eme64 Feb 10, 2025
19e61c3
rename test
eme64 Feb 10, 2025
31c969c
stub of EXPRESSIONS, stub of TestLibrary.java
eme64 Feb 11, 2025
bcc553e
basic format tests
eme64 Feb 11, 2025
81f3d98
TestLibrary
eme64 Feb 11, 2025
a1a787c
fix formatting issues, rename test
eme64 Feb 11, 2025
a356ea8
Library more expression
eme64 Feb 11, 2025
4bf2575
binary operators
eme64 Feb 11, 2025
3e6a24d
run more tests
eme64 Feb 11, 2025
e30c562
handle exceptions
eme64 Feb 11, 2025
5bcb8aa
add exception test
eme64 Feb 11, 2025
c504690
static field load and NaN verify issue
eme64 Feb 12, 2025
9cef04a
Boolean
eme64 Feb 12, 2025
89a1112
another library template
eme64 Feb 12, 2025
a36f585
refactor GENERATE_EARLILER_VALUE_FROM_DELAYED_BOOLEAN
eme64 Feb 12, 2025
e7c9bff
rm special fuel handling
eme64 Feb 12, 2025
1ced857
operator refactoring
eme64 Feb 12, 2025
34759d8
add unary and ternary operators
eme64 Feb 12, 2025
21f721e
move TODOs around
eme64 Feb 12, 2025
8d5b98c
rename / comments
eme64 Feb 12, 2025
85d8b02
rename test
eme64 Feb 12, 2025
fd66de7
TestAdvanced first part
eme64 Feb 12, 2025
42f4a53
more examples
eme64 Feb 12, 2025
231d6d7
example 4 with hooks
eme64 Feb 12, 2025
26e2816
more hook examples
eme64 Feb 12, 2025
ea31f23
more examples and some other changes
eme64 Feb 12, 2025
93989e0
follow up test fix
eme64 Feb 12, 2025
53a06f2
improve documentationo
eme64 Feb 12, 2025
3325e11
more documentation
eme64 Feb 17, 2025
f76492e
some more docs
eme64 Feb 17, 2025
ee35c31
more documentation
eme64 Feb 17, 2025
c880eff
use token for defineName
eme64 Feb 17, 2025
9b64bd5
more documentation
eme64 Feb 17, 2025
1a01a33
more documentation
eme64 Feb 17, 2025
03d67f2
rm NameSelection
eme64 Feb 17, 2025
fd1d4ed
more docs and hook.insert refactor
eme64 Feb 18, 2025
2d70ba3
hook documentation
eme64 Feb 18, 2025
d6f0657
better Template documentation
eme64 Feb 18, 2025
df48a92
template library refactor
eme64 Feb 18, 2025
3052633
improve comments
eme64 Feb 18, 2025
a8129cb
more documentation
eme64 Feb 18, 2025
4dcbbb7
more README documentation
eme64 Feb 19, 2025
3521418
wip types refactor
eme64 Feb 20, 2025
94df422
small change
eme64 Feb 20, 2025
9b4bed8
more expression code wip
eme64 Feb 20, 2025
fc4b3c9
use expression twice
eme64 Feb 20, 2025
0d19345
random unary expressions
eme64 Feb 20, 2025
c9fd16f
binary and ternary operation
eme64 Feb 20, 2025
6991e20
some binary int ops
eme64 Feb 21, 2025
a2d2f26
move files around
eme64 Feb 21, 2025
84a4ec3
more types
eme64 Feb 21, 2025
9630be7
both gold and reference version of test
eme64 Feb 21, 2025
27f5c90
implement expression arguments
eme64 Feb 21, 2025
e79aeba
cleanup and ideas
eme64 Feb 21, 2025
08b7e30
more cleanup
eme64 Feb 21, 2025
c76263f
expressions refactor
eme64 Feb 21, 2025
adbafd7
stub Value.java
eme64 Feb 21, 2025
2baec92
refactor names
eme64 Feb 21, 2025
cef9f8d
improve documentation
eme64 Feb 21, 2025
4c0c462
fix TestTutorial.java with Names
eme64 Feb 24, 2025
a80d414
PrimitiveType refactor
eme64 Feb 24, 2025
c8b1032
Hook.isSet
eme64 Feb 24, 2025
8268427
defineField
eme64 Feb 24, 2025
5247647
set class hook in test
eme64 Feb 24, 2025
87ba30d
3 args, and array test
eme64 Feb 24, 2025
80c70b5
more test descriptions
eme64 Feb 24, 2025
a95147b
using local variables for expression input values
eme64 Feb 24, 2025
b93c4f6
larger expressions, random size, and fix float array verification for…
eme64 Feb 25, 2025
742265e
fill arrays in test
eme64 Feb 25, 2025
3289d45
ByteType
eme64 Feb 26, 2025
d841fe4
char and short
eme64 Feb 26, 2025
46e5a0b
more operations
eme64 Feb 26, 2025
d761512
rm Float.floatToFloat16 because of NaN issues
eme64 Feb 27, 2025
ff0478d
operation refactor
eme64 Feb 27, 2025
330b966
type refactor
eme64 Feb 27, 2025
1b742e3
problem list some operations
eme64 Feb 27, 2025
ff45068
add constants into expression
eme64 Feb 27, 2025
9e87707
stub for Vector API types
eme64 Feb 28, 2025
9a60be3
generate vectors, and verify them
eme64 Feb 28, 2025
da1f140
wip vector api types
eme64 Feb 28, 2025
43c2c7f
wip vector api, and small expression refactoring
eme64 Mar 3, 2025
8a29285
fix broadcast operation
eme64 Mar 3, 2025
2e699fe
a few more ops and fix null for vector api
eme64 Mar 3, 2025
627a048
more ops
eme64 Mar 3, 2025
172d292
make methods final
eme64 Mar 3, 2025
c0d6c4a
load from / store to array
eme64 Mar 3, 2025
05d0e93
add castShape
eme64 Mar 3, 2025
ecf22e7
convert vector api
eme64 Mar 4, 2025
e3facb2
more vector ops
eme64 Mar 4, 2025
d1e68fa
wip lanewise for vector api
eme64 Mar 4, 2025
1f6fb6b
add list of exceptions to operations
eme64 Mar 4, 2025
e253301
argTypes renaming
eme64 Mar 4, 2025
77b6230
handle some vector api exceptions
eme64 Mar 5, 2025
a4b28ef
TestFuzzExpression.java restrict the exceptions we catch
eme64 Mar 5, 2025
4589b93
tweak
eme64 Mar 5, 2025
d199aaa
fix convert
eme64 Mar 5, 2025
158c824
handle addIndex
eme64 Mar 5, 2025
d9958c4
vector api unary and ternary ops
eme64 Mar 5, 2025
f187489
more ops
eme64 Mar 5, 2025
884b932
VOPType refactor
eme64 Mar 6, 2025
88def19
reduction
eme64 Mar 6, 2025
3d0e62c
restrict some operation generations further
eme64 Mar 6, 2025
7380f19
more reinterpret
eme64 Mar 6, 2025
e1d7442
selectFrom
eme64 Mar 6, 2025
1fec66b
slice and some other fixes
eme64 Mar 7, 2025
e83b756
more ops
eme64 Mar 7, 2025
0c1c110
withLane
eme64 Mar 7, 2025
9a32624
more ops
eme64 Mar 7, 2025
443676e
note about toShuffle
eme64 Mar 7, 2025
c812aae
wip MaskType and Verify removed explicit dependency on VectorAPI
eme64 Mar 7, 2025
c4340f2
compile and vmFlags
eme64 Mar 10, 2025
911d723
fix tests
eme64 Mar 10, 2025
8924d9d
rm instructions for JDK-8351627 and rename type lists
eme64 Mar 11, 2025
fc0abb4
Merge branch 'master' into JDK-8344942-TemplateFramework-v2
eme64 Mar 11, 2025
c696d46
some first masked ops and disabling ops from JDK-8350177
eme64 Mar 11, 2025
b3a016f
increase compile timeout and rm expand bc of JDK-8351645
eme64 Mar 11, 2025
2a60dfd
improve assert
eme64 Mar 11, 2025
fefab60
blend
eme64 Mar 11, 2025
cf9d078
fix formatting with backslashes - created issues with windows paths
eme64 Mar 12, 2025
db8e440
more ops
eme64 Mar 13, 2025
9ff7b75
disable FIRST_NONZERO for graal
eme64 Mar 13, 2025
abe5952
more ops
eme64 Mar 13, 2025
7e6385c
more vector ops
eme64 Mar 13, 2025
821acf6
Quaternary and fix
eme64 Mar 13, 2025
fbd63c3
masked lanewise ops
eme64 Mar 13, 2025
232d817
rm ops bc of JDK-8351950
eme64 Mar 13, 2025
a354cd5
more masked ops
eme64 Mar 13, 2025
2f229b2
add test ops
eme64 Mar 13, 2025
c2d5505
more ops
eme64 Mar 13, 2025
2e94741
fix syntax
eme64 Mar 14, 2025
119ed65
basic vector shuffle
eme64 Mar 18, 2025
ad2cf40
minor refactor
eme64 Mar 18, 2025
af80885
more todos
eme64 Mar 18, 2025
62f9968
wip ClassType
eme64 Mar 18, 2025
d83c7ee
wip refactor Verify
eme64 Mar 18, 2025
8e996b6
context / field refactor
eme64 Mar 19, 2025
7c2ff77
impl and test isFloatCheckWithRawBits
eme64 Mar 19, 2025
7b92aba
stub arbitrary classes
eme64 Mar 19, 2025
4fa0a88
wip arbitrary classes
eme64 Mar 19, 2025
e49815c
wip
eme64 Mar 19, 2025
765349e
classes with extends
eme64 Mar 19, 2025
5edfa61
records
eme64 Mar 19, 2025
e1c940e
improve documentation
eme64 Mar 19, 2025
b29a2d2
fix verify usage
eme64 Mar 19, 2025
6d4c1b6
wip class template, and fix verify NaN issue
eme64 Mar 20, 2025
5911fe2
rm print
eme64 Mar 20, 2025
81a012b
class constructors and subclasses
eme64 Mar 20, 2025
bf77472
fix issue with boxed types
eme64 Mar 20, 2025
69b1185
stub random code test
eme64 Mar 20, 2025
2a35613
wip fuzz code, return variable
eme64 Mar 20, 2025
82ae973
stub dispatcher
eme64 Mar 20, 2025
cd2f31e
basic working dispatcher
eme64 Mar 21, 2025
f001720
fix indentation
eme64 Mar 24, 2025
5978068
wip predicate refactor
eme64 Mar 24, 2025
5f02937
weights in dispatcher
eme64 Mar 24, 2025
19775a8
wip fuzz code
eme64 Mar 24, 2025
c943e13
wip, incl if statement
eme64 Mar 24, 2025
921d82e
basic for loop example
eme64 Mar 24, 2025
cc4a96e
limit variables
eme64 Mar 24, 2025
9f0e1f6
manual merge with master
eme64 Mar 25, 2025
89a5600
revert spurious changes
eme64 Mar 25, 2025
ec3915c
catch exceptions more properly
eme64 Mar 25, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*
* 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 compiler.lib.template_framework;

record AddNameToken(Name name) implements Token {}
52 changes: 52 additions & 0 deletions test/hotspot/jtreg/compiler/lib/template_framework/Code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.
*
* 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 compiler.lib.template_framework;

import java.util.ArrayList;
import java.util.List;

/**
* This class collects code, i.e. {@link String}s or {@link List}s of {@link String}s.
* All the Strings are later collected in a {@link StringBuilder}. If we used a {@link StringBuilder}
* directly to collect the Strings, we could not as easily insert code at an "earlier" position, i.e.
* reaching out to a {@link Hook#set}.
*/
sealed interface Code permits Code.Token, Code.CodeList {

record Token(String s) implements Code {
@Override
public void renderTo(StringBuilder builder) {
builder.append(s);
}
}

record CodeList(List<Code> list) implements Code {
@Override
public void renderTo(StringBuilder builder) {
list.forEach(code -> code.renderTo(builder));
}
}

void renderTo(StringBuilder builder);
}
148 changes: 148 additions & 0 deletions test/hotspot/jtreg/compiler/lib/template_framework/CodeFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* 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.
*
* 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 compiler.lib.template_framework;

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;

/**
* The {@link CodeFrame} represents a frame (i.e. scope) of code, appending {@link Code} to the {@code 'codeList'}
* as {@link Token}s are rendered, and adding names to the {@link NameSet}s as they get defined by {@link Template#define}.
* {@link Hook}s can be added to a frame, which allows code to be inserted at that location later.
* When a {@link Hook} is {@link Hook#set}, this separates the {@link Template} into an outer and inner
* {@link CodeFrame}, ensuring that names that are {@link Template#defineName}'d inside the inner frame
* are only available inside that frame.
*
* <p>
* On the other hand, each {@link TemplateFrame} represents the frame (or scope) of exactly one use of a
* {@link Template}.
*
* <p>
* For simple {@link Template} nesting, the {@link CodeFrame}s and {@link TemplateFrame}s overlap exactly.
* However, when using {@link Hook#insert}, we simply nest {@link TemplateFrame}s, going further "in",
* but we jump to an outer {@link CodeFrame}, ensuring that we insert {@link Code} at the outer frame,
* and operating on the names of the outer frame. Once the {@link Hook#insert}ion is complete, we jump
* back to the caller {@link TemplateFrame} and {@link CodeFrame}.
*/
class CodeFrame {
public final CodeFrame parent;
private final List<Code> codeList = new ArrayList<Code>();
private final Map<Hook, Code.CodeList> hookCodeLists = new HashMap<>();

/**
* The {@link NameSet} is used for variable and fields etc.
*/
final NameSet names;

private CodeFrame(CodeFrame parent, boolean isTransparentForNames) {
this.parent = parent;
if (parent == null) {
// NameSet without any parent.
this.names = new NameSet(null);
} else if (isTransparentForNames) {
// We use the same NameSet as the parent - makes it transparent.
this.names = parent.names;
} else {
// New NameSet, to make sure we have a nested scope for the names.
this.names = new NameSet(parent.names);
}
}

/**
* Creates a base frame, which has no {@link parent}.
*/
public static CodeFrame makeBase() {
return new CodeFrame(null, false);
}

/**
* Creates a normal frame, which has a {@link parent} and which defines an inner
* {@link NameSet}, for the names that are generated inside this frame. Once this
* frame is exited, the name from inside this frame are not available any more.
*/
public static CodeFrame make(CodeFrame parent) {
return new CodeFrame(parent, false);
}

/**
* Creates a special frame, which has a {@link parent} and but uses the {@link NameSet}
* from the parent frame, allowing {@link Template#defineName} to persist in the outer
* frame when the current frame is exited. This is necessary for {@link Hook#insert},
* where we would possibly want to make field or variable definitions during the insertion
* that are not just local to the insertion but affect the {@link CodeFrame} that we
* {@link Hook#set} earlier and are now {@link Hook#insert}ing into.
*/
public static CodeFrame makeTransparentForNames(CodeFrame parent) {
return new CodeFrame(parent, true);
}

void addString(String s) {
codeList.add(new Code.Token(s));
}

void addCode(Code code) {
codeList.add(code);
}

void addHook(Hook hook) {
if (hasHook(hook)) {
// This should never happen, as we add a dedicated CodeFrame for each hook.
throw new RuntimeException("Internal error: Duplicate Hook in CodeFrame: " + hook.name());
}
hookCodeLists.put(hook, new Code.CodeList(new ArrayList<Code>()));
}

boolean hasHook(Hook hook) {
return hookCodeLists.containsKey(hook);
}

CodeFrame codeFrameForHook(Hook hook) {
CodeFrame current = this;
while (current != null) {
if (current.hasHook(hook)) {
return current;
}
current = current.parent;
}
return null;
}

void addName(Name name) {
names.add(name);
}

long weighNames(Name.Type type, boolean onlyMutable) {
return names.weight(type, onlyMutable);
}

Name sampleName(Name.Type type, boolean onlyMutable) {
return names.sample(type, onlyMutable);
}

Code getCode() {
return new Code.CodeList(codeList);
}
}
101 changes: 101 additions & 0 deletions test/hotspot/jtreg/compiler/lib/template_framework/Hook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.
*
* 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 compiler.lib.template_framework;

import java.util.List;

/**
* {@link Hook}s can be {@link set} for a certain scope in a {@link Template}, and all nested
* {@link Template}s in this scope, and then from within this scope, any {@link Template} can
* {@link insert} code to where the {@link Hook} was {@link set}. This can be useful to reach
* "back" or to some outer scope, e.g. while generating code for a method, one can reach out
* to the class scope to insert fields.
*
* Example:
* {@snippet lang=java :
* var myHook = new Hook("MyHook");
*
* var template1 = Template.make("name", (String name) -> body(
* """
* public static int #name = 42;
* """
* ));
*
* var template2 = Template.make(() -> body(
* """
* public class Test {
* """,
* // Set the hook here.
* myHook.set(
* """
* public static void main(String[] args) {
* System.out.println("$field: " + $field)
* """,
* // Reach out to where the hook was set, and insert the code of template1.
* myHook.insert(template1.withArgs($("field"))),
* """
* }
* """
* ),
* """
* }
* """
* ));
* }
*
* @param name The name of the Hook, for debugging purposes only.
*/
public record Hook(String name) {
/**
* Set this {@link Hook} for the scope of the provided {@code 'tokens'}.
* From anywhere inside this scope, even in nested {@link Template}s, code can be
* {@link insert}ed back to the location where this {@link Hook} was {@link set}.
*
* @param tokens A list of tokens, which have the same restrictions as {@link Template#body}.
* @return A {@link Token} that captures the setting of the scope and the list of validated {@link Token}s.
*/
public Token set(Object... tokens) {
return new HookSetToken(this, Token.parse(tokens));
}

/**
* Inserts a {@link TemplateWithArgs} to the innermost location where this {@link Hook} was {@link set}.
* This could be in the same {@link Template}, or one nested further out.
*
* @param templateWithArgs The {@link Template} with applied arguments to be inserted at the {@link Hook}.
* @return The {@link Token} which when used inside a {@link Template#body} performs the code insertion into the {@link Hook}.
*/
public Token insert(TemplateWithArgs templateWithArgs) {
return new HookInsertToken(this, templateWithArgs);
}

/**
* Checks if the {@link Hook} was {@link Hook#set} for the current scope or an outer scope.
*
* @return If the {@link Hook} was {@link Hook#set} for the current scope or an outer scope.
*/
public boolean isSet() {
return Renderer.getCurrent().isSet(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.
*
* 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 compiler.lib.template_framework;

record HookInsertToken(Hook hook, TemplateWithArgs templateWithArgs) implements Token {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.
*
* 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 compiler.lib.template_framework;

import java.util.List;

record HookSetToken(Hook hook, List<Token> tokens) implements Token {}
Loading