| 787 | : context(context), masterReader(masterReader) {} |
| 788 | |
| 789 | class Iterator { |
| 790 | public: |
| 791 | Iterator(DebugInfoReader &reader, uint64_t opIndex) |
| 792 | : reader(reader), opIndex(opIndex) {} |
| 793 | |
| 794 | /// Return the next debug info attribute for the current operation. |
| 795 | template <typename T> |
| 796 | T next() { |
| 797 | // Check if the index is reserved for special debug info attributes. |
| 798 | if (opIndex == static_cast<uint64_t>(Bytecode::DebugReserved::UnknownLoc)) |
| 799 | return dyn_cast<T>(UnknownLoc::get(&reader.context)); |
| 800 | |
| 801 | // Adjust the index to account for reserved indices. |
| 802 | auto actualOpIndex = |
| 803 | opIndex - static_cast<uint64_t>(Bytecode::DebugReserved::SIZE); |
| 804 | |
| 805 | // Calculate the offset for the current operation index. |
| 806 | if (actualOpIndex >= reader.diIndexOffsets.size()) |
| 807 | return T(); |
| 808 | auto offset = reader.diIndexOffsets[actualOpIndex]; |
| 809 | |
| 810 | // Validate size to prevent excessive memory allocation. |
| 811 | if (reader.diIndices.size() > (std::numeric_limits<uint32_t>::max() - 1)) |
| 812 | return T(); |
| 813 | if (offset > (std::numeric_limits<uint32_t>::max() - 1)) |
| 814 | return T(); |
| 815 | if (offset + opIndexOffset >= reader.diIndices.size()) |
| 816 | return T(); |
| 817 | offset += opIndexOffset++; |
| 818 | |
| 819 | // Return the next debug info attribute for the current operation. |
| 820 | return reader.getDebugInfo<T>(reader.diIndices[offset]); |
| 821 | } |
| 822 | |
| 823 | private: |
| 824 | DebugInfoReader &reader; |
| 825 | uint64_t opIndex; |
| 826 | uint64_t opIndexOffset = 0; |
| 827 | }; |
| 828 | |
| 829 | Iterator getIterator(uint64_t opIndex) { return Iterator(*this, opIndex); } |
| 830 | |