Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
m0rkeulv committed Nov 4, 2023
1 parent 11cd342 commit 644ed56
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ private static void inheritTypeParametersFromArgument(ResultHolder parameterType
HaxeGenericResolver inherit = findTypeParametersToInherit(parameterType.getType(), argumentType.getType(), resolver, typeParamMap);
resolver.addAll(inherit);
// parameter is a typeParameter type, we can just add it to resolver
if (parameterType.getClassType().isFromTypeParameter()) {
if (parameterType.getClassType().isTypeParameter()) {
String className = parameterType.getClassType().getClassName();
resolver.add(className, argumentType, ResolveSource.ARGUMENT_TYPE);
typeParamMap.put(className, argumentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static Map<String, ResultHolder> createTypeParameterConstraintMap(List<Ha

public static boolean containsTypeParameter(@NotNull ResultHolder parameterType, @NotNull Map<String, ResultHolder> typeParamMap) {
if (parameterType.getClassType() == null) return false;
if (parameterType.getClassType().isFromTypeParameter()) return true;
if (parameterType.getClassType().isTypeParameter()) return true;


ResultHolder[] specifics = parameterType.getClassType().getSpecifics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ else if (implementOrExtendSameClass) {
HaxeResolveResult result = null;
HaxeGenericSpecialization specialization = getSpecialization();
if (resolvedType.getClassType() != null && !resolvedType.isUnknown()) {
if (!resolvedType.getClassType().isFromTypeParameter()) {
if (!resolvedType.getClassType().isTypeParameter()) {
result = resolvedType.getClassType().asResolveResult();
if (specialization != null) {
result.specializeByParent(specialization.toGenericResolver(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public List<SpecificTypeReference> getImplicitCastToTypesList(SpecificHaxeClassR
for (HaxeMethodModel methodModel : methodsWithMetadata) {
if (castMethodAcceptsSource(sourceType, methodModel)) {
SpecificTypeReference returnType = getReturnType(methodModel);
if (returnType.isFromTypeParameter()) {
if (returnType.isTypeParameter()) {
ResultHolder resolve = sourceType.getGenericResolver().resolve(((SpecificHaxeClassReference)returnType).getClassName());
if (resolve!= null && !resolve.isUnknown()) {
returnType = resolve.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ static private ResultHolder _handle(final PsiElement element,
return handle.getType().createHolder();
}
}
if (type.isFromTypeParameter()) {
if (type.isTypeParameter()) {
if (iterable.getExpression() instanceof HaxeReference reference) {
HaxeResolveResult result = reference.resolveHaxeClass();
HaxeGenericResolver classResolver = result.getGenericResolver();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private int ResolverPrioritySort(ResolverEntry entry, ResolverEntry entry1) {
@Nullable
public ResultHolder resolveReturnType(SpecificHaxeClassReference reference) {
if (null == reference ) return null;
if (reference.isFromTypeParameter()) {
if (reference.isTypeParameter()) {
String className = reference.getHaxeClassReference().name;
List<ResolverEntry> resolveValues = resolvers.stream().filter(entry -> entry.name().equals(className)).toList();
List<ResolverEntry> constraints = constaints.stream().filter(entry -> entry.name().equals(className)).toList();
Expand All @@ -182,7 +182,7 @@ public ResultHolder resolveReturnType(SpecificHaxeClassReference reference) {
}
}
// todo recursion guard
if (!reference.isFromTypeParameter() && needResolve(reference)) {
if (!reference.isTypeParameter() && needResolve(reference)) {
return HaxeTypeResolver.resolveParameterizedType(reference.createHolder(), this, true);
}
return reference.createHolder();
Expand All @@ -202,7 +202,7 @@ private Optional<ResolverEntry> findAsignToType() {
@Nullable
public ResultHolder resolveReturnType(ResultHolder resultHolder) {
if (null == resultHolder ) return null;
if (resultHolder.getType().isFromTypeParameter()) {
if (resultHolder.getType().isTypeParameter()) {
String className = resultHolder.getType().context.getText();
List<ResolverEntry> list = resolvers.stream().filter(entry -> entry.name().equals(className)).sorted(this::ResolverPrioritySort).toList();
if (list.isEmpty()) {
Expand All @@ -215,7 +215,7 @@ public ResultHolder resolveReturnType(ResultHolder resultHolder) {
return list.get(0).type();
}
}
if (!resultHolder.getType().isFromTypeParameter()) {
if (!resultHolder.getType().isTypeParameter()) {
Optional<ResolverEntry> assign = findAsignToType();
if (assign.isPresent()) { // if we got expected return type we want to pass along expected typeParameter values when resolving
SpecificHaxeClassReference expectedType = assign.get().type().getClassType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package com.intellij.plugins.haxe.model.type;

import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.model.*;
import com.intellij.plugins.haxe.util.HaxeResolveUtil;
Expand Down Expand Up @@ -213,7 +212,7 @@ static public boolean canAssignToFrom(
return toReference.isSameType(fromReference);
}
// if value is type parameter and we have reached this point without resolving its type we just accept it as it would be the same as unknown
if (to.isFromTypeParameter()) return true;
if (to.isTypeParameter()) return true;


return false;
Expand Down Expand Up @@ -439,7 +438,7 @@ static private boolean canAssignToFromType(
for (SpecificHaxeClassReference compatibleType : compatibleTypes) {
if (canAssignToFromSpecificType(to, compatibleType)) return true;
}
if (to.isFromTypeParameter()) {
if (to.isTypeParameter()) {
// if we don't know the type and don't have any constraints for Type parameters we just accept it for now
// to avoid wrong error annotations
return true;
Expand All @@ -450,7 +449,7 @@ static private boolean canAssignToFromType(
}

private static boolean containsAllMembers(SpecificHaxeClassReference to, SpecificHaxeClassReference from) {
if (to.isFromTypeParameter() || from.isFromTypeParameter() ) return false; // unable to evaluate members when type is not resolved
if (to.isTypeParameter() || from.isTypeParameter() ) return false; // unable to evaluate members when type is not resolved

// if one of the types is a Class<T> its was probably wrapped so we unwrap to T
if (to.isClass() && to.getSpecifics().length == 1) to = to.getSpecifics()[0].getClassType();
Expand Down Expand Up @@ -622,7 +621,7 @@ else if (from.getSpecifics().length == 0) {
}
}
// if not working with type parameters check inheritance
if (!to.isFromTypeParameter() && !from.isFromTypeParameter()) {
if (!to.isTypeParameter() && !from.isTypeParameter()) {
if (to.getHaxeClass() != null && to.getHaxeClass().isInterface()) {
Set<SpecificHaxeClassReference> fromInferTypes = from.getInferTypes();
for (SpecificHaxeClassReference fromInterface : fromInferTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ static public ResultHolder resolveParameterizedType(@NotNull ResultHolder result
static public ResultHolder resolveParameterizedType(@NotNull ResultHolder result, HaxeGenericResolver resolver, boolean returnType) {
SpecificTypeReference typeReference = result.getType();
if (resolver != null) {
if (typeReference instanceof SpecificHaxeClassReference haxeClassReference && typeReference.isFromTypeParameter()) {
if (typeReference instanceof SpecificHaxeClassReference haxeClassReference && typeReference.isTypeParameter()) {
String className = haxeClassReference.getClassName();
ResultHolder resolved = returnType ? resolver.resolveReturnType(haxeClassReference) : resolver.resolve(className);
if (null != resolved) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ public static SpecificHaxeClassReference propagateGenericsToType(@Nullable Speci
if (type == null) return null;
if (genericResolver == null || genericResolver.isEmpty()) return type;

if (type.isFromTypeParameter()) {
if (type.isTypeParameter()) {
String typeVariableName = type.getHaxeClassReference().name;
ResultHolder possibleValue = isReturnType ? genericResolver.resolveReturnType(type) : genericResolver.resolve(typeVariableName);
if (possibleValue != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.intellij.plugins.haxe.model.HaxeProjectModel;
import com.intellij.plugins.haxe.util.HaxeProjectUtil;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import lombok.CustomLog;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -309,13 +310,30 @@ final public boolean isAbstractType() {
}
return false;
}
final public boolean isFromTypeParameter() {
/**
* In some cases we can not resolve typeParameters (ex. class level typeParameters used in class members)
* We currently work around this by creating synthetic types, this method will return true if the type is
* one of these synthetic types.
*/
final public boolean isTypeParameter() {
if (this instanceof SpecificHaxeClassReference specificHaxeClassReference) {
return specificHaxeClassReference.getHaxeClassReference().isTypeParameter();
}
return false;
}

/**
* checks if type reference is from a typeParameter (ex. `Array<TypeParameter>`)
* or if its a normal reference (ex `var x = String;` or `MyType.staticMethod()`)
*
* @return is class reference from typeParameter.
*/
final public boolean isReferenceFromTypeParameter() {
//TODO cache ?
HaxeTypeParam type = PsiTreeUtil.getParentOfType(this.context, HaxeTypeParam.class);
return type != null;
}

public boolean isAnonymousType() {
if (this instanceof SpecificHaxeClassReference specificHaxeClassReference) {
if(specificHaxeClassReference.getHaxeClassReference().getHaxeClass() instanceof HaxeTypedefDeclaration typedefDeclaration) {
Expand Down Expand Up @@ -495,7 +513,7 @@ public static SpecificTypeReference propagateGenericsToType(@Nullable SpecificTy
if (type == null) return null;
if (genericResolver == null) return type;

if (type.isFromTypeParameter() && type instanceof SpecificHaxeClassReference classReference) {
if (type.isTypeParameter() && type instanceof SpecificHaxeClassReference classReference) {
String typeVariableName = classReference.getHaxeClassReference().name;
ResultHolder possibleValue = isReturnType ? genericResolver.resolveReturnType(classReference) : genericResolver.resolve(typeVariableName);
if (possibleValue != null) {
Expand All @@ -513,7 +531,7 @@ public static SpecificTypeReference propagateGenericsToType(@Nullable SpecificTy
//final SpecificTypeReference typeReference = propagateGenericsToType(specific.getClassType(), genericResolver);
if (specific.getClassType() != null) {
SpecificTypeReference typeReference = null;
if (!specific.getClassType().isFromTypeParameter()) {
if (!specific.getClassType().isTypeParameter()) {
typeReference = propagateGenericsToType(specific.getClassType(), specific.getClassType().getGenericResolver(), isReturnType);
}else {
if (isReturnType) {
Expand Down

0 comments on commit 644ed56

Please sign in to comment.