Skip to content

Commit

Permalink
Type mapping revisions.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Nov 1, 2023
1 parent 93a64c1 commit 88ecfb2
Show file tree
Hide file tree
Showing 8 changed files with 1,345 additions and 1,785 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/openrewrite/kotlin/KotlinParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
// PSI based parser
SourceFile kcuPsi = null;
// TODO replace JavaTypeCache.
KotlinTypeMapping typeMapping = new KotlinTypeMapping(new JavaTypeCache(), firSession, kotlinSource.getFirFile().getSymbol());
KotlinTypeMapping typeMapping = new KotlinTypeMapping(new JavaTypeCache(), firSession, kotlinSource.getFirFile());
PsiElementAssociations associations = new PsiElementAssociations(typeMapping, kotlinSource.getFirFile());
associations.initialize();
KotlinTreeParserVisitor psiParser = new KotlinTreeParserVisitor(kotlinSource, associations, styles, relativeTo, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,16 +275,14 @@ public J visitCallableReferenceExpression(KtCallableReferenceExpression expressi
if (reference != null && reference.getResolvedSymbol() instanceof FirNamedFunctionSymbol) {
methodReferenceType = psiElementAssociations.getTypeMapping().methodDeclarationType(
((FirNamedFunctionSymbol) reference.getResolvedSymbol()).getFir(),
TypeUtils.asFullyQualified(type(expression.getReceiverExpression())),
owner(expression)
expression.getReceiverExpression()
);
}
JavaType.Variable fieldReferenceType = null;
if (reference != null && reference.getResolvedSymbol() instanceof FirPropertySymbol) {
fieldReferenceType = psiElementAssociations.getTypeMapping().variableType(
(FirVariableSymbol<FirVariable>) reference.getResolvedSymbol(),
TypeUtils.asFullyQualified(type(expression.getReceiverExpression())),
owner(expression)
((FirPropertySymbol) reference.getResolvedSymbol()).getFir(),
expression.getReceiverExpression()
);
}
JRightPadded<Expression> receiver;
Expand Down Expand Up @@ -812,7 +810,7 @@ public J visitParameter(KtParameter parameter, ExecutionContext data) {
.withPrefix(prefix(parameter));
}

JavaType.Variable vt = variableType(parameter);
JavaType.Variable vt = variableType(parameter, owner(parameter));
J.Identifier name = createIdentifier(requireNonNull(parameter.getNameIdentifier()), vt);

if (parameter.getTypeReference() != null) {
Expand Down Expand Up @@ -2167,7 +2165,7 @@ public J visitDestructuringDeclaration(KtDestructuringDeclaration multiDeclarati
}
}

JavaType.Variable vt = variableType(entry);
JavaType.Variable vt = variableType(entry, owner(entry));

if (entry.getName() == null) {
throw new UnsupportedOperationException();
Expand Down Expand Up @@ -2197,7 +2195,7 @@ public J visitDestructuringDeclaration(KtDestructuringDeclaration multiDeclarati
vars.add(padRight(namedVariable, suffix(entry)));
}

JavaType.Variable vt = variableType(multiDeclaration);
JavaType.Variable vt = variableType(multiDeclaration, owner(multiDeclaration));
J.VariableDeclarations.NamedVariable emptyWithInitializer = new J.VariableDeclarations.NamedVariable(
randomId(),
Space.EMPTY,
Expand Down Expand Up @@ -2274,14 +2272,13 @@ public J visitDotQualifiedExpression(KtDotQualifiedExpression expression, Execut
return methodInvocation;
} else if (expression.getSelectorExpression() instanceof KtNameReferenceExpression) {
// Maybe need to type check before creating a field access.
JavaType.Variable vt = variableType(expression.getSelectorExpression());
return new J.FieldAccess(
randomId(),
prefix(expression),
Markers.EMPTY,
expression.getReceiverExpression().accept(this, data).withPrefix(Space.EMPTY),
padLeft(suffix(expression.getReceiverExpression()), createIdentifier(expression.getSelectorExpression(), vt)),
vt
padLeft(suffix(expression.getReceiverExpression()), (J.Identifier) expression.getSelectorExpression().accept(this, data)),
type(expression.getSelectorExpression())
);
} else {
throw new UnsupportedOperationException("Unsupported dot qualified selector: " + expression.getSelectorExpression().getClass());
Expand Down Expand Up @@ -2725,7 +2722,7 @@ public J visitProperty(KtProperty property, ExecutionContext data) {
maybeBeforeSemicolon = prefix(property.getLastChild());
}

JavaType.Variable vt = variableType(property);
JavaType.Variable vt = variableType(property, owner(property));
J.VariableDeclarations.NamedVariable namedVariable =
new J.VariableDeclarations.NamedVariable(
randomId(),
Expand Down Expand Up @@ -3187,62 +3184,23 @@ private JavaType type(@Nullable KtElement psi) {
return psiElementAssociations.type(psi, owner(psi));
}

@Nullable
private JavaType.Primitive primitiveType(PsiElement psi) {
FirElement firElement = psiElementAssociations.primary(psi);
if (firElement instanceof FirConstExpression) {
return psiElementAssociations.getTypeMapping().primitive((ConeClassLikeType) ((FirResolvedTypeRef) ((FirConstExpression<?>) firElement).getTypeRef()).getType());
}

if (firElement instanceof FirStringConcatenationCall) {
return JavaType.Primitive.String;
}

return null;
return psiElementAssociations.primitiveType(psi);
}

@Nullable
private JavaType.Variable variableType(PsiElement psi) {
if (psi instanceof KtDeclaration) {
FirBasedSymbol<?> basedSymbol = psiElementAssociations.symbol((KtDeclaration) psi);
if (basedSymbol instanceof FirVariableSymbol) {
return (JavaType.Variable) psiElementAssociations.getTypeMapping().type(basedSymbol.getFir(), owner(psi));
}
} else if (psi instanceof KtNameReferenceExpression) {
FirBasedSymbol<?> basedSymbol = psiElementAssociations.symbol((KtNameReferenceExpression) psi);
if (basedSymbol instanceof FirVariableSymbol) {
return (JavaType.Variable) psiElementAssociations.getTypeMapping().type(basedSymbol.getFir(), owner(psi));
}

}
return null;
private JavaType.Variable variableType(PsiElement psi, @Nullable FirElement parent) {
return psiElementAssociations.variableType(psi, parent);
}

@Nullable
private JavaType.Method methodDeclarationType(PsiElement psi) {
if (psi instanceof KtDeclaration) {
FirBasedSymbol<?> basedSymbol = psiElementAssociations.symbol((KtDeclaration) psi);
if (basedSymbol != null && basedSymbol.getFir() instanceof FirFunction) {
return psiElementAssociations.getTypeMapping().methodDeclarationType((FirFunction) basedSymbol.getFir(), null, psiElementAssociations.getFile().getSymbol());
}
}
return null;
return psiElementAssociations.methodDeclarationType(psi);
}

@Nullable
private JavaType.Method methodInvocationType(PsiElement psi) {
FirElement firElement = psiElementAssociations.component(psi);
if (firElement == null) {
// TODO analyze why this is required (example `MethodReferenceTest#noReceiver()`)
firElement = psiElementAssociations.component(psi.getParent());
}
if (firElement instanceof FirFunctionCall) {
return psiElementAssociations.getTypeMapping().methodInvocationType((FirFunctionCall) firElement, psiElementAssociations.getFile().getSymbol());
}
if (firElement instanceof FirResolvedNamedReference) {
return psiElementAssociations.getTypeMapping().methodDeclarationType(((FirFunction) ((FirResolvedNamedReference) firElement).getResolvedSymbol().getFir()), null, psiElementAssociations.getFile().getSymbol());
}
return null;
return psiElementAssociations.methodInvocationType(psi);
}

/*====================================================================
Expand Down Expand Up @@ -3274,12 +3232,12 @@ private J.Identifier createIdentifier(String name, Space prefix,
}

@Nullable
private FirBasedSymbol<?> owner(PsiElement element) {
private FirElement owner(PsiElement element) {
KtElement owner = ownerStack.peek() == element ? ownerStack.get(ownerStack.size() - 2) : ownerStack.peek();
if (owner instanceof KtDeclaration) {
return psiElementAssociations.symbol(((KtDeclaration) owner));
return psiElementAssociations.primary(owner);
} else if (owner instanceof KtFile) {
return ((FirFile) requireNonNull(psiElementAssociations.primary(owner))).getSymbol();
return psiElementAssociations.primary(owner);
}
return null;
}
Expand Down Expand Up @@ -3473,7 +3431,7 @@ private J.VariableDeclarations mapDestructuringDeclaration(KtDestructuringDeclar
name,
emptyList(),
null,
variableType(ktDestructuringDeclarationEntry)
variableType(ktDestructuringDeclarationEntry, owner(ktDestructuringDeclarationEntry))
);
variables.add(padRight(namedVariable, suffix(ktDestructuringDeclarationEntry)));
}
Expand Down
Loading

0 comments on commit 88ecfb2

Please sign in to comment.