Goose  Diff

Differences From Artifact [ffe3ff4027]:

  • File bs/eir/match.h — part of check-in [1793989d05] at 2024-02-09 18:05:28 on branch trunk — Lowering: completely reworked all lowering to happen in the same two unified extension points upstream of all three CIR consumers (user: zlodo size: 3410)

To Artifact [e4c5fe700d]:

  • File bs/eir/match.h — part of check-in [0db147f117] at 2024-09-15 20:24:31 on branch cir-ssa-refactor — Add clang format settings, reformat everything (user: achavasse size: 3201)

23
24
25
26
27
28
29
30
31
32




33


34
35
36
37
38
39
40







41
42
43


44
45
46
47



48
49
50


51
52
53
54



55
56
57
58



59
60
61
62
63
64
65
66
67
68
69
70
71












72
73
74
75



76
77
78
79
80
81
82





83
84
85
86



87
88
89
90
91




92
93

94
95
96


97
98
99
100



101
102
103
104

105


106
107
23
24
25
26
27
28
29



30
31
32
33
34
35
36







37
38
39
40
41
42
43
44


45
46
47



48
49
50
51


52
53
54



55
56
57




58
59
60
61












62
63
64
65
66
67
68
69
70
71
72
73
74



75
76
77

78





79
80
81
82
83
84



85
86
87
88




89
90
91
92
93

94
95


96
97
98



99
100
101
102
103
104

105

106
107
108
109







-
-
-
+
+
+
+

+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+

-
-
+
+

-
-
-
+
+
+

-
-
+
+

-
-
-
+
+
+
-
-
-
-
+
+
+

-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+

-
-
-
+
+
+
-

-
-
-
-
-
+
+
+
+
+

-
-
-
+
+
+

-
-
-
-
+
+
+
+

-
+

-
-
+
+

-
-
-
+
+
+



-
+
-
+
+



            return m_numVars < rhs.m_numVars;
        }
    };

    class MatchSolution
    {
        public:
            size_t complexity() const { return m_complexity; }
            size_t numVars() const;
      public:
        size_t complexity() const { return m_complexity; }

        size_t numVars() const;

        MatchScore score() const
        {
            MatchScore score() const { return MatchScore{ m_complexity, m_pVars ? m_pVars->size() : 0 }; }

            template< typename T >
            const T* getVar( StringId name ) const
            {
                if( name == "_"_sid )
                    return nullptr;
            return MatchScore{ m_complexity, m_pVars ? m_pVars->size() : 0 };
        }

        template< typename T > const T* getVar( StringId name ) const
        {
            if( name == "_"_sid )
                return nullptr;

                if( !m_pVars )
                    return nullptr;
            if( !m_pVars )
                return nullptr;

                auto it = m_pVars->find( name );
                if( it == m_pVars->end() )
                    return nullptr;
            auto it = m_pVars->find( name );
            if( it == m_pVars->end() )
                return nullptr;

                return any_cast< T >( &it->second );
            }
            return any_cast< T >( &it->second );
        }

            // Tries to set the variable. If it already exists but have a different
            // value, returns false.
            template< typename T >
        // Tries to set the variable. If it already exists but have a different
        // value, returns false.
        template< typename T > bool setVar( StringId name, T&& val )
            bool setVar( StringId name, T&& val )
            {
                if( name == "_"_sid )
                    return true;
        {
            if( name == "_"_sid )
                return true;

                if( m_pVars )
                {
                    auto it = m_pVars->find( name );
                    if( it != m_pVars->end() )
                    {
                        using TT = remove_cvref_t< T >;
                        if( const auto* pExistingVal = any_cast< TT >( &it->second ) )
                        {
                            if constexpr( is_same_v< TT, VecLength > )
                            {
                                if( !AreVecLengthsCompatible( *pExistingVal, val ) )
                                    return false;
            if( m_pVars )
            {
                auto it = m_pVars->find( name );
                if( it != m_pVars->end() )
                {
                    using TT = remove_cvref_t< T >;
                    if( const auto* pExistingVal = any_cast< TT >( &it->second ) )
                    {
                        if constexpr( is_same_v< TT, VecLength > )
                        {
                            if( !AreVecLengthsCompatible( *pExistingVal, val ) )
                                return false;

                                it->second = VecLength(
                                    max( pExistingVal->minLength(), val.minLength() ),
                                    pExistingVal->isVariable() && val.isVariable()
                            it->second =
                                VecLength( max( pExistingVal->minLength(), val.minLength() ),
                                    pExistingVal->isVariable() && val.isVariable() );
                                );

                                return true;
                            }
                            else
                                return *pExistingVal == val;
                        }
                            return true;
                        }
                        else
                            return *pExistingVal == val;
                    }

                        return false;
                    }
                }
                    return false;
                }
            }

                setupVars();
                m_pVars->emplace( name, forward< T >( val ) );
                return true;
            }
            setupVars();
            m_pVars->emplace( name, forward< T >( val ) );
            return true;
        }

            void addComplexity( uint32_t n ) { m_complexity += n; }
        void addComplexity( uint32_t n ) { m_complexity += n; }

        private:
            void setupVars();
      private:
        void setupVars();

            ptr< unordered_map< StringId, any > > m_pVars;
            uint32_t m_complexity = 0;    // Each matched structural element of the pattern (vector) adds 1,
                                          // each matched literal element of the pattern adds 2
        ptr< unordered_map< StringId, any > > m_pVars;
        uint32_t m_complexity = 0; // Each matched structural element of the pattern (vector)
                                   // adds 1, each matched literal element of the pattern adds 2
    };

    template< typename U >
    static Generator< pair< MatchSolution, const U& > > Match( const Term& expression, const Trie< U >& patterns );
    static Generator< pair< MatchSolution, const U& > > Match(
}
        const Term& expression, const Trie< U >& patterns );
} // namespace goose::eir

#endif