//
// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
//
// The contents of this file are covered under the licence agreement in the
// file "LICENCE" distributed with Cforall.
//
// CastCost.cc --
//
// Author           : Richard C. Bilson
// Created On       : Sun May 17 06:57:43 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Tue Feb  2 15:34:36 2016
// Update Count     : 7
//

#include <cassert>                       // for assert

#include "ConversionCost.h"              // for ConversionCost
#include "Cost.h"                        // for Cost, Cost::infinity
#include "ResolvExpr/TypeEnvironment.h"  // for TypeEnvironment, EqvClass
#include "SymTab/Indexer.h"              // for Indexer
#include "SynTree/Declaration.h"         // for TypeDecl, NamedTypeDecl
#include "SynTree/Type.h"                // for PointerType, Type, TypeInstType
#include "typeops.h"                     // for typesCompatibleIgnoreQualifiers


namespace ResolvExpr {
	class CastCost : public ConversionCost {
	  public:
		CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );

		virtual void visit( BasicType *basicType );
		virtual void visit( PointerType *pointerType );
	};

	Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
		if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
			EqvClass eqvClass;
			NamedTypeDecl *namedType;
			if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
				if ( eqvClass.type ) {
					return castCost( src, eqvClass.type, indexer, env );
				} else {
					return Cost::infinity;
				}
			} else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
				TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
				// all typedefs should be gone by this point
				assert( type );
				if ( type->get_base() ) {
					return castCost( src, type->get_base(), indexer, env ) + Cost::safe;
				} // if
			} // if
		} // if
		if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
			return Cost::zero;
		} else if ( dynamic_cast< VoidType* >( dest ) ) {
			return Cost::safe;
		} else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
			return convertToReferenceCost( src, refType, indexer, env );
		} else {
			CastCost converter( dest, indexer, env );
			src->accept( converter );
			if ( converter.get_cost() == Cost::infinity ) {
				return Cost::infinity;
			} else {
				// xxx - why are we adding cost 0 here?
				return converter.get_cost() + Cost::zero;
			} // if
		} // if
	}

	CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
		: ConversionCost( dest, indexer, env ) {
	}

	void CastCost::visit( BasicType *basicType ) {
		PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
		if ( destAsPointer && basicType->isInteger() ) {
			// necessary for, e.g. unsigned long => void*
			cost = Cost::unsafe;
		} else {
			cost = conversionCost( basicType, dest, indexer, env );
		} // if
	}

	void CastCost::visit( PointerType *pointerType ) {
		if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
			if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
				cost = Cost::safe;
			} else {
				TypeEnvironment newEnv( env );
				newEnv.add( pointerType->get_forall() );
				newEnv.add( pointerType->get_base()->get_forall() );
				int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
				if ( castResult > 0 ) {
					cost = Cost::safe;
				} else if ( castResult < 0 ) {
					cost = Cost::infinity;
				} // if
			} // if
		} else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
			if ( destAsBasic->isInteger() ) {
				// necessary for, e.g. void* => unsigned long
				cost = Cost::unsafe;
			} // if
		}
	}
} // namespace ResolvExpr

// Local Variables: //
// tab-width: 4 //
// mode: c++ //
// compile-command: "make install" //
// End: //
