1 | /* |
---|
2 | * StandardMethod.java |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Peter Graves |
---|
5 | * $Id: StandardMethod.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 StandardMethod extends StandardObject |
---|
39 | { |
---|
40 | public StandardMethod() |
---|
41 | { |
---|
42 | super(StandardClass.STANDARD_METHOD, |
---|
43 | StandardClass.STANDARD_METHOD.getClassLayout().getLength()); |
---|
44 | } |
---|
45 | |
---|
46 | protected StandardMethod(LispClass cls, int length) |
---|
47 | { |
---|
48 | super(cls, length); |
---|
49 | } |
---|
50 | |
---|
51 | public StandardMethod(StandardGenericFunction gf, |
---|
52 | Function fastFunction, |
---|
53 | LispObject lambdaList, |
---|
54 | LispObject specializers) |
---|
55 | { |
---|
56 | this(); |
---|
57 | slots[StandardMethodClass.SLOT_INDEX_GENERIC_FUNCTION] = gf; |
---|
58 | slots[StandardMethodClass.SLOT_INDEX_LAMBDA_LIST] = lambdaList; |
---|
59 | slots[StandardMethodClass.SLOT_INDEX_SPECIALIZERS] = specializers; |
---|
60 | slots[StandardMethodClass.SLOT_INDEX_QUALIFIERS] = NIL; |
---|
61 | slots[StandardMethodClass.SLOT_INDEX_FUNCTION] = NIL; |
---|
62 | slots[StandardMethodClass.SLOT_INDEX_FAST_FUNCTION] = fastFunction; |
---|
63 | slots[StandardMethodClass.SLOT_INDEX_DOCUMENTATION] = NIL; |
---|
64 | } |
---|
65 | |
---|
66 | // ### method-lambda-list |
---|
67 | // generic function |
---|
68 | private static final Primitive METHOD_LAMBDA_LIST = |
---|
69 | new Primitive("method-lambda-list", PACKAGE_SYS, true, "method") |
---|
70 | { |
---|
71 | @Override |
---|
72 | public LispObject execute(LispObject arg) |
---|
73 | { |
---|
74 | return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_LAMBDA_LIST]; |
---|
75 | } |
---|
76 | }; |
---|
77 | |
---|
78 | // ### set-method-lambda-list |
---|
79 | private static final Primitive SET_METHOD_LAMBDA_LIST = |
---|
80 | new Primitive("set-method-lambda-list", PACKAGE_SYS, true, |
---|
81 | "method lambda-list") |
---|
82 | { |
---|
83 | @Override |
---|
84 | public LispObject execute(LispObject first, LispObject second) |
---|
85 | |
---|
86 | { |
---|
87 | checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_LAMBDA_LIST] = second; |
---|
88 | return second; |
---|
89 | } |
---|
90 | }; |
---|
91 | |
---|
92 | // ### method-qualifiers |
---|
93 | private static final Primitive _METHOD_QUALIFIERS = |
---|
94 | new Primitive("%method-qualifiers", PACKAGE_SYS, true, "method") |
---|
95 | { |
---|
96 | @Override |
---|
97 | public LispObject execute(LispObject arg) |
---|
98 | { |
---|
99 | return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_QUALIFIERS]; |
---|
100 | } |
---|
101 | }; |
---|
102 | |
---|
103 | // ### set-method-qualifiers |
---|
104 | private static final Primitive SET_METHOD_QUALIFIERS = |
---|
105 | new Primitive("set-method-qualifiers", PACKAGE_SYS, true, |
---|
106 | "method qualifiers") |
---|
107 | { |
---|
108 | @Override |
---|
109 | public LispObject execute(LispObject first, LispObject second) |
---|
110 | |
---|
111 | { |
---|
112 | checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_QUALIFIERS] = second; |
---|
113 | return second; |
---|
114 | } |
---|
115 | }; |
---|
116 | |
---|
117 | // ### method-documentation |
---|
118 | private static final Primitive METHOD_DOCUMENTATION = |
---|
119 | new Primitive("method-documentation", PACKAGE_SYS, true, "method") |
---|
120 | { |
---|
121 | @Override |
---|
122 | public LispObject execute(LispObject arg) |
---|
123 | { |
---|
124 | return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_DOCUMENTATION]; |
---|
125 | } |
---|
126 | }; |
---|
127 | |
---|
128 | // ### set-method-documentation |
---|
129 | private static final Primitive SET_METHOD_DOCUMENTATION = |
---|
130 | new Primitive("set-method-documentation", PACKAGE_SYS, true, |
---|
131 | "method documentation") |
---|
132 | { |
---|
133 | @Override |
---|
134 | public LispObject execute(LispObject first, LispObject second) |
---|
135 | |
---|
136 | { |
---|
137 | checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_DOCUMENTATION] = second; |
---|
138 | return second; |
---|
139 | } |
---|
140 | }; |
---|
141 | |
---|
142 | public LispObject getFunction() |
---|
143 | { |
---|
144 | return slots[StandardMethodClass.SLOT_INDEX_FUNCTION]; |
---|
145 | } |
---|
146 | |
---|
147 | @Override |
---|
148 | public String writeToString() |
---|
149 | { |
---|
150 | LispObject genericFunction = |
---|
151 | slots[StandardMethodClass.SLOT_INDEX_GENERIC_FUNCTION]; |
---|
152 | if (genericFunction instanceof StandardGenericFunction) |
---|
153 | { |
---|
154 | LispObject name = |
---|
155 | ((StandardGenericFunction)genericFunction).getGenericFunctionName(); |
---|
156 | if (name != null) |
---|
157 | { |
---|
158 | StringBuilder sb = new StringBuilder(); |
---|
159 | LispObject className; |
---|
160 | LispObject lispClass = getLispClass(); |
---|
161 | if (lispClass instanceof LispClass) |
---|
162 | className = ((LispClass)lispClass).getName(); |
---|
163 | else |
---|
164 | className = Symbol.CLASS_NAME.execute(lispClass); |
---|
165 | |
---|
166 | sb.append(className.writeToString()); |
---|
167 | sb.append(' '); |
---|
168 | sb.append(name.writeToString()); |
---|
169 | LispObject specializers = |
---|
170 | slots[StandardMethodClass.SLOT_INDEX_SPECIALIZERS]; |
---|
171 | if (specializers != null) |
---|
172 | { |
---|
173 | LispObject specs = specializers; |
---|
174 | LispObject names = NIL; |
---|
175 | while (specs != NIL) |
---|
176 | { |
---|
177 | LispObject spec = specs.car(); |
---|
178 | if (spec instanceof LispClass) |
---|
179 | names = names.push(((LispClass)spec).getName()); |
---|
180 | else |
---|
181 | names = names.push(spec); |
---|
182 | specs = specs.cdr(); |
---|
183 | } |
---|
184 | sb.append(' '); |
---|
185 | sb.append(names.nreverse().writeToString()); |
---|
186 | } |
---|
187 | return unreadableString(sb.toString()); |
---|
188 | } |
---|
189 | } |
---|
190 | return super.writeToString(); |
---|
191 | } |
---|
192 | |
---|
193 | // ### %method-generic-function |
---|
194 | private static final Primitive _METHOD_GENERIC_FUNCTION = |
---|
195 | new Primitive("%method-generic-function", PACKAGE_SYS, true) |
---|
196 | { |
---|
197 | @Override |
---|
198 | public LispObject execute(LispObject arg) |
---|
199 | { |
---|
200 | return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_GENERIC_FUNCTION]; |
---|
201 | } |
---|
202 | }; |
---|
203 | |
---|
204 | // ### %set-method-generic-function |
---|
205 | private static final Primitive _SET_METHOD_GENERICFUNCTION = |
---|
206 | new Primitive("%set-method-generic-function", PACKAGE_SYS, true) |
---|
207 | { |
---|
208 | @Override |
---|
209 | public LispObject execute(LispObject first, LispObject second) |
---|
210 | |
---|
211 | { |
---|
212 | checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_GENERIC_FUNCTION] = second; |
---|
213 | return second; |
---|
214 | } |
---|
215 | }; |
---|
216 | |
---|
217 | // ### %method-function |
---|
218 | private static final Primitive _METHOD_FUNCTION = |
---|
219 | new Primitive("%method-function", PACKAGE_SYS, true, "method") |
---|
220 | { |
---|
221 | @Override |
---|
222 | public LispObject execute(LispObject arg) |
---|
223 | { |
---|
224 | return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_FUNCTION]; |
---|
225 | } |
---|
226 | }; |
---|
227 | |
---|
228 | // ### %set-method-function |
---|
229 | private static final Primitive _SET_METHOD_FUNCTION = |
---|
230 | new Primitive("%set-method-function", PACKAGE_SYS, true, |
---|
231 | "method function") |
---|
232 | { |
---|
233 | @Override |
---|
234 | public LispObject execute(LispObject first, LispObject second) |
---|
235 | |
---|
236 | { |
---|
237 | checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_FUNCTION] = second; |
---|
238 | return second; |
---|
239 | } |
---|
240 | }; |
---|
241 | |
---|
242 | // ### %method-fast-function |
---|
243 | private static final Primitive _METHOD_FAST_FUNCTION = |
---|
244 | new Primitive("%method-fast-function", PACKAGE_SYS, true, "method") |
---|
245 | { |
---|
246 | @Override |
---|
247 | public LispObject execute(LispObject arg) |
---|
248 | { |
---|
249 | return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_FAST_FUNCTION]; |
---|
250 | } |
---|
251 | }; |
---|
252 | |
---|
253 | // ### %set-method-fast-function |
---|
254 | private static final Primitive _SET_METHOD_FAST_FUNCTION = |
---|
255 | new Primitive("%set-method-fast-function", PACKAGE_SYS, true, |
---|
256 | "method fast-function") |
---|
257 | { |
---|
258 | @Override |
---|
259 | public LispObject execute(LispObject first, LispObject second) |
---|
260 | |
---|
261 | { |
---|
262 | checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_FAST_FUNCTION] = second; |
---|
263 | return second; |
---|
264 | } |
---|
265 | }; |
---|
266 | |
---|
267 | // ### %method-specializers |
---|
268 | private static final Primitive _METHOD_SPECIALIZERS = |
---|
269 | new Primitive("%method-specializers", PACKAGE_SYS, true, "method") |
---|
270 | { |
---|
271 | @Override |
---|
272 | public LispObject execute(LispObject arg) |
---|
273 | { |
---|
274 | return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_SPECIALIZERS]; |
---|
275 | } |
---|
276 | }; |
---|
277 | |
---|
278 | // ### %set-method-specializers |
---|
279 | private static final Primitive _SET_METHOD_SPECIALIZERS = |
---|
280 | new Primitive("%set-method-specializers", PACKAGE_SYS, true, |
---|
281 | "method specializers") |
---|
282 | { |
---|
283 | @Override |
---|
284 | public LispObject execute(LispObject first, LispObject second) |
---|
285 | |
---|
286 | { |
---|
287 | checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_SPECIALIZERS] = second; |
---|
288 | return second; |
---|
289 | } |
---|
290 | }; |
---|
291 | |
---|
292 | private static final StandardGenericFunction METHOD_SPECIALIZERS = |
---|
293 | new StandardGenericFunction("method-specializers", |
---|
294 | PACKAGE_MOP, |
---|
295 | true, |
---|
296 | _METHOD_SPECIALIZERS, |
---|
297 | list(Symbol.METHOD), |
---|
298 | list(StandardClass.STANDARD_METHOD)); |
---|
299 | |
---|
300 | private static final StandardGenericFunction METHOD_QUALIFIERS = |
---|
301 | new StandardGenericFunction("method-qualifiers", |
---|
302 | PACKAGE_MOP, |
---|
303 | true, |
---|
304 | _METHOD_QUALIFIERS, |
---|
305 | list(Symbol.METHOD), |
---|
306 | list(StandardClass.STANDARD_METHOD)); |
---|
307 | |
---|
308 | final public static StandardMethod checkStandardMethod(LispObject first) |
---|
309 | { |
---|
310 | if (first instanceof StandardMethod) |
---|
311 | return (StandardMethod) first; |
---|
312 | return (StandardMethod) type_error(first, Symbol.METHOD); |
---|
313 | } |
---|
314 | |
---|
315 | } |
---|