Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
Expand Up @@ -116,36 +116,44 @@ public FuncOp body(Consumer<Block.Builder> c) {
* The externalized attribute modelling the function name
*/
static final String ATTRIBUTE_FUNC_NAME = NAME + ".name";
static final String ATTRIBUTE_FUNC_MREF = NAME + ".mref";

final String funcName;
final Body body;
final MethodRef mref;

FuncOp(ExternalizedOp def) {
if (!def.operands().isEmpty()) {
throw new IllegalStateException("Bad op " + def.name());
}

String funcName = def.extractAttributeValue(ATTRIBUTE_FUNC_NAME, true,
MethodRef mref = def.extractAttributeValue(ATTRIBUTE_FUNC_MREF, false,
v -> switch (v) {
case String s -> s;
case null, default -> throw new UnsupportedOperationException("Unsupported func name value:" + v);
case MethodRef r -> r;
case null, default -> {
String funcName = def.extractAttributeValue(ATTRIBUTE_FUNC_NAME, true,
u -> switch (u) {
case String s -> s;
case null, default -> throw new UnsupportedOperationException("Unsupported func name value:" + u);
});
yield MethodRef.method(null, funcName, def.bodyDefinitions().get(0).bodyType());
}
});

this(funcName, def.bodyDefinitions().get(0));
this(mref, def.bodyDefinitions().get(0));
}

FuncOp(FuncOp that, CodeContext cc, CodeTransformer ot) {
super(that, cc);

this.funcName = that.funcName;
this.body = that.body.transform(cc, ot).build(this);
this.mref = that.mref;
}

FuncOp(FuncOp that, String funcName, CodeContext cc, CodeTransformer ot) {
super(that, cc);

this.funcName = funcName;
this.body = that.body.transform(cc, ot).build(this);
this.mref = MethodRef.method(that.mref.refType(), funcName, that.mref.type());
}

@Override
Expand Down Expand Up @@ -177,8 +185,15 @@ public FuncOp transform(String funcName, CodeTransformer ot) {
FuncOp(String funcName, Body.Builder bodyBuilder) {
super(List.of());

this.funcName = funcName;
this.body = bodyBuilder.build(this);
this.mref = MethodRef.method(null, funcName, body.bodyType());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like that you reused the method reference, but I am unsure about using null as for the reference owner type, arguably MethodRef should throw NPE for any given null arguments. Perhaps we can use void?

}

FuncOp(MethodRef mref, Body.Builder bodyBuilder) {
super(List.of());
Comment thread
mabbay marked this conversation as resolved.

this.body = bodyBuilder.build(this);
this.mref = mref;
}

@Override
Expand All @@ -188,7 +203,13 @@ public List<Body> bodies() {

@Override
public Map<String, Object> externalize() {
return Map.of("", funcName);
Map<String, Object> m = new HashMap<>();
if (mref.refType() != null) { // mref.refType can be null e.g. a user built model providing the name only
m.put(ATTRIBUTE_FUNC_MREF, mref);
} else {
m.put("", mref.name());
}
return m;
}

@Override
Expand All @@ -200,7 +221,7 @@ public FunctionType invokableType() {
* {@return the function name}
*/
public String funcName() {
return funcName;
return mref.name();
}

@Override
Expand All @@ -219,6 +240,10 @@ public Block.Builder lower(Block.Builder b, CodeTransformer _ignore) {
public TypeElement resultType() {
return JavaType.VOID;
}

public Optional<MethodRef> mref() {
return mref.refType() == null ? Optional.empty() : Optional.of(mref);
}
}

/**
Expand Down Expand Up @@ -1518,6 +1543,17 @@ public static FuncOp func(String funcName, Body.Builder body) {
return new FuncOp(funcName, body);
}

/**
* Creates a function operation.
*
* @param mref the method reference the function operation models
* @param body the body builder defining the function body
* @return the function operation
*/
public static FuncOp func(MethodRef mref, Body.Builder body) {
return new FuncOp(mref, body);
}

/**
* Creates a function call operation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2491,7 +2491,8 @@ UnsupportedASTException unsupported(JCTree tree) {
CoreOp.FuncOp scanMethod(JCBlock body) {
scan(body, ReflectMethods.this.currentNode());
appendReturnOrUnreachable(body);
CoreOp.FuncOp func = CoreOp.func(name.toString(), stack.body);
MethodRef mref = symbolToMethodRef(((JCMethodDecl) tree).sym);
CoreOp.FuncOp func = CoreOp.func(mref, stack.body);
func.setLocation(generateLocation(tree, true));
return func;
}
Expand Down