• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 MAPLEBE_MDGEN_INCLUDE_MDREORD_H
17 #define MAPLEBE_MDGEN_INCLUDE_MDREORD_H
18 
19 #include <vector>
20 #include <unordered_map>
21 #include <map>
22 #include <set>
23 #include <limits.h>
24 #include "mempool_allocator.h"
25 #include "mempool.h"
26 #include "mpl_logging.h"
27 #include "types_def.h"
28 
29 /* Define base data structure which is used to store information in  .md files */
30 namespace MDGen {
31 class MDClass; /* circular dependency */
32 
33 enum RecordType : maple::uint32 {
34     kClassName,
35     kAnonClassName,
36     kObjectName,
37     kElementName,
38     kIntType,
39     kStringType,
40     kTypeName,
41     kTypeMemberName,
42     kUndefinedStr
43 };
44 
45 struct StrInfo {
46     unsigned int idx;
47     RecordType sType;
StrInfoStrInfo48     StrInfo(unsigned int curIdx, RecordType curTy) : idx(curIdx), sType(curTy) {}
49 };
50 
51 class MDElement {
52 public:
53     MDElement() = default;
54     virtual ~MDElement() = default;
55     enum ElementTy : maple::uint32 {
56         kEleIntTy,
57         kEleStrTy,
58         kEleDefTyTy,
59         kEleDefObjTy,
60         kEleVecTy,
61         kEleDefaultTy,
62         kEleInValidTy
63     };
64 
GetContent()65     unsigned int GetContent() const
66     {
67         return DoGetContent();
68     }
69 
GetRecDataTy()70     ElementTy GetRecDataTy() const
71     {
72         return eleType;
73     }
74 
75 protected:
76     ElementTy eleType = kEleInValidTy;
77 
78 private:
79     virtual unsigned int DoGetContent() const = 0;
80 };
81 
82 class DefaultElement : public MDElement {
83 public:
DefaultElement()84     DefaultElement()
85     {
86         eleType = kEleDefaultTy;
87     }
88 
89     ~DefaultElement() override = default;
90 
91 private:
DoGetContent()92     unsigned int DoGetContent() const override
93     {
94         CHECK_FATAL(false, "Cannnot load default element's content");
95         return UINT_MAX;
96     }
97 };
98 
99 class IntElement : public MDElement {
100 public:
IntElement(unsigned int curVal)101     explicit IntElement(unsigned int curVal) : intEleVal(curVal)
102     {
103         eleType = kEleIntTy;
104     }
105 
106     ~IntElement() override = default;
107 
108 private:
109     unsigned int intEleVal;
DoGetContent()110     unsigned int DoGetContent() const override
111     {
112         return intEleVal;
113     }
114 };
115 
116 class StringElement : public MDElement {
117 public:
StringElement(unsigned int curIdx)118     explicit StringElement(unsigned int curIdx) : strElemntIdx(curIdx)
119     {
120         eleType = kEleStrTy;
121     }
122 
123     ~StringElement() override = default;
124 
125 private:
126     unsigned int strElemntIdx;
DoGetContent()127     unsigned int DoGetContent() const override
128     {
129         return strElemntIdx;
130     }
131 };
132 
133 class DefTyElement : public MDElement {
134 public:
DefTyElement()135     DefTyElement()
136     {
137         eleType = kEleDefTyTy;
138     }
139 
140     ~DefTyElement() override = default;
141 
142     bool SetContent(const StrInfo curInfo, const std::set<unsigned int> &childTySet);
143 
144 private:
145     unsigned int elementIdx = UINT_MAX;
DoGetContent()146     unsigned int DoGetContent() const override
147     {
148         return elementIdx;
149     }
150 };
151 
152 class DefObjElement : public MDElement {
153 public:
DefObjElement()154     DefObjElement()
155     {
156         eleType = kEleDefObjTy;
157     }
158 
159     ~DefObjElement() override = default;
160 
161     bool SetContent(const StrInfo curInfo, const MDClass &parentClass);
162 
163 private:
164     unsigned int elementIdx = UINT_MAX;
DoGetContent()165     unsigned int DoGetContent() const override
166     {
167         return elementIdx;
168     }
169 };
170 
171 class VecElement : public MDElement {
172 public:
VecElement(maple::MemPool & mem)173     explicit VecElement(maple::MemPool &mem) : alloc(&mem), vecData(alloc.Adapter())
174     {
175         eleType = kEleVecTy;
176     }
177 
178     ~VecElement() override = default;
179 
appendElement(MDElement * curElement)180     void appendElement(MDElement *curElement)
181     {
182         vecData.emplace_back(curElement);
183     }
184 
GetVecData()185     const maple::MapleVector<MDElement *> GetVecData() const
186     {
187         return vecData;
188     }
189 
GetVecDataSize()190     size_t GetVecDataSize() const
191     {
192         return vecData.size();
193     }
194 
195 private:
196     maple::MapleAllocator alloc;
197     maple::MapleVector<MDElement *> vecData;
198 
DoGetContent()199     unsigned int DoGetContent() const override
200     {
201         CHECK_FATAL(false, "Vector element does not have a single content");
202         return UINT_MAX;
203     }
204 };
205 
206 class MDObject {
207 public:
MDObject(unsigned int curIdx,MDClass & pClass,maple::MemPool & memPool)208     MDObject(unsigned int curIdx, MDClass &pClass, maple::MemPool &memPool)
209         : objectIdx(curIdx), parentClass(&pClass), alloc(&memPool), mdElements(alloc.Adapter())
210     {
211     }
212 
213     ~MDObject() = default;
214 
215     const MDElement *GetOneMDElement(size_t index) const;
216 
AddMDElements(MDElement * curElement)217     void AddMDElements(MDElement *curElement)
218     {
219         mdElements.emplace_back(curElement);
220     }
221 
GetIdx()222     unsigned int GetIdx() const
223     {
224         return objectIdx;
225     }
226 
GetParentClass()227     const MDClass *GetParentClass() const
228     {
229         return parentClass;
230     }
231 
232 private:
233     unsigned int objectIdx;
234     MDClass *parentClass;
235     maple::MapleAllocator alloc;
236     maple::MapleVector<MDElement *> mdElements;
237 };
238 
239 class MDClass {
240 public:
MDClass(unsigned int classIdx,bool isAnonymous)241     MDClass(unsigned int classIdx, bool isAnonymous)
242     {
243         this->classIdx = classIdx;
244         this->isAnonymous = isAnonymous;
245     }
246     ~MDClass() = default;
247 
248     const MDObject &GetOneMDObject(size_t index) const;
249     void AddClassMember(MDObject inputObj);
250     bool IsClassMember(unsigned int curIdx) const;
251     bool IsValidStructEle(RecordType curTy) const;
GetClassIdx()252     unsigned int GetClassIdx() const
253     {
254         return classIdx;
255     }
IsAnonymousClass()256     bool IsAnonymousClass() const
257     {
258         return isAnonymous;
259     }
GetFormalTypes()260     const std::vector<std::pair<uint32, bool>> GetFormalTypes() const
261     {
262         return formalTypes;
263     }
GetchildObjNames()264     const std::set<unsigned int> GetchildObjNames() const
265     {
266         return childObjNames;
267     }
GetFormalTypeSize()268     size_t GetFormalTypeSize() const
269     {
270         return formalTypes.size();
271     }
GetMDObjectSize()272     size_t GetMDObjectSize() const
273     {
274         return mdObjects.size();
275     }
276     void BuildFormalTypes(unsigned int memberIdx, bool isVec);
277 
278 private:
279     unsigned int classIdx;
280     bool isAnonymous;
281     std::vector<MDObject> mdObjects;
282     std::vector<std::pair<uint32, bool>> formalTypes;
283     std::set<unsigned int> childObjNames;
284 };
285 
286 class MDClassRange {
287 public:
MDClassRange(std::string module)288     explicit MDClassRange(std::string module) : moduleName(module)
289     {
290         stringTable.clear();
291         stringHashTable.clear();
292         /* init common types such as unsigned int ,string , float */
293         std::set<unsigned int> initTypes;
294         AddDefinedType(CreateStrInTable("int", kIntType), initTypes);
295         AddDefinedType(CreateStrInTable("string", kStringType), initTypes);
296     }
297     ~MDClassRange() = default;
298 
299     StrInfo GetStrInTable(const std::string &inStr);
300     RecordType GetStrTyByIdx(size_t curIdx);
301     const std::string &GetStrByIdx(size_t curIdx);
302     void AddMDClass(MDClass curClass);
303     MDClass GetOneMDClass(unsigned int givenIdx);
304     std::set<unsigned int> GetOneSpcType(unsigned int givenTyIdx);
GetStringTableSize()305     size_t GetStringTableSize() const
306     {
307         return stringTable.size();
308     }
309     unsigned int CreateStrInTable(const std::string &inStr, RecordType curTy);
310     void ModifyStrTyInTable(const std::string &inStr, RecordType newTy);
311     void AddDefinedType(unsigned int typesName, std::set<unsigned int> typesSet);
312     void FillMDClass(unsigned int givenIdx, const MDObject &insertObj);
313 
314 private:
315     std::string moduleName;
316     std::unordered_map<std::string, StrInfo> stringHashTable;
317     std::vector<std::string> stringTable;
318     unsigned int totalStr = 0;
319     std::unordered_map<uint32, std::set<unsigned int>> definedTypes;
320     std::unordered_map<uint32, MDClass> allClasses;
321 };
322 } /* namespace MDGen */
323 
324 #endif /* MAPLEBE_MDGEN_INCLUDE_MDREORD_H */
325