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