//
// 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.
//
// PtrsAssignable.cc --
//
// Author           : Richard C. Bilson
// Created On       : Sun May 17 11:44:11 2015
// Last Modified By : Peter A. Buhr
// Last Modified On : Wed Mar  2 17:36:05 2016
// Update Count     : 8
//

#include "Common/PassVisitor.h"
#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
#include "SynTree/Type.h"                // for TypeInstType, Type, BasicType
#include "SynTree/Visitor.h"             // for Visitor


namespace ResolvExpr {
	struct PtrsAssignable : public WithShortCircuiting {
		PtrsAssignable( Type *dest, const TypeEnvironment &env );

		int get_result() const { return result; }

		void previsit( Type * ) { visit_children = false; }

		void postvisit( VoidType * voidType );
		void postvisit( BasicType * basicType );
		void postvisit( PointerType * pointerType );
		void postvisit( ArrayType * arrayType );
		void postvisit( FunctionType * functionType );
		void postvisit( StructInstType * inst );
		void postvisit( UnionInstType * inst );
		void postvisit( EnumInstType * inst );
		void postvisit( TraitInstType * inst );
		void postvisit( TypeInstType * inst );
		void postvisit( TupleType * tupleType );
		void postvisit( VarArgsType * varArgsType );
		void postvisit( ZeroType * zeroType );
		void postvisit( OneType * oneType );
	  private:
		Type *dest;
		int result;
		const TypeEnvironment &env;
	};

	int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) {
		// std::cerr << "assignable: " << src << " | " << dest << std::endl;
		if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
			if ( const EqvClass *eqvClass = env.lookup( destAsTypeInst->get_name() ) ) {
				return ptrsAssignable( src, eqvClass->type, env );
			} // if
		} // if
		if ( dynamic_cast< VoidType* >( dest ) ) {
			// void * = T * for any T is unsafe
			// xxx - this should be safe, but that currently breaks the build
			return -1;
		} else {
			PassVisitor<PtrsAssignable> ptrs( dest, env );
			src->accept( ptrs );
			return ptrs.pass.get_result();
		} // if
	}

	PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {}

	void PtrsAssignable::postvisit( VoidType * ) {
		// T * = void * is disallowed - this is a change from C, where any
		// void * can be assigned or passed to a non-void pointer without a cast.
	}

	void PtrsAssignable::postvisit( __attribute__((unused)) BasicType *basicType ) {}
	void PtrsAssignable::postvisit( __attribute__((unused)) PointerType *pointerType ) {}
	void PtrsAssignable::postvisit( __attribute__((unused)) ArrayType *arrayType ) {}
	void PtrsAssignable::postvisit( __attribute__((unused)) FunctionType *functionType ) {}

	void PtrsAssignable::postvisit(  __attribute__((unused)) StructInstType *inst ) {}
	void PtrsAssignable::postvisit(  __attribute__((unused)) UnionInstType *inst ) {}

	void PtrsAssignable::postvisit( EnumInstType * ) {
		if ( dynamic_cast< BasicType* >( dest ) ) {
			// int * = E *, etc. is safe. This isn't technically correct, as each
			// enum has one basic type that it is compatible with, an that type can
			// differ from enum to enum. Without replicating GCC's internal logic,
			// there is no way to know which type this particular enum is compatible
			// with, so punt on this for now.
			result = 1;
		}
	}

	void PtrsAssignable::postvisit(  __attribute__((unused)) TraitInstType *inst ) {}
	void PtrsAssignable::postvisit( TypeInstType *inst ) {
		if ( const EqvClass *eqvClass = env.lookup( inst->get_name() ) ) {
			if ( eqvClass->type ) {
				// T * = S * for any S depends on the type bound to T
				result = ptrsAssignable( eqvClass->type, dest, env );
			}
		} // if
	}

	void PtrsAssignable::postvisit(  __attribute__((unused)) TupleType *tupleType ) {}
	void PtrsAssignable::postvisit(  __attribute__((unused)) VarArgsType *varArgsType ) {}
	void PtrsAssignable::postvisit(  __attribute__((unused)) ZeroType *zeroType ) {}
	void PtrsAssignable::postvisit(  __attribute__((unused)) OneType *oneType ) {}

} // namespace ResolvExpr

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