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 GetAnonymousFunctionNames(const ir::ScriptFunction *func) const; 55 56 int64_t CalculateUserType() const; 57 void AddUserType(int64_t index); 58 59 int64_t GetTypeSummaryIndex() const; 60 void SetTypeSummaryIndex(int64_t index); 61 62 int64_t GetUserTypeIndexShift() const; 63 void SetUserTypeIndexShift(int64_t index); 64 65 int64_t GetNodeTypeIndex(const ir::AstNode *node) const; 66 void SetNodeTypeIndex(const ir::AstNode *node, int64_t index); 67 68 int64_t GetVariableTypeIndex(const binder::Variable *variable) const; 69 void SetVariableTypeIndex(const binder::Variable *variable, int64_t index); 70 71 void SetIdentifierTypeIndex(const ir::Identifier *identifier, int64_t index); 72 73 int64_t GetBuiltinInst(const std::vector<int64_t> &allTypes) const; 74 void SetBuiltinInst(const std::vector<int64_t> &allTypes, int64_t instIndex); 75 76 int64_t GetClassInst(int64_t classIndex) const; 77 void SetClassInst(int64_t classIndex, int64_t instIndex); 78 79 int64_t GetClassType(int64_t instIndex) const; 80 void SetClassType(int64_t instIndex, int64_t classIndex); 81 82 int64_t GetArrayType(int64_t contentIndex) const; 83 void SetArrayType(int64_t contentIndex, int64_t arrayIndex); 84 85 int64_t GetUnionType(const std::string &unionStr) const; 86 void SetUnionType(const std::string &unionStr, int64_t unionIndex); 87 88 int64_t GetObjectType(const std::string &objectStr) const; 89 void SetObjectType(const std::string &objectStr, int64_t objectIndex); 90 91 int64_t GetExportType(const std::string &exportStr) const; 92 void SetExportType(const std::string &exportStr, int64_t exportIndex); 93 94 int64_t GetDeclareType(const std::string &declareStr) const; 95 void SetDeclareType(const std::string &declareStr, int64_t declareIndex); 96 97 int64_t GetNamespaceType(const std::string &namespaceStr) const; 98 void SetNamespaceType(const std::string &namespaceStr, int64_t namespaceIndex); 99 100 std::string GetNamespacePath(const std::string &namespaceStr) const; 101 void SetNamespacePath(const std::string &namespaceStr, const std::string &filePath); 102 103 const std::set<util::StringView> &GetAnonymousReExport() const; 104 void AddAnonymousReExport(const util::StringView &reExportStr); 105 106 ALWAYS_INLINE void Dump(const parser::Program *program) const; 107 108 static constexpr uint8_t PRIMITIVETYPE_ANY = 0U; 109 static constexpr int64_t USERTYPEINDEXHEAD = 100; 110 111 private: 112 ArenaAllocator *allocator_; 113 compiler::CompilerContext *context_; 114 ArenaVector<compiler::LiteralBuffer *> buffStorage_; 115 int64_t typeSummaryIndex_ = 0; 116 int64_t userTypeIndexShift_ = USERTYPEINDEXHEAD; 117 std::set<int64_t> userType_ {}; 118 std::unordered_map<const ir::AstNode *, int64_t> nodeTypeIndex_ {}; 119 std::unordered_map<const binder::Variable *, int64_t> variableTypeIndex_ {}; 120 std::map<std::vector<int64_t>, int64_t> builtinInst_ {}; 121 std::unordered_map<int64_t, int64_t> classInst_ {}; 122 std::unordered_map<int64_t, int64_t> classType_ {}; 123 std::unordered_map<int64_t, int64_t> arrayType_ {}; 124 std::unordered_map<std::string, int64_t> unionType_ {}; 125 std::unordered_map<std::string, int64_t> objectType_ {}; 126 // Export symbols 127 std::unordered_map<std::string, int64_t> exportType_ {}; 128 std::unordered_map<std::string, int64_t> declareType_ {}; 129 // Namespace in import / export declaration 130 std::unordered_map<std::string, int64_t> namespaceType_ {}; 131 std::unordered_map<std::string, std::string> namespacePath_ {}; 132 std::set<util::StringView> anonymousReExport_ {}; 133 }; 134 135 } // namespace panda::es2panda::extractor 136 137 #endif // ES2PANDA_TYPESCRIPT_EXACTOR_TYPERECORDER_H 138