source: branches/0.19.x/abcl/src/org/armedbear/lisp/StandardMethod.java

Last change on this file was 12481, checked in by ehuelsmann, 15 years ago

Merge 'metaclass' branch, making STANDARD-CLASS have slots to

be inherited by deriving metaclasses.

Note: this does definitely *not* complete the metaclass work.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.3 KB
Line 
1/*
2 * StandardMethod.java
3 *
4 * Copyright (C) 2005 Peter Graves
5 * $Id: StandardMethod.java 12481 2010-02-14 21:29:58Z 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
34package org.armedbear.lisp;
35
36import static org.armedbear.lisp.Lisp.*;
37
38public 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            sb.append(getLispClass().getName().writeToString());
160            sb.append(' ');
161            sb.append(name.writeToString());
162            LispObject specializers =
163              slots[StandardMethodClass.SLOT_INDEX_SPECIALIZERS];
164            if (specializers != null)
165              {
166                LispObject specs = specializers;
167                LispObject names = NIL;
168                while (specs != NIL)
169                  {
170                    LispObject spec = specs.car();
171                    if (spec instanceof LispClass)
172                      names = names.push(((LispClass)spec).getName());
173                    else
174                      names = names.push(spec);
175                    specs = specs.cdr();
176                  }
177                sb.append(' ');
178                sb.append(names.nreverse().writeToString());
179              }
180            return unreadableString(sb.toString());
181          }
182      }
183    return super.writeToString();
184  }
185
186  // ### %method-generic-function
187  private static final Primitive _METHOD_GENERIC_FUNCTION =
188    new Primitive("%method-generic-function", PACKAGE_SYS, true)
189    {
190      @Override
191      public LispObject execute(LispObject arg)
192      {
193          return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_GENERIC_FUNCTION];
194      }
195    };
196
197  // ### %set-method-generic-function
198  private static final Primitive _SET_METHOD_GENERICFUNCTION =
199    new Primitive("%set-method-generic-function", PACKAGE_SYS, true)
200    {
201      @Override
202      public LispObject execute(LispObject first, LispObject second)
203
204      {
205          checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_GENERIC_FUNCTION] = second;
206          return second;
207      }
208    };
209
210  // ### %method-function
211  private static final Primitive _METHOD_FUNCTION =
212    new Primitive("%method-function", PACKAGE_SYS, true, "method")
213    {
214      @Override
215      public LispObject execute(LispObject arg)
216      {
217          return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_FUNCTION];
218      }
219    };
220
221  // ### %set-method-function
222  private static final Primitive _SET_METHOD_FUNCTION =
223    new Primitive("%set-method-function", PACKAGE_SYS, true,
224                  "method function")
225    {
226      @Override
227      public LispObject execute(LispObject first, LispObject second)
228
229      {
230          checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_FUNCTION] = second;
231          return second;
232      }
233    };
234
235  // ### %method-fast-function
236  private static final Primitive _METHOD_FAST_FUNCTION =
237    new Primitive("%method-fast-function", PACKAGE_SYS, true, "method")
238    {
239      @Override
240      public LispObject execute(LispObject arg)
241      {
242          return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_FAST_FUNCTION];
243      }
244    };
245
246  // ### %set-method-fast-function
247  private static final Primitive _SET_METHOD_FAST_FUNCTION =
248    new Primitive("%set-method-fast-function", PACKAGE_SYS, true,
249                  "method fast-function")
250    {
251      @Override
252      public LispObject execute(LispObject first, LispObject second)
253
254      {
255          checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_FAST_FUNCTION] = second;
256          return second;
257      }
258    };
259
260  // ### %method-specializers
261  private static final Primitive _METHOD_SPECIALIZERS =
262    new Primitive("%method-specializers", PACKAGE_SYS, true, "method")
263    {
264      @Override
265      public LispObject execute(LispObject arg)
266      {
267          return checkStandardMethod(arg).slots[StandardMethodClass.SLOT_INDEX_SPECIALIZERS];
268      }
269    };
270
271  // ### %set-method-specializers
272  private static final Primitive _SET_METHOD_SPECIALIZERS =
273    new Primitive("%set-method-specializers", PACKAGE_SYS, true,
274                  "method specializers")
275    {
276      @Override
277      public LispObject execute(LispObject first, LispObject second)
278
279      {
280          checkStandardMethod(first).slots[StandardMethodClass.SLOT_INDEX_SPECIALIZERS] = second;
281          return second;
282      }
283    };
284
285  private static final StandardGenericFunction METHOD_SPECIALIZERS =
286    new StandardGenericFunction("method-specializers",
287                                PACKAGE_MOP,
288                                true,
289                                _METHOD_SPECIALIZERS,
290                                list(Symbol.METHOD),
291                                list(StandardClass.STANDARD_METHOD));
292
293  private static final StandardGenericFunction METHOD_QUALIFIERS =
294    new StandardGenericFunction("method-qualifiers",
295                                PACKAGE_MOP,
296                                true,
297                                _METHOD_QUALIFIERS,
298                                list(Symbol.METHOD),
299                                list(StandardClass.STANDARD_METHOD));
300
301        final public static StandardMethod checkStandardMethod(LispObject first)
302        {
303                if (first instanceof StandardMethod)
304                        return (StandardMethod) first;
305                return (StandardMethod) type_error(first, Symbol.METHOD);
306        }
307
308}
Note: See TracBrowser for help on using the repository browser.