Skip to content

Commit

Permalink
Type Inference
Browse files Browse the repository at this point in the history
* fixed logger integration in the type inference pass, due to an API change
  in the libstdhl::Logger implementation
  - see: sealangdotorg/libuse@84fe1df
  - related to ref sealangdotorg/sea#20
  • Loading branch information
ppaulweber committed May 7, 2017
1 parent 9e90dea commit 98fa690
Showing 1 changed file with 5 additions and 52 deletions.
57 changes: 5 additions & 52 deletions src/analyze/TypeInferencePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,11 @@ class TypeCheckVisitor final : public RecursiveVisitor
void visit( ExpressionCase& node ) override;
void visit( DefaultCase& node ) override;

u64 errors( void ) const;

private:
void push( VariableDefinition& identifier );
void pop( VariableDefinition& identifier );

Logger& m_log;
u64 m_err;
Namespace& m_symboltable;

std::unordered_map< std::string, VariableDefinition* > m_id2var;
Expand Down Expand Up @@ -112,7 +109,6 @@ static const std::unordered_map< std::string, libcasm_ir::Type::Ptr > basicTypes

TypeCheckVisitor::TypeCheckVisitor( Logger& log, Namespace& symboltable )
: m_log( log )
, m_err( 0 )
, m_symboltable( symboltable )
{
}
Expand Down Expand Up @@ -190,7 +186,6 @@ void TypeCheckVisitor::visit( DirectCallExpression& node )
}
catch( const std::domain_error& e )
{
// m_err++; // TODO: PPA: enable this
m_log.error( { node.sourceLocation() },
"unable to find symbol '"
+ identifierSearchPath.path()
Expand All @@ -199,7 +194,6 @@ void TypeCheckVisitor::visit( DirectCallExpression& node )
}
else
{
// m_err++; // TODO: PPA: enable this
m_log.error( { node.sourceLocation() },
"unable to resolve symbol '" + path.path() + "'" );
}
Expand Down Expand Up @@ -229,14 +223,12 @@ void TypeCheckVisitor::visit( DirectCallExpression& node )
}
catch( const std::domain_error& e )
{
// m_err++; // TODO: PPA: enable this
m_log.error( { node.sourceLocation() },
"unknown symbol '" + path.path() + "' found" );
}
}
else
{
// m_err++; // TODO: PPA: enable this
m_log.error( { node.sourceLocation() },
"unknown symbol '" + path.path() + "' found" );
}
Expand Down Expand Up @@ -324,7 +316,6 @@ void TypeCheckVisitor::visit( BasicType& node )
else if( name.compare( "RuleRef" ) == 0
or name.compare( "FuncRef" ) == 0 )
{
m_err++;
m_log.error( { node.sourceLocation() },
"reference type '" + name + "' defined without relation" );
}
Expand All @@ -346,7 +337,6 @@ void TypeCheckVisitor::visit( BasicType& node )
}
catch( const std::domain_error& e )
{
m_err++;
m_log.error( { node.sourceLocation() },
"unknown type '" + name + "' found" );
}
Expand Down Expand Up @@ -407,7 +397,6 @@ void TypeCheckVisitor::visit( RelationType& node )
}
else
{
m_err++;
m_log.error( { node.sourceLocation() },
"unknown relation type '" + name + "' found" );
}
Expand Down Expand Up @@ -437,13 +426,11 @@ void TypeCheckVisitor::visit( FixedSizedType& node )
}
catch( const std::domain_error& e )
{
m_err++;
m_log.error( { expr.sourceLocation() }, e.what() );
}
}
else
{
m_err++;
m_log.error( { expr.sourceLocation() },
"unsupported expr for 'Bit' type, constant Integer value "
"expected" );
Expand Down Expand Up @@ -495,22 +482,19 @@ void TypeCheckVisitor::visit( FixedSizedType& node )
}
catch( const std::domain_error& e )
{
m_err++;
m_log.error( { expr.sourceLocation() }, e.what() );
}
}
}
else
{
m_err++;
m_log.error( { expr.sourceLocation() },
"unsupported expr for 'Integer' type, only range "
"expressions are allowed, e.g. `Integer'[5..10]`" );
}
}
else
{
m_err++;
m_log.error( { node.sourceLocation() },
"unknown type '" + name + "' found" );
}
Expand All @@ -530,19 +514,13 @@ void TypeCheckVisitor::visit( DefaultCase& node )
// omit redundant traversal of rule(), see visit( CaseRule& )
}

u64 TypeCheckVisitor::errors( void ) const
{
return m_err;
}

void TypeCheckVisitor::push( VariableDefinition& node )
{
const auto& name = node.identifier()->name();

auto result = m_id2var.emplace( name, &node );
if( not result.second )
{
m_err++;
m_log.error( { node.sourceLocation() },
"symbol '" + name + "' already defined" );
}
Expand Down Expand Up @@ -609,11 +587,8 @@ class TypeInferenceVisitor final : public RecursiveVisitor
void pop( VariableDefinition& node );
VariableDefinition& find( const IdentifierPath& node );

u64 errors( void ) const;

private:
Logger& m_log;
u64 m_err;
Namespace& m_symboltable;
u1 m_functionInitially;

Expand All @@ -626,7 +601,6 @@ class TypeInferenceVisitor final : public RecursiveVisitor
TypeInferenceVisitor::TypeInferenceVisitor(
Logger& log, Namespace& symboltable )
: m_log( log )
, m_err( 0 )
, m_symboltable( symboltable )
, m_functionInitially( false )
{
Expand Down Expand Up @@ -767,7 +741,6 @@ void TypeInferenceVisitor::visit( ReferenceAtom& node )
}
default:
{
m_err++;
m_log.error( { node.sourceLocation() },
"cannot reference '" + CallExpression::targetTypeString(
symbol.targetType() )
Expand Down Expand Up @@ -805,7 +778,6 @@ void TypeInferenceVisitor::visit( DirectCallExpression& node )
}
catch( const std::domain_error& e )
{
m_err++;
m_log.error( { node.sourceLocation() },
"unable to resolve built-in symbol '"
+ path.path()
Expand Down Expand Up @@ -884,7 +856,6 @@ void TypeInferenceVisitor::visit( DirectCallExpression& node )
}
catch( const std::domain_error& e )
{
m_err++;
m_log.error( { node.sourceLocation() }, e.what() );
return;
}
Expand Down Expand Up @@ -958,7 +929,6 @@ void TypeInferenceVisitor::visit( DirectCallExpression& node )
{
if( not node.type() )
{
m_err++;
m_log.error( { node.sourceLocation() }, "TODO: ENUMERATION" );
}
break;
Expand All @@ -967,7 +937,6 @@ void TypeInferenceVisitor::visit( DirectCallExpression& node )
{
if( not node.type() )
{
m_err++;
m_log.error( { node.sourceLocation() }, "TODO: CONSTANT" );
}
break;
Expand All @@ -988,7 +957,6 @@ void TypeInferenceVisitor::visit( IndirectCallExpression& node )

if( not node.expression()->type() )
{
m_err++;
m_log.error( { node.sourceLocation() },
"unable to resolve type of indirect call expression" );
}
Expand Down Expand Up @@ -1031,7 +999,6 @@ void TypeInferenceVisitor::visit( RangeExpression& node )

if( *lhs.type() != *rhs.type() )
{
m_err++;
m_log.error(
{ node.sourceLocation() }, "types of range does not match" );
return;
Expand Down Expand Up @@ -1112,30 +1079,27 @@ void TypeInferenceVisitor::assignment( const Node& node, TypedNode& lhs,
rhs.setType( lhs.type()->ptr_result() );
}

const auto error_count = m_err;
const auto error_count = m_log.errors();

if( not lhs.type() )
{
m_err++;
m_log.error(
{ lhs.sourceLocation() }, "unable to infer type of " + dst );
}

if( not rhs.type() )
{
m_err++;
m_log.error(
{ rhs.sourceLocation() }, "unable to infer type of " + src );
}

if( error_count != m_err )
if( error_count != m_log.errors() )
{
return;
}

if( lhs.type()->result() != rhs.type()->result() )
{
m_err++;
m_log.error( { lhs.sourceLocation(), rhs.sourceLocation() },
"type of " + dst + " does not match type of " + src + ": '"
+ lhs.type()->description()
Expand Down Expand Up @@ -1206,7 +1170,6 @@ void TypeInferenceVisitor::inference( const std::string& description,
auto result = m_resultTypes.find( &node );
if( result == m_resultTypes.end() )
{
m_err++;
m_log.error( { node.sourceLocation() },
"unable to find type annotation for " + description );
return;
Expand Down Expand Up @@ -1266,7 +1229,6 @@ void TypeInferenceVisitor::inference( const std::string& description,
}
catch( const std::invalid_argument& e )
{
m_err++;
m_log.error( { node.sourceLocation() },
"unable to infer result type of " + description );
}
Expand All @@ -1276,7 +1238,6 @@ void TypeInferenceVisitor::inference( const std::string& description,

if( resTypes.size() < 1 )
{
m_err++;
m_log.error( { node.sourceLocation() },
"unable to infer result type of " + description );
return;
Expand All @@ -1292,7 +1253,6 @@ void TypeInferenceVisitor::inference( const std::string& description,
first = false;
}

m_err++;
m_log.error( { node.sourceLocation() },
"unable to infer result type of " + description
+ " from multiple possible types: "
Expand Down Expand Up @@ -1361,7 +1321,6 @@ void TypeInferenceVisitor::inference( const std::string& description,

if( not node.type() )
{
m_err++;
m_log.error(
{ node.sourceLocation() }, "unable to resolve type of expression" );
}
Expand Down Expand Up @@ -1459,7 +1418,6 @@ void TypeInferenceVisitor::push( VariableDefinition& node )
auto result = m_id2var.emplace( name, &node );
if( not result.second )
{
m_err++;
m_log.error( { node.sourceLocation() },
"symbol '" + name + "' already defined" );
}
Expand Down Expand Up @@ -1487,11 +1445,6 @@ VariableDefinition& TypeInferenceVisitor::find( const IdentifierPath& node )
return *result->second;
}

u64 TypeInferenceVisitor::errors( void ) const
{
return m_err;
}

//
// TypeInferencePass
//
Expand All @@ -1512,7 +1465,7 @@ u1 TypeInferencePass::run( libpass::PassResult& pr )
TypeCheckVisitor typeCheck( log, *symboltable );
specification->accept( typeCheck );

const auto typChkErr = typeCheck.errors();
const auto typChkErr = log.errors();
if( typChkErr )
{
log.debug( "found %lu error(s) during type checking", typChkErr );
Expand All @@ -1522,11 +1475,11 @@ u1 TypeInferencePass::run( libpass::PassResult& pr )
TypeInferenceVisitor typeInfer( log, *symboltable );
specification->accept( typeInfer );

const auto typInfErr = typeInfer.errors();
const auto typInfErr = log.errors();
if( typInfErr )
{
log.debug( "found %lu error(s) during type inference", typInfErr );
// return false;
return false;
}

pr.setResult< TypeInferencePass >(
Expand Down

0 comments on commit 98fa690

Please sign in to comment.