]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/gnat_ugn.texi
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / gnat_ugn.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 _ U G N o
9 @c o
10 @c GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). o
11 @c o
12 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
13
14 @setfilename gnat_ugn.info
15
16 @copying
17 Copyright @copyright{} 1995-2009 Free Software Foundation,
18 Inc.
19
20 Permission is granted to copy, distribute and/or modify this document
21 under the terms of the GNU Free Documentation License, Version 1.3 or
22 any later version published by the Free Software Foundation; with no
23 Invariant Sections, with no Front-Cover Texts and with no Back-Cover
24 Texts. A copy of the license is included in the section entitled
25 ``GNU Free Documentation License''.
26 @end copying
27
28 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
29 @c
30 @c GNAT_UGN Style Guide
31 @c
32 @c 1. Always put a @noindent on the line before the first paragraph
33 @c after any of these commands:
34 @c
35 @c @chapter
36 @c @section
37 @c @subsection
38 @c @subsubsection
39 @c @subsubsubsection
40 @c
41 @c @end smallexample
42 @c @end itemize
43 @c @end enumerate
44 @c
45 @c 2. DO NOT use @example. Use @smallexample instead.
46 @c a) DO NOT use highlighting commands (@b{}, @i{}) inside an @smallexample
47 @c context. These can interfere with the readability of the texi
48 @c source file. Instead, use one of the following annotated
49 @c @smallexample commands, and preprocess the texi file with the
50 @c ada2texi tool (which generates appropriate highlighting):
51 @c @smallexample @c ada
52 @c @smallexample @c adanocomment
53 @c @smallexample @c projectfile
54 @c b) The "@c ada" markup will result in boldface for reserved words
55 @c and italics for comments
56 @c c) The "@c adanocomment" markup will result only in boldface for
57 @c reserved words (comments are left alone)
58 @c d) The "@c projectfile" markup is like "@c ada" except that the set
59 @c of reserved words include the new reserved words for project files
60 @c
61 @c 3. Each @chapter, @section, @subsection, @subsubsection, etc.
62 @c command must be preceded by two empty lines
63 @c
64 @c 4. The @item command should be on a line of its own if it is in an
65 @c @itemize or @enumerate command.
66 @c
67 @c 5. When talking about ALI files use "ALI" (all uppercase), not "Ali"
68 @c or "ali".
69 @c
70 @c 6. DO NOT put trailing spaces at the end of a line. Such spaces will
71 @c cause the document build to fail.
72 @c
73 @c 7. DO NOT use @cartouche for examples that are longer than around 10 lines.
74 @c This command inhibits page breaks, so long examples in a @cartouche can
75 @c lead to large, ugly patches of empty space on a page.
76 @c
77 @c NOTE: This file should be submitted to xgnatugn with either the vms flag
78 @c or the unw flag set. The unw flag covers topics for both Unix and
79 @c Windows.
80 @c
81 @c oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
82
83 @set NOW January 2007
84 @c This flag is used where the text refers to conditions that exist when the
85 @c text was entered into the document but which may change over time.
86 @c Update the setting for the flag, and (if necessary) the text surrounding,
87 @c the references to the flag, on future doc revisions:
88 @c search for @value{NOW}.
89
90 @set FSFEDITION
91 @set EDITION GNAT
92 @set DEFAULTLANGUAGEVERSION Ada 2005
93 @set NONDEFAULTLANGUAGEVERSION Ada 95
94
95 @ifset unw
96 @set PLATFORM
97 @end ifset
98
99 @ifset vms
100 @set PLATFORM OpenVMS
101 @end ifset
102
103 @c @ovar(ARG)
104 @c ----------
105 @c The ARG is an optional argument. To be used for macro arguments in
106 @c their documentation (@defmac).
107 @macro ovar{varname}
108 @r{[}@var{\varname\}@r{]}@c
109 @end macro
110 @c Status as of November 2009:
111 @c Unfortunately texi2pdf and texi2html treat the trailing "@c"
112 @c differently, and faulty output is produced by one or the other
113 @c depending on whether the "@c" is present or absent.
114 @c As a result, the @ovar macro is not used, and all invocations
115 @c of the @ovar macro have been expanded inline.
116
117
118 @settitle @value{EDITION} User's Guide @value{PLATFORM}
119 @dircategory GNU Ada tools
120 @direntry
121 * @value{EDITION} User's Guide: (gnat_ugn). @value{PLATFORM}
122 @end direntry
123
124 @include gcc-common.texi
125
126 @setchapternewpage odd
127 @syncodeindex fn cp
128 @c %**end of header
129
130 @titlepage
131 @title @value{EDITION} User's Guide
132 @ifset vms
133 @sp 1
134 @flushright
135 @titlefont{@i{@value{PLATFORM}}}
136 @end flushright
137 @end ifset
138
139 @sp 2
140
141 @subtitle GNAT, The GNU Ada Compiler
142 @versionsubtitle
143 @author AdaCore
144
145 @page
146 @vskip 0pt plus 1filll
147
148 @insertcopying
149
150 @end titlepage
151
152 @ifnottex
153 @node Top, About This Guide, (dir), (dir)
154 @top @value{EDITION} User's Guide
155
156 @noindent
157 @value{EDITION} User's Guide @value{PLATFORM}
158
159 @noindent
160 GNAT, The GNU Ada Compiler@*
161 GCC version @value{version-GCC}@*
162
163 @noindent
164 AdaCore@*
165
166 @menu
167 * About This Guide::
168 * Getting Started with GNAT::
169 * The GNAT Compilation Model::
170 * Compiling Using gcc::
171 * Binding Using gnatbind::
172 * Linking Using gnatlink::
173 * The GNAT Make Program gnatmake::
174 * Improving Performance::
175 * Renaming Files Using gnatchop::
176 * Configuration Pragmas::
177 * Handling Arbitrary File Naming Conventions Using gnatname::
178 * GNAT Project Manager::
179 * The Cross-Referencing Tools gnatxref and gnatfind::
180 * The GNAT Pretty-Printer gnatpp::
181 * The GNAT Metric Tool gnatmetric::
182 * File Name Krunching Using gnatkr::
183 * Preprocessing Using gnatprep::
184 @ifset vms
185 * The GNAT Run-Time Library Builder gnatlbr::
186 @end ifset
187 * The GNAT Library Browser gnatls::
188 * Cleaning Up Using gnatclean::
189 @ifclear vms
190 * GNAT and Libraries::
191 * Using the GNU make Utility::
192 @end ifclear
193 * Memory Management Issues::
194 * Stack Related Facilities::
195 * Verifying Properties Using gnatcheck::
196 * Creating Sample Bodies Using gnatstub::
197 * Generating Ada Bindings for C and C++ headers::
198 * Other Utility Programs::
199 * Running and Debugging Ada Programs::
200 @ifclear vms
201 * Code Coverage and Profiling::
202 @end ifclear
203 @ifset vms
204 * Compatibility with HP Ada::
205 @end ifset
206 * Platform-Specific Information for the Run-Time Libraries::
207 * Example of Binder Output File::
208 * Elaboration Order Handling in GNAT::
209 * Conditional Compilation::
210 * Inline Assembler::
211 * Compatibility and Porting Guide::
212 @ifset unw
213 * Microsoft Windows Topics::
214 @end ifset
215 * GNU Free Documentation License::
216 * Index::
217
218 --- The Detailed Node Listing ---
219
220 About This Guide
221
222 * What This Guide Contains::
223 * What You Should Know before Reading This Guide::
224 * Related Information::
225 * Conventions::
226
227 Getting Started with GNAT
228
229 * Running GNAT::
230 * Running a Simple Ada Program::
231 * Running a Program with Multiple Units::
232 * Using the gnatmake Utility::
233 @ifset vms
234 * Editing with Emacs::
235 @end ifset
236 @ifclear vms
237 * Introduction to GPS::
238 @end ifclear
239
240 The GNAT Compilation Model
241
242 * Source Representation::
243 * Foreign Language Representation::
244 * File Naming Rules::
245 * Using Other File Names::
246 * Alternative File Naming Schemes::
247 * Generating Object Files::
248 * Source Dependencies::
249 * The Ada Library Information Files::
250 * Binding an Ada Program::
251 * Mixed Language Programming::
252 @ifclear vms
253 * Building Mixed Ada & C++ Programs::
254 * Comparison between GNAT and C/C++ Compilation Models::
255 @end ifclear
256 * Comparison between GNAT and Conventional Ada Library Models::
257 @ifset vms
258 * Placement of temporary files::
259 @end ifset
260
261 Foreign Language Representation
262
263 * Latin-1::
264 * Other 8-Bit Codes::
265 * Wide Character Encodings::
266
267 Compiling Ada Programs With gcc
268
269 * Compiling Programs::
270 * Switches for gcc::
271 * Search Paths and the Run-Time Library (RTL)::
272 * Order of Compilation Issues::
273 * Examples::
274
275 Switches for gcc
276
277 * Output and Error Message Control::
278 * Warning Message Control::
279 * Debugging and Assertion Control::
280 * Validity Checking::
281 * Style Checking::
282 * Run-Time Checks::
283 * Using gcc for Syntax Checking::
284 * Using gcc for Semantic Checking::
285 * Compiling Different Versions of Ada::
286 * Character Set Control::
287 * File Naming Control::
288 * Subprogram Inlining Control::
289 * Auxiliary Output Control::
290 * Debugging Control::
291 * Exception Handling Control::
292 * Units to Sources Mapping Files::
293 * Integrated Preprocessing::
294 @ifset vms
295 * Return Codes::
296 @end ifset
297
298 Binding Ada Programs With gnatbind
299
300 * Running gnatbind::
301 * Switches for gnatbind::
302 * Command-Line Access::
303 * Search Paths for gnatbind::
304 * Examples of gnatbind Usage::
305
306 Switches for gnatbind
307
308 * Consistency-Checking Modes::
309 * Binder Error Message Control::
310 * Elaboration Control::
311 * Output Control::
312 * Binding with Non-Ada Main Programs::
313 * Binding Programs with No Main Subprogram::
314
315 Linking Using gnatlink
316
317 * Running gnatlink::
318 * Switches for gnatlink::
319
320 The GNAT Make Program gnatmake
321
322 * Running gnatmake::
323 * Switches for gnatmake::
324 * Mode Switches for gnatmake::
325 * Notes on the Command Line::
326 * How gnatmake Works::
327 * Examples of gnatmake Usage::
328
329 Improving Performance
330 * Performance Considerations::
331 * Text_IO Suggestions::
332 * Reducing Size of Ada Executables with gnatelim::
333 * Reducing Size of Executables with unused subprogram/data elimination::
334
335 Performance Considerations
336 * Controlling Run-Time Checks::
337 * Use of Restrictions::
338 * Optimization Levels::
339 * Debugging Optimized Code::
340 * Inlining of Subprograms::
341 * Other Optimization Switches::
342 * Optimization and Strict Aliasing::
343 @ifset vms
344 * Coverage Analysis::
345 @end ifset
346
347 Reducing Size of Ada Executables with gnatelim
348 * About gnatelim::
349 * Running gnatelim::
350 * Correcting the List of Eliminate Pragmas::
351 * Making Your Executables Smaller::
352 * Summary of the gnatelim Usage Cycle::
353
354 Reducing Size of Executables with unused subprogram/data elimination
355 * About unused subprogram/data elimination::
356 * Compilation options::
357
358 Renaming Files Using gnatchop
359
360 * Handling Files with Multiple Units::
361 * Operating gnatchop in Compilation Mode::
362 * Command Line for gnatchop::
363 * Switches for gnatchop::
364 * Examples of gnatchop Usage::
365
366 Configuration Pragmas
367
368 * Handling of Configuration Pragmas::
369 * The Configuration Pragmas Files::
370
371 Handling Arbitrary File Naming Conventions Using gnatname
372
373 * Arbitrary File Naming Conventions::
374 * Running gnatname::
375 * Switches for gnatname::
376 * Examples of gnatname Usage::
377
378 GNAT Project Manager
379
380 * Introduction::
381 * Examples of Project Files::
382 * Project File Syntax::
383 * Objects and Sources in Project Files::
384 * Importing Projects::
385 * Project Extension::
386 * Project Hierarchy Extension::
387 * External References in Project Files::
388 * Packages in Project Files::
389 * Variables from Imported Projects::
390 * Naming Schemes::
391 * Library Projects::
392 * Stand-alone Library Projects::
393 * Switches Related to Project Files::
394 * Tools Supporting Project Files::
395 * An Extended Example::
396 * Project File Complete Syntax::
397
398 The Cross-Referencing Tools gnatxref and gnatfind
399
400 * Switches for gnatxref::
401 * Switches for gnatfind::
402 * Project Files for gnatxref and gnatfind::
403 * Regular Expressions in gnatfind and gnatxref::
404 * Examples of gnatxref Usage::
405 * Examples of gnatfind Usage::
406
407 The GNAT Pretty-Printer gnatpp
408
409 * Switches for gnatpp::
410 * Formatting Rules::
411
412 The GNAT Metrics Tool gnatmetric
413
414 * Switches for gnatmetric::
415
416 File Name Krunching Using gnatkr
417
418 * About gnatkr::
419 * Using gnatkr::
420 * Krunching Method::
421 * Examples of gnatkr Usage::
422
423 Preprocessing Using gnatprep
424 * Preprocessing Symbols::
425 * Using gnatprep::
426 * Switches for gnatprep::
427 * Form of Definitions File::
428 * Form of Input Text for gnatprep::
429
430 @ifset vms
431 The GNAT Run-Time Library Builder gnatlbr
432
433 * Running gnatlbr::
434 * Switches for gnatlbr::
435 * Examples of gnatlbr Usage::
436 @end ifset
437
438 The GNAT Library Browser gnatls
439
440 * Running gnatls::
441 * Switches for gnatls::
442 * Examples of gnatls Usage::
443
444 Cleaning Up Using gnatclean
445
446 * Running gnatclean::
447 * Switches for gnatclean::
448 @c * Examples of gnatclean Usage::
449
450 @ifclear vms
451
452 GNAT and Libraries
453
454 * Introduction to Libraries in GNAT::
455 * General Ada Libraries::
456 * Stand-alone Ada Libraries::
457 * Rebuilding the GNAT Run-Time Library::
458
459 Using the GNU make Utility
460
461 * Using gnatmake in a Makefile::
462 * Automatically Creating a List of Directories::
463 * Generating the Command Line Switches::
464 * Overcoming Command Line Length Limits::
465 @end ifclear
466
467 Memory Management Issues
468
469 * Some Useful Memory Pools::
470 * The GNAT Debug Pool Facility::
471 @ifclear vms
472 * The gnatmem Tool::
473 @end ifclear
474
475 Stack Related Facilities
476
477 * Stack Overflow Checking::
478 * Static Stack Usage Analysis::
479 * Dynamic Stack Usage Analysis::
480
481 Some Useful Memory Pools
482
483 The GNAT Debug Pool Facility
484
485 @ifclear vms
486 The gnatmem Tool
487
488 * Running gnatmem::
489 * Switches for gnatmem::
490 * Example of gnatmem Usage::
491 @end ifclear
492
493 Verifying Properties Using gnatcheck
494
495 * Format of the Report File::
496 * General gnatcheck Switches::
497 * gnatcheck Rule Options::
498 * Adding the Results of Compiler Checks to gnatcheck Output::
499 * Project-Wide Checks::
500 * Rule exemption::
501 * Predefined Rules::
502 * Example of gnatcheck Usage::
503
504 Sample Bodies Using gnatstub
505
506 * Running gnatstub::
507 * Switches for gnatstub::
508
509 Other Utility Programs
510
511 * Using Other Utility Programs with GNAT::
512 * The External Symbol Naming Scheme of GNAT::
513 * Converting Ada Files to html with gnathtml::
514
515 @ifclear vms
516 Code Coverage and Profiling
517
518 * Code Coverage of Ada Programs using gcov::
519 * Profiling an Ada Program using gprof::
520 @end ifclear
521
522 Running and Debugging Ada Programs
523
524 * The GNAT Debugger GDB::
525 * Running GDB::
526 * Introduction to GDB Commands::
527 * Using Ada Expressions::
528 * Calling User-Defined Subprograms::
529 * Using the Next Command in a Function::
530 * Ada Exceptions::
531 * Ada Tasks::
532 * Debugging Generic Units::
533 * GNAT Abnormal Termination or Failure to Terminate::
534 * Naming Conventions for GNAT Source Files::
535 * Getting Internal Debugging Information::
536 * Stack Traceback::
537
538 @ifset vms
539 * LSE::
540 @end ifset
541
542 @ifset vms
543 Compatibility with HP Ada
544
545 * Ada Language Compatibility::
546 * Differences in the Definition of Package System::
547 * Language-Related Features::
548 * The Package STANDARD::
549 * The Package SYSTEM::
550 * Tasking and Task-Related Features::
551 * Pragmas and Pragma-Related Features::
552 * Library of Predefined Units::
553 * Bindings::
554 * Main Program Definition::
555 * Implementation-Defined Attributes::
556 * Compiler and Run-Time Interfacing::
557 * Program Compilation and Library Management::
558 * Input-Output::
559 * Implementation Limits::
560 * Tools and Utilities::
561
562 Language-Related Features
563
564 * Integer Types and Representations::
565 * Floating-Point Types and Representations::
566 * Pragmas Float_Representation and Long_Float::
567 * Fixed-Point Types and Representations::
568 * Record and Array Component Alignment::
569 * Address Clauses::
570 * Other Representation Clauses::
571
572 Tasking and Task-Related Features
573
574 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
575 * Assigning Task IDs::
576 * Task IDs and Delays::
577 * Task-Related Pragmas::
578 * Scheduling and Task Priority::
579 * The Task Stack::
580 * External Interrupts::
581
582 Pragmas and Pragma-Related Features
583
584 * Restrictions on the Pragma INLINE::
585 * Restrictions on the Pragma INTERFACE::
586 * Restrictions on the Pragma SYSTEM_NAME::
587
588 Library of Predefined Units
589
590 * Changes to DECLIB::
591
592 Bindings
593
594 * Shared Libraries and Options Files::
595 * Interfaces to C::
596 @end ifset
597
598 Platform-Specific Information for the Run-Time Libraries
599
600 * Summary of Run-Time Configurations::
601 * Specifying a Run-Time Library::
602 * Choosing the Scheduling Policy::
603 * Solaris-Specific Considerations::
604 * Linux-Specific Considerations::
605 * AIX-Specific Considerations::
606 * Irix-Specific Considerations::
607 * RTX-Specific Considerations::
608 * HP-UX-Specific Considerations::
609
610 Example of Binder Output File
611
612 Elaboration Order Handling in GNAT
613
614 * Elaboration Code::
615 * Checking the Elaboration Order::
616 * Controlling the Elaboration Order::
617 * Controlling Elaboration in GNAT - Internal Calls::
618 * Controlling Elaboration in GNAT - External Calls::
619 * Default Behavior in GNAT - Ensuring Safety::
620 * Treatment of Pragma Elaborate::
621 * Elaboration Issues for Library Tasks::
622 * Mixing Elaboration Models::
623 * What to Do If the Default Elaboration Behavior Fails::
624 * Elaboration for Access-to-Subprogram Values::
625 * Summary of Procedures for Elaboration Control::
626 * Other Elaboration Order Considerations::
627
628 Conditional Compilation
629 * Use of Boolean Constants::
630 * Debugging - A Special Case::
631 * Conditionalizing Declarations::
632 * Use of Alternative Implementations::
633 * Preprocessing::
634
635 Inline Assembler
636
637 * Basic Assembler Syntax::
638 * A Simple Example of Inline Assembler::
639 * Output Variables in Inline Assembler::
640 * Input Variables in Inline Assembler::
641 * Inlining Inline Assembler Code::
642 * Other Asm Functionality::
643
644 Compatibility and Porting Guide
645
646 * Compatibility with Ada 83::
647 * Compatibility between Ada 95 and Ada 2005::
648 * Implementation-dependent characteristics::
649 @ifclear vms
650 @c This brief section is only in the non-VMS version
651 @c The complete chapter on HP Ada issues is in the VMS version
652 * Compatibility with HP Ada 83::
653 @end ifclear
654 * Compatibility with Other Ada Systems::
655 * Representation Clauses::
656 @ifset vms
657 * Transitioning to 64-Bit GNAT for OpenVMS::
658 @end ifset
659
660 @ifset unw
661 Microsoft Windows Topics
662
663 * Using GNAT on Windows::
664 * CONSOLE and WINDOWS subsystems::
665 * Temporary Files::
666 * Mixed-Language Programming on Windows::
667 * Windows Calling Conventions::
668 * Introduction to Dynamic Link Libraries (DLLs)::
669 * Using DLLs with GNAT::
670 * Building DLLs with GNAT::
671 * GNAT and Windows Resources::
672 * Debugging a DLL::
673 * Setting Stack Size from gnatlink::
674 * Setting Heap Size from gnatlink::
675 @end ifset
676
677 * Index::
678 @end menu
679 @end ifnottex
680
681 @node About This Guide
682 @unnumbered About This Guide
683
684 @noindent
685 @ifset vms
686 This guide describes the use of @value{EDITION},
687 a compiler and software development toolset for the full Ada
688 programming language, implemented on OpenVMS for HP's Alpha and
689 Integrity server (I64) platforms.
690 @end ifset
691 @ifclear vms
692 This guide describes the use of @value{EDITION},
693 a compiler and software development
694 toolset for the full Ada programming language.
695 @end ifclear
696 It documents the features of the compiler and tools, and explains
697 how to use them to build Ada applications.
698
699 @value{EDITION} implements Ada 95 and Ada 2005, and it may also be invoked in
700 Ada 83 compatibility mode.
701 By default, @value{EDITION} assumes @value{DEFAULTLANGUAGEVERSION},
702 but you can override with a compiler switch
703 (@pxref{Compiling Different Versions of Ada})
704 to explicitly specify the language version.
705 Throughout this manual, references to ``Ada'' without a year suffix
706 apply to both the Ada 95 and Ada 2005 versions of the language.
707
708
709 @ifclear FSFEDITION
710 For ease of exposition, ``@value{EDITION}'' will be referred to simply as
711 ``GNAT'' in the remainder of this document.
712 @end ifclear
713
714
715
716
717 @menu
718 * What This Guide Contains::
719 * What You Should Know before Reading This Guide::
720 * Related Information::
721 * Conventions::
722 @end menu
723
724 @node What This Guide Contains
725 @unnumberedsec What This Guide Contains
726
727 @noindent
728 This guide contains the following chapters:
729 @itemize @bullet
730
731 @item
732 @ref{Getting Started with GNAT}, describes how to get started compiling
733 and running Ada programs with the GNAT Ada programming environment.
734 @item
735 @ref{The GNAT Compilation Model}, describes the compilation model used
736 by GNAT.
737
738 @item
739 @ref{Compiling Using gcc}, describes how to compile
740 Ada programs with @command{gcc}, the Ada compiler.
741
742 @item
743 @ref{Binding Using gnatbind}, describes how to
744 perform binding of Ada programs with @code{gnatbind}, the GNAT binding
745 utility.
746
747 @item
748 @ref{Linking Using gnatlink},
749 describes @command{gnatlink}, a
750 program that provides for linking using the GNAT run-time library to
751 construct a program. @command{gnatlink} can also incorporate foreign language
752 object units into the executable.
753
754 @item
755 @ref{The GNAT Make Program gnatmake}, describes @command{gnatmake}, a
756 utility that automatically determines the set of sources
757 needed by an Ada compilation unit, and executes the necessary compilations
758 binding and link.
759
760 @item
761 @ref{Improving Performance}, shows various techniques for making your
762 Ada program run faster or take less space.
763 It discusses the effect of the compiler's optimization switch and
764 also describes the @command{gnatelim} tool and unused subprogram/data
765 elimination.
766
767 @item
768 @ref{Renaming Files Using gnatchop}, describes
769 @code{gnatchop}, a utility that allows you to preprocess a file that
770 contains Ada source code, and split it into one or more new files, one
771 for each compilation unit.
772
773 @item
774 @ref{Configuration Pragmas}, describes the configuration pragmas
775 handled by GNAT.
776
777 @item
778 @ref{Handling Arbitrary File Naming Conventions Using gnatname},
779 shows how to override the default GNAT file naming conventions,
780 either for an individual unit or globally.
781
782 @item
783 @ref{GNAT Project Manager}, describes how to use project files
784 to organize large projects.
785
786 @item
787 @ref{The Cross-Referencing Tools gnatxref and gnatfind}, discusses
788 @code{gnatxref} and @code{gnatfind}, two tools that provide an easy
789 way to navigate through sources.
790
791 @item
792 @ref{The GNAT Pretty-Printer gnatpp}, shows how to produce a reformatted
793 version of an Ada source file with control over casing, indentation,
794 comment placement, and other elements of program presentation style.
795
796 @item
797 @ref{The GNAT Metric Tool gnatmetric}, shows how to compute various
798 metrics for an Ada source file, such as the number of types and subprograms,
799 and assorted complexity measures.
800
801 @item
802 @ref{File Name Krunching Using gnatkr}, describes the @code{gnatkr}
803 file name krunching utility, used to handle shortened
804 file names on operating systems with a limit on the length of names.
805
806 @item
807 @ref{Preprocessing Using gnatprep}, describes @code{gnatprep}, a
808 preprocessor utility that allows a single source file to be used to
809 generate multiple or parameterized source files by means of macro
810 substitution.
811
812 @ifset vms
813 @item
814 @ref{The GNAT Run-Time Library Builder gnatlbr}, describes @command{gnatlbr},
815 a tool for rebuilding the GNAT run time with user-supplied
816 configuration pragmas.
817 @end ifset
818
819 @item
820 @ref{The GNAT Library Browser gnatls}, describes @code{gnatls}, a
821 utility that displays information about compiled units, including dependences
822 on the corresponding sources files, and consistency of compilations.
823
824 @item
825 @ref{Cleaning Up Using gnatclean}, describes @code{gnatclean}, a utility
826 to delete files that are produced by the compiler, binder and linker.
827
828 @ifclear vms
829 @item
830 @ref{GNAT and Libraries}, describes the process of creating and using
831 Libraries with GNAT. It also describes how to recompile the GNAT run-time
832 library.
833
834 @item
835 @ref{Using the GNU make Utility}, describes some techniques for using
836 the GNAT toolset in Makefiles.
837 @end ifclear
838
839 @item
840 @ref{Memory Management Issues}, describes some useful predefined storage pools
841 and in particular the GNAT Debug Pool facility, which helps detect incorrect
842 memory references.
843 @ifclear vms
844 It also describes @command{gnatmem}, a utility that monitors dynamic
845 allocation and deallocation and helps detect ``memory leaks''.
846 @end ifclear
847
848 @item
849 @ref{Stack Related Facilities}, describes some useful tools associated with
850 stack checking and analysis.
851
852 @item
853 @ref{Verifying Properties Using gnatcheck}, discusses @code{gnatcheck},
854 a utility that checks Ada code against a set of rules.
855
856 @item
857 @ref{Creating Sample Bodies Using gnatstub}, discusses @code{gnatstub},
858 a utility that generates empty but compilable bodies for library units.
859
860 @item
861 @ref{Generating Ada Bindings for C and C++ headers}, describes how to
862 generate automatically Ada bindings from C and C++ headers.
863
864 @item
865 @ref{Other Utility Programs}, discusses several other GNAT utilities,
866 including @code{gnathtml}.
867
868 @ifclear vms
869 @item
870 @ref{Code Coverage and Profiling}, describes how to perform a structural
871 coverage and profile the execution of Ada programs.
872 @end ifclear
873
874 @item
875 @ref{Running and Debugging Ada Programs}, describes how to run and debug
876 Ada programs.
877
878 @ifset vms
879 @item
880 @ref{Compatibility with HP Ada}, details the compatibility of GNAT with
881 HP Ada 83 @footnote{``HP Ada'' refers to the legacy product originally
882 developed by Digital Equipment Corporation and currently supported by HP.}
883 for OpenVMS Alpha. This product was formerly known as DEC Ada,
884 @cindex DEC Ada
885 and for
886 historical compatibility reasons, the relevant libraries still use the
887 DEC prefix.
888 @end ifset
889
890 @item
891 @ref{Platform-Specific Information for the Run-Time Libraries},
892 describes the various run-time
893 libraries supported by GNAT on various platforms and explains how to
894 choose a particular library.
895
896 @item
897 @ref{Example of Binder Output File}, shows the source code for the binder
898 output file for a sample program.
899
900 @item
901 @ref{Elaboration Order Handling in GNAT}, describes how GNAT helps
902 you deal with elaboration order issues.
903
904 @item
905 @ref{Conditional Compilation}, describes how to model conditional compilation,
906 both with Ada in general and with GNAT facilities in particular.
907
908 @item
909 @ref{Inline Assembler}, shows how to use the inline assembly facility
910 in an Ada program.
911
912 @item
913 @ref{Compatibility and Porting Guide}, contains sections on compatibility
914 of GNAT with other Ada development environments (including Ada 83 systems),
915 to assist in porting code from those environments.
916
917 @ifset unw
918 @item
919 @ref{Microsoft Windows Topics}, presents information relevant to the
920 Microsoft Windows platform.
921 @end ifset
922 @end itemize
923
924 @c *************************************************
925 @node What You Should Know before Reading This Guide
926 @c *************************************************
927 @unnumberedsec What You Should Know before Reading This Guide
928
929 @cindex Ada 95 Language Reference Manual
930 @cindex Ada 2005 Language Reference Manual
931 @noindent
932 This guide assumes a basic familiarity with the Ada 95 language, as
933 described in the International Standard ANSI/ISO/IEC-8652:1995, January
934 1995.
935 It does not require knowledge of the new features introduced by Ada 2005,
936 (officially known as ISO/IEC 8652:1995 with Technical Corrigendum 1
937 and Amendment 1).
938 Both reference manuals are included in the GNAT documentation
939 package.
940
941 @node Related Information
942 @unnumberedsec Related Information
943
944 @noindent
945 For further information about related tools, refer to the following
946 documents:
947
948 @itemize @bullet
949 @item
950 @xref{Top, GNAT Reference Manual, About This Guide, gnat_rm, GNAT
951 Reference Manual}, which contains all reference material for the GNAT
952 implementation of Ada.
953
954 @ifset unw
955 @item
956 @cite{Using the GNAT Programming Studio}, which describes the GPS
957 Integrated Development Environment.
958
959 @item
960 @cite{GNAT Programming Studio Tutorial}, which introduces the
961 main GPS features through examples.
962 @end ifset
963
964 @item
965 @cite{Ada 95 Reference Manual}, which contains reference
966 material for the Ada 95 programming language.
967
968 @item
969 @cite{Ada 2005 Reference Manual}, which contains reference
970 material for the Ada 2005 programming language.
971
972 @item
973 @xref{Top,, Debugging with GDB, gdb, Debugging with GDB},
974 @ifset vms
975 in the GNU:[DOCS] directory,
976 @end ifset
977 for all details on the use of the GNU source-level debugger.
978
979 @item
980 @xref{Top,, The extensible self-documenting text editor, emacs,
981 GNU Emacs Manual},
982 @ifset vms
983 located in the GNU:[DOCS] directory if the EMACS kit is installed,
984 @end ifset
985 for full information on the extensible editor and programming
986 environment Emacs.
987
988 @end itemize
989
990 @c **************
991 @node Conventions
992 @unnumberedsec Conventions
993 @cindex Conventions
994 @cindex Typographical conventions
995
996 @noindent
997 Following are examples of the typographical and graphic conventions used
998 in this guide:
999
1000 @itemize @bullet
1001 @item
1002 @code{Functions}, @command{utility program names}, @code{standard names},
1003 and @code{classes}.
1004
1005 @item
1006 @option{Option flags}
1007
1008 @item
1009 @file{File names}, @samp{button names}, and @samp{field names}.
1010
1011 @item
1012 @code{Variables}, @env{environment variables}, and @var{metasyntactic
1013 variables}.
1014
1015 @item
1016 @emph{Emphasis}.
1017
1018 @item
1019 @r{[}optional information or parameters@r{]}
1020
1021 @item
1022 Examples are described by text
1023 @smallexample
1024 and then shown this way.
1025 @end smallexample
1026 @end itemize
1027
1028 @noindent
1029 Commands that are entered by the user are preceded in this manual by the
1030 characters @w{``@code{$ }''} (dollar sign followed by space). If your system
1031 uses this sequence as a prompt, then the commands will appear exactly as
1032 you see them in the manual. If your system uses some other prompt, then
1033 the command will appear with the @code{$} replaced by whatever prompt
1034 character you are using.
1035
1036 @ifset unw
1037 Full file names are shown with the ``@code{/}'' character
1038 as the directory separator; e.g., @file{parent-dir/subdir/myfile.adb}.
1039 If you are using GNAT on a Windows platform, please note that
1040 the ``@code{\}'' character should be used instead.
1041 @end ifset
1042
1043 @c ****************************
1044 @node Getting Started with GNAT
1045 @chapter Getting Started with GNAT
1046
1047 @noindent
1048 This chapter describes some simple ways of using GNAT to build
1049 executable Ada programs.
1050 @ifset unw
1051 @ref{Running GNAT}, through @ref{Using the gnatmake Utility},
1052 show how to use the command line environment.
1053 @ref{Introduction to GPS}, provides a brief
1054 introduction to the GNAT Programming Studio, a visually-oriented
1055 Integrated Development Environment for GNAT.
1056 GPS offers a graphical ``look and feel'', support for development in
1057 other programming languages, comprehensive browsing features, and
1058 many other capabilities.
1059 For information on GPS please refer to
1060 @cite{Using the GNAT Programming Studio}.
1061 @end ifset
1062
1063 @menu
1064 * Running GNAT::
1065 * Running a Simple Ada Program::
1066 * Running a Program with Multiple Units::
1067 * Using the gnatmake Utility::
1068 @ifset vms
1069 * Editing with Emacs::
1070 @end ifset
1071 @ifclear vms
1072 * Introduction to GPS::
1073 @end ifclear
1074 @end menu
1075
1076 @node Running GNAT
1077 @section Running GNAT
1078
1079 @noindent
1080 Three steps are needed to create an executable file from an Ada source
1081 file:
1082
1083 @enumerate
1084 @item
1085 The source file(s) must be compiled.
1086 @item
1087 The file(s) must be bound using the GNAT binder.
1088 @item
1089 All appropriate object files must be linked to produce an executable.
1090 @end enumerate
1091
1092 @noindent
1093 All three steps are most commonly handled by using the @command{gnatmake}
1094 utility program that, given the name of the main program, automatically
1095 performs the necessary compilation, binding and linking steps.
1096
1097 @node Running a Simple Ada Program
1098 @section Running a Simple Ada Program
1099
1100 @noindent
1101 Any text editor may be used to prepare an Ada program.
1102 (If @code{Emacs} is
1103 used, the optional Ada mode may be helpful in laying out the program.)
1104 The
1105 program text is a normal text file. We will assume in our initial
1106 example that you have used your editor to prepare the following
1107 standard format text file:
1108
1109 @smallexample @c ada
1110 @cartouche
1111 with Ada.Text_IO; use Ada.Text_IO;
1112 procedure Hello is
1113 begin
1114 Put_Line ("Hello WORLD!");
1115 end Hello;
1116 @end cartouche
1117 @end smallexample
1118
1119 @noindent
1120 This file should be named @file{hello.adb}.
1121 With the normal default file naming conventions, GNAT requires
1122 that each file
1123 contain a single compilation unit whose file name is the
1124 unit name,
1125 with periods replaced by hyphens; the
1126 extension is @file{ads} for a
1127 spec and @file{adb} for a body.
1128 You can override this default file naming convention by use of the
1129 special pragma @code{Source_File_Name} (@pxref{Using Other File Names}).
1130 Alternatively, if you want to rename your files according to this default
1131 convention, which is probably more convenient if you will be using GNAT
1132 for all your compilations, then the @code{gnatchop} utility
1133 can be used to generate correctly-named source files
1134 (@pxref{Renaming Files Using gnatchop}).
1135
1136 You can compile the program using the following command (@code{$} is used
1137 as the command prompt in the examples in this document):
1138
1139 @smallexample
1140 $ gcc -c hello.adb
1141 @end smallexample
1142
1143 @noindent
1144 @command{gcc} is the command used to run the compiler. This compiler is
1145 capable of compiling programs in several languages, including Ada and
1146 C. It assumes that you have given it an Ada program if the file extension is
1147 either @file{.ads} or @file{.adb}, and it will then call
1148 the GNAT compiler to compile the specified file.
1149
1150 @ifclear vms
1151 The @option{-c} switch is required. It tells @command{gcc} to only do a
1152 compilation. (For C programs, @command{gcc} can also do linking, but this
1153 capability is not used directly for Ada programs, so the @option{-c}
1154 switch must always be present.)
1155 @end ifclear
1156
1157 This compile command generates a file
1158 @file{hello.o}, which is the object
1159 file corresponding to your Ada program. It also generates
1160 an ``Ada Library Information'' file @file{hello.ali},
1161 which contains additional information used to check
1162 that an Ada program is consistent.
1163 To build an executable file,
1164 use @code{gnatbind} to bind the program
1165 and @command{gnatlink} to link it. The
1166 argument to both @code{gnatbind} and @command{gnatlink} is the name of the
1167 @file{ALI} file, but the default extension of @file{.ali} can
1168 be omitted. This means that in the most common case, the argument
1169 is simply the name of the main program:
1170
1171 @smallexample
1172 $ gnatbind hello
1173 $ gnatlink hello
1174 @end smallexample
1175
1176 @noindent
1177 A simpler method of carrying out these steps is to use
1178 @command{gnatmake},
1179 a master program that invokes all the required
1180 compilation, binding and linking tools in the correct order. In particular,
1181 @command{gnatmake} automatically recompiles any sources that have been
1182 modified since they were last compiled, or sources that depend
1183 on such modified sources, so that ``version skew'' is avoided.
1184 @cindex Version skew (avoided by @command{gnatmake})
1185
1186 @smallexample
1187 $ gnatmake hello.adb
1188 @end smallexample
1189
1190 @noindent
1191 The result is an executable program called @file{hello}, which can be
1192 run by entering:
1193
1194 @smallexample
1195 $ ^hello^RUN HELLO^
1196 @end smallexample
1197
1198 @noindent
1199 assuming that the current directory is on the search path
1200 for executable programs.
1201
1202 @noindent
1203 and, if all has gone well, you will see
1204
1205 @smallexample
1206 Hello WORLD!
1207 @end smallexample
1208
1209 @noindent
1210 appear in response to this command.
1211
1212 @c ****************************************
1213 @node Running a Program with Multiple Units
1214 @section Running a Program with Multiple Units
1215
1216 @noindent
1217 Consider a slightly more complicated example that has three files: a
1218 main program, and the spec and body of a package:
1219
1220 @smallexample @c ada
1221 @cartouche
1222 @group
1223 package Greetings is
1224 procedure Hello;
1225 procedure Goodbye;
1226 end Greetings;
1227
1228 with Ada.Text_IO; use Ada.Text_IO;
1229 package body Greetings is
1230 procedure Hello is
1231 begin
1232 Put_Line ("Hello WORLD!");
1233 end Hello;
1234
1235 procedure Goodbye is
1236 begin
1237 Put_Line ("Goodbye WORLD!");
1238 end Goodbye;
1239 end Greetings;
1240 @end group
1241
1242 @group
1243 with Greetings;
1244 procedure Gmain is
1245 begin
1246 Greetings.Hello;
1247 Greetings.Goodbye;
1248 end Gmain;
1249 @end group
1250 @end cartouche
1251 @end smallexample
1252
1253 @noindent
1254 Following the one-unit-per-file rule, place this program in the
1255 following three separate files:
1256
1257 @table @file
1258 @item greetings.ads
1259 spec of package @code{Greetings}
1260
1261 @item greetings.adb
1262 body of package @code{Greetings}
1263
1264 @item gmain.adb
1265 body of main program
1266 @end table
1267
1268 @noindent
1269 To build an executable version of
1270 this program, we could use four separate steps to compile, bind, and link
1271 the program, as follows:
1272
1273 @smallexample
1274 $ gcc -c gmain.adb
1275 $ gcc -c greetings.adb
1276 $ gnatbind gmain
1277 $ gnatlink gmain
1278 @end smallexample
1279
1280 @noindent
1281 Note that there is no required order of compilation when using GNAT.
1282 In particular it is perfectly fine to compile the main program first.
1283 Also, it is not necessary to compile package specs in the case where
1284 there is an accompanying body; you only need to compile the body. If you want
1285 to submit these files to the compiler for semantic checking and not code
1286 generation, then use the
1287 @option{-gnatc} switch:
1288
1289 @smallexample
1290 $ gcc -c greetings.ads -gnatc
1291 @end smallexample
1292
1293 @noindent
1294 Although the compilation can be done in separate steps as in the
1295 above example, in practice it is almost always more convenient
1296 to use the @command{gnatmake} tool. All you need to know in this case
1297 is the name of the main program's source file. The effect of the above four
1298 commands can be achieved with a single one:
1299
1300 @smallexample
1301 $ gnatmake gmain.adb
1302 @end smallexample
1303
1304 @noindent
1305 In the next section we discuss the advantages of using @command{gnatmake} in
1306 more detail.
1307
1308 @c *****************************
1309 @node Using the gnatmake Utility
1310 @section Using the @command{gnatmake} Utility
1311
1312 @noindent
1313 If you work on a program by compiling single components at a time using
1314 @command{gcc}, you typically keep track of the units you modify. In order to
1315 build a consistent system, you compile not only these units, but also any
1316 units that depend on the units you have modified.
1317 For example, in the preceding case,
1318 if you edit @file{gmain.adb}, you only need to recompile that file. But if
1319 you edit @file{greetings.ads}, you must recompile both
1320 @file{greetings.adb} and @file{gmain.adb}, because both files contain
1321 units that depend on @file{greetings.ads}.
1322
1323 @code{gnatbind} will warn you if you forget one of these compilation
1324 steps, so that it is impossible to generate an inconsistent program as a
1325 result of forgetting to do a compilation. Nevertheless it is tedious and
1326 error-prone to keep track of dependencies among units.
1327 One approach to handle the dependency-bookkeeping is to use a
1328 makefile. However, makefiles present maintenance problems of their own:
1329 if the dependencies change as you change the program, you must make
1330 sure that the makefile is kept up-to-date manually, which is also an
1331 error-prone process.
1332
1333 The @command{gnatmake} utility takes care of these details automatically.
1334 Invoke it using either one of the following forms:
1335
1336 @smallexample
1337 $ gnatmake gmain.adb
1338 $ gnatmake ^gmain^GMAIN^
1339 @end smallexample
1340
1341 @noindent
1342 The argument is the name of the file containing the main program;
1343 you may omit the extension. @command{gnatmake}
1344 examines the environment, automatically recompiles any files that need
1345 recompiling, and binds and links the resulting set of object files,
1346 generating the executable file, @file{^gmain^GMAIN.EXE^}.
1347 In a large program, it
1348 can be extremely helpful to use @command{gnatmake}, because working out by hand
1349 what needs to be recompiled can be difficult.
1350
1351 Note that @command{gnatmake}
1352 takes into account all the Ada rules that
1353 establish dependencies among units. These include dependencies that result
1354 from inlining subprogram bodies, and from
1355 generic instantiation. Unlike some other
1356 Ada make tools, @command{gnatmake} does not rely on the dependencies that were
1357 found by the compiler on a previous compilation, which may possibly
1358 be wrong when sources change. @command{gnatmake} determines the exact set of
1359 dependencies from scratch each time it is run.
1360
1361 @ifset vms
1362 @node Editing with Emacs
1363 @section Editing with Emacs
1364 @cindex Emacs
1365
1366 @noindent
1367 Emacs is an extensible self-documenting text editor that is available in a
1368 separate VMSINSTAL kit.
1369
1370 Invoke Emacs by typing @kbd{Emacs} at the command prompt. To get started,
1371 click on the Emacs Help menu and run the Emacs Tutorial.
1372 In a character cell terminal, Emacs help is invoked with @kbd{Ctrl-h} (also
1373 written as @kbd{C-h}), and the tutorial by @kbd{C-h t}.
1374
1375 Documentation on Emacs and other tools is available in Emacs under the
1376 pull-down menu button: @code{Help - Info}. After selecting @code{Info},
1377 use the middle mouse button to select a topic (e.g.@: Emacs).
1378
1379 In a character cell terminal, do @kbd{C-h i} to invoke info, and then @kbd{m}
1380 (stands for menu) followed by the menu item desired, as in @kbd{m Emacs}, to
1381 get to the Emacs manual.
1382 Help on Emacs is also available by typing @kbd{HELP EMACS} at the DCL command
1383 prompt.
1384
1385 The tutorial is highly recommended in order to learn the intricacies of Emacs,
1386 which is sufficiently extensible to provide for a complete programming
1387 environment and shell for the sophisticated user.
1388 @end ifset
1389
1390 @ifclear vms
1391 @node Introduction to GPS
1392 @section Introduction to GPS
1393 @cindex GPS (GNAT Programming Studio)
1394 @cindex GNAT Programming Studio (GPS)
1395 @noindent
1396 Although the command line interface (@command{gnatmake}, etc.) alone
1397 is sufficient, a graphical Interactive Development
1398 Environment can make it easier for you to compose, navigate, and debug
1399 programs. This section describes the main features of GPS
1400 (``GNAT Programming Studio''), the GNAT graphical IDE.
1401 You will see how to use GPS to build and debug an executable, and
1402 you will also learn some of the basics of the GNAT ``project'' facility.
1403
1404 GPS enables you to do much more than is presented here;
1405 e.g., you can produce a call graph, interface to a third-party
1406 Version Control System, and inspect the generated assembly language
1407 for a program.
1408 Indeed, GPS also supports languages other than Ada.
1409 Such additional information, and an explanation of all of the GPS menu
1410 items. may be found in the on-line help, which includes
1411 a user's guide and a tutorial (these are also accessible from the GNAT
1412 startup menu).
1413
1414 @menu
1415 * Building a New Program with GPS::
1416 * Simple Debugging with GPS::
1417 @end menu
1418
1419 @node Building a New Program with GPS
1420 @subsection Building a New Program with GPS
1421 @noindent
1422 GPS invokes the GNAT compilation tools using information
1423 contained in a @emph{project} (also known as a @emph{project file}):
1424 a collection of properties such
1425 as source directories, identities of main subprograms, tool switches, etc.,
1426 and their associated values.
1427 See @ref{GNAT Project Manager} for details.
1428 In order to run GPS, you will need to either create a new project
1429 or else open an existing one.
1430
1431 This section will explain how you can use GPS to create a project,
1432 to associate Ada source files with a project, and to build and run
1433 programs.
1434
1435 @enumerate
1436 @item @emph{Creating a project}
1437
1438 Invoke GPS, either from the command line or the platform's IDE.
1439 After it starts, GPS will display a ``Welcome'' screen with three
1440 radio buttons:
1441
1442 @itemize @bullet
1443 @item
1444 @code{Start with default project in directory}
1445
1446 @item
1447 @code{Create new project with wizard}
1448
1449 @item
1450 @code{Open existing project}
1451 @end itemize
1452
1453 @noindent
1454 Select @code{Create new project with wizard} and press @code{OK}.
1455 A new window will appear. In the text box labeled with
1456 @code{Enter the name of the project to create}, type @file{sample}
1457 as the project name.
1458 In the next box, browse to choose the directory in which you
1459 would like to create the project file.
1460 After selecting an appropriate directory, press @code{Forward}.
1461
1462 A window will appear with the title
1463 @code{Version Control System Configuration}.
1464 Simply press @code{Forward}.
1465
1466 A window will appear with the title
1467 @code{Please select the source directories for this project}.
1468 The directory that you specified for the project file will be selected
1469 by default as the one to use for sources; simply press @code{Forward}.
1470
1471 A window will appear with the title
1472 @code{Please select the build directory for this project}.
1473 The directory that you specified for the project file will be selected
1474 by default for object files and executables;
1475 simply press @code{Forward}.
1476
1477 A window will appear with the title
1478 @code{Please select the main units for this project}.
1479 You will supply this information later, after creating the source file.
1480 Simply press @code{Forward} for now.
1481
1482 A window will appear with the title
1483 @code{Please select the switches to build the project}.
1484 Press @code{Apply}. This will create a project file named
1485 @file{sample.prj} in the directory that you had specified.
1486
1487 @item @emph{Creating and saving the source file}
1488
1489 After you create the new project, a GPS window will appear, which is
1490 partitioned into two main sections:
1491
1492 @itemize @bullet
1493 @item
1494 A @emph{Workspace area}, initially greyed out, which you will use for
1495 creating and editing source files
1496
1497 @item
1498 Directly below, a @emph{Messages area}, which initially displays a
1499 ``Welcome'' message.
1500 (If the Messages area is not visible, drag its border upward to expand it.)
1501 @end itemize
1502
1503 @noindent
1504 Select @code{File} on the menu bar, and then the @code{New} command.
1505 The Workspace area will become white, and you can now
1506 enter the source program explicitly.
1507 Type the following text
1508
1509 @smallexample @c ada
1510 @group
1511 with Ada.Text_IO; use Ada.Text_IO;
1512 procedure Hello is
1513 begin
1514 Put_Line("Hello from GPS!");
1515 end Hello;
1516 @end group
1517 @end smallexample
1518
1519 @noindent
1520 Select @code{File}, then @code{Save As}, and enter the source file name
1521 @file{hello.adb}.
1522 The file will be saved in the same directory you specified as the
1523 location of the default project file.
1524
1525 @item @emph{Updating the project file}
1526
1527 You need to add the new source file to the project.
1528 To do this, select
1529 the @code{Project} menu and then @code{Edit project properties}.
1530 Click the @code{Main files} tab on the left, and then the
1531 @code{Add} button.
1532 Choose @file{hello.adb} from the list, and press @code{Open}.
1533 The project settings window will reflect this action.
1534 Click @code{OK}.
1535
1536 @item @emph{Building and running the program}
1537
1538 In the main GPS window, now choose the @code{Build} menu, then @code{Make},
1539 and select @file{hello.adb}.
1540 The Messages window will display the resulting invocations of @command{gcc},
1541 @command{gnatbind}, and @command{gnatlink}
1542 (reflecting the default switch settings from the
1543 project file that you created) and then a ``successful compilation/build''
1544 message.
1545
1546 To run the program, choose the @code{Build} menu, then @code{Run}, and
1547 select @command{hello}.
1548 An @emph{Arguments Selection} window will appear.
1549 There are no command line arguments, so just click @code{OK}.
1550
1551 The Messages window will now display the program's output (the string
1552 @code{Hello from GPS}), and at the bottom of the GPS window a status
1553 update is displayed (@code{Run: hello}).
1554 Close the GPS window (or select @code{File}, then @code{Exit}) to
1555 terminate this GPS session.
1556 @end enumerate
1557
1558 @node Simple Debugging with GPS
1559 @subsection Simple Debugging with GPS
1560 @noindent
1561 This section illustrates basic debugging techniques (setting breakpoints,
1562 examining/modifying variables, single stepping).
1563
1564 @enumerate
1565 @item @emph{Opening a project}
1566
1567 Start GPS and select @code{Open existing project}; browse to
1568 specify the project file @file{sample.prj} that you had created in the
1569 earlier example.
1570
1571 @item @emph{Creating a source file}
1572
1573 Select @code{File}, then @code{New}, and type in the following program:
1574
1575 @smallexample @c ada
1576 @group
1577 with Ada.Text_IO; use Ada.Text_IO;
1578 procedure Example is
1579 Line : String (1..80);
1580 N : Natural;
1581 begin
1582 Put_Line("Type a line of text at each prompt; an empty line to exit");
1583 loop
1584 Put(": ");
1585 Get_Line (Line, N);
1586 Put_Line (Line (1..N) );
1587 exit when N=0;
1588 end loop;
1589 end Example;
1590 @end group
1591 @end smallexample
1592
1593 @noindent
1594 Select @code{File}, then @code{Save as}, and enter the file name
1595 @file{example.adb}.
1596
1597 @item @emph{Updating the project file}
1598
1599 Add @code{Example} as a new main unit for the project:
1600 @enumerate a
1601 @item
1602 Select @code{Project}, then @code{Edit Project Properties}.
1603
1604 @item
1605 Select the @code{Main files} tab, click @code{Add}, then
1606 select the file @file{example.adb} from the list, and
1607 click @code{Open}.
1608 You will see the file name appear in the list of main units
1609
1610 @item
1611 Click @code{OK}
1612 @end enumerate
1613
1614 @item @emph{Building/running the executable}
1615
1616 To build the executable
1617 select @code{Build}, then @code{Make}, and then choose @file{example.adb}.
1618
1619 Run the program to see its effect (in the Messages area).
1620 Each line that you enter is displayed; an empty line will
1621 cause the loop to exit and the program to terminate.
1622
1623 @item @emph{Debugging the program}
1624
1625 Note that the @option{-g} switches to @command{gcc} and @command{gnatlink},
1626 which are required for debugging, are on by default when you create
1627 a new project.
1628 Thus unless you intentionally remove these settings, you will be able
1629 to debug any program that you develop using GPS.
1630
1631 @enumerate a
1632 @item @emph{Initializing}
1633
1634 Select @code{Debug}, then @code{Initialize}, then @file{example}
1635
1636 @item @emph{Setting a breakpoint}
1637
1638 After performing the initialization step, you will observe a small
1639 icon to the right of each line number.
1640 This serves as a toggle for breakpoints; clicking the icon will
1641 set a breakpoint at the corresponding line (the icon will change to
1642 a red circle with an ``x''), and clicking it again
1643 will remove the breakpoint / reset the icon.
1644
1645 For purposes of this example, set a breakpoint at line 10 (the
1646 statement @code{Put_Line@ (Line@ (1..N));}
1647
1648 @item @emph{Starting program execution}
1649
1650 Select @code{Debug}, then @code{Run}. When the
1651 @code{Program Arguments} window appears, click @code{OK}.
1652 A console window will appear; enter some line of text,
1653 e.g.@: @code{abcde}, at the prompt.
1654 The program will pause execution when it gets to the
1655 breakpoint, and the corresponding line is highlighted.
1656
1657 @item @emph{Examining a variable}
1658
1659 Move the mouse over one of the occurrences of the variable @code{N}.
1660 You will see the value (5) displayed, in ``tool tip'' fashion.
1661 Right click on @code{N}, select @code{Debug}, then select @code{Display N}.
1662 You will see information about @code{N} appear in the @code{Debugger Data}
1663 pane, showing the value as 5.
1664
1665 @item @emph{Assigning a new value to a variable}
1666
1667 Right click on the @code{N} in the @code{Debugger Data} pane, and
1668 select @code{Set value of N}.
1669 When the input window appears, enter the value @code{4} and click
1670 @code{OK}.
1671 This value does not automatically appear in the @code{Debugger Data}
1672 pane; to see it, right click again on the @code{N} in the
1673 @code{Debugger Data} pane and select @code{Update value}.
1674 The new value, 4, will appear in red.
1675
1676 @item @emph{Single stepping}
1677
1678 Select @code{Debug}, then @code{Next}.
1679 This will cause the next statement to be executed, in this case the
1680 call of @code{Put_Line} with the string slice.
1681 Notice in the console window that the displayed string is simply
1682 @code{abcd} and not @code{abcde} which you had entered.
1683 This is because the upper bound of the slice is now 4 rather than 5.
1684
1685 @item @emph{Removing a breakpoint}
1686
1687 Toggle the breakpoint icon at line 10.
1688
1689 @item @emph{Resuming execution from a breakpoint}
1690
1691 Select @code{Debug}, then @code{Continue}.
1692 The program will reach the next iteration of the loop, and
1693 wait for input after displaying the prompt.
1694 This time, just hit the @kbd{Enter} key.
1695 The value of @code{N} will be 0, and the program will terminate.
1696 The console window will disappear.
1697 @end enumerate
1698 @end enumerate
1699 @end ifclear
1700
1701 @node The GNAT Compilation Model
1702 @chapter The GNAT Compilation Model
1703 @cindex GNAT compilation model
1704 @cindex Compilation model
1705
1706 @menu
1707 * Source Representation::
1708 * Foreign Language Representation::
1709 * File Naming Rules::
1710 * Using Other File Names::
1711 * Alternative File Naming Schemes::
1712 * Generating Object Files::
1713 * Source Dependencies::
1714 * The Ada Library Information Files::
1715 * Binding an Ada Program::
1716 * Mixed Language Programming::
1717 @ifclear vms
1718 * Building Mixed Ada & C++ Programs::
1719 * Comparison between GNAT and C/C++ Compilation Models::
1720 @end ifclear
1721 * Comparison between GNAT and Conventional Ada Library Models::
1722 @ifset vms
1723 * Placement of temporary files::
1724 @end ifset
1725 @end menu
1726
1727 @noindent
1728 This chapter describes the compilation model used by GNAT. Although
1729 similar to that used by other languages, such as C and C++, this model
1730 is substantially different from the traditional Ada compilation models,
1731 which are based on a library. The model is initially described without
1732 reference to the library-based model. If you have not previously used an
1733 Ada compiler, you need only read the first part of this chapter. The
1734 last section describes and discusses the differences between the GNAT
1735 model and the traditional Ada compiler models. If you have used other
1736 Ada compilers, this section will help you to understand those
1737 differences, and the advantages of the GNAT model.
1738
1739 @node Source Representation
1740 @section Source Representation
1741 @cindex Latin-1
1742
1743 @noindent
1744 Ada source programs are represented in standard text files, using
1745 Latin-1 coding. Latin-1 is an 8-bit code that includes the familiar
1746 7-bit ASCII set, plus additional characters used for
1747 representing foreign languages (@pxref{Foreign Language Representation}
1748 for support of non-USA character sets). The format effector characters
1749 are represented using their standard ASCII encodings, as follows:
1750
1751 @table @code
1752 @item VT
1753 @findex VT
1754 Vertical tab, @code{16#0B#}
1755
1756 @item HT
1757 @findex HT
1758 Horizontal tab, @code{16#09#}
1759
1760 @item CR
1761 @findex CR
1762 Carriage return, @code{16#0D#}
1763
1764 @item LF
1765 @findex LF
1766 Line feed, @code{16#0A#}
1767
1768 @item FF
1769 @findex FF
1770 Form feed, @code{16#0C#}
1771 @end table
1772
1773 @noindent
1774 Source files are in standard text file format. In addition, GNAT will
1775 recognize a wide variety of stream formats, in which the end of
1776 physical lines is marked by any of the following sequences:
1777 @code{LF}, @code{CR}, @code{CR-LF}, or @code{LF-CR}. This is useful
1778 in accommodating files that are imported from other operating systems.
1779
1780 @cindex End of source file
1781 @cindex Source file, end
1782 @findex SUB
1783 The end of a source file is normally represented by the physical end of
1784 file. However, the control character @code{16#1A#} (@code{SUB}) is also
1785 recognized as signalling the end of the source file. Again, this is
1786 provided for compatibility with other operating systems where this
1787 code is used to represent the end of file.
1788
1789 Each file contains a single Ada compilation unit, including any pragmas
1790 associated with the unit. For example, this means you must place a
1791 package declaration (a package @dfn{spec}) and the corresponding body in
1792 separate files. An Ada @dfn{compilation} (which is a sequence of
1793 compilation units) is represented using a sequence of files. Similarly,
1794 you will place each subunit or child unit in a separate file.
1795
1796 @node Foreign Language Representation
1797 @section Foreign Language Representation
1798
1799 @noindent
1800 GNAT supports the standard character sets defined in Ada as well as
1801 several other non-standard character sets for use in localized versions
1802 of the compiler (@pxref{Character Set Control}).
1803 @menu
1804 * Latin-1::
1805 * Other 8-Bit Codes::
1806 * Wide Character Encodings::
1807 @end menu
1808
1809 @node Latin-1
1810 @subsection Latin-1
1811 @cindex Latin-1
1812
1813 @noindent
1814 The basic character set is Latin-1. This character set is defined by ISO
1815 standard 8859, part 1. The lower half (character codes @code{16#00#}
1816 @dots{} @code{16#7F#)} is identical to standard ASCII coding, but the upper half
1817 is used to represent additional characters. These include extended letters
1818 used by European languages, such as French accents, the vowels with umlauts
1819 used in German, and the extra letter A-ring used in Swedish.
1820
1821 @findex Ada.Characters.Latin_1
1822 For a complete list of Latin-1 codes and their encodings, see the source
1823 file of library unit @code{Ada.Characters.Latin_1} in file
1824 @file{a-chlat1.ads}.
1825 You may use any of these extended characters freely in character or
1826 string literals. In addition, the extended characters that represent
1827 letters can be used in identifiers.
1828
1829 @node Other 8-Bit Codes
1830 @subsection Other 8-Bit Codes
1831
1832 @noindent
1833 GNAT also supports several other 8-bit coding schemes:
1834
1835 @table @asis
1836 @item ISO 8859-2 (Latin-2)
1837 @cindex Latin-2
1838 @cindex ISO 8859-2
1839 Latin-2 letters allowed in identifiers, with uppercase and lowercase
1840 equivalence.
1841
1842 @item ISO 8859-3 (Latin-3)
1843 @cindex Latin-3
1844 @cindex ISO 8859-3
1845 Latin-3 letters allowed in identifiers, with uppercase and lowercase
1846 equivalence.
1847
1848 @item ISO 8859-4 (Latin-4)
1849 @cindex Latin-4
1850 @cindex ISO 8859-4
1851 Latin-4 letters allowed in identifiers, with uppercase and lowercase
1852 equivalence.
1853
1854 @item ISO 8859-5 (Cyrillic)
1855 @cindex ISO 8859-5
1856 @cindex Cyrillic
1857 ISO 8859-5 letters (Cyrillic) allowed in identifiers, with uppercase and
1858 lowercase equivalence.
1859
1860 @item ISO 8859-15 (Latin-9)
1861 @cindex ISO 8859-15
1862 @cindex Latin-9
1863 ISO 8859-15 (Latin-9) letters allowed in identifiers, with uppercase and
1864 lowercase equivalence
1865
1866 @item IBM PC (code page 437)
1867 @cindex code page 437
1868 This code page is the normal default for PCs in the U.S. It corresponds
1869 to the original IBM PC character set. This set has some, but not all, of
1870 the extended Latin-1 letters, but these letters do not have the same
1871 encoding as Latin-1. In this mode, these letters are allowed in
1872 identifiers with uppercase and lowercase equivalence.
1873
1874 @item IBM PC (code page 850)
1875 @cindex code page 850
1876 This code page is a modification of 437 extended to include all the
1877 Latin-1 letters, but still not with the usual Latin-1 encoding. In this
1878 mode, all these letters are allowed in identifiers with uppercase and
1879 lowercase equivalence.
1880
1881 @item Full Upper 8-bit
1882 Any character in the range 80-FF allowed in identifiers, and all are
1883 considered distinct. In other words, there are no uppercase and lowercase
1884 equivalences in this range. This is useful in conjunction with
1885 certain encoding schemes used for some foreign character sets (e.g.,
1886 the typical method of representing Chinese characters on the PC).
1887
1888 @item No Upper-Half
1889 No upper-half characters in the range 80-FF are allowed in identifiers.
1890 This gives Ada 83 compatibility for identifier names.
1891 @end table
1892
1893 @noindent
1894 For precise data on the encodings permitted, and the uppercase and lowercase
1895 equivalences that are recognized, see the file @file{csets.adb} in
1896 the GNAT compiler sources. You will need to obtain a full source release
1897 of GNAT to obtain this file.
1898
1899 @node Wide Character Encodings
1900 @subsection Wide Character Encodings
1901
1902 @noindent
1903 GNAT allows wide character codes to appear in character and string
1904 literals, and also optionally in identifiers, by means of the following
1905 possible encoding schemes:
1906
1907 @table @asis
1908
1909 @item Hex Coding
1910 In this encoding, a wide character is represented by the following five
1911 character sequence:
1912
1913 @smallexample
1914 ESC a b c d
1915 @end smallexample
1916
1917 @noindent
1918 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1919 characters (using uppercase letters) of the wide character code. For
1920 example, ESC A345 is used to represent the wide character with code
1921 @code{16#A345#}.
1922 This scheme is compatible with use of the full Wide_Character set.
1923
1924 @item Upper-Half Coding
1925 @cindex Upper-Half Coding
1926 The wide character with encoding @code{16#abcd#} where the upper bit is on
1927 (in other words, ``a'' is in the range 8-F) is represented as two bytes,
1928 @code{16#ab#} and @code{16#cd#}. The second byte cannot be a format control
1929 character, but is not required to be in the upper half. This method can
1930 be also used for shift-JIS or EUC, where the internal coding matches the
1931 external coding.
1932
1933 @item Shift JIS Coding
1934 @cindex Shift JIS Coding
1935 A wide character is represented by a two-character sequence,
1936 @code{16#ab#} and
1937 @code{16#cd#}, with the restrictions described for upper-half encoding as
1938 described above. The internal character code is the corresponding JIS
1939 character according to the standard algorithm for Shift-JIS
1940 conversion. Only characters defined in the JIS code set table can be
1941 used with this encoding method.
1942
1943 @item EUC Coding
1944 @cindex EUC Coding
1945 A wide character is represented by a two-character sequence
1946 @code{16#ab#} and
1947 @code{16#cd#}, with both characters being in the upper half. The internal
1948 character code is the corresponding JIS character according to the EUC
1949 encoding algorithm. Only characters defined in the JIS code set table
1950 can be used with this encoding method.
1951
1952 @item UTF-8 Coding
1953 A wide character is represented using
1954 UCS Transformation Format 8 (UTF-8) as defined in Annex R of ISO
1955 10646-1/Am.2. Depending on the character value, the representation
1956 is a one, two, or three byte sequence:
1957 @smallexample
1958 @iftex
1959 @leftskip=.7cm
1960 @end iftex
1961 16#0000#-16#007f#: 2#0@var{xxxxxxx}#
1962 16#0080#-16#07ff#: 2#110@var{xxxxx}# 2#10@var{xxxxxx}#
1963 16#0800#-16#ffff#: 2#1110@var{xxxx}# 2#10@var{xxxxxx}# 2#10@var{xxxxxx}#
1964
1965 @end smallexample
1966
1967 @noindent
1968 where the @var{xxx} bits correspond to the left-padded bits of the
1969 16-bit character value. Note that all lower half ASCII characters
1970 are represented as ASCII bytes and all upper half characters and
1971 other wide characters are represented as sequences of upper-half
1972 (The full UTF-8 scheme allows for encoding 31-bit characters as
1973 6-byte sequences, but in this implementation, all UTF-8 sequences
1974 of four or more bytes length will be treated as illegal).
1975 @item Brackets Coding
1976 In this encoding, a wide character is represented by the following eight
1977 character sequence:
1978
1979 @smallexample
1980 [ " a b c d " ]
1981 @end smallexample
1982
1983 @noindent
1984 Where @code{a}, @code{b}, @code{c}, @code{d} are the four hexadecimal
1985 characters (using uppercase letters) of the wide character code. For
1986 example, [``A345''] is used to represent the wide character with code
1987 @code{16#A345#}. It is also possible (though not required) to use the
1988 Brackets coding for upper half characters. For example, the code
1989 @code{16#A3#} can be represented as @code{[``A3'']}.
1990
1991 This scheme is compatible with use of the full Wide_Character set,
1992 and is also the method used for wide character encoding in the standard
1993 ACVC (Ada Compiler Validation Capability) test suite distributions.
1994
1995 @end table
1996
1997 @noindent
1998 Note: Some of these coding schemes do not permit the full use of the
1999 Ada character set. For example, neither Shift JIS, nor EUC allow the
2000 use of the upper half of the Latin-1 set.
2001
2002 @node File Naming Rules
2003 @section File Naming Rules
2004
2005 @noindent
2006 The default file name is determined by the name of the unit that the
2007 file contains. The name is formed by taking the full expanded name of
2008 the unit and replacing the separating dots with hyphens and using
2009 ^lowercase^uppercase^ for all letters.
2010
2011 An exception arises if the file name generated by the above rules starts
2012 with one of the characters
2013 @ifset vms
2014 @samp{A}, @samp{G}, @samp{I}, or @samp{S},
2015 @end ifset
2016 @ifclear vms
2017 @samp{a}, @samp{g}, @samp{i}, or @samp{s},
2018 @end ifclear
2019 and the second character is a
2020 minus. In this case, the character ^tilde^dollar sign^ is used in place
2021 of the minus. The reason for this special rule is to avoid clashes with
2022 the standard names for child units of the packages System, Ada,
2023 Interfaces, and GNAT, which use the prefixes
2024 @ifset vms
2025 @samp{S-}, @samp{A-}, @samp{I-}, and @samp{G-},
2026 @end ifset
2027 @ifclear vms
2028 @samp{s-}, @samp{a-}, @samp{i-}, and @samp{g-},
2029 @end ifclear
2030 respectively.
2031
2032 The file extension is @file{.ads} for a spec and
2033 @file{.adb} for a body. The following list shows some
2034 examples of these rules.
2035
2036 @table @file
2037 @item main.ads
2038 Main (spec)
2039 @item main.adb
2040 Main (body)
2041 @item arith_functions.ads
2042 Arith_Functions (package spec)
2043 @item arith_functions.adb
2044 Arith_Functions (package body)
2045 @item func-spec.ads
2046 Func.Spec (child package spec)
2047 @item func-spec.adb
2048 Func.Spec (child package body)
2049 @item main-sub.adb
2050 Sub (subunit of Main)
2051 @item ^a~bad.adb^A$BAD.ADB^
2052 A.Bad (child package body)
2053 @end table
2054
2055 @noindent
2056 Following these rules can result in excessively long
2057 file names if corresponding
2058 unit names are long (for example, if child units or subunits are
2059 heavily nested). An option is available to shorten such long file names
2060 (called file name ``krunching''). This may be particularly useful when
2061 programs being developed with GNAT are to be used on operating systems
2062 with limited file name lengths. @xref{Using gnatkr}.
2063
2064 Of course, no file shortening algorithm can guarantee uniqueness over
2065 all possible unit names; if file name krunching is used, it is your
2066 responsibility to ensure no name clashes occur. Alternatively you
2067 can specify the exact file names that you want used, as described
2068 in the next section. Finally, if your Ada programs are migrating from a
2069 compiler with a different naming convention, you can use the gnatchop
2070 utility to produce source files that follow the GNAT naming conventions.
2071 (For details @pxref{Renaming Files Using gnatchop}.)
2072
2073 Note: in the case of @code{Windows NT/XP} or @code{OpenVMS} operating
2074 systems, case is not significant. So for example on @code{Windows XP}
2075 if the canonical name is @code{main-sub.adb}, you can use the file name
2076 @code{Main-Sub.adb} instead. However, case is significant for other
2077 operating systems, so for example, if you want to use other than
2078 canonically cased file names on a Unix system, you need to follow
2079 the procedures described in the next section.
2080
2081 @node Using Other File Names
2082 @section Using Other File Names
2083 @cindex File names
2084
2085 @noindent
2086 In the previous section, we have described the default rules used by
2087 GNAT to determine the file name in which a given unit resides. It is
2088 often convenient to follow these default rules, and if you follow them,
2089 the compiler knows without being explicitly told where to find all
2090 the files it needs.
2091
2092 However, in some cases, particularly when a program is imported from
2093 another Ada compiler environment, it may be more convenient for the
2094 programmer to specify which file names contain which units. GNAT allows
2095 arbitrary file names to be used by means of the Source_File_Name pragma.
2096 The form of this pragma is as shown in the following examples:
2097 @cindex Source_File_Name pragma
2098
2099 @smallexample @c ada
2100 @cartouche
2101 pragma Source_File_Name (My_Utilities.Stacks,
2102 Spec_File_Name => "myutilst_a.ada");
2103 pragma Source_File_name (My_Utilities.Stacks,
2104 Body_File_Name => "myutilst.ada");
2105 @end cartouche
2106 @end smallexample
2107
2108 @noindent
2109 As shown in this example, the first argument for the pragma is the unit
2110 name (in this example a child unit). The second argument has the form
2111 of a named association. The identifier
2112 indicates whether the file name is for a spec or a body;
2113 the file name itself is given by a string literal.
2114
2115 The source file name pragma is a configuration pragma, which means that
2116 normally it will be placed in the @file{gnat.adc}
2117 file used to hold configuration
2118 pragmas that apply to a complete compilation environment.
2119 For more details on how the @file{gnat.adc} file is created and used
2120 see @ref{Handling of Configuration Pragmas}.
2121 @cindex @file{gnat.adc}
2122
2123 @ifclear vms
2124 GNAT allows completely arbitrary file names to be specified using the
2125 source file name pragma. However, if the file name specified has an
2126 extension other than @file{.ads} or @file{.adb} it is necessary to use
2127 a special syntax when compiling the file. The name in this case must be
2128 preceded by the special sequence @option{-x} followed by a space and the name
2129 of the language, here @code{ada}, as in:
2130
2131 @smallexample
2132 $ gcc -c -x ada peculiar_file_name.sim
2133 @end smallexample
2134 @end ifclear
2135
2136 @noindent
2137 @command{gnatmake} handles non-standard file names in the usual manner (the
2138 non-standard file name for the main program is simply used as the
2139 argument to gnatmake). Note that if the extension is also non-standard,
2140 then it must be included in the @command{gnatmake} command, it may not
2141 be omitted.
2142
2143 @node Alternative File Naming Schemes
2144 @section Alternative File Naming Schemes
2145 @cindex File naming schemes, alternative
2146 @cindex File names
2147
2148 In the previous section, we described the use of the @code{Source_File_Name}
2149 pragma to allow arbitrary names to be assigned to individual source files.
2150 However, this approach requires one pragma for each file, and especially in
2151 large systems can result in very long @file{gnat.adc} files, and also create
2152 a maintenance problem.
2153
2154 GNAT also provides a facility for specifying systematic file naming schemes
2155 other than the standard default naming scheme previously described. An
2156 alternative scheme for naming is specified by the use of
2157 @code{Source_File_Name} pragmas having the following format:
2158 @cindex Source_File_Name pragma
2159
2160 @smallexample @c ada
2161 pragma Source_File_Name (
2162 Spec_File_Name => FILE_NAME_PATTERN
2163 @r{[},Casing => CASING_SPEC@r{]}
2164 @r{[},Dot_Replacement => STRING_LITERAL@r{]});
2165
2166 pragma Source_File_Name (
2167 Body_File_Name => FILE_NAME_PATTERN
2168 @r{[},Casing => CASING_SPEC@r{]}
2169 @r{[},Dot_Replacement => STRING_LITERAL@r{]});
2170
2171 pragma Source_File_Name (
2172 Subunit_File_Name => FILE_NAME_PATTERN
2173 @r{[},Casing => CASING_SPEC@r{]}
2174 @r{[},Dot_Replacement => STRING_LITERAL@r{]});
2175
2176 FILE_NAME_PATTERN ::= STRING_LITERAL
2177 CASING_SPEC ::= Lowercase | Uppercase | Mixedcase
2178 @end smallexample
2179
2180 @noindent
2181 The @code{FILE_NAME_PATTERN} string shows how the file name is constructed.
2182 It contains a single asterisk character, and the unit name is substituted
2183 systematically for this asterisk. The optional parameter
2184 @code{Casing} indicates
2185 whether the unit name is to be all upper-case letters, all lower-case letters,
2186 or mixed-case. If no
2187 @code{Casing} parameter is used, then the default is all
2188 ^lower-case^upper-case^.
2189
2190 The optional @code{Dot_Replacement} string is used to replace any periods
2191 that occur in subunit or child unit names. If no @code{Dot_Replacement}
2192 argument is used then separating dots appear unchanged in the resulting
2193 file name.
2194 Although the above syntax indicates that the
2195 @code{Casing} argument must appear
2196 before the @code{Dot_Replacement} argument, but it
2197 is also permissible to write these arguments in the opposite order.
2198
2199 As indicated, it is possible to specify different naming schemes for
2200 bodies, specs, and subunits. Quite often the rule for subunits is the
2201 same as the rule for bodies, in which case, there is no need to give
2202 a separate @code{Subunit_File_Name} rule, and in this case the
2203 @code{Body_File_name} rule is used for subunits as well.
2204
2205 The separate rule for subunits can also be used to implement the rather
2206 unusual case of a compilation environment (e.g.@: a single directory) which
2207 contains a subunit and a child unit with the same unit name. Although
2208 both units cannot appear in the same partition, the Ada Reference Manual
2209 allows (but does not require) the possibility of the two units coexisting
2210 in the same environment.
2211
2212 The file name translation works in the following steps:
2213
2214 @itemize @bullet
2215
2216 @item
2217 If there is a specific @code{Source_File_Name} pragma for the given unit,
2218 then this is always used, and any general pattern rules are ignored.
2219
2220 @item
2221 If there is a pattern type @code{Source_File_Name} pragma that applies to
2222 the unit, then the resulting file name will be used if the file exists. If
2223 more than one pattern matches, the latest one will be tried first, and the
2224 first attempt resulting in a reference to a file that exists will be used.
2225
2226 @item
2227 If no pattern type @code{Source_File_Name} pragma that applies to the unit
2228 for which the corresponding file exists, then the standard GNAT default
2229 naming rules are used.
2230
2231 @end itemize
2232
2233 @noindent
2234 As an example of the use of this mechanism, consider a commonly used scheme
2235 in which file names are all lower case, with separating periods copied
2236 unchanged to the resulting file name, and specs end with @file{.1.ada}, and
2237 bodies end with @file{.2.ada}. GNAT will follow this scheme if the following
2238 two pragmas appear:
2239
2240 @smallexample @c ada
2241 pragma Source_File_Name
2242 (Spec_File_Name => "*.1.ada");
2243 pragma Source_File_Name
2244 (Body_File_Name => "*.2.ada");
2245 @end smallexample
2246
2247 @noindent
2248 The default GNAT scheme is actually implemented by providing the following
2249 default pragmas internally:
2250
2251 @smallexample @c ada
2252 pragma Source_File_Name
2253 (Spec_File_Name => "*.ads", Dot_Replacement => "-");
2254 pragma Source_File_Name
2255 (Body_File_Name => "*.adb", Dot_Replacement => "-");
2256 @end smallexample
2257
2258 @noindent
2259 Our final example implements a scheme typically used with one of the
2260 Ada 83 compilers, where the separator character for subunits was ``__''
2261 (two underscores), specs were identified by adding @file{_.ADA}, bodies
2262 by adding @file{.ADA}, and subunits by
2263 adding @file{.SEP}. All file names were
2264 upper case. Child units were not present of course since this was an
2265 Ada 83 compiler, but it seems reasonable to extend this scheme to use
2266 the same double underscore separator for child units.
2267
2268 @smallexample @c ada
2269 pragma Source_File_Name
2270 (Spec_File_Name => "*_.ADA",
2271 Dot_Replacement => "__",
2272 Casing = Uppercase);
2273 pragma Source_File_Name
2274 (Body_File_Name => "*.ADA",
2275 Dot_Replacement => "__",
2276 Casing = Uppercase);
2277 pragma Source_File_Name
2278 (Subunit_File_Name => "*.SEP",
2279 Dot_Replacement => "__",
2280 Casing = Uppercase);
2281 @end smallexample
2282
2283 @node Generating Object Files
2284 @section Generating Object Files
2285
2286 @noindent
2287 An Ada program consists of a set of source files, and the first step in
2288 compiling the program is to generate the corresponding object files.
2289 These are generated by compiling a subset of these source files.
2290 The files you need to compile are the following:
2291
2292 @itemize @bullet
2293 @item
2294 If a package spec has no body, compile the package spec to produce the
2295 object file for the package.
2296
2297 @item
2298 If a package has both a spec and a body, compile the body to produce the
2299 object file for the package. The source file for the package spec need
2300 not be compiled in this case because there is only one object file, which
2301 contains the code for both the spec and body of the package.
2302
2303 @item
2304 For a subprogram, compile the subprogram body to produce the object file
2305 for the subprogram. The spec, if one is present, is as usual in a
2306 separate file, and need not be compiled.
2307
2308 @item
2309 @cindex Subunits
2310 In the case of subunits, only compile the parent unit. A single object
2311 file is generated for the entire subunit tree, which includes all the
2312 subunits.
2313
2314 @item
2315 Compile child units independently of their parent units
2316 (though, of course, the spec of all the ancestor unit must be present in order
2317 to compile a child unit).
2318
2319 @item
2320 @cindex Generics
2321 Compile generic units in the same manner as any other units. The object
2322 files in this case are small dummy files that contain at most the
2323 flag used for elaboration checking. This is because GNAT always handles generic
2324 instantiation by means of macro expansion. However, it is still necessary to
2325 compile generic units, for dependency checking and elaboration purposes.
2326 @end itemize
2327
2328 @noindent
2329 The preceding rules describe the set of files that must be compiled to
2330 generate the object files for a program. Each object file has the same
2331 name as the corresponding source file, except that the extension is
2332 @file{.o} as usual.
2333
2334 You may wish to compile other files for the purpose of checking their
2335 syntactic and semantic correctness. For example, in the case where a
2336 package has a separate spec and body, you would not normally compile the
2337 spec. However, it is convenient in practice to compile the spec to make
2338 sure it is error-free before compiling clients of this spec, because such
2339 compilations will fail if there is an error in the spec.
2340
2341 GNAT provides an option for compiling such files purely for the
2342 purposes of checking correctness; such compilations are not required as
2343 part of the process of building a program. To compile a file in this
2344 checking mode, use the @option{-gnatc} switch.
2345
2346 @node Source Dependencies
2347 @section Source Dependencies
2348
2349 @noindent
2350 A given object file clearly depends on the source file which is compiled
2351 to produce it. Here we are using @dfn{depends} in the sense of a typical
2352 @code{make} utility; in other words, an object file depends on a source
2353 file if changes to the source file require the object file to be
2354 recompiled.
2355 In addition to this basic dependency, a given object may depend on
2356 additional source files as follows:
2357
2358 @itemize @bullet
2359 @item
2360 If a file being compiled @code{with}'s a unit @var{X}, the object file
2361 depends on the file containing the spec of unit @var{X}. This includes
2362 files that are @code{with}'ed implicitly either because they are parents
2363 of @code{with}'ed child units or they are run-time units required by the
2364 language constructs used in a particular unit.
2365
2366 @item
2367 If a file being compiled instantiates a library level generic unit, the
2368 object file depends on both the spec and body files for this generic
2369 unit.
2370
2371 @item
2372 If a file being compiled instantiates a generic unit defined within a
2373 package, the object file depends on the body file for the package as
2374 well as the spec file.
2375
2376 @item
2377 @findex Inline
2378 @cindex @option{-gnatn} switch
2379 If a file being compiled contains a call to a subprogram for which
2380 pragma @code{Inline} applies and inlining is activated with the
2381 @option{-gnatn} switch, the object file depends on the file containing the
2382 body of this subprogram as well as on the file containing the spec. Note
2383 that for inlining to actually occur as a result of the use of this switch,
2384 it is necessary to compile in optimizing mode.
2385
2386 @cindex @option{-gnatN} switch
2387 The use of @option{-gnatN} activates inlining optimization
2388 that is performed by the front end of the compiler. This inlining does
2389 not require that the code generation be optimized. Like @option{-gnatn},
2390 the use of this switch generates additional dependencies.
2391
2392 When using a gcc-based back end (in practice this means using any version
2393 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
2394 @option{-gnatN} is deprecated, and the use of @option{-gnatn} is preferred.
2395 Historically front end inlining was more extensive than the gcc back end
2396 inlining, but that is no longer the case.
2397
2398 @item
2399 If an object file @file{O} depends on the proper body of a subunit through
2400 inlining or instantiation, it depends on the parent unit of the subunit.
2401 This means that any modification of the parent unit or one of its subunits
2402 affects the compilation of @file{O}.
2403
2404 @item
2405 The object file for a parent unit depends on all its subunit body files.
2406
2407 @item
2408 The previous two rules meant that for purposes of computing dependencies and
2409 recompilation, a body and all its subunits are treated as an indivisible whole.
2410
2411 @noindent
2412 These rules are applied transitively: if unit @code{A} @code{with}'s
2413 unit @code{B}, whose elaboration calls an inlined procedure in package
2414 @code{C}, the object file for unit @code{A} will depend on the body of
2415 @code{C}, in file @file{c.adb}.
2416
2417 The set of dependent files described by these rules includes all the
2418 files on which the unit is semantically dependent, as dictated by the
2419 Ada language standard. However, it is a superset of what the
2420 standard describes, because it includes generic, inline, and subunit
2421 dependencies.
2422
2423 An object file must be recreated by recompiling the corresponding source
2424 file if any of the source files on which it depends are modified. For
2425 example, if the @code{make} utility is used to control compilation,
2426 the rule for an Ada object file must mention all the source files on
2427 which the object file depends, according to the above definition.
2428 The determination of the necessary
2429 recompilations is done automatically when one uses @command{gnatmake}.
2430 @end itemize
2431
2432 @node The Ada Library Information Files
2433 @section The Ada Library Information Files
2434 @cindex Ada Library Information files
2435 @cindex @file{ALI} files
2436
2437 @noindent
2438 Each compilation actually generates two output files. The first of these
2439 is the normal object file that has a @file{.o} extension. The second is a
2440 text file containing full dependency information. It has the same
2441 name as the source file, but an @file{.ali} extension.
2442 This file is known as the Ada Library Information (@file{ALI}) file.
2443 The following information is contained in the @file{ALI} file.
2444
2445 @itemize @bullet
2446 @item
2447 Version information (indicates which version of GNAT was used to compile
2448 the unit(s) in question)
2449
2450 @item
2451 Main program information (including priority and time slice settings,
2452 as well as the wide character encoding used during compilation).
2453
2454 @item
2455 List of arguments used in the @command{gcc} command for the compilation
2456
2457 @item
2458 Attributes of the unit, including configuration pragmas used, an indication
2459 of whether the compilation was successful, exception model used etc.
2460
2461 @item
2462 A list of relevant restrictions applying to the unit (used for consistency)
2463 checking.
2464
2465 @item
2466 Categorization information (e.g.@: use of pragma @code{Pure}).
2467
2468 @item
2469 Information on all @code{with}'ed units, including presence of
2470 @code{Elaborate} or @code{Elaborate_All} pragmas.
2471
2472 @item
2473 Information from any @code{Linker_Options} pragmas used in the unit
2474
2475 @item
2476 Information on the use of @code{Body_Version} or @code{Version}
2477 attributes in the unit.
2478
2479 @item
2480 Dependency information. This is a list of files, together with
2481 time stamp and checksum information. These are files on which
2482 the unit depends in the sense that recompilation is required
2483 if any of these units are modified.
2484
2485 @item
2486 Cross-reference data. Contains information on all entities referenced
2487 in the unit. Used by tools like @code{gnatxref} and @code{gnatfind} to
2488 provide cross-reference information.
2489
2490 @end itemize
2491
2492 @noindent
2493 For a full detailed description of the format of the @file{ALI} file,
2494 see the source of the body of unit @code{Lib.Writ}, contained in file
2495 @file{lib-writ.adb} in the GNAT compiler sources.
2496
2497 @node Binding an Ada Program
2498 @section Binding an Ada Program
2499
2500 @noindent
2501 When using languages such as C and C++, once the source files have been
2502 compiled the only remaining step in building an executable program
2503 is linking the object modules together. This means that it is possible to
2504 link an inconsistent version of a program, in which two units have
2505 included different versions of the same header.
2506
2507 The rules of Ada do not permit such an inconsistent program to be built.
2508 For example, if two clients have different versions of the same package,
2509 it is illegal to build a program containing these two clients.
2510 These rules are enforced by the GNAT binder, which also determines an
2511 elaboration order consistent with the Ada rules.
2512
2513 The GNAT binder is run after all the object files for a program have
2514 been created. It is given the name of the main program unit, and from
2515 this it determines the set of units required by the program, by reading the
2516 corresponding ALI files. It generates error messages if the program is
2517 inconsistent or if no valid order of elaboration exists.
2518
2519 If no errors are detected, the binder produces a main program, in Ada by
2520 default, that contains calls to the elaboration procedures of those
2521 compilation unit that require them, followed by
2522 a call to the main program. This Ada program is compiled to generate the
2523 object file for the main program. The name of
2524 the Ada file is @file{b~@var{xxx}.adb} (with the corresponding spec
2525 @file{b~@var{xxx}.ads}) where @var{xxx} is the name of the
2526 main program unit.
2527
2528 Finally, the linker is used to build the resulting executable program,
2529 using the object from the main program from the bind step as well as the
2530 object files for the Ada units of the program.
2531
2532 @node Mixed Language Programming
2533 @section Mixed Language Programming
2534 @cindex Mixed Language Programming
2535
2536 @noindent
2537 This section describes how to develop a mixed-language program,
2538 specifically one that comprises units in both Ada and C.
2539
2540 @menu
2541 * Interfacing to C::
2542 * Calling Conventions::
2543 @end menu
2544
2545 @node Interfacing to C
2546 @subsection Interfacing to C
2547 @noindent
2548 Interfacing Ada with a foreign language such as C involves using
2549 compiler directives to import and/or export entity definitions in each
2550 language---using @code{extern} statements in C, for instance, and the
2551 @code{Import}, @code{Export}, and @code{Convention} pragmas in Ada.
2552 A full treatment of these topics is provided in Appendix B, section 1
2553 of the Ada Reference Manual.
2554
2555 There are two ways to build a program using GNAT that contains some Ada
2556 sources and some foreign language sources, depending on whether or not
2557 the main subprogram is written in Ada. Here is a source example with
2558 the main subprogram in Ada:
2559
2560 @smallexample
2561 /* file1.c */
2562 #include <stdio.h>
2563
2564 void print_num (int num)
2565 @{
2566 printf ("num is %d.\n", num);
2567 return;
2568 @}
2569
2570 /* file2.c */
2571
2572 /* num_from_Ada is declared in my_main.adb */
2573 extern int num_from_Ada;
2574
2575 int get_num (void)
2576 @{
2577 return num_from_Ada;
2578 @}
2579 @end smallexample
2580
2581 @smallexample @c ada
2582 -- my_main.adb
2583 procedure My_Main is
2584
2585 -- Declare then export an Integer entity called num_from_Ada
2586 My_Num : Integer := 10;
2587 pragma Export (C, My_Num, "num_from_Ada");
2588
2589 -- Declare an Ada function spec for Get_Num, then use
2590 -- C function get_num for the implementation.
2591 function Get_Num return Integer;
2592 pragma Import (C, Get_Num, "get_num");
2593
2594 -- Declare an Ada procedure spec for Print_Num, then use
2595 -- C function print_num for the implementation.
2596 procedure Print_Num (Num : Integer);
2597 pragma Import (C, Print_Num, "print_num");
2598
2599 begin
2600 Print_Num (Get_Num);
2601 end My_Main;
2602 @end smallexample
2603
2604 @enumerate
2605 @item
2606 To build this example, first compile the foreign language files to
2607 generate object files:
2608 @smallexample
2609 ^gcc -c file1.c^gcc -c FILE1.C^
2610 ^gcc -c file2.c^gcc -c FILE2.C^
2611 @end smallexample
2612
2613 @item
2614 Then, compile the Ada units to produce a set of object files and ALI
2615 files:
2616 @smallexample
2617 gnatmake ^-c^/ACTIONS=COMPILE^ my_main.adb
2618 @end smallexample
2619
2620 @item
2621 Run the Ada binder on the Ada main program:
2622 @smallexample
2623 gnatbind my_main.ali
2624 @end smallexample
2625
2626 @item
2627 Link the Ada main program, the Ada objects and the other language
2628 objects:
2629 @smallexample
2630 gnatlink my_main.ali file1.o file2.o
2631 @end smallexample
2632 @end enumerate
2633
2634 The last three steps can be grouped in a single command:
2635 @smallexample
2636 gnatmake my_main.adb -largs file1.o file2.o
2637 @end smallexample
2638
2639 @cindex Binder output file
2640 @noindent
2641 If the main program is in a language other than Ada, then you may have
2642 more than one entry point into the Ada subsystem. You must use a special
2643 binder option to generate callable routines that initialize and
2644 finalize the Ada units (@pxref{Binding with Non-Ada Main Programs}).
2645 Calls to the initialization and finalization routines must be inserted
2646 in the main program, or some other appropriate point in the code. The
2647 call to initialize the Ada units must occur before the first Ada
2648 subprogram is called, and the call to finalize the Ada units must occur
2649 after the last Ada subprogram returns. The binder will place the
2650 initialization and finalization subprograms into the
2651 @file{b~@var{xxx}.adb} file where they can be accessed by your C
2652 sources. To illustrate, we have the following example:
2653
2654 @smallexample
2655 /* main.c */
2656 extern void adainit (void);
2657 extern void adafinal (void);
2658 extern int add (int, int);
2659 extern int sub (int, int);
2660
2661 int main (int argc, char *argv[])
2662 @{
2663 int a = 21, b = 7;
2664
2665 adainit();
2666
2667 /* Should print "21 + 7 = 28" */
2668 printf ("%d + %d = %d\n", a, b, add (a, b));
2669 /* Should print "21 - 7 = 14" */
2670 printf ("%d - %d = %d\n", a, b, sub (a, b));
2671
2672 adafinal();
2673 @}
2674 @end smallexample
2675
2676 @smallexample @c ada
2677 -- unit1.ads
2678 package Unit1 is
2679 function Add (A, B : Integer) return Integer;
2680 pragma Export (C, Add, "add");
2681 end Unit1;
2682
2683 -- unit1.adb
2684 package body Unit1 is
2685 function Add (A, B : Integer) return Integer is
2686 begin
2687 return A + B;
2688 end Add;
2689 end Unit1;
2690
2691 -- unit2.ads
2692 package Unit2 is
2693 function Sub (A, B : Integer) return Integer;
2694 pragma Export (C, Sub, "sub");
2695 end Unit2;
2696
2697 -- unit2.adb
2698 package body Unit2 is
2699 function Sub (A, B : Integer) return Integer is
2700 begin
2701 return A - B;
2702 end Sub;
2703 end Unit2;
2704 @end smallexample
2705
2706 @enumerate
2707 @item
2708 The build procedure for this application is similar to the last
2709 example's. First, compile the foreign language files to generate object
2710 files:
2711 @smallexample
2712 ^gcc -c main.c^gcc -c main.c^
2713 @end smallexample
2714
2715 @item
2716 Next, compile the Ada units to produce a set of object files and ALI
2717 files:
2718 @smallexample
2719 gnatmake ^-c^/ACTIONS=COMPILE^ unit1.adb
2720 gnatmake ^-c^/ACTIONS=COMPILE^ unit2.adb
2721 @end smallexample
2722
2723 @item
2724 Run the Ada binder on every generated ALI file. Make sure to use the
2725 @option{-n} option to specify a foreign main program:
2726 @smallexample
2727 gnatbind ^-n^/NOMAIN^ unit1.ali unit2.ali
2728 @end smallexample
2729
2730 @item
2731 Link the Ada main program, the Ada objects and the foreign language
2732 objects. You need only list the last ALI file here:
2733 @smallexample
2734 gnatlink unit2.ali main.o -o exec_file
2735 @end smallexample
2736
2737 This procedure yields a binary executable called @file{exec_file}.
2738 @end enumerate
2739
2740 @noindent
2741 Depending on the circumstances (for example when your non-Ada main object
2742 does not provide symbol @code{main}), you may also need to instruct the
2743 GNAT linker not to include the standard startup objects by passing the
2744 @option{^-nostartfiles^/NOSTART_FILES^} switch to @command{gnatlink}.
2745
2746 @node Calling Conventions
2747 @subsection Calling Conventions
2748 @cindex Foreign Languages
2749 @cindex Calling Conventions
2750 GNAT follows standard calling sequence conventions and will thus interface
2751 to any other language that also follows these conventions. The following
2752 Convention identifiers are recognized by GNAT:
2753
2754 @table @code
2755 @cindex Interfacing to Ada
2756 @cindex Other Ada compilers
2757 @cindex Convention Ada
2758 @item Ada
2759 This indicates that the standard Ada calling sequence will be
2760 used and all Ada data items may be passed without any limitations in the
2761 case where GNAT is used to generate both the caller and callee. It is also
2762 possible to mix GNAT generated code and code generated by another Ada
2763 compiler. In this case, the data types should be restricted to simple
2764 cases, including primitive types. Whether complex data types can be passed
2765 depends on the situation. Probably it is safe to pass simple arrays, such
2766 as arrays of integers or floats. Records may or may not work, depending
2767 on whether both compilers lay them out identically. Complex structures
2768 involving variant records, access parameters, tasks, or protected types,
2769 are unlikely to be able to be passed.
2770
2771 Note that in the case of GNAT running
2772 on a platform that supports HP Ada 83, a higher degree of compatibility
2773 can be guaranteed, and in particular records are layed out in an identical
2774 manner in the two compilers. Note also that if output from two different
2775 compilers is mixed, the program is responsible for dealing with elaboration
2776 issues. Probably the safest approach is to write the main program in the
2777 version of Ada other than GNAT, so that it takes care of its own elaboration
2778 requirements, and then call the GNAT-generated adainit procedure to ensure
2779 elaboration of the GNAT components. Consult the documentation of the other
2780 Ada compiler for further details on elaboration.
2781
2782 However, it is not possible to mix the tasking run time of GNAT and
2783 HP Ada 83, All the tasking operations must either be entirely within
2784 GNAT compiled sections of the program, or entirely within HP Ada 83
2785 compiled sections of the program.
2786
2787 @cindex Interfacing to Assembly
2788 @cindex Convention Assembler
2789 @item Assembler
2790 Specifies assembler as the convention. In practice this has the
2791 same effect as convention Ada (but is not equivalent in the sense of being
2792 considered the same convention).
2793
2794 @cindex Convention Asm
2795 @findex Asm
2796 @item Asm
2797 Equivalent to Assembler.
2798
2799 @cindex Interfacing to COBOL
2800 @cindex Convention COBOL
2801 @findex COBOL
2802 @item COBOL
2803 Data will be passed according to the conventions described
2804 in section B.4 of the Ada Reference Manual.
2805
2806 @findex C
2807 @cindex Interfacing to C
2808 @cindex Convention C
2809 @item C
2810 Data will be passed according to the conventions described
2811 in section B.3 of the Ada Reference Manual.
2812
2813 A note on interfacing to a C ``varargs'' function:
2814 @findex C varargs function
2815 @cindex Interfacing to C varargs function
2816 @cindex varargs function interfaces
2817
2818 @itemize @bullet
2819 @item
2820 In C, @code{varargs} allows a function to take a variable number of
2821 arguments. There is no direct equivalent in this to Ada. One
2822 approach that can be used is to create a C wrapper for each
2823 different profile and then interface to this C wrapper. For
2824 example, to print an @code{int} value using @code{printf},
2825 create a C function @code{printfi} that takes two arguments, a
2826 pointer to a string and an int, and calls @code{printf}.
2827 Then in the Ada program, use pragma @code{Import} to
2828 interface to @code{printfi}.
2829
2830 @item
2831 It may work on some platforms to directly interface to
2832 a @code{varargs} function by providing a specific Ada profile
2833 for a particular call. However, this does not work on
2834 all platforms, since there is no guarantee that the
2835 calling sequence for a two argument normal C function
2836 is the same as for calling a @code{varargs} C function with
2837 the same two arguments.
2838 @end itemize
2839
2840 @cindex Convention Default
2841 @findex Default
2842 @item Default
2843 Equivalent to C.
2844
2845 @cindex Convention External
2846 @findex External
2847 @item External
2848 Equivalent to C.
2849
2850 @ifclear vms
2851 @findex C++
2852 @cindex Interfacing to C++
2853 @cindex Convention C++
2854 @item C_Plus_Plus (or CPP)
2855 This stands for C++. For most purposes this is identical to C.
2856 See the separate description of the specialized GNAT pragmas relating to
2857 C++ interfacing for further details.
2858 @end ifclear
2859
2860 @findex Fortran
2861 @cindex Interfacing to Fortran
2862 @cindex Convention Fortran
2863 @item Fortran
2864 Data will be passed according to the conventions described
2865 in section B.5 of the Ada Reference Manual.
2866
2867 @item Intrinsic
2868 This applies to an intrinsic operation, as defined in the Ada
2869 Reference Manual. If a pragma Import (Intrinsic) applies to a subprogram,
2870 this means that the body of the subprogram is provided by the compiler itself,
2871 usually by means of an efficient code sequence, and that the user does not
2872 supply an explicit body for it. In an application program, the pragma may
2873 be applied to the following sets of names:
2874
2875 @itemize @bullet
2876 @item
2877 Rotate_Left, Rotate_Right, Shift_Left, Shift_Right,
2878 Shift_Right_Arithmetic. The corresponding subprogram declaration must have
2879 two formal parameters. The
2880 first one must be a signed integer type or a modular type with a binary
2881 modulus, and the second parameter must be of type Natural.
2882 The return type must be the same as the type of the first argument. The size
2883 of this type can only be 8, 16, 32, or 64.
2884
2885 @item
2886 Binary arithmetic operators: ``+'', ``-'', ``*'', ``/''
2887 The corresponding operator declaration must have parameters and result type
2888 that have the same root numeric type (for example, all three are long_float
2889 types). This simplifies the definition of operations that use type checking
2890 to perform dimensional checks:
2891
2892 @smallexample @c ada
2893 type Distance is new Long_Float;
2894 type Time is new Long_Float;
2895 type Velocity is new Long_Float;
2896 function "/" (D : Distance; T : Time)
2897 return Velocity;
2898 pragma Import (Intrinsic, "/");
2899 @end smallexample
2900
2901 @noindent
2902 This common idiom is often programmed with a generic definition and an
2903 explicit body. The pragma makes it simpler to introduce such declarations.
2904 It incurs no overhead in compilation time or code size, because it is
2905 implemented as a single machine instruction.
2906
2907 @item
2908 General subprogram entities, to bind an Ada subprogram declaration to
2909 a compiler builtin by name with back-ends where such interfaces are
2910 available. A typical example is the set of ``__builtin'' functions
2911 exposed by the GCC back-end, as in the following example:
2912
2913 @smallexample @c ada
2914 function builtin_sqrt (F : Float) return Float;
2915 pragma Import (Intrinsic, builtin_sqrt, "__builtin_sqrtf");
2916 @end smallexample
2917
2918 Most of the GCC builtins are accessible this way, and as for other
2919 import conventions (e.g. C), it is the user's responsibility to ensure
2920 that the Ada subprogram profile matches the underlying builtin
2921 expectations.
2922 @end itemize
2923
2924 @noindent
2925
2926 @ifset unw
2927 @findex Stdcall
2928 @cindex Convention Stdcall
2929 @item Stdcall
2930 This is relevant only to Windows XP/2000/NT implementations of GNAT,
2931 and specifies that the @code{Stdcall} calling sequence will be used,
2932 as defined by the NT API. Nevertheless, to ease building
2933 cross-platform bindings this convention will be handled as a @code{C} calling
2934 convention on non-Windows platforms.
2935
2936 @findex DLL
2937 @cindex Convention DLL
2938 @item DLL
2939 This is equivalent to @code{Stdcall}.
2940
2941 @findex Win32
2942 @cindex Convention Win32
2943 @item Win32
2944 This is equivalent to @code{Stdcall}.
2945 @end ifset
2946
2947 @findex Stubbed
2948 @cindex Convention Stubbed
2949 @item Stubbed
2950 This is a special convention that indicates that the compiler
2951 should provide a stub body that raises @code{Program_Error}.
2952 @end table
2953
2954 @noindent
2955 GNAT additionally provides a useful pragma @code{Convention_Identifier}
2956 that can be used to parametrize conventions and allow additional synonyms
2957 to be specified. For example if you have legacy code in which the convention
2958 identifier Fortran77 was used for Fortran, you can use the configuration
2959 pragma:
2960
2961 @smallexample @c ada
2962 pragma Convention_Identifier (Fortran77, Fortran);
2963 @end smallexample
2964
2965 @noindent
2966 And from now on the identifier Fortran77 may be used as a convention
2967 identifier (for example in an @code{Import} pragma) with the same
2968 meaning as Fortran.
2969
2970 @ifclear vms
2971 @node Building Mixed Ada & C++ Programs
2972 @section Building Mixed Ada and C++ Programs
2973
2974 @noindent
2975 A programmer inexperienced with mixed-language development may find that
2976 building an application containing both Ada and C++ code can be a
2977 challenge. This section gives a few
2978 hints that should make this task easier. The first section addresses
2979 the differences between interfacing with C and interfacing with C++.
2980 The second section
2981 looks into the delicate problem of linking the complete application from
2982 its Ada and C++ parts. The last section gives some hints on how the GNAT
2983 run-time library can be adapted in order to allow inter-language dispatching
2984 with a new C++ compiler.
2985
2986 @menu
2987 * Interfacing to C++::
2988 * Linking a Mixed C++ & Ada Program::
2989 * A Simple Example::
2990 * Interfacing with C++ constructors::
2991 * Interfacing with C++ at the Class Level::
2992 @end menu
2993
2994 @node Interfacing to C++
2995 @subsection Interfacing to C++
2996
2997 @noindent
2998 GNAT supports interfacing with the G++ compiler (or any C++ compiler
2999 generating code that is compatible with the G++ Application Binary
3000 Interface ---see http://www.codesourcery.com/archives/cxx-abi).
3001
3002 @noindent
3003 Interfacing can be done at 3 levels: simple data, subprograms, and
3004 classes. In the first two cases, GNAT offers a specific @code{Convention
3005 C_Plus_Plus} (or @code{CPP}) that behaves exactly like @code{Convention C}.
3006 Usually, C++ mangles the names of subprograms. To generate proper mangled
3007 names automatically, see @ref{Generating Ada Bindings for C and C++ headers}).
3008 This problem can also be addressed manually in two ways:
3009
3010 @itemize @bullet
3011 @item
3012 by modifying the C++ code in order to force a C convention using
3013 the @code{extern "C"} syntax.
3014
3015 @item
3016 by figuring out the mangled name (using e.g. @command{nm}) and using it as the
3017 Link_Name argument of the pragma import.
3018 @end itemize
3019
3020 @noindent
3021 Interfacing at the class level can be achieved by using the GNAT specific
3022 pragmas such as @code{CPP_Constructor}. @xref{Interfacing to C++,,,
3023 gnat_rm, GNAT Reference Manual}, for additional information.
3024
3025 @node Linking a Mixed C++ & Ada Program
3026 @subsection Linking a Mixed C++ & Ada Program
3027
3028 @noindent
3029 Usually the linker of the C++ development system must be used to link
3030 mixed applications because most C++ systems will resolve elaboration
3031 issues (such as calling constructors on global class instances)
3032 transparently during the link phase. GNAT has been adapted to ease the
3033 use of a foreign linker for the last phase. Three cases can be
3034 considered:
3035 @enumerate
3036
3037 @item
3038 Using GNAT and G++ (GNU C++ compiler) from the same GCC installation:
3039 The C++ linker can simply be called by using the C++ specific driver
3040 called @code{g++}.
3041
3042 Note that if the C++ code uses inline functions, you will need to
3043 compile your C++ code with the @code{-fkeep-inline-functions} switch in
3044 order to provide an existing function implementation that the Ada code can
3045 link with.
3046
3047 @smallexample
3048 $ g++ -c -fkeep-inline-functions file1.C
3049 $ g++ -c -fkeep-inline-functions file2.C
3050 $ gnatmake ada_unit -largs file1.o file2.o --LINK=g++
3051 @end smallexample
3052
3053 @item
3054 Using GNAT and G++ from two different GCC installations: If both
3055 compilers are on the @env{PATH}, the previous method may be used. It is
3056 important to note that environment variables such as
3057 @env{C_INCLUDE_PATH}, @env{GCC_EXEC_PREFIX}, @env{BINUTILS_ROOT}, and
3058 @env{GCC_ROOT} will affect both compilers
3059 at the same time and may make one of the two compilers operate
3060 improperly if set during invocation of the wrong compiler. It is also
3061 very important that the linker uses the proper @file{libgcc.a} GCC
3062 library -- that is, the one from the C++ compiler installation. The
3063 implicit link command as suggested in the @command{gnatmake} command
3064 from the former example can be replaced by an explicit link command with
3065 the full-verbosity option in order to verify which library is used:
3066 @smallexample
3067 $ gnatbind ada_unit
3068 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=c++
3069 @end smallexample
3070 If there is a problem due to interfering environment variables, it can
3071 be worked around by using an intermediate script. The following example
3072 shows the proper script to use when GNAT has not been installed at its
3073 default location and g++ has been installed at its default location:
3074
3075 @smallexample
3076 $ cat ./my_script
3077 #!/bin/sh
3078 unset BINUTILS_ROOT
3079 unset GCC_ROOT
3080 c++ $*
3081 $ gnatlink -v -v ada_unit file1.o file2.o --LINK=./my_script
3082 @end smallexample
3083
3084 @item
3085 Using a non-GNU C++ compiler: The commands previously described can be
3086 used to insure that the C++ linker is used. Nonetheless, you need to add
3087 a few more parameters to the link command line, depending on the exception
3088 mechanism used.
3089
3090 If the @code{setjmp/longjmp} exception mechanism is used, only the paths
3091 to the libgcc libraries are required:
3092
3093 @smallexample
3094 $ cat ./my_script
3095 #!/bin/sh
3096 CC $* `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a`
3097 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3098 @end smallexample
3099
3100 Where CC is the name of the non-GNU C++ compiler.
3101
3102 If the @code{zero cost} exception mechanism is used, and the platform
3103 supports automatic registration of exception tables (e.g.@: Solaris or IRIX),
3104 paths to more objects are required:
3105
3106 @smallexample
3107 $ cat ./my_script
3108 #!/bin/sh
3109 CC `gcc -print-file-name=crtbegin.o` $* \
3110 `gcc -print-file-name=libgcc.a` `gcc -print-file-name=libgcc_eh.a` \
3111 `gcc -print-file-name=crtend.o`
3112 $ gnatlink ada_unit file1.o file2.o --LINK=./my_script
3113 @end smallexample
3114
3115 If the @code{zero cost} exception mechanism is used, and the platform
3116 doesn't support automatic registration of exception tables (e.g.@: HP-UX,
3117 Tru64 or AIX), the simple approach described above will not work and
3118 a pre-linking phase using GNAT will be necessary.
3119
3120 @end enumerate
3121
3122 Another alternative is to use the @command{gprbuild} multi-language builder
3123 which has a large knowledge base and knows how to link Ada and C++ code
3124 together automatically in most cases.
3125
3126 @node A Simple Example
3127 @subsection A Simple Example
3128 @noindent
3129 The following example, provided as part of the GNAT examples, shows how
3130 to achieve procedural interfacing between Ada and C++ in both
3131 directions. The C++ class A has two methods. The first method is exported
3132 to Ada by the means of an extern C wrapper function. The second method
3133 calls an Ada subprogram. On the Ada side, The C++ calls are modelled by
3134 a limited record with a layout comparable to the C++ class. The Ada
3135 subprogram, in turn, calls the C++ method. So, starting from the C++
3136 main program, the process passes back and forth between the two
3137 languages.
3138
3139 @noindent
3140 Here are the compilation commands:
3141 @smallexample
3142 $ gnatmake -c simple_cpp_interface
3143 $ g++ -c cpp_main.C
3144 $ g++ -c ex7.C
3145 $ gnatbind -n simple_cpp_interface
3146 $ gnatlink simple_cpp_interface -o cpp_main --LINK=g++
3147 -lstdc++ ex7.o cpp_main.o
3148 @end smallexample
3149
3150 @noindent
3151 Here are the corresponding sources:
3152 @smallexample
3153
3154 //cpp_main.C
3155
3156 #include "ex7.h"
3157
3158 extern "C" @{
3159 void adainit (void);
3160 void adafinal (void);
3161 void method1 (A *t);
3162 @}
3163
3164 void method1 (A *t)
3165 @{
3166 t->method1 ();
3167 @}
3168
3169 int main ()
3170 @{
3171 A obj;
3172 adainit ();
3173 obj.method2 (3030);
3174 adafinal ();
3175 @}
3176
3177 //ex7.h
3178
3179 class Origin @{
3180 public:
3181 int o_value;
3182 @};
3183 class A : public Origin @{
3184 public:
3185 void method1 (void);
3186 void method2 (int v);
3187 A();
3188 int a_value;
3189 @};
3190
3191 //ex7.C
3192
3193 #include "ex7.h"
3194 #include <stdio.h>
3195
3196 extern "C" @{ void ada_method2 (A *t, int v);@}
3197
3198 void A::method1 (void)
3199 @{
3200 a_value = 2020;
3201 printf ("in A::method1, a_value = %d \n",a_value);
3202
3203 @}
3204
3205 void A::method2 (int v)
3206 @{
3207 ada_method2 (this, v);
3208 printf ("in A::method2, a_value = %d \n",a_value);
3209
3210 @}
3211
3212 A::A(void)
3213 @{
3214 a_value = 1010;
3215 printf ("in A::A, a_value = %d \n",a_value);
3216 @}
3217 @end smallexample
3218
3219 @smallexample @c ada
3220 -- Ada sources
3221 package body Simple_Cpp_Interface is
3222
3223 procedure Ada_Method2 (This : in out A; V : Integer) is
3224 begin
3225 Method1 (This);
3226 This.A_Value := V;
3227 end Ada_Method2;
3228
3229 end Simple_Cpp_Interface;
3230
3231 with System;
3232 package Simple_Cpp_Interface is
3233 type A is limited
3234 record
3235 Vptr : System.Address;
3236 O_Value : Integer;
3237 A_Value : Integer;
3238 end record;
3239 pragma Convention (C, A);
3240
3241 procedure Method1 (This : in out A);
3242 pragma Import (C, Method1);
3243
3244 procedure Ada_Method2 (This : in out A; V : Integer);
3245 pragma Export (C, Ada_Method2);
3246
3247 end Simple_Cpp_Interface;
3248 @end smallexample
3249
3250 @node Interfacing with C++ constructors
3251 @subsection Interfacing with C++ constructors
3252 @noindent
3253
3254 In order to interface with C++ constructors GNAT provides the
3255 @code{pragma CPP_Constructor} (@xref{Interfacing to C++,,,
3256 gnat_rm, GNAT Reference Manual}, for additional information).
3257 In this section we present some common uses of C++ constructors
3258 in mixed-languages programs in GNAT.
3259
3260 Let us assume that we need to interface with the following
3261 C++ class:
3262
3263 @smallexample
3264 @b{class} Root @{
3265 @b{public}:
3266 int a_value;
3267 int b_value;
3268 @b{virtual} int Get_Value ();
3269 Root(); // Default constructor
3270 Root(int v); // 1st non-default constructor
3271 Root(int v, int w); // 2nd non-default constructor
3272 @};
3273 @end smallexample
3274
3275 For this purpose we can write the following package spec (further
3276 information on how to build this spec is available in
3277 @ref{Interfacing with C++ at the Class Level} and
3278 @ref{Generating Ada Bindings for C and C++ headers}).
3279
3280 @smallexample @c ada
3281 with Interfaces.C; use Interfaces.C;
3282 package Pkg_Root is
3283 type Root is tagged limited record
3284 A_Value : int;
3285 B_Value : int;
3286 end record;
3287 pragma Import (CPP, Root);
3288
3289 function Get_Value (Obj : Root) return int;
3290 pragma Import (CPP, Get_Value);
3291
3292 function Constructor return Root;
3293 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ev");
3294
3295 function Constructor (v : Integer) return Root;
3296 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Ei");
3297
3298 function Constructor (v, w : Integer) return Root;
3299 pragma Cpp_Constructor (Constructor, "_ZN4RootC1Eii");
3300 end Pkg_Root;
3301 @end smallexample
3302
3303 On the Ada side the constructor is represented by a function (whose
3304 name is arbitrary) that returns the classwide type corresponding to
3305 the imported C++ class. Although the constructor is described as a
3306 function, it is typically a procedure with an extra implicit argument
3307 (the object being initialized) at the implementation level. GNAT
3308 issues the appropriate call, whatever it is, to get the object
3309 properly initialized.
3310
3311 Constructors can only appear in the following contexts:
3312
3313 @itemize @bullet
3314 @item
3315 On the right side of an initialization of an object of type @var{T}.
3316 @item
3317 On the right side of an initialization of a record component of type @var{T}.
3318 @item
3319 In an Ada 2005 limited aggregate.
3320 @item
3321 In an Ada 2005 nested limited aggregate.
3322 @item
3323 In an Ada 2005 limited aggregate that initializes an object built in
3324 place by an extended return statement.
3325 @end itemize
3326
3327 @noindent
3328 In a declaration of an object whose type is a class imported from C++,
3329 either the default C++ constructor is implicitly called by GNAT, or
3330 else the required C++ constructor must be explicitly called in the
3331 expression that initializes the object. For example:
3332
3333 @smallexample @c ada
3334 Obj1 : Root;
3335 Obj2 : Root := Constructor;
3336 Obj3 : Root := Constructor (v => 10);
3337 Obj4 : Root := Constructor (30, 40);
3338 @end smallexample
3339
3340 The first two declarations are equivalent: in both cases the default C++
3341 constructor is invoked (in the former case the call to the constructor is
3342 implicit, and in the latter case the call is explicit in the object
3343 declaration). @code{Obj3} is initialized by the C++ non-default constructor
3344 that takes an integer argument, and @code{Obj4} is initialized by the
3345 non-default C++ constructor that takes two integers.
3346
3347 Let us derive the imported C++ class in the Ada side. For example:
3348
3349 @smallexample @c ada
3350 type DT is new Root with record
3351 C_Value : Natural := 2009;
3352 end record;
3353 @end smallexample
3354
3355 In this case the components DT inherited from the C++ side must be
3356 initialized by a C++ constructor, and the additional Ada components
3357 of type DT are initialized by GNAT. The initialization of such an
3358 object is done either by default, or by means of a function returning
3359 an aggregate of type DT, or by means of an extension aggregate.
3360
3361 @smallexample @c ada
3362 Obj5 : DT;
3363 Obj6 : DT := Function_Returning_DT (50);
3364 Obj7 : DT := (Constructor (30,40) with C_Value => 50);
3365 @end smallexample
3366
3367 The declaration of @code{Obj5} invokes the default constructors: the
3368 C++ default constructor of the parent type takes care of the initialization
3369 of the components inherited from Root, and GNAT takes care of the default
3370 initialization of the additional Ada components of type DT (that is,
3371 @code{C_Value} is initialized to value 2009). The order of invocation of
3372 the constructors is consistent with the order of elaboration required by
3373 Ada and C++. That is, the constructor of the parent type is always called
3374 before the constructor of the derived type.
3375
3376 Let us now consider a record that has components whose type is imported
3377 from C++. For example:
3378
3379 @smallexample @c ada
3380 type Rec1 is limited record
3381 Data1 : Root := Constructor (10);
3382 Value : Natural := 1000;
3383 end record;
3384
3385 type Rec2 (D : Integer := 20) is limited record
3386 Rec : Rec1;
3387 Data2 : Root := Constructor (D, 30);
3388 end record;
3389 @end smallexample
3390
3391 The initialization of an object of type @code{Rec2} will call the
3392 non-default C++ constructors specified for the imported components.
3393 For example:
3394
3395 @smallexample @c ada
3396 Obj8 : Rec2 (40);
3397 @end smallexample
3398
3399 Using Ada 2005 we can use limited aggregates to initialize an object
3400 invoking C++ constructors that differ from those specified in the type
3401 declarations. For example:
3402
3403 @smallexample @c ada
3404 Obj9 : Rec2 := (Rec => (Data1 => Constructor (15, 16),
3405 others => <>),
3406 others => <>);
3407 @end smallexample
3408
3409 The above declaration uses an Ada 2005 limited aggregate to
3410 initialize @code{Obj9}, and the C++ constructor that has two integer
3411 arguments is invoked to initialize the @code{Data1} component instead
3412 of the constructor specified in the declaration of type @code{Rec1}. In
3413 Ada 2005 the box in the aggregate indicates that unspecified components
3414 are initialized using the expression (if any) available in the component
3415 declaration. That is, in this case discriminant @code{D} is initialized
3416 to value @code{20}, @code{Value} is initialized to value 1000, and the
3417 non-default C++ constructor that handles two integers takes care of
3418 initializing component @code{Data2} with values @code{20,30}.
3419
3420 In Ada 2005 we can use the extended return statement to build the Ada
3421 equivalent to C++ non-default constructors. For example:
3422
3423 @smallexample @c ada
3424 function Constructor (V : Integer) return Rec2 is
3425 begin
3426 return Obj : Rec2 := (Rec => (Data1 => Constructor (V, 20),
3427 others => <>),
3428 others => <>) do
3429 -- Further actions required for construction of
3430 -- objects of type Rec2
3431 ...
3432 end record;
3433 end Constructor;
3434 @end smallexample
3435
3436 In this example the extended return statement construct is used to
3437 build in place the returned object whose components are initialized
3438 by means of a limited aggregate. Any further action associated with
3439 the constructor can be placed inside the construct.
3440
3441 @node Interfacing with C++ at the Class Level
3442 @subsection Interfacing with C++ at the Class Level
3443 @noindent
3444 In this section we demonstrate the GNAT features for interfacing with
3445 C++ by means of an example making use of Ada 2005 abstract interface
3446 types. This example consists of a classification of animals; classes
3447 have been used to model our main classification of animals, and
3448 interfaces provide support for the management of secondary
3449 classifications. We first demonstrate a case in which the types and
3450 constructors are defined on the C++ side and imported from the Ada
3451 side, and latter the reverse case.
3452
3453 The root of our derivation will be the @code{Animal} class, with a
3454 single private attribute (the @code{Age} of the animal) and two public
3455 primitives to set and get the value of this attribute.
3456
3457 @smallexample
3458 @b{class} Animal @{
3459 @b{public}:
3460 @b{virtual} void Set_Age (int New_Age);
3461 @b{virtual} int Age ();
3462 @b{private}:
3463 int Age_Count;
3464 @};
3465 @end smallexample
3466
3467 Abstract interface types are defined in C++ by means of classes with pure
3468 virtual functions and no data members. In our example we will use two
3469 interfaces that provide support for the common management of @code{Carnivore}
3470 and @code{Domestic} animals:
3471
3472 @smallexample
3473 @b{class} Carnivore @{
3474 @b{public}:
3475 @b{virtual} int Number_Of_Teeth () = 0;
3476 @};
3477
3478 @b{class} Domestic @{
3479 @b{public}:
3480 @b{virtual void} Set_Owner (char* Name) = 0;
3481 @};
3482 @end smallexample
3483
3484 Using these declarations, we can now say that a @code{Dog} is an animal that is
3485 both Carnivore and Domestic, that is:
3486
3487 @smallexample
3488 @b{class} Dog : Animal, Carnivore, Domestic @{
3489 @b{public}:
3490 @b{virtual} int Number_Of_Teeth ();
3491 @b{virtual} void Set_Owner (char* Name);
3492
3493 Dog(); // Constructor
3494 @b{private}:
3495 int Tooth_Count;
3496 char *Owner;
3497 @};
3498 @end smallexample
3499
3500 In the following examples we will assume that the previous declarations are
3501 located in a file named @code{animals.h}. The following package demonstrates
3502 how to import these C++ declarations from the Ada side:
3503
3504 @smallexample @c ada
3505 with Interfaces.C.Strings; use Interfaces.C.Strings;
3506 package Animals is
3507 type Carnivore is interface;
3508 pragma Convention (C_Plus_Plus, Carnivore);
3509 function Number_Of_Teeth (X : Carnivore)
3510 return Natural is abstract;
3511
3512 type Domestic is interface;
3513 pragma Convention (C_Plus_Plus, Set_Owner);
3514 procedure Set_Owner
3515 (X : in out Domestic;
3516 Name : Chars_Ptr) is abstract;
3517
3518 type Animal is tagged record
3519 Age : Natural := 0;
3520 end record;
3521 pragma Import (C_Plus_Plus, Animal);
3522
3523 procedure Set_Age (X : in out Animal; Age : Integer);
3524 pragma Import (C_Plus_Plus, Set_Age);
3525
3526 function Age (X : Animal) return Integer;
3527 pragma Import (C_Plus_Plus, Age);
3528
3529 type Dog is new Animal and Carnivore and Domestic with record
3530 Tooth_Count : Natural;
3531 Owner : String (1 .. 30);
3532 end record;
3533 pragma Import (C_Plus_Plus, Dog);
3534
3535 function Number_Of_Teeth (A : Dog) return Integer;
3536 pragma Import (C_Plus_Plus, Number_Of_Teeth);
3537
3538 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
3539 pragma Import (C_Plus_Plus, Set_Owner);
3540
3541 function New_Dog return Dog;
3542 pragma CPP_Constructor (New_Dog);
3543 pragma Import (CPP, New_Dog, "_ZN3DogC2Ev");
3544 end Animals;
3545 @end smallexample
3546
3547 Thanks to the compatibility between GNAT run-time structures and the C++ ABI,
3548 interfacing with these C++ classes is easy. The only requirement is that all
3549 the primitives and components must be declared exactly in the same order in
3550 the two languages.
3551
3552 Regarding the abstract interfaces, we must indicate to the GNAT compiler by
3553 means of a @code{pragma Convention (C_Plus_Plus)}, the convention used to pass
3554 the arguments to the called primitives will be the same as for C++. For the
3555 imported classes we use @code{pragma Import} with convention @code{C_Plus_Plus}
3556 to indicate that they have been defined on the C++ side; this is required
3557 because the dispatch table associated with these tagged types will be built
3558 in the C++ side and therefore will not contain the predefined Ada primitives
3559 which Ada would otherwise expect.
3560
3561 As the reader can see there is no need to indicate the C++ mangled names
3562 associated with each subprogram because it is assumed that all the calls to
3563 these primitives will be dispatching calls. The only exception is the
3564 constructor, which must be registered with the compiler by means of
3565 @code{pragma CPP_Constructor} and needs to provide its associated C++
3566 mangled name because the Ada compiler generates direct calls to it.
3567
3568 With the above packages we can now declare objects of type Dog on the Ada side
3569 and dispatch calls to the corresponding subprograms on the C++ side. We can
3570 also extend the tagged type Dog with further fields and primitives, and
3571 override some of its C++ primitives on the Ada side. For example, here we have
3572 a type derivation defined on the Ada side that inherits all the dispatching
3573 primitives of the ancestor from the C++ side.
3574
3575 @smallexample
3576 @b{with} Animals; @b{use} Animals;
3577 @b{package} Vaccinated_Animals @b{is}
3578 @b{type} Vaccinated_Dog @b{is new} Dog @b{with null record};
3579 @b{function} Vaccination_Expired (A : Vaccinated_Dog) @b{return} Boolean;
3580 @b{end} Vaccinated_Animals;
3581 @end smallexample
3582
3583 It is important to note that, because of the ABI compatibility, the programmer
3584 does not need to add any further information to indicate either the object
3585 layout or the dispatch table entry associated with each dispatching operation.
3586
3587 Now let us define all the types and constructors on the Ada side and export
3588 them to C++, using the same hierarchy of our previous example:
3589
3590 @smallexample @c ada
3591 with Interfaces.C.Strings;
3592 use Interfaces.C.Strings;
3593 package Animals is
3594 type Carnivore is interface;
3595 pragma Convention (C_Plus_Plus, Carnivore);
3596 function Number_Of_Teeth (X : Carnivore)
3597 return Natural is abstract;
3598
3599 type Domestic is interface;
3600 pragma Convention (C_Plus_Plus, Set_Owner);
3601 procedure Set_Owner
3602 (X : in out Domestic;
3603 Name : Chars_Ptr) is abstract;
3604
3605 type Animal is tagged record
3606 Age : Natural := 0;
3607 end record;
3608 pragma Convention (C_Plus_Plus, Animal);
3609
3610 procedure Set_Age (X : in out Animal; Age : Integer);
3611 pragma Export (C_Plus_Plus, Set_Age);
3612
3613 function Age (X : Animal) return Integer;
3614 pragma Export (C_Plus_Plus, Age);
3615
3616 type Dog is new Animal and Carnivore and Domestic with record
3617 Tooth_Count : Natural;
3618 Owner : String (1 .. 30);
3619 end record;
3620 pragma Convention (C_Plus_Plus, Dog);
3621
3622 function Number_Of_Teeth (A : Dog) return Integer;
3623 pragma Export (C_Plus_Plus, Number_Of_Teeth);
3624
3625 procedure Set_Owner (A : in out Dog; Name : Chars_Ptr);
3626 pragma Export (C_Plus_Plus, Set_Owner);
3627
3628 function New_Dog return Dog'Class;
3629 pragma Export (C_Plus_Plus, New_Dog);
3630 end Animals;
3631 @end smallexample
3632
3633 Compared with our previous example the only difference is the use of
3634 @code{pragma Export} to indicate to the GNAT compiler that the primitives will
3635 be available to C++. Thanks to the ABI compatibility, on the C++ side there is
3636 nothing else to be done; as explained above, the only requirement is that all
3637 the primitives and components are declared in exactly the same order.
3638
3639 For completeness, let us see a brief C++ main program that uses the
3640 declarations available in @code{animals.h} (presented in our first example) to
3641 import and use the declarations from the Ada side, properly initializing and
3642 finalizing the Ada run-time system along the way:
3643
3644 @smallexample
3645 @b{#include} "animals.h"
3646 @b{#include} <iostream>
3647 @b{using namespace} std;
3648
3649 void Check_Carnivore (Carnivore *obj) @{@dots{}@}
3650 void Check_Domestic (Domestic *obj) @{@dots{}@}
3651 void Check_Animal (Animal *obj) @{@dots{}@}
3652 void Check_Dog (Dog *obj) @{@dots{}@}
3653
3654 @b{extern} "C" @{
3655 void adainit (void);
3656 void adafinal (void);
3657 Dog* new_dog ();
3658 @}
3659
3660 void test ()
3661 @{
3662 Dog *obj = new_dog(); // Ada constructor
3663 Check_Carnivore (obj); // Check secondary DT
3664 Check_Domestic (obj); // Check secondary DT
3665 Check_Animal (obj); // Check primary DT
3666 Check_Dog (obj); // Check primary DT
3667 @}
3668
3669 int main ()
3670 @{
3671 adainit (); test(); adafinal ();
3672 return 0;
3673 @}
3674 @end smallexample
3675
3676 @node Comparison between GNAT and C/C++ Compilation Models
3677 @section Comparison between GNAT and C/C++ Compilation Models
3678
3679 @noindent
3680 The GNAT model of compilation is close to the C and C++ models. You can
3681 think of Ada specs as corresponding to header files in C. As in C, you
3682 don't need to compile specs; they are compiled when they are used. The
3683 Ada @code{with} is similar in effect to the @code{#include} of a C
3684 header.
3685
3686 One notable difference is that, in Ada, you may compile specs separately
3687 to check them for semantic and syntactic accuracy. This is not always
3688 possible with C headers because they are fragments of programs that have
3689 less specific syntactic or semantic rules.
3690
3691 The other major difference is the requirement for running the binder,
3692 which performs two important functions. First, it checks for
3693 consistency. In C or C++, the only defense against assembling
3694 inconsistent programs lies outside the compiler, in a makefile, for
3695 example. The binder satisfies the Ada requirement that it be impossible
3696 to construct an inconsistent program when the compiler is used in normal
3697 mode.
3698
3699 @cindex Elaboration order control
3700 The other important function of the binder is to deal with elaboration
3701 issues. There are also elaboration issues in C++ that are handled
3702 automatically. This automatic handling has the advantage of being
3703 simpler to use, but the C++ programmer has no control over elaboration.
3704 Where @code{gnatbind} might complain there was no valid order of
3705 elaboration, a C++ compiler would simply construct a program that
3706 malfunctioned at run time.
3707 @end ifclear
3708
3709 @node Comparison between GNAT and Conventional Ada Library Models
3710 @section Comparison between GNAT and Conventional Ada Library Models
3711
3712 @noindent
3713 This section is intended for Ada programmers who have
3714 used an Ada compiler implementing the traditional Ada library
3715 model, as described in the Ada Reference Manual.
3716
3717 @cindex GNAT library
3718 In GNAT, there is no ``library'' in the normal sense. Instead, the set of
3719 source files themselves acts as the library. Compiling Ada programs does
3720 not generate any centralized information, but rather an object file and
3721 a ALI file, which are of interest only to the binder and linker.
3722 In a traditional system, the compiler reads information not only from
3723 the source file being compiled, but also from the centralized library.
3724 This means that the effect of a compilation depends on what has been
3725 previously compiled. In particular:
3726
3727 @itemize @bullet
3728 @item
3729 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3730 to the version of the unit most recently compiled into the library.
3731
3732 @item
3733 Inlining is effective only if the necessary body has already been
3734 compiled into the library.
3735
3736 @item
3737 Compiling a unit may obsolete other units in the library.
3738 @end itemize
3739
3740 @noindent
3741 In GNAT, compiling one unit never affects the compilation of any other
3742 units because the compiler reads only source files. Only changes to source
3743 files can affect the results of a compilation. In particular:
3744
3745 @itemize @bullet
3746 @item
3747 When a unit is @code{with}'ed, the unit seen by the compiler corresponds
3748 to the source version of the unit that is currently accessible to the
3749 compiler.
3750
3751 @item
3752 @cindex Inlining
3753 Inlining requires the appropriate source files for the package or
3754 subprogram bodies to be available to the compiler. Inlining is always
3755 effective, independent of the order in which units are complied.
3756
3757 @item
3758 Compiling a unit never affects any other compilations. The editing of
3759 sources may cause previous compilations to be out of date if they
3760 depended on the source file being modified.
3761 @end itemize
3762
3763 @noindent
3764 The most important result of these differences is that order of compilation
3765 is never significant in GNAT. There is no situation in which one is
3766 required to do one compilation before another. What shows up as order of
3767 compilation requirements in the traditional Ada library becomes, in
3768 GNAT, simple source dependencies; in other words, there is only a set
3769 of rules saying what source files must be present when a file is
3770 compiled.
3771
3772 @ifset vms
3773 @node Placement of temporary files
3774 @section Placement of temporary files
3775 @cindex Temporary files (user control over placement)
3776
3777 @noindent
3778 GNAT creates temporary files in the directory designated by the environment
3779 variable @env{TMPDIR}.
3780 (See the HP @emph{C RTL Reference Manual} on the function @code{getenv()}
3781 for detailed information on how environment variables are resolved.
3782 For most users the easiest way to make use of this feature is to simply
3783 define @env{TMPDIR} as a job level logical name).
3784 For example, if you wish to use a Ramdisk (assuming DECRAM is installed)
3785 for compiler temporary files, then you can include something like the
3786 following command in your @file{LOGIN.COM} file:
3787
3788 @smallexample
3789 $ define/job TMPDIR "/disk$scratchram/000000/temp/"
3790 @end smallexample
3791
3792 @noindent
3793 If @env{TMPDIR} is not defined, then GNAT uses the directory designated by
3794 @env{TMP}; if @env{TMP} is not defined, then GNAT uses the directory
3795 designated by @env{TEMP}.
3796 If none of these environment variables are defined then GNAT uses the
3797 directory designated by the logical name @code{SYS$SCRATCH:}
3798 (by default the user's home directory). If all else fails
3799 GNAT uses the current directory for temporary files.
3800 @end ifset
3801
3802 @c *************************
3803 @node Compiling Using gcc
3804 @chapter Compiling Using @command{gcc}
3805
3806 @noindent
3807 This chapter discusses how to compile Ada programs using the @command{gcc}
3808 command. It also describes the set of switches
3809 that can be used to control the behavior of the compiler.
3810 @menu
3811 * Compiling Programs::
3812 * Switches for gcc::
3813 * Search Paths and the Run-Time Library (RTL)::
3814 * Order of Compilation Issues::
3815 * Examples::
3816 @end menu
3817
3818 @node Compiling Programs
3819 @section Compiling Programs
3820
3821 @noindent
3822 The first step in creating an executable program is to compile the units
3823 of the program using the @command{gcc} command. You must compile the
3824 following files:
3825
3826 @itemize @bullet
3827 @item
3828 the body file (@file{.adb}) for a library level subprogram or generic
3829 subprogram
3830
3831 @item
3832 the spec file (@file{.ads}) for a library level package or generic
3833 package that has no body
3834
3835 @item
3836 the body file (@file{.adb}) for a library level package
3837 or generic package that has a body
3838
3839 @end itemize
3840
3841 @noindent
3842 You need @emph{not} compile the following files
3843
3844 @itemize @bullet
3845
3846 @item
3847 the spec of a library unit which has a body
3848
3849 @item
3850 subunits
3851 @end itemize
3852
3853 @noindent
3854 because they are compiled as part of compiling related units. GNAT
3855 package specs
3856 when the corresponding body is compiled, and subunits when the parent is
3857 compiled.
3858
3859 @cindex cannot generate code
3860 If you attempt to compile any of these files, you will get one of the
3861 following error messages (where @var{fff} is the name of the file you compiled):
3862
3863 @smallexample
3864 cannot generate code for file @var{fff} (package spec)
3865 to check package spec, use -gnatc
3866
3867 cannot generate code for file @var{fff} (missing subunits)
3868 to check parent unit, use -gnatc
3869
3870 cannot generate code for file @var{fff} (subprogram spec)
3871 to check subprogram spec, use -gnatc
3872
3873 cannot generate code for file @var{fff} (subunit)
3874 to check subunit, use -gnatc
3875 @end smallexample
3876
3877 @noindent
3878 As indicated by the above error messages, if you want to submit
3879 one of these files to the compiler to check for correct semantics
3880 without generating code, then use the @option{-gnatc} switch.
3881
3882 The basic command for compiling a file containing an Ada unit is
3883
3884 @smallexample
3885 @c $ gcc -c @ovar{switches} @file{file name}
3886 @c Expanding @ovar macro inline (explanation in macro def comments)
3887 $ gcc -c @r{[}@var{switches}@r{]} @file{file name}
3888 @end smallexample
3889
3890 @noindent
3891 where @var{file name} is the name of the Ada file (usually
3892 having an extension
3893 @file{.ads} for a spec or @file{.adb} for a body).
3894 @ifclear vms
3895 You specify the
3896 @option{-c} switch to tell @command{gcc} to compile, but not link, the file.
3897 @end ifclear
3898 The result of a successful compilation is an object file, which has the
3899 same name as the source file but an extension of @file{.o} and an Ada
3900 Library Information (ALI) file, which also has the same name as the
3901 source file, but with @file{.ali} as the extension. GNAT creates these
3902 two output files in the current directory, but you may specify a source
3903 file in any directory using an absolute or relative path specification
3904 containing the directory information.
3905
3906 @findex gnat1
3907 @command{gcc} is actually a driver program that looks at the extensions of
3908 the file arguments and loads the appropriate compiler. For example, the
3909 GNU C compiler is @file{cc1}, and the Ada compiler is @file{gnat1}.
3910 These programs are in directories known to the driver program (in some
3911 configurations via environment variables you set), but need not be in
3912 your path. The @command{gcc} driver also calls the assembler and any other
3913 utilities needed to complete the generation of the required object
3914 files.
3915
3916 It is possible to supply several file names on the same @command{gcc}
3917 command. This causes @command{gcc} to call the appropriate compiler for
3918 each file. For example, the following command lists three separate
3919 files to be compiled:
3920
3921 @smallexample
3922 $ gcc -c x.adb y.adb z.c
3923 @end smallexample
3924
3925 @noindent
3926 calls @code{gnat1} (the Ada compiler) twice to compile @file{x.adb} and
3927 @file{y.adb}, and @code{cc1} (the C compiler) once to compile @file{z.c}.
3928 The compiler generates three object files @file{x.o}, @file{y.o} and
3929 @file{z.o} and the two ALI files @file{x.ali} and @file{y.ali} from the
3930 Ada compilations. Any switches apply to all the files ^listed,^listed.^
3931 @ifclear vms
3932 except for
3933 @option{-gnat@var{x}} switches, which apply only to Ada compilations.
3934 @end ifclear
3935
3936 @node Switches for gcc
3937 @section Switches for @command{gcc}
3938
3939 @noindent
3940 The @command{gcc} command accepts switches that control the
3941 compilation process. These switches are fully described in this section.
3942 First we briefly list all the switches, in alphabetical order, then we
3943 describe the switches in more detail in functionally grouped sections.
3944
3945 More switches exist for GCC than those documented here, especially
3946 for specific targets. However, their use is not recommended as
3947 they may change code generation in ways that are incompatible with
3948 the Ada run-time library, or can cause inconsistencies between
3949 compilation units.
3950
3951 @menu
3952 * Output and Error Message Control::
3953 * Warning Message Control::
3954 * Debugging and Assertion Control::
3955 * Validity Checking::
3956 * Style Checking::
3957 * Run-Time Checks::
3958 * Using gcc for Syntax Checking::
3959 * Using gcc for Semantic Checking::
3960 * Compiling Different Versions of Ada::
3961 * Character Set Control::
3962 * File Naming Control::
3963 * Subprogram Inlining Control::
3964 * Auxiliary Output Control::
3965 * Debugging Control::
3966 * Exception Handling Control::
3967 * Units to Sources Mapping Files::
3968 * Integrated Preprocessing::
3969 * Code Generation Control::
3970 @ifset vms
3971 * Return Codes::
3972 @end ifset
3973 @end menu
3974
3975 @table @option
3976 @c !sort!
3977 @ifclear vms
3978 @cindex @option{-b} (@command{gcc})
3979 @item -b @var{target}
3980 Compile your program to run on @var{target}, which is the name of a
3981 system configuration. You must have a GNAT cross-compiler built if
3982 @var{target} is not the same as your host system.
3983
3984 @item -B@var{dir}
3985 @cindex @option{-B} (@command{gcc})
3986 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
3987 from @var{dir} instead of the default location. Only use this switch
3988 when multiple versions of the GNAT compiler are available.
3989 @xref{Directory Options,, Options for Directory Search, gcc, Using the
3990 GNU Compiler Collection (GCC)}, for further details. You would normally
3991 use the @option{-b} or @option{-V} switch instead.
3992
3993 @item -c
3994 @cindex @option{-c} (@command{gcc})
3995 Compile. Always use this switch when compiling Ada programs.
3996
3997 Note: for some other languages when using @command{gcc}, notably in
3998 the case of C and C++, it is possible to use
3999 use @command{gcc} without a @option{-c} switch to
4000 compile and link in one step. In the case of GNAT, you
4001 cannot use this approach, because the binder must be run
4002 and @command{gcc} cannot be used to run the GNAT binder.
4003 @end ifclear
4004
4005 @item -fno-inline
4006 @cindex @option{-fno-inline} (@command{gcc})
4007 Suppresses all back-end inlining, even if other optimization or inlining
4008 switches are set.
4009 This includes suppression of inlining that results
4010 from the use of the pragma @code{Inline_Always}.
4011 Any occurrences of pragma @code{Inline} or @code{Inline_Always}
4012 are ignored, and @option{-gnatn} and @option{-gnatN} have no
4013 effect if this switch is present.
4014
4015 @item -fno-inline-functions
4016 @cindex @option{-fno-inline-functions} (@command{gcc})
4017 Suppresses automatic inlining of simple subprograms, which is enabled
4018 if @option{-O3} is used.
4019
4020 @item -fno-inline-small-functions
4021 @cindex @option{-fno-inline-small-functions} (@command{gcc})
4022 Suppresses automatic inlining of small subprograms, which is enabled
4023 if @option{-O2} is used.
4024
4025 @item -fno-inline-functions-called-once
4026 @cindex @option{-fno-inline-functions-called-once} (@command{gcc})
4027 Suppresses inlining of subprograms local to the unit and called once
4028 from within it, which is enabled if @option{-O1} is used.
4029
4030 @item -fno-ivopts
4031 @cindex @option{-fno-ivopts} (@command{gcc})
4032 Suppresses high-level loop induction variable optimizations, which are
4033 enabled if @option{-O1} is used. These optimizations are generally
4034 profitable but, for some specific cases of loops with numerous uses
4035 of the iteration variable that follow a common pattern, they may end
4036 up destroying the regularity that could be exploited at a lower level
4037 and thus producing inferior code.
4038
4039 @item -fno-strict-aliasing
4040 @cindex @option{-fno-strict-aliasing} (@command{gcc})
4041 Causes the compiler to avoid assumptions regarding non-aliasing
4042 of objects of different types. See
4043 @ref{Optimization and Strict Aliasing} for details.
4044
4045 @item -fstack-check
4046 @cindex @option{-fstack-check} (@command{gcc})
4047 Activates stack checking.
4048 See @ref{Stack Overflow Checking} for details.
4049
4050 @item -fstack-usage
4051 @cindex @option{-fstack-usage} (@command{gcc})
4052 Makes the compiler output stack usage information for the program, on a
4053 per-function basis. See @ref{Static Stack Usage Analysis} for details.
4054
4055 @item -fcallgraph-info@r{[}=su@r{]}
4056 @cindex @option{-fcallgraph-info} (@command{gcc})
4057 Makes the compiler output callgraph information for the program, on a
4058 per-file basis. The information is generated in the VCG format. It can
4059 be decorated with stack-usage per-node information.
4060
4061 @item ^-g^/DEBUG^
4062 @cindex @option{^-g^/DEBUG^} (@command{gcc})
4063 Generate debugging information. This information is stored in the object
4064 file and copied from there to the final executable file by the linker,
4065 where it can be read by the debugger. You must use the
4066 @option{^-g^/DEBUG^} switch if you plan on using the debugger.
4067
4068 @item -gnat83
4069 @cindex @option{-gnat83} (@command{gcc})
4070 Enforce Ada 83 restrictions.
4071
4072 @item -gnat95
4073 @cindex @option{-gnat95} (@command{gcc})
4074 Enforce Ada 95 restrictions.
4075
4076 @item -gnat05
4077 @cindex @option{-gnat05} (@command{gcc})
4078 Allow full Ada 2005 features.
4079
4080 @item -gnata
4081 @cindex @option{-gnata} (@command{gcc})
4082 Assertions enabled. @code{Pragma Assert} and @code{pragma Debug} to be
4083 activated. Note that these pragmas can also be controlled using the
4084 configuration pragmas @code{Assertion_Policy} and @code{Debug_Policy}.
4085 It also activates pragmas @code{Check}, @code{Precondition}, and
4086 @code{Postcondition}. Note that these pragmas can also be controlled
4087 using the configuration pragma @code{Check_Policy}.
4088
4089 @item -gnatA
4090 @cindex @option{-gnatA} (@command{gcc})
4091 Avoid processing @file{gnat.adc}. If a @file{gnat.adc} file is present,
4092 it will be ignored.
4093
4094 @item -gnatb
4095 @cindex @option{-gnatb} (@command{gcc})
4096 Generate brief messages to @file{stderr} even if verbose mode set.
4097
4098 @item -gnatB
4099 @cindex @option{-gnatB} (@command{gcc})
4100 Assume no invalid (bad) values except for 'Valid attribute use
4101 (@pxref{Validity Checking}).
4102
4103 @item -gnatc
4104 @cindex @option{-gnatc} (@command{gcc})
4105 Check syntax and semantics only (no code generation attempted).
4106
4107 @item -gnatC
4108 @cindex @option{-gnatC} (@command{gcc})
4109 Generate CodePeer information (no code generation attempted).
4110 This switch will generate an intermediate representation suitable for
4111 use by CodePeer (@file{.scil} files). This switch is not compatible with
4112 code generation (it will, among other things, disable some switches such
4113 as -gnatn, and enable others such as -gnata).
4114
4115 @item -gnatd
4116 @cindex @option{-gnatd} (@command{gcc})
4117 Specify debug options for the compiler. The string of characters after
4118 the @option{-gnatd} specify the specific debug options. The possible
4119 characters are 0-9, a-z, A-Z, optionally preceded by a dot. See
4120 compiler source file @file{debug.adb} for details of the implemented
4121 debug options. Certain debug options are relevant to applications
4122 programmers, and these are documented at appropriate points in this
4123 users guide.
4124
4125 @ifclear vms
4126 @item -gnatD
4127 @cindex @option{-gnatD[nn]} (@command{gcc})
4128 @end ifclear
4129 @ifset vms
4130 @item /XDEBUG /LXDEBUG=nnn
4131 @end ifset
4132 Create expanded source files for source level debugging. This switch
4133 also suppress generation of cross-reference information
4134 (see @option{-gnatx}).
4135
4136 @item -gnatec=@var{path}
4137 @cindex @option{-gnatec} (@command{gcc})
4138 Specify a configuration pragma file
4139 @ifclear vms
4140 (the equal sign is optional)
4141 @end ifclear
4142 (@pxref{The Configuration Pragmas Files}).
4143
4144 @item ^-gnateD^/DATA_PREPROCESSING=^symbol@r{[}=@var{value}@r{]}
4145 @cindex @option{-gnateD} (@command{gcc})
4146 Defines a symbol, associated with @var{value}, for preprocessing.
4147 (@pxref{Integrated Preprocessing}).
4148
4149 @item -gnatef
4150 @cindex @option{-gnatef} (@command{gcc})
4151 Display full source path name in brief error messages.
4152
4153 @item -gnateG
4154 @cindex @option{-gnateG} (@command{gcc})
4155 Save result of preprocessing in a text file.
4156
4157 @item -gnatem=@var{path}
4158 @cindex @option{-gnatem} (@command{gcc})
4159 Specify a mapping file
4160 @ifclear vms
4161 (the equal sign is optional)
4162 @end ifclear
4163 (@pxref{Units to Sources Mapping Files}).
4164
4165 @item -gnatep=@var{file}
4166 @cindex @option{-gnatep} (@command{gcc})
4167 Specify a preprocessing data file
4168 @ifclear vms
4169 (the equal sign is optional)
4170 @end ifclear
4171 (@pxref{Integrated Preprocessing}).
4172
4173 @item -gnateS
4174 @cindex @option{-gnateS} (@command{gcc})
4175 Generate SCO (Source Coverage Obligation) information in the ALI
4176 file. This information is used by advanced coverage tools. See
4177 unit @file{SCOs} in the compiler sources for details in files
4178 @file{scos.ads} and @file{scos.adb}.
4179
4180 @item -gnatE
4181 @cindex @option{-gnatE} (@command{gcc})
4182 Full dynamic elaboration checks.
4183
4184 @item -gnatf
4185 @cindex @option{-gnatf} (@command{gcc})
4186 Full errors. Multiple errors per line, all undefined references, do not
4187 attempt to suppress cascaded errors.
4188
4189 @item -gnatF
4190 @cindex @option{-gnatF} (@command{gcc})
4191 Externals names are folded to all uppercase.
4192
4193 @item ^-gnatg^/GNAT_INTERNAL^
4194 @cindex @option{^-gnatg^/GNAT_INTERNAL^} (@command{gcc})
4195 Internal GNAT implementation mode. This should not be used for
4196 applications programs, it is intended only for use by the compiler
4197 and its run-time library. For documentation, see the GNAT sources.
4198 Note that @option{^-gnatg^/GNAT_INTERNAL^} implies
4199 @option{^-gnatwae^/WARNINGS=ALL,ERRORS^} and
4200 @option{^-gnatyg^/STYLE_CHECKS=GNAT^}
4201 so that all standard warnings and all standard style options are turned on.
4202 All warnings and style error messages are treated as errors.
4203
4204 @ifclear vms
4205 @item -gnatG=nn
4206 @cindex @option{-gnatG[nn]} (@command{gcc})
4207 @end ifclear
4208 @ifset vms
4209 @item /EXPAND_SOURCE, /LEXPAND_SOURCE=nnn
4210 @end ifset
4211 List generated expanded code in source form.
4212
4213 @item ^-gnath^/HELP^
4214 @cindex @option{^-gnath^/HELP^} (@command{gcc})
4215 Output usage information. The output is written to @file{stdout}.
4216
4217 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
4218 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
4219 Identifier character set
4220 @ifclear vms
4221 (@var{c}=1/2/3/4/8/9/p/f/n/w).
4222 @end ifclear
4223 For details of the possible selections for @var{c},
4224 see @ref{Character Set Control}.
4225
4226 @item ^-gnatI^/IGNORE_REP_CLAUSES^
4227 @cindex @option{^-gnatI^IGNORE_REP_CLAUSES^} (@command{gcc})
4228 Ignore representation clauses. When this switch is used,
4229 representation clauses are treated as comments. This is useful
4230 when initially porting code where you want to ignore rep clause
4231 problems, and also for compiling foreign code (particularly
4232 for use with ASIS). The representation clauses that are ignored
4233 are: enumeration_representation_clause, record_representation_clause,
4234 and attribute_definition_clause for the following attributes:
4235 Address, Alignment, Bit_Order, Component_Size, Machine_Radix,
4236 Object_Size, Size, Small, Stream_Size, and Value_Size.
4237 Note that this option should be used only for compiling -- the
4238 code is likely to malfunction at run time.
4239
4240 @item -gnatjnn
4241 @cindex @option{-gnatjnn} (@command{gcc})
4242 Reformat error messages to fit on nn character lines
4243
4244 @item -gnatk=@var{n}
4245 @cindex @option{-gnatk} (@command{gcc})
4246 Limit file names to @var{n} (1-999) characters ^(@code{k} = krunch)^^.
4247
4248 @item -gnatl
4249 @cindex @option{-gnatl} (@command{gcc})
4250 Output full source listing with embedded error messages.
4251
4252 @item -gnatL
4253 @cindex @option{-gnatL} (@command{gcc})
4254 Used in conjunction with -gnatG or -gnatD to intersperse original
4255 source lines (as comment lines with line numbers) in the expanded
4256 source output.
4257
4258 @item -gnatm=@var{n}
4259 @cindex @option{-gnatm} (@command{gcc})
4260 Limit number of detected error or warning messages to @var{n}
4261 where @var{n} is in the range 1..999999. The default setting if
4262 no switch is given is 9999. If the number of warnings reaches this
4263 limit, then a message is output and further warnings are suppressed,
4264 but the compilation is continued. If the number of error messages
4265 reaches this limit, then a message is output and the compilation
4266 is abandoned. The equal sign here is optional. A value of zero
4267 means that no limit applies.
4268
4269 @item -gnatn
4270 @cindex @option{-gnatn} (@command{gcc})
4271 Activate inlining for subprograms for which
4272 pragma @code{inline} is specified. This inlining is performed
4273 by the GCC back-end.
4274
4275 @item -gnatN
4276 @cindex @option{-gnatN} (@command{gcc})
4277 Activate front end inlining for subprograms for which
4278 pragma @code{Inline} is specified. This inlining is performed
4279 by the front end and will be visible in the
4280 @option{-gnatG} output.
4281
4282 When using a gcc-based back end (in practice this means using any version
4283 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
4284 @option{-gnatN} is deprecated, and the use of @option{-gnatn} is preferred.
4285 Historically front end inlining was more extensive than the gcc back end
4286 inlining, but that is no longer the case.
4287
4288 @item -gnato
4289 @cindex @option{-gnato} (@command{gcc})
4290 Enable numeric overflow checking (which is not normally enabled by
4291 default). Note that division by zero is a separate check that is not
4292 controlled by this switch (division by zero checking is on by default).
4293
4294 @item -gnatp
4295 @cindex @option{-gnatp} (@command{gcc})
4296 Suppress all checks. See @ref{Run-Time Checks} for details.
4297
4298 @item -gnatP
4299 @cindex @option{-gnatP} (@command{gcc})
4300 Enable polling. This is required on some systems (notably Windows NT) to
4301 obtain asynchronous abort and asynchronous transfer of control capability.
4302 @xref{Pragma Polling,,, gnat_rm, GNAT Reference Manual}, for full
4303 details.
4304
4305 @item -gnatq
4306 @cindex @option{-gnatq} (@command{gcc})
4307 Don't quit. Try semantics, even if parse errors.
4308
4309 @item -gnatQ
4310 @cindex @option{-gnatQ} (@command{gcc})
4311 Don't quit. Generate @file{ALI} and tree files even if illegalities.
4312
4313 @item -gnatr
4314 @cindex @option{-gnatr} (@command{gcc})
4315 Treat pragma Restrictions as Restriction_Warnings.
4316
4317 @item ^-gnatR@r{[}0@r{/}1@r{/}2@r{/}3@r{[}s@r{]]}^/REPRESENTATION_INFO^
4318 @cindex @option{-gnatR} (@command{gcc})
4319 Output representation information for declared types and objects.
4320
4321 @item -gnats
4322 @cindex @option{-gnats} (@command{gcc})
4323 Syntax check only.
4324
4325 @item -gnatS
4326 @cindex @option{-gnatS} (@command{gcc})
4327 Print package Standard.
4328
4329 @item -gnatt
4330 @cindex @option{-gnatt} (@command{gcc})
4331 Generate tree output file.
4332
4333 @item ^-gnatT^/TABLE_MULTIPLIER=^@var{nnn}
4334 @cindex @option{^-gnatT^/TABLE_MULTIPLIER^} (@command{gcc})
4335 All compiler tables start at @var{nnn} times usual starting size.
4336
4337 @item -gnatu
4338 @cindex @option{-gnatu} (@command{gcc})
4339 List units for this compilation.
4340
4341 @item -gnatU
4342 @cindex @option{-gnatU} (@command{gcc})
4343 Tag all error messages with the unique string ``error:''
4344
4345 @item -gnatv
4346 @cindex @option{-gnatv} (@command{gcc})
4347 Verbose mode. Full error output with source lines to @file{stdout}.
4348
4349 @item -gnatV
4350 @cindex @option{-gnatV} (@command{gcc})
4351 Control level of validity checking (@pxref{Validity Checking}).
4352
4353 @item ^-gnatw@var{xxx}^/WARNINGS=(@var{option}@r{[},@dots{}@r{]})^
4354 @cindex @option{^-gnatw^/WARNINGS^} (@command{gcc})
4355 Warning mode where
4356 ^@var{xxx} is a string of option letters that^the list of options^ denotes
4357 the exact warnings that
4358 are enabled or disabled (@pxref{Warning Message Control}).
4359
4360 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
4361 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
4362 Wide character encoding method
4363 @ifclear vms
4364 (@var{e}=n/h/u/s/e/8).
4365 @end ifclear
4366 @ifset vms
4367 (@var{e}=@code{BRACKETS, NONE, HEX, UPPER, SHIFT_JIS, EUC, UTF8})
4368 @end ifset
4369
4370 @item -gnatx
4371 @cindex @option{-gnatx} (@command{gcc})
4372 Suppress generation of cross-reference information.
4373
4374 @item ^-gnaty^/STYLE_CHECKS=(option,option@dots{})^
4375 @cindex @option{^-gnaty^/STYLE_CHECKS^} (@command{gcc})
4376 Enable built-in style checks (@pxref{Style Checking}).
4377
4378 @item ^-gnatz^/DISTRIBUTION_STUBS=^@var{m}
4379 @cindex @option{^-gnatz^/DISTRIBUTION_STUBS^} (@command{gcc})
4380 Distribution stub generation and compilation
4381 @ifclear vms
4382 (@var{m}=r/c for receiver/caller stubs).
4383 @end ifclear
4384 @ifset vms
4385 (@var{m}=@code{RECEIVER} or @code{CALLER} to specify the type of stubs
4386 to be generated and compiled).
4387 @end ifset
4388
4389 @item ^-I^/SEARCH=^@var{dir}
4390 @cindex @option{^-I^/SEARCH^} (@command{gcc})
4391 @cindex RTL
4392 Direct GNAT to search the @var{dir} directory for source files needed by
4393 the current compilation
4394 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4395
4396 @item ^-I-^/NOCURRENT_DIRECTORY^
4397 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gcc})
4398 @cindex RTL
4399 Except for the source file named in the command line, do not look for source
4400 files in the directory containing the source file named in the command line
4401 (@pxref{Search Paths and the Run-Time Library (RTL)}).
4402
4403 @ifclear vms
4404 @item -mbig-switch
4405 @cindex @option{-mbig-switch} (@command{gcc})
4406 @cindex @code{case} statement (effect of @option{-mbig-switch} option)
4407 This standard gcc switch causes the compiler to use larger offsets in its
4408 jump table representation for @code{case} statements.
4409 This may result in less efficient code, but is sometimes necessary
4410 (for example on HP-UX targets)
4411 @cindex HP-UX and @option{-mbig-switch} option
4412 in order to compile large and/or nested @code{case} statements.
4413
4414 @item -o @var{file}
4415 @cindex @option{-o} (@command{gcc})
4416 This switch is used in @command{gcc} to redirect the generated object file
4417 and its associated ALI file. Beware of this switch with GNAT, because it may
4418 cause the object file and ALI file to have different names which in turn
4419 may confuse the binder and the linker.
4420 @end ifclear
4421
4422 @item -nostdinc
4423 @cindex @option{-nostdinc} (@command{gcc})
4424 Inhibit the search of the default location for the GNAT Run Time
4425 Library (RTL) source files.
4426
4427 @item -nostdlib
4428 @cindex @option{-nostdlib} (@command{gcc})
4429 Inhibit the search of the default location for the GNAT Run Time
4430 Library (RTL) ALI files.
4431
4432 @ifclear vms
4433 @c @item -O@ovar{n}
4434 @c Expanding @ovar macro inline (explanation in macro def comments)
4435 @item -O@r{[}@var{n}@r{]}
4436 @cindex @option{-O} (@command{gcc})
4437 @var{n} controls the optimization level.
4438
4439 @table @asis
4440 @item n = 0
4441 No optimization, the default setting if no @option{-O} appears
4442
4443 @item n = 1
4444 Normal optimization, the default if you specify @option{-O} without
4445 an operand. A good compromise between code quality and compilation
4446 time.
4447
4448 @item n = 2
4449 Extensive optimization, may improve execution time, possibly at the cost of
4450 substantially increased compilation time.
4451
4452 @item n = 3
4453 Same as @option{-O2}, and also includes inline expansion for small subprograms
4454 in the same unit.
4455
4456 @item n = s
4457 Optimize space usage
4458 @end table
4459
4460 @noindent
4461 See also @ref{Optimization Levels}.
4462 @end ifclear
4463
4464 @ifset vms
4465 @item /NOOPTIMIZE
4466 @cindex @option{/NOOPTIMIZE} (@code{GNAT COMPILE})
4467 Equivalent to @option{/OPTIMIZE=NONE}.
4468 This is the default behavior in the absence of an @option{/OPTIMIZE}
4469 qualifier.
4470
4471 @item /OPTIMIZE@r{[}=(keyword@r{[},@dots{}@r{]})@r{]}
4472 @cindex @option{/OPTIMIZE} (@code{GNAT COMPILE})
4473 Selects the level of optimization for your program. The supported
4474 keywords are as follows:
4475 @table @code
4476 @item ALL
4477 Perform most optimizations, including those that
4478 are expensive.
4479 This is the default if the @option{/OPTIMIZE} qualifier is supplied
4480 without keyword options.
4481
4482 @item NONE
4483 Do not do any optimizations. Same as @code{/NOOPTIMIZE}.
4484
4485 @item SOME
4486 Perform some optimizations, but omit ones that are costly.
4487
4488 @item DEVELOPMENT
4489 Same as @code{SOME}.
4490
4491 @item INLINING
4492 Full optimization as in @option{/OPTIMIZE=ALL}, and also attempts
4493 automatic inlining of small subprograms within a unit
4494
4495 @item UNROLL_LOOPS
4496 Try to unroll loops. This keyword may be specified together with
4497 any keyword above other than @code{NONE}. Loop unrolling
4498 usually, but not always, improves the performance of programs.
4499
4500 @item SPACE
4501 Optimize space usage
4502 @end table
4503
4504 @noindent
4505 See also @ref{Optimization Levels}.
4506 @end ifset
4507
4508 @ifclear vms
4509 @item -pass-exit-codes
4510 @cindex @option{-pass-exit-codes} (@command{gcc})
4511 Catch exit codes from the compiler and use the most meaningful as
4512 exit status.
4513 @end ifclear
4514
4515 @item --RTS=@var{rts-path}
4516 @cindex @option{--RTS} (@command{gcc})
4517 Specifies the default location of the runtime library. Same meaning as the
4518 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
4519
4520 @item ^-S^/ASM^
4521 @cindex @option{^-S^/ASM^} (@command{gcc})
4522 ^Used in place of @option{-c} to^Used to^
4523 cause the assembler source file to be
4524 generated, using @file{^.s^.S^} as the extension,
4525 instead of the object file.
4526 This may be useful if you need to examine the generated assembly code.
4527
4528 @item ^-fverbose-asm^/VERBOSE_ASM^
4529 @cindex @option{^-fverbose-asm^/VERBOSE_ASM^} (@command{gcc})
4530 ^Used in conjunction with @option{-S}^Used in place of @option{/ASM}^
4531 to cause the generated assembly code file to be annotated with variable
4532 names, making it significantly easier to follow.
4533
4534 @item ^-v^/VERBOSE^
4535 @cindex @option{^-v^/VERBOSE^} (@command{gcc})
4536 Show commands generated by the @command{gcc} driver. Normally used only for
4537 debugging purposes or if you need to be sure what version of the
4538 compiler you are executing.
4539
4540 @ifclear vms
4541 @item -V @var{ver}
4542 @cindex @option{-V} (@command{gcc})
4543 Execute @var{ver} version of the compiler. This is the @command{gcc}
4544 version, not the GNAT version.
4545 @end ifclear
4546
4547 @item ^-w^/NO_BACK_END_WARNINGS^
4548 @cindex @option{-w} (@command{gcc})
4549 Turn off warnings generated by the back end of the compiler. Use of
4550 this switch also causes the default for front end warnings to be set
4551 to suppress (as though @option{-gnatws} had appeared at the start of
4552 the options).
4553
4554 @end table
4555
4556 @ifclear vms
4557 @c Combining qualifiers does not work on VMS
4558 You may combine a sequence of GNAT switches into a single switch. For
4559 example, the combined switch
4560
4561 @cindex Combining GNAT switches
4562 @smallexample
4563 -gnatofi3
4564 @end smallexample
4565
4566 @noindent
4567 is equivalent to specifying the following sequence of switches:
4568
4569 @smallexample
4570 -gnato -gnatf -gnati3
4571 @end smallexample
4572 @end ifclear
4573
4574 @noindent
4575 The following restrictions apply to the combination of switches
4576 in this manner:
4577
4578 @itemize @bullet
4579 @item
4580 The switch @option{-gnatc} if combined with other switches must come
4581 first in the string.
4582
4583 @item
4584 The switch @option{-gnats} if combined with other switches must come
4585 first in the string.
4586
4587 @item
4588 The switches
4589 ^^@option{/DISTRIBUTION_STUBS=},^
4590 @option{-gnatzc} and @option{-gnatzr} may not be combined with any other
4591 switches, and only one of them may appear in the command line.
4592
4593 @ifclear vms
4594 @item
4595 Once a ``y'' appears in the string (that is a use of the @option{-gnaty}
4596 switch), then all further characters in the switch are interpreted
4597 as style modifiers (see description of @option{-gnaty}).
4598
4599 @item
4600 Once a ``d'' appears in the string (that is a use of the @option{-gnatd}
4601 switch), then all further characters in the switch are interpreted
4602 as debug flags (see description of @option{-gnatd}).
4603
4604 @item
4605 Once a ``w'' appears in the string (that is a use of the @option{-gnatw}
4606 switch), then all further characters in the switch are interpreted
4607 as warning mode modifiers (see description of @option{-gnatw}).
4608
4609 @item
4610 Once a ``V'' appears in the string (that is a use of the @option{-gnatV}
4611 switch), then all further characters in the switch are interpreted
4612 as validity checking options (@pxref{Validity Checking}).
4613
4614 @item
4615 Option ``em'', ``ec'', ``ep'', ``l='' and ``R'' must be the last options in
4616 a combined list of options.
4617 @end ifclear
4618 @end itemize
4619
4620 @node Output and Error Message Control
4621 @subsection Output and Error Message Control
4622 @findex stderr
4623
4624 @noindent
4625 The standard default format for error messages is called ``brief format''.
4626 Brief format messages are written to @file{stderr} (the standard error
4627 file) and have the following form:
4628
4629 @smallexample
4630 e.adb:3:04: Incorrect spelling of keyword "function"
4631 e.adb:4:20: ";" should be "is"
4632 @end smallexample
4633
4634 @noindent
4635 The first integer after the file name is the line number in the file,
4636 and the second integer is the column number within the line.
4637 @ifclear vms
4638 @code{GPS} can parse the error messages
4639 and point to the referenced character.
4640 @end ifclear
4641 The following switches provide control over the error message
4642 format:
4643
4644 @table @option
4645 @c !sort!
4646 @item -gnatv
4647 @cindex @option{-gnatv} (@command{gcc})
4648 @findex stdout
4649 @ifclear vms
4650 The v stands for verbose.
4651 @end ifclear
4652 The effect of this setting is to write long-format error
4653 messages to @file{stdout} (the standard output file.
4654 The same program compiled with the
4655 @option{-gnatv} switch would generate:
4656
4657 @smallexample
4658 @cartouche
4659 3. funcion X (Q : Integer)
4660 |
4661 >>> Incorrect spelling of keyword "function"
4662 4. return Integer;
4663 |
4664 >>> ";" should be "is"
4665 @end cartouche
4666 @end smallexample
4667
4668 @noindent
4669 The vertical bar indicates the location of the error, and the @samp{>>>}
4670 prefix can be used to search for error messages. When this switch is
4671 used the only source lines output are those with errors.
4672
4673 @item -gnatl
4674 @cindex @option{-gnatl} (@command{gcc})
4675 @ifclear vms
4676 The @code{l} stands for list.
4677 @end ifclear
4678 This switch causes a full listing of
4679 the file to be generated. In the case where a body is
4680 compiled, the corresponding spec is also listed, along
4681 with any subunits. Typical output from compiling a package
4682 body @file{p.adb} might look like:
4683
4684 @smallexample @c ada
4685 @cartouche
4686 Compiling: p.adb
4687
4688 1. package body p is
4689 2. procedure a;
4690 3. procedure a is separate;
4691 4. begin
4692 5. null
4693 |
4694 >>> missing ";"
4695
4696 6. end;
4697
4698 Compiling: p.ads
4699
4700 1. package p is
4701 2. pragma Elaborate_Body
4702 |
4703 >>> missing ";"
4704
4705 3. end p;
4706
4707 Compiling: p-a.adb
4708
4709 1. separate p
4710 |
4711 >>> missing "("
4712
4713 2. procedure a is
4714 3. begin
4715 4. null
4716 |
4717 >>> missing ";"
4718
4719 5. end;
4720 @end cartouche
4721 @end smallexample
4722
4723 @noindent
4724 @findex stderr
4725 When you specify the @option{-gnatv} or @option{-gnatl} switches and
4726 standard output is redirected, a brief summary is written to
4727 @file{stderr} (standard error) giving the number of error messages and
4728 warning messages generated.
4729
4730 @item -^gnatl^OUTPUT_FILE^=file
4731 @cindex @option{^-gnatl^OUTPUT_FILE^=fname} (@command{gcc})
4732 This has the same effect as @option{-gnatl} except that the output is
4733 written to a file instead of to standard output. If the given name
4734 @file{fname} does not start with a period, then it is the full name
4735 of the file to be written. If @file{fname} is an extension, it is
4736 appended to the name of the file being compiled. For example, if
4737 file @file{xyz.adb} is compiled with @option{^-gnatl^OUTPUT_FILE^=.lst},
4738 then the output is written to file ^xyz.adb.lst^xyz.adb_lst^.
4739
4740 @item -gnatU
4741 @cindex @option{-gnatU} (@command{gcc})
4742 This switch forces all error messages to be preceded by the unique
4743 string ``error:''. This means that error messages take a few more
4744 characters in space, but allows easy searching for and identification
4745 of error messages.
4746
4747 @item -gnatb
4748 @cindex @option{-gnatb} (@command{gcc})
4749 @ifclear vms
4750 The @code{b} stands for brief.
4751 @end ifclear
4752 This switch causes GNAT to generate the
4753 brief format error messages to @file{stderr} (the standard error
4754 file) as well as the verbose
4755 format message or full listing (which as usual is written to
4756 @file{stdout} (the standard output file).
4757
4758 @item -gnatm=@var{n}
4759 @cindex @option{-gnatm} (@command{gcc})
4760 @ifclear vms
4761 The @code{m} stands for maximum.
4762 @end ifclear
4763 @var{n} is a decimal integer in the
4764 range of 1 to 999999 and limits the number of error or warning
4765 messages to be generated. For example, using
4766 @option{-gnatm2} might yield
4767
4768 @smallexample
4769 e.adb:3:04: Incorrect spelling of keyword "function"
4770 e.adb:5:35: missing ".."
4771 fatal error: maximum number of errors detected
4772 compilation abandoned
4773 @end smallexample
4774
4775 @noindent
4776 The default setting if
4777 no switch is given is 9999. If the number of warnings reaches this
4778 limit, then a message is output and further warnings are suppressed,
4779 but the compilation is continued. If the number of error messages
4780 reaches this limit, then a message is output and the compilation
4781 is abandoned. A value of zero means that no limit applies.
4782
4783 @noindent
4784 Note that the equal sign is optional, so the switches
4785 @option{-gnatm2} and @option{-gnatm=2} are equivalent.
4786
4787 @item -gnatf
4788 @cindex @option{-gnatf} (@command{gcc})
4789 @cindex Error messages, suppressing
4790 @ifclear vms
4791 The @code{f} stands for full.
4792 @end ifclear
4793 Normally, the compiler suppresses error messages that are likely to be
4794 redundant. This switch causes all error
4795 messages to be generated. In particular, in the case of
4796 references to undefined variables. If a given variable is referenced
4797 several times, the normal format of messages is
4798 @smallexample
4799 e.adb:7:07: "V" is undefined (more references follow)
4800 @end smallexample
4801
4802 @noindent
4803 where the parenthetical comment warns that there are additional
4804 references to the variable @code{V}. Compiling the same program with the
4805 @option{-gnatf} switch yields
4806
4807 @smallexample
4808 e.adb:7:07: "V" is undefined
4809 e.adb:8:07: "V" is undefined
4810 e.adb:8:12: "V" is undefined
4811 e.adb:8:16: "V" is undefined
4812 e.adb:9:07: "V" is undefined
4813 e.adb:9:12: "V" is undefined
4814 @end smallexample
4815
4816 @noindent
4817 The @option{-gnatf} switch also generates additional information for
4818 some error messages. Some examples are:
4819
4820 @itemize @bullet
4821 @item
4822 Details on possibly non-portable unchecked conversion
4823 @item
4824 List possible interpretations for ambiguous calls
4825 @item
4826 Additional details on incorrect parameters
4827 @end itemize
4828
4829 @item -gnatjnn
4830 @cindex @option{-gnatjnn} (@command{gcc})
4831 In normal operation mode (or if @option{-gnatj0} is used, then error messages
4832 with continuation lines are treated as though the continuation lines were
4833 separate messages (and so a warning with two continuation lines counts as
4834 three warnings, and is listed as three separate messages).
4835
4836 If the @option{-gnatjnn} switch is used with a positive value for nn, then
4837 messages are output in a different manner. A message and all its continuation
4838 lines are treated as a unit, and count as only one warning or message in the
4839 statistics totals. Furthermore, the message is reformatted so that no line
4840 is longer than nn characters.
4841
4842 @item -gnatq
4843 @cindex @option{-gnatq} (@command{gcc})
4844 @ifclear vms
4845 The @code{q} stands for quit (really ``don't quit'').
4846 @end ifclear
4847 In normal operation mode, the compiler first parses the program and
4848 determines if there are any syntax errors. If there are, appropriate
4849 error messages are generated and compilation is immediately terminated.
4850 This switch tells
4851 GNAT to continue with semantic analysis even if syntax errors have been
4852 found. This may enable the detection of more errors in a single run. On
4853 the other hand, the semantic analyzer is more likely to encounter some
4854 internal fatal error when given a syntactically invalid tree.
4855
4856 @item -gnatQ
4857 @cindex @option{-gnatQ} (@command{gcc})
4858 In normal operation mode, the @file{ALI} file is not generated if any
4859 illegalities are detected in the program. The use of @option{-gnatQ} forces
4860 generation of the @file{ALI} file. This file is marked as being in
4861 error, so it cannot be used for binding purposes, but it does contain
4862 reasonably complete cross-reference information, and thus may be useful
4863 for use by tools (e.g., semantic browsing tools or integrated development
4864 environments) that are driven from the @file{ALI} file. This switch
4865 implies @option{-gnatq}, since the semantic phase must be run to get a
4866 meaningful ALI file.
4867
4868 In addition, if @option{-gnatt} is also specified, then the tree file is
4869 generated even if there are illegalities. It may be useful in this case
4870 to also specify @option{-gnatq} to ensure that full semantic processing
4871 occurs. The resulting tree file can be processed by ASIS, for the purpose
4872 of providing partial information about illegal units, but if the error
4873 causes the tree to be badly malformed, then ASIS may crash during the
4874 analysis.
4875
4876 When @option{-gnatQ} is used and the generated @file{ALI} file is marked as
4877 being in error, @command{gnatmake} will attempt to recompile the source when it
4878 finds such an @file{ALI} file, including with switch @option{-gnatc}.
4879
4880 Note that @option{-gnatQ} has no effect if @option{-gnats} is specified,
4881 since ALI files are never generated if @option{-gnats} is set.
4882
4883 @end table
4884
4885 @node Warning Message Control
4886 @subsection Warning Message Control
4887 @cindex Warning messages
4888 @noindent
4889 In addition to error messages, which correspond to illegalities as defined
4890 in the Ada Reference Manual, the compiler detects two kinds of warning
4891 situations.
4892
4893 First, the compiler considers some constructs suspicious and generates a
4894 warning message to alert you to a possible error. Second, if the
4895 compiler detects a situation that is sure to raise an exception at
4896 run time, it generates a warning message. The following shows an example
4897 of warning messages:
4898 @smallexample
4899 e.adb:4:24: warning: creation of object may raise Storage_Error
4900 e.adb:10:17: warning: static value out of range
4901 e.adb:10:17: warning: "Constraint_Error" will be raised at run time
4902 @end smallexample
4903
4904 @noindent
4905 GNAT considers a large number of situations as appropriate
4906 for the generation of warning messages. As always, warnings are not
4907 definite indications of errors. For example, if you do an out-of-range
4908 assignment with the deliberate intention of raising a
4909 @code{Constraint_Error} exception, then the warning that may be
4910 issued does not indicate an error. Some of the situations for which GNAT
4911 issues warnings (at least some of the time) are given in the following
4912 list. This list is not complete, and new warnings are often added to
4913 subsequent versions of GNAT. The list is intended to give a general idea
4914 of the kinds of warnings that are generated.
4915
4916 @itemize @bullet
4917 @item
4918 Possible infinitely recursive calls
4919
4920 @item
4921 Out-of-range values being assigned
4922
4923 @item
4924 Possible order of elaboration problems
4925
4926 @item
4927 Assertions (pragma Assert) that are sure to fail
4928
4929 @item
4930 Unreachable code
4931
4932 @item
4933 Address clauses with possibly unaligned values, or where an attempt is
4934 made to overlay a smaller variable with a larger one.
4935
4936 @item
4937 Fixed-point type declarations with a null range
4938
4939 @item
4940 Direct_IO or Sequential_IO instantiated with a type that has access values
4941
4942 @item
4943 Variables that are never assigned a value
4944
4945 @item
4946 Variables that are referenced before being initialized
4947
4948 @item
4949 Task entries with no corresponding @code{accept} statement
4950
4951 @item
4952 Duplicate accepts for the same task entry in a @code{select}
4953
4954 @item
4955 Objects that take too much storage
4956
4957 @item
4958 Unchecked conversion between types of differing sizes
4959
4960 @item
4961 Missing @code{return} statement along some execution path in a function
4962
4963 @item
4964 Incorrect (unrecognized) pragmas
4965
4966 @item
4967 Incorrect external names
4968
4969 @item
4970 Allocation from empty storage pool
4971
4972 @item
4973 Potentially blocking operation in protected type
4974
4975 @item
4976 Suspicious parenthesization of expressions
4977
4978 @item
4979 Mismatching bounds in an aggregate
4980
4981 @item
4982 Attempt to return local value by reference
4983
4984 @item
4985 Premature instantiation of a generic body
4986
4987 @item
4988 Attempt to pack aliased components
4989
4990 @item
4991 Out of bounds array subscripts
4992
4993 @item
4994 Wrong length on string assignment
4995
4996 @item
4997 Violations of style rules if style checking is enabled
4998
4999 @item
5000 Unused @code{with} clauses
5001
5002 @item
5003 @code{Bit_Order} usage that does not have any effect
5004
5005 @item
5006 @code{Standard.Duration} used to resolve universal fixed expression
5007
5008 @item
5009 Dereference of possibly null value
5010
5011 @item
5012 Declaration that is likely to cause storage error
5013
5014 @item
5015 Internal GNAT unit @code{with}'ed by application unit
5016
5017 @item
5018 Values known to be out of range at compile time
5019
5020 @item
5021 Unreferenced labels and variables
5022
5023 @item
5024 Address overlays that could clobber memory
5025
5026 @item
5027 Unexpected initialization when address clause present
5028
5029 @item
5030 Bad alignment for address clause
5031
5032 @item
5033 Useless type conversions
5034
5035 @item
5036 Redundant assignment statements and other redundant constructs
5037
5038 @item
5039 Useless exception handlers
5040
5041 @item
5042 Accidental hiding of name by child unit
5043
5044 @item
5045 Access before elaboration detected at compile time
5046
5047 @item
5048 A range in a @code{for} loop that is known to be null or might be null
5049
5050 @end itemize
5051
5052 @noindent
5053 The following section lists compiler switches that are available
5054 to control the handling of warning messages. It is also possible
5055 to exercise much finer control over what warnings are issued and
5056 suppressed using the GNAT pragma Warnings, @xref{Pragma Warnings,,,
5057 gnat_rm, GNAT Reference manual}.
5058
5059 @table @option
5060 @c !sort!
5061 @item -gnatwa
5062 @emph{Activate all optional errors.}
5063 @cindex @option{-gnatwa} (@command{gcc})
5064 This switch activates most optional warning messages, see remaining list
5065 in this section for details on optional warning messages that can be
5066 individually controlled. The warnings that are not turned on by this
5067 switch are
5068 @option{-gnatwd} (implicit dereferencing),
5069 @option{-gnatwh} (hiding),
5070 @option{-gnatwl} (elaboration warnings),
5071 @option{-gnatw.o} (warn on values set by out parameters ignored)
5072 and @option{-gnatwt} (tracking of deleted conditional code).
5073 All other optional warnings are turned on.
5074
5075 @item -gnatwA
5076 @emph{Suppress all optional errors.}
5077 @cindex @option{-gnatwA} (@command{gcc})
5078 This switch suppresses all optional warning messages, see remaining list
5079 in this section for details on optional warning messages that can be
5080 individually controlled.
5081
5082 @item -gnatw.a
5083 @emph{Activate warnings on failing assertions.}
5084 @cindex @option{-gnatw.a} (@command{gcc})
5085 @cindex Assert failures
5086 This switch activates warnings for assertions where the compiler can tell at
5087 compile time that the assertion will fail. Note that this warning is given
5088 even if assertions are disabled. The default is that such warnings are
5089 generated.
5090
5091 @item -gnatw.A
5092 @emph{Suppress warnings on failing assertions.}
5093 @cindex @option{-gnatw.A} (@command{gcc})
5094 @cindex Assert failures
5095 This switch suppresses warnings for assertions where the compiler can tell at
5096 compile time that the assertion will fail.
5097
5098 @item -gnatwb
5099 @emph{Activate warnings on bad fixed values.}
5100 @cindex @option{-gnatwb} (@command{gcc})
5101 @cindex Bad fixed values
5102 @cindex Fixed-point Small value
5103 @cindex Small value
5104 This switch activates warnings for static fixed-point expressions whose
5105 value is not an exact multiple of Small. Such values are implementation
5106 dependent, since an implementation is free to choose either of the multiples
5107 that surround the value. GNAT always chooses the closer one, but this is not
5108 required behavior, and it is better to specify a value that is an exact
5109 multiple, ensuring predictable execution. The default is that such warnings
5110 are not generated.
5111
5112 @item -gnatwB
5113 @emph{Suppress warnings on bad fixed values.}
5114 @cindex @option{-gnatwB} (@command{gcc})
5115 This switch suppresses warnings for static fixed-point expressions whose
5116 value is not an exact multiple of Small.
5117
5118 @item -gnatw.b
5119 @emph{Activate warnings on biased representation.}
5120 @cindex @option{-gnatw.b} (@command{gcc})
5121 @cindex Biased representation
5122 This switch activates warnings when a size clause, value size clause, component
5123 clause, or component size clause forces the use of biased representation for an
5124 integer type (e.g. representing a range of 10..11 in a single bit by using 0/1
5125 to represent 10/11). The default is that such warnings are generated.
5126
5127 @item -gnatw.B
5128 @emph{Suppress warnings on biased representation.}
5129 @cindex @option{-gnatwB} (@command{gcc})
5130 This switch suppresses warnings for representation clauses that force the use
5131 of biased representation.
5132
5133 @item -gnatwc
5134 @emph{Activate warnings on conditionals.}
5135 @cindex @option{-gnatwc} (@command{gcc})
5136 @cindex Conditionals, constant
5137 This switch activates warnings for conditional expressions used in
5138 tests that are known to be True or False at compile time. The default
5139 is that such warnings are not generated.
5140 Note that this warning does
5141 not get issued for the use of boolean variables or constants whose
5142 values are known at compile time, since this is a standard technique
5143 for conditional compilation in Ada, and this would generate too many
5144 false positive warnings.
5145
5146 This warning option also activates a special test for comparisons using
5147 the operators ``>='' and`` <=''.
5148 If the compiler can tell that only the equality condition is possible,
5149 then it will warn that the ``>'' or ``<'' part of the test
5150 is useless and that the operator could be replaced by ``=''.
5151 An example would be comparing a @code{Natural} variable <= 0.
5152
5153 This warning option also generates warnings if
5154 one or both tests is optimized away in a membership test for integer
5155 values if the result can be determined at compile time. Range tests on
5156 enumeration types are not included, since it is common for such tests
5157 to include an end point.
5158
5159 This warning can also be turned on using @option{-gnatwa}.
5160
5161 @item -gnatwC
5162 @emph{Suppress warnings on conditionals.}
5163 @cindex @option{-gnatwC} (@command{gcc})
5164 This switch suppresses warnings for conditional expressions used in
5165 tests that are known to be True or False at compile time.
5166
5167 @item -gnatw.c
5168 @emph{Activate warnings on missing component clauses.}
5169 @cindex @option{-gnatw.c} (@command{gcc})
5170 @cindex Component clause, missing
5171 This switch activates warnings for record components where a record
5172 representation clause is present and has component clauses for the
5173 majority, but not all, of the components. A warning is given for each
5174 component for which no component clause is present.
5175
5176 This warning can also be turned on using @option{-gnatwa}.
5177
5178 @item -gnatw.C
5179 @emph{Suppress warnings on missing component clauses.}
5180 @cindex @option{-gnatwC} (@command{gcc})
5181 This switch suppresses warnings for record components that are
5182 missing a component clause in the situation described above.
5183
5184 @item -gnatwd
5185 @emph{Activate warnings on implicit dereferencing.}
5186 @cindex @option{-gnatwd} (@command{gcc})
5187 If this switch is set, then the use of a prefix of an access type
5188 in an indexed component, slice, or selected component without an
5189 explicit @code{.all} will generate a warning. With this warning
5190 enabled, access checks occur only at points where an explicit
5191 @code{.all} appears in the source code (assuming no warnings are
5192 generated as a result of this switch). The default is that such
5193 warnings are not generated.
5194 Note that @option{-gnatwa} does not affect the setting of
5195 this warning option.
5196
5197 @item -gnatwD
5198 @emph{Suppress warnings on implicit dereferencing.}
5199 @cindex @option{-gnatwD} (@command{gcc})
5200 @cindex Implicit dereferencing
5201 @cindex Dereferencing, implicit
5202 This switch suppresses warnings for implicit dereferences in
5203 indexed components, slices, and selected components.
5204
5205 @item -gnatwe
5206 @emph{Treat warnings as errors.}
5207 @cindex @option{-gnatwe} (@command{gcc})
5208 @cindex Warnings, treat as error
5209 This switch causes warning messages to be treated as errors.
5210 The warning string still appears, but the warning messages are counted
5211 as errors, and prevent the generation of an object file.
5212
5213 @item -gnatw.e
5214 @emph{Activate every optional warning}
5215 @cindex @option{-gnatw.e} (@command{gcc})
5216 @cindex Warnings, activate every optional warning
5217 This switch activates all optional warnings, including those which
5218 are not activated by @code{-gnatwa}.
5219
5220 @item -gnatwf
5221 @emph{Activate warnings on unreferenced formals.}
5222 @cindex @option{-gnatwf} (@command{gcc})
5223 @cindex Formals, unreferenced
5224 This switch causes a warning to be generated if a formal parameter
5225 is not referenced in the body of the subprogram. This warning can
5226 also be turned on using @option{-gnatwa} or @option{-gnatwu}. The
5227 default is that these warnings are not generated.
5228
5229 @item -gnatwF
5230 @emph{Suppress warnings on unreferenced formals.}
5231 @cindex @option{-gnatwF} (@command{gcc})
5232 This switch suppresses warnings for unreferenced formal
5233 parameters. Note that the
5234 combination @option{-gnatwu} followed by @option{-gnatwF} has the
5235 effect of warning on unreferenced entities other than subprogram
5236 formals.
5237
5238 @item -gnatwg
5239 @emph{Activate warnings on unrecognized pragmas.}
5240 @cindex @option{-gnatwg} (@command{gcc})
5241 @cindex Pragmas, unrecognized
5242 This switch causes a warning to be generated if an unrecognized
5243 pragma is encountered. Apart from issuing this warning, the
5244 pragma is ignored and has no effect. This warning can
5245 also be turned on using @option{-gnatwa}. The default
5246 is that such warnings are issued (satisfying the Ada Reference
5247 Manual requirement that such warnings appear).
5248
5249 @item -gnatwG
5250 @emph{Suppress warnings on unrecognized pragmas.}
5251 @cindex @option{-gnatwG} (@command{gcc})
5252 This switch suppresses warnings for unrecognized pragmas.
5253
5254 @item -gnatwh
5255 @emph{Activate warnings on hiding.}
5256 @cindex @option{-gnatwh} (@command{gcc})
5257 @cindex Hiding of Declarations
5258 This switch activates warnings on hiding declarations.
5259 A declaration is considered hiding
5260 if it is for a non-overloadable entity, and it declares an entity with the
5261 same name as some other entity that is directly or use-visible. The default
5262 is that such warnings are not generated.
5263 Note that @option{-gnatwa} does not affect the setting of this warning option.
5264
5265 @item -gnatwH
5266 @emph{Suppress warnings on hiding.}
5267 @cindex @option{-gnatwH} (@command{gcc})
5268 This switch suppresses warnings on hiding declarations.
5269
5270 @item -gnatwi
5271 @emph{Activate warnings on implementation units.}
5272 @cindex @option{-gnatwi} (@command{gcc})
5273 This switch activates warnings for a @code{with} of an internal GNAT
5274 implementation unit, defined as any unit from the @code{Ada},
5275 @code{Interfaces}, @code{GNAT},
5276 ^^@code{DEC},^ or @code{System}
5277 hierarchies that is not
5278 documented in either the Ada Reference Manual or the GNAT
5279 Programmer's Reference Manual. Such units are intended only
5280 for internal implementation purposes and should not be @code{with}'ed
5281 by user programs. The default is that such warnings are generated
5282 This warning can also be turned on using @option{-gnatwa}.
5283
5284 @item -gnatwI
5285 @emph{Disable warnings on implementation units.}
5286 @cindex @option{-gnatwI} (@command{gcc})
5287 This switch disables warnings for a @code{with} of an internal GNAT
5288 implementation unit.
5289
5290 @item -gnatw.i
5291 @emph{Activate warnings on overlapping actuals.}
5292 @cindex @option{-gnatw.i} (@command{gcc})
5293 This switch enables a warning on statically detectable overlapping actuals in
5294 a subprogram call, when one of the actuals is an in-out parameter, and the
5295 types of the actuals are not by-copy types. The warning is off by default,
5296 and is not included under -gnatwa.
5297
5298 @item -gnatw.I
5299 @emph{Disable warnings on overlapping actuals.}
5300 @cindex @option{-gnatw.I} (@command{gcc})
5301 This switch disables warnings on overlapping actuals in a call..
5302
5303 @item -gnatwj
5304 @emph{Activate warnings on obsolescent features (Annex J).}
5305 @cindex @option{-gnatwj} (@command{gcc})
5306 @cindex Features, obsolescent
5307 @cindex Obsolescent features
5308 If this warning option is activated, then warnings are generated for
5309 calls to subprograms marked with @code{pragma Obsolescent} and
5310 for use of features in Annex J of the Ada Reference Manual. In the
5311 case of Annex J, not all features are flagged. In particular use
5312 of the renamed packages (like @code{Text_IO}) and use of package
5313 @code{ASCII} are not flagged, since these are very common and
5314 would generate many annoying positive warnings. The default is that
5315 such warnings are not generated. This warning is also turned on by
5316 the use of @option{-gnatwa}.
5317
5318 In addition to the above cases, warnings are also generated for
5319 GNAT features that have been provided in past versions but which
5320 have been superseded (typically by features in the new Ada standard).
5321 For example, @code{pragma Ravenscar} will be flagged since its
5322 function is replaced by @code{pragma Profile(Ravenscar)}.
5323
5324 Note that this warning option functions differently from the
5325 restriction @code{No_Obsolescent_Features} in two respects.
5326 First, the restriction applies only to annex J features.
5327 Second, the restriction does flag uses of package @code{ASCII}.
5328
5329 @item -gnatwJ
5330 @emph{Suppress warnings on obsolescent features (Annex J).}
5331 @cindex @option{-gnatwJ} (@command{gcc})
5332 This switch disables warnings on use of obsolescent features.
5333
5334 @item -gnatwk
5335 @emph{Activate warnings on variables that could be constants.}
5336 @cindex @option{-gnatwk} (@command{gcc})
5337 This switch activates warnings for variables that are initialized but
5338 never modified, and then could be declared constants. The default is that
5339 such warnings are not given.
5340 This warning can also be turned on using @option{-gnatwa}.
5341
5342 @item -gnatwK
5343 @emph{Suppress warnings on variables that could be constants.}
5344 @cindex @option{-gnatwK} (@command{gcc})
5345 This switch disables warnings on variables that could be declared constants.
5346
5347 @item -gnatwl
5348 @emph{Activate warnings for elaboration pragmas.}
5349 @cindex @option{-gnatwl} (@command{gcc})
5350 @cindex Elaboration, warnings
5351 This switch activates warnings on missing
5352 @code{Elaborate_All} and @code{Elaborate} pragmas.
5353 See the section in this guide on elaboration checking for details on
5354 when such pragmas should be used. In dynamic elaboration mode, this switch
5355 generations warnings about the need to add elaboration pragmas. Note however,
5356 that if you blindly follow these warnings, and add @code{Elaborate_All}
5357 warnings wherever they are recommended, you basically end up with the
5358 equivalent of the static elaboration model, which may not be what you want for
5359 legacy code for which the static model does not work.
5360
5361 For the static model, the messages generated are labeled "info:" (for
5362 information messages). They are not warnings to add elaboration pragmas,
5363 merely informational messages showing what implicit elaboration pragmas
5364 have been added, for use in analyzing elaboration circularity problems.
5365
5366 Warnings are also generated if you
5367 are using the static mode of elaboration, and a @code{pragma Elaborate}
5368 is encountered. The default is that such warnings
5369 are not generated.
5370 This warning is not automatically turned on by the use of @option{-gnatwa}.
5371
5372 @item -gnatwL
5373 @emph{Suppress warnings for elaboration pragmas.}
5374 @cindex @option{-gnatwL} (@command{gcc})
5375 This switch suppresses warnings on missing Elaborate and Elaborate_All pragmas.
5376 See the section in this guide on elaboration checking for details on
5377 when such pragmas should be used.
5378
5379 @item -gnatwm
5380 @emph{Activate warnings on modified but unreferenced variables.}
5381 @cindex @option{-gnatwm} (@command{gcc})
5382 This switch activates warnings for variables that are assigned (using
5383 an initialization value or with one or more assignment statements) but
5384 whose value is never read. The warning is suppressed for volatile
5385 variables and also for variables that are renamings of other variables
5386 or for which an address clause is given.
5387 This warning can also be turned on using @option{-gnatwa}.
5388 The default is that these warnings are not given.
5389
5390 @item -gnatwM
5391 @emph{Disable warnings on modified but unreferenced variables.}
5392 @cindex @option{-gnatwM} (@command{gcc})
5393 This switch disables warnings for variables that are assigned or
5394 initialized, but never read.
5395
5396 @item -gnatw.m
5397 @emph{Activate warnings on suspicious modulus values.}
5398 @cindex @option{-gnatw.m} (@command{gcc})
5399 This switch activates warnings for modulus values that seem suspicious.
5400 The cases caught are where the size is the same as the modulus (e.g.
5401 a modulus of 7 with a size of 7 bits), and modulus values of 32 or 64
5402 with no size clause. The guess in both cases is that 2**x was intended
5403 rather than x. The default is that these warnings are given.
5404
5405 @item -gnatw.M
5406 @emph{Disable warnings on suspicious modulus values.}
5407 @cindex @option{-gnatw.M} (@command{gcc})
5408 This switch disables warnings for suspicious modulus values.
5409
5410 @item -gnatwn
5411 @emph{Set normal warnings mode.}
5412 @cindex @option{-gnatwn} (@command{gcc})
5413 This switch sets normal warning mode, in which enabled warnings are
5414 issued and treated as warnings rather than errors. This is the default
5415 mode. the switch @option{-gnatwn} can be used to cancel the effect of
5416 an explicit @option{-gnatws} or
5417 @option{-gnatwe}. It also cancels the effect of the
5418 implicit @option{-gnatwe} that is activated by the
5419 use of @option{-gnatg}.
5420
5421 @item -gnatwo
5422 @emph{Activate warnings on address clause overlays.}
5423 @cindex @option{-gnatwo} (@command{gcc})
5424 @cindex Address Clauses, warnings
5425 This switch activates warnings for possibly unintended initialization
5426 effects of defining address clauses that cause one variable to overlap
5427 another. The default is that such warnings are generated.
5428 This warning can also be turned on using @option{-gnatwa}.
5429
5430 @item -gnatwO
5431 @emph{Suppress warnings on address clause overlays.}
5432 @cindex @option{-gnatwO} (@command{gcc})
5433 This switch suppresses warnings on possibly unintended initialization
5434 effects of defining address clauses that cause one variable to overlap
5435 another.
5436
5437 @item -gnatw.o
5438 @emph{Activate warnings on modified but unreferenced out parameters.}
5439 @cindex @option{-gnatw.o} (@command{gcc})
5440 This switch activates warnings for variables that are modified by using
5441 them as actuals for a call to a procedure with an out mode formal, where
5442 the resulting assigned value is never read. It is applicable in the case
5443 where there is more than one out mode formal. If there is only one out
5444 mode formal, the warning is issued by default (controlled by -gnatwu).
5445 The warning is suppressed for volatile
5446 variables and also for variables that are renamings of other variables
5447 or for which an address clause is given.
5448 The default is that these warnings are not given. Note that this warning
5449 is not included in -gnatwa, it must be activated explicitly.
5450
5451 @item -gnatw.O
5452 @emph{Disable warnings on modified but unreferenced out parameters.}
5453 @cindex @option{-gnatw.O} (@command{gcc})
5454 This switch suppresses warnings for variables that are modified by using
5455 them as actuals for a call to a procedure with an out mode formal, where
5456 the resulting assigned value is never read.
5457
5458 @item -gnatwp
5459 @emph{Activate warnings on ineffective pragma Inlines.}
5460 @cindex @option{-gnatwp} (@command{gcc})
5461 @cindex Inlining, warnings
5462 This switch activates warnings for failure of front end inlining
5463 (activated by @option{-gnatN}) to inline a particular call. There are
5464 many reasons for not being able to inline a call, including most
5465 commonly that the call is too complex to inline. The default is
5466 that such warnings are not given.
5467 This warning can also be turned on using @option{-gnatwa}.
5468 Warnings on ineffective inlining by the gcc back-end can be activated
5469 separately, using the gcc switch -Winline.
5470
5471 @item -gnatwP
5472 @emph{Suppress warnings on ineffective pragma Inlines.}
5473 @cindex @option{-gnatwP} (@command{gcc})
5474 This switch suppresses warnings on ineffective pragma Inlines. If the
5475 inlining mechanism cannot inline a call, it will simply ignore the
5476 request silently.
5477
5478 @item -gnatw.p
5479 @emph{Activate warnings on parameter ordering.}
5480 @cindex @option{-gnatw.p} (@command{gcc})
5481 @cindex Parameter order, warnings
5482 This switch activates warnings for cases of suspicious parameter
5483 ordering when the list of arguments are all simple identifiers that
5484 match the names of the formals, but are in a different order. The
5485 warning is suppressed if any use of named parameter notation is used,
5486 so this is the appropriate way to suppress a false positive (and
5487 serves to emphasize that the "misordering" is deliberate). The
5488 default is
5489 that such warnings are not given.
5490 This warning can also be turned on using @option{-gnatwa}.
5491
5492 @item -gnatw.P
5493 @emph{Suppress warnings on parameter ordering.}
5494 @cindex @option{-gnatw.P} (@command{gcc})
5495 This switch suppresses warnings on cases of suspicious parameter
5496 ordering.
5497
5498 @item -gnatwq
5499 @emph{Activate warnings on questionable missing parentheses.}
5500 @cindex @option{-gnatwq} (@command{gcc})
5501 @cindex Parentheses, warnings
5502 This switch activates warnings for cases where parentheses are not used and
5503 the result is potential ambiguity from a readers point of view. For example
5504 (not a > b) when a and b are modular means ((not a) > b) and very likely the
5505 programmer intended (not (a > b)). Similarly (-x mod 5) means (-(x mod 5)) and
5506 quite likely ((-x) mod 5) was intended. In such situations it seems best to
5507 follow the rule of always parenthesizing to make the association clear, and
5508 this warning switch warns if such parentheses are not present. The default
5509 is that these warnings are given.
5510 This warning can also be turned on using @option{-gnatwa}.
5511
5512 @item -gnatwQ
5513 @emph{Suppress warnings on questionable missing parentheses.}
5514 @cindex @option{-gnatwQ} (@command{gcc})
5515 This switch suppresses warnings for cases where the association is not
5516 clear and the use of parentheses is preferred.
5517
5518 @item -gnatwr
5519 @emph{Activate warnings on redundant constructs.}
5520 @cindex @option{-gnatwr} (@command{gcc})
5521 This switch activates warnings for redundant constructs. The following
5522 is the current list of constructs regarded as redundant:
5523
5524 @itemize @bullet
5525 @item
5526 Assignment of an item to itself.
5527 @item
5528 Type conversion that converts an expression to its own type.
5529 @item
5530 Use of the attribute @code{Base} where @code{typ'Base} is the same
5531 as @code{typ}.
5532 @item
5533 Use of pragma @code{Pack} when all components are placed by a record
5534 representation clause.
5535 @item
5536 Exception handler containing only a reraise statement (raise with no
5537 operand) which has no effect.
5538 @item
5539 Use of the operator abs on an operand that is known at compile time
5540 to be non-negative
5541 @item
5542 Comparison of boolean expressions to an explicit True value.
5543 @end itemize
5544
5545 This warning can also be turned on using @option{-gnatwa}.
5546 The default is that warnings for redundant constructs are not given.
5547
5548 @item -gnatwR
5549 @emph{Suppress warnings on redundant constructs.}
5550 @cindex @option{-gnatwR} (@command{gcc})
5551 This switch suppresses warnings for redundant constructs.
5552
5553 @item -gnatw.r
5554 @emph{Activate warnings for object renaming function.}
5555 @cindex @option{-gnatw.r} (@command{gcc})
5556 This switch activates warnings for an object renaming that renames a
5557 function call, which is equivalent to a constant declaration (as
5558 opposed to renaming the function itself). The default is that these
5559 warnings are given. This warning can also be turned on using
5560 @option{-gnatwa}.
5561
5562 @item -gnatw.R
5563 @emph{Suppress warnings for object renaming function.}
5564 @cindex @option{-gnatwT} (@command{gcc})
5565 This switch suppresses warnings for object renaming function.
5566
5567 @item -gnatws
5568 @emph{Suppress all warnings.}
5569 @cindex @option{-gnatws} (@command{gcc})
5570 This switch completely suppresses the
5571 output of all warning messages from the GNAT front end.
5572 Note that it does not suppress warnings from the @command{gcc} back end.
5573 To suppress these back end warnings as well, use the switch @option{-w}
5574 in addition to @option{-gnatws}.
5575
5576 @item -gnatwt
5577 @emph{Activate warnings for tracking of deleted conditional code.}
5578 @cindex @option{-gnatwt} (@command{gcc})
5579 @cindex Deactivated code, warnings
5580 @cindex Deleted code, warnings
5581 This switch activates warnings for tracking of code in conditionals (IF and
5582 CASE statements) that is detected to be dead code which cannot be executed, and
5583 which is removed by the front end. This warning is off by default, and is not
5584 turned on by @option{-gnatwa}, it has to be turned on explicitly. This may be
5585 useful for detecting deactivated code in certified applications.
5586
5587 @item -gnatwT
5588 @emph{Suppress warnings for tracking of deleted conditional code.}
5589 @cindex @option{-gnatwT} (@command{gcc})
5590 This switch suppresses warnings for tracking of deleted conditional code.
5591
5592 @item -gnatwu
5593 @emph{Activate warnings on unused entities.}
5594 @cindex @option{-gnatwu} (@command{gcc})
5595 This switch activates warnings to be generated for entities that
5596 are declared but not referenced, and for units that are @code{with}'ed
5597 and not
5598 referenced. In the case of packages, a warning is also generated if
5599 no entities in the package are referenced. This means that if the package
5600 is referenced but the only references are in @code{use}
5601 clauses or @code{renames}
5602 declarations, a warning is still generated. A warning is also generated
5603 for a generic package that is @code{with}'ed but never instantiated.
5604 In the case where a package or subprogram body is compiled, and there
5605 is a @code{with} on the corresponding spec
5606 that is only referenced in the body,
5607 a warning is also generated, noting that the
5608 @code{with} can be moved to the body. The default is that
5609 such warnings are not generated.
5610 This switch also activates warnings on unreferenced formals
5611 (it includes the effect of @option{-gnatwf}).
5612 This warning can also be turned on using @option{-gnatwa}.
5613
5614 @item -gnatwU
5615 @emph{Suppress warnings on unused entities.}
5616 @cindex @option{-gnatwU} (@command{gcc})
5617 This switch suppresses warnings for unused entities and packages.
5618 It also turns off warnings on unreferenced formals (and thus includes
5619 the effect of @option{-gnatwF}).
5620
5621 @item -gnatwv
5622 @emph{Activate warnings on unassigned variables.}
5623 @cindex @option{-gnatwv} (@command{gcc})
5624 @cindex Unassigned variable warnings
5625 This switch activates warnings for access to variables which
5626 may not be properly initialized. The default is that
5627 such warnings are generated.
5628 This warning can also be turned on using @option{-gnatwa}.
5629
5630 @item -gnatwV
5631 @emph{Suppress warnings on unassigned variables.}
5632 @cindex @option{-gnatwV} (@command{gcc})
5633 This switch suppresses warnings for access to variables which
5634 may not be properly initialized.
5635 For variables of a composite type, the warning can also be suppressed in
5636 Ada 2005 by using a default initialization with a box. For example, if
5637 Table is an array of records whose components are only partially uninitialized,
5638 then the following code:
5639
5640 @smallexample @c ada
5641 Tab : Table := (others => <>);
5642 @end smallexample
5643
5644 will suppress warnings on subsequent statements that access components
5645 of variable Tab.
5646
5647 @item -gnatww
5648 @emph{Activate warnings on wrong low bound assumption.}
5649 @cindex @option{-gnatww} (@command{gcc})
5650 @cindex String indexing warnings
5651 This switch activates warnings for indexing an unconstrained string parameter
5652 with a literal or S'Length. This is a case where the code is assuming that the
5653 low bound is one, which is in general not true (for example when a slice is
5654 passed). The default is that such warnings are generated.
5655 This warning can also be turned on using @option{-gnatwa}.
5656
5657 @item -gnatwW
5658 @emph{Suppress warnings on wrong low bound assumption.}
5659 @cindex @option{-gnatwW} (@command{gcc})
5660 This switch suppresses warnings for indexing an unconstrained string parameter
5661 with a literal or S'Length. Note that this warning can also be suppressed
5662 in a particular case by adding an
5663 assertion that the lower bound is 1,
5664 as shown in the following example.
5665
5666 @smallexample @c ada
5667 procedure K (S : String) is
5668 pragma Assert (S'First = 1);
5669 @dots{}
5670 @end smallexample
5671
5672 @item -gnatw.w
5673 @emph{Activate warnings on unnecessary Warnings Off pragmas}
5674 @cindex @option{-gnatw.w} (@command{gcc})
5675 @cindex Warnings Off control
5676 This switch activates warnings for use of @code{pragma Warnings (Off, entity}
5677 where either the pragma is entirely useless (because it suppresses no
5678 warnings), or it could be replaced by @code{pragma Unreferenced} or
5679 @code{pragma Unmodified}.The default is that these warnings are not given.
5680 Note that this warning is not included in -gnatwa, it must be
5681 activated explicitly.
5682
5683 @item -gnatw.W
5684 @emph{Suppress warnings on unnecessary Warnings Off pragmas}
5685 @cindex @option{-gnatw.W} (@command{gcc})
5686 This switch suppresses warnings for use of @code{pragma Warnings (Off, entity}.
5687
5688 @item -gnatwx
5689 @emph{Activate warnings on Export/Import pragmas.}
5690 @cindex @option{-gnatwx} (@command{gcc})
5691 @cindex Export/Import pragma warnings
5692 This switch activates warnings on Export/Import pragmas when
5693 the compiler detects a possible conflict between the Ada and
5694 foreign language calling sequences. For example, the use of
5695 default parameters in a convention C procedure is dubious
5696 because the C compiler cannot supply the proper default, so
5697 a warning is issued. The default is that such warnings are
5698 generated.
5699 This warning can also be turned on using @option{-gnatwa}.
5700
5701 @item -gnatwX
5702 @emph{Suppress warnings on Export/Import pragmas.}
5703 @cindex @option{-gnatwX} (@command{gcc})
5704 This switch suppresses warnings on Export/Import pragmas.
5705 The sense of this is that you are telling the compiler that
5706 you know what you are doing in writing the pragma, and it
5707 should not complain at you.
5708
5709 @item -gnatw.x
5710 @emph{Activate warnings for No_Exception_Propagation mode.}
5711 @cindex @option{-gnatwm} (@command{gcc})
5712 This switch activates warnings for exception usage when pragma Restrictions
5713 (No_Exception_Propagation) is in effect. Warnings are given for implicit or
5714 explicit exception raises which are not covered by a local handler, and for
5715 exception handlers which do not cover a local raise. The default is that these
5716 warnings are not given.
5717
5718 @item -gnatw.X
5719 @emph{Disable warnings for No_Exception_Propagation mode.}
5720 This switch disables warnings for exception usage when pragma Restrictions
5721 (No_Exception_Propagation) is in effect.
5722
5723 @item -gnatwy
5724 @emph{Activate warnings for Ada 2005 compatibility issues.}
5725 @cindex @option{-gnatwy} (@command{gcc})
5726 @cindex Ada 2005 compatibility issues warnings
5727 For the most part Ada 2005 is upwards compatible with Ada 95,
5728 but there are some exceptions (for example the fact that
5729 @code{interface} is now a reserved word in Ada 2005). This
5730 switch activates several warnings to help in identifying
5731 and correcting such incompatibilities. The default is that
5732 these warnings are generated. Note that at one point Ada 2005
5733 was called Ada 0Y, hence the choice of character.
5734 This warning can also be turned on using @option{-gnatwa}.
5735
5736 @item -gnatwY
5737 @emph{Disable warnings for Ada 2005 compatibility issues.}
5738 @cindex @option{-gnatwY} (@command{gcc})
5739 @cindex Ada 2005 compatibility issues warnings
5740 This switch suppresses several warnings intended to help in identifying
5741 incompatibilities between Ada 95 and Ada 2005.
5742
5743 @item -gnatwz
5744 @emph{Activate warnings on unchecked conversions.}
5745 @cindex @option{-gnatwz} (@command{gcc})
5746 @cindex Unchecked_Conversion warnings
5747 This switch activates warnings for unchecked conversions
5748 where the types are known at compile time to have different
5749 sizes. The default
5750 is that such warnings are generated. Warnings are also
5751 generated for subprogram pointers with different conventions,
5752 and, on VMS only, for data pointers with different conventions.
5753 This warning can also be turned on using @option{-gnatwa}.
5754
5755 @item -gnatwZ
5756 @emph{Suppress warnings on unchecked conversions.}
5757 @cindex @option{-gnatwZ} (@command{gcc})
5758 This switch suppresses warnings for unchecked conversions
5759 where the types are known at compile time to have different
5760 sizes or conventions.
5761
5762 @item ^-Wunused^WARNINGS=UNUSED^
5763 @cindex @option{-Wunused}
5764 The warnings controlled by the @option{-gnatw} switch are generated by
5765 the front end of the compiler. The @option{GCC} back end can provide
5766 additional warnings and they are controlled by the @option{-W} switch.
5767 For example, @option{^-Wunused^WARNINGS=UNUSED^} activates back end
5768 warnings for entities that are declared but not referenced.
5769
5770 @item ^-Wuninitialized^WARNINGS=UNINITIALIZED^
5771 @cindex @option{-Wuninitialized}
5772 Similarly, @option{^-Wuninitialized^WARNINGS=UNINITIALIZED^} activates
5773 the back end warning for uninitialized variables. This switch must be
5774 used in conjunction with an optimization level greater than zero.
5775
5776 @item ^-Wall^/ALL_BACK_END_WARNINGS^
5777 @cindex @option{-Wall}
5778 This switch enables all the above warnings from the @option{GCC} back end.
5779 The code generator detects a number of warning situations that are missed
5780 by the @option{GNAT} front end, and this switch can be used to activate them.
5781 The use of this switch also sets the default front end warning mode to
5782 @option{-gnatwa}, that is, most front end warnings activated as well.
5783
5784 @item ^-w^/NO_BACK_END_WARNINGS^
5785 @cindex @option{-w}
5786 Conversely, this switch suppresses warnings from the @option{GCC} back end.
5787 The use of this switch also sets the default front end warning mode to
5788 @option{-gnatws}, that is, front end warnings suppressed as well.
5789
5790 @end table
5791
5792 @noindent
5793 @ifclear vms
5794 A string of warning parameters can be used in the same parameter. For example:
5795
5796 @smallexample
5797 -gnatwaLe
5798 @end smallexample
5799
5800 @noindent
5801 will turn on all optional warnings except for elaboration pragma warnings,
5802 and also specify that warnings should be treated as errors.
5803 @end ifclear
5804 When no switch @option{^-gnatw^/WARNINGS^} is used, this is equivalent to:
5805
5806 @table @option
5807 @c !sort!
5808 @item -gnatwC
5809 @item -gnatwD
5810 @item -gnatwF
5811 @item -gnatwg
5812 @item -gnatwH
5813 @item -gnatwi
5814 @item -gnatwJ
5815 @item -gnatwK
5816 @item -gnatwL
5817 @item -gnatwM
5818 @item -gnatwn
5819 @item -gnatwo
5820 @item -gnatwP
5821 @item -gnatwR
5822 @item -gnatwU
5823 @item -gnatwv
5824 @item -gnatwz
5825 @item -gnatwx
5826
5827 @end table
5828
5829 @node Debugging and Assertion Control
5830 @subsection Debugging and Assertion Control
5831
5832 @table @option
5833 @item -gnata
5834 @cindex @option{-gnata} (@command{gcc})
5835 @findex Assert
5836 @findex Debug
5837 @cindex Assertions
5838
5839 @noindent
5840 The pragmas @code{Assert} and @code{Debug} normally have no effect and
5841 are ignored. This switch, where @samp{a} stands for assert, causes
5842 @code{Assert} and @code{Debug} pragmas to be activated.
5843
5844 The pragmas have the form:
5845
5846 @smallexample
5847 @cartouche
5848 @b{pragma} Assert (@var{Boolean-expression} @r{[},
5849 @var{static-string-expression}@r{]})
5850 @b{pragma} Debug (@var{procedure call})
5851 @end cartouche
5852 @end smallexample
5853
5854 @noindent
5855 The @code{Assert} pragma causes @var{Boolean-expression} to be tested.
5856 If the result is @code{True}, the pragma has no effect (other than
5857 possible side effects from evaluating the expression). If the result is
5858 @code{False}, the exception @code{Assert_Failure} declared in the package
5859 @code{System.Assertions} is
5860 raised (passing @var{static-string-expression}, if present, as the
5861 message associated with the exception). If no string expression is
5862 given the default is a string giving the file name and line number
5863 of the pragma.
5864
5865 The @code{Debug} pragma causes @var{procedure} to be called. Note that
5866 @code{pragma Debug} may appear within a declaration sequence, allowing
5867 debugging procedures to be called between declarations.
5868
5869 @ifset vms
5870 @item /DEBUG@r{[}=debug-level@r{]}
5871 @itemx /NODEBUG
5872 Specifies how much debugging information is to be included in
5873 the resulting object file where 'debug-level' is one of the following:
5874 @table @code
5875 @item TRACEBACK
5876 Include both debugger symbol records and traceback
5877 the object file.
5878 This is the default setting.
5879 @item ALL
5880 Include both debugger symbol records and traceback in
5881 object file.
5882 @item NONE
5883 Excludes both debugger symbol records and traceback
5884 the object file. Same as /NODEBUG.
5885 @item SYMBOLS
5886 Includes only debugger symbol records in the object
5887 file. Note that this doesn't include traceback information.
5888 @end table
5889 @end ifset
5890 @end table
5891
5892 @node Validity Checking
5893 @subsection Validity Checking
5894 @findex Validity Checking
5895
5896 @noindent
5897 The Ada Reference Manual defines the concept of invalid values (see
5898 RM 13.9.1). The primary source of invalid values is uninitialized
5899 variables. A scalar variable that is left uninitialized may contain
5900 an invalid value; the concept of invalid does not apply to access or
5901 composite types.
5902
5903 It is an error to read an invalid value, but the RM does not require
5904 run-time checks to detect such errors, except for some minimal
5905 checking to prevent erroneous execution (i.e. unpredictable
5906 behavior). This corresponds to the @option{-gnatVd} switch below,
5907 which is the default. For example, by default, if the expression of a
5908 case statement is invalid, it will raise Constraint_Error rather than
5909 causing a wild jump, and if an array index on the left-hand side of an
5910 assignment is invalid, it will raise Constraint_Error rather than
5911 overwriting an arbitrary memory location.
5912
5913 The @option{-gnatVa} may be used to enable additional validity checks,
5914 which are not required by the RM. These checks are often very
5915 expensive (which is why the RM does not require them). These checks
5916 are useful in tracking down uninitialized variables, but they are
5917 not usually recommended for production builds.
5918
5919 The other @option{-gnatV^@var{x}^^} switches below allow finer-grained
5920 control; you can enable whichever validity checks you desire. However,
5921 for most debugging purposes, @option{-gnatVa} is sufficient, and the
5922 default @option{-gnatVd} (i.e. standard Ada behavior) is usually
5923 sufficient for non-debugging use.
5924
5925 The @option{-gnatB} switch tells the compiler to assume that all
5926 values are valid (that is, within their declared subtype range)
5927 except in the context of a use of the Valid attribute. This means
5928 the compiler can generate more efficient code, since the range
5929 of values is better known at compile time. However, an uninitialized
5930 variable can cause wild jumps and memory corruption in this mode.
5931
5932 The @option{-gnatV^@var{x}^^} switch allows control over the validity
5933 checking mode as described below.
5934 @ifclear vms
5935 The @code{x} argument is a string of letters that
5936 indicate validity checks that are performed or not performed in addition
5937 to the default checks required by Ada as described above.
5938 @end ifclear
5939 @ifset vms
5940 The options allowed for this qualifier
5941 indicate validity checks that are performed or not performed in addition
5942 to the default checks required by Ada as described above.
5943 @end ifset
5944
5945 @table @option
5946 @c !sort!
5947 @item -gnatVa
5948 @emph{All validity checks.}
5949 @cindex @option{-gnatVa} (@command{gcc})
5950 All validity checks are turned on.
5951 @ifclear vms
5952 That is, @option{-gnatVa} is
5953 equivalent to @option{gnatVcdfimorst}.
5954 @end ifclear
5955
5956 @item -gnatVc
5957 @emph{Validity checks for copies.}
5958 @cindex @option{-gnatVc} (@command{gcc})
5959 The right hand side of assignments, and the initializing values of
5960 object declarations are validity checked.
5961
5962 @item -gnatVd
5963 @emph{Default (RM) validity checks.}
5964 @cindex @option{-gnatVd} (@command{gcc})
5965 Some validity checks are done by default following normal Ada semantics
5966 (RM 13.9.1 (9-11)).
5967 A check is done in case statements that the expression is within the range
5968 of the subtype. If it is not, Constraint_Error is raised.
5969 For assignments to array components, a check is done that the expression used
5970 as index is within the range. If it is not, Constraint_Error is raised.
5971 Both these validity checks may be turned off using switch @option{-gnatVD}.
5972 They are turned on by default. If @option{-gnatVD} is specified, a subsequent
5973 switch @option{-gnatVd} will leave the checks turned on.
5974 Switch @option{-gnatVD} should be used only if you are sure that all such
5975 expressions have valid values. If you use this switch and invalid values
5976 are present, then the program is erroneous, and wild jumps or memory
5977 overwriting may occur.
5978
5979 @item -gnatVe
5980 @emph{Validity checks for elementary components.}
5981 @cindex @option{-gnatVe} (@command{gcc})
5982 In the absence of this switch, assignments to record or array components are
5983 not validity checked, even if validity checks for assignments generally
5984 (@option{-gnatVc}) are turned on. In Ada, assignment of composite values do not
5985 require valid data, but assignment of individual components does. So for
5986 example, there is a difference between copying the elements of an array with a
5987 slice assignment, compared to assigning element by element in a loop. This
5988 switch allows you to turn off validity checking for components, even when they
5989 are assigned component by component.
5990
5991 @item -gnatVf
5992 @emph{Validity checks for floating-point values.}
5993 @cindex @option{-gnatVf} (@command{gcc})
5994 In the absence of this switch, validity checking occurs only for discrete
5995 values. If @option{-gnatVf} is specified, then validity checking also applies
5996 for floating-point values, and NaNs and infinities are considered invalid,
5997 as well as out of range values for constrained types. Note that this means
5998 that standard IEEE infinity mode is not allowed. The exact contexts
5999 in which floating-point values are checked depends on the setting of other
6000 options. For example,
6001 @option{^-gnatVif^VALIDITY_CHECKING=(IN_PARAMS,FLOATS)^} or
6002 @option{^-gnatVfi^VALIDITY_CHECKING=(FLOATS,IN_PARAMS)^}
6003 (the order does not matter) specifies that floating-point parameters of mode
6004 @code{in} should be validity checked.
6005
6006 @item -gnatVi
6007 @emph{Validity checks for @code{in} mode parameters}
6008 @cindex @option{-gnatVi} (@command{gcc})
6009 Arguments for parameters of mode @code{in} are validity checked in function
6010 and procedure calls at the point of call.
6011
6012 @item -gnatVm
6013 @emph{Validity checks for @code{in out} mode parameters.}
6014 @cindex @option{-gnatVm} (@command{gcc})
6015 Arguments for parameters of mode @code{in out} are validity checked in
6016 procedure calls at the point of call. The @code{'m'} here stands for
6017 modify, since this concerns parameters that can be modified by the call.
6018 Note that there is no specific option to test @code{out} parameters,
6019 but any reference within the subprogram will be tested in the usual
6020 manner, and if an invalid value is copied back, any reference to it
6021 will be subject to validity checking.
6022
6023 @item -gnatVn
6024 @emph{No validity checks.}
6025 @cindex @option{-gnatVn} (@command{gcc})
6026 This switch turns off all validity checking, including the default checking
6027 for case statements and left hand side subscripts. Note that the use of
6028 the switch @option{-gnatp} suppresses all run-time checks, including
6029 validity checks, and thus implies @option{-gnatVn}. When this switch
6030 is used, it cancels any other @option{-gnatV} previously issued.
6031
6032 @item -gnatVo
6033 @emph{Validity checks for operator and attribute operands.}
6034 @cindex @option{-gnatVo} (@command{gcc})
6035 Arguments for predefined operators and attributes are validity checked.
6036 This includes all operators in package @code{Standard},
6037 the shift operators defined as intrinsic in package @code{Interfaces}
6038 and operands for attributes such as @code{Pos}. Checks are also made
6039 on individual component values for composite comparisons, and on the
6040 expressions in type conversions and qualified expressions. Checks are
6041 also made on explicit ranges using @samp{..} (e.g.@: slices, loops etc).
6042
6043 @item -gnatVp
6044 @emph{Validity checks for parameters.}
6045 @cindex @option{-gnatVp} (@command{gcc})
6046 This controls the treatment of parameters within a subprogram (as opposed
6047 to @option{-gnatVi} and @option{-gnatVm} which control validity testing
6048 of parameters on a call. If either of these call options is used, then
6049 normally an assumption is made within a subprogram that the input arguments
6050 have been validity checking at the point of call, and do not need checking
6051 again within a subprogram). If @option{-gnatVp} is set, then this assumption
6052 is not made, and parameters are not assumed to be valid, so their validity
6053 will be checked (or rechecked) within the subprogram.
6054
6055 @item -gnatVr
6056 @emph{Validity checks for function returns.}
6057 @cindex @option{-gnatVr} (@command{gcc})
6058 The expression in @code{return} statements in functions is validity
6059 checked.
6060
6061 @item -gnatVs
6062 @emph{Validity checks for subscripts.}
6063 @cindex @option{-gnatVs} (@command{gcc})
6064 All subscripts expressions are checked for validity, whether they appear
6065 on the right side or left side (in default mode only left side subscripts
6066 are validity checked).
6067
6068 @item -gnatVt
6069 @emph{Validity checks for tests.}
6070 @cindex @option{-gnatVt} (@command{gcc})
6071 Expressions used as conditions in @code{if}, @code{while} or @code{exit}
6072 statements are checked, as well as guard expressions in entry calls.
6073
6074 @end table
6075
6076 @noindent
6077 The @option{-gnatV} switch may be followed by
6078 ^a string of letters^a list of options^
6079 to turn on a series of validity checking options.
6080 For example,
6081 @option{^-gnatVcr^/VALIDITY_CHECKING=(COPIES, RETURNS)^}
6082 specifies that in addition to the default validity checking, copies and
6083 function return expressions are to be validity checked.
6084 In order to make it easier
6085 to specify the desired combination of effects,
6086 @ifclear vms
6087 the upper case letters @code{CDFIMORST} may
6088 be used to turn off the corresponding lower case option.
6089 @end ifclear
6090 @ifset vms
6091 the prefix @code{NO} on an option turns off the corresponding validity
6092 checking:
6093 @itemize @bullet
6094 @item @code{NOCOPIES}
6095 @item @code{NODEFAULT}
6096 @item @code{NOFLOATS}
6097 @item @code{NOIN_PARAMS}
6098 @item @code{NOMOD_PARAMS}
6099 @item @code{NOOPERANDS}
6100 @item @code{NORETURNS}
6101 @item @code{NOSUBSCRIPTS}
6102 @item @code{NOTESTS}
6103 @end itemize
6104 @end ifset
6105 Thus
6106 @option{^-gnatVaM^/VALIDITY_CHECKING=(ALL, NOMOD_PARAMS)^}
6107 turns on all validity checking options except for
6108 checking of @code{@b{in out}} procedure arguments.
6109
6110 The specification of additional validity checking generates extra code (and
6111 in the case of @option{-gnatVa} the code expansion can be substantial).
6112 However, these additional checks can be very useful in detecting
6113 uninitialized variables, incorrect use of unchecked conversion, and other
6114 errors leading to invalid values. The use of pragma @code{Initialize_Scalars}
6115 is useful in conjunction with the extra validity checking, since this
6116 ensures that wherever possible uninitialized variables have invalid values.
6117
6118 See also the pragma @code{Validity_Checks} which allows modification of
6119 the validity checking mode at the program source level, and also allows for
6120 temporary disabling of validity checks.
6121
6122 @node Style Checking
6123 @subsection Style Checking
6124 @findex Style checking
6125
6126 @noindent
6127 The @option{-gnaty^x^(option,option,@dots{})^} switch
6128 @cindex @option{-gnaty} (@command{gcc})
6129 causes the compiler to
6130 enforce specified style rules. A limited set of style rules has been used
6131 in writing the GNAT sources themselves. This switch allows user programs
6132 to activate all or some of these checks. If the source program fails a
6133 specified style check, an appropriate warning message is given, preceded by
6134 the character sequence ``(style)''.
6135 @ifset vms
6136 @code{(option,option,@dots{})} is a sequence of keywords
6137 @end ifset
6138 @ifclear vms
6139 The string @var{x} is a sequence of letters or digits
6140 @end ifclear
6141 indicating the particular style
6142 checks to be performed. The following checks are defined:
6143
6144 @table @option
6145 @c !sort!
6146 @item 0-9
6147 @emph{Specify indentation level.}
6148 If a digit from 1-9 appears
6149 ^in the string after @option{-gnaty}^as an option for /STYLE_CHECKS^
6150 then proper indentation is checked, with the digit indicating the
6151 indentation level required. A value of zero turns off this style check.
6152 The general style of required indentation is as specified by
6153 the examples in the Ada Reference Manual. Full line comments must be
6154 aligned with the @code{--} starting on a column that is a multiple of
6155 the alignment level, or they may be aligned the same way as the following
6156 non-blank line (this is useful when full line comments appear in the middle
6157 of a statement.
6158
6159 @item ^a^ATTRIBUTE^
6160 @emph{Check attribute casing.}
6161 Attribute names, including the case of keywords such as @code{digits}
6162 used as attributes names, must be written in mixed case, that is, the
6163 initial letter and any letter following an underscore must be uppercase.
6164 All other letters must be lowercase.
6165
6166 @item ^A^ARRAY_INDEXES^
6167 @emph{Use of array index numbers in array attributes.}
6168 When using the array attributes First, Last, Range,
6169 or Length, the index number must be omitted for one-dimensional arrays
6170 and is required for multi-dimensional arrays.
6171
6172 @item ^b^BLANKS^
6173 @emph{Blanks not allowed at statement end.}
6174 Trailing blanks are not allowed at the end of statements. The purpose of this
6175 rule, together with h (no horizontal tabs), is to enforce a canonical format
6176 for the use of blanks to separate source tokens.
6177
6178 @item ^B^BOOLEAN_OPERATORS^
6179 @emph{Check Boolean operators.}
6180 The use of AND/OR operators is not permitted except in the cases of modular
6181 operands, array operands, and simple stand-alone boolean variables or
6182 boolean constants. In all other cases AND THEN/OR ELSE are required.
6183
6184 @item ^c^COMMENTS^
6185 @emph{Check comments.}
6186 Comments must meet the following set of rules:
6187
6188 @itemize @bullet
6189
6190 @item
6191 The ``@code{--}'' that starts the column must either start in column one,
6192 or else at least one blank must precede this sequence.
6193
6194 @item
6195 Comments that follow other tokens on a line must have at least one blank
6196 following the ``@code{--}'' at the start of the comment.
6197
6198 @item
6199 Full line comments must have two blanks following the ``@code{--}'' that
6200 starts the comment, with the following exceptions.
6201
6202 @item
6203 A line consisting only of the ``@code{--}'' characters, possibly preceded
6204 by blanks is permitted.
6205
6206 @item
6207 A comment starting with ``@code{--x}'' where @code{x} is a special character
6208 is permitted.
6209 This allows proper processing of the output generated by specialized tools
6210 including @command{gnatprep} (where ``@code{--!}'' is used) and the SPARK
6211 annotation
6212 language (where ``@code{--#}'' is used). For the purposes of this rule, a
6213 special character is defined as being in one of the ASCII ranges
6214 @code{16#21#@dots{}16#2F#} or @code{16#3A#@dots{}16#3F#}.
6215 Note that this usage is not permitted
6216 in GNAT implementation units (i.e., when @option{-gnatg} is used).
6217
6218 @item
6219 A line consisting entirely of minus signs, possibly preceded by blanks, is
6220 permitted. This allows the construction of box comments where lines of minus
6221 signs are used to form the top and bottom of the box.
6222
6223 @item
6224 A comment that starts and ends with ``@code{--}'' is permitted as long as at
6225 least one blank follows the initial ``@code{--}''. Together with the preceding
6226 rule, this allows the construction of box comments, as shown in the following
6227 example:
6228 @smallexample
6229 ---------------------------
6230 -- This is a box comment --
6231 -- with two text lines. --
6232 ---------------------------
6233 @end smallexample
6234 @end itemize
6235
6236 @item ^d^DOS_LINE_ENDINGS^
6237 @emph{Check no DOS line terminators present.}
6238 All lines must be terminated by a single ASCII.LF
6239 character (in particular the DOS line terminator sequence CR/LF is not
6240 allowed).
6241
6242 @item ^e^END^
6243 @emph{Check end/exit labels.}
6244 Optional labels on @code{end} statements ending subprograms and on
6245 @code{exit} statements exiting named loops, are required to be present.
6246
6247 @item ^f^VTABS^
6248 @emph{No form feeds or vertical tabs.}
6249 Neither form feeds nor vertical tab characters are permitted
6250 in the source text.
6251
6252 @item ^g^GNAT^
6253 @emph{GNAT style mode}
6254 The set of style check switches is set to match that used by the GNAT sources.
6255 This may be useful when developing code that is eventually intended to be
6256 incorporated into GNAT. For further details, see GNAT sources.
6257
6258 @item ^h^HTABS^
6259 @emph{No horizontal tabs.}
6260 Horizontal tab characters are not permitted in the source text.
6261 Together with the b (no blanks at end of line) check, this
6262 enforces a canonical form for the use of blanks to separate
6263 source tokens.
6264
6265 @item ^i^IF_THEN^
6266 @emph{Check if-then layout.}
6267 The keyword @code{then} must appear either on the same
6268 line as corresponding @code{if}, or on a line on its own, lined
6269 up under the @code{if} with at least one non-blank line in between
6270 containing all or part of the condition to be tested.
6271
6272 @item ^I^IN_MODE^
6273 @emph{check mode IN keywords}
6274 Mode @code{in} (the default mode) is not
6275 allowed to be given explicitly. @code{in out} is fine,
6276 but not @code{in} on its own.
6277
6278 @item ^k^KEYWORD^
6279 @emph{Check keyword casing.}
6280 All keywords must be in lower case (with the exception of keywords
6281 such as @code{digits} used as attribute names to which this check
6282 does not apply).
6283
6284 @item ^l^LAYOUT^
6285 @emph{Check layout.}
6286 Layout of statement and declaration constructs must follow the
6287 recommendations in the Ada Reference Manual, as indicated by the
6288 form of the syntax rules. For example an @code{else} keyword must
6289 be lined up with the corresponding @code{if} keyword.
6290
6291 There are two respects in which the style rule enforced by this check
6292 option are more liberal than those in the Ada Reference Manual. First
6293 in the case of record declarations, it is permissible to put the
6294 @code{record} keyword on the same line as the @code{type} keyword, and
6295 then the @code{end} in @code{end record} must line up under @code{type}.
6296 This is also permitted when the type declaration is split on two lines.
6297 For example, any of the following three layouts is acceptable:
6298
6299 @smallexample @c ada
6300 @cartouche
6301 type q is record
6302 a : integer;
6303 b : integer;
6304 end record;
6305
6306 type q is
6307 record
6308 a : integer;
6309 b : integer;
6310 end record;
6311
6312 type q is
6313 record
6314 a : integer;
6315 b : integer;
6316 end record;
6317
6318 @end cartouche
6319 @end smallexample
6320
6321 @noindent
6322 Second, in the case of a block statement, a permitted alternative
6323 is to put the block label on the same line as the @code{declare} or
6324 @code{begin} keyword, and then line the @code{end} keyword up under
6325 the block label. For example both the following are permitted:
6326
6327 @smallexample @c ada
6328 @cartouche
6329 Block : declare
6330 A : Integer := 3;
6331 begin
6332 Proc (A, A);
6333 end Block;
6334
6335 Block :
6336 declare
6337 A : Integer := 3;
6338 begin
6339 Proc (A, A);
6340 end Block;
6341 @end cartouche
6342 @end smallexample
6343
6344 @noindent
6345 The same alternative format is allowed for loops. For example, both of
6346 the following are permitted:
6347
6348 @smallexample @c ada
6349 @cartouche
6350 Clear : while J < 10 loop
6351 A (J) := 0;
6352 end loop Clear;
6353
6354 Clear :
6355 while J < 10 loop
6356 A (J) := 0;
6357 end loop Clear;
6358 @end cartouche
6359 @end smallexample
6360
6361 @item ^Lnnn^MAX_NESTING=nnn^
6362 @emph{Set maximum nesting level}
6363 The maximum level of nesting of constructs (including subprograms, loops,
6364 blocks, packages, and conditionals) may not exceed the given value
6365 @option{nnn}. A value of zero disconnects this style check.
6366
6367 @item ^m^LINE_LENGTH^
6368 @emph{Check maximum line length.}
6369 The length of source lines must not exceed 79 characters, including
6370 any trailing blanks. The value of 79 allows convenient display on an
6371 80 character wide device or window, allowing for possible special
6372 treatment of 80 character lines. Note that this count is of
6373 characters in the source text. This means that a tab character counts
6374 as one character in this count but a wide character sequence counts as
6375 a single character (however many bytes are needed in the encoding).
6376
6377 @item ^Mnnn^MAX_LENGTH=nnn^
6378 @emph{Set maximum line length.}
6379 The length of lines must not exceed the
6380 given value @option{nnn}. The maximum value that can be specified is 32767.
6381
6382 @item ^n^STANDARD_CASING^
6383 @emph{Check casing of entities in Standard.}
6384 Any identifier from Standard must be cased
6385 to match the presentation in the Ada Reference Manual (for example,
6386 @code{Integer} and @code{ASCII.NUL}).
6387
6388 @item ^N^NONE^
6389 @emph{Turn off all style checks}
6390 All style check options are turned off.
6391
6392 @item ^o^ORDERED_SUBPROGRAMS^
6393 @emph{Check order of subprogram bodies.}
6394 All subprogram bodies in a given scope
6395 (e.g.@: a package body) must be in alphabetical order. The ordering
6396 rule uses normal Ada rules for comparing strings, ignoring casing
6397 of letters, except that if there is a trailing numeric suffix, then
6398 the value of this suffix is used in the ordering (e.g.@: Junk2 comes
6399 before Junk10).
6400
6401 @item ^O^OVERRIDING_INDICATORS^
6402 @emph{Check that overriding subprograms are explicitly marked as such.}
6403 The declaration of a primitive operation of a type extension that overrides
6404 an inherited operation must carry an overriding indicator.
6405
6406 @item ^p^PRAGMA^
6407 @emph{Check pragma casing.}
6408 Pragma names must be written in mixed case, that is, the
6409 initial letter and any letter following an underscore must be uppercase.
6410 All other letters must be lowercase.
6411
6412 @item ^r^REFERENCES^
6413 @emph{Check references.}
6414 All identifier references must be cased in the same way as the
6415 corresponding declaration. No specific casing style is imposed on
6416 identifiers. The only requirement is for consistency of references
6417 with declarations.
6418
6419 @item ^S^STATEMENTS_AFTER_THEN_ELSE^
6420 @emph{Check no statements after THEN/ELSE.}
6421 No statements are allowed
6422 on the same line as a THEN or ELSE keyword following the
6423 keyword in an IF statement. OR ELSE and AND THEN are not affected,
6424 and a special exception allows a pragma to appear after ELSE.
6425
6426 @item ^s^SPECS^
6427 @emph{Check separate specs.}
6428 Separate declarations (``specs'') are required for subprograms (a
6429 body is not allowed to serve as its own declaration). The only
6430 exception is that parameterless library level procedures are
6431 not required to have a separate declaration. This exception covers
6432 the most frequent form of main program procedures.
6433
6434 @item ^t^TOKEN^
6435 @emph{Check token spacing.}
6436 The following token spacing rules are enforced:
6437
6438 @itemize @bullet
6439
6440 @item
6441 The keywords @code{@b{abs}} and @code{@b{not}} must be followed by a space.
6442
6443 @item
6444 The token @code{=>} must be surrounded by spaces.
6445
6446 @item
6447 The token @code{<>} must be preceded by a space or a left parenthesis.
6448
6449 @item
6450 Binary operators other than @code{**} must be surrounded by spaces.
6451 There is no restriction on the layout of the @code{**} binary operator.
6452
6453 @item
6454 Colon must be surrounded by spaces.
6455
6456 @item
6457 Colon-equal (assignment, initialization) must be surrounded by spaces.
6458
6459 @item
6460 Comma must be the first non-blank character on the line, or be
6461 immediately preceded by a non-blank character, and must be followed
6462 by a space.
6463
6464 @item
6465 If the token preceding a left parenthesis ends with a letter or digit, then
6466 a space must separate the two tokens.
6467
6468 @item
6469 if the token following a right parenthesis starts with a letter or digit, then
6470 a space must separate the two tokens.
6471
6472 @item
6473 A right parenthesis must either be the first non-blank character on
6474 a line, or it must be preceded by a non-blank character.
6475
6476 @item
6477 A semicolon must not be preceded by a space, and must not be followed by
6478 a non-blank character.
6479
6480 @item
6481 A unary plus or minus may not be followed by a space.
6482
6483 @item
6484 A vertical bar must be surrounded by spaces.
6485 @end itemize
6486
6487 @item ^u^UNNECESSARY_BLANK_LINES^
6488 @emph{Check unnecessary blank lines.}
6489 Unnecessary blank lines are not allowed. A blank line is considered
6490 unnecessary if it appears at the end of the file, or if more than
6491 one blank line occurs in sequence.
6492
6493 @item ^x^XTRA_PARENS^
6494 @emph{Check extra parentheses.}
6495 Unnecessary extra level of parentheses (C-style) are not allowed
6496 around conditions in @code{if} statements, @code{while} statements and
6497 @code{exit} statements.
6498
6499 @item ^y^ALL_BUILTIN^
6500 @emph{Set all standard style check options}
6501 This is equivalent to @code{gnaty3aAbcefhiklmnprst}, that is all checking
6502 options enabled with the exception of @option{-gnatyo}, @option{-gnatyI},
6503 @option{-gnatyS}, @option{-gnatyLnnn},
6504 @option{-gnatyd}, @option{-gnatyu}, and @option{-gnatyx}.
6505
6506 @ifclear vms
6507 @item -
6508 @emph{Remove style check options}
6509 This causes any subsequent options in the string to act as canceling the
6510 corresponding style check option. To cancel maximum nesting level control,
6511 use @option{L} parameter witout any integer value after that, because any
6512 digit following @option{-} in the parameter string of the @option{-gnaty}
6513 option will be threated as canceling indentation check. The same is true
6514 for @option{M} parameter. @option{y} and @option{N} parameters are not
6515 allowed after @option{-}.
6516
6517 @item +
6518 This causes any subsequent options in the string to enable the corresponding
6519 style check option. That is, it cancels the effect of a previous ^-^REMOVE^,
6520 if any.
6521 @end ifclear
6522
6523 @ifset vms
6524 @item NOxxx
6525 @emph{Removing style check options}
6526 If the name of a style check is preceded by @option{NO} then the corresponding
6527 style check is turned off. For example @option{NOCOMMENTS} turns off style
6528 checking for comments.
6529 @end ifset
6530 @end table
6531
6532 @noindent
6533 In the above rules, appearing in column one is always permitted, that is,
6534 counts as meeting either a requirement for a required preceding space,
6535 or as meeting a requirement for no preceding space.
6536
6537 Appearing at the end of a line is also always permitted, that is, counts
6538 as meeting either a requirement for a following space, or as meeting
6539 a requirement for no following space.
6540
6541 @noindent
6542 If any of these style rules is violated, a message is generated giving
6543 details on the violation. The initial characters of such messages are
6544 always ``@code{(style)}''. Note that these messages are treated as warning
6545 messages, so they normally do not prevent the generation of an object
6546 file. The @option{-gnatwe} switch can be used to treat warning messages,
6547 including style messages, as fatal errors.
6548
6549 The switch
6550 @ifclear vms
6551 @option{-gnaty} on its own (that is not
6552 followed by any letters or digits), then the effect is equivalent
6553 to the use of @option{-gnatyy}, as described above, that is all
6554 built-in standard style check options are enabled.
6555
6556 @end ifclear
6557 @ifset vms
6558 /STYLE_CHECKS=ALL_BUILTIN enables all checking options with
6559 the exception of ORDERED_SUBPROGRAMS, UNNECESSARY_BLANK_LINES,
6560 XTRA_PARENS, and DOS_LINE_ENDINGS. In addition
6561 @end ifset
6562
6563 The switch
6564 @ifclear vms
6565 @option{-gnatyN}
6566 @end ifclear
6567 @ifset vms
6568 /STYLE_CHECKS=NONE
6569 @end ifset
6570 clears any previously set style checks.
6571
6572 @node Run-Time Checks
6573 @subsection Run-Time Checks
6574 @cindex Division by zero
6575 @cindex Access before elaboration
6576 @cindex Checks, division by zero
6577 @cindex Checks, access before elaboration
6578 @cindex Checks, stack overflow checking
6579
6580 @noindent
6581 By default, the following checks are suppressed: integer overflow
6582 checks, stack overflow checks, and checks for access before
6583 elaboration on subprogram calls. All other checks, including range
6584 checks and array bounds checks, are turned on by default. The
6585 following @command{gcc} switches refine this default behavior.
6586
6587 @table @option
6588 @c !sort!
6589 @item -gnatp
6590 @cindex @option{-gnatp} (@command{gcc})
6591 @cindex Suppressing checks
6592 @cindex Checks, suppressing
6593 @findex Suppress
6594 This switch causes the unit to be compiled
6595 as though @code{pragma Suppress (All_checks)}
6596 had been present in the source. Validity checks are also eliminated (in
6597 other words @option{-gnatp} also implies @option{-gnatVn}.
6598 Use this switch to improve the performance
6599 of the code at the expense of safety in the presence of invalid data or
6600 program bugs.
6601
6602 Note that when checks are suppressed, the compiler is allowed, but not
6603 required, to omit the checking code. If the run-time cost of the
6604 checking code is zero or near-zero, the compiler will generate it even
6605 if checks are suppressed. In particular, if the compiler can prove
6606 that a certain check will necessarily fail, it will generate code to
6607 do an unconditional ``raise'', even if checks are suppressed. The
6608 compiler warns in this case. Another case in which checks may not be
6609 eliminated is when they are embedded in certain run time routines such
6610 as math library routines.
6611
6612 Of course, run-time checks are omitted whenever the compiler can prove
6613 that they will not fail, whether or not checks are suppressed.
6614
6615 Note that if you suppress a check that would have failed, program
6616 execution is erroneous, which means the behavior is totally
6617 unpredictable. The program might crash, or print wrong answers, or
6618 do anything else. It might even do exactly what you wanted it to do
6619 (and then it might start failing mysteriously next week or next
6620 year). The compiler will generate code based on the assumption that
6621 the condition being checked is true, which can result in disaster if
6622 that assumption is wrong.
6623
6624 @item -gnato
6625 @cindex @option{-gnato} (@command{gcc})
6626 @cindex Overflow checks
6627 @cindex Check, overflow
6628 Enables overflow checking for integer operations.
6629 This causes GNAT to generate slower and larger executable
6630 programs by adding code to check for overflow (resulting in raising
6631 @code{Constraint_Error} as required by standard Ada
6632 semantics). These overflow checks correspond to situations in which
6633 the true value of the result of an operation may be outside the base
6634 range of the result type. The following example shows the distinction:
6635
6636 @smallexample @c ada
6637 X1 : Integer := "Integer'Last";
6638 X2 : Integer range 1 .. 5 := "5";
6639 X3 : Integer := "Integer'Last";
6640 X4 : Integer range 1 .. 5 := "5";
6641 F : Float := "2.0E+20";
6642 @dots{}
6643 X1 := X1 + 1;
6644 X2 := X2 + 1;
6645 X3 := Integer (F);
6646 X4 := Integer (F);
6647 @end smallexample
6648
6649 @noindent
6650 Note that if explicit values are assigned at compile time, the
6651 compiler may be able to detect overflow at compile time, in which case
6652 no actual run-time checking code is required, and Constraint_Error
6653 will be raised unconditionally, with or without
6654 @option{-gnato}. That's why the assigned values in the above fragment
6655 are in quotes, the meaning is "assign a value not known to the
6656 compiler that happens to be equal to ...". The remaining discussion
6657 assumes that the compiler cannot detect the values at compile time.
6658
6659 Here the first addition results in a value that is outside the base range
6660 of Integer, and hence requires an overflow check for detection of the
6661 constraint error. Thus the first assignment to @code{X1} raises a
6662 @code{Constraint_Error} exception only if @option{-gnato} is set.
6663
6664 The second increment operation results in a violation of the explicit
6665 range constraint; such range checks are performed by default, and are
6666 unaffected by @option{-gnato}.
6667
6668 The two conversions of @code{F} both result in values that are outside
6669 the base range of type @code{Integer} and thus will raise
6670 @code{Constraint_Error} exceptions only if @option{-gnato} is used.
6671 The fact that the result of the second conversion is assigned to
6672 variable @code{X4} with a restricted range is irrelevant, since the problem
6673 is in the conversion, not the assignment.
6674
6675 Basically the rule is that in the default mode (@option{-gnato} not
6676 used), the generated code assures that all integer variables stay
6677 within their declared ranges, or within the base range if there is
6678 no declared range. This prevents any serious problems like indexes
6679 out of range for array operations.
6680
6681 What is not checked in default mode is an overflow that results in
6682 an in-range, but incorrect value. In the above example, the assignments
6683 to @code{X1}, @code{X2}, @code{X3} all give results that are within the
6684 range of the target variable, but the result is wrong in the sense that
6685 it is too large to be represented correctly. Typically the assignment
6686 to @code{X1} will result in wrap around to the largest negative number.
6687 The conversions of @code{F} will result in some @code{Integer} value
6688 and if that integer value is out of the @code{X4} range then the
6689 subsequent assignment would generate an exception.
6690
6691 @findex Machine_Overflows
6692 Note that the @option{-gnato} switch does not affect the code generated
6693 for any floating-point operations; it applies only to integer
6694 semantics).
6695 For floating-point, GNAT has the @code{Machine_Overflows}
6696 attribute set to @code{False} and the normal mode of operation is to
6697 generate IEEE NaN and infinite values on overflow or invalid operations
6698 (such as dividing 0.0 by 0.0).
6699
6700 The reason that we distinguish overflow checking from other kinds of
6701 range constraint checking is that a failure of an overflow check, unlike
6702 for example the failure of a range check, can result in an incorrect
6703 value, but cannot cause random memory destruction (like an out of range
6704 subscript), or a wild jump (from an out of range case value). Overflow
6705 checking is also quite expensive in time and space, since in general it
6706 requires the use of double length arithmetic.
6707
6708 Note again that @option{-gnato} is off by default, so overflow checking is
6709 not performed in default mode. This means that out of the box, with the
6710 default settings, GNAT does not do all the checks expected from the
6711 language description in the Ada Reference Manual. If you want all constraint
6712 checks to be performed, as described in this Manual, then you must
6713 explicitly use the -gnato switch either on the @command{gnatmake} or
6714 @command{gcc} command.
6715
6716 @item -gnatE
6717 @cindex @option{-gnatE} (@command{gcc})
6718 @cindex Elaboration checks
6719 @cindex Check, elaboration
6720 Enables dynamic checks for access-before-elaboration
6721 on subprogram calls and generic instantiations.
6722 Note that @option{-gnatE} is not necessary for safety, because in the
6723 default mode, GNAT ensures statically that the checks would not fail.
6724 For full details of the effect and use of this switch,
6725 @xref{Compiling Using gcc}.
6726
6727 @item -fstack-check
6728 @cindex @option{-fstack-check} (@command{gcc})
6729 @cindex Stack Overflow Checking
6730 @cindex Checks, stack overflow checking
6731 Activates stack overflow checking. For full details of the effect and use of
6732 this switch see @ref{Stack Overflow Checking}.
6733 @end table
6734
6735 @findex Unsuppress
6736 @noindent
6737 The setting of these switches only controls the default setting of the
6738 checks. You may modify them using either @code{Suppress} (to remove
6739 checks) or @code{Unsuppress} (to add back suppressed checks) pragmas in
6740 the program source.
6741
6742 @node Using gcc for Syntax Checking
6743 @subsection Using @command{gcc} for Syntax Checking
6744 @table @option
6745 @item -gnats
6746 @cindex @option{-gnats} (@command{gcc})
6747 @ifclear vms
6748
6749 @noindent
6750 The @code{s} stands for ``syntax''.
6751 @end ifclear
6752
6753 Run GNAT in syntax checking only mode. For
6754 example, the command
6755
6756 @smallexample
6757 $ gcc -c -gnats x.adb
6758 @end smallexample
6759
6760 @noindent
6761 compiles file @file{x.adb} in syntax-check-only mode. You can check a
6762 series of files in a single command
6763 @ifclear vms
6764 , and can use wild cards to specify such a group of files.
6765 Note that you must specify the @option{-c} (compile
6766 only) flag in addition to the @option{-gnats} flag.
6767 @end ifclear
6768 .
6769 You may use other switches in conjunction with @option{-gnats}. In
6770 particular, @option{-gnatl} and @option{-gnatv} are useful to control the
6771 format of any generated error messages.
6772
6773 When the source file is empty or contains only empty lines and/or comments,
6774 the output is a warning:
6775
6776 @smallexample
6777 $ gcc -c -gnats -x ada toto.txt
6778 toto.txt:1:01: warning: empty file, contains no compilation units
6779 $
6780 @end smallexample
6781
6782 Otherwise, the output is simply the error messages, if any. No object file or
6783 ALI file is generated by a syntax-only compilation. Also, no units other
6784 than the one specified are accessed. For example, if a unit @code{X}
6785 @code{with}'s a unit @code{Y}, compiling unit @code{X} in syntax
6786 check only mode does not access the source file containing unit
6787 @code{Y}.
6788
6789 @cindex Multiple units, syntax checking
6790 Normally, GNAT allows only a single unit in a source file. However, this
6791 restriction does not apply in syntax-check-only mode, and it is possible
6792 to check a file containing multiple compilation units concatenated
6793 together. This is primarily used by the @code{gnatchop} utility
6794 (@pxref{Renaming Files Using gnatchop}).
6795 @end table
6796
6797 @node Using gcc for Semantic Checking
6798 @subsection Using @command{gcc} for Semantic Checking
6799 @table @option
6800 @item -gnatc
6801 @cindex @option{-gnatc} (@command{gcc})
6802
6803 @ifclear vms
6804 @noindent
6805 The @code{c} stands for ``check''.
6806 @end ifclear
6807 Causes the compiler to operate in semantic check mode,
6808 with full checking for all illegalities specified in the
6809 Ada Reference Manual, but without generation of any object code
6810 (no object file is generated).
6811
6812 Because dependent files must be accessed, you must follow the GNAT
6813 semantic restrictions on file structuring to operate in this mode:
6814
6815 @itemize @bullet
6816 @item
6817 The needed source files must be accessible
6818 (@pxref{Search Paths and the Run-Time Library (RTL)}).
6819
6820 @item
6821 Each file must contain only one compilation unit.
6822
6823 @item
6824 The file name and unit name must match (@pxref{File Naming Rules}).
6825 @end itemize
6826
6827 The output consists of error messages as appropriate. No object file is
6828 generated. An @file{ALI} file is generated for use in the context of
6829 cross-reference tools, but this file is marked as not being suitable
6830 for binding (since no object file is generated).
6831 The checking corresponds exactly to the notion of
6832 legality in the Ada Reference Manual.
6833
6834 Any unit can be compiled in semantics-checking-only mode, including
6835 units that would not normally be compiled (subunits,
6836 and specifications where a separate body is present).
6837 @end table
6838
6839 @node Compiling Different Versions of Ada
6840 @subsection Compiling Different Versions of Ada
6841
6842 @noindent
6843 The switches described in this section allow you to explicitly specify
6844 the version of the Ada language that your programs are written in.
6845 By default @value{EDITION} assumes @value{DEFAULTLANGUAGEVERSION},
6846 but you can also specify @value{NONDEFAULTLANGUAGEVERSION} or
6847 indicate Ada 83 compatibility mode.
6848
6849 @table @option
6850 @cindex Compatibility with Ada 83
6851
6852 @item -gnat83 (Ada 83 Compatibility Mode)
6853 @cindex @option{-gnat83} (@command{gcc})
6854 @cindex ACVC, Ada 83 tests
6855 @cindex Ada 83 mode
6856
6857 @noindent
6858 Although GNAT is primarily an Ada 95 / Ada 2005 compiler, this switch
6859 specifies that the program is to be compiled in Ada 83 mode. With
6860 @option{-gnat83}, GNAT rejects most post-Ada 83 extensions and applies Ada 83
6861 semantics where this can be done easily.
6862 It is not possible to guarantee this switch does a perfect
6863 job; some subtle tests, such as are
6864 found in earlier ACVC tests (and that have been removed from the ACATS suite
6865 for Ada 95), might not compile correctly.
6866 Nevertheless, this switch may be useful in some circumstances, for example
6867 where, due to contractual reasons, existing code needs to be maintained
6868 using only Ada 83 features.
6869
6870 With few exceptions (most notably the need to use @code{<>} on
6871 @cindex Generic formal parameters
6872 unconstrained generic formal parameters, the use of the new Ada 95 / Ada 2005
6873 reserved words, and the use of packages
6874 with optional bodies), it is not necessary to specify the
6875 @option{-gnat83} switch when compiling Ada 83 programs, because, with rare
6876 exceptions, Ada 95 and Ada 2005 are upwardly compatible with Ada 83. Thus
6877 a correct Ada 83 program is usually also a correct program
6878 in these later versions of the language standard.
6879 For further information, please refer to @ref{Compatibility and Porting Guide}.
6880
6881 @item -gnat95 (Ada 95 mode)
6882 @cindex @option{-gnat95} (@command{gcc})
6883 @cindex Ada 95 mode
6884
6885 @noindent
6886 This switch directs the compiler to implement the Ada 95 version of the
6887 language.
6888 Since Ada 95 is almost completely upwards
6889 compatible with Ada 83, Ada 83 programs may generally be compiled using
6890 this switch (see the description of the @option{-gnat83} switch for further
6891 information about Ada 83 mode).
6892 If an Ada 2005 program is compiled in Ada 95 mode,
6893 uses of the new Ada 2005 features will cause error
6894 messages or warnings.
6895
6896 This switch also can be used to cancel the effect of a previous
6897 @option{-gnat83} or @option{-gnat05} switch earlier in the command line.
6898
6899 @item -gnat05 (Ada 2005 mode)
6900 @cindex @option{-gnat05} (@command{gcc})
6901 @cindex Ada 2005 mode
6902
6903 @noindent
6904 This switch directs the compiler to implement the Ada 2005 version of the
6905 language.
6906 Since Ada 2005 is almost completely upwards
6907 compatible with Ada 95 (and thus also with Ada 83), Ada 83 and Ada 95 programs
6908 may generally be compiled using this switch (see the description of the
6909 @option{-gnat83} and @option{-gnat95} switches for further
6910 information).
6911
6912 For information about the approved ``Ada Issues'' that have been incorporated
6913 into Ada 2005, see @url{http://www.ada-auth.org/cgi-bin/cvsweb.cgi/AIs}.
6914 Included with GNAT releases is a file @file{features-ada0y} that describes
6915 the set of implemented Ada 2005 features.
6916 @end table
6917
6918
6919 @node Character Set Control
6920 @subsection Character Set Control
6921 @table @option
6922 @item ^-gnati^/IDENTIFIER_CHARACTER_SET=^@var{c}
6923 @cindex @option{^-gnati^/IDENTIFIER_CHARACTER_SET^} (@command{gcc})
6924
6925 @noindent
6926 Normally GNAT recognizes the Latin-1 character set in source program
6927 identifiers, as described in the Ada Reference Manual.
6928 This switch causes
6929 GNAT to recognize alternate character sets in identifiers. @var{c} is a
6930 single character ^^or word^ indicating the character set, as follows:
6931
6932 @table @code
6933 @item 1
6934 ISO 8859-1 (Latin-1) identifiers
6935
6936 @item 2
6937 ISO 8859-2 (Latin-2) letters allowed in identifiers
6938
6939 @item 3
6940 ISO 8859-3 (Latin-3) letters allowed in identifiers
6941
6942 @item 4
6943 ISO 8859-4 (Latin-4) letters allowed in identifiers
6944
6945 @item 5
6946 ISO 8859-5 (Cyrillic) letters allowed in identifiers
6947
6948 @item 9
6949 ISO 8859-15 (Latin-9) letters allowed in identifiers
6950
6951 @item ^p^PC^
6952 IBM PC letters (code page 437) allowed in identifiers
6953
6954 @item ^8^PC850^
6955 IBM PC letters (code page 850) allowed in identifiers
6956
6957 @item ^f^FULL_UPPER^
6958 Full upper-half codes allowed in identifiers
6959
6960 @item ^n^NO_UPPER^
6961 No upper-half codes allowed in identifiers
6962
6963 @item ^w^WIDE^
6964 Wide-character codes (that is, codes greater than 255)
6965 allowed in identifiers
6966 @end table
6967
6968 @xref{Foreign Language Representation}, for full details on the
6969 implementation of these character sets.
6970
6971 @item ^-gnatW^/WIDE_CHARACTER_ENCODING=^@var{e}
6972 @cindex @option{^-gnatW^/WIDE_CHARACTER_ENCODING^} (@command{gcc})
6973 Specify the method of encoding for wide characters.
6974 @var{e} is one of the following:
6975
6976 @table @code
6977
6978 @item ^h^HEX^
6979 Hex encoding (brackets coding also recognized)
6980
6981 @item ^u^UPPER^
6982 Upper half encoding (brackets encoding also recognized)
6983
6984 @item ^s^SHIFT_JIS^
6985 Shift/JIS encoding (brackets encoding also recognized)
6986
6987 @item ^e^EUC^
6988 EUC encoding (brackets encoding also recognized)
6989
6990 @item ^8^UTF8^
6991 UTF-8 encoding (brackets encoding also recognized)
6992
6993 @item ^b^BRACKETS^
6994 Brackets encoding only (default value)
6995 @end table
6996 For full details on these encoding
6997 methods see @ref{Wide Character Encodings}.
6998 Note that brackets coding is always accepted, even if one of the other
6999 options is specified, so for example @option{-gnatW8} specifies that both
7000 brackets and UTF-8 encodings will be recognized. The units that are
7001 with'ed directly or indirectly will be scanned using the specified
7002 representation scheme, and so if one of the non-brackets scheme is
7003 used, it must be used consistently throughout the program. However,
7004 since brackets encoding is always recognized, it may be conveniently
7005 used in standard libraries, allowing these libraries to be used with
7006 any of the available coding schemes.
7007 scheme.
7008
7009 If no @option{-gnatW?} parameter is present, then the default
7010 representation is normally Brackets encoding only. However, if the
7011 first three characters of the file are 16#EF# 16#BB# 16#BF# (the standard
7012 byte order mark or BOM for UTF-8), then these three characters are
7013 skipped and the default representation for the file is set to UTF-8.
7014
7015 Note that the wide character representation that is specified (explicitly
7016 or by default) for the main program also acts as the default encoding used
7017 for Wide_Text_IO files if not specifically overridden by a WCEM form
7018 parameter.
7019
7020 @end table
7021 @node File Naming Control
7022 @subsection File Naming Control
7023
7024 @table @option
7025 @item ^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{n}
7026 @cindex @option{-gnatk} (@command{gcc})
7027 Activates file name ``krunching''. @var{n}, a decimal integer in the range
7028 1-999, indicates the maximum allowable length of a file name (not
7029 including the @file{.ads} or @file{.adb} extension). The default is not
7030 to enable file name krunching.
7031
7032 For the source file naming rules, @xref{File Naming Rules}.
7033 @end table
7034
7035 @node Subprogram Inlining Control
7036 @subsection Subprogram Inlining Control
7037
7038 @table @option
7039 @c !sort!
7040 @item -gnatn
7041 @cindex @option{-gnatn} (@command{gcc})
7042 @ifclear vms
7043 The @code{n} here is intended to suggest the first syllable of the
7044 word ``inline''.
7045 @end ifclear
7046 GNAT recognizes and processes @code{Inline} pragmas. However, for the
7047 inlining to actually occur, optimization must be enabled. To enable
7048 inlining of subprograms specified by pragma @code{Inline},
7049 you must also specify this switch.
7050 In the absence of this switch, GNAT does not attempt
7051 inlining and does not need to access the bodies of
7052 subprograms for which @code{pragma Inline} is specified if they are not
7053 in the current unit.
7054
7055 If you specify this switch the compiler will access these bodies,
7056 creating an extra source dependency for the resulting object file, and
7057 where possible, the call will be inlined.
7058 For further details on when inlining is possible
7059 see @ref{Inlining of Subprograms}.
7060
7061 @item -gnatN
7062 @cindex @option{-gnatN} (@command{gcc})
7063 This switch activates front-end inlining which also
7064 generates additional dependencies.
7065
7066 When using a gcc-based back end (in practice this means using any version
7067 of GNAT other than the JGNAT, .NET or GNAAMP versions), then the use of
7068 @option{-gnatN} is deprecated, and the use of @option{-gnatn} is preferred.
7069 Historically front end inlining was more extensive than the gcc back end
7070 inlining, but that is no longer the case.
7071 @end table
7072
7073 @node Auxiliary Output Control
7074 @subsection Auxiliary Output Control
7075
7076 @table @option
7077 @item -gnatt
7078 @cindex @option{-gnatt} (@command{gcc})
7079 @cindex Writing internal trees
7080 @cindex Internal trees, writing to file
7081 Causes GNAT to write the internal tree for a unit to a file (with the
7082 extension @file{.adt}.
7083 This not normally required, but is used by separate analysis tools.
7084 Typically
7085 these tools do the necessary compilations automatically, so you should
7086 not have to specify this switch in normal operation.
7087 Note that the combination of switches @option{-gnatct}
7088 generates a tree in the form required by ASIS applications.
7089
7090 @item -gnatu
7091 @cindex @option{-gnatu} (@command{gcc})
7092 Print a list of units required by this compilation on @file{stdout}.
7093 The listing includes all units on which the unit being compiled depends
7094 either directly or indirectly.
7095
7096 @ifclear vms
7097 @item -pass-exit-codes
7098 @cindex @option{-pass-exit-codes} (@command{gcc})
7099 If this switch is not used, the exit code returned by @command{gcc} when
7100 compiling multiple files indicates whether all source files have
7101 been successfully used to generate object files or not.
7102
7103 When @option{-pass-exit-codes} is used, @command{gcc} exits with an extended
7104 exit status and allows an integrated development environment to better
7105 react to a compilation failure. Those exit status are:
7106
7107 @table @asis
7108 @item 5
7109 There was an error in at least one source file.
7110 @item 3
7111 At least one source file did not generate an object file.
7112 @item 2
7113 The compiler died unexpectedly (internal error for example).
7114 @item 0
7115 An object file has been generated for every source file.
7116 @end table
7117 @end ifclear
7118 @end table
7119
7120 @node Debugging Control
7121 @subsection Debugging Control
7122
7123 @table @option
7124 @c !sort!
7125 @cindex Debugging options
7126 @ifclear vms
7127 @item -gnatd@var{x}
7128 @cindex @option{-gnatd} (@command{gcc})
7129 Activate internal debugging switches. @var{x} is a letter or digit, or
7130 string of letters or digits, which specifies the type of debugging
7131 outputs desired. Normally these are used only for internal development
7132 or system debugging purposes. You can find full documentation for these
7133 switches in the body of the @code{Debug} unit in the compiler source
7134 file @file{debug.adb}.
7135 @end ifclear
7136
7137 @item -gnatG[=nn]
7138 @cindex @option{-gnatG} (@command{gcc})
7139 This switch causes the compiler to generate auxiliary output containing
7140 a pseudo-source listing of the generated expanded code. Like most Ada
7141 compilers, GNAT works by first transforming the high level Ada code into
7142 lower level constructs. For example, tasking operations are transformed
7143 into calls to the tasking run-time routines. A unique capability of GNAT
7144 is to list this expanded code in a form very close to normal Ada source.
7145 This is very useful in understanding the implications of various Ada
7146 usage on the efficiency of the generated code. There are many cases in
7147 Ada (e.g.@: the use of controlled types), where simple Ada statements can
7148 generate a lot of run-time code. By using @option{-gnatG} you can identify
7149 these cases, and consider whether it may be desirable to modify the coding
7150 approach to improve efficiency.
7151
7152 The optional parameter @code{nn} if present after -gnatG specifies an
7153 alternative maximum line length that overrides the normal default of 72.
7154 This value is in the range 40-999999, values less than 40 being silently
7155 reset to 40. The equal sign is optional.
7156
7157 The format of the output is very similar to standard Ada source, and is
7158 easily understood by an Ada programmer. The following special syntactic
7159 additions correspond to low level features used in the generated code that
7160 do not have any exact analogies in pure Ada source form. The following
7161 is a partial list of these special constructions. See the spec
7162 of package @code{Sprint} in file @file{sprint.ads} for a full list.
7163
7164 If the switch @option{-gnatL} is used in conjunction with
7165 @cindex @option{-gnatL} (@command{gcc})
7166 @option{-gnatG}, then the original source lines are interspersed
7167 in the expanded source (as comment lines with the original line number).
7168
7169 @table @code
7170 @item new @var{xxx} @r{[}storage_pool = @var{yyy}@r{]}
7171 Shows the storage pool being used for an allocator.
7172
7173 @item at end @var{procedure-name};
7174 Shows the finalization (cleanup) procedure for a scope.
7175
7176 @item (if @var{expr} then @var{expr} else @var{expr})
7177 Conditional expression equivalent to the @code{x?y:z} construction in C.
7178
7179 @item @var{target}^^^(@var{source})
7180 A conversion with floating-point truncation instead of rounding.
7181
7182 @item @var{target}?(@var{source})
7183 A conversion that bypasses normal Ada semantic checking. In particular
7184 enumeration types and fixed-point types are treated simply as integers.
7185
7186 @item @var{target}?^^^(@var{source})
7187 Combines the above two cases.
7188
7189 @item @var{x} #/ @var{y}
7190 @itemx @var{x} #mod @var{y}
7191 @itemx @var{x} #* @var{y}
7192 @itemx @var{x} #rem @var{y}
7193 A division or multiplication of fixed-point values which are treated as
7194 integers without any kind of scaling.
7195
7196 @item free @var{expr} @r{[}storage_pool = @var{xxx}@r{]}
7197 Shows the storage pool associated with a @code{free} statement.
7198
7199 @item [subtype or type declaration]
7200 Used to list an equivalent declaration for an internally generated
7201 type that is referenced elsewhere in the listing.
7202
7203 @c @item freeze @var{type-name} @ovar{actions}
7204 @c Expanding @ovar macro inline (explanation in macro def comments)
7205 @item freeze @var{type-name} @r{[}@var{actions}@r{]}
7206 Shows the point at which @var{type-name} is frozen, with possible
7207 associated actions to be performed at the freeze point.
7208
7209 @item reference @var{itype}
7210 Reference (and hence definition) to internal type @var{itype}.
7211
7212 @item @var{function-name}! (@var{arg}, @var{arg}, @var{arg})
7213 Intrinsic function call.
7214
7215 @item @var{label-name} : label
7216 Declaration of label @var{labelname}.
7217
7218 @item #$ @var{subprogram-name}
7219 An implicit call to a run-time support routine
7220 (to meet the requirement of H.3.1(9) in a
7221 convenient manner).
7222
7223 @item @var{expr} && @var{expr} && @var{expr} @dots{} && @var{expr}
7224 A multiple concatenation (same effect as @var{expr} & @var{expr} &
7225 @var{expr}, but handled more efficiently).
7226
7227 @item [constraint_error]
7228 Raise the @code{Constraint_Error} exception.
7229
7230 @item @var{expression}'reference
7231 A pointer to the result of evaluating @var{expression}.
7232
7233 @item @var{target-type}!(@var{source-expression})
7234 An unchecked conversion of @var{source-expression} to @var{target-type}.
7235
7236 @item [@var{numerator}/@var{denominator}]
7237 Used to represent internal real literals (that) have no exact
7238 representation in base 2-16 (for example, the result of compile time
7239 evaluation of the expression 1.0/27.0).
7240 @end table
7241
7242 @item -gnatD[=nn]
7243 @cindex @option{-gnatD} (@command{gcc})
7244 When used in conjunction with @option{-gnatG}, this switch causes
7245 the expanded source, as described above for
7246 @option{-gnatG} to be written to files with names
7247 @file{^xxx.dg^XXX_DG^}, where @file{xxx} is the normal file name,
7248 instead of to the standard output file. For
7249 example, if the source file name is @file{hello.adb}, then a file
7250 @file{^hello.adb.dg^HELLO.ADB_DG^} will be written. The debugging
7251 information generated by the @command{gcc} @option{^-g^/DEBUG^} switch
7252 will refer to the generated @file{^xxx.dg^XXX_DG^} file. This allows
7253 you to do source level debugging using the generated code which is
7254 sometimes useful for complex code, for example to find out exactly
7255 which part of a complex construction raised an exception. This switch
7256 also suppress generation of cross-reference information (see
7257 @option{-gnatx}) since otherwise the cross-reference information
7258 would refer to the @file{^.dg^.DG^} file, which would cause
7259 confusion since this is not the original source file.
7260
7261 Note that @option{-gnatD} actually implies @option{-gnatG}
7262 automatically, so it is not necessary to give both options.
7263 In other words @option{-gnatD} is equivalent to @option{-gnatDG}).
7264
7265 If the switch @option{-gnatL} is used in conjunction with
7266 @cindex @option{-gnatL} (@command{gcc})
7267 @option{-gnatDG}, then the original source lines are interspersed
7268 in the expanded source (as comment lines with the original line number).
7269
7270 The optional parameter @code{nn} if present after -gnatD specifies an
7271 alternative maximum line length that overrides the normal default of 72.
7272 This value is in the range 40-999999, values less than 40 being silently
7273 reset to 40. The equal sign is optional.
7274
7275 @item -gnatr
7276 @cindex @option{-gnatr} (@command{gcc})
7277 @cindex pragma Restrictions
7278 This switch causes pragma Restrictions to be treated as Restriction_Warnings
7279 so that violation of restrictions causes warnings rather than illegalities.
7280 This is useful during the development process when new restrictions are added
7281 or investigated. The switch also causes pragma Profile to be treated as
7282 Profile_Warnings, and pragma Restricted_Run_Time and pragma Ravenscar set
7283 restriction warnings rather than restrictions.
7284
7285 @ifclear vms
7286 @item -gnatR@r{[}0@r{|}1@r{|}2@r{|}3@r{[}s@r{]]}
7287 @cindex @option{-gnatR} (@command{gcc})
7288 This switch controls output from the compiler of a listing showing
7289 representation information for declared types and objects. For
7290 @option{-gnatR0}, no information is output (equivalent to omitting
7291 the @option{-gnatR} switch). For @option{-gnatR1} (which is the default,
7292 so @option{-gnatR} with no parameter has the same effect), size and alignment
7293 information is listed for declared array and record types. For
7294 @option{-gnatR2}, size and alignment information is listed for all
7295 declared types and objects. Finally @option{-gnatR3} includes symbolic
7296 expressions for values that are computed at run time for
7297 variant records. These symbolic expressions have a mostly obvious
7298 format with #n being used to represent the value of the n'th
7299 discriminant. See source files @file{repinfo.ads/adb} in the
7300 @code{GNAT} sources for full details on the format of @option{-gnatR3}
7301 output. If the switch is followed by an s (e.g.@: @option{-gnatR2s}), then
7302 the output is to a file with the name @file{^file.rep^file_REP^} where
7303 file is the name of the corresponding source file.
7304 @end ifclear
7305 @ifset vms
7306 @item /REPRESENTATION_INFO
7307 @cindex @option{/REPRESENTATION_INFO} (@command{gcc})
7308 This qualifier controls output from the compiler of a listing showing
7309 representation information for declared types and objects. For
7310 @option{/REPRESENTATION_INFO=NONE}, no information is output
7311 (equivalent to omitting the @option{/REPRESENTATION_INFO} qualifier).
7312 @option{/REPRESENTATION_INFO} without option is equivalent to
7313 @option{/REPRESENTATION_INFO=ARRAYS}.
7314 For @option{/REPRESENTATION_INFO=ARRAYS}, size and alignment
7315 information is listed for declared array and record types. For
7316 @option{/REPRESENTATION_INFO=OBJECTS}, size and alignment information
7317 is listed for all expression information for values that are computed
7318 at run time for variant records. These symbolic expressions have a mostly
7319 obvious format with #n being used to represent the value of the n'th
7320 discriminant. See source files @file{REPINFO.ADS/ADB} in the
7321 @code{GNAT} sources for full details on the format of
7322 @option{/REPRESENTATION_INFO=SYMBOLIC} output.
7323 If _FILE is added at the end of an option
7324 (e.g.@: @option{/REPRESENTATION_INFO=ARRAYS_FILE}),
7325 then the output is to a file with the name @file{file_REP} where
7326 file is the name of the corresponding source file.
7327 @end ifset
7328 Note that it is possible for record components to have zero size. In
7329 this case, the component clause uses an obvious extension of permitted
7330 Ada syntax, for example @code{at 0 range 0 .. -1}.
7331
7332 Representation information requires that code be generated (since it is the
7333 code generator that lays out complex data structures). If an attempt is made
7334 to output representation information when no code is generated, for example
7335 when a subunit is compiled on its own, then no information can be generated
7336 and the compiler outputs a message to this effect.
7337
7338 @item -gnatS
7339 @cindex @option{-gnatS} (@command{gcc})
7340 The use of the switch @option{-gnatS} for an
7341 Ada compilation will cause the compiler to output a
7342 representation of package Standard in a form very
7343 close to standard Ada. It is not quite possible to
7344 do this entirely in standard Ada (since new
7345 numeric base types cannot be created in standard
7346 Ada), but the output is easily
7347 readable to any Ada programmer, and is useful to
7348 determine the characteristics of target dependent
7349 types in package Standard.
7350
7351 @item -gnatx
7352 @cindex @option{-gnatx} (@command{gcc})
7353 Normally the compiler generates full cross-referencing information in
7354 the @file{ALI} file. This information is used by a number of tools,
7355 including @code{gnatfind} and @code{gnatxref}. The @option{-gnatx} switch
7356 suppresses this information. This saves some space and may slightly
7357 speed up compilation, but means that these tools cannot be used.
7358 @end table
7359
7360 @node Exception Handling Control
7361 @subsection Exception Handling Control
7362
7363 @noindent
7364 GNAT uses two methods for handling exceptions at run-time. The
7365 @code{setjmp/longjmp} method saves the context when entering
7366 a frame with an exception handler. Then when an exception is
7367 raised, the context can be restored immediately, without the
7368 need for tracing stack frames. This method provides very fast
7369 exception propagation, but introduces significant overhead for
7370 the use of exception handlers, even if no exception is raised.
7371
7372 The other approach is called ``zero cost'' exception handling.
7373 With this method, the compiler builds static tables to describe
7374 the exception ranges. No dynamic code is required when entering
7375 a frame containing an exception handler. When an exception is
7376 raised, the tables are used to control a back trace of the
7377 subprogram invocation stack to locate the required exception
7378 handler. This method has considerably poorer performance for
7379 the propagation of exceptions, but there is no overhead for
7380 exception handlers if no exception is raised. Note that in this
7381 mode and in the context of mixed Ada and C/C++ programming,
7382 to propagate an exception through a C/C++ code, the C/C++ code
7383 must be compiled with the @option{-funwind-tables} GCC's
7384 option.
7385
7386 The following switches may be used to control which of the
7387 two exception handling methods is used.
7388
7389 @table @option
7390 @c !sort!
7391
7392 @item --RTS=sjlj
7393 @cindex @option{--RTS=sjlj} (@command{gnatmake})
7394 This switch causes the setjmp/longjmp run-time (when available) to be used
7395 for exception handling. If the default
7396 mechanism for the target is zero cost exceptions, then
7397 this switch can be used to modify this default, and must be
7398 used for all units in the partition.
7399 This option is rarely used. One case in which it may be
7400 advantageous is if you have an application where exception
7401 raising is common and the overall performance of the
7402 application is improved by favoring exception propagation.
7403
7404 @item --RTS=zcx
7405 @cindex @option{--RTS=zcx} (@command{gnatmake})
7406 @cindex Zero Cost Exceptions
7407 This switch causes the zero cost approach to be used
7408 for exception handling. If this is the default mechanism for the
7409 target (see below), then this switch is unneeded. If the default
7410 mechanism for the target is setjmp/longjmp exceptions, then
7411 this switch can be used to modify this default, and must be
7412 used for all units in the partition.
7413 This option can only be used if the zero cost approach
7414 is available for the target in use, otherwise it will generate an error.
7415 @end table
7416
7417 @noindent
7418 The same option @option{--RTS} must be used both for @command{gcc}
7419 and @command{gnatbind}. Passing this option to @command{gnatmake}
7420 (@pxref{Switches for gnatmake}) will ensure the required consistency
7421 through the compilation and binding steps.
7422
7423 @node Units to Sources Mapping Files
7424 @subsection Units to Sources Mapping Files
7425
7426 @table @option
7427
7428 @item -gnatem=@var{path}
7429 @cindex @option{-gnatem} (@command{gcc})
7430 A mapping file is a way to communicate to the compiler two mappings:
7431 from unit names to file names (without any directory information) and from
7432 file names to path names (with full directory information). These mappings
7433 are used by the compiler to short-circuit the path search.
7434
7435 The use of mapping files is not required for correct operation of the
7436 compiler, but mapping files can improve efficiency, particularly when
7437 sources are read over a slow network connection. In normal operation,
7438 you need not be concerned with the format or use of mapping files,
7439 and the @option{-gnatem} switch is not a switch that you would use
7440 explicitly. It is intended primarily for use by automatic tools such as
7441 @command{gnatmake} running under the project file facility. The
7442 description here of the format of mapping files is provided
7443 for completeness and for possible use by other tools.
7444
7445 A mapping file is a sequence of sets of three lines. In each set, the
7446 first line is the unit name, in lower case, with @code{%s} appended
7447 for specs and @code{%b} appended for bodies; the second line is the
7448 file name; and the third line is the path name.
7449
7450 Example:
7451 @smallexample
7452 main%b
7453 main.2.ada
7454 /gnat/project1/sources/main.2.ada
7455 @end smallexample
7456
7457 When the switch @option{-gnatem} is specified, the compiler will
7458 create in memory the two mappings from the specified file. If there is
7459 any problem (nonexistent file, truncated file or duplicate entries),
7460 no mapping will be created.
7461
7462 Several @option{-gnatem} switches may be specified; however, only the
7463 last one on the command line will be taken into account.
7464
7465 When using a project file, @command{gnatmake} creates a temporary
7466 mapping file and communicates it to the compiler using this switch.
7467
7468 @end table
7469
7470 @node Integrated Preprocessing
7471 @subsection Integrated Preprocessing
7472
7473 @noindent
7474 GNAT sources may be preprocessed immediately before compilation.
7475 In this case, the actual
7476 text of the source is not the text of the source file, but is derived from it
7477 through a process called preprocessing. Integrated preprocessing is specified
7478 through switches @option{-gnatep} and/or @option{-gnateD}. @option{-gnatep}
7479 indicates, through a text file, the preprocessing data to be used.
7480 @option{-gnateD} specifies or modifies the values of preprocessing symbol.
7481
7482 @noindent
7483 Note that when integrated preprocessing is used, the output from the
7484 preprocessor is not written to any external file. Instead it is passed
7485 internally to the compiler. If you need to preserve the result of
7486 preprocessing in a file, then you should use @command{gnatprep}
7487 to perform the desired preprocessing in stand-alone mode.
7488
7489 @noindent
7490 It is recommended that @command{gnatmake} switch ^-s^/SWITCH_CHECK^ should be
7491 used when Integrated Preprocessing is used. The reason is that preprocessing
7492 with another Preprocessing Data file without changing the sources will
7493 not trigger recompilation without this switch.
7494
7495 @noindent
7496 Note that @command{gnatmake} switch ^-m^/MINIMAL_RECOMPILATION^ will almost
7497 always trigger recompilation for sources that are preprocessed,
7498 because @command{gnatmake} cannot compute the checksum of the source after
7499 preprocessing.
7500
7501 @noindent
7502 The actual preprocessing function is described in details in section
7503 @ref{Preprocessing Using gnatprep}. This section only describes how integrated
7504 preprocessing is triggered and parameterized.
7505
7506 @table @code
7507
7508 @item -gnatep=@var{file}
7509 @cindex @option{-gnatep} (@command{gcc})
7510 This switch indicates to the compiler the file name (without directory
7511 information) of the preprocessor data file to use. The preprocessor data file
7512 should be found in the source directories.
7513
7514 @noindent
7515 A preprocessing data file is a text file with significant lines indicating
7516 how should be preprocessed either a specific source or all sources not
7517 mentioned in other lines. A significant line is a nonempty, non-comment line.
7518 Comments are similar to Ada comments.
7519
7520 @noindent
7521 Each significant line starts with either a literal string or the character '*'.
7522 A literal string is the file name (without directory information) of the source
7523 to preprocess. A character '*' indicates the preprocessing for all the sources
7524 that are not specified explicitly on other lines (order of the lines is not
7525 significant). It is an error to have two lines with the same file name or two
7526 lines starting with the character '*'.
7527
7528 @noindent
7529 After the file name or the character '*', another optional literal string
7530 indicating the file name of the definition file to be used for preprocessing
7531 (@pxref{Form of Definitions File}). The definition files are found by the
7532 compiler in one of the source directories. In some cases, when compiling
7533 a source in a directory other than the current directory, if the definition
7534 file is in the current directory, it may be necessary to add the current
7535 directory as a source directory through switch ^-I.^/SEARCH=[]^, otherwise
7536 the compiler would not find the definition file.
7537
7538 @noindent
7539 Then, optionally, ^switches^switches^ similar to those of @code{gnatprep} may
7540 be found. Those ^switches^switches^ are:
7541
7542 @table @code
7543
7544 @item -b
7545 Causes both preprocessor lines and the lines deleted by
7546 preprocessing to be replaced by blank lines, preserving the line number.
7547 This ^switch^switch^ is always implied; however, if specified after @option{-c}
7548 it cancels the effect of @option{-c}.
7549
7550 @item -c
7551 Causes both preprocessor lines and the lines deleted
7552 by preprocessing to be retained as comments marked
7553 with the special string ``@code{--! }''.
7554
7555 @item -Dsymbol=value
7556 Define or redefine a symbol, associated with value. A symbol is an Ada
7557 identifier, or an Ada reserved word, with the exception of @code{if},
7558 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7559 @code{value} is either a literal string, an Ada identifier or any Ada reserved
7560 word. A symbol declared with this ^switch^switch^ replaces a symbol with the
7561 same name defined in a definition file.
7562
7563 @item -s
7564 Causes a sorted list of symbol names and values to be
7565 listed on the standard output file.
7566
7567 @item -u
7568 Causes undefined symbols to be treated as having the value @code{FALSE}
7569 in the context
7570 of a preprocessor test. In the absence of this option, an undefined symbol in
7571 a @code{#if} or @code{#elsif} test will be treated as an error.
7572
7573 @end table
7574
7575 @noindent
7576 Examples of valid lines in a preprocessor data file:
7577
7578 @smallexample
7579 "toto.adb" "prep.def" -u
7580 -- preprocess "toto.adb", using definition file "prep.def",
7581 -- undefined symbol are False.
7582
7583 * -c -DVERSION=V101
7584 -- preprocess all other sources without a definition file;
7585 -- suppressed lined are commented; symbol VERSION has the value V101.
7586
7587 "titi.adb" "prep2.def" -s
7588 -- preprocess "titi.adb", using definition file "prep2.def";
7589 -- list all symbols with their values.
7590 @end smallexample
7591
7592 @item ^-gnateD^/DATA_PREPROCESSING=^symbol@r{[}=value@r{]}
7593 @cindex @option{-gnateD} (@command{gcc})
7594 Define or redefine a preprocessing symbol, associated with value. If no value
7595 is given on the command line, then the value of the symbol is @code{True}.
7596 A symbol is an identifier, following normal Ada (case-insensitive)
7597 rules for its syntax, and value is any sequence (including an empty sequence)
7598 of characters from the set (letters, digits, period, underline).
7599 Ada reserved words may be used as symbols, with the exceptions of @code{if},
7600 @code{else}, @code{elsif}, @code{end}, @code{and}, @code{or} and @code{then}.
7601
7602 @noindent
7603 A symbol declared with this ^switch^switch^ on the command line replaces a
7604 symbol with the same name either in a definition file or specified with a
7605 ^switch^switch^ -D in the preprocessor data file.
7606
7607 @noindent
7608 This switch is similar to switch @option{^-D^/ASSOCIATE^} of @code{gnatprep}.
7609
7610 @item -gnateG
7611 When integrated preprocessing is performed and the preprocessor modifies
7612 the source text, write the result of this preprocessing into a file
7613 <source>^.prep^_prep^.
7614
7615 @end table
7616
7617 @node Code Generation Control
7618 @subsection Code Generation Control
7619
7620 @noindent
7621
7622 The GCC technology provides a wide range of target dependent
7623 @option{-m} switches for controlling
7624 details of code generation with respect to different versions of
7625 architectures. This includes variations in instruction sets (e.g.@:
7626 different members of the power pc family), and different requirements
7627 for optimal arrangement of instructions (e.g.@: different members of
7628 the x86 family). The list of available @option{-m} switches may be
7629 found in the GCC documentation.
7630
7631 Use of these @option{-m} switches may in some cases result in improved
7632 code performance.
7633
7634 The GNAT Pro technology is tested and qualified without any
7635 @option{-m} switches,
7636 so generally the most reliable approach is to avoid the use of these
7637 switches. However, we generally expect most of these switches to work
7638 successfully with GNAT Pro, and many customers have reported successful
7639 use of these options.
7640
7641 Our general advice is to avoid the use of @option{-m} switches unless
7642 special needs lead to requirements in this area. In particular,
7643 there is no point in using @option{-m} switches to improve performance
7644 unless you actually see a performance improvement.
7645
7646 @ifset vms
7647 @node Return Codes
7648 @subsection Return Codes
7649 @cindex Return Codes
7650 @cindex @option{/RETURN_CODES=VMS}
7651
7652 @noindent
7653 On VMS, GNAT compiled programs return POSIX-style codes by default,
7654 e.g.@: @option{/RETURN_CODES=POSIX}.
7655
7656 To enable VMS style return codes, use GNAT BIND and LINK with the option
7657 @option{/RETURN_CODES=VMS}. For example:
7658
7659 @smallexample
7660 GNAT BIND MYMAIN.ALI /RETURN_CODES=VMS
7661 GNAT LINK MYMAIN.ALI /RETURN_CODES=VMS
7662 @end smallexample
7663
7664 @noindent
7665 Programs built with /RETURN_CODES=VMS are suitable to be called in
7666 VMS DCL scripts. Programs compiled with the default /RETURN_CODES=POSIX
7667 are suitable for spawning with appropriate GNAT RTL routines.
7668
7669 @end ifset
7670
7671 @node Search Paths and the Run-Time Library (RTL)
7672 @section Search Paths and the Run-Time Library (RTL)
7673
7674 @noindent
7675 With the GNAT source-based library system, the compiler must be able to
7676 find source files for units that are needed by the unit being compiled.
7677 Search paths are used to guide this process.
7678
7679 The compiler compiles one source file whose name must be given
7680 explicitly on the command line. In other words, no searching is done
7681 for this file. To find all other source files that are needed (the most
7682 common being the specs of units), the compiler examines the following
7683 directories, in the following order:
7684
7685 @enumerate
7686 @item
7687 The directory containing the source file of the main unit being compiled
7688 (the file name on the command line).
7689
7690 @item
7691 Each directory named by an @option{^-I^/SOURCE_SEARCH^} switch given on the
7692 @command{gcc} command line, in the order given.
7693
7694 @item
7695 @findex ADA_PRJ_INCLUDE_FILE
7696 Each of the directories listed in the text file whose name is given
7697 by the @env{ADA_PRJ_INCLUDE_FILE} ^environment variable^logical name^.
7698
7699 @noindent
7700 @env{ADA_PRJ_INCLUDE_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
7701 driver when project files are used. It should not normally be set
7702 by other means.
7703
7704 @item
7705 @findex ADA_INCLUDE_PATH
7706 Each of the directories listed in the value of the
7707 @env{ADA_INCLUDE_PATH} ^environment variable^logical name^.
7708 @ifclear vms
7709 Construct this value
7710 exactly as the @env{PATH} environment variable: a list of directory
7711 names separated by colons (semicolons when working with the NT version).
7712 @end ifclear
7713 @ifset vms
7714 Normally, define this value as a logical name containing a comma separated
7715 list of directory names.
7716
7717 This variable can also be defined by means of an environment string
7718 (an argument to the HP C exec* set of functions).
7719
7720 Logical Name:
7721 @smallexample
7722 DEFINE ANOTHER_PATH FOO:[BAG]
7723 DEFINE ADA_INCLUDE_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
7724 @end smallexample
7725
7726 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
7727 first, followed by the standard Ada
7728 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADAINCLUDE].
7729 If this is not redefined, the user will obtain the HP Ada 83 IO packages
7730 (Text_IO, Sequential_IO, etc)
7731 instead of the standard Ada packages. Thus, in order to get the standard Ada
7732 packages by default, ADA_INCLUDE_PATH must be redefined.
7733 @end ifset
7734
7735 @item
7736 The content of the @file{ada_source_path} file which is part of the GNAT
7737 installation tree and is used to store standard libraries such as the
7738 GNAT Run Time Library (RTL) source files.
7739 @ifclear vms
7740 @ref{Installing a library}
7741 @end ifclear
7742 @end enumerate
7743
7744 @noindent
7745 Specifying the switch @option{^-I-^/NOCURRENT_DIRECTORY^}
7746 inhibits the use of the directory
7747 containing the source file named in the command line. You can still
7748 have this directory on your search path, but in this case it must be
7749 explicitly requested with a @option{^-I^/SOURCE_SEARCH^} switch.
7750
7751 Specifying the switch @option{-nostdinc}
7752 inhibits the search of the default location for the GNAT Run Time
7753 Library (RTL) source files.
7754
7755 The compiler outputs its object files and ALI files in the current
7756 working directory.
7757 @ifclear vms
7758 Caution: The object file can be redirected with the @option{-o} switch;
7759 however, @command{gcc} and @code{gnat1} have not been coordinated on this
7760 so the @file{ALI} file will not go to the right place. Therefore, you should
7761 avoid using the @option{-o} switch.
7762 @end ifclear
7763
7764 @findex System.IO
7765 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
7766 children make up the GNAT RTL, together with the simple @code{System.IO}
7767 package used in the @code{"Hello World"} example. The sources for these units
7768 are needed by the compiler and are kept together in one directory. Not
7769 all of the bodies are needed, but all of the sources are kept together
7770 anyway. In a normal installation, you need not specify these directory
7771 names when compiling or binding. Either the environment variables or
7772 the built-in defaults cause these files to be found.
7773
7774 In addition to the language-defined hierarchies (@code{System}, @code{Ada} and
7775 @code{Interfaces}), the GNAT distribution provides a fourth hierarchy,
7776 consisting of child units of @code{GNAT}. This is a collection of generally
7777 useful types, subprograms, etc. @xref{Top, GNAT Reference Manual, About
7778 This Guid, gnat_rm, GNAT Reference Manual}, for further details.
7779
7780 Besides simplifying access to the RTL, a major use of search paths is
7781 in compiling sources from multiple directories. This can make
7782 development environments much more flexible.
7783
7784 @node Order of Compilation Issues
7785 @section Order of Compilation Issues
7786
7787 @noindent
7788 If, in our earlier example, there was a spec for the @code{hello}
7789 procedure, it would be contained in the file @file{hello.ads}; yet this
7790 file would not have to be explicitly compiled. This is the result of the
7791 model we chose to implement library management. Some of the consequences
7792 of this model are as follows:
7793
7794 @itemize @bullet
7795 @item
7796 There is no point in compiling specs (except for package
7797 specs with no bodies) because these are compiled as needed by clients. If
7798 you attempt a useless compilation, you will receive an error message.
7799 It is also useless to compile subunits because they are compiled as needed
7800 by the parent.
7801
7802 @item
7803 There are no order of compilation requirements: performing a
7804 compilation never obsoletes anything. The only way you can obsolete
7805 something and require recompilations is to modify one of the
7806 source files on which it depends.
7807
7808 @item
7809 There is no library as such, apart from the ALI files
7810 (@pxref{The Ada Library Information Files}, for information on the format
7811 of these files). For now we find it convenient to create separate ALI files,
7812 but eventually the information therein may be incorporated into the object
7813 file directly.
7814
7815 @item
7816 When you compile a unit, the source files for the specs of all units
7817 that it @code{with}'s, all its subunits, and the bodies of any generics it
7818 instantiates must be available (reachable by the search-paths mechanism
7819 described above), or you will receive a fatal error message.
7820 @end itemize
7821
7822 @node Examples
7823 @section Examples
7824
7825 @noindent
7826 The following are some typical Ada compilation command line examples:
7827
7828 @table @code
7829 @item $ gcc -c xyz.adb
7830 Compile body in file @file{xyz.adb} with all default options.
7831
7832 @ifclear vms
7833 @item $ gcc -c -O2 -gnata xyz-def.adb
7834 @end ifclear
7835 @ifset vms
7836 @item $ GNAT COMPILE /OPTIMIZE=ALL -gnata xyz-def.adb
7837 @end ifset
7838
7839 Compile the child unit package in file @file{xyz-def.adb} with extensive
7840 optimizations, and pragma @code{Assert}/@code{Debug} statements
7841 enabled.
7842
7843 @item $ gcc -c -gnatc abc-def.adb
7844 Compile the subunit in file @file{abc-def.adb} in semantic-checking-only
7845 mode.
7846 @end table
7847
7848 @node Binding Using gnatbind
7849 @chapter Binding Using @code{gnatbind}
7850 @findex gnatbind
7851
7852 @menu
7853 * Running gnatbind::
7854 * Switches for gnatbind::
7855 * Command-Line Access::
7856 * Search Paths for gnatbind::
7857 * Examples of gnatbind Usage::
7858 @end menu
7859
7860 @noindent
7861 This chapter describes the GNAT binder, @code{gnatbind}, which is used
7862 to bind compiled GNAT objects.
7863
7864 Note: to invoke @code{gnatbind} with a project file, use the @code{gnat}
7865 driver (see @ref{The GNAT Driver and Project Files}).
7866
7867 The @code{gnatbind} program performs four separate functions:
7868
7869 @enumerate
7870 @item
7871 Checks that a program is consistent, in accordance with the rules in
7872 Chapter 10 of the Ada Reference Manual. In particular, error
7873 messages are generated if a program uses inconsistent versions of a
7874 given unit.
7875
7876 @item
7877 Checks that an acceptable order of elaboration exists for the program
7878 and issues an error message if it cannot find an order of elaboration
7879 that satisfies the rules in Chapter 10 of the Ada Language Manual.
7880
7881 @item
7882 Generates a main program incorporating the given elaboration order.
7883 This program is a small Ada package (body and spec) that
7884 must be subsequently compiled
7885 using the GNAT compiler. The necessary compilation step is usually
7886 performed automatically by @command{gnatlink}. The two most important
7887 functions of this program
7888 are to call the elaboration routines of units in an appropriate order
7889 and to call the main program.
7890
7891 @item
7892 Determines the set of object files required by the given main program.
7893 This information is output in the forms of comments in the generated program,
7894 to be read by the @command{gnatlink} utility used to link the Ada application.
7895 @end enumerate
7896
7897 @node Running gnatbind
7898 @section Running @code{gnatbind}
7899
7900 @noindent
7901 The form of the @code{gnatbind} command is
7902
7903 @smallexample
7904 @c $ gnatbind @ovar{switches} @var{mainprog}@r{[}.ali@r{]} @ovar{switches}
7905 @c Expanding @ovar macro inline (explanation in macro def comments)
7906 $ gnatbind @r{[}@var{switches}@r{]} @var{mainprog}@r{[}.ali@r{]} @r{[}@var{switches}@r{]}
7907 @end smallexample
7908
7909 @noindent
7910 where @file{@var{mainprog}.adb} is the Ada file containing the main program
7911 unit body. If no switches are specified, @code{gnatbind} constructs an Ada
7912 package in two files whose names are
7913 @file{b~@var{mainprog}.ads}, and @file{b~@var{mainprog}.adb}.
7914 For example, if given the
7915 parameter @file{hello.ali}, for a main program contained in file
7916 @file{hello.adb}, the binder output files would be @file{b~hello.ads}
7917 and @file{b~hello.adb}.
7918
7919 When doing consistency checking, the binder takes into consideration
7920 any source files it can locate. For example, if the binder determines
7921 that the given main program requires the package @code{Pack}, whose
7922 @file{.ALI}
7923 file is @file{pack.ali} and whose corresponding source spec file is
7924 @file{pack.ads}, it attempts to locate the source file @file{pack.ads}
7925 (using the same search path conventions as previously described for the
7926 @command{gcc} command). If it can locate this source file, it checks that
7927 the time stamps
7928 or source checksums of the source and its references to in @file{ALI} files
7929 match. In other words, any @file{ALI} files that mentions this spec must have
7930 resulted from compiling this version of the source file (or in the case
7931 where the source checksums match, a version close enough that the
7932 difference does not matter).
7933
7934 @cindex Source files, use by binder
7935 The effect of this consistency checking, which includes source files, is
7936 that the binder ensures that the program is consistent with the latest
7937 version of the source files that can be located at bind time. Editing a
7938 source file without compiling files that depend on the source file cause
7939 error messages to be generated by the binder.
7940
7941 For example, suppose you have a main program @file{hello.adb} and a
7942 package @code{P}, from file @file{p.ads} and you perform the following
7943 steps:
7944
7945 @enumerate
7946 @item
7947 Enter @code{gcc -c hello.adb} to compile the main program.
7948
7949 @item
7950 Enter @code{gcc -c p.ads} to compile package @code{P}.
7951
7952 @item
7953 Edit file @file{p.ads}.
7954
7955 @item
7956 Enter @code{gnatbind hello}.
7957 @end enumerate
7958
7959 @noindent
7960 At this point, the file @file{p.ali} contains an out-of-date time stamp
7961 because the file @file{p.ads} has been edited. The attempt at binding
7962 fails, and the binder generates the following error messages:
7963
7964 @smallexample
7965 error: "hello.adb" must be recompiled ("p.ads" has been modified)
7966 error: "p.ads" has been modified and must be recompiled
7967 @end smallexample
7968
7969 @noindent
7970 Now both files must be recompiled as indicated, and then the bind can
7971 succeed, generating a main program. You need not normally be concerned
7972 with the contents of this file, but for reference purposes a sample
7973 binder output file is given in @ref{Example of Binder Output File}.
7974
7975 In most normal usage, the default mode of @command{gnatbind} which is to
7976 generate the main package in Ada, as described in the previous section.
7977 In particular, this means that any Ada programmer can read and understand
7978 the generated main program. It can also be debugged just like any other
7979 Ada code provided the @option{^-g^/DEBUG^} switch is used for
7980 @command{gnatbind} and @command{gnatlink}.
7981
7982 However for some purposes it may be convenient to generate the main
7983 program in C rather than Ada. This may for example be helpful when you
7984 are generating a mixed language program with the main program in C. The
7985 GNAT compiler itself is an example.
7986 The use of the @option{^-C^/BIND_FILE=C^} switch
7987 for both @code{gnatbind} and @command{gnatlink} will cause the program to
7988 be generated in C (and compiled using the gnu C compiler).
7989
7990 @node Switches for gnatbind
7991 @section Switches for @command{gnatbind}
7992
7993 @noindent
7994 The following switches are available with @code{gnatbind}; details will
7995 be presented in subsequent sections.
7996
7997 @menu
7998 * Consistency-Checking Modes::
7999 * Binder Error Message Control::
8000 * Elaboration Control::
8001 * Output Control::
8002 * Binding with Non-Ada Main Programs::
8003 * Binding Programs with No Main Subprogram::
8004 @end menu
8005
8006 @table @option
8007 @c !sort!
8008
8009 @item --version
8010 @cindex @option{--version} @command{gnatbind}
8011 Display Copyright and version, then exit disregarding all other options.
8012
8013 @item --help
8014 @cindex @option{--help} @command{gnatbind}
8015 If @option{--version} was not used, display usage, then exit disregarding
8016 all other options.
8017
8018 @item -a
8019 @cindex @option{-a} @command{gnatbind}
8020 Indicates that, if supported by the platform, the adainit procedure should
8021 be treated as an initialisation routine by the linker (a constructor). This
8022 is intended to be used by the Project Manager to automatically initialize
8023 shared Stand-Alone Libraries.
8024
8025 @item ^-aO^/OBJECT_SEARCH^
8026 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatbind})
8027 Specify directory to be searched for ALI files.
8028
8029 @item ^-aI^/SOURCE_SEARCH^
8030 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
8031 Specify directory to be searched for source file.
8032
8033 @item ^-A^/BIND_FILE=ADA^
8034 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatbind})
8035 Generate binder program in Ada (default)
8036
8037 @item ^-b^/REPORT_ERRORS=BRIEF^
8038 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@command{gnatbind})
8039 Generate brief messages to @file{stderr} even if verbose mode set.
8040
8041 @item ^-c^/NOOUTPUT^
8042 @cindex @option{^-c^/NOOUTPUT^} (@command{gnatbind})
8043 Check only, no generation of binder output file.
8044
8045 @item ^-C^/BIND_FILE=C^
8046 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatbind})
8047 Generate binder program in C
8048
8049 @item ^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
8050 @cindex @option{^-d^/DEFAULT_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}} (@command{gnatbind})
8051 This switch can be used to change the default task stack size value
8052 to a specified size @var{nn}, which is expressed in bytes by default, or
8053 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
8054 with @var{m}.
8055 In the absence of a @samp{@r{[}k@r{|}m@r{]}} suffix, this switch is equivalent,
8056 in effect, to completing all task specs with
8057 @smallexample @c ada
8058 pragma Storage_Size (nn);
8059 @end smallexample
8060 When they do not already have such a pragma.
8061
8062 @item ^-D^/DEFAULT_SECONDARY_STACK_SIZE=^@var{nn}@r{[}k@r{|}m@r{]}
8063 @cindex @option{^-D^/DEFAULT_SECONDARY_STACK_SIZE=nnnnn^} (@command{gnatbind})
8064 This switch can be used to change the default secondary stack size value
8065 to a specified size @var{nn}, which is expressed in bytes by default, or
8066 in kilobytes when suffixed with @var{k} or in megabytes when suffixed
8067 with @var{m}.
8068
8069 The secondary stack is used to deal with functions that return a variable
8070 sized result, for example a function returning an unconstrained
8071 String. There are two ways in which this secondary stack is allocated.
8072
8073 For most targets, the secondary stack is growing on demand and is allocated
8074 as a chain of blocks in the heap. The -D option is not very
8075 relevant. It only give some control over the size of the allocated
8076 blocks (whose size is the minimum of the default secondary stack size value,
8077 and the actual size needed for the current allocation request).
8078
8079 For certain targets, notably VxWorks 653,
8080 the secondary stack is allocated by carving off a fixed ratio chunk of the
8081 primary task stack. The -D option is used to define the
8082 size of the environment task's secondary stack.
8083
8084 @item ^-e^/ELABORATION_DEPENDENCIES^
8085 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@command{gnatbind})
8086 Output complete list of elaboration-order dependencies.
8087
8088 @item ^-E^/STORE_TRACEBACKS^
8089 @cindex @option{^-E^/STORE_TRACEBACKS^} (@command{gnatbind})
8090 Store tracebacks in exception occurrences when the target supports it.
8091 This is the default with the zero cost exception mechanism.
8092 @ignore
8093 @c The following may get moved to an appendix
8094 This option is currently supported on the following targets:
8095 all x86 ports, Solaris, Windows, HP-UX, AIX, PowerPC VxWorks and Alpha VxWorks.
8096 @end ignore
8097 See also the packages @code{GNAT.Traceback} and
8098 @code{GNAT.Traceback.Symbolic} for more information.
8099 @ifclear vms
8100 Note that on x86 ports, you must not use @option{-fomit-frame-pointer}
8101 @command{gcc} option.
8102 @end ifclear
8103
8104 @item ^-F^/FORCE_ELABS_FLAGS^
8105 @cindex @option{^-F^/FORCE_ELABS_FLAGS^} (@command{gnatbind})
8106 Force the checks of elaboration flags. @command{gnatbind} does not normally
8107 generate checks of elaboration flags for the main executable, except when
8108 a Stand-Alone Library is used. However, there are cases when this cannot be
8109 detected by gnatbind. An example is importing an interface of a Stand-Alone
8110 Library through a pragma Import and only specifying through a linker switch
8111 this Stand-Alone Library. This switch is used to guarantee that elaboration
8112 flag checks are generated.
8113
8114 @item ^-h^/HELP^
8115 @cindex @option{^-h^/HELP^} (@command{gnatbind})
8116 Output usage (help) information
8117
8118 @item ^-I^/SEARCH^
8119 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
8120 Specify directory to be searched for source and ALI files.
8121
8122 @item ^-I-^/NOCURRENT_DIRECTORY^
8123 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatbind})
8124 Do not look for sources in the current directory where @code{gnatbind} was
8125 invoked, and do not look for ALI files in the directory containing the
8126 ALI file named in the @code{gnatbind} command line.
8127
8128 @item ^-l^/ORDER_OF_ELABORATION^
8129 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@command{gnatbind})
8130 Output chosen elaboration order.
8131
8132 @item ^-L@var{xxx}^/BUILD_LIBRARY=@var{xxx}^
8133 @cindex @option{^-L^/BUILD_LIBRARY^} (@command{gnatbind})
8134 Bind the units for library building. In this case the adainit and
8135 adafinal procedures (@pxref{Binding with Non-Ada Main Programs})
8136 are renamed to ^@var{xxx}init^@var{XXX}INIT^ and
8137 ^@var{xxx}final^@var{XXX}FINAL^.
8138 Implies ^-n^/NOCOMPILE^.
8139 @ifclear vms
8140 (@xref{GNAT and Libraries}, for more details.)
8141 @end ifclear
8142 @ifset vms
8143 On OpenVMS, these init and final procedures are exported in uppercase
8144 letters. For example if /BUILD_LIBRARY=toto is used, the exported name of
8145 the init procedure will be "TOTOINIT" and the exported name of the final
8146 procedure will be "TOTOFINAL".
8147 @end ifset
8148
8149 @item ^-Mxyz^/RENAME_MAIN=xyz^
8150 @cindex @option{^-M^/RENAME_MAIN^} (@command{gnatbind})
8151 Rename generated main program from main to xyz. This option is
8152 supported on cross environments only.
8153
8154 @item ^-m^/ERROR_LIMIT=^@var{n}
8155 @cindex @option{^-m^/ERROR_LIMIT^} (@command{gnatbind})
8156 Limit number of detected errors or warnings to @var{n}, where @var{n} is
8157 in the range 1..999999. The default value if no switch is
8158 given is 9999. If the number of warnings reaches this limit, then a
8159 message is output and further warnings are suppressed, the bind
8160 continues in this case. If the number of errors reaches this
8161 limit, then a message is output and the bind is abandoned.
8162 A value of zero means that no limit is enforced. The equal
8163 sign is optional.
8164
8165 @ifset unw
8166 Furthermore, under Windows, the sources pointed to by the libraries path
8167 set in the registry are not searched for.
8168 @end ifset
8169
8170 @item ^-n^/NOMAIN^
8171 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
8172 No main program.
8173
8174 @item -nostdinc
8175 @cindex @option{-nostdinc} (@command{gnatbind})
8176 Do not look for sources in the system default directory.
8177
8178 @item -nostdlib
8179 @cindex @option{-nostdlib} (@command{gnatbind})
8180 Do not look for library files in the system default directory.
8181
8182 @item --RTS=@var{rts-path}
8183 @cindex @option{--RTS} (@code{gnatbind})
8184 Specifies the default location of the runtime library. Same meaning as the
8185 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
8186
8187 @item ^-o ^/OUTPUT=^@var{file}
8188 @cindex @option{^-o ^/OUTPUT^} (@command{gnatbind})
8189 Name the output file @var{file} (default is @file{b~@var{xxx}.adb}).
8190 Note that if this option is used, then linking must be done manually,
8191 gnatlink cannot be used.
8192
8193 @item ^-O^/OBJECT_LIST^
8194 @cindex @option{^-O^/OBJECT_LIST^} (@command{gnatbind})
8195 Output object list.
8196
8197 @item ^-p^/PESSIMISTIC_ELABORATION^
8198 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@command{gnatbind})
8199 Pessimistic (worst-case) elaboration order
8200
8201 @item ^-R^-R^
8202 @cindex @option{^-R^-R^} (@command{gnatbind})
8203 Output closure source list.
8204
8205 @item ^-s^/READ_SOURCES=ALL^
8206 @cindex @option{^-s^/READ_SOURCES=ALL^} (@command{gnatbind})
8207 Require all source files to be present.
8208
8209 @item ^-S@var{xxx}^/INITIALIZE_SCALARS=@var{xxx}^
8210 @cindex @option{^-S^/INITIALIZE_SCALARS^} (@command{gnatbind})
8211 Specifies the value to be used when detecting uninitialized scalar
8212 objects with pragma Initialize_Scalars.
8213 The @var{xxx} ^string specified with the switch^option^ may be either
8214 @itemize @bullet
8215 @item ``@option{^in^INVALID^}'' requesting an invalid value where possible
8216 @item ``@option{^lo^LOW^}'' for the lowest possible value
8217 @item ``@option{^hi^HIGH^}'' for the highest possible value
8218 @item ``@option{@var{xx}}'' for a value consisting of repeated bytes with the
8219 value @code{16#@var{xx}#} (i.e., @var{xx} is a string of two hexadecimal digits).
8220 @end itemize
8221
8222 In addition, you can specify @option{-Sev} to indicate that the value is
8223 to be set at run time. In this case, the program will look for an environment
8224 @cindex GNAT_INIT_SCALARS
8225 variable of the form @env{GNAT_INIT_SCALARS=@var{xx}}, where @var{xx} is one
8226 of @option{in/lo/hi/@var{xx}} with the same meanings as above.
8227 If no environment variable is found, or if it does not have a valid value,
8228 then the default is @option{in} (invalid values).
8229
8230 @ifclear vms
8231 @item -static
8232 @cindex @option{-static} (@code{gnatbind})
8233 Link against a static GNAT run time.
8234
8235 @item -shared
8236 @cindex @option{-shared} (@code{gnatbind})
8237 Link against a shared GNAT run time when available.
8238 @end ifclear
8239
8240 @item ^-t^/NOTIME_STAMP_CHECK^
8241 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
8242 Tolerate time stamp and other consistency errors
8243
8244 @item ^-T@var{n}^/TIME_SLICE=@var{n}^
8245 @cindex @option{^-T^/TIME_SLICE^} (@code{gnatbind})
8246 Set the time slice value to @var{n} milliseconds. If the system supports
8247 the specification of a specific time slice value, then the indicated value
8248 is used. If the system does not support specific time slice values, but
8249 does support some general notion of round-robin scheduling, then any
8250 nonzero value will activate round-robin scheduling.
8251
8252 A value of zero is treated specially. It turns off time
8253 slicing, and in addition, indicates to the tasking run time that the
8254 semantics should match as closely as possible the Annex D
8255 requirements of the Ada RM, and in particular sets the default
8256 scheduling policy to @code{FIFO_Within_Priorities}.
8257
8258 @item ^-u@var{n}^/DYNAMIC_STACK_USAGE=@var{n}^
8259 @cindex @option{^-u^/DYNAMIC_STACK_USAGE^} (@code{gnatbind})
8260 Enable dynamic stack usage, with @var{n} results stored and displayed
8261 at program termination. A result is generated when a task
8262 terminates. Results that can't be stored are displayed on the fly, at
8263 task termination. This option is currently not supported on Itanium
8264 platforms. (See @ref{Dynamic Stack Usage Analysis} for details.)
8265
8266 @item ^-v^/REPORT_ERRORS=VERBOSE^
8267 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
8268 Verbose mode. Write error messages, header, summary output to
8269 @file{stdout}.
8270
8271 @ifclear vms
8272 @item -w@var{x}
8273 @cindex @option{-w} (@code{gnatbind})
8274 Warning mode (@var{x}=s/e for suppress/treat as error)
8275 @end ifclear
8276
8277 @ifset vms
8278 @item /WARNINGS=NORMAL
8279 @cindex @option{/WARNINGS} (@code{gnatbind})
8280 Normal warnings mode. Warnings are issued but ignored
8281
8282 @item /WARNINGS=SUPPRESS
8283 @cindex @option{/WARNINGS} (@code{gnatbind})
8284 All warning messages are suppressed
8285
8286 @item /WARNINGS=ERROR
8287 @cindex @option{/WARNINGS} (@code{gnatbind})
8288 Warning messages are treated as fatal errors
8289 @end ifset
8290
8291 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
8292 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
8293 Override default wide character encoding for standard Text_IO files.
8294
8295 @item ^-x^/READ_SOURCES=NONE^
8296 @cindex @option{^-x^/READ_SOURCES^} (@code{gnatbind})
8297 Exclude source files (check object consistency only).
8298
8299 @ifset vms
8300 @item /READ_SOURCES=AVAILABLE
8301 @cindex @option{/READ_SOURCES} (@code{gnatbind})
8302 Default mode, in which sources are checked for consistency only if
8303 they are available.
8304 @end ifset
8305
8306 @item ^-y^/ENABLE_LEAP_SECONDS^
8307 @cindex @option{^-y^/ENABLE_LEAP_SECONDS^} (@code{gnatbind})
8308 Enable leap seconds support in @code{Ada.Calendar} and its children.
8309
8310 @item ^-z^/ZERO_MAIN^
8311 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
8312 No main subprogram.
8313 @end table
8314
8315 @ifclear vms
8316 @noindent
8317 You may obtain this listing of switches by running @code{gnatbind} with
8318 no arguments.
8319 @end ifclear
8320
8321 @node Consistency-Checking Modes
8322 @subsection Consistency-Checking Modes
8323
8324 @noindent
8325 As described earlier, by default @code{gnatbind} checks
8326 that object files are consistent with one another and are consistent
8327 with any source files it can locate. The following switches control binder
8328 access to sources.
8329
8330 @table @option
8331 @c !sort!
8332 @item ^-s^/READ_SOURCES=ALL^
8333 @cindex @option{^-s^/READ_SOURCES=ALL^} (@code{gnatbind})
8334 Require source files to be present. In this mode, the binder must be
8335 able to locate all source files that are referenced, in order to check
8336 their consistency. In normal mode, if a source file cannot be located it
8337 is simply ignored. If you specify this switch, a missing source
8338 file is an error.
8339
8340 @item ^-Wx^/WIDE_CHARACTER_ENCODING=^@var{e}
8341 @cindex @option{^-Wx^/WIDE_CHARACTER_ENCODING^} (@code{gnatbind})
8342 Override default wide character encoding for standard Text_IO files.
8343 Normally the default wide character encoding method used for standard
8344 [Wide_[Wide_]]Text_IO files is taken from the encoding specified for
8345 the main source input (see description of switch
8346 @option{^-gnatWx^/WIDE_CHARACTER_ENCODING^} for the compiler). The
8347 use of this switch for the binder (which has the same set of
8348 possible arguments) overrides this default as specified.
8349
8350 @item ^-x^/READ_SOURCES=NONE^
8351 @cindex @option{^-x^/READ_SOURCES=NONE^} (@code{gnatbind})
8352 Exclude source files. In this mode, the binder only checks that ALI
8353 files are consistent with one another. Source files are not accessed.
8354 The binder runs faster in this mode, and there is still a guarantee that
8355 the resulting program is self-consistent.
8356 If a source file has been edited since it was last compiled, and you
8357 specify this switch, the binder will not detect that the object
8358 file is out of date with respect to the source file. Note that this is the
8359 mode that is automatically used by @command{gnatmake} because in this
8360 case the checking against sources has already been performed by
8361 @command{gnatmake} in the course of compilation (i.e.@: before binding).
8362
8363 @ifset vms
8364 @item /READ_SOURCES=AVAILABLE
8365 @cindex @code{/READ_SOURCES=AVAILABLE} (@code{gnatbind})
8366 This is the default mode in which source files are checked if they are
8367 available, and ignored if they are not available.
8368 @end ifset
8369 @end table
8370
8371 @node Binder Error Message Control
8372 @subsection Binder Error Message Control
8373
8374 @noindent
8375 The following switches provide control over the generation of error
8376 messages from the binder:
8377
8378 @table @option
8379 @c !sort!
8380 @item ^-v^/REPORT_ERRORS=VERBOSE^
8381 @cindex @option{^-v^/REPORT_ERRORS=VERBOSE^} (@code{gnatbind})
8382 Verbose mode. In the normal mode, brief error messages are generated to
8383 @file{stderr}. If this switch is present, a header is written
8384 to @file{stdout} and any error messages are directed to @file{stdout}.
8385 All that is written to @file{stderr} is a brief summary message.
8386
8387 @item ^-b^/REPORT_ERRORS=BRIEF^
8388 @cindex @option{^-b^/REPORT_ERRORS=BRIEF^} (@code{gnatbind})
8389 Generate brief error messages to @file{stderr} even if verbose mode is
8390 specified. This is relevant only when used with the
8391 @option{^-v^/REPORT_ERRORS=VERBOSE^} switch.
8392
8393 @ifclear vms
8394 @item -m@var{n}
8395 @cindex @option{-m} (@code{gnatbind})
8396 Limits the number of error messages to @var{n}, a decimal integer in the
8397 range 1-999. The binder terminates immediately if this limit is reached.
8398
8399 @item -M@var{xxx}
8400 @cindex @option{-M} (@code{gnatbind})
8401 Renames the generated main program from @code{main} to @code{xxx}.
8402 This is useful in the case of some cross-building environments, where
8403 the actual main program is separate from the one generated
8404 by @code{gnatbind}.
8405 @end ifclear
8406
8407 @item ^-ws^/WARNINGS=SUPPRESS^
8408 @cindex @option{^-ws^/WARNINGS=SUPPRESS^} (@code{gnatbind})
8409 @cindex Warnings
8410 Suppress all warning messages.
8411
8412 @item ^-we^/WARNINGS=ERROR^
8413 @cindex @option{^-we^/WARNINGS=ERROR^} (@code{gnatbind})
8414 Treat any warning messages as fatal errors.
8415
8416 @ifset vms
8417 @item /WARNINGS=NORMAL
8418 Standard mode with warnings generated, but warnings do not get treated
8419 as errors.
8420 @end ifset
8421
8422 @item ^-t^/NOTIME_STAMP_CHECK^
8423 @cindex @option{^-t^/NOTIME_STAMP_CHECK^} (@code{gnatbind})
8424 @cindex Time stamp checks, in binder
8425 @cindex Binder consistency checks
8426 @cindex Consistency checks, in binder
8427 The binder performs a number of consistency checks including:
8428
8429 @itemize @bullet
8430 @item
8431 Check that time stamps of a given source unit are consistent
8432 @item
8433 Check that checksums of a given source unit are consistent
8434 @item
8435 Check that consistent versions of @code{GNAT} were used for compilation
8436 @item
8437 Check consistency of configuration pragmas as required
8438 @end itemize
8439
8440 @noindent
8441 Normally failure of such checks, in accordance with the consistency
8442 requirements of the Ada Reference Manual, causes error messages to be
8443 generated which abort the binder and prevent the output of a binder
8444 file and subsequent link to obtain an executable.
8445
8446 The @option{^-t^/NOTIME_STAMP_CHECK^} switch converts these error messages
8447 into warnings, so that
8448 binding and linking can continue to completion even in the presence of such
8449 errors. The result may be a failed link (due to missing symbols), or a
8450 non-functional executable which has undefined semantics.
8451 @emph{This means that
8452 @option{^-t^/NOTIME_STAMP_CHECK^} should be used only in unusual situations,
8453 with extreme care.}
8454 @end table
8455
8456 @node Elaboration Control
8457 @subsection Elaboration Control
8458
8459 @noindent
8460 The following switches provide additional control over the elaboration
8461 order. For full details see @ref{Elaboration Order Handling in GNAT}.
8462
8463 @table @option
8464 @item ^-p^/PESSIMISTIC_ELABORATION^
8465 @cindex @option{^-p^/PESSIMISTIC_ELABORATION^} (@code{gnatbind})
8466 Normally the binder attempts to choose an elaboration order that is
8467 likely to minimize the likelihood of an elaboration order error resulting
8468 in raising a @code{Program_Error} exception. This switch reverses the
8469 action of the binder, and requests that it deliberately choose an order
8470 that is likely to maximize the likelihood of an elaboration error.
8471 This is useful in ensuring portability and avoiding dependence on
8472 accidental fortuitous elaboration ordering.
8473
8474 Normally it only makes sense to use the @option{^-p^/PESSIMISTIC_ELABORATION^}
8475 switch if dynamic
8476 elaboration checking is used (@option{-gnatE} switch used for compilation).
8477 This is because in the default static elaboration mode, all necessary
8478 @code{Elaborate} and @code{Elaborate_All} pragmas are implicitly inserted.
8479 These implicit pragmas are still respected by the binder in
8480 @option{^-p^/PESSIMISTIC_ELABORATION^} mode, so a
8481 safe elaboration order is assured.
8482 @end table
8483
8484 @node Output Control
8485 @subsection Output Control
8486
8487 @noindent
8488 The following switches allow additional control over the output
8489 generated by the binder.
8490
8491 @table @option
8492 @c !sort!
8493
8494 @item ^-A^/BIND_FILE=ADA^
8495 @cindex @option{^-A^/BIND_FILE=ADA^} (@code{gnatbind})
8496 Generate binder program in Ada (default). The binder program is named
8497 @file{b~@var{mainprog}.adb} by default. This can be changed with
8498 @option{^-o^/OUTPUT^} @code{gnatbind} option.
8499
8500 @item ^-c^/NOOUTPUT^
8501 @cindex @option{^-c^/NOOUTPUT^} (@code{gnatbind})
8502 Check only. Do not generate the binder output file. In this mode the
8503 binder performs all error checks but does not generate an output file.
8504
8505 @item ^-C^/BIND_FILE=C^
8506 @cindex @option{^-C^/BIND_FILE=C^} (@code{gnatbind})
8507 Generate binder program in C. The binder program is named
8508 @file{b_@var{mainprog}.c}.
8509 This can be changed with @option{^-o^/OUTPUT^} @code{gnatbind}
8510 option.
8511
8512 @item ^-e^/ELABORATION_DEPENDENCIES^
8513 @cindex @option{^-e^/ELABORATION_DEPENDENCIES^} (@code{gnatbind})
8514 Output complete list of elaboration-order dependencies, showing the
8515 reason for each dependency. This output can be rather extensive but may
8516 be useful in diagnosing problems with elaboration order. The output is
8517 written to @file{stdout}.
8518
8519 @item ^-h^/HELP^
8520 @cindex @option{^-h^/HELP^} (@code{gnatbind})
8521 Output usage information. The output is written to @file{stdout}.
8522
8523 @item ^-K^/LINKER_OPTION_LIST^
8524 @cindex @option{^-K^/LINKER_OPTION_LIST^} (@code{gnatbind})
8525 Output linker options to @file{stdout}. Includes library search paths,
8526 contents of pragmas Ident and Linker_Options, and libraries added
8527 by @code{gnatbind}.
8528
8529 @item ^-l^/ORDER_OF_ELABORATION^
8530 @cindex @option{^-l^/ORDER_OF_ELABORATION^} (@code{gnatbind})
8531 Output chosen elaboration order. The output is written to @file{stdout}.
8532
8533 @item ^-O^/OBJECT_LIST^
8534 @cindex @option{^-O^/OBJECT_LIST^} (@code{gnatbind})
8535 Output full names of all the object files that must be linked to provide
8536 the Ada component of the program. The output is written to @file{stdout}.
8537 This list includes the files explicitly supplied and referenced by the user
8538 as well as implicitly referenced run-time unit files. The latter are
8539 omitted if the corresponding units reside in shared libraries. The
8540 directory names for the run-time units depend on the system configuration.
8541
8542 @item ^-o ^/OUTPUT=^@var{file}
8543 @cindex @option{^-o^/OUTPUT^} (@code{gnatbind})
8544 Set name of output file to @var{file} instead of the normal
8545 @file{b~@var{mainprog}.adb} default. Note that @var{file} denote the Ada
8546 binder generated body filename. In C mode you would normally give
8547 @var{file} an extension of @file{.c} because it will be a C source program.
8548 Note that if this option is used, then linking must be done manually.
8549 It is not possible to use gnatlink in this case, since it cannot locate
8550 the binder file.
8551
8552 @item ^-r^/RESTRICTION_LIST^
8553 @cindex @option{^-r^/RESTRICTION_LIST^} (@code{gnatbind})
8554 Generate list of @code{pragma Restrictions} that could be applied to
8555 the current unit. This is useful for code audit purposes, and also may
8556 be used to improve code generation in some cases.
8557
8558 @end table
8559
8560 @node Binding with Non-Ada Main Programs
8561 @subsection Binding with Non-Ada Main Programs
8562
8563 @noindent
8564 In our description so far we have assumed that the main
8565 program is in Ada, and that the task of the binder is to generate a
8566 corresponding function @code{main} that invokes this Ada main
8567 program. GNAT also supports the building of executable programs where
8568 the main program is not in Ada, but some of the called routines are
8569 written in Ada and compiled using GNAT (@pxref{Mixed Language Programming}).
8570 The following switch is used in this situation:
8571
8572 @table @option
8573 @item ^-n^/NOMAIN^
8574 @cindex @option{^-n^/NOMAIN^} (@code{gnatbind})
8575 No main program. The main program is not in Ada.
8576 @end table
8577
8578 @noindent
8579 In this case, most of the functions of the binder are still required,
8580 but instead of generating a main program, the binder generates a file
8581 containing the following callable routines:
8582
8583 @table @code
8584 @item adainit
8585 @findex adainit
8586 You must call this routine to initialize the Ada part of the program by
8587 calling the necessary elaboration routines. A call to @code{adainit} is
8588 required before the first call to an Ada subprogram.
8589
8590 Note that it is assumed that the basic execution environment must be setup
8591 to be appropriate for Ada execution at the point where the first Ada
8592 subprogram is called. In particular, if the Ada code will do any
8593 floating-point operations, then the FPU must be setup in an appropriate
8594 manner. For the case of the x86, for example, full precision mode is
8595 required. The procedure GNAT.Float_Control.Reset may be used to ensure
8596 that the FPU is in the right state.
8597
8598 @item adafinal
8599 @findex adafinal
8600 You must call this routine to perform any library-level finalization
8601 required by the Ada subprograms. A call to @code{adafinal} is required
8602 after the last call to an Ada subprogram, and before the program
8603 terminates.
8604 @end table
8605
8606 @noindent
8607 If the @option{^-n^/NOMAIN^} switch
8608 @cindex @option{^-n^/NOMAIN^} (@command{gnatbind})
8609 @cindex Binder, multiple input files
8610 is given, more than one ALI file may appear on
8611 the command line for @code{gnatbind}. The normal @dfn{closure}
8612 calculation is performed for each of the specified units. Calculating
8613 the closure means finding out the set of units involved by tracing
8614 @code{with} references. The reason it is necessary to be able to
8615 specify more than one ALI file is that a given program may invoke two or
8616 more quite separate groups of Ada units.
8617
8618 The binder takes the name of its output file from the last specified ALI
8619 file, unless overridden by the use of the @option{^-o file^/OUTPUT=file^}.
8620 @cindex @option{^-o^/OUTPUT^} (@command{gnatbind})
8621 The output is an Ada unit in source form that can
8622 be compiled with GNAT unless the -C switch is used in which case the
8623 output is a C source file, which must be compiled using the C compiler.
8624 This compilation occurs automatically as part of the @command{gnatlink}
8625 processing.
8626
8627 Currently the GNAT run time requires a FPU using 80 bits mode
8628 precision. Under targets where this is not the default it is required to
8629 call GNAT.Float_Control.Reset before using floating point numbers (this
8630 include float computation, float input and output) in the Ada code. A
8631 side effect is that this could be the wrong mode for the foreign code
8632 where floating point computation could be broken after this call.
8633
8634 @node Binding Programs with No Main Subprogram
8635 @subsection Binding Programs with No Main Subprogram
8636
8637 @noindent
8638 It is possible to have an Ada program which does not have a main
8639 subprogram. This program will call the elaboration routines of all the
8640 packages, then the finalization routines.
8641
8642 The following switch is used to bind programs organized in this manner:
8643
8644 @table @option
8645 @item ^-z^/ZERO_MAIN^
8646 @cindex @option{^-z^/ZERO_MAIN^} (@code{gnatbind})
8647 Normally the binder checks that the unit name given on the command line
8648 corresponds to a suitable main subprogram. When this switch is used,
8649 a list of ALI files can be given, and the execution of the program
8650 consists of elaboration of these units in an appropriate order. Note
8651 that the default wide character encoding method for standard Text_IO
8652 files is always set to Brackets if this switch is set (you can use
8653 the binder switch
8654 @option{^-Wx^WIDE_CHARACTER_ENCODING^} to override this default).
8655 @end table
8656
8657 @node Command-Line Access
8658 @section Command-Line Access
8659
8660 @noindent
8661 The package @code{Ada.Command_Line} provides access to the command-line
8662 arguments and program name. In order for this interface to operate
8663 correctly, the two variables
8664
8665 @smallexample
8666 @group
8667 int gnat_argc;
8668 char **gnat_argv;
8669 @end group
8670 @end smallexample
8671
8672 @noindent
8673 @findex gnat_argv
8674 @findex gnat_argc
8675 are declared in one of the GNAT library routines. These variables must
8676 be set from the actual @code{argc} and @code{argv} values passed to the
8677 main program. With no @option{^n^/NOMAIN^} present, @code{gnatbind}
8678 generates the C main program to automatically set these variables.
8679 If the @option{^n^/NOMAIN^} switch is used, there is no automatic way to
8680 set these variables. If they are not set, the procedures in
8681 @code{Ada.Command_Line} will not be available, and any attempt to use
8682 them will raise @code{Constraint_Error}. If command line access is
8683 required, your main program must set @code{gnat_argc} and
8684 @code{gnat_argv} from the @code{argc} and @code{argv} values passed to
8685 it.
8686
8687 @node Search Paths for gnatbind
8688 @section Search Paths for @code{gnatbind}
8689
8690 @noindent
8691 The binder takes the name of an ALI file as its argument and needs to
8692 locate source files as well as other ALI files to verify object consistency.
8693
8694 For source files, it follows exactly the same search rules as @command{gcc}
8695 (@pxref{Search Paths and the Run-Time Library (RTL)}). For ALI files the
8696 directories searched are:
8697
8698 @enumerate
8699 @item
8700 The directory containing the ALI file named in the command line, unless
8701 the switch @option{^-I-^/NOCURRENT_DIRECTORY^} is specified.
8702
8703 @item
8704 All directories specified by @option{^-I^/SEARCH^}
8705 switches on the @code{gnatbind}
8706 command line, in the order given.
8707
8708 @item
8709 @findex ADA_PRJ_OBJECTS_FILE
8710 Each of the directories listed in the text file whose name is given
8711 by the @env{ADA_PRJ_OBJECTS_FILE} ^environment variable^logical name^.
8712
8713 @noindent
8714 @env{ADA_PRJ_OBJECTS_FILE} is normally set by gnatmake or by the ^gnat^GNAT^
8715 driver when project files are used. It should not normally be set
8716 by other means.
8717
8718 @item
8719 @findex ADA_OBJECTS_PATH
8720 Each of the directories listed in the value of the
8721 @env{ADA_OBJECTS_PATH} ^environment variable^logical name^.
8722 @ifset unw
8723 Construct this value
8724 exactly as the @env{PATH} environment variable: a list of directory
8725 names separated by colons (semicolons when working with the NT version
8726 of GNAT).
8727 @end ifset
8728 @ifset vms
8729 Normally, define this value as a logical name containing a comma separated
8730 list of directory names.
8731
8732 This variable can also be defined by means of an environment string
8733 (an argument to the HP C exec* set of functions).
8734
8735 Logical Name:
8736 @smallexample
8737 DEFINE ANOTHER_PATH FOO:[BAG]
8738 DEFINE ADA_OBJECTS_PATH ANOTHER_PATH,FOO:[BAM],FOO:[BAR]
8739 @end smallexample
8740
8741 By default, the path includes GNU:[LIB.OPENVMS7_x.2_8_x.DECLIB]
8742 first, followed by the standard Ada
8743 libraries in GNU:[LIB.OPENVMS7_x.2_8_x.ADALIB].
8744 If this is not redefined, the user will obtain the HP Ada 83 IO packages
8745 (Text_IO, Sequential_IO, etc)
8746 instead of the standard Ada packages. Thus, in order to get the standard Ada
8747 packages by default, ADA_OBJECTS_PATH must be redefined.
8748 @end ifset
8749
8750 @item
8751 The content of the @file{ada_object_path} file which is part of the GNAT
8752 installation tree and is used to store standard libraries such as the
8753 GNAT Run Time Library (RTL) unless the switch @option{-nostdlib} is
8754 specified.
8755 @ifclear vms
8756 @ref{Installing a library}
8757 @end ifclear
8758 @end enumerate
8759
8760 @noindent
8761 In the binder the switch @option{^-I^/SEARCH^}
8762 @cindex @option{^-I^/SEARCH^} (@command{gnatbind})
8763 is used to specify both source and
8764 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
8765 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatbind})
8766 instead if you want to specify
8767 source paths only, and @option{^-aO^/LIBRARY_SEARCH^}
8768 @cindex @option{^-aO^/LIBRARY_SEARCH^} (@command{gnatbind})
8769 if you want to specify library paths
8770 only. This means that for the binder
8771 @option{^-I^/SEARCH=^}@var{dir} is equivalent to
8772 @option{^-aI^/SOURCE_SEARCH=^}@var{dir}
8773 @option{^-aO^/OBJECT_SEARCH=^}@var{dir}.
8774 The binder generates the bind file (a C language source file) in the
8775 current working directory.
8776
8777 @findex Ada
8778 @findex System
8779 @findex Interfaces
8780 @findex GNAT
8781 The packages @code{Ada}, @code{System}, and @code{Interfaces} and their
8782 children make up the GNAT Run-Time Library, together with the package
8783 GNAT and its children, which contain a set of useful additional
8784 library functions provided by GNAT. The sources for these units are
8785 needed by the compiler and are kept together in one directory. The ALI
8786 files and object files generated by compiling the RTL are needed by the
8787 binder and the linker and are kept together in one directory, typically
8788 different from the directory containing the sources. In a normal
8789 installation, you need not specify these directory names when compiling
8790 or binding. Either the environment variables or the built-in defaults
8791 cause these files to be found.
8792
8793 Besides simplifying access to the RTL, a major use of search paths is
8794 in compiling sources from multiple directories. This can make
8795 development environments much more flexible.
8796
8797 @node Examples of gnatbind Usage
8798 @section Examples of @code{gnatbind} Usage
8799
8800 @noindent
8801 This section contains a number of examples of using the GNAT binding
8802 utility @code{gnatbind}.
8803
8804 @table @code
8805 @item gnatbind hello
8806 The main program @code{Hello} (source program in @file{hello.adb}) is
8807 bound using the standard switch settings. The generated main program is
8808 @file{b~hello.adb}. This is the normal, default use of the binder.
8809
8810 @ifclear vms
8811 @item gnatbind hello -o mainprog.adb
8812 @end ifclear
8813 @ifset vms
8814 @item gnatbind HELLO.ALI /OUTPUT=Mainprog.ADB
8815 @end ifset
8816 The main program @code{Hello} (source program in @file{hello.adb}) is
8817 bound using the standard switch settings. The generated main program is
8818 @file{mainprog.adb} with the associated spec in
8819 @file{mainprog.ads}. Note that you must specify the body here not the
8820 spec, in the case where the output is in Ada. Note that if this option
8821 is used, then linking must be done manually, since gnatlink will not
8822 be able to find the generated file.
8823
8824 @ifclear vms
8825 @item gnatbind main -C -o mainprog.c -x
8826 @end ifclear
8827 @ifset vms
8828 @item gnatbind MAIN.ALI /BIND_FILE=C /OUTPUT=Mainprog.C /READ_SOURCES=NONE
8829 @end ifset
8830 The main program @code{Main} (source program in
8831 @file{main.adb}) is bound, excluding source files from the
8832 consistency checking, generating
8833 the file @file{mainprog.c}.
8834
8835 @ifclear vms
8836 @item gnatbind -x main_program -C -o mainprog.c
8837 This command is exactly the same as the previous example. Switches may
8838 appear anywhere in the command line, and single letter switches may be
8839 combined into a single switch.
8840 @end ifclear
8841
8842 @ifclear vms
8843 @item gnatbind -n math dbase -C -o ada-control.c
8844 @end ifclear
8845 @ifset vms
8846 @item gnatbind /NOMAIN math dbase /BIND_FILE=C /OUTPUT=ada-control.c
8847 @end ifset
8848 The main program is in a language other than Ada, but calls to
8849 subprograms in packages @code{Math} and @code{Dbase} appear. This call
8850 to @code{gnatbind} generates the file @file{ada-control.c} containing
8851 the @code{adainit} and @code{adafinal} routines to be called before and
8852 after accessing the Ada units.
8853 @end table
8854
8855 @c ------------------------------------
8856 @node Linking Using gnatlink
8857 @chapter Linking Using @command{gnatlink}
8858 @c ------------------------------------
8859 @findex gnatlink
8860
8861 @noindent
8862 This chapter discusses @command{gnatlink}, a tool that links
8863 an Ada program and builds an executable file. This utility
8864 invokes the system linker ^(via the @command{gcc} command)^^
8865 with a correct list of object files and library references.
8866 @command{gnatlink} automatically determines the list of files and
8867 references for the Ada part of a program. It uses the binder file
8868 generated by the @command{gnatbind} to determine this list.
8869
8870 Note: to invoke @code{gnatlink} with a project file, use the @code{gnat}
8871 driver (see @ref{The GNAT Driver and Project Files}).
8872
8873 @menu
8874 * Running gnatlink::
8875 * Switches for gnatlink::
8876 @end menu
8877
8878 @node Running gnatlink
8879 @section Running @command{gnatlink}
8880
8881 @noindent
8882 The form of the @command{gnatlink} command is
8883
8884 @smallexample
8885 @c $ gnatlink @ovar{switches} @var{mainprog}@r{[}.ali@r{]}
8886 @c @ovar{non-Ada objects} @ovar{linker options}
8887 @c Expanding @ovar macro inline (explanation in macro def comments)
8888 $ gnatlink @r{[}@var{switches}@r{]} @var{mainprog}@r{[}.ali@r{]}
8889 @r{[}@var{non-Ada objects}@r{]} @r{[}@var{linker options}@r{]}
8890
8891 @end smallexample
8892
8893 @noindent
8894 The arguments of @command{gnatlink} (switches, main @file{ALI} file,
8895 non-Ada objects
8896 or linker options) may be in any order, provided that no non-Ada object may
8897 be mistaken for a main @file{ALI} file.
8898 Any file name @file{F} without the @file{.ali}
8899 extension will be taken as the main @file{ALI} file if a file exists
8900 whose name is the concatenation of @file{F} and @file{.ali}.
8901
8902 @noindent
8903 @file{@var{mainprog}.ali} references the ALI file of the main program.
8904 The @file{.ali} extension of this file can be omitted. From this
8905 reference, @command{gnatlink} locates the corresponding binder file
8906 @file{b~@var{mainprog}.adb} and, using the information in this file along
8907 with the list of non-Ada objects and linker options, constructs a
8908 linker command file to create the executable.
8909
8910 The arguments other than the @command{gnatlink} switches and the main
8911 @file{ALI} file are passed to the linker uninterpreted.
8912 They typically include the names of
8913 object files for units written in other languages than Ada and any library
8914 references required to resolve references in any of these foreign language
8915 units, or in @code{Import} pragmas in any Ada units.
8916
8917 @var{linker options} is an optional list of linker specific
8918 switches.
8919 The default linker called by gnatlink is @command{gcc} which in
8920 turn calls the appropriate system linker.
8921 Standard options for the linker such as @option{-lmy_lib} or
8922 @option{-Ldir} can be added as is.
8923 For options that are not recognized by
8924 @command{gcc} as linker options, use the @command{gcc} switches
8925 @option{-Xlinker} or @option{-Wl,}.
8926 Refer to the GCC documentation for
8927 details. Here is an example showing how to generate a linker map:
8928
8929 @smallexample
8930 $ ^gnatlink my_prog -Wl,-Map,MAPFILE^GNAT LINK my_prog.ali /MAP^
8931 @end smallexample
8932
8933 Using @var{linker options} it is possible to set the program stack and
8934 heap size.
8935 @ifset unw
8936 See @ref{Setting Stack Size from gnatlink} and
8937 @ref{Setting Heap Size from gnatlink}.
8938 @end ifset
8939
8940 @command{gnatlink} determines the list of objects required by the Ada
8941 program and prepends them to the list of objects passed to the linker.
8942 @command{gnatlink} also gathers any arguments set by the use of
8943 @code{pragma Linker_Options} and adds them to the list of arguments
8944 presented to the linker.
8945
8946 @ifset vms
8947 @command{gnatlink} accepts the following types of extra files on the command
8948 line: objects (@file{.OBJ}), libraries (@file{.OLB}), sharable images
8949 (@file{.EXE}), and options files (@file{.OPT}). These are recognized and
8950 handled according to their extension.
8951 @end ifset
8952
8953 @node Switches for gnatlink
8954 @section Switches for @command{gnatlink}
8955
8956 @noindent
8957 The following switches are available with the @command{gnatlink} utility:
8958
8959 @table @option
8960 @c !sort!
8961
8962 @item --version
8963 @cindex @option{--version} @command{gnatlink}
8964 Display Copyright and version, then exit disregarding all other options.
8965
8966 @item --help
8967 @cindex @option{--help} @command{gnatlink}
8968 If @option{--version} was not used, display usage, then exit disregarding
8969 all other options.
8970
8971 @item ^-A^/BIND_FILE=ADA^
8972 @cindex @option{^-A^/BIND_FILE=ADA^} (@command{gnatlink})
8973 The binder has generated code in Ada. This is the default.
8974
8975 @item ^-C^/BIND_FILE=C^
8976 @cindex @option{^-C^/BIND_FILE=C^} (@command{gnatlink})
8977 If instead of generating a file in Ada, the binder has generated one in
8978 C, then the linker needs to know about it. Use this switch to signal
8979 to @command{gnatlink} that the binder has generated C code rather than
8980 Ada code.
8981
8982 @item ^-f^/FORCE_OBJECT_FILE_LIST^
8983 @cindex Command line length
8984 @cindex @option{^-f^/FORCE_OBJECT_FILE_LIST^} (@command{gnatlink})
8985 On some targets, the command line length is limited, and @command{gnatlink}
8986 will generate a separate file for the linker if the list of object files
8987 is too long.
8988 The @option{^-f^/FORCE_OBJECT_FILE_LIST^} switch forces this file
8989 to be generated even if
8990 the limit is not exceeded. This is useful in some cases to deal with
8991 special situations where the command line length is exceeded.
8992
8993 @item ^-g^/DEBUG^
8994 @cindex Debugging information, including
8995 @cindex @option{^-g^/DEBUG^} (@command{gnatlink})
8996 The option to include debugging information causes the Ada bind file (in
8997 other words, @file{b~@var{mainprog}.adb}) to be compiled with
8998 @option{^-g^/DEBUG^}.
8999 In addition, the binder does not delete the @file{b~@var{mainprog}.adb},
9000 @file{b~@var{mainprog}.o} and @file{b~@var{mainprog}.ali} files.
9001 Without @option{^-g^/DEBUG^}, the binder removes these files by
9002 default. The same procedure apply if a C bind file was generated using
9003 @option{^-C^/BIND_FILE=C^} @code{gnatbind} option, in this case the filenames
9004 are @file{b_@var{mainprog}.c} and @file{b_@var{mainprog}.o}.
9005
9006 @item ^-n^/NOCOMPILE^
9007 @cindex @option{^-n^/NOCOMPILE^} (@command{gnatlink})
9008 Do not compile the file generated by the binder. This may be used when
9009 a link is rerun with different options, but there is no need to recompile
9010 the binder file.
9011
9012 @item ^-v^/VERBOSE^
9013 @cindex @option{^-v^/VERBOSE^} (@command{gnatlink})
9014 Causes additional information to be output, including a full list of the
9015 included object files. This switch option is most useful when you want
9016 to see what set of object files are being used in the link step.
9017
9018 @item ^-v -v^/VERBOSE/VERBOSE^
9019 @cindex @option{^-v -v^/VERBOSE/VERBOSE^} (@command{gnatlink})
9020 Very verbose mode. Requests that the compiler operate in verbose mode when
9021 it compiles the binder file, and that the system linker run in verbose mode.
9022
9023 @item ^-o ^/EXECUTABLE=^@var{exec-name}
9024 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatlink})
9025 @var{exec-name} specifies an alternate name for the generated
9026 executable program. If this switch is omitted, the executable has the same
9027 name as the main unit. For example, @code{gnatlink try.ali} creates
9028 an executable called @file{^try^TRY.EXE^}.
9029
9030 @ifclear vms
9031 @item -b @var{target}
9032 @cindex @option{-b} (@command{gnatlink})
9033 Compile your program to run on @var{target}, which is the name of a
9034 system configuration. You must have a GNAT cross-compiler built if
9035 @var{target} is not the same as your host system.
9036
9037 @item -B@var{dir}
9038 @cindex @option{-B} (@command{gnatlink})
9039 Load compiler executables (for example, @code{gnat1}, the Ada compiler)
9040 from @var{dir} instead of the default location. Only use this switch
9041 when multiple versions of the GNAT compiler are available.
9042 @xref{Directory Options,,, gcc, The GNU Compiler Collection},
9043 for further details. You would normally use the @option{-b} or
9044 @option{-V} switch instead.
9045
9046 @item --GCC=@var{compiler_name}
9047 @cindex @option{--GCC=compiler_name} (@command{gnatlink})
9048 Program used for compiling the binder file. The default is
9049 @command{gcc}. You need to use quotes around @var{compiler_name} if
9050 @code{compiler_name} contains spaces or other separator characters.
9051 As an example @option{--GCC="foo -x -y"} will instruct @command{gnatlink} to
9052 use @code{foo -x -y} as your compiler. Note that switch @option{-c} is always
9053 inserted after your command name. Thus in the above example the compiler
9054 command that will be used by @command{gnatlink} will be @code{foo -c -x -y}.
9055 A limitation of this syntax is that the name and path name of the executable
9056 itself must not include any embedded spaces. If the compiler executable is
9057 different from the default one (gcc or <prefix>-gcc), then the back-end
9058 switches in the ALI file are not used to compile the binder generated source.
9059 For example, this is the case with @option{--GCC="foo -x -y"}. But the back end
9060 switches will be used for @option{--GCC="gcc -gnatv"}. If several
9061 @option{--GCC=compiler_name} are used, only the last @var{compiler_name}
9062 is taken into account. However, all the additional switches are also taken
9063 into account. Thus,
9064 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
9065 @option{--GCC="bar -x -y -z -t"}.
9066
9067 @item --LINK=@var{name}
9068 @cindex @option{--LINK=} (@command{gnatlink})
9069 @var{name} is the name of the linker to be invoked. This is especially
9070 useful in mixed language programs since languages such as C++ require
9071 their own linker to be used. When this switch is omitted, the default
9072 name for the linker is @command{gcc}. When this switch is used, the
9073 specified linker is called instead of @command{gcc} with exactly the same
9074 parameters that would have been passed to @command{gcc} so if the desired
9075 linker requires different parameters it is necessary to use a wrapper
9076 script that massages the parameters before invoking the real linker. It
9077 may be useful to control the exact invocation by using the verbose
9078 switch.
9079
9080 @end ifclear
9081
9082 @ifset vms
9083 @item /DEBUG=TRACEBACK
9084 @cindex @code{/DEBUG=TRACEBACK} (@command{gnatlink})
9085 This qualifier causes sufficient information to be included in the
9086 executable file to allow a traceback, but does not include the full
9087 symbol information needed by the debugger.
9088
9089 @item /IDENTIFICATION="<string>"
9090 @code{"<string>"} specifies the string to be stored in the image file
9091 identification field in the image header.
9092 It overrides any pragma @code{Ident} specified string.
9093
9094 @item /NOINHIBIT-EXEC
9095 Generate the executable file even if there are linker warnings.
9096
9097 @item /NOSTART_FILES
9098 Don't link in the object file containing the ``main'' transfer address.
9099 Used when linking with a foreign language main program compiled with an
9100 HP compiler.
9101
9102 @item /STATIC
9103 Prefer linking with object libraries over sharable images, even without
9104 /DEBUG.
9105 @end ifset
9106
9107 @end table
9108
9109 @node The GNAT Make Program gnatmake
9110 @chapter The GNAT Make Program @command{gnatmake}
9111 @findex gnatmake
9112
9113 @menu
9114 * Running gnatmake::
9115 * Switches for gnatmake::
9116 * Mode Switches for gnatmake::
9117 * Notes on the Command Line::
9118 * How gnatmake Works::
9119 * Examples of gnatmake Usage::
9120 @end menu
9121 @noindent
9122 A typical development cycle when working on an Ada program consists of
9123 the following steps:
9124
9125 @enumerate
9126 @item
9127 Edit some sources to fix bugs.
9128
9129 @item
9130 Add enhancements.
9131
9132 @item
9133 Compile all sources affected.
9134
9135 @item
9136 Rebind and relink.
9137
9138 @item
9139 Test.
9140 @end enumerate
9141
9142 @noindent
9143 The third step can be tricky, because not only do the modified files
9144 @cindex Dependency rules
9145 have to be compiled, but any files depending on these files must also be
9146 recompiled. The dependency rules in Ada can be quite complex, especially
9147 in the presence of overloading, @code{use} clauses, generics and inlined
9148 subprograms.
9149
9150 @command{gnatmake} automatically takes care of the third and fourth steps
9151 of this process. It determines which sources need to be compiled,
9152 compiles them, and binds and links the resulting object files.
9153
9154 Unlike some other Ada make programs, the dependencies are always
9155 accurately recomputed from the new sources. The source based approach of
9156 the GNAT compilation model makes this possible. This means that if
9157 changes to the source program cause corresponding changes in
9158 dependencies, they will always be tracked exactly correctly by
9159 @command{gnatmake}.
9160
9161 @node Running gnatmake
9162 @section Running @command{gnatmake}
9163
9164 @noindent
9165 The usual form of the @command{gnatmake} command is
9166
9167 @smallexample
9168 @c $ gnatmake @ovar{switches} @var{file_name}
9169 @c @ovar{file_names} @ovar{mode_switches}
9170 @c Expanding @ovar macro inline (explanation in macro def comments)
9171 $ gnatmake @r{[}@var{switches}@r{]} @var{file_name}
9172 @r{[}@var{file_names}@r{]} @r{[}@var{mode_switches}@r{]}
9173 @end smallexample
9174
9175 @noindent
9176 The only required argument is one @var{file_name}, which specifies
9177 a compilation unit that is a main program. Several @var{file_names} can be
9178 specified: this will result in several executables being built.
9179 If @code{switches} are present, they can be placed before the first
9180 @var{file_name}, between @var{file_names} or after the last @var{file_name}.
9181 If @var{mode_switches} are present, they must always be placed after
9182 the last @var{file_name} and all @code{switches}.
9183
9184 If you are using standard file extensions (@file{.adb} and @file{.ads}), then the
9185 extension may be omitted from the @var{file_name} arguments. However, if
9186 you are using non-standard extensions, then it is required that the
9187 extension be given. A relative or absolute directory path can be
9188 specified in a @var{file_name}, in which case, the input source file will
9189 be searched for in the specified directory only. Otherwise, the input
9190 source file will first be searched in the directory where
9191 @command{gnatmake} was invoked and if it is not found, it will be search on
9192 the source path of the compiler as described in
9193 @ref{Search Paths and the Run-Time Library (RTL)}.
9194
9195 All @command{gnatmake} output (except when you specify
9196 @option{^-M^/DEPENDENCIES_LIST^}) is to
9197 @file{stderr}. The output produced by the
9198 @option{^-M^/DEPENDENCIES_LIST^} switch is send to
9199 @file{stdout}.
9200
9201 @node Switches for gnatmake
9202 @section Switches for @command{gnatmake}
9203
9204 @noindent
9205 You may specify any of the following switches to @command{gnatmake}:
9206
9207 @table @option
9208 @c !sort!
9209
9210 @item --version
9211 @cindex @option{--version} @command{gnatmake}
9212 Display Copyright and version, then exit disregarding all other options.
9213
9214 @item --help
9215 @cindex @option{--help} @command{gnatmake}
9216 If @option{--version} was not used, display usage, then exit disregarding
9217 all other options.
9218
9219 @ifclear vms
9220 @item --GCC=@var{compiler_name}
9221 @cindex @option{--GCC=compiler_name} (@command{gnatmake})
9222 Program used for compiling. The default is `@command{gcc}'. You need to use
9223 quotes around @var{compiler_name} if @code{compiler_name} contains
9224 spaces or other separator characters. As an example @option{--GCC="foo -x
9225 -y"} will instruct @command{gnatmake} to use @code{foo -x -y} as your
9226 compiler. A limitation of this syntax is that the name and path name of
9227 the executable itself must not include any embedded spaces. Note that
9228 switch @option{-c} is always inserted after your command name. Thus in the
9229 above example the compiler command that will be used by @command{gnatmake}
9230 will be @code{foo -c -x -y}. If several @option{--GCC=compiler_name} are
9231 used, only the last @var{compiler_name} is taken into account. However,
9232 all the additional switches are also taken into account. Thus,
9233 @option{--GCC="foo -x -y" --GCC="bar -z -t"} is equivalent to
9234 @option{--GCC="bar -x -y -z -t"}.
9235
9236 @item --GNATBIND=@var{binder_name}
9237 @cindex @option{--GNATBIND=binder_name} (@command{gnatmake})
9238 Program used for binding. The default is `@code{gnatbind}'. You need to
9239 use quotes around @var{binder_name} if @var{binder_name} contains spaces
9240 or other separator characters. As an example @option{--GNATBIND="bar -x
9241 -y"} will instruct @command{gnatmake} to use @code{bar -x -y} as your
9242 binder. Binder switches that are normally appended by @command{gnatmake}
9243 to `@code{gnatbind}' are now appended to the end of @code{bar -x -y}.
9244 A limitation of this syntax is that the name and path name of the executable
9245 itself must not include any embedded spaces.
9246
9247 @item --GNATLINK=@var{linker_name}
9248 @cindex @option{--GNATLINK=linker_name} (@command{gnatmake})
9249 Program used for linking. The default is `@command{gnatlink}'. You need to
9250 use quotes around @var{linker_name} if @var{linker_name} contains spaces
9251 or other separator characters. As an example @option{--GNATLINK="lan -x
9252 -y"} will instruct @command{gnatmake} to use @code{lan -x -y} as your
9253 linker. Linker switches that are normally appended by @command{gnatmake} to
9254 `@command{gnatlink}' are now appended to the end of @code{lan -x -y}.
9255 A limitation of this syntax is that the name and path name of the executable
9256 itself must not include any embedded spaces.
9257
9258 @end ifclear
9259
9260 @item ^-a^/ALL_FILES^
9261 @cindex @option{^-a^/ALL_FILES^} (@command{gnatmake})
9262 Consider all files in the make process, even the GNAT internal system
9263 files (for example, the predefined Ada library files), as well as any
9264 locked files. Locked files are files whose ALI file is write-protected.
9265 By default,
9266 @command{gnatmake} does not check these files,
9267 because the assumption is that the GNAT internal files are properly up
9268 to date, and also that any write protected ALI files have been properly
9269 installed. Note that if there is an installation problem, such that one
9270 of these files is not up to date, it will be properly caught by the
9271 binder.
9272 You may have to specify this switch if you are working on GNAT
9273 itself. The switch @option{^-a^/ALL_FILES^} is also useful
9274 in conjunction with @option{^-f^/FORCE_COMPILE^}
9275 if you need to recompile an entire application,
9276 including run-time files, using special configuration pragmas,
9277 such as a @code{Normalize_Scalars} pragma.
9278
9279 By default
9280 @code{gnatmake ^-a^/ALL_FILES^} compiles all GNAT
9281 internal files with
9282 @ifclear vms
9283 @code{gcc -c -gnatpg} rather than @code{gcc -c}.
9284 @end ifclear
9285 @ifset vms
9286 the @code{/CHECKS=SUPPRESS_ALL /STYLE_CHECKS=GNAT} switch.
9287 @end ifset
9288
9289 @item ^-b^/ACTIONS=BIND^
9290 @cindex @option{^-b^/ACTIONS=BIND^} (@command{gnatmake})
9291 Bind only. Can be combined with @option{^-c^/ACTIONS=COMPILE^} to do
9292 compilation and binding, but no link.
9293 Can be combined with @option{^-l^/ACTIONS=LINK^}
9294 to do binding and linking. When not combined with
9295 @option{^-c^/ACTIONS=COMPILE^}
9296 all the units in the closure of the main program must have been previously
9297 compiled and must be up to date. The root unit specified by @var{file_name}
9298 may be given without extension, with the source extension or, if no GNAT
9299 Project File is specified, with the ALI file extension.
9300
9301 @item ^-c^/ACTIONS=COMPILE^
9302 @cindex @option{^-c^/ACTIONS=COMPILE^} (@command{gnatmake})
9303 Compile only. Do not perform binding, except when @option{^-b^/ACTIONS=BIND^}
9304 is also specified. Do not perform linking, except if both
9305 @option{^-b^/ACTIONS=BIND^} and
9306 @option{^-l^/ACTIONS=LINK^} are also specified.
9307 If the root unit specified by @var{file_name} is not a main unit, this is the
9308 default. Otherwise @command{gnatmake} will attempt binding and linking
9309 unless all objects are up to date and the executable is more recent than
9310 the objects.
9311
9312 @item ^-C^/MAPPING^
9313 @cindex @option{^-C^/MAPPING^} (@command{gnatmake})
9314 Use a temporary mapping file. A mapping file is a way to communicate
9315 to the compiler two mappings: from unit names to file names (without
9316 any directory information) and from file names to path names (with
9317 full directory information). A mapping file can make the compiler's
9318 file searches faster, especially if there are many source directories,
9319 or the sources are read over a slow network connection. If
9320 @option{^-P^/PROJECT_FILE^} is used, a mapping file is always used, so
9321 @option{^-C^/MAPPING^} is unnecessary; in this case the mapping file
9322 is initially populated based on the project file. If
9323 @option{^-C^/MAPPING^} is used without
9324 @option{^-P^/PROJECT_FILE^},
9325 the mapping file is initially empty. Each invocation of the compiler
9326 will add any newly accessed sources to the mapping file.
9327
9328 @item ^-C=^/USE_MAPPING_FILE=^@var{file}
9329 @cindex @option{^-C=^/USE_MAPPING^} (@command{gnatmake})
9330 Use a specific mapping file. The file, specified as a path name (absolute or
9331 relative) by this switch, should already exist, otherwise the switch is
9332 ineffective. The specified mapping file will be communicated to the compiler.
9333 This switch is not compatible with a project file
9334 (^-P^/PROJECT_FILE=^@var{file}) or with multiple compiling processes
9335 (^-j^/PROCESSES=^nnn, when nnn is greater than 1).
9336
9337 @item ^-d^/DISPLAY_PROGRESS^
9338 @cindex @option{^-d^/DISPLAY_PROGRESS^} (@command{gnatmake})
9339 Display progress for each source, up to date or not, as a single line
9340
9341 @smallexample
9342 completed x out of y (zz%)
9343 @end smallexample
9344
9345 If the file needs to be compiled this is displayed after the invocation of
9346 the compiler. These lines are displayed even in quiet output mode.
9347
9348 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
9349 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@command{gnatmake})
9350 Put all object files and ALI file in directory @var{dir}.
9351 If the @option{^-D^/DIRECTORY_OBJECTS^} switch is not used, all object files
9352 and ALI files go in the current working directory.
9353
9354 This switch cannot be used when using a project file.
9355
9356 @ifclear vms
9357 @item -eL
9358 @cindex @option{-eL} (@command{gnatmake})
9359 @cindex symbolic links
9360 Follow all symbolic links when processing project files.
9361 This should be used if your project uses symbolic links for files or
9362 directories, but is not needed in other cases.
9363
9364 @cindex naming scheme
9365 This also assumes that no directory matches the naming scheme for files (for
9366 instance that you do not have a directory called "sources.ads" when using the
9367 default GNAT naming scheme).
9368
9369 When you do not have to use this switch (ie by default), gnatmake is able to
9370 save a lot of system calls (several per source file and object file), which
9371 can result in a significant speed up to load and manipulate a project file,
9372 especially when using source files from a remote system.
9373
9374 @end ifclear
9375
9376 @item ^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^
9377 @cindex @option{^-eS^/STANDARD_OUTPUT_FOR_COMMANDS^} (@command{gnatmake})
9378 Output the commands for the compiler, the binder and the linker
9379 on ^standard output^SYS$OUTPUT^,
9380 instead of ^standard error^SYS$ERROR^.
9381
9382 @item ^-f^/FORCE_COMPILE^
9383 @cindex @option{^-f^/FORCE_COMPILE^} (@command{gnatmake})
9384 Force recompilations. Recompile all sources, even though some object
9385 files may be up to date, but don't recompile predefined or GNAT internal
9386 files or locked files (files with a write-protected ALI file),
9387 unless the @option{^-a^/ALL_FILES^} switch is also specified.
9388
9389 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
9390 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@command{gnatmake})
9391 When using project files, if some errors or warnings are detected during
9392 parsing and verbose mode is not in effect (no use of switch
9393 ^-v^/VERBOSE^), then error lines start with the full path name of the project
9394 file, rather than its simple file name.
9395
9396 @item ^-g^/DEBUG^
9397 @cindex @option{^-g^/DEBUG^} (@command{gnatmake})
9398 Enable debugging. This switch is simply passed to the compiler and to the
9399 linker.
9400
9401 @item ^-i^/IN_PLACE^
9402 @cindex @option{^-i^/IN_PLACE^} (@command{gnatmake})
9403 In normal mode, @command{gnatmake} compiles all object files and ALI files
9404 into the current directory. If the @option{^-i^/IN_PLACE^} switch is used,
9405 then instead object files and ALI files that already exist are overwritten
9406 in place. This means that once a large project is organized into separate
9407 directories in the desired manner, then @command{gnatmake} will automatically
9408 maintain and update this organization. If no ALI files are found on the
9409 Ada object path (@ref{Search Paths and the Run-Time Library (RTL)}),
9410 the new object and ALI files are created in the
9411 directory containing the source being compiled. If another organization
9412 is desired, where objects and sources are kept in different directories,
9413 a useful technique is to create dummy ALI files in the desired directories.
9414 When detecting such a dummy file, @command{gnatmake} will be forced to
9415 recompile the corresponding source file, and it will be put the resulting
9416 object and ALI files in the directory where it found the dummy file.
9417
9418 @item ^-j^/PROCESSES=^@var{n}
9419 @cindex @option{^-j^/PROCESSES^} (@command{gnatmake})
9420 @cindex Parallel make
9421 Use @var{n} processes to carry out the (re)compilations. On a
9422 multiprocessor machine compilations will occur in parallel. In the
9423 event of compilation errors, messages from various compilations might
9424 get interspersed (but @command{gnatmake} will give you the full ordered
9425 list of failing compiles at the end). If this is problematic, rerun
9426 the make process with n set to 1 to get a clean list of messages.
9427
9428 @item ^-k^/CONTINUE_ON_ERROR^
9429 @cindex @option{^-k^/CONTINUE_ON_ERROR^} (@command{gnatmake})
9430 Keep going. Continue as much as possible after a compilation error. To
9431 ease the programmer's task in case of compilation errors, the list of
9432 sources for which the compile fails is given when @command{gnatmake}
9433 terminates.
9434
9435 If @command{gnatmake} is invoked with several @file{file_names} and with this
9436 switch, if there are compilation errors when building an executable,
9437 @command{gnatmake} will not attempt to build the following executables.
9438
9439 @item ^-l^/ACTIONS=LINK^
9440 @cindex @option{^-l^/ACTIONS=LINK^} (@command{gnatmake})
9441 Link only. Can be combined with @option{^-b^/ACTIONS=BIND^} to binding
9442 and linking. Linking will not be performed if combined with
9443 @option{^-c^/ACTIONS=COMPILE^}
9444 but not with @option{^-b^/ACTIONS=BIND^}.
9445 When not combined with @option{^-b^/ACTIONS=BIND^}
9446 all the units in the closure of the main program must have been previously
9447 compiled and must be up to date, and the main program needs to have been bound.
9448 The root unit specified by @var{file_name}
9449 may be given without extension, with the source extension or, if no GNAT
9450 Project File is specified, with the ALI file extension.
9451
9452 @item ^-m^/MINIMAL_RECOMPILATION^
9453 @cindex @option{^-m^/MINIMAL_RECOMPILATION^} (@command{gnatmake})
9454 Specify that the minimum necessary amount of recompilations
9455 be performed. In this mode @command{gnatmake} ignores time
9456 stamp differences when the only
9457 modifications to a source file consist in adding/removing comments,
9458 empty lines, spaces or tabs. This means that if you have changed the
9459 comments in a source file or have simply reformatted it, using this
9460 switch will tell @command{gnatmake} not to recompile files that depend on it
9461 (provided other sources on which these files depend have undergone no
9462 semantic modifications). Note that the debugging information may be
9463 out of date with respect to the sources if the @option{-m} switch causes
9464 a compilation to be switched, so the use of this switch represents a
9465 trade-off between compilation time and accurate debugging information.
9466
9467 @item ^-M^/DEPENDENCIES_LIST^
9468 @cindex Dependencies, producing list
9469 @cindex @option{^-M^/DEPENDENCIES_LIST^} (@command{gnatmake})
9470 Check if all objects are up to date. If they are, output the object
9471 dependences to @file{stdout} in a form that can be directly exploited in
9472 a @file{Makefile}. By default, each source file is prefixed with its
9473 (relative or absolute) directory name. This name is whatever you
9474 specified in the various @option{^-aI^/SOURCE_SEARCH^}
9475 and @option{^-I^/SEARCH^} switches. If you use
9476 @code{gnatmake ^-M^/DEPENDENCIES_LIST^}
9477 @option{^-q^/QUIET^}
9478 (see below), only the source file names,
9479 without relative paths, are output. If you just specify the
9480 @option{^-M^/DEPENDENCIES_LIST^}
9481 switch, dependencies of the GNAT internal system files are omitted. This
9482 is typically what you want. If you also specify
9483 the @option{^-a^/ALL_FILES^} switch,
9484 dependencies of the GNAT internal files are also listed. Note that
9485 dependencies of the objects in external Ada libraries (see switch
9486 @option{^-aL^/SKIP_MISSING=^}@var{dir} in the following list)
9487 are never reported.
9488
9489 @item ^-n^/DO_OBJECT_CHECK^
9490 @cindex @option{^-n^/DO_OBJECT_CHECK^} (@command{gnatmake})
9491 Don't compile, bind, or link. Checks if all objects are up to date.
9492 If they are not, the full name of the first file that needs to be
9493 recompiled is printed.
9494 Repeated use of this option, followed by compiling the indicated source
9495 file, will eventually result in recompiling all required units.
9496
9497 @item ^-o ^/EXECUTABLE=^@var{exec_name}
9498 @cindex @option{^-o^/EXECUTABLE^} (@command{gnatmake})
9499 Output executable name. The name of the final executable program will be
9500 @var{exec_name}. If the @option{^-o^/EXECUTABLE^} switch is omitted the default
9501 name for the executable will be the name of the input file in appropriate form
9502 for an executable file on the host system.
9503
9504 This switch cannot be used when invoking @command{gnatmake} with several
9505 @file{file_names}.
9506
9507 @item ^-p or --create-missing-dirs^/CREATE_MISSING_DIRS^
9508 @cindex @option{^-p^/CREATE_MISSING_DIRS^} (@command{gnatmake})
9509 When using project files (^-P^/PROJECT_FILE=^@var{project}), create
9510 automatically missing object directories, library directories and exec
9511 directories.
9512
9513 @item ^-P^/PROJECT_FILE=^@var{project}
9514 @cindex @option{^-P^/PROJECT_FILE^} (@command{gnatmake})
9515 Use project file @var{project}. Only one such switch can be used.
9516 @xref{gnatmake and Project Files}.
9517
9518 @item ^-q^/QUIET^
9519 @cindex @option{^-q^/QUIET^} (@command{gnatmake})
9520 Quiet. When this flag is not set, the commands carried out by
9521 @command{gnatmake} are displayed.
9522
9523 @item ^-s^/SWITCH_CHECK/^
9524 @cindex @option{^-s^/SWITCH_CHECK^} (@command{gnatmake})
9525 Recompile if compiler switches have changed since last compilation.
9526 All compiler switches but -I and -o are taken into account in the
9527 following way:
9528 orders between different ``first letter'' switches are ignored, but
9529 orders between same switches are taken into account. For example,
9530 @option{-O -O2} is different than @option{-O2 -O}, but @option{-g -O}
9531 is equivalent to @option{-O -g}.
9532
9533 This switch is recommended when Integrated Preprocessing is used.
9534
9535 @item ^-u^/UNIQUE^
9536 @cindex @option{^-u^/UNIQUE^} (@command{gnatmake})
9537 Unique. Recompile at most the main files. It implies -c. Combined with
9538 -f, it is equivalent to calling the compiler directly. Note that using
9539 ^-u^/UNIQUE^ with a project file and no main has a special meaning
9540 (@pxref{Project Files and Main Subprograms}).
9541
9542 @item ^-U^/ALL_PROJECTS^
9543 @cindex @option{^-U^/ALL_PROJECTS^} (@command{gnatmake})
9544 When used without a project file or with one or several mains on the command
9545 line, is equivalent to ^-u^/UNIQUE^. When used with a project file and no main
9546 on the command line, all sources of all project files are checked and compiled
9547 if not up to date, and libraries are rebuilt, if necessary.
9548
9549 @item ^-v^/REASONS^
9550 @cindex @option{^-v^/REASONS^} (@command{gnatmake})
9551 Verbose. Display the reason for all recompilations @command{gnatmake}
9552 decides are necessary, with the highest verbosity level.
9553
9554 @item ^-vl^/LOW_VERBOSITY^
9555 @cindex @option{^-vl^/LOW_VERBOSITY^} (@command{gnatmake})
9556 Verbosity level Low. Display fewer lines than in verbosity Medium.
9557
9558 @item ^-vm^/MEDIUM_VERBOSITY^
9559 @cindex @option{^-vm^/MEDIUM_VERBOSITY^} (@command{gnatmake})
9560 Verbosity level Medium. Potentially display fewer lines than in verbosity High.
9561
9562 @item ^-vh^/HIGH_VERBOSITY^
9563 @cindex @option{^-vm^/HIGH_VERBOSITY^} (@command{gnatmake})
9564 Verbosity level High. Equivalent to ^-v^/REASONS^.
9565
9566 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
9567 Indicate the verbosity of the parsing of GNAT project files.
9568 @xref{Switches Related to Project Files}.
9569
9570 @item ^-x^/NON_PROJECT_UNIT_COMPILATION^
9571 @cindex @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} (@command{gnatmake})
9572 Indicate that sources that are not part of any Project File may be compiled.
9573 Normally, when using Project Files, only sources that are part of a Project
9574 File may be compile. When this switch is used, a source outside of all Project
9575 Files may be compiled. The ALI file and the object file will be put in the
9576 object directory of the main Project. The compilation switches used will only
9577 be those specified on the command line. Even when
9578 @option{^-x^/NON_PROJECT_UNIT_COMPILATION^} is used, mains specified on the
9579 command line need to be sources of a project file.
9580
9581 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
9582 Indicate that external variable @var{name} has the value @var{value}.
9583 The Project Manager will use this value for occurrences of
9584 @code{external(name)} when parsing the project file.
9585 @xref{Switches Related to Project Files}.
9586
9587 @item ^-z^/NOMAIN^
9588 @cindex @option{^-z^/NOMAIN^} (@command{gnatmake})
9589 No main subprogram. Bind and link the program even if the unit name
9590 given on the command line is a package name. The resulting executable
9591 will execute the elaboration routines of the package and its closure,
9592 then the finalization routines.
9593
9594 @end table
9595
9596 @table @asis
9597 @item @command{gcc} @asis{switches}
9598 @ifclear vms
9599 Any uppercase or multi-character switch that is not a @command{gnatmake} switch
9600 is passed to @command{gcc} (e.g.@: @option{-O}, @option{-gnato,} etc.)
9601 @end ifclear
9602 @ifset vms
9603 Any qualifier that cannot be recognized as a qualifier for @code{GNAT MAKE}
9604 but is recognizable as a valid qualifier for @code{GNAT COMPILE} is
9605 automatically treated as a compiler switch, and passed on to all
9606 compilations that are carried out.
9607 @end ifset
9608 @end table
9609
9610 @noindent
9611 Source and library search path switches:
9612
9613 @table @option
9614 @c !sort!
9615 @item ^-aI^/SOURCE_SEARCH=^@var{dir}
9616 @cindex @option{^-aI^/SOURCE_SEARCH^} (@command{gnatmake})
9617 When looking for source files also look in directory @var{dir}.
9618 The order in which source files search is undertaken is
9619 described in @ref{Search Paths and the Run-Time Library (RTL)}.
9620
9621 @item ^-aL^/SKIP_MISSING=^@var{dir}
9622 @cindex @option{^-aL^/SKIP_MISSING^} (@command{gnatmake})
9623 Consider @var{dir} as being an externally provided Ada library.
9624 Instructs @command{gnatmake} to skip compilation units whose @file{.ALI}
9625 files have been located in directory @var{dir}. This allows you to have
9626 missing bodies for the units in @var{dir} and to ignore out of date bodies
9627 for the same units. You still need to specify
9628 the location of the specs for these units by using the switches
9629 @option{^-aI^/SOURCE_SEARCH=^@var{dir}}
9630 or @option{^-I^/SEARCH=^@var{dir}}.
9631 Note: this switch is provided for compatibility with previous versions
9632 of @command{gnatmake}. The easier method of causing standard libraries
9633 to be excluded from consideration is to write-protect the corresponding
9634 ALI files.
9635
9636 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
9637 @cindex @option{^-aO^/OBJECT_SEARCH^} (@command{gnatmake})
9638 When searching for library and object files, look in directory
9639 @var{dir}. The order in which library files are searched is described in
9640 @ref{Search Paths for gnatbind}.
9641
9642 @item ^-A^/CONDITIONAL_SOURCE_SEARCH=^@var{dir}
9643 @cindex Search paths, for @command{gnatmake}
9644 @cindex @option{^-A^/CONDITIONAL_SOURCE_SEARCH^} (@command{gnatmake})
9645 Equivalent to @option{^-aL^/SKIP_MISSING=^@var{dir}
9646 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9647
9648 @item ^-I^/SEARCH=^@var{dir}
9649 @cindex @option{^-I^/SEARCH^} (@command{gnatmake})
9650 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}
9651 ^-aI^/SOURCE_SEARCH=^@var{dir}}.
9652
9653 @item ^-I-^/NOCURRENT_DIRECTORY^
9654 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@command{gnatmake})
9655 @cindex Source files, suppressing search
9656 Do not look for source files in the directory containing the source
9657 file named in the command line.
9658 Do not look for ALI or object files in the directory
9659 where @command{gnatmake} was invoked.
9660
9661 @item ^-L^/LIBRARY_SEARCH=^@var{dir}
9662 @cindex @option{^-L^/LIBRARY_SEARCH^} (@command{gnatmake})
9663 @cindex Linker libraries
9664 Add directory @var{dir} to the list of directories in which the linker
9665 will search for libraries. This is equivalent to
9666 @option{-largs ^-L^/LIBRARY_SEARCH=^}@var{dir}.
9667 @ifclear vms
9668 Furthermore, under Windows, the sources pointed to by the libraries path
9669 set in the registry are not searched for.
9670 @end ifclear
9671
9672 @item -nostdinc
9673 @cindex @option{-nostdinc} (@command{gnatmake})
9674 Do not look for source files in the system default directory.
9675
9676 @item -nostdlib
9677 @cindex @option{-nostdlib} (@command{gnatmake})
9678 Do not look for library files in the system default directory.
9679
9680 @item --RTS=@var{rts-path}
9681 @cindex @option{--RTS} (@command{gnatmake})
9682 Specifies the default location of the runtime library. GNAT looks for the
9683 runtime
9684 in the following directories, and stops as soon as a valid runtime is found
9685 (@file{adainclude} or @file{ada_source_path}, and @file{adalib} or
9686 @file{ada_object_path} present):
9687
9688 @itemize @bullet
9689 @item <current directory>/$rts_path
9690
9691 @item <default-search-dir>/$rts_path
9692
9693 @item <default-search-dir>/rts-$rts_path
9694 @end itemize
9695
9696 @noindent
9697 The selected path is handled like a normal RTS path.
9698
9699 @end table
9700
9701 @node Mode Switches for gnatmake
9702 @section Mode Switches for @command{gnatmake}
9703
9704 @noindent
9705 The mode switches (referred to as @code{mode_switches}) allow the
9706 inclusion of switches that are to be passed to the compiler itself, the
9707 binder or the linker. The effect of a mode switch is to cause all
9708 subsequent switches up to the end of the switch list, or up to the next
9709 mode switch, to be interpreted as switches to be passed on to the
9710 designated component of GNAT.
9711
9712 @table @option
9713 @c !sort!
9714 @item -cargs @var{switches}
9715 @cindex @option{-cargs} (@command{gnatmake})
9716 Compiler switches. Here @var{switches} is a list of switches
9717 that are valid switches for @command{gcc}. They will be passed on to
9718 all compile steps performed by @command{gnatmake}.
9719
9720 @item -bargs @var{switches}
9721 @cindex @option{-bargs} (@command{gnatmake})
9722 Binder switches. Here @var{switches} is a list of switches
9723 that are valid switches for @code{gnatbind}. They will be passed on to
9724 all bind steps performed by @command{gnatmake}.
9725
9726 @item -largs @var{switches}
9727 @cindex @option{-largs} (@command{gnatmake})
9728 Linker switches. Here @var{switches} is a list of switches
9729 that are valid switches for @command{gnatlink}. They will be passed on to
9730 all link steps performed by @command{gnatmake}.
9731
9732 @item -margs @var{switches}
9733 @cindex @option{-margs} (@command{gnatmake})
9734 Make switches. The switches are directly interpreted by @command{gnatmake},
9735 regardless of any previous occurrence of @option{-cargs}, @option{-bargs}
9736 or @option{-largs}.
9737 @end table
9738
9739 @node Notes on the Command Line
9740 @section Notes on the Command Line
9741
9742 @noindent
9743 This section contains some additional useful notes on the operation
9744 of the @command{gnatmake} command.
9745
9746 @itemize @bullet
9747 @item
9748 @cindex Recompilation, by @command{gnatmake}
9749 If @command{gnatmake} finds no ALI files, it recompiles the main program
9750 and all other units required by the main program.
9751 This means that @command{gnatmake}
9752 can be used for the initial compile, as well as during subsequent steps of
9753 the development cycle.
9754
9755 @item
9756 If you enter @code{gnatmake @var{file}.adb}, where @file{@var{file}.adb}
9757 is a subunit or body of a generic unit, @command{gnatmake} recompiles
9758 @file{@var{file}.adb} (because it finds no ALI) and stops, issuing a
9759 warning.
9760
9761 @item
9762 In @command{gnatmake} the switch @option{^-I^/SEARCH^}
9763 is used to specify both source and
9764 library file paths. Use @option{^-aI^/SOURCE_SEARCH^}
9765 instead if you just want to specify
9766 source paths only and @option{^-aO^/OBJECT_SEARCH^}
9767 if you want to specify library paths
9768 only.
9769
9770 @item
9771 @command{gnatmake} will ignore any files whose ALI file is write-protected.
9772 This may conveniently be used to exclude standard libraries from
9773 consideration and in particular it means that the use of the
9774 @option{^-f^/FORCE_COMPILE^} switch will not recompile these files
9775 unless @option{^-a^/ALL_FILES^} is also specified.
9776
9777 @item
9778 @command{gnatmake} has been designed to make the use of Ada libraries
9779 particularly convenient. Assume you have an Ada library organized
9780 as follows: @i{^obj-dir^[OBJ_DIR]^} contains the objects and ALI files for
9781 of your Ada compilation units,
9782 whereas @i{^include-dir^[INCLUDE_DIR]^} contains the
9783 specs of these units, but no bodies. Then to compile a unit
9784 stored in @code{main.adb}, which uses this Ada library you would just type
9785
9786 @smallexample
9787 @ifclear vms
9788 $ gnatmake -aI@var{include-dir} -aL@var{obj-dir} main
9789 @end ifclear
9790 @ifset vms
9791 $ gnatmake /SOURCE_SEARCH=@i{[INCLUDE_DIR]}
9792 /SKIP_MISSING=@i{[OBJ_DIR]} main
9793 @end ifset
9794 @end smallexample
9795
9796 @item
9797 Using @command{gnatmake} along with the
9798 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}
9799 switch provides a mechanism for avoiding unnecessary recompilations. Using
9800 this switch,
9801 you can update the comments/format of your
9802 source files without having to recompile everything. Note, however, that
9803 adding or deleting lines in a source files may render its debugging
9804 info obsolete. If the file in question is a spec, the impact is rather
9805 limited, as that debugging info will only be useful during the
9806 elaboration phase of your program. For bodies the impact can be more
9807 significant. In all events, your debugger will warn you if a source file
9808 is more recent than the corresponding object, and alert you to the fact
9809 that the debugging information may be out of date.
9810 @end itemize
9811
9812 @node How gnatmake Works
9813 @section How @command{gnatmake} Works
9814
9815 @noindent
9816 Generally @command{gnatmake} automatically performs all necessary
9817 recompilations and you don't need to worry about how it works. However,
9818 it may be useful to have some basic understanding of the @command{gnatmake}
9819 approach and in particular to understand how it uses the results of
9820 previous compilations without incorrectly depending on them.
9821
9822 First a definition: an object file is considered @dfn{up to date} if the
9823 corresponding ALI file exists and if all the source files listed in the
9824 dependency section of this ALI file have time stamps matching those in
9825 the ALI file. This means that neither the source file itself nor any
9826 files that it depends on have been modified, and hence there is no need
9827 to recompile this file.
9828
9829 @command{gnatmake} works by first checking if the specified main unit is up
9830 to date. If so, no compilations are required for the main unit. If not,
9831 @command{gnatmake} compiles the main program to build a new ALI file that
9832 reflects the latest sources. Then the ALI file of the main unit is
9833 examined to find all the source files on which the main program depends,
9834 and @command{gnatmake} recursively applies the above procedure on all these
9835 files.
9836
9837 This process ensures that @command{gnatmake} only trusts the dependencies
9838 in an existing ALI file if they are known to be correct. Otherwise it
9839 always recompiles to determine a new, guaranteed accurate set of
9840 dependencies. As a result the program is compiled ``upside down'' from what may
9841 be more familiar as the required order of compilation in some other Ada
9842 systems. In particular, clients are compiled before the units on which
9843 they depend. The ability of GNAT to compile in any order is critical in
9844 allowing an order of compilation to be chosen that guarantees that
9845 @command{gnatmake} will recompute a correct set of new dependencies if
9846 necessary.
9847
9848 When invoking @command{gnatmake} with several @var{file_names}, if a unit is
9849 imported by several of the executables, it will be recompiled at most once.
9850
9851 Note: when using non-standard naming conventions
9852 (@pxref{Using Other File Names}), changing through a configuration pragmas
9853 file the version of a source and invoking @command{gnatmake} to recompile may
9854 have no effect, if the previous version of the source is still accessible
9855 by @command{gnatmake}. It may be necessary to use the switch
9856 ^-f^/FORCE_COMPILE^.
9857
9858 @node Examples of gnatmake Usage
9859 @section Examples of @command{gnatmake} Usage
9860
9861 @table @code
9862 @item gnatmake hello.adb
9863 Compile all files necessary to bind and link the main program
9864 @file{hello.adb} (containing unit @code{Hello}) and bind and link the
9865 resulting object files to generate an executable file @file{^hello^HELLO.EXE^}.
9866
9867 @item gnatmake main1 main2 main3
9868 Compile all files necessary to bind and link the main programs
9869 @file{main1.adb} (containing unit @code{Main1}), @file{main2.adb}
9870 (containing unit @code{Main2}) and @file{main3.adb}
9871 (containing unit @code{Main3}) and bind and link the resulting object files
9872 to generate three executable files @file{^main1^MAIN1.EXE^},
9873 @file{^main2^MAIN2.EXE^}
9874 and @file{^main3^MAIN3.EXE^}.
9875
9876 @ifclear vms
9877 @item gnatmake -q Main_Unit -cargs -O2 -bargs -l
9878 @end ifclear
9879
9880 @ifset vms
9881 @item gnatmake Main_Unit /QUIET
9882 /COMPILER_QUALIFIERS /OPTIMIZE=ALL
9883 /BINDER_QUALIFIERS /ORDER_OF_ELABORATION
9884 @end ifset
9885 Compile all files necessary to bind and link the main program unit
9886 @code{Main_Unit} (from file @file{main_unit.adb}). All compilations will
9887 be done with optimization level 2 and the order of elaboration will be
9888 listed by the binder. @command{gnatmake} will operate in quiet mode, not
9889 displaying commands it is executing.
9890 @end table
9891
9892 @c *************************
9893 @node Improving Performance
9894 @chapter Improving Performance
9895 @cindex Improving performance
9896
9897 @noindent
9898 This chapter presents several topics related to program performance.
9899 It first describes some of the tradeoffs that need to be considered
9900 and some of the techniques for making your program run faster.
9901 It then documents the @command{gnatelim} tool and unused subprogram/data
9902 elimination feature, which can reduce the size of program executables.
9903
9904 Note: to invoke @command{gnatelim} with a project file, use the @code{gnat}
9905 driver (see @ref{The GNAT Driver and Project Files}).
9906
9907 @ifnottex
9908 @menu
9909 * Performance Considerations::
9910 * Text_IO Suggestions::
9911 * Reducing Size of Ada Executables with gnatelim::
9912 * Reducing Size of Executables with unused subprogram/data elimination::
9913 @end menu
9914 @end ifnottex
9915
9916 @c *****************************
9917 @node Performance Considerations
9918 @section Performance Considerations
9919
9920 @noindent
9921 The GNAT system provides a number of options that allow a trade-off
9922 between
9923
9924 @itemize @bullet
9925 @item
9926 performance of the generated code
9927
9928 @item
9929 speed of compilation
9930
9931 @item
9932 minimization of dependences and recompilation
9933
9934 @item
9935 the degree of run-time checking.
9936 @end itemize
9937
9938 @noindent
9939 The defaults (if no options are selected) aim at improving the speed
9940 of compilation and minimizing dependences, at the expense of performance
9941 of the generated code:
9942
9943 @itemize @bullet
9944 @item
9945 no optimization
9946
9947 @item
9948 no inlining of subprogram calls
9949
9950 @item
9951 all run-time checks enabled except overflow and elaboration checks
9952 @end itemize
9953
9954 @noindent
9955 These options are suitable for most program development purposes. This
9956 chapter describes how you can modify these choices, and also provides
9957 some guidelines on debugging optimized code.
9958
9959 @menu
9960 * Controlling Run-Time Checks::
9961 * Use of Restrictions::
9962 * Optimization Levels::
9963 * Debugging Optimized Code::
9964 * Inlining of Subprograms::
9965 * Other Optimization Switches::
9966 * Optimization and Strict Aliasing::
9967
9968 @ifset vms
9969 * Coverage Analysis::
9970 @end ifset
9971 @end menu
9972
9973 @node Controlling Run-Time Checks
9974 @subsection Controlling Run-Time Checks
9975
9976 @noindent
9977 By default, GNAT generates all run-time checks, except integer overflow
9978 checks, stack overflow checks, and checks for access before elaboration on
9979 subprogram calls. The latter are not required in default mode, because all
9980 necessary checking is done at compile time.
9981 @cindex @option{-gnatp} (@command{gcc})
9982 @cindex @option{-gnato} (@command{gcc})
9983 Two gnat switches, @option{-gnatp} and @option{-gnato} allow this default to
9984 be modified. @xref{Run-Time Checks}.
9985
9986 Our experience is that the default is suitable for most development
9987 purposes.
9988
9989 We treat integer overflow specially because these
9990 are quite expensive and in our experience are not as important as other
9991 run-time checks in the development process. Note that division by zero
9992 is not considered an overflow check, and divide by zero checks are
9993 generated where required by default.
9994
9995 Elaboration checks are off by default, and also not needed by default, since
9996 GNAT uses a static elaboration analysis approach that avoids the need for
9997 run-time checking. This manual contains a full chapter discussing the issue
9998 of elaboration checks, and if the default is not satisfactory for your use,
9999 you should read this chapter.
10000
10001 For validity checks, the minimal checks required by the Ada Reference
10002 Manual (for case statements and assignments to array elements) are on
10003 by default. These can be suppressed by use of the @option{-gnatVn} switch.
10004 Note that in Ada 83, there were no validity checks, so if the Ada 83 mode
10005 is acceptable (or when comparing GNAT performance with an Ada 83 compiler),
10006 it may be reasonable to routinely use @option{-gnatVn}. Validity checks
10007 are also suppressed entirely if @option{-gnatp} is used.
10008
10009 @cindex Overflow checks
10010 @cindex Checks, overflow
10011 @findex Suppress
10012 @findex Unsuppress
10013 @cindex pragma Suppress
10014 @cindex pragma Unsuppress
10015 Note that the setting of the switches controls the default setting of
10016 the checks. They may be modified using either @code{pragma Suppress} (to
10017 remove checks) or @code{pragma Unsuppress} (to add back suppressed
10018 checks) in the program source.
10019
10020 @node Use of Restrictions
10021 @subsection Use of Restrictions
10022
10023 @noindent
10024 The use of pragma Restrictions allows you to control which features are
10025 permitted in your program. Apart from the obvious point that if you avoid
10026 relatively expensive features like finalization (enforceable by the use
10027 of pragma Restrictions (No_Finalization), the use of this pragma does not
10028 affect the generated code in most cases.
10029
10030 One notable exception to this rule is that the possibility of task abort
10031 results in some distributed overhead, particularly if finalization or
10032 exception handlers are used. The reason is that certain sections of code
10033 have to be marked as non-abortable.
10034
10035 If you use neither the @code{abort} statement, nor asynchronous transfer
10036 of control (@code{select @dots{} then abort}), then this distributed overhead
10037 is removed, which may have a general positive effect in improving
10038 overall performance. Especially code involving frequent use of tasking
10039 constructs and controlled types will show much improved performance.
10040 The relevant restrictions pragmas are
10041
10042 @smallexample @c ada
10043 pragma Restrictions (No_Abort_Statements);
10044 pragma Restrictions (Max_Asynchronous_Select_Nesting => 0);
10045 @end smallexample
10046
10047 @noindent
10048 It is recommended that these restriction pragmas be used if possible. Note
10049 that this also means that you can write code without worrying about the
10050 possibility of an immediate abort at any point.
10051
10052 @node Optimization Levels
10053 @subsection Optimization Levels
10054 @cindex @option{^-O^/OPTIMIZE^} (@command{gcc})
10055
10056 @noindent
10057 Without any optimization ^option,^qualifier,^
10058 the compiler's goal is to reduce the cost of
10059 compilation and to make debugging produce the expected results.
10060 Statements are independent: if you stop the program with a breakpoint between
10061 statements, you can then assign a new value to any variable or change
10062 the program counter to any other statement in the subprogram and get exactly
10063 the results you would expect from the source code.
10064
10065 Turning on optimization makes the compiler attempt to improve the
10066 performance and/or code size at the expense of compilation time and
10067 possibly the ability to debug the program.
10068
10069 If you use multiple
10070 ^-O options, with or without level numbers,^/OPTIMIZE qualifiers,^
10071 the last such option is the one that is effective.
10072
10073 @noindent
10074 The default is optimization off. This results in the fastest compile
10075 times, but GNAT makes absolutely no attempt to optimize, and the
10076 generated programs are considerably larger and slower than when
10077 optimization is enabled. You can use the
10078 @ifclear vms
10079 @option{-O} switch (the permitted forms are @option{-O0}, @option{-O1}
10080 @option{-O2}, @option{-O3}, and @option{-Os})
10081 @end ifclear
10082 @ifset vms
10083 @code{OPTIMIZE} qualifier
10084 @end ifset
10085 to @command{gcc} to control the optimization level:
10086
10087 @table @option
10088 @item ^-O0^/OPTIMIZE=NONE^
10089 No optimization (the default);
10090 generates unoptimized code but has
10091 the fastest compilation time.
10092
10093 Note that many other compilers do fairly extensive optimization
10094 even if ``no optimization'' is specified. With gcc, it is
10095 very unusual to use ^-O0^/OPTIMIZE=NONE^ for production if
10096 execution time is of any concern, since ^-O0^/OPTIMIZE=NONE^
10097 really does mean no optimization at all. This difference between
10098 gcc and other compilers should be kept in mind when doing
10099 performance comparisons.
10100
10101 @item ^-O1^/OPTIMIZE=SOME^
10102 Moderate optimization;
10103 optimizes reasonably well but does not
10104 degrade compilation time significantly.
10105
10106 @item ^-O2^/OPTIMIZE=ALL^
10107 @ifset vms
10108 @itemx /OPTIMIZE=DEVELOPMENT
10109 @end ifset
10110 Full optimization;
10111 generates highly optimized code and has
10112 the slowest compilation time.
10113
10114 @item ^-O3^/OPTIMIZE=INLINING^
10115 Full optimization as in @option{-O2},
10116 and also attempts automatic inlining of small
10117 subprograms within a unit (@pxref{Inlining of Subprograms}).
10118
10119 @item ^-Os^/OPTIMIZE=SPACE^
10120 Optimize space usage of resulting program.
10121 @end table
10122
10123 @noindent
10124 Higher optimization levels perform more global transformations on the
10125 program and apply more expensive analysis algorithms in order to generate
10126 faster and more compact code. The price in compilation time, and the
10127 resulting improvement in execution time,
10128 both depend on the particular application and the hardware environment.
10129 You should experiment to find the best level for your application.
10130
10131 Since the precise set of optimizations done at each level will vary from
10132 release to release (and sometime from target to target), it is best to think
10133 of the optimization settings in general terms.
10134 @xref{Optimize Options,, Options That Control Optimization, gcc, Using
10135 the GNU Compiler Collection (GCC)}, for details about
10136 ^the @option{-O} settings and a number of @option{-f} options that^how to^
10137 individually enable or disable specific optimizations.
10138
10139 Unlike some other compilation systems, ^@command{gcc}^GNAT^ has
10140 been tested extensively at all optimization levels. There are some bugs
10141 which appear only with optimization turned on, but there have also been
10142 bugs which show up only in @emph{unoptimized} code. Selecting a lower
10143 level of optimization does not improve the reliability of the code
10144 generator, which in practice is highly reliable at all optimization
10145 levels.
10146
10147 Note regarding the use of @option{-O3}: The use of this optimization level
10148 is generally discouraged with GNAT, since it often results in larger
10149 executables which run more slowly. See further discussion of this point
10150 in @ref{Inlining of Subprograms}.
10151
10152 @node Debugging Optimized Code
10153 @subsection Debugging Optimized Code
10154 @cindex Debugging optimized code
10155 @cindex Optimization and debugging
10156
10157 @noindent
10158 Although it is possible to do a reasonable amount of debugging at
10159 @ifclear vms
10160 nonzero optimization levels,
10161 the higher the level the more likely that
10162 @end ifclear
10163 @ifset vms
10164 @option{/OPTIMIZE} settings other than @code{NONE},
10165 such settings will make it more likely that
10166 @end ifset
10167 source-level constructs will have been eliminated by optimization.
10168 For example, if a loop is strength-reduced, the loop
10169 control variable may be completely eliminated and thus cannot be
10170 displayed in the debugger.
10171 This can only happen at @option{-O2} or @option{-O3}.
10172 Explicit temporary variables that you code might be eliminated at
10173 ^level^setting^ @option{-O1} or higher.
10174
10175 The use of the @option{^-g^/DEBUG^} switch,
10176 @cindex @option{^-g^/DEBUG^} (@command{gcc})
10177 which is needed for source-level debugging,
10178 affects the size of the program executable on disk,
10179 and indeed the debugging information can be quite large.
10180 However, it has no effect on the generated code (and thus does not
10181 degrade performance)
10182
10183 Since the compiler generates debugging tables for a compilation unit before
10184 it performs optimizations, the optimizing transformations may invalidate some
10185 of the debugging data. You therefore need to anticipate certain
10186 anomalous situations that may arise while debugging optimized code.
10187 These are the most common cases:
10188
10189 @enumerate
10190 @item
10191 @i{The ``hopping Program Counter'':} Repeated @code{step} or @code{next}
10192 commands show
10193 the PC bouncing back and forth in the code. This may result from any of
10194 the following optimizations:
10195
10196 @itemize @bullet
10197 @item
10198 @i{Common subexpression elimination:} using a single instance of code for a
10199 quantity that the source computes several times. As a result you
10200 may not be able to stop on what looks like a statement.
10201
10202 @item
10203 @i{Invariant code motion:} moving an expression that does not change within a
10204 loop, to the beginning of the loop.
10205
10206 @item
10207 @i{Instruction scheduling:} moving instructions so as to
10208 overlap loads and stores (typically) with other code, or in
10209 general to move computations of values closer to their uses. Often
10210 this causes you to pass an assignment statement without the assignment
10211 happening and then later bounce back to the statement when the
10212 value is actually needed. Placing a breakpoint on a line of code
10213 and then stepping over it may, therefore, not always cause all the
10214 expected side-effects.
10215 @end itemize
10216
10217 @item
10218 @i{The ``big leap'':} More commonly known as @emph{cross-jumping}, in which
10219 two identical pieces of code are merged and the program counter suddenly
10220 jumps to a statement that is not supposed to be executed, simply because
10221 it (and the code following) translates to the same thing as the code
10222 that @emph{was} supposed to be executed. This effect is typically seen in
10223 sequences that end in a jump, such as a @code{goto}, a @code{return}, or
10224 a @code{break} in a C @code{^switch^switch^} statement.
10225
10226 @item
10227 @i{The ``roving variable'':} The symptom is an unexpected value in a variable.
10228 There are various reasons for this effect:
10229
10230 @itemize @bullet
10231 @item
10232 In a subprogram prologue, a parameter may not yet have been moved to its
10233 ``home''.
10234
10235 @item
10236 A variable may be dead, and its register re-used. This is
10237 probably the most common cause.
10238
10239 @item
10240 As mentioned above, the assignment of a value to a variable may
10241 have been moved.
10242
10243 @item
10244 A variable may be eliminated entirely by value propagation or
10245 other means. In this case, GCC may incorrectly generate debugging
10246 information for the variable
10247 @end itemize
10248
10249 @noindent
10250 In general, when an unexpected value appears for a local variable or parameter
10251 you should first ascertain if that value was actually computed by
10252 your program, as opposed to being incorrectly reported by the debugger.
10253 Record fields or
10254 array elements in an object designated by an access value
10255 are generally less of a problem, once you have ascertained that the access
10256 value is sensible.
10257 Typically, this means checking variables in the preceding code and in the
10258 calling subprogram to verify that the value observed is explainable from other
10259 values (one must apply the procedure recursively to those
10260 other values); or re-running the code and stopping a little earlier
10261 (perhaps before the call) and stepping to better see how the variable obtained
10262 the value in question; or continuing to step @emph{from} the point of the
10263 strange value to see if code motion had simply moved the variable's
10264 assignments later.
10265 @end enumerate
10266
10267 @noindent
10268 In light of such anomalies, a recommended technique is to use @option{-O0}
10269 early in the software development cycle, when extensive debugging capabilities
10270 are most needed, and then move to @option{-O1} and later @option{-O2} as
10271 the debugger becomes less critical.
10272 Whether to use the @option{^-g^/DEBUG^} switch in the release version is
10273 a release management issue.
10274 @ifclear vms
10275 Note that if you use @option{-g} you can then use the @command{strip} program
10276 on the resulting executable,
10277 which removes both debugging information and global symbols.
10278 @end ifclear
10279
10280 @node Inlining of Subprograms
10281 @subsection Inlining of Subprograms
10282
10283 @noindent
10284 A call to a subprogram in the current unit is inlined if all the
10285 following conditions are met:
10286
10287 @itemize @bullet
10288 @item
10289 The optimization level is at least @option{-O1}.
10290
10291 @item
10292 The called subprogram is suitable for inlining: It must be small enough
10293 and not contain something that @command{gcc} cannot support in inlined
10294 subprograms.
10295
10296 @item
10297 @cindex pragma Inline
10298 @findex Inline
10299 Either @code{pragma Inline} applies to the subprogram, or it is local
10300 to the unit and called once from within it, or it is small and automatic
10301 inlining (optimization level @option{-O3}) is specified.
10302 @end itemize
10303
10304 @noindent
10305 Calls to subprograms in @code{with}'ed units are normally not inlined.
10306 To achieve actual inlining (that is, replacement of the call by the code
10307 in the body of the subprogram), the following conditions must all be true.
10308
10309 @itemize @bullet
10310 @item
10311 The optimization level is at least @option{-O1}.
10312
10313 @item
10314 The called subprogram is suitable for inlining: It must be small enough
10315 and not contain something that @command{gcc} cannot support in inlined
10316 subprograms.
10317
10318 @item
10319 The call appears in a body (not in a package spec).
10320
10321 @item
10322 There is a @code{pragma Inline} for the subprogram.
10323
10324 @item
10325 @cindex @option{-gnatn} (@command{gcc})
10326 The @option{^-gnatn^/INLINE^} switch
10327 is used in the @command{gcc} command line
10328 @end itemize
10329
10330 Even if all these conditions are met, it may not be possible for
10331 the compiler to inline the call, due to the length of the body,
10332 or features in the body that make it impossible for the compiler
10333 to do the inlining.
10334
10335 Note that specifying the @option{-gnatn} switch causes additional
10336 compilation dependencies. Consider the following:
10337
10338 @smallexample @c ada
10339 @cartouche
10340 package R is
10341 procedure Q;
10342 pragma Inline (Q);
10343 end R;
10344 package body R is
10345 @dots{}
10346 end R;
10347
10348 with R;
10349 procedure Main is
10350 begin
10351 @dots{}
10352 R.Q;
10353 end Main;
10354 @end cartouche
10355 @end smallexample
10356
10357 @noindent
10358 With the default behavior (no @option{-gnatn} switch specified), the
10359 compilation of the @code{Main} procedure depends only on its own source,
10360 @file{main.adb}, and the spec of the package in file @file{r.ads}. This
10361 means that editing the body of @code{R} does not require recompiling
10362 @code{Main}.
10363
10364 On the other hand, the call @code{R.Q} is not inlined under these
10365 circumstances. If the @option{-gnatn} switch is present when @code{Main}
10366 is compiled, the call will be inlined if the body of @code{Q} is small
10367 enough, but now @code{Main} depends on the body of @code{R} in
10368 @file{r.adb} as well as on the spec. This means that if this body is edited,
10369 the main program must be recompiled. Note that this extra dependency
10370 occurs whether or not the call is in fact inlined by @command{gcc}.
10371
10372 The use of front end inlining with @option{-gnatN} generates similar
10373 additional dependencies.
10374
10375 @cindex @option{^-fno-inline^/INLINE=SUPPRESS^} (@command{gcc})
10376 Note: The @option{^-fno-inline^/INLINE=SUPPRESS^} switch
10377 can be used to prevent
10378 all inlining. This switch overrides all other conditions and ensures
10379 that no inlining occurs. The extra dependences resulting from
10380 @option{-gnatn} will still be active, even if
10381 this switch is used to suppress the resulting inlining actions.
10382
10383 @cindex @option{-fno-inline-functions} (@command{gcc})
10384 Note: The @option{-fno-inline-functions} switch can be used to prevent
10385 automatic inlining of small subprograms if @option{-O3} is used.
10386
10387 @cindex @option{-fno-inline-functions-called-once} (@command{gcc})
10388 Note: The @option{-fno-inline-functions-called-once} switch
10389 can be used to prevent inlining of subprograms local to the unit
10390 and called once from within it if @option{-O1} is used.
10391
10392 Note regarding the use of @option{-O3}: There is no difference in inlining
10393 behavior between @option{-O2} and @option{-O3} for subprograms with an explicit
10394 pragma @code{Inline} assuming the use of @option{-gnatn}
10395 or @option{-gnatN} (the switches that activate inlining). If you have used
10396 pragma @code{Inline} in appropriate cases, then it is usually much better
10397 to use @option{-O2} and @option{-gnatn} and avoid the use of @option{-O3} which
10398 in this case only has the effect of inlining subprograms you did not
10399 think should be inlined. We often find that the use of @option{-O3} slows
10400 down code by performing excessive inlining, leading to increased instruction
10401 cache pressure from the increased code size. So the bottom line here is
10402 that you should not automatically assume that @option{-O3} is better than
10403 @option{-O2}, and indeed you should use @option{-O3} only if tests show that
10404 it actually improves performance.
10405
10406 @node Other Optimization Switches
10407 @subsection Other Optimization Switches
10408 @cindex Optimization Switches
10409
10410 Since @code{GNAT} uses the @command{gcc} back end, all the specialized
10411 @command{gcc} optimization switches are potentially usable. These switches
10412 have not been extensively tested with GNAT but can generally be expected
10413 to work. Examples of switches in this category are
10414 @option{-funroll-loops} and
10415 the various target-specific @option{-m} options (in particular, it has been
10416 observed that @option{-march=pentium4} can significantly improve performance
10417 on appropriate machines). For full details of these switches, see
10418 @ref{Submodel Options,, Hardware Models and Configurations, gcc, Using
10419 the GNU Compiler Collection (GCC)}.
10420
10421 @node Optimization and Strict Aliasing
10422 @subsection Optimization and Strict Aliasing
10423 @cindex Aliasing
10424 @cindex Strict Aliasing
10425 @cindex No_Strict_Aliasing
10426
10427 @noindent
10428 The strong typing capabilities of Ada allow an optimizer to generate
10429 efficient code in situations where other languages would be forced to
10430 make worst case assumptions preventing such optimizations. Consider
10431 the following example:
10432
10433 @smallexample @c ada
10434 @cartouche
10435 procedure R is
10436 type Int1 is new Integer;
10437 type Int2 is new Integer;
10438 type Int1A is access Int1;
10439 type Int2A is access Int2;
10440 Int1V : Int1A;
10441 Int2V : Int2A;
10442 @dots{}
10443
10444 begin
10445 @dots{}
10446 for J in Data'Range loop
10447 if Data (J) = Int1V.all then
10448 Int2V.all := Int2V.all + 1;
10449 end if;
10450 end loop;
10451 @dots{}
10452 end R;
10453 @end cartouche
10454 @end smallexample
10455
10456 @noindent
10457 In this example, since the variable @code{Int1V} can only access objects
10458 of type @code{Int1}, and @code{Int2V} can only access objects of type
10459 @code{Int2}, there is no possibility that the assignment to
10460 @code{Int2V.all} affects the value of @code{Int1V.all}. This means that
10461 the compiler optimizer can "know" that the value @code{Int1V.all} is constant
10462 for all iterations of the loop and avoid the extra memory reference
10463 required to dereference it each time through the loop.
10464
10465 This kind of optimization, called strict aliasing analysis, is
10466 triggered by specifying an optimization level of @option{-O2} or
10467 higher or @option{-Os} and allows @code{GNAT} to generate more efficient code
10468 when access values are involved.
10469
10470 However, although this optimization is always correct in terms of
10471 the formal semantics of the Ada Reference Manual, difficulties can
10472 arise if features like @code{Unchecked_Conversion} are used to break
10473 the typing system. Consider the following complete program example:
10474
10475 @smallexample @c ada
10476 @cartouche
10477 package p1 is
10478 type int1 is new integer;
10479 type int2 is new integer;
10480 type a1 is access int1;
10481 type a2 is access int2;
10482 end p1;
10483
10484 with p1; use p1;
10485 package p2 is
10486 function to_a2 (Input : a1) return a2;
10487 end p2;
10488
10489 with Unchecked_Conversion;
10490 package body p2 is
10491 function to_a2 (Input : a1) return a2 is
10492 function to_a2u is
10493 new Unchecked_Conversion (a1, a2);
10494 begin
10495 return to_a2u (Input);
10496 end to_a2;
10497 end p2;
10498
10499 with p2; use p2;
10500 with p1; use p1;
10501 with Text_IO; use Text_IO;
10502 procedure m is
10503 v1 : a1 := new int1;
10504 v2 : a2 := to_a2 (v1);
10505 begin
10506 v1.all := 1;
10507 v2.all := 0;
10508 put_line (int1'image (v1.all));
10509 end;
10510 @end cartouche
10511 @end smallexample
10512
10513 @noindent
10514 This program prints out 0 in @option{-O0} or @option{-O1}
10515 mode, but it prints out 1 in @option{-O2} mode. That's
10516 because in strict aliasing mode, the compiler can and
10517 does assume that the assignment to @code{v2.all} could not
10518 affect the value of @code{v1.all}, since different types
10519 are involved.
10520
10521 This behavior is not a case of non-conformance with the standard, since
10522 the Ada RM specifies that an unchecked conversion where the resulting
10523 bit pattern is not a correct value of the target type can result in an
10524 abnormal value and attempting to reference an abnormal value makes the
10525 execution of a program erroneous. That's the case here since the result
10526 does not point to an object of type @code{int2}. This means that the
10527 effect is entirely unpredictable.
10528
10529 However, although that explanation may satisfy a language
10530 lawyer, in practice an applications programmer expects an
10531 unchecked conversion involving pointers to create true
10532 aliases and the behavior of printing 1 seems plain wrong.
10533 In this case, the strict aliasing optimization is unwelcome.
10534
10535 Indeed the compiler recognizes this possibility, and the
10536 unchecked conversion generates a warning:
10537
10538 @smallexample
10539 p2.adb:5:07: warning: possible aliasing problem with type "a2"
10540 p2.adb:5:07: warning: use -fno-strict-aliasing switch for references
10541 p2.adb:5:07: warning: or use "pragma No_Strict_Aliasing (a2);"
10542 @end smallexample
10543
10544 @noindent
10545 Unfortunately the problem is recognized when compiling the body of
10546 package @code{p2}, but the actual "bad" code is generated while
10547 compiling the body of @code{m} and this latter compilation does not see
10548 the suspicious @code{Unchecked_Conversion}.
10549
10550 As implied by the warning message, there are approaches you can use to
10551 avoid the unwanted strict aliasing optimization in a case like this.
10552
10553 One possibility is to simply avoid the use of @option{-O2}, but
10554 that is a bit drastic, since it throws away a number of useful
10555 optimizations that do not involve strict aliasing assumptions.
10556
10557 A less drastic approach is to compile the program using the
10558 option @option{-fno-strict-aliasing}. Actually it is only the
10559 unit containing the dereferencing of the suspicious pointer
10560 that needs to be compiled. So in this case, if we compile
10561 unit @code{m} with this switch, then we get the expected
10562 value of zero printed. Analyzing which units might need
10563 the switch can be painful, so a more reasonable approach
10564 is to compile the entire program with options @option{-O2}
10565 and @option{-fno-strict-aliasing}. If the performance is
10566 satisfactory with this combination of options, then the
10567 advantage is that the entire issue of possible "wrong"
10568 optimization due to strict aliasing is avoided.
10569
10570 To avoid the use of compiler switches, the configuration
10571 pragma @code{No_Strict_Aliasing} with no parameters may be
10572 used to specify that for all access types, the strict
10573 aliasing optimization should be suppressed.
10574
10575 However, these approaches are still overkill, in that they causes
10576 all manipulations of all access values to be deoptimized. A more
10577 refined approach is to concentrate attention on the specific
10578 access type identified as problematic.
10579
10580 First, if a careful analysis of uses of the pointer shows
10581 that there are no possible problematic references, then
10582 the warning can be suppressed by bracketing the
10583 instantiation of @code{Unchecked_Conversion} to turn
10584 the warning off:
10585
10586 @smallexample @c ada
10587 pragma Warnings (Off);
10588 function to_a2u is
10589 new Unchecked_Conversion (a1, a2);
10590 pragma Warnings (On);
10591 @end smallexample
10592
10593 @noindent
10594 Of course that approach is not appropriate for this particular
10595 example, since indeed there is a problematic reference. In this
10596 case we can take one of two other approaches.
10597
10598 The first possibility is to move the instantiation of unchecked
10599 conversion to the unit in which the type is declared. In
10600 this example, we would move the instantiation of
10601 @code{Unchecked_Conversion} from the body of package
10602 @code{p2} to the spec of package @code{p1}. Now the
10603 warning disappears. That's because any use of the
10604 access type knows there is a suspicious unchecked
10605 conversion, and the strict aliasing optimization
10606 is automatically suppressed for the type.
10607
10608 If it is not practical to move the unchecked conversion to the same unit
10609 in which the destination access type is declared (perhaps because the
10610 source type is not visible in that unit), you may use pragma
10611 @code{No_Strict_Aliasing} for the type. This pragma must occur in the
10612 same declarative sequence as the declaration of the access type:
10613
10614 @smallexample @c ada
10615 type a2 is access int2;
10616 pragma No_Strict_Aliasing (a2);
10617 @end smallexample
10618
10619 @noindent
10620 Here again, the compiler now knows that the strict aliasing optimization
10621 should be suppressed for any reference to type @code{a2} and the
10622 expected behavior is obtained.
10623
10624 Finally, note that although the compiler can generate warnings for
10625 simple cases of unchecked conversions, there are tricker and more
10626 indirect ways of creating type incorrect aliases which the compiler
10627 cannot detect. Examples are the use of address overlays and unchecked
10628 conversions involving composite types containing access types as
10629 components. In such cases, no warnings are generated, but there can
10630 still be aliasing problems. One safe coding practice is to forbid the
10631 use of address clauses for type overlaying, and to allow unchecked
10632 conversion only for primitive types. This is not really a significant
10633 restriction since any possible desired effect can be achieved by
10634 unchecked conversion of access values.
10635
10636 The aliasing analysis done in strict aliasing mode can certainly
10637 have significant benefits. We have seen cases of large scale
10638 application code where the time is increased by up to 5% by turning
10639 this optimization off. If you have code that includes significant
10640 usage of unchecked conversion, you might want to just stick with
10641 @option{-O1} and avoid the entire issue. If you get adequate
10642 performance at this level of optimization level, that's probably
10643 the safest approach. If tests show that you really need higher
10644 levels of optimization, then you can experiment with @option{-O2}
10645 and @option{-O2 -fno-strict-aliasing} to see how much effect this
10646 has on size and speed of the code. If you really need to use
10647 @option{-O2} with strict aliasing in effect, then you should
10648 review any uses of unchecked conversion of access types,
10649 particularly if you are getting the warnings described above.
10650
10651 @ifset vms
10652 @node Coverage Analysis
10653 @subsection Coverage Analysis
10654
10655 @noindent
10656 GNAT supports the HP Performance Coverage Analyzer (PCA), which allows
10657 the user to determine the distribution of execution time across a program,
10658 @pxref{Profiling} for details of usage.
10659 @end ifset
10660
10661
10662 @node Text_IO Suggestions
10663 @section @code{Text_IO} Suggestions
10664 @cindex @code{Text_IO} and performance
10665
10666 @noindent
10667 The @code{Ada.Text_IO} package has fairly high overheads due in part to
10668 the requirement of maintaining page and line counts. If performance
10669 is critical, a recommendation is to use @code{Stream_IO} instead of
10670 @code{Text_IO} for volume output, since this package has less overhead.
10671
10672 If @code{Text_IO} must be used, note that by default output to the standard
10673 output and standard error files is unbuffered (this provides better
10674 behavior when output statements are used for debugging, or if the
10675 progress of a program is observed by tracking the output, e.g. by
10676 using the Unix @command{tail -f} command to watch redirected output.
10677
10678 If you are generating large volumes of output with @code{Text_IO} and
10679 performance is an important factor, use a designated file instead
10680 of the standard output file, or change the standard output file to
10681 be buffered using @code{Interfaces.C_Streams.setvbuf}.
10682
10683
10684
10685 @node Reducing Size of Ada Executables with gnatelim
10686 @section Reducing Size of Ada Executables with @code{gnatelim}
10687 @findex gnatelim
10688
10689 @noindent
10690 This section describes @command{gnatelim}, a tool which detects unused
10691 subprograms and helps the compiler to create a smaller executable for your
10692 program.
10693
10694 @menu
10695 * About gnatelim::
10696 * Running gnatelim::
10697 * Correcting the List of Eliminate Pragmas::
10698 * Making Your Executables Smaller::
10699 * Summary of the gnatelim Usage Cycle::
10700 @end menu
10701
10702 @node About gnatelim
10703 @subsection About @code{gnatelim}
10704
10705 @noindent
10706 When a program shares a set of Ada
10707 packages with other programs, it may happen that this program uses
10708 only a fraction of the subprograms defined in these packages. The code
10709 created for these unused subprograms increases the size of the executable.
10710
10711 @code{gnatelim} tracks unused subprograms in an Ada program and
10712 outputs a list of GNAT-specific pragmas @code{Eliminate} marking all the
10713 subprograms that are declared but never called. By placing the list of
10714 @code{Eliminate} pragmas in the GNAT configuration file @file{gnat.adc} and
10715 recompiling your program, you may decrease the size of its executable,
10716 because the compiler will not generate the code for 'eliminated' subprograms.
10717 @xref{Pragma Eliminate,,, gnat_rm, GNAT Reference Manual}, for more
10718 information about this pragma.
10719
10720 @code{gnatelim} needs as its input data the name of the main subprogram
10721 and a bind file for a main subprogram.
10722
10723 To create a bind file for @code{gnatelim}, run @code{gnatbind} for
10724 the main subprogram. @code{gnatelim} can work with both Ada and C
10725 bind files; when both are present, it uses the Ada bind file.
10726 The following commands will build the program and create the bind file:
10727
10728 @smallexample
10729 $ gnatmake ^-c Main_Prog^/ACTIONS=COMPILE MAIN_PROG^
10730 $ gnatbind main_prog
10731 @end smallexample
10732
10733 Note that @code{gnatelim} needs neither object nor ALI files.
10734
10735 @node Running gnatelim
10736 @subsection Running @code{gnatelim}
10737
10738 @noindent
10739 @code{gnatelim} has the following command-line interface:
10740
10741 @smallexample
10742 $ gnatelim [@var{options}] name
10743 @end smallexample
10744
10745 @noindent
10746 @code{name} should be a name of a source file that contains the main subprogram
10747 of a program (partition).
10748
10749 @code{gnatelim} has the following switches:
10750
10751 @table @option
10752 @c !sort!
10753 @item ^-q^/QUIET^
10754 @cindex @option{^-q^/QUIET^} (@command{gnatelim})
10755 Quiet mode: by default @code{gnatelim} outputs to the standard error
10756 stream the number of program units left to be processed. This option turns
10757 this trace off.
10758
10759 @item ^-v^/VERBOSE^
10760 @cindex @option{^-v^/VERBOSE^} (@command{gnatelim})
10761 Verbose mode: @code{gnatelim} version information is printed as Ada
10762 comments to the standard output stream. Also, in addition to the number of
10763 program units left @code{gnatelim} will output the name of the current unit
10764 being processed.
10765
10766 @item ^-a^/ALL^
10767 @cindex @option{^-a^/ALL^} (@command{gnatelim})
10768 Also look for subprograms from the GNAT run time that can be eliminated. Note
10769 that when @file{gnat.adc} is produced using this switch, the entire program
10770 must be recompiled with switch @option{^-a^/ALL_FILES^} to @command{gnatmake}.
10771
10772 @item ^-I^/INCLUDE_DIRS=^@var{dir}
10773 @cindex @option{^-I^/INCLUDE_DIRS^} (@command{gnatelim})
10774 When looking for source files also look in directory @var{dir}. Specifying
10775 @option{^-I-^/INCLUDE_DIRS=-^} instructs @code{gnatelim} not to look for
10776 sources in the current directory.
10777
10778 @item ^-b^/BIND_FILE=^@var{bind_file}
10779 @cindex @option{^-b^/BIND_FILE^} (@command{gnatelim})
10780 Specifies @var{bind_file} as the bind file to process. If not set, the name
10781 of the bind file is computed from the full expanded Ada name
10782 of a main subprogram.
10783
10784 @item ^-C^/CONFIG_FILE=^@var{config_file}
10785 @cindex @option{^-C^/CONFIG_FILE^} (@command{gnatelim})
10786 Specifies a file @var{config_file} that contains configuration pragmas. The
10787 file must be specified with full path.
10788
10789 @item ^--GCC^/COMPILER^=@var{compiler_name}
10790 @cindex @option{^-GCC^/COMPILER^} (@command{gnatelim})
10791 Instructs @code{gnatelim} to use specific @command{gcc} compiler instead of one
10792 available on the path.
10793
10794 @item ^--GNATMAKE^/GNATMAKE^=@var{gnatmake_name}
10795 @cindex @option{^--GNATMAKE^/GNATMAKE^} (@command{gnatelim})
10796 Instructs @code{gnatelim} to use specific @command{gnatmake} instead of one
10797 available on the path.
10798 @end table
10799
10800 @noindent
10801 @code{gnatelim} sends its output to the standard output stream, and all the
10802 tracing and debug information is sent to the standard error stream.
10803 In order to produce a proper GNAT configuration file
10804 @file{gnat.adc}, redirection must be used:
10805
10806 @smallexample
10807 @ifset vms
10808 $ PIPE GNAT ELIM MAIN_PROG.ADB > GNAT.ADC
10809 @end ifset
10810 @ifclear vms
10811 $ gnatelim main_prog.adb > gnat.adc
10812 @end ifclear
10813 @end smallexample
10814
10815 @ifclear vms
10816 @noindent
10817 or
10818
10819 @smallexample
10820 $ gnatelim main_prog.adb >> gnat.adc
10821 @end smallexample
10822
10823 @noindent
10824 in order to append the @code{gnatelim} output to the existing contents of
10825 @file{gnat.adc}.
10826 @end ifclear
10827
10828 @node Correcting the List of Eliminate Pragmas
10829 @subsection Correcting the List of Eliminate Pragmas
10830
10831 @noindent
10832 In some rare cases @code{gnatelim} may try to eliminate
10833 subprograms that are actually called in the program. In this case, the
10834 compiler will generate an error message of the form:
10835
10836 @smallexample
10837 file.adb:106:07: cannot call eliminated subprogram "My_Prog"
10838 @end smallexample
10839
10840 @noindent
10841 You will need to manually remove the wrong @code{Eliminate} pragmas from
10842 the @file{gnat.adc} file. You should recompile your program
10843 from scratch after that, because you need a consistent @file{gnat.adc} file
10844 during the entire compilation.
10845
10846 @node Making Your Executables Smaller
10847 @subsection Making Your Executables Smaller
10848
10849 @noindent
10850 In order to get a smaller executable for your program you now have to
10851 recompile the program completely with the new @file{gnat.adc} file
10852 created by @code{gnatelim} in your current directory:
10853
10854 @smallexample
10855 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10856 @end smallexample
10857
10858 @noindent
10859 (Use the @option{^-f^/FORCE_COMPILE^} option for @command{gnatmake} to
10860 recompile everything
10861 with the set of pragmas @code{Eliminate} that you have obtained with
10862 @command{gnatelim}).
10863
10864 Be aware that the set of @code{Eliminate} pragmas is specific to each
10865 program. It is not recommended to merge sets of @code{Eliminate}
10866 pragmas created for different programs in one @file{gnat.adc} file.
10867
10868 @node Summary of the gnatelim Usage Cycle
10869 @subsection Summary of the gnatelim Usage Cycle
10870
10871 @noindent
10872 Here is a quick summary of the steps to be taken in order to reduce
10873 the size of your executables with @code{gnatelim}. You may use
10874 other GNAT options to control the optimization level,
10875 to produce the debugging information, to set search path, etc.
10876
10877 @enumerate
10878 @item
10879 Produce a bind file
10880
10881 @smallexample
10882 $ gnatmake ^-c main_prog^/ACTIONS=COMPILE MAIN_PROG^
10883 $ gnatbind main_prog
10884 @end smallexample
10885
10886 @item
10887 Generate a list of @code{Eliminate} pragmas
10888 @smallexample
10889 @ifset vms
10890 $ PIPE GNAT ELIM MAIN_PROG > GNAT.ADC
10891 @end ifset
10892 @ifclear vms
10893 $ gnatelim main_prog >@r{[}>@r{]} gnat.adc
10894 @end ifclear
10895 @end smallexample
10896
10897 @item
10898 Recompile the application
10899
10900 @smallexample
10901 $ gnatmake ^-f main_prog^/FORCE_COMPILE MAIN_PROG^
10902 @end smallexample
10903
10904 @end enumerate
10905
10906 @node Reducing Size of Executables with unused subprogram/data elimination
10907 @section Reducing Size of Executables with Unused Subprogram/Data Elimination
10908 @findex unused subprogram/data elimination
10909
10910 @noindent
10911 This section describes how you can eliminate unused subprograms and data from
10912 your executable just by setting options at compilation time.
10913
10914 @menu
10915 * About unused subprogram/data elimination::
10916 * Compilation options::
10917 * Example of unused subprogram/data elimination::
10918 @end menu
10919
10920 @node About unused subprogram/data elimination
10921 @subsection About unused subprogram/data elimination
10922
10923 @noindent
10924 By default, an executable contains all code and data of its composing objects
10925 (directly linked or coming from statically linked libraries), even data or code
10926 never used by this executable.
10927
10928 This feature will allow you to eliminate such unused code from your
10929 executable, making it smaller (in disk and in memory).
10930
10931 This functionality is available on all Linux platforms except for the IA-64
10932 architecture and on all cross platforms using the ELF binary file format.
10933 In both cases GNU binutils version 2.16 or later are required to enable it.
10934
10935 @node Compilation options
10936 @subsection Compilation options
10937
10938 @noindent
10939 The operation of eliminating the unused code and data from the final executable
10940 is directly performed by the linker.
10941
10942 In order to do this, it has to work with objects compiled with the
10943 following options:
10944 @option{-ffunction-sections} @option{-fdata-sections}.
10945 @cindex @option{-ffunction-sections} (@command{gcc})
10946 @cindex @option{-fdata-sections} (@command{gcc})
10947 These options are usable with C and Ada files.
10948 They will place respectively each
10949 function or data in a separate section in the resulting object file.
10950
10951 Once the objects and static libraries are created with these options, the
10952 linker can perform the dead code elimination. You can do this by setting
10953 the @option{-Wl,--gc-sections} option to gcc command or in the
10954 @option{-largs} section of @command{gnatmake}. This will perform a
10955 garbage collection of code and data never referenced.
10956
10957 If the linker performs a partial link (@option{-r} ld linker option), then you
10958 will need to provide one or several entry point using the
10959 @option{-e} / @option{--entry} ld option.
10960
10961 Note that objects compiled without the @option{-ffunction-sections} and
10962 @option{-fdata-sections} options can still be linked with the executable.
10963 However, no dead code elimination will be performed on those objects (they will
10964 be linked as is).
10965
10966 The GNAT static library is now compiled with -ffunction-sections and
10967 -fdata-sections on some platforms. This allows you to eliminate the unused code
10968 and data of the GNAT library from your executable.
10969
10970 @node Example of unused subprogram/data elimination
10971 @subsection Example of unused subprogram/data elimination
10972
10973 @noindent
10974 Here is a simple example:
10975
10976 @smallexample @c ada
10977 with Aux;
10978
10979 procedure Test is
10980 begin
10981 Aux.Used (10);
10982 end Test;
10983
10984 package Aux is
10985 Used_Data : Integer;
10986 Unused_Data : Integer;
10987
10988 procedure Used (Data : Integer);
10989 procedure Unused (Data : Integer);
10990 end Aux;
10991
10992 package body Aux is
10993 procedure Used (Data : Integer) is
10994 begin
10995 Used_Data := Data;
10996 end Used;
10997
10998 procedure Unused (Data : Integer) is
10999 begin
11000 Unused_Data := Data;
11001 end Unused;
11002 end Aux;
11003 @end smallexample
11004
11005 @noindent
11006 @code{Unused} and @code{Unused_Data} are never referenced in this code
11007 excerpt, and hence they may be safely removed from the final executable.
11008
11009 @smallexample
11010 $ gnatmake test
11011
11012 $ nm test | grep used
11013 020015f0 T aux__unused
11014 02005d88 B aux__unused_data
11015 020015cc T aux__used
11016 02005d84 B aux__used_data
11017
11018 $ gnatmake test -cargs -fdata-sections -ffunction-sections \
11019 -largs -Wl,--gc-sections
11020
11021 $ nm test | grep used
11022 02005350 T aux__used
11023 0201ffe0 B aux__used_data
11024 @end smallexample
11025
11026 @noindent
11027 It can be observed that the procedure @code{Unused} and the object
11028 @code{Unused_Data} are removed by the linker when using the
11029 appropriate options.
11030
11031 @c ********************************
11032 @node Renaming Files Using gnatchop
11033 @chapter Renaming Files Using @code{gnatchop}
11034 @findex gnatchop
11035
11036 @noindent
11037 This chapter discusses how to handle files with multiple units by using
11038 the @code{gnatchop} utility. This utility is also useful in renaming
11039 files to meet the standard GNAT default file naming conventions.
11040
11041 @menu
11042 * Handling Files with Multiple Units::
11043 * Operating gnatchop in Compilation Mode::
11044 * Command Line for gnatchop::
11045 * Switches for gnatchop::
11046 * Examples of gnatchop Usage::
11047 @end menu
11048
11049 @node Handling Files with Multiple Units
11050 @section Handling Files with Multiple Units
11051
11052 @noindent
11053 The basic compilation model of GNAT requires that a file submitted to the
11054 compiler have only one unit and there be a strict correspondence
11055 between the file name and the unit name.
11056
11057 The @code{gnatchop} utility allows both of these rules to be relaxed,
11058 allowing GNAT to process files which contain multiple compilation units
11059 and files with arbitrary file names. @code{gnatchop}
11060 reads the specified file and generates one or more output files,
11061 containing one unit per file. The unit and the file name correspond,
11062 as required by GNAT.
11063
11064 If you want to permanently restructure a set of ``foreign'' files so that
11065 they match the GNAT rules, and do the remaining development using the
11066 GNAT structure, you can simply use @command{gnatchop} once, generate the
11067 new set of files and work with them from that point on.
11068
11069 Alternatively, if you want to keep your files in the ``foreign'' format,
11070 perhaps to maintain compatibility with some other Ada compilation
11071 system, you can set up a procedure where you use @command{gnatchop} each
11072 time you compile, regarding the source files that it writes as temporary
11073 files that you throw away.
11074
11075 Note that if your file containing multiple units starts with a byte order
11076 mark (BOM) specifying UTF-8 encoding, then the files generated by gnatchop
11077 will each start with a copy of this BOM, meaning that they can be compiled
11078 automatically in UTF-8 mode without needing to specify an explicit encoding.
11079
11080 @node Operating gnatchop in Compilation Mode
11081 @section Operating gnatchop in Compilation Mode
11082
11083 @noindent
11084 The basic function of @code{gnatchop} is to take a file with multiple units
11085 and split it into separate files. The boundary between files is reasonably
11086 clear, except for the issue of comments and pragmas. In default mode, the
11087 rule is that any pragmas between units belong to the previous unit, except
11088 that configuration pragmas always belong to the following unit. Any comments
11089 belong to the following unit. These rules
11090 almost always result in the right choice of
11091 the split point without needing to mark it explicitly and most users will
11092 find this default to be what they want. In this default mode it is incorrect to
11093 submit a file containing only configuration pragmas, or one that ends in
11094 configuration pragmas, to @code{gnatchop}.
11095
11096 However, using a special option to activate ``compilation mode'',
11097 @code{gnatchop}
11098 can perform another function, which is to provide exactly the semantics
11099 required by the RM for handling of configuration pragmas in a compilation.
11100 In the absence of configuration pragmas (at the main file level), this
11101 option has no effect, but it causes such configuration pragmas to be handled
11102 in a quite different manner.
11103
11104 First, in compilation mode, if @code{gnatchop} is given a file that consists of
11105 only configuration pragmas, then this file is appended to the
11106 @file{gnat.adc} file in the current directory. This behavior provides
11107 the required behavior described in the RM for the actions to be taken
11108 on submitting such a file to the compiler, namely that these pragmas
11109 should apply to all subsequent compilations in the same compilation
11110 environment. Using GNAT, the current directory, possibly containing a
11111 @file{gnat.adc} file is the representation
11112 of a compilation environment. For more information on the
11113 @file{gnat.adc} file, see @ref{Handling of Configuration Pragmas}.
11114
11115 Second, in compilation mode, if @code{gnatchop}
11116 is given a file that starts with
11117 configuration pragmas, and contains one or more units, then these
11118 configuration pragmas are prepended to each of the chopped files. This
11119 behavior provides the required behavior described in the RM for the
11120 actions to be taken on compiling such a file, namely that the pragmas
11121 apply to all units in the compilation, but not to subsequently compiled
11122 units.
11123
11124 Finally, if configuration pragmas appear between units, they are appended
11125 to the previous unit. This results in the previous unit being illegal,
11126 since the compiler does not accept configuration pragmas that follow
11127 a unit. This provides the required RM behavior that forbids configuration
11128 pragmas other than those preceding the first compilation unit of a
11129 compilation.
11130
11131 For most purposes, @code{gnatchop} will be used in default mode. The
11132 compilation mode described above is used only if you need exactly
11133 accurate behavior with respect to compilations, and you have files
11134 that contain multiple units and configuration pragmas. In this
11135 circumstance the use of @code{gnatchop} with the compilation mode
11136 switch provides the required behavior, and is for example the mode
11137 in which GNAT processes the ACVC tests.
11138
11139 @node Command Line for gnatchop
11140 @section Command Line for @code{gnatchop}
11141
11142 @noindent
11143 The @code{gnatchop} command has the form:
11144
11145 @smallexample
11146 @c $ gnatchop switches @var{file name} @r{[}@var{file name} @dots{}@r{]}
11147 @c @ovar{directory}
11148 @c Expanding @ovar macro inline (explanation in macro def comments)
11149 $ gnatchop switches @var{file name} @r{[}@var{file name} @dots{}@r{]}
11150 @r{[}@var{directory}@r{]}
11151 @end smallexample
11152
11153 @noindent
11154 The only required argument is the file name of the file to be chopped.
11155 There are no restrictions on the form of this file name. The file itself
11156 contains one or more Ada units, in normal GNAT format, concatenated
11157 together. As shown, more than one file may be presented to be chopped.
11158
11159 When run in default mode, @code{gnatchop} generates one output file in
11160 the current directory for each unit in each of the files.
11161
11162 @var{directory}, if specified, gives the name of the directory to which
11163 the output files will be written. If it is not specified, all files are
11164 written to the current directory.
11165
11166 For example, given a
11167 file called @file{hellofiles} containing
11168
11169 @smallexample @c ada
11170 @group
11171 @cartouche
11172 procedure hello;
11173
11174 with Text_IO; use Text_IO;
11175 procedure hello is
11176 begin
11177 Put_Line ("Hello");
11178 end hello;
11179 @end cartouche
11180 @end group
11181 @end smallexample
11182
11183 @noindent
11184 the command
11185
11186 @smallexample
11187 $ gnatchop ^hellofiles^HELLOFILES.^
11188 @end smallexample
11189
11190 @noindent
11191 generates two files in the current directory, one called
11192 @file{hello.ads} containing the single line that is the procedure spec,
11193 and the other called @file{hello.adb} containing the remaining text. The
11194 original file is not affected. The generated files can be compiled in
11195 the normal manner.
11196
11197 @noindent
11198 When gnatchop is invoked on a file that is empty or that contains only empty
11199 lines and/or comments, gnatchop will not fail, but will not produce any
11200 new sources.
11201
11202 For example, given a
11203 file called @file{toto.txt} containing
11204
11205 @smallexample @c ada
11206 @group
11207 @cartouche
11208 -- Just a comment
11209 @end cartouche
11210 @end group
11211 @end smallexample
11212
11213 @noindent
11214 the command
11215
11216 @smallexample
11217 $ gnatchop ^toto.txt^TOT.TXT^
11218 @end smallexample
11219
11220 @noindent
11221 will not produce any new file and will result in the following warnings:
11222
11223 @smallexample
11224 toto.txt:1:01: warning: empty file, contains no compilation units
11225 no compilation units found
11226 no source files written
11227 @end smallexample
11228
11229 @node Switches for gnatchop
11230 @section Switches for @code{gnatchop}
11231
11232 @noindent
11233 @command{gnatchop} recognizes the following switches:
11234
11235 @table @option
11236 @c !sort!
11237
11238 @item --version
11239 @cindex @option{--version} @command{gnatchop}
11240 Display Copyright and version, then exit disregarding all other options.
11241
11242 @item --help
11243 @cindex @option{--help} @command{gnatchop}
11244 If @option{--version} was not used, display usage, then exit disregarding
11245 all other options.
11246
11247 @item ^-c^/COMPILATION^
11248 @cindex @option{^-c^/COMPILATION^} (@code{gnatchop})
11249 Causes @code{gnatchop} to operate in compilation mode, in which
11250 configuration pragmas are handled according to strict RM rules. See
11251 previous section for a full description of this mode.
11252
11253 @ifclear vms
11254 @item -gnat@var{xxx}
11255 This passes the given @option{-gnat@var{xxx}} switch to @code{gnat} which is
11256 used to parse the given file. Not all @var{xxx} options make sense,
11257 but for example, the use of @option{-gnati2} allows @code{gnatchop} to
11258 process a source file that uses Latin-2 coding for identifiers.
11259 @end ifclear
11260
11261 @item ^-h^/HELP^
11262 Causes @code{gnatchop} to generate a brief help summary to the standard
11263 output file showing usage information.
11264
11265 @item ^-k@var{mm}^/FILE_NAME_MAX_LENGTH=@var{mm}^
11266 @cindex @option{^-k^/FILE_NAME_MAX_LENGTH^} (@code{gnatchop})
11267 Limit generated file names to the specified number @code{mm}
11268 of characters.
11269 This is useful if the
11270 resulting set of files is required to be interoperable with systems
11271 which limit the length of file names.
11272 @ifset vms
11273 If no value is given, or
11274 if no @code{/FILE_NAME_MAX_LENGTH} qualifier is given,
11275 a default of 39, suitable for OpenVMS Alpha
11276 Systems, is assumed
11277 @end ifset
11278 @ifclear vms
11279 No space is allowed between the @option{-k} and the numeric value. The numeric
11280 value may be omitted in which case a default of @option{-k8},
11281 suitable for use
11282 with DOS-like file systems, is used. If no @option{-k} switch
11283 is present then
11284 there is no limit on the length of file names.
11285 @end ifclear
11286
11287 @item ^-p^/PRESERVE^
11288 @cindex @option{^-p^/PRESERVE^} (@code{gnatchop})
11289 Causes the file ^modification^creation^ time stamp of the input file to be
11290 preserved and used for the time stamp of the output file(s). This may be
11291 useful for preserving coherency of time stamps in an environment where
11292 @code{gnatchop} is used as part of a standard build process.
11293
11294 @item ^-q^/QUIET^
11295 @cindex @option{^-q^/QUIET^} (@code{gnatchop})
11296 Causes output of informational messages indicating the set of generated
11297 files to be suppressed. Warnings and error messages are unaffected.
11298
11299 @item ^-r^/REFERENCE^
11300 @cindex @option{^-r^/REFERENCE^} (@code{gnatchop})
11301 @findex Source_Reference
11302 Generate @code{Source_Reference} pragmas. Use this switch if the output
11303 files are regarded as temporary and development is to be done in terms
11304 of the original unchopped file. This switch causes
11305 @code{Source_Reference} pragmas to be inserted into each of the
11306 generated files to refers back to the original file name and line number.
11307 The result is that all error messages refer back to the original
11308 unchopped file.
11309 In addition, the debugging information placed into the object file (when
11310 the @option{^-g^/DEBUG^} switch of @command{gcc} or @command{gnatmake} is
11311 specified)
11312 also refers back to this original file so that tools like profilers and
11313 debuggers will give information in terms of the original unchopped file.
11314
11315 If the original file to be chopped itself contains
11316 a @code{Source_Reference}
11317 pragma referencing a third file, then gnatchop respects
11318 this pragma, and the generated @code{Source_Reference} pragmas
11319 in the chopped file refer to the original file, with appropriate
11320 line numbers. This is particularly useful when @code{gnatchop}
11321 is used in conjunction with @code{gnatprep} to compile files that
11322 contain preprocessing statements and multiple units.
11323
11324 @item ^-v^/VERBOSE^
11325 @cindex @option{^-v^/VERBOSE^} (@code{gnatchop})
11326 Causes @code{gnatchop} to operate in verbose mode. The version
11327 number and copyright notice are output, as well as exact copies of
11328 the gnat1 commands spawned to obtain the chop control information.
11329
11330 @item ^-w^/OVERWRITE^
11331 @cindex @option{^-w^/OVERWRITE^} (@code{gnatchop})
11332 Overwrite existing file names. Normally @code{gnatchop} regards it as a
11333 fatal error if there is already a file with the same name as a
11334 file it would otherwise output, in other words if the files to be
11335 chopped contain duplicated units. This switch bypasses this
11336 check, and causes all but the last instance of such duplicated
11337 units to be skipped.
11338
11339 @ifclear vms
11340 @item --GCC=@var{xxxx}
11341 @cindex @option{--GCC=} (@code{gnatchop})
11342 Specify the path of the GNAT parser to be used. When this switch is used,
11343 no attempt is made to add the prefix to the GNAT parser executable.
11344 @end ifclear
11345 @end table
11346
11347 @node Examples of gnatchop Usage
11348 @section Examples of @code{gnatchop} Usage
11349
11350 @table @code
11351 @ifset vms
11352 @item gnatchop /OVERWRITE HELLO_S.ADA [PRERELEASE.FILES]
11353 @end ifset
11354 @ifclear vms
11355 @item gnatchop -w hello_s.ada prerelease/files
11356 @end ifclear
11357
11358 Chops the source file @file{hello_s.ada}. The output files will be
11359 placed in the directory @file{^prerelease/files^[PRERELEASE.FILES]^},
11360 overwriting any
11361 files with matching names in that directory (no files in the current
11362 directory are modified).
11363
11364 @item gnatchop ^archive^ARCHIVE.^
11365 Chops the source file @file{^archive^ARCHIVE.^}
11366 into the current directory. One
11367 useful application of @code{gnatchop} is in sending sets of sources
11368 around, for example in email messages. The required sources are simply
11369 concatenated (for example, using a ^Unix @code{cat}^VMS @code{APPEND/NEW}^
11370 command), and then
11371 @command{gnatchop} is used at the other end to reconstitute the original
11372 file names.
11373
11374 @item gnatchop file1 file2 file3 direc
11375 Chops all units in files @file{file1}, @file{file2}, @file{file3}, placing
11376 the resulting files in the directory @file{direc}. Note that if any units
11377 occur more than once anywhere within this set of files, an error message
11378 is generated, and no files are written. To override this check, use the
11379 @option{^-w^/OVERWRITE^} switch,
11380 in which case the last occurrence in the last file will
11381 be the one that is output, and earlier duplicate occurrences for a given
11382 unit will be skipped.
11383 @end table
11384
11385 @node Configuration Pragmas
11386 @chapter Configuration Pragmas
11387 @cindex Configuration pragmas
11388 @cindex Pragmas, configuration
11389
11390 @noindent
11391 Configuration pragmas include those pragmas described as
11392 such in the Ada Reference Manual, as well as
11393 implementation-dependent pragmas that are configuration pragmas.
11394 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
11395 for details on these additional GNAT-specific configuration pragmas.
11396 Most notably, the pragma @code{Source_File_Name}, which allows
11397 specifying non-default names for source files, is a configuration
11398 pragma. The following is a complete list of configuration pragmas
11399 recognized by GNAT:
11400
11401 @smallexample
11402 Ada_83
11403 Ada_95
11404 Ada_05
11405 Ada_2005
11406 Assertion_Policy
11407 Assume_No_Invalid_Values
11408 C_Pass_By_Copy
11409 Check_Name
11410 Check_Policy
11411 Compile_Time_Error
11412 Compile_Time_Warning
11413 Compiler_Unit
11414 Component_Alignment
11415 Convention_Identifier
11416 Debug_Policy
11417 Detect_Blocking
11418 Discard_Names
11419 Elaboration_Checks
11420 Eliminate
11421 Extend_System
11422 Extensions_Allowed
11423 External_Name_Casing
11424 Fast_Math
11425 Favor_Top_Level
11426 Float_Representation
11427 Implicit_Packing
11428 Initialize_Scalars
11429 Interrupt_State
11430 License
11431 Locking_Policy
11432 Long_Float
11433 No_Run_Time
11434 No_Strict_Aliasing
11435 Normalize_Scalars
11436 Optimize_Alignment
11437 Persistent_BSS
11438 Polling
11439 Priority_Specific_Dispatching
11440 Profile
11441 Profile_Warnings
11442 Propagate_Exceptions
11443 Queuing_Policy
11444 Ravenscar
11445 Restricted_Run_Time
11446 Restrictions
11447 Restrictions_Warnings
11448 Reviewable
11449 Source_File_Name
11450 Source_File_Name_Project
11451 Style_Checks
11452 Suppress
11453 Suppress_Exception_Locations
11454 Task_Dispatching_Policy
11455 Universal_Data
11456 Unsuppress
11457 Use_VADS_Size
11458 Validity_Checks
11459 Warnings
11460 Wide_Character_Encoding
11461
11462 @end smallexample
11463
11464 @menu
11465 * Handling of Configuration Pragmas::
11466 * The Configuration Pragmas Files::
11467 @end menu
11468
11469 @node Handling of Configuration Pragmas
11470 @section Handling of Configuration Pragmas
11471
11472 Configuration pragmas may either appear at the start of a compilation
11473 unit, in which case they apply only to that unit, or they may apply to
11474 all compilations performed in a given compilation environment.
11475
11476 GNAT also provides the @code{gnatchop} utility to provide an automatic
11477 way to handle configuration pragmas following the semantics for
11478 compilations (that is, files with multiple units), described in the RM.
11479 See @ref{Operating gnatchop in Compilation Mode} for details.
11480 However, for most purposes, it will be more convenient to edit the
11481 @file{gnat.adc} file that contains configuration pragmas directly,
11482 as described in the following section.
11483
11484 @node The Configuration Pragmas Files
11485 @section The Configuration Pragmas Files
11486 @cindex @file{gnat.adc}
11487
11488 @noindent
11489 In GNAT a compilation environment is defined by the current
11490 directory at the time that a compile command is given. This current
11491 directory is searched for a file whose name is @file{gnat.adc}. If
11492 this file is present, it is expected to contain one or more
11493 configuration pragmas that will be applied to the current compilation.
11494 However, if the switch @option{-gnatA} is used, @file{gnat.adc} is not
11495 considered.
11496
11497 Configuration pragmas may be entered into the @file{gnat.adc} file
11498 either by running @code{gnatchop} on a source file that consists only of
11499 configuration pragmas, or more conveniently by
11500 direct editing of the @file{gnat.adc} file, which is a standard format
11501 source file.
11502
11503 In addition to @file{gnat.adc}, additional files containing configuration
11504 pragmas may be applied to the current compilation using the switch
11505 @option{-gnatec}@var{path}. @var{path} must designate an existing file that
11506 contains only configuration pragmas. These configuration pragmas are
11507 in addition to those found in @file{gnat.adc} (provided @file{gnat.adc}
11508 is present and switch @option{-gnatA} is not used).
11509
11510 It is allowed to specify several switches @option{-gnatec}, all of which
11511 will be taken into account.
11512
11513 If you are using project file, a separate mechanism is provided using
11514 project attributes, see @ref{Specifying Configuration Pragmas} for more
11515 details.
11516
11517 @ifset vms
11518 Of special interest to GNAT OpenVMS Alpha is the following
11519 configuration pragma:
11520
11521 @smallexample @c ada
11522 @cartouche
11523 pragma Extend_System (Aux_DEC);
11524 @end cartouche
11525 @end smallexample
11526
11527 @noindent
11528 In the presence of this pragma, GNAT adds to the definition of the
11529 predefined package SYSTEM all the additional types and subprograms that are
11530 defined in HP Ada. See @ref{Compatibility with HP Ada} for details.
11531 @end ifset
11532
11533 @node Handling Arbitrary File Naming Conventions Using gnatname
11534 @chapter Handling Arbitrary File Naming Conventions Using @code{gnatname}
11535 @cindex Arbitrary File Naming Conventions
11536
11537 @menu
11538 * Arbitrary File Naming Conventions::
11539 * Running gnatname::
11540 * Switches for gnatname::
11541 * Examples of gnatname Usage::
11542 @end menu
11543
11544 @node Arbitrary File Naming Conventions
11545 @section Arbitrary File Naming Conventions
11546
11547 @noindent
11548 The GNAT compiler must be able to know the source file name of a compilation
11549 unit. When using the standard GNAT default file naming conventions
11550 (@code{.ads} for specs, @code{.adb} for bodies), the GNAT compiler
11551 does not need additional information.
11552
11553 @noindent
11554 When the source file names do not follow the standard GNAT default file naming
11555 conventions, the GNAT compiler must be given additional information through
11556 a configuration pragmas file (@pxref{Configuration Pragmas})
11557 or a project file.
11558 When the non-standard file naming conventions are well-defined,
11559 a small number of pragmas @code{Source_File_Name} specifying a naming pattern
11560 (@pxref{Alternative File Naming Schemes}) may be sufficient. However,
11561 if the file naming conventions are irregular or arbitrary, a number
11562 of pragma @code{Source_File_Name} for individual compilation units
11563 must be defined.
11564 To help maintain the correspondence between compilation unit names and
11565 source file names within the compiler,
11566 GNAT provides a tool @code{gnatname} to generate the required pragmas for a
11567 set of files.
11568
11569 @node Running gnatname
11570 @section Running @code{gnatname}
11571
11572 @noindent
11573 The usual form of the @code{gnatname} command is
11574
11575 @smallexample
11576 @c $ gnatname @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}
11577 @c @r{[}--and @ovar{switches} @var{naming_pattern} @ovar{naming_patterns}@r{]}
11578 @c Expanding @ovar macro inline (explanation in macro def comments)
11579 $ gnatname @r{[}@var{switches}@r{]} @var{naming_pattern} @r{[}@var{naming_patterns}@r{]}
11580 @r{[}--and @r{[}@var{switches}@r{]} @var{naming_pattern} @r{[}@var{naming_patterns}@r{]}@r{]}
11581 @end smallexample
11582
11583 @noindent
11584 All of the arguments are optional. If invoked without any argument,
11585 @code{gnatname} will display its usage.
11586
11587 @noindent
11588 When used with at least one naming pattern, @code{gnatname} will attempt to
11589 find all the compilation units in files that follow at least one of the
11590 naming patterns. To find these compilation units,
11591 @code{gnatname} will use the GNAT compiler in syntax-check-only mode on all
11592 regular files.
11593
11594 @noindent
11595 One or several Naming Patterns may be given as arguments to @code{gnatname}.
11596 Each Naming Pattern is enclosed between double quotes.
11597 A Naming Pattern is a regular expression similar to the wildcard patterns
11598 used in file names by the Unix shells or the DOS prompt.
11599
11600 @noindent
11601 @code{gnatname} may be called with several sections of directories/patterns.
11602 Sections are separated by switch @code{--and}. In each section, there must be
11603 at least one pattern. If no directory is specified in a section, the current
11604 directory (or the project directory is @code{-P} is used) is implied.
11605 The options other that the directory switches and the patterns apply globally
11606 even if they are in different sections.
11607
11608 @noindent
11609 Examples of Naming Patterns are
11610
11611 @smallexample
11612 "*.[12].ada"
11613 "*.ad[sb]*"
11614 "body_*" "spec_*"
11615 @end smallexample
11616
11617 @noindent
11618 For a more complete description of the syntax of Naming Patterns,
11619 see the second kind of regular expressions described in @file{g-regexp.ads}
11620 (the ``Glob'' regular expressions).
11621
11622 @noindent
11623 When invoked with no switch @code{-P}, @code{gnatname} will create a
11624 configuration pragmas file @file{gnat.adc} in the current working directory,
11625 with pragmas @code{Source_File_Name} for each file that contains a valid Ada
11626 unit.
11627
11628 @node Switches for gnatname
11629 @section Switches for @code{gnatname}
11630
11631 @noindent
11632 Switches for @code{gnatname} must precede any specified Naming Pattern.
11633
11634 @noindent
11635 You may specify any of the following switches to @code{gnatname}:
11636
11637 @table @option
11638 @c !sort!
11639
11640 @item --version
11641 @cindex @option{--version} @command{gnatname}
11642 Display Copyright and version, then exit disregarding all other options.
11643
11644 @item --help
11645 @cindex @option{--help} @command{gnatname}
11646 If @option{--version} was not used, display usage, then exit disregarding
11647 all other options.
11648
11649 @item --and
11650 Start another section of directories/patterns.
11651
11652 @item ^-c^/CONFIG_FILE=^@file{file}
11653 @cindex @option{^-c^/CONFIG_FILE^} (@code{gnatname})
11654 Create a configuration pragmas file @file{file} (instead of the default
11655 @file{gnat.adc}).
11656 @ifclear vms
11657 There may be zero, one or more space between @option{-c} and
11658 @file{file}.
11659 @end ifclear
11660 @file{file} may include directory information. @file{file} must be
11661 writable. There may be only one switch @option{^-c^/CONFIG_FILE^}.
11662 When a switch @option{^-c^/CONFIG_FILE^} is
11663 specified, no switch @option{^-P^/PROJECT_FILE^} may be specified (see below).
11664
11665 @item ^-d^/SOURCE_DIRS=^@file{dir}
11666 @cindex @option{^-d^/SOURCE_DIRS^} (@code{gnatname})
11667 Look for source files in directory @file{dir}. There may be zero, one or more
11668 spaces between @option{^-d^/SOURCE_DIRS=^} and @file{dir}.
11669 When a switch @option{^-d^/SOURCE_DIRS^}
11670 is specified, the current working directory will not be searched for source
11671 files, unless it is explicitly specified with a @option{^-d^/SOURCE_DIRS^}
11672 or @option{^-D^/DIR_FILES^} switch.
11673 Several switches @option{^-d^/SOURCE_DIRS^} may be specified.
11674 If @file{dir} is a relative path, it is relative to the directory of
11675 the configuration pragmas file specified with switch
11676 @option{^-c^/CONFIG_FILE^},
11677 or to the directory of the project file specified with switch
11678 @option{^-P^/PROJECT_FILE^} or,
11679 if neither switch @option{^-c^/CONFIG_FILE^}
11680 nor switch @option{^-P^/PROJECT_FILE^} are specified, it is relative to the
11681 current working directory. The directory
11682 specified with switch @option{^-d^/SOURCE_DIRS^} must exist and be readable.
11683
11684 @item ^-D^/DIRS_FILE=^@file{file}
11685 @cindex @option{^-D^/DIRS_FILE^} (@code{gnatname})
11686 Look for source files in all directories listed in text file @file{file}.
11687 There may be zero, one or more spaces between @option{^-D^/DIRS_FILE=^}
11688 and @file{file}.
11689 @file{file} must be an existing, readable text file.
11690 Each nonempty line in @file{file} must be a directory.
11691 Specifying switch @option{^-D^/DIRS_FILE^} is equivalent to specifying as many
11692 switches @option{^-d^/SOURCE_DIRS^} as there are nonempty lines in
11693 @file{file}.
11694
11695 @item ^-f^/FOREIGN_PATTERN=^@file{pattern}
11696 @cindex @option{^-f^/FOREIGN_PATTERN^} (@code{gnatname})
11697 Foreign patterns. Using this switch, it is possible to add sources of languages
11698 other than Ada to the list of sources of a project file.
11699 It is only useful if a ^-P^/PROJECT_FILE^ switch is used.
11700 For example,
11701 @smallexample
11702 gnatname ^-Pprj -f"*.c"^/PROJECT_FILE=PRJ /FOREIGN_PATTERN=*.C^ "*.ada"
11703 @end smallexample
11704 @noindent
11705 will look for Ada units in all files with the @file{.ada} extension,
11706 and will add to the list of file for project @file{prj.gpr} the C files
11707 with extension @file{.^c^C^}.
11708
11709 @item ^-h^/HELP^
11710 @cindex @option{^-h^/HELP^} (@code{gnatname})
11711 Output usage (help) information. The output is written to @file{stdout}.
11712
11713 @item ^-P^/PROJECT_FILE=^@file{proj}
11714 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatname})
11715 Create or update project file @file{proj}. There may be zero, one or more space
11716 between @option{-P} and @file{proj}. @file{proj} may include directory
11717 information. @file{proj} must be writable.
11718 There may be only one switch @option{^-P^/PROJECT_FILE^}.
11719 When a switch @option{^-P^/PROJECT_FILE^} is specified,
11720 no switch @option{^-c^/CONFIG_FILE^} may be specified.
11721
11722 @item ^-v^/VERBOSE^
11723 @cindex @option{^-v^/VERBOSE^} (@code{gnatname})
11724 Verbose mode. Output detailed explanation of behavior to @file{stdout}.
11725 This includes name of the file written, the name of the directories to search
11726 and, for each file in those directories whose name matches at least one of
11727 the Naming Patterns, an indication of whether the file contains a unit,
11728 and if so the name of the unit.
11729
11730 @item ^-v -v^/VERBOSE /VERBOSE^
11731 @cindex @option{^-v -v^/VERBOSE /VERBOSE^} (@code{gnatname})
11732 Very Verbose mode. In addition to the output produced in verbose mode,
11733 for each file in the searched directories whose name matches none of
11734 the Naming Patterns, an indication is given that there is no match.
11735
11736 @item ^-x^/EXCLUDED_PATTERN=^@file{pattern}
11737 @cindex @option{^-x^/EXCLUDED_PATTERN^} (@code{gnatname})
11738 Excluded patterns. Using this switch, it is possible to exclude some files
11739 that would match the name patterns. For example,
11740 @smallexample
11741 gnatname ^-x "*_nt.ada"^/EXCLUDED_PATTERN=*_nt.ada^ "*.ada"
11742 @end smallexample
11743 @noindent
11744 will look for Ada units in all files with the @file{.ada} extension,
11745 except those whose names end with @file{_nt.ada}.
11746
11747 @end table
11748
11749 @node Examples of gnatname Usage
11750 @section Examples of @code{gnatname} Usage
11751
11752 @ifset vms
11753 @smallexample
11754 $ gnatname /CONFIG_FILE=[HOME.ME]NAMES.ADC /SOURCE_DIRS=SOURCES "[a-z]*.ada*"
11755 @end smallexample
11756 @end ifset
11757
11758 @ifclear vms
11759 @smallexample
11760 $ gnatname -c /home/me/names.adc -d sources "[a-z]*.ada*"
11761 @end smallexample
11762 @end ifclear
11763
11764 @noindent
11765 In this example, the directory @file{^/home/me^[HOME.ME]^} must already exist
11766 and be writable. In addition, the directory
11767 @file{^/home/me/sources^[HOME.ME.SOURCES]^} (specified by
11768 @option{^-d sources^/SOURCE_DIRS=SOURCES^}) must exist and be readable.
11769
11770 @ifclear vms
11771 Note the optional spaces after @option{-c} and @option{-d}.
11772 @end ifclear
11773
11774 @smallexample
11775 @ifclear vms
11776 $ gnatname -P/home/me/proj -x "*_nt_body.ada"
11777 -dsources -dsources/plus -Dcommon_dirs.txt "body_*" "spec_*"
11778 @end ifclear
11779 @ifset vms
11780 $ gnatname /PROJECT_FILE=[HOME.ME]PROJ
11781 /EXCLUDED_PATTERN=*_nt_body.ada
11782 /SOURCE_DIRS=(SOURCES,[SOURCES.PLUS])
11783 /DIRS_FILE=COMMON_DIRS.TXT "body_*" "spec_*"
11784 @end ifset
11785 @end smallexample
11786
11787 Note that several switches @option{^-d^/SOURCE_DIRS^} may be used,
11788 even in conjunction with one or several switches
11789 @option{^-D^/DIRS_FILE^}. Several Naming Patterns and one excluded pattern
11790 are used in this example.
11791
11792 @c *****************************************
11793 @c * G N A T P r o j e c t M a n a g e r *
11794 @c *****************************************
11795 @node GNAT Project Manager
11796 @chapter GNAT Project Manager
11797
11798 @menu
11799 * Introduction::
11800 * Examples of Project Files::
11801 * Project File Syntax::
11802 * Objects and Sources in Project Files::
11803 * Importing Projects::
11804 * Project Extension::
11805 * Project Hierarchy Extension::
11806 * External References in Project Files::
11807 * Packages in Project Files::
11808 * Variables from Imported Projects::
11809 * Naming Schemes::
11810 * Library Projects::
11811 * Stand-alone Library Projects::
11812 * Switches Related to Project Files::
11813 * Tools Supporting Project Files::
11814 * An Extended Example::
11815 * Project File Complete Syntax::
11816 @end menu
11817
11818 @c ****************
11819 @c * Introduction *
11820 @c ****************
11821
11822 @node Introduction
11823 @section Introduction
11824
11825 @noindent
11826 This chapter describes GNAT's @emph{Project Manager}, a facility that allows
11827 you to manage complex builds involving a number of source files, directories,
11828 and compilation options for different system configurations. In particular,
11829 project files allow you to specify:
11830 @itemize @bullet
11831 @item
11832 The directory or set of directories containing the source files, and/or the
11833 names of the specific source files themselves
11834 @item
11835 The directory in which the compiler's output
11836 (@file{ALI} files, object files, tree files) is to be placed
11837 @item
11838 The directory in which the executable programs is to be placed
11839 @item
11840 ^Switch^Switch^ settings for any of the project-enabled tools
11841 (@command{gnatmake}, compiler, binder, linker, @code{gnatls}, @code{gnatxref},
11842 @code{gnatfind}); you can apply these settings either globally or to individual
11843 compilation units.
11844 @item
11845 The source files containing the main subprogram(s) to be built
11846 @item
11847 The source programming language(s) (currently Ada and/or C)
11848 @item
11849 Source file naming conventions; you can specify these either globally or for
11850 individual compilation units
11851 @end itemize
11852
11853 @menu
11854 * Project Files::
11855 @end menu
11856
11857 @node Project Files
11858 @subsection Project Files
11859
11860 @noindent
11861 Project files are written in a syntax close to that of Ada, using familiar
11862 notions such as packages, context clauses, declarations, default values,
11863 assignments, and inheritance. Finally, project files can be built
11864 hierarchically from other project files, simplifying complex system
11865 integration and project reuse.
11866
11867 A @dfn{project} is a specific set of values for various compilation properties.
11868 The settings for a given project are described by means of
11869 a @dfn{project file}, which is a text file written in an Ada-like syntax.
11870 Property values in project files are either strings or lists of strings.
11871 Properties that are not explicitly set receive default values. A project
11872 file may interrogate the values of @dfn{external variables} (user-defined
11873 command-line switches or environment variables), and it may specify property
11874 settings conditionally, based on the value of such variables.
11875
11876 In simple cases, a project's source files depend only on other source files
11877 in the same project, or on the predefined libraries. (@emph{Dependence} is
11878 used in
11879 the Ada technical sense; as in one Ada unit @code{with}ing another.) However,
11880 the Project Manager also allows more sophisticated arrangements,
11881 where the source files in one project depend on source files in other
11882 projects:
11883 @itemize @bullet
11884 @item
11885 One project can @emph{import} other projects containing needed source files.
11886 @item
11887 You can organize GNAT projects in a hierarchy: a @emph{child} project
11888 can extend a @emph{parent} project, inheriting the parent's source files and
11889 optionally overriding any of them with alternative versions
11890 @end itemize
11891
11892 @noindent
11893 More generally, the Project Manager lets you structure large development
11894 efforts into hierarchical subsystems, where build decisions are delegated
11895 to the subsystem level, and thus different compilation environments
11896 (^switch^switch^ settings) used for different subsystems.
11897
11898 The Project Manager is invoked through the
11899 @option{^-P^/PROJECT_FILE=^@emph{projectfile}}
11900 switch to @command{gnatmake} or to the @command{^gnat^GNAT^} front driver.
11901 @ifclear vms
11902 There may be zero, one or more spaces between @option{-P} and
11903 @option{@emph{projectfile}}.
11904 @end ifclear
11905 If you want to define (on the command line) an external variable that is
11906 queried by the project file, you must use the
11907 @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
11908 The Project Manager parses and interprets the project file, and drives the
11909 invoked tool based on the project settings.
11910
11911 The Project Manager supports a wide range of development strategies,
11912 for systems of all sizes. Here are some typical practices that are
11913 easily handled:
11914 @itemize @bullet
11915 @item
11916 Using a common set of source files, but generating object files in different
11917 directories via different ^switch^switch^ settings
11918 @item
11919 Using a mostly-shared set of source files, but with different versions of
11920 some unit or units
11921 @end itemize
11922
11923 @noindent
11924 The destination of an executable can be controlled inside a project file
11925 using the @option{^-o^-o^}
11926 ^switch^switch^.
11927 In the absence of such a ^switch^switch^ either inside
11928 the project file or on the command line, any executable files generated by
11929 @command{gnatmake} are placed in the directory @code{Exec_Dir} specified
11930 in the project file. If no @code{Exec_Dir} is specified, they will be placed
11931 in the object directory of the project.
11932
11933 You can use project files to achieve some of the effects of a source
11934 versioning system (for example, defining separate projects for
11935 the different sets of sources that comprise different releases) but the
11936 Project Manager is independent of any source configuration management tools
11937 that might be used by the developers.
11938
11939 The next section introduces the main features of GNAT's project facility
11940 through a sequence of examples; subsequent sections will present the syntax
11941 and semantics in more detail. A more formal description of the project
11942 facility appears in @ref{Project File Reference,,, gnat_rm, GNAT
11943 Reference Manual}.
11944
11945 @c *****************************
11946 @c * Examples of Project Files *
11947 @c *****************************
11948
11949 @node Examples of Project Files
11950 @section Examples of Project Files
11951 @noindent
11952 This section illustrates some of the typical uses of project files and
11953 explains their basic structure and behavior.
11954
11955 @menu
11956 * Common Sources with Different ^Switches^Switches^ and Directories::
11957 * Using External Variables::
11958 * Importing Other Projects::
11959 * Extending a Project::
11960 @end menu
11961
11962 @node Common Sources with Different ^Switches^Switches^ and Directories
11963 @subsection Common Sources with Different ^Switches^Switches^ and Directories
11964
11965 @menu
11966 * Source Files::
11967 * Specifying the Object Directory::
11968 * Specifying the Exec Directory::
11969 * Project File Packages::
11970 * Specifying ^Switch^Switch^ Settings::
11971 * Main Subprograms::
11972 * Executable File Names::
11973 * Source File Naming Conventions::
11974 * Source Language(s)::
11975 @end menu
11976
11977 @noindent
11978 Suppose that the Ada source files @file{pack.ads}, @file{pack.adb}, and
11979 @file{proc.adb} are in the @file{/common} directory. The file
11980 @file{proc.adb} contains an Ada main subprogram @code{Proc} that @code{with}s
11981 package @code{Pack}. We want to compile these source files under two sets
11982 of ^switches^switches^:
11983 @itemize @bullet
11984 @item
11985 When debugging, we want to pass the @option{-g} switch to @command{gnatmake},
11986 and the @option{^-gnata^-gnata^},
11987 @option{^-gnato^-gnato^},
11988 and @option{^-gnatE^-gnatE^} switches to the
11989 compiler; the compiler's output is to appear in @file{/common/debug}
11990 @item
11991 When preparing a release version, we want to pass the @option{^-O2^O2^} switch
11992 to the compiler; the compiler's output is to appear in @file{/common/release}
11993 @end itemize
11994
11995 @noindent
11996 The GNAT project files shown below, respectively @file{debug.gpr} and
11997 @file{release.gpr} in the @file{/common} directory, achieve these effects.
11998
11999 Schematically:
12000 @smallexample
12001 @group
12002 ^/common^[COMMON]^
12003 debug.gpr
12004 release.gpr
12005 pack.ads
12006 pack.adb
12007 proc.adb
12008 @end group
12009 @group
12010 ^/common/debug^[COMMON.DEBUG]^
12011 proc.ali, proc.o
12012 pack.ali, pack.o
12013 @end group
12014 @group
12015 ^/common/release^[COMMON.RELEASE]^
12016 proc.ali, proc.o
12017 pack.ali, pack.o
12018 @end group
12019 @end smallexample
12020 Here are the corresponding project files:
12021
12022 @smallexample @c projectfile
12023 @group
12024 project Debug is
12025 for Object_Dir use "debug";
12026 for Main use ("proc");
12027
12028 package Builder is
12029 for ^Default_Switches^Default_Switches^ ("Ada")
12030 use ("^-g^-g^");
12031 for Executable ("proc.adb") use "proc1";
12032 end Builder;
12033 @end group
12034
12035 @group
12036 package Compiler is
12037 for ^Default_Switches^Default_Switches^ ("Ada")
12038 use ("-fstack-check",
12039 "^-gnata^-gnata^",
12040 "^-gnato^-gnato^",
12041 "^-gnatE^-gnatE^");
12042 end Compiler;
12043 end Debug;
12044 @end group
12045 @end smallexample
12046
12047 @smallexample @c projectfile
12048 @group
12049 project Release is
12050 for Object_Dir use "release";
12051 for Exec_Dir use ".";
12052 for Main use ("proc");
12053
12054 package Compiler is
12055 for ^Default_Switches^Default_Switches^ ("Ada")
12056 use ("^-O2^-O2^");
12057 end Compiler;
12058 end Release;
12059 @end group
12060 @end smallexample
12061
12062 @noindent
12063 The name of the project defined by @file{debug.gpr} is @code{"Debug"} (case
12064 insensitive), and analogously the project defined by @file{release.gpr} is
12065 @code{"Release"}. For consistency the file should have the same name as the
12066 project, and the project file's extension should be @code{"gpr"}. These
12067 conventions are not required, but a warning is issued if they are not followed.
12068
12069 If the current directory is @file{^/temp^[TEMP]^}, then the command
12070 @smallexample
12071 gnatmake ^-P/common/debug.gpr^/PROJECT_FILE=[COMMON]DEBUG^
12072 @end smallexample
12073
12074 @noindent
12075 generates object and ALI files in @file{^/common/debug^[COMMON.DEBUG]^},
12076 as well as the @code{^proc1^PROC1.EXE^} executable,
12077 using the ^switch^switch^ settings defined in the project file.
12078
12079 Likewise, the command
12080 @smallexample
12081 gnatmake ^-P/common/release.gpr^/PROJECT_FILE=[COMMON]RELEASE^
12082 @end smallexample
12083
12084 @noindent
12085 generates object and ALI files in @file{^/common/release^[COMMON.RELEASE]^},
12086 and the @code{^proc^PROC.EXE^}
12087 executable in @file{^/common^[COMMON]^},
12088 using the ^switch^switch^ settings from the project file.
12089
12090 @node Source Files
12091 @unnumberedsubsubsec Source Files
12092
12093 @noindent
12094 If a project file does not explicitly specify a set of source directories or
12095 a set of source files, then by default the project's source files are the
12096 Ada source files in the project file directory. Thus @file{pack.ads},
12097 @file{pack.adb}, and @file{proc.adb} are the source files for both projects.
12098
12099 @node Specifying the Object Directory
12100 @unnumberedsubsubsec Specifying the Object Directory
12101
12102 @noindent
12103 Several project properties are modeled by Ada-style @emph{attributes};
12104 a property is defined by supplying the equivalent of an Ada attribute
12105 definition clause in the project file.
12106 A project's object directory is another such a property; the corresponding
12107 attribute is @code{Object_Dir}, and its value is also a string expression,
12108 specified either as absolute or relative. In the later case,
12109 it is relative to the project file directory. Thus the compiler's
12110 output is directed to @file{^/common/debug^[COMMON.DEBUG]^}
12111 (for the @code{Debug} project)
12112 and to @file{^/common/release^[COMMON.RELEASE]^}
12113 (for the @code{Release} project).
12114 If @code{Object_Dir} is not specified, then the default is the project file
12115 directory itself.
12116
12117 @node Specifying the Exec Directory
12118 @unnumberedsubsubsec Specifying the Exec Directory
12119
12120 @noindent
12121 A project's exec directory is another property; the corresponding
12122 attribute is @code{Exec_Dir}, and its value is also a string expression,
12123 either specified as relative or absolute. If @code{Exec_Dir} is not specified,
12124 then the default is the object directory (which may also be the project file
12125 directory if attribute @code{Object_Dir} is not specified). Thus the executable
12126 is placed in @file{^/common/debug^[COMMON.DEBUG]^}
12127 for the @code{Debug} project (attribute @code{Exec_Dir} not specified)
12128 and in @file{^/common^[COMMON]^} for the @code{Release} project.
12129
12130 @node Project File Packages
12131 @unnumberedsubsubsec Project File Packages
12132
12133 @noindent
12134 A GNAT tool that is integrated with the Project Manager is modeled by a
12135 corresponding package in the project file. In the example above,
12136 The @code{Debug} project defines the packages @code{Builder}
12137 (for @command{gnatmake}) and @code{Compiler};
12138 the @code{Release} project defines only the @code{Compiler} package.
12139
12140 The Ada-like package syntax is not to be taken literally. Although packages in
12141 project files bear a surface resemblance to packages in Ada source code, the
12142 notation is simply a way to convey a grouping of properties for a named
12143 entity. Indeed, the package names permitted in project files are restricted
12144 to a predefined set, corresponding to the project-aware tools, and the contents
12145 of packages are limited to a small set of constructs.
12146 The packages in the example above contain attribute definitions.
12147
12148 @node Specifying ^Switch^Switch^ Settings
12149 @unnumberedsubsubsec Specifying ^Switch^Switch^ Settings
12150
12151 @noindent
12152 ^Switch^Switch^ settings for a project-aware tool can be specified through
12153 attributes in the package that corresponds to the tool.
12154 The example above illustrates one of the relevant attributes,
12155 @code{^Default_Switches^Default_Switches^}, which is defined in packages
12156 in both project files.
12157 Unlike simple attributes like @code{Source_Dirs},
12158 @code{^Default_Switches^Default_Switches^} is
12159 known as an @emph{associative array}. When you define this attribute, you must
12160 supply an ``index'' (a literal string), and the effect of the attribute
12161 definition is to set the value of the array at the specified index.
12162 For the @code{^Default_Switches^Default_Switches^} attribute,
12163 the index is a programming language (in our case, Ada),
12164 and the value specified (after @code{use}) must be a list
12165 of string expressions.
12166
12167 The attributes permitted in project files are restricted to a predefined set.
12168 Some may appear at project level, others in packages.
12169 For any attribute that is an associative array, the index must always be a
12170 literal string, but the restrictions on this string (e.g., a file name or a
12171 language name) depend on the individual attribute.
12172 Also depending on the attribute, its specified value will need to be either a
12173 string or a string list.
12174
12175 In the @code{Debug} project, we set the switches for two tools,
12176 @command{gnatmake} and the compiler, and thus we include the two corresponding
12177 packages; each package defines the @code{^Default_Switches^Default_Switches^}
12178 attribute with index @code{"Ada"}.
12179 Note that the package corresponding to
12180 @command{gnatmake} is named @code{Builder}. The @code{Release} project is
12181 similar, but only includes the @code{Compiler} package.
12182
12183 In project @code{Debug} above, the ^switches^switches^ starting with
12184 @option{-gnat} that are specified in package @code{Compiler}
12185 could have been placed in package @code{Builder}, since @command{gnatmake}
12186 transmits all such ^switches^switches^ to the compiler.
12187
12188 @node Main Subprograms
12189 @unnumberedsubsubsec Main Subprograms
12190
12191 @noindent
12192 One of the specifiable properties of a project is a list of files that contain
12193 main subprograms. This property is captured in the @code{Main} attribute,
12194 whose value is a list of strings. If a project defines the @code{Main}
12195 attribute, it is not necessary to identify the main subprogram(s) when
12196 invoking @command{gnatmake} (@pxref{gnatmake and Project Files}).
12197
12198 @node Executable File Names
12199 @unnumberedsubsubsec Executable File Names
12200
12201 @noindent
12202 By default, the executable file name corresponding to a main source is
12203 deduced from the main source file name. Through the attributes
12204 @code{Executable} and @code{Executable_Suffix} of package @code{Builder},
12205 it is possible to change this default.
12206 In project @code{Debug} above, the executable file name
12207 for main source @file{^proc.adb^PROC.ADB^} is
12208 @file{^proc1^PROC1.EXE^}.
12209 Attribute @code{Executable_Suffix}, when specified, may change the suffix
12210 of the executable files, when no attribute @code{Executable} applies:
12211 its value replace the platform-specific executable suffix.
12212 Attributes @code{Executable} and @code{Executable_Suffix} are the only ways to
12213 specify a non-default executable file name when several mains are built at once
12214 in a single @command{gnatmake} command.
12215
12216 @node Source File Naming Conventions
12217 @unnumberedsubsubsec Source File Naming Conventions
12218
12219 @noindent
12220 Since the project files above do not specify any source file naming
12221 conventions, the GNAT defaults are used. The mechanism for defining source
12222 file naming conventions -- a package named @code{Naming} --
12223 is described below (@pxref{Naming Schemes}).
12224
12225 @node Source Language(s)
12226 @unnumberedsubsubsec Source Language(s)
12227
12228 @noindent
12229 Since the project files do not specify a @code{Languages} attribute, by
12230 default the GNAT tools assume that the language of the project file is Ada.
12231 More generally, a project can comprise source files
12232 in Ada, C, and/or other languages.
12233
12234 @node Using External Variables
12235 @subsection Using External Variables
12236
12237 @noindent
12238 Instead of supplying different project files for debug and release, we can
12239 define a single project file that queries an external variable (set either
12240 on the command line or via an ^environment variable^logical name^) in order to
12241 conditionally define the appropriate settings. Again, assume that the
12242 source files @file{pack.ads}, @file{pack.adb}, and @file{proc.adb} are
12243 located in directory @file{^/common^[COMMON]^}. The following project file,
12244 @file{build.gpr}, queries the external variable named @code{STYLE} and
12245 defines an object directory and ^switch^switch^ settings based on whether
12246 the value is @code{"deb"} (debug) or @code{"rel"} (release), and where
12247 the default is @code{"deb"}.
12248
12249 @smallexample @c projectfile
12250 @group
12251 project Build is
12252 for Main use ("proc");
12253
12254 type Style_Type is ("deb", "rel");
12255 Style : Style_Type := external ("STYLE", "deb");
12256
12257 case Style is
12258 when "deb" =>
12259 for Object_Dir use "debug";
12260
12261 when "rel" =>
12262 for Object_Dir use "release";
12263 for Exec_Dir use ".";
12264 end case;
12265 @end group
12266
12267 @group
12268 package Builder is
12269
12270 case Style is
12271 when "deb" =>
12272 for ^Default_Switches^Default_Switches^ ("Ada")
12273 use ("^-g^-g^");
12274 for Executable ("proc") use "proc1";
12275 when others =>
12276 null;
12277 end case;
12278
12279 end Builder;
12280 @end group
12281
12282 @group
12283 package Compiler is
12284
12285 case Style is
12286 when "deb" =>
12287 for ^Default_Switches^Default_Switches^ ("Ada")
12288 use ("^-gnata^-gnata^",
12289 "^-gnato^-gnato^",
12290 "^-gnatE^-gnatE^");
12291
12292 when "rel" =>
12293 for ^Default_Switches^Default_Switches^ ("Ada")
12294 use ("^-O2^-O2^");
12295 end case;
12296
12297 end Compiler;
12298
12299 end Build;
12300 @end group
12301 @end smallexample
12302
12303 @noindent
12304 @code{Style_Type} is an example of a @emph{string type}, which is the project
12305 file analog of an Ada enumeration type but whose components are string literals
12306 rather than identifiers. @code{Style} is declared as a variable of this type.
12307
12308 The form @code{external("STYLE", "deb")} is known as an
12309 @emph{external reference}; its first argument is the name of an
12310 @emph{external variable}, and the second argument is a default value to be
12311 used if the external variable doesn't exist. You can define an external
12312 variable on the command line via the @option{^-X^/EXTERNAL_REFERENCE^} switch,
12313 or you can use ^an environment variable^a logical name^
12314 as an external variable.
12315
12316 Each @code{case} construct is expanded by the Project Manager based on the
12317 value of @code{Style}. Thus the command
12318 @ifclear vms
12319 @smallexample
12320 gnatmake -P/common/build.gpr -XSTYLE=deb
12321 @end smallexample
12322 @end ifclear
12323
12324 @ifset vms
12325 @smallexample
12326 gnatmake /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=deb
12327 @end smallexample
12328 @end ifset
12329
12330 @noindent
12331 is equivalent to the @command{gnatmake} invocation using the project file
12332 @file{debug.gpr} in the earlier example. So is the command
12333 @smallexample
12334 gnatmake ^-P/common/build.gpr^/PROJECT_FILE=[COMMON]BUILD.GPR^
12335 @end smallexample
12336
12337 @noindent
12338 since @code{"deb"} is the default for @code{STYLE}.
12339
12340 Analogously,
12341
12342 @ifclear vms
12343 @smallexample
12344 gnatmake -P/common/build.gpr -XSTYLE=rel
12345 @end smallexample
12346 @end ifclear
12347
12348 @ifset vms
12349 @smallexample
12350 GNAT MAKE /PROJECT_FILE=[COMMON]BUILD.GPR /EXTERNAL_REFERENCE=STYLE=rel
12351 @end smallexample
12352 @end ifset
12353
12354 @noindent
12355 is equivalent to the @command{gnatmake} invocation using the project file
12356 @file{release.gpr} in the earlier example.
12357
12358 @node Importing Other Projects
12359 @subsection Importing Other Projects
12360 @cindex @code{ADA_PROJECT_PATH}
12361 @cindex @code{GPR_PROJECT_PATH}
12362
12363 @noindent
12364 A compilation unit in a source file in one project may depend on compilation
12365 units in source files in other projects. To compile this unit under
12366 control of a project file, the
12367 dependent project must @emph{import} the projects containing the needed source
12368 files.
12369 This effect is obtained using syntax similar to an Ada @code{with} clause,
12370 but where @code{with}ed entities are strings that denote project files.
12371
12372 As an example, suppose that the two projects @code{GUI_Proj} and
12373 @code{Comm_Proj} are defined in the project files @file{gui_proj.gpr} and
12374 @file{comm_proj.gpr} in directories @file{^/gui^[GUI]^}
12375 and @file{^/comm^[COMM]^}, respectively.
12376 Suppose that the source files for @code{GUI_Proj} are
12377 @file{gui.ads} and @file{gui.adb}, and that the source files for
12378 @code{Comm_Proj} are @file{comm.ads} and @file{comm.adb}, where each set of
12379 files is located in its respective project file directory. Schematically:
12380
12381 @smallexample
12382 @group
12383 ^/gui^[GUI]^
12384 gui_proj.gpr
12385 gui.ads
12386 gui.adb
12387 @end group
12388
12389 @group
12390 ^/comm^[COMM]^
12391 comm_proj.gpr
12392 comm.ads
12393 comm.adb
12394 @end group
12395 @end smallexample
12396
12397 @noindent
12398 We want to develop an application in directory @file{^/app^[APP]^} that
12399 @code{with} the packages @code{GUI} and @code{Comm}, using the properties of
12400 the corresponding project files (e.g.@: the ^switch^switch^ settings
12401 and object directory).
12402 Skeletal code for a main procedure might be something like the following:
12403
12404 @smallexample @c ada
12405 @group
12406 with GUI, Comm;
12407 procedure App_Main is
12408 @dots{}
12409 begin
12410 @dots{}
12411 end App_Main;
12412 @end group
12413 @end smallexample
12414
12415 @noindent
12416 Here is a project file, @file{app_proj.gpr}, that achieves the desired
12417 effect:
12418
12419 @smallexample @c projectfile
12420 @group
12421 with "/gui/gui_proj", "/comm/comm_proj";
12422 project App_Proj is
12423 for Main use ("app_main");
12424 end App_Proj;
12425 @end group
12426 @end smallexample
12427
12428 @noindent
12429 Building an executable is achieved through the command:
12430 @smallexample
12431 gnatmake ^-P/app/app_proj^/PROJECT_FILE=[APP]APP_PROJ^
12432 @end smallexample
12433 @noindent
12434 which will generate the @code{^app_main^APP_MAIN.EXE^} executable
12435 in the directory where @file{app_proj.gpr} resides.
12436
12437 If an imported project file uses the standard extension (@code{^gpr^GPR^}) then
12438 (as illustrated above) the @code{with} clause can omit the extension.
12439
12440 Our example specified an absolute path for each imported project file.
12441 Alternatively, the directory name of an imported object can be omitted
12442 if either
12443 @itemize @bullet
12444 @item
12445 The imported project file is in the same directory as the importing project
12446 file, or
12447 @item
12448 You have defined one or two ^environment variables^logical names^
12449 that includes the directory containing
12450 the needed project file. The syntax of @code{GPR_PROJECT_PATH} and
12451 @code{ADA_PROJECT_PATH} is the same as
12452 the syntax of @code{ADA_INCLUDE_PATH} and @code{ADA_OBJECTS_PATH}: a list of
12453 directory names separated by colons (semicolons on Windows).
12454 @end itemize
12455
12456 @noindent
12457 Thus, if we define @code{ADA_PROJECT_PATH} or @code{GPR_PROJECT_PATH}
12458 to include @file{^/gui^[GUI]^} and
12459 @file{^/comm^[COMM]^}, then our project file @file{app_proj.gpr} can be written
12460 as follows:
12461
12462 @smallexample @c projectfile
12463 @group
12464 with "gui_proj", "comm_proj";
12465 project App_Proj is
12466 for Main use ("app_main");
12467 end App_Proj;
12468 @end group
12469 @end smallexample
12470
12471 @noindent
12472 Importing other projects can create ambiguities.
12473 For example, the same unit might be present in different imported projects, or
12474 it might be present in both the importing project and in an imported project.
12475 Both of these conditions are errors. Note that in the current version of
12476 the Project Manager, it is illegal to have an ambiguous unit even if the
12477 unit is never referenced by the importing project. This restriction may be
12478 relaxed in a future release.
12479
12480 @node Extending a Project
12481 @subsection Extending a Project
12482
12483 @noindent
12484 In large software systems it is common to have multiple
12485 implementations of a common interface; in Ada terms, multiple versions of a
12486 package body for the same spec. For example, one implementation
12487 might be safe for use in tasking programs, while another might only be used
12488 in sequential applications. This can be modeled in GNAT using the concept
12489 of @emph{project extension}. If one project (the ``child'') @emph{extends}
12490 another project (the ``parent'') then by default all source files of the
12491 parent project are inherited by the child, but the child project can
12492 override any of the parent's source files with new versions, and can also
12493 add new files. This facility is the project analog of a type extension in
12494 Object-Oriented Programming. Project hierarchies are permitted (a child
12495 project may be the parent of yet another project), and a project that
12496 inherits one project can also import other projects.
12497
12498 As an example, suppose that directory @file{^/seq^[SEQ]^} contains the project
12499 file @file{seq_proj.gpr} as well as the source files @file{pack.ads},
12500 @file{pack.adb}, and @file{proc.adb}:
12501
12502 @smallexample
12503 @group
12504 ^/seq^[SEQ]^
12505 pack.ads
12506 pack.adb
12507 proc.adb
12508 seq_proj.gpr
12509 @end group
12510 @end smallexample
12511
12512 @noindent
12513 Note that the project file can simply be empty (that is, no attribute or
12514 package is defined):
12515
12516 @smallexample @c projectfile
12517 @group
12518 project Seq_Proj is
12519 end Seq_Proj;
12520 @end group
12521 @end smallexample
12522
12523 @noindent
12524 implying that its source files are all the Ada source files in the project
12525 directory.
12526
12527 Suppose we want to supply an alternate version of @file{pack.adb}, in
12528 directory @file{^/tasking^[TASKING]^}, but use the existing versions of
12529 @file{pack.ads} and @file{proc.adb}. We can define a project
12530 @code{Tasking_Proj} that inherits @code{Seq_Proj}:
12531
12532 @smallexample
12533 @group
12534 ^/tasking^[TASKING]^
12535 pack.adb
12536 tasking_proj.gpr
12537 @end group
12538
12539 @group
12540 project Tasking_Proj extends "/seq/seq_proj" is
12541 end Tasking_Proj;
12542 @end group
12543 @end smallexample
12544
12545 @noindent
12546 The version of @file{pack.adb} used in a build depends on which project file
12547 is specified.
12548
12549 Note that we could have obtained the desired behavior using project import
12550 rather than project inheritance; a @code{base} project would contain the
12551 sources for @file{pack.ads} and @file{proc.adb}, a sequential project would
12552 import @code{base} and add @file{pack.adb}, and likewise a tasking project
12553 would import @code{base} and add a different version of @file{pack.adb}. The
12554 choice depends on whether other sources in the original project need to be
12555 overridden. If they do, then project extension is necessary, otherwise,
12556 importing is sufficient.
12557
12558 @noindent
12559 In a project file that extends another project file, it is possible to
12560 indicate that an inherited source is not part of the sources of the extending
12561 project. This is necessary sometimes when a package spec has been overloaded
12562 and no longer requires a body: in this case, it is necessary to indicate that
12563 the inherited body is not part of the sources of the project, otherwise there
12564 will be a compilation error when compiling the spec.
12565
12566 For that purpose, the attribute @code{Excluded_Source_Files} is used.
12567 Its value is a string list: a list of file names. It is also possible to use
12568 attribute @code{Excluded_Source_List_File}. Its value is a single string:
12569 the file name of a text file containing a list of file names, one per line.
12570
12571 @smallexample @c @projectfile
12572 project B extends "a" is
12573 for Source_Files use ("pkg.ads");
12574 -- New spec of Pkg does not need a completion
12575 for Excluded_Source_Files use ("pkg.adb");
12576 end B;
12577 @end smallexample
12578
12579 Attribute @code{Excluded_Source_Files} may also be used to check if a source
12580 is still needed: if it is possible to build using @command{gnatmake} when such
12581 a source is put in attribute @code{Excluded_Source_Files} of a project P, then
12582 it is possible to remove the source completely from a system that includes
12583 project P.
12584
12585 @c ***********************
12586 @c * Project File Syntax *
12587 @c ***********************
12588
12589 @node Project File Syntax
12590 @section Project File Syntax
12591
12592 @menu
12593 * Basic Syntax::
12594 * Qualified Projects::
12595 * Packages::
12596 * Expressions::
12597 * String Types::
12598 * Variables::
12599 * Attributes::
12600 * Associative Array Attributes::
12601 * case Constructions::
12602 @end menu
12603
12604 @noindent
12605 This section describes the structure of project files.
12606
12607 A project may be an @emph{independent project}, entirely defined by a single
12608 project file. Any Ada source file in an independent project depends only
12609 on the predefined library and other Ada source files in the same project.
12610
12611 @noindent
12612 A project may also @dfn{depend on} other projects, in either or both of
12613 the following ways:
12614 @itemize @bullet
12615 @item It may import any number of projects
12616 @item It may extend at most one other project
12617 @end itemize
12618
12619 @noindent
12620 The dependence relation is a directed acyclic graph (the subgraph reflecting
12621 the ``extends'' relation is a tree).
12622
12623 A project's @dfn{immediate sources} are the source files directly defined by
12624 that project, either implicitly by residing in the project file's directory,
12625 or explicitly through any of the source-related attributes described below.
12626 More generally, a project @var{proj}'s @dfn{sources} are the immediate sources
12627 of @var{proj} together with the immediate sources (unless overridden) of any
12628 project on which @var{proj} depends (either directly or indirectly).
12629
12630 @node Basic Syntax
12631 @subsection Basic Syntax
12632
12633 @noindent
12634 As seen in the earlier examples, project files have an Ada-like syntax.
12635 The minimal project file is:
12636 @smallexample @c projectfile
12637 @group
12638 project Empty is
12639
12640 end Empty;
12641 @end group
12642 @end smallexample
12643
12644 @noindent
12645 The identifier @code{Empty} is the name of the project.
12646 This project name must be present after the reserved
12647 word @code{end} at the end of the project file, followed by a semi-colon.
12648
12649 Any name in a project file, such as the project name or a variable name,
12650 has the same syntax as an Ada identifier.
12651
12652 The reserved words of project files are the Ada 95 reserved words plus
12653 @code{extends}, @code{external}, and @code{project}. Note that the only Ada
12654 reserved words currently used in project file syntax are:
12655
12656 @itemize @bullet
12657 @item
12658 @code{all}
12659 @item
12660 @code{at}
12661 @item
12662 @code{case}
12663 @item
12664 @code{end}
12665 @item
12666 @code{for}
12667 @item
12668 @code{is}
12669 @item
12670 @code{limited}
12671 @item
12672 @code{null}
12673 @item
12674 @code{others}
12675 @item
12676 @code{package}
12677 @item
12678 @code{renames}
12679 @item
12680 @code{type}
12681 @item
12682 @code{use}
12683 @item
12684 @code{when}
12685 @item
12686 @code{with}
12687 @end itemize
12688
12689 @noindent
12690 Comments in project files have the same syntax as in Ada, two consecutive
12691 hyphens through the end of the line.
12692
12693 @node Qualified Projects
12694 @subsection Qualified Projects
12695
12696 @noindent
12697 Before the reserved @code{project}, there may be one or two "qualifiers", that
12698 is identifiers or other reserved words, to qualify the project.
12699
12700 The current list of qualifiers is:
12701
12702 @itemize @bullet
12703 @item
12704 @code{abstract}: qualify a project with no sources. A qualified abstract
12705 project must either have no declaration of attributes @code{Source_Dirs},
12706 @code{Source_Files}, @code{Languages} or @code{Source_List_File}, or one of
12707 @code{Source_Dirs}, @code{Source_Files}, or @code{Languages} must be declared
12708 as empty. If it extends another project, the project it extends must also be a
12709 qualified abstract project.
12710
12711 @item
12712 @code{standard}: a standard project is a non library project with sources.
12713
12714 @item
12715 @code{aggregate}: for future extension
12716
12717 @item
12718 @code{aggregate library}: for future extension
12719
12720 @item
12721 @code{library}: a library project must declare both attributes
12722 @code{Library_Name} and @code{Library_Dir}.
12723
12724 @item
12725 @code{configuration}: a configuration project cannot be in a project tree.
12726 @end itemize
12727
12728 @node Packages
12729 @subsection Packages
12730
12731 @noindent
12732 A project file may contain @emph{packages}. The name of a package must be one
12733 of the identifiers from the following list. A package
12734 with a given name may only appear once in a project file. Package names are
12735 case insensitive. The following package names are legal:
12736
12737 @itemize @bullet
12738 @item
12739 @code{Naming}
12740 @item
12741 @code{Builder}
12742 @item
12743 @code{Compiler}
12744 @item
12745 @code{Binder}
12746 @item
12747 @code{Linker}
12748 @item
12749 @code{Finder}
12750 @item
12751 @code{Cross_Reference}
12752 @item
12753 @code{Check}
12754 @item
12755 @code{Eliminate}
12756 @item
12757 @code{Pretty_Printer}
12758 @item
12759 @code{Metrics}
12760 @item
12761 @code{gnatls}
12762 @item
12763 @code{gnatstub}
12764 @item
12765 @code{IDE}
12766 @item
12767 @code{Language_Processing}
12768 @end itemize
12769
12770 @noindent
12771 In its simplest form, a package may be empty:
12772
12773 @smallexample @c projectfile
12774 @group
12775 project Simple is
12776 package Builder is
12777 end Builder;
12778 end Simple;
12779 @end group
12780 @end smallexample
12781
12782 @noindent
12783 A package may contain @emph{attribute declarations},
12784 @emph{variable declarations} and @emph{case constructions}, as will be
12785 described below.
12786
12787 When there is ambiguity between a project name and a package name,
12788 the name always designates the project. To avoid possible confusion, it is
12789 always a good idea to avoid naming a project with one of the
12790 names allowed for packages or any name that starts with @code{gnat}.
12791
12792 @node Expressions
12793 @subsection Expressions
12794
12795 @noindent
12796 An @emph{expression} is either a @emph{string expression} or a
12797 @emph{string list expression}.
12798
12799 A @emph{string expression} is either a @emph{simple string expression} or a
12800 @emph{compound string expression}.
12801
12802 A @emph{simple string expression} is one of the following:
12803 @itemize @bullet
12804 @item A literal string; e.g.@: @code{"comm/my_proj.gpr"}
12805 @item A string-valued variable reference (@pxref{Variables})
12806 @item A string-valued attribute reference (@pxref{Attributes})
12807 @item An external reference (@pxref{External References in Project Files})
12808 @end itemize
12809
12810 @noindent
12811 A @emph{compound string expression} is a concatenation of string expressions,
12812 using the operator @code{"&"}
12813 @smallexample
12814 Path & "/" & File_Name & ".ads"
12815 @end smallexample
12816
12817 @noindent
12818 A @emph{string list expression} is either a
12819 @emph{simple string list expression} or a
12820 @emph{compound string list expression}.
12821
12822 A @emph{simple string list expression} is one of the following:
12823 @itemize @bullet
12824 @item A parenthesized list of zero or more string expressions,
12825 separated by commas
12826 @smallexample
12827 File_Names := (File_Name, "gnat.adc", File_Name & ".orig");
12828 Empty_List := ();
12829 @end smallexample
12830 @item A string list-valued variable reference
12831 @item A string list-valued attribute reference
12832 @end itemize
12833
12834 @noindent
12835 A @emph{compound string list expression} is the concatenation (using
12836 @code{"&"}) of a simple string list expression and an expression. Note that
12837 each term in a compound string list expression, except the first, may be
12838 either a string expression or a string list expression.
12839
12840 @smallexample @c projectfile
12841 @group
12842 File_Name_List := () & File_Name; -- One string in this list
12843 Extended_File_Name_List := File_Name_List & (File_Name & ".orig");
12844 -- Two strings
12845 Big_List := File_Name_List & Extended_File_Name_List;
12846 -- Concatenation of two string lists: three strings
12847 Illegal_List := "gnat.adc" & Extended_File_Name_List;
12848 -- Illegal: must start with a string list
12849 @end group
12850 @end smallexample
12851
12852 @node String Types
12853 @subsection String Types
12854
12855 @noindent
12856 A @emph{string type declaration} introduces a discrete set of string literals.
12857 If a string variable is declared to have this type, its value
12858 is restricted to the given set of literals.
12859
12860 Here is an example of a string type declaration:
12861
12862 @smallexample @c projectfile
12863 type OS is ("NT", "nt", "Unix", "GNU/Linux", "other OS");
12864 @end smallexample
12865
12866 @noindent
12867 Variables of a string type are called @emph{typed variables}; all other
12868 variables are called @emph{untyped variables}. Typed variables are
12869 particularly useful in @code{case} constructions, to support conditional
12870 attribute declarations.
12871 (@pxref{case Constructions}).
12872
12873 The string literals in the list are case sensitive and must all be different.
12874 They may include any graphic characters allowed in Ada, including spaces.
12875
12876 A string type may only be declared at the project level, not inside a package.
12877
12878 A string type may be referenced by its name if it has been declared in the same
12879 project file, or by an expanded name whose prefix is the name of the project
12880 in which it is declared.
12881
12882 @node Variables
12883 @subsection Variables
12884
12885 @noindent
12886 A variable may be declared at the project file level, or within a package.
12887 Here are some examples of variable declarations:
12888
12889 @smallexample @c projectfile
12890 @group
12891 This_OS : OS := external ("OS"); -- a typed variable declaration
12892 That_OS := "GNU/Linux"; -- an untyped variable declaration
12893 @end group
12894 @end smallexample
12895
12896 @noindent
12897 The syntax of a @emph{typed variable declaration} is identical to the Ada
12898 syntax for an object declaration. By contrast, the syntax of an untyped
12899 variable declaration is identical to an Ada assignment statement. In fact,
12900 variable declarations in project files have some of the characteristics of
12901 an assignment, in that successive declarations for the same variable are
12902 allowed. Untyped variable declarations do establish the expected kind of the
12903 variable (string or string list), and successive declarations for it must
12904 respect the initial kind.
12905
12906 @noindent
12907 A string variable declaration (typed or untyped) declares a variable
12908 whose value is a string. This variable may be used as a string expression.
12909 @smallexample @c projectfile
12910 File_Name := "readme.txt";
12911 Saved_File_Name := File_Name & ".saved";
12912 @end smallexample
12913
12914 @noindent
12915 A string list variable declaration declares a variable whose value is a list
12916 of strings. The list may contain any number (zero or more) of strings.
12917
12918 @smallexample @c projectfile
12919 Empty_List := ();
12920 List_With_One_Element := ("^-gnaty^-gnaty^");
12921 List_With_Two_Elements := List_With_One_Element & "^-gnatg^-gnatg^";
12922 Long_List := ("main.ada", "pack1_.ada", "pack1.ada", "pack2_.ada"
12923 "pack2.ada", "util_.ada", "util.ada");
12924 @end smallexample
12925
12926 @noindent
12927 The same typed variable may not be declared more than once at project level,
12928 and it may not be declared more than once in any package; it is in effect
12929 a constant.
12930
12931 The same untyped variable may be declared several times. Declarations are
12932 elaborated in the order in which they appear, so the new value replaces
12933 the old one, and any subsequent reference to the variable uses the new value.
12934 However, as noted above, if a variable has been declared as a string, all
12935 subsequent
12936 declarations must give it a string value. Similarly, if a variable has
12937 been declared as a string list, all subsequent declarations
12938 must give it a string list value.
12939
12940 A @emph{variable reference} may take several forms:
12941
12942 @itemize @bullet
12943 @item The simple variable name, for a variable in the current package (if any)
12944 or in the current project
12945 @item An expanded name, whose prefix is a context name.
12946 @end itemize
12947
12948 @noindent
12949 A @emph{context} may be one of the following:
12950
12951 @itemize @bullet
12952 @item The name of an existing package in the current project
12953 @item The name of an imported project of the current project
12954 @item The name of an ancestor project (i.e., a project extended by the current
12955 project, either directly or indirectly)
12956 @item An expanded name whose prefix is an imported/parent project name, and
12957 whose selector is a package name in that project.
12958 @end itemize
12959
12960 @noindent
12961 A variable reference may be used in an expression.
12962
12963 @node Attributes
12964 @subsection Attributes
12965
12966 @noindent
12967 A project (and its packages) may have @emph{attributes} that define
12968 the project's properties. Some attributes have values that are strings;
12969 others have values that are string lists.
12970
12971 There are two categories of attributes: @emph{simple attributes}
12972 and @emph{associative arrays} (@pxref{Associative Array Attributes}).
12973
12974 Legal project attribute names, and attribute names for each legal package are
12975 listed below. Attributes names are case-insensitive.
12976
12977 The following attributes are defined on projects (all are simple attributes):
12978
12979 @multitable @columnfractions .4 .3
12980 @item @emph{Attribute Name}
12981 @tab @emph{Value}
12982 @item @code{Source_Files}
12983 @tab string list
12984 @item @code{Source_Dirs}
12985 @tab string list
12986 @item @code{Source_List_File}
12987 @tab string
12988 @item @code{Object_Dir}
12989 @tab string
12990 @item @code{Exec_Dir}
12991 @tab string
12992 @item @code{Excluded_Source_Dirs}
12993 @tab string list
12994 @item @code{Excluded_Source_Files}
12995 @tab string list
12996 @item @code{Excluded_Source_List_File}
12997 @tab string
12998 @item @code{Languages}
12999 @tab string list
13000 @item @code{Main}
13001 @tab string list
13002 @item @code{Library_Dir}
13003 @tab string
13004 @item @code{Library_Name}
13005 @tab string
13006 @item @code{Library_Kind}
13007 @tab string
13008 @item @code{Library_Version}
13009 @tab string
13010 @item @code{Library_Interface}
13011 @tab string
13012 @item @code{Library_Auto_Init}
13013 @tab string
13014 @item @code{Library_Options}
13015 @tab string list
13016 @item @code{Library_Src_Dir}
13017 @tab string
13018 @item @code{Library_ALI_Dir}
13019 @tab string
13020 @item @code{Library_GCC}
13021 @tab string
13022 @item @code{Library_Symbol_File}
13023 @tab string
13024 @item @code{Library_Symbol_Policy}
13025 @tab string
13026 @item @code{Library_Reference_Symbol_File}
13027 @tab string
13028 @item @code{Externally_Built}
13029 @tab string
13030 @end multitable
13031
13032 @noindent
13033 The following attributes are defined for package @code{Naming}
13034 (@pxref{Naming Schemes}):
13035
13036 @multitable @columnfractions .4 .2 .2 .2
13037 @item Attribute Name @tab Category @tab Index @tab Value
13038 @item @code{Spec_Suffix}
13039 @tab associative array
13040 @tab language name
13041 @tab string
13042 @item @code{Body_Suffix}
13043 @tab associative array
13044 @tab language name
13045 @tab string
13046 @item @code{Separate_Suffix}
13047 @tab simple attribute
13048 @tab n/a
13049 @tab string
13050 @item @code{Casing}
13051 @tab simple attribute
13052 @tab n/a
13053 @tab string
13054 @item @code{Dot_Replacement}
13055 @tab simple attribute
13056 @tab n/a
13057 @tab string
13058 @item @code{Spec}
13059 @tab associative array
13060 @tab Ada unit name
13061 @tab string
13062 @item @code{Body}
13063 @tab associative array
13064 @tab Ada unit name
13065 @tab string
13066 @item @code{Specification_Exceptions}
13067 @tab associative array
13068 @tab language name
13069 @tab string list
13070 @item @code{Implementation_Exceptions}
13071 @tab associative array
13072 @tab language name
13073 @tab string list
13074 @end multitable
13075
13076 @noindent
13077 The following attributes are defined for packages @code{Builder},
13078 @code{Compiler}, @code{Binder},
13079 @code{Linker}, @code{Cross_Reference}, and @code{Finder}
13080 (@pxref{^Switches^Switches^ and Project Files}).
13081
13082 @multitable @columnfractions .4 .2 .2 .2
13083 @item Attribute Name @tab Category @tab Index @tab Value
13084 @item @code{^Default_Switches^Default_Switches^}
13085 @tab associative array
13086 @tab language name
13087 @tab string list
13088 @item @code{^Switches^Switches^}
13089 @tab associative array
13090 @tab file name
13091 @tab string list
13092 @end multitable
13093
13094 @noindent
13095 In addition, package @code{Compiler} has a single string attribute
13096 @code{Local_Configuration_Pragmas} and package @code{Builder} has a single
13097 string attribute @code{Global_Configuration_Pragmas}.
13098
13099 @noindent
13100 Each simple attribute has a default value: the empty string (for string-valued
13101 attributes) and the empty list (for string list-valued attributes).
13102
13103 An attribute declaration defines a new value for an attribute.
13104
13105 Examples of simple attribute declarations:
13106
13107 @smallexample @c projectfile
13108 for Object_Dir use "objects";
13109 for Source_Dirs use ("units", "test/drivers");
13110 @end smallexample
13111
13112 @noindent
13113 The syntax of a @dfn{simple attribute declaration} is similar to that of an
13114 attribute definition clause in Ada.
13115
13116 Attributes references may be appear in expressions.
13117 The general form for such a reference is @code{<entity>'<attribute>}:
13118 Associative array attributes are functions. Associative
13119 array attribute references must have an argument that is a string literal.
13120
13121 Examples are:
13122
13123 @smallexample @c projectfile
13124 project'Object_Dir
13125 Naming'Dot_Replacement
13126 Imported_Project'Source_Dirs
13127 Imported_Project.Naming'Casing
13128 Builder'^Default_Switches^Default_Switches^("Ada")
13129 @end smallexample
13130
13131 @noindent
13132 The prefix of an attribute may be:
13133 @itemize @bullet
13134 @item @code{project} for an attribute of the current project
13135 @item The name of an existing package of the current project
13136 @item The name of an imported project
13137 @item The name of a parent project that is extended by the current project
13138 @item An expanded name whose prefix is imported/parent project name,
13139 and whose selector is a package name
13140 @end itemize
13141
13142 @noindent
13143 Example:
13144 @smallexample @c projectfile
13145 @group
13146 project Prj is
13147 for Source_Dirs use project'Source_Dirs & "units";
13148 for Source_Dirs use project'Source_Dirs & "test/drivers"
13149 end Prj;
13150 @end group
13151 @end smallexample
13152
13153 @noindent
13154 In the first attribute declaration, initially the attribute @code{Source_Dirs}
13155 has the default value: an empty string list. After this declaration,
13156 @code{Source_Dirs} is a string list of one element: @code{"units"}.
13157 After the second attribute declaration @code{Source_Dirs} is a string list of
13158 two elements: @code{"units"} and @code{"test/drivers"}.
13159
13160 Note: this example is for illustration only. In practice,
13161 the project file would contain only one attribute declaration:
13162
13163 @smallexample @c projectfile
13164 for Source_Dirs use ("units", "test/drivers");
13165 @end smallexample
13166
13167 @node Associative Array Attributes
13168 @subsection Associative Array Attributes
13169
13170 @noindent
13171 Some attributes are defined as @emph{associative arrays}. An associative
13172 array may be regarded as a function that takes a string as a parameter
13173 and delivers a string or string list value as its result.
13174
13175 Here are some examples of single associative array attribute associations:
13176
13177 @smallexample @c projectfile
13178 for Body ("main") use "Main.ada";
13179 for ^Switches^Switches^ ("main.ada")
13180 use ("^-v^-v^",
13181 "^-gnatv^-gnatv^");
13182 for ^Switches^Switches^ ("main.ada")
13183 use Builder'^Switches^Switches^ ("main.ada")
13184 & "^-g^-g^";
13185 @end smallexample
13186
13187 @noindent
13188 Like untyped variables and simple attributes, associative array attributes
13189 may be declared several times. Each declaration supplies a new value for the
13190 attribute, and replaces the previous setting.
13191
13192 @noindent
13193 An associative array attribute may be declared as a full associative array
13194 declaration, with the value of the same attribute in an imported or extended
13195 project.
13196
13197 @smallexample @c projectfile
13198 package Builder is
13199 for Default_Switches use Default.Builder'Default_Switches;
13200 end Builder;
13201 @end smallexample
13202
13203 @noindent
13204 In this example, @code{Default} must be either a project imported by the
13205 current project, or the project that the current project extends. If the
13206 attribute is in a package (in this case, in package @code{Builder}), the same
13207 package needs to be specified.
13208
13209 @noindent
13210 A full associative array declaration replaces any other declaration for the
13211 attribute, including other full associative array declaration. Single
13212 associative array associations may be declare after a full associative
13213 declaration, modifying the value for a single association of the attribute.
13214
13215 @node case Constructions
13216 @subsection @code{case} Constructions
13217
13218 @noindent
13219 A @code{case} construction is used in a project file to effect conditional
13220 behavior.
13221 Here is a typical example:
13222
13223 @smallexample @c projectfile
13224 @group
13225 project MyProj is
13226 type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
13227
13228 OS : OS_Type := external ("OS", "GNU/Linux");
13229 @end group
13230
13231 @group
13232 package Compiler is
13233 case OS is
13234 when "GNU/Linux" | "Unix" =>
13235 for ^Default_Switches^Default_Switches^ ("Ada")
13236 use ("^-gnath^-gnath^");
13237 when "NT" =>
13238 for ^Default_Switches^Default_Switches^ ("Ada")
13239 use ("^-gnatP^-gnatP^");
13240 when others =>
13241 end case;
13242 end Compiler;
13243 end MyProj;
13244 @end group
13245 @end smallexample
13246
13247 @noindent
13248 The syntax of a @code{case} construction is based on the Ada case statement
13249 (although there is no @code{null} construction for empty alternatives).
13250
13251 The case expression must be a typed string variable.
13252 Each alternative comprises the reserved word @code{when}, either a list of
13253 literal strings separated by the @code{"|"} character or the reserved word
13254 @code{others}, and the @code{"=>"} token.
13255 Each literal string must belong to the string type that is the type of the
13256 case variable.
13257 An @code{others} alternative, if present, must occur last.
13258
13259 After each @code{=>}, there are zero or more constructions. The only
13260 constructions allowed in a case construction are other case constructions,
13261 attribute declarations and variable declarations. String type declarations and
13262 package declarations are not allowed. Variable declarations are restricted to
13263 variables that have already been declared before the case construction.
13264
13265 The value of the case variable is often given by an external reference
13266 (@pxref{External References in Project Files}).
13267
13268 @c ****************************************
13269 @c * Objects and Sources in Project Files *
13270 @c ****************************************
13271
13272 @node Objects and Sources in Project Files
13273 @section Objects and Sources in Project Files
13274
13275 @menu
13276 * Object Directory::
13277 * Exec Directory::
13278 * Source Directories::
13279 * Source File Names::
13280 @end menu
13281
13282 @noindent
13283 Each project has exactly one object directory and one or more source
13284 directories. The source directories must contain at least one source file,
13285 unless the project file explicitly specifies that no source files are present
13286 (@pxref{Source File Names}).
13287
13288 @node Object Directory
13289 @subsection Object Directory
13290
13291 @noindent
13292 The object directory for a project is the directory containing the compiler's
13293 output (such as @file{ALI} files and object files) for the project's immediate
13294 sources.
13295
13296 The object directory is given by the value of the attribute @code{Object_Dir}
13297 in the project file.
13298
13299 @smallexample @c projectfile
13300 for Object_Dir use "objects";
13301 @end smallexample
13302
13303 @noindent
13304 The attribute @code{Object_Dir} has a string value, the path name of the object
13305 directory. The path name may be absolute or relative to the directory of the
13306 project file. This directory must already exist, and be readable and writable.
13307
13308 By default, when the attribute @code{Object_Dir} is not given an explicit value
13309 or when its value is the empty string, the object directory is the same as the
13310 directory containing the project file.
13311
13312 @node Exec Directory
13313 @subsection Exec Directory
13314
13315 @noindent
13316 The exec directory for a project is the directory containing the executables
13317 for the project's main subprograms.
13318
13319 The exec directory is given by the value of the attribute @code{Exec_Dir}
13320 in the project file.
13321
13322 @smallexample @c projectfile
13323 for Exec_Dir use "executables";
13324 @end smallexample
13325
13326 @noindent
13327 The attribute @code{Exec_Dir} has a string value, the path name of the exec
13328 directory. The path name may be absolute or relative to the directory of the
13329 project file. This directory must already exist, and be writable.
13330
13331 By default, when the attribute @code{Exec_Dir} is not given an explicit value
13332 or when its value is the empty string, the exec directory is the same as the
13333 object directory of the project file.
13334
13335 @node Source Directories
13336 @subsection Source Directories
13337
13338 @noindent
13339 The source directories of a project are specified by the project file
13340 attribute @code{Source_Dirs}.
13341
13342 This attribute's value is a string list. If the attribute is not given an
13343 explicit value, then there is only one source directory, the one where the
13344 project file resides.
13345
13346 A @code{Source_Dirs} attribute that is explicitly defined to be the empty list,
13347 as in
13348
13349 @smallexample @c projectfile
13350 for Source_Dirs use ();
13351 @end smallexample
13352
13353 @noindent
13354 indicates that the project contains no source files.
13355
13356 Otherwise, each string in the string list designates one or more
13357 source directories.
13358
13359 @smallexample @c projectfile
13360 for Source_Dirs use ("sources", "test/drivers");
13361 @end smallexample
13362
13363 @noindent
13364 If a string in the list ends with @code{"/**"}, then the directory whose path
13365 name precedes the two asterisks, as well as all its subdirectories
13366 (recursively), are source directories.
13367
13368 @smallexample @c projectfile
13369 for Source_Dirs use ("/system/sources/**");
13370 @end smallexample
13371
13372 @noindent
13373 Here the directory @code{/system/sources} and all of its subdirectories
13374 (recursively) are source directories.
13375
13376 To specify that the source directories are the directory of the project file
13377 and all of its subdirectories, you can declare @code{Source_Dirs} as follows:
13378 @smallexample @c projectfile
13379 for Source_Dirs use ("./**");
13380 @end smallexample
13381
13382 @noindent
13383 Each of the source directories must exist and be readable.
13384
13385 @node Source File Names
13386 @subsection Source File Names
13387
13388 @noindent
13389 In a project that contains source files, their names may be specified by the
13390 attributes @code{Source_Files} (a string list) or @code{Source_List_File}
13391 (a string). Source file names never include any directory information.
13392
13393 If the attribute @code{Source_Files} is given an explicit value, then each
13394 element of the list is a source file name.
13395
13396 @smallexample @c projectfile
13397 for Source_Files use ("main.adb");
13398 for Source_Files use ("main.adb", "pack1.ads", "pack2.adb");
13399 @end smallexample
13400
13401 @noindent
13402 If the attribute @code{Source_Files} is not given an explicit value,
13403 but the attribute @code{Source_List_File} is given a string value,
13404 then the source file names are contained in the text file whose path name
13405 (absolute or relative to the directory of the project file) is the
13406 value of the attribute @code{Source_List_File}.
13407
13408 Each line in the file that is not empty or is not a comment
13409 contains a source file name.
13410
13411 @smallexample @c projectfile
13412 for Source_List_File use "source_list.txt";
13413 @end smallexample
13414
13415 @noindent
13416 By default, if neither the attribute @code{Source_Files} nor the attribute
13417 @code{Source_List_File} is given an explicit value, then each file in the
13418 source directories that conforms to the project's naming scheme
13419 (@pxref{Naming Schemes}) is an immediate source of the project.
13420
13421 A warning is issued if both attributes @code{Source_Files} and
13422 @code{Source_List_File} are given explicit values. In this case, the attribute
13423 @code{Source_Files} prevails.
13424
13425 Each source file name must be the name of one existing source file
13426 in one of the source directories.
13427
13428 A @code{Source_Files} attribute whose value is an empty list
13429 indicates that there are no source files in the project.
13430
13431 If the order of the source directories is known statically, that is if
13432 @code{"/**"} is not used in the string list @code{Source_Dirs}, then there may
13433 be several files with the same source file name. In this case, only the file
13434 in the first directory is considered as an immediate source of the project
13435 file. If the order of the source directories is not known statically, it is
13436 an error to have several files with the same source file name.
13437
13438 Projects can be specified to have no Ada source
13439 files: the value of @code{Source_Dirs} or @code{Source_Files} may be an empty
13440 list, or the @code{"Ada"} may be absent from @code{Languages}:
13441
13442 @smallexample @c projectfile
13443 for Source_Dirs use ();
13444 for Source_Files use ();
13445 for Languages use ("C", "C++");
13446 @end smallexample
13447
13448 @noindent
13449 Otherwise, a project must contain at least one immediate source.
13450
13451 Projects with no source files are useful as template packages
13452 (@pxref{Packages in Project Files}) for other projects; in particular to
13453 define a package @code{Naming} (@pxref{Naming Schemes}).
13454
13455 @c ****************************
13456 @c * Importing Projects *
13457 @c ****************************
13458
13459 @node Importing Projects
13460 @section Importing Projects
13461 @cindex @code{ADA_PROJECT_PATH}
13462 @cindex @code{GPR_PROJECT_PATH}
13463
13464 @noindent
13465 An immediate source of a project P may depend on source files that
13466 are neither immediate sources of P nor in the predefined library.
13467 To get this effect, P must @emph{import} the projects that contain the needed
13468 source files.
13469
13470 @smallexample @c projectfile
13471 @group
13472 with "project1", "utilities.gpr";
13473 with "/namings/apex.gpr";
13474 project Main is
13475 @dots{}
13476 @end group
13477 @end smallexample
13478
13479 @noindent
13480 As can be seen in this example, the syntax for importing projects is similar
13481 to the syntax for importing compilation units in Ada. However, project files
13482 use literal strings instead of names, and the @code{with} clause identifies
13483 project files rather than packages.
13484
13485 Each literal string is the file name or path name (absolute or relative) of a
13486 project file. If a string corresponds to a file name, with no path or a
13487 relative path, then its location is determined by the @emph{project path}. The
13488 latter can be queried using @code{gnatls -v}. It contains:
13489
13490 @itemize @bullet
13491 @item
13492 In first position, the directory containing the current project file.
13493 @item
13494 In last position, the default project directory. This default project directory
13495 is part of the GNAT installation and is the standard place to install project
13496 files giving access to standard support libraries.
13497 @ifclear vms
13498 @ref{Installing a library}
13499 @end ifclear
13500
13501 @item
13502 In between, all the directories referenced in the
13503 ^environment variables^logical names^ @env{GPR_PROJECT_PATH}
13504 and @env{ADA_PROJECT_PATH} if they exist, and in that order.
13505 @end itemize
13506
13507 @noindent
13508 If a relative pathname is used, as in
13509
13510 @smallexample @c projectfile
13511 with "tests/proj";
13512 @end smallexample
13513
13514 @noindent
13515 then the full path for the project is constructed by concatenating this
13516 relative path to those in the project path, in order, until a matching file is
13517 found. Any symbolic link will be fully resolved in the directory of the
13518 importing project file before the imported project file is examined.
13519
13520 If the @code{with}'ed project file name does not have an extension,
13521 the default is @file{^.gpr^.GPR^}. If a file with this extension is not found,
13522 then the file name as specified in the @code{with} clause (no extension) will
13523 be used. In the above example, if a file @code{project1.gpr} is found, then it
13524 will be used; otherwise, if a file @code{^project1^PROJECT1^} exists
13525 then it will be used; if neither file exists, this is an error.
13526
13527 A warning is issued if the name of the project file does not match the
13528 name of the project; this check is case insensitive.
13529
13530 Any source file that is an immediate source of the imported project can be
13531 used by the immediate sources of the importing project, transitively. Thus
13532 if @code{A} imports @code{B}, and @code{B} imports @code{C}, the immediate
13533 sources of @code{A} may depend on the immediate sources of @code{C}, even if
13534 @code{A} does not import @code{C} explicitly. However, this is not recommended,
13535 because if and when @code{B} ceases to import @code{C}, some sources in
13536 @code{A} will no longer compile.
13537
13538 A side effect of this capability is that normally cyclic dependencies are not
13539 permitted: if @code{A} imports @code{B} (directly or indirectly) then @code{B}
13540 is not allowed to import @code{A}. However, there are cases when cyclic
13541 dependencies would be beneficial. For these cases, another form of import
13542 between projects exists, the @code{limited with}: a project @code{A} that
13543 imports a project @code{B} with a straight @code{with} may also be imported,
13544 directly or indirectly, by @code{B} on the condition that imports from @code{B}
13545 to @code{A} include at least one @code{limited with}.
13546
13547 @smallexample @c 0projectfile
13548 with "../b/b.gpr";
13549 with "../c/c.gpr";
13550 project A is
13551 end A;
13552
13553 limited with "../a/a.gpr";
13554 project B is
13555 end B;
13556
13557 with "../d/d.gpr";
13558 project C is
13559 end C;
13560
13561 limited with "../a/a.gpr";
13562 project D is
13563 end D;
13564 @end smallexample
13565
13566 @noindent
13567 In the above legal example, there are two project cycles:
13568 @itemize @bullet
13569 @item A-> B-> A
13570 @item A -> C -> D -> A
13571 @end itemize
13572
13573 @noindent
13574 In each of these cycle there is one @code{limited with}: import of @code{A}
13575 from @code{B} and import of @code{A} from @code{D}.
13576
13577 The difference between straight @code{with} and @code{limited with} is that
13578 the name of a project imported with a @code{limited with} cannot be used in the
13579 project that imports it. In particular, its packages cannot be renamed and
13580 its variables cannot be referred to.
13581
13582 An exception to the above rules for @code{limited with} is that for the main
13583 project specified to @command{gnatmake} or to the @command{GNAT} driver a
13584 @code{limited with} is equivalent to a straight @code{with}. For example,
13585 in the example above, projects @code{B} and @code{D} could not be main
13586 projects for @command{gnatmake} or to the @command{GNAT} driver, because they
13587 each have a @code{limited with} that is the only one in a cycle of importing
13588 projects.
13589
13590 @c *********************
13591 @c * Project Extension *
13592 @c *********************
13593
13594 @node Project Extension
13595 @section Project Extension
13596
13597 @noindent
13598 During development of a large system, it is sometimes necessary to use
13599 modified versions of some of the source files, without changing the original
13600 sources. This can be achieved through the @emph{project extension} facility.
13601
13602 @smallexample @c projectfile
13603 project Modified_Utilities extends "/baseline/utilities.gpr" is @dots{}
13604 @end smallexample
13605
13606 @noindent
13607 A project extension declaration introduces an extending project
13608 (the @emph{child}) and a project being extended (the @emph{parent}).
13609
13610 By default, a child project inherits all the sources of its parent.
13611 However, inherited sources can be overridden: a unit in a parent is hidden
13612 by a unit of the same name in the child.
13613
13614 Inherited sources are considered to be sources (but not immediate sources)
13615 of the child project; see @ref{Project File Syntax}.
13616
13617 An inherited source file retains any switches specified in the parent project.
13618
13619 For example if the project @code{Utilities} contains the spec and the
13620 body of an Ada package @code{Util_IO}, then the project
13621 @code{Modified_Utilities} can contain a new body for package @code{Util_IO}.
13622 The original body of @code{Util_IO} will not be considered in program builds.
13623 However, the package spec will still be found in the project
13624 @code{Utilities}.
13625
13626 A child project can have only one parent, except when it is qualified as
13627 abstract. But it may import any number of other projects.
13628
13629 A project is not allowed to import directly or indirectly at the same time a
13630 child project and any of its ancestors.
13631
13632 @c *******************************
13633 @c * Project Hierarchy Extension *
13634 @c *******************************
13635
13636 @node Project Hierarchy Extension
13637 @section Project Hierarchy Extension
13638
13639 @noindent
13640 When extending a large system spanning multiple projects, it is often
13641 inconvenient to extend every project in the hierarchy that is impacted by a
13642 small change introduced. In such cases, it is possible to create a virtual
13643 extension of entire hierarchy using @code{extends all} relationship.
13644
13645 When the project is extended using @code{extends all} inheritance, all projects
13646 that are imported by it, both directly and indirectly, are considered virtually
13647 extended. That is, the Project Manager creates "virtual projects"
13648 that extend every project in the hierarchy; all these virtual projects have
13649 no sources of their own and have as object directory the object directory of
13650 the root of "extending all" project.
13651
13652 It is possible to explicitly extend one or more projects in the hierarchy
13653 in order to modify the sources. These extending projects must be imported by
13654 the "extending all" project, which will replace the corresponding virtual
13655 projects with the explicit ones.
13656
13657 When building such a project hierarchy extension, the Project Manager will
13658 ensure that both modified sources and sources in virtual extending projects
13659 that depend on them, are recompiled.
13660
13661 By means of example, consider the following hierarchy of projects.
13662
13663 @enumerate
13664 @item
13665 project A, containing package P1
13666 @item
13667 project B importing A and containing package P2 which depends on P1
13668 @item
13669 project C importing B and containing package P3 which depends on P2
13670 @end enumerate
13671
13672 @noindent
13673 We want to modify packages P1 and P3.
13674
13675 This project hierarchy will need to be extended as follows:
13676
13677 @enumerate
13678 @item
13679 Create project A1 that extends A, placing modified P1 there:
13680
13681 @smallexample @c 0projectfile
13682 project A1 extends "(@dots{})/A" is
13683 end A1;
13684 @end smallexample
13685
13686 @item
13687 Create project C1 that "extends all" C and imports A1, placing modified
13688 P3 there:
13689
13690 @smallexample @c 0projectfile
13691 with "(@dots{})/A1";
13692 project C1 extends all "(@dots{})/C" is
13693 end C1;
13694 @end smallexample
13695 @end enumerate
13696
13697 When you build project C1, your entire modified project space will be
13698 recompiled, including the virtual project B1 that has been impacted by the
13699 "extending all" inheritance of project C.
13700
13701 Note that if a Library Project in the hierarchy is virtually extended,
13702 the virtual project that extends the Library Project is not a Library Project.
13703
13704 @c ****************************************
13705 @c * External References in Project Files *
13706 @c ****************************************
13707
13708 @node External References in Project Files
13709 @section External References in Project Files
13710
13711 @noindent
13712 A project file may contain references to external variables; such references
13713 are called @emph{external references}.
13714
13715 An external variable is either defined as part of the environment (an
13716 environment variable in Unix, for example) or else specified on the command
13717 line via the @option{^-X^/EXTERNAL_REFERENCE=^@emph{vbl}=@emph{value}} switch.
13718 If both, then the command line value is used.
13719
13720 The value of an external reference is obtained by means of the built-in
13721 function @code{external}, which returns a string value.
13722 This function has two forms:
13723 @itemize @bullet
13724 @item @code{external (external_variable_name)}
13725 @item @code{external (external_variable_name, default_value)}
13726 @end itemize
13727
13728 @noindent
13729 Each parameter must be a string literal. For example:
13730
13731 @smallexample @c projectfile
13732 external ("USER")
13733 external ("OS", "GNU/Linux")
13734 @end smallexample
13735
13736 @noindent
13737 In the form with one parameter, the function returns the value of
13738 the external variable given as parameter. If this name is not present in the
13739 environment, the function returns an empty string.
13740
13741 In the form with two string parameters, the second argument is
13742 the value returned when the variable given as the first argument is not
13743 present in the environment. In the example above, if @code{"OS"} is not
13744 the name of ^an environment variable^a logical name^ and is not passed on
13745 the command line, then the returned value is @code{"GNU/Linux"}.
13746
13747 An external reference may be part of a string expression or of a string
13748 list expression, and can therefore appear in a variable declaration or
13749 an attribute declaration.
13750
13751 @smallexample @c projectfile
13752 @group
13753 type Mode_Type is ("Debug", "Release");
13754 Mode : Mode_Type := external ("MODE");
13755 case Mode is
13756 when "Debug" =>
13757 @dots{}
13758 @end group
13759 @end smallexample
13760
13761 @c *****************************
13762 @c * Packages in Project Files *
13763 @c *****************************
13764
13765 @node Packages in Project Files
13766 @section Packages in Project Files
13767
13768 @noindent
13769 A @emph{package} defines the settings for project-aware tools within a
13770 project.
13771 For each such tool one can declare a package; the names for these
13772 packages are preset (@pxref{Packages}).
13773 A package may contain variable declarations, attribute declarations, and case
13774 constructions.
13775
13776 @smallexample @c projectfile
13777 @group
13778 project Proj is
13779 package Builder is -- used by gnatmake
13780 for ^Default_Switches^Default_Switches^ ("Ada")
13781 use ("^-v^-v^",
13782 "^-g^-g^");
13783 end Builder;
13784 end Proj;
13785 @end group
13786 @end smallexample
13787
13788 @noindent
13789 The syntax of package declarations mimics that of package in Ada.
13790
13791 Most of the packages have an attribute
13792 @code{^Default_Switches^Default_Switches^}.
13793 This attribute is an associative array, and its value is a string list.
13794 The index of the associative array is the name of a programming language (case
13795 insensitive). This attribute indicates the ^switch^switch^
13796 or ^switches^switches^ to be used
13797 with the corresponding tool.
13798
13799 Some packages also have another attribute, @code{^Switches^Switches^},
13800 an associative array whose value is a string list.
13801 The index is the name of a source file.
13802 This attribute indicates the ^switch^switch^
13803 or ^switches^switches^ to be used by the corresponding
13804 tool when dealing with this specific file.
13805
13806 Further information on these ^switch^switch^-related attributes is found in
13807 @ref{^Switches^Switches^ and Project Files}.
13808
13809 A package may be declared as a @emph{renaming} of another package; e.g., from
13810 the project file for an imported project.
13811
13812 @smallexample @c projectfile
13813 @group
13814 with "/global/apex.gpr";
13815 project Example is
13816 package Naming renames Apex.Naming;
13817 @dots{}
13818 end Example;
13819 @end group
13820 @end smallexample
13821
13822 @noindent
13823 Packages that are renamed in other project files often come from project files
13824 that have no sources: they are just used as templates. Any modification in the
13825 template will be reflected automatically in all the project files that rename
13826 a package from the template.
13827
13828 In addition to the tool-oriented packages, you can also declare a package
13829 named @code{Naming} to establish specialized source file naming conventions
13830 (@pxref{Naming Schemes}).
13831
13832 @c ************************************
13833 @c * Variables from Imported Projects *
13834 @c ************************************
13835
13836 @node Variables from Imported Projects
13837 @section Variables from Imported Projects
13838
13839 @noindent
13840 An attribute or variable defined in an imported or parent project can
13841 be used in expressions in the importing / extending project.
13842 Such an attribute or variable is denoted by an expanded name whose prefix
13843 is either the name of the project or the expanded name of a package within
13844 a project.
13845
13846 @smallexample @c projectfile
13847 @group
13848 with "imported";
13849 project Main extends "base" is
13850 Var1 := Imported.Var;
13851 Var2 := Base.Var & ".new";
13852 @end group
13853
13854 @group
13855 package Builder is
13856 for ^Default_Switches^Default_Switches^ ("Ada")
13857 use Imported.Builder'Ada_^Switches^Switches^ &
13858 "^-gnatg^-gnatg^" &
13859 "^-v^-v^";
13860 end Builder;
13861 @end group
13862
13863 @group
13864 package Compiler is
13865 for ^Default_Switches^Default_Switches^ ("Ada")
13866 use Base.Compiler'Ada_^Switches^Switches^;
13867 end Compiler;
13868 end Main;
13869 @end group
13870 @end smallexample
13871
13872 @noindent
13873 In this example:
13874
13875 @itemize @bullet
13876 @item
13877 The value of @code{Var1} is a copy of the variable @code{Var} defined
13878 in the project file @file{"imported.gpr"}
13879 @item
13880 the value of @code{Var2} is a copy of the value of variable @code{Var}
13881 defined in the project file @file{base.gpr}, concatenated with @code{".new"}
13882 @item
13883 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13884 @code{Builder} is a string list that includes in its value a copy of the value
13885 of @code{Ada_^Switches^Switches^} defined in the @code{Builder} package
13886 in project file @file{imported.gpr} plus two new elements:
13887 @option{"^-gnatg^-gnatg^"}
13888 and @option{"^-v^-v^"};
13889 @item
13890 attribute @code{^Default_Switches^Default_Switches^ ("Ada")} in package
13891 @code{Compiler} is a copy of the variable @code{Ada_^Switches^Switches^}
13892 defined in the @code{Compiler} package in project file @file{base.gpr},
13893 the project being extended.
13894 @end itemize
13895
13896 @c ******************
13897 @c * Naming Schemes *
13898 @c ******************
13899
13900 @node Naming Schemes
13901 @section Naming Schemes
13902
13903 @noindent
13904 Sometimes an Ada software system is ported from a foreign compilation
13905 environment to GNAT, and the file names do not use the default GNAT
13906 conventions. Instead of changing all the file names (which for a variety
13907 of reasons might not be possible), you can define the relevant file
13908 naming scheme in the @code{Naming} package in your project file.
13909
13910 @noindent
13911 Note that the use of pragmas described in
13912 @ref{Alternative File Naming Schemes} by mean of a configuration
13913 pragmas file is not supported when using project files. You must use
13914 the features described in this paragraph. You can however use specify
13915 other configuration pragmas (@pxref{Specifying Configuration Pragmas}).
13916
13917 @ifclear vms
13918 For example, the following
13919 package models the Apex file naming rules:
13920
13921 @smallexample @c projectfile
13922 @group
13923 package Naming is
13924 for Casing use "lowercase";
13925 for Dot_Replacement use ".";
13926 for Spec_Suffix ("Ada") use ".1.ada";
13927 for Body_Suffix ("Ada") use ".2.ada";
13928 end Naming;
13929 @end group
13930 @end smallexample
13931 @end ifclear
13932
13933 @ifset vms
13934 For example, the following package models the HP Ada file naming rules:
13935
13936 @smallexample @c projectfile
13937 @group
13938 package Naming is
13939 for Casing use "lowercase";
13940 for Dot_Replacement use "__";
13941 for Spec_Suffix ("Ada") use "_.^ada^ada^";
13942 for Body_Suffix ("Ada") use ".^ada^ada^";
13943 end Naming;
13944 @end group
13945 @end smallexample
13946
13947 @noindent
13948 (Note that @code{Casing} is @code{"lowercase"} because GNAT gets the file
13949 names in lower case)
13950 @end ifset
13951
13952 @noindent
13953 You can define the following attributes in package @code{Naming}:
13954
13955 @table @code
13956
13957 @item @code{Casing}
13958 This must be a string with one of the three values @code{"lowercase"},
13959 @code{"uppercase"} or @code{"mixedcase"}; these strings are case insensitive.
13960
13961 @noindent
13962 If @code{Casing} is not specified, then the default is @code{"lowercase"}.
13963
13964 @item @code{Dot_Replacement}
13965 This must be a string whose value satisfies the following conditions:
13966
13967 @itemize @bullet
13968 @item It must not be empty
13969 @item It cannot start or end with an alphanumeric character
13970 @item It cannot be a single underscore
13971 @item It cannot start with an underscore followed by an alphanumeric
13972 @item It cannot contain a dot @code{'.'} except if the entire string
13973 is @code{"."}
13974 @end itemize
13975
13976 @noindent
13977 If @code{Dot_Replacement} is not specified, then the default is @code{"-"}.
13978
13979 @item @code{Spec_Suffix}
13980 This is an associative array (indexed by the programming language name, case
13981 insensitive) whose value is a string that must satisfy the following
13982 conditions:
13983
13984 @itemize @bullet
13985 @item It must not be empty
13986 @item It must include at least one dot
13987 @end itemize
13988 @noindent
13989 If @code{Spec_Suffix ("Ada")} is not specified, then the default is
13990 @code{"^.ads^.ADS^"}.
13991
13992 @item @code{Body_Suffix}
13993 This is an associative array (indexed by the programming language name, case
13994 insensitive) whose value is a string that must satisfy the following
13995 conditions:
13996
13997 @itemize @bullet
13998 @item It must not be empty
13999 @item It must include at least one dot
14000 @item It cannot be the same as @code{Spec_Suffix ("Ada")}
14001 @end itemize
14002 @noindent
14003 If @code{Body_Suffix ("Ada")} and @code{Spec_Suffix ("Ada")} end with the
14004 same string, then a file name that ends with the longest of these two suffixes
14005 will be a body if the longest suffix is @code{Body_Suffix ("Ada")} or a spec
14006 if the longest suffix is @code{Spec_Suffix ("Ada")}.
14007
14008 If the suffix does not start with a '.', a file with a name exactly equal
14009 to the suffix will also be part of the project (for instance if you define
14010 the suffix as @code{Makefile}, a file called @file{Makefile} will be part
14011 of the project. This is not interesting in general when using projects to
14012 compile. However, it might become useful when a project is also used to
14013 find the list of source files in an editor, like the GNAT Programming System
14014 (GPS).
14015
14016 If @code{Body_Suffix ("Ada")} is not specified, then the default is
14017 @code{"^.adb^.ADB^"}.
14018
14019 @item @code{Separate_Suffix}
14020 This must be a string whose value satisfies the same conditions as
14021 @code{Body_Suffix}. The same "longest suffix" rules apply.
14022
14023 @noindent
14024 If @code{Separate_Suffix ("Ada")} is not specified, then it defaults to same
14025 value as @code{Body_Suffix ("Ada")}.
14026
14027 @item @code{Spec}
14028 @noindent
14029 You can use the associative array attribute @code{Spec} to define
14030 the source file name for an individual Ada compilation unit's spec. The array
14031 index must be a string literal that identifies the Ada unit (case insensitive).
14032 The value of this attribute must be a string that identifies the file that
14033 contains this unit's spec (case sensitive or insensitive depending on the
14034 operating system).
14035
14036 @smallexample @c projectfile
14037 for Spec ("MyPack.MyChild") use "mypack.mychild.spec";
14038 @end smallexample
14039
14040 When the source file contains several units, you can indicate at what
14041 position the unit occurs in the file, with the following. The first unit
14042 in the file has index 1
14043
14044 @smallexample @c projectfile
14045 for Body ("top") use "foo.a" at 1;
14046 for Body ("foo") use "foo.a" at 2;
14047 @end smallexample
14048
14049 @item @code{Body}
14050
14051 You can use the associative array attribute @code{Body} to
14052 define the source file name for an individual Ada compilation unit's body
14053 (possibly a subunit). The array index must be a string literal that identifies
14054 the Ada unit (case insensitive). The value of this attribute must be a string
14055 that identifies the file that contains this unit's body or subunit (case
14056 sensitive or insensitive depending on the operating system).
14057
14058 @smallexample @c projectfile
14059 for Body ("MyPack.MyChild") use "mypack.mychild.body";
14060 @end smallexample
14061 @end table
14062
14063 @c ********************
14064 @c * Library Projects *
14065 @c ********************
14066
14067 @node Library Projects
14068 @section Library Projects
14069
14070 @noindent
14071 @emph{Library projects} are projects whose object code is placed in a library.
14072 (Note that this facility is not yet supported on all platforms).
14073
14074 @code{gnatmake} or @code{gprbuild} will collect all object files into a
14075 single archive, which might either be a shared or a static library. This
14076 library can later on be linked with multiple executables, potentially
14077 reducing their sizes.
14078
14079 If your project file specifies languages other than Ada, but you are still
14080 using @code{gnatmake} to compile and link, the latter will not try to
14081 compile your sources other than Ada (you should use @code{gprbuild} if that
14082 is your intent). However, @code{gnatmake} will automatically link all object
14083 files found in the object directory, whether or not they were compiled from
14084 an Ada source file. This specific behavior only applies when multiple
14085 languages are specified.
14086
14087 To create a library project, you need to define in its project file
14088 two project-level attributes: @code{Library_Name} and @code{Library_Dir}.
14089 Additionally, you may define other library-related attributes such as
14090 @code{Library_Kind}, @code{Library_Version}, @code{Library_Interface},
14091 @code{Library_Auto_Init}, @code{Library_Options} and @code{Library_GCC}.
14092
14093 The @code{Library_Name} attribute has a string value. There is no restriction
14094 on the name of a library. It is the responsibility of the developer to
14095 choose a name that will be accepted by the platform. It is recommended to
14096 choose names that could be Ada identifiers; such names are almost guaranteed
14097 to be acceptable on all platforms.
14098
14099 The @code{Library_Dir} attribute has a string value that designates the path
14100 (absolute or relative) of the directory where the library will reside.
14101 It must designate an existing directory, and this directory must be writable,
14102 different from the project's object directory and from any source directory
14103 in the project tree.
14104
14105 If both @code{Library_Name} and @code{Library_Dir} are specified and
14106 are legal, then the project file defines a library project. The optional
14107 library-related attributes are checked only for such project files.
14108
14109 The @code{Library_Kind} attribute has a string value that must be one of the
14110 following (case insensitive): @code{"static"}, @code{"dynamic"} or
14111 @code{"relocatable"} (which is a synonym for @code{"dynamic"}). If this
14112 attribute is not specified, the library is a static library, that is
14113 an archive of object files that can be potentially linked into a
14114 static executable. Otherwise, the library may be dynamic or
14115 relocatable, that is a library that is loaded only at the start of execution.
14116
14117 If you need to build both a static and a dynamic library, you should use two
14118 different object directories, since in some cases some extra code needs to
14119 be generated for the latter. For such cases, it is recommended to either use
14120 two different project files, or a single one which uses external variables
14121 to indicate what kind of library should be build.
14122
14123 The @code{Library_ALI_Dir} attribute may be specified to indicate the
14124 directory where the ALI files of the library will be copied. When it is
14125 not specified, the ALI files are copied to the directory specified in
14126 attribute @code{Library_Dir}. The directory specified by @code{Library_ALI_Dir}
14127 must be writable and different from the project's object directory and from
14128 any source directory in the project tree.
14129
14130 The @code{Library_Version} attribute has a string value whose interpretation
14131 is platform dependent. It has no effect on VMS and Windows. On Unix, it is
14132 used only for dynamic/relocatable libraries as the internal name of the
14133 library (the @code{"soname"}). If the library file name (built from the
14134 @code{Library_Name}) is different from the @code{Library_Version}, then the
14135 library file will be a symbolic link to the actual file whose name will be
14136 @code{Library_Version}.
14137
14138 Example (on Unix):
14139
14140 @smallexample @c projectfile
14141 @group
14142 project Plib is
14143
14144 Version := "1";
14145
14146 for Library_Dir use "lib_dir";
14147 for Library_Name use "dummy";
14148 for Library_Kind use "relocatable";
14149 for Library_Version use "libdummy.so." & Version;
14150
14151 end Plib;
14152 @end group
14153 @end smallexample
14154
14155 @noindent
14156 Directory @file{lib_dir} will contain the internal library file whose name
14157 will be @file{libdummy.so.1}, and @file{libdummy.so} will be a symbolic link to
14158 @file{libdummy.so.1}.
14159
14160 When @command{gnatmake} detects that a project file
14161 is a library project file, it will check all immediate sources of the project
14162 and rebuild the library if any of the sources have been recompiled.
14163
14164 Standard project files can import library project files. In such cases,
14165 the libraries will only be rebuilt if some of its sources are recompiled
14166 because they are in the closure of some other source in an importing project.
14167 Sources of the library project files that are not in such a closure will
14168 not be checked, unless the full library is checked, because one of its sources
14169 needs to be recompiled.
14170
14171 For instance, assume the project file @code{A} imports the library project file
14172 @code{L}. The immediate sources of A are @file{a1.adb}, @file{a2.ads} and
14173 @file{a2.adb}. The immediate sources of L are @file{l1.ads}, @file{l1.adb},
14174 @file{l2.ads}, @file{l2.adb}.
14175
14176 If @file{l1.adb} has been modified, then the library associated with @code{L}
14177 will be rebuilt when compiling all the immediate sources of @code{A} only
14178 if @file{a1.ads}, @file{a2.ads} or @file{a2.adb} includes a statement
14179 @code{"with L1;"}.
14180
14181 To be sure that all the sources in the library associated with @code{L} are
14182 up to date, and that all the sources of project @code{A} are also up to date,
14183 the following two commands needs to be used:
14184
14185 @smallexample
14186 gnatmake -Pl.gpr
14187 gnatmake -Pa.gpr
14188 @end smallexample
14189
14190 When a library is built or rebuilt, an attempt is made first to delete all
14191 files in the library directory.
14192 All @file{ALI} files will also be copied from the object directory to the
14193 library directory. To build executables, @command{gnatmake} will use the
14194 library rather than the individual object files.
14195
14196 @ifclear vms
14197 It is also possible to create library project files for third-party libraries
14198 that are precompiled and cannot be compiled locally thanks to the
14199 @code{externally_built} attribute. (See @ref{Installing a library}).
14200 @end ifclear
14201
14202 @c *******************************
14203 @c * Stand-alone Library Projects *
14204 @c *******************************
14205
14206 @node Stand-alone Library Projects
14207 @section Stand-alone Library Projects
14208
14209 @noindent
14210 A Stand-alone Library is a library that contains the necessary code to
14211 elaborate the Ada units that are included in the library. A Stand-alone
14212 Library is suitable to be used in an executable when the main is not
14213 in Ada. However, Stand-alone Libraries may also be used with an Ada main
14214 subprogram.
14215
14216 A Stand-alone Library Project is a Library Project where the library is
14217 a Stand-alone Library.
14218
14219 To be a Stand-alone Library Project, in addition to the two attributes
14220 that make a project a Library Project (@code{Library_Name} and
14221 @code{Library_Dir}, see @ref{Library Projects}), the attribute
14222 @code{Library_Interface} must be defined.
14223
14224 @smallexample @c projectfile
14225 @group
14226 for Library_Dir use "lib_dir";
14227 for Library_Name use "dummy";
14228 for Library_Interface use ("int1", "int1.child");
14229 @end group
14230 @end smallexample
14231
14232 Attribute @code{Library_Interface} has a nonempty string list value,
14233 each string in the list designating a unit contained in an immediate source
14234 of the project file.
14235
14236 When a Stand-alone Library is built, first the binder is invoked to build
14237 a package whose name depends on the library name
14238 (^b~dummy.ads/b^B$DUMMY.ADS/B^ in the example above).
14239 This binder-generated package includes initialization and
14240 finalization procedures whose
14241 names depend on the library name (dummyinit and dummyfinal in the example
14242 above). The object corresponding to this package is included in the library.
14243
14244 A dynamic or relocatable Stand-alone Library is automatically initialized
14245 if automatic initialization of Stand-alone Libraries is supported on the
14246 platform and if attribute @code{Library_Auto_Init} is not specified or
14247 is specified with the value "true". A static Stand-alone Library is never
14248 automatically initialized.
14249
14250 Single string attribute @code{Library_Auto_Init} may be specified with only
14251 two possible values: "false" or "true" (case-insensitive). Specifying
14252 "false" for attribute @code{Library_Auto_Init} will prevent automatic
14253 initialization of dynamic or relocatable libraries.
14254
14255 When a non-automatically initialized Stand-alone Library is used
14256 in an executable, its initialization procedure must be called before
14257 any service of the library is used.
14258 When the main subprogram is in Ada, it may mean that the initialization
14259 procedure has to be called during elaboration of another package.
14260
14261 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
14262 (those that are listed in attribute @code{Library_Interface}) are copied to
14263 the Library Directory. As a consequence, only the Interface Units may be
14264 imported from Ada units outside of the library. If other units are imported,
14265 the binding phase will fail.
14266
14267 When a Stand-Alone Library is bound, the switches that are specified in
14268 the attribute @code{Default_Switches ("Ada")} in package @code{Binder} are
14269 used in the call to @command{gnatbind}.
14270
14271 The string list attribute @code{Library_Options} may be used to specified
14272 additional switches to the call to @command{gcc} to link the library.
14273
14274 The attribute @code{Library_Src_Dir}, may be specified for a
14275 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
14276 single string value. Its value must be the path (absolute or relative to the
14277 project directory) of an existing directory. This directory cannot be the
14278 object directory or one of the source directories, but it can be the same as
14279 the library directory. The sources of the Interface
14280 Units of the library, necessary to an Ada client of the library, will be
14281 copied to the designated directory, called Interface Copy directory.
14282 These sources includes the specs of the Interface Units, but they may also
14283 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
14284 are used, or when there is a generic units in the spec. Before the sources
14285 are copied to the Interface Copy directory, an attempt is made to delete all
14286 files in the Interface Copy directory.
14287
14288 @c *************************************
14289 @c * Switches Related to Project Files *
14290 @c *************************************
14291 @node Switches Related to Project Files
14292 @section Switches Related to Project Files
14293
14294 @noindent
14295 The following switches are used by GNAT tools that support project files:
14296
14297 @table @option
14298
14299 @item ^-P^/PROJECT_FILE=^@var{project}
14300 @cindex @option{^-P^/PROJECT_FILE^} (any project-aware tool)
14301 Indicates the name of a project file. This project file will be parsed with
14302 the verbosity indicated by @option{^-vP^MESSAGE_PROJECT_FILES=^@emph{x}},
14303 if any, and using the external references indicated
14304 by @option{^-X^/EXTERNAL_REFERENCE^} switches, if any.
14305 @ifclear vms
14306 There may zero, one or more spaces between @option{-P} and @var{project}.
14307 @end ifclear
14308
14309 @noindent
14310 There must be only one @option{^-P^/PROJECT_FILE^} switch on the command line.
14311
14312 @noindent
14313 Since the Project Manager parses the project file only after all the switches
14314 on the command line are checked, the order of the switches
14315 @option{^-P^/PROJECT_FILE^},
14316 @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}}
14317 or @option{^-X^/EXTERNAL_REFERENCE^} is not significant.
14318
14319 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
14320 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (any project-aware tool)
14321 Indicates that external variable @var{name} has the value @var{value}.
14322 The Project Manager will use this value for occurrences of
14323 @code{external(name)} when parsing the project file.
14324
14325 @ifclear vms
14326 @noindent
14327 If @var{name} or @var{value} includes a space, then @var{name=value} should be
14328 put between quotes.
14329 @smallexample
14330 -XOS=NT
14331 -X"user=John Doe"
14332 @end smallexample
14333 @end ifclear
14334
14335 @noindent
14336 Several @option{^-X^/EXTERNAL_REFERENCE^} switches can be used simultaneously.
14337 If several @option{^-X^/EXTERNAL_REFERENCE^} switches specify the same
14338 @var{name}, only the last one is used.
14339
14340 @noindent
14341 An external variable specified with a @option{^-X^/EXTERNAL_REFERENCE^} switch
14342 takes precedence over the value of the same name in the environment.
14343
14344 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
14345 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (any project-aware tool)
14346 Indicates the verbosity of the parsing of GNAT project files.
14347
14348 @ifclear vms
14349 @option{-vP0} means Default;
14350 @option{-vP1} means Medium;
14351 @option{-vP2} means High.
14352 @end ifclear
14353
14354 @ifset vms
14355 There are three possible options for this qualifier: DEFAULT, MEDIUM and
14356 HIGH.
14357 @end ifset
14358
14359 @noindent
14360 The default is ^Default^DEFAULT^: no output for syntactically correct
14361 project files.
14362 @noindent
14363 If several @option{^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}} switches are present,
14364 only the last one is used.
14365
14366 @item ^-aP^/ADD_PROJECT_SEARCH_DIR=^<dir>
14367 @cindex @option{^-aP^/ADD_PROJECT_SEARCH_DIR=^} (any project-aware tool)
14368 Add directory <dir> at the beginning of the project search path, in order,
14369 after the current working directory.
14370
14371 @ifclear vms
14372 @item -eL
14373 @cindex @option{-eL} (any project-aware tool)
14374 Follow all symbolic links when processing project files.
14375 @end ifclear
14376
14377 @item ^--subdirs^/SUBDIRS^=<subdir>
14378 @cindex @option{^--subdirs^/SUBDIRS^=} (gnatmake and gnatclean)
14379 This switch is recognized by gnatmake and gnatclean. It indicate that the real
14380 directories (except the source directories) are the subdirectories <subdir>
14381 of the directories specified in the project files. This applies in particular
14382 to object directories, library directories and exec directories. If the
14383 subdirectories do not exist, they are created automatically.
14384
14385 @end table
14386
14387 @c **********************************
14388 @c * Tools Supporting Project Files *
14389 @c **********************************
14390
14391 @node Tools Supporting Project Files
14392 @section Tools Supporting Project Files
14393
14394 @menu
14395 * gnatmake and Project Files::
14396 * The GNAT Driver and Project Files::
14397 @end menu
14398
14399 @node gnatmake and Project Files
14400 @subsection gnatmake and Project Files
14401
14402 @noindent
14403 This section covers several topics related to @command{gnatmake} and
14404 project files: defining ^switches^switches^ for @command{gnatmake}
14405 and for the tools that it invokes; specifying configuration pragmas;
14406 the use of the @code{Main} attribute; building and rebuilding library project
14407 files.
14408
14409 @menu
14410 * ^Switches^Switches^ and Project Files::
14411 * Specifying Configuration Pragmas::
14412 * Project Files and Main Subprograms::
14413 * Library Project Files::
14414 @end menu
14415
14416 @node ^Switches^Switches^ and Project Files
14417 @subsubsection ^Switches^Switches^ and Project Files
14418
14419 @ifset vms
14420 It is not currently possible to specify VMS style qualifiers in the project
14421 files; only Unix style ^switches^switches^ may be specified.
14422 @end ifset
14423
14424 @noindent
14425 For each of the packages @code{Builder}, @code{Compiler}, @code{Binder}, and
14426 @code{Linker}, you can specify a @code{^Default_Switches^Default_Switches^}
14427 attribute, a @code{^Switches^Switches^} attribute, or both;
14428 as their names imply, these ^switch^switch^-related
14429 attributes affect the ^switches^switches^ that are used for each of these GNAT
14430 components when
14431 @command{gnatmake} is invoked. As will be explained below, these
14432 component-specific ^switches^switches^ precede
14433 the ^switches^switches^ provided on the @command{gnatmake} command line.
14434
14435 The @code{^Default_Switches^Default_Switches^} attribute is an associative
14436 array indexed by language name (case insensitive) whose value is a string list.
14437 For example:
14438
14439 @smallexample @c projectfile
14440 @group
14441 package Compiler is
14442 for ^Default_Switches^Default_Switches^ ("Ada")
14443 use ("^-gnaty^-gnaty^",
14444 "^-v^-v^");
14445 end Compiler;
14446 @end group
14447 @end smallexample
14448
14449 @noindent
14450 The @code{^Switches^Switches^} attribute is also an associative array,
14451 indexed by a file name (which may or may not be case sensitive, depending
14452 on the operating system) whose value is a string list. For example:
14453
14454 @smallexample @c projectfile
14455 @group
14456 package Builder is
14457 for ^Switches^Switches^ ("main1.adb")
14458 use ("^-O2^-O2^");
14459 for ^Switches^Switches^ ("main2.adb")
14460 use ("^-g^-g^");
14461 end Builder;
14462 @end group
14463 @end smallexample
14464
14465 @noindent
14466 For the @code{Builder} package, the file names must designate source files
14467 for main subprograms. For the @code{Binder} and @code{Linker} packages, the
14468 file names must designate @file{ALI} or source files for main subprograms.
14469 In each case just the file name without an explicit extension is acceptable.
14470
14471 For each tool used in a program build (@command{gnatmake}, the compiler, the
14472 binder, and the linker), the corresponding package @dfn{contributes} a set of
14473 ^switches^switches^ for each file on which the tool is invoked, based on the
14474 ^switch^switch^-related attributes defined in the package.
14475 In particular, the ^switches^switches^
14476 that each of these packages contributes for a given file @var{f} comprise:
14477
14478 @itemize @bullet
14479 @item
14480 the value of attribute @code{^Switches^Switches^ (@var{f})},
14481 if it is specified in the package for the given file,
14482 @item
14483 otherwise, the value of @code{^Default_Switches^Default_Switches^ ("Ada")},
14484 if it is specified in the package.
14485 @end itemize
14486
14487 @noindent
14488 If neither of these attributes is defined in the package, then the package does
14489 not contribute any ^switches^switches^ for the given file.
14490
14491 When @command{gnatmake} is invoked on a file, the ^switches^switches^ comprise
14492 two sets, in the following order: those contributed for the file
14493 by the @code{Builder} package;
14494 and the switches passed on the command line.
14495
14496 When @command{gnatmake} invokes a tool (compiler, binder, linker) on a file,
14497 the ^switches^switches^ passed to the tool comprise three sets,
14498 in the following order:
14499
14500 @enumerate
14501 @item
14502 the applicable ^switches^switches^ contributed for the file
14503 by the @code{Builder} package in the project file supplied on the command line;
14504
14505 @item
14506 those contributed for the file by the package (in the relevant project file --
14507 see below) corresponding to the tool; and
14508
14509 @item
14510 the applicable switches passed on the command line.
14511 @end enumerate
14512
14513 @noindent
14514 The term @emph{applicable ^switches^switches^} reflects the fact that
14515 @command{gnatmake} ^switches^switches^ may or may not be passed to individual
14516 tools, depending on the individual ^switch^switch^.
14517
14518 @command{gnatmake} may invoke the compiler on source files from different
14519 projects. The Project Manager will use the appropriate project file to
14520 determine the @code{Compiler} package for each source file being compiled.
14521 Likewise for the @code{Binder} and @code{Linker} packages.
14522
14523 As an example, consider the following package in a project file:
14524
14525 @smallexample @c projectfile
14526 @group
14527 project Proj1 is
14528 package Compiler is
14529 for ^Default_Switches^Default_Switches^ ("Ada")
14530 use ("^-g^-g^");
14531 for ^Switches^Switches^ ("a.adb")
14532 use ("^-O1^-O1^");
14533 for ^Switches^Switches^ ("b.adb")
14534 use ("^-O2^-O2^",
14535 "^-gnaty^-gnaty^");
14536 end Compiler;
14537 end Proj1;
14538 @end group
14539 @end smallexample
14540
14541 @noindent
14542 If @command{gnatmake} is invoked with this project file, and it needs to
14543 compile, say, the files @file{a.adb}, @file{b.adb}, and @file{c.adb}, then
14544 @file{a.adb} will be compiled with the ^switch^switch^
14545 @option{^-O1^-O1^},
14546 @file{b.adb} with ^switches^switches^
14547 @option{^-O2^-O2^}
14548 and @option{^-gnaty^-gnaty^},
14549 and @file{c.adb} with @option{^-g^-g^}.
14550
14551 The following example illustrates the ordering of the ^switches^switches^
14552 contributed by different packages:
14553
14554 @smallexample @c projectfile
14555 @group
14556 project Proj2 is
14557 package Builder is
14558 for ^Switches^Switches^ ("main.adb")
14559 use ("^-g^-g^",
14560 "^-O1^-)1^",
14561 "^-f^-f^");
14562 end Builder;
14563 @end group
14564
14565 @group
14566 package Compiler is
14567 for ^Switches^Switches^ ("main.adb")
14568 use ("^-O2^-O2^");
14569 end Compiler;
14570 end Proj2;
14571 @end group
14572 @end smallexample
14573
14574 @noindent
14575 If you issue the command:
14576
14577 @smallexample
14578 gnatmake ^-Pproj2^/PROJECT_FILE=PROJ2^ -O0 main
14579 @end smallexample
14580
14581 @noindent
14582 then the compiler will be invoked on @file{main.adb} with the following
14583 sequence of ^switches^switches^
14584
14585 @smallexample
14586 ^-g -O1 -O2 -O0^-g -O1 -O2 -O0^
14587 @end smallexample
14588
14589 with the last @option{^-O^-O^}
14590 ^switch^switch^ having precedence over the earlier ones;
14591 several other ^switches^switches^
14592 (such as @option{^-c^-c^}) are added implicitly.
14593
14594 The ^switches^switches^
14595 @option{^-g^-g^}
14596 and @option{^-O1^-O1^} are contributed by package
14597 @code{Builder}, @option{^-O2^-O2^} is contributed
14598 by the package @code{Compiler}
14599 and @option{^-O0^-O0^} comes from the command line.
14600
14601 The @option{^-g^-g^}
14602 ^switch^switch^ will also be passed in the invocation of
14603 @command{Gnatlink.}
14604
14605 A final example illustrates switch contributions from packages in different
14606 project files:
14607
14608 @smallexample @c projectfile
14609 @group
14610 project Proj3 is
14611 for Source_Files use ("pack.ads", "pack.adb");
14612 package Compiler is
14613 for ^Default_Switches^Default_Switches^ ("Ada")
14614 use ("^-gnata^-gnata^");
14615 end Compiler;
14616 end Proj3;
14617 @end group
14618
14619 @group
14620 with "Proj3";
14621 project Proj4 is
14622 for Source_Files use ("foo_main.adb", "bar_main.adb");
14623 package Builder is
14624 for ^Switches^Switches^ ("foo_main.adb")
14625 use ("^-s^-s^",
14626 "^-g^-g^");
14627 end Builder;
14628 end Proj4;
14629 @end group
14630
14631 @group
14632 -- Ada source file:
14633 with Pack;
14634 procedure Foo_Main is
14635 @dots{}
14636 end Foo_Main;
14637 @end group
14638 @end smallexample
14639
14640 If the command is
14641 @smallexample
14642 gnatmake ^-PProj4^/PROJECT_FILE=PROJ4^ foo_main.adb -cargs -gnato
14643 @end smallexample
14644
14645 @noindent
14646 then the ^switches^switches^ passed to the compiler for @file{foo_main.adb} are
14647 @option{^-g^-g^} (contributed by the package @code{Proj4.Builder}) and
14648 @option{^-gnato^-gnato^} (passed on the command line).
14649 When the imported package @code{Pack} is compiled, the ^switches^switches^ used
14650 are @option{^-g^-g^} from @code{Proj4.Builder},
14651 @option{^-gnata^-gnata^} (contributed from package @code{Proj3.Compiler},
14652 and @option{^-gnato^-gnato^} from the command line.
14653
14654 @noindent
14655 When using @command{gnatmake} with project files, some ^switches^switches^ or
14656 arguments may be expressed as relative paths. As the working directory where
14657 compilation occurs may change, these relative paths are converted to absolute
14658 paths. For the ^switches^switches^ found in a project file, the relative paths
14659 are relative to the project file directory, for the switches on the command
14660 line, they are relative to the directory where @command{gnatmake} is invoked.
14661 The ^switches^switches^ for which this occurs are:
14662 ^-I^-I^,
14663 ^-A^-A^,
14664 ^-L^-L^,
14665 ^-aO^-aO^,
14666 ^-aL^-aL^,
14667 ^-aI^-aI^, as well as all arguments that are not switches (arguments to
14668 ^switch^switch^
14669 ^-o^-o^, object files specified in package @code{Linker} or after
14670 -largs on the command line). The exception to this rule is the ^switch^switch^
14671 ^--RTS=^--RTS=^ for which a relative path argument is never converted.
14672
14673 @node Specifying Configuration Pragmas
14674 @subsubsection Specifying Configuration Pragmas
14675
14676 When using @command{gnatmake} with project files, if there exists a file
14677 @file{gnat.adc} that contains configuration pragmas, this file will be
14678 ignored.
14679
14680 Configuration pragmas can be defined by means of the following attributes in
14681 project files: @code{Global_Configuration_Pragmas} in package @code{Builder}
14682 and @code{Local_Configuration_Pragmas} in package @code{Compiler}.
14683
14684 Both these attributes are single string attributes. Their values is the path
14685 name of a file containing configuration pragmas. If a path name is relative,
14686 then it is relative to the project directory of the project file where the
14687 attribute is defined.
14688
14689 When compiling a source, the configuration pragmas used are, in order,
14690 those listed in the file designated by attribute
14691 @code{Global_Configuration_Pragmas} in package @code{Builder} of the main
14692 project file, if it is specified, and those listed in the file designated by
14693 attribute @code{Local_Configuration_Pragmas} in package @code{Compiler} of
14694 the project file of the source, if it exists.
14695
14696 @node Project Files and Main Subprograms
14697 @subsubsection Project Files and Main Subprograms
14698
14699 @noindent
14700 When using a project file, you can invoke @command{gnatmake}
14701 with one or several main subprograms, by specifying their source files on the
14702 command line.
14703
14704 @smallexample
14705 gnatmake ^-P^/PROJECT_FILE=^prj main1 main2 main3
14706 @end smallexample
14707
14708 @noindent
14709 Each of these needs to be a source file of the same project, except
14710 when the switch ^-u^/UNIQUE^ is used.
14711
14712 @noindent
14713 When ^-u^/UNIQUE^ is not used, all the mains need to be sources of the
14714 same project, one of the project in the tree rooted at the project specified
14715 on the command line. The package @code{Builder} of this common project, the
14716 "main project" is the one that is considered by @command{gnatmake}.
14717
14718 @noindent
14719 When ^-u^/UNIQUE^ is used, the specified source files may be in projects
14720 imported directly or indirectly by the project specified on the command line.
14721 Note that if such a source file is not part of the project specified on the
14722 command line, the ^switches^switches^ found in package @code{Builder} of the
14723 project specified on the command line, if any, that are transmitted
14724 to the compiler will still be used, not those found in the project file of
14725 the source file.
14726
14727 @noindent
14728 When using a project file, you can also invoke @command{gnatmake} without
14729 explicitly specifying any main, and the effect depends on whether you have
14730 defined the @code{Main} attribute. This attribute has a string list value,
14731 where each element in the list is the name of a source file (the file
14732 extension is optional) that contains a unit that can be a main subprogram.
14733
14734 If the @code{Main} attribute is defined in a project file as a non-empty
14735 string list and the switch @option{^-u^/UNIQUE^} is not used on the command
14736 line, then invoking @command{gnatmake} with this project file but without any
14737 main on the command line is equivalent to invoking @command{gnatmake} with all
14738 the file names in the @code{Main} attribute on the command line.
14739
14740 Example:
14741 @smallexample @c projectfile
14742 @group
14743 project Prj is
14744 for Main use ("main1", "main2", "main3");
14745 end Prj;
14746 @end group
14747 @end smallexample
14748
14749 @noindent
14750 With this project file, @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^"}
14751 is equivalent to
14752 @code{"gnatmake ^-Pprj^/PROJECT_FILE=PRJ^ main1 main2 main3"}.
14753
14754 When the project attribute @code{Main} is not specified, or is specified
14755 as an empty string list, or when the switch @option{-u} is used on the command
14756 line, then invoking @command{gnatmake} with no main on the command line will
14757 result in all immediate sources of the project file being checked, and
14758 potentially recompiled. Depending on the presence of the switch @option{-u},
14759 sources from other project files on which the immediate sources of the main
14760 project file depend are also checked and potentially recompiled. In other
14761 words, the @option{-u} switch is applied to all of the immediate sources of the
14762 main project file.
14763
14764 When no main is specified on the command line and attribute @code{Main} exists
14765 and includes several mains, or when several mains are specified on the
14766 command line, the default ^switches^switches^ in package @code{Builder} will
14767 be used for all mains, even if there are specific ^switches^switches^
14768 specified for one or several mains.
14769
14770 But the ^switches^switches^ from package @code{Binder} or @code{Linker} will be
14771 the specific ^switches^switches^ for each main, if they are specified.
14772
14773 @node Library Project Files
14774 @subsubsection Library Project Files
14775
14776 @noindent
14777 When @command{gnatmake} is invoked with a main project file that is a library
14778 project file, it is not allowed to specify one or more mains on the command
14779 line.
14780
14781 @noindent
14782 When a library project file is specified, switches ^-b^/ACTION=BIND^ and
14783 ^-l^/ACTION=LINK^ have special meanings.
14784
14785 @itemize @bullet
14786 @item ^-b^/ACTION=BIND^ is only allowed for stand-alone libraries. It indicates
14787 to @command{gnatmake} that @command{gnatbind} should be invoked for the
14788 library.
14789
14790 @item ^-l^/ACTION=LINK^ may be used for all library projects. It indicates
14791 to @command{gnatmake} that the binder generated file should be compiled
14792 (in the case of a stand-alone library) and that the library should be built.
14793
14794 @end itemize
14795
14796 @node The GNAT Driver and Project Files
14797 @subsection The GNAT Driver and Project Files
14798
14799 @noindent
14800 A number of GNAT tools, other than @command{^gnatmake^gnatmake^}
14801 can benefit from project files:
14802 @command{^gnatbind^gnatbind^},
14803 @command{^gnatcheck^gnatcheck^}),
14804 @command{^gnatclean^gnatclean^}),
14805 @command{^gnatelim^gnatelim^},
14806 @command{^gnatfind^gnatfind^},
14807 @command{^gnatlink^gnatlink^},
14808 @command{^gnatls^gnatls^},
14809 @command{^gnatmetric^gnatmetric^},
14810 @command{^gnatpp^gnatpp^},
14811 @command{^gnatstub^gnatstub^},
14812 and @command{^gnatxref^gnatxref^}. However, none of these tools can be invoked
14813 directly with a project file switch (@option{^-P^/PROJECT_FILE=^}).
14814 They must be invoked through the @command{gnat} driver.
14815
14816 The @command{gnat} driver is a wrapper that accepts a number of commands and
14817 calls the corresponding tool. It was designed initially for VMS platforms (to
14818 convert VMS qualifiers to Unix-style switches), but it is now available on all
14819 GNAT platforms.
14820
14821 On non-VMS platforms, the @command{gnat} driver accepts the following commands
14822 (case insensitive):
14823
14824 @itemize @bullet
14825 @item
14826 BIND to invoke @command{^gnatbind^gnatbind^}
14827 @item
14828 CHOP to invoke @command{^gnatchop^gnatchop^}
14829 @item
14830 CLEAN to invoke @command{^gnatclean^gnatclean^}
14831 @item
14832 COMP or COMPILE to invoke the compiler
14833 @item
14834 ELIM to invoke @command{^gnatelim^gnatelim^}
14835 @item
14836 FIND to invoke @command{^gnatfind^gnatfind^}
14837 @item
14838 KR or KRUNCH to invoke @command{^gnatkr^gnatkr^}
14839 @item
14840 LINK to invoke @command{^gnatlink^gnatlink^}
14841 @item
14842 LS or LIST to invoke @command{^gnatls^gnatls^}
14843 @item
14844 MAKE to invoke @command{^gnatmake^gnatmake^}
14845 @item
14846 NAME to invoke @command{^gnatname^gnatname^}
14847 @item
14848 PREP or PREPROCESS to invoke @command{^gnatprep^gnatprep^}
14849 @item
14850 PP or PRETTY to invoke @command{^gnatpp^gnatpp^}
14851 @item
14852 METRIC to invoke @command{^gnatmetric^gnatmetric^}
14853 @item
14854 STUB to invoke @command{^gnatstub^gnatstub^}
14855 @item
14856 XREF to invoke @command{^gnatxref^gnatxref^}
14857 @end itemize
14858
14859 @noindent
14860 (note that the compiler is invoked using the command
14861 @command{^gnatmake -f -u -c^gnatmake -f -u -c^}).
14862
14863 @noindent
14864 On non-VMS platforms, between @command{gnat} and the command, two
14865 special switches may be used:
14866
14867 @itemize @bullet
14868 @item
14869 @command{-v} to display the invocation of the tool.
14870 @item
14871 @command{-dn} to prevent the @command{gnat} driver from removing
14872 the temporary files it has created. These temporary files are
14873 configuration files and temporary file list files.
14874 @end itemize
14875
14876 @noindent
14877 The command may be followed by switches and arguments for the invoked
14878 tool.
14879
14880 @smallexample
14881 gnat bind -C main.ali
14882 gnat ls -a main
14883 gnat chop foo.txt
14884 @end smallexample
14885
14886 @noindent
14887 Switches may also be put in text files, one switch per line, and the text
14888 files may be specified with their path name preceded by '@@'.
14889
14890 @smallexample
14891 gnat bind @@args.txt main.ali
14892 @end smallexample
14893
14894 @noindent
14895 In addition, for commands BIND, COMP or COMPILE, FIND, ELIM, LS or LIST, LINK,
14896 METRIC, PP or PRETTY, STUB and XREF, the project file related switches
14897 (@option{^-P^/PROJECT_FILE^},
14898 @option{^-X^/EXTERNAL_REFERENCE^} and
14899 @option{^-vP^/MESSAGES_PROJECT_FILE=^x}) may be used in addition to
14900 the switches of the invoking tool.
14901
14902 @noindent
14903 When GNAT PP or GNAT PRETTY is used with a project file, but with no source
14904 specified on the command line, it invokes @command{^gnatpp^gnatpp^} with all
14905 the immediate sources of the specified project file.
14906
14907 @noindent
14908 When GNAT METRIC is used with a project file, but with no source
14909 specified on the command line, it invokes @command{^gnatmetric^gnatmetric^}
14910 with all the immediate sources of the specified project file and with
14911 @option{^-d^/DIRECTORY^} with the parameter pointing to the object directory
14912 of the project.
14913
14914 @noindent
14915 In addition, when GNAT PP, GNAT PRETTY or GNAT METRIC is used with
14916 a project file, no source is specified on the command line and
14917 switch ^-U^/ALL_PROJECTS^ is specified on the command line, then
14918 the underlying tool (^gnatpp^gnatpp^ or
14919 ^gnatmetric^gnatmetric^) is invoked for all sources of all projects,
14920 not only for the immediate sources of the main project.
14921 @ifclear vms
14922 (-U stands for Universal or Union of the project files of the project tree)
14923 @end ifclear
14924
14925 @noindent
14926 For each of the following commands, there is optionally a corresponding
14927 package in the main project.
14928
14929 @itemize @bullet
14930 @item
14931 package @code{Binder} for command BIND (invoking @code{^gnatbind^gnatbind^})
14932
14933 @item
14934 package @code{Check} for command CHECK (invoking
14935 @code{^gnatcheck^gnatcheck^})
14936
14937 @item
14938 package @code{Compiler} for command COMP or COMPILE (invoking the compiler)
14939
14940 @item
14941 package @code{Cross_Reference} for command XREF (invoking
14942 @code{^gnatxref^gnatxref^})
14943
14944 @item
14945 package @code{Eliminate} for command ELIM (invoking
14946 @code{^gnatelim^gnatelim^})
14947
14948 @item
14949 package @code{Finder} for command FIND (invoking @code{^gnatfind^gnatfind^})
14950
14951 @item
14952 package @code{Gnatls} for command LS or LIST (invoking @code{^gnatls^gnatls^})
14953
14954 @item
14955 package @code{Gnatstub} for command STUB
14956 (invoking @code{^gnatstub^gnatstub^})
14957
14958 @item
14959 package @code{Linker} for command LINK (invoking @code{^gnatlink^gnatlink^})
14960
14961 @item
14962 package @code{Check} for command CHECK
14963 (invoking @code{^gnatcheck^gnatcheck^})
14964
14965 @item
14966 package @code{Metrics} for command METRIC
14967 (invoking @code{^gnatmetric^gnatmetric^})
14968
14969 @item
14970 package @code{Pretty_Printer} for command PP or PRETTY
14971 (invoking @code{^gnatpp^gnatpp^})
14972
14973 @end itemize
14974
14975 @noindent
14976 Package @code{Gnatls} has a unique attribute @code{^Switches^Switches^},
14977 a simple variable with a string list value. It contains ^switches^switches^
14978 for the invocation of @code{^gnatls^gnatls^}.
14979
14980 @smallexample @c projectfile
14981 @group
14982 project Proj1 is
14983 package gnatls is
14984 for ^Switches^Switches^
14985 use ("^-a^-a^",
14986 "^-v^-v^");
14987 end gnatls;
14988 end Proj1;
14989 @end group
14990 @end smallexample
14991
14992 @noindent
14993 All other packages have two attribute @code{^Switches^Switches^} and
14994 @code{^Default_Switches^Default_Switches^}.
14995
14996 @noindent
14997 @code{^Switches^Switches^} is an associative array attribute, indexed by the
14998 source file name, that has a string list value: the ^switches^switches^ to be
14999 used when the tool corresponding to the package is invoked for the specific
15000 source file.
15001
15002 @noindent
15003 @code{^Default_Switches^Default_Switches^} is an associative array attribute,
15004 indexed by the programming language that has a string list value.
15005 @code{^Default_Switches^Default_Switches^ ("Ada")} contains the
15006 ^switches^switches^ for the invocation of the tool corresponding
15007 to the package, except if a specific @code{^Switches^Switches^} attribute
15008 is specified for the source file.
15009
15010 @smallexample @c projectfile
15011 @group
15012 project Proj is
15013
15014 for Source_Dirs use ("./**");
15015
15016 package gnatls is
15017 for ^Switches^Switches^ use
15018 ("^-a^-a^",
15019 "^-v^-v^");
15020 end gnatls;
15021 @end group
15022 @group
15023
15024 package Compiler is
15025 for ^Default_Switches^Default_Switches^ ("Ada")
15026 use ("^-gnatv^-gnatv^",
15027 "^-gnatwa^-gnatwa^");
15028 end Binder;
15029 @end group
15030 @group
15031
15032 package Binder is
15033 for ^Default_Switches^Default_Switches^ ("Ada")
15034 use ("^-C^-C^",
15035 "^-e^-e^");
15036 end Binder;
15037 @end group
15038 @group
15039
15040 package Linker is
15041 for ^Default_Switches^Default_Switches^ ("Ada")
15042 use ("^-C^-C^");
15043 for ^Switches^Switches^ ("main.adb")
15044 use ("^-C^-C^",
15045 "^-v^-v^",
15046 "^-v^-v^");
15047 end Linker;
15048 @end group
15049 @group
15050
15051 package Finder is
15052 for ^Default_Switches^Default_Switches^ ("Ada")
15053 use ("^-a^-a^",
15054 "^-f^-f^");
15055 end Finder;
15056 @end group
15057 @group
15058
15059 package Cross_Reference is
15060 for ^Default_Switches^Default_Switches^ ("Ada")
15061 use ("^-a^-a^",
15062 "^-f^-f^",
15063 "^-d^-d^",
15064 "^-u^-u^");
15065 end Cross_Reference;
15066 end Proj;
15067 @end group
15068 @end smallexample
15069
15070 @noindent
15071 With the above project file, commands such as
15072
15073 @smallexample
15074 ^gnat comp -Pproj main^GNAT COMP /PROJECT_FILE=PROJ MAIN^
15075 ^gnat ls -Pproj main^GNAT LIST /PROJECT_FILE=PROJ MAIN^
15076 ^gnat xref -Pproj main^GNAT XREF /PROJECT_FILE=PROJ MAIN^
15077 ^gnat bind -Pproj main.ali^GNAT BIND /PROJECT_FILE=PROJ MAIN.ALI^
15078 ^gnat link -Pproj main.ali^GNAT LINK /PROJECT_FILE=PROJ MAIN.ALI^
15079 @end smallexample
15080
15081 @noindent
15082 will set up the environment properly and invoke the tool with the switches
15083 found in the package corresponding to the tool:
15084 @code{^Default_Switches^Default_Switches^ ("Ada")} for all tools,
15085 except @code{^Switches^Switches^ ("main.adb")}
15086 for @code{^gnatlink^gnatlink^}.
15087 It is also possible to invoke some of the tools,
15088 @code{^gnatcheck^gnatcheck^}),
15089 @code{^gnatmetric^gnatmetric^}),
15090 and @code{^gnatpp^gnatpp^})
15091 on a set of project units thanks to the combination of the switches
15092 @option{-P}, @option{-U} and possibly the main unit when one is interested
15093 in its closure. For instance,
15094 @smallexample
15095 gnat metric -Pproj
15096 @end smallexample
15097 will compute the metrics for all the immediate units of project
15098 @code{proj}.
15099 @smallexample
15100 gnat metric -Pproj -U
15101 @end smallexample
15102 will compute the metrics for all the units of the closure of projects
15103 rooted at @code{proj}.
15104 @smallexample
15105 gnat metric -Pproj -U main_unit
15106 @end smallexample
15107 will compute the metrics for the closure of units rooted at
15108 @code{main_unit}. This last possibility relies implicitly
15109 on @command{gnatbind}'s option @option{-R}.
15110
15111 @c **********************
15112 @node An Extended Example
15113 @section An Extended Example
15114
15115 @noindent
15116 Suppose that we have two programs, @var{prog1} and @var{prog2},
15117 whose sources are in corresponding directories. We would like
15118 to build them with a single @command{gnatmake} command, and we want to place
15119 their object files into @file{build} subdirectories of the source directories.
15120 Furthermore, we want to have to have two separate subdirectories
15121 in @file{build} -- @file{release} and @file{debug} -- which will contain
15122 the object files compiled with different set of compilation flags.
15123
15124 In other words, we have the following structure:
15125
15126 @smallexample
15127 @group
15128 main
15129 |- prog1
15130 | |- build
15131 | | debug
15132 | | release
15133 |- prog2
15134 |- build
15135 | debug
15136 | release
15137 @end group
15138 @end smallexample
15139
15140 @noindent
15141 Here are the project files that we must place in a directory @file{main}
15142 to maintain this structure:
15143
15144 @enumerate
15145
15146 @item We create a @code{Common} project with a package @code{Compiler} that
15147 specifies the compilation ^switches^switches^:
15148
15149 @smallexample
15150 File "common.gpr":
15151 @group
15152 @b{project} Common @b{is}
15153
15154 @b{for} Source_Dirs @b{use} (); -- No source files
15155 @end group
15156
15157 @group
15158 @b{type} Build_Type @b{is} ("release", "debug");
15159 Build : Build_Type := External ("BUILD", "debug");
15160 @end group
15161 @group
15162 @b{package} Compiler @b{is}
15163 @b{case} Build @b{is}
15164 @b{when} "release" =>
15165 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
15166 @b{use} ("^-O2^-O2^");
15167 @b{when} "debug" =>
15168 @b{for} ^Default_Switches^Default_Switches^ ("Ada")
15169 @b{use} ("^-g^-g^");
15170 @b{end case};
15171 @b{end} Compiler;
15172
15173 @b{end} Common;
15174 @end group
15175 @end smallexample
15176
15177 @item We create separate projects for the two programs:
15178
15179 @smallexample
15180 @group
15181 File "prog1.gpr":
15182
15183 @b{with} "common";
15184 @b{project} Prog1 @b{is}
15185
15186 @b{for} Source_Dirs @b{use} ("prog1");
15187 @b{for} Object_Dir @b{use} "prog1/build/" & Common.Build;
15188
15189 @b{package} Compiler @b{renames} Common.Compiler;
15190
15191 @b{end} Prog1;
15192 @end group
15193 @end smallexample
15194
15195 @smallexample
15196 @group
15197 File "prog2.gpr":
15198
15199 @b{with} "common";
15200 @b{project} Prog2 @b{is}
15201
15202 @b{for} Source_Dirs @b{use} ("prog2");
15203 @b{for} Object_Dir @b{use} "prog2/build/" & Common.Build;
15204
15205 @b{package} Compiler @b{renames} Common.Compiler;
15206
15207 @end group
15208 @b{end} Prog2;
15209 @end smallexample
15210
15211 @item We create a wrapping project @code{Main}:
15212
15213 @smallexample
15214 @group
15215 File "main.gpr":
15216
15217 @b{with} "common";
15218 @b{with} "prog1";
15219 @b{with} "prog2";
15220 @b{project} Main @b{is}
15221
15222 @b{package} Compiler @b{renames} Common.Compiler;
15223
15224 @b{end} Main;
15225 @end group
15226 @end smallexample
15227
15228 @item Finally we need to create a dummy procedure that @code{with}s (either
15229 explicitly or implicitly) all the sources of our two programs.
15230
15231 @end enumerate
15232
15233 @noindent
15234 Now we can build the programs using the command
15235
15236 @smallexample
15237 gnatmake ^-P^/PROJECT_FILE=^main dummy
15238 @end smallexample
15239
15240 @noindent
15241 for the Debug mode, or
15242
15243 @ifclear vms
15244 @smallexample
15245 gnatmake -Pmain -XBUILD=release
15246 @end smallexample
15247 @end ifclear
15248
15249 @ifset vms
15250 @smallexample
15251 GNAT MAKE /PROJECT_FILE=main /EXTERNAL_REFERENCE=BUILD=release
15252 @end smallexample
15253 @end ifset
15254
15255 @noindent
15256 for the Release mode.
15257
15258 @c ********************************
15259 @c * Project File Complete Syntax *
15260 @c ********************************
15261
15262 @node Project File Complete Syntax
15263 @section Project File Complete Syntax
15264
15265 @smallexample
15266 project ::=
15267 context_clause project_declaration
15268
15269 context_clause ::=
15270 @{with_clause@}
15271
15272 with_clause ::=
15273 @b{with} path_name @{ , path_name @} ;
15274
15275 path_name ::=
15276 string_literal
15277
15278 project_declaration ::=
15279 simple_project_declaration | project_extension
15280
15281 simple_project_declaration ::=
15282 @b{project} <project_>simple_name @b{is}
15283 @{declarative_item@}
15284 @b{end} <project_>simple_name;
15285
15286 project_extension ::=
15287 @b{project} <project_>simple_name @b{extends} path_name @b{is}
15288 @{declarative_item@}
15289 @b{end} <project_>simple_name;
15290
15291 declarative_item ::=
15292 package_declaration |
15293 typed_string_declaration |
15294 other_declarative_item
15295
15296 package_declaration ::=
15297 package_spec | package_renaming
15298
15299 package_spec ::=
15300 @b{package} package_identifier @b{is}
15301 @{simple_declarative_item@}
15302 @b{end} package_identifier ;
15303
15304 package_identifier ::=
15305 @code{Naming} | @code{Builder} | @code{Compiler} | @code{Binder} |
15306 @code{Linker} | @code{Finder} | @code{Cross_Reference} |
15307 @code{^gnatls^gnatls^} | @code{IDE} | @code{Pretty_Printer}
15308
15309 package_renaming ::==
15310 @b{package} package_identifier @b{renames}
15311 <project_>simple_name.package_identifier ;
15312
15313 typed_string_declaration ::=
15314 @b{type} <typed_string_>_simple_name @b{is}
15315 ( string_literal @{, string_literal@} );
15316
15317 other_declarative_item ::=
15318 attribute_declaration |
15319 typed_variable_declaration |
15320 variable_declaration |
15321 case_construction
15322
15323 attribute_declaration ::=
15324 full_associative_array_declaration |
15325 @b{for} attribute_designator @b{use} expression ;
15326
15327 full_associative_array_declaration ::=
15328 @b{for} <associative_array_attribute_>simple_name @b{use}
15329 <project_>simple_name [ . <package_>simple_Name ] ' <attribute_>simple_name ;
15330
15331 attribute_designator ::=
15332 <simple_attribute_>simple_name |
15333 <associative_array_attribute_>simple_name ( string_literal )
15334
15335 typed_variable_declaration ::=
15336 <typed_variable_>simple_name : <typed_string_>name := string_expression ;
15337
15338 variable_declaration ::=
15339 <variable_>simple_name := expression;
15340
15341 expression ::=
15342 term @{& term@}
15343
15344 term ::=
15345 literal_string |
15346 string_list |
15347 <variable_>name |
15348 external_value |
15349 attribute_reference
15350
15351 string_literal ::=
15352 (same as Ada)
15353
15354 string_list ::=
15355 ( <string_>expression @{ , <string_>expression @} )
15356
15357 external_value ::=
15358 @b{external} ( string_literal [, string_literal] )
15359
15360 attribute_reference ::=
15361 attribute_prefix ' <simple_attribute_>simple_name [ ( literal_string ) ]
15362
15363 attribute_prefix ::=
15364 @b{project} |
15365 <project_>simple_name | package_identifier |
15366 <project_>simple_name . package_identifier
15367
15368 case_construction ::=
15369 @b{case} <typed_variable_>name @b{is}
15370 @{case_item@}
15371 @b{end case} ;
15372
15373 case_item ::=
15374 @b{when} discrete_choice_list =>
15375 @{case_construction | attribute_declaration@}
15376
15377 discrete_choice_list ::=
15378 string_literal @{| string_literal@} |
15379 @b{others}
15380
15381 name ::=
15382 simple_name @{. simple_name@}
15383
15384 simple_name ::=
15385 identifier (same as Ada)
15386
15387 @end smallexample
15388
15389 @node The Cross-Referencing Tools gnatxref and gnatfind
15390 @chapter The Cross-Referencing Tools @code{gnatxref} and @code{gnatfind}
15391 @findex gnatxref
15392 @findex gnatfind
15393
15394 @noindent
15395 The compiler generates cross-referencing information (unless
15396 you set the @samp{-gnatx} switch), which are saved in the @file{.ali} files.
15397 This information indicates where in the source each entity is declared and
15398 referenced. Note that entities in package Standard are not included, but
15399 entities in all other predefined units are included in the output.
15400
15401 Before using any of these two tools, you need to compile successfully your
15402 application, so that GNAT gets a chance to generate the cross-referencing
15403 information.
15404
15405 The two tools @code{gnatxref} and @code{gnatfind} take advantage of this
15406 information to provide the user with the capability to easily locate the
15407 declaration and references to an entity. These tools are quite similar,
15408 the difference being that @code{gnatfind} is intended for locating
15409 definitions and/or references to a specified entity or entities, whereas
15410 @code{gnatxref} is oriented to generating a full report of all
15411 cross-references.
15412
15413 To use these tools, you must not compile your application using the
15414 @option{-gnatx} switch on the @command{gnatmake} command line
15415 (@pxref{The GNAT Make Program gnatmake}). Otherwise, cross-referencing
15416 information will not be generated.
15417
15418 Note: to invoke @code{gnatxref} or @code{gnatfind} with a project file,
15419 use the @code{gnat} driver (see @ref{The GNAT Driver and Project Files}).
15420
15421 @menu
15422 * Switches for gnatxref::
15423 * Switches for gnatfind::
15424 * Project Files for gnatxref and gnatfind::
15425 * Regular Expressions in gnatfind and gnatxref::
15426 * Examples of gnatxref Usage::
15427 * Examples of gnatfind Usage::
15428 @end menu
15429
15430 @node Switches for gnatxref
15431 @section @code{gnatxref} Switches
15432
15433 @noindent
15434 The command invocation for @code{gnatxref} is:
15435 @smallexample
15436 @c $ gnatxref @ovar{switches} @var{sourcefile1} @r{[}@var{sourcefile2} @dots{}@r{]}
15437 @c Expanding @ovar macro inline (explanation in macro def comments)
15438 $ gnatxref @r{[}@var{switches}@r{]} @var{sourcefile1} @r{[}@var{sourcefile2} @dots{}@r{]}
15439 @end smallexample
15440
15441 @noindent
15442 where
15443
15444 @table @var
15445 @item sourcefile1
15446 @itemx sourcefile2
15447 identifies the source files for which a report is to be generated. The
15448 ``with''ed units will be processed too. You must provide at least one file.
15449
15450 These file names are considered to be regular expressions, so for instance
15451 specifying @file{source*.adb} is the same as giving every file in the current
15452 directory whose name starts with @file{source} and whose extension is
15453 @file{adb}.
15454
15455 You shouldn't specify any directory name, just base names. @command{gnatxref}
15456 and @command{gnatfind} will be able to locate these files by themselves using
15457 the source path. If you specify directories, no result is produced.
15458
15459 @end table
15460
15461 @noindent
15462 The switches can be:
15463 @table @option
15464 @c !sort!
15465 @item --version
15466 @cindex @option{--version} @command{gnatxref}
15467 Display Copyright and version, then exit disregarding all other options.
15468
15469 @item --help
15470 @cindex @option{--help} @command{gnatxref}
15471 If @option{--version} was not used, display usage, then exit disregarding
15472 all other options.
15473
15474 @item ^-a^/ALL_FILES^
15475 @cindex @option{^-a^/ALL_FILES^} (@command{gnatxref})
15476 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
15477 the read-only files found in the library search path. Otherwise, these files
15478 will be ignored. This option can be used to protect Gnat sources or your own
15479 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
15480 much faster, and their output much smaller. Read-only here refers to access
15481 or permissions status in the file system for the current user.
15482
15483 @item -aIDIR
15484 @cindex @option{-aIDIR} (@command{gnatxref})
15485 When looking for source files also look in directory DIR. The order in which
15486 source file search is undertaken is the same as for @command{gnatmake}.
15487
15488 @item -aODIR
15489 @cindex @option{-aODIR} (@command{gnatxref})
15490 When searching for library and object files, look in directory
15491 DIR. The order in which library files are searched is the same as for
15492 @command{gnatmake}.
15493
15494 @item -nostdinc
15495 @cindex @option{-nostdinc} (@command{gnatxref})
15496 Do not look for sources in the system default directory.
15497
15498 @item -nostdlib
15499 @cindex @option{-nostdlib} (@command{gnatxref})
15500 Do not look for library files in the system default directory.
15501
15502 @item --RTS=@var{rts-path}
15503 @cindex @option{--RTS} (@command{gnatxref})
15504 Specifies the default location of the runtime library. Same meaning as the
15505 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
15506
15507 @item ^-d^/DERIVED_TYPES^
15508 @cindex @option{^-d^/DERIVED_TYPES^} (@command{gnatxref})
15509 If this switch is set @code{gnatxref} will output the parent type
15510 reference for each matching derived types.
15511
15512 @item ^-f^/FULL_PATHNAME^
15513 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatxref})
15514 If this switch is set, the output file names will be preceded by their
15515 directory (if the file was found in the search path). If this switch is
15516 not set, the directory will not be printed.
15517
15518 @item ^-g^/IGNORE_LOCALS^
15519 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatxref})
15520 If this switch is set, information is output only for library-level
15521 entities, ignoring local entities. The use of this switch may accelerate
15522 @code{gnatfind} and @code{gnatxref}.
15523
15524 @item -IDIR
15525 @cindex @option{-IDIR} (@command{gnatxref})
15526 Equivalent to @samp{-aODIR -aIDIR}.
15527
15528 @item -pFILE
15529 @cindex @option{-pFILE} (@command{gnatxref})
15530 Specify a project file to use @xref{Project Files}.
15531 If you need to use the @file{.gpr}
15532 project files, you should use gnatxref through the GNAT driver
15533 (@command{gnat xref -Pproject}).
15534
15535 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15536 project file in the current directory.
15537
15538 If a project file is either specified or found by the tools, then the content
15539 of the source directory and object directory lines are added as if they
15540 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^}
15541 and @samp{^-aO^OBJECT_SEARCH^}.
15542 @item ^-u^/UNUSED^
15543 Output only unused symbols. This may be really useful if you give your
15544 main compilation unit on the command line, as @code{gnatxref} will then
15545 display every unused entity and 'with'ed package.
15546
15547 @ifclear vms
15548 @item -v
15549 Instead of producing the default output, @code{gnatxref} will generate a
15550 @file{tags} file that can be used by vi. For examples how to use this
15551 feature, see @ref{Examples of gnatxref Usage}. The tags file is output
15552 to the standard output, thus you will have to redirect it to a file.
15553 @end ifclear
15554
15555 @end table
15556
15557 @noindent
15558 All these switches may be in any order on the command line, and may even
15559 appear after the file names. They need not be separated by spaces, thus
15560 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15561 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15562
15563 @node Switches for gnatfind
15564 @section @code{gnatfind} Switches
15565
15566 @noindent
15567 The command line for @code{gnatfind} is:
15568
15569 @smallexample
15570 @c $ gnatfind @ovar{switches} @var{pattern}@r{[}:@var{sourcefile}@r{[}:@var{line}@r{[}:@var{column}@r{]]]}
15571 @c @r{[}@var{file1} @var{file2} @dots{}]
15572 @c Expanding @ovar macro inline (explanation in macro def comments)
15573 $ gnatfind @r{[}@var{switches}@r{]} @var{pattern}@r{[}:@var{sourcefile}@r{[}:@var{line}@r{[}:@var{column}@r{]]]}
15574 @r{[}@var{file1} @var{file2} @dots{}@r{]}
15575 @end smallexample
15576
15577 @noindent
15578 where
15579
15580 @table @var
15581 @item pattern
15582 An entity will be output only if it matches the regular expression found
15583 in @var{pattern}, see @ref{Regular Expressions in gnatfind and gnatxref}.
15584
15585 Omitting the pattern is equivalent to specifying @samp{*}, which
15586 will match any entity. Note that if you do not provide a pattern, you
15587 have to provide both a sourcefile and a line.
15588
15589 Entity names are given in Latin-1, with uppercase/lowercase equivalence
15590 for matching purposes. At the current time there is no support for
15591 8-bit codes other than Latin-1, or for wide characters in identifiers.
15592
15593 @item sourcefile
15594 @code{gnatfind} will look for references, bodies or declarations
15595 of symbols referenced in @file{@var{sourcefile}}, at line @var{line}
15596 and column @var{column}. See @ref{Examples of gnatfind Usage}
15597 for syntax examples.
15598
15599 @item line
15600 is a decimal integer identifying the line number containing
15601 the reference to the entity (or entities) to be located.
15602
15603 @item column
15604 is a decimal integer identifying the exact location on the
15605 line of the first character of the identifier for the
15606 entity reference. Columns are numbered from 1.
15607
15608 @item file1 file2 @dots{}
15609 The search will be restricted to these source files. If none are given, then
15610 the search will be done for every library file in the search path.
15611 These file must appear only after the pattern or sourcefile.
15612
15613 These file names are considered to be regular expressions, so for instance
15614 specifying @file{source*.adb} is the same as giving every file in the current
15615 directory whose name starts with @file{source} and whose extension is
15616 @file{adb}.
15617
15618 The location of the spec of the entity will always be displayed, even if it
15619 isn't in one of @file{@var{file1}}, @file{@var{file2}},@enddots{} The
15620 occurrences of the entity in the separate units of the ones given on the
15621 command line will also be displayed.
15622
15623 Note that if you specify at least one file in this part, @code{gnatfind} may
15624 sometimes not be able to find the body of the subprograms.
15625
15626 @end table
15627
15628 @noindent
15629 At least one of 'sourcefile' or 'pattern' has to be present on
15630 the command line.
15631
15632 The following switches are available:
15633 @table @option
15634 @c !sort!
15635
15636 @cindex @option{--version} @command{gnatfind}
15637 Display Copyright and version, then exit disregarding all other options.
15638
15639 @item --help
15640 @cindex @option{--help} @command{gnatfind}
15641 If @option{--version} was not used, display usage, then exit disregarding
15642 all other options.
15643
15644 @item ^-a^/ALL_FILES^
15645 @cindex @option{^-a^/ALL_FILES^} (@command{gnatfind})
15646 If this switch is present, @code{gnatfind} and @code{gnatxref} will parse
15647 the read-only files found in the library search path. Otherwise, these files
15648 will be ignored. This option can be used to protect Gnat sources or your own
15649 libraries from being parsed, thus making @code{gnatfind} and @code{gnatxref}
15650 much faster, and their output much smaller. Read-only here refers to access
15651 or permission status in the file system for the current user.
15652
15653 @item -aIDIR
15654 @cindex @option{-aIDIR} (@command{gnatfind})
15655 When looking for source files also look in directory DIR. The order in which
15656 source file search is undertaken is the same as for @command{gnatmake}.
15657
15658 @item -aODIR
15659 @cindex @option{-aODIR} (@command{gnatfind})
15660 When searching for library and object files, look in directory
15661 DIR. The order in which library files are searched is the same as for
15662 @command{gnatmake}.
15663
15664 @item -nostdinc
15665 @cindex @option{-nostdinc} (@command{gnatfind})
15666 Do not look for sources in the system default directory.
15667
15668 @item -nostdlib
15669 @cindex @option{-nostdlib} (@command{gnatfind})
15670 Do not look for library files in the system default directory.
15671
15672 @item --ext=@var{extension}
15673 @cindex @option{--ext} (@command{gnatfind})
15674 Specify an alternate ali file extension. The default is @code{ali} and other
15675 extensions (e.g. @code{sli} for SPARK library files) may be specified via this
15676 switch. Note that if this switch overrides the default, which means that only
15677 the new extension will be considered.
15678
15679 @item --RTS=@var{rts-path}
15680 @cindex @option{--RTS} (@command{gnatfind})
15681 Specifies the default location of the runtime library. Same meaning as the
15682 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
15683
15684 @item ^-d^/DERIVED_TYPE_INFORMATION^
15685 @cindex @option{^-d^/DERIVED_TYPE_INFORMATION^} (@code{gnatfind})
15686 If this switch is set, then @code{gnatfind} will output the parent type
15687 reference for each matching derived types.
15688
15689 @item ^-e^/EXPRESSIONS^
15690 @cindex @option{^-e^/EXPRESSIONS^} (@command{gnatfind})
15691 By default, @code{gnatfind} accept the simple regular expression set for
15692 @samp{pattern}. If this switch is set, then the pattern will be
15693 considered as full Unix-style regular expression.
15694
15695 @item ^-f^/FULL_PATHNAME^
15696 @cindex @option{^-f^/FULL_PATHNAME^} (@command{gnatfind})
15697 If this switch is set, the output file names will be preceded by their
15698 directory (if the file was found in the search path). If this switch is
15699 not set, the directory will not be printed.
15700
15701 @item ^-g^/IGNORE_LOCALS^
15702 @cindex @option{^-g^/IGNORE_LOCALS^} (@command{gnatfind})
15703 If this switch is set, information is output only for library-level
15704 entities, ignoring local entities. The use of this switch may accelerate
15705 @code{gnatfind} and @code{gnatxref}.
15706
15707 @item -IDIR
15708 @cindex @option{-IDIR} (@command{gnatfind})
15709 Equivalent to @samp{-aODIR -aIDIR}.
15710
15711 @item -pFILE
15712 @cindex @option{-pFILE} (@command{gnatfind})
15713 Specify a project file (@pxref{Project Files}) to use.
15714 By default, @code{gnatxref} and @code{gnatfind} will try to locate a
15715 project file in the current directory.
15716
15717 If a project file is either specified or found by the tools, then the content
15718 of the source directory and object directory lines are added as if they
15719 had been specified respectively by @samp{^-aI^/SOURCE_SEARCH^} and
15720 @samp{^-aO^/OBJECT_SEARCH^}.
15721
15722 @item ^-r^/REFERENCES^
15723 @cindex @option{^-r^/REFERENCES^} (@command{gnatfind})
15724 By default, @code{gnatfind} will output only the information about the
15725 declaration, body or type completion of the entities. If this switch is
15726 set, the @code{gnatfind} will locate every reference to the entities in
15727 the files specified on the command line (or in every file in the search
15728 path if no file is given on the command line).
15729
15730 @item ^-s^/PRINT_LINES^
15731 @cindex @option{^-s^/PRINT_LINES^} (@command{gnatfind})
15732 If this switch is set, then @code{gnatfind} will output the content
15733 of the Ada source file lines were the entity was found.
15734
15735 @item ^-t^/TYPE_HIERARCHY^
15736 @cindex @option{^-t^/TYPE_HIERARCHY^} (@command{gnatfind})
15737 If this switch is set, then @code{gnatfind} will output the type hierarchy for
15738 the specified type. It act like -d option but recursively from parent
15739 type to parent type. When this switch is set it is not possible to
15740 specify more than one file.
15741
15742 @end table
15743
15744 @noindent
15745 All these switches may be in any order on the command line, and may even
15746 appear after the file names. They need not be separated by spaces, thus
15747 you can say @samp{gnatxref ^-ag^/ALL_FILES/IGNORE_LOCALS^} instead of
15748 @samp{gnatxref ^-a -g^/ALL_FILES /IGNORE_LOCALS^}.
15749
15750 As stated previously, gnatfind will search in every directory in the
15751 search path. You can force it to look only in the current directory if
15752 you specify @code{*} at the end of the command line.
15753
15754 @node Project Files for gnatxref and gnatfind
15755 @section Project Files for @command{gnatxref} and @command{gnatfind}
15756
15757 @noindent
15758 Project files allow a programmer to specify how to compile its
15759 application, where to find sources, etc. These files are used
15760 @ifclear vms
15761 primarily by GPS, but they can also be used
15762 @end ifclear
15763 by the two tools
15764 @code{gnatxref} and @code{gnatfind}.
15765
15766 A project file name must end with @file{.gpr}. If a single one is
15767 present in the current directory, then @code{gnatxref} and @code{gnatfind} will
15768 extract the information from it. If multiple project files are found, none of
15769 them is read, and you have to use the @samp{-p} switch to specify the one
15770 you want to use.
15771
15772 The following lines can be included, even though most of them have default
15773 values which can be used in most cases.
15774 The lines can be entered in any order in the file.
15775 Except for @file{src_dir} and @file{obj_dir}, you can only have one instance of
15776 each line. If you have multiple instances, only the last one is taken into
15777 account.
15778
15779 @table @code
15780 @item src_dir=DIR
15781 [default: @code{"^./^[]^"}]
15782 specifies a directory where to look for source files. Multiple @code{src_dir}
15783 lines can be specified and they will be searched in the order they
15784 are specified.
15785
15786 @item obj_dir=DIR
15787 [default: @code{"^./^[]^"}]
15788 specifies a directory where to look for object and library files. Multiple
15789 @code{obj_dir} lines can be specified, and they will be searched in the order
15790 they are specified
15791
15792 @item comp_opt=SWITCHES
15793 [default: @code{""}]
15794 creates a variable which can be referred to subsequently by using
15795 the @code{$@{comp_opt@}} notation. This is intended to store the default
15796 switches given to @command{gnatmake} and @command{gcc}.
15797
15798 @item bind_opt=SWITCHES
15799 [default: @code{""}]
15800 creates a variable which can be referred to subsequently by using
15801 the @samp{$@{bind_opt@}} notation. This is intended to store the default
15802 switches given to @command{gnatbind}.
15803
15804 @item link_opt=SWITCHES
15805 [default: @code{""}]
15806 creates a variable which can be referred to subsequently by using
15807 the @samp{$@{link_opt@}} notation. This is intended to store the default
15808 switches given to @command{gnatlink}.
15809
15810 @item main=EXECUTABLE
15811 [default: @code{""}]
15812 specifies the name of the executable for the application. This variable can
15813 be referred to in the following lines by using the @samp{$@{main@}} notation.
15814
15815 @ifset vms
15816 @item comp_cmd=COMMAND
15817 [default: @code{"GNAT COMPILE /SEARCH=$@{src_dir@} /DEBUG /TRY_SEMANTICS"}]
15818 @end ifset
15819 @ifclear vms
15820 @item comp_cmd=COMMAND
15821 [default: @code{"gcc -c -I$@{src_dir@} -g -gnatq"}]
15822 @end ifclear
15823 specifies the command used to compile a single file in the application.
15824
15825 @ifset vms
15826 @item make_cmd=COMMAND
15827 [default: @code{"GNAT MAKE $@{main@}
15828 /SOURCE_SEARCH=$@{src_dir@} /OBJECT_SEARCH=$@{obj_dir@}
15829 /DEBUG /TRY_SEMANTICS /COMPILER_QUALIFIERS $@{comp_opt@}
15830 /BINDER_QUALIFIERS $@{bind_opt@} /LINKER_QUALIFIERS $@{link_opt@}"}]
15831 @end ifset
15832 @ifclear vms
15833 @item make_cmd=COMMAND
15834 [default: @code{"gnatmake $@{main@} -aI$@{src_dir@}
15835 -aO$@{obj_dir@} -g -gnatq -cargs $@{comp_opt@}
15836 -bargs $@{bind_opt@} -largs $@{link_opt@}"}]
15837 @end ifclear
15838 specifies the command used to recompile the whole application.
15839
15840 @item run_cmd=COMMAND
15841 [default: @code{"$@{main@}"}]
15842 specifies the command used to run the application.
15843
15844 @item debug_cmd=COMMAND
15845 [default: @code{"gdb $@{main@}"}]
15846 specifies the command used to debug the application
15847
15848 @end table
15849
15850 @noindent
15851 @command{gnatxref} and @command{gnatfind} only take into account the
15852 @code{src_dir} and @code{obj_dir} lines, and ignore the others.
15853
15854 @node Regular Expressions in gnatfind and gnatxref
15855 @section Regular Expressions in @code{gnatfind} and @code{gnatxref}
15856
15857 @noindent
15858 As specified in the section about @command{gnatfind}, the pattern can be a
15859 regular expression. Actually, there are to set of regular expressions
15860 which are recognized by the program:
15861
15862 @table @code
15863 @item globbing patterns
15864 These are the most usual regular expression. They are the same that you
15865 generally used in a Unix shell command line, or in a DOS session.
15866
15867 Here is a more formal grammar:
15868 @smallexample
15869 @group
15870 @iftex
15871 @leftskip=.5cm
15872 @end iftex
15873 regexp ::= term
15874 term ::= elmt -- matches elmt
15875 term ::= elmt elmt -- concatenation (elmt then elmt)
15876 term ::= * -- any string of 0 or more characters
15877 term ::= ? -- matches any character
15878 term ::= [char @{char@}] -- matches any character listed
15879 term ::= [char - char] -- matches any character in range
15880 @end group
15881 @end smallexample
15882
15883 @item full regular expression
15884 The second set of regular expressions is much more powerful. This is the
15885 type of regular expressions recognized by utilities such a @file{grep}.
15886
15887 The following is the form of a regular expression, expressed in Ada
15888 reference manual style BNF is as follows
15889
15890 @smallexample
15891 @iftex
15892 @leftskip=.5cm
15893 @end iftex
15894 @group
15895 regexp ::= term @{| term@} -- alternation (term or term @dots{})
15896
15897 term ::= item @{item@} -- concatenation (item then item)
15898
15899 item ::= elmt -- match elmt
15900 item ::= elmt * -- zero or more elmt's
15901 item ::= elmt + -- one or more elmt's
15902 item ::= elmt ? -- matches elmt or nothing
15903 @end group
15904 @group
15905 elmt ::= nschar -- matches given character
15906 elmt ::= [nschar @{nschar@}] -- matches any character listed
15907 elmt ::= [^^^ nschar @{nschar@}] -- matches any character not listed
15908 elmt ::= [char - char] -- matches chars in given range
15909 elmt ::= \ char -- matches given character
15910 elmt ::= . -- matches any single character
15911 elmt ::= ( regexp ) -- parens used for grouping
15912
15913 char ::= any character, including special characters
15914 nschar ::= any character except ()[].*+?^^^
15915 @end group
15916 @end smallexample
15917
15918 Following are a few examples:
15919
15920 @table @samp
15921 @item abcde|fghi
15922 will match any of the two strings @samp{abcde} and @samp{fghi},
15923
15924 @item abc*d
15925 will match any string like @samp{abd}, @samp{abcd}, @samp{abccd},
15926 @samp{abcccd}, and so on,
15927
15928 @item [a-z]+
15929 will match any string which has only lowercase characters in it (and at
15930 least one character.
15931
15932 @end table
15933 @end table
15934
15935 @node Examples of gnatxref Usage
15936 @section Examples of @code{gnatxref} Usage
15937
15938 @subsection General Usage
15939
15940 @noindent
15941 For the following examples, we will consider the following units:
15942
15943 @smallexample @c ada
15944 @group
15945 @cartouche
15946 main.ads:
15947 1: with Bar;
15948 2: package Main is
15949 3: procedure Foo (B : in Integer);
15950 4: C : Integer;
15951 5: private
15952 6: D : Integer;
15953 7: end Main;
15954
15955 main.adb:
15956 1: package body Main is
15957 2: procedure Foo (B : in Integer) is
15958 3: begin
15959 4: C := B;
15960 5: D := B;
15961 6: Bar.Print (B);
15962 7: Bar.Print (C);
15963 8: end Foo;
15964 9: end Main;
15965
15966 bar.ads:
15967 1: package Bar is
15968 2: procedure Print (B : Integer);
15969 3: end bar;
15970 @end cartouche
15971 @end group
15972 @end smallexample
15973
15974 @table @code
15975
15976 @noindent
15977 The first thing to do is to recompile your application (for instance, in
15978 that case just by doing a @samp{gnatmake main}, so that GNAT generates
15979 the cross-referencing information.
15980 You can then issue any of the following commands:
15981
15982 @item gnatxref main.adb
15983 @code{gnatxref} generates cross-reference information for main.adb
15984 and every unit 'with'ed by main.adb.
15985
15986 The output would be:
15987 @smallexample
15988 @iftex
15989 @leftskip=0cm
15990 @end iftex
15991 B Type: Integer
15992 Decl: bar.ads 2:22
15993 B Type: Integer
15994 Decl: main.ads 3:20
15995 Body: main.adb 2:20
15996 Ref: main.adb 4:13 5:13 6:19
15997 Bar Type: Unit
15998 Decl: bar.ads 1:9
15999 Ref: main.adb 6:8 7:8
16000 main.ads 1:6
16001 C Type: Integer
16002 Decl: main.ads 4:5
16003 Modi: main.adb 4:8
16004 Ref: main.adb 7:19
16005 D Type: Integer
16006 Decl: main.ads 6:5
16007 Modi: main.adb 5:8
16008 Foo Type: Unit
16009 Decl: main.ads 3:15
16010 Body: main.adb 2:15
16011 Main Type: Unit
16012 Decl: main.ads 2:9
16013 Body: main.adb 1:14
16014 Print Type: Unit
16015 Decl: bar.ads 2:15
16016 Ref: main.adb 6:12 7:12
16017 @end smallexample
16018
16019 @noindent
16020 that is the entity @code{Main} is declared in main.ads, line 2, column 9,
16021 its body is in main.adb, line 1, column 14 and is not referenced any where.
16022
16023 The entity @code{Print} is declared in bar.ads, line 2, column 15 and it
16024 it referenced in main.adb, line 6 column 12 and line 7 column 12.
16025
16026 @item gnatxref package1.adb package2.ads
16027 @code{gnatxref} will generates cross-reference information for
16028 package1.adb, package2.ads and any other package 'with'ed by any
16029 of these.
16030
16031 @end table
16032
16033 @ifclear vms
16034 @subsection Using gnatxref with vi
16035
16036 @code{gnatxref} can generate a tags file output, which can be used
16037 directly from @command{vi}. Note that the standard version of @command{vi}
16038 will not work properly with overloaded symbols. Consider using another
16039 free implementation of @command{vi}, such as @command{vim}.
16040
16041 @smallexample
16042 $ gnatxref -v gnatfind.adb > tags
16043 @end smallexample
16044
16045 @noindent
16046 will generate the tags file for @code{gnatfind} itself (if the sources
16047 are in the search path!).
16048
16049 From @command{vi}, you can then use the command @samp{:tag @var{entity}}
16050 (replacing @var{entity} by whatever you are looking for), and vi will
16051 display a new file with the corresponding declaration of entity.
16052 @end ifclear
16053
16054 @node Examples of gnatfind Usage
16055 @section Examples of @code{gnatfind} Usage
16056
16057 @table @code
16058
16059 @item gnatfind ^-f^/FULL_PATHNAME^ xyz:main.adb
16060 Find declarations for all entities xyz referenced at least once in
16061 main.adb. The references are search in every library file in the search
16062 path.
16063
16064 The directories will be printed as well (as the @samp{^-f^/FULL_PATHNAME^}
16065 switch is set)
16066
16067 The output will look like:
16068 @smallexample
16069 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
16070 ^directory/^[directory]^main.adb:24:10: xyz <= body
16071 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
16072 @end smallexample
16073
16074 @noindent
16075 that is to say, one of the entities xyz found in main.adb is declared at
16076 line 12 of main.ads (and its body is in main.adb), and another one is
16077 declared at line 45 of foo.ads
16078
16079 @item gnatfind ^-fs^/FULL_PATHNAME/SOURCE_LINE^ xyz:main.adb
16080 This is the same command as the previous one, instead @code{gnatfind} will
16081 display the content of the Ada source file lines.
16082
16083 The output will look like:
16084
16085 @smallexample
16086 ^directory/^[directory]^main.ads:106:14: xyz <= declaration
16087 procedure xyz;
16088 ^directory/^[directory]^main.adb:24:10: xyz <= body
16089 procedure xyz is
16090 ^directory/^[directory]^foo.ads:45:23: xyz <= declaration
16091 xyz : Integer;
16092 @end smallexample
16093
16094 @noindent
16095 This can make it easier to find exactly the location your are looking
16096 for.
16097
16098 @item gnatfind ^-r^/REFERENCES^ "*x*":main.ads:123 foo.adb
16099 Find references to all entities containing an x that are
16100 referenced on line 123 of main.ads.
16101 The references will be searched only in main.ads and foo.adb.
16102
16103 @item gnatfind main.ads:123
16104 Find declarations and bodies for all entities that are referenced on
16105 line 123 of main.ads.
16106
16107 This is the same as @code{gnatfind "*":main.adb:123}.
16108
16109 @item gnatfind ^mydir/^[mydir]^main.adb:123:45
16110 Find the declaration for the entity referenced at column 45 in
16111 line 123 of file main.adb in directory mydir. Note that it
16112 is usual to omit the identifier name when the column is given,
16113 since the column position identifies a unique reference.
16114
16115 The column has to be the beginning of the identifier, and should not
16116 point to any character in the middle of the identifier.
16117
16118 @end table
16119
16120 @c *********************************
16121 @node The GNAT Pretty-Printer gnatpp
16122 @chapter The GNAT Pretty-Printer @command{gnatpp}
16123 @findex gnatpp
16124 @cindex Pretty-Printer
16125
16126 @noindent
16127 ^The @command{gnatpp} tool^GNAT PRETTY^ is an ASIS-based utility
16128 for source reformatting / pretty-printing.
16129 It takes an Ada source file as input and generates a reformatted
16130 version as output.
16131 You can specify various style directives via switches; e.g.,
16132 identifier case conventions, rules of indentation, and comment layout.
16133
16134 To produce a reformatted file, @command{gnatpp} generates and uses the ASIS
16135 tree for the input source and thus requires the input to be syntactically and
16136 semantically legal.
16137 If this condition is not met, @command{gnatpp} will terminate with an
16138 error message; no output file will be generated.
16139
16140 If the source files presented to @command{gnatpp} contain
16141 preprocessing directives, then the output file will
16142 correspond to the generated source after all
16143 preprocessing is carried out. There is no way
16144 using @command{gnatpp} to obtain pretty printed files that
16145 include the preprocessing directives.
16146
16147 If the compilation unit
16148 contained in the input source depends semantically upon units located
16149 outside the current directory, you have to provide the source search path
16150 when invoking @command{gnatpp}, if these units are contained in files with
16151 names that do not follow the GNAT file naming rules, you have to provide
16152 the configuration file describing the corresponding naming scheme;
16153 see the description of the @command{gnatpp}
16154 switches below. Another possibility is to use a project file and to
16155 call @command{gnatpp} through the @command{gnat} driver
16156
16157 The @command{gnatpp} command has the form
16158
16159 @smallexample
16160 @c $ gnatpp @ovar{switches} @var{filename}
16161 @c Expanding @ovar macro inline (explanation in macro def comments)
16162 $ gnatpp @r{[}@var{switches}@r{]} @var{filename}
16163 @end smallexample
16164
16165 @noindent
16166 where
16167 @itemize @bullet
16168 @item
16169 @var{switches} is an optional sequence of switches defining such properties as
16170 the formatting rules, the source search path, and the destination for the
16171 output source file
16172
16173 @item
16174 @var{filename} is the name (including the extension) of the source file to
16175 reformat; ``wildcards'' or several file names on the same gnatpp command are
16176 allowed. The file name may contain path information; it does not have to
16177 follow the GNAT file naming rules
16178 @end itemize
16179
16180 @menu
16181 * Switches for gnatpp::
16182 * Formatting Rules::
16183 @end menu
16184
16185 @node Switches for gnatpp
16186 @section Switches for @command{gnatpp}
16187
16188 @noindent
16189 The following subsections describe the various switches accepted by
16190 @command{gnatpp}, organized by category.
16191
16192 @ifclear vms
16193 You specify a switch by supplying a name and generally also a value.
16194 In many cases the values for a switch with a given name are incompatible with
16195 each other
16196 (for example the switch that controls the casing of a reserved word may have
16197 exactly one value: upper case, lower case, or
16198 mixed case) and thus exactly one such switch can be in effect for an
16199 invocation of @command{gnatpp}.
16200 If more than one is supplied, the last one is used.
16201 However, some values for the same switch are mutually compatible.
16202 You may supply several such switches to @command{gnatpp}, but then
16203 each must be specified in full, with both the name and the value.
16204 Abbreviated forms (the name appearing once, followed by each value) are
16205 not permitted.
16206 For example, to set
16207 the alignment of the assignment delimiter both in declarations and in
16208 assignment statements, you must write @option{-A2A3}
16209 (or @option{-A2 -A3}), but not @option{-A23}.
16210 @end ifclear
16211
16212 @ifset vms
16213 In many cases the set of options for a given qualifier are incompatible with
16214 each other (for example the qualifier that controls the casing of a reserved
16215 word may have exactly one option, which specifies either upper case, lower
16216 case, or mixed case), and thus exactly one such option can be in effect for
16217 an invocation of @command{gnatpp}.
16218 If more than one is supplied, the last one is used.
16219 However, some qualifiers have options that are mutually compatible,
16220 and then you may then supply several such options when invoking
16221 @command{gnatpp}.
16222 @end ifset
16223
16224 In most cases, it is obvious whether or not the
16225 ^values for a switch with a given name^options for a given qualifier^
16226 are compatible with each other.
16227 When the semantics might not be evident, the summaries below explicitly
16228 indicate the effect.
16229
16230 @menu
16231 * Alignment Control::
16232 * Casing Control::
16233 * Construct Layout Control::
16234 * General Text Layout Control::
16235 * Other Formatting Options::
16236 * Setting the Source Search Path::
16237 * Output File Control::
16238 * Other gnatpp Switches::
16239 @end menu
16240
16241 @node Alignment Control
16242 @subsection Alignment Control
16243 @cindex Alignment control in @command{gnatpp}
16244
16245 @noindent
16246 Programs can be easier to read if certain constructs are vertically aligned.
16247 By default all alignments are set ON.
16248 Through the @option{^-A0^/ALIGN=OFF^} switch you may reset the default to
16249 OFF, and then use one or more of the other
16250 ^@option{-A@var{n}} switches^@option{/ALIGN} options^
16251 to activate alignment for specific constructs.
16252
16253 @table @option
16254 @cindex @option{^-A@var{n}^/ALIGN^} (@command{gnatpp})
16255
16256 @ifset vms
16257 @item /ALIGN=ON
16258 Set all alignments to ON
16259 @end ifset
16260
16261 @item ^-A0^/ALIGN=OFF^
16262 Set all alignments to OFF
16263
16264 @item ^-A1^/ALIGN=COLONS^
16265 Align @code{:} in declarations
16266
16267 @item ^-A2^/ALIGN=DECLARATIONS^
16268 Align @code{:=} in initializations in declarations
16269
16270 @item ^-A3^/ALIGN=STATEMENTS^
16271 Align @code{:=} in assignment statements
16272
16273 @item ^-A4^/ALIGN=ARROWS^
16274 Align @code{=>} in associations
16275
16276 @item ^-A5^/ALIGN=COMPONENT_CLAUSES^
16277 Align @code{at} keywords in the component clauses in record
16278 representation clauses
16279 @end table
16280
16281 @noindent
16282 The @option{^-A^/ALIGN^} switches are mutually compatible; any combination
16283 is allowed.
16284
16285 @node Casing Control
16286 @subsection Casing Control
16287 @cindex Casing control in @command{gnatpp}
16288
16289 @noindent
16290 @command{gnatpp} allows you to specify the casing for reserved words,
16291 pragma names, attribute designators and identifiers.
16292 For identifiers you may define a
16293 general rule for name casing but also override this rule
16294 via a set of dictionary files.
16295
16296 Three types of casing are supported: lower case, upper case, and mixed case.
16297 Lower and upper case are self-explanatory (but since some letters in
16298 Latin1 and other GNAT-supported character sets
16299 exist only in lower-case form, an upper case conversion will have no
16300 effect on them.)
16301 ``Mixed case'' means that the first letter, and also each letter immediately
16302 following an underscore, are converted to their uppercase forms;
16303 all the other letters are converted to their lowercase forms.
16304
16305 @table @option
16306 @cindex @option{^-a@var{x}^/ATTRIBUTE^} (@command{gnatpp})
16307 @item ^-aL^/ATTRIBUTE_CASING=LOWER_CASE^
16308 Attribute designators are lower case
16309
16310 @item ^-aU^/ATTRIBUTE_CASING=UPPER_CASE^
16311 Attribute designators are upper case
16312
16313 @item ^-aM^/ATTRIBUTE_CASING=MIXED_CASE^
16314 Attribute designators are mixed case (this is the default)
16315
16316 @cindex @option{^-k@var{x}^/KEYWORD_CASING^} (@command{gnatpp})
16317 @item ^-kL^/KEYWORD_CASING=LOWER_CASE^
16318 Keywords (technically, these are known in Ada as @emph{reserved words}) are
16319 lower case (this is the default)
16320
16321 @item ^-kU^/KEYWORD_CASING=UPPER_CASE^
16322 Keywords are upper case
16323
16324 @cindex @option{^-n@var{x}^/NAME_CASING^} (@command{gnatpp})
16325 @item ^-nD^/NAME_CASING=AS_DECLARED^
16326 Name casing for defining occurrences are as they appear in the source file
16327 (this is the default)
16328
16329 @item ^-nU^/NAME_CASING=UPPER_CASE^
16330 Names are in upper case
16331
16332 @item ^-nL^/NAME_CASING=LOWER_CASE^
16333 Names are in lower case
16334
16335 @item ^-nM^/NAME_CASING=MIXED_CASE^
16336 Names are in mixed case
16337
16338 @cindex @option{^-p@var{x}^/PRAGMA_CASING^} (@command{gnatpp})
16339 @item ^-pL^/PRAGMA_CASING=LOWER_CASE^
16340 Pragma names are lower case
16341
16342 @item ^-pU^/PRAGMA_CASING=UPPER_CASE^
16343 Pragma names are upper case
16344
16345 @item ^-pM^/PRAGMA_CASING=MIXED_CASE^
16346 Pragma names are mixed case (this is the default)
16347
16348 @item ^-D@var{file}^/DICTIONARY=@var{file}^
16349 @cindex @option{^-D^/DICTIONARY^} (@command{gnatpp})
16350 Use @var{file} as a @emph{dictionary file} that defines
16351 the casing for a set of specified names,
16352 thereby overriding the effect on these names by
16353 any explicit or implicit
16354 ^-n^/NAME_CASING^ switch.
16355 To supply more than one dictionary file,
16356 use ^several @option{-D} switches^a list of files as options^.
16357
16358 @noindent
16359 @option{gnatpp} implicitly uses a @emph{default dictionary file}
16360 to define the casing for the Ada predefined names and
16361 the names declared in the GNAT libraries.
16362
16363 @item ^-D-^/SPECIFIC_CASING^
16364 @cindex @option{^-D-^/SPECIFIC_CASING^} (@command{gnatpp})
16365 Do not use the default dictionary file;
16366 instead, use the casing
16367 defined by a @option{^-n^/NAME_CASING^} switch and any explicit
16368 dictionary file(s)
16369 @end table
16370
16371 @noindent
16372 The structure of a dictionary file, and details on the conventions
16373 used in the default dictionary file, are defined in @ref{Name Casing}.
16374
16375 The @option{^-D-^/SPECIFIC_CASING^} and
16376 @option{^-D@var{file}^/DICTIONARY=@var{file}^} switches are mutually
16377 compatible.
16378
16379 @node Construct Layout Control
16380 @subsection Construct Layout Control
16381 @cindex Layout control in @command{gnatpp}
16382
16383 @noindent
16384 This group of @command{gnatpp} switches controls the layout of comments and
16385 complex syntactic constructs. See @ref{Formatting Comments} for details
16386 on their effect.
16387
16388 @table @option
16389 @cindex @option{^-c@var{n}^/COMMENTS_LAYOUT^} (@command{gnatpp})
16390 @item ^-c0^/COMMENTS_LAYOUT=UNTOUCHED^
16391 All the comments remain unchanged
16392
16393 @item ^-c1^/COMMENTS_LAYOUT=DEFAULT^
16394 GNAT-style comment line indentation (this is the default).
16395
16396 @item ^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^
16397 Reference-manual comment line indentation.
16398
16399 @item ^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^
16400 GNAT-style comment beginning
16401
16402 @item ^-c4^/COMMENTS_LAYOUT=REFORMAT^
16403 Reformat comment blocks
16404
16405 @item ^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^
16406 Keep unchanged special form comments
16407
16408 Reformat comment blocks
16409
16410 @cindex @option{^-l@var{n}^/CONSTRUCT_LAYOUT^} (@command{gnatpp})
16411 @item ^-l1^/CONSTRUCT_LAYOUT=GNAT^
16412 GNAT-style layout (this is the default)
16413
16414 @item ^-l2^/CONSTRUCT_LAYOUT=COMPACT^
16415 Compact layout
16416
16417 @item ^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^
16418 Uncompact layout
16419
16420 @cindex @option{^-N^/NOTABS^} (@command{gnatpp})
16421 @item ^-N^/NOTABS^
16422 All the VT characters are removed from the comment text. All the HT characters
16423 are expanded with the sequences of space characters to get to the next tab
16424 stops.
16425
16426 @cindex @option{^--no-separate-is^/NO_SEPARATE_IS^} (@command{gnatpp})
16427 @item ^--no-separate-is^/NO_SEPARATE_IS^
16428 Do not place the keyword @code{is} on a separate line in a subprogram body in
16429 case if the spec occupies more then one line.
16430
16431 @cindex @option{^--separate-label^/SEPARATE_LABEL^} (@command{gnatpp})
16432 @item ^--separate-label^/SEPARATE_LABEL^
16433 Place statement label(s) on a separate line, with the following statement
16434 on the next line.
16435
16436 @cindex @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} (@command{gnatpp})
16437 @item ^--separate-loop-then^/SEPARATE_LOOP_THEN^
16438 Place the keyword @code{loop} in FOR and WHILE loop statements and the
16439 keyword @code{then} in IF statements on a separate line.
16440
16441 @cindex @option{^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^} (@command{gnatpp})
16442 @item ^--no-separate-loop-then^/NO_SEPARATE_LOOP_THEN^
16443 Do not place the keyword @code{loop} in FOR and WHILE loop statements and the
16444 keyword @code{then} in IF statements on a separate line. This option is
16445 incompatible with @option{^--separate-loop-then^/SEPARATE_LOOP_THEN^} option.
16446
16447 @cindex @option{^--use-on-new-line^/USE_ON_NEW_LINE^} (@command{gnatpp})
16448 @item ^--use-on-new-line^/USE_ON_NEW_LINE^
16449 Start each USE clause in a context clause from a separate line.
16450
16451 @cindex @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^} (@command{gnatpp})
16452 @item ^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^
16453 Use a separate line for a loop or block statement name, but do not use an extra
16454 indentation level for the statement itself.
16455
16456 @end table
16457
16458 @ifclear vms
16459 @noindent
16460 The @option{-c1} and @option{-c2} switches are incompatible.
16461 The @option{-c3} and @option{-c4} switches are compatible with each other and
16462 also with @option{-c1} and @option{-c2}. The @option{-c0} switch disables all
16463 the other comment formatting switches.
16464
16465 The @option{-l1}, @option{-l2}, and @option{-l3} switches are incompatible.
16466 @end ifclear
16467
16468 @ifset vms
16469 @noindent
16470 For the @option{/COMMENTS_LAYOUT} qualifier:
16471 @itemize @bullet
16472 @item
16473 The @option{DEFAULT} and @option{STANDARD_INDENT} options are incompatible.
16474 @item
16475 The @option{GNAT_BEGINNING} and @option{REFORMAT} options are compatible with
16476 each other and also with @option{DEFAULT} and @option{STANDARD_INDENT}.
16477 @end itemize
16478
16479 @noindent
16480 The @option{GNAT}, @option{COMPACT}, and @option{UNCOMPACT} options for the
16481 @option{/CONSTRUCT_LAYOUT} qualifier are incompatible.
16482 @end ifset
16483
16484 @node General Text Layout Control
16485 @subsection General Text Layout Control
16486
16487 @noindent
16488 These switches allow control over line length and indentation.
16489
16490 @table @option
16491 @item ^-M@var{nnn}^/LINE_LENGTH_MAX=@var{nnn}^
16492 @cindex @option{^-M^/LINE_LENGTH^} (@command{gnatpp})
16493 Maximum line length, @var{nnn} from 32@dots{}256, the default value is 79
16494
16495 @item ^-i@var{nnn}^/INDENTATION_LEVEL=@var{nnn}^
16496 @cindex @option{^-i^/INDENTATION_LEVEL^} (@command{gnatpp})
16497 Indentation level, @var{nnn} from 1@dots{}9, the default value is 3
16498
16499 @item ^-cl@var{nnn}^/CONTINUATION_INDENT=@var{nnn}^
16500 @cindex @option{^-cl^/CONTINUATION_INDENT^} (@command{gnatpp})
16501 Indentation level for continuation lines (relative to the line being
16502 continued), @var{nnn} from 1@dots{}9.
16503 The default
16504 value is one less then the (normal) indentation level, unless the
16505 indentation is set to 1 (in which case the default value for continuation
16506 line indentation is also 1)
16507 @end table
16508
16509 @node Other Formatting Options
16510 @subsection Other Formatting Options
16511
16512 @noindent
16513 These switches control the inclusion of missing end/exit labels, and
16514 the indentation level in @b{case} statements.
16515
16516 @table @option
16517 @item ^-e^/NO_MISSED_LABELS^
16518 @cindex @option{^-e^/NO_MISSED_LABELS^} (@command{gnatpp})
16519 Do not insert missing end/exit labels. An end label is the name of
16520 a construct that may optionally be repeated at the end of the
16521 construct's declaration;
16522 e.g., the names of packages, subprograms, and tasks.
16523 An exit label is the name of a loop that may appear as target
16524 of an exit statement within the loop.
16525 By default, @command{gnatpp} inserts these end/exit labels when
16526 they are absent from the original source. This option suppresses such
16527 insertion, so that the formatted source reflects the original.
16528
16529 @item ^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^
16530 @cindex @option{^-ff^/FORM_FEED_AFTER_PRAGMA_PAGE^} (@command{gnatpp})
16531 Insert a Form Feed character after a pragma Page.
16532
16533 @item ^-T@var{nnn}^/MAX_INDENT=@var{nnn}^
16534 @cindex @option{^-T^/MAX_INDENT^} (@command{gnatpp})
16535 Do not use an additional indentation level for @b{case} alternatives
16536 and variants if there are @var{nnn} or more (the default
16537 value is 10).
16538 If @var{nnn} is 0, an additional indentation level is
16539 used for @b{case} alternatives and variants regardless of their number.
16540 @end table
16541
16542 @node Setting the Source Search Path
16543 @subsection Setting the Source Search Path
16544
16545 @noindent
16546 To define the search path for the input source file, @command{gnatpp}
16547 uses the same switches as the GNAT compiler, with the same effects.
16548
16549 @table @option
16550 @item ^-I^/SEARCH=^@var{dir}
16551 @cindex @option{^-I^/SEARCH^} (@code{gnatpp})
16552 The same as the corresponding gcc switch
16553
16554 @item ^-I-^/NOCURRENT_DIRECTORY^
16555 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatpp})
16556 The same as the corresponding gcc switch
16557
16558 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE^=@var{path}
16559 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@code{gnatpp})
16560 The same as the corresponding gcc switch
16561
16562 @item ^--RTS^/RUNTIME_SYSTEM^=@var{path}
16563 @cindex @option{^--RTS^/RUNTIME_SYSTEM^} (@code{gnatpp})
16564 The same as the corresponding gcc switch
16565
16566 @end table
16567
16568 @node Output File Control
16569 @subsection Output File Control
16570
16571 @noindent
16572 By default the output is sent to the file whose name is obtained by appending
16573 the ^@file{.pp}^@file{$PP}^ suffix to the name of the input file
16574 (if the file with this name already exists, it is unconditionally overwritten).
16575 Thus if the input file is @file{^my_ada_proc.adb^MY_ADA_PROC.ADB^} then
16576 @command{gnatpp} will produce @file{^my_ada_proc.adb.pp^MY_ADA_PROC.ADB$PP^}
16577 as output file.
16578 The output may be redirected by the following switches:
16579
16580 @table @option
16581 @item ^-pipe^/STANDARD_OUTPUT^
16582 @cindex @option{^-pipe^/STANDARD_OUTPUT^} (@code{gnatpp})
16583 Send the output to @code{Standard_Output}
16584
16585 @item ^-o @var{output_file}^/OUTPUT=@var{output_file}^
16586 @cindex @option{^-o^/OUTPUT^} (@code{gnatpp})
16587 Write the output into @var{output_file}.
16588 If @var{output_file} already exists, @command{gnatpp} terminates without
16589 reading or processing the input file.
16590
16591 @item ^-of ^/FORCED_OUTPUT=^@var{output_file}
16592 @cindex @option{^-of^/FORCED_OUTPUT^} (@code{gnatpp})
16593 Write the output into @var{output_file}, overwriting the existing file
16594 (if one is present).
16595
16596 @item ^-r^/REPLACE^
16597 @cindex @option{^-r^/REPLACE^} (@code{gnatpp})
16598 Replace the input source file with the reformatted output, and copy the
16599 original input source into the file whose name is obtained by appending the
16600 ^@file{.npp}^@file{$NPP}^ suffix to the name of the input file.
16601 If a file with this name already exists, @command{gnatpp} terminates without
16602 reading or processing the input file.
16603
16604 @item ^-rf^/OVERRIDING_REPLACE^
16605 @cindex @option{^-rf^/OVERRIDING_REPLACE^} (@code{gnatpp})
16606 Like @option{^-r^/REPLACE^} except that if the file with the specified name
16607 already exists, it is overwritten.
16608
16609 @item ^-rnb^/REPLACE_NO_BACKUP^
16610 @cindex @option{^-rnb^/REPLACE_NO_BACKUP^} (@code{gnatpp})
16611 Replace the input source file with the reformatted output without
16612 creating any backup copy of the input source.
16613
16614 @item ^--eol=@var{xxx}^/END_OF_LINE=@var{xxx}^
16615 @cindex @option{^--eol^/END_OF_LINE^} (@code{gnatpp})
16616 Specifies the format of the reformatted output file. The @var{xxx}
16617 ^string specified with the switch^option^ may be either
16618 @itemize @bullet
16619 @item ``@option{^dos^DOS^}'' MS DOS style, lines end with CR LF characters
16620 @item ``@option{^crlf^CRLF^}''
16621 the same as @option{^crlf^CRLF^}
16622 @item ``@option{^unix^UNIX^}'' UNIX style, lines end with LF character
16623 @item ``@option{^lf^LF^}''
16624 the same as @option{^unix^UNIX^}
16625 @end itemize
16626
16627 @item ^-W^/RESULT_ENCODING=^@var{e}
16628 @cindex @option{^-W^/RESULT_ENCODING=^} (@command{gnatpp})
16629 Specify the wide character encoding method used to write the code in the
16630 result file
16631 @var{e} is one of the following:
16632
16633 @itemize @bullet
16634
16635 @item ^h^HEX^
16636 Hex encoding
16637
16638 @item ^u^UPPER^
16639 Upper half encoding
16640
16641 @item ^s^SHIFT_JIS^
16642 Shift/JIS encoding
16643
16644 @item ^e^EUC^
16645 EUC encoding
16646
16647 @item ^8^UTF8^
16648 UTF-8 encoding
16649
16650 @item ^b^BRACKETS^
16651 Brackets encoding (default value)
16652 @end itemize
16653
16654 @end table
16655
16656 @noindent
16657 Options @option{^-pipe^/STANDARD_OUTPUT^},
16658 @option{^-o^/OUTPUT^} and
16659 @option{^-of^/FORCED_OUTPUT^} are allowed only if the call to gnatpp
16660 contains only one file to reformat.
16661 Option
16662 @option{^--eol^/END_OF_LINE^}
16663 and
16664 @option{^-W^/RESULT_ENCODING^}
16665 cannot be used together
16666 with @option{^-pipe^/STANDARD_OUTPUT^} option.
16667
16668 @node Other gnatpp Switches
16669 @subsection Other @code{gnatpp} Switches
16670
16671 @noindent
16672 The additional @command{gnatpp} switches are defined in this subsection.
16673
16674 @table @option
16675 @item ^-files @var{filename}^/FILES=@var{output_file}^
16676 @cindex @option{^-files^/FILES^} (@code{gnatpp})
16677 Take the argument source files from the specified file. This file should be an
16678 ordinary text file containing file names separated by spaces or
16679 line breaks. You can use this switch more than once in the same call to
16680 @command{gnatpp}. You also can combine this switch with an explicit list of
16681 files.
16682
16683 @item ^-v^/VERBOSE^
16684 @cindex @option{^-v^/VERBOSE^} (@code{gnatpp})
16685 Verbose mode;
16686 @command{gnatpp} generates version information and then
16687 a trace of the actions it takes to produce or obtain the ASIS tree.
16688
16689 @item ^-w^/WARNINGS^
16690 @cindex @option{^-w^/WARNINGS^} (@code{gnatpp})
16691 Warning mode;
16692 @command{gnatpp} generates a warning whenever it cannot provide
16693 a required layout in the result source.
16694 @end table
16695
16696 @node Formatting Rules
16697 @section Formatting Rules
16698
16699 @noindent
16700 The following subsections show how @command{gnatpp} treats ``white space'',
16701 comments, program layout, and name casing.
16702 They provide the detailed descriptions of the switches shown above.
16703
16704 @menu
16705 * White Space and Empty Lines::
16706 * Formatting Comments::
16707 * Construct Layout::
16708 * Name Casing::
16709 @end menu
16710
16711 @node White Space and Empty Lines
16712 @subsection White Space and Empty Lines
16713
16714 @noindent
16715 @command{gnatpp} does not have an option to control space characters.
16716 It will add or remove spaces according to the style illustrated by the
16717 examples in the @cite{Ada Reference Manual}.
16718
16719 The only format effectors
16720 (see @cite{Ada Reference Manual}, paragraph 2.1(13))
16721 that will appear in the output file are platform-specific line breaks,
16722 and also format effectors within (but not at the end of) comments.
16723 In particular, each horizontal tab character that is not inside
16724 a comment will be treated as a space and thus will appear in the
16725 output file as zero or more spaces depending on
16726 the reformatting of the line in which it appears.
16727 The only exception is a Form Feed character, which is inserted after a
16728 pragma @code{Page} when @option{-ff} is set.
16729
16730 The output file will contain no lines with trailing ``white space'' (spaces,
16731 format effectors).
16732
16733 Empty lines in the original source are preserved
16734 only if they separate declarations or statements.
16735 In such contexts, a
16736 sequence of two or more empty lines is replaced by exactly one empty line.
16737 Note that a blank line will be removed if it separates two ``comment blocks''
16738 (a comment block is a sequence of whole-line comments).
16739 In order to preserve a visual separation between comment blocks, use an
16740 ``empty comment'' (a line comprising only hyphens) rather than an empty line.
16741 Likewise, if for some reason you wish to have a sequence of empty lines,
16742 use a sequence of empty comments instead.
16743
16744 @node Formatting Comments
16745 @subsection Formatting Comments
16746
16747 @noindent
16748 Comments in Ada code are of two kinds:
16749 @itemize @bullet
16750 @item
16751 a @emph{whole-line comment}, which appears by itself (possibly preceded by
16752 ``white space'') on a line
16753
16754 @item
16755 an @emph{end-of-line comment}, which follows some other Ada lexical element
16756 on the same line.
16757 @end itemize
16758
16759 @noindent
16760 The indentation of a whole-line comment is that of either
16761 the preceding or following line in
16762 the formatted source, depending on switch settings as will be described below.
16763
16764 For an end-of-line comment, @command{gnatpp} leaves the same number of spaces
16765 between the end of the preceding Ada lexical element and the beginning
16766 of the comment as appear in the original source,
16767 unless either the comment has to be split to
16768 satisfy the line length limitation, or else the next line contains a
16769 whole line comment that is considered a continuation of this end-of-line
16770 comment (because it starts at the same position).
16771 In the latter two
16772 cases, the start of the end-of-line comment is moved right to the nearest
16773 multiple of the indentation level.
16774 This may result in a ``line overflow'' (the right-shifted comment extending
16775 beyond the maximum line length), in which case the comment is split as
16776 described below.
16777
16778 There is a difference between @option{^-c1^/COMMENTS_LAYOUT=DEFAULT^}
16779 (GNAT-style comment line indentation)
16780 and @option{^-c2^/COMMENTS_LAYOUT=STANDARD_INDENT^}
16781 (reference-manual comment line indentation).
16782 With reference-manual style, a whole-line comment is indented as if it
16783 were a declaration or statement at the same place
16784 (i.e., according to the indentation of the preceding line(s)).
16785 With GNAT style, a whole-line comment that is immediately followed by an
16786 @b{if} or @b{case} statement alternative, a record variant, or the reserved
16787 word @b{begin}, is indented based on the construct that follows it.
16788
16789 For example:
16790 @smallexample @c ada
16791 @cartouche
16792 if A then
16793 null;
16794 -- some comment
16795 else
16796 null;
16797 end if;
16798 @end cartouche
16799 @end smallexample
16800
16801 @noindent
16802 Reference-manual indentation produces:
16803
16804 @smallexample @c ada
16805 @cartouche
16806 if A then
16807 null;
16808 -- some comment
16809 else
16810 null;
16811 end if;
16812 @end cartouche
16813 @end smallexample
16814
16815 @noindent
16816 while GNAT-style indentation produces:
16817
16818 @smallexample @c ada
16819 @cartouche
16820 if A then
16821 null;
16822 -- some comment
16823 else
16824 null;
16825 end if;
16826 @end cartouche
16827 @end smallexample
16828
16829 @noindent
16830 The @option{^-c3^/COMMENTS_LAYOUT=GNAT_BEGINNING^} switch
16831 (GNAT style comment beginning) has the following
16832 effect:
16833
16834 @itemize @bullet
16835 @item
16836 For each whole-line comment that does not end with two hyphens,
16837 @command{gnatpp} inserts spaces if necessary after the starting two hyphens
16838 to ensure that there are at least two spaces between these hyphens and the
16839 first non-blank character of the comment.
16840 @end itemize
16841
16842 @noindent
16843 For an end-of-line comment, if in the original source the next line is a
16844 whole-line comment that starts at the same position
16845 as the end-of-line comment,
16846 then the whole-line comment (and all whole-line comments
16847 that follow it and that start at the same position)
16848 will start at this position in the output file.
16849
16850 @noindent
16851 That is, if in the original source we have:
16852
16853 @smallexample @c ada
16854 @cartouche
16855 begin
16856 A := B + C; -- B must be in the range Low1..High1
16857 -- C must be in the range Low2..High2
16858 --B+C will be in the range Low1+Low2..High1+High2
16859 X := X + 1;
16860 @end cartouche
16861 @end smallexample
16862
16863 @noindent
16864 Then in the formatted source we get
16865
16866 @smallexample @c ada
16867 @cartouche
16868 begin
16869 A := B + C; -- B must be in the range Low1..High1
16870 -- C must be in the range Low2..High2
16871 -- B+C will be in the range Low1+Low2..High1+High2
16872 X := X + 1;
16873 @end cartouche
16874 @end smallexample
16875
16876 @noindent
16877 A comment that exceeds the line length limit will be split.
16878 Unless switch
16879 @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} (reformat comment blocks) is set and
16880 the line belongs to a reformattable block, splitting the line generates a
16881 @command{gnatpp} warning.
16882 The @option{^-c4^/COMMENTS_LAYOUT=REFORMAT^} switch specifies that whole-line
16883 comments may be reformatted in typical
16884 word processor style (that is, moving words between lines and putting as
16885 many words in a line as possible).
16886
16887 @noindent
16888 The @option{^-c5^/COMMENTS_LAYOUT=KEEP_SPECIAL^} switch specifies, that comments
16889 that has a special format (that is, a character that is neither a letter nor digit
16890 not white space nor line break immediately following the leading @code{--} of
16891 the comment) should be without any change moved from the argument source
16892 into reformatted source. This switch allows to preserve comments that are used
16893 as a special marks in the code (e.g.@: SPARK annotation).
16894
16895 @node Construct Layout
16896 @subsection Construct Layout
16897
16898 @noindent
16899 In several cases the suggested layout in the Ada Reference Manual includes
16900 an extra level of indentation that many programmers prefer to avoid. The
16901 affected cases include:
16902
16903 @itemize @bullet
16904
16905 @item Record type declaration (RM 3.8)
16906
16907 @item Record representation clause (RM 13.5.1)
16908
16909 @item Loop statement in case if a loop has a statement identifier (RM 5.6)
16910
16911 @item Block statement in case if a block has a statement identifier (RM 5.6)
16912 @end itemize
16913
16914 @noindent
16915 In compact mode (when GNAT style layout or compact layout is set),
16916 the pretty printer uses one level of indentation instead
16917 of two. This is achieved in the record definition and record representation
16918 clause cases by putting the @code{record} keyword on the same line as the
16919 start of the declaration or representation clause, and in the block and loop
16920 case by putting the block or loop header on the same line as the statement
16921 identifier.
16922
16923 @noindent
16924 The difference between GNAT style @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^}
16925 and compact @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^}
16926 layout on the one hand, and uncompact layout
16927 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} on the other hand,
16928 can be illustrated by the following examples:
16929
16930 @iftex
16931 @cartouche
16932 @multitable @columnfractions .5 .5
16933 @item @i{GNAT style, compact layout} @tab @i{Uncompact layout}
16934
16935 @item
16936 @smallexample @c ada
16937 type q is record
16938 a : integer;
16939 b : integer;
16940 end record;
16941 @end smallexample
16942 @tab
16943 @smallexample @c ada
16944 type q is
16945 record
16946 a : integer;
16947 b : integer;
16948 end record;
16949 @end smallexample
16950
16951 @item
16952 @smallexample @c ada
16953 for q use record
16954 a at 0 range 0 .. 31;
16955 b at 4 range 0 .. 31;
16956 end record;
16957 @end smallexample
16958 @tab
16959 @smallexample @c ada
16960 for q use
16961 record
16962 a at 0 range 0 .. 31;
16963 b at 4 range 0 .. 31;
16964 end record;
16965 @end smallexample
16966
16967 @item
16968 @smallexample @c ada
16969 Block : declare
16970 A : Integer := 3;
16971 begin
16972 Proc (A, A);
16973 end Block;
16974 @end smallexample
16975 @tab
16976 @smallexample @c ada
16977 Block :
16978 declare
16979 A : Integer := 3;
16980 begin
16981 Proc (A, A);
16982 end Block;
16983 @end smallexample
16984
16985 @item
16986 @smallexample @c ada
16987 Clear : for J in 1 .. 10 loop
16988 A (J) := 0;
16989 end loop Clear;
16990 @end smallexample
16991 @tab
16992 @smallexample @c ada
16993 Clear :
16994 for J in 1 .. 10 loop
16995 A (J) := 0;
16996 end loop Clear;
16997 @end smallexample
16998 @end multitable
16999 @end cartouche
17000 @end iftex
17001
17002 @ifnottex
17003 @smallexample
17004 @cartouche
17005 GNAT style, compact layout Uncompact layout
17006
17007 type q is record type q is
17008 a : integer; record
17009 b : integer; a : integer;
17010 end record; b : integer;
17011 end record;
17012
17013 for q use record for q use
17014 a at 0 range 0 .. 31; record
17015 b at 4 range 0 .. 31; a at 0 range 0 .. 31;
17016 end record; b at 4 range 0 .. 31;
17017 end record;
17018
17019 Block : declare Block :
17020 A : Integer := 3; declare
17021 begin A : Integer := 3;
17022 Proc (A, A); begin
17023 end Block; Proc (A, A);
17024 end Block;
17025
17026 Clear : for J in 1 .. 10 loop Clear :
17027 A (J) := 0; for J in 1 .. 10 loop
17028 end loop Clear; A (J) := 0;
17029 end loop Clear;
17030 @end cartouche
17031 @end smallexample
17032 @end ifnottex
17033
17034 @noindent
17035 A further difference between GNAT style layout and compact layout is that
17036 GNAT style layout inserts empty lines as separation for
17037 compound statements, return statements and bodies.
17038
17039 Note that the layout specified by
17040 @option{^--separate-stmt-name^/STMT_NAME_ON_NEW_LINE^}
17041 for named block and loop statements overrides the layout defined by these
17042 constructs by @option{^-l1^/CONSTRUCT_LAYOUT=GNAT^},
17043 @option{^-l2^/CONSTRUCT_LAYOUT=COMPACT^} or
17044 @option{^-l3^/CONSTRUCT_LAYOUT=UNCOMPACT^} option.
17045
17046 @node Name Casing
17047 @subsection Name Casing
17048
17049 @noindent
17050 @command{gnatpp} always converts the usage occurrence of a (simple) name to
17051 the same casing as the corresponding defining identifier.
17052
17053 You control the casing for defining occurrences via the
17054 @option{^-n^/NAME_CASING^} switch.
17055 @ifclear vms
17056 With @option{-nD} (``as declared'', which is the default),
17057 @end ifclear
17058 @ifset vms
17059 With @option{/NAME_CASING=AS_DECLARED}, which is the default,
17060 @end ifset
17061 defining occurrences appear exactly as in the source file
17062 where they are declared.
17063 The other ^values for this switch^options for this qualifier^ ---
17064 @option{^-nU^UPPER_CASE^},
17065 @option{^-nL^LOWER_CASE^},
17066 @option{^-nM^MIXED_CASE^} ---
17067 result in
17068 ^upper, lower, or mixed case, respectively^the corresponding casing^.
17069 If @command{gnatpp} changes the casing of a defining
17070 occurrence, it analogously changes the casing of all the
17071 usage occurrences of this name.
17072
17073 If the defining occurrence of a name is not in the source compilation unit
17074 currently being processed by @command{gnatpp}, the casing of each reference to
17075 this name is changed according to the value of the @option{^-n^/NAME_CASING^}
17076 switch (subject to the dictionary file mechanism described below).
17077 Thus @command{gnatpp} acts as though the @option{^-n^/NAME_CASING^} switch
17078 had affected the
17079 casing for the defining occurrence of the name.
17080
17081 Some names may need to be spelled with casing conventions that are not
17082 covered by the upper-, lower-, and mixed-case transformations.
17083 You can arrange correct casing by placing such names in a
17084 @emph{dictionary file},
17085 and then supplying a @option{^-D^/DICTIONARY^} switch.
17086 The casing of names from dictionary files overrides
17087 any @option{^-n^/NAME_CASING^} switch.
17088
17089 To handle the casing of Ada predefined names and the names from GNAT libraries,
17090 @command{gnatpp} assumes a default dictionary file.
17091 The name of each predefined entity is spelled with the same casing as is used
17092 for the entity in the @cite{Ada Reference Manual}.
17093 The name of each entity in the GNAT libraries is spelled with the same casing
17094 as is used in the declaration of that entity.
17095
17096 The @w{@option{^-D-^/SPECIFIC_CASING^}} switch suppresses the use of the
17097 default dictionary file.
17098 Instead, the casing for predefined and GNAT-defined names will be established
17099 by the @option{^-n^/NAME_CASING^} switch or explicit dictionary files.
17100 For example, by default the names @code{Ada.Text_IO} and @code{GNAT.OS_Lib}
17101 will appear as just shown,
17102 even in the presence of a @option{^-nU^/NAME_CASING=UPPER_CASE^} switch.
17103 To ensure that even such names are rendered in uppercase,
17104 additionally supply the @w{@option{^-D-^/SPECIFIC_CASING^}} switch
17105 (or else, less conveniently, place these names in upper case in a dictionary
17106 file).
17107
17108 A dictionary file is
17109 a plain text file; each line in this file can be either a blank line
17110 (containing only space characters and ASCII.HT characters), an Ada comment
17111 line, or the specification of exactly one @emph{casing schema}.
17112
17113 A casing schema is a string that has the following syntax:
17114
17115 @smallexample
17116 @cartouche
17117 @var{casing_schema} ::= @var{identifier} | *@var{simple_identifier}*
17118
17119 @var{simple_identifier} ::= @var{letter}@{@var{letter_or_digit}@}
17120 @end cartouche
17121 @end smallexample
17122
17123 @noindent
17124 (See @cite{Ada Reference Manual}, Section 2.3) for the definition of the
17125 @var{identifier} lexical element and the @var{letter_or_digit} category.)
17126
17127 The casing schema string can be followed by white space and/or an Ada-style
17128 comment; any amount of white space is allowed before the string.
17129
17130 If a dictionary file is passed as
17131 @ifclear vms
17132 the value of a @option{-D@var{file}} switch
17133 @end ifclear
17134 @ifset vms
17135 an option to the @option{/DICTIONARY} qualifier
17136 @end ifset
17137 then for every
17138 simple name and every identifier, @command{gnatpp} checks if the dictionary
17139 defines the casing for the name or for some of its parts (the term ``subword''
17140 is used below to denote the part of a name which is delimited by ``_'' or by
17141 the beginning or end of the word and which does not contain any ``_'' inside):
17142
17143 @itemize @bullet
17144 @item
17145 if the whole name is in the dictionary, @command{gnatpp} uses for this name
17146 the casing defined by the dictionary; no subwords are checked for this word
17147
17148 @item
17149 for every subword @command{gnatpp} checks if the dictionary contains the
17150 corresponding string of the form @code{*@var{simple_identifier}*},
17151 and if it does, the casing of this @var{simple_identifier} is used
17152 for this subword
17153
17154 @item
17155 if the whole name does not contain any ``_'' inside, and if for this name
17156 the dictionary contains two entries - one of the form @var{identifier},
17157 and another - of the form *@var{simple_identifier}*, then the first one
17158 is applied to define the casing of this name
17159
17160 @item
17161 if more than one dictionary file is passed as @command{gnatpp} switches, each
17162 dictionary adds new casing exceptions and overrides all the existing casing
17163 exceptions set by the previous dictionaries
17164
17165 @item
17166 when @command{gnatpp} checks if the word or subword is in the dictionary,
17167 this check is not case sensitive
17168 @end itemize
17169
17170 @noindent
17171 For example, suppose we have the following source to reformat:
17172
17173 @smallexample @c ada
17174 @cartouche
17175 procedure test is
17176 name1 : integer := 1;
17177 name4_name3_name2 : integer := 2;
17178 name2_name3_name4 : Boolean;
17179 name1_var : Float;
17180 begin
17181 name2_name3_name4 := name4_name3_name2 > name1;
17182 end;
17183 @end cartouche
17184 @end smallexample
17185
17186 @noindent
17187 And suppose we have two dictionaries:
17188
17189 @smallexample
17190 @cartouche
17191 @i{dict1:}
17192 NAME1
17193 *NaMe3*
17194 *Name1*
17195 @end cartouche
17196
17197 @cartouche
17198 @i{dict2:}
17199 *NAME3*
17200 @end cartouche
17201 @end smallexample
17202
17203 @noindent
17204 If @command{gnatpp} is called with the following switches:
17205
17206 @smallexample
17207 @ifclear vms
17208 @command{gnatpp -nM -D dict1 -D dict2 test.adb}
17209 @end ifclear
17210 @ifset vms
17211 @command{gnatpp test.adb /NAME_CASING=MIXED_CASE /DICTIONARY=(dict1, dict2)}
17212 @end ifset
17213 @end smallexample
17214
17215 @noindent
17216 then we will get the following name casing in the @command{gnatpp} output:
17217
17218 @smallexample @c ada
17219 @cartouche
17220 procedure Test is
17221 NAME1 : Integer := 1;
17222 Name4_NAME3_Name2 : Integer := 2;
17223 Name2_NAME3_Name4 : Boolean;
17224 Name1_Var : Float;
17225 begin
17226 Name2_NAME3_Name4 := Name4_NAME3_Name2 > NAME1;
17227 end Test;
17228 @end cartouche
17229 @end smallexample
17230
17231 @c *********************************
17232 @node The GNAT Metric Tool gnatmetric
17233 @chapter The GNAT Metric Tool @command{gnatmetric}
17234 @findex gnatmetric
17235 @cindex Metric tool
17236
17237 @noindent
17238 ^The @command{gnatmetric} tool^@command{GNAT METRIC}^ is an ASIS-based utility
17239 for computing various program metrics.
17240 It takes an Ada source file as input and generates a file containing the
17241 metrics data as output. Various switches control which
17242 metrics are computed and output.
17243
17244 @command{gnatmetric} generates and uses the ASIS
17245 tree for the input source and thus requires the input to be syntactically and
17246 semantically legal.
17247 If this condition is not met, @command{gnatmetric} will generate
17248 an error message; no metric information for this file will be
17249 computed and reported.
17250
17251 If the compilation unit contained in the input source depends semantically
17252 upon units in files located outside the current directory, you have to provide
17253 the source search path when invoking @command{gnatmetric}.
17254 If it depends semantically upon units that are contained
17255 in files with names that do not follow the GNAT file naming rules, you have to
17256 provide the configuration file describing the corresponding naming scheme (see
17257 the description of the @command{gnatmetric} switches below.)
17258 Alternatively, you may use a project file and invoke @command{gnatmetric}
17259 through the @command{gnat} driver.
17260
17261 The @command{gnatmetric} command has the form
17262
17263 @smallexample
17264 @c $ gnatmetric @ovar{switches} @{@var{filename}@} @r{[}-cargs @var{gcc_switches}@r{]}
17265 @c Expanding @ovar macro inline (explanation in macro def comments)
17266 $ gnatmetric @r{[}@var{switches}@r{]} @{@var{filename}@} @r{[}-cargs @var{gcc_switches}@r{]}
17267 @end smallexample
17268
17269 @noindent
17270 where
17271 @itemize @bullet
17272 @item
17273 @var{switches} specify the metrics to compute and define the destination for
17274 the output
17275
17276 @item
17277 Each @var{filename} is the name (including the extension) of a source
17278 file to process. ``Wildcards'' are allowed, and
17279 the file name may contain path information.
17280 If no @var{filename} is supplied, then the @var{switches} list must contain
17281 at least one
17282 @option{-files} switch (@pxref{Other gnatmetric Switches}).
17283 Including both a @option{-files} switch and one or more
17284 @var{filename} arguments is permitted.
17285
17286 @item
17287 @samp{-cargs @var{gcc_switches}} is a list of switches for
17288 @command{gcc}. They will be passed on to all compiler invocations made by
17289 @command{gnatmetric} to generate the ASIS trees. Here you can provide
17290 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
17291 and use the @option{-gnatec} switch to set the configuration file.
17292 @end itemize
17293
17294 @menu
17295 * Switches for gnatmetric::
17296 @end menu
17297
17298 @node Switches for gnatmetric
17299 @section Switches for @command{gnatmetric}
17300
17301 @noindent
17302 The following subsections describe the various switches accepted by
17303 @command{gnatmetric}, organized by category.
17304
17305 @menu
17306 * Output Files Control::
17307 * Disable Metrics For Local Units::
17308 * Specifying a set of metrics to compute::
17309 * Other gnatmetric Switches::
17310 * Generate project-wide metrics::
17311 @end menu
17312
17313 @node Output Files Control
17314 @subsection Output File Control
17315 @cindex Output file control in @command{gnatmetric}
17316
17317 @noindent
17318 @command{gnatmetric} has two output formats. It can generate a
17319 textual (human-readable) form, and also XML. By default only textual
17320 output is generated.
17321
17322 When generating the output in textual form, @command{gnatmetric} creates
17323 for each Ada source file a corresponding text file
17324 containing the computed metrics, except for the case when the set of metrics
17325 specified by gnatmetric parameters consists only of metrics that are computed
17326 for the whole set of analyzed sources, but not for each Ada source.
17327 By default, this file is placed in the same directory as where the source
17328 file is located, and its name is obtained
17329 by appending the ^@file{.metrix}^@file{$METRIX}^ suffix to the name of the
17330 input file.
17331
17332 All the output information generated in XML format is placed in a single
17333 file. By default this file is placed in the current directory and has the
17334 name ^@file{metrix.xml}^@file{METRIX$XML}^.
17335
17336 Some of the computed metrics are summed over the units passed to
17337 @command{gnatmetric}; for example, the total number of lines of code.
17338 By default this information is sent to @file{stdout}, but a file
17339 can be specified with the @option{-og} switch.
17340
17341 The following switches control the @command{gnatmetric} output:
17342
17343 @table @option
17344 @cindex @option{^-x^/XML^} (@command{gnatmetric})
17345 @item ^-x^/XML^
17346 Generate the XML output
17347
17348 @cindex @option{^-xs^/XSD^} (@command{gnatmetric})
17349 @item ^-xs^/XSD^
17350 Generate the XML output and the XML schema file that describes the structure
17351 of the XML metric report, this schema is assigned to the XML file. The schema
17352 file has the same name as the XML output file with @file{.xml} suffix replaced
17353 with @file{.xsd}
17354
17355 @cindex @option{^-nt^/NO_TEXT^} (@command{gnatmetric})
17356 @item ^-nt^/NO_TEXT^
17357 Do not generate the output in text form (implies @option{^-x^/XML^})
17358
17359 @cindex @option{^-d^/DIRECTORY^} (@command{gnatmetric})
17360 @item ^-d @var{output_dir}^/DIRECTORY=@var{output_dir}^
17361 Put text files with detailed metrics into @var{output_dir}
17362
17363 @cindex @option{^-o^/SUFFIX_DETAILS^} (@command{gnatmetric})
17364 @item ^-o @var{file_suffix}^/SUFFIX_DETAILS=@var{file_suffix}^
17365 Use @var{file_suffix}, instead of ^@file{.metrix}^@file{$METRIX}^
17366 in the name of the output file.
17367
17368 @cindex @option{^-og^/GLOBAL_OUTPUT^} (@command{gnatmetric})
17369 @item ^-og @var{file_name}^/GLOBAL_OUTPUT=@var{file_name}^
17370 Put global metrics into @var{file_name}
17371
17372 @cindex @option{^-ox^/XML_OUTPUT^} (@command{gnatmetric})
17373 @item ^-ox @var{file_name}^/XML_OUTPUT=@var{file_name}^
17374 Put the XML output into @var{file_name} (also implies @option{^-x^/XML^})
17375
17376 @cindex @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} (@command{gnatmetric})
17377 @item ^-sfn^/SHORT_SOURCE_FILE_NAME^
17378 Use ``short'' source file names in the output. (The @command{gnatmetric}
17379 output includes the name(s) of the Ada source file(s) from which the metrics
17380 are computed. By default each name includes the absolute path. The
17381 @option{^-sfn^/SHORT_SOURCE_FILE_NAME^} switch causes @command{gnatmetric}
17382 to exclude all directory information from the file names that are output.)
17383
17384 @end table
17385
17386 @node Disable Metrics For Local Units
17387 @subsection Disable Metrics For Local Units
17388 @cindex Disable Metrics For Local Units in @command{gnatmetric}
17389
17390 @noindent
17391 @command{gnatmetric} relies on the GNAT compilation model @minus{}
17392 one compilation
17393 unit per one source file. It computes line metrics for the whole source
17394 file, and it also computes syntax
17395 and complexity metrics for the file's outermost unit.
17396
17397 By default, @command{gnatmetric} will also compute all metrics for certain
17398 kinds of locally declared program units:
17399
17400 @itemize @bullet
17401 @item
17402 subprogram (and generic subprogram) bodies;
17403
17404 @item
17405 package (and generic package) specs and bodies;
17406
17407 @item
17408 task object and type specifications and bodies;
17409
17410 @item
17411 protected object and type specifications and bodies.
17412 @end itemize
17413
17414 @noindent
17415 These kinds of entities will be referred to as
17416 @emph{eligible local program units}, or simply @emph{eligible local units},
17417 @cindex Eligible local unit (for @command{gnatmetric})
17418 in the discussion below.
17419
17420 Note that a subprogram declaration, generic instantiation,
17421 or renaming declaration only receives metrics
17422 computation when it appear as the outermost entity
17423 in a source file.
17424
17425 Suppression of metrics computation for eligible local units can be
17426 obtained via the following switch:
17427
17428 @table @option
17429 @cindex @option{^-n@var{x}^/SUPPRESS^} (@command{gnatmetric})
17430 @item ^-nolocal^/SUPPRESS=LOCAL_DETAILS^
17431 Do not compute detailed metrics for eligible local program units
17432
17433 @end table
17434
17435 @node Specifying a set of metrics to compute
17436 @subsection Specifying a set of metrics to compute
17437
17438 @noindent
17439 By default all the metrics are computed and reported. The switches
17440 described in this subsection allow you to control, on an individual
17441 basis, whether metrics are computed and
17442 reported. If at least one positive metric
17443 switch is specified (that is, a switch that defines that a given
17444 metric or set of metrics is to be computed), then only
17445 explicitly specified metrics are reported.
17446
17447 @menu
17448 * Line Metrics Control::
17449 * Syntax Metrics Control::
17450 * Complexity Metrics Control::
17451 * Object-Oriented Metrics Control::
17452 @end menu
17453
17454 @node Line Metrics Control
17455 @subsubsection Line Metrics Control
17456 @cindex Line metrics control in @command{gnatmetric}
17457
17458 @noindent
17459 For any (legal) source file, and for each of its
17460 eligible local program units, @command{gnatmetric} computes the following
17461 metrics:
17462
17463 @itemize @bullet
17464 @item
17465 the total number of lines;
17466
17467 @item
17468 the total number of code lines (i.e., non-blank lines that are not comments)
17469
17470 @item
17471 the number of comment lines
17472
17473 @item
17474 the number of code lines containing end-of-line comments;
17475
17476 @item
17477 the comment percentage: the ratio between the number of lines that contain
17478 comments and the number of all non-blank lines, expressed as a percentage;
17479
17480 @item
17481 the number of empty lines and lines containing only space characters and/or
17482 format effectors (blank lines)
17483
17484 @item
17485 the average number of code lines in subprogram bodies, task bodies, entry
17486 bodies and statement sequences in package bodies (this metric is only computed
17487 across the whole set of the analyzed units)
17488
17489 @end itemize
17490
17491 @noindent
17492 @command{gnatmetric} sums the values of the line metrics for all the
17493 files being processed and then generates the cumulative results. The tool
17494 also computes for all the files being processed the average number of code
17495 lines in bodies.
17496
17497 You can use the following switches to select the specific line metrics
17498 to be computed and reported.
17499
17500 @table @option
17501 @cindex @option{^--lines@var{x}^/LINE_COUNT_METRICS^} (@command{gnatmetric})
17502
17503 @ifclear vms
17504 @cindex @option{--no-lines@var{x}}
17505 @end ifclear
17506
17507 @item ^--lines-all^/LINE_COUNT_METRICS=ALL^
17508 Report all the line metrics
17509
17510 @item ^--no-lines-all^/LINE_COUNT_METRICS=NONE^
17511 Do not report any of line metrics
17512
17513 @item ^--lines^/LINE_COUNT_METRICS=ALL_LINES^
17514 Report the number of all lines
17515
17516 @item ^--no-lines^/LINE_COUNT_METRICS=NOALL_LINES^
17517 Do not report the number of all lines
17518
17519 @item ^--lines-code^/LINE_COUNT_METRICS=CODE_LINES^
17520 Report the number of code lines
17521
17522 @item ^--no-lines-code^/LINE_COUNT_METRICS=NOCODE_LINES^
17523 Do not report the number of code lines
17524
17525 @item ^--lines-comment^/LINE_COUNT_METRICS=COMMENT_LINES^
17526 Report the number of comment lines
17527
17528 @item ^--no-lines-comment^/LINE_COUNT_METRICS=NOCOMMENT_LINES^
17529 Do not report the number of comment lines
17530
17531 @item ^--lines-eol-comment^/LINE_COUNT_METRICS=CODE_COMMENT_LINES^
17532 Report the number of code lines containing
17533 end-of-line comments
17534
17535 @item ^--no-lines-eol-comment^/LINE_COUNT_METRICS=NOCODE_COMMENT_LINES^
17536 Do not report the number of code lines containing
17537 end-of-line comments
17538
17539 @item ^--lines-ratio^/LINE_COUNT_METRICS=COMMENT_PERCENTAGE^
17540 Report the comment percentage in the program text
17541
17542 @item ^--no-lines-ratio^/LINE_COUNT_METRICS=NOCOMMENT_PERCENTAGE^
17543 Do not report the comment percentage in the program text
17544
17545 @item ^--lines-blank^/LINE_COUNT_METRICS=BLANK_LINES^
17546 Report the number of blank lines
17547
17548 @item ^--no-lines-blank^/LINE_COUNT_METRICS=NOBLANK_LINES^
17549 Do not report the number of blank lines
17550
17551 @item ^--lines-average^/LINE_COUNT_METRICS=AVERAGE_BODY_LINES^
17552 Report the average number of code lines in subprogram bodies, task bodies,
17553 entry bodies and statement sequences in package bodies. The metric is computed
17554 and reported for the whole set of processed Ada sources only.
17555
17556 @item ^--no-lines-average^/LINE_COUNT_METRICS=NOAVERAGE_BODY_LINES^
17557 Do not report the average number of code lines in subprogram bodies,
17558 task bodies, entry bodies and statement sequences in package bodies.
17559
17560 @end table
17561
17562 @node Syntax Metrics Control
17563 @subsubsection Syntax Metrics Control
17564 @cindex Syntax metrics control in @command{gnatmetric}
17565
17566 @noindent
17567 @command{gnatmetric} computes various syntactic metrics for the
17568 outermost unit and for each eligible local unit:
17569
17570 @table @emph
17571 @item LSLOC (``Logical Source Lines Of Code'')
17572 The total number of declarations and the total number of statements
17573
17574 @item Maximal static nesting level of inner program units
17575 According to
17576 @cite{Ada Reference Manual}, 10.1(1), ``A program unit is either a
17577 package, a task unit, a protected unit, a
17578 protected entry, a generic unit, or an explicitly declared subprogram other
17579 than an enumeration literal.''
17580
17581 @item Maximal nesting level of composite syntactic constructs
17582 This corresponds to the notion of the
17583 maximum nesting level in the GNAT built-in style checks
17584 (@pxref{Style Checking})
17585 @end table
17586
17587 @noindent
17588 For the outermost unit in the file, @command{gnatmetric} additionally computes
17589 the following metrics:
17590
17591 @table @emph
17592 @item Public subprograms
17593 This metric is computed for package specs. It is the
17594 number of subprograms and generic subprograms declared in the visible
17595 part (including the visible part of nested packages, protected objects, and
17596 protected types).
17597
17598 @item All subprograms
17599 This metric is computed for bodies and subunits. The
17600 metric is equal to a total number of subprogram bodies in the compilation
17601 unit.
17602 Neither generic instantiations nor renamings-as-a-body nor body stubs
17603 are counted. Any subprogram body is counted, independently of its nesting
17604 level and enclosing constructs. Generic bodies and bodies of protected
17605 subprograms are counted in the same way as ``usual'' subprogram bodies.
17606
17607 @item Public types
17608 This metric is computed for package specs and
17609 generic package declarations. It is the total number of types
17610 that can be referenced from outside this compilation unit, plus the
17611 number of types from all the visible parts of all the visible generic
17612 packages. Generic formal types are not counted. Only types, not subtypes,
17613 are included.
17614
17615 @noindent
17616 Along with the total number of public types, the following
17617 types are counted and reported separately:
17618
17619 @itemize @bullet
17620 @item
17621 Abstract types
17622
17623 @item
17624 Root tagged types (abstract, non-abstract, private, non-private). Type
17625 extensions are @emph{not} counted
17626
17627 @item
17628 Private types (including private extensions)
17629
17630 @item
17631 Task types
17632
17633 @item
17634 Protected types
17635
17636 @end itemize
17637
17638 @item All types
17639 This metric is computed for any compilation unit. It is equal to the total
17640 number of the declarations of different types given in the compilation unit.
17641 The private and the corresponding full type declaration are counted as one
17642 type declaration. Incomplete type declarations and generic formal types
17643 are not counted.
17644 No distinction is made among different kinds of types (abstract,
17645 private etc.); the total number of types is computed and reported.
17646
17647 @end table
17648
17649 @noindent
17650 By default, all the syntax metrics are computed and reported. You can use the
17651 following switches to select specific syntax metrics.
17652
17653 @table @option
17654
17655 @cindex @option{^--syntax@var{x}^/SYNTAX_METRICS^} (@command{gnatmetric})
17656
17657 @ifclear vms
17658 @cindex @option{--no-syntax@var{x}} (@command{gnatmetric})
17659 @end ifclear
17660
17661 @item ^--syntax-all^/SYNTAX_METRICS=ALL^
17662 Report all the syntax metrics
17663
17664 @item ^--no-syntax-all^/SYNTAX_METRICS=NONE^
17665 Do not report any of syntax metrics
17666
17667 @item ^--declarations^/SYNTAX_METRICS=DECLARATIONS^
17668 Report the total number of declarations
17669
17670 @item ^--no-declarations^/SYNTAX_METRICS=NODECLARATIONS^
17671 Do not report the total number of declarations
17672
17673 @item ^--statements^/SYNTAX_METRICS=STATEMENTS^
17674 Report the total number of statements
17675
17676 @item ^--no-statements^/SYNTAX_METRICS=NOSTATEMENTS^
17677 Do not report the total number of statements
17678
17679 @item ^--public-subprograms^/SYNTAX_METRICS=PUBLIC_SUBPROGRAMS^
17680 Report the number of public subprograms in a compilation unit
17681
17682 @item ^--no-public-subprograms^/SYNTAX_METRICS=NOPUBLIC_SUBPROGRAMS^
17683 Do not report the number of public subprograms in a compilation unit
17684
17685 @item ^--all-subprograms^/SYNTAX_METRICS=ALL_SUBPROGRAMS^
17686 Report the number of all the subprograms in a compilation unit
17687
17688 @item ^--no-all-subprograms^/SYNTAX_METRICS=NOALL_SUBPROGRAMS^
17689 Do not report the number of all the subprograms in a compilation unit
17690
17691 @item ^--public-types^/SYNTAX_METRICS=PUBLIC_TYPES^
17692 Report the number of public types in a compilation unit
17693
17694 @item ^--no-public-types^/SYNTAX_METRICS=NOPUBLIC_TYPES^
17695 Do not report the number of public types in a compilation unit
17696
17697 @item ^--all-types^/SYNTAX_METRICS=ALL_TYPES^
17698 Report the number of all the types in a compilation unit
17699
17700 @item ^--no-all-types^/SYNTAX_METRICS=NOALL_TYPES^
17701 Do not report the number of all the types in a compilation unit
17702
17703 @item ^--unit-nesting^/SYNTAX_METRICS=UNIT_NESTING^
17704 Report the maximal program unit nesting level
17705
17706 @item ^--no-unit-nesting^/SYNTAX_METRICS=UNIT_NESTING_OFF^
17707 Do not report the maximal program unit nesting level
17708
17709 @item ^--construct-nesting^/SYNTAX_METRICS=CONSTRUCT_NESTING^
17710 Report the maximal construct nesting level
17711
17712 @item ^--no-construct-nesting^/SYNTAX_METRICS=NOCONSTRUCT_NESTING^
17713 Do not report the maximal construct nesting level
17714
17715 @end table
17716
17717 @node Complexity Metrics Control
17718 @subsubsection Complexity Metrics Control
17719 @cindex Complexity metrics control in @command{gnatmetric}
17720
17721 @noindent
17722 For a program unit that is an executable body (a subprogram body (including
17723 generic bodies), task body, entry body or a package body containing
17724 its own statement sequence) @command{gnatmetric} computes the following
17725 complexity metrics:
17726
17727 @itemize @bullet
17728 @item
17729 McCabe cyclomatic complexity;
17730
17731 @item
17732 McCabe essential complexity;
17733
17734 @item
17735 maximal loop nesting level
17736
17737 @end itemize
17738
17739 @noindent
17740 The McCabe complexity metrics are defined
17741 in @url{http://www.mccabe.com/pdf/nist235r.pdf}
17742
17743 According to McCabe, both control statements and short-circuit control forms
17744 should be taken into account when computing cyclomatic complexity. For each
17745 body, we compute three metric values:
17746
17747 @itemize @bullet
17748 @item
17749 the complexity introduced by control
17750 statements only, without taking into account short-circuit forms,
17751
17752 @item
17753 the complexity introduced by short-circuit control forms only, and
17754
17755 @item
17756 the total
17757 cyclomatic complexity, which is the sum of these two values.
17758 @end itemize
17759
17760 @noindent
17761 When computing cyclomatic and essential complexity, @command{gnatmetric} skips
17762 the code in the exception handlers and in all the nested program units.
17763
17764 By default, all the complexity metrics are computed and reported.
17765 For more fine-grained control you can use
17766 the following switches:
17767
17768 @table @option
17769 @cindex @option{^-complexity@var{x}^/COMPLEXITY_METRICS^} (@command{gnatmetric})
17770
17771 @ifclear vms
17772 @cindex @option{--no-complexity@var{x}}
17773 @end ifclear
17774
17775 @item ^--complexity-all^/COMPLEXITY_METRICS=ALL^
17776 Report all the complexity metrics
17777
17778 @item ^--no-complexity-all^/COMPLEXITY_METRICS=NONE^
17779 Do not report any of complexity metrics
17780
17781 @item ^--complexity-cyclomatic^/COMPLEXITY_METRICS=CYCLOMATIC^
17782 Report the McCabe Cyclomatic Complexity
17783
17784 @item ^--no-complexity-cyclomatic^/COMPLEXITY_METRICS=NOCYCLOMATIC^
17785 Do not report the McCabe Cyclomatic Complexity
17786
17787 @item ^--complexity-essential^/COMPLEXITY_METRICS=ESSENTIAL^
17788 Report the Essential Complexity
17789
17790 @item ^--no-complexity-essential^/COMPLEXITY_METRICS=NOESSENTIAL^
17791 Do not report the Essential Complexity
17792
17793 @item ^--loop-nesting^/COMPLEXITY_METRICS=LOOP_NESTING_ON^
17794 Report maximal loop nesting level
17795
17796 @item ^--no-loop-nesting^/COMPLEXITY_METRICS=NOLOOP_NESTING^
17797 Do not report maximal loop nesting level
17798
17799 @item ^--complexity-average^/COMPLEXITY_METRICS=AVERAGE_COMPLEXITY^
17800 Report the average McCabe Cyclomatic Complexity for all the subprogram bodies,
17801 task bodies, entry bodies and statement sequences in package bodies.
17802 The metric is computed and reported for whole set of processed Ada sources
17803 only.
17804
17805 @item ^--no-complexity-average^/COMPLEXITY_METRICS=NOAVERAGE_COMPLEXITY^
17806 Do not report the average McCabe Cyclomatic Complexity for all the subprogram
17807 bodies, task bodies, entry bodies and statement sequences in package bodies
17808
17809 @cindex @option{^-ne^/NO_EXITS_AS_GOTOS^} (@command{gnatmetric})
17810 @item ^-ne^/NO_EXITS_AS_GOTOS^
17811 Do not consider @code{exit} statements as @code{goto}s when
17812 computing Essential Complexity
17813
17814 @item ^--extra-exit-points^/EXTRA_EXIT_POINTS^
17815 Report the extra exit points for subprogram bodies. As an exit point, this
17816 metric counts @code{return} statements and raise statements in case when the
17817 raised exception is not handled in the same body. In case of a function this
17818 metric subtracts 1 from the number of exit points, because a function body
17819 must contain at least one @code{return} statement.
17820
17821 @item ^--no-extra-exit-points^/NOEXTRA_EXIT_POINTS^
17822 Do not report the extra exit points for subprogram bodies
17823 @end table
17824
17825
17826 @node Object-Oriented Metrics Control
17827 @subsubsection Object-Oriented Metrics Control
17828 @cindex Object-Oriented metrics control in @command{gnatmetric}
17829
17830 @noindent
17831 @cindex Coupling metrics (in in @command{gnatmetric})
17832 Coupling metrics are object-oriented metrics that measure the
17833 dependencies between a given class (or a group of classes) and the
17834 ``external world'' (that is, the other classes in the program). In this
17835 subsection the term ``class'' is used in its
17836 traditional object-oriented programming sense
17837 (an instantiable module that contains data and/or method members).
17838 A @emph{category} (of classes)
17839 is a group of closely related classes that are reused and/or
17840 modified together.
17841
17842 A class @code{K}'s @emph{efferent coupling} is the number of classes
17843 that @code{K} depends upon.
17844 A category's efferent coupling is the number of classes outside the
17845 category that the classes inside the category depend upon.
17846
17847 A class @code{K}'s @emph{afferent coupling} is the number of classes
17848 that depend upon @code{K}.
17849 A category's afferent coupling is the number of classes outside the
17850 category that depend on classes belonging to the category.
17851
17852 Ada's implementation of the object-oriented paradigm does not use the
17853 traditional class notion, so the definition of the coupling
17854 metrics for Ada maps the class and class category notions
17855 onto Ada constructs.
17856
17857 For the coupling metrics, several kinds of modules -- a library package,
17858 a library generic package, and a library generic package instantiation --
17859 that define a tagged type or an interface type are
17860 considered to be a class. A category consists of a library package (or
17861 a library generic package) that defines a tagged or an interface type,
17862 together with all its descendant (generic) packages that define tagged
17863 or interface types. For any package counted as a class,
17864 its body and subunits (if any) are considered
17865 together with its spec when counting the dependencies, and coupling
17866 metrics are reported for spec units only. For dependencies
17867 between classes, the Ada semantic dependencies are considered.
17868 For coupling metrics, only dependencies on units that are considered as
17869 classes, are considered.
17870
17871 When computing coupling metrics, @command{gnatmetric} counts only
17872 dependencies between units that are arguments of the gnatmetric call.
17873 Coupling metrics are program-wide (or project-wide) metrics, so to
17874 get a valid result, you should call @command{gnatmetric} for
17875 the whole set of sources that make up your program. It can be done
17876 by calling @command{gnatmetric} from the GNAT driver with @option{-U}
17877 option (see See @ref{The GNAT Driver and Project Files} for details.
17878
17879 By default, all the coupling metrics are disabled. You can use the following
17880 switches to specify the coupling metrics to be computed and reported:
17881
17882 @table @option
17883
17884 @ifclear vms
17885 @cindex @option{--package@var{x}} (@command{gnatmetric})
17886 @cindex @option{--no-package@var{x}} (@command{gnatmetric})
17887 @cindex @option{--category@var{x}} (@command{gnatmetric})
17888 @cindex @option{--no-category@var{x}} (@command{gnatmetric})
17889 @end ifclear
17890
17891 @ifset vms
17892 @cindex @option{/COUPLING_METRICS} (@command{gnatmetric})
17893 @end ifset
17894
17895 @item ^--coupling-all^/COUPLING_METRICS=ALL^
17896 Report all the coupling metrics
17897
17898 @item ^--no-coupling-all^/COUPLING_METRICS=NONE^
17899 Do not report any of metrics
17900
17901 @item ^--package-efferent-coupling^/COUPLING_METRICS=PACKAGE_EFFERENT^
17902 Report package efferent coupling
17903
17904 @item ^--no-package-efferent-coupling^/COUPLING_METRICS=NOPACKAGE_EFFERENT^
17905 Do not report package efferent coupling
17906
17907 @item ^--package-afferent-coupling^/COUPLING_METRICS=PACKAGE_AFFERENT^
17908 Report package afferent coupling
17909
17910 @item ^--no-package-afferent-coupling^/COUPLING_METRICS=NOPACKAGE_AFFERENT^
17911 Do not report package afferent coupling
17912
17913 @item ^--category-efferent-coupling^/COUPLING_METRICS=CATEGORY_EFFERENT^
17914 Report category efferent coupling
17915
17916 @item ^--no-category-efferent-coupling^/COUPLING_METRICS=NOCATEGORY_EFFERENT^
17917 Do not report category efferent coupling
17918
17919 @item ^--category-afferent-coupling^/COUPLING_METRICS=CATEGORY_AFFERENT^
17920 Report category afferent coupling
17921
17922 @item ^--no-category-afferent-coupling^/COUPLING_METRICS=NOCATEGORY_AFFERENT^
17923 Do not report category afferent coupling
17924
17925 @end table
17926
17927 @node Other gnatmetric Switches
17928 @subsection Other @code{gnatmetric} Switches
17929
17930 @noindent
17931 Additional @command{gnatmetric} switches are as follows:
17932
17933 @table @option
17934 @item ^-files @var{filename}^/FILES=@var{filename}^
17935 @cindex @option{^-files^/FILES^} (@code{gnatmetric})
17936 Take the argument source files from the specified file. This file should be an
17937 ordinary text file containing file names separated by spaces or
17938 line breaks. You can use this switch more than once in the same call to
17939 @command{gnatmetric}. You also can combine this switch with
17940 an explicit list of files.
17941
17942 @item ^-v^/VERBOSE^
17943 @cindex @option{^-v^/VERBOSE^} (@code{gnatmetric})
17944 Verbose mode;
17945 @command{gnatmetric} generates version information and then
17946 a trace of sources being processed.
17947
17948 @item ^-dv^/DEBUG_OUTPUT^
17949 @cindex @option{^-dv^/DEBUG_OUTPUT^} (@code{gnatmetric})
17950 Debug mode;
17951 @command{gnatmetric} generates various messages useful to understand what
17952 happens during the metrics computation
17953
17954 @item ^-q^/QUIET^
17955 @cindex @option{^-q^/QUIET^} (@code{gnatmetric})
17956 Quiet mode.
17957 @end table
17958
17959 @node Generate project-wide metrics
17960 @subsection Generate project-wide metrics
17961
17962 In order to compute metrics on all units of a given project, you can use
17963 the @command{gnat} driver along with the @option{-P} option:
17964 @smallexample
17965 gnat metric -Pproj
17966 @end smallexample
17967
17968 @noindent
17969 If the project @code{proj} depends upon other projects, you can compute
17970 the metrics on the project closure using the @option{-U} option:
17971 @smallexample
17972 gnat metric -Pproj -U
17973 @end smallexample
17974
17975 @noindent
17976 Finally, if not all the units are relevant to a particular main
17977 program in the project closure, you can generate metrics for the set
17978 of units needed to create a given main program (unit closure) using
17979 the @option{-U} option followed by the name of the main unit:
17980 @smallexample
17981 gnat metric -Pproj -U main
17982 @end smallexample
17983
17984
17985 @c ***********************************
17986 @node File Name Krunching Using gnatkr
17987 @chapter File Name Krunching Using @code{gnatkr}
17988 @findex gnatkr
17989
17990 @noindent
17991 This chapter discusses the method used by the compiler to shorten
17992 the default file names chosen for Ada units so that they do not
17993 exceed the maximum length permitted. It also describes the
17994 @code{gnatkr} utility that can be used to determine the result of
17995 applying this shortening.
17996 @menu
17997 * About gnatkr::
17998 * Using gnatkr::
17999 * Krunching Method::
18000 * Examples of gnatkr Usage::
18001 @end menu
18002
18003 @node About gnatkr
18004 @section About @code{gnatkr}
18005
18006 @noindent
18007 The default file naming rule in GNAT
18008 is that the file name must be derived from
18009 the unit name. The exact default rule is as follows:
18010 @itemize @bullet
18011 @item
18012 Take the unit name and replace all dots by hyphens.
18013 @item
18014 If such a replacement occurs in the
18015 second character position of a name, and the first character is
18016 ^@samp{a}, @samp{g}, @samp{s}, or @samp{i}, ^@samp{A}, @samp{G}, @samp{S}, or @samp{I},^
18017 then replace the dot by the character
18018 ^@samp{~} (tilde)^@samp{$} (dollar sign)^
18019 instead of a minus.
18020 @end itemize
18021 The reason for this exception is to avoid clashes
18022 with the standard names for children of System, Ada, Interfaces,
18023 and GNAT, which use the prefixes
18024 ^@samp{s-}, @samp{a-}, @samp{i-}, and @samp{g-},^@samp{S-}, @samp{A-}, @samp{I-}, and @samp{G-},^
18025 respectively.
18026
18027 The @option{^-gnatk^/FILE_NAME_MAX_LENGTH=^@var{nn}}
18028 switch of the compiler activates a ``krunching''
18029 circuit that limits file names to nn characters (where nn is a decimal
18030 integer). For example, using OpenVMS,
18031 where the maximum file name length is
18032 39, the value of nn is usually set to 39, but if you want to generate
18033 a set of files that would be usable if ported to a system with some
18034 different maximum file length, then a different value can be specified.
18035 The default value of 39 for OpenVMS need not be specified.
18036
18037 The @code{gnatkr} utility can be used to determine the krunched name for
18038 a given file, when krunched to a specified maximum length.
18039
18040 @node Using gnatkr
18041 @section Using @code{gnatkr}
18042
18043 @noindent
18044 The @code{gnatkr} command has the form
18045
18046 @ifclear vms
18047 @smallexample
18048 @c $ gnatkr @var{name} @ovar{length}
18049 @c Expanding @ovar macro inline (explanation in macro def comments)
18050 $ gnatkr @var{name} @r{[}@var{length}@r{]}
18051 @end smallexample
18052 @end ifclear
18053
18054 @ifset vms
18055 @smallexample
18056 $ gnatkr @var{name} /COUNT=nn
18057 @end smallexample
18058 @end ifset
18059
18060 @noindent
18061 @var{name} is the uncrunched file name, derived from the name of the unit
18062 in the standard manner described in the previous section (i.e., in particular
18063 all dots are replaced by hyphens). The file name may or may not have an
18064 extension (defined as a suffix of the form period followed by arbitrary
18065 characters other than period). If an extension is present then it will
18066 be preserved in the output. For example, when krunching @file{hellofile.ads}
18067 to eight characters, the result will be hellofil.ads.
18068
18069 Note: for compatibility with previous versions of @code{gnatkr} dots may
18070 appear in the name instead of hyphens, but the last dot will always be
18071 taken as the start of an extension. So if @code{gnatkr} is given an argument
18072 such as @file{Hello.World.adb} it will be treated exactly as if the first
18073 period had been a hyphen, and for example krunching to eight characters
18074 gives the result @file{hellworl.adb}.
18075
18076 Note that the result is always all lower case (except on OpenVMS where it is
18077 all upper case). Characters of the other case are folded as required.
18078
18079 @var{length} represents the length of the krunched name. The default
18080 when no argument is given is ^8^39^ characters. A length of zero stands for
18081 unlimited, in other words do not chop except for system files where the
18082 implied crunching length is always eight characters.
18083
18084 @noindent
18085 The output is the krunched name. The output has an extension only if the
18086 original argument was a file name with an extension.
18087
18088 @node Krunching Method
18089 @section Krunching Method
18090
18091 @noindent
18092 The initial file name is determined by the name of the unit that the file
18093 contains. The name is formed by taking the full expanded name of the
18094 unit and replacing the separating dots with hyphens and
18095 using ^lowercase^uppercase^
18096 for all letters, except that a hyphen in the second character position is
18097 replaced by a ^tilde^dollar sign^ if the first character is
18098 ^@samp{a}, @samp{i}, @samp{g}, or @samp{s}^@samp{A}, @samp{I}, @samp{G}, or @samp{S}^.
18099 The extension is @code{.ads} for a
18100 spec and @code{.adb} for a body.
18101 Krunching does not affect the extension, but the file name is shortened to
18102 the specified length by following these rules:
18103
18104 @itemize @bullet
18105 @item
18106 The name is divided into segments separated by hyphens, tildes or
18107 underscores and all hyphens, tildes, and underscores are
18108 eliminated. If this leaves the name short enough, we are done.
18109
18110 @item
18111 If the name is too long, the longest segment is located (left-most
18112 if there are two of equal length), and shortened by dropping
18113 its last character. This is repeated until the name is short enough.
18114
18115 As an example, consider the krunching of @*@file{our-strings-wide_fixed.adb}
18116 to fit the name into 8 characters as required by some operating systems.
18117
18118 @smallexample
18119 our-strings-wide_fixed 22
18120 our strings wide fixed 19
18121 our string wide fixed 18
18122 our strin wide fixed 17
18123 our stri wide fixed 16
18124 our stri wide fixe 15
18125 our str wide fixe 14
18126 our str wid fixe 13
18127 our str wid fix 12
18128 ou str wid fix 11
18129 ou st wid fix 10
18130 ou st wi fix 9
18131 ou st wi fi 8
18132 Final file name: oustwifi.adb
18133 @end smallexample
18134
18135 @item
18136 The file names for all predefined units are always krunched to eight
18137 characters. The krunching of these predefined units uses the following
18138 special prefix replacements:
18139
18140 @table @file
18141 @item ada-
18142 replaced by @file{^a^A^-}
18143
18144 @item gnat-
18145 replaced by @file{^g^G^-}
18146
18147 @item interfaces-
18148 replaced by @file{^i^I^-}
18149
18150 @item system-
18151 replaced by @file{^s^S^-}
18152 @end table
18153
18154 These system files have a hyphen in the second character position. That
18155 is why normal user files replace such a character with a
18156 ^tilde^dollar sign^, to
18157 avoid confusion with system file names.
18158
18159 As an example of this special rule, consider
18160 @*@file{ada-strings-wide_fixed.adb}, which gets krunched as follows:
18161
18162 @smallexample
18163 ada-strings-wide_fixed 22
18164 a- strings wide fixed 18
18165 a- string wide fixed 17
18166 a- strin wide fixed 16
18167 a- stri wide fixed 15
18168 a- stri wide fixe 14
18169 a- str wide fixe 13
18170 a- str wid fixe 12
18171 a- str wid fix 11
18172 a- st wid fix 10
18173 a- st wi fix 9
18174 a- st wi fi 8
18175 Final file name: a-stwifi.adb
18176 @end smallexample
18177 @end itemize
18178
18179 Of course no file shortening algorithm can guarantee uniqueness over all
18180 possible unit names, and if file name krunching is used then it is your
18181 responsibility to ensure that no name clashes occur. The utility
18182 program @code{gnatkr} is supplied for conveniently determining the
18183 krunched name of a file.
18184
18185 @node Examples of gnatkr Usage
18186 @section Examples of @code{gnatkr} Usage
18187
18188 @smallexample
18189 @iftex
18190 @leftskip=0cm
18191 @end iftex
18192 @ifclear vms
18193 $ gnatkr very_long_unit_name.ads --> velounna.ads
18194 $ gnatkr grandparent-parent-child.ads --> grparchi.ads
18195 $ gnatkr Grandparent.Parent.Child.ads --> grparchi.ads
18196 $ gnatkr grandparent-parent-child --> grparchi
18197 @end ifclear
18198 $ gnatkr very_long_unit_name.ads/count=6 --> vlunna.ads
18199 $ gnatkr very_long_unit_name.ads/count=0 --> very_long_unit_name.ads
18200 @end smallexample
18201
18202 @node Preprocessing Using gnatprep
18203 @chapter Preprocessing Using @code{gnatprep}
18204 @findex gnatprep
18205
18206 @noindent
18207 This chapter discusses how to use GNAT's @code{gnatprep} utility for simple
18208 preprocessing.
18209 Although designed for use with GNAT, @code{gnatprep} does not depend on any
18210 special GNAT features.
18211 For further discussion of conditional compilation in general, see
18212 @ref{Conditional Compilation}.
18213
18214 @menu
18215 * Preprocessing Symbols::
18216 * Using gnatprep::
18217 * Switches for gnatprep::
18218 * Form of Definitions File::
18219 * Form of Input Text for gnatprep::
18220 @end menu
18221
18222 @node Preprocessing Symbols
18223 @section Preprocessing Symbols
18224
18225 @noindent
18226 Preprocessing symbols are defined in definition files and referred to in
18227 sources to be preprocessed. A Preprocessing symbol is an identifier, following
18228 normal Ada (case-insensitive) rules for its syntax, with the restriction that
18229 all characters need to be in the ASCII set (no accented letters).
18230
18231 @node Using gnatprep
18232 @section Using @code{gnatprep}
18233
18234 @noindent
18235 To call @code{gnatprep} use
18236
18237 @smallexample
18238 @c $ gnatprep @ovar{switches} @var{infile} @var{outfile} @ovar{deffile}
18239 @c Expanding @ovar macro inline (explanation in macro def comments)
18240 $ gnatprep @r{[}@var{switches}@r{]} @var{infile} @var{outfile} @r{[}@var{deffile}@r{]}
18241 @end smallexample
18242
18243 @noindent
18244 where
18245 @table @var
18246 @item switches
18247 is an optional sequence of switches as described in the next section.
18248
18249 @item infile
18250 is the full name of the input file, which is an Ada source
18251 file containing preprocessor directives.
18252
18253 @item outfile
18254 is the full name of the output file, which is an Ada source
18255 in standard Ada form. When used with GNAT, this file name will
18256 normally have an ads or adb suffix.
18257
18258 @item deffile
18259 is the full name of a text file containing definitions of
18260 preprocessing symbols to be referenced by the preprocessor. This argument is
18261 optional, and can be replaced by the use of the @option{-D} switch.
18262
18263 @end table
18264
18265 @node Switches for gnatprep
18266 @section Switches for @code{gnatprep}
18267
18268 @table @option
18269 @c !sort!
18270
18271 @item ^-b^/BLANK_LINES^
18272 @cindex @option{^-b^/BLANK_LINES^} (@command{gnatprep})
18273 Causes both preprocessor lines and the lines deleted by
18274 preprocessing to be replaced by blank lines in the output source file,
18275 preserving line numbers in the output file.
18276
18277 @item ^-c^/COMMENTS^
18278 @cindex @option{^-c^/COMMENTS^} (@command{gnatprep})
18279 Causes both preprocessor lines and the lines deleted
18280 by preprocessing to be retained in the output source as comments marked
18281 with the special string @code{"--! "}. This option will result in line numbers
18282 being preserved in the output file.
18283
18284 @item ^-C^/REPLACE_IN_COMMENTS^
18285 @cindex @option{^-C^/REPLACE_IN_COMMENTS^} (@command{gnatprep})
18286 Causes comments to be scanned. Normally comments are ignored by gnatprep.
18287 If this option is specified, then comments are scanned and any $symbol
18288 substitutions performed as in program text. This is particularly useful
18289 when structured comments are used (e.g., when writing programs in the
18290 SPARK dialect of Ada). Note that this switch is not available when
18291 doing integrated preprocessing (it would be useless in this context
18292 since comments are ignored by the compiler in any case).
18293
18294 @item ^-Dsymbol=value^/ASSOCIATE="symbol=value"^
18295 @cindex @option{^-D^/ASSOCIATE^} (@command{gnatprep})
18296 Defines a new preprocessing symbol, associated with value. If no value is given
18297 on the command line, then symbol is considered to be @code{True}. This switch
18298 can be used in place of a definition file.
18299
18300 @ifset vms
18301 @item /REMOVE
18302 @cindex @option{/REMOVE} (@command{gnatprep})
18303 This is the default setting which causes lines deleted by preprocessing
18304 to be entirely removed from the output file.
18305 @end ifset
18306
18307 @item ^-r^/REFERENCE^
18308 @cindex @option{^-r^/REFERENCE^} (@command{gnatprep})
18309 Causes a @code{Source_Reference} pragma to be generated that
18310 references the original input file, so that error messages will use
18311 the file name of this original file. The use of this switch implies
18312 that preprocessor lines are not to be removed from the file, so its
18313 use will force @option{^-b^/BLANK_LINES^} mode if
18314 @option{^-c^/COMMENTS^}
18315 has not been specified explicitly.
18316
18317 Note that if the file to be preprocessed contains multiple units, then
18318 it will be necessary to @code{gnatchop} the output file from
18319 @code{gnatprep}. If a @code{Source_Reference} pragma is present
18320 in the preprocessed file, it will be respected by
18321 @code{gnatchop ^-r^/REFERENCE^}
18322 so that the final chopped files will correctly refer to the original
18323 input source file for @code{gnatprep}.
18324
18325 @item ^-s^/SYMBOLS^
18326 @cindex @option{^-s^/SYMBOLS^} (@command{gnatprep})
18327 Causes a sorted list of symbol names and values to be
18328 listed on the standard output file.
18329
18330 @item ^-u^/UNDEFINED^
18331 @cindex @option{^-u^/UNDEFINED^} (@command{gnatprep})
18332 Causes undefined symbols to be treated as having the value FALSE in the context
18333 of a preprocessor test. In the absence of this option, an undefined symbol in
18334 a @code{#if} or @code{#elsif} test will be treated as an error.
18335
18336 @end table
18337
18338 @ifclear vms
18339 @noindent
18340 Note: if neither @option{-b} nor @option{-c} is present,
18341 then preprocessor lines and
18342 deleted lines are completely removed from the output, unless -r is
18343 specified, in which case -b is assumed.
18344 @end ifclear
18345
18346 @node Form of Definitions File
18347 @section Form of Definitions File
18348
18349 @noindent
18350 The definitions file contains lines of the form
18351
18352 @smallexample
18353 symbol := value
18354 @end smallexample
18355
18356 @noindent
18357 where symbol is a preprocessing symbol, and value is one of the following:
18358
18359 @itemize @bullet
18360 @item
18361 Empty, corresponding to a null substitution
18362 @item
18363 A string literal using normal Ada syntax
18364 @item
18365 Any sequence of characters from the set
18366 (letters, digits, period, underline).
18367 @end itemize
18368
18369 @noindent
18370 Comment lines may also appear in the definitions file, starting with
18371 the usual @code{--},
18372 and comments may be added to the definitions lines.
18373
18374 @node Form of Input Text for gnatprep
18375 @section Form of Input Text for @code{gnatprep}
18376
18377 @noindent
18378 The input text may contain preprocessor conditional inclusion lines,
18379 as well as general symbol substitution sequences.
18380
18381 The preprocessor conditional inclusion commands have the form
18382
18383 @smallexample
18384 @group
18385 @cartouche
18386 #if @i{expression} @r{[}then@r{]}
18387 lines
18388 #elsif @i{expression} @r{[}then@r{]}
18389 lines
18390 #elsif @i{expression} @r{[}then@r{]}
18391 lines
18392 @dots{}
18393 #else
18394 lines
18395 #end if;
18396 @end cartouche
18397 @end group
18398 @end smallexample
18399
18400 @noindent
18401 In this example, @i{expression} is defined by the following grammar:
18402 @smallexample
18403 @i{expression} ::= <symbol>
18404 @i{expression} ::= <symbol> = "<value>"
18405 @i{expression} ::= <symbol> = <symbol>
18406 @i{expression} ::= <symbol> 'Defined
18407 @i{expression} ::= not @i{expression}
18408 @i{expression} ::= @i{expression} and @i{expression}
18409 @i{expression} ::= @i{expression} or @i{expression}
18410 @i{expression} ::= @i{expression} and then @i{expression}
18411 @i{expression} ::= @i{expression} or else @i{expression}
18412 @i{expression} ::= ( @i{expression} )
18413 @end smallexample
18414
18415 The following restriction exists: it is not allowed to have "and" or "or"
18416 following "not" in the same expression without parentheses. For example, this
18417 is not allowed:
18418
18419 @smallexample
18420 not X or Y
18421 @end smallexample
18422
18423 This should be one of the following:
18424
18425 @smallexample
18426 (not X) or Y
18427 not (X or Y)
18428 @end smallexample
18429
18430 @noindent
18431 For the first test (@i{expression} ::= <symbol>) the symbol must have
18432 either the value true or false, that is to say the right-hand of the
18433 symbol definition must be one of the (case-insensitive) literals
18434 @code{True} or @code{False}. If the value is true, then the
18435 corresponding lines are included, and if the value is false, they are
18436 excluded.
18437
18438 The test (@i{expression} ::= <symbol> @code{'Defined}) is true only if
18439 the symbol has been defined in the definition file or by a @option{-D}
18440 switch on the command line. Otherwise, the test is false.
18441
18442 The equality tests are case insensitive, as are all the preprocessor lines.
18443
18444 If the symbol referenced is not defined in the symbol definitions file,
18445 then the effect depends on whether or not switch @option{-u}
18446 is specified. If so, then the symbol is treated as if it had the value
18447 false and the test fails. If this switch is not specified, then
18448 it is an error to reference an undefined symbol. It is also an error to
18449 reference a symbol that is defined with a value other than @code{True}
18450 or @code{False}.
18451
18452 The use of the @code{not} operator inverts the sense of this logical test.
18453 The @code{not} operator cannot be combined with the @code{or} or @code{and}
18454 operators, without parentheses. For example, "if not X or Y then" is not
18455 allowed, but "if (not X) or Y then" and "if not (X or Y) then" are.
18456
18457 The @code{then} keyword is optional as shown
18458
18459 The @code{#} must be the first non-blank character on a line, but
18460 otherwise the format is free form. Spaces or tabs may appear between
18461 the @code{#} and the keyword. The keywords and the symbols are case
18462 insensitive as in normal Ada code. Comments may be used on a
18463 preprocessor line, but other than that, no other tokens may appear on a
18464 preprocessor line. Any number of @code{elsif} clauses can be present,
18465 including none at all. The @code{else} is optional, as in Ada.
18466
18467 The @code{#} marking the start of a preprocessor line must be the first
18468 non-blank character on the line, i.e., it must be preceded only by
18469 spaces or horizontal tabs.
18470
18471 Symbol substitution outside of preprocessor lines is obtained by using
18472 the sequence
18473
18474 @smallexample
18475 $symbol
18476 @end smallexample
18477
18478 @noindent
18479 anywhere within a source line, except in a comment or within a
18480 string literal. The identifier
18481 following the @code{$} must match one of the symbols defined in the symbol
18482 definition file, and the result is to substitute the value of the
18483 symbol in place of @code{$symbol} in the output file.
18484
18485 Note that although the substitution of strings within a string literal
18486 is not possible, it is possible to have a symbol whose defined value is
18487 a string literal. So instead of setting XYZ to @code{hello} and writing:
18488
18489 @smallexample
18490 Header : String := "$XYZ";
18491 @end smallexample
18492
18493 @noindent
18494 you should set XYZ to @code{"hello"} and write:
18495
18496 @smallexample
18497 Header : String := $XYZ;
18498 @end smallexample
18499
18500 @noindent
18501 and then the substitution will occur as desired.
18502
18503 @ifset vms
18504 @node The GNAT Run-Time Library Builder gnatlbr
18505 @chapter The GNAT Run-Time Library Builder @code{gnatlbr}
18506 @findex gnatlbr
18507 @cindex Library builder
18508
18509 @noindent
18510 @code{gnatlbr} is a tool for rebuilding the GNAT run time with user
18511 supplied configuration pragmas.
18512
18513 @menu
18514 * Running gnatlbr::
18515 * Switches for gnatlbr::
18516 * Examples of gnatlbr Usage::
18517 @end menu
18518
18519 @node Running gnatlbr
18520 @section Running @code{gnatlbr}
18521
18522 @noindent
18523 The @code{gnatlbr} command has the form
18524
18525 @smallexample
18526 $ GNAT LIBRARY /@r{[}CREATE@r{|}SET@r{|}DELETE@r{]}=directory @r{[}/CONFIG=file@r{]}
18527 @end smallexample
18528
18529 @node Switches for gnatlbr
18530 @section Switches for @code{gnatlbr}
18531
18532 @noindent
18533 @code{gnatlbr} recognizes the following switches:
18534
18535 @table @option
18536 @c !sort!
18537 @item /CREATE=directory
18538 @cindex @code{/CREATE} (@code{gnatlbr})
18539 Create the new run-time library in the specified directory.
18540
18541 @item /SET=directory
18542 @cindex @code{/SET} (@code{gnatlbr})
18543 Make the library in the specified directory the current run-time library.
18544
18545 @item /DELETE=directory
18546 @cindex @code{/DELETE} (@code{gnatlbr})
18547 Delete the run-time library in the specified directory.
18548
18549 @item /CONFIG=file
18550 @cindex @code{/CONFIG} (@code{gnatlbr})
18551 With /CREATE: Use the configuration pragmas in the specified file when
18552 building the library.
18553
18554 With /SET: Use the configuration pragmas in the specified file when
18555 compiling.
18556
18557 @end table
18558
18559 @node Examples of gnatlbr Usage
18560 @section Example of @code{gnatlbr} Usage
18561
18562 @smallexample
18563 Contents of VAXFLOAT.ADC:
18564 pragma Float_Representation (VAX_Float);
18565
18566 $ GNAT LIBRARY /CREATE=[.VAXFLOAT] /CONFIG=VAXFLOAT.ADC
18567
18568 GNAT LIBRARY rebuilds the run-time library in directory [.VAXFLOAT]
18569
18570 @end smallexample
18571 @end ifset
18572
18573 @node The GNAT Library Browser gnatls
18574 @chapter The GNAT Library Browser @code{gnatls}
18575 @findex gnatls
18576 @cindex Library browser
18577
18578 @noindent
18579 @code{gnatls} is a tool that outputs information about compiled
18580 units. It gives the relationship between objects, unit names and source
18581 files. It can also be used to check the source dependencies of a unit
18582 as well as various characteristics.
18583
18584 Note: to invoke @code{gnatls} with a project file, use the @code{gnat}
18585 driver (see @ref{The GNAT Driver and Project Files}).
18586
18587 @menu
18588 * Running gnatls::
18589 * Switches for gnatls::
18590 * Examples of gnatls Usage::
18591 @end menu
18592
18593 @node Running gnatls
18594 @section Running @code{gnatls}
18595
18596 @noindent
18597 The @code{gnatls} command has the form
18598
18599 @smallexample
18600 $ gnatls switches @var{object_or_ali_file}
18601 @end smallexample
18602
18603 @noindent
18604 The main argument is the list of object or @file{ali} files
18605 (@pxref{The Ada Library Information Files})
18606 for which information is requested.
18607
18608 In normal mode, without additional option, @code{gnatls} produces a
18609 four-column listing. Each line represents information for a specific
18610 object. The first column gives the full path of the object, the second
18611 column gives the name of the principal unit in this object, the third
18612 column gives the status of the source and the fourth column gives the
18613 full path of the source representing this unit.
18614 Here is a simple example of use:
18615
18616 @smallexample
18617 $ gnatls *.o
18618 ^./^[]^demo1.o demo1 DIF demo1.adb
18619 ^./^[]^demo2.o demo2 OK demo2.adb
18620 ^./^[]^hello.o h1 OK hello.adb
18621 ^./^[]^instr-child.o instr.child MOK instr-child.adb
18622 ^./^[]^instr.o instr OK instr.adb
18623 ^./^[]^tef.o tef DIF tef.adb
18624 ^./^[]^text_io_example.o text_io_example OK text_io_example.adb
18625 ^./^[]^tgef.o tgef DIF tgef.adb
18626 @end smallexample
18627
18628 @noindent
18629 The first line can be interpreted as follows: the main unit which is
18630 contained in
18631 object file @file{demo1.o} is demo1, whose main source is in
18632 @file{demo1.adb}. Furthermore, the version of the source used for the
18633 compilation of demo1 has been modified (DIF). Each source file has a status
18634 qualifier which can be:
18635
18636 @table @code
18637 @item OK (unchanged)
18638 The version of the source file used for the compilation of the
18639 specified unit corresponds exactly to the actual source file.
18640
18641 @item MOK (slightly modified)
18642 The version of the source file used for the compilation of the
18643 specified unit differs from the actual source file but not enough to
18644 require recompilation. If you use gnatmake with the qualifier
18645 @option{^-m (minimal recompilation)^/MINIMAL_RECOMPILATION^}, a file marked
18646 MOK will not be recompiled.
18647
18648 @item DIF (modified)
18649 No version of the source found on the path corresponds to the source
18650 used to build this object.
18651
18652 @item ??? (file not found)
18653 No source file was found for this unit.
18654
18655 @item HID (hidden, unchanged version not first on PATH)
18656 The version of the source that corresponds exactly to the source used
18657 for compilation has been found on the path but it is hidden by another
18658 version of the same source that has been modified.
18659
18660 @end table
18661
18662 @node Switches for gnatls
18663 @section Switches for @code{gnatls}
18664
18665 @noindent
18666 @code{gnatls} recognizes the following switches:
18667
18668 @table @option
18669 @c !sort!
18670 @cindex @option{--version} @command{gnatls}
18671 Display Copyright and version, then exit disregarding all other options.
18672
18673 @item --help
18674 @cindex @option{--help} @command{gnatls}
18675 If @option{--version} was not used, display usage, then exit disregarding
18676 all other options.
18677
18678 @item ^-a^/ALL_UNITS^
18679 @cindex @option{^-a^/ALL_UNITS^} (@code{gnatls})
18680 Consider all units, including those of the predefined Ada library.
18681 Especially useful with @option{^-d^/DEPENDENCIES^}.
18682
18683 @item ^-d^/DEPENDENCIES^
18684 @cindex @option{^-d^/DEPENDENCIES^} (@code{gnatls})
18685 List sources from which specified units depend on.
18686
18687 @item ^-h^/OUTPUT=OPTIONS^
18688 @cindex @option{^-h^/OUTPUT=OPTIONS^} (@code{gnatls})
18689 Output the list of options.
18690
18691 @item ^-o^/OUTPUT=OBJECTS^
18692 @cindex @option{^-o^/OUTPUT=OBJECTS^} (@code{gnatls})
18693 Only output information about object files.
18694
18695 @item ^-s^/OUTPUT=SOURCES^
18696 @cindex @option{^-s^/OUTPUT=SOURCES^} (@code{gnatls})
18697 Only output information about source files.
18698
18699 @item ^-u^/OUTPUT=UNITS^
18700 @cindex @option{^-u^/OUTPUT=UNITS^} (@code{gnatls})
18701 Only output information about compilation units.
18702
18703 @item ^-files^/FILES^=@var{file}
18704 @cindex @option{^-files^/FILES^} (@code{gnatls})
18705 Take as arguments the files listed in text file @var{file}.
18706 Text file @var{file} may contain empty lines that are ignored.
18707 Each nonempty line should contain the name of an existing file.
18708 Several such switches may be specified simultaneously.
18709
18710 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18711 @itemx ^-aI^/SOURCE_SEARCH=^@var{dir}
18712 @itemx ^-I^/SEARCH=^@var{dir}
18713 @itemx ^-I-^/NOCURRENT_DIRECTORY^
18714 @itemx -nostdinc
18715 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatls})
18716 @cindex @option{^-aI^/SOURCE_SEARCH^} (@code{gnatls})
18717 @cindex @option{^-I^/SEARCH^} (@code{gnatls})
18718 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatls})
18719 Source path manipulation. Same meaning as the equivalent @command{gnatmake}
18720 flags (@pxref{Switches for gnatmake}).
18721
18722 @item --RTS=@var{rts-path}
18723 @cindex @option{--RTS} (@code{gnatls})
18724 Specifies the default location of the runtime library. Same meaning as the
18725 equivalent @command{gnatmake} flag (@pxref{Switches for gnatmake}).
18726
18727 @item ^-v^/OUTPUT=VERBOSE^
18728 @cindex @option{^-v^/OUTPUT=VERBOSE^} (@code{gnatls})
18729 Verbose mode. Output the complete source, object and project paths. Do not use
18730 the default column layout but instead use long format giving as much as
18731 information possible on each requested units, including special
18732 characteristics such as:
18733
18734 @table @code
18735 @item Preelaborable
18736 The unit is preelaborable in the Ada sense.
18737
18738 @item No_Elab_Code
18739 No elaboration code has been produced by the compiler for this unit.
18740
18741 @item Pure
18742 The unit is pure in the Ada sense.
18743
18744 @item Elaborate_Body
18745 The unit contains a pragma Elaborate_Body.
18746
18747 @item Remote_Types
18748 The unit contains a pragma Remote_Types.
18749
18750 @item Shared_Passive
18751 The unit contains a pragma Shared_Passive.
18752
18753 @item Predefined
18754 This unit is part of the predefined environment and cannot be modified
18755 by the user.
18756
18757 @item Remote_Call_Interface
18758 The unit contains a pragma Remote_Call_Interface.
18759
18760 @end table
18761
18762 @end table
18763
18764 @node Examples of gnatls Usage
18765 @section Example of @code{gnatls} Usage
18766 @ifclear vms
18767
18768 @noindent
18769 Example of using the verbose switch. Note how the source and
18770 object paths are affected by the -I switch.
18771
18772 @smallexample
18773 $ gnatls -v -I.. demo1.o
18774
18775 GNATLS 5.03w (20041123-34)
18776 Copyright 1997-2004 Free Software Foundation, Inc.
18777
18778 Source Search Path:
18779 <Current_Directory>
18780 ../
18781 /home/comar/local/adainclude/
18782
18783 Object Search Path:
18784 <Current_Directory>
18785 ../
18786 /home/comar/local/lib/gcc-lib/x86-linux/3.4.3/adalib/
18787
18788 Project Search Path:
18789 <Current_Directory>
18790 /home/comar/local/lib/gnat/
18791
18792 ./demo1.o
18793 Unit =>
18794 Name => demo1
18795 Kind => subprogram body
18796 Flags => No_Elab_Code
18797 Source => demo1.adb modified
18798 @end smallexample
18799
18800 @noindent
18801 The following is an example of use of the dependency list.
18802 Note the use of the -s switch
18803 which gives a straight list of source files. This can be useful for
18804 building specialized scripts.
18805
18806 @smallexample
18807 $ gnatls -d demo2.o
18808 ./demo2.o demo2 OK demo2.adb
18809 OK gen_list.ads
18810 OK gen_list.adb
18811 OK instr.ads
18812 OK instr-child.ads
18813
18814 $ gnatls -d -s -a demo1.o
18815 demo1.adb
18816 /home/comar/local/adainclude/ada.ads
18817 /home/comar/local/adainclude/a-finali.ads
18818 /home/comar/local/adainclude/a-filico.ads
18819 /home/comar/local/adainclude/a-stream.ads
18820 /home/comar/local/adainclude/a-tags.ads
18821 gen_list.ads
18822 gen_list.adb
18823 /home/comar/local/adainclude/gnat.ads
18824 /home/comar/local/adainclude/g-io.ads
18825 instr.ads
18826 /home/comar/local/adainclude/system.ads
18827 /home/comar/local/adainclude/s-exctab.ads
18828 /home/comar/local/adainclude/s-finimp.ads
18829 /home/comar/local/adainclude/s-finroo.ads
18830 /home/comar/local/adainclude/s-secsta.ads
18831 /home/comar/local/adainclude/s-stalib.ads
18832 /home/comar/local/adainclude/s-stoele.ads
18833 /home/comar/local/adainclude/s-stratt.ads
18834 /home/comar/local/adainclude/s-tasoli.ads
18835 /home/comar/local/adainclude/s-unstyp.ads
18836 /home/comar/local/adainclude/unchconv.ads
18837 @end smallexample
18838 @end ifclear
18839
18840 @ifset vms
18841 @smallexample
18842 GNAT LIST /DEPENDENCIES /OUTPUT=SOURCES /ALL_UNITS DEMO1.ADB
18843
18844 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]ada.ads
18845 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-finali.ads
18846 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-filico.ads
18847 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-stream.ads
18848 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]a-tags.ads
18849 demo1.adb
18850 gen_list.ads
18851 gen_list.adb
18852 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]gnat.ads
18853 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]g-io.ads
18854 instr.ads
18855 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]system.ads
18856 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-exctab.ads
18857 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finimp.ads
18858 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-finroo.ads
18859 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-secsta.ads
18860 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stalib.ads
18861 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stoele.ads
18862 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-stratt.ads
18863 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-tasoli.ads
18864 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]s-unstyp.ads
18865 GNU:[LIB.OPENVMS7_1.2_8_1.ADALIB]unchconv.ads
18866 @end smallexample
18867 @end ifset
18868
18869 @node Cleaning Up Using gnatclean
18870 @chapter Cleaning Up Using @code{gnatclean}
18871 @findex gnatclean
18872 @cindex Cleaning tool
18873
18874 @noindent
18875 @code{gnatclean} is a tool that allows the deletion of files produced by the
18876 compiler, binder and linker, including ALI files, object files, tree files,
18877 expanded source files, library files, interface copy source files, binder
18878 generated files and executable files.
18879
18880 @menu
18881 * Running gnatclean::
18882 * Switches for gnatclean::
18883 @c * Examples of gnatclean Usage::
18884 @end menu
18885
18886 @node Running gnatclean
18887 @section Running @code{gnatclean}
18888
18889 @noindent
18890 The @code{gnatclean} command has the form:
18891
18892 @smallexample
18893 $ gnatclean switches @var{names}
18894 @end smallexample
18895
18896 @noindent
18897 @var{names} is a list of source file names. Suffixes @code{.^ads^ADS^} and
18898 @code{^adb^ADB^} may be omitted. If a project file is specified using switch
18899 @code{^-P^/PROJECT_FILE=^}, then @var{names} may be completely omitted.
18900
18901 @noindent
18902 In normal mode, @code{gnatclean} delete the files produced by the compiler and,
18903 if switch @code{^-c^/COMPILER_FILES_ONLY^} is not specified, by the binder and
18904 the linker. In informative-only mode, specified by switch
18905 @code{^-n^/NODELETE^}, the list of files that would have been deleted in
18906 normal mode is listed, but no file is actually deleted.
18907
18908 @node Switches for gnatclean
18909 @section Switches for @code{gnatclean}
18910
18911 @noindent
18912 @code{gnatclean} recognizes the following switches:
18913
18914 @table @option
18915 @c !sort!
18916 @cindex @option{--version} @command{gnatclean}
18917 Display Copyright and version, then exit disregarding all other options.
18918
18919 @item --help
18920 @cindex @option{--help} @command{gnatclean}
18921 If @option{--version} was not used, display usage, then exit disregarding
18922 all other options.
18923
18924 @item ^-c^/COMPILER_FILES_ONLY^
18925 @cindex @option{^-c^/COMPILER_FILES_ONLY^} (@code{gnatclean})
18926 Only attempt to delete the files produced by the compiler, not those produced
18927 by the binder or the linker. The files that are not to be deleted are library
18928 files, interface copy files, binder generated files and executable files.
18929
18930 @item ^-D ^/DIRECTORY_OBJECTS=^@var{dir}
18931 @cindex @option{^-D^/DIRECTORY_OBJECTS^} (@code{gnatclean})
18932 Indicate that ALI and object files should normally be found in directory
18933 @var{dir}.
18934
18935 @item ^-F^/FULL_PATH_IN_BRIEF_MESSAGES^
18936 @cindex @option{^-F^/FULL_PATH_IN_BRIEF_MESSAGES^} (@code{gnatclean})
18937 When using project files, if some errors or warnings are detected during
18938 parsing and verbose mode is not in effect (no use of switch
18939 ^-v^/VERBOSE^), then error lines start with the full path name of the project
18940 file, rather than its simple file name.
18941
18942 @item ^-h^/HELP^
18943 @cindex @option{^-h^/HELP^} (@code{gnatclean})
18944 Output a message explaining the usage of @code{^gnatclean^gnatclean^}.
18945
18946 @item ^-n^/NODELETE^
18947 @cindex @option{^-n^/NODELETE^} (@code{gnatclean})
18948 Informative-only mode. Do not delete any files. Output the list of the files
18949 that would have been deleted if this switch was not specified.
18950
18951 @item ^-P^/PROJECT_FILE=^@var{project}
18952 @cindex @option{^-P^/PROJECT_FILE^} (@code{gnatclean})
18953 Use project file @var{project}. Only one such switch can be used.
18954 When cleaning a project file, the files produced by the compilation of the
18955 immediate sources or inherited sources of the project files are to be
18956 deleted. This is not depending on the presence or not of executable names
18957 on the command line.
18958
18959 @item ^-q^/QUIET^
18960 @cindex @option{^-q^/QUIET^} (@code{gnatclean})
18961 Quiet output. If there are no errors, do not output anything, except in
18962 verbose mode (switch ^-v^/VERBOSE^) or in informative-only mode
18963 (switch ^-n^/NODELETE^).
18964
18965 @item ^-r^/RECURSIVE^
18966 @cindex @option{^-r^/RECURSIVE^} (@code{gnatclean})
18967 When a project file is specified (using switch ^-P^/PROJECT_FILE=^),
18968 clean all imported and extended project files, recursively. If this switch
18969 is not specified, only the files related to the main project file are to be
18970 deleted. This switch has no effect if no project file is specified.
18971
18972 @item ^-v^/VERBOSE^
18973 @cindex @option{^-v^/VERBOSE^} (@code{gnatclean})
18974 Verbose mode.
18975
18976 @item ^-vP^/MESSAGES_PROJECT_FILE=^@emph{x}
18977 @cindex @option{^-vP^/MESSAGES_PROJECT_FILE^} (@code{gnatclean})
18978 Indicates the verbosity of the parsing of GNAT project files.
18979 @xref{Switches Related to Project Files}.
18980
18981 @item ^-X^/EXTERNAL_REFERENCE=^@var{name=value}
18982 @cindex @option{^-X^/EXTERNAL_REFERENCE^} (@code{gnatclean})
18983 Indicates that external variable @var{name} has the value @var{value}.
18984 The Project Manager will use this value for occurrences of
18985 @code{external(name)} when parsing the project file.
18986 @xref{Switches Related to Project Files}.
18987
18988 @item ^-aO^/OBJECT_SEARCH=^@var{dir}
18989 @cindex @option{^-aO^/OBJECT_SEARCH^} (@code{gnatclean})
18990 When searching for ALI and object files, look in directory
18991 @var{dir}.
18992
18993 @item ^-I^/SEARCH=^@var{dir}
18994 @cindex @option{^-I^/SEARCH^} (@code{gnatclean})
18995 Equivalent to @option{^-aO^/OBJECT_SEARCH=^@var{dir}}.
18996
18997 @item ^-I-^/NOCURRENT_DIRECTORY^
18998 @cindex @option{^-I-^/NOCURRENT_DIRECTORY^} (@code{gnatclean})
18999 @cindex Source files, suppressing search
19000 Do not look for ALI or object files in the directory
19001 where @code{gnatclean} was invoked.
19002
19003 @end table
19004
19005 @c @node Examples of gnatclean Usage
19006 @c @section Examples of @code{gnatclean} Usage
19007
19008 @ifclear vms
19009 @node GNAT and Libraries
19010 @chapter GNAT and Libraries
19011 @cindex Library, building, installing, using
19012
19013 @noindent
19014 This chapter describes how to build and use libraries with GNAT, and also shows
19015 how to recompile the GNAT run-time library. You should be familiar with the
19016 Project Manager facility (@pxref{GNAT Project Manager}) before reading this
19017 chapter.
19018
19019 @menu
19020 * Introduction to Libraries in GNAT::
19021 * General Ada Libraries::
19022 * Stand-alone Ada Libraries::
19023 * Rebuilding the GNAT Run-Time Library::
19024 @end menu
19025
19026 @node Introduction to Libraries in GNAT
19027 @section Introduction to Libraries in GNAT
19028
19029 @noindent
19030 A library is, conceptually, a collection of objects which does not have its
19031 own main thread of execution, but rather provides certain services to the
19032 applications that use it. A library can be either statically linked with the
19033 application, in which case its code is directly included in the application,
19034 or, on platforms that support it, be dynamically linked, in which case
19035 its code is shared by all applications making use of this library.
19036
19037 GNAT supports both types of libraries.
19038 In the static case, the compiled code can be provided in different ways. The
19039 simplest approach is to provide directly the set of objects resulting from
19040 compilation of the library source files. Alternatively, you can group the
19041 objects into an archive using whatever commands are provided by the operating
19042 system. For the latter case, the objects are grouped into a shared library.
19043
19044 In the GNAT environment, a library has three types of components:
19045 @itemize @bullet
19046 @item
19047 Source files.
19048 @item
19049 @file{ALI} files.
19050 @xref{The Ada Library Information Files}.
19051 @item
19052 Object files, an archive or a shared library.
19053 @end itemize
19054
19055 @noindent
19056 A GNAT library may expose all its source files, which is useful for
19057 documentation purposes. Alternatively, it may expose only the units needed by
19058 an external user to make use of the library. That is to say, the specs
19059 reflecting the library services along with all the units needed to compile
19060 those specs, which can include generic bodies or any body implementing an
19061 inlined routine. In the case of @emph{stand-alone libraries} those exposed
19062 units are called @emph{interface units} (@pxref{Stand-alone Ada Libraries}).
19063
19064 All compilation units comprising an application, including those in a library,
19065 need to be elaborated in an order partially defined by Ada's semantics. GNAT
19066 computes the elaboration order from the @file{ALI} files and this is why they
19067 constitute a mandatory part of GNAT libraries.
19068 @emph{Stand-alone libraries} are the exception to this rule because a specific
19069 library elaboration routine is produced independently of the application(s)
19070 using the library.
19071
19072 @node General Ada Libraries
19073 @section General Ada Libraries
19074
19075 @menu
19076 * Building a library::
19077 * Installing a library::
19078 * Using a library::
19079 @end menu
19080
19081 @node Building a library
19082 @subsection Building a library
19083
19084 @noindent
19085 The easiest way to build a library is to use the Project Manager,
19086 which supports a special type of project called a @emph{Library Project}
19087 (@pxref{Library Projects}).
19088
19089 A project is considered a library project, when two project-level attributes
19090 are defined in it: @code{Library_Name} and @code{Library_Dir}. In order to
19091 control different aspects of library configuration, additional optional
19092 project-level attributes can be specified:
19093 @table @code
19094 @item Library_Kind
19095 This attribute controls whether the library is to be static or dynamic
19096
19097 @item Library_Version
19098 This attribute specifies the library version; this value is used
19099 during dynamic linking of shared libraries to determine if the currently
19100 installed versions of the binaries are compatible.
19101
19102 @item Library_Options
19103 @item Library_GCC
19104 These attributes specify additional low-level options to be used during
19105 library generation, and redefine the actual application used to generate
19106 library.
19107 @end table
19108
19109 @noindent
19110 The GNAT Project Manager takes full care of the library maintenance task,
19111 including recompilation of the source files for which objects do not exist
19112 or are not up to date, assembly of the library archive, and installation of
19113 the library (i.e., copying associated source, object and @file{ALI} files
19114 to the specified location).
19115
19116 Here is a simple library project file:
19117 @smallexample @c ada
19118 project My_Lib is
19119 for Source_Dirs use ("src1", "src2");
19120 for Object_Dir use "obj";
19121 for Library_Name use "mylib";
19122 for Library_Dir use "lib";
19123 for Library_Kind use "dynamic";
19124 end My_lib;
19125 @end smallexample
19126
19127 @noindent
19128 and the compilation command to build and install the library:
19129
19130 @smallexample @c ada
19131 $ gnatmake -Pmy_lib
19132 @end smallexample
19133
19134 @noindent
19135 It is not entirely trivial to perform manually all the steps required to
19136 produce a library. We recommend that you use the GNAT Project Manager
19137 for this task. In special cases where this is not desired, the necessary
19138 steps are discussed below.
19139
19140 There are various possibilities for compiling the units that make up the
19141 library: for example with a Makefile (@pxref{Using the GNU make Utility}) or
19142 with a conventional script. For simple libraries, it is also possible to create
19143 a dummy main program which depends upon all the packages that comprise the
19144 interface of the library. This dummy main program can then be given to
19145 @command{gnatmake}, which will ensure that all necessary objects are built.
19146
19147 After this task is accomplished, you should follow the standard procedure
19148 of the underlying operating system to produce the static or shared library.
19149
19150 Here is an example of such a dummy program:
19151 @smallexample @c ada
19152 @group
19153 with My_Lib.Service1;
19154 with My_Lib.Service2;
19155 with My_Lib.Service3;
19156 procedure My_Lib_Dummy is
19157 begin
19158 null;
19159 end;
19160 @end group
19161 @end smallexample
19162
19163 @noindent
19164 Here are the generic commands that will build an archive or a shared library.
19165
19166 @smallexample
19167 # compiling the library
19168 $ gnatmake -c my_lib_dummy.adb
19169
19170 # we don't need the dummy object itself
19171 $ rm my_lib_dummy.o my_lib_dummy.ali
19172
19173 # create an archive with the remaining objects
19174 $ ar rc libmy_lib.a *.o
19175 # some systems may require "ranlib" to be run as well
19176
19177 # or create a shared library
19178 $ gcc -shared -o libmy_lib.so *.o
19179 # some systems may require the code to have been compiled with -fPIC
19180
19181 # remove the object files that are now in the library
19182 $ rm *.o
19183
19184 # Make the ALI files read-only so that gnatmake will not try to
19185 # regenerate the objects that are in the library
19186 $ chmod -w *.ali
19187 @end smallexample
19188
19189 @noindent
19190 Please note that the library must have a name of the form @file{lib@var{xxx}.a}
19191 or @file{lib@var{xxx}.so} (or @file{lib@var{xxx}.dll} on Windows) in order to
19192 be accessed by the directive @option{-l@var{xxx}} at link time.
19193
19194 @node Installing a library
19195 @subsection Installing a library
19196 @cindex @code{ADA_PROJECT_PATH}
19197 @cindex @code{GPR_PROJECT_PATH}
19198
19199 @noindent
19200 If you use project files, library installation is part of the library build
19201 process. Thus no further action is needed in order to make use of the
19202 libraries that are built as part of the general application build. A usable
19203 version of the library is installed in the directory specified by the
19204 @code{Library_Dir} attribute of the library project file.
19205
19206 You may want to install a library in a context different from where the library
19207 is built. This situation arises with third party suppliers, who may want
19208 to distribute a library in binary form where the user is not expected to be
19209 able to recompile the library. The simplest option in this case is to provide
19210 a project file slightly different from the one used to build the library, by
19211 using the @code{externally_built} attribute. For instance, the project
19212 file used to build the library in the previous section can be changed into the
19213 following one when the library is installed:
19214
19215 @smallexample @c projectfile
19216 project My_Lib is
19217 for Source_Dirs use ("src1", "src2");
19218 for Library_Name use "mylib";
19219 for Library_Dir use "lib";
19220 for Library_Kind use "dynamic";
19221 for Externally_Built use "true";
19222 end My_lib;
19223 @end smallexample
19224
19225 @noindent
19226 This project file assumes that the directories @file{src1},
19227 @file{src2}, and @file{lib} exist in
19228 the directory containing the project file. The @code{externally_built}
19229 attribute makes it clear to the GNAT builder that it should not attempt to
19230 recompile any of the units from this library. It allows the library provider to
19231 restrict the source set to the minimum necessary for clients to make use of the
19232 library as described in the first section of this chapter. It is the
19233 responsibility of the library provider to install the necessary sources, ALI
19234 files and libraries in the directories mentioned in the project file. For
19235 convenience, the user's library project file should be installed in a location
19236 that will be searched automatically by the GNAT
19237 builder. These are the directories referenced in the @env{GPR_PROJECT_PATH}
19238 environment variable (@pxref{Importing Projects}), and also the default GNAT
19239 library location that can be queried with @command{gnatls -v} and is usually of
19240 the form $gnat_install_root/lib/gnat.
19241
19242 When project files are not an option, it is also possible, but not recommended,
19243 to install the library so that the sources needed to use the library are on the
19244 Ada source path and the ALI files & libraries be on the Ada Object path (see
19245 @ref{Search Paths and the Run-Time Library (RTL)}. Alternatively, the system
19246 administrator can place general-purpose libraries in the default compiler
19247 paths, by specifying the libraries' location in the configuration files
19248 @file{ada_source_path} and @file{ada_object_path}. These configuration files
19249 must be located in the GNAT installation tree at the same place as the gcc spec
19250 file. The location of the gcc spec file can be determined as follows:
19251 @smallexample
19252 $ gcc -v
19253 @end smallexample
19254
19255 @noindent
19256 The configuration files mentioned above have a simple format: each line
19257 must contain one unique directory name.
19258 Those names are added to the corresponding path
19259 in their order of appearance in the file. The names can be either absolute
19260 or relative; in the latter case, they are relative to where theses files
19261 are located.
19262
19263 The files @file{ada_source_path} and @file{ada_object_path} might not be
19264 present in a
19265 GNAT installation, in which case, GNAT will look for its run-time library in
19266 the directories @file{adainclude} (for the sources) and @file{adalib} (for the
19267 objects and @file{ALI} files). When the files exist, the compiler does not
19268 look in @file{adainclude} and @file{adalib}, and thus the
19269 @file{ada_source_path} file
19270 must contain the location for the GNAT run-time sources (which can simply
19271 be @file{adainclude}). In the same way, the @file{ada_object_path} file must
19272 contain the location for the GNAT run-time objects (which can simply
19273 be @file{adalib}).
19274
19275 You can also specify a new default path to the run-time library at compilation
19276 time with the switch @option{--RTS=rts-path}. You can thus choose / change
19277 the run-time library you want your program to be compiled with. This switch is
19278 recognized by @command{gcc}, @command{gnatmake}, @command{gnatbind},
19279 @command{gnatls}, @command{gnatfind} and @command{gnatxref}.
19280
19281 It is possible to install a library before or after the standard GNAT
19282 library, by reordering the lines in the configuration files. In general, a
19283 library must be installed before the GNAT library if it redefines
19284 any part of it.
19285
19286 @node Using a library
19287 @subsection Using a library
19288
19289 @noindent Once again, the project facility greatly simplifies the use of
19290 libraries. In this context, using a library is just a matter of adding a
19291 @code{with} clause in the user project. For instance, to make use of the
19292 library @code{My_Lib} shown in examples in earlier sections, you can
19293 write:
19294
19295 @smallexample @c projectfile
19296 with "my_lib";
19297 project My_Proj is
19298 @dots{}
19299 end My_Proj;
19300 @end smallexample
19301
19302 Even if you have a third-party, non-Ada library, you can still use GNAT's
19303 Project Manager facility to provide a wrapper for it. For example, the
19304 following project, when @code{with}ed by your main project, will link with the
19305 third-party library @file{liba.a}:
19306
19307 @smallexample @c projectfile
19308 @group
19309 project Liba is
19310 for Externally_Built use "true";
19311 for Source_Files use ();
19312 for Library_Dir use "lib";
19313 for Library_Name use "a";
19314 for Library_Kind use "static";
19315 end Liba;
19316 @end group
19317 @end smallexample
19318 This is an alternative to the use of @code{pragma Linker_Options}. It is
19319 especially interesting in the context of systems with several interdependent
19320 static libraries where finding a proper linker order is not easy and best be
19321 left to the tools having visibility over project dependence information.
19322
19323 @noindent
19324 In order to use an Ada library manually, you need to make sure that this
19325 library is on both your source and object path
19326 (see @ref{Search Paths and the Run-Time Library (RTL)}
19327 and @ref{Search Paths for gnatbind}). Furthermore, when the objects are grouped
19328 in an archive or a shared library, you need to specify the desired
19329 library at link time.
19330
19331 For example, you can use the library @file{mylib} installed in
19332 @file{/dir/my_lib_src} and @file{/dir/my_lib_obj} with the following commands:
19333
19334 @smallexample
19335 $ gnatmake -aI/dir/my_lib_src -aO/dir/my_lib_obj my_appl \
19336 -largs -lmy_lib
19337 @end smallexample
19338
19339 @noindent
19340 This can be expressed more simply:
19341 @smallexample
19342 $ gnatmake my_appl
19343 @end smallexample
19344 @noindent
19345 when the following conditions are met:
19346 @itemize @bullet
19347 @item
19348 @file{/dir/my_lib_src} has been added by the user to the environment
19349 variable @env{ADA_INCLUDE_PATH}, or by the administrator to the file
19350 @file{ada_source_path}
19351 @item
19352 @file{/dir/my_lib_obj} has been added by the user to the environment
19353 variable @env{ADA_OBJECTS_PATH}, or by the administrator to the file
19354 @file{ada_object_path}
19355 @item
19356 a pragma @code{Linker_Options} has been added to one of the sources.
19357 For example:
19358
19359 @smallexample @c ada
19360 pragma Linker_Options ("-lmy_lib");
19361 @end smallexample
19362 @end itemize
19363
19364 @node Stand-alone Ada Libraries
19365 @section Stand-alone Ada Libraries
19366 @cindex Stand-alone library, building, using
19367
19368 @menu
19369 * Introduction to Stand-alone Libraries::
19370 * Building a Stand-alone Library::
19371 * Creating a Stand-alone Library to be used in a non-Ada context::
19372 * Restrictions in Stand-alone Libraries::
19373 @end menu
19374
19375 @node Introduction to Stand-alone Libraries
19376 @subsection Introduction to Stand-alone Libraries
19377
19378 @noindent
19379 A Stand-alone Library (abbreviated ``SAL'') is a library that contains the
19380 necessary code to
19381 elaborate the Ada units that are included in the library. In contrast with
19382 an ordinary library, which consists of all sources, objects and @file{ALI}
19383 files of the
19384 library, a SAL may specify a restricted subset of compilation units
19385 to serve as a library interface. In this case, the fully
19386 self-sufficient set of files will normally consist of an objects
19387 archive, the sources of interface units' specs, and the @file{ALI}
19388 files of interface units.
19389 If an interface spec contains a generic unit or an inlined subprogram,
19390 the body's
19391 source must also be provided; if the units that must be provided in the source
19392 form depend on other units, the source and @file{ALI} files of those must
19393 also be provided.
19394
19395 The main purpose of a SAL is to minimize the recompilation overhead of client
19396 applications when a new version of the library is installed. Specifically,
19397 if the interface sources have not changed, client applications do not need to
19398 be recompiled. If, furthermore, a SAL is provided in the shared form and its
19399 version, controlled by @code{Library_Version} attribute, is not changed,
19400 then the clients do not need to be relinked.
19401
19402 SALs also allow the library providers to minimize the amount of library source
19403 text exposed to the clients. Such ``information hiding'' might be useful or
19404 necessary for various reasons.
19405
19406 Stand-alone libraries are also well suited to be used in an executable whose
19407 main routine is not written in Ada.
19408
19409 @node Building a Stand-alone Library
19410 @subsection Building a Stand-alone Library
19411
19412 @noindent
19413 GNAT's Project facility provides a simple way of building and installing
19414 stand-alone libraries; see @ref{Stand-alone Library Projects}.
19415 To be a Stand-alone Library Project, in addition to the two attributes
19416 that make a project a Library Project (@code{Library_Name} and
19417 @code{Library_Dir}; see @ref{Library Projects}), the attribute
19418 @code{Library_Interface} must be defined. For example:
19419
19420 @smallexample @c projectfile
19421 @group
19422 for Library_Dir use "lib_dir";
19423 for Library_Name use "dummy";
19424 for Library_Interface use ("int1", "int1.child");
19425 @end group
19426 @end smallexample
19427
19428 @noindent
19429 Attribute @code{Library_Interface} has a non-empty string list value,
19430 each string in the list designating a unit contained in an immediate source
19431 of the project file.
19432
19433 When a Stand-alone Library is built, first the binder is invoked to build
19434 a package whose name depends on the library name
19435 (@file{^b~dummy.ads/b^B$DUMMY.ADS/B^} in the example above).
19436 This binder-generated package includes initialization and
19437 finalization procedures whose
19438 names depend on the library name (@code{dummyinit} and @code{dummyfinal}
19439 in the example
19440 above). The object corresponding to this package is included in the library.
19441
19442 You must ensure timely (e.g., prior to any use of interfaces in the SAL)
19443 calling of these procedures if a static SAL is built, or if a shared SAL
19444 is built
19445 with the project-level attribute @code{Library_Auto_Init} set to
19446 @code{"false"}.
19447
19448 For a Stand-Alone Library, only the @file{ALI} files of the Interface Units
19449 (those that are listed in attribute @code{Library_Interface}) are copied to
19450 the Library Directory. As a consequence, only the Interface Units may be
19451 imported from Ada units outside of the library. If other units are imported,
19452 the binding phase will fail.
19453
19454 The attribute @code{Library_Src_Dir} may be specified for a
19455 Stand-Alone Library. @code{Library_Src_Dir} is a simple attribute that has a
19456 single string value. Its value must be the path (absolute or relative to the
19457 project directory) of an existing directory. This directory cannot be the
19458 object directory or one of the source directories, but it can be the same as
19459 the library directory. The sources of the Interface
19460 Units of the library that are needed by an Ada client of the library will be
19461 copied to the designated directory, called the Interface Copy directory.
19462 These sources include the specs of the Interface Units, but they may also
19463 include bodies and subunits, when pragmas @code{Inline} or @code{Inline_Always}
19464 are used, or when there is a generic unit in the spec. Before the sources
19465 are copied to the Interface Copy directory, an attempt is made to delete all
19466 files in the Interface Copy directory.
19467
19468 Building stand-alone libraries by hand is somewhat tedious, but for those
19469 occasions when it is necessary here are the steps that you need to perform:
19470 @itemize @bullet
19471 @item
19472 Compile all library sources.
19473
19474 @item
19475 Invoke the binder with the switch @option{-n} (No Ada main program),
19476 with all the @file{ALI} files of the interfaces, and
19477 with the switch @option{-L} to give specific names to the @code{init}
19478 and @code{final} procedures. For example:
19479 @smallexample
19480 gnatbind -n int1.ali int2.ali -Lsal1
19481 @end smallexample
19482
19483 @item
19484 Compile the binder generated file:
19485 @smallexample
19486 gcc -c b~int2.adb
19487 @end smallexample
19488
19489 @item
19490 Link the dynamic library with all the necessary object files,
19491 indicating to the linker the names of the @code{init} (and possibly
19492 @code{final}) procedures for automatic initialization (and finalization).
19493 The built library should be placed in a directory different from
19494 the object directory.
19495
19496 @item
19497 Copy the @code{ALI} files of the interface to the library directory,
19498 add in this copy an indication that it is an interface to a SAL
19499 (i.e., add a word @option{SL} on the line in the @file{ALI} file that starts
19500 with letter ``P'') and make the modified copy of the @file{ALI} file
19501 read-only.
19502 @end itemize
19503
19504 @noindent
19505 Using SALs is not different from using other libraries
19506 (see @ref{Using a library}).
19507
19508 @node Creating a Stand-alone Library to be used in a non-Ada context
19509 @subsection Creating a Stand-alone Library to be used in a non-Ada context
19510
19511 @noindent
19512 It is easy to adapt the SAL build procedure discussed above for use of a SAL in
19513 a non-Ada context.
19514
19515 The only extra step required is to ensure that library interface subprograms
19516 are compatible with the main program, by means of @code{pragma Export}
19517 or @code{pragma Convention}.
19518
19519 Here is an example of simple library interface for use with C main program:
19520
19521 @smallexample @c ada
19522 package My_Package is
19523
19524 procedure Do_Something;
19525 pragma Export (C, Do_Something, "do_something");
19526
19527 procedure Do_Something_Else;
19528 pragma Export (C, Do_Something_Else, "do_something_else");
19529
19530 end My_Package;
19531 @end smallexample
19532
19533 @noindent
19534 On the foreign language side, you must provide a ``foreign'' view of the
19535 library interface; remember that it should contain elaboration routines in
19536 addition to interface subprograms.
19537
19538 The example below shows the content of @code{mylib_interface.h} (note
19539 that there is no rule for the naming of this file, any name can be used)
19540 @smallexample
19541 /* the library elaboration procedure */
19542 extern void mylibinit (void);
19543
19544 /* the library finalization procedure */
19545 extern void mylibfinal (void);
19546
19547 /* the interface exported by the library */
19548 extern void do_something (void);
19549 extern void do_something_else (void);
19550 @end smallexample
19551
19552 @noindent
19553 Libraries built as explained above can be used from any program, provided
19554 that the elaboration procedures (named @code{mylibinit} in the previous
19555 example) are called before the library services are used. Any number of
19556 libraries can be used simultaneously, as long as the elaboration
19557 procedure of each library is called.
19558
19559 Below is an example of a C program that uses the @code{mylib} library.
19560
19561 @smallexample
19562 #include "mylib_interface.h"
19563
19564 int
19565 main (void)
19566 @{
19567 /* First, elaborate the library before using it */
19568 mylibinit ();
19569
19570 /* Main program, using the library exported entities */
19571 do_something ();
19572 do_something_else ();
19573
19574 /* Library finalization at the end of the program */
19575 mylibfinal ();
19576 return 0;
19577 @}
19578 @end smallexample
19579
19580 @noindent
19581 Note that invoking any library finalization procedure generated by
19582 @code{gnatbind} shuts down the Ada run-time environment.
19583 Consequently, the
19584 finalization of all Ada libraries must be performed at the end of the program.
19585 No call to these libraries or to the Ada run-time library should be made
19586 after the finalization phase.
19587
19588 @node Restrictions in Stand-alone Libraries
19589 @subsection Restrictions in Stand-alone Libraries
19590
19591 @noindent
19592 The pragmas listed below should be used with caution inside libraries,
19593 as they can create incompatibilities with other Ada libraries:
19594 @itemize @bullet
19595 @item pragma @code{Locking_Policy}
19596 @item pragma @code{Queuing_Policy}
19597 @item pragma @code{Task_Dispatching_Policy}
19598 @item pragma @code{Unreserve_All_Interrupts}
19599 @end itemize
19600
19601 @noindent
19602 When using a library that contains such pragmas, the user must make sure
19603 that all libraries use the same pragmas with the same values. Otherwise,
19604 @code{Program_Error} will
19605 be raised during the elaboration of the conflicting
19606 libraries. The usage of these pragmas and its consequences for the user
19607 should therefore be well documented.
19608
19609 Similarly, the traceback in the exception occurrence mechanism should be
19610 enabled or disabled in a consistent manner across all libraries.
19611 Otherwise, Program_Error will be raised during the elaboration of the
19612 conflicting libraries.
19613
19614 If the @code{Version} or @code{Body_Version}
19615 attributes are used inside a library, then you need to
19616 perform a @code{gnatbind} step that specifies all @file{ALI} files in all
19617 libraries, so that version identifiers can be properly computed.
19618 In practice these attributes are rarely used, so this is unlikely
19619 to be a consideration.
19620
19621 @node Rebuilding the GNAT Run-Time Library
19622 @section Rebuilding the GNAT Run-Time Library
19623 @cindex GNAT Run-Time Library, rebuilding
19624 @cindex Building the GNAT Run-Time Library
19625 @cindex Rebuilding the GNAT Run-Time Library
19626 @cindex Run-Time Library, rebuilding
19627
19628 @noindent
19629 It may be useful to recompile the GNAT library in various contexts, the
19630 most important one being the use of partition-wide configuration pragmas
19631 such as @code{Normalize_Scalars}. A special Makefile called
19632 @code{Makefile.adalib} is provided to that effect and can be found in
19633 the directory containing the GNAT library. The location of this
19634 directory depends on the way the GNAT environment has been installed and can
19635 be determined by means of the command:
19636
19637 @smallexample
19638 $ gnatls -v
19639 @end smallexample
19640
19641 @noindent
19642 The last entry in the object search path usually contains the
19643 gnat library. This Makefile contains its own documentation and in
19644 particular the set of instructions needed to rebuild a new library and
19645 to use it.
19646
19647 @node Using the GNU make Utility
19648 @chapter Using the GNU @code{make} Utility
19649 @findex make
19650
19651 @noindent
19652 This chapter offers some examples of makefiles that solve specific
19653 problems. It does not explain how to write a makefile (@pxref{Top,, GNU
19654 make, make, GNU @code{make}}), nor does it try to replace the
19655 @command{gnatmake} utility (@pxref{The GNAT Make Program gnatmake}).
19656
19657 All the examples in this section are specific to the GNU version of
19658 make. Although @command{make} is a standard utility, and the basic language
19659 is the same, these examples use some advanced features found only in
19660 @code{GNU make}.
19661
19662 @menu
19663 * Using gnatmake in a Makefile::
19664 * Automatically Creating a List of Directories::
19665 * Generating the Command Line Switches::
19666 * Overcoming Command Line Length Limits::
19667 @end menu
19668
19669 @node Using gnatmake in a Makefile
19670 @section Using gnatmake in a Makefile
19671 @findex makefile
19672 @cindex GNU make
19673
19674 @noindent
19675 Complex project organizations can be handled in a very powerful way by
19676 using GNU make combined with gnatmake. For instance, here is a Makefile
19677 which allows you to build each subsystem of a big project into a separate
19678 shared library. Such a makefile allows you to significantly reduce the link
19679 time of very big applications while maintaining full coherence at
19680 each step of the build process.
19681
19682 The list of dependencies are handled automatically by
19683 @command{gnatmake}. The Makefile is simply used to call gnatmake in each of
19684 the appropriate directories.
19685
19686 Note that you should also read the example on how to automatically
19687 create the list of directories
19688 (@pxref{Automatically Creating a List of Directories})
19689 which might help you in case your project has a lot of subdirectories.
19690
19691 @smallexample
19692 @iftex
19693 @leftskip=0cm
19694 @font@heightrm=cmr8
19695 @heightrm
19696 @end iftex
19697 ## This Makefile is intended to be used with the following directory
19698 ## configuration:
19699 ## - The sources are split into a series of csc (computer software components)
19700 ## Each of these csc is put in its own directory.
19701 ## Their name are referenced by the directory names.
19702 ## They will be compiled into shared library (although this would also work
19703 ## with static libraries
19704 ## - The main program (and possibly other packages that do not belong to any
19705 ## csc is put in the top level directory (where the Makefile is).
19706 ## toplevel_dir __ first_csc (sources) __ lib (will contain the library)
19707 ## \_ second_csc (sources) __ lib (will contain the library)
19708 ## \_ @dots{}
19709 ## Although this Makefile is build for shared library, it is easy to modify
19710 ## to build partial link objects instead (modify the lines with -shared and
19711 ## gnatlink below)
19712 ##
19713 ## With this makefile, you can change any file in the system or add any new
19714 ## file, and everything will be recompiled correctly (only the relevant shared
19715 ## objects will be recompiled, and the main program will be re-linked).
19716
19717 # The list of computer software component for your project. This might be
19718 # generated automatically.
19719 CSC_LIST=aa bb cc
19720
19721 # Name of the main program (no extension)
19722 MAIN=main
19723
19724 # If we need to build objects with -fPIC, uncomment the following line
19725 #NEED_FPIC=-fPIC
19726
19727 # The following variable should give the directory containing libgnat.so
19728 # You can get this directory through 'gnatls -v'. This is usually the last
19729 # directory in the Object_Path.
19730 GLIB=@dots{}
19731
19732 # The directories for the libraries
19733 # (This macro expands the list of CSC to the list of shared libraries, you
19734 # could simply use the expanded form:
19735 # LIB_DIR=aa/lib/libaa.so bb/lib/libbb.so cc/lib/libcc.so
19736 LIB_DIR=$@{foreach dir,$@{CSC_LIST@},$@{dir@}/lib/lib$@{dir@}.so@}
19737
19738 $@{MAIN@}: objects $@{LIB_DIR@}
19739 gnatbind $@{MAIN@} $@{CSC_LIST:%=-aO%/lib@} -shared
19740 gnatlink $@{MAIN@} $@{CSC_LIST:%=-l%@}
19741
19742 objects::
19743 # recompile the sources
19744 gnatmake -c -i $@{MAIN@}.adb $@{NEED_FPIC@} $@{CSC_LIST:%=-I%@}
19745
19746 # Note: In a future version of GNAT, the following commands will be simplified
19747 # by a new tool, gnatmlib
19748 $@{LIB_DIR@}:
19749 mkdir -p $@{dir $@@ @}
19750 cd $@{dir $@@ @} && gcc -shared -o $@{notdir $@@ @} ../*.o -L$@{GLIB@} -lgnat
19751 cd $@{dir $@@ @} && cp -f ../*.ali .
19752
19753 # The dependencies for the modules
19754 # Note that we have to force the expansion of *.o, since in some cases
19755 # make won't be able to do it itself.
19756 aa/lib/libaa.so: $@{wildcard aa/*.o@}
19757 bb/lib/libbb.so: $@{wildcard bb/*.o@}
19758 cc/lib/libcc.so: $@{wildcard cc/*.o@}
19759
19760 # Make sure all of the shared libraries are in the path before starting the
19761 # program
19762 run::
19763 LD_LIBRARY_PATH=`pwd`/aa/lib:`pwd`/bb/lib:`pwd`/cc/lib ./$@{MAIN@}
19764
19765 clean::
19766 $@{RM@} -rf $@{CSC_LIST:%=%/lib@}
19767 $@{RM@} $@{CSC_LIST:%=%/*.ali@}
19768 $@{RM@} $@{CSC_LIST:%=%/*.o@}
19769 $@{RM@} *.o *.ali $@{MAIN@}
19770 @end smallexample
19771
19772 @node Automatically Creating a List of Directories
19773 @section Automatically Creating a List of Directories
19774
19775 @noindent
19776 In most makefiles, you will have to specify a list of directories, and
19777 store it in a variable. For small projects, it is often easier to
19778 specify each of them by hand, since you then have full control over what
19779 is the proper order for these directories, which ones should be
19780 included.
19781
19782 However, in larger projects, which might involve hundreds of
19783 subdirectories, it might be more convenient to generate this list
19784 automatically.
19785
19786 The example below presents two methods. The first one, although less
19787 general, gives you more control over the list. It involves wildcard
19788 characters, that are automatically expanded by @command{make}. Its
19789 shortcoming is that you need to explicitly specify some of the
19790 organization of your project, such as for instance the directory tree
19791 depth, whether some directories are found in a separate tree, @enddots{}
19792
19793 The second method is the most general one. It requires an external
19794 program, called @command{find}, which is standard on all Unix systems. All
19795 the directories found under a given root directory will be added to the
19796 list.
19797
19798 @smallexample
19799 @iftex
19800 @leftskip=0cm
19801 @font@heightrm=cmr8
19802 @heightrm
19803 @end iftex
19804 # The examples below are based on the following directory hierarchy:
19805 # All the directories can contain any number of files
19806 # ROOT_DIRECTORY -> a -> aa -> aaa
19807 # -> ab
19808 # -> ac
19809 # -> b -> ba -> baa
19810 # -> bb
19811 # -> bc
19812 # This Makefile creates a variable called DIRS, that can be reused any time
19813 # you need this list (see the other examples in this section)
19814
19815 # The root of your project's directory hierarchy
19816 ROOT_DIRECTORY=.
19817
19818 ####
19819 # First method: specify explicitly the list of directories
19820 # This allows you to specify any subset of all the directories you need.
19821 ####
19822
19823 DIRS := a/aa/ a/ab/ b/ba/
19824
19825 ####
19826 # Second method: use wildcards
19827 # Note that the argument(s) to wildcard below should end with a '/'.
19828 # Since wildcards also return file names, we have to filter them out
19829 # to avoid duplicate directory names.
19830 # We thus use make's @code{dir} and @code{sort} functions.
19831 # It sets DIRs to the following value (note that the directories aaa and baa
19832 # are not given, unless you change the arguments to wildcard).
19833 # DIRS= ./a/a/ ./b/ ./a/aa/ ./a/ab/ ./a/ac/ ./b/ba/ ./b/bb/ ./b/bc/
19834 ####
19835
19836 DIRS := $@{sort $@{dir $@{wildcard $@{ROOT_DIRECTORY@}/*/
19837 $@{ROOT_DIRECTORY@}/*/*/@}@}@}
19838
19839 ####
19840 # Third method: use an external program
19841 # This command is much faster if run on local disks, avoiding NFS slowdowns.
19842 # This is the most complete command: it sets DIRs to the following value:
19843 # DIRS= ./a ./a/aa ./a/aa/aaa ./a/ab ./a/ac ./b ./b/ba ./b/ba/baa ./b/bb ./b/bc
19844 ####
19845
19846 DIRS := $@{shell find $@{ROOT_DIRECTORY@} -type d -print@}
19847
19848 @end smallexample
19849
19850 @node Generating the Command Line Switches
19851 @section Generating the Command Line Switches
19852
19853 @noindent
19854 Once you have created the list of directories as explained in the
19855 previous section (@pxref{Automatically Creating a List of Directories}),
19856 you can easily generate the command line arguments to pass to gnatmake.
19857
19858 For the sake of completeness, this example assumes that the source path
19859 is not the same as the object path, and that you have two separate lists
19860 of directories.
19861
19862 @smallexample
19863 # see "Automatically creating a list of directories" to create
19864 # these variables
19865 SOURCE_DIRS=
19866 OBJECT_DIRS=
19867
19868 GNATMAKE_SWITCHES := $@{patsubst %,-aI%,$@{SOURCE_DIRS@}@}
19869 GNATMAKE_SWITCHES += $@{patsubst %,-aO%,$@{OBJECT_DIRS@}@}
19870
19871 all:
19872 gnatmake $@{GNATMAKE_SWITCHES@} main_unit
19873 @end smallexample
19874
19875 @node Overcoming Command Line Length Limits
19876 @section Overcoming Command Line Length Limits
19877
19878 @noindent
19879 One problem that might be encountered on big projects is that many
19880 operating systems limit the length of the command line. It is thus hard to give
19881 gnatmake the list of source and object directories.
19882
19883 This example shows how you can set up environment variables, which will
19884 make @command{gnatmake} behave exactly as if the directories had been
19885 specified on the command line, but have a much higher length limit (or
19886 even none on most systems).
19887
19888 It assumes that you have created a list of directories in your Makefile,
19889 using one of the methods presented in
19890 @ref{Automatically Creating a List of Directories}.
19891 For the sake of completeness, we assume that the object
19892 path (where the ALI files are found) is different from the sources patch.
19893
19894 Note a small trick in the Makefile below: for efficiency reasons, we
19895 create two temporary variables (SOURCE_LIST and OBJECT_LIST), that are
19896 expanded immediately by @code{make}. This way we overcome the standard
19897 make behavior which is to expand the variables only when they are
19898 actually used.
19899
19900 On Windows, if you are using the standard Windows command shell, you must
19901 replace colons with semicolons in the assignments to these variables.
19902
19903 @smallexample
19904 @iftex
19905 @leftskip=0cm
19906 @font@heightrm=cmr8
19907 @heightrm
19908 @end iftex
19909 # In this example, we create both ADA_INCLUDE_PATH and ADA_OBJECT_PATH.
19910 # This is the same thing as putting the -I arguments on the command line.
19911 # (the equivalent of using -aI on the command line would be to define
19912 # only ADA_INCLUDE_PATH, the equivalent of -aO is ADA_OBJECT_PATH).
19913 # You can of course have different values for these variables.
19914 #
19915 # Note also that we need to keep the previous values of these variables, since
19916 # they might have been set before running 'make' to specify where the GNAT
19917 # library is installed.
19918
19919 # see "Automatically creating a list of directories" to create these
19920 # variables
19921 SOURCE_DIRS=
19922 OBJECT_DIRS=
19923
19924 empty:=
19925 space:=$@{empty@} $@{empty@}
19926 SOURCE_LIST := $@{subst $@{space@},:,$@{SOURCE_DIRS@}@}
19927 OBJECT_LIST := $@{subst $@{space@},:,$@{OBJECT_DIRS@}@}
19928 ADA_INCLUDE_PATH += $@{SOURCE_LIST@}
19929 ADA_OBJECT_PATH += $@{OBJECT_LIST@}
19930 export ADA_INCLUDE_PATH
19931 export ADA_OBJECT_PATH
19932
19933 all:
19934 gnatmake main_unit
19935 @end smallexample
19936 @end ifclear
19937
19938 @node Memory Management Issues
19939 @chapter Memory Management Issues
19940
19941 @noindent
19942 This chapter describes some useful memory pools provided in the GNAT library
19943 and in particular the GNAT Debug Pool facility, which can be used to detect
19944 incorrect uses of access values (including ``dangling references'').
19945 @ifclear vms
19946 It also describes the @command{gnatmem} tool, which can be used to track down
19947 ``memory leaks''.
19948 @end ifclear
19949
19950 @menu
19951 * Some Useful Memory Pools::
19952 * The GNAT Debug Pool Facility::
19953 @ifclear vms
19954 * The gnatmem Tool::
19955 @end ifclear
19956 @end menu
19957
19958 @node Some Useful Memory Pools
19959 @section Some Useful Memory Pools
19960 @findex Memory Pool
19961 @cindex storage, pool
19962
19963 @noindent
19964 The @code{System.Pool_Global} package offers the Unbounded_No_Reclaim_Pool
19965 storage pool. Allocations use the standard system call @code{malloc} while
19966 deallocations use the standard system call @code{free}. No reclamation is
19967 performed when the pool goes out of scope. For performance reasons, the
19968 standard default Ada allocators/deallocators do not use any explicit storage
19969 pools but if they did, they could use this storage pool without any change in
19970 behavior. That is why this storage pool is used when the user
19971 manages to make the default implicit allocator explicit as in this example:
19972 @smallexample @c ada
19973 type T1 is access Something;
19974 -- no Storage pool is defined for T2
19975 type T2 is access Something_Else;
19976 for T2'Storage_Pool use T1'Storage_Pool;
19977 -- the above is equivalent to
19978 for T2'Storage_Pool use System.Pool_Global.Global_Pool_Object;
19979 @end smallexample
19980
19981 @noindent
19982 The @code{System.Pool_Local} package offers the Unbounded_Reclaim_Pool storage
19983 pool. The allocation strategy is similar to @code{Pool_Local}'s
19984 except that the all
19985 storage allocated with this pool is reclaimed when the pool object goes out of
19986 scope. This pool provides a explicit mechanism similar to the implicit one
19987 provided by several Ada 83 compilers for allocations performed through a local
19988 access type and whose purpose was to reclaim memory when exiting the
19989 scope of a given local access. As an example, the following program does not
19990 leak memory even though it does not perform explicit deallocation:
19991
19992 @smallexample @c ada
19993 with System.Pool_Local;
19994 procedure Pooloc1 is
19995 procedure Internal is
19996 type A is access Integer;
19997 X : System.Pool_Local.Unbounded_Reclaim_Pool;
19998 for A'Storage_Pool use X;
19999 v : A;
20000 begin
20001 for I in 1 .. 50 loop
20002 v := new Integer;
20003 end loop;
20004 end Internal;
20005 begin
20006 for I in 1 .. 100 loop
20007 Internal;
20008 end loop;
20009 end Pooloc1;
20010 @end smallexample
20011
20012 @noindent
20013 The @code{System.Pool_Size} package implements the Stack_Bounded_Pool used when
20014 @code{Storage_Size} is specified for an access type.
20015 The whole storage for the pool is
20016 allocated at once, usually on the stack at the point where the access type is
20017 elaborated. It is automatically reclaimed when exiting the scope where the
20018 access type is defined. This package is not intended to be used directly by the
20019 user and it is implicitly used for each such declaration:
20020
20021 @smallexample @c ada
20022 type T1 is access Something;
20023 for T1'Storage_Size use 10_000;
20024 @end smallexample
20025
20026 @node The GNAT Debug Pool Facility
20027 @section The GNAT Debug Pool Facility
20028 @findex Debug Pool
20029 @cindex storage, pool, memory corruption
20030
20031 @noindent
20032 The use of unchecked deallocation and unchecked conversion can easily
20033 lead to incorrect memory references. The problems generated by such
20034 references are usually difficult to tackle because the symptoms can be
20035 very remote from the origin of the problem. In such cases, it is
20036 very helpful to detect the problem as early as possible. This is the
20037 purpose of the Storage Pool provided by @code{GNAT.Debug_Pools}.
20038
20039 In order to use the GNAT specific debugging pool, the user must
20040 associate a debug pool object with each of the access types that may be
20041 related to suspected memory problems. See Ada Reference Manual 13.11.
20042 @smallexample @c ada
20043 type Ptr is access Some_Type;
20044 Pool : GNAT.Debug_Pools.Debug_Pool;
20045 for Ptr'Storage_Pool use Pool;
20046 @end smallexample
20047
20048 @noindent
20049 @code{GNAT.Debug_Pools} is derived from a GNAT-specific kind of
20050 pool: the @code{Checked_Pool}. Such pools, like standard Ada storage pools,
20051 allow the user to redefine allocation and deallocation strategies. They
20052 also provide a checkpoint for each dereference, through the use of
20053 the primitive operation @code{Dereference} which is implicitly called at
20054 each dereference of an access value.
20055
20056 Once an access type has been associated with a debug pool, operations on
20057 values of the type may raise four distinct exceptions,
20058 which correspond to four potential kinds of memory corruption:
20059 @itemize @bullet
20060 @item
20061 @code{GNAT.Debug_Pools.Accessing_Not_Allocated_Storage}
20062 @item
20063 @code{GNAT.Debug_Pools.Accessing_Deallocated_Storage}
20064 @item
20065 @code{GNAT.Debug_Pools.Freeing_Not_Allocated_Storage}
20066 @item
20067 @code{GNAT.Debug_Pools.Freeing_Deallocated_Storage }
20068 @end itemize
20069
20070 @noindent
20071 For types associated with a Debug_Pool, dynamic allocation is performed using
20072 the standard GNAT allocation routine. References to all allocated chunks of
20073 memory are kept in an internal dictionary. Several deallocation strategies are
20074 provided, whereupon the user can choose to release the memory to the system,
20075 keep it allocated for further invalid access checks, or fill it with an easily
20076 recognizable pattern for debug sessions. The memory pattern is the old IBM
20077 hexadecimal convention: @code{16#DEADBEEF#}.
20078
20079 See the documentation in the file g-debpoo.ads for more information on the
20080 various strategies.
20081
20082 Upon each dereference, a check is made that the access value denotes a
20083 properly allocated memory location. Here is a complete example of use of
20084 @code{Debug_Pools}, that includes typical instances of memory corruption:
20085 @smallexample @c ada
20086 @iftex
20087 @leftskip=0cm
20088 @end iftex
20089 with Gnat.Io; use Gnat.Io;
20090 with Unchecked_Deallocation;
20091 with Unchecked_Conversion;
20092 with GNAT.Debug_Pools;
20093 with System.Storage_Elements;
20094 with Ada.Exceptions; use Ada.Exceptions;
20095 procedure Debug_Pool_Test is
20096
20097 type T is access Integer;
20098 type U is access all T;
20099
20100 P : GNAT.Debug_Pools.Debug_Pool;
20101 for T'Storage_Pool use P;
20102
20103 procedure Free is new Unchecked_Deallocation (Integer, T);
20104 function UC is new Unchecked_Conversion (U, T);
20105 A, B : aliased T;
20106
20107 procedure Info is new GNAT.Debug_Pools.Print_Info(Put_Line);
20108
20109 begin
20110 Info (P);
20111 A := new Integer;
20112 B := new Integer;
20113 B := A;
20114 Info (P);
20115 Free (A);
20116 begin
20117 Put_Line (Integer'Image(B.all));
20118 exception
20119 when E : others => Put_Line ("raised: " & Exception_Name (E));
20120 end;
20121 begin
20122 Free (B);
20123 exception
20124 when E : others => Put_Line ("raised: " & Exception_Name (E));
20125 end;
20126 B := UC(A'Access);
20127 begin
20128 Put_Line (Integer'Image(B.all));
20129 exception
20130 when E : others => Put_Line ("raised: " & Exception_Name (E));
20131 end;
20132 begin
20133 Free (B);
20134 exception
20135 when E : others => Put_Line ("raised: " & Exception_Name (E));
20136 end;
20137 Info (P);
20138 end Debug_Pool_Test;
20139 @end smallexample
20140
20141 @noindent
20142 The debug pool mechanism provides the following precise diagnostics on the
20143 execution of this erroneous program:
20144 @smallexample
20145 Debug Pool info:
20146 Total allocated bytes : 0
20147 Total deallocated bytes : 0
20148 Current Water Mark: 0
20149 High Water Mark: 0
20150
20151 Debug Pool info:
20152 Total allocated bytes : 8
20153 Total deallocated bytes : 0
20154 Current Water Mark: 8
20155 High Water Mark: 8
20156
20157 raised: GNAT.DEBUG_POOLS.ACCESSING_DEALLOCATED_STORAGE
20158 raised: GNAT.DEBUG_POOLS.FREEING_DEALLOCATED_STORAGE
20159 raised: GNAT.DEBUG_POOLS.ACCESSING_NOT_ALLOCATED_STORAGE
20160 raised: GNAT.DEBUG_POOLS.FREEING_NOT_ALLOCATED_STORAGE
20161 Debug Pool info:
20162 Total allocated bytes : 8
20163 Total deallocated bytes : 4
20164 Current Water Mark: 4
20165 High Water Mark: 8
20166 @end smallexample
20167
20168 @ifclear vms
20169 @node The gnatmem Tool
20170 @section The @command{gnatmem} Tool
20171 @findex gnatmem
20172
20173 @noindent
20174 The @code{gnatmem} utility monitors dynamic allocation and
20175 deallocation activity in a program, and displays information about
20176 incorrect deallocations and possible sources of memory leaks.
20177 It is designed to work in association with a static runtime library
20178 only and in this context provides three types of information:
20179 @itemize @bullet
20180 @item
20181 General information concerning memory management, such as the total
20182 number of allocations and deallocations, the amount of allocated
20183 memory and the high water mark, i.e.@: the largest amount of allocated
20184 memory in the course of program execution.
20185
20186 @item
20187 Backtraces for all incorrect deallocations, that is to say deallocations
20188 which do not correspond to a valid allocation.
20189
20190 @item
20191 Information on each allocation that is potentially the origin of a memory
20192 leak.
20193 @end itemize
20194
20195 @menu
20196 * Running gnatmem::
20197 * Switches for gnatmem::
20198 * Example of gnatmem Usage::
20199 @end menu
20200
20201 @node Running gnatmem
20202 @subsection Running @code{gnatmem}
20203
20204 @noindent
20205 @code{gnatmem} makes use of the output created by the special version of
20206 allocation and deallocation routines that record call information. This
20207 allows to obtain accurate dynamic memory usage history at a minimal cost to
20208 the execution speed. Note however, that @code{gnatmem} is not supported on
20209 all platforms (currently, it is supported on AIX, HP-UX, GNU/Linux,
20210 Solaris and Windows NT/2000/XP (x86).
20211
20212 @noindent
20213 The @code{gnatmem} command has the form
20214
20215 @smallexample
20216 @c $ gnatmem @ovar{switches} user_program
20217 @c Expanding @ovar macro inline (explanation in macro def comments)
20218 $ gnatmem @r{[}@var{switches}@r{]} @var{user_program}
20219 @end smallexample
20220
20221 @noindent
20222 The program must have been linked with the instrumented version of the
20223 allocation and deallocation routines. This is done by linking with the
20224 @file{libgmem.a} library. For correct symbolic backtrace information,
20225 the user program should be compiled with debugging options
20226 (see @ref{Switches for gcc}). For example to build @file{my_program}:
20227
20228 @smallexample
20229 $ gnatmake -g my_program -largs -lgmem
20230 @end smallexample
20231
20232 @noindent
20233 As library @file{libgmem.a} contains an alternate body for package
20234 @code{System.Memory}, @file{s-memory.adb} should not be compiled and linked
20235 when an executable is linked with library @file{libgmem.a}. It is then not
20236 recommended to use @command{gnatmake} with switch @option{^-a^/ALL_FILES^}.
20237
20238 @noindent
20239 When @file{my_program} is executed, the file @file{gmem.out} is produced.
20240 This file contains information about all allocations and deallocations
20241 performed by the program. It is produced by the instrumented allocations and
20242 deallocations routines and will be used by @code{gnatmem}.
20243
20244 In order to produce symbolic backtrace information for allocations and
20245 deallocations performed by the GNAT run-time library, you need to use a
20246 version of that library that has been compiled with the @option{-g} switch
20247 (see @ref{Rebuilding the GNAT Run-Time Library}).
20248
20249 Gnatmem must be supplied with the @file{gmem.out} file and the executable to
20250 examine. If the location of @file{gmem.out} file was not explicitly supplied by
20251 @option{-i} switch, gnatmem will assume that this file can be found in the
20252 current directory. For example, after you have executed @file{my_program},
20253 @file{gmem.out} can be analyzed by @code{gnatmem} using the command:
20254
20255 @smallexample
20256 $ gnatmem my_program
20257 @end smallexample
20258
20259 @noindent
20260 This will produce the output with the following format:
20261
20262 *************** debut cc
20263 @smallexample
20264 $ gnatmem my_program
20265
20266 Global information
20267 ------------------
20268 Total number of allocations : 45
20269 Total number of deallocations : 6
20270 Final Water Mark (non freed mem) : 11.29 Kilobytes
20271 High Water Mark : 11.40 Kilobytes
20272
20273 .
20274 .
20275 .
20276 Allocation Root # 2
20277 -------------------
20278 Number of non freed allocations : 11
20279 Final Water Mark (non freed mem) : 1.16 Kilobytes
20280 High Water Mark : 1.27 Kilobytes
20281 Backtrace :
20282 my_program.adb:23 my_program.alloc
20283 .
20284 .
20285 .
20286 @end smallexample
20287
20288 The first block of output gives general information. In this case, the
20289 Ada construct ``@code{@b{new}}'' was executed 45 times, and only 6 calls to an
20290 Unchecked_Deallocation routine occurred.
20291
20292 @noindent
20293 Subsequent paragraphs display information on all allocation roots.
20294 An allocation root is a specific point in the execution of the program
20295 that generates some dynamic allocation, such as a ``@code{@b{new}}''
20296 construct. This root is represented by an execution backtrace (or subprogram
20297 call stack). By default the backtrace depth for allocations roots is 1, so
20298 that a root corresponds exactly to a source location. The backtrace can
20299 be made deeper, to make the root more specific.
20300
20301 @node Switches for gnatmem
20302 @subsection Switches for @code{gnatmem}
20303
20304 @noindent
20305 @code{gnatmem} recognizes the following switches:
20306
20307 @table @option
20308
20309 @item -q
20310 @cindex @option{-q} (@code{gnatmem})
20311 Quiet. Gives the minimum output needed to identify the origin of the
20312 memory leaks. Omits statistical information.
20313
20314 @item @var{N}
20315 @cindex @var{N} (@code{gnatmem})
20316 N is an integer literal (usually between 1 and 10) which controls the
20317 depth of the backtraces defining allocation root. The default value for
20318 N is 1. The deeper the backtrace, the more precise the localization of
20319 the root. Note that the total number of roots can depend on this
20320 parameter. This parameter must be specified @emph{before} the name of the
20321 executable to be analyzed, to avoid ambiguity.
20322
20323 @item -b n
20324 @cindex @option{-b} (@code{gnatmem})
20325 This switch has the same effect as just depth parameter.
20326
20327 @item -i @var{file}
20328 @cindex @option{-i} (@code{gnatmem})
20329 Do the @code{gnatmem} processing starting from @file{file}, rather than
20330 @file{gmem.out} in the current directory.
20331
20332 @item -m n
20333 @cindex @option{-m} (@code{gnatmem})
20334 This switch causes @code{gnatmem} to mask the allocation roots that have less
20335 than n leaks. The default value is 1. Specifying the value of 0 will allow to
20336 examine even the roots that didn't result in leaks.
20337
20338 @item -s order
20339 @cindex @option{-s} (@code{gnatmem})
20340 This switch causes @code{gnatmem} to sort the allocation roots according to the
20341 specified order of sort criteria, each identified by a single letter. The
20342 currently supported criteria are @code{n, h, w} standing respectively for
20343 number of unfreed allocations, high watermark, and final watermark
20344 corresponding to a specific root. The default order is @code{nwh}.
20345
20346 @end table
20347
20348 @node Example of gnatmem Usage
20349 @subsection Example of @code{gnatmem} Usage
20350
20351 @noindent
20352 The following example shows the use of @code{gnatmem}
20353 on a simple memory-leaking program.
20354 Suppose that we have the following Ada program:
20355
20356 @smallexample @c ada
20357 @group
20358 @cartouche
20359 with Unchecked_Deallocation;
20360 procedure Test_Gm is
20361
20362 type T is array (1..1000) of Integer;
20363 type Ptr is access T;
20364 procedure Free is new Unchecked_Deallocation (T, Ptr);
20365 A : Ptr;
20366
20367 procedure My_Alloc is
20368 begin
20369 A := new T;
20370 end My_Alloc;
20371
20372 procedure My_DeAlloc is
20373 B : Ptr := A;
20374 begin
20375 Free (B);
20376 end My_DeAlloc;
20377
20378 begin
20379 My_Alloc;
20380 for I in 1 .. 5 loop
20381 for J in I .. 5 loop
20382 My_Alloc;
20383 end loop;
20384 My_Dealloc;
20385 end loop;
20386 end;
20387 @end cartouche
20388 @end group
20389 @end smallexample
20390
20391 @noindent
20392 The program needs to be compiled with debugging option and linked with
20393 @code{gmem} library:
20394
20395 @smallexample
20396 $ gnatmake -g test_gm -largs -lgmem
20397 @end smallexample
20398
20399 @noindent
20400 Then we execute the program as usual:
20401
20402 @smallexample
20403 $ test_gm
20404 @end smallexample
20405
20406 @noindent
20407 Then @code{gnatmem} is invoked simply with
20408 @smallexample
20409 $ gnatmem test_gm
20410 @end smallexample
20411
20412 @noindent
20413 which produces the following output (result may vary on different platforms):
20414
20415 @smallexample
20416 Global information
20417 ------------------
20418 Total number of allocations : 18
20419 Total number of deallocations : 5
20420 Final Water Mark (non freed mem) : 53.00 Kilobytes
20421 High Water Mark : 56.90 Kilobytes
20422
20423 Allocation Root # 1
20424 -------------------
20425 Number of non freed allocations : 11
20426 Final Water Mark (non freed mem) : 42.97 Kilobytes
20427 High Water Mark : 46.88 Kilobytes
20428 Backtrace :
20429 test_gm.adb:11 test_gm.my_alloc
20430
20431 Allocation Root # 2
20432 -------------------
20433 Number of non freed allocations : 1
20434 Final Water Mark (non freed mem) : 10.02 Kilobytes
20435 High Water Mark : 10.02 Kilobytes
20436 Backtrace :
20437 s-secsta.adb:81 system.secondary_stack.ss_init
20438
20439 Allocation Root # 3
20440 -------------------
20441 Number of non freed allocations : 1
20442 Final Water Mark (non freed mem) : 12 Bytes
20443 High Water Mark : 12 Bytes
20444 Backtrace :
20445 s-secsta.adb:181 system.secondary_stack.ss_init
20446 @end smallexample
20447
20448 @noindent
20449 Note that the GNAT run time contains itself a certain number of
20450 allocations that have no corresponding deallocation,
20451 as shown here for root #2 and root
20452 #3. This is a normal behavior when the number of non-freed allocations
20453 is one, it allocates dynamic data structures that the run time needs for
20454 the complete lifetime of the program. Note also that there is only one
20455 allocation root in the user program with a single line back trace:
20456 test_gm.adb:11 test_gm.my_alloc, whereas a careful analysis of the
20457 program shows that 'My_Alloc' is called at 2 different points in the
20458 source (line 21 and line 24). If those two allocation roots need to be
20459 distinguished, the backtrace depth parameter can be used:
20460
20461 @smallexample
20462 $ gnatmem 3 test_gm
20463 @end smallexample
20464
20465 @noindent
20466 which will give the following output:
20467
20468 @smallexample
20469 Global information
20470 ------------------
20471 Total number of allocations : 18
20472 Total number of deallocations : 5
20473 Final Water Mark (non freed mem) : 53.00 Kilobytes
20474 High Water Mark : 56.90 Kilobytes
20475
20476 Allocation Root # 1
20477 -------------------
20478 Number of non freed allocations : 10
20479 Final Water Mark (non freed mem) : 39.06 Kilobytes
20480 High Water Mark : 42.97 Kilobytes
20481 Backtrace :
20482 test_gm.adb:11 test_gm.my_alloc
20483 test_gm.adb:24 test_gm
20484 b_test_gm.c:52 main
20485
20486 Allocation Root # 2
20487 -------------------
20488 Number of non freed allocations : 1
20489 Final Water Mark (non freed mem) : 10.02 Kilobytes
20490 High Water Mark : 10.02 Kilobytes
20491 Backtrace :
20492 s-secsta.adb:81 system.secondary_stack.ss_init
20493 s-secsta.adb:283 <system__secondary_stack___elabb>
20494 b_test_gm.c:33 adainit
20495
20496 Allocation Root # 3
20497 -------------------
20498 Number of non freed allocations : 1
20499 Final Water Mark (non freed mem) : 3.91 Kilobytes
20500 High Water Mark : 3.91 Kilobytes
20501 Backtrace :
20502 test_gm.adb:11 test_gm.my_alloc
20503 test_gm.adb:21 test_gm
20504 b_test_gm.c:52 main
20505
20506 Allocation Root # 4
20507 -------------------
20508 Number of non freed allocations : 1
20509 Final Water Mark (non freed mem) : 12 Bytes
20510 High Water Mark : 12 Bytes
20511 Backtrace :
20512 s-secsta.adb:181 system.secondary_stack.ss_init
20513 s-secsta.adb:283 <system__secondary_stack___elabb>
20514 b_test_gm.c:33 adainit
20515 @end smallexample
20516
20517 @noindent
20518 The allocation root #1 of the first example has been split in 2 roots #1
20519 and #3 thanks to the more precise associated backtrace.
20520
20521 @end ifclear
20522
20523 @node Stack Related Facilities
20524 @chapter Stack Related Facilities
20525
20526 @noindent
20527 This chapter describes some useful tools associated with stack
20528 checking and analysis. In
20529 particular, it deals with dynamic and static stack usage measurements.
20530
20531 @menu
20532 * Stack Overflow Checking::
20533 * Static Stack Usage Analysis::
20534 * Dynamic Stack Usage Analysis::
20535 @end menu
20536
20537 @node Stack Overflow Checking
20538 @section Stack Overflow Checking
20539 @cindex Stack Overflow Checking
20540 @cindex -fstack-check
20541
20542 @noindent
20543 For most operating systems, @command{gcc} does not perform stack overflow
20544 checking by default. This means that if the main environment task or
20545 some other task exceeds the available stack space, then unpredictable
20546 behavior will occur. Most native systems offer some level of protection by
20547 adding a guard page at the end of each task stack. This mechanism is usually
20548 not enough for dealing properly with stack overflow situations because
20549 a large local variable could ``jump'' above the guard page.
20550 Furthermore, when the
20551 guard page is hit, there may not be any space left on the stack for executing
20552 the exception propagation code. Enabling stack checking avoids
20553 such situations.
20554
20555 To activate stack checking, compile all units with the gcc option
20556 @option{-fstack-check}. For example:
20557
20558 @smallexample
20559 gcc -c -fstack-check package1.adb
20560 @end smallexample
20561
20562 @noindent
20563 Units compiled with this option will generate extra instructions to check
20564 that any use of the stack (for procedure calls or for declaring local
20565 variables in declare blocks) does not exceed the available stack space.
20566 If the space is exceeded, then a @code{Storage_Error} exception is raised.
20567
20568 For declared tasks, the stack size is controlled by the size
20569 given in an applicable @code{Storage_Size} pragma or by the value specified
20570 at bind time with @option{-d} (@pxref{Switches for gnatbind}) or is set to
20571 the default size as defined in the GNAT runtime otherwise.
20572
20573 For the environment task, the stack size depends on
20574 system defaults and is unknown to the compiler. Stack checking
20575 may still work correctly if a fixed
20576 size stack is allocated, but this cannot be guaranteed.
20577 @ifclear vms
20578 To ensure that a clean exception is signalled for stack
20579 overflow, set the environment variable
20580 @env{GNAT_STACK_LIMIT} to indicate the maximum
20581 stack area that can be used, as in:
20582 @cindex GNAT_STACK_LIMIT
20583
20584 @smallexample
20585 SET GNAT_STACK_LIMIT 1600
20586 @end smallexample
20587
20588 @noindent
20589 The limit is given in kilobytes, so the above declaration would
20590 set the stack limit of the environment task to 1.6 megabytes.
20591 Note that the only purpose of this usage is to limit the amount
20592 of stack used by the environment task. If it is necessary to
20593 increase the amount of stack for the environment task, then this
20594 is an operating systems issue, and must be addressed with the
20595 appropriate operating systems commands.
20596 @end ifclear
20597 @ifset vms
20598 To have a fixed size stack in the environment task, the stack must be put
20599 in the P0 address space and its size specified. Use these switches to
20600 create a p0 image:
20601
20602 @smallexample
20603 gnatmake my_progs -largs "-Wl,--opt=STACK=4000,/p0image"
20604 @end smallexample
20605
20606 @noindent
20607 The quotes are required to keep case. The number after @samp{STACK=} is the
20608 size of the environmental task stack in pagelets (512 bytes). In this example
20609 the stack size is about 2 megabytes.
20610
20611 @noindent
20612 A consequence of the @option{/p0image} qualifier is also to makes RMS buffers
20613 be placed in P0 space. Refer to @cite{HP OpenVMS Linker Utility Manual} for
20614 more details about the @option{/p0image} qualifier and the @option{stack}
20615 option.
20616 @end ifset
20617
20618 @node Static Stack Usage Analysis
20619 @section Static Stack Usage Analysis
20620 @cindex Static Stack Usage Analysis
20621 @cindex -fstack-usage
20622
20623 @noindent
20624 A unit compiled with @option{-fstack-usage} will generate an extra file
20625 that specifies
20626 the maximum amount of stack used, on a per-function basis.
20627 The file has the same
20628 basename as the target object file with a @file{.su} extension.
20629 Each line of this file is made up of three fields:
20630
20631 @itemize
20632 @item
20633 The name of the function.
20634 @item
20635 A number of bytes.
20636 @item
20637 One or more qualifiers: @code{static}, @code{dynamic}, @code{bounded}.
20638 @end itemize
20639
20640 The second field corresponds to the size of the known part of the function
20641 frame.
20642
20643 The qualifier @code{static} means that the function frame size
20644 is purely static.
20645 It usually means that all local variables have a static size.
20646 In this case, the second field is a reliable measure of the function stack
20647 utilization.
20648
20649 The qualifier @code{dynamic} means that the function frame size is not static.
20650 It happens mainly when some local variables have a dynamic size. When this
20651 qualifier appears alone, the second field is not a reliable measure
20652 of the function stack analysis. When it is qualified with @code{bounded}, it
20653 means that the second field is a reliable maximum of the function stack
20654 utilization.
20655
20656 @node Dynamic Stack Usage Analysis
20657 @section Dynamic Stack Usage Analysis
20658
20659 @noindent
20660 It is possible to measure the maximum amount of stack used by a task, by
20661 adding a switch to @command{gnatbind}, as:
20662
20663 @smallexample
20664 $ gnatbind -u0 file
20665 @end smallexample
20666
20667 @noindent
20668 With this option, at each task termination, its stack usage is output on
20669 @file{stderr}.
20670 It is not always convenient to output the stack usage when the program
20671 is still running. Hence, it is possible to delay this output until program
20672 termination. for a given number of tasks specified as the argument of the
20673 @option{-u} option. For instance:
20674
20675 @smallexample
20676 $ gnatbind -u100 file
20677 @end smallexample
20678
20679 @noindent
20680 will buffer the stack usage information of the first 100 tasks to terminate and
20681 output this info at program termination. Results are displayed in four
20682 columns:
20683
20684 @noindent
20685 Index | Task Name | Stack Size | Stack Usage [Value +/- Variation]
20686
20687 @noindent
20688 where:
20689
20690 @table @emph
20691 @item Index
20692 is a number associated with each task.
20693
20694 @item Task Name
20695 is the name of the task analyzed.
20696
20697 @item Stack Size
20698 is the maximum size for the stack.
20699
20700 @item Stack Usage
20701 is the measure done by the stack analyzer. In order to prevent overflow, the stack
20702 is not entirely analyzed, and it's not possible to know exactly how
20703 much has actually been used. The report thus contains the theoretical stack usage
20704 (Value) and the possible variation (Variation) around this value.
20705
20706 @end table
20707
20708 @noindent
20709 The environment task stack, e.g., the stack that contains the main unit, is
20710 only processed when the environment variable GNAT_STACK_LIMIT is set.
20711
20712
20713 @c *********************************
20714 @c * GNATCHECK *
20715 @c *********************************
20716 @node Verifying Properties Using gnatcheck
20717 @chapter Verifying Properties Using @command{gnatcheck}
20718 @findex gnatcheck
20719 @cindex @command{gnatcheck}
20720
20721 @noindent
20722 The @command{gnatcheck} tool is an ASIS-based utility that checks properties
20723 of Ada source files according to a given set of semantic rules.
20724 @cindex ASIS
20725
20726 In order to check compliance with a given rule, @command{gnatcheck} has to
20727 semantically analyze the Ada sources.
20728 Therefore, checks can only be performed on
20729 legal Ada units. Moreover, when a unit depends semantically upon units located
20730 outside the current directory, the source search path has to be provided when
20731 calling @command{gnatcheck}, either through a specified project file or
20732 through @command{gnatcheck} switches as described below.
20733
20734 A number of rules are predefined in @command{gnatcheck} and are described
20735 later in this chapter.
20736 You can also add new rules, by modifying the @command{gnatcheck} code and
20737 rebuilding the tool. In order to add a simple rule making some local checks,
20738 a small amount of straightforward ASIS-based programming is usually needed.
20739
20740 Project support for @command{gnatcheck} is provided by the GNAT
20741 driver (see @ref{The GNAT Driver and Project Files}).
20742
20743 Invoking @command{gnatcheck} on the command line has the form:
20744
20745 @smallexample
20746 @c $ gnatcheck @ovar{switches} @{@var{filename}@}
20747 @c @r{[}^-files^/FILES^=@{@var{arg_list_filename}@}@r{]}
20748 @c @r{[}-cargs @var{gcc_switches}@r{]} -rules @var{rule_options}
20749 @c Expanding @ovar macro inline (explanation in macro def comments)
20750 $ gnatcheck @r{[}@var{switches}@r{]} @{@var{filename}@}
20751 @r{[}^-files^/FILES^=@{@var{arg_list_filename}@}@r{]}
20752 @r{[}-cargs @var{gcc_switches}@r{]} -rules @var{rule_options}
20753 @end smallexample
20754
20755 @noindent
20756 where
20757 @itemize @bullet
20758 @item
20759 @var{switches} specify the general tool options
20760
20761 @item
20762 Each @var{filename} is the name (including the extension) of a source
20763 file to process. ``Wildcards'' are allowed, and
20764 the file name may contain path information.
20765
20766 @item
20767 Each @var{arg_list_filename} is the name (including the extension) of a text
20768 file containing the names of the source files to process, separated by spaces
20769 or line breaks.
20770
20771 @item
20772 @var{gcc_switches} is a list of switches for
20773 @command{gcc}. They will be passed on to all compiler invocations made by
20774 @command{gnatcheck} to generate the ASIS trees. Here you can provide
20775 @option{^-I^/INCLUDE_DIRS=^} switches to form the source search path,
20776 and use the @option{-gnatec} switch to set the configuration file.
20777
20778 @item
20779 @var{rule_options} is a list of options for controlling a set of
20780 rules to be checked by @command{gnatcheck} (@pxref{gnatcheck Rule Options}).
20781 @end itemize
20782
20783 @noindent
20784 Either a @file{@var{filename}} or an @file{@var{arg_list_filename}} must be
20785 supplied.
20786
20787 @menu
20788 * Format of the Report File::
20789 * General gnatcheck Switches::
20790 * gnatcheck Rule Options::
20791 * Adding the Results of Compiler Checks to gnatcheck Output::
20792 * Project-Wide Checks::
20793 * Rule exemption::
20794 * Predefined Rules::
20795 * Example of gnatcheck Usage::
20796 @end menu
20797
20798 @node Format of the Report File
20799 @section Format of the Report File
20800 @cindex Report file (for @code{gnatcheck})
20801
20802 @noindent
20803 The @command{gnatcheck} tool outputs on @file{stdout} all messages concerning
20804 rule violations.
20805 It also creates a text file that
20806 contains the complete report of the last gnatcheck run. By default this file
20807 is named named @file{^gnatcheck.out^GNATCHECK.OUT^} and it is located in the
20808 current directory; the @option{^-o^/OUTPUT^} option can be used to change the
20809 name and/or location of the report file. This report contains:
20810 @itemize @bullet
20811 @item date and time of @command{gnatcheck} run, the version of
20812 the tool that has generated this report and the full parameters
20813 of the @command{gnatcheck} invocation;
20814 @item list of enabled rules;
20815 @item total number of detected violations;
20816 @item list of source files where rule violations have been detected;
20817 @item list of source files where no violations have been detected.
20818 @end itemize
20819
20820 @node General gnatcheck Switches
20821 @section General @command{gnatcheck} Switches
20822
20823 @noindent
20824 The following switches control the general @command{gnatcheck} behavior
20825
20826 @table @option
20827 @c !sort!
20828 @cindex @option{^-a^/ALL^} (@command{gnatcheck})
20829 @item ^-a^/ALL^
20830 Process all units including those with read-only ALI files such as
20831 those from the GNAT Run-Time library.
20832
20833 @ifclear vms
20834 @ignore
20835 @cindex @option{-d} (@command{gnatcheck})
20836 @item -d
20837 Debug mode
20838 @end ignore
20839
20840 @cindex @option{-dd} (@command{gnatcheck})
20841 @item -dd
20842 Progress indicator mode (for use in GPS).
20843 @end ifclear
20844
20845 @cindex @option{^-h^/HELP^} (@command{gnatcheck})
20846 @item ^-h^/HELP^
20847 List the predefined and user-defined rules. For more details see
20848 @ref{Predefined Rules}.
20849
20850 @cindex @option{^-l^/LOCS^} (@command{gnatcheck})
20851 @item ^-l^/LOCS^
20852 Use full source locations references in the report file. For a construct from
20853 a generic instantiation a full source location is a chain from the location
20854 of this construct in the generic unit to the place where this unit is
20855 instantiated.
20856
20857 @cindex @option{^-log^/LOG^} (@command{gnatcheck})
20858 @item ^-log^/LOG^
20859 Duplicate all the output sent to @file{stderr} into a log file. The log file
20860 is named @file{gnatcheck.log} and is located in the current directory.
20861
20862 @cindex @option{^-m^/DIAGNOSTIC_LIMIT^} (@command{gnatcheck})
20863 @item ^-m@i{nnnn}^/DIAGNOSTIC_LIMIT=@i{nnnn}^
20864 Maximum number of diagnostics to be sent to @file{stdout}, where @i{nnnn} is in
20865 the range 0@dots{}1000;
20866 the default value is 500. Zero means that there is no limitation on
20867 the number of diagnostic messages to be output.
20868
20869 @cindex @option{^-q^/QUIET^} (@command{gnatcheck})
20870 @item ^-q^/QUIET^
20871 Quiet mode. All the diagnostics about rule violations are placed in the
20872 @command{gnatcheck} report file only, without duplication on @file{stdout}.
20873
20874 @cindex @option{^-s^/SHORT^} (@command{gnatcheck})
20875 @item ^-s^/SHORT^
20876 Short format of the report file (no version information, no list of applied
20877 rules, no list of checked sources is included)
20878
20879 @cindex @option{^--include-file=@var{file}^/INCLUDE_FILE=@var{file}^} (@command{gnatcheck})
20880 @item ^--include-file^/INCLUDE_FILE^
20881 Append the content of the specified text file to the report file
20882
20883 @cindex @option{^-t^/TIME^} (@command{gnatcheck})
20884 @item ^-t^/TIME^
20885 Print out execution time.
20886
20887 @cindex @option{^-v^/VERBOSE^} (@command{gnatcheck})
20888 @item ^-v^/VERBOSE^
20889 Verbose mode; @command{gnatcheck} generates version information and then
20890 a trace of sources being processed.
20891
20892 @cindex @option{^-o ^/OUTPUT^} (@command{gnatcheck})
20893 @item ^-o ^/OUTPUT=^@var{report_file}
20894 Set name of report file file to @var{report_file} .
20895
20896 @end table
20897
20898 @noindent
20899 Note that if any of the options @option{^-s1^/COMPILER_STYLE^},
20900 @option{^-s2^/BY_RULES^} or
20901 @option{^-s3^/BY_FILES_BY_RULES^} is specified,
20902 then the @command{gnatcheck} report file will only contain sections
20903 explicitly denoted by these options.
20904
20905 @node gnatcheck Rule Options
20906 @section @command{gnatcheck} Rule Options
20907
20908 @noindent
20909 The following options control the processing performed by
20910 @command{gnatcheck}.
20911
20912 @table @option
20913 @cindex @option{+ALL} (@command{gnatcheck})
20914 @item +ALL
20915 Turn all the rule checks ON.
20916
20917 @cindex @option{-ALL} (@command{gnatcheck})
20918 @item -ALL
20919 Turn all the rule checks OFF.
20920
20921 @cindex @option{+R} (@command{gnatcheck})
20922 @item +R@var{rule_id}@r{[}:@var{param}@r{]}
20923 Turn on the check for a specified rule with the specified parameter, if any.
20924 @var{rule_id} must be the identifier of one of the currently implemented rules
20925 (use @option{^-h^/HELP^} for the list of implemented rules). Rule identifiers
20926 are not case-sensitive. The @var{param} item must
20927 be a string representing a valid parameter(s) for the specified rule.
20928 If it contains any space characters then this string must be enclosed in
20929 quotation marks.
20930
20931 @cindex @option{-R} (@command{gnatcheck})
20932 @item -R@var{rule_id}@r{[}:@var{param}@r{]}
20933 Turn off the check for a specified rule with the specified parameter, if any.
20934
20935 @cindex @option{-from} (@command{gnatcheck})
20936 @item -from=@var{rule_option_filename}
20937 Read the rule options from the text file @var{rule_option_filename}, referred
20938 to as a ``coding standard file'' below.
20939
20940 @end table
20941
20942 @noindent
20943 The default behavior is that all the rule checks are disabled.
20944
20945 A coding standard file is a text file that contains a set of rule options
20946 described above.
20947 @cindex Coding standard file (for @code{gnatcheck})
20948 The file may contain empty lines and Ada-style comments (comment
20949 lines and end-of-line comments). There can be several rule options on a
20950 single line (separated by a space).
20951
20952 A coding standard file may reference other coding standard files by including
20953 more @option{-from=@var{rule_option_filename}}
20954 options, each such option being replaced with the content of the
20955 corresponding coding standard file during processing. In case a
20956 cycle is detected (that is, @file{@var{rule_file_1}} reads rule options
20957 from @file{@var{rule_file_2}}, and @file{@var{rule_file_2}} reads
20958 (directly or indirectly) rule options from @file{@var{rule_file_1}}),
20959 processing fails with an error message.
20960
20961
20962 @node Adding the Results of Compiler Checks to gnatcheck Output
20963 @section Adding the Results of Compiler Checks to @command{gnatcheck} Output
20964
20965 @noindent
20966 The @command{gnatcheck} tool can include in the generated diagnostic messages
20967 and in
20968 the report file the results of the checks performed by the compiler. Though
20969 disabled by default, this effect may be obtained by using @option{+R} with
20970 the following rule identifiers and parameters:
20971
20972 @table @option
20973 @item Restrictions
20974 To record restrictions violations (which are performed by the compiler if the
20975 pragma @code{Restrictions} or @code{Restriction_Warnings} are given),
20976 use the @code{Restrictions} rule
20977 with the same parameters as pragma
20978 @code{Restrictions} or @code{Restriction_Warnings}.
20979
20980 @item Style_Checks
20981 To record compiler style checks (@pxref{Style Checking}), use the
20982 @code{Style_Checks} rule.
20983 This rule takes a parameter in one of the following forms:
20984 @itemize
20985 @item
20986 @code{All_Checks},
20987 which enables the standard style checks corresponding to the @option{-gnatyy}
20988 GNAT style check option, or
20989
20990 @item
20991 a string with the same
20992 structure and semantics as the @code{string_LITERAL} parameter of the
20993 GNAT pragma @code{Style_Checks}
20994 (for further information about this pragma,
20995 @pxref{Pragma Style_Checks,,, gnat_rm, GNAT Reference Manual}).
20996 @end itemize
20997
20998 @noindent
20999 For example, the
21000 @code{+RStyle_Checks:O} rule option activates
21001 the compiler style check that corresponds to
21002 @code{-gnatyO} style check option.
21003
21004 @item Warnings
21005 To record compiler warnings (@pxref{Warning Message Control}), use the
21006 @code{Warnings} rule with a parameter that is a valid
21007 @i{static_string_expression} argument of the GNAT pragma @code{Warnings}
21008 (for further information about this pragma,
21009 @pxref{Pragma Warnings,,,gnat_rm, GNAT Reference Manual}).
21010 Note that in case of gnatcheck
21011 's' parameter, that corresponds to the GNAT @option{-gnatws} option, disables
21012 all the specific warnings, but not suppresses the warning mode,
21013 and 'e' parameter, corresponding to @option{-gnatwe} that means
21014 "treat warnings as errors", does not have any effect.
21015
21016 @end table
21017
21018 To disable a specific restriction check, use @code{-RStyle_Checks} gnatcheck
21019 option with the corresponding restriction name as a parameter. @code{-R} is
21020 not available for @code{Style_Checks} and @code{Warnings} options, to disable
21021 warnings and style checks, use the corresponding warning and style options.
21022
21023 @node Project-Wide Checks
21024 @section Project-Wide Checks
21025 @cindex Project-wide checks (for @command{gnatcheck})
21026
21027 @noindent
21028 In order to perform checks on all units of a given project, you can use
21029 the GNAT driver along with the @option{-P} option:
21030 @smallexample
21031 gnat check -Pproj -rules -from=my_rules
21032 @end smallexample
21033
21034 @noindent
21035 If the project @code{proj} depends upon other projects, you can perform
21036 checks on the project closure using the @option{-U} option:
21037 @smallexample
21038 gnat check -Pproj -U -rules -from=my_rules
21039 @end smallexample
21040
21041 @noindent
21042 Finally, if not all the units are relevant to a particular main
21043 program in the project closure, you can perform checks for the set
21044 of units needed to create a given main program (unit closure) using
21045 the @option{-U} option followed by the name of the main unit:
21046 @smallexample
21047 gnat check -Pproj -U main -rules -from=my_rules
21048 @end smallexample
21049
21050
21051 @node Rule exemption
21052 @section Rule exemption
21053 @cindex Rule exemption (for @command{gnatcheck})
21054
21055 @noindent
21056 One of the most useful applications of @command{gnatcheck} is to
21057 automate the enforcement of project-specific coding standards,
21058 for example in safety-critical systems where particular features
21059 must be restricted in order to simplify the certification effort.
21060 However, it may sometimes be appropriate to violate a coding standard rule,
21061 and in such cases the rationale for the violation should be provided
21062 in the source program itself so that the individuals
21063 reviewing or maintaining the program can immediately understand the intent.
21064
21065 The @command{gnatcheck} tool supports this practice with the notion of
21066 a ``rule exemption'' covering a specific source code section. Normally
21067 rule violation messages are issued both on @file{stderr}
21068 and in a report file. In contrast, exempted violations are not listed on
21069 @file{stderr}; thus users invoking @command{gnatcheck} interactively
21070 (e.g. in its GPS interface) do not need to pay attention to known and
21071 justified violations. However, exempted violations along with their
21072 justification are documented in a special section of the report file that
21073 @command{gnatcheck} generates.
21074
21075 @menu
21076 * Using pragma Annotate to Control Rule Exemption::
21077 * gnatcheck Annotations Rules::
21078 @end menu
21079
21080 @node Using pragma Annotate to Control Rule Exemption
21081 @subsection Using pragma @code{Annotate} to Control Rule Exemption
21082 @cindex Using pragma Annotate to control rule exemption
21083
21084 @noindent
21085 Rule exemption is controlled by pragma @code{Annotate} when its first
21086 argument is ``gnatcheck''. The syntax of @command{gnatcheck}'s
21087 exemption control annotations is as follows:
21088
21089 @smallexample @c ada
21090 @group
21091 pragma Annotate (gnatcheck, @i{exemption_control}, @i{Rule_Name}, [@i{justification}]);
21092
21093 @i{exemption_control} ::= Exempt_On | Exempt_Off
21094
21095 @i{Rule_Name} ::= string_literal
21096
21097 @i{justification} ::= string_literal
21098 @end group
21099 @end smallexample
21100
21101 @noindent
21102 When a @command{gnatcheck} annotation has more then four arguments,
21103 @command{gnatcheck} issues a warning and ignores the additional arguments.
21104 If the additional arguments do not follow the syntax above,
21105 @command{gnatcheck} emits a warning and ignores the annotation.
21106
21107 The @i{@code{Rule_Name}} argument should be the name of some existing
21108 @command{gnatcheck} rule.
21109 Otherwise a warning message is generated and the pragma is
21110 ignored. If @code{Rule_Name} denotes a rule that is not activated by the given
21111 @command{gnatcheck} call, the pragma is ignored and no warning is issued.
21112
21113 A source code section where an exemption is active for a given rule is
21114 delimited by an @code{exempt_on} and @code{exempt_off} annotation pair:
21115
21116 @smallexample @c ada
21117 pragma Annotate (gnatcheck, Exempt_On, Rule_Name, "justification");
21118 -- source code section
21119 pragma Annotate (gnatcheck, Exempt_Off, Rule_Name);
21120 @end smallexample
21121
21122
21123 @node gnatcheck Annotations Rules
21124 @subsection @command{gnatcheck} Annotations Rules
21125 @cindex @command{gnatcheck} annotations rules
21126
21127 @itemize @bullet
21128
21129 @item
21130 An ``Exempt_Off'' annotation can only appear after a corresponding
21131 ``Exempt_On'' annotation.
21132
21133 @item
21134 Exempted source code sections are only based on the source location of the
21135 annotations. Any source construct between the two
21136 annotations is part of the exempted source code section.
21137
21138 @item
21139 Exempted source code sections for different rules are independent. They can
21140 be nested or intersect with one another without limitation.
21141 Creating nested or intersecting source code sections for the same rule is
21142 not allowed.
21143
21144 @item
21145 Malformed exempted source code sections are reported by a warning, and
21146 the corresponding rule exemptions are ignored.
21147
21148 @item
21149 When an exempted source code section does not contain at least one violation
21150 of the exempted rule, a warning is emitted on @file{stderr}.
21151
21152 @item
21153 If an ``Exempt_On'' annotation pragma does not have a matching
21154 ``Exempt_Off'' annotation pragma in the same compilation unit, then the
21155 exemption for the given rule is ignored and a warning is issued.
21156 @end itemize
21157
21158
21159 @node Predefined Rules
21160 @section Predefined Rules
21161 @cindex Predefined rules (for @command{gnatcheck})
21162
21163 @ignore
21164 @c (Jan 2007) Since the global rules are still under development and are not
21165 @c documented, there is no point in explaining the difference between
21166 @c global and local rules
21167 @noindent
21168 A rule in @command{gnatcheck} is either local or global.
21169 A @emph{local rule} is a rule that applies to a well-defined section
21170 of a program and that can be checked by analyzing only this section.
21171 A @emph{global rule} requires analysis of some global properties of the
21172 whole program (mostly related to the program call graph).
21173 As of @value{NOW}, the implementation of global rules should be
21174 considered to be at a preliminary stage. You can use the
21175 @option{+GLOBAL} option to enable all the global rules, and the
21176 @option{-GLOBAL} rule option to disable all the global rules.
21177
21178 All the global rules in the list below are
21179 so indicated by marking them ``GLOBAL''.
21180 This +GLOBAL and -GLOBAL options are not
21181 included in the list of gnatcheck options above, because at the moment they
21182 are considered as a temporary debug options.
21183
21184 @command{gnatcheck} performs rule checks for generic
21185 instances only for global rules. This limitation may be relaxed in a later
21186 release.
21187 @end ignore
21188
21189 @noindent
21190 The following subsections document the rules implemented in
21191 @command{gnatcheck}.
21192 The subsection title is the same as the rule identifier, which may be
21193 used as a parameter of the @option{+R} or @option{-R} options.
21194
21195
21196 @menu
21197 * Abstract_Type_Declarations::
21198 * Anonymous_Arrays::
21199 * Anonymous_Subtypes::
21200 * Blocks::
21201 * Boolean_Relational_Operators::
21202 @ignore
21203 * Ceiling_Violations::
21204 @end ignore
21205 * Complex_Inlined_Subprograms::
21206 * Controlled_Type_Declarations::
21207 * Declarations_In_Blocks::
21208 * Deep_Inheritance_Hierarchies::
21209 * Deeply_Nested_Generics::
21210 * Deeply_Nested_Inlining::
21211 @ignore
21212 * Deeply_Nested_Local_Inlining::
21213 @end ignore
21214 * Default_Parameters::
21215 * Direct_Calls_To_Primitives::
21216 * Discriminated_Records::
21217 * Enumeration_Ranges_In_CASE_Statements::
21218 * Exceptions_As_Control_Flow::
21219 * Exits_From_Conditional_Loops::
21220 * EXIT_Statements_With_No_Loop_Name::
21221 * Expanded_Loop_Exit_Names::
21222 * Explicit_Full_Discrete_Ranges::
21223 * Float_Equality_Checks::
21224 * Forbidden_Attributes::
21225 * Forbidden_Pragmas::
21226 * Function_Style_Procedures::
21227 * Generics_In_Subprograms::
21228 * GOTO_Statements::
21229 * Implicit_IN_Mode_Parameters::
21230 * Implicit_SMALL_For_Fixed_Point_Types::
21231 * Improperly_Located_Instantiations::
21232 * Improper_Returns::
21233 * Library_Level_Subprograms::
21234 * Local_Packages::
21235 @ignore
21236 * Improperly_Called_Protected_Entries::
21237 @end ignore
21238 * Metrics::
21239 * Misnamed_Controlling_Parameters::
21240 * Misnamed_Identifiers::
21241 * Multiple_Entries_In_Protected_Definitions::
21242 * Name_Clashes::
21243 * Non_Qualified_Aggregates::
21244 * Non_Short_Circuit_Operators::
21245 * Non_SPARK_Attributes::
21246 * Non_Tagged_Derived_Types::
21247 * Non_Visible_Exceptions::
21248 * Numeric_Literals::
21249 * OTHERS_In_Aggregates::
21250 * OTHERS_In_CASE_Statements::
21251 * OTHERS_In_Exception_Handlers::
21252 * Outer_Loop_Exits::
21253 * Overloaded_Operators::
21254 * Overly_Nested_Control_Structures::
21255 * Parameters_Out_Of_Order::
21256 * Positional_Actuals_For_Defaulted_Generic_Parameters::
21257 * Positional_Actuals_For_Defaulted_Parameters::
21258 * Positional_Components::
21259 * Positional_Generic_Parameters::
21260 * Positional_Parameters::
21261 * Predefined_Numeric_Types::
21262 * Raising_External_Exceptions::
21263 * Raising_Predefined_Exceptions::
21264 * Separate_Numeric_Error_Handlers::
21265 @ignore
21266 * Recursion::
21267 * Side_Effect_Functions::
21268 @end ignore
21269 * Slices::
21270 * Too_Many_Parents::
21271 * Unassigned_OUT_Parameters::
21272 * Uncommented_BEGIN_In_Package_Bodies::
21273 * Unconditional_Exits::
21274 * Unconstrained_Array_Returns::
21275 * Universal_Ranges::
21276 * Unnamed_Blocks_And_Loops::
21277 @ignore
21278 * Unused_Subprograms::
21279 @end ignore
21280 * USE_PACKAGE_Clauses::
21281 * Visible_Components::
21282 * Volatile_Objects_Without_Address_Clauses::
21283 @end menu
21284
21285
21286 @node Abstract_Type_Declarations
21287 @subsection @code{Abstract_Type_Declarations}
21288 @cindex @code{Abstract_Type_Declarations} rule (for @command{gnatcheck})
21289
21290 @noindent
21291 Flag all declarations of abstract types. For an abstract private
21292 type, both the private and full type declarations are flagged.
21293
21294 This rule has no parameters.
21295
21296
21297 @node Anonymous_Arrays
21298 @subsection @code{Anonymous_Arrays}
21299 @cindex @code{Anonymous_Arrays} rule (for @command{gnatcheck})
21300
21301 @noindent
21302 Flag all anonymous array type definitions (by Ada semantics these can only
21303 occur in object declarations).
21304
21305 This rule has no parameters.
21306
21307 @node Anonymous_Subtypes
21308 @subsection @code{Anonymous_Subtypes}
21309 @cindex @code{Anonymous_Subtypes} rule (for @command{gnatcheck})
21310
21311 @noindent
21312 Flag all uses of anonymous subtypes (except cases when subtype indication
21313 is a part of a record component definition, and this subtype indication
21314 depends on a discriminant). A use of an anonymous subtype is
21315 any instance of a subtype indication with a constraint, other than one
21316 that occurs immediately within a subtype declaration. Any use of a range
21317 other than as a constraint used immediately within a subtype declaration
21318 is considered as an anonymous subtype.
21319
21320 An effect of this rule is that @code{for} loops such as the following are
21321 flagged (since @code{1..N} is formally a ``range''):
21322
21323 @smallexample @c ada
21324 for I in 1 .. N loop
21325 @dots{}
21326 end loop;
21327 @end smallexample
21328
21329 @noindent
21330 Declaring an explicit subtype solves the problem:
21331
21332 @smallexample @c ada
21333 subtype S is Integer range 1..N;
21334 @dots{}
21335 for I in S loop
21336 @dots{}
21337 end loop;
21338 @end smallexample
21339
21340 @noindent
21341 This rule has no parameters.
21342
21343 @node Blocks
21344 @subsection @code{Blocks}
21345 @cindex @code{Blocks} rule (for @command{gnatcheck})
21346
21347 @noindent
21348 Flag each block statement.
21349
21350 This rule has no parameters.
21351
21352 @node Boolean_Relational_Operators
21353 @subsection @code{Boolean_Relational_Operators}
21354 @cindex @code{Boolean_Relational_Operators} rule (for @command{gnatcheck})
21355
21356 @noindent
21357 Flag each call to a predefined relational operator (``<'', ``>'', ``<='',
21358 ``>='', ``='' and ``/='') for the predefined Boolean type.
21359 (This rule is useful in enforcing the SPARK language restrictions.)
21360
21361 Calls to predefined relational operators of any type derived from
21362 @code{Standard.Boolean} are not detected. Calls to user-defined functions
21363 with these designators, and uses of operators that are renamings
21364 of the predefined relational operators for @code{Standard.Boolean},
21365 are likewise not detected.
21366
21367 This rule has no parameters.
21368
21369 @ignore
21370 @node Ceiling_Violations
21371 @subsection @code{Ceiling5_Violations} (under construction, GLOBAL)
21372 @cindex @code{Ceiling_Violations} rule (for @command{gnatcheck})
21373
21374 @noindent
21375 Flag invocations of a protected operation by a task whose priority exceeds
21376 the protected object's ceiling.
21377
21378 As of @value{NOW}, this rule has the following limitations:
21379
21380 @itemize @bullet
21381
21382 @item
21383 We consider only pragmas Priority and Interrupt_Priority as means to define
21384 a task/protected operation priority. We do not consider the effect of using
21385 Ada.Dynamic_Priorities.Set_Priority procedure;
21386
21387 @item
21388 We consider only base task priorities, and no priority inheritance. That is,
21389 we do not make a difference between calls issued during task activation and
21390 execution of the sequence of statements from task body;
21391
21392 @item
21393 Any situation when the priority of protected operation caller is set by a
21394 dynamic expression (that is, the corresponding Priority or
21395 Interrupt_Priority pragma has a non-static expression as an argument) we
21396 treat as a priority inconsistency (and, therefore, detect this situation).
21397 @end itemize
21398
21399 @noindent
21400 At the moment the notion of the main subprogram is not implemented in
21401 gnatcheck, so any pragma Priority in a library level subprogram body (in case
21402 if this subprogram can be a main subprogram of a partition) changes the
21403 priority of an environment task. So if we have more then one such pragma in
21404 the set of processed sources, the pragma that is processed last, defines the
21405 priority of an environment task.
21406
21407 This rule has no parameters.
21408 @end ignore
21409
21410 @node Controlled_Type_Declarations
21411 @subsection @code{Controlled_Type_Declarations}
21412 @cindex @code{Controlled_Type_Declarations} rule (for @command{gnatcheck})
21413
21414 @noindent
21415 Flag all declarations of controlled types. A declaration of a private type
21416 is flagged if its full declaration declares a controlled type. A declaration
21417 of a derived type is flagged if its ancestor type is controlled. Subtype
21418 declarations are not checked. A declaration of a type that itself is not a
21419 descendant of a type declared in @code{Ada.Finalization} but has a controlled
21420 component is not checked.
21421
21422 This rule has no parameters.
21423
21424
21425 @node Complex_Inlined_Subprograms
21426 @subsection @code{Complex_Inlined_Subprograms}
21427 @cindex @code{Complex_Inlined_Subprograms} rule (for @command{gnatcheck})
21428
21429 @noindent
21430 Flags a subprogram (or generic subprogram) if
21431 pragma Inline is applied to the subprogram and at least one of the following
21432 conditions is met:
21433
21434 @itemize @bullet
21435 @item
21436 it contains at least one complex declaration such as a subprogram body,
21437 package, task, protected declaration, or a generic instantiation
21438 (except instantiation of @code{Ada.Unchecked_Conversion});
21439
21440 @item
21441 it contains at least one complex statement such as a loop, a case
21442 or a if statement, or a short circuit control form;
21443
21444 @item
21445 the number of statements exceeds
21446 a value specified by the @option{N} rule parameter;
21447 @end itemize
21448
21449 @noindent
21450 This rule has the following (mandatory) parameter for the @option{+R} option:
21451
21452 @table @emph
21453 @item N
21454 Positive integer specifying the maximum allowed total number of statements
21455 in the subprogram body.
21456 @end table
21457
21458
21459 @node Declarations_In_Blocks
21460 @subsection @code{Declarations_In_Blocks}
21461 @cindex @code{Declarations_In_Blocks} rule (for @command{gnatcheck})
21462
21463 @noindent
21464 Flag all block statements containing local declarations. A @code{declare}
21465 block with an empty @i{declarative_part} or with a @i{declarative part}
21466 containing only pragmas and/or @code{use} clauses is not flagged.
21467
21468 This rule has no parameters.
21469
21470
21471 @node Deep_Inheritance_Hierarchies
21472 @subsection @code{Deep_Inheritance_Hierarchies}
21473 @cindex @code{Deep_Inheritance_Hierarchies} rule (for @command{gnatcheck})
21474
21475 @noindent
21476 Flags a tagged derived type declaration or an interface type declaration if
21477 its depth (in its inheritance
21478 hierarchy) exceeds the value specified by the @option{N} rule parameter.
21479
21480 The inheritance depth of a tagged type or interface type is defined as 0 for
21481 a type with no parent and no progenitor, and otherwise as 1 + max of the
21482 depths of the immediate parent and immediate progenitors.
21483
21484 This rule does not flag private extension
21485 declarations. In the case of a private extension, the corresponding full
21486 declaration is checked.
21487
21488 This rule has the following (mandatory) parameter for the @option{+R} option:
21489
21490 @table @emph
21491 @item N
21492 Integer not less than -1 specifying the maximal allowed depth of any inheritance
21493 hierarchy. If the rule parameter is set to -1, the rule flags all the declarations
21494 of tagged and interface types.
21495 @end table
21496
21497
21498 @node Deeply_Nested_Generics
21499 @subsection @code{Deeply_Nested_Generics}
21500 @cindex @code{Deeply_Nested_Generics} rule (for @command{gnatcheck})
21501
21502 @noindent
21503 Flags a generic declaration nested in another generic declaration if
21504 the nesting level of the inner generic exceeds
21505 a value specified by the @option{N} rule parameter.
21506 The nesting level is the number of generic declaratons that enclose the given
21507 (generic) declaration. Formal packages are not flagged by this rule.
21508
21509 This rule has the following (mandatory) parameters for the @option{+R} option:
21510
21511 @table @emph
21512 @item N
21513 Positive integer specifying the maximal allowed nesting level
21514 for a generic declaration.
21515 @end table
21516
21517 @node Deeply_Nested_Inlining
21518 @subsection @code{Deeply_Nested_Inlining}
21519 @cindex @code{Deeply_Nested_Inlining} rule (for @command{gnatcheck})
21520
21521 @noindent
21522 Flags a subprogram (or generic subprogram) if
21523 pragma Inline has been applied to the subprogram but the subprogram
21524 calls to another inlined subprogram that results in nested inlining
21525 with nesting depth exceeding the value specified by the
21526 @option{N} rule parameter.
21527
21528 This rule requires the global analysis of all the compilation units that
21529 are @command{gnatcheck} arguments; such analysis may affect the tool's
21530 performance.
21531
21532 This rule has the following (mandatory) parameter for the @option{+R} option:
21533
21534 @table @emph
21535 @item N
21536 Positive integer specifying the maximal allowed level of nested inlining.
21537 @end table
21538
21539
21540 @ignore
21541 @node Deeply_Nested_Local_Inlining
21542 @subsection @code{Deeply_Nested_Local_Inlining}
21543 @cindex @code{Deeply_Nested_Local_Inlining} rule (for @command{gnatcheck})
21544
21545 @noindent
21546 Flags a subprogram body if a pragma @code{Inline} is applied to the
21547 corresponding subprogram (or generic subprogram) and the body contains a call
21548 to another inlined subprogram that results in nested inlining with nesting
21549 depth more then a value specified by the @option{N} rule parameter.
21550 This rule is similar to @code{Deeply_Nested_Inlining} rule, but it
21551 assumes that calls to subprograms in
21552 with'ed units are not inlided, so all the analysis of the depth of inlining is
21553 limited by the compilation unit where the subprogram body is located and the
21554 units it depends semantically upon. Such analysis may be usefull for the case
21555 when neiter @option{-gnatn} nor @option{-gnatN} option is used when building
21556 the executable.
21557
21558 This rule has the following (mandatory) parameters for the @option{+R} option:
21559
21560 @table @emph
21561 @item N
21562 Positive integer specifying the maximal allowed level of nested inlining.
21563 @end table
21564
21565 @end ignore
21566
21567 @node Default_Parameters
21568 @subsection @code{Default_Parameters}
21569 @cindex @code{Default_Parameters} rule (for @command{gnatcheck})
21570
21571 @noindent
21572 Flag all default expressions for subprogram parameters. Parameter
21573 declarations of formal and generic subprograms are also checked.
21574
21575 This rule has no parameters.
21576
21577
21578 @node Direct_Calls_To_Primitives
21579 @subsection @code{Direct_Calls_To_Primitives}
21580 @cindex @code{Direct_Calls_To_Primitives} rule (for @command{gnatcheck})
21581
21582 @noindent
21583 Flags any non-dispatching call to a dispatching primitive operation, except
21584 for the common idiom where a primitive subprogram for a tagged type
21585 directly calls the same primitive subprogram of the type's immediate ancestor.
21586
21587 This rule has no parameters.
21588
21589
21590 @node Discriminated_Records
21591 @subsection @code{Discriminated_Records}
21592 @cindex @code{Discriminated_Records} rule (for @command{gnatcheck})
21593
21594 @noindent
21595 Flag all declarations of record types with discriminants. Only the
21596 declarations of record and record extension types are checked. Incomplete,
21597 formal, private, derived and private extension type declarations are not
21598 checked. Task and protected type declarations also are not checked.
21599
21600 This rule has no parameters.
21601
21602
21603 @node Enumeration_Ranges_In_CASE_Statements
21604 @subsection @code{Enumeration_Ranges_In_CASE_Statements}
21605 @cindex @code{Enumeration_Ranges_In_CASE_Statements} (for @command{gnatcheck})
21606
21607 @noindent
21608 Flag each use of a range of enumeration literals as a choice in a
21609 @code{case} statement.
21610 All forms for specifying a range (explicit ranges
21611 such as @code{A .. B}, subtype marks and @code{'Range} attributes) are flagged.
21612 An enumeration range is
21613 flagged even if contains exactly one enumeration value or no values at all. A
21614 type derived from an enumeration type is considered as an enumeration type.
21615
21616 This rule helps prevent maintenance problems arising from adding an
21617 enumeration value to a type and having it implicitly handled by an existing
21618 @code{case} statement with an enumeration range that includes the new literal.
21619
21620 This rule has no parameters.
21621
21622
21623 @node Exceptions_As_Control_Flow
21624 @subsection @code{Exceptions_As_Control_Flow}
21625 @cindex @code{Exceptions_As_Control_Flow} (for @command{gnatcheck})
21626
21627 @noindent
21628 Flag each place where an exception is explicitly raised and handled in the
21629 same subprogram body. A @code{raise} statement in an exception handler,
21630 package body, task body or entry body is not flagged.
21631
21632 The rule has no parameters.
21633
21634 @node Exits_From_Conditional_Loops
21635 @subsection @code{Exits_From_Conditional_Loops}
21636 @cindex @code{Exits_From_Conditional_Loops} (for @command{gnatcheck})
21637
21638 @noindent
21639 Flag any exit statement if it transfers the control out of a @code{for} loop
21640 or a @code{while} loop. This includes cases when the @code{exit} statement
21641 applies to a @code{FOR} or @code{while} loop, and cases when it is enclosed
21642 in some @code{for} or @code{while} loop, but transfers the control from some
21643 outer (inconditional) @code{loop} statement.
21644
21645 The rule has no parameters.
21646
21647
21648 @node EXIT_Statements_With_No_Loop_Name
21649 @subsection @code{EXIT_Statements_With_No_Loop_Name}
21650 @cindex @code{EXIT_Statements_With_No_Loop_Name} (for @command{gnatcheck})
21651
21652 @noindent
21653 Flag each @code{exit} statement that does not specify the name of the loop
21654 being exited.
21655
21656 The rule has no parameters.
21657
21658
21659 @node Expanded_Loop_Exit_Names
21660 @subsection @code{Expanded_Loop_Exit_Names}
21661 @cindex @code{Expanded_Loop_Exit_Names} rule (for @command{gnatcheck})
21662
21663 @noindent
21664 Flag all expanded loop names in @code{exit} statements.
21665
21666 This rule has no parameters.
21667
21668 @node Explicit_Full_Discrete_Ranges
21669 @subsection @code{Explicit_Full_Discrete_Ranges}
21670 @cindex @code{Explicit_Full_Discrete_Ranges} rule (for @command{gnatcheck})
21671
21672 @noindent
21673 Flag each discrete range that has the form @code{A'First .. A'Last}.
21674
21675 This rule has no parameters.
21676
21677 @node Float_Equality_Checks
21678 @subsection @code{Float_Equality_Checks}
21679 @cindex @code{Float_Equality_Checks} rule (for @command{gnatcheck})
21680
21681 @noindent
21682 Flag all calls to the predefined equality operations for floating-point types.
21683 Both ``@code{=}'' and ``@code{/=}'' operations are checked.
21684 User-defined equality operations are not flagged, nor are ``@code{=}''
21685 and ``@code{/=}'' operations for fixed-point types.
21686
21687 This rule has no parameters.
21688
21689
21690 @node Forbidden_Attributes
21691 @subsection @code{Forbidden_Attributes}
21692 @cindex @code{Forbidden_Attributes} rule (for @command{gnatcheck})
21693
21694 @noindent
21695 Flag each use of the specified attributes. The attributes to be detected are
21696 named in the rule's parameters.
21697
21698 This rule has the following parameters:
21699
21700 @itemize @bullet
21701 @item For the @option{+R} option
21702
21703 @table @asis
21704 @item @emph{Attribute_Designator}
21705 Adds the specified attribute to the set of attributes to be detected and sets
21706 the detection checks for all the specified attributes ON.
21707 If @emph{Attribute_Designator}
21708 does not denote any attribute defined in the Ada standard
21709 or in
21710 @ref{Implementation Defined Attributes,,, gnat_rm, GNAT Reference
21711 Manual}, it is treated as the name of unknown attribute.
21712
21713 @item @code{GNAT}
21714 All the GNAT-specific attributes are detected; this sets
21715 the detection checks for all the specified attributes ON.
21716
21717 @item @code{ALL}
21718 All attributes are detected; this sets the rule ON.
21719 @end table
21720
21721 @item For the @option{-R} option
21722 @table @asis
21723 @item @emph{Attribute_Designator}
21724 Removes the specified attribute from the set of attributes to be
21725 detected without affecting detection checks for
21726 other attributes. If @emph{Attribute_Designator} does not correspond to any
21727 attribute defined in the Ada standard or in
21728 @ref{Implementation Defined Attributes,,, gnat_rm, GNAT Reference Manual},
21729 this option is treated as turning OFF detection of all unknown attributes.
21730
21731 @item GNAT
21732 Turn OFF detection of all GNAT-specific attributes
21733
21734 @item ALL
21735 Clear the list of the attributes to be detected and
21736 turn the rule OFF.
21737 @end table
21738 @end itemize
21739
21740 @noindent
21741 Parameters are not case sensitive. If @emph{Attribute_Designator} does not
21742 have the syntax of an Ada identifier and therefore can not be considered as a
21743 (part of an) attribute designator, a diagnostic message is generated and the
21744 corresponding parameter is ignored. (If an attribute allows a static
21745 expression to be a part of the attribute designator, this expression is
21746 ignored by this rule.)
21747
21748 When more then one parameter is given in the same rule option, the parameters
21749 must be separated by commas.
21750
21751 If more then one option for this rule is specified for the gnatcheck call, a
21752 new option overrides the previous one(s).
21753
21754 The @option{+R} option with no parameters turns the rule ON, with the set of
21755 attributes to be detected defined by the previous rule options.
21756 (By default this set is empty, so if the only option specified for the rule is
21757 @option{+RForbidden_Attributes} (with
21758 no parameter), then the rule is enabled, but it does not detect anything).
21759 The @option{-R} option with no parameter turns the rule OFF, but it does not
21760 affect the set of attributes to be detected.
21761
21762
21763 @node Forbidden_Pragmas
21764 @subsection @code{Forbidden_Pragmas}
21765 @cindex @code{Forbidden_Pragmas} rule (for @command{gnatcheck})
21766
21767 @noindent
21768 Flag each use of the specified pragmas. The pragmas to be detected
21769 are named in the rule's parameters.
21770
21771 This rule has the following parameters:
21772
21773 @itemize @bullet
21774 @item For the @option{+R} option
21775
21776 @table @asis
21777 @item @emph{Pragma_Name}
21778 Adds the specified pragma to the set of pragmas to be
21779 checked and sets the checks for all the specified pragmas
21780 ON. @emph{Pragma_Name} is treated as a name of a pragma. If it
21781 does not correspond to any pragma name defined in the Ada
21782 standard or to the name of a GNAT-specific pragma defined
21783 in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
21784 Manual}, it is treated as the name of unknown pragma.
21785
21786 @item @code{GNAT}
21787 All the GNAT-specific pragmas are detected; this sets
21788 the checks for all the specified pragmas ON.
21789
21790 @item @code{ALL}
21791 All pragmas are detected; this sets the rule ON.
21792 @end table
21793
21794 @item For the @option{-R} option
21795 @table @asis
21796 @item @emph{Pragma_Name}
21797 Removes the specified pragma from the set of pragmas to be
21798 checked without affecting checks for
21799 other pragmas. @emph{Pragma_Name} is treated as a name
21800 of a pragma. If it does not correspond to any pragma
21801 defined in the Ada standard or to any name defined in
21802 @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
21803 this option is treated as turning OFF detection of all unknown pragmas.
21804
21805 @item GNAT
21806 Turn OFF detection of all GNAT-specific pragmas
21807
21808 @item ALL
21809 Clear the list of the pragmas to be detected and
21810 turn the rule OFF.
21811 @end table
21812 @end itemize
21813
21814 @noindent
21815 Parameters are not case sensitive. If @emph{Pragma_Name} does not have
21816 the syntax of an Ada identifier and therefore can not be considered
21817 as a pragma name, a diagnostic message is generated and the corresponding
21818 parameter is ignored.
21819
21820 When more then one parameter is given in the same rule option, the parameters
21821 must be separated by a comma.
21822
21823 If more then one option for this rule is specified for the @command{gnatcheck}
21824 call, a new option overrides the previous one(s).
21825
21826 The @option{+R} option with no parameters turns the rule ON with the set of
21827 pragmas to be detected defined by the previous rule options.
21828 (By default this set is empty, so if the only option specified for the rule is
21829 @option{+RForbidden_Pragmas} (with
21830 no parameter), then the rule is enabled, but it does not detect anything).
21831 The @option{-R} option with no parameter turns the rule OFF, but it does not
21832 affect the set of pragmas to be detected.
21833
21834
21835
21836
21837 @node Function_Style_Procedures
21838 @subsection @code{Function_Style_Procedures}
21839 @cindex @code{Function_Style_Procedures} rule (for @command{gnatcheck})
21840
21841 @noindent
21842 Flag each procedure that can be rewritten as a function. A procedure can be
21843 converted into a function if it has exactly one parameter of mode @code{out}
21844 and no parameters of mode @code{in out}. Procedure declarations,
21845 formal procedure declarations, and generic procedure declarations are always
21846 checked. Procedure
21847 bodies and body stubs are flagged only if they do not have corresponding
21848 separate declarations. Procedure renamings and procedure instantiations are
21849 not flagged.
21850
21851 If a procedure can be rewritten as a function, but its @code{out} parameter is
21852 of a limited type, it is not flagged.
21853
21854 Protected procedures are not flagged. Null procedures also are not flagged.
21855
21856 This rule has no parameters.
21857
21858
21859 @node Generics_In_Subprograms
21860 @subsection @code{Generics_In_Subprograms}
21861 @cindex @code{Generics_In_Subprograms} rule (for @command{gnatcheck})
21862
21863 @noindent
21864 Flag each declaration of a generic unit in a subprogram. Generic
21865 declarations in the bodies of generic subprograms are also flagged.
21866 A generic unit nested in another generic unit is not flagged.
21867 If a generic unit is
21868 declared in a local package that is declared in a subprogram body, the
21869 generic unit is flagged.
21870
21871 This rule has no parameters.
21872
21873
21874 @node GOTO_Statements
21875 @subsection @code{GOTO_Statements}
21876 @cindex @code{GOTO_Statements} rule (for @command{gnatcheck})
21877
21878 @noindent
21879 Flag each occurrence of a @code{goto} statement.
21880
21881 This rule has no parameters.
21882
21883
21884 @node Implicit_IN_Mode_Parameters
21885 @subsection @code{Implicit_IN_Mode_Parameters}
21886 @cindex @code{Implicit_IN_Mode_Parameters} rule (for @command{gnatcheck})
21887
21888 @noindent
21889 Flag each occurrence of a formal parameter with an implicit @code{in} mode.
21890 Note that @code{access} parameters, although they technically behave
21891 like @code{in} parameters, are not flagged.
21892
21893 This rule has no parameters.
21894
21895
21896 @node Implicit_SMALL_For_Fixed_Point_Types
21897 @subsection @code{Implicit_SMALL_For_Fixed_Point_Types}
21898 @cindex @code{Implicit_SMALL_For_Fixed_Point_Types} rule (for @command{gnatcheck})
21899
21900 @noindent
21901 Flag each fixed point type declaration that lacks an explicit
21902 representation clause to define its @code{'Small} value.
21903 Since @code{'Small} can be defined only for ordinary fixed point types,
21904 decimal fixed point type declarations are not checked.
21905
21906 This rule has no parameters.
21907
21908
21909 @node Improperly_Located_Instantiations
21910 @subsection @code{Improperly_Located_Instantiations}
21911 @cindex @code{Improperly_Located_Instantiations} rule (for @command{gnatcheck})
21912
21913 @noindent
21914 Flag all generic instantiations in library-level package specs
21915 (including library generic packages) and in all subprogram bodies.
21916
21917 Instantiations in task and entry bodies are not flagged. Instantiations in the
21918 bodies of protected subprograms are flagged.
21919
21920 This rule has no parameters.
21921
21922
21923
21924 @node Improper_Returns
21925 @subsection @code{Improper_Returns}
21926 @cindex @code{Improper_Returns} rule (for @command{gnatcheck})
21927
21928 @noindent
21929 Flag each explicit @code{return} statement in procedures, and
21930 multiple @code{return} statements in functions.
21931 Diagnostic messages are generated for all @code{return} statements
21932 in a procedure (thus each procedure must be written so that it
21933 returns implicitly at the end of its statement part),
21934 and for all @code{return} statements in a function after the first one.
21935 This rule supports the stylistic convention that each subprogram
21936 should have no more than one point of normal return.
21937
21938 This rule has no parameters.
21939
21940
21941 @node Library_Level_Subprograms
21942 @subsection @code{Library_Level_Subprograms}
21943 @cindex @code{Library_Level_Subprograms} rule (for @command{gnatcheck})
21944
21945 @noindent
21946 Flag all library-level subprograms (including generic subprogram instantiations).
21947
21948 This rule has no parameters.
21949
21950
21951 @node Local_Packages
21952 @subsection @code{Local_Packages}
21953 @cindex @code{Local_Packages} rule (for @command{gnatcheck})
21954
21955 @noindent
21956 Flag all local packages declared in package and generic package
21957 specs.
21958 Local packages in bodies are not flagged.
21959
21960 This rule has no parameters.
21961
21962 @ignore
21963 @node Improperly_Called_Protected_Entries
21964 @subsection @code{Improperly_Called_Protected_Entries} (under construction, GLOBAL)
21965 @cindex @code{Improperly_Called_Protected_Entries} rule (for @command{gnatcheck})
21966
21967 @noindent
21968 Flag each protected entry that can be called from more than one task.
21969
21970 This rule has no parameters.
21971 @end ignore
21972
21973 @node Metrics
21974 @subsection @code{Metrics}
21975 @cindex @code{Metrics} rule (for @command{gnatcheck})
21976
21977 @noindent
21978 There is a set of checks based on computing a metric value and comparing the
21979 result with the specified upper (or lower, depending on a specific metric)
21980 value specified for a given metric. A construct is flagged if a given metric
21981 is applicable (can be computed) for it and the computed value is greater
21982 then (lover then) the specified upper (lower) bound.
21983
21984 The name of any metric-based rule consists of the prefix @code{Metrics_}
21985 followed by the name of the corresponding metric (see the table below).
21986 For @option{+R} option, each metric-based rule has a numeric parameter
21987 specifying the bound (integer or real, depending on a metric), @option{-R}
21988 option for metric rules does not have a parameter.
21989
21990 The following table shows the metric names for that the corresponding
21991 metrics-based checks are supported by gnatcheck, including the
21992 constraint that must be satisfied by the bound that is specified for the check
21993 and what bound - upper (U) or lower (L) - should be specified.
21994
21995 @multitable {@code{Cyclomatic_Complexity}}{Cyclomatic complexity}{Positive integer}
21996 @ifnothtml
21997 @headitem Check Name @tab Description @tab Bounds Value
21998 @end ifnothtml
21999 @ifhtml
22000 @item @b{Check Name} @tab @b{Description} @tab @b{Bounds Value}
22001 @end ifhtml
22002 @c Above conditional code is workaround to bug in texi2html (Feb 2008)
22003 @item @code{Essential_Complexity} @tab Essential complexity @tab Positive integer (U)
22004 @item @code{Cyclomatic_Complexity} @tab Cyclomatic complexity @tab Positive integer (U)
22005 @item @code{LSLOC} @tab Logical Source Lines of Code @tab Positive integer (U)
22006 @end multitable
22007
22008 @noindent
22009 The meaning and the computed values for all these metrics are exactly
22010 the same as for the corresponding metrics in @command{gnatmetric}.
22011
22012 @emph{Example:} the rule
22013 @smallexample
22014 +RMetrics_Cyclomatic_Complexity : 7
22015 @end smallexample
22016 @noindent
22017 means that all bodies with cyclomatic complexity exceeding 7 will be flagged.
22018
22019 To turn OFF the check for cyclomatic complexity metric, use the following option:
22020 @smallexample
22021 -RMetrics_Cyclomatic_Complexity
22022 @end smallexample
22023
22024
22025 @node Misnamed_Controlling_Parameters
22026 @subsection @code{Misnamed_Controlling_Parameters}
22027 @cindex @code{Misnamed_Controlling_Parameters} rule (for @command{gnatcheck})
22028
22029 @noindent
22030 Flags a declaration of a dispatching operation, if the first parameter is
22031 not a controlling one and its name is not @code{This} (the check for
22032 parameter name is not case-sensitive). Declarations of dispatching functions
22033 with controlling result and no controlling parameter are never flagged.
22034
22035 A subprogram body declaration, subprogram renaming declaration or subprogram
22036 body stub is flagged only if it is not a completion of a prior subprogram
22037 declaration.
22038
22039 This rule has no parameters.
22040
22041
22042
22043 @node Misnamed_Identifiers
22044 @subsection @code{Misnamed_Identifiers}
22045 @cindex @code{Misnamed_Identifiers} rule (for @command{gnatcheck})
22046
22047 @noindent
22048 Flag the declaration of each identifier that does not have a suffix
22049 corresponding to the kind of entity being declared.
22050 The following declarations are checked:
22051
22052 @itemize @bullet
22053 @item
22054 type declarations
22055
22056 @item
22057 subtype declarations
22058
22059 @item
22060 constant declarations (but not number declarations)
22061
22062 @item
22063 package renaming declarations (but not generic package renaming
22064 declarations)
22065 @end itemize
22066
22067 @noindent
22068 This rule may have parameters. When used without parameters, the rule enforces
22069 the following checks:
22070
22071 @itemize @bullet
22072 @item
22073 type-defining names end with @code{_T}, unless the type is an access type,
22074 in which case the suffix must be @code{_A}
22075 @item
22076 constant names end with @code{_C}
22077 @item
22078 names defining package renamings end with @code{_R}
22079 @end itemize
22080
22081 @noindent
22082 Defining identifiers from incomplete type declarations are never flagged.
22083
22084 For a private type declaration (including private extensions), the defining
22085 identifier from the private type declaration is checked against the type
22086 suffix (even if the corresponding full declaration is an access type
22087 declaration), and the defining identifier from the corresponding full type
22088 declaration is not checked.
22089
22090 @noindent
22091 For a deferred constant, the defining name in the corresponding full constant
22092 declaration is not checked.
22093
22094 Defining names of formal types are not checked.
22095
22096 The rule may have the following parameters:
22097
22098 @itemize @bullet
22099 @item
22100 For the @option{+R} option:
22101 @table @code
22102 @item Default
22103 Sets the default listed above for all the names to be checked.
22104
22105 @item Type_Suffix=@emph{string}
22106 Specifies the suffix for a type name.
22107
22108 @item Access_Suffix=@emph{string}
22109 Specifies the suffix for an access type name. If
22110 this parameter is set, it overrides for access
22111 types the suffix set by the @code{Type_Suffix} parameter.
22112 For access types, @emph{string} may have the following format:
22113 @emph{suffix1(suffix2)}. That means that an access type name
22114 should have the @emph{suffix1} suffix except for the case when
22115 the designated type is also an access type, in this case the
22116 type name should have the @emph{suffix1 & suffix2} suffix.
22117
22118 @item Class_Access_Suffix=@emph{string}
22119 Specifies the suffix for the name of an access type that points to some class-wide
22120 type. If this parameter is set, it overrides for such access
22121 types the suffix set by the @code{Type_Suffix} or @code{Access_Suffix}
22122 parameter.
22123
22124 @item Class_Subtype_Suffix=@emph{string}
22125 Specifies the suffix for the name of a subtype that denotes a class-wide type.
22126
22127 @item Constant_Suffix=@emph{string}
22128 Specifies the suffix for a constant name.
22129
22130 @item Renaming_Suffix=@emph{string}
22131 Specifies the suffix for a package renaming name.
22132 @end table
22133
22134 @item
22135 For the @option{-R} option:
22136 @table @code
22137 @item All_Suffixes
22138 Remove all the suffixes specified for the
22139 identifier suffix checks, whether by default or
22140 as specified by other rule parameters. All the
22141 checks for this rule are disabled as a result.
22142
22143 @item Type_Suffix
22144 Removes the suffix specified for types. This
22145 disables checks for types but does not disable
22146 any other checks for this rule (including the
22147 check for access type names if @code{Access_Suffix} is
22148 set).
22149
22150 @item Access_Suffix
22151 Removes the suffix specified for access types.
22152 This disables checks for access type names but
22153 does not disable any other checks for this rule.
22154 If @code{Type_Suffix} is set, access type names are
22155 checked as ordinary type names.
22156
22157 @item Class_Access_Suffix
22158 Removes the suffix specified for access types pointing to class-wide
22159 type. This disables specific checks for names of access types pointing to
22160 class-wide types but does not disable any other checks for this rule.
22161 If @code{Type_Suffix} is set, access type names are
22162 checked as ordinary type names. If @code{Access_Suffix} is set, these
22163 access types are checked as any other access type name.
22164
22165 @item Class_Subtype_Suffix=@emph{string}
22166 Removes the suffix specified for subtype names.
22167 This disables checks for subtype names but
22168 does not disable any other checks for this rule.
22169
22170 @item Constant_Suffix
22171 Removes the suffix specified for constants. This
22172 disables checks for constant names but does not
22173 disable any other checks for this rule.
22174
22175 @item Renaming_Suffix
22176 Removes the suffix specified for package
22177 renamings. This disables checks for package
22178 renamings but does not disable any other checks
22179 for this rule.
22180 @end table
22181 @end itemize
22182
22183 @noindent
22184 If more than one parameter is used, parameters must be separated by commas.
22185
22186 If more than one option is specified for the @command{gnatcheck} invocation,
22187 a new option overrides the previous one(s).
22188
22189 The @option{+RMisnamed_Identifiers} option (with no parameter) enables
22190 checks for all the
22191 name suffixes specified by previous options used for this rule.
22192
22193 The @option{-RMisnamed_Identifiers} option (with no parameter) disables
22194 all the checks but keeps
22195 all the suffixes specified by previous options used for this rule.
22196
22197 The @emph{string} value must be a valid suffix for an Ada identifier (after
22198 trimming all the leading and trailing space characters, if any).
22199 Parameters are not case sensitive, except the @emph{string} part.
22200
22201 If any error is detected in a rule parameter, the parameter is ignored.
22202 In such a case the options that are set for the rule are not
22203 specified.
22204
22205
22206
22207 @node Multiple_Entries_In_Protected_Definitions
22208 @subsection @code{Multiple_Entries_In_Protected_Definitions}
22209 @cindex @code{Multiple_Entries_In_Protected_Definitions} rule (for @command{gnatcheck})
22210
22211 @noindent
22212 Flag each protected definition (i.e., each protected object/type declaration)
22213 that defines more than one entry.
22214 Diagnostic messages are generated for all the entry declarations
22215 except the first one. An entry family is counted as one entry. Entries from
22216 the private part of the protected definition are also checked.
22217
22218 This rule has no parameters.
22219
22220 @node Name_Clashes
22221 @subsection @code{Name_Clashes}
22222 @cindex @code{Name_Clashes} rule (for @command{gnatcheck})
22223
22224 @noindent
22225 Check that certain names are not used as defining identifiers. To activate
22226 this rule, you need to supply a reference to the dictionary file(s) as a rule
22227 parameter(s) (more then one dictionary file can be specified). If no
22228 dictionary file is set, this rule will not cause anything to be flagged.
22229 Only defining occurrences, not references, are checked.
22230 The check is not case-sensitive.
22231
22232 This rule is enabled by default, but without setting any corresponding
22233 dictionary file(s); thus the default effect is to do no checks.
22234
22235 A dictionary file is a plain text file. The maximum line length for this file
22236 is 1024 characters. If the line is longer then this limit, extra characters
22237 are ignored.
22238
22239 Each line can be either an empty line, a comment line, or a line containing
22240 a list of identifiers separated by space or HT characters.
22241 A comment is an Ada-style comment (from @code{--} to end-of-line).
22242 Identifiers must follow the Ada syntax for identifiers.
22243 A line containing one or more identifiers may end with a comment.
22244
22245 @node Non_Qualified_Aggregates
22246 @subsection @code{Non_Qualified_Aggregates}
22247 @cindex @code{Non_Qualified_Aggregates} rule (for @command{gnatcheck})
22248
22249 @noindent
22250 Flag each non-qualified aggregate.
22251 A non-qualified aggregate is an
22252 aggregate that is not the expression of a qualified expression. A
22253 string literal is not considered an aggregate, but an array
22254 aggregate of a string type is considered as a normal aggregate.
22255 Aggregates of anonymous array types are not flagged.
22256
22257 This rule has no parameters.
22258
22259
22260 @node Non_Short_Circuit_Operators
22261 @subsection @code{Non_Short_Circuit_Operators}
22262 @cindex @code{Non_Short_Circuit_Operators} rule (for @command{gnatcheck})
22263
22264 @noindent
22265 Flag all calls to predefined @code{and} and @code{or} operators for
22266 any boolean type. Calls to
22267 user-defined @code{and} and @code{or} and to operators defined by renaming
22268 declarations are not flagged. Calls to predefined @code{and} and @code{or}
22269 operators for modular types or boolean array types are not flagged.
22270
22271 This rule has no parameters.
22272
22273
22274
22275 @node Non_SPARK_Attributes
22276 @subsection @code{Non_SPARK_Attributes}
22277 @cindex @code{Non_SPARK_Attributes} rule (for @command{gnatcheck})
22278
22279 @noindent
22280 The SPARK language defines the following subset of Ada 95 attribute
22281 designators as those that can be used in SPARK programs. The use of
22282 any other attribute is flagged.
22283
22284 @itemize @bullet
22285 @item @code{'Adjacent}
22286 @item @code{'Aft}
22287 @item @code{'Base}
22288 @item @code{'Ceiling}
22289 @item @code{'Component_Size}
22290 @item @code{'Compose}
22291 @item @code{'Copy_Sign}
22292 @item @code{'Delta}
22293 @item @code{'Denorm}
22294 @item @code{'Digits}
22295 @item @code{'Exponent}
22296 @item @code{'First}
22297 @item @code{'Floor}
22298 @item @code{'Fore}
22299 @item @code{'Fraction}
22300 @item @code{'Last}
22301 @item @code{'Leading_Part}
22302 @item @code{'Length}
22303 @item @code{'Machine}
22304 @item @code{'Machine_Emax}
22305 @item @code{'Machine_Emin}
22306 @item @code{'Machine_Mantissa}
22307 @item @code{'Machine_Overflows}
22308 @item @code{'Machine_Radix}
22309 @item @code{'Machine_Rounds}
22310 @item @code{'Max}
22311 @item @code{'Min}
22312 @item @code{'Model}
22313 @item @code{'Model_Emin}
22314 @item @code{'Model_Epsilon}
22315 @item @code{'Model_Mantissa}
22316 @item @code{'Model_Small}
22317 @item @code{'Modulus}
22318 @item @code{'Pos}
22319 @item @code{'Pred}
22320 @item @code{'Range}
22321 @item @code{'Remainder}
22322 @item @code{'Rounding}
22323 @item @code{'Safe_First}
22324 @item @code{'Safe_Last}
22325 @item @code{'Scaling}
22326 @item @code{'Signed_Zeros}
22327 @item @code{'Size}
22328 @item @code{'Small}
22329 @item @code{'Succ}
22330 @item @code{'Truncation}
22331 @item @code{'Unbiased_Rounding}
22332 @item @code{'Val}
22333 @item @code{'Valid}
22334 @end itemize
22335
22336 @noindent
22337 This rule has no parameters.
22338
22339
22340 @node Non_Tagged_Derived_Types
22341 @subsection @code{Non_Tagged_Derived_Types}
22342 @cindex @code{Non_Tagged_Derived_Types} rule (for @command{gnatcheck})
22343
22344 @noindent
22345 Flag all derived type declarations that do not have a record extension part.
22346
22347 This rule has no parameters.
22348
22349
22350
22351 @node Non_Visible_Exceptions
22352 @subsection @code{Non_Visible_Exceptions}
22353 @cindex @code{Non_Visible_Exceptions} rule (for @command{gnatcheck})
22354
22355 @noindent
22356 Flag constructs leading to the possibility of propagating an exception
22357 out of the scope in which the exception is declared.
22358 Two cases are detected:
22359
22360 @itemize @bullet
22361 @item
22362 An exception declaration in a subprogram body, task body or block
22363 statement is flagged if the body or statement does not contain a handler for
22364 that exception or a handler with an @code{others} choice.
22365
22366 @item
22367 A @code{raise} statement in an exception handler of a subprogram body,
22368 task body or block statement is flagged if it (re)raises a locally
22369 declared exception. This may occur under the following circumstances:
22370 @itemize @minus
22371 @item
22372 it explicitly raises a locally declared exception, or
22373 @item
22374 it does not specify an exception name (i.e., it is simply @code{raise;})
22375 and the enclosing handler contains a locally declared exception in its
22376 exception choices.
22377 @end itemize
22378 @end itemize
22379
22380 @noindent
22381 Renamings of local exceptions are not flagged.
22382
22383 This rule has no parameters.
22384
22385
22386 @node Numeric_Literals
22387 @subsection @code{Numeric_Literals}
22388 @cindex @code{Numeric_Literals} rule (for @command{gnatcheck})
22389
22390 @noindent
22391 Flag each use of a numeric literal in an index expression, and in any
22392 circumstance except for the following:
22393
22394 @itemize @bullet
22395 @item
22396 a literal occurring in the initialization expression for a constant
22397 declaration or a named number declaration, or
22398
22399 @item
22400 an integer literal that is less than or equal to a value
22401 specified by the @option{N} rule parameter.
22402 @end itemize
22403
22404 @noindent
22405 This rule may have the following parameters for the @option{+R} option:
22406
22407 @table @asis
22408 @item @emph{N}
22409 @emph{N} is an integer literal used as the maximal value that is not flagged
22410 (i.e., integer literals not exceeding this value are allowed)
22411
22412 @item @code{ALL}
22413 All integer literals are flagged
22414 @end table
22415
22416 @noindent
22417 If no parameters are set, the maximum unflagged value is 1.
22418
22419 The last specified check limit (or the fact that there is no limit at
22420 all) is used when multiple @option{+R} options appear.
22421
22422 The @option{-R} option for this rule has no parameters.
22423 It disables the rule but retains the last specified maximum unflagged value.
22424 If the @option{+R} option subsequently appears, this value is used as the
22425 threshold for the check.
22426
22427
22428 @node OTHERS_In_Aggregates
22429 @subsection @code{OTHERS_In_Aggregates}
22430 @cindex @code{OTHERS_In_Aggregates} rule (for @command{gnatcheck})
22431
22432 @noindent
22433 Flag each use of an @code{others} choice in extension aggregates.
22434 In record and array aggregates, an @code{others} choice is flagged unless
22435 it is used to refer to all components, or to all but one component.
22436
22437 If, in case of a named array aggregate, there are two associations, one
22438 with an @code{others} choice and another with a discrete range, the
22439 @code{others} choice is flagged even if the discrete range specifies
22440 exactly one component; for example, @code{(1..1 => 0, others => 1)}.
22441
22442 This rule has no parameters.
22443
22444 @node OTHERS_In_CASE_Statements
22445 @subsection @code{OTHERS_In_CASE_Statements}
22446 @cindex @code{OTHERS_In_CASE_Statements} rule (for @command{gnatcheck})
22447
22448 @noindent
22449 Flag any use of an @code{others} choice in a @code{case} statement.
22450
22451 This rule has no parameters.
22452
22453 @node OTHERS_In_Exception_Handlers
22454 @subsection @code{OTHERS_In_Exception_Handlers}
22455 @cindex @code{OTHERS_In_Exception_Handlers} rule (for @command{gnatcheck})
22456
22457 @noindent
22458 Flag any use of an @code{others} choice in an exception handler.
22459
22460 This rule has no parameters.
22461
22462
22463 @node Outer_Loop_Exits
22464 @subsection @code{Outer_Loop_Exits}
22465 @cindex @code{Outer_Loop_Exits} rule (for @command{gnatcheck})
22466
22467 @noindent
22468 Flag each @code{exit} statement containing a loop name that is not the name
22469 of the immediately enclosing @code{loop} statement.
22470
22471 This rule has no parameters.
22472
22473
22474 @node Overloaded_Operators
22475 @subsection @code{Overloaded_Operators}
22476 @cindex @code{Overloaded_Operators} rule (for @command{gnatcheck})
22477
22478 @noindent
22479 Flag each function declaration that overloads an operator symbol.
22480 A function body is checked only if the body does not have a
22481 separate spec. Formal functions are also checked. For a
22482 renaming declaration, only renaming-as-declaration is checked
22483
22484 This rule has no parameters.
22485
22486
22487 @node Overly_Nested_Control_Structures
22488 @subsection @code{Overly_Nested_Control_Structures}
22489 @cindex @code{Overly_Nested_Control_Structures} rule (for @command{gnatcheck})
22490
22491 @noindent
22492 Flag each control structure whose nesting level exceeds the value provided
22493 in the rule parameter.
22494
22495 The control structures checked are the following:
22496
22497 @itemize @bullet
22498 @item @code{if} statement
22499 @item @code{case} statement
22500 @item @code{loop} statement
22501 @item Selective accept statement
22502 @item Timed entry call statement
22503 @item Conditional entry call
22504 @item Asynchronous select statement
22505 @end itemize
22506
22507 @noindent
22508 The rule has the following parameter for the @option{+R} option:
22509
22510 @table @emph
22511 @item N
22512 Positive integer specifying the maximal control structure nesting
22513 level that is not flagged
22514 @end table
22515
22516 @noindent
22517 If the parameter for the @option{+R} option is not specified or
22518 if it is not a positive integer, @option{+R} option is ignored.
22519
22520 If more then one option is specified for the gnatcheck call, the later option and
22521 new parameter override the previous one(s).
22522
22523
22524 @node Parameters_Out_Of_Order
22525 @subsection @code{Parameters_Out_Of_Order}
22526 @cindex @code{Parameters_Out_Of_Order} rule (for @command{gnatcheck})
22527
22528 @noindent
22529 Flag each subprogram and entry declaration whose formal parameters are not
22530 ordered according to the following scheme:
22531
22532 @itemize @bullet
22533
22534 @item @code{in} and @code{access} parameters first,
22535 then @code{in out} parameters,
22536 and then @code{out} parameters;
22537
22538 @item for @code{in} mode, parameters with default initialization expressions
22539 occur last
22540 @end itemize
22541
22542 @noindent
22543 Only the first violation of the described order is flagged.
22544
22545 The following constructs are checked:
22546
22547 @itemize @bullet
22548 @item subprogram declarations (including null procedures);
22549 @item generic subprogram declarations;
22550 @item formal subprogram declarations;
22551 @item entry declarations;
22552 @item subprogram bodies and subprogram body stubs that do not
22553 have separate specifications
22554 @end itemize
22555
22556 @noindent
22557 Subprogram renamings are not checked.
22558
22559 This rule has no parameters.
22560
22561
22562 @node Positional_Actuals_For_Defaulted_Generic_Parameters
22563 @subsection @code{Positional_Actuals_For_Defaulted_Generic_Parameters}
22564 @cindex @code{Positional_Actuals_For_Defaulted_Generic_Parameters} rule (for @command{gnatcheck})
22565
22566 @noindent
22567 Flag each generic actual parameter corresponding to a generic formal
22568 parameter with a default initialization, if positional notation is used.
22569
22570 This rule has no parameters.
22571
22572 @node Positional_Actuals_For_Defaulted_Parameters
22573 @subsection @code{Positional_Actuals_For_Defaulted_Parameters}
22574 @cindex @code{Positional_Actuals_For_Defaulted_Parameters} rule (for @command{gnatcheck})
22575
22576 @noindent
22577 Flag each actual parameter to a subprogram or entry call where the
22578 corresponding formal parameter has a default expression, if positional
22579 notation is used.
22580
22581 This rule has no parameters.
22582
22583 @node Positional_Components
22584 @subsection @code{Positional_Components}
22585 @cindex @code{Positional_Components} rule (for @command{gnatcheck})
22586
22587 @noindent
22588 Flag each array, record and extension aggregate that includes positional
22589 notation.
22590
22591 This rule has no parameters.
22592
22593
22594 @node Positional_Generic_Parameters
22595 @subsection @code{Positional_Generic_Parameters}
22596 @cindex @code{Positional_Generic_Parameters} rule (for @command{gnatcheck})
22597
22598 @noindent
22599 Flag each positional actual generic parameter except for the case when
22600 the generic unit being iinstantiated has exactly one generic formal
22601 parameter.
22602
22603 This rule has no parameters.
22604
22605
22606 @node Positional_Parameters
22607 @subsection @code{Positional_Parameters}
22608 @cindex @code{Positional_Parameters} rule (for @command{gnatcheck})
22609
22610 @noindent
22611 Flag each positional parameter notation in a subprogram or entry call,
22612 except for the following:
22613
22614 @itemize @bullet
22615 @item
22616 Parameters of calls to of prefix or infix operators are not flagged
22617 @item
22618 If the called subprogram or entry has only one formal parameter,
22619 the parameter of the call is not flagged;
22620 @item
22621 If a subprogram call uses the @emph{Object.Operation} notation, then
22622 @itemize @minus
22623 @item
22624 the first parameter (that is, @emph{Object}) is not flagged;
22625 @item
22626 if the called subprogram has only two parameters, the second parameter
22627 of the call is not flagged;
22628 @end itemize
22629 @end itemize
22630
22631 @noindent
22632 This rule has no parameters.
22633
22634
22635
22636
22637 @node Predefined_Numeric_Types
22638 @subsection @code{Predefined_Numeric_Types}
22639 @cindex @code{Predefined_Numeric_Types} rule (for @command{gnatcheck})
22640
22641 @noindent
22642 Flag each explicit use of the name of any numeric type or subtype defined
22643 in package @code{Standard}.
22644
22645 The rationale for this rule is to detect when the
22646 program may depend on platform-specific characteristics of the implementation
22647 of the predefined numeric types. Note that this rule is over-pessimistic;
22648 for example, a program that uses @code{String} indexing
22649 likely needs a variable of type @code{Integer}.
22650 Another example is the flagging of predefined numeric types with explicit
22651 constraints:
22652
22653 @smallexample @c ada
22654 subtype My_Integer is Integer range Left .. Right;
22655 Vy_Var : My_Integer;
22656 @end smallexample
22657
22658 @noindent
22659 This rule detects only numeric types and subtypes defined in
22660 @code{Standard}. The use of numeric types and subtypes defined in other
22661 predefined packages (such as @code{System.Any_Priority} or
22662 @code{Ada.Text_IO.Count}) is not flagged
22663
22664 This rule has no parameters.
22665
22666
22667
22668 @node Raising_External_Exceptions
22669 @subsection @code{Raising_External_Exceptions}
22670 @cindex @code{Raising_External_Exceptions} rule (for @command{gnatcheck})
22671
22672 @noindent
22673 Flag any @code{raise} statement, in a program unit declared in a library
22674 package or in a generic library package, for an exception that is
22675 neither a predefined exception nor an exception that is also declared (or
22676 renamed) in the visible part of the package.
22677
22678 This rule has no parameters.
22679
22680
22681
22682 @node Raising_Predefined_Exceptions
22683 @subsection @code{Raising_Predefined_Exceptions}
22684 @cindex @code{Raising_Predefined_Exceptions} rule (for @command{gnatcheck})
22685
22686 @noindent
22687 Flag each @code{raise} statement that raises a predefined exception
22688 (i.e., one of the exceptions @code{Constraint_Error}, @code{Numeric_Error},
22689 @code{Program_Error}, @code{Storage_Error}, or @code{Tasking_Error}).
22690
22691 This rule has no parameters.
22692
22693 @node Separate_Numeric_Error_Handlers
22694 @subsection @code{Separate_Numeric_Error_Handlers}
22695 @cindex @code{Separate_Numeric_Error_Handlers} rule (for @command{gnatcheck})
22696
22697 @noindent
22698 Flags each exception handler that contains a choice for
22699 the predefined @code{Constraint_Error} exception, but does not contain
22700 the choice for the predefined @code{Numeric_Error} exception, or
22701 that contains the choice for @code{Numeric_Error}, but does not contain the
22702 choice for @code{Constraint_Error}.
22703
22704 This rule has no parameters.
22705
22706 @ignore
22707 @node Recursion
22708 @subsection @code{Recursion} (under construction, GLOBAL)
22709 @cindex @code{Recursion} rule (for @command{gnatcheck})
22710
22711 @noindent
22712 Flag recursive subprograms (cycles in the call graph). Declarations, and not
22713 calls, of recursive subprograms are detected.
22714
22715 This rule has no parameters.
22716 @end ignore
22717
22718 @ignore
22719 @node Side_Effect_Functions
22720 @subsection @code{Side_Effect_Functions} (under construction, GLOBAL)
22721 @cindex @code{Side_Effect_Functions} rule (for @command{gnatcheck})
22722
22723 @noindent
22724 Flag functions with side effects.
22725
22726 We define a side effect as changing any data object that is not local for the
22727 body of this function.
22728
22729 At the moment, we do NOT consider a side effect any input-output operations
22730 (changing a state or a content of any file).
22731
22732 We do not consider protected functions for this rule (???)
22733
22734 There are the following sources of side effect:
22735
22736 @enumerate
22737 @item Explicit (or direct) side-effect:
22738
22739 @itemize @bullet
22740 @item
22741 direct assignment to a non-local variable;
22742
22743 @item
22744 direct call to an entity that is known to change some data object that is
22745 not local for the body of this function (Note, that if F1 calls F2 and F2
22746 does have a side effect, this does not automatically mean that F1 also
22747 have a side effect, because it may be the case that F2 is declared in
22748 F1's body and it changes some data object that is global for F2, but
22749 local for F1);
22750 @end itemize
22751
22752 @item Indirect side-effect:
22753 @itemize @bullet
22754 @item
22755 Subprogram calls implicitly issued by:
22756 @itemize @bullet
22757 @item
22758 computing initialization expressions from type declarations as a part
22759 of object elaboration or allocator evaluation;
22760 @item
22761 computing implicit parameters of subprogram or entry calls or generic
22762 instantiations;
22763 @end itemize
22764
22765 @item
22766 activation of a task that change some non-local data object (directly or
22767 indirectly);
22768
22769 @item
22770 elaboration code of a package that is a result of a package instantiation;
22771
22772 @item
22773 controlled objects;
22774 @end itemize
22775
22776 @item Situations when we can suspect a side-effect, but the full static check
22777 is either impossible or too hard:
22778 @itemize @bullet
22779 @item
22780 assignment to access variables or to the objects pointed by access
22781 variables;
22782
22783 @item
22784 call to a subprogram pointed by access-to-subprogram value
22785
22786 @item
22787 dispatching calls;
22788 @end itemize
22789 @end enumerate
22790
22791 @noindent
22792 This rule has no parameters.
22793 @end ignore
22794
22795 @node Slices
22796 @subsection @code{Slices}
22797 @cindex @code{Slices} rule (for @command{gnatcheck})
22798
22799 @noindent
22800 Flag all uses of array slicing
22801
22802 This rule has no parameters.
22803
22804
22805 @node Too_Many_Parents
22806 @subsection @code{Too_Many_Parents}
22807 @cindex @code{Too_Many_Parents} rule (for @command{gnatcheck})
22808
22809 @noindent
22810 Flags any type declaration, single task declaration or single protected
22811 declaration that has more then @option{N} parents, @option{N} is a parameter
22812 of the rule.
22813 A parent here is either a (sub)type denoted by the subtype mark from the
22814 parent_subtype_indication (in case of a derived type declaration), or
22815 any of the progenitors from the interface list, if any.
22816
22817 This rule has the following (mandatory) parameters for the @option{+R} option:
22818
22819 @table @emph
22820 @item N
22821 Positive integer specifying the maximal allowed number of parents.
22822 @end table
22823
22824
22825 @node Unassigned_OUT_Parameters
22826 @subsection @code{Unassigned_OUT_Parameters}
22827 @cindex @code{Unassigned_OUT_Parameters} rule (for @command{gnatcheck})
22828
22829 @noindent
22830 Flags procedures' @code{out} parameters that are not assigned, and
22831 identifies the contexts in which the assignments are missing.
22832
22833 An @code{out} parameter is flagged in the statements in the procedure
22834 body's handled sequence of statements (before the procedure body's
22835 @code{exception} part, if any) if this sequence of statements contains
22836 no assignments to the parameter.
22837
22838 An @code{out} parameter is flagged in an exception handler in the exception
22839 part of the procedure body's handled sequence of statements if the handler
22840 contains no assignment to the parameter.
22841
22842 Bodies of generic procedures are also considered.
22843
22844 The following are treated as assignments to an @code{out} parameter:
22845
22846 @itemize @bullet
22847 @item
22848 an assignment statement, with the parameter or some component as the target;
22849
22850 @item
22851 passing the parameter (or one of its components) as an @code{out} or
22852 @code{in out} parameter.
22853 @end itemize
22854
22855 @noindent
22856 This rule does not have any parameters.
22857
22858
22859
22860 @node Uncommented_BEGIN_In_Package_Bodies
22861 @subsection @code{Uncommented_BEGIN_In_Package_Bodies}
22862 @cindex @code{Uncommented_BEGIN_In_Package_Bodies} rule (for @command{gnatcheck})
22863
22864 @noindent
22865 Flags each package body with declarations and a statement part that does not
22866 include a trailing comment on the line containing the @code{begin} keyword;
22867 this trailing comment needs to specify the package name and nothing else.
22868 The @code{begin} is not flagged if the package body does not
22869 contain any declarations.
22870
22871 If the @code{begin} keyword is placed on the
22872 same line as the last declaration or the first statement, it is flagged
22873 independently of whether the line contains a trailing comment. The
22874 diagnostic message is attached to the line containing the first statement.
22875
22876 This rule has no parameters.
22877
22878 @node Unconditional_Exits
22879 @subsection @code{Unconditional_Exits}
22880 @cindex @code{Unconditional_Exits} rule (for @command{gnatcheck})
22881
22882 @noindent
22883 Flag unconditional @code{exit} statements.
22884
22885 This rule has no parameters.
22886
22887 @node Unconstrained_Array_Returns
22888 @subsection @code{Unconstrained_Array_Returns}
22889 @cindex @code{Unconstrained_Array_Returns} rule (for @command{gnatcheck})
22890
22891 @noindent
22892 Flag each function returning an unconstrained array. Function declarations,
22893 function bodies (and body stubs) having no separate specifications,
22894 and generic function instantiations are checked.
22895 Function calls and function renamings are
22896 not checked.
22897
22898 Generic function declarations, and function declarations in generic
22899 packages are not checked, instead this rule checks the results of
22900 generic instantiations (that is, expanded specification and expanded
22901 body corresponding to an instantiation).
22902
22903 This rule has no parameters.
22904
22905 @node Universal_Ranges
22906 @subsection @code{Universal_Ranges}
22907 @cindex @code{Universal_Ranges} rule (for @command{gnatcheck})
22908
22909 @noindent
22910 Flag discrete ranges that are a part of an index constraint, constrained
22911 array definition, or @code{for}-loop parameter specification, and whose bounds
22912 are both of type @i{universal_integer}. Ranges that have at least one
22913 bound of a specific type (such as @code{1 .. N}, where @code{N} is a variable
22914 or an expression of non-universal type) are not flagged.
22915
22916 This rule has no parameters.
22917
22918
22919 @node Unnamed_Blocks_And_Loops
22920 @subsection @code{Unnamed_Blocks_And_Loops}
22921 @cindex @code{Unnamed_Blocks_And_Loops} rule (for @command{gnatcheck})
22922
22923 @noindent
22924 Flag each unnamed block statement and loop statement.
22925
22926 The rule has no parameters.
22927
22928
22929
22930 @ignore
22931 @node Unused_Subprograms
22932 @subsection @code{Unused_Subprograms} (under construction, GLOBAL)
22933 @cindex @code{Unused_Subprograms} rule (for @command{gnatcheck})
22934
22935 @noindent
22936 Flag all unused subprograms.
22937
22938 This rule has no parameters.
22939 @end ignore
22940
22941
22942
22943
22944 @node USE_PACKAGE_Clauses
22945 @subsection @code{USE_PACKAGE_Clauses}
22946 @cindex @code{USE_PACKAGE_Clauses} rule (for @command{gnatcheck})
22947
22948 @noindent
22949 Flag all @code{use} clauses for packages; @code{use type} clauses are
22950 not flagged.
22951
22952 This rule has no parameters.
22953
22954
22955 @node Visible_Components
22956 @subsection @code{Visible_Components}
22957 @cindex @code{Visible_Components} rule (for @command{gnatcheck})
22958
22959 @noindent
22960 Flags all the type declarations located in the visible part of a library
22961 package or a library generic package that can declare a visible component. A
22962 type is considered as declaring a visible component if it contains a record
22963 definition by its own or as a part of a record extension. Type declaration is
22964 flagged even if it contains a record definition that defines no components.
22965
22966 Declarations located in private parts of local (generic) packages are not
22967 flagged. Declarations in private packages are not flagged.
22968
22969 This rule has no parameters.
22970
22971
22972 @node Volatile_Objects_Without_Address_Clauses
22973 @subsection @code{Volatile_Objects_Without_Address_Clauses}
22974 @cindex @code{Volatile_Objects_Without_Address_Clauses} rule (for @command{gnatcheck})
22975
22976 @noindent
22977 Flag each volatile object that does not have an address clause.
22978
22979 The following check is made: if the pragma @code{Volatile} is applied to a
22980 data object or to its type, then an address clause must
22981 be supplied for this object.
22982
22983 This rule does not check the components of data objects,
22984 array components that are volatile as a result of the pragma
22985 @code{Volatile_Components}, or objects that are volatile because
22986 they are atomic as a result of pragmas @code{Atomic} or
22987 @code{Atomic_Components}.
22988
22989 Only variable declarations, and not constant declarations, are checked.
22990
22991 This rule has no parameters.
22992
22993 @node Example of gnatcheck Usage
22994 @section Example of @command{gnatcheck} Usage
22995
22996 @noindent
22997 Here is a simple example. Suppose that in the current directory we have a
22998 project file named @file{gnatcheck_example.gpr} with the following content:
22999
23000 @smallexample @c projectfile
23001 project Gnatcheck_Example is
23002
23003 for Source_Dirs use ("src");
23004 for Object_Dir use "obj";
23005 for Main use ("main.adb");
23006
23007 package Check is
23008 for Default_Switches ("ada") use ("-rules", "-from=coding_standard");
23009 end Check;
23010
23011 end Gnatcheck_Example;
23012 @end smallexample
23013
23014 @noindent
23015 And the file named @file{coding_standard} is also located in the current
23016 directory and has the following content:
23017
23018 @smallexample
23019 -----------------------------------------------------
23020 -- This is a sample gnatcheck coding standard file --
23021 -----------------------------------------------------
23022
23023 -- First, turning on rules, that are directly implemented in gnatcheck
23024 +RAbstract_Type_Declarations
23025 +RAnonymous_Arrays
23026 +RLocal_Packages
23027 +RFloat_Equality_Checks
23028 +REXIT_Statements_With_No_Loop_Name
23029
23030 -- Then, activating compiler checks of interest:
23031 +RStyle_Checks:e
23032 -- This style check checks if a unit name is present on END keyword that
23033 -- is the end of the unit declaration
23034 @end smallexample
23035
23036 @noindent
23037 And the subdirectory @file{src} contains the following Ada sources:
23038
23039 @file{pack.ads}:
23040
23041 @smallexample @c ada
23042 package Pack is
23043 type T is abstract tagged private;
23044 procedure P (X : T) is abstract;
23045
23046 package Inner is
23047 type My_Float is digits 8;
23048 function Is_Equal (L, R : My_Float) return Boolean;
23049 end Inner;
23050 private
23051 type T is abstract tagged null record;
23052 end;
23053 @end smallexample
23054
23055 @noindent
23056 @file{pack.adb}:
23057
23058 @smallexample @c ada
23059 package body Pack is
23060 package body Inner is
23061 function Is_Equal (L, R : My_Float) return Boolean is
23062 begin
23063 return L = R;
23064 end;
23065 end Inner;
23066 end Pack;
23067 @end smallexample
23068
23069 @noindent
23070 and @file{main.adb}
23071
23072 @smallexample @c ada
23073 with Pack; use Pack;
23074 procedure Main is
23075
23076 pragma Annotate
23077 (gnatcheck, Exempt_On, "Anonymous_Arrays", "this one is fine");
23078 Float_Array : array (1 .. 10) of Inner.My_Float;
23079 pragma Annotate (gnatcheck, Exempt_Off, "Anonymous_Arrays");
23080
23081 Another_Float_Array : array (1 .. 10) of Inner.My_Float;
23082
23083 use Inner;
23084
23085 B : Boolean := False;
23086
23087 begin
23088 for J in Float_Array'Range loop
23089 if Is_Equal (Float_Array (J), Another_Float_Array (J)) then
23090 B := True;
23091 exit;
23092 end if;
23093 end loop;
23094 end Main;
23095 @end smallexample
23096
23097 @noindent
23098 And suppose we call @command{gnatcheck} from the current directory using
23099 the @command{gnat} driver:
23100
23101 @smallexample
23102 gnat check -Pgnatcheck_example.gpr
23103 @end smallexample
23104
23105 @noindent
23106 As a result, @command{gnatcheck} is called to check all the files from the
23107 project @file{gnatcheck_example.gpr} using the coding standard defined by
23108 the file @file{coding_standard}. As the result, the @command{gnatcheck}
23109 report file named @file{gnatcheck.out} will be created in the current
23110 directory, and it will have the following content:
23111
23112 @smallexample
23113 RULE CHECKING REPORT
23114
23115 1. OVERVIEW
23116
23117 Date and time of execution: 2009.10.28 14:17
23118 Tool version: GNATCHECK (built with ASIS 2.0.R for GNAT Pro 6.3.0w (20091016))
23119 Command line:
23120
23121 gnatcheck -files=.../GNAT-TEMP-000004.TMP -cargs -gnatec=.../GNAT-TEMP-000003.TMP -rules -from=coding_standard
23122
23123 Coding standard (applied rules):
23124 Abstract_Type_Declarations
23125 Anonymous_Arrays
23126 EXIT_Statements_With_No_Loop_Name
23127 Float_Equality_Checks
23128 Local_Packages
23129
23130 Compiler style checks: -gnatye
23131
23132 Number of coding standard violations: 6
23133 Number of exempted coding standard violations: 1
23134
23135 2. DETECTED RULE VIOLATIONS
23136
23137 2.1. NON-EXEMPTED VIOLATIONS
23138
23139 Source files with non-exempted violations
23140 pack.ads
23141 pack.adb
23142 main.adb
23143
23144 List of violations grouped by files, and ordered by increasing source location:
23145
23146 pack.ads:2:4: declaration of abstract type
23147 pack.ads:5:4: declaration of local package
23148 pack.ads:10:30: declaration of abstract type
23149 pack.ads:11:1: (style) "end Pack" required
23150 pack.adb:5:19: use of equality operation for float values
23151 pack.adb:6:7: (style) "end Is_Equal" required
23152 main.adb:9:26: anonymous array type
23153 main.adb:19:10: exit statement with no loop name
23154
23155 2.2. EXEMPTED VIOLATIONS
23156
23157 Source files with exempted violations
23158 main.adb
23159
23160 List of violations grouped by files, and ordered by increasing source location:
23161
23162 main.adb:6:18: anonymous array type
23163 (this one is fine)
23164
23165 2.3. SOURCE FILES WITH NO VIOLATION
23166
23167 No files without violations
23168
23169 END OF REPORT
23170 @end smallexample
23171
23172
23173 @c *********************************
23174 @node Creating Sample Bodies Using gnatstub
23175 @chapter Creating Sample Bodies Using @command{gnatstub}
23176 @findex gnatstub
23177
23178 @noindent
23179 @command{gnatstub} creates body stubs, that is, empty but compilable bodies
23180 for library unit declarations.
23181
23182 Note: to invoke @code{gnatstub} with a project file, use the @code{gnat}
23183 driver (see @ref{The GNAT Driver and Project Files}).
23184
23185 To create a body stub, @command{gnatstub} has to compile the library
23186 unit declaration. Therefore, bodies can be created only for legal
23187 library units. Moreover, if a library unit depends semantically upon
23188 units located outside the current directory, you have to provide
23189 the source search path when calling @command{gnatstub}, see the description
23190 of @command{gnatstub} switches below.
23191
23192 By default, all the program unit body stubs generated by @code{gnatstub}
23193 raise the predefined @code{Program_Error} exception, which will catch
23194 accidental calls of generated stubs. This behavior can be changed with
23195 option @option{^--no-exception^/NO_EXCEPTION^} (see below).
23196
23197 @menu
23198 * Running gnatstub::
23199 * Switches for gnatstub::
23200 @end menu
23201
23202 @node Running gnatstub
23203 @section Running @command{gnatstub}
23204
23205 @noindent
23206 @command{gnatstub} has the command-line interface of the form
23207
23208 @smallexample
23209 @c $ gnatstub @ovar{switches} @var{filename} @ovar{directory}
23210 @c Expanding @ovar macro inline (explanation in macro def comments)
23211 $ gnatstub @r{[}@var{switches}@r{]} @var{filename} @r{[}@var{directory}@r{]}
23212 @end smallexample
23213
23214 @noindent
23215 where
23216 @table @var
23217 @item filename
23218 is the name of the source file that contains a library unit declaration
23219 for which a body must be created. The file name may contain the path
23220 information.
23221 The file name does not have to follow the GNAT file name conventions. If the
23222 name
23223 does not follow GNAT file naming conventions, the name of the body file must
23224 be provided
23225 explicitly as the value of the @option{^-o^/BODY=^@var{body-name}} option.
23226 If the file name follows the GNAT file naming
23227 conventions and the name of the body file is not provided,
23228 @command{gnatstub}
23229 creates the name
23230 of the body file from the argument file name by replacing the @file{.ads}
23231 suffix
23232 with the @file{.adb} suffix.
23233
23234 @item directory
23235 indicates the directory in which the body stub is to be placed (the default
23236 is the
23237 current directory)
23238
23239 @item switches
23240 is an optional sequence of switches as described in the next section
23241 @end table
23242
23243 @node Switches for gnatstub
23244 @section Switches for @command{gnatstub}
23245
23246 @table @option
23247 @c !sort!
23248
23249 @item ^-f^/FULL^
23250 @cindex @option{^-f^/FULL^} (@command{gnatstub})
23251 If the destination directory already contains a file with the name of the
23252 body file
23253 for the argument spec file, replace it with the generated body stub.
23254
23255 @item ^-hs^/HEADER=SPEC^
23256 @cindex @option{^-hs^/HEADER=SPEC^} (@command{gnatstub})
23257 Put the comment header (i.e., all the comments preceding the
23258 compilation unit) from the source of the library unit declaration
23259 into the body stub.
23260
23261 @item ^-hg^/HEADER=GENERAL^
23262 @cindex @option{^-hg^/HEADER=GENERAL^} (@command{gnatstub})
23263 Put a sample comment header into the body stub.
23264
23265 @item ^--header-file=@var{filename}^/FROM_HEADER_FILE=@var{filename}^
23266 @cindex @option{^--header-file^/FROM_HEADER_FILE=^} (@command{gnatstub})
23267 Use the content of the file as the comment header for a generated body stub.
23268
23269 @ifclear vms
23270 @item -IDIR
23271 @cindex @option{-IDIR} (@command{gnatstub})
23272 @itemx -I-
23273 @cindex @option{-I-} (@command{gnatstub})
23274 @end ifclear
23275 @ifset vms
23276 @item /NOCURRENT_DIRECTORY
23277 @cindex @option{/NOCURRENT_DIRECTORY} (@command{gnatstub})
23278 @end ifset
23279 ^These switches have ^This switch has^ the same meaning as in calls to
23280 @command{gcc}.
23281 ^They define ^It defines ^ the source search path in the call to
23282 @command{gcc} issued
23283 by @command{gnatstub} to compile an argument source file.
23284
23285 @item ^-gnatec^/CONFIGURATION_PRAGMAS_FILE=^@var{PATH}
23286 @cindex @option{^-gnatec^/CONFIGURATION_PRAGMAS_FILE^} (@command{gnatstub})
23287 This switch has the same meaning as in calls to @command{gcc}.
23288 It defines the additional configuration file to be passed to the call to
23289 @command{gcc} issued
23290 by @command{gnatstub} to compile an argument source file.
23291
23292 @item ^-gnatyM^/MAX_LINE_LENGTH=^@var{n}
23293 @cindex @option{^-gnatyM^/MAX_LINE_LENGTH^} (@command{gnatstub})
23294 (@var{n} is a non-negative integer). Set the maximum line length in the
23295 body stub to @var{n}; the default is 79. The maximum value that can be
23296 specified is 32767. Note that in the special case of configuration
23297 pragma files, the maximum is always 32767 regardless of whether or
23298 not this switch appears.
23299
23300 @item ^-gnaty^/STYLE_CHECKS=^@var{n}
23301 @cindex @option{^-gnaty^/STYLE_CHECKS=^} (@command{gnatstub})
23302 (@var{n} is a non-negative integer from 1 to 9). Set the indentation level in
23303 the generated body sample to @var{n}.
23304 The default indentation is 3.
23305
23306 @item ^-gnatyo^/ORDERED_SUBPROGRAMS^
23307 @cindex @option{^-gnato^/ORDERED_SUBPROGRAMS^} (@command{gnatstub})
23308 Order local bodies alphabetically. (By default local bodies are ordered
23309 in the same way as the corresponding local specs in the argument spec file.)
23310
23311 @item ^-i^/INDENTATION=^@var{n}
23312 @cindex @option{^-i^/INDENTATION^} (@command{gnatstub})
23313 Same as @option{^-gnaty^/STYLE_CHECKS=^@var{n}}
23314
23315 @item ^-k^/TREE_FILE=SAVE^
23316 @cindex @option{^-k^/TREE_FILE=SAVE^} (@command{gnatstub})
23317 Do not remove the tree file (i.e., the snapshot of the compiler internal
23318 structures used by @command{gnatstub}) after creating the body stub.
23319
23320 @item ^-l^/LINE_LENGTH=^@var{n}
23321 @cindex @option{^-l^/LINE_LENGTH^} (@command{gnatstub})
23322 Same as @option{^-gnatyM^/MAX_LINE_LENGTH=^@var{n}}
23323
23324 @item ^--no-exception^/NO_EXCEPTION^
23325 @cindex @option{^--no-exception^/NO_EXCEPTION^} (@command{gnatstub})
23326 Avoind raising PROGRAM_ERROR in the generated bodies of program unit stubs.
23327 This is not always possible for function stubs.
23328
23329 @item ^--no-local-header^/NO_LOCAL_HEADER^
23330 @cindex @option{^--no-local-header^/NO_LOCAL_HEADER^} (@command{gnatstub})
23331 Do not place local comment header with unit name before body stub for a
23332 unit.
23333
23334 @item ^-o ^/BODY=^@var{body-name}
23335 @cindex @option{^-o^/BODY^} (@command{gnatstub})
23336 Body file name. This should be set if the argument file name does not
23337 follow
23338 the GNAT file naming
23339 conventions. If this switch is omitted the default name for the body will be
23340 obtained
23341 from the argument file name according to the GNAT file naming conventions.
23342
23343 @item ^-q^/QUIET^
23344 @cindex @option{^-q^/QUIET^} (@command{gnatstub})
23345 Quiet mode: do not generate a confirmation when a body is
23346 successfully created, and do not generate a message when a body is not
23347 required for an
23348 argument unit.
23349
23350 @item ^-r^/TREE_FILE=REUSE^
23351 @cindex @option{^-r^/TREE_FILE=REUSE^} (@command{gnatstub})
23352 Reuse the tree file (if it exists) instead of creating it. Instead of
23353 creating the tree file for the library unit declaration, @command{gnatstub}
23354 tries to find it in the current directory and use it for creating
23355 a body. If the tree file is not found, no body is created. This option
23356 also implies @option{^-k^/SAVE^}, whether or not
23357 the latter is set explicitly.
23358
23359 @item ^-t^/TREE_FILE=OVERWRITE^
23360 @cindex @option{^-t^/TREE_FILE=OVERWRITE^} (@command{gnatstub})
23361 Overwrite the existing tree file. If the current directory already
23362 contains the file which, according to the GNAT file naming rules should
23363 be considered as a tree file for the argument source file,
23364 @command{gnatstub}
23365 will refuse to create the tree file needed to create a sample body
23366 unless this option is set.
23367
23368 @item ^-v^/VERBOSE^
23369 @cindex @option{^-v^/VERBOSE^} (@command{gnatstub})
23370 Verbose mode: generate version information.
23371
23372 @end table
23373
23374 @c *********************************
23375 @node Generating Ada Bindings for C and C++ headers
23376 @chapter Generating Ada Bindings for C and C++ headers
23377 @findex binding
23378
23379 @noindent
23380 GNAT now comes with a binding generator for C and C++ headers which is
23381 intended to do 95% of the tedious work of generating Ada specs from C
23382 or C++ header files.
23383
23384 Note that this capability is not intended to generate 100% correct Ada specs,
23385 and will is some cases require manual adjustments, although it can often
23386 be used out of the box in practice.
23387
23388 Some of the known limitations include:
23389
23390 @itemize @bullet
23391 @item only very simple character constant macros are translated into Ada
23392 constants. Function macros (macros with arguments) are partially translated
23393 as comments, to be completed manually if needed.
23394 @item some extensions (e.g. vector types) are not supported
23395 @item pointers to pointers or complex structures are mapped to System.Address
23396 @end itemize
23397
23398 The code generated is using the Ada 2005 syntax, which makes it
23399 easier to interface with other languages than previous versions of Ada.
23400
23401 @menu
23402 * Running the binding generator::
23403 * Generating bindings for C++ headers::
23404 * Switches::
23405 @end menu
23406
23407 @node Running the binding generator
23408 @section Running the binding generator
23409
23410 @noindent
23411 The binding generator is part of the @command{gcc} compiler and can be
23412 invoked via the @option{-fdump-ada-spec} switch, which will generate Ada
23413 spec files for the header files specified on the command line, and all
23414 header files needed by these files transitivitely. For example:
23415
23416 @smallexample
23417 $ g++ -c -fdump-ada-spec -C /usr/include/time.h
23418 $ gcc -c -gnat05 *.ads
23419 @end smallexample
23420
23421 will generate, under GNU/Linux, the following files: @file{time_h.ads},
23422 @file{bits_time_h.ads}, @file{stddef_h.ads}, @file{bits_types_h.ads} which
23423 correspond to the files @file{/usr/include/time.h},
23424 @file{/usr/include/bits/time.h}, etc@dots{}, and will then compile in Ada 2005
23425 mode these Ada specs.
23426
23427 The @code{-C} switch tells @command{gcc} to extract comments from headers,
23428 and will attempt to generate corresponding Ada comments.
23429
23430 If you want to generate a single Ada file and not the transitive closure, you
23431 can use instead the @option{-fdump-ada-spec-slim} switch.
23432
23433 Note that we recommend when possible to use the @command{g++} driver to
23434 generate bindings, even for most C headers, since this will in general
23435 generate better Ada specs. For generating bindings for C++ headers, it is
23436 mandatory to use the @command{g++} command, or @command{gcc -x c++} which
23437 is equivalent in this case. If @command{g++} cannot work on your C headers
23438 because of incompatibilities between C and C++, then you can fallback to
23439 @command{gcc} instead.
23440
23441 For an example of better bindings generated from the C++ front-end,
23442 the name of the parameters (when available) are actually ignored by the C
23443 front-end. Consider the following C header:
23444
23445 @smallexample
23446 extern void foo (int variable);
23447 @end smallexample
23448
23449 with the C front-end, @code{variable} is ignored, and the above is handled as:
23450
23451 @smallexample
23452 extern void foo (int);
23453 @end smallexample
23454
23455 generating a generic:
23456
23457 @smallexample
23458 procedure foo (param1 : int);
23459 @end smallexample
23460
23461 with the C++ front-end, the name is available, and we generate:
23462
23463 @smallexample
23464 procedure foo (variable : int);
23465 @end smallexample
23466
23467 In some cases, the generated bindings will be more complete or more meaningful
23468 when defining some macros, which you can do via the @option{-D} switch. This
23469 is for example the case with @file{Xlib.h} under GNU/Linux:
23470
23471 @smallexample
23472 g++ -c -fdump-ada-spec -DXLIB_ILLEGAL_ACCESS -C /usr/include/X11/Xlib.h
23473 @end smallexample
23474
23475 The above will generate more complete bindings than a straight call without
23476 the @option{-DXLIB_ILLEGAL_ACCESS} switch.
23477
23478 In other cases, it is not possible to parse a header file in a stand alone
23479 manner, because other include files need to be included first. In this
23480 case, the solution is to create a small header file including the needed
23481 @code{#include} and possible @code{#define} directives. For example, to
23482 generate Ada bindings for @file{readline/readline.h}, you need to first
23483 include @file{stdio.h}, so you can create a file with the following two
23484 lines in e.g. @file{readline1.h}:
23485
23486 @smallexample
23487 #include <stdio.h>
23488 #include <readline/readline.h>
23489 @end smallexample
23490
23491 and then generate Ada bindings from this file:
23492
23493 @smallexample
23494 $ g++ -c -fdump-ada-spec readline1.h
23495 @end smallexample
23496
23497 @node Generating bindings for C++ headers
23498 @section Generating bindings for C++ headers
23499
23500 @noindent
23501 Generating bindings for C++ headers is done using the same options, always
23502 with the @command{g++} compiler.
23503
23504 In this mode, C++ classes will be mapped to Ada tagged types, constructors
23505 will be mapped using the @code{CPP_Constructor} pragma, and when possible,
23506 multiple inheritance of abstract classes will be mapped to Ada interfaces
23507 (@xref{Interfacing to C++,,,gnat_rm, GNAT Reference Manual}, for additional
23508 information on interfacing to C++).
23509
23510 For example, given the following C++ header file:
23511
23512 @smallexample
23513 @group
23514 @cartouche
23515 class Carnivore @{
23516 public:
23517 virtual int Number_Of_Teeth () = 0;
23518 @};
23519
23520 class Domestic @{
23521 public:
23522 virtual void Set_Owner (char* Name) = 0;
23523 @};
23524
23525 class Animal @{
23526 public:
23527 int Age_Count;
23528 virtual void Set_Age (int New_Age);
23529 @};
23530
23531 class Dog : Animal, Carnivore, Domestic @{
23532 public:
23533 int Tooth_Count;
23534 char *Owner;
23535
23536 virtual int Number_Of_Teeth ();
23537 virtual void Set_Owner (char* Name);
23538
23539 Dog();
23540 @};
23541 @end cartouche
23542 @end group
23543 @end smallexample
23544
23545 The corresponding Ada code is generated:
23546
23547 @smallexample @c ada
23548 @group
23549 @cartouche
23550 package Class_Carnivore is
23551 type Carnivore is limited interface;
23552 pragma Import (CPP, Carnivore);
23553
23554 function Number_Of_Teeth (this : access Carnivore) return int is abstract;
23555 end;
23556 use Class_Carnivore;
23557
23558 package Class_Domestic is
23559 type Domestic is limited interface;
23560 pragma Import (CPP, Domestic);
23561
23562 procedure Set_Owner
23563 (this : access Domestic;
23564 Name : Interfaces.C.Strings.chars_ptr) is abstract;
23565 end;
23566 use Class_Domestic;
23567
23568 package Class_Animal is
23569 type Animal is tagged limited record
23570 Age_Count : aliased int;
23571 end record;
23572 pragma Import (CPP, Animal);
23573
23574 procedure Set_Age (this : access Animal; New_Age : int);
23575 pragma Import (CPP, Set_Age, "_ZN6Animal7Set_AgeEi");
23576 end;
23577 use Class_Animal;
23578
23579 package Class_Dog is
23580 type Dog is new Animal and Carnivore and Domestic with record
23581 Tooth_Count : aliased int;
23582 Owner : Interfaces.C.Strings.chars_ptr;
23583 end record;
23584 pragma Import (CPP, Dog);
23585
23586 function Number_Of_Teeth (this : access Dog) return int;
23587 pragma Import (CPP, Number_Of_Teeth, "_ZN3Dog15Number_Of_TeethEv");
23588
23589 procedure Set_Owner
23590 (this : access Dog; Name : Interfaces.C.Strings.chars_ptr);
23591 pragma Import (CPP, Set_Owner, "_ZN3Dog9Set_OwnerEPc");
23592
23593 function New_Dog return Dog;
23594 pragma CPP_Constructor (New_Dog);
23595 pragma Import (CPP, New_Dog, "_ZN3DogC1Ev");
23596 end;
23597 use Class_Dog;
23598 @end cartouche
23599 @end group
23600 @end smallexample
23601
23602 @node Switches
23603 @section Switches
23604
23605 @table @option
23606 @item -fdump-ada-spec
23607 @cindex @option{-fdump-ada-spec} (@command{gcc})
23608 Generate Ada spec files for the given header files transitively (including
23609 all header files that these headers depend upon).
23610
23611 @item -fdump-ada-spec-slim
23612 @cindex @option{-fdump-ada-spec-slim} (@command{gcc})
23613 Generate Ada spec files for the header files specified on the command line
23614 only.
23615
23616 @item -C
23617 @cindex @option{-C} (@command{gcc})
23618 Extract comments from headers and generate Ada comments in the Ada spec files.
23619 @end table
23620
23621 @node Other Utility Programs
23622 @chapter Other Utility Programs
23623
23624 @noindent
23625 This chapter discusses some other utility programs available in the Ada
23626 environment.
23627
23628 @menu
23629 * Using Other Utility Programs with GNAT::
23630 * The External Symbol Naming Scheme of GNAT::
23631 * Converting Ada Files to html with gnathtml::
23632 * Installing gnathtml::
23633 @ifset vms
23634 * LSE::
23635 * Profiling::
23636 @end ifset
23637 @end menu
23638
23639 @node Using Other Utility Programs with GNAT
23640 @section Using Other Utility Programs with GNAT
23641
23642 @noindent
23643 The object files generated by GNAT are in standard system format and in
23644 particular the debugging information uses this format. This means
23645 programs generated by GNAT can be used with existing utilities that
23646 depend on these formats.
23647
23648 @ifclear vms
23649 In general, any utility program that works with C will also often work with
23650 Ada programs generated by GNAT. This includes software utilities such as
23651 gprof (a profiling program), @code{gdb} (the FSF debugger), and utilities such
23652 as Purify.
23653 @end ifclear
23654
23655 @node The External Symbol Naming Scheme of GNAT
23656 @section The External Symbol Naming Scheme of GNAT
23657
23658 @noindent
23659 In order to interpret the output from GNAT, when using tools that are
23660 originally intended for use with other languages, it is useful to
23661 understand the conventions used to generate link names from the Ada
23662 entity names.
23663
23664 All link names are in all lowercase letters. With the exception of library
23665 procedure names, the mechanism used is simply to use the full expanded
23666 Ada name with dots replaced by double underscores. For example, suppose
23667 we have the following package spec:
23668
23669 @smallexample @c ada
23670 @group
23671 @cartouche
23672 package QRS is
23673 MN : Integer;
23674 end QRS;
23675 @end cartouche
23676 @end group
23677 @end smallexample
23678
23679 @noindent
23680 The variable @code{MN} has a full expanded Ada name of @code{QRS.MN}, so
23681 the corresponding link name is @code{qrs__mn}.
23682 @findex Export
23683 Of course if a @code{pragma Export} is used this may be overridden:
23684
23685 @smallexample @c ada
23686 @group
23687 @cartouche
23688 package Exports is
23689 Var1 : Integer;
23690 pragma Export (Var1, C, External_Name => "var1_name");
23691 Var2 : Integer;
23692 pragma Export (Var2, C, Link_Name => "var2_link_name");
23693 end Exports;
23694 @end cartouche
23695 @end group
23696 @end smallexample
23697
23698 @noindent
23699 In this case, the link name for @var{Var1} is whatever link name the
23700 C compiler would assign for the C function @var{var1_name}. This typically
23701 would be either @var{var1_name} or @var{_var1_name}, depending on operating
23702 system conventions, but other possibilities exist. The link name for
23703 @var{Var2} is @var{var2_link_name}, and this is not operating system
23704 dependent.
23705
23706 @findex _main
23707 One exception occurs for library level procedures. A potential ambiguity
23708 arises between the required name @code{_main} for the C main program,
23709 and the name we would otherwise assign to an Ada library level procedure
23710 called @code{Main} (which might well not be the main program).
23711
23712 To avoid this ambiguity, we attach the prefix @code{_ada_} to such
23713 names. So if we have a library level procedure such as
23714
23715 @smallexample @c ada
23716 @group
23717 @cartouche
23718 procedure Hello (S : String);
23719 @end cartouche
23720 @end group
23721 @end smallexample
23722
23723 @noindent
23724 the external name of this procedure will be @var{_ada_hello}.
23725
23726
23727 @node Converting Ada Files to html with gnathtml
23728 @section Converting Ada Files to HTML with @code{gnathtml}
23729
23730 @noindent
23731 This @code{Perl} script allows Ada source files to be browsed using
23732 standard Web browsers. For installation procedure, see the section
23733 @xref{Installing gnathtml}.
23734
23735 Ada reserved keywords are highlighted in a bold font and Ada comments in
23736 a blue font. Unless your program was compiled with the gcc @option{-gnatx}
23737 switch to suppress the generation of cross-referencing information, user
23738 defined variables and types will appear in a different color; you will
23739 be able to click on any identifier and go to its declaration.
23740
23741 The command line is as follow:
23742 @smallexample
23743 @c $ perl gnathtml.pl @ovar{^switches^options^} @var{ada-files}
23744 @c Expanding @ovar macro inline (explanation in macro def comments)
23745 $ perl gnathtml.pl @r{[}@var{^switches^options^}@r{]} @var{ada-files}
23746 @end smallexample
23747
23748 @noindent
23749 You can pass it as many Ada files as you want. @code{gnathtml} will generate
23750 an html file for every ada file, and a global file called @file{index.htm}.
23751 This file is an index of every identifier defined in the files.
23752
23753 The available ^switches^options^ are the following ones:
23754
23755 @table @option
23756 @item -83
23757 @cindex @option{-83} (@code{gnathtml})
23758 Only the Ada 83 subset of keywords will be highlighted.
23759
23760 @item -cc @var{color}
23761 @cindex @option{-cc} (@code{gnathtml})
23762 This option allows you to change the color used for comments. The default
23763 value is green. The color argument can be any name accepted by html.
23764
23765 @item -d
23766 @cindex @option{-d} (@code{gnathtml})
23767 If the Ada files depend on some other files (for instance through
23768 @code{with} clauses, the latter files will also be converted to html.
23769 Only the files in the user project will be converted to html, not the files
23770 in the run-time library itself.
23771
23772 @item -D
23773 @cindex @option{-D} (@code{gnathtml})
23774 This command is the same as @option{-d} above, but @command{gnathtml} will
23775 also look for files in the run-time library, and generate html files for them.
23776
23777 @item -ext @var{extension}
23778 @cindex @option{-ext} (@code{gnathtml})
23779 This option allows you to change the extension of the generated HTML files.
23780 If you do not specify an extension, it will default to @file{htm}.
23781
23782 @item -f
23783 @cindex @option{-f} (@code{gnathtml})
23784 By default, gnathtml will generate html links only for global entities
23785 ('with'ed units, global variables and types,@dots{}). If you specify
23786 @option{-f} on the command line, then links will be generated for local
23787 entities too.
23788
23789 @item -l @var{number}
23790 @cindex @option{-l} (@code{gnathtml})
23791 If this ^switch^option^ is provided and @var{number} is not 0, then
23792 @code{gnathtml} will number the html files every @var{number} line.
23793
23794 @item -I @var{dir}
23795 @cindex @option{-I} (@code{gnathtml})
23796 Specify a directory to search for library files (@file{.ALI} files) and
23797 source files. You can provide several -I switches on the command line,
23798 and the directories will be parsed in the order of the command line.
23799
23800 @item -o @var{dir}
23801 @cindex @option{-o} (@code{gnathtml})
23802 Specify the output directory for html files. By default, gnathtml will
23803 saved the generated html files in a subdirectory named @file{html/}.
23804
23805 @item -p @var{file}
23806 @cindex @option{-p} (@code{gnathtml})
23807 If you are using Emacs and the most recent Emacs Ada mode, which provides
23808 a full Integrated Development Environment for compiling, checking,
23809 running and debugging applications, you may use @file{.gpr} files
23810 to give the directories where Emacs can find sources and object files.
23811
23812 Using this ^switch^option^, you can tell gnathtml to use these files.
23813 This allows you to get an html version of your application, even if it
23814 is spread over multiple directories.
23815
23816 @item -sc @var{color}
23817 @cindex @option{-sc} (@code{gnathtml})
23818 This ^switch^option^ allows you to change the color used for symbol
23819 definitions.
23820 The default value is red. The color argument can be any name accepted by html.
23821
23822 @item -t @var{file}
23823 @cindex @option{-t} (@code{gnathtml})
23824 This ^switch^option^ provides the name of a file. This file contains a list of
23825 file names to be converted, and the effect is exactly as though they had
23826 appeared explicitly on the command line. This
23827 is the recommended way to work around the command line length limit on some
23828 systems.
23829
23830 @end table
23831
23832 @node Installing gnathtml
23833 @section Installing @code{gnathtml}
23834
23835 @noindent
23836 @code{Perl} needs to be installed on your machine to run this script.
23837 @code{Perl} is freely available for almost every architecture and
23838 Operating System via the Internet.
23839
23840 On Unix systems, you may want to modify the first line of the script
23841 @code{gnathtml}, to explicitly tell the Operating system where Perl
23842 is. The syntax of this line is:
23843 @smallexample
23844 #!full_path_name_to_perl
23845 @end smallexample
23846
23847 @noindent
23848 Alternatively, you may run the script using the following command line:
23849
23850 @smallexample
23851 @c $ perl gnathtml.pl @ovar{switches} @var{files}
23852 @c Expanding @ovar macro inline (explanation in macro def comments)
23853 $ perl gnathtml.pl @r{[}@var{switches}@r{]} @var{files}
23854 @end smallexample
23855
23856 @ifset vms
23857 @node LSE
23858 @section LSE
23859 @findex LSE
23860
23861 @noindent
23862 The GNAT distribution provides an Ada 95 template for the HP Language
23863 Sensitive Editor (LSE), a component of DECset. In order to
23864 access it, invoke LSE with the qualifier /ENVIRONMENT=GNU:[LIB]ADA95.ENV.
23865
23866 @node Profiling
23867 @section Profiling
23868 @findex PCA
23869
23870 @noindent
23871 GNAT supports The HP Performance Coverage Analyzer (PCA), a component
23872 of DECset. To use it proceed as outlined under ``HELP PCA'', except for running
23873 the collection phase with the /DEBUG qualifier.
23874
23875 @smallexample
23876 $ GNAT MAKE /DEBUG <PROGRAM_NAME>
23877 $ DEFINE LIB$DEBUG PCA$COLLECTOR
23878 $ RUN/DEBUG <PROGRAM_NAME>
23879 @end smallexample
23880 @noindent
23881 @end ifset
23882
23883 @ifclear vms
23884 @c ******************************
23885 @node Code Coverage and Profiling
23886 @chapter Code Coverage and Profiling
23887 @cindex Code Coverage
23888 @cindex Profiling
23889
23890 @noindent
23891 This chapter describes how to use @code{gcov} - coverage testing tool - and
23892 @code{gprof} - profiler tool - on your Ada programs.
23893
23894 @menu
23895 * Code Coverage of Ada Programs using gcov::
23896 * Profiling an Ada Program using gprof::
23897 @end menu
23898
23899 @node Code Coverage of Ada Programs using gcov
23900 @section Code Coverage of Ada Programs using gcov
23901 @cindex gcov
23902 @cindex -fprofile-arcs
23903 @cindex -ftest-coverage
23904 @cindex -coverage
23905 @cindex Code Coverage
23906
23907 @noindent
23908 @code{gcov} is a test coverage program: it analyzes the execution of a given
23909 program on selected tests, to help you determine the portions of the program
23910 that are still untested.
23911
23912 @code{gcov} is part of the GCC suite, and is described in detail in the GCC
23913 User's Guide. You can refer to this documentation for a more complete
23914 description.
23915
23916 This chapter provides a quick startup guide, and
23917 details some Gnat-specific features.
23918
23919 @menu
23920 * Quick startup guide::
23921 * Gnat specifics::
23922 @end menu
23923
23924 @node Quick startup guide
23925 @subsection Quick startup guide
23926
23927 In order to perform coverage analysis of a program using @code{gcov}, 3
23928 steps are needed:
23929
23930 @itemize @bullet
23931 @item
23932 Code instrumentation during the compilation process
23933 @item
23934 Execution of the instrumented program
23935 @item
23936 Execution of the @code{gcov} tool to generate the result.
23937 @end itemize
23938
23939 The code instrumentation needed by gcov is created at the object level:
23940 The source code is not modified in any way, because the instrumentation code is
23941 inserted by gcc during the compilation process. To compile your code with code
23942 coverage activated, you need to recompile your whole project using the
23943 switches
23944 @code{-fprofile-arcs} and @code{-ftest-coverage}, and link it using
23945 @code{-fprofile-arcs}.
23946
23947 @smallexample
23948 $ gnatmake -P my_project.gpr -f -cargs -fprofile-arcs -ftest-coverage \
23949 -largs -fprofile-arcs
23950 @end smallexample
23951
23952 This compilation process will create @file{.gcno} files together with
23953 the usual object files.
23954
23955 Once the program is compiled with coverage instrumentation, you can
23956 run it as many times as needed - on portions of a test suite for
23957 example. The first execution will produce @file{.gcda} files at the
23958 same location as the @file{.gcno} files. The following executions
23959 will update those files, so that a cumulative result of the covered
23960 portions of the program is generated.
23961
23962 Finally, you need to call the @code{gcov} tool. The different options of
23963 @code{gcov} are available in the GCC User's Guide, section 'Invoking gcov'.
23964
23965 This will create annotated source files with a @file{.gcov} extension:
23966 @file{my_main.adb} file will be analysed in @file{my_main.adb.gcov}.
23967
23968 @node Gnat specifics
23969 @subsection Gnat specifics
23970
23971 Because Ada semantics, portions of the source code may be shared among
23972 several object files. This is the case for example when generics are
23973 involved, when inlining is active or when declarations generate initialisation
23974 calls. In order to take
23975 into account this shared code, you need to call @code{gcov} on all
23976 source files of the tested program at once.
23977
23978 The list of source files might exceed the system's maximum command line
23979 length. In order to bypass this limitation, a new mechanism has been
23980 implemented in @code{gcov}: you can now list all your project's files into a
23981 text file, and provide this file to gcov as a parameter, preceded by a @@
23982 (e.g. @samp{gcov @@mysrclist.txt}).
23983
23984 Note that on AIX compiling a static library with @code{-fprofile-arcs} is
23985 not supported as there can be unresolved symbols during the final link.
23986
23987 @node Profiling an Ada Program using gprof
23988 @section Profiling an Ada Program using gprof
23989 @cindex gprof
23990 @cindex -pg
23991 @cindex Profiling
23992
23993 @noindent
23994 This section is not meant to be an exhaustive documentation of @code{gprof}.
23995 Full documentation for it can be found in the GNU Profiler User's Guide
23996 documentation that is part of this GNAT distribution.
23997
23998 Profiling a program helps determine the parts of a program that are executed
23999 most often, and are therefore the most time-consuming.
24000
24001 @code{gprof} is the standard GNU profiling tool; it has been enhanced to
24002 better handle Ada programs and multitasking.
24003 It is currently supported on the following platforms
24004 @itemize @bullet
24005 @item
24006 linux x86/x86_64
24007 @item
24008 solaris sparc/sparc64/x86
24009 @item
24010 windows x86
24011 @end itemize
24012
24013 @noindent
24014 In order to profile a program using @code{gprof}, 3 steps are needed:
24015
24016 @itemize @bullet
24017 @item
24018 Code instrumentation, requiring a full recompilation of the project with the
24019 proper switches.
24020 @item
24021 Execution of the program under the analysis conditions, i.e. with the desired
24022 input.
24023 @item
24024 Analysis of the results using the @code{gprof} tool.
24025 @end itemize
24026
24027 @noindent
24028 The following sections detail the different steps, and indicate how
24029 to interpret the results:
24030 @menu
24031 * Compilation for profiling::
24032 * Program execution::
24033 * Running gprof::
24034 * Interpretation of profiling results::
24035 @end menu
24036
24037 @node Compilation for profiling
24038 @subsection Compilation for profiling
24039 @cindex -pg
24040 @cindex Profiling
24041
24042 In order to profile a program the first step is to tell the compiler
24043 to generate the necessary profiling information. The compiler switch to be used
24044 is @code{-pg}, which must be added to other compilation switches. This
24045 switch needs to be specified both during compilation and link stages, and can
24046 be specified once when using gnatmake:
24047
24048 @smallexample
24049 gnatmake -f -pg -P my_project
24050 @end smallexample
24051
24052 @noindent
24053 Note that only the objects that were compiled with the @samp{-pg} switch will be
24054 profiled; if you need to profile your whole project, use the
24055 @samp{-f} gnatmake switch to force full recompilation.
24056
24057 @node Program execution
24058 @subsection Program execution
24059
24060 @noindent
24061 Once the program has been compiled for profiling, you can run it as usual.
24062
24063 The only constraint imposed by profiling is that the program must terminate
24064 normally. An interrupted program (via a Ctrl-C, kill, etc.) will not be
24065 properly analyzed.
24066
24067 Once the program completes execution, a data file called @file{gmon.out} is
24068 generated in the directory where the program was launched from. If this file
24069 already exists, it will be overwritten.
24070
24071 @node Running gprof
24072 @subsection Running gprof
24073
24074 @noindent
24075 The @code{gprof} tool is called as follow:
24076
24077 @smallexample
24078 gprof my_prog gmon.out
24079 @end smallexample
24080
24081 @noindent
24082 or simpler:
24083
24084 @smallexample
24085 gprof my_prog
24086 @end smallexample
24087
24088 @noindent
24089 The complete form of the gprof command line is the following:
24090
24091 @smallexample
24092 gprof [^switches^options^] [executable [data-file]]
24093 @end smallexample
24094
24095 @noindent
24096 @code{gprof} supports numerous ^switch^options^. The order of these
24097 ^switch^options^ does not matter. The full list of options can be found in
24098 the GNU Profiler User's Guide documentation that comes with this documentation.
24099
24100 The following is the subset of those switches that is most relevant:
24101
24102 @table @option
24103
24104 @item --demangle[=@var{style}]
24105 @itemx --no-demangle
24106 @cindex @option{--demangle} (@code{gprof})
24107 These options control whether symbol names should be demangled when
24108 printing output. The default is to demangle C++ symbols. The
24109 @code{--no-demangle} option may be used to turn off demangling. Different
24110 compilers have different mangling styles. The optional demangling style
24111 argument can be used to choose an appropriate demangling style for your
24112 compiler, in particular Ada symbols generated by GNAT can be demangled using
24113 @code{--demangle=gnat}.
24114
24115 @item -e @var{function_name}
24116 @cindex @option{-e} (@code{gprof})
24117 The @samp{-e @var{function}} option tells @code{gprof} not to print
24118 information about the function @var{function_name} (and its
24119 children@dots{}) in the call graph. The function will still be listed
24120 as a child of any functions that call it, but its index number will be
24121 shown as @samp{[not printed]}. More than one @samp{-e} option may be
24122 given; only one @var{function_name} may be indicated with each @samp{-e}
24123 option.
24124
24125 @item -E @var{function_name}
24126 @cindex @option{-E} (@code{gprof})
24127 The @code{-E @var{function}} option works like the @code{-e} option, but
24128 execution time spent in the function (and children who were not called from
24129 anywhere else), will not be used to compute the percentages-of-time for
24130 the call graph. More than one @samp{-E} option may be given; only one
24131 @var{function_name} may be indicated with each @samp{-E} option.
24132
24133 @item -f @var{function_name}
24134 @cindex @option{-f} (@code{gprof})
24135 The @samp{-f @var{function}} option causes @code{gprof} to limit the
24136 call graph to the function @var{function_name} and its children (and
24137 their children@dots{}). More than one @samp{-f} option may be given;
24138 only one @var{function_name} may be indicated with each @samp{-f}
24139 option.
24140
24141 @item -F @var{function_name}
24142 @cindex @option{-F} (@code{gprof})
24143 The @samp{-F @var{function}} option works like the @code{-f} option, but
24144 only time spent in the function and its children (and their
24145 children@dots{}) will be used to determine total-time and
24146 percentages-of-time for the call graph. More than one @samp{-F} option
24147 may be given; only one @var{function_name} may be indicated with each
24148 @samp{-F} option. The @samp{-F} option overrides the @samp{-E} option.
24149
24150 @end table
24151
24152 @node Interpretation of profiling results
24153 @subsection Interpretation of profiling results
24154
24155 @noindent
24156
24157 The results of the profiling analysis are represented by two arrays: the
24158 'flat profile' and the 'call graph'. Full documentation of those outputs
24159 can be found in the GNU Profiler User's Guide.
24160
24161 The flat profile shows the time spent in each function of the program, and how
24162 many time it has been called. This allows you to locate easily the most
24163 time-consuming functions.
24164
24165 The call graph shows, for each subprogram, the subprograms that call it,
24166 and the subprograms that it calls. It also provides an estimate of the time
24167 spent in each of those callers/called subprograms.
24168 @end ifclear
24169
24170 @c ******************************
24171 @node Running and Debugging Ada Programs
24172 @chapter Running and Debugging Ada Programs
24173 @cindex Debugging
24174
24175 @noindent
24176 This chapter discusses how to debug Ada programs.
24177 @ifset vms
24178 It applies to GNAT on the Alpha OpenVMS platform;
24179 for I64 OpenVMS please refer to the @cite{OpenVMS Debugger Manual},
24180 since HP has implemented Ada support in the OpenVMS debugger on I64.
24181 @end ifset
24182
24183 An incorrect Ada program may be handled in three ways by the GNAT compiler:
24184
24185 @enumerate
24186 @item
24187 The illegality may be a violation of the static semantics of Ada. In
24188 that case GNAT diagnoses the constructs in the program that are illegal.
24189 It is then a straightforward matter for the user to modify those parts of
24190 the program.
24191
24192 @item
24193 The illegality may be a violation of the dynamic semantics of Ada. In
24194 that case the program compiles and executes, but may generate incorrect
24195 results, or may terminate abnormally with some exception.
24196
24197 @item
24198 When presented with a program that contains convoluted errors, GNAT
24199 itself may terminate abnormally without providing full diagnostics on
24200 the incorrect user program.
24201 @end enumerate
24202
24203 @menu
24204 * The GNAT Debugger GDB::
24205 * Running GDB::
24206 * Introduction to GDB Commands::
24207 * Using Ada Expressions::
24208 * Calling User-Defined Subprograms::
24209 * Using the Next Command in a Function::
24210 * Ada Exceptions::
24211 * Ada Tasks::
24212 * Debugging Generic Units::
24213 * GNAT Abnormal Termination or Failure to Terminate::
24214 * Naming Conventions for GNAT Source Files::
24215 * Getting Internal Debugging Information::
24216 * Stack Traceback::
24217 @end menu
24218
24219 @cindex Debugger
24220 @findex gdb
24221
24222 @node The GNAT Debugger GDB
24223 @section The GNAT Debugger GDB
24224
24225 @noindent
24226 @code{GDB} is a general purpose, platform-independent debugger that
24227 can be used to debug mixed-language programs compiled with @command{gcc},
24228 and in particular is capable of debugging Ada programs compiled with
24229 GNAT. The latest versions of @code{GDB} are Ada-aware and can handle
24230 complex Ada data structures.
24231
24232 @xref{Top,, Debugging with GDB, gdb, Debugging with GDB},
24233 @ifset vms
24234 located in the GNU:[DOCS] directory,
24235 @end ifset
24236 for full details on the usage of @code{GDB}, including a section on
24237 its usage on programs. This manual should be consulted for full
24238 details. The section that follows is a brief introduction to the
24239 philosophy and use of @code{GDB}.
24240
24241 When GNAT programs are compiled, the compiler optionally writes debugging
24242 information into the generated object file, including information on
24243 line numbers, and on declared types and variables. This information is
24244 separate from the generated code. It makes the object files considerably
24245 larger, but it does not add to the size of the actual executable that
24246 will be loaded into memory, and has no impact on run-time performance. The
24247 generation of debug information is triggered by the use of the
24248 ^-g^/DEBUG^ switch in the @command{gcc} or @command{gnatmake} command
24249 used to carry out the compilations. It is important to emphasize that
24250 the use of these options does not change the generated code.
24251
24252 The debugging information is written in standard system formats that
24253 are used by many tools, including debuggers and profilers. The format
24254 of the information is typically designed to describe C types and
24255 semantics, but GNAT implements a translation scheme which allows full
24256 details about Ada types and variables to be encoded into these
24257 standard C formats. Details of this encoding scheme may be found in
24258 the file exp_dbug.ads in the GNAT source distribution. However, the
24259 details of this encoding are, in general, of no interest to a user,
24260 since @code{GDB} automatically performs the necessary decoding.
24261
24262 When a program is bound and linked, the debugging information is
24263 collected from the object files, and stored in the executable image of
24264 the program. Again, this process significantly increases the size of
24265 the generated executable file, but it does not increase the size of
24266 the executable program itself. Furthermore, if this program is run in
24267 the normal manner, it runs exactly as if the debug information were
24268 not present, and takes no more actual memory.
24269
24270 However, if the program is run under control of @code{GDB}, the
24271 debugger is activated. The image of the program is loaded, at which
24272 point it is ready to run. If a run command is given, then the program
24273 will run exactly as it would have if @code{GDB} were not present. This
24274 is a crucial part of the @code{GDB} design philosophy. @code{GDB} is
24275 entirely non-intrusive until a breakpoint is encountered. If no
24276 breakpoint is ever hit, the program will run exactly as it would if no
24277 debugger were present. When a breakpoint is hit, @code{GDB} accesses
24278 the debugging information and can respond to user commands to inspect
24279 variables, and more generally to report on the state of execution.
24280
24281 @c **************
24282 @node Running GDB
24283 @section Running GDB
24284
24285 @noindent
24286 This section describes how to initiate the debugger.
24287 @c The above sentence is really just filler, but it was otherwise
24288 @c clumsy to get the first paragraph nonindented given the conditional
24289 @c nature of the description
24290
24291 @ifclear vms
24292 The debugger can be launched from a @code{GPS} menu or
24293 directly from the command line. The description below covers the latter use.
24294 All the commands shown can be used in the @code{GPS} debug console window,
24295 but there are usually more GUI-based ways to achieve the same effect.
24296 @end ifclear
24297
24298 The command to run @code{GDB} is
24299
24300 @smallexample
24301 $ ^gdb program^GDB PROGRAM^
24302 @end smallexample
24303
24304 @noindent
24305 where @code{^program^PROGRAM^} is the name of the executable file. This
24306 activates the debugger and results in a prompt for debugger commands.
24307 The simplest command is simply @code{run}, which causes the program to run
24308 exactly as if the debugger were not present. The following section
24309 describes some of the additional commands that can be given to @code{GDB}.
24310
24311 @c *******************************
24312 @node Introduction to GDB Commands
24313 @section Introduction to GDB Commands
24314
24315 @noindent
24316 @code{GDB} contains a large repertoire of commands. @xref{Top,,
24317 Debugging with GDB, gdb, Debugging with GDB},
24318 @ifset vms
24319 located in the GNU:[DOCS] directory,
24320 @end ifset
24321 for extensive documentation on the use
24322 of these commands, together with examples of their use. Furthermore,
24323 the command @command{help} invoked from within GDB activates a simple help
24324 facility which summarizes the available commands and their options.
24325 In this section we summarize a few of the most commonly
24326 used commands to give an idea of what @code{GDB} is about. You should create
24327 a simple program with debugging information and experiment with the use of
24328 these @code{GDB} commands on the program as you read through the
24329 following section.
24330
24331 @table @code
24332 @item set args @var{arguments}
24333 The @var{arguments} list above is a list of arguments to be passed to
24334 the program on a subsequent run command, just as though the arguments
24335 had been entered on a normal invocation of the program. The @code{set args}
24336 command is not needed if the program does not require arguments.
24337
24338 @item run
24339 The @code{run} command causes execution of the program to start from
24340 the beginning. If the program is already running, that is to say if
24341 you are currently positioned at a breakpoint, then a prompt will ask
24342 for confirmation that you want to abandon the current execution and
24343 restart.
24344
24345 @item breakpoint @var{location}
24346 The breakpoint command sets a breakpoint, that is to say a point at which
24347 execution will halt and @code{GDB} will await further
24348 commands. @var{location} is
24349 either a line number within a file, given in the format @code{file:linenumber},
24350 or it is the name of a subprogram. If you request that a breakpoint be set on
24351 a subprogram that is overloaded, a prompt will ask you to specify on which of
24352 those subprograms you want to breakpoint. You can also
24353 specify that all of them should be breakpointed. If the program is run
24354 and execution encounters the breakpoint, then the program
24355 stops and @code{GDB} signals that the breakpoint was encountered by
24356 printing the line of code before which the program is halted.
24357
24358 @item breakpoint exception @var{name}
24359 A special form of the breakpoint command which breakpoints whenever
24360 exception @var{name} is raised.
24361 If @var{name} is omitted,
24362 then a breakpoint will occur when any exception is raised.
24363
24364 @item print @var{expression}
24365 This will print the value of the given expression. Most simple
24366 Ada expression formats are properly handled by @code{GDB}, so the expression
24367 can contain function calls, variables, operators, and attribute references.
24368
24369 @item continue
24370 Continues execution following a breakpoint, until the next breakpoint or the
24371 termination of the program.
24372
24373 @item step
24374 Executes a single line after a breakpoint. If the next statement
24375 is a subprogram call, execution continues into (the first statement of)
24376 the called subprogram.
24377
24378 @item next
24379 Executes a single line. If this line is a subprogram call, executes and
24380 returns from the call.
24381
24382 @item list
24383 Lists a few lines around the current source location. In practice, it
24384 is usually more convenient to have a separate edit window open with the
24385 relevant source file displayed. Successive applications of this command
24386 print subsequent lines. The command can be given an argument which is a
24387 line number, in which case it displays a few lines around the specified one.
24388
24389 @item backtrace
24390 Displays a backtrace of the call chain. This command is typically
24391 used after a breakpoint has occurred, to examine the sequence of calls that
24392 leads to the current breakpoint. The display includes one line for each
24393 activation record (frame) corresponding to an active subprogram.
24394
24395 @item up
24396 At a breakpoint, @code{GDB} can display the values of variables local
24397 to the current frame. The command @code{up} can be used to
24398 examine the contents of other active frames, by moving the focus up
24399 the stack, that is to say from callee to caller, one frame at a time.
24400
24401 @item down
24402 Moves the focus of @code{GDB} down from the frame currently being
24403 examined to the frame of its callee (the reverse of the previous command),
24404
24405 @item frame @var{n}
24406 Inspect the frame with the given number. The value 0 denotes the frame
24407 of the current breakpoint, that is to say the top of the call stack.
24408
24409 @end table
24410
24411 @noindent
24412 The above list is a very short introduction to the commands that
24413 @code{GDB} provides. Important additional capabilities, including conditional
24414 breakpoints, the ability to execute command sequences on a breakpoint,
24415 the ability to debug at the machine instruction level and many other
24416 features are described in detail in @ref{Top,, Debugging with GDB, gdb,
24417 Debugging with GDB}. Note that most commands can be abbreviated
24418 (for example, c for continue, bt for backtrace).
24419
24420 @node Using Ada Expressions
24421 @section Using Ada Expressions
24422 @cindex Ada expressions
24423
24424 @noindent
24425 @code{GDB} supports a fairly large subset of Ada expression syntax, with some
24426 extensions. The philosophy behind the design of this subset is
24427
24428 @itemize @bullet
24429 @item
24430 That @code{GDB} should provide basic literals and access to operations for
24431 arithmetic, dereferencing, field selection, indexing, and subprogram calls,
24432 leaving more sophisticated computations to subprograms written into the
24433 program (which therefore may be called from @code{GDB}).
24434
24435 @item
24436 That type safety and strict adherence to Ada language restrictions
24437 are not particularly important to the @code{GDB} user.
24438
24439 @item
24440 That brevity is important to the @code{GDB} user.
24441 @end itemize
24442
24443 @noindent
24444 Thus, for brevity, the debugger acts as if there were
24445 implicit @code{with} and @code{use} clauses in effect for all user-written
24446 packages, thus making it unnecessary to fully qualify most names with
24447 their packages, regardless of context. Where this causes ambiguity,
24448 @code{GDB} asks the user's intent.
24449
24450 For details on the supported Ada syntax, see @ref{Top,, Debugging with
24451 GDB, gdb, Debugging with GDB}.
24452
24453 @node Calling User-Defined Subprograms
24454 @section Calling User-Defined Subprograms
24455
24456 @noindent
24457 An important capability of @code{GDB} is the ability to call user-defined
24458 subprograms while debugging. This is achieved simply by entering
24459 a subprogram call statement in the form:
24460
24461 @smallexample
24462 call subprogram-name (parameters)
24463 @end smallexample
24464
24465 @noindent
24466 The keyword @code{call} can be omitted in the normal case where the
24467 @code{subprogram-name} does not coincide with any of the predefined
24468 @code{GDB} commands.
24469
24470 The effect is to invoke the given subprogram, passing it the
24471 list of parameters that is supplied. The parameters can be expressions and
24472 can include variables from the program being debugged. The
24473 subprogram must be defined
24474 at the library level within your program, and @code{GDB} will call the
24475 subprogram within the environment of your program execution (which
24476 means that the subprogram is free to access or even modify variables
24477 within your program).
24478
24479 The most important use of this facility is in allowing the inclusion of
24480 debugging routines that are tailored to particular data structures
24481 in your program. Such debugging routines can be written to provide a suitably
24482 high-level description of an abstract type, rather than a low-level dump
24483 of its physical layout. After all, the standard
24484 @code{GDB print} command only knows the physical layout of your
24485 types, not their abstract meaning. Debugging routines can provide information
24486 at the desired semantic level and are thus enormously useful.
24487
24488 For example, when debugging GNAT itself, it is crucial to have access to
24489 the contents of the tree nodes used to represent the program internally.
24490 But tree nodes are represented simply by an integer value (which in turn
24491 is an index into a table of nodes).
24492 Using the @code{print} command on a tree node would simply print this integer
24493 value, which is not very useful. But the PN routine (defined in file
24494 treepr.adb in the GNAT sources) takes a tree node as input, and displays
24495 a useful high level representation of the tree node, which includes the
24496 syntactic category of the node, its position in the source, the integers
24497 that denote descendant nodes and parent node, as well as varied
24498 semantic information. To study this example in more detail, you might want to
24499 look at the body of the PN procedure in the stated file.
24500
24501 @node Using the Next Command in a Function
24502 @section Using the Next Command in a Function
24503
24504 @noindent
24505 When you use the @code{next} command in a function, the current source
24506 location will advance to the next statement as usual. A special case
24507 arises in the case of a @code{return} statement.
24508
24509 Part of the code for a return statement is the ``epilog'' of the function.
24510 This is the code that returns to the caller. There is only one copy of
24511 this epilog code, and it is typically associated with the last return
24512 statement in the function if there is more than one return. In some
24513 implementations, this epilog is associated with the first statement
24514 of the function.
24515
24516 The result is that if you use the @code{next} command from a return
24517 statement that is not the last return statement of the function you
24518 may see a strange apparent jump to the last return statement or to
24519 the start of the function. You should simply ignore this odd jump.
24520 The value returned is always that from the first return statement
24521 that was stepped through.
24522
24523 @node Ada Exceptions
24524 @section Breaking on Ada Exceptions
24525 @cindex Exceptions
24526
24527 @noindent
24528 You can set breakpoints that trip when your program raises
24529 selected exceptions.
24530
24531 @table @code
24532 @item break exception
24533 Set a breakpoint that trips whenever (any task in the) program raises
24534 any exception.
24535
24536 @item break exception @var{name}
24537 Set a breakpoint that trips whenever (any task in the) program raises
24538 the exception @var{name}.
24539
24540 @item break exception unhandled
24541 Set a breakpoint that trips whenever (any task in the) program raises an
24542 exception for which there is no handler.
24543
24544 @item info exceptions
24545 @itemx info exceptions @var{regexp}
24546 The @code{info exceptions} command permits the user to examine all defined
24547 exceptions within Ada programs. With a regular expression, @var{regexp}, as
24548 argument, prints out only those exceptions whose name matches @var{regexp}.
24549 @end table
24550
24551 @node Ada Tasks
24552 @section Ada Tasks
24553 @cindex Tasks
24554
24555 @noindent
24556 @code{GDB} allows the following task-related commands:
24557
24558 @table @code
24559 @item info tasks
24560 This command shows a list of current Ada tasks, as in the following example:
24561
24562 @smallexample
24563 @iftex
24564 @leftskip=0cm
24565 @end iftex
24566 (gdb) info tasks
24567 ID TID P-ID Thread Pri State Name
24568 1 8088000 0 807e000 15 Child Activation Wait main_task
24569 2 80a4000 1 80ae000 15 Accept/Select Wait b
24570 3 809a800 1 80a4800 15 Child Activation Wait a
24571 * 4 80ae800 3 80b8000 15 Running c
24572 @end smallexample
24573
24574 @noindent
24575 In this listing, the asterisk before the first task indicates it to be the
24576 currently running task. The first column lists the task ID that is used
24577 to refer to tasks in the following commands.
24578
24579 @item break @var{linespec} task @var{taskid}
24580 @itemx break @var{linespec} task @var{taskid} if @dots{}
24581 @cindex Breakpoints and tasks
24582 These commands are like the @code{break @dots{} thread @dots{}}.
24583 @var{linespec} specifies source lines.
24584
24585 Use the qualifier @samp{task @var{taskid}} with a breakpoint command
24586 to specify that you only want @code{GDB} to stop the program when a
24587 particular Ada task reaches this breakpoint. @var{taskid} is one of the
24588 numeric task identifiers assigned by @code{GDB}, shown in the first
24589 column of the @samp{info tasks} display.
24590
24591 If you do not specify @samp{task @var{taskid}} when you set a
24592 breakpoint, the breakpoint applies to @emph{all} tasks of your
24593 program.
24594
24595 You can use the @code{task} qualifier on conditional breakpoints as
24596 well; in this case, place @samp{task @var{taskid}} before the
24597 breakpoint condition (before the @code{if}).
24598
24599 @item task @var{taskno}
24600 @cindex Task switching
24601
24602 This command allows to switch to the task referred by @var{taskno}. In
24603 particular, This allows to browse the backtrace of the specified
24604 task. It is advised to switch back to the original task before
24605 continuing execution otherwise the scheduling of the program may be
24606 perturbed.
24607 @end table
24608
24609 @noindent
24610 For more detailed information on the tasking support,
24611 see @ref{Top,, Debugging with GDB, gdb, Debugging with GDB}.
24612
24613 @node Debugging Generic Units
24614 @section Debugging Generic Units
24615 @cindex Debugging Generic Units
24616 @cindex Generics
24617
24618 @noindent
24619 GNAT always uses code expansion for generic instantiation. This means that
24620 each time an instantiation occurs, a complete copy of the original code is
24621 made, with appropriate substitutions of formals by actuals.
24622
24623 It is not possible to refer to the original generic entities in
24624 @code{GDB}, but it is always possible to debug a particular instance of
24625 a generic, by using the appropriate expanded names. For example, if we have
24626
24627 @smallexample @c ada
24628 @group
24629 @cartouche
24630 procedure g is
24631
24632 generic package k is
24633 procedure kp (v1 : in out integer);
24634 end k;
24635
24636 package body k is
24637 procedure kp (v1 : in out integer) is
24638 begin
24639 v1 := v1 + 1;
24640 end kp;
24641 end k;
24642
24643 package k1 is new k;
24644 package k2 is new k;
24645
24646 var : integer := 1;
24647
24648 begin
24649 k1.kp (var);
24650 k2.kp (var);
24651 k1.kp (var);
24652 k2.kp (var);
24653 end;
24654 @end cartouche
24655 @end group
24656 @end smallexample
24657
24658 @noindent
24659 Then to break on a call to procedure kp in the k2 instance, simply
24660 use the command:
24661
24662 @smallexample
24663 (gdb) break g.k2.kp
24664 @end smallexample
24665
24666 @noindent
24667 When the breakpoint occurs, you can step through the code of the
24668 instance in the normal manner and examine the values of local variables, as for
24669 other units.
24670
24671 @node GNAT Abnormal Termination or Failure to Terminate
24672 @section GNAT Abnormal Termination or Failure to Terminate
24673 @cindex GNAT Abnormal Termination or Failure to Terminate
24674
24675 @noindent
24676 When presented with programs that contain serious errors in syntax
24677 or semantics,
24678 GNAT may on rare occasions experience problems in operation, such
24679 as aborting with a
24680 segmentation fault or illegal memory access, raising an internal
24681 exception, terminating abnormally, or failing to terminate at all.
24682 In such cases, you can activate
24683 various features of GNAT that can help you pinpoint the construct in your
24684 program that is the likely source of the problem.
24685
24686 The following strategies are presented in increasing order of
24687 difficulty, corresponding to your experience in using GNAT and your
24688 familiarity with compiler internals.
24689
24690 @enumerate
24691 @item
24692 Run @command{gcc} with the @option{-gnatf}. This first
24693 switch causes all errors on a given line to be reported. In its absence,
24694 only the first error on a line is displayed.
24695
24696 The @option{-gnatdO} switch causes errors to be displayed as soon as they
24697 are encountered, rather than after compilation is terminated. If GNAT
24698 terminates prematurely or goes into an infinite loop, the last error
24699 message displayed may help to pinpoint the culprit.
24700
24701 @item
24702 Run @command{gcc} with the @option{^-v (verbose)^/VERBOSE^} switch. In this
24703 mode, @command{gcc} produces ongoing information about the progress of the
24704 compilation and provides the name of each procedure as code is
24705 generated. This switch allows you to find which Ada procedure was being
24706 compiled when it encountered a code generation problem.
24707
24708 @item
24709 @cindex @option{-gnatdc} switch
24710 Run @command{gcc} with the @option{-gnatdc} switch. This is a GNAT specific
24711 switch that does for the front-end what @option{^-v^VERBOSE^} does
24712 for the back end. The system prints the name of each unit,
24713 either a compilation unit or nested unit, as it is being analyzed.
24714 @item
24715 Finally, you can start
24716 @code{gdb} directly on the @code{gnat1} executable. @code{gnat1} is the
24717 front-end of GNAT, and can be run independently (normally it is just
24718 called from @command{gcc}). You can use @code{gdb} on @code{gnat1} as you
24719 would on a C program (but @pxref{The GNAT Debugger GDB} for caveats). The
24720 @code{where} command is the first line of attack; the variable
24721 @code{lineno} (seen by @code{print lineno}), used by the second phase of
24722 @code{gnat1} and by the @command{gcc} backend, indicates the source line at
24723 which the execution stopped, and @code{input_file name} indicates the name of
24724 the source file.
24725 @end enumerate
24726
24727 @node Naming Conventions for GNAT Source Files
24728 @section Naming Conventions for GNAT Source Files
24729
24730 @noindent
24731 In order to examine the workings of the GNAT system, the following
24732 brief description of its organization may be helpful:
24733
24734 @itemize @bullet
24735 @item
24736 Files with prefix @file{^sc^SC^} contain the lexical scanner.
24737
24738 @item
24739 All files prefixed with @file{^par^PAR^} are components of the parser. The
24740 numbers correspond to chapters of the Ada Reference Manual. For example,
24741 parsing of select statements can be found in @file{par-ch9.adb}.
24742
24743 @item
24744 All files prefixed with @file{^sem^SEM^} perform semantic analysis. The
24745 numbers correspond to chapters of the Ada standard. For example, all
24746 issues involving context clauses can be found in @file{sem_ch10.adb}. In
24747 addition, some features of the language require sufficient special processing
24748 to justify their own semantic files: sem_aggr for aggregates, sem_disp for
24749 dynamic dispatching, etc.
24750
24751 @item
24752 All files prefixed with @file{^exp^EXP^} perform normalization and
24753 expansion of the intermediate representation (abstract syntax tree, or AST).
24754 these files use the same numbering scheme as the parser and semantics files.
24755 For example, the construction of record initialization procedures is done in
24756 @file{exp_ch3.adb}.
24757
24758 @item
24759 The files prefixed with @file{^bind^BIND^} implement the binder, which
24760 verifies the consistency of the compilation, determines an order of
24761 elaboration, and generates the bind file.
24762
24763 @item
24764 The files @file{atree.ads} and @file{atree.adb} detail the low-level
24765 data structures used by the front-end.
24766
24767 @item
24768 The files @file{sinfo.ads} and @file{sinfo.adb} detail the structure of
24769 the abstract syntax tree as produced by the parser.
24770
24771 @item
24772 The files @file{einfo.ads} and @file{einfo.adb} detail the attributes of
24773 all entities, computed during semantic analysis.
24774
24775 @item
24776 Library management issues are dealt with in files with prefix
24777 @file{^lib^LIB^}.
24778
24779 @item
24780 @findex Ada
24781 @cindex Annex A
24782 Ada files with the prefix @file{^a-^A-^} are children of @code{Ada}, as
24783 defined in Annex A.
24784
24785 @item
24786 @findex Interfaces
24787 @cindex Annex B
24788 Files with prefix @file{^i-^I-^} are children of @code{Interfaces}, as
24789 defined in Annex B.
24790
24791 @item
24792 @findex System
24793 Files with prefix @file{^s-^S-^} are children of @code{System}. This includes
24794 both language-defined children and GNAT run-time routines.
24795
24796 @item
24797 @findex GNAT
24798 Files with prefix @file{^g-^G-^} are children of @code{GNAT}. These are useful
24799 general-purpose packages, fully documented in their specs. All
24800 the other @file{.c} files are modifications of common @command{gcc} files.
24801 @end itemize
24802
24803 @node Getting Internal Debugging Information
24804 @section Getting Internal Debugging Information
24805
24806 @noindent
24807 Most compilers have internal debugging switches and modes. GNAT
24808 does also, except GNAT internal debugging switches and modes are not
24809 secret. A summary and full description of all the compiler and binder
24810 debug flags are in the file @file{debug.adb}. You must obtain the
24811 sources of the compiler to see the full detailed effects of these flags.
24812
24813 The switches that print the source of the program (reconstructed from
24814 the internal tree) are of general interest for user programs, as are the
24815 options to print
24816 the full internal tree, and the entity table (the symbol table
24817 information). The reconstructed source provides a readable version of the
24818 program after the front-end has completed analysis and expansion,
24819 and is useful when studying the performance of specific constructs.
24820 For example, constraint checks are indicated, complex aggregates
24821 are replaced with loops and assignments, and tasking primitives
24822 are replaced with run-time calls.
24823
24824 @node Stack Traceback
24825 @section Stack Traceback
24826 @cindex traceback
24827 @cindex stack traceback
24828 @cindex stack unwinding
24829
24830 @noindent
24831 Traceback is a mechanism to display the sequence of subprogram calls that
24832 leads to a specified execution point in a program. Often (but not always)
24833 the execution point is an instruction at which an exception has been raised.
24834 This mechanism is also known as @i{stack unwinding} because it obtains
24835 its information by scanning the run-time stack and recovering the activation
24836 records of all active subprograms. Stack unwinding is one of the most
24837 important tools for program debugging.
24838
24839 The first entry stored in traceback corresponds to the deepest calling level,
24840 that is to say the subprogram currently executing the instruction
24841 from which we want to obtain the traceback.
24842
24843 Note that there is no runtime performance penalty when stack traceback
24844 is enabled, and no exception is raised during program execution.
24845
24846 @menu
24847 * Non-Symbolic Traceback::
24848 * Symbolic Traceback::
24849 @end menu
24850
24851 @node Non-Symbolic Traceback
24852 @subsection Non-Symbolic Traceback
24853 @cindex traceback, non-symbolic
24854
24855 @noindent
24856 Note: this feature is not supported on all platforms. See
24857 @file{GNAT.Traceback spec in g-traceb.ads} for a complete list of supported
24858 platforms.
24859
24860 @menu
24861 * Tracebacks From an Unhandled Exception::
24862 * Tracebacks From Exception Occurrences (non-symbolic)::
24863 * Tracebacks From Anywhere in a Program (non-symbolic)::
24864 @end menu
24865
24866 @node Tracebacks From an Unhandled Exception
24867 @subsubsection Tracebacks From an Unhandled Exception
24868
24869 @noindent
24870 A runtime non-symbolic traceback is a list of addresses of call instructions.
24871 To enable this feature you must use the @option{-E}
24872 @code{gnatbind}'s option. With this option a stack traceback is stored as part
24873 of exception information. You can retrieve this information using the
24874 @code{addr2line} tool.
24875
24876 Here is a simple example:
24877
24878 @smallexample @c ada
24879 @cartouche
24880 procedure STB is
24881
24882 procedure P1 is
24883 begin
24884 raise Constraint_Error;
24885 end P1;
24886
24887 procedure P2 is
24888 begin
24889 P1;
24890 end P2;
24891
24892 begin
24893 P2;
24894 end STB;
24895 @end cartouche
24896 @end smallexample
24897
24898 @smallexample
24899 $ gnatmake stb -bargs -E
24900 $ stb
24901
24902 Execution terminated by unhandled exception
24903 Exception name: CONSTRAINT_ERROR
24904 Message: stb.adb:5
24905 Call stack traceback locations:
24906 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
24907 @end smallexample
24908
24909 @noindent
24910 As we see the traceback lists a sequence of addresses for the unhandled
24911 exception @code{CONSTRAINT_ERROR} raised in procedure P1. It is easy to
24912 guess that this exception come from procedure P1. To translate these
24913 addresses into the source lines where the calls appear, the
24914 @code{addr2line} tool, described below, is invaluable. The use of this tool
24915 requires the program to be compiled with debug information.
24916
24917 @smallexample
24918 $ gnatmake -g stb -bargs -E
24919 $ stb
24920
24921 Execution terminated by unhandled exception
24922 Exception name: CONSTRAINT_ERROR
24923 Message: stb.adb:5
24924 Call stack traceback locations:
24925 0x401373 0x40138b 0x40139c 0x401335 0x4011c4 0x4011f1 0x77e892a4
24926
24927 $ addr2line --exe=stb 0x401373 0x40138b 0x40139c 0x401335 0x4011c4
24928 0x4011f1 0x77e892a4
24929
24930 00401373 at d:/stb/stb.adb:5
24931 0040138B at d:/stb/stb.adb:10
24932 0040139C at d:/stb/stb.adb:14
24933 00401335 at d:/stb/b~stb.adb:104
24934 004011C4 at /build/@dots{}/crt1.c:200
24935 004011F1 at /build/@dots{}/crt1.c:222
24936 77E892A4 in ?? at ??:0
24937 @end smallexample
24938
24939 @noindent
24940 The @code{addr2line} tool has several other useful options:
24941
24942 @table @code
24943 @item --functions
24944 to get the function name corresponding to any location
24945
24946 @item --demangle=gnat
24947 to use the gnat decoding mode for the function names. Note that
24948 for binutils version 2.9.x the option is simply @option{--demangle}.
24949 @end table
24950
24951 @smallexample
24952 $ addr2line --exe=stb --functions --demangle=gnat 0x401373 0x40138b
24953 0x40139c 0x401335 0x4011c4 0x4011f1
24954
24955 00401373 in stb.p1 at d:/stb/stb.adb:5
24956 0040138B in stb.p2 at d:/stb/stb.adb:10
24957 0040139C in stb at d:/stb/stb.adb:14
24958 00401335 in main at d:/stb/b~stb.adb:104
24959 004011C4 in <__mingw_CRTStartup> at /build/@dots{}/crt1.c:200
24960 004011F1 in <mainCRTStartup> at /build/@dots{}/crt1.c:222
24961 @end smallexample
24962
24963 @noindent
24964 From this traceback we can see that the exception was raised in
24965 @file{stb.adb} at line 5, which was reached from a procedure call in
24966 @file{stb.adb} at line 10, and so on. The @file{b~std.adb} is the binder file,
24967 which contains the call to the main program.
24968 @xref{Running gnatbind}. The remaining entries are assorted runtime routines,
24969 and the output will vary from platform to platform.
24970
24971 It is also possible to use @code{GDB} with these traceback addresses to debug
24972 the program. For example, we can break at a given code location, as reported
24973 in the stack traceback:
24974
24975 @smallexample
24976 $ gdb -nw stb
24977 @ifclear vms
24978 @noindent
24979 Furthermore, this feature is not implemented inside Windows DLL. Only
24980 the non-symbolic traceback is reported in this case.
24981 @end ifclear
24982
24983 (gdb) break *0x401373
24984 Breakpoint 1 at 0x401373: file stb.adb, line 5.
24985 @end smallexample
24986
24987 @noindent
24988 It is important to note that the stack traceback addresses
24989 do not change when debug information is included. This is particularly useful
24990 because it makes it possible to release software without debug information (to
24991 minimize object size), get a field report that includes a stack traceback
24992 whenever an internal bug occurs, and then be able to retrieve the sequence
24993 of calls with the same program compiled with debug information.
24994
24995 @node Tracebacks From Exception Occurrences (non-symbolic)
24996 @subsubsection Tracebacks From Exception Occurrences
24997
24998 @noindent
24999 Non-symbolic tracebacks are obtained by using the @option{-E} binder argument.
25000 The stack traceback is attached to the exception information string, and can
25001 be retrieved in an exception handler within the Ada program, by means of the
25002 Ada facilities defined in @code{Ada.Exceptions}. Here is a simple example:
25003
25004 @smallexample @c ada
25005 with Ada.Text_IO;
25006 with Ada.Exceptions;
25007
25008 procedure STB is
25009
25010 use Ada;
25011 use Ada.Exceptions;
25012
25013 procedure P1 is
25014 K : Positive := 1;
25015 begin
25016 K := K - 1;
25017 exception
25018 when E : others =>
25019 Text_IO.Put_Line (Exception_Information (E));
25020 end P1;
25021
25022 procedure P2 is
25023 begin
25024 P1;
25025 end P2;
25026
25027 begin
25028 P2;
25029 end STB;
25030 @end smallexample
25031
25032 @noindent
25033 This program will output:
25034
25035 @smallexample
25036 $ stb
25037
25038 Exception name: CONSTRAINT_ERROR
25039 Message: stb.adb:12
25040 Call stack traceback locations:
25041 0x4015e4 0x401633 0x401644 0x401461 0x4011c4 0x4011f1 0x77e892a4
25042 @end smallexample
25043
25044 @node Tracebacks From Anywhere in a Program (non-symbolic)
25045 @subsubsection Tracebacks From Anywhere in a Program
25046
25047 @noindent
25048 It is also possible to retrieve a stack traceback from anywhere in a
25049 program. For this you need to
25050 use the @code{GNAT.Traceback} API. This package includes a procedure called
25051 @code{Call_Chain} that computes a complete stack traceback, as well as useful
25052 display procedures described below. It is not necessary to use the
25053 @option{-E gnatbind} option in this case, because the stack traceback mechanism
25054 is invoked explicitly.
25055
25056 @noindent
25057 In the following example we compute a traceback at a specific location in
25058 the program, and we display it using @code{GNAT.Debug_Utilities.Image} to
25059 convert addresses to strings:
25060
25061 @smallexample @c ada
25062 with Ada.Text_IO;
25063 with GNAT.Traceback;
25064 with GNAT.Debug_Utilities;
25065
25066 procedure STB is
25067
25068 use Ada;
25069 use GNAT;
25070 use GNAT.Traceback;
25071
25072 procedure P1 is
25073 TB : Tracebacks_Array (1 .. 10);
25074 -- We are asking for a maximum of 10 stack frames.
25075 Len : Natural;
25076 -- Len will receive the actual number of stack frames returned.
25077 begin
25078 Call_Chain (TB, Len);
25079
25080 Text_IO.Put ("In STB.P1 : ");
25081
25082 for K in 1 .. Len loop
25083 Text_IO.Put (Debug_Utilities.Image (TB (K)));
25084 Text_IO.Put (' ');
25085 end loop;
25086
25087 Text_IO.New_Line;
25088 end P1;
25089
25090 procedure P2 is
25091 begin
25092 P1;
25093 end P2;
25094
25095 begin
25096 P2;
25097 end STB;
25098 @end smallexample
25099
25100 @smallexample
25101 $ gnatmake -g stb
25102 $ stb
25103
25104 In STB.P1 : 16#0040_F1E4# 16#0040_14F2# 16#0040_170B# 16#0040_171C#
25105 16#0040_1461# 16#0040_11C4# 16#0040_11F1# 16#77E8_92A4#
25106 @end smallexample
25107
25108 @noindent
25109 You can then get further information by invoking the @code{addr2line}
25110 tool as described earlier (note that the hexadecimal addresses
25111 need to be specified in C format, with a leading ``0x'').
25112
25113 @node Symbolic Traceback
25114 @subsection Symbolic Traceback
25115 @cindex traceback, symbolic
25116
25117 @noindent
25118 A symbolic traceback is a stack traceback in which procedure names are
25119 associated with each code location.
25120
25121 @noindent
25122 Note that this feature is not supported on all platforms. See
25123 @file{GNAT.Traceback.Symbolic spec in g-trasym.ads} for a complete
25124 list of currently supported platforms.
25125
25126 @noindent
25127 Note that the symbolic traceback requires that the program be compiled
25128 with debug information. If it is not compiled with debug information
25129 only the non-symbolic information will be valid.
25130
25131 @menu
25132 * Tracebacks From Exception Occurrences (symbolic)::
25133 * Tracebacks From Anywhere in a Program (symbolic)::
25134 @end menu
25135
25136 @node Tracebacks From Exception Occurrences (symbolic)
25137 @subsubsection Tracebacks From Exception Occurrences
25138
25139 @smallexample @c ada
25140 with Ada.Text_IO;
25141 with GNAT.Traceback.Symbolic;
25142
25143 procedure STB is
25144
25145 procedure P1 is
25146 begin
25147 raise Constraint_Error;
25148 end P1;
25149
25150 procedure P2 is
25151 begin
25152 P1;
25153 end P2;
25154
25155 procedure P3 is
25156 begin
25157 P2;
25158 end P3;
25159
25160 begin
25161 P3;
25162 exception
25163 when E : others =>
25164 Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
25165 end STB;
25166 @end smallexample
25167
25168 @smallexample
25169 $ gnatmake -g .\stb -bargs -E -largs -lgnat -laddr2line -lintl
25170 $ stb
25171
25172 0040149F in stb.p1 at stb.adb:8
25173 004014B7 in stb.p2 at stb.adb:13
25174 004014CF in stb.p3 at stb.adb:18
25175 004015DD in ada.stb at stb.adb:22
25176 00401461 in main at b~stb.adb:168
25177 004011C4 in __mingw_CRTStartup at crt1.c:200
25178 004011F1 in mainCRTStartup at crt1.c:222
25179 77E892A4 in ?? at ??:0
25180 @end smallexample
25181
25182 @noindent
25183 In the above example the ``.\'' syntax in the @command{gnatmake} command
25184 is currently required by @command{addr2line} for files that are in
25185 the current working directory.
25186 Moreover, the exact sequence of linker options may vary from platform
25187 to platform.
25188 The above @option{-largs} section is for Windows platforms. By contrast,
25189 under Unix there is no need for the @option{-largs} section.
25190 Differences across platforms are due to details of linker implementation.
25191
25192 @node Tracebacks From Anywhere in a Program (symbolic)
25193 @subsubsection Tracebacks From Anywhere in a Program
25194
25195 @noindent
25196 It is possible to get a symbolic stack traceback
25197 from anywhere in a program, just as for non-symbolic tracebacks.
25198 The first step is to obtain a non-symbolic
25199 traceback, and then call @code{Symbolic_Traceback} to compute the symbolic
25200 information. Here is an example:
25201
25202 @smallexample @c ada
25203 with Ada.Text_IO;
25204 with GNAT.Traceback;
25205 with GNAT.Traceback.Symbolic;
25206
25207 procedure STB is
25208
25209 use Ada;
25210 use GNAT.Traceback;
25211 use GNAT.Traceback.Symbolic;
25212
25213 procedure P1 is
25214 TB : Tracebacks_Array (1 .. 10);
25215 -- We are asking for a maximum of 10 stack frames.
25216 Len : Natural;
25217 -- Len will receive the actual number of stack frames returned.
25218 begin
25219 Call_Chain (TB, Len);
25220 Text_IO.Put_Line (Symbolic_Traceback (TB (1 .. Len)));
25221 end P1;
25222
25223 procedure P2 is
25224 begin
25225 P1;
25226 end P2;
25227
25228 begin
25229 P2;
25230 end STB;
25231 @end smallexample
25232
25233 @c ******************************
25234 @ifset vms
25235 @node Compatibility with HP Ada
25236 @chapter Compatibility with HP Ada
25237 @cindex Compatibility
25238
25239 @noindent
25240 @cindex DEC Ada
25241 @cindex HP Ada
25242 @cindex Compatibility between GNAT and HP Ada
25243 This chapter compares HP Ada (formerly known as ``DEC Ada'')
25244 for OpenVMS Alpha and GNAT for OpenVMS for Alpha and for I64.
25245 GNAT is highly compatible
25246 with HP Ada, and it should generally be straightforward to port code
25247 from the HP Ada environment to GNAT. However, there are a few language
25248 and implementation differences of which the user must be aware. These
25249 differences are discussed in this chapter. In
25250 addition, the operating environment and command structure for the
25251 compiler are different, and these differences are also discussed.
25252
25253 For further details on these and other compatibility issues,
25254 see Appendix E of the HP publication
25255 @cite{HP Ada, Technical Overview and Comparison on HP Platforms}.
25256
25257 Except where otherwise indicated, the description of GNAT for OpenVMS
25258 applies to both the Alpha and I64 platforms.
25259
25260 For information on porting Ada code from GNAT on Alpha OpenVMS to GNAT on
25261 I64 OpenVMS, see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
25262
25263 The discussion in this chapter addresses specifically the implementation
25264 of Ada 83 for HP OpenVMS Alpha Systems. In cases where the implementation
25265 of HP Ada differs between OpenVMS Alpha Systems and OpenVMS VAX Systems,
25266 GNAT always follows the Alpha implementation.
25267
25268 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
25269 attributes are recognized, although only a subset of them can sensibly
25270 be implemented. The description of pragmas in
25271 @xref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference Manual},
25272 indicates whether or not they are applicable to non-VMS systems.
25273
25274 @menu
25275 * Ada Language Compatibility::
25276 * Differences in the Definition of Package System::
25277 * Language-Related Features::
25278 * The Package STANDARD::
25279 * The Package SYSTEM::
25280 * Tasking and Task-Related Features::
25281 * Pragmas and Pragma-Related Features::
25282 * Library of Predefined Units::
25283 * Bindings::
25284 * Main Program Definition::
25285 * Implementation-Defined Attributes::
25286 * Compiler and Run-Time Interfacing::
25287 * Program Compilation and Library Management::
25288 * Input-Output::
25289 * Implementation Limits::
25290 * Tools and Utilities::
25291 @end menu
25292
25293 @node Ada Language Compatibility
25294 @section Ada Language Compatibility
25295
25296 @noindent
25297 GNAT handles Ada 95 and Ada 2005 as well as Ada 83, whereas HP Ada is only
25298 for Ada 83. Ada 95 and Ada 2005 are almost completely upwards compatible
25299 with Ada 83, and therefore Ada 83 programs will compile
25300 and run under GNAT with
25301 no changes or only minor changes. The @cite{Annotated Ada Reference Manual}
25302 provides details on specific incompatibilities.
25303
25304 GNAT provides the switch @option{/83} on the @command{GNAT COMPILE} command,
25305 as well as the pragma @code{ADA_83}, to force the compiler to
25306 operate in Ada 83 mode. This mode does not guarantee complete
25307 conformance to Ada 83, but in practice is sufficient to
25308 eliminate most sources of incompatibilities.
25309 In particular, it eliminates the recognition of the
25310 additional Ada 95 and Ada 2005 keywords, so that their use as identifiers
25311 in Ada 83 programs is legal, and handles the cases of packages
25312 with optional bodies, and generics that instantiate unconstrained
25313 types without the use of @code{(<>)}.
25314
25315 @node Differences in the Definition of Package System
25316 @section Differences in the Definition of Package @code{System}
25317
25318 @noindent
25319 An Ada compiler is allowed to add
25320 implementation-dependent declarations to package @code{System}.
25321 In normal mode,
25322 GNAT does not take advantage of this permission, and the version of
25323 @code{System} provided by GNAT exactly matches that defined in the Ada
25324 Reference Manual.
25325
25326 However, HP Ada adds an extensive set of declarations to package
25327 @code{System},
25328 as fully documented in the HP Ada manuals. To minimize changes required
25329 for programs that make use of these extensions, GNAT provides the pragma
25330 @code{Extend_System} for extending the definition of package System. By using:
25331 @cindex pragma @code{Extend_System}
25332 @cindex @code{Extend_System} pragma
25333
25334 @smallexample @c ada
25335 @group
25336 @cartouche
25337 pragma Extend_System (Aux_DEC);
25338 @end cartouche
25339 @end group
25340 @end smallexample
25341
25342 @noindent
25343 the set of definitions in @code{System} is extended to include those in
25344 package @code{System.Aux_DEC}.
25345 @cindex @code{System.Aux_DEC} package
25346 @cindex @code{Aux_DEC} package (child of @code{System})
25347 These definitions are incorporated directly into package @code{System},
25348 as though they had been declared there. For a
25349 list of the declarations added, see the spec of this package,
25350 which can be found in the file @file{s-auxdec.ads} in the GNAT library.
25351 @cindex @file{s-auxdec.ads} file
25352 The pragma @code{Extend_System} is a configuration pragma, which means that
25353 it can be placed in the file @file{gnat.adc}, so that it will automatically
25354 apply to all subsequent compilations. See @ref{Configuration Pragmas},
25355 for further details.
25356
25357 An alternative approach that avoids the use of the non-standard
25358 @code{Extend_System} pragma is to add a context clause to the unit that
25359 references these facilities:
25360
25361 @smallexample @c ada
25362 @cartouche
25363 with System.Aux_DEC;
25364 use System.Aux_DEC;
25365 @end cartouche
25366 @end smallexample
25367
25368 @noindent
25369 The effect is not quite semantically identical to incorporating
25370 the declarations directly into package @code{System},
25371 but most programs will not notice a difference
25372 unless they use prefix notation (e.g.@: @code{System.Integer_8})
25373 to reference the entities directly in package @code{System}.
25374 For units containing such references,
25375 the prefixes must either be removed, or the pragma @code{Extend_System}
25376 must be used.
25377
25378 @node Language-Related Features
25379 @section Language-Related Features
25380
25381 @noindent
25382 The following sections highlight differences in types,
25383 representations of types, operations, alignment, and
25384 related topics.
25385
25386 @menu
25387 * Integer Types and Representations::
25388 * Floating-Point Types and Representations::
25389 * Pragmas Float_Representation and Long_Float::
25390 * Fixed-Point Types and Representations::
25391 * Record and Array Component Alignment::
25392 * Address Clauses::
25393 * Other Representation Clauses::
25394 @end menu
25395
25396 @node Integer Types and Representations
25397 @subsection Integer Types and Representations
25398
25399 @noindent
25400 The set of predefined integer types is identical in HP Ada and GNAT.
25401 Furthermore the representation of these integer types is also identical,
25402 including the capability of size clauses forcing biased representation.
25403
25404 In addition,
25405 HP Ada for OpenVMS Alpha systems has defined the
25406 following additional integer types in package @code{System}:
25407
25408 @itemize @bullet
25409
25410 @item
25411 @code{INTEGER_8}
25412
25413 @item
25414 @code{INTEGER_16}
25415
25416 @item
25417 @code{INTEGER_32}
25418
25419 @item
25420 @code{INTEGER_64}
25421
25422 @item
25423 @code{LARGEST_INTEGER}
25424 @end itemize
25425
25426 @noindent
25427 In GNAT, the first four of these types may be obtained from the
25428 standard Ada package @code{Interfaces}.
25429 Alternatively, by use of the pragma @code{Extend_System}, identical
25430 declarations can be referenced directly in package @code{System}.
25431 On both GNAT and HP Ada, the maximum integer size is 64 bits.
25432
25433 @node Floating-Point Types and Representations
25434 @subsection Floating-Point Types and Representations
25435 @cindex Floating-Point types
25436
25437 @noindent
25438 The set of predefined floating-point types is identical in HP Ada and GNAT.
25439 Furthermore the representation of these floating-point
25440 types is also identical. One important difference is that the default
25441 representation for HP Ada is @code{VAX_Float}, but the default representation
25442 for GNAT is IEEE.
25443
25444 Specific types may be declared to be @code{VAX_Float} or IEEE, using the
25445 pragma @code{Float_Representation} as described in the HP Ada
25446 documentation.
25447 For example, the declarations:
25448
25449 @smallexample @c ada
25450 @cartouche
25451 type F_Float is digits 6;
25452 pragma Float_Representation (VAX_Float, F_Float);
25453 @end cartouche
25454 @end smallexample
25455
25456 @noindent
25457 declares a type @code{F_Float} that will be represented in @code{VAX_Float}
25458 format.
25459 This set of declarations actually appears in @code{System.Aux_DEC},
25460 which contains
25461 the full set of additional floating-point declarations provided in
25462 the HP Ada version of package @code{System}.
25463 This and similar declarations may be accessed in a user program
25464 by using pragma @code{Extend_System}. The use of this
25465 pragma, and the related pragma @code{Long_Float} is described in further
25466 detail in the following section.
25467
25468 @node Pragmas Float_Representation and Long_Float
25469 @subsection Pragmas @code{Float_Representation} and @code{Long_Float}
25470
25471 @noindent
25472 HP Ada provides the pragma @code{Float_Representation}, which
25473 acts as a program library switch to allow control over
25474 the internal representation chosen for the predefined
25475 floating-point types declared in the package @code{Standard}.
25476 The format of this pragma is as follows:
25477
25478 @smallexample @c ada
25479 @cartouche
25480 pragma Float_Representation(VAX_Float | IEEE_Float);
25481 @end cartouche
25482 @end smallexample
25483
25484 @noindent
25485 This pragma controls the representation of floating-point
25486 types as follows:
25487
25488 @itemize @bullet
25489 @item
25490 @code{VAX_Float} specifies that floating-point
25491 types are represented by default with the VAX system hardware types
25492 @code{F-floating}, @code{D-floating}, @code{G-floating}.
25493 Note that the @code{H-floating}
25494 type was available only on VAX systems, and is not available
25495 in either HP Ada or GNAT.
25496
25497 @item
25498 @code{IEEE_Float} specifies that floating-point
25499 types are represented by default with the IEEE single and
25500 double floating-point types.
25501 @end itemize
25502
25503 @noindent
25504 GNAT provides an identical implementation of the pragma
25505 @code{Float_Representation}, except that it functions as a
25506 configuration pragma. Note that the
25507 notion of configuration pragma corresponds closely to the
25508 HP Ada notion of a program library switch.
25509
25510 When no pragma is used in GNAT, the default is @code{IEEE_Float},
25511 which is different
25512 from HP Ada 83, where the default is @code{VAX_Float}. In addition, the
25513 predefined libraries in GNAT are built using @code{IEEE_Float}, so it is not
25514 advisable to change the format of numbers passed to standard library
25515 routines, and if necessary explicit type conversions may be needed.
25516
25517 The use of @code{IEEE_Float} is recommended in GNAT since it is more
25518 efficient, and (given that it conforms to an international standard)
25519 potentially more portable.
25520 The situation in which @code{VAX_Float} may be useful is in interfacing
25521 to existing code and data that expect the use of @code{VAX_Float}.
25522 In such a situation use the predefined @code{VAX_Float}
25523 types in package @code{System}, as extended by
25524 @code{Extend_System}. For example, use @code{System.F_Float}
25525 to specify the 32-bit @code{F-Float} format.
25526
25527 @noindent
25528 On OpenVMS systems, HP Ada provides the pragma @code{Long_Float}
25529 to allow control over the internal representation chosen
25530 for the predefined type @code{Long_Float} and for floating-point
25531 type declarations with digits specified in the range 7 .. 15.
25532 The format of this pragma is as follows:
25533
25534 @smallexample @c ada
25535 @cartouche
25536 pragma Long_Float (D_FLOAT | G_FLOAT);
25537 @end cartouche
25538 @end smallexample
25539
25540 @node Fixed-Point Types and Representations
25541 @subsection Fixed-Point Types and Representations
25542
25543 @noindent
25544 On HP Ada for OpenVMS Alpha systems, rounding is
25545 away from zero for both positive and negative numbers.
25546 Therefore, @code{+0.5} rounds to @code{1},
25547 and @code{-0.5} rounds to @code{-1}.
25548
25549 On GNAT the results of operations
25550 on fixed-point types are in accordance with the Ada
25551 rules. In particular, results of operations on decimal
25552 fixed-point types are truncated.
25553
25554 @node Record and Array Component Alignment
25555 @subsection Record and Array Component Alignment
25556
25557 @noindent
25558 On HP Ada for OpenVMS Alpha, all non-composite components
25559 are aligned on natural boundaries. For example, 1-byte
25560 components are aligned on byte boundaries, 2-byte
25561 components on 2-byte boundaries, 4-byte components on 4-byte
25562 byte boundaries, and so on. The OpenVMS Alpha hardware
25563 runs more efficiently with naturally aligned data.
25564
25565 On GNAT, alignment rules are compatible
25566 with HP Ada for OpenVMS Alpha.
25567
25568 @node Address Clauses
25569 @subsection Address Clauses
25570
25571 @noindent
25572 In HP Ada and GNAT, address clauses are supported for
25573 objects and imported subprograms.
25574 The predefined type @code{System.Address} is a private type
25575 in both compilers on Alpha OpenVMS, with the same representation
25576 (it is simply a machine pointer). Addition, subtraction, and comparison
25577 operations are available in the standard Ada package
25578 @code{System.Storage_Elements}, or in package @code{System}
25579 if it is extended to include @code{System.Aux_DEC} using a
25580 pragma @code{Extend_System} as previously described.
25581
25582 Note that code that @code{with}'s both this extended package @code{System}
25583 and the package @code{System.Storage_Elements} should not @code{use}
25584 both packages, or ambiguities will result. In general it is better
25585 not to mix these two sets of facilities. The Ada package was
25586 designed specifically to provide the kind of features that HP Ada
25587 adds directly to package @code{System}.
25588
25589 The type @code{System.Address} is a 64-bit integer type in GNAT for
25590 I64 OpenVMS. For more information,
25591 see @ref{Transitioning to 64-Bit GNAT for OpenVMS}.
25592
25593 GNAT is compatible with HP Ada in its handling of address
25594 clauses, except for some limitations in
25595 the form of address clauses for composite objects with
25596 initialization. Such address clauses are easily replaced
25597 by the use of an explicitly-defined constant as described
25598 in the Ada Reference Manual (13.1(22)). For example, the sequence
25599 of declarations:
25600
25601 @smallexample @c ada
25602 @cartouche
25603 X, Y : Integer := Init_Func;
25604 Q : String (X .. Y) := "abc";
25605 @dots{}
25606 for Q'Address use Compute_Address;
25607 @end cartouche
25608 @end smallexample
25609
25610 @noindent
25611 will be rejected by GNAT, since the address cannot be computed at the time
25612 that @code{Q} is declared. To achieve the intended effect, write instead:
25613
25614 @smallexample @c ada
25615 @group
25616 @cartouche
25617 X, Y : Integer := Init_Func;
25618 Q_Address : constant Address := Compute_Address;
25619 Q : String (X .. Y) := "abc";
25620 @dots{}
25621 for Q'Address use Q_Address;
25622 @end cartouche
25623 @end group
25624 @end smallexample
25625
25626 @noindent
25627 which will be accepted by GNAT (and other Ada compilers), and is also
25628 compatible with Ada 83. A fuller description of the restrictions
25629 on address specifications is found in @ref{Top, GNAT Reference Manual,
25630 About This Guide, gnat_rm, GNAT Reference Manual}.
25631
25632 @node Other Representation Clauses
25633 @subsection Other Representation Clauses
25634
25635 @noindent
25636 GNAT implements in a compatible manner all the representation
25637 clauses supported by HP Ada. In addition, GNAT
25638 implements the representation clause forms that were introduced in Ada 95,
25639 including @code{COMPONENT_SIZE} and @code{SIZE} clauses for objects.
25640
25641 @node The Package STANDARD
25642 @section The Package @code{STANDARD}
25643
25644 @noindent
25645 The package @code{STANDARD}, as implemented by HP Ada, is fully
25646 described in the @cite{Ada Reference Manual} and in the
25647 @cite{HP Ada Language Reference Manual}. As implemented by GNAT, the
25648 package @code{STANDARD} is described in the @cite{Ada Reference Manual}.
25649
25650 In addition, HP Ada supports the Latin-1 character set in
25651 the type @code{CHARACTER}. GNAT supports the Latin-1 character set
25652 in the type @code{CHARACTER} and also Unicode (ISO 10646 BMP) in
25653 the type @code{WIDE_CHARACTER}.
25654
25655 The floating-point types supported by GNAT are those
25656 supported by HP Ada, but the defaults are different, and are controlled by
25657 pragmas. See @ref{Floating-Point Types and Representations}, for details.
25658
25659 @node The Package SYSTEM
25660 @section The Package @code{SYSTEM}
25661
25662 @noindent
25663 HP Ada provides a specific version of the package
25664 @code{SYSTEM} for each platform on which the language is implemented.
25665 For the complete spec of the package @code{SYSTEM}, see
25666 Appendix F of the @cite{HP Ada Language Reference Manual}.
25667
25668 On HP Ada, the package @code{SYSTEM} includes the following conversion
25669 functions:
25670 @itemize @bullet
25671 @item @code{TO_ADDRESS(INTEGER)}
25672
25673 @item @code{TO_ADDRESS(UNSIGNED_LONGWORD)}
25674
25675 @item @code{TO_ADDRESS(}@i{universal_integer}@code{)}
25676
25677 @item @code{TO_INTEGER(ADDRESS)}
25678
25679 @item @code{TO_UNSIGNED_LONGWORD(ADDRESS)}
25680
25681 @item Function @code{IMPORT_VALUE return UNSIGNED_LONGWORD} and the
25682 functions @code{IMPORT_ADDRESS} and @code{IMPORT_LARGEST_VALUE}
25683 @end itemize
25684
25685 @noindent
25686 By default, GNAT supplies a version of @code{SYSTEM} that matches
25687 the definition given in the @cite{Ada Reference Manual}.
25688 This
25689 is a subset of the HP system definitions, which is as
25690 close as possible to the original definitions. The only difference
25691 is that the definition of @code{SYSTEM_NAME} is different:
25692
25693 @smallexample @c ada
25694 @cartouche
25695 type Name is (SYSTEM_NAME_GNAT);
25696 System_Name : constant Name := SYSTEM_NAME_GNAT;
25697 @end cartouche
25698 @end smallexample
25699
25700 @noindent
25701 Also, GNAT adds the Ada declarations for
25702 @code{BIT_ORDER} and @code{DEFAULT_BIT_ORDER}.
25703
25704 However, the use of the following pragma causes GNAT
25705 to extend the definition of package @code{SYSTEM} so that it
25706 encompasses the full set of HP-specific extensions,
25707 including the functions listed above:
25708
25709 @smallexample @c ada
25710 @cartouche
25711 pragma Extend_System (Aux_DEC);
25712 @end cartouche
25713 @end smallexample
25714
25715 @noindent
25716 The pragma @code{Extend_System} is a configuration pragma that
25717 is most conveniently placed in the @file{gnat.adc} file. @xref{Pragma
25718 Extend_System,,, gnat_rm, GNAT Reference Manual}, for further details.
25719
25720 HP Ada does not allow the recompilation of the package
25721 @code{SYSTEM}. Instead HP Ada provides several pragmas
25722 (@code{SYSTEM_NAME}, @code{STORAGE_UNIT}, and @code{MEMORY_SIZE})
25723 to modify values in the package @code{SYSTEM}.
25724 On OpenVMS Alpha systems, the pragma
25725 @code{SYSTEM_NAME} takes the enumeration literal @code{OPENVMS_AXP} as
25726 its single argument.
25727
25728 GNAT does permit the recompilation of package @code{SYSTEM} using
25729 the special switch @option{-gnatg}, and this switch can be used if
25730 it is necessary to modify the definitions in @code{SYSTEM}. GNAT does
25731 not permit the specification of @code{SYSTEM_NAME}, @code{STORAGE_UNIT}
25732 or @code{MEMORY_SIZE} by any other means.
25733
25734 On GNAT systems, the pragma @code{SYSTEM_NAME} takes the
25735 enumeration literal @code{SYSTEM_NAME_GNAT}.
25736
25737 The definitions provided by the use of
25738
25739 @smallexample @c ada
25740 pragma Extend_System (AUX_Dec);
25741 @end smallexample
25742
25743 @noindent
25744 are virtually identical to those provided by the HP Ada 83 package
25745 @code{SYSTEM}. One important difference is that the name of the
25746 @code{TO_ADDRESS}
25747 function for type @code{UNSIGNED_LONGWORD} is changed to
25748 @code{TO_ADDRESS_LONG}.
25749 @xref{Address Clauses,,, gnat_rm, GNAT Reference Manual}, for a
25750 discussion of why this change was necessary.
25751
25752 @noindent
25753 The version of @code{TO_ADDRESS} taking a @i{universal_integer} argument
25754 is in fact
25755 an extension to Ada 83 not strictly compatible with the reference manual.
25756 GNAT, in order to be exactly compatible with the standard,
25757 does not provide this capability. In HP Ada 83, the
25758 point of this definition is to deal with a call like:
25759
25760 @smallexample @c ada
25761 TO_ADDRESS (16#12777#);
25762 @end smallexample
25763
25764 @noindent
25765 Normally, according to Ada 83 semantics, one would expect this to be
25766 ambiguous, since it matches both the @code{INTEGER} and
25767 @code{UNSIGNED_LONGWORD} forms of @code{TO_ADDRESS}.
25768 However, in HP Ada 83, there is no ambiguity, since the
25769 definition using @i{universal_integer} takes precedence.
25770
25771 In GNAT, since the version with @i{universal_integer} cannot be supplied,
25772 it is
25773 not possible to be 100% compatible. Since there are many programs using
25774 numeric constants for the argument to @code{TO_ADDRESS}, the decision in
25775 GNAT was
25776 to change the name of the function in the @code{UNSIGNED_LONGWORD} case,
25777 so the declarations provided in the GNAT version of @code{AUX_Dec} are:
25778
25779 @smallexample @c ada
25780 function To_Address (X : Integer) return Address;
25781 pragma Pure_Function (To_Address);
25782
25783 function To_Address_Long (X : Unsigned_Longword) return Address;
25784 pragma Pure_Function (To_Address_Long);
25785 @end smallexample
25786
25787 @noindent
25788 This means that programs using @code{TO_ADDRESS} for
25789 @code{UNSIGNED_LONGWORD} must change the name to @code{TO_ADDRESS_LONG}.
25790
25791 @node Tasking and Task-Related Features
25792 @section Tasking and Task-Related Features
25793
25794 @noindent
25795 This section compares the treatment of tasking in GNAT
25796 and in HP Ada for OpenVMS Alpha.
25797 The GNAT description applies to both Alpha and I64 OpenVMS.
25798 For detailed information on tasking in
25799 HP Ada, see the @cite{HP Ada Language Reference Manual} and the
25800 relevant run-time reference manual.
25801
25802 @menu
25803 * Implementation of Tasks in HP Ada for OpenVMS Alpha Systems::
25804 * Assigning Task IDs::
25805 * Task IDs and Delays::
25806 * Task-Related Pragmas::
25807 * Scheduling and Task Priority::
25808 * The Task Stack::
25809 * External Interrupts::
25810 @end menu
25811
25812 @node Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
25813 @subsection Implementation of Tasks in HP Ada for OpenVMS Alpha Systems
25814
25815 @noindent
25816 On OpenVMS Alpha systems, each Ada task (except a passive
25817 task) is implemented as a single stream of execution
25818 that is created and managed by the kernel. On these
25819 systems, HP Ada tasking support is based on DECthreads,
25820 an implementation of the POSIX standard for threads.
25821
25822 Also, on OpenVMS Alpha systems, HP Ada tasks and foreign
25823 code that calls DECthreads routines can be used together.
25824 The interaction between Ada tasks and DECthreads routines
25825 can have some benefits. For example when on OpenVMS Alpha,
25826 HP Ada can call C code that is already threaded.
25827
25828 GNAT uses the facilities of DECthreads,
25829 and Ada tasks are mapped to threads.
25830
25831 @node Assigning Task IDs
25832 @subsection Assigning Task IDs
25833
25834 @noindent
25835 The HP Ada Run-Time Library always assigns @code{%TASK 1} to
25836 the environment task that executes the main program. On
25837 OpenVMS Alpha systems, @code{%TASK 0} is often used for tasks
25838 that have been created but are not yet activated.
25839
25840 On OpenVMS Alpha systems, task IDs are assigned at
25841 activation. On GNAT systems, task IDs are also assigned at
25842 task creation but do not have the same form or values as
25843 task ID values in HP Ada. There is no null task, and the
25844 environment task does not have a specific task ID value.
25845
25846 @node Task IDs and Delays
25847 @subsection Task IDs and Delays
25848
25849 @noindent
25850 On OpenVMS Alpha systems, tasking delays are implemented
25851 using Timer System Services. The Task ID is used for the
25852 identification of the timer request (the @code{REQIDT} parameter).
25853 If Timers are used in the application take care not to use
25854 @code{0} for the identification, because cancelling such a timer
25855 will cancel all timers and may lead to unpredictable results.
25856
25857 @node Task-Related Pragmas
25858 @subsection Task-Related Pragmas
25859
25860 @noindent
25861 Ada supplies the pragma @code{TASK_STORAGE}, which allows
25862 specification of the size of the guard area for a task
25863 stack. (The guard area forms an area of memory that has no
25864 read or write access and thus helps in the detection of
25865 stack overflow.) On OpenVMS Alpha systems, if the pragma
25866 @code{TASK_STORAGE} specifies a value of zero, a minimal guard
25867 area is created. In the absence of a pragma @code{TASK_STORAGE},
25868 a default guard area is created.
25869
25870 GNAT supplies the following task-related pragmas:
25871
25872 @itemize @bullet
25873 @item @code{TASK_INFO}
25874
25875 This pragma appears within a task definition and
25876 applies to the task in which it appears. The argument
25877 must be of type @code{SYSTEM.TASK_INFO.TASK_INFO_TYPE}.
25878
25879 @item @code{TASK_STORAGE}
25880
25881 GNAT implements pragma @code{TASK_STORAGE} in the same way as HP Ada.
25882 Both HP Ada and GNAT supply the pragmas @code{PASSIVE},
25883 @code{SUPPRESS}, and @code{VOLATILE}.
25884 @end itemize
25885 @node Scheduling and Task Priority
25886 @subsection Scheduling and Task Priority
25887
25888 @noindent
25889 HP Ada implements the Ada language requirement that
25890 when two tasks are eligible for execution and they have
25891 different priorities, the lower priority task does not
25892 execute while the higher priority task is waiting. The HP
25893 Ada Run-Time Library keeps a task running until either the
25894 task is suspended or a higher priority task becomes ready.
25895
25896 On OpenVMS Alpha systems, the default strategy is round-
25897 robin with preemption. Tasks of equal priority take turns
25898 at the processor. A task is run for a certain period of
25899 time and then placed at the tail of the ready queue for
25900 its priority level.
25901
25902 HP Ada provides the implementation-defined pragma @code{TIME_SLICE},
25903 which can be used to enable or disable round-robin
25904 scheduling of tasks with the same priority.
25905 See the relevant HP Ada run-time reference manual for
25906 information on using the pragmas to control HP Ada task
25907 scheduling.
25908
25909 GNAT follows the scheduling rules of Annex D (Real-Time
25910 Annex) of the @cite{Ada Reference Manual}. In general, this
25911 scheduling strategy is fully compatible with HP Ada
25912 although it provides some additional constraints (as
25913 fully documented in Annex D).
25914 GNAT implements time slicing control in a manner compatible with
25915 HP Ada 83, by means of the pragma @code{Time_Slice}, whose semantics
25916 are identical to the HP Ada 83 pragma of the same name.
25917 Note that it is not possible to mix GNAT tasking and
25918 HP Ada 83 tasking in the same program, since the two run-time
25919 libraries are not compatible.
25920
25921 @node The Task Stack
25922 @subsection The Task Stack
25923
25924 @noindent
25925 In HP Ada, a task stack is allocated each time a
25926 non-passive task is activated. As soon as the task is
25927 terminated, the storage for the task stack is deallocated.
25928 If you specify a size of zero (bytes) with @code{T'STORAGE_SIZE},
25929 a default stack size is used. Also, regardless of the size
25930 specified, some additional space is allocated for task
25931 management purposes. On OpenVMS Alpha systems, at least
25932 one page is allocated.
25933
25934 GNAT handles task stacks in a similar manner. In accordance with
25935 the Ada rules, it provides the pragma @code{STORAGE_SIZE} as
25936 an alternative method for controlling the task stack size.
25937 The specification of the attribute @code{T'STORAGE_SIZE} is also
25938 supported in a manner compatible with HP Ada.
25939
25940 @node External Interrupts
25941 @subsection External Interrupts
25942
25943 @noindent
25944 On HP Ada, external interrupts can be associated with task entries.
25945 GNAT is compatible with HP Ada in its handling of external interrupts.
25946
25947 @node Pragmas and Pragma-Related Features
25948 @section Pragmas and Pragma-Related Features
25949
25950 @noindent
25951 Both HP Ada and GNAT supply all language-defined pragmas
25952 as specified by the Ada 83 standard. GNAT also supplies all
25953 language-defined pragmas introduced by Ada 95 and Ada 2005.
25954 In addition, GNAT implements the implementation-defined pragmas
25955 from HP Ada 83.
25956
25957 @itemize @bullet
25958 @item @code{AST_ENTRY}
25959
25960 @item @code{COMMON_OBJECT}
25961
25962 @item @code{COMPONENT_ALIGNMENT}
25963
25964 @item @code{EXPORT_EXCEPTION}
25965
25966 @item @code{EXPORT_FUNCTION}
25967
25968 @item @code{EXPORT_OBJECT}
25969
25970 @item @code{EXPORT_PROCEDURE}
25971
25972 @item @code{EXPORT_VALUED_PROCEDURE}
25973
25974 @item @code{FLOAT_REPRESENTATION}
25975
25976 @item @code{IDENT}
25977
25978 @item @code{IMPORT_EXCEPTION}
25979
25980 @item @code{IMPORT_FUNCTION}
25981
25982 @item @code{IMPORT_OBJECT}
25983
25984 @item @code{IMPORT_PROCEDURE}
25985
25986 @item @code{IMPORT_VALUED_PROCEDURE}
25987
25988 @item @code{INLINE_GENERIC}
25989
25990 @item @code{INTERFACE_NAME}
25991
25992 @item @code{LONG_FLOAT}
25993
25994 @item @code{MAIN_STORAGE}
25995
25996 @item @code{PASSIVE}
25997
25998 @item @code{PSECT_OBJECT}
25999
26000 @item @code{SHARE_GENERIC}
26001
26002 @item @code{SUPPRESS_ALL}
26003
26004 @item @code{TASK_STORAGE}
26005
26006 @item @code{TIME_SLICE}
26007
26008 @item @code{TITLE}
26009 @end itemize
26010
26011 @noindent
26012 These pragmas are all fully implemented, with the exception of @code{TITLE},
26013 @code{PASSIVE}, and @code{SHARE_GENERIC}, which are
26014 recognized, but which have no
26015 effect in GNAT. The effect of @code{PASSIVE} may be obtained by the
26016 use of Ada protected objects. In GNAT, all generics are inlined.
26017
26018 Unlike HP Ada, the GNAT ``@code{EXPORT_}@i{subprogram}'' pragmas require
26019 a separate subprogram specification which must appear before the
26020 subprogram body.
26021
26022 GNAT also supplies a number of implementation-defined pragmas as follows:
26023 @itemize @bullet
26024 @item @code{ABORT_DEFER}
26025
26026 @item @code{ADA_83}
26027
26028 @item @code{ADA_95}
26029
26030 @item @code{ADA_05}
26031
26032 @item @code{ANNOTATE}
26033
26034 @item @code{ASSERT}
26035
26036 @item @code{C_PASS_BY_COPY}
26037
26038 @item @code{CPP_CLASS}
26039
26040 @item @code{CPP_CONSTRUCTOR}
26041
26042 @item @code{CPP_DESTRUCTOR}
26043
26044 @item @code{DEBUG}
26045
26046 @item @code{EXTEND_SYSTEM}
26047
26048 @item @code{LINKER_ALIAS}
26049
26050 @item @code{LINKER_SECTION}
26051
26052 @item @code{MACHINE_ATTRIBUTE}
26053
26054 @item @code{NO_RETURN}
26055
26056 @item @code{PURE_FUNCTION}
26057
26058 @item @code{SOURCE_FILE_NAME}
26059
26060 @item @code{SOURCE_REFERENCE}
26061
26062 @item @code{TASK_INFO}
26063
26064 @item @code{UNCHECKED_UNION}
26065
26066 @item @code{UNIMPLEMENTED_UNIT}
26067
26068 @item @code{UNIVERSAL_DATA}
26069
26070 @item @code{UNSUPPRESS}
26071
26072 @item @code{WARNINGS}
26073
26074 @item @code{WEAK_EXTERNAL}
26075 @end itemize
26076
26077 @noindent
26078 For full details on these GNAT implementation-defined pragmas,
26079 see @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT Reference
26080 Manual}.
26081
26082 @menu
26083 * Restrictions on the Pragma INLINE::
26084 * Restrictions on the Pragma INTERFACE::
26085 * Restrictions on the Pragma SYSTEM_NAME::
26086 @end menu
26087
26088 @node Restrictions on the Pragma INLINE
26089 @subsection Restrictions on Pragma @code{INLINE}
26090
26091 @noindent
26092 HP Ada enforces the following restrictions on the pragma @code{INLINE}:
26093 @itemize @bullet
26094 @item Parameters cannot have a task type.
26095
26096 @item Function results cannot be task types, unconstrained
26097 array types, or unconstrained types with discriminants.
26098
26099 @item Bodies cannot declare the following:
26100 @itemize @bullet
26101 @item Subprogram body or stub (imported subprogram is allowed)
26102
26103 @item Tasks
26104
26105 @item Generic declarations
26106
26107 @item Instantiations
26108
26109 @item Exceptions
26110
26111 @item Access types (types derived from access types allowed)
26112
26113 @item Array or record types
26114
26115 @item Dependent tasks
26116
26117 @item Direct recursive calls of subprogram or containing
26118 subprogram, directly or via a renaming
26119
26120 @end itemize
26121 @end itemize
26122
26123 @noindent
26124 In GNAT, the only restriction on pragma @code{INLINE} is that the
26125 body must occur before the call if both are in the same
26126 unit, and the size must be appropriately small. There are
26127 no other specific restrictions which cause subprograms to
26128 be incapable of being inlined.
26129
26130 @node Restrictions on the Pragma INTERFACE
26131 @subsection Restrictions on Pragma @code{INTERFACE}
26132
26133 @noindent
26134 The following restrictions on pragma @code{INTERFACE}
26135 are enforced by both HP Ada and GNAT:
26136 @itemize @bullet
26137 @item Languages accepted: Ada, Bliss, C, Fortran, Default.
26138 Default is the default on OpenVMS Alpha systems.
26139
26140 @item Parameter passing: Language specifies default
26141 mechanisms but can be overridden with an @code{EXPORT} pragma.
26142
26143 @itemize @bullet
26144 @item Ada: Use internal Ada rules.
26145
26146 @item Bliss, C: Parameters must be mode @code{in}; cannot be
26147 record or task type. Result cannot be a string, an
26148 array, or a record.
26149
26150 @item Fortran: Parameters cannot have a task type. Result cannot
26151 be a string, an array, or a record.
26152 @end itemize
26153 @end itemize
26154
26155 @noindent
26156 GNAT is entirely upwards compatible with HP Ada, and in addition allows
26157 record parameters for all languages.
26158
26159 @node Restrictions on the Pragma SYSTEM_NAME
26160 @subsection Restrictions on Pragma @code{SYSTEM_NAME}
26161
26162 @noindent
26163 For HP Ada for OpenVMS Alpha, the enumeration literal
26164 for the type @code{NAME} is @code{OPENVMS_AXP}.
26165 In GNAT, the enumeration
26166 literal for the type @code{NAME} is @code{SYSTEM_NAME_GNAT}.
26167
26168 @node Library of Predefined Units
26169 @section Library of Predefined Units
26170
26171 @noindent
26172 A library of predefined units is provided as part of the
26173 HP Ada and GNAT implementations. HP Ada does not provide
26174 the package @code{MACHINE_CODE} but instead recommends importing
26175 assembler code.
26176
26177 The GNAT versions of the HP Ada Run-Time Library (@code{ADA$PREDEFINED:})
26178 units are taken from the OpenVMS Alpha version, not the OpenVMS VAX
26179 version.
26180 The HP Ada Predefined Library units are modified to remove post-Ada 83
26181 incompatibilities and to make them interoperable with GNAT
26182 (@pxref{Changes to DECLIB}, for details).
26183 The units are located in the @file{DECLIB} directory.
26184
26185 The GNAT RTL is contained in
26186 the @file{ADALIB} directory, and
26187 the default search path is set up to find @code{DECLIB} units in preference
26188 to @code{ADALIB} units with the same name (@code{TEXT_IO},
26189 @code{SEQUENTIAL_IO}, and @code{DIRECT_IO}, for example).
26190
26191 @menu
26192 * Changes to DECLIB::
26193 @end menu
26194
26195 @node Changes to DECLIB
26196 @subsection Changes to @code{DECLIB}
26197
26198 @noindent
26199 The changes made to the HP Ada predefined library for GNAT and post-Ada 83
26200 compatibility are minor and include the following:
26201
26202 @itemize @bullet
26203 @item Adjusting the location of pragmas and record representation
26204 clauses to obey Ada 95 (and thus Ada 2005) rules
26205
26206 @item Adding the proper notation to generic formal parameters
26207 that take unconstrained types in instantiation
26208
26209 @item Adding pragma @code{ELABORATE_BODY} to package specs
26210 that have package bodies not otherwise allowed
26211
26212 @item Replacing occurrences of the identifier ``@code{PROTECTED}'' by
26213 ``@code{PROTECTD}''.
26214 Currently these are found only in the @code{STARLET} package spec.
26215
26216 @item Changing @code{SYSTEM.ADDRESS} to @code{SYSTEM.SHORT_ADDRESS}
26217 where the address size is constrained to 32 bits.
26218 @end itemize
26219
26220 @noindent
26221 None of the above changes is visible to users.
26222
26223 @node Bindings
26224 @section Bindings
26225
26226 @noindent
26227 On OpenVMS Alpha, HP Ada provides the following strongly-typed bindings:
26228 @itemize @bullet
26229
26230 @item Command Language Interpreter (CLI interface)
26231
26232 @item DECtalk Run-Time Library (DTK interface)
26233
26234 @item Librarian utility routines (LBR interface)
26235
26236 @item General Purpose Run-Time Library (LIB interface)
26237
26238 @item Math Run-Time Library (MTH interface)
26239
26240 @item National Character Set Run-Time Library (NCS interface)
26241
26242 @item Compiled Code Support Run-Time Library (OTS interface)
26243
26244 @item Parallel Processing Run-Time Library (PPL interface)
26245
26246 @item Screen Management Run-Time Library (SMG interface)
26247
26248 @item Sort Run-Time Library (SOR interface)
26249
26250 @item String Run-Time Library (STR interface)
26251
26252 @item STARLET System Library
26253 @findex Starlet
26254
26255 @item X Window System Version 11R4 and 11R5 (X, XLIB interface)
26256
26257 @item X Windows Toolkit (XT interface)
26258
26259 @item X/Motif Version 1.1.3 and 1.2 (XM interface)
26260 @end itemize
26261
26262 @noindent
26263 GNAT provides implementations of these HP bindings in the @code{DECLIB}
26264 directory, on both the Alpha and I64 OpenVMS platforms.
26265
26266 The X/Motif bindings used to build @code{DECLIB} are whatever versions are
26267 in the
26268 HP Ada @file{ADA$PREDEFINED} directory with extension @file{.ADC}.
26269 A pragma @code{Linker_Options} has been added to packages @code{Xm},
26270 @code{Xt}, and @code{X_Lib}
26271 causing the default X/Motif sharable image libraries to be linked in. This
26272 is done via options files named @file{xm.opt}, @file{xt.opt}, and
26273 @file{x_lib.opt} (also located in the @file{DECLIB} directory).
26274
26275 It may be necessary to edit these options files to update or correct the
26276 library names if, for example, the newer X/Motif bindings from
26277 @file{ADA$EXAMPLES}
26278 had been (previous to installing GNAT) copied and renamed to supersede the
26279 default @file{ADA$PREDEFINED} versions.
26280
26281 @menu
26282 * Shared Libraries and Options Files::
26283 * Interfaces to C::
26284 @end menu
26285
26286 @node Shared Libraries and Options Files
26287 @subsection Shared Libraries and Options Files
26288
26289 @noindent
26290 When using the HP Ada
26291 predefined X and Motif bindings, the linking with their sharable images is
26292 done automatically by @command{GNAT LINK}.
26293 When using other X and Motif bindings, you need
26294 to add the corresponding sharable images to the command line for
26295 @code{GNAT LINK}. When linking with shared libraries, or with
26296 @file{.OPT} files, you must
26297 also add them to the command line for @command{GNAT LINK}.
26298
26299 A shared library to be used with GNAT is built in the same way as other
26300 libraries under VMS. The VMS Link command can be used in standard fashion.
26301
26302 @node Interfaces to C
26303 @subsection Interfaces to C
26304
26305 @noindent
26306 HP Ada
26307 provides the following Ada types and operations:
26308
26309 @itemize @bullet
26310 @item C types package (@code{C_TYPES})
26311
26312 @item C strings (@code{C_TYPES.NULL_TERMINATED})
26313
26314 @item Other_types (@code{SHORT_INT})
26315 @end itemize
26316
26317 @noindent
26318 Interfacing to C with GNAT, you can use the above approach
26319 described for HP Ada or the facilities of Annex B of
26320 the @cite{Ada Reference Manual} (packages @code{INTERFACES.C},
26321 @code{INTERFACES.C.STRINGS} and @code{INTERFACES.C.POINTERS}). For more
26322 information, see @ref{Interfacing to C,,, gnat_rm, GNAT Reference Manual}.
26323
26324 The @option{-gnatF} qualifier forces default and explicit
26325 @code{External_Name} parameters in pragmas @code{Import} and @code{Export}
26326 to be uppercased for compatibility with the default behavior
26327 of HP C. The qualifier has no effect on @code{Link_Name} parameters.
26328
26329 @node Main Program Definition
26330 @section Main Program Definition
26331
26332 @noindent
26333 The following section discusses differences in the
26334 definition of main programs on HP Ada and GNAT.
26335 On HP Ada, main programs are defined to meet the
26336 following conditions:
26337 @itemize @bullet
26338 @item Procedure with no formal parameters (returns @code{0} upon
26339 normal completion)
26340
26341 @item Procedure with no formal parameters (returns @code{42} when
26342 an unhandled exception is raised)
26343
26344 @item Function with no formal parameters whose returned value
26345 is of a discrete type
26346
26347 @item Procedure with one @code{out} formal of a discrete type for
26348 which a specification of pragma @code{EXPORT_VALUED_PROCEDURE} is given.
26349
26350 @end itemize
26351
26352 @noindent
26353 When declared with the pragma @code{EXPORT_VALUED_PROCEDURE},
26354 a main function or main procedure returns a discrete
26355 value whose size is less than 64 bits (32 on VAX systems),
26356 the value is zero- or sign-extended as appropriate.
26357 On GNAT, main programs are defined as follows:
26358 @itemize @bullet
26359 @item Must be a non-generic, parameterless subprogram that
26360 is either a procedure or function returning an Ada
26361 @code{STANDARD.INTEGER} (the predefined type)
26362
26363 @item Cannot be a generic subprogram or an instantiation of a
26364 generic subprogram
26365 @end itemize
26366
26367 @node Implementation-Defined Attributes
26368 @section Implementation-Defined Attributes
26369
26370 @noindent
26371 GNAT provides all HP Ada implementation-defined
26372 attributes.
26373
26374 @node Compiler and Run-Time Interfacing
26375 @section Compiler and Run-Time Interfacing
26376
26377 @noindent
26378 HP Ada provides the following qualifiers to pass options to the linker
26379 (ACS LINK):
26380 @itemize @bullet
26381 @item @option{/WAIT} and @option{/SUBMIT}
26382
26383 @item @option{/COMMAND}
26384
26385 @item @option{/@r{[}NO@r{]}MAP}
26386
26387 @item @option{/OUTPUT=@var{file-spec}}
26388
26389 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
26390 @end itemize
26391
26392 @noindent
26393 To pass options to the linker, GNAT provides the following
26394 switches:
26395
26396 @itemize @bullet
26397 @item @option{/EXECUTABLE=@var{exec-name}}
26398
26399 @item @option{/VERBOSE}
26400
26401 @item @option{/@r{[}NO@r{]}DEBUG} and @option{/@r{[}NO@r{]}TRACEBACK}
26402 @end itemize
26403
26404 @noindent
26405 For more information on these switches, see
26406 @ref{Switches for gnatlink}.
26407 In HP Ada, the command-line switch @option{/OPTIMIZE} is available
26408 to control optimization. HP Ada also supplies the
26409 following pragmas:
26410 @itemize @bullet
26411 @item @code{OPTIMIZE}
26412
26413 @item @code{INLINE}
26414
26415 @item @code{INLINE_GENERIC}
26416
26417 @item @code{SUPPRESS_ALL}
26418
26419 @item @code{PASSIVE}
26420 @end itemize
26421
26422 @noindent
26423 In GNAT, optimization is controlled strictly by command
26424 line parameters, as described in the corresponding section of this guide.
26425 The HP pragmas for control of optimization are
26426 recognized but ignored.
26427
26428 Note that in GNAT, the default is optimization off, whereas in HP Ada
26429 the default is that optimization is turned on.
26430
26431 @node Program Compilation and Library Management
26432 @section Program Compilation and Library Management
26433
26434 @noindent
26435 HP Ada and GNAT provide a comparable set of commands to
26436 build programs. HP Ada also provides a program library,
26437 which is a concept that does not exist on GNAT. Instead,
26438 GNAT provides directories of sources that are compiled as
26439 needed.
26440
26441 The following table summarizes
26442 the HP Ada commands and provides
26443 equivalent GNAT commands. In this table, some GNAT
26444 equivalents reflect the fact that GNAT does not use the
26445 concept of a program library. Instead, it uses a model
26446 in which collections of source and object files are used
26447 in a manner consistent with other languages like C and
26448 Fortran. Therefore, standard system file commands are used
26449 to manipulate these elements. Those GNAT commands are marked with
26450 an asterisk.
26451 Note that, unlike HP Ada, none of the GNAT commands accepts wild cards.
26452
26453 @need 1500
26454 @multitable @columnfractions .35 .65
26455
26456 @item @emph{HP Ada Command}
26457 @tab @emph{GNAT Equivalent / Description}
26458
26459 @item @command{ADA}
26460 @tab @command{GNAT COMPILE}@*
26461 Invokes the compiler to compile one or more Ada source files.
26462
26463 @item @command{ACS ATTACH}@*
26464 @tab [No equivalent]@*
26465 Switches control of terminal from current process running the program
26466 library manager.
26467
26468 @item @command{ACS CHECK}
26469 @tab @command{GNAT MAKE /DEPENDENCY_LIST}@*
26470 Forms the execution closure of one
26471 or more compiled units and checks completeness and currency.
26472
26473 @item @command{ACS COMPILE}
26474 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
26475 Forms the execution closure of one or
26476 more specified units, checks completeness and currency,
26477 identifies units that have revised source files, compiles same,
26478 and recompiles units that are or will become obsolete.
26479 Also completes incomplete generic instantiations.
26480
26481 @item @command{ACS COPY FOREIGN}
26482 @tab Copy (*)@*
26483 Copies a foreign object file into the program library as a
26484 library unit body.
26485
26486 @item @command{ACS COPY UNIT}
26487 @tab Copy (*)@*
26488 Copies a compiled unit from one program library to another.
26489
26490 @item @command{ACS CREATE LIBRARY}
26491 @tab Create /directory (*)@*
26492 Creates a program library.
26493
26494 @item @command{ACS CREATE SUBLIBRARY}
26495 @tab Create /directory (*)@*
26496 Creates a program sublibrary.
26497
26498 @item @command{ACS DELETE LIBRARY}
26499 @tab @*
26500 Deletes a program library and its contents.
26501
26502 @item @command{ACS DELETE SUBLIBRARY}
26503 @tab @*
26504 Deletes a program sublibrary and its contents.
26505
26506 @item @command{ACS DELETE UNIT}
26507 @tab Delete file (*)@*
26508 On OpenVMS systems, deletes one or more compiled units from
26509 the current program library.
26510
26511 @item @command{ACS DIRECTORY}
26512 @tab Directory (*)@*
26513 On OpenVMS systems, lists units contained in the current
26514 program library.
26515
26516 @item @command{ACS ENTER FOREIGN}
26517 @tab Copy (*)@*
26518 Allows the import of a foreign body as an Ada library
26519 spec and enters a reference to a pointer.
26520
26521 @item @command{ACS ENTER UNIT}
26522 @tab Copy (*)@*
26523 Enters a reference (pointer) from the current program library to
26524 a unit compiled into another program library.
26525
26526 @item @command{ACS EXIT}
26527 @tab [No equivalent]@*
26528 Exits from the program library manager.
26529
26530 @item @command{ACS EXPORT}
26531 @tab Copy (*)@*
26532 Creates an object file that contains system-specific object code
26533 for one or more units. With GNAT, object files can simply be copied
26534 into the desired directory.
26535
26536 @item @command{ACS EXTRACT SOURCE}
26537 @tab Copy (*)@*
26538 Allows access to the copied source file for each Ada compilation unit
26539
26540 @item @command{ACS HELP}
26541 @tab @command{HELP GNAT}@*
26542 Provides online help.
26543
26544 @item @command{ACS LINK}
26545 @tab @command{GNAT LINK}@*
26546 Links an object file containing Ada units into an executable file.
26547
26548 @item @command{ACS LOAD}
26549 @tab Copy (*)@*
26550 Loads (partially compiles) Ada units into the program library.
26551 Allows loading a program from a collection of files into a library
26552 without knowing the relationship among units.
26553
26554 @item @command{ACS MERGE}
26555 @tab Copy (*)@*
26556 Merges into the current program library, one or more units from
26557 another library where they were modified.
26558
26559 @item @command{ACS RECOMPILE}
26560 @tab @command{GNAT MAKE /ACTIONS=COMPILE}@*
26561 Recompiles from external or copied source files any obsolete
26562 unit in the closure. Also, completes any incomplete generic
26563 instantiations.
26564
26565 @item @command{ACS REENTER}
26566 @tab @command{GNAT MAKE}@*
26567 Reenters current references to units compiled after last entered
26568 with the @command{ACS ENTER UNIT} command.
26569
26570 @item @command{ACS SET LIBRARY}
26571 @tab Set default (*)@*
26572 Defines a program library to be the compilation context as well
26573 as the target library for compiler output and commands in general.
26574
26575 @item @command{ACS SET PRAGMA}
26576 @tab Edit @file{gnat.adc} (*)@*
26577 Redefines specified values of the library characteristics
26578 @code{LONG_ FLOAT}, @code{MEMORY_SIZE}, @code{SYSTEM_NAME},
26579 and @code{Float_Representation}.
26580
26581 @item @command{ACS SET SOURCE}
26582 @tab Define @code{ADA_INCLUDE_PATH} path (*)@*
26583 Defines the source file search list for the @command{ACS COMPILE} command.
26584
26585 @item @command{ACS SHOW LIBRARY}
26586 @tab Directory (*)@*
26587 Lists information about one or more program libraries.
26588
26589 @item @command{ACS SHOW PROGRAM}
26590 @tab [No equivalent]@*
26591 Lists information about the execution closure of one or
26592 more units in the program library.
26593
26594 @item @command{ACS SHOW SOURCE}
26595 @tab Show logical @code{ADA_INCLUDE_PATH}@*
26596 Shows the source file search used when compiling units.
26597
26598 @item @command{ACS SHOW VERSION}
26599 @tab Compile with @option{VERBOSE} option
26600 Displays the version number of the compiler and program library
26601 manager used.
26602
26603 @item @command{ACS SPAWN}
26604 @tab [No equivalent]@*
26605 Creates a subprocess of the current process (same as @command{DCL SPAWN}
26606 command).
26607
26608 @item @command{ACS VERIFY}
26609 @tab [No equivalent]@*
26610 Performs a series of consistency checks on a program library to
26611 determine whether the library structure and library files are in
26612 valid form.
26613 @end multitable
26614
26615 @noindent
26616
26617 @node Input-Output
26618 @section Input-Output
26619
26620 @noindent
26621 On OpenVMS Alpha systems, HP Ada uses OpenVMS Record
26622 Management Services (RMS) to perform operations on
26623 external files.
26624
26625 @noindent
26626 HP Ada and GNAT predefine an identical set of input-
26627 output packages. To make the use of the
26628 generic @code{TEXT_IO} operations more convenient, HP Ada
26629 provides predefined library packages that instantiate the
26630 integer and floating-point operations for the predefined
26631 integer and floating-point types as shown in the following table.
26632
26633 @multitable @columnfractions .45 .55
26634 @item @emph{Package Name} @tab Instantiation
26635
26636 @item @code{INTEGER_TEXT_IO}
26637 @tab @code{INTEGER_IO(INTEGER)}
26638
26639 @item @code{SHORT_INTEGER_TEXT_IO}
26640 @tab @code{INTEGER_IO(SHORT_INTEGER)}
26641
26642 @item @code{SHORT_SHORT_INTEGER_TEXT_IO}
26643 @tab @code{INTEGER_IO(SHORT_SHORT_INTEGER)}
26644
26645 @item @code{FLOAT_TEXT_IO}
26646 @tab @code{FLOAT_IO(FLOAT)}
26647
26648 @item @code{LONG_FLOAT_TEXT_IO}
26649 @tab @code{FLOAT_IO(LONG_FLOAT)}
26650 @end multitable
26651
26652 @noindent
26653 The HP Ada predefined packages and their operations
26654 are implemented using OpenVMS Alpha files and input-output
26655 facilities. HP Ada supports asynchronous input-output on OpenVMS Alpha.
26656 Familiarity with the following is recommended:
26657 @itemize @bullet
26658 @item RMS file organizations and access methods
26659
26660 @item OpenVMS file specifications and directories
26661
26662 @item OpenVMS File Definition Language (FDL)
26663 @end itemize
26664
26665 @noindent
26666 GNAT provides I/O facilities that are completely
26667 compatible with HP Ada. The distribution includes the
26668 standard HP Ada versions of all I/O packages, operating
26669 in a manner compatible with HP Ada. In particular, the
26670 following packages are by default the HP Ada (Ada 83)
26671 versions of these packages rather than the renamings
26672 suggested in Annex J of the Ada Reference Manual:
26673 @itemize @bullet
26674 @item @code{TEXT_IO}
26675
26676 @item @code{SEQUENTIAL_IO}
26677
26678 @item @code{DIRECT_IO}
26679 @end itemize
26680
26681 @noindent
26682 The use of the standard child package syntax (for
26683 example, @code{ADA.TEXT_IO}) retrieves the post-Ada 83 versions of these
26684 packages.
26685 GNAT provides HP-compatible predefined instantiations
26686 of the @code{TEXT_IO} packages, and also
26687 provides the standard predefined instantiations required
26688 by the @cite{Ada Reference Manual}.
26689
26690 For further information on how GNAT interfaces to the file
26691 system or how I/O is implemented in programs written in
26692 mixed languages, see @ref{Implementation of the Standard I/O,,,
26693 gnat_rm, GNAT Reference Manual}.
26694 This chapter covers the following:
26695 @itemize @bullet
26696 @item Standard I/O packages
26697
26698 @item @code{FORM} strings
26699
26700 @item @code{ADA.DIRECT_IO}
26701
26702 @item @code{ADA.SEQUENTIAL_IO}
26703
26704 @item @code{ADA.TEXT_IO}
26705
26706 @item Stream pointer positioning
26707
26708 @item Reading and writing non-regular files
26709
26710 @item @code{GET_IMMEDIATE}
26711
26712 @item Treating @code{TEXT_IO} files as streams
26713
26714 @item Shared files
26715
26716 @item Open modes
26717 @end itemize
26718
26719 @node Implementation Limits
26720 @section Implementation Limits
26721
26722 @noindent
26723 The following table lists implementation limits for HP Ada
26724 and GNAT systems.
26725 @multitable @columnfractions .60 .20 .20
26726 @sp 1
26727 @item @emph{Compilation Parameter}
26728 @tab @emph{HP Ada}
26729 @tab @emph{GNAT}
26730 @sp 1
26731
26732 @item In a subprogram or entry declaration, maximum number of
26733 formal parameters that are of an unconstrained record type
26734 @tab 32
26735 @tab No set limit
26736 @sp 1
26737
26738 @item Maximum identifier length (number of characters)
26739 @tab 255
26740 @tab 32766
26741 @sp 1
26742
26743 @item Maximum number of characters in a source line
26744 @tab 255
26745 @tab 32766
26746 @sp 1
26747
26748 @item Maximum collection size (number of bytes)
26749 @tab 2**31-1
26750 @tab 2**31-1
26751 @sp 1
26752
26753 @item Maximum number of discriminants for a record type
26754 @tab 245
26755 @tab No set limit
26756 @sp 1
26757
26758 @item Maximum number of formal parameters in an entry or
26759 subprogram declaration
26760 @tab 246
26761 @tab No set limit
26762 @sp 1
26763
26764 @item Maximum number of dimensions in an array type
26765 @tab 255
26766 @tab No set limit
26767 @sp 1
26768
26769 @item Maximum number of library units and subunits in a compilation.
26770 @tab 4095
26771 @tab No set limit
26772 @sp 1
26773
26774 @item Maximum number of library units and subunits in an execution.
26775 @tab 16383
26776 @tab No set limit
26777 @sp 1
26778
26779 @item Maximum number of objects declared with the pragma @code{COMMON_OBJECT}
26780 or @code{PSECT_OBJECT}
26781 @tab 32757
26782 @tab No set limit
26783 @sp 1
26784
26785 @item Maximum number of enumeration literals in an enumeration type
26786 definition
26787 @tab 65535
26788 @tab No set limit
26789 @sp 1
26790
26791 @item Maximum number of lines in a source file
26792 @tab 65534
26793 @tab No set limit
26794 @sp 1
26795
26796 @item Maximum number of bits in any object
26797 @tab 2**31-1
26798 @tab 2**31-1
26799 @sp 1
26800
26801 @item Maximum size of the static portion of a stack frame (approximate)
26802 @tab 2**31-1
26803 @tab 2**31-1
26804 @end multitable
26805
26806 @node Tools and Utilities
26807 @section Tools and Utilities
26808
26809 @noindent
26810 The following table lists some of the OpenVMS development tools
26811 available for HP Ada, and the corresponding tools for
26812 use with @value{EDITION} on Alpha and I64 platforms.
26813 Aside from the debugger, all the OpenVMS tools identified are part
26814 of the DECset package.
26815
26816 @iftex
26817 @c Specify table in TeX since Texinfo does a poor job
26818 @tex
26819 \smallskip
26820 \smallskip
26821 \settabs\+Language-Sensitive Editor\quad
26822 &Product with HP Ada\quad
26823 &\cr
26824 \+\it Tool
26825 &\it Product with HP Ada
26826 & \it Product with GNAT Pro\cr
26827 \smallskip
26828 \+Code Management System
26829 &HP CMS
26830 & HP CMS\cr
26831 \smallskip
26832 \+Language-Sensitive Editor
26833 &HP LSE
26834 & emacs or HP LSE (Alpha)\cr
26835 \+
26836 &
26837 & HP LSE (I64)\cr
26838 \smallskip
26839 \+Debugger
26840 &OpenVMS Debug
26841 & gdb (Alpha),\cr
26842 \+
26843 &
26844 & OpenVMS Debug (I64)\cr
26845 \smallskip
26846 \+Source Code Analyzer /
26847 &HP SCA
26848 & GNAT XREF\cr
26849 \+Cross Referencer
26850 &
26851 &\cr
26852 \smallskip
26853 \+Test Manager
26854 &HP Digital Test
26855 & HP DTM\cr
26856 \+
26857 &Manager (DTM)
26858 &\cr
26859 \smallskip
26860 \+Performance and
26861 & HP PCA
26862 & HP PCA\cr
26863 \+Coverage Analyzer
26864 &
26865 &\cr
26866 \smallskip
26867 \+Module Management
26868 & HP MMS
26869 & Not applicable\cr
26870 \+ System
26871 &
26872 &\cr
26873 \smallskip
26874 \smallskip
26875 @end tex
26876 @end iftex
26877
26878 @ifnottex
26879 @c This is the Texinfo version of the table. It renders poorly in pdf, hence
26880 @c the TeX version above for the printed version
26881 @flushleft
26882 @c @multitable @columnfractions .3 .4 .4
26883 @multitable {Source Code Analyzer /}{Tool with HP Ada}{Tool with GNAT Pro}
26884 @item @i{Tool}
26885 @tab @i{Tool with HP Ada}
26886 @tab @i{Tool with @value{EDITION}}
26887 @item Code Management@*System
26888 @tab HP CMS
26889 @tab HP CMS
26890 @item Language-Sensitive@*Editor
26891 @tab HP LSE
26892 @tab emacs or HP LSE (Alpha)
26893 @item
26894 @tab
26895 @tab HP LSE (I64)
26896 @item Debugger
26897 @tab OpenVMS Debug
26898 @tab gdb (Alpha),
26899 @item
26900 @tab
26901 @tab OpenVMS Debug (I64)
26902 @item Source Code Analyzer /@*Cross Referencer
26903 @tab HP SCA
26904 @tab GNAT XREF
26905 @item Test Manager
26906 @tab HP Digital Test@*Manager (DTM)
26907 @tab HP DTM
26908 @item Performance and@*Coverage Analyzer
26909 @tab HP PCA
26910 @tab HP PCA
26911 @item Module Management@*System
26912 @tab HP MMS
26913 @tab Not applicable
26914 @end multitable
26915 @end flushleft
26916 @end ifnottex
26917
26918 @end ifset
26919
26920 @c **************************************
26921 @node Platform-Specific Information for the Run-Time Libraries
26922 @appendix Platform-Specific Information for the Run-Time Libraries
26923 @cindex Tasking and threads libraries
26924 @cindex Threads libraries and tasking
26925 @cindex Run-time libraries (platform-specific information)
26926
26927 @noindent
26928 The GNAT run-time implementation may vary with respect to both the
26929 underlying threads library and the exception handling scheme.
26930 For threads support, one or more of the following are supplied:
26931 @itemize @bullet
26932 @item @b{native threads library}, a binding to the thread package from
26933 the underlying operating system
26934
26935 @item @b{pthreads library} (Sparc Solaris only), a binding to the Solaris
26936 POSIX thread package
26937 @end itemize
26938
26939 @noindent
26940 For exception handling, either or both of two models are supplied:
26941 @itemize @bullet
26942 @item @b{Zero-Cost Exceptions} (``ZCX''),@footnote{
26943 Most programs should experience a substantial speed improvement by
26944 being compiled with a ZCX run-time.
26945 This is especially true for
26946 tasking applications or applications with many exception handlers.}
26947 @cindex Zero-Cost Exceptions
26948 @cindex ZCX (Zero-Cost Exceptions)
26949 which uses binder-generated tables that
26950 are interrogated at run time to locate a handler
26951
26952 @item @b{setjmp / longjmp} (``SJLJ''),
26953 @cindex setjmp/longjmp Exception Model
26954 @cindex SJLJ (setjmp/longjmp Exception Model)
26955 which uses dynamically-set data to establish
26956 the set of handlers
26957 @end itemize
26958
26959 @noindent
26960 This appendix summarizes which combinations of threads and exception support
26961 are supplied on various GNAT platforms.
26962 It then shows how to select a particular library either
26963 permanently or temporarily,
26964 explains the properties of (and tradeoffs among) the various threads
26965 libraries, and provides some additional
26966 information about several specific platforms.
26967
26968 @menu
26969 * Summary of Run-Time Configurations::
26970 * Specifying a Run-Time Library::
26971 * Choosing the Scheduling Policy::
26972 * Solaris-Specific Considerations::
26973 * Linux-Specific Considerations::
26974 * AIX-Specific Considerations::
26975 * Irix-Specific Considerations::
26976 * RTX-Specific Considerations::
26977 * HP-UX-Specific Considerations::
26978 @end menu
26979
26980 @node Summary of Run-Time Configurations
26981 @section Summary of Run-Time Configurations
26982
26983 @multitable @columnfractions .30 .70
26984 @item @b{alpha-openvms}
26985 @item @code{@ @ }@i{rts-native (default)}
26986 @item @code{@ @ @ @ }Tasking @tab native VMS threads
26987 @item @code{@ @ @ @ }Exceptions @tab ZCX
26988 @*
26989 @item @b{alpha-tru64}
26990 @item @code{@ @ }@i{rts-native (default)}
26991 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
26992 @item @code{@ @ @ @ }Exceptions @tab ZCX
26993 @*
26994 @item @code{@ @ }@i{rts-sjlj}
26995 @item @code{@ @ @ @ }Tasking @tab native TRU64 threads
26996 @item @code{@ @ @ @ }Exceptions @tab SJLJ
26997 @*
26998 @item @b{ia64-hp_linux}
26999 @item @code{@ @ }@i{rts-native (default)}
27000 @item @code{@ @ @ @ }Tasking @tab pthread library
27001 @item @code{@ @ @ @ }Exceptions @tab ZCX
27002 @*
27003 @item @b{ia64-hpux}
27004 @item @code{@ @ }@i{rts-native (default)}
27005 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
27006 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27007 @*
27008 @item @b{ia64-openvms}
27009 @item @code{@ @ }@i{rts-native (default)}
27010 @item @code{@ @ @ @ }Tasking @tab native VMS threads
27011 @item @code{@ @ @ @ }Exceptions @tab ZCX
27012 @*
27013 @item @b{ia64-sgi_linux}
27014 @item @code{@ @ }@i{rts-native (default)}
27015 @item @code{@ @ @ @ }Tasking @tab pthread library
27016 @item @code{@ @ @ @ }Exceptions @tab ZCX
27017 @*
27018 @item @b{mips-irix}
27019 @item @code{@ @ }@i{rts-native (default)}
27020 @item @code{@ @ @ @ }Tasking @tab native IRIX threads
27021 @item @code{@ @ @ @ }Exceptions @tab ZCX
27022 @*
27023 @item @b{pa-hpux}
27024 @item @code{@ @ }@i{rts-native (default)}
27025 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
27026 @item @code{@ @ @ @ }Exceptions @tab ZCX
27027 @*
27028 @item @code{@ @ }@i{rts-sjlj}
27029 @item @code{@ @ @ @ }Tasking @tab native HP-UX threads
27030 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27031 @*
27032 @item @b{ppc-aix}
27033 @item @code{@ @ }@i{rts-native (default)}
27034 @item @code{@ @ @ @ }Tasking @tab native AIX threads
27035 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27036 @*
27037 @item @b{ppc-darwin}
27038 @item @code{@ @ }@i{rts-native (default)}
27039 @item @code{@ @ @ @ }Tasking @tab native MacOS threads
27040 @item @code{@ @ @ @ }Exceptions @tab ZCX
27041 @*
27042 @item @b{sparc-solaris} @tab
27043 @item @code{@ @ }@i{rts-native (default)}
27044 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
27045 @item @code{@ @ @ @ }Exceptions @tab ZCX
27046 @*
27047 @item @code{@ @ }@i{rts-pthread}
27048 @item @code{@ @ @ @ }Tasking @tab pthread library
27049 @item @code{@ @ @ @ }Exceptions @tab ZCX
27050 @*
27051 @item @code{@ @ }@i{rts-sjlj}
27052 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
27053 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27054 @*
27055 @item @b{sparc64-solaris} @tab
27056 @item @code{@ @ }@i{rts-native (default)}
27057 @item @code{@ @ @ @ }Tasking @tab native Solaris threads library
27058 @item @code{@ @ @ @ }Exceptions @tab ZCX
27059 @*
27060 @item @b{x86-linux}
27061 @item @code{@ @ }@i{rts-native (default)}
27062 @item @code{@ @ @ @ }Tasking @tab pthread library
27063 @item @code{@ @ @ @ }Exceptions @tab ZCX
27064 @*
27065 @item @code{@ @ }@i{rts-sjlj}
27066 @item @code{@ @ @ @ }Tasking @tab pthread library
27067 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27068 @*
27069 @item @b{x86-lynx}
27070 @item @code{@ @ }@i{rts-native (default)}
27071 @item @code{@ @ @ @ }Tasking @tab native LynxOS threads
27072 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27073 @*
27074 @item @b{x86-solaris}
27075 @item @code{@ @ }@i{rts-native (default)}
27076 @item @code{@ @ @ @ }Tasking @tab native Solaris threads
27077 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27078 @*
27079 @item @b{x86-windows}
27080 @item @code{@ @ }@i{rts-native (default)}
27081 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
27082 @item @code{@ @ @ @ }Exceptions @tab ZCX
27083 @*
27084 @item @code{@ @ }@i{rts-sjlj (default)}
27085 @item @code{@ @ @ @ }Tasking @tab native Win32 threads
27086 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27087 @*
27088 @item @b{x86-windows-rtx}
27089 @item @code{@ @ }@i{rts-rtx-rtss (default)}
27090 @item @code{@ @ @ @ }Tasking @tab RTX real-time subsystem RTSS threads (kernel mode)
27091 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27092 @*
27093 @item @code{@ @ }@i{rts-rtx-w32}
27094 @item @code{@ @ @ @ }Tasking @tab RTX Win32 threads (user mode)
27095 @item @code{@ @ @ @ }Exceptions @tab ZCX
27096 @*
27097 @item @b{x86_64-linux}
27098 @item @code{@ @ }@i{rts-native (default)}
27099 @item @code{@ @ @ @ }Tasking @tab pthread library
27100 @item @code{@ @ @ @ }Exceptions @tab ZCX
27101 @*
27102 @item @code{@ @ }@i{rts-sjlj}
27103 @item @code{@ @ @ @ }Tasking @tab pthread library
27104 @item @code{@ @ @ @ }Exceptions @tab SJLJ
27105 @*
27106 @end multitable
27107
27108 @node Specifying a Run-Time Library
27109 @section Specifying a Run-Time Library
27110
27111 @noindent
27112 The @file{adainclude} subdirectory containing the sources of the GNAT
27113 run-time library, and the @file{adalib} subdirectory containing the
27114 @file{ALI} files and the static and/or shared GNAT library, are located
27115 in the gcc target-dependent area:
27116
27117 @smallexample
27118 target=$prefix/lib/gcc/gcc-@i{dumpmachine}/gcc-@i{dumpversion}/
27119 @end smallexample
27120
27121 @noindent
27122 As indicated above, on some platforms several run-time libraries are supplied.
27123 These libraries are installed in the target dependent area and
27124 contain a complete source and binary subdirectory. The detailed description
27125 below explains the differences between the different libraries in terms of
27126 their thread support.
27127
27128 The default run-time library (when GNAT is installed) is @emph{rts-native}.
27129 This default run time is selected by the means of soft links.
27130 For example on x86-linux:
27131
27132 @smallexample
27133 @group
27134 $(target-dir)
27135 |
27136 +--- adainclude----------+
27137 | |
27138 +--- adalib-----------+ |
27139 | | |
27140 +--- rts-native | |
27141 | | | |
27142 | +--- adainclude <---+
27143 | | |
27144 | +--- adalib <----+
27145 |
27146 +--- rts-sjlj
27147 |
27148 +--- adainclude
27149 |
27150 +--- adalib
27151 @end group
27152 @end smallexample
27153
27154 @noindent
27155 If the @i{rts-sjlj} library is to be selected on a permanent basis,
27156 these soft links can be modified with the following commands:
27157
27158 @smallexample
27159 $ cd $target
27160 $ rm -f adainclude adalib
27161 $ ln -s rts-sjlj/adainclude adainclude
27162 $ ln -s rts-sjlj/adalib adalib
27163 @end smallexample
27164
27165 @noindent
27166 Alternatively, you can specify @file{rts-sjlj/adainclude} in the file
27167 @file{$target/ada_source_path} and @file{rts-sjlj/adalib} in
27168 @file{$target/ada_object_path}.
27169
27170 Selecting another run-time library temporarily can be
27171 achieved by using the @option{--RTS} switch, e.g., @option{--RTS=sjlj}
27172 @cindex @option{--RTS} option
27173
27174 @node Choosing the Scheduling Policy
27175 @section Choosing the Scheduling Policy
27176
27177 @noindent
27178 When using a POSIX threads implementation, you have a choice of several
27179 scheduling policies: @code{SCHED_FIFO},
27180 @cindex @code{SCHED_FIFO} scheduling policy
27181 @code{SCHED_RR}
27182 @cindex @code{SCHED_RR} scheduling policy
27183 and @code{SCHED_OTHER}.
27184 @cindex @code{SCHED_OTHER} scheduling policy
27185 Typically, the default is @code{SCHED_OTHER}, while using @code{SCHED_FIFO}
27186 or @code{SCHED_RR} requires special (e.g., root) privileges.
27187
27188 By default, GNAT uses the @code{SCHED_OTHER} policy. To specify
27189 @code{SCHED_FIFO},
27190 @cindex @code{SCHED_FIFO} scheduling policy
27191 you can use one of the following:
27192
27193 @itemize @bullet
27194 @item
27195 @code{pragma Time_Slice (0.0)}
27196 @cindex pragma Time_Slice
27197 @item
27198 the corresponding binder option @option{-T0}
27199 @cindex @option{-T0} option
27200 @item
27201 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
27202 @cindex pragma Task_Dispatching_Policy
27203 @end itemize
27204
27205 @noindent
27206 To specify @code{SCHED_RR},
27207 @cindex @code{SCHED_RR} scheduling policy
27208 you should use @code{pragma Time_Slice} with a
27209 value greater than @code{0.0}, or else use the corresponding @option{-T}
27210 binder option.
27211
27212 @node Solaris-Specific Considerations
27213 @section Solaris-Specific Considerations
27214 @cindex Solaris Sparc threads libraries
27215
27216 @noindent
27217 This section addresses some topics related to the various threads libraries
27218 on Sparc Solaris.
27219
27220 @menu
27221 * Solaris Threads Issues::
27222 @end menu
27223
27224 @node Solaris Threads Issues
27225 @subsection Solaris Threads Issues
27226
27227 @noindent
27228 GNAT under Solaris/Sparc 32 bits comes with an alternate tasking run-time
27229 library based on POSIX threads --- @emph{rts-pthread}.
27230 @cindex rts-pthread threads library
27231 This run-time library has the advantage of being mostly shared across all
27232 POSIX-compliant thread implementations, and it also provides under
27233 @w{Solaris 8} the @code{PTHREAD_PRIO_INHERIT}
27234 @cindex @code{PTHREAD_PRIO_INHERIT} policy (under rts-pthread)
27235 and @code{PTHREAD_PRIO_PROTECT}
27236 @cindex @code{PTHREAD_PRIO_PROTECT} policy (under rts-pthread)
27237 semantics that can be selected using the predefined pragma
27238 @code{Locking_Policy}
27239 @cindex pragma Locking_Policy (under rts-pthread)
27240 with respectively
27241 @code{Inheritance_Locking} and @code{Ceiling_Locking} as the policy.
27242 @cindex @code{Inheritance_Locking} (under rts-pthread)
27243 @cindex @code{Ceiling_Locking} (under rts-pthread)
27244
27245 As explained above, the native run-time library is based on the Solaris thread
27246 library (@code{libthread}) and is the default library.
27247
27248 When the Solaris threads library is used (this is the default), programs
27249 compiled with GNAT can automatically take advantage of
27250 and can thus execute on multiple processors.
27251 The user can alternatively specify a processor on which the program should run
27252 to emulate a single-processor system. The multiprocessor / uniprocessor choice
27253 is made by
27254 setting the environment variable @env{GNAT_PROCESSOR}
27255 @cindex @env{GNAT_PROCESSOR} environment variable (on Sparc Solaris)
27256 to one of the following:
27257
27258 @table @code
27259 @item -2
27260 Use the default configuration (run the program on all
27261 available processors) - this is the same as having @code{GNAT_PROCESSOR}
27262 unset
27263
27264 @item -1
27265 Let the run-time implementation choose one processor and run the program on
27266 that processor
27267
27268 @item 0 .. Last_Proc
27269 Run the program on the specified processor.
27270 @code{Last_Proc} is equal to @code{_SC_NPROCESSORS_CONF - 1}
27271 (where @code{_SC_NPROCESSORS_CONF} is a system variable).
27272 @end table
27273
27274 @node Linux-Specific Considerations
27275 @section Linux-Specific Considerations
27276 @cindex Linux threads libraries
27277
27278 @noindent
27279 On GNU/Linux without NPTL support (usually system with GNU C Library
27280 older than 2.3), the signal model is not POSIX compliant, which means
27281 that to send a signal to the process, you need to send the signal to all
27282 threads, e.g.@: by using @code{killpg()}.
27283
27284 @node AIX-Specific Considerations
27285 @section AIX-Specific Considerations
27286 @cindex AIX resolver library
27287
27288 @noindent
27289 On AIX, the resolver library initializes some internal structure on
27290 the first call to @code{get*by*} functions, which are used to implement
27291 @code{GNAT.Sockets.Get_Host_By_Name} and
27292 @code{GNAT.Sockets.Get_Host_By_Address}.
27293 If such initialization occurs within an Ada task, and the stack size for
27294 the task is the default size, a stack overflow may occur.
27295
27296 To avoid this overflow, the user should either ensure that the first call
27297 to @code{GNAT.Sockets.Get_Host_By_Name} or
27298 @code{GNAT.Sockets.Get_Host_By_Addrss}
27299 occurs in the environment task, or use @code{pragma Storage_Size} to
27300 specify a sufficiently large size for the stack of the task that contains
27301 this call.
27302
27303 @node Irix-Specific Considerations
27304 @section Irix-Specific Considerations
27305 @cindex Irix libraries
27306
27307 @noindent
27308 The GCC support libraries coming with the Irix compiler have moved to
27309 their canonical place with respect to the general Irix ABI related
27310 conventions. Running applications built with the default shared GNAT
27311 run-time now requires the LD_LIBRARY_PATH environment variable to
27312 include this location. A possible way to achieve this is to issue the
27313 following command line on a bash prompt:
27314
27315 @smallexample
27316 @group
27317 $ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`dirname \`gcc --print-file-name=libgcc_s.so\``
27318 @end group
27319 @end smallexample
27320
27321 @node RTX-Specific Considerations
27322 @section RTX-Specific Considerations
27323 @cindex RTX libraries
27324
27325 @noindent
27326 The Real-time Extension (RTX) to Windows is based on the Windows Win32
27327 API. Applications can be built to work in two different modes:
27328
27329 @itemize @bullet
27330 @item
27331 Windows executables that run in Ring 3 to utilize memory protection
27332 (@emph{rts-rtx-w32}).
27333
27334 @item
27335 Real-time subsystem (RTSS) executables that run in Ring 0, where
27336 performance can be optimized with RTSS applications taking precedent
27337 over all Windows applications (@emph{rts-rtx-rtss}).
27338
27339 @end itemize
27340
27341 @node HP-UX-Specific Considerations
27342 @section HP-UX-Specific Considerations
27343 @cindex HP-UX Scheduling
27344
27345 @noindent
27346 On HP-UX, appropriate privileges are required to change the scheduling
27347 parameters of a task. The calling process must have appropriate
27348 privileges or be a member of a group having @code{PRIV_RTSCHED} access to
27349 successfully change the scheduling parameters.
27350
27351 By default, GNAT uses the @code{SCHED_HPUX} policy. To have access to the
27352 priority range 0-31 either the @code{FIFO_Within_Priorities} or the
27353 @code{Round_Robin_Within_Priorities} scheduling policies need to be set.
27354
27355 To specify the @code{FIFO_Within_Priorities} scheduling policy you can use
27356 one of the following:
27357
27358 @itemize @bullet
27359 @item
27360 @code{pragma Time_Slice (0.0)}
27361 @cindex pragma Time_Slice
27362 @item
27363 the corresponding binder option @option{-T0}
27364 @cindex @option{-T0} option
27365 @item
27366 @code{pragma Task_Dispatching_Policy (FIFO_Within_Priorities)}
27367 @cindex pragma Task_Dispatching_Policy
27368 @end itemize
27369
27370 @noindent
27371 To specify the @code{Round_Robin_Within_Priorities}, scheduling policy
27372 you should use @code{pragma Time_Slice} with a
27373 value greater than @code{0.0}, or use the corresponding @option{-T}
27374 binder option, or set the @code{pragma Task_Dispatching_Policy
27375 (Round_Robin_Within_Priorities)}.
27376
27377 @c *******************************
27378 @node Example of Binder Output File
27379 @appendix Example of Binder Output File
27380
27381 @noindent
27382 This Appendix displays the source code for @command{gnatbind}'s output
27383 file generated for a simple ``Hello World'' program.
27384 Comments have been added for clarification purposes.
27385
27386 @smallexample @c adanocomment
27387 @iftex
27388 @leftskip=0cm
27389 @end iftex
27390 -- The package is called Ada_Main unless this name is actually used
27391 -- as a unit name in the partition, in which case some other unique
27392 -- name is used.
27393
27394 with System;
27395 package ada_main is
27396
27397 Elab_Final_Code : Integer;
27398 pragma Import (C, Elab_Final_Code, "__gnat_inside_elab_final_code");
27399
27400 -- The main program saves the parameters (argument count,
27401 -- argument values, environment pointer) in global variables
27402 -- for later access by other units including
27403 -- Ada.Command_Line.
27404
27405 gnat_argc : Integer;
27406 gnat_argv : System.Address;
27407 gnat_envp : System.Address;
27408
27409 -- The actual variables are stored in a library routine. This
27410 -- is useful for some shared library situations, where there
27411 -- are problems if variables are not in the library.
27412
27413 pragma Import (C, gnat_argc);
27414 pragma Import (C, gnat_argv);
27415 pragma Import (C, gnat_envp);
27416
27417 -- The exit status is similarly an external location
27418
27419 gnat_exit_status : Integer;
27420 pragma Import (C, gnat_exit_status);
27421
27422 GNAT_Version : constant String :=
27423 "GNAT Version: 6.0.0w (20061115)";
27424 pragma Export (C, GNAT_Version, "__gnat_version");
27425
27426 -- This is the generated adafinal routine that performs
27427 -- finalization at the end of execution. In the case where
27428 -- Ada is the main program, this main program makes a call
27429 -- to adafinal at program termination.
27430
27431 procedure adafinal;
27432 pragma Export (C, adafinal, "adafinal");
27433
27434 -- This is the generated adainit routine that performs
27435 -- initialization at the start of execution. In the case
27436 -- where Ada is the main program, this main program makes
27437 -- a call to adainit at program startup.
27438
27439 procedure adainit;
27440 pragma Export (C, adainit, "adainit");
27441
27442 -- This routine is called at the start of execution. It is
27443 -- a dummy routine that is used by the debugger to breakpoint
27444 -- at the start of execution.
27445
27446 procedure Break_Start;
27447 pragma Import (C, Break_Start, "__gnat_break_start");
27448
27449 -- This is the actual generated main program (it would be
27450 -- suppressed if the no main program switch were used). As
27451 -- required by standard system conventions, this program has
27452 -- the external name main.
27453
27454 function main
27455 (argc : Integer;
27456 argv : System.Address;
27457 envp : System.Address)
27458 return Integer;
27459 pragma Export (C, main, "main");
27460
27461 -- The following set of constants give the version
27462 -- identification values for every unit in the bound
27463 -- partition. This identification is computed from all
27464 -- dependent semantic units, and corresponds to the
27465 -- string that would be returned by use of the
27466 -- Body_Version or Version attributes.
27467
27468 type Version_32 is mod 2 ** 32;
27469 u00001 : constant Version_32 := 16#7880BEB3#;
27470 u00002 : constant Version_32 := 16#0D24CBD0#;
27471 u00003 : constant Version_32 := 16#3283DBEB#;
27472 u00004 : constant Version_32 := 16#2359F9ED#;
27473 u00005 : constant Version_32 := 16#664FB847#;
27474 u00006 : constant Version_32 := 16#68E803DF#;
27475 u00007 : constant Version_32 := 16#5572E604#;
27476 u00008 : constant Version_32 := 16#46B173D8#;
27477 u00009 : constant Version_32 := 16#156A40CF#;
27478 u00010 : constant Version_32 := 16#033DABE0#;
27479 u00011 : constant Version_32 := 16#6AB38FEA#;
27480 u00012 : constant Version_32 := 16#22B6217D#;
27481 u00013 : constant Version_32 := 16#68A22947#;
27482 u00014 : constant Version_32 := 16#18CC4A56#;
27483 u00015 : constant Version_32 := 16#08258E1B#;
27484 u00016 : constant Version_32 := 16#367D5222#;
27485 u00017 : constant Version_32 := 16#20C9ECA4#;
27486 u00018 : constant Version_32 := 16#50D32CB6#;
27487 u00019 : constant Version_32 := 16#39A8BB77#;
27488 u00020 : constant Version_32 := 16#5CF8FA2B#;
27489 u00021 : constant Version_32 := 16#2F1EB794#;
27490 u00022 : constant Version_32 := 16#31AB6444#;
27491 u00023 : constant Version_32 := 16#1574B6E9#;
27492 u00024 : constant Version_32 := 16#5109C189#;
27493 u00025 : constant Version_32 := 16#56D770CD#;
27494 u00026 : constant Version_32 := 16#02F9DE3D#;
27495 u00027 : constant Version_32 := 16#08AB6B2C#;
27496 u00028 : constant Version_32 := 16#3FA37670#;
27497 u00029 : constant Version_32 := 16#476457A0#;
27498 u00030 : constant Version_32 := 16#731E1B6E#;
27499 u00031 : constant Version_32 := 16#23C2E789#;
27500 u00032 : constant Version_32 := 16#0F1BD6A1#;
27501 u00033 : constant Version_32 := 16#7C25DE96#;
27502 u00034 : constant Version_32 := 16#39ADFFA2#;
27503 u00035 : constant Version_32 := 16#571DE3E7#;
27504 u00036 : constant Version_32 := 16#5EB646AB#;
27505 u00037 : constant Version_32 := 16#4249379B#;
27506 u00038 : constant Version_32 := 16#0357E00A#;
27507 u00039 : constant Version_32 := 16#3784FB72#;
27508 u00040 : constant Version_32 := 16#2E723019#;
27509 u00041 : constant Version_32 := 16#623358EA#;
27510 u00042 : constant Version_32 := 16#107F9465#;
27511 u00043 : constant Version_32 := 16#6843F68A#;
27512 u00044 : constant Version_32 := 16#63305874#;
27513 u00045 : constant Version_32 := 16#31E56CE1#;
27514 u00046 : constant Version_32 := 16#02917970#;
27515 u00047 : constant Version_32 := 16#6CCBA70E#;
27516 u00048 : constant Version_32 := 16#41CD4204#;
27517 u00049 : constant Version_32 := 16#572E3F58#;
27518 u00050 : constant Version_32 := 16#20729FF5#;
27519 u00051 : constant Version_32 := 16#1D4F93E8#;
27520 u00052 : constant Version_32 := 16#30B2EC3D#;
27521 u00053 : constant Version_32 := 16#34054F96#;
27522 u00054 : constant Version_32 := 16#5A199860#;
27523 u00055 : constant Version_32 := 16#0E7F912B#;
27524 u00056 : constant Version_32 := 16#5760634A#;
27525 u00057 : constant Version_32 := 16#5D851835#;
27526
27527 -- The following Export pragmas export the version numbers
27528 -- with symbolic names ending in B (for body) or S
27529 -- (for spec) so that they can be located in a link. The
27530 -- information provided here is sufficient to track down
27531 -- the exact versions of units used in a given build.
27532
27533 pragma Export (C, u00001, "helloB");
27534 pragma Export (C, u00002, "system__standard_libraryB");
27535 pragma Export (C, u00003, "system__standard_libraryS");
27536 pragma Export (C, u00004, "adaS");
27537 pragma Export (C, u00005, "ada__text_ioB");
27538 pragma Export (C, u00006, "ada__text_ioS");
27539 pragma Export (C, u00007, "ada__exceptionsB");
27540 pragma Export (C, u00008, "ada__exceptionsS");
27541 pragma Export (C, u00009, "gnatS");
27542 pragma Export (C, u00010, "gnat__heap_sort_aB");
27543 pragma Export (C, u00011, "gnat__heap_sort_aS");
27544 pragma Export (C, u00012, "systemS");
27545 pragma Export (C, u00013, "system__exception_tableB");
27546 pragma Export (C, u00014, "system__exception_tableS");
27547 pragma Export (C, u00015, "gnat__htableB");
27548 pragma Export (C, u00016, "gnat__htableS");
27549 pragma Export (C, u00017, "system__exceptionsS");
27550 pragma Export (C, u00018, "system__machine_state_operationsB");
27551 pragma Export (C, u00019, "system__machine_state_operationsS");
27552 pragma Export (C, u00020, "system__machine_codeS");
27553 pragma Export (C, u00021, "system__storage_elementsB");
27554 pragma Export (C, u00022, "system__storage_elementsS");
27555 pragma Export (C, u00023, "system__secondary_stackB");
27556 pragma Export (C, u00024, "system__secondary_stackS");
27557 pragma Export (C, u00025, "system__parametersB");
27558 pragma Export (C, u00026, "system__parametersS");
27559 pragma Export (C, u00027, "system__soft_linksB");
27560 pragma Export (C, u00028, "system__soft_linksS");
27561 pragma Export (C, u00029, "system__stack_checkingB");
27562 pragma Export (C, u00030, "system__stack_checkingS");
27563 pragma Export (C, u00031, "system__tracebackB");
27564 pragma Export (C, u00032, "system__tracebackS");
27565 pragma Export (C, u00033, "ada__streamsS");
27566 pragma Export (C, u00034, "ada__tagsB");
27567 pragma Export (C, u00035, "ada__tagsS");
27568 pragma Export (C, u00036, "system__string_opsB");
27569 pragma Export (C, u00037, "system__string_opsS");
27570 pragma Export (C, u00038, "interfacesS");
27571 pragma Export (C, u00039, "interfaces__c_streamsB");
27572 pragma Export (C, u00040, "interfaces__c_streamsS");
27573 pragma Export (C, u00041, "system__file_ioB");
27574 pragma Export (C, u00042, "system__file_ioS");
27575 pragma Export (C, u00043, "ada__finalizationB");
27576 pragma Export (C, u00044, "ada__finalizationS");
27577 pragma Export (C, u00045, "system__finalization_rootB");
27578 pragma Export (C, u00046, "system__finalization_rootS");
27579 pragma Export (C, u00047, "system__finalization_implementationB");
27580 pragma Export (C, u00048, "system__finalization_implementationS");
27581 pragma Export (C, u00049, "system__string_ops_concat_3B");
27582 pragma Export (C, u00050, "system__string_ops_concat_3S");
27583 pragma Export (C, u00051, "system__stream_attributesB");
27584 pragma Export (C, u00052, "system__stream_attributesS");
27585 pragma Export (C, u00053, "ada__io_exceptionsS");
27586 pragma Export (C, u00054, "system__unsigned_typesS");
27587 pragma Export (C, u00055, "system__file_control_blockS");
27588 pragma Export (C, u00056, "ada__finalization__list_controllerB");
27589 pragma Export (C, u00057, "ada__finalization__list_controllerS");
27590
27591 -- BEGIN ELABORATION ORDER
27592 -- ada (spec)
27593 -- gnat (spec)
27594 -- gnat.heap_sort_a (spec)
27595 -- gnat.heap_sort_a (body)
27596 -- gnat.htable (spec)
27597 -- gnat.htable (body)
27598 -- interfaces (spec)
27599 -- system (spec)
27600 -- system.machine_code (spec)
27601 -- system.parameters (spec)
27602 -- system.parameters (body)
27603 -- interfaces.c_streams (spec)
27604 -- interfaces.c_streams (body)
27605 -- system.standard_library (spec)
27606 -- ada.exceptions (spec)
27607 -- system.exception_table (spec)
27608 -- system.exception_table (body)
27609 -- ada.io_exceptions (spec)
27610 -- system.exceptions (spec)
27611 -- system.storage_elements (spec)
27612 -- system.storage_elements (body)
27613 -- system.machine_state_operations (spec)
27614 -- system.machine_state_operations (body)
27615 -- system.secondary_stack (spec)
27616 -- system.stack_checking (spec)
27617 -- system.soft_links (spec)
27618 -- system.soft_links (body)
27619 -- system.stack_checking (body)
27620 -- system.secondary_stack (body)
27621 -- system.standard_library (body)
27622 -- system.string_ops (spec)
27623 -- system.string_ops (body)
27624 -- ada.tags (spec)
27625 -- ada.tags (body)
27626 -- ada.streams (spec)
27627 -- system.finalization_root (spec)
27628 -- system.finalization_root (body)
27629 -- system.string_ops_concat_3 (spec)
27630 -- system.string_ops_concat_3 (body)
27631 -- system.traceback (spec)
27632 -- system.traceback (body)
27633 -- ada.exceptions (body)
27634 -- system.unsigned_types (spec)
27635 -- system.stream_attributes (spec)
27636 -- system.stream_attributes (body)
27637 -- system.finalization_implementation (spec)
27638 -- system.finalization_implementation (body)
27639 -- ada.finalization (spec)
27640 -- ada.finalization (body)
27641 -- ada.finalization.list_controller (spec)
27642 -- ada.finalization.list_controller (body)
27643 -- system.file_control_block (spec)
27644 -- system.file_io (spec)
27645 -- system.file_io (body)
27646 -- ada.text_io (spec)
27647 -- ada.text_io (body)
27648 -- hello (body)
27649 -- END ELABORATION ORDER
27650
27651 end ada_main;
27652
27653 -- The following source file name pragmas allow the generated file
27654 -- names to be unique for different main programs. They are needed
27655 -- since the package name will always be Ada_Main.
27656
27657 pragma Source_File_Name (ada_main, Spec_File_Name => "b~hello.ads");
27658 pragma Source_File_Name (ada_main, Body_File_Name => "b~hello.adb");
27659
27660 -- Generated package body for Ada_Main starts here
27661
27662 package body ada_main is
27663
27664 -- The actual finalization is performed by calling the
27665 -- library routine in System.Standard_Library.Adafinal
27666
27667 procedure Do_Finalize;
27668 pragma Import (C, Do_Finalize, "system__standard_library__adafinal");
27669
27670 -------------
27671 -- adainit --
27672 -------------
27673
27674 @findex adainit
27675 procedure adainit is
27676
27677 -- These booleans are set to True once the associated unit has
27678 -- been elaborated. It is also used to avoid elaborating the
27679 -- same unit twice.
27680
27681 E040 : Boolean;
27682 pragma Import (Ada, E040, "interfaces__c_streams_E");
27683
27684 E008 : Boolean;
27685 pragma Import (Ada, E008, "ada__exceptions_E");
27686
27687 E014 : Boolean;
27688 pragma Import (Ada, E014, "system__exception_table_E");
27689
27690 E053 : Boolean;
27691 pragma Import (Ada, E053, "ada__io_exceptions_E");
27692
27693 E017 : Boolean;
27694 pragma Import (Ada, E017, "system__exceptions_E");
27695
27696 E024 : Boolean;
27697 pragma Import (Ada, E024, "system__secondary_stack_E");
27698
27699 E030 : Boolean;
27700 pragma Import (Ada, E030, "system__stack_checking_E");
27701
27702 E028 : Boolean;
27703 pragma Import (Ada, E028, "system__soft_links_E");
27704
27705 E035 : Boolean;
27706 pragma Import (Ada, E035, "ada__tags_E");
27707
27708 E033 : Boolean;
27709 pragma Import (Ada, E033, "ada__streams_E");
27710
27711 E046 : Boolean;
27712 pragma Import (Ada, E046, "system__finalization_root_E");
27713
27714 E048 : Boolean;
27715 pragma Import (Ada, E048, "system__finalization_implementation_E");
27716
27717 E044 : Boolean;
27718 pragma Import (Ada, E044, "ada__finalization_E");
27719
27720 E057 : Boolean;
27721 pragma Import (Ada, E057, "ada__finalization__list_controller_E");
27722
27723 E055 : Boolean;
27724 pragma Import (Ada, E055, "system__file_control_block_E");
27725
27726 E042 : Boolean;
27727 pragma Import (Ada, E042, "system__file_io_E");
27728
27729 E006 : Boolean;
27730 pragma Import (Ada, E006, "ada__text_io_E");
27731
27732 -- Set_Globals is a library routine that stores away the
27733 -- value of the indicated set of global values in global
27734 -- variables within the library.
27735
27736 procedure Set_Globals
27737 (Main_Priority : Integer;
27738 Time_Slice_Value : Integer;
27739 WC_Encoding : Character;
27740 Locking_Policy : Character;
27741 Queuing_Policy : Character;
27742 Task_Dispatching_Policy : Character;
27743 Adafinal : System.Address;
27744 Unreserve_All_Interrupts : Integer;
27745 Exception_Tracebacks : Integer);
27746 @findex __gnat_set_globals
27747 pragma Import (C, Set_Globals, "__gnat_set_globals");
27748
27749 -- SDP_Table_Build is a library routine used to build the
27750 -- exception tables. See unit Ada.Exceptions in files
27751 -- a-except.ads/adb for full details of how zero cost
27752 -- exception handling works. This procedure, the call to
27753 -- it, and the two following tables are all omitted if the
27754 -- build is in longjmp/setjmp exception mode.
27755
27756 @findex SDP_Table_Build
27757 @findex Zero Cost Exceptions
27758 procedure SDP_Table_Build
27759 (SDP_Addresses : System.Address;
27760 SDP_Count : Natural;
27761 Elab_Addresses : System.Address;
27762 Elab_Addr_Count : Natural);
27763 pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
27764
27765 -- Table of Unit_Exception_Table addresses. Used for zero
27766 -- cost exception handling to build the top level table.
27767
27768 ST : aliased constant array (1 .. 23) of System.Address := (
27769 Hello'UET_Address,
27770 Ada.Text_Io'UET_Address,
27771 Ada.Exceptions'UET_Address,
27772 Gnat.Heap_Sort_A'UET_Address,
27773 System.Exception_Table'UET_Address,
27774 System.Machine_State_Operations'UET_Address,
27775 System.Secondary_Stack'UET_Address,
27776 System.Parameters'UET_Address,
27777 System.Soft_Links'UET_Address,
27778 System.Stack_Checking'UET_Address,
27779 System.Traceback'UET_Address,
27780 Ada.Streams'UET_Address,
27781 Ada.Tags'UET_Address,
27782 System.String_Ops'UET_Address,
27783 Interfaces.C_Streams'UET_Address,
27784 System.File_Io'UET_Address,
27785 Ada.Finalization'UET_Address,
27786 System.Finalization_Root'UET_Address,
27787 System.Finalization_Implementation'UET_Address,
27788 System.String_Ops_Concat_3'UET_Address,
27789 System.Stream_Attributes'UET_Address,
27790 System.File_Control_Block'UET_Address,
27791 Ada.Finalization.List_Controller'UET_Address);
27792
27793 -- Table of addresses of elaboration routines. Used for
27794 -- zero cost exception handling to make sure these
27795 -- addresses are included in the top level procedure
27796 -- address table.
27797
27798 EA : aliased constant array (1 .. 23) of System.Address := (
27799 adainit'Code_Address,
27800 Do_Finalize'Code_Address,
27801 Ada.Exceptions'Elab_Spec'Address,
27802 System.Exceptions'Elab_Spec'Address,
27803 Interfaces.C_Streams'Elab_Spec'Address,
27804 System.Exception_Table'Elab_Body'Address,
27805 Ada.Io_Exceptions'Elab_Spec'Address,
27806 System.Stack_Checking'Elab_Spec'Address,
27807 System.Soft_Links'Elab_Body'Address,
27808 System.Secondary_Stack'Elab_Body'Address,
27809 Ada.Tags'Elab_Spec'Address,
27810 Ada.Tags'Elab_Body'Address,
27811 Ada.Streams'Elab_Spec'Address,
27812 System.Finalization_Root'Elab_Spec'Address,
27813 Ada.Exceptions'Elab_Body'Address,
27814 System.Finalization_Implementation'Elab_Spec'Address,
27815 System.Finalization_Implementation'Elab_Body'Address,
27816 Ada.Finalization'Elab_Spec'Address,
27817 Ada.Finalization.List_Controller'Elab_Spec'Address,
27818 System.File_Control_Block'Elab_Spec'Address,
27819 System.File_Io'Elab_Body'Address,
27820 Ada.Text_Io'Elab_Spec'Address,
27821 Ada.Text_Io'Elab_Body'Address);
27822
27823 -- Start of processing for adainit
27824
27825 begin
27826
27827 -- Call SDP_Table_Build to build the top level procedure
27828 -- table for zero cost exception handling (omitted in
27829 -- longjmp/setjmp mode).
27830
27831 SDP_Table_Build (ST'Address, 23, EA'Address, 23);
27832
27833 -- Call Set_Globals to record various information for
27834 -- this partition. The values are derived by the binder
27835 -- from information stored in the ali files by the compiler.
27836
27837 @findex __gnat_set_globals
27838 Set_Globals
27839 (Main_Priority => -1,
27840 -- Priority of main program, -1 if no pragma Priority used
27841
27842 Time_Slice_Value => -1,
27843 -- Time slice from Time_Slice pragma, -1 if none used
27844
27845 WC_Encoding => 'b',
27846 -- Wide_Character encoding used, default is brackets
27847
27848 Locking_Policy => ' ',
27849 -- Locking_Policy used, default of space means not
27850 -- specified, otherwise it is the first character of
27851 -- the policy name.
27852
27853 Queuing_Policy => ' ',
27854 -- Queuing_Policy used, default of space means not
27855 -- specified, otherwise it is the first character of
27856 -- the policy name.
27857
27858 Task_Dispatching_Policy => ' ',
27859 -- Task_Dispatching_Policy used, default of space means
27860 -- not specified, otherwise first character of the
27861 -- policy name.
27862
27863 Adafinal => System.Null_Address,
27864 -- Address of Adafinal routine, not used anymore
27865
27866 Unreserve_All_Interrupts => 0,
27867 -- Set true if pragma Unreserve_All_Interrupts was used
27868
27869 Exception_Tracebacks => 0);
27870 -- Indicates if exception tracebacks are enabled
27871
27872 Elab_Final_Code := 1;
27873
27874 -- Now we have the elaboration calls for all units in the partition.
27875 -- The Elab_Spec and Elab_Body attributes generate references to the
27876 -- implicit elaboration procedures generated by the compiler for
27877 -- each unit that requires elaboration.
27878
27879 if not E040 then
27880 Interfaces.C_Streams'Elab_Spec;
27881 end if;
27882 E040 := True;
27883 if not E008 then
27884 Ada.Exceptions'Elab_Spec;
27885 end if;
27886 if not E014 then
27887 System.Exception_Table'Elab_Body;
27888 E014 := True;
27889 end if;
27890 if not E053 then
27891 Ada.Io_Exceptions'Elab_Spec;
27892 E053 := True;
27893 end if;
27894 if not E017 then
27895 System.Exceptions'Elab_Spec;
27896 E017 := True;
27897 end if;
27898 if not E030 then
27899 System.Stack_Checking'Elab_Spec;
27900 end if;
27901 if not E028 then
27902 System.Soft_Links'Elab_Body;
27903 E028 := True;
27904 end if;
27905 E030 := True;
27906 if not E024 then
27907 System.Secondary_Stack'Elab_Body;
27908 E024 := True;
27909 end if;
27910 if not E035 then
27911 Ada.Tags'Elab_Spec;
27912 end if;
27913 if not E035 then
27914 Ada.Tags'Elab_Body;
27915 E035 := True;
27916 end if;
27917 if not E033 then
27918 Ada.Streams'Elab_Spec;
27919 E033 := True;
27920 end if;
27921 if not E046 then
27922 System.Finalization_Root'Elab_Spec;
27923 end if;
27924 E046 := True;
27925 if not E008 then
27926 Ada.Exceptions'Elab_Body;
27927 E008 := True;
27928 end if;
27929 if not E048 then
27930 System.Finalization_Implementation'Elab_Spec;
27931 end if;
27932 if not E048 then
27933 System.Finalization_Implementation'Elab_Body;
27934 E048 := True;
27935 end if;
27936 if not E044 then
27937 Ada.Finalization'Elab_Spec;
27938 end if;
27939 E044 := True;
27940 if not E057 then
27941 Ada.Finalization.List_Controller'Elab_Spec;
27942 end if;
27943 E057 := True;
27944 if not E055 then
27945 System.File_Control_Block'Elab_Spec;
27946 E055 := True;
27947 end if;
27948 if not E042 then
27949 System.File_Io'Elab_Body;
27950 E042 := True;
27951 end if;
27952 if not E006 then
27953 Ada.Text_Io'Elab_Spec;
27954 end if;
27955 if not E006 then
27956 Ada.Text_Io'Elab_Body;
27957 E006 := True;
27958 end if;
27959
27960 Elab_Final_Code := 0;
27961 end adainit;
27962
27963 --------------
27964 -- adafinal --
27965 --------------
27966
27967 @findex adafinal
27968 procedure adafinal is
27969 begin
27970 Do_Finalize;
27971 end adafinal;
27972
27973 ----------
27974 -- main --
27975 ----------
27976
27977 -- main is actually a function, as in the ANSI C standard,
27978 -- defined to return the exit status. The three parameters
27979 -- are the argument count, argument values and environment
27980 -- pointer.
27981
27982 @findex Main Program
27983 function main
27984 (argc : Integer;
27985 argv : System.Address;
27986 envp : System.Address)
27987 return Integer
27988 is
27989 -- The initialize routine performs low level system
27990 -- initialization using a standard library routine which
27991 -- sets up signal handling and performs any other
27992 -- required setup. The routine can be found in file
27993 -- a-init.c.
27994
27995 @findex __gnat_initialize
27996 procedure initialize;
27997 pragma Import (C, initialize, "__gnat_initialize");
27998
27999 -- The finalize routine performs low level system
28000 -- finalization using a standard library routine. The
28001 -- routine is found in file a-final.c and in the standard
28002 -- distribution is a dummy routine that does nothing, so
28003 -- really this is a hook for special user finalization.
28004
28005 @findex __gnat_finalize
28006 procedure finalize;
28007 pragma Import (C, finalize, "__gnat_finalize");
28008
28009 -- We get to the main program of the partition by using
28010 -- pragma Import because if we try to with the unit and
28011 -- call it Ada style, then not only do we waste time
28012 -- recompiling it, but also, we don't really know the right
28013 -- switches (e.g.@: identifier character set) to be used
28014 -- to compile it.
28015
28016 procedure Ada_Main_Program;
28017 pragma Import (Ada, Ada_Main_Program, "_ada_hello");
28018
28019 -- Start of processing for main
28020
28021 begin
28022 -- Save global variables
28023
28024 gnat_argc := argc;
28025 gnat_argv := argv;
28026 gnat_envp := envp;
28027
28028 -- Call low level system initialization
28029
28030 Initialize;
28031
28032 -- Call our generated Ada initialization routine
28033
28034 adainit;
28035
28036 -- This is the point at which we want the debugger to get
28037 -- control
28038
28039 Break_Start;
28040
28041 -- Now we call the main program of the partition
28042
28043 Ada_Main_Program;
28044
28045 -- Perform Ada finalization
28046
28047 adafinal;
28048
28049 -- Perform low level system finalization
28050
28051 Finalize;
28052
28053 -- Return the proper exit status
28054 return (gnat_exit_status);
28055 end;
28056
28057 -- This section is entirely comments, so it has no effect on the
28058 -- compilation of the Ada_Main package. It provides the list of
28059 -- object files and linker options, as well as some standard
28060 -- libraries needed for the link. The gnatlink utility parses
28061 -- this b~hello.adb file to read these comment lines to generate
28062 -- the appropriate command line arguments for the call to the
28063 -- system linker. The BEGIN/END lines are used for sentinels for
28064 -- this parsing operation.
28065
28066 -- The exact file names will of course depend on the environment,
28067 -- host/target and location of files on the host system.
28068
28069 @findex Object file list
28070 -- BEGIN Object file/option list
28071 -- ./hello.o
28072 -- -L./
28073 -- -L/usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/
28074 -- /usr/local/gnat/lib/gcc-lib/i686-pc-linux-gnu/2.8.1/adalib/libgnat.a
28075 -- END Object file/option list
28076
28077 end ada_main;
28078 @end smallexample
28079
28080 @noindent
28081 The Ada code in the above example is exactly what is generated by the
28082 binder. We have added comments to more clearly indicate the function
28083 of each part of the generated @code{Ada_Main} package.
28084
28085 The code is standard Ada in all respects, and can be processed by any
28086 tools that handle Ada. In particular, it is possible to use the debugger
28087 in Ada mode to debug the generated @code{Ada_Main} package. For example,
28088 suppose that for reasons that you do not understand, your program is crashing
28089 during elaboration of the body of @code{Ada.Text_IO}. To locate this bug,
28090 you can place a breakpoint on the call:
28091
28092 @smallexample @c ada
28093 Ada.Text_Io'Elab_Body;
28094 @end smallexample
28095
28096 @noindent
28097 and trace the elaboration routine for this package to find out where
28098 the problem might be (more usually of course you would be debugging
28099 elaboration code in your own application).
28100
28101 @node Elaboration Order Handling in GNAT
28102 @appendix Elaboration Order Handling in GNAT
28103 @cindex Order of elaboration
28104 @cindex Elaboration control
28105
28106 @menu
28107 * Elaboration Code::
28108 * Checking the Elaboration Order::
28109 * Controlling the Elaboration Order::
28110 * Controlling Elaboration in GNAT - Internal Calls::
28111 * Controlling Elaboration in GNAT - External Calls::
28112 * Default Behavior in GNAT - Ensuring Safety::
28113 * Treatment of Pragma Elaborate::
28114 * Elaboration Issues for Library Tasks::
28115 * Mixing Elaboration Models::
28116 * What to Do If the Default Elaboration Behavior Fails::
28117 * Elaboration for Access-to-Subprogram Values::
28118 * Summary of Procedures for Elaboration Control::
28119 * Other Elaboration Order Considerations::
28120 @end menu
28121
28122 @noindent
28123 This chapter describes the handling of elaboration code in Ada and
28124 in GNAT, and discusses how the order of elaboration of program units can
28125 be controlled in GNAT, either automatically or with explicit programming
28126 features.
28127
28128 @node Elaboration Code
28129 @section Elaboration Code
28130
28131 @noindent
28132 Ada provides rather general mechanisms for executing code at elaboration
28133 time, that is to say before the main program starts executing. Such code arises
28134 in three contexts:
28135
28136 @table @asis
28137 @item Initializers for variables.
28138 Variables declared at the library level, in package specs or bodies, can
28139 require initialization that is performed at elaboration time, as in:
28140 @smallexample @c ada
28141 @cartouche
28142 Sqrt_Half : Float := Sqrt (0.5);
28143 @end cartouche
28144 @end smallexample
28145
28146 @item Package initialization code
28147 Code in a @code{BEGIN-END} section at the outer level of a package body is
28148 executed as part of the package body elaboration code.
28149
28150 @item Library level task allocators
28151 Tasks that are declared using task allocators at the library level
28152 start executing immediately and hence can execute at elaboration time.
28153 @end table
28154
28155 @noindent
28156 Subprogram calls are possible in any of these contexts, which means that
28157 any arbitrary part of the program may be executed as part of the elaboration
28158 code. It is even possible to write a program which does all its work at
28159 elaboration time, with a null main program, although stylistically this
28160 would usually be considered an inappropriate way to structure
28161 a program.
28162
28163 An important concern arises in the context of elaboration code:
28164 we have to be sure that it is executed in an appropriate order. What we
28165 have is a series of elaboration code sections, potentially one section
28166 for each unit in the program. It is important that these execute
28167 in the correct order. Correctness here means that, taking the above
28168 example of the declaration of @code{Sqrt_Half},
28169 if some other piece of
28170 elaboration code references @code{Sqrt_Half},
28171 then it must run after the
28172 section of elaboration code that contains the declaration of
28173 @code{Sqrt_Half}.
28174
28175 There would never be any order of elaboration problem if we made a rule
28176 that whenever you @code{with} a unit, you must elaborate both the spec and body
28177 of that unit before elaborating the unit doing the @code{with}'ing:
28178
28179 @smallexample @c ada
28180 @group
28181 @cartouche
28182 with Unit_1;
28183 package Unit_2 is @dots{}
28184 @end cartouche
28185 @end group
28186 @end smallexample
28187
28188 @noindent
28189 would require that both the body and spec of @code{Unit_1} be elaborated
28190 before the spec of @code{Unit_2}. However, a rule like that would be far too
28191 restrictive. In particular, it would make it impossible to have routines
28192 in separate packages that were mutually recursive.
28193
28194 You might think that a clever enough compiler could look at the actual
28195 elaboration code and determine an appropriate correct order of elaboration,
28196 but in the general case, this is not possible. Consider the following
28197 example.
28198
28199 In the body of @code{Unit_1}, we have a procedure @code{Func_1}
28200 that references
28201 the variable @code{Sqrt_1}, which is declared in the elaboration code
28202 of the body of @code{Unit_1}:
28203
28204 @smallexample @c ada
28205 @cartouche
28206 Sqrt_1 : Float := Sqrt (0.1);
28207 @end cartouche
28208 @end smallexample
28209
28210 @noindent
28211 The elaboration code of the body of @code{Unit_1} also contains:
28212
28213 @smallexample @c ada
28214 @group
28215 @cartouche
28216 if expression_1 = 1 then
28217 Q := Unit_2.Func_2;
28218 end if;
28219 @end cartouche
28220 @end group
28221 @end smallexample
28222
28223 @noindent
28224 @code{Unit_2} is exactly parallel,
28225 it has a procedure @code{Func_2} that references
28226 the variable @code{Sqrt_2}, which is declared in the elaboration code of
28227 the body @code{Unit_2}:
28228
28229 @smallexample @c ada
28230 @cartouche
28231 Sqrt_2 : Float := Sqrt (0.1);
28232 @end cartouche
28233 @end smallexample
28234
28235 @noindent
28236 The elaboration code of the body of @code{Unit_2} also contains:
28237
28238 @smallexample @c ada
28239 @group
28240 @cartouche
28241 if expression_2 = 2 then
28242 Q := Unit_1.Func_1;
28243 end if;
28244 @end cartouche
28245 @end group
28246 @end smallexample
28247
28248 @noindent
28249 Now the question is, which of the following orders of elaboration is
28250 acceptable:
28251
28252 @smallexample
28253 @group
28254 Spec of Unit_1
28255 Spec of Unit_2
28256 Body of Unit_1
28257 Body of Unit_2
28258 @end group
28259 @end smallexample
28260
28261 @noindent
28262 or
28263
28264 @smallexample
28265 @group
28266 Spec of Unit_2
28267 Spec of Unit_1
28268 Body of Unit_2
28269 Body of Unit_1
28270 @end group
28271 @end smallexample
28272
28273 @noindent
28274 If you carefully analyze the flow here, you will see that you cannot tell
28275 at compile time the answer to this question.
28276 If @code{expression_1} is not equal to 1,
28277 and @code{expression_2} is not equal to 2,
28278 then either order is acceptable, because neither of the function calls is
28279 executed. If both tests evaluate to true, then neither order is acceptable
28280 and in fact there is no correct order.
28281
28282 If one of the two expressions is true, and the other is false, then one
28283 of the above orders is correct, and the other is incorrect. For example,
28284 if @code{expression_1} /= 1 and @code{expression_2} = 2,
28285 then the call to @code{Func_1}
28286 will occur, but not the call to @code{Func_2.}
28287 This means that it is essential
28288 to elaborate the body of @code{Unit_1} before
28289 the body of @code{Unit_2}, so the first
28290 order of elaboration is correct and the second is wrong.
28291
28292 By making @code{expression_1} and @code{expression_2}
28293 depend on input data, or perhaps
28294 the time of day, we can make it impossible for the compiler or binder
28295 to figure out which of these expressions will be true, and hence it
28296 is impossible to guarantee a safe order of elaboration at run time.
28297
28298 @node Checking the Elaboration Order
28299 @section Checking the Elaboration Order
28300
28301 @noindent
28302 In some languages that involve the same kind of elaboration problems,
28303 e.g.@: Java and C++, the programmer is expected to worry about these
28304 ordering problems himself, and it is common to
28305 write a program in which an incorrect elaboration order gives
28306 surprising results, because it references variables before they
28307 are initialized.
28308 Ada is designed to be a safe language, and a programmer-beware approach is
28309 clearly not sufficient. Consequently, the language provides three lines
28310 of defense:
28311
28312 @table @asis
28313 @item Standard rules
28314 Some standard rules restrict the possible choice of elaboration
28315 order. In particular, if you @code{with} a unit, then its spec is always
28316 elaborated before the unit doing the @code{with}. Similarly, a parent
28317 spec is always elaborated before the child spec, and finally
28318 a spec is always elaborated before its corresponding body.
28319
28320 @item Dynamic elaboration checks
28321 @cindex Elaboration checks
28322 @cindex Checks, elaboration
28323 Dynamic checks are made at run time, so that if some entity is accessed
28324 before it is elaborated (typically by means of a subprogram call)
28325 then the exception (@code{Program_Error}) is raised.
28326
28327 @item Elaboration control
28328 Facilities are provided for the programmer to specify the desired order
28329 of elaboration.
28330 @end table
28331
28332 Let's look at these facilities in more detail. First, the rules for
28333 dynamic checking. One possible rule would be simply to say that the
28334 exception is raised if you access a variable which has not yet been
28335 elaborated. The trouble with this approach is that it could require
28336 expensive checks on every variable reference. Instead Ada has two
28337 rules which are a little more restrictive, but easier to check, and
28338 easier to state:
28339
28340 @table @asis
28341 @item Restrictions on calls
28342 A subprogram can only be called at elaboration time if its body
28343 has been elaborated. The rules for elaboration given above guarantee
28344 that the spec of the subprogram has been elaborated before the
28345 call, but not the body. If this rule is violated, then the
28346 exception @code{Program_Error} is raised.
28347
28348 @item Restrictions on instantiations
28349 A generic unit can only be instantiated if the body of the generic
28350 unit has been elaborated. Again, the rules for elaboration given above
28351 guarantee that the spec of the generic unit has been elaborated
28352 before the instantiation, but not the body. If this rule is
28353 violated, then the exception @code{Program_Error} is raised.
28354 @end table
28355
28356 @noindent
28357 The idea is that if the body has been elaborated, then any variables
28358 it references must have been elaborated; by checking for the body being
28359 elaborated we guarantee that none of its references causes any
28360 trouble. As we noted above, this is a little too restrictive, because a
28361 subprogram that has no non-local references in its body may in fact be safe
28362 to call. However, it really would be unsafe to rely on this, because
28363 it would mean that the caller was aware of details of the implementation
28364 in the body. This goes against the basic tenets of Ada.
28365
28366 A plausible implementation can be described as follows.
28367 A Boolean variable is associated with each subprogram
28368 and each generic unit. This variable is initialized to False, and is set to
28369 True at the point body is elaborated. Every call or instantiation checks the
28370 variable, and raises @code{Program_Error} if the variable is False.
28371
28372 Note that one might think that it would be good enough to have one Boolean
28373 variable for each package, but that would not deal with cases of trying
28374 to call a body in the same package as the call
28375 that has not been elaborated yet.
28376 Of course a compiler may be able to do enough analysis to optimize away
28377 some of the Boolean variables as unnecessary, and @code{GNAT} indeed
28378 does such optimizations, but still the easiest conceptual model is to
28379 think of there being one variable per subprogram.
28380
28381 @node Controlling the Elaboration Order
28382 @section Controlling the Elaboration Order
28383
28384 @noindent
28385 In the previous section we discussed the rules in Ada which ensure
28386 that @code{Program_Error} is raised if an incorrect elaboration order is
28387 chosen. This prevents erroneous executions, but we need mechanisms to
28388 specify a correct execution and avoid the exception altogether.
28389 To achieve this, Ada provides a number of features for controlling
28390 the order of elaboration. We discuss these features in this section.
28391
28392 First, there are several ways of indicating to the compiler that a given
28393 unit has no elaboration problems:
28394
28395 @table @asis
28396 @item packages that do not require a body
28397 A library package that does not require a body does not permit
28398 a body (this rule was introduced in Ada 95).
28399 Thus if we have a such a package, as in:
28400
28401 @smallexample @c ada
28402 @group
28403 @cartouche
28404 package Definitions is
28405 generic
28406 type m is new integer;
28407 package Subp is
28408 type a is array (1 .. 10) of m;
28409 type b is array (1 .. 20) of m;
28410 end Subp;
28411 end Definitions;
28412 @end cartouche
28413 @end group
28414 @end smallexample
28415
28416 @noindent
28417 A package that @code{with}'s @code{Definitions} may safely instantiate
28418 @code{Definitions.Subp} because the compiler can determine that there
28419 definitely is no package body to worry about in this case
28420
28421 @item pragma Pure
28422 @cindex pragma Pure
28423 @findex Pure
28424 Places sufficient restrictions on a unit to guarantee that
28425 no call to any subprogram in the unit can result in an
28426 elaboration problem. This means that the compiler does not need
28427 to worry about the point of elaboration of such units, and in
28428 particular, does not need to check any calls to any subprograms
28429 in this unit.
28430
28431 @item pragma Preelaborate
28432 @findex Preelaborate
28433 @cindex pragma Preelaborate
28434 This pragma places slightly less stringent restrictions on a unit than
28435 does pragma Pure,
28436 but these restrictions are still sufficient to ensure that there
28437 are no elaboration problems with any calls to the unit.
28438
28439 @item pragma Elaborate_Body
28440 @findex Elaborate_Body
28441 @cindex pragma Elaborate_Body
28442 This pragma requires that the body of a unit be elaborated immediately
28443 after its spec. Suppose a unit @code{A} has such a pragma,
28444 and unit @code{B} does
28445 a @code{with} of unit @code{A}. Recall that the standard rules require
28446 the spec of unit @code{A}
28447 to be elaborated before the @code{with}'ing unit; given the pragma in
28448 @code{A}, we also know that the body of @code{A}
28449 will be elaborated before @code{B}, so
28450 that calls to @code{A} are safe and do not need a check.
28451 @end table
28452
28453 @noindent
28454 Note that,
28455 unlike pragma @code{Pure} and pragma @code{Preelaborate},
28456 the use of
28457 @code{Elaborate_Body} does not guarantee that the program is
28458 free of elaboration problems, because it may not be possible
28459 to satisfy the requested elaboration order.
28460 Let's go back to the example with @code{Unit_1} and @code{Unit_2}.
28461 If a programmer
28462 marks @code{Unit_1} as @code{Elaborate_Body},
28463 and not @code{Unit_2,} then the order of
28464 elaboration will be:
28465
28466 @smallexample
28467 @group
28468 Spec of Unit_2
28469 Spec of Unit_1
28470 Body of Unit_1
28471 Body of Unit_2
28472 @end group
28473 @end smallexample
28474
28475 @noindent
28476 Now that means that the call to @code{Func_1} in @code{Unit_2}
28477 need not be checked,
28478 it must be safe. But the call to @code{Func_2} in
28479 @code{Unit_1} may still fail if
28480 @code{Expression_1} is equal to 1,
28481 and the programmer must still take
28482 responsibility for this not being the case.
28483
28484 If all units carry a pragma @code{Elaborate_Body}, then all problems are
28485 eliminated, except for calls entirely within a body, which are
28486 in any case fully under programmer control. However, using the pragma
28487 everywhere is not always possible.
28488 In particular, for our @code{Unit_1}/@code{Unit_2} example, if
28489 we marked both of them as having pragma @code{Elaborate_Body}, then
28490 clearly there would be no possible elaboration order.
28491
28492 The above pragmas allow a server to guarantee safe use by clients, and
28493 clearly this is the preferable approach. Consequently a good rule
28494 is to mark units as @code{Pure} or @code{Preelaborate} if possible,
28495 and if this is not possible,
28496 mark them as @code{Elaborate_Body} if possible.
28497 As we have seen, there are situations where neither of these
28498 three pragmas can be used.
28499 So we also provide methods for clients to control the
28500 order of elaboration of the servers on which they depend:
28501
28502 @table @asis
28503 @item pragma Elaborate (unit)
28504 @findex Elaborate
28505 @cindex pragma Elaborate
28506 This pragma is placed in the context clause, after a @code{with} clause,
28507 and it requires that the body of the named unit be elaborated before
28508 the unit in which the pragma occurs. The idea is to use this pragma
28509 if the current unit calls at elaboration time, directly or indirectly,
28510 some subprogram in the named unit.
28511
28512 @item pragma Elaborate_All (unit)
28513 @findex Elaborate_All
28514 @cindex pragma Elaborate_All
28515 This is a stronger version of the Elaborate pragma. Consider the
28516 following example:
28517
28518 @smallexample
28519 Unit A @code{with}'s unit B and calls B.Func in elab code
28520 Unit B @code{with}'s unit C, and B.Func calls C.Func
28521 @end smallexample
28522
28523 @noindent
28524 Now if we put a pragma @code{Elaborate (B)}
28525 in unit @code{A}, this ensures that the
28526 body of @code{B} is elaborated before the call, but not the
28527 body of @code{C}, so
28528 the call to @code{C.Func} could still cause @code{Program_Error} to
28529 be raised.
28530
28531 The effect of a pragma @code{Elaborate_All} is stronger, it requires
28532 not only that the body of the named unit be elaborated before the
28533 unit doing the @code{with}, but also the bodies of all units that the
28534 named unit uses, following @code{with} links transitively. For example,
28535 if we put a pragma @code{Elaborate_All (B)} in unit @code{A},
28536 then it requires
28537 not only that the body of @code{B} be elaborated before @code{A},
28538 but also the
28539 body of @code{C}, because @code{B} @code{with}'s @code{C}.
28540 @end table
28541
28542 @noindent
28543 We are now in a position to give a usage rule in Ada for avoiding
28544 elaboration problems, at least if dynamic dispatching and access to
28545 subprogram values are not used. We will handle these cases separately
28546 later.
28547
28548 The rule is simple. If a unit has elaboration code that can directly or
28549 indirectly make a call to a subprogram in a @code{with}'ed unit, or instantiate
28550 a generic package in a @code{with}'ed unit,
28551 then if the @code{with}'ed unit does not have
28552 pragma @code{Pure} or @code{Preelaborate}, then the client should have
28553 a pragma @code{Elaborate_All}
28554 for the @code{with}'ed unit. By following this rule a client is
28555 assured that calls can be made without risk of an exception.
28556
28557 For generic subprogram instantiations, the rule can be relaxed to
28558 require only a pragma @code{Elaborate} since elaborating the body
28559 of a subprogram cannot cause any transitive elaboration (we are
28560 not calling the subprogram in this case, just elaborating its
28561 declaration).
28562
28563 If this rule is not followed, then a program may be in one of four
28564 states:
28565
28566 @table @asis
28567 @item No order exists
28568 No order of elaboration exists which follows the rules, taking into
28569 account any @code{Elaborate}, @code{Elaborate_All},
28570 or @code{Elaborate_Body} pragmas. In
28571 this case, an Ada compiler must diagnose the situation at bind
28572 time, and refuse to build an executable program.
28573
28574 @item One or more orders exist, all incorrect
28575 One or more acceptable elaboration orders exist, and all of them
28576 generate an elaboration order problem. In this case, the binder
28577 can build an executable program, but @code{Program_Error} will be raised
28578 when the program is run.
28579
28580 @item Several orders exist, some right, some incorrect
28581 One or more acceptable elaboration orders exists, and some of them
28582 work, and some do not. The programmer has not controlled
28583 the order of elaboration, so the binder may or may not pick one of
28584 the correct orders, and the program may or may not raise an
28585 exception when it is run. This is the worst case, because it means
28586 that the program may fail when moved to another compiler, or even
28587 another version of the same compiler.
28588
28589 @item One or more orders exists, all correct
28590 One ore more acceptable elaboration orders exist, and all of them
28591 work. In this case the program runs successfully. This state of
28592 affairs can be guaranteed by following the rule we gave above, but
28593 may be true even if the rule is not followed.
28594 @end table
28595
28596 @noindent
28597 Note that one additional advantage of following our rules on the use
28598 of @code{Elaborate} and @code{Elaborate_All}
28599 is that the program continues to stay in the ideal (all orders OK) state
28600 even if maintenance
28601 changes some bodies of some units. Conversely, if a program that does
28602 not follow this rule happens to be safe at some point, this state of affairs
28603 may deteriorate silently as a result of maintenance changes.
28604
28605 You may have noticed that the above discussion did not mention
28606 the use of @code{Elaborate_Body}. This was a deliberate omission. If you
28607 @code{with} an @code{Elaborate_Body} unit, it still may be the case that
28608 code in the body makes calls to some other unit, so it is still necessary
28609 to use @code{Elaborate_All} on such units.
28610
28611 @node Controlling Elaboration in GNAT - Internal Calls
28612 @section Controlling Elaboration in GNAT - Internal Calls
28613
28614 @noindent
28615 In the case of internal calls, i.e., calls within a single package, the
28616 programmer has full control over the order of elaboration, and it is up
28617 to the programmer to elaborate declarations in an appropriate order. For
28618 example writing:
28619
28620 @smallexample @c ada
28621 @group
28622 @cartouche
28623 function One return Float;
28624
28625 Q : Float := One;
28626
28627 function One return Float is
28628 begin
28629 return 1.0;
28630 end One;
28631 @end cartouche
28632 @end group
28633 @end smallexample
28634
28635 @noindent
28636 will obviously raise @code{Program_Error} at run time, because function
28637 One will be called before its body is elaborated. In this case GNAT will
28638 generate a warning that the call will raise @code{Program_Error}:
28639
28640 @smallexample
28641 @group
28642 @cartouche
28643 1. procedure y is
28644 2. function One return Float;
28645 3.
28646 4. Q : Float := One;
28647 |
28648 >>> warning: cannot call "One" before body is elaborated
28649 >>> warning: Program_Error will be raised at run time
28650
28651 5.
28652 6. function One return Float is
28653 7. begin
28654 8. return 1.0;
28655 9. end One;
28656 10.
28657 11. begin
28658 12. null;
28659 13. end;
28660 @end cartouche
28661 @end group
28662 @end smallexample
28663
28664 @noindent
28665 Note that in this particular case, it is likely that the call is safe, because
28666 the function @code{One} does not access any global variables.
28667 Nevertheless in Ada, we do not want the validity of the check to depend on
28668 the contents of the body (think about the separate compilation case), so this
28669 is still wrong, as we discussed in the previous sections.
28670
28671 The error is easily corrected by rearranging the declarations so that the
28672 body of @code{One} appears before the declaration containing the call
28673 (note that in Ada 95 and Ada 2005,
28674 declarations can appear in any order, so there is no restriction that
28675 would prevent this reordering, and if we write:
28676
28677 @smallexample @c ada
28678 @group
28679 @cartouche
28680 function One return Float;
28681
28682 function One return Float is
28683 begin
28684 return 1.0;
28685 end One;
28686
28687 Q : Float := One;
28688 @end cartouche
28689 @end group
28690 @end smallexample
28691
28692 @noindent
28693 then all is well, no warning is generated, and no
28694 @code{Program_Error} exception
28695 will be raised.
28696 Things are more complicated when a chain of subprograms is executed:
28697
28698 @smallexample @c ada
28699 @group
28700 @cartouche
28701 function A return Integer;
28702 function B return Integer;
28703 function C return Integer;
28704
28705 function B return Integer is begin return A; end;
28706 function C return Integer is begin return B; end;
28707
28708 X : Integer := C;
28709
28710 function A return Integer is begin return 1; end;
28711 @end cartouche
28712 @end group
28713 @end smallexample
28714
28715 @noindent
28716 Now the call to @code{C}
28717 at elaboration time in the declaration of @code{X} is correct, because
28718 the body of @code{C} is already elaborated,
28719 and the call to @code{B} within the body of
28720 @code{C} is correct, but the call
28721 to @code{A} within the body of @code{B} is incorrect, because the body
28722 of @code{A} has not been elaborated, so @code{Program_Error}
28723 will be raised on the call to @code{A}.
28724 In this case GNAT will generate a
28725 warning that @code{Program_Error} may be
28726 raised at the point of the call. Let's look at the warning:
28727
28728 @smallexample
28729 @group
28730 @cartouche
28731 1. procedure x is
28732 2. function A return Integer;
28733 3. function B return Integer;
28734 4. function C return Integer;
28735 5.
28736 6. function B return Integer is begin return A; end;
28737 |
28738 >>> warning: call to "A" before body is elaborated may
28739 raise Program_Error
28740 >>> warning: "B" called at line 7
28741 >>> warning: "C" called at line 9
28742
28743 7. function C return Integer is begin return B; end;
28744 8.
28745 9. X : Integer := C;
28746 10.
28747 11. function A return Integer is begin return 1; end;
28748 12.
28749 13. begin
28750 14. null;
28751 15. end;
28752 @end cartouche
28753 @end group
28754 @end smallexample
28755
28756 @noindent
28757 Note that the message here says ``may raise'', instead of the direct case,
28758 where the message says ``will be raised''. That's because whether
28759 @code{A} is
28760 actually called depends in general on run-time flow of control.
28761 For example, if the body of @code{B} said
28762
28763 @smallexample @c ada
28764 @group
28765 @cartouche
28766 function B return Integer is
28767 begin
28768 if some-condition-depending-on-input-data then
28769 return A;
28770 else
28771 return 1;
28772 end if;
28773 end B;
28774 @end cartouche
28775 @end group
28776 @end smallexample
28777
28778 @noindent
28779 then we could not know until run time whether the incorrect call to A would
28780 actually occur, so @code{Program_Error} might
28781 or might not be raised. It is possible for a compiler to
28782 do a better job of analyzing bodies, to
28783 determine whether or not @code{Program_Error}
28784 might be raised, but it certainly
28785 couldn't do a perfect job (that would require solving the halting problem
28786 and is provably impossible), and because this is a warning anyway, it does
28787 not seem worth the effort to do the analysis. Cases in which it
28788 would be relevant are rare.
28789
28790 In practice, warnings of either of the forms given
28791 above will usually correspond to
28792 real errors, and should be examined carefully and eliminated.
28793 In the rare case where a warning is bogus, it can be suppressed by any of
28794 the following methods:
28795
28796 @itemize @bullet
28797 @item
28798 Compile with the @option{-gnatws} switch set
28799
28800 @item
28801 Suppress @code{Elaboration_Check} for the called subprogram
28802
28803 @item
28804 Use pragma @code{Warnings_Off} to turn warnings off for the call
28805 @end itemize
28806
28807 @noindent
28808 For the internal elaboration check case,
28809 GNAT by default generates the
28810 necessary run-time checks to ensure
28811 that @code{Program_Error} is raised if any
28812 call fails an elaboration check. Of course this can only happen if a
28813 warning has been issued as described above. The use of pragma
28814 @code{Suppress (Elaboration_Check)} may (but is not guaranteed to) suppress
28815 some of these checks, meaning that it may be possible (but is not
28816 guaranteed) for a program to be able to call a subprogram whose body
28817 is not yet elaborated, without raising a @code{Program_Error} exception.
28818
28819 @node Controlling Elaboration in GNAT - External Calls
28820 @section Controlling Elaboration in GNAT - External Calls
28821
28822 @noindent
28823 The previous section discussed the case in which the execution of a
28824 particular thread of elaboration code occurred entirely within a
28825 single unit. This is the easy case to handle, because a programmer
28826 has direct and total control over the order of elaboration, and
28827 furthermore, checks need only be generated in cases which are rare
28828 and which the compiler can easily detect.
28829 The situation is more complex when separate compilation is taken into account.
28830 Consider the following:
28831
28832 @smallexample @c ada
28833 @cartouche
28834 @group
28835 package Math is
28836 function Sqrt (Arg : Float) return Float;
28837 end Math;
28838
28839 package body Math is
28840 function Sqrt (Arg : Float) return Float is
28841 begin
28842 @dots{}
28843 end Sqrt;
28844 end Math;
28845 @end group
28846 @group
28847 with Math;
28848 package Stuff is
28849 X : Float := Math.Sqrt (0.5);
28850 end Stuff;
28851
28852 with Stuff;
28853 procedure Main is
28854 begin
28855 @dots{}
28856 end Main;
28857 @end group
28858 @end cartouche
28859 @end smallexample
28860
28861 @noindent
28862 where @code{Main} is the main program. When this program is executed, the
28863 elaboration code must first be executed, and one of the jobs of the
28864 binder is to determine the order in which the units of a program are
28865 to be elaborated. In this case we have four units: the spec and body
28866 of @code{Math},
28867 the spec of @code{Stuff} and the body of @code{Main}).
28868 In what order should the four separate sections of elaboration code
28869 be executed?
28870
28871 There are some restrictions in the order of elaboration that the binder
28872 can choose. In particular, if unit U has a @code{with}
28873 for a package @code{X}, then you
28874 are assured that the spec of @code{X}
28875 is elaborated before U , but you are
28876 not assured that the body of @code{X}
28877 is elaborated before U.
28878 This means that in the above case, the binder is allowed to choose the
28879 order:
28880
28881 @smallexample
28882 spec of Math
28883 spec of Stuff
28884 body of Math
28885 body of Main
28886 @end smallexample
28887
28888 @noindent
28889 but that's not good, because now the call to @code{Math.Sqrt}
28890 that happens during
28891 the elaboration of the @code{Stuff}
28892 spec happens before the body of @code{Math.Sqrt} is
28893 elaborated, and hence causes @code{Program_Error} exception to be raised.
28894 At first glance, one might say that the binder is misbehaving, because
28895 obviously you want to elaborate the body of something you @code{with}
28896 first, but
28897 that is not a general rule that can be followed in all cases. Consider
28898
28899 @smallexample @c ada
28900 @group
28901 @cartouche
28902 package X is @dots{}
28903
28904 package Y is @dots{}
28905
28906 with X;
28907 package body Y is @dots{}
28908
28909 with Y;
28910 package body X is @dots{}
28911 @end cartouche
28912 @end group
28913 @end smallexample
28914
28915 @noindent
28916 This is a common arrangement, and, apart from the order of elaboration
28917 problems that might arise in connection with elaboration code, this works fine.
28918 A rule that says that you must first elaborate the body of anything you
28919 @code{with} cannot work in this case:
28920 the body of @code{X} @code{with}'s @code{Y},
28921 which means you would have to
28922 elaborate the body of @code{Y} first, but that @code{with}'s @code{X},
28923 which means
28924 you have to elaborate the body of @code{X} first, but @dots{} and we have a
28925 loop that cannot be broken.
28926
28927 It is true that the binder can in many cases guess an order of elaboration
28928 that is unlikely to cause a @code{Program_Error}
28929 exception to be raised, and it tries to do so (in the
28930 above example of @code{Math/Stuff/Spec}, the GNAT binder will
28931 by default
28932 elaborate the body of @code{Math} right after its spec, so all will be well).
28933
28934 However, a program that blindly relies on the binder to be helpful can
28935 get into trouble, as we discussed in the previous sections, so
28936 GNAT
28937 provides a number of facilities for assisting the programmer in
28938 developing programs that are robust with respect to elaboration order.
28939
28940 @node Default Behavior in GNAT - Ensuring Safety
28941 @section Default Behavior in GNAT - Ensuring Safety
28942
28943 @noindent
28944 The default behavior in GNAT ensures elaboration safety. In its
28945 default mode GNAT implements the
28946 rule we previously described as the right approach. Let's restate it:
28947
28948 @itemize
28949 @item
28950 @emph{If a unit has elaboration code that can directly or indirectly make a
28951 call to a subprogram in a @code{with}'ed unit, or instantiate a generic
28952 package in a @code{with}'ed unit, then if the @code{with}'ed unit
28953 does not have pragma @code{Pure} or
28954 @code{Preelaborate}, then the client should have an
28955 @code{Elaborate_All} pragma for the @code{with}'ed unit.}
28956
28957 @emph{In the case of instantiating a generic subprogram, it is always
28958 sufficient to have only an @code{Elaborate} pragma for the
28959 @code{with}'ed unit.}
28960 @end itemize
28961
28962 @noindent
28963 By following this rule a client is assured that calls and instantiations
28964 can be made without risk of an exception.
28965
28966 In this mode GNAT traces all calls that are potentially made from
28967 elaboration code, and puts in any missing implicit @code{Elaborate}
28968 and @code{Elaborate_All} pragmas.
28969 The advantage of this approach is that no elaboration problems
28970 are possible if the binder can find an elaboration order that is
28971 consistent with these implicit @code{Elaborate} and
28972 @code{Elaborate_All} pragmas. The
28973 disadvantage of this approach is that no such order may exist.
28974
28975 If the binder does not generate any diagnostics, then it means that it has
28976 found an elaboration order that is guaranteed to be safe. However, the binder
28977 may still be relying on implicitly generated @code{Elaborate} and
28978 @code{Elaborate_All} pragmas so portability to other compilers than GNAT is not
28979 guaranteed.
28980
28981 If it is important to guarantee portability, then the compilations should
28982 use the
28983 @option{-gnatwl}
28984 (warn on elaboration problems) switch. This will cause warning messages
28985 to be generated indicating the missing @code{Elaborate} and
28986 @code{Elaborate_All} pragmas.
28987 Consider the following source program:
28988
28989 @smallexample @c ada
28990 @group
28991 @cartouche
28992 with k;
28993 package j is
28994 m : integer := k.r;
28995 end;
28996 @end cartouche
28997 @end group
28998 @end smallexample
28999
29000 @noindent
29001 where it is clear that there
29002 should be a pragma @code{Elaborate_All}
29003 for unit @code{k}. An implicit pragma will be generated, and it is
29004 likely that the binder will be able to honor it. However, if you want
29005 to port this program to some other Ada compiler than GNAT.
29006 it is safer to include the pragma explicitly in the source. If this
29007 unit is compiled with the
29008 @option{-gnatwl}
29009 switch, then the compiler outputs a warning:
29010
29011 @smallexample
29012 @group
29013 @cartouche
29014 1. with k;
29015 2. package j is
29016 3. m : integer := k.r;
29017 |
29018 >>> warning: call to "r" may raise Program_Error
29019 >>> warning: missing pragma Elaborate_All for "k"
29020
29021 4. end;
29022 @end cartouche
29023 @end group
29024 @end smallexample
29025
29026 @noindent
29027 and these warnings can be used as a guide for supplying manually
29028 the missing pragmas. It is usually a bad idea to use this warning
29029 option during development. That's because it will warn you when
29030 you need to put in a pragma, but cannot warn you when it is time
29031 to take it out. So the use of pragma @code{Elaborate_All} may lead to
29032 unnecessary dependencies and even false circularities.
29033
29034 This default mode is more restrictive than the Ada Reference
29035 Manual, and it is possible to construct programs which will compile
29036 using the dynamic model described there, but will run into a
29037 circularity using the safer static model we have described.
29038
29039 Of course any Ada compiler must be able to operate in a mode
29040 consistent with the requirements of the Ada Reference Manual,
29041 and in particular must have the capability of implementing the
29042 standard dynamic model of elaboration with run-time checks.
29043
29044 In GNAT, this standard mode can be achieved either by the use of
29045 the @option{-gnatE} switch on the compiler (@command{gcc} or
29046 @command{gnatmake}) command, or by the use of the configuration pragma:
29047
29048 @smallexample @c ada
29049 pragma Elaboration_Checks (DYNAMIC);
29050 @end smallexample
29051
29052 @noindent
29053 Either approach will cause the unit affected to be compiled using the
29054 standard dynamic run-time elaboration checks described in the Ada
29055 Reference Manual. The static model is generally preferable, since it
29056 is clearly safer to rely on compile and link time checks rather than
29057 run-time checks. However, in the case of legacy code, it may be
29058 difficult to meet the requirements of the static model. This
29059 issue is further discussed in
29060 @ref{What to Do If the Default Elaboration Behavior Fails}.
29061
29062 Note that the static model provides a strict subset of the allowed
29063 behavior and programs of the Ada Reference Manual, so if you do
29064 adhere to the static model and no circularities exist,
29065 then you are assured that your program will
29066 work using the dynamic model, providing that you remove any
29067 pragma Elaborate statements from the source.
29068
29069 @node Treatment of Pragma Elaborate
29070 @section Treatment of Pragma Elaborate
29071 @cindex Pragma Elaborate
29072
29073 @noindent
29074 The use of @code{pragma Elaborate}
29075 should generally be avoided in Ada 95 and Ada 2005 programs,
29076 since there is no guarantee that transitive calls
29077 will be properly handled. Indeed at one point, this pragma was placed
29078 in Annex J (Obsolescent Features), on the grounds that it is never useful.
29079
29080 Now that's a bit restrictive. In practice, the case in which
29081 @code{pragma Elaborate} is useful is when the caller knows that there
29082 are no transitive calls, or that the called unit contains all necessary
29083 transitive @code{pragma Elaborate} statements, and legacy code often
29084 contains such uses.
29085
29086 Strictly speaking the static mode in GNAT should ignore such pragmas,
29087 since there is no assurance at compile time that the necessary safety
29088 conditions are met. In practice, this would cause GNAT to be incompatible
29089 with correctly written Ada 83 code that had all necessary
29090 @code{pragma Elaborate} statements in place. Consequently, we made the
29091 decision that GNAT in its default mode will believe that if it encounters
29092 a @code{pragma Elaborate} then the programmer knows what they are doing,
29093 and it will trust that no elaboration errors can occur.
29094
29095 The result of this decision is two-fold. First to be safe using the
29096 static mode, you should remove all @code{pragma Elaborate} statements.
29097 Second, when fixing circularities in existing code, you can selectively
29098 use @code{pragma Elaborate} statements to convince the static mode of
29099 GNAT that it need not generate an implicit @code{pragma Elaborate_All}
29100 statement.
29101
29102 When using the static mode with @option{-gnatwl}, any use of
29103 @code{pragma Elaborate} will generate a warning about possible
29104 problems.
29105
29106 @node Elaboration Issues for Library Tasks
29107 @section Elaboration Issues for Library Tasks
29108 @cindex Library tasks, elaboration issues
29109 @cindex Elaboration of library tasks
29110
29111 @noindent
29112 In this section we examine special elaboration issues that arise for
29113 programs that declare library level tasks.
29114
29115 Generally the model of execution of an Ada program is that all units are
29116 elaborated, and then execution of the program starts. However, the
29117 declaration of library tasks definitely does not fit this model. The
29118 reason for this is that library tasks start as soon as they are declared
29119 (more precisely, as soon as the statement part of the enclosing package
29120 body is reached), that is to say before elaboration
29121 of the program is complete. This means that if such a task calls a
29122 subprogram, or an entry in another task, the callee may or may not be
29123 elaborated yet, and in the standard
29124 Reference Manual model of dynamic elaboration checks, you can even
29125 get timing dependent Program_Error exceptions, since there can be
29126 a race between the elaboration code and the task code.
29127
29128 The static model of elaboration in GNAT seeks to avoid all such
29129 dynamic behavior, by being conservative, and the conservative
29130 approach in this particular case is to assume that all the code
29131 in a task body is potentially executed at elaboration time if
29132 a task is declared at the library level.
29133
29134 This can definitely result in unexpected circularities. Consider
29135 the following example
29136
29137 @smallexample @c ada
29138 package Decls is
29139 task Lib_Task is
29140 entry Start;
29141 end Lib_Task;
29142
29143 type My_Int is new Integer;
29144
29145 function Ident (M : My_Int) return My_Int;
29146 end Decls;
29147
29148 with Utils;
29149 package body Decls is
29150 task body Lib_Task is
29151 begin
29152 accept Start;
29153 Utils.Put_Val (2);
29154 end Lib_Task;
29155
29156 function Ident (M : My_Int) return My_Int is
29157 begin
29158 return M;
29159 end Ident;
29160 end Decls;
29161
29162 with Decls;
29163 package Utils is
29164 procedure Put_Val (Arg : Decls.My_Int);
29165 end Utils;
29166
29167 with Text_IO;
29168 package body Utils is
29169 procedure Put_Val (Arg : Decls.My_Int) is
29170 begin
29171 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
29172 end Put_Val;
29173 end Utils;
29174
29175 with Decls;
29176 procedure Main is
29177 begin
29178 Decls.Lib_Task.Start;
29179 end;
29180 @end smallexample
29181
29182 @noindent
29183 If the above example is compiled in the default static elaboration
29184 mode, then a circularity occurs. The circularity comes from the call
29185 @code{Utils.Put_Val} in the task body of @code{Decls.Lib_Task}. Since
29186 this call occurs in elaboration code, we need an implicit pragma
29187 @code{Elaborate_All} for @code{Utils}. This means that not only must
29188 the spec and body of @code{Utils} be elaborated before the body
29189 of @code{Decls}, but also the spec and body of any unit that is
29190 @code{with'ed} by the body of @code{Utils} must also be elaborated before
29191 the body of @code{Decls}. This is the transitive implication of
29192 pragma @code{Elaborate_All} and it makes sense, because in general
29193 the body of @code{Put_Val} might have a call to something in a
29194 @code{with'ed} unit.
29195
29196 In this case, the body of Utils (actually its spec) @code{with's}
29197 @code{Decls}. Unfortunately this means that the body of @code{Decls}
29198 must be elaborated before itself, in case there is a call from the
29199 body of @code{Utils}.
29200
29201 Here is the exact chain of events we are worrying about:
29202
29203 @enumerate
29204 @item
29205 In the body of @code{Decls} a call is made from within the body of a library
29206 task to a subprogram in the package @code{Utils}. Since this call may
29207 occur at elaboration time (given that the task is activated at elaboration
29208 time), we have to assume the worst, i.e., that the
29209 call does happen at elaboration time.
29210
29211 @item
29212 This means that the body and spec of @code{Util} must be elaborated before
29213 the body of @code{Decls} so that this call does not cause an access before
29214 elaboration.
29215
29216 @item
29217 Within the body of @code{Util}, specifically within the body of
29218 @code{Util.Put_Val} there may be calls to any unit @code{with}'ed
29219 by this package.
29220
29221 @item
29222 One such @code{with}'ed package is package @code{Decls}, so there
29223 might be a call to a subprogram in @code{Decls} in @code{Put_Val}.
29224 In fact there is such a call in this example, but we would have to
29225 assume that there was such a call even if it were not there, since
29226 we are not supposed to write the body of @code{Decls} knowing what
29227 is in the body of @code{Utils}; certainly in the case of the
29228 static elaboration model, the compiler does not know what is in
29229 other bodies and must assume the worst.
29230
29231 @item
29232 This means that the spec and body of @code{Decls} must also be
29233 elaborated before we elaborate the unit containing the call, but
29234 that unit is @code{Decls}! This means that the body of @code{Decls}
29235 must be elaborated before itself, and that's a circularity.
29236 @end enumerate
29237
29238 @noindent
29239 Indeed, if you add an explicit pragma @code{Elaborate_All} for @code{Utils} in
29240 the body of @code{Decls} you will get a true Ada Reference Manual
29241 circularity that makes the program illegal.
29242
29243 In practice, we have found that problems with the static model of
29244 elaboration in existing code often arise from library tasks, so
29245 we must address this particular situation.
29246
29247 Note that if we compile and run the program above, using the dynamic model of
29248 elaboration (that is to say use the @option{-gnatE} switch),
29249 then it compiles, binds,
29250 links, and runs, printing the expected result of 2. Therefore in some sense
29251 the circularity here is only apparent, and we need to capture
29252 the properties of this program that distinguish it from other library-level
29253 tasks that have real elaboration problems.
29254
29255 We have four possible answers to this question:
29256
29257 @itemize @bullet
29258
29259 @item
29260 Use the dynamic model of elaboration.
29261
29262 If we use the @option{-gnatE} switch, then as noted above, the program works.
29263 Why is this? If we examine the task body, it is apparent that the task cannot
29264 proceed past the
29265 @code{accept} statement until after elaboration has been completed, because
29266 the corresponding entry call comes from the main program, not earlier.
29267 This is why the dynamic model works here. But that's really giving
29268 up on a precise analysis, and we prefer to take this approach only if we cannot
29269 solve the
29270 problem in any other manner. So let us examine two ways to reorganize
29271 the program to avoid the potential elaboration problem.
29272
29273 @item
29274 Split library tasks into separate packages.
29275
29276 Write separate packages, so that library tasks are isolated from
29277 other declarations as much as possible. Let us look at a variation on
29278 the above program.
29279
29280 @smallexample @c ada
29281 package Decls1 is
29282 task Lib_Task is
29283 entry Start;
29284 end Lib_Task;
29285 end Decls1;
29286
29287 with Utils;
29288 package body Decls1 is
29289 task body Lib_Task is
29290 begin
29291 accept Start;
29292 Utils.Put_Val (2);
29293 end Lib_Task;
29294 end Decls1;
29295
29296 package Decls2 is
29297 type My_Int is new Integer;
29298 function Ident (M : My_Int) return My_Int;
29299 end Decls2;
29300
29301 with Utils;
29302 package body Decls2 is
29303 function Ident (M : My_Int) return My_Int is
29304 begin
29305 return M;
29306 end Ident;
29307 end Decls2;
29308
29309 with Decls2;
29310 package Utils is
29311 procedure Put_Val (Arg : Decls2.My_Int);
29312 end Utils;
29313
29314 with Text_IO;
29315 package body Utils is
29316 procedure Put_Val (Arg : Decls2.My_Int) is
29317 begin
29318 Text_IO.Put_Line (Decls2.My_Int'Image (Decls2.Ident (Arg)));
29319 end Put_Val;
29320 end Utils;
29321
29322 with Decls1;
29323 procedure Main is
29324 begin
29325 Decls1.Lib_Task.Start;
29326 end;
29327 @end smallexample
29328
29329 @noindent
29330 All we have done is to split @code{Decls} into two packages, one
29331 containing the library task, and one containing everything else. Now
29332 there is no cycle, and the program compiles, binds, links and executes
29333 using the default static model of elaboration.
29334
29335 @item
29336 Declare separate task types.
29337
29338 A significant part of the problem arises because of the use of the
29339 single task declaration form. This means that the elaboration of
29340 the task type, and the elaboration of the task itself (i.e.@: the
29341 creation of the task) happen at the same time. A good rule
29342 of style in Ada is to always create explicit task types. By
29343 following the additional step of placing task objects in separate
29344 packages from the task type declaration, many elaboration problems
29345 are avoided. Here is another modified example of the example program:
29346
29347 @smallexample @c ada
29348 package Decls is
29349 task type Lib_Task_Type is
29350 entry Start;
29351 end Lib_Task_Type;
29352
29353 type My_Int is new Integer;
29354
29355 function Ident (M : My_Int) return My_Int;
29356 end Decls;
29357
29358 with Utils;
29359 package body Decls is
29360 task body Lib_Task_Type is
29361 begin
29362 accept Start;
29363 Utils.Put_Val (2);
29364 end Lib_Task_Type;
29365
29366 function Ident (M : My_Int) return My_Int is
29367 begin
29368 return M;
29369 end Ident;
29370 end Decls;
29371
29372 with Decls;
29373 package Utils is
29374 procedure Put_Val (Arg : Decls.My_Int);
29375 end Utils;
29376
29377 with Text_IO;
29378 package body Utils is
29379 procedure Put_Val (Arg : Decls.My_Int) is
29380 begin
29381 Text_IO.Put_Line (Decls.My_Int'Image (Decls.Ident (Arg)));
29382 end Put_Val;
29383 end Utils;
29384
29385 with Decls;
29386 package Declst is
29387 Lib_Task : Decls.Lib_Task_Type;
29388 end Declst;
29389
29390 with Declst;
29391 procedure Main is
29392 begin
29393 Declst.Lib_Task.Start;
29394 end;
29395 @end smallexample
29396
29397 @noindent
29398 What we have done here is to replace the @code{task} declaration in
29399 package @code{Decls} with a @code{task type} declaration. Then we
29400 introduce a separate package @code{Declst} to contain the actual
29401 task object. This separates the elaboration issues for
29402 the @code{task type}
29403 declaration, which causes no trouble, from the elaboration issues
29404 of the task object, which is also unproblematic, since it is now independent
29405 of the elaboration of @code{Utils}.
29406 This separation of concerns also corresponds to
29407 a generally sound engineering principle of separating declarations
29408 from instances. This version of the program also compiles, binds, links,
29409 and executes, generating the expected output.
29410
29411 @item
29412 Use No_Entry_Calls_In_Elaboration_Code restriction.
29413 @cindex No_Entry_Calls_In_Elaboration_Code
29414
29415 The previous two approaches described how a program can be restructured
29416 to avoid the special problems caused by library task bodies. in practice,
29417 however, such restructuring may be difficult to apply to existing legacy code,
29418 so we must consider solutions that do not require massive rewriting.
29419
29420 Let us consider more carefully why our original sample program works
29421 under the dynamic model of elaboration. The reason is that the code
29422 in the task body blocks immediately on the @code{accept}
29423 statement. Now of course there is nothing to prohibit elaboration
29424 code from making entry calls (for example from another library level task),
29425 so we cannot tell in isolation that
29426 the task will not execute the accept statement during elaboration.
29427
29428 However, in practice it is very unusual to see elaboration code
29429 make any entry calls, and the pattern of tasks starting
29430 at elaboration time and then immediately blocking on @code{accept} or
29431 @code{select} statements is very common. What this means is that
29432 the compiler is being too pessimistic when it analyzes the
29433 whole package body as though it might be executed at elaboration
29434 time.
29435
29436 If we know that the elaboration code contains no entry calls, (a very safe
29437 assumption most of the time, that could almost be made the default
29438 behavior), then we can compile all units of the program under control
29439 of the following configuration pragma:
29440
29441 @smallexample
29442 pragma Restrictions (No_Entry_Calls_In_Elaboration_Code);
29443 @end smallexample
29444
29445 @noindent
29446 This pragma can be placed in the @file{gnat.adc} file in the usual
29447 manner. If we take our original unmodified program and compile it
29448 in the presence of a @file{gnat.adc} containing the above pragma,
29449 then once again, we can compile, bind, link, and execute, obtaining
29450 the expected result. In the presence of this pragma, the compiler does
29451 not trace calls in a task body, that appear after the first @code{accept}
29452 or @code{select} statement, and therefore does not report a potential
29453 circularity in the original program.
29454
29455 The compiler will check to the extent it can that the above
29456 restriction is not violated, but it is not always possible to do a
29457 complete check at compile time, so it is important to use this
29458 pragma only if the stated restriction is in fact met, that is to say
29459 no task receives an entry call before elaboration of all units is completed.
29460
29461 @end itemize
29462
29463 @node Mixing Elaboration Models
29464 @section Mixing Elaboration Models
29465 @noindent
29466 So far, we have assumed that the entire program is either compiled
29467 using the dynamic model or static model, ensuring consistency. It
29468 is possible to mix the two models, but rules have to be followed
29469 if this mixing is done to ensure that elaboration checks are not
29470 omitted.
29471
29472 The basic rule is that @emph{a unit compiled with the static model cannot
29473 be @code{with'ed} by a unit compiled with the dynamic model}. The
29474 reason for this is that in the static model, a unit assumes that
29475 its clients guarantee to use (the equivalent of) pragma
29476 @code{Elaborate_All} so that no elaboration checks are required
29477 in inner subprograms, and this assumption is violated if the
29478 client is compiled with dynamic checks.
29479
29480 The precise rule is as follows. A unit that is compiled with dynamic
29481 checks can only @code{with} a unit that meets at least one of the
29482 following criteria:
29483
29484 @itemize @bullet
29485
29486 @item
29487 The @code{with'ed} unit is itself compiled with dynamic elaboration
29488 checks (that is with the @option{-gnatE} switch.
29489
29490 @item
29491 The @code{with'ed} unit is an internal GNAT implementation unit from
29492 the System, Interfaces, Ada, or GNAT hierarchies.
29493
29494 @item
29495 The @code{with'ed} unit has pragma Preelaborate or pragma Pure.
29496
29497 @item
29498 The @code{with'ing} unit (that is the client) has an explicit pragma
29499 @code{Elaborate_All} for the @code{with'ed} unit.
29500
29501 @end itemize
29502
29503 @noindent
29504 If this rule is violated, that is if a unit with dynamic elaboration
29505 checks @code{with's} a unit that does not meet one of the above four
29506 criteria, then the binder (@code{gnatbind}) will issue a warning
29507 similar to that in the following example:
29508
29509 @smallexample
29510 warning: "x.ads" has dynamic elaboration checks and with's
29511 warning: "y.ads" which has static elaboration checks
29512 @end smallexample
29513
29514 @noindent
29515 These warnings indicate that the rule has been violated, and that as a result
29516 elaboration checks may be missed in the resulting executable file.
29517 This warning may be suppressed using the @option{-ws} binder switch
29518 in the usual manner.
29519
29520 One useful application of this mixing rule is in the case of a subsystem
29521 which does not itself @code{with} units from the remainder of the
29522 application. In this case, the entire subsystem can be compiled with
29523 dynamic checks to resolve a circularity in the subsystem, while
29524 allowing the main application that uses this subsystem to be compiled
29525 using the more reliable default static model.
29526
29527 @node What to Do If the Default Elaboration Behavior Fails
29528 @section What to Do If the Default Elaboration Behavior Fails
29529
29530 @noindent
29531 If the binder cannot find an acceptable order, it outputs detailed
29532 diagnostics. For example:
29533 @smallexample
29534 @group
29535 @iftex
29536 @leftskip=0cm
29537 @end iftex
29538 error: elaboration circularity detected
29539 info: "proc (body)" must be elaborated before "pack (body)"
29540 info: reason: Elaborate_All probably needed in unit "pack (body)"
29541 info: recompile "pack (body)" with -gnatwl
29542 info: for full details
29543 info: "proc (body)"
29544 info: is needed by its spec:
29545 info: "proc (spec)"
29546 info: which is withed by:
29547 info: "pack (body)"
29548 info: "pack (body)" must be elaborated before "proc (body)"
29549 info: reason: pragma Elaborate in unit "proc (body)"
29550 @end group
29551
29552 @end smallexample
29553
29554 @noindent
29555 In this case we have a cycle that the binder cannot break. On the one
29556 hand, there is an explicit pragma Elaborate in @code{proc} for
29557 @code{pack}. This means that the body of @code{pack} must be elaborated
29558 before the body of @code{proc}. On the other hand, there is elaboration
29559 code in @code{pack} that calls a subprogram in @code{proc}. This means
29560 that for maximum safety, there should really be a pragma
29561 Elaborate_All in @code{pack} for @code{proc} which would require that
29562 the body of @code{proc} be elaborated before the body of
29563 @code{pack}. Clearly both requirements cannot be satisfied.
29564 Faced with a circularity of this kind, you have three different options.
29565
29566 @table @asis
29567 @item Fix the program
29568 The most desirable option from the point of view of long-term maintenance
29569 is to rearrange the program so that the elaboration problems are avoided.
29570 One useful technique is to place the elaboration code into separate
29571 child packages. Another is to move some of the initialization code to
29572 explicitly called subprograms, where the program controls the order
29573 of initialization explicitly. Although this is the most desirable option,
29574 it may be impractical and involve too much modification, especially in
29575 the case of complex legacy code.
29576
29577 @item Perform dynamic checks
29578 If the compilations are done using the
29579 @option{-gnatE}
29580 (dynamic elaboration check) switch, then GNAT behaves in a quite different
29581 manner. Dynamic checks are generated for all calls that could possibly result
29582 in raising an exception. With this switch, the compiler does not generate
29583 implicit @code{Elaborate} or @code{Elaborate_All} pragmas. The behavior then is
29584 exactly as specified in the @cite{Ada Reference Manual}.
29585 The binder will generate
29586 an executable program that may or may not raise @code{Program_Error}, and then
29587 it is the programmer's job to ensure that it does not raise an exception. Note
29588 that it is important to compile all units with the switch, it cannot be used
29589 selectively.
29590
29591 @item Suppress checks
29592 The drawback of dynamic checks is that they generate a
29593 significant overhead at run time, both in space and time. If you
29594 are absolutely sure that your program cannot raise any elaboration
29595 exceptions, and you still want to use the dynamic elaboration model,
29596 then you can use the configuration pragma
29597 @code{Suppress (Elaboration_Check)} to suppress all such checks. For
29598 example this pragma could be placed in the @file{gnat.adc} file.
29599
29600 @item Suppress checks selectively
29601 When you know that certain calls or instantiations in elaboration code cannot
29602 possibly lead to an elaboration error, and the binder nevertheless complains
29603 about implicit @code{Elaborate} and @code{Elaborate_All} pragmas that lead to
29604 elaboration circularities, it is possible to remove those warnings locally and
29605 obtain a program that will bind. Clearly this can be unsafe, and it is the
29606 responsibility of the programmer to make sure that the resulting program has no
29607 elaboration anomalies. The pragma @code{Suppress (Elaboration_Check)} can be
29608 used with different granularity to suppress warnings and break elaboration
29609 circularities:
29610
29611 @itemize @bullet
29612 @item
29613 Place the pragma that names the called subprogram in the declarative part
29614 that contains the call.
29615
29616 @item
29617 Place the pragma in the declarative part, without naming an entity. This
29618 disables warnings on all calls in the corresponding declarative region.
29619
29620 @item
29621 Place the pragma in the package spec that declares the called subprogram,
29622 and name the subprogram. This disables warnings on all elaboration calls to
29623 that subprogram.
29624
29625 @item
29626 Place the pragma in the package spec that declares the called subprogram,
29627 without naming any entity. This disables warnings on all elaboration calls to
29628 all subprograms declared in this spec.
29629
29630 @item Use Pragma Elaborate
29631 As previously described in section @xref{Treatment of Pragma Elaborate},
29632 GNAT in static mode assumes that a @code{pragma} Elaborate indicates correctly
29633 that no elaboration checks are required on calls to the designated unit.
29634 There may be cases in which the caller knows that no transitive calls
29635 can occur, so that a @code{pragma Elaborate} will be sufficient in a
29636 case where @code{pragma Elaborate_All} would cause a circularity.
29637 @end itemize
29638
29639 @noindent
29640 These five cases are listed in order of decreasing safety, and therefore
29641 require increasing programmer care in their application. Consider the
29642 following program:
29643
29644 @smallexample @c adanocomment
29645 package Pack1 is
29646 function F1 return Integer;
29647 X1 : Integer;
29648 end Pack1;
29649
29650 package Pack2 is
29651 function F2 return Integer;
29652 function Pure (x : integer) return integer;
29653 -- pragma Suppress (Elaboration_Check, On => Pure); -- (3)
29654 -- pragma Suppress (Elaboration_Check); -- (4)
29655 end Pack2;
29656
29657 with Pack2;
29658 package body Pack1 is
29659 function F1 return Integer is
29660 begin
29661 return 100;
29662 end F1;
29663 Val : integer := Pack2.Pure (11); -- Elab. call (1)
29664 begin
29665 declare
29666 -- pragma Suppress(Elaboration_Check, Pack2.F2); -- (1)
29667 -- pragma Suppress(Elaboration_Check); -- (2)
29668 begin
29669 X1 := Pack2.F2 + 1; -- Elab. call (2)
29670 end;
29671 end Pack1;
29672
29673 with Pack1;
29674 package body Pack2 is
29675 function F2 return Integer is
29676 begin
29677 return Pack1.F1;
29678 end F2;
29679 function Pure (x : integer) return integer is
29680 begin
29681 return x ** 3 - 3 * x;
29682 end;
29683 end Pack2;
29684
29685 with Pack1, Ada.Text_IO;
29686 procedure Proc3 is
29687 begin
29688 Ada.Text_IO.Put_Line(Pack1.X1'Img); -- 101
29689 end Proc3;
29690 @end smallexample
29691 In the absence of any pragmas, an attempt to bind this program produces
29692 the following diagnostics:
29693 @smallexample
29694 @group
29695 @iftex
29696 @leftskip=.5cm
29697 @end iftex
29698 error: elaboration circularity detected
29699 info: "pack1 (body)" must be elaborated before "pack1 (body)"
29700 info: reason: Elaborate_All probably needed in unit "pack1 (body)"
29701 info: recompile "pack1 (body)" with -gnatwl for full details
29702 info: "pack1 (body)"
29703 info: must be elaborated along with its spec:
29704 info: "pack1 (spec)"
29705 info: which is withed by:
29706 info: "pack2 (body)"
29707 info: which must be elaborated along with its spec:
29708 info: "pack2 (spec)"
29709 info: which is withed by:
29710 info: "pack1 (body)"
29711 @end group
29712 @end smallexample
29713 The sources of the circularity are the two calls to @code{Pack2.Pure} and
29714 @code{Pack2.F2} in the body of @code{Pack1}. We can see that the call to
29715 F2 is safe, even though F2 calls F1, because the call appears after the
29716 elaboration of the body of F1. Therefore the pragma (1) is safe, and will
29717 remove the warning on the call. It is also possible to use pragma (2)
29718 because there are no other potentially unsafe calls in the block.
29719
29720 @noindent
29721 The call to @code{Pure} is safe because this function does not depend on the
29722 state of @code{Pack2}. Therefore any call to this function is safe, and it
29723 is correct to place pragma (3) in the corresponding package spec.
29724
29725 @noindent
29726 Finally, we could place pragma (4) in the spec of @code{Pack2} to disable
29727 warnings on all calls to functions declared therein. Note that this is not
29728 necessarily safe, and requires more detailed examination of the subprogram
29729 bodies involved. In particular, a call to @code{F2} requires that @code{F1}
29730 be already elaborated.
29731 @end table
29732
29733 @noindent
29734 It is hard to generalize on which of these four approaches should be
29735 taken. Obviously if it is possible to fix the program so that the default
29736 treatment works, this is preferable, but this may not always be practical.
29737 It is certainly simple enough to use
29738 @option{-gnatE}
29739 but the danger in this case is that, even if the GNAT binder
29740 finds a correct elaboration order, it may not always do so,
29741 and certainly a binder from another Ada compiler might not. A
29742 combination of testing and analysis (for which the warnings generated
29743 with the
29744 @option{-gnatwl}
29745 switch can be useful) must be used to ensure that the program is free
29746 of errors. One switch that is useful in this testing is the
29747 @option{^-p (pessimistic elaboration order)^/PESSIMISTIC_ELABORATION_ORDER^}
29748 switch for
29749 @code{gnatbind}.
29750 Normally the binder tries to find an order that has the best chance
29751 of avoiding elaboration problems. However, if this switch is used, the binder
29752 plays a devil's advocate role, and tries to choose the order that
29753 has the best chance of failing. If your program works even with this
29754 switch, then it has a better chance of being error free, but this is still
29755 not a guarantee.
29756
29757 For an example of this approach in action, consider the C-tests (executable
29758 tests) from the ACVC suite. If these are compiled and run with the default
29759 treatment, then all but one of them succeed without generating any error
29760 diagnostics from the binder. However, there is one test that fails, and
29761 this is not surprising, because the whole point of this test is to ensure
29762 that the compiler can handle cases where it is impossible to determine
29763 a correct order statically, and it checks that an exception is indeed
29764 raised at run time.
29765
29766 This one test must be compiled and run using the
29767 @option{-gnatE}
29768 switch, and then it passes. Alternatively, the entire suite can
29769 be run using this switch. It is never wrong to run with the dynamic
29770 elaboration switch if your code is correct, and we assume that the
29771 C-tests are indeed correct (it is less efficient, but efficiency is
29772 not a factor in running the ACVC tests.)
29773
29774 @node Elaboration for Access-to-Subprogram Values
29775 @section Elaboration for Access-to-Subprogram Values
29776 @cindex Access-to-subprogram
29777
29778 @noindent
29779 Access-to-subprogram types (introduced in Ada 95) complicate
29780 the handling of elaboration. The trouble is that it becomes
29781 impossible to tell at compile time which procedure
29782 is being called. This means that it is not possible for the binder
29783 to analyze the elaboration requirements in this case.
29784
29785 If at the point at which the access value is created
29786 (i.e., the evaluation of @code{P'Access} for a subprogram @code{P}),
29787 the body of the subprogram is
29788 known to have been elaborated, then the access value is safe, and its use
29789 does not require a check. This may be achieved by appropriate arrangement
29790 of the order of declarations if the subprogram is in the current unit,
29791 or, if the subprogram is in another unit, by using pragma
29792 @code{Pure}, @code{Preelaborate}, or @code{Elaborate_Body}
29793 on the referenced unit.
29794
29795 If the referenced body is not known to have been elaborated at the point
29796 the access value is created, then any use of the access value must do a
29797 dynamic check, and this dynamic check will fail and raise a
29798 @code{Program_Error} exception if the body has not been elaborated yet.
29799 GNAT will generate the necessary checks, and in addition, if the
29800 @option{-gnatwl}
29801 switch is set, will generate warnings that such checks are required.
29802
29803 The use of dynamic dispatching for tagged types similarly generates
29804 a requirement for dynamic checks, and premature calls to any primitive
29805 operation of a tagged type before the body of the operation has been
29806 elaborated, will result in the raising of @code{Program_Error}.
29807
29808 @node Summary of Procedures for Elaboration Control
29809 @section Summary of Procedures for Elaboration Control
29810 @cindex Elaboration control
29811
29812 @noindent
29813 First, compile your program with the default options, using none of
29814 the special elaboration control switches. If the binder successfully
29815 binds your program, then you can be confident that, apart from issues
29816 raised by the use of access-to-subprogram types and dynamic dispatching,
29817 the program is free of elaboration errors. If it is important that the
29818 program be portable, then use the
29819 @option{-gnatwl}
29820 switch to generate warnings about missing @code{Elaborate} or
29821 @code{Elaborate_All} pragmas, and supply the missing pragmas.
29822
29823 If the program fails to bind using the default static elaboration
29824 handling, then you can fix the program to eliminate the binder
29825 message, or recompile the entire program with the
29826 @option{-gnatE} switch to generate dynamic elaboration checks,
29827 and, if you are sure there really are no elaboration problems,
29828 use a global pragma @code{Suppress (Elaboration_Check)}.
29829
29830 @node Other Elaboration Order Considerations
29831 @section Other Elaboration Order Considerations
29832 @noindent
29833 This section has been entirely concerned with the issue of finding a valid
29834 elaboration order, as defined by the Ada Reference Manual. In a case
29835 where several elaboration orders are valid, the task is to find one
29836 of the possible valid elaboration orders (and the static model in GNAT
29837 will ensure that this is achieved).
29838
29839 The purpose of the elaboration rules in the Ada Reference Manual is to
29840 make sure that no entity is accessed before it has been elaborated. For
29841 a subprogram, this means that the spec and body must have been elaborated
29842 before the subprogram is called. For an object, this means that the object
29843 must have been elaborated before its value is read or written. A violation
29844 of either of these two requirements is an access before elaboration order,
29845 and this section has been all about avoiding such errors.
29846
29847 In the case where more than one order of elaboration is possible, in the
29848 sense that access before elaboration errors are avoided, then any one of
29849 the orders is ``correct'' in the sense that it meets the requirements of
29850 the Ada Reference Manual, and no such error occurs.
29851
29852 However, it may be the case for a given program, that there are
29853 constraints on the order of elaboration that come not from consideration
29854 of avoiding elaboration errors, but rather from extra-lingual logic
29855 requirements. Consider this example:
29856
29857 @smallexample @c ada
29858 with Init_Constants;
29859 package Constants is
29860 X : Integer := 0;
29861 Y : Integer := 0;
29862 end Constants;
29863
29864 package Init_Constants is
29865 procedure P; -- require a body
29866 end Init_Constants;
29867
29868 with Constants;
29869 package body Init_Constants is
29870 procedure P is begin null; end;
29871 begin
29872 Constants.X := 3;
29873 Constants.Y := 4;
29874 end Init_Constants;
29875
29876 with Constants;
29877 package Calc is
29878 Z : Integer := Constants.X + Constants.Y;
29879 end Calc;
29880
29881 with Calc;
29882 with Text_IO; use Text_IO;
29883 procedure Main is
29884 begin
29885 Put_Line (Calc.Z'Img);
29886 end Main;
29887 @end smallexample
29888
29889 @noindent
29890 In this example, there is more than one valid order of elaboration. For
29891 example both the following are correct orders:
29892
29893 @smallexample
29894 Init_Constants spec
29895 Constants spec
29896 Calc spec
29897 Init_Constants body
29898 Main body
29899
29900 and
29901
29902 Init_Constants spec
29903 Init_Constants body
29904 Constants spec
29905 Calc spec
29906 Main body
29907 @end smallexample
29908
29909 @noindent
29910 There is no language rule to prefer one or the other, both are correct
29911 from an order of elaboration point of view. But the programmatic effects
29912 of the two orders are very different. In the first, the elaboration routine
29913 of @code{Calc} initializes @code{Z} to zero, and then the main program
29914 runs with this value of zero. But in the second order, the elaboration
29915 routine of @code{Calc} runs after the body of Init_Constants has set
29916 @code{X} and @code{Y} and thus @code{Z} is set to 7 before @code{Main}
29917 runs.
29918
29919 One could perhaps by applying pretty clever non-artificial intelligence
29920 to the situation guess that it is more likely that the second order of
29921 elaboration is the one desired, but there is no formal linguistic reason
29922 to prefer one over the other. In fact in this particular case, GNAT will
29923 prefer the second order, because of the rule that bodies are elaborated
29924 as soon as possible, but it's just luck that this is what was wanted
29925 (if indeed the second order was preferred).
29926
29927 If the program cares about the order of elaboration routines in a case like
29928 this, it is important to specify the order required. In this particular
29929 case, that could have been achieved by adding to the spec of Calc:
29930
29931 @smallexample @c ada
29932 pragma Elaborate_All (Constants);
29933 @end smallexample
29934
29935 @noindent
29936 which requires that the body (if any) and spec of @code{Constants},
29937 as well as the body and spec of any unit @code{with}'ed by
29938 @code{Constants} be elaborated before @code{Calc} is elaborated.
29939
29940 Clearly no automatic method can always guess which alternative you require,
29941 and if you are working with legacy code that had constraints of this kind
29942 which were not properly specified by adding @code{Elaborate} or
29943 @code{Elaborate_All} pragmas, then indeed it is possible that two different
29944 compilers can choose different orders.
29945
29946 However, GNAT does attempt to diagnose the common situation where there
29947 are uninitialized variables in the visible part of a package spec, and the
29948 corresponding package body has an elaboration block that directly or
29949 indirectly initialized one or more of these variables. This is the situation
29950 in which a pragma Elaborate_Body is usually desirable, and GNAT will generate
29951 a warning that suggests this addition if it detects this situation.
29952
29953 The @code{gnatbind}
29954 @option{^-p^/PESSIMISTIC_ELABORATION^} switch may be useful in smoking
29955 out problems. This switch causes bodies to be elaborated as late as possible
29956 instead of as early as possible. In the example above, it would have forced
29957 the choice of the first elaboration order. If you get different results
29958 when using this switch, and particularly if one set of results is right,
29959 and one is wrong as far as you are concerned, it shows that you have some
29960 missing @code{Elaborate} pragmas. For the example above, we have the
29961 following output:
29962
29963 @smallexample
29964 gnatmake -f -q main
29965 main
29966 7
29967 gnatmake -f -q main -bargs -p
29968 main
29969 0
29970 @end smallexample
29971
29972 @noindent
29973 It is of course quite unlikely that both these results are correct, so
29974 it is up to you in a case like this to investigate the source of the
29975 difference, by looking at the two elaboration orders that are chosen,
29976 and figuring out which is correct, and then adding the necessary
29977 @code{Elaborate} or @code{Elaborate_All} pragmas to ensure the desired order.
29978
29979
29980
29981 @c *******************************
29982 @node Conditional Compilation
29983 @appendix Conditional Compilation
29984 @c *******************************
29985 @cindex Conditional compilation
29986
29987 @noindent
29988 It is often necessary to arrange for a single source program
29989 to serve multiple purposes, where it is compiled in different
29990 ways to achieve these different goals. Some examples of the
29991 need for this feature are
29992
29993 @itemize @bullet
29994 @item Adapting a program to a different hardware environment
29995 @item Adapting a program to a different target architecture
29996 @item Turning debugging features on and off
29997 @item Arranging for a program to compile with different compilers
29998 @end itemize
29999
30000 @noindent
30001 In C, or C++, the typical approach would be to use the preprocessor
30002 that is defined as part of the language. The Ada language does not
30003 contain such a feature. This is not an oversight, but rather a very
30004 deliberate design decision, based on the experience that overuse of
30005 the preprocessing features in C and C++ can result in programs that
30006 are extremely difficult to maintain. For example, if we have ten
30007 switches that can be on or off, this means that there are a thousand
30008 separate programs, any one of which might not even be syntactically
30009 correct, and even if syntactically correct, the resulting program
30010 might not work correctly. Testing all combinations can quickly become
30011 impossible.
30012
30013 Nevertheless, the need to tailor programs certainly exists, and in
30014 this Appendix we will discuss how this can
30015 be achieved using Ada in general, and GNAT in particular.
30016
30017 @menu
30018 * Use of Boolean Constants::
30019 * Debugging - A Special Case::
30020 * Conditionalizing Declarations::
30021 * Use of Alternative Implementations::
30022 * Preprocessing::
30023 @end menu
30024
30025 @node Use of Boolean Constants
30026 @section Use of Boolean Constants
30027
30028 @noindent
30029 In the case where the difference is simply which code
30030 sequence is executed, the cleanest solution is to use Boolean
30031 constants to control which code is executed.
30032
30033 @smallexample @c ada
30034 @group
30035 FP_Initialize_Required : constant Boolean := True;
30036 @dots{}
30037 if FP_Initialize_Required then
30038 @dots{}
30039 end if;
30040 @end group
30041 @end smallexample
30042
30043 @noindent
30044 Not only will the code inside the @code{if} statement not be executed if
30045 the constant Boolean is @code{False}, but it will also be completely
30046 deleted from the program.
30047 However, the code is only deleted after the @code{if} statement
30048 has been checked for syntactic and semantic correctness.
30049 (In contrast, with preprocessors the code is deleted before the
30050 compiler ever gets to see it, so it is not checked until the switch
30051 is turned on.)
30052 @cindex Preprocessors (contrasted with conditional compilation)
30053
30054 Typically the Boolean constants will be in a separate package,
30055 something like:
30056
30057 @smallexample @c ada
30058 @group
30059 package Config is
30060 FP_Initialize_Required : constant Boolean := True;
30061 Reset_Available : constant Boolean := False;
30062 @dots{}
30063 end Config;
30064 @end group
30065 @end smallexample
30066
30067 @noindent
30068 The @code{Config} package exists in multiple forms for the various targets,
30069 with an appropriate script selecting the version of @code{Config} needed.
30070 Then any other unit requiring conditional compilation can do a @code{with}
30071 of @code{Config} to make the constants visible.
30072
30073
30074 @node Debugging - A Special Case
30075 @section Debugging - A Special Case
30076
30077 @noindent
30078 A common use of conditional code is to execute statements (for example
30079 dynamic checks, or output of intermediate results) under control of a
30080 debug switch, so that the debugging behavior can be turned on and off.
30081 This can be done using a Boolean constant to control whether the code
30082 is active:
30083
30084 @smallexample @c ada
30085 @group
30086 if Debugging then
30087 Put_Line ("got to the first stage!");
30088 end if;
30089 @end group
30090 @end smallexample
30091
30092 @noindent
30093 or
30094
30095 @smallexample @c ada
30096 @group
30097 if Debugging and then Temperature > 999.0 then
30098 raise Temperature_Crazy;
30099 end if;
30100 @end group
30101 @end smallexample
30102
30103 @noindent
30104 Since this is a common case, there are special features to deal with
30105 this in a convenient manner. For the case of tests, Ada 2005 has added
30106 a pragma @code{Assert} that can be used for such tests. This pragma is modeled
30107 @cindex pragma @code{Assert}
30108 on the @code{Assert} pragma that has always been available in GNAT, so this
30109 feature may be used with GNAT even if you are not using Ada 2005 features.
30110 The use of pragma @code{Assert} is described in
30111 @ref{Pragma Assert,,, gnat_rm, GNAT Reference Manual}, but as an
30112 example, the last test could be written:
30113
30114 @smallexample @c ada
30115 pragma Assert (Temperature <= 999.0, "Temperature Crazy");
30116 @end smallexample
30117
30118 @noindent
30119 or simply
30120
30121 @smallexample @c ada
30122 pragma Assert (Temperature <= 999.0);
30123 @end smallexample
30124
30125 @noindent
30126 In both cases, if assertions are active and the temperature is excessive,
30127 the exception @code{Assert_Failure} will be raised, with the given string in
30128 the first case or a string indicating the location of the pragma in the second
30129 case used as the exception message.
30130
30131 You can turn assertions on and off by using the @code{Assertion_Policy}
30132 pragma.
30133 @cindex pragma @code{Assertion_Policy}
30134 This is an Ada 2005 pragma which is implemented in all modes by
30135 GNAT, but only in the latest versions of GNAT which include Ada 2005
30136 capability. Alternatively, you can use the @option{-gnata} switch
30137 @cindex @option{-gnata} switch
30138 to enable assertions from the command line (this is recognized by all versions
30139 of GNAT).
30140
30141 For the example above with the @code{Put_Line}, the GNAT-specific pragma
30142 @code{Debug} can be used:
30143 @cindex pragma @code{Debug}
30144
30145 @smallexample @c ada
30146 pragma Debug (Put_Line ("got to the first stage!"));
30147 @end smallexample
30148
30149 @noindent
30150 If debug pragmas are enabled, the argument, which must be of the form of
30151 a procedure call, is executed (in this case, @code{Put_Line} will be called).
30152 Only one call can be present, but of course a special debugging procedure
30153 containing any code you like can be included in the program and then
30154 called in a pragma @code{Debug} argument as needed.
30155
30156 One advantage of pragma @code{Debug} over the @code{if Debugging then}
30157 construct is that pragma @code{Debug} can appear in declarative contexts,
30158 such as at the very beginning of a procedure, before local declarations have
30159 been elaborated.
30160
30161 Debug pragmas are enabled using either the @option{-gnata} switch that also
30162 controls assertions, or with a separate Debug_Policy pragma.
30163 @cindex pragma @code{Debug_Policy}
30164 The latter pragma is new in the Ada 2005 versions of GNAT (but it can be used
30165 in Ada 95 and Ada 83 programs as well), and is analogous to
30166 pragma @code{Assertion_Policy} to control assertions.
30167
30168 @code{Assertion_Policy} and @code{Debug_Policy} are configuration pragmas,
30169 and thus they can appear in @file{gnat.adc} if you are not using a
30170 project file, or in the file designated to contain configuration pragmas
30171 in a project file.
30172 They then apply to all subsequent compilations. In practice the use of
30173 the @option{-gnata} switch is often the most convenient method of controlling
30174 the status of these pragmas.
30175
30176 Note that a pragma is not a statement, so in contexts where a statement
30177 sequence is required, you can't just write a pragma on its own. You have
30178 to add a @code{null} statement.
30179
30180 @smallexample @c ada
30181 @group
30182 if @dots{} then
30183 @dots{} -- some statements
30184 else
30185 pragma Assert (Num_Cases < 10);
30186 null;
30187 end if;
30188 @end group
30189 @end smallexample
30190
30191
30192 @node Conditionalizing Declarations
30193 @section Conditionalizing Declarations
30194
30195 @noindent
30196 In some cases, it may be necessary to conditionalize declarations to meet
30197 different requirements. For example we might want a bit string whose length
30198 is set to meet some hardware message requirement.
30199
30200 In some cases, it may be possible to do this using declare blocks controlled
30201 by conditional constants:
30202
30203 @smallexample @c ada
30204 @group
30205 if Small_Machine then
30206 declare
30207 X : Bit_String (1 .. 10);
30208 begin
30209 @dots{}
30210 end;
30211 else
30212 declare
30213 X : Large_Bit_String (1 .. 1000);
30214 begin
30215 @dots{}
30216 end;
30217 end if;
30218 @end group
30219 @end smallexample
30220
30221 @noindent
30222 Note that in this approach, both declarations are analyzed by the
30223 compiler so this can only be used where both declarations are legal,
30224 even though one of them will not be used.
30225
30226 Another approach is to define integer constants, e.g.@: @code{Bits_Per_Word}, or
30227 Boolean constants, e.g.@: @code{Little_Endian}, and then write declarations
30228 that are parameterized by these constants. For example
30229
30230 @smallexample @c ada
30231 @group
30232 for Rec use
30233 Field1 at 0 range Boolean'Pos (Little_Endian) * 10 .. Bits_Per_Word;
30234 end record;
30235 @end group
30236 @end smallexample
30237
30238 @noindent
30239 If @code{Bits_Per_Word} is set to 32, this generates either
30240
30241 @smallexample @c ada
30242 @group
30243 for Rec use
30244 Field1 at 0 range 0 .. 32;
30245 end record;
30246 @end group
30247 @end smallexample
30248
30249 @noindent
30250 for the big endian case, or
30251
30252 @smallexample @c ada
30253 @group
30254 for Rec use record
30255 Field1 at 0 range 10 .. 32;
30256 end record;
30257 @end group
30258 @end smallexample
30259
30260 @noindent
30261 for the little endian case. Since a powerful subset of Ada expression
30262 notation is usable for creating static constants, clever use of this
30263 feature can often solve quite difficult problems in conditionalizing
30264 compilation (note incidentally that in Ada 95, the little endian
30265 constant was introduced as @code{System.Default_Bit_Order}, so you do not
30266 need to define this one yourself).
30267
30268
30269 @node Use of Alternative Implementations
30270 @section Use of Alternative Implementations
30271
30272 @noindent
30273 In some cases, none of the approaches described above are adequate. This
30274 can occur for example if the set of declarations required is radically
30275 different for two different configurations.
30276
30277 In this situation, the official Ada way of dealing with conditionalizing
30278 such code is to write separate units for the different cases. As long as
30279 this does not result in excessive duplication of code, this can be done
30280 without creating maintenance problems. The approach is to share common
30281 code as far as possible, and then isolate the code and declarations
30282 that are different. Subunits are often a convenient method for breaking
30283 out a piece of a unit that is to be conditionalized, with separate files
30284 for different versions of the subunit for different targets, where the
30285 build script selects the right one to give to the compiler.
30286 @cindex Subunits (and conditional compilation)
30287
30288 As an example, consider a situation where a new feature in Ada 2005
30289 allows something to be done in a really nice way. But your code must be able
30290 to compile with an Ada 95 compiler. Conceptually you want to say:
30291
30292 @smallexample @c ada
30293 @group
30294 if Ada_2005 then
30295 @dots{} neat Ada 2005 code
30296 else
30297 @dots{} not quite as neat Ada 95 code
30298 end if;
30299 @end group
30300 @end smallexample
30301
30302 @noindent
30303 where @code{Ada_2005} is a Boolean constant.
30304
30305 But this won't work when @code{Ada_2005} is set to @code{False},
30306 since the @code{then} clause will be illegal for an Ada 95 compiler.
30307 (Recall that although such unreachable code would eventually be deleted
30308 by the compiler, it still needs to be legal. If it uses features
30309 introduced in Ada 2005, it will be illegal in Ada 95.)
30310
30311 So instead we write
30312
30313 @smallexample @c ada
30314 procedure Insert is separate;
30315 @end smallexample
30316
30317 @noindent
30318 Then we have two files for the subunit @code{Insert}, with the two sets of
30319 code.
30320 If the package containing this is called @code{File_Queries}, then we might
30321 have two files
30322
30323 @itemize @bullet
30324 @item @file{file_queries-insert-2005.adb}
30325 @item @file{file_queries-insert-95.adb}
30326 @end itemize
30327
30328 @noindent
30329 and the build script renames the appropriate file to
30330
30331 @smallexample
30332 file_queries-insert.adb
30333 @end smallexample
30334
30335 @noindent
30336 and then carries out the compilation.
30337
30338 This can also be done with project files' naming schemes. For example:
30339
30340 @smallexample @c project
30341 For Body ("File_Queries.Insert") use "file_queries-insert-2005.ada";
30342 @end smallexample
30343
30344 @noindent
30345 Note also that with project files it is desirable to use a different extension
30346 than @file{ads} / @file{adb} for alternative versions. Otherwise a naming
30347 conflict may arise through another commonly used feature: to declare as part
30348 of the project a set of directories containing all the sources obeying the
30349 default naming scheme.
30350
30351 The use of alternative units is certainly feasible in all situations,
30352 and for example the Ada part of the GNAT run-time is conditionalized
30353 based on the target architecture using this approach. As a specific example,
30354 consider the implementation of the AST feature in VMS. There is one
30355 spec:
30356
30357 @smallexample
30358 s-asthan.ads
30359 @end smallexample
30360
30361 @noindent
30362 which is the same for all architectures, and three bodies:
30363
30364 @table @file
30365 @item s-asthan.adb
30366 used for all non-VMS operating systems
30367 @item s-asthan-vms-alpha.adb
30368 used for VMS on the Alpha
30369 @item s-asthan-vms-ia64.adb
30370 used for VMS on the ia64
30371 @end table
30372
30373 @noindent
30374 The dummy version @file{s-asthan.adb} simply raises exceptions noting that
30375 this operating system feature is not available, and the two remaining
30376 versions interface with the corresponding versions of VMS to provide
30377 VMS-compatible AST handling. The GNAT build script knows the architecture
30378 and operating system, and automatically selects the right version,
30379 renaming it if necessary to @file{s-asthan.adb} before the run-time build.
30380
30381 Another style for arranging alternative implementations is through Ada's
30382 access-to-subprogram facility.
30383 In case some functionality is to be conditionally included,
30384 you can declare an access-to-procedure variable @code{Ref} that is initialized
30385 to designate a ``do nothing'' procedure, and then invoke @code{Ref.all}
30386 when appropriate.
30387 In some library package, set @code{Ref} to @code{Proc'Access} for some
30388 procedure @code{Proc} that performs the relevant processing.
30389 The initialization only occurs if the library package is included in the
30390 program.
30391 The same idea can also be implemented using tagged types and dispatching
30392 calls.
30393
30394
30395 @node Preprocessing
30396 @section Preprocessing
30397 @cindex Preprocessing
30398
30399 @noindent
30400 Although it is quite possible to conditionalize code without the use of
30401 C-style preprocessing, as described earlier in this section, it is
30402 nevertheless convenient in some cases to use the C approach. Moreover,
30403 older Ada compilers have often provided some preprocessing capability,
30404 so legacy code may depend on this approach, even though it is not
30405 standard.
30406
30407 To accommodate such use, GNAT provides a preprocessor (modeled to a large
30408 extent on the various preprocessors that have been used
30409 with legacy code on other compilers, to enable easier transition).
30410
30411 The preprocessor may be used in two separate modes. It can be used quite
30412 separately from the compiler, to generate a separate output source file
30413 that is then fed to the compiler as a separate step. This is the
30414 @code{gnatprep} utility, whose use is fully described in
30415 @ref{Preprocessing Using gnatprep}.
30416 @cindex @code{gnatprep}
30417
30418 The preprocessing language allows such constructs as
30419
30420 @smallexample
30421 @group
30422 #if DEBUG or PRIORITY > 4 then
30423 bunch of declarations
30424 #else
30425 completely different bunch of declarations
30426 #end if;
30427 @end group
30428 @end smallexample
30429
30430 @noindent
30431 The values of the symbols @code{DEBUG} and @code{PRIORITY} can be
30432 defined either on the command line or in a separate file.
30433
30434 The other way of running the preprocessor is even closer to the C style and
30435 often more convenient. In this approach the preprocessing is integrated into
30436 the compilation process. The compiler is fed the preprocessor input which
30437 includes @code{#if} lines etc, and then the compiler carries out the
30438 preprocessing internally and processes the resulting output.
30439 For more details on this approach, see @ref{Integrated Preprocessing}.
30440
30441
30442 @c *******************************
30443 @node Inline Assembler
30444 @appendix Inline Assembler
30445 @c *******************************
30446
30447 @noindent
30448 If you need to write low-level software that interacts directly
30449 with the hardware, Ada provides two ways to incorporate assembly
30450 language code into your program. First, you can import and invoke
30451 external routines written in assembly language, an Ada feature fully
30452 supported by GNAT@. However, for small sections of code it may be simpler
30453 or more efficient to include assembly language statements directly
30454 in your Ada source program, using the facilities of the implementation-defined
30455 package @code{System.Machine_Code}, which incorporates the gcc
30456 Inline Assembler. The Inline Assembler approach offers a number of advantages,
30457 including the following:
30458
30459 @itemize @bullet
30460 @item No need to use non-Ada tools
30461 @item Consistent interface over different targets
30462 @item Automatic usage of the proper calling conventions
30463 @item Access to Ada constants and variables
30464 @item Definition of intrinsic routines
30465 @item Possibility of inlining a subprogram comprising assembler code
30466 @item Code optimizer can take Inline Assembler code into account
30467 @end itemize
30468
30469 This chapter presents a series of examples to show you how to use
30470 the Inline Assembler. Although it focuses on the Intel x86,
30471 the general approach applies also to other processors.
30472 It is assumed that you are familiar with Ada
30473 and with assembly language programming.
30474
30475 @menu
30476 * Basic Assembler Syntax::
30477 * A Simple Example of Inline Assembler::
30478 * Output Variables in Inline Assembler::
30479 * Input Variables in Inline Assembler::
30480 * Inlining Inline Assembler Code::
30481 * Other Asm Functionality::
30482 @end menu
30483
30484 @c ---------------------------------------------------------------------------
30485 @node Basic Assembler Syntax
30486 @section Basic Assembler Syntax
30487
30488 @noindent
30489 The assembler used by GNAT and gcc is based not on the Intel assembly
30490 language, but rather on a language that descends from the AT&T Unix
30491 assembler @emph{as} (and which is often referred to as ``AT&T syntax'').
30492 The following table summarizes the main features of @emph{as} syntax
30493 and points out the differences from the Intel conventions.
30494 See the gcc @emph{as} and @emph{gas} (an @emph{as} macro
30495 pre-processor) documentation for further information.
30496
30497 @table @asis
30498 @item Register names
30499 gcc / @emph{as}: Prefix with ``%''; for example @code{%eax}
30500 @*
30501 Intel: No extra punctuation; for example @code{eax}
30502
30503 @item Immediate operand
30504 gcc / @emph{as}: Prefix with ``$''; for example @code{$4}
30505 @*
30506 Intel: No extra punctuation; for example @code{4}
30507
30508 @item Address
30509 gcc / @emph{as}: Prefix with ``$''; for example @code{$loc}
30510 @*
30511 Intel: No extra punctuation; for example @code{loc}
30512
30513 @item Memory contents
30514 gcc / @emph{as}: No extra punctuation; for example @code{loc}
30515 @*
30516 Intel: Square brackets; for example @code{[loc]}
30517
30518 @item Register contents
30519 gcc / @emph{as}: Parentheses; for example @code{(%eax)}
30520 @*
30521 Intel: Square brackets; for example @code{[eax]}
30522
30523 @item Hexadecimal numbers
30524 gcc / @emph{as}: Leading ``0x'' (C language syntax); for example @code{0xA0}
30525 @*
30526 Intel: Trailing ``h''; for example @code{A0h}
30527
30528 @item Operand size
30529 gcc / @emph{as}: Explicit in op code; for example @code{movw} to move
30530 a 16-bit word
30531 @*
30532 Intel: Implicit, deduced by assembler; for example @code{mov}
30533
30534 @item Instruction repetition
30535 gcc / @emph{as}: Split into two lines; for example
30536 @*
30537 @code{rep}
30538 @*
30539 @code{stosl}
30540 @*
30541 Intel: Keep on one line; for example @code{rep stosl}
30542
30543 @item Order of operands
30544 gcc / @emph{as}: Source first; for example @code{movw $4, %eax}
30545 @*
30546 Intel: Destination first; for example @code{mov eax, 4}
30547 @end table
30548
30549 @c ---------------------------------------------------------------------------
30550 @node A Simple Example of Inline Assembler
30551 @section A Simple Example of Inline Assembler
30552
30553 @noindent
30554 The following example will generate a single assembly language statement,
30555 @code{nop}, which does nothing. Despite its lack of run-time effect,
30556 the example will be useful in illustrating the basics of
30557 the Inline Assembler facility.
30558
30559 @smallexample @c ada
30560 @group
30561 with System.Machine_Code; use System.Machine_Code;
30562 procedure Nothing is
30563 begin
30564 Asm ("nop");
30565 end Nothing;
30566 @end group
30567 @end smallexample
30568
30569 @code{Asm} is a procedure declared in package @code{System.Machine_Code};
30570 here it takes one parameter, a @emph{template string} that must be a static
30571 expression and that will form the generated instruction.
30572 @code{Asm} may be regarded as a compile-time procedure that parses
30573 the template string and additional parameters (none here),
30574 from which it generates a sequence of assembly language instructions.
30575
30576 The examples in this chapter will illustrate several of the forms
30577 for invoking @code{Asm}; a complete specification of the syntax
30578 is found in @ref{Machine Code Insertions,,, gnat_rm, GNAT Reference
30579 Manual}.
30580
30581 Under the standard GNAT conventions, the @code{Nothing} procedure
30582 should be in a file named @file{nothing.adb}.
30583 You can build the executable in the usual way:
30584 @smallexample
30585 gnatmake nothing
30586 @end smallexample
30587 However, the interesting aspect of this example is not its run-time behavior
30588 but rather the generated assembly code.
30589 To see this output, invoke the compiler as follows:
30590 @smallexample
30591 gcc -c -S -fomit-frame-pointer -gnatp @file{nothing.adb}
30592 @end smallexample
30593 where the options are:
30594
30595 @table @code
30596 @item -c
30597 compile only (no bind or link)
30598 @item -S
30599 generate assembler listing
30600 @item -fomit-frame-pointer
30601 do not set up separate stack frames
30602 @item -gnatp
30603 do not add runtime checks
30604 @end table
30605
30606 This gives a human-readable assembler version of the code. The resulting
30607 file will have the same name as the Ada source file, but with a @code{.s}
30608 extension. In our example, the file @file{nothing.s} has the following
30609 contents:
30610
30611 @smallexample
30612 @group
30613 .file "nothing.adb"
30614 gcc2_compiled.:
30615 ___gnu_compiled_ada:
30616 .text
30617 .align 4
30618 .globl __ada_nothing
30619 __ada_nothing:
30620 #APP
30621 nop
30622 #NO_APP
30623 jmp L1
30624 .align 2,0x90
30625 L1:
30626 ret
30627 @end group
30628 @end smallexample
30629
30630 The assembly code you included is clearly indicated by
30631 the compiler, between the @code{#APP} and @code{#NO_APP}
30632 delimiters. The character before the 'APP' and 'NOAPP'
30633 can differ on different targets. For example, GNU/Linux uses '#APP' while
30634 on NT you will see '/APP'.
30635
30636 If you make a mistake in your assembler code (such as using the
30637 wrong size modifier, or using a wrong operand for the instruction) GNAT
30638 will report this error in a temporary file, which will be deleted when
30639 the compilation is finished. Generating an assembler file will help
30640 in such cases, since you can assemble this file separately using the
30641 @emph{as} assembler that comes with gcc.
30642
30643 Assembling the file using the command
30644
30645 @smallexample
30646 as @file{nothing.s}
30647 @end smallexample
30648 @noindent
30649 will give you error messages whose lines correspond to the assembler
30650 input file, so you can easily find and correct any mistakes you made.
30651 If there are no errors, @emph{as} will generate an object file
30652 @file{nothing.out}.
30653
30654 @c ---------------------------------------------------------------------------
30655 @node Output Variables in Inline Assembler
30656 @section Output Variables in Inline Assembler
30657
30658 @noindent
30659 The examples in this section, showing how to access the processor flags,
30660 illustrate how to specify the destination operands for assembly language
30661 statements.
30662
30663 @smallexample @c ada
30664 @group
30665 with Interfaces; use Interfaces;
30666 with Ada.Text_IO; use Ada.Text_IO;
30667 with System.Machine_Code; use System.Machine_Code;
30668 procedure Get_Flags is
30669 Flags : Unsigned_32;
30670 use ASCII;
30671 begin
30672 Asm ("pushfl" & LF & HT & -- push flags on stack
30673 "popl %%eax" & LF & HT & -- load eax with flags
30674 "movl %%eax, %0", -- store flags in variable
30675 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
30676 Put_Line ("Flags register:" & Flags'Img);
30677 end Get_Flags;
30678 @end group
30679 @end smallexample
30680
30681 In order to have a nicely aligned assembly listing, we have separated
30682 multiple assembler statements in the Asm template string with linefeed
30683 (ASCII.LF) and horizontal tab (ASCII.HT) characters.
30684 The resulting section of the assembly output file is:
30685
30686 @smallexample
30687 @group
30688 #APP
30689 pushfl
30690 popl %eax
30691 movl %eax, -40(%ebp)
30692 #NO_APP
30693 @end group
30694 @end smallexample
30695
30696 It would have been legal to write the Asm invocation as:
30697
30698 @smallexample
30699 Asm ("pushfl popl %%eax movl %%eax, %0")
30700 @end smallexample
30701
30702 but in the generated assembler file, this would come out as:
30703
30704 @smallexample
30705 #APP
30706 pushfl popl %eax movl %eax, -40(%ebp)
30707 #NO_APP
30708 @end smallexample
30709
30710 which is not so convenient for the human reader.
30711
30712 We use Ada comments
30713 at the end of each line to explain what the assembler instructions
30714 actually do. This is a useful convention.
30715
30716 When writing Inline Assembler instructions, you need to precede each register
30717 and variable name with a percent sign. Since the assembler already requires
30718 a percent sign at the beginning of a register name, you need two consecutive
30719 percent signs for such names in the Asm template string, thus @code{%%eax}.
30720 In the generated assembly code, one of the percent signs will be stripped off.
30721
30722 Names such as @code{%0}, @code{%1}, @code{%2}, etc., denote input or output
30723 variables: operands you later define using @code{Input} or @code{Output}
30724 parameters to @code{Asm}.
30725 An output variable is illustrated in
30726 the third statement in the Asm template string:
30727 @smallexample
30728 movl %%eax, %0
30729 @end smallexample
30730 The intent is to store the contents of the eax register in a variable that can
30731 be accessed in Ada. Simply writing @code{movl %%eax, Flags} would not
30732 necessarily work, since the compiler might optimize by using a register
30733 to hold Flags, and the expansion of the @code{movl} instruction would not be
30734 aware of this optimization. The solution is not to store the result directly
30735 but rather to advise the compiler to choose the correct operand form;
30736 that is the purpose of the @code{%0} output variable.
30737
30738 Information about the output variable is supplied in the @code{Outputs}
30739 parameter to @code{Asm}:
30740 @smallexample
30741 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
30742 @end smallexample
30743
30744 The output is defined by the @code{Asm_Output} attribute of the target type;
30745 the general format is
30746 @smallexample
30747 Type'Asm_Output (constraint_string, variable_name)
30748 @end smallexample
30749
30750 The constraint string directs the compiler how
30751 to store/access the associated variable. In the example
30752 @smallexample
30753 Unsigned_32'Asm_Output ("=m", Flags);
30754 @end smallexample
30755 the @code{"m"} (memory) constraint tells the compiler that the variable
30756 @code{Flags} should be stored in a memory variable, thus preventing
30757 the optimizer from keeping it in a register. In contrast,
30758 @smallexample
30759 Unsigned_32'Asm_Output ("=r", Flags);
30760 @end smallexample
30761 uses the @code{"r"} (register) constraint, telling the compiler to
30762 store the variable in a register.
30763
30764 If the constraint is preceded by the equal character (@strong{=}), it tells
30765 the compiler that the variable will be used to store data into it.
30766
30767 In the @code{Get_Flags} example, we used the @code{"g"} (global) constraint,
30768 allowing the optimizer to choose whatever it deems best.
30769
30770 There are a fairly large number of constraints, but the ones that are
30771 most useful (for the Intel x86 processor) are the following:
30772
30773 @table @code
30774 @item =
30775 output constraint
30776 @item g
30777 global (i.e.@: can be stored anywhere)
30778 @item m
30779 in memory
30780 @item I
30781 a constant
30782 @item a
30783 use eax
30784 @item b
30785 use ebx
30786 @item c
30787 use ecx
30788 @item d
30789 use edx
30790 @item S
30791 use esi
30792 @item D
30793 use edi
30794 @item r
30795 use one of eax, ebx, ecx or edx
30796 @item q
30797 use one of eax, ebx, ecx, edx, esi or edi
30798 @end table
30799
30800 The full set of constraints is described in the gcc and @emph{as}
30801 documentation; note that it is possible to combine certain constraints
30802 in one constraint string.
30803
30804 You specify the association of an output variable with an assembler operand
30805 through the @code{%}@emph{n} notation, where @emph{n} is a non-negative
30806 integer. Thus in
30807 @smallexample @c ada
30808 @group
30809 Asm ("pushfl" & LF & HT & -- push flags on stack
30810 "popl %%eax" & LF & HT & -- load eax with flags
30811 "movl %%eax, %0", -- store flags in variable
30812 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
30813 @end group
30814 @end smallexample
30815 @noindent
30816 @code{%0} will be replaced in the expanded code by the appropriate operand,
30817 whatever
30818 the compiler decided for the @code{Flags} variable.
30819
30820 In general, you may have any number of output variables:
30821 @itemize @bullet
30822 @item
30823 Count the operands starting at 0; thus @code{%0}, @code{%1}, etc.
30824 @item
30825 Specify the @code{Outputs} parameter as a parenthesized comma-separated list
30826 of @code{Asm_Output} attributes
30827 @end itemize
30828
30829 For example:
30830 @smallexample @c ada
30831 @group
30832 Asm ("movl %%eax, %0" & LF & HT &
30833 "movl %%ebx, %1" & LF & HT &
30834 "movl %%ecx, %2",
30835 Outputs => (Unsigned_32'Asm_Output ("=g", Var_A), -- %0 = Var_A
30836 Unsigned_32'Asm_Output ("=g", Var_B), -- %1 = Var_B
30837 Unsigned_32'Asm_Output ("=g", Var_C))); -- %2 = Var_C
30838 @end group
30839 @end smallexample
30840 @noindent
30841 where @code{Var_A}, @code{Var_B}, and @code{Var_C} are variables
30842 in the Ada program.
30843
30844 As a variation on the @code{Get_Flags} example, we can use the constraints
30845 string to direct the compiler to store the eax register into the @code{Flags}
30846 variable, instead of including the store instruction explicitly in the
30847 @code{Asm} template string:
30848
30849 @smallexample @c ada
30850 @group
30851 with Interfaces; use Interfaces;
30852 with Ada.Text_IO; use Ada.Text_IO;
30853 with System.Machine_Code; use System.Machine_Code;
30854 procedure Get_Flags_2 is
30855 Flags : Unsigned_32;
30856 use ASCII;
30857 begin
30858 Asm ("pushfl" & LF & HT & -- push flags on stack
30859 "popl %%eax", -- save flags in eax
30860 Outputs => Unsigned_32'Asm_Output ("=a", Flags));
30861 Put_Line ("Flags register:" & Flags'Img);
30862 end Get_Flags_2;
30863 @end group
30864 @end smallexample
30865
30866 @noindent
30867 The @code{"a"} constraint tells the compiler that the @code{Flags}
30868 variable will come from the eax register. Here is the resulting code:
30869
30870 @smallexample
30871 @group
30872 #APP
30873 pushfl
30874 popl %eax
30875 #NO_APP
30876 movl %eax,-40(%ebp)
30877 @end group
30878 @end smallexample
30879
30880 @noindent
30881 The compiler generated the store of eax into Flags after
30882 expanding the assembler code.
30883
30884 Actually, there was no need to pop the flags into the eax register;
30885 more simply, we could just pop the flags directly into the program variable:
30886
30887 @smallexample @c ada
30888 @group
30889 with Interfaces; use Interfaces;
30890 with Ada.Text_IO; use Ada.Text_IO;
30891 with System.Machine_Code; use System.Machine_Code;
30892 procedure Get_Flags_3 is
30893 Flags : Unsigned_32;
30894 use ASCII;
30895 begin
30896 Asm ("pushfl" & LF & HT & -- push flags on stack
30897 "pop %0", -- save flags in Flags
30898 Outputs => Unsigned_32'Asm_Output ("=g", Flags));
30899 Put_Line ("Flags register:" & Flags'Img);
30900 end Get_Flags_3;
30901 @end group
30902 @end smallexample
30903
30904 @c ---------------------------------------------------------------------------
30905 @node Input Variables in Inline Assembler
30906 @section Input Variables in Inline Assembler
30907
30908 @noindent
30909 The example in this section illustrates how to specify the source operands
30910 for assembly language statements.
30911 The program simply increments its input value by 1:
30912
30913 @smallexample @c ada
30914 @group
30915 with Interfaces; use Interfaces;
30916 with Ada.Text_IO; use Ada.Text_IO;
30917 with System.Machine_Code; use System.Machine_Code;
30918 procedure Increment is
30919
30920 function Incr (Value : Unsigned_32) return Unsigned_32 is
30921 Result : Unsigned_32;
30922 begin
30923 Asm ("incl %0",
30924 Inputs => Unsigned_32'Asm_Input ("a", Value),
30925 Outputs => Unsigned_32'Asm_Output ("=a", Result));
30926 return Result;
30927 end Incr;
30928
30929 Value : Unsigned_32;
30930
30931 begin
30932 Value := 5;
30933 Put_Line ("Value before is" & Value'Img);
30934 Value := Incr (Value);
30935 Put_Line ("Value after is" & Value'Img);
30936 end Increment;
30937 @end group
30938 @end smallexample
30939
30940 The @code{Outputs} parameter to @code{Asm} specifies
30941 that the result will be in the eax register and that it is to be stored
30942 in the @code{Result} variable.
30943
30944 The @code{Inputs} parameter looks much like the @code{Outputs} parameter,
30945 but with an @code{Asm_Input} attribute.
30946 The @code{"="} constraint, indicating an output value, is not present.
30947
30948 You can have multiple input variables, in the same way that you can have more
30949 than one output variable.
30950
30951 The parameter count (%0, %1) etc, now starts at the first input
30952 statement, and continues with the output statements.
30953 When both parameters use the same variable, the
30954 compiler will treat them as the same %n operand, which is the case here.
30955
30956 Just as the @code{Outputs} parameter causes the register to be stored into the
30957 target variable after execution of the assembler statements, so does the
30958 @code{Inputs} parameter cause its variable to be loaded into the register
30959 before execution of the assembler statements.
30960
30961 Thus the effect of the @code{Asm} invocation is:
30962 @enumerate
30963 @item load the 32-bit value of @code{Value} into eax
30964 @item execute the @code{incl %eax} instruction
30965 @item store the contents of eax into the @code{Result} variable
30966 @end enumerate
30967
30968 The resulting assembler file (with @option{-O2} optimization) contains:
30969 @smallexample
30970 @group
30971 _increment__incr.1:
30972 subl $4,%esp
30973 movl 8(%esp),%eax
30974 #APP
30975 incl %eax
30976 #NO_APP
30977 movl %eax,%edx
30978 movl %ecx,(%esp)
30979 addl $4,%esp
30980 ret
30981 @end group
30982 @end smallexample
30983
30984 @c ---------------------------------------------------------------------------
30985 @node Inlining Inline Assembler Code
30986 @section Inlining Inline Assembler Code
30987
30988 @noindent
30989 For a short subprogram such as the @code{Incr} function in the previous
30990 section, the overhead of the call and return (creating / deleting the stack
30991 frame) can be significant, compared to the amount of code in the subprogram
30992 body. A solution is to apply Ada's @code{Inline} pragma to the subprogram,
30993 which directs the compiler to expand invocations of the subprogram at the
30994 point(s) of call, instead of setting up a stack frame for out-of-line calls.
30995 Here is the resulting program:
30996
30997 @smallexample @c ada
30998 @group
30999 with Interfaces; use Interfaces;
31000 with Ada.Text_IO; use Ada.Text_IO;
31001 with System.Machine_Code; use System.Machine_Code;
31002 procedure Increment_2 is
31003
31004 function Incr (Value : Unsigned_32) return Unsigned_32 is
31005 Result : Unsigned_32;
31006 begin
31007 Asm ("incl %0",
31008 Inputs => Unsigned_32'Asm_Input ("a", Value),
31009 Outputs => Unsigned_32'Asm_Output ("=a", Result));
31010 return Result;
31011 end Incr;
31012 pragma Inline (Increment);
31013
31014 Value : Unsigned_32;
31015
31016 begin
31017 Value := 5;
31018 Put_Line ("Value before is" & Value'Img);
31019 Value := Increment (Value);
31020 Put_Line ("Value after is" & Value'Img);
31021 end Increment_2;
31022 @end group
31023 @end smallexample
31024
31025 Compile the program with both optimization (@option{-O2}) and inlining
31026 (@option{-gnatn}) enabled.
31027
31028 The @code{Incr} function is still compiled as usual, but at the
31029 point in @code{Increment} where our function used to be called:
31030
31031 @smallexample
31032 @group
31033 pushl %edi
31034 call _increment__incr.1
31035 @end group
31036 @end smallexample
31037
31038 @noindent
31039 the code for the function body directly appears:
31040
31041 @smallexample
31042 @group
31043 movl %esi,%eax
31044 #APP
31045 incl %eax
31046 #NO_APP
31047 movl %eax,%edx
31048 @end group
31049 @end smallexample
31050
31051 @noindent
31052 thus saving the overhead of stack frame setup and an out-of-line call.
31053
31054 @c ---------------------------------------------------------------------------
31055 @node Other Asm Functionality
31056 @section Other @code{Asm} Functionality
31057
31058 @noindent
31059 This section describes two important parameters to the @code{Asm}
31060 procedure: @code{Clobber}, which identifies register usage;
31061 and @code{Volatile}, which inhibits unwanted optimizations.
31062
31063 @menu
31064 * The Clobber Parameter::
31065 * The Volatile Parameter::
31066 @end menu
31067
31068 @c ---------------------------------------------------------------------------
31069 @node The Clobber Parameter
31070 @subsection The @code{Clobber} Parameter
31071
31072 @noindent
31073 One of the dangers of intermixing assembly language and a compiled language
31074 such as Ada is that the compiler needs to be aware of which registers are
31075 being used by the assembly code. In some cases, such as the earlier examples,
31076 the constraint string is sufficient to indicate register usage (e.g.,
31077 @code{"a"} for
31078 the eax register). But more generally, the compiler needs an explicit
31079 identification of the registers that are used by the Inline Assembly
31080 statements.
31081
31082 Using a register that the compiler doesn't know about
31083 could be a side effect of an instruction (like @code{mull}
31084 storing its result in both eax and edx).
31085 It can also arise from explicit register usage in your
31086 assembly code; for example:
31087 @smallexample
31088 @group
31089 Asm ("movl %0, %%ebx" & LF & HT &
31090 "movl %%ebx, %1",
31091 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
31092 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out));
31093 @end group
31094 @end smallexample
31095 @noindent
31096 where the compiler (since it does not analyze the @code{Asm} template string)
31097 does not know you are using the ebx register.
31098
31099 In such cases you need to supply the @code{Clobber} parameter to @code{Asm},
31100 to identify the registers that will be used by your assembly code:
31101
31102 @smallexample
31103 @group
31104 Asm ("movl %0, %%ebx" & LF & HT &
31105 "movl %%ebx, %1",
31106 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
31107 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
31108 Clobber => "ebx");
31109 @end group
31110 @end smallexample
31111
31112 The Clobber parameter is a static string expression specifying the
31113 register(s) you are using. Note that register names are @emph{not} prefixed
31114 by a percent sign. Also, if more than one register is used then their names
31115 are separated by commas; e.g., @code{"eax, ebx"}
31116
31117 The @code{Clobber} parameter has several additional uses:
31118 @enumerate
31119 @item Use ``register'' name @code{cc} to indicate that flags might have changed
31120 @item Use ``register'' name @code{memory} if you changed a memory location
31121 @end enumerate
31122
31123 @c ---------------------------------------------------------------------------
31124 @node The Volatile Parameter
31125 @subsection The @code{Volatile} Parameter
31126 @cindex Volatile parameter
31127
31128 @noindent
31129 Compiler optimizations in the presence of Inline Assembler may sometimes have
31130 unwanted effects. For example, when an @code{Asm} invocation with an input
31131 variable is inside a loop, the compiler might move the loading of the input
31132 variable outside the loop, regarding it as a one-time initialization.
31133
31134 If this effect is not desired, you can disable such optimizations by setting
31135 the @code{Volatile} parameter to @code{True}; for example:
31136
31137 @smallexample @c ada
31138 @group
31139 Asm ("movl %0, %%ebx" & LF & HT &
31140 "movl %%ebx, %1",
31141 Inputs => Unsigned_32'Asm_Input ("g", Var_In),
31142 Outputs => Unsigned_32'Asm_Output ("=g", Var_Out),
31143 Clobber => "ebx",
31144 Volatile => True);
31145 @end group
31146 @end smallexample
31147
31148 By default, @code{Volatile} is set to @code{False} unless there is no
31149 @code{Outputs} parameter.
31150
31151 Although setting @code{Volatile} to @code{True} prevents unwanted
31152 optimizations, it will also disable other optimizations that might be
31153 important for efficiency. In general, you should set @code{Volatile}
31154 to @code{True} only if the compiler's optimizations have created
31155 problems.
31156 @c END OF INLINE ASSEMBLER CHAPTER
31157 @c ===============================
31158
31159 @c ***********************************
31160 @c * Compatibility and Porting Guide *
31161 @c ***********************************
31162 @node Compatibility and Porting Guide
31163 @appendix Compatibility and Porting Guide
31164
31165 @noindent
31166 This chapter describes the compatibility issues that may arise between
31167 GNAT and other Ada compilation systems (including those for Ada 83),
31168 and shows how GNAT can expedite porting
31169 applications developed in other Ada environments.
31170
31171 @menu
31172 * Compatibility with Ada 83::
31173 * Compatibility between Ada 95 and Ada 2005::
31174 * Implementation-dependent characteristics::
31175 * Compatibility with Other Ada Systems::
31176 * Representation Clauses::
31177 @ifclear vms
31178 @c Brief section is only in non-VMS version
31179 @c Full chapter is in VMS version
31180 * Compatibility with HP Ada 83::
31181 @end ifclear
31182 @ifset vms
31183 * Transitioning to 64-Bit GNAT for OpenVMS::
31184 @end ifset
31185 @end menu
31186
31187 @node Compatibility with Ada 83
31188 @section Compatibility with Ada 83
31189 @cindex Compatibility (between Ada 83 and Ada 95 / Ada 2005)
31190
31191 @noindent
31192 Ada 95 and Ada 2005 are highly upwards compatible with Ada 83. In
31193 particular, the design intention was that the difficulties associated
31194 with moving from Ada 83 to Ada 95 or Ada 2005 should be no greater than those
31195 that occur when moving from one Ada 83 system to another.
31196
31197 However, there are a number of points at which there are minor
31198 incompatibilities. The @cite{Ada 95 Annotated Reference Manual} contains
31199 full details of these issues,
31200 and should be consulted for a complete treatment.
31201 In practice the
31202 following subsections treat the most likely issues to be encountered.
31203
31204 @menu
31205 * Legal Ada 83 programs that are illegal in Ada 95::
31206 * More deterministic semantics::
31207 * Changed semantics::
31208 * Other language compatibility issues::
31209 @end menu
31210
31211 @node Legal Ada 83 programs that are illegal in Ada 95
31212 @subsection Legal Ada 83 programs that are illegal in Ada 95
31213
31214 Some legal Ada 83 programs are illegal (i.e., they will fail to compile) in
31215 Ada 95 and thus also in Ada 2005:
31216
31217 @table @emph
31218 @item Character literals
31219 Some uses of character literals are ambiguous. Since Ada 95 has introduced
31220 @code{Wide_Character} as a new predefined character type, some uses of
31221 character literals that were legal in Ada 83 are illegal in Ada 95.
31222 For example:
31223 @smallexample @c ada
31224 for Char in 'A' .. 'Z' loop @dots{} end loop;
31225 @end smallexample
31226
31227 @noindent
31228 The problem is that @code{'A'} and @code{'Z'} could be from either
31229 @code{Character} or @code{Wide_Character}. The simplest correction
31230 is to make the type explicit; e.g.:
31231 @smallexample @c ada
31232 for Char in Character range 'A' .. 'Z' loop @dots{} end loop;
31233 @end smallexample
31234
31235 @item New reserved words
31236 The identifiers @code{abstract}, @code{aliased}, @code{protected},
31237 @code{requeue}, @code{tagged}, and @code{until} are reserved in Ada 95.
31238 Existing Ada 83 code using any of these identifiers must be edited to
31239 use some alternative name.
31240
31241 @item Freezing rules
31242 The rules in Ada 95 are slightly different with regard to the point at
31243 which entities are frozen, and representation pragmas and clauses are
31244 not permitted past the freeze point. This shows up most typically in
31245 the form of an error message complaining that a representation item
31246 appears too late, and the appropriate corrective action is to move
31247 the item nearer to the declaration of the entity to which it refers.
31248
31249 A particular case is that representation pragmas
31250 @ifset vms
31251 (including the
31252 extended HP Ada 83 compatibility pragmas such as @code{Export_Procedure})
31253 @end ifset
31254 cannot be applied to a subprogram body. If necessary, a separate subprogram
31255 declaration must be introduced to which the pragma can be applied.
31256
31257 @item Optional bodies for library packages
31258 In Ada 83, a package that did not require a package body was nevertheless
31259 allowed to have one. This lead to certain surprises in compiling large
31260 systems (situations in which the body could be unexpectedly ignored by the
31261 binder). In Ada 95, if a package does not require a body then it is not
31262 permitted to have a body. To fix this problem, simply remove a redundant
31263 body if it is empty, or, if it is non-empty, introduce a dummy declaration
31264 into the spec that makes the body required. One approach is to add a private
31265 part to the package declaration (if necessary), and define a parameterless
31266 procedure called @code{Requires_Body}, which must then be given a dummy
31267 procedure body in the package body, which then becomes required.
31268 Another approach (assuming that this does not introduce elaboration
31269 circularities) is to add an @code{Elaborate_Body} pragma to the package spec,
31270 since one effect of this pragma is to require the presence of a package body.
31271
31272 @item @code{Numeric_Error} is now the same as @code{Constraint_Error}
31273 In Ada 95, the exception @code{Numeric_Error} is a renaming of
31274 @code{Constraint_Error}.
31275 This means that it is illegal to have separate exception handlers for
31276 the two exceptions. The fix is simply to remove the handler for the
31277 @code{Numeric_Error} case (since even in Ada 83, a compiler was free to raise
31278 @code{Constraint_Error} in place of @code{Numeric_Error} in all cases).
31279
31280 @item Indefinite subtypes in generics
31281 In Ada 83, it was permissible to pass an indefinite type (e.g.@: @code{String})
31282 as the actual for a generic formal private type, but then the instantiation
31283 would be illegal if there were any instances of declarations of variables
31284 of this type in the generic body. In Ada 95, to avoid this clear violation
31285 of the methodological principle known as the ``contract model'',
31286 the generic declaration explicitly indicates whether
31287 or not such instantiations are permitted. If a generic formal parameter
31288 has explicit unknown discriminants, indicated by using @code{(<>)} after the
31289 type name, then it can be instantiated with indefinite types, but no
31290 stand-alone variables can be declared of this type. Any attempt to declare
31291 such a variable will result in an illegality at the time the generic is
31292 declared. If the @code{(<>)} notation is not used, then it is illegal
31293 to instantiate the generic with an indefinite type.
31294 This is the potential incompatibility issue when porting Ada 83 code to Ada 95.
31295 It will show up as a compile time error, and
31296 the fix is usually simply to add the @code{(<>)} to the generic declaration.
31297 @end table
31298
31299 @node More deterministic semantics
31300 @subsection More deterministic semantics
31301
31302 @table @emph
31303 @item Conversions
31304 Conversions from real types to integer types round away from 0. In Ada 83
31305 the conversion Integer(2.5) could deliver either 2 or 3 as its value. This
31306 implementation freedom was intended to support unbiased rounding in
31307 statistical applications, but in practice it interfered with portability.
31308 In Ada 95 the conversion semantics are unambiguous, and rounding away from 0
31309 is required. Numeric code may be affected by this change in semantics.
31310 Note, though, that this issue is no worse than already existed in Ada 83
31311 when porting code from one vendor to another.
31312
31313 @item Tasking
31314 The Real-Time Annex introduces a set of policies that define the behavior of
31315 features that were implementation dependent in Ada 83, such as the order in
31316 which open select branches are executed.
31317 @end table
31318
31319 @node Changed semantics
31320 @subsection Changed semantics
31321
31322 @noindent
31323 The worst kind of incompatibility is one where a program that is legal in
31324 Ada 83 is also legal in Ada 95 but can have an effect in Ada 95 that was not
31325 possible in Ada 83. Fortunately this is extremely rare, but the one
31326 situation that you should be alert to is the change in the predefined type
31327 @code{Character} from 7-bit ASCII to 8-bit Latin-1.
31328
31329 @table @emph
31330 @item Range of type @code{Character}
31331 The range of @code{Standard.Character} is now the full 256 characters
31332 of Latin-1, whereas in most Ada 83 implementations it was restricted
31333 to 128 characters. Although some of the effects of
31334 this change will be manifest in compile-time rejection of legal
31335 Ada 83 programs it is possible for a working Ada 83 program to have
31336 a different effect in Ada 95, one that was not permitted in Ada 83.
31337 As an example, the expression
31338 @code{Character'Pos(Character'Last)} returned @code{127} in Ada 83 and now
31339 delivers @code{255} as its value.
31340 In general, you should look at the logic of any
31341 character-processing Ada 83 program and see whether it needs to be adapted
31342 to work correctly with Latin-1. Note that the predefined Ada 95 API has a
31343 character handling package that may be relevant if code needs to be adapted
31344 to account for the additional Latin-1 elements.
31345 The desirable fix is to
31346 modify the program to accommodate the full character set, but in some cases
31347 it may be convenient to define a subtype or derived type of Character that
31348 covers only the restricted range.
31349 @cindex Latin-1
31350 @end table
31351
31352 @node Other language compatibility issues
31353 @subsection Other language compatibility issues
31354
31355 @table @emph
31356 @item @option{-gnat83} switch
31357 All implementations of GNAT provide a switch that causes GNAT to operate
31358 in Ada 83 mode. In this mode, some but not all compatibility problems
31359 of the type described above are handled automatically. For example, the
31360 new reserved words introduced in Ada 95 and Ada 2005 are treated simply
31361 as identifiers as in Ada 83.
31362 However,
31363 in practice, it is usually advisable to make the necessary modifications
31364 to the program to remove the need for using this switch.
31365 See @ref{Compiling Different Versions of Ada}.
31366
31367 @item Support for removed Ada 83 pragmas and attributes
31368 A number of pragmas and attributes from Ada 83 were removed from Ada 95,
31369 generally because they were replaced by other mechanisms. Ada 95 and Ada 2005
31370 compilers are allowed, but not required, to implement these missing
31371 elements. In contrast with some other compilers, GNAT implements all
31372 such pragmas and attributes, eliminating this compatibility concern. These
31373 include @code{pragma Interface} and the floating point type attributes
31374 (@code{Emax}, @code{Mantissa}, etc.), among other items.
31375 @end table
31376
31377
31378 @node Compatibility between Ada 95 and Ada 2005
31379 @section Compatibility between Ada 95 and Ada 2005
31380 @cindex Compatibility between Ada 95 and Ada 2005
31381
31382 @noindent
31383 Although Ada 2005 was designed to be upwards compatible with Ada 95, there are
31384 a number of incompatibilities. Several are enumerated below;
31385 for a complete description please see the
31386 Annotated Ada 2005 Reference Manual, or section 9.1.1 in
31387 @cite{Rationale for Ada 2005}.
31388
31389 @table @emph
31390 @item New reserved words.
31391 The words @code{interface}, @code{overriding} and @code{synchronized} are
31392 reserved in Ada 2005.
31393 A pre-Ada 2005 program that uses any of these as an identifier will be
31394 illegal.
31395
31396 @item New declarations in predefined packages.
31397 A number of packages in the predefined environment contain new declarations:
31398 @code{Ada.Exceptions}, @code{Ada.Real_Time}, @code{Ada.Strings},
31399 @code{Ada.Strings.Fixed}, @code{Ada.Strings.Bounded},
31400 @code{Ada.Strings.Unbounded}, @code{Ada.Strings.Wide_Fixed},
31401 @code{Ada.Strings.Wide_Bounded}, @code{Ada.Strings.Wide_Unbounded},
31402 @code{Ada.Tags}, @code{Ada.Text_IO}, and @code{Interfaces.C}.
31403 If an Ada 95 program does a @code{with} and @code{use} of any of these
31404 packages, the new declarations may cause name clashes.
31405
31406 @item Access parameters.
31407 A nondispatching subprogram with an access parameter cannot be renamed
31408 as a dispatching operation. This was permitted in Ada 95.
31409
31410 @item Access types, discriminants, and constraints.
31411 Rule changes in this area have led to some incompatibilities; for example,
31412 constrained subtypes of some access types are not permitted in Ada 2005.
31413
31414 @item Aggregates for limited types.
31415 The allowance of aggregates for limited types in Ada 2005 raises the
31416 possibility of ambiguities in legal Ada 95 programs, since additional types
31417 now need to be considered in expression resolution.
31418
31419 @item Fixed-point multiplication and division.
31420 Certain expressions involving ``*'' or ``/'' for a fixed-point type, which
31421 were legal in Ada 95 and invoked the predefined versions of these operations,
31422 are now ambiguous.
31423 The ambiguity may be resolved either by applying a type conversion to the
31424 expression, or by explicitly invoking the operation from package
31425 @code{Standard}.
31426
31427 @item Return-by-reference types.
31428 The Ada 95 return-by-reference mechanism has been removed. Instead, the user
31429 can declare a function returning a value from an anonymous access type.
31430 @end table
31431
31432
31433 @node Implementation-dependent characteristics
31434 @section Implementation-dependent characteristics
31435 @noindent
31436 Although the Ada language defines the semantics of each construct as
31437 precisely as practical, in some situations (for example for reasons of
31438 efficiency, or where the effect is heavily dependent on the host or target
31439 platform) the implementation is allowed some freedom. In porting Ada 83
31440 code to GNAT, you need to be aware of whether / how the existing code
31441 exercised such implementation dependencies. Such characteristics fall into
31442 several categories, and GNAT offers specific support in assisting the
31443 transition from certain Ada 83 compilers.
31444
31445 @menu
31446 * Implementation-defined pragmas::
31447 * Implementation-defined attributes::
31448 * Libraries::
31449 * Elaboration order::
31450 * Target-specific aspects::
31451 @end menu
31452
31453 @node Implementation-defined pragmas
31454 @subsection Implementation-defined pragmas
31455
31456 @noindent
31457 Ada compilers are allowed to supplement the language-defined pragmas, and
31458 these are a potential source of non-portability. All GNAT-defined pragmas
31459 are described in @ref{Implementation Defined Pragmas,,, gnat_rm, GNAT
31460 Reference Manual}, and these include several that are specifically
31461 intended to correspond to other vendors' Ada 83 pragmas.
31462 For migrating from VADS, the pragma @code{Use_VADS_Size} may be useful.
31463 For compatibility with HP Ada 83, GNAT supplies the pragmas
31464 @code{Extend_System}, @code{Ident}, @code{Inline_Generic},
31465 @code{Interface_Name}, @code{Passive}, @code{Suppress_All},
31466 and @code{Volatile}.
31467 Other relevant pragmas include @code{External} and @code{Link_With}.
31468 Some vendor-specific
31469 Ada 83 pragmas (@code{Share_Generic}, @code{Subtitle}, and @code{Title}) are
31470 recognized, thus
31471 avoiding compiler rejection of units that contain such pragmas; they are not
31472 relevant in a GNAT context and hence are not otherwise implemented.
31473
31474 @node Implementation-defined attributes
31475 @subsection Implementation-defined attributes
31476
31477 Analogous to pragmas, the set of attributes may be extended by an
31478 implementation. All GNAT-defined attributes are described in
31479 @ref{Implementation Defined Attributes,,, gnat_rm, GNAT Reference
31480 Manual}, and these include several that are specifically intended
31481 to correspond to other vendors' Ada 83 attributes. For migrating from VADS,
31482 the attribute @code{VADS_Size} may be useful. For compatibility with HP
31483 Ada 83, GNAT supplies the attributes @code{Bit}, @code{Machine_Size} and
31484 @code{Type_Class}.
31485
31486 @node Libraries
31487 @subsection Libraries
31488 @noindent
31489 Vendors may supply libraries to supplement the standard Ada API. If Ada 83
31490 code uses vendor-specific libraries then there are several ways to manage
31491 this in Ada 95 or Ada 2005:
31492 @enumerate
31493 @item
31494 If the source code for the libraries (specs and bodies) are
31495 available, then the libraries can be migrated in the same way as the
31496 application.
31497 @item
31498 If the source code for the specs but not the bodies are
31499 available, then you can reimplement the bodies.
31500 @item
31501 Some features introduced by Ada 95 obviate the need for library support. For
31502 example most Ada 83 vendors supplied a package for unsigned integers. The
31503 Ada 95 modular type feature is the preferred way to handle this need, so
31504 instead of migrating or reimplementing the unsigned integer package it may
31505 be preferable to retrofit the application using modular types.
31506 @end enumerate
31507
31508 @node Elaboration order
31509 @subsection Elaboration order
31510 @noindent
31511 The implementation can choose any elaboration order consistent with the unit
31512 dependency relationship. This freedom means that some orders can result in
31513 Program_Error being raised due to an ``Access Before Elaboration'': an attempt
31514 to invoke a subprogram its body has been elaborated, or to instantiate a
31515 generic before the generic body has been elaborated. By default GNAT
31516 attempts to choose a safe order (one that will not encounter access before
31517 elaboration problems) by implicitly inserting @code{Elaborate} or
31518 @code{Elaborate_All} pragmas where
31519 needed. However, this can lead to the creation of elaboration circularities
31520 and a resulting rejection of the program by gnatbind. This issue is
31521 thoroughly described in @ref{Elaboration Order Handling in GNAT}.
31522 In brief, there are several
31523 ways to deal with this situation:
31524
31525 @itemize @bullet
31526 @item
31527 Modify the program to eliminate the circularities, e.g.@: by moving
31528 elaboration-time code into explicitly-invoked procedures
31529 @item
31530 Constrain the elaboration order by including explicit @code{Elaborate_Body} or
31531 @code{Elaborate} pragmas, and then inhibit the generation of implicit
31532 @code{Elaborate_All}
31533 pragmas either globally (as an effect of the @option{-gnatE} switch) or locally
31534 (by selectively suppressing elaboration checks via pragma
31535 @code{Suppress(Elaboration_Check)} when it is safe to do so).
31536 @end itemize
31537
31538 @node Target-specific aspects
31539 @subsection Target-specific aspects
31540 @noindent
31541 Low-level applications need to deal with machine addresses, data
31542 representations, interfacing with assembler code, and similar issues. If
31543 such an Ada 83 application is being ported to different target hardware (for
31544 example where the byte endianness has changed) then you will need to
31545 carefully examine the program logic; the porting effort will heavily depend
31546 on the robustness of the original design. Moreover, Ada 95 (and thus
31547 Ada 2005) are sometimes
31548 incompatible with typical Ada 83 compiler practices regarding implicit
31549 packing, the meaning of the Size attribute, and the size of access values.
31550 GNAT's approach to these issues is described in @ref{Representation Clauses}.
31551
31552 @node Compatibility with Other Ada Systems
31553 @section Compatibility with Other Ada Systems
31554
31555 @noindent
31556 If programs avoid the use of implementation dependent and
31557 implementation defined features, as documented in the @cite{Ada
31558 Reference Manual}, there should be a high degree of portability between
31559 GNAT and other Ada systems. The following are specific items which
31560 have proved troublesome in moving Ada 95 programs from GNAT to other Ada 95
31561 compilers, but do not affect porting code to GNAT@.
31562 (As of @value{NOW}, GNAT is the only compiler available for Ada 2005;
31563 the following issues may or may not arise for Ada 2005 programs
31564 when other compilers appear.)
31565
31566 @table @emph
31567 @item Ada 83 Pragmas and Attributes
31568 Ada 95 compilers are allowed, but not required, to implement the missing
31569 Ada 83 pragmas and attributes that are no longer defined in Ada 95.
31570 GNAT implements all such pragmas and attributes, eliminating this as
31571 a compatibility concern, but some other Ada 95 compilers reject these
31572 pragmas and attributes.
31573
31574 @item Specialized Needs Annexes
31575 GNAT implements the full set of special needs annexes. At the
31576 current time, it is the only Ada 95 compiler to do so. This means that
31577 programs making use of these features may not be portable to other Ada
31578 95 compilation systems.
31579
31580 @item Representation Clauses
31581 Some other Ada 95 compilers implement only the minimal set of
31582 representation clauses required by the Ada 95 reference manual. GNAT goes
31583 far beyond this minimal set, as described in the next section.
31584 @end table
31585
31586 @node Representation Clauses
31587 @section Representation Clauses
31588
31589 @noindent
31590 The Ada 83 reference manual was quite vague in describing both the minimal
31591 required implementation of representation clauses, and also their precise
31592 effects. Ada 95 (and thus also Ada 2005) are much more explicit, but the
31593 minimal set of capabilities required is still quite limited.
31594
31595 GNAT implements the full required set of capabilities in
31596 Ada 95 and Ada 2005, but also goes much further, and in particular
31597 an effort has been made to be compatible with existing Ada 83 usage to the
31598 greatest extent possible.
31599
31600 A few cases exist in which Ada 83 compiler behavior is incompatible with
31601 the requirements in Ada 95 (and thus also Ada 2005). These are instances of
31602 intentional or accidental dependence on specific implementation dependent
31603 characteristics of these Ada 83 compilers. The following is a list of
31604 the cases most likely to arise in existing Ada 83 code.
31605
31606 @table @emph
31607 @item Implicit Packing
31608 Some Ada 83 compilers allowed a Size specification to cause implicit
31609 packing of an array or record. This could cause expensive implicit
31610 conversions for change of representation in the presence of derived
31611 types, and the Ada design intends to avoid this possibility.
31612 Subsequent AI's were issued to make it clear that such implicit
31613 change of representation in response to a Size clause is inadvisable,
31614 and this recommendation is represented explicitly in the Ada 95 (and Ada 2005)
31615 Reference Manuals as implementation advice that is followed by GNAT@.
31616 The problem will show up as an error
31617 message rejecting the size clause. The fix is simply to provide
31618 the explicit pragma @code{Pack}, or for more fine tuned control, provide
31619 a Component_Size clause.
31620
31621 @item Meaning of Size Attribute
31622 The Size attribute in Ada 95 (and Ada 2005) for discrete types is defined as
31623 the minimal number of bits required to hold values of the type. For example,
31624 on a 32-bit machine, the size of @code{Natural} will typically be 31 and not
31625 32 (since no sign bit is required). Some Ada 83 compilers gave 31, and
31626 some 32 in this situation. This problem will usually show up as a compile
31627 time error, but not always. It is a good idea to check all uses of the
31628 'Size attribute when porting Ada 83 code. The GNAT specific attribute
31629 Object_Size can provide a useful way of duplicating the behavior of
31630 some Ada 83 compiler systems.
31631
31632 @item Size of Access Types
31633 A common assumption in Ada 83 code is that an access type is in fact a pointer,
31634 and that therefore it will be the same size as a System.Address value. This
31635 assumption is true for GNAT in most cases with one exception. For the case of
31636 a pointer to an unconstrained array type (where the bounds may vary from one
31637 value of the access type to another), the default is to use a ``fat pointer'',
31638 which is represented as two separate pointers, one to the bounds, and one to
31639 the array. This representation has a number of advantages, including improved
31640 efficiency. However, it may cause some difficulties in porting existing Ada 83
31641 code which makes the assumption that, for example, pointers fit in 32 bits on
31642 a machine with 32-bit addressing.
31643
31644 To get around this problem, GNAT also permits the use of ``thin pointers'' for
31645 access types in this case (where the designated type is an unconstrained array
31646 type). These thin pointers are indeed the same size as a System.Address value.
31647 To specify a thin pointer, use a size clause for the type, for example:
31648
31649 @smallexample @c ada
31650 type X is access all String;
31651 for X'Size use Standard'Address_Size;
31652 @end smallexample
31653
31654 @noindent
31655 which will cause the type X to be represented using a single pointer.
31656 When using this representation, the bounds are right behind the array.
31657 This representation is slightly less efficient, and does not allow quite
31658 such flexibility in the use of foreign pointers or in using the
31659 Unrestricted_Access attribute to create pointers to non-aliased objects.
31660 But for any standard portable use of the access type it will work in
31661 a functionally correct manner and allow porting of existing code.
31662 Note that another way of forcing a thin pointer representation
31663 is to use a component size clause for the element size in an array,
31664 or a record representation clause for an access field in a record.
31665 @end table
31666
31667 @ifclear vms
31668 @c This brief section is only in the non-VMS version
31669 @c The complete chapter on HP Ada is in the VMS version
31670 @node Compatibility with HP Ada 83
31671 @section Compatibility with HP Ada 83
31672
31673 @noindent
31674 The VMS version of GNAT fully implements all the pragmas and attributes
31675 provided by HP Ada 83, as well as providing the standard HP Ada 83
31676 libraries, including Starlet. In addition, data layouts and parameter
31677 passing conventions are highly compatible. This means that porting
31678 existing HP Ada 83 code to GNAT in VMS systems should be easier than
31679 most other porting efforts. The following are some of the most
31680 significant differences between GNAT and HP Ada 83.
31681
31682 @table @emph
31683 @item Default floating-point representation
31684 In GNAT, the default floating-point format is IEEE, whereas in HP Ada 83,
31685 it is VMS format. GNAT does implement the necessary pragmas
31686 (Long_Float, Float_Representation) for changing this default.
31687
31688 @item System
31689 The package System in GNAT exactly corresponds to the definition in the
31690 Ada 95 reference manual, which means that it excludes many of the
31691 HP Ada 83 extensions. However, a separate package Aux_DEC is provided
31692 that contains the additional definitions, and a special pragma,
31693 Extend_System allows this package to be treated transparently as an
31694 extension of package System.
31695
31696 @item To_Address
31697 The definitions provided by Aux_DEC are exactly compatible with those
31698 in the HP Ada 83 version of System, with one exception.
31699 HP Ada provides the following declarations:
31700
31701 @smallexample @c ada
31702 TO_ADDRESS (INTEGER)
31703 TO_ADDRESS (UNSIGNED_LONGWORD)
31704 TO_ADDRESS (@i{universal_integer})
31705 @end smallexample
31706
31707 @noindent
31708 The version of TO_ADDRESS taking a @i{universal integer} argument is in fact
31709 an extension to Ada 83 not strictly compatible with the reference manual.
31710 In GNAT, we are constrained to be exactly compatible with the standard,
31711 and this means we cannot provide this capability. In HP Ada 83, the
31712 point of this definition is to deal with a call like:
31713
31714 @smallexample @c ada
31715 TO_ADDRESS (16#12777#);
31716 @end smallexample
31717
31718 @noindent
31719 Normally, according to the Ada 83 standard, one would expect this to be
31720 ambiguous, since it matches both the INTEGER and UNSIGNED_LONGWORD forms
31721 of TO_ADDRESS@. However, in HP Ada 83, there is no ambiguity, since the
31722 definition using @i{universal_integer} takes precedence.
31723
31724 In GNAT, since the version with @i{universal_integer} cannot be supplied, it
31725 is not possible to be 100% compatible. Since there are many programs using
31726 numeric constants for the argument to TO_ADDRESS, the decision in GNAT was
31727 to change the name of the function in the UNSIGNED_LONGWORD case, so the
31728 declarations provided in the GNAT version of AUX_Dec are:
31729
31730 @smallexample @c ada
31731 function To_Address (X : Integer) return Address;
31732 pragma Pure_Function (To_Address);
31733
31734 function To_Address_Long (X : Unsigned_Longword)
31735 return Address;
31736 pragma Pure_Function (To_Address_Long);
31737 @end smallexample
31738
31739 @noindent
31740 This means that programs using TO_ADDRESS for UNSIGNED_LONGWORD must
31741 change the name to TO_ADDRESS_LONG@.
31742
31743 @item Task_Id values
31744 The Task_Id values assigned will be different in the two systems, and GNAT
31745 does not provide a specified value for the Task_Id of the environment task,
31746 which in GNAT is treated like any other declared task.
31747 @end table
31748
31749 @noindent
31750 For full details on these and other less significant compatibility issues,
31751 see appendix E of the HP publication entitled @cite{HP Ada, Technical
31752 Overview and Comparison on HP Platforms}.
31753
31754 For GNAT running on other than VMS systems, all the HP Ada 83 pragmas and
31755 attributes are recognized, although only a subset of them can sensibly
31756 be implemented. The description of pragmas in @ref{Implementation
31757 Defined Pragmas,,, gnat_rm, GNAT Reference Manual}
31758 indicates whether or not they are applicable to non-VMS systems.
31759 @end ifclear
31760
31761 @ifset vms
31762 @node Transitioning to 64-Bit GNAT for OpenVMS
31763 @section Transitioning to 64-Bit @value{EDITION} for OpenVMS
31764
31765 @noindent
31766 This section is meant to assist users of pre-2006 @value{EDITION}
31767 for Alpha OpenVMS who are transitioning to 64-bit @value{EDITION},
31768 the version of the GNAT technology supplied in 2006 and later for
31769 OpenVMS on both Alpha and I64.
31770
31771 @menu
31772 * Introduction to transitioning::
31773 * Migration of 32 bit code::
31774 * Taking advantage of 64 bit addressing::
31775 * Technical details::
31776 @end menu
31777
31778 @node Introduction to transitioning
31779 @subsection Introduction
31780
31781 @noindent
31782 64-bit @value{EDITION} for Open VMS has been designed to meet
31783 three main goals:
31784
31785 @enumerate
31786 @item
31787 Providing a full conforming implementation of Ada 95 and Ada 2005
31788
31789 @item
31790 Allowing maximum backward compatibility, thus easing migration of existing
31791 Ada source code
31792
31793 @item
31794 Supplying a path for exploiting the full 64-bit address range
31795 @end enumerate
31796
31797 @noindent
31798 Ada's strong typing semantics has made it
31799 impractical to have different 32-bit and 64-bit modes. As soon as
31800 one object could possibly be outside the 32-bit address space, this
31801 would make it necessary for the @code{System.Address} type to be 64 bits.
31802 In particular, this would cause inconsistencies if 32-bit code is
31803 called from 64-bit code that raises an exception.
31804
31805 This issue has been resolved by always using 64-bit addressing
31806 at the system level, but allowing for automatic conversions between
31807 32-bit and 64-bit addresses where required. Thus users who
31808 do not currently require 64-bit addressing capabilities, can
31809 recompile their code with only minimal changes (and indeed
31810 if the code is written in portable Ada, with no assumptions about
31811 the size of the @code{Address} type, then no changes at all are necessary).
31812 At the same time,
31813 this approach provides a simple, gradual upgrade path to future
31814 use of larger memories than available for 32-bit systems.
31815 Also, newly written applications or libraries will by default
31816 be fully compatible with future systems exploiting 64-bit
31817 addressing capabilities.
31818
31819 @ref{Migration of 32 bit code}, will focus on porting applications
31820 that do not require more than 2 GB of
31821 addressable memory. This code will be referred to as
31822 @emph{32-bit code}.
31823 For applications intending to exploit the full 64-bit address space,
31824 @ref{Taking advantage of 64 bit addressing},
31825 will consider further changes that may be required.
31826 Such code will be referred to below as @emph{64-bit code}.
31827
31828 @node Migration of 32 bit code
31829 @subsection Migration of 32-bit code
31830
31831 @menu
31832 * Address types::
31833 * Access types::
31834 * Unchecked conversions::
31835 * Predefined constants::
31836 * Interfacing with C::
31837 * Experience with source compatibility::
31838 @end menu
31839
31840 @node Address types
31841 @subsubsection Address types
31842
31843 @noindent
31844 To solve the problem of mixing 64-bit and 32-bit addressing,
31845 while maintaining maximum backward compatibility, the following
31846 approach has been taken:
31847
31848 @itemize @bullet
31849 @item
31850 @code{System.Address} always has a size of 64 bits
31851
31852 @item
31853 @code{System.Short_Address} is a 32-bit subtype of @code{System.Address}
31854 @end itemize
31855
31856 @noindent
31857 Since @code{System.Short_Address} is a subtype of @code{System.Address},
31858 a @code{Short_Address}
31859 may be used where an @code{Address} is required, and vice versa, without
31860 needing explicit type conversions.
31861 By virtue of the Open VMS parameter passing conventions,
31862 even imported
31863 and exported subprograms that have 32-bit address parameters are
31864 compatible with those that have 64-bit address parameters.
31865 (See @ref{Making code 64 bit clean} for details.)
31866
31867 The areas that may need attention are those where record types have
31868 been defined that contain components of the type @code{System.Address}, and
31869 where objects of this type are passed to code expecting a record layout with
31870 32-bit addresses.
31871
31872 Different compilers on different platforms cannot be
31873 expected to represent the same type in the same way,
31874 since alignment constraints
31875 and other system-dependent properties affect the compiler's decision.
31876 For that reason, Ada code
31877 generally uses representation clauses to specify the expected
31878 layout where required.
31879
31880 If such a representation clause uses 32 bits for a component having
31881 the type @code{System.Address}, 64-bit @value{EDITION} for OpenVMS
31882 will detect that error and produce a specific diagnostic message.
31883 The developer should then determine whether the representation
31884 should be 64 bits or not and make either of two changes:
31885 change the size to 64 bits and leave the type as @code{System.Address}, or
31886 leave the size as 32 bits and change the type to @code{System.Short_Address}.
31887 Since @code{Short_Address} is a subtype of @code{Address}, no changes are
31888 required in any code setting or accessing the field; the compiler will
31889 automatically perform any needed conversions between address
31890 formats.
31891
31892 @node Access types
31893 @subsubsection Access types
31894
31895 @noindent
31896 By default, objects designated by access values are always
31897 allocated in the 32-bit
31898 address space. Thus legacy code will never contain
31899 any objects that are not addressable with 32-bit addresses, and
31900 the compiler will never raise exceptions as result of mixing
31901 32-bit and 64-bit addresses.
31902
31903 However, the access values themselves are represented in 64 bits, for optimum
31904 performance and future compatibility with 64-bit code. As was
31905 the case with @code{System.Address}, the compiler will give an error message
31906 if an object or record component has a representation clause that
31907 requires the access value to fit in 32 bits. In such a situation,
31908 an explicit size clause for the access type, specifying 32 bits,
31909 will have the desired effect.
31910
31911 General access types (declared with @code{access all}) can never be
31912 32 bits, as values of such types must be able to refer to any object
31913 of the designated type,
31914 including objects residing outside the 32-bit address range.
31915 Existing Ada 83 code will not contain such type definitions,
31916 however, since general access types were introduced in Ada 95.
31917
31918 @node Unchecked conversions
31919 @subsubsection Unchecked conversions
31920
31921 @noindent
31922 In the case of an @code{Unchecked_Conversion} where the source type is a
31923 64-bit access type or the type @code{System.Address}, and the target
31924 type is a 32-bit type, the compiler will generate a warning.
31925 Even though the generated code will still perform the required
31926 conversions, it is highly recommended in these cases to use
31927 respectively a 32-bit access type or @code{System.Short_Address}
31928 as the source type.
31929
31930 @node Predefined constants
31931 @subsubsection Predefined constants
31932
31933 @noindent
31934 The following table shows the correspondence between pre-2006 versions of
31935 @value{EDITION} on Alpha OpenVMS (``Old'') and 64-bit @value{EDITION}
31936 (``New''):
31937
31938 @multitable {@code{System.Short_Memory_Size}} {2**32} {2**64}
31939 @item @b{Constant} @tab @b{Old} @tab @b{New}
31940 @item @code{System.Word_Size} @tab 32 @tab 64
31941 @item @code{System.Memory_Size} @tab 2**32 @tab 2**64
31942 @item @code{System.Short_Memory_Size} @tab 2**32 @tab 2**32
31943 @item @code{System.Address_Size} @tab 32 @tab 64
31944 @end multitable
31945
31946 @noindent
31947 If you need to refer to the specific
31948 memory size of a 32-bit implementation, instead of the
31949 actual memory size, use @code{System.Short_Memory_Size}
31950 rather than @code{System.Memory_Size}.
31951 Similarly, references to @code{System.Address_Size} may need
31952 to be replaced by @code{System.Short_Address'Size}.
31953 The program @command{gnatfind} may be useful for locating
31954 references to the above constants, so that you can verify that they
31955 are still correct.
31956
31957 @node Interfacing with C
31958 @subsubsection Interfacing with C
31959
31960 @noindent
31961 In order to minimize the impact of the transition to 64-bit addresses on
31962 legacy programs, some fundamental types in the @code{Interfaces.C}
31963 package hierarchy continue to be represented in 32 bits.
31964 These types are: @code{ptrdiff_t}, @code{size_t}, and @code{chars_ptr}.
31965 This eases integration with the default HP C layout choices, for example
31966 as found in the system routines in @code{DECC$SHR.EXE}.
31967 Because of this implementation choice, the type fully compatible with
31968 @code{chars_ptr} is now @code{Short_Address} and not @code{Address}.
31969 Depending on the context the compiler will issue a
31970 warning or an error when type @code{Address} is used, alerting the user to a
31971 potential problem. Otherwise 32-bit programs that use
31972 @code{Interfaces.C} should normally not require code modifications
31973
31974 The other issue arising with C interfacing concerns pragma @code{Convention}.
31975 For VMS 64-bit systems, there is an issue of the appropriate default size
31976 of C convention pointers in the absence of an explicit size clause. The HP
31977 C compiler can choose either 32 or 64 bits depending on compiler options.
31978 GNAT chooses 32-bits rather than 64-bits in the default case where no size
31979 clause is given. This proves a better choice for porting 32-bit legacy
31980 applications. In order to have a 64-bit representation, it is necessary to
31981 specify a size representation clause. For example:
31982
31983 @smallexample @c ada
31984 type int_star is access Interfaces.C.int;
31985 pragma Convention(C, int_star);
31986 for int_star'Size use 64; -- Necessary to get 64 and not 32 bits
31987 @end smallexample
31988
31989 @node Experience with source compatibility
31990 @subsubsection Experience with source compatibility
31991
31992 @noindent
31993 The Security Server and STARLET on I64 provide an interesting ``test case''
31994 for source compatibility issues, since it is in such system code
31995 where assumptions about @code{Address} size might be expected to occur.
31996 Indeed, there were a small number of occasions in the Security Server
31997 file @file{jibdef.ads}
31998 where a representation clause for a record type specified
31999 32 bits for a component of type @code{Address}.
32000 All of these errors were detected by the compiler.
32001 The repair was obvious and immediate; to simply replace @code{Address} by
32002 @code{Short_Address}.
32003
32004 In the case of STARLET, there were several record types that should
32005 have had representation clauses but did not. In these record types
32006 there was an implicit assumption that an @code{Address} value occupied
32007 32 bits.
32008 These compiled without error, but their usage resulted in run-time error
32009 returns from STARLET system calls.
32010 Future GNAT technology enhancements may include a tool that detects and flags
32011 these sorts of potential source code porting problems.
32012
32013 @c ****************************************
32014 @node Taking advantage of 64 bit addressing
32015 @subsection Taking advantage of 64-bit addressing
32016
32017 @menu
32018 * Making code 64 bit clean::
32019 * Allocating memory from the 64 bit storage pool::
32020 * Restrictions on use of 64 bit objects::
32021 * Using 64 bit storage pools by default::
32022 * General access types::
32023 * STARLET and other predefined libraries::
32024 @end menu
32025
32026 @node Making code 64 bit clean
32027 @subsubsection Making code 64-bit clean
32028
32029 @noindent
32030 In order to prevent problems that may occur when (parts of) a
32031 system start using memory outside the 32-bit address range,
32032 we recommend some additional guidelines:
32033
32034 @itemize @bullet
32035 @item
32036 For imported subprograms that take parameters of the
32037 type @code{System.Address}, ensure that these subprograms can
32038 indeed handle 64-bit addresses. If not, or when in doubt,
32039 change the subprogram declaration to specify
32040 @code{System.Short_Address} instead.
32041
32042 @item
32043 Resolve all warnings related to size mismatches in
32044 unchecked conversions. Failing to do so causes
32045 erroneous execution if the source object is outside
32046 the 32-bit address space.
32047
32048 @item
32049 (optional) Explicitly use the 32-bit storage pool
32050 for access types used in a 32-bit context, or use
32051 generic access types where possible
32052 (@pxref{Restrictions on use of 64 bit objects}).
32053 @end itemize
32054
32055 @noindent
32056 If these rules are followed, the compiler will automatically insert
32057 any necessary checks to ensure that no addresses or access values
32058 passed to 32-bit code ever refer to objects outside the 32-bit
32059 address range.
32060 Any attempt to do this will raise @code{Constraint_Error}.
32061
32062 @node Allocating memory from the 64 bit storage pool
32063 @subsubsection Allocating memory from the 64-bit storage pool
32064
32065 @noindent
32066 For any access type @code{T} that potentially requires memory allocations
32067 beyond the 32-bit address space,
32068 use the following representation clause:
32069
32070 @smallexample @c ada
32071 for T'Storage_Pool use System.Pool_64;
32072 @end smallexample
32073
32074 @node Restrictions on use of 64 bit objects
32075 @subsubsection Restrictions on use of 64-bit objects
32076
32077 @noindent
32078 Taking the address of an object allocated from a 64-bit storage pool,
32079 and then passing this address to a subprogram expecting
32080 @code{System.Short_Address},
32081 or assigning it to a variable of type @code{Short_Address}, will cause
32082 @code{Constraint_Error} to be raised. In case the code is not 64-bit clean
32083 (@pxref{Making code 64 bit clean}), or checks are suppressed,
32084 no exception is raised and execution
32085 will become erroneous.
32086
32087 @node Using 64 bit storage pools by default
32088 @subsubsection Using 64-bit storage pools by default
32089
32090 @noindent
32091 In some cases it may be desirable to have the compiler allocate
32092 from 64-bit storage pools by default. This may be the case for
32093 libraries that are 64-bit clean, but may be used in both 32-bit
32094 and 64-bit contexts. For these cases the following configuration
32095 pragma may be specified:
32096
32097 @smallexample @c ada
32098 pragma Pool_64_Default;
32099 @end smallexample
32100
32101 @noindent
32102 Any code compiled in the context of this pragma will by default
32103 use the @code{System.Pool_64} storage pool. This default may be overridden
32104 for a specific access type @code{T} by the representation clause:
32105
32106 @smallexample @c ada
32107 for T'Storage_Pool use System.Pool_32;
32108 @end smallexample
32109
32110 @noindent
32111 Any object whose address may be passed to a subprogram with a
32112 @code{Short_Address} argument, or assigned to a variable of type
32113 @code{Short_Address}, needs to be allocated from this pool.
32114
32115 @node General access types
32116 @subsubsection General access types
32117
32118 @noindent
32119 Objects designated by access values from a
32120 general access type (declared with @code{access all}) are never allocated
32121 from a 64-bit storage pool. Code that uses general access types will
32122 accept objects allocated in either 32-bit or 64-bit address spaces,
32123 but never allocate objects outside the 32-bit address space.
32124 Using general access types ensures maximum compatibility with both
32125 32-bit and 64-bit code.
32126
32127 @node STARLET and other predefined libraries
32128 @subsubsection STARLET and other predefined libraries
32129
32130 @noindent
32131 All code that comes as part of GNAT is 64-bit clean, but the
32132 restrictions given in @ref{Restrictions on use of 64 bit objects},
32133 still apply. Look at the package
32134 specs to see in which contexts objects allocated
32135 in 64-bit address space are acceptable.
32136
32137 @node Technical details
32138 @subsection Technical details
32139
32140 @noindent
32141 64-bit @value{EDITION} for Open VMS takes advantage of the freedom given in the
32142 Ada standard with respect to the type of @code{System.Address}. Previous
32143 versions of GNAT Pro have defined this type as private and implemented it as a
32144 modular type.
32145
32146 In order to allow defining @code{System.Short_Address} as a proper subtype,
32147 and to match the implicit sign extension in parameter passing,
32148 in 64-bit @value{EDITION} for Open VMS, @code{System.Address} is defined as a
32149 visible (i.e., non-private) integer type.
32150 Standard operations on the type, such as the binary operators ``+'', ``-'',
32151 etc., that take @code{Address} operands and return an @code{Address} result,
32152 have been hidden by declaring these
32153 @code{abstract}, a feature introduced in Ada 95 that helps avoid the potential
32154 ambiguities that would otherwise result from overloading.
32155 (Note that, although @code{Address} is a visible integer type,
32156 good programming practice dictates against exploiting the type's
32157 integer properties such as literals, since this will compromise
32158 code portability.)
32159
32160 Defining @code{Address} as a visible integer type helps achieve
32161 maximum compatibility for existing Ada code,
32162 without sacrificing the capabilities of the 64-bit architecture.
32163 @end ifset
32164
32165 @c ************************************************
32166 @ifset unw
32167 @node Microsoft Windows Topics
32168 @appendix Microsoft Windows Topics
32169 @cindex Windows NT
32170 @cindex Windows 95
32171 @cindex Windows 98
32172
32173 @noindent
32174 This chapter describes topics that are specific to the Microsoft Windows
32175 platforms (NT, 2000, and XP Professional).
32176
32177 @menu
32178 * Using GNAT on Windows::
32179 * Using a network installation of GNAT::
32180 * CONSOLE and WINDOWS subsystems::
32181 * Temporary Files::
32182 * Mixed-Language Programming on Windows::
32183 * Windows Calling Conventions::
32184 * Introduction to Dynamic Link Libraries (DLLs)::
32185 * Using DLLs with GNAT::
32186 * Building DLLs with GNAT::
32187 * Building DLLs with GNAT Project files::
32188 * Building DLLs with gnatdll::
32189 * GNAT and Windows Resources::
32190 * Debugging a DLL::
32191 * Setting Stack Size from gnatlink::
32192 * Setting Heap Size from gnatlink::
32193 @end menu
32194
32195 @node Using GNAT on Windows
32196 @section Using GNAT on Windows
32197
32198 @noindent
32199 One of the strengths of the GNAT technology is that its tool set
32200 (@command{gcc}, @command{gnatbind}, @command{gnatlink}, @command{gnatmake}, the
32201 @code{gdb} debugger, etc.) is used in the same way regardless of the
32202 platform.
32203
32204 On Windows this tool set is complemented by a number of Microsoft-specific
32205 tools that have been provided to facilitate interoperability with Windows
32206 when this is required. With these tools:
32207
32208 @itemize @bullet
32209
32210 @item
32211 You can build applications using the @code{CONSOLE} or @code{WINDOWS}
32212 subsystems.
32213
32214 @item
32215 You can use any Dynamically Linked Library (DLL) in your Ada code (both
32216 relocatable and non-relocatable DLLs are supported).
32217
32218 @item
32219 You can build Ada DLLs for use in other applications. These applications
32220 can be written in a language other than Ada (e.g., C, C++, etc). Again both
32221 relocatable and non-relocatable Ada DLLs are supported.
32222
32223 @item
32224 You can include Windows resources in your Ada application.
32225
32226 @item
32227 You can use or create COM/DCOM objects.
32228 @end itemize
32229
32230 @noindent
32231 Immediately below are listed all known general GNAT-for-Windows restrictions.
32232 Other restrictions about specific features like Windows Resources and DLLs
32233 are listed in separate sections below.
32234
32235 @itemize @bullet
32236
32237 @item
32238 It is not possible to use @code{GetLastError} and @code{SetLastError}
32239 when tasking, protected records, or exceptions are used. In these
32240 cases, in order to implement Ada semantics, the GNAT run-time system
32241 calls certain Win32 routines that set the last error variable to 0 upon
32242 success. It should be possible to use @code{GetLastError} and
32243 @code{SetLastError} when tasking, protected record, and exception
32244 features are not used, but it is not guaranteed to work.
32245
32246 @item
32247 It is not possible to link against Microsoft libraries except for
32248 import libraries. The library must be built to be compatible with
32249 @file{MSVCRT.LIB} (/MD Microsoft compiler option), @file{LIBC.LIB} and
32250 @file{LIBCMT.LIB} (/ML or /MT Microsoft compiler options) are known to
32251 not be compatible with the GNAT runtime. Even if the library is
32252 compatible with @file{MSVCRT.LIB} it is not guaranteed to work.
32253
32254 @item
32255 When the compilation environment is located on FAT32 drives, users may
32256 experience recompilations of the source files that have not changed if
32257 Daylight Saving Time (DST) state has changed since the last time files
32258 were compiled. NTFS drives do not have this problem.
32259
32260 @item
32261 No components of the GNAT toolset use any entries in the Windows
32262 registry. The only entries that can be created are file associations and
32263 PATH settings, provided the user has chosen to create them at installation
32264 time, as well as some minimal book-keeping information needed to correctly
32265 uninstall or integrate different GNAT products.
32266 @end itemize
32267
32268 @node Using a network installation of GNAT
32269 @section Using a network installation of GNAT
32270
32271 @noindent
32272 Make sure the system on which GNAT is installed is accessible from the
32273 current machine, i.e., the install location is shared over the network.
32274 Shared resources are accessed on Windows by means of UNC paths, which
32275 have the format @code{\\server\sharename\path}
32276
32277 In order to use such a network installation, simply add the UNC path of the
32278 @file{bin} directory of your GNAT installation in front of your PATH. For
32279 example, if GNAT is installed in @file{\GNAT} directory of a share location
32280 called @file{c-drive} on a machine @file{LOKI}, the following command will
32281 make it available:
32282
32283 @code{@ @ @ path \\loki\c-drive\gnat\bin;%path%}
32284
32285 Be aware that every compilation using the network installation results in the
32286 transfer of large amounts of data across the network and will likely cause
32287 serious performance penalty.
32288
32289 @node CONSOLE and WINDOWS subsystems
32290 @section CONSOLE and WINDOWS subsystems
32291 @cindex CONSOLE Subsystem
32292 @cindex WINDOWS Subsystem
32293 @cindex -mwindows
32294
32295 @noindent
32296 There are two main subsystems under Windows. The @code{CONSOLE} subsystem
32297 (which is the default subsystem) will always create a console when
32298 launching the application. This is not something desirable when the
32299 application has a Windows GUI. To get rid of this console the
32300 application must be using the @code{WINDOWS} subsystem. To do so
32301 the @option{-mwindows} linker option must be specified.
32302
32303 @smallexample
32304 $ gnatmake winprog -largs -mwindows
32305 @end smallexample
32306
32307 @node Temporary Files
32308 @section Temporary Files
32309 @cindex Temporary files
32310
32311 @noindent
32312 It is possible to control where temporary files gets created by setting
32313 the @env{TMP} environment variable. The file will be created:
32314
32315 @itemize
32316 @item Under the directory pointed to by the @env{TMP} environment variable if
32317 this directory exists.
32318
32319 @item Under @file{c:\temp}, if the @env{TMP} environment variable is not
32320 set (or not pointing to a directory) and if this directory exists.
32321
32322 @item Under the current working directory otherwise.
32323 @end itemize
32324
32325 @noindent
32326 This allows you to determine exactly where the temporary
32327 file will be created. This is particularly useful in networked
32328 environments where you may not have write access to some
32329 directories.
32330
32331 @node Mixed-Language Programming on Windows
32332 @section Mixed-Language Programming on Windows
32333
32334 @noindent
32335 Developing pure Ada applications on Windows is no different than on
32336 other GNAT-supported platforms. However, when developing or porting an
32337 application that contains a mix of Ada and C/C++, the choice of your
32338 Windows C/C++ development environment conditions your overall
32339 interoperability strategy.
32340
32341 If you use @command{gcc} to compile the non-Ada part of your application,
32342 there are no Windows-specific restrictions that affect the overall
32343 interoperability with your Ada code. If you plan to use
32344 Microsoft tools (e.g.@: Microsoft Visual C/C++), you should be aware of
32345 the following limitations:
32346
32347 @itemize @bullet
32348 @item
32349 You cannot link your Ada code with an object or library generated with
32350 Microsoft tools if these use the @code{.tls} section (Thread Local
32351 Storage section) since the GNAT linker does not yet support this section.
32352
32353 @item
32354 You cannot link your Ada code with an object or library generated with
32355 Microsoft tools if these use I/O routines other than those provided in
32356 the Microsoft DLL: @code{msvcrt.dll}. This is because the GNAT run time
32357 uses the services of @code{msvcrt.dll} for its I/Os. Use of other I/O
32358 libraries can cause a conflict with @code{msvcrt.dll} services. For
32359 instance Visual C++ I/O stream routines conflict with those in
32360 @code{msvcrt.dll}.
32361 @end itemize
32362
32363 @noindent
32364 If you do want to use the Microsoft tools for your non-Ada code and hit one
32365 of the above limitations, you have two choices:
32366
32367 @enumerate
32368 @item
32369 Encapsulate your non-Ada code in a DLL to be linked with your Ada
32370 application. In this case, use the Microsoft or whatever environment to
32371 build the DLL and use GNAT to build your executable
32372 (@pxref{Using DLLs with GNAT}).
32373
32374 @item
32375 Or you can encapsulate your Ada code in a DLL to be linked with the
32376 other part of your application. In this case, use GNAT to build the DLL
32377 (@pxref{Building DLLs with GNAT}) and use the Microsoft or whatever
32378 environment to build your executable.
32379 @end enumerate
32380
32381 @node Windows Calling Conventions
32382 @section Windows Calling Conventions
32383 @findex Stdcall
32384 @findex APIENTRY
32385
32386 @menu
32387 * C Calling Convention::
32388 * Stdcall Calling Convention::
32389 * Win32 Calling Convention::
32390 * DLL Calling Convention::
32391 @end menu
32392
32393 @noindent
32394 When a subprogram @code{F} (caller) calls a subprogram @code{G}
32395 (callee), there are several ways to push @code{G}'s parameters on the
32396 stack and there are several possible scenarios to clean up the stack
32397 upon @code{G}'s return. A calling convention is an agreed upon software
32398 protocol whereby the responsibilities between the caller (@code{F}) and
32399 the callee (@code{G}) are clearly defined. Several calling conventions
32400 are available for Windows:
32401
32402 @itemize @bullet
32403 @item
32404 @code{C} (Microsoft defined)
32405
32406 @item
32407 @code{Stdcall} (Microsoft defined)
32408
32409 @item
32410 @code{Win32} (GNAT specific)
32411
32412 @item
32413 @code{DLL} (GNAT specific)
32414 @end itemize
32415
32416 @node C Calling Convention
32417 @subsection @code{C} Calling Convention
32418
32419 @noindent
32420 This is the default calling convention used when interfacing to C/C++
32421 routines compiled with either @command{gcc} or Microsoft Visual C++.
32422
32423 In the @code{C} calling convention subprogram parameters are pushed on the
32424 stack by the caller from right to left. The caller itself is in charge of
32425 cleaning up the stack after the call. In addition, the name of a routine
32426 with @code{C} calling convention is mangled by adding a leading underscore.
32427
32428 The name to use on the Ada side when importing (or exporting) a routine
32429 with @code{C} calling convention is the name of the routine. For
32430 instance the C function:
32431
32432 @smallexample
32433 int get_val (long);
32434 @end smallexample
32435
32436 @noindent
32437 should be imported from Ada as follows:
32438
32439 @smallexample @c ada
32440 @group
32441 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
32442 pragma Import (C, Get_Val, External_Name => "get_val");
32443 @end group
32444 @end smallexample
32445
32446 @noindent
32447 Note that in this particular case the @code{External_Name} parameter could
32448 have been omitted since, when missing, this parameter is taken to be the
32449 name of the Ada entity in lower case. When the @code{Link_Name} parameter
32450 is missing, as in the above example, this parameter is set to be the
32451 @code{External_Name} with a leading underscore.
32452
32453 When importing a variable defined in C, you should always use the @code{C}
32454 calling convention unless the object containing the variable is part of a
32455 DLL (in which case you should use the @code{Stdcall} calling
32456 convention, @pxref{Stdcall Calling Convention}).
32457
32458 @node Stdcall Calling Convention
32459 @subsection @code{Stdcall} Calling Convention
32460
32461 @noindent
32462 This convention, which was the calling convention used for Pascal
32463 programs, is used by Microsoft for all the routines in the Win32 API for
32464 efficiency reasons. It must be used to import any routine for which this
32465 convention was specified.
32466
32467 In the @code{Stdcall} calling convention subprogram parameters are pushed
32468 on the stack by the caller from right to left. The callee (and not the
32469 caller) is in charge of cleaning the stack on routine exit. In addition,
32470 the name of a routine with @code{Stdcall} calling convention is mangled by
32471 adding a leading underscore (as for the @code{C} calling convention) and a
32472 trailing @code{@@}@code{@var{nn}}, where @var{nn} is the overall size (in
32473 bytes) of the parameters passed to the routine.
32474
32475 The name to use on the Ada side when importing a C routine with a
32476 @code{Stdcall} calling convention is the name of the C routine. The leading
32477 underscore and trailing @code{@@}@code{@var{nn}} are added automatically by
32478 the compiler. For instance the Win32 function:
32479
32480 @smallexample
32481 @b{APIENTRY} int get_val (long);
32482 @end smallexample
32483
32484 @noindent
32485 should be imported from Ada as follows:
32486
32487 @smallexample @c ada
32488 @group
32489 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
32490 pragma Import (Stdcall, Get_Val);
32491 -- On the x86 a long is 4 bytes, so the Link_Name is "_get_val@@4"
32492 @end group
32493 @end smallexample
32494
32495 @noindent
32496 As for the @code{C} calling convention, when the @code{External_Name}
32497 parameter is missing, it is taken to be the name of the Ada entity in lower
32498 case. If instead of writing the above import pragma you write:
32499
32500 @smallexample @c ada
32501 @group
32502 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
32503 pragma Import (Stdcall, Get_Val, External_Name => "retrieve_val");
32504 @end group
32505 @end smallexample
32506
32507 @noindent
32508 then the imported routine is @code{_retrieve_val@@4}. However, if instead
32509 of specifying the @code{External_Name} parameter you specify the
32510 @code{Link_Name} as in the following example:
32511
32512 @smallexample @c ada
32513 @group
32514 function Get_Val (V : Interfaces.C.long) return Interfaces.C.int;
32515 pragma Import (Stdcall, Get_Val, Link_Name => "retrieve_val");
32516 @end group
32517 @end smallexample
32518
32519 @noindent
32520 then the imported routine is @code{retrieve_val}, that is, there is no
32521 decoration at all. No leading underscore and no Stdcall suffix
32522 @code{@@}@code{@var{nn}}.
32523
32524 @noindent
32525 This is especially important as in some special cases a DLL's entry
32526 point name lacks a trailing @code{@@}@code{@var{nn}} while the exported
32527 name generated for a call has it.
32528
32529 @noindent
32530 It is also possible to import variables defined in a DLL by using an
32531 import pragma for a variable. As an example, if a DLL contains a
32532 variable defined as:
32533
32534 @smallexample
32535 int my_var;
32536 @end smallexample
32537
32538 @noindent
32539 then, to access this variable from Ada you should write:
32540
32541 @smallexample @c ada
32542 @group
32543 My_Var : Interfaces.C.int;
32544 pragma Import (Stdcall, My_Var);
32545 @end group
32546 @end smallexample
32547
32548 @noindent
32549 Note that to ease building cross-platform bindings this convention
32550 will be handled as a @code{C} calling convention on non-Windows platforms.
32551
32552 @node Win32 Calling Convention
32553 @subsection @code{Win32} Calling Convention
32554
32555 @noindent
32556 This convention, which is GNAT-specific is fully equivalent to the
32557 @code{Stdcall} calling convention described above.
32558
32559 @node DLL Calling Convention
32560 @subsection @code{DLL} Calling Convention
32561
32562 @noindent
32563 This convention, which is GNAT-specific is fully equivalent to the
32564 @code{Stdcall} calling convention described above.
32565
32566 @node Introduction to Dynamic Link Libraries (DLLs)
32567 @section Introduction to Dynamic Link Libraries (DLLs)
32568 @findex DLL
32569
32570 @noindent
32571 A Dynamically Linked Library (DLL) is a library that can be shared by
32572 several applications running under Windows. A DLL can contain any number of
32573 routines and variables.
32574
32575 One advantage of DLLs is that you can change and enhance them without
32576 forcing all the applications that depend on them to be relinked or
32577 recompiled. However, you should be aware than all calls to DLL routines are
32578 slower since, as you will understand below, such calls are indirect.
32579
32580 To illustrate the remainder of this section, suppose that an application
32581 wants to use the services of a DLL @file{API.dll}. To use the services
32582 provided by @file{API.dll} you must statically link against the DLL or
32583 an import library which contains a jump table with an entry for each
32584 routine and variable exported by the DLL. In the Microsoft world this
32585 import library is called @file{API.lib}. When using GNAT this import
32586 library is called either @file{libAPI.dll.a}, @file{libapi.dll.a},
32587 @file{libAPI.a} or @file{libapi.a} (names are case insensitive).
32588
32589 After you have linked your application with the DLL or the import library
32590 and you run your application, here is what happens:
32591
32592 @enumerate
32593 @item
32594 Your application is loaded into memory.
32595
32596 @item
32597 The DLL @file{API.dll} is mapped into the address space of your
32598 application. This means that:
32599
32600 @itemize @bullet
32601 @item
32602 The DLL will use the stack of the calling thread.
32603
32604 @item
32605 The DLL will use the virtual address space of the calling process.
32606
32607 @item
32608 The DLL will allocate memory from the virtual address space of the calling
32609 process.
32610
32611 @item
32612 Handles (pointers) can be safely exchanged between routines in the DLL
32613 routines and routines in the application using the DLL.
32614 @end itemize
32615
32616 @item
32617 The entries in the jump table (from the import library @file{libAPI.dll.a}
32618 or @file{API.lib} or automatically created when linking against a DLL)
32619 which is part of your application are initialized with the addresses
32620 of the routines and variables in @file{API.dll}.
32621
32622 @item
32623 If present in @file{API.dll}, routines @code{DllMain} or
32624 @code{DllMainCRTStartup} are invoked. These routines typically contain
32625 the initialization code needed for the well-being of the routines and
32626 variables exported by the DLL.
32627 @end enumerate
32628
32629 @noindent
32630 There is an additional point which is worth mentioning. In the Windows
32631 world there are two kind of DLLs: relocatable and non-relocatable
32632 DLLs. Non-relocatable DLLs can only be loaded at a very specific address
32633 in the target application address space. If the addresses of two
32634 non-relocatable DLLs overlap and these happen to be used by the same
32635 application, a conflict will occur and the application will run
32636 incorrectly. Hence, when possible, it is always preferable to use and
32637 build relocatable DLLs. Both relocatable and non-relocatable DLLs are
32638 supported by GNAT. Note that the @option{-s} linker option (see GNU Linker
32639 User's Guide) removes the debugging symbols from the DLL but the DLL can
32640 still be relocated.
32641
32642 As a side note, an interesting difference between Microsoft DLLs and
32643 Unix shared libraries, is the fact that on most Unix systems all public
32644 routines are exported by default in a Unix shared library, while under
32645 Windows it is possible (but not required) to list exported routines in
32646 a definition file (@pxref{The Definition File}).
32647
32648 @node Using DLLs with GNAT
32649 @section Using DLLs with GNAT
32650
32651 @menu
32652 * Creating an Ada Spec for the DLL Services::
32653 * Creating an Import Library::
32654 @end menu
32655
32656 @noindent
32657 To use the services of a DLL, say @file{API.dll}, in your Ada application
32658 you must have:
32659
32660 @enumerate
32661 @item
32662 The Ada spec for the routines and/or variables you want to access in
32663 @file{API.dll}. If not available this Ada spec must be built from the C/C++
32664 header files provided with the DLL.
32665
32666 @item
32667 The import library (@file{libAPI.dll.a} or @file{API.lib}). As previously
32668 mentioned an import library is a statically linked library containing the
32669 import table which will be filled at load time to point to the actual
32670 @file{API.dll} routines. Sometimes you don't have an import library for the
32671 DLL you want to use. The following sections will explain how to build
32672 one. Note that this is optional.
32673
32674 @item
32675 The actual DLL, @file{API.dll}.
32676 @end enumerate
32677
32678 @noindent
32679 Once you have all the above, to compile an Ada application that uses the
32680 services of @file{API.dll} and whose main subprogram is @code{My_Ada_App},
32681 you simply issue the command
32682
32683 @smallexample
32684 $ gnatmake my_ada_app -largs -lAPI
32685 @end smallexample
32686
32687 @noindent
32688 The argument @option{-largs -lAPI} at the end of the @command{gnatmake} command
32689 tells the GNAT linker to look first for a library named @file{API.lib}
32690 (Microsoft-style name) and if not found for a libraries named
32691 @file{libAPI.dll.a}, @file{API.dll.a} or @file{libAPI.a}.
32692 (GNAT-style name). Note that if the Ada package spec for @file{API.dll}
32693 contains the following pragma
32694
32695 @smallexample @c ada
32696 pragma Linker_Options ("-lAPI");
32697 @end smallexample
32698
32699 @noindent
32700 you do not have to add @option{-largs -lAPI} at the end of the
32701 @command{gnatmake} command.
32702
32703 If any one of the items above is missing you will have to create it
32704 yourself. The following sections explain how to do so using as an
32705 example a fictitious DLL called @file{API.dll}.
32706
32707 @node Creating an Ada Spec for the DLL Services
32708 @subsection Creating an Ada Spec for the DLL Services
32709
32710 @noindent
32711 A DLL typically comes with a C/C++ header file which provides the
32712 definitions of the routines and variables exported by the DLL. The Ada
32713 equivalent of this header file is a package spec that contains definitions
32714 for the imported entities. If the DLL you intend to use does not come with
32715 an Ada spec you have to generate one such spec yourself. For example if
32716 the header file of @file{API.dll} is a file @file{api.h} containing the
32717 following two definitions:
32718
32719 @smallexample
32720 @group
32721 @cartouche
32722 int some_var;
32723 int get (char *);
32724 @end cartouche
32725 @end group
32726 @end smallexample
32727
32728 @noindent
32729 then the equivalent Ada spec could be:
32730
32731 @smallexample @c ada
32732 @group
32733 @cartouche
32734 with Interfaces.C.Strings;
32735 package API is
32736 use Interfaces;
32737
32738 Some_Var : C.int;
32739 function Get (Str : C.Strings.Chars_Ptr) return C.int;
32740
32741 private
32742 pragma Import (C, Get);
32743 pragma Import (DLL, Some_Var);
32744 end API;
32745 @end cartouche
32746 @end group
32747 @end smallexample
32748
32749 @noindent
32750 Note that a variable is
32751 @strong{always imported with a Stdcall convention}. A function
32752 can have @code{C} or @code{Stdcall} convention.
32753 (@pxref{Windows Calling Conventions}).
32754
32755 @node Creating an Import Library
32756 @subsection Creating an Import Library
32757 @cindex Import library
32758
32759 @menu
32760 * The Definition File::
32761 * GNAT-Style Import Library::
32762 * Microsoft-Style Import Library::
32763 @end menu
32764
32765 @noindent
32766 If a Microsoft-style import library @file{API.lib} or a GNAT-style
32767 import library @file{libAPI.dll.a} or @file{libAPI.a} is available
32768 with @file{API.dll} you can skip this section. You can also skip this
32769 section if @file{API.dll} or @file{libAPI.dll} is built with GNU tools
32770 as in this case it is possible to link directly against the
32771 DLL. Otherwise read on.
32772
32773 @node The Definition File
32774 @subsubsection The Definition File
32775 @cindex Definition file
32776 @findex .def
32777
32778 @noindent
32779 As previously mentioned, and unlike Unix systems, the list of symbols
32780 that are exported from a DLL must be provided explicitly in Windows.
32781 The main goal of a definition file is precisely that: list the symbols
32782 exported by a DLL. A definition file (usually a file with a @code{.def}
32783 suffix) has the following structure:
32784
32785 @smallexample
32786 @group
32787 @cartouche
32788 @r{[}LIBRARY @var{name}@r{]}
32789 @r{[}DESCRIPTION @var{string}@r{]}
32790 EXPORTS
32791 @var{symbol1}
32792 @var{symbol2}
32793 @dots{}
32794 @end cartouche
32795 @end group
32796 @end smallexample
32797
32798 @table @code
32799 @item LIBRARY @var{name}
32800 This section, which is optional, gives the name of the DLL.
32801
32802 @item DESCRIPTION @var{string}
32803 This section, which is optional, gives a description string that will be
32804 embedded in the import library.
32805
32806 @item EXPORTS
32807 This section gives the list of exported symbols (procedures, functions or
32808 variables). For instance in the case of @file{API.dll} the @code{EXPORTS}
32809 section of @file{API.def} looks like:
32810
32811 @smallexample
32812 @group
32813 @cartouche
32814 EXPORTS
32815 some_var
32816 get
32817 @end cartouche
32818 @end group
32819 @end smallexample
32820 @end table
32821
32822 @noindent
32823 Note that you must specify the correct suffix (@code{@@}@code{@var{nn}})
32824 (@pxref{Windows Calling Conventions}) for a Stdcall
32825 calling convention function in the exported symbols list.
32826
32827 @noindent
32828 There can actually be other sections in a definition file, but these
32829 sections are not relevant to the discussion at hand.
32830
32831 @node GNAT-Style Import Library
32832 @subsubsection GNAT-Style Import Library
32833
32834 @noindent
32835 To create a static import library from @file{API.dll} with the GNAT tools
32836 you should proceed as follows:
32837
32838 @enumerate
32839 @item
32840 Create the definition file @file{API.def} (@pxref{The Definition File}).
32841 For that use the @code{dll2def} tool as follows:
32842
32843 @smallexample
32844 $ dll2def API.dll > API.def
32845 @end smallexample
32846
32847 @noindent
32848 @code{dll2def} is a very simple tool: it takes as input a DLL and prints
32849 to standard output the list of entry points in the DLL. Note that if
32850 some routines in the DLL have the @code{Stdcall} convention
32851 (@pxref{Windows Calling Conventions}) with stripped @code{@@}@var{nn}
32852 suffix then you'll have to edit @file{api.def} to add it, and specify
32853 @option{-k} to @command{gnatdll} when creating the import library.
32854
32855 @noindent
32856 Here are some hints to find the right @code{@@}@var{nn} suffix.
32857
32858 @enumerate
32859 @item
32860 If you have the Microsoft import library (.lib), it is possible to get
32861 the right symbols by using Microsoft @code{dumpbin} tool (see the
32862 corresponding Microsoft documentation for further details).
32863
32864 @smallexample
32865 $ dumpbin /exports api.lib
32866 @end smallexample
32867
32868 @item
32869 If you have a message about a missing symbol at link time the compiler
32870 tells you what symbol is expected. You just have to go back to the
32871 definition file and add the right suffix.
32872 @end enumerate
32873
32874 @item
32875 Build the import library @code{libAPI.dll.a}, using @code{gnatdll}
32876 (@pxref{Using gnatdll}) as follows:
32877
32878 @smallexample
32879 $ gnatdll -e API.def -d API.dll
32880 @end smallexample
32881
32882 @noindent
32883 @code{gnatdll} takes as input a definition file @file{API.def} and the
32884 name of the DLL containing the services listed in the definition file
32885 @file{API.dll}. The name of the static import library generated is
32886 computed from the name of the definition file as follows: if the
32887 definition file name is @var{xyz}@code{.def}, the import library name will
32888 be @code{lib}@var{xyz}@code{.a}. Note that in the previous example option
32889 @option{-e} could have been removed because the name of the definition
32890 file (before the ``@code{.def}'' suffix) is the same as the name of the
32891 DLL (@pxref{Using gnatdll} for more information about @code{gnatdll}).
32892 @end enumerate
32893
32894 @node Microsoft-Style Import Library
32895 @subsubsection Microsoft-Style Import Library
32896
32897 @noindent
32898 With GNAT you can either use a GNAT-style or Microsoft-style import
32899 library. A Microsoft import library is needed only if you plan to make an
32900 Ada DLL available to applications developed with Microsoft
32901 tools (@pxref{Mixed-Language Programming on Windows}).
32902
32903 To create a Microsoft-style import library for @file{API.dll} you
32904 should proceed as follows:
32905
32906 @enumerate
32907 @item
32908 Create the definition file @file{API.def} from the DLL. For this use either
32909 the @code{dll2def} tool as described above or the Microsoft @code{dumpbin}
32910 tool (see the corresponding Microsoft documentation for further details).
32911
32912 @item
32913 Build the actual import library using Microsoft's @code{lib} utility:
32914
32915 @smallexample
32916 $ lib -machine:IX86 -def:API.def -out:API.lib
32917 @end smallexample
32918
32919 @noindent
32920 If you use the above command the definition file @file{API.def} must
32921 contain a line giving the name of the DLL:
32922
32923 @smallexample
32924 LIBRARY "API"
32925 @end smallexample
32926
32927 @noindent
32928 See the Microsoft documentation for further details about the usage of
32929 @code{lib}.
32930 @end enumerate
32931
32932 @node Building DLLs with GNAT
32933 @section Building DLLs with GNAT
32934 @cindex DLLs, building
32935
32936 @noindent
32937 This section explain how to build DLLs using the GNAT built-in DLL
32938 support. With the following procedure it is straight forward to build
32939 and use DLLs with GNAT.
32940
32941 @enumerate
32942
32943 @item building object files
32944
32945 The first step is to build all objects files that are to be included
32946 into the DLL. This is done by using the standard @command{gnatmake} tool.
32947
32948 @item building the DLL
32949
32950 To build the DLL you must use @command{gcc}'s @option{-shared}
32951 option. It is quite simple to use this method:
32952
32953 @smallexample
32954 $ gcc -shared -o api.dll obj1.o obj2.o @dots{}
32955 @end smallexample
32956
32957 It is important to note that in this case all symbols found in the
32958 object files are automatically exported. It is possible to restrict
32959 the set of symbols to export by passing to @command{gcc} a definition
32960 file, @pxref{The Definition File}. For example:
32961
32962 @smallexample
32963 $ gcc -shared -o api.dll api.def obj1.o obj2.o @dots{}
32964 @end smallexample
32965
32966 If you use a definition file you must export the elaboration procedures
32967 for every package that required one. Elaboration procedures are named
32968 using the package name followed by "_E".
32969
32970 @item preparing DLL to be used
32971
32972 For the DLL to be used by client programs the bodies must be hidden
32973 from it and the .ali set with read-only attribute. This is very important
32974 otherwise GNAT will recompile all packages and will not actually use
32975 the code in the DLL. For example:
32976
32977 @smallexample
32978 $ mkdir apilib
32979 $ copy *.ads *.ali api.dll apilib
32980 $ attrib +R apilib\*.ali
32981 @end smallexample
32982
32983 @end enumerate
32984
32985 At this point it is possible to use the DLL by directly linking
32986 against it. Note that you must use the GNAT shared runtime when using
32987 GNAT shared libraries. This is achieved by using @option{-shared} binder's
32988 option.
32989
32990 @smallexample
32991 $ gnatmake main -Iapilib -bargs -shared -largs -Lapilib -lAPI
32992 @end smallexample
32993
32994 @node Building DLLs with GNAT Project files
32995 @section Building DLLs with GNAT Project files
32996 @cindex DLLs, building
32997
32998 @noindent
32999 There is nothing specific to Windows in the build process.
33000 @pxref{Library Projects}.
33001
33002 @noindent
33003 Due to a system limitation, it is not possible under Windows to create threads
33004 when inside the @code{DllMain} routine which is used for auto-initialization
33005 of shared libraries, so it is not possible to have library level tasks in SALs.
33006
33007 @node Building DLLs with gnatdll
33008 @section Building DLLs with gnatdll
33009 @cindex DLLs, building
33010
33011 @menu
33012 * Limitations When Using Ada DLLs from Ada::
33013 * Exporting Ada Entities::
33014 * Ada DLLs and Elaboration::
33015 * Ada DLLs and Finalization::
33016 * Creating a Spec for Ada DLLs::
33017 * Creating the Definition File::
33018 * Using gnatdll::
33019 @end menu
33020
33021 @noindent
33022 Note that it is preferred to use the built-in GNAT DLL support
33023 (@pxref{Building DLLs with GNAT}) or GNAT Project files
33024 (@pxref{Building DLLs with GNAT Project files}) to build DLLs.
33025
33026 This section explains how to build DLLs containing Ada code using
33027 @code{gnatdll}. These DLLs will be referred to as Ada DLLs in the
33028 remainder of this section.
33029
33030 The steps required to build an Ada DLL that is to be used by Ada as well as
33031 non-Ada applications are as follows:
33032
33033 @enumerate
33034 @item
33035 You need to mark each Ada @i{entity} exported by the DLL with a @code{C} or
33036 @code{Stdcall} calling convention to avoid any Ada name mangling for the
33037 entities exported by the DLL (@pxref{Exporting Ada Entities}). You can
33038 skip this step if you plan to use the Ada DLL only from Ada applications.
33039
33040 @item
33041 Your Ada code must export an initialization routine which calls the routine
33042 @code{adainit} generated by @command{gnatbind} to perform the elaboration of
33043 the Ada code in the DLL (@pxref{Ada DLLs and Elaboration}). The initialization
33044 routine exported by the Ada DLL must be invoked by the clients of the DLL
33045 to initialize the DLL.
33046
33047 @item
33048 When useful, the DLL should also export a finalization routine which calls
33049 routine @code{adafinal} generated by @command{gnatbind} to perform the
33050 finalization of the Ada code in the DLL (@pxref{Ada DLLs and Finalization}).
33051 The finalization routine exported by the Ada DLL must be invoked by the
33052 clients of the DLL when the DLL services are no further needed.
33053
33054 @item
33055 You must provide a spec for the services exported by the Ada DLL in each
33056 of the programming languages to which you plan to make the DLL available.
33057
33058 @item
33059 You must provide a definition file listing the exported entities
33060 (@pxref{The Definition File}).
33061
33062 @item
33063 Finally you must use @code{gnatdll} to produce the DLL and the import
33064 library (@pxref{Using gnatdll}).
33065 @end enumerate
33066
33067 @noindent
33068 Note that a relocatable DLL stripped using the @code{strip}
33069 binutils tool will not be relocatable anymore. To build a DLL without
33070 debug information pass @code{-largs -s} to @code{gnatdll}. This
33071 restriction does not apply to a DLL built using a Library Project.
33072 @pxref{Library Projects}.
33073
33074 @node Limitations When Using Ada DLLs from Ada
33075 @subsection Limitations When Using Ada DLLs from Ada
33076
33077 @noindent
33078 When using Ada DLLs from Ada applications there is a limitation users
33079 should be aware of. Because on Windows the GNAT run time is not in a DLL of
33080 its own, each Ada DLL includes a part of the GNAT run time. Specifically,
33081 each Ada DLL includes the services of the GNAT run time that are necessary
33082 to the Ada code inside the DLL. As a result, when an Ada program uses an
33083 Ada DLL there are two independent GNAT run times: one in the Ada DLL and
33084 one in the main program.
33085
33086 It is therefore not possible to exchange GNAT run-time objects between the
33087 Ada DLL and the main Ada program. Example of GNAT run-time objects are file
33088 handles (e.g.@: @code{Text_IO.File_Type}), tasks types, protected objects
33089 types, etc.
33090
33091 It is completely safe to exchange plain elementary, array or record types,
33092 Windows object handles, etc.
33093
33094 @node Exporting Ada Entities
33095 @subsection Exporting Ada Entities
33096 @cindex Export table
33097
33098 @noindent
33099 Building a DLL is a way to encapsulate a set of services usable from any
33100 application. As a result, the Ada entities exported by a DLL should be
33101 exported with the @code{C} or @code{Stdcall} calling conventions to avoid
33102 any Ada name mangling. As an example here is an Ada package
33103 @code{API}, spec and body, exporting two procedures, a function, and a
33104 variable:
33105
33106 @smallexample @c ada
33107 @group
33108 @cartouche
33109 with Interfaces.C; use Interfaces;
33110 package API is
33111 Count : C.int := 0;
33112 function Factorial (Val : C.int) return C.int;
33113
33114 procedure Initialize_API;
33115 procedure Finalize_API;
33116 -- Initialization & Finalization routines. More in the next section.
33117 private
33118 pragma Export (C, Initialize_API);
33119 pragma Export (C, Finalize_API);
33120 pragma Export (C, Count);
33121 pragma Export (C, Factorial);
33122 end API;
33123 @end cartouche
33124 @end group
33125 @end smallexample
33126
33127 @smallexample @c ada
33128 @group
33129 @cartouche
33130 package body API is
33131 function Factorial (Val : C.int) return C.int is
33132 Fact : C.int := 1;
33133 begin
33134 Count := Count + 1;
33135 for K in 1 .. Val loop
33136 Fact := Fact * K;
33137 end loop;
33138 return Fact;
33139 end Factorial;
33140
33141 procedure Initialize_API is
33142 procedure Adainit;
33143 pragma Import (C, Adainit);
33144 begin
33145 Adainit;
33146 end Initialize_API;
33147
33148 procedure Finalize_API is
33149 procedure Adafinal;
33150 pragma Import (C, Adafinal);
33151 begin
33152 Adafinal;
33153 end Finalize_API;
33154 end API;
33155 @end cartouche
33156 @end group
33157 @end smallexample
33158
33159 @noindent
33160 If the Ada DLL you are building will only be used by Ada applications
33161 you do not have to export Ada entities with a @code{C} or @code{Stdcall}
33162 convention. As an example, the previous package could be written as
33163 follows:
33164
33165 @smallexample @c ada
33166 @group
33167 @cartouche
33168 package API is
33169 Count : Integer := 0;
33170 function Factorial (Val : Integer) return Integer;
33171
33172 procedure Initialize_API;
33173 procedure Finalize_API;
33174 -- Initialization and Finalization routines.
33175 end API;
33176 @end cartouche
33177 @end group
33178 @end smallexample
33179
33180 @smallexample @c ada
33181 @group
33182 @cartouche
33183 package body API is
33184 function Factorial (Val : Integer) return Integer is
33185 Fact : Integer := 1;
33186 begin
33187 Count := Count + 1;
33188 for K in 1 .. Val loop
33189 Fact := Fact * K;
33190 end loop;
33191 return Fact;
33192 end Factorial;
33193
33194 @dots{}
33195 -- The remainder of this package body is unchanged.
33196 end API;
33197 @end cartouche
33198 @end group
33199 @end smallexample
33200
33201 @noindent
33202 Note that if you do not export the Ada entities with a @code{C} or
33203 @code{Stdcall} convention you will have to provide the mangled Ada names
33204 in the definition file of the Ada DLL
33205 (@pxref{Creating the Definition File}).
33206
33207 @node Ada DLLs and Elaboration
33208 @subsection Ada DLLs and Elaboration
33209 @cindex DLLs and elaboration
33210
33211 @noindent
33212 The DLL that you are building contains your Ada code as well as all the
33213 routines in the Ada library that are needed by it. The first thing a
33214 user of your DLL must do is elaborate the Ada code
33215 (@pxref{Elaboration Order Handling in GNAT}).
33216
33217 To achieve this you must export an initialization routine
33218 (@code{Initialize_API} in the previous example), which must be invoked
33219 before using any of the DLL services. This elaboration routine must call
33220 the Ada elaboration routine @code{adainit} generated by the GNAT binder
33221 (@pxref{Binding with Non-Ada Main Programs}). See the body of
33222 @code{Initialize_Api} for an example. Note that the GNAT binder is
33223 automatically invoked during the DLL build process by the @code{gnatdll}
33224 tool (@pxref{Using gnatdll}).
33225
33226 When a DLL is loaded, Windows systematically invokes a routine called
33227 @code{DllMain}. It would therefore be possible to call @code{adainit}
33228 directly from @code{DllMain} without having to provide an explicit
33229 initialization routine. Unfortunately, it is not possible to call
33230 @code{adainit} from the @code{DllMain} if your program has library level
33231 tasks because access to the @code{DllMain} entry point is serialized by
33232 the system (that is, only a single thread can execute ``through'' it at a
33233 time), which means that the GNAT run time will deadlock waiting for the
33234 newly created task to complete its initialization.
33235
33236 @node Ada DLLs and Finalization
33237 @subsection Ada DLLs and Finalization
33238 @cindex DLLs and finalization
33239
33240 @noindent
33241 When the services of an Ada DLL are no longer needed, the client code should
33242 invoke the DLL finalization routine, if available. The DLL finalization
33243 routine is in charge of releasing all resources acquired by the DLL. In the
33244 case of the Ada code contained in the DLL, this is achieved by calling
33245 routine @code{adafinal} generated by the GNAT binder
33246 (@pxref{Binding with Non-Ada Main Programs}).
33247 See the body of @code{Finalize_Api} for an
33248 example. As already pointed out the GNAT binder is automatically invoked
33249 during the DLL build process by the @code{gnatdll} tool
33250 (@pxref{Using gnatdll}).
33251
33252 @node Creating a Spec for Ada DLLs
33253 @subsection Creating a Spec for Ada DLLs
33254
33255 @noindent
33256 To use the services exported by the Ada DLL from another programming
33257 language (e.g.@: C), you have to translate the specs of the exported Ada
33258 entities in that language. For instance in the case of @code{API.dll},
33259 the corresponding C header file could look like:
33260
33261 @smallexample
33262 @group
33263 @cartouche
33264 extern int *_imp__count;
33265 #define count (*_imp__count)
33266 int factorial (int);
33267 @end cartouche
33268 @end group
33269 @end smallexample
33270
33271 @noindent
33272 It is important to understand that when building an Ada DLL to be used by
33273 other Ada applications, you need two different specs for the packages
33274 contained in the DLL: one for building the DLL and the other for using
33275 the DLL. This is because the @code{DLL} calling convention is needed to
33276 use a variable defined in a DLL, but when building the DLL, the variable
33277 must have either the @code{Ada} or @code{C} calling convention. As an
33278 example consider a DLL comprising the following package @code{API}:
33279
33280 @smallexample @c ada
33281 @group
33282 @cartouche
33283 package API is
33284 Count : Integer := 0;
33285 @dots{}
33286 -- Remainder of the package omitted.
33287 end API;
33288 @end cartouche
33289 @end group
33290 @end smallexample
33291
33292 @noindent
33293 After producing a DLL containing package @code{API}, the spec that
33294 must be used to import @code{API.Count} from Ada code outside of the
33295 DLL is:
33296
33297 @smallexample @c ada
33298 @group
33299 @cartouche
33300 package API is
33301 Count : Integer;
33302 pragma Import (DLL, Count);
33303 end API;
33304 @end cartouche
33305 @end group
33306 @end smallexample
33307
33308 @node Creating the Definition File
33309 @subsection Creating the Definition File
33310
33311 @noindent
33312 The definition file is the last file needed to build the DLL. It lists
33313 the exported symbols. As an example, the definition file for a DLL
33314 containing only package @code{API} (where all the entities are exported
33315 with a @code{C} calling convention) is:
33316
33317 @smallexample
33318 @group
33319 @cartouche
33320 EXPORTS
33321 count
33322 factorial
33323 finalize_api
33324 initialize_api
33325 @end cartouche
33326 @end group
33327 @end smallexample
33328
33329 @noindent
33330 If the @code{C} calling convention is missing from package @code{API},
33331 then the definition file contains the mangled Ada names of the above
33332 entities, which in this case are:
33333
33334 @smallexample
33335 @group
33336 @cartouche
33337 EXPORTS
33338 api__count
33339 api__factorial
33340 api__finalize_api
33341 api__initialize_api
33342 @end cartouche
33343 @end group
33344 @end smallexample
33345
33346 @node Using gnatdll
33347 @subsection Using @code{gnatdll}
33348 @findex gnatdll
33349
33350 @menu
33351 * gnatdll Example::
33352 * gnatdll behind the Scenes::
33353 * Using dlltool::
33354 @end menu
33355
33356 @noindent
33357 @code{gnatdll} is a tool to automate the DLL build process once all the Ada
33358 and non-Ada sources that make up your DLL have been compiled.
33359 @code{gnatdll} is actually in charge of two distinct tasks: build the
33360 static import library for the DLL and the actual DLL. The form of the
33361 @code{gnatdll} command is
33362
33363 @smallexample
33364 @cartouche
33365 @c $ gnatdll @ovar{switches} @var{list-of-files} @r{[}-largs @var{opts}@r{]}
33366 @c Expanding @ovar macro inline (explanation in macro def comments)
33367 $ gnatdll @r{[}@var{switches}@r{]} @var{list-of-files} @r{[}-largs @var{opts}@r{]}
33368 @end cartouche
33369 @end smallexample
33370
33371 @noindent
33372 where @var{list-of-files} is a list of ALI and object files. The object
33373 file list must be the exact list of objects corresponding to the non-Ada
33374 sources whose services are to be included in the DLL. The ALI file list
33375 must be the exact list of ALI files for the corresponding Ada sources
33376 whose services are to be included in the DLL. If @var{list-of-files} is
33377 missing, only the static import library is generated.
33378
33379 @noindent
33380 You may specify any of the following switches to @code{gnatdll}:
33381
33382 @table @code
33383 @c @item -a@ovar{address}
33384 @c Expanding @ovar macro inline (explanation in macro def comments)
33385 @item -a@r{[}@var{address}@r{]}
33386 @cindex @option{-a} (@code{gnatdll})
33387 Build a non-relocatable DLL at @var{address}. If @var{address} is not
33388 specified the default address @var{0x11000000} will be used. By default,
33389 when this switch is missing, @code{gnatdll} builds relocatable DLL. We
33390 advise the reader to build relocatable DLL.
33391
33392 @item -b @var{address}
33393 @cindex @option{-b} (@code{gnatdll})
33394 Set the relocatable DLL base address. By default the address is
33395 @code{0x11000000}.
33396
33397 @item -bargs @var{opts}
33398 @cindex @option{-bargs} (@code{gnatdll})
33399 Binder options. Pass @var{opts} to the binder.
33400
33401 @item -d @var{dllfile}
33402 @cindex @option{-d} (@code{gnatdll})
33403 @var{dllfile} is the name of the DLL. This switch must be present for
33404 @code{gnatdll} to do anything. The name of the generated import library is
33405 obtained algorithmically from @var{dllfile} as shown in the following
33406 example: if @var{dllfile} is @code{xyz.dll}, the import library name is
33407 @code{libxyz.dll.a}. The name of the definition file to use (if not specified
33408 by option @option{-e}) is obtained algorithmically from @var{dllfile}
33409 as shown in the following example:
33410 if @var{dllfile} is @code{xyz.dll}, the definition
33411 file used is @code{xyz.def}.
33412
33413 @item -e @var{deffile}
33414 @cindex @option{-e} (@code{gnatdll})
33415 @var{deffile} is the name of the definition file.
33416
33417 @item -g
33418 @cindex @option{-g} (@code{gnatdll})
33419 Generate debugging information. This information is stored in the object
33420 file and copied from there to the final DLL file by the linker,
33421 where it can be read by the debugger. You must use the
33422 @option{-g} switch if you plan on using the debugger or the symbolic
33423 stack traceback.
33424
33425 @item -h
33426 @cindex @option{-h} (@code{gnatdll})
33427 Help mode. Displays @code{gnatdll} switch usage information.
33428
33429 @item -Idir
33430 @cindex @option{-I} (@code{gnatdll})
33431 Direct @code{gnatdll} to search the @var{dir} directory for source and
33432 object files needed to build the DLL.
33433 (@pxref{Search Paths and the Run-Time Library (RTL)}).
33434
33435 @item -k
33436 @cindex @option{-k} (@code{gnatdll})
33437 Removes the @code{@@}@var{nn} suffix from the import library's exported
33438 names, but keeps them for the link names. You must specify this
33439 option if you want to use a @code{Stdcall} function in a DLL for which
33440 the @code{@@}@var{nn} suffix has been removed. This is the case for most
33441 of the Windows NT DLL for example. This option has no effect when
33442 @option{-n} option is specified.
33443
33444 @item -l @var{file}
33445 @cindex @option{-l} (@code{gnatdll})
33446 The list of ALI and object files used to build the DLL are listed in
33447 @var{file}, instead of being given in the command line. Each line in
33448 @var{file} contains the name of an ALI or object file.
33449
33450 @item -n
33451 @cindex @option{-n} (@code{gnatdll})
33452 No Import. Do not create the import library.
33453
33454 @item -q
33455 @cindex @option{-q} (@code{gnatdll})
33456 Quiet mode. Do not display unnecessary messages.
33457
33458 @item -v
33459 @cindex @option{-v} (@code{gnatdll})
33460 Verbose mode. Display extra information.
33461
33462 @item -largs @var{opts}
33463 @cindex @option{-largs} (@code{gnatdll})
33464 Linker options. Pass @var{opts} to the linker.
33465 @end table
33466
33467 @node gnatdll Example
33468 @subsubsection @code{gnatdll} Example
33469
33470 @noindent
33471 As an example the command to build a relocatable DLL from @file{api.adb}
33472 once @file{api.adb} has been compiled and @file{api.def} created is
33473
33474 @smallexample
33475 $ gnatdll -d api.dll api.ali
33476 @end smallexample
33477
33478 @noindent
33479 The above command creates two files: @file{libapi.dll.a} (the import
33480 library) and @file{api.dll} (the actual DLL). If you want to create
33481 only the DLL, just type:
33482
33483 @smallexample
33484 $ gnatdll -d api.dll -n api.ali
33485 @end smallexample
33486
33487 @noindent
33488 Alternatively if you want to create just the import library, type:
33489
33490 @smallexample
33491 $ gnatdll -d api.dll
33492 @end smallexample
33493
33494 @node gnatdll behind the Scenes
33495 @subsubsection @code{gnatdll} behind the Scenes
33496
33497 @noindent
33498 This section details the steps involved in creating a DLL. @code{gnatdll}
33499 does these steps for you. Unless you are interested in understanding what
33500 goes on behind the scenes, you should skip this section.
33501
33502 We use the previous example of a DLL containing the Ada package @code{API},
33503 to illustrate the steps necessary to build a DLL. The starting point is a
33504 set of objects that will make up the DLL and the corresponding ALI
33505 files. In the case of this example this means that @file{api.o} and
33506 @file{api.ali} are available. To build a relocatable DLL, @code{gnatdll} does
33507 the following:
33508
33509 @enumerate
33510 @item
33511 @code{gnatdll} builds the base file (@file{api.base}). A base file gives
33512 the information necessary to generate relocation information for the
33513 DLL.
33514
33515 @smallexample
33516 @group
33517 $ gnatbind -n api
33518 $ gnatlink api -o api.jnk -mdll -Wl,--base-file,api.base
33519 @end group
33520 @end smallexample
33521
33522 @noindent
33523 In addition to the base file, the @command{gnatlink} command generates an
33524 output file @file{api.jnk} which can be discarded. The @option{-mdll} switch
33525 asks @command{gnatlink} to generate the routines @code{DllMain} and
33526 @code{DllMainCRTStartup} that are called by the Windows loader when the DLL
33527 is loaded into memory.
33528
33529 @item
33530 @code{gnatdll} uses @code{dlltool} (@pxref{Using dlltool}) to build the
33531 export table (@file{api.exp}). The export table contains the relocation
33532 information in a form which can be used during the final link to ensure
33533 that the Windows loader is able to place the DLL anywhere in memory.
33534
33535 @smallexample
33536 @group
33537 $ dlltool --dllname api.dll --def api.def --base-file api.base \
33538 --output-exp api.exp
33539 @end group
33540 @end smallexample
33541
33542 @item
33543 @code{gnatdll} builds the base file using the new export table. Note that
33544 @command{gnatbind} must be called once again since the binder generated file
33545 has been deleted during the previous call to @command{gnatlink}.
33546
33547 @smallexample
33548 @group
33549 $ gnatbind -n api
33550 $ gnatlink api -o api.jnk api.exp -mdll
33551 -Wl,--base-file,api.base
33552 @end group
33553 @end smallexample
33554
33555 @item
33556 @code{gnatdll} builds the new export table using the new base file and
33557 generates the DLL import library @file{libAPI.dll.a}.
33558
33559 @smallexample
33560 @group
33561 $ dlltool --dllname api.dll --def api.def --base-file api.base \
33562 --output-exp api.exp --output-lib libAPI.a
33563 @end group
33564 @end smallexample
33565
33566 @item
33567 Finally @code{gnatdll} builds the relocatable DLL using the final export
33568 table.
33569
33570 @smallexample
33571 @group
33572 $ gnatbind -n api
33573 $ gnatlink api api.exp -o api.dll -mdll
33574 @end group
33575 @end smallexample
33576 @end enumerate
33577
33578 @node Using dlltool
33579 @subsubsection Using @code{dlltool}
33580
33581 @noindent
33582 @code{dlltool} is the low-level tool used by @code{gnatdll} to build
33583 DLLs and static import libraries. This section summarizes the most
33584 common @code{dlltool} switches. The form of the @code{dlltool} command
33585 is
33586
33587 @smallexample
33588 @c $ dlltool @ovar{switches}
33589 @c Expanding @ovar macro inline (explanation in macro def comments)
33590 $ dlltool @r{[}@var{switches}@r{]}
33591 @end smallexample
33592
33593 @noindent
33594 @code{dlltool} switches include:
33595
33596 @table @option
33597 @item --base-file @var{basefile}
33598 @cindex @option{--base-file} (@command{dlltool})
33599 Read the base file @var{basefile} generated by the linker. This switch
33600 is used to create a relocatable DLL.
33601
33602 @item --def @var{deffile}
33603 @cindex @option{--def} (@command{dlltool})
33604 Read the definition file.
33605
33606 @item --dllname @var{name}
33607 @cindex @option{--dllname} (@command{dlltool})
33608 Gives the name of the DLL. This switch is used to embed the name of the
33609 DLL in the static import library generated by @code{dlltool} with switch
33610 @option{--output-lib}.
33611
33612 @item -k
33613 @cindex @option{-k} (@command{dlltool})
33614 Kill @code{@@}@var{nn} from exported names
33615 (@pxref{Windows Calling Conventions}
33616 for a discussion about @code{Stdcall}-style symbols.
33617
33618 @item --help
33619 @cindex @option{--help} (@command{dlltool})
33620 Prints the @code{dlltool} switches with a concise description.
33621
33622 @item --output-exp @var{exportfile}
33623 @cindex @option{--output-exp} (@command{dlltool})
33624 Generate an export file @var{exportfile}. The export file contains the
33625 export table (list of symbols in the DLL) and is used to create the DLL.
33626
33627 @item --output-lib @var{libfile}
33628 @cindex @option{--output-lib} (@command{dlltool})
33629 Generate a static import library @var{libfile}.
33630
33631 @item -v
33632 @cindex @option{-v} (@command{dlltool})
33633 Verbose mode.
33634
33635 @item --as @var{assembler-name}
33636 @cindex @option{--as} (@command{dlltool})
33637 Use @var{assembler-name} as the assembler. The default is @code{as}.
33638 @end table
33639
33640 @node GNAT and Windows Resources
33641 @section GNAT and Windows Resources
33642 @cindex Resources, windows
33643
33644 @menu
33645 * Building Resources::
33646 * Compiling Resources::
33647 * Using Resources::
33648 @end menu
33649
33650 @noindent
33651 Resources are an easy way to add Windows specific objects to your
33652 application. The objects that can be added as resources include:
33653
33654 @itemize @bullet
33655 @item
33656 menus
33657
33658 @item
33659 accelerators
33660
33661 @item
33662 dialog boxes
33663
33664 @item
33665 string tables
33666
33667 @item
33668 bitmaps
33669
33670 @item
33671 cursors
33672
33673 @item
33674 icons
33675
33676 @item
33677 fonts
33678 @end itemize
33679
33680 @noindent
33681 This section explains how to build, compile and use resources.
33682
33683 @node Building Resources
33684 @subsection Building Resources
33685 @cindex Resources, building
33686
33687 @noindent
33688 A resource file is an ASCII file. By convention resource files have an
33689 @file{.rc} extension.
33690 The easiest way to build a resource file is to use Microsoft tools
33691 such as @code{imagedit.exe} to build bitmaps, icons and cursors and
33692 @code{dlgedit.exe} to build dialogs.
33693 It is always possible to build an @file{.rc} file yourself by writing a
33694 resource script.
33695
33696 It is not our objective to explain how to write a resource file. A
33697 complete description of the resource script language can be found in the
33698 Microsoft documentation.
33699
33700 @node Compiling Resources
33701 @subsection Compiling Resources
33702 @findex rc
33703 @findex windres
33704 @cindex Resources, compiling
33705
33706 @noindent
33707 This section describes how to build a GNAT-compatible (COFF) object file
33708 containing the resources. This is done using the Resource Compiler
33709 @code{windres} as follows:
33710
33711 @smallexample
33712 $ windres -i myres.rc -o myres.o
33713 @end smallexample
33714
33715 @noindent
33716 By default @code{windres} will run @command{gcc} to preprocess the @file{.rc}
33717 file. You can specify an alternate preprocessor (usually named
33718 @file{cpp.exe}) using the @code{windres} @option{--preprocessor}
33719 parameter. A list of all possible options may be obtained by entering
33720 the command @code{windres} @option{--help}.
33721
33722 It is also possible to use the Microsoft resource compiler @code{rc.exe}
33723 to produce a @file{.res} file (binary resource file). See the
33724 corresponding Microsoft documentation for further details. In this case
33725 you need to use @code{windres} to translate the @file{.res} file to a
33726 GNAT-compatible object file as follows:
33727
33728 @smallexample
33729 $ windres -i myres.res -o myres.o
33730 @end smallexample
33731
33732 @node Using Resources
33733 @subsection Using Resources
33734 @cindex Resources, using
33735
33736 @noindent
33737 To include the resource file in your program just add the
33738 GNAT-compatible object file for the resource(s) to the linker
33739 arguments. With @command{gnatmake} this is done by using the @option{-largs}
33740 option:
33741
33742 @smallexample
33743 $ gnatmake myprog -largs myres.o
33744 @end smallexample
33745
33746 @node Debugging a DLL
33747 @section Debugging a DLL
33748 @cindex DLL debugging
33749
33750 @menu
33751 * Program and DLL Both Built with GCC/GNAT::
33752 * Program Built with Foreign Tools and DLL Built with GCC/GNAT::
33753 @end menu
33754
33755 @noindent
33756 Debugging a DLL is similar to debugging a standard program. But
33757 we have to deal with two different executable parts: the DLL and the
33758 program that uses it. We have the following four possibilities:
33759
33760 @enumerate 1
33761 @item
33762 The program and the DLL are built with @code{GCC/GNAT}.
33763 @item
33764 The program is built with foreign tools and the DLL is built with
33765 @code{GCC/GNAT}.
33766 @item
33767 The program is built with @code{GCC/GNAT} and the DLL is built with
33768 foreign tools.
33769 @item
33770 @end enumerate
33771
33772 @noindent
33773 In this section we address only cases one and two above.
33774 There is no point in trying to debug
33775 a DLL with @code{GNU/GDB}, if there is no GDB-compatible debugging
33776 information in it. To do so you must use a debugger compatible with the
33777 tools suite used to build the DLL.
33778
33779 @node Program and DLL Both Built with GCC/GNAT
33780 @subsection Program and DLL Both Built with GCC/GNAT
33781
33782 @noindent
33783 This is the simplest case. Both the DLL and the program have @code{GDB}
33784 compatible debugging information. It is then possible to break anywhere in
33785 the process. Let's suppose here that the main procedure is named
33786 @code{ada_main} and that in the DLL there is an entry point named
33787 @code{ada_dll}.
33788
33789 @noindent
33790 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) and
33791 program must have been built with the debugging information (see GNAT -g
33792 switch). Here are the step-by-step instructions for debugging it:
33793
33794 @enumerate 1
33795 @item Launch @code{GDB} on the main program.
33796
33797 @smallexample
33798 $ gdb -nw ada_main
33799 @end smallexample
33800
33801 @item Start the program and stop at the beginning of the main procedure
33802
33803 @smallexample
33804 (gdb) start
33805 @end smallexample
33806
33807 @noindent
33808 This step is required to be able to set a breakpoint inside the DLL. As long
33809 as the program is not run, the DLL is not loaded. This has the
33810 consequence that the DLL debugging information is also not loaded, so it is not
33811 possible to set a breakpoint in the DLL.
33812
33813 @item Set a breakpoint inside the DLL
33814
33815 @smallexample
33816 (gdb) break ada_dll
33817 (gdb) cont
33818 @end smallexample
33819
33820 @end enumerate
33821
33822 @noindent
33823 At this stage a breakpoint is set inside the DLL. From there on
33824 you can use the standard approach to debug the whole program
33825 (@pxref{Running and Debugging Ada Programs}).
33826
33827 @ignore
33828 @c This used to work, probably because the DLLs were non-relocatable
33829 @c keep this section around until the problem is sorted out.
33830
33831 To break on the @code{DllMain} routine it is not possible to follow
33832 the procedure above. At the time the program stop on @code{ada_main}
33833 the @code{DllMain} routine as already been called. Either you can use
33834 the procedure below @pxref{Debugging the DLL Directly} or this procedure:
33835
33836 @enumerate 1
33837 @item Launch @code{GDB} on the main program.
33838
33839 @smallexample
33840 $ gdb ada_main
33841 @end smallexample
33842
33843 @item Load DLL symbols
33844
33845 @smallexample
33846 (gdb) add-sym api.dll
33847 @end smallexample
33848
33849 @item Set a breakpoint inside the DLL
33850
33851 @smallexample
33852 (gdb) break ada_dll.adb:45
33853 @end smallexample
33854
33855 Note that at this point it is not possible to break using the routine symbol
33856 directly as the program is not yet running. The solution is to break
33857 on the proper line (break in @file{ada_dll.adb} line 45).
33858
33859 @item Start the program
33860
33861 @smallexample
33862 (gdb) run
33863 @end smallexample
33864
33865 @end enumerate
33866 @end ignore
33867
33868 @node Program Built with Foreign Tools and DLL Built with GCC/GNAT
33869 @subsection Program Built with Foreign Tools and DLL Built with GCC/GNAT
33870
33871 @menu
33872 * Debugging the DLL Directly::
33873 * Attaching to a Running Process::
33874 @end menu
33875
33876 @noindent
33877 In this case things are slightly more complex because it is not possible to
33878 start the main program and then break at the beginning to load the DLL and the
33879 associated DLL debugging information. It is not possible to break at the
33880 beginning of the program because there is no @code{GDB} debugging information,
33881 and therefore there is no direct way of getting initial control. This
33882 section addresses this issue by describing some methods that can be used
33883 to break somewhere in the DLL to debug it.
33884
33885 @noindent
33886 First suppose that the main procedure is named @code{main} (this is for
33887 example some C code built with Microsoft Visual C) and that there is a
33888 DLL named @code{test.dll} containing an Ada entry point named
33889 @code{ada_dll}.
33890
33891 @noindent
33892 The DLL (@pxref{Introduction to Dynamic Link Libraries (DLLs)}) must have
33893 been built with debugging information (see GNAT -g option).
33894
33895 @node Debugging the DLL Directly
33896 @subsubsection Debugging the DLL Directly
33897
33898 @enumerate 1
33899 @item
33900 Find out the executable starting address
33901
33902 @smallexample
33903 $ objdump --file-header main.exe
33904 @end smallexample
33905
33906 The starting address is reported on the last line. For example:
33907
33908 @smallexample
33909 main.exe: file format pei-i386
33910 architecture: i386, flags 0x0000010a:
33911 EXEC_P, HAS_DEBUG, D_PAGED
33912 start address 0x00401010
33913 @end smallexample
33914
33915 @item
33916 Launch the debugger on the executable.
33917
33918 @smallexample
33919 $ gdb main.exe
33920 @end smallexample
33921
33922 @item
33923 Set a breakpoint at the starting address, and launch the program.
33924
33925 @smallexample
33926 $ (gdb) break *0x00401010
33927 $ (gdb) run
33928 @end smallexample
33929
33930 The program will stop at the given address.
33931
33932 @item
33933 Set a breakpoint on a DLL subroutine.
33934
33935 @smallexample
33936 (gdb) break ada_dll.adb:45
33937 @end smallexample
33938
33939 Or if you want to break using a symbol on the DLL, you need first to
33940 select the Ada language (language used by the DLL).
33941
33942 @smallexample
33943 (gdb) set language ada
33944 (gdb) break ada_dll
33945 @end smallexample
33946
33947 @item
33948 Continue the program.
33949
33950 @smallexample
33951 (gdb) cont
33952 @end smallexample
33953
33954 @noindent
33955 This will run the program until it reaches the breakpoint that has been
33956 set. From that point you can use the standard way to debug a program
33957 as described in (@pxref{Running and Debugging Ada Programs}).
33958
33959 @end enumerate
33960
33961 @noindent
33962 It is also possible to debug the DLL by attaching to a running process.
33963
33964 @node Attaching to a Running Process
33965 @subsubsection Attaching to a Running Process
33966 @cindex DLL debugging, attach to process
33967
33968 @noindent
33969 With @code{GDB} it is always possible to debug a running process by
33970 attaching to it. It is possible to debug a DLL this way. The limitation
33971 of this approach is that the DLL must run long enough to perform the
33972 attach operation. It may be useful for instance to insert a time wasting
33973 loop in the code of the DLL to meet this criterion.
33974
33975 @enumerate 1
33976
33977 @item Launch the main program @file{main.exe}.
33978
33979 @smallexample
33980 $ main
33981 @end smallexample
33982
33983 @item Use the Windows @i{Task Manager} to find the process ID. Let's say
33984 that the process PID for @file{main.exe} is 208.
33985
33986 @item Launch gdb.
33987
33988 @smallexample
33989 $ gdb
33990 @end smallexample
33991
33992 @item Attach to the running process to be debugged.
33993
33994 @smallexample
33995 (gdb) attach 208
33996 @end smallexample
33997
33998 @item Load the process debugging information.
33999
34000 @smallexample
34001 (gdb) symbol-file main.exe
34002 @end smallexample
34003
34004 @item Break somewhere in the DLL.
34005
34006 @smallexample
34007 (gdb) break ada_dll
34008 @end smallexample
34009
34010 @item Continue process execution.
34011
34012 @smallexample
34013 (gdb) cont
34014 @end smallexample
34015
34016 @end enumerate
34017
34018 @noindent
34019 This last step will resume the process execution, and stop at
34020 the breakpoint we have set. From there you can use the standard
34021 approach to debug a program as described in
34022 (@pxref{Running and Debugging Ada Programs}).
34023
34024 @node Setting Stack Size from gnatlink
34025 @section Setting Stack Size from @command{gnatlink}
34026
34027 @noindent
34028 It is possible to specify the program stack size at link time. On modern
34029 versions of Windows, starting with XP, this is mostly useful to set the size of
34030 the main stack (environment task). The other task stacks are set with pragma
34031 Storage_Size or with the @command{gnatbind -d} command.
34032
34033 Since older versions of Windows (2000, NT4, etc.) do not allow setting the
34034 reserve size of individual tasks, the link-time stack size applies to all
34035 tasks, and pragma Storage_Size has no effect.
34036 In particular, Stack Overflow checks are made against this
34037 link-time specified size.
34038
34039 This setting can be done with
34040 @command{gnatlink} using either:
34041
34042 @itemize @bullet
34043
34044 @item using @option{-Xlinker} linker option
34045
34046 @smallexample
34047 $ gnatlink hello -Xlinker --stack=0x10000,0x1000
34048 @end smallexample
34049
34050 This sets the stack reserve size to 0x10000 bytes and the stack commit
34051 size to 0x1000 bytes.
34052
34053 @item using @option{-Wl} linker option
34054
34055 @smallexample
34056 $ gnatlink hello -Wl,--stack=0x1000000
34057 @end smallexample
34058
34059 This sets the stack reserve size to 0x1000000 bytes. Note that with
34060 @option{-Wl} option it is not possible to set the stack commit size
34061 because the coma is a separator for this option.
34062
34063 @end itemize
34064
34065 @node Setting Heap Size from gnatlink
34066 @section Setting Heap Size from @command{gnatlink}
34067
34068 @noindent
34069 Under Windows systems, it is possible to specify the program heap size from
34070 @command{gnatlink} using either:
34071
34072 @itemize @bullet
34073
34074 @item using @option{-Xlinker} linker option
34075
34076 @smallexample
34077 $ gnatlink hello -Xlinker --heap=0x10000,0x1000
34078 @end smallexample
34079
34080 This sets the heap reserve size to 0x10000 bytes and the heap commit
34081 size to 0x1000 bytes.
34082
34083 @item using @option{-Wl} linker option
34084
34085 @smallexample
34086 $ gnatlink hello -Wl,--heap=0x1000000
34087 @end smallexample
34088
34089 This sets the heap reserve size to 0x1000000 bytes. Note that with
34090 @option{-Wl} option it is not possible to set the heap commit size
34091 because the coma is a separator for this option.
34092
34093 @end itemize
34094
34095 @end ifset
34096
34097 @c **********************************
34098 @c * GNU Free Documentation License *
34099 @c **********************************
34100 @include fdl.texi
34101 @c GNU Free Documentation License
34102
34103 @node Index,,GNU Free Documentation License, Top
34104 @unnumbered Index
34105
34106 @printindex cp
34107
34108 @contents
34109 @c Put table of contents at end, otherwise it precedes the "title page" in
34110 @c the .txt version
34111 @c Edit the pdf file to move the contents to the beginning, after the title
34112 @c page
34113
34114 @bye