• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-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 #include "partialExportClassGen.h"
17 
18 #include "checker/ETSchecker.h"
19 #include "varbinder/ETSBinder.h"
20 #include "varbinder/recordTable.h"
21 
22 namespace ark::es2panda::compiler {
23 
GeneratePartialDeclForExported(const public_lib::Context * const ctx,ir::AstNode * const node)24 static void GeneratePartialDeclForExported(const public_lib::Context *const ctx, ir::AstNode *const node)
25 {
26     // NOTE (mmartin): handle interfaces
27 
28     if (node->IsClassDeclaration()) {
29         ctx->checker->AsETSChecker()->CreatePartialType(node->AsClassDeclaration()->Definition()->TsType());
30     }
31 }
32 
Perform(public_lib::Context * const ctx,parser::Program * const program)33 bool PartialExportClassGen::Perform(public_lib::Context *const ctx, parser::Program *const program)
34 {
35     if (ctx->checker->VarBinder()->IsGenStdLib()) {
36         for (const auto &[_, extPrograms] : program->ExternalSources()) {
37             (void)_;
38             for (auto *const extProg : extPrograms) {
39                 Perform(ctx, extProg);
40             }
41         }
42     }
43 
44     program->Ast()->TransformChildrenRecursively(
45         [ctx, &program](ir::AstNode *const ast) {
46             if ((ast->IsClassDeclaration() || ast->IsTSInterfaceDeclaration()) && ast->IsExported()) {
47                 auto *const savedProg = ctx->checker->VarBinder()->AsETSBinder()->Program();
48                 ctx->checker->VarBinder()->AsETSBinder()->SetProgram(program);
49                 GeneratePartialDeclForExported(ctx, ast);
50                 ctx->checker->VarBinder()->AsETSBinder()->SetProgram(savedProg);
51             }
52 
53             return ast;
54         },
55         Name());
56 
57     return true;
58 }
59 
60 }  // namespace ark::es2panda::compiler
61