Skip to content

Commit

Permalink
Initial push
Browse files Browse the repository at this point in the history
Todo: lift non-final static locals into fields
  • Loading branch information
mcimadamore committed Oct 24, 2023
1 parent 99632e4 commit d713427
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 95 deletions.
16 changes: 3 additions & 13 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,6 @@ public static EnumSet<Flag> asFlagSet(long flags) {
*/
public static final long NAME_FILLED = 1L<<52; //ParamSymbols only

/**
* Flag to mark a lazy static field
*/
public static final long LAZY= 1L<<49; // VarSymbols only (shared with LAMBDA_METHOD)

/**
* Flag to indicate the given ModuleSymbol is a system module.
*/
Expand Down Expand Up @@ -433,14 +428,14 @@ public static String toSource(long flags) {
RecordMethodFlags = AccessFlags | ABSTRACT | STATIC |
SYNCHRONIZED | FINAL | STRICTFP;
public static final long
ExtendedStandardFlags = (long)StandardFlags | DEFAULT | SEALED | NON_SEALED | LAZY,
ExtendedStandardFlags = (long)StandardFlags | DEFAULT | SEALED | NON_SEALED,
ExtendedMemberClassFlags = (long)MemberClassFlags | SEALED | NON_SEALED,
ExtendedMemberStaticClassFlags = (long) MemberStaticClassFlags | SEALED | NON_SEALED,
ExtendedClassFlags = (long)ClassFlags | SEALED | NON_SEALED,
ModifierFlags = ((long)StandardFlags & ~INTERFACE) | DEFAULT | SEALED | NON_SEALED,
InterfaceMethodMask = ABSTRACT | PRIVATE | STATIC | PUBLIC | STRICTFP | DEFAULT,
AnnotationTypeElementMask = ABSTRACT | PUBLIC,
LocalVarFlags = FINAL | PARAMETER,
LocalVarFlags = FINAL | PARAMETER | STATIC,
ReceiverParamFlags = PARAMETER;

public static Set<Modifier> asModifierSet(long flags) {
Expand Down Expand Up @@ -532,12 +527,7 @@ public enum Flag {
BAD_OVERRIDE(Flags.BAD_OVERRIDE),
SIGNATURE_POLYMORPHIC(Flags.SIGNATURE_POLYMORPHIC),
THROWS(Flags.THROWS),
LAZY(Flags.LAZY) {
@Override
public String toString() {
return "lazy-static";
}
},
LAMBDA_METHOD(Flags.LAMBDA_METHOD),
TYPE_TRANSLATED(Flags.TYPE_TRANSLATED),
MODULE(Flags.MODULE),
AUTOMATIC_MODULE(Flags.AUTOMATIC_MODULE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1215,10 +1215,10 @@ else if (sym.owner.kind != TYP)
mask = LocalVarFlags;
else if ((sym.owner.flags_field & INTERFACE) != 0) {
implicit = InterfaceVarFlags;
mask = InterfaceVarFlags | LAZY;
mask = InterfaceVarFlags;
}
else
mask = VarFlags | LAZY;
mask = VarFlags;
break;
case MTH:
if (sym.name == names.init) {
Expand Down Expand Up @@ -1308,9 +1308,6 @@ else if ((sym.owner.flags_field & INTERFACE) != 0) {
mask |= INTERFACE;
}
else {
if ((illegal & LAZY) != 0) {
illegal &= ~STATIC;
}
log.error(pos,
Errors.ModNotAllowedHere(asFlagSet(illegal)));
}
Expand Down
54 changes: 7 additions & 47 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public static Lower instance(Context context) {
private final PkgInfo pkginfoOpt;
private final boolean optimizeOuterThis;
private final boolean useMatchException;
private final boolean desugarLazyStatics;

@SuppressWarnings("this-escape")
protected Lower(Context context) {
Expand Down Expand Up @@ -134,7 +133,6 @@ protected Lower(Context context) {
Preview preview = Preview.instance(context);
useMatchException = Feature.PATTERN_SWITCH.allowedInSource(source) &&
(preview.isEnabled() || !preview.isPreview(Feature.PATTERN_SWITCH));
desugarLazyStatics = options.isSet("desugarLazyStatics");
}

/** The currently enclosing class.
Expand Down Expand Up @@ -1126,38 +1124,10 @@ MethodSymbol accessSymbol(Symbol sym, JCTree tree, JCTree enclOp,
return accessor;
}

MethodSymbol lazyStaticAccessSymbol(Symbol sym) {
// lookup symbol and create one if it doesn't exist
Symbol getter = sym.owner.members().findFirst(lazyStaticGetName(sym));
if (getter == null) {
getter = new MethodSymbol(
STATIC | SYNTHETIC | (sym.flags() & AccessFlags),
lazyStaticGetName(sym),
new MethodType(List.nil(), sym.type, List.nil(), syms.methodClass),
sym.owner);
sym.owner.members().enter(getter);
}
return (MethodSymbol)getter;
}

JCMethodDecl lazyStaticAccessDef(Symbol sym) {
MethodSymbol getter = lazyStaticAccessSymbol(sym);
Assert.checkNonNull(getter);
// create synthetic getter tree
JCMethodDecl initDef = make.MethodDef(getter, make.Block(0, List.of(make.Return(make.Ident(sym)))));
JCClassDecl currentDecl = classDef(currentClass);
currentDecl.defs = currentDecl.defs.prepend(initDef);
return initDef;
}

Name lazyStaticInitName(Symbol sym) {
return sym.name.append('$', names.fromString("init"));
}

Name lazyStaticGetName(Symbol sym) {
return sym.name.append('$', names.fromString("get"));
}

/** The qualifier to be used for accessing a symbol in an outer class.
* This is either C.sym or C.this.sym, depending on whether or not
* sym is static.
Expand Down Expand Up @@ -1289,16 +1259,6 @@ JCExpression access(Symbol sym, JCExpression tree, JCExpression enclOp, boolean
break;
case MTH: case VAR:
if (sym.owner.kind == TYP) {
boolean isLazyStatic = (sym.flags() & LAZY) != 0;
if (desugarLazyStatics && isLazyStatic &&
sym.owner != currentClass) {
Symbol access = lazyStaticAccessSymbol(sym);
JCExpression receiver = make.Select(
base != null ? base : make.QualIdent(access.owner),
access);
return make.App(receiver, List.nil());
}

// Access methods are required for
// - private members,
// - protected members in a superclass of an
Expand Down Expand Up @@ -3714,13 +3674,13 @@ public void visitVarDef(JCVariableDecl tree) {
new MethodSymbol((tree.mods.flags&STATIC) | BLOCK,
names.empty, null,
currentClass);
// handle lazy
if ((tree.sym.flags() & LAZY) != 0) {
splitInit(tree);
lazyStaticAccessDef(tree.sym);
}
}
if (tree.init != null) tree.init = translate(tree.init, tree.type);
// handle static locals
if (tree.sym.isStatic() && tree.sym.owner.kind == MTH) {
splitInit(tree);
} else {
if (tree.init != null) tree.init = translate(tree.init, tree.type);
}
result = tree;
currentMethodSym = oldMethodSym;
}
Expand All @@ -3735,7 +3695,7 @@ private MethodSymbol splitInit(JCVariableDecl tree) {
currentClass);
enterSynthetic(tree.pos(), initSym, currentClass.members());
// create synthetic init tree
JCExpression initExpr = tree.init;
JCExpression initExpr = translate(tree.init, tree.type);
JCMethodDecl initDef = make.MethodDef(initSym, make.Block(0, List.of(make.Return(initExpr))));
JCClassDecl currentDecl = classDef(currentClass);
currentDecl.defs = currentDecl.defs.prepend(initDef);
Expand Down
13 changes: 5 additions & 8 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public class Gen extends JCTree.Visitor {

/** Format of stackmap tables to be generated. */
private final Code.StackMapFormat stackMap;
private final boolean desugarLazyStatics;

/** A type that serves as the expected type for all method expressions.
*/
Expand Down Expand Up @@ -133,7 +132,6 @@ protected Gen(Context context) {
this.stackMap = StackMapFormat.JSR202;
annotate = Annotate.instance(context);
qualifiedSymbolCache = new HashMap<>();
desugarLazyStatics = options.isSet("desugarLazyStatics");
}

/** Switches
Expand Down Expand Up @@ -2321,7 +2319,7 @@ public void visitIndexed(JCArrayAccess tree) {

public void visitIdent(JCIdent tree) {
Symbol sym = tree.sym;
if (desugarLazyStatics && isLazyStatic(sym)) {
if (isStaticLocal(sym)) {
sym = ((VarSymbol)sym).lazyConstValue();
}
if (tree.name == names._this || tree.name == names._super) {
Expand Down Expand Up @@ -2361,9 +2359,6 @@ private boolean nonVirtualForPrivateAccess(Symbol sym) {

public void visitSelect(JCFieldAccess tree) {
Symbol sym = tree.sym;
if (desugarLazyStatics && isLazyStatic(sym)) {
sym = ((VarSymbol)sym).lazyConstValue();
}

if (tree.name == names._class) {
code.emitLdc((LoadableConstant)checkDimension(tree.pos(), tree.selected.type));
Expand Down Expand Up @@ -2432,8 +2427,10 @@ public boolean isInvokeDynamic(Symbol sym) {
return sym.kind == MTH && ((MethodSymbol)sym).isDynamic();
}

public boolean isLazyStatic(Symbol sym) {
return (sym.flags() & LAZY) != 0;
public boolean isStaticLocal(Symbol sym) {
return sym.kind == VAR &&
sym.isStatic() &&
sym.owner.kind == MTH;
}

public void visitLiteral(JCLiteral tree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2818,7 +2818,8 @@ List<JCStatement> blockStatement() {
case ASSERT:
return List.of(parseSimpleStatement());
case MONKEYS_AT:
case FINAL: {
case FINAL:
case STATIC: {
dc = token.docComment();
JCModifiers mods = modifiersOpt();
if (isDeclaration()) {
Expand Down Expand Up @@ -3497,27 +3498,11 @@ protected JCModifiers modifiersOpt(JCModifiers partial) {
flag = Flags.SEALED;
break;
}
if (isLazyStaticIdentifier()) {
// @@@: make sure this is a field
checkSourceLevel(Feature.LAZY_STATICS);
flag = Flags.LAZY | Flags.STATIC | Flags.FINAL;
nextToken();
nextToken();
break;
}
break loop;
}
default: break loop;
}
if ((flags & flag) != 0) {
if ((flag == Flags.STATIC && (flags & Flags.LAZY) != 0) ||
((flag & Flags.LAZY) != 0 && (flags & (Flags.STATIC | Flags.LAZY)) == Flags.STATIC)) {
log.error(DiagnosticFlag.SYNTAX, token.pos,
Errors.IllegalCombinationOfModifiers(Set.of(Flags.Flag.LAZY), Set.of(Flags.Flag.STATIC)));
} else {
log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier);
}
}
if ((flags & flag) != 0) log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier);
lastPos = token.pos;
nextToken();
if (flag == Flags.ANNOTATION) {
Expand Down Expand Up @@ -4719,9 +4704,8 @@ private List<JCTree> constructorOrMethodOrFieldDeclaration(JCModifiers mods, Nam
// Field
if (!isVoid && typarams.isEmpty()) {
if (!isRecord || (isRecord && (mods.flags & Flags.STATIC) != 0)) {
boolean isLazy = (mods.flags & Flags.LAZY) != 0;
List<JCTree> defs =
variableDeclaratorsRest(pos, mods, type, name, isInterface || isLazy, dc,
variableDeclaratorsRest(pos, mods, type, name, isInterface, dc,
new ListBuffer<JCTree>(), false).toList();
accept(SEMI);
storeEnd(defs.last(), S.prevToken().endPos);
Expand Down Expand Up @@ -4859,7 +4843,7 @@ protected boolean isSealedClassStart(boolean local) {
}

protected boolean isLazyStaticIdentifier() {
return (token.name() == names.lazy && peekToken(0, TokenKind.SUB, STATIC));
return false;
}

private boolean allowedAfterSealedOrNonSealed(Token next, boolean local, boolean currentIsNonSealed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ static <U> U m(String param) {

// can't be explicitly static
for (String s : List.of("static record RR() { }", "static interface I {}", "static enum E { A }")) {
assertFail("compiler.err.illegal.start.of.expr", "class R { void m() { #S } }".replaceFirst("#S", s));
assertOK("class R { void m() { #S } }".replaceFirst("#S", s));
}

// but static fields can be accessed
Expand Down

0 comments on commit d713427

Please sign in to comment.