Skip to content

Commit

Permalink
[javac] Have a convertType method which returns the created Type. C…
Browse files Browse the repository at this point in the history
…allers are then responsible for adding it to the appropriate list.

Types are added as follows
 - Top level types: Added to compilation unit
 - Nested types: Added to parent type
 - Local types: Added as a LocalClassDeclarationStatement
 - Anonymous types: Added as the NewInstance.anonymousInnerClass

Fixes this error:
RestoreVariableScoping expects the inline anonymous type to be in the `NewInstance.getAnonymousInnerClass`. Because `convertAndAddType` always returned null and instead added the type to the `CompilationUnit`, the pass could not find the relevant scope and throws NPE.

Relevant: 47a8809
PiperOrigin-RevId: 693478297
  • Loading branch information
Googler authored and copybara-github committed Nov 5, 2024
1 parent c671610 commit 9683a28
Showing 1 changed file with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.google.j2cl.transpiler.ast.LabelReference;
import com.google.j2cl.transpiler.ast.LabeledStatement;
import com.google.j2cl.transpiler.ast.Literal;
import com.google.j2cl.transpiler.ast.LocalClassDeclarationStatement;
import com.google.j2cl.transpiler.ast.Method;
import com.google.j2cl.transpiler.ast.MethodCall;
import com.google.j2cl.transpiler.ast.MethodDescriptor;
Expand Down Expand Up @@ -148,7 +149,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import javax.annotation.Nullable;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
Expand All @@ -168,6 +168,22 @@ private CompilationUnitBuilder(JavaEnvironment environment) {
this.environment = environment;
}

/**
* Constructs a type and maintains the type stack.
*
* @return The created {@link Type}.
*/
private Type convertType(
ClassSymbol typeElement, List<JCTree> bodyDeclarations, JCTree sourcePositionNode) {
Type type = createType(typeElement, sourcePositionNode);
return processEnclosedBy(
type,
() -> {
convertTypeBody(type, bodyDeclarations);
return type;
});
}

@Nullable
private Type createType(ClassSymbol typeElement, JCTree sourcePositionNode) {
if (typeElement == null) {
Expand All @@ -182,35 +198,12 @@ private Type createType(ClassSymbol typeElement, JCTree sourcePositionNode) {
typeDeclaration);
}

/**
* Constructs a type, maintains the type stack and let's the caller to do additional work by
* supplying a {@code typeProcessor}.
*
* @return {T} the value returned by {@code typeProcessor}
*/
private <T> T convertAndAddType(
ClassSymbol typeElement,
List<JCTree> bodyDeclarations,
JCTree sourcePositionNode,
Function<Type, T> typeProcessor) {
Type type = createType(typeElement, sourcePositionNode);
getCurrentCompilationUnit().addType(type);
return processEnclosedBy(
type,
() -> {
;
convertTypeBody(type, bodyDeclarations);
return typeProcessor.apply(type);
});
}

private Type convertClassDeclaration(JCClassDecl classDecl) {
return convertClassDeclaration(classDecl, classDecl);
}

private Type convertClassDeclaration(JCClassDecl classDecl, JCTree sourcePositionNode) {
return convertAndAddType(
classDecl.sym, classDecl.getMembers(), sourcePositionNode, type -> null);
return convertType(classDecl.sym, classDecl.getMembers(), sourcePositionNode);
}

private void convertTypeBody(Type type, List<JCTree> bodyDeclarations) {
Expand Down Expand Up @@ -245,7 +238,7 @@ private void convertTypeBody(Type type, List<JCTree> bodyDeclarations) {
} else if (bodyDeclaration instanceof JCClassDecl) {
// Nested class
JCClassDecl nestedTypeDeclaration = (JCClassDecl) bodyDeclaration;
convertClassDeclaration(nestedTypeDeclaration);
type.addType(convertClassDeclaration(nestedTypeDeclaration));
} else {
throw internalCompilerError(
"Unimplemented translation for BodyDeclaration type: %s.",
Expand Down Expand Up @@ -579,8 +572,8 @@ private Statement convertStatement(JCStatement jcStatement) {
case BREAK:
return convertBreak((JCBreak) jcStatement);
case CLASS:
convertClassDeclaration((JCClassDecl) jcStatement);
return null;
return new LocalClassDeclarationStatement(
convertClassDeclaration((JCClassDecl) jcStatement), getSourcePosition(jcStatement));
case CONTINUE:
return convertContinue((JCContinue) jcStatement);
case DO_WHILE_LOOP:
Expand Down Expand Up @@ -1296,7 +1289,7 @@ private CompilationUnit build(JCCompilationUnit javacUnit) {
javacUnit.getPackageName() == null ? "" : javacUnit.getPackageName().toString()));
for (JCTree tree : javacUnit.getTypeDecls()) {
if (tree instanceof JCClassDecl) {
convertClassDeclaration((JCClassDecl) tree);
getCurrentCompilationUnit().addType(convertClassDeclaration((JCClassDecl) tree));
}
}
return getCurrentCompilationUnit();
Expand Down

0 comments on commit 9683a28

Please sign in to comment.