]> git.draconx.ca Git - cdecl99.git/blobdiff - doc/libcdecl.3
Expand some sections of the libcdecl manual.
[cdecl99.git] / doc / libcdecl.3
index 495a353f68b8810ee27b59f6876cc64abc86eb40..0d17d2b739f368cb964b05c0dca2780ab3d74f3e 100644 (file)
@@ -1,4 +1,4 @@
-.Dd March 6, 2021
+.Dd July 27, 2023
 .Os cdecl99
 .Dt LIBCDECL \&3 "Cdecl99 Developer's Manual"
 .Sh NAME
@@ -11,8 +11,8 @@
 .Fd struct cdecl *cdecl_parse_english(const char *english);
 .Fd void cdecl_free(struct cdecl *decl);
 .Pp
-.Fd size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl);
 .Fd size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl);
+.Fd size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl);
 .Pp
 .Fd const struct cdecl_error *cdecl_get_error(void);
 .Pp
@@ -190,8 +190,8 @@ In the next section, we will use the word
 to describe this inverted child relationship: we consider the outermost
 declarator's
 .Va parent
-as the declaration's base type (found amongst the declaration specifiers, e.g.
-.Li int , const unsigned long ,
+as the declaration's base type (found amongst the declaration specifiers,
+.No e.g. Li int , const unsigned long ,
 etc.)
 .Pp
 The five types of declarators, described below, are distinguished by the
@@ -322,56 +322,87 @@ This error information can be retrieved by calling the function
 .Pp
 which returns a pointer to the error structure most recently generated in the
 current thread.
-It is therefore thread-safe in that errors occurring in another thread will not
-interfere with the current one.
-The returned pointer shall remain valid until the next call to any function
+It is therefore thread-safe in the sense that errors occurring concurrently
+in another thread will not interfere with a call to
+.Fn cdecl_get_error .
+The returned structure shall remain valid until the next call to any function
 from
-.Nm
-by the same thread, except that multiple consecutive calls to
-.Va cdecl_get_error
-shall all return the same value.
-The same applies to the
-.Va str
-pointer inside the error structure itself.
+.Nm ,
+by the same thread, other than another call to
+.Fn cdecl_get_error .
 .Pp
-If this function is called before an error has been indicated by an earlier
-call in the same thread, the behaviour is undefined.
+If no prior
+.Nm
+call has indicated that an error occurred in the current thread, the result
+from calling
+.Fn cdecl_get_error
+is unspecified.
 .Sh PARSING DECLARATIONS
-To parse a declaration, the function
-.Pp
-.Fd struct cdecl *cdecl_parse_decl(const char *declstr);
-.Pp
-can be used.
-The provided string is parsed as a C declaration.
-If successful, this function returns a pointer to an abstract syntax tree
-representing the declaration.
-If the parse fails for any reason, the function returns NULL.
-.Pp
-Similarly, English declarations can be parsed by using the function
+The functions
 .Pp
+.Fd struct cdecl *cdecl_parse_decl(const char *decl);
 .Fd struct cdecl *cdecl_parse_english(const char *english);
 .Pp
-When the AST is no longer needed, it must be freed by passing it to the
+parse a string into an abstract syntax tree representing the declaration.
+The
+.Fn cdecl_parse_decl
+function interprets the string as C declaration syntax, while
+.Fn cdecl_parse_english
+uses the English declaration syntax.
+If successful, a pointer to the abstract syntax tree representing the
+declaration is returned.
+If the parse fails for any reason, the function returns NULL, and
+.Fn cdecl_get_error
+may be used to retrieve the reason for failure.
+.Pp
+The manner in which memory is allocated by these functions for the returned
+tree structure is unspecified.
+In particular, multiple nodes may share memory or may be implemented as
+read-only static allocations.
+Thus, the caller should not directly modify any part of the returned structure,
+as the results may be unexpected.
+A copy should be made if modifications are required.
+.Pp
+When the structure returned by either parsing function is no longer needed, the
 function
 .Pp
 .Fd void cdecl_free(struct cdecl *decl);
+.Pp
+may be used to release all memory allocations associated with that parse tree.
 .Sh RENDERING DECLARATIONS
-After parsing, the abstract syntax tree can be rendered to a string for output.
-Use the function
+An abstract syntax tree (which may be the result of calling one of the parsing
+functions or constructed explicitly by the program) can be rendered to a string
+for output.
+The functions
 .Pp
+.Fd size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl);
 .Fd size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl);
 .Pp
-to format the AST pointed to by
+perform this rendering.
+The
+.Fa cdecl_declare
+function produces output in C declaration syntax, while the
+.Fa cdecl_explain
+function produces output in the English declaration syntax.
+.Pp
+Only one top-level full declarator is rendered by each call; that is, these
+functions do not traverse the
+.Fa decl
+linked list at the top level.
+The caller can traverse this list to render multiple declarators.
+In order to assist with generating C declaration syntax, as a special case,
+when calling
+.Fn cdecl_declare
+the
+.Va specifiers
+member of the
 .Fa decl
-into something resembling English.
-At most one full declarator is rendered in this way; for declarations with more
-than one full declarator, this function should be called on each
-.Dv struct cdecl
-in the singly-linked list.
-.Pp
-In a manner similar to that of
-.Xr snprintf 3 ,
-at most
+structure may be a null pointer.
+In this case, only the declarator is rendered.
+.Pp
+The string is output in a manner similar to that of
+.Xr snprintf 3 .
+At most
 .Fa n
 bytes, including the '\\0' terminator, are written to
 .Fa buf .
@@ -380,27 +411,21 @@ If
 is zero, it is acceptable for
 .Fa buf
 to be a null pointer.
-Regardless, the function returns the number of characters that would be written
-to
+.Pp
+The number of characters that would be written to
 .Fa buf
 if
 .Fa n
-were long enough, not including the '\\0' terminator.
-Thus, the entire string was written if a value less than
+were large enough is returned, not including the '\\0' terminator.
+Hence the entire string was written if the returned value is less than
+.Fa n .
+If
 .Fa n
-is returned.
-.Pp
-Similarly, the function
-.Pp
-.Fd size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl);
-.Pp
-will render the AST pointed to by
-.Fa decl
-into C code.
+is non-zero, the resulting string is '\\0' terminated even if it was truncated.
 .Sh AUTHORS
 Nick Bowler <nbowler@draconx.ca>
 .Sh COPYRIGHT
-Copyright \(co 2011\(en2012, 2021 Nick Bowler
+Copyright \(co 2011\(en2012, 2021, 2023 Nick Bowler
 .Pp
 Permission is granted to copy, distribute and/or modify this manual under the
 terms of the GNU General Public License as published by the Free Software