function-name.lisp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ;; Copyright (c) 2004 Sean Ross
  2. ;; All rights reserved.
  3. ;; Redistribution and use in source and binary forms, with or without
  4. ;; modification, are permitted provided that the following conditions
  5. ;; are met:
  6. ;; 1. Redistributions of source code must retain the above copyright
  7. ;; notice, this list of conditions and the following disclaimer.
  8. ;; 2. Redistributions in binary form must reproduce the above copyright
  9. ;; notice, this list of conditions and the following disclaimer in the
  10. ;; documentation and/or other materials provided with the distribution.
  11. ;; 3. The names of the authors and contributors may not be used to endorse
  12. ;; or promote products derived from this software without specific prior
  13. ;; written permission.
  14. ;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
  15. ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
  18. ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. ;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. ;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. ;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. ;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. ;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. ;; SUCH DAMAGE.
  25. (in-package :cl-i18n)
  26. ;; Function storing hack.
  27. ;; This just stores the function name if we can find it
  28. ;; or signal a store-error.
  29. (defun parse-name (name)
  30. (let ((name (subseq name 21)))
  31. (declare (type simple-string name))
  32. (if (search name "SB!" :end1 3)
  33. (replace name "SB-" :end1 3)
  34. name)))
  35. #+sbcl
  36. (defvar *sbcl-readtable* (copy-readtable nil))
  37. #+sbcl
  38. (set-macro-character #\# #'(lambda (c s)
  39. (declare (ignore c s))
  40. "Invalid character in function name.")
  41. nil
  42. *sbcl-readtable*)
  43. (defun get-function-name (obj)
  44. (multiple-value-bind (l cp name) (function-lambda-expression obj)
  45. (declare (ignore l cp))
  46. (cond #+sbcl
  47. ;; handle (SB-C::&OPTIONAL-DISPATCH MAKE-FOO) names introduced around 1.0.15
  48. ((and name (consp name) (not (cddr name)) (eql (first name) (find-symbol "&OPTIONAL-DISPATCH" :sb-c)))
  49. (second name))
  50. ;; normal names and (setf name)
  51. ((and name (or (symbolp name) (consp name))) name)
  52. ;; Try to deal with sbcl's naming convention
  53. ;; of built in functions (pre 0.9)
  54. #+sbcl
  55. ((and name (stringp name)
  56. (search "top level local call " (the simple-string name)))
  57. (let ((new-name (parse-name name))
  58. (*readtable* *sbcl-readtable*))
  59. (unless (string= new-name "")
  60. (handler-case (read-from-string new-name)
  61. (error (c)
  62. (declare (ignore c))
  63. "Unable to determine function name for ~A.")))))
  64. (t "Unable to determine function name for ~A."))))