• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #ifndef ES2PANDA_TYPESCRIPT_EXACTOR_TYPEEXTRACTOR_H
17 #define ES2PANDA_TYPESCRIPT_EXACTOR_TYPEEXTRACTOR_H
18 
19 #include <ir/statements/blockStatement.h>
20 #include <macros.h>
21 
22 #include "typeRecorder.h"
23 
24 namespace panda::es2panda::extractor {
25 
26 using Getter = std::function<int64_t(const ir::AstNode *, bool isNewInstance)>;
27 using Handler = std::function<void(const ir::AstNode *)>;
28 
29 class TypeExtractor {
30 public:
31     explicit TypeExtractor(const ir::BlockStatement *rootNode, bool typeDtsExtractor, bool typeDtsBuiltin,
32         ArenaAllocator *allocator, compiler::CompilerContext *context);
33     ~TypeExtractor() = default;
34     NO_COPY_SEMANTIC(TypeExtractor);
35     NO_MOVE_SEMANTIC(TypeExtractor);
36 
37     void StartTypeExtractor(const parser::Program *program);
38 
39     bool GetTypeDtsExtractor() const;
40     bool GetTypeDtsBuiltin() const;
41     TypeRecorder *Recorder() const;
42 
43     const ir::Identifier *GetIdentifierFromExpression(const ir::Expression *expression);
44     int64_t GetTypeIndexFromAnnotation(const ir::Expression *typeAnnotation);
45     int64_t GetTypeIndexFromIdentifier(const ir::Identifier *identifier);
46     int64_t GetTypeIndexFromInitializer(const ir::Expression *initializer);
47 
48     static int64_t GetBuiltinTypeIndex(util::StringView name);
49 
50 private:
51     const ir::BlockStatement *rootNode_;
52     const bool typeDtsExtractor_;
53     const bool typeDtsBuiltin_;
54     std::unique_ptr<TypeRecorder> recorder_;
55     std::unordered_map<ir::AstNodeType, Getter> getterMap_;
56     std::unordered_map<ir::AstNodeType, Handler> handlerMap_;
57 
58     void ExtractNodesType(const ir::AstNode *parent);
59     void ExtractNodeType(const ir::AstNode *parent, const ir::AstNode *childNode);
60     void ExtractImport(const parser::Program *program);
61     void ExtractExport(const parser::Program *program);
62 
63     const ir::AstNode *GetDeclNodeFromIdentifier(const ir::Identifier *identifier, const ir::Identifier **variable);
64     const ir::AstNode *GetDeclNodeFromInitializer(const ir::Expression *initializer, const ir::Identifier **variable);
65 
66     int64_t GetTypeIndexFromDeclNode(const ir::AstNode *node, bool isNewInstance);
67     int64_t GetTypeIndexFromIdentifierNode(const ir::AstNode *node, bool isNewInstance);
68     int64_t GetTypeIndexFromClassExpression(const ir::AstNode *node, bool isNewInstance);
69     int64_t GetTypeIndexFromClassDefinition(const ir::AstNode *node, bool isNewInstance);
70     int64_t GetTypeIndexFromInterfaceNode(const ir::AstNode *node, bool isNewInstance);
71     int64_t GetTypeIndexFromImportNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance);
72     int64_t GetTypeIndexFromTypeAliasNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance);
73     int64_t GetTypeIndexFromMemberNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance);
74 
75     void HandleVariableDeclaration(const ir::AstNode *node);
76     void HandleFunctionDeclaration(const ir::AstNode *node);
77     void HandleClassDeclaration(const ir::AstNode *node);
78     void HandleInterfaceDeclaration(const ir::AstNode *node);
79     void HandleTypeAliasDeclaration(const ir::AstNode *node);
80 
81     // Helpers
82     int64_t GetTypeIndexFromClassInst(int64_t typeIndex);
83     int64_t GetTypeIndexFromTypeReference(const ir::TSTypeReference *typeReference);
84 
85     // Builtin Helpers
86     int64_t GetTypeIndexFromBuiltin(const util::StringView &name, const ir::TSTypeParameterInstantiation *node);
87     int64_t GetTypeIndexFromBuiltinInst(int64_t typeIndexBuiltin, const ir::TSTypeParameterInstantiation *node);
88 
89     // Other Helpers
90     bool IsExportNode(const ir::AstNode *node) const;
91     bool IsDeclareNode(const ir::AstNode *node) const;
92 };
93 
94 }  // namespace panda::es2panda::extractor
95 
96 #endif  // ES2PANDA_TYPESCRIPT_EXACTOR_TYPEEXTRACTOR_H
97