/* * This file is part of the Cforall project * * $Id: Resolver.cc,v 1.19 2005/08/29 20:14:16 rcbilson Exp $ * */ #include "Resolver.h" #include "AlternativeFinder.h" #include "Alternative.h" #include "RenameVars.h" #include "ResolveTypeof.h" #include "SynTree/Statement.h" #include "SynTree/Type.h" #include "SynTree/Expression.h" #include "SynTree/Initializer.h" #include "SymTab/Indexer.h" #include "utility.h" namespace ResolvExpr { class Resolver : public SymTab::Indexer { public: Resolver() : SymTab::Indexer( false ), switchType( 0 ) {} virtual void visit( FunctionDecl *functionDecl ); virtual void visit( ObjectDecl *functionDecl ); virtual void visit( TypeDecl *typeDecl ); virtual void visit( ExprStmt *exprStmt ); virtual void visit( IfStmt *ifStmt ); virtual void visit( WhileStmt *whileStmt ); virtual void visit( ForStmt *forStmt ); virtual void visit( SwitchStmt *switchStmt ); virtual void visit( ChooseStmt *switchStmt ); virtual void visit( CaseStmt *caseStmt ); virtual void visit( ReturnStmt *returnStmt ); virtual void visit( SingleInit *singleInit ); private: std::list< Type* > functionReturn; Type* initContext; Type *switchType; }; void resolve( std::list< Declaration* > translationUnit ) { Resolver resolver; acceptAll( translationUnit, resolver ); /// for( std::list< Declaration* >::iterator i = translationUnit.begin(); i != translationUnit.end(); ++i ) { /// (*i)->print( std::cerr ); /// (*i)->accept( resolver ); /// } } Expression * resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { TypeEnvironment env; return resolveInVoidContext( expr, indexer, env ); } namespace { void finishExpr( Expression *expr, const TypeEnvironment &env ) { expr->set_env( new TypeSubstitution ); env.makeSubstitution( *expr->get_env() ); } Expression* findVoidExpression( Expression *untyped, const SymTab::Indexer &indexer ) { global_renamer.reset(); TypeEnvironment env; Expression *newExpr = resolveInVoidContext( untyped, indexer, env ); finishExpr( newExpr, env ); return newExpr; } Expression* findSingleExpression( Expression *untyped, const SymTab::Indexer &indexer ) { TypeEnvironment env; AlternativeFinder finder( indexer, env ); finder.find( untyped ); /// if( finder.get_alternatives().size() != 1 ) { /// std::cout << "untyped expr is "; /// untyped->print( std::cout ); /// std::cout << std::endl << "alternatives are:"; /// for( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { /// i->print( std::cout ); /// } /// } assert( finder.get_alternatives().size() == 1 ); Alternative &choice = finder.get_alternatives().front(); Expression *newExpr = choice.expr->clone(); finishExpr( newExpr, choice.env ); return newExpr; } bool isIntegralType( Type *type ) { if( dynamic_cast< EnumInstType* >( type ) ) { return true; } else if( BasicType *bt = dynamic_cast< BasicType* >( type ) ) { return bt->isInteger(); } else { return true; } } Expression* findIntegralExpression( Expression *untyped, const SymTab::Indexer &indexer ) { TypeEnvironment env; AlternativeFinder finder( indexer, env ); finder.find( untyped ); /// if( finder.get_alternatives().size() != 1 ) { /// std::cout << "untyped expr is "; /// untyped->print( std::cout ); /// std::cout << std::endl << "alternatives are:"; /// for( std::list< Alternative >::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { /// i->print( std::cout ); /// } /// } Expression *newExpr = 0; const TypeEnvironment *newEnv = 0; for( AltList::const_iterator i = finder.get_alternatives().begin(); i != finder.get_alternatives().end(); ++i ) { if( i->expr->get_results().size() == 1 && isIntegralType( i->expr->get_results().front() ) ) { if( newExpr ) { throw SemanticError( "Too many interpretations for switch control expression", untyped ); } else { newExpr = i->expr->clone(); newEnv = &i->env; } } } if( !newExpr ) { throw SemanticError( "Too many interpretations for switch control expression", untyped ); } finishExpr( newExpr, *newEnv ); return newExpr; } } void Resolver::visit( ObjectDecl *objectDecl ) { Type *new_type = resolveTypeof( objectDecl->get_type(), *this ); objectDecl->set_type( new_type ); initContext = new_type; SymTab::Indexer::visit( objectDecl ); } void Resolver::visit( TypeDecl *typeDecl ) { if( typeDecl->get_base() ) { Type *new_type = resolveTypeof( typeDecl->get_base(), *this ); typeDecl->set_base( new_type ); } SymTab::Indexer::visit( typeDecl ); } void Resolver::visit( FunctionDecl *functionDecl ) { /// std::cout << "resolver visiting functiondecl "; /// functionDecl->print( std::cout ); /// std::cout << std::endl; Type *new_type = resolveTypeof( functionDecl->get_type(), *this ); functionDecl->set_type( new_type ); std::list< Type* > oldFunctionReturn = functionReturn; functionReturn.clear(); for( std::list< DeclarationWithType* >::const_iterator i = functionDecl->get_functionType()->get_returnVals().begin(); i != functionDecl->get_functionType()->get_returnVals().end(); ++i ) { functionReturn.push_back( (*i)->get_type() ); } SymTab::Indexer::visit( functionDecl ); functionReturn = oldFunctionReturn; } void Resolver::visit( ExprStmt *exprStmt ) { if( exprStmt->get_expr() ) { Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this ); delete exprStmt->get_expr(); exprStmt->set_expr( newExpr ); } } void Resolver::visit( IfStmt *ifStmt ) { Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this ); delete ifStmt->get_condition(); ifStmt->set_condition( newExpr ); Visitor::visit( ifStmt ); } void Resolver::visit( WhileStmt *whileStmt ) { Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this ); delete whileStmt->get_condition(); whileStmt->set_condition( newExpr ); Visitor::visit( whileStmt ); } void Resolver::visit( ForStmt *forStmt ) { Expression *newExpr; if( forStmt->get_condition() ) { newExpr = findSingleExpression( forStmt->get_condition(), *this ); delete forStmt->get_condition(); forStmt->set_condition( newExpr ); } if( forStmt->get_increment() ) { newExpr = findVoidExpression( forStmt->get_increment(), *this ); delete forStmt->get_increment(); forStmt->set_increment( newExpr ); } Visitor::visit( forStmt ); } template< typename SwitchClass > void handleSwitchStmt( SwitchClass *switchStmt, SymTab::Indexer &visitor ) { Expression *newExpr; newExpr = findIntegralExpression( switchStmt->get_condition(), visitor ); delete switchStmt->get_condition(); switchStmt->set_condition( newExpr ); visitor.Visitor::visit( switchStmt ); } void Resolver::visit( SwitchStmt *switchStmt ) { handleSwitchStmt( switchStmt, *this ); } void Resolver::visit( ChooseStmt *switchStmt ) { handleSwitchStmt( switchStmt, *this ); } void Resolver::visit( CaseStmt *caseStmt ) { Visitor::visit( caseStmt ); } void Resolver::visit( ReturnStmt *returnStmt ) { if( returnStmt->get_expr() ) { CastExpr *castExpr = new CastExpr( returnStmt->get_expr() ); cloneAll( functionReturn, castExpr->get_results() ); Expression *newExpr = findSingleExpression( castExpr, *this ); delete castExpr; returnStmt->set_expr( newExpr ); } } void Resolver::visit( SingleInit *singleInit ) { if( singleInit->get_value() ) { CastExpr *castExpr = new CastExpr( singleInit->get_value(), initContext->clone() ); Expression *newExpr = findSingleExpression( castExpr, *this ); delete castExpr; singleInit->set_value( newExpr ); } singleInit->get_value()->accept( *this ); } } // namespace ResolvExpr