source: branches/0.26.x/abcl/src/org/armedbear/lisp/SlotDefinition.java

Last change on this file was 13273, checked in by astalla, 14 years ago

Correct support for custom slots definitions in MOP:

  • class hierarchy for slot definitions as specified by AMOP (except the METAOBJECT superclass)
  • above class hierarchy is extensible by users
  • custom slot options are not evaluated
  • compute-effective-slot-definition lambda list updated to take NAME argument (2nd)
  • a few more symbols exported from the MOP package

There are no new ANSI tests failures caused by these changes on my machine.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 12.8 KB
Line 
1/*
2 * SlotDefinition.java
3 *
4 * Copyright (C) 2005 Peter Graves
5 * $Id: SlotDefinition.java 13273 2011-05-01 22:34:47Z astalla $
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
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38public final class SlotDefinition extends StandardObject
39{
40  public SlotDefinition()
41  {
42    super(StandardClass.STANDARD_SLOT_DEFINITION,
43          StandardClass.STANDARD_SLOT_DEFINITION.getClassLayout().getLength());
44    slots[SlotDefinitionClass.SLOT_INDEX_LOCATION] = NIL;
45  }
46
47    public SlotDefinition(StandardClass clazz) {
48        super(clazz, clazz.getClassLayout().getLength());
49        slots[SlotDefinitionClass.SLOT_INDEX_LOCATION] = NIL;
50    }
51
52    public SlotDefinition(StandardClass clazz, LispObject name) {
53        super(clazz, clazz.getClassLayout().getLength());
54        slots[SlotDefinitionClass.SLOT_INDEX_NAME] = name;
55        slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION] = NIL;
56        slots[SlotDefinitionClass.SLOT_INDEX_INITARGS] =
57            new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()));
58        slots[SlotDefinitionClass.SLOT_INDEX_READERS] = NIL;
59        slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION] = Keyword.INSTANCE;
60        slots[SlotDefinitionClass.SLOT_INDEX_LOCATION] = NIL;
61    }
62
63  public SlotDefinition(LispObject name, LispObject readers)
64  {
65    this();
66    Debug.assertTrue(name instanceof Symbol);
67    slots[SlotDefinitionClass.SLOT_INDEX_NAME] = name;
68    slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION] = NIL;
69    slots[SlotDefinitionClass.SLOT_INDEX_INITARGS] =
70      new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()));
71    slots[SlotDefinitionClass.SLOT_INDEX_READERS] = readers;
72    slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION] = Keyword.INSTANCE;
73  }
74
75  public SlotDefinition(LispObject name, LispObject readers,
76                        LispObject initForm)
77  {
78    this();
79    Debug.assertTrue(name instanceof Symbol);
80    slots[SlotDefinitionClass.SLOT_INDEX_NAME] = name;
81    slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION] = NIL;
82    slots[SlotDefinitionClass.SLOT_INDEX_INITFORM] = initForm;
83    slots[SlotDefinitionClass.SLOT_INDEX_INITARGS] =
84      new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()));
85    slots[SlotDefinitionClass.SLOT_INDEX_READERS] = readers;
86    slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION] = Keyword.INSTANCE;
87  }
88
89  public SlotDefinition(LispObject name, LispObject readers,
90                        Function initFunction)
91  {
92    this();
93    Debug.assertTrue(name instanceof Symbol);
94    slots[SlotDefinitionClass.SLOT_INDEX_NAME] = name;
95    slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION] = initFunction;
96    slots[SlotDefinitionClass.SLOT_INDEX_INITFORM] = NIL;
97    slots[SlotDefinitionClass.SLOT_INDEX_INITARGS] =
98      new Cons(PACKAGE_KEYWORD.intern(((Symbol)name).getName()));
99    slots[SlotDefinitionClass.SLOT_INDEX_READERS] = readers;
100    slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION] = Keyword.INSTANCE;
101  }
102
103  public static StandardObject checkSlotDefinition(LispObject obj) {
104          if (obj instanceof StandardObject) return (StandardObject)obj;
105      return (StandardObject)type_error(obj, Symbol.SLOT_DEFINITION);
106  }
107
108  public final LispObject getName()
109  {
110    return slots[SlotDefinitionClass.SLOT_INDEX_NAME];
111  }
112
113  public final void setLocation(int i)
114  {
115    slots[SlotDefinitionClass.SLOT_INDEX_LOCATION] = Fixnum.getInstance(i);
116  }
117
118  @Override
119  public String writeToString()
120  {
121    StringBuilder sb =
122      new StringBuilder(Symbol.SLOT_DEFINITION.writeToString());
123    LispObject name = slots[SlotDefinitionClass.SLOT_INDEX_NAME];
124    if (name != null && name != NIL)
125      {
126        sb.append(' ');
127        sb.append(name.writeToString());
128      }
129    return unreadableString(sb.toString());
130  }
131
132  // ### make-slot-definition &optional class
133  private static final Primitive MAKE_SLOT_DEFINITION =
134    new Primitive("make-slot-definition", PACKAGE_SYS, true, "&optional class")
135    {
136      @Override
137      public LispObject execute()
138      {
139        return new SlotDefinition();
140      }
141      @Override
142      public LispObject execute(LispObject slotDefinitionClass)
143      {
144          return new SlotDefinition((StandardClass) slotDefinitionClass);
145      }
146    };
147
148  // ### %slot-definition-name
149  private static final Primitive _SLOT_DEFINITION_NAME =
150    new Primitive(Symbol._SLOT_DEFINITION_NAME, "slot-definition")
151    {
152      @Override
153      public LispObject execute(LispObject arg)
154      {
155          return checkSlotDefinition(arg).slots[SlotDefinitionClass.SLOT_INDEX_NAME];
156      }
157    };
158
159  // ### set-slot-definition-name
160  private static final Primitive SET_SLOT_DEFINITION_NAME =
161    new Primitive("set-slot-definition-name", PACKAGE_SYS, true,
162                  "slot-definition name")
163    {
164      @Override
165      public LispObject execute(LispObject first, LispObject second)
166
167      {
168          checkSlotDefinition(first).slots[SlotDefinitionClass.SLOT_INDEX_NAME] = second;
169          return second;
170      }
171    };
172
173  // ### %slot-definition-initfunction
174  private static final Primitive _SLOT_DEFINITION_INITFUNCTION =
175    new Primitive(Symbol._SLOT_DEFINITION_INITFUNCTION, "slot-definition")
176    {
177      @Override
178      public LispObject execute(LispObject arg)
179      {
180          return checkSlotDefinition(arg).slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION];
181      }
182    };
183
184  // ### set-slot-definition-initfunction
185  static final Primitive SET_SLOT_DEFINITION_INITFUNCTION =
186    new Primitive("set-slot-definition-initfunction", PACKAGE_SYS, true,
187                  "slot-definition initfunction")
188    {
189      @Override
190      public LispObject execute(LispObject first, LispObject second)
191
192      {
193          checkSlotDefinition(first).slots[SlotDefinitionClass.SLOT_INDEX_INITFUNCTION] = second;
194          return second;
195      }
196    };
197
198  // ### %slot-definition-initform
199  private static final Primitive _SLOT_DEFINITION_INITFORM =
200    new Primitive("%slot-definition-initform", PACKAGE_SYS, true,
201                  "slot-definition")
202    {
203      @Override
204      public LispObject execute(LispObject arg)
205      {
206          return checkSlotDefinition(arg).slots[SlotDefinitionClass.SLOT_INDEX_INITFORM];
207      }
208    };
209
210  // ### set-slot-definition-initform
211  static final Primitive SET_SLOT_DEFINITION_INITFORM =
212    new Primitive("set-slot-definition-initform", PACKAGE_SYS, true,
213                  "slot-definition initform")
214    {
215      @Override
216      public LispObject execute(LispObject first, LispObject second)
217
218      {
219          checkSlotDefinition(first).slots[SlotDefinitionClass.SLOT_INDEX_INITFORM] = second;
220          return second;
221      }
222    };
223
224  // ### %slot-definition-initargs
225  private static final Primitive _SLOT_DEFINITION_INITARGS =
226    new Primitive(Symbol._SLOT_DEFINITION_INITARGS, "slot-definition")
227    {
228      @Override
229      public LispObject execute(LispObject arg)
230      {
231          return checkSlotDefinition(arg).slots[SlotDefinitionClass.SLOT_INDEX_INITARGS];
232      }
233    };
234
235  // ### set-slot-definition-initargs
236  private static final Primitive SET_SLOT_DEFINITION_INITARGS =
237    new Primitive("set-slot-definition-initargs", PACKAGE_SYS, true,
238                  "slot-definition initargs")
239    {
240      @Override
241      public LispObject execute(LispObject first, LispObject second)
242
243      {
244          checkSlotDefinition(first).slots[SlotDefinitionClass.SLOT_INDEX_INITARGS] = second;
245          return second;
246      }
247    };
248
249  // ### %slot-definition-readers
250  private static final Primitive _SLOT_DEFINITION_READERS =
251    new Primitive("%slot-definition-readers", PACKAGE_SYS, true,
252                  "slot-definition")
253    {
254      @Override
255      public LispObject execute(LispObject arg)
256      {
257          return checkSlotDefinition(arg).slots[SlotDefinitionClass.SLOT_INDEX_READERS];
258      }
259    };
260
261  // ### set-slot-definition-readers
262  private static final Primitive SET_SLOT_DEFINITION_READERS =
263    new Primitive("set-slot-definition-readers", PACKAGE_SYS, true,
264                  "slot-definition readers")
265    {
266      @Override
267      public LispObject execute(LispObject first, LispObject second)
268
269      {
270          checkSlotDefinition(first).slots[SlotDefinitionClass.SLOT_INDEX_READERS] = second;
271          return second;
272      }
273    };
274
275  // ### %slot-definition-writers
276  private static final Primitive _SLOT_DEFINITION_WRITERS =
277    new Primitive("%slot-definition-writers", PACKAGE_SYS, true,
278                  "slot-definition")
279    {
280      @Override
281      public LispObject execute(LispObject arg)
282      {
283          return checkSlotDefinition(arg).slots[SlotDefinitionClass.SLOT_INDEX_WRITERS];
284      }
285    };
286
287  // ### set-slot-definition-writers
288  private static final Primitive SET_SLOT_DEFINITION_WRITERS =
289    new Primitive("set-slot-definition-writers", PACKAGE_SYS, true,
290                  "slot-definition writers")
291    {
292      @Override
293      public LispObject execute(LispObject first, LispObject second)
294
295      {
296          checkSlotDefinition(first).slots[SlotDefinitionClass.SLOT_INDEX_WRITERS] = second;
297          return second;
298      }
299    };
300
301  // ### %slot-definition-allocation
302  private static final Primitive _SLOT_DEFINITION_ALLOCATION =
303    new Primitive("%slot-definition-allocation", PACKAGE_SYS, true,
304                  "slot-definition")
305    {
306      @Override
307      public LispObject execute(LispObject arg)
308      {
309          return checkSlotDefinition(arg).slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION];
310      }
311    };
312
313  // ### set-slot-definition-allocation
314  private static final Primitive SET_SLOT_DEFINITION_ALLOCATION =
315    new Primitive("set-slot-definition-allocation", PACKAGE_SYS, true,
316                  "slot-definition allocation")
317    {
318      @Override
319      public LispObject execute(LispObject first, LispObject second)
320
321      {
322          checkSlotDefinition(first).slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION] = second;
323          return second;
324      }
325    };
326
327  // ### %slot-definition-allocation-class
328  private static final Primitive _SLOT_DEFINITION_ALLOCATION_CLASS =
329    new Primitive("%slot-definition-allocation-class", PACKAGE_SYS, true,
330                  "slot-definition")
331    {
332      @Override
333      public LispObject execute(LispObject arg)
334      {
335          return checkSlotDefinition(arg).slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION_CLASS];
336      }
337    };
338
339  // ### set-slot-definition-allocation-class
340  private static final Primitive SET_SLOT_DEFINITION_ALLOCATION_CLASS =
341    new Primitive("set-slot-definition-allocation-class", PACKAGE_SYS, true,
342                  "slot-definition allocation-class")
343    {
344      @Override
345      public LispObject execute(LispObject first, LispObject second)
346
347      {
348          checkSlotDefinition(first).slots[SlotDefinitionClass.SLOT_INDEX_ALLOCATION_CLASS] = second;
349          return second;
350      }
351    };
352
353  // ### %slot-definition-location
354  private static final Primitive _SLOT_DEFINITION_LOCATION =
355    new Primitive("%slot-definition-location", PACKAGE_SYS, true, "slot-definition")
356    {
357      @Override
358      public LispObject execute(LispObject arg)
359      {
360          return checkSlotDefinition(arg).slots[SlotDefinitionClass.SLOT_INDEX_LOCATION];
361      }
362    };
363
364  // ### set-slot-definition-location
365  private static final Primitive SET_SLOT_DEFINITION_LOCATION =
366    new Primitive("set-slot-definition-location", PACKAGE_SYS, true, "slot-definition location")
367    {
368      @Override
369      public LispObject execute(LispObject first, LispObject second)
370
371      {
372          checkSlotDefinition(first).slots[SlotDefinitionClass.SLOT_INDEX_LOCATION] = second;
373          return second;
374      }
375    };
376}
Note: See TracBrowser for help on using the repository browser.