-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from jvalue/import-cycle-evaluate-expression
Remove Import Cycle in type-inference.ts
- Loading branch information
Showing
12 changed files
with
180 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
libs/language-server/src/lib/ast/wrappers/util/value-type-util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { strict as assert } from 'assert'; | ||
|
||
import { | ||
type AtomicValueType, | ||
isAtomicValueType, | ||
} from '../value-type/atomic-value-type'; | ||
import { | ||
type PrimitiveValueType, | ||
isPrimitiveValueType, | ||
} from '../value-type/primitive'; | ||
import { type ValueType } from '../value-type/value-type'; | ||
|
||
type ValuetypeHierarchyStack = [PrimitiveValueType, ...AtomicValueType[]]; | ||
|
||
export function getValuetypeHierarchyStack( | ||
valueType: ValueType, | ||
): ValuetypeHierarchyStack { | ||
if (isPrimitiveValueType(valueType)) { | ||
return [valueType]; | ||
} else if (isAtomicValueType(valueType)) { | ||
const supertype = valueType.getSupertype(); | ||
assert(supertype !== undefined); | ||
return [...getValuetypeHierarchyStack(supertype), valueType]; | ||
} | ||
throw new Error( | ||
'Should be unreachable, encountered an unknown kind of value type', | ||
); | ||
} | ||
|
||
export function pickCommonPrimitiveValuetype( | ||
primitiveValuetypes: PrimitiveValueType[], | ||
): PrimitiveValueType | undefined { | ||
assert(primitiveValuetypes.length > 0); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
let resultingType: PrimitiveValueType = primitiveValuetypes[0]!; | ||
for (let i = 1; i < primitiveValuetypes.length; ++i) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const currentType = primitiveValuetypes[i]!; | ||
|
||
if (currentType.isConvertibleTo(resultingType)) { | ||
continue; | ||
} | ||
|
||
if (resultingType.isConvertibleTo(currentType)) { | ||
// Pick the more general type as a result | ||
resultingType = currentType; | ||
continue; | ||
} | ||
|
||
// Unable to convert the value types into each other, so there is no common primitive value type | ||
return undefined; | ||
} | ||
return resultingType; | ||
} | ||
|
||
export function pickCommonAtomicValueType( | ||
stacks: ValuetypeHierarchyStack[], | ||
): PrimitiveValueType | AtomicValueType | undefined { | ||
const minimumStackLength = Math.min(...stacks.map((stack) => stack.length)); | ||
|
||
let resultingType: PrimitiveValueType | AtomicValueType | undefined = | ||
undefined; | ||
for (let stackLevel = 1; stackLevel < minimumStackLength; ++stackLevel) { | ||
const typesOfCurrentLevel: (PrimitiveValueType | AtomicValueType)[] = | ||
stacks.map( | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
(stack) => stack[stackLevel]!, | ||
); | ||
|
||
if (!areAllTypesEqual(typesOfCurrentLevel)) { | ||
// Return the common value type of the previous level | ||
return resultingType; | ||
} | ||
|
||
// Pick any type of the current level since they are all equal | ||
resultingType = typesOfCurrentLevel[0]; | ||
} | ||
return resultingType; | ||
} | ||
|
||
export function areAllTypesEqual(types: ValueType[]): boolean { | ||
for (let i = 1; i < types.length; i++) { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const current = types[i - 1]!; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const afterCurrent = types[i]!; | ||
if (!current.equals(afterCurrent)) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
64 changes: 64 additions & 0 deletions
64
libs/language-server/src/lib/ast/wrappers/value-type/abstract-value-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { type InternalValueRepresentation } from '../../expressions'; | ||
|
||
import { type ValueType, type ValueTypeVisitor } from './value-type'; | ||
|
||
export abstract class AbstractValueType<I extends InternalValueRepresentation> | ||
implements ValueType<I> | ||
{ | ||
abstract acceptVisitor<R>(visitor: ValueTypeVisitor<R>): R; | ||
|
||
isSubtypeOf(other: ValueType): boolean { | ||
let othersSupertype = other.getSupertype(); | ||
while (othersSupertype !== undefined) { | ||
if (othersSupertype === this) { | ||
return true; | ||
} | ||
othersSupertype = othersSupertype.getSupertype(); | ||
} | ||
return false; | ||
} | ||
|
||
getSupertype(): ValueType | undefined { | ||
if (this.hasSupertypeCycle()) { | ||
return undefined; | ||
} | ||
return this.doGetSupertype(); | ||
} | ||
|
||
protected abstract doGetSupertype(): ValueType | undefined; | ||
|
||
abstract equals(target: ValueType): boolean; | ||
|
||
abstract isAllowedAsRuntimeParameter(): boolean; | ||
|
||
abstract isConvertibleTo(target: ValueType): boolean; | ||
|
||
isReferenceableByUser(): boolean { | ||
return false; | ||
} | ||
|
||
abstract isInternalValueRepresentation( | ||
operandValue: InternalValueRepresentation | undefined, | ||
): operandValue is I; | ||
|
||
abstract getName(): string; | ||
|
||
hasSupertypeCycle(visited: ValueType[] = []): boolean { | ||
const cycleDetected = visited.some((v) => v.equals(this)); | ||
if (cycleDetected) { | ||
return true; | ||
} | ||
visited.push(this); | ||
|
||
const supertype = this.doGetSupertype(); | ||
if (supertype === undefined) { | ||
return false; | ||
} | ||
|
||
return supertype.hasSupertypeCycle(visited); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.