Goose  Diff

Differences From Artifact [56906993bf]:

  • File bs/builtins/types/localvar/localvar.cpp — part of check-in [27fc719d74] at 2019-08-16 14:48:20 on branch trunk — Added a new type of template expression: TVec, along with a helper function to make it possible for parametric types to be constructed either normally or as a template expression when passed template parameters. Only used by LocalVar for now, parametric runtime types require some refactoring. (user: achavasse size: 3097)

To Artifact [04a9b106b3]:

  • File bs/builtins/types/localvar/localvar.cpp — part of check-in [5a361c8b86] at 2019-08-16 20:33:20 on branch trunk — Implemented variable declarations with local type inference. (user: achavasse size: 7434)

32
33
34
35
36
37
38























































































































39
40
41
42
43
44
45
        else
        {
            p.pushValue( InvokeOverloadSet( p.resolver()->context(),
                p.resolver()->context().env()->extInitializeLocalVar(),
                MakeTuple( ToValue( lv ) ) ) );
        }
























































































































        auto locVar = ToValue( lv );
        auto identity = AppendToVectorTerm( p.resolver()->context().identity(), name );

        p.resolver()->context().env()->storeValue( identity, ANYTERM( _ ),
            ValueToIRExpr( locVar ) );

        p.resolver()->clearLookAheadCache();







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
        else
        {
            p.pushValue( InvokeOverloadSet( p.resolver()->context(),
                p.resolver()->context().env()->extInitializeLocalVar(),
                MakeTuple( ToValue( lv ) ) ) );
        }

        auto locVar = ToValue( lv );
        auto identity = AppendToVectorTerm( p.resolver()->context().identity(), name );

        p.resolver()->context().env()->storeValue( identity, ANYTERM( _ ),
            ValueToIRExpr( locVar ) );

        p.resolver()->clearLookAheadCache();
        return locVar;
    }

    Value DeclareLocalVarWithTypeInference( Parser& p, const Term& typeTExpr, StringId name, const Value& initVal )
    {
        const auto& c = p.resolver()->context();

        // To infer the type and obtain a suitable initialization function,
        // we want the declaration to behave as if it were the following function call:
        // Params: ( $T,  _ ( LocalVar[$T], initVal ) initFunc )
        // Args:   ( pat, InitializeLocalVar                   )
        //
        // Where pat is the TNamedDecl's type pattern and initVal is  the initialization
        // expression.

        // We construct the above expressions and unify them. The best solution (if any)
        // will give us the wanted type and the function to invoke to initialize it.

        // Create the _ texpr.
        static auto anyTVar = ValueToIRExpr( ToValue( TVar( "_"_sid ) ) );

        // The $T texpr.
        static auto TTVar = ValueToIRExpr( ToValue( TVar( "T"_sid ) ) );

        // Create the LocalVar[$T] param.
        auto locVarPatParam = ValueToIRExpr( ToValue( TNamedDecl( GetValueType< LocalVar >( TTVar ), "lv"_sid ) ) );

        auto initValPattern = ValueToIRExpr( ValuePattern( TSID( constant ), initVal.type(), MkHole( "_"_sid ) ) );

        // Create the _ ( LocalVar[pat], initType ) initFunc param.
        auto initFuncTFTParam = ValueToIRExpr( ToValue(
            TNamedDecl( ValueToIRExpr( ToValue( TFuncType( DomainAny(), anyTVar, VEC( move( locVarPatParam ), move( initValPattern ) ) ) ) ),
            "initFunc"_sid ) ) );

        // Create our parameter list pattern.
        auto paramPat = VEC(
            *BuildTemplateSignature( c, TTVar ),
            *BuildTemplateSignature( c, initFuncTFTParam )
        );

        // Create our arg list patter,.
        auto args = VEC(
            *BuildTemplateArgPattern( c, typeTExpr ),
            ValueToIRExpr( ToValue( c.env()->extInitializeLocalVar() ) )
        );

        optional< UnificationContext > bestUC;
        optional< Term > bestSol;
        bool ambiguous = false;

        for( auto&& [s, uc] : FullUnify( paramPat, args, c ) )
        {
            if( !bestSol || uc.score() > bestUC->score() )
            {
                bestUC = uc;
                bestSol = s;
                ambiguous = false;
                continue;
            }

            if( uc.score() < bestUC->score() )
                continue;

            if( s != bestSol )
                ambiguous = true;
        }

        if( ambiguous )
        {
            // TODO display details
            DiagnosticsManager::GetInstance().emitErrorMessage( 0,
                "ambiguous variable type inference." );
            return PoisonValue();
        }

        if( !bestSol )
        {
            // TODO display details
            DiagnosticsManager::GetInstance().emitErrorMessage( 0,
                "variable initialization type mismatch." );
            return PoisonValue();
        }

        auto ppCallPat = Postprocess( *bestSol, *bestUC );
        if( !ppCallPat )
            return PoisonValue();

        // Perform the setup of the type template expression. This will create local bindings
        // for tvars ($whatever), if any, making them available for further use.
        TemplateSetup( c, *bestUC, typeTExpr );

        auto callDecomp = Decompose( *ppCallPat,
            Vec(
                SubTerm(),  // locvar
                SubTerm()   // initializer
            )
        );

        auto&& [type, initializer] = *callDecomp;
        auto initializerVal = *ValueFromIRExpr( initializer );

        auto cfgId = p.cfg()->uniqueId();
        auto index = p.cfg()->getNewTemporaryIndex();

        LocalVar lv( type, cfgId, index );

        auto bb = p.currentBB();
        bb->emplace_back( AllocVar( *ValueFromIRExpr( lv.type() ), cfgId, index ) );

        p.pushValue( ResolveInvocation( c, GetInvocationRule( *c.env(), initializerVal ), initializerVal,
            MakeTuple( ToValue( lv ), initVal ) ) );

        auto locVar = ToValue( lv );
        auto identity = AppendToVectorTerm( p.resolver()->context().identity(), name );

        p.resolver()->context().env()->storeValue( identity, ANYTERM( _ ),
            ValueToIRExpr( locVar ) );

        p.resolver()->clearLookAheadCache();