Search in sources :

Example 1 with PureCompilationException

use of org.finos.legend.pure.m4.exception.PureCompilationException 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 PureCompilationException

use of org.finos.legend.pure.m4.exception.PureCompilationException 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 PureCompilationException

use of org.finos.legend.pure.m4.exception.PureCompilationException in project legend-pure by finos.

the class AssociationProcessor method getPropertyNameForLeftSideOfQualifiedPropertyFilter.

private static String getPropertyNameForLeftSideOfQualifiedPropertyFilter(Association association, QualifiedProperty qualifiedProperty, ValueSpecification instance, Context context, ProcessorSupport processorSupport) {
    String functionName = instance instanceof FunctionExpression ? ((FunctionExpression) instance)._functionName() : null;
    String propertyNameForLeftSideOfQualifiedPropertyFilter;
    if ("filter".equals(functionName)) {
        ValueSpecification leftSideOfFilter = ((FunctionExpression) instance)._parametersValues().toList().getFirst();
        CoreInstance propertyName = leftSideOfFilter instanceof FunctionExpression ? ((FunctionExpression) leftSideOfFilter)._propertyName()._valuesCoreInstance().toList().getFirst() : null;
        ValueSpecification variableExpression = leftSideOfFilter instanceof FunctionExpression ? ((FunctionExpression) leftSideOfFilter)._parametersValues().toList().getFirst() : null;
        String variableExpressionName = variableExpression instanceof VariableExpression ? ((VariableExpression) variableExpression)._name() : null;
        if (!"this".equals(variableExpressionName)) {
            throw new PureCompilationException(instance.getSourceInformation(), validQualifiedPropertyInAssociationMsg() + qualifiedPropertyCompileErrorMsgPrefix(association, qualifiedProperty) + " left side of filter should refer to '$this' not '" + variableExpressionName + "'");
        }
        propertyNameForLeftSideOfQualifiedPropertyFilter = Objects.requireNonNull(propertyName).getName();
    } else {
        ValueSpecification firstParamValue = instance instanceof FunctionExpression ? ((FunctionExpression) instance)._parametersValues().toList().getFirst() : null;
        if (firstParamValue != null) {
            propertyNameForLeftSideOfQualifiedPropertyFilter = getPropertyNameForLeftSideOfQualifiedPropertyFilter(association, qualifiedProperty, firstParamValue, context, processorSupport);
        } else {
            throw new PureCompilationException(qualifiedProperty.getSourceInformation(), validQualifiedPropertyInAssociationMsg() + qualifiedPropertyCompileErrorMsgPrefix(association, qualifiedProperty) + " does not use the 'filter' function");
        }
    }
    return propertyNameForLeftSideOfQualifiedPropertyFilter;
}
Also used : FunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.FunctionExpression) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) 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 4 with PureCompilationException

use of org.finos.legend.pure.m4.exception.PureCompilationException in project legend-pure by finos.

the class ConcreteFunctionDefinitionNameProcessor method process.

public static void process(Function<?> function, ModelRepository repository, ProcessorSupport processorSupport) throws PureCompilationException {
    Package parent = function._package();
    if (parent != null) {
        // Make sure we have a unique name for overloaded functions.
        String signature = getSignatureAndResolveImports(function, repository, processorSupport);
        parent._childrenRemove(function);
        function.setName(signature);
        parent._childrenAdd(function);
        if (function._name() == null) {
            function._name(signature);
        }
        if (parent._children().count(c -> signature.equals(c.getName())) > 1) {
            ListIterable<SourceInformation> sourceInfos = parent._children().collectIf(c -> signature.equals(c.getName()), CoreInstance::getSourceInformation, Lists.mutable.empty()).sortThis();
            String pkg = PackageableElement.getUserPathForPackageableElement(parent);
            if (M3Paths.Root.equals(pkg)) {
                pkg = "::";
            }
            StringBuilder message = new StringBuilder("The function '").append(signature).append("' is defined more than once in the package '").append(pkg).append("' at: ");
            boolean first = true;
            for (SourceInformation sourceInfo : sourceInfos) {
                if (first) {
                    first = false;
                } else {
                    message.append(", ");
                }
                message.append(sourceInfo.getSourceId()).append(" (line:").append(sourceInfo.getLine()).append(" column:").append(sourceInfo.getColumn()).append(')');
            }
            throw new PureCompilationException(function.getSourceInformation(), message.toString());
        }
    }
}
Also used : Multiplicity(org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) PackageableElement(org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement) ModelRepository(org.finos.legend.pure.m4.ModelRepository) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) Lists(org.eclipse.collections.api.factory.Lists) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) MutableList(org.eclipse.collections.api.list.MutableList) Type(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type) M3Paths(org.finos.legend.pure.m3.navigation.M3Paths) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) Function(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) ImportStub(org.finos.legend.pure.m3.navigation.importstub.ImportStub) Package(org.finos.legend.pure.m3.coreinstance.Package) ListIterable(org.eclipse.collections.api.list.ListIterable) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) Package(org.finos.legend.pure.m3.coreinstance.Package) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 5 with PureCompilationException

use of org.finos.legend.pure.m4.exception.PureCompilationException in project legend-pure by finos.

the class M3ToJavaGenerator method createClassPropertyGetter.

private String createClassPropertyGetter(String className, CoreInstance property, CoreInstance propertyReturnGenericType, Imports imports) {
    boolean isPlatformClass = isPlatformClass(property);
    boolean isToOne = isToOne(property);
    boolean isStub = isStubType(property, propertyReturnGenericType);
    String stubType = getSubstituteType(property, propertyReturnGenericType);
    String propertyTypeExternal = getPropertyTypeExternal(property, propertyReturnGenericType, imports, isToOne, false, isPlatformClass, false, "Object");
    String propertyTypeInternal = getPropertyTypeInternal(property, propertyReturnGenericType, imports, isToOne, false, isPlatformClass);
    String propertyTypeExternalToOneNoGenerics = isToOne ? propertyTypeExternal : getPropertyTypeExternal(property, propertyReturnGenericType, imports, true, false, false, false);
    boolean isOwningClass = isOwningClass(className, propertyTypeExternalToOneNoGenerics);
    CoreInstance propertyReturnRawType = getTypeFromGenericType(propertyReturnGenericType);
    String expression;
    if (!isOwningClass && isStub) {
        expression = "return " + fromCoreInstance(property, propertyReturnGenericType, imports, applyFunctionWithCardinality(property, stubType + "Helper.FROM_STUB_FN", "this." + getUnifiedMethodName(property) + "CoreInstance()", isToOne), isToOne);
    } else if (isAnyType(propertyReturnRawType)) {
        stubType = "AnyStub";
        expression = "return " + applyFunctionWithCardinality(property, "Functions.chain(" + stubType + "Helper.FROM_STUB_FN, AnyHelper.UNWRAP_PRIMITIVES)", "this." + getUnifiedMethodName(property) + "CoreInstance()", isToOne);
    } else if (isToOne) {
        if (isMandatoryProperty(property) && isPrimitiveTypeProperty(propertyReturnGenericType) && ("Integer".equals(Objects.requireNonNull(getTypeFromGenericType(propertyReturnGenericType)).getName()) || "Float".equals(Objects.requireNonNull(getTypeFromGenericType(propertyReturnGenericType)).getName()))) {
            expression = propertyTypeInternal + " value = this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ";\n" + "        if (value == null)\n" + "        {\n" + "            throw new PureCompilationException(this.getSourceInformation(), \"'" + property.getName() + "' is a mandatory property\");\n" + "        }\n" + "        return value.getValue()." + PRIMITIVES_EXTERNAL.get(Objects.requireNonNull(getTypeFromGenericType(propertyReturnGenericType)).getName()) + "Value()";
        } else {
            expression = "this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property);
            if (!propertyTypeInternal.equals(propertyTypeExternal)) {
                expression = fromCoreInstanceGettor(property, propertyReturnGenericType, imports, expression);
            }
            expression = "return " + expression;
        }
    } else {
        String toOnePropertyTypeInternal = getPropertyTypeInternal(property, propertyReturnGenericType, imports, true, false, isPlatformClass);
        String toOnePropertyTypeExternal = getPropertyTypeExternal(property, propertyReturnGenericType, imports, true, false, isPlatformClass, false);
        expression = "values.getValues()";
        if (!toOnePropertyTypeInternal.equals(toOnePropertyTypeExternal)) {
            expression = fromCoreInstanceGettor(property, propertyReturnGenericType, imports, expression);
        }
        expression = propertyTypeInternal + " values = this.getState()." + getPropertyNameAsValidJavaIdentifierSwitchName(property) + ";\n" + "        return (values == null) ? Lists.immutable.<" + toOnePropertyTypeExternal + ">empty() : " + expression;
    }
    return "    public " + propertyTypeExternal + " " + getUnifiedMethodName(property) + "()\n" + "    {\n" + "        " + expression + ";\n" + "    }\n" + "\n" + (requiresCoreInstanceMethods(className, propertyTypeExternalToOneNoGenerics, property, propertyReturnGenericType) ? this.createClassPropertyCoreInstanceGetter(property) : "");
}
Also used : CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Aggregations

PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)193 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)108 Test (org.junit.Test)60 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)40 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)38 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)33 Type (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Type)28 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)28 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)20 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)18 Class (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)17 Package (org.finos.legend.pure.m3.coreinstance.Package)15 RichIterable (org.eclipse.collections.api.RichIterable)14 AbstractProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.AbstractProperty)14 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)14 MutableList (org.eclipse.collections.api.list.MutableList)12 ListIterable (org.eclipse.collections.api.list.ListIterable)11 ValidatorState (org.finos.legend.pure.m3.compiler.validation.ValidatorState)11 PropertyMapping (org.finos.legend.pure.m3.coreinstance.meta.pure.mapping.PropertyMapping)11 Matcher (org.finos.legend.pure.m3.tools.matcher.Matcher)11