OSDN Git Service

PR ada/14150
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnat-style.texi
1 \input texinfo   @c -*-texinfo-*-
2 @c %**start of header
3
4 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
5 @c                                                                            o
6 @c                           GNAT DOCUMENTATION                               o
7 @c                                                                            o
8 @c                      G N A T   C O D I N G   S T Y L E                     o
9 @c                                                                            o
10 @c          Copyright (C) 1992-2003 Ada Core Technologies, Inc.               o
11 @c                                                                            o
12 @c  GNAT is free software;  you can  redistribute it  and/or modify it under  o
13 @c  terms of the  GNU General Public License as published  by the Free Soft-  o
14 @c  ware  Foundation;  either version 2,  or (at your option) any later ver-  o
15 @c  sion.  GNAT is distributed in the hope that it will be useful, but WITH-  o
16 @c  OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY  o
17 @c  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License  o
18 @c  for  more details.  You should have  received  a copy of the GNU General  o
19 @c  Public License  distributed with GNAT;  see file COPYING.  If not, write  o
20 @c  to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston,  o
21 @c  MA 02111-1307, USA.                                                       o
22 @c                                                                            o
23 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
24
25 @setfilename gnat-style.info
26
27 @settitle GNAT Coding Style
28 @setchapternewpage odd
29
30
31 @dircategory Programming
32 @direntry
33 * gnat-style: (gnat-style).      GNAT Coding Style
34 @end direntry
35
36 @macro syntax{element}
37 @t{\element\}
38 @end macro
39 @c %**end of header
40
41 @titlepage
42 @sp 10
43 @title GNAT Coding Style
44 @flushright
45 @titlefont{A Guide for GNAT Developers}
46 @end flushright
47 @sp 2
48 @subtitle GNAT, The GNU Ada 95 Compiler
49 @subtitle Document revision level $Revision: 1.8 $
50 @subtitle Date:  $Date: 2003/05/24 21:29:30 $
51
52 @author Ada Core Technologies, Inc.
53
54 @page
55 @vskip 0pt plus 1filll
56
57 Copyright @copyright{} 1995-2003, Free Software Foundation
58
59 Permission is granted to copy, distribute and/or modify this document
60 under the terms of the GNU Free Documentation License, Version 1.1
61 or any later version published by the Free Software Foundation;
62 with the Invariant Sections being ``GNU Free Documentation License'', with the
63 Front-Cover Texts being
64 ``GNAT Coding Style'' and ``A Guide for GNAT Developers'',
65 and with no Back-Cover Texts.
66 A copy of the license is included in the section entitled
67 ``GNU Free Documentation License''.
68 @end titlepage
69
70 @raisesections
71
72 @node    Top,       General,         , (dir)
73 @comment node-name, next,    previous, up
74
75 @ifinfo
76 @noindent
77 GNAT Coding Style@*
78 A Guide for GNAT Developers
79 @sp 2
80 @noindent
81 GNAT, The GNU Ada 95 Compiler@*
82 Document revision level $Revision: 1.8 $@*
83 Date:  $Date: 2003/05/24 21:29:30 $
84
85 @noindent
86 Permission is granted to copy, distribute and/or modify this document
87 under the terms of the GNU Free Documentation License, Version 1.1
88 or any later version published by the Free Software Foundation;
89 with the Invariant Sections being ``GNU Free Documentation License'', with the
90 Front-Cover Texts being
91 ``GNAT Coding Style'' and ``A Guide for GNAT Developers''
92 and with no Back-Cover Texts.
93 A copy of the license is included in the section entitled
94 ``GNU Free Documentation License''.
95 @end ifinfo
96
97
98 @menu
99 * General::
100 * Lexical Elements::
101 * Declarations and Types::
102 * Expressions and Names::
103 * Statements::
104 * Subprograms::
105 * Packages::
106 * Program Structure::
107 * GNU Free Documentation License::
108 * Index::
109 @end menu
110
111 @c  -------------------------------------------------------------------------
112 @node    General, Lexical Elements, Top, Top
113 @section General
114 @c  -------------------------------------------------------------------------
115
116 @noindent
117 Most of GNAT is written in Ada using a consistent style to ensure
118 readability of the code.  This document has been written to help
119 maintain this consistent style, while having a large group of developers
120 work on the compiler.
121
122 For the coding style in the C parts of the compiler and run time,
123 see the GNU Coding Guidelines.
124
125 This document is structured after the @cite{Ada Reference Manual}.
126 Those familiar with that document should be able to quickly
127 lookup style rules for particular constructs.
128
129
130 @c  -------------------------------------------------------------------------
131 @node    Lexical Elements, Declarations and Types, General, Top
132 @section Lexical Elements
133 @c  -------------------------------------------------------------------------
134 @cindex Lexical elements
135
136 @subsection Character Set and Separators
137 @c  -------------------------------------------------------------------------
138 @cindex Character set
139 @cindex ASCII
140 @cindex Separators
141 @cindex End-of-line
142 @cindex Line length
143 @cindex Indentation
144
145 @itemize @bullet
146 @item
147 The character set used should be plain 7-bit ASCII@.
148 The only separators allowed are space and the end-of-line sequence.
149 No other control character or format effector (such as @code{HT},
150 @code{VT}, @code{FF})
151 should be used.
152 The normal end-of-line sequence is used, which may be
153 @code{LF}, @code{CR/LF} or @code{CR},
154 depending on the host system.  An optional @code{SUB}
155 (@code{16#1A#}) may be present as the
156 last character in the file on hosts using that character as file terminator.
157
158 @item
159 Files that are checked in or distributed should be in host format.
160
161 @item
162 A line should never be longer than 79 characters, not counting the line
163 separator.
164
165 @item
166 Lines must not have trailing blanks.
167
168 @item
169 Indentation is 3 characters per level for @code{if} statements, loops, and
170 @code{case} statements.
171 For exact information on required spacing between lexical
172 elements, see file @file{style.adb}.
173 @cindex @file{style.adb} file
174 @end itemize
175
176
177 @subsection Identifiers
178 @c  -------------------------------------------------------------------------
179 @itemize @bullet
180 @cindex Identifiers
181
182 @item
183 Identifiers will start with an upper case letter, and each letter following
184 an underscore will be upper case.
185 @cindex Casing (for identifiers)
186 Short acronyms may be all upper case.
187 All other letters are lower case.
188 An exception is for identifiers matching a foreign language.  In particular,
189 we use all lower case where appropriate for C@.
190
191 @item
192 Use underscores to separate words in an identifier.
193 @cindex Underscores
194
195 @item Try to limit your use of abbreviations in identifiers.
196 It is ok to make a few abbreviations, explain what they mean, and then
197 use them frequently, but don't use lots of obscure abbreviations.  An
198 example is the @code{ALI} word which stands for Ada Library
199 Information and is by convention always written in upper-case when
200 used in entity names.
201
202 @smallexample @c adanocomment
203        procedure Find_ALI_Files;
204 @end smallexample
205
206 @item
207 Don't use the variable name @code{I}, use @code{J} instead; @code{I} is too
208 easily confused with @code{1} in some fonts.  Similarly don't use the
209 variable @code{O}, which is too easily mistaken for the number @code{0}.
210 @end itemize
211
212 @subsection Numeric Literals
213 @c  -------------------------------------------------------------------------
214 @cindex Numeric literals
215
216 @itemize @bullet
217 @item
218 Numeric literals should include underscores where helpful for
219 readability.
220 @cindex Underscores
221
222 @smallexample
223       1_000_000
224       16#8000_000#
225       3.14159_26535_89793_23846
226 @end smallexample
227 @end itemize
228
229 @subsection Reserved Words
230 @c  -------------------------------------------------------------------------
231 @cindex Reserved words
232
233 @itemize @bullet
234 @item
235 Reserved words use all lower case.
236 @cindex Casing (for reserved words)
237
238 @smallexample @c adanocomment
239        return else
240 @end smallexample
241
242 @item
243 The words @code{Access}, @code{Delta} and @code{Digits} are
244 capitalized when used as @syntax{attribute_designator}.
245 @end itemize
246
247 @subsection Comments
248 @c  -------------------------------------------------------------------------
249 @cindex Comments
250
251 @itemize @bullet
252 @item
253 A comment starts with @code{--} followed by two spaces).
254 The only exception to this rule (i.e.@: one space is tolerated) is when the
255 comment ends with a single space followed by @code{--}.
256 It is also acceptable to have only one space between @code{--} and the start
257 of the comment when the comment is at the end of a line,
258 after some Ada code.
259
260 @item
261 Every sentence in a comment should start with an upper-case letter (including
262 the first letter of the comment).
263 @cindex Casing (in comments)
264
265 @item
266 When declarations are commented with ``hanging'' comments, i.e.@:
267 comments after the declaration, there is no blank line before the
268 comment, and if it is absolutely necessary to have blank lines within
269 the comments these blank lines @emph{do} have a @code{--} (unlike the
270 normal rule, which is to use entirely blank lines for separating
271 comment paragraphs).  The comment starts at same level of indentation
272 as code it is commenting.
273 @cindex Blank lines (in comments)
274 @cindex Indentation
275
276 @smallexample @c adanocomment
277        z : Integer;
278        --  Integer value for storing value of z
279        --
280        --  The previous line was a blank line.
281 @end smallexample
282
283 @item
284 Comments that are dubious or incomplete, or that comment on possibly
285 wrong or incomplete code, should be preceded or followed by @code{???}@.
286
287 @item
288 Comments in a subprogram body must generally be surrounded by blank lines.
289 An exception is a comment that follows a line containing a single keyword
290 (@code{begin}, @code{else}, @code{loop}):
291
292 @smallexample @c adanocomment
293 @group
294        begin
295           --  Comment for the next statement
296
297           A := 5;
298
299           --  Comment for the B statement
300
301           B := 6;
302        end;
303 @end group
304 @end smallexample
305
306 @item
307 In sequences of statements, comments at the end of the lines should be
308 aligned.
309 @cindex Alignment (in comments)
310
311 @smallexample @c adanocomment
312         My_Identifier := 5;      --  First comment
313         Other_Id := 6;           --  Second comment
314 @end smallexample
315
316 @item
317 Short comments that fit on a single line are @emph{not} ended with a
318 period.  Comments taking more than a line are punctuated in the normal
319 manner.
320
321 @item
322 Comments should focus on @emph{why} instead of @emph{what}.
323 Descriptions of what subprograms do go with the specification.
324
325 @item
326 Comments describing a subprogram spec should specifically mention the
327 formal argument names.  General rule: write a comment that does not
328 depend on the names of things.  The names are supplementary, not
329 sufficient, as comments.
330
331 @item
332 @emph{Do not} put two spaces after periods in comments.
333 @end itemize
334
335 @c  -------------------------------------------------------------------------
336 @node    Declarations and Types, Expressions and Names, Lexical Elements,Top
337 @section Declarations and Types
338 @c  -------------------------------------------------------------------------
339 @cindex Declarationa and Types
340
341 @itemize @bullet
342 @item
343 In entity declarations, colons must be surrounded by spaces.  Colons
344 should be aligned.
345 @cindex Alignment (in declarations)
346
347 @smallexample @c adanocomment
348         Entity1   : Integer;
349         My_Entity : Integer;
350 @end smallexample
351
352 @item
353 Declarations should be grouped in a logical order.
354 Related groups of declarations may be preceded by a header comment.
355
356 @item
357 All local subprograms in a subprogram or package body should be declared
358 before the first local subprogram body.
359
360 @item
361 Do not declare local entities that hide global entities.
362 @cindex Hiding of outer entities
363
364 @item
365 Do not declare multiple variables in one declaration that spans lines.
366 Start a new declaration on each line, instead.
367
368 @item
369 The @syntax{defining_identifier}s of global declarations serve as
370 comments of a sort.  So don't choose terse names, but look for names
371 that give useful information instead.
372
373 @item
374 Local names can be shorter, because they are used only within
375 one context, where comments explain their purpose.
376
377 @end itemize
378
379
380 @c  -------------------------------------------------------------------------
381 @node    Expressions and Names, Statements, Declarations and Types, Top
382 @section Expressions and Names
383 @c  -------------------------------------------------------------------------
384 @cindex Expressions and names
385
386 @itemize @bullet
387
388 @item
389 Every operator must be surrounded by spaces, except for the
390 exponentiation operator.
391 @cindex Operators
392
393 @smallexample @c adanocomment
394        E := A * B**2 + 3 * (C - D);
395 @end smallexample
396
397 @item
398 When folding a long line, fold before an operator, not after.
399
400 @item
401 Use parentheses where they clarify the intended association of operands
402 with operators:
403 @cindex Parenthesization of expressions
404 @smallexample @c adanocomment
405        (A / B) * C
406 @end smallexample
407 @end itemize
408
409 @c  -------------------------------------------------------------------------
410 @node    Statements, Subprograms, Expressions and Names, Top
411 @section Statements
412 @c  -------------------------------------------------------------------------
413 @cindex Statements
414
415 @subsection Simple and Compound Statements
416 @c  -------------------------------------------------------------------------
417 @cindex Simple and compound statements
418
419 @itemize @bullet
420 @item
421 Use only one statement or label per line.
422 @item
423 A longer @syntax{sequence_of_statements} may be divided in logical
424 groups or separated from surrounding code using a blank line.
425 @end itemize
426
427 @subsection If Statements
428 @c  -------------------------------------------------------------------------
429 @cindex @code{if} statement
430
431 @itemize @bullet
432 @item
433 When the @code{if}, @code{elsif} or @code{else} keywords fit on the
434 same line with the condition and the @code{then} keyword, then the
435 statement is formatted as follows:
436 @cindex Alignment (in an @code{if} statement)
437
438 @smallexample @c adanocomment
439 @group
440         if @var{condition} then
441            ...
442         elsif @var{condition} then
443            ...
444         else
445            ...
446         end if;
447 @end group
448 @end smallexample
449
450 @noindent
451 When the above layout is not possible, @code{then} should be aligned
452 with @code{if}, and conditions should preferably be split before an
453 @code{and} or @code{or} keyword a follows:
454
455 @smallexample @c adanocomment
456 @group
457         if @var{long_condition_that_has_to_be_split}
458           and then @var{continued_on_the_next_line}
459         then
460            ...
461         end if;
462 @end group
463 @end smallexample
464
465 @noindent
466 The @code{elsif}, @code{else} and @code{end if} always line up with
467 the @code{if} keyword.  The preferred location for splitting the line
468 is before @code{and} or @code{or}.  The continuation of a condition is
469 indented with two spaces or as many as needed to make nesting clear.
470 As an exception, if conditions are closely related either of the
471 following is allowed:
472
473 @smallexample
474 @group
475      if x = lakdsjfhlkashfdlkflkdsalkhfsalkdhflkjdsahf
476           or else
477         x = asldkjhalkdsjfhhfd
478           or else
479         x = asdfadsfadsf
480      then
481        ...
482      end if;
483 @end group
484
485 @group
486      if x = lakdsjfhlkashfdlkflkdsalkhfsalkdhflkjdsahf or else
487         x = asldkjhalkdsjfhhfd                         or else
488         x = asdfadsfadsf
489      then
490        ...
491      end if;
492 @end group
493 @end smallexample
494
495 @item
496 Conditions should use short-circuit forms (@code{and then},
497 @code{or else}).
498 @cindex Short-circuit forms
499
500 @item
501 Complex conditions in @code{if} statements are indented two characters:
502 @cindex Indentation (in @code{if} statements)
503
504 @smallexample @c adanocomment
505 @group
506       if @var{this_complex_condition}
507         and then @var{that_other_one}
508         and then @var{one_last_one}
509       then
510          ...
511       end if;
512 @end group
513 @end smallexample
514
515 @item
516 Every @code{if} block is preceded and followed by a blank line, except
517 where it begins or ends a @syntax{sequence_of_statements}.
518 @cindex Blank lines (in an @code{if} statement)
519
520 @smallexample @c adanocomment
521 @group
522         A := 5;
523
524         if A = 5 then
525            null;
526         end if;
527
528         A := 6;
529 @end group
530 @end smallexample
531 @end itemize
532
533 @subsection Case Statements
534 @cindex @code{case} statements
535
536 @itemize @bullet
537 @item
538 Layout is as below.  For long @code{case} statements, the extra indentation
539 can be saved by aligning the @code{when} clauses with the opening @code{case}.
540
541 @smallexample @c adanocomment
542 @group
543        case @var{expression} is
544           when @var{condition} =>
545              ...
546           when @var{condition} =>
547              ...
548        end case;
549 @end group
550 @end smallexample
551 @end itemize
552
553 @subsection Loop Statements
554 @cindex Loop statements
555
556 @itemize @bullet
557 @noindent
558 When possible, have @code{for} or @code{while} on one line with the
559 condition and the @code{loop} keyword.
560
561 @smallexample @c adanocomment
562 @group
563        for J in S'Range loop
564           ...
565        end loop;
566 @end group
567 @end smallexample
568
569 @noindent
570 If the condition is too long, split the condition (see ``If
571 statements'' above) and align @code{loop} with the @code{for} or
572 @code{while} keyword.
573 @cindex Alignment (in a loop statement)
574
575 @smallexample @c adanocomment
576 @group
577       while @var{long_condition_that_has_to_be_split}
578         and then @var{continued_on_the_next_line}
579       loop
580          ...
581       end loop;
582 @end group
583 @end smallexample
584
585 @noindent
586 If the @syntax{loop_statement} has an identifier, it is laid out as follows:
587
588 @smallexample @c adanocomment
589 @group
590       Outer : while not @var{condition} loop
591          ...
592       end Outer;
593 @end group
594 @end smallexample
595 @end itemize
596
597 @subsection Block Statements
598 @cindex Block statement
599
600 @itemize @bullet
601 @item
602 The @code{declare} (optional), @code{begin} and @code{end} words
603 are aligned, except when the @syntax{block_statement} is named.  There
604 is a blank line before the @code{begin} keyword:
605 @cindex Alignment (in a block statement)
606
607 @smallexample @c adanocomment
608 @group
609       Some_Block : declare
610          ...
611
612       begin
613          ...
614       end Some_Block;
615 @end group
616 @end smallexample
617
618 @end itemize
619
620 @c  -------------------------------------------------------------------------
621 @node    Subprograms, Packages, Statements, Top
622 @section Subprograms
623 @c  -------------------------------------------------------------------------
624 @cindex Subprograms
625
626 @subsection Subprogram Declarations
627 @c  -------------------------------------------------------------------------
628 @itemize @bullet
629
630 @item
631 Do not write the @code{in} for parameters, especially in functions:
632
633 @smallexample @c adanocomment
634       function Length (S : String) return Integer;
635 @end smallexample
636
637 @item
638 When the declaration line for a procedure or a function is too long to fit
639 the entire declaration (including the keyword procedure or function) on a
640 single line, then fold it, putting a single parameter on a line, aligning
641 the colons, as in:
642
643 @smallexample @c adanocomment
644 @group
645      procedure Set_Heading
646         (Source : String;
647          Count  : Natural;
648          Pad    : Character := Space;
649          Fill   : Boolean   := True);
650 @end group
651 @end smallexample
652
653 @noindent
654 In the case of a function, if the entire spec does not fit on one line, then
655 the return may appear after the last parameter, as in:
656
657 @smallexample @c adanocomment
658 @group
659       function Head
660         (Source : String;
661          Count  : Natural;
662          Pad    : Character := Space) return String;
663 @end group
664 @end smallexample
665
666 @noindent
667 Or it may appear on its own as a separate line. This form is preferred when
668 putting the return on the same line as the last parameter would result in
669 an overlong line. The return type may optionally be aligned with the types
670 of the parameters (usually we do this aligning if it results only in a small
671 number of extra spaces, and otherwise we don't attempt to align). So two
672 alternative forms for the above spec are:
673
674 @smallexample @c adanocomment
675 @group
676       function Head
677         (Source : String;
678          Count  : Natural;
679          Pad    : Character := Space)
680          return   String;
681
682       function Head
683         (Source : String;
684          Count  : Natural;
685          Pad    : Character := Space)
686          return String;
687 @end group
688 @end smallexample
689
690 @end itemize
691
692 @subsection Subprogram Bodies
693 @c  -------------------------------------------------------------------------
694 @cindex Subprogram bodies
695
696 @itemize @bullet
697 @item
698 Function and procedure bodies should usually be sorted alphabetically. Do
699 not attempt to sort them in some logical order by functionality. For a
700 sequence of subrpgroams specs, a general alphabetical sorting is also
701 usually appropriate, but occasionally it makes sense to group by major
702 function, with appropriate headers.
703
704 @item
705 All subprograms have a header giving the function name, with the following
706 format:
707
708 @smallexample @c adanocomment
709 @group
710       -----------------
711       -- My_Function --
712       -----------------
713
714       procedure My_Function is
715       begin
716         ...
717       end My_Function;
718 @end group
719 @end smallexample
720
721 @noindent
722 Note that the name in the header is preceded by a single space,
723 not two spaces as for other comments.
724
725 @item
726 Every subprogram body must have a preceding @syntax{subprogram_declaration}.
727
728 @item
729 @cindex Blank lines (in subprogram bodies)
730 A sequence of declarations may optionally be separated from the following
731 begin by a blank line.  Just as we optionally allow blank lines in general
732 between declarations, this blank line should be present only if it improves
733 readability. Generally we avoid this blank line if the declarative part is
734 small (one or two lines) and we include it if the declarative part is long.
735
736 @item
737 If the declarations in a subprogram contain at least one nested
738 subprogram body, then just before the @code{begin} of the enclosing
739 subprogram, there is a comment line and a blank line:
740
741 @smallexample @c adanocomment
742 @group
743     --  Start of processing for @var{Enclosing_Subprogram}
744
745     begin
746       ...
747     end @var{Enclosing_Subprogram};
748 @end group
749 @end smallexample
750
751 @end itemize
752
753 @c  -------------------------------------------------------------------------
754 @node    Packages, Program Structure, Subprograms, Top
755 @section Packages and Visibility Rules
756 @c  -------------------------------------------------------------------------
757 @cindex Packages
758
759 @itemize @bullet
760 @item
761 All program units and subprograms have their name at the end:
762
763 @smallexample @c adanocomment
764 @group
765       package P is
766          ...
767       end P;
768 @end group
769 @end smallexample
770
771 @item
772 We will use the style of @code{use}-ing @code{with}-ed packages, with
773 the context clauses looking like:
774 @cindex @code{use} clauses
775
776 @smallexample @c adanocomment
777 @group
778       with A; use A;
779       with B; use B;
780 @end group
781 @end smallexample
782
783 @item
784 Names declared in the visible part of packages should be
785 unique, to prevent name clashes when the packages are @code{use}d.
786 @cindex Name clash avoidance
787
788 @smallexample @c adanocomment
789 @group
790       package Entity is
791          type Entity_Kind is ...;
792          ...
793       end Entity;
794 @end group
795 @end smallexample
796
797 @item
798 After the file header comment, the context clause and unit specification
799 should be the first thing in a @syntax{program_unit}.
800 @end itemize
801
802 @c  -------------------------------------------------------------------------
803 @node    Program Structure, GNU Free Documentation License, Packages, Top
804 @section Program Structure and Compilation Issues
805 @c  -------------------------------------------------------------------------
806 @cindex Program structure
807
808 @itemize @bullet
809 @item
810 Every GNAT source file must be compiled with the @option{-gnatg}
811 switch to check the coding style.
812 (Note that you should look at
813 @file{style.adb} to see the lexical rules enforced by
814 @option{-gnatg}).
815 @cindex @option{-gnatg} option (to gcc)
816 @cindex @file{style.adb} file
817
818 @item
819 Each source file should contain only one compilation unit.
820
821 @item
822 Filenames should be 8 or fewer characters, followed by the @code{.adb}
823 extension for a body or @code{.ads} for a spec.
824 @cindex File name length
825
826 @item
827 Unit names should be distinct when ``krunch''ed to 8 characters
828 (see @file{krunch.ads}) and the filenames should match the unit name,
829 except that they are all lower case.
830 @cindex @file{krunch.ads} file
831 @end itemize
832
833
834 @c **********************************
835 @c * GNU Free Documentation License *
836 @c **********************************
837 @include fdl.texi
838 @c GNU Free Documentation License
839 @cindex GNU Free Documentation License
840
841 @node Index,,GNU Free Documentation License, Top
842 @unnumberedsec Index
843
844 @printindex cp
845
846 @contents
847
848 @bye