Skip to content

Commit 42f66c0

Browse files
committed
Add the 'create_from_template' method to rewriting context
1 parent a405e90 commit 42f66c0

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/methods/RewritingContextMethods.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package com.adacore.lkql_jit.built_ins.methods;
77

88
import com.adacore.libadalang.Libadalang;
9+
import com.adacore.libadalang.Libadalang.GrammarRule;
910
import com.adacore.libadalang.Libadalang.MemberReference;
1011
import com.adacore.libadalang.Libadalang.RewritingContext;
1112
import com.adacore.libadalang.Libadalang.RewritingNode;
@@ -15,7 +16,9 @@
1516
import com.adacore.lkql_jit.exception.LKQLRuntimeException;
1617
import com.adacore.lkql_jit.nodes.utils.RewritingNodeConverter;
1718
import com.adacore.lkql_jit.nodes.utils.RewritingNodeConverterNodeGen;
19+
import com.adacore.lkql_jit.runtime.values.lists.LKQLList;
1820
import com.adacore.lkql_jit.utils.LKQLTypesHelper;
21+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1922
import com.oracle.truffle.api.dsl.Specialization;
2023
import com.oracle.truffle.api.frame.VirtualFrame;
2124

@@ -181,4 +184,39 @@ public Object executeGeneric(VirtualFrame frame, RewritingContext ctx, Object ob
181184
return ctx;
182185
}
183186
}
187+
188+
@BuiltInMethod(
189+
name = "create_from_template",
190+
doc = "Create a new node from the provided template, filling '{}' with provided" +
191+
" argument, and parsing the template with the specified grammar rule"
192+
)
193+
public abstract static class CreateFromTemplateExpr extends BaseRewritingContextExpr {
194+
195+
@TruffleBoundary
196+
private static GrammarRule ruleFromString(String ruleName) {
197+
return GrammarRule.valueOf(ruleName.toUpperCase());
198+
}
199+
200+
@Specialization
201+
public RewritingNode doGeneric(
202+
VirtualFrame frame,
203+
RewritingContext ctx,
204+
String template,
205+
String grammarRule,
206+
LKQLList arguments
207+
) {
208+
// Translate the provided LKQL list into a rewriting node list
209+
final var args = new RewritingNode[(int) arguments.size()];
210+
for (int i = 0; i < args.length; i++) {
211+
args[i] = convert(frame, arguments.get(i), true);
212+
}
213+
214+
// Then call the internal function to process the template
215+
try {
216+
return ctx.createFromTemplate(template, ruleFromString(grammarRule), args);
217+
} catch (Exception e) {
218+
throw LKQLRuntimeException.fromJavaException(e, this.callNode);
219+
}
220+
}
221+
}
184222
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@check(
2+
message="Custom rule violation",
3+
auto_fix=(n, ctx) => ctx.replace(
4+
n,
5+
ctx.create_from_template("Call_Something ({});", "call_stmt_rule", [n.f_label_name])
6+
)
7+
)
8+
fun custom(node) = node is GotoStmt
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
procedure Main is
2+
begin
3+
goto lbl; -- FLAG
4+
end Main;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
main.adb:3:4: rule violation: Custom rule violation
2+
3 | goto lbl; -- FLAG
3+
| ^^^^^^^^^
4+
5+
Patched "main.adb":
6+
===================
7+
8+
procedure Main is
9+
begin
10+
Call_Something(lbl);end Main;
11+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
driver: checker
2+
input_sources:
3+
- main.adb
4+
rule_name: custom
5+
auto_fix: True

0 commit comments

Comments
 (0)