chiark / gitweb /
src/c-types-proto.lisp, src/c-types-impl.lisp: Qualifier name protocol.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 26 May 2016 08:26:09 +0000 (09:26 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 29 May 2016 14:08:43 +0000 (15:08 +0100)
Add a new generic function `c-qualifier-keyword' to convert a Lisp
qualifier (confusingly, a Lisp keyword) into its C equivalent.
Currently, this just lowercases the name, but future keywords won't
necessarily have such simple mappings.

Also add `c-type-qualifier-keywords' to collect and convert the list of
keywords attached to a type, and use it in the type printing functions.
Take the opportunity to add more discretionary newlines in the pretty-
printing.

doc/SYMBOLS
doc/clang.tex
src/c-types-impl.lisp
src/c-types-proto.lisp

index 86ef28da957eacf5b7bdb3591706620dffc85c47..26b7c02f7fd8a7b7423a0c6e147e08cf5ab4117d 100644 (file)
@@ -145,9 +145,11 @@ c-types-proto.lisp
   argument-type                                 function
   argumentp                                     function
   c-name-case                                   function
+  c-qualifier-keyword                           generic
   c-type                                        macro class
   c-type-alias                                  macro
   c-type-equal-p                                generic
+  c-type-qualifier-keywords                     function
   c-type-qualifiers                             generic
   c-type-space                                  function
   c-type-subtype                                generic
@@ -681,6 +683,8 @@ c-fragment-text
   c-fragment
 c-function-arguments
   c-function-type
+c-qualifier-keyword
+  cl:symbol
 c-tagged-type-kind
   c-enum-type
   c-struct-type
index b94ef9505c9fe7640ad1c3febd06ff6ae244d14f..700033fe5e6976c7dc57f0e8cf6e8124d03c6f47 100644 (file)
@@ -343,6 +343,23 @@ argument lists for methods.  This is done by @|c-type-equal-p|.
   non-null then the final character of the returned string will be a space.
 \end{describe}
 
+\begin{describe}{gf}{c-qualifier-keyword @<qualifier> @> @<string>}
+  Return, as a string, the C keyword corresponding to the Lisp @<qualifier>.
+
+  There is a standard method, which deals with many qualifiers.  Additional
+  methods exist for qualifier keywords which need special handling, such as
+  @|:atomic|; they are not listed here explicitly.
+
+  \begin{describe}{meth}{c-qualifier-keyword @<keyword> @> @<string>}
+    Returns the @<keyword>'s print-name, in lower case.  This is sufficient
+    for the standard qualifiers @|:const|, @|:restrict|, and @|:volatile|.
+  \end{describe}
+\end{describe}
+
+\begin{describe}{fun}{c-type-qualifier-keywords @<c-type> @> @<list>}
+  Return the @<c-type>'s qualifiers, as a list of C keyword names.
+\end{describe}
+
 
 \subsection{Leaf types} \label{sec:clang.c-types.leaf}
 
index e4e9587411a33b3a8232aecfbbf4b48f36a1af32..ce3aedfdb8e0b5469c1a741c6a9007480b255338 100644 (file)
@@ -120,8 +120,8 @@ (defmethod c-type-equal-p and
 
 (defmethod pprint-c-type ((type simple-c-type) stream kernel)
   (pprint-logical-block (stream nil)
-    (format stream "~{~(~A~) ~@_~}~A"
-           (c-type-qualifiers type)
+    (format stream "~{~A ~@_~}~A"
+           (c-type-qualifier-keywords type)
            (c-type-name type))
     (funcall kernel stream 0 t)))
 
@@ -262,8 +262,8 @@ (defmethod c-type-equal-p and ((type-a tagged-c-type) (type-b tagged-c-type))
 
 (defmethod pprint-c-type ((type tagged-c-type) stream kernel)
   (pprint-logical-block (stream nil)
-    (format stream "~{~(~A~) ~@_~}~(~A~) ~A"
-           (c-type-qualifiers type)
+    (format stream "~{~A ~@_~}~(~A~) ~A"
+           (c-type-qualifier-keywords type)
            (c-tagged-type-kind type)
            (c-type-tag type))
     (funcall kernel stream 0 t)))
@@ -309,8 +309,8 @@ (defmethod pprint-c-type ((type c-pointer-type) stream kernel)
                 (lambda (stream prio spacep)
                   (when spacep (c-type-space stream))
                   (maybe-in-parens (stream (> prio 1))
-                    (format stream "*~{~(~A~)~^ ~@_~}"
-                            (c-type-qualifiers type))
+                    (format stream "*~{~A~^ ~@_~}"
+                            (c-type-qualifier-keywords type))
                     (funcall kernel stream 1 (c-type-qualifiers type))))))
 
 ;; S-expression notation protocol.
index c8aa72eccb86487335897d9485d0489fc3b72516..10573217ec4328273838029c6938ac927ae81190 100644 (file)
@@ -57,6 +57,16 @@ (defgeneric qualify-c-type (type qualifiers)
    The qualifiers of the returned type are the union of the requested
    QUALIFIERS and the qualifiers already applied to TYPE."))
 
+(export 'c-qualifier-keyword)
+(defgeneric c-qualifier-keyword (qualifier)
+  (:documentation "Return the C keyword for the QUALIFIER (a Lisp keyword).")
+  (:method ((qualifier symbol)) (string-downcase qualifier)))
+
+(export 'c-type-qualifier-keywords)
+(defun c-type-qualifier-keywords (c-type)
+  "Return the type's qualifiers, as a list of C keyword names."
+  (mapcar #'c-qualifier-keyword (c-type-qualifiers c-type)))
+
 (export 'c-type-subtype)
 (defgeneric c-type-subtype (type)
   (:documentation