Skip to content
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

Support storing the code that builds the code model #305

Open
wants to merge 2 commits into
base: code-reflection
Choose a base branch
from
Open
Changes from all 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 @@ -138,6 +138,7 @@ public static ReflectMethods instance(Context context) {
private final CodeReflectionSymbols crSyms;
private final boolean dumpIR;
private final boolean lineDebugInfo;
private final CodeModelStorageOption codeModelStorageOption;

// @@@ Separate out mutable state
private TreeMaker make;
Expand All @@ -154,6 +155,8 @@ protected ReflectMethods(Context context) {
lineDebugInfo =
options.isUnset(G_CUSTOM) ||
options.isSet(G_CUSTOM, "lines");
String cmso = options.get("codeModelStorageOption");
codeModelStorageOption = CodeModelStorageOption.valueOf(cmso != null ? cmso : CodeModelStorageOption.TEXT.name());
names = Names.instance(context);
syms = Symtab.instance(context);
types = Types.instance(context);
Expand Down Expand Up @@ -206,7 +209,7 @@ public void visitMethodDef(JCMethodDecl tree) {
log.note(MethodIrDump(tree.sym.enclClass(), tree.sym, funcOp.toText()));
}
// create a static method that returns the op
classOps.add(opMethodDecl(methodName(bodyScanner.symbolToErasedMethodRef(tree.sym)), funcOp));
classOps.add(opMethodDecl(methodName(bodyScanner.symbolToErasedMethodRef(tree.sym)), funcOp, codeModelStorageOption));
} catch (UnsupportedASTException ex) {
// whoops, some AST node inside the method body were not supported. Log it and move on.
log.note(ex.tree, MethodIrSkip(tree.sym.enclClass(), tree.sym, ex.tree.getTag().toString()));
Expand Down Expand Up @@ -254,7 +257,7 @@ public void visitLambda(JCLambda tree) {
log.note(QuotedIrDump(funcOp.toText()));
}
// create a static method that returns the FuncOp representing the lambda
JCMethodDecl opMethod = opMethodDecl(lambdaName(), funcOp);
JCMethodDecl opMethod = opMethodDecl(lambdaName(), funcOp, codeModelStorageOption);
classOps.add(opMethod);

switch (kind) {
Expand Down Expand Up @@ -315,7 +318,7 @@ public void visitReference(JCMemberReference tree) {
log.note(QuotedIrDump(funcOp.toText()));
}
// create a method that returns the FuncOp representing the lambda
JCMethodDecl opMethod = opMethodDecl(lambdaName(), funcOp);
JCMethodDecl opMethod = opMethodDecl(lambdaName(), funcOp, codeModelStorageOption);
classOps.add(opMethod);
tree.codeModel = opMethod.sym;
super.visitReference(tree);
Expand Down Expand Up @@ -388,19 +391,30 @@ Name methodName(MethodRef method) {
return names.fromChars(sigCh, 0, sigCh.length);
}

private JCMethodDecl opMethodDecl(Name methodName, CoreOp.FuncOp op) {
var mt = new MethodType(com.sun.tools.javac.util.List.nil(), crSyms.funcOpType,
com.sun.tools.javac.util.List.nil(), syms.methodClass);
var mn = names.fromString("op$").append(methodName);
var ms = new MethodSymbol(PUBLIC | STATIC | SYNTHETIC, mn, mt, currentClassSym);
currentClassSym.members().enter(ms);

var opFromStr = make.App(make.Ident(crSyms.opParserFromString),
com.sun.tools.javac.util.List.of(make.Literal(op.toText())));
var ret = make.Return(make.TypeCast(crSyms.funcOpType, opFromStr));
private enum CodeModelStorageOption {
TEXT,
CODE_BUILDER
}

var md = make.MethodDef(ms, make.Block(0, com.sun.tools.javac.util.List.of(ret)));
return md;
private JCMethodDecl opMethodDecl(Name methodName, CoreOp.FuncOp op, CodeModelStorageOption codeModelStorageOption) {
if (CodeModelStorageOption.TEXT.equals(codeModelStorageOption)) {
var mt = new MethodType(com.sun.tools.javac.util.List.nil(), crSyms.funcOpType,
com.sun.tools.javac.util.List.nil(), syms.methodClass);
var mn = names.fromString("op$").append(methodName);
var ms = new MethodSymbol(PUBLIC | STATIC | SYNTHETIC, mn, mt, currentClassSym);
currentClassSym.members().enter(ms);

var opFromStr = make.App(make.Ident(crSyms.opParserFromString),
com.sun.tools.javac.util.List.of(make.Literal(op.toText())));
var ret = make.Return(make.TypeCast(crSyms.funcOpType, opFromStr));

var md = make.MethodDef(ms, make.Block(0, com.sun.tools.javac.util.List.of(ret)));
return md;
} else if (CodeModelStorageOption.CODE_BUILDER.equals(codeModelStorageOption)) {
throw new IllegalStateException("code model storage option %s not supported for the moment".formatted(codeModelStorageOption));
} else {
throw new IllegalStateException("unknown code model storage option: " + codeModelStorageOption);
}
}

public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) {
Expand Down