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;
}
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!");
}
});
}
}
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;
}
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());
}
}
}
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) : "");
}
Aggregations