Search in sources :

Example 1 with ProcessorState

use of org.finos.legend.pure.m3.compiler.postprocessing.ProcessorState 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 2 with ProcessorState

use of org.finos.legend.pure.m3.compiler.postprocessing.ProcessorState 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 3 with ProcessorState

use of org.finos.legend.pure.m3.compiler.postprocessing.ProcessorState in project legend-pure by finos.

the class TypeInference method potentiallyUpdateParentTypeParamForInstanceValueWithManyElements.

public static void potentiallyUpdateParentTypeParamForInstanceValueWithManyElements(InstanceValue instance, TypeInferenceContext typeInferenceContext, ProcessorState state, ProcessorSupport processorSupport) {
    MutableList<TypeInferenceContextState> set = typeInferenceContext.drop(instance._values().size());
    if (typeInferenceContext.getParent() != null) {
        TypeInferenceContextState nonInstanceSpecificState = set.get(0);
        RichIterable<TypeInferenceContextState> instanceStates = ListHelper.tail(set);
        // Accumulate changes so as not to modify during iteration
        MutableMap<GenericType, GenericType> toRegisterTypes = Maps.mutable.empty();
        for (String typeParam : nonInstanceSpecificState.getTypeParameters()) {
            CoreInstance possibleParentTypeParam = nonInstanceSpecificState.getTypeParameterValue(typeParam);
            if (!org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(possibleParentTypeParam)) {
                MutableList<CoreInstance> allGenericTypes = Lists.mutable.empty();
                for (TypeInferenceContextState v : instanceStates) {
                    allGenericTypes.add(v.getTypeParameterValue(typeParam));
                }
                CoreInstance res = org.finos.legend.pure.m3.navigation.generictype.GenericType.findBestCommonGenericType(allGenericTypes, org.finos.legend.pure.m3.navigation.typeparameter.TypeParameter.isCovariant(possibleParentTypeParam), false, processorSupport);
                toRegisterTypes.put((GenericType) possibleParentTypeParam, (GenericType) res);
            }
        }
        // Accumulate changes so as not to modify during iteration
        MutableMap<Multiplicity, Multiplicity> toRegisterMultiplicities = Maps.mutable.empty();
        for (String multiplicityParam : nonInstanceSpecificState.getMultiplicityParameters()) {
            CoreInstance possibleParentMultiplicityTypeParam = nonInstanceSpecificState.getMultiplicityParameterValue(multiplicityParam);
            if (!org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.isMultiplicityConcrete(possibleParentMultiplicityTypeParam) && !instanceStates.isEmpty()) {
                CoreInstance res = instanceStates.getFirst().getMultiplicityParameterValue(multiplicityParam);
                for (TypeInferenceContextState v : instanceStates) {
                    res = org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.minSubsumingMultiplicity(res, v.getMultiplicityParameterValue(multiplicityParam), processorSupport);
                }
                toRegisterMultiplicities.put((Multiplicity) possibleParentMultiplicityTypeParam, (Multiplicity) res);
            }
        }
        toRegisterTypes.forEachKeyValue((from, to) -> typeInferenceContext.getParent().register(from, to, typeInferenceContext.getParent(), state.getObserver()));
        toRegisterMultiplicities.forEachKeyValue((from, to) -> typeInferenceContext.getParent().registerMul(from, to, typeInferenceContext.getParent(), state.getObserver()));
    }
}
Also used : GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 4 with ProcessorState

use of org.finos.legend.pure.m3.compiler.postprocessing.ProcessorState in project legend-pure by finos.

the class AssociationProcessor method process.

@Override
public void process(Association association, ProcessorState state, Matcher matcher, ModelRepository repository, Context context, ProcessorSupport processorSupport) {
    RichIterable<? extends AbstractProperty> properties = association._properties();
    RichIterable<? extends AbstractProperty> originalMilestonedProperties = association._originalMilestonedProperties();
    RichIterable<? extends AbstractProperty> qualifiedProperties = association._qualifiedProperties();
    for (AbstractProperty property : LazyIterate.concatenate((Iterable<AbstractProperty>) properties, (Iterable<AbstractProperty>) qualifiedProperties).concatenate((Iterable<AbstractProperty>) originalMilestonedProperties)) {
        PostProcessor.processElement(matcher, property, state, processorSupport);
    }
}
Also used : SetIterable(org.eclipse.collections.api.set.SetIterable) RichIterable(org.eclipse.collections.api.RichIterable) ListIterable(org.eclipse.collections.api.list.ListIterable) AbstractProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty)

Example 5 with ProcessorState

use of org.finos.legend.pure.m3.compiler.postprocessing.ProcessorState in project legend-pure by finos.

the class FunctionDefinitionProcessor method processExpression.

private static void processExpression(FunctionDefinition<?> functionDefinition, Matcher matcher, ProcessorState processorState, ProcessorSupport processorSupport, int i, ValueSpecification expression) {
    processorState.resetVariables();
    PostProcessor.processElement(matcher, expression, processorState, processorSupport);
    if (expression._usageContext() == null) {
        ExpressionSequenceValueSpecificationContext usageContext = (ExpressionSequenceValueSpecificationContext) processorSupport.newAnonymousCoreInstance(null, M3Paths.ExpressionSequenceValueSpecificationContext);
        usageContext._offset(i);
        usageContext._functionDefinition(functionDefinition);
        expression._usageContext(usageContext);
    }
}
Also used : ExpressionSequenceValueSpecificationContext(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ExpressionSequenceValueSpecificationContext)

Aggregations

CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)28 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)23 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)21 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)20 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)17 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)12 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)12 RichIterable (org.eclipse.collections.api.RichIterable)11 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)10 RelationalOperationElement (org.finos.legend.pure.m3.coreinstance.meta.relational.metamodel.RelationalOperationElement)9 ProcessorState (org.finos.legend.pure.m3.compiler.postprocessing.ProcessorState)8 QualifiedProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)8 InstanceValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.InstanceValue)8 PropertyMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping)7 LambdaFunction (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.LambdaFunction)7 AbstractProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty)7 ClassInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)7 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)6 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)6 ListIterable (org.eclipse.collections.api.list.ListIterable)5