Skip to content

Commit

Permalink
Type Inference Pass
Browse files Browse the repository at this point in the history
* cont'd by setting and calculating the correct 'libcasm_ir::Type' from
  'Ast::BasicType' and 'Ast::FixedSizeType'
  - related to sealangdotorg/sea#20
  - RESTRICTION: only a trivial 'static' Ast::ValueAtom is allowed for now
  - WIP: ranged integer is not implemented yet!
  • Loading branch information
ppaulweber committed Apr 5, 2017
1 parent 4334b20 commit 2ec2aae
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
67 changes: 52 additions & 15 deletions src/analyze/TypeInferencePass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "TypeInferencePass.h"

#include "../Logger.h"
#include "../Namespace.h"
#include "../analyze/SymbolResolverPass.h"
#include "../ast/RecursiveVisitor.h"

#include "../casm-ir/src/Builtin.h"
Expand All @@ -47,7 +47,7 @@ static libpass::PassRegistration< TypeInferencePass > PASS(
class TypeCheckVisitor final : public RecursiveVisitor
{
public:
TypeCheckVisitor( Logger& log );
TypeCheckVisitor( Logger& log, Namespace& symboltable );

void visit( BasicType& node ) override;
void visit( ComposedType& node ) override;
Expand All @@ -58,6 +58,7 @@ class TypeCheckVisitor final : public RecursiveVisitor
private:
Logger& m_log;
u64 m_err;
Namespace& m_symboltable;
};

static const std::unordered_map< std::string, libcasm_ir::Type::Ptr > basicTypes
Expand All @@ -68,13 +69,12 @@ static const std::unordered_map< std::string, libcasm_ir::Type::Ptr > basicTypes
{ "Bit", libstdhl::get< libcasm_ir::BitType >( 1 ) },
{ "String", libstdhl::get< libcasm_ir::StringType >() },
{ "Floating", libstdhl::get< libcasm_ir::FloatingType >() },
// enumeration
// agent
};

TypeCheckVisitor::TypeCheckVisitor( Logger& log )
TypeCheckVisitor::TypeCheckVisitor( Logger& log, Namespace& symboltable )
: m_log( log )
, m_err( 0 )
, m_symboltable( symboltable )
{
}

Expand All @@ -91,16 +91,44 @@ void TypeCheckVisitor::visit( BasicType& node )
}
else if( name.compare( "Agent" ) == 0 )
{
// TODO: handle the agent case
m_log.debug( "TODO: FIXME: handle 'Agent' case!" ); // TODO: PPA:
}
else
{
// m_err++; // TODO: enable this line!
m_log.error( { node.sourceLocation() },
"unknown type '" + name + "' found" );
try
{
auto symbol = m_symboltable.find( node );

assert( symbol.targetType()
== CallExpression::TargetType::ENUMERATION );

auto& definition = static_cast< EnumerationDefinition& >(
symbol.definition() );

if( not definition.type() )
{
m_log.debug( "enum IR type not set yet" );

auto kind
= libstdhl::make< libcasm_ir::Enumeration >( name );
for( auto e : *definition.enumerators() )
{
kind->add( e->identifier() );
}

// TODO: it could still be a enumeration type etc., check this in
// the symbol table!
auto type
= libstdhl::make< libcasm_ir::EnumerationType >( kind );
definition.setType( type );
}

node.setType( definition.type() );
}
catch( const std::domain_error& e )
{
// m_err++; // TODO: enable this line!
m_log.error( { node.sourceLocation() },
"unknown type '" + name + "' found" );
}
}
}

Expand All @@ -118,14 +146,22 @@ void TypeCheckVisitor::visit( FixedSizedType& node )
if( not node.type() )
{
const auto& name = node.name()->identifier();
auto const& expr = *node.size();
const auto& expr = *node.size();

if( name.compare( "Bit" ) == 0 )
{
if( expr.id() == Node::ID::VALUE_ATOM and expr.type()->isInteger() )
{
// TODO: handle bit vector case, check if integer > 0 and set
// the IR::Bit(n) type
const auto& atom = static_cast< const ValueAtom& >( expr );

const auto value
= std::static_pointer_cast< libcasm_ir::IntegerConstant >(
atom.value() );

auto type
= libstdhl::get< libcasm_ir::BitType >( value->value() );

node.setType( type );
}
else
{
Expand Down Expand Up @@ -225,8 +261,9 @@ u1 TypeInferencePass::run( libpass::PassResult& pr )

const auto data = pr.result< SymbolResolverPass >();
const auto specification = data->specification();
const auto symboltable = data->symboltable();

TypeCheckVisitor typChkVisitor( log );
TypeCheckVisitor typChkVisitor( log, *symboltable );
specification->accept( typChkVisitor );

const auto typChkErr = typChkVisitor.errors();
Expand Down
7 changes: 2 additions & 5 deletions src/analyze/TypeInferencePass.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@
#ifndef _LIB_CASMFE_TYPE_INFERENCE_PASS_H_
#define _LIB_CASMFE_TYPE_INFERENCE_PASS_H_

#include "../analyze/SymbolResolverPass.h"

#include "../ast/RecursiveVisitor.h"
#include "../ast/Specification.h"
#include "../transform/SourceToAstPass.h"

namespace libcasm_fe
{
Expand All @@ -45,7 +42,7 @@ namespace libcasm_fe

bool run( libpass::PassResult& pr ) override;

using Data = SymbolResolverPass::Data;
using Data = SourceToAstPass::Data;
};
}

Expand Down

0 comments on commit 2ec2aae

Please sign in to comment.