13af6ab5fSopenharmony_ci/** 23af6ab5fSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#ifndef ES2PANDA_DECLGEN_ETS2TS_H 173af6ab5fSopenharmony_ci#define ES2PANDA_DECLGEN_ETS2TS_H 183af6ab5fSopenharmony_ci 193af6ab5fSopenharmony_ci#include "parser/program/program.h" 203af6ab5fSopenharmony_ci#include "checker/ETSchecker.h" 213af6ab5fSopenharmony_ci#include "libpandabase/os/file.h" 223af6ab5fSopenharmony_ci 233af6ab5fSopenharmony_cinamespace ark::es2panda::declgen_ets2ts { 243af6ab5fSopenharmony_ci 253af6ab5fSopenharmony_ci// Consume program after checker stage and generate out_path typescript file with declarations 263af6ab5fSopenharmony_cibool GenerateTsDeclarations(checker::ETSChecker *checker, const ark::es2panda::parser::Program *program, 273af6ab5fSopenharmony_ci const std::string &outPath); 283af6ab5fSopenharmony_ci 293af6ab5fSopenharmony_ciclass TSDeclGen { 303af6ab5fSopenharmony_cipublic: 313af6ab5fSopenharmony_ci TSDeclGen(checker::ETSChecker *checker, const ark::es2panda::parser::Program *program) 323af6ab5fSopenharmony_ci : checker_(checker), program_(program) 333af6ab5fSopenharmony_ci { 343af6ab5fSopenharmony_ci } 353af6ab5fSopenharmony_ci 363af6ab5fSopenharmony_ci std::stringstream &Output() 373af6ab5fSopenharmony_ci { 383af6ab5fSopenharmony_ci return output_; 393af6ab5fSopenharmony_ci } 403af6ab5fSopenharmony_ci 413af6ab5fSopenharmony_ci void Generate(); 423af6ab5fSopenharmony_ci 433af6ab5fSopenharmony_ci static constexpr std::string_view INDENT = " "; 443af6ab5fSopenharmony_ci 453af6ab5fSopenharmony_ciprivate: 463af6ab5fSopenharmony_ci void ThrowError(std::string_view message, const lexer::SourcePosition &pos); 473af6ab5fSopenharmony_ci const ir::Identifier *GetKeyIdent(const ir::Expression *key); 483af6ab5fSopenharmony_ci 493af6ab5fSopenharmony_ci void GenType(const checker::Type *checkerType); 503af6ab5fSopenharmony_ci void GenFunctionType(const checker::ETSFunctionType *functionType, const ir::MethodDefinition *methodDef = nullptr); 513af6ab5fSopenharmony_ci void GenFunctionBody(const ir::MethodDefinition *methodDef, const checker::Signature *sig, const bool isConstructor, 523af6ab5fSopenharmony_ci const bool isSetter); 533af6ab5fSopenharmony_ci void GenObjectType(const checker::ETSObjectType *objectType); 543af6ab5fSopenharmony_ci void GenEnumType(const checker::ETSIntEnumType *enumType); 553af6ab5fSopenharmony_ci void GenUnionType(const checker::ETSUnionType *unionType); 563af6ab5fSopenharmony_ci 573af6ab5fSopenharmony_ci void GenImportDeclaration(const ir::ETSImportDeclaration *importDeclaration); 583af6ab5fSopenharmony_ci void GenTypeAliasDeclaration(const ir::TSTypeAliasDeclaration *typeAlias); 593af6ab5fSopenharmony_ci void GenEnumDeclaration(const ir::TSEnumDeclaration *enumDecl); 603af6ab5fSopenharmony_ci void GenInterfaceDeclaration(const ir::TSInterfaceDeclaration *interfaceDecl); 613af6ab5fSopenharmony_ci void GenClassDeclaration(const ir::ClassDeclaration *classDecl); 623af6ab5fSopenharmony_ci void GenMethodDeclaration(const ir::MethodDefinition *methodDef); 633af6ab5fSopenharmony_ci void GenPropDeclaration(const ir::ClassProperty *classProp); 643af6ab5fSopenharmony_ci void GenGlobalVarDeclaration(const ir::ClassProperty *globalVar); 653af6ab5fSopenharmony_ci void GenLiteral(const ir::Literal *literal); 663af6ab5fSopenharmony_ci 673af6ab5fSopenharmony_ci template <class T> 683af6ab5fSopenharmony_ci void GenModifier(const T *node); 693af6ab5fSopenharmony_ci void GenTypeParameters(const ir::TSTypeParameterDeclaration *typeParams); 703af6ab5fSopenharmony_ci void GenExport(const ir::Identifier *symbol); 713af6ab5fSopenharmony_ci void GenExport(const ir::Identifier *symbol, const std::string &alias); 723af6ab5fSopenharmony_ci void GenDefaultExport(const ir::Identifier *symbol); 733af6ab5fSopenharmony_ci void ExportIfNeeded(const ir::Identifier *symbol); 743af6ab5fSopenharmony_ci 753af6ab5fSopenharmony_ci template <class T, class CB> 763af6ab5fSopenharmony_ci void GenSeparated(const T &container, const CB &cb, const char *separator = ", "); 773af6ab5fSopenharmony_ci 783af6ab5fSopenharmony_ci void Out() {} 793af6ab5fSopenharmony_ci template <class F, class... T> 803af6ab5fSopenharmony_ci void Out(F &&first, T &&...rest) 813af6ab5fSopenharmony_ci { 823af6ab5fSopenharmony_ci output_ << first; 833af6ab5fSopenharmony_ci Out(std::forward<T>(rest)...); 843af6ab5fSopenharmony_ci } 853af6ab5fSopenharmony_ci void OutEndl(const std::size_t count = 1) 863af6ab5fSopenharmony_ci { 873af6ab5fSopenharmony_ci ark::os::file::File::GetEndLine(output_, count); 883af6ab5fSopenharmony_ci } 893af6ab5fSopenharmony_ci 903af6ab5fSopenharmony_ci void ResetState() 913af6ab5fSopenharmony_ci { 923af6ab5fSopenharmony_ci state_ = GenState(); 933af6ab5fSopenharmony_ci } 943af6ab5fSopenharmony_ci 953af6ab5fSopenharmony_ci struct GenState { 963af6ab5fSopenharmony_ci const ir::Expression *super {nullptr}; 973af6ab5fSopenharmony_ci bool inInterface {false}; 983af6ab5fSopenharmony_ci bool inGlobalClass {false}; 993af6ab5fSopenharmony_ci std::string currentClassDescriptor {}; 1003af6ab5fSopenharmony_ci } state_ {}; 1013af6ab5fSopenharmony_ci 1023af6ab5fSopenharmony_ci std::stringstream output_ {}; 1033af6ab5fSopenharmony_ci checker::ETSChecker *checker_ {}; 1043af6ab5fSopenharmony_ci const ark::es2panda::parser::Program *program_ {}; 1053af6ab5fSopenharmony_ci}; 1063af6ab5fSopenharmony_ci} // namespace ark::es2panda::declgen_ets2ts 1073af6ab5fSopenharmony_ci 1083af6ab5fSopenharmony_ci#endif // ES2PANDA_DECLGEN_ETS2TS_H 109