xref: /plan9-contrib/sys/src/cmd/gs/doc/C-style.htm (revision d46c239f8612929b7dbade67d0d071633df3a15d)
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<head>
4<title>Ghostscript C coding guidelines</title>
5<!-- $Id: C-style.htm,v 1.21.2.2 2002/02/01 05:31:24 raph Exp $ -->
6<!-- Originally: c-style.txt -->
7<link rel="stylesheet" type="text/css" href="gs.css" title="Ghostscript Style">
8</head>
9
10<body>
11<!-- [1.0 begin visible header] ============================================ -->
12
13<!-- [1.1 begin headline] ================================================== -->
14
15<h1>Ghostscript C coding guidelines</h1>
16
17<!-- [1.1 end headline] ==================================================== -->
18
19<!-- [1.2 begin table of contents] ========================================= -->
20
21<h2>Table of contents</h2>
22
23<blockquote><ul>
24<li><a href="#Introduction">Introduction</a>
25<li><a href="#C_language">C language do's and don'ts</a>
26<ul>
27<li>Preprocessor:
28    <a href="#Conditionals">Conditionals</a>,
29    <a href="#Macros">Macros</a>,
30    <a href="#Preprocessor_other">Other</a>
31<li><a href="#Lexical_elements">Lexical elements</a>
32<li><a href="#Scoping">Scoping</a>
33<li>Data types:
34    <a href="#Scalars">Scalars</a>,
35    <a href="#Arrays">Arrays</a>,
36    <a href="#Typedefs">Typedefs</a>,
37    <a href="#Structures">Structures</a>,
38    <a href="#Unions">Unions</a>
39<li><a href="#Expressions">Expressions</a>
40<li><a href="#Statements">Statements</a>
41<li><a href="#Procedures">Procedures</a> (prototypes and definitions)
42<li><a href="#Standard_library">Standard library</a>
43</ul>
44<li><a href="#Language_extensions">Language extensions</a>
45<li><a href="#Stylistic_conventions">Stylistic conventions</a>
46<ul>
47<li>Formatting:
48    <a href="#Indentation">Indentation</a>,
49    <a href="#Spaces">Spaces</a>,
50    <a href="#Parentheses">Parentheses</a>
51<li><a href="#Preprocessor_style">Preprocessor</a>
52<li><a href="#Naming">Naming</a>
53<li><a href="#Types">Types</a>
54<li><a href="#Procedures_style">Procedures</a>,
55<li>Miscellany:
56    <a href="#Local_variables">Local variables</a>,
57    <a href="#Compiler_warnings">Compiler warnings</a>
58</ul>
59<li><a href="#File_structuring">File structuring and naming</a>
60<ul>
61<li><a href="#All_files">All files</a>
62<li><a href="#Makefiles">Makefiles</a>
63<li><a href="#General_C_code">General C Code</a>
64<li><a href="#Headers">Headers (<b><tt>.h</tt></b> files)</a>
65<li><a href="#Source">Source (<b><tt>.c</tt></b> files)</a>
66</ul>
67<li><a href="#Conventions">Ghostscript conventions</a>
68<ul>
69<li><a href="#Specific_names">Specific names</a>:
70    <a href="#code"><b><tt>code</tt></b></a>,
71    <a href="#status"><b><tt>status</tt></b></a>
72<li><a href="#Structure_type_descriptors">Structure type descriptors</a>
73<li><a href="#Objects">"Objects"</a>
74<li><a href="#Error_handling">Error handling</a>
75</ul>
76</ul></blockquote>
77
78<!-- [1.2 end table of contents] =========================================== -->
79
80<!-- [1.3 begin hint] ====================================================== -->
81
82<p>
83For other information, see the <a href="Readme.htm">Ghostscript
84overview</a>.
85
86<!-- [1.3 end hint] ======================================================== -->
87
88<hr>
89
90<!-- [1.0 end visible header] ============================================== -->
91
92<!-- [2.0 begin contents] ================================================== -->
93
94<h2><a name="Introduction"></a>Introduction</h2>
95
96<p>
97This document describes Ghostscript's C coding conventions.  It is primarily
98<em>prescriptive</em>, documenting what developers should do when writing
99new code; the companion developer documentation (<a
100 href="Develop.htm">Develop.htm</a>) is primarily <em>descriptive</em>,
101documenting the way things are.
102
103<p>
104We encourage following the general language usage and stylistic rules for
105any code that will be integrated with Ghostscript, even if the code doesn't
106use Ghostscript's run-time facilities or have anything to do with
107PostScript, PDF, or page description languages.  Ghostscript itself follows
108some additional conventions; these are documented separately under "<a
109href="#Conventions">Ghostscript conventions</a>" below.
110
111<hr>
112
113<h2><a name="C_language"></a>C language do's and don'ts</h2>
114
115<p>
116There are several different versions of the C language, and even of the ANSI
117C standard.  Ghostscript versions through 7.0 were designed to compile on
118pre-ANSI compilers as well as on many compilers that implemented the ANSI
119standard with varying faithfulness.  Ghostscript versions since 7.0 do not
120cater for pre-ANSI compilers: they must conform to the ANSI 1989 standard
121(ANS X3.159-1989), with certain restrictions and a few conventional
122additions.
123
124<h3>Preprocessor</h3>
125
126<h4><a name="Conditionals"></a>Conditionals</h4>
127
128Restrictions:
129
130<ul>
131
132<li>Don't assume that <b><tt>#if</tt></b> will treat undefined names as 0.
133While the ANSI standard requires this, it may produce a warning.
134
135<li>In <b><tt>.c</tt></b> files, don't use preprocessor conditionals that
136test for individual platforms or compilers.  Use them only in header files
137named xxx<b><tt>_.h</tt></b>.
138
139</ul>
140
141<h4><a name="Macros"></a>Macros</h4>
142
143<p>
144Restrictions:
145
146<ul>
147
148<li>Don't redefine a macro, even with the same definition, without using
149<b><tt>#undef</tt></b>.
150
151<li><b><tt>CAPITALIZE</tt></b> macro names unless there is a good reason not
152to.
153
154<li>Don't use a macro call within a macro argument if the call expands to a
155token sequence that includes any commas not within parentheses: this
156produces different results depending on whether the compiler expands the
157inner call before or after the argument is substituted into the macro body.
158(The ANSI standard says that calls must be expanded after substitution, but
159some compilers do it the other way.)
160
161<li>Don't use macro names, even inadvertently, in string constants.  Some
162compilers erroneously try to expand them.
163
164<li>Don't use macros to define shorthands for casted pointers.  For
165instance, avoid
166
167<blockquote><b><tt>
168#define fdev ((gx_device_fubar *)dev)
169</tt></b></blockquote>
170
171<p>
172and instead use
173
174<blockquote><b><tt>
175gx_device_fubar * const fdev = (gx_device_fubar *)dev;
176</tt></b></blockquote>
177
178<p>
179The use of <b><tt>const</tt></b> alerts the reader that this is effectively
180a synonym.
181
182<li>If a macro generates anything larger than a single expression (that is,
183one or more statements), surround it with <b><tt>BEGIN</tt></b> and
184<b><tt>END</tt></b>.  These work around the fact that simple statements and
185compound statements in C can't be substituted for each other syntactically.
186
187</ul>
188
189<h3><a name="Preprocessor_other"></a>Other</h3>
190
191<p>
192Restrictions:
193
194<ul>
195
196<li>Only use <b><tt>#pragma</tt></b> in files that are explicitly identified
197as being platform-dependent.  Many compilers complain if this is used at
198all, and some complain if they don't recognize the specific pragma being
199requested (both incorrect according to the ANSI standard).
200
201</ul>
202
203<h3><a name="Lexical_elements"></a>Lexical elements</h3>
204
205<p>
206Do not use:
207
208<ul>
209
210<li>ANSI trigraphs (??x)
211<li>Nested comments (/* /* */ */) (not ANSI compliant, but often accepted)
212<li>Multi-character character constants ('abc')
213<li>Wide-character character or string constants (L'x', L"x")
214
215</ul>
216
217<p>
218Restrictions:
219
220<ul>
221
222<li>Procedure and static variable names must be 31 characters or less.
223
224<li>Externally visible procedure and variable names must be unique in the
225first 23 characters.
226
227</ul>
228
229<h3><a name="Scoping"></a>Scoping (extern, static, ...)</h3>
230
231<p>
232Do not use:
233
234<ul>
235
236<li><b><tt>register</tt></b>
237
238</ul>
239
240<p>
241Restrictions:
242
243<ul>
244
245<li>Do not allow a global variable (constant) to have more than one
246non-<b><tt>extern</tt></b> definition, even though some ANSI C compilers
247allow this.  Every global constant should have exactly one definition, in a
248<b><tt>.c</tt></b> file, and preferably just one <b><tt>extern</tt></b>
249declaration, in a header file.
250
251<li>Use <b><tt>private</tt></b> instead of <b><tt>static</tt></b> for
252procedures and variables declared at the outermost scope of a file.  This
253allows making such constructs either visible or invisible to profilers by
254changing a single <b><tt>#define</tt></b>.
255
256<li><b><tt>static</tt></b> or <b><tt>private</tt></b> variables must be
257<b><tt>const</tt></b> and initialized: non-<b><tt>const</tt></b> statically
258allocated variables are incompatible with reentrancy, and we're in the
259process of eliminating all of them.
260
261<li>Do not use <b><tt>extern</tt></b> in <b><tt>.c</tt></b> files, only in
262<b><tt>.h</tt></b> files, unless you have a very good reason for it (e.g.,
263as in <a href="../src/iconf.c">iconf.c</a>).  There are too many such
264<b><tt>extern</tt></b>s in the code now: we are eliminating them over time.
265
266<li>Do not declare the same name as both <b><tt>static</tt></b>
267(<b><tt>private</tt></b>) and non-<b><tt>static</tt></b> within the same
268compilation.  (Some compilers complain, some do not.)  This is especially a
269problem for procedures: it is easy to declare a procedure as
270<b><tt>private</tt></b> near the beginning of a file and accidentally not
271declare it <b><tt>private</tt></b> where it is defined later in the file.
272
273<li>Even though the ANSI standard allows initialized external declarations
274(<b><tt>extern&nbsp;int&nbsp;x&nbsp;=&nbsp;0</tt></b>), don't use them.
275
276</ul>
277
278<h3><a name="Scalars"></a>Scalars</h3>
279
280<p>
281Restrictions:
282
283<ul>
284
285<li>Avoid using <b><tt>char</tt></b>, except for <b><tt>char&nbsp;*</tt></b>
286for a pointer to a string.  Don't assume that <b><tt>char</tt></b> is
287signed; also don't assume it is unsigned.
288
289<li>Never cast a <b><tt>float</tt></b> to a <b><tt>double</tt></b>
290explicitly.  ANSI compilers in their default mode do all floating point
291computations in double precision, and handle such casts automatically.
292
293<li>Don't use <b><tt>long long</tt></b>: even though it is in the ANSI
294standard, not all compilers support it.  Use <b><tt>bits64</tt></b> instead
295(see below under "<a href="#Language_extensions">Language extensions</a>").
296
297<li>Don't assume anything about whether <b><tt>sizeof(long)</tt></b> is less
298than, equal to, or greater than <b><tt>sizeof(ptr)</tt></b>.  (However, you
299can make such comparisons in preprocessor conditionals using
300<b><tt>ARCH_SIZEOF_LONG</tt></b> and <b><tt>ARCH_SIZEOF_PTR</tt></b>.)
301
302</ul>
303
304<h3><a name="Arrays"></a>Arrays</h3>
305
306<p>
307Restrictions:
308
309<ul>
310
311<li>Don't declare arrays of size 0.  (The newer ANSI standard allows this,
312but the older one doesn't.)
313
314<li>Don't declare an array of size 1 at the end of a structure to indicate
315that a variable-size array follows.
316
317<li>Don't declare initialized <b><tt>auto</tt></b> arrays.
318
319</ul>
320
321<h3><a name="Typedefs"></a>Typedefs</h3>
322
323<p>
324Restrictions:
325
326<ul>
327
328<li>Don't use <b><tt>typedef</tt></b> for function types, such as
329
330<blockquote>
331<b><tt>typedef int proc_xyz_t(double, int *);</tt></b>
332</blockquote>
333
334<p>Many compilers don't handle this correctly -- they will give errors, or
335do the wrong thing, when declaring variables of type
336<b><tt>proc_xyz_t</tt></b> and/or <b><tt>proc_xyz_t *</tt></b>.  Instead, do
337this:
338
339<blockquote>
340<b><tt>#define PROC_XYZ(proc) int proc(double, int *)<br>
341PROC_XYZ(some_proc);  /* declare a procedure of this type */<br>
342typedef PROC_XYZ((*proc_xyz_ptr_t));  /* define a type for procedure ptrs */<br>
343<br>
344proc_xyz_ptr_t pp;  /* pointer to procedure */</tt></b>
345</blockquote>
346
347<li>Don't redefine <b><tt>typedef</tt></b>'ed names, even with the same
348definition.  Some compilers complain about this, and the standard doesn't
349allow it.
350
351</ul>
352
353<h3><a name="Structures"></a>Structures</h3>
354
355<p>
356Restrictions:
357
358<li>Don't use anonymous structures if you can possibly avoid it, except
359occasionally as components of other structures. Ideally, use the
360<b><tt>struct</tt></b> keyword only for declaring named structure types,
361like this:
362
363<blockquote>
364<b><tt>typedef struct xxx_s {</tt></b><br>
365&nbsp;&nbsp;&nbsp;... members ...<br>
366<b><tt>} xxx_t;</tt></b>
367</blockquote>
368
369<li>Use <b><tt>struct</tt></b> only when declaring structure types, never
370for referring to them (e.g., never declare a variable as type
371<b><tt>struct&nbsp;xxx_s&nbsp;*</tt></b>).
372
373<li>Don't assume that the compiler will (or won't) insert padding in
374structures to align members for best performance.  To preserve alignment,
375only declare structure members that are narrower than an <b><tt>int</tt></b>
376if there will be a lot of instances of that structure in memory.  For such
377structures, insert <b><tt>byte</tt></b> and/or <b><tt>short</tt></b> padding
378members as necessary to re-establish <b><tt>int</tt></b> alignment.
379
380<li>Don't declare initialized <b><tt>auto</tt></b> structures.
381
382</ul>
383
384<h3><a name="Unions"></a>Unions</h3>
385
386<p>
387Restrictions:
388
389<ul>
390
391<li>Use unions only as components of structures, not as typedefs in their
392own right.
393
394<li>Don't attempt to initialize unions: not all compilers support this, even
395though it is in the 1989 ANSI standard.
396
397</ul>
398
399<h3><a name="Expressions"></a>Expressions</h3>
400
401<p>
402Restrictions:
403
404<ul>
405
406<li>Don't assign a larger integer data type to a smaller one without a cast
407(<b><tt>int_x&nbsp;=&nbsp;long_y</tt></b>).
408
409<li>It's OK to use the address of a structure or array element
410(<b><tt>&p->e</tt></b>, <b><tt>&a[i]</tt></b>) locally, or pass it to a
411procedure that won't store it, but don't store such an address in allocated
412storage unless you're very sure of what you're doing.
413
414<li>Don't use conditional expressions with structure or union values.
415(Pointers to structures or unions are OK.)
416
417<li>For calling a variable or parameter procedure, use
418<b><tt>ptr-&gt;func(...)</tt></b>.  Some old code uses explicit indirection,
419<b><tt>(*ptr-&gt;func)(...)</tt></b>: don't use this in new code.
420
421<li>Don't write expressions that depend on order of evaluation, unless the
422order is created explicitly by use of <b><tt>||</tt></b>,
423<b><tt>&amp;&amp;</tt></b>, <b><tt>?:</tt></b>, <b><tt>,</tt></b>, or
424function nesting (the arguments of a function must be evaluated before the
425function is called).  In particular, don't assume that the arguments of a
426function will be evaluated left-to-right, or that the left side of an
427assignment will be evaluated before the right.
428
429<li>Don't mix integer and enumerated types ad lib: treat enumerated types as
430distinct from integer types, and use casts to convert between the two.
431(Some compilers generate warnings if you do not do this.)
432
433</ul>
434
435<h3><a name="Statements"></a>Statements</h3>
436
437<p>
438Restrictions:
439
440<ul>
441
442<li>If you use an expression as a statement, other than an assignment or a
443function call with <b><tt>void</tt></b> return value, enclose it explicitly
444in <b><tt>DISCARD()</tt></b>.
445
446<li>The type of the operand of a <b><tt>switch</tt></b> must match the type
447of the case labels, whether the labels are <b><tt>int</tt></b>s or the
448members of an <b><tt>enum</tt></b> type.  (Use a cast if necessary.)
449
450<li>It is OK for one case of a switch to "fall through" into another (i.e.,
451for the statement just before a case label not to be a control transfer),
452but a comment <b><tt>/*&nbsp;falls&nbsp;through&nbsp;*/</tt></b> is
453required.
454
455<li>If you are returning an error code specified explicitly (e.g.,
456<b><tt>return&nbsp;gs_error_rangecheck</tt></b> or
457<b><tt>return&nbsp;e_rangecheck</tt></b>), use
458<b><tt>return_error()</tt></b> rather than plain <b><tt>return</tt></b>.
459However, if the program is simply propagating an error code generated
460elsewhere, as opposed to generating the error, use <b><tt>return</tt></b>
461(e.g., <b><tt>if&nbsp;(code&nbsp;<&nbsp;0)&nbsp;return&nbsp;code</tt></b>).
462
463</ul>
464
465<h3><a name="Procedures"></a>Procedures</h3>
466
467<p>
468Restrictions:
469
470<ul>
471
472<li>Provide a prototype for every procedure, and make sure the prototype is
473available at every call site.  If the procedure is local to a file
474(<b><tt>private</tt></b>), the prototype should precede the procedure, in
475the same file; if the procedure is global, the prototype should be in a
476header file.
477
478<li>If a procedure parameter is itself a procedure, do list its parameter
479types rather than just using <b><tt>()</tt></b>.  For example,
480
481<blockquote><b><tt>
482int foo(int (*callback)(int, int));
483</tt></b></blockquote>
484
485<p>
486rather than just
487
488<blockquote><b><tt>
489int foo(int (*callback)());
490</tt></b></blockquote>
491
492<li>Don't use the <b><tt>P</tt></b>* macros in new code.  (See the
493Procedures section of <a href="#Language_extensions">Language extensions</a>
494below for more information.)
495
496<li>Always provide an explicit return type for procedures, in both the
497prototype and the definition: don't rely on the implicit declaration as
498<b><tt>int</tt></b>.
499
500<li>Don't use <b><tt>float</tt></b> as the return type of a procedure,
501unless there's a special reason.  Floating point hardware typically does
502everything in double precision internally and has to do extra work to
503convert between double and single precision.
504
505<li>Don't declare parameters as being of type <b><tt>float</tt></b>,
506<b><tt>short</tt></b>, or <b><tt>char</tt></b>.  If you do this and forget
507to include the prototype at a call site, ANSI compilers will generate
508incompatible calling sequences.  Use <b><tt>floatp</tt></b> (a synonym for
509<b><tt>double</tt></b>, mnemonic for "float parameter") instead of
510<b><tt>float</tt></b>, and use <b><tt>int</tt></b> or <b><tt>uint</tt></b>
511instead of <b><tt>short</tt></b> or <b><tt>char</tt></b>.
512
513</ul>
514
515<h3><a name="Standard_library"></a>Standard library</h3>
516
517<p>
518Restrictions:
519
520<ul>
521
522<li>Only use library features that are documented in the established ANSI
523standard (e.g., Harbison & Steele's book).  Do not use procedures that are
524"standards" promulgated by Microsoft (e.g., <b><tt>stricmp</tt></b>), or
525originate in BSD Unix (e.g., <b><tt>strcasecmp</tt></b>), or were added in
526later versions of the standard such as C 9X.
527
528<li>Do not use any features from <b><tt>stdio.h</tt></b> that assume the
529existence of <b><tt>stdin</tt></b>, <b><tt>stdout</tt></b>, or
530<b><tt>stderr</tt></b>.  See <a href="../src/gsio.h">gsio.h</a> for the full
531list.  Instead, use <b><tt>gs_stdin</tt></b> et al.
532
533</ul>
534
535<hr>
536
537<h2><a name="Language_extensions"></a>Language extensions</h2>
538
539<h3>Scoping</h3>
540
541<dl>
542
543<dt><b><tt>inline</tt></b>
544
545<dd><b><tt>inline</tt></b> is available even if the compiler does not
546support it.  Be aware, however, that it may have no effect.  In particular,
547do not use <b><tt>inline</tt></b> in header files.  Instead, use the
548<b><tt>extern_inline</tt></b> facility described just below.
549
550<dt><b><tt>extern_inline</tt></b>
551
552<dd>Compilers that do support <b><tt>inline</tt></b> vary in how they decide
553whether to (also) compile a closed-code copy of the procedure.  Because of
554this, putting an <b><tt>inline</tt></b> procedure in a header file may
555produce multiple closed copies, causing duplicate name errors at link time.
556<b><tt>extern_inline</tt></b> provides a safe way to put
557<b><tt>inline</tt></b> procedures in header files, regardless of compiler.
558Unfortunately, the only way we've found to make this fully portable involves
559a fair amount of boilerplate.  For details, please see <a
560href="../src/stdpre.h">stdpre.h</a>.
561
562<dt><b><tt>private</tt></b>
563
564<dd><b>Use <tt>private</tt></b> instead of <b><tt>static</tt></b> for all
565file-local procedures, and also for file-local variables defined at the
566outermost level.  However, use <b><tt>static</tt></b>, not
567<b><tt>private</tt></b>, for variables defined within a procedure.
568
569<p>
570<b><tt>private</tt></b> is normally #define'd as <b><tt>static</tt></b>.
571However, it can also be #define'd as empty, which allows profilers to
572measure all procedures, and 'nm' to list all interesting statically
573allocated variables, not just public ones.
574
575</dl>
576
577<h3>Scalar types</h3>
578
579<dl>
580
581<dt><b><tt>bool, true, false</tt></b>
582
583<dd><b><tt>bool</tt></b> is intended as a Boolean type, with canonical
584values <b><tt>true</tt></b> and <b><tt>false</tt></b>.  In a more reasonable
585language, such as Java, <b><tt>bool</tt></b> is an enumerated type requiring
586an explicit cast to or from <b><tt>int</tt></b>; however, because C's
587conditionals are defined as producing <b><tt>int</tt></b> values, we can't
588even define <b><tt>bool</tt></b> as a C <b><tt>enum</tt></b> without
589provoking compiler warnings.
590
591<p>
592Even though <b><tt>bool</tt></b> is a synonym for <b><tt>int</tt></b>, treat
593them as conceptually different types:
594
595<ul>
596<li>Initialize or set <b><tt>bool</tt></b> variables to <b><tt>true</tt></b>
597or <b><tt>false</tt></b>, not 0 or 1.
598<li>Use the Boolean operators <b><tt>!</tt></b>, <b><tt>&&</tt></b>,
599and <b><tt>||</tt></b> only with Booleans.  Don't use the idiom
600<b><tt>!!x</tt></b> to create a Boolean that is true iff <b><tt>x</tt></b>
601!= 0: use <b><tt>x != 0</tt></b>.
602<li>Use an explicit <b><tt>(int)</tt></b> cast to convert a Boolean to an
603integer.
604</ul>
605
606<dt><b><tt>byte, ushort, uint, ulong</tt></b>
607
608<dd>These types are simply shorthands for <b><tt>unsigned char, short, int,
609long</tt></b>.
610
611<dt><b><tt>floatp</tt></b>
612
613<dd>This is a synonym for <b><tt>double</tt></b>.  It should be used for,
614and only for, procedure parameters that would otherwise be
615<b><tt>float</tt></b>.  (As noted above, procedure parameters should not be
616declared as <b><tt>float</tt></b>.)
617
618<dt><b><tt>bits8, bits16, bits32</tt></b>
619
620<dd>These are unsigned integer types of the given width.  Use them wherever
621the actual width matters: do <em>not</em>, for example, use
622<b><tt>short</tt></b> assuming that it is 16 bits wide.
623
624<dt><b><tt>bits64</tt></b>
625
626<dd><strong>****** NOT IMPLEMENTED YET ******</strong>
627This is an unsigned 64-bit integer type, but it may not be available on all
628platforms.  Any code that uses this type should be surrounded by
629<b><tt>#if&nbsp;ARCH_HAS_BITS64</tt></b>.
630
631</dl>
632
633<h3>Procedures</h3>
634
635<dl>
636
637<dt><b><tt>P0(), P1(p1), ..., P16(p1, ..., p16)</tt></b> [deprecated]
638
639<dd>Nearly all existing code uses these macros for the parameter lists of
640procedure prototypes, e.g.,
641<b><tt>int&nbsp;proc(P2(int&nbsp;a,&nbsp;int&nbsp;b))</tt></b>, to provide
642compatibility with pre-ANSI, a.k.a. "traditional" or "K &amp; R", compilers.
643Since only ANSI compilers are now supported, don't use these macros in new
644code: use the ANSI standard syntax
645<b><tt>int&nbsp;proc(int&nbsp;a,&nbsp;int&nbsp;b)</tt></b>.
646
647</dl>
648
649<hr>
650
651<h2><a name="Stylistic_conventions"></a>Stylistic conventions</h2>
652
653<p>
654Ghostscript's coding rules cover not only the use of the language, but also
655many stylistic issues like the choice of names and the use of whitespace.
656The stylistic rules are meant to produce code that is easy to read.  It's
657important to observe them as much as possible in order to maintain a
658consistent style, but if you find these rules getting in your way or
659producing ugly-looking results once in a while, it's OK to break it.
660
661<h3><a name="Formatting"></a>Formatting</h3>
662
663<h4><a name="Indentation"></a>Indentation</h4>
664
665<p>
666We've formatted all of our code using the GNU <b><tt>indent</tt></b> program.
667
668<blockquote><b><tt>
669indent&nbsp;-bad&nbsp;-nbap&nbsp;-nsob&nbsp;-br&nbsp;-ce&nbsp;-cli4&nbsp;-npcs&nbsp;-ncs&nbsp;\<br>
670&nbsp;&nbsp;&nbsp;-i4&nbsp;-di0&nbsp;-psl&nbsp;-lp&nbsp;-lps&nbsp;somefile.c
671</tt></b></blockquote>
672
673<p>
674does a 98% accurate job of producing our preferred style.  Unfortunately,
675there are bugs in all versions of GNU <b><tt>indent</tt></b>, requiring
676both pre- and post-processing of the code.  The <b><tt>gsindent</tt></b>
677script in the Ghostscript fileset contains the necessary workarounds.
678
679<p>
680Put indentation points every 4 spaces, with 8 spaces = 1 tab stop.
681
682<p>
683For assignments (including chain assignments), put the entire statement on
684one line if it will fit; if not, break it after a <b><tt>=</tt></b> and
685indent all the following lines.  I.e., format like this:
686
687<blockquote>
688var1&nbsp;<b><tt>=</tt></b>&nbsp;value<b><tt>;</tt></b><br>
689var1&nbsp;<b><tt>=</tt></b>&nbsp;var2&nbsp;<b><tt>=</tt></b>&nbsp;value<b><tt>;</tt></b><br>
690var1&nbsp;<b><tt>=</tt></b><br>
691&nbsp;&nbsp;&nbsp;&nbsp;value<b><tt>;</tt></b><br>
692var1&nbsp;<b><tt>=</tt></b><br>
693&nbsp;&nbsp;&nbsp;&nbsp;var2&nbsp;<b><tt>=</tt></b>&nbsp;value<b><tt>;</tt></b><br>
694var1&nbsp;<b><tt>=</tt></b>&nbsp;var2&nbsp;<b><tt>=</tt></b><br>
695&nbsp;&nbsp;&nbsp;&nbsp;value<b><tt>;</tt></b>
696</blockquote>
697
698<p>
699But not like this:
700
701<blockquote>
702var1&nbsp;<b><tt>=</tt></b><br>
703var2&nbsp;<b><tt>=</tt></b> value<b><tt>;</tt></b>
704</blockquote>
705
706<p>
707Indent in-line blocks thus:
708
709<blockquote>
710<b><tt>{</tt></b><br>
711&nbsp;&nbsp;&nbsp;... declarations ...<br>
712&nbsp;&nbsp;&nbsp;{{ blank line if any declarations above }}<br>
713&nbsp;&nbsp;&nbsp;... statements ...<br>
714<b><tt>}</tt></b>
715</blockquote>
716
717<p>
718Similarly, indent procedures thus:
719
720<blockquote>
721return_type<br>
722proc_name(... arguments ...)<br>
723<b><tt>{</tt></b><br>
724&nbsp;&nbsp;&nbsp;... declarations ...<br>
725&nbsp;&nbsp;&nbsp;{{ blank line if any declarations above }}<br>
726&nbsp;&nbsp;&nbsp;... statements ...<br>
727<b><tt>}</tt></b>
728</blockquote>
729
730<p>
731If a control construct (<b><tt>if</tt></b>, <b><tt>do</tt></b>,
732<b><tt>while</tt></b>, or <b><tt>for</tt></b>) has a one-line body, use
733this:
734
735<blockquote>
736... control construct ...<br>
737&nbsp;&nbsp;&nbsp;... subordinate simple statement ...
738</blockquote>
739
740<p>
741If it has a multi-line body, use this:
742
743<blockquote>
744... control construct ... <b><tt>{</tt></b><br>
745&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
746<b><tt>}</tt></b>
747</blockquote>
748
749<p>
750If the subordinate code has declarations, see blocks above.
751
752<p>
753For if-else statements, do this:
754
755<blockquote>
756<b><tt>if (</tt></b> ...<b><tt> ) {</tt></b><br>
757&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
758<b><tt>} else if (</tt></b> ...<b><tt> ) {</tt></b><br>
759&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
760<b><tt>} else {</tt></b><br>
761&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
762<b><tt>}</tt></b>
763</blockquote>
764
765<p>
766When there are more than two alternatives, as in the example above, use the
767above ("parallel") syntax rather than the following ("nested") syntax:
768
769<blockquote>
770<b><tt>if (</tt></b> ...<b><tt> ) {</tt></b><br>
771&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
772<b><tt>} else {</tt></b><br>
773<b><tt>&nbsp;&nbsp;&nbsp;if (</tt></b> ...<b><tt> ) {</tt></b><br>
774&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
775<b><tt>&nbsp;&nbsp;&nbsp;} else {</tt></b><br>
776&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... subordinate code ...<br>
777<b><tt>&nbsp;&nbsp;&nbsp;}</tt></b><br>
778<b><tt>}</tt></b>
779</blockquote>
780
781<p>
782Similarly, for do-while statements, do this:
783
784<blockquote>
785<b><tt>do {</tt></b><br>
786&nbsp;&nbsp;&nbsp;... body ...<br>
787<b><tt>} while (</tt></b> ... condition ... <b><tt>);</tt></b>
788</blockquote>
789
790<h4><a name="Spaces"></a>Spaces</h4>
791
792<p>
793Do put a space:
794<ul>
795<li>after every comma and semicolon, unless it ends a line;
796<li>around every binary operator other than "<b><tt>-&gt;</tt></b>" and
797"<b><tt>.</tt></b>", although you can omit the spaces around the innermost
798operator in a nested expression if you like;
799<li>on both sides of the parentheses of an <b><tt>if</tt></b>, <b><tt>for</tt></b>, or <b><tt>while</tt></b>.
800</ul>
801
802<p>
803Don't put a space:
804<ul>
805<li>at the end of a line;
806<li>before a comma or semicolon;
807<li>after unary prefix operators;
808<li>before the parenthesis of a macro or procedure call.
809</ul>
810
811<h4><a name="Parentheses"></a>Parentheses</h4>
812
813<p>
814Parentheses are important in only a few places:
815
816<ul>
817<li>Around the inner subexpressions in expressions that mix
818<b><tt>&amp;&amp;</tt></b> and <b><tt>||</tt></b>, even if they are not
819required by precedence, for example
820
821<blockquote><b><tt>
822(xx &amp;&amp; yy) || zz
823</tt></b></blockquote>
824
825<li>Similarly around inner subexpressions in expressions that mix
826<b><tt>&amp;</tt></b>, <b><tt>|</tt></b>, or shifts, especially if mixing
827these with other operators, for instance
828
829<blockquote><b><tt>
830(x &lt;&lt; 3) | (y &gt;&gt; 5)
831</tt></b></blockquote>
832
833<li>In macro definitions around every use of an argument that logically
834could be an expression, for example
835
836<blockquote><b><tt>
837((x) * (x) + (y) * (y))
838</tt></b></blockquote>
839
840</ul>
841
842<p>
843Anywhere else, given the choice, use fewer parentheses.
844
845<p>
846For stylistic consistency with the existing Ghostscript code, put
847parentheses around conditional expressions even if they aren't
848syntactically required, unless you really dislike doing this.  Note that
849the parentheses should go around the entire expression, not the condition.
850For instance, instead of
851
852<blockquote><b><tt>
853hpgl_add_point_to_path(pgls, arccoord_x, arccoord_y,<br>
854&nbsp;&nbsp;&nbsp;(pgls-&gt;g.pen_down) ? gs_lineto : gs_moveto);
855</tt></b></blockquote>
856
857<p>
858use
859
860<blockquote><b><tt>
861hpgl_add_point_to_path(pgls, arccoord_x, arccoord_y,<br>
862&nbsp;&nbsp;&nbsp;(pgls-&gt;g.pen_down ? gs_lineto : gs_moveto));
863</tt></b></blockquote>
864
865<h3><a name="Preprocessor_style"></a>Preprocessor</h3>
866
867<h4>Conditionals</h4>
868
869<p>
870Using preprocessor conditionals can easily lead to unreadable code, since
871the eye really wants to read linearly rather than having to parse the
872conditionals just to figure out what code is relevant.  It's OK to use
873conditionals that have small scope and that don't change the structure or
874logic of the program (typically, they select between different sets of
875values depending on some configuration parameter), but where possible, break
876up source modules rather than use conditionals that affect the structure or
877logic.
878
879<h4>Macros</h4>
880
881<p>
882Ghostscript code uses macros heavily to effectively extend the rather
883weak abstraction capabilities of the C language, specifically in the area of
884memory management and garbage collection: in order to read Ghostscript code
885effectively, you simply have to learn some of these macros as though they
886were part of the language.  The current code also uses macros heavily for
887other purposes, but we are trying to phase these out as rapidly as possible,
888because they make the code harder to read and debug, and to use the
889rules that follow consistently in new code.
890
891<p>
892Define macros in the smallest scope you can manage (procedure, file, or
893<b><tt>.h</tt></b> file), and <b><tt>#undef</tt></b> them at the end of
894that scope: that way, someone reading the code can see the definitions
895easily when reading the uses.  If that isn't appropriate, define them in as
896large a scope as possible, so that they effectively become part of the
897language.  This places an additional burden on the reader, but it can be
898amortized over reading larger amounts of code.
899
900<p>
901Try hard to use procedures instead of macros.  Use "<b><tt>inline</tt></b>"
902if you really think the extra speed is needed, but only within a
903<b><tt>.c</tt></b> file: don't put inline procedures in <b><tt>.h</tt></b>
904files, because most compilers don't honor "<b><tt>inline</tt></b>" and
905you'll wind up with a copy of the procedure in every <b><tt>.c</tt></b>
906file that includes the <b><tt>.h</tt></b> file.
907
908<p>
909If you define a macro that looks like a procedure, make sure it will work
910wherever a procedure will work.  In particular, put parentheses around every
911use of an argument within the macro body, so that the macro will parse
912correctly if some of the arguments are expressions, and put parentheses
913around the entire macro body.  (This is still subject to the problem that an
914argument may be evaluated more than once, but there is no way around this in
915C, since C doesn't provide for local variables within expressions.)
916
917<p>
918If you define macros for special loop control structures, make their uses
919look somewhat like ordinary loops, for instance:
920
921<blockquote>
922<b><tt>BEGIN_RECT(xx, yy) {</tt></b><br>
923&nbsp;&nbsp;... body indented one position ...<br>
924<b><tt>} END_RECT(xx, yy);</tt></b>
925</blockquote>
926
927<p>
928If at all possible, don't use free variables in macros -- that is, variables
929that aren't apparent as arguments of the macro.  If you must use free
930variables, list them all in a comment at the point where the macro is
931defined.
932
933<h3><a name="Comments"></a>Comments</h3>
934
935<p>
936The most important descriptive comments are ones in header files that
937describe structures, including invariants; but every procedure or structure
938declaration, or group of other declarations, should have a comment.  Don't
939spend a lot of time commenting executable code unless something unusual or
940subtle is going on.
941
942<h3><a name="Naming"></a>Naming</h3>
943
944<p>
945Use fully spelled-out English words in names, rather than contractions.
946This is most important for procedure and macro names, global variables and
947constants, values of <b><tt>#define</tt></b> and <b><tt>enum</tt></b>,
948<b><tt>struct</tt></b> and other <b><tt>typedef</tt></b> names, and
949structure member names, and for argument and variable names which have
950uninformative types like <b><tt>int</tt></b>.  It's not very important for
951arguments or local variables of distinctive types, or for local index or
952count variables.
953
954<p>
955Avoid names that run English words together:
956"<b><tt>hpgl_compute_arc_center</tt></b>" is better than
957"<b><tt>hpgl_compute_arccenter</tt></b>".  However, for terms drawn from
958some predefined source, like the names of PostScript operators, use a term
959in its well-known form (for instance, <b><tt>gs_setlinewidth</tt></b>
960rather than <b><tt>gs_set_line_width</tt></b>).
961
962<p>
963Procedures, variables, and structures visible outside a single
964<b><tt>.c</tt></b> file should generally have prefixes that indicate what
965subsystem they belong to (in the case of Ghostscript, <b><tt>gs_</tt></b>
966or <b><tt>gx_</tt></b>).  This rule isn't followed very consistently.
967
968<h3><a name="Types"></a>Types</h3>
969
970<p>
971Many older structure names don't have <b><tt>_t</tt></b> on the end, but
972this suffix should be used in all new code.  (The <b><tt>_s</tt></b>
973structure name is needed only to satisfy some debuggers.  No code other than
974the structure declaration should refer to it.)
975
976<p>
977Declare structure types that contain pointers to other instances of
978themselves like this:
979
980<blockquote>
981<b><tt>typedef struct xxx_s xxx_t;</tt></b><br>
982<b><tt>struct xxx_s {</tt></b><br>
983&nbsp;&nbsp;&nbsp;... members ...<br>
984&nbsp;&nbsp;&nbsp;<b><tt>xxx_t *</tt></b>ptr_member_name;<br>
985&nbsp;&nbsp;&nbsp;... members ...<br>
986<b><tt>};</tt></b>
987</blockquote>
988
989<p>
990If, to maintain data abstraction and avoid including otherwise unnecessary
991header files, you find that you want the type <b><tt>xxx_t</tt></b> to be
992available in a header file that doesn't include the definition of the
993structure <b><tt>xxx_s</tt></b>, use this approach:
994
995<blockquote>
996<b><tt>#ifndef xxx_DEFINED</tt></b><br>
997<b><tt>#&nbsp;&nbsp;define xxx_DEFINED</tt></b><br>
998<b><tt>typedef struct xxx_s xxx_t;</tt></b><br>
999<b><tt>#endif</tt></b><br>
1000<b><tt>struct xxx_s {</tt></b><br>
1001&nbsp;&nbsp;&nbsp;... members ...<br>
1002<b><tt>};</tt></b>
1003</blockquote>
1004
1005<p>
1006You can then copy the first 4 lines in other header files.  (Don't ever
1007include them in an executable code file.)
1008
1009<p>
1010Don't bother using <b><tt>const</tt></b> for anything other than with
1011pointers as described below.  However, in those places where it is necessary
1012to cast a pointer of type <b><tt>const&nbsp;T&nbsp;*</tt></b> to type
1013<b><tt>T&nbsp;*</tt></b>, always include a comment that explains why you are
1014"breaking const".
1015
1016<h4>Pointers</h4>
1017
1018<p>
1019Use <b><tt>const</tt></b> for pointer referents (that is,
1020<b><tt>const&nbsp;T&nbsp;*</tt></b>) wherever possible and appropriate.
1021
1022<p>
1023If you find yourself wanting to use <b><tt>void&nbsp;*</tt></b>, try to
1024find an alternative using unions or (in the case of super- and subclasses)
1025casts, unless you're writing something like a memory manager that really
1026treats memory as opaque.
1027
1028<h3><a name="Procedures_style"></a>Procedures</h3>
1029
1030<p>
1031In general, don't create procedures that are private and only called from
1032one place.  However, if a compound statement (especially an arm of a
1033conditional) is too long for the eye easily to match its enclosing braces
1034"<b><tt>{...}</tt></b>" -- that is, longer than 10 or 15 lines -- and it
1035doesn't use or set a lot of state held in outer local variables, it may be
1036more readable if you put it in a procedure.
1037
1038<h3>Miscellany</h3>
1039
1040<h4><a name="Local_variables"></a>Local variables</h4>
1041
1042<p>
1043Don't assign new values to procedure parameters.  It makes debugging very
1044confusing when the parameter values printed for a procedure are not the
1045ones actually supplied by the caller.  Instead use a separate local
1046variable initialized to the value of the parameter.
1047
1048<p>
1049If a local variable is only assigned a value once, assign it that value at
1050its declaration, if possible.  For example,
1051
1052<blockquote>
1053<b><tt>int x = </tt></b>some expression <b><tt>;</tt></b>
1054</blockquote>
1055
1056<p>
1057rather than
1058
1059<blockquote>
1060<b><tt>int x;</tt></b><br>
1061...<br>
1062<b><tt>x = </tt></b> some expression <b><tt>;</tt></b>
1063</blockquote>
1064
1065<p>
1066Use a local pointer variable like this to "narrow" pointer types:
1067
1068<blockquote>
1069<b><tt>int</tt></b><br>
1070someproc(... <b><tt>gx_device *dev</tt></b> ...)<br>
1071<b><tt>{<br>
1072&nbsp;&nbsp;&nbsp;gx_device_printer *const pdev = (gx_device_printer *)dev;</tt></b><br>
1073&nbsp;&nbsp;&nbsp;...<br>
1074<b><tt>}</tt></b>
1075</blockquote>
1076
1077<h4><a name="Compiler_warnings"></a>Compiler warnings</h4>
1078
1079<p>
1080The following section refers to the warnings produced by <b><tt>gcc</tt></b>:
1081your compiler may produce different ones.
1082
1083<p>
1084It's OK if compilation produces the following warnings:
1085
1086<ul>
1087<li><b><tt>&lt;name&gt; might be used uninitialized in this function</tt></b>
1088<li><b><tt>cast discards `const' from pointer target type</tt></b>
1089</ul>
1090
1091<p>
1092The first of these often occurs because the compiler isn't aware of control
1093flow restrictions that guarantee the variable will be initialized before
1094use: if it occurs in new code, check the code carefully, but don't worry
1095about the message.  The second is often unavoidable in code that initializes
1096or frees a structure that is otherwise <b><tt>const</tt></b> during its
1097lifetime.
1098<p>
1099
1100Do work hard to eliminate all warnings other than these,
1101since they often indicate the possible presence of coding errors.
1102In particular, get rid of warnings about parameter passing or
1103initialization that discards <b><tt>const</tt></b>,
1104by using explicit casts.
1105
1106<hr>
1107
1108<h2><a name="File_structuring"></a>File structuring</h2>
1109
1110<h3><a name="All_files"></a>All files</h3>
1111
1112<p>
1113Keep file names within the "8.3" format for portability:
1114<ul>
1115<li>Use only letters, digits, dash, and underscore in file names.
1116<li>Don't assume upper and lower case letters are distinct.
1117<li>Put no more than 8 characters before the ".", if any.
1118<li>If there is a ".", put between 1 and 3 characters after the ".".
1119</ul>
1120
1121<p>
1122For files other than documentation files, use only lower case letters
1123in the names; for HTML documentation files, capitalize the first letter.
1124
1125<p>
1126Every code file should start with comments containing
1127
1128<ol>
1129<li>a copyright notice,
1130<li>the name of the file in the form of an RCS Id:
1131
1132<blockquote><b><tt>
1133/*Id$: filename.ext $*/
1134</tt></b></blockquote>
1135
1136<p>
1137(using the comment convention appropriate to the language of the file), and
1138
1139<li>a summary, no more than one line, of what the file contains.
1140</ol>
1141
1142<p>
1143If you create a file by copying the beginning of another file, be sure to
1144update the copyright year and change the file name.
1145
1146<h3><a name="Makefiles"></a>Makefiles</h3>
1147
1148<p>
1149Use the extension <b><tt>.mak</tt></b> for makefiles.
1150
1151<p>
1152For each
1153
1154<blockquote><b><tt>
1155#include "xxx.h"
1156</tt></b></blockquote>
1157
1158<p>
1159make sure there is a dependency on <b><tt>$(xxx_h)</tt></b> in the
1160makefile.  If xxx ends with a "<b><tt>_</tt></b>", this rule still holds,
1161so that if you code
1162
1163<blockquote><b><tt>
1164#include "math_.h"
1165</tt></b></blockquote>
1166
1167<p>
1168the makefile must contain a dependency on "<b><tt>$(math__h)</tt></b>"
1169(note the two underscores "<b><tt>__</tt></b>").
1170
1171<p>
1172List the dependencies bottom-to-top, like the <b><tt>#include</tt></b>
1173statements themselves; within each level, list them alphabetically.  Do
1174this also with <b><tt>#include</tt></b> statements themselves whenever
1175possible (but sometimes there are inter-header dependencies that require
1176bending this rule).
1177
1178<p>
1179For compatibility with the build utilities on OpenVMS, always put a space
1180before the colon that separates the target(s) of a rule from the dependents.
1181
1182<h3><a name="General_C_code"></a>General C code</h3>
1183
1184<p>
1185List <b><tt>#include</tt></b> statements from "bottom" to "top", that is,
1186in the following order:
1187
1188<blockquote><ol>
1189<li>System includes (<b><tt>"xxx_.h"</tt></b>)
1190<li><b><tt>gs*.h</tt></b>
1191<li><b><tt>gx*.h</tt></b> (yes, <b><tt>gs</tt></b> and <b><tt>gx</tt></b>
1192are in the wrong order.)
1193<li><b><tt>s*.h</tt></b>
1194<li><b><tt>i*.h</tt></b> (or other interpreter headers that don't start
1195with "<b><tt>i</tt></b>")
1196</ol></blockquote>
1197
1198<h3><a name="Headers"></a>Headers (<b><tt>.h</tt></b> files)</h3>
1199
1200<p>
1201In header files, always use the following at the beginning of a header file
1202to prevent double inclusion:
1203
1204<blockquote>
1205{{ Copyright notice etc. }}<br><br>
1206
1207<b><tt>#ifndef </tt></b>&lt;filename&gt;<b><tt>_INCLUDED</tt></b><br>
1208<b><tt>#define </tt></b>&lt;filename&gt;<b><tt>_INCLUDED</tt></b><br><br>
1209
1210{{ The contents of the file }}<br><br>
1211
1212<b><tt>#endif /* </tt></b>&lt;filename&gt;<b><tt>_INCLUDED */</tt></b>
1213</blockquote>
1214
1215<p>
1216The header file is the first place that a reader goes for
1217information about procedures, structures, constants, etc., so ensure that
1218every procedure and structure has a comment that says what it does.  Divide
1219procedures into meaningful groups set off by some distinguished form of
1220comment.
1221
1222<h3><a name="Source"></a>Source (<b><tt>.c</tt></b> files)</h3>
1223
1224<p>
1225After the initial comments, arrange C files in the following order:
1226
1227<blockquote><ol>
1228<li><b><tt>#include</tt></b> statements
1229<li>Exported data declarations
1230<li>Explicit externs (if necessary)
1231<li>Forward declarations of procedures
1232<li>Private data declarations
1233<li>Exported procedures
1234<li>Private procedures
1235</ol></blockquote>
1236
1237<p>
1238Be flexible about the order of the declarations if necessary to improve
1239readability.  Many older files don't follow this order, often without good
1240reason.
1241
1242<hr>
1243
1244<h2><a name="Conventions"></a>Ghostscript conventions</h2>
1245
1246<h3><a name="Specific_names"></a>Specific names</h3>
1247
1248<p>
1249The Ghostscript code uses certain names consistently for certain kinds of
1250values.  Some of the commonest and least obvious are these two:
1251
1252<h4><a name="code"></a><b><tt>code</tt></b></h4>
1253
1254<blockquote>
1255A value to be returned from a procedure:
1256
1257<table cellpadding=0 cellspacing=0>
1258<tr valign=top>	<td align=right>&lt; 0
1259	<td>&nbsp;&nbsp;&nbsp;&nbsp;
1260	<td>An error code defined in
1261<a href="../src/gserrors.h">gserrors.h</a>
1262(or <a href="../src/errors.h">errors.h</a>)
1263<tr valign=top>	<td align=right>0
1264	<td>&nbsp;
1265	<td>Normal return
1266<tr valign=top>	<td align=right>&gt; 0
1267	<td>&nbsp;
1268	<td>A non-standard but successful return (which must be documented, preferably with the procedure's prototype)
1269</table>
1270
1271</blockquote>
1272
1273<h4><a name="status"></a><b><tt>status</tt></b></h4>
1274
1275<blockquote>
1276A value returned from a stream procedure:
1277
1278<table cellpadding=0 cellspacing=0>
1279<tr valign=top>	<td align=right>&lt; 0
1280	<td>&nbsp;&nbsp;&nbsp;&nbsp;
1281	<td>An exceptional condition as defined in
1282<a href="../src/scommon.h">scommon.h</a>
1283<tr valign=top>	<td align=right>0
1284	<td>&nbsp;
1285	<td>Normal return (or, from the "<b><tt>process</tt></b>" procedure, means that more input is needed)
1286<tr valign=top>	<td align=right>1
1287	<td>&nbsp;
1288	<td>More output space is needed (from the "<b><tt>process</tt></b>" procedure)
1289</table>
1290</blockquote>
1291
1292<h3><a name="Structure_type_descriptors"></a>Structure type descriptors</h3>
1293
1294<p>
1295The Ghostscript memory manager requires run-time type information for every
1296structure.  (We don't document this in detail here: see the <a
1297href="Develop.htm#Structure_descriptors">Structure descriptors</a> section
1298of the developer documentation for details.)  Putting the descriptor for a
1299structure next to the structure definition will help keep the two
1300consistent, so immediately after the definition of a structure
1301<b><tt>xxx_s</tt></b>, define its structure descriptor:
1302
1303<blockquote>
1304<b><tt>struct xxx_s {</tt></b><br>
1305&nbsp;&nbsp;&nbsp;... members ...<br>
1306<b><tt>};</tt></b><br>
1307<b><tt>#define private_st_xxx()&nbsp;&nbsp;/* in </tt></b>&lt;filename&gt;<tt><b>.c */\</tt></b><br>
1308<b><tt>&nbsp;&nbsp;gs_private_st_</tt></b>&lt;whatever&gt;<b><tt>(st_xxx, xxx_t,\</tt></b><br>
1309<b><tt>&nbsp;&nbsp;&nbsp;&nbsp;"xxx_t", xxx_enum_ptrs, xxx_reloc_ptrs,\</tt></b><br>
1310<b><tt>&nbsp;&nbsp;&nbsp;&nbsp;</tt></b>... additional parameters as needed ...<b><tt>)</tt></b>
1311</blockquote>
1312
1313<p>
1314The file that implements operations on this structure
1315(&lt;filename&gt;<b><tt>.c</tt></b>) should then include, near the
1316beginning, the line:
1317
1318<blockquote>
1319<b><tt>private_st_xxx();</tt></b>
1320</blockquote>
1321
1322<p>
1323In much existing code, structure descriptors are declared as
1324<b><tt>public</tt></b>, which allows clients to allocate instances of the
1325structure directly.  We now consider this bad design.  Instead, structure
1326descriptors should always be <b><tt>private</tt></b>; the implementation
1327file should provide one or more procedures for allocating instances, e.g.,
1328
1329<blockquote>
1330<b><tt>xxx_t *gs_xxx_alloc(P1(gs_memory_t *mem));</tt></b>
1331</blockquote>
1332
1333<p>
1334If it is necessary to make a structure descriptor public, it should be
1335declared in its clients as
1336
1337<blockquote>
1338<b><tt>extern_st(st_xxx);</tt></b>
1339</blockquote>
1340
1341<h3><a name="Objects"></a>"Objects"</h3>
1342
1343<p>
1344Ghostscript makes heavy use of object-oriented constructs, including
1345analogues of classes, instances, subclassing, and class-associated
1346procedures.  However, these constructs are implemented in C rather than C++,
1347for two reasons:
1348
1349<ul>
1350
1351<li>The first Ghostscript code was written in 1986, long before C++ was
1352codified or was well supported by tools.  Even today, C++ tools rarely
1353support C++ as well as C tools support C.
1354
1355<li>C++ imposes its own implementations for virtual procedures, inheritance,
1356run-time type information, and (to some extent) memory management.
1357Ghostscript requires use of its own memory manager, and also sometimes
1358requires the ability to change the virtual procedures of an object
1359dynamically.
1360
1361</ul>
1362
1363<h4>Classes</h4>
1364
1365<p>
1366The source code representation of a class is simply a
1367<b><tt>typedef</tt></b> for a C <b><tt>struct</tt></b>.  See <a
1368href="C-style.htm#Structures">Structures</a>, above, for details.
1369
1370<h4>Procedures</h4>
1371
1372<p>
1373Ghostscript has no special construct for non-virtual procedures associated
1374with a class.  In some cases, the <b><tt>typedef</tt></b> for the class is
1375in a header file but the <b><tt>struct</tt></b> declaration is in the
1376implementation code file: this provides an extra level of opaqueness, since
1377clients then can't see the representation and must make all accesses through
1378procedures.  You should use this approach in new code, if it doesn't
1379increase the size of the code too much or require procedure calls for very
1380heavily used accesses.
1381
1382<p>
1383Ghostscript uses three different approaches for storing and accessing
1384virtual procedures, plus a fourth one that is recommended but not currently
1385used.  For exposition, we assume the class (type) is named
1386<b><tt>xxx_t</tt></b>, it has a virtual procedure
1387<b><tt>void&nbsp;(*virtu)(P1(xxx_t&nbsp;*))</tt></b>, and we have a variable
1388declared as <b><tt>xxx_t&nbsp;*pxx</tt></b>.
1389
1390<ol>
1391
1392<li>The procedures are stored in a separate, constant structure of type
1393<b><tt>xxx_procs</tt></b>, of which <b><tt>virtu</tt></b> is a member.  The
1394structure definition of <b><tt>xxx_t</tt></b> includes a member defined as
1395<b><tt>const&nbsp;xxx_procs&nbsp;*procs</tt></b> (always named
1396<b><tt>procs</tt></b>).  The construct for calling the virtual procedure is
1397<b><tt>pxx->procs->virtu(pxx)</tt></b>.
1398
1399<li>The procedures are defined in a structure of type
1400<b><tt>xxx_procs</tt></b> as above.  The structure definition of
1401<b><tt>xxx_t</tt></b> includes a member defined as
1402<b><tt>xxx_procs&nbsp;procs</tt></b> (always named <b><tt>procs</tt></b>).
1403The construct for calling the virtual procedure is
1404<b><tt>pxx->procs.virtu(pxx)</tt></b>.
1405
1406<li>The procedures are not defined in a separate structure: each procedure
1407is a separate member of <b><tt>xxx_t</tt></b>.  The construct for calling
1408the virtual procedure is <b><tt>pxx->virtu(pxx)</tt></b>.
1409
1410<li>The procedures are defined in a structure of type
1411<b><tt>xxx_procs</tt></b> as above.  The structure definition of
1412<b><tt>xxx_t</tt></b> includes a member defined as
1413<b><tt>xxx_procs&nbsp;procs[1]</tt></b> (always named
1414<b><tt>procs</tt></b>).  The construct for calling the virtual procedure is
1415again <b><tt>pxx->procs->virtu(pxx)</tt></b>.
1416
1417</ol>
1418
1419<p>
1420Note that in approach 1, the procedures are in a shared constant structure;
1421in approaches 2 - 4, they are in a per-instance structure that can be
1422changed dynamically, which is sometimes important.
1423
1424<p>
1425In the present Ghostscript code, approach 1 is most common, followed by 2
1426and 3; 4 is not used at all.  For new code, you should use 1 or 4: that way,
1427all virtual procedure calls have the same form, regardless of whether the
1428procedures are shared and constant or per-instance and mutable.
1429
1430<h4>Subclassing</h4>
1431
1432<p>
1433Ghostscript's class mechanism allows for subclasses that can add data
1434members, or can add procedure members if approach 1 or 3 (above) is used.
1435Since C doesn't support subclassing, we use a convention to accomplish it.
1436In the example below, <b><tt>gx_device</tt></b> is the root class; it has a
1437subclass <b><tt>gx_device_forward</tt></b>, which in turn has a subclass
1438<b><tt>gx_device_null</tt></b>.  First we define a macro for all the members
1439of the root class, and the root class type.  (As for structures in general,
1440classes need a structure descriptor, as discussed in <a
1441href="#Structures">Structures</a> above: we include these in the examples
1442below.)
1443
1444<blockquote><b><tt>
1445#define gx_device_common\<br>
1446&nbsp;&nbsp;&nbsp;&nbsp;type1 member1;\<br>
1447&nbsp;&nbsp;&nbsp;&nbsp;</tt></b>...<b><tt><br>
1448&nbsp;&nbsp;&nbsp;&nbsp;typeN memberN<br>
1449<br>
1450typedef struct gx_device_s {<br>
1451&nbsp;&nbsp;&nbsp;&nbsp;gx_device_common;<br>
1452} gx_device;<br>
1453<br>
1454#define private_st_gx_device()&nbsp;&nbsp;/* in gsdevice.c */\<br>
1455&nbsp;&nbsp;gs_private_st_</tt></b>&lt;whatever&gt;<b><tt>(st_gx_device, gx_device,\<br>
1456&nbsp;&nbsp;&nbsp;&nbsp;"gx_device", device_enum_ptrs, device_reloc_ptrs,\<br>
1457&nbsp;&nbsp;&nbsp;&nbsp;</tt></b>... additional parameters as needed ...<b><tt>)</tt></b>
1458</tt></b></blockquote>
1459
1460<p>
1461We then define a similar macro and type for the subclass.
1462
1463<blockquote><b><tt>
1464#define gx_device_forward_common\<br>
1465&nbsp;&nbsp;&nbsp;&nbsp;gx_device_common;\<br>
1466&nbsp;&nbsp;&nbsp;&nbsp;gx_device *target<br>
1467<br>
1468typedef struct gx_device_forward_s {<br>
1469&nbsp;&nbsp;&nbsp;&nbsp;gx_device_forward_common;<br>
1470} gx_device_forward;<br>
1471<br>
1472#define private_st_device_forward()&nbsp;&nbsp;/* in gsdevice.c */\<br>
1473&nbsp;&nbsp;gs_private_st_suffix_add1(st_device_forward, gx_device_forward,\<br>
1474&nbsp;&nbsp;&nbsp;&nbsp;"gx_device_forward", device_forward_enum_ptrs, device_forward_reloc_ptrs,\<br>
1475&nbsp;&nbsp;&nbsp;&nbsp;gx_device, target)
1476</tt></b></blockquote>
1477
1478<p>
1479Finally, we define a leaf class, which doesn't need a macro because we don't
1480currently subclass it.  (We can create the macro later if needed, with no
1481changes anywhere else.)  In this particular case, the leaf class has no
1482additional data members, but it could have some.
1483
1484<blockquote><b><tt>
1485typedef struct gx_device_null_s {<br>
1486&nbsp;&nbsp;&nbsp;&nbsp;gx_device_forward_common;<br>
1487};<br>
1488<br>
1489#define private_st_device_null()&nbsp;&nbsp;/* in gsdevice.c */\<br>
1490&nbsp;&nbsp;gs_private_st_suffix_add0_local(st_device_null, gx_device_null,\<br>
1491&nbsp;&nbsp;&nbsp;&nbsp;"gx_device_null", device_null_enum_ptrs, device_null_reloc_ptrs,\<br>
1492&nbsp;&nbsp;&nbsp;&nbsp;gx_device_forward)
1493</tt></b></blockquote>
1494
1495<p>
1496Note that the above example is <strong>not</strong> the actual definition of
1497the <b><tt>gx_device</tt></b> structure type: the actual type has some
1498additional complications because it has a finalization procedure.  See <a
1499href="../src/gxdevcli.h">src/gxdevcli.h</a> for the details.
1500
1501<p>
1502If you add members to a root class (such as <b><tt>gx_device</tt></b> in
1503this example), or change existing members, do this in the
1504<b><tt>gx_device_common</tt></b> macro, not the <b><tt>gx_device</tt></b>
1505structure definition.  Similarly, to change the
1506<b><tt>gx_device_forward</tt></b> class, modify the
1507<b><tt>gx_device_forward_common</tt></b> macro, not the structure
1508definition.  Only change the structure definition if the class is a leaf
1509class (one with no <b><tt>_common</tt></b> macro and no possibility of
1510subclassing), like <b><tt>gx_device_null</tt></b>.
1511
1512<h3><a name="Error_handling"></a>Error handling</h3>
1513
1514<p>
1515Every caller should check for error returns and, in general, propagate them
1516to <b>its</b> callers.  By convention, nearly every procedure returns an
1517<b><tt>int</tt></b> to indicate the outcome of the call:
1518
1519<blockquote><table cellpadding=0 cellspacing=0>
1520<tr valign=top>	<td align=right>&lt; 0
1521	<td>&nbsp;&nbsp;&nbsp;&nbsp;
1522	<td>Error return
1523<tr valign=top>	<td align=right>0
1524	<td>&nbsp;
1525	<td>Normal return
1526<tr valign=top>	<td align=right>&gt; 0
1527	<td>&nbsp;
1528	<td>Non-error return other than the normal case
1529</table></blockquote>
1530
1531<p>
1532To make a procedure generate an error and return it, as opposed to
1533propagating an error generated by a lower procedure, you should use
1534
1535<blockquote>
1536<b><tt>return_error(</tt></b><em>error_number</em><b><tt>);</tt></b>
1537</blockquote>
1538
1539<p>
1540Sometimes it is more convenient to generate the error in one place and
1541return it in another.  In this case, you should use
1542
1543<blockquote>
1544<b><tt>code = gs_note_error(</tt></b><em>error_number</em><b><tt>);</tt></b><br>
1545...<br>
1546<b><tt>return code;</tt></b>
1547</blockquote>
1548
1549<p>
1550In executables built for debugging, the <b><tt>-E</tt></b> (or
1551<b><tt>-Z#</tt></b>) command line switch causes <b><tt>return_error</tt></b>
1552and <b><tt>gs_note_error</tt></b> to print the error number and the source
1553file and line: this is often helpful for identifying the original cause of
1554an error.
1555
1556<p>
1557See the file <a href="../src/gserrors.h">src/gserrors.h</a> for the error
1558return codes used by the graphics library, most of which correspond directly
1559to PostScript error conditions.
1560
1561<!-- [2.0 end contents] ==================================================== -->
1562
1563<!-- [3.0 begin visible trailer] =========================================== -->
1564<hr>
1565
1566<p>
1567<small>Copyright &copy; 1996, 1997, 1998 Aladdin Enterprises.
1568Copyright &copy; 2001 artofcode LLC.
1569All rights reserved.</small>
1570
1571<p>
1572<small>This file is part of AFPL Ghostscript.  See the <a
1573href="Public.htm">Aladdin Free Public License</a> (the "License") for full
1574details of the terms of using, copying, modifying, and redistributing AFPL
1575Ghostscript.</small>
1576
1577<p>
1578<small>Ghostscript version 7.04, 31 January 2002
1579
1580<!-- [3.0 end visible trailer] ============================================= -->
1581
1582</body>
1583</html>
1584