1 //////////////////////////////////////////////////////////////////////////////
3 // This file implements datatype instantiations
5 //////////////////////////////////////////////////////////////////////////////
13 //////////////////////////////////////////////////////////////////////////////
15 // Method to instantiate a list of datatypes
17 //////////////////////////////////////////////////////////////////////////////
18 void DatatypeCompiler::instantiate_datatypes(Bool external, Tys tys)
21 { for_each (Ty, ty, tys) instantiate_datatypes(true,ty); }
22 { for_each (Ty, ty, tys) instantiate_datatypes(external,ty); }
25 //////////////////////////////////////////////////////////////////////////////
27 // Method to instantiate one datatype
29 //////////////////////////////////////////////////////////////////////////////
30 void DatatypeCompiler::instantiate_datatypes(Bool e, Ty ty)
31 { match (deref_all(ty))
32 { my_ty as DATATYPEty({ id, hierarchy, polyty ... },arg_types):
34 { error ("%Ldefinition of datatype %T is unknown\n", ty);
36 { if (! is_ground(my_ty))
37 error("%Lmissing parameters in instantiation of datatype %T\n",
39 hierarchy->generate_datatype_instantiations(*this,arg_types,e);
42 | ty: { error ("%Ldefinition of %T is not found\n", ty); }
46 //////////////////////////////////////////////////////////////////////////////
48 // Method to instantiate a datatype
50 //////////////////////////////////////////////////////////////////////////////
51 void DatatypeHierarchy::generate_datatype_instantiations
52 (CodeGen& C, Tys argument_types, Bool interface)
54 if (is_view()) return;
57 Id msg = interface ? "Interface specification" : "Instantiation";
60 "%^// %s of datatype %s%P"
64 msg, datatype_name, argument_types, line, file
66 DefKind k = interface ? EXTERNAL_DEFINITION : EXTERNAL_INSTANTIATION;
68 gen_class_instantiation(C, argument_types, k);
69 gen_class_implementation(C, argument_types, k);
70 for (int i = 0; i < number_of_subclasses; i++)
71 subclasses[i]->gen_class_implementation(C, argument_types, k);
75 //////////////////////////////////////////////////////////////////////////////
77 // Method to generate the implementation of a class
79 //////////////////////////////////////////////////////////////////////////////
80 void DatatypeClass::gen_class_implementation(CodeGen& C, Tys tys, DefKind k)
82 ///////////////////////////////////////////////////////////////////////////
84 // Compute the new argument types of the constructors
86 ///////////////////////////////////////////////////////////////////////////
88 { ONEcons { ty, cons_ty ... }:
90 { case EXTERNAL_INSTANTIATION:
91 case EXTERNAL_DEFINITION:
92 cons_arg_ty = apply_ty(cons_ty,tys); break;
94 cons_arg_ty = ty; break;
98 { cons_arg_ty = NOty; }
100 ///////////////////////////////////////////////////////////////////////////
102 // Generate the constructors, destructors, and datatype constructor
103 // function if inline methods are not used.
105 ///////////////////////////////////////////////////////////////////////////
106 if (! root->inline_methods)
108 // Generate the constructor of the class
109 if (cons != NOcons && (k != EXTERNAL_DEFINITION || tys != #[]))
110 gen_class_constructor(C, tys, k);
112 // Generate the destructor of the class
113 if ((k != EXTERNAL_DEFINITION || tys != #[]) &&
114 ((root->qualifiers & QUALvirtualdestr) ||
115 (qualifiers & QUALvirtualdestr) ||
117 gen_class_destructor(C, tys, k);
119 if (cons != NOcons && (k != EXTERNAL_DEFINITION || tys != #[]))
120 generate_datatype_constructor(C, tys, k);
123 ///////////////////////////////////////////////////////////////////////////
125 // Generate the implementation methods for various features
127 ///////////////////////////////////////////////////////////////////////////
128 if (root->qualifiers & QUALpersistent && k == EXTERNAL_INSTANTIATION)
129 generate_persistence_implementation(C,tys,k);
130 //if (root->qualifiers & QUALvariable && k == EXTERNAL_INSTANTIATION)
131 // generate_logic_implementation(C,tys,k);
132 if (root->qualifiers & QUALcollectable && k == EXTERNAL_INSTANTIATION)
133 generate_gc_implementation(C,tys,k);
134 if (root->qualifiers & QUALrelation && k == EXTERNAL_INSTANTIATION)
135 generate_inference_implementation(C,tys,k);
136 if (root->qualifiers & QUALprintable)
137 generate_print_implementation(C,tys,k);
140 void DatatypeHierarchy::gen_class_implementation(CodeGen& C, Tys tys,DefKind k)
142 DatatypeClass::gen_class_implementation(C,tys,k);
145 //////////////////////////////////////////////////////////////////////////////
147 // Method to generate template instantiation of a class
149 //////////////////////////////////////////////////////////////////////////////
150 void DatatypeClass::gen_class_instantiation(CodeGen& C, Tys tys, DefKind k)
152 C.pr("%^template class %S%P;", class_name, tys);
154 if (cons != NOcons && root->inline_methods)
155 { // Constructor function
156 C.pr("%^template %S%P * %S%b;",
157 root->class_name, tys,
158 constructor_name, cons_arg_ty, cons->name, TYsimpleformal);
161 { C.pr("%^template %S%P * %S%b;",
162 root->class_name, tys, constructor_name,
163 component_ty(cons_arg_ty,1), cons->name, TYsimpleformal);
169 //////////////////////////////////////////////////////////////////////////////
171 // Method to generate template instantiation of a class hierarchy
173 //////////////////////////////////////////////////////////////////////////////
174 void DatatypeHierarchy::gen_class_instantiation(CodeGen& C, Tys tys, DefKind k)
175 { if (tys != #[] && k == EXTERNAL_INSTANTIATION && arg_constructors > 0)
176 { C.pr("%n#ifdef PROP_EXPLICIT_TEMPLATE_INSTANTIATION");
177 DatatypeClass::gen_class_instantiation(C,tys,k);
178 for (int i = 0; i < number_of_subclasses; i++)
179 { subclasses[i]->gen_class_instantiation(C,tys,k);
181 gen_untagging_function_instantiation(C,tys,k);
182 gen_downcasting_function_instantiation(C,tys,k);
183 C.pr("%n#endif /* PROP_EXPLICIT_TEMPLATE_INSTANTIATION */");
187 //////////////////////////////////////////////////////////////////////////////
189 // Method to generate template instantiation of untagging functions.
191 //////////////////////////////////////////////////////////////////////////////
192 void DatatypeHierarchy::gen_untagging_function_instantiation
193 (CodeGen& C, Tys tys, DefKind k)
195 C.pr("%^template int boxed(const %S%P *);"
196 "%^template int untag(const %S%P *);",
197 class_name, tys, class_name, tys);
198 if (optimizations & OPTtaggedpointer)
199 { C.pr("%^template int untagp(const %S%P *);"
200 "%^template %S%P * derefp(const %S%P *);",
201 class_name, tys, class_name, tys, class_name, tys);
205 //////////////////////////////////////////////////////////////////////////////
207 // Method to generate template instantiation of downcasting functions.
209 //////////////////////////////////////////////////////////////////////////////
210 void DatatypeHierarchy::gen_downcasting_function_instantiation
211 (CodeGen& C, Tys tys, DefKind k)
213 if (options.inline_casts == false || parameters != #[])
214 { for (int i = 0; i < number_of_subclasses; i++)
215 { DatatypeClass * D = subclasses[i];
216 C.pr("%^template %S%P * _%S(const %S%P *);",
217 root->class_name, tys, D->constructor_name, class_name, tys);