BfMangler.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #pragma once
  2. #include "BfSystem.h"
  3. #include "BeefySysLib/util/SLIList.h"
  4. #include "BeefySysLib/util/SizedArray.h"
  5. NS_BF_BEGIN
  6. class BfType;
  7. class BfFieldInstance;
  8. class BfCustomAttributes;
  9. class BfMangler
  10. {
  11. public:
  12. enum MangleKind
  13. {
  14. MangleKind_GNU,
  15. MangleKind_Microsoft_32,
  16. MangleKind_Microsoft_64
  17. };
  18. struct NameSubstitute
  19. {
  20. public:
  21. enum Kind
  22. {
  23. Kind_None,
  24. Kind_NamespaceAtom,
  25. Kind_TypeDefNameAtom,
  26. Kind_TypeInstName,
  27. Kind_TypeGenericArgs,
  28. Kind_MethodName,
  29. Kind_Prefix,
  30. Kind_PrimitivePrefix,
  31. Kind_GenericParam
  32. };
  33. public:
  34. union
  35. {
  36. int mExtendsIdx;
  37. int mExtendsTypeId;
  38. BfTypeCode mExtendsTypeCode;
  39. };
  40. Kind mKind;
  41. union
  42. {
  43. void* mParam;
  44. BfAtom* mAtom;
  45. BfTypeDef* mTypeDef;
  46. BfType* mType;
  47. BfTypeInstance* mTypeInst;
  48. BfMethodInstance* mMethodInstance;
  49. const char* mPrefix;
  50. };
  51. public:
  52. NameSubstitute(Kind kind, void* param)
  53. {
  54. mExtendsIdx = -1;
  55. mKind = kind;
  56. mParam = param;
  57. }
  58. NameSubstitute(Kind kind, void* param, int extendsId)
  59. {
  60. mExtendsIdx = extendsId;
  61. mKind = kind;
  62. mParam = param;
  63. }
  64. NameSubstitute()
  65. {
  66. mExtendsIdx = -1;
  67. mKind = Kind_None;
  68. mParam = NULL;
  69. }
  70. };
  71. static void Mangle(StringImpl& outStr, MangleKind mangleKind, BfType* type, BfModule* module = NULL);
  72. static void Mangle(StringImpl& outStr, MangleKind mangleKind, BfMethodInstance* methodRef);
  73. static void Mangle(StringImpl& outStr, MangleKind mangleKind, BfFieldInstance* fieldInstance);
  74. static void MangleMethodName(StringImpl& outStr, MangleKind mangleKind, BfTypeInstance* type, const StringImpl& methodName);
  75. static void MangleStaticFieldName(StringImpl& outStr, MangleKind mangleKind, BfTypeInstance* owner, const StringImpl& fieldName, BfType* fieldType = NULL);
  76. static void HandleCustomAttributes(BfCustomAttributes* customAttributes, BfIRConstHolder* constHolder, BfModule* module, StringImpl& name, bool& isCMangle, bool& isCPPMangle);
  77. static void HandleParamCustomAttributes(BfAttributeDirective* attributes, bool isReturn, bool& isConst);
  78. };
  79. class BfGNUMangler : public BfMangler
  80. {
  81. public:
  82. class MangleContext
  83. {
  84. public:
  85. BfModule* mModule;
  86. bool mCCompat;
  87. bool mCPPMangle;
  88. bool mInArgs;
  89. bool mPrefixObjectPointer;
  90. llvm::SmallVector<NameSubstitute, 32> mSubstituteList;
  91. public:
  92. MangleContext()
  93. {
  94. mModule = NULL;
  95. mCCompat = false;
  96. mCPPMangle = false;
  97. mInArgs = false;
  98. mPrefixObjectPointer = false;
  99. }
  100. };
  101. static int ParseSubIdx(StringImpl& name, int strIdx);
  102. static void AddSubIdx(StringImpl& name, int strIdx);
  103. static void AddSizedString(StringImpl& name, const StringImpl& addStr);
  104. static BfTypeCode GetPrimTypeAt(MangleContext& mangleContext, StringImpl& name, int strIdx);
  105. static void AddPrefix(MangleContext& mangleContext, StringImpl& name, int startIdx, const char* prefix);
  106. static void FindOrCreateNameSub(MangleContext& mangleContext, StringImpl& name, const NameSubstitute& newNameSub, int& curMatchIdx, bool& matchFailed);
  107. static void FindOrCreateNameSub(MangleContext& mangleContext, StringImpl& name, BfTypeInstance* typeInst, int& curMatchIdx, bool& matchFailed);
  108. static void FindOrCreateNameSub(MangleContext& mangleContext, StringImpl& name, const NameSubstitute& newNameSub);
  109. public:
  110. static void MangleTypeInst(MangleContext& mangleContext, StringImpl& name, BfTypeInstance* typeInst, BfTypeInstance* postfixTypeInst = NULL, bool* isEndOpen = NULL);
  111. static void Mangle(MangleContext& mangleContext, StringImpl& name, BfType* type, BfType* postfixType = NULL, bool isConst = false);
  112. static String Mangle(BfType* type, BfModule* module = NULL);
  113. static String Mangle(BfMethodInstance* methodRef);
  114. static String MangleMethodName(BfTypeInstance* type, const StringImpl& methodName);
  115. static String Mangle(BfFieldInstance* methodRef);
  116. static String MangleStaticFieldName(BfTypeInstance* type, const StringImpl& fieldName);
  117. };
  118. class BfMSMangler : public BfMangler
  119. {
  120. public:
  121. class MangleContext
  122. {
  123. public:
  124. bool mIsSafeMangle;
  125. BfModule* mModule;
  126. bool mCCompat;
  127. bool mCPPMangle;
  128. bool mIs64Bit;
  129. SizedArray<NameSubstitute, 10> mSubstituteList;
  130. SizedArray<BfType*, 10> mSubstituteTypeList;
  131. bool mWantsGroupStart;
  132. bool mInArgs;
  133. bool mInRet;
  134. public:
  135. MangleContext()
  136. {
  137. mIsSafeMangle = false;
  138. mModule = NULL;
  139. mCCompat = false;
  140. mIs64Bit = false;
  141. mCPPMangle = false;
  142. mWantsGroupStart = false;
  143. mInArgs = false;
  144. mInRet = false;
  145. }
  146. BfModule* GetUnreifiedModule();
  147. };
  148. static void AddGenericArgs(MangleContext& mangleContext, StringImpl& name, const SizedArrayImpl<BfType*>& genericArgs, int numOuterGenericParams = 0);
  149. static void AddStr(MangleContext& mangleContext, StringImpl& name, const StringImpl& str);
  150. static void Mangle(MangleContext& mangleContext, StringImpl& name, BfType* type, bool useTypeList = false, bool isConst = false);
  151. static void Mangle(MangleContext& mangleContext, StringImpl& name, BfTypeInstance* typeInst, bool isAlreadyStarted, bool isOuterType = false);
  152. static void MangleConst(MangleContext& mangleContext, StringImpl& name, int64 val);
  153. void AddPrefix(MangleContext & mangleContext, StringImpl& name, int startIdx, const char * prefix);
  154. static void AddSubIdx(StringImpl& name, int strIdx);
  155. static void AddTypeStart(MangleContext & mangleContext, StringImpl& name, BfType* type);
  156. static bool FindOrCreateNameSub(MangleContext& mangleContext, StringImpl& name, const NameSubstitute& newNameSub);
  157. public:
  158. static void Mangle(StringImpl& outStr, bool is64Bit, BfType* type, BfModule* module = NULL);
  159. static void Mangle(StringImpl& outStr, bool is64Bit, BfMethodInstance* methodRef);
  160. static void Mangle(StringImpl& outStr, bool is64Bit, BfFieldInstance* fieldInstance);
  161. static void MangleMethodName(StringImpl& outStr, bool is64Bit, BfTypeInstance* type, const StringImpl& methodName);
  162. static void MangleStaticFieldName(StringImpl& outStr, bool is64Bit, BfTypeInstance* type, const StringImpl& fieldName, BfType* fieldType = NULL);
  163. };
  164. // A "safe mangle" is used for reconnecting deleted types to their previous id. We make sure we never force a
  165. // PopulateType with this kind of mangle, as referenced types may be deleted and marked as undefined
  166. class BfSafeMangler : public BfMSMangler
  167. {
  168. public:
  169. static String Mangle(BfType* type, BfModule* module = NULL);
  170. };
  171. NS_BF_END