• 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 ECMASCRIPT_TS_TYPES_TS_TYPE_PARSER_H
17 #define ECMASCRIPT_TS_TYPES_TS_TYPE_PARSER_H
18 
19 #include "ecmascript/ts_types/ts_type_table_generator.h"
20 
21 namespace panda::ecmascript {
22 /* TSTypeParser parses types recorded in abc files into TSTypes. VM uses TSTypeTables to
23  * store TSTypes. Each TSTypeTable is used to store all types from the same record.
24  * Since VM can only record types in GlobalTSTypeRef::MAX_MODULE_ID records and
25  * can only record GlobalTSTypeRef::MAX_LOCAL_ID types in one record, all types outside
26  * this range will not be parsed and will be treated as any.
27  * In addition to this, in the following case, types will not be parsed and will be treated as any.
28  * 1. Import types with module request that does not point to one abc file
29  * 2. Import types with module request that point to one abc file which is generated by JS
30  * 3. Types with kind that are not supported temporarily
31  */
32 class TSTypeParser {
33 public:
TSTypeParser(TSManager * tsManager)34     explicit TSTypeParser(TSManager *tsManager)
35         : tsManager_(tsManager), vm_(tsManager->GetEcmaVM()),
36           thread_(vm_->GetJSThread()), factory_(vm_->GetFactory()),
37           tableGenerator_(tsManager_) {}
38     ~TSTypeParser() = default;
39 
40     GlobalTSTypeRef PUBLIC_API CreateGT(const JSPandaFile *jsPandaFile, const CString &recordName, uint32_t typeId);
41 
42     static constexpr size_t USER_DEFINED_TYPE_OFFSET = 100;
43 
44 private:
45     static constexpr size_t TYPE_KIND_INDEX_IN_LITERAL = 0;
46     static constexpr size_t BUILDIN_TYPE_OFFSET = 20;
47     static constexpr size_t IMPORT_PATH_OFFSET_IN_LITERAL = 1;
48 
49     static constexpr const char* DECLARED_SYMBOL_TYPES = "declaredSymbolTypes";
50     static constexpr const char* EXPORTED_SYMBOL_TYPES = "exportedSymbolTypes";
51 
52     inline GlobalTSTypeRef GetAndStoreGT(const JSPandaFile *jsPandaFile, uint32_t typeId, const CString &recordName,
53                                          uint32_t moduleId = 0, uint32_t localId = 0)
54     {
55         GlobalTSTypeRef gt(moduleId, localId);
56         tsManager_->AddElementToLiteralOffsetGTMap(jsPandaFile, typeId, recordName, gt);
57         return gt;
58     }
59 
GetAndStoreImportGT(const JSPandaFile * jsPandaFile,uint32_t typeId,const CString & recordName,GlobalTSTypeRef gt)60     inline GlobalTSTypeRef GetAndStoreImportGT(const JSPandaFile *jsPandaFile, uint32_t typeId,
61                                                const CString &recordName, GlobalTSTypeRef gt)
62     {
63         tsManager_->AddElementToLiteralOffsetGTMap(jsPandaFile, typeId, recordName, gt, true);
64         return gt;
65     }
66 
SetTSType(JSHandle<TSTypeTable> table,JSHandle<JSTaggedValue> type,const GlobalTSTypeRef & gt)67     inline void SetTSType(JSHandle<TSTypeTable> table, JSHandle<JSTaggedValue> type,
68                           const GlobalTSTypeRef &gt)
69     {
70         JSHandle<TSType>(type)->SetGT(gt);
71         uint32_t localId = gt.GetLocalId();
72         table->Set(thread_, localId, type);
73     }
74 
75     GlobalTSTypeRef ParseType(const JSPandaFile *jsPandaFile, const CString &recordName, uint32_t typeId);
76 
77     GlobalTSTypeRef ParseBuiltinObjType(uint32_t typeId);
78 
79     GlobalTSTypeRef ResolveImportType(const JSPandaFile *jsPandaFile, const CString &recordName,
80                                       JSHandle<TaggedArray> literal, uint32_t typeId);
81 
82     JSHandle<JSTaggedValue> ParseNonImportType(const JSPandaFile *jsPandaFile, const CString &recordName,
83                                                JSHandle<TaggedArray> literal, TSTypeKind kind, uint32_t typeId);
84 
85     JSHandle<TSClassType> ParseClassType(const JSPandaFile *jsPandaFile, const CString &recordName,
86                                          const JSHandle<TaggedArray> &literal, uint32_t typeId);
87 
88     JSHandle<TSClassInstanceType> ParseClassInstanceType(const JSPandaFile *jsPandaFile, const CString &recordName,
89                                                          const JSHandle<TaggedArray> &literal);
90 
91     JSHandle<TSInterfaceType> ParseInterfaceType(const JSPandaFile *jsPandaFile, const CString &recordName,
92                                                  const JSHandle<TaggedArray> &literal);
93 
94     JSHandle<TSUnionType> ParseUnionType(const JSPandaFile *jsPandaFile, const CString &recordName,
95                                          const JSHandle<TaggedArray> &literal);
96 
97     JSHandle<TSFunctionType> ParseFunctionType(const JSPandaFile *jsPandaFile, const CString &recordName,
98                                                const JSHandle<TaggedArray> &literal);
99 
100     JSHandle<TSArrayType> ParseArrayType(const JSPandaFile *jsPandaFile, const CString &recordName,
101                                          const JSHandle<TaggedArray> &literal);
102 
103     JSHandle<TSObjectType> ParseObjectType(const JSPandaFile *jsPandaFile, const CString &recordName,
104                                            const JSHandle<TaggedArray> &literal);
105 
106     void FillPropertyTypes(const JSPandaFile *jsPandaFile,
107                            const CString &recordName,
108                            JSHandle<TSObjLayoutInfo> &layout,
109                            const JSHandle<TaggedArray> &literal,
110                            uint32_t startIndex, uint32_t lastIndex,
111                            uint32_t &index, bool isField);
112 
113     void FillInterfaceMethodTypes(const JSPandaFile *jsPandaFile,
114                                   const CString &recordName,
115                                   JSHandle<TSObjLayoutInfo> &layout,
116                                   const JSHandle<TaggedArray> &literal,
117                                   uint32_t startIndex, uint32_t lastIndex,
118                                   uint32_t &index);
119 
120     JSHandle<TaggedArray> GetExportDataFromRecord(const JSPandaFile *jsPandaFile, const CString &recordName);
121 
122     JSHandle<JSTaggedValue> GenerateExportTableFromRecord(const JSPandaFile *jsPandaFile, const CString &recordName,
123                                                           const JSHandle<TSTypeTable> &table);
124 
125     JSHandle<EcmaString> GenerateImportRelativePath(JSHandle<EcmaString> importRel) const;
126 
127     JSHandle<EcmaString> GenerateImportVar(JSHandle<EcmaString> import) const;
128 
129     GlobalTSTypeRef GetExportGTByName(JSHandle<EcmaString> target, JSHandle<TaggedArray> &exportTable) const;
130 
131     TSManager *tsManager_ {nullptr};
132     EcmaVM *vm_ {nullptr};
133     JSThread *thread_ {nullptr};
134     ObjectFactory *factory_ {nullptr};
135     TSTypeTableGenerator tableGenerator_;
136 };
137 }  // panda::ecmascript
138 #endif  // ECMASCRIPT_TS_TYPES_TS_TYPE_PARSER_H
139