DxcPixTypes.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxcPixTypes.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Defines the implementation for the DxcPixType -- and its subinterfaces. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include "DxcPixBase.h"
  12. #include "DxcPixTypes.h"
  13. #include "DxilDiaSession.h"
  14. HRESULT dxil_debug_info::CreateDxcPixType(
  15. DxcPixDxilDebugInfo *pDxilDebugInfo,
  16. llvm::DIType *diType,
  17. IDxcPixType **ppResult)
  18. {
  19. if (auto *BT = llvm::dyn_cast<llvm::DIBasicType>(diType))
  20. {
  21. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixScalarType>(
  22. ppResult,
  23. pDxilDebugInfo->GetMallocNoRef(),
  24. pDxilDebugInfo,
  25. BT);
  26. }
  27. else if (auto *CT = llvm::dyn_cast<llvm::DICompositeType>(diType))
  28. {
  29. switch (CT->getTag())
  30. {
  31. default:
  32. break;
  33. case llvm::dwarf::DW_TAG_array_type:
  34. {
  35. const unsigned FirstDim = 0;
  36. return NewDxcPixDxilDebugInfoObjectOrThrow< DxcPixArrayType>(
  37. ppResult,
  38. pDxilDebugInfo->GetMallocNoRef(),
  39. pDxilDebugInfo,
  40. CT,
  41. FirstDim);
  42. }
  43. case llvm::dwarf::DW_TAG_class_type:
  44. case llvm::dwarf::DW_TAG_structure_type:
  45. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixStructType>(
  46. ppResult,
  47. pDxilDebugInfo->GetMallocNoRef(),
  48. pDxilDebugInfo,
  49. CT);
  50. }
  51. }
  52. else if (auto* DT = llvm::dyn_cast<llvm::DIDerivedType>(diType))
  53. {
  54. switch (DT->getTag())
  55. {
  56. default:
  57. break;
  58. case llvm::dwarf::DW_TAG_const_type:
  59. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixConstType>(
  60. ppResult,
  61. pDxilDebugInfo->GetMallocNoRef(),
  62. pDxilDebugInfo,
  63. DT);
  64. case llvm::dwarf::DW_TAG_typedef:
  65. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixTypedefType>(
  66. ppResult,
  67. pDxilDebugInfo->GetMallocNoRef(),
  68. pDxilDebugInfo,
  69. DT);
  70. }
  71. }
  72. return E_UNEXPECTED;
  73. }
  74. STDMETHODIMP dxil_debug_info::DxcPixConstType::GetName(
  75. _Outptr_result_z_ BSTR *Name)
  76. {
  77. CComPtr<IDxcPixType> BaseType;
  78. IFR(UnAlias(&BaseType));
  79. CComBSTR BaseName;
  80. IFR(BaseType->GetName(&BaseName));
  81. *Name = CComBSTR((L"const " + std::wstring(BaseName)).c_str()).Detach();
  82. return S_OK;
  83. }
  84. STDMETHODIMP dxil_debug_info::DxcPixConstType::GetSizeInBits(
  85. _Outptr_result_z_ DWORD *pSize)
  86. {
  87. CComPtr<IDxcPixType> BaseType;
  88. IFR(UnAlias(&BaseType));
  89. return BaseType->GetSizeInBits(pSize);
  90. }
  91. STDMETHODIMP dxil_debug_info::DxcPixConstType::UnAlias(
  92. _Outptr_result_z_ IDxcPixType **ppType)
  93. {
  94. return CreateDxcPixType(m_pDxilDebugInfo, m_pBaseType, ppType);
  95. }
  96. STDMETHODIMP dxil_debug_info::DxcPixTypedefType::GetName(
  97. _Outptr_result_z_ BSTR *Name)
  98. {
  99. *Name = CComBSTR(CA2W(m_pType->getName().data())).Detach();
  100. return S_OK;
  101. }
  102. STDMETHODIMP dxil_debug_info::DxcPixTypedefType::GetSizeInBits(
  103. _Outptr_result_z_ DWORD *pSize)
  104. {
  105. CComPtr<IDxcPixType> BaseType;
  106. IFR(UnAlias(&BaseType));
  107. return BaseType->GetSizeInBits(pSize);
  108. }
  109. STDMETHODIMP dxil_debug_info::DxcPixTypedefType::UnAlias(
  110. _Outptr_result_z_ IDxcPixType **ppType)
  111. {
  112. return CreateDxcPixType(m_pDxilDebugInfo, m_pBaseType, ppType);
  113. }
  114. STDMETHODIMP dxil_debug_info::DxcPixScalarType::GetName(
  115. _Outptr_result_z_ BSTR *Name)
  116. {
  117. *Name = CComBSTR(CA2W(m_pType->getName().data())).Detach();
  118. return S_OK;
  119. }
  120. STDMETHODIMP dxil_debug_info::DxcPixScalarType::GetSizeInBits(
  121. _Outptr_result_z_ DWORD *pSizeInBits)
  122. {
  123. *pSizeInBits = m_pType->getSizeInBits();
  124. return S_OK;
  125. }
  126. STDMETHODIMP dxil_debug_info::DxcPixScalarType::UnAlias(
  127. _Outptr_result_z_ IDxcPixType **ppType)
  128. {
  129. *ppType = this;
  130. this->AddRef();
  131. return S_FALSE;
  132. }
  133. STDMETHODIMP dxil_debug_info::DxcPixArrayType::GetName(
  134. _Outptr_result_z_ BSTR *Name)
  135. {
  136. return E_FAIL;
  137. }
  138. STDMETHODIMP dxil_debug_info::DxcPixArrayType::GetSizeInBits(
  139. _Outptr_result_z_ DWORD *pSizeInBits)
  140. {
  141. *pSizeInBits = m_pArray->getSizeInBits();
  142. for (unsigned ContainerDims = 0; ContainerDims < m_DimNum; ++ContainerDims)
  143. {
  144. auto *SR = llvm::dyn_cast<llvm::DISubrange>(m_pArray->getElements()[ContainerDims]);
  145. auto count = SR->getCount();
  146. if (count == 0)
  147. {
  148. return E_FAIL;
  149. }
  150. *pSizeInBits /= count;
  151. }
  152. return S_OK;
  153. }
  154. STDMETHODIMP dxil_debug_info::DxcPixArrayType::UnAlias(
  155. _Outptr_result_z_ IDxcPixType **ppType)
  156. {
  157. *ppType = this;
  158. this->AddRef();
  159. return S_FALSE;
  160. }
  161. STDMETHODIMP dxil_debug_info::DxcPixArrayType::GetNumElements(
  162. _Outptr_result_z_ DWORD *ppNumElements)
  163. {
  164. auto* SR = llvm::dyn_cast<llvm::DISubrange>(m_pArray->getElements()[m_DimNum]);
  165. if (SR == nullptr)
  166. {
  167. return E_FAIL;
  168. }
  169. *ppNumElements = SR->getCount();
  170. return S_OK;
  171. }
  172. STDMETHODIMP dxil_debug_info::DxcPixArrayType::GetIndexedType(
  173. _Outptr_result_z_ IDxcPixType **ppIndexedElement)
  174. {
  175. assert(1 + m_DimNum <= m_pArray->getElements().size());
  176. if (1 + m_DimNum == m_pArray->getElements().size())
  177. {
  178. return CreateDxcPixType(m_pDxilDebugInfo, m_pBaseType, ppIndexedElement);
  179. }
  180. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixArrayType>(
  181. ppIndexedElement,
  182. m_pMalloc,
  183. m_pDxilDebugInfo,
  184. m_pArray,
  185. 1 + m_DimNum);
  186. }
  187. STDMETHODIMP dxil_debug_info::DxcPixArrayType::GetElementType(
  188. _Outptr_result_z_ IDxcPixType **ppElementType)
  189. {
  190. return CreateDxcPixType(m_pDxilDebugInfo, m_pBaseType, ppElementType);
  191. }
  192. STDMETHODIMP dxil_debug_info::DxcPixStructType::GetName(
  193. _Outptr_result_z_ BSTR *Name)
  194. {
  195. *Name = CComBSTR(CA2W(m_pStruct->getName().data())).Detach();
  196. return S_OK;
  197. }
  198. STDMETHODIMP dxil_debug_info::DxcPixStructType::GetSizeInBits(
  199. _Outptr_result_z_ DWORD *pSizeInBits)
  200. {
  201. *pSizeInBits = m_pStruct->getSizeInBits();
  202. return S_OK;
  203. }
  204. STDMETHODIMP dxil_debug_info::DxcPixStructType::UnAlias(
  205. _Outptr_result_z_ IDxcPixType **ppType)
  206. {
  207. *ppType = this;
  208. this->AddRef();
  209. return S_FALSE;
  210. }
  211. STDMETHODIMP dxil_debug_info::DxcPixStructType::GetNumFields(
  212. _Outptr_result_z_ DWORD *ppNumFields)
  213. {
  214. *ppNumFields = m_pStruct->getElements()->getNumOperands();
  215. return S_OK;
  216. }
  217. STDMETHODIMP dxil_debug_info::DxcPixStructType::GetFieldByIndex(
  218. DWORD dwIndex,
  219. _Outptr_result_z_ IDxcPixStructField **ppField)
  220. {
  221. if (dwIndex >= m_pStruct->getElements().size())
  222. {
  223. return E_BOUNDS;
  224. }
  225. auto* pDIField = llvm::dyn_cast<llvm::DIDerivedType>(
  226. m_pStruct->getElements()[dwIndex]);
  227. if (pDIField == nullptr)
  228. {
  229. return E_FAIL;
  230. }
  231. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixStructField>(
  232. ppField,
  233. m_pMalloc,
  234. m_pDxilDebugInfo,
  235. pDIField);
  236. }
  237. STDMETHODIMP dxil_debug_info::DxcPixStructType::GetFieldByName(
  238. _In_ LPCWSTR lpName,
  239. _Outptr_result_z_ IDxcPixStructField **ppField)
  240. {
  241. std::string name = CW2A(lpName);
  242. for (auto *Node : m_pStruct->getElements())
  243. {
  244. auto* pDIField = llvm::dyn_cast<llvm::DIDerivedType>(Node);
  245. if (pDIField == nullptr)
  246. {
  247. return E_FAIL;
  248. }
  249. if (pDIField->getName() == name)
  250. {
  251. return NewDxcPixDxilDebugInfoObjectOrThrow<DxcPixStructField>(
  252. ppField,
  253. m_pMalloc,
  254. m_pDxilDebugInfo,
  255. pDIField);
  256. }
  257. }
  258. return E_BOUNDS;
  259. }
  260. STDMETHODIMP dxil_debug_info::DxcPixStructField::GetName(
  261. _Outptr_result_z_ BSTR *Name)
  262. {
  263. *Name = CComBSTR(CA2W(m_pField->getName().data())).Detach();
  264. return S_OK;
  265. }
  266. STDMETHODIMP dxil_debug_info::DxcPixStructField::GetType(
  267. _Outptr_result_z_ IDxcPixType **ppType)
  268. {
  269. return CreateDxcPixType(m_pDxilDebugInfo, m_pType, ppType);
  270. }
  271. STDMETHODIMP dxil_debug_info::DxcPixStructField::GetOffsetInBits(
  272. _Outptr_result_z_ DWORD *pOffsetInBits)
  273. {
  274. *pOffsetInBits = m_pField->getOffsetInBits();
  275. return S_OK;
  276. }