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
|
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
|
-
-
+
+
-
+
-
+
-
-
+
+
-
+
-
+
-
-
-
-
-
+
+
-
+
-
+
-
-
-
-
|
return nullopt;
return TDecl( *typeSig, name );
}
TFuncType BuildTFuncType( const Term& domain, const Value& returnType, const Value& params )
{
immer::vector< Term > v;
auto vt = v.transient();
auto v = make_shared< Vector >();
v->reserve( TupleSize( params ) );
ForEachInTuple( params, [&]( auto&& param )
{
vt.push_back( ValueToIRExpr( param ) );
v->append( ValueToIRExpr( param ) );
return true;
} );
return TFuncType( domain, ValueToIRExpr( returnType ), TERM( make_shared< Vector >( vt.persistent() ) ) );
return TFuncType( domain, ValueToIRExpr( returnType ), v );
}
optional< Term > BuildTFuncSignature( const Context& c, const TFuncType& tft )
{
immer::vector< Term > v;
auto vt = v.transient();
auto v = make_shared< Vector >();
v->reserve( VecSize( tft.params() ) );
bool success = true;
ForEachInVectorTerm( tft.params(), [&]( auto&& param )
{
auto teSig = BuildTemplateSignature( c, param );
if( !teSig )
{
DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( param )->locationId(),
"Invalid template parameter." );
success = false;
return false;
}
vt.push_back( move( *teSig ) );
v->append( move( *teSig ) );
return true;
} );
if( !success )
return nullopt;
auto rtSig = BuildTemplateSignature( c, tft.returnType() );
if( !rtSig )
{
DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( tft.returnType() )->locationId(),
"Invalid template return type or texpr." );
return nullopt;
}
return VEC(
return VEC( tft.domain(), v, *rtSig );
tft.domain(),
TERM( make_shared< Vector >( vt.persistent() ) ),
*rtSig );
}
Value BuildTFunc( const Context& c, const TFuncType& tft, const Term& identity, const Value& params, ptr< void > body )
{
auto sig = BuildTFuncSignature( c, tft );
if( !sig )
return PoisonValue();
return ToValue( TFunc( tft, *sig, identity, body ) );
}
optional< Term > BuildArgPatternFromTFuncType( const Context& c, const Value& tfuncType )
{
const auto& ftype = FromValue< TFuncType >( tfuncType );
assert( ftype );
immer::vector< Term > apv;
auto apvt = apv.transient();
auto apv = make_shared< Vector >();
apv->reserve( VecSize( ftype->params() ) );
bool success = true;
ForEachInVectorTerm( ftype->params(), [&]( auto&& param )
{
auto teArgPat = BuildTemplateArgPattern( c, param );
if( !teArgPat )
{
DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( param )->locationId(),
"Invalid template parameter." );
success = false;
return false;
}
apvt.push_back( move( *teArgPat ) );
apv->append( move( *teArgPat ) );
return true;
} );
if( !success )
return nullopt;
auto rtArgPat = BuildTemplateArgPattern( c, ftype->returnType() );
if( !rtArgPat )
{
DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromIRExpr( ftype->returnType() )->locationId(),
"Invalid template return type or texpr." );
return nullopt;
}
return VEC(
return VEC( ftype->domain(), apv,*rtArgPat );
ftype->domain(),
TERM( make_shared< Vector >( apvt.persistent() ) ),
*rtArgPat
);
}
}
|