comparison man/internals/internals.texi @ 1709:2f1ad95e2378

[xemacs-hg @ 2003-09-22 11:53:20 by stephent] coding conventions, minor fixes <8765jl2g98.fsf@tleepslib.sk.tsukuba.ac.jp>
author stephent
date Mon, 22 Sep 2003 11:53:23 +0000
parents e7b471ce22c1
children f43f9ca6c7d9
comparison
equal deleted inserted replaced
1708:a19b0eb5dfc1 1709:2f1ad95e2378
10 * Internals: (internals). XEmacs Internals Manual. 10 * Internals: (internals). XEmacs Internals Manual.
11 @end direntry 11 @end direntry
12 12
13 Copyright @copyright{} 1992 - 1996 Ben Wing. 13 Copyright @copyright{} 1992 - 1996 Ben Wing.
14 Copyright @copyright{} 1996, 1997 Sun Microsystems. 14 Copyright @copyright{} 1996, 1997 Sun Microsystems.
15 Copyright @copyright{} 1994 - 1998 Free Software Foundation. 15 Copyright @copyright{} 1994 - 1998, 2002, 2003 Free Software Foundation.
16 Copyright @copyright{} 1994, 1995 Board of Trustees, University of Illinois. 16 Copyright @copyright{} 1994, 1995 Board of Trustees, University of Illinois.
17 17
18 18
19 Permission is granted to make and distribute verbatim copies of this 19 Permission is granted to make and distribute verbatim copies of this
20 manual provided the copyright notice and this permission notice are 20 manual provided the copyright notice and this permission notice are
2257 of the utmost importance that you follow them. If you don't, you may 2257 of the utmost importance that you follow them. If you don't, you may
2258 get something that appears to work, but which will crash in odd 2258 get something that appears to work, but which will crash in odd
2259 situations, often in code far away from where the actual breakage is. 2259 situations, often in code far away from where the actual breakage is.
2260 2260
2261 @menu 2261 @menu
2262 * A Reader's Guide to XEmacs Coding Conventions::
2262 * General Coding Rules:: 2263 * General Coding Rules::
2263 * Writing Lisp Primitives:: 2264 * Writing Lisp Primitives::
2264 * Writing Good Comments:: 2265 * Writing Good Comments::
2265 * Adding Global Lisp Variables:: 2266 * Adding Global Lisp Variables::
2266 * Proper Use of Unsigned Types:: 2267 * Proper Use of Unsigned Types::
2267 * Coding for Mule:: 2268 * Coding for Mule::
2268 * Techniques for XEmacs Developers:: 2269 * Techniques for XEmacs Developers::
2269 @end menu 2270 @end menu
2271
2272 @node A Reader's Guide to XEmacs Coding Conventions
2273 @section A Reader's Guide to XEmacs Coding Conventions
2274 @cindex coding conventions
2275 @cindex reader's guide
2276 @cindex coding rules, naming
2277
2278 Of course the low-level implementation language of XEmacs is C, but much
2279 of that uses the Lisp engine to do its work. However, because the code
2280 is ``inside'' of the protective containment shell around the ``reactor
2281 core,'' you'll see lots of complex ``plumbing'' needed to do the work
2282 and ``safety mechanisms,'' whose failure results in a meltdown. This
2283 section provides a quick overview (or review) of the various components
2284 of the implementation of Lisp objects.
2285
2286 Two typographic conventions help to identify C objects that implement
2287 Lisp objects. The first is that capitalized identifiers, especially
2288 beginning with the letters @samp{Q}, @samp{V}, @samp{F}, and @samp{S},
2289 for C variables and functions, and C macros with beginning with the
2290 letter @samp{X}, are used to implement Lisp. The second is that where
2291 Lisp uses the hyphen @samp{-} in symbol names, the corresponding C
2292 identifiers use the underscore @samp{_}. Of course, since XEmacs Lisp
2293 contains interfaces to many external libraries, those external names
2294 will follow the coding conventions their authors chose, and may overlap
2295 the ``XEmacs name space.'' However these cases are usually pretty
2296 obvious.
2297
2298 All Lisp objects are handled indirectly. The @code{Lisp_Object}
2299 type is usually a pointer to a structure, except for a very small number
2300 of types with immediate representations (currently characters and
2301 integers). However, these types cannot be directly operated on in C
2302 code, either, so they can also be considered indirect. Types that do
2303 not have an immediate representation always have a C typedef
2304 @code{Lisp_@var{type}} for a corresponding structure.
2305 @c #### mention l(c)records here?
2306
2307 In older code, it was common practice to pass around pointers to
2308 @code{Lisp_@var{type}}, but this is now deprecated in favor of using
2309 @code{Lisp_Object} for all function arguments and return values that are
2310 Lisp objects. The @code{X@var{type}} macro is used to extract the
2311 pointer and cast it to @code{(Lisp_@var{type} *)} for the desired type.
2312
2313 @strong{Convention}: macros whose names begin with @samp{X} operate on
2314 @code{Lisp_Object}s and do no type-checking. Many such macros are type
2315 extractors, but others implement Lisp operations in C (@emph{e.g.},
2316 @code{XCAR} implements the Lisp @code{car} function). These are unsafe,
2317 and must only be used where types of all data have already been checked.
2318 Such macros are only applied to @code{Lisp_Object}s. In internal
2319 implementations where the pointer has already been converted, the
2320 structure is operated on directly using the C @code{->} member access
2321 operator.
2322
2323 The @code{@var{type}P}, @code{CHECK_@var{type}}, and
2324 @code{CONCHECK_@var{type}} macros are used to test types. The first
2325 returns a Boolean value, and the latter signal errors. (The
2326 @samp{CONCHECK} variety allows execution to be CONtinued under some
2327 circumstances, thus the name.) Functions which expect to be passed user
2328 data invariably call @samp{CHECK} macros on arguments.
2329
2330 There are many types of specialized Lisp objects implemented in C, but
2331 the most pervasive type is the @dfn{symbol}. Symbols are used as
2332 identifiers, variables, and functions.
2333
2334 @strong{Convention}: Global variables whose names begin with @samp{Q}
2335 are constants whose value is a symbol. The name of the variable should
2336 be derived from the name of the symbol using the same rules as for Lisp
2337 primitives. Such variables allow the C code to check whether a
2338 particular @code{Lisp_Object} is equal to a given symbol. Symbols are
2339 Lisp objects, so these variables may be passed to Lisp primitives. (An
2340 alternative to the use of @samp{Q...} variables is to call the
2341 @code{intern} function at initialization in the
2342 @code{vars_of_@var{module}} function, which is hardly less efficient.)
2343
2344 @strong{Convention}: Global variables whose names begin with @samp{V}
2345 are variables that contain Lisp objects. The convention here is that
2346 all global variables of type @code{Lisp_Object} begin with @samp{V}, and
2347 no others do (not even integer and boolean variables that have Lisp
2348 equivalents). Most of the time, these variables have equivalents in
2349 Lisp, which are defined via the @samp{DEFVAR} family of macros, but some
2350 don't. Since the variable's value is a @code{Lisp_Object}, it can be
2351 passed to Lisp primitives.
2352
2353 The implementation of Lisp primitives is more complex.
2354 @strong{Convention}: Global variables with names beginning with @samp{S}
2355 contain a structure that allows the Lisp engine to identify and call a C
2356 function. In modern versions of XEmacs, these identifiers are almost
2357 always completely hidden in the @code{DEFUN} and @code{SUBR} macros, but
2358 you will encounter them if you look at very old versions of XEmacs or at
2359 GNU Emacs. @strong{Convention}: Functions with names beginning with
2360 @samp{F} implement Lisp primitives. Of course all their arguments and
2361 their return values must be Lisp_Objects. (This is hidden in the
2362 @code{DEFUN} macro.)
2363
2270 2364
2271 @node General Coding Rules 2365 @node General Coding Rules
2272 @section General Coding Rules 2366 @section General Coding Rules
2273 @cindex coding rules, general 2367 @cindex coding rules, general
2274 2368
2725 @strong{Please note:} It is potentially deadly if you declare a 2819 @strong{Please note:} It is potentially deadly if you declare a
2726 @samp{Q...} variable in two different modules. The two calls to 2820 @samp{Q...} variable in two different modules. The two calls to
2727 @code{defsymbol()} are no problem, but some linkers will complain about 2821 @code{defsymbol()} are no problem, but some linkers will complain about
2728 multiply-defined symbols. The most insidious aspect of this is that 2822 multiply-defined symbols. The most insidious aspect of this is that
2729 often the link will succeed anyway, but then the resulting executable 2823 often the link will succeed anyway, but then the resulting executable
2730 will sometimes crash in obscure ways during certain operations! To 2824 will sometimes crash in obscure ways during certain operations!
2731 avoid this problem, declare any symbols with common names (such as 2825
2826 To avoid this problem, declare any symbols with common names (such as
2732 @code{text}) that are not obviously associated with this particular 2827 @code{text}) that are not obviously associated with this particular
2733 module in the module @file{general.c}. 2828 module in the file @file{general-slots.h}. The ``-slots'' suffix
2829 indicates that this is a file that is included multiple times in
2830 @file{general.c}. Redefinition of preprocessor macros allows the
2831 effects to be different in each context, so this is actually more
2832 convenient and less error-prone than doing it in your module.
2734 2833
2735 Global variables whose names begin with @samp{V} are variables that 2834 Global variables whose names begin with @samp{V} are variables that
2736 contain Lisp objects. The convention here is that all global variables 2835 contain Lisp objects. The convention here is that all global variables
2737 of type @code{Lisp_Object} begin with @samp{V}, and all others don't 2836 of type @code{Lisp_Object} begin with @samp{V}, and all others don't
2738 (including integer and boolean variables that have Lisp 2837 (including integer and boolean variables that have Lisp
5946 different section of code. 6045 different section of code.
5947 @end enumerate 6046 @end enumerate
5948 6047
5949 If you don't understand whether to @code{GCPRO} in a particular 6048 If you don't understand whether to @code{GCPRO} in a particular
5950 instance, ask on the mailing lists. A general hint is that @code{prog1} 6049 instance, ask on the mailing lists. A general hint is that @code{prog1}
5951 is the canonical example 6050 is the canonical example.
5952 6051
5953 @cindex garbage collection, conservative 6052 @cindex garbage collection, conservative
5954 @cindex conservative garbage collection 6053 @cindex conservative garbage collection
5955 Given the extremely error-prone nature of the @code{GCPRO} scheme, and 6054 Given the extremely error-prone nature of the @code{GCPRO} scheme, and
5956 the difficulties in tracking down, it should be considered a deficiency 6055 the difficulties in tracking down, it should be considered a deficiency