1 /*
2  * symbol.h - Public API for Scheme symbols
3  *
4  *   Copyright (c) 2000-2020  Shiro Kawai  <shiro@acm.org>
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 /* This file is included from gauche.h */
35 
36 #ifndef GAUCHE_SYMBOL_H
37 #define GAUCHE_SYMBOL_H
38 
39 struct ScmSymbolRec {
40     SCM_HEADER;
41     ScmString *name;
42     int        flags;
43 };
44 
45 SCM_CLASS_DECL(Scm_SymbolClass);
46 #define SCM_CLASS_SYMBOL       (&Scm_SymbolClass)
47 
48 typedef enum {
49     SCM_SYMBOL_FLAG_INTERNED = 1L<<0
50 } ScmSymbolFlags;
51 
52 #define SCM_SYMBOL(obj)          ((ScmSymbol*)(obj))
53 #define SCM_SYMBOLP(obj)         SCM_ISA(obj, SCM_CLASS_SYMBOL)
54 #define SCM_SYMBOL_NAME(obj)     (SCM_SYMBOL(obj)->name)
55 #define SCM_SYMBOL_INTERNED(obj) \
56     (SCM_SYMBOL(obj)->flags&SCM_SYMBOL_FLAG_INTERNED)
57 
58 /* Compatibility note: The 'flags' slot in ScmSymbol used to be an
59  * 'interned', just to keep whether the symbol is interned or not.
60  * We changed it in 0.9.2 to allow future extensions.
61  * All statically defined ScmSymbol has initialized this slot by
62  * either 0 or 1, so we can make use of the rest of bits; however,
63  * the existing code checks SCM_SYMBOL_INTERNED without the mask,
64  * so, in order to keep the binary compatibility, we can set other
65  * bits of flags only if the symbol is interened.   This restriction
66  * can be removed upon 1.0 release.
67  */
68 
69 SCM_EXTERN ScmObj Scm_MakeSymbol(ScmString *name, int interned);
70 SCM_EXTERN ScmObj Scm_Gensym(ScmString *prefix);
71 SCM_EXTERN ScmObj Scm_SymbolSansPrefix(ScmSymbol *s, ScmSymbol *p);
72 
73 #define Scm_Intern(name)  Scm_MakeSymbol(name, TRUE)
74 #define SCM_INTERN(cstr)  Scm_Intern(SCM_STRING(SCM_MAKE_STR_IMMUTABLE(cstr)))
75 
76 SCM_EXTERN void Scm_WriteSymbolName(ScmString *snam, ScmPort *port,
77                                     ScmWriteContext *ctx, u_int flags);
78 
79 /* flags for Scm_WriteSymbolName */
80 #define SCM_SYMBOL_WRITER_NOESCAPE_INITIAL  1u
81 #define SCM_SYMBOL_WRITER_NOESCAPE_EMPTY    2u
82 
83 typedef ScmSymbol ScmKeyword;
84 
85 SCM_CLASS_DECL(Scm_KeywordClass);
86 #define SCM_CLASS_KEYWORD       (&Scm_KeywordClass)
87 
88 #define SCM_KEYWORD(obj)        ((ScmKeyword*)(obj))
89 #define SCM_KEYWORDP(obj)       SCM_XTYPEP(obj, SCM_CLASS_KEYWORD)
90 #define SCM_KEYWORD_NAME(obj)   (SCM_KEYWORD(obj)->name)
91 
92 SCM_EXTERN ScmObj Scm_MakeKeyword(ScmString *name);
93 SCM_EXTERN ScmObj Scm_GetKeyword(ScmObj key, ScmObj list, ScmObj fallback);
94 SCM_EXTERN ScmObj Scm_DeleteKeyword(ScmObj key, ScmObj list);
95 SCM_EXTERN ScmObj Scm_DeleteKeywordX(ScmObj key, ScmObj list);
96 SCM_EXTERN ScmObj Scm_KeywordToString(ScmKeyword *k);
97 
98 #define SCM_MAKE_KEYWORD(cstr) \
99     Scm_MakeKeyword(SCM_STRING(SCM_MAKE_STR_IMMUTABLE(cstr)))
100 #define SCM_GET_KEYWORD(cstr, list, fallback) \
101     Scm_GetKeyword(SCM_MAKE_KEYWORD(cstr), list, fallback)
102 
103 #endif /* GAUCHE_SYMBOL_H */
104 
105