use of org.finos.legend.pure.m3.compiler.postprocessing.VariableContext.VariableNameConflictException in project legend-pure by finos.
the class FunctionExpressionProcessor method process.
@Override
public void process(FunctionExpression functionExpression, final ProcessorState state, final Matcher matcher, final ModelRepository repository, final Context context, final ProcessorSupport processorSupport) {
TypeInferenceObserver observer = state.getObserver();
state.pushTypeInferenceContext();
ListIterable<? extends ValueSpecification> parametersValues = ListHelper.wrapListIterable(functionExpression._parametersValues());
// Process the function's parameters (FIRST PASS)
boolean inferenceSuccess = firstPassTypeInference(functionExpression, parametersValues, state, matcher, repository, context, processorSupport);
// Function matching
ListIterable<? extends Function<?>> foundFunctions = null != functionExpression._funcCoreInstance() ? Lists.immutable.with((Function<?>) ImportStub.withImportStubByPass(functionExpression._funcCoreInstance(), processorSupport)) : Lists.immutable.empty();
String functionName = null;
if (foundFunctions.isEmpty()) {
// Check if the function is a property
InstanceValue propertyNameInstanceVal = functionExpression._propertyName();
if (null != propertyNameInstanceVal) {
ValueSpecification source = parametersValues.get(0);
String propertyName = ImportStub.withImportStubByPass(propertyNameInstanceVal._valuesCoreInstance().toList().get(0), processorSupport).getName();
GenericType sourceGenericType = extractAndValidateGenericType(processorSupport, propertyName, source);
// Is it an enum?
if (org.finos.legend.pure.m3.navigation.generictype.GenericType.subTypeOf(sourceGenericType, org.finos.legend.pure.m3.navigation.type.Type.wrapGenericType(processorSupport.package_getByUserPath(M3Paths.Enumeration), processorSupport), processorSupport)) {
reprocessEnumValueInExtractEnumValue(functionExpression, propertyName, state, repository, processorSupport);
} else {
Multiplicity sourceMultiplicity = source._multiplicity();
if (org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.isToOne(sourceMultiplicity, true)) {
AbstractProperty<?> propertyFunc = findFunctionForPropertyBasedOnMultiplicity(functionExpression, sourceGenericType, state, processorSupport, matcher);
if (null != propertyFunc) {
if (MilestoningFunctions.isGeneratedMilestonedQualifiedPropertyWithMissingDates(propertyFunc, processorSupport)) {
propertyFunc = (AbstractProperty<?>) MilestoningDatesPropagationFunctions.getMilestoningQualifiedPropertyWithAllDatesSupplied(functionExpression, state, repository, context, processorSupport, propertyNameInstanceVal, source, propertyName, propertyFunc);
}
foundFunctions = Lists.immutable.with(propertyFunc);
}
} else {
// Automap
reprocessPropertyForManySources(functionExpression, parametersValues, M3Properties.propertyName, sourceGenericType, repository, processorSupport);
// The parameters values are now different, so update
parametersValues = functionExpression._parametersValues().toList();
// Have another go at type inference
inferenceSuccess = firstPassTypeInference(functionExpression, parametersValues, state, matcher, repository, context, processorSupport);
// return;
}
}
} else // Check if the function is a qualifiedProperty
{
InstanceValue qualifiedPropertyNameVal = functionExpression._qualifiedPropertyName();
if (null != qualifiedPropertyNameVal) {
ValueSpecification source = parametersValues.get(0);
String qualifiedPropertyName = ImportStub.withImportStubByPass(qualifiedPropertyNameVal._valuesCoreInstance().toList().get(0), processorSupport).getName();
GenericType sourceGenericType = extractAndValidateGenericType(processorSupport, qualifiedPropertyName, source);
Multiplicity sourceMultiplicity = source._multiplicity();
// }
if (org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.isToOne(sourceMultiplicity, true)) {
ListIterable<QualifiedProperty<?>> qualifiedPropertyFuncs = findFunctionsForQualifiedPropertyBasedOnMultiplicity(functionExpression, sourceGenericType, parametersValues, processorSupport, matcher, state);
if (qualifiedPropertyFuncs.size() == 1 && MilestoningFunctions.isGeneratedMilestonedQualifiedPropertyWithMissingDates(qualifiedPropertyFuncs.getFirst(), processorSupport)) {
Function<?> mqp = (Function<?>) MilestoningDatesPropagationFunctions.getMilestoningQualifiedPropertyWithAllDatesSupplied(functionExpression, state, repository, context, processorSupport, qualifiedPropertyNameVal, source, qualifiedPropertyName, qualifiedPropertyFuncs.getFirst());
foundFunctions = Lists.immutable.with(mqp);
} else {
foundFunctions = qualifiedPropertyFuncs;
}
} else {
// Automap
reprocessPropertyForManySources(functionExpression, parametersValues, M3Properties.qualifiedPropertyName, sourceGenericType, repository, processorSupport);
// The parameters values are now different, so update
parametersValues = functionExpression._parametersValues().toList();
// Have another go at type inference
inferenceSuccess = firstPassTypeInference(functionExpression, parametersValues, state, matcher, repository, context, processorSupport);
}
}
}
if (foundFunctions.isEmpty()) {
// Match the functionExpression with the Function library (may still need to do it even if the function is a property because it may have been reprocessed as a Collect!)
foundFunctions = FunctionExpressionMatcher.findMatchingFunctionsInTheRepository(functionExpression, true, processorSupport);
functionName = getFunctionName(functionExpression);
}
}
Function<?> finalFunction = null;
boolean someInferenceFailed = false;
for (Function<?> foundFunction : foundFunctions) {
functionExpression._funcRemove();
functionExpression._funcCoreInstance(foundFunction);
state.getTypeInferenceContext().setScope(foundFunction);
FunctionType foundFunctionType = (FunctionType) processorSupport.function_getFunctionType(foundFunction);
observer.functionMatched(foundFunction, foundFunctionType);
// SECOND PASS
ListIterable<? extends VariableExpression> paramsType = foundFunctionType._parameters().toList();
// enumValues, autoMaps, etc...
parametersValues = functionExpression._parametersValues().toList();
boolean success = true;
if (!inferenceSuccess) {
observer.firstPassInferenceFailed();
observer.shiftTab();
observer.shiftTab();
observer.matchTypeParamsFromFoundFunction(foundFunction);
observer.shiftTab();
for (int z = 0; z < parametersValues.size(); z++) {
ValueSpecification instance = parametersValues.get(z);
if (isInferenceSuccess(instance, processorSupport)) {
observer.matchParam(z);
GenericType templateGenType = paramsType.get(z)._genericType();
TypeInferenceContext typeInferenceContext = state.getTypeInferenceContext();
typeInferenceContext.register(templateGenType, instance._genericType(), typeInferenceContext.getTopContext(), observer);
typeInferenceContext.registerMul(paramsType.get(z)._multiplicity(), instance._multiplicity(), typeInferenceContext.getTopContext(), observer);
} else {
observer.paramInferenceFailed(z);
}
}
observer.unShiftTab();
observer.reverseMatching();
for (int z = 0; z < parametersValues.size(); z++) {
final ValueSpecification instance = parametersValues.get(z);
observer.processingParameter(functionExpression, z, instance);
GenericType templateGenType = paramsType.get(z)._genericType();
Multiplicity templateMultiplicity = paramsType.get(z)._multiplicity();
GenericType resolvedGenericType = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(templateGenType, state.getTypeInferenceContext().getTypeParameterToGenericType(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity(), processorSupport);
Multiplicity resolvedMultiplicity = (Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.makeMultiplicityAsConcreteAsPossible(templateMultiplicity, state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity());
if (isLambdaWithEmptyParamType(instance, processorSupport)) {
observer.shiftTab();
final VariableExpression templateToMatchLambdaTo = Objects.requireNonNull(getRawTypeFromGenericType(foundFunction, processorSupport))._parameters().toList().get(z);
observer.register(templateToMatchLambdaTo, templateToMatchLambdaTo, state.getTypeInferenceContext(), state.getTypeInferenceContext());
for (final CoreInstance val : ((InstanceValue) instance)._valuesCoreInstance()) {
if (val instanceof LambdaFunction) {
org.eclipse.collections.api.block.function.Function<CoreInstance, Boolean> processParamTypesOfLambdaUsedAsAFunctionExpressionParamValue = coreInstance -> !TypeInference.processParamTypesOfLambdaUsedAsAFunctionExpressionParamValue(instance, (LambdaFunction<?>) val, templateToMatchLambdaTo, matcher, state, repository, processorSupport);
success = success && MilestoningDatesPropagationFunctions.possiblyExecuteInNewMilestoningDateContext(functionExpression, val, processParamTypesOfLambdaUsedAsAFunctionExpressionParamValue, state, repository, context, processorSupport);
// Manage return type in any case
GenericType templateGenericType = templateToMatchLambdaTo._genericType();
ClassInstance functionClass = (ClassInstance) processorSupport.package_getByUserPath(M3Paths.Function);
if (org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(templateGenericType, processorSupport) && org.finos.legend.pure.m3.navigation.type.Type.subTypeOf(ImportStub.withImportStubByPass(templateGenericType._rawTypeCoreInstance(), processorSupport), functionClass, processorSupport)) {
GenericType templateGenFunctionType = templateGenericType._typeArguments().toList().get(0);
if (org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(templateGenFunctionType, processorSupport) && !org.finos.legend.pure.m3.navigation.type.Type.isTopType(Instance.getValueForMetaPropertyToOneResolved(templateGenFunctionType, M3Properties.rawType, processorSupport), processorSupport)) {
GenericType templateReturnType = null != ImportStub.withImportStubByPass(templateGenFunctionType._rawTypeCoreInstance(), processorSupport) ? ((FunctionType) ImportStub.withImportStubByPass(templateGenFunctionType._rawTypeCoreInstance(), processorSupport))._returnType() : null;
// Generics in lambdas are relative to their environment (i.e. the function in which they are defined)
TypeInferenceContext lambdaInferenceContext = state.getTypeInferenceContext().getTopContext();
if (null != templateReturnType) {
FunctionType lambdaFunctionType = Objects.requireNonNull(getRawTypeFromGenericType((LambdaFunction<?>) val, processorSupport));
GenericType concreteGenericType = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(lambdaFunctionType._returnType(), lambdaInferenceContext.getTypeParameterToGenericType(), lambdaInferenceContext.getMultiplicityParameterToMultiplicity(), processorSupport);
lambdaFunctionType._returnTypeRemove();
lambdaFunctionType._returnType(concreteGenericType);
if (!org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(templateReturnType, processorSupport)) {
TypeInferenceContext typeInferenceContext = state.getTypeInferenceContext();
typeInferenceContext.register(templateReturnType, concreteGenericType, typeInferenceContext.getParent(), observer);
}
}
Multiplicity templateReturnMultiplicity = null != ImportStub.withImportStubByPass(templateGenFunctionType._rawTypeCoreInstance(), processorSupport) ? ((FunctionType) ImportStub.withImportStubByPass(templateGenFunctionType._rawTypeCoreInstance(), processorSupport))._returnMultiplicity() : null;
if (null != templateReturnMultiplicity) {
FunctionType lambdaFunctionType = Objects.requireNonNull(getRawTypeFromGenericType((LambdaFunction<?>) val, processorSupport));
Multiplicity concreteMultiplicity = (Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.makeMultiplicityAsConcreteAsPossible(Instance.getValueForMetaPropertyToOneResolved(lambdaFunctionType, M3Properties.returnMultiplicity, processorSupport), lambdaInferenceContext.getMultiplicityParameterToMultiplicity());
lambdaFunctionType._returnMultiplicityRemove();
lambdaFunctionType._returnMultiplicity(concreteMultiplicity);
if (null != concreteMultiplicity) {
TypeInferenceContext typeInferenceContext = state.getTypeInferenceContext();
typeInferenceContext.registerMul(templateReturnMultiplicity, concreteMultiplicity, typeInferenceContext.getParent(), observer);
}
}
}
}
}
}
observer.unShiftTab();
} else {
state.pushTypeInferenceContextAhead();
TypeInferenceContext typeInferenceContext = state.getTypeInferenceContext();
typeInferenceContext.setScope(instance instanceof SimpleFunctionExpression ? ((SimpleFunctionExpression) instance)._funcCoreInstance() : null);
typeInferenceContext.register(instance._genericType(), resolvedGenericType, typeInferenceContext.getParent(), observer);
typeInferenceContext.registerMul(instance._multiplicity(), resolvedMultiplicity, typeInferenceContext.getParent(), observer);
cleanProcess(instance, state, repository, context, processorSupport);
PostProcessor.processElement(matcher, instance, state, processorSupport);
state.popTypeInferenceContextAhead();
}
}
observer.unShiftTab();
observer.unShiftTab();
} else {
observer.parameterInferenceSucceeded();
observer.shiftTab();
observer.shiftTab();
parametersValues.forEachWithIndex((instance, z) -> {
TypeInferenceContext typeInferenceContext = state.getTypeInferenceContext();
typeInferenceContext.register(paramsType.get(z)._genericType(), instance._genericType(), typeInferenceContext.getTopContext(), observer);
typeInferenceContext.registerMul(paramsType.get(z)._multiplicity(), instance._multiplicity(), typeInferenceContext.getTopContext(), observer);
});
// WARNING / returnType may need reverse matching to be found
GenericType returnGenericType = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(foundFunctionType._returnType(), state.getTypeInferenceContext().getTypeParameterToGenericType(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity(), processorSupport);
observer.returnType(returnGenericType);
if (!org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(returnGenericType, processorSupport) && !state.getTypeInferenceContext().isTop(org.finos.legend.pure.m3.navigation.generictype.GenericType.getTypeParameterName(returnGenericType, processorSupport))) {
observer.shiftTab();
observer.returnTypeNotConcrete();
// reverse matching
parametersValues.forEachWithIndex((instance, z) -> {
GenericType templateGenType = paramsType.get(z)._genericType();
Multiplicity templateMultiplicity = paramsType.get(z)._multiplicity();
GenericType resolvedGenericType = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(templateGenType, state.getTypeInferenceContext().getTypeParameterToGenericType(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity(), processorSupport);
Multiplicity resolvedMultiplicity = (Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.makeMultiplicityAsConcreteAsPossible(templateMultiplicity, state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity());
state.pushTypeInferenceContextAhead();
state.getTypeInferenceContext().setScope(instance instanceof FunctionExpression ? ((FunctionExpression) instance)._funcCoreInstance() : null);
observer.processingParameter(functionExpression, z, instance);
TypeInferenceContext typeInferenceContext = state.getTypeInferenceContext();
typeInferenceContext.register(instance._genericType(), resolvedGenericType, typeInferenceContext.getParent(), observer);
typeInferenceContext.registerMul(instance._multiplicity(), resolvedMultiplicity, typeInferenceContext.getParent(), observer);
observer.shiftTab();
observer.reprocessingTheParameter();
observer.shiftTab();
cleanProcess(instance, state, repository, context, processorSupport);
PostProcessor.processElement(matcher, instance, state, processorSupport);
observer.unShiftTab();
observer.finishedProcessParameter();
observer.unShiftTab();
state.popTypeInferenceContextAhead();
});
observer.unShiftTab();
returnGenericType = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(foundFunctionType._returnType(), state.getTypeInferenceContext().getTypeParameterToGenericType(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity(), processorSupport);
observer.newReturnType(returnGenericType);
}
observer.unShiftTab();
observer.unShiftTab();
observer.finishedRegisteringParametersAndMultiplicities();
}
// We can infer the parameter types for Lambdas given as parameters (now that we know which function to use).
if (success) {
TypeInference.storeInferredTypeParametersInFunctionExpression(functionExpression, state, processorSupport, foundFunction);
// Get the return type information
// Pair<CoreInstance, CoreInstance> result = FunctionExpression.resolveFunctionGenericReturnTypeAndMultiplicity(functionExpression, context, processorSupport);
// result.getOne();
GenericType returnGenericType = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(foundFunctionType._returnType(), state.getTypeInferenceContext().getTypeParameterToGenericType(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity(), processorSupport);
Multiplicity returnMultiplicity = (Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.makeMultiplicityAsConcreteAsPossible(foundFunctionType._returnMultiplicity(), state.getTypeInferenceContext().getMultiplicityParameterToMultiplicity());
if (!org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(returnGenericType, processorSupport) && !state.getTypeInferenceContext().isTop(org.finos.legend.pure.m3.navigation.generictype.GenericType.getTypeParameterName(returnGenericType, processorSupport))) {
throw new PureCompilationException(functionExpression.getSourceInformation(), "The system is not capable of inferring the return type of the function '" + functionExpression.getValueForMetaPropertyToOne(M3Properties.func).getValueForMetaPropertyToOne(M3Properties.functionName).getName() + "'. Check your signatures!");
}
// Update the type
GenericType genericTypeCopy = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericTypeAsInferredGenericType(returnGenericType, functionExpression.getSourceInformation(), processorSupport);
functionExpression._genericType(genericTypeCopy);
// Update the multiplicity
Multiplicity returnMultiplicityCopy = (Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.copyMultiplicity(returnMultiplicity, functionExpression.getSourceInformation(), processorSupport);
functionExpression._multiplicity(returnMultiplicityCopy);
} else {
// Inference failed...
FunctionType functionType = (FunctionType) processorSupport.function_getFunctionType(foundFunction);
GenericType returnGenericType = functionType._returnType();
GenericType genericTypeCopy = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.copyGenericTypeAsInferredGenericType(returnGenericType, functionExpression.getSourceInformation(), processorSupport);
functionExpression._genericType(genericTypeCopy);
Multiplicity returnMultiplicity = functionType._returnMultiplicity();
Multiplicity returnMultiplicityCopy = (Multiplicity) org.finos.legend.pure.m3.navigation.multiplicity.Multiplicity.copyMultiplicity(returnMultiplicity, functionExpression.getSourceInformation(), processorSupport);
functionExpression._multiplicity(returnMultiplicityCopy);
}
if (null == functionName) {
finalFunction = foundFunction;
} else if (!success) {
someInferenceFailed = true;
} else {
Function<?> bestMatch = FunctionExpressionMatcher.getBestFunctionMatch(foundFunctions, parametersValues, functionName, functionExpression.getSourceInformation(), false, processorSupport);
if (bestMatch == foundFunction) {
finalFunction = foundFunction;
}
}
if (null != finalFunction) {
break;
}
// Clean up before re-trying
if (1 < foundFunctions.size()) {
for (ValueSpecification parameterValue : parametersValues) {
cleanProcess(parameterValue, state, repository, context, processorSupport);
}
inferenceSuccess = firstPassTypeInference(functionExpression, parametersValues, state, matcher, repository, context, processorSupport);
}
}
if (null != finalFunction) {
finalFunction._applications(Lists.immutable.<FunctionExpression>withAll(finalFunction._applications()).newWith(functionExpression));
// Update the function in the function expression in the reverse
if ("new_Class_1__String_1__KeyExpression_MANY__T_1_".equals(finalFunction.getName()) || "new_Class_1__String_1__T_1_".equals(finalFunction.getName())) {
this.addTraceForKeyExpressions(functionExpression, processorSupport);
}
if ("copy_T_1__String_1__KeyExpression_MANY__T_1_".equals(finalFunction.getName()) || "copy_T_1__String_1__T_1_".equals(finalFunction.getName())) {
this.addTraceForKeyExpressions(functionExpression, processorSupport);
}
if ("letFunction_String_1__T_m__T_m_".equals(finalFunction.getName())) {
try {
state.getVariableContext().getParent().registerValue(((InstanceValue) parametersValues.get(0))._valuesCoreInstance().toList().get(0).getName(), parametersValues.get(1));
} catch (VariableNameConflictException e) {
throw new PureCompilationException(functionExpression.getSourceInformation(), e.getMessage());
}
}
} else if (!someInferenceFailed) {
throwNoMatchException(functionExpression, state, processorSupport);
}
observer.unShiftTab();
observer.finishedProcessingFunctionExpression(functionExpression);
state.popTypeInferenceContext();
}
use of org.finos.legend.pure.m3.compiler.postprocessing.VariableContext.VariableNameConflictException in project legend-pure by finos.
the class FunctionDefinitionProcessor method process.
public static void process(FunctionDefinition<?> functionDefinition, ProcessorState state, Matcher matcher, ModelRepository repository) {
ProcessorSupport processorSupport = state.getProcessorSupport();
VariableContext variableContext = state.getVariableContext();
FunctionType functionType = (FunctionType) processorSupport.function_getFunctionType(functionDefinition);
state.getObserver().startProcessingFunction(functionDefinition, functionType);
URLPatternLibrary urlPatternLibrary = state.getURLPatternLibrary();
if (urlPatternLibrary != null) {
urlPatternLibrary.possiblyRegister(functionDefinition, processorSupport);
}
boolean shouldSetTypeInferenceContext = (functionDefinition instanceof ConcreteFunctionDefinition) && (functionDefinition._classifierGenericType() != null) && (functionDefinition._classifierGenericType()._rawTypeCoreInstance() != null) && "ConcreteFunctionDefinition".equals(functionDefinition._classifierGenericType()._rawTypeCoreInstance().getName());
if (shouldSetTypeInferenceContext) {
state.newTypeInferenceContext(functionType);
}
functionType._parameters().forEach(var -> {
try {
variableContext.registerValue(var._name(), var);
} catch (VariableNameConflictException e) {
throw new PureCompilationException(functionDefinition.getSourceInformation(), e.getMessage());
}
GenericType propertyType = var._genericType();
// The property type may be null if it's a lambda expression...
if (propertyType != null) {
// We resolve because we want to fail fast if a given type is unknown...
org.finos.legend.pure.m3.navigation.generictype.GenericType.resolveGenericTypeUsingImports(propertyType, repository, processorSupport);
}
});
ListIterable<? extends ValueSpecification> expressions = functionDefinition._expressionSequence().toList();
if (expressions.isEmpty()) {
throw new PureCompilationException(functionDefinition.getSourceInformation(), "Function definition must contain at least one expression");
}
// The function is going to be processed again after inference
if (TypeInference.canProcessLambda(functionDefinition, state, processorSupport)) {
state.getObserver().shiftTab();
state.getObserver().startProcessingFunctionBody();
processExpressions(functionDefinition, expressions, matcher, state, processorSupport);
findReturnTypesForLambda(functionDefinition, functionType, processorSupport);
FunctionDefinitionValidator.validateFunctionReturnType(functionDefinition, functionType, processorSupport);
state.getObserver().finishedProcessingFunctionBody();
state.getObserver().unShiftTab();
state.addFunctionDefinition(functionDefinition);
}
if (shouldSetTypeInferenceContext) {
state.deleteTypeInferenceContext();
}
state.getVariableContext().buildAndRegister("return", functionType._returnType(), functionType._returnMultiplicity(), processorSupport);
RichIterable<? extends Constraint> constraints = functionDefinition._preConstraints();
if (constraints.notEmpty()) {
processConstraints(functionDefinition, constraints.toList(), matcher, state, processorSupport);
}
RichIterable<? extends Constraint> postConstraints = functionDefinition._postConstraints();
if (postConstraints.notEmpty()) {
processConstraints(functionDefinition, postConstraints.toList(), matcher, state, processorSupport);
}
state.getObserver().finishedProcessingFunction(functionType);
}
use of org.finos.legend.pure.m3.compiler.postprocessing.VariableContext.VariableNameConflictException in project legend-pure by finos.
the class FunctionExecutionInterpreted method executeFunction.
public CoreInstance executeFunction(boolean limitScope, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function<CoreInstance> function, ListIterable<? extends CoreInstance> params, final Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, final Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, final VariableContext varContext, final CoreInstance functionExpressionToUseInStack, final Profiler profiler, final InstantiationContext instantiationContext, final ExecutionSupport executionSupport) {
try {
if (this.cancelExecution.compareAndSet(true, false)) {
throw new PureExecutionException("Cancelled!");
}
final ProcessorSupport processorSupport = this.runtime.getProcessorSupport();
ListIterable<? extends CoreInstance> signatureVars = Instance.getValueForMetaPropertyToManyResolved(processorSupport.function_getFunctionType(function), M3Properties.parameters, processorSupport);
if (signatureVars.size() != params.size()) {
StringBuilder builder = new StringBuilder();
Function.print(builder, function, processorSupport);
String message = "Error executing the function:" + builder + ". Mismatch between the number of function parameters (" + signatureVars.size() + ") and the number of supplied arguments (" + params.size() + ")\n" + params.collect(new org.eclipse.collections.api.block.function.Function<CoreInstance, String>() {
@Override
public String valueOf(CoreInstance coreInstance) {
return coreInstance.printWithoutDebug("", 3);
}
}).makeString("\n");
throw new PureExecutionException(functionExpressionToUseInStack == null ? null : functionExpressionToUseInStack.getSourceInformation(), message);
}
final VariableContext variableContext = this.moveParametersIntoVariableContext(varContext, signatureVars, params, functionExpressionToUseInStack);
if (limitScope) {
variableContext.markVariableScopeBoundary();
}
for (CoreInstance constraint : function._preConstraints()) {
CoreInstance definition = Instance.getValueForMetaPropertyToOneResolved(Instance.getValueForMetaPropertyToOneResolved(constraint, M3Properties.functionDefinition, processorSupport), M3Properties.expressionSequence, processorSupport);
String ruleId = Instance.getValueForMetaPropertyToOneResolved(constraint, M3Properties.name, processorSupport).getName();
CoreInstance evaluatedConstraint = this.executeValueSpecification(definition, new Stack<MutableMap<String, CoreInstance>>(), new Stack<MutableMap<String, CoreInstance>>(), null, variableContext, VoidProfiler.VOID_PROFILER, instantiationContext, executionSupport);
if ("false".equals(evaluatedConstraint.getValueForMetaPropertyToOne(M3Properties.values).getName())) {
throw new PureExecutionException(functionExpressionToUseInStack == null ? null : functionExpressionToUseInStack.getSourceInformation(), "Constraint (PRE):[" + ruleId + "] violated. (Function:" + function.getName() + ")");
}
}
// Execute
CoreInstance result;
if (Instance.instanceOf(function, M3Paths.NativeFunction, processorSupport)) {
org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.NativeFunction function1 = NativeFunctionCoreInstanceWrapper.toNativeFunction(function);
NativeFunction nativeFunction = this.nativeFunctions.get(function1.getName());
if (nativeFunction == null) {
throw new PureExecutionException(functionExpressionToUseInStack.getSourceInformation(), "The function '" + function1.getName() + "' is not supported by this execution platform");
}
result = nativeFunction.execute(params, resolvedTypeParameters, resolvedMultiplicityParameters, variableContext, functionExpressionToUseInStack, profiler, instantiationContext, executionSupport, this.runtime.getContext(), this.runtime.getProcessorSupport());
} else if (Instance.instanceOf(function, M3Paths.Property, processorSupport)) {
result = this.executeProperty(PropertyCoreInstanceWrapper.toProperty(function), true, resolvedTypeParameters, resolvedMultiplicityParameters, varContext, profiler, params, functionExpressionToUseInStack, instantiationContext, executionSupport);
} else // Qualified properties also go here
if (Instance.instanceOf(function, M3Paths.FunctionDefinition, processorSupport)) {
RichIterable<? extends CoreInstance> expressions = FunctionDefinitionCoreInstanceWrapper.toFunctionDefinition(function)._expressionSequence();
CoreInstance returnVal = null;
for (CoreInstance expression : expressions) {
Executor executor = findValueSpecificationExecutor(expression, functionExpressionToUseInStack, processorSupport, this);
returnVal = executor.execute(expression, resolvedTypeParameters, resolvedMultiplicityParameters, functionExpressionToUseInStack, variableContext, profiler, instantiationContext, executionSupport, this, processorSupport);
}
result = returnVal;
} else if (Instance.instanceOf(function, M3Paths.Path, processorSupport)) {
final boolean executable = ValueSpecification.isExecutable(params.get(0), processorSupport);
CoreInstance value = params.get(0).getValueForMetaPropertyToOne(M3Properties.values);
MutableList<CoreInstance> res = function.getValueForMetaPropertyToMany(M3Properties.path).injectInto(FastList.newListWith(value), new CheckedFunction2<MutableList<CoreInstance>, CoreInstance, MutableList<CoreInstance>>() {
@Override
public MutableList<CoreInstance> safeValue(MutableList<CoreInstance> instances, final CoreInstance pathElement) throws Exception {
return instances.flatCollect(new CheckedFunction<CoreInstance, Iterable<CoreInstance>>() {
@Override
public Iterable<CoreInstance> safeValueOf(CoreInstance instance) throws Exception {
CoreInstance property = Instance.getValueForMetaPropertyToOneResolved(pathElement, M3Properties.property, processorSupport);
MutableList<CoreInstance> parameters = FastList.newListWith(ValueSpecificationBootstrap.wrapValueSpecification(instance, executable, processorSupport));
parameters.addAllIterable(Instance.getValueForMetaPropertyToManyResolved(pathElement, M3Properties.parameters, processorSupport).collect(new org.eclipse.collections.api.block.function.Function<CoreInstance, CoreInstance>() {
@Override
public CoreInstance valueOf(CoreInstance coreInstance) {
return ValueSpecificationBootstrap.wrapValueSpecification(Instance.getValueForMetaPropertyToManyResolved(coreInstance, M3Properties.values, processorSupport), executable, processorSupport);
}
}));
return (Iterable<CoreInstance>) FunctionExecutionInterpreted.this.executeFunction(false, PropertyCoreInstanceWrapper.toProperty(property), parameters, resolvedTypeParameters, resolvedMultiplicityParameters, variableContext, functionExpressionToUseInStack, profiler, instantiationContext, executionSupport).getValueForMetaPropertyToMany(M3Properties.values);
}
});
}
});
result = ValueSpecificationBootstrap.wrapValueSpecification(res, executable, processorSupport);
} else {
throw new PureExecutionException("Unsupported function for execution");
}
if (function._postConstraints().notEmpty()) {
try {
variableContext.registerValue("return", result);
} catch (VariableNameConflictException e) {
throw new PureExecutionException(functionExpressionToUseInStack.getSourceInformation(), e.getMessage(), e);
}
for (CoreInstance constraint : function._postConstraints()) {
CoreInstance definition = Instance.getValueForMetaPropertyToOneResolved(Instance.getValueForMetaPropertyToOneResolved(constraint, M3Properties.functionDefinition, processorSupport), M3Properties.expressionSequence, processorSupport);
String ruleId = Instance.getValueForMetaPropertyToOneResolved(constraint, M3Properties.name, processorSupport).getName();
CoreInstance evaluatedConstraint = this.executeValueSpecification(definition, new Stack<MutableMap<String, CoreInstance>>(), new Stack<MutableMap<String, CoreInstance>>(), null, variableContext, VoidProfiler.VOID_PROFILER, instantiationContext, executionSupport);
if ("false".equals(evaluatedConstraint.getValueForMetaPropertyToOne(M3Properties.values).getName())) {
throw new PureExecutionException(functionExpressionToUseInStack == null ? null : functionExpressionToUseInStack.getSourceInformation(), "Constraint (POST):[" + ruleId + "] violated. (Function:" + function.getName() + ")");
}
}
}
return result;
} catch (PureAssertFailException e) {
org.finos.legend.pure.m4.coreinstance.SourceInformation sourceInfo = (functionExpressionToUseInStack == null ? null : functionExpressionToUseInStack.getSourceInformation());
if (sourceInfo != null && sourceInfo != e.getSourceInformation()) {
String testPurePlatformFileName = "/platform/pure/corefunctions/test.pure";
boolean allFromAssert = true;
for (org.finos.legend.pure.m4.coreinstance.SourceInformation si : e.getPureStackSourceInformation()) {
allFromAssert = allFromAssert && si != null && testPurePlatformFileName.equals(si.getSourceId());
}
if (allFromAssert && !testPurePlatformFileName.equals(sourceInfo.getSourceId())) {
throw new PureAssertFailException(sourceInfo, e.getInfo());
} else {
throw new PureAssertFailException(sourceInfo, e.getInfo(), e);
}
} else {
throw e;
}
} catch (PureException e) {
if (functionExpressionToUseInStack != null) {
org.finos.legend.pure.m4.coreinstance.SourceInformation sourceInfo = functionExpressionToUseInStack.getSourceInformation();
if (sourceInfo != null && !sourceInfo.equals(e.getSourceInformation())) {
throw new PureExecutionException(sourceInfo, e.getInfo(), e);
}
}
throw e;
} catch (RuntimeException e) {
if (functionExpressionToUseInStack != null) {
org.finos.legend.pure.m4.coreinstance.SourceInformation sourceInfo = functionExpressionToUseInStack.getSourceInformation();
PureException pureException = PureException.findPureException(e);
if (pureException == null) {
if (sourceInfo != null) {
throw new PureExecutionException(sourceInfo, e.getMessage(), e);
}
} else if (sourceInfo != null && sourceInfo != pureException.getSourceInformation()) {
if (pureException instanceof PureAssertFailException) {
throw new PureAssertFailException(sourceInfo, pureException.getInfo(), (PureAssertFailException) pureException);
} else {
throw new PureExecutionException(sourceInfo, pureException.getInfo(), pureException);
}
} else {
throw pureException;
}
}
throw e;
}
}
use of org.finos.legend.pure.m3.compiler.postprocessing.VariableContext.VariableNameConflictException in project legend-pure by finos.
the class FunctionExecutionInterpreted method moveParametersIntoVariableContext.
private VariableContext moveParametersIntoVariableContext(VariableContext variableContext, ListIterable<? extends CoreInstance> signatureVars, ListIterable<? extends CoreInstance> parameters, CoreInstance functionExpressionToUseInStack) {
VariableContext newVarContext = VariableContext.newVariableContext(variableContext);
try {
ProcessorSupport processorSupport = this.runtime.getProcessorSupport();
for (int i = 0, length = signatureVars.size(); i < length; i++) {
CoreInstance varName = Instance.getValueForMetaPropertyToOneResolved(signatureVars.get(i), M3Properties.name, processorSupport);
newVarContext.registerValue(varName == null ? "Unknown" : varName.getName(), parameters.get(i));
}
} catch (VariableNameConflictException e) {
throw new PureExecutionException(functionExpressionToUseInStack.getSourceInformation(), e.getMessage(), e);
}
return newVarContext;
}
Aggregations