Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | Simplified ir::Vector, use plain std::vector to store the terms. immer::vector was pointless for that since we pretty much never actually take advantage of them being CoW in this case. |
|---|---|
| Downloads: | Tarball | ZIP archive |
| Timelines: | family | ancestors | descendants | both | trunk |
| Files: | files | file ages | folders |
| SHA3-256: |
f2ca82dfce6ffddae6f7221ad620b430 |
| User & Date: | achavasse 2019-08-16 23:52:52.491 |
Context
|
2019-08-17
| ||
| 14:49 |
| |
|
2019-08-16
| ||
| 23:52 | Simplified ir::Vector, use plain std::vector to store the terms. immer::vector was pointless for that since we pretty much never actually take advantage of them being CoW in this case. check-in: f2ca82dfce user: achavasse tags: trunk | |
| 20:33 | Implemented variable declarations with local type inference. check-in: 5a361c8b86 user: achavasse tags: trunk | |
Changes
Changes to bs/builtins/types/func/build.cpp.
1 2 3 4 5 6 |
#include "builtins/builtins.h"
namespace empathy::builtins
{
FuncType BuildFuncType( const Term& domain, const Value& returnType, const Value& params )
{
| | | | | | < | 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 |
#include "builtins/builtins.h"
namespace empathy::builtins
{
FuncType BuildFuncType( const Term& domain, const Value& returnType, const Value& params )
{
auto tv = make_shared< Vector >();
tv->reserve( TupleSize( params ) );
ForEachInTuple( params, [&]( auto&& param )
{
if( IsDecl( param ) )
{
auto decl = *FromValue< Decl >( param );
tv->append( ValueToIRExpr( ValuePattern( MkHole( "_"_sid ), move( decl.type() ), MkHole( "_"_sid ) ) ) );
}
else if( param.isConstant() )
tv->append( ValueToIRExpr( param ) );
return true;
} );
return FuncType( domain, ValueToIRExpr( returnType ), tv );
}
Func BuildExternalFunc( const FuncType& funcType, const string& symbol )
{
return Func( funcType, symbol);
}
|
| ︙ | ︙ | |||
88 89 90 91 92 93 94 |
return Func( funcType, unparsedBody, pFuncLLR.get() );
}
Term BuildCallPatternFromFuncType( const Value& funcType )
{
auto ftype = *FromValue< FuncType >( funcType );
| < | > | | < < < | < | 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 |
return Func( funcType, unparsedBody, pFuncLLR.get() );
}
Term BuildCallPatternFromFuncType( const Value& funcType )
{
auto ftype = *FromValue< FuncType >( funcType );
auto apv = make_shared< Vector >();
apv->reserve( VecSize( ftype.params() ) );
ForEachInVectorTerm( ftype.params(), [&]( auto&& param )
{
auto result = Decompose( param,
Vec(
Lit( "value"_sid ),
SubTerm(),
SubTerm(),
SubTerm(),
Val< LocationId >()
)
);
assert( result );
auto&& [sort, type, val, locId] = *result;
if( sort == TSID( constant ) )
apv->append( param );
else
apv->append( ValueToIRExpr( ValuePattern( TSID( computed ), type, TERM( ptr< void >() ) ) ) );
return true;
} );
return VEC( ftype.domain(), apv,ftype.returnType() );
}
}
|
Changes to bs/builtins/types/func/func.cpp.
| ︙ | ︙ | |||
238 239 240 241 242 243 244 |
pFuncLLR->body() = cfg;
return true;
}
Term BuildArgListForCall( const FuncType& ft, const Term& unifiedArgs )
{
| | | | | | 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
pFuncLLR->body() = cfg;
return true;
}
Term BuildArgListForCall( const FuncType& ft, const Term& unifiedArgs )
{
auto av = make_shared< Vector >();
av->reserve( VecSize( ft.params() ) );
ForEachInVectorTerms( ft.params(), unifiedArgs, [&]( auto&& p, auto&& a )
{
auto vp = ValuePatternFromIRExpr( p );
if( vp->val() == MkHole( "_"_sid ) )
av->append( a );
return true;
} );
return av;
}
}
namespace empathy::ir
{
const Term& Bridge< FuncType >::Type()
{
|
| ︙ | ︙ |
Changes to bs/builtins/types/template/build.cpp.
| ︙ | ︙ | |||
9 10 11 12 13 14 15 |
return nullopt;
return TDecl( *typeSig, name );
}
TFuncType BuildTFuncType( const Term& domain, const Value& returnType, const Value& params )
{
| | | | | | | | | < < < < | > | | < < < < | 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 )
{
auto v = make_shared< Vector >();
v->reserve( TupleSize( params ) );
ForEachInTuple( params, [&]( auto&& param )
{
v->append( ValueToIRExpr( param ) );
return true;
} );
return TFuncType( domain, ValueToIRExpr( returnType ), v );
}
optional< Term > BuildTFuncSignature( const Context& c, const TFuncType& tft )
{
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;
}
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( tft.domain(), v, *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 );
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;
}
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( ftype->domain(), apv,*rtArgPat );
}
}
|
Changes to bs/builtins/types/template/rules.cpp.
| ︙ | ︙ | |||
157 158 159 160 161 162 163 |
class TVecTemplateRule : public TemplateRule
{
optional< Term > buildSignature( const Context& c, const Value& val ) const final
{
auto tvec = FromValue< TVec >( val );
assert( tvec );
| | | | | | | 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
class TVecTemplateRule : public TemplateRule
{
optional< Term > buildSignature( const Context& c, const Value& val ) const final
{
auto tvec = FromValue< TVec >( val );
assert( tvec );
auto v = make_shared< Vector >();
v->reserve( tvec->content()->terms().size() );
for( auto&& x : tvec->content()->terms() )
{
if( auto s = BuildTemplateSignature( c, x ) )
v->append( move( *s ) );
else
v->append( x );
}
return v;
}
Value buildParamDecl( const Context& c, const Value& param, const Value& arg ) const final
{
return arg;
}
|
| ︙ | ︙ | |||
190 191 192 193 194 195 196 |
}
optional< Term > buildArgPattern( const Context& c, const Value& val ) const final
{
auto tvec = FromValue< TVec >( val );
assert( tvec );
| | | | | | 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
}
optional< Term > buildArgPattern( const Context& c, const Value& val ) const final
{
auto tvec = FromValue< TVec >( val );
assert( tvec );
auto v = make_shared< Vector >();
v->reserve( tvec->content()->terms().size() );
for( auto&& x : tvec->content()->terms() )
{
auto s = BuildTemplateArgPattern( c, x );
if( !s )
return nullopt;
v->append( move( *s ) );
}
return v;
}
};
class ValueTemplateRule : public TemplateRule
{
optional< Term > buildSignature( const Context& c, const Value& val ) const final
{
|
| ︙ | ︙ |
Changes to bs/codegen/type.cpp.
| ︙ | ︙ | |||
38 39 40 41 42 43 44 |
if( !rt )
return nullopt;
vector< llvm::Type* > paramTypes;
const auto& paramVec = *get< pvec >( ft.params() );
paramTypes.reserve( paramVec.terms().size() );
| | | | 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
if( !rt )
return nullopt;
vector< llvm::Type* > paramTypes;
const auto& paramVec = *get< pvec >( ft.params() );
paramTypes.reserve( paramVec.terms().size() );
auto pv = make_shared< Vector >();
pv->reserve( VecSize( ft.params() ) );
bool success = true;
ForEachInVectorTerm( ft.params(), [&]( auto&& p )
{
// Params are not stored explicitely, but as patterns
// for expediency (where the value is either a hole or an actual
// value depending on whether this is a specialization param),
|
| ︙ | ︙ | |||
68 69 70 71 72 73 74 |
success = false;
return false;
}
paramTypes.emplace_back( GetLLVMType( *type ) );
vp->type() = ValueToIRExpr( *type );
| | | | < | 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 |
success = false;
return false;
}
paramTypes.emplace_back( GetLLVMType( *type ) );
vp->type() = ValueToIRExpr( *type );
pv->append( ValueToIRExpr( *vp ) );
}
else
pv->append( p );
return true;
} );
if( !success )
return nullopt;
auto* pLLVMType = llvm::FunctionType::get( GetLLVMType( *rt ), paramTypes, false );
if( !pLLVMType )
return nullopt;
return FuncType( ft.domain(), ValueToIRExpr( *rt ), pv, pLLVMType );
}
}
|
Changes to bs/ir/helpers.h.
1 2 3 4 5 6 7 8 9 10 11 12 |
#ifndef EMPATHY_IR_HELPERS_H
#define EMPATHY_IR_HELPERS_H
namespace empathy::ir
{
template< typename... T >
Term AppendToVectorTerm( const Term& vectorTerm, T&&... t );
extern Term ConcatenateVectorTerms( const Term& vector1, const Term& vector2 );
extern Term TakeVectorTerm( const Term& vectorTerm, size_t n );
extern Term DropVectorTerm( const Term& vectorTerm, size_t n );
| > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#ifndef EMPATHY_IR_HELPERS_H
#define EMPATHY_IR_HELPERS_H
namespace empathy::ir
{
static inline auto VecSize( const Term& vectorTerm )
{
return get< pvec >( vectorTerm )->terms().size();
}
template< typename... T >
Term AppendToVectorTerm( const Term& vectorTerm, T&&... t );
extern Term ConcatenateVectorTerms( const Term& vector1, const Term& vector2 );
extern Term TakeVectorTerm( const Term& vectorTerm, size_t n );
extern Term DropVectorTerm( const Term& vectorTerm, size_t n );
|
| ︙ | ︙ |
Changes to bs/ir/vector.h.
| ︙ | ︙ | |||
32 33 34 35 36 37 38 |
class Vector
{
public:
Vector() {}
template< typename T >
| | > > > > > > | | > > | < > > > > | | < | | > | | > > > | < | < < | < < < | | > | < | < < | | 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 |
class Vector
{
public:
Vector() {}
template< typename T >
using container_type = vector< T >;
const auto& terms() const { return m_terms; }
auto& terms() { return m_terms; }
uint32_t complexity() const { return m_complexity; }
void setComplexity( uint32_t c ) { m_complexity = c; }
void reserve( size_t n )
{
m_terms.reserve( n );
}
template< typename T >
void append( T&& term )
{
if constexpr( !is_same_v< Repetition, remove_cvref_t< T > > )
{
m_complexity += GetComplexity( term ),
m_terms.emplace_back( forward< T >( term ) );
}
else
{
setRepetitionTerm( forward< T >( term ) );
}
}
template< typename... T >
static auto Make( T&&... terms )
{
auto v = make_shared< Vector >();
v->reserve( sizeof... ( T ) );
( v->append( terms ), ... );
return v;
}
template< typename... T >
static Vector MakeAppend( const Vector& vec, T&&... terms )
{
Vector v( vec );
v.reserve( v.m_terms.size() + sizeof... ( T ) );
( v.append( terms ), ... );
return v;
}
template< typename... T >
static Vector MakeConcat( const Vector& vec1, const Vector& vec2 )
{
Vector v( vec1 );
v.reserve( vec1.m_terms.size() + vec2.m_terms.size() );
for( auto&& t : vec2.terms() )
v.append( t );
return v;
}
friend ostream& ToString( ostream& out, const pvec& v );
Vector( container_type< Term >&& terms ) :
m_terms( move( terms ) )
{}
|
| ︙ | ︙ | |||
111 112 113 114 115 116 117 |
if( m_repetitionTerm )
co_yield *m_repetitionTerm;
}
template< typename F >
pvec transform( F&& func ) const
{
| | | < | < < | | > < | | > > < | | > > > | | | < | | 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 167 168 169 170 171 172 |
if( m_repetitionTerm )
co_yield *m_repetitionTerm;
}
template< typename F >
pvec transform( F&& func ) const
{
auto v = make_shared< Vector >();
v->reserve( m_terms.size() );
uint32_t c = 0;
for( auto&& x : m_terms )
{
auto newX = func( x );
if( !newX )
return nullptr;
v->append( move( *newX ) );
}
return v;
}
Vector take( size_t n ) const
{
Vector v;
v.reserve( max( n, m_terms.size() ) );
auto it = m_terms.begin();
while( n-- )
v.append( *it++ );
return v;
}
Vector drop( size_t n ) const
{
Vector v;
v.reserve( m_terms.size() > n ? m_terms.size() - n : 0 );
auto it = m_terms.begin();
advance( it, n );
while( it != m_terms.end() )
v.append( *it++ );
return v;
}
template< typename T >
void setRepetitionTerm( T&& term )
{
uint32_t c = complexity();
if( m_repetitionTerm )
|
| ︙ | ︙ | |||
176 177 178 179 180 181 182 |
const auto& operator[]( size_t i ) const
{
return m_terms[i];
}
private:
| < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
const auto& operator[]( size_t i ) const
{
return m_terms[i];
}
private:
container_type< Term > m_terms;
optional< Term > m_repetitionTerm;
uint32_t m_complexity = 1;
};
template< typename T >
static inline auto&& SetComplexity( T&& term, uint32_t complexity )
|
| ︙ | ︙ |
Changes to bs/sema/domain.cpp.
| ︙ | ︙ | |||
15 16 17 18 19 20 21 |
Term InjectDomainIntoIdentity( const Term& identity, const Term& domain )
{
const auto& vec = get< pvec >( identity );
assert( vec );
assert( !vec->empty() );
| | > | | 15 16 17 18 19 20 21 22 23 24 25 26 |
Term InjectDomainIntoIdentity( const Term& identity, const Term& domain )
{
const auto& vec = get< pvec >( identity );
assert( vec );
assert( !vec->empty() );
auto newVec = make_shared< Vector >( *vec );
newVec->terms().front() = domain;
return newVec;
}
}
|
Changes to bs/sema/postprocess.cpp.
| ︙ | ︙ | |||
44 45 46 47 48 49 50 |
if( !val )
return nullopt;
return ( *optPP->second )( *val, uc );
}
const auto& vec = *get< pvec >( src );
| | > > | | | | 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
if( !val )
return nullopt;
return ( *optPP->second )( *val, uc );
}
const auto& vec = *get< pvec >( src );
auto outputTerms = make_shared< Vector >();
outputTerms->reserve( vec.terms().size() );
for( auto&& t : vec.terms() )
{
auto newT = Postprocess( t, uc );
if( !newT )
return nullopt;
outputTerms->append( move( *newT ) );
}
return outputTerms;
}
}
|
Changes to bs/sema/substitute.cpp.
| ︙ | ︙ | |||
20 21 22 23 24 25 26 |
if( !optVal )
return MkHole( "_"_sid );
return Substitute( *optVal, context );
}
const auto& vec = *get< pvec >( src );
| | > > | | | | 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
if( !optVal )
return MkHole( "_"_sid );
return Substitute( *optVal, context );
}
const auto& vec = *get< pvec >( src );
auto outputTerms = make_shared< Vector >();
outputTerms->reserve( vec.terms().size() );
for( auto&& t : vec.terms() )
outputTerms->append( Substitute( t, context ) );
return outputTerms;
}
Term SubstituteNamed( const Term& src, const UnificationContext& context )
{
if( !holds_alternative< pvec >( src ) )
return src;
|
| ︙ | ︙ | |||
61 62 63 64 65 66 67 |
return MkHole( "_"_sid );
return SubstituteNamed( *optVal, context );
}
}
const auto& vec = *get< pvec >( src );
| | > > | | | | 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
return MkHole( "_"_sid );
return SubstituteNamed( *optVal, context );
}
}
const auto& vec = *get< pvec >( src );
auto outputTerms = make_shared< Vector >();
outputTerms->reserve( vec.terms().size() );
for( auto&& t : vec.terms() )
outputTerms->append( SubstituteNamed( t, context ) );
return outputTerms;
}
}
|