You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

547 lines
19 KiB

25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
25 years ago
Remove exception hooks in favor of real C++ exceptions: * include/cln/exception.h: New file... * include/cln/cln.h: ...included here... * include/cln/rational.h: ...and here. * include/cln/abort.h: Removed. * include/cln/integer.h (ash_exception, exquo_exception): Add exception types. * include/cln/float.h (floating_point_exception, floating_point_nan_exception, floating_point_overflow_exception, floating_point_underflow_exception): Likewise. * include/cln/number_io.h (read_number_exception, read_number_bad_syntax_exception, read_number_junk_exception, read_number_eof_exception): Likewise. * include/cln/object.h (CL_DEFINE_CONVERTER): Replace dynamic assertion with a compile-time assertion. * include/cln/GV.h: Replace nonreturning functions with exceptions. * include/cln/modinteger.h: Likewise. * include/cln/SV.h: Likewise. * include/cln/ring.h: Likewise. * include/cln/string.h: Likewise. * include/cln/univpoly.h: Likewise. * src/base/cl_abort.cc: Removed. * src/base/cl_N_err_d0.cc: Removed. * src/base/cl_d0_exception.cc: New file. * src/base/cl_as_err.cc: Removed. * src/base/cl_as_exception.cc: New file. * src/base/cl_notreached.cc: Removed. * src/base/cl_notreached_exception.cc: New file. * src/base/input/cl_read_err_bad.cc: Removed. * src/base/input/cl_read_bad_syntax_exception.cc: New file. * src/base/input/cl_read_err_junk.cc: Removed. * src/base/input/cl_read_junk_exception.cc: New file. * src/base/input/cl_read_err_eof.cc: Removed. * src/base/input/cl_read_eof_exception.cc: New file. * src/base/cl_N.h (cl_as_error): Removed (see cln/exception.h). * src/base/macros.h (NOTREACHED): Throw. * src/base/cl_malloc.cc (xmalloc): Throw. * src/base/digitseq/cl_2DS_div.cc: Throw. * src/base/digitseq/cl_DS_div.cc: Throw. * src/base/digitseq/cl_DS_mul.cc: Throw. * src/base/digitseq/cl_DS_mul_fftc.h: Throw. * src/base/digitseq/cl_DS_mul_fftcs.h: Throw. * src/base/digitseq/cl_DS_mul_fftm.h: Throw. * src/base/digitseq/cl_DS_mul_fftp.h: Throw. * src/base/digitseq/cl_DS_mul_fftp3.h: Throw. * src/base/digitseq/cl_DS_mul_fftp3m.h: Throw. * src/base/digitseq/cl_DS_mul_fftr.h: Throw. * src/base/digitseq/cl_DS_mul_nuss.h: Throw. * src/base/digitseq/cl_DS_recipsqrt.cc: Throw. * src/base/digitseq/cl_DS_sqrt.cc: Throw. * src/base/hash/cl_hash.h: Throw. * src/base/hash/cl_hash1.h: Throw. * src/base/hash/cl_hash1weak.h: Throw. * src/base/hash/cl_hash2.h: Throw. * src/base/hash/cl_hash2weak.h: Throw. * src/base/hash/cl_hashset.h: Throw. * src/base/hash/cl_hashuniq.h: Throw. * src/base/hash/cl_hashuniqweak.h: Throw. * src/base/proplist/cl_pl_add.cc: Throw. * src/base/ring/cl_no_ring.cc: Throw. * src/base/string/cl_spushstring.h: Throw. * src/base/symbol/cl_symbol.cc: Throw. * src/integer/bitwise/cl_I_ash.h: Removed (see cln/integer.h.) * src/integer/bitwise/cl_I_asherr.cc: Removed. * src/integer/bitwise/cl_I_ash_exception.cc: New file. * src/integer/division/cl_I_exquoerr.cc: Removed. * src/integer/division/cl_I_exquo_exception.cc: New file. * src/integer/cl_I.h: Throw. * src/integer/division/cl_I_exquopos.cc: Throw. * src/integer/bitwise/cl_I_logbitp_I.cc: Throw. * src/integer/bitwise/cl_I_ash.cc: Throw. * src/integer/bitwise/cl_I_ash_I.cc: Throw. * src/integer/division/cl_I_exquo.cc: Throw. * src/integer/gcd/cl_I_gcd_aux2.cc: Throw. * src/integer/conv/cl_I_to_L.cc: Throw. * src/integer/conv/cl_I_to_Q.cc: Throw. * src/integer/conv/cl_I_to_UL.cc: Throw. * src/integer/conv/cl_I_to_UQ.cc: Throw. * src/integer/conv/cl_I_to_digits.cc: Throw. * src/integer/elem/cl_I_div.cc: Throw. * src/integer/algebraic/cl_I_sqrt.cc: Throw. * src/integer/input/cl_I_read.cc: Throw. * src/integer/input/cl_I_read_stream.cc: Throw. * src/integer/misc/cl_I_as.cc: Throw. * src/rational/elem/cl_RA_from_I_I_div.cc: Throw. * src/rational/elem/cl_RA_recip.cc: Throw. * src/rational/input/cl_RA_read.cc: Throw. * src/rational/input/cl_RA_read_stream.cc: Throw. * src/rational/input/cl_RA_readparsed.cc: Throw. * src/rational/misc/cl_RA_as.cc: Throw. * src/float/base/cl_F_err_nan.cc: Removed. * src/float/base/cl_F_nan_exception.cc: New file. * src/float/base/cl_F_err_un.cc: Removed. * src/float/base/cl_F_underflow_exception.cc: New file. * src/float/base/cl_F_err_ov.cc: Removed. * src/float/base/cl_F_overflow_exception.cc: New file. * src/float/cl_F.h (cl_error_floating_point_nan, cl_error_floating_point_overflow, cl_error_floating_point_underflow): Removed (see cln/float.h.) * src/float/sfloat/cl_SF.h: Throw. * src/float/sfloat/elem/cl_SF_div.cc: Throw. * src/float/sfloat/elem/cl_SF_from_RA.cc: Throw. * src/float/sfloat/elem/cl_SF_scale.cc: Throw. * src/float/sfloat/elem/cl_SF_scale_I.cc: Throw. * src/float/sfloat/misc/cl_SF_as.cc: Throw. * src/float/ffloat/cl_FF.h: Throw. * src/float/ffloat/conv/cl_FF_from_float.cc: Throw. * src/float/ffloat/elem/cl_FF_div.cc: Throw. * src/float/ffloat/elem/cl_FF_from_RA.cc: Throw. * src/float/ffloat/elem/cl_FF_scale.cc: Throw. * src/float/ffloat/elem/cl_FF_scale_I.cc: Throw. * src/float/ffloat/misc/cl_FF_as.cc: Throw. * src/float/dfloat/cl_DF.h: Throw. * src/float/dfloat/conv/cl_DF_from_double.cc: Throw. * src/float/dfloat/elem/cl_DF_div.cc: Throw. * src/float/dfloat/elem/cl_DF_from_RA.cc: Throw. * src/float/dfloat/elem/cl_DF_scale.cc: Throw. * src/float/dfloat/elem/cl_DF_scale_I.cc: Throw. * src/float/dfloat/misc/cl_DF_as.cc: Throw. * src/float/lfloat/algebraic/cl_LF_sqrt.cc: Throw. * src/float/lfloat/elem/cl_LF_1plus.cc: Throw. * src/float/lfloat/elem/cl_LF_I_div.cc: Throw. * src/float/lfloat/elem/cl_LF_I_mul.cc: Throw. * src/float/lfloat/elem/cl_LF_div.cc: Throw. * src/float/lfloat/elem/cl_LF_from_I.cc: Throw. * src/float/lfloat/elem/cl_LF_mul.cc: Throw. * src/float/lfloat/elem/cl_LF_scale.cc: Throw. * src/float/lfloat/elem/cl_LF_scale_I.cc: Throw. * src/float/lfloat/elem/cl_LF_square.cc: Throw. * src/float/lfloat/misc/cl_LF_as.cc: Throw. * src/float/lfloat/misc/cl_LF_shorten.cc: Throw. * src/float/lfloat/misc/cl_LF_shortenrel.cc: Throw. * src/float/lfloat/misc/cl_LF_shortenwith.cc: Throw. * src/float/input/cl_F_read.cc: Throw. * src/float/input/cl_F_read_stream.cc: Throw. * src/float/misc/cl_F_as.cc: Throw. * src/float/misc/cl_F_shortenrel.cc: Throw. * src/float/transcendental/cl_LF_coshsinh_aux.cc: Throw. * src/float/transcendental/cl_LF_cossin_aux.cc: Throw. * src/float/transcendental/cl_LF_exp_aux.cc: Throw. * src/float/transcendental/cl_LF_ratseries_a.cc: Throw. * src/float/transcendental/cl_LF_ratseries_ab.cc: Throw. * src/float/transcendental/cl_LF_ratseries_b.cc: Throw. * src/float/transcendental/cl_LF_ratseries_p.cc: Throw. * src/float/transcendental/cl_LF_ratseries_pa.cc: Throw. * src/float/transcendental/cl_LF_ratseries_pab.cc: Throw. * src/float/transcendental/cl_LF_ratseries_pb.cc: Throw. * src/float/transcendental/cl_LF_ratseries_pq.cc: Throw. * src/float/transcendental/cl_LF_ratseries_pqa.cc: Throw. * src/float/transcendental/cl_LF_ratseries_pqab.cc: Throw. * src/float/transcendental/cl_LF_ratseries_pqb.cc: Throw. * src/float/transcendental/cl_LF_ratseries_q.cc: Throw. * src/float/transcendental/cl_LF_ratseries_qa.cc: Throw. * src/float/transcendental/cl_LF_ratseries_qab.cc: Throw. * src/float/transcendental/cl_LF_ratseries_qb.cc: Throw. * src/float/transcendental/cl_LF_ratseries_stream_pq.cc: Throw. * src/float/transcendental/cl_LF_ratseries_stream_pqa.cc: Throw. * src/float/transcendental/cl_LF_ratseries_stream_pqab.cc: Throw. * src/float/transcendental/cl_LF_ratseries_stream_pqb.cc: Throw. * src/float/transcendental/cl_LF_ratsumseries_pqcd_aux.cc: Throw. * src/float/transcendental/cl_LF_ratsumseries_pqd.cc: Throw. * src/float/transcendental/cl_LF_ratsumseries_pqd_aux.cc: Throw. * src/float/transcendental/cl_LF_zeta_int.cc: Throw. * src/real/elem/cl_R_div.cc: Throw. * src/real/format-output/cl_fmt_cardinal.cc: Throw. * src/real/format-output/cl_fmt_newroman.cc: Throw. * src/real/format-output/cl_fmt_oldroman.cc: Throw. * src/real/input/cl_R_read.cc: Throw. * src/real/input/cl_R_read_stream.cc: Throw. * src/real/misc/cl_R_as.cc: Throw. * src/real/random/cl_R_random.cc: Throw. * src/real/transcendental/cl_R_atan2.cc: Throw. * src/real/transcendental/cl_R_log.cc: Throw. * src/complex/input/cl_N_read.cc: Throw. * src/complex/input/cl_N_read_stream.cc: Throw. * src/complex/misc/cl_N_as.cc: Throw. * src/complex/transcendental/cl_C_atanh_aux.cc: Throw. * src/complex/transcendental/cl_C_expt_C.cc: Throw. * src/complex/transcendental/cl_C_log.cc: Throw. * src/complex/transcendental/cl_C_log2.cc: Throw. * src/numtheory/cl_nt_cornacchia1.cc: Throw. * src/numtheory/cl_nt_cornacchia4.cc: Throw. * src/numtheory/cl_nt_isprobprime.cc: Throw. * src/numtheory/cl_nt_jacobi.cc: Throw. * src/numtheory/cl_nt_jacobi_low.cc: Throw. * src/numtheory/cl_nt_sqrtmodp.cc: Throw. * src/modinteger/cl_MI.cc: Throw. * src/modinteger/cl_MI_int.h: Throw. * src/modinteger/cl_MI_montgom.h: Throw. * src/modinteger/cl_MI_pow2.h: Throw. * src/modinteger/cl_MI_rshift.cc: Throw. * src/modinteger/cl_MI_std.h: Throw. * src/polynomial/elem/cl_UP_GF2.h: Throw. * src/polynomial/elem/cl_UP_MI.h: Throw. * src/polynomial/elem/cl_UP_gen.h: Throw. * src/polynomial/elem/cl_UP_named.cc: Throw. * src/polynomial/elem/cl_UP_no_ring.cc (uninitialized_error, uninitialized_ring): Removed (see cln/ring.h.) * src/polynomial/elem/cl_UP_number.h: Throw. * src/polynomial/elem/cl_UP_unnamed.cc: Throw. * src/vector/cl_GV_I.cc: Throw. * src/vector/cl_GV_number.cc: Throw. * tests/timediv2adic-compare.cc: Use default abort(), not cl_abort(). * tests/timeprint-compare.cc: Likewise. * tests/timerecip2adic-compare.cc: Likewise. * doc/cln.tex: Document the exception classes. * examples/contfrac.cc: Use try/catch instead of setjmp/longjmp. * INSTALL: undocument -fno-exceptions. * README: Add exceptions to list of used C++ features.
18 years ago
25 years ago
25 years ago
  1. // General object definitions: pointers, reference counting, garbage collection.
  2. #ifndef _CL_OBJECT_H
  3. #define _CL_OBJECT_H
  4. #include "cln/types.h"
  5. #include "cln/modules.h"
  6. #include <cstdlib>
  7. namespace cln {
  8. // We don't have to deal with circular structures, so normal reference counting
  9. // is sufficient. Is also has the advantage of being mostly non-interrupting.
  10. // An object is either a pointer to heap allocated data
  11. // or immediate data.
  12. // It is possible to distinguish these because pointers are aligned.
  13. // cl_word_alignment is the guaranteed alignment of a `void*' or `long'
  14. // in memory. Must be > 1.
  15. #if defined(__m68k__)
  16. #define cl_word_alignment 2
  17. #endif
  18. #if defined(__i386__) || (defined(__mips__) && !defined(__LP64__)) || (defined(__sparc__) && !defined(__arch64__)) || defined(__hppa__) || defined(__arm__) || defined(__rs6000__) || defined(__m88k__) || defined(__convex__) || (defined(__s390__) && !defined(__s390x__)) || defined(__sh__) || (defined(__x86_64__) && defined(__ILP32__))
  19. #define cl_word_alignment 4
  20. #endif
  21. #if defined(__alpha__) || defined(__ia64__) || defined(__mips64__) || defined(__powerpc64__) || (defined(__sparc__) && defined(__arch64__)) || (defined(__x86_64__) && !defined(__ILP32__)) || defined(__s390x__) || defined(__aarch64__)
  22. #define cl_word_alignment 8
  23. #endif
  24. #if !defined(cl_word_alignment)
  25. #error "Define cl_word_alignment for your CPU!"
  26. #endif
  27. // Four basic classes are introduced:
  28. //
  29. // gcobject rcobject
  30. //
  31. // gcpointer rcpointer
  32. //
  33. // `gcobject' = garbage collectible object (pointer or immediate),
  34. // `gcpointer' = garbage collectible pointer,
  35. // `rcobject' = reference counted object (pointer or immediate),
  36. // `rcpointer' = reference counted pointer.
  37. //
  38. // "garbage collectible" means that a reference count is maintained, and
  39. // when the reference count drops to 0, the object is freed. This is useful
  40. // for all kind of short- or long-lived objects.
  41. // "reference counted" means that a reference count is maintained, which
  42. // cannot drop to 0. This is useful for objects which are registered in a
  43. // global cache table, in order to know which objects can be thrown away
  44. // when the cache is cleaned. (If the cache were never cleaned, its objects
  45. // would never be freed, and we could get away with normal C pointers.)
  46. //
  47. // It is permissible to treat a `rcobject' as a `gcobject', and a `rcpointer'
  48. // as a `gcpointer', but this just increases the destructor and copy-constructor
  49. // overhead.
  50. // It is also permissible to treat a `gcpointer' as a `gcobject', and a
  51. // `rcpointer' as a `rcobject', but this just increases the destructor and
  52. // copy-constructor overhead.
  53. // Immediate data is a word, as wide as a pointer.
  54. typedef sintP cl_sint;
  55. typedef uintP cl_uint; // This ought to be called `cl_word'.
  56. #define cl_pointer_size intPsize
  57. // NB: (cl_pointer_size==64) implies defined(HAVE_FAST_LONGLONG)
  58. #if (cl_pointer_size==64)
  59. #define CL_WIDE_POINTERS
  60. #endif
  61. // Distinguish immediate data from pointers.
  62. inline bool cl_pointer_p (cl_uint word)
  63. {
  64. return (word & (cl_word_alignment-1)) == 0;
  65. }
  66. inline bool cl_immediate_p (cl_uint word)
  67. {
  68. return (word & (cl_word_alignment-1)) != 0;
  69. }
  70. // Immediate data: Fixnum, Short Float, maybe Single Float.
  71. // They have type tags.
  72. // |...............................|......|
  73. // cl_value cl_tag
  74. // Number of bits reserved for tagging information:
  75. #if (cl_word_alignment <= 4)
  76. #define cl_tag_len 2
  77. #else
  78. #define cl_tag_len 3
  79. #endif
  80. #define cl_tag_shift 0
  81. #define cl_value_shift (cl_tag_len+cl_tag_shift)
  82. #define cl_value_len (cl_pointer_size - cl_value_shift)
  83. #define cl_tag_mask (((1UL << cl_tag_len) - 1) << cl_tag_shift)
  84. #define cl_value_mask (((1UL << cl_value_len) - 1) << cl_value_shift)
  85. // Return the tag of a word.
  86. inline cl_uint cl_tag (cl_uint word)
  87. {
  88. return (word & cl_tag_mask) >> cl_tag_shift;
  89. }
  90. // Return the value (unsigned) of a word.
  91. inline cl_uint cl_value (cl_uint word)
  92. {
  93. // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
  94. return word >> cl_value_shift;
  95. }
  96. // Return a word, combining a value and a tag.
  97. inline cl_uint cl_combine (cl_uint tag, cl_uint value)
  98. {
  99. return (value << cl_value_shift) + (tag << cl_tag_shift);
  100. }
  101. inline cl_uint cl_combine (cl_uint tag, cl_sint value)
  102. {
  103. // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
  104. return (value << cl_value_shift) + (tag << cl_tag_shift);
  105. }
  106. // Keep the compiler happy.
  107. inline cl_uint cl_combine (cl_uint tag, unsigned int value)
  108. { return cl_combine(tag, (cl_uint)value); }
  109. inline cl_uint cl_combine (cl_uint tag, int value)
  110. { return cl_combine(tag, (cl_sint)value); }
  111. #ifdef HAVE_LONGLONG
  112. inline cl_uint cl_combine (cl_uint tag, unsigned long long value)
  113. { return cl_combine(tag, (cl_uint)value); }
  114. inline cl_uint cl_combine (cl_uint tag, long long value)
  115. { return cl_combine(tag, (cl_uint)value); }
  116. #endif
  117. // Definition of the tags.
  118. #if !defined(CL_WIDE_POINTERS)
  119. #if (cl_word_alignment == 2)
  120. #define cl_FN_tag 1
  121. #define cl_SF_tag 3 // must satisfy the cl_immediate_p predicate!
  122. #endif
  123. #if (cl_word_alignment == 4)
  124. #define cl_FN_tag 1
  125. #define cl_SF_tag 2
  126. #endif
  127. #else // CL_WIDE_POINTERS
  128. // Single Floats are immediate as well.
  129. #define cl_FN_tag 1
  130. #define cl_SF_tag 2
  131. #define cl_FF_tag 3
  132. #endif
  133. // Corresponding classes.
  134. extern const struct cl_class * cl_immediate_classes [1<<cl_tag_len];
  135. // Heap allocated data contains a header, for two purposes:
  136. // - dynamic typing,
  137. // - reference count (a portable alternative to garbage collection,
  138. // or the basis for a portable and interoperable garbage collection).
  139. struct cl_heap {
  140. int refcount; // reference count
  141. const struct cl_class * type; // type tag
  142. };
  143. // Function to destroy the contents of a heap object.
  144. typedef void (*cl_heap_destructor_function) (cl_heap* pointer);
  145. // Flags, may be ORed together.
  146. #define cl_class_flags_subclass_complex 1 // all instances belong to cl_N
  147. #define cl_class_flags_subclass_real 2 // all instances belong to cl_R
  148. #define cl_class_flags_subclass_float 4 // all instances belong to cl_F
  149. #define cl_class_flags_subclass_rational 8 // all instances belong to cl_RA
  150. #define cl_class_flags_number_ring 16 // all instances are rings whose
  151. // elements belong to cl_number
  152. #define cl_class_flags_modint_ring 32 // all instances are rings whose
  153. // elements belong to cl_MI
  154. #define cl_class_flags_univpoly_ring 64 // all instances are rings whose
  155. // elements belong to cl_UP
  156. // Function to print an object for debugging, to cerr.
  157. typedef void (*cl_heap_dprint_function) (cl_heap* pointer);
  158. struct cl_class {
  159. cl_heap_destructor_function destruct;
  160. int flags;
  161. cl_heap_dprint_function dprint;
  162. };
  163. // Free an object on heap.
  164. extern void cl_free_heap_object (cl_heap* pointer);
  165. // Debugging support for dynamic typing: Register a debugging print function.
  166. #define cl_register_type_printer(type,printer) \
  167. { extern cl_class type; type.dprint = (printer); }
  168. // cl_private_thing: An immediate value or a pointer into the heap.
  169. // This must be as wide as a `cl_uint'.
  170. // (Actually, this ought to be a union { void*; cl_uint; }, but using
  171. // a pointer type generates better code.)
  172. // Never throw away a cl_private_thing, or reference counts will be wrong!
  173. typedef struct cl_anything * cl_private_thing;
  174. // Increment the reference count.
  175. inline void cl_inc_pointer_refcount (cl_heap* pointer)
  176. {
  177. pointer->refcount++;
  178. }
  179. // Decrement the reference count of a garbage collected pointer.
  180. inline void cl_gc_dec_pointer_refcount (cl_heap* pointer)
  181. {
  182. if (--pointer->refcount == 0)
  183. cl_free_heap_object(pointer);
  184. }
  185. // Decrement the reference count of a reference counted pointer.
  186. inline void cl_rc_dec_pointer_refcount (cl_heap* pointer)
  187. {
  188. --pointer->refcount;
  189. }
  190. // Increment the reference count.
  191. // This must be a macro, not an inline function, because pointer_p() and
  192. // inc_pointer_refcount() are non-virtual member functions, so that the
  193. // compiler can optimize it.
  194. #define cl_inc_refcount(x) \
  195. if ((x).pointer_p()) \
  196. (x).inc_pointer_refcount(); \
  197. // Decrement the reference count.
  198. // This must be a macro, not an inline function, because pointer_p() and
  199. // dec_pointer_refcount() are non-virtual member functions, so that the
  200. // compiler can optimize it.
  201. #define cl_dec_refcount(x) \
  202. if ((x).pointer_p()) \
  203. (x).dec_pointer_refcount(); \
  204. // The declaration of a copy constructor.
  205. // Restriction: The base class's default constructor must do nothing or
  206. // initialize `pointer' to a constant expression.
  207. #define CL_DEFINE_COPY_CONSTRUCTOR1(_class_) \
  208. _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_class_)
  209. #define _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_classname_) \
  210. inline _class_::_classname_ (const _class_& x) \
  211. { \
  212. cl_uint x_word = x.word; \
  213. cl_inc_refcount(x); \
  214. this->word = x_word; \
  215. }
  216. // The declaration of a copy constructor.
  217. // Restriction: The base class must have the usual `cl_private_thing'
  218. // constructor. Drawback: The base class must be known here.
  219. #define CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_baseclass_) \
  220. _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_class_,_baseclass_)
  221. #define _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_classname_,_baseclass_) \
  222. inline _class_::_classname_ (const _class_& x) \
  223. : _baseclass_ (as_cl_private_thing(x)) {}
  224. // The declaration of an assignment operator.
  225. #define CL_DEFINE_ASSIGNMENT_OPERATOR(dest_class,src_class) \
  226. inline dest_class& dest_class::operator= (const src_class& x) \
  227. { \
  228. /* Be careful, we might be assigning x to itself. */ \
  229. cl_uint x_word = x.word; \
  230. cl_inc_refcount(x); \
  231. cl_dec_refcount(*this); \
  232. this->word = x_word; \
  233. return *this; \
  234. }
  235. // We have a small problem with destructors: The specialized destructor
  236. // of a leaf class such as `cl_SF' should be more efficient than the
  237. // general destructor for `cl_N'. Since (by C++ specs) destructing a cl_SF
  238. // would run the destructors for cl_SF, cl_F, cl_R, cl_N (in that order),
  239. // and in the last step the compiler does not know any more that the object
  240. // actually is a cl_SF, there is no way to optimize the destructor!
  241. // ("progn-reversed" method combination is evil.)
  242. // And if we define "mirror"/"shadow" classes with no destructors (such
  243. // that `cl_F' inherits from `cl_F_no_destructor' buts adds a destructor)
  244. // then we need to add explicit conversion operators cl_SF -> cl_F -> cl_R ...,
  245. // with the effect that calling an overloaded function like `as_cl_F'
  246. // (which has two signatures `as_cl_F(cl_number)' and `as_cl_F(cl_F)')
  247. // with a cl_SF argument gives an "call of overloaded function is ambiguous"
  248. // error.
  249. // There is no help: If we want overloaded functions to be callable in a way
  250. // that makes sense, `cl_SF' has to be a subclass of `cl_F', and then the
  251. // destructor of `cl_SF' will do at least as much computation as the `cl_F'
  252. // destructor. Praise C++ ! :-((
  253. // (Even making `pointer_p()' a virtual function would not help.)
  254. // This is obnoxious.
  255. template <class key1_type, class value_type> struct cl_htentry1;
  256. // The four concrete classes of all objects.
  257. class cl_gcobject {
  258. public: /* ugh */
  259. union {
  260. void* pointer;
  261. cl_heap* heappointer;
  262. cl_uint word;
  263. };
  264. public:
  265. // Default constructor. (Used for objects with no initializer.)
  266. cl_gcobject ();
  267. // Destructor. (Used when a variable goes out of scope.)
  268. ~cl_gcobject ();
  269. // Copy constructor.
  270. cl_gcobject (const cl_gcobject&);
  271. // Assignment operator.
  272. cl_gcobject& operator= (const cl_gcobject&);
  273. // Distinguish immediate data from pointer.
  274. bool pointer_p() const
  275. { return cl_pointer_p(word); }
  276. // Reference counting.
  277. void inc_pointer_refcount () const
  278. { cl_inc_pointer_refcount(heappointer); }
  279. void dec_pointer_refcount () const
  280. { cl_gc_dec_pointer_refcount(heappointer); }
  281. // Return the type tag of an immediate number.
  282. cl_uint nonpointer_tag () const
  283. { return cl_tag(word); }
  284. // Return the type tag of a heap-allocated number.
  285. const cl_class * pointer_type () const
  286. { return heappointer->type; }
  287. // Private pointer manipulations.
  288. cl_private_thing _as_cl_private_thing () const;
  289. // Private constructor.
  290. cl_gcobject (cl_private_thing p)
  291. : pointer (p) {}
  292. // Debugging output.
  293. void debug_print () const;
  294. // Ability to place an object at a given address.
  295. void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
  296. void* operator new (size_t size) { return ::operator new (size); }
  297. };
  298. inline cl_gcobject::cl_gcobject () {}
  299. inline cl_gcobject::~cl_gcobject () { cl_dec_refcount(*this); }
  300. CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcobject)
  301. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcobject,cl_gcobject)
  302. class cl_gcpointer {
  303. public: /* ugh */
  304. union {
  305. void* pointer;
  306. cl_heap* heappointer;
  307. cl_uint word;
  308. };
  309. public:
  310. // Default constructor. (Used for objects with no initializer.)
  311. cl_gcpointer ();
  312. // Destructor. (Used when a variable goes out of scope.)
  313. ~cl_gcpointer ();
  314. // Copy constructor.
  315. cl_gcpointer (const cl_gcpointer&);
  316. // Assignment operator.
  317. cl_gcpointer& operator= (const cl_gcpointer&);
  318. // Distinguish immediate data from pointer.
  319. bool pointer_p() const
  320. { return true; }
  321. // Reference counting.
  322. void inc_pointer_refcount () const
  323. { cl_inc_pointer_refcount(heappointer); }
  324. void dec_pointer_refcount () const
  325. { cl_gc_dec_pointer_refcount(heappointer); }
  326. // Return the type tag of an immediate number.
  327. cl_uint nonpointer_tag () const
  328. { return cl_tag(word); }
  329. // Return the type tag of a heap-allocated number.
  330. const cl_class * pointer_type () const
  331. { return heappointer->type; }
  332. // Private pointer manipulations.
  333. cl_private_thing _as_cl_private_thing () const;
  334. // Private constructor.
  335. cl_gcpointer (cl_private_thing p)
  336. : pointer (p) {}
  337. // Debugging output.
  338. void debug_print () const;
  339. // Ability to place an object at a given address.
  340. void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
  341. void* operator new (size_t size) { return ::operator new (size); }
  342. };
  343. inline cl_gcpointer::cl_gcpointer () {}
  344. inline cl_gcpointer::~cl_gcpointer () { cl_dec_refcount(*this); }
  345. CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcpointer)
  346. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcpointer,cl_gcpointer)
  347. class cl_rcobject {
  348. public: /* ugh */
  349. union {
  350. void* pointer;
  351. cl_heap* heappointer;
  352. cl_uint word;
  353. };
  354. public:
  355. // Default constructor. (Used for objects with no initializer.)
  356. cl_rcobject ();
  357. // Destructor. (Used when a variable goes out of scope.)
  358. ~cl_rcobject ();
  359. // Copy constructor.
  360. cl_rcobject (const cl_rcobject&);
  361. // Assignment operator.
  362. cl_rcobject& operator= (const cl_rcobject&);
  363. // Distinguish immediate data from pointer.
  364. bool pointer_p() const
  365. { return cl_pointer_p(word); }
  366. // Reference counting.
  367. void inc_pointer_refcount () const
  368. { cl_inc_pointer_refcount(heappointer); }
  369. void dec_pointer_refcount () const
  370. { cl_rc_dec_pointer_refcount(heappointer); }
  371. // Return the type tag of an immediate number.
  372. cl_uint nonpointer_tag () const
  373. { return cl_tag(word); }
  374. // Return the type tag of a heap-allocated number.
  375. const cl_class * pointer_type () const
  376. { return heappointer->type; }
  377. // Private pointer manipulations.
  378. cl_private_thing _as_cl_private_thing () const;
  379. // Private constructor.
  380. cl_rcobject (cl_private_thing p)
  381. : pointer (p) {}
  382. // Debugging output.
  383. void debug_print () const;
  384. // Ability to place an object at a given address.
  385. void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
  386. void* operator new (size_t size) { return ::operator new (size); }
  387. };
  388. inline cl_rcobject::cl_rcobject () {}
  389. inline cl_rcobject::~cl_rcobject () { cl_dec_refcount(*this); }
  390. CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcobject)
  391. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcobject,cl_rcobject)
  392. class cl_rcpointer {
  393. public: /* ugh */
  394. union {
  395. void* pointer;
  396. cl_heap* heappointer;
  397. cl_uint word;
  398. };
  399. public:
  400. // Default constructor. (Used for objects with no initializer.)
  401. cl_rcpointer ();
  402. // Destructor. (Used when a variable goes out of scope.)
  403. ~cl_rcpointer ();
  404. // Copy constructor.
  405. cl_rcpointer (const cl_rcpointer&);
  406. // Assignment operator.
  407. cl_rcpointer& operator= (const cl_rcpointer&);
  408. // Distinguish immediate data from pointer.
  409. bool pointer_p() const
  410. { return true; }
  411. // Reference counting.
  412. void inc_pointer_refcount () const
  413. { cl_inc_pointer_refcount(heappointer); }
  414. void dec_pointer_refcount () const
  415. { cl_rc_dec_pointer_refcount(heappointer); }
  416. // Return the type tag of an immediate number.
  417. cl_uint nonpointer_tag () const
  418. { return cl_tag(word); }
  419. // Return the type tag of a heap-allocated number.
  420. const cl_class * pointer_type () const
  421. { return heappointer->type; }
  422. // Private pointer manipulations.
  423. cl_private_thing _as_cl_private_thing () const;
  424. // Private constructor.
  425. cl_rcpointer (cl_private_thing p)
  426. : pointer (p) {}
  427. // Debugging output.
  428. void debug_print () const;
  429. // Ability to place an object at a given address.
  430. void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
  431. void* operator new (size_t size) { return ::operator new (size); }
  432. };
  433. inline cl_rcpointer::cl_rcpointer () {}
  434. inline cl_rcpointer::~cl_rcpointer () { cl_dec_refcount(*this); }
  435. CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcpointer)
  436. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcpointer,cl_rcpointer)
  437. // Private pointer manipulations.
  438. inline cl_private_thing cl_gcobject::_as_cl_private_thing () const
  439. {
  440. cl_private_thing p = (cl_private_thing) pointer;
  441. cl_inc_refcount(*this);
  442. return p;
  443. }
  444. inline cl_private_thing as_cl_private_thing (const cl_gcobject& x)
  445. {
  446. return x._as_cl_private_thing();
  447. }
  448. inline cl_private_thing cl_gcpointer::_as_cl_private_thing () const
  449. {
  450. cl_private_thing p = (cl_private_thing) pointer;
  451. cl_inc_refcount(*this);
  452. return p;
  453. }
  454. inline cl_private_thing as_cl_private_thing (const cl_gcpointer& x)
  455. {
  456. return x._as_cl_private_thing();
  457. }
  458. inline cl_private_thing cl_rcobject::_as_cl_private_thing () const
  459. {
  460. cl_private_thing p = (cl_private_thing) pointer;
  461. cl_inc_refcount(*this);
  462. return p;
  463. }
  464. inline cl_private_thing as_cl_private_thing (const cl_rcobject& x)
  465. {
  466. return x._as_cl_private_thing();
  467. }
  468. inline cl_private_thing cl_rcpointer::_as_cl_private_thing () const
  469. {
  470. cl_private_thing p = (cl_private_thing) pointer;
  471. cl_inc_refcount(*this);
  472. return p;
  473. }
  474. inline cl_private_thing as_cl_private_thing (const cl_rcpointer& x)
  475. {
  476. return x._as_cl_private_thing();
  477. }
  478. // Note: When we define a function that returns a class object by value,
  479. // we normally return it as const value. The declarations
  480. // T func (...); (A)
  481. // and
  482. // const T func (...); (B)
  483. // behave identically and generate identical code, except that the code
  484. // func(...) = foo;
  485. // compiles fine with (A) but is an error (and yields a warning) with (B).
  486. // We want this warning.
  487. // Define a conversion operator from one object to another object of the
  488. // same size.
  489. #define CL_DEFINE_CONVERTER(target_class) \
  490. operator const target_class & () const \
  491. { \
  492. typedef int assert1 [2*(sizeof(target_class)==sizeof(*this))-1]; \
  493. return * (const target_class *) (void*) this; \
  494. }
  495. } // namespace cln
  496. #endif /* _CL_OBJECT_H */