Function: comp-c-func-name

comp-c-func-name is an autoloaded and byte-compiled function defined in comp.el.gz.

Signature

(comp-c-func-name NAME PREFIX &optional FIRST)

Documentation

Given NAME, return a name suitable for the native code.

Add PREFIX in front of it. If FIRST is not nil, pick the first available name ignoring compilation context and potential name clashes.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/comp.el.gz
;; Autoloaded as might be used by `disassemble-internal'.
;;;###autoload
(defun comp-c-func-name (name prefix &optional first)
  "Given NAME, return a name suitable for the native code.
Add PREFIX in front of it.  If FIRST is not nil, pick the first
available name ignoring compilation context and potential name
clashes."
  ;; Unfortunately not all symbol names are valid as C function names...
  ;; Nassi's algorithm here:
  (let* ((orig-name (if (symbolp name) (symbol-name name) name))
         (crypted (cl-loop with str = (make-string (* 2 (length orig-name)) 0)
                           for j from 0 by 2
                           for i across orig-name
                           for byte = (format "%x" i)
                           do (aset str j (aref byte 0))
			      (aset str (1+ j) (if (length> byte 1)
						   (aref byte 1)
						 ?\_))
                           finally return str))
         (human-readable (string-replace
                          "-" "_" orig-name))
         (human-readable (replace-regexp-in-string
                          (rx (not (any "0-9a-z_"))) "" human-readable)))
    (if (null first)
        ;; Prevent C namespace conflicts.
        (cl-loop
         with h = (comp-ctxt-funcs-h comp-ctxt)
         for i from 0
         for c-sym = (concat prefix crypted "_" human-readable "_"
                             (number-to-string i))
         unless (gethash c-sym h)
         return c-sym)
      ;; When called out of a compilation context (ex disassembling)
      ;; pick the first one.
      (concat prefix crypted "_" human-readable "_0"))))