Search in sources :

Example 1 with FunctionType

use of org.finos.legend.pure.m3.navigation.function.FunctionType in project legend-pure by finos.

the class TypeInference method resolveTypeParameters.

private static void resolveTypeParameters(GenericType template, GenericType fromInstance, MutableMap<String, CoreInstance> rtypes, MutableMap<String, CoreInstance> rmultiplicities, MutableMap<String, CoreInstance> types, MutableMap<String, CoreInstance> multiplicities, ProcessorSupport processorSupport) {
    GenericType g = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericType(fromInstance, false, processorSupport), types, multiplicities, processorSupport);
    Type rawType = template._rawType();
    if (rawType != null) {
        if (org.finos.legend.pure.m3.navigation.type.Type.subTypeOf(rawType, processorSupport.package_getByUserPath(M3Paths.Function), processorSupport)) {
            if (template._typeArguments().notEmpty() && g._typeArguments().notEmpty()) {
                CoreInstance fTypR = template._typeArguments().getFirst()._rawType();
                CoreInstance sTypeR = g._typeArguments().getFirst()._rawType();
                if (fTypR instanceof FunctionType && sTypeR instanceof FunctionType) {
                    FunctionType fType = (FunctionType) fTypR;
                    FunctionType sType = (FunctionType) sTypeR;
                    resolveTypeParameters(fType._returnType(), sType._returnType(), rtypes, rmultiplicities, types, multiplicities, processorSupport);
                    resolveMultiplicityParameters(fType._returnMultiplicity(), sType._returnMultiplicity(), rmultiplicities, multiplicities);
                    fType._parameters().zip(sType._parameters()).forEach(pair -> {
                        resolveTypeParameters(pair.getOne()._genericType(), pair.getTwo()._genericType(), rtypes, rmultiplicities, types, multiplicities, processorSupport);
                        resolveMultiplicityParameters(pair.getOne()._multiplicity(), pair.getTwo()._multiplicity(), rmultiplicities, multiplicities);
                    });
                }
            }
        }
    } else {
        rtypes.put(template._typeParameter()._name(), g);
    }
    template._typeArguments().zip(g._typeArguments()).forEach(pair -> resolveTypeParameters(pair.getOne(), pair.getTwo(), rtypes, rmultiplicities, types, multiplicities, processorSupport));
    template._multiplicityArguments().zip(g._multiplicityArguments()).forEach(pair -> resolveMultiplicityParameters(pair.getOne(), pair.getTwo(), rmultiplicities, multiplicities));
}
Also used : GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 2 with FunctionType

use of org.finos.legend.pure.m3.navigation.function.FunctionType in project legend-pure by finos.

the class TypeInference method processParamTypesOfLambdaUsedAsAFunctionExpressionParamValue.

public static boolean processParamTypesOfLambdaUsedAsAFunctionExpressionParamValue(ValueSpecification instanceValueContainer, LambdaFunction<?> lambdaFunction, VariableExpression templateToMatchLambdaTo, Matcher matcher, ProcessorState state, ModelRepository repository, ProcessorSupport processorSupport) throws PureCompilationException {
    GenericType templateGenericType = templateToMatchLambdaTo._genericType();
    Type templateFunctionType = templateGenericType._typeArguments().notEmpty() ? (Type) ImportStub.withImportStubByPass(templateGenericType._typeArguments().getFirst()._rawTypeCoreInstance(), processorSupport) : null;
    FunctionType lambdaFunctionType = (FunctionType) ImportStub.withImportStubByPass(lambdaFunction._classifierGenericType()._typeArguments().getFirst()._rawTypeCoreInstance(), processorSupport);
    if (org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(templateGenericType) && org.finos.legend.pure.m3.navigation.type.Type.subTypeOf(ImportStub.withImportStubByPass(templateGenericType._rawTypeCoreInstance(), processorSupport), processorSupport.package_getByUserPath(M3Paths.Function), processorSupport)) {
        ListIterable<? extends VariableExpression> parameters = ListHelper.wrapListIterable(lambdaFunctionType._parameters());
        for (int j = 0; j < parameters.size(); j++) {
            VariableExpression param = parameters.get(j);
            if (param._genericType() == null) {
                if (org.finos.legend.pure.m3.navigation.type.Type.isBottomType(templateFunctionType, processorSupport) || org.finos.legend.pure.m3.navigation.type.Type.isTopType(templateFunctionType, processorSupport)) {
                    throw new PureCompilationException(lambdaFunction.getSourceInformation(), "Can't infer the parameters' types for the lambda. Please specify it in the signature.");
                }
                VariableExpression templateParam = ListHelper.wrapListIterable(((FunctionType) Objects.requireNonNull(templateFunctionType))._parameters()).get(j);
                CoreInstance genericType = org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(templateParam._genericType(), state.getTypeInferenceContext().getTypeParameterToGenericType(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity(), processorSupport);
                if (state.getTypeInferenceContext().isTypeParameterResolved(genericType)) {
                    genericType = state.getTypeInferenceContext().resolve(genericType);
                } else {
                    return true;
                }
                CoreInstance multiplicity = org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.makeMultiplicityAsConcreteAsPossible(templateParam._multiplicity(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity());
                param._genericType((GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericTypeAsInferredGenericType(genericType, param.getSourceInformation(), processorSupport));
                param._multiplicity((Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.copyMultiplicity(multiplicity, param.getSourceInformation(), processorSupport));
            }
        }
        state.pushVariableContext();
        FunctionDefinitionProcessor.process(lambdaFunction, state, matcher, repository);
        LambdaFunctionProcessor.process(lambdaFunction, state, matcher, repository);
        state.popVariableContext();
    } else {
        throw new PureCompilationException(lambdaFunction.getSourceInformation(), "Can't infer the parameters' types for the lambda. Please specify it in the signature.");
    }
    instanceValueContainer._genericTypeRemove();
    InstanceValueProcessor.updateInstanceValue(instanceValueContainer, processorSupport);
    return false;
}
Also used : GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 3 with FunctionType

use of org.finos.legend.pure.m3.navigation.function.FunctionType in project legend-pure by finos.

the class TypeInference method storeInferredTypeParametersInFunctionExpression.

public static void storeInferredTypeParametersInFunctionExpression(FunctionExpression functionExpression, ProcessorState state, ProcessorSupport processorSupport, Function<?> foundFunction) throws PureCompilationException {
    // Store the inferred params in the FunctionExpression
    if (!(foundFunction instanceof QualifiedProperty)) {
        TypeInferenceContext typeInferenceContext = state.getTypeInferenceContext();
        FunctionType functionType = (FunctionType) processorSupport.function_getFunctionType(foundFunction);
        functionType._typeParameters().forEach(typeParameter -> {
            CoreInstance value = typeInferenceContext.getTypeParameterValue(typeParameter._name());
            if (value != null) {
                functionExpression._resolvedTypeParametersAdd((GenericType) value);
            } else if (typeInferenceContext.getParent() == null) {
                StringBuilder builder = new StringBuilder("The type parameter ").append(typeParameter._name()).append(" was not resolved (").append(foundFunction._functionName()).append(" / ");
                org.finos.legend.pure.m3.navigation.function.FunctionType.print(builder, functionType, processorSupport).append(")!");
                throw new PureCompilationException(functionExpression.getSourceInformation(), builder.toString());
            }
        });
        functionType._multiplicityParameters().forEach(multiplicityParameter -> {
            String parameterName = multiplicityParameter._valuesCoreInstance().getFirst().getName();
            CoreInstance value = typeInferenceContext.getMultiplicityParameterValue(parameterName);
            if (value != null) {
                functionExpression._resolvedMultiplicityParametersAdd((Multiplicity) value);
            } else {
                throw new PureCompilationException(functionExpression.getSourceInformation(), "The multiplicity parameter " + parameterName + " was not resolved!");
            }
        });
    }
}
Also used : FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 4 with FunctionType

use of org.finos.legend.pure.m3.navigation.function.FunctionType in project legend-pure by finos.

the class AssociationProcessor method updateClassifierGenericTypeForQualifiedPropertiesThisVarExprParams.

private static void updateClassifierGenericTypeForQualifiedPropertiesThisVarExprParams(Association association, QualifiedProperty qualifiedProperty, final Class qualifiedPropertyReturnType, ListIterable<? extends Property> assnProperties, final Context context, final ProcessorSupport processorSupport) {
    validateQualifiedPropertyLeftSideOfFilterByPropertyName(association, qualifiedProperty, qualifiedPropertyReturnType, assnProperties, context, processorSupport);
    Property leftSideOfFilterProperty = getQualifiedPropertiesFilterLeftSideParam(qualifiedPropertyReturnType, assnProperties, processorSupport);
    Property leftSideOfFilterOtherProperty = assnProperties.detect(Predicates.notEqual(leftSideOfFilterProperty));
    GenericType leftSideOfFilterOtherPropertyGenericType = leftSideOfFilterOtherProperty._genericType();
    FunctionType functionType = (FunctionType) qualifiedProperty._classifierGenericType()._typeArguments().toList().getFirst()._rawTypeCoreInstance();
    Iterable<? extends VariableExpression> functionTypeParams = functionType._parameters();
    for (VariableExpression functionTypeParam : functionTypeParams) {
        if (functionTypeParam != null && "this".equals(functionTypeParam._name())) {
            GenericType genericTypeCopy = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericType(leftSideOfFilterOtherPropertyGenericType, processorSupport);
            functionTypeParam._genericType(genericTypeCopy);
            context.update(functionTypeParam);
            if (functionTypeParam.hasBeenValidated()) {
                functionTypeParam.markNotValidated();
                context.update(functionTypeParam);
            }
        }
    }
}
Also used : GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) AbstractProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)

Example 5 with FunctionType

use of org.finos.legend.pure.m3.navigation.function.FunctionType in project legend-pure by finos.

the class GenericTypeTraceability method addTraceForFunctionDefinition.

public static void addTraceForFunctionDefinition(FunctionDefinition<?> functionDefinition, ModelRepository repository, ProcessorSupport processorSupport) {
    FunctionType functionType = (FunctionType) processorSupport.function_getFunctionType(functionDefinition);
    if (functionType._functionCoreInstance().isEmpty()) {
        functionType._functionAdd(functionDefinition);
    }
    addTraceForFunctionType(functionType, repository, processorSupport);
}
Also used : FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)

Aggregations

FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)67 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)53 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)46 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)36 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)33 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)27 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)24 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)24 RichIterable (org.eclipse.collections.api.RichIterable)17 QualifiedProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)16 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)16 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)16 ListIterable (org.eclipse.collections.api.list.ListIterable)14 AbstractProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty)14 InstanceValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue)14 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)13 MutableList (org.eclipse.collections.api.list.MutableList)12 Property (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property)12 FastList (org.eclipse.collections.impl.list.mutable.FastList)10 Class (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)10