source: trunk/source/level-0/l0-def.lisp @ 14577

Last change on this file since 14577 was 14577, checked in by gb, 9 years ago

Typecheck the NAME argument in SYMBOL-FUNCTION; use FSET-SYMBOL
as SYMBOL-FUNCTION's SETF inverse.
Fixes ticket:808.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.5 KB
Line 
1;;;-*- Mode: Lisp; Package: CCL -*-
2;;;
3;;;   Copyright (C) 2009 Clozure Associates
4;;;   Copyright (C) 1994-2001 Digitool, Inc
5;;;   This file is part of Clozure CL. 
6;;;
7;;;   Clozure CL is licensed under the terms of the Lisp Lesser GNU Public
8;;;   License , known as the LLGPL and distributed with Clozure CL as the
9;;;   file "LICENSE".  The LLGPL consists of a preamble and the LGPL,
10;;;   which is distributed with Clozure CL as the file "LGPL".  Where these
11;;;   conflict, the preamble takes precedence. 
12;;;
13;;;   Clozure CL is referenced in the preamble as the "LIBRARY."
14;;;
15;;;   The LLGPL is also available online at
16;;;   http://opensource.franz.com/preamble.html
17
18(in-package "CCL")
19
20;;; primitives that manipulate function & variable definitions.
21
22
23
24
25
26(defun functionp (arg)
27  "Return true if OBJECT is a FUNCTION, and NIL otherwise."
28  (functionp arg))
29
30(defun lfunp (arg)
31  (functionp arg))
32
33(defun %proclaim-special (sym &optional initp)
34  (let* ((oldbits (%symbol-bits sym)))
35    (declare (fixnum oldbits))
36    (%symbol-bits sym (bitset $sym_vbit_special oldbits))
37    initp))
38
39(setq *lfun-names* (make-hash-table :test 'eq :weak t))
40
41(defun lookup-lfun-name (lfun) 
42  (gethash lfun *lfun-names*))
43
44
45(defun function-name (fun)
46  (or (and (functionp fun) (lfun-name fun))
47      (if (compiled-function-p (setq fun (closure-function fun)))
48        (lfun-name fun))))
49
50
51(defun bootstrapping-fmakunbound (name)
52  (when (consp name)
53    (unless (eq (%car name) 'setf)
54      (error "Function spec handler not loaded yet"))
55    (setq name (setf-function-name (cadr name))))
56  (%unfhave name)
57  name)
58
59;;; redefined in sysutils.
60(%fhave 'fmakunbound #'bootstrapping-fmakunbound)
61
62(defun bootstrapping-fset (name fn)
63  (fmakunbound name)
64  (%fhave name fn)
65  fn)
66
67;Redefined in sysutils.
68(%fhave 'fset #'bootstrapping-fset)
69
70(defun fset-symbol (name fn)
71  (fset (require-type name 'symbol) fn))
72
73
74(defun bootstrapping-record-source-file (fn &optional type)
75  (declare (ignore fn type))
76  nil)
77
78;Redefined in l1-utils.
79(%fhave 'record-source-file #'bootstrapping-record-source-file)
80
81
82(setq *fasload-print* nil)
83(setq *save-doc-strings* t)
84
85
86
87(%fhave '%defun-encapsulated-maybe ;Redefined in encapsulate
88        (qlfun bootstrapping-defun-encapsulated (name fn)
89          (declare (ignore name fn))
90          nil))
91
92(%fhave 'encapsulated-function-name  ;Redefined in encapsulate - used in l1-io
93        (qlfun bootstrapping-encapsulated-function-name (fn)
94          (declare (ignore fn))
95          nil))
96
97(%fhave 'set-function-info (qlfun set-function-info  (name info)
98                                  (if (typep info 'string)
99                                    (set-documentation name 'function info))
100                                  name))
101
102(defun %defun (named-fn &optional info)
103  (unless (typep named-fn 'function)
104    (dbg named-fn))
105  (let* ((name (function-name named-fn)))
106    (unless (and name
107                 (or (symbolp name)
108                     (setf-function-name-p name)))
109      (dbg named-fn))
110  (record-source-file name 'function)
111  (when (not (%defun-encapsulated-maybe name named-fn))
112    (when (and (symbolp name) (macro-function name nil))
113      (warn "The macro ~s is being redefined as a function." name)
114      (fmakunbound name))
115    (fset name named-fn))
116  (set-function-info name info)
117  (when *fasload-print* (format t "~&~S~%" name))
118  name))
119
120(defun validate-function-name (name)
121  (if (symbolp name)
122    name
123    (if (setf-function-name-p name)
124      (setf-function-name (cadr name))
125      (report-bad-arg name 'function-name))))
126
127;;;    There are three kinds of things which can go in the function
128;;;    cell of a symbol: 1) A function.  2) The thing which is the
129;;;    value of %unbound-function%: a 1-element vector whose 0th
130;;;    element is a code vector which causes an "undefined function"
131;;;    error to be signalled.  3) A macro or special-form definition,
132;;;    which is a 2-element vector whose 0th element is a code vector
133;;;    which signals a "can't apply macro or special form" error when
134;;;    executed and whose 1st element is a macro or special-operator
135;;;    name.  It doesn't matter what type of gvector cases 2 and 3
136;;;    are.  Once that's decided, it wouldn't hurt if %FHAVE
137;;;    typechecked its second arg.
138
139(defun %fhave (name def)
140  (let* ((fname (validate-function-name name)))
141    (setf (%svref (symptr->symvector (%symbol->symptr fname)) target::symbol.fcell-cell) def)))
142
143;;; FBOUNDP is true of any symbol whose function-cell contains something other
144;;; than %unbound-function%; we expect FBOUNDP to return that something.
145(defun fboundp (name)
146  "Return true if name has a global function definition."
147  (let* ((fname (validate-function-name name))
148         (def (%svref (symptr->symvector (%symbol->symptr fname)) target::symbol.fcell-cell)))
149    (unless (eq def %unbound-function%)
150      def)))
151
152;;; %UNFHAVE doesn't seem to want to deal with SETF names or function specs.
153;;; Who does ?
154
155(defun %unfhave (sym)
156  (let* ((symvec (symptr->symvector (%symbol->symptr sym)))
157         (old (%svref symvec target::symbol.fcell-cell))
158         (unbound %unbound-function%))
159    (setf (%svref symvec target::symbol.fcell-cell) unbound)
160    (not (eq old unbound))))
161
162;;; It's guaranteed that lfun-bits is a fixnum.  Might be a 30-bit fixnum ...
163
164
165
166
167
168(defun lfun-vector-name (fun &optional (new-name nil set-name-p))
169  (let* ((bits (lfun-bits fun)))
170    (declare (fixnum bits))
171    (if (and (logbitp $lfbits-gfn-bit bits)
172             (not (logbitp $lfbits-method-bit bits)))
173      (progn
174        (if set-name-p
175          (%gf-name fun new-name)
176          (%gf-name fun)))
177      (let* ((has-name-cell (not (logbitp $lfbits-noname-bit bits))))
178        (if has-name-cell
179          (let* ((lfv (lfun-vector fun))
180                 (name-idx (- (the fixnum (uvsize lfv)) 2))
181                 (old-name (%svref lfv name-idx)))
182            (declare (fixnum name-idx))
183            (if (and set-name-p (not (eq old-name new-name)))
184              (setf (%svref lfv name-idx) new-name))
185            old-name))))))
186
187(defun lfun-name (fun &optional (new-name nil set-name-p))
188  (multiple-value-bind (stored-name stored?) (lookup-lfun-name fun)
189    (unless stored?
190      (setq stored-name (lfun-vector-name fun)))
191    (when (and set-name-p (neq new-name stored-name))
192      (if (and stored? (eq new-name (lfun-vector-name fun)))
193        (remhash fun *lfun-names*)
194        (if (logbitp $lfbits-noname-bit (the fixnum (lfun-bits fun)))   ; no name-cell in function vector.
195          (puthash fun *lfun-names* new-name)
196          (lfun-vector-name fun new-name))))
197    stored-name))
198
199(defun lfun-bits (function &optional new)
200  (unless (functionp function)
201    (setq function (require-type function 'function)))
202  (let* ((lfv (lfun-vector function))
203         (idx (1- (the fixnum (uvsize lfv))))
204         (old (%svref lfv idx)))
205    (declare (fixnum idx))
206    (if new
207      (setf (%svref lfv idx) new))
208    old))
209   
210(defun %macro-have (symbol macro-function)
211  (declare (special %macro-code%))      ; magically set by xloader.
212  (%fhave symbol (vector %macro-code% macro-function)))
213
214
215(defun special-operator-p (symbol)
216  "If the symbol globally names a special form, return T, otherwise NIL."
217  (let ((def (fboundp symbol)))
218    (and (typep def 'simple-vector)
219         (not (lfunp (svref def 1))))))
220
221(defun special-form-p (x) (special-operator-p x))
222
223(defun setf-function-name-p (thing)
224  (and (consp thing)
225       (consp (%cdr thing))
226       (null (%cddr thing))
227       (eq (%car thing) 'setf)
228       (symbolp (%cadr thing))))
229
230(defun macro-function (form &optional env)
231  "If SYMBOL names a macro in ENV, returns the expansion function,
232   else returns NIL. If ENV is unspecified or NIL, use the global
233   environment only."
234  (setq form (require-type form 'symbol))
235  (when env
236    ; A definition-environment isn't a lexical environment, but it can
237    ; be an ancestor of one.
238    (unless (istruct-typep env 'lexical-environment)
239        (report-bad-arg env 'lexical-environment))
240      (let ((cell nil))
241        (tagbody
242          top
243          (if (setq cell (%cdr (assq form (lexenv.functions env))))
244            (return-from macro-function 
245              (if (eq (car cell) 'macro) (%cdr cell))))
246          (unless (listp (setq env (lexenv.parent-env env)))
247            (go top)))))
248      ; Not found in env, look in function cell.
249  (%global-macro-function form))
250
251(defun %fixnum-ref-macptr (fixnum &optional (offset 0))
252  (%int-to-ptr (%fixnum-ref-natural fixnum offset)))
253
254(defun %fixnum-set-macptr (fixnum offset &optional (newval offset newval-p))
255  (%fixnum-set-natural fixnum (if newval-p offset 0) (%ptr-to-int newval))
256  newval)
257
258;;; end of l0-def.lisp
Note: See TracBrowser for help on using the repository browser.