+
+(defvar *null-lexenv* (make-null-lexenv))
+
+(defun augment-environment
+ (env &key variable symbol-macro function macro declare)
+ "Create a new lexical environment by augmenting ENV with new information.
+
+ VARIABLE
+ is a list of symbols to introduce as new variable bindings.
+
+ SYMBOL-MACRO
+ is a list symbol macro bindings of the form (name definition).
+
+ MACRO
+ is a list of macro definitions of the form (name definition), where
+ definition is a function of two arguments (a form and an environment).
+
+ FUNCTION
+ is a list of symbols to introduce as new local function bindings.
+
+ DECLARE
+ is a list of declaration specifiers. Declaration specifiers attach to the
+ new variable or function bindings as if they appeared in let, let*, flet
+ or labels form. For example:
+
+ (augment-environment env :variable '(x) :declare '((special x)))
+
+ is like
+
+ (let (x) (declare (special x)) ....)
+
+ but
+
+ (augment-environment (augment-environment env :variable '(x))
+ :declare '((special x)))
+
+ is like
+
+ (let (x) (locally (declare (special x))) ...)
+"
+ (collect ((lvars)
+ (clambdas))
+ (unless (or variable symbol-macro function macro declare)
+ (return-from augment-environment env))
+
+ (if (null env)
+ (setq env (make-null-lexenv))
+ (setq env (copy-structure env)))
+
+ ;; a null policy is used to identify a null lexenv
+ (when (sb-c::null-lexenv-p env)
+ (setf (sb-c::lexenv-%policy env) sb-c::*policy*))
+
+ (when macro
+ (setf (sb-c::lexenv-funs env)
+ (nconc
+ (loop for (name def) in macro
+ collect (cons name (cons 'sb-sys::macro def)))
+ (sb-c::lexenv-funs env))))
+
+ (when symbol-macro
+ (setf (sb-c::lexenv-vars env)
+ (nconc
+ (loop for (name def) in symbol-macro
+ collect (cons name (cons 'sb-sys::macro def)))
+ (sb-c::lexenv-vars env))))
+
+ (dolist (name variable)
+ (lvars (sb-c::make-lambda-var :%source-name name)))
+
+ (dolist (name function)
+ (clambdas
+ (sb-c::make-lambda
+ :lexenv *null-lexenv*
+ :%source-name name
+ :allow-instrumenting nil)))
+
+ (when declare
+ ;; process-decls looks in *lexenv* policy to decide what warnings to print
+ (let ((*lexenv* *null-lexenv*))
+ (setq env (sb-c::process-decls
+ (list `(declare ,@declare))
+ (lvars) (clambdas) :lexenv env :context nil))))
+
+ (when function
+ (setf (sb-c::lexenv-funs env)
+ (nconc
+ (loop for name in function for lambda in (clambdas)
+ collect (cons name lambda))
+ (sb-c::lexenv-funs env))))
+
+ (when variable
+ (setf (sb-c::lexenv-vars env)
+ (nconc
+ (loop for name in variable for lvar in (lvars)
+ collect
+ (cons name
+ ;; If one of the lvars is declared special then
+ ;; process-decls will set it's specvar.
+ (if (sb-c::lambda-var-specvar lvar)
+ (sb-c::lambda-var-specvar lvar)
+ lvar)))
+ (sb-c::lexenv-vars env))))
+
+ env))
+
+;;; Retrieve the user-supplied (from define-declaration) pairs for a
+;;; function or a variable from a lexical environment.
+;;;
+;;; KEYWORD should be :function or :variable, VAR should be a
+;;; function or variable name, respectively.
+(defun extra-pairs (keyword var binding env)
+ (when env
+ (let ((ret nil))
+ (dolist (entry (sb-c::lexenv-user-data env))
+ (destructuring-bind
+ (entry-keyword entry-var entry-binding &rest entry-cons)
+ entry
+ (when (and (eq keyword entry-keyword)
+ (typecase binding
+ (sb-c::global-var
+ (and (eq var entry-var)
+ (typecase entry-binding
+ (sb-c::global-var t)
+ (sb-c::lambda-var
+ (sb-c::lambda-var-specvar entry-binding))
+ (null t)
+ (t nil))))
+ (t
+ (eq binding entry-binding))))
+ (push entry-cons ret))))
+ (nreverse ret))))
+
+;;; Retrieve the user-supplied (from define-declaration) value for
+;;; the declaration with the given NAME
+(defun extra-decl-info (name env)
+ (when env
+ (dolist (entry (sb-c::lexenv-user-data env))
+ (when (and (eq :declare (car entry))
+ (eq name (cadr entry)))
+ (return-from extra-decl-info (cddr entry))))
+ nil))
+
+
+(declaim (ftype (sfunction ((or symbol cons) &optional (or null lexenv))
+ (values (member nil :function :macro :special-form)
+ boolean
+ list))
+ function-information))
+(defun function-information (name &optional env)
+ "Return information about the function NAME in the lexical environment ENV.
+Note that the global function binding may differ from the local one.
+
+This function returns three values. The first indicates the type of
+function definition or binding:
+
+ NIL
+ There is no apparent definition for NAME.
+
+ :FUNCTION
+ NAME refers to a function.
+
+ :MACRO
+ NAME refers to a macro.
+
+ :SPECIAL-FORM
+ NAME refers to a special operator. If the name refers to both a
+ macro and a special operator, the macro takes precedence.
+
+The second value is true if NAME is bound locally.
+
+The third value is an alist describing the declarations that apply to
+the function NAME. Standard declaration specifiers that may appear in
+CARS of the alist include:
+
+ DYNAMIC-EXTENT
+ If the CDR is T, NAME has been declared DYNAMIC-EXTENT. If the CDR
+ is NIL, the alist element may be omitted.
+
+ INLINE
+ The CDR is one of the symbols INLINE, NOTINLINE, or NIL, to
+ indicate if the function has been declared INLINE or NOTINLINE. If
+ the CDR is NIL the alist element may be omitted.
+
+ FTYPE
+ The CDR is the type specifier associated with NAME, or the symbol
+ FUNCTION if there is functional type declaration or proclamation
+ associated with NAME. If the CDR is FUNCTION the alist element may
+ be omitted.
+
+In addition to these declarations defined using DEFINE-DECLARATION may
+appear."
+ (let* ((*lexenv* (or env (make-null-lexenv)))
+ (fun (lexenv-find name funs))
+ binding localp ftype dx inlinep)
+ (etypecase fun
+ (sb-c::leaf
+ (let ((env-type (or (lexenv-find fun type-restrictions)
+ *universal-fun-type*)))
+ (setf binding :function
+ ftype (type-intersection (sb-c::leaf-type fun) env-type)
+ dx (sb-c::leaf-dynamic-extent fun))
+ (etypecase fun
+ (sb-c::functional
+ (setf localp t
+ inlinep (sb-c::functional-inlinep fun)))
+ (sb-c::defined-fun
+ ;; Inlined known functions.
+ (setf localp nil
+ inlinep (sb-c::defined-fun-inlinep fun))))))
+ (cons
+ (setf binding :macro
+ localp t))
+ (null
+ (case (info :function :kind name)
+ (:macro
+ (setf binding :macro
+ localp nil))
+ (:special-form
+ (setf binding :special-form
+ localp nil))
+ (:function
+ (setf binding :function
+ localp nil
+ ftype (when (eq :declared (info :function :where-from name))
+ (info :function :type name))
+ inlinep (info :function :inlinep name))))))
+ (values binding
+ localp
+ (let (alist)
+ (when (and ftype (neq *universal-fun-type* ftype))
+ (push (cons 'ftype (type-specifier ftype)) alist))
+ (ecase inlinep
+ ((:inline :maybe-inline) (push (cons 'inline 'inline) alist))
+ (:notinline (push (cons 'inline 'notinline) alist))
+ ((nil)))
+ (when dx (push (cons 'dynamic-extent t) alist))
+ (append alist (extra-pairs :function name fun *lexenv*))))))
+
+
+