0.8.0.6:
[sbcl.git] / src / compiler / parse-lambda-list.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
9
10 (in-package "SB!C")
11
12 (/show0 "parse-lambda-list.lisp 12")
13
14 ;;; Break something like a lambda list (but not necessarily actually a
15 ;;; lambda list, e.g. the representation of argument types which is
16 ;;; used within an FTYPE specification) into its component parts. We
17 ;;; return twelve values:
18 ;;;  1. a list of the required args;
19 ;;;  2. a list of the &OPTIONAL arg specs;
20 ;;;  3. true if a &REST arg was specified;
21 ;;;  4. the &REST arg;
22 ;;;  5. true if &KEY args are present;
23 ;;;  6. a list of the &KEY arg specs;
24 ;;;  7. true if &ALLOW-OTHER-KEYS was specified.;
25 ;;;  8. true if any &AUX is present (new in SBCL vs. CMU CL);
26 ;;;  9. a list of the &AUX specifiers;
27 ;;; 10. true if a &MORE arg was specified;
28 ;;; 11. the &MORE context var;
29 ;;; 12. the &MORE count var;
30 ;;; 13. true if any lambda list keyword is present (only for
31 ;;;     PARSE-LAMBDA-LIST-LIKE-THING).
32 ;;;
33 ;;; The top level lambda list syntax is checked for validity, but the
34 ;;; arg specifiers are just passed through untouched. If something is
35 ;;; wrong, we use COMPILER-ERROR, aborting compilation to the last
36 ;;; recovery point.
37 (declaim (ftype (sfunction (list)
38                            (values list list boolean t boolean list boolean
39                                    boolean list boolean t t boolean))
40                 parse-lambda-list-like-thing))
41 (declaim (ftype (sfunction (list)
42                            (values list list boolean t boolean list boolean
43                                    boolean list boolean t t))
44                 parse-lambda-list))
45 (defun parse-lambda-list-like-thing (list)
46   (collect ((required)
47             (optional)
48             (keys)
49             (aux))
50     (let ((restp nil)
51           (rest nil)
52           (morep nil)
53           (more-context nil)
54           (more-count nil)
55           (keyp nil)
56           (auxp nil)
57           (allowp nil)
58           (state :required))
59       (declare (type (member :allow-other-keys :aux
60                              :key
61                              :more-context :more-count
62                              :optional
63                              :post-more :post-rest
64                              :required :rest)
65                      state))
66       (dolist (arg list)
67         (if (and (symbolp arg)
68                  (let ((name (symbol-name arg)))
69                    (and (plusp (length name))
70                         (char= (char name 0) #\&))))
71             (case arg
72               (&optional
73                (unless (eq state :required)
74                  (compiler-error "misplaced &OPTIONAL in lambda list: ~S"
75                                  list))
76                (setq state :optional))
77               (&rest
78                (unless (member state '(:required :optional))
79                  (compiler-error "misplaced &REST in lambda list: ~S" list))
80                (setq state :rest))
81               (&more
82                (unless (member state '(:required :optional))
83                  (compiler-error "misplaced &MORE in lambda list: ~S" list))
84                (setq morep t
85                      state :more-context))
86               (&key
87                (unless (member state
88                                '(:required :optional :post-rest :post-more))
89                  (compiler-error "misplaced &KEY in lambda list: ~S" list))
90                (setq keyp t
91                      state :key))
92               (&allow-other-keys
93                (unless (eq state ':key)
94                  (compiler-error "misplaced &ALLOW-OTHER-KEYS in ~
95                                   lambda list: ~S"
96                                  list))
97                (setq allowp t
98                      state :allow-other-keys))
99               (&aux
100                (when (member state '(:rest :more-context :more-count))
101                  (compiler-error "misplaced &AUX in lambda list: ~S" list))
102                (setq auxp t
103                      state :aux))
104               ;; FIXME: I don't think ANSI says this is an error. (It
105               ;; should certainly be good for a STYLE-WARNING,
106               ;; though.)
107               (t
108                (compiler-error "unknown &KEYWORD in lambda list: ~S" arg)))
109             (case state
110               (:required (required arg))
111               (:optional (optional arg))
112               (:rest
113                (setq restp t
114                      rest arg
115                      state :post-rest))
116               (:more-context
117                (setq more-context arg
118                      state :more-count))
119               (:more-count
120                (setq more-count arg
121                      state :post-more))
122               (:key (keys arg))
123               (:aux (aux arg))
124               (t
125                (compiler-error "found garbage in lambda list when expecting ~
126                                 a keyword: ~S"
127                                arg)))))
128       (when (eq state :rest)
129         (compiler-error "&REST without rest variable"))
130
131       (values (required) (optional) restp rest keyp (keys) allowp auxp (aux)
132               morep more-context more-count
133               (neq state :required)))))
134
135 ;;; like PARSE-LAMBDA-LIST-LIKE-THING, except our LAMBDA-LIST argument
136 ;;; really *is* a lambda list, not just a "lambda-list-like thing", so
137 ;;; can barf on things which're illegal as arguments in lambda lists
138 ;;; even if they could conceivably be legal in not-quite-a-lambda-list
139 ;;; weirdosities
140 (defun parse-lambda-list (lambda-list)
141
142   ;; Classify parameters without checking their validity individually.
143   (multiple-value-bind (required optional restp rest keyp keys allowp auxp aux
144                         morep more-context more-count)
145       (parse-lambda-list-like-thing lambda-list)
146
147     ;; Check validity of parameters.
148     (flet ((need-symbol (x why)
149              (unless (symbolp x)
150                (compiler-error "~A is not a symbol: ~S" why x))))
151       (dolist (i required)
152         (need-symbol i "Required argument"))
153       (dolist (i optional)
154         (typecase i
155           (symbol)
156           (cons
157            (destructuring-bind (var &optional init-form supplied-p) i
158              (declare (ignore init-form supplied-p))
159              (need-symbol var "&OPTIONAL parameter name")))
160           (t
161            (compiler-error "&OPTIONAL parameter is not a symbol or cons: ~S"
162                            i))))
163       (when restp
164         (need-symbol rest "&REST argument"))
165       (when keyp
166         (dolist (i keys)
167           (typecase i
168             (symbol)
169             (cons
170              (destructuring-bind (var-or-kv &optional init-form supplied-p) i
171                (declare (ignore init-form supplied-p))
172                (if (consp var-or-kv)
173                    (destructuring-bind (keyword-name var) var-or-kv
174                      (declare (ignore keyword-name))
175                      (need-symbol var "&KEY parameter name"))
176                    (need-symbol var-or-kv "&KEY parameter name"))))
177             (t
178              (compiler-error "&KEY parameter is not a symbol or cons: ~S"
179                              i))))))
180
181     ;; Voila.
182     (values required optional restp rest keyp keys allowp auxp aux
183             morep more-context more-count)))
184
185 (/show0 "parse-lambda-list.lisp end of file")