diff --git a/src/mapleall/maple_ir/include/mir_scope.h b/src/mapleall/maple_ir/include/mir_scope.h index b889c5cefda236f9eeed0276f5a43e0fe90a2154..c2d3ceea04d8cda004c20ab925596cc84519df55 100644 --- a/src/mapleall/maple_ir/include/mir_scope.h +++ b/src/mapleall/maple_ir/include/mir_scope.h @@ -36,13 +36,43 @@ struct MIRAliasVars { TypeAttrs attrs; }; +class MIRAlias { + public: + explicit MIRAlias(MIRModule *mod) : module(mod) {} + ~MIRAlias() = default; + + bool IsEmpty() const { + return aliasVarMap.size() == 0; + } + + void SetAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) { + aliasVarMap[idx] = vars; + } + + void AddAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) { + /* allow same idx, save last aliasVars */ + aliasVarMap[idx] = vars; + } + + MapleMap &GetAliasVarMap() { + return aliasVarMap; + } + + void Dump(int32 indent, bool isLocal = true) const; + + private: + MIRModule *module; + // source to maple variable alias + MapleMap aliasVarMap { module->GetMPAllocator().Adapter() }; +}; + class MIRScope { public: explicit MIRScope(MIRModule *mod, MIRFunction *f = nullptr); ~MIRScope() = default; bool IsEmpty() const { - return aliasVarMap.size() == 0 && subScopes.size() == 0; + return (!alias || alias->IsEmpty()) && subScopes.size() == 0; } bool IsSubScope(const MIRScope *scp) const; @@ -72,16 +102,16 @@ class MIRScope { } void SetAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) { - aliasVarMap[idx] = vars; + alias->SetAliasVarMap(idx, vars); } void AddAliasVarMap(GStrIdx idx, const MIRAliasVars &vars) { /* allow same idx, save last aliasVars */ - aliasVarMap[idx] = vars; + alias->AddAliasVarMap(idx, vars); } MapleMap &GetAliasVarMap() { - return aliasVarMap; + return alias->GetAliasVarMap(); } MapleVector &GetSubScopes() { @@ -107,8 +137,7 @@ class MIRScope { MIRFunction *func; unsigned id; std::pair range; - // source to maple variable alias - MapleMap aliasVarMap { module->GetMPAllocator().Adapter() }; + MIRAlias *alias = nullptr; // subscopes' range should be disjoint MapleVector subScopes { module->GetMPAllocator().Adapter() }; MapleVector> blkSrcPos { module->GetMPAllocator().Adapter() }; diff --git a/src/mapleall/maple_ir/include/mir_type.h b/src/mapleall/maple_ir/include/mir_type.h index 0744abd35c10afeca780775cc64b8c5d894598f7..faaeed6bfea0e16212056b407d5511edd2fdcd3d 100644 --- a/src/mapleall/maple_ir/include/mir_type.h +++ b/src/mapleall/maple_ir/include/mir_type.h @@ -29,6 +29,7 @@ constexpr uint32 kTypeHashLength = 12289; // hash length for mirtype, ref: plan const std::string kRenameKeyWord = "_MNO"; // A static symbol name will be renamed as oriname_MNOxxx. class FieldAttrs; // circular dependency exists, no other choice +class MIRAlias; using TyIdxFieldAttrPair = std::pair; using FieldPair = std::pair; using FieldVector = std::vector; @@ -1481,6 +1482,9 @@ class MIRStructType : public MIRType { bool HasPadding() const; + void SetAlias(MIRAlias *a) { alias = a; } + MIRAlias *GetAlias() const { return alias; } + protected: FieldVector fields{}; std::vector fieldInferredTyIdx{}; @@ -1510,6 +1514,7 @@ class MIRStructType : public MIRType { bool HasTypeParamInFields(const FieldVector &fieldsOfStruct) const; int64 GetBitOffsetFromUnionBaseAddr(FieldID fieldID) const; int64 GetBitOffsetFromStructBaseAddr(FieldID fieldID) const; + MIRAlias *alias = nullptr; }; // java array type, must not be nested inside another aggregate diff --git a/src/mapleall/maple_ir/src/mir_scope.cpp b/src/mapleall/maple_ir/src/mir_scope.cpp index 2883ecc556a4be7d5b1cf3b1461c55dc8682bdce..d569270d79e7491955c775d01c87dcd992df9bb0 100644 --- a/src/mapleall/maple_ir/src/mir_scope.cpp +++ b/src/mapleall/maple_ir/src/mir_scope.cpp @@ -20,7 +20,52 @@ 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++) { + alias = 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; + } else { + LogInfo::MapleLogger() << ",\n"; + } + PrintIndentation(indent); + LogInfo::MapleLogger() << "ALIAS " + << (isLocal ? "%" : "$") + << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) + << ((it.second.isLocal) ? " %" : " $") + << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.mplStrIdx) << " "; + switch (it.second.atk) { + case kATKType: { + TyIdx idx(it.second.index); + GlobalTables::GetTypeTable().GetTypeFromTyIdx(TyIdx(idx))->Dump(0); + break; + } + case kATKString: { + GStrIdx idx(it.second.index); + LogInfo::MapleLogger() << "\"" << GlobalTables::GetStrTable().GetStringFromStrIdx(idx) + << "\""; + break; + } + case kATKEnum: { + MIREnum *mirEnum = GlobalTables::GetEnumTable().enumTable[it.second.index]; + CHECK_NULL_FATAL(mirEnum); + LogInfo::MapleLogger() << "$" << GlobalTables::GetStrTable().GetStringFromStrIdx(mirEnum->GetNameIdx()); + break; + } + default : + break; + } + it.second.attrs.DumpAttributes(); + if (it.second.sigStrIdx) { + LogInfo::MapleLogger() << " \"" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.sigStrIdx) << "\""; + } + } +} // scp is a sub scope // (low (scp.low, scp.high] high] @@ -107,6 +152,7 @@ bool MIRScope::AddScope(MIRScope *scope) { } void MIRScope::Dump(int32 indent, bool isLocal) const { + LogInfo::MapleLogger() << '\n'; SrcPosition low = range.first; SrcPosition high = range.second; PrintIndentation(indent); @@ -117,43 +163,13 @@ void MIRScope::Dump(int32 indent, bool isLocal) const { low.Column() << "), (" << high.FileNum() << ", " << high.LineNum() << ", " << - high.Column() << ")> {\n"; + high.Column() << ")> {"; - for (auto it : aliasVarMap) { - PrintIndentation(indent + 1); - LogInfo::MapleLogger() << "ALIAS " - << (isLocal ? " %" : " $") - << GlobalTables::GetStrTable().GetStringFromStrIdx(it.first) - << ((it.second.isLocal) ? " %" : " $") - << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.mplStrIdx) << " "; - switch (it.second.atk) { - case kATKType: { - TyIdx idx(it.second.index); - GlobalTables::GetTypeTable().GetTypeFromTyIdx(TyIdx(idx))->Dump(0); - break; - } - case kATKString: { - GStrIdx idx(it.second.index); - LogInfo::MapleLogger() << "\"" << GlobalTables::GetStrTable().GetStringFromStrIdx(idx) - << "\""; - break; - } - case kATKEnum: { - MIREnum *mirEnum = GlobalTables::GetEnumTable().enumTable[it.second.index]; - CHECK_NULL_FATAL(mirEnum); - LogInfo::MapleLogger() << "$" << GlobalTables::GetStrTable().GetStringFromStrIdx(mirEnum->GetNameIdx()); - break; - } - default : - break; - } - it.second.attrs.DumpAttributes(); - if (it.second.sigStrIdx) { - LogInfo::MapleLogger() << " \"" << GlobalTables::GetStrTable().GetStringFromStrIdx(it.second.sigStrIdx) << "\""; - } - LogInfo::MapleLogger() << '\n'; - } + alias->Dump(indent + 1, isLocal); + if (subScopes.size() == 0) { + LogInfo::MapleLogger() << "\n"; + } for (auto it : subScopes) { if (!it->IsEmpty()) { it->Dump(indent + 1); diff --git a/src/mapleall/maple_ir/src/mir_type.cpp b/src/mapleall/maple_ir/src/mir_type.cpp index 098b0a8d317b9e52c0bf3f17635fe5078efcc511..5f77b1b899fb3bdc55759e9b57d99de755a506ba 100644 --- a/src/mapleall/maple_ir/src/mir_type.cpp +++ b/src/mapleall/maple_ir/src/mir_type.cpp @@ -14,6 +14,7 @@ */ #include "mir_type.h" #include "mir_symbol.h" +#include "mir_scope.h" #include "printing.h" #include "namemangler.h" #include "global_tables.h" @@ -1347,6 +1348,10 @@ void MIRStructType::DumpFieldsAndMethods(int indent, bool hasMethod) const { LogInfo::MapleLogger() << ","; } DumpMethods(methods, indent); + if (alias && !alias->IsEmpty()) { + LogInfo::MapleLogger() << ","; + alias->Dump(indent, true); + } } void MIRStructType::Dump(int indent, bool dontUseName) const { diff --git a/src/mapleall/maple_ir/src/parser.cpp b/src/mapleall/maple_ir/src/parser.cpp index c04bf3b41cb2f71f45bf768a9cfdbac37a0c0e18..5894e12f2a2df8bdfde6b3166f9026b67b2696f6 100644 --- a/src/mapleall/maple_ir/src/parser.cpp +++ b/src/mapleall/maple_ir/src/parser.cpp @@ -784,6 +784,24 @@ bool MIRParser::ParseFields(MIRStructType &type) { tk = lexer.NextToken(); } } + // alias + while (tk == TK_ALIAS) { + MIRAlias *alias = type.GetAlias(); + if (!alias) { + alias = mod.GetMemPool()->New(&mod); + type.SetAlias(alias); + } + GStrIdx strIdx; + MIRAliasVars aliasVar; + bool status = ParseOneAlias(strIdx, aliasVar); + if (status) { + alias->SetAliasVarMap(strIdx, aliasVar); + } + tk = lexer.GetTokenKind(); + if (tk == TK_coma) { + tk = lexer.NextToken(); + } + } // allow empty class for third party classes we do not have info if (tk == TK_rbrace) { return true; @@ -2663,6 +2681,10 @@ bool MIRParser::ParseOneAlias(GStrIdx &strIdx, MIRAliasVars &aliasVar) { lexer.NextToken(); } aliasVar.sigStrIdx = signStrIdx; + tk = lexer.GetTokenKind(); + if (tk == TK_coma) { + tk = lexer.NextToken(); + } return true; }