Search in sources :

Example 1 with URLPatternLibrary

use of org.finos.legend.pure.m3.serialization.runtime.pattern.URLPatternLibrary 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);
}
Also used : ConcreteFunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) URLPatternLibrary(org.finos.legend.pure.m3.serialization.runtime.pattern.URLPatternLibrary) FunctionType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType) VariableContext(org.finos.legend.pure.m3.compiler.postprocessing.VariableContext) VariableNameConflictException(org.finos.legend.pure.m3.compiler.postprocessing.VariableContext.VariableNameConflictException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 2 with URLPatternLibrary

use of org.finos.legend.pure.m3.serialization.runtime.pattern.URLPatternLibrary in project legend-pure by finos.

the class PostProcessor method process.

public static SourceMutation process(Iterable<? extends CoreInstance> newInstancesConsolidated, ModelRepository modelRepository, ParserLibrary parserLibrary, InlineDSLLibrary inlineDSLLibrary, CodeStorage codeStorage, Context context, ProcessorSupport processorSupport, URLPatternLibrary URLPatternLibrary, Message message) throws PureCompilationException {
    CoreInstance concreteFunctionDefinition = processorSupport.package_getByUserPath(M3Paths.ConcreteFunctionDefinition);
    CoreInstance nativeFunction = processorSupport.package_getByUserPath(M3Paths.NativeFunction);
    MutableSet<CoreInstance> set = Sets.mutable.with(concreteFunctionDefinition, nativeFunction);
    validatePackages(newInstancesConsolidated, processorSupport);
    renameFunctions(newInstancesConsolidated, modelRepository, set, context, processorSupport);
    populateSpecializations(newInstancesConsolidated, processorSupport);
    populateTemporalMilestonedProperties(newInstancesConsolidated, modelRepository, context, processorSupport);
    MutableList<CoreInstance> allInstancesConsolidated = Lists.mutable.withAll(newInstancesConsolidated);
    allInstancesConsolidated.addAllIterable(populatePropertiesFromAssociations(newInstancesConsolidated, modelRepository, context, processorSupport));
    // Post Process
    Matcher matcher = new Matcher(modelRepository, context, processorSupport);
    addMatchersComingFromParsers(parserLibrary, matcher);
    inlineDSLLibrary.getInlineDSLs().forEach(dsl -> dsl.getProcessors().forEach(matcher::addMatchIfTypeIsKnown));
    ProcessorState state = new ProcessorState(VariableContext.newVariableContext(), parserLibrary, inlineDSLLibrary, processorSupport, URLPatternLibrary, codeStorage, message);
    allInstancesConsolidated.forEach(coreInstance -> {
        state.resetVariableContext();
        processElement(matcher, coreInstance, state, processorSupport);
    });
    state.getFunctionDefinitions().forEach(functionDef -> GenericTypeTraceability.addTraceForFunctionDefinition((FunctionDefinition<?>) functionDef, modelRepository, processorSupport));
    return state.getSourceMutation();
}
Also used : Matcher(org.finos.legend.pure.m3.tools.matcher.Matcher) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) FunctionDefinition(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition)

Aggregations

VariableContext (org.finos.legend.pure.m3.compiler.postprocessing.VariableContext)1 VariableNameConflictException (org.finos.legend.pure.m3.compiler.postprocessing.VariableContext.VariableNameConflictException)1 ConcreteFunctionDefinition (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.ConcreteFunctionDefinition)1 FunctionDefinition (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.FunctionDefinition)1 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)1 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)1 ProcessorSupport (org.finos.legend.pure.m3.navigation.ProcessorSupport)1 URLPatternLibrary (org.finos.legend.pure.m3.serialization.runtime.pattern.URLPatternLibrary)1 Matcher (org.finos.legend.pure.m3.tools.matcher.Matcher)1 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)1 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)1