[code-reflection] RFR: 8324789: Add line number information to code models
Paul Sandoz
psandoz at openjdk.org
Tue Apr 23 23:11:53 UTC 2024
On Thu, 18 Apr 2024 10:52:00 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> Enable operations to have originating source location information, specifically source reference, line and column information.
>>
>> The location information may be set (one or more times) on an operation while it is unbound. Once it is bound, when it is a member of a block and that block is bound, it can no longer be set. Copying an operation will also copy the location. This enables preservation when transforming, especially for lowering.
>>
>> The compiler generates location information from a tree node, and sets it on the associated operation(s). For the operations associated with a reflected method (FuncOp) or quoted lambda expression (LambdaOp or ClosureOp) the source reference (a URI) is added to the location, whereas for all other operations the source reference is absent.
>>
>> Example:
>>
>>
>> @CodeReflection // 47
>> static int f(int n) { // 48
>> int sum = 0; // 49
>> for (int i = 0; i < n; i++) { // 50
>> sum += i; // 51
>> } // 52
>> return sum; // 53
>> } // 54
>>
>>
>> Model:
>>
>>
>> func @"f" @loc="47:5:file:///Users/sandoz/Projects/jdk/test/babylon-test/src/test/java/T.java" (%0 : int)int -> {
>> %1 : Var<int> = var %0 @"n" @loc="47:5";
>> %2 : int = constant @"0" @loc="49:19";
>> %3 : Var<int> = var %2 @"sum" @loc="49:9";
>> java.for @loc="50:9"
>> ()Var<int> -> {
>> %4 : int = constant @"0" @loc="50:22";
>> %5 : Var<int> = var %4 @"i" @loc="50:14";
>> yield %5 @loc="50:9";
>> }
>> (%6 : Var<int>)boolean -> {
>> %7 : int = var.load %6 @loc="50:25";
>> %8 : int = var.load %1 @loc="50:29";
>> %9 : boolean = lt %7 %8 @loc="50:25";
>> yield %9 @loc="50:9";
>> }
>> (%10 : Var<int>)void -> {
>> %11 : int = var.load %10 @loc="50:32";
>> %12 : int = constant @"1" @loc="50:32";
>> %13 : int = add %11 %12 @loc="50:32";
>> var.store %10 %13 @loc="50:32";
>> yield @loc="50:9";
>> }
>> (%14 : Var<int>)void -> {
>> %15 : int = var.load %3 @loc="51:13";
>> %16 : int = var.load %14 @loc="51:20";
>> %17 : int = add %15 %16 @loc="51:13";
>> var.store %3 %17 @loc="51:13";
>> ...
>
> src/java.base/share/classes/java/lang/reflect/code/Op.java line 268:
>
>> 266: * {@return the originating source location of this operation, otherwise {@code null} if not specified}
>> 267: */
>> 268: public final Location location() {
>
> Should this be returning `Optional`? Or, do we want to have a "no position" (like javac has)?
I was pondering aliasing `null`, such as `static final Location NOLOCATION = null`.
-------------
PR Review Comment: https://git.openjdk.org/babylon/pull/54#discussion_r1570972522
More information about the babylon-dev
mailing list