1 | /* |
---|
2 | * SlotDefinition.java |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Peter Graves |
---|
5 | * $Id: SlotDefinition.java 14479 2013-04-24 12:51:16Z rschlatte $ |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | * |
---|
21 | * As a special exception, the copyright holders of this library give you |
---|
22 | * permission to link this library with independent modules to produce an |
---|
23 | * executable, regardless of the license terms of these independent |
---|
24 | * modules, and to copy and distribute the resulting executable under |
---|
25 | * terms of your choice, provided that you also meet, for each linked |
---|
26 | * independent module, the terms and conditions of the license of that |
---|
27 | * module. An independent module is a module which is not derived from |
---|
28 | * or based on this library. If you modify this library, you may extend |
---|
29 | * this exception to your version of the library, but you are not |
---|
30 | * obligated to do so. If you do not wish to do so, delete this |
---|
31 | * exception statement from your version. |
---|
32 | */ |
---|
33 | |
---|
34 | package org.armedbear.lisp; |
---|
35 | |
---|
36 | import static org.armedbear.lisp.Lisp.*; |
---|
37 | |
---|
38 | public final class SlotDefinition extends StandardObject |
---|
39 | { |
---|
40 | private SlotDefinition() |
---|
41 | { |
---|
42 | super(StandardClass.STANDARD_SLOT_DEFINITION, |
---|
43 | StandardClass.STANDARD_SLOT_DEFINITION.getClassLayout().getLength()); |
---|
44 | setInstanceSlotValue(Symbol.LOCATION, NIL); |
---|
45 | setInstanceSlotValue(Symbol._TYPE, T); |
---|
46 | setInstanceSlotValue(Symbol._DOCUMENTATION, NIL); |
---|
47 | } |
---|
48 | |
---|
49 | private SlotDefinition(StandardClass clazz) { |
---|
50 | // clazz layout needs to have SlotDefinitionClass layout as prefix |
---|
51 | // or indexed slot access won't work |
---|
52 | super(clazz, clazz.getClassLayout().getLength()); |
---|
53 | setInstanceSlotValue(Symbol.LOCATION, NIL); |
---|
54 | } |
---|
55 | |
---|
56 | public SlotDefinition(StandardClass clazz, LispObject name) { |
---|
57 | // clazz layout needs to have SlotDefinitionClass layout as prefix |
---|
58 | // or indexed slot access won't work |
---|
59 | super(clazz, clazz.getClassLayout().getLength()); |
---|
60 | Debug.assertTrue(name instanceof Symbol); |
---|
61 | setInstanceSlotValue(Symbol.NAME, name); |
---|
62 | setInstanceSlotValue(Symbol.INITFUNCTION, NIL); |
---|
63 | setInstanceSlotValue(Symbol.INITARGS, |
---|
64 | new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()))); |
---|
65 | setInstanceSlotValue(Symbol.READERS, NIL); |
---|
66 | setInstanceSlotValue(Symbol.ALLOCATION, Keyword.INSTANCE); |
---|
67 | setInstanceSlotValue(Symbol.LOCATION, NIL); |
---|
68 | setInstanceSlotValue(Symbol._TYPE, T); |
---|
69 | setInstanceSlotValue(Symbol._DOCUMENTATION, NIL); |
---|
70 | } |
---|
71 | |
---|
72 | public SlotDefinition(LispObject name, LispObject readers) |
---|
73 | { |
---|
74 | this(); |
---|
75 | Debug.assertTrue(name instanceof Symbol); |
---|
76 | setInstanceSlotValue(Symbol.NAME, name); |
---|
77 | setInstanceSlotValue(Symbol.INITFUNCTION, NIL); |
---|
78 | setInstanceSlotValue(Symbol.INITARGS, |
---|
79 | new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()))); |
---|
80 | setInstanceSlotValue(Symbol.READERS, readers); |
---|
81 | setInstanceSlotValue(Symbol.ALLOCATION, Keyword.INSTANCE); |
---|
82 | } |
---|
83 | |
---|
84 | public SlotDefinition(LispObject name, LispObject readers, |
---|
85 | LispObject initForm) |
---|
86 | { |
---|
87 | this(); |
---|
88 | Debug.assertTrue(name instanceof Symbol); |
---|
89 | setInstanceSlotValue(Symbol.NAME, name); |
---|
90 | setInstanceSlotValue(Symbol.INITFUNCTION, NIL); |
---|
91 | setInstanceSlotValue(Symbol.INITFORM, initForm); |
---|
92 | setInstanceSlotValue(Symbol.INITARGS, |
---|
93 | new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()))); |
---|
94 | setInstanceSlotValue(Symbol.READERS, readers); |
---|
95 | setInstanceSlotValue(Symbol.ALLOCATION, Keyword.INSTANCE); |
---|
96 | } |
---|
97 | |
---|
98 | public SlotDefinition(LispObject name, LispObject readers, |
---|
99 | Function initFunction) |
---|
100 | { |
---|
101 | this(); |
---|
102 | Debug.assertTrue(name instanceof Symbol); |
---|
103 | setInstanceSlotValue(Symbol.NAME, name); |
---|
104 | setInstanceSlotValue(Symbol.INITFUNCTION, initFunction); |
---|
105 | setInstanceSlotValue(Symbol.INITFORM, NIL); |
---|
106 | setInstanceSlotValue(Symbol.INITARGS, |
---|
107 | new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()))); |
---|
108 | setInstanceSlotValue(Symbol.READERS, readers); |
---|
109 | setInstanceSlotValue(Symbol.ALLOCATION, Keyword.INSTANCE); |
---|
110 | } |
---|
111 | |
---|
112 | public SlotDefinition(LispObject name, LispObject readers, |
---|
113 | Function initFunction, LispObject initargs) |
---|
114 | { |
---|
115 | this(); |
---|
116 | Debug.assertTrue(name instanceof Symbol); |
---|
117 | setInstanceSlotValue(Symbol.NAME, name); |
---|
118 | setInstanceSlotValue(Symbol.INITFUNCTION, initFunction); |
---|
119 | setInstanceSlotValue(Symbol.INITFORM, NIL); |
---|
120 | setInstanceSlotValue(Symbol.INITARGS, initargs); |
---|
121 | setInstanceSlotValue(Symbol.READERS, readers); |
---|
122 | setInstanceSlotValue(Symbol.ALLOCATION, Keyword.INSTANCE); |
---|
123 | } |
---|
124 | |
---|
125 | @Override |
---|
126 | public String printObject() |
---|
127 | { |
---|
128 | StringBuilder sb = |
---|
129 | new StringBuilder(Symbol.SLOT_DEFINITION.printObject()); |
---|
130 | LispObject name = getInstanceSlotValue(Symbol.NAME); |
---|
131 | if (name != null && name != NIL) { |
---|
132 | sb.append(' '); |
---|
133 | sb.append(name.printObject()); |
---|
134 | } |
---|
135 | return unreadableString(sb.toString()); |
---|
136 | } |
---|
137 | |
---|
138 | private static final Primitive MAKE_SLOT_DEFINITION |
---|
139 | = new pf_make_slot_definition(); |
---|
140 | @DocString(name="%make-slot-definition", |
---|
141 | args="slot-class", |
---|
142 | doc="Argument must be a subclass of standard-slot-definition") |
---|
143 | private static final class pf_make_slot_definition extends Primitive |
---|
144 | { |
---|
145 | pf_make_slot_definition() |
---|
146 | { |
---|
147 | super("%make-slot-definition", PACKAGE_SYS, true, "slot-class"); |
---|
148 | } |
---|
149 | @Override |
---|
150 | public LispObject execute(LispObject slotDefinitionClass) |
---|
151 | { |
---|
152 | if (!(slotDefinitionClass instanceof StandardClass)) |
---|
153 | return type_error(slotDefinitionClass, |
---|
154 | StandardClass.STANDARD_SLOT_DEFINITION); |
---|
155 | // we could check whether slotClass is a subtype of |
---|
156 | // standard-slot-definition here, but subtypep doesn't work early |
---|
157 | // in the build process |
---|
158 | return new SlotDefinition((StandardClass)slotDefinitionClass); |
---|
159 | } |
---|
160 | }; |
---|
161 | |
---|
162 | } |
---|