source: branches/0.15.x/abcl/src/org/armedbear/lisp/SlotClass.java

Last change on this file was 11754, checked in by vvoutilainen, 16 years ago

Convert using ClassCastException? to checking instanceof.
Performance tests show this approach to be faster.
Patch by Douglas R. Miles. I modified the patch to
remove tabs, so indentation may be slightly off in places.
That's something that we need to handle separately, abcl
doesn't have a clear indentation policy.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.4 KB
Line 
1/*
2 * SlotClass.java
3 *
4 * Copyright (C) 2003-2005 Peter Graves
5 * $Id: SlotClass.java 11754 2009-04-12 10:53:39Z vvoutilainen $
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
36public class SlotClass extends LispClass
37{
38    private LispObject directSlotDefinitions = NIL;
39    private LispObject slotDefinitions = NIL;
40    private LispObject directDefaultInitargs = NIL;
41    private LispObject defaultInitargs = NIL;
42
43    public SlotClass()
44    {
45    }
46
47    public SlotClass(Symbol symbol, LispObject directSuperclasses)
48    {
49        super(symbol, directSuperclasses);
50    }
51
52    @Override
53    public LispObject getParts() throws ConditionThrowable
54    {
55        LispObject result = super.getParts().nreverse();
56        result = result.push(new Cons("DIRECT-SLOTS", directSlotDefinitions));
57        result = result.push(new Cons("SLOTS", slotDefinitions));
58        result = result.push(new Cons("DIRECT-DEFAULT-INITARGS", directDefaultInitargs));
59        result = result.push(new Cons("DEFAULT-INITARGS", defaultInitargs));
60        return result.nreverse();
61    }
62
63    @Override
64    public LispObject typep(LispObject type) throws ConditionThrowable
65    {
66        return super.typep(type);
67    }
68
69    public LispObject getDirectSlotDefinitions()
70    {
71        return directSlotDefinitions;
72    }
73
74    public void setDirectSlotDefinitions(LispObject directSlotDefinitions)
75    {
76        this.directSlotDefinitions = directSlotDefinitions;
77    }
78
79    public final LispObject getSlotDefinitions()
80    {
81        return slotDefinitions;
82    }
83
84    public void setSlotDefinitions(LispObject slotDefinitions)
85    {
86        this.slotDefinitions = slotDefinitions;
87    }
88
89    public LispObject getDirectDefaultInitargs()
90    {
91        return directDefaultInitargs;
92    }
93
94    public void setDirectDefaultInitargs(LispObject directDefaultInitargs)
95    {
96        this.directDefaultInitargs = directDefaultInitargs;
97    }
98
99    public void setDefaultInitargs(LispObject defaultInitargs)
100    {
101        this.defaultInitargs = defaultInitargs;
102    }
103
104    private LispObject computeDefaultInitargs() throws ConditionThrowable
105    {
106        LispObject result = NIL;
107        LispObject cpl = getCPL();
108        while (cpl != NIL) {
109            LispClass c = (LispClass) cpl.car();
110            if (c instanceof StandardClass) {
111                LispObject obj = ((StandardClass)c).getDirectDefaultInitargs();
112                if (obj != NIL)
113                    result = Symbol.APPEND.execute(result, obj);
114            }
115            cpl = cpl.cdr();
116        }
117        return result;
118    }
119
120    public void finalizeClass()
121    {
122        if (isFinalized())
123            return;
124        try {
125            Debug.assertTrue(slotDefinitions == NIL);
126            LispObject cpl = getCPL();
127            Debug.assertTrue(cpl != null);
128            Debug.assertTrue(cpl.listp());
129            cpl = cpl.reverse();
130            while (cpl != NIL) {
131                LispObject car = cpl.car();
132                if (car instanceof StandardClass) {
133                    StandardClass cls = (StandardClass) car;
134                    LispObject defs = cls.getDirectSlotDefinitions();
135                    Debug.assertTrue(defs != null);
136                    Debug.assertTrue(defs.listp());
137                    while (defs != NIL) {
138                        slotDefinitions = slotDefinitions.push(defs.car());
139                        defs = defs.cdr();
140                    }
141                }
142                cpl = cpl.cdr();
143            }
144            slotDefinitions = slotDefinitions.nreverse();
145            LispObject[] instanceSlotNames = new LispObject[slotDefinitions.length()];
146            int i = 0;
147            LispObject tail = slotDefinitions;
148            while (tail != NIL) {
149                SlotDefinition slotDefinition = (SlotDefinition) tail.car();
150                slotDefinition.setLocation(i);
151                instanceSlotNames[i++] = slotDefinition.getName();
152                tail = tail.cdr();
153            }
154            setClassLayout(new Layout(this, instanceSlotNames, NIL));
155            setDefaultInitargs(computeDefaultInitargs());
156            setFinalized(true);
157        }
158        catch (Throwable t) {
159            Debug.trace(t);
160        }
161    }
162
163    // ### class-direct-slots
164    private static final Primitive CLASS_DIRECT_SLOTS =
165        new Primitive("class-direct-slots", PACKAGE_SYS, true)
166    {
167        @Override
168        public LispObject execute(LispObject arg)
169            throws ConditionThrowable
170        {
171            if (arg instanceof SlotClass)
172                return ((SlotClass)arg).directSlotDefinitions;
173            if (arg instanceof BuiltInClass)
174                return NIL;
175            return type_error(arg, Symbol.STANDARD_CLASS);
176        }
177    };
178
179    // ### %set-class-direct-slots
180    private static final Primitive _SET_CLASS_DIRECT_SLOTS =
181        new Primitive("%set-class-direct-slots", PACKAGE_SYS, true)
182    {
183        @Override
184        public LispObject execute(LispObject first, LispObject second)
185            throws ConditionThrowable
186        {
187                if (first instanceof SlotClass) {
188                ((SlotClass)first).directSlotDefinitions = second;
189                return second;
190            }
191                else {
192                return type_error(first, Symbol.STANDARD_CLASS);
193            }
194        }
195    };
196
197    // ### %class-slots
198    private static final Primitive _CLASS_SLOTS =
199        new Primitive(Symbol._CLASS_SLOTS, "class")
200    {
201        @Override
202        public LispObject execute(LispObject arg)
203            throws ConditionThrowable
204        {
205            if (arg instanceof SlotClass)
206                return ((SlotClass)arg).slotDefinitions;
207            if (arg instanceof BuiltInClass)
208                return NIL;
209            return type_error(arg, Symbol.STANDARD_CLASS);
210        }
211    };
212
213    // ### set-class-slots
214    private static final Primitive SET_CLASS_SLOTS =
215        new Primitive(Symbol.SET_CLASS_SLOTS, "class slot-definitions")
216    {
217        @Override
218        public LispObject execute(LispObject first, LispObject second)
219            throws ConditionThrowable
220        {
221                if (first instanceof SlotClass) {
222                ((SlotClass)first).slotDefinitions = second;
223                return second;
224            }
225                else {
226                return type_error(first, Symbol.STANDARD_CLASS);
227            }
228        }
229    };
230
231    // ### class-direct-default-initargs
232    private static final Primitive CLASS_DIRECT_DEFAULT_INITARGS =
233        new Primitive("class-direct-default-initargs", PACKAGE_SYS, true)
234    {
235        @Override
236        public LispObject execute(LispObject arg)
237            throws ConditionThrowable
238        {
239            if (arg instanceof SlotClass)
240                return ((SlotClass)arg).directDefaultInitargs;
241            if (arg instanceof BuiltInClass)
242                return NIL;
243            return type_error(arg, Symbol.STANDARD_CLASS);
244        }
245    };
246
247    // ### %set-class-direct-default-initargs
248    private static final Primitive _SET_CLASS_DIRECT_DEFAULT_INITARGS =
249        new Primitive("%set-class-direct-default-initargs", PACKAGE_SYS, true)
250    {
251        @Override
252        public LispObject execute(LispObject first, LispObject second)
253            throws ConditionThrowable
254        {
255                   if (first instanceof SlotClass) {               
256                           ((SlotClass)first).directDefaultInitargs = second;               
257                           return second;
258                   }
259                   return type_error(first, Symbol.STANDARD_CLASS);
260        }
261    };
262
263    // ### class-default-initargs
264    private static final Primitive CLASS_DEFAULT_INITARGS =
265        new Primitive("class-default-initargs", PACKAGE_SYS, true)
266    {
267        @Override
268        public LispObject execute(LispObject arg)
269            throws ConditionThrowable
270        {
271            if (arg instanceof SlotClass)
272                return ((SlotClass)arg).defaultInitargs;
273            if (arg instanceof BuiltInClass)
274                return NIL;
275            return type_error(arg, Symbol.STANDARD_CLASS);
276        }
277    };
278
279    // ### %set-class-default-initargs
280    private static final Primitive _SET_CLASS_DEFAULT_INITARGS =
281        new Primitive("%set-class-default-initargs", PACKAGE_SYS, true)
282    {
283        @Override
284        public LispObject execute(LispObject first, LispObject second)
285            throws ConditionThrowable
286        {
287            if (first instanceof SlotClass) {
288                ((SlotClass)first).defaultInitargs = second;
289                return second;
290            }
291            return type_error(first, Symbol.STANDARD_CLASS);
292        }
293    };
294
295    // ### compute-class-default-initargs
296    private static final Primitive COMPUTE_CLASS_DEFAULT_INITARGS =
297        new Primitive("compute-class-default-initargs", PACKAGE_SYS, true)
298    {
299        @Override
300        public LispObject execute(LispObject arg)
301            throws ConditionThrowable
302        {
303            final SlotClass c;
304            if (arg instanceof SlotClass) {
305                c = (SlotClass) arg;
306            }
307            else {
308                return type_error(arg, Symbol.STANDARD_CLASS);
309            }
310            return c.computeDefaultInitargs();
311        }
312    };
313}
Note: See TracBrowser for help on using the repository browser.