From 02c95fe75ca0d622a30c50817cfdec6835a8a034 Mon Sep 17 00:00:00 2001 From: Wen HU Date: Thu, 22 Sep 2022 14:46:52 -0700 Subject: [PATCH] consolidate TYPEDEF with TYPEALIAS --- .../clang/src/ast_struct2fe_helper.cpp | 2 +- src/hir2mpl/ast_input/common/src/ast_decl.cpp | 6 +- src/hir2mpl/common/src/fe_type_manager.cpp | 4 +- src/mapleall/maple_ir/include/debug_info.h | 16 +-- src/mapleall/maple_ir/include/global_tables.h | 33 ----- src/mapleall/maple_ir/include/keywords.def | 1 - src/mapleall/maple_ir/include/mir_function.h | 5 - src/mapleall/maple_ir/include/mir_parser.h | 1 - src/mapleall/maple_ir/include/mir_scope.h | 45 ++++--- src/mapleall/maple_ir/src/debug_info.cpp | 114 +++++++++--------- src/mapleall/maple_ir/src/global_tables.cpp | 11 -- src/mapleall/maple_ir/src/mir_module.cpp | 4 +- src/mapleall/maple_ir/src/mir_scope.cpp | 19 +-- src/mapleall/maple_ir/src/parser.cpp | 33 +---- src/mapleall/mpl2mpl/src/retype.cpp | 2 +- 15 files changed, 110 insertions(+), 186 deletions(-) diff --git a/src/hir2mpl/ast_input/clang/src/ast_struct2fe_helper.cpp b/src/hir2mpl/ast_input/clang/src/ast_struct2fe_helper.cpp index 586263c2a1..f798fbda4e 100644 --- a/src/hir2mpl/ast_input/clang/src/ast_struct2fe_helper.cpp +++ b/src/hir2mpl/ast_input/clang/src/ast_struct2fe_helper.cpp @@ -156,7 +156,7 @@ bool ASTStructField2FEHelper::ProcessDeclWithContainerImpl(MapleAllocator &alloc CHECK_NULL_FATAL(mirAlias); TypeAttrs typeAttrs = field.GetGenericAttrs().ConvertToTypeAttrs(); MIRAliasVars aliasVar = FEUtils::AddAlias(idx, field.GetSourceType(), typeAttrs); - mirAlias->AddAliasVarMap(idx, aliasVar); + mirAlias->SetAliasVarMap(idx, aliasVar); } field.ClearGenericAttrsContentMap(); return true; diff --git a/src/hir2mpl/ast_input/common/src/ast_decl.cpp b/src/hir2mpl/ast_input/common/src/ast_decl.cpp index 4c560330cc..d97fa6fd5d 100644 --- a/src/hir2mpl/ast_input/common/src/ast_decl.cpp +++ b/src/hir2mpl/ast_input/common/src/ast_decl.cpp @@ -305,7 +305,7 @@ void ASTStruct::GenerateInitStmtImpl(std::list &stmts) { } FEFunction &feFunction = FEManager::GetCurrentFEFunction(); MIRScope *mirScope = feFunction.GetTopMIRScope(); - mirScope->SetTypeAlias(GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(GetName()), + mirScope->SetTypeAliasMap(GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(GetName()), typeDesc.front()->GetTypeIndex()); } @@ -318,7 +318,7 @@ void ASTEnumDecl::GenerateInitStmtImpl(std::list &stmts) { FEFunction &feFunction = FEManager::GetCurrentFEFunction(); MIRScope *mirScope = feFunction.GetTopMIRScope(); MIRTypeByName *type = FEManager::GetTypeManager().GetOrCreateTypeByNameType(GenerateUniqueVarName()); - mirScope->SetTypeAlias(GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(GetName()), type->GetTypeIndex()); + mirScope->SetTypeAliasMap(GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(GetName()), type->GetTypeIndex()); } // ---------- ASTTypedefDecl ---------- @@ -333,7 +333,7 @@ void ASTTypedefDecl::GenerateInitStmtImpl(std::list &stmts) { while (astTypedef != nullptr && !astTypedef->isGlobalDecl) { MIRTypeByName *type = FEManager::GetTypeManager().CreateTypedef( astTypedef->GenerateUniqueVarName(), *astTypedef->GetTypeDesc().front()); - mirScope->SetTypeAlias(GlobalTables::GetStrTable().GetOrCreateStrIdxFromName( + mirScope->SetTypeAliasMap(GlobalTables::GetStrTable().GetOrCreateStrIdxFromName( astTypedef->GetName()), type->GetTypeIndex()); astTypedef = astTypedef->GetSubTypedefDecl(); } diff --git a/src/hir2mpl/common/src/fe_type_manager.cpp b/src/hir2mpl/common/src/fe_type_manager.cpp index 79f6407f9c..5685715a32 100644 --- a/src/hir2mpl/common/src/fe_type_manager.cpp +++ b/src/hir2mpl/common/src/fe_type_manager.cpp @@ -248,8 +248,8 @@ MIRTypeByName *FETypeManager::GetTypeByNameType(const GStrIdx &nameIdx) { MIRTypeByName *FETypeManager::CreateTypedef(const std::string &name, const MIRType &type) { MIRTypeByName *typdefType = GetOrCreateTypeByNameType(name); - if (GlobalTables::GetTypedefTable().GetTyIdxFromMap(typdefType->GetNameStrIdx()) == TyIdx(0)) { - GlobalTables::GetTypedefTable().SetTypedefNameMap(typdefType->GetNameStrIdx(), type.GetTypeIndex()); + if (!module.GetScope()->GetTypeAlias()->Exist(typdefType->GetNameStrIdx())) { + module.GetScope()->SetTypeAliasMap(typdefType->GetNameStrIdx(), type.GetTypeIndex()); } return typdefType; } diff --git a/src/mapleall/maple_ir/include/debug_info.h b/src/mapleall/maple_ir/include/debug_info.h index 595274642b..d25228e5e1 100644 --- a/src/mapleall/maple_ir/include/debug_info.h +++ b/src/mapleall/maple_ir/include/debug_info.h @@ -607,7 +607,7 @@ class DebugInfo { funcScopeLows(std::less(), m->GetMPAllocator().Adapter()), funcScopeHighs(std::less(), m->GetMPAllocator().Adapter()), funcScopeIdStatus(std::less(), m->GetMPAllocator().Adapter()), - typedefStrIdxTyIdxMap(std::less(), m->GetMPAllocator().Adapter()), + globalTypeAliasMap(std::less(), m->GetMPAllocator().Adapter()), constTypeDieMap(std::less(), m->GetMPAllocator().Adapter()), volatileTypeDieMap(std::less(), m->GetMPAllocator().Adapter()), strps(std::less(), m->GetMPAllocator().Adapter()) { @@ -746,7 +746,7 @@ class DebugInfo { MapleMap> funcScopeIdStatus; /* alias type */ - MapleMap typedefStrIdxTyIdxMap; + MapleMap globalTypeAliasMap; MapleMap constTypeDieMap; MapleMap volatileTypeDieMap; @@ -763,7 +763,6 @@ class DebugInfo { void SetupCU(); void BuildDebugInfoEnums(); - void BuildDebugInfoTypedefs(); void BuildDebugInfoContainers(); void BuildDebugInfoGlobalSymbols(); void BuildDebugInfoFunctions(); @@ -845,19 +844,14 @@ class DebugInfo { GStrIdx GetPrimTypeCName(PrimType pty); - void AddScopeDie(MIRScope *scope, bool isLocal); + void AddScopeDie(MIRScope *scope); DBGDie *GetAliasVarTypeDie(const MIRAliasVars &aliasVar, TyIdx tyidx); - void HandleTypeAlias(const MIRScope &scope); - void AddAliasDies(MIRScope &scope, bool isLocal); + void HandleTypeAlias(MIRScope *scope); + void AddAliasDies(MIRScope *scope, bool isLocal); void CollectScopePos(MIRFunction *func, MIRScope *scope); // Functions for calculating the size and offset of each DW_TAG_xxx and DW_AT_xxx void ComputeSizeAndOffset(DBGDie *die, uint32 &cuOffset); - - void AddTypedefMap(GStrIdx stridx, TyIdx tyidx) { - typedefStrIdxTyIdxMap[stridx.GetIdx()] = tyidx.GetIdx(); - } - void DumpTypedefMap() const; }; } // namespace maple #endif // MAPLE_IR_INCLUDE_DBG_INFO_H diff --git a/src/mapleall/maple_ir/include/global_tables.h b/src/mapleall/maple_ir/include/global_tables.h index 97d42c678f..7b7250be47 100644 --- a/src/mapleall/maple_ir/include/global_tables.h +++ b/src/mapleall/maple_ir/include/global_tables.h @@ -765,34 +765,6 @@ class GSymbolTable { std::vector symbolTable; // map symbol idx to symbol node }; -// For debuginfo record typedef name -class TypedefTable { - public: - TypedefTable() {} - virtual ~TypedefTable() = default; - - const std::unordered_map &GetTypedefNameMap() const { - return typedefNameMap; - } - - TyIdx GetTyIdxFromMap(GStrIdx idx) const { - auto it = typedefNameMap.find(idx); - if (it == typedefNameMap.cend()) { - return TyIdx(0); - } - return it->second; - } - - void SetTypedefNameMap(GStrIdx gStrIdx, TyIdx tyIdx) { - typedefNameMap[gStrIdx] = tyIdx; - } - - void Dump() const; - - private: - std::unordered_map typedefNameMap; -}; - class ConstPool { public: std::unordered_map &GetConstU16StringPool() { @@ -871,10 +843,6 @@ class GlobalTables { return globalTables.enumTable; } - static TypedefTable &GetTypedefTable() { - return globalTables.typedefTable; - } - GlobalTables(const GlobalTables &globalTables) = delete; GlobalTables(const GlobalTables &&globalTables) = delete; GlobalTables &operator=(const GlobalTables &globalTables) = delete; @@ -901,7 +869,6 @@ class GlobalTables { StringTable uStrTable; StringTable u16StringTable; EnumTable enumTable; - TypedefTable typedefTable; }; inline MIRType &GetTypeFromTyIdx(TyIdx idx) { diff --git a/src/mapleall/maple_ir/include/keywords.def b/src/mapleall/maple_ir/include/keywords.def index 996272f074..14c906656f 100644 --- a/src/mapleall/maple_ir/include/keywords.def +++ b/src/mapleall/maple_ir/include/keywords.def @@ -98,7 +98,6 @@ KEYWORD(SCOPE) KEYWORD(ALIAS) KEYWORD(ENUMERATION) - KEYWORD(TYPEDEF) KEYWORD(TYPEALIAS) // storage class KEYWORD(pstatic) diff --git a/src/mapleall/maple_ir/include/mir_function.h b/src/mapleall/maple_ir/include/mir_function.h index daba84d31a..54032e4e1a 100644 --- a/src/mapleall/maple_ir/include/mir_function.h +++ b/src/mapleall/maple_ir/include/mir_function.h @@ -754,11 +754,6 @@ class MIRFunction { scope->SetAliasVarMap(idx, vars); } - void AddAliasVarMap(const GStrIdx &idx, const MIRAliasVars &vars) { - SetupScope(); - scope->AddAliasVarMap(idx, vars); - } - bool HasVlaOrAlloca() const { return hasVlaOrAlloca; } diff --git a/src/mapleall/maple_ir/include/mir_parser.h b/src/mapleall/maple_ir/include/mir_parser.h index 059f2ac7d3..f8a2897d8d 100644 --- a/src/mapleall/maple_ir/include/mir_parser.h +++ b/src/mapleall/maple_ir/include/mir_parser.h @@ -99,7 +99,6 @@ class MIRParser { bool ParseDerivedType(TyIdx &tyIdx, MIRTypeKind kind = kTypeUnknown, const GStrIdx &strIdx = GStrIdx(0)); bool ParseType(TyIdx &tyIdx); bool ParseEnumeration(); - bool ParseTypedef(); bool ParseStatement(StmtNodePtr &stmt); bool ParseSpecialReg(PregIdx &pRegIdx); bool ParsePseudoReg(PrimType primType, PregIdx &pRegIdx); diff --git a/src/mapleall/maple_ir/include/mir_scope.h b/src/mapleall/maple_ir/include/mir_scope.h index 57cb62ccf2..7d404f42ef 100644 --- a/src/mapleall/maple_ir/include/mir_scope.h +++ b/src/mapleall/maple_ir/include/mir_scope.h @@ -49,11 +49,6 @@ class MIRAlias { aliasVarMap[idx] = vars; } - void AddAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) { - /* allow same idx, save last aliasVars */ - aliasVarMap[idx] = vars; - } - MapleMap &GetAliasVarMap() { return aliasVarMap; } @@ -66,15 +61,19 @@ class MIRAlias { MapleMap aliasVarMap { module->GetMPAllocator().Adapter() }; }; -class MIRTypeAliasTable { +class MIRTypeAlias { public: - explicit MIRTypeAliasTable(MIRModule *mod) : module(mod) {} - virtual ~MIRTypeAliasTable() = default; + explicit MIRTypeAlias(MIRModule *mod) : module(mod) {} + virtual ~MIRTypeAlias() = default; bool IsEmpty() const { return typeAliasMap.size() == 0; } + bool Exist(GStrIdx idx) const { + return typeAliasMap.find(idx) != typeAliasMap.end(); + } + const MapleMap &GetTypeAliasMap() const { return typeAliasMap; } @@ -140,11 +139,6 @@ class MIRScope { alias->SetAliasVarMap(idx, vars); } - void AddAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) { - /* allow same idx, save last aliasVars */ - alias->AddAliasVarMap(idx, vars); - } - MapleMap &GetAliasVarMap() { return alias->GetAliasVarMap(); } @@ -164,27 +158,38 @@ class MIRScope { SrcPosition GetScopeEndPos(const SrcPosition &pos); bool AddScope(MIRScope *scope); - void SetTypeAlias(GStrIdx gStrIdx, TyIdx tyIdx) { - if (typeAlias == nullptr) { - typeAlias = module->GetMemPool()->New(module); - } + void SetTypeAliasMap(GStrIdx gStrIdx, TyIdx tyIdx) { typeAlias->SetTypeAliasMap(gStrIdx, tyIdx); } - const MIRTypeAliasTable *GetTypeAliasTable() const { + MIRTypeAlias *GetTypeAlias() { return typeAlias; } - void Dump(int32 indent, bool isLocal = true) const; + const MIRTypeAlias *GetTypeAlias() const { + return typeAlias; + } + + void SetIsLocal(bool b) { + isLocal = b; + } + + bool IsLocal() const { + return isLocal; + } + + void DumpTypedef(int32 indent) const; + void Dump(int32 indent) const; void Dump() const; private: MIRModule *module; MIRFunction *func; unsigned id; + bool isLocal; std::pair range; MIRAlias *alias = nullptr; - MIRTypeAliasTable *typeAlias = nullptr; + MIRTypeAlias *typeAlias = nullptr; // subscopes' range should be disjoint MapleVector subScopes { module->GetMPAllocator().Adapter() }; MapleVector> blkSrcPos { module->GetMPAllocator().Adapter() }; diff --git a/src/mapleall/maple_ir/src/debug_info.cpp b/src/mapleall/maple_ir/src/debug_info.cpp index dd20be8767..96d6151a6c 100644 --- a/src/mapleall/maple_ir/src/debug_info.cpp +++ b/src/mapleall/maple_ir/src/debug_info.cpp @@ -303,11 +303,12 @@ void DebugInfo::SetupCU() { compUnit->AddAttr(DW_AT_stmt_list, DW_FORM_sec_offset, kDbgDefaultVal); } -void DebugInfo::AddScopeDie(MIRScope *scope, bool isLocal) { - if (scope->IsEmpty()) { +void DebugInfo::AddScopeDie(MIRScope *scope) { + if (!scope || scope->IsEmpty()) { return; } + bool isLocal = scope->IsLocal(); MIRFunction *func = GetCurFunction(); // for non-function local scope, add a lexical block bool createBlock = isLocal && (scope != func->GetScope()); @@ -323,15 +324,15 @@ void DebugInfo::AddScopeDie(MIRScope *scope, bool isLocal) { } // process type alias - HandleTypeAlias(*scope); + HandleTypeAlias(scope); // process alias - AddAliasDies(*scope, isLocal); + AddAliasDies(scope, isLocal); if (scope->GetSubScopes().size() > 0) { // process subScopes for (auto it : scope->GetSubScopes()) { - AddScopeDie(it, isLocal); + AddScopeDie(it); } } @@ -377,40 +378,60 @@ DBGDie *DebugInfo::GetOrCreateTypeByNameDie(const MIRType &type) { } if (!die) { // look for the typedef - TyIdx undlyingTypeIdx = GlobalTables::GetTypedefTable().GetTyIdxFromMap(type.GetNameStrIdx()); - CHECK_FATAL(undlyingTypeIdx != TyIdx(0), "typedef not found in TypedefTable"); + TyIdx undlyingTypeIdx = module->GetScope()->GetTypeAlias()->GetTyIdxFromMap(type.GetNameStrIdx()); + CHECK_FATAL(undlyingTypeIdx != TyIdx(0), "typedef not found in TypeAliasTable"); die = GetOrCreateTypedefDie(type.GetNameStrIdx(), undlyingTypeIdx); } return die; } -void DebugInfo::HandleTypeAlias(const MIRScope &scope) { - const MIRTypeAliasTable *table = scope.GetTypeAliasTable(); - if (table == nullptr) { +void DebugInfo::HandleTypeAlias(MIRScope *scope) { + const MIRTypeAlias *typeAlias = scope->GetTypeAlias(); + if (typeAlias == nullptr) { return; } - for (auto &i : table->GetTypeAliasMap()) { - uint32 tid = i.second.GetIdx(); - MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(tid); - CHECK_NULL_FATAL(type); - DBGDie *die = nullptr; - if (type->GetKind() == kTypeByName) { - die = GetOrCreateTypeByNameDie(*type); - } else if (tyIdxDieIdMap.find(tid) != tyIdxDieIdMap.end()) { - uint32 id = tyIdxDieIdMap[tid]; - die = idDieMap[id]; - } else { - ASSERT(false, "type alias type not in tyIdxDieIdMap"); - continue; + + if (scope->IsLocal()) { + for (auto &i : typeAlias->GetTypeAliasMap()) { + uint32 tid = i.second.GetIdx(); + MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(tid); + CHECK_NULL_FATAL(type); + DBGDie *die = nullptr; + if (type->GetKind() == kTypeByName) { + die = GetOrCreateTypeByNameDie(*type); + } else if (tyIdxDieIdMap.find(tid) != tyIdxDieIdMap.end()) { + uint32 id = tyIdxDieIdMap[tid]; + die = idDieMap[id]; + } else { + ASSERT(false, "type alias type not in tyIdxDieIdMap"); + continue; + } + (void)die->SetAttr(DW_AT_name, i.first.GetIdx()); + AddStrps(i.first.GetIdx()); + } + } else { + for (auto it : typeAlias->GetTypeAliasMap()) { + globalTypeAliasMap[it.first.GetIdx()] = it.second.GetIdx(); + } + + for (auto it : globalTypeAliasMap) { + (void)GetOrCreateTypeDie(TyIdx(it.second)); + DBGDie *die = GetOrCreateTypedefDie(GStrIdx(it.first), TyIdx(it.second)); + compUnit->AddSubVec(die); + // associate typedef's type with die + for (auto i : module->GetTypeNameTab()->GetGStrIdxToTyIdxMap()) { + if (i.first.GetIdx() == it.first) { + tyIdxDieIdMap[i.second.GetIdx()] = die->GetId(); + break; + } + } } - (void)die->SetAttr(DW_AT_name, i.first.GetIdx()); - AddStrps(i.first.GetIdx()); } } -void DebugInfo::AddAliasDies(MIRScope &scope, bool isLocal) { +void DebugInfo::AddAliasDies(MIRScope *scope, bool isLocal) { MIRFunction *func = GetCurFunction(); - for (auto &i : scope.GetAliasVarMap()) { + for (auto &i : scope->GetAliasVarMap()) { // maple var and die MIRSymbol *mplVar = nullptr; DBGDie *mplDie = nullptr; @@ -423,7 +444,7 @@ void DebugInfo::AddAliasDies(MIRScope &scope, bool isLocal) { mplDie = GetGlobalDie(mplIdx); } // some global vars are introduced by system, and - // some local vars are discarded in O2, skip all them + // some local vars are discarded in O2, skip them if (mplVar == nullptr || mplDie == nullptr) { continue; } @@ -511,25 +532,6 @@ void DebugInfo::BuildDebugInfoEnums() { } } -void DebugInfo::BuildDebugInfoTypedefs() { - for (auto it : GlobalTables::GetTypedefTable().GetTypedefNameMap()) { - module->GetDbgInfo()->AddTypedefMap(it.first, it.second); - } - - for (auto it : typedefStrIdxTyIdxMap) { - (void)GetOrCreateTypeDie(TyIdx(it.second)); - DBGDie *die = GetOrCreateTypedefDie(GStrIdx(it.first), TyIdx(it.second)); - compUnit->AddSubVec(die); - // associate typedef's type with die - for (auto i : module->GetTypeNameTab()->GetGStrIdxToTyIdxMap()) { - if (i.first.GetIdx() == it.first) { - tyIdxDieIdMap[i.second.GetIdx()] = die->GetId(); - break; - } - } - } -} - void DebugInfo::BuildDebugInfoContainers() { for (auto it : module->GetTypeNameTab()->GetGStrIdxToTyIdxMap()) { TyIdx tyIdx = it.second; @@ -546,7 +548,7 @@ void DebugInfo::BuildDebugInfoContainers() { (void)GetOrCreateStructTypeDie(type); break; case kTypeByName: - if (typedefStrIdxTyIdxMap.find(it.first.GetIdx()) == typedefStrIdxTyIdxMap.end()) { + if (globalTypeAliasMap.find(it.first.GetIdx()) == globalTypeAliasMap.end()) { #ifdef DEBUG // not typedef LogInfo::MapleLogger() << "named type " @@ -609,8 +611,8 @@ void DebugInfo::BuildDebugInfo() { // setup debug info for enum types BuildDebugInfoEnums(); - // setup debug info for typedef types - BuildDebugInfoTypedefs(); + // setup debug info for global type alias + HandleTypeAlias(module->GetScope()); // containner types BuildDebugInfoContainers(); @@ -619,7 +621,7 @@ void DebugInfo::BuildDebugInfo() { BuildDebugInfoGlobalSymbols(); // handle global scope - AddScopeDie(module->GetScope(), false); + AddScopeDie(module->GetScope()); // setup debug info for functions BuildDebugInfoFunctions(); @@ -945,7 +947,7 @@ DBGDie *DebugInfo::GetOrCreateFuncDefDie(MIRFunction *func) { CreateFuncLocalSymbolsDies(func, die); // add scope die - AddScopeDie(func->GetScope(), true); + AddScopeDie(func->GetScope()); CollectScopePos(func, func->GetScope()); PopParentDie(); @@ -1807,14 +1809,6 @@ void DebugInfo::Dump(int indent) { return; } -void DebugInfo::DumpTypedefMap() const { - for (auto it : typedefStrIdxTyIdxMap) { - MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(TyIdx(it.second)); - LogInfo::MapleLogger() << " " << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first).c_str() << " " - << type->GetName() << "\n"; - } -} - void DBGExprLoc::Dump() const { LogInfo::MapleLogger() << " " << HEX(GetOp()); for (auto it : simpLoc->GetOpnd()) { diff --git a/src/mapleall/maple_ir/src/global_tables.cpp b/src/mapleall/maple_ir/src/global_tables.cpp index 05d07e3c67..a519918ce4 100644 --- a/src/mapleall/maple_ir/src/global_tables.cpp +++ b/src/mapleall/maple_ir/src/global_tables.cpp @@ -488,17 +488,6 @@ void GSymbolTable::Dump(bool isLocal, int32 indent) const { } } -void TypedefTable::Dump() const { - for (auto it = typedefNameMap.cbegin(); it != typedefNameMap.cend(); ++it) { - const std::string name = GlobalTables::GetStrTable().GetStringFromStrIdx(it->first); - MIRType *underlyingType = GlobalTables::GetTypeTable().GetTypeFromTyIdx(it->second); - CHECK_NULL_FATAL(underlyingType); - LogInfo::MapleLogger() << "TYPEDEF $" << name << " "; - underlyingType->Dump(1); - LogInfo::MapleLogger() << "\n"; - } -} - GlobalTables GlobalTables::globalTables; GlobalTables &GlobalTables::GetGlobalTables() { return globalTables; diff --git a/src/mapleall/maple_ir/src/mir_module.cpp b/src/mapleall/maple_ir/src/mir_module.cpp index d028a01436..334216779e 100644 --- a/src/mapleall/maple_ir/src/mir_module.cpp +++ b/src/mapleall/maple_ir/src/mir_module.cpp @@ -56,6 +56,7 @@ MIRModule::MIRModule(const std::string &fn) mirBuilder = memPool->New(this); dbgInfo = memPool->New(this); scope = memPool->New(this, nullptr); + scope->SetIsLocal(false); IntrinDesc::InitMIRModule(this); } @@ -293,10 +294,9 @@ void MIRModule::DumpGlobals(bool emitStructureType) const { } } if (scope && !scope->IsEmpty()) { - scope->Dump(0, false); + scope->Dump(0); } } - GlobalTables::GetTypedefTable().Dump(); } void MIRModule::Dump(bool emitStructureType, const std::unordered_set *dumpFuncSet) const { diff --git a/src/mapleall/maple_ir/src/mir_scope.cpp b/src/mapleall/maple_ir/src/mir_scope.cpp index b4099b36e3..e7e32bbc00 100644 --- a/src/mapleall/maple_ir/src/mir_scope.cpp +++ b/src/mapleall/maple_ir/src/mir_scope.cpp @@ -20,16 +20,18 @@ namespace maple { static unsigned scopeId = 1; -MIRScope::MIRScope(MIRModule *mod, MIRFunction *f) : module(mod), func(f), id(scopeId++) { +MIRScope::MIRScope(MIRModule *mod, MIRFunction *f) + : module(mod), func(f), id(scopeId++), isLocal(true) { alias = module->GetMemPool()->New(module); + typeAlias = module->GetMemPool()->New(module); } void MIRAlias::Dump(int32 indent, bool isLocal) const { - LogInfo::MapleLogger() << '\n'; bool first = true; for (auto it : aliasVarMap) { if (first) { first = false; + LogInfo::MapleLogger() << '\n'; } else { LogInfo::MapleLogger() << ",\n"; } @@ -67,12 +69,12 @@ void MIRAlias::Dump(int32 indent, bool isLocal) const { } } -void MIRTypeAliasTable::Dump(int32 indent) const { - LogInfo::MapleLogger() << '\n'; +void MIRTypeAlias::Dump(int32 indent) const { bool first = true; for (auto it = typeAliasMap.cbegin(); it != typeAliasMap.cend(); ++it) { if (first) { first = false; + LogInfo::MapleLogger() << '\n'; } else { LogInfo::MapleLogger() << ",\n"; } @@ -169,7 +171,7 @@ bool MIRScope::AddScope(MIRScope *scope) { return true; } -void MIRScope::Dump(int32 indent, bool isLocal) const { +void MIRScope::Dump(int32 indent) const { LogInfo::MapleLogger() << '\n'; SrcPosition low = range.first; SrcPosition high = range.second; @@ -183,10 +185,11 @@ void MIRScope::Dump(int32 indent, bool isLocal) const { high.LineNum() << ", " << high.Column() << ")> {"; - alias->Dump(indent + 1, isLocal); - if (typeAlias != nullptr) { - typeAlias->Dump(indent + 1); + typeAlias->Dump(indent + 1); + if (!typeAlias->IsEmpty() && !alias->IsEmpty()) { + LogInfo::MapleLogger() << ","; } + alias->Dump(indent + 1, isLocal); if (subScopes.size() == 0) { LogInfo::MapleLogger() << "\n"; } diff --git a/src/mapleall/maple_ir/src/parser.cpp b/src/mapleall/maple_ir/src/parser.cpp index e507752ee4..0c60b5a9b7 100644 --- a/src/mapleall/maple_ir/src/parser.cpp +++ b/src/mapleall/maple_ir/src/parser.cpp @@ -1773,29 +1773,6 @@ bool MIRParser::ParseEnumeration() { return true; } -// for debuginfo parsing c typedef -bool MIRParser::ParseTypedef() { - if (lexer.GetTokenKind() != TK_TYPEDEF) { - Error("expect typedef but get "); - return false; - } - TokenKind tokenKind = lexer.NextToken(); - if (tokenKind != TK_gname) { - Error("expect typedef name but get "); - return false; - } - const std::string &name = lexer.GetName(); - GStrIdx strIdx = GlobalTables::GetStrTable().GetOrCreateStrIdxFromName(name); - lexer.NextToken(); - TyIdx tyIdx(0); - if (!ParseType(tyIdx)) { - Error("expect underlying type name but get "); - return false; - } - GlobalTables::GetTypedefTable().SetTypedefNameMap(strIdx, tyIdx); - return true; -} - bool MIRParser::ParseJavaClassInterface(MIRSymbol &symbol, bool isClass) { TokenKind tk = lexer.NextToken(); if (tk != TK_gname) { @@ -2589,7 +2566,7 @@ bool MIRParser::ParseOneScope(MIRScope &scope) { if (scope.GetRangeLow().IsEq(scp->GetRangeLow()) && scope.GetRangeHigh().IsEq(scp->GetRangeHigh())) { for (auto it : scp->GetAliasVarMap()) { - scope.AddAliasVarMap(it.first, it.second); + scope.SetAliasVarMap(it.first, it.second); } for (auto it : scp->GetSubScopes()) { (void)scope.AddScope(it); @@ -2742,7 +2719,10 @@ bool MIRParser::ParseTypeAlias(MIRScope &scope) { Error("expect underlying type name but get "); return false; } - scope.SetTypeAlias(strIdx, tyIdx); + scope.SetTypeAliasMap(strIdx, tyIdx); + if (!scope.IsLocal()) { + mod.GetScope()->GetTypeAlias()->SetTypeAliasMap(strIdx, tyIdx); + } TokenKind tk = lexer.GetTokenKind(); if (tk == TK_coma) { (void)lexer.NextToken(); @@ -2881,7 +2861,7 @@ bool MIRParser::ParseMIR(uint32 fileIdx, uint32 option, bool isIPA, bool isComb) MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(it->second); CHECK_FATAL(type != nullptr, "type is null"); if (type->GetKind() == kTypeByName) { - TyIdx tyIdx = GlobalTables::GetTypedefTable().GetTyIdxFromMap(it->first); + TyIdx tyIdx = mod.GetScope()->GetTypeAlias()->GetTyIdxFromMap(it->first); if (tyIdx.GetIdx() != 0) { continue; } @@ -2942,7 +2922,6 @@ std::map MIRParser::InitFuncPtrMap funcPtrMap[TK_ALIAS] = &MIRParser::ParseAlias; funcPtrMap[TK_SCOPE] = &MIRParser::ParseScope; funcPtrMap[TK_ENUMERATION] = &MIRParser::ParseEnumeration; - funcPtrMap[TK_TYPEDEF] = &MIRParser::ParseTypedef; return funcPtrMap; } diff --git a/src/mapleall/mpl2mpl/src/retype.cpp b/src/mapleall/mpl2mpl/src/retype.cpp index 8e79552f9c..5b74b145a1 100644 --- a/src/mapleall/mpl2mpl/src/retype.cpp +++ b/src/mapleall/mpl2mpl/src/retype.cpp @@ -56,7 +56,7 @@ void Retype::TransmitGenericInfo(MIRFunction &func, StmtNode &stmt) { if (map.find(rhsNameStrIdx) != map.end() && map.at(rhsNameStrIdx).mplStrIdx != rhsNameStrIdx) { CHECK_FATAL(false, "must be"); } - func.AddAliasVarMap(rhsNameStrIdx, aliasVar); + func.SetAliasVarMap(rhsNameStrIdx, aliasVar); } void Retype::RetypeStmt(MIRFunction &func) { -- Gitee