File indexing completed on 2024-04-28 05:48:20

0001 #pragma once
0002 
0003 #include <QAbstractTableModel>
0004 #include <QFont>
0005 
0006 /**
0007  * Corresponding source code location
0008  */
0009 struct SourcePos {
0010     QString file;
0011     int line = 0;
0012     int col = 0;
0013 };
0014 
0015 inline size_t qHash(const SourcePos &key, size_t seed = 0)
0016 {
0017     return qHashMulti(seed, key.line, key.file);
0018 }
0019 
0020 inline bool operator==(const SourcePos &l, const SourcePos &r)
0021 {
0022     return r.file == l.file && r.line == l.line;
0023 }
0024 
0025 struct LabelInRow {
0026     /**
0027      * position of label in text
0028      */
0029     int col = 0;
0030     int len = 0;
0031 };
0032 Q_DECLARE_METATYPE(QList<LabelInRow>)
0033 
0034 struct AsmRow {
0035     QList<LabelInRow> labels;
0036 
0037     // Corresponding source location
0038     // for this asm row
0039     SourcePos source;
0040 
0041     QString text;
0042 };
0043 
0044 class AsmViewModel : public QAbstractTableModel
0045 {
0046     Q_OBJECT
0047 public:
0048     explicit AsmViewModel(QObject *parent);
0049 
0050     enum Columns {
0051         Column_LineNo = 0,
0052         Column_Text = 1,
0053         Column_COUNT,
0054     };
0055     enum Roles {
0056         RowLabels = Qt::UserRole + 1,
0057     };
0058 
0059     int rowCount(const QModelIndex &p) const override
0060     {
0061         return p.isValid() ? 0 : m_rows.size();
0062     }
0063 
0064     int columnCount(const QModelIndex &) const override
0065     {
0066         return Column_COUNT;
0067     }
0068 
0069     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0070 
0071     using CodeGenLines = std::vector<int>;
0072     void setDataFromCE(std::vector<AsmRow> text, QHash<SourcePos, CodeGenLines> sourceToAsm, QHash<QString, int> labelToAsmLines);
0073 
0074     void clear();
0075 
0076     CodeGenLines asmForSourcePos(const SourcePos &p) const
0077     {
0078         return m_sourceToAsm.value(p);
0079     }
0080 
0081     void setFont(const QFont &f)
0082     {
0083         m_font = f;
0084     }
0085 
0086     QFont font() const
0087     {
0088         return m_font;
0089     }
0090 
0091     int sourceLineForAsmLine(const QModelIndex &asmLineIndex)
0092     {
0093         if (!asmLineIndex.isValid()) {
0094             return -1;
0095         }
0096 
0097         int row = asmLineIndex.row();
0098         return m_rows.at(row).source.line;
0099     }
0100 
0101     void highlightLinkedAsm(int line);
0102 
0103     int asmLineForLabel(const QString &label) const
0104     {
0105         return m_labelToAsmLine.value(label, -1);
0106     }
0107 
0108     bool hasError() const
0109     {
0110         return m_hasError;
0111     }
0112 
0113     void setHasError(bool err)
0114     {
0115         m_hasError = err;
0116     }
0117 
0118 private:
0119     std::vector<AsmRow> m_rows;
0120     QHash<SourcePos, CodeGenLines> m_sourceToAsm;
0121     QHash<QString, int> m_labelToAsmLine;
0122     QFont m_font;
0123     int m_hoveredLine = -1;
0124     bool m_hasError = false;
0125 };