7f60223f26fea39780baf93adbe85d48e0761631
[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 (member arg sb!xc:lambda-list-keywords)
68             (case arg
69               (&optional
70                (unless (eq state :required)
71                  (compiler-error "misplaced &OPTIONAL in lambda list: ~S"
72                                  list))
73                (setq state :optional))
74               (&rest
75                (unless (member state '(:required :optional))
76                  (compiler-error "misplaced &REST in lambda list: ~S" list))
77                (setq state :rest))
78               (&more
79                (unless (member state '(:required :optional))
80                  (compiler-error "misplaced &MORE in lambda list: ~S" list))
81                (setq morep t
82                      state :more-context))
83               (&key
84                (unless (member state
85                                '(:required :optional :post-rest :post-more))
86                  (compiler-error "misplaced &KEY in lambda list: ~S" list))
87                #-sb-xc-host
88                (when (optional)
89                  (compiler-style-warn
90                   "&OPTIONAL and &KEY found in the same lambda list: ~S" list))
91                (setq keyp t
92                      state :key))
93               (&allow-other-keys
94                (unless (eq state ':key)
95                  (compiler-error "misplaced &ALLOW-OTHER-KEYS in ~
96                                   lambda list: ~S"
97                                  list))
98                (setq allowp t
99                      state :allow-other-keys))
100               (&aux
101                (when (member state '(:rest :more-context :more-count))
102                  (compiler-error "misplaced &AUX in lambda list: ~S" list))
103                (when auxp
104                  (compiler-error "multiple &AUX in lambda list: ~S" list))
105                (setq auxp t
106                      state :aux))
107               (t
108                ;; It could be argued that &WHOLE and friends would be
109                ;; just ordinary variables in an ordinary lambda-list,
110                ;; but since (1) that seem exceedingly to have been the
111                ;; programmers intent and (2) the spec can be
112                ;; interpreted as giving as licence to signal an
113                ;; error[*] that is what we do.
114                ;;
115                ;; [* All lambda list keywords used in the
116                ;; implementation appear in LAMBDA-LIST-KEYWORDS. Each
117                ;; member of a lambda list is either a parameter
118                ;; specifier ot a lambda list keyword. Ergo, symbols
119                ;; appearing in LAMBDA-LIST-KEYWORDS cannot be
120                ;; parameter specifiers.]
121                (compiler-error 'simple-program-error
122                                :format-control "Bad lambda list keyword ~S in: ~S"
123                                :format-arguments (list arg list))))
124             (progn
125               (when (symbolp arg)
126                 (let ((name (symbol-name arg)))
127                   (when (and (plusp (length name))
128                              (char= (char name 0) #\&))
129                     (style-warn
130                      "suspicious variable in lambda list: ~S." arg))))
131               (case state
132                 (:required (required arg))
133                 (:optional (optional arg))
134                 (:rest
135                  (setq restp t
136                        rest arg
137                        state :post-rest))
138                 (:more-context
139                  (setq more-context arg
140                        state :more-count))
141                 (:more-count
142                  (setq more-count arg
143                        state :post-more))
144                 (:key (keys arg))
145                 (:aux (aux arg))
146                 (t
147                  (compiler-error "found garbage in lambda list when expecting ~
148                                   a keyword: ~S"
149                                  arg))))))
150       (when (eq state :rest)
151         (compiler-error "&REST without rest variable"))
152
153       (values (required) (optional) restp rest keyp (keys) allowp auxp (aux)
154               morep more-context more-count
155               (neq state :required)))))
156
157 ;;; like PARSE-LAMBDA-LIST-LIKE-THING, except our LAMBDA-LIST argument
158 ;;; really *is* a lambda list, not just a "lambda-list-like thing", so
159 ;;; can barf on things which're illegal as arguments in lambda lists
160 ;;; even if they could conceivably be legal in not-quite-a-lambda-list
161 ;;; weirdosities
162 (defun parse-lambda-list (lambda-list)
163   ;; Classify parameters without checking their validity individually.
164   (multiple-value-bind (required optional restp rest keyp keys allowp auxp aux
165                         morep more-context more-count)
166       (parse-lambda-list-like-thing lambda-list)
167
168     ;; Check validity of parameters.
169     (flet ((need-symbol (x why)
170              (unless (symbolp x)
171                (compiler-error "~A is not a symbol: ~S" why x))))
172       (dolist (i required)
173         (need-symbol i "Required argument"))
174       (dolist (i optional)
175         (typecase i
176           (symbol)
177           (cons
178            (destructuring-bind (var &optional init-form supplied-p) i
179              (declare (ignore init-form supplied-p))
180              (need-symbol var "&OPTIONAL parameter name")))
181           (t
182            (compiler-error "&OPTIONAL parameter is not a symbol or cons: ~S"
183                            i))))
184       (when restp
185         (need-symbol rest "&REST argument"))
186       (when keyp
187         (dolist (i keys)
188           (typecase i
189             (symbol)
190             (cons
191              (destructuring-bind (var-or-kv &optional init-form supplied-p) i
192                (declare (ignore init-form supplied-p))
193                (if (consp var-or-kv)
194                    (destructuring-bind (keyword-name var) var-or-kv
195                      (declare (ignore keyword-name))
196                      (need-symbol var "&KEY parameter name"))
197                    (need-symbol var-or-kv "&KEY parameter name"))))
198             (t
199              (compiler-error "&KEY parameter is not a symbol or cons: ~S"
200                              i))))))
201
202     ;; Voila.
203     (values required optional restp rest keyp keys allowp auxp aux
204             morep more-context more-count)))
205
206 (/show0 "parse-lambda-list.lisp end of file")