//
// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
//
// The contents of this file are covered under the licence agreement in the
// file "LICENCE" distributed with Cforall.
//
// exception.h -- Builtins for exception handling.
//
// Author           : Andrew Beach
// Created On       : Mon Jun 26 15:11:00 2017
// Last Modified By : Peter A. Buhr
// Last Modified On : Thu Feb 22 18:11:15 2018
// Update Count     : 8
//

#pragma once


#ifdef __cforall
extern "C" {
#endif

struct __cfaabi_ehm__base_exception_t;
typedef struct __cfaabi_ehm__base_exception_t exception_t;
struct __cfaabi_ehm__base_exception_t_vtable {
	const struct __cfaabi_ehm__base_exception_t_vtable * parent;
	size_t size;
	void (*copy)(struct __cfaabi_ehm__base_exception_t *this,
	             struct __cfaabi_ehm__base_exception_t * other);
	void (*free)(struct __cfaabi_ehm__base_exception_t *this);
	const char * (*msg)(struct __cfaabi_ehm__base_exception_t *this);
};
struct __cfaabi_ehm__base_exception_t {
	struct __cfaabi_ehm__base_exception_t_vtable const * virtual_table;
};
extern struct __cfaabi_ehm__base_exception_t_vtable
	___cfaabi_ehm__base_exception_t_vtable_instance;


// Used in throw statement translation.
void __cfaabi_ehm__throw_terminate(exception_t * except) __attribute__((noreturn));
void __cfaabi_ehm__rethrow_terminate() __attribute__((noreturn));
void __cfaabi_ehm__throw_resume(exception_t * except);

// Function catches termination exceptions.
void __cfaabi_ehm__try_terminate(
    void (*try_block)(),
    void (*catch_block)(int index, exception_t * except),
    int (*match_block)(exception_t * except));

// Clean-up the exception in catch blocks.
void __cfaabi_ehm__cleanup_terminate(void * except);

// Data structure creates a list of resume handlers.
struct __cfaabi_ehm__try_resume_node {
    struct __cfaabi_ehm__try_resume_node * next;
    _Bool (*handler)(exception_t * except);
};

// These act as constructor and destructor for the resume node.
void __cfaabi_ehm__try_resume_setup(
    struct __cfaabi_ehm__try_resume_node * node,
    _Bool (*handler)(exception_t * except));
void __cfaabi_ehm__try_resume_cleanup(
    struct __cfaabi_ehm__try_resume_node * node);

// Check for a standard way to call fake deconstructors.
struct __cfaabi_ehm__cleanup_hook {};

#ifdef __cforall
}
#endif
