Search in sources :

Example 1 with TypeParameter

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter 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 2 with TypeParameter

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter in project legend-pure by finos.

the class AntlrContextToM3CoreInstance method classParser.

private CoreInstance classParser(ClassDefinitionContext ctx, ImportGroup importId, boolean addLines) throws PureParserException {
    MutableList<Property<? extends CoreInstance, ?>> properties = Lists.mutable.empty();
    MutableList<QualifiedProperty<? extends CoreInstance>> qualifiedProperties = Lists.mutable.empty();
    MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
    boolean isDirectSubTypeofAny = false;
    MutableList<String> typeParameterNames = Lists.mutable.empty();
    MutableList<Boolean> contravariants = Lists.mutable.empty();
    MutableList<String> multiplicityParameterNames = Lists.mutable.empty();
    ImportStubInstance ownerType;
    ListIterable<CoreInstance> stereotypes = null;
    ListIterable<TaggedValue> tags = null;
    ClassInstance classInstance;
    if (ctx.stereotypes() != null) {
        stereotypes = this.stereotypes(ctx.stereotypes(), importId);
    }
    if (ctx.taggedValues() != null) {
        tags = this.taggedValues(ctx.taggedValues(), importId);
    }
    this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
    if (ctx.typeParametersWithContravarianceAndMultiplicityParameters() != null) {
        this.typeParametersWithContravarianceAndMultiplicityParameters(ctx.typeParametersWithContravarianceAndMultiplicityParameters(), typeParameterNames, contravariants, multiplicityParameterNames);
    }
    if (ctx.projection() != null) {
        return this.projectionParser(ctx, importId, addLines, stereotypes, tags);
    } else {
        if (ctx.EXTENDS() != null) {
            for (TypeContext typeCtx : ctx.type()) {
                superTypesGenericTypes.add(this.type(typeCtx, typeParameterNames, "", importId, addLines));
            }
        }
        String className = ctx.qualifiedName().identifier().getText();
        classInstance = ClassInstance.createPersistent(this.repository, className);
        PackageInstance packageInstance = this.buildPackage(ctx.qualifiedName().packagePath());
        classInstance._package(packageInstance);
        packageInstance._childrenAdd(classInstance);
        String fullName = this.getQualifiedNameString(ctx.qualifiedName());
        ownerType = ImportStubInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.qualifiedName().identifier().getStart()), fullName, importId);
        LambdaContext lambdaContext = new LambdaContext(fullName.replace("::", "_"));
        MutableList<Constraint> constraints = this.constraints(classInstance, ctx.constraints(), importId, lambdaContext, addLines);
        this.propertyParser(ctx.classBody().properties(), properties, qualifiedProperties, typeParameterNames, multiplicityParameterNames, ownerType, importId, 0);
        classInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
        if (superTypesGenericTypes.isEmpty()) {
            isDirectSubTypeofAny = true;
            GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
            genericTypeInstance._rawTypeCoreInstance(this.processorSupport.package_getByUserPath(M3Paths.Any));
            superTypesGenericTypes.add(genericTypeInstance);
        }
        GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
        ClassInstance classType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Class);
        classifierGT._rawTypeCoreInstance(classType);
        GenericTypeInstance classifierGTTA = GenericTypeInstance.createPersistent(this.repository);
        classifierGTTA._rawTypeCoreInstance(classInstance);
        if (!typeParameterNames.isEmpty()) {
            MutableList<TypeParameter> typeParameters = Lists.mutable.of();
            MutableList<Pair<String, Boolean>> tps = typeParameterNames.zip(contravariants);
            for (Pair<String, Boolean> typeParam : tps) {
                TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParam.getOne());
                tp._contravariant(typeParam.getTwo());
                typeParameters.add(tp);
            }
            classInstance._typeParameters(typeParameters);
            MutableList<GenericType> typeArgs = Lists.mutable.of();
            for (String typeParamName : typeParameterNames) {
                TypeParameterInstance tp = TypeParameterInstance.createPersistent(this.repository, typeParamName);
                GenericTypeInstance gt = GenericTypeInstance.createPersistent(this.repository);
                gt._typeParameter(tp);
                typeArgs.add(gt);
            }
            classifierGTTA._typeArguments(typeArgs);
        }
        if (!multiplicityParameterNames.isEmpty()) {
            MutableList<Multiplicity> multParameters = Lists.mutable.of();
            for (String multiplicityParam : multiplicityParameterNames) {
                MultiplicityInstance mult = MultiplicityInstance.createPersistent(this.repository, null, null);
                mult._multiplicityParameter(multiplicityParam);
                multParameters.add(mult);
            }
            classInstance._multiplicityParameters(this.processMultiplicityParametersInstance(multiplicityParameterNames));
            classifierGTTA._multiplicityArguments(multParameters);
        }
        classifierGT._typeArguments(Lists.mutable.<GenericType>of(classifierGTTA));
        classInstance._classifierGenericType(classifierGT);
        if (properties.notEmpty()) {
            classInstance._properties(properties);
        }
        if (qualifiedProperties.notEmpty()) {
            classInstance._qualifiedProperties(qualifiedProperties);
        }
        classInstance._name(ctx.qualifiedName().identifier().getText());
        if (stereotypes != null) {
            classInstance._stereotypesCoreInstance(stereotypes);
        }
        if (tags != null) {
            classInstance._taggedValues(tags);
        }
        if (constraints.notEmpty()) {
            classInstance._constraints(constraints);
        }
        MutableList<Generalization> generalizations = Lists.mutable.empty();
        for (GenericType superType : superTypesGenericTypes) {
            GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, classInstance);
            generalizations.add(generalizationInstance);
        }
        classInstance._generalizations(generalizations);
        if (isDirectSubTypeofAny) {
            MilestoningClassProcessor.addMilestoningProperty(classInstance, this.context, this.processorSupport, this.repository);
        }
        return classInstance;
    }
}
Also used : TypeParameter(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter) Constraint(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.constraint.Constraint) Generalization(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Generalization) LambdaParamTypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LambdaParamTypeContext) TypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TypeContext) TreePathPropertyParameterTypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.TreePathPropertyParameterTypeContext) FunctionTypePureTypeContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionTypePureTypeContext) Multiplicity(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity) MultiplicityInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty) Property(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.Property) ClassInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance) Pair(org.eclipse.collections.api.tuple.Pair) GenericTypeInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance) GenericType(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType) TypeParameterInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameterInstance) TaggedValue(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue) PackageInstance(org.finos.legend.pure.m3.coreinstance.PackageInstance) GeneralizationInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ImportStubInstance(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStubInstance) QualifiedProperty(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)

Example 3 with TypeParameter

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter in project legend-pure by finos.

the class GenericTypeMatch method newGenericTypeMatch.

public static GenericTypeMatch newGenericTypeMatch(CoreInstance targetGenericType, CoreInstance valueGenericType, boolean covariant, NullMatchBehavior valueNullMatchBehavior, ParameterMatchBehavior targetParameterMatchBehavior, ParameterMatchBehavior valueParameterMatchBehavior, ProcessorSupport processorSupport) {
    // Null checks
    if (targetGenericType == null) {
        throw new IllegalArgumentException("Target generic type may not be null");
    }
    if (valueGenericType == null) {
        switch(getNullMatchBehavior(valueNullMatchBehavior)) {
            case MATCH_ANYTHING:
                {
                    return NULL_MATCH;
                }
            case MATCH_NOTHING:
                {
                    return null;
                }
            case ERROR:
                {
                    throw new RuntimeException("Value generic type may not be null");
                }
        }
    }
    // Identical generic types
    if (GenericType.genericTypesEqual(targetGenericType, valueGenericType, processorSupport)) {
        return EXACT_MATCH;
    }
    CoreInstance targetRawType = Instance.getValueForMetaPropertyToOneResolved(targetGenericType, M3Properties.rawType, processorSupport);
    CoreInstance valueRawType = Instance.getValueForMetaPropertyToOneResolved(valueGenericType, M3Properties.rawType, processorSupport);
    // Target generic type is not concrete (i.e., targetRawType == null)
    if (targetRawType == null) {
        switch(getParameterMatchBehavior(targetParameterMatchBehavior)) {
            case MATCH_ANYTHING:
                {
                    return NON_CONCRETE_MATCH;
                }
            case MATCH_CAUTIOUSLY:
                {
                    if (valueRawType == null) {
                        String targetTypeParameterName = GenericType.getTypeParameterName(targetGenericType);
                        String valueTypeParameterName = GenericType.getTypeParameterName(valueGenericType);
                        return (targetTypeParameterName != null) && targetTypeParameterName.equals(valueTypeParameterName) ? NON_CONCRETE_MATCH : null;
                    }
                    boolean willMatchParameterBinding = covariant ? Type.isBottomType(valueRawType, processorSupport) : Type.isTopType(valueRawType, processorSupport);
                    return willMatchParameterBinding ? NON_CONCRETE_MATCH : null;
                }
            case MATCH_NOTHING:
                {
                    return null;
                }
            case ERROR:
                {
                    throw new RuntimeException("Target generic type must be concrete, got: " + GenericType.print(targetGenericType, processorSupport));
                }
            default:
                {
                    throw new RuntimeException("Should not be possible");
                }
        }
    }
    // If the value generic type is not concrete (i.e., valueRawType == null), then it only matches Any (for covariant) or Nil (for contravariant)
    if (valueRawType == null) {
        switch(getParameterMatchBehavior(valueParameterMatchBehavior)) {
            case MATCH_ANYTHING:
                {
                    return NON_CONCRETE_MATCH;
                }
            case MATCH_CAUTIOUSLY:
                {
                    boolean anyParameterBindingWillMatch = covariant ? Type.isTopType(targetRawType, processorSupport) : Type.isBottomType(targetRawType, processorSupport);
                    return anyParameterBindingWillMatch ? NON_CONCRETE_MATCH : null;
                }
            case MATCH_NOTHING:
                {
                    return null;
                }
            case ERROR:
                {
                    throw new RuntimeException("Value generic type must be concrete, got: " + GenericType.print(valueGenericType, processorSupport));
                }
        }
    }
    // Both generic types are concrete, so we calculate a match between the raw types
    TypeMatch rawTypeMatch = TypeMatch.newTypeMatch(targetRawType, valueRawType, covariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior, processorSupport);
    if (rawTypeMatch == null) {
        return null;
    }
    if (Type.isBottomType(covariant ? valueRawType : targetRawType, processorSupport) || Type.isTopType(covariant ? targetRawType : valueRawType, processorSupport)) {
        return new GenericTypeMatch(rawTypeMatch);
    }
    ListIterable<? extends CoreInstance> targetTypeArguments = targetGenericType.getValueForMetaPropertyToMany(M3Properties.typeArguments);
    int typeParameterCount = targetTypeArguments.size();
    MutableList<GenericTypeMatch> typeArgumentMatches = FastList.newList(typeParameterCount);
    if (typeParameterCount > 0) {
        GenericTypeWithXArguments homogenizedTypeArguments = GenericType.resolveClassTypeParameterUsingInheritance(valueGenericType, targetGenericType, processorSupport);
        if (homogenizedTypeArguments == null) {
            return null;
        }
        ImmutableMap<String, CoreInstance> valueTypeArgumentsByParam = homogenizedTypeArguments.getArgumentsByParameterName();
        if (typeParameterCount != valueTypeArgumentsByParam.size()) {
            return null;
        }
        ListIterable<? extends CoreInstance> typeParameters = targetRawType.getValueForMetaPropertyToMany(M3Properties.typeParameters);
        for (int i = 0; i < typeParameterCount; i++) {
            CoreInstance typeParameter = typeParameters.get(i);
            boolean contravariant = PrimitiveUtilities.getBooleanValue(typeParameter.getValueForMetaPropertyToOne(M3Properties.contravariant), false);
            boolean paramCovariant = contravariant ? !covariant : covariant;
            CoreInstance valueTypeArgument = valueTypeArgumentsByParam.get(PrimitiveUtilities.getStringValue(typeParameter.getValueForMetaPropertyToOne(M3Properties.name)));
            GenericTypeMatch typeArgumentMatch = newGenericTypeMatch(targetTypeArguments.get(i), valueTypeArgument, paramCovariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior, processorSupport);
            if (typeArgumentMatch == null) {
                return null;
            }
            typeArgumentMatches.add(typeArgumentMatch);
        }
    }
    ListIterable<? extends CoreInstance> targetMultArguments = Instance.getValueForMetaPropertyToManyResolved(targetGenericType, M3Properties.multiplicityArguments, processorSupport);
    int multParameterCount = targetMultArguments.size();
    MutableList<MultiplicityMatch> multArgumentMatches = FastList.newList(multParameterCount);
    if (multParameterCount > 0) {
        GenericTypeWithXArguments homogenizedMultArguments = GenericType.resolveClassMultiplicityParameterUsingInheritance(valueGenericType, targetRawType, processorSupport);
        if (homogenizedMultArguments == null) {
            return null;
        }
        ListIterable<CoreInstance> valueMultArguments = homogenizedMultArguments.extractArgumentsAsMultiplicityParameters(processorSupport);
        if (multParameterCount != valueMultArguments.size()) {
            return null;
        }
        for (int i = 0; i < multParameterCount; i++) {
            MultiplicityMatch multArgumentMatch = MultiplicityMatch.newMultiplicityMatch(targetMultArguments.get(i), valueMultArguments.get(i), covariant, valueNullMatchBehavior, targetParameterMatchBehavior, valueParameterMatchBehavior);
            if (multArgumentMatch == null) {
                return null;
            }
            multArgumentMatches.add(multArgumentMatch);
        }
    }
    return new GenericTypeMatch(rawTypeMatch, typeArgumentMatches, multArgumentMatches);
}
Also used : MultiplicityMatch(org.finos.legend.pure.m3.navigation.multiplicity.MultiplicityMatch) GenericTypeWithXArguments(org.finos.legend.pure.m3.navigation.generictype.GenericTypeWithXArguments) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 4 with TypeParameter

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter in project legend-pure by finos.

the class Support method processOneLevelGenericType.

private static void processOneLevelGenericType(CoreInstance parameterGenericType, CoreInstance parameterMultiplicity, CoreInstance parameterValueGenericType, CoreInstance parameterValueMultiplicity, MutableMap<String, CoreInstance> inferredTypeParams, MutableMap<String, CoreInstance> inferredMultiplicityParams, ProcessorSupport processorSupport) {
    if (!Multiplicity.isMultiplicityConcrete(parameterMultiplicity)) {
        String multiplicityParam = Multiplicity.getMultiplicityParameter(parameterMultiplicity);
        CoreInstance currentMultiplicity = inferredMultiplicityParams.get(multiplicityParam);
        if (currentMultiplicity == null) {
            inferredMultiplicityParams.put(multiplicityParam, parameterValueMultiplicity);
        } else if (!Multiplicity.isMultiplicityConcrete(currentMultiplicity) && Multiplicity.isMultiplicityConcrete(parameterValueMultiplicity)) {
            inferredMultiplicityParams.put(multiplicityParam, parameterValueMultiplicity);
        } else if (!Multiplicity.multiplicitiesEqual(currentMultiplicity, parameterValueMultiplicity)) {
            inferredMultiplicityParams.put(multiplicityParam, Multiplicity.minSubsumingMultiplicity(currentMultiplicity, parameterValueMultiplicity, processorSupport));
        }
    }
    if (!GenericType.isGenericTypeConcrete(parameterGenericType)) {
        String typeParameter = GenericType.getTypeParameterName(parameterGenericType);
        CoreInstance currentGenericType = inferredTypeParams.get(typeParameter);
        if (currentGenericType == null) {
            inferredTypeParams.put(typeParameter, parameterValueGenericType);
        } else if (!GenericType.isGenericTypeConcrete(currentGenericType) && GenericType.isGenericTypeConcrete(parameterValueGenericType)) {
            inferredTypeParams.put(typeParameter, parameterValueGenericType);
        } else if (!GenericType.genericTypesEqual(currentGenericType, parameterValueGenericType, processorSupport)) {
            inferredTypeParams.put(typeParameter, GenericType.findBestCommonGenericType(Lists.immutable.with(currentGenericType, parameterValueGenericType), true, false, processorSupport));
        }
    } else if (FunctionType.isFunctionType(parameterGenericType, processorSupport)) {
        inferTypeParameterUsingResolvedFunctionType(parameterGenericType, parameterValueGenericType, inferredTypeParams, inferredMultiplicityParams, processorSupport);
    } else if (!Type.isBottomType(parameterValueGenericType.getValueForMetaPropertyToOne(M3Properties.rawType), processorSupport)) {
        ListIterable<? extends CoreInstance> typeArguments = Instance.getValueForMetaPropertyToManyResolved(parameterGenericType, M3Properties.typeArguments, processorSupport);
        int typeArgumentCount = typeArguments.size();
        if (typeArgumentCount > 0) {
            CoreInstance parameterRawType = Instance.getValueForMetaPropertyToOneResolved(parameterGenericType, M3Properties.rawType, processorSupport);
            ListIterable<? extends CoreInstance> typeParameters = Instance.getValueForMetaPropertyToManyResolved(parameterRawType, M3Properties.typeParameters, processorSupport);
            if (typeArgumentCount != typeParameters.size()) {
                throw new RuntimeException("Mismatch between the number of type parameters (" + typeParameters.size() + ") and the number of type arguments provided (" + typeArgumentCount + ")");
            }
            GenericTypeWithXArguments homogenizedTypeArgs = GenericType.resolveClassTypeParameterUsingInheritance(parameterValueGenericType, parameterGenericType, processorSupport);
            for (int i = 0; i < typeArgumentCount; i++) {
                CoreInstance typeArgument = typeArguments.get(i);
                CoreInstance typeParameter = typeParameters.get(i);
                String typeParameterNameInSource = PrimitiveUtilities.getStringValue(Instance.getValueForMetaPropertyToOneResolved(typeParameter, M3Properties.name, processorSupport));
                processOneLevelGenericType(typeArgument, parameterMultiplicity, homogenizedTypeArgs.getArgumentByParameterName(typeParameterNameInSource), parameterValueMultiplicity, inferredTypeParams, inferredMultiplicityParams, processorSupport);
            }
        }
    }
}
Also used : GenericTypeWithXArguments(org.finos.legend.pure.m3.navigation.generictype.GenericTypeWithXArguments) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 5 with TypeParameter

use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter in project legend-pure by finos.

the class FunctionExpressionValidator method validateFunctionExpression.

public static void validateFunctionExpression(Matcher matcher, ValidatorState validatorState, FunctionExpression instance, ModelRepository repository, ProcessorSupport processorSupport) throws PureCompilationException {
    Function function = (Function) ImportStub.withImportStubByPass(instance._funcCoreInstance(), processorSupport);
    if (function == null) {
        throw new RuntimeException("No function found to the expression:\n" + instance.print(""));
    }
    if ("new_Class_1__String_1__KeyExpression_MANY__T_1_".equals(function.getName()) || "new_Class_1__String_1__T_1_".equals(function.getName())) {
        NewValidator.validateNew(matcher, validatorState, instance, processorSupport);
    }
    if ("copy_T_1__String_1__KeyExpression_MANY__T_1_".equals(function.getName()) || "copy_T_1__String_1__T_1_".equals(function.getName())) {
        CopyValidator.validateCopy(matcher, validatorState, instance, processorSupport);
    }
    if ("extractEnumValue_Enumeration_1__String_1__T_1_".equals(function.getName())) {
        EnumValidator.validateEnum(instance, processorSupport);
    }
    if ("subType_Any_m__T_1__T_m_".equals(function.getName())) {
        SubTypeValidator.validateSubType(instance, processorSupport);
    }
    if (function.getName().startsWith("getAll_Class_1__")) {
        GetAllValidator.validate(instance, processorSupport);
    }
    if (function.getName().startsWith("getAllVersions_Class_1__")) {
        GetAllVersionsValidator.validate(instance, processorSupport);
    }
    if (function.getName().startsWith("getAllVersionsInRange_Class_1__")) {
        GetAllVersionsInRangeValidator.validate(instance, processorSupport);
    }
    MilestoningFunctionExpressionValidator.validateFunctionExpression(instance, function, repository, processorSupport);
    FunctionType functionType = (FunctionType) processorSupport.function_getFunctionType(ImportStub.withImportStubByPass(instance._funcCoreInstance(), processorSupport));
    ListIterable<? extends VariableExpression> parameters = (ListIterable<? extends VariableExpression>) functionType._parameters();
    ListIterable<? extends ValueSpecification> parametersValues = (ListIterable<? extends ValueSpecification>) instance._parametersValues();
    int i = 0;
    for (ValueSpecification inst : parametersValues) {
        Validator.validate(inst, validatorState, matcher, processorSupport);
        GenericType genericType = inst._genericType();
        Validator.validate(genericType, validatorState, matcher, processorSupport);
        if (genericType._rawTypeCoreInstance() != processorSupport.package_getByUserPath(M3Paths.Nil)) {
            ListIterable<? extends GenericType> funcP = (ListIterable<? extends GenericType>) parameters.get(i)._genericType()._typeArguments();
            GenericType givenInstanceGenericType;
            if (!funcP.isEmpty() && org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(parameters.get(i)._genericType(), processorSupport) && org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericTypeConcrete(inst._genericType(), processorSupport)) {
                // This line should actually return a Pair (including the resolved multiplicities)
                GenericTypeWithXArguments homogenizedTypeArgs = org.finos.legend.pure.m3.navigation.generictype.GenericType.resolveClassTypeParameterUsingInheritance(inst._genericType(), parameters.get(i)._genericType(), processorSupport);
                // The empty multiplicities bellow should come from the Pair
                givenInstanceGenericType = (GenericType) org.finos.legend.pure.m3.navigation.generictype.GenericType.makeTypeArgumentAsConcreteAsPossible(homogenizedTypeArgs.getGenericType(), homogenizedTypeArgs.getArgumentsByParameterName(), Maps.immutable.<String, CoreInstance>of(), processorSupport);
            } else {
                givenInstanceGenericType = inst._genericType();
            }
            ListIterable<? extends GenericType> bound = (ListIterable<? extends GenericType>) givenInstanceGenericType._typeArguments();
            if (funcP.size() > bound.size()) {
                throw new RuntimeException("Type Arguments mismatch of a function parameter. Function:'" + ImportStub.withImportStubByPass(instance._funcCoreInstance(), processorSupport).getName() + "'" + " / Param:'" + parameters.get(i)._name() + ":" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(parameters.get(i)._genericType(), processorSupport) + "' / Given Instance Type:'" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(inst._genericType(), processorSupport) + "'");
            }
            for (int k = 0; k < funcP.size(); k++) {
                if (ImportStub.withImportStubByPass(funcP.get(k)._rawTypeCoreInstance(), processorSupport) != null) {
                    Type typeArgument1 = (Type) ImportStub.withImportStubByPass(funcP.get(k)._rawTypeCoreInstance(), processorSupport);
                    if ("FunctionType".equals(typeArgument1.getClassifier().getName())) {
                        if (!org.finos.legend.pure.m3.navigation.generictype.GenericType.isGenericCompatibleWith(inst._genericType(), parameters.get(i)._genericType(), processorSupport)) {
                            throw new PureCompilationException(instance.getSourceInformation(), "typeArgument mismatch! Expected:" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(parameters.get(i)._genericType(), processorSupport) + " Found:" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(inst._genericType(), processorSupport));
                        }
                    } else {
                        Type type = (Type) ImportStub.withImportStubByPass(givenInstanceGenericType._rawTypeCoreInstance(), processorSupport);
                        ListIterable<? extends TypeParameter> typeParameters = type instanceof Class ? (ListIterable<? extends TypeParameter>) ((Class) type)._typeParameters() : Lists.fixedSize.<TypeParameter>empty();
                        boolean covariant = org.finos.legend.pure.m3.navigation.typeparameter.TypeParameter.isCovariant(typeParameters.get(k));
                        Type typeArgument2 = (Type) ImportStub.withImportStubByPass(bound.get(k)._rawTypeCoreInstance(), processorSupport);
                        if (typeArgument2 != null && (covariant ? !org.finos.legend.pure.m3.navigation.type.Type.subTypeOf(typeArgument2, typeArgument1, processorSupport) : !org.finos.legend.pure.m3.navigation.type.Type.subTypeOf(typeArgument1, typeArgument2, processorSupport))) {
                            if (!("Any".equals(typeArgument1.getName()) && "FunctionType".equals(typeArgument2.getClassifier().getName()))) {
                                throw new PureCompilationException(instance.getSourceInformation(), function.getName() + " " + " / typeArgument mismatch! Expected:" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(parameters.get(i)._genericType(), processorSupport) + " Found:" + org.finos.legend.pure.m3.navigation.generictype.GenericType.print(inst._genericType(), processorSupport));
                            }
                        }
                    }
                }
            }
        }
        i++;
    }
}
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) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) VariableExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression) ListIterable(org.eclipse.collections.api.list.ListIterable) Function(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.Function) 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) GenericTypeWithXArguments(org.finos.legend.pure.m3.navigation.generictype.GenericTypeWithXArguments) Class(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Aggregations

CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)5 GenericType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericType)4 FunctionType (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionType)3 GenericTypeWithXArguments (org.finos.legend.pure.m3.navigation.generictype.GenericTypeWithXArguments)3 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)3 Pair (org.eclipse.collections.api.tuple.Pair)2 PackageInstance (org.finos.legend.pure.m3.coreinstance.PackageInstance)2 TaggedValue (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.extension.TaggedValue)2 QualifiedProperty (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.function.property.QualifiedProperty)2 Multiplicity (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.Multiplicity)2 MultiplicityInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.multiplicity.MultiplicityInstance)2 Generalization (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.Generalization)2 GeneralizationInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.relationship.GeneralizationInstance)2 Class (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.Class)2 ClassInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.ClassInstance)2 GenericTypeInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.GenericTypeInstance)2 TypeParameter (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameter)2 TypeParameterInstance (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.generics.TypeParameterInstance)2 VariableExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.VariableExpression)2 PrimitiveCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance)2