Debian Codegraph @

src/gauche.h

raw
1 /*
2 * gauche.h - Gauche scheme system header
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #ifndef GAUCHE_H
35 #define GAUCHE_H
36
37 /* Read config.h _before_ other headers, for it may affect the behavior
38 of system header files. Currently the only known instance of it is
39 sigwait() on Solaris---we need to define _POSIX_PTHREAD_SEMANTICS to
40 get pthread-compatible sigwait()---but we may encounter more of such
41 instances. */
42 #include <gauche/config.h>
43 #include <gauche/config_threads.h>
44
45 /* GAUCHE_API_VERSION is GAUCHE_MAJOR_VERSION*1000 + revision.
46 The revision is only incremented when we change API, which we expect
47 rare during the same major revision.
48
49 As of 0.9.10, API_VERSION is 97, corresponding to libgauche-0.97.so.
50
51 As of 0.9.11, API_VERSION is 98, corresponding to libgauche-0.98.so.
52
53 This can be overridden by --with-api-version configure option.
54 */
55 #ifndef GAUCHE_API_VERSION
56 #define GAUCHE_API_VERSION 98
57 //#define GAUCHE_API_VERSION 1000
58 #endif
59
60 /* As of 1.0, We always use utf8 in internal encoding. However,
61 existing code may check this. */
62 #define GAUCHE_CHAR_ENCODING_UTF8 1
63
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <sys/types.h>
67 #include <sys/stat.h>
68 #include <stdarg.h>
69 #include <setjmp.h>
70 #include <limits.h>
71 #include <signal.h>
72 #include <string.h>
73 #include <errno.h>
74 #include <stdint.h>
75 #include <inttypes.h>
76 #include <math.h>
77 #include <complex.h> /* needs to be outside of extern "C" */
78
79 #ifdef HAVE_SYS_TIME_H
80 # include <sys/time.h>
81 #endif
82 #include <time.h>
83
84 typedef ssize_t ScmSize;
85
86 /* For Windows platforms, we need some compatibility tricks.
87 This defines GAUCHE_WINDOWS preprocessor symbol.
88 (This should come before including gc.h) */
89 #if defined(__MINGW32__) || defined(MSVC)
90 #include <gauche/win-compat.h>
91 #endif /* MINGW32 || WINDOWS */
92
93 /* Defines SCM_EXTERN magic. */
94 #include <gauche/extern.h>
95
96 #if defined(LIBGAUCHE_BODY)
97 /* We incoporate gc files within libgauche, so we need to refer to them
98 "internally" while we're compiling gauche body. SCM_EXTERN handles it. */
99 #define GC_API SCM_EXTERN
100 #if !defined(GC_DLL)
101 #define GC_DLL /* for gc.h to handle Win32 crazyness */
102 #endif
103 #endif /* LIBGAUCHE_BODY */
104 #include <gc.h>
105
106 #ifndef SCM_DECL_BEGIN
107 #ifdef __cplusplus
108 #define SCM_DECL_BEGIN extern "C" {
109 #define SCM_DECL_END }
110 #else /*! __cplusplus */
111 #define SCM_DECL_BEGIN
112 #define SCM_DECL_END
113 #endif /*! __cplusplus */
114 #endif /*!defined(SCM_DECL_BEGIN)*/
115
116 SCM_DECL_BEGIN
117
118 #ifdef HAVE_UNISTD_H
119 #include <unistd.h>
120 #endif /*HAVE_UNISTD_H*/
121
122 /* This must come after gauche/extern.h */
123 #include <gauche/float.h>
124
125 /* Some useful macros */
126 #ifndef FALSE
127 #define FALSE 0
128 #endif
129 #ifndef TRUE
130 #define TRUE (!FALSE)
131 #endif
132
133
134
135 /* Define this to 0 to turn off fast flonum extension. See the comment in
136 gauche/number.h for the details. */
137 #define GAUCHE_FFX 1
138
139 /* Temporary - to test alignment of pairs */
140 #define GAUCHE_CHECK_PAIR_ALIGNMENT 0
141
142 /* Enable an option to make keywords and symbols disjoint.
143 (Transient: Will be gone once we completely migrate to
144 unified keyword-symbol system */
145 #define GAUCHE_KEEP_DISJOINT_KEYWORD_OPTION 1
146
147 /* Experimental: Enable lightweight continuation capturing in exception
148 handling. */
149 #define GAUCHE_SPLIT_STACK 0
150
151 /* Define to 1 if debugging the bootstrap module */
152 #define GAUCHE_DEBUG_BOOTSTRAP 0
153
154 /* Include appropriate threading interface. Threading primitives are
155 abstracted with SCM_INTERNAL_* macros and ScmInternal* typedefs.
156 See gauche/pthread.h for the semantics of these primitives. */
157 #ifdef GAUCHE_USE_PTHREADS
158 # include <gauche/pthread.h>
159 #elif GAUCHE_USE_WTHREADS
160 # include <gauche/wthread.h>
161 #else
162 # error "No thread support"
163 #endif
164
165 /* For the backward compatibility. Now we always have threads. */
166 #define GAUCHE_HAS_THREADS 1
167
168 #define SCM_WORD_BITS (SIZEOF_LONG*8)
169
170 /* Newer gcc/glibc adds lots of __attribute__((warn_unused_result)) that
171 causes excessive warnings for the code that intentionally ignores the
172 return value. Casting the result to void won't silence it.
173 Hence this macro. */
174 #define SCM_IGNORE_RESULT(expr) do { if(expr) {} } while(0)
175
176 /* ScmFlonum and ScmClass must always be aligned in 8-byte boundaries.
177 Some platform doesn't align static double in 8-byte boundaries, so
178 we try this as well. */
179 #ifdef __GNUC__
180 #define SCM_ALIGN8 __attribute__ ((aligned (8)))
181 #else /* !__GNUC__ */
182 #define SCM_ALIGN8 /*empty*/
183 #endif /* !__GNUC__ */
184
185 /* Statically allocated ScmPair must be aligned in two ScmWords boundary.*/
186 #ifdef __GNUC__
187 #define SCM_PAIR_ALWAYS_ALIGNED_EVEN_WORDS 1
188 #define SCM_ALIGN_PAIR __attribute__ ((aligned(sizeof(ScmWord)*2)))
189 #else /* !__GNUC__ */
190 #define SCM_PAIR_ALWAYS_ALIGNED_EVEN_WORDS 0
191 #define SCM_ALIGN_PAIR /*empty*/
192 #endif /* !__GNUC__ */
193
194 /* 'No return' attribute */
195 #ifdef __GNUC__
196 #define SCM_NORETURN __attribute__((__noreturn__))
197 #else /*__GNUC__*/
198 #define SCM_NORETURN /*empty*/
199 #endif /*__GNUC__*/
200
201 /* 'unused' attribute */
202 #ifdef __GNUC__
203 #define SCM_UNUSED __attribute__((__unused__))
204 #else /*__GNUC__*/
205 #define SCM_UNUSED /*empty*/
206 #endif /*__GNUC__*/
207
208 /* 'noinline' attribute */
209 #ifdef __GNUC__
210 #define SCM_NOINLINE __attribute__((__noinline__))
211 #else /*__GNUC__*/
212 #define SCM_NOINLINE /*empty*/
213 #endif /*__GNUC__*/
214
215
216 /*-------------------------------------------------------------
217 * BASIC TYPES
218 */
219
220 /*
221 * A word large enough to hold a pointer
222 */
223 typedef intptr_t ScmWord;
224
225 /*
226 * A byte
227 */
228 typedef unsigned char ScmByte;
229
230 /*
231 * A character.
232 */
233 typedef long ScmChar;
234
235 /*
236 * An opaque pointer. All Scheme objects are represented by
237 * this type.
238 */
239 typedef struct ScmHeaderRec *ScmObj;
240
241 /*
242 * The class structure. ScmClass is actually a subclass of ScmObj.
243 */
244 typedef struct ScmClassRec ScmClass;
245
246 /* TAG STRUCTURE
247 *
248 * [Pointer]
249 * -------- -------- -------- ------00
250 * Points to a pair or other heap-allocated objects.
251 * If the lower 3 bits of the pointed word are '111',
252 * it's a heap object (see below). Otherwise, it's
253 * a pair.
254 *
255 * [Fixnum]
256 * -------- -------- -------- ------01
257 * 30 or 62-bit signed integer
258 *
259 * [Flonum]
260 * -------- -------- -------- -----M10
261 * Points to C double. M=0 if the double is in the VM
262 * register, M=1 if it is on the heap. See the comment in
263 * gauche/number.h for the details.
264 *
265 * [Character]
266 * -------- -------- -------- 00000011
267 * 24-bit. 20bits are enough to cover all UCS, but we
268 * reserve a few extra bits for possible future extension.
269 *
270 * [Miscellaneous]
271 * -------- -------- -------- 00001011
272 * #f, #t, '(), eof-object, undefined, uninitialized
273 *
274 * [Pattern variable]
275 * -------- -------- -------- 00010011
276 * Used in macro expander.
277 *
278 * [String cursor]
279 * -------- -------- -------- 00011011
280 * Represent short string cursors.
281 *
282 * [Heap object]
283 * -------- -------- -------- -----111
284 * Only appears at the first word of heap-allocated
285 * objects except pairs and flonums. Masking lower
286 * 3bits gives a pointer to ScmClass.
287 */
288
289 /* Type coercer */
290
291 #define SCM_OBJ(obj) ((ScmObj)(obj))
292 #define SCM_WORD(obj) ((ScmWord)(obj))
293
294 /*
295 * PRIMARY TAG IDENTIFICATION
296 */
297
298 #define SCM_TAG1(obj) (SCM_WORD(obj) & 0x01)
299 #define SCM_TAG2(obj) (SCM_WORD(obj) & 0x03)
300 #define SCM_TAG3(obj) (SCM_WORD(obj) & 0x07)
301 #define SCM_TAG8(obj) (SCM_WORD(obj) & 0xff)
302
303 /* Check if the ScmObj is a 'pointer'---either to a pair,
304 a heap object, or a ScmFlonum. */
305 #define SCM_PTRP(obj) (SCM_TAG1(obj) == 0)
306
307 /* Check if the ScmObj is a pointer to either a pair or a heap
308 (That is, we can safely take SCM_OBJ(obj)->tag) */
309 #define SCM_HPTRP(obj) (SCM_TAG2(obj) == 0)
310
311 /* This macro further takes the lower three bits of the word pointed
312 by OBJ, to distinguish whether it's a pair or a heap object. */
313 #define SCM_HTAG(obj) (SCM_WORD(SCM_OBJ(obj)->tag)&7)
314
315 /*
316 * IMMEDIATE OBJECTS
317 */
318
319 #define SCM_IMMEDIATEP(obj) (SCM_TAG8(obj) == 0x0b)
320 #define SCM_ITAG(obj) (SCM_WORD(obj)>>8)
321
322 #define SCM__MAKE_ITAG(num) (((num)<<8) + 0x0b)
323 #define SCM_FALSE SCM_OBJ(SCM__MAKE_ITAG(0)) /* #f */
324 #define SCM_TRUE SCM_OBJ(SCM__MAKE_ITAG(1)) /* #t */
325 #define SCM_NIL SCM_OBJ(SCM__MAKE_ITAG(2)) /* '() */
326 #define SCM_EOF SCM_OBJ(SCM__MAKE_ITAG(3)) /* eof-object */
327 #define SCM_UNDEFINED SCM_OBJ(SCM__MAKE_ITAG(4)) /* #undefined */
328 #define SCM_UNBOUND SCM_OBJ(SCM__MAKE_ITAG(5)) /* unbound value */
329 #define SCM_UNINITIALIZED SCM_OBJ(SCM__MAKE_ITAG(6)) /* uninitialized */
330
331 #define SCM_FALSEP(obj) ((obj) == SCM_FALSE)
332 #define SCM_TRUEP(obj) ((obj) == SCM_TRUE)
333 #define SCM_NULLP(obj) ((obj) == SCM_NIL)
334 #define SCM_EOFP(obj) ((obj) == SCM_EOF)
335 #define SCM_UNDEFINEDP(obj) ((obj) == SCM_UNDEFINED)
336 #define SCM_UNBOUNDP(obj) ((obj) == SCM_UNBOUND)
337 #define SCM_UNINITIALIZEDP(obj) ((obj) == SCM_UNINITIALIZED)
338
339 /*
340 * BOOLEAN
341 */
342 #define SCM_BOOLP(obj) ((obj) == SCM_TRUE || (obj) == SCM_FALSE)
343 #define SCM_BOOL_VALUE(obj) (!SCM_FALSEP(obj))
344 #define SCM_MAKE_BOOL(obj) ((obj)? SCM_TRUE:SCM_FALSE)
345
346 #define SCM_EQ(x, y) ((x) == (y))
347
348 SCM_EXTERN int Scm_EqP(ScmObj x, ScmObj y);
349 SCM_EXTERN int Scm_EqvP(ScmObj x, ScmObj y);
350 SCM_EXTERN int Scm_EqualP(ScmObj x, ScmObj y);
351
352 /* comparison mode */
353 enum {
354 SCM_CMP_EQ,
355 SCM_CMP_EQV,
356 SCM_CMP_EQUAL
357 };
358
359 SCM_EXTERN int Scm_EqualM(ScmObj x, ScmObj y, int mode);
360
361 /*
362 * FIXNUM
363 */
364
365 #define SCM_INTP(obj) (SCM_TAG2(obj) == 1)
366 #define SCM_INT_VALUE(obj) (((signed long int)SCM_WORD(obj)) >> 2)
367 #define SCM_MAKE_INT(obj) SCM_OBJ(((uintptr_t)(obj) << 2) + 1)
368
369 #define SCM_UINTP(obj) (SCM_INTP(obj)&&((signed long int)SCM_WORD(obj)>=0))
370 typedef long ScmSmallInt; /* C integer type corresponds to Scheme fixnum
371 See SCM_SMALL_* macros in gauche/number.h */
372
373 /*
374 * FLONUM
375 */
376
377 typedef struct ScmFlonumRec {
378 double val;
379 } ScmFlonum SCM_ALIGN8;
380
381 #define SCM_FLONUM(obj) ((ScmFlonum*)(SCM_WORD(obj)&~0x07))
382 #define SCM_FLONUMP(obj) (SCM_TAG2(obj) == 2)
383 #define SCM_FLONUM_VALUE(obj) (SCM_FLONUM(obj)->val)
384
385 /*
386 * CHARACTERS
387 *
388 * A character is represented by an integer Unicode codepoint.
389 */
390
391 #define SCM_CHAR(obj) ((ScmChar)(obj))
392 #define SCM_CHARP(obj) ((SCM_WORD(obj)&0xff) == 3)
393 #define SCM_CHAR_VALUE(obj) SCM_CHAR(((unsigned long)SCM_WORD(obj)) >> 8)
394 #define SCM_MAKE_CHAR(ch) SCM_OBJ((intptr_t)(((unsigned long)(ch))<<8) + 3)
395
396 #define SCM_CHAR_INVALID ((ScmChar)(-2)) /* indicate invalid char (-2 is used to avoid conflict with libc's EOF) */
397 #define SCM_CHAR_MAX (0x10ffff)
398
399 #define SCM_CHAR_ASCII_P(ch) ((ch) < 0x80)
400
401 /* The following four macros are obsoleted; use API version instead.*/
402 #define SCM_CHAR_UPPER_P(ch) Scm_CharUppercaseP(ch)
403 #define SCM_CHAR_LOWER_P(ch) Scm_CharLowercaseP(ch)
404 #define SCM_CHAR_UPCASE(ch) Scm_CharUpcase(ch)
405 #define SCM_CHAR_DOWNCASE(ch) Scm_CharDowncase(ch)
406
407 SCM_EXTERN int Scm_DigitToInt(ScmChar ch, int radix, int extended);
408 SCM_EXTERN ScmChar Scm_IntToDigit(int n, int radix, int basechar1, int basechar2);
409 SCM_EXTERN int Scm_CharToUcs(ScmChar ch);
410 SCM_EXTERN ScmChar Scm_UcsToChar(int ucs);
411 SCM_EXTERN ScmObj Scm_CharEncodingName(void);
412 SCM_EXTERN const char **Scm_SupportedCharacterEncodings(void);
413 SCM_EXTERN int Scm_SupportedCharacterEncodingP(const char *encoding);
414
415 SCM_EXTERN int Scm_CharGeneralCategory(ScmChar ch);
416 SCM_EXTERN int Scm_CharAlphabeticP(ScmChar ch);
417 SCM_EXTERN int Scm_CharUppercaseP(ScmChar ch);
418 SCM_EXTERN int Scm_CharLowercaseP(ScmChar ch);
419 SCM_EXTERN int Scm_CharTitlecaseP(ScmChar ch);
420 SCM_EXTERN int Scm_CharNumericP(ScmChar ch);
421
422 SCM_EXTERN ScmChar Scm_CharUpcase(ScmChar ch);
423 SCM_EXTERN ScmChar Scm_CharDowncase(ScmChar ch);
424 SCM_EXTERN ScmChar Scm_CharTitlecase(ScmChar ch);
425 SCM_EXTERN ScmChar Scm_CharFoldcase(ScmChar ch);
426
427 #include "gauche/char_utf_8.h"
428
429 /* Character lexer category. See 7.1.1 of R7RS */
430 typedef enum {
431 SCM_CHAR_INITIAL,
432 SCM_CHAR_SUBSEQUENT,
433 SCM_CHAR_SIGN_SUBSEQUENT,
434 } ScmCharLexerCategory;
435
436 SCM_EXTERN int Scm_CharLexerCategoryP(ScmChar c, ScmCharLexerCategory cat);
437
438 /*
439 * HEAP ALLOCATED OBJECTS
440 *
441 * A heap allocated object has its class tag in the first word
442 * (except pairs). Masking the lower three bits of class tag
443 * gives a pointer to the class object.
444 */
445
446 #define SCM_HOBJP(obj) (SCM_HPTRP(obj)&&(SCM_HTAG(obj)==7))
447
448 #define SCM_CPP_CAT(a, b) a##b
449 #define SCM_CPP_CAT3(a, b, c) a ## b ## c
450
451 /* We use a pointer to the class structure (with low-bit tag) as
452 the generic type tag. NB: The ScmClass structure is always
453 aligned on 8-byte boundary, so +7 makes the tag's lower
454 3 bits '111'. Such pattern never appears in tagged pointer,
455 so we can distinguish heap allocated objects from ScmPair. */
456 #define SCM_CLASS2TAG(klass) ((ScmByte*)(klass) + 7)
457
458 /* A common header for heap-allocated objects */
459 typedef struct ScmHeaderRec {
460 ScmByte *tag; /* private. should be accessed
461 only via SCM_CLASS_OF and SCM_SET_CLASS
462 macros. */
463 } ScmHeader;
464
465 #define SCM_HEADER ScmHeader hdr /* for declaration */
466
467 /* Here comes the ugly part. To understand the general idea, just ignore
468 GAUCHE_BROKEN_LINKER_WORKAROUND part; except that, it's pretty simple.
469 Every heap allocated object contains (pointer to its class + 7) in its
470 tag field. */
471 #if !defined(GAUCHE_BROKEN_LINKER_WORKAROUND)
472
473 # define SCM_CLASS_DECL(klass) extern ScmClass klass
474 # define SCM_CLASS_STATIC_PTR(klass) (&klass)
475 # define SCM_CLASS_STATIC_TAG(klass) SCM_CLASS2TAG(&klass)
476
477 /* Extract the class pointer from the tag.
478 You can use these only if SCM_HOBJP(obj) != FALSE.
479 To get a class of a given object, always use Scm_ClassOf().
480 */
481 # define SCM_CLASS_OF(obj) SCM_CLASS((SCM_OBJ(obj)->tag - 7))
482 # define SCM_SET_CLASS(obj, k) (SCM_OBJ(obj)->tag = (ScmByte*)(k) + 7)
483
484 /* Check if classof(OBJ) equals to an extended class KLASS.
485 We can check SCM_HPTRP instead of SCM_HOBJP here, since a pair never
486 satisfies the second test. */
487 # define SCM_XTYPEP(obj, klass) \
488 (SCM_HPTRP(obj)&&(SCM_OBJ(obj)->tag == SCM_CLASS2TAG(klass)))
489
490 #else /*GAUCHE_BROKEN_LINKER_WORKAROUND*/
491
492 /* You don't want to understand these. */
493 # define SCM_CLASS_DECL(klass) \
494 SCM_EXTERN ScmClass klass; \
495 extern ScmClass *SCM_CPP_CAT(_imp__, klass)
496 # define SCM_CLASS_STATIC_PTR(klass) ((ScmClass*)(&SCM_CPP_CAT(_imp__,klass)))
497 # define SCM_CLASS_STATIC_TAG(klass) SCM_CLASS2TAG(SCM_CLASS_STATIC_PTR(klass))
498
499 # define SCM_CLASS_OF(obj) (*(ScmClass**)((SCM_OBJ(obj)->tag - 7)))
500 # define SCM_SET_CLASS(obj, k) (SCM_OBJ(obj)->tag = (ScmByte*)((k)->classPtr) + 7)
501
502 # define SCM_XTYPEP(obj, klass) \
503 (SCM_HOBJP(obj)&&(SCM_CLASS_OF(obj) == klass))
504 #endif /*GAUCHE_BROKEN_LINKER_WORKAROUND*/
505
506
507
508 /* Check if classof(OBJ) is a subtype of an extended class KLASS */
509 #define SCM_ISA(obj, klass) (SCM_XTYPEP(obj,klass)||Scm_TypeP(SCM_OBJ(obj),klass))
510
511 /* A common header for objects whose class is defined in Scheme */
512 typedef struct ScmInstanceRec {
513 ScmByte *tag; /* private */
514 ScmObj *slots; /* private */
515 } ScmInstance;
516
517 #define SCM_INSTANCE_HEADER ScmInstance hdr /* for declaration */
518
519 #define SCM_INSTANCE(obj) ((ScmInstance*)(obj))
520 #define SCM_INSTANCE_SLOTS(obj) (SCM_INSTANCE(obj)->slots)
521
522 /* Fundamental allocators */
523 #define SCM_MALLOC(size) GC_MALLOC(size)
524 #define SCM_MALLOC_ATOMIC(size) GC_MALLOC_ATOMIC(size)
525 #define SCM_STRDUP(s) GC_STRDUP(s)
526 #define SCM_STRDUP_PARTIAL(s, n) Scm_StrdupPartial(s, n)
527
528 #define SCM_NEW(type) ((type*)(SCM_MALLOC(sizeof(type))))
529 #define SCM_NEW_ARRAY(type, nelts) ((type*)(SCM_MALLOC(sizeof(type)*(nelts))))
530 #define SCM_NEW2(type, size) ((type)(SCM_MALLOC(size)))
531 #define SCM_NEW_ATOMIC(type) ((type*)(SCM_MALLOC_ATOMIC(sizeof(type))))
532 #define SCM_NEW_ATOMIC_ARRAY(type, nelts) ((type*)(SCM_MALLOC_ATOMIC(sizeof(type)*(nelts))))
533 #define SCM_NEW_ATOMIC2(type, size) ((type)(SCM_MALLOC_ATOMIC(size)))
534
535 typedef void (*ScmFinalizerProc)(ScmObj z, void *data);
536 SCM_EXTERN void Scm_RegisterFinalizer(ScmObj z, ScmFinalizerProc finalizer,
537 void *data);
538 SCM_EXTERN void Scm_UnregisterFinalizer(ScmObj z);
539
540 /* Safe coercer */
541 #define SCM_OBJ_SAFE(obj) ((obj)?SCM_OBJ(obj):SCM_UNDEFINED)
542
543 typedef struct ScmVMRec ScmVM;
544 typedef struct ScmPairRec ScmPair;
545 typedef struct ScmExtendedPairRec ScmExtendedPair;
546 typedef struct ScmLazyPairRec ScmLazyPair;
547 typedef struct ScmCharSetRec ScmCharSet;
548 typedef struct ScmStringRec ScmString;
549 typedef struct ScmDStringRec ScmDString;
550 typedef struct ScmVectorRec ScmVector;
551 typedef struct ScmBignumRec ScmBignum;
552 typedef struct ScmRatnumRec ScmRatnum;
553 typedef struct ScmCompnumRec ScmCompnum;
554 typedef struct ScmPortRec ScmPort;
555 typedef struct ScmHashTableRec ScmHashTable;
556 typedef struct ScmTreeMapRec ScmTreeMap;
557 typedef struct ScmModuleRec ScmModule;
558 typedef struct ScmSymbolRec ScmSymbol;
559 typedef struct ScmGlocRec ScmGloc;
560 typedef struct ScmProcedureRec ScmProcedure;
561 typedef struct ScmClosureRec ScmClosure;
562 typedef struct ScmSubrRec ScmSubr;
563 typedef struct ScmGenericRec ScmGeneric;
564 typedef struct ScmMethodRec ScmMethod;
565 typedef struct ScmNextMethodRec ScmNextMethod;
566 typedef struct ScmSyntaxRec ScmSyntax;
567 typedef struct ScmMacroRec ScmMacro;
568 typedef struct ScmMemoryRegionRec ScmMemoryRegion; /* see mmapP.h */
569 typedef struct ScmPromiseRec ScmPromise;
570 typedef struct ScmRegexpRec ScmRegexp;
571 typedef struct ScmRegMatchRec ScmRegMatch;
572 typedef struct ScmWriteControlsRec ScmWriteControls; /* see writerP.h */
573 typedef struct ScmWriteContextRec ScmWriteContext; /* see writerP.h */
574 typedef struct ScmWriteStateRec ScmWriteState; /* see wrtierP.h */
575 typedef struct ScmAutoloadRec ScmAutoload;
576 typedef struct ScmComparatorRec ScmComparator;
577 typedef struct ScmDLObjRec ScmDLObj; /* see load.c */
578 typedef struct ScmReadContextRec ScmReadContext; /* see read.c */
579
580 typedef ScmObj ScmSubrProc(ScmObj *, int, void*);
581
582 #include <gauche/bits.h>
583
584 /*---------------------------------------------------------
585 * VM STUFF
586 */
587
588 /* Detailed definitions are in vm.h. Here I expose external interface */
589
590 #include <gauche/parameter.h>
591 #include <gauche/vm.h>
592
593 #define SCM_VM(obj) ((ScmVM *)(obj))
594 #define SCM_VMP(obj) SCM_XTYPEP(obj, SCM_CLASS_VM)
595
596 SCM_EXTERN ScmVM *Scm_VM(void); /* Returns the current VM */
597
598 /* The new APIs to run Scheme code from C.
599 Returns # of results (>=0) if operation is successful,
600 -1 if an error is occurred and captured.
601 All result values are available in ScmEvalPacket.
602 Exceptions are captured and returned in the ScmEvalPacket. */
603 typedef struct ScmEvalPacketRec {
604 ScmObj results[SCM_VM_MAX_VALUES];
605 int numResults;
606 ScmObj exception;
607 ScmModule *module; /* 'Current module' after evaluation */
608 } ScmEvalPacket;
609
610 SCM_EXTERN int Scm_Eval(ScmObj form, ScmObj env, ScmEvalPacket *packet);
611 SCM_EXTERN int Scm_EvalCString(const char *form, ScmObj env,
612 ScmEvalPacket *packet);
613 SCM_EXTERN int Scm_Apply(ScmObj proc, ScmObj args,
614 ScmEvalPacket *packet);
615
616 /* Calls VM recursively to evaluate the Scheme code. These
617 ones does not capture exceptions. */
618 SCM_EXTERN ScmObj Scm_EvalRec(ScmObj form, ScmObj env);
619 SCM_EXTERN ScmObj Scm_ApplyRec(ScmObj proc, ScmObj args);
620 SCM_EXTERN ScmObj Scm_ApplyRec0(ScmObj proc);
621 SCM_EXTERN ScmObj Scm_ApplyRec1(ScmObj proc, ScmObj arg0);
622 SCM_EXTERN ScmObj Scm_ApplyRec2(ScmObj proc, ScmObj arg0, ScmObj arg1);
623 SCM_EXTERN ScmObj Scm_ApplyRec3(ScmObj proc, ScmObj arg0, ScmObj arg1,
624 ScmObj arg2);
625 SCM_EXTERN ScmObj Scm_ApplyRec4(ScmObj proc, ScmObj arg0, ScmObj arg1,
626 ScmObj arg2, ScmObj arg3);
627 SCM_EXTERN ScmObj Scm_ApplyRec5(ScmObj proc, ScmObj arg0, ScmObj arg1,
628 ScmObj arg2, ScmObj arg3, ScmObj arg4);
629
630 /* for compatibility */
631 #define Scm_EvalCStringRec(f, e) Scm_EvalRec(Scm_ReadFromCString(f), e)
632
633 /* Returns multiple values. Actually these functions just sets
634 extra values in VM and returns the primary value. */
635 SCM_EXTERN ScmObj Scm_Values(ScmObj args);
636 SCM_EXTERN ScmObj Scm_Values2(ScmObj val0, ScmObj val1);
637 SCM_EXTERN ScmObj Scm_Values3(ScmObj val0, ScmObj val1, ScmObj val2);
638 SCM_EXTERN ScmObj Scm_Values4(ScmObj val0, ScmObj val1, ScmObj val2,
639 ScmObj val3);
640 SCM_EXTERN ScmObj Scm_Values5(ScmObj val0, ScmObj val1, ScmObj val2,
641 ScmObj val3, ScmObj val4);
642 SCM_EXTERN ScmObj Scm_ValuesFromArray(ScmObj *argv, ScmSmallInt argc);
643
644 /* CPS API for evaluating Scheme fragments on VM. */
645 SCM_EXTERN ScmObj Scm_VMApply(ScmObj proc, ScmObj args);
646 SCM_EXTERN ScmObj Scm_VMApply0(ScmObj proc);
647 SCM_EXTERN ScmObj Scm_VMApply1(ScmObj proc, ScmObj arg);
648 SCM_EXTERN ScmObj Scm_VMApply2(ScmObj proc, ScmObj arg1, ScmObj arg2);
649 SCM_EXTERN ScmObj Scm_VMApply3(ScmObj proc, ScmObj arg1, ScmObj arg2,
650 ScmObj arg3);
651 SCM_EXTERN ScmObj Scm_VMApply4(ScmObj proc, ScmObj arg1, ScmObj arg2,
652 ScmObj arg3, ScmObj arg4);
653 SCM_EXTERN ScmObj Scm_VMEval(ScmObj expr, ScmObj env);
654 SCM_EXTERN ScmObj Scm_VMCall(ScmObj *args, int argcnt, void *data);
655
656 SCM_EXTERN ScmObj Scm_VMCallCC(ScmObj proc);
657 SCM_EXTERN ScmObj Scm_VMCallPC(ScmObj proc);
658 SCM_EXTERN ScmObj Scm_VMReset(ScmObj proc);
659 SCM_EXTERN void Scm_VMPushDynamicHandlers(ScmObj, ScmObj, ScmObj);
660 SCM_EXTERN ScmObj Scm_VMDynamicWind(ScmObj pre, ScmObj body, ScmObj post);
661 SCM_EXTERN int Scm_ContinuationP(ScmObj proc);
662
663 SCM_EXTERN ScmObj Scm_VMCallWithContinuationPrompt(ScmObj, ScmObj, ScmObj);
664 SCM_EXTERN ScmObj Scm_VMAbortCurrentContinuation(ScmObj, ScmObj);
665
666 SCM_EXTERN ScmObj Scm_VMWithErrorHandler(ScmObj handler, ScmObj thunk);
667 SCM_EXTERN ScmObj Scm_VMWithGuardHandler(ScmObj handler, ScmObj thunk);
668 SCM_EXTERN ScmObj Scm_VMWithExceptionHandler(ScmObj handler, ScmObj thunk);
669 SCM_EXTERN ScmObj Scm_VMReraise();
670
671 /* Miscellaneous stuff */
672 SCM_EXTERN int Scm_VMGetNumResults(ScmVM *vm);
673 SCM_EXTERN ScmObj Scm_VMGetResult(ScmVM *vm);
674 SCM_EXTERN ScmObj Scm_VMGetStackLite(ScmVM *vm);
675 SCM_EXTERN ScmObj Scm_VMGetCallTraceLite(ScmVM *vm);
676 SCM_EXTERN ScmObj Scm_VMGetStack(ScmVM *vm);
677
678 /*---------------------------------------------------------
679 * BOX
680 */
681
682 #include <gauche/box.h>
683
684 /*---------------------------------------------------------
685 * CLASS AND TYPE
686 */
687
688 /* A type is a metaobject that defines/describes certain properties of
689 a group of objects. In Gauche, we have a few different kind of types.
690
691 CLASS - A class defines the structure and behavior of the group of
692 objects (instances). Every runtime object directly belongs to a class.
693 Classes are inheritable, so an instance can have is-a relationship to
694 more than one classes. The class structure is defined in gauche/class.h,
695 and mainly implemented in class.c.
696
697 DESCRIPTIVE TYPE - Represents type constraints. You can create a
698 descriptive type such as "this value should be either an integer or a
699 string". It can be used to validate a value, but cannot be used
700 to create an instance. This is implemented in libtype.scm.
701
702 PROXY TYPE - This is a special wrapper of classes, and usually it isn't
703 visible for the users. This is required because Gauche allows
704 classes to be redefined. See ScmProxyType defintion below.
705 */
706
707 #include <gauche/class.h>
708
709 /*--------------------------------------------------------
710 * COLLECTION INTERFACE
711 */
712
713 #include <gauche/collection.h>
714
715 /*--------------------------------------------------------
716 * CONNECTION INTERFACE
717 */
718
719 SCM_CLASS_DECL(Scm_ConnectionClass);
720 #define SCM_CLASS_CONNECTION (&Scm_ConnectionClass)
721
722 /*--------------------------------------------------------
723 * PAIR AND LIST
724 */
725
726 /* An ordinary pair uses two words. It can be distinguished from
727 * other heap allocated objects by checking the first word doesn't
728 * have "111" in the lower bits.
729 */
730 struct ScmPairRec {
731 ScmObj car; /* should be accessed via macros */
732 ScmObj cdr; /* ditto */
733 };
734
735 /* An extended pair behaves like an ordinary pair for read operations,
736 * but can keep extra information in attributes. It also has
737 * hidden field, and can behave differently on mutating operations.
738 * Immutable pairs are implemented on that mechanism.
739 * See priv/pairP.h for the real structure of an extended pair.
740 */
741 struct ScmExtendedPairRec {
742 ScmObj car; /* should be accessed via macros */
743 ScmObj cdr; /* ditto */
744 ScmObj attributes; /* should be accessed via API func. */
745 };
746
747 #if GAUCHE_CHECK_PAIR_ALIGNMENT
748 # define SCM_PAIRP(obj) (Scm_CheckingPairP(SCM_OBJ(obj)))
749 SCM_EXTERN int Scm_CheckingPairP(ScmObj);
750 #else
751 # define SCM_PAIRP(obj) \
752 (SCM_HPTRP(obj)&&(SCM_HTAG(obj)!=7||Scm_PairP(SCM_OBJ(obj))))
753 #endif
754
755 #define SCM_PAIR(obj) ((ScmPair*)(obj))
756 #define SCM_CAR(obj) (SCM_PAIR(obj)->car)
757 #define SCM_CDR(obj) (SCM_PAIR(obj)->cdr)
758 #define SCM_CAAR(obj) (SCM_CAR(SCM_CAR(obj)))
759 #define SCM_CADR(obj) (SCM_CAR(SCM_CDR(obj)))
760 #define SCM_CDAR(obj) (SCM_CDR(SCM_CAR(obj)))
761 #define SCM_CDDR(obj) (SCM_CDR(SCM_CDR(obj)))
762
763 #define SCM_SET_CAR(obj, value) Scm_SetCar(obj, value)
764 #define SCM_SET_CDR(obj, value) Scm_SetCdr(obj, value)
765
766 /* Use these only if you know OBJ is a mutable pair */
767 #define SCM_SET_CAR_UNCHECKED(obj, value) (SCM_CAR(obj) = (value))
768 #define SCM_SET_CDR_UNCHECKED(obj, value) (SCM_CDR(obj) = (value))
769
770 #if SIZEOF_INTPTR_T == 4
771 #define SCM_ODD_WORD_POINTER_P(p) (SCM_WORD(p) & 0x4)
772 #else /*SIZEOF_INTPTR_T == 8*/
773 #define SCM_ODD_WORD_POINTER_P(p) (SCM_WORD(p) & 0x8)
774 #endif
775
776 #if SCM_PAIR_ALWAYS_ALIGNED_EVEN_WORDS
777 #define SCM_EXTENDED_PAIR_P(obj) \
778 (SCM_ODD_WORD_POINTER_P(obj)&&SCM_PAIRP(obj))
779 #else /*!SCM_PAIR_ALWAYS_ALIGNED_EVEN_WORDS*/
780 #define SCM_EXTENDED_PAIR_P(obj) \
781 (SCM_ODD_WORD_POINTER_P(obj)&&SCM_PAIRP(obj)&&SCM_HOBJP(((ScmObj*)(obj))-1))
782 #endif /*!SCM_PAIR_ALWAYS_ALIGNED_EVEN_WORDS*/
783 #define SCM_EXTENDED_PAIR(obj) ((ScmExtendedPair*)(obj))
784
785
786 SCM_CLASS_DECL(Scm_ListClass);
787 SCM_CLASS_DECL(Scm_PairClass);
788 SCM_CLASS_DECL(Scm_NullClass);
789 #define SCM_CLASS_LIST (&Scm_ListClass)
790 #define SCM_CLASS_PAIR (&Scm_PairClass)
791 #define SCM_CLASS_NULL (&Scm_NullClass)
792
793 #define SCM_LISTP(obj) (SCM_NULLP(obj) || SCM_PAIRP(obj))
794
795 /* Useful macros to manipulate lists. */
796
797 #define SCM_FOR_EACH(p, list) \
798 for((p) = (list); SCM_PAIRP(p); (p) = SCM_CDR(p))
799
800 #define SCM_APPEND1(start, last, obj) \
801 do { \
802 if (SCM_NULLP(start)) { \
803 (start) = (last) = Scm_Cons((obj), SCM_NIL); \
804 } else { \
805 SCM_SET_CDR((last), Scm_Cons((obj), SCM_NIL)); \
806 (last) = SCM_CDR(last); \
807 } \
808 } while (0)
809
810 #define SCM_APPEND(start, last, obj) \
811 do { \
812 ScmObj list_SCM_GLS = (obj); \
813 if (SCM_NULLP(start)) { \
814 (start) = (list_SCM_GLS); \
815 if (!SCM_NULLP(list_SCM_GLS)) { \
816 (last) = Scm_LastPair(list_SCM_GLS); \
817 } \
818 } else { \
819 SCM_SET_CDR((last), (list_SCM_GLS)); \
820 (last) = Scm_LastPair(last); \
821 } \
822 } while (0)
823
824 #define SCM_LIST1(a) Scm_Cons(a, SCM_NIL)
825 #define SCM_LIST2(a,b) Scm_Cons(a, SCM_LIST1(b))
826 #define SCM_LIST3(a,b,c) Scm_Cons(a, SCM_LIST2(b, c))
827 #define SCM_LIST4(a,b,c,d) Scm_Cons(a, SCM_LIST3(b, c, d))
828 #define SCM_LIST5(a,b,c,d,e) Scm_Cons(a, SCM_LIST4(b, c, d, e))
829
830 /* special return value of Scm_Length */
831 enum {
832 SCM_LIST_DOTTED = -1, /* dotted list */
833 SCM_LIST_CIRCULAR = -2 /* circular list */
834 };
835
836 #define SCM_PROPER_LIST_P(obj) (Scm_Length(obj) >= 0)
837 #define SCM_DOTTED_LIST_P(obj) (Scm_Length(obj) == SCM_LIST_DOTTED)
838 #define SCM_CIRCULAR_LIST_P(obj) (Scm_Length(obj) == SCM_LIST_CIRCULAR)
839
840 SCM_EXTERN ScmObj Scm_Cons(ScmObj car, ScmObj cdr);
841 SCM_EXTERN ScmObj Scm_Acons(ScmObj caar, ScmObj cdar, ScmObj cdr);
842 SCM_EXTERN ScmObj Scm_MakeImmutablePair(ScmObj car, ScmObj cdr, ScmObj attrs);
843 SCM_EXTERN ScmObj Scm_List(ScmObj elt, ...);
844 SCM_EXTERN ScmObj Scm_Conses(ScmObj elt, ...);
845 SCM_EXTERN ScmObj Scm_VaList(va_list elts);
846 SCM_EXTERN ScmObj Scm_VaCons(va_list elts);
847 SCM_EXTERN ScmObj Scm_ArrayToList(ScmObj *elts, ScmSize nelts);
848 SCM_EXTERN ScmObj Scm_ArrayToListWithTail(ScmObj *elts, ScmSize nelts,
849 ScmObj tail);
850 SCM_EXTERN ScmObj *Scm_ListToArray(ScmObj list, ScmSize *nelts, ScmObj *store,
851 int alloc);
852
853 SCM_EXTERN ScmObj Scm_Car(ScmObj obj);
854 SCM_EXTERN ScmObj Scm_Cdr(ScmObj obj);
855 SCM_EXTERN ScmObj Scm_Caar(ScmObj obj);
856 SCM_EXTERN ScmObj Scm_Cadr(ScmObj obj);
857 SCM_EXTERN ScmObj Scm_Cdar(ScmObj obj);
858 SCM_EXTERN ScmObj Scm_Cddr(ScmObj obj);
859
860 SCM_EXTERN int Scm_ImmutablePairP(ScmObj obj);
861 SCM_EXTERN void Scm_SetCar(ScmObj pair, ScmObj value);
862 SCM_EXTERN void Scm_SetCdr(ScmObj pair, ScmObj value);
863
864 SCM_EXTERN ScmSize Scm_Length(ScmObj obj);
865 SCM_EXTERN ScmObj Scm_CopyList(ScmObj list);
866 SCM_EXTERN ScmObj Scm_MakeList(ScmSmallInt len, ScmObj fill);
867 SCM_EXTERN ScmObj Scm_AlistCopy(ScmObj alist);
868 SCM_EXTERN ScmObj Scm_Append2X(ScmObj list, ScmObj obj);
869 SCM_EXTERN ScmObj Scm_Append2(ScmObj list, ScmObj obj);
870 SCM_EXTERN ScmObj Scm_Append(ScmObj args);
871 SCM_EXTERN ScmObj Scm_ReverseX(ScmObj list);
872 SCM_EXTERN ScmObj Scm_Reverse(ScmObj list);
873 SCM_EXTERN ScmObj Scm_Reverse2X(ScmObj list, ScmObj tail);
874 SCM_EXTERN ScmObj Scm_Reverse2(ScmObj list, ScmObj tail);
875 SCM_EXTERN ScmObj Scm_ListTail(ScmObj list, ScmSmallInt i, ScmObj fallback);
876 SCM_EXTERN ScmObj Scm_ListRef(ScmObj list, ScmSmallInt i, ScmObj fallback);
877 SCM_EXTERN ScmObj Scm_LastPair(ScmObj list);
878
879 SCM_EXTERN ScmObj Scm_Memq(ScmObj obj, ScmObj list);
880 SCM_EXTERN ScmObj Scm_Memv(ScmObj obj, ScmObj list);
881 SCM_EXTERN ScmObj Scm_Member(ScmObj obj, ScmObj list, int cmpmode);
882 SCM_EXTERN ScmObj Scm_Assq(ScmObj obj, ScmObj alist);
883 SCM_EXTERN ScmObj Scm_Assv(ScmObj obj, ScmObj alist);
884 SCM_EXTERN ScmObj Scm_Assoc(ScmObj obj, ScmObj alist, int cmpmode);
885
886 SCM_EXTERN ScmObj Scm_Delete(ScmObj obj, ScmObj list, int cmpmode);
887 SCM_EXTERN ScmObj Scm_DeleteX(ScmObj obj, ScmObj list, int cmpmode);
888 SCM_EXTERN ScmObj Scm_AssocDelete(ScmObj elt, ScmObj alist, int cmpmode);
889 SCM_EXTERN ScmObj Scm_AssocDeleteX(ScmObj elt, ScmObj alist, int cmpmode);
890
891 SCM_EXTERN ScmObj Scm_DeleteDuplicates(ScmObj list, int cmpmode);
892 SCM_EXTERN ScmObj Scm_DeleteDuplicatesX(ScmObj list, int cmpmode);
893
894 SCM_EXTERN ScmObj Scm_MakeExtendedPair(ScmObj car, ScmObj cdr, ScmObj attrs);
895 SCM_EXTERN ScmObj Scm_ExtendedCons(ScmObj car, ScmObj cdr);
896 SCM_EXTERN ScmObj Scm_PairAttr(ScmPair *pair);
897 SCM_EXTERN ScmObj Scm_PairAttrGet(ScmPair *pair, ScmObj key, ScmObj fallback);
898 SCM_EXTERN ScmObj Scm_PairAttrSet(ScmPair *pair, ScmObj key, ScmObj value);
899
900 #if GAUCHE_API_VERSION >= 98
901 SCM_EXTERN ScmObj Scm_MonotonicMerge(ScmObj sequences);
902 #define Scm_MonotonicMerge1(x) Scm_MonotonicMerge(x)
903 #else /* GAUCHE_API_VERSION < 98 */
904 SCM_EXTERN ScmObj Scm_MonotonicMerge(ScmObj start, ScmObj sequences);
905 SCM_EXTERN ScmObj Scm_MonotonicMerge1(ScmObj sequences);
906 #endif /* GAUCHE_API_VERSION < 98 */
907
908 /*--------------------------------------------------------
909 * CHARACTERS
910 */
911
912 /* OBSOLETED */
913 /* This kind of thing is now handled by string-incomplete->complete
914 in libstr.scm. */
915 typedef enum {
916 SCM_ILLEGAL_CHAR_REJECT, /* Refuse to handle illegal chars. For ports
917 this means raising an error. For string
918 conversion procedure, this makes it to
919 return #f. */
920 SCM_ILLEGAL_CHAR_OMIT, /* Silently discard the illegal chars. */
921 SCM_ILLEGAL_CHAR_REPLACE /* Replace an illegal char to a substitute
922 char, specified elsewhere. */
923 } ScmIllegalCharHandling;
924
925
926 /*--------------------------------------------------------
927 * STRING
928 */
929
930 #include <gauche/string.h>
931
932 /*--------------------------------------------------------
933 * VECTOR
934 */
935
936 #include <gauche/vector.h>
937
938 /*--------------------------------------------------------
939 * PORT
940 */
941
942 #include <gauche/port.h>
943
944
945 /*--------------------------------------------------------
946 * WRITE
947 */
948
949 #include <gauche/writer.h>
950
951 /*---------------------------------------------------------
952 * READ
953 */
954
955 #include <gauche/reader.h>
956
957 /*--------------------------------------------------------
958 * HASHTABLE
959 */
960
961 #include <gauche/hash.h>
962
963 /*--------------------------------------------------------
964 * TREEMAP
965 */
966
967 #include <gauche/treemap.h>
968
969 /*--------------------------------------------------------
970 * WEAK VECTOR, WEAK BOX & WEAK HASH TABLES
971 */
972
973 #include <gauche/weak.h>
974
975 /*--------------------------------------------------------
976 * CHAR-SET
977 */
978
979 #include <gauche/charset.h>
980
981 /*--------------------------------------------------------
982 * MODULE
983 */
984
985 #include <gauche/module.h>
986
987 /*--------------------------------------------------------
988 * SYMBOL
989 */
990
991 #include <gauche/symbol.h>
992
993 /*--------------------------------------------------------
994 * GLOC
995 */
996
997 #include <gauche/gloc.h>
998
999 /*--------------------------------------------------------
1000 * NUMBER
1001 */
1002
1003 #include <gauche/number.h>
1004
1005 /*--------------------------------------------------------
1006 * PROCEDURE (APPLICABLE OBJECT)
1007 */
1008
1009
1010 typedef ScmObj (*ScmTransformerProc)(ScmObj self, ScmObj form, ScmObj env,
1011 void *data);
1012
1013 /* Base structure */
1014 struct ScmProcedureRec {
1015 SCM_INSTANCE_HEADER;
1016 unsigned int required : 16; /* # of required args */
1017 unsigned int optional : 8; /* >=1 if it takes opt args. see below.*/
1018 unsigned int type : 3; /* ScmProcedureType */
1019 unsigned int locked : 1; /* setter locked? (see below) */
1020 unsigned int currying : 1; /* autocurrying */
1021 unsigned int constant : 1; /* constant procedure. see below. */
1022 unsigned int leaf : 1; /* leaf procedure/method */
1023 unsigned int reserved : 1; /* unused yet. */
1024 #if GAUCHE_API_VERSION >= 98
1025 unsigned int reserved32 : 32; /* unused yet. */
1026 #endif /*GAUCHE_API_VERSION >= 98*/
1027 ScmObj info; /* source code info (see below) */
1028 ScmObj setter; /* setter, if exists. */
1029 ScmObj inliner; /* inliner information (see below) */
1030 #if GAUCHE_API_VERSION >= 98
1031 ScmObj typeHint; /* info to be used for type checking.
1032 shouldn't be accessed directly, for
1033 we do some tricky stuff here.
1034 Use Scheme API procedure-type
1035 to get the type info.
1036 */
1037 ScmObj tagsAlist; /* Alist of procedure tags. Procedure
1038 tags can carry extra info about
1039 the procedure. They should be treated
1040 as immutable property, for procedures
1041 can be duplicated/consolidated for
1042 optimizations and other runtime handlings.
1043 NB: The value of a tag can have a
1044 mutable structure.
1045 SRFI-229 procedure tag is held as
1046 a value corresponds to 'srfi-229-tag.
1047 This shouldn't be accessed directly;
1048 use API instead.
1049 */
1050 #endif /*GAUCHE_API_VERSION >= 98*/
1051 };
1052
1053 /* About locked slot:
1054 For <procedure> and <generic>, it shows whether the setter is locked.
1055 For <method>, it shows whether the alteration of the method is disallowed,
1056 i.e. one can't redefine a method with matching signature.
1057 (These two roles are reflected to the two macros,
1058 SCM_PROCEDURE_SETTER_LOCKED and SCM_PROCEDURE_METHOD_LOCKED)
1059 TODO: When we change ABI, maybe split these roles to different flags.
1060 */
1061
1062 /* About optional slot:
1063 If this slot is non-zero, the procedure takes optional arguments.
1064 For Standard Scheme procedures with 'rest' arguments, this slot is 1
1065 and all excessive arguments are 'folded' in a list.
1066
1067 This slot may have a value more than 1. If it is N (>1), then up to N-1
1068 optional arguments are passed without being folded (that is, passed
1069 'on the stack'. Only when the given argument is more than or equal to
1070 N + reqargs, the excessive arguments are folded and passed in a list.
1071 Thus, such procedure may get between reqargs values and N+reqargs values
1072 after folding (NB: Fixed argument procedure always get regargs values,
1073 and standard Scheme variable argument procedure always get reqargs+1 values
1074 after argument folding).
1075
1076 This special treatment is to avoid unnecessary consing of argumets;
1077 if we know the callee immediately unfolds the rest argument, it's no
1078 use to fold excessive arguments anyway.
1079 */
1080
1081 /* About 'constant' flag:
1082
1083 For a <procedure> and <method>, this flag being TRUE means it returns
1084 the same constant value if given same constant arguments, and it does
1085 not have any other external effects. The compiler may use this info
1086 to replace a call of this proc with the resulting value,
1087 if all the arguments are known at compile-time.
1088 The resulting value must be serializable to the
1089 precompiled file. The result shouldn't be affected
1090 by the timing of the compile, architecture on which the compiler runs,
1091 or the compiler configuration (e.g. internal encoding).
1092
1093 If <generic> has this flag, it tells the compiler that it can calculate
1094 applicable method at the compile time. It is independent from method's
1095 constantness---the selected method may or may not be used as a compile-time
1096 calculation; but it is safe to pre-select that method, given that
1097 enough information is available at the compile time.
1098 We warn if a new method is added to a 'constant' generic.
1099 */
1100
1101 /* About 'leaf' flag:
1102 For METHOD, this flag indicates the method doesn't refer to next-method
1103 argument at all, so we can skip creating next-method instance when
1104 making a call.
1105 For CLOSURE, we *plan* to use this to indicate the closure body doesn't
1106 make a call to another procedures, to allow certain optimizations.
1107 */
1108
1109 /* About 'info' slot:
1110 This is a sort of the kitchen sink slot, keeping whatever miscellaneous
1111 information as our implementation evolves. Since this can be a part of
1112 statically allocated structure, we can't change its format in a way
1113 that breaks the backward compatibility.
1114
1115 SUBR, CLOSURE:
1116 This slot may contain one of this:
1117 - Signature: For example, the subr `cons' has (cons obj1 obj2)
1118 in it. The first pair may have the following pair attributes.
1119
1120 `source-info' (<filename> <lineno>)
1121 The source location the procedure is defined, if known.
1122 This info can be retrieved with (source-location PROC).
1123 `bind-info' (<module-name> <var-name>)
1124 The proc is bound to <var-name> in a module named
1125 <module-name>, and it's inlinable binding. When the
1126 compiler can pre-calculate the proc to be called in a
1127 code, it can replace the original code with a global
1128 variable reference to <var-name>. (We can't directly
1129 insert reference to the proc, for it may not be
1130 serializable for AOT compilation).
1131
1132 - A <primitive-parameter> or <parameter> object. R7RS requires
1133 parameters to be a procedure, responding #t to procedure?.
1134 We need to adapt Gauche parameter into that, saving the
1135 actual parameter instance here.
1136
1137 - Subr's name, as a string or a symbol. This is the old format.
1138 It may also the case that subr is created from C function
1139 Scm_MakeSubr(), for it's cumbersome in C routine to construct
1140 the signature list. Accept it, but not recommended to use
1141 this format in the new code.
1142 - #f. Indicates there's no useful info.
1143
1144 GENERIC:
1145 This slot contains the "name" of the gf, which is a symbol.
1146 A kludge: For setter gf, which can be created indirectly
1147 via (define-method (setter GF) ...), we use a weird name
1148 |setter of GF|. This is a quick hack to make it work, but ideally
1149 we should accept a list (setter GF) as the name. Anticipate
1150 this change in future.
1151 Furthermore, in order to hold source-info, we might just make
1152 it a pair, e.g. (NAME) or ((setter NAME)).
1153
1154 METHOD:
1155 This slot contains (<name> <specializer> ...),
1156 where <name> is the name of the generic function, and
1157 <specializer>s are the name of classes.
1158
1159 NEXT_METHOD:
1160 This slot isn't used.
1161 */
1162
1163 /* About procedure inliner:
1164 This slot holds information to inline procedures. The value of this slot
1165 can be one of the following kinds:
1166
1167 #f: No inliner associated to this procedure. (For historical
1168 reasons, the code that access to this slot expects this slot can be
1169 NULL and treats it as SCM_FALSE in that case)
1170
1171 <integer>: Only appears in some built-in procedures, and specifies
1172 the VM instruction number. This should be considered as a special
1173 hack. The set of procedures that can have this type of inliner
1174 is tied to the VM definition.
1175
1176 <vector>: Procedures defined with define-inline have this. The vector
1177 encodes intermediate form (IForm) of the procedure code, which will be
1178 expanded into the caller.
1179
1180 <macro>: A compiler macro. The macro expander is invoked with the
1181 original source and macro-use environment, just like the ordinary macro
1182 call. The expander must return an Sexpr. If the expander returns
1183 the input as is, it indicates expansion is not possible and the form
1184 is compiled as the ordinary procedure call.
1185
1186 <procedure>: A procedural inliner. It has signature Sexpr,[IForm] -> IForm,
1187 where Sexpr is the original source of call site (just for debug info) and
1188 input [IForm] is the IForm for list of arguments. See compiler-1.scm.
1189 It returns the modified IForm. It can return #<undef>, to indicate
1190 inlining isn't possible.
1191 */
1192
1193 /* procedure type */
1194 enum ScmProcedureType {
1195 SCM_PROC_SUBR,
1196 SCM_PROC_CLOSURE,
1197 SCM_PROC_GENERIC,
1198 SCM_PROC_METHOD,
1199 SCM_PROC_NEXT_METHOD
1200 };
1201
1202 #define SCM_PROCEDURE(obj) ((ScmProcedure*)(obj))
1203 #define SCM_PROCEDURE_REQUIRED(obj) SCM_PROCEDURE(obj)->required
1204 #define SCM_PROCEDURE_OPTIONAL(obj) SCM_PROCEDURE(obj)->optional
1205 #define SCM_PROCEDURE_TYPE(obj) SCM_PROCEDURE(obj)->type
1206 #define SCM_PROCEDURE_CONSTANT(obj) SCM_PROCEDURE(obj)->constant
1207 #define SCM_PROCEDURE_CURRYING(obj) SCM_PROCEDURE(obj)->currying
1208 #define SCM_PROCEDURE_INFO(obj) SCM_PROCEDURE(obj)->info
1209 #define SCM_PROCEDURE_SETTER(obj) SCM_PROCEDURE(obj)->setter
1210 #define SCM_PROCEDURE_INLINER(obj) SCM_PROCEDURE(obj)->inliner
1211 #define SCM_PROCEDURE_SETTER_LOCKED(obj) SCM_PROCEDURE(obj)->locked
1212 #define SCM_PROCEDURE_LEAF(obj) SCM_PROCEDURE(obj)->leaf
1213
1214 SCM_CLASS_DECL(Scm_ProcedureClass);
1215 #define SCM_CLASS_PROCEDURE (&Scm_ProcedureClass)
1216 #define SCM_PROCEDUREP(obj) \
1217 (SCM_HOBJP(obj) && SCM_CLASS_APPLICABLE_P(SCM_CLASS_OF(obj)))
1218 #define SCM_PROCEDURE_TAKE_NARG_P(obj, narg) \
1219 (SCM_PROCEDUREP(obj)&& \
1220 ( (!SCM_PROCEDURE_OPTIONAL(obj)&&SCM_PROCEDURE_REQUIRED(obj)==(narg)) \
1221 ||(SCM_PROCEDURE_OPTIONAL(obj)&&SCM_PROCEDURE_REQUIRED(obj)<=(narg))))
1222 #define SCM_PROCEDURE_THUNK_P(obj) \
1223 (SCM_PROCEDUREP(obj)&& \
1224 ( (!SCM_PROCEDURE_OPTIONAL(obj)&&SCM_PROCEDURE_REQUIRED(obj)==0) \
1225 ||(SCM_PROCEDURE_OPTIONAL(obj))))
1226
1227 /* This is internal - should never be used directly */
1228 #if GAUCHE_API_VERSION >= 98
1229 #define SCM__PROCEDURE_INITIALIZER(klass, req, opt, typ, cst, lef, inf, inl) \
1230 { { klass, NULL }, (req), (opt), (typ), FALSE, FALSE, cst, lef, 0, 0, \
1231 (inf), SCM_FALSE, (inl), SCM_FALSE, SCM_NIL }
1232 #else /* GAUCHE_API_VERSION < 98 */
1233 #define SCM__PROCEDURE_INITIALIZER(klass, req, opt, typ, cst, lef, inf, inl) \
1234 { { klass, NULL }, (req), (opt), (typ), FALSE, FALSE, cst, lef, 0, \
1235 (inf), SCM_FALSE, (inl) }
1236 #endif /* GAUCHE_API_VERSION < 98 */
1237
1238 SCM_EXTERN ScmObj Scm_CurryProcedure(ScmObj proc, ScmObj *given,
1239 int ngiven, int foldlen);
1240
1241 /* Closure - Scheme defined procedure */
1242 struct ScmClosureRec {
1243 ScmProcedure common;
1244 ScmObj code; /* compiled code */
1245 ScmEnvFrame *env; /* environment */
1246 };
1247
1248 #define SCM_CLOSUREP(obj) \
1249 (SCM_PROCEDUREP(obj)&&(SCM_PROCEDURE_TYPE(obj)==SCM_PROC_CLOSURE))
1250 #define SCM_CLOSURE(obj) ((ScmClosure*)(obj))
1251 #define SCM_CLOSURE_CODE(obj) SCM_CLOSURE(obj)->code
1252 #define SCM_CLOSURE_ENV(obj) SCM_CLOSURE(obj)->env
1253
1254 SCM_EXTERN ScmObj Scm_MakeClosure(ScmObj code, ScmEnvFrame *env);
1255
1256 /* Subr - C defined procedure */
1257 struct ScmSubrRec {
1258 ScmProcedure common;
1259 int flags;
1260 ScmSubrProc *func;
1261 void *data;
1262 };
1263
1264 #define SCM_SUBRP(obj) \
1265 (SCM_PROCEDUREP(obj)&&(SCM_PROCEDURE_TYPE(obj)==SCM_PROC_SUBR))
1266 #define SCM_SUBR(obj) ((ScmSubr*)(obj))
1267 #define SCM_SUBR_FLAGS(obj) SCM_SUBR(obj)->flags
1268 #define SCM_SUBR_FUNC(obj) SCM_SUBR(obj)->func
1269 #define SCM_SUBR_DATA(obj) SCM_SUBR(obj)->data
1270
1271 /* flags */
1272 #define SCM_SUBR_IMMEDIATE_ARG (1L<<0) /* This subr will not retain a reference
1273 to the flonums given to args. VM
1274 can safely pass the register flonums
1275 to the subr. This is added when
1276 the :fast-flonum flag is given to
1277 define-cproc. */
1278
1279 #define SCM__DEFINE_SUBR_INT(cvar, req, opt, cst, inf, flags, func, inliner, data) \
1280 ScmSubr cvar = { \
1281 SCM__PROCEDURE_INITIALIZER(SCM_CLASS_STATIC_TAG(Scm_ProcedureClass),\
1282 req, opt, SCM_PROC_SUBR, cst, 0, inf, inliner), \
1283 flags, (func), (data) \
1284 }
1285
1286 #define SCM_DEFINE_SUBR(cvar, req, opt, inf, func, inliner, data) \
1287 SCM__DEFINE_SUBR_INT(cvar, req, opt, 0, inf, 0, func, inliner, data)
1288 #define SCM_DEFINE_SUBRX(cvar, req, opt, cst, inf, flags, func, inliner, data) \
1289 SCM__DEFINE_SUBR_INT(cvar, req, opt, cst, inf, flags, func, inliner, data)
1290
1291 SCM_EXTERN ScmObj Scm_MakeSubr(ScmSubrProc *func,
1292 void *data,
1293 int required, int optional,
1294 ScmObj info);
1295 SCM_EXTERN ScmObj Scm_NullProc(void);
1296
1297 SCM_EXTERN ScmObj Scm_SetterSet(ScmProcedure *proc, ScmProcedure *setter,
1298 int lock);
1299 SCM_EXTERN ScmObj Scm_Setter(ScmObj proc);
1300 SCM_EXTERN int Scm_HasSetter(ScmObj proc);
1301
1302 /* Generic - Generic function */
1303 struct ScmGenericRec {
1304 ScmProcedure common;
1305 ScmObj methods; /* list of methods */
1306 int maxReqargs; /* maximum # of args required to select
1307 applicable methods */
1308 ScmObj (*fallback)(ScmObj *argv, int argc, ScmGeneric *gf);
1309 void *dispatcher;
1310 void *data;
1311 ScmInternalMutex lock;
1312 };
1313
1314 SCM_CLASS_DECL(Scm_GenericClass);
1315 #define SCM_CLASS_GENERIC (&Scm_GenericClass)
1316 #define SCM_GENERICP(obj) SCM_XTYPEP(obj, SCM_CLASS_GENERIC)
1317 #define SCM_GENERIC(obj) ((ScmGeneric*)obj)
1318 #define SCM_GENERIC_DATA(obj) (SCM_GENERIC(obj)->data)
1319
1320 /* we share 'constant' flag for sealed generic */
1321 #define SCM_GENERIC_SEALED_P(obj) SCM_PROCEDURE_CONSTANT(obj)
1322
1323 #define SCM_DEFINE_GENERIC(cvar, cfunc, data) \
1324 ScmGeneric cvar = { \
1325 SCM__PROCEDURE_INITIALIZER(SCM_CLASS_STATIC_TAG(Scm_GenericClass),\
1326 0, 0, SCM_PROC_GENERIC, 0, 0, \
1327 SCM_FALSE, NULL), \
1328 SCM_NIL, 0, cfunc, NULL, data, \
1329 SCM_INTERNAL_MUTEX_INITIALIZER \
1330 }
1331
1332 SCM_EXTERN void Scm_InitBuiltinGeneric(ScmGeneric *gf, const char *name,
1333 ScmModule *mod);
1334 SCM_EXTERN ScmObj Scm_MakeBaseGeneric(ScmObj name,
1335 ScmObj (*fallback)(ScmObj *, int, ScmGeneric*),
1336 void *data);
1337 SCM_EXTERN ScmObj Scm_NoNextMethod(ScmObj *argv, int argc, ScmGeneric *gf);
1338 SCM_EXTERN ScmObj Scm_NoOperation(ScmObj *argv, int argc, ScmGeneric *gf);
1339 SCM_EXTERN ScmObj Scm_InvalidApply(ScmObj *argv, int argc, ScmGeneric *gf);
1340
1341 /* Method - method
1342 A method can be defined either by C or by Scheme. C-defined method
1343 have func ptr, with optional data. Scheme-define method has NULL
1344 in func, code in data, and optional environment in env. */
1345 struct ScmMethodRec {
1346 ScmProcedure common;
1347 ScmGeneric *generic;
1348 ScmClass **specializers; /* array of specializers, size==required */
1349 ScmObj (*func)(ScmNextMethod *nm, ScmObj *argv, int argc, void * data);
1350 void *data; /* closure, or code */
1351 ScmEnvFrame *env; /* environment (for Scheme created method) */
1352 };
1353
1354 SCM_CLASS_DECL(Scm_MethodClass);
1355 #define SCM_CLASS_METHOD (&Scm_MethodClass)
1356 #define SCM_METHODP(obj) SCM_ISA(obj, SCM_CLASS_METHOD)
1357 #define SCM_METHOD(obj) ((ScmMethod*)obj)
1358 #define SCM_METHOD_LOCKED(obj) SCM_METHOD(obj)->common.locked
1359 #define SCM_METHOD_LEAF_P(obj) SCM_METHOD(obj)->common.leaf
1360
1361 #define SCM_DEFINE_METHOD(cvar, gf, req, opt, specs, func, data) \
1362 ScmMethod cvar = { \
1363 SCM__PROCEDURE_INITIALIZER(SCM_CLASS_STATIC_TAG(Scm_MethodClass),\
1364 req, opt, SCM_PROC_METHOD, 0, 0, \
1365 SCM_FALSE, NULL), \
1366 gf, specs, func, data, NULL \
1367 }
1368
1369 SCM_EXTERN void Scm_InitBuiltinMethod(ScmMethod *m);
1370
1371 /* Next method object
1372 Next method is just another callable entity, with memoizing
1373 the arguments. */
1374 struct ScmNextMethodRec {
1375 ScmProcedure common;
1376 ScmGeneric *generic;
1377 ScmObj methods; /* list of applicable methods */
1378 ScmObj *argv; /* original arguments */
1379 int argc; /* # of original arguments */
1380 int applyargs; /* if TRUE, argv[argc-1] has a list of rest args */
1381 };
1382
1383 SCM_CLASS_DECL(Scm_NextMethodClass);
1384 #define SCM_CLASS_NEXT_METHOD (&Scm_NextMethodClass)
1385 #define SCM_NEXT_METHODP(obj) SCM_XTYPEP(obj, SCM_CLASS_NEXT_METHOD)
1386 #define SCM_NEXT_METHOD(obj) ((ScmNextMethod*)obj)
1387
1388 /* Calling a Scheme function from C
1389 *
1390 * static ScmObj proc = SCM_UNDEFINED;
1391 *
1392 * SCM_BIND_PROC(proc, "scheme-proc-name", module);
1393 *
1394 * Scm_ApplyRec(proc, args);
1395 * or
1396 * Scm_Apply(proc, args, &result);
1397 *
1398 * SCM_BIND_PROC macro initializes the C variable proc to the value of
1399 * the global Scheme variable scheme-proc-name in the module.
1400 * It is idempotent operation, so it's MT-safe.
1401 */
1402 #define SCM_BIND_PROC(var, name, module) \
1403 do { \
1404 if (SCM_UNDEFINEDP(var)) { \
1405 ScmObj v__ = \
1406 Scm_GlobalVariableRef(module, \
1407 SCM_SYMBOL(SCM_INTERN(name)), \
1408 0); \
1409 if (SCM_UNBOUNDP(v__)) { \
1410 Scm_Error("Procedure %s is unbound", name); \
1411 } \
1412 var = v__; \
1413 } \
1414 } while (0)
1415
1416
1417 /*--------------------------------------------------------
1418 * MACROS AND SYNTAX
1419 */
1420
1421 /* The actual definitions of ScmSyntax and ScmMacro are private.*/
1422
1423 #define SCM_SYNTAX(obj) ((ScmSyntax*)(obj))
1424 #define SCM_SYNTAXP(obj) SCM_XTYPEP(obj, SCM_CLASS_SYNTAX)
1425 SCM_CLASS_DECL(Scm_SyntaxClass);
1426 #define SCM_CLASS_SYNTAX (&Scm_SyntaxClass)
1427
1428 #define SCM_MACRO(obj) ((ScmMacro*)(obj))
1429 #define SCM_MACROP(obj) SCM_XTYPEP(obj, SCM_CLASS_MACRO)
1430 SCM_CLASS_DECL(Scm_MacroClass);
1431 #define SCM_CLASS_MACRO (&Scm_MacroClass)
1432
1433 SCM_EXTERN ScmObj Scm_MakeMacro(ScmObj name,
1434 ScmObj transformer,
1435 ScmObj info, /* alist */
1436 u_long flags);
1437 SCM_EXTERN ScmObj Scm_MacroTransformer(ScmMacro *mac);
1438 SCM_EXTERN ScmObj Scm_MacroName(ScmMacro *mac);
1439
1440 SCM_EXTERN ScmObj Scm_MakeMacroAutoload(ScmSymbol *name,
1441 ScmAutoload *al);
1442
1443 #if GAUCHE_API_VERSION >= 98
1444 SCM_EXTERN ScmObj Scm_UnwrapSyntax(ScmObj form, int immutablep);
1445 #define Scm_UnwrapSyntax2(form, imm) Scm_UnwrapSyntax(form, imm)
1446 #else /* GAUCHE_API_VERSION < 98 */
1447 SCM_EXTERN ScmObj Scm_UnwrapSyntax(ScmObj form);
1448 SCM_EXTERN ScmObj Scm_UnwrapSyntax2(ScmObj form, int immutablep);
1449 #endif /* GAUCHE_API_VERSION < 98 */
1450
1451 /*--------------------------------------------------------
1452 * PROMISE
1453 */
1454
1455 struct ScmPromiseRec {
1456 SCM_HEADER;
1457 ScmObj kind; /* promise kind */
1458 struct ScmPromiseContentRec *content; /* opaque */
1459 };
1460
1461 SCM_CLASS_DECL(Scm_PromiseClass);
1462 #define SCM_CLASS_PROMISE (&Scm_PromiseClass)
1463 #define SCM_PROMISE(obj) ((ScmPromise*)(obj))
1464 #define SCM_PROMISEP(obj) SCM_XTYPEP(obj, SCM_CLASS_PROMISE)
1465
1466 SCM_EXTERN ScmObj Scm_VMForce(ScmObj p); /* CPS, lightweight */
1467 SCM_EXTERN ScmObj Scm_Force(ScmObj p);
1468
1469 /* Lazy pair structure is opaque to public. Whenever you apply to an
1470 ScmObj SCM_PAIRP, a lazy pair morphs itself to a pair, so the normal
1471 code never see lazy pairs. */
1472
1473 SCM_CLASS_DECL(Scm_LazyPairClass);
1474 #define SCM_CLASS_LAZY_PAIR (&Scm_LazyPairClass)
1475 #define SCM_LAZY_PAIR(obj) ((ScmLazyPair*)(obj))
1476 #define SCM_LAZY_PAIR_P(obj) SCM_XTYPEP(obj, SCM_CLASS_LAZY_PAIR)
1477
1478 SCM_EXTERN ScmObj Scm_MakeLazyPair(ScmObj item, ScmObj generator, ScmObj attrs);
1479 SCM_EXTERN ScmObj Scm_LazyCons(ScmObj item, ScmObj thunk, ScmObj attrs);
1480 SCM_EXTERN ScmObj Scm_GeneratorToLazyPair(ScmObj generator);
1481 SCM_EXTERN ScmObj Scm_ForceLazyPair(volatile ScmLazyPair *lp);
1482 SCM_EXTERN int Scm_PairP(ScmObj x);
1483
1484 /*--------------------------------------------------------
1485 * condition
1486 */
1487
1488 /* Condition classes are defined in a separate file */
1489 #include <gauche/exception.h>
1490
1491 /* 'reason' flag for Scm_PortError */
1492 enum {
1493 SCM_PORT_ERROR_INPUT,
1494 SCM_PORT_ERROR_OUTPUT,
1495 SCM_PORT_ERROR_CLOSED,
1496 SCM_PORT_ERROR_UNIT,
1497 SCM_PORT_ERROR_DECODING,
1498 SCM_PORT_ERROR_ENCODING,
1499 SCM_PORT_ERROR_SEEK,
1500 SCM_PORT_ERROR_INVALID_POSITION,
1501 SCM_PORT_ERROR_OTHER
1502 };
1503
1504 /* Throwing error */
1505 SCM_EXTERN void Scm_Error(const char *msg, ...) SCM_NORETURN;
1506 SCM_EXTERN void Scm_SysError(const char *msg, ...) SCM_NORETURN;
1507 SCM_EXTERN void Scm_TypeError(const char *what,
1508 const char *expected, ScmObj got) SCM_NORETURN;
1509 SCM_EXTERN void Scm_PortError(ScmPort *port, int reason,
1510 const char *msg, ...) SCM_NORETURN;
1511 SCM_EXTERN void Scm_PortErrorWithAux(ScmPort *port, int reason,
1512 ScmObj auxinfo,
1513 const char *msg, ...) SCM_NORETURN;
1514
1515 /* common pattern */
1516 #define SCM_TYPE_ERROR(arg, expected) Scm_TypeError(#arg, expected, arg)
1517
1518 SCM_EXTERN void Scm_Warn(const char *msg, ...);
1519 SCM_EXTERN void Scm_FWarn(ScmString *fmt, ScmObj args);
1520
1521 SCM_EXTERN ScmObj Scm_Raise(ScmObj exception, u_long flags);
1522
1523 /* flags for Scm_Raise */
1524 enum {
1525 SCM_RAISE_NON_CONTINUABLE = (1L<<0)
1526 };
1527
1528 SCM_EXTERN ScmObj Scm_RaiseCondition(ScmObj conditionType, ...);
1529
1530 /* A marker to insert between key-value pair and formatting string
1531 in Scm_RaiseCondition. */
1532 #define SCM_RAISE_CONDITION_MESSAGE ((const char *)1)
1533
1534 SCM_EXTERN int Scm_ConditionHasType(ScmObj c, ScmObj k);
1535 SCM_EXTERN ScmObj Scm_ConditionMessage(ScmObj c);
1536 SCM_EXTERN ScmObj Scm_ConditionTypeName(ScmObj c);
1537
1538 enum {
1539 /* predefined stack trace formats. EXPERIMENTAL. */
1540 SCM_STACK_TRACE_FORMAT_ORIGINAL, /* original format */
1541 SCM_STACK_TRACE_FORMAT_CC /* compiler-message-like format */
1542 };
1543
1544 SCM_EXTERN void Scm_ShowStackTrace(ScmPort *out, ScmObj stacklite,
1545 int maxdepth, int skip, int offset,
1546 int format);
1547
1548 SCM_EXTERN void Scm_SetCallTraceSize(u_long size);
1549
1550 SCM_EXTERN ScmObj Scm_ReportError(ScmObj e, ScmObj out);
1551
1552 /*--------------------------------------------------------
1553 * REGEXP
1554 */
1555
1556 /* The definition of Scm_RegexpRec and Scm_RegeMatchRec is hidden
1557 in gauche/priv/regexpP.h */
1558
1559 SCM_CLASS_DECL(Scm_RegexpClass);
1560 #define SCM_CLASS_REGEXP (&Scm_RegexpClass)
1561 #define SCM_REGEXP(obj) ((ScmRegexp*)obj)
1562 #define SCM_REGEXPP(obj) SCM_XTYPEP(obj, SCM_CLASS_REGEXP)
1563
1564 /* flags */
1565 #define SCM_REGEXP_CASE_FOLD (1L<<0)
1566 #define SCM_REGEXP_PARSE_ONLY (1L<<1)
1567 /* bits 2 and 3 are used internally */
1568 #define SCM_REGEXP_MULTI_LINE (1L<<4)
1569
1570 SCM_EXTERN ScmObj Scm_RegComp(ScmString *pattern, int flags);
1571 #if GAUCHE_API_VERSION >= 98
1572 SCM_EXTERN ScmObj Scm_RegCompFromAST(ScmObj ast, int flags);
1573 #define Scm_RegCompFromAST2(a,b) Scm_RegCompFromAST(a,b)
1574 #else /* GAUCHE_API_VERSION < 98 */
1575 SCM_EXTERN ScmObj Scm_RegCompFromAST(ScmObj ast);
1576 SCM_EXTERN ScmObj Scm_RegCompFromAST2(ScmObj ast, int flags);
1577 #endif /* GAUCHE_API_VERSION < 98 */
1578 SCM_EXTERN ScmObj Scm_RegOptimizeAST(ScmObj ast);
1579 SCM_EXTERN ScmObj Scm_RegExec(ScmRegexp *rx, ScmString *input, ScmObj start, ScmObj end);
1580 SCM_EXTERN void Scm_RegDump(ScmRegexp *rx);
1581
1582 SCM_CLASS_DECL(Scm_RegMatchClass);
1583 #define SCM_CLASS_REGMATCH (&Scm_RegMatchClass)
1584 #define SCM_REGMATCH(obj) ((ScmRegMatch*)obj)
1585 #define SCM_REGMATCHP(obj) SCM_XTYPEP(obj, SCM_CLASS_REGMATCH)
1586
1587 SCM_EXTERN ScmObj Scm_RegMatchSubstr(ScmRegMatch *rm, ScmObj obj);
1588 SCM_EXTERN ScmObj Scm_RegMatchStart(ScmRegMatch *rm, ScmObj obj);
1589 SCM_EXTERN ScmObj Scm_RegMatchEnd(ScmRegMatch *rm, ScmObj obj);
1590 SCM_EXTERN ScmObj Scm_RegMatchAfter(ScmRegMatch *rm, ScmObj obj);
1591 SCM_EXTERN ScmObj Scm_RegMatchBefore(ScmRegMatch *rm, ScmObj obj);
1592 SCM_EXTERN void Scm_RegMatchDump(ScmRegMatch *match);
1593
1594 /*-------------------------------------------------------
1595 * STUB MACROS
1596 */
1597 #define SCM_ENTER_SUBR(name)
1598
1599 #define SCM_ARGREF(count) (SCM_FP[count])
1600 #define SCM_RETURN(value) return value
1601 #define SCM_CURRENT_MODULE() (Scm_VM()->module)
1602 #define SCM_VOID_RETURN_VALUE(expr) ((void)(expr), SCM_UNDEFINED)
1603
1604 #define SCM_MAYBE_P(pred, obj) (SCM_FALSEP(obj)||(pred(obj)))
1605 #define SCM_MAYBE(unboxer, obj) (SCM_FALSEP(obj)?NULL:(unboxer(obj)))
1606 #define SCM_MAKE_MAYBE(boxer, obj) ((obj)?(boxer(obj)):SCM_FALSE)
1607
1608 /*---------------------------------------------------
1609 * SIGNAL
1610 */
1611
1612 typedef struct ScmSysSigsetRec {
1613 SCM_HEADER;
1614 sigset_t set;
1615 } ScmSysSigset;
1616
1617 SCM_CLASS_DECL(Scm_SysSigsetClass);
1618 #define SCM_CLASS_SYS_SIGSET (&Scm_SysSigsetClass)
1619 #define SCM_SYS_SIGSET(obj) ((ScmSysSigset*)(obj))
1620 #define SCM_SYS_SIGSET_P(obj) SCM_XTYPEP(obj, SCM_CLASS_SYS_SIGSET)
1621
1622 SCM_EXTERN ScmObj Scm_SysSigsetOp(ScmSysSigset*, ScmObj, int);
1623 SCM_EXTERN ScmObj Scm_SysSigsetFill(ScmSysSigset*, int);
1624 SCM_EXTERN void Scm_SigFillSetMostly(sigset_t *set);
1625 SCM_EXTERN ScmObj Scm_GetSignalHandler(int);
1626 SCM_EXTERN ScmObj Scm_GetSignalHandlerMask(int);
1627 SCM_EXTERN ScmObj Scm_GetSignalHandlers(void);
1628 SCM_EXTERN ScmObj Scm_SetSignalHandler(ScmObj, ScmObj, ScmSysSigset*);
1629 SCM_EXTERN ScmObj Scm_SysSigmask(int how, ScmSysSigset *newmask);
1630 SCM_EXTERN ScmObj Scm_Pause(void);
1631 SCM_EXTERN ScmObj Scm_SigSuspend(ScmSysSigset *mask);
1632 SCM_EXTERN int Scm_SigWait(ScmSysSigset *mask);
1633 SCM_EXTERN sigset_t Scm_GetMasterSigmask(void);
1634 SCM_EXTERN void Scm_SetMasterSigmask(sigset_t *set);
1635 SCM_EXTERN ScmObj Scm_SignalName(int signum);
1636 SCM_EXTERN void Scm_ResetSignalHandlers(sigset_t *mask);
1637
1638 #if GAUCHE_API_VERSION < 98
1639 SCM_EXTERN void Scm_GetSigmask(sigset_t *mask);
1640 SCM_EXTERN void Scm_SetSigmask(sigset_t *mask);
1641 #endif /*GAUCHE_API_VERSION < 98*/
1642
1643 /*---------------------------------------------------
1644 * SYSTEM
1645 */
1646
1647 #include <gauche/system.h>
1648
1649 /*---------------------------------------------------
1650 * LOAD AND DYNAMIC LINK
1651 */
1652
1653 #include <gauche/load.h>
1654
1655 /*---------------------------------------------------
1656 * THREADS
1657 */
1658
1659 #include <gauche/thread.h>
1660
1661 /*---------------------------------------------------
1662 * PROFILER INTERFACE
1663 */
1664
1665 SCM_EXTERN void Scm_ProfilerStart(void);
1666 SCM_EXTERN int Scm_ProfilerStop(void);
1667 SCM_EXTERN void Scm_ProfilerReset(void);
1668
1669 /*---------------------------------------------------
1670 * UTILITY STUFF
1671 */
1672
1673 /* Program start and termination */
1674
1675 SCM_EXTERN void Scm_Init(const char *signature);
1676 SCM_EXTERN void Scm_Cleanup(void);
1677 SCM_EXTERN void Scm_Exit(int code) SCM_NORETURN;
1678 SCM_EXTERN int Scm_ObjToExitCode(ScmObj obj);
1679 SCM_EXTERN void Scm_Abort(const char *msg) SCM_NORETURN;
1680 SCM_EXTERN void Scm_Panic(const char *msg, ...) SCM_NORETURN;
1681
1682 /* Runtime state, obtainable by Scm_RuntimeState().
1683 This is used internally for diagnostic purposes. E.g. an elaborated error
1684 repoter may not function if a module required for that reporter hasn't
1685 been initialized.
1686 */
1687 typedef enum {
1688 /* We're executing Scm_Init(). Not all fundamental components are
1689 functional. */
1690 SCM_RUNTIME_INITIALIZING,
1691
1692 /* All pre-compiled components are initialized, ready to load Scheme
1693 program. Scheme script is run in this state. */
1694 SCM_RUNTIME_INITIALIZED,
1695
1696 /* Minimum REPL is working. This is the REPL you get with 'gosh -q'.
1697 You can get to this state without loading any Scheme files. */
1698 SCM_RUNTIME_MINI_REPL,
1699
1700 /* REPL is working. This is the normal REPL you get with gosh.
1701 You can assume full Gauche functionalities are available. */
1702 SCM_RUNTIME_FULL_REPL,
1703 } ScmRuntimeState;
1704
1705 SCM_EXTERN ScmRuntimeState Scm_RuntimeState(void);
1706 SCM_EXTERN void Scm_SetRuntimeReplState(int);
1707
1708 /* 'kind' argument of Scm_InitCommandLine */
1709 enum {
1710 SCM_COMMAND_LINE_SCRIPT = 1, /* for (command-line) */
1711 SCM_COMMAND_LINE_OS = 2, /* for (os-command-line) */
1712 SCM_COMMAND_LINE_BOTH = (SCM_COMMAND_LINE_SCRIPT|SCM_COMMAND_LINE_OS)
1713 };
1714
1715 #if GAUCHE_API_VERSION >= 98
1716 SCM_EXTERN ScmObj Scm_InitCommandLine(int argc, const char *argv[],
1717 int kind);
1718 #define Scm_InitCommandLine2(ac, av, kind) Scm_InitCommandLine(ac, av, kind)
1719 #else /* GAUCHE_API_VERSION < 98 */
1720 SCM_EXTERN ScmObj Scm_InitCommandLine(int argc, const char *argv[]);
1721 SCM_EXTERN ScmObj Scm_InitCommandLine2(int argc, const char *argv[], int kind);
1722 #endif /* GAUCHE_API_VERSION < 98 */
1723
1724 SCM_EXTERN void Scm_SimpleMain(int argc, const char *argv[],
1725 const char *script, u_long flags);
1726
1727 SCM_EXTERN void Scm_GC(void);
1728 SCM_EXTERN void Scm_PrintStaticRoots(void);
1729 SCM_EXTERN void Scm_RegisterDL(void *data_start, void *data_end,
1730 void *bss_start, void *bss_end);
1731 SCM_EXTERN void Scm_GCSentinel(void *obj, const char *name);
1732
1733 SCM_EXTERN ScmObj Scm_GetFeatures(void);
1734 SCM_EXTERN void Scm_AddFeature(const char *feature, const char *mod);
1735 SCM_EXTERN void Scm_DisableFeature(const char *feature);
1736 SCM_EXTERN ScmObj Scm_BuildGoshVersion(void);
1737
1738 SCM_EXTERN void *Scm_AddCleanupHandler(void (*proc)(void *data), void *data);
1739 SCM_EXTERN void Scm_DeleteCleanupHandler(void *handle);
1740
1741 /* repl */
1742 SCM_EXTERN void Scm_Repl(ScmObj reader, ScmObj evaluator, ScmObj printer,
1743 ScmObj prompter);
1744
1745 /* Inspect the configuration */
1746 SCM_EXTERN const char *Scm_HostArchitecture(void);
1747
1748 SCM_EXTERN ScmObj Scm_LibraryDirectory(void);
1749 SCM_EXTERN ScmObj Scm_ArchitectureDirectory(void);
1750 SCM_EXTERN ScmObj Scm_SiteLibraryDirectory(void);
1751 SCM_EXTERN ScmObj Scm_SiteArchitectureDirectory(void);
1752 SCM_EXTERN ScmObj Scm_RuntimeDirectory(void); /* may return SCM_FALSE */
1753 SCM_EXTERN ScmObj Scm_LibgauchePath(void); /* may return SCM_FALSE */
1754 SCM_EXTERN ScmObj Scm_ExecutablePath(void); /* may return SCM_FALSE */
1755
1756 /* Compare and Sort */
1757
1758 #include <gauche/compare.h>
1759
1760 /* Assertion */
1761
1762 #ifdef GAUCHE_RECKLESS
1763 #define SCM_ASSERT(expr) /* nothing */
1764 #else
1765
1766 #ifdef __GNUC__
1767
1768 #define SCM_ASSERT(expr) \
1769 do { \
1770 if (!(expr)) \
1771 Scm_Panic("\"%s\", line %d (%s): Assertion failed: %s", \
1772 __FILE__, __LINE__, __PRETTY_FUNCTION__, #expr); \
1773 } while (0)
1774
1775 #else
1776
1777 #define SCM_ASSERT(expr) \
1778 do { \
1779 if (!(expr)) \
1780 Scm_Panic("\"%s\", line %d: Assertion failed: %s", \
1781 __FILE__, __LINE__, #expr); \
1782 } while (0)
1783
1784 #endif /* !__GNUC__ */
1785
1786 #endif /* !GAUCHE_RECKLESS */
1787
1788 #include <gauche/scmconst.h>
1789 #include <gauche/endian.h>
1790
1791 SCM_DECL_END
1792
1793 #endif /* GAUCHE_H */