UNPKG

typir

Version:

General purpose type checking library

73 lines 3.4 kB
/****************************************************************************** * Copyright 2024 TypeFox GmbH * This program and the accompanying materials are made available under the * terms of the MIT License, which is available in the project root. ******************************************************************************/ import { isType, Type } from '../../graph/type-node.js'; import { TypeEqualityProblem } from '../../services/equality.js'; import { isSubTypeProblem } from '../../services/subtype.js'; import { checkValueForConflict, createKindConflict } from '../../utils/utils-type-comparison.js'; import { isMultiplicityKind } from './multiplicity-kind.js'; export class MultiplicityType extends Type { constructor(kind, identifier, typeDetails) { super(identifier, typeDetails); this.kind = kind; this.constrainedType = typeDetails.constrainedType; this.lowerBound = typeDetails.lowerBound; this.upperBound = typeDetails.upperBound; this.defineTheInitializationProcessOfThisType({}); // TODO preconditions } getName() { return `${this.constrainedType.getName()}${this.kind.printRange(this.getLowerBound(), this.getUpperBound())}`; } getUserRepresentation() { return this.getName(); } analyzeTypeEqualityProblems(otherType) { if (isMultiplicityKind(otherType)) { const conflicts = []; // check the multiplicities conflicts.push(...checkValueForConflict(this.getLowerBound(), this.getLowerBound(), 'lower bound')); conflicts.push(...checkValueForConflict(this.getUpperBound(), this.getUpperBound(), 'upper bound')); // check the constrained type const constrainedTypeConflict = this.kind.services.Equality.getTypeEqualityProblem(this.getConstrainedType(), this.getConstrainedType()); if (constrainedTypeConflict !== undefined) { conflicts.push(constrainedTypeConflict); } return conflicts; } else { return [{ $problem: TypeEqualityProblem, type1: this, type2: otherType, subProblems: [createKindConflict(otherType, this)], }]; } } analyzeSubTypeProblems(subType, superType) { const conflicts = []; // check the multiplicities conflicts.push(...checkValueForConflict(subType.getLowerBound(), superType.getLowerBound(), 'lower bound', this.kind.isBoundGreaterEquals)); conflicts.push(...checkValueForConflict(subType.getUpperBound(), superType.getUpperBound(), 'upper bound', this.kind.isBoundGreaterEquals)); // check the constrained type const constrainedTypeConflict = this.kind.services.Subtype.getSubTypeResult(subType.getConstrainedType(), superType.getConstrainedType()); if (isSubTypeProblem(constrainedTypeConflict)) { conflicts.push(constrainedTypeConflict); } return conflicts; } getConstrainedType() { return this.constrainedType; } getLowerBound() { return this.lowerBound; } getUpperBound() { return this.upperBound; } } export function isMultiplicityType(type) { return isType(type) && isMultiplicityKind(type.kind); } //# sourceMappingURL=multiplicity-type.js.map