Goose  Diff

Differences From Artifact [46229c668f]:

  • File bs/builtins/types/reference/unify.cpp — part of check-in [947b9d7cfc] at 2020-06-13 22:59:24 on branch trunk —
    • Implemented Initialize overloads for tuples.
    • Lots of cleanup.
    (user: achavasse size: 6019)

To Artifact [f463635838]:

  • File bs/builtins/types/reference/typecheck.cpp — part of check-in [b64ea47f6b] at 2020-06-27 22:05:05 on branch trunk — Clearly separate the type checking rules and the unification rules, instead of lumping them all together in a single set of patterns which is increasingly confusing. (user: achavasse size: 6130)

1
2
3
4
5
6
7
8
9

10
11
12
13
14
15
16
17
18
19
20
21
22
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
110
111


112
113
114
115
116

117
118
119
120
121
122
123
124
125

126
127
128
129
130
131
132
133
134
135
136
137
138
139

140
141
142
143

144
145
146
147
148
149
150
151
152

153
154
155
156
157

158
159
160
161
162
163


164
165
166
1
2
3
4
5
6
7
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
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


110
111
112
113
114
115

116
117
118
119
120
121
122
123
124

125
126
127
128
129
130
131
132
133
134
135
136
137
138

139
140
141
142

143
144
145
146
147
148
149
150
151

152
153
154
155
156

157
158
159
160
161


162
163
164
165
166








-
+
















-
+








-
+







-
+


-
+


-
+






-
+



-
+

-
+




-
+



-
+

-
+




-
+



-
+

-
+





-
+








-
+










-
-
+
+




-
+








-
+













-
+



-
+








-
+




-
+




-
-
+
+



#include "builtins/builtins.h"

using namespace goose;
using namespace goose::ir;
using namespace goose::llr;

namespace goose::builtins
{
    void SetupReferenceUnification( Env& e )
    void SetupReferenceTypeChecking( Env& e )
    {
        auto localVarPattern = GetValueType< LocalVar >( ANYTERM( _ ) );

        auto refTypePattern = ValueToIRExpr(
            Value( GetValueType< ReferenceType >(), TVEC( TSID( reference ), ANYTERM( _ ), ANYTERM( _ ) ) ) );

        auto refTypePatternConstant = ValueToIRExpr(
            Value( GetValueType< ReferenceType >(), TVEC( TSID( reference ), TSID( const ), ANYTERM( _ ) ) ) );

        auto refTypePatternMutable = ValueToIRExpr(
            Value( GetValueType< ReferenceType >(), TVEC( TSID( reference ), TSID( mut ), ANYTERM( _ ) ) ) );

        auto refTypePatternTemporary = ValueToIRExpr(
            Value( GetValueType< ReferenceType >(), TVEC( TSID( reference ), TSID( temporary ), ANYTERM( _ ) ) ) );

        // Reference unification rule.
        e.unificationRuleSet()->addSymRule( URINFOS,
        e.typeCheckingRuleSet()->addTypeCheckingRule( TCRINFOS,

            ParamPat( refTypePattern ),

            ValueToIRExpr( ValuePattern(
                ANYTERM( _ ),
                refTypePattern,
                ANYTERM( _ ) ) ),

            []( const Term& lhs, const Term& rhs, const UnificationContext& uc ) -> UniGen
            []( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
            {
                auto lRefType = *FromValue< ReferenceType >( *ValueFromIRExpr( ValuePatternFromIRExpr( lhs )->type() ) );

                auto rhsVal = *ValueFromIRExpr( rhs );
                auto rRefType = *FromValue< ReferenceType >( *ValueFromIRExpr( rhsVal.type() ) );

                // Unify the behaviors
                for( auto&& [b, uc] : Unify( lRefType.behavior(), rRefType.behavior(), uc ) )
                for( auto&& [b, tcc] : Unify( lRefType.behavior(), rRefType.behavior(), tcc ) )
                {
                    // Unify the types
                    for( auto&& [t, uc] : Unify( lRefType.type(), rRefType.type(), uc ) )
                    for( auto&& [t, tcc] : Unify( lRefType.type(), rRefType.type(), tcc ) )
                    {
                        co_yield { ValueToIRExpr( Value( ValueToIRExpr( ToValue( ReferenceType( t, b ) ) ),
                            rhsVal.llr() ) ), uc };
                            rhsVal.llr() ) ), tcc };
                    }
                }
            }
        );

        // mut -> const reference unification rule.
        e.unificationRuleSet()->addAsymRule( URINFOS,
        e.typeCheckingRuleSet()->addUnificationRule( TCRINFOS,
            TSID( const ),
            TSID( mut ),

            []( const Term& lhs, const Term& rhs, const UnificationContext& uc ) -> UniGen
            []( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
            {
                co_yield { TSID( const ), uc };
                co_yield { TSID( const ), tcc };
            }
        );

        // temp -> const reference unification rule.
        e.unificationRuleSet()->addAsymRule( URINFOS,
        e.typeCheckingRuleSet()->addUnificationRule( TCRINFOS,
            TSID( const ),
            TSID( temp ),

            []( const Term& lhs, const Term& rhs, const UnificationContext& uc ) -> UniGen
            []( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
            {
                co_yield { TSID( const ), uc };
                co_yield { TSID( const ), tcc };
            }
        );

        // temp -> mut reference unification rule.
        e.unificationRuleSet()->addAsymRule( URINFOS,
        e.typeCheckingRuleSet()->addUnificationRule( TCRINFOS,
            TSID( mut ),
            TSID( temp ),

            []( const Term& lhs, const Term& rhs, const UnificationContext& uc ) -> UniGen
            []( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
            {
                co_yield { TSID( mut ), uc };
                co_yield { TSID( mut ), tcc };
            }
        );

        // Reference unification against a param (implicit dereferencing):
        // Unify the referenced value with the param.
        e.unificationRuleSet()->addSymRule( URINFOS,
        e.typeCheckingRuleSet()->addTypeCheckingRule( TCRINFOS,

            ParamPat( ANYTERM( _ ) ),

            ValueToIRExpr( ValuePattern(
                ANYTERM( _ ),
                refTypePattern,
                ANYTERM( _ ) ) ),

            []( const Term& lhs, const Term& rhs, const UnificationContext& uc ) -> UniGen
            []( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
            {
                auto refval = *ValueFromIRExpr( rhs );
                auto ref = FromValue< Reference >( refval );
                if( !ref )
                    co_return;

                auto content = ValueToIRExpr( BuildComputedValue( ref->type().type(),
                    llr::Load( ref->address(), ref->type().type() ) )
                    .setLocationId( refval.locationId() ) );

                // Unify the param with the ref's content
                co_yield Unify( lhs, content, uc );
                // TypeCheck the param with the ref's content
                co_yield TypeCheck( lhs, content, tcc );
            } );

        // LocalVar unification against a param (implicit referencing):
        // Build a mutable ref value.
        e.unificationRuleSet()->addSymRule( URINFOS,
        e.typeCheckingRuleSet()->addTypeCheckingRule( TCRINFOS,

            ParamPat( ANYTERM( _ ) ),

            ValueToIRExpr( ValuePattern(
                ANYTERM( _ ),
                localVarPattern,
                ANYTERM( _ ) ) ),

        []( const Term& lhs, const Term& rhs, const UnificationContext& uc ) -> UniGen
        []( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
        {
            auto ltype = ValuePatternFromIRExpr( lhs )->type();

            auto lvval = *ValueFromIRExpr( rhs );
            auto locvar = FromValue< LocalVar >( lvval );
            if( !locvar )
                co_return;

            ReferenceType rt( locvar->type(), TSID( mut ) );
            auto ref = ValueToIRExpr( BuildComputedValue( ValueToIRExpr( ToValue( rt ) ),
                Load( VarAddress( locvar->index() ), rt.type() ) )
                .setLocationId( lvval.locationId() ) );

            co_yield Unify( lhs, ref, uc );
            co_yield TypeCheck( lhs, ref, tcc );
        } );

        // Implicit referencing of non-variables: build a tempref
        e.unificationRuleSet()->addSymRule( URINFOS,
        e.typeCheckingRuleSet()->addTypeCheckingRule( TCRINFOS,

            ParamPat( refTypePattern ),

            ValueToIRExpr( ValuePattern(
                ANYTERM( _ ),
                ANYTERM( _ ),
                ANYTERM( _ ) ) ),

        []( const Term& lhs, const Term& rhs, const UnificationContext& uc ) -> UniGen
        []( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
        {
            auto rval = *ValueFromIRExpr( rhs );

            ReferenceType rt( rval.type(), TSID( temp ) );
            auto tempIndex = uc.context().codeBuilder()->cfg()->getNewTemporaryIndex();
            auto tempIndex = tcc.context().codeBuilder()->cfg()->getNewTemporaryIndex();
            auto ref = ValueToIRExpr( BuildComputedValue( ValueToIRExpr( ToValue( rt ) ),
                Load( TemporaryAddress( tempIndex, rval ), rt.type() ) )
                .setLocationId( rval.locationId() ) );

            // Unify the param with the ref
            co_yield Unify( lhs, ref, uc );
            // TypeCheck the param with the ref
            co_yield TypeCheck( lhs, ref, tcc );
        } );
    }
}