|
| 1 | +/*license*/ |
| 2 | +#pragma once |
| 3 | +#include "nodes.h" |
| 4 | +#include "traverse.h" |
| 5 | + |
| 6 | +// ノードを比べる。3 段ある。 |
| 7 | +// |
| 8 | +// x.id() == y.id() 同じノードか。id を見るだけ (nodes.h) |
| 9 | +// identical(a, x, y) 木として同じか。全フィールドと loc まで見る |
| 10 | +// equivalent(a, x, y) 意味として同じか。位置と cosmetic なフィールドを飛ばす |
| 11 | +// |
| 12 | +// cosmetic は nodes.json 側の宣言で、weak と同じ扱い。今は Type::is_explicit だけが |
| 13 | +// 立っている。u8 と明示的に書いたか推論されたかは、型としての意味を変えないため。 |
| 14 | +// lexer::Loc 型のフィールドは宣言なしで飛ばす。位置が意味に効くことはない。 |
| 15 | +// |
| 16 | +// weak は所有辺ではないので、両方の比較とも id の一致だけを見て降りない。 |
| 17 | +// 降りると belong や base で循環する。 |
| 18 | + |
| 19 | +namespace brgen::nast { |
| 20 | + |
| 21 | + enum class CompareMode { |
| 22 | + identical, |
| 23 | + equivalent, |
| 24 | + }; |
| 25 | + |
| 26 | + namespace compare_detail { |
| 27 | + |
| 28 | + template <class T> |
| 29 | + struct is_loc : std::false_type {}; |
| 30 | + |
| 31 | + template <> |
| 32 | + struct is_loc<lexer::Loc> : std::true_type {}; |
| 33 | + |
| 34 | + template <class T> |
| 35 | + constexpr bool compare(Arena& a, Node<T> l, Node<T> r, CompareMode mode); |
| 36 | + |
| 37 | + template <class M> |
| 38 | + constexpr bool compare_field(Arena& a, const M& lv, const M& rv, bool weak, CompareMode mode) { |
| 39 | + if constexpr (node_of<M>::is_node) { |
| 40 | + if (weak) { |
| 41 | + return lv.id() == rv.id(); |
| 42 | + } |
| 43 | + return compare(a, lv, rv, mode); |
| 44 | + } |
| 45 | + else if constexpr (vector_of<M>::is_vector) { |
| 46 | + if (lv.size() != rv.size()) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + for (std::size_t i = 0; i < lv.size(); i++) { |
| 50 | + if (weak) { |
| 51 | + if (lv[i].id() != rv[i].id()) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + } |
| 55 | + else if (!compare(a, lv[i], rv[i], mode)) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + return true; |
| 60 | + } |
| 61 | + else { |
| 62 | + return lv == rv; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + template <class T> |
| 67 | + constexpr bool compare(Arena& a, Node<T> l, Node<T> r, CompareMode mode) { |
| 68 | + if (l.id() == r.id()) { |
| 69 | + return true; // 同じノードなら中身を見るまでもない |
| 70 | + } |
| 71 | + if (l.is_null() || r.is_null()) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + auto* lh = a.header_at(l.id()); |
| 75 | + auto* rh = a.header_at(r.id()); |
| 76 | + if (!lh || !rh || lh->type != rh->type) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + if (mode == CompareMode::identical && !(lh->loc == rh->loc)) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + bool eq = true; |
| 83 | + auto li = lh->data_index; |
| 84 | + auto ri = rh->data_index; |
| 85 | + visit_node_type(lh->type, [&](auto tag) { |
| 86 | + using U = typename decltype(tag)::type; |
| 87 | + auto* ld = a.template data_at<U>(li); |
| 88 | + auto* rd = a.template data_at<U>(ri); |
| 89 | + if (!ld || !rd) { |
| 90 | + eq = (ld == rd); |
| 91 | + return; |
| 92 | + } |
| 93 | + ld->for_each_field(*rd, [&](const char*, const auto& lv, const auto& rv, |
| 94 | + bool weak, bool cosmetic) { |
| 95 | + if (!eq) { |
| 96 | + return; |
| 97 | + } |
| 98 | + using M = std::decay_t<decltype(lv)>; |
| 99 | + if (mode == CompareMode::equivalent && (cosmetic || is_loc<M>::value)) { |
| 100 | + return; |
| 101 | + } |
| 102 | + eq = compare_field(a, lv, rv, weak, mode); |
| 103 | + }); |
| 104 | + }); |
| 105 | + return eq; |
| 106 | + } |
| 107 | + |
| 108 | + } // namespace compare_detail |
| 109 | + |
| 110 | + // 木として同じか。位置も含めて全部見る。 |
| 111 | + template <class T> |
| 112 | + constexpr bool identical(Arena& a, Node<T> l, Node<T> r) { |
| 113 | + return compare_detail::compare(a, l, r, CompareMode::identical); |
| 114 | + } |
| 115 | + |
| 116 | + // 意味として同じか。位置と cosmetic なフィールドは見ない。 |
| 117 | + template <class T> |
| 118 | + constexpr bool equivalent(Arena& a, Node<T> l, Node<T> r) { |
| 119 | + return compare_detail::compare(a, l, r, CompareMode::equivalent); |
| 120 | + } |
| 121 | + |
| 122 | +} // namespace brgen::nast |
0 commit comments