idlize

Форк
0
/
Materialized.ts 
154 строки · 5.2 Кб
1
/*
2
 * Copyright (c) 2024 Huawei Device Co., Ltd.
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at
6
 *
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15

16
import * as ts from "typescript"
17
import { ArgConvertor, RetConvertor } from "./Convertors"
18
import { Field, Method, MethodModifier, Type } from "./LanguageWriters"
19
import { PeerMethod } from "./PeerMethod"
20
import { identName } from "../util"
21
import { PeerClassBase } from "./PeerClass"
22
import { DeclarationTarget, PrimitiveType } from "./DeclarationTable"
23
import { ImportFeature } from "./ImportsCollector"
24
import { PeerGeneratorConfig } from "./PeerGeneratorConfig"
25
import { isBuilderClass } from "./BuilderClass"
26

27
export function checkTSDeclarationMaterialized(declaration: ts.Declaration): boolean {
28
    return (ts.isInterfaceDeclaration(declaration) || ts.isClassDeclaration(declaration)) && isMaterialized(declaration)
29
}
30

31
export function checkDeclarationTargetMaterialized(declaration: DeclarationTarget): boolean {
32
    return !(declaration instanceof PrimitiveType) && (ts.isInterfaceDeclaration(declaration) || ts.isClassDeclaration(declaration)) && isMaterialized(declaration)
33
}
34

35
export function isMaterialized(declaration: ts.InterfaceDeclaration | ts.ClassDeclaration): boolean {
36

37
    const name = identName(declaration)!
38

39
    if (PeerGeneratorConfig.isMaterializedIgnored(name)) {
40
        return false;
41
    }
42

43
    if (isBuilderClass(declaration)) {
44
        return false
45
    }
46

47
    // TODO: parse Builder classes separatly
48

49
    // A materialized class is a class or an interface with methods
50
    // excluding components and related classes
51
    return (ts.isClassDeclaration(declaration) && declaration.members.some(ts.isMethodDeclaration))
52
        || (ts.isInterfaceDeclaration(declaration) && declaration.members.some(ts.isMethodSignature))
53
}
54

55
export class MaterializedField {
56
    constructor(
57
        public declarationTarget: DeclarationTarget,
58
        public argConvertor: ArgConvertor,
59
        public retConvertor: RetConvertor,
60
        public field: Field
61
    ) { }
62
}
63

64
export class MaterializedMethod extends PeerMethod {
65
    constructor(
66
        originalParentName: string,
67
        declarationTargets: DeclarationTarget[],
68
        argConvertors: ArgConvertor[],
69
        retConvertor: RetConvertor,
70
        isCallSignature: boolean,
71
        method: Method
72
    ) {
73
        super(originalParentName, declarationTargets, argConvertors, retConvertor, isCallSignature, false, method)
74
    }
75

76
    override get peerMethodName() {
77
        return this.overloadedName
78
    }
79

80
    override get toStringName(): string {
81
        switch (this.method.name) {
82
            case "ctor": return `new ${this.originalParentName}`
83
            case "destructor": return `delete ${this.originalParentName}`
84
            default: return super.toStringName
85
        }
86
    }
87

88
    override get dummyReturnValue(): string | undefined {
89
        if (this.method.name === "ctor") return `(void*) 100`
90
        if (this.method.name === "getFinalizer") return `fnPtr<KNativePointer>(dummyClassFinalizer)`
91
        if (this.method.modifiers?.includes(MethodModifier.STATIC)) return `(void*) 300`
92
        return undefined;
93
    }
94

95
    override get receiverType(): string {
96
        return `${this.originalParentName}Peer*`
97
    }
98

99
    override get apiCall(): string {
100
        return "GetAccessors()"
101
    }
102

103
    override get apiKind(): string {
104
        return "Accessor"
105
    }
106

107
    override generateReceiver(): { argName: string; argType: string } | undefined {
108
        if (!this.hasReceiver()) return undefined
109
        return {
110
            argName: 'peer',
111
            argType: `${this.originalParentName}Peer*`
112
        }
113
    }
114

115
    tsReturnType(): Type | undefined {
116
        const returnType = this.method.signature.returnType
117
        return this.hasReceiver() && returnType.name === this.originalParentName ? Type.This : returnType
118
    }
119
}
120

121
export class SuperElement {
122
    constructor(
123
        public readonly name: string,
124
        public readonly generics?: string[]
125
    ) { }
126
}
127

128
export class MaterializedClass implements PeerClassBase {
129
    constructor(
130
        public readonly className: string,
131
        public readonly isInterface: boolean,
132
        public readonly superClass: SuperElement | undefined,
133
        public readonly generics: string[] | undefined,
134
        public readonly fields: MaterializedField[],
135
        public readonly ctor: MaterializedMethod,
136
        public readonly finalizer: MaterializedMethod,
137
        public readonly importFeatures: ImportFeature[],
138
        public methods: MaterializedMethod[],
139
    ) {
140
        PeerMethod.markOverloads(methods)
141
    }
142

143
    getComponentName(): string {
144
        return this.className
145
    }
146

147
    setGenerationContext(context: string| undefined): void {
148
       // TODO: set generation context!
149
    }
150

151
    generatedName(isCallSignature: boolean): string{
152
        return this.className
153
    }
154
}
155

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.