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 #include "mdrecord.h"
17
18 namespace MDGen {
19 constexpr unsigned int kInValidStrIdx = UINT_MAX;
20
SetContent(const StrInfo curInfo,const std::set<unsigned int> & childTySet)21 bool DefTyElement::SetContent(const StrInfo curInfo, const std::set<unsigned int> &childTySet)
22 {
23 if (!childTySet.count(curInfo.idx)) {
24 return false;
25 }
26 elementIdx = curInfo.idx;
27 return true;
28 }
29
SetContent(const StrInfo curInfo,const MDClass & parentClass)30 bool DefObjElement::SetContent(const StrInfo curInfo, const MDClass &parentClass)
31 {
32 if (!parentClass.IsClassMember(curInfo.idx)) {
33 return false;
34 }
35 elementIdx = curInfo.idx;
36 return true;
37 }
38
GetOneMDElement(size_t index) const39 const MDElement *MDObject::GetOneMDElement(size_t index) const
40 {
41 CHECK_FATAL(index < mdElements.size(), "Array boundary check failed");
42 return mdElements[index];
43 }
44
GetOneMDObject(size_t index) const45 const MDObject &MDClass::GetOneMDObject(size_t index) const
46 {
47 CHECK_FATAL(index < mdObjects.size(), "Array boundary check failed");
48 return mdObjects[index];
49 }
50
AddClassMember(MDObject inputObj)51 void MDClass::AddClassMember(MDObject inputObj)
52 {
53 mdObjects.emplace_back(inputObj);
54 (void)childObjNames.insert(inputObj.GetIdx());
55 }
56
IsClassMember(unsigned int curIdx) const57 bool MDClass::IsClassMember(unsigned int curIdx) const
58 {
59 return childObjNames.count(curIdx);
60 }
61
BuildFormalTypes(unsigned int memberIdx,bool isVec)62 void MDClass::BuildFormalTypes(unsigned int memberIdx, bool isVec)
63 {
64 formalTypes.emplace_back(std::make_pair(memberIdx, isVec));
65 }
66
IsValidStructEle(RecordType curTy) const67 bool MDClass::IsValidStructEle(RecordType curTy) const
68 {
69 return (curTy == kTypeName || curTy == kClassName || curTy == kIntType || curTy == kStringType);
70 }
71
CreateStrInTable(const std::string & inStr,RecordType curTy)72 unsigned int MDClassRange::CreateStrInTable(const std::string &inStr, RecordType curTy)
73 {
74 unsigned int result = kInValidStrIdx;
75 StrInfo curInfo(totalStr, curTy);
76 auto ret = stringHashTable.insert(std::make_pair(inStr, curInfo));
77 if (ret.second) {
78 unsigned int temp = totalStr;
79 stringTable.emplace_back(inStr);
80 ++totalStr;
81 return temp;
82 }
83 return result;
84 }
85
GetStrInTable(const std::string & inStr)86 StrInfo MDClassRange::GetStrInTable(const std::string &inStr)
87 {
88 auto ret = stringHashTable.find(inStr);
89 StrInfo inValidInfo(UINT_MAX, kUndefinedStr);
90 return (ret != stringHashTable.end()) ? ret->second : inValidInfo;
91 }
92
GetStrTyByIdx(size_t curIdx)93 RecordType MDClassRange::GetStrTyByIdx(size_t curIdx)
94 {
95 CHECK_FATAL(curIdx < stringTable.size(), "Array boundary check failed");
96 return GetStrInTable(stringTable[curIdx]).sType;
97 }
98
GetStrByIdx(size_t curIdx)99 const std::string &MDClassRange::GetStrByIdx(size_t curIdx)
100 {
101 CHECK_FATAL(curIdx < stringTable.size(), "Array boundary check failed");
102 return stringTable[curIdx];
103 }
104
ModifyStrTyInTable(const std::string & inStr,RecordType newTy)105 void MDClassRange::ModifyStrTyInTable(const std::string &inStr, RecordType newTy)
106 {
107 auto ret = stringHashTable.find(inStr);
108 CHECK_FATAL(ret != stringHashTable.end(), "find string failed!");
109 ret->second.sType = newTy;
110 }
111
AddDefinedType(unsigned int typesName,std::set<unsigned int> typesSet)112 void MDClassRange::AddDefinedType(unsigned int typesName, std::set<unsigned int> typesSet)
113 {
114 (void)definedTypes.insert(std::make_pair(typesName, typesSet));
115 }
116
AddMDClass(MDClass curClass)117 void MDClassRange::AddMDClass(MDClass curClass)
118 {
119 (void)allClasses.insert(std::make_pair(curClass.GetClassIdx(), curClass));
120 }
121
FillMDClass(unsigned int givenIdx,const MDObject & insertObj)122 void MDClassRange::FillMDClass(unsigned int givenIdx, const MDObject &insertObj)
123 {
124 auto ret = allClasses.find(givenIdx);
125 CHECK_FATAL(ret != allClasses.end(), "Cannot achieve target MD Class");
126 ret->second.AddClassMember(insertObj);
127 }
128
GetOneMDClass(unsigned int givenIdx)129 MDClass MDClassRange::GetOneMDClass(unsigned int givenIdx)
130 {
131 auto ret = allClasses.find(givenIdx);
132 CHECK_FATAL(ret != allClasses.end(), "Cannot achieve target MD Class");
133 return ret->second;
134 }
135
GetOneSpcType(unsigned int givenTyIdx)136 std::set<unsigned int> MDClassRange::GetOneSpcType(unsigned int givenTyIdx)
137 {
138 auto ret = definedTypes.find(givenTyIdx);
139 CHECK_FATAL(ret != definedTypes.end(), "Cannot achieve a defined type");
140 return ret->second;
141 }
142 } /* namespace MDGen */
143