• 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_TYPERECORDER_H
17 #define ES2PANDA_TYPESCRIPT_EXACTOR_TYPERECORDER_H
18 
19 #include <macros.h>
20 
21 #include <binder/binder.h>
22 #include <binder/variable.h>
23 #include <compiler/base/literals.h>
24 #include <compiler/core/compilerContext.h>
25 #include <ir/astNode.h>
26 #include <ir/expressions/identifier.h>
27 #include <parser/program/program.h>
28 #include <mem/arena_allocator.h>
29 #include <utils/arena_containers.h>
30 
31 namespace panda::es2panda::extractor {
32 
33 const util::StringView DEFAULT_NAME = "default";
34 
35 class TypeRecorder {
36 public:
37     explicit TypeRecorder(ArenaAllocator *allocator, compiler::CompilerContext *context);
38     ~TypeRecorder() = default;
39     NO_COPY_SEMANTIC(TypeRecorder);
40     NO_MOVE_SEMANTIC(TypeRecorder);
41 
42     ArenaAllocator *Allocator() const;
43     const ArenaVector<compiler::LiteralBuffer *> &BuffStorage() const;
44     const std::unordered_map<const ir::AstNode *, int64_t> &NodeTypeIndex() const;
45     const std::unordered_map<const binder::Variable *, int64_t> &VariableTypeIndex() const;
46     const std::unordered_map<std::string, int64_t> &ExportType() const;
47     const std::unordered_map<std::string, int64_t> &DeclareType() const;
48 
49     compiler::LiteralBuffer *NewLiteralBuffer();
50     int64_t AddLiteralBuffer(compiler::LiteralBuffer *buffer);
51     compiler::LiteralBuffer *GetLiteralBuffer(int64_t index) const;
52     void SetLiteralBuffer(int64_t index, compiler::LiteralBuffer *buffer);
53 
54     util::StringView GetRecordName() const;
55     util::StringView GetAnonymousFunctionNames(const ir::ScriptFunction *func) const;
56 
57     const std::set<int64_t> &GetUserType() const;
58     void AddUserType(int64_t index);
59 
60     int64_t GetTypeSummaryIndex() const;
61     void SetTypeSummaryIndex(int64_t index);
62 
63     int64_t GetUserTypeIndexShift() const;
64     void SetUserTypeIndexShift(int64_t index);
65 
66     int64_t GetNodeTypeIndex(const ir::AstNode *node) const;
67     void SetNodeTypeIndex(const ir::AstNode *node, int64_t index);
68     void UpdateNodeTypeIndex(const ir::AstNode *node, int64_t index);
69 
70     int64_t GetVariableTypeIndex(const binder::Variable *variable) const;
71     void SetVariableTypeIndex(const binder::Variable *variable, int64_t index);
72 
73     void SetIdentifierTypeIndex(const ir::Identifier *identifier, int64_t index);
74 
75     int64_t GetBuiltinInst(const std::vector<int64_t> &allTypes) const;
76     void SetBuiltinInst(const std::vector<int64_t> &allTypes, int64_t instIndex);
77 
78     int64_t GetGenericInst(const std::vector<int64_t> &allTypes) const;
79     void SetGenericInst(const std::vector<int64_t> &allTypes, int64_t instIndex);
80 
81     int64_t GetIndexSig(int64_t refIndex) const;
82     void SetIndexSig(int64_t refIndex, int64_t indexSigIndex);
83 
84     int64_t GetClassInst(int64_t classIndex) const;
85     void SetClassInst(int64_t classIndex, int64_t instIndex);
86 
87     int64_t GetClassType(int64_t instIndex) const;
88     void SetClassType(int64_t instIndex, int64_t classIndex);
89 
90     int64_t GetArrayType(int64_t contentIndex) const;
91     void SetArrayType(int64_t contentIndex, int64_t arrayIndex);
92 
93     int64_t GetUnionType(const std::string &unionStr) const;
94     void SetUnionType(const std::string &unionStr, int64_t unionIndex);
95 
96     int64_t GetObjectType(const std::string &objectStr) const;
97     void SetObjectType(const std::string &objectStr, int64_t objectIndex);
98 
99     int64_t GetFunctionType(const std::string &functionStr) const;
100     void SetFunctionType(const std::string &functionStr, int64_t functionIndex);
101 
102     int64_t GetExportType(const std::string &exportStr) const;
103     void SetExportType(const std::string &exportStr, int64_t exportIndex);
104 
105     int64_t GetDeclareType(const std::string &declareStr) const;
106     void SetDeclareType(const std::string &declareStr, int64_t declareIndex);
107 
108     int64_t GetNamespaceType(const std::string &namespaceStr) const;
109     void SetNamespaceType(const std::string &namespaceStr, int64_t namespaceIndex);
110 
111     std::string GetNamespacePath(const std::string &namespaceStr) const;
112     void SetNamespacePath(const std::string &namespaceStr, const std::string &filePath);
113 
114     const std::set<util::StringView> &GetAnonymousReExport() const;
115     void AddAnonymousReExport(const util::StringView &reExportStr);
116 
117     ALWAYS_INLINE void Dump(const parser::Program *program) const;
118 
isBuiltinType(const int64_t typeIndex)119     bool isBuiltinType(const int64_t typeIndex)
120     {
121         if (typeIndex > BUILTINTYPE_HEAD && typeIndex < USERTYPEINDEXHEAD) {
122             return true;
123         }
124         return false;
125     }
126 
127     static constexpr int64_t PRIMITIVETYPE_ANY = 0;
128     static constexpr int64_t BUILTINTYPE_HEAD = 20;
129     static constexpr int64_t USERTYPEINDEXHEAD = 100;
130 
131 private:
132     ArenaAllocator *allocator_;
133     compiler::CompilerContext *context_;
134     ArenaVector<compiler::LiteralBuffer *> buffStorage_;
135     int64_t typeSummaryIndex_ = 0;
136     int64_t userTypeIndexShift_ = USERTYPEINDEXHEAD;
137     std::set<int64_t> userType_ {};
138     std::unordered_map<const ir::AstNode *, int64_t> nodeTypeIndex_ {};
139     std::unordered_map<const binder::Variable *, int64_t> variableTypeIndex_ {};
140     std::map<std::vector<int64_t>, int64_t> builtinInst_ {};
141     std::map<std::vector<int64_t>, int64_t> genericInst_ {};
142     std::unordered_map<int64_t, int64_t> indexSig_ {};
143     std::unordered_map<int64_t, int64_t> classInst_ {};
144     std::unordered_map<int64_t, int64_t> classType_ {};
145     std::unordered_map<int64_t, int64_t> arrayType_ {};
146     std::unordered_map<std::string, int64_t> unionType_ {};
147     std::unordered_map<std::string, int64_t> objectType_ {};
148     std::unordered_map<std::string, int64_t> functionType_ {};
149     // Export symbols
150     std::unordered_map<std::string, int64_t> exportType_ {};
151     std::unordered_map<std::string, int64_t> declareType_ {};
152     // Namespace in import / export declaration
153     std::unordered_map<std::string, int64_t> namespaceType_ {};
154     std::unordered_map<std::string, std::string> namespacePath_ {};
155     std::set<util::StringView> anonymousReExport_ {};
156 };
157 
158 }  // namespace panda::es2panda::extractor
159 
160 #endif  // ES2PANDA_TYPESCRIPT_EXACTOR_TYPERECORDER_H
161