use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method qualifiedProperty.
private void qualifiedProperty(QualifiedPropertyContext ctx, MutableList<QualifiedProperty<? extends CoreInstance>> qualifiedProperties, MutableList<String> typeParameterNames, MutableList<String> multiplicityParameterNames, ImportStub isOwner, ImportGroup importId, boolean addLines, int qualifiedPropertyIndex) {
ListIterable<CoreInstance> stereotypes = (ctx.stereotypes() == null) ? null : stereotypes(ctx.stereotypes(), importId);
ListIterable<TaggedValue> tags = (ctx.taggedValues() == null) ? null : taggedValues(ctx.taggedValues(), importId);
MutableList<VariableExpression> vars = Lists.mutable.of();
ListIterable<ValueSpecification> code = Lists.fixedSize.empty();
GenericType genericType = this.type(ctx.propertyReturnType().type(), typeParameterNames, "", importId, addLines);
Multiplicity multiplicity = this.buildMultiplicity(ctx.propertyReturnType().multiplicity().multiplicityArgument());
String propertyName = ctx.identifier().getText();
final String qualifiedPropertyName = propertyName + "_" + qualifiedPropertyIndex;
if (ctx.qualifiedPropertyBody() != null) {
if (ctx.qualifiedPropertyBody().functionVariableExpression() != null) {
ListIterate.collect(ctx.qualifiedPropertyBody().functionVariableExpression(), fveCtx -> functionVariableExpression(fveCtx, typeParameterNames, importId, ""), vars);
}
if (ctx.qualifiedPropertyBody().codeBlock() != null) {
LambdaContext lambdaContext = new LambdaContext(isOwner._idOrPath().replace("::", "_") + "_" + qualifiedPropertyName);
code = this.codeBlock(ctx.qualifiedPropertyBody().codeBlock(), typeParameterNames, importId, lambdaContext, addLines, "");
}
}
GenericTypeInstance variableGenericType = GenericTypeInstance.createPersistent(this.repository);
variableGenericType._rawTypeCoreInstance(isOwner);
if (typeParameterNames.notEmpty()) {
MutableList<GenericType> typeArgs = typeParameterNames.collect(n -> GenericTypeInstance.createPersistent(this.repository)._typeParameter(TypeParameterInstance.createPersistent(this.repository, n)));
variableGenericType._typeArguments(typeArgs);
}
if (multiplicityParameterNames.notEmpty()) {
MutableList<Multiplicity> multParameters = multiplicityParameterNames.collect(n -> MultiplicityInstance.createPersistent(this.repository, null, null)._multiplicityParameter(n));
variableGenericType._multiplicityArguments(multParameters);
}
VariableExpressionInstance vei = VariableExpressionInstance.createPersistent(this.repository, variableGenericType, this.getPureOne(), "this");
GenericTypeInstance ngt = GenericTypeInstance.createPersistent(this.repository);
CoreInstance rawType = genericType._rawTypeCoreInstance();
if (rawType != null) {
if (rawType instanceof ImportStub) {
ImportStub gtis = (ImportStub) rawType;
ImportStubInstance is = ImportStubInstance.createPersistent(this.repository, gtis.getSourceInformation(), gtis._idOrPath(), gtis._importGroup());
// ImportStubInstance is = ImportStubInstance.createPersistent(this.repository, gtis.getSourceInformation(), ((ImportStubInstance)gtis)._idOrPathAsCoreInstance().getName(), (ImportGroup)gtis._importGroup());
ngt._rawTypeCoreInstance(is);
} else {
ngt._rawTypeCoreInstance(rawType);
}
}
if (!genericType._typeArguments().isEmpty()) {
ngt._typeArguments(genericType._typeArguments());
}
ngt._typeParameter(genericType._typeParameter());
if (!genericType._multiplicityArguments().isEmpty()) {
ngt._multiplicityArguments(genericType._multiplicityArguments());
}
QualifiedPropertyInstance qpi = QualifiedPropertyInstance.createPersistent(this.repository, qualifiedPropertyName, this.sourceInformation.getPureSourceInformation(ctx.identifier().getStart(), ctx.identifier().getStart(), ctx.getStop()), genericType, multiplicity, null);
qpi._name(propertyName);
qpi._functionName(propertyName);
qpi._expressionSequence(code);
qpi._stereotypesCoreInstance(stereotypes);
qpi._taggedValues(tags);
FunctionTypeInstance ft = FunctionTypeInstance.createPersistent(this.repository, qpi.getSourceInformation(), multiplicity, ngt);
if (!typeParameterNames.isEmpty()) {
ft._typeParameters(this.processTypeParametersInstance(this.repository, typeParameterNames));
}
ft._parameters(Lists.mutable.<VariableExpression>of(vei).withAll(vars));
GenericTypeInstance ftGenericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
ftGenericTypeInstance._rawTypeCoreInstance(ft);
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstance._rawTypeCoreInstance(this.processorSupport.package_getByUserPath(M3Paths.QualifiedProperty));
genericTypeInstance._typeArguments(Lists.fixedSize.<GenericType>of(ftGenericTypeInstance));
qpi._classifierGenericType(genericTypeInstance);
qualifiedProperties.add(qpi);
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method lambdaPipe.
private Any lambdaPipe(LambdaPipeContext ctx, Token firstToken, ListIterable<VariableExpression> params, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
Token lambdaStartToken = firstToken != null ? firstToken : ctx.PIPE().getSymbol();
ListIterable<ValueSpecification> block = codeBlock(ctx.codeBlock(), typeParametersNames, importId, lambdaContext, addLines, spacePlusTabs(space, 6));
FunctionTypeInstance signature = FunctionTypeInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(lambdaStartToken), null, null);
if (Iterate.notEmpty(params)) {
signature._parameters(params);
}
// Note: we cannot set the function of the signature FunctionType, as this can cause stack overflow if serializing to M4
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
Type type = (Type) this.processorSupport.package_getByUserPath(M3Paths.LambdaFunction);
genericTypeInstance._rawTypeCoreInstance(type);
GenericTypeInstance genericTypeInstanceTa = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstanceTa._rawTypeCoreInstance(signature);
genericTypeInstance._typeArguments(Lists.mutable.<GenericType>of(genericTypeInstanceTa));
LambdaFunctionInstance lambdaFunction = LambdaFunctionInstance.createPersistent(this.repository, lambdaContext.getLambdaFunctionUniqueName(), this.sourceInformation.getPureSourceInformation(lambdaStartToken));
lambdaFunction._classifierGenericType(genericTypeInstance);
lambdaFunction._expressionSequence(block);
return wrapFlag ? this.doWrap(lambdaFunction, lambdaStartToken) : lambdaFunction;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method unitParser.
/**
* Helps build the unitInstance for any canonical and non-canonical units and returns the parsed unitInstance.
*/
private UnitInstance unitParser(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureExprContext ctx, ImportGroup importId, MeasureInstance measureInstance, org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.MeasureDefinitionContext mctx) throws PureParserException {
UnitInstance unitInstance;
MutableList<GenericType> superTypesGenericTypes = Lists.mutable.empty();
this.checkExists(ctx.qualifiedName().packagePath(), ctx.qualifiedName().identifier(), null);
String unitName = mctx.qualifiedName().identifier().getText().concat(this.tilde).concat(ctx.qualifiedName().identifier().getText());
unitInstance = UnitInstance.createPersistent(this.repository, unitName);
PackageInstance packageInstance = buildPackage(Lists.mutable.withAll(mctx.qualifiedName().packagePath() == null ? Lists.mutable.empty() : ListAdapter.adapt(mctx.qualifiedName().packagePath().identifier())));
unitInstance._package(packageInstance);
packageInstance._childrenAdd(unitInstance);
unitInstance.setSourceInformation(this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.qualifiedName().identifier().getStart(), ctx.getStop()));
GenericTypeInstance classifierGT = GenericTypeInstance.createPersistent(this.repository);
ClassInstance unitType = (ClassInstance) this.processorSupport.package_getByUserPath(M3Paths.Unit);
classifierGT._rawTypeCoreInstance(unitType);
unitInstance._classifierGenericType(classifierGT);
unitInstance._name(unitName);
if (superTypesGenericTypes.isEmpty()) {
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
// set unit super type to be its measure (Kilogram -> Mass)
genericTypeInstance._rawTypeCoreInstance(measureInstance);
superTypesGenericTypes.add(genericTypeInstance);
}
MutableList<Generalization> generalizations = Lists.mutable.empty();
for (GenericType superType : superTypesGenericTypes) {
GeneralizationInstance generalizationInstance = GeneralizationInstance.createPersistent(this.repository, superType, unitInstance);
generalizations.add(generalizationInstance);
}
unitInstance._generalizations(generalizations);
unitInstance._measure(measureInstance);
// prepare lambda instance for the conversion function
String fullName = this.getQualifiedNameString(ctx.qualifiedName());
LambdaContext lambdaContext = new LambdaContext(fullName.replace("::", "_"));
MutableList<String> typeParametersNames = Lists.mutable.empty();
FunctionTypeInstance signature = FunctionTypeInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.unitExpr().getStart(), ctx.unitExpr().getStart(), ctx.unitExpr().getStop()), null, null);
// prepare params
VariableExpression expr = this.lambdaParam(null, ctx.unitExpr().identifier(), typeParametersNames, "", importId);
expr._multiplicity(this.getPureOne());
expr._functionTypeOwner(signature);
GenericTypeInstance paramGenericType = GenericTypeInstance.createPersistent(this.repository);
CoreInstance paramType = this.processorSupport.package_getByUserPath(M3Paths.Number);
paramGenericType._rawTypeCoreInstance(paramType);
expr._genericType(paramGenericType);
signature._parameters(Lists.mutable.with(expr));
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
Type type = (Type) this.processorSupport.package_getByUserPath(M3Paths.LambdaFunction);
genericTypeInstance._rawTypeCoreInstance(type);
GenericTypeInstance genericTypeInstanceTa = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstanceTa._rawTypeCoreInstance(signature);
genericTypeInstance._typeArguments(Lists.mutable.<GenericType>of(genericTypeInstanceTa));
LambdaFunctionInstance lambdaFunctionInstance = LambdaFunctionInstance.createPersistent(this.repository, lambdaContext.getLambdaFunctionUniqueName(), this.sourceInformation.getPureSourceInformation(ctx.unitExpr().ARROW().getSymbol()));
lambdaFunctionInstance._classifierGenericType(genericTypeInstance);
signature._functionAdd(lambdaFunctionInstance);
ListIterable<ValueSpecification> block = this.codeBlock(ctx.unitExpr().codeBlock(), typeParametersNames, importId, lambdaContext, addLines, tabs(6));
lambdaFunctionInstance._expressionSequence(block);
unitInstance._conversionFunction(lambdaFunctionInstance);
return unitInstance;
}
use of org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.type.FunctionTypeInstance in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method type.
public GenericType type(TypeContext ctx, MutableList<String> typeParametersNames, String space, ImportGroup importId, boolean addLines) {
if (ctx.qualifiedName() != null) {
ListIterable<GenericType> renderedTypeArguments = this.typeArguments(ctx.typeArguments(), typeParametersNames, importId, addLines);
ListIterable<Multiplicity> renderedMultiplicityArguments = this.multiplicityArguments(ctx.multiplicityArguments());
return this.processType(ctx.qualifiedName(), typeParametersNames, renderedTypeArguments, renderedMultiplicityArguments, importId);
}
if (ctx.unitName() != null) {
return this.processUnitType(ctx.unitName(), importId);
}
GenericType returnType = this.type(ctx.type(), typeParametersNames, spacePlusTabs(space, 5), importId, addLines);
Multiplicity returnMultiplicity = this.buildMultiplicity(ctx.multiplicity().multiplicityArgument());
SourceInformation sourceInfo = this.sourceInformation.getPureSourceInformation(ctx.getStart(), ctx.getStart(), ctx.getStop());
FunctionTypeInstance functionTypeInstance = FunctionTypeInstance.createPersistent(this.repository, sourceInfo, returnMultiplicity, returnType);
MutableList<VariableExpression> params = ListIterate.collect(ctx.functionTypePureType(), fCtx -> typeFunctionTypePureType(fCtx, typeParametersNames, space, importId, addLines));
if (params.notEmpty()) {
functionTypeInstance._parameters(params);
}
GenericTypeInstance genericTypeInstance = GenericTypeInstance.createPersistent(this.repository);
genericTypeInstance._rawTypeCoreInstance(functionTypeInstance);
return genericTypeInstance;
}
Aggregations