| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* typespec.d - type specifier routines |
| 6 |
* |
| 7 |
* Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya |
| 8 |
* Copyright (c) 1990 Giuseppe Attardi |
| 9 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 10 |
* |
| 11 |
* See file 'LICENSE' for the copyright details. |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
#include <ecl/ecl.h> |
| 16 |
|
| 17 |
void |
| 18 |
FEtype_error_fixnum(cl_object x) { |
| 19 |
FEwrong_type_argument(@[fixnum], x); |
| 20 |
} |
| 21 |
|
| 22 |
void |
| 23 |
FEtype_error_size(cl_object x) { |
| 24 |
FEwrong_type_argument(cl_list(3, @'integer', ecl_make_fixnum(0), |
| 25 |
ecl_make_fixnum(MOST_POSITIVE_FIXNUM)), |
| 26 |
x); |
| 27 |
} |
| 28 |
|
| 29 |
void |
| 30 |
FEtype_error_cons(cl_object x) { |
| 31 |
FEwrong_type_argument(@[cons], x); |
| 32 |
} |
| 33 |
|
| 34 |
void |
| 35 |
FEtype_error_list(cl_object x) { |
| 36 |
FEwrong_type_argument(@[list], x); |
| 37 |
} |
| 38 |
|
| 39 |
void |
| 40 |
FEtype_error_proper_list(cl_object x) { |
| 41 |
cl_error(9, @'simple-type-error', @':format-control', |
| 42 |
@"Not a proper list ~D", |
| 43 |
@':format-arguments', cl_list(1, x), |
| 44 |
@':expected-type', ecl_read_from_cstring("si::proper-list"), |
| 45 |
@':datum', x); |
| 46 |
} |
| 47 |
|
| 48 |
void |
| 49 |
FEcircular_list(cl_object x) |
| 50 |
{ |
| 51 |
/* FIXME: Is this the right way to rebind it? */ |
| 52 |
ecl_bds_bind(ecl_process_env(), @'*print-circle*', ECL_T); |
| 53 |
cl_error(9, @'simple-type-error', @':format-control', |
| 54 |
@"Circular list ~D", |
| 55 |
@':format-arguments', cl_list(1, x), |
| 56 |
@':expected-type', @'list', |
| 57 |
@':datum', x); |
| 58 |
} |
| 59 |
|
| 60 |
void |
| 61 |
FEtype_error_index(cl_object seq, cl_fixnum ndx) |
| 62 |
{ |
| 63 |
cl_object n = ecl_make_fixnum(ndx); |
| 64 |
cl_index l = ECL_INSTANCEP(seq)? seq->instance.length : ecl_length(seq); |
| 65 |
cl_error(9, @'simple-type-error', @':format-control', |
| 66 |
@"~S is not a valid index into the object ~S", |
| 67 |
@':format-arguments', cl_list(2, n, seq), |
| 68 |
@':expected-type', cl_list(3, @'integer', ecl_make_fixnum(0), ecl_make_fixnum(l-1)), |
| 69 |
@':datum', n); |
| 70 |
} |
| 71 |
|
| 72 |
void |
| 73 |
FEtype_error_array(cl_object v) |
| 74 |
{ |
| 75 |
FEwrong_type_argument(@[array], v); |
| 76 |
} |
| 77 |
|
| 78 |
void |
| 79 |
FEtype_error_vector(cl_object v) |
| 80 |
{ |
| 81 |
FEwrong_type_argument(@[vector], v); |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
FEtype_error_sequence(cl_object x) { |
| 86 |
FEwrong_type_argument(@[sequence], x); |
| 87 |
} |
| 88 |
|
| 89 |
cl_object |
| 90 |
ecl_type_error(cl_object function, const char *place, cl_object o, |
| 91 |
cl_object type) |
| 92 |
{ |
| 93 |
return si_wrong_type_argument(4, o, type, |
| 94 |
(*place? ecl_make_constant_base_string(place,-1) : ECL_NIL), |
| 95 |
function); |
| 96 |
} |
| 97 |
|
| 98 |
/**********************************************************************/ |
| 99 |
|
| 100 |
static cl_object |
| 101 |
ecl_type_to_symbol(cl_type t) |
| 102 |
{ |
| 103 |
switch(t) { |
| 104 |
case t_character: |
| 105 |
return @'character'; |
| 106 |
case t_fixnum: |
| 107 |
return @'fixnum'; |
| 108 |
case t_bignum: |
| 109 |
return @'bignum'; |
| 110 |
case t_ratio: |
| 111 |
return @'ratio'; |
| 112 |
case t_singlefloat: |
| 113 |
return @'single-float'; |
| 114 |
case t_doublefloat: |
| 115 |
return @'double-float'; |
| 116 |
case t_longfloat: |
| 117 |
return @'long-float'; |
| 118 |
case t_complex: |
| 119 |
return @'complex'; |
| 120 |
#ifdef ECL_COMPLEX_FLOAT |
| 121 |
case t_csfloat: |
| 122 |
return @'si::complex-single-float'; |
| 123 |
case t_cdfloat: |
| 124 |
return @'si::complex-double-float'; |
| 125 |
case t_clfloat: |
| 126 |
return @'si::complex-long-float'; |
| 127 |
#endif |
| 128 |
case t_symbol: |
| 129 |
return @'symbol'; |
| 130 |
case t_package: |
| 131 |
return @'package'; |
| 132 |
case t_list: |
| 133 |
return @'list'; |
| 134 |
case t_hashtable: |
| 135 |
return @'hash-table'; |
| 136 |
case t_array: |
| 137 |
return @'array'; |
| 138 |
case t_vector: |
| 139 |
return @'vector'; |
| 140 |
case t_bitvector: |
| 141 |
return @'bit-vector'; |
| 142 |
#ifdef ECL_UNICODE |
| 143 |
case t_string: |
| 144 |
return @'string'; |
| 145 |
#endif |
| 146 |
case t_base_string: |
| 147 |
return @'base-string'; |
| 148 |
case t_stream: |
| 149 |
return @'stream'; |
| 150 |
case t_readtable: |
| 151 |
return @'readtable'; |
| 152 |
case t_pathname: |
| 153 |
return @'pathname'; |
| 154 |
case t_random: |
| 155 |
return @'random-state'; |
| 156 |
case t_bytecodes: |
| 157 |
case t_bclosure: |
| 158 |
case t_cfun: |
| 159 |
case t_cfunfixed: |
| 160 |
case t_cclosure: |
| 161 |
return @'compiled-function'; |
| 162 |
#ifdef ECL_THREADS |
| 163 |
case t_process: |
| 164 |
return @'mp::process'; |
| 165 |
case t_lock: |
| 166 |
return @'mp::lock'; |
| 167 |
case t_rwlock: |
| 168 |
return @'mp::rwlock'; |
| 169 |
case t_condition_variable: |
| 170 |
return @'mp::condition-variable'; |
| 171 |
case t_semaphore: |
| 172 |
return @'mp::semaphore'; |
| 173 |
case t_barrier: |
| 174 |
return @'mp::barrier'; |
| 175 |
case t_mailbox: |
| 176 |
return @'mp::mailbox'; |
| 177 |
#endif |
| 178 |
case t_codeblock: |
| 179 |
return @'si::code-block'; |
| 180 |
case t_foreign: |
| 181 |
return @'si::foreign-data'; |
| 182 |
case t_frame: |
| 183 |
return @'si::frame'; |
| 184 |
case t_weak_pointer: |
| 185 |
return @'ext::weak-pointer'; |
| 186 |
#ifdef ECL_SSE2 |
| 187 |
case t_sse_pack: |
| 188 |
return @'ext::sse-pack'; |
| 189 |
#endif |
| 190 |
default: |
| 191 |
ecl_internal_error("not a lisp data object"); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
cl_object |
| 196 |
ecl_check_cl_type(cl_object fun, cl_object p, cl_type t) |
| 197 |
{ |
| 198 |
while (ecl_t_of(p) != t) { |
| 199 |
p = ecl_type_error(fun, "argument", p, ecl_type_to_symbol(t)); |
| 200 |
} |
| 201 |
return p; |
| 202 |
} |
| 203 |
|
| 204 |
void |
| 205 |
assert_type_integer(cl_object p) |
| 206 |
{ |
| 207 |
cl_type t = ecl_t_of(p); |
| 208 |
if (t != t_fixnum && t != t_bignum) |
| 209 |
FEwrong_type_nth_arg(@[coerce], 1, p, @[integer]); |
| 210 |
} |
| 211 |
|
| 212 |
void |
| 213 |
assert_type_non_negative_integer(cl_object p) |
| 214 |
{ |
| 215 |
cl_type t = ecl_t_of(p); |
| 216 |
|
| 217 |
if (t == t_fixnum) { |
| 218 |
if (!ecl_fixnum_minusp(p)) |
| 219 |
return; |
| 220 |
} else if (t == t_bignum) { |
| 221 |
if (_ecl_big_sign(p) >= 0) |
| 222 |
return; |
| 223 |
} |
| 224 |
FEwrong_type_argument(cl_list(3,@'integer',ecl_make_fixnum(0),@'*'), p); |
| 225 |
} |
| 226 |
|
| 227 |
void |
| 228 |
assert_type_proper_list(cl_object p) |
| 229 |
{ |
| 230 |
if (ECL_ATOM(p) && p != ECL_NIL) |
| 231 |
FEtype_error_list(p); |
| 232 |
if (cl_list_length(p) == ECL_NIL) |
| 233 |
FEcircular_list(p); |
| 234 |
} |
| 235 |
|
| 236 |
cl_object |
| 237 |
cl_type_of(cl_object x) |
| 238 |
{ |
| 239 |
cl_object t; |
| 240 |
cl_type tx = ecl_t_of(x); |
| 241 |
switch (tx) { |
| 242 |
case t_instance: { |
| 243 |
cl_object cl = ECL_CLASS_OF(x); |
| 244 |
t = ECL_CLASS_NAME(cl); |
| 245 |
if (t == ECL_NIL || cl != cl_find_class(2, t, ECL_NIL)) |
| 246 |
t = cl; |
| 247 |
break; |
| 248 |
} |
| 249 |
case t_fixnum: |
| 250 |
case t_bignum: |
| 251 |
t = cl_list(3, @'integer', x, x); |
| 252 |
break; |
| 253 |
case t_character: { |
| 254 |
int i = ECL_CHAR_CODE(x); |
| 255 |
if (ecl_standard_char_p(i)) { |
| 256 |
t = @'standard-char'; |
| 257 |
} else if (ecl_base_char_p(i)) { |
| 258 |
t = @'base-char'; |
| 259 |
} else { |
| 260 |
t = @'character'; |
| 261 |
} |
| 262 |
break; |
| 263 |
} |
| 264 |
#ifdef ECL_COMPLEX_FLOAT |
| 265 |
case t_complex: |
| 266 |
t = cl_list(2, @'complex', @'rational'); |
| 267 |
break; |
| 268 |
case t_csfloat: |
| 269 |
t = cl_list(2, @'complex', @'single-float'); |
| 270 |
break; |
| 271 |
case t_cdfloat: |
| 272 |
t = cl_list(2, @'complex', @'double-float'); |
| 273 |
break; |
| 274 |
case t_clfloat: |
| 275 |
t = cl_list(2, @'complex', @'long-float'); |
| 276 |
break; |
| 277 |
#else |
| 278 |
case t_complex: |
| 279 |
t = cl_list(2, @'complex', @'real'); |
| 280 |
break; |
| 281 |
#endif |
| 282 |
case t_symbol: |
| 283 |
if (x == ECL_T) |
| 284 |
t = @'boolean'; |
| 285 |
else if (x->symbol.hpack == cl_core.keyword_package) |
| 286 |
t = @'keyword'; |
| 287 |
else |
| 288 |
t = @'symbol'; |
| 289 |
break; |
| 290 |
case t_array: |
| 291 |
if (ECL_ADJUSTABLE_ARRAY_P(x) || |
| 292 |
!Null(CAR(x->array.displaced))) |
| 293 |
t = @'array'; |
| 294 |
else |
| 295 |
t = @'simple-array'; |
| 296 |
t = cl_list(3, t, ecl_elttype_to_symbol(ecl_array_elttype(x)), |
| 297 |
cl_array_dimensions(x)); |
| 298 |
break; |
| 299 |
case t_vector: |
| 300 |
if (ECL_ADJUSTABLE_ARRAY_P(x) || |
| 301 |
!Null(CAR(x->vector.displaced))) { |
| 302 |
t = cl_list(3, @'vector', ecl_elttype_to_symbol(ecl_array_elttype(x)), |
| 303 |
ecl_make_fixnum(x->vector.dim)); |
| 304 |
} else if (ECL_ARRAY_HAS_FILL_POINTER_P(x) || |
| 305 |
(cl_elttype)x->vector.elttype != ecl_aet_object) { |
| 306 |
t = cl_list(3, @'simple-array', |
| 307 |
ecl_elttype_to_symbol(ecl_array_elttype(x)), |
| 308 |
cl_array_dimensions(x)); |
| 309 |
} else { |
| 310 |
t = cl_list(2, @'simple-vector', ecl_make_fixnum(x->vector.dim)); |
| 311 |
} |
| 312 |
break; |
| 313 |
#ifdef ECL_UNICODE |
| 314 |
case t_string: |
| 315 |
if (ECL_ADJUSTABLE_ARRAY_P(x) || |
| 316 |
ECL_ARRAY_HAS_FILL_POINTER_P(x) || |
| 317 |
!Null(CAR(x->string.displaced))) |
| 318 |
t = @'array'; |
| 319 |
else |
| 320 |
t = @'simple-array'; |
| 321 |
t = cl_list(3, t, @'character', cl_list(1, ecl_make_fixnum(x->string.dim))); |
| 322 |
break; |
| 323 |
#endif |
| 324 |
case t_base_string: |
| 325 |
if (ECL_ADJUSTABLE_ARRAY_P(x) || |
| 326 |
ECL_ARRAY_HAS_FILL_POINTER_P(x) || |
| 327 |
!Null(CAR(x->base_string.displaced))) |
| 328 |
t = @'array'; |
| 329 |
else |
| 330 |
t = @'simple-array'; |
| 331 |
t = cl_list(3, t, @'base-char', cl_list(1, ecl_make_fixnum(x->base_string.dim))); |
| 332 |
break; |
| 333 |
case t_bitvector: |
| 334 |
if (ECL_ADJUSTABLE_ARRAY_P(x) || |
| 335 |
ECL_ARRAY_HAS_FILL_POINTER_P(x) || |
| 336 |
!Null(CAR(x->vector.displaced))) |
| 337 |
t = @'array'; |
| 338 |
else |
| 339 |
t = @'simple-array'; |
| 340 |
t = cl_list(3, t, @'bit', cl_list(1, ecl_make_fixnum(x->vector.dim))); |
| 341 |
break; |
| 342 |
case t_stream: |
| 343 |
switch (x->stream.mode) { |
| 344 |
case ecl_smm_synonym: t = @'synonym-stream'; break; |
| 345 |
case ecl_smm_broadcast: t = @'broadcast-stream'; break; |
| 346 |
case ecl_smm_concatenated: t = @'concatenated-stream'; break; |
| 347 |
case ecl_smm_two_way: t = @'two-way-stream'; break; |
| 348 |
case ecl_smm_string_input: |
| 349 |
case ecl_smm_string_output: t = @'string-stream'; break; |
| 350 |
case ecl_smm_echo: t = @'echo-stream'; break; |
| 351 |
case ecl_smm_sequence_input: |
| 352 |
case ecl_smm_sequence_output: t = @'ext::sequence-stream'; break; |
| 353 |
default: t = @'file-stream'; break; |
| 354 |
} |
| 355 |
break; |
| 356 |
case t_pathname: |
| 357 |
t = x->pathname.logical? @'logical-pathname' : @'pathname'; |
| 358 |
break; |
| 359 |
case t_list: |
| 360 |
t = Null(x) ? @'null' : @'cons'; |
| 361 |
break; |
| 362 |
#ifdef ECL_SSE2 |
| 363 |
case t_sse_pack: |
| 364 |
t = @'ext::sse-pack'; |
| 365 |
break; |
| 366 |
#endif |
| 367 |
default: |
| 368 |
t = ecl_type_to_symbol(tx); |
| 369 |
} |
| 370 |
@(return t); |
| 371 |
} |
| 372 |
|
| 373 |
cl_object |
| 374 |
ecl_make_integer_type(cl_object min, cl_object max) |
| 375 |
{ |
| 376 |
return cl_list(3, @'integer', min, max); |
| 377 |
} |