Home | History | Annotate | Line # | Download | only in interface
      1  1.1  mrg #ifndef ISL_INTERFACE_TEMPLATE_CPP_H
      2  1.1  mrg #define ISL_INTERFACE_TEMPLATE_CPP_H
      3  1.1  mrg 
      4  1.1  mrg #include <initializer_list>
      5  1.1  mrg #include <iostream>
      6  1.1  mrg #include <map>
      7  1.1  mrg #include <memory>
      8  1.1  mrg #include <set>
      9  1.1  mrg #include <string>
     10  1.1  mrg #include <unordered_map>
     11  1.1  mrg 
     12  1.1  mrg #include "cpp.h"
     13  1.1  mrg 
     14  1.1  mrg struct Fixed;
     15  1.1  mrg 
     16  1.1  mrg struct TupleKind;
     17  1.1  mrg 
     18  1.1  mrg /* A shared pointer to a TupleKind.
     19  1.1  mrg  */
     20  1.1  mrg struct TupleKindPtr : public std::shared_ptr<const TupleKind> {
     21  1.1  mrg   using Base = std::shared_ptr<const TupleKind>;
     22  1.1  mrg   TupleKindPtr() = default;
     23  1.1  mrg   TupleKindPtr(Fixed);
     24  1.1  mrg   TupleKindPtr(Base base) : Base(base) {}
     25  1.1  mrg   TupleKindPtr(const std::string &name);
     26  1.1  mrg   TupleKindPtr(const TupleKindPtr &left, const TupleKindPtr &right);
     27  1.1  mrg };
     28  1.1  mrg 
     29  1.1  mrg /* A substitution mapping leaf tuple kind names to tuple kinds.
     30  1.1  mrg  */
     31  1.1  mrg using Substitution = std::unordered_map<std::string, TupleKindPtr>;
     32  1.1  mrg 
     33  1.1  mrg /* A representation of a (possibly improper) tuple kind.
     34  1.1  mrg  * That is, this also includes tuple kinds for types
     35  1.1  mrg  * that do not have any tuples.
     36  1.1  mrg  *
     37  1.1  mrg  * The kind could be a name (the base case) or
     38  1.1  mrg  * a (currently) unnamed nested pair of tuple kinds.
     39  1.1  mrg  */
     40  1.1  mrg struct TupleKind {
     41  1.1  mrg 	TupleKind(const std::string &name) : name(name) {}
     42  1.1  mrg 
     43  1.1  mrg 	virtual std::string to_string() const;
     44  1.1  mrg 	virtual std::vector<std::string> params() const;
     45  1.1  mrg 	virtual TupleKindPtr apply(const Substitution &subs,
     46  1.1  mrg 		const TupleKindPtr &self) const;
     47  1.1  mrg 	virtual TupleKindPtr left() const;
     48  1.1  mrg 	virtual TupleKindPtr right() const;
     49  1.1  mrg 
     50  1.1  mrg 	const std::string name;
     51  1.1  mrg };
     52  1.1  mrg 
     53  1.1  mrg /* A sequence of tuple kinds, representing a kind of objects.
     54  1.1  mrg  */
     55  1.1  mrg struct Kind : public std::vector<TupleKindPtr> {
     56  1.1  mrg 	Kind() {}
     57  1.1  mrg 	Kind(std::initializer_list<TupleKindPtr> list) : vector(list) {}
     58  1.1  mrg 
     59  1.1  mrg 	bool is_anon() const;
     60  1.1  mrg 	bool is_set() const;
     61  1.1  mrg 	bool is_anon_set() const;
     62  1.1  mrg 	std::vector<std::string> params() const;
     63  1.1  mrg 	Kind apply(const Substitution &subs) const;
     64  1.1  mrg };
     65  1.1  mrg 
     66  1.1  mrg /* A representation of a template class.
     67  1.1  mrg  *
     68  1.1  mrg  * "class_name" is the name of the template class.
     69  1.1  mrg  * "super_name" is the (fully qualified) name of the corresponding
     70  1.1  mrg  * plain C++ interface class, from which this template class derives.
     71  1.1  mrg  * "clazz" describes the plain class.
     72  1.1  mrg  *
     73  1.1  mrg  * "class_tuples" contains the specializations.
     74  1.1  mrg  * It is initialized with a predefined set of specializations,
     75  1.1  mrg  * but may be extended during the generations of the specializations.
     76  1.1  mrg  */
     77  1.1  mrg struct template_class {
     78  1.1  mrg 	const std::string class_name;
     79  1.1  mrg 	const std::string super_name;
     80  1.1  mrg 	const isl_class &clazz;
     81  1.1  mrg 
     82  1.1  mrg 	std::vector<Kind> class_tuples;
     83  1.1  mrg 
     84  1.1  mrg 	bool is_anon() const;
     85  1.1  mrg 	bool is_anon_set() const;
     86  1.1  mrg 	void add_specialization(const Kind &kind);
     87  1.1  mrg };
     88  1.1  mrg 
     89  1.1  mrg /* A generator for templated C++ bindings.
     90  1.1  mrg  *
     91  1.1  mrg  * "template_classes" contains all generated template classes,
     92  1.1  mrg  * keyed on their names.
     93  1.1  mrg  */
     94  1.1  mrg class template_cpp_generator : public cpp_generator {
     95  1.1  mrg 	struct class_printer;
     96  1.1  mrg 	struct method_decl_printer;
     97  1.1  mrg 	struct method_impl_printer;
     98  1.1  mrg 	struct class_decl_printer;
     99  1.1  mrg 	struct class_impl_printer;
    100  1.1  mrg 
    101  1.1  mrg 	void add_template_class(const isl_class &clazz, const std::string &name,
    102  1.1  mrg 		const std::vector<Kind> &class_tuples);
    103  1.1  mrg public:
    104  1.1  mrg 	template_cpp_generator(clang::SourceManager &SM,
    105  1.1  mrg 		std::set<clang::RecordDecl *> &exported_types,
    106  1.1  mrg 		std::set<clang::FunctionDecl *> exported_functions,
    107  1.1  mrg 		std::set<clang::FunctionDecl *> functions);
    108  1.1  mrg 
    109  1.1  mrg 	virtual void generate() override;
    110  1.1  mrg 	void foreach_template_class(
    111  1.1  mrg 		const std::function<void(const template_class &)> &fn) const;
    112  1.1  mrg 	void print_forward_declarations(std::ostream &os);
    113  1.1  mrg 	void print_friends(std::ostream &os);
    114  1.1  mrg 
    115  1.1  mrg 	std::map<std::string, template_class> template_classes;
    116  1.1  mrg };
    117  1.1  mrg 
    118  1.1  mrg #endif
    119