idlize
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
16import * as ts from "typescript"
17import { ArgConvertor, RetConvertor } from "./Convertors"
18import { Field, Method, MethodModifier, Type } from "./LanguageWriters"
19import { PeerMethod } from "./PeerMethod"
20import { identName } from "../util"
21import { PeerClassBase } from "./PeerClass"
22import { DeclarationTarget, PrimitiveType } from "./DeclarationTable"
23import { ImportFeature } from "./ImportsCollector"
24import { PeerGeneratorConfig } from "./PeerGeneratorConfig"
25import { isBuilderClass } from "./BuilderClass"
26
27export function checkTSDeclarationMaterialized(declaration: ts.Declaration): boolean {
28return (ts.isInterfaceDeclaration(declaration) || ts.isClassDeclaration(declaration)) && isMaterialized(declaration)
29}
30
31export function checkDeclarationTargetMaterialized(declaration: DeclarationTarget): boolean {
32return !(declaration instanceof PrimitiveType) && (ts.isInterfaceDeclaration(declaration) || ts.isClassDeclaration(declaration)) && isMaterialized(declaration)
33}
34
35export function isMaterialized(declaration: ts.InterfaceDeclaration | ts.ClassDeclaration): boolean {
36
37const name = identName(declaration)!
38
39if (PeerGeneratorConfig.isMaterializedIgnored(name)) {
40return false;
41}
42
43if (isBuilderClass(declaration)) {
44return 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
51return (ts.isClassDeclaration(declaration) && declaration.members.some(ts.isMethodDeclaration))
52|| (ts.isInterfaceDeclaration(declaration) && declaration.members.some(ts.isMethodSignature))
53}
54
55export class MaterializedField {
56constructor(
57public declarationTarget: DeclarationTarget,
58public argConvertor: ArgConvertor,
59public retConvertor: RetConvertor,
60public field: Field
61) { }
62}
63
64export class MaterializedMethod extends PeerMethod {
65constructor(
66originalParentName: string,
67declarationTargets: DeclarationTarget[],
68argConvertors: ArgConvertor[],
69retConvertor: RetConvertor,
70isCallSignature: boolean,
71method: Method
72) {
73super(originalParentName, declarationTargets, argConvertors, retConvertor, isCallSignature, false, method)
74}
75
76override get peerMethodName() {
77return this.overloadedName
78}
79
80override get toStringName(): string {
81switch (this.method.name) {
82case "ctor": return `new ${this.originalParentName}`
83case "destructor": return `delete ${this.originalParentName}`
84default: return super.toStringName
85}
86}
87
88override get dummyReturnValue(): string | undefined {
89if (this.method.name === "ctor") return `(void*) 100`
90if (this.method.name === "getFinalizer") return `fnPtr<KNativePointer>(dummyClassFinalizer)`
91if (this.method.modifiers?.includes(MethodModifier.STATIC)) return `(void*) 300`
92return undefined;
93}
94
95override get receiverType(): string {
96return `${this.originalParentName}Peer*`
97}
98
99override get apiCall(): string {
100return "GetAccessors()"
101}
102
103override get apiKind(): string {
104return "Accessor"
105}
106
107override generateReceiver(): { argName: string; argType: string } | undefined {
108if (!this.hasReceiver()) return undefined
109return {
110argName: 'peer',
111argType: `${this.originalParentName}Peer*`
112}
113}
114
115tsReturnType(): Type | undefined {
116const returnType = this.method.signature.returnType
117return this.hasReceiver() && returnType.name === this.originalParentName ? Type.This : returnType
118}
119}
120
121export class SuperElement {
122constructor(
123public readonly name: string,
124public readonly generics?: string[]
125) { }
126}
127
128export class MaterializedClass implements PeerClassBase {
129constructor(
130public readonly className: string,
131public readonly isInterface: boolean,
132public readonly superClass: SuperElement | undefined,
133public readonly generics: string[] | undefined,
134public readonly fields: MaterializedField[],
135public readonly ctor: MaterializedMethod,
136public readonly finalizer: MaterializedMethod,
137public readonly importFeatures: ImportFeature[],
138public methods: MaterializedMethod[],
139) {
140PeerMethod.markOverloads(methods)
141}
142
143getComponentName(): string {
144return this.className
145}
146
147setGenerationContext(context: string| undefined): void {
148// TODO: set generation context!
149}
150
151generatedName(isCallSignature: boolean): string{
152return this.className
153}
154}
155