Skip to content

Commit

Permalink
fixed bugs how to use kinds by other kinds
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesMeierSE committed Dec 10, 2024
1 parent db3192c commit 0ae76c0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
16 changes: 7 additions & 9 deletions packages/typir/src/kinds/class/class-kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
******************************************************************************/

import { assertUnreachable } from 'langium';
import { InferenceRuleNotApplicable } from '../../services/inference.js';
import { TypeInitializer } from '../../initialization/type-initializer.js';
import { TypeReference, resolveTypeSelector } from '../../initialization/type-reference.js';
import { TypeSelector } from '../../initialization/type-selector.js';
import { InferenceRuleNotApplicable } from '../../services/inference.js';
import { TypirServices } from '../../typir.js';
import { TypeCheckStrategy } from '../../utils/utils-type-comparison.js';
import { assertTrue, assertType, toArray } from '../../utils/utils.js';
import { CreateFunctionTypeDetails, FunctionKind, FunctionKindName, isFunctionKind } from '../function/function-kind.js';
import { CreateFunctionTypeDetails, FunctionPredefinedService } from '../function/function-kind.js';
import { Kind, isKind } from '../kind.js';
import { ClassTypeInitializer } from './class-initializer.js';
import { ClassType, isClassType } from './class-type.js';
import { TopClassKind, TopClassKindName, TopClassTypeDetails, isTopClassKind } from './top-class-kind.js';
import { TopClassType } from './top-class-type.js';
import { ClassType, isClassType } from './class-type.js';
import { ClassTypeInitializer } from './class-initializer.js';

export interface ClassKindOptions {
typing: 'Structural' | 'Nominal', // JS classes are nominal, TS structures are structural
Expand Down Expand Up @@ -143,7 +143,7 @@ export class ClassKind implements Kind, ClassFactoryService {
.sort() // the order of fields does not matter, therefore we need a stable order to make the identifiers comparable
.join(',');
// methods
const functionKind = this.getMethodKind();
const functionKind = this.getMethodFactory();
const methods: string = typeDetails.methods
.map(createMethodDetails => {
return functionKind.calculateIdentifier(createMethodDetails); // reuse the Identifier for Functions here!
Expand Down Expand Up @@ -180,10 +180,8 @@ export class ClassKind implements Kind, ClassFactoryService {
return `${this.getIdentifierPrefix()}${typeDetails.className}`;
}

getMethodKind(): FunctionKind {
// ensure, that Typir uses the predefined 'function' kind for methods
const kind = this.services.kinds.get(FunctionKindName);
return isFunctionKind(kind) ? kind : new FunctionKind(this.services);
getMethodFactory(): FunctionPredefinedService {
return this.services.factory.functions;
}

getOrCreateTopClassType(typeDetails: TopClassTypeDetails): TopClassType {
Expand Down
2 changes: 1 addition & 1 deletion packages/typir/src/kinds/class/class-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class ClassType extends Type {

// resolve methods
this.methods = typeDetails.methods.map(method => <MethodDetails>{
type: new TypeReference(kind.getMethodKind().create(method), kind.services),
type: new TypeReference(kind.getMethodFactory().create(method), kind.services),
});
const refMethods = this.methods.map(m => m.type);
// the uniqueness of methods can be checked with the predefined UniqueMethodValidation below
Expand Down
1 change: 1 addition & 0 deletions packages/typir/src/kinds/function/function-kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export type InferFunctionCall<T = unknown> = {
export interface FunctionPredefinedService {
create<T>(typeDetails: CreateFunctionTypeDetails<T>): TypeInitializer<FunctionType>;
get(typeDetails: FunctionTypeDetails): TypeReference<FunctionType>;
calculateIdentifier(typeDetails: FunctionTypeDetails): string;
}

/**
Expand Down
12 changes: 5 additions & 7 deletions packages/typir/src/services/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
******************************************************************************/

import { Type } from '../graph/type-node.js';
import { FunctionKind, FunctionKindName, isFunctionKind, NO_PARAMETER_NAME } from '../kinds/function/function-kind.js';
import { TypirServices } from '../typir.js';
import { TypeInitializer } from '../initialization/type-initializer.js';
import { FunctionPredefinedService, NO_PARAMETER_NAME } from '../kinds/function/function-kind.js';
import { TypirServices } from '../typir.js';
import { NameTypePair, TypeInitializers } from '../utils/utils-definitions.js';
import { toArray } from '../utils/utils.js';

Expand Down Expand Up @@ -152,7 +152,7 @@ export class DefaultOperatorManager implements OperatorManager {

createGeneric<T>(typeDetails: GenericOperatorDetails<T>): TypeInitializer<Type> {
// define/register the wanted operator as "special" function
const functionKind = this.getFunctionKind();
const functionKind = this.getFunctionFactory();

// create the operator as type of kind 'function'
const newOperatorType = functionKind.create({
Expand All @@ -174,9 +174,7 @@ export class DefaultOperatorManager implements OperatorManager {
return newOperatorType as unknown as TypeInitializer<Type>;
}

protected getFunctionKind(): FunctionKind {
// ensure, that Typir uses the predefined 'function' kind
const kind = this.services.kinds.get(FunctionKindName);
return isFunctionKind(kind) ? kind : new FunctionKind(this.services);
protected getFunctionFactory(): FunctionPredefinedService {
return this.services.factory.functions;
}
}

0 comments on commit 0ae76c0

Please sign in to comment.