Eliminate style-warning about undefined type GLOBAL-VAR
[sbcl.git] / src / compiler / early-c.lisp
blobdcecc2eaa8246d443c96ba57377716ee37d850ad
1 ;;;; This file contains compiler code and compiler-related stuff which
2 ;;;; can be built early on. Some of the stuff may be here because it's
3 ;;;; needed early on, some other stuff (e.g. constants) just because
4 ;;;; it might as well be done early so we don't have to think about
5 ;;;; whether it's done early enough.
7 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; more information.
9 ;;;;
10 ;;;; This software is derived from the CMU CL system, which was
11 ;;;; written at Carnegie Mellon University and released into the
12 ;;;; public domain. The software is in the public domain and is
13 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
14 ;;;; files for more information.
16 (in-package "SB!C")
18 ;;; ANSI limits on compilation
19 (def!constant sb!xc:call-arguments-limit sb!xc:most-positive-fixnum
20 #!+sb-doc
21 "The exclusive upper bound on the number of arguments which may be passed
22 to a function, including &REST args.")
23 (def!constant sb!xc:lambda-parameters-limit sb!xc:most-positive-fixnum
24 #!+sb-doc
25 "The exclusive upper bound on the number of parameters which may be specified
26 in a given lambda list. This is actually the limit on required and &OPTIONAL
27 parameters. With &KEY and &AUX you can get more.")
28 (def!constant sb!xc:multiple-values-limit sb!xc:most-positive-fixnum
29 #!+sb-doc
30 "The exclusive upper bound on the number of multiple VALUES that you can
31 return.")
33 ;;;; cross-compiler-only versions of CL special variables, so that we
34 ;;;; don't have weird interactions with the host compiler
36 (defvar sb!xc:*compile-file-pathname*)
37 (defvar sb!xc:*compile-file-truename*)
38 (defvar sb!xc:*compile-print*)
39 (defvar sb!xc:*compile-verbose*)
41 ;;;; miscellaneous types used both in the cross-compiler and on the target
43 ;;;; FIXME: The INDEX and LAYOUT-DEPTHOID definitions probably belong
44 ;;;; somewhere else, not "early-c", since they're after all not part
45 ;;;; of the compiler.
47 ;;; the type of LAYOUT-DEPTHOID slot values
48 (def!type layout-depthoid () '(or index (integer -1 -1)))
50 ;;; An INLINEP value describes how a function is called. The values
51 ;;; have these meanings:
52 ;;; NIL No declaration seen: do whatever you feel like, but don't
53 ;;; dump an inline expansion.
54 ;;; :NOTINLINE NOTINLINE declaration seen: always do full function call.
55 ;;; :INLINE INLINE declaration seen: save expansion, expanding to it
56 ;;; if policy favors.
57 ;;; :MAYBE-INLINE
58 ;;; Retain expansion, but only use it opportunistically.
59 ;;; :MAYBE-INLINE is quite different from :INLINE. As explained
60 ;;; by APD on #lisp 2005-11-26: "MAYBE-INLINE lambda is
61 ;;; instantiated once per component, INLINE - for all
62 ;;; references (even under #'without FUNCALL)."
63 (deftype inlinep ()
64 '(member :inline :maybe-inline :notinline nil))
65 (defparameter *inlinep-translations*
66 '((inline . :inline)
67 (notinline . :notinline)
68 (maybe-inline . :maybe-inline)))
70 ;;; *FREE-VARS* translates from the names of variables referenced
71 ;;; globally to the LEAF structures for them. *FREE-FUNS* is like
72 ;;; *FREE-VARS*, only it deals with function names.
73 (defvar *free-vars*)
74 (defvar *free-funs*)
75 (declaim (type hash-table *free-vars* *free-funs*))
77 ;;; We use the same CONSTANT structure to represent all equal anonymous
78 ;;; constants. This hashtable translates from constants to the LEAFs that
79 ;;; represent them.
80 (defvar *constants*)
81 (declaim (type hash-table *constants*))
83 ;;; *ALLOW-INSTRUMENTING* controls whether we should allow the
84 ;;; insertion of instrumenting code (like a (CATCH ...)) around code
85 ;;; to allow the debugger RETURN and STEP commands to function (we
86 ;;; disallow it for internal stuff).
87 (defvar *allow-instrumenting*)
89 ;;; miscellaneous forward declarations
90 (defvar *code-segment*)
91 #!+sb-dyncount (defvar *collect-dynamic-statistics*)
92 (defvar *component-being-compiled*)
93 (defvar *compiler-error-context*)
94 (defvar *compiler-error-count*)
95 (defvar *compiler-warning-count*)
96 (defvar *compiler-style-warning-count*)
97 (defvar *compiler-note-count*)
98 (defvar *compiler-trace-output*)
99 (defvar *constraint-universe*)
100 (defvar *current-path*)
101 (defvar *current-component*)
102 (defvar *delayed-ir1-transforms*)
103 (defvar *eval-tlf-index*)
104 (defvar *dynamic-counts-tn*)
105 (defvar *elsewhere*)
106 (defvar *event-info*)
107 (defvar *event-note-threshold*)
108 (defvar *failure-p*)
109 (defvar *fixup-notes*)
110 #!+inline-constants
111 (progn
112 (defvar *constant-segment*)
113 (defvar *constant-table*)
114 (defvar *constant-vector*))
115 (defvar *source-info*)
116 (defvar *source-plist*)
117 (defvar *source-namestring*)
118 (defvar *undefined-warnings*)
119 (defvar *warnings-p*)
120 (defvar *lambda-conversions*)
122 (defvar *stack-allocate-dynamic-extent* t
123 #!+sb-doc
124 "If true (the default), the compiler respects DYNAMIC-EXTENT declarations
125 and stack allocates otherwise inaccessible parts of the object whenever
126 possible. Potentially long (over one page in size) vectors are, however, not
127 stack allocated except in zero SAFETY code, as such a vector could overflow
128 the stack without triggering overflow protection.")
130 (!begin-collecting-cold-init-forms)
131 ;;; This lock is seized in the compiler, and related areas -- like the
132 ;;; classoid/layout/class system.
133 (defglobal **world-lock** nil)
134 (!cold-init-forms
135 (setf **world-lock** (sb!thread:make-mutex :name "World Lock")))
136 (!defun-from-collected-cold-init-forms !world-lock-cold-init)
138 (defmacro with-world-lock (() &body body)
139 `(sb!thread:with-recursive-lock (**world-lock**)
140 ,@body))
142 (declaim (type fixnum *compiler-sset-counter*))
143 (defvar *compiler-sset-counter* 0)
145 ;;; unique ID for the next object created (to let us track object
146 ;;; identity even across GC, useful for understanding weird compiler
147 ;;; bugs where something is supposed to be unique but is instead
148 ;;; exists as duplicate objects)
149 #!+sb-show
150 (progn
151 (defvar *object-id-counter* 0)
152 (defun new-object-id ()
153 (prog1
154 *object-id-counter*
155 (incf *object-id-counter*))))
157 ;;;; miscellaneous utilities
159 ;;; This is for "observers" who want to know if type names have been added.
160 ;;; Rather than registering listeners, they can detect changes by comparing
161 ;;; their stored nonce to the current nonce. Additionally the observers
162 ;;; can detect whether function definitions have occurred.
163 (declaim (fixnum *type-cache-nonce*))
164 (!defglobal *type-cache-nonce* 0)
166 (def!struct (undefined-warning
167 #-no-ansi-print-object
168 (:print-object (lambda (x s)
169 (print-unreadable-object (x s :type t)
170 (prin1 (undefined-warning-name x) s))))
171 (:copier nil))
172 ;; the name of the unknown thing
173 (name nil :type (or symbol list))
174 ;; the kind of reference to NAME
175 (kind (missing-arg) :type (member :function :type :variable))
176 ;; the number of times this thing was used
177 (count 0 :type unsigned-byte)
178 ;; a list of COMPILER-ERROR-CONTEXT structures describing places
179 ;; where this thing was used. Note that we only record the first
180 ;; *UNDEFINED-WARNING-LIMIT* calls.
181 (warnings () :type list))
183 ;;; Delete any undefined warnings for NAME and KIND. This is for the
184 ;;; benefit of the compiler, but it's sometimes called from stuff like
185 ;;; type-defining code which isn't logically part of the compiler.
186 (declaim (ftype (function ((or symbol cons) keyword) (values))
187 note-name-defined))
188 (defun note-name-defined (name kind)
189 #-sb-xc-host (atomic-incf *type-cache-nonce*)
190 ;; We do this BOUNDP check because this function can be called when
191 ;; not in a compilation unit (as when loading top level forms).
192 (when (boundp '*undefined-warnings*)
193 (setq *undefined-warnings*
194 (delete-if (lambda (x)
195 (and (equal (undefined-warning-name x) name)
196 (eq (undefined-warning-kind x) kind)))
197 *undefined-warnings*)))
198 (values))
200 ;;; to be called when a variable is lexically bound
201 (declaim (ftype (function (symbol) (values)) note-lexical-binding))
202 (defun note-lexical-binding (symbol)
203 ;; This check is intended to protect us from getting silently
204 ;; burned when we define
205 ;; foo.lisp:
206 ;; (DEFVAR *FOO* -3)
207 ;; (DEFUN FOO (X) (+ X *FOO*))
208 ;; bar.lisp:
209 ;; (DEFUN BAR (X)
210 ;; (LET ((*FOO* X))
211 ;; (FOO 14)))
212 ;; and then we happen to compile bar.lisp before foo.lisp.
213 (when (looks-like-name-of-special-var-p symbol)
214 ;; FIXME: should be COMPILER-STYLE-WARNING?
215 (style-warn 'asterisks-around-lexical-variable-name
216 :format-control
217 "using the lexical binding of the symbol ~
218 ~/sb-impl::print-symbol-with-prefix/, not the~@
219 dynamic binding"
220 :format-arguments (list symbol)))
221 (values))
223 (def!struct (debug-name-marker (:make-load-form-fun dump-debug-name-marker)
224 (:print-function print-debug-name-marker)))
226 (defvar *debug-name-level* 4)
227 (defvar *debug-name-length* 12)
228 (defvar *debug-name-punt*)
229 (defvar *debug-name-sharp*)
230 (defvar *debug-name-ellipsis*)
232 (eval-when (#-sb-xc :compile-toplevel :load-toplevel :execute)
233 (defun dump-debug-name-marker (marker &optional env)
234 (declare (ignore env))
235 (cond ((eq marker *debug-name-sharp*)
236 `(if (boundp '*debug-name-sharp*)
237 *debug-name-sharp*
238 (make-debug-name-marker)))
239 ((eq marker *debug-name-ellipsis*)
240 `(if (boundp '*debug-name-ellipsis*)
241 *debug-name-ellipsis*
242 (make-debug-name-marker)))
244 (warn "Dumping unknown debug-name marker.")
245 '(make-debug-name-marker)))))
247 (defun print-debug-name-marker (marker stream level)
248 (declare (ignore level))
249 (cond ((eq marker *debug-name-sharp*)
250 (write-char #\# stream))
251 ((eq marker *debug-name-ellipsis*)
252 (write-string "..." stream))
254 (write-string "???" stream))))
256 (setf *debug-name-sharp* (make-debug-name-marker)
257 *debug-name-ellipsis* (make-debug-name-marker))
259 (declaim (ftype (sfunction () list) name-context))
260 (defun debug-name (type thing &optional context)
261 (let ((*debug-name-punt* nil))
262 (labels ((walk (x)
263 (typecase x
264 (cons
265 (if (plusp *debug-name-level*)
266 (let ((*debug-name-level* (1- *debug-name-level*)))
267 (do ((tail (cdr x) (cdr tail))
268 (name (cons (walk (car x)) nil)
269 (cons (walk (car tail)) name))
270 (n (1- *debug-name-length*) (1- n)))
271 ((or (not (consp tail))
272 (not (plusp n))
273 *debug-name-punt*)
274 (cond (*debug-name-punt*
275 (setf *debug-name-punt* nil)
276 (nreverse name))
277 ((atom tail)
278 (nconc (nreverse name) (walk tail)))
280 (setf *debug-name-punt* t)
281 (nconc (nreverse name) (list *debug-name-ellipsis*)))))))
282 *debug-name-sharp*))
283 ((or symbol number string)
286 (type-of x)))))
287 (let ((name (list* type (walk thing) (when context (name-context)))))
288 (when (legal-fun-name-p name)
289 (bug "~S is a legal function name, and cannot be used as a ~
290 debug name." name))
291 name))))