Debian Codegraph @

src/cmp/cmpenv-declare.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8
9 ;;;;
10 ;;;; CMPENV-DECLARE -- Declarations for the compiler
11 ;;;;
12 ;;;; Extract, process and incorporate declarations into the compiler
13 ;;;; environment. Unlike proclamations, these are local to the current
14 ;;;; compiled file and do not propagate beyond it.
15 ;;;;
16
17 (in-package "COMPILER")
18
19 (defun valid-form-p (x &optional test)
20 (and (si:proper-list-p x)
21 (or (null test)
22 (every test x))))
23
24 (defun type-name-p (name)
25 (or (cmp-env-search-type name *cmp-env* nil)
26 (si:get-sysprop name 'SI::DEFTYPE-DEFINITION)
27 (find-class name nil)
28 (si:get-sysprop name 'SI::STRUCTURE-TYPE)))
29
30 (defun validate-alien-declaration (names-list error)
31 (dolist (new-declaration names-list)
32 (unless (symbolp new-declaration)
33 (funcall error "The declaration ~s is not a symbol" new-declaration))
34 (when (type-name-p new-declaration)
35 (funcall error "Symbol name ~S cannot be both the name of a type and of a declaration"
36 new-declaration))))
37
38 (defun alien-declaration-p (name &optional (env *cmp-env*))
39 (and (symbolp name)
40 (member name (cmp-env-search-declaration 'alien env si:*alien-declarations*)
41 :test 'eq)))
42
43 (defun policy-declaration-p (name)
44 (and (gethash name *optimization-quality-switches*) t))
45
46 (defun parse-ignore-declaration (decl-args expected-ref-number tail)
47 (declare (si::c-local))
48 (loop for name in decl-args
49 do (if (symbolp name)
50 (push (cons name expected-ref-number) tail)
51 (cmpassert (and (consp name)
52 (= (length name) 2)
53 (eq (first name) 'function))
54 "Invalid argument to IGNORE/IGNORABLE declaration:~&~A"
55 name)))
56 tail)
57
58 (defun collect-declared (type var-list tail)
59 (declare (si::c-local))
60 (cmpassert (valid-form-p var-list #'symbolp)
61 "Syntax error in declaration ~s" `(TYPE ,type ,var-list))
62 (loop for var-name in var-list
63 do (push (cons var-name type) tail))
64 tail)
65
66 (defun c1body (body doc-p)
67 "Split a function body into a list of forms, a set of declarations,
68 and a possible documentation string (only accepted when DOC-P is true)."
69 (multiple-value-bind (all-declarations body doc specials)
70 (si:process-declarations body doc-p)
71 (loop with others = '()
72 with types = '()
73 with ignored = '()
74 for decl in all-declarations
75 for decl-name = (first decl)
76 for decl-args = (rest decl)
77 do (cmpassert (and (valid-form-p decl-args)
78 (or (symbolp decl-name)
79 (and (consp decl-name)
80 (valid-type-specifier decl-name))))
81 "Syntax error in declaration ~s" decl)
82 do (case decl-name
83 (cl:SPECIAL)
84 (cl:IGNORE
85 (cmpassert (valid-form-p decl-args)
86 "Syntax error in declaration ~s" decl)
87 (setf ignored (parse-ignore-declaration decl-args -1 ignored)))
88 (cl:IGNORABLE
89 (cmpassert (valid-form-p decl-args)
90 "Syntax error in declaration ~s" decl)
91 (setf ignored (parse-ignore-declaration decl-args 0 ignored)))
92 (cl:TYPE
93 (cmpassert (and (consp decl-args)
94 (valid-form-p (rest decl-args) #'symbolp))
95 "Syntax error in declaration ~s" decl)
96 (setf types (collect-declared (first decl-args)
97 (rest decl-args)
98 types)))
99 (OBJECT
100 (cmpassert (valid-form-p decl-args #'symbolp)
101 "Syntax error in declaration ~s" decl)
102 (setf types (collect-declared 'OBJECT decl-args types)))
103 ((cl:OPTIMIZE cl:FTYPE cl:INLINE cl:NOTINLINE cl:DECLARATION SI::C-LOCAL
104 SI::C-GLOBAL cl:DYNAMIC-EXTENT cl:VALUES
105 SI::NO-CHECK-TYPE POLICY-DEBUG-IHS-FRAME :READ-ONLY)
106 (push decl others))
107 (SI:FUNCTION-BLOCK-NAME)
108 (otherwise
109 (if (or (alien-declaration-p decl-name)
110 (policy-declaration-p decl-name))
111 (push decl others)
112 (multiple-value-bind (ok type)
113 (if (machine-c-type-p decl-name)
114 (values t decl-name)
115 (valid-type-specifier decl-name))
116 (if (null ok)
117 (cmpwarn "Unknown declaration specifier ~s." decl-name)
118 (setf types (collect-declared type decl-args types)))))))
119 finally (return (values body specials types ignored
120 (nreverse others) doc all-declarations)))))
121
122 (defun add-one-declaration (env decl)
123 "Add to the environment one declarations which is not type, ignorable or
124 special variable declarations, as these have been extracted before."
125 (case (car decl)
126 (cl:OPTIMIZE
127 (cmp-env-add-optimizations (rest decl) env))
128 (POLICY-DEBUG-IHS-FRAME
129 (let ((flag (or (rest decl) '(t))))
130 (if *current-function*
131 (progn
132 (cmp-env-add-declaration 'policy-debug-ihs-frame flag
133 (fun-cmp-env *current-function*))
134 env)
135 (cmp-env-add-declaration 'policy-debug-ihs-frame
136 flag env))))
137 (cl:FTYPE
138 (if (atom (rest decl))
139 (cmpwarn "Syntax error in declaration ~a" decl)
140 (multiple-value-bind (type-name args)
141 (si::normalize-type (second decl) env)
142 (if (eq type-name 'FUNCTION)
143 (dolist (v (cddr decl))
144 (setf env (add-function-declaration v args env)))
145 (cmpwarn "In an FTYPE declaration, found ~A which is not a function type."
146 (second decl)))))
147 env)
148 (cl:INLINE
149 (loop for name in (rest decl) do (setf env (declare-inline name env)))
150 env)
151 (cl:NOTINLINE
152 (loop for name in (rest decl) do (setf env (declare-notinline name env)))
153 env)
154 (cl:DECLARATION
155 (validate-alien-declaration (rest decl) #'cmperr)
156 (cmp-env-extend-declaration 'alien (rest decl) env si:*alien-declarations*))
157 ((SI::C-LOCAL SI::C-GLOBAL SI::NO-CHECK-TYPE :READ-ONLY)
158 env)
159 ((cl:DYNAMIC-EXTENT cl:IGNORABLE SI:FUNCTION-BLOCK-NAME)
160 ;; FIXME! SOME ARE IGNORED!
161 env)
162 (otherwise
163 (cond ((alien-declaration-p (first decl) env)
164 env)
165 ((maybe-add-policy decl env))
166 (t
167 (cmpwarn "Unknown declaration specifier ~s" (first decl))
168 env)))))
169
170 (defun symbol-macro-declaration-p (name type)
171 (ext:when-let ((record (cmp-env-search-symbol-macro name)))
172 (let* ((expression (funcall record name nil)))
173 (cmp-env-register-symbol-macro name `(the ,type ,expression)))
174 t))
175
176 (defun check-vdecl (vnames ts is)
177 (loop for (name . type) in ts
178 unless (or (member name vnames :test #'eq)
179 (symbol-macro-declaration-p name type)
180 (cmp-env-search-var name))
181 do (cmpwarn "Declaration of type~&~4T~A~&was found for not bound variable ~s."
182 type name))
183 (loop for (name . expected-uses) in is
184 unless (or (member name vnames :test #'eq)
185 (cmp-env-search-symbol-macro name)
186 (cmp-env-search-macro name))
187 do (cmpwarn (if (minusp expected-uses)
188 "IGNORE declaration was found for not bound variable ~s."
189 "IGNORABLE declaration was found for not bound variable ~s.")
190 name)))