• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# coding=utf-8
2#
3# Copyright (c) 2025 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from dataclasses import dataclass
17from typing import TYPE_CHECKING, ClassVar
18
19from taihe.driver.backend import Backend, BackendConfig
20
21if TYPE_CHECKING:
22    from taihe.driver.contexts import CompilerInstance
23
24
25@dataclass
26class CppCommonHeadersBackendConfig(BackendConfig):
27    NAME = "cpp-common"
28    DEPS: ClassVar = ["abi-header"]
29
30    def construct(self, instance: "CompilerInstance") -> Backend:
31        from taihe.codegen.cpp.gen_common import CppHeadersGenerator
32
33        class CppCommonHeadersBackendImpl(Backend):
34            def __init__(self, ci: "CompilerInstance"):
35                super().__init__(ci)
36                self._ci = ci
37
38            def generate(self):
39                om = self._ci.output_manager
40                am = self._ci.analysis_manager
41                pg = self._ci.package_group
42                CppHeadersGenerator(om, am).generate(pg)
43
44        return CppCommonHeadersBackendImpl(instance)
45
46
47@dataclass
48class CppAuthorBackendConfig(BackendConfig):
49    NAME = "cpp-author"
50    DEPS: ClassVar = ["cpp-common", "abi-source"]
51
52    def construct(self, instance: "CompilerInstance") -> Backend:
53        from taihe.codegen.cpp.gen_impl import (
54            CppImplHeadersGenerator,
55            CppImplSourcesGenerator,
56        )
57
58        # TODO: unify CppImpl{Headers,Sources}Generator
59        class CppImplBackendImpl(Backend):
60            def __init__(self, ci: "CompilerInstance"):
61                super().__init__(ci)
62                self._ci = ci
63
64            def generate(self):
65                om = self._ci.output_manager
66                am = self._ci.analysis_manager
67                pg = self._ci.package_group
68                CppImplSourcesGenerator(om, am).generate(pg)
69                CppImplHeadersGenerator(om, am).generate(pg)
70
71        return CppImplBackendImpl(instance)
72
73
74@dataclass
75class CppUserHeadersBackendConfig(BackendConfig):
76    NAME = "cpp-user"
77    DEPS: ClassVar = ["cpp-common"]
78
79    def construct(self, instance: "CompilerInstance") -> Backend:
80        from taihe.codegen.cpp.gen_user import CppUserHeadersGenerator
81
82        class CppUserHeadersBackendImpl(Backend):
83            def __init__(self, ci: "CompilerInstance"):
84                super().__init__(ci)
85                self._ci = ci
86
87            def generate(self):
88                om = self._ci.output_manager
89                am = self._ci.analysis_manager
90                pg = self._ci.package_group
91                CppUserHeadersGenerator(om, am).generate(pg)
92
93        return CppUserHeadersBackendImpl(instance)