1 //////////////////////////////////////////////////////////////////////////////
3 // The following classes are used to generate C++ class definitions
4 // for algebraic datatypes.
6 //////////////////////////////////////////////////////////////////////////////
7 #ifndef datatype_class_definition
8 #define datatype_class_definition
14 //////////////////////////////////////////////////////////////////////////////
16 // Forward type definitions.
18 //////////////////////////////////////////////////////////////////////////////
20 class DatatypeHierarchy;
22 //////////////////////////////////////////////////////////////////////////////
24 // This class represents one subclass in an algebraic datatype class
27 //////////////////////////////////////////////////////////////////////////////
28 class DatatypeClass : public ClassDefinition
30 DatatypeClass(const DatatypeClass&);
31 void operator = (const DatatypeClass&);
34 Id constructor_name; // name of constructor
35 Id is_const; // a const type?
36 Cons cons; // the constructor descriptor
37 DatatypeHierarchy * root; // root of the hierarchy
38 Bool generating_list_special_forms;
39 Ty cons_arg_ty; // current constructor argument type
40 Bool is_list; // is it a list
41 Bool is_array; // is it an array
44 ///////////////////////////////////////////////////////////////////////////
46 // Constructors and destructors
48 ///////////////////////////////////////////////////////////////////////////
49 DatatypeClass(CLASS_TYPE,
50 Id, Id, TyVars, Inherits, TyQual, Decls, Cons,
52 virtual ~DatatypeClass();
54 void instantiate_datatype(CodeGen&, TyVars);
55 void generate_forward_declarations(CodeGen&);
56 void generate_datatype_constructor(CodeGen&,
57 Tys, DefKind = INLINE_IMPLEMENTATION);
59 virtual void visualize(PropVisualizer&);
63 friend class DatatypeHierarchy;
65 // Methods that adhere to the ClassDefinition protocol
66 virtual void gen_class_destructor_body(CodeGen&, Tys, DefKind);
68 virtual void gen_class_predefinition(CodeGen&);
69 virtual void gen_class_interface(CodeGen&);
70 virtual void gen_class_postdefinition(CodeGen&);
71 virtual void gen_class_implementation(CodeGen&, Tys, DefKind);
73 virtual void gen_class_instantiation(CodeGen&, Tys, DefKind);
75 // Methods for generating the functionality for various features
76 void generate_persistence_interface(CodeGen&);
77 void generate_logic_interface(CodeGen&);
78 void generate_gc_interface(CodeGen&);
79 void generate_inference_interface(CodeGen&);
80 virtual void generate_print_interface(CodeGen&);
82 void generate_persistence_implementation(CodeGen&, Tys, DefKind);
83 void generate_logic_implementation(CodeGen&, Tys, DefKind);
84 void generate_gc_implementation(CodeGen&, Tys, DefKind);
85 void generate_inference_implementation(CodeGen&, Tys, DefKind);
86 virtual void generate_print_implementation(CodeGen&, Tys, DefKind);
90 // Methods for generating class constructor code.
91 virtual void gen_class_constructor(CodeGen&, Tys, DefKind);
92 virtual void gen_class_constructor_parameters(CodeGen&, Tys, DefKind);
93 virtual void gen_class_constructor_initializers(CodeGen&, Tys, DefKind);
94 virtual void gen_class_constructor_body(CodeGen&, Tys, DefKind);
95 void gen_constructor_initializers(CodeGen&, Tys, DefKind, Ty, Id, Id);
96 void gen_array_initializer(CodeGen&, Tys, DefKind, Id, Ty, Id);
99 // Methods for generating garbage collection code
101 void gen_super_class_tracing_methods(CodeGen&, Tys, DefKind);
102 void gen_field_tracing_methods(CodeGen&, Exp, Ty, Tys, DefKind, Bool=false);
105 // Methods for generating persistence I/O code
107 void gen_super_class_persist_IO(CodeGen&, Tys, DefKind, Id);
108 void gen_field_persist_IO(CodeGen&, Exp, Ty, Tys, DefKind, Id, Bool=false);
111 //////////////////////////////////////////////////////////////////////////////
113 // This class is responsible for generating a class hierarchy for a
114 // algebraic datatype.
116 //////////////////////////////////////////////////////////////////////////////
117 class DatatypeHierarchy : public DatatypeClass
119 DatatypeHierarchy(const DatatypeHierarchy&);
120 void operator = (const DatatypeHierarchy&);
123 friend class DatatypeClass;
124 const Id datatype_name; // name of datatype
125 const TermDefs term_defs; // datatype terms
126 DatatypeClass ** subclasses; // subclasses of the hierarchy
127 int number_of_subclasses;
128 int arity; // total number of constructors
129 int unit_constructors; // number of unit constructors
130 int arg_constructors; // number of argument constructors
131 Cons * constructor_terms; // constructor descriptors
132 TyOpt optimizations; // optimizations?
133 Bool inline_methods; // should be inline the methods?
134 Bool has_variant_tag; // do we have a variant tag?
135 Bool use_gc_base; // do we need to trace base class?
136 Bool use_persist_base; // do we need to print base class?
137 Ty datatype_ty; // type of the datatype
142 DatatypeHierarchy(Id,TyVars,Inherits,TyQual,TermDefs, Decls);
143 virtual ~DatatypeHierarchy();
145 void generate_forward_declarations(CodeGen&);
146 void generate_datatype_definitions(CodeGen&);
147 void generate_datatype_instantiations(CodeGen&, Tys, Bool);
149 virtual void gen_class_instantiation(CodeGen&, Tys, DefKind);
151 virtual void visualize(PropVisualizer&);
154 // Methods to generate code
155 void generate_forward_class_declarations(CodeGen&);
156 void generate_forward_constructor_declarations(CodeGen&);
157 void generate_unit_constructors(CodeGen&);
158 void generate_constructor_tags(CodeGen&, Id, Id, int, Cons []);
159 void generate_define_tags(CodeGen&, int, Cons []);
160 void generate_untagging_member_functions(CodeGen&);
161 void generate_untagging_functions(CodeGen&);
162 void generate_downcasting_functions(CodeGen&);
163 void generate_datatype_constructors(CodeGen&, Tys, DefKind);
165 // Methods to instantiate templates
166 void gen_untagging_function_instantiation(CodeGen&, Tys, DefKind);
167 void gen_downcasting_function_instantiation(CodeGen&, Tys, DefKind);
169 // Methods that adhere to the ClassDefinition protocol
170 virtual void gen_class_interface(CodeGen&);
171 virtual void gen_class_implementation(CodeGen&, Tys, DefKind);
172 virtual void gen_class_postdefinition(CodeGen&);
175 // Methods to constructor the class hierarchy
176 void build_class_hierarchy();
177 DatatypeClass * build_one_subclass(Cons);
178 void build_inheritance_list();
180 // Methods for generating pretty printers
181 virtual void generate_print_interface(CodeGen&);
182 virtual void generate_print_implementation(CodeGen&, Tys, DefKind);
183 void generate_printer(CodeGen&, Tys, DefKind, Ty, Cons);
184 void generate_default_printer(CodeGen&, Tys, DefKind, Ty, Cons, Ty);
185 void generate_formatted_printer(CodeGen&, Tys, DefKind, Ty, Cons, Ty,
187 void gen_print_field(CodeGen&, Exp, Ty);